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