blob: ff50cc14cc394d4a3e97e0de25a3ca907266c1ba [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 Winklerfb601ad2012-10-15 12:06:48 +0200149 mei_clear_list(dev, file, &dev->amthi_cmd_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300150 if (mei_clear_list(dev, file,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200151 &dev->amthi_read_complete_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300152 removed = true;
153
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200154 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300155
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200156 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300157 removed = true;
158
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200159 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300160 removed = true;
161
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200162 if (mei_clear_list(dev, file, &dev->write_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300163 removed = true;
164
165 /* check if iamthif_current_cb not NULL */
166 if (dev->iamthif_current_cb && !removed) {
167 /* check file and iamthif current cb association */
168 if (dev->iamthif_current_cb->file_object == file) {
169 /* remove cb */
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200170 mei_io_cb_free(dev->iamthif_current_cb);
Oren Weilab841162011-05-15 13:43:41 +0300171 dev->iamthif_current_cb = NULL;
172 removed = true;
173 }
174 }
175 return removed;
176}
177/**
178 * find_read_list_entry - find read list entry
179 *
180 * @dev: device structure
181 * @file: pointer to file structure
182 *
183 * returns cb on success, NULL on error
184 */
185static struct mei_cl_cb *find_read_list_entry(
186 struct mei_device *dev,
187 struct mei_cl *cl)
188{
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200189 struct mei_cl_cb *pos = NULL;
190 struct mei_cl_cb *next = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300191
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200192 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200193 list_for_each_entry_safe(pos, next, &dev->read_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200194 struct mei_cl *cl_temp;
195 cl_temp = (struct mei_cl *)pos->file_private;
Oren Weilab841162011-05-15 13:43:41 +0300196
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200197 if (mei_cl_cmp_id(cl, cl_temp))
198 return pos;
Oren Weilab841162011-05-15 13:43:41 +0300199 }
200 return NULL;
201}
202
203/**
204 * mei_open - the open function
205 *
206 * @inode: pointer to inode structure
207 * @file: pointer to file structure
208 *
209 * returns 0 on success, <0 on error
210 */
211static int mei_open(struct inode *inode, struct file *file)
212{
213 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +0300214 struct mei_device *dev;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200215 unsigned long cl_id;
216 int err;
Oren Weilab841162011-05-15 13:43:41 +0300217
218 err = -ENODEV;
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300219 if (!mei_pdev)
Oren Weilab841162011-05-15 13:43:41 +0300220 goto out;
221
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300222 dev = pci_get_drvdata(mei_pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200223 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +0300224 goto out;
225
226 mutex_lock(&dev->device_lock);
227 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300228 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +0300229 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +0400230 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +0300231
232 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +0300233 if (dev->dev_state != MEI_DEV_ENABLED) {
234 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
235 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +0300236 goto out_unlock;
237 }
238 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +0300239 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
240 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
241 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +0300242 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300243 }
Oren Weilab841162011-05-15 13:43:41 +0300244
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200245 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler1b812942012-09-11 00:43:20 +0300246 if (cl_id >= MEI_CLIENTS_MAX) {
247 dev_err(&dev->pdev->dev, "client_id exceded %d",
248 MEI_CLIENTS_MAX) ;
Oren Weilab841162011-05-15 13:43:41 +0300249 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300250 }
Oren Weilab841162011-05-15 13:43:41 +0300251
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200252 cl->host_client_id = cl_id;
253
Oren Weilab841162011-05-15 13:43:41 +0300254 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
255
256 dev->open_handle_count++;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200257
Oren Weilab841162011-05-15 13:43:41 +0300258 list_add_tail(&cl->link, &dev->file_list);
259
260 set_bit(cl->host_client_id, dev->host_clients_map);
261 cl->state = MEI_FILE_INITIALIZING;
262 cl->sm_state = 0;
263
264 file->private_data = cl;
265 mutex_unlock(&dev->device_lock);
266
Oren Weil5b881e32011-11-13 09:41:14 +0200267 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300268
269out_unlock:
270 mutex_unlock(&dev->device_lock);
271 kfree(cl);
272out:
273 return err;
274}
275
276/**
277 * mei_release - the release function
278 *
279 * @inode: pointer to inode structure
280 * @file: pointer to file structure
281 *
282 * returns 0 on success, <0 on error
283 */
284static int mei_release(struct inode *inode, struct file *file)
285{
286 struct mei_cl *cl = file->private_data;
287 struct mei_cl_cb *cb;
288 struct mei_device *dev;
289 int rets = 0;
290
291 if (WARN_ON(!cl || !cl->dev))
292 return -ENODEV;
293
294 dev = cl->dev;
295
296 mutex_lock(&dev->device_lock);
297 if (cl != &dev->iamthif_cl) {
298 if (cl->state == MEI_FILE_CONNECTED) {
299 cl->state = MEI_FILE_DISCONNECTING;
300 dev_dbg(&dev->pdev->dev,
301 "disconnecting client host client = %d, "
302 "ME client = %d\n",
303 cl->host_client_id,
304 cl->me_client_id);
305 rets = mei_disconnect_host_client(dev, cl);
306 }
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300307 mei_cl_flush_queues(cl);
Oren Weilab841162011-05-15 13:43:41 +0300308 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
309 cl->host_client_id,
310 cl->me_client_id);
311
312 if (dev->open_handle_count > 0) {
Tomas Winkler441ab502011-12-13 23:39:34 +0200313 clear_bit(cl->host_client_id, dev->host_clients_map);
Oren Weilab841162011-05-15 13:43:41 +0300314 dev->open_handle_count--;
315 }
316 mei_remove_client_from_file_list(dev, cl->host_client_id);
317
318 /* free read cb */
319 cb = NULL;
320 if (cl->read_cb) {
321 cb = find_read_list_entry(dev, cl);
322 /* Remove entry from read list */
323 if (cb)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200324 list_del(&cb->list);
Oren Weilab841162011-05-15 13:43:41 +0300325
326 cb = cl->read_cb;
327 cl->read_cb = NULL;
328 }
329
330 file->private_data = NULL;
331
332 if (cb) {
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200333 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300334 cb = NULL;
335 }
336
337 kfree(cl);
338 } else {
339 if (dev->open_handle_count > 0)
340 dev->open_handle_count--;
341
342 if (dev->iamthif_file_object == file &&
343 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
344
345 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
346 dev->iamthif_state);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300347 dev->iamthif_canceled = true;
Oren Weilab841162011-05-15 13:43:41 +0300348 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
349 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200350 mei_amthif_run_next_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300351 }
352 }
353
354 if (mei_clear_lists(dev, file))
355 dev->iamthif_state = MEI_IAMTHIF_IDLE;
356
357 }
358 mutex_unlock(&dev->device_lock);
359 return rets;
360}
361
362
363/**
364 * mei_read - the read function.
365 *
366 * @file: pointer to file structure
367 * @ubuf: pointer to user buffer
368 * @length: buffer length
369 * @offset: data offset in buffer
370 *
371 * returns >=0 data length on success , <0 on error
372 */
373static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200374 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300375{
376 struct mei_cl *cl = file->private_data;
377 struct mei_cl_cb *cb_pos = NULL;
378 struct mei_cl_cb *cb = NULL;
379 struct mei_device *dev;
380 int i;
381 int rets;
382 int err;
383
384
385 if (WARN_ON(!cl || !cl->dev))
386 return -ENODEV;
387
388 dev = cl->dev;
389
390 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300391 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300392 rets = -ENODEV;
393 goto out;
394 }
395
396 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
397 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300398 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300399 if (i >= 0) {
400 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300401 if (cl->me_client_id == me_client->client_id) {
402 rets = -EBADF;
403 goto out;
404 }
405 }
406 } else {
407 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
408 }
409
410 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200411 rets = mei_amthif_read(dev, file, ubuf, length, offset);
Oren Weilab841162011-05-15 13:43:41 +0300412 goto out;
413 }
414
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200415 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300416 cb = cl->read_cb;
417 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200418 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
419 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300420 cb = cl->read_cb;
421 rets = 0;
422 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200423 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700424 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300425 *offset = 0;
426 rets = 0;
427 goto out;
428 }
429
430 err = mei_start_read(dev, cl);
431 if (err && err != -EBUSY) {
432 dev_dbg(&dev->pdev->dev,
433 "mei start read failure with status = %d\n", err);
434 rets = err;
435 goto out;
436 }
437
438 if (MEI_READ_COMPLETE != cl->reading_state &&
439 !waitqueue_active(&cl->rx_wait)) {
440 if (file->f_flags & O_NONBLOCK) {
441 rets = -EAGAIN;
442 goto out;
443 }
444
445 mutex_unlock(&dev->device_lock);
446
447 if (wait_event_interruptible(cl->rx_wait,
448 (MEI_READ_COMPLETE == cl->reading_state ||
449 MEI_FILE_INITIALIZING == cl->state ||
450 MEI_FILE_DISCONNECTED == cl->state ||
451 MEI_FILE_DISCONNECTING == cl->state))) {
452 if (signal_pending(current))
453 return -EINTR;
454 return -ERESTARTSYS;
455 }
456
457 mutex_lock(&dev->device_lock);
458 if (MEI_FILE_INITIALIZING == cl->state ||
459 MEI_FILE_DISCONNECTED == cl->state ||
460 MEI_FILE_DISCONNECTING == cl->state) {
461 rets = -EBUSY;
462 goto out;
463 }
464 }
465
466 cb = cl->read_cb;
467
468 if (!cb) {
469 rets = -ENODEV;
470 goto out;
471 }
472 if (cl->reading_state != MEI_READ_COMPLETE) {
473 rets = 0;
474 goto out;
475 }
476 /* now copy the data to user space */
477copy_buffer:
478 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
479 cb->response_buffer.size);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200480 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
481 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300482 rets = -EMSGSIZE;
483 goto free;
484 }
485
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200486 /* length is being truncated to PAGE_SIZE,
487 * however buf_idx may point beyond that */
488 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300489
Tomas Winkler441ab502011-12-13 23:39:34 +0200490 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300491 rets = -EFAULT;
492 goto free;
493 }
494
495 rets = length;
496 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200497 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300498 goto out;
499
500free:
501 cb_pos = find_read_list_entry(dev, cl);
502 /* Remove entry from read list */
503 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200504 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200505 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300506 cl->reading_state = MEI_IDLE;
507 cl->read_cb = NULL;
508 cl->read_pending = 0;
509out:
510 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
511 mutex_unlock(&dev->device_lock);
512 return rets;
513}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200514/**
Oren Weilab841162011-05-15 13:43:41 +0300515 * mei_write - the write function.
516 *
517 * @file: pointer to file structure
518 * @ubuf: pointer to user buffer
519 * @length: buffer length
520 * @offset: data offset in buffer
521 *
522 * returns >=0 data length on success , <0 on error
523 */
524static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200525 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300526{
527 struct mei_cl *cl = file->private_data;
528 struct mei_cl_cb *write_cb = NULL;
529 struct mei_msg_hdr mei_hdr;
530 struct mei_device *dev;
531 unsigned long timeout = 0;
532 int rets;
533 int i;
534
535 if (WARN_ON(!cl || !cl->dev))
536 return -ENODEV;
537
538 dev = cl->dev;
539
540 mutex_lock(&dev->device_lock);
541
Tomas Winklerb210d752012-08-07 00:03:56 +0300542 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200543 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200544 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300545 }
546
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200547 i = mei_me_cl_by_id(dev, cl->me_client_id);
548 if (i < 0) {
549 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200550 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200551 }
552 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
553 rets = -EMSGSIZE;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200554 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200555 }
556
557 if (cl->state != MEI_FILE_CONNECTED) {
558 rets = -ENODEV;
559 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
560 cl->host_client_id, cl->me_client_id);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200561 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200562 }
Oren Weilab841162011-05-15 13:43:41 +0300563 if (cl == &dev->iamthif_cl) {
Tomas Winkler19838fb2012-11-01 21:17:15 +0200564 write_cb = mei_amthif_find_read_list_entry(dev, file);
Oren Weilab841162011-05-15 13:43:41 +0300565
566 if (write_cb) {
567 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200568 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300569
570 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200571 cl->reading_state == MEI_READ_COMPLETE) {
572 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200573 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200574 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200575 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300576 }
577 }
578 }
579
580 /* free entry used in read */
581 if (cl->reading_state == MEI_READ_COMPLETE) {
582 *offset = 0;
583 write_cb = find_read_list_entry(dev, cl);
584 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200585 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200586 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300587 write_cb = NULL;
588 cl->reading_state = MEI_IDLE;
589 cl->read_cb = NULL;
590 cl->read_pending = 0;
591 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200592 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
Oren Weilab841162011-05-15 13:43:41 +0300593 *offset = 0;
594
595
Tomas Winkler33d28c92012-10-09 16:50:17 +0200596 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300597 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200598 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
599 rets = -ENOMEM;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200600 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300601 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200602 rets = mei_io_cb_alloc_req_buf(write_cb, length);
603 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200604 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300605
Tomas Winkler33d28c92012-10-09 16:50:17 +0200606 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
Oren Weilab841162011-05-15 13:43:41 +0300607
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200608 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
609 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200610 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300611
612 cl->sm_state = 0;
613 if (length == 4 &&
614 ((memcmp(mei_wd_state_independence_msg[0],
615 write_cb->request_buffer.data, 4) == 0) ||
616 (memcmp(mei_wd_state_independence_msg[1],
617 write_cb->request_buffer.data, 4) == 0) ||
618 (memcmp(mei_wd_state_independence_msg[2],
619 write_cb->request_buffer.data, 4) == 0)))
620 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
621
Oren Weilab841162011-05-15 13:43:41 +0300622 if (cl == &dev->iamthif_cl) {
Tomas Winklerab5c4a52012-11-01 21:17:18 +0200623 rets = mei_amthif_write(dev, write_cb);
624
625 if (rets) {
626 dev_err(&dev->pdev->dev,
627 "amthi write failed with status = %d\n", rets);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200628 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300629 }
630 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200631 return length;
Oren Weilab841162011-05-15 13:43:41 +0300632 }
633
634 write_cb->major_file_operations = MEI_WRITE;
Oren Weilab841162011-05-15 13:43:41 +0300635
636 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
637 cl->host_client_id, cl->me_client_id);
Oren Weilab841162011-05-15 13:43:41 +0300638 rets = mei_flow_ctrl_creds(dev, cl);
639 if (rets < 0)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200640 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300641
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200642 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200643 write_cb->buf_idx = 0;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200644 mei_hdr.msg_complete = 0;
Oren Weilab841162011-05-15 13:43:41 +0300645 cl->writing_state = MEI_WRITING;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200646 goto out;
647 }
648
649 dev->mei_host_buffer_is_empty = false;
650 if (length > mei_hbuf_max_data(dev)) {
651 mei_hdr.length = mei_hbuf_max_data(dev);
652 mei_hdr.msg_complete = 0;
653 } else {
654 mei_hdr.length = length;
655 mei_hdr.msg_complete = 1;
656 }
657 mei_hdr.host_addr = cl->host_client_id;
658 mei_hdr.me_addr = cl->me_client_id;
659 mei_hdr.reserved = 0;
660 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
661 *((u32 *) &mei_hdr));
662 if (mei_write_message(dev, &mei_hdr,
663 write_cb->request_buffer.data, mei_hdr.length)) {
664 rets = -ENODEV;
665 goto err;
666 }
667 cl->writing_state = MEI_WRITING;
668 write_cb->buf_idx = mei_hdr.length;
669
670out:
671 if (mei_hdr.msg_complete) {
672 if (mei_flow_ctrl_reduce(dev, cl)) {
673 rets = -ENODEV;
674 goto err;
675 }
676 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
677 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200678 list_add_tail(&write_cb->list, &dev->write_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300679 }
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200680
Oren Weilab841162011-05-15 13:43:41 +0300681 mutex_unlock(&dev->device_lock);
682 return length;
683
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200684err:
Oren Weilab841162011-05-15 13:43:41 +0300685 mutex_unlock(&dev->device_lock);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200686 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300687 return rets;
688}
689
690
691/**
692 * mei_ioctl - the IOCTL function
693 *
694 * @file: pointer to file structure
695 * @cmd: ioctl command
696 * @data: pointer to mei message structure
697 *
698 * returns 0 on success , <0 on error
699 */
700static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
701{
702 struct mei_device *dev;
703 struct mei_cl *cl = file->private_data;
704 struct mei_connect_client_data *connect_data = NULL;
705 int rets;
706
707 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
708 return -EINVAL;
709
710 if (WARN_ON(!cl || !cl->dev))
711 return -ENODEV;
712
713 dev = cl->dev;
714
715 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
716
717 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300718 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300719 rets = -ENODEV;
720 goto out;
721 }
722
723 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
724
725 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
726 GFP_KERNEL);
727 if (!connect_data) {
728 rets = -ENOMEM;
729 goto out;
730 }
731 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
732 if (copy_from_user(connect_data, (char __user *)data,
733 sizeof(struct mei_connect_client_data))) {
734 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
735 rets = -EFAULT;
736 goto out;
737 }
738 rets = mei_ioctl_connect_client(file, connect_data);
739
740 /* if all is ok, copying the data back to user. */
741 if (rets)
742 goto out;
743
744 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
745 if (copy_to_user((char __user *)data, connect_data,
746 sizeof(struct mei_connect_client_data))) {
747 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
748 rets = -EFAULT;
749 goto out;
750 }
751
752out:
753 kfree(connect_data);
754 mutex_unlock(&dev->device_lock);
755 return rets;
756}
757
758/**
759 * mei_compat_ioctl - the compat IOCTL function
760 *
761 * @file: pointer to file structure
762 * @cmd: ioctl command
763 * @data: pointer to mei message structure
764 *
765 * returns 0 on success , <0 on error
766 */
767#ifdef CONFIG_COMPAT
768static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200769 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300770{
771 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
772}
773#endif
774
775
776/**
777 * mei_poll - the poll function
778 *
779 * @file: pointer to file structure
780 * @wait: pointer to poll_table structure
781 *
782 * returns poll mask
783 */
784static unsigned int mei_poll(struct file *file, poll_table *wait)
785{
786 struct mei_cl *cl = file->private_data;
787 struct mei_device *dev;
788 unsigned int mask = 0;
789
790 if (WARN_ON(!cl || !cl->dev))
791 return mask;
792
793 dev = cl->dev;
794
795 mutex_lock(&dev->device_lock);
796
Tomas Winklerb210d752012-08-07 00:03:56 +0300797 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300798 goto out;
799
800
801 if (cl == &dev->iamthif_cl) {
802 mutex_unlock(&dev->device_lock);
803 poll_wait(file, &dev->iamthif_cl.wait, wait);
804 mutex_lock(&dev->device_lock);
805 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
806 dev->iamthif_file_object == file) {
807 mask |= (POLLIN | POLLRDNORM);
808 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
Tomas Winkler19838fb2012-11-01 21:17:15 +0200809 mei_amthif_run_next_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300810 }
811 goto out;
812 }
813
814 mutex_unlock(&dev->device_lock);
815 poll_wait(file, &cl->tx_wait, wait);
816 mutex_lock(&dev->device_lock);
817 if (MEI_WRITE_COMPLETE == cl->writing_state)
818 mask |= (POLLIN | POLLRDNORM);
819
820out:
821 mutex_unlock(&dev->device_lock);
822 return mask;
823}
824
Oren Weil5b881e32011-11-13 09:41:14 +0200825/*
826 * file operations structure will be used for mei char device.
827 */
828static const struct file_operations mei_fops = {
829 .owner = THIS_MODULE,
830 .read = mei_read,
831 .unlocked_ioctl = mei_ioctl,
832#ifdef CONFIG_COMPAT
833 .compat_ioctl = mei_compat_ioctl,
834#endif
835 .open = mei_open,
836 .release = mei_release,
837 .write = mei_write,
838 .poll = mei_poll,
839 .llseek = no_llseek
840};
841
842
843/*
844 * Misc Device Struct
845 */
846static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300847 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200848 .fops = &mei_fops,
849 .minor = MISC_DYNAMIC_MINOR,
850};
851
852/**
Tomas Winkler9a123f12012-08-06 15:23:55 +0300853 * mei_quirk_probe - probe for devices that doesn't valid ME interface
854 * @pdev: PCI device structure
855 * @ent: entry into pci_device_table
856 *
857 * returns true if ME Interface is valid, false otherwise
858 */
859static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
860 const struct pci_device_id *ent)
861{
862 u32 reg;
863 if (ent->device == MEI_DEV_ID_PBG_1) {
864 pci_read_config_dword(pdev, 0x48, &reg);
865 /* make sure that bit 9 is up and bit 10 is down */
866 if ((reg & 0x600) == 0x200) {
867 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
868 return false;
869 }
870 }
871 return true;
872}
873/**
Oren Weil5b881e32011-11-13 09:41:14 +0200874 * mei_probe - Device Initialization Routine
875 *
876 * @pdev: PCI device structure
877 * @ent: entry in kcs_pci_tbl
878 *
879 * returns 0 on success, <0 on failure.
880 */
881static int __devinit mei_probe(struct pci_dev *pdev,
882 const struct pci_device_id *ent)
883{
884 struct mei_device *dev;
885 int err;
886
887 mutex_lock(&mei_mutex);
Tomas Winkler9a123f12012-08-06 15:23:55 +0300888
889 if (!mei_quirk_probe(pdev, ent)) {
890 err = -ENODEV;
891 goto end;
892 }
893
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300894 if (mei_pdev) {
Oren Weil5b881e32011-11-13 09:41:14 +0200895 err = -EEXIST;
896 goto end;
897 }
898 /* enable pci dev */
899 err = pci_enable_device(pdev);
900 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300901 dev_err(&pdev->dev, "failed to enable pci device.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200902 goto end;
903 }
904 /* set PCI host mastering */
905 pci_set_master(pdev);
906 /* pci request regions for mei driver */
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300907 err = pci_request_regions(pdev, KBUILD_MODNAME);
Oren Weil5b881e32011-11-13 09:41:14 +0200908 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300909 dev_err(&pdev->dev, "failed to get pci regions.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200910 goto disable_device;
911 }
912 /* allocates and initializes the mei dev structure */
913 dev = mei_device_init(pdev);
914 if (!dev) {
915 err = -ENOMEM;
916 goto release_regions;
917 }
918 /* mapping IO device memory */
919 dev->mem_addr = pci_iomap(pdev, 0, 0);
920 if (!dev->mem_addr) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300921 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200922 err = -ENOMEM;
923 goto free_device;
924 }
925 pci_enable_msi(pdev);
926
927 /* request and enable interrupt */
928 if (pci_dev_msi_enabled(pdev))
929 err = request_threaded_irq(pdev->irq,
930 NULL,
931 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300932 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200933 else
934 err = request_threaded_irq(pdev->irq,
935 mei_interrupt_quick_handler,
936 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300937 IRQF_SHARED, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200938
939 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300940 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
Oren Weil5b881e32011-11-13 09:41:14 +0200941 pdev->irq);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300942 goto disable_msi;
Oren Weil5b881e32011-11-13 09:41:14 +0200943 }
944 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
945 if (mei_hw_init(dev)) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300946 dev_err(&pdev->dev, "init hw failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200947 err = -ENODEV;
948 goto release_irq;
949 }
950
951 err = misc_register(&mei_misc_device);
952 if (err)
953 goto release_irq;
954
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300955 mei_pdev = pdev;
Oren Weil5b881e32011-11-13 09:41:14 +0200956 pci_set_drvdata(pdev, dev);
957
958
959 schedule_delayed_work(&dev->timer_work, HZ);
960
961 mutex_unlock(&mei_mutex);
962
Tomas Winkler2f3d2b42012-03-19 22:38:13 +0200963 pr_debug("initialization successful.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200964
965 return 0;
966
967release_irq:
968 /* disable interrupts */
969 dev->host_hw_state = mei_hcsr_read(dev);
970 mei_disable_interrupts(dev);
971 flush_scheduled_work();
972 free_irq(pdev->irq, dev);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300973disable_msi:
Oren Weil5b881e32011-11-13 09:41:14 +0200974 pci_disable_msi(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200975 pci_iounmap(pdev, dev->mem_addr);
976free_device:
977 kfree(dev);
978release_regions:
979 pci_release_regions(pdev);
980disable_device:
981 pci_disable_device(pdev);
982end:
983 mutex_unlock(&mei_mutex);
Tomas Winkler32c826b2012-05-08 23:04:56 +0300984 dev_err(&pdev->dev, "initialization failed.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200985 return err;
986}
987
988/**
989 * mei_remove - Device Removal Routine
990 *
991 * @pdev: PCI device structure
992 *
993 * mei_remove is called by the PCI subsystem to alert the driver
994 * that it should release a PCI device.
995 */
996static void __devexit mei_remove(struct pci_dev *pdev)
997{
998 struct mei_device *dev;
999
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001000 if (mei_pdev != pdev)
Oren Weil5b881e32011-11-13 09:41:14 +02001001 return;
1002
1003 dev = pci_get_drvdata(pdev);
1004 if (!dev)
1005 return;
1006
1007 mutex_lock(&dev->device_lock);
1008
Tomas Winklerc216fde2012-08-16 19:39:43 +03001009 cancel_delayed_work(&dev->timer_work);
1010
1011 mei_wd_stop(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001012
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001013 mei_pdev = NULL;
Oren Weil5b881e32011-11-13 09:41:14 +02001014
1015 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1016 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1017 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1018 }
1019 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1020 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1021 mei_disconnect_host_client(dev, &dev->wd_cl);
1022 }
1023
1024 /* Unregistering watchdog device */
Tomas Winkler70cd5332011-12-22 18:50:50 +02001025 mei_watchdog_unregister(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001026
1027 /* remove entry if already in list */
1028 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1029 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1030 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1031
1032 dev->iamthif_current_cb = NULL;
1033 dev->me_clients_num = 0;
1034
1035 mutex_unlock(&dev->device_lock);
1036
1037 flush_scheduled_work();
1038
1039 /* disable interrupts */
1040 mei_disable_interrupts(dev);
1041
1042 free_irq(pdev->irq, dev);
1043 pci_disable_msi(pdev);
1044 pci_set_drvdata(pdev, NULL);
1045
1046 if (dev->mem_addr)
1047 pci_iounmap(pdev, dev->mem_addr);
1048
1049 kfree(dev);
1050
1051 pci_release_regions(pdev);
1052 pci_disable_device(pdev);
Tomas Winklera44cab42012-05-29 16:39:11 +03001053
1054 misc_deregister(&mei_misc_device);
Oren Weil5b881e32011-11-13 09:41:14 +02001055}
Oren Weilab841162011-05-15 13:43:41 +03001056#ifdef CONFIG_PM
1057static int mei_pci_suspend(struct device *device)
1058{
1059 struct pci_dev *pdev = to_pci_dev(device);
1060 struct mei_device *dev = pci_get_drvdata(pdev);
1061 int err;
1062
1063 if (!dev)
1064 return -ENODEV;
1065 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +03001066
1067 cancel_delayed_work(&dev->timer_work);
1068
Oren Weilab841162011-05-15 13:43:41 +03001069 /* Stop watchdog if exists */
Tomas Winklerc216fde2012-08-16 19:39:43 +03001070 err = mei_wd_stop(dev);
Oren Weilab841162011-05-15 13:43:41 +03001071 /* Set new mei state */
Tomas Winklerb210d752012-08-07 00:03:56 +03001072 if (dev->dev_state == MEI_DEV_ENABLED ||
1073 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1074 dev->dev_state = MEI_DEV_POWER_DOWN;
Oren Weilab841162011-05-15 13:43:41 +03001075 mei_reset(dev, 0);
1076 }
1077 mutex_unlock(&dev->device_lock);
1078
1079 free_irq(pdev->irq, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001080 pci_disable_msi(pdev);
Oren Weilab841162011-05-15 13:43:41 +03001081
1082 return err;
1083}
1084
1085static int mei_pci_resume(struct device *device)
1086{
1087 struct pci_dev *pdev = to_pci_dev(device);
1088 struct mei_device *dev;
1089 int err;
1090
1091 dev = pci_get_drvdata(pdev);
1092 if (!dev)
1093 return -ENODEV;
1094
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001095 pci_enable_msi(pdev);
1096
1097 /* request and enable interrupt */
1098 if (pci_dev_msi_enabled(pdev))
1099 err = request_threaded_irq(pdev->irq,
1100 NULL,
1101 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001102 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001103 else
1104 err = request_threaded_irq(pdev->irq,
Oren Weilab841162011-05-15 13:43:41 +03001105 mei_interrupt_quick_handler,
1106 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001107 IRQF_SHARED, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001108
Oren Weilab841162011-05-15 13:43:41 +03001109 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001110 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1111 pdev->irq);
Oren Weilab841162011-05-15 13:43:41 +03001112 return err;
1113 }
1114
1115 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001116 dev->dev_state = MEI_DEV_POWER_UP;
Oren Weilab841162011-05-15 13:43:41 +03001117 mei_reset(dev, 1);
1118 mutex_unlock(&dev->device_lock);
1119
Oren Weil6d70e932011-09-07 09:03:14 +03001120 /* Start timer if stopped in suspend */
1121 schedule_delayed_work(&dev->timer_work, HZ);
1122
Oren Weilab841162011-05-15 13:43:41 +03001123 return err;
1124}
1125static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1126#define MEI_PM_OPS (&mei_pm_ops)
1127#else
Randy Dunlap2d990362011-05-19 08:52:34 -07001128#define MEI_PM_OPS NULL
Oren Weilab841162011-05-15 13:43:41 +03001129#endif /* CONFIG_PM */
1130/*
1131 * PCI driver structure
1132 */
1133static struct pci_driver mei_driver = {
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001134 .name = KBUILD_MODNAME,
Oren Weilab841162011-05-15 13:43:41 +03001135 .id_table = mei_pci_tbl,
1136 .probe = mei_probe,
1137 .remove = __devexit_p(mei_remove),
1138 .shutdown = __devexit_p(mei_remove),
1139 .driver.pm = MEI_PM_OPS,
1140};
1141
Tomas Winkler60781882012-07-19 09:45:32 +03001142module_pci_driver(mei_driver);
Oren Weilab841162011-05-15 13:43:41 +03001143
1144MODULE_AUTHOR("Intel Corporation");
1145MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1146MODULE_LICENSE("GPL v2");