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