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