blob: 5c7c85505c91cba2b9f0acec3016aa465024990d [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
Tomas Winkler4f3afe12012-05-09 16:38:59 +030040#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020041
42#include "mei_dev.h"
Tomas Winkler9dc64d62013-01-08 23:07:17 +020043#include "hw-me.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020044#include "client.h"
Oren Weilab841162011-05-15 13:43:41 +030045
Tomas Winklerdaed6b52012-08-17 09:54:23 +030046/* AMT device is a singleton on the platform */
47static struct pci_dev *mei_pdev;
Oren Weilab841162011-05-15 13:43:41 +030048
Oren Weilab841162011-05-15 13:43:41 +030049/* mei_pci_tbl - PCI Device ID Table */
50static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
Tomas Winkler9af51422012-08-29 01:15:50 +030082 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
83 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
Oren Weilab841162011-05-15 13:43:41 +030084
85 /* required last entry */
86 {0, }
87};
88
89MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
90
91static DEFINE_MUTEX(mei_mutex);
92
Oren Weilab841162011-05-15 13:43:41 +030093
94/**
Oren Weilab841162011-05-15 13:43:41 +030095 * mei_open - the open function
96 *
97 * @inode: pointer to inode structure
98 * @file: pointer to file structure
99 *
100 * returns 0 on success, <0 on error
101 */
102static int mei_open(struct inode *inode, struct file *file)
103{
104 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +0300105 struct mei_device *dev;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200106 unsigned long cl_id;
107 int err;
Oren Weilab841162011-05-15 13:43:41 +0300108
109 err = -ENODEV;
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300110 if (!mei_pdev)
Oren Weilab841162011-05-15 13:43:41 +0300111 goto out;
112
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300113 dev = pci_get_drvdata(mei_pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200114 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +0300115 goto out;
116
117 mutex_lock(&dev->device_lock);
118 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300119 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +0300120 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +0400121 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +0300122
123 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +0300124 if (dev->dev_state != MEI_DEV_ENABLED) {
125 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
126 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +0300127 goto out_unlock;
128 }
129 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +0300130 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
131 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
132 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +0300133 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300134 }
Oren Weilab841162011-05-15 13:43:41 +0300135
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200136 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler1b812942012-09-11 00:43:20 +0300137 if (cl_id >= MEI_CLIENTS_MAX) {
138 dev_err(&dev->pdev->dev, "client_id exceded %d",
139 MEI_CLIENTS_MAX) ;
Oren Weilab841162011-05-15 13:43:41 +0300140 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300141 }
Oren Weilab841162011-05-15 13:43:41 +0300142
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200143 cl->host_client_id = cl_id;
144
Oren Weilab841162011-05-15 13:43:41 +0300145 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
146
147 dev->open_handle_count++;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200148
Oren Weilab841162011-05-15 13:43:41 +0300149 list_add_tail(&cl->link, &dev->file_list);
150
151 set_bit(cl->host_client_id, dev->host_clients_map);
152 cl->state = MEI_FILE_INITIALIZING;
153 cl->sm_state = 0;
154
155 file->private_data = cl;
156 mutex_unlock(&dev->device_lock);
157
Oren Weil5b881e32011-11-13 09:41:14 +0200158 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300159
160out_unlock:
161 mutex_unlock(&dev->device_lock);
162 kfree(cl);
163out:
164 return err;
165}
166
167/**
168 * mei_release - the release function
169 *
170 * @inode: pointer to inode structure
171 * @file: pointer to file structure
172 *
173 * returns 0 on success, <0 on error
174 */
175static int mei_release(struct inode *inode, struct file *file)
176{
177 struct mei_cl *cl = file->private_data;
178 struct mei_cl_cb *cb;
179 struct mei_device *dev;
180 int rets = 0;
181
182 if (WARN_ON(!cl || !cl->dev))
183 return -ENODEV;
184
185 dev = cl->dev;
186
187 mutex_lock(&dev->device_lock);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200188 if (cl == &dev->iamthif_cl) {
189 rets = mei_amthif_release(dev, file);
190 goto out;
191 }
192 if (cl->state == MEI_FILE_CONNECTED) {
193 cl->state = MEI_FILE_DISCONNECTING;
194 dev_dbg(&dev->pdev->dev,
195 "disconnecting client host client = %d, "
196 "ME client = %d\n",
Oren Weilab841162011-05-15 13:43:41 +0300197 cl->host_client_id,
198 cl->me_client_id);
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200199 rets = mei_cl_disconnect(cl);
Oren Weilab841162011-05-15 13:43:41 +0300200 }
Tomas Winklera562d5c2012-11-11 17:38:01 +0200201 mei_cl_flush_queues(cl);
202 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
203 cl->host_client_id,
204 cl->me_client_id);
205
206 if (dev->open_handle_count > 0) {
207 clear_bit(cl->host_client_id, dev->host_clients_map);
208 dev->open_handle_count--;
209 }
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200210 mei_cl_unlink(cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200211
212 /* free read cb */
213 cb = NULL;
214 if (cl->read_cb) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200215 cb = mei_cl_find_read_cb(cl);
Tomas Winklera562d5c2012-11-11 17:38:01 +0200216 /* Remove entry from read list */
217 if (cb)
218 list_del(&cb->list);
219
220 cb = cl->read_cb;
221 cl->read_cb = NULL;
222 }
223
224 file->private_data = NULL;
225
226 if (cb) {
227 mei_io_cb_free(cb);
228 cb = NULL;
229 }
230
231 kfree(cl);
232out:
Oren Weilab841162011-05-15 13:43:41 +0300233 mutex_unlock(&dev->device_lock);
234 return rets;
235}
236
237
238/**
239 * mei_read - the read function.
240 *
241 * @file: pointer to file structure
242 * @ubuf: pointer to user buffer
243 * @length: buffer length
244 * @offset: data offset in buffer
245 *
246 * returns >=0 data length on success , <0 on error
247 */
248static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200249 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300250{
251 struct mei_cl *cl = file->private_data;
252 struct mei_cl_cb *cb_pos = NULL;
253 struct mei_cl_cb *cb = NULL;
254 struct mei_device *dev;
255 int i;
256 int rets;
257 int err;
258
259
260 if (WARN_ON(!cl || !cl->dev))
261 return -ENODEV;
262
263 dev = cl->dev;
264
265 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300266 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300267 rets = -ENODEV;
268 goto out;
269 }
270
271 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
272 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300273 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300274 if (i >= 0) {
275 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300276 if (cl->me_client_id == me_client->client_id) {
277 rets = -EBADF;
278 goto out;
279 }
280 }
281 } else {
282 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
283 }
284
285 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200286 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300287 goto out;
288 }
289
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200290 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300291 cb = cl->read_cb;
292 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200293 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
294 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300295 cb = cl->read_cb;
296 rets = 0;
297 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200298 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700299 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300300 *offset = 0;
301 rets = 0;
302 goto out;
303 }
304
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200305 err = mei_cl_read_start(cl);
Oren Weilab841162011-05-15 13:43:41 +0300306 if (err && err != -EBUSY) {
307 dev_dbg(&dev->pdev->dev,
308 "mei start read failure with status = %d\n", err);
309 rets = err;
310 goto out;
311 }
312
313 if (MEI_READ_COMPLETE != cl->reading_state &&
314 !waitqueue_active(&cl->rx_wait)) {
315 if (file->f_flags & O_NONBLOCK) {
316 rets = -EAGAIN;
317 goto out;
318 }
319
320 mutex_unlock(&dev->device_lock);
321
322 if (wait_event_interruptible(cl->rx_wait,
323 (MEI_READ_COMPLETE == cl->reading_state ||
324 MEI_FILE_INITIALIZING == cl->state ||
325 MEI_FILE_DISCONNECTED == cl->state ||
326 MEI_FILE_DISCONNECTING == cl->state))) {
327 if (signal_pending(current))
328 return -EINTR;
329 return -ERESTARTSYS;
330 }
331
332 mutex_lock(&dev->device_lock);
333 if (MEI_FILE_INITIALIZING == cl->state ||
334 MEI_FILE_DISCONNECTED == cl->state ||
335 MEI_FILE_DISCONNECTING == cl->state) {
336 rets = -EBUSY;
337 goto out;
338 }
339 }
340
341 cb = cl->read_cb;
342
343 if (!cb) {
344 rets = -ENODEV;
345 goto out;
346 }
347 if (cl->reading_state != MEI_READ_COMPLETE) {
348 rets = 0;
349 goto out;
350 }
351 /* now copy the data to user space */
352copy_buffer:
353 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
354 cb->response_buffer.size);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200355 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
356 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300357 rets = -EMSGSIZE;
358 goto free;
359 }
360
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200361 /* length is being truncated to PAGE_SIZE,
362 * however buf_idx may point beyond that */
363 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300364
Tomas Winkler441ab502011-12-13 23:39:34 +0200365 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300366 rets = -EFAULT;
367 goto free;
368 }
369
370 rets = length;
371 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200372 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300373 goto out;
374
375free:
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200376 cb_pos = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300377 /* Remove entry from read list */
378 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200379 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200380 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300381 cl->reading_state = MEI_IDLE;
382 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300383out:
384 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
385 mutex_unlock(&dev->device_lock);
386 return rets;
387}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200388/**
Oren Weilab841162011-05-15 13:43:41 +0300389 * mei_write - the write function.
390 *
391 * @file: pointer to file structure
392 * @ubuf: pointer to user buffer
393 * @length: buffer length
394 * @offset: data offset in buffer
395 *
396 * returns >=0 data length on success , <0 on error
397 */
398static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200399 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300400{
401 struct mei_cl *cl = file->private_data;
402 struct mei_cl_cb *write_cb = NULL;
403 struct mei_msg_hdr mei_hdr;
404 struct mei_device *dev;
405 unsigned long timeout = 0;
406 int rets;
407 int i;
408
409 if (WARN_ON(!cl || !cl->dev))
410 return -ENODEV;
411
412 dev = cl->dev;
413
414 mutex_lock(&dev->device_lock);
415
Tomas Winklerb210d752012-08-07 00:03:56 +0300416 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200417 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200418 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300419 }
420
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200421 i = mei_me_cl_by_id(dev, cl->me_client_id);
422 if (i < 0) {
423 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200424 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200425 }
426 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
427 rets = -EMSGSIZE;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200428 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200429 }
430
431 if (cl->state != MEI_FILE_CONNECTED) {
432 rets = -ENODEV;
433 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
434 cl->host_client_id, cl->me_client_id);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200435 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200436 }
Oren Weilab841162011-05-15 13:43:41 +0300437 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200438 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300439
440 if (write_cb) {
441 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200442 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300443
444 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200445 cl->reading_state == MEI_READ_COMPLETE) {
446 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200447 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200448 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200449 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300450 }
451 }
452 }
453
454 /* free entry used in read */
455 if (cl->reading_state == MEI_READ_COMPLETE) {
456 *offset = 0;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200457 write_cb = mei_cl_find_read_cb(cl);
Oren Weilab841162011-05-15 13:43:41 +0300458 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200459 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200460 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300461 write_cb = NULL;
462 cl->reading_state = MEI_IDLE;
463 cl->read_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300464 }
Tomas Winklerd91aaed2013-01-08 23:07:18 +0200465 } else if (cl->reading_state == MEI_IDLE)
Oren Weilab841162011-05-15 13:43:41 +0300466 *offset = 0;
467
468
Tomas Winkler33d28c92012-10-09 16:50:17 +0200469 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300470 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200471 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
472 rets = -ENOMEM;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200473 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300474 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200475 rets = mei_io_cb_alloc_req_buf(write_cb, length);
476 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200477 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300478
Tomas Winkler33d28c92012-10-09 16:50:17 +0200479 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
Oren Weilab841162011-05-15 13:43:41 +0300480
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200481 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
482 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200483 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300484
485 cl->sm_state = 0;
486 if (length == 4 &&
487 ((memcmp(mei_wd_state_independence_msg[0],
488 write_cb->request_buffer.data, 4) == 0) ||
489 (memcmp(mei_wd_state_independence_msg[1],
490 write_cb->request_buffer.data, 4) == 0) ||
491 (memcmp(mei_wd_state_independence_msg[2],
492 write_cb->request_buffer.data, 4) == 0)))
493 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
494
Oren Weilab841162011-05-15 13:43:41 +0300495 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200496 rets = mei_amthif_write(dev, write_cb);
497
498 if (rets) {
499 dev_err(&dev->pdev->dev,
500 "amthi write failed with status = %d\n", rets);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200501 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300502 }
503 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200504 return length;
Oren Weilab841162011-05-15 13:43:41 +0300505 }
506
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200507 write_cb->fop_type = MEI_FOP_WRITE;
Oren Weilab841162011-05-15 13:43:41 +0300508
509 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
510 cl->host_client_id, cl->me_client_id);
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200511 rets = mei_cl_flow_ctrl_creds(cl);
Oren Weilab841162011-05-15 13:43:41 +0300512 if (rets < 0)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200513 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300514
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200515 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200516 write_cb->buf_idx = 0;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200517 mei_hdr.msg_complete = 0;
Oren Weilab841162011-05-15 13:43:41 +0300518 cl->writing_state = MEI_WRITING;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200519 goto out;
520 }
521
522 dev->mei_host_buffer_is_empty = false;
523 if (length > mei_hbuf_max_data(dev)) {
524 mei_hdr.length = mei_hbuf_max_data(dev);
525 mei_hdr.msg_complete = 0;
526 } else {
527 mei_hdr.length = length;
528 mei_hdr.msg_complete = 1;
529 }
530 mei_hdr.host_addr = cl->host_client_id;
531 mei_hdr.me_addr = cl->me_client_id;
532 mei_hdr.reserved = 0;
Tomas Winkler15d4acc2012-12-25 19:06:00 +0200533
534 dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
535 MEI_HDR_PRM(&mei_hdr));
Tomas Winkler438763f2012-12-25 19:05:59 +0200536 if (mei_write_message(dev, &mei_hdr, write_cb->request_buffer.data)) {
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200537 rets = -ENODEV;
538 goto err;
539 }
540 cl->writing_state = MEI_WRITING;
541 write_cb->buf_idx = mei_hdr.length;
542
543out:
544 if (mei_hdr.msg_complete) {
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200545 if (mei_cl_flow_ctrl_reduce(cl)) {
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200546 rets = -ENODEV;
547 goto err;
548 }
549 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
550 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200551 list_add_tail(&write_cb->list, &dev->write_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300552 }
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200553
Oren Weilab841162011-05-15 13:43:41 +0300554 mutex_unlock(&dev->device_lock);
555 return length;
556
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200557err:
Oren Weilab841162011-05-15 13:43:41 +0300558 mutex_unlock(&dev->device_lock);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200559 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300560 return rets;
561}
562
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200563/**
564 * mei_ioctl_connect_client - the connect to fw client IOCTL function
565 *
566 * @dev: the device structure
567 * @data: IOCTL connect data, input and output parameters
568 * @file: private data of the file object
569 *
570 * Locking: called under "dev->device_lock" lock
571 *
572 * returns 0 on success, <0 on failure.
573 */
574static int mei_ioctl_connect_client(struct file *file,
575 struct mei_connect_client_data *data)
576{
577 struct mei_device *dev;
578 struct mei_client *client;
579 struct mei_cl *cl;
580 int i;
581 int rets;
582
583 cl = file->private_data;
584 if (WARN_ON(!cl || !cl->dev))
585 return -ENODEV;
586
587 dev = cl->dev;
588
589 if (dev->dev_state != MEI_DEV_ENABLED) {
590 rets = -ENODEV;
591 goto end;
592 }
593
594 if (cl->state != MEI_FILE_INITIALIZING &&
595 cl->state != MEI_FILE_DISCONNECTED) {
596 rets = -EBUSY;
597 goto end;
598 }
599
600 /* find ME client we're trying to connect to */
601 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
602 if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
603 cl->me_client_id = dev->me_clients[i].client_id;
604 cl->state = MEI_FILE_CONNECTING;
605 }
606
607 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
608 cl->me_client_id);
609 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
610 dev->me_clients[i].props.protocol_version);
611 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
612 dev->me_clients[i].props.max_msg_length);
613
614 /* if we're connecting to amthi client then we will use the
615 * existing connection
616 */
617 if (uuid_le_cmp(data->in_client_uuid, mei_amthi_guid) == 0) {
618 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
619 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
620 rets = -ENODEV;
621 goto end;
622 }
623 clear_bit(cl->host_client_id, dev->host_clients_map);
624 mei_cl_unlink(cl);
625
626 kfree(cl);
627 cl = NULL;
628 file->private_data = &dev->iamthif_cl;
629
630 client = &data->out_client_properties;
631 client->max_msg_length =
632 dev->me_clients[i].props.max_msg_length;
633 client->protocol_version =
634 dev->me_clients[i].props.protocol_version;
635 rets = dev->iamthif_cl.status;
636
637 goto end;
638 }
639
640 if (cl->state != MEI_FILE_CONNECTING) {
641 rets = -ENODEV;
642 goto end;
643 }
644
645
646 /* prepare the output buffer */
647 client = &data->out_client_properties;
648 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
649 client->protocol_version = dev->me_clients[i].props.protocol_version;
650 dev_dbg(&dev->pdev->dev, "Can connect?\n");
651
652
653 rets = mei_cl_connect(cl, file);
654
655end:
656 dev_dbg(&dev->pdev->dev, "free connect cb memory.");
657 return rets;
658}
659
Oren Weilab841162011-05-15 13:43:41 +0300660
661/**
662 * mei_ioctl - the IOCTL function
663 *
664 * @file: pointer to file structure
665 * @cmd: ioctl command
666 * @data: pointer to mei message structure
667 *
668 * returns 0 on success , <0 on error
669 */
670static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
671{
672 struct mei_device *dev;
673 struct mei_cl *cl = file->private_data;
674 struct mei_connect_client_data *connect_data = NULL;
675 int rets;
676
677 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
678 return -EINVAL;
679
680 if (WARN_ON(!cl || !cl->dev))
681 return -ENODEV;
682
683 dev = cl->dev;
684
685 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
686
687 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300688 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300689 rets = -ENODEV;
690 goto out;
691 }
692
693 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
694
695 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
696 GFP_KERNEL);
697 if (!connect_data) {
698 rets = -ENOMEM;
699 goto out;
700 }
701 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
702 if (copy_from_user(connect_data, (char __user *)data,
703 sizeof(struct mei_connect_client_data))) {
704 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
705 rets = -EFAULT;
706 goto out;
707 }
Tomas Winkler9f81abda2013-01-08 23:07:15 +0200708
Oren Weilab841162011-05-15 13:43:41 +0300709 rets = mei_ioctl_connect_client(file, connect_data);
710
711 /* if all is ok, copying the data back to user. */
712 if (rets)
713 goto out;
714
715 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
716 if (copy_to_user((char __user *)data, connect_data,
717 sizeof(struct mei_connect_client_data))) {
718 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
719 rets = -EFAULT;
720 goto out;
721 }
722
723out:
724 kfree(connect_data);
725 mutex_unlock(&dev->device_lock);
726 return rets;
727}
728
729/**
730 * mei_compat_ioctl - the compat IOCTL function
731 *
732 * @file: pointer to file structure
733 * @cmd: ioctl command
734 * @data: pointer to mei message structure
735 *
736 * returns 0 on success , <0 on error
737 */
738#ifdef CONFIG_COMPAT
739static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200740 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300741{
742 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
743}
744#endif
745
746
747/**
748 * mei_poll - the poll function
749 *
750 * @file: pointer to file structure
751 * @wait: pointer to poll_table structure
752 *
753 * returns poll mask
754 */
755static unsigned int mei_poll(struct file *file, poll_table *wait)
756{
757 struct mei_cl *cl = file->private_data;
758 struct mei_device *dev;
759 unsigned int mask = 0;
760
761 if (WARN_ON(!cl || !cl->dev))
762 return mask;
763
764 dev = cl->dev;
765
766 mutex_lock(&dev->device_lock);
767
Tomas Winklerb210d752012-08-07 00:03:56 +0300768 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300769 goto out;
770
771
772 if (cl == &dev->iamthif_cl) {
Tomas Winkler744f0f22012-11-11 17:38:02 +0200773 mask = mei_amthif_poll(dev, file, wait);
Oren Weilab841162011-05-15 13:43:41 +0300774 goto out;
775 }
776
777 mutex_unlock(&dev->device_lock);
778 poll_wait(file, &cl->tx_wait, wait);
779 mutex_lock(&dev->device_lock);
780 if (MEI_WRITE_COMPLETE == cl->writing_state)
781 mask |= (POLLIN | POLLRDNORM);
782
783out:
784 mutex_unlock(&dev->device_lock);
785 return mask;
786}
787
Oren Weil5b881e32011-11-13 09:41:14 +0200788/*
789 * file operations structure will be used for mei char device.
790 */
791static const struct file_operations mei_fops = {
792 .owner = THIS_MODULE,
793 .read = mei_read,
794 .unlocked_ioctl = mei_ioctl,
795#ifdef CONFIG_COMPAT
796 .compat_ioctl = mei_compat_ioctl,
797#endif
798 .open = mei_open,
799 .release = mei_release,
800 .write = mei_write,
801 .poll = mei_poll,
802 .llseek = no_llseek
803};
804
805
806/*
807 * Misc Device Struct
808 */
809static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300810 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200811 .fops = &mei_fops,
812 .minor = MISC_DYNAMIC_MINOR,
813};
814
815/**
Tomas Winkler9a123f12012-08-06 15:23:55 +0300816 * mei_quirk_probe - probe for devices that doesn't valid ME interface
817 * @pdev: PCI device structure
818 * @ent: entry into pci_device_table
819 *
820 * returns true if ME Interface is valid, false otherwise
821 */
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500822static bool mei_quirk_probe(struct pci_dev *pdev,
Tomas Winkler9a123f12012-08-06 15:23:55 +0300823 const struct pci_device_id *ent)
824{
825 u32 reg;
826 if (ent->device == MEI_DEV_ID_PBG_1) {
827 pci_read_config_dword(pdev, 0x48, &reg);
828 /* make sure that bit 9 is up and bit 10 is down */
829 if ((reg & 0x600) == 0x200) {
830 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
831 return false;
832 }
833 }
834 return true;
835}
836/**
Oren Weil5b881e32011-11-13 09:41:14 +0200837 * mei_probe - Device Initialization Routine
838 *
839 * @pdev: PCI device structure
840 * @ent: entry in kcs_pci_tbl
841 *
842 * returns 0 on success, <0 on failure.
843 */
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500844static int mei_probe(struct pci_dev *pdev,
Oren Weil5b881e32011-11-13 09:41:14 +0200845 const struct pci_device_id *ent)
846{
847 struct mei_device *dev;
848 int err;
849
850 mutex_lock(&mei_mutex);
Tomas Winkler9a123f12012-08-06 15:23:55 +0300851
852 if (!mei_quirk_probe(pdev, ent)) {
853 err = -ENODEV;
854 goto end;
855 }
856
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300857 if (mei_pdev) {
Oren Weil5b881e32011-11-13 09:41:14 +0200858 err = -EEXIST;
859 goto end;
860 }
861 /* enable pci dev */
862 err = pci_enable_device(pdev);
863 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300864 dev_err(&pdev->dev, "failed to enable pci device.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200865 goto end;
866 }
867 /* set PCI host mastering */
868 pci_set_master(pdev);
869 /* pci request regions for mei driver */
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300870 err = pci_request_regions(pdev, KBUILD_MODNAME);
Oren Weil5b881e32011-11-13 09:41:14 +0200871 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300872 dev_err(&pdev->dev, "failed to get pci regions.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200873 goto disable_device;
874 }
875 /* allocates and initializes the mei dev structure */
876 dev = mei_device_init(pdev);
877 if (!dev) {
878 err = -ENOMEM;
879 goto release_regions;
880 }
881 /* mapping IO device memory */
882 dev->mem_addr = pci_iomap(pdev, 0, 0);
883 if (!dev->mem_addr) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300884 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200885 err = -ENOMEM;
886 goto free_device;
887 }
888 pci_enable_msi(pdev);
889
890 /* request and enable interrupt */
891 if (pci_dev_msi_enabled(pdev))
892 err = request_threaded_irq(pdev->irq,
893 NULL,
894 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300895 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200896 else
897 err = request_threaded_irq(pdev->irq,
898 mei_interrupt_quick_handler,
899 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300900 IRQF_SHARED, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200901
902 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300903 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
Oren Weil5b881e32011-11-13 09:41:14 +0200904 pdev->irq);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300905 goto disable_msi;
Oren Weil5b881e32011-11-13 09:41:14 +0200906 }
907 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
Samuel Ortizc1174c02012-11-18 15:13:20 +0200908 INIT_WORK(&dev->init_work, mei_host_client_init);
909
Oren Weil5b881e32011-11-13 09:41:14 +0200910 if (mei_hw_init(dev)) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300911 dev_err(&pdev->dev, "init hw failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200912 err = -ENODEV;
913 goto release_irq;
914 }
915
916 err = misc_register(&mei_misc_device);
917 if (err)
918 goto release_irq;
919
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300920 mei_pdev = pdev;
Oren Weil5b881e32011-11-13 09:41:14 +0200921 pci_set_drvdata(pdev, dev);
922
923
924 schedule_delayed_work(&dev->timer_work, HZ);
925
926 mutex_unlock(&mei_mutex);
927
Tomas Winkler2f3d2b42012-03-19 22:38:13 +0200928 pr_debug("initialization successful.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200929
930 return 0;
931
932release_irq:
933 /* disable interrupts */
934 dev->host_hw_state = mei_hcsr_read(dev);
935 mei_disable_interrupts(dev);
936 flush_scheduled_work();
937 free_irq(pdev->irq, dev);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300938disable_msi:
Oren Weil5b881e32011-11-13 09:41:14 +0200939 pci_disable_msi(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200940 pci_iounmap(pdev, dev->mem_addr);
941free_device:
942 kfree(dev);
943release_regions:
944 pci_release_regions(pdev);
945disable_device:
946 pci_disable_device(pdev);
947end:
948 mutex_unlock(&mei_mutex);
Tomas Winkler32c826b2012-05-08 23:04:56 +0300949 dev_err(&pdev->dev, "initialization failed.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200950 return err;
951}
952
953/**
954 * mei_remove - Device Removal Routine
955 *
956 * @pdev: PCI device structure
957 *
958 * mei_remove is called by the PCI subsystem to alert the driver
959 * that it should release a PCI device.
960 */
Bill Pemberton486a5c22012-11-19 13:26:02 -0500961static void mei_remove(struct pci_dev *pdev)
Oren Weil5b881e32011-11-13 09:41:14 +0200962{
963 struct mei_device *dev;
964
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300965 if (mei_pdev != pdev)
Oren Weil5b881e32011-11-13 09:41:14 +0200966 return;
967
968 dev = pci_get_drvdata(pdev);
969 if (!dev)
970 return;
971
972 mutex_lock(&dev->device_lock);
973
Tomas Winklerc216fde2012-08-16 19:39:43 +0300974 cancel_delayed_work(&dev->timer_work);
975
976 mei_wd_stop(dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200977
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300978 mei_pdev = NULL;
Oren Weil5b881e32011-11-13 09:41:14 +0200979
980 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
981 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200982 mei_cl_disconnect(&dev->iamthif_cl);
Oren Weil5b881e32011-11-13 09:41:14 +0200983 }
984 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
985 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200986 mei_cl_disconnect(&dev->wd_cl);
Oren Weil5b881e32011-11-13 09:41:14 +0200987 }
988
989 /* Unregistering watchdog device */
Tomas Winkler70cd5332011-12-22 18:50:50 +0200990 mei_watchdog_unregister(dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200991
992 /* remove entry if already in list */
993 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200994 mei_cl_unlink(&dev->wd_cl);
995 mei_cl_unlink(&dev->iamthif_cl);
Oren Weil5b881e32011-11-13 09:41:14 +0200996
997 dev->iamthif_current_cb = NULL;
998 dev->me_clients_num = 0;
999
1000 mutex_unlock(&dev->device_lock);
1001
1002 flush_scheduled_work();
1003
1004 /* disable interrupts */
1005 mei_disable_interrupts(dev);
1006
1007 free_irq(pdev->irq, dev);
1008 pci_disable_msi(pdev);
1009 pci_set_drvdata(pdev, NULL);
1010
1011 if (dev->mem_addr)
1012 pci_iounmap(pdev, dev->mem_addr);
1013
1014 kfree(dev);
1015
1016 pci_release_regions(pdev);
1017 pci_disable_device(pdev);
Tomas Winklera44cab42012-05-29 16:39:11 +03001018
1019 misc_deregister(&mei_misc_device);
Oren Weil5b881e32011-11-13 09:41:14 +02001020}
Oren Weilab841162011-05-15 13:43:41 +03001021#ifdef CONFIG_PM
1022static int mei_pci_suspend(struct device *device)
1023{
1024 struct pci_dev *pdev = to_pci_dev(device);
1025 struct mei_device *dev = pci_get_drvdata(pdev);
1026 int err;
1027
1028 if (!dev)
1029 return -ENODEV;
1030 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +03001031
1032 cancel_delayed_work(&dev->timer_work);
1033
Oren Weilab841162011-05-15 13:43:41 +03001034 /* Stop watchdog if exists */
Tomas Winklerc216fde2012-08-16 19:39:43 +03001035 err = mei_wd_stop(dev);
Oren Weilab841162011-05-15 13:43:41 +03001036 /* Set new mei state */
Tomas Winklerb210d752012-08-07 00:03:56 +03001037 if (dev->dev_state == MEI_DEV_ENABLED ||
1038 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1039 dev->dev_state = MEI_DEV_POWER_DOWN;
Oren Weilab841162011-05-15 13:43:41 +03001040 mei_reset(dev, 0);
1041 }
1042 mutex_unlock(&dev->device_lock);
1043
1044 free_irq(pdev->irq, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001045 pci_disable_msi(pdev);
Oren Weilab841162011-05-15 13:43:41 +03001046
1047 return err;
1048}
1049
1050static int mei_pci_resume(struct device *device)
1051{
1052 struct pci_dev *pdev = to_pci_dev(device);
1053 struct mei_device *dev;
1054 int err;
1055
1056 dev = pci_get_drvdata(pdev);
1057 if (!dev)
1058 return -ENODEV;
1059
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001060 pci_enable_msi(pdev);
1061
1062 /* request and enable interrupt */
1063 if (pci_dev_msi_enabled(pdev))
1064 err = request_threaded_irq(pdev->irq,
1065 NULL,
1066 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001067 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001068 else
1069 err = request_threaded_irq(pdev->irq,
Oren Weilab841162011-05-15 13:43:41 +03001070 mei_interrupt_quick_handler,
1071 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001072 IRQF_SHARED, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001073
Oren Weilab841162011-05-15 13:43:41 +03001074 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001075 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1076 pdev->irq);
Oren Weilab841162011-05-15 13:43:41 +03001077 return err;
1078 }
1079
1080 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001081 dev->dev_state = MEI_DEV_POWER_UP;
Oren Weilab841162011-05-15 13:43:41 +03001082 mei_reset(dev, 1);
1083 mutex_unlock(&dev->device_lock);
1084
Oren Weil6d70e932011-09-07 09:03:14 +03001085 /* Start timer if stopped in suspend */
1086 schedule_delayed_work(&dev->timer_work, HZ);
1087
Oren Weilab841162011-05-15 13:43:41 +03001088 return err;
1089}
1090static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1091#define MEI_PM_OPS (&mei_pm_ops)
1092#else
Randy Dunlap2d990362011-05-19 08:52:34 -07001093#define MEI_PM_OPS NULL
Oren Weilab841162011-05-15 13:43:41 +03001094#endif /* CONFIG_PM */
1095/*
1096 * PCI driver structure
1097 */
1098static struct pci_driver mei_driver = {
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001099 .name = KBUILD_MODNAME,
Oren Weilab841162011-05-15 13:43:41 +03001100 .id_table = mei_pci_tbl,
1101 .probe = mei_probe,
Bill Pemberton9306a8b2012-11-19 13:20:25 -05001102 .remove = mei_remove,
1103 .shutdown = mei_remove,
Oren Weilab841162011-05-15 13:43:41 +03001104 .driver.pm = MEI_PM_OPS,
1105};
1106
Tomas Winkler60781882012-07-19 09:45:32 +03001107module_pci_driver(mei_driver);
Oren Weilab841162011-05-15 13:43:41 +03001108
1109MODULE_AUTHOR("Intel Corporation");
1110MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1111MODULE_LICENSE("GPL v2");