blob: e0e39c498d12c77c01274242781abf057b1d7e46 [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,
Tomas Winkler15ea1912012-11-01 21:17:16 +0200106 const struct file *file, struct list_head *mei_cb_list)
Oren Weilab841162011-05-15 13:43:41 +0300107{
108 struct mei_cl_cb *cb_pos = NULL;
109 struct mei_cl_cb *cb_next = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300110 bool removed = false;
111
112 /* list all list member */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200113 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
Oren Weilab841162011-05-15 13:43:41 +0300114 /* check if list member associated with a file */
Tomas Winkler15ea1912012-11-01 21:17:16 +0200115 if (file == cb_pos->file_object) {
Oren Weilab841162011-05-15 13:43:41 +0300116 /* remove member from the list */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200117 list_del(&cb_pos->list);
Oren Weilab841162011-05-15 13:43:41 +0300118 /* 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 */
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200125 mei_io_cb_free(cb_pos);
Oren Weilab841162011-05-15 13:43:41 +0300126 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 */
Tomas Winklere773efc2012-11-11 17:37:58 +0200149 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
150 if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300151 removed = true;
152
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200153 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300154
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200155 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300156 removed = true;
157
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200158 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300159 removed = true;
160
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200161 if (mei_clear_list(dev, file, &dev->write_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300162 removed = true;
163
164 /* check if iamthif_current_cb not NULL */
165 if (dev->iamthif_current_cb && !removed) {
166 /* check file and iamthif current cb association */
167 if (dev->iamthif_current_cb->file_object == file) {
168 /* remove cb */
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200169 mei_io_cb_free(dev->iamthif_current_cb);
Oren Weilab841162011-05-15 13:43:41 +0300170 dev->iamthif_current_cb = NULL;
171 removed = true;
172 }
173 }
174 return removed;
175}
176/**
177 * find_read_list_entry - find read list entry
178 *
179 * @dev: device structure
180 * @file: pointer to file structure
181 *
182 * returns cb on success, NULL on error
183 */
184static struct mei_cl_cb *find_read_list_entry(
185 struct mei_device *dev,
186 struct mei_cl *cl)
187{
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200188 struct mei_cl_cb *pos = NULL;
189 struct mei_cl_cb *next = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300190
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200191 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
Tomas Winklerdb3ed432012-11-11 17:37:59 +0200192 list_for_each_entry_safe(pos, next, &dev->read_list.list, list)
193 if (mei_cl_cmp_id(cl, pos->cl))
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200194 return pos;
Oren Weilab841162011-05-15 13:43:41 +0300195 return NULL;
196}
197
198/**
199 * mei_open - the open function
200 *
201 * @inode: pointer to inode structure
202 * @file: pointer to file structure
203 *
204 * returns 0 on success, <0 on error
205 */
206static int mei_open(struct inode *inode, struct file *file)
207{
208 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +0300209 struct mei_device *dev;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200210 unsigned long cl_id;
211 int err;
Oren Weilab841162011-05-15 13:43:41 +0300212
213 err = -ENODEV;
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300214 if (!mei_pdev)
Oren Weilab841162011-05-15 13:43:41 +0300215 goto out;
216
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300217 dev = pci_get_drvdata(mei_pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200218 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +0300219 goto out;
220
221 mutex_lock(&dev->device_lock);
222 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300223 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +0300224 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +0400225 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +0300226
227 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +0300228 if (dev->dev_state != MEI_DEV_ENABLED) {
229 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
230 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +0300231 goto out_unlock;
232 }
233 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +0300234 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
235 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
236 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +0300237 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300238 }
Oren Weilab841162011-05-15 13:43:41 +0300239
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200240 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler1b812942012-09-11 00:43:20 +0300241 if (cl_id >= MEI_CLIENTS_MAX) {
242 dev_err(&dev->pdev->dev, "client_id exceded %d",
243 MEI_CLIENTS_MAX) ;
Oren Weilab841162011-05-15 13:43:41 +0300244 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300245 }
Oren Weilab841162011-05-15 13:43:41 +0300246
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200247 cl->host_client_id = cl_id;
248
Oren Weilab841162011-05-15 13:43:41 +0300249 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
250
251 dev->open_handle_count++;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200252
Oren Weilab841162011-05-15 13:43:41 +0300253 list_add_tail(&cl->link, &dev->file_list);
254
255 set_bit(cl->host_client_id, dev->host_clients_map);
256 cl->state = MEI_FILE_INITIALIZING;
257 cl->sm_state = 0;
258
259 file->private_data = cl;
260 mutex_unlock(&dev->device_lock);
261
Oren Weil5b881e32011-11-13 09:41:14 +0200262 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300263
264out_unlock:
265 mutex_unlock(&dev->device_lock);
266 kfree(cl);
267out:
268 return err;
269}
270
271/**
272 * mei_release - the release function
273 *
274 * @inode: pointer to inode structure
275 * @file: pointer to file structure
276 *
277 * returns 0 on success, <0 on error
278 */
279static int mei_release(struct inode *inode, struct file *file)
280{
281 struct mei_cl *cl = file->private_data;
282 struct mei_cl_cb *cb;
283 struct mei_device *dev;
284 int rets = 0;
285
286 if (WARN_ON(!cl || !cl->dev))
287 return -ENODEV;
288
289 dev = cl->dev;
290
291 mutex_lock(&dev->device_lock);
292 if (cl != &dev->iamthif_cl) {
293 if (cl->state == MEI_FILE_CONNECTED) {
294 cl->state = MEI_FILE_DISCONNECTING;
295 dev_dbg(&dev->pdev->dev,
296 "disconnecting client host client = %d, "
297 "ME client = %d\n",
298 cl->host_client_id,
299 cl->me_client_id);
300 rets = mei_disconnect_host_client(dev, cl);
301 }
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300302 mei_cl_flush_queues(cl);
Oren Weilab841162011-05-15 13:43:41 +0300303 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
304 cl->host_client_id,
305 cl->me_client_id);
306
307 if (dev->open_handle_count > 0) {
Tomas Winkler441ab502011-12-13 23:39:34 +0200308 clear_bit(cl->host_client_id, dev->host_clients_map);
Oren Weilab841162011-05-15 13:43:41 +0300309 dev->open_handle_count--;
310 }
311 mei_remove_client_from_file_list(dev, cl->host_client_id);
312
313 /* free read cb */
314 cb = NULL;
315 if (cl->read_cb) {
316 cb = find_read_list_entry(dev, cl);
317 /* Remove entry from read list */
318 if (cb)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200319 list_del(&cb->list);
Oren Weilab841162011-05-15 13:43:41 +0300320
321 cb = cl->read_cb;
322 cl->read_cb = NULL;
323 }
324
325 file->private_data = NULL;
326
327 if (cb) {
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200328 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300329 cb = NULL;
330 }
331
332 kfree(cl);
333 } else {
334 if (dev->open_handle_count > 0)
335 dev->open_handle_count--;
336
337 if (dev->iamthif_file_object == file &&
338 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
339
340 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
341 dev->iamthif_state);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300342 dev->iamthif_canceled = true;
Oren Weilab841162011-05-15 13:43:41 +0300343 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
344 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200345 mei_amthif_run_next_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300346 }
347 }
348
349 if (mei_clear_lists(dev, file))
350 dev->iamthif_state = MEI_IAMTHIF_IDLE;
351
352 }
353 mutex_unlock(&dev->device_lock);
354 return rets;
355}
356
357
358/**
359 * mei_read - the read function.
360 *
361 * @file: pointer to file structure
362 * @ubuf: pointer to user buffer
363 * @length: buffer length
364 * @offset: data offset in buffer
365 *
366 * returns >=0 data length on success , <0 on error
367 */
368static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200369 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300370{
371 struct mei_cl *cl = file->private_data;
372 struct mei_cl_cb *cb_pos = NULL;
373 struct mei_cl_cb *cb = NULL;
374 struct mei_device *dev;
375 int i;
376 int rets;
377 int err;
378
379
380 if (WARN_ON(!cl || !cl->dev))
381 return -ENODEV;
382
383 dev = cl->dev;
384
385 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300386 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300387 rets = -ENODEV;
388 goto out;
389 }
390
391 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
392 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300393 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300394 if (i >= 0) {
395 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300396 if (cl->me_client_id == me_client->client_id) {
397 rets = -EBADF;
398 goto out;
399 }
400 }
401 } else {
402 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
403 }
404
405 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200406 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300407 goto out;
408 }
409
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200410 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300411 cb = cl->read_cb;
412 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200413 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
414 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300415 cb = cl->read_cb;
416 rets = 0;
417 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200418 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700419 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300420 *offset = 0;
421 rets = 0;
422 goto out;
423 }
424
425 err = mei_start_read(dev, cl);
426 if (err && err != -EBUSY) {
427 dev_dbg(&dev->pdev->dev,
428 "mei start read failure with status = %d\n", err);
429 rets = err;
430 goto out;
431 }
432
433 if (MEI_READ_COMPLETE != cl->reading_state &&
434 !waitqueue_active(&cl->rx_wait)) {
435 if (file->f_flags & O_NONBLOCK) {
436 rets = -EAGAIN;
437 goto out;
438 }
439
440 mutex_unlock(&dev->device_lock);
441
442 if (wait_event_interruptible(cl->rx_wait,
443 (MEI_READ_COMPLETE == cl->reading_state ||
444 MEI_FILE_INITIALIZING == cl->state ||
445 MEI_FILE_DISCONNECTED == cl->state ||
446 MEI_FILE_DISCONNECTING == cl->state))) {
447 if (signal_pending(current))
448 return -EINTR;
449 return -ERESTARTSYS;
450 }
451
452 mutex_lock(&dev->device_lock);
453 if (MEI_FILE_INITIALIZING == cl->state ||
454 MEI_FILE_DISCONNECTED == cl->state ||
455 MEI_FILE_DISCONNECTING == cl->state) {
456 rets = -EBUSY;
457 goto out;
458 }
459 }
460
461 cb = cl->read_cb;
462
463 if (!cb) {
464 rets = -ENODEV;
465 goto out;
466 }
467 if (cl->reading_state != MEI_READ_COMPLETE) {
468 rets = 0;
469 goto out;
470 }
471 /* now copy the data to user space */
472copy_buffer:
473 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
474 cb->response_buffer.size);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200475 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
476 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300477 rets = -EMSGSIZE;
478 goto free;
479 }
480
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200481 /* length is being truncated to PAGE_SIZE,
482 * however buf_idx may point beyond that */
483 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300484
Tomas Winkler441ab502011-12-13 23:39:34 +0200485 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300486 rets = -EFAULT;
487 goto free;
488 }
489
490 rets = length;
491 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200492 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300493 goto out;
494
495free:
496 cb_pos = find_read_list_entry(dev, cl);
497 /* Remove entry from read list */
498 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200499 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200500 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300501 cl->reading_state = MEI_IDLE;
502 cl->read_cb = NULL;
503 cl->read_pending = 0;
504out:
505 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
506 mutex_unlock(&dev->device_lock);
507 return rets;
508}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200509/**
Oren Weilab841162011-05-15 13:43:41 +0300510 * mei_write - the write function.
511 *
512 * @file: pointer to file structure
513 * @ubuf: pointer to user buffer
514 * @length: buffer length
515 * @offset: data offset in buffer
516 *
517 * returns >=0 data length on success , <0 on error
518 */
519static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200520 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300521{
522 struct mei_cl *cl = file->private_data;
523 struct mei_cl_cb *write_cb = NULL;
524 struct mei_msg_hdr mei_hdr;
525 struct mei_device *dev;
526 unsigned long timeout = 0;
527 int rets;
528 int i;
529
530 if (WARN_ON(!cl || !cl->dev))
531 return -ENODEV;
532
533 dev = cl->dev;
534
535 mutex_lock(&dev->device_lock);
536
Tomas Winklerb210d752012-08-07 00:03:56 +0300537 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200538 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200539 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300540 }
541
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200542 i = mei_me_cl_by_id(dev, cl->me_client_id);
543 if (i < 0) {
544 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200545 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200546 }
547 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
548 rets = -EMSGSIZE;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200549 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200550 }
551
552 if (cl->state != MEI_FILE_CONNECTED) {
553 rets = -ENODEV;
554 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
555 cl->host_client_id, cl->me_client_id);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200556 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200557 }
Oren Weilab841162011-05-15 13:43:41 +0300558 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200559 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300560
561 if (write_cb) {
562 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200563 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300564
565 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200566 cl->reading_state == MEI_READ_COMPLETE) {
567 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200568 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200569 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200570 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300571 }
572 }
573 }
574
575 /* free entry used in read */
576 if (cl->reading_state == MEI_READ_COMPLETE) {
577 *offset = 0;
578 write_cb = find_read_list_entry(dev, cl);
579 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200580 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200581 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300582 write_cb = NULL;
583 cl->reading_state = MEI_IDLE;
584 cl->read_cb = NULL;
585 cl->read_pending = 0;
586 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200587 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
Oren Weilab841162011-05-15 13:43:41 +0300588 *offset = 0;
589
590
Tomas Winkler33d28c92012-10-09 16:50:17 +0200591 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300592 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200593 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
594 rets = -ENOMEM;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200595 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300596 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200597 rets = mei_io_cb_alloc_req_buf(write_cb, length);
598 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200599 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300600
Tomas Winkler33d28c92012-10-09 16:50:17 +0200601 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
Oren Weilab841162011-05-15 13:43:41 +0300602
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200603 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
604 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200605 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300606
607 cl->sm_state = 0;
608 if (length == 4 &&
609 ((memcmp(mei_wd_state_independence_msg[0],
610 write_cb->request_buffer.data, 4) == 0) ||
611 (memcmp(mei_wd_state_independence_msg[1],
612 write_cb->request_buffer.data, 4) == 0) ||
613 (memcmp(mei_wd_state_independence_msg[2],
614 write_cb->request_buffer.data, 4) == 0)))
615 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
616
Oren Weilab841162011-05-15 13:43:41 +0300617 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200618 rets = mei_amthif_write(dev, write_cb);
619
620 if (rets) {
621 dev_err(&dev->pdev->dev,
622 "amthi write failed with status = %d\n", rets);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200623 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300624 }
625 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200626 return length;
Oren Weilab841162011-05-15 13:43:41 +0300627 }
628
629 write_cb->major_file_operations = MEI_WRITE;
Oren Weilab841162011-05-15 13:43:41 +0300630
631 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
632 cl->host_client_id, cl->me_client_id);
Oren Weilab841162011-05-15 13:43:41 +0300633 rets = mei_flow_ctrl_creds(dev, cl);
634 if (rets < 0)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200635 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300636
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200637 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200638 write_cb->buf_idx = 0;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200639 mei_hdr.msg_complete = 0;
Oren Weilab841162011-05-15 13:43:41 +0300640 cl->writing_state = MEI_WRITING;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200641 goto out;
642 }
643
644 dev->mei_host_buffer_is_empty = false;
645 if (length > mei_hbuf_max_data(dev)) {
646 mei_hdr.length = mei_hbuf_max_data(dev);
647 mei_hdr.msg_complete = 0;
648 } else {
649 mei_hdr.length = length;
650 mei_hdr.msg_complete = 1;
651 }
652 mei_hdr.host_addr = cl->host_client_id;
653 mei_hdr.me_addr = cl->me_client_id;
654 mei_hdr.reserved = 0;
655 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
656 *((u32 *) &mei_hdr));
657 if (mei_write_message(dev, &mei_hdr,
658 write_cb->request_buffer.data, mei_hdr.length)) {
659 rets = -ENODEV;
660 goto err;
661 }
662 cl->writing_state = MEI_WRITING;
663 write_cb->buf_idx = mei_hdr.length;
664
665out:
666 if (mei_hdr.msg_complete) {
667 if (mei_flow_ctrl_reduce(dev, cl)) {
668 rets = -ENODEV;
669 goto err;
670 }
671 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
672 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200673 list_add_tail(&write_cb->list, &dev->write_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300674 }
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200675
Oren Weilab841162011-05-15 13:43:41 +0300676 mutex_unlock(&dev->device_lock);
677 return length;
678
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200679err:
Oren Weilab841162011-05-15 13:43:41 +0300680 mutex_unlock(&dev->device_lock);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200681 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300682 return rets;
683}
684
685
686/**
687 * mei_ioctl - the IOCTL function
688 *
689 * @file: pointer to file structure
690 * @cmd: ioctl command
691 * @data: pointer to mei message structure
692 *
693 * returns 0 on success , <0 on error
694 */
695static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
696{
697 struct mei_device *dev;
698 struct mei_cl *cl = file->private_data;
699 struct mei_connect_client_data *connect_data = NULL;
700 int rets;
701
702 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
703 return -EINVAL;
704
705 if (WARN_ON(!cl || !cl->dev))
706 return -ENODEV;
707
708 dev = cl->dev;
709
710 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
711
712 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300713 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300714 rets = -ENODEV;
715 goto out;
716 }
717
718 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
719
720 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
721 GFP_KERNEL);
722 if (!connect_data) {
723 rets = -ENOMEM;
724 goto out;
725 }
726 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
727 if (copy_from_user(connect_data, (char __user *)data,
728 sizeof(struct mei_connect_client_data))) {
729 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
730 rets = -EFAULT;
731 goto out;
732 }
733 rets = mei_ioctl_connect_client(file, connect_data);
734
735 /* if all is ok, copying the data back to user. */
736 if (rets)
737 goto out;
738
739 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
740 if (copy_to_user((char __user *)data, connect_data,
741 sizeof(struct mei_connect_client_data))) {
742 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
743 rets = -EFAULT;
744 goto out;
745 }
746
747out:
748 kfree(connect_data);
749 mutex_unlock(&dev->device_lock);
750 return rets;
751}
752
753/**
754 * mei_compat_ioctl - the compat IOCTL function
755 *
756 * @file: pointer to file structure
757 * @cmd: ioctl command
758 * @data: pointer to mei message structure
759 *
760 * returns 0 on success , <0 on error
761 */
762#ifdef CONFIG_COMPAT
763static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200764 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300765{
766 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
767}
768#endif
769
770
771/**
772 * mei_poll - the poll function
773 *
774 * @file: pointer to file structure
775 * @wait: pointer to poll_table structure
776 *
777 * returns poll mask
778 */
779static unsigned int mei_poll(struct file *file, poll_table *wait)
780{
781 struct mei_cl *cl = file->private_data;
782 struct mei_device *dev;
783 unsigned int mask = 0;
784
785 if (WARN_ON(!cl || !cl->dev))
786 return mask;
787
788 dev = cl->dev;
789
790 mutex_lock(&dev->device_lock);
791
Tomas Winklerb210d752012-08-07 00:03:56 +0300792 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300793 goto out;
794
795
796 if (cl == &dev->iamthif_cl) {
797 mutex_unlock(&dev->device_lock);
798 poll_wait(file, &dev->iamthif_cl.wait, wait);
799 mutex_lock(&dev->device_lock);
800 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
801 dev->iamthif_file_object == file) {
802 mask |= (POLLIN | POLLRDNORM);
803 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200804 mei_amthif_run_next_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300805 }
806 goto out;
807 }
808
809 mutex_unlock(&dev->device_lock);
810 poll_wait(file, &cl->tx_wait, wait);
811 mutex_lock(&dev->device_lock);
812 if (MEI_WRITE_COMPLETE == cl->writing_state)
813 mask |= (POLLIN | POLLRDNORM);
814
815out:
816 mutex_unlock(&dev->device_lock);
817 return mask;
818}
819
Oren Weil5b881e32011-11-13 09:41:14 +0200820/*
821 * file operations structure will be used for mei char device.
822 */
823static const struct file_operations mei_fops = {
824 .owner = THIS_MODULE,
825 .read = mei_read,
826 .unlocked_ioctl = mei_ioctl,
827#ifdef CONFIG_COMPAT
828 .compat_ioctl = mei_compat_ioctl,
829#endif
830 .open = mei_open,
831 .release = mei_release,
832 .write = mei_write,
833 .poll = mei_poll,
834 .llseek = no_llseek
835};
836
837
838/*
839 * Misc Device Struct
840 */
841static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300842 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200843 .fops = &mei_fops,
844 .minor = MISC_DYNAMIC_MINOR,
845};
846
847/**
Tomas Winkler9a123f12012-08-06 15:23:55 +0300848 * mei_quirk_probe - probe for devices that doesn't valid ME interface
849 * @pdev: PCI device structure
850 * @ent: entry into pci_device_table
851 *
852 * returns true if ME Interface is valid, false otherwise
853 */
854static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
855 const struct pci_device_id *ent)
856{
857 u32 reg;
858 if (ent->device == MEI_DEV_ID_PBG_1) {
859 pci_read_config_dword(pdev, 0x48, &reg);
860 /* make sure that bit 9 is up and bit 10 is down */
861 if ((reg & 0x600) == 0x200) {
862 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
863 return false;
864 }
865 }
866 return true;
867}
868/**
Oren Weil5b881e32011-11-13 09:41:14 +0200869 * mei_probe - Device Initialization Routine
870 *
871 * @pdev: PCI device structure
872 * @ent: entry in kcs_pci_tbl
873 *
874 * returns 0 on success, <0 on failure.
875 */
876static int __devinit mei_probe(struct pci_dev *pdev,
877 const struct pci_device_id *ent)
878{
879 struct mei_device *dev;
880 int err;
881
882 mutex_lock(&mei_mutex);
Tomas Winkler9a123f12012-08-06 15:23:55 +0300883
884 if (!mei_quirk_probe(pdev, ent)) {
885 err = -ENODEV;
886 goto end;
887 }
888
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300889 if (mei_pdev) {
Oren Weil5b881e32011-11-13 09:41:14 +0200890 err = -EEXIST;
891 goto end;
892 }
893 /* enable pci dev */
894 err = pci_enable_device(pdev);
895 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300896 dev_err(&pdev->dev, "failed to enable pci device.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200897 goto end;
898 }
899 /* set PCI host mastering */
900 pci_set_master(pdev);
901 /* pci request regions for mei driver */
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300902 err = pci_request_regions(pdev, KBUILD_MODNAME);
Oren Weil5b881e32011-11-13 09:41:14 +0200903 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300904 dev_err(&pdev->dev, "failed to get pci regions.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200905 goto disable_device;
906 }
907 /* allocates and initializes the mei dev structure */
908 dev = mei_device_init(pdev);
909 if (!dev) {
910 err = -ENOMEM;
911 goto release_regions;
912 }
913 /* mapping IO device memory */
914 dev->mem_addr = pci_iomap(pdev, 0, 0);
915 if (!dev->mem_addr) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300916 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200917 err = -ENOMEM;
918 goto free_device;
919 }
920 pci_enable_msi(pdev);
921
922 /* request and enable interrupt */
923 if (pci_dev_msi_enabled(pdev))
924 err = request_threaded_irq(pdev->irq,
925 NULL,
926 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300927 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200928 else
929 err = request_threaded_irq(pdev->irq,
930 mei_interrupt_quick_handler,
931 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300932 IRQF_SHARED, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200933
934 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300935 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
Oren Weil5b881e32011-11-13 09:41:14 +0200936 pdev->irq);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300937 goto disable_msi;
Oren Weil5b881e32011-11-13 09:41:14 +0200938 }
939 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
940 if (mei_hw_init(dev)) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300941 dev_err(&pdev->dev, "init hw failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200942 err = -ENODEV;
943 goto release_irq;
944 }
945
946 err = misc_register(&mei_misc_device);
947 if (err)
948 goto release_irq;
949
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300950 mei_pdev = pdev;
Oren Weil5b881e32011-11-13 09:41:14 +0200951 pci_set_drvdata(pdev, dev);
952
953
954 schedule_delayed_work(&dev->timer_work, HZ);
955
956 mutex_unlock(&mei_mutex);
957
Tomas Winkler2f3d2b42012-03-19 22:38:13 +0200958 pr_debug("initialization successful.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200959
960 return 0;
961
962release_irq:
963 /* disable interrupts */
964 dev->host_hw_state = mei_hcsr_read(dev);
965 mei_disable_interrupts(dev);
966 flush_scheduled_work();
967 free_irq(pdev->irq, dev);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300968disable_msi:
Oren Weil5b881e32011-11-13 09:41:14 +0200969 pci_disable_msi(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200970 pci_iounmap(pdev, dev->mem_addr);
971free_device:
972 kfree(dev);
973release_regions:
974 pci_release_regions(pdev);
975disable_device:
976 pci_disable_device(pdev);
977end:
978 mutex_unlock(&mei_mutex);
Tomas Winkler32c826b2012-05-08 23:04:56 +0300979 dev_err(&pdev->dev, "initialization failed.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200980 return err;
981}
982
983/**
984 * mei_remove - Device Removal Routine
985 *
986 * @pdev: PCI device structure
987 *
988 * mei_remove is called by the PCI subsystem to alert the driver
989 * that it should release a PCI device.
990 */
991static void __devexit mei_remove(struct pci_dev *pdev)
992{
993 struct mei_device *dev;
994
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300995 if (mei_pdev != pdev)
Oren Weil5b881e32011-11-13 09:41:14 +0200996 return;
997
998 dev = pci_get_drvdata(pdev);
999 if (!dev)
1000 return;
1001
1002 mutex_lock(&dev->device_lock);
1003
Tomas Winklerc216fde2012-08-16 19:39:43 +03001004 cancel_delayed_work(&dev->timer_work);
1005
1006 mei_wd_stop(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001007
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001008 mei_pdev = NULL;
Oren Weil5b881e32011-11-13 09:41:14 +02001009
1010 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1011 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1012 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1013 }
1014 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1015 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1016 mei_disconnect_host_client(dev, &dev->wd_cl);
1017 }
1018
1019 /* Unregistering watchdog device */
Tomas Winkler70cd5332011-12-22 18:50:50 +02001020 mei_watchdog_unregister(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001021
1022 /* remove entry if already in list */
1023 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1024 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1025 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1026
1027 dev->iamthif_current_cb = NULL;
1028 dev->me_clients_num = 0;
1029
1030 mutex_unlock(&dev->device_lock);
1031
1032 flush_scheduled_work();
1033
1034 /* disable interrupts */
1035 mei_disable_interrupts(dev);
1036
1037 free_irq(pdev->irq, dev);
1038 pci_disable_msi(pdev);
1039 pci_set_drvdata(pdev, NULL);
1040
1041 if (dev->mem_addr)
1042 pci_iounmap(pdev, dev->mem_addr);
1043
1044 kfree(dev);
1045
1046 pci_release_regions(pdev);
1047 pci_disable_device(pdev);
Tomas Winklera44cab42012-05-29 16:39:11 +03001048
1049 misc_deregister(&mei_misc_device);
Oren Weil5b881e32011-11-13 09:41:14 +02001050}
Oren Weilab841162011-05-15 13:43:41 +03001051#ifdef CONFIG_PM
1052static int mei_pci_suspend(struct device *device)
1053{
1054 struct pci_dev *pdev = to_pci_dev(device);
1055 struct mei_device *dev = pci_get_drvdata(pdev);
1056 int err;
1057
1058 if (!dev)
1059 return -ENODEV;
1060 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +03001061
1062 cancel_delayed_work(&dev->timer_work);
1063
Oren Weilab841162011-05-15 13:43:41 +03001064 /* Stop watchdog if exists */
Tomas Winklerc216fde2012-08-16 19:39:43 +03001065 err = mei_wd_stop(dev);
Oren Weilab841162011-05-15 13:43:41 +03001066 /* Set new mei state */
Tomas Winklerb210d752012-08-07 00:03:56 +03001067 if (dev->dev_state == MEI_DEV_ENABLED ||
1068 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1069 dev->dev_state = MEI_DEV_POWER_DOWN;
Oren Weilab841162011-05-15 13:43:41 +03001070 mei_reset(dev, 0);
1071 }
1072 mutex_unlock(&dev->device_lock);
1073
1074 free_irq(pdev->irq, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001075 pci_disable_msi(pdev);
Oren Weilab841162011-05-15 13:43:41 +03001076
1077 return err;
1078}
1079
1080static int mei_pci_resume(struct device *device)
1081{
1082 struct pci_dev *pdev = to_pci_dev(device);
1083 struct mei_device *dev;
1084 int err;
1085
1086 dev = pci_get_drvdata(pdev);
1087 if (!dev)
1088 return -ENODEV;
1089
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001090 pci_enable_msi(pdev);
1091
1092 /* request and enable interrupt */
1093 if (pci_dev_msi_enabled(pdev))
1094 err = request_threaded_irq(pdev->irq,
1095 NULL,
1096 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001097 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001098 else
1099 err = request_threaded_irq(pdev->irq,
Oren Weilab841162011-05-15 13:43:41 +03001100 mei_interrupt_quick_handler,
1101 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001102 IRQF_SHARED, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001103
Oren Weilab841162011-05-15 13:43:41 +03001104 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001105 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1106 pdev->irq);
Oren Weilab841162011-05-15 13:43:41 +03001107 return err;
1108 }
1109
1110 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001111 dev->dev_state = MEI_DEV_POWER_UP;
Oren Weilab841162011-05-15 13:43:41 +03001112 mei_reset(dev, 1);
1113 mutex_unlock(&dev->device_lock);
1114
Oren Weil6d70e932011-09-07 09:03:14 +03001115 /* Start timer if stopped in suspend */
1116 schedule_delayed_work(&dev->timer_work, HZ);
1117
Oren Weilab841162011-05-15 13:43:41 +03001118 return err;
1119}
1120static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1121#define MEI_PM_OPS (&mei_pm_ops)
1122#else
Randy Dunlap2d990362011-05-19 08:52:34 -07001123#define MEI_PM_OPS NULL
Oren Weilab841162011-05-15 13:43:41 +03001124#endif /* CONFIG_PM */
1125/*
1126 * PCI driver structure
1127 */
1128static struct pci_driver mei_driver = {
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001129 .name = KBUILD_MODNAME,
Oren Weilab841162011-05-15 13:43:41 +03001130 .id_table = mei_pci_tbl,
1131 .probe = mei_probe,
1132 .remove = __devexit_p(mei_remove),
1133 .shutdown = __devexit_p(mei_remove),
1134 .driver.pm = MEI_PM_OPS,
1135};
1136
Tomas Winkler60781882012-07-19 09:45:32 +03001137module_pci_driver(mei_driver);
Oren Weilab841162011-05-15 13:43:41 +03001138
1139MODULE_AUTHOR("Intel Corporation");
1140MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1141MODULE_LICENSE("GPL v2");