mei: simplify handling of hbm client events
[firefly-linux-kernel-4.4.55.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  * This function returns -EIO on write failure
376  */
377 int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
378 {
379         struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
380         struct hbm_power_gate *req;
381         const size_t len = sizeof(struct hbm_power_gate);
382         int ret;
383
384         mei_hbm_hdr(mei_hdr, len);
385
386         req = (struct hbm_power_gate *)dev->wr_msg.data;
387         memset(req, 0, len);
388         req->hbm_cmd = pg_cmd;
389
390         ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
391         if (ret)
392                 dev_err(&dev->pdev->dev, "power gate command write failed.\n");
393         return ret;
394 }
395 EXPORT_SYMBOL_GPL(mei_hbm_pg);
396
397 /**
398  * mei_hbm_stop_req - send stop request message
399  *
400  * @dev - mei device
401  * @cl: client info
402  *
403  * This function returns -EIO on write failure
404  */
405 static int mei_hbm_stop_req(struct mei_device *dev)
406 {
407         struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
408         struct hbm_host_stop_request *req =
409                         (struct hbm_host_stop_request *)dev->wr_msg.data;
410         const size_t len = sizeof(struct hbm_host_stop_request);
411
412         mei_hbm_hdr(mei_hdr, len);
413
414         memset(req, 0, len);
415         req->hbm_cmd = HOST_STOP_REQ_CMD;
416         req->reason = DRIVER_STOP_REQUEST;
417
418         return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
419 }
420
421 /**
422  * mei_hbm_cl_flow_control_req - sends flow control request.
423  *
424  * @dev: the device structure
425  * @cl: client info
426  *
427  * This function returns -EIO on write failure
428  */
429 int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
430 {
431         const size_t len = sizeof(struct hbm_flow_control);
432         cl_dbg(dev, cl, "sending flow control\n");
433         return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD, len);
434 }
435
436 /**
437  * mei_hbm_add_single_flow_creds - adds single buffer credentials.
438  *
439  * @dev: the device structure
440  * @flow: flow control.
441  *
442  * return 0 on success, < 0 otherwise
443  */
444 static int mei_hbm_add_single_flow_creds(struct mei_device *dev,
445                                   struct hbm_flow_control *flow)
446 {
447         struct mei_me_client *me_cl;
448
449         me_cl = mei_me_cl_by_id(dev, flow->me_addr);
450         if (!me_cl) {
451                 dev_err(&dev->pdev->dev, "no such me client %d\n",
452                         flow->me_addr);
453                 return -ENOENT;
454         }
455
456         if (WARN_ON(me_cl->props.single_recv_buf == 0))
457                 return -EINVAL;
458
459         me_cl->mei_flow_ctrl_creds++;
460         dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
461             flow->me_addr, me_cl->mei_flow_ctrl_creds);
462
463         return 0;
464 }
465
466 /**
467  * mei_hbm_cl_flow_control_res - flow control response from me
468  *
469  * @dev: the device structure
470  * @flow_control: flow control response bus message
471  */
472 static void mei_hbm_cl_flow_control_res(struct mei_device *dev,
473                                         struct hbm_flow_control *flow_control)
474 {
475         struct mei_cl *cl;
476
477         if (!flow_control->host_addr) {
478                 /* single receive buffer */
479                 mei_hbm_add_single_flow_creds(dev, flow_control);
480                 return;
481         }
482
483         cl = mei_hbm_cl_find_by_cmd(dev, flow_control);
484         if (cl) {
485                 cl->mei_flow_ctrl_creds++;
486                 cl_dbg(dev, cl, "flow control creds = %d.\n",
487                                 cl->mei_flow_ctrl_creds);
488         }
489 }
490
491
492 /**
493  * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
494  *
495  * @dev: the device structure
496  * @cl: a client to disconnect from
497  *
498  * This function returns -EIO on write failure
499  */
500 int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
501 {
502         const size_t len = sizeof(struct hbm_client_connect_request);
503         return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD, len);
504 }
505
506 /**
507  * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
508  *
509  * @dev: the device structure
510  * @cl: a client to disconnect from
511  *
512  * This function returns -EIO on write failure
513  */
514 int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
515 {
516         const size_t len = sizeof(struct hbm_client_connect_response);
517         return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD, len);
518 }
519
520 /**
521  * mei_hbm_cl_disconnect_res - update the client state according
522  *       disconnect response
523  *
524  * @cl: mei host client
525  * @cmd: disconnect client response host bus message
526  */
527 static void mei_hbm_cl_disconnect_res(struct mei_cl *cl,
528                                       struct mei_hbm_cl_cmd *cmd)
529 {
530         struct hbm_client_connect_response *rs =
531                 (struct hbm_client_connect_response *)cmd;
532
533         dev_dbg(&cl->dev->pdev->dev, "hbm: disconnect response cl:host=%02d me=%02d status=%d\n",
534                         rs->me_addr, rs->host_addr, rs->status);
535
536         if (rs->status == MEI_CL_DISCONN_SUCCESS)
537                 cl->state = MEI_FILE_DISCONNECTED;
538         cl->status = 0;
539 }
540
541 /**
542  * mei_hbm_cl_connect_req - send connection request to specific me client
543  *
544  * @dev: the device structure
545  * @cl: a client to connect to
546  *
547  * returns -EIO on write failure
548  */
549 int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
550 {
551         const size_t len = sizeof(struct hbm_client_connect_request);
552         return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD, len);
553 }
554
555 /**
556  * mei_hbm_cl_connect_res - update the client state according
557  *        connection response
558  *
559  * @cl: mei host client
560  * @cmd: connect client response host bus message
561  */
562 static void mei_hbm_cl_connect_res(struct mei_cl *cl,
563                                    struct mei_hbm_cl_cmd *cmd)
564 {
565         struct hbm_client_connect_response *rs =
566                 (struct hbm_client_connect_response *)cmd;
567
568         dev_dbg(&cl->dev->pdev->dev, "hbm: connect response cl:host=%02d me=%02d status=%s\n",
569                         rs->me_addr, rs->host_addr,
570                         mei_cl_conn_status_str(rs->status));
571
572         if (rs->status == MEI_CL_CONN_SUCCESS)
573                 cl->state = MEI_FILE_CONNECTED;
574         else
575                 cl->state = MEI_FILE_DISCONNECTED;
576         cl->status = mei_cl_conn_status_to_errno(rs->status);
577 }
578
579 /**
580  * mei_hbm_cl_res - process hbm response received on behalf
581  *         an client
582  *
583  * @dev: the device structure
584  * @rs:  hbm client message
585  * @fop_type: file operation type
586  */
587 static void mei_hbm_cl_res(struct mei_device *dev,
588                            struct mei_hbm_cl_cmd *rs,
589                            enum mei_cb_file_ops fop_type)
590 {
591         struct mei_cl *cl;
592         struct mei_cl_cb *cb, *next;
593
594         cl = NULL;
595         list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
596
597                 cl = cb->cl;
598                 /* this should not happen */
599                 if (WARN_ON(!cl)) {
600                         list_del_init(&cb->list);
601                         continue;
602                 }
603
604                 if (cb->fop_type != fop_type)
605                         continue;
606
607                 if (mei_hbm_cl_addr_equal(cl, rs)) {
608                         list_del(&cb->list);
609                         break;
610                 }
611         }
612
613         if (!cl)
614                 return;
615
616         switch (fop_type) {
617         case MEI_FOP_CONNECT:
618                 mei_hbm_cl_connect_res(cl, rs);
619                 break;
620         case MEI_FOP_DISCONNECT:
621                 mei_hbm_cl_disconnect_res(cl, rs);
622                 break;
623         default:
624                 return;
625         }
626
627         cl->timer_count = 0;
628         wake_up(&cl->wait);
629 }
630
631
632 /**
633  * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
634  *  host sends disconnect response
635  *
636  * @dev: the device structure.
637  * @disconnect_req: disconnect request bus message from the me
638  *
639  * returns -ENOMEM on allocation failure
640  */
641 static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
642                 struct hbm_client_connect_request *disconnect_req)
643 {
644         struct mei_cl *cl;
645         struct mei_cl_cb *cb;
646
647         cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
648         if (cl) {
649                 cl_dbg(dev, cl, "disconnect request received\n");
650                 cl->state = MEI_FILE_DISCONNECTED;
651                 cl->timer_count = 0;
652
653                 cb = mei_io_cb_init(cl, NULL);
654                 if (!cb)
655                         return -ENOMEM;
656                 cb->fop_type = MEI_FOP_DISCONNECT_RSP;
657                 cl_dbg(dev, cl, "add disconnect response as first\n");
658                 list_add(&cb->list, &dev->ctrl_wr_list.list);
659         }
660         return 0;
661 }
662
663
664 /**
665  * mei_hbm_version_is_supported - checks whether the driver can
666  *     support the hbm version of the device
667  *
668  * @dev: the device structure
669  * returns true if driver can support hbm version of the device
670  */
671 bool mei_hbm_version_is_supported(struct mei_device *dev)
672 {
673         return  (dev->version.major_version < HBM_MAJOR_VERSION) ||
674                 (dev->version.major_version == HBM_MAJOR_VERSION &&
675                  dev->version.minor_version <= HBM_MINOR_VERSION);
676 }
677
678 /**
679  * mei_hbm_dispatch - bottom half read routine after ISR to
680  * handle the read bus message cmd processing.
681  *
682  * @dev: the device structure
683  * @mei_hdr: header of bus message
684  *
685  * returns 0 on success and < 0 on failure
686  */
687 int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
688 {
689         struct mei_bus_message *mei_msg;
690         struct hbm_host_version_response *version_res;
691         struct hbm_props_response *props_res;
692         struct hbm_host_enum_response *enum_res;
693
694         struct mei_hbm_cl_cmd *cl_cmd;
695         struct hbm_client_connect_request *disconnect_req;
696         struct hbm_flow_control *flow_control;
697
698         /* read the message to our buffer */
699         BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
700         mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
701         mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
702         cl_cmd  = (struct mei_hbm_cl_cmd *)mei_msg;
703
704         /* ignore spurious message and prevent reset nesting
705          * hbm is put to idle during system reset
706          */
707         if (dev->hbm_state == MEI_HBM_IDLE) {
708                 dev_dbg(&dev->pdev->dev, "hbm: state is idle ignore spurious messages\n");
709                 return 0;
710         }
711
712         switch (mei_msg->hbm_cmd) {
713         case HOST_START_RES_CMD:
714                 dev_dbg(&dev->pdev->dev, "hbm: start: response message received.\n");
715
716                 dev->init_clients_timer = 0;
717
718                 version_res = (struct hbm_host_version_response *)mei_msg;
719
720                 dev_dbg(&dev->pdev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
721                                 HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
722                                 version_res->me_max_version.major_version,
723                                 version_res->me_max_version.minor_version);
724
725                 if (version_res->host_version_supported) {
726                         dev->version.major_version = HBM_MAJOR_VERSION;
727                         dev->version.minor_version = HBM_MINOR_VERSION;
728                 } else {
729                         dev->version.major_version =
730                                 version_res->me_max_version.major_version;
731                         dev->version.minor_version =
732                                 version_res->me_max_version.minor_version;
733                 }
734
735                 if (!mei_hbm_version_is_supported(dev)) {
736                         dev_warn(&dev->pdev->dev, "hbm: start: version mismatch - stopping the driver.\n");
737
738                         dev->hbm_state = MEI_HBM_STOPPED;
739                         if (mei_hbm_stop_req(dev)) {
740                                 dev_err(&dev->pdev->dev, "hbm: start: failed to send stop request\n");
741                                 return -EIO;
742                         }
743                         break;
744                 }
745
746                 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
747                     dev->hbm_state != MEI_HBM_STARTING) {
748                         dev_err(&dev->pdev->dev, "hbm: start: state mismatch, [%d, %d]\n",
749                                 dev->dev_state, dev->hbm_state);
750                         return -EPROTO;
751                 }
752
753                 dev->hbm_state = MEI_HBM_STARTED;
754
755                 if (mei_hbm_enum_clients_req(dev)) {
756                         dev_err(&dev->pdev->dev, "hbm: start: failed to send enumeration request\n");
757                         return -EIO;
758                 }
759
760                 wake_up(&dev->wait_hbm_start);
761                 break;
762
763         case CLIENT_CONNECT_RES_CMD:
764                 dev_dbg(&dev->pdev->dev, "hbm: client connect response: message received.\n");
765                 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
766                 break;
767
768         case CLIENT_DISCONNECT_RES_CMD:
769                 dev_dbg(&dev->pdev->dev, "hbm: client disconnect response: message received.\n");
770                 mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
771                 break;
772
773         case MEI_FLOW_CONTROL_CMD:
774                 dev_dbg(&dev->pdev->dev, "hbm: client flow control response: message received.\n");
775
776                 flow_control = (struct hbm_flow_control *) mei_msg;
777                 mei_hbm_cl_flow_control_res(dev, flow_control);
778                 break;
779
780         case MEI_PG_ISOLATION_ENTRY_RES_CMD:
781                 dev_dbg(&dev->pdev->dev, "power gate isolation entry response received\n");
782                 dev->pg_event = MEI_PG_EVENT_RECEIVED;
783                 if (waitqueue_active(&dev->wait_pg))
784                         wake_up(&dev->wait_pg);
785                 break;
786
787         case MEI_PG_ISOLATION_EXIT_REQ_CMD:
788                 dev_dbg(&dev->pdev->dev, "power gate isolation exit request received\n");
789                 dev->pg_event = MEI_PG_EVENT_RECEIVED;
790                 if (waitqueue_active(&dev->wait_pg))
791                         wake_up(&dev->wait_pg);
792                 else
793                         /*
794                         * If the driver is not waiting on this then
795                         * this is HW initiated exit from PG.
796                         * Start runtime pm resume sequence to exit from PG.
797                         */
798                         pm_request_resume(&dev->pdev->dev);
799                 break;
800
801         case HOST_CLIENT_PROPERTIES_RES_CMD:
802                 dev_dbg(&dev->pdev->dev, "hbm: properties response: message received.\n");
803
804                 dev->init_clients_timer = 0;
805
806                 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
807                     dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
808                         dev_err(&dev->pdev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
809                                 dev->dev_state, dev->hbm_state);
810                         return -EPROTO;
811                 }
812
813                 props_res = (struct hbm_props_response *)mei_msg;
814
815                 if (props_res->status) {
816                         dev_err(&dev->pdev->dev, "hbm: properties response: wrong status = %d %s\n",
817                                 props_res->status,
818                                 mei_hbm_status_str(props_res->status));
819                         return -EPROTO;
820                 }
821
822                 mei_hbm_me_cl_add(dev, props_res);
823
824                 dev->me_client_index++;
825                 dev->me_client_presentation_num++;
826
827                 /* request property for the next client */
828                 if (mei_hbm_prop_req(dev))
829                         return -EIO;
830
831                 break;
832
833         case HOST_ENUM_RES_CMD:
834                 dev_dbg(&dev->pdev->dev, "hbm: enumeration response: message received\n");
835
836                 dev->init_clients_timer = 0;
837
838                 enum_res = (struct hbm_host_enum_response *) mei_msg;
839                 BUILD_BUG_ON(sizeof(dev->me_clients_map)
840                                 < sizeof(enum_res->valid_addresses));
841                 memcpy(dev->me_clients_map, enum_res->valid_addresses,
842                                 sizeof(enum_res->valid_addresses));
843
844                 if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
845                     dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
846                         dev_err(&dev->pdev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
847                                 dev->dev_state, dev->hbm_state);
848                         return -EPROTO;
849                 }
850
851                 dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
852
853                 /* first property request */
854                 if (mei_hbm_prop_req(dev))
855                         return -EIO;
856
857                 break;
858
859         case HOST_STOP_RES_CMD:
860                 dev_dbg(&dev->pdev->dev, "hbm: stop response: message received\n");
861
862                 dev->init_clients_timer = 0;
863
864                 if (dev->hbm_state != MEI_HBM_STOPPED) {
865                         dev_err(&dev->pdev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
866                                 dev->dev_state, dev->hbm_state);
867                         return -EPROTO;
868                 }
869
870                 dev->dev_state = MEI_DEV_POWER_DOWN;
871                 dev_info(&dev->pdev->dev, "hbm: stop response: resetting.\n");
872                 /* force the reset */
873                 return -EPROTO;
874                 break;
875
876         case CLIENT_DISCONNECT_REQ_CMD:
877                 dev_dbg(&dev->pdev->dev, "hbm: disconnect request: message received\n");
878
879                 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
880                 mei_hbm_fw_disconnect_req(dev, disconnect_req);
881                 break;
882
883         case ME_STOP_REQ_CMD:
884                 dev_dbg(&dev->pdev->dev, "hbm: stop request: message received\n");
885                 dev->hbm_state = MEI_HBM_STOPPED;
886                 if (mei_hbm_stop_req(dev)) {
887                         dev_err(&dev->pdev->dev, "hbm: stop request: failed to send stop request\n");
888                         return -EIO;
889                 }
890                 break;
891         default:
892                 BUG();
893                 break;
894
895         }
896         return 0;
897 }
898