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