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