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