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