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