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