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