blob: bea545a5ae2bd5f4cb038614811b925beca95d50 [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 Winklerfb601ad2012-10-15 12:06:48 +0200192 list_for_each_entry_safe(pos, next, &dev->read_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200193 struct mei_cl *cl_temp;
194 cl_temp = (struct mei_cl *)pos->file_private;
Oren Weilab841162011-05-15 13:43:41 +0300195
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200196 if (mei_cl_cmp_id(cl, cl_temp))
197 return pos;
Oren Weilab841162011-05-15 13:43:41 +0300198 }
199 return NULL;
200}
201
202/**
203 * mei_open - the open function
204 *
205 * @inode: pointer to inode structure
206 * @file: pointer to file structure
207 *
208 * returns 0 on success, <0 on error
209 */
210static int mei_open(struct inode *inode, struct file *file)
211{
212 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +0300213 struct mei_device *dev;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200214 unsigned long cl_id;
215 int err;
Oren Weilab841162011-05-15 13:43:41 +0300216
217 err = -ENODEV;
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300218 if (!mei_pdev)
Oren Weilab841162011-05-15 13:43:41 +0300219 goto out;
220
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300221 dev = pci_get_drvdata(mei_pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200222 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +0300223 goto out;
224
225 mutex_lock(&dev->device_lock);
226 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300227 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +0300228 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +0400229 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +0300230
231 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +0300232 if (dev->dev_state != MEI_DEV_ENABLED) {
233 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
234 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +0300235 goto out_unlock;
236 }
237 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +0300238 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
239 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
240 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +0300241 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300242 }
Oren Weilab841162011-05-15 13:43:41 +0300243
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200244 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler1b812942012-09-11 00:43:20 +0300245 if (cl_id >= MEI_CLIENTS_MAX) {
246 dev_err(&dev->pdev->dev, "client_id exceded %d",
247 MEI_CLIENTS_MAX) ;
Oren Weilab841162011-05-15 13:43:41 +0300248 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300249 }
Oren Weilab841162011-05-15 13:43:41 +0300250
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200251 cl->host_client_id = cl_id;
252
Oren Weilab841162011-05-15 13:43:41 +0300253 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
254
255 dev->open_handle_count++;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200256
Oren Weilab841162011-05-15 13:43:41 +0300257 list_add_tail(&cl->link, &dev->file_list);
258
259 set_bit(cl->host_client_id, dev->host_clients_map);
260 cl->state = MEI_FILE_INITIALIZING;
261 cl->sm_state = 0;
262
263 file->private_data = cl;
264 mutex_unlock(&dev->device_lock);
265
Oren Weil5b881e32011-11-13 09:41:14 +0200266 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300267
268out_unlock:
269 mutex_unlock(&dev->device_lock);
270 kfree(cl);
271out:
272 return err;
273}
274
275/**
276 * mei_release - the release function
277 *
278 * @inode: pointer to inode structure
279 * @file: pointer to file structure
280 *
281 * returns 0 on success, <0 on error
282 */
283static int mei_release(struct inode *inode, struct file *file)
284{
285 struct mei_cl *cl = file->private_data;
286 struct mei_cl_cb *cb;
287 struct mei_device *dev;
288 int rets = 0;
289
290 if (WARN_ON(!cl || !cl->dev))
291 return -ENODEV;
292
293 dev = cl->dev;
294
295 mutex_lock(&dev->device_lock);
296 if (cl != &dev->iamthif_cl) {
297 if (cl->state == MEI_FILE_CONNECTED) {
298 cl->state = MEI_FILE_DISCONNECTING;
299 dev_dbg(&dev->pdev->dev,
300 "disconnecting client host client = %d, "
301 "ME client = %d\n",
302 cl->host_client_id,
303 cl->me_client_id);
304 rets = mei_disconnect_host_client(dev, cl);
305 }
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300306 mei_cl_flush_queues(cl);
Oren Weilab841162011-05-15 13:43:41 +0300307 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
308 cl->host_client_id,
309 cl->me_client_id);
310
311 if (dev->open_handle_count > 0) {
Tomas Winkler441ab502011-12-13 23:39:34 +0200312 clear_bit(cl->host_client_id, dev->host_clients_map);
Oren Weilab841162011-05-15 13:43:41 +0300313 dev->open_handle_count--;
314 }
315 mei_remove_client_from_file_list(dev, cl->host_client_id);
316
317 /* free read cb */
318 cb = NULL;
319 if (cl->read_cb) {
320 cb = find_read_list_entry(dev, cl);
321 /* Remove entry from read list */
322 if (cb)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200323 list_del(&cb->list);
Oren Weilab841162011-05-15 13:43:41 +0300324
325 cb = cl->read_cb;
326 cl->read_cb = NULL;
327 }
328
329 file->private_data = NULL;
330
331 if (cb) {
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200332 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300333 cb = NULL;
334 }
335
336 kfree(cl);
337 } else {
338 if (dev->open_handle_count > 0)
339 dev->open_handle_count--;
340
341 if (dev->iamthif_file_object == file &&
342 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
343
344 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
345 dev->iamthif_state);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300346 dev->iamthif_canceled = true;
Oren Weilab841162011-05-15 13:43:41 +0300347 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
348 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200349 mei_amthif_run_next_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300350 }
351 }
352
353 if (mei_clear_lists(dev, file))
354 dev->iamthif_state = MEI_IAMTHIF_IDLE;
355
356 }
357 mutex_unlock(&dev->device_lock);
358 return rets;
359}
360
361
362/**
363 * mei_read - the read function.
364 *
365 * @file: pointer to file structure
366 * @ubuf: pointer to user buffer
367 * @length: buffer length
368 * @offset: data offset in buffer
369 *
370 * returns >=0 data length on success , <0 on error
371 */
372static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200373 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300374{
375 struct mei_cl *cl = file->private_data;
376 struct mei_cl_cb *cb_pos = NULL;
377 struct mei_cl_cb *cb = NULL;
378 struct mei_device *dev;
379 int i;
380 int rets;
381 int err;
382
383
384 if (WARN_ON(!cl || !cl->dev))
385 return -ENODEV;
386
387 dev = cl->dev;
388
389 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300390 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300391 rets = -ENODEV;
392 goto out;
393 }
394
395 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
396 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300397 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300398 if (i >= 0) {
399 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300400 if (cl->me_client_id == me_client->client_id) {
401 rets = -EBADF;
402 goto out;
403 }
404 }
405 } else {
406 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
407 }
408
409 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200410 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300411 goto out;
412 }
413
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200414 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300415 cb = cl->read_cb;
416 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200417 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
418 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300419 cb = cl->read_cb;
420 rets = 0;
421 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200422 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700423 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300424 *offset = 0;
425 rets = 0;
426 goto out;
427 }
428
429 err = mei_start_read(dev, cl);
430 if (err && err != -EBUSY) {
431 dev_dbg(&dev->pdev->dev,
432 "mei start read failure with status = %d\n", err);
433 rets = err;
434 goto out;
435 }
436
437 if (MEI_READ_COMPLETE != cl->reading_state &&
438 !waitqueue_active(&cl->rx_wait)) {
439 if (file->f_flags & O_NONBLOCK) {
440 rets = -EAGAIN;
441 goto out;
442 }
443
444 mutex_unlock(&dev->device_lock);
445
446 if (wait_event_interruptible(cl->rx_wait,
447 (MEI_READ_COMPLETE == cl->reading_state ||
448 MEI_FILE_INITIALIZING == cl->state ||
449 MEI_FILE_DISCONNECTED == cl->state ||
450 MEI_FILE_DISCONNECTING == cl->state))) {
451 if (signal_pending(current))
452 return -EINTR;
453 return -ERESTARTSYS;
454 }
455
456 mutex_lock(&dev->device_lock);
457 if (MEI_FILE_INITIALIZING == cl->state ||
458 MEI_FILE_DISCONNECTED == cl->state ||
459 MEI_FILE_DISCONNECTING == cl->state) {
460 rets = -EBUSY;
461 goto out;
462 }
463 }
464
465 cb = cl->read_cb;
466
467 if (!cb) {
468 rets = -ENODEV;
469 goto out;
470 }
471 if (cl->reading_state != MEI_READ_COMPLETE) {
472 rets = 0;
473 goto out;
474 }
475 /* now copy the data to user space */
476copy_buffer:
477 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
478 cb->response_buffer.size);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200479 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
480 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300481 rets = -EMSGSIZE;
482 goto free;
483 }
484
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200485 /* length is being truncated to PAGE_SIZE,
486 * however buf_idx may point beyond that */
487 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300488
Tomas Winkler441ab502011-12-13 23:39:34 +0200489 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300490 rets = -EFAULT;
491 goto free;
492 }
493
494 rets = length;
495 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200496 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300497 goto out;
498
499free:
500 cb_pos = find_read_list_entry(dev, cl);
501 /* Remove entry from read list */
502 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200503 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200504 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300505 cl->reading_state = MEI_IDLE;
506 cl->read_cb = NULL;
507 cl->read_pending = 0;
508out:
509 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
510 mutex_unlock(&dev->device_lock);
511 return rets;
512}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200513/**
Oren Weilab841162011-05-15 13:43:41 +0300514 * mei_write - the write function.
515 *
516 * @file: pointer to file structure
517 * @ubuf: pointer to user buffer
518 * @length: buffer length
519 * @offset: data offset in buffer
520 *
521 * returns >=0 data length on success , <0 on error
522 */
523static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200524 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300525{
526 struct mei_cl *cl = file->private_data;
527 struct mei_cl_cb *write_cb = NULL;
528 struct mei_msg_hdr mei_hdr;
529 struct mei_device *dev;
530 unsigned long timeout = 0;
531 int rets;
532 int i;
533
534 if (WARN_ON(!cl || !cl->dev))
535 return -ENODEV;
536
537 dev = cl->dev;
538
539 mutex_lock(&dev->device_lock);
540
Tomas Winklerb210d752012-08-07 00:03:56 +0300541 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200542 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200543 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300544 }
545
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200546 i = mei_me_cl_by_id(dev, cl->me_client_id);
547 if (i < 0) {
548 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200549 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200550 }
551 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
552 rets = -EMSGSIZE;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200553 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200554 }
555
556 if (cl->state != MEI_FILE_CONNECTED) {
557 rets = -ENODEV;
558 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
559 cl->host_client_id, cl->me_client_id);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200560 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200561 }
Oren Weilab841162011-05-15 13:43:41 +0300562 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200563 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300564
565 if (write_cb) {
566 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200567 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300568
569 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200570 cl->reading_state == MEI_READ_COMPLETE) {
571 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200572 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200573 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200574 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300575 }
576 }
577 }
578
579 /* free entry used in read */
580 if (cl->reading_state == MEI_READ_COMPLETE) {
581 *offset = 0;
582 write_cb = find_read_list_entry(dev, cl);
583 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200584 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200585 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300586 write_cb = NULL;
587 cl->reading_state = MEI_IDLE;
588 cl->read_cb = NULL;
589 cl->read_pending = 0;
590 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200591 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
Oren Weilab841162011-05-15 13:43:41 +0300592 *offset = 0;
593
594
Tomas Winkler33d28c92012-10-09 16:50:17 +0200595 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300596 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200597 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
598 rets = -ENOMEM;
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 rets = mei_io_cb_alloc_req_buf(write_cb, length);
602 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200603 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300604
Tomas Winkler33d28c92012-10-09 16:50:17 +0200605 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
Oren Weilab841162011-05-15 13:43:41 +0300606
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200607 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
608 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200609 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300610
611 cl->sm_state = 0;
612 if (length == 4 &&
613 ((memcmp(mei_wd_state_independence_msg[0],
614 write_cb->request_buffer.data, 4) == 0) ||
615 (memcmp(mei_wd_state_independence_msg[1],
616 write_cb->request_buffer.data, 4) == 0) ||
617 (memcmp(mei_wd_state_independence_msg[2],
618 write_cb->request_buffer.data, 4) == 0)))
619 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
620
Oren Weilab841162011-05-15 13:43:41 +0300621 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200622 rets = mei_amthif_write(dev, write_cb);
623
624 if (rets) {
625 dev_err(&dev->pdev->dev,
626 "amthi write failed with status = %d\n", rets);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200627 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300628 }
629 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200630 return length;
Oren Weilab841162011-05-15 13:43:41 +0300631 }
632
633 write_cb->major_file_operations = MEI_WRITE;
Oren Weilab841162011-05-15 13:43:41 +0300634
635 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
636 cl->host_client_id, cl->me_client_id);
Oren Weilab841162011-05-15 13:43:41 +0300637 rets = mei_flow_ctrl_creds(dev, cl);
638 if (rets < 0)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200639 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300640
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200641 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200642 write_cb->buf_idx = 0;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200643 mei_hdr.msg_complete = 0;
Oren Weilab841162011-05-15 13:43:41 +0300644 cl->writing_state = MEI_WRITING;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200645 goto out;
646 }
647
648 dev->mei_host_buffer_is_empty = false;
649 if (length > mei_hbuf_max_data(dev)) {
650 mei_hdr.length = mei_hbuf_max_data(dev);
651 mei_hdr.msg_complete = 0;
652 } else {
653 mei_hdr.length = length;
654 mei_hdr.msg_complete = 1;
655 }
656 mei_hdr.host_addr = cl->host_client_id;
657 mei_hdr.me_addr = cl->me_client_id;
658 mei_hdr.reserved = 0;
659 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
660 *((u32 *) &mei_hdr));
661 if (mei_write_message(dev, &mei_hdr,
662 write_cb->request_buffer.data, mei_hdr.length)) {
663 rets = -ENODEV;
664 goto err;
665 }
666 cl->writing_state = MEI_WRITING;
667 write_cb->buf_idx = mei_hdr.length;
668
669out:
670 if (mei_hdr.msg_complete) {
671 if (mei_flow_ctrl_reduce(dev, cl)) {
672 rets = -ENODEV;
673 goto err;
674 }
675 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
676 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200677 list_add_tail(&write_cb->list, &dev->write_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300678 }
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200679
Oren Weilab841162011-05-15 13:43:41 +0300680 mutex_unlock(&dev->device_lock);
681 return length;
682
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200683err:
Oren Weilab841162011-05-15 13:43:41 +0300684 mutex_unlock(&dev->device_lock);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200685 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300686 return rets;
687}
688
689
690/**
691 * mei_ioctl - the IOCTL function
692 *
693 * @file: pointer to file structure
694 * @cmd: ioctl command
695 * @data: pointer to mei message structure
696 *
697 * returns 0 on success , <0 on error
698 */
699static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
700{
701 struct mei_device *dev;
702 struct mei_cl *cl = file->private_data;
703 struct mei_connect_client_data *connect_data = NULL;
704 int rets;
705
706 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
707 return -EINVAL;
708
709 if (WARN_ON(!cl || !cl->dev))
710 return -ENODEV;
711
712 dev = cl->dev;
713
714 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
715
716 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300717 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300718 rets = -ENODEV;
719 goto out;
720 }
721
722 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
723
724 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
725 GFP_KERNEL);
726 if (!connect_data) {
727 rets = -ENOMEM;
728 goto out;
729 }
730 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
731 if (copy_from_user(connect_data, (char __user *)data,
732 sizeof(struct mei_connect_client_data))) {
733 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
734 rets = -EFAULT;
735 goto out;
736 }
737 rets = mei_ioctl_connect_client(file, connect_data);
738
739 /* if all is ok, copying the data back to user. */
740 if (rets)
741 goto out;
742
743 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
744 if (copy_to_user((char __user *)data, connect_data,
745 sizeof(struct mei_connect_client_data))) {
746 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
747 rets = -EFAULT;
748 goto out;
749 }
750
751out:
752 kfree(connect_data);
753 mutex_unlock(&dev->device_lock);
754 return rets;
755}
756
757/**
758 * mei_compat_ioctl - the compat IOCTL function
759 *
760 * @file: pointer to file structure
761 * @cmd: ioctl command
762 * @data: pointer to mei message structure
763 *
764 * returns 0 on success , <0 on error
765 */
766#ifdef CONFIG_COMPAT
767static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200768 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300769{
770 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
771}
772#endif
773
774
775/**
776 * mei_poll - the poll function
777 *
778 * @file: pointer to file structure
779 * @wait: pointer to poll_table structure
780 *
781 * returns poll mask
782 */
783static unsigned int mei_poll(struct file *file, poll_table *wait)
784{
785 struct mei_cl *cl = file->private_data;
786 struct mei_device *dev;
787 unsigned int mask = 0;
788
789 if (WARN_ON(!cl || !cl->dev))
790 return mask;
791
792 dev = cl->dev;
793
794 mutex_lock(&dev->device_lock);
795
Tomas Winklerb210d752012-08-07 00:03:56 +0300796 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300797 goto out;
798
799
800 if (cl == &dev->iamthif_cl) {
801 mutex_unlock(&dev->device_lock);
802 poll_wait(file, &dev->iamthif_cl.wait, wait);
803 mutex_lock(&dev->device_lock);
804 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
805 dev->iamthif_file_object == file) {
806 mask |= (POLLIN | POLLRDNORM);
807 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200808 mei_amthif_run_next_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300809 }
810 goto out;
811 }
812
813 mutex_unlock(&dev->device_lock);
814 poll_wait(file, &cl->tx_wait, wait);
815 mutex_lock(&dev->device_lock);
816 if (MEI_WRITE_COMPLETE == cl->writing_state)
817 mask |= (POLLIN | POLLRDNORM);
818
819out:
820 mutex_unlock(&dev->device_lock);
821 return mask;
822}
823
Oren Weil5b881e32011-11-13 09:41:14 +0200824/*
825 * file operations structure will be used for mei char device.
826 */
827static const struct file_operations mei_fops = {
828 .owner = THIS_MODULE,
829 .read = mei_read,
830 .unlocked_ioctl = mei_ioctl,
831#ifdef CONFIG_COMPAT
832 .compat_ioctl = mei_compat_ioctl,
833#endif
834 .open = mei_open,
835 .release = mei_release,
836 .write = mei_write,
837 .poll = mei_poll,
838 .llseek = no_llseek
839};
840
841
842/*
843 * Misc Device Struct
844 */
845static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300846 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200847 .fops = &mei_fops,
848 .minor = MISC_DYNAMIC_MINOR,
849};
850
851/**
Tomas Winkler9a123f12012-08-06 15:23:55 +0300852 * mei_quirk_probe - probe for devices that doesn't valid ME interface
853 * @pdev: PCI device structure
854 * @ent: entry into pci_device_table
855 *
856 * returns true if ME Interface is valid, false otherwise
857 */
858static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
859 const struct pci_device_id *ent)
860{
861 u32 reg;
862 if (ent->device == MEI_DEV_ID_PBG_1) {
863 pci_read_config_dword(pdev, 0x48, &reg);
864 /* make sure that bit 9 is up and bit 10 is down */
865 if ((reg & 0x600) == 0x200) {
866 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
867 return false;
868 }
869 }
870 return true;
871}
872/**
Oren Weil5b881e32011-11-13 09:41:14 +0200873 * mei_probe - Device Initialization Routine
874 *
875 * @pdev: PCI device structure
876 * @ent: entry in kcs_pci_tbl
877 *
878 * returns 0 on success, <0 on failure.
879 */
880static int __devinit mei_probe(struct pci_dev *pdev,
881 const struct pci_device_id *ent)
882{
883 struct mei_device *dev;
884 int err;
885
886 mutex_lock(&mei_mutex);
Tomas Winkler9a123f12012-08-06 15:23:55 +0300887
888 if (!mei_quirk_probe(pdev, ent)) {
889 err = -ENODEV;
890 goto end;
891 }
892
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300893 if (mei_pdev) {
Oren Weil5b881e32011-11-13 09:41:14 +0200894 err = -EEXIST;
895 goto end;
896 }
897 /* enable pci dev */
898 err = pci_enable_device(pdev);
899 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300900 dev_err(&pdev->dev, "failed to enable pci device.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200901 goto end;
902 }
903 /* set PCI host mastering */
904 pci_set_master(pdev);
905 /* pci request regions for mei driver */
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300906 err = pci_request_regions(pdev, KBUILD_MODNAME);
Oren Weil5b881e32011-11-13 09:41:14 +0200907 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300908 dev_err(&pdev->dev, "failed to get pci regions.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200909 goto disable_device;
910 }
911 /* allocates and initializes the mei dev structure */
912 dev = mei_device_init(pdev);
913 if (!dev) {
914 err = -ENOMEM;
915 goto release_regions;
916 }
917 /* mapping IO device memory */
918 dev->mem_addr = pci_iomap(pdev, 0, 0);
919 if (!dev->mem_addr) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300920 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200921 err = -ENOMEM;
922 goto free_device;
923 }
924 pci_enable_msi(pdev);
925
926 /* request and enable interrupt */
927 if (pci_dev_msi_enabled(pdev))
928 err = request_threaded_irq(pdev->irq,
929 NULL,
930 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300931 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200932 else
933 err = request_threaded_irq(pdev->irq,
934 mei_interrupt_quick_handler,
935 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300936 IRQF_SHARED, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200937
938 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300939 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
Oren Weil5b881e32011-11-13 09:41:14 +0200940 pdev->irq);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300941 goto disable_msi;
Oren Weil5b881e32011-11-13 09:41:14 +0200942 }
943 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
944 if (mei_hw_init(dev)) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300945 dev_err(&pdev->dev, "init hw failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200946 err = -ENODEV;
947 goto release_irq;
948 }
949
950 err = misc_register(&mei_misc_device);
951 if (err)
952 goto release_irq;
953
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300954 mei_pdev = pdev;
Oren Weil5b881e32011-11-13 09:41:14 +0200955 pci_set_drvdata(pdev, dev);
956
957
958 schedule_delayed_work(&dev->timer_work, HZ);
959
960 mutex_unlock(&mei_mutex);
961
Tomas Winkler2f3d2b42012-03-19 22:38:13 +0200962 pr_debug("initialization successful.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200963
964 return 0;
965
966release_irq:
967 /* disable interrupts */
968 dev->host_hw_state = mei_hcsr_read(dev);
969 mei_disable_interrupts(dev);
970 flush_scheduled_work();
971 free_irq(pdev->irq, dev);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300972disable_msi:
Oren Weil5b881e32011-11-13 09:41:14 +0200973 pci_disable_msi(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200974 pci_iounmap(pdev, dev->mem_addr);
975free_device:
976 kfree(dev);
977release_regions:
978 pci_release_regions(pdev);
979disable_device:
980 pci_disable_device(pdev);
981end:
982 mutex_unlock(&mei_mutex);
Tomas Winkler32c826b2012-05-08 23:04:56 +0300983 dev_err(&pdev->dev, "initialization failed.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200984 return err;
985}
986
987/**
988 * mei_remove - Device Removal Routine
989 *
990 * @pdev: PCI device structure
991 *
992 * mei_remove is called by the PCI subsystem to alert the driver
993 * that it should release a PCI device.
994 */
995static void __devexit mei_remove(struct pci_dev *pdev)
996{
997 struct mei_device *dev;
998
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300999 if (mei_pdev != pdev)
Oren Weil5b881e32011-11-13 09:41:14 +02001000 return;
1001
1002 dev = pci_get_drvdata(pdev);
1003 if (!dev)
1004 return;
1005
1006 mutex_lock(&dev->device_lock);
1007
Tomas Winklerc216fde2012-08-16 19:39:43 +03001008 cancel_delayed_work(&dev->timer_work);
1009
1010 mei_wd_stop(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001011
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001012 mei_pdev = NULL;
Oren Weil5b881e32011-11-13 09:41:14 +02001013
1014 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1015 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1016 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1017 }
1018 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1019 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1020 mei_disconnect_host_client(dev, &dev->wd_cl);
1021 }
1022
1023 /* Unregistering watchdog device */
Tomas Winkler70cd5332011-12-22 18:50:50 +02001024 mei_watchdog_unregister(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001025
1026 /* remove entry if already in list */
1027 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1028 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1029 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1030
1031 dev->iamthif_current_cb = NULL;
1032 dev->me_clients_num = 0;
1033
1034 mutex_unlock(&dev->device_lock);
1035
1036 flush_scheduled_work();
1037
1038 /* disable interrupts */
1039 mei_disable_interrupts(dev);
1040
1041 free_irq(pdev->irq, dev);
1042 pci_disable_msi(pdev);
1043 pci_set_drvdata(pdev, NULL);
1044
1045 if (dev->mem_addr)
1046 pci_iounmap(pdev, dev->mem_addr);
1047
1048 kfree(dev);
1049
1050 pci_release_regions(pdev);
1051 pci_disable_device(pdev);
Tomas Winklera44cab42012-05-29 16:39:11 +03001052
1053 misc_deregister(&mei_misc_device);
Oren Weil5b881e32011-11-13 09:41:14 +02001054}
Oren Weilab841162011-05-15 13:43:41 +03001055#ifdef CONFIG_PM
1056static int mei_pci_suspend(struct device *device)
1057{
1058 struct pci_dev *pdev = to_pci_dev(device);
1059 struct mei_device *dev = pci_get_drvdata(pdev);
1060 int err;
1061
1062 if (!dev)
1063 return -ENODEV;
1064 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +03001065
1066 cancel_delayed_work(&dev->timer_work);
1067
Oren Weilab841162011-05-15 13:43:41 +03001068 /* Stop watchdog if exists */
Tomas Winklerc216fde2012-08-16 19:39:43 +03001069 err = mei_wd_stop(dev);
Oren Weilab841162011-05-15 13:43:41 +03001070 /* Set new mei state */
Tomas Winklerb210d752012-08-07 00:03:56 +03001071 if (dev->dev_state == MEI_DEV_ENABLED ||
1072 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1073 dev->dev_state = MEI_DEV_POWER_DOWN;
Oren Weilab841162011-05-15 13:43:41 +03001074 mei_reset(dev, 0);
1075 }
1076 mutex_unlock(&dev->device_lock);
1077
1078 free_irq(pdev->irq, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001079 pci_disable_msi(pdev);
Oren Weilab841162011-05-15 13:43:41 +03001080
1081 return err;
1082}
1083
1084static int mei_pci_resume(struct device *device)
1085{
1086 struct pci_dev *pdev = to_pci_dev(device);
1087 struct mei_device *dev;
1088 int err;
1089
1090 dev = pci_get_drvdata(pdev);
1091 if (!dev)
1092 return -ENODEV;
1093
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001094 pci_enable_msi(pdev);
1095
1096 /* request and enable interrupt */
1097 if (pci_dev_msi_enabled(pdev))
1098 err = request_threaded_irq(pdev->irq,
1099 NULL,
1100 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001101 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001102 else
1103 err = request_threaded_irq(pdev->irq,
Oren Weilab841162011-05-15 13:43:41 +03001104 mei_interrupt_quick_handler,
1105 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001106 IRQF_SHARED, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001107
Oren Weilab841162011-05-15 13:43:41 +03001108 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001109 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1110 pdev->irq);
Oren Weilab841162011-05-15 13:43:41 +03001111 return err;
1112 }
1113
1114 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001115 dev->dev_state = MEI_DEV_POWER_UP;
Oren Weilab841162011-05-15 13:43:41 +03001116 mei_reset(dev, 1);
1117 mutex_unlock(&dev->device_lock);
1118
Oren Weil6d70e932011-09-07 09:03:14 +03001119 /* Start timer if stopped in suspend */
1120 schedule_delayed_work(&dev->timer_work, HZ);
1121
Oren Weilab841162011-05-15 13:43:41 +03001122 return err;
1123}
1124static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1125#define MEI_PM_OPS (&mei_pm_ops)
1126#else
Randy Dunlap2d990362011-05-19 08:52:34 -07001127#define MEI_PM_OPS NULL
Oren Weilab841162011-05-15 13:43:41 +03001128#endif /* CONFIG_PM */
1129/*
1130 * PCI driver structure
1131 */
1132static struct pci_driver mei_driver = {
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001133 .name = KBUILD_MODNAME,
Oren Weilab841162011-05-15 13:43:41 +03001134 .id_table = mei_pci_tbl,
1135 .probe = mei_probe,
1136 .remove = __devexit_p(mei_remove),
1137 .shutdown = __devexit_p(mei_remove),
1138 .driver.pm = MEI_PM_OPS,
1139};
1140
Tomas Winkler60781882012-07-19 09:45:32 +03001141module_pci_driver(mei_driver);
Oren Weilab841162011-05-15 13:43:41 +03001142
1143MODULE_AUTHOR("Intel Corporation");
1144MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1145MODULE_LICENSE("GPL v2");