uio: fix vma io range check in mmap
[deliverable/linux.git] / drivers / misc / mei / hbm.c
CommitLineData
bb1b0133
TW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
4fcbc99b 17#include <linux/export.h>
bb1b0133
TW
18#include <linux/pci.h>
19#include <linux/sched.h>
20#include <linux/wait.h>
21#include <linux/mei.h>
180ea05b 22#include <linux/pm_runtime.h>
bb1b0133
TW
23
24#include "mei_dev.h"
0edb23fc 25#include "hbm.h"
12d00665 26#include "client.h"
bb1b0133 27
285e2996
AU
28static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
29{
30#define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
31 switch (status) {
32 MEI_CL_CS(SUCCESS);
33 MEI_CL_CS(NOT_FOUND);
34 MEI_CL_CS(ALREADY_STARTED);
35 MEI_CL_CS(OUT_OF_RESOURCES);
36 MEI_CL_CS(MESSAGE_SMALL);
37 default: return "unknown";
38 }
39#undef MEI_CL_CCS
40}
41
42/**
43 * mei_cl_conn_status_to_errno - convert client connect response
44 * status to error code
45 *
46 * @status: client connect response status
47 *
48 * returns corresponding error code
49 */
50static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
51{
52 switch (status) {
53 case MEI_CL_CONN_SUCCESS: return 0;
54 case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
55 case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
56 case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
57 case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
58 default: return -EINVAL;
59 }
60}
61
a40b260d
TW
62/**
63 * mei_hbm_me_cl_allocate - allocates storage for me clients
64 *
65 * @dev: the device structure
66 *
544f9460 67 * returns 0 on success -ENOMEM on allocation failure
a40b260d 68 */
544f9460 69static int mei_hbm_me_cl_allocate(struct mei_device *dev)
a40b260d
TW
70{
71 struct mei_me_client *clients;
72 int b;
73
1aee351a
TW
74 dev->me_clients_num = 0;
75 dev->me_client_presentation_num = 0;
76 dev->me_client_index = 0;
77
a40b260d
TW
78 /* count how many ME clients we have */
79 for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
80 dev->me_clients_num++;
81
1aee351a 82 if (dev->me_clients_num == 0)
544f9460 83 return 0;
a40b260d
TW
84
85 kfree(dev->me_clients);
86 dev->me_clients = NULL;
87
e19555ce 88 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%ld.\n",
a40b260d
TW
89 dev->me_clients_num * sizeof(struct mei_me_client));
90 /* allocate storage for ME clients representation */
91 clients = kcalloc(dev->me_clients_num,
92 sizeof(struct mei_me_client), GFP_KERNEL);
93 if (!clients) {
94 dev_err(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
544f9460 95 return -ENOMEM;
a40b260d
TW
96 }
97 dev->me_clients = clients;
544f9460 98 return 0;
a40b260d
TW
99}
100
cd51ed64
TW
101/**
102 * mei_hbm_cl_hdr - construct client hbm header
393b148f 103 *
cd51ed64
TW
104 * @cl: - client
105 * @hbm_cmd: host bus message command
106 * @buf: buffer for cl header
107 * @len: buffer length
108 */
109static inline
110void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
111{
112 struct mei_hbm_cl_cmd *cmd = buf;
113
114 memset(cmd, 0, len);
115
116 cmd->hbm_cmd = hbm_cmd;
117 cmd->host_addr = cl->host_client_id;
118 cmd->me_addr = cl->me_client_id;
119}
120
121/**
83ce0741 122 * mei_hbm_cl_addr_equal - tells if they have the same address
cd51ed64 123 *
83ce0741
AU
124 * @cl: - client
125 * @buf: buffer with cl header
cd51ed64 126 *
83ce0741 127 * returns true if addresses are the same
cd51ed64
TW
128 */
129static inline
130bool mei_hbm_cl_addr_equal(struct mei_cl *cl, void *buf)
131{
132 struct mei_hbm_cl_cmd *cmd = buf;
133 return cl->host_client_id == cmd->host_addr &&
134 cl->me_client_id == cmd->me_addr;
135}
136
137
66ae460b
TW
138/**
139 * mei_hbm_idle - set hbm to idle state
140 *
141 * @dev: the device structure
142 */
143void mei_hbm_idle(struct mei_device *dev)
144{
145 dev->init_clients_timer = 0;
146 dev->hbm_state = MEI_HBM_IDLE;
147}
148
9b0d5efc
TW
149int mei_hbm_start_wait(struct mei_device *dev)
150{
151 int ret;
152 if (dev->hbm_state > MEI_HBM_START)
153 return 0;
154
155 mutex_unlock(&dev->device_lock);
156 ret = wait_event_interruptible_timeout(dev->wait_recvd_msg,
157 dev->hbm_state == MEI_HBM_IDLE ||
544f9460 158 dev->hbm_state >= MEI_HBM_STARTED,
7d93e58d 159 mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
9b0d5efc
TW
160 mutex_lock(&dev->device_lock);
161
162 if (ret <= 0 && (dev->hbm_state <= MEI_HBM_START)) {
163 dev->hbm_state = MEI_HBM_IDLE;
8b513d0c 164 dev_err(&dev->pdev->dev, "waiting for mei start failed\n");
7ca96aa2 165 return -ETIME;
9b0d5efc
TW
166 }
167 return 0;
168}
169
bb1b0133 170/**
8120e720 171 * mei_hbm_start_req - sends start request message.
bb1b0133
TW
172 *
173 * @dev: the device structure
544f9460
TW
174 *
175 * returns 0 on success and < 0 on failure
bb1b0133 176 */
9b0d5efc 177int mei_hbm_start_req(struct mei_device *dev)
bb1b0133 178{
e46f1874 179 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
bb1b0133
TW
180 struct hbm_host_version_request *start_req;
181 const size_t len = sizeof(struct hbm_host_version_request);
544f9460 182 int ret;
bb1b0133 183
e46f1874 184 mei_hbm_hdr(mei_hdr, len);
bb1b0133
TW
185
186 /* host start message */
e46f1874 187 start_req = (struct hbm_host_version_request *)dev->wr_msg.data;
bb1b0133
TW
188 memset(start_req, 0, len);
189 start_req->hbm_cmd = HOST_START_REQ_CMD;
190 start_req->host_version.major_version = HBM_MAJOR_VERSION;
191 start_req->host_version.minor_version = HBM_MINOR_VERSION;
192
9b0d5efc 193 dev->hbm_state = MEI_HBM_IDLE;
544f9460
TW
194 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
195 if (ret) {
196 dev_err(&dev->pdev->dev, "version message write failed: ret = %d\n",
197 ret);
198 return ret;
bb1b0133 199 }
544f9460 200
9b0d5efc 201 dev->hbm_state = MEI_HBM_START;
bb1b0133 202 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
9b0d5efc 203 return 0;
bb1b0133
TW
204}
205
9b0d5efc 206/*
8120e720 207 * mei_hbm_enum_clients_req - sends enumeration client request message.
bb1b0133
TW
208 *
209 * @dev: the device structure
210 *
544f9460 211 * returns 0 on success and < 0 on failure
bb1b0133 212 */
544f9460 213static int mei_hbm_enum_clients_req(struct mei_device *dev)
bb1b0133 214{
e46f1874 215 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
bb1b0133
TW
216 struct hbm_host_enum_request *enum_req;
217 const size_t len = sizeof(struct hbm_host_enum_request);
544f9460
TW
218 int ret;
219
bb1b0133 220 /* enumerate clients */
e46f1874 221 mei_hbm_hdr(mei_hdr, len);
bb1b0133 222
e46f1874
TW
223 enum_req = (struct hbm_host_enum_request *)dev->wr_msg.data;
224 memset(enum_req, 0, len);
bb1b0133
TW
225 enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
226
544f9460
TW
227 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
228 if (ret) {
229 dev_err(&dev->pdev->dev, "enumeration request write failed: ret = %d.\n",
230 ret);
231 return ret;
bb1b0133 232 }
9b0d5efc 233 dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
bb1b0133 234 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
544f9460 235 return 0;
bb1b0133
TW
236}
237
8120e720 238/**
393b148f 239 * mei_hbm_prop_req - request property for a single client
8120e720
TW
240 *
241 * @dev: the device structure
242 *
544f9460 243 * returns 0 on success and < 0 on failure
8120e720 244 */
bb1b0133 245
8120e720 246static int mei_hbm_prop_req(struct mei_device *dev)
bb1b0133
TW
247{
248
e46f1874 249 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
bb1b0133
TW
250 struct hbm_props_request *prop_req;
251 const size_t len = sizeof(struct hbm_props_request);
252 unsigned long next_client_index;
1aee351a 253 unsigned long client_num;
544f9460 254 int ret;
bb1b0133
TW
255
256 client_num = dev->me_client_presentation_num;
257
258 next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX,
259 dev->me_client_index);
260
261 /* We got all client properties */
262 if (next_client_index == MEI_CLIENTS_MAX) {
9b0d5efc 263 dev->hbm_state = MEI_HBM_STARTED;
bb1b0133
TW
264 schedule_work(&dev->init_work);
265
266 return 0;
267 }
268
269 dev->me_clients[client_num].client_id = next_client_index;
270 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
271
e46f1874
TW
272 mei_hbm_hdr(mei_hdr, len);
273 prop_req = (struct hbm_props_request *)dev->wr_msg.data;
bb1b0133
TW
274
275 memset(prop_req, 0, sizeof(struct hbm_props_request));
276
277
278 prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
279 prop_req->address = next_client_index;
280
544f9460
TW
281 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
282 if (ret) {
283 dev_err(&dev->pdev->dev, "properties request write failed: ret = %d\n",
284 ret);
285 return ret;
bb1b0133
TW
286 }
287
288 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
289 dev->me_client_index = next_client_index;
290
291 return 0;
292}
293
4fcbc99b
TW
294/*
295 * mei_hbm_pg - sends pg command
296 *
297 * @dev: the device structure
298 * @pg_cmd: the pg command code
299 *
300 * This function returns -EIO on write failure
301 */
302int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
303{
304 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
305 struct hbm_power_gate *req;
306 const size_t len = sizeof(struct hbm_power_gate);
307 int ret;
308
309 mei_hbm_hdr(mei_hdr, len);
310
311 req = (struct hbm_power_gate *)dev->wr_msg.data;
312 memset(req, 0, len);
313 req->hbm_cmd = pg_cmd;
314
315 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
316 if (ret)
317 dev_err(&dev->pdev->dev, "power gate command write failed.\n");
318 return ret;
319}
320EXPORT_SYMBOL_GPL(mei_hbm_pg);
321
e46f1874 322/**
6bb948c9 323 * mei_hbm_stop_req - send stop request message
e46f1874
TW
324 *
325 * @dev - mei device
6bb948c9
TW
326 * @cl: client info
327 *
328 * This function returns -EIO on write failure
e46f1874 329 */
6bb948c9 330static int mei_hbm_stop_req(struct mei_device *dev)
e46f1874 331{
6bb948c9 332 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
e46f1874 333 struct hbm_host_stop_request *req =
6bb948c9 334 (struct hbm_host_stop_request *)dev->wr_msg.data;
e46f1874
TW
335 const size_t len = sizeof(struct hbm_host_stop_request);
336
337 mei_hbm_hdr(mei_hdr, len);
338
339 memset(req, 0, len);
340 req->hbm_cmd = HOST_STOP_REQ_CMD;
341 req->reason = DRIVER_STOP_REQUEST;
6bb948c9
TW
342
343 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
e46f1874
TW
344}
345
bb1b0133 346/**
83ce0741 347 * mei_hbm_cl_flow_control_req - sends flow control request.
bb1b0133
TW
348 *
349 * @dev: the device structure
8120e720 350 * @cl: client info
bb1b0133
TW
351 *
352 * This function returns -EIO on write failure
353 */
8120e720 354int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
bb1b0133 355{
e46f1874 356 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
bb1b0133
TW
357 const size_t len = sizeof(struct hbm_flow_control);
358
e46f1874
TW
359 mei_hbm_hdr(mei_hdr, len);
360 mei_hbm_cl_hdr(cl, MEI_FLOW_CONTROL_CMD, dev->wr_msg.data, len);
bb1b0133 361
46922186 362 cl_dbg(dev, cl, "sending flow control\n");
bb1b0133 363
e46f1874 364 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
bb1b0133
TW
365}
366
6bbda15f 367/**
393b148f 368 * mei_hbm_add_single_flow_creds - adds single buffer credentials.
6bbda15f 369 *
393b148f 370 * @dev: the device structure
6bbda15f 371 * @flow: flow control.
12d00665
AU
372 *
373 * return 0 on success, < 0 otherwise
6bbda15f 374 */
12d00665 375static int mei_hbm_add_single_flow_creds(struct mei_device *dev,
6bbda15f
TW
376 struct hbm_flow_control *flow)
377{
12d00665
AU
378 struct mei_me_client *me_cl;
379 int id;
380
381 id = mei_me_cl_by_id(dev, flow->me_addr);
382 if (id < 0) {
383 dev_err(&dev->pdev->dev, "no such me client %d\n",
384 flow->me_addr);
385 return id;
386 }
387
388 me_cl = &dev->me_clients[id];
389 if (me_cl->props.single_recv_buf) {
390 me_cl->mei_flow_ctrl_creds++;
391 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
392 flow->me_addr);
393 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
394 me_cl->mei_flow_ctrl_creds);
395 } else {
396 BUG(); /* error in flow control */
6bbda15f 397 }
12d00665
AU
398
399 return 0;
6bbda15f
TW
400}
401
402/**
403 * mei_hbm_cl_flow_control_res - flow control response from me
404 *
405 * @dev: the device structure
406 * @flow_control: flow control response bus message
407 */
408static void mei_hbm_cl_flow_control_res(struct mei_device *dev,
409 struct hbm_flow_control *flow_control)
410{
31f88f57 411 struct mei_cl *cl;
6bbda15f
TW
412
413 if (!flow_control->host_addr) {
414 /* single receive buffer */
415 mei_hbm_add_single_flow_creds(dev, flow_control);
416 return;
417 }
418
419 /* normal connection */
31f88f57 420 list_for_each_entry(cl, &dev->file_list, link) {
6bbda15f
TW
421 if (mei_hbm_cl_addr_equal(cl, flow_control)) {
422 cl->mei_flow_ctrl_creds++;
423 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
424 flow_control->host_addr, flow_control->me_addr);
425 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
426 cl->mei_flow_ctrl_creds);
427 break;
428 }
429 }
430}
431
432
bb1b0133 433/**
8120e720 434 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
bb1b0133
TW
435 *
436 * @dev: the device structure
8120e720 437 * @cl: a client to disconnect from
bb1b0133
TW
438 *
439 * This function returns -EIO on write failure
440 */
8120e720 441int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
bb1b0133 442{
e46f1874 443 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
bb1b0133
TW
444 const size_t len = sizeof(struct hbm_client_connect_request);
445
e46f1874
TW
446 mei_hbm_hdr(mei_hdr, len);
447 mei_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, dev->wr_msg.data, len);
bb1b0133 448
e46f1874 449 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
bb1b0133
TW
450}
451
6bb948c9
TW
452/**
453 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
454 *
455 * @dev: the device structure
456 * @cl: a client to disconnect from
457 *
458 * This function returns -EIO on write failure
459 */
460int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
461{
462 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
463 const size_t len = sizeof(struct hbm_client_connect_response);
464
465 mei_hbm_hdr(mei_hdr, len);
466 mei_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, dev->wr_msg.data, len);
467
468 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
469}
470
6bbda15f
TW
471/**
472 * mei_hbm_cl_disconnect_res - disconnect response from ME
473 *
474 * @dev: the device structure
475 * @rs: disconnect response bus message
476 */
477static void mei_hbm_cl_disconnect_res(struct mei_device *dev,
478 struct hbm_client_connect_response *rs)
479{
480 struct mei_cl *cl;
64092858 481 struct mei_cl_cb *cb, *next;
6bbda15f 482
285e2996
AU
483 dev_dbg(&dev->pdev->dev, "hbm: disconnect response cl:host=%02d me=%02d status=%d\n",
484 rs->me_addr, rs->host_addr, rs->status);
6bbda15f 485
64092858
TW
486 list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
487 cl = cb->cl;
6bbda15f 488
64092858
TW
489 /* this should not happen */
490 if (WARN_ON(!cl)) {
491 list_del(&cb->list);
6bbda15f
TW
492 return;
493 }
494
6bbda15f 495 if (mei_hbm_cl_addr_equal(cl, rs)) {
64092858 496 list_del(&cb->list);
285e2996 497 if (rs->status == MEI_CL_DISCONN_SUCCESS)
6bbda15f
TW
498 cl->state = MEI_FILE_DISCONNECTED;
499
500 cl->status = 0;
501 cl->timer_count = 0;
502 break;
503 }
504 }
505}
506
bb1b0133 507/**
8120e720 508 * mei_hbm_cl_connect_req - send connection request to specific me client
bb1b0133
TW
509 *
510 * @dev: the device structure
8120e720 511 * @cl: a client to connect to
bb1b0133 512 *
8120e720 513 * returns -EIO on write failure
bb1b0133 514 */
8120e720 515int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
bb1b0133 516{
e46f1874 517 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
bb1b0133
TW
518 const size_t len = sizeof(struct hbm_client_connect_request);
519
e46f1874
TW
520 mei_hbm_hdr(mei_hdr, len);
521 mei_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, dev->wr_msg.data, len);
bb1b0133 522
e46f1874 523 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
bb1b0133
TW
524}
525
6bbda15f 526/**
83ce0741 527 * mei_hbm_cl_connect_res - connect response from the ME
6bbda15f
TW
528 *
529 * @dev: the device structure
530 * @rs: connect response bus message
531 */
532static void mei_hbm_cl_connect_res(struct mei_device *dev,
533 struct hbm_client_connect_response *rs)
534{
535
536 struct mei_cl *cl;
64092858 537 struct mei_cl_cb *cb, *next;
6bbda15f 538
285e2996
AU
539 dev_dbg(&dev->pdev->dev, "hbm: connect response cl:host=%02d me=%02d status=%s\n",
540 rs->me_addr, rs->host_addr,
541 mei_cl_conn_status_str(rs->status));
6bbda15f 542
64092858 543 cl = NULL;
6bbda15f 544
64092858 545 list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
6bbda15f 546
64092858
TW
547 cl = cb->cl;
548 /* this should not happen */
549 if (WARN_ON(!cl)) {
550 list_del_init(&cb->list);
551 continue;
552 }
6bbda15f 553
64092858
TW
554 if (cb->fop_type != MEI_FOP_CONNECT)
555 continue;
6bbda15f 556
64092858
TW
557 if (mei_hbm_cl_addr_equal(cl, rs)) {
558 list_del(&cb->list);
559 break;
6bbda15f
TW
560 }
561 }
64092858
TW
562
563 if (!cl)
564 return;
565
566 cl->timer_count = 0;
567 if (rs->status == MEI_CL_CONN_SUCCESS)
568 cl->state = MEI_FILE_CONNECTED;
569 else
570 cl->state = MEI_FILE_DISCONNECTED;
571 cl->status = mei_cl_conn_status_to_errno(rs->status);
6bbda15f
TW
572}
573
574
bb1b0133 575/**
83ce0741
AU
576 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
577 * host sends disconnect response
bb1b0133
TW
578 *
579 * @dev: the device structure.
8120e720 580 * @disconnect_req: disconnect request bus message from the me
6bb948c9
TW
581 *
582 * returns -ENOMEM on allocation failure
bb1b0133 583 */
6bb948c9 584static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
bb1b0133
TW
585 struct hbm_client_connect_request *disconnect_req)
586{
31f88f57 587 struct mei_cl *cl;
6bb948c9 588 struct mei_cl_cb *cb;
bb1b0133 589
31f88f57 590 list_for_each_entry(cl, &dev->file_list, link) {
cd51ed64 591 if (mei_hbm_cl_addr_equal(cl, disconnect_req)) {
bb1b0133
TW
592 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
593 disconnect_req->host_addr,
594 disconnect_req->me_addr);
cd51ed64
TW
595 cl->state = MEI_FILE_DISCONNECTED;
596 cl->timer_count = 0;
bb1b0133 597
6bb948c9
TW
598 cb = mei_io_cb_init(cl, NULL);
599 if (!cb)
600 return -ENOMEM;
601 cb->fop_type = MEI_FOP_DISCONNECT_RSP;
602 cl_dbg(dev, cl, "add disconnect response as first\n");
603 list_add(&cb->list, &dev->ctrl_wr_list.list);
604
bb1b0133
TW
605 break;
606 }
607 }
6bb948c9 608 return 0;
bb1b0133
TW
609}
610
611
2c9b48ac
TW
612/**
613 * mei_hbm_version_is_supported - checks whether the driver can
614 * support the hbm version of the device
615 *
616 * @dev: the device structure
617 * returns true if driver can support hbm version of the device
618 */
619bool mei_hbm_version_is_supported(struct mei_device *dev)
620{
621 return (dev->version.major_version < HBM_MAJOR_VERSION) ||
622 (dev->version.major_version == HBM_MAJOR_VERSION &&
623 dev->version.minor_version <= HBM_MINOR_VERSION);
624}
625
bb1b0133
TW
626/**
627 * mei_hbm_dispatch - bottom half read routine after ISR to
628 * handle the read bus message cmd processing.
629 *
630 * @dev: the device structure
631 * @mei_hdr: header of bus message
544f9460
TW
632 *
633 * returns 0 on success and < 0 on failure
bb1b0133 634 */
544f9460 635int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
bb1b0133
TW
636{
637 struct mei_bus_message *mei_msg;
638 struct mei_me_client *me_client;
639 struct hbm_host_version_response *version_res;
640 struct hbm_client_connect_response *connect_res;
641 struct hbm_client_connect_response *disconnect_res;
642 struct hbm_client_connect_request *disconnect_req;
643 struct hbm_flow_control *flow_control;
644 struct hbm_props_response *props_res;
645 struct hbm_host_enum_response *enum_res;
bb1b0133
TW
646
647 /* read the message to our buffer */
648 BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
649 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
650 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
651
66ae460b
TW
652 /* ignore spurious message and prevent reset nesting
653 * hbm is put to idle during system reset
654 */
655 if (dev->hbm_state == MEI_HBM_IDLE) {
656 dev_dbg(&dev->pdev->dev, "hbm: state is idle ignore spurious messages\n");
657 return 0;
658 }
659
bb1b0133
TW
660 switch (mei_msg->hbm_cmd) {
661 case HOST_START_RES_CMD:
544f9460
TW
662 dev_dbg(&dev->pdev->dev, "hbm: start: response message received.\n");
663
664 dev->init_clients_timer = 0;
665
bb1b0133 666 version_res = (struct hbm_host_version_response *)mei_msg;
2c9b48ac
TW
667
668 dev_dbg(&dev->pdev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
669 HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
670 version_res->me_max_version.major_version,
671 version_res->me_max_version.minor_version);
672
673 if (version_res->host_version_supported) {
674 dev->version.major_version = HBM_MAJOR_VERSION;
675 dev->version.minor_version = HBM_MINOR_VERSION;
676 } else {
677 dev->version.major_version =
678 version_res->me_max_version.major_version;
679 dev->version.minor_version =
680 version_res->me_max_version.minor_version;
681 }
682
683 if (!mei_hbm_version_is_supported(dev)) {
544f9460 684 dev_warn(&dev->pdev->dev, "hbm: start: version mismatch - stopping the driver.\n");
bb1b0133 685
544f9460 686 dev->hbm_state = MEI_HBM_STOPPED;
6bb948c9 687 if (mei_hbm_stop_req(dev)) {
544f9460
TW
688 dev_err(&dev->pdev->dev, "hbm: start: failed to send stop request\n");
689 return -EIO;
690 }
691 break;
692 }
9b0d5efc 693
544f9460
TW
694 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
695 dev->hbm_state != MEI_HBM_START) {
696 dev_err(&dev->pdev->dev, "hbm: start: state mismatch, [%d, %d]\n",
697 dev->dev_state, dev->hbm_state);
698 return -EPROTO;
e46f1874 699 }
bb1b0133 700
544f9460
TW
701 dev->hbm_state = MEI_HBM_STARTED;
702
703 if (mei_hbm_enum_clients_req(dev)) {
704 dev_err(&dev->pdev->dev, "hbm: start: failed to send enumeration request\n");
705 return -EIO;
bb1b0133
TW
706 }
707
9b0d5efc 708 wake_up_interruptible(&dev->wait_recvd_msg);
bb1b0133
TW
709 break;
710
711 case CLIENT_CONNECT_RES_CMD:
544f9460
TW
712 dev_dbg(&dev->pdev->dev, "hbm: client connect response: message received.\n");
713
bb1b0133 714 connect_res = (struct hbm_client_connect_response *) mei_msg;
6bbda15f 715 mei_hbm_cl_connect_res(dev, connect_res);
bb1b0133
TW
716 wake_up(&dev->wait_recvd_msg);
717 break;
718
719 case CLIENT_DISCONNECT_RES_CMD:
544f9460
TW
720 dev_dbg(&dev->pdev->dev, "hbm: client disconnect response: message received.\n");
721
bb1b0133 722 disconnect_res = (struct hbm_client_connect_response *) mei_msg;
6bbda15f 723 mei_hbm_cl_disconnect_res(dev, disconnect_res);
bb1b0133
TW
724 wake_up(&dev->wait_recvd_msg);
725 break;
726
727 case MEI_FLOW_CONTROL_CMD:
544f9460
TW
728 dev_dbg(&dev->pdev->dev, "hbm: client flow control response: message received.\n");
729
bb1b0133 730 flow_control = (struct hbm_flow_control *) mei_msg;
6bbda15f 731 mei_hbm_cl_flow_control_res(dev, flow_control);
bb1b0133
TW
732 break;
733
4fcbc99b
TW
734 case MEI_PG_ISOLATION_ENTRY_RES_CMD:
735 dev_dbg(&dev->pdev->dev, "power gate isolation entry response received\n");
ba9cdd0e 736 dev->pg_event = MEI_PG_EVENT_RECEIVED;
4fcbc99b
TW
737 if (waitqueue_active(&dev->wait_pg))
738 wake_up(&dev->wait_pg);
739 break;
740
741 case MEI_PG_ISOLATION_EXIT_REQ_CMD:
742 dev_dbg(&dev->pdev->dev, "power gate isolation exit request received\n");
ba9cdd0e 743 dev->pg_event = MEI_PG_EVENT_RECEIVED;
4fcbc99b
TW
744 if (waitqueue_active(&dev->wait_pg))
745 wake_up(&dev->wait_pg);
180ea05b
TW
746 else
747 /*
748 * If the driver is not waiting on this then
749 * this is HW initiated exit from PG.
750 * Start runtime pm resume sequence to exit from PG.
751 */
752 pm_request_resume(&dev->pdev->dev);
4fcbc99b
TW
753 break;
754
bb1b0133 755 case HOST_CLIENT_PROPERTIES_RES_CMD:
544f9460
TW
756 dev_dbg(&dev->pdev->dev, "hbm: properties response: message received.\n");
757
758 dev->init_clients_timer = 0;
759
760 if (dev->me_clients == NULL) {
761 dev_err(&dev->pdev->dev, "hbm: properties response: mei_clients not allocated\n");
762 return -EPROTO;
763 }
764
bb1b0133
TW
765 props_res = (struct hbm_props_response *)mei_msg;
766 me_client = &dev->me_clients[dev->me_client_presentation_num];
767
544f9460
TW
768 if (props_res->status) {
769 dev_err(&dev->pdev->dev, "hbm: properties response: wrong status = %d\n",
770 props_res->status);
771 return -EPROTO;
bb1b0133
TW
772 }
773
774 if (me_client->client_id != props_res->address) {
544f9460
TW
775 dev_err(&dev->pdev->dev, "hbm: properties response: address mismatch %d ?= %d\n",
776 me_client->client_id, props_res->address);
777 return -EPROTO;
bb1b0133
TW
778 }
779
780 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
9b0d5efc 781 dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
544f9460
TW
782 dev_err(&dev->pdev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
783 dev->dev_state, dev->hbm_state);
784 return -EPROTO;
bb1b0133
TW
785 }
786
787 me_client->props = props_res->client_properties;
788 dev->me_client_index++;
789 dev->me_client_presentation_num++;
790
8120e720 791 /* request property for the next client */
544f9460
TW
792 if (mei_hbm_prop_req(dev))
793 return -EIO;
bb1b0133
TW
794
795 break;
796
797 case HOST_ENUM_RES_CMD:
544f9460
TW
798 dev_dbg(&dev->pdev->dev, "hbm: enumeration response: message received\n");
799
800 dev->init_clients_timer = 0;
801
bb1b0133 802 enum_res = (struct hbm_host_enum_response *) mei_msg;
23f5a322
TW
803 BUILD_BUG_ON(sizeof(dev->me_clients_map)
804 < sizeof(enum_res->valid_addresses));
805 memcpy(dev->me_clients_map, enum_res->valid_addresses,
806 sizeof(enum_res->valid_addresses));
544f9460
TW
807
808 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
809 dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
810 dev_err(&dev->pdev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
811 dev->dev_state, dev->hbm_state);
812 return -EPROTO;
bb1b0133 813 }
544f9460
TW
814
815 if (mei_hbm_me_cl_allocate(dev)) {
816 dev_err(&dev->pdev->dev, "hbm: enumeration response: cannot allocate clients array\n");
817 return -ENOMEM;
818 }
819
820 dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
821
822 /* first property request */
823 if (mei_hbm_prop_req(dev))
824 return -EIO;
825
bb1b0133
TW
826 break;
827
828 case HOST_STOP_RES_CMD:
544f9460
TW
829 dev_dbg(&dev->pdev->dev, "hbm: stop response: message received\n");
830
831 dev->init_clients_timer = 0;
832
833 if (dev->hbm_state != MEI_HBM_STOPPED) {
834 dev_err(&dev->pdev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
835 dev->dev_state, dev->hbm_state);
836 return -EPROTO;
837 }
9b0d5efc 838
33ec0826 839 dev->dev_state = MEI_DEV_POWER_DOWN;
544f9460
TW
840 dev_info(&dev->pdev->dev, "hbm: stop response: resetting.\n");
841 /* force the reset */
842 return -EPROTO;
bb1b0133
TW
843 break;
844
845 case CLIENT_DISCONNECT_REQ_CMD:
544f9460
TW
846 dev_dbg(&dev->pdev->dev, "hbm: disconnect request: message received\n");
847
bb1b0133 848 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
8120e720 849 mei_hbm_fw_disconnect_req(dev, disconnect_req);
bb1b0133
TW
850 break;
851
852 case ME_STOP_REQ_CMD:
544f9460 853 dev_dbg(&dev->pdev->dev, "hbm: stop request: message received\n");
544f9460 854 dev->hbm_state = MEI_HBM_STOPPED;
6bb948c9
TW
855 if (mei_hbm_stop_req(dev)) {
856 dev_err(&dev->pdev->dev, "hbm: start: failed to send stop request\n");
857 return -EIO;
858 }
bb1b0133 859 break;
bb1b0133
TW
860 default:
861 BUG();
862 break;
863
864 }
544f9460 865 return 0;
bb1b0133
TW
866}
867
This page took 0.125787 seconds and 5 git commands to generate.