Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / staging / mei / main.c
blob8d7678569e7196e6d374f98bfb06d4e0e7e36c30
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2011, Intel Corporation.
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.
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.
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/fs.h>
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/fcntl.h>
25 #include <linux/aio.h>
26 #include <linux/pci.h>
27 #include <linux/poll.h>
28 #include <linux/init.h>
29 #include <linux/ioctl.h>
30 #include <linux/cdev.h>
31 #include <linux/sched.h>
32 #include <linux/uuid.h>
33 #include <linux/compat.h>
34 #include <linux/jiffies.h>
35 #include <linux/interrupt.h>
37 #include "mei_dev.h"
38 #include "mei.h"
39 #include "interface.h"
40 #include "mei_version.h"
43 #define MEI_READ_TIMEOUT 45
44 #define MEI_DRIVER_NAME "mei"
45 #define MEI_DEV_NAME "mei"
48 * mei driver strings
50 static char mei_driver_name[] = MEI_DRIVER_NAME;
51 static const char mei_driver_string[] = "Intel(R) Management Engine Interface";
52 static const char mei_driver_version[] = MEI_DRIVER_VERSION;
54 /* mei char device for registration */
55 static struct cdev mei_cdev;
57 /* major number for device */
58 static int mei_major;
59 /* The device pointer */
60 /* Currently this driver works as long as there is only a single AMT device. */
61 static struct pci_dev *mei_device;
63 static struct class *mei_class;
66 /* mei_pci_tbl - PCI Device ID Table */
67 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
82 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
83 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
84 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
85 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
86 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
87 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
88 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
89 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
90 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
91 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
92 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
93 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
94 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
95 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
96 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
97 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
98 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
100 /* required last entry */
101 {0, }
104 MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
106 static DEFINE_MUTEX(mei_mutex);
109 * mei_probe - Device Initialization Routine
111 * @pdev: PCI device structure
112 * @ent: entry in kcs_pci_tbl
114 * returns 0 on success, <0 on failure.
116 static int __devinit mei_probe(struct pci_dev *pdev,
117 const struct pci_device_id *ent)
119 struct mei_device *dev;
120 int err;
122 mutex_lock(&mei_mutex);
123 if (mei_device) {
124 err = -EEXIST;
125 goto end;
127 /* enable pci dev */
128 err = pci_enable_device(pdev);
129 if (err) {
130 printk(KERN_ERR "mei: Failed to enable pci device.\n");
131 goto end;
133 /* set PCI host mastering */
134 pci_set_master(pdev);
135 /* pci request regions for mei driver */
136 err = pci_request_regions(pdev, mei_driver_name);
137 if (err) {
138 printk(KERN_ERR "mei: Failed to get pci regions.\n");
139 goto disable_device;
141 /* allocates and initializes the mei dev structure */
142 dev = mei_device_init(pdev);
143 if (!dev) {
144 err = -ENOMEM;
145 goto release_regions;
147 /* mapping IO device memory */
148 dev->mem_addr = pci_iomap(pdev, 0, 0);
149 if (!dev->mem_addr) {
150 printk(KERN_ERR "mei: mapping I/O device memory failure.\n");
151 err = -ENOMEM;
152 goto free_device;
154 pci_enable_msi(pdev);
156 /* request and enable interrupt */
157 if (pci_dev_msi_enabled(pdev))
158 err = request_threaded_irq(pdev->irq,
159 NULL,
160 mei_interrupt_thread_handler,
161 0, mei_driver_name, dev);
162 else
163 err = request_threaded_irq(pdev->irq,
164 mei_interrupt_quick_handler,
165 mei_interrupt_thread_handler,
166 IRQF_SHARED, mei_driver_name, dev);
168 if (err) {
169 printk(KERN_ERR "mei: request_threaded_irq failure. irq = %d\n",
170 pdev->irq);
171 goto unmap_memory;
173 INIT_DELAYED_WORK(&dev->wd_work, mei_wd_timer);
174 if (mei_hw_init(dev)) {
175 printk(KERN_ERR "mei: Init hw failure.\n");
176 err = -ENODEV;
177 goto release_irq;
179 mei_device = pdev;
180 pci_set_drvdata(pdev, dev);
181 schedule_delayed_work(&dev->wd_work, HZ);
183 mutex_unlock(&mei_mutex);
185 pr_debug("mei: Driver initialization successful.\n");
187 return 0;
189 release_irq:
190 /* disable interrupts */
191 dev->host_hw_state = mei_hcsr_read(dev);
192 mei_disable_interrupts(dev);
193 flush_scheduled_work();
194 free_irq(pdev->irq, dev);
195 pci_disable_msi(pdev);
196 unmap_memory:
197 pci_iounmap(pdev, dev->mem_addr);
198 free_device:
199 kfree(dev);
200 release_regions:
201 pci_release_regions(pdev);
202 disable_device:
203 pci_disable_device(pdev);
204 end:
205 mutex_unlock(&mei_mutex);
206 printk(KERN_ERR "mei: Driver initialization failed.\n");
207 return err;
211 * mei_remove - Device Removal Routine
213 * @pdev: PCI device structure
215 * mei_remove is called by the PCI subsystem to alert the driver
216 * that it should release a PCI device.
218 static void __devexit mei_remove(struct pci_dev *pdev)
220 struct mei_device *dev;
222 if (mei_device != pdev)
223 return;
225 dev = pci_get_drvdata(pdev);
226 if (!dev)
227 return;
229 mutex_lock(&dev->device_lock);
231 mei_wd_stop(dev, false);
233 mei_device = NULL;
235 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
236 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
237 mei_disconnect_host_client(dev, &dev->iamthif_cl);
239 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
240 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
241 mei_disconnect_host_client(dev, &dev->wd_cl);
244 /* remove entry if already in list */
245 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
246 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
247 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
249 dev->iamthif_current_cb = NULL;
250 dev->me_clients_num = 0;
252 mutex_unlock(&dev->device_lock);
254 flush_scheduled_work();
256 /* disable interrupts */
257 mei_disable_interrupts(dev);
259 free_irq(pdev->irq, dev);
260 pci_disable_msi(pdev);
261 pci_set_drvdata(pdev, NULL);
263 if (dev->mem_addr)
264 pci_iounmap(pdev, dev->mem_addr);
266 kfree(dev);
268 pci_release_regions(pdev);
269 pci_disable_device(pdev);
273 * mei_clear_list - removes all callbacks associated with file
274 * from mei_cb_list
276 * @dev: device structure.
277 * @file: file structure
278 * @mei_cb_list: callbacks list
280 * mei_clear_list is called to clear resources associated with file
281 * when application calls close function or Ctrl-C was pressed
283 * returns true if callback removed from the list, false otherwise
285 static bool mei_clear_list(struct mei_device *dev,
286 struct file *file, struct list_head *mei_cb_list)
288 struct mei_cl_cb *cb_pos = NULL;
289 struct mei_cl_cb *cb_next = NULL;
290 struct file *file_temp;
291 bool removed = false;
293 /* list all list member */
294 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
295 file_temp = (struct file *)cb_pos->file_object;
296 /* check if list member associated with a file */
297 if (file_temp == file) {
298 /* remove member from the list */
299 list_del(&cb_pos->cb_list);
300 /* check if cb equal to current iamthif cb */
301 if (dev->iamthif_current_cb == cb_pos) {
302 dev->iamthif_current_cb = NULL;
303 /* send flow control to iamthif client */
304 mei_send_flow_control(dev, &dev->iamthif_cl);
306 /* free all allocated buffers */
307 mei_free_cb_private(cb_pos);
308 cb_pos = NULL;
309 removed = true;
312 return removed;
316 * mei_clear_lists - removes all callbacks associated with file
318 * @dev: device structure
319 * @file: file structure
321 * mei_clear_lists is called to clear resources associated with file
322 * when application calls close function or Ctrl-C was pressed
324 * returns true if callback removed from the list, false otherwise
326 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
328 bool removed = false;
330 /* remove callbacks associated with a file */
331 mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
332 if (mei_clear_list(dev, file,
333 &dev->amthi_read_complete_list.mei_cb.cb_list))
334 removed = true;
336 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
338 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
339 removed = true;
341 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
342 removed = true;
344 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
345 removed = true;
347 /* check if iamthif_current_cb not NULL */
348 if (dev->iamthif_current_cb && !removed) {
349 /* check file and iamthif current cb association */
350 if (dev->iamthif_current_cb->file_object == file) {
351 /* remove cb */
352 mei_free_cb_private(dev->iamthif_current_cb);
353 dev->iamthif_current_cb = NULL;
354 removed = true;
357 return removed;
360 * find_read_list_entry - find read list entry
362 * @dev: device structure
363 * @file: pointer to file structure
365 * returns cb on success, NULL on error
367 static struct mei_cl_cb *find_read_list_entry(
368 struct mei_device *dev,
369 struct mei_cl *cl)
371 struct mei_cl_cb *cb_pos = NULL;
372 struct mei_cl_cb *cb_next = NULL;
374 if (!dev->read_list.status &&
375 !list_empty(&dev->read_list.mei_cb.cb_list)) {
377 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
378 list_for_each_entry_safe(cb_pos, cb_next,
379 &dev->read_list.mei_cb.cb_list, cb_list) {
380 struct mei_cl *cl_temp;
381 cl_temp = (struct mei_cl *)cb_pos->file_private;
383 if (mei_cl_cmp_id(cl, cl_temp))
384 return cb_pos;
387 return NULL;
391 * mei_open - the open function
393 * @inode: pointer to inode structure
394 * @file: pointer to file structure
396 * returns 0 on success, <0 on error
398 static int mei_open(struct inode *inode, struct file *file)
400 struct mei_cl *cl;
401 int if_num = iminor(inode), err;
402 struct mei_device *dev;
404 err = -ENODEV;
405 if (!mei_device)
406 goto out;
408 dev = pci_get_drvdata(mei_device);
409 if (if_num != MEI_MINOR_NUMBER || !dev)
410 goto out;
412 mutex_lock(&dev->device_lock);
413 err = -ENOMEM;
414 cl = mei_cl_allocate(dev);
415 if (!cl)
416 goto out;
418 err = -ENODEV;
419 if (dev->mei_state != MEI_ENABLED) {
420 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
421 dev->mei_state);
422 goto out_unlock;
424 err = -EMFILE;
425 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
426 goto out_unlock;
428 cl->host_client_id = find_first_zero_bit(dev->host_clients_map,
429 MEI_CLIENTS_MAX);
430 if (cl->host_client_id > MEI_CLIENTS_MAX)
431 goto out_unlock;
433 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
435 dev->open_handle_count++;
436 list_add_tail(&cl->link, &dev->file_list);
438 set_bit(cl->host_client_id, dev->host_clients_map);
439 cl->state = MEI_FILE_INITIALIZING;
440 cl->sm_state = 0;
442 file->private_data = cl;
443 mutex_unlock(&dev->device_lock);
445 return 0;
447 out_unlock:
448 mutex_unlock(&dev->device_lock);
449 kfree(cl);
450 out:
451 return err;
455 * mei_release - the release function
457 * @inode: pointer to inode structure
458 * @file: pointer to file structure
460 * returns 0 on success, <0 on error
462 static int mei_release(struct inode *inode, struct file *file)
464 struct mei_cl *cl = file->private_data;
465 struct mei_cl_cb *cb;
466 struct mei_device *dev;
467 int rets = 0;
469 if (WARN_ON(!cl || !cl->dev))
470 return -ENODEV;
472 dev = cl->dev;
474 mutex_lock(&dev->device_lock);
475 if (cl != &dev->iamthif_cl) {
476 if (cl->state == MEI_FILE_CONNECTED) {
477 cl->state = MEI_FILE_DISCONNECTING;
478 dev_dbg(&dev->pdev->dev,
479 "disconnecting client host client = %d, "
480 "ME client = %d\n",
481 cl->host_client_id,
482 cl->me_client_id);
483 rets = mei_disconnect_host_client(dev, cl);
485 mei_cl_flush_queues(cl);
486 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
487 cl->host_client_id,
488 cl->me_client_id);
490 if (dev->open_handle_count > 0) {
491 clear_bit(cl->host_client_id,
492 dev->host_clients_map);
493 dev->open_handle_count--;
495 mei_remove_client_from_file_list(dev, cl->host_client_id);
497 /* free read cb */
498 cb = NULL;
499 if (cl->read_cb) {
500 cb = find_read_list_entry(dev, cl);
501 /* Remove entry from read list */
502 if (cb)
503 list_del(&cb->cb_list);
505 cb = cl->read_cb;
506 cl->read_cb = NULL;
509 file->private_data = NULL;
511 if (cb) {
512 mei_free_cb_private(cb);
513 cb = NULL;
516 kfree(cl);
517 } else {
518 if (dev->open_handle_count > 0)
519 dev->open_handle_count--;
521 if (dev->iamthif_file_object == file &&
522 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
524 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
525 dev->iamthif_state);
526 dev->iamthif_canceled = true;
527 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
528 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
529 mei_run_next_iamthif_cmd(dev);
533 if (mei_clear_lists(dev, file))
534 dev->iamthif_state = MEI_IAMTHIF_IDLE;
537 mutex_unlock(&dev->device_lock);
538 return rets;
543 * mei_read - the read function.
545 * @file: pointer to file structure
546 * @ubuf: pointer to user buffer
547 * @length: buffer length
548 * @offset: data offset in buffer
550 * returns >=0 data length on success , <0 on error
552 static ssize_t mei_read(struct file *file, char __user *ubuf,
553 size_t length, loff_t *offset)
555 struct mei_cl *cl = file->private_data;
556 struct mei_cl_cb *cb_pos = NULL;
557 struct mei_cl_cb *cb = NULL;
558 struct mei_device *dev;
559 int i;
560 int rets;
561 int err;
564 if (WARN_ON(!cl || !cl->dev))
565 return -ENODEV;
567 dev = cl->dev;
569 mutex_lock(&dev->device_lock);
570 if (dev->mei_state != MEI_ENABLED) {
571 rets = -ENODEV;
572 goto out;
575 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
576 /* Do not allow to read watchdog client */
577 i = mei_find_me_client_index(dev, mei_wd_guid);
578 if (i >= 0) {
579 struct mei_me_client *me_client = &dev->me_clients[i];
581 if (cl->me_client_id == me_client->client_id) {
582 rets = -EBADF;
583 goto out;
586 } else {
587 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
590 if (cl == &dev->iamthif_cl) {
591 rets = amthi_read(dev, file, ubuf, length, offset);
592 goto out;
595 if (cl->read_cb && cl->read_cb->information > *offset) {
596 cb = cl->read_cb;
597 goto copy_buffer;
598 } else if (cl->read_cb && cl->read_cb->information > 0 &&
599 cl->read_cb->information <= *offset) {
600 cb = cl->read_cb;
601 rets = 0;
602 goto free;
603 } else if ((!cl->read_cb || !cl->read_cb->information) &&
604 *offset > 0) {
605 /*Offset needs to be cleaned for contingous reads*/
606 *offset = 0;
607 rets = 0;
608 goto out;
611 err = mei_start_read(dev, cl);
612 if (err && err != -EBUSY) {
613 dev_dbg(&dev->pdev->dev,
614 "mei start read failure with status = %d\n", err);
615 rets = err;
616 goto out;
619 if (MEI_READ_COMPLETE != cl->reading_state &&
620 !waitqueue_active(&cl->rx_wait)) {
621 if (file->f_flags & O_NONBLOCK) {
622 rets = -EAGAIN;
623 goto out;
626 mutex_unlock(&dev->device_lock);
628 if (wait_event_interruptible(cl->rx_wait,
629 (MEI_READ_COMPLETE == cl->reading_state ||
630 MEI_FILE_INITIALIZING == cl->state ||
631 MEI_FILE_DISCONNECTED == cl->state ||
632 MEI_FILE_DISCONNECTING == cl->state))) {
633 if (signal_pending(current))
634 return -EINTR;
635 return -ERESTARTSYS;
638 mutex_lock(&dev->device_lock);
639 if (MEI_FILE_INITIALIZING == cl->state ||
640 MEI_FILE_DISCONNECTED == cl->state ||
641 MEI_FILE_DISCONNECTING == cl->state) {
642 rets = -EBUSY;
643 goto out;
647 cb = cl->read_cb;
649 if (!cb) {
650 rets = -ENODEV;
651 goto out;
653 if (cl->reading_state != MEI_READ_COMPLETE) {
654 rets = 0;
655 goto out;
657 /* now copy the data to user space */
658 copy_buffer:
659 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
660 cb->response_buffer.size);
661 dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
662 cb->information);
663 if (length == 0 || ubuf == NULL || *offset > cb->information) {
664 rets = -EMSGSIZE;
665 goto free;
668 /* length is being turncated to PAGE_SIZE, however, */
669 /* information size may be longer */
670 length = min_t(size_t, length, (cb->information - *offset));
672 if (copy_to_user(ubuf,
673 cb->response_buffer.data + *offset,
674 length)) {
675 rets = -EFAULT;
676 goto free;
679 rets = length;
680 *offset += length;
681 if ((unsigned long)*offset < cb->information)
682 goto out;
684 free:
685 cb_pos = find_read_list_entry(dev, cl);
686 /* Remove entry from read list */
687 if (cb_pos)
688 list_del(&cb_pos->cb_list);
689 mei_free_cb_private(cb);
690 cl->reading_state = MEI_IDLE;
691 cl->read_cb = NULL;
692 cl->read_pending = 0;
693 out:
694 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
695 mutex_unlock(&dev->device_lock);
696 return rets;
700 * mei_write - the write function.
702 * @file: pointer to file structure
703 * @ubuf: pointer to user buffer
704 * @length: buffer length
705 * @offset: data offset in buffer
707 * returns >=0 data length on success , <0 on error
709 static ssize_t mei_write(struct file *file, const char __user *ubuf,
710 size_t length, loff_t *offset)
712 struct mei_cl *cl = file->private_data;
713 struct mei_cl_cb *write_cb = NULL;
714 struct mei_msg_hdr mei_hdr;
715 struct mei_device *dev;
716 unsigned long timeout = 0;
717 int rets;
718 int i;
720 if (WARN_ON(!cl || !cl->dev))
721 return -ENODEV;
723 dev = cl->dev;
725 mutex_lock(&dev->device_lock);
727 if (dev->mei_state != MEI_ENABLED) {
728 mutex_unlock(&dev->device_lock);
729 return -ENODEV;
732 if (cl == &dev->iamthif_cl) {
733 write_cb = find_amthi_read_list_entry(dev, file);
735 if (write_cb) {
736 timeout = write_cb->read_time +
737 msecs_to_jiffies(IAMTHIF_READ_TIMER);
739 if (time_after(jiffies, timeout) ||
740 cl->reading_state == MEI_READ_COMPLETE) {
741 *offset = 0;
742 list_del(&write_cb->cb_list);
743 mei_free_cb_private(write_cb);
744 write_cb = NULL;
749 /* free entry used in read */
750 if (cl->reading_state == MEI_READ_COMPLETE) {
751 *offset = 0;
752 write_cb = find_read_list_entry(dev, cl);
753 if (write_cb) {
754 list_del(&write_cb->cb_list);
755 mei_free_cb_private(write_cb);
756 write_cb = NULL;
757 cl->reading_state = MEI_IDLE;
758 cl->read_cb = NULL;
759 cl->read_pending = 0;
761 } else if (cl->reading_state == MEI_IDLE &&
762 !cl->read_pending)
763 *offset = 0;
766 write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
767 if (!write_cb) {
768 mutex_unlock(&dev->device_lock);
769 return -ENOMEM;
772 write_cb->file_object = file;
773 write_cb->file_private = cl;
774 write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
775 rets = -ENOMEM;
776 if (!write_cb->request_buffer.data)
777 goto unlock_dev;
779 dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
781 rets = -EFAULT;
782 if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
783 goto unlock_dev;
785 cl->sm_state = 0;
786 if (length == 4 &&
787 ((memcmp(mei_wd_state_independence_msg[0],
788 write_cb->request_buffer.data, 4) == 0) ||
789 (memcmp(mei_wd_state_independence_msg[1],
790 write_cb->request_buffer.data, 4) == 0) ||
791 (memcmp(mei_wd_state_independence_msg[2],
792 write_cb->request_buffer.data, 4) == 0)))
793 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
795 INIT_LIST_HEAD(&write_cb->cb_list);
796 if (cl == &dev->iamthif_cl) {
797 write_cb->response_buffer.data =
798 kmalloc(dev->iamthif_mtu, GFP_KERNEL);
799 if (!write_cb->response_buffer.data) {
800 rets = -ENOMEM;
801 goto unlock_dev;
803 if (dev->mei_state != MEI_ENABLED) {
804 rets = -ENODEV;
805 goto unlock_dev;
807 for (i = 0; i < dev->me_clients_num; i++) {
808 if (dev->me_clients[i].client_id ==
809 dev->iamthif_cl.me_client_id)
810 break;
813 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
814 rets = -ENODEV;
815 goto unlock_dev;
817 if (i == dev->me_clients_num ||
818 (dev->me_clients[i].client_id !=
819 dev->iamthif_cl.me_client_id)) {
820 rets = -ENODEV;
821 goto unlock_dev;
822 } else if (length > dev->me_clients[i].props.max_msg_length ||
823 length <= 0) {
824 rets = -EMSGSIZE;
825 goto unlock_dev;
828 write_cb->response_buffer.size = dev->iamthif_mtu;
829 write_cb->major_file_operations = MEI_IOCTL;
830 write_cb->information = 0;
831 write_cb->request_buffer.size = length;
832 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
833 rets = -ENODEV;
834 goto unlock_dev;
837 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
838 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
839 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
840 (int) dev->iamthif_state);
841 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
842 list_add_tail(&write_cb->cb_list,
843 &dev->amthi_cmd_list.mei_cb.cb_list);
844 rets = length;
845 } else {
846 dev_dbg(&dev->pdev->dev, "call amthi write\n");
847 rets = amthi_write(dev, write_cb);
849 if (rets) {
850 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
851 rets);
852 goto unlock_dev;
854 rets = length;
856 mutex_unlock(&dev->device_lock);
857 return rets;
860 write_cb->major_file_operations = MEI_WRITE;
861 /* make sure information is zero before we start */
863 write_cb->information = 0;
864 write_cb->request_buffer.size = length;
866 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
867 cl->host_client_id, cl->me_client_id);
868 if (cl->state != MEI_FILE_CONNECTED) {
869 rets = -ENODEV;
870 dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
871 cl->host_client_id,
872 cl->me_client_id);
873 goto unlock_dev;
875 for (i = 0; i < dev->me_clients_num; i++) {
876 if (dev->me_clients[i].client_id ==
877 cl->me_client_id)
878 break;
880 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
881 rets = -ENODEV;
882 goto unlock_dev;
884 if (i == dev->me_clients_num) {
885 rets = -ENODEV;
886 goto unlock_dev;
888 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
889 rets = -EINVAL;
890 goto unlock_dev;
892 write_cb->file_private = cl;
894 rets = mei_flow_ctrl_creds(dev, cl);
895 if (rets < 0)
896 goto unlock_dev;
898 if (rets && dev->mei_host_buffer_is_empty) {
899 rets = 0;
900 dev->mei_host_buffer_is_empty = false;
901 if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
902 sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
904 mei_hdr.length =
905 (((dev->host_hw_state & H_CBD) >> 24) *
906 sizeof(u32)) -
907 sizeof(struct mei_msg_hdr);
908 mei_hdr.msg_complete = 0;
909 } else {
910 mei_hdr.length = length;
911 mei_hdr.msg_complete = 1;
913 mei_hdr.host_addr = cl->host_client_id;
914 mei_hdr.me_addr = cl->me_client_id;
915 mei_hdr.reserved = 0;
916 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
917 *((u32 *) &mei_hdr));
918 if (!mei_write_message(dev, &mei_hdr,
919 (unsigned char *) (write_cb->request_buffer.data),
920 mei_hdr.length)) {
921 rets = -ENODEV;
922 goto unlock_dev;
924 cl->writing_state = MEI_WRITING;
925 write_cb->information = mei_hdr.length;
926 if (mei_hdr.msg_complete) {
927 if (mei_flow_ctrl_reduce(dev, cl)) {
928 rets = -ENODEV;
929 goto unlock_dev;
931 list_add_tail(&write_cb->cb_list,
932 &dev->write_waiting_list.mei_cb.cb_list);
933 } else {
934 list_add_tail(&write_cb->cb_list,
935 &dev->write_list.mei_cb.cb_list);
938 } else {
940 write_cb->information = 0;
941 cl->writing_state = MEI_WRITING;
942 list_add_tail(&write_cb->cb_list,
943 &dev->write_list.mei_cb.cb_list);
945 mutex_unlock(&dev->device_lock);
946 return length;
948 unlock_dev:
949 mutex_unlock(&dev->device_lock);
950 mei_free_cb_private(write_cb);
951 return rets;
956 * mei_ioctl - the IOCTL function
958 * @file: pointer to file structure
959 * @cmd: ioctl command
960 * @data: pointer to mei message structure
962 * returns 0 on success , <0 on error
964 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
966 struct mei_device *dev;
967 struct mei_cl *cl = file->private_data;
968 struct mei_connect_client_data *connect_data = NULL;
969 int rets;
971 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
972 return -EINVAL;
974 if (WARN_ON(!cl || !cl->dev))
975 return -ENODEV;
977 dev = cl->dev;
979 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
981 mutex_lock(&dev->device_lock);
982 if (dev->mei_state != MEI_ENABLED) {
983 rets = -ENODEV;
984 goto out;
987 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
989 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
990 GFP_KERNEL);
991 if (!connect_data) {
992 rets = -ENOMEM;
993 goto out;
995 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
996 if (copy_from_user(connect_data, (char __user *)data,
997 sizeof(struct mei_connect_client_data))) {
998 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
999 rets = -EFAULT;
1000 goto out;
1002 rets = mei_ioctl_connect_client(file, connect_data);
1004 /* if all is ok, copying the data back to user. */
1005 if (rets)
1006 goto out;
1008 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
1009 if (copy_to_user((char __user *)data, connect_data,
1010 sizeof(struct mei_connect_client_data))) {
1011 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
1012 rets = -EFAULT;
1013 goto out;
1016 out:
1017 kfree(connect_data);
1018 mutex_unlock(&dev->device_lock);
1019 return rets;
1023 * mei_compat_ioctl - the compat IOCTL function
1025 * @file: pointer to file structure
1026 * @cmd: ioctl command
1027 * @data: pointer to mei message structure
1029 * returns 0 on success , <0 on error
1031 #ifdef CONFIG_COMPAT
1032 static long mei_compat_ioctl(struct file *file,
1033 unsigned int cmd, unsigned long data)
1035 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
1037 #endif
1041 * mei_poll - the poll function
1043 * @file: pointer to file structure
1044 * @wait: pointer to poll_table structure
1046 * returns poll mask
1048 static unsigned int mei_poll(struct file *file, poll_table *wait)
1050 struct mei_cl *cl = file->private_data;
1051 struct mei_device *dev;
1052 unsigned int mask = 0;
1054 if (WARN_ON(!cl || !cl->dev))
1055 return mask;
1057 dev = cl->dev;
1059 mutex_lock(&dev->device_lock);
1061 if (dev->mei_state != MEI_ENABLED)
1062 goto out;
1065 if (cl == &dev->iamthif_cl) {
1066 mutex_unlock(&dev->device_lock);
1067 poll_wait(file, &dev->iamthif_cl.wait, wait);
1068 mutex_lock(&dev->device_lock);
1069 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
1070 dev->iamthif_file_object == file) {
1071 mask |= (POLLIN | POLLRDNORM);
1072 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
1073 mei_run_next_iamthif_cmd(dev);
1075 goto out;
1078 mutex_unlock(&dev->device_lock);
1079 poll_wait(file, &cl->tx_wait, wait);
1080 mutex_lock(&dev->device_lock);
1081 if (MEI_WRITE_COMPLETE == cl->writing_state)
1082 mask |= (POLLIN | POLLRDNORM);
1084 out:
1085 mutex_unlock(&dev->device_lock);
1086 return mask;
1089 #ifdef CONFIG_PM
1090 static int mei_pci_suspend(struct device *device)
1092 struct pci_dev *pdev = to_pci_dev(device);
1093 struct mei_device *dev = pci_get_drvdata(pdev);
1094 int err;
1096 if (!dev)
1097 return -ENODEV;
1098 mutex_lock(&dev->device_lock);
1099 /* Stop watchdog if exists */
1100 err = mei_wd_stop(dev, true);
1101 /* Set new mei state */
1102 if (dev->mei_state == MEI_ENABLED ||
1103 dev->mei_state == MEI_RECOVERING_FROM_RESET) {
1104 dev->mei_state = MEI_POWER_DOWN;
1105 mei_reset(dev, 0);
1107 mutex_unlock(&dev->device_lock);
1109 free_irq(pdev->irq, dev);
1110 pci_disable_msi(pdev);
1112 return err;
1115 static int mei_pci_resume(struct device *device)
1117 struct pci_dev *pdev = to_pci_dev(device);
1118 struct mei_device *dev;
1119 int err;
1121 dev = pci_get_drvdata(pdev);
1122 if (!dev)
1123 return -ENODEV;
1125 pci_enable_msi(pdev);
1127 /* request and enable interrupt */
1128 if (pci_dev_msi_enabled(pdev))
1129 err = request_threaded_irq(pdev->irq,
1130 NULL,
1131 mei_interrupt_thread_handler,
1132 0, mei_driver_name, dev);
1133 else
1134 err = request_threaded_irq(pdev->irq,
1135 mei_interrupt_quick_handler,
1136 mei_interrupt_thread_handler,
1137 IRQF_SHARED, mei_driver_name, dev);
1139 if (err) {
1140 printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
1141 pdev->irq);
1142 return err;
1145 mutex_lock(&dev->device_lock);
1146 dev->mei_state = MEI_POWER_UP;
1147 mei_reset(dev, 1);
1148 mutex_unlock(&dev->device_lock);
1150 /* Start watchdog if stopped in suspend */
1151 if (dev->wd_timeout) {
1152 mei_wd_start_setup(dev);
1153 dev->wd_due_counter = 1;
1154 schedule_delayed_work(&dev->wd_work, HZ);
1156 return err;
1158 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1159 #define MEI_PM_OPS (&mei_pm_ops)
1160 #else
1161 #define MEI_PM_OPS NULL
1162 #endif /* CONFIG_PM */
1164 * PCI driver structure
1166 static struct pci_driver mei_driver = {
1167 .name = mei_driver_name,
1168 .id_table = mei_pci_tbl,
1169 .probe = mei_probe,
1170 .remove = __devexit_p(mei_remove),
1171 .shutdown = __devexit_p(mei_remove),
1172 .driver.pm = MEI_PM_OPS,
1176 * file operations structure will be used for mei char device.
1178 static const struct file_operations mei_fops = {
1179 .owner = THIS_MODULE,
1180 .read = mei_read,
1181 .unlocked_ioctl = mei_ioctl,
1182 #ifdef CONFIG_COMPAT
1183 .compat_ioctl = mei_compat_ioctl,
1184 #endif
1185 .open = mei_open,
1186 .release = mei_release,
1187 .write = mei_write,
1188 .poll = mei_poll,
1192 * mei_registration_cdev - sets up the cdev structure for mei device.
1194 * @dev: char device struct
1195 * @hminor: minor number for registration char device
1196 * @fops: file operations structure
1198 * returns 0 on success, <0 on failure.
1200 static int mei_registration_cdev(struct cdev *dev, int hminor,
1201 const struct file_operations *fops)
1203 int ret, devno = MKDEV(mei_major, hminor);
1205 cdev_init(dev, fops);
1206 dev->owner = THIS_MODULE;
1207 ret = cdev_add(dev, devno, 1);
1208 /* Fail gracefully if need be */
1209 if (ret)
1210 printk(KERN_ERR "mei: Error %d registering mei device %d\n",
1211 ret, hminor);
1212 return ret;
1216 * mei_register_cdev - registers mei char device
1218 * returns 0 on success, <0 on failure.
1220 static int mei_register_cdev(void)
1222 int ret;
1223 dev_t dev;
1225 /* registration of char devices */
1226 ret = alloc_chrdev_region(&dev, MEI_MINORS_BASE, MEI_MINORS_COUNT,
1227 MEI_DRIVER_NAME);
1228 if (ret) {
1229 printk(KERN_ERR "mei: Error allocating char device region.\n");
1230 return ret;
1233 mei_major = MAJOR(dev);
1235 ret = mei_registration_cdev(&mei_cdev, MEI_MINOR_NUMBER,
1236 &mei_fops);
1237 if (ret)
1238 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1239 MEI_MINORS_COUNT);
1241 return ret;
1245 * mei_unregister_cdev - unregisters mei char device
1247 static void mei_unregister_cdev(void)
1249 cdev_del(&mei_cdev);
1250 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1251 MEI_MINORS_COUNT);
1255 * mei_sysfs_device_create - adds device entry to sysfs
1257 * returns 0 on success, <0 on failure.
1259 static int mei_sysfs_device_create(void)
1261 struct class *class;
1262 void *tmphdev;
1263 int err;
1265 class = class_create(THIS_MODULE, MEI_DRIVER_NAME);
1266 if (IS_ERR(class)) {
1267 err = PTR_ERR(class);
1268 printk(KERN_ERR "mei: Error creating mei class.\n");
1269 goto err_out;
1272 tmphdev = device_create(class, NULL, mei_cdev.dev, NULL,
1273 MEI_DEV_NAME);
1274 if (IS_ERR(tmphdev)) {
1275 err = PTR_ERR(tmphdev);
1276 goto err_destroy;
1279 mei_class = class;
1280 return 0;
1282 err_destroy:
1283 class_destroy(class);
1284 err_out:
1285 return err;
1289 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1291 static void mei_sysfs_device_remove(void)
1293 if (IS_ERR_OR_NULL(mei_class))
1294 return;
1296 device_destroy(mei_class, mei_cdev.dev);
1297 class_destroy(mei_class);
1301 * mei_init_module - Driver Registration Routine
1303 * mei_init_module is the first routine called when the driver is
1304 * loaded. All it does is to register with the PCI subsystem.
1306 * returns 0 on success, <0 on failure.
1308 static int __init mei_init_module(void)
1310 int ret;
1312 pr_debug("mei: %s - version %s\n",
1313 mei_driver_string, mei_driver_version);
1314 /* init pci module */
1315 ret = pci_register_driver(&mei_driver);
1316 if (ret < 0) {
1317 printk(KERN_ERR "mei: Error registering driver.\n");
1318 goto end;
1321 ret = mei_register_cdev();
1322 if (ret)
1323 goto unregister_pci;
1325 ret = mei_sysfs_device_create();
1326 if (ret)
1327 goto unregister_cdev;
1329 return ret;
1331 unregister_cdev:
1332 mei_unregister_cdev();
1333 unregister_pci:
1334 pci_unregister_driver(&mei_driver);
1335 end:
1336 return ret;
1339 module_init(mei_init_module);
1342 * mei_exit_module - Driver Exit Cleanup Routine
1344 * mei_exit_module is called just before the driver is removed
1345 * from memory.
1347 static void __exit mei_exit_module(void)
1349 mei_sysfs_device_remove();
1350 mei_unregister_cdev();
1351 pci_unregister_driver(&mei_driver);
1353 pr_debug("mei: Driver unloaded successfully.\n");
1356 module_exit(mei_exit_module);
1359 MODULE_AUTHOR("Intel Corporation");
1360 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1361 MODULE_LICENSE("GPL v2");
1362 MODULE_VERSION(MEI_DRIVER_VERSION);