virtio: console: fix error handling in init() function
[deliverable/linux.git] / drivers / misc / mei / main.c
CommitLineData
ab841160
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
733ba91c 4 * Copyright (c) 2003-2012, Intel Corporation.
ab841160
OW
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
2f3d2b49
TW
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
ab841160
OW
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/kernel.h>
22#include <linux/device.h>
23#include <linux/fs.h>
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/fcntl.h>
27#include <linux/aio.h>
28#include <linux/pci.h>
29#include <linux/poll.h>
30#include <linux/init.h>
31#include <linux/ioctl.h>
32#include <linux/cdev.h>
ab841160
OW
33#include <linux/sched.h>
34#include <linux/uuid.h>
35#include <linux/compat.h>
36#include <linux/jiffies.h>
37#include <linux/interrupt.h>
5b881e3c 38#include <linux/miscdevice.h>
ab841160
OW
39
40#include "mei_dev.h"
4f3afe1d 41#include <linux/mei.h>
ab841160 42#include "interface.h"
ab841160 43
ab841160
OW
44/* The device pointer */
45/* Currently this driver works as long as there is only a single AMT device. */
4a3cafd5 46struct pci_dev *mei_device;
ab841160 47
ab841160
OW
48/* mei_pci_tbl - PCI Device ID Table */
49static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
50 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
81
82 /* required last entry */
83 {0, }
84};
85
86MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
87
88static DEFINE_MUTEX(mei_mutex);
89
ab841160
OW
90
91/**
92 * mei_clear_list - removes all callbacks associated with file
93 * from mei_cb_list
94 *
95 * @dev: device structure.
96 * @file: file structure
97 * @mei_cb_list: callbacks list
98 *
99 * mei_clear_list is called to clear resources associated with file
100 * when application calls close function or Ctrl-C was pressed
101 *
102 * returns true if callback removed from the list, false otherwise
103 */
104static bool mei_clear_list(struct mei_device *dev,
105 struct file *file, struct list_head *mei_cb_list)
106{
107 struct mei_cl_cb *cb_pos = NULL;
108 struct mei_cl_cb *cb_next = NULL;
109 struct file *file_temp;
110 bool removed = false;
111
112 /* list all list member */
113 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
114 file_temp = (struct file *)cb_pos->file_object;
115 /* check if list member associated with a file */
116 if (file_temp == file) {
117 /* remove member from the list */
118 list_del(&cb_pos->cb_list);
119 /* check if cb equal to current iamthif cb */
120 if (dev->iamthif_current_cb == cb_pos) {
121 dev->iamthif_current_cb = NULL;
122 /* send flow control to iamthif client */
123 mei_send_flow_control(dev, &dev->iamthif_cl);
124 }
125 /* free all allocated buffers */
126 mei_free_cb_private(cb_pos);
127 cb_pos = NULL;
128 removed = true;
129 }
130 }
131 return removed;
132}
133
134/**
135 * mei_clear_lists - removes all callbacks associated with file
136 *
137 * @dev: device structure
138 * @file: file structure
139 *
140 * mei_clear_lists is called to clear resources associated with file
141 * when application calls close function or Ctrl-C was pressed
142 *
143 * returns true if callback removed from the list, false otherwise
144 */
145static bool mei_clear_lists(struct mei_device *dev, struct file *file)
146{
147 bool removed = false;
148
149 /* remove callbacks associated with a file */
150 mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
151 if (mei_clear_list(dev, file,
152 &dev->amthi_read_complete_list.mei_cb.cb_list))
153 removed = true;
154
155 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
156
157 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
158 removed = true;
159
160 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
161 removed = true;
162
163 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
164 removed = true;
165
166 /* check if iamthif_current_cb not NULL */
167 if (dev->iamthif_current_cb && !removed) {
168 /* check file and iamthif current cb association */
169 if (dev->iamthif_current_cb->file_object == file) {
170 /* remove cb */
171 mei_free_cb_private(dev->iamthif_current_cb);
172 dev->iamthif_current_cb = NULL;
173 removed = true;
174 }
175 }
176 return removed;
177}
178/**
179 * find_read_list_entry - find read list entry
180 *
181 * @dev: device structure
182 * @file: pointer to file structure
183 *
184 * returns cb on success, NULL on error
185 */
186static struct mei_cl_cb *find_read_list_entry(
187 struct mei_device *dev,
188 struct mei_cl *cl)
189{
b7cd2d9f
TW
190 struct mei_cl_cb *pos = NULL;
191 struct mei_cl_cb *next = NULL;
ab841160 192
b7cd2d9f
TW
193 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
194 list_for_each_entry_safe(pos, next,
195 &dev->read_list.mei_cb.cb_list, cb_list) {
196 struct mei_cl *cl_temp;
197 cl_temp = (struct mei_cl *)pos->file_private;
ab841160 198
b7cd2d9f
TW
199 if (mei_cl_cmp_id(cl, cl_temp))
200 return pos;
ab841160
OW
201 }
202 return NULL;
203}
204
205/**
206 * mei_open - the open function
207 *
208 * @inode: pointer to inode structure
209 * @file: pointer to file structure
210 *
211 * returns 0 on success, <0 on error
212 */
213static int mei_open(struct inode *inode, struct file *file)
214{
215 struct mei_cl *cl;
ab841160 216 struct mei_device *dev;
6f37aca8
TW
217 unsigned long cl_id;
218 int err;
ab841160
OW
219
220 err = -ENODEV;
221 if (!mei_device)
222 goto out;
223
224 dev = pci_get_drvdata(mei_device);
5b881e3c 225 if (!dev)
ab841160
OW
226 goto out;
227
228 mutex_lock(&dev->device_lock);
229 err = -ENOMEM;
c95efb74 230 cl = mei_cl_allocate(dev);
ab841160 231 if (!cl)
303dfbf5 232 goto out_unlock;
ab841160
OW
233
234 err = -ENODEV;
b210d750
TW
235 if (dev->dev_state != MEI_DEV_ENABLED) {
236 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
237 mei_dev_state_str(dev->dev_state));
ab841160
OW
238 goto out_unlock;
239 }
240 err = -EMFILE;
241 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
242 goto out_unlock;
243
6f37aca8
TW
244 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
245 if (cl_id >= MEI_CLIENTS_MAX)
ab841160
OW
246 goto out_unlock;
247
6f37aca8
TW
248 cl->host_client_id = cl_id;
249
ab841160
OW
250 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
251
252 dev->open_handle_count++;
6f37aca8 253
ab841160
OW
254 list_add_tail(&cl->link, &dev->file_list);
255
256 set_bit(cl->host_client_id, dev->host_clients_map);
257 cl->state = MEI_FILE_INITIALIZING;
258 cl->sm_state = 0;
259
260 file->private_data = cl;
261 mutex_unlock(&dev->device_lock);
262
5b881e3c 263 return nonseekable_open(inode, file);
ab841160
OW
264
265out_unlock:
266 mutex_unlock(&dev->device_lock);
267 kfree(cl);
268out:
269 return err;
270}
271
272/**
273 * mei_release - the release function
274 *
275 * @inode: pointer to inode structure
276 * @file: pointer to file structure
277 *
278 * returns 0 on success, <0 on error
279 */
280static int mei_release(struct inode *inode, struct file *file)
281{
282 struct mei_cl *cl = file->private_data;
283 struct mei_cl_cb *cb;
284 struct mei_device *dev;
285 int rets = 0;
286
287 if (WARN_ON(!cl || !cl->dev))
288 return -ENODEV;
289
290 dev = cl->dev;
291
292 mutex_lock(&dev->device_lock);
293 if (cl != &dev->iamthif_cl) {
294 if (cl->state == MEI_FILE_CONNECTED) {
295 cl->state = MEI_FILE_DISCONNECTING;
296 dev_dbg(&dev->pdev->dev,
297 "disconnecting client host client = %d, "
298 "ME client = %d\n",
299 cl->host_client_id,
300 cl->me_client_id);
301 rets = mei_disconnect_host_client(dev, cl);
302 }
0288c7c9 303 mei_cl_flush_queues(cl);
ab841160
OW
304 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
305 cl->host_client_id,
306 cl->me_client_id);
307
308 if (dev->open_handle_count > 0) {
441ab50f 309 clear_bit(cl->host_client_id, dev->host_clients_map);
ab841160
OW
310 dev->open_handle_count--;
311 }
312 mei_remove_client_from_file_list(dev, cl->host_client_id);
313
314 /* free read cb */
315 cb = NULL;
316 if (cl->read_cb) {
317 cb = find_read_list_entry(dev, cl);
318 /* Remove entry from read list */
319 if (cb)
320 list_del(&cb->cb_list);
321
322 cb = cl->read_cb;
323 cl->read_cb = NULL;
324 }
325
326 file->private_data = NULL;
327
328 if (cb) {
329 mei_free_cb_private(cb);
330 cb = NULL;
331 }
332
333 kfree(cl);
334 } else {
335 if (dev->open_handle_count > 0)
336 dev->open_handle_count--;
337
338 if (dev->iamthif_file_object == file &&
339 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
340
341 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
342 dev->iamthif_state);
eb9af0ac 343 dev->iamthif_canceled = true;
ab841160
OW
344 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
345 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
c95efb74 346 mei_run_next_iamthif_cmd(dev);
ab841160
OW
347 }
348 }
349
350 if (mei_clear_lists(dev, file))
351 dev->iamthif_state = MEI_IAMTHIF_IDLE;
352
353 }
354 mutex_unlock(&dev->device_lock);
355 return rets;
356}
357
358
359/**
360 * mei_read - the read function.
361 *
362 * @file: pointer to file structure
363 * @ubuf: pointer to user buffer
364 * @length: buffer length
365 * @offset: data offset in buffer
366 *
367 * returns >=0 data length on success , <0 on error
368 */
369static ssize_t mei_read(struct file *file, char __user *ubuf,
441ab50f 370 size_t length, loff_t *offset)
ab841160
OW
371{
372 struct mei_cl *cl = file->private_data;
373 struct mei_cl_cb *cb_pos = NULL;
374 struct mei_cl_cb *cb = NULL;
375 struct mei_device *dev;
376 int i;
377 int rets;
378 int err;
379
380
381 if (WARN_ON(!cl || !cl->dev))
382 return -ENODEV;
383
384 dev = cl->dev;
385
386 mutex_lock(&dev->device_lock);
b210d750 387 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
388 rets = -ENODEV;
389 goto out;
390 }
391
392 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
393 /* Do not allow to read watchdog client */
07b509b7 394 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
ab841160
OW
395 if (i >= 0) {
396 struct mei_me_client *me_client = &dev->me_clients[i];
ab841160
OW
397 if (cl->me_client_id == me_client->client_id) {
398 rets = -EBADF;
399 goto out;
400 }
401 }
402 } else {
403 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
404 }
405
406 if (cl == &dev->iamthif_cl) {
407 rets = amthi_read(dev, file, ubuf, length, offset);
408 goto out;
409 }
410
411 if (cl->read_cb && cl->read_cb->information > *offset) {
412 cb = cl->read_cb;
413 goto copy_buffer;
414 } else if (cl->read_cb && cl->read_cb->information > 0 &&
415 cl->read_cb->information <= *offset) {
416 cb = cl->read_cb;
417 rets = 0;
418 goto free;
419 } else if ((!cl->read_cb || !cl->read_cb->information) &&
420 *offset > 0) {
5f9092f3 421 /*Offset needs to be cleaned for contiguous reads*/
ab841160
OW
422 *offset = 0;
423 rets = 0;
424 goto out;
425 }
426
427 err = mei_start_read(dev, cl);
428 if (err && err != -EBUSY) {
429 dev_dbg(&dev->pdev->dev,
430 "mei start read failure with status = %d\n", err);
431 rets = err;
432 goto out;
433 }
434
435 if (MEI_READ_COMPLETE != cl->reading_state &&
436 !waitqueue_active(&cl->rx_wait)) {
437 if (file->f_flags & O_NONBLOCK) {
438 rets = -EAGAIN;
439 goto out;
440 }
441
442 mutex_unlock(&dev->device_lock);
443
444 if (wait_event_interruptible(cl->rx_wait,
445 (MEI_READ_COMPLETE == cl->reading_state ||
446 MEI_FILE_INITIALIZING == cl->state ||
447 MEI_FILE_DISCONNECTED == cl->state ||
448 MEI_FILE_DISCONNECTING == cl->state))) {
449 if (signal_pending(current))
450 return -EINTR;
451 return -ERESTARTSYS;
452 }
453
454 mutex_lock(&dev->device_lock);
455 if (MEI_FILE_INITIALIZING == cl->state ||
456 MEI_FILE_DISCONNECTED == cl->state ||
457 MEI_FILE_DISCONNECTING == cl->state) {
458 rets = -EBUSY;
459 goto out;
460 }
461 }
462
463 cb = cl->read_cb;
464
465 if (!cb) {
466 rets = -ENODEV;
467 goto out;
468 }
469 if (cl->reading_state != MEI_READ_COMPLETE) {
470 rets = 0;
471 goto out;
472 }
473 /* now copy the data to user space */
474copy_buffer:
475 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
476 cb->response_buffer.size);
477 dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
478 cb->information);
479 if (length == 0 || ubuf == NULL || *offset > cb->information) {
480 rets = -EMSGSIZE;
481 goto free;
482 }
483
5f9092f3 484 /* length is being truncated to PAGE_SIZE, however, */
ab841160
OW
485 /* information size may be longer */
486 length = min_t(size_t, length, (cb->information - *offset));
487
441ab50f 488 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
ab841160
OW
489 rets = -EFAULT;
490 goto free;
491 }
492
493 rets = length;
494 *offset += length;
495 if ((unsigned long)*offset < cb->information)
496 goto out;
497
498free:
499 cb_pos = find_read_list_entry(dev, cl);
500 /* Remove entry from read list */
501 if (cb_pos)
502 list_del(&cb_pos->cb_list);
503 mei_free_cb_private(cb);
504 cl->reading_state = MEI_IDLE;
505 cl->read_cb = NULL;
506 cl->read_pending = 0;
507out:
508 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
509 mutex_unlock(&dev->device_lock);
510 return rets;
511}
512
513/**
514 * mei_write - the write function.
515 *
516 * @file: pointer to file structure
517 * @ubuf: pointer to user buffer
518 * @length: buffer length
519 * @offset: data offset in buffer
520 *
521 * returns >=0 data length on success , <0 on error
522 */
523static ssize_t mei_write(struct file *file, const char __user *ubuf,
441ab50f 524 size_t length, loff_t *offset)
ab841160
OW
525{
526 struct mei_cl *cl = file->private_data;
527 struct mei_cl_cb *write_cb = NULL;
528 struct mei_msg_hdr mei_hdr;
529 struct mei_device *dev;
530 unsigned long timeout = 0;
531 int rets;
532 int i;
533
534 if (WARN_ON(!cl || !cl->dev))
535 return -ENODEV;
536
537 dev = cl->dev;
538
539 mutex_lock(&dev->device_lock);
540
b210d750 541 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
542 mutex_unlock(&dev->device_lock);
543 return -ENODEV;
544 }
545
546 if (cl == &dev->iamthif_cl) {
547 write_cb = find_amthi_read_list_entry(dev, file);
548
549 if (write_cb) {
550 timeout = write_cb->read_time +
551 msecs_to_jiffies(IAMTHIF_READ_TIMER);
552
553 if (time_after(jiffies, timeout) ||
554 cl->reading_state == MEI_READ_COMPLETE) {
555 *offset = 0;
556 list_del(&write_cb->cb_list);
557 mei_free_cb_private(write_cb);
558 write_cb = NULL;
559 }
560 }
561 }
562
563 /* free entry used in read */
564 if (cl->reading_state == MEI_READ_COMPLETE) {
565 *offset = 0;
566 write_cb = find_read_list_entry(dev, cl);
567 if (write_cb) {
568 list_del(&write_cb->cb_list);
569 mei_free_cb_private(write_cb);
570 write_cb = NULL;
571 cl->reading_state = MEI_IDLE;
572 cl->read_cb = NULL;
573 cl->read_pending = 0;
574 }
441ab50f 575 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
ab841160
OW
576 *offset = 0;
577
578
579 write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
580 if (!write_cb) {
581 mutex_unlock(&dev->device_lock);
582 return -ENOMEM;
583 }
584
585 write_cb->file_object = file;
586 write_cb->file_private = cl;
587 write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
588 rets = -ENOMEM;
589 if (!write_cb->request_buffer.data)
590 goto unlock_dev;
591
592 dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
593
594 rets = -EFAULT;
595 if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
596 goto unlock_dev;
597
598 cl->sm_state = 0;
599 if (length == 4 &&
600 ((memcmp(mei_wd_state_independence_msg[0],
601 write_cb->request_buffer.data, 4) == 0) ||
602 (memcmp(mei_wd_state_independence_msg[1],
603 write_cb->request_buffer.data, 4) == 0) ||
604 (memcmp(mei_wd_state_independence_msg[2],
605 write_cb->request_buffer.data, 4) == 0)))
606 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
607
608 INIT_LIST_HEAD(&write_cb->cb_list);
609 if (cl == &dev->iamthif_cl) {
610 write_cb->response_buffer.data =
611 kmalloc(dev->iamthif_mtu, GFP_KERNEL);
612 if (!write_cb->response_buffer.data) {
613 rets = -ENOMEM;
614 goto unlock_dev;
615 }
b210d750 616 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
617 rets = -ENODEV;
618 goto unlock_dev;
619 }
07b509b7
TW
620 i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
621 if (i < 0) {
ab841160
OW
622 rets = -ENODEV;
623 goto unlock_dev;
624 }
07b509b7 625 if (length > dev->me_clients[i].props.max_msg_length ||
ab841160
OW
626 length <= 0) {
627 rets = -EMSGSIZE;
628 goto unlock_dev;
629 }
630
631 write_cb->response_buffer.size = dev->iamthif_mtu;
632 write_cb->major_file_operations = MEI_IOCTL;
633 write_cb->information = 0;
634 write_cb->request_buffer.size = length;
635 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
636 rets = -ENODEV;
637 goto unlock_dev;
638 }
639
640 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
641 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
642 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
643 (int) dev->iamthif_state);
644 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
645 list_add_tail(&write_cb->cb_list,
646 &dev->amthi_cmd_list.mei_cb.cb_list);
647 rets = length;
648 } else {
649 dev_dbg(&dev->pdev->dev, "call amthi write\n");
650 rets = amthi_write(dev, write_cb);
651
652 if (rets) {
653 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
654 rets);
655 goto unlock_dev;
656 }
657 rets = length;
658 }
659 mutex_unlock(&dev->device_lock);
660 return rets;
661 }
662
663 write_cb->major_file_operations = MEI_WRITE;
664 /* make sure information is zero before we start */
665
666 write_cb->information = 0;
667 write_cb->request_buffer.size = length;
668
669 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
670 cl->host_client_id, cl->me_client_id);
671 if (cl->state != MEI_FILE_CONNECTED) {
672 rets = -ENODEV;
673 dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
674 cl->host_client_id,
675 cl->me_client_id);
676 goto unlock_dev;
677 }
07b509b7
TW
678 i = mei_me_cl_by_id(dev, cl->me_client_id);
679 if (i < 0) {
ab841160
OW
680 rets = -ENODEV;
681 goto unlock_dev;
682 }
683 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
684 rets = -EINVAL;
685 goto unlock_dev;
686 }
687 write_cb->file_private = cl;
688
689 rets = mei_flow_ctrl_creds(dev, cl);
690 if (rets < 0)
691 goto unlock_dev;
692
693 if (rets && dev->mei_host_buffer_is_empty) {
694 rets = 0;
eb9af0ac 695 dev->mei_host_buffer_is_empty = false;
24aadc80
TW
696 if (length > mei_hbuf_max_data(dev)) {
697 mei_hdr.length = mei_hbuf_max_data(dev);
ab841160
OW
698 mei_hdr.msg_complete = 0;
699 } else {
700 mei_hdr.length = length;
701 mei_hdr.msg_complete = 1;
702 }
703 mei_hdr.host_addr = cl->host_client_id;
704 mei_hdr.me_addr = cl->me_client_id;
705 mei_hdr.reserved = 0;
706 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
707 *((u32 *) &mei_hdr));
1ccb7b62 708 if (mei_write_message(dev, &mei_hdr,
ab841160
OW
709 (unsigned char *) (write_cb->request_buffer.data),
710 mei_hdr.length)) {
711 rets = -ENODEV;
712 goto unlock_dev;
713 }
714 cl->writing_state = MEI_WRITING;
715 write_cb->information = mei_hdr.length;
716 if (mei_hdr.msg_complete) {
717 if (mei_flow_ctrl_reduce(dev, cl)) {
718 rets = -ENODEV;
719 goto unlock_dev;
720 }
721 list_add_tail(&write_cb->cb_list,
722 &dev->write_waiting_list.mei_cb.cb_list);
723 } else {
724 list_add_tail(&write_cb->cb_list,
725 &dev->write_list.mei_cb.cb_list);
726 }
727
728 } else {
729
730 write_cb->information = 0;
731 cl->writing_state = MEI_WRITING;
732 list_add_tail(&write_cb->cb_list,
733 &dev->write_list.mei_cb.cb_list);
734 }
735 mutex_unlock(&dev->device_lock);
736 return length;
737
738unlock_dev:
739 mutex_unlock(&dev->device_lock);
740 mei_free_cb_private(write_cb);
741 return rets;
742}
743
744
745/**
746 * mei_ioctl - the IOCTL function
747 *
748 * @file: pointer to file structure
749 * @cmd: ioctl command
750 * @data: pointer to mei message structure
751 *
752 * returns 0 on success , <0 on error
753 */
754static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
755{
756 struct mei_device *dev;
757 struct mei_cl *cl = file->private_data;
758 struct mei_connect_client_data *connect_data = NULL;
759 int rets;
760
761 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
762 return -EINVAL;
763
764 if (WARN_ON(!cl || !cl->dev))
765 return -ENODEV;
766
767 dev = cl->dev;
768
769 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
770
771 mutex_lock(&dev->device_lock);
b210d750 772 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
773 rets = -ENODEV;
774 goto out;
775 }
776
777 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
778
779 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
780 GFP_KERNEL);
781 if (!connect_data) {
782 rets = -ENOMEM;
783 goto out;
784 }
785 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
786 if (copy_from_user(connect_data, (char __user *)data,
787 sizeof(struct mei_connect_client_data))) {
788 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
789 rets = -EFAULT;
790 goto out;
791 }
792 rets = mei_ioctl_connect_client(file, connect_data);
793
794 /* if all is ok, copying the data back to user. */
795 if (rets)
796 goto out;
797
798 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
799 if (copy_to_user((char __user *)data, connect_data,
800 sizeof(struct mei_connect_client_data))) {
801 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
802 rets = -EFAULT;
803 goto out;
804 }
805
806out:
807 kfree(connect_data);
808 mutex_unlock(&dev->device_lock);
809 return rets;
810}
811
812/**
813 * mei_compat_ioctl - the compat IOCTL function
814 *
815 * @file: pointer to file structure
816 * @cmd: ioctl command
817 * @data: pointer to mei message structure
818 *
819 * returns 0 on success , <0 on error
820 */
821#ifdef CONFIG_COMPAT
822static long mei_compat_ioctl(struct file *file,
441ab50f 823 unsigned int cmd, unsigned long data)
ab841160
OW
824{
825 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
826}
827#endif
828
829
830/**
831 * mei_poll - the poll function
832 *
833 * @file: pointer to file structure
834 * @wait: pointer to poll_table structure
835 *
836 * returns poll mask
837 */
838static unsigned int mei_poll(struct file *file, poll_table *wait)
839{
840 struct mei_cl *cl = file->private_data;
841 struct mei_device *dev;
842 unsigned int mask = 0;
843
844 if (WARN_ON(!cl || !cl->dev))
845 return mask;
846
847 dev = cl->dev;
848
849 mutex_lock(&dev->device_lock);
850
b210d750 851 if (dev->dev_state != MEI_DEV_ENABLED)
ab841160
OW
852 goto out;
853
854
855 if (cl == &dev->iamthif_cl) {
856 mutex_unlock(&dev->device_lock);
857 poll_wait(file, &dev->iamthif_cl.wait, wait);
858 mutex_lock(&dev->device_lock);
859 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
860 dev->iamthif_file_object == file) {
861 mask |= (POLLIN | POLLRDNORM);
862 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
c95efb74 863 mei_run_next_iamthif_cmd(dev);
ab841160
OW
864 }
865 goto out;
866 }
867
868 mutex_unlock(&dev->device_lock);
869 poll_wait(file, &cl->tx_wait, wait);
870 mutex_lock(&dev->device_lock);
871 if (MEI_WRITE_COMPLETE == cl->writing_state)
872 mask |= (POLLIN | POLLRDNORM);
873
874out:
875 mutex_unlock(&dev->device_lock);
876 return mask;
877}
878
5b881e3c
OW
879/*
880 * file operations structure will be used for mei char device.
881 */
882static const struct file_operations mei_fops = {
883 .owner = THIS_MODULE,
884 .read = mei_read,
885 .unlocked_ioctl = mei_ioctl,
886#ifdef CONFIG_COMPAT
887 .compat_ioctl = mei_compat_ioctl,
888#endif
889 .open = mei_open,
890 .release = mei_release,
891 .write = mei_write,
892 .poll = mei_poll,
893 .llseek = no_llseek
894};
895
896
897/*
898 * Misc Device Struct
899 */
900static struct miscdevice mei_misc_device = {
c38ea24e 901 .name = "mei",
5b881e3c
OW
902 .fops = &mei_fops,
903 .minor = MISC_DYNAMIC_MINOR,
904};
905
9a123f19
TW
906/**
907 * mei_quirk_probe - probe for devices that doesn't valid ME interface
908 * @pdev: PCI device structure
909 * @ent: entry into pci_device_table
910 *
911 * returns true if ME Interface is valid, false otherwise
912 */
913static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
914 const struct pci_device_id *ent)
915{
916 u32 reg;
917 if (ent->device == MEI_DEV_ID_PBG_1) {
918 pci_read_config_dword(pdev, 0x48, &reg);
919 /* make sure that bit 9 is up and bit 10 is down */
920 if ((reg & 0x600) == 0x200) {
921 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
922 return false;
923 }
924 }
925 return true;
926}
5b881e3c
OW
927/**
928 * mei_probe - Device Initialization Routine
929 *
930 * @pdev: PCI device structure
931 * @ent: entry in kcs_pci_tbl
932 *
933 * returns 0 on success, <0 on failure.
934 */
935static int __devinit mei_probe(struct pci_dev *pdev,
936 const struct pci_device_id *ent)
937{
938 struct mei_device *dev;
939 int err;
940
941 mutex_lock(&mei_mutex);
9a123f19
TW
942
943 if (!mei_quirk_probe(pdev, ent)) {
944 err = -ENODEV;
945 goto end;
946 }
947
5b881e3c
OW
948 if (mei_device) {
949 err = -EEXIST;
950 goto end;
951 }
952 /* enable pci dev */
953 err = pci_enable_device(pdev);
954 if (err) {
32c826b6 955 dev_err(&pdev->dev, "failed to enable pci device.\n");
5b881e3c
OW
956 goto end;
957 }
958 /* set PCI host mastering */
959 pci_set_master(pdev);
960 /* pci request regions for mei driver */
068c0ae9 961 err = pci_request_regions(pdev, KBUILD_MODNAME);
5b881e3c 962 if (err) {
32c826b6 963 dev_err(&pdev->dev, "failed to get pci regions.\n");
5b881e3c
OW
964 goto disable_device;
965 }
966 /* allocates and initializes the mei dev structure */
967 dev = mei_device_init(pdev);
968 if (!dev) {
969 err = -ENOMEM;
970 goto release_regions;
971 }
972 /* mapping IO device memory */
973 dev->mem_addr = pci_iomap(pdev, 0, 0);
974 if (!dev->mem_addr) {
32c826b6 975 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
5b881e3c
OW
976 err = -ENOMEM;
977 goto free_device;
978 }
979 pci_enable_msi(pdev);
980
981 /* request and enable interrupt */
982 if (pci_dev_msi_enabled(pdev))
983 err = request_threaded_irq(pdev->irq,
984 NULL,
985 mei_interrupt_thread_handler,
068c0ae9 986 IRQF_ONESHOT, KBUILD_MODNAME, dev);
5b881e3c
OW
987 else
988 err = request_threaded_irq(pdev->irq,
989 mei_interrupt_quick_handler,
990 mei_interrupt_thread_handler,
068c0ae9 991 IRQF_SHARED, KBUILD_MODNAME, dev);
5b881e3c
OW
992
993 if (err) {
32c826b6 994 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
5b881e3c 995 pdev->irq);
169dc388 996 goto disable_msi;
5b881e3c
OW
997 }
998 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
999 if (mei_hw_init(dev)) {
32c826b6 1000 dev_err(&pdev->dev, "init hw failure.\n");
5b881e3c
OW
1001 err = -ENODEV;
1002 goto release_irq;
1003 }
1004
1005 err = misc_register(&mei_misc_device);
1006 if (err)
1007 goto release_irq;
1008
1009 mei_device = pdev;
1010 pci_set_drvdata(pdev, dev);
1011
1012
1013 schedule_delayed_work(&dev->timer_work, HZ);
1014
1015 mutex_unlock(&mei_mutex);
1016
2f3d2b49 1017 pr_debug("initialization successful.\n");
5b881e3c
OW
1018
1019 return 0;
1020
1021release_irq:
1022 /* disable interrupts */
1023 dev->host_hw_state = mei_hcsr_read(dev);
1024 mei_disable_interrupts(dev);
1025 flush_scheduled_work();
1026 free_irq(pdev->irq, dev);
169dc388 1027disable_msi:
5b881e3c 1028 pci_disable_msi(pdev);
5b881e3c
OW
1029 pci_iounmap(pdev, dev->mem_addr);
1030free_device:
1031 kfree(dev);
1032release_regions:
1033 pci_release_regions(pdev);
1034disable_device:
1035 pci_disable_device(pdev);
1036end:
1037 mutex_unlock(&mei_mutex);
32c826b6 1038 dev_err(&pdev->dev, "initialization failed.\n");
5b881e3c
OW
1039 return err;
1040}
1041
1042/**
1043 * mei_remove - Device Removal Routine
1044 *
1045 * @pdev: PCI device structure
1046 *
1047 * mei_remove is called by the PCI subsystem to alert the driver
1048 * that it should release a PCI device.
1049 */
1050static void __devexit mei_remove(struct pci_dev *pdev)
1051{
1052 struct mei_device *dev;
1053
1054 if (mei_device != pdev)
1055 return;
1056
1057 dev = pci_get_drvdata(pdev);
1058 if (!dev)
1059 return;
1060
1061 mutex_lock(&dev->device_lock);
1062
c216fdeb
TW
1063 cancel_delayed_work(&dev->timer_work);
1064
1065 mei_wd_stop(dev);
5b881e3c
OW
1066
1067 mei_device = NULL;
1068
1069 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1070 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1071 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1072 }
1073 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1074 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1075 mei_disconnect_host_client(dev, &dev->wd_cl);
1076 }
1077
1078 /* Unregistering watchdog device */
70cd5337 1079 mei_watchdog_unregister(dev);
5b881e3c
OW
1080
1081 /* remove entry if already in list */
1082 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1083 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1084 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1085
1086 dev->iamthif_current_cb = NULL;
1087 dev->me_clients_num = 0;
1088
1089 mutex_unlock(&dev->device_lock);
1090
1091 flush_scheduled_work();
1092
1093 /* disable interrupts */
1094 mei_disable_interrupts(dev);
1095
1096 free_irq(pdev->irq, dev);
1097 pci_disable_msi(pdev);
1098 pci_set_drvdata(pdev, NULL);
1099
1100 if (dev->mem_addr)
1101 pci_iounmap(pdev, dev->mem_addr);
1102
1103 kfree(dev);
1104
1105 pci_release_regions(pdev);
1106 pci_disable_device(pdev);
a44cab4a
TW
1107
1108 misc_deregister(&mei_misc_device);
5b881e3c 1109}
ab841160
OW
1110#ifdef CONFIG_PM
1111static int mei_pci_suspend(struct device *device)
1112{
1113 struct pci_dev *pdev = to_pci_dev(device);
1114 struct mei_device *dev = pci_get_drvdata(pdev);
1115 int err;
1116
1117 if (!dev)
1118 return -ENODEV;
1119 mutex_lock(&dev->device_lock);
c216fdeb
TW
1120
1121 cancel_delayed_work(&dev->timer_work);
1122
ab841160 1123 /* Stop watchdog if exists */
c216fdeb 1124 err = mei_wd_stop(dev);
ab841160 1125 /* Set new mei state */
b210d750
TW
1126 if (dev->dev_state == MEI_DEV_ENABLED ||
1127 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1128 dev->dev_state = MEI_DEV_POWER_DOWN;
ab841160
OW
1129 mei_reset(dev, 0);
1130 }
1131 mutex_unlock(&dev->device_lock);
1132
1133 free_irq(pdev->irq, dev);
4f61a7ad 1134 pci_disable_msi(pdev);
ab841160
OW
1135
1136 return err;
1137}
1138
1139static int mei_pci_resume(struct device *device)
1140{
1141 struct pci_dev *pdev = to_pci_dev(device);
1142 struct mei_device *dev;
1143 int err;
1144
1145 dev = pci_get_drvdata(pdev);
1146 if (!dev)
1147 return -ENODEV;
1148
4f61a7ad
TW
1149 pci_enable_msi(pdev);
1150
1151 /* request and enable interrupt */
1152 if (pci_dev_msi_enabled(pdev))
1153 err = request_threaded_irq(pdev->irq,
1154 NULL,
1155 mei_interrupt_thread_handler,
068c0ae9 1156 IRQF_ONESHOT, KBUILD_MODNAME, dev);
4f61a7ad
TW
1157 else
1158 err = request_threaded_irq(pdev->irq,
ab841160
OW
1159 mei_interrupt_quick_handler,
1160 mei_interrupt_thread_handler,
068c0ae9 1161 IRQF_SHARED, KBUILD_MODNAME, dev);
4f61a7ad 1162
ab841160 1163 if (err) {
32c826b6
TW
1164 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1165 pdev->irq);
ab841160
OW
1166 return err;
1167 }
1168
1169 mutex_lock(&dev->device_lock);
b210d750 1170 dev->dev_state = MEI_DEV_POWER_UP;
ab841160
OW
1171 mei_reset(dev, 1);
1172 mutex_unlock(&dev->device_lock);
1173
6d70e935
OW
1174 /* Start timer if stopped in suspend */
1175 schedule_delayed_work(&dev->timer_work, HZ);
1176
ab841160
OW
1177 return err;
1178}
1179static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1180#define MEI_PM_OPS (&mei_pm_ops)
1181#else
2d990362 1182#define MEI_PM_OPS NULL
ab841160
OW
1183#endif /* CONFIG_PM */
1184/*
1185 * PCI driver structure
1186 */
1187static struct pci_driver mei_driver = {
068c0ae9 1188 .name = KBUILD_MODNAME,
ab841160
OW
1189 .id_table = mei_pci_tbl,
1190 .probe = mei_probe,
1191 .remove = __devexit_p(mei_remove),
1192 .shutdown = __devexit_p(mei_remove),
1193 .driver.pm = MEI_PM_OPS,
1194};
1195
6078188e 1196module_pci_driver(mei_driver);
ab841160
OW
1197
1198MODULE_AUTHOR("Intel Corporation");
1199MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1200MODULE_LICENSE("GPL v2");
This page took 0.179862 seconds and 5 git commands to generate.