blob: 8dcf59d43b0d21b6ef04d3e518a265d0b0f0a094 [file] [log] [blame]
Oren Weilab841162011-05-15 13:43:41 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weilab841162011-05-15 13:43:41 +03005 *
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
Tomas Winkler2f3d2b42012-03-19 22:38:13 +020017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Oren Weilab841162011-05-15 13:43:41 +030019#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>
Oren Weilab841162011-05-15 13:43:41 +030033#include <linux/sched.h>
34#include <linux/uuid.h>
35#include <linux/compat.h>
36#include <linux/jiffies.h>
37#include <linux/interrupt.h>
Oren Weil5b881e32011-11-13 09:41:14 +020038#include <linux/miscdevice.h>
Oren Weilab841162011-05-15 13:43:41 +030039
40#include "mei_dev.h"
Tomas Winkler4f3afe12012-05-09 16:38:59 +030041#include <linux/mei.h>
Oren Weilab841162011-05-15 13:43:41 +030042#include "interface.h"
Oren Weilab841162011-05-15 13:43:41 +030043
Tomas Winklerdaed6b52012-08-17 09:54:23 +030044/* AMT device is a singleton on the platform */
45static struct pci_dev *mei_pdev;
Oren Weilab841162011-05-15 13:43:41 +030046
Oren Weilab841162011-05-15 13:43:41 +030047/* 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)},
Tomas Winkler9af51422012-08-29 01:15:50 +030080 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
Oren Weilab841162011-05-15 13:43:41 +030082
83 /* required last entry */
84 {0, }
85};
86
87MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
88
89static DEFINE_MUTEX(mei_mutex);
90
Oren Weilab841162011-05-15 13:43:41 +030091
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 */
114 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
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 */
119 list_del(&cb_pos->cb_list);
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 */
127 mei_free_cb_private(cb_pos);
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 */
151 mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
152 if (mei_clear_list(dev, file,
153 &dev->amthi_read_complete_list.mei_cb.cb_list))
154 removed = true;
155
156 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
157
158 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
159 removed = true;
160
161 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
162 removed = true;
163
164 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
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 */
172 mei_free_cb_private(dev->iamthif_current_cb);
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{
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200191 struct mei_cl_cb *pos = NULL;
192 struct mei_cl_cb *next = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300193
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200194 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
195 list_for_each_entry_safe(pos, next,
196 &dev->read_list.mei_cb.cb_list, cb_list) {
197 struct mei_cl *cl_temp;
198 cl_temp = (struct mei_cl *)pos->file_private;
Oren Weilab841162011-05-15 13:43:41 +0300199
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200200 if (mei_cl_cmp_id(cl, cl_temp))
201 return pos;
Oren Weilab841162011-05-15 13:43:41 +0300202 }
203 return NULL;
204}
205
206/**
207 * mei_open - the open function
208 *
209 * @inode: pointer to inode structure
210 * @file: pointer to file structure
211 *
212 * returns 0 on success, <0 on error
213 */
214static int mei_open(struct inode *inode, struct file *file)
215{
216 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +0300217 struct mei_device *dev;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200218 unsigned long cl_id;
219 int err;
Oren Weilab841162011-05-15 13:43:41 +0300220
221 err = -ENODEV;
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300222 if (!mei_pdev)
Oren Weilab841162011-05-15 13:43:41 +0300223 goto out;
224
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300225 dev = pci_get_drvdata(mei_pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200226 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +0300227 goto out;
228
229 mutex_lock(&dev->device_lock);
230 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300231 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +0300232 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +0400233 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +0300234
235 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +0300236 if (dev->dev_state != MEI_DEV_ENABLED) {
237 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
238 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +0300239 goto out_unlock;
240 }
241 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +0300242 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
243 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
244 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +0300245 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300246 }
Oren Weilab841162011-05-15 13:43:41 +0300247
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200248 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler1b812942012-09-11 00:43:20 +0300249 if (cl_id >= MEI_CLIENTS_MAX) {
250 dev_err(&dev->pdev->dev, "client_id exceded %d",
251 MEI_CLIENTS_MAX) ;
Oren Weilab841162011-05-15 13:43:41 +0300252 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300253 }
Oren Weilab841162011-05-15 13:43:41 +0300254
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200255 cl->host_client_id = cl_id;
256
Oren Weilab841162011-05-15 13:43:41 +0300257 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
258
259 dev->open_handle_count++;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200260
Oren Weilab841162011-05-15 13:43:41 +0300261 list_add_tail(&cl->link, &dev->file_list);
262
263 set_bit(cl->host_client_id, dev->host_clients_map);
264 cl->state = MEI_FILE_INITIALIZING;
265 cl->sm_state = 0;
266
267 file->private_data = cl;
268 mutex_unlock(&dev->device_lock);
269
Oren Weil5b881e32011-11-13 09:41:14 +0200270 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300271
272out_unlock:
273 mutex_unlock(&dev->device_lock);
274 kfree(cl);
275out:
276 return err;
277}
278
279/**
280 * mei_release - the release function
281 *
282 * @inode: pointer to inode structure
283 * @file: pointer to file structure
284 *
285 * returns 0 on success, <0 on error
286 */
287static int mei_release(struct inode *inode, struct file *file)
288{
289 struct mei_cl *cl = file->private_data;
290 struct mei_cl_cb *cb;
291 struct mei_device *dev;
292 int rets = 0;
293
294 if (WARN_ON(!cl || !cl->dev))
295 return -ENODEV;
296
297 dev = cl->dev;
298
299 mutex_lock(&dev->device_lock);
300 if (cl != &dev->iamthif_cl) {
301 if (cl->state == MEI_FILE_CONNECTED) {
302 cl->state = MEI_FILE_DISCONNECTING;
303 dev_dbg(&dev->pdev->dev,
304 "disconnecting client host client = %d, "
305 "ME client = %d\n",
306 cl->host_client_id,
307 cl->me_client_id);
308 rets = mei_disconnect_host_client(dev, cl);
309 }
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300310 mei_cl_flush_queues(cl);
Oren Weilab841162011-05-15 13:43:41 +0300311 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
312 cl->host_client_id,
313 cl->me_client_id);
314
315 if (dev->open_handle_count > 0) {
Tomas Winkler441ab502011-12-13 23:39:34 +0200316 clear_bit(cl->host_client_id, dev->host_clients_map);
Oren Weilab841162011-05-15 13:43:41 +0300317 dev->open_handle_count--;
318 }
319 mei_remove_client_from_file_list(dev, cl->host_client_id);
320
321 /* free read cb */
322 cb = NULL;
323 if (cl->read_cb) {
324 cb = find_read_list_entry(dev, cl);
325 /* Remove entry from read list */
326 if (cb)
327 list_del(&cb->cb_list);
328
329 cb = cl->read_cb;
330 cl->read_cb = NULL;
331 }
332
333 file->private_data = NULL;
334
335 if (cb) {
336 mei_free_cb_private(cb);
337 cb = NULL;
338 }
339
340 kfree(cl);
341 } else {
342 if (dev->open_handle_count > 0)
343 dev->open_handle_count--;
344
345 if (dev->iamthif_file_object == file &&
346 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
347
348 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
349 dev->iamthif_state);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300350 dev->iamthif_canceled = true;
Oren Weilab841162011-05-15 13:43:41 +0300351 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
352 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
Tomas Winklerc95efb72011-05-25 17:28:21 +0300353 mei_run_next_iamthif_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300354 }
355 }
356
357 if (mei_clear_lists(dev, file))
358 dev->iamthif_state = MEI_IAMTHIF_IDLE;
359
360 }
361 mutex_unlock(&dev->device_lock);
362 return rets;
363}
364
365
366/**
367 * mei_read - the read function.
368 *
369 * @file: pointer to file structure
370 * @ubuf: pointer to user buffer
371 * @length: buffer length
372 * @offset: data offset in buffer
373 *
374 * returns >=0 data length on success , <0 on error
375 */
376static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200377 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300378{
379 struct mei_cl *cl = file->private_data;
380 struct mei_cl_cb *cb_pos = NULL;
381 struct mei_cl_cb *cb = NULL;
382 struct mei_device *dev;
383 int i;
384 int rets;
385 int err;
386
387
388 if (WARN_ON(!cl || !cl->dev))
389 return -ENODEV;
390
391 dev = cl->dev;
392
393 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300394 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300395 rets = -ENODEV;
396 goto out;
397 }
398
399 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
400 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300401 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300402 if (i >= 0) {
403 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300404 if (cl->me_client_id == me_client->client_id) {
405 rets = -EBADF;
406 goto out;
407 }
408 }
409 } else {
410 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
411 }
412
413 if (cl == &dev->iamthif_cl) {
414 rets = amthi_read(dev, file, ubuf, length, offset);
415 goto out;
416 }
417
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200418 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300419 cb = cl->read_cb;
420 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200421 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
422 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300423 cb = cl->read_cb;
424 rets = 0;
425 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200426 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700427 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300428 *offset = 0;
429 rets = 0;
430 goto out;
431 }
432
433 err = mei_start_read(dev, cl);
434 if (err && err != -EBUSY) {
435 dev_dbg(&dev->pdev->dev,
436 "mei start read failure with status = %d\n", err);
437 rets = err;
438 goto out;
439 }
440
441 if (MEI_READ_COMPLETE != cl->reading_state &&
442 !waitqueue_active(&cl->rx_wait)) {
443 if (file->f_flags & O_NONBLOCK) {
444 rets = -EAGAIN;
445 goto out;
446 }
447
448 mutex_unlock(&dev->device_lock);
449
450 if (wait_event_interruptible(cl->rx_wait,
451 (MEI_READ_COMPLETE == cl->reading_state ||
452 MEI_FILE_INITIALIZING == cl->state ||
453 MEI_FILE_DISCONNECTED == cl->state ||
454 MEI_FILE_DISCONNECTING == cl->state))) {
455 if (signal_pending(current))
456 return -EINTR;
457 return -ERESTARTSYS;
458 }
459
460 mutex_lock(&dev->device_lock);
461 if (MEI_FILE_INITIALIZING == cl->state ||
462 MEI_FILE_DISCONNECTED == cl->state ||
463 MEI_FILE_DISCONNECTING == cl->state) {
464 rets = -EBUSY;
465 goto out;
466 }
467 }
468
469 cb = cl->read_cb;
470
471 if (!cb) {
472 rets = -ENODEV;
473 goto out;
474 }
475 if (cl->reading_state != MEI_READ_COMPLETE) {
476 rets = 0;
477 goto out;
478 }
479 /* now copy the data to user space */
480copy_buffer:
481 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
482 cb->response_buffer.size);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200483 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
484 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300485 rets = -EMSGSIZE;
486 goto free;
487 }
488
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200489 /* length is being truncated to PAGE_SIZE,
490 * however buf_idx may point beyond that */
491 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300492
Tomas Winkler441ab502011-12-13 23:39:34 +0200493 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300494 rets = -EFAULT;
495 goto free;
496 }
497
498 rets = length;
499 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200500 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300501 goto out;
502
503free:
504 cb_pos = find_read_list_entry(dev, cl);
505 /* Remove entry from read list */
506 if (cb_pos)
507 list_del(&cb_pos->cb_list);
508 mei_free_cb_private(cb);
509 cl->reading_state = MEI_IDLE;
510 cl->read_cb = NULL;
511 cl->read_pending = 0;
512out:
513 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
514 mutex_unlock(&dev->device_lock);
515 return rets;
516}
517
518/**
Tomas Winkler33d28c92012-10-09 16:50:17 +0200519 * mei_io_cb_init - allocate and initialize io callback
520 *
521 * @cl - mei client
522 * @file: pointer to file structure
523 *
524 * returns mei_cl_cb pointer or NULL;
525 */
526static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
527{
528 struct mei_cl_cb *cb;
529 struct mei_device *dev;
530
531 dev = cl->dev;
532
533 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
534 if (!cb)
535 return NULL;
536
537 INIT_LIST_HEAD(&cb->cb_list);
538
539 cb->file_object = fp;
540 cb->file_private = cl;
541 cb->buf_idx = 0;
542 return cb;
543}
544
545
546/**
547 * mei_io_cb_alloc_req_buf - allocate request buffer
548 *
549 * @cb - io callback structure
550 * @size: size of the buffer
551 *
552 * returns 0 on success
553 * -EINVAL if cb is NULL
554 * -ENOMEM if allocation failed
555 */
556static int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
557{
558 if (!cb)
559 return -EINVAL;
560
561 if (length == 0)
562 return 0;
563
564 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
565 if (!cb->request_buffer.data)
566 return -ENOMEM;
567 cb->request_buffer.size = length;
568 return 0;
569}
570/**
571 * mei_io_cb_alloc_req_buf - allocate respose buffer
572 *
573 * @cb - io callback structure
574 * @size: size of the buffer
575 *
576 * returns 0 on success
577 * -EINVAL if cb is NULL
578 * -ENOMEM if allocation failed
579 */
580static int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
581{
582 if (!cb)
583 return -EINVAL;
584
585 if (length == 0)
586 return 0;
587
588 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
589 if (!cb->response_buffer.data)
590 return -ENOMEM;
591 cb->response_buffer.size = length;
592 return 0;
593}
594
595/**
Oren Weilab841162011-05-15 13:43:41 +0300596 * mei_write - the write function.
597 *
598 * @file: pointer to file structure
599 * @ubuf: pointer to user buffer
600 * @length: buffer length
601 * @offset: data offset in buffer
602 *
603 * returns >=0 data length on success , <0 on error
604 */
605static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200606 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300607{
608 struct mei_cl *cl = file->private_data;
609 struct mei_cl_cb *write_cb = NULL;
610 struct mei_msg_hdr mei_hdr;
611 struct mei_device *dev;
612 unsigned long timeout = 0;
613 int rets;
614 int i;
615
616 if (WARN_ON(!cl || !cl->dev))
617 return -ENODEV;
618
619 dev = cl->dev;
620
621 mutex_lock(&dev->device_lock);
622
Tomas Winklerb210d752012-08-07 00:03:56 +0300623 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200624 rets = -ENODEV;
625 goto unlock_dev;
Oren Weilab841162011-05-15 13:43:41 +0300626 }
627
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200628 i = mei_me_cl_by_id(dev, cl->me_client_id);
629 if (i < 0) {
630 rets = -ENODEV;
631 goto unlock_dev;
632 }
633 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
634 rets = -EMSGSIZE;
635 goto unlock_dev;
636 }
637
638 if (cl->state != MEI_FILE_CONNECTED) {
639 rets = -ENODEV;
640 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
641 cl->host_client_id, cl->me_client_id);
642 goto unlock_dev;
643 }
Oren Weilab841162011-05-15 13:43:41 +0300644 if (cl == &dev->iamthif_cl) {
645 write_cb = find_amthi_read_list_entry(dev, file);
646
647 if (write_cb) {
648 timeout = write_cb->read_time +
649 msecs_to_jiffies(IAMTHIF_READ_TIMER);
650
651 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200652 cl->reading_state == MEI_READ_COMPLETE) {
653 *offset = 0;
654 list_del(&write_cb->cb_list);
655 mei_free_cb_private(write_cb);
656 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300657 }
658 }
659 }
660
661 /* free entry used in read */
662 if (cl->reading_state == MEI_READ_COMPLETE) {
663 *offset = 0;
664 write_cb = find_read_list_entry(dev, cl);
665 if (write_cb) {
666 list_del(&write_cb->cb_list);
667 mei_free_cb_private(write_cb);
668 write_cb = NULL;
669 cl->reading_state = MEI_IDLE;
670 cl->read_cb = NULL;
671 cl->read_pending = 0;
672 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200673 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
Oren Weilab841162011-05-15 13:43:41 +0300674 *offset = 0;
675
676
Tomas Winkler33d28c92012-10-09 16:50:17 +0200677 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300678 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200679 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
680 rets = -ENOMEM;
681 goto unlock_dev;
Oren Weilab841162011-05-15 13:43:41 +0300682 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200683 rets = mei_io_cb_alloc_req_buf(write_cb, length);
684 if (rets)
Oren Weilab841162011-05-15 13:43:41 +0300685 goto unlock_dev;
686
Tomas Winkler33d28c92012-10-09 16:50:17 +0200687 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
Oren Weilab841162011-05-15 13:43:41 +0300688
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200689 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
690 if (rets)
Oren Weilab841162011-05-15 13:43:41 +0300691 goto unlock_dev;
692
693 cl->sm_state = 0;
694 if (length == 4 &&
695 ((memcmp(mei_wd_state_independence_msg[0],
696 write_cb->request_buffer.data, 4) == 0) ||
697 (memcmp(mei_wd_state_independence_msg[1],
698 write_cb->request_buffer.data, 4) == 0) ||
699 (memcmp(mei_wd_state_independence_msg[2],
700 write_cb->request_buffer.data, 4) == 0)))
701 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
702
Oren Weilab841162011-05-15 13:43:41 +0300703 if (cl == &dev->iamthif_cl) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200704 rets = mei_io_cb_alloc_resp_buf(write_cb, dev->iamthif_mtu);
705 if (rets)
706 goto unlock_dev;
Oren Weilab841162011-05-15 13:43:41 +0300707
Oren Weilab841162011-05-15 13:43:41 +0300708 write_cb->major_file_operations = MEI_IOCTL;
Oren Weilab841162011-05-15 13:43:41 +0300709
710 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
711 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
712 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
713 (int) dev->iamthif_state);
714 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
715 list_add_tail(&write_cb->cb_list,
716 &dev->amthi_cmd_list.mei_cb.cb_list);
Oren Weilab841162011-05-15 13:43:41 +0300717 } else {
718 dev_dbg(&dev->pdev->dev, "call amthi write\n");
719 rets = amthi_write(dev, write_cb);
720
721 if (rets) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200722 dev_err(&dev->pdev->dev, "amthi write failed with status = %d\n",
Oren Weilab841162011-05-15 13:43:41 +0300723 rets);
724 goto unlock_dev;
725 }
Oren Weilab841162011-05-15 13:43:41 +0300726 }
727 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200728 return length;
Oren Weilab841162011-05-15 13:43:41 +0300729 }
730
731 write_cb->major_file_operations = MEI_WRITE;
Oren Weilab841162011-05-15 13:43:41 +0300732
733 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
734 cl->host_client_id, cl->me_client_id);
Oren Weilab841162011-05-15 13:43:41 +0300735 rets = mei_flow_ctrl_creds(dev, cl);
736 if (rets < 0)
737 goto unlock_dev;
738
739 if (rets && dev->mei_host_buffer_is_empty) {
740 rets = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300741 dev->mei_host_buffer_is_empty = false;
Tomas Winkler24aadc82012-06-25 23:46:27 +0300742 if (length > mei_hbuf_max_data(dev)) {
743 mei_hdr.length = mei_hbuf_max_data(dev);
Oren Weilab841162011-05-15 13:43:41 +0300744 mei_hdr.msg_complete = 0;
745 } else {
746 mei_hdr.length = length;
747 mei_hdr.msg_complete = 1;
748 }
749 mei_hdr.host_addr = cl->host_client_id;
750 mei_hdr.me_addr = cl->me_client_id;
751 mei_hdr.reserved = 0;
752 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
753 *((u32 *) &mei_hdr));
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200754 if (mei_write_message(dev, &mei_hdr,
Oren Weilab841162011-05-15 13:43:41 +0300755 (unsigned char *) (write_cb->request_buffer.data),
756 mei_hdr.length)) {
757 rets = -ENODEV;
758 goto unlock_dev;
759 }
760 cl->writing_state = MEI_WRITING;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200761 write_cb->buf_idx = mei_hdr.length;
Oren Weilab841162011-05-15 13:43:41 +0300762 if (mei_hdr.msg_complete) {
763 if (mei_flow_ctrl_reduce(dev, cl)) {
764 rets = -ENODEV;
765 goto unlock_dev;
766 }
767 list_add_tail(&write_cb->cb_list,
768 &dev->write_waiting_list.mei_cb.cb_list);
769 } else {
770 list_add_tail(&write_cb->cb_list,
771 &dev->write_list.mei_cb.cb_list);
772 }
773
774 } else {
775
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200776 write_cb->buf_idx = 0;
Oren Weilab841162011-05-15 13:43:41 +0300777 cl->writing_state = MEI_WRITING;
778 list_add_tail(&write_cb->cb_list,
779 &dev->write_list.mei_cb.cb_list);
780 }
781 mutex_unlock(&dev->device_lock);
782 return length;
783
784unlock_dev:
785 mutex_unlock(&dev->device_lock);
786 mei_free_cb_private(write_cb);
787 return rets;
788}
789
790
791/**
792 * mei_ioctl - the IOCTL function
793 *
794 * @file: pointer to file structure
795 * @cmd: ioctl command
796 * @data: pointer to mei message structure
797 *
798 * returns 0 on success , <0 on error
799 */
800static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
801{
802 struct mei_device *dev;
803 struct mei_cl *cl = file->private_data;
804 struct mei_connect_client_data *connect_data = NULL;
805 int rets;
806
807 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
808 return -EINVAL;
809
810 if (WARN_ON(!cl || !cl->dev))
811 return -ENODEV;
812
813 dev = cl->dev;
814
815 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
816
817 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300818 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300819 rets = -ENODEV;
820 goto out;
821 }
822
823 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
824
825 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
826 GFP_KERNEL);
827 if (!connect_data) {
828 rets = -ENOMEM;
829 goto out;
830 }
831 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
832 if (copy_from_user(connect_data, (char __user *)data,
833 sizeof(struct mei_connect_client_data))) {
834 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
835 rets = -EFAULT;
836 goto out;
837 }
838 rets = mei_ioctl_connect_client(file, connect_data);
839
840 /* if all is ok, copying the data back to user. */
841 if (rets)
842 goto out;
843
844 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
845 if (copy_to_user((char __user *)data, connect_data,
846 sizeof(struct mei_connect_client_data))) {
847 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
848 rets = -EFAULT;
849 goto out;
850 }
851
852out:
853 kfree(connect_data);
854 mutex_unlock(&dev->device_lock);
855 return rets;
856}
857
858/**
859 * mei_compat_ioctl - the compat IOCTL function
860 *
861 * @file: pointer to file structure
862 * @cmd: ioctl command
863 * @data: pointer to mei message structure
864 *
865 * returns 0 on success , <0 on error
866 */
867#ifdef CONFIG_COMPAT
868static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200869 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300870{
871 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
872}
873#endif
874
875
876/**
877 * mei_poll - the poll function
878 *
879 * @file: pointer to file structure
880 * @wait: pointer to poll_table structure
881 *
882 * returns poll mask
883 */
884static unsigned int mei_poll(struct file *file, poll_table *wait)
885{
886 struct mei_cl *cl = file->private_data;
887 struct mei_device *dev;
888 unsigned int mask = 0;
889
890 if (WARN_ON(!cl || !cl->dev))
891 return mask;
892
893 dev = cl->dev;
894
895 mutex_lock(&dev->device_lock);
896
Tomas Winklerb210d752012-08-07 00:03:56 +0300897 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300898 goto out;
899
900
901 if (cl == &dev->iamthif_cl) {
902 mutex_unlock(&dev->device_lock);
903 poll_wait(file, &dev->iamthif_cl.wait, wait);
904 mutex_lock(&dev->device_lock);
905 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
906 dev->iamthif_file_object == file) {
907 mask |= (POLLIN | POLLRDNORM);
908 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
Tomas Winklerc95efb72011-05-25 17:28:21 +0300909 mei_run_next_iamthif_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300910 }
911 goto out;
912 }
913
914 mutex_unlock(&dev->device_lock);
915 poll_wait(file, &cl->tx_wait, wait);
916 mutex_lock(&dev->device_lock);
917 if (MEI_WRITE_COMPLETE == cl->writing_state)
918 mask |= (POLLIN | POLLRDNORM);
919
920out:
921 mutex_unlock(&dev->device_lock);
922 return mask;
923}
924
Oren Weil5b881e32011-11-13 09:41:14 +0200925/*
926 * file operations structure will be used for mei char device.
927 */
928static const struct file_operations mei_fops = {
929 .owner = THIS_MODULE,
930 .read = mei_read,
931 .unlocked_ioctl = mei_ioctl,
932#ifdef CONFIG_COMPAT
933 .compat_ioctl = mei_compat_ioctl,
934#endif
935 .open = mei_open,
936 .release = mei_release,
937 .write = mei_write,
938 .poll = mei_poll,
939 .llseek = no_llseek
940};
941
942
943/*
944 * Misc Device Struct
945 */
946static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300947 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200948 .fops = &mei_fops,
949 .minor = MISC_DYNAMIC_MINOR,
950};
951
952/**
Tomas Winkler9a123f12012-08-06 15:23:55 +0300953 * mei_quirk_probe - probe for devices that doesn't valid ME interface
954 * @pdev: PCI device structure
955 * @ent: entry into pci_device_table
956 *
957 * returns true if ME Interface is valid, false otherwise
958 */
959static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
960 const struct pci_device_id *ent)
961{
962 u32 reg;
963 if (ent->device == MEI_DEV_ID_PBG_1) {
964 pci_read_config_dword(pdev, 0x48, &reg);
965 /* make sure that bit 9 is up and bit 10 is down */
966 if ((reg & 0x600) == 0x200) {
967 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
968 return false;
969 }
970 }
971 return true;
972}
973/**
Oren Weil5b881e32011-11-13 09:41:14 +0200974 * mei_probe - Device Initialization Routine
975 *
976 * @pdev: PCI device structure
977 * @ent: entry in kcs_pci_tbl
978 *
979 * returns 0 on success, <0 on failure.
980 */
981static int __devinit mei_probe(struct pci_dev *pdev,
982 const struct pci_device_id *ent)
983{
984 struct mei_device *dev;
985 int err;
986
987 mutex_lock(&mei_mutex);
Tomas Winkler9a123f12012-08-06 15:23:55 +0300988
989 if (!mei_quirk_probe(pdev, ent)) {
990 err = -ENODEV;
991 goto end;
992 }
993
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300994 if (mei_pdev) {
Oren Weil5b881e32011-11-13 09:41:14 +0200995 err = -EEXIST;
996 goto end;
997 }
998 /* enable pci dev */
999 err = pci_enable_device(pdev);
1000 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001001 dev_err(&pdev->dev, "failed to enable pci device.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001002 goto end;
1003 }
1004 /* set PCI host mastering */
1005 pci_set_master(pdev);
1006 /* pci request regions for mei driver */
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001007 err = pci_request_regions(pdev, KBUILD_MODNAME);
Oren Weil5b881e32011-11-13 09:41:14 +02001008 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001009 dev_err(&pdev->dev, "failed to get pci regions.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001010 goto disable_device;
1011 }
1012 /* allocates and initializes the mei dev structure */
1013 dev = mei_device_init(pdev);
1014 if (!dev) {
1015 err = -ENOMEM;
1016 goto release_regions;
1017 }
1018 /* mapping IO device memory */
1019 dev->mem_addr = pci_iomap(pdev, 0, 0);
1020 if (!dev->mem_addr) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001021 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001022 err = -ENOMEM;
1023 goto free_device;
1024 }
1025 pci_enable_msi(pdev);
1026
1027 /* request and enable interrupt */
1028 if (pci_dev_msi_enabled(pdev))
1029 err = request_threaded_irq(pdev->irq,
1030 NULL,
1031 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001032 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001033 else
1034 err = request_threaded_irq(pdev->irq,
1035 mei_interrupt_quick_handler,
1036 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001037 IRQF_SHARED, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001038
1039 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001040 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
Oren Weil5b881e32011-11-13 09:41:14 +02001041 pdev->irq);
Samuel Ortiz169dc382012-06-11 12:18:30 +03001042 goto disable_msi;
Oren Weil5b881e32011-11-13 09:41:14 +02001043 }
1044 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
1045 if (mei_hw_init(dev)) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001046 dev_err(&pdev->dev, "init hw failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001047 err = -ENODEV;
1048 goto release_irq;
1049 }
1050
1051 err = misc_register(&mei_misc_device);
1052 if (err)
1053 goto release_irq;
1054
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001055 mei_pdev = pdev;
Oren Weil5b881e32011-11-13 09:41:14 +02001056 pci_set_drvdata(pdev, dev);
1057
1058
1059 schedule_delayed_work(&dev->timer_work, HZ);
1060
1061 mutex_unlock(&mei_mutex);
1062
Tomas Winkler2f3d2b42012-03-19 22:38:13 +02001063 pr_debug("initialization successful.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001064
1065 return 0;
1066
1067release_irq:
1068 /* disable interrupts */
1069 dev->host_hw_state = mei_hcsr_read(dev);
1070 mei_disable_interrupts(dev);
1071 flush_scheduled_work();
1072 free_irq(pdev->irq, dev);
Samuel Ortiz169dc382012-06-11 12:18:30 +03001073disable_msi:
Oren Weil5b881e32011-11-13 09:41:14 +02001074 pci_disable_msi(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +02001075 pci_iounmap(pdev, dev->mem_addr);
1076free_device:
1077 kfree(dev);
1078release_regions:
1079 pci_release_regions(pdev);
1080disable_device:
1081 pci_disable_device(pdev);
1082end:
1083 mutex_unlock(&mei_mutex);
Tomas Winkler32c826b2012-05-08 23:04:56 +03001084 dev_err(&pdev->dev, "initialization failed.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001085 return err;
1086}
1087
1088/**
1089 * mei_remove - Device Removal Routine
1090 *
1091 * @pdev: PCI device structure
1092 *
1093 * mei_remove is called by the PCI subsystem to alert the driver
1094 * that it should release a PCI device.
1095 */
1096static void __devexit mei_remove(struct pci_dev *pdev)
1097{
1098 struct mei_device *dev;
1099
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001100 if (mei_pdev != pdev)
Oren Weil5b881e32011-11-13 09:41:14 +02001101 return;
1102
1103 dev = pci_get_drvdata(pdev);
1104 if (!dev)
1105 return;
1106
1107 mutex_lock(&dev->device_lock);
1108
Tomas Winklerc216fde2012-08-16 19:39:43 +03001109 cancel_delayed_work(&dev->timer_work);
1110
1111 mei_wd_stop(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001112
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001113 mei_pdev = NULL;
Oren Weil5b881e32011-11-13 09:41:14 +02001114
1115 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1116 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1117 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1118 }
1119 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1120 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1121 mei_disconnect_host_client(dev, &dev->wd_cl);
1122 }
1123
1124 /* Unregistering watchdog device */
Tomas Winkler70cd5332011-12-22 18:50:50 +02001125 mei_watchdog_unregister(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001126
1127 /* remove entry if already in list */
1128 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1129 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1130 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1131
1132 dev->iamthif_current_cb = NULL;
1133 dev->me_clients_num = 0;
1134
1135 mutex_unlock(&dev->device_lock);
1136
1137 flush_scheduled_work();
1138
1139 /* disable interrupts */
1140 mei_disable_interrupts(dev);
1141
1142 free_irq(pdev->irq, dev);
1143 pci_disable_msi(pdev);
1144 pci_set_drvdata(pdev, NULL);
1145
1146 if (dev->mem_addr)
1147 pci_iounmap(pdev, dev->mem_addr);
1148
1149 kfree(dev);
1150
1151 pci_release_regions(pdev);
1152 pci_disable_device(pdev);
Tomas Winklera44cab42012-05-29 16:39:11 +03001153
1154 misc_deregister(&mei_misc_device);
Oren Weil5b881e32011-11-13 09:41:14 +02001155}
Oren Weilab841162011-05-15 13:43:41 +03001156#ifdef CONFIG_PM
1157static int mei_pci_suspend(struct device *device)
1158{
1159 struct pci_dev *pdev = to_pci_dev(device);
1160 struct mei_device *dev = pci_get_drvdata(pdev);
1161 int err;
1162
1163 if (!dev)
1164 return -ENODEV;
1165 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +03001166
1167 cancel_delayed_work(&dev->timer_work);
1168
Oren Weilab841162011-05-15 13:43:41 +03001169 /* Stop watchdog if exists */
Tomas Winklerc216fde2012-08-16 19:39:43 +03001170 err = mei_wd_stop(dev);
Oren Weilab841162011-05-15 13:43:41 +03001171 /* Set new mei state */
Tomas Winklerb210d752012-08-07 00:03:56 +03001172 if (dev->dev_state == MEI_DEV_ENABLED ||
1173 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1174 dev->dev_state = MEI_DEV_POWER_DOWN;
Oren Weilab841162011-05-15 13:43:41 +03001175 mei_reset(dev, 0);
1176 }
1177 mutex_unlock(&dev->device_lock);
1178
1179 free_irq(pdev->irq, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001180 pci_disable_msi(pdev);
Oren Weilab841162011-05-15 13:43:41 +03001181
1182 return err;
1183}
1184
1185static int mei_pci_resume(struct device *device)
1186{
1187 struct pci_dev *pdev = to_pci_dev(device);
1188 struct mei_device *dev;
1189 int err;
1190
1191 dev = pci_get_drvdata(pdev);
1192 if (!dev)
1193 return -ENODEV;
1194
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001195 pci_enable_msi(pdev);
1196
1197 /* request and enable interrupt */
1198 if (pci_dev_msi_enabled(pdev))
1199 err = request_threaded_irq(pdev->irq,
1200 NULL,
1201 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001202 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001203 else
1204 err = request_threaded_irq(pdev->irq,
Oren Weilab841162011-05-15 13:43:41 +03001205 mei_interrupt_quick_handler,
1206 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001207 IRQF_SHARED, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001208
Oren Weilab841162011-05-15 13:43:41 +03001209 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001210 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1211 pdev->irq);
Oren Weilab841162011-05-15 13:43:41 +03001212 return err;
1213 }
1214
1215 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001216 dev->dev_state = MEI_DEV_POWER_UP;
Oren Weilab841162011-05-15 13:43:41 +03001217 mei_reset(dev, 1);
1218 mutex_unlock(&dev->device_lock);
1219
Oren Weil6d70e932011-09-07 09:03:14 +03001220 /* Start timer if stopped in suspend */
1221 schedule_delayed_work(&dev->timer_work, HZ);
1222
Oren Weilab841162011-05-15 13:43:41 +03001223 return err;
1224}
1225static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1226#define MEI_PM_OPS (&mei_pm_ops)
1227#else
Randy Dunlap2d990362011-05-19 08:52:34 -07001228#define MEI_PM_OPS NULL
Oren Weilab841162011-05-15 13:43:41 +03001229#endif /* CONFIG_PM */
1230/*
1231 * PCI driver structure
1232 */
1233static struct pci_driver mei_driver = {
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001234 .name = KBUILD_MODNAME,
Oren Weilab841162011-05-15 13:43:41 +03001235 .id_table = mei_pci_tbl,
1236 .probe = mei_probe,
1237 .remove = __devexit_p(mei_remove),
1238 .shutdown = __devexit_p(mei_remove),
1239 .driver.pm = MEI_PM_OPS,
1240};
1241
Tomas Winkler60781882012-07-19 09:45:32 +03001242module_pci_driver(mei_driver);
Oren Weilab841162011-05-15 13:43:41 +03001243
1244MODULE_AUTHOR("Intel Corporation");
1245MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1246MODULE_LICENSE("GPL v2");