]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/mei/main.c
staging:mei: wd_ops and wd_info should be static
[mirror_ubuntu-artful-kernel.git] / drivers / staging / mei / main.c
CommitLineData
ab841160
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2011, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
ab841160
OW
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>
ab841160
OW
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>
36
37#include "mei_dev.h"
38#include "mei.h"
39#include "interface.h"
40#include "mei_version.h"
41
42
43#define MEI_READ_TIMEOUT 45
44#define MEI_DRIVER_NAME "mei"
45#define MEI_DEV_NAME "mei"
46
47/*
48 * mei driver strings
49 */
50static char mei_driver_name[] = MEI_DRIVER_NAME;
51static const char mei_driver_string[] = "Intel(R) Management Engine Interface";
52static const char mei_driver_version[] = MEI_DRIVER_VERSION;
53
54/* mei char device for registration */
55static struct cdev mei_cdev;
56
57/* major number for device */
58static int mei_major;
59/* The device pointer */
60/* Currently this driver works as long as there is only a single AMT device. */
4a3cafd5 61struct pci_dev *mei_device;
ab841160
OW
62
63static struct class *mei_class;
64
65
66/* mei_pci_tbl - PCI Device ID Table */
67static 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)},
99
100 /* required last entry */
101 {0, }
102};
103
104MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
105
106static DEFINE_MUTEX(mei_mutex);
107
108/**
109 * mei_probe - Device Initialization Routine
110 *
111 * @pdev: PCI device structure
112 * @ent: entry in kcs_pci_tbl
113 *
114 * returns 0 on success, <0 on failure.
115 */
116static int __devinit mei_probe(struct pci_dev *pdev,
117 const struct pci_device_id *ent)
118{
119 struct mei_device *dev;
120 int err;
121
122 mutex_lock(&mei_mutex);
123 if (mei_device) {
124 err = -EEXIST;
125 goto end;
126 }
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;
132 }
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;
140 }
141 /* allocates and initializes the mei dev structure */
c95efb74 142 dev = mei_device_init(pdev);
ab841160
OW
143 if (!dev) {
144 err = -ENOMEM;
145 goto release_regions;
146 }
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;
153 }
4f61a7ad
TW
154 pci_enable_msi(pdev);
155
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,
ab841160
OW
164 mei_interrupt_quick_handler,
165 mei_interrupt_thread_handler,
166 IRQF_SHARED, mei_driver_name, dev);
4f61a7ad 167
ab841160
OW
168 if (err) {
169 printk(KERN_ERR "mei: request_threaded_irq failure. irq = %d\n",
170 pdev->irq);
171 goto unmap_memory;
172 }
a61c6530 173 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
ab841160
OW
174 if (mei_hw_init(dev)) {
175 printk(KERN_ERR "mei: Init hw failure.\n");
176 err = -ENODEV;
177 goto release_irq;
178 }
179 mei_device = pdev;
180 pci_set_drvdata(pdev, dev);
a61c6530 181 schedule_delayed_work(&dev->timer_work, HZ);
ab841160
OW
182
183 mutex_unlock(&mei_mutex);
184
185 pr_debug("mei: Driver initialization successful.\n");
186
187 return 0;
188
189release_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);
4f61a7ad 195 pci_disable_msi(pdev);
ab841160
OW
196unmap_memory:
197 pci_iounmap(pdev, dev->mem_addr);
198free_device:
199 kfree(dev);
200release_regions:
201 pci_release_regions(pdev);
202disable_device:
203 pci_disable_device(pdev);
204end:
205 mutex_unlock(&mei_mutex);
206 printk(KERN_ERR "mei: Driver initialization failed.\n");
207 return err;
208}
209
210/**
211 * mei_remove - Device Removal Routine
212 *
213 * @pdev: PCI device structure
214 *
215 * mei_remove is called by the PCI subsystem to alert the driver
216 * that it should release a PCI device.
217 */
218static void __devexit mei_remove(struct pci_dev *pdev)
219{
220 struct mei_device *dev;
221
222 if (mei_device != pdev)
223 return;
224
225 dev = pci_get_drvdata(pdev);
226 if (!dev)
227 return;
228
229 mutex_lock(&dev->device_lock);
230
231 mei_wd_stop(dev, false);
232
233 mei_device = NULL;
234
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);
238 }
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);
242 }
243
9ce178e5
OW
244 /* Unregistering watchdog device */
245 if (dev->wd_interface_reg)
246 watchdog_unregister_device(&amt_wd_dev);
247
ab841160
OW
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);
252
253 dev->iamthif_current_cb = NULL;
cf9673da 254 dev->me_clients_num = 0;
ab841160
OW
255
256 mutex_unlock(&dev->device_lock);
257
258 flush_scheduled_work();
259
260 /* disable interrupts */
261 mei_disable_interrupts(dev);
262
263 free_irq(pdev->irq, dev);
4f61a7ad 264 pci_disable_msi(pdev);
ab841160
OW
265 pci_set_drvdata(pdev, NULL);
266
267 if (dev->mem_addr)
268 pci_iounmap(pdev, dev->mem_addr);
269
270 kfree(dev);
271
272 pci_release_regions(pdev);
273 pci_disable_device(pdev);
274}
275
276/**
277 * mei_clear_list - removes all callbacks associated with file
278 * from mei_cb_list
279 *
280 * @dev: device structure.
281 * @file: file structure
282 * @mei_cb_list: callbacks list
283 *
284 * mei_clear_list is called to clear resources associated with file
285 * when application calls close function or Ctrl-C was pressed
286 *
287 * returns true if callback removed from the list, false otherwise
288 */
289static bool mei_clear_list(struct mei_device *dev,
290 struct file *file, struct list_head *mei_cb_list)
291{
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;
296
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);
309 }
310 /* free all allocated buffers */
311 mei_free_cb_private(cb_pos);
312 cb_pos = NULL;
313 removed = true;
314 }
315 }
316 return removed;
317}
318
319/**
320 * mei_clear_lists - removes all callbacks associated with file
321 *
322 * @dev: device structure
323 * @file: file structure
324 *
325 * mei_clear_lists is called to clear resources associated with file
326 * when application calls close function or Ctrl-C was pressed
327 *
328 * returns true if callback removed from the list, false otherwise
329 */
330static bool mei_clear_lists(struct mei_device *dev, struct file *file)
331{
332 bool removed = false;
333
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;
339
340 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
341
342 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
343 removed = true;
344
345 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
346 removed = true;
347
348 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
349 removed = true;
350
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;
359 }
360 }
361 return removed;
362}
363/**
364 * find_read_list_entry - find read list entry
365 *
366 * @dev: device structure
367 * @file: pointer to file structure
368 *
369 * returns cb on success, NULL on error
370 */
371static struct mei_cl_cb *find_read_list_entry(
372 struct mei_device *dev,
373 struct mei_cl *cl)
374{
375 struct mei_cl_cb *cb_pos = NULL;
376 struct mei_cl_cb *cb_next = NULL;
ab841160
OW
377
378 if (!dev->read_list.status &&
379 !list_empty(&dev->read_list.mei_cb.cb_list)) {
380
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) {
0288c7c9
TW
384 struct mei_cl *cl_temp;
385 cl_temp = (struct mei_cl *)cb_pos->file_private;
ab841160 386
0288c7c9 387 if (mei_cl_cmp_id(cl, cl_temp))
ab841160 388 return cb_pos;
ab841160
OW
389 }
390 }
391 return NULL;
392}
393
394/**
395 * mei_open - the open function
396 *
397 * @inode: pointer to inode structure
398 * @file: pointer to file structure
399 *
400 * returns 0 on success, <0 on error
401 */
402static int mei_open(struct inode *inode, struct file *file)
403{
404 struct mei_cl *cl;
405 int if_num = iminor(inode), err;
406 struct mei_device *dev;
407
408 err = -ENODEV;
409 if (!mei_device)
410 goto out;
411
412 dev = pci_get_drvdata(mei_device);
413 if (if_num != MEI_MINOR_NUMBER || !dev)
414 goto out;
415
416 mutex_lock(&dev->device_lock);
417 err = -ENOMEM;
c95efb74 418 cl = mei_cl_allocate(dev);
ab841160 419 if (!cl)
303dfbf5 420 goto out_unlock;
ab841160
OW
421
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;
427 }
428 err = -EMFILE;
429 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
430 goto out_unlock;
431
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;
436
437 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
438
439 dev->open_handle_count++;
440 list_add_tail(&cl->link, &dev->file_list);
441
442 set_bit(cl->host_client_id, dev->host_clients_map);
443 cl->state = MEI_FILE_INITIALIZING;
444 cl->sm_state = 0;
445
446 file->private_data = cl;
447 mutex_unlock(&dev->device_lock);
448
449 return 0;
450
451out_unlock:
452 mutex_unlock(&dev->device_lock);
453 kfree(cl);
454out:
455 return err;
456}
457
458/**
459 * mei_release - the release function
460 *
461 * @inode: pointer to inode structure
462 * @file: pointer to file structure
463 *
464 * returns 0 on success, <0 on error
465 */
466static int mei_release(struct inode *inode, struct file *file)
467{
468 struct mei_cl *cl = file->private_data;
469 struct mei_cl_cb *cb;
470 struct mei_device *dev;
471 int rets = 0;
472
473 if (WARN_ON(!cl || !cl->dev))
474 return -ENODEV;
475
476 dev = cl->dev;
477
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);
488 }
0288c7c9 489 mei_cl_flush_queues(cl);
ab841160
OW
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);
493
494 if (dev->open_handle_count > 0) {
495 clear_bit(cl->host_client_id,
496 dev->host_clients_map);
497 dev->open_handle_count--;
498 }
499 mei_remove_client_from_file_list(dev, cl->host_client_id);
500
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);
508
509 cb = cl->read_cb;
510 cl->read_cb = NULL;
511 }
512
513 file->private_data = NULL;
514
515 if (cb) {
516 mei_free_cb_private(cb);
517 cb = NULL;
518 }
519
520 kfree(cl);
521 } else {
522 if (dev->open_handle_count > 0)
523 dev->open_handle_count--;
524
525 if (dev->iamthif_file_object == file &&
526 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
527
528 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
529 dev->iamthif_state);
eb9af0ac 530 dev->iamthif_canceled = true;
ab841160
OW
531 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
532 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
c95efb74 533 mei_run_next_iamthif_cmd(dev);
ab841160
OW
534 }
535 }
536
537 if (mei_clear_lists(dev, file))
538 dev->iamthif_state = MEI_IAMTHIF_IDLE;
539
540 }
541 mutex_unlock(&dev->device_lock);
542 return rets;
543}
544
545
546/**
547 * mei_read - the read function.
548 *
549 * @file: pointer to file structure
550 * @ubuf: pointer to user buffer
551 * @length: buffer length
552 * @offset: data offset in buffer
553 *
554 * returns >=0 data length on success , <0 on error
555 */
556static ssize_t mei_read(struct file *file, char __user *ubuf,
557 size_t length, loff_t *offset)
558{
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;
566
567
568 if (WARN_ON(!cl || !cl->dev))
569 return -ENODEV;
570
571 dev = cl->dev;
572
573 mutex_lock(&dev->device_lock);
574 if (dev->mei_state != MEI_ENABLED) {
575 rets = -ENODEV;
576 goto out;
577 }
578
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];
584
585 if (cl->me_client_id == me_client->client_id) {
586 rets = -EBADF;
587 goto out;
588 }
589 }
590 } else {
591 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
592 }
593
594 if (cl == &dev->iamthif_cl) {
595 rets = amthi_read(dev, file, ubuf, length, offset);
596 goto out;
597 }
598
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;
613 }
614
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;
621 }
622
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;
628 }
629
630 mutex_unlock(&dev->device_lock);
631
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;
640 }
641
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;
648 }
649 }
650
651 cb = cl->read_cb;
652
653 if (!cb) {
654 rets = -ENODEV;
655 goto out;
656 }
657 if (cl->reading_state != MEI_READ_COMPLETE) {
658 rets = 0;
659 goto out;
660 }
661 /* now copy the data to user space */
662copy_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;
670 }
671
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));
675
676 if (copy_to_user(ubuf,
677 cb->response_buffer.data + *offset,
678 length)) {
679 rets = -EFAULT;
680 goto free;
681 }
682
683 rets = length;
684 *offset += length;
685 if ((unsigned long)*offset < cb->information)
686 goto out;
687
688free:
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;
697out:
698 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
699 mutex_unlock(&dev->device_lock);
700 return rets;
701}
702
703/**
704 * mei_write - the write function.
705 *
706 * @file: pointer to file structure
707 * @ubuf: pointer to user buffer
708 * @length: buffer length
709 * @offset: data offset in buffer
710 *
711 * returns >=0 data length on success , <0 on error
712 */
713static ssize_t mei_write(struct file *file, const char __user *ubuf,
714 size_t length, loff_t *offset)
715{
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;
723
724 if (WARN_ON(!cl || !cl->dev))
725 return -ENODEV;
726
727 dev = cl->dev;
728
729 mutex_lock(&dev->device_lock);
730
731 if (dev->mei_state != MEI_ENABLED) {
732 mutex_unlock(&dev->device_lock);
733 return -ENODEV;
734 }
735
736 if (cl == &dev->iamthif_cl) {
737 write_cb = find_amthi_read_list_entry(dev, file);
738
739 if (write_cb) {
740 timeout = write_cb->read_time +
741 msecs_to_jiffies(IAMTHIF_READ_TIMER);
742
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;
749 }
750 }
751 }
752
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;
764 }
765 } else if (cl->reading_state == MEI_IDLE &&
766 !cl->read_pending)
767 *offset = 0;
768
769
770 write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
771 if (!write_cb) {
772 mutex_unlock(&dev->device_lock);
773 return -ENOMEM;
774 }
775
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;
782
783 dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
784
785 rets = -EFAULT;
786 if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
787 goto unlock_dev;
788
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;
798
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;
806 }
807 if (dev->mei_state != MEI_ENABLED) {
808 rets = -ENODEV;
809 goto unlock_dev;
810 }
cf9673da 811 for (i = 0; i < dev->me_clients_num; i++) {
ab841160
OW
812 if (dev->me_clients[i].client_id ==
813 dev->iamthif_cl.me_client_id)
814 break;
815 }
816
817 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
818 rets = -ENODEV;
819 goto unlock_dev;
820 }
cf9673da 821 if (i == dev->me_clients_num ||
ab841160
OW
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;
830 }
831
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;
839 }
840
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);
852
853 if (rets) {
854 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
855 rets);
856 goto unlock_dev;
857 }
858 rets = length;
859 }
860 mutex_unlock(&dev->device_lock);
861 return rets;
862 }
863
864 write_cb->major_file_operations = MEI_WRITE;
865 /* make sure information is zero before we start */
866
867 write_cb->information = 0;
868 write_cb->request_buffer.size = length;
869
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;
878 }
cf9673da 879 for (i = 0; i < dev->me_clients_num; i++) {
ab841160
OW
880 if (dev->me_clients[i].client_id ==
881 cl->me_client_id)
882 break;
883 }
884 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
885 rets = -ENODEV;
886 goto unlock_dev;
887 }
cf9673da 888 if (i == dev->me_clients_num) {
ab841160
OW
889 rets = -ENODEV;
890 goto unlock_dev;
891 }
892 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
893 rets = -EINVAL;
894 goto unlock_dev;
895 }
896 write_cb->file_private = cl;
897
898 rets = mei_flow_ctrl_creds(dev, cl);
899 if (rets < 0)
900 goto unlock_dev;
901
902 if (rets && dev->mei_host_buffer_is_empty) {
903 rets = 0;
eb9af0ac 904 dev->mei_host_buffer_is_empty = false;
ab841160
OW
905 if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
906 sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
907
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;
916 }
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;
927 }
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;
934 }
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);
940 }
941
942 } else {
943
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);
948 }
949 mutex_unlock(&dev->device_lock);
950 return length;
951
952unlock_dev:
953 mutex_unlock(&dev->device_lock);
954 mei_free_cb_private(write_cb);
955 return rets;
956}
957
958
959/**
960 * mei_ioctl - the IOCTL function
961 *
962 * @file: pointer to file structure
963 * @cmd: ioctl command
964 * @data: pointer to mei message structure
965 *
966 * returns 0 on success , <0 on error
967 */
968static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
969{
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;
974
975 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
976 return -EINVAL;
977
978 if (WARN_ON(!cl || !cl->dev))
979 return -ENODEV;
980
981 dev = cl->dev;
982
983 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
984
985 mutex_lock(&dev->device_lock);
986 if (dev->mei_state != MEI_ENABLED) {
987 rets = -ENODEV;
988 goto out;
989 }
990
991 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
992
993 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
994 GFP_KERNEL);
995 if (!connect_data) {
996 rets = -ENOMEM;
997 goto out;
998 }
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;
1005 }
1006 rets = mei_ioctl_connect_client(file, connect_data);
1007
1008 /* if all is ok, copying the data back to user. */
1009 if (rets)
1010 goto out;
1011
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;
1018 }
1019
1020out:
1021 kfree(connect_data);
1022 mutex_unlock(&dev->device_lock);
1023 return rets;
1024}
1025
1026/**
1027 * mei_compat_ioctl - the compat IOCTL function
1028 *
1029 * @file: pointer to file structure
1030 * @cmd: ioctl command
1031 * @data: pointer to mei message structure
1032 *
1033 * returns 0 on success , <0 on error
1034 */
1035#ifdef CONFIG_COMPAT
1036static long mei_compat_ioctl(struct file *file,
1037 unsigned int cmd, unsigned long data)
1038{
1039 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
1040}
1041#endif
1042
1043
1044/**
1045 * mei_poll - the poll function
1046 *
1047 * @file: pointer to file structure
1048 * @wait: pointer to poll_table structure
1049 *
1050 * returns poll mask
1051 */
1052static unsigned int mei_poll(struct file *file, poll_table *wait)
1053{
1054 struct mei_cl *cl = file->private_data;
1055 struct mei_device *dev;
1056 unsigned int mask = 0;
1057
1058 if (WARN_ON(!cl || !cl->dev))
1059 return mask;
1060
1061 dev = cl->dev;
1062
1063 mutex_lock(&dev->device_lock);
1064
1065 if (dev->mei_state != MEI_ENABLED)
1066 goto out;
1067
1068
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");
c95efb74 1077 mei_run_next_iamthif_cmd(dev);
ab841160
OW
1078 }
1079 goto out;
1080 }
1081
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);
1087
1088out:
1089 mutex_unlock(&dev->device_lock);
1090 return mask;
1091}
1092
1093#ifdef CONFIG_PM
1094static int mei_pci_suspend(struct device *device)
1095{
1096 struct pci_dev *pdev = to_pci_dev(device);
1097 struct mei_device *dev = pci_get_drvdata(pdev);
1098 int err;
1099
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);
1110 }
1111 mutex_unlock(&dev->device_lock);
1112
1113 free_irq(pdev->irq, dev);
4f61a7ad 1114 pci_disable_msi(pdev);
ab841160
OW
1115
1116 return err;
1117}
1118
1119static int mei_pci_resume(struct device *device)
1120{
1121 struct pci_dev *pdev = to_pci_dev(device);
1122 struct mei_device *dev;
1123 int err;
1124
1125 dev = pci_get_drvdata(pdev);
1126 if (!dev)
1127 return -ENODEV;
1128
4f61a7ad
TW
1129 pci_enable_msi(pdev);
1130
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,
ab841160
OW
1139 mei_interrupt_quick_handler,
1140 mei_interrupt_thread_handler,
1141 IRQF_SHARED, mei_driver_name, dev);
4f61a7ad 1142
ab841160
OW
1143 if (err) {
1144 printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
1145 pdev->irq);
1146 return err;
1147 }
1148
1149 mutex_lock(&dev->device_lock);
1150 dev->mei_state = MEI_POWER_UP;
1151 mei_reset(dev, 1);
1152 mutex_unlock(&dev->device_lock);
1153
6d70e935
OW
1154 /* Start timer if stopped in suspend */
1155 schedule_delayed_work(&dev->timer_work, HZ);
1156
ab841160
OW
1157 return err;
1158}
1159static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1160#define MEI_PM_OPS (&mei_pm_ops)
1161#else
2d990362 1162#define MEI_PM_OPS NULL
ab841160
OW
1163#endif /* CONFIG_PM */
1164/*
1165 * PCI driver structure
1166 */
1167static 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,
1174};
1175
1176/*
1177 * file operations structure will be used for mei char device.
1178 */
1179static 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,
1190};
1191
1192/**
1193 * mei_registration_cdev - sets up the cdev structure for mei device.
1194 *
1195 * @dev: char device struct
1196 * @hminor: minor number for registration char device
1197 * @fops: file operations structure
1198 *
1199 * returns 0 on success, <0 on failure.
1200 */
1201static int mei_registration_cdev(struct cdev *dev, int hminor,
1202 const struct file_operations *fops)
1203{
1204 int ret, devno = MKDEV(mei_major, hminor);
1205
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;
1214}
1215
1216/**
1217 * mei_register_cdev - registers mei char device
1218 *
1219 * returns 0 on success, <0 on failure.
1220 */
1221static int mei_register_cdev(void)
1222{
1223 int ret;
1224 dev_t dev;
1225
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;
1232 }
1233
1234 mei_major = MAJOR(dev);
1235
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);
1241
1242 return ret;
1243}
1244
1245/**
1246 * mei_unregister_cdev - unregisters mei char device
1247 */
1248static void mei_unregister_cdev(void)
1249{
1250 cdev_del(&mei_cdev);
1251 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1252 MEI_MINORS_COUNT);
1253}
1254
1255/**
1256 * mei_sysfs_device_create - adds device entry to sysfs
1257 *
1258 * returns 0 on success, <0 on failure.
1259 */
1260static int mei_sysfs_device_create(void)
1261{
1262 struct class *class;
1263 void *tmphdev;
1264 int err;
1265
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;
1271 }
1272
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;
1278 }
1279
1280 mei_class = class;
1281 return 0;
1282
1283err_destroy:
1284 class_destroy(class);
1285err_out:
1286 return err;
1287}
1288
1289/**
1290 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1291 */
1292static void mei_sysfs_device_remove(void)
1293{
1294 if (IS_ERR_OR_NULL(mei_class))
1295 return;
1296
1297 device_destroy(mei_class, mei_cdev.dev);
1298 class_destroy(mei_class);
1299}
1300
1301/**
1302 * mei_init_module - Driver Registration Routine
1303 *
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.
1306 *
1307 * returns 0 on success, <0 on failure.
1308 */
1309static int __init mei_init_module(void)
1310{
1311 int ret;
1312
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;
1320 }
1321
1322 ret = mei_register_cdev();
1323 if (ret)
1324 goto unregister_pci;
1325
1326 ret = mei_sysfs_device_create();
1327 if (ret)
1328 goto unregister_cdev;
1329
1330 return ret;
1331
1332unregister_cdev:
1333 mei_unregister_cdev();
1334unregister_pci:
1335 pci_unregister_driver(&mei_driver);
1336end:
1337 return ret;
1338}
1339
1340module_init(mei_init_module);
1341
1342/**
1343 * mei_exit_module - Driver Exit Cleanup Routine
1344 *
1345 * mei_exit_module is called just before the driver is removed
1346 * from memory.
1347 */
1348static void __exit mei_exit_module(void)
1349{
ab841160
OW
1350 mei_sysfs_device_remove();
1351 mei_unregister_cdev();
cbecb8bf 1352 pci_unregister_driver(&mei_driver);
ab841160
OW
1353
1354 pr_debug("mei: Driver unloaded successfully.\n");
1355}
1356
1357module_exit(mei_exit_module);
1358
1359
1360MODULE_AUTHOR("Intel Corporation");
1361MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1362MODULE_LICENSE("GPL v2");
1363MODULE_VERSION(MEI_DRIVER_VERSION);