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
17 #include <linux/export.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22
23 #include <linux/mei.h>
24
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
28
mei_hbm_status_str(enum mei_hbm_status status)29 static const char *mei_hbm_status_str(enum mei_hbm_status status)
30 {
31 #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
32 switch (status) {
33 MEI_HBM_STATUS(SUCCESS);
34 MEI_HBM_STATUS(CLIENT_NOT_FOUND);
35 MEI_HBM_STATUS(ALREADY_EXISTS);
36 MEI_HBM_STATUS(REJECTED);
37 MEI_HBM_STATUS(INVALID_PARAMETER);
38 MEI_HBM_STATUS(NOT_ALLOWED);
39 MEI_HBM_STATUS(ALREADY_STARTED);
40 MEI_HBM_STATUS(NOT_STARTED);
41 default: return "unknown";
42 }
43 #undef MEI_HBM_STATUS
44 };
45
mei_cl_conn_status_str(enum mei_cl_connect_status status)46 static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
47 {
48 #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
49 switch (status) {
50 MEI_CL_CS(SUCCESS);
51 MEI_CL_CS(NOT_FOUND);
52 MEI_CL_CS(ALREADY_STARTED);
53 MEI_CL_CS(OUT_OF_RESOURCES);
54 MEI_CL_CS(MESSAGE_SMALL);
55 default: return "unknown";
56 }
57 #undef MEI_CL_CCS
58 }
59
mei_hbm_state_str(enum mei_hbm_state state)60 const char *mei_hbm_state_str(enum mei_hbm_state state)
61 {
62 #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
63 switch (state) {
64 MEI_HBM_STATE(IDLE);
65 MEI_HBM_STATE(STARTING);
66 MEI_HBM_STATE(STARTED);
67 MEI_HBM_STATE(ENUM_CLIENTS);
68 MEI_HBM_STATE(CLIENT_PROPERTIES);
69 MEI_HBM_STATE(STOPPED);
70 default:
71 return "unknown";
72 }
73 #undef MEI_HBM_STATE
74 }
75
76 /**
77 * mei_cl_conn_status_to_errno - convert client connect response
78 * status to error code
79 *
80 * @status: client connect response status
81 *
82 * Return: corresponding error code
83 */
mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)84 static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
85 {
86 switch (status) {
87 case MEI_CL_CONN_SUCCESS: return 0;
88 case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
89 case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
90 case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
91 case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
92 default: return -EINVAL;
93 }
94 }
95
96 /**
97 * mei_hbm_idle - set hbm to idle state
98 *
99 * @dev: the device structure
100 */
mei_hbm_idle(struct mei_device * dev)101 void mei_hbm_idle(struct mei_device *dev)
102 {
103 dev->init_clients_timer = 0;
104 dev->hbm_state = MEI_HBM_IDLE;
105 }
106
107 /**
108 * mei_me_cl_remove_all - remove all me clients
109 *
110 * @dev: the device structure
111 */
mei_me_cl_remove_all(struct mei_device * dev)112 static void mei_me_cl_remove_all(struct mei_device *dev)
113 {
114 struct mei_me_client *me_cl, *next;
115
116 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) {
117 list_del(&me_cl->list);
118 kfree(me_cl);
119 }
120 }
121
122 /**
123 * mei_hbm_reset - reset hbm counters and book keeping data structurs
124 *
125 * @dev: the device structure
126 */
mei_hbm_reset(struct mei_device * dev)127 void mei_hbm_reset(struct mei_device *dev)
128 {
129 dev->me_client_index = 0;
130
131 mei_me_cl_remove_all(dev);
132
133 mei_hbm_idle(dev);
134 }
135
136 /**
137 * mei_hbm_hdr - construct hbm header
138 *
139 * @hdr: hbm header
140 * @length: payload length
141 */
142
mei_hbm_hdr(struct mei_msg_hdr * hdr,size_t length)143 static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length)
144 {
145 hdr->host_addr = 0;
146 hdr->me_addr = 0;
147 hdr->length = length;
148 hdr->msg_complete = 1;
149 hdr->reserved = 0;
150 }
151
152 /**
153 * mei_hbm_cl_hdr - construct client hbm header
154 *
155 * @cl: client
156 * @hbm_cmd: host bus message command
157 * @buf: buffer for cl header
158 * @len: buffer length
159 */
160 static inline
mei_hbm_cl_hdr(struct mei_cl * cl,u8 hbm_cmd,void * buf,size_t len)161 void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
162 {
163 struct mei_hbm_cl_cmd *cmd = buf;
164
165 memset(cmd, 0, len);
166
167 cmd->hbm_cmd = hbm_cmd;
168 cmd->host_addr = cl->host_client_id;
169 cmd->me_addr = cl->me_client_id;
170 }
171
172 /**
173 * mei_hbm_cl_write - write simple hbm client message
174 *
175 * @dev: the device structure
176 * @cl: client
177 * @hbm_cmd: host bus message command
178 * @len: buffer length
179 *
180 * Return: 0 on success, <0 on failure.
181 */
182 static inline
mei_hbm_cl_write(struct mei_device * dev,struct mei_cl * cl,u8 hbm_cmd,size_t len)183 int mei_hbm_cl_write(struct mei_device *dev,
184 struct mei_cl *cl, u8 hbm_cmd, size_t len)
185 {
186 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
187
188 mei_hbm_hdr(mei_hdr, len);
189 mei_hbm_cl_hdr(cl, hbm_cmd, dev->wr_msg.data, len);
190
191 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
192 }
193
194 /**
195 * mei_hbm_cl_addr_equal - check if the client's and
196 * the message address match
197 *
198 * @cl: client
199 * @cmd: hbm client message
200 *
201 * Return: true if addresses are the same
202 */
203 static inline
mei_hbm_cl_addr_equal(struct mei_cl * cl,struct mei_hbm_cl_cmd * cmd)204 bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
205 {
206 return cl->host_client_id == cmd->host_addr &&
207 cl->me_client_id == cmd->me_addr;
208 }
209
210 /**
211 * mei_hbm_cl_find_by_cmd - find recipient client
212 *
213 * @dev: the device structure
214 * @buf: a buffer with hbm cl command
215 *
216 * Return: the recipient client or NULL if not found
217 */
218 static inline
mei_hbm_cl_find_by_cmd(struct mei_device * dev,void * buf)219 struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
220 {
221 struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
222 struct mei_cl *cl;
223
224 list_for_each_entry(cl, &dev->file_list, link)
225 if (mei_hbm_cl_addr_equal(cl, cmd))
226 return cl;
227 return NULL;
228 }
229
230
231 /**
232 * mei_hbm_start_wait - wait for start response message.
233 *
234 * @dev: the device structure
235 *
236 * Return: 0 on success and < 0 on failure
237 */
mei_hbm_start_wait(struct mei_device * dev)238 int mei_hbm_start_wait(struct mei_device *dev)
239 {
240 int ret;
241
242 if (dev->hbm_state > MEI_HBM_STARTING)
243 return 0;
244
245 mutex_unlock(&dev->device_lock);
246 ret = wait_event_timeout(dev->wait_hbm_start,
247 dev->hbm_state != MEI_HBM_STARTING,
248 mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
249 mutex_lock(&dev->device_lock);
250
251 if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
252 dev->hbm_state = MEI_HBM_IDLE;
253 dev_err(dev->dev, "waiting for mei start failed\n");
254 return -ETIME;
255 }
256 return 0;
257 }
258
259 /**
260 * mei_hbm_start_req - sends start request message.
261 *
262 * @dev: the device structure
263 *
264 * Return: 0 on success and < 0 on failure
265 */
mei_hbm_start_req(struct mei_device * dev)266 int mei_hbm_start_req(struct mei_device *dev)
267 {
268 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
269 struct hbm_host_version_request *start_req;
270 const size_t len = sizeof(struct hbm_host_version_request);
271 int ret;
272
273 mei_hbm_reset(dev);
274
275 mei_hbm_hdr(mei_hdr, len);
276
277 /* host start message */
278 start_req = (struct hbm_host_version_request *)dev->wr_msg.data;
279 memset(start_req, 0, len);
280 start_req->hbm_cmd = HOST_START_REQ_CMD;
281 start_req->host_version.major_version = HBM_MAJOR_VERSION;
282 start_req->host_version.minor_version = HBM_MINOR_VERSION;
283
284 dev->hbm_state = MEI_HBM_IDLE;
285 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
286 if (ret) {
287 dev_err(dev->dev, "version message write failed: ret = %d\n",
288 ret);
289 return ret;
290 }
291
292 dev->hbm_state = MEI_HBM_STARTING;
293 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
294 return 0;
295 }
296
297 /*
298 * mei_hbm_enum_clients_req - sends enumeration client request message.
299 *
300 * @dev: the device structure
301 *
302 * Return: 0 on success and < 0 on failure
303 */
mei_hbm_enum_clients_req(struct mei_device * dev)304 static int mei_hbm_enum_clients_req(struct mei_device *dev)
305 {
306 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
307 struct hbm_host_enum_request *enum_req;
308 const size_t len = sizeof(struct hbm_host_enum_request);
309 int ret;
310
311 /* enumerate clients */
312 mei_hbm_hdr(mei_hdr, len);
313
314 enum_req = (struct hbm_host_enum_request *)dev->wr_msg.data;
315 memset(enum_req, 0, len);
316 enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
317
318 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
319 if (ret) {
320 dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
321 ret);
322 return ret;
323 }
324 dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
325 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
326 return 0;
327 }
328
329 /*
330 * mei_hbm_me_cl_add - add new me client to the list
331 *
332 * @dev: the device structure
333 * @res: hbm property response
334 *
335 * Return: 0 on success and -ENOMEM on allocation failure
336 */
337
mei_hbm_me_cl_add(struct mei_device * dev,struct hbm_props_response * res)338 static int mei_hbm_me_cl_add(struct mei_device *dev,
339 struct hbm_props_response *res)
340 {
341 struct mei_me_client *me_cl;
342
343 me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
344 if (!me_cl)
345 return -ENOMEM;
346
347 me_cl->props = res->client_properties;
348 me_cl->client_id = res->me_addr;
349 me_cl->mei_flow_ctrl_creds = 0;
350
351 list_add(&me_cl->list, &dev->me_clients);
352 return 0;
353 }
354
355 /**
356 * mei_hbm_prop_req - request property for a single client
357 *
358 * @dev: the device structure
359 *
360 * Return: 0 on success and < 0 on failure
361 */
362
mei_hbm_prop_req(struct mei_device * dev)363 static int mei_hbm_prop_req(struct mei_device *dev)
364 {
365
366 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
367 struct hbm_props_request *prop_req;
368 const size_t len = sizeof(struct hbm_props_request);
369 unsigned long next_client_index;
370 int ret;
371
372 next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX,
373 dev->me_client_index);
374
375 /* We got all client properties */
376 if (next_client_index == MEI_CLIENTS_MAX) {
377 dev->hbm_state = MEI_HBM_STARTED;
378 schedule_work(&dev->init_work);
379
380 return 0;
381 }
382
383 mei_hbm_hdr(mei_hdr, len);
384 prop_req = (struct hbm_props_request *)dev->wr_msg.data;
385
386 memset(prop_req, 0, sizeof(struct hbm_props_request));
387
388 prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
389 prop_req->me_addr = next_client_index;
390
391 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
392 if (ret) {
393 dev_err(dev->dev, "properties request write failed: ret = %d\n",
394 ret);
395 return ret;
396 }
397
398 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
399 dev->me_client_index = next_client_index;
400
401 return 0;
402 }
403
404 /*
405 * mei_hbm_pg - sends pg command
406 *
407 * @dev: the device structure
408 * @pg_cmd: the pg command code
409 *
410 * Return: -EIO on write failure
411 * -EOPNOTSUPP if the operation is not supported by the protocol
412 */
mei_hbm_pg(struct mei_device * dev,u8 pg_cmd)413 int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
414 {
415 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
416 struct hbm_power_gate *req;
417 const size_t len = sizeof(struct hbm_power_gate);
418 int ret;
419
420 if (!dev->hbm_f_pg_supported)
421 return -EOPNOTSUPP;
422
423 mei_hbm_hdr(mei_hdr, len);
424
425 req = (struct hbm_power_gate *)dev->wr_msg.data;
426 memset(req, 0, len);
427 req->hbm_cmd = pg_cmd;
428
429 ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
430 if (ret)
431 dev_err(dev->dev, "power gate command write failed.\n");
432 return ret;
433 }
434 EXPORT_SYMBOL_GPL(mei_hbm_pg);
435
436 /**
437 * mei_hbm_stop_req - send stop request message
438 *
439 * @dev: mei device
440 *
441 * Return: -EIO on write failure
442 */
mei_hbm_stop_req(struct mei_device * dev)443 static int mei_hbm_stop_req(struct mei_device *dev)
444 {
445 struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
446 struct hbm_host_stop_request *req =
447 (struct hbm_host_stop_request *)dev->wr_msg.data;
448 const size_t len = sizeof(struct hbm_host_stop_request);
449
450 mei_hbm_hdr(mei_hdr, len);
451
452 memset(req, 0, len);
453 req->hbm_cmd = HOST_STOP_REQ_CMD;
454 req->reason = DRIVER_STOP_REQUEST;
455
456 return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
457 }
458
459 /**
460 * mei_hbm_cl_flow_control_req - sends flow control request.
461 *
462 * @dev: the device structure
463 * @cl: client info
464 *
465 * Return: -EIO on write failure
466 */
mei_hbm_cl_flow_control_req(struct mei_device * dev,struct mei_cl * cl)467 int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
468 {
469 const size_t len = sizeof(struct hbm_flow_control);
470
471 cl_dbg(dev, cl, "sending flow control\n");
472 return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD, len);
473 }
474
475 /**
476 * mei_hbm_add_single_flow_creds - adds single buffer credentials.
477 *
478 * @dev: the device structure
479 * @flow: flow control.
480 *
481 * Return: 0 on success, < 0 otherwise
482 */
mei_hbm_add_single_flow_creds(struct mei_device * dev,struct hbm_flow_control * flow)483 static int mei_hbm_add_single_flow_creds(struct mei_device *dev,
484 struct hbm_flow_control *flow)
485 {
486 struct mei_me_client *me_cl;
487
488 me_cl = mei_me_cl_by_id(dev, flow->me_addr);
489 if (!me_cl) {
490 dev_err(dev->dev, "no such me client %d\n",
491 flow->me_addr);
492 return -ENOENT;
493 }
494
495 if (WARN_ON(me_cl->props.single_recv_buf == 0))
496 return -EINVAL;
497
498 me_cl->mei_flow_ctrl_creds++;
499 dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
500 flow->me_addr, me_cl->mei_flow_ctrl_creds);
501
502 return 0;
503 }
504
505 /**
506 * mei_hbm_cl_flow_control_res - flow control response from me
507 *
508 * @dev: the device structure
509 * @flow_control: flow control response bus message
510 */
mei_hbm_cl_flow_control_res(struct mei_device * dev,struct hbm_flow_control * flow_control)511 static void mei_hbm_cl_flow_control_res(struct mei_device *dev,
512 struct hbm_flow_control *flow_control)
513 {
514 struct mei_cl *cl;
515
516 if (!flow_control->host_addr) {
517 /* single receive buffer */
518 mei_hbm_add_single_flow_creds(dev, flow_control);
519 return;
520 }
521
522 cl = mei_hbm_cl_find_by_cmd(dev, flow_control);
523 if (cl) {
524 cl->mei_flow_ctrl_creds++;
525 cl_dbg(dev, cl, "flow control creds = %d.\n",
526 cl->mei_flow_ctrl_creds);
527 }
528 }
529
530
531 /**
532 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
533 *
534 * @dev: the device structure
535 * @cl: a client to disconnect from
536 *
537 * Return: -EIO on write failure
538 */
mei_hbm_cl_disconnect_req(struct mei_device * dev,struct mei_cl * cl)539 int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
540 {
541 const size_t len = sizeof(struct hbm_client_connect_request);
542
543 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD, len);
544 }
545
546 /**
547 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
548 *
549 * @dev: the device structure
550 * @cl: a client to disconnect from
551 *
552 * Return: -EIO on write failure
553 */
mei_hbm_cl_disconnect_rsp(struct mei_device * dev,struct mei_cl * cl)554 int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
555 {
556 const size_t len = sizeof(struct hbm_client_connect_response);
557
558 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD, len);
559 }
560
561 /**
562 * mei_hbm_cl_disconnect_res - update the client state according
563 * disconnect response
564 *
565 * @cl: mei host client
566 * @cmd: disconnect client response host bus message
567 */
mei_hbm_cl_disconnect_res(struct mei_cl * cl,struct mei_hbm_cl_cmd * cmd)568 static void mei_hbm_cl_disconnect_res(struct mei_cl *cl,
569 struct mei_hbm_cl_cmd *cmd)
570 {
571 struct hbm_client_connect_response *rs =
572 (struct hbm_client_connect_response *)cmd;
573
574 dev_dbg(cl->dev->dev, "hbm: disconnect response cl:host=%02d me=%02d status=%d\n",
575 rs->me_addr, rs->host_addr, rs->status);
576
577 if (rs->status == MEI_CL_DISCONN_SUCCESS)
578 cl->state = MEI_FILE_DISCONNECTED;
579 cl->status = 0;
580 }
581
582 /**
583 * mei_hbm_cl_connect_req - send connection request to specific me client
584 *
585 * @dev: the device structure
586 * @cl: a client to connect to
587 *
588 * Return: -EIO on write failure
589 */
mei_hbm_cl_connect_req(struct mei_device * dev,struct mei_cl * cl)590 int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
591 {
592 const size_t len = sizeof(struct hbm_client_connect_request);
593
594 return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD, len);
595 }
596
597 /**
598 * mei_hbm_cl_connect_res - update the client state according
599 * connection response
600 *
601 * @cl: mei host client
602 * @cmd: connect client response host bus message
603 */
mei_hbm_cl_connect_res(struct mei_cl * cl,struct mei_hbm_cl_cmd * cmd)604 static void mei_hbm_cl_connect_res(struct mei_cl *cl,
605 struct mei_hbm_cl_cmd *cmd)
606 {
607 struct hbm_client_connect_response *rs =
608 (struct hbm_client_connect_response *)cmd;
609
610 dev_dbg(cl->dev->dev, "hbm: connect response cl:host=%02d me=%02d status=%s\n",
611 rs->me_addr, rs->host_addr,
612 mei_cl_conn_status_str(rs->status));
613
614 if (rs->status == MEI_CL_CONN_SUCCESS)
615 cl->state = MEI_FILE_CONNECTED;
616 else
617 cl->state = MEI_FILE_DISCONNECTED;
618 cl->status = mei_cl_conn_status_to_errno(rs->status);
619 }
620
621 /**
622 * mei_hbm_cl_res - process hbm response received on behalf
623 * an client
624 *
625 * @dev: the device structure
626 * @rs: hbm client message
627 * @fop_type: file operation type
628 */
mei_hbm_cl_res(struct mei_device * dev,struct mei_hbm_cl_cmd * rs,enum mei_cb_file_ops fop_type)629 static void mei_hbm_cl_res(struct mei_device *dev,
630 struct mei_hbm_cl_cmd *rs,
631 enum mei_cb_file_ops fop_type)
632 {
633 struct mei_cl *cl;
634 struct mei_cl_cb *cb, *next;
635
636 cl = NULL;
637 list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
638
639 cl = cb->cl;
640 /* this should not happen */
641 if (WARN_ON(!cl)) {
642 list_del_init(&cb->list);
643 continue;
644 }
645
646 if (cb->fop_type != fop_type)
647 continue;
648
649 if (mei_hbm_cl_addr_equal(cl, rs)) {
650 list_del(&cb->list);
651 break;
652 }
653 }
654
655 if (!cl)
656 return;
657
658 switch (fop_type) {
659 case MEI_FOP_CONNECT:
660 mei_hbm_cl_connect_res(cl, rs);
661 break;
662 case MEI_FOP_DISCONNECT:
663 mei_hbm_cl_disconnect_res(cl, rs);
664 break;
665 default:
666 return;
667 }
668
669 cl->timer_count = 0;
670 wake_up(&cl->wait);
671 }
672
673
674 /**
675 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
676 * host sends disconnect response
677 *
678 * @dev: the device structure.
679 * @disconnect_req: disconnect request bus message from the me
680 *
681 * Return: -ENOMEM on allocation failure
682 */
mei_hbm_fw_disconnect_req(struct mei_device * dev,struct hbm_client_connect_request * disconnect_req)683 static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
684 struct hbm_client_connect_request *disconnect_req)
685 {
686 struct mei_cl *cl;
687 struct mei_cl_cb *cb;
688
689 cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
690 if (cl) {
691 cl_dbg(dev, cl, "disconnect request received\n");
692 cl->state = MEI_FILE_DISCONNECTED;
693 cl->timer_count = 0;
694
695 cb = mei_io_cb_init(cl, NULL);
696 if (!cb)
697 return -ENOMEM;
698 cb->fop_type = MEI_FOP_DISCONNECT_RSP;
699 cl_dbg(dev, cl, "add disconnect response as first\n");
700 list_add(&cb->list, &dev->ctrl_wr_list.list);
701 }
702 return 0;
703 }
704
705 /**
706 * mei_hbm_config_features - check what hbm features and commands
707 * are supported by the fw
708 *
709 * @dev: the device structure
710 */
mei_hbm_config_features(struct mei_device * dev)711 static void mei_hbm_config_features(struct mei_device *dev)
712 {
713 /* Power Gating Isolation Support */
714 dev->hbm_f_pg_supported = 0;
715 if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
716 dev->hbm_f_pg_supported = 1;
717
718 if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
719 dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
720 dev->hbm_f_pg_supported = 1;
721 }
722
723 /**
724 * mei_hbm_version_is_supported - checks whether the driver can
725 * support the hbm version of the device
726 *
727 * @dev: the device structure
728 * Return: true if driver can support hbm version of the device
729 */
mei_hbm_version_is_supported(struct mei_device * dev)730 bool mei_hbm_version_is_supported(struct mei_device *dev)
731 {
732 return (dev->version.major_version < HBM_MAJOR_VERSION) ||
733 (dev->version.major_version == HBM_MAJOR_VERSION &&
734 dev->version.minor_version <= HBM_MINOR_VERSION);
735 }
736
737 /**
738 * mei_hbm_dispatch - bottom half read routine after ISR to
739 * handle the read bus message cmd processing.
740 *
741 * @dev: the device structure
742 * @hdr: header of bus message
743 *
744 * Return: 0 on success and < 0 on failure
745 */
mei_hbm_dispatch(struct mei_device * dev,struct mei_msg_hdr * hdr)746 int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
747 {
748 struct mei_bus_message *mei_msg;
749 struct hbm_host_version_response *version_res;
750 struct hbm_props_response *props_res;
751 struct hbm_host_enum_response *enum_res;
752
753 struct mei_hbm_cl_cmd *cl_cmd;
754 struct hbm_client_connect_request *disconnect_req;
755 struct hbm_flow_control *flow_control;
756
757 /* read the message to our buffer */
758 BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
759 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
760 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
761 cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg;
762
763 /* ignore spurious message and prevent reset nesting
764 * hbm is put to idle during system reset
765 */
766 if (dev->hbm_state == MEI_HBM_IDLE) {
767 dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
768 return 0;
769 }
770
771 switch (mei_msg->hbm_cmd) {
772 case HOST_START_RES_CMD:
773 dev_dbg(dev->dev, "hbm: start: response message received.\n");
774
775 dev->init_clients_timer = 0;
776
777 version_res = (struct hbm_host_version_response *)mei_msg;
778
779 dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
780 HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
781 version_res->me_max_version.major_version,
782 version_res->me_max_version.minor_version);
783
784 if (version_res->host_version_supported) {
785 dev->version.major_version = HBM_MAJOR_VERSION;
786 dev->version.minor_version = HBM_MINOR_VERSION;
787 } else {
788 dev->version.major_version =
789 version_res->me_max_version.major_version;
790 dev->version.minor_version =
791 version_res->me_max_version.minor_version;
792 }
793
794 if (!mei_hbm_version_is_supported(dev)) {
795 dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
796
797 dev->hbm_state = MEI_HBM_STOPPED;
798 if (mei_hbm_stop_req(dev)) {
799 dev_err(dev->dev, "hbm: start: failed to send stop request\n");
800 return -EIO;
801 }
802 break;
803 }
804
805 mei_hbm_config_features(dev);
806
807 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
808 dev->hbm_state != MEI_HBM_STARTING) {
809 dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
810 dev->dev_state, dev->hbm_state);
811 return -EPROTO;
812 }
813
814 dev->hbm_state = MEI_HBM_STARTED;
815
816 if (mei_hbm_enum_clients_req(dev)) {
817 dev_err(dev->dev, "hbm: start: failed to send enumeration request\n");
818 return -EIO;
819 }
820
821 wake_up(&dev->wait_hbm_start);
822 break;
823
824 case CLIENT_CONNECT_RES_CMD:
825 dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
826 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
827 break;
828
829 case CLIENT_DISCONNECT_RES_CMD:
830 dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
831 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
832 break;
833
834 case MEI_FLOW_CONTROL_CMD:
835 dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
836
837 flow_control = (struct hbm_flow_control *) mei_msg;
838 mei_hbm_cl_flow_control_res(dev, flow_control);
839 break;
840
841 case MEI_PG_ISOLATION_ENTRY_RES_CMD:
842 dev_dbg(dev->dev, "power gate isolation entry response received\n");
843 dev->pg_event = MEI_PG_EVENT_RECEIVED;
844 if (waitqueue_active(&dev->wait_pg))
845 wake_up(&dev->wait_pg);
846 break;
847
848 case MEI_PG_ISOLATION_EXIT_REQ_CMD:
849 dev_dbg(dev->dev, "power gate isolation exit request received\n");
850 dev->pg_event = MEI_PG_EVENT_RECEIVED;
851 if (waitqueue_active(&dev->wait_pg))
852 wake_up(&dev->wait_pg);
853 else
854 /*
855 * If the driver is not waiting on this then
856 * this is HW initiated exit from PG.
857 * Start runtime pm resume sequence to exit from PG.
858 */
859 pm_request_resume(dev->dev);
860 break;
861
862 case HOST_CLIENT_PROPERTIES_RES_CMD:
863 dev_dbg(dev->dev, "hbm: properties response: message received.\n");
864
865 dev->init_clients_timer = 0;
866
867 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
868 dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
869 dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
870 dev->dev_state, dev->hbm_state);
871 return -EPROTO;
872 }
873
874 props_res = (struct hbm_props_response *)mei_msg;
875
876 if (props_res->status) {
877 dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
878 props_res->status,
879 mei_hbm_status_str(props_res->status));
880 return -EPROTO;
881 }
882
883 mei_hbm_me_cl_add(dev, props_res);
884
885 dev->me_client_index++;
886
887 /* request property for the next client */
888 if (mei_hbm_prop_req(dev))
889 return -EIO;
890
891 break;
892
893 case HOST_ENUM_RES_CMD:
894 dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
895
896 dev->init_clients_timer = 0;
897
898 enum_res = (struct hbm_host_enum_response *) mei_msg;
899 BUILD_BUG_ON(sizeof(dev->me_clients_map)
900 < sizeof(enum_res->valid_addresses));
901 memcpy(dev->me_clients_map, enum_res->valid_addresses,
902 sizeof(enum_res->valid_addresses));
903
904 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
905 dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
906 dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
907 dev->dev_state, dev->hbm_state);
908 return -EPROTO;
909 }
910
911 dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
912
913 /* first property request */
914 if (mei_hbm_prop_req(dev))
915 return -EIO;
916
917 break;
918
919 case HOST_STOP_RES_CMD:
920 dev_dbg(dev->dev, "hbm: stop response: message received\n");
921
922 dev->init_clients_timer = 0;
923
924 if (dev->hbm_state != MEI_HBM_STOPPED) {
925 dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
926 dev->dev_state, dev->hbm_state);
927 return -EPROTO;
928 }
929
930 dev->dev_state = MEI_DEV_POWER_DOWN;
931 dev_info(dev->dev, "hbm: stop response: resetting.\n");
932 /* force the reset */
933 return -EPROTO;
934 break;
935
936 case CLIENT_DISCONNECT_REQ_CMD:
937 dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
938
939 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
940 mei_hbm_fw_disconnect_req(dev, disconnect_req);
941 break;
942
943 case ME_STOP_REQ_CMD:
944 dev_dbg(dev->dev, "hbm: stop request: message received\n");
945 dev->hbm_state = MEI_HBM_STOPPED;
946 if (mei_hbm_stop_req(dev)) {
947 dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
948 return -EIO;
949 }
950 break;
951 default:
952 BUG();
953 break;
954
955 }
956 return 0;
957 }
958
959