staging: brcm80211: remove brcms_b_dotxstatus wrapper function
[zen-stable.git] / drivers / staging / mei / main.c
blobeb05c36f45d4181f0fb7c4df36165a9a9dd87e8f
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 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->timer_work, mei_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->timer_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 /* Unregistering watchdog device */
245 if (dev->wd_interface_reg)
246 watchdog_unregister_device(&amt_wd_dev);
248 /* remove entry if already in list */
249 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
250 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
251 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
253 dev->iamthif_current_cb = NULL;
254 dev->me_clients_num = 0;
256 mutex_unlock(&dev->device_lock);
258 flush_scheduled_work();
260 /* disable interrupts */
261 mei_disable_interrupts(dev);
263 free_irq(pdev->irq, dev);
264 pci_disable_msi(pdev);
265 pci_set_drvdata(pdev, NULL);
267 if (dev->mem_addr)
268 pci_iounmap(pdev, dev->mem_addr);
270 kfree(dev);
272 pci_release_regions(pdev);
273 pci_disable_device(pdev);
277 * mei_clear_list - removes all callbacks associated with file
278 * from mei_cb_list
280 * @dev: device structure.
281 * @file: file structure
282 * @mei_cb_list: callbacks list
284 * mei_clear_list is called to clear resources associated with file
285 * when application calls close function or Ctrl-C was pressed
287 * returns true if callback removed from the list, false otherwise
289 static bool mei_clear_list(struct mei_device *dev,
290 struct file *file, struct list_head *mei_cb_list)
292 struct mei_cl_cb *cb_pos = NULL;
293 struct mei_cl_cb *cb_next = NULL;
294 struct file *file_temp;
295 bool removed = false;
297 /* list all list member */
298 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
299 file_temp = (struct file *)cb_pos->file_object;
300 /* check if list member associated with a file */
301 if (file_temp == file) {
302 /* remove member from the list */
303 list_del(&cb_pos->cb_list);
304 /* check if cb equal to current iamthif cb */
305 if (dev->iamthif_current_cb == cb_pos) {
306 dev->iamthif_current_cb = NULL;
307 /* send flow control to iamthif client */
308 mei_send_flow_control(dev, &dev->iamthif_cl);
310 /* free all allocated buffers */
311 mei_free_cb_private(cb_pos);
312 cb_pos = NULL;
313 removed = true;
316 return removed;
320 * mei_clear_lists - removes all callbacks associated with file
322 * @dev: device structure
323 * @file: file structure
325 * mei_clear_lists is called to clear resources associated with file
326 * when application calls close function or Ctrl-C was pressed
328 * returns true if callback removed from the list, false otherwise
330 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
332 bool removed = false;
334 /* remove callbacks associated with a file */
335 mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
336 if (mei_clear_list(dev, file,
337 &dev->amthi_read_complete_list.mei_cb.cb_list))
338 removed = true;
340 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
342 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
343 removed = true;
345 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
346 removed = true;
348 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
349 removed = true;
351 /* check if iamthif_current_cb not NULL */
352 if (dev->iamthif_current_cb && !removed) {
353 /* check file and iamthif current cb association */
354 if (dev->iamthif_current_cb->file_object == file) {
355 /* remove cb */
356 mei_free_cb_private(dev->iamthif_current_cb);
357 dev->iamthif_current_cb = NULL;
358 removed = true;
361 return removed;
364 * find_read_list_entry - find read list entry
366 * @dev: device structure
367 * @file: pointer to file structure
369 * returns cb on success, NULL on error
371 static struct mei_cl_cb *find_read_list_entry(
372 struct mei_device *dev,
373 struct mei_cl *cl)
375 struct mei_cl_cb *cb_pos = NULL;
376 struct mei_cl_cb *cb_next = NULL;
378 if (!dev->read_list.status &&
379 !list_empty(&dev->read_list.mei_cb.cb_list)) {
381 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
382 list_for_each_entry_safe(cb_pos, cb_next,
383 &dev->read_list.mei_cb.cb_list, cb_list) {
384 struct mei_cl *cl_temp;
385 cl_temp = (struct mei_cl *)cb_pos->file_private;
387 if (mei_cl_cmp_id(cl, cl_temp))
388 return cb_pos;
391 return NULL;
395 * mei_open - the open function
397 * @inode: pointer to inode structure
398 * @file: pointer to file structure
400 * returns 0 on success, <0 on error
402 static int mei_open(struct inode *inode, struct file *file)
404 struct mei_cl *cl;
405 int if_num = iminor(inode), err;
406 struct mei_device *dev;
408 err = -ENODEV;
409 if (!mei_device)
410 goto out;
412 dev = pci_get_drvdata(mei_device);
413 if (if_num != MEI_MINOR_NUMBER || !dev)
414 goto out;
416 mutex_lock(&dev->device_lock);
417 err = -ENOMEM;
418 cl = mei_cl_allocate(dev);
419 if (!cl)
420 goto out_unlock;
422 err = -ENODEV;
423 if (dev->mei_state != MEI_ENABLED) {
424 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
425 dev->mei_state);
426 goto out_unlock;
428 err = -EMFILE;
429 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
430 goto out_unlock;
432 cl->host_client_id = find_first_zero_bit(dev->host_clients_map,
433 MEI_CLIENTS_MAX);
434 if (cl->host_client_id > MEI_CLIENTS_MAX)
435 goto out_unlock;
437 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
439 dev->open_handle_count++;
440 list_add_tail(&cl->link, &dev->file_list);
442 set_bit(cl->host_client_id, dev->host_clients_map);
443 cl->state = MEI_FILE_INITIALIZING;
444 cl->sm_state = 0;
446 file->private_data = cl;
447 mutex_unlock(&dev->device_lock);
449 return 0;
451 out_unlock:
452 mutex_unlock(&dev->device_lock);
453 kfree(cl);
454 out:
455 return err;
459 * mei_release - the release function
461 * @inode: pointer to inode structure
462 * @file: pointer to file structure
464 * returns 0 on success, <0 on error
466 static int mei_release(struct inode *inode, struct file *file)
468 struct mei_cl *cl = file->private_data;
469 struct mei_cl_cb *cb;
470 struct mei_device *dev;
471 int rets = 0;
473 if (WARN_ON(!cl || !cl->dev))
474 return -ENODEV;
476 dev = cl->dev;
478 mutex_lock(&dev->device_lock);
479 if (cl != &dev->iamthif_cl) {
480 if (cl->state == MEI_FILE_CONNECTED) {
481 cl->state = MEI_FILE_DISCONNECTING;
482 dev_dbg(&dev->pdev->dev,
483 "disconnecting client host client = %d, "
484 "ME client = %d\n",
485 cl->host_client_id,
486 cl->me_client_id);
487 rets = mei_disconnect_host_client(dev, cl);
489 mei_cl_flush_queues(cl);
490 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
491 cl->host_client_id,
492 cl->me_client_id);
494 if (dev->open_handle_count > 0) {
495 clear_bit(cl->host_client_id,
496 dev->host_clients_map);
497 dev->open_handle_count--;
499 mei_remove_client_from_file_list(dev, cl->host_client_id);
501 /* free read cb */
502 cb = NULL;
503 if (cl->read_cb) {
504 cb = find_read_list_entry(dev, cl);
505 /* Remove entry from read list */
506 if (cb)
507 list_del(&cb->cb_list);
509 cb = cl->read_cb;
510 cl->read_cb = NULL;
513 file->private_data = NULL;
515 if (cb) {
516 mei_free_cb_private(cb);
517 cb = NULL;
520 kfree(cl);
521 } else {
522 if (dev->open_handle_count > 0)
523 dev->open_handle_count--;
525 if (dev->iamthif_file_object == file &&
526 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
528 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
529 dev->iamthif_state);
530 dev->iamthif_canceled = true;
531 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
532 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
533 mei_run_next_iamthif_cmd(dev);
537 if (mei_clear_lists(dev, file))
538 dev->iamthif_state = MEI_IAMTHIF_IDLE;
541 mutex_unlock(&dev->device_lock);
542 return rets;
547 * mei_read - the read function.
549 * @file: pointer to file structure
550 * @ubuf: pointer to user buffer
551 * @length: buffer length
552 * @offset: data offset in buffer
554 * returns >=0 data length on success , <0 on error
556 static ssize_t mei_read(struct file *file, char __user *ubuf,
557 size_t length, loff_t *offset)
559 struct mei_cl *cl = file->private_data;
560 struct mei_cl_cb *cb_pos = NULL;
561 struct mei_cl_cb *cb = NULL;
562 struct mei_device *dev;
563 int i;
564 int rets;
565 int err;
568 if (WARN_ON(!cl || !cl->dev))
569 return -ENODEV;
571 dev = cl->dev;
573 mutex_lock(&dev->device_lock);
574 if (dev->mei_state != MEI_ENABLED) {
575 rets = -ENODEV;
576 goto out;
579 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
580 /* Do not allow to read watchdog client */
581 i = mei_find_me_client_index(dev, mei_wd_guid);
582 if (i >= 0) {
583 struct mei_me_client *me_client = &dev->me_clients[i];
585 if (cl->me_client_id == me_client->client_id) {
586 rets = -EBADF;
587 goto out;
590 } else {
591 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
594 if (cl == &dev->iamthif_cl) {
595 rets = amthi_read(dev, file, ubuf, length, offset);
596 goto out;
599 if (cl->read_cb && cl->read_cb->information > *offset) {
600 cb = cl->read_cb;
601 goto copy_buffer;
602 } else if (cl->read_cb && cl->read_cb->information > 0 &&
603 cl->read_cb->information <= *offset) {
604 cb = cl->read_cb;
605 rets = 0;
606 goto free;
607 } else if ((!cl->read_cb || !cl->read_cb->information) &&
608 *offset > 0) {
609 /*Offset needs to be cleaned for contingous reads*/
610 *offset = 0;
611 rets = 0;
612 goto out;
615 err = mei_start_read(dev, cl);
616 if (err && err != -EBUSY) {
617 dev_dbg(&dev->pdev->dev,
618 "mei start read failure with status = %d\n", err);
619 rets = err;
620 goto out;
623 if (MEI_READ_COMPLETE != cl->reading_state &&
624 !waitqueue_active(&cl->rx_wait)) {
625 if (file->f_flags & O_NONBLOCK) {
626 rets = -EAGAIN;
627 goto out;
630 mutex_unlock(&dev->device_lock);
632 if (wait_event_interruptible(cl->rx_wait,
633 (MEI_READ_COMPLETE == cl->reading_state ||
634 MEI_FILE_INITIALIZING == cl->state ||
635 MEI_FILE_DISCONNECTED == cl->state ||
636 MEI_FILE_DISCONNECTING == cl->state))) {
637 if (signal_pending(current))
638 return -EINTR;
639 return -ERESTARTSYS;
642 mutex_lock(&dev->device_lock);
643 if (MEI_FILE_INITIALIZING == cl->state ||
644 MEI_FILE_DISCONNECTED == cl->state ||
645 MEI_FILE_DISCONNECTING == cl->state) {
646 rets = -EBUSY;
647 goto out;
651 cb = cl->read_cb;
653 if (!cb) {
654 rets = -ENODEV;
655 goto out;
657 if (cl->reading_state != MEI_READ_COMPLETE) {
658 rets = 0;
659 goto out;
661 /* now copy the data to user space */
662 copy_buffer:
663 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
664 cb->response_buffer.size);
665 dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
666 cb->information);
667 if (length == 0 || ubuf == NULL || *offset > cb->information) {
668 rets = -EMSGSIZE;
669 goto free;
672 /* length is being turncated to PAGE_SIZE, however, */
673 /* information size may be longer */
674 length = min_t(size_t, length, (cb->information - *offset));
676 if (copy_to_user(ubuf,
677 cb->response_buffer.data + *offset,
678 length)) {
679 rets = -EFAULT;
680 goto free;
683 rets = length;
684 *offset += length;
685 if ((unsigned long)*offset < cb->information)
686 goto out;
688 free:
689 cb_pos = find_read_list_entry(dev, cl);
690 /* Remove entry from read list */
691 if (cb_pos)
692 list_del(&cb_pos->cb_list);
693 mei_free_cb_private(cb);
694 cl->reading_state = MEI_IDLE;
695 cl->read_cb = NULL;
696 cl->read_pending = 0;
697 out:
698 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
699 mutex_unlock(&dev->device_lock);
700 return rets;
704 * mei_write - the write function.
706 * @file: pointer to file structure
707 * @ubuf: pointer to user buffer
708 * @length: buffer length
709 * @offset: data offset in buffer
711 * returns >=0 data length on success , <0 on error
713 static ssize_t mei_write(struct file *file, const char __user *ubuf,
714 size_t length, loff_t *offset)
716 struct mei_cl *cl = file->private_data;
717 struct mei_cl_cb *write_cb = NULL;
718 struct mei_msg_hdr mei_hdr;
719 struct mei_device *dev;
720 unsigned long timeout = 0;
721 int rets;
722 int i;
724 if (WARN_ON(!cl || !cl->dev))
725 return -ENODEV;
727 dev = cl->dev;
729 mutex_lock(&dev->device_lock);
731 if (dev->mei_state != MEI_ENABLED) {
732 mutex_unlock(&dev->device_lock);
733 return -ENODEV;
736 if (cl == &dev->iamthif_cl) {
737 write_cb = find_amthi_read_list_entry(dev, file);
739 if (write_cb) {
740 timeout = write_cb->read_time +
741 msecs_to_jiffies(IAMTHIF_READ_TIMER);
743 if (time_after(jiffies, timeout) ||
744 cl->reading_state == MEI_READ_COMPLETE) {
745 *offset = 0;
746 list_del(&write_cb->cb_list);
747 mei_free_cb_private(write_cb);
748 write_cb = NULL;
753 /* free entry used in read */
754 if (cl->reading_state == MEI_READ_COMPLETE) {
755 *offset = 0;
756 write_cb = find_read_list_entry(dev, cl);
757 if (write_cb) {
758 list_del(&write_cb->cb_list);
759 mei_free_cb_private(write_cb);
760 write_cb = NULL;
761 cl->reading_state = MEI_IDLE;
762 cl->read_cb = NULL;
763 cl->read_pending = 0;
765 } else if (cl->reading_state == MEI_IDLE &&
766 !cl->read_pending)
767 *offset = 0;
770 write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
771 if (!write_cb) {
772 mutex_unlock(&dev->device_lock);
773 return -ENOMEM;
776 write_cb->file_object = file;
777 write_cb->file_private = cl;
778 write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
779 rets = -ENOMEM;
780 if (!write_cb->request_buffer.data)
781 goto unlock_dev;
783 dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
785 rets = -EFAULT;
786 if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
787 goto unlock_dev;
789 cl->sm_state = 0;
790 if (length == 4 &&
791 ((memcmp(mei_wd_state_independence_msg[0],
792 write_cb->request_buffer.data, 4) == 0) ||
793 (memcmp(mei_wd_state_independence_msg[1],
794 write_cb->request_buffer.data, 4) == 0) ||
795 (memcmp(mei_wd_state_independence_msg[2],
796 write_cb->request_buffer.data, 4) == 0)))
797 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
799 INIT_LIST_HEAD(&write_cb->cb_list);
800 if (cl == &dev->iamthif_cl) {
801 write_cb->response_buffer.data =
802 kmalloc(dev->iamthif_mtu, GFP_KERNEL);
803 if (!write_cb->response_buffer.data) {
804 rets = -ENOMEM;
805 goto unlock_dev;
807 if (dev->mei_state != MEI_ENABLED) {
808 rets = -ENODEV;
809 goto unlock_dev;
811 for (i = 0; i < dev->me_clients_num; i++) {
812 if (dev->me_clients[i].client_id ==
813 dev->iamthif_cl.me_client_id)
814 break;
817 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
818 rets = -ENODEV;
819 goto unlock_dev;
821 if (i == dev->me_clients_num ||
822 (dev->me_clients[i].client_id !=
823 dev->iamthif_cl.me_client_id)) {
824 rets = -ENODEV;
825 goto unlock_dev;
826 } else if (length > dev->me_clients[i].props.max_msg_length ||
827 length <= 0) {
828 rets = -EMSGSIZE;
829 goto unlock_dev;
832 write_cb->response_buffer.size = dev->iamthif_mtu;
833 write_cb->major_file_operations = MEI_IOCTL;
834 write_cb->information = 0;
835 write_cb->request_buffer.size = length;
836 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
837 rets = -ENODEV;
838 goto unlock_dev;
841 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
842 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
843 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
844 (int) dev->iamthif_state);
845 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
846 list_add_tail(&write_cb->cb_list,
847 &dev->amthi_cmd_list.mei_cb.cb_list);
848 rets = length;
849 } else {
850 dev_dbg(&dev->pdev->dev, "call amthi write\n");
851 rets = amthi_write(dev, write_cb);
853 if (rets) {
854 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
855 rets);
856 goto unlock_dev;
858 rets = length;
860 mutex_unlock(&dev->device_lock);
861 return rets;
864 write_cb->major_file_operations = MEI_WRITE;
865 /* make sure information is zero before we start */
867 write_cb->information = 0;
868 write_cb->request_buffer.size = length;
870 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
871 cl->host_client_id, cl->me_client_id);
872 if (cl->state != MEI_FILE_CONNECTED) {
873 rets = -ENODEV;
874 dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
875 cl->host_client_id,
876 cl->me_client_id);
877 goto unlock_dev;
879 for (i = 0; i < dev->me_clients_num; i++) {
880 if (dev->me_clients[i].client_id ==
881 cl->me_client_id)
882 break;
884 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
885 rets = -ENODEV;
886 goto unlock_dev;
888 if (i == dev->me_clients_num) {
889 rets = -ENODEV;
890 goto unlock_dev;
892 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
893 rets = -EINVAL;
894 goto unlock_dev;
896 write_cb->file_private = cl;
898 rets = mei_flow_ctrl_creds(dev, cl);
899 if (rets < 0)
900 goto unlock_dev;
902 if (rets && dev->mei_host_buffer_is_empty) {
903 rets = 0;
904 dev->mei_host_buffer_is_empty = false;
905 if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
906 sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
908 mei_hdr.length =
909 (((dev->host_hw_state & H_CBD) >> 24) *
910 sizeof(u32)) -
911 sizeof(struct mei_msg_hdr);
912 mei_hdr.msg_complete = 0;
913 } else {
914 mei_hdr.length = length;
915 mei_hdr.msg_complete = 1;
917 mei_hdr.host_addr = cl->host_client_id;
918 mei_hdr.me_addr = cl->me_client_id;
919 mei_hdr.reserved = 0;
920 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
921 *((u32 *) &mei_hdr));
922 if (!mei_write_message(dev, &mei_hdr,
923 (unsigned char *) (write_cb->request_buffer.data),
924 mei_hdr.length)) {
925 rets = -ENODEV;
926 goto unlock_dev;
928 cl->writing_state = MEI_WRITING;
929 write_cb->information = mei_hdr.length;
930 if (mei_hdr.msg_complete) {
931 if (mei_flow_ctrl_reduce(dev, cl)) {
932 rets = -ENODEV;
933 goto unlock_dev;
935 list_add_tail(&write_cb->cb_list,
936 &dev->write_waiting_list.mei_cb.cb_list);
937 } else {
938 list_add_tail(&write_cb->cb_list,
939 &dev->write_list.mei_cb.cb_list);
942 } else {
944 write_cb->information = 0;
945 cl->writing_state = MEI_WRITING;
946 list_add_tail(&write_cb->cb_list,
947 &dev->write_list.mei_cb.cb_list);
949 mutex_unlock(&dev->device_lock);
950 return length;
952 unlock_dev:
953 mutex_unlock(&dev->device_lock);
954 mei_free_cb_private(write_cb);
955 return rets;
960 * mei_ioctl - the IOCTL function
962 * @file: pointer to file structure
963 * @cmd: ioctl command
964 * @data: pointer to mei message structure
966 * returns 0 on success , <0 on error
968 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
970 struct mei_device *dev;
971 struct mei_cl *cl = file->private_data;
972 struct mei_connect_client_data *connect_data = NULL;
973 int rets;
975 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
976 return -EINVAL;
978 if (WARN_ON(!cl || !cl->dev))
979 return -ENODEV;
981 dev = cl->dev;
983 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
985 mutex_lock(&dev->device_lock);
986 if (dev->mei_state != MEI_ENABLED) {
987 rets = -ENODEV;
988 goto out;
991 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
993 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
994 GFP_KERNEL);
995 if (!connect_data) {
996 rets = -ENOMEM;
997 goto out;
999 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
1000 if (copy_from_user(connect_data, (char __user *)data,
1001 sizeof(struct mei_connect_client_data))) {
1002 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
1003 rets = -EFAULT;
1004 goto out;
1006 rets = mei_ioctl_connect_client(file, connect_data);
1008 /* if all is ok, copying the data back to user. */
1009 if (rets)
1010 goto out;
1012 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
1013 if (copy_to_user((char __user *)data, connect_data,
1014 sizeof(struct mei_connect_client_data))) {
1015 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
1016 rets = -EFAULT;
1017 goto out;
1020 out:
1021 kfree(connect_data);
1022 mutex_unlock(&dev->device_lock);
1023 return rets;
1027 * mei_compat_ioctl - the compat IOCTL function
1029 * @file: pointer to file structure
1030 * @cmd: ioctl command
1031 * @data: pointer to mei message structure
1033 * returns 0 on success , <0 on error
1035 #ifdef CONFIG_COMPAT
1036 static long mei_compat_ioctl(struct file *file,
1037 unsigned int cmd, unsigned long data)
1039 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
1041 #endif
1045 * mei_poll - the poll function
1047 * @file: pointer to file structure
1048 * @wait: pointer to poll_table structure
1050 * returns poll mask
1052 static unsigned int mei_poll(struct file *file, poll_table *wait)
1054 struct mei_cl *cl = file->private_data;
1055 struct mei_device *dev;
1056 unsigned int mask = 0;
1058 if (WARN_ON(!cl || !cl->dev))
1059 return mask;
1061 dev = cl->dev;
1063 mutex_lock(&dev->device_lock);
1065 if (dev->mei_state != MEI_ENABLED)
1066 goto out;
1069 if (cl == &dev->iamthif_cl) {
1070 mutex_unlock(&dev->device_lock);
1071 poll_wait(file, &dev->iamthif_cl.wait, wait);
1072 mutex_lock(&dev->device_lock);
1073 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
1074 dev->iamthif_file_object == file) {
1075 mask |= (POLLIN | POLLRDNORM);
1076 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
1077 mei_run_next_iamthif_cmd(dev);
1079 goto out;
1082 mutex_unlock(&dev->device_lock);
1083 poll_wait(file, &cl->tx_wait, wait);
1084 mutex_lock(&dev->device_lock);
1085 if (MEI_WRITE_COMPLETE == cl->writing_state)
1086 mask |= (POLLIN | POLLRDNORM);
1088 out:
1089 mutex_unlock(&dev->device_lock);
1090 return mask;
1093 #ifdef CONFIG_PM
1094 static int mei_pci_suspend(struct device *device)
1096 struct pci_dev *pdev = to_pci_dev(device);
1097 struct mei_device *dev = pci_get_drvdata(pdev);
1098 int err;
1100 if (!dev)
1101 return -ENODEV;
1102 mutex_lock(&dev->device_lock);
1103 /* Stop watchdog if exists */
1104 err = mei_wd_stop(dev, true);
1105 /* Set new mei state */
1106 if (dev->mei_state == MEI_ENABLED ||
1107 dev->mei_state == MEI_RECOVERING_FROM_RESET) {
1108 dev->mei_state = MEI_POWER_DOWN;
1109 mei_reset(dev, 0);
1111 mutex_unlock(&dev->device_lock);
1113 free_irq(pdev->irq, dev);
1114 pci_disable_msi(pdev);
1116 return err;
1119 static int mei_pci_resume(struct device *device)
1121 struct pci_dev *pdev = to_pci_dev(device);
1122 struct mei_device *dev;
1123 int err;
1125 dev = pci_get_drvdata(pdev);
1126 if (!dev)
1127 return -ENODEV;
1129 pci_enable_msi(pdev);
1131 /* request and enable interrupt */
1132 if (pci_dev_msi_enabled(pdev))
1133 err = request_threaded_irq(pdev->irq,
1134 NULL,
1135 mei_interrupt_thread_handler,
1136 0, mei_driver_name, dev);
1137 else
1138 err = request_threaded_irq(pdev->irq,
1139 mei_interrupt_quick_handler,
1140 mei_interrupt_thread_handler,
1141 IRQF_SHARED, mei_driver_name, dev);
1143 if (err) {
1144 printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
1145 pdev->irq);
1146 return err;
1149 mutex_lock(&dev->device_lock);
1150 dev->mei_state = MEI_POWER_UP;
1151 mei_reset(dev, 1);
1152 mutex_unlock(&dev->device_lock);
1154 /* Start timer if stopped in suspend */
1155 schedule_delayed_work(&dev->timer_work, HZ);
1157 return err;
1159 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1160 #define MEI_PM_OPS (&mei_pm_ops)
1161 #else
1162 #define MEI_PM_OPS NULL
1163 #endif /* CONFIG_PM */
1165 * PCI driver structure
1167 static struct pci_driver mei_driver = {
1168 .name = mei_driver_name,
1169 .id_table = mei_pci_tbl,
1170 .probe = mei_probe,
1171 .remove = __devexit_p(mei_remove),
1172 .shutdown = __devexit_p(mei_remove),
1173 .driver.pm = MEI_PM_OPS,
1177 * file operations structure will be used for mei char device.
1179 static const struct file_operations mei_fops = {
1180 .owner = THIS_MODULE,
1181 .read = mei_read,
1182 .unlocked_ioctl = mei_ioctl,
1183 #ifdef CONFIG_COMPAT
1184 .compat_ioctl = mei_compat_ioctl,
1185 #endif
1186 .open = mei_open,
1187 .release = mei_release,
1188 .write = mei_write,
1189 .poll = mei_poll,
1193 * mei_registration_cdev - sets up the cdev structure for mei device.
1195 * @dev: char device struct
1196 * @hminor: minor number for registration char device
1197 * @fops: file operations structure
1199 * returns 0 on success, <0 on failure.
1201 static int mei_registration_cdev(struct cdev *dev, int hminor,
1202 const struct file_operations *fops)
1204 int ret, devno = MKDEV(mei_major, hminor);
1206 cdev_init(dev, fops);
1207 dev->owner = THIS_MODULE;
1208 ret = cdev_add(dev, devno, 1);
1209 /* Fail gracefully if need be */
1210 if (ret)
1211 printk(KERN_ERR "mei: Error %d registering mei device %d\n",
1212 ret, hminor);
1213 return ret;
1217 * mei_register_cdev - registers mei char device
1219 * returns 0 on success, <0 on failure.
1221 static int mei_register_cdev(void)
1223 int ret;
1224 dev_t dev;
1226 /* registration of char devices */
1227 ret = alloc_chrdev_region(&dev, MEI_MINORS_BASE, MEI_MINORS_COUNT,
1228 MEI_DRIVER_NAME);
1229 if (ret) {
1230 printk(KERN_ERR "mei: Error allocating char device region.\n");
1231 return ret;
1234 mei_major = MAJOR(dev);
1236 ret = mei_registration_cdev(&mei_cdev, MEI_MINOR_NUMBER,
1237 &mei_fops);
1238 if (ret)
1239 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1240 MEI_MINORS_COUNT);
1242 return ret;
1246 * mei_unregister_cdev - unregisters mei char device
1248 static void mei_unregister_cdev(void)
1250 cdev_del(&mei_cdev);
1251 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1252 MEI_MINORS_COUNT);
1256 * mei_sysfs_device_create - adds device entry to sysfs
1258 * returns 0 on success, <0 on failure.
1260 static int mei_sysfs_device_create(void)
1262 struct class *class;
1263 void *tmphdev;
1264 int err;
1266 class = class_create(THIS_MODULE, MEI_DRIVER_NAME);
1267 if (IS_ERR(class)) {
1268 err = PTR_ERR(class);
1269 printk(KERN_ERR "mei: Error creating mei class.\n");
1270 goto err_out;
1273 tmphdev = device_create(class, NULL, mei_cdev.dev, NULL,
1274 MEI_DEV_NAME);
1275 if (IS_ERR(tmphdev)) {
1276 err = PTR_ERR(tmphdev);
1277 goto err_destroy;
1280 mei_class = class;
1281 return 0;
1283 err_destroy:
1284 class_destroy(class);
1285 err_out:
1286 return err;
1290 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1292 static void mei_sysfs_device_remove(void)
1294 if (IS_ERR_OR_NULL(mei_class))
1295 return;
1297 device_destroy(mei_class, mei_cdev.dev);
1298 class_destroy(mei_class);
1302 * mei_init_module - Driver Registration Routine
1304 * mei_init_module is the first routine called when the driver is
1305 * loaded. All it does is to register with the PCI subsystem.
1307 * returns 0 on success, <0 on failure.
1309 static int __init mei_init_module(void)
1311 int ret;
1313 pr_debug("mei: %s - version %s\n",
1314 mei_driver_string, mei_driver_version);
1315 /* init pci module */
1316 ret = pci_register_driver(&mei_driver);
1317 if (ret < 0) {
1318 printk(KERN_ERR "mei: Error registering driver.\n");
1319 goto end;
1322 ret = mei_register_cdev();
1323 if (ret)
1324 goto unregister_pci;
1326 ret = mei_sysfs_device_create();
1327 if (ret)
1328 goto unregister_cdev;
1330 return ret;
1332 unregister_cdev:
1333 mei_unregister_cdev();
1334 unregister_pci:
1335 pci_unregister_driver(&mei_driver);
1336 end:
1337 return ret;
1340 module_init(mei_init_module);
1343 * mei_exit_module - Driver Exit Cleanup Routine
1345 * mei_exit_module is called just before the driver is removed
1346 * from memory.
1348 static void __exit mei_exit_module(void)
1350 mei_sysfs_device_remove();
1351 mei_unregister_cdev();
1352 pci_unregister_driver(&mei_driver);
1354 pr_debug("mei: Driver unloaded successfully.\n");
1357 module_exit(mei_exit_module);
1360 MODULE_AUTHOR("Intel Corporation");
1361 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1362 MODULE_LICENSE("GPL v2");
1363 MODULE_VERSION(MEI_DRIVER_VERSION);