Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
29static 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
46static 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 MEI_CL_CS(NOT_ALLOWED);
56 default: return "unknown";
57 }
58#undef MEI_CL_CCS
59}
60
61const char *mei_hbm_state_str(enum mei_hbm_state state)
62{
63#define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
64 switch (state) {
65 MEI_HBM_STATE(IDLE);
66 MEI_HBM_STATE(STARTING);
67 MEI_HBM_STATE(STARTED);
68 MEI_HBM_STATE(DR_SETUP);
69 MEI_HBM_STATE(ENUM_CLIENTS);
70 MEI_HBM_STATE(CLIENT_PROPERTIES);
71 MEI_HBM_STATE(STOPPED);
72 default:
73 return "unknown";
74 }
75#undef MEI_HBM_STATE
76}
77
78/**
79 * mei_cl_conn_status_to_errno - convert client connect response
80 * status to error code
81 *
82 * @status: client connect response status
83 *
84 * Return: corresponding error code
85 */
86static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
87{
88 switch (status) {
89 case MEI_CL_CONN_SUCCESS: return 0;
90 case MEI_CL_CONN_NOT_FOUND: return -ENOTTY;
91 case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY;
92 case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
93 case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL;
94 case MEI_CL_CONN_NOT_ALLOWED: return -EBUSY;
95 default: return -EINVAL;
96 }
97}
98
99/**
100 * mei_hbm_write_message - wrapper for sending hbm messages.
101 *
102 * @dev: mei device
103 * @hdr: mei header
104 * @data: payload
105 */
106static inline int mei_hbm_write_message(struct mei_device *dev,
107 struct mei_msg_hdr *hdr,
108 const void *data)
109{
110 return mei_write_message(dev, hdr, sizeof(*hdr), data, hdr->length);
111}
112
113/**
114 * mei_hbm_idle - set hbm to idle state
115 *
116 * @dev: the device structure
117 */
118void mei_hbm_idle(struct mei_device *dev)
119{
120 dev->init_clients_timer = 0;
121 dev->hbm_state = MEI_HBM_IDLE;
122}
123
124/**
125 * mei_hbm_reset - reset hbm counters and book keeping data structurs
126 *
127 * @dev: the device structure
128 */
129void mei_hbm_reset(struct mei_device *dev)
130{
131 mei_me_cl_rm_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
143static 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->dma_ring = 0;
150 hdr->reserved = 0;
151 hdr->internal = 0;
152}
153
154/**
155 * mei_hbm_cl_hdr - construct client hbm header
156 *
157 * @cl: client
158 * @hbm_cmd: host bus message command
159 * @buf: buffer for cl header
160 * @len: buffer length
161 */
162static inline
163void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
164{
165 struct mei_hbm_cl_cmd *cmd = buf;
166
167 memset(cmd, 0, len);
168
169 cmd->hbm_cmd = hbm_cmd;
170 cmd->host_addr = mei_cl_host_addr(cl);
171 cmd->me_addr = mei_cl_me_id(cl);
172}
173
174/**
175 * mei_hbm_cl_write - write simple hbm client message
176 *
177 * @dev: the device structure
178 * @cl: client
179 * @hbm_cmd: host bus message command
180 * @buf: message buffer
181 * @len: buffer length
182 *
183 * Return: 0 on success, <0 on failure.
184 */
185static inline int mei_hbm_cl_write(struct mei_device *dev, struct mei_cl *cl,
186 u8 hbm_cmd, void *buf, size_t len)
187{
188 struct mei_msg_hdr mei_hdr;
189
190 mei_hbm_hdr(&mei_hdr, len);
191 mei_hbm_cl_hdr(cl, hbm_cmd, buf, len);
192
193 return mei_hbm_write_message(dev, &mei_hdr, buf);
194}
195
196/**
197 * mei_hbm_cl_addr_equal - check if the client's and
198 * the message address match
199 *
200 * @cl: client
201 * @cmd: hbm client message
202 *
203 * Return: true if addresses are the same
204 */
205static inline
206bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
207{
208 return mei_cl_host_addr(cl) == cmd->host_addr &&
209 mei_cl_me_id(cl) == cmd->me_addr;
210}
211
212/**
213 * mei_hbm_cl_find_by_cmd - find recipient client
214 *
215 * @dev: the device structure
216 * @buf: a buffer with hbm cl command
217 *
218 * Return: the recipient client or NULL if not found
219 */
220static inline
221struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
222{
223 struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
224 struct mei_cl *cl;
225
226 list_for_each_entry(cl, &dev->file_list, link)
227 if (mei_hbm_cl_addr_equal(cl, cmd))
228 return cl;
229 return NULL;
230}
231
232
233/**
234 * mei_hbm_start_wait - wait for start response message.
235 *
236 * @dev: the device structure
237 *
238 * Return: 0 on success and < 0 on failure
239 */
240int mei_hbm_start_wait(struct mei_device *dev)
241{
242 int ret;
243
244 if (dev->hbm_state > MEI_HBM_STARTING)
245 return 0;
246
247 mutex_unlock(&dev->device_lock);
248 ret = wait_event_timeout(dev->wait_hbm_start,
249 dev->hbm_state != MEI_HBM_STARTING,
250 mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
251 mutex_lock(&dev->device_lock);
252
253 if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
254 dev->hbm_state = MEI_HBM_IDLE;
255 dev_err(dev->dev, "waiting for mei start failed\n");
256 return -ETIME;
257 }
258 return 0;
259}
260
261/**
262 * mei_hbm_start_req - sends start request message.
263 *
264 * @dev: the device structure
265 *
266 * Return: 0 on success and < 0 on failure
267 */
268int mei_hbm_start_req(struct mei_device *dev)
269{
270 struct mei_msg_hdr mei_hdr;
271 struct hbm_host_version_request start_req;
272 const size_t len = sizeof(struct hbm_host_version_request);
273 int ret;
274
275 mei_hbm_reset(dev);
276
277 mei_hbm_hdr(&mei_hdr, len);
278
279 /* host start message */
280 memset(&start_req, 0, len);
281 start_req.hbm_cmd = HOST_START_REQ_CMD;
282 start_req.host_version.major_version = HBM_MAJOR_VERSION;
283 start_req.host_version.minor_version = HBM_MINOR_VERSION;
284
285 dev->hbm_state = MEI_HBM_IDLE;
286 ret = mei_hbm_write_message(dev, &mei_hdr, &start_req);
287 if (ret) {
288 dev_err(dev->dev, "version message write failed: ret = %d\n",
289 ret);
290 return ret;
291 }
292
293 dev->hbm_state = MEI_HBM_STARTING;
294 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
295 mei_schedule_stall_timer(dev);
296 return 0;
297}
298
299/**
300 * mei_hbm_dma_setup_req() - setup DMA request
301 * @dev: the device structure
302 *
303 * Return: 0 on success and < 0 on failure
304 */
305static int mei_hbm_dma_setup_req(struct mei_device *dev)
306{
307 struct mei_msg_hdr mei_hdr;
308 struct hbm_dma_setup_request req;
309 const size_t len = sizeof(struct hbm_dma_setup_request);
310 unsigned int i;
311 int ret;
312
313 mei_hbm_hdr(&mei_hdr, len);
314
315 memset(&req, 0, len);
316 req.hbm_cmd = MEI_HBM_DMA_SETUP_REQ_CMD;
317 for (i = 0; i < DMA_DSCR_NUM; i++) {
318 phys_addr_t paddr;
319
320 paddr = dev->dr_dscr[i].daddr;
321 req.dma_dscr[i].addr_hi = upper_32_bits(paddr);
322 req.dma_dscr[i].addr_lo = lower_32_bits(paddr);
323 req.dma_dscr[i].size = dev->dr_dscr[i].size;
324 }
325
326 mei_dma_ring_reset(dev);
327
328 ret = mei_hbm_write_message(dev, &mei_hdr, &req);
329 if (ret) {
330 dev_err(dev->dev, "dma setup request write failed: ret = %d.\n",
331 ret);
332 return ret;
333 }
334
335 dev->hbm_state = MEI_HBM_DR_SETUP;
336 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
337 mei_schedule_stall_timer(dev);
338 return 0;
339}
340
341/**
342 * mei_hbm_enum_clients_req - sends enumeration client request message.
343 *
344 * @dev: the device structure
345 *
346 * Return: 0 on success and < 0 on failure
347 */
348static int mei_hbm_enum_clients_req(struct mei_device *dev)
349{
350 struct mei_msg_hdr mei_hdr;
351 struct hbm_host_enum_request enum_req;
352 const size_t len = sizeof(struct hbm_host_enum_request);
353 int ret;
354
355 /* enumerate clients */
356 mei_hbm_hdr(&mei_hdr, len);
357
358 memset(&enum_req, 0, len);
359 enum_req.hbm_cmd = HOST_ENUM_REQ_CMD;
360 enum_req.flags |= dev->hbm_f_dc_supported ?
361 MEI_HBM_ENUM_F_ALLOW_ADD : 0;
362 enum_req.flags |= dev->hbm_f_ie_supported ?
363 MEI_HBM_ENUM_F_IMMEDIATE_ENUM : 0;
364
365 ret = mei_hbm_write_message(dev, &mei_hdr, &enum_req);
366 if (ret) {
367 dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
368 ret);
369 return ret;
370 }
371 dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
372 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
373 mei_schedule_stall_timer(dev);
374 return 0;
375}
376
377/**
378 * mei_hbm_me_cl_add - add new me client to the list
379 *
380 * @dev: the device structure
381 * @res: hbm property response
382 *
383 * Return: 0 on success and -ENOMEM on allocation failure
384 */
385
386static int mei_hbm_me_cl_add(struct mei_device *dev,
387 struct hbm_props_response *res)
388{
389 struct mei_me_client *me_cl;
390 const uuid_le *uuid = &res->client_properties.protocol_name;
391
392 mei_me_cl_rm_by_uuid(dev, uuid);
393
394 me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
395 if (!me_cl)
396 return -ENOMEM;
397
398 mei_me_cl_init(me_cl);
399
400 me_cl->props = res->client_properties;
401 me_cl->client_id = res->me_addr;
402 me_cl->tx_flow_ctrl_creds = 0;
403
404 mei_me_cl_add(dev, me_cl);
405
406 return 0;
407}
408
409/**
410 * mei_hbm_add_cl_resp - send response to fw on client add request
411 *
412 * @dev: the device structure
413 * @addr: me address
414 * @status: response status
415 *
416 * Return: 0 on success and < 0 on failure
417 */
418static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status)
419{
420 struct mei_msg_hdr mei_hdr;
421 struct hbm_add_client_response resp;
422 const size_t len = sizeof(struct hbm_add_client_response);
423 int ret;
424
425 dev_dbg(dev->dev, "adding client response\n");
426
427 mei_hbm_hdr(&mei_hdr, len);
428
429 memset(&resp, 0, sizeof(struct hbm_add_client_response));
430 resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
431 resp.me_addr = addr;
432 resp.status = status;
433
434 ret = mei_hbm_write_message(dev, &mei_hdr, &resp);
435 if (ret)
436 dev_err(dev->dev, "add client response write failed: ret = %d\n",
437 ret);
438 return ret;
439}
440
441/**
442 * mei_hbm_fw_add_cl_req - request from the fw to add a client
443 *
444 * @dev: the device structure
445 * @req: add client request
446 *
447 * Return: 0 on success and < 0 on failure
448 */
449static int mei_hbm_fw_add_cl_req(struct mei_device *dev,
450 struct hbm_add_client_request *req)
451{
452 int ret;
453 u8 status = MEI_HBMS_SUCCESS;
454
455 BUILD_BUG_ON(sizeof(struct hbm_add_client_request) !=
456 sizeof(struct hbm_props_response));
457
458 ret = mei_hbm_me_cl_add(dev, (struct hbm_props_response *)req);
459 if (ret)
460 status = !MEI_HBMS_SUCCESS;
461
462 if (dev->dev_state == MEI_DEV_ENABLED)
463 schedule_work(&dev->bus_rescan_work);
464
465 return mei_hbm_add_cl_resp(dev, req->me_addr, status);
466}
467
468/**
469 * mei_hbm_cl_notify_req - send notification request
470 *
471 * @dev: the device structure
472 * @cl: a client to disconnect from
473 * @start: true for start false for stop
474 *
475 * Return: 0 on success and -EIO on write failure
476 */
477int mei_hbm_cl_notify_req(struct mei_device *dev,
478 struct mei_cl *cl, u8 start)
479{
480
481 struct mei_msg_hdr mei_hdr;
482 struct hbm_notification_request req;
483 const size_t len = sizeof(struct hbm_notification_request);
484 int ret;
485
486 mei_hbm_hdr(&mei_hdr, len);
487 mei_hbm_cl_hdr(cl, MEI_HBM_NOTIFY_REQ_CMD, &req, len);
488
489 req.start = start;
490
491 ret = mei_hbm_write_message(dev, &mei_hdr, &req);
492 if (ret)
493 dev_err(dev->dev, "notify request failed: ret = %d\n", ret);
494
495 return ret;
496}
497
498/**
499 * notify_res_to_fop - convert notification response to the proper
500 * notification FOP
501 *
502 * @cmd: client notification start response command
503 *
504 * Return: MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP;
505 */
506static inline enum mei_cb_file_ops notify_res_to_fop(struct mei_hbm_cl_cmd *cmd)
507{
508 struct hbm_notification_response *rs =
509 (struct hbm_notification_response *)cmd;
510
511 return mei_cl_notify_req2fop(rs->start);
512}
513
514/**
515 * mei_hbm_cl_notify_start_res - update the client state according
516 * notify start response
517 *
518 * @dev: the device structure
519 * @cl: mei host client
520 * @cmd: client notification start response command
521 */
522static void mei_hbm_cl_notify_start_res(struct mei_device *dev,
523 struct mei_cl *cl,
524 struct mei_hbm_cl_cmd *cmd)
525{
526 struct hbm_notification_response *rs =
527 (struct hbm_notification_response *)cmd;
528
529 cl_dbg(dev, cl, "hbm: notify start response status=%d\n", rs->status);
530
531 if (rs->status == MEI_HBMS_SUCCESS ||
532 rs->status == MEI_HBMS_ALREADY_STARTED) {
533 cl->notify_en = true;
534 cl->status = 0;
535 } else {
536 cl->status = -EINVAL;
537 }
538}
539
540/**
541 * mei_hbm_cl_notify_stop_res - update the client state according
542 * notify stop response
543 *
544 * @dev: the device structure
545 * @cl: mei host client
546 * @cmd: client notification stop response command
547 */
548static void mei_hbm_cl_notify_stop_res(struct mei_device *dev,
549 struct mei_cl *cl,
550 struct mei_hbm_cl_cmd *cmd)
551{
552 struct hbm_notification_response *rs =
553 (struct hbm_notification_response *)cmd;
554
555 cl_dbg(dev, cl, "hbm: notify stop response status=%d\n", rs->status);
556
557 if (rs->status == MEI_HBMS_SUCCESS ||
558 rs->status == MEI_HBMS_NOT_STARTED) {
559 cl->notify_en = false;
560 cl->status = 0;
561 } else {
562 /* TODO: spec is not clear yet about other possible issues */
563 cl->status = -EINVAL;
564 }
565}
566
567/**
568 * mei_hbm_cl_notify - signal notification event
569 *
570 * @dev: the device structure
571 * @cmd: notification client message
572 */
573static void mei_hbm_cl_notify(struct mei_device *dev,
574 struct mei_hbm_cl_cmd *cmd)
575{
576 struct mei_cl *cl;
577
578 cl = mei_hbm_cl_find_by_cmd(dev, cmd);
579 if (cl)
580 mei_cl_notify(cl);
581}
582
583/**
584 * mei_hbm_prop_req - request property for a single client
585 *
586 * @dev: the device structure
587 * @start_idx: client index to start search
588 *
589 * Return: 0 on success and < 0 on failure
590 */
591static int mei_hbm_prop_req(struct mei_device *dev, unsigned long start_idx)
592{
593 struct mei_msg_hdr mei_hdr;
594 struct hbm_props_request prop_req;
595 const size_t len = sizeof(struct hbm_props_request);
596 unsigned long addr;
597 int ret;
598
599 addr = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, start_idx);
600
601 /* We got all client properties */
602 if (addr == MEI_CLIENTS_MAX) {
603 dev->hbm_state = MEI_HBM_STARTED;
604 mei_host_client_init(dev);
605
606 return 0;
607 }
608
609 mei_hbm_hdr(&mei_hdr, len);
610
611 memset(&prop_req, 0, sizeof(struct hbm_props_request));
612
613 prop_req.hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
614 prop_req.me_addr = addr;
615
616 ret = mei_hbm_write_message(dev, &mei_hdr, &prop_req);
617 if (ret) {
618 dev_err(dev->dev, "properties request write failed: ret = %d\n",
619 ret);
620 return ret;
621 }
622
623 dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
624 mei_schedule_stall_timer(dev);
625
626 return 0;
627}
628
629/**
630 * mei_hbm_pg - sends pg command
631 *
632 * @dev: the device structure
633 * @pg_cmd: the pg command code
634 *
635 * Return: -EIO on write failure
636 * -EOPNOTSUPP if the operation is not supported by the protocol
637 */
638int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
639{
640 struct mei_msg_hdr mei_hdr;
641 struct hbm_power_gate req;
642 const size_t len = sizeof(struct hbm_power_gate);
643 int ret;
644
645 if (!dev->hbm_f_pg_supported)
646 return -EOPNOTSUPP;
647
648 mei_hbm_hdr(&mei_hdr, len);
649
650 memset(&req, 0, len);
651 req.hbm_cmd = pg_cmd;
652
653 ret = mei_hbm_write_message(dev, &mei_hdr, &req);
654 if (ret)
655 dev_err(dev->dev, "power gate command write failed.\n");
656 return ret;
657}
658EXPORT_SYMBOL_GPL(mei_hbm_pg);
659
660/**
661 * mei_hbm_stop_req - send stop request message
662 *
663 * @dev: mei device
664 *
665 * Return: -EIO on write failure
666 */
667static int mei_hbm_stop_req(struct mei_device *dev)
668{
669 struct mei_msg_hdr mei_hdr;
670 struct hbm_host_stop_request req;
671 const size_t len = sizeof(struct hbm_host_stop_request);
672
673 mei_hbm_hdr(&mei_hdr, len);
674
675 memset(&req, 0, len);
676 req.hbm_cmd = HOST_STOP_REQ_CMD;
677 req.reason = DRIVER_STOP_REQUEST;
678
679 return mei_hbm_write_message(dev, &mei_hdr, &req);
680}
681
682/**
683 * mei_hbm_cl_flow_control_req - sends flow control request.
684 *
685 * @dev: the device structure
686 * @cl: client info
687 *
688 * Return: -EIO on write failure
689 */
690int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
691{
692 struct hbm_flow_control req;
693
694 cl_dbg(dev, cl, "sending flow control\n");
695 return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD,
696 &req, sizeof(req));
697}
698
699/**
700 * mei_hbm_add_single_tx_flow_ctrl_creds - adds single buffer credentials.
701 *
702 * @dev: the device structure
703 * @fctrl: flow control response bus message
704 *
705 * Return: 0 on success, < 0 otherwise
706 */
707static int mei_hbm_add_single_tx_flow_ctrl_creds(struct mei_device *dev,
708 struct hbm_flow_control *fctrl)
709{
710 struct mei_me_client *me_cl;
711 int rets;
712
713 me_cl = mei_me_cl_by_id(dev, fctrl->me_addr);
714 if (!me_cl) {
715 dev_err(dev->dev, "no such me client %d\n", fctrl->me_addr);
716 return -ENOENT;
717 }
718
719 if (WARN_ON(me_cl->props.single_recv_buf == 0)) {
720 rets = -EINVAL;
721 goto out;
722 }
723
724 me_cl->tx_flow_ctrl_creds++;
725 dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
726 fctrl->me_addr, me_cl->tx_flow_ctrl_creds);
727
728 rets = 0;
729out:
730 mei_me_cl_put(me_cl);
731 return rets;
732}
733
734/**
735 * mei_hbm_cl_flow_control_res - flow control response from me
736 *
737 * @dev: the device structure
738 * @fctrl: flow control response bus message
739 */
740static void mei_hbm_cl_tx_flow_ctrl_creds_res(struct mei_device *dev,
741 struct hbm_flow_control *fctrl)
742{
743 struct mei_cl *cl;
744
745 if (!fctrl->host_addr) {
746 /* single receive buffer */
747 mei_hbm_add_single_tx_flow_ctrl_creds(dev, fctrl);
748 return;
749 }
750
751 cl = mei_hbm_cl_find_by_cmd(dev, fctrl);
752 if (cl) {
753 cl->tx_flow_ctrl_creds++;
754 cl_dbg(dev, cl, "flow control creds = %d.\n",
755 cl->tx_flow_ctrl_creds);
756 }
757}
758
759
760/**
761 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
762 *
763 * @dev: the device structure
764 * @cl: a client to disconnect from
765 *
766 * Return: -EIO on write failure
767 */
768int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
769{
770 struct hbm_client_connect_request req;
771
772 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD,
773 &req, sizeof(req));
774}
775
776/**
777 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
778 *
779 * @dev: the device structure
780 * @cl: a client to disconnect from
781 *
782 * Return: -EIO on write failure
783 */
784int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
785{
786 struct hbm_client_connect_response resp;
787
788 return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD,
789 &resp, sizeof(resp));
790}
791
792/**
793 * mei_hbm_cl_disconnect_res - update the client state according
794 * disconnect response
795 *
796 * @dev: the device structure
797 * @cl: mei host client
798 * @cmd: disconnect client response host bus message
799 */
800static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
801 struct mei_hbm_cl_cmd *cmd)
802{
803 struct hbm_client_connect_response *rs =
804 (struct hbm_client_connect_response *)cmd;
805
806 cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
807
808 if (rs->status == MEI_CL_DISCONN_SUCCESS)
809 cl->state = MEI_FILE_DISCONNECT_REPLY;
810 cl->status = 0;
811}
812
813/**
814 * mei_hbm_cl_connect_req - send connection request to specific me client
815 *
816 * @dev: the device structure
817 * @cl: a client to connect to
818 *
819 * Return: -EIO on write failure
820 */
821int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
822{
823 struct hbm_client_connect_request req;
824
825 return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD,
826 &req, sizeof(req));
827}
828
829/**
830 * mei_hbm_cl_connect_res - update the client state according
831 * connection response
832 *
833 * @dev: the device structure
834 * @cl: mei host client
835 * @cmd: connect client response host bus message
836 */
837static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
838 struct mei_hbm_cl_cmd *cmd)
839{
840 struct hbm_client_connect_response *rs =
841 (struct hbm_client_connect_response *)cmd;
842
843 cl_dbg(dev, cl, "hbm: connect response status=%s\n",
844 mei_cl_conn_status_str(rs->status));
845
846 if (rs->status == MEI_CL_CONN_SUCCESS)
847 cl->state = MEI_FILE_CONNECTED;
848 else {
849 cl->state = MEI_FILE_DISCONNECT_REPLY;
850 if (rs->status == MEI_CL_CONN_NOT_FOUND) {
851 mei_me_cl_del(dev, cl->me_cl);
852 if (dev->dev_state == MEI_DEV_ENABLED)
853 schedule_work(&dev->bus_rescan_work);
854 }
855 }
856 cl->status = mei_cl_conn_status_to_errno(rs->status);
857}
858
859/**
860 * mei_hbm_cl_res - process hbm response received on behalf
861 * an client
862 *
863 * @dev: the device structure
864 * @rs: hbm client message
865 * @fop_type: file operation type
866 */
867static void mei_hbm_cl_res(struct mei_device *dev,
868 struct mei_hbm_cl_cmd *rs,
869 enum mei_cb_file_ops fop_type)
870{
871 struct mei_cl *cl;
872 struct mei_cl_cb *cb, *next;
873
874 cl = NULL;
875 list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) {
876
877 cl = cb->cl;
878
879 if (cb->fop_type != fop_type)
880 continue;
881
882 if (mei_hbm_cl_addr_equal(cl, rs)) {
883 list_del_init(&cb->list);
884 break;
885 }
886 }
887
888 if (!cl)
889 return;
890
891 switch (fop_type) {
892 case MEI_FOP_CONNECT:
893 mei_hbm_cl_connect_res(dev, cl, rs);
894 break;
895 case MEI_FOP_DISCONNECT:
896 mei_hbm_cl_disconnect_res(dev, cl, rs);
897 break;
898 case MEI_FOP_NOTIFY_START:
899 mei_hbm_cl_notify_start_res(dev, cl, rs);
900 break;
901 case MEI_FOP_NOTIFY_STOP:
902 mei_hbm_cl_notify_stop_res(dev, cl, rs);
903 break;
904 default:
905 return;
906 }
907
908 cl->timer_count = 0;
909 wake_up(&cl->wait);
910}
911
912
913/**
914 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
915 * host sends disconnect response
916 *
917 * @dev: the device structure.
918 * @disconnect_req: disconnect request bus message from the me
919 *
920 * Return: -ENOMEM on allocation failure
921 */
922static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
923 struct hbm_client_connect_request *disconnect_req)
924{
925 struct mei_cl *cl;
926 struct mei_cl_cb *cb;
927
928 cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
929 if (cl) {
930 cl_warn(dev, cl, "fw disconnect request received\n");
931 cl->state = MEI_FILE_DISCONNECTING;
932 cl->timer_count = 0;
933
934 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT_RSP,
935 NULL);
936 if (!cb)
937 return -ENOMEM;
938 }
939 return 0;
940}
941
942/**
943 * mei_hbm_pg_enter_res - PG enter response received
944 *
945 * @dev: the device structure.
946 *
947 * Return: 0 on success, -EPROTO on state mismatch
948 */
949static int mei_hbm_pg_enter_res(struct mei_device *dev)
950{
951 if (mei_pg_state(dev) != MEI_PG_OFF ||
952 dev->pg_event != MEI_PG_EVENT_WAIT) {
953 dev_err(dev->dev, "hbm: pg entry response: state mismatch [%s, %d]\n",
954 mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
955 return -EPROTO;
956 }
957
958 dev->pg_event = MEI_PG_EVENT_RECEIVED;
959 wake_up(&dev->wait_pg);
960
961 return 0;
962}
963
964/**
965 * mei_hbm_pg_resume - process with PG resume
966 *
967 * @dev: the device structure.
968 */
969void mei_hbm_pg_resume(struct mei_device *dev)
970{
971 pm_request_resume(dev->dev);
972}
973EXPORT_SYMBOL_GPL(mei_hbm_pg_resume);
974
975/**
976 * mei_hbm_pg_exit_res - PG exit response received
977 *
978 * @dev: the device structure.
979 *
980 * Return: 0 on success, -EPROTO on state mismatch
981 */
982static int mei_hbm_pg_exit_res(struct mei_device *dev)
983{
984 if (mei_pg_state(dev) != MEI_PG_ON ||
985 (dev->pg_event != MEI_PG_EVENT_WAIT &&
986 dev->pg_event != MEI_PG_EVENT_IDLE)) {
987 dev_err(dev->dev, "hbm: pg exit response: state mismatch [%s, %d]\n",
988 mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
989 return -EPROTO;
990 }
991
992 switch (dev->pg_event) {
993 case MEI_PG_EVENT_WAIT:
994 dev->pg_event = MEI_PG_EVENT_RECEIVED;
995 wake_up(&dev->wait_pg);
996 break;
997 case MEI_PG_EVENT_IDLE:
998 /*
999 * If the driver is not waiting on this then
1000 * this is HW initiated exit from PG.
1001 * Start runtime pm resume sequence to exit from PG.
1002 */
1003 dev->pg_event = MEI_PG_EVENT_RECEIVED;
1004 mei_hbm_pg_resume(dev);
1005 break;
1006 default:
1007 WARN(1, "hbm: pg exit response: unexpected pg event = %d\n",
1008 dev->pg_event);
1009 return -EPROTO;
1010 }
1011
1012 return 0;
1013}
1014
1015/**
1016 * mei_hbm_config_features - check what hbm features and commands
1017 * are supported by the fw
1018 *
1019 * @dev: the device structure
1020 */
1021static void mei_hbm_config_features(struct mei_device *dev)
1022{
1023 /* Power Gating Isolation Support */
1024 dev->hbm_f_pg_supported = 0;
1025 if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
1026 dev->hbm_f_pg_supported = 1;
1027
1028 if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
1029 dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
1030 dev->hbm_f_pg_supported = 1;
1031
1032 if (dev->version.major_version >= HBM_MAJOR_VERSION_DC)
1033 dev->hbm_f_dc_supported = 1;
1034
1035 if (dev->version.major_version >= HBM_MAJOR_VERSION_IE)
1036 dev->hbm_f_ie_supported = 1;
1037
1038 /* disconnect on connect timeout instead of link reset */
1039 if (dev->version.major_version >= HBM_MAJOR_VERSION_DOT)
1040 dev->hbm_f_dot_supported = 1;
1041
1042 /* Notification Event Support */
1043 if (dev->version.major_version >= HBM_MAJOR_VERSION_EV)
1044 dev->hbm_f_ev_supported = 1;
1045
1046 /* Fixed Address Client Support */
1047 if (dev->version.major_version >= HBM_MAJOR_VERSION_FA)
1048 dev->hbm_f_fa_supported = 1;
1049
1050 /* OS ver message Support */
1051 if (dev->version.major_version >= HBM_MAJOR_VERSION_OS)
1052 dev->hbm_f_os_supported = 1;
1053
1054 /* DMA Ring Support */
1055 if (dev->version.major_version > HBM_MAJOR_VERSION_DR ||
1056 (dev->version.major_version == HBM_MAJOR_VERSION_DR &&
1057 dev->version.minor_version >= HBM_MINOR_VERSION_DR))
1058 dev->hbm_f_dr_supported = 1;
1059}
1060
1061/**
1062 * mei_hbm_version_is_supported - checks whether the driver can
1063 * support the hbm version of the device
1064 *
1065 * @dev: the device structure
1066 * Return: true if driver can support hbm version of the device
1067 */
1068bool mei_hbm_version_is_supported(struct mei_device *dev)
1069{
1070 return (dev->version.major_version < HBM_MAJOR_VERSION) ||
1071 (dev->version.major_version == HBM_MAJOR_VERSION &&
1072 dev->version.minor_version <= HBM_MINOR_VERSION);
1073}
1074
1075/**
1076 * mei_hbm_dispatch - bottom half read routine after ISR to
1077 * handle the read bus message cmd processing.
1078 *
1079 * @dev: the device structure
1080 * @hdr: header of bus message
1081 *
1082 * Return: 0 on success and < 0 on failure
1083 */
1084int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
1085{
1086 struct mei_bus_message *mei_msg;
1087 struct hbm_host_version_response *version_res;
1088 struct hbm_props_response *props_res;
1089 struct hbm_host_enum_response *enum_res;
1090 struct hbm_dma_setup_response *dma_setup_res;
1091 struct hbm_add_client_request *add_cl_req;
1092 int ret;
1093
1094 struct mei_hbm_cl_cmd *cl_cmd;
1095 struct hbm_client_connect_request *disconnect_req;
1096 struct hbm_flow_control *fctrl;
1097
1098 /* read the message to our buffer */
1099 BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
1100 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
1101 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
1102 cl_cmd = (struct mei_hbm_cl_cmd *)mei_msg;
1103
1104 /* ignore spurious message and prevent reset nesting
1105 * hbm is put to idle during system reset
1106 */
1107 if (dev->hbm_state == MEI_HBM_IDLE) {
1108 dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
1109 return 0;
1110 }
1111
1112 switch (mei_msg->hbm_cmd) {
1113 case HOST_START_RES_CMD:
1114 dev_dbg(dev->dev, "hbm: start: response message received.\n");
1115
1116 dev->init_clients_timer = 0;
1117
1118 version_res = (struct hbm_host_version_response *)mei_msg;
1119
1120 dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
1121 HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
1122 version_res->me_max_version.major_version,
1123 version_res->me_max_version.minor_version);
1124
1125 if (version_res->host_version_supported) {
1126 dev->version.major_version = HBM_MAJOR_VERSION;
1127 dev->version.minor_version = HBM_MINOR_VERSION;
1128 } else {
1129 dev->version.major_version =
1130 version_res->me_max_version.major_version;
1131 dev->version.minor_version =
1132 version_res->me_max_version.minor_version;
1133 }
1134
1135 if (!mei_hbm_version_is_supported(dev)) {
1136 dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
1137
1138 dev->hbm_state = MEI_HBM_STOPPED;
1139 if (mei_hbm_stop_req(dev)) {
1140 dev_err(dev->dev, "hbm: start: failed to send stop request\n");
1141 return -EIO;
1142 }
1143 break;
1144 }
1145
1146 mei_hbm_config_features(dev);
1147
1148 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1149 dev->hbm_state != MEI_HBM_STARTING) {
1150 dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
1151 dev->dev_state, dev->hbm_state);
1152 return -EPROTO;
1153 }
1154
1155 if (dev->hbm_f_dr_supported) {
1156 if (mei_dmam_ring_alloc(dev))
1157 dev_info(dev->dev, "running w/o dma ring\n");
1158 if (mei_dma_ring_is_allocated(dev)) {
1159 if (mei_hbm_dma_setup_req(dev))
1160 return -EIO;
1161
1162 wake_up(&dev->wait_hbm_start);
1163 break;
1164 }
1165 }
1166
1167 dev->hbm_f_dr_supported = 0;
1168 mei_dmam_ring_free(dev);
1169
1170 if (mei_hbm_enum_clients_req(dev))
1171 return -EIO;
1172
1173 wake_up(&dev->wait_hbm_start);
1174 break;
1175
1176 case MEI_HBM_DMA_SETUP_RES_CMD:
1177 dev_dbg(dev->dev, "hbm: dma setup response: message received.\n");
1178
1179 dev->init_clients_timer = 0;
1180
1181 if (dev->hbm_state != MEI_HBM_DR_SETUP) {
1182 dev_err(dev->dev, "hbm: dma setup response: state mismatch, [%d, %d]\n",
1183 dev->dev_state, dev->hbm_state);
1184 return -EPROTO;
1185 }
1186
1187 dma_setup_res = (struct hbm_dma_setup_response *)mei_msg;
1188
1189 if (dma_setup_res->status) {
1190 u8 status = dma_setup_res->status;
1191
1192 if (status == MEI_HBMS_NOT_ALLOWED) {
1193 dev_dbg(dev->dev, "hbm: dma setup not allowed\n");
1194 } else {
1195 dev_info(dev->dev, "hbm: dma setup response: failure = %d %s\n",
1196 status,
1197 mei_hbm_status_str(status));
1198 }
1199 dev->hbm_f_dr_supported = 0;
1200 mei_dmam_ring_free(dev);
1201 }
1202
1203 if (mei_hbm_enum_clients_req(dev))
1204 return -EIO;
1205 break;
1206
1207 case CLIENT_CONNECT_RES_CMD:
1208 dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
1209 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
1210 break;
1211
1212 case CLIENT_DISCONNECT_RES_CMD:
1213 dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
1214 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
1215 break;
1216
1217 case MEI_FLOW_CONTROL_CMD:
1218 dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
1219
1220 fctrl = (struct hbm_flow_control *)mei_msg;
1221 mei_hbm_cl_tx_flow_ctrl_creds_res(dev, fctrl);
1222 break;
1223
1224 case MEI_PG_ISOLATION_ENTRY_RES_CMD:
1225 dev_dbg(dev->dev, "hbm: power gate isolation entry response received\n");
1226 ret = mei_hbm_pg_enter_res(dev);
1227 if (ret)
1228 return ret;
1229 break;
1230
1231 case MEI_PG_ISOLATION_EXIT_REQ_CMD:
1232 dev_dbg(dev->dev, "hbm: power gate isolation exit request received\n");
1233 ret = mei_hbm_pg_exit_res(dev);
1234 if (ret)
1235 return ret;
1236 break;
1237
1238 case HOST_CLIENT_PROPERTIES_RES_CMD:
1239 dev_dbg(dev->dev, "hbm: properties response: message received.\n");
1240
1241 dev->init_clients_timer = 0;
1242
1243 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1244 dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
1245 dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
1246 dev->dev_state, dev->hbm_state);
1247 return -EPROTO;
1248 }
1249
1250 props_res = (struct hbm_props_response *)mei_msg;
1251
1252 if (props_res->status == MEI_HBMS_CLIENT_NOT_FOUND) {
1253 dev_dbg(dev->dev, "hbm: properties response: %d CLIENT_NOT_FOUND\n",
1254 props_res->me_addr);
1255 } else if (props_res->status) {
1256 dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
1257 props_res->status,
1258 mei_hbm_status_str(props_res->status));
1259 return -EPROTO;
1260 } else {
1261 mei_hbm_me_cl_add(dev, props_res);
1262 }
1263
1264 /* request property for the next client */
1265 if (mei_hbm_prop_req(dev, props_res->me_addr + 1))
1266 return -EIO;
1267
1268 break;
1269
1270 case HOST_ENUM_RES_CMD:
1271 dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
1272
1273 dev->init_clients_timer = 0;
1274
1275 enum_res = (struct hbm_host_enum_response *) mei_msg;
1276 BUILD_BUG_ON(sizeof(dev->me_clients_map)
1277 < sizeof(enum_res->valid_addresses));
1278 memcpy(dev->me_clients_map, enum_res->valid_addresses,
1279 sizeof(enum_res->valid_addresses));
1280
1281 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1282 dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
1283 dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
1284 dev->dev_state, dev->hbm_state);
1285 return -EPROTO;
1286 }
1287
1288 dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
1289
1290 /* first property request */
1291 if (mei_hbm_prop_req(dev, 0))
1292 return -EIO;
1293
1294 break;
1295
1296 case HOST_STOP_RES_CMD:
1297 dev_dbg(dev->dev, "hbm: stop response: message received\n");
1298
1299 dev->init_clients_timer = 0;
1300
1301 if (dev->hbm_state != MEI_HBM_STOPPED) {
1302 dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
1303 dev->dev_state, dev->hbm_state);
1304 return -EPROTO;
1305 }
1306
1307 dev->dev_state = MEI_DEV_POWER_DOWN;
1308 dev_info(dev->dev, "hbm: stop response: resetting.\n");
1309 /* force the reset */
1310 return -EPROTO;
1311 break;
1312
1313 case CLIENT_DISCONNECT_REQ_CMD:
1314 dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
1315
1316 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
1317 mei_hbm_fw_disconnect_req(dev, disconnect_req);
1318 break;
1319
1320 case ME_STOP_REQ_CMD:
1321 dev_dbg(dev->dev, "hbm: stop request: message received\n");
1322 dev->hbm_state = MEI_HBM_STOPPED;
1323 if (mei_hbm_stop_req(dev)) {
1324 dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
1325 return -EIO;
1326 }
1327 break;
1328
1329 case MEI_HBM_ADD_CLIENT_REQ_CMD:
1330 dev_dbg(dev->dev, "hbm: add client request received\n");
1331 /*
1332 * after the host receives the enum_resp
1333 * message clients may be added or removed
1334 */
1335 if (dev->hbm_state <= MEI_HBM_ENUM_CLIENTS ||
1336 dev->hbm_state >= MEI_HBM_STOPPED) {
1337 dev_err(dev->dev, "hbm: add client: state mismatch, [%d, %d]\n",
1338 dev->dev_state, dev->hbm_state);
1339 return -EPROTO;
1340 }
1341 add_cl_req = (struct hbm_add_client_request *)mei_msg;
1342 ret = mei_hbm_fw_add_cl_req(dev, add_cl_req);
1343 if (ret) {
1344 dev_err(dev->dev, "hbm: add client: failed to send response %d\n",
1345 ret);
1346 return -EIO;
1347 }
1348 dev_dbg(dev->dev, "hbm: add client request processed\n");
1349 break;
1350
1351 case MEI_HBM_NOTIFY_RES_CMD:
1352 dev_dbg(dev->dev, "hbm: notify response received\n");
1353 mei_hbm_cl_res(dev, cl_cmd, notify_res_to_fop(cl_cmd));
1354 break;
1355
1356 case MEI_HBM_NOTIFICATION_CMD:
1357 dev_dbg(dev->dev, "hbm: notification\n");
1358 mei_hbm_cl_notify(dev, cl_cmd);
1359 break;
1360
1361 default:
1362 WARN(1, "hbm: wrong command %d\n", mei_msg->hbm_cmd);
1363 return -EPROTO;
1364
1365 }
1366 return 0;
1367}
1368