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