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