Merge branch 'for-linus' of git://github.com/richardweinberger/linux
[pandora-kernel.git] / drivers / staging / mei / init.c
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2011, 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/delay.h>
21
22 #include "mei_dev.h"
23 #include "hw.h"
24 #include "interface.h"
25 #include "mei.h"
26
27 const uuid_le mei_amthi_guid  = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
28                                                 0xa8, 0x46, 0xe0, 0xff, 0x65,
29                                                 0x81, 0x4c);
30
31 /**
32  * mei_io_list_init - Sets up a queue list.
33  *
34  * @list: An instance io list structure
35  * @dev: the device structure
36  */
37 void mei_io_list_init(struct mei_io_list *list)
38 {
39         /* initialize our queue list */
40         INIT_LIST_HEAD(&list->mei_cb.cb_list);
41         list->status = 0;
42 }
43
44 /**
45  * mei_io_list_flush - removes list entry belonging to cl.
46  *
47  * @list:  An instance of our list structure
48  * @cl: private data of the file object
49  */
50 void mei_io_list_flush(struct mei_io_list *list, struct mei_cl *cl)
51 {
52         struct mei_cl_cb *cb_pos = NULL;
53         struct mei_cl_cb *cb_next = NULL;
54
55         if (list->status != 0)
56                 return;
57
58         if (list_empty(&list->mei_cb.cb_list))
59                 return;
60
61         list_for_each_entry_safe(cb_pos, cb_next,
62                                  &list->mei_cb.cb_list, cb_list) {
63                 if (cb_pos) {
64                         struct mei_cl *cl_tmp;
65                         cl_tmp = (struct mei_cl *)cb_pos->file_private;
66                         if (mei_cl_cmp_id(cl, cl_tmp))
67                                 list_del(&cb_pos->cb_list);
68                 }
69         }
70 }
71 /**
72  * mei_cl_flush_queues - flushes queue lists belonging to cl.
73  *
74  * @dev: the device structure
75  * @cl: private data of the file object
76  */
77 int mei_cl_flush_queues(struct mei_cl *cl)
78 {
79         if (!cl || !cl->dev)
80                 return -EINVAL;
81
82         dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
83         mei_io_list_flush(&cl->dev->read_list, cl);
84         mei_io_list_flush(&cl->dev->write_list, cl);
85         mei_io_list_flush(&cl->dev->write_waiting_list, cl);
86         mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
87         mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
88         mei_io_list_flush(&cl->dev->amthi_cmd_list, cl);
89         mei_io_list_flush(&cl->dev->amthi_read_complete_list, cl);
90         return 0;
91 }
92
93
94
95 /**
96  * mei_reset_iamthif_params - initializes mei device iamthif
97  *
98  * @dev: the device structure
99  */
100 static void mei_reset_iamthif_params(struct mei_device *dev)
101 {
102         /* reset iamthif parameters. */
103         dev->iamthif_current_cb = NULL;
104         dev->iamthif_msg_buf_size = 0;
105         dev->iamthif_msg_buf_index = 0;
106         dev->iamthif_canceled = false;
107         dev->iamthif_ioctl = false;
108         dev->iamthif_state = MEI_IAMTHIF_IDLE;
109         dev->iamthif_timer = 0;
110 }
111
112 /**
113  * init_mei_device - allocates and initializes the mei device structure
114  *
115  * @pdev: The pci device structure
116  *
117  * returns The mei_device_device pointer on success, NULL on failure.
118  */
119 struct mei_device *mei_device_init(struct pci_dev *pdev)
120 {
121         struct mei_device *dev;
122
123         dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
124         if (!dev)
125                 return NULL;
126
127         /* setup our list array */
128         INIT_LIST_HEAD(&dev->file_list);
129         INIT_LIST_HEAD(&dev->wd_cl.link);
130         INIT_LIST_HEAD(&dev->iamthif_cl.link);
131         mutex_init(&dev->device_lock);
132         init_waitqueue_head(&dev->wait_recvd_msg);
133         init_waitqueue_head(&dev->wait_stop_wd);
134         dev->mei_state = MEI_INITIALIZING;
135         dev->iamthif_state = MEI_IAMTHIF_IDLE;
136         dev->wd_interface_reg = false;
137
138
139         mei_io_list_init(&dev->read_list);
140         mei_io_list_init(&dev->write_list);
141         mei_io_list_init(&dev->write_waiting_list);
142         mei_io_list_init(&dev->ctrl_wr_list);
143         mei_io_list_init(&dev->ctrl_rd_list);
144         mei_io_list_init(&dev->amthi_cmd_list);
145         mei_io_list_init(&dev->amthi_read_complete_list);
146         dev->pdev = pdev;
147         return dev;
148 }
149
150 /**
151  * mei_hw_init - initializes host and fw to start work.
152  *
153  * @dev: the device structure
154  *
155  * returns 0 on success, <0 on failure.
156  */
157 int mei_hw_init(struct mei_device *dev)
158 {
159         int err = 0;
160         int ret;
161
162         mutex_lock(&dev->device_lock);
163
164         dev->host_hw_state = mei_hcsr_read(dev);
165         dev->me_hw_state = mei_mecsr_read(dev);
166         dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
167             dev->host_hw_state, dev->me_hw_state);
168
169         /* acknowledge interrupt and stop interupts */
170         if ((dev->host_hw_state & H_IS) == H_IS)
171                 mei_reg_write(dev, H_CSR, dev->host_hw_state);
172
173         dev->recvd_msg = false;
174         dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
175
176         mei_reset(dev, 1);
177
178         dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
179             dev->host_hw_state, dev->me_hw_state);
180
181         /* wait for ME to turn on ME_RDY */
182         if (!dev->recvd_msg) {
183                 mutex_unlock(&dev->device_lock);
184                 err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
185                         dev->recvd_msg, MEI_INTEROP_TIMEOUT);
186                 mutex_lock(&dev->device_lock);
187         }
188
189         if (err <= 0 && !dev->recvd_msg) {
190                 dev->mei_state = MEI_DISABLED;
191                 dev_dbg(&dev->pdev->dev,
192                         "wait_event_interruptible_timeout failed"
193                         "on wait for ME to turn on ME_RDY.\n");
194                 ret = -ENODEV;
195                 goto out;
196         }
197
198         if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
199               ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
200                 dev->mei_state = MEI_DISABLED;
201                 dev_dbg(&dev->pdev->dev,
202                         "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
203                         dev->host_hw_state, dev->me_hw_state);
204
205                 if (!(dev->host_hw_state & H_RDY))
206                         dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
207
208                 if (!(dev->me_hw_state & ME_RDY_HRA))
209                         dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
210
211                 printk(KERN_ERR "mei: link layer initialization failed.\n");
212                 ret = -ENODEV;
213                 goto out;
214         }
215
216         if (dev->version.major_version != HBM_MAJOR_VERSION ||
217             dev->version.minor_version != HBM_MINOR_VERSION) {
218                 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
219                 ret = -ENODEV;
220                 goto out;
221         }
222
223         dev->recvd_msg = false;
224         dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
225             dev->host_hw_state, dev->me_hw_state);
226         dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
227         dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
228         dev_dbg(&dev->pdev->dev, "MEI  start success.\n");
229         ret = 0;
230
231 out:
232         mutex_unlock(&dev->device_lock);
233         return ret;
234 }
235
236 /**
237  * mei_hw_reset - resets fw via mei csr register.
238  *
239  * @dev: the device structure
240  * @interrupts_enabled: if interrupt should be enabled after reset.
241  */
242 static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
243 {
244         dev->host_hw_state |= (H_RST | H_IG);
245
246         if (interrupts_enabled)
247                 mei_enable_interrupts(dev);
248         else
249                 mei_disable_interrupts(dev);
250 }
251
252 /**
253  * mei_reset - resets host and fw.
254  *
255  * @dev: the device structure
256  * @interrupts_enabled: if interrupt should be enabled after reset.
257  */
258 void mei_reset(struct mei_device *dev, int interrupts_enabled)
259 {
260         struct mei_cl *cl_pos = NULL;
261         struct mei_cl *cl_next = NULL;
262         struct mei_cl_cb *cb_pos = NULL;
263         struct mei_cl_cb *cb_next = NULL;
264         bool unexpected;
265
266         if (dev->mei_state == MEI_RECOVERING_FROM_RESET) {
267                 dev->need_reset = true;
268                 return;
269         }
270
271         unexpected = (dev->mei_state != MEI_INITIALIZING &&
272                         dev->mei_state != MEI_DISABLED &&
273                         dev->mei_state != MEI_POWER_DOWN &&
274                         dev->mei_state != MEI_POWER_UP);
275
276         dev->host_hw_state = mei_hcsr_read(dev);
277
278         dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
279             dev->host_hw_state);
280
281         mei_hw_reset(dev, interrupts_enabled);
282
283         dev->host_hw_state &= ~H_RST;
284         dev->host_hw_state |= H_IG;
285
286         mei_hcsr_set(dev);
287
288         dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
289             dev->host_hw_state);
290
291         dev->need_reset = false;
292
293         if (dev->mei_state != MEI_INITIALIZING) {
294                 if (dev->mei_state != MEI_DISABLED &&
295                     dev->mei_state != MEI_POWER_DOWN)
296                         dev->mei_state = MEI_RESETING;
297
298                 list_for_each_entry_safe(cl_pos,
299                                 cl_next, &dev->file_list, link) {
300                         cl_pos->state = MEI_FILE_DISCONNECTED;
301                         cl_pos->mei_flow_ctrl_creds = 0;
302                         cl_pos->read_cb = NULL;
303                         cl_pos->timer_count = 0;
304                 }
305                 /* remove entry if already in list */
306                 dev_dbg(&dev->pdev->dev, "list del iamthif and wd file list.\n");
307                 mei_remove_client_from_file_list(dev,
308                                 dev->wd_cl.host_client_id);
309
310                 mei_remove_client_from_file_list(dev,
311                                 dev->iamthif_cl.host_client_id);
312
313                 mei_reset_iamthif_params(dev);
314                 dev->wd_due_counter = 0;
315                 dev->extra_write_index = 0;
316         }
317
318         dev->me_clients_num = 0;
319         dev->rd_msg_hdr = 0;
320         dev->stop = false;
321         dev->wd_pending = false;
322
323         /* update the state of the registers after reset */
324         dev->host_hw_state = mei_hcsr_read(dev);
325         dev->me_hw_state = mei_mecsr_read(dev);
326
327         dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
328             dev->host_hw_state, dev->me_hw_state);
329
330         if (unexpected)
331                 dev_warn(&dev->pdev->dev, "unexpected reset.\n");
332
333         /* Wake up all readings so they can be interrupted */
334         list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
335                 if (waitqueue_active(&cl_pos->rx_wait)) {
336                         dev_dbg(&dev->pdev->dev, "Waking up client!\n");
337                         wake_up_interruptible(&cl_pos->rx_wait);
338                 }
339         }
340         /* remove all waiting requests */
341         if (dev->write_list.status == 0 &&
342                 !list_empty(&dev->write_list.mei_cb.cb_list)) {
343                 list_for_each_entry_safe(cb_pos, cb_next,
344                                 &dev->write_list.mei_cb.cb_list, cb_list) {
345                         if (cb_pos) {
346                                 list_del(&cb_pos->cb_list);
347                                 mei_free_cb_private(cb_pos);
348                                 cb_pos = NULL;
349                         }
350                 }
351         }
352 }
353
354
355
356 /**
357  * host_start_message - mei host sends start message.
358  *
359  * @dev: the device structure
360  *
361  * returns none.
362  */
363 void mei_host_start_message(struct mei_device *dev)
364 {
365         struct mei_msg_hdr *mei_hdr;
366         struct hbm_host_version_request *host_start_req;
367
368         /* host start message */
369         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
370         mei_hdr->host_addr = 0;
371         mei_hdr->me_addr = 0;
372         mei_hdr->length = sizeof(struct hbm_host_version_request);
373         mei_hdr->msg_complete = 1;
374         mei_hdr->reserved = 0;
375
376         host_start_req =
377             (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
378         memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
379         host_start_req->cmd.cmd = HOST_START_REQ_CMD;
380         host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
381         host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
382         dev->recvd_msg = false;
383         if (!mei_write_message(dev, mei_hdr,
384                                        (unsigned char *) (host_start_req),
385                                        mei_hdr->length)) {
386                 dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
387                 dev->mei_state = MEI_RESETING;
388                 mei_reset(dev, 1);
389         }
390         dev->init_clients_state = MEI_START_MESSAGE;
391         dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
392         return ;
393 }
394
395 /**
396  * host_enum_clients_message - host sends enumeration client request message.
397  *
398  * @dev: the device structure
399  *
400  * returns none.
401  */
402 void mei_host_enum_clients_message(struct mei_device *dev)
403 {
404         struct mei_msg_hdr *mei_hdr;
405         struct hbm_host_enum_request *host_enum_req;
406         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
407         /* enumerate clients */
408         mei_hdr->host_addr = 0;
409         mei_hdr->me_addr = 0;
410         mei_hdr->length = sizeof(struct hbm_host_enum_request);
411         mei_hdr->msg_complete = 1;
412         mei_hdr->reserved = 0;
413
414         host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
415         memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
416         host_enum_req->cmd.cmd = HOST_ENUM_REQ_CMD;
417         if (!mei_write_message(dev, mei_hdr,
418                                (unsigned char *) (host_enum_req),
419                                 mei_hdr->length)) {
420                 dev->mei_state = MEI_RESETING;
421                 dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
422                 mei_reset(dev, 1);
423         }
424         dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
425         dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
426         return ;
427 }
428
429
430 /**
431  * allocate_me_clients_storage - allocates storage for me clients
432  *
433  * @dev: the device structure
434  *
435  * returns none.
436  */
437 void mei_allocate_me_clients_storage(struct mei_device *dev)
438 {
439         struct mei_me_client *clients;
440         int b;
441
442         /* count how many ME clients we have */
443         for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
444                 dev->me_clients_num++;
445
446         if (dev->me_clients_num <= 0)
447                 return ;
448
449
450         if (dev->me_clients != NULL) {
451                 kfree(dev->me_clients);
452                 dev->me_clients = NULL;
453         }
454         dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
455                 dev->me_clients_num * sizeof(struct mei_me_client));
456         /* allocate storage for ME clients representation */
457         clients = kcalloc(dev->me_clients_num,
458                         sizeof(struct mei_me_client), GFP_KERNEL);
459         if (!clients) {
460                 dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
461                 dev->mei_state = MEI_RESETING;
462                 mei_reset(dev, 1);
463                 return ;
464         }
465         dev->me_clients = clients;
466         return ;
467 }
468 /**
469  * host_client_properties - reads properties for client
470  *
471  * @dev: the device structure
472  *
473  * returns:
474  *      < 0 - Error.
475  *  = 0 - no more clients.
476  *  = 1 - still have clients to send properties request.
477  */
478 int mei_host_client_properties(struct mei_device *dev)
479 {
480         struct mei_msg_hdr *mei_header;
481         struct hbm_props_request *host_cli_req;
482         int b;
483         u8 client_num = dev->me_client_presentation_num;
484
485         b = dev->me_client_index;
486         b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
487         if (b < MEI_CLIENTS_MAX) {
488                 dev->me_clients[client_num].client_id = b;
489                 dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
490                 mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
491                 mei_header->host_addr = 0;
492                 mei_header->me_addr = 0;
493                 mei_header->length = sizeof(struct hbm_props_request);
494                 mei_header->msg_complete = 1;
495                 mei_header->reserved = 0;
496
497                 host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
498
499                 memset(host_cli_req, 0, sizeof(struct hbm_props_request));
500
501                 host_cli_req->cmd.cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
502                 host_cli_req->address = b;
503
504                 if (!mei_write_message(dev, mei_header,
505                                 (unsigned char *)host_cli_req,
506                                 mei_header->length)) {
507                         dev->mei_state = MEI_RESETING;
508                         dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
509                         mei_reset(dev, 1);
510                         return -EIO;
511                 }
512
513                 dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
514                 dev->me_client_index = b;
515                 return 1;
516         }
517
518         return 0;
519 }
520
521 /**
522  * mei_init_file_private - initializes private file structure.
523  *
524  * @priv: private file structure to be initialized
525  * @file: the file structure
526  */
527 void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
528 {
529         memset(priv, 0, sizeof(struct mei_cl));
530         init_waitqueue_head(&priv->wait);
531         init_waitqueue_head(&priv->rx_wait);
532         init_waitqueue_head(&priv->tx_wait);
533         INIT_LIST_HEAD(&priv->link);
534         priv->reading_state = MEI_IDLE;
535         priv->writing_state = MEI_IDLE;
536         priv->dev = dev;
537 }
538
539 int mei_find_me_client_index(const struct mei_device *dev, uuid_le cuuid)
540 {
541         int i, res = -1;
542
543         for (i = 0; i < dev->me_clients_num; ++i)
544                 if (uuid_le_cmp(cuuid,
545                                 dev->me_clients[i].props.protocol_name) == 0) {
546                         res = i;
547                         break;
548                 }
549
550         return res;
551 }
552
553
554 /**
555  * mei_find_me_client_update_filext - searches for ME client guid
556  *                       sets client_id in mei_file_private if found
557  * @dev: the device structure
558  * @priv: private file structure to set client_id in
559  * @cguid: searched guid of ME client
560  * @client_id: id of host client to be set in file private structure
561  *
562  * returns ME client index
563  */
564 u8 mei_find_me_client_update_filext(struct mei_device *dev, struct mei_cl *priv,
565                                 const uuid_le *cguid, u8 client_id)
566 {
567         int i;
568
569         if (!dev || !priv || !cguid)
570                 return 0;
571
572         /* check for valid client id */
573         i = mei_find_me_client_index(dev, *cguid);
574         if (i >= 0) {
575                 priv->me_client_id = dev->me_clients[i].client_id;
576                 priv->state = MEI_FILE_CONNECTING;
577                 priv->host_client_id = client_id;
578
579                 list_add_tail(&priv->link, &dev->file_list);
580                 return (u8)i;
581         }
582
583         return 0;
584 }
585
586 /**
587  * host_init_iamthif - mei initialization iamthif client.
588  *
589  * @dev: the device structure
590  *
591  */
592 void mei_host_init_iamthif(struct mei_device *dev)
593 {
594         u8 i;
595         unsigned char *msg_buf;
596
597         mei_cl_init(&dev->iamthif_cl, dev);
598         dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
599
600         /* find ME amthi client */
601         i = mei_find_me_client_update_filext(dev, &dev->iamthif_cl,
602                             &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
603         if (dev->iamthif_cl.state != MEI_FILE_CONNECTING) {
604                 dev_dbg(&dev->pdev->dev, "failed to find iamthif client.\n");
605                 return;
606         }
607
608         /* Do not render the system unusable when iamthif_mtu is not equal to
609         the value received from ME.
610         Assign iamthif_mtu to the value received from ME in order to solve the
611         hardware macro incompatibility. */
612
613         dev_dbg(&dev->pdev->dev, "[DEFAULT] IAMTHIF = %d\n", dev->iamthif_mtu);
614         dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
615         dev_dbg(&dev->pdev->dev,
616                         "IAMTHIF = %d\n",
617                         dev->me_clients[i].props.max_msg_length);
618
619         kfree(dev->iamthif_msg_buf);
620         dev->iamthif_msg_buf = NULL;
621
622         /* allocate storage for ME message buffer */
623         msg_buf = kcalloc(dev->iamthif_mtu,
624                         sizeof(unsigned char), GFP_KERNEL);
625         if (!msg_buf) {
626                 dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
627                 return;
628         }
629
630         dev->iamthif_msg_buf = msg_buf;
631
632         if (!mei_connect(dev, &dev->iamthif_cl)) {
633                 dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
634                 dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
635                 dev->iamthif_cl.host_client_id = 0;
636         } else {
637                 dev->iamthif_cl.timer_count = CONNECT_TIMEOUT;
638         }
639 }
640
641 /**
642  * mei_alloc_file_private - allocates a private file structure and sets it up.
643  * @file: the file structure
644  *
645  * returns  The allocated file or NULL on failure
646  */
647 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
648 {
649         struct mei_cl *cl;
650
651         cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
652         if (!cl)
653                 return NULL;
654
655         mei_cl_init(cl, dev);
656
657         return cl;
658 }
659
660
661
662 /**
663  * mei_disconnect_host_client - sends disconnect message to fw from host client.
664  *
665  * @dev: the device structure
666  * @cl: private data of the file object
667  *
668  * Locking: called under "dev->device_lock" lock
669  *
670  * returns 0 on success, <0 on failure.
671  */
672 int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
673 {
674         int rets, err;
675         long timeout = 15;      /* 15 seconds */
676         struct mei_cl_cb *cb;
677
678         if (!dev || !cl)
679                 return -ENODEV;
680
681         if (cl->state != MEI_FILE_DISCONNECTING)
682                 return 0;
683
684         cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
685         if (!cb)
686                 return -ENOMEM;
687
688         INIT_LIST_HEAD(&cb->cb_list);
689         cb->file_private = cl;
690         cb->major_file_operations = MEI_CLOSE;
691         if (dev->mei_host_buffer_is_empty) {
692                 dev->mei_host_buffer_is_empty = false;
693                 if (mei_disconnect(dev, cl)) {
694                         mdelay(10); /* Wait for hardware disconnection ready */
695                         list_add_tail(&cb->cb_list,
696                                 &dev->ctrl_rd_list.mei_cb.cb_list);
697                 } else {
698                         rets = -ENODEV;
699                         dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
700                         goto free;
701                 }
702         } else {
703                 dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
704                 list_add_tail(&cb->cb_list,
705                                 &dev->ctrl_wr_list.mei_cb.cb_list);
706         }
707         mutex_unlock(&dev->device_lock);
708
709         err = wait_event_timeout(dev->wait_recvd_msg,
710                  (MEI_FILE_DISCONNECTED == cl->state),
711                  timeout * HZ);
712
713         mutex_lock(&dev->device_lock);
714         if (MEI_FILE_DISCONNECTED == cl->state) {
715                 rets = 0;
716                 dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
717         } else {
718                 rets = -ENODEV;
719                 if (MEI_FILE_DISCONNECTED != cl->state)
720                         dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
721
722                 if (err)
723                         dev_dbg(&dev->pdev->dev,
724                                         "wait failed disconnect err=%08x\n",
725                                         err);
726
727                 dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
728         }
729
730         mei_io_list_flush(&dev->ctrl_rd_list, cl);
731         mei_io_list_flush(&dev->ctrl_wr_list, cl);
732 free:
733         mei_free_cb_private(cb);
734         return rets;
735 }
736
737 /**
738  * mei_remove_client_from_file_list -
739  *      removes file private data from device file list
740  *
741  * @dev: the device structure
742  * @host_client_id: host client id to be removed
743  */
744 void mei_remove_client_from_file_list(struct mei_device *dev,
745                                        u8 host_client_id)
746 {
747         struct mei_cl *cl_pos = NULL;
748         struct mei_cl *cl_next = NULL;
749         list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
750                 if (host_client_id == cl_pos->host_client_id) {
751                         dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
752                                         cl_pos->host_client_id,
753                                         cl_pos->me_client_id);
754                         list_del_init(&cl_pos->link);
755                         break;
756                 }
757         }
758 }