blob: 659727a79012823479e2aa05d07eb82db9e54cbb [file] [log] [blame]
Oren Weilab841162011-05-15 13:43:41 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weilab841162011-05-15 13:43:41 +03005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
Tomas Winkler2f3d2b42012-03-19 22:38:13 +020017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Oren Weilab841162011-05-15 13:43:41 +030019#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/kernel.h>
22#include <linux/device.h>
23#include <linux/fs.h>
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/fcntl.h>
27#include <linux/aio.h>
28#include <linux/pci.h>
29#include <linux/poll.h>
30#include <linux/init.h>
31#include <linux/ioctl.h>
32#include <linux/cdev.h>
Oren Weilab841162011-05-15 13:43:41 +030033#include <linux/sched.h>
34#include <linux/uuid.h>
35#include <linux/compat.h>
36#include <linux/jiffies.h>
37#include <linux/interrupt.h>
Oren Weil5b881e32011-11-13 09:41:14 +020038#include <linux/miscdevice.h>
Oren Weilab841162011-05-15 13:43:41 +030039
40#include "mei_dev.h"
Tomas Winkler4f3afe12012-05-09 16:38:59 +030041#include <linux/mei.h>
Oren Weilab841162011-05-15 13:43:41 +030042#include "interface.h"
Oren Weilab841162011-05-15 13:43:41 +030043
Tomas Winklerdaed6b52012-08-17 09:54:23 +030044/* AMT device is a singleton on the platform */
45static struct pci_dev *mei_pdev;
Oren Weilab841162011-05-15 13:43:41 +030046
Oren Weilab841162011-05-15 13:43:41 +030047/* mei_pci_tbl - PCI Device ID Table */
48static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
49 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
50 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
Tomas Winkler9af51422012-08-29 01:15:50 +030080 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
Oren Weilab841162011-05-15 13:43:41 +030082
83 /* required last entry */
84 {0, }
85};
86
87MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
88
89static DEFINE_MUTEX(mei_mutex);
90
Oren Weilab841162011-05-15 13:43:41 +030091
92/**
93 * mei_clear_list - removes all callbacks associated with file
94 * from mei_cb_list
95 *
96 * @dev: device structure.
97 * @file: file structure
98 * @mei_cb_list: callbacks list
99 *
100 * mei_clear_list is called to clear resources associated with file
101 * when application calls close function or Ctrl-C was pressed
102 *
103 * returns true if callback removed from the list, false otherwise
104 */
105static bool mei_clear_list(struct mei_device *dev,
106 struct file *file, struct list_head *mei_cb_list)
107{
108 struct mei_cl_cb *cb_pos = NULL;
109 struct mei_cl_cb *cb_next = NULL;
110 struct file *file_temp;
111 bool removed = false;
112
113 /* list all list member */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200114 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) {
Oren Weilab841162011-05-15 13:43:41 +0300115 file_temp = (struct file *)cb_pos->file_object;
116 /* check if list member associated with a file */
117 if (file_temp == file) {
118 /* remove member from the list */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200119 list_del(&cb_pos->list);
Oren Weilab841162011-05-15 13:43:41 +0300120 /* check if cb equal to current iamthif cb */
121 if (dev->iamthif_current_cb == cb_pos) {
122 dev->iamthif_current_cb = NULL;
123 /* send flow control to iamthif client */
124 mei_send_flow_control(dev, &dev->iamthif_cl);
125 }
126 /* free all allocated buffers */
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200127 mei_io_cb_free(cb_pos);
Oren Weilab841162011-05-15 13:43:41 +0300128 cb_pos = NULL;
129 removed = true;
130 }
131 }
132 return removed;
133}
134
135/**
136 * mei_clear_lists - removes all callbacks associated with file
137 *
138 * @dev: device structure
139 * @file: file structure
140 *
141 * mei_clear_lists is called to clear resources associated with file
142 * when application calls close function or Ctrl-C was pressed
143 *
144 * returns true if callback removed from the list, false otherwise
145 */
146static bool mei_clear_lists(struct mei_device *dev, struct file *file)
147{
148 bool removed = false;
149
150 /* remove callbacks associated with a file */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200151 mei_clear_list(dev, file, &dev->amthi_cmd_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300152 if (mei_clear_list(dev, file,
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200153 &dev->amthi_read_complete_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300154 removed = true;
155
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200156 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300157
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200158 if (mei_clear_list(dev, file, &dev->ctrl_wr_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_waiting_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300162 removed = true;
163
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200164 if (mei_clear_list(dev, file, &dev->write_list.list))
Oren Weilab841162011-05-15 13:43:41 +0300165 removed = true;
166
167 /* check if iamthif_current_cb not NULL */
168 if (dev->iamthif_current_cb && !removed) {
169 /* check file and iamthif current cb association */
170 if (dev->iamthif_current_cb->file_object == file) {
171 /* remove cb */
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200172 mei_io_cb_free(dev->iamthif_current_cb);
Oren Weilab841162011-05-15 13:43:41 +0300173 dev->iamthif_current_cb = NULL;
174 removed = true;
175 }
176 }
177 return removed;
178}
179/**
180 * find_read_list_entry - find read list entry
181 *
182 * @dev: device structure
183 * @file: pointer to file structure
184 *
185 * returns cb on success, NULL on error
186 */
187static struct mei_cl_cb *find_read_list_entry(
188 struct mei_device *dev,
189 struct mei_cl *cl)
190{
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200191 struct mei_cl_cb *pos = NULL;
192 struct mei_cl_cb *next = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300193
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200194 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200195 list_for_each_entry_safe(pos, next, &dev->read_list.list, list) {
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200196 struct mei_cl *cl_temp;
197 cl_temp = (struct mei_cl *)pos->file_private;
Oren Weilab841162011-05-15 13:43:41 +0300198
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200199 if (mei_cl_cmp_id(cl, cl_temp))
200 return pos;
Oren Weilab841162011-05-15 13:43:41 +0300201 }
202 return NULL;
203}
204
205/**
206 * mei_open - the open function
207 *
208 * @inode: pointer to inode structure
209 * @file: pointer to file structure
210 *
211 * returns 0 on success, <0 on error
212 */
213static int mei_open(struct inode *inode, struct file *file)
214{
215 struct mei_cl *cl;
Oren Weilab841162011-05-15 13:43:41 +0300216 struct mei_device *dev;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200217 unsigned long cl_id;
218 int err;
Oren Weilab841162011-05-15 13:43:41 +0300219
220 err = -ENODEV;
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300221 if (!mei_pdev)
Oren Weilab841162011-05-15 13:43:41 +0300222 goto out;
223
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300224 dev = pci_get_drvdata(mei_pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200225 if (!dev)
Oren Weilab841162011-05-15 13:43:41 +0300226 goto out;
227
228 mutex_lock(&dev->device_lock);
229 err = -ENOMEM;
Tomas Winklerc95efb72011-05-25 17:28:21 +0300230 cl = mei_cl_allocate(dev);
Oren Weilab841162011-05-15 13:43:41 +0300231 if (!cl)
Alexey Khoroshilov303dfbf2011-08-31 00:41:14 +0400232 goto out_unlock;
Oren Weilab841162011-05-15 13:43:41 +0300233
234 err = -ENODEV;
Tomas Winklerb210d752012-08-07 00:03:56 +0300235 if (dev->dev_state != MEI_DEV_ENABLED) {
236 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
237 mei_dev_state_str(dev->dev_state));
Oren Weilab841162011-05-15 13:43:41 +0300238 goto out_unlock;
239 }
240 err = -EMFILE;
Tomas Winkler1b812942012-09-11 00:43:20 +0300241 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
242 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
243 MEI_MAX_OPEN_HANDLE_COUNT);
Oren Weilab841162011-05-15 13:43:41 +0300244 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300245 }
Oren Weilab841162011-05-15 13:43:41 +0300246
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200247 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
Tomas Winkler1b812942012-09-11 00:43:20 +0300248 if (cl_id >= MEI_CLIENTS_MAX) {
249 dev_err(&dev->pdev->dev, "client_id exceded %d",
250 MEI_CLIENTS_MAX) ;
Oren Weilab841162011-05-15 13:43:41 +0300251 goto out_unlock;
Tomas Winkler1b812942012-09-11 00:43:20 +0300252 }
Oren Weilab841162011-05-15 13:43:41 +0300253
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200254 cl->host_client_id = cl_id;
255
Oren Weilab841162011-05-15 13:43:41 +0300256 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
257
258 dev->open_handle_count++;
Tomas Winkler6f37aca2011-11-13 09:41:15 +0200259
Oren Weilab841162011-05-15 13:43:41 +0300260 list_add_tail(&cl->link, &dev->file_list);
261
262 set_bit(cl->host_client_id, dev->host_clients_map);
263 cl->state = MEI_FILE_INITIALIZING;
264 cl->sm_state = 0;
265
266 file->private_data = cl;
267 mutex_unlock(&dev->device_lock);
268
Oren Weil5b881e32011-11-13 09:41:14 +0200269 return nonseekable_open(inode, file);
Oren Weilab841162011-05-15 13:43:41 +0300270
271out_unlock:
272 mutex_unlock(&dev->device_lock);
273 kfree(cl);
274out:
275 return err;
276}
277
278/**
279 * mei_release - the release function
280 *
281 * @inode: pointer to inode structure
282 * @file: pointer to file structure
283 *
284 * returns 0 on success, <0 on error
285 */
286static int mei_release(struct inode *inode, struct file *file)
287{
288 struct mei_cl *cl = file->private_data;
289 struct mei_cl_cb *cb;
290 struct mei_device *dev;
291 int rets = 0;
292
293 if (WARN_ON(!cl || !cl->dev))
294 return -ENODEV;
295
296 dev = cl->dev;
297
298 mutex_lock(&dev->device_lock);
299 if (cl != &dev->iamthif_cl) {
300 if (cl->state == MEI_FILE_CONNECTED) {
301 cl->state = MEI_FILE_DISCONNECTING;
302 dev_dbg(&dev->pdev->dev,
303 "disconnecting client host client = %d, "
304 "ME client = %d\n",
305 cl->host_client_id,
306 cl->me_client_id);
307 rets = mei_disconnect_host_client(dev, cl);
308 }
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300309 mei_cl_flush_queues(cl);
Oren Weilab841162011-05-15 13:43:41 +0300310 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
311 cl->host_client_id,
312 cl->me_client_id);
313
314 if (dev->open_handle_count > 0) {
Tomas Winkler441ab502011-12-13 23:39:34 +0200315 clear_bit(cl->host_client_id, dev->host_clients_map);
Oren Weilab841162011-05-15 13:43:41 +0300316 dev->open_handle_count--;
317 }
318 mei_remove_client_from_file_list(dev, cl->host_client_id);
319
320 /* free read cb */
321 cb = NULL;
322 if (cl->read_cb) {
323 cb = find_read_list_entry(dev, cl);
324 /* Remove entry from read list */
325 if (cb)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200326 list_del(&cb->list);
Oren Weilab841162011-05-15 13:43:41 +0300327
328 cb = cl->read_cb;
329 cl->read_cb = NULL;
330 }
331
332 file->private_data = NULL;
333
334 if (cb) {
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200335 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300336 cb = NULL;
337 }
338
339 kfree(cl);
340 } else {
341 if (dev->open_handle_count > 0)
342 dev->open_handle_count--;
343
344 if (dev->iamthif_file_object == file &&
345 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
346
347 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
348 dev->iamthif_state);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300349 dev->iamthif_canceled = true;
Oren Weilab841162011-05-15 13:43:41 +0300350 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
351 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
Tomas Winklerc95efb72011-05-25 17:28:21 +0300352 mei_run_next_iamthif_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300353 }
354 }
355
356 if (mei_clear_lists(dev, file))
357 dev->iamthif_state = MEI_IAMTHIF_IDLE;
358
359 }
360 mutex_unlock(&dev->device_lock);
361 return rets;
362}
363
364
365/**
366 * mei_read - the read function.
367 *
368 * @file: pointer to file structure
369 * @ubuf: pointer to user buffer
370 * @length: buffer length
371 * @offset: data offset in buffer
372 *
373 * returns >=0 data length on success , <0 on error
374 */
375static ssize_t mei_read(struct file *file, char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200376 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300377{
378 struct mei_cl *cl = file->private_data;
379 struct mei_cl_cb *cb_pos = NULL;
380 struct mei_cl_cb *cb = NULL;
381 struct mei_device *dev;
382 int i;
383 int rets;
384 int err;
385
386
387 if (WARN_ON(!cl || !cl->dev))
388 return -ENODEV;
389
390 dev = cl->dev;
391
392 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300393 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300394 rets = -ENODEV;
395 goto out;
396 }
397
398 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
399 /* Do not allow to read watchdog client */
Tomas Winkler07b509b2012-07-23 14:05:39 +0300400 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
Oren Weilab841162011-05-15 13:43:41 +0300401 if (i >= 0) {
402 struct mei_me_client *me_client = &dev->me_clients[i];
Oren Weilab841162011-05-15 13:43:41 +0300403 if (cl->me_client_id == me_client->client_id) {
404 rets = -EBADF;
405 goto out;
406 }
407 }
408 } else {
409 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
410 }
411
412 if (cl == &dev->iamthif_cl) {
413 rets = amthi_read(dev, file, ubuf, length, offset);
414 goto out;
415 }
416
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200417 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300418 cb = cl->read_cb;
419 goto copy_buffer;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200420 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
421 cl->read_cb->buf_idx <= *offset) {
Oren Weilab841162011-05-15 13:43:41 +0300422 cb = cl->read_cb;
423 rets = 0;
424 goto free;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200425 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700426 /*Offset needs to be cleaned for contiguous reads*/
Oren Weilab841162011-05-15 13:43:41 +0300427 *offset = 0;
428 rets = 0;
429 goto out;
430 }
431
432 err = mei_start_read(dev, cl);
433 if (err && err != -EBUSY) {
434 dev_dbg(&dev->pdev->dev,
435 "mei start read failure with status = %d\n", err);
436 rets = err;
437 goto out;
438 }
439
440 if (MEI_READ_COMPLETE != cl->reading_state &&
441 !waitqueue_active(&cl->rx_wait)) {
442 if (file->f_flags & O_NONBLOCK) {
443 rets = -EAGAIN;
444 goto out;
445 }
446
447 mutex_unlock(&dev->device_lock);
448
449 if (wait_event_interruptible(cl->rx_wait,
450 (MEI_READ_COMPLETE == cl->reading_state ||
451 MEI_FILE_INITIALIZING == cl->state ||
452 MEI_FILE_DISCONNECTED == cl->state ||
453 MEI_FILE_DISCONNECTING == cl->state))) {
454 if (signal_pending(current))
455 return -EINTR;
456 return -ERESTARTSYS;
457 }
458
459 mutex_lock(&dev->device_lock);
460 if (MEI_FILE_INITIALIZING == cl->state ||
461 MEI_FILE_DISCONNECTED == cl->state ||
462 MEI_FILE_DISCONNECTING == cl->state) {
463 rets = -EBUSY;
464 goto out;
465 }
466 }
467
468 cb = cl->read_cb;
469
470 if (!cb) {
471 rets = -ENODEV;
472 goto out;
473 }
474 if (cl->reading_state != MEI_READ_COMPLETE) {
475 rets = 0;
476 goto out;
477 }
478 /* now copy the data to user space */
479copy_buffer:
480 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
481 cb->response_buffer.size);
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200482 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
483 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
Oren Weilab841162011-05-15 13:43:41 +0300484 rets = -EMSGSIZE;
485 goto free;
486 }
487
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200488 /* length is being truncated to PAGE_SIZE,
489 * however buf_idx may point beyond that */
490 length = min_t(size_t, length, cb->buf_idx - *offset);
Oren Weilab841162011-05-15 13:43:41 +0300491
Tomas Winkler441ab502011-12-13 23:39:34 +0200492 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
Oren Weilab841162011-05-15 13:43:41 +0300493 rets = -EFAULT;
494 goto free;
495 }
496
497 rets = length;
498 *offset += length;
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200499 if ((unsigned long)*offset < cb->buf_idx)
Oren Weilab841162011-05-15 13:43:41 +0300500 goto out;
501
502free:
503 cb_pos = find_read_list_entry(dev, cl);
504 /* Remove entry from read list */
505 if (cb_pos)
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200506 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200507 mei_io_cb_free(cb);
Oren Weilab841162011-05-15 13:43:41 +0300508 cl->reading_state = MEI_IDLE;
509 cl->read_cb = NULL;
510 cl->read_pending = 0;
511out:
512 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
513 mutex_unlock(&dev->device_lock);
514 return rets;
515}
Tomas Winkler33d28c92012-10-09 16:50:17 +0200516/**
Oren Weilab841162011-05-15 13:43:41 +0300517 * mei_write - the write function.
518 *
519 * @file: pointer to file structure
520 * @ubuf: pointer to user buffer
521 * @length: buffer length
522 * @offset: data offset in buffer
523 *
524 * returns >=0 data length on success , <0 on error
525 */
526static ssize_t mei_write(struct file *file, const char __user *ubuf,
Tomas Winkler441ab502011-12-13 23:39:34 +0200527 size_t length, loff_t *offset)
Oren Weilab841162011-05-15 13:43:41 +0300528{
529 struct mei_cl *cl = file->private_data;
530 struct mei_cl_cb *write_cb = NULL;
531 struct mei_msg_hdr mei_hdr;
532 struct mei_device *dev;
533 unsigned long timeout = 0;
534 int rets;
535 int i;
536
537 if (WARN_ON(!cl || !cl->dev))
538 return -ENODEV;
539
540 dev = cl->dev;
541
542 mutex_lock(&dev->device_lock);
543
Tomas Winklerb210d752012-08-07 00:03:56 +0300544 if (dev->dev_state != MEI_DEV_ENABLED) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200545 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200546 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300547 }
548
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200549 i = mei_me_cl_by_id(dev, cl->me_client_id);
550 if (i < 0) {
551 rets = -ENODEV;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200552 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200553 }
554 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
555 rets = -EMSGSIZE;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200556 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200557 }
558
559 if (cl->state != MEI_FILE_CONNECTED) {
560 rets = -ENODEV;
561 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
562 cl->host_client_id, cl->me_client_id);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200563 goto err;
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200564 }
Oren Weilab841162011-05-15 13:43:41 +0300565 if (cl == &dev->iamthif_cl) {
566 write_cb = find_amthi_read_list_entry(dev, file);
567
568 if (write_cb) {
569 timeout = write_cb->read_time +
Tomas Winkler3870c322012-11-01 21:17:14 +0200570 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilab841162011-05-15 13:43:41 +0300571
572 if (time_after(jiffies, timeout) ||
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200573 cl->reading_state == MEI_READ_COMPLETE) {
574 *offset = 0;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200575 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200576 mei_io_cb_free(write_cb);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200577 write_cb = NULL;
Oren Weilab841162011-05-15 13:43:41 +0300578 }
579 }
580 }
581
582 /* free entry used in read */
583 if (cl->reading_state == MEI_READ_COMPLETE) {
584 *offset = 0;
585 write_cb = find_read_list_entry(dev, cl);
586 if (write_cb) {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200587 list_del(&write_cb->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200588 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300589 write_cb = NULL;
590 cl->reading_state = MEI_IDLE;
591 cl->read_cb = NULL;
592 cl->read_pending = 0;
593 }
Tomas Winkler441ab502011-12-13 23:39:34 +0200594 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
Oren Weilab841162011-05-15 13:43:41 +0300595 *offset = 0;
596
597
Tomas Winkler33d28c92012-10-09 16:50:17 +0200598 write_cb = mei_io_cb_init(cl, file);
Oren Weilab841162011-05-15 13:43:41 +0300599 if (!write_cb) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200600 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
601 rets = -ENOMEM;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200602 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300603 }
Tomas Winkler33d28c92012-10-09 16:50:17 +0200604 rets = mei_io_cb_alloc_req_buf(write_cb, length);
605 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200606 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300607
Tomas Winkler33d28c92012-10-09 16:50:17 +0200608 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
Oren Weilab841162011-05-15 13:43:41 +0300609
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200610 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
611 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200612 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300613
614 cl->sm_state = 0;
615 if (length == 4 &&
616 ((memcmp(mei_wd_state_independence_msg[0],
617 write_cb->request_buffer.data, 4) == 0) ||
618 (memcmp(mei_wd_state_independence_msg[1],
619 write_cb->request_buffer.data, 4) == 0) ||
620 (memcmp(mei_wd_state_independence_msg[2],
621 write_cb->request_buffer.data, 4) == 0)))
622 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
623
Oren Weilab841162011-05-15 13:43:41 +0300624 if (cl == &dev->iamthif_cl) {
Tomas Winkler33d28c92012-10-09 16:50:17 +0200625 rets = mei_io_cb_alloc_resp_buf(write_cb, dev->iamthif_mtu);
626 if (rets)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200627 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300628
Oren Weilab841162011-05-15 13:43:41 +0300629 write_cb->major_file_operations = MEI_IOCTL;
Oren Weilab841162011-05-15 13:43:41 +0300630
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200631 if (!list_empty(&dev->amthi_cmd_list.list) ||
Oren Weilab841162011-05-15 13:43:41 +0300632 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
633 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
634 (int) dev->iamthif_state);
635 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200636 list_add_tail(&write_cb->list, &dev->amthi_cmd_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300637 } else {
638 dev_dbg(&dev->pdev->dev, "call amthi write\n");
639 rets = amthi_write(dev, write_cb);
640
641 if (rets) {
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200642 dev_err(&dev->pdev->dev, "amthi write failed with status = %d\n",
Oren Weilab841162011-05-15 13:43:41 +0300643 rets);
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200644 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300645 }
Oren Weilab841162011-05-15 13:43:41 +0300646 }
647 mutex_unlock(&dev->device_lock);
Tomas Winkler75f0ee12012-10-09 16:50:18 +0200648 return length;
Oren Weilab841162011-05-15 13:43:41 +0300649 }
650
651 write_cb->major_file_operations = MEI_WRITE;
Oren Weilab841162011-05-15 13:43:41 +0300652
653 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
654 cl->host_client_id, cl->me_client_id);
Oren Weilab841162011-05-15 13:43:41 +0300655 rets = mei_flow_ctrl_creds(dev, cl);
656 if (rets < 0)
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200657 goto err;
Oren Weilab841162011-05-15 13:43:41 +0300658
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200659 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
Tomas Winklerebb108ef2012-10-09 16:50:16 +0200660 write_cb->buf_idx = 0;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200661 mei_hdr.msg_complete = 0;
Oren Weilab841162011-05-15 13:43:41 +0300662 cl->writing_state = MEI_WRITING;
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200663 goto out;
664 }
665
666 dev->mei_host_buffer_is_empty = false;
667 if (length > mei_hbuf_max_data(dev)) {
668 mei_hdr.length = mei_hbuf_max_data(dev);
669 mei_hdr.msg_complete = 0;
670 } else {
671 mei_hdr.length = length;
672 mei_hdr.msg_complete = 1;
673 }
674 mei_hdr.host_addr = cl->host_client_id;
675 mei_hdr.me_addr = cl->me_client_id;
676 mei_hdr.reserved = 0;
677 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
678 *((u32 *) &mei_hdr));
679 if (mei_write_message(dev, &mei_hdr,
680 write_cb->request_buffer.data, mei_hdr.length)) {
681 rets = -ENODEV;
682 goto err;
683 }
684 cl->writing_state = MEI_WRITING;
685 write_cb->buf_idx = mei_hdr.length;
686
687out:
688 if (mei_hdr.msg_complete) {
689 if (mei_flow_ctrl_reduce(dev, cl)) {
690 rets = -ENODEV;
691 goto err;
692 }
693 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
694 } else {
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200695 list_add_tail(&write_cb->list, &dev->write_list.list);
Oren Weilab841162011-05-15 13:43:41 +0300696 }
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200697
Oren Weilab841162011-05-15 13:43:41 +0300698 mutex_unlock(&dev->device_lock);
699 return length;
700
Tomas Winklerb0d0cf72012-11-01 21:17:13 +0200701err:
Oren Weilab841162011-05-15 13:43:41 +0300702 mutex_unlock(&dev->device_lock);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200703 mei_io_cb_free(write_cb);
Oren Weilab841162011-05-15 13:43:41 +0300704 return rets;
705}
706
707
708/**
709 * mei_ioctl - the IOCTL function
710 *
711 * @file: pointer to file structure
712 * @cmd: ioctl command
713 * @data: pointer to mei message structure
714 *
715 * returns 0 on success , <0 on error
716 */
717static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
718{
719 struct mei_device *dev;
720 struct mei_cl *cl = file->private_data;
721 struct mei_connect_client_data *connect_data = NULL;
722 int rets;
723
724 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
725 return -EINVAL;
726
727 if (WARN_ON(!cl || !cl->dev))
728 return -ENODEV;
729
730 dev = cl->dev;
731
732 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
733
734 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +0300735 if (dev->dev_state != MEI_DEV_ENABLED) {
Oren Weilab841162011-05-15 13:43:41 +0300736 rets = -ENODEV;
737 goto out;
738 }
739
740 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
741
742 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
743 GFP_KERNEL);
744 if (!connect_data) {
745 rets = -ENOMEM;
746 goto out;
747 }
748 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
749 if (copy_from_user(connect_data, (char __user *)data,
750 sizeof(struct mei_connect_client_data))) {
751 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
752 rets = -EFAULT;
753 goto out;
754 }
755 rets = mei_ioctl_connect_client(file, connect_data);
756
757 /* if all is ok, copying the data back to user. */
758 if (rets)
759 goto out;
760
761 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
762 if (copy_to_user((char __user *)data, connect_data,
763 sizeof(struct mei_connect_client_data))) {
764 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
765 rets = -EFAULT;
766 goto out;
767 }
768
769out:
770 kfree(connect_data);
771 mutex_unlock(&dev->device_lock);
772 return rets;
773}
774
775/**
776 * mei_compat_ioctl - the compat IOCTL function
777 *
778 * @file: pointer to file structure
779 * @cmd: ioctl command
780 * @data: pointer to mei message structure
781 *
782 * returns 0 on success , <0 on error
783 */
784#ifdef CONFIG_COMPAT
785static long mei_compat_ioctl(struct file *file,
Tomas Winkler441ab502011-12-13 23:39:34 +0200786 unsigned int cmd, unsigned long data)
Oren Weilab841162011-05-15 13:43:41 +0300787{
788 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
789}
790#endif
791
792
793/**
794 * mei_poll - the poll function
795 *
796 * @file: pointer to file structure
797 * @wait: pointer to poll_table structure
798 *
799 * returns poll mask
800 */
801static unsigned int mei_poll(struct file *file, poll_table *wait)
802{
803 struct mei_cl *cl = file->private_data;
804 struct mei_device *dev;
805 unsigned int mask = 0;
806
807 if (WARN_ON(!cl || !cl->dev))
808 return mask;
809
810 dev = cl->dev;
811
812 mutex_lock(&dev->device_lock);
813
Tomas Winklerb210d752012-08-07 00:03:56 +0300814 if (dev->dev_state != MEI_DEV_ENABLED)
Oren Weilab841162011-05-15 13:43:41 +0300815 goto out;
816
817
818 if (cl == &dev->iamthif_cl) {
819 mutex_unlock(&dev->device_lock);
820 poll_wait(file, &dev->iamthif_cl.wait, wait);
821 mutex_lock(&dev->device_lock);
822 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
823 dev->iamthif_file_object == file) {
824 mask |= (POLLIN | POLLRDNORM);
825 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
Tomas Winklerc95efb72011-05-25 17:28:21 +0300826 mei_run_next_iamthif_cmd(dev);
Oren Weilab841162011-05-15 13:43:41 +0300827 }
828 goto out;
829 }
830
831 mutex_unlock(&dev->device_lock);
832 poll_wait(file, &cl->tx_wait, wait);
833 mutex_lock(&dev->device_lock);
834 if (MEI_WRITE_COMPLETE == cl->writing_state)
835 mask |= (POLLIN | POLLRDNORM);
836
837out:
838 mutex_unlock(&dev->device_lock);
839 return mask;
840}
841
Oren Weil5b881e32011-11-13 09:41:14 +0200842/*
843 * file operations structure will be used for mei char device.
844 */
845static const struct file_operations mei_fops = {
846 .owner = THIS_MODULE,
847 .read = mei_read,
848 .unlocked_ioctl = mei_ioctl,
849#ifdef CONFIG_COMPAT
850 .compat_ioctl = mei_compat_ioctl,
851#endif
852 .open = mei_open,
853 .release = mei_release,
854 .write = mei_write,
855 .poll = mei_poll,
856 .llseek = no_llseek
857};
858
859
860/*
861 * Misc Device Struct
862 */
863static struct miscdevice mei_misc_device = {
Tomas Winklerc38ea242012-04-02 20:32:39 +0300864 .name = "mei",
Oren Weil5b881e32011-11-13 09:41:14 +0200865 .fops = &mei_fops,
866 .minor = MISC_DYNAMIC_MINOR,
867};
868
869/**
Tomas Winkler9a123f12012-08-06 15:23:55 +0300870 * mei_quirk_probe - probe for devices that doesn't valid ME interface
871 * @pdev: PCI device structure
872 * @ent: entry into pci_device_table
873 *
874 * returns true if ME Interface is valid, false otherwise
875 */
876static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
877 const struct pci_device_id *ent)
878{
879 u32 reg;
880 if (ent->device == MEI_DEV_ID_PBG_1) {
881 pci_read_config_dword(pdev, 0x48, &reg);
882 /* make sure that bit 9 is up and bit 10 is down */
883 if ((reg & 0x600) == 0x200) {
884 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
885 return false;
886 }
887 }
888 return true;
889}
890/**
Oren Weil5b881e32011-11-13 09:41:14 +0200891 * mei_probe - Device Initialization Routine
892 *
893 * @pdev: PCI device structure
894 * @ent: entry in kcs_pci_tbl
895 *
896 * returns 0 on success, <0 on failure.
897 */
898static int __devinit mei_probe(struct pci_dev *pdev,
899 const struct pci_device_id *ent)
900{
901 struct mei_device *dev;
902 int err;
903
904 mutex_lock(&mei_mutex);
Tomas Winkler9a123f12012-08-06 15:23:55 +0300905
906 if (!mei_quirk_probe(pdev, ent)) {
907 err = -ENODEV;
908 goto end;
909 }
910
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300911 if (mei_pdev) {
Oren Weil5b881e32011-11-13 09:41:14 +0200912 err = -EEXIST;
913 goto end;
914 }
915 /* enable pci dev */
916 err = pci_enable_device(pdev);
917 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300918 dev_err(&pdev->dev, "failed to enable pci device.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200919 goto end;
920 }
921 /* set PCI host mastering */
922 pci_set_master(pdev);
923 /* pci request regions for mei driver */
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300924 err = pci_request_regions(pdev, KBUILD_MODNAME);
Oren Weil5b881e32011-11-13 09:41:14 +0200925 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300926 dev_err(&pdev->dev, "failed to get pci regions.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200927 goto disable_device;
928 }
929 /* allocates and initializes the mei dev structure */
930 dev = mei_device_init(pdev);
931 if (!dev) {
932 err = -ENOMEM;
933 goto release_regions;
934 }
935 /* mapping IO device memory */
936 dev->mem_addr = pci_iomap(pdev, 0, 0);
937 if (!dev->mem_addr) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300938 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200939 err = -ENOMEM;
940 goto free_device;
941 }
942 pci_enable_msi(pdev);
943
944 /* request and enable interrupt */
945 if (pci_dev_msi_enabled(pdev))
946 err = request_threaded_irq(pdev->irq,
947 NULL,
948 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300949 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200950 else
951 err = request_threaded_irq(pdev->irq,
952 mei_interrupt_quick_handler,
953 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +0300954 IRQF_SHARED, KBUILD_MODNAME, dev);
Oren Weil5b881e32011-11-13 09:41:14 +0200955
956 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300957 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
Oren Weil5b881e32011-11-13 09:41:14 +0200958 pdev->irq);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300959 goto disable_msi;
Oren Weil5b881e32011-11-13 09:41:14 +0200960 }
961 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
962 if (mei_hw_init(dev)) {
Tomas Winkler32c826b2012-05-08 23:04:56 +0300963 dev_err(&pdev->dev, "init hw failure.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200964 err = -ENODEV;
965 goto release_irq;
966 }
967
968 err = misc_register(&mei_misc_device);
969 if (err)
970 goto release_irq;
971
Tomas Winklerdaed6b52012-08-17 09:54:23 +0300972 mei_pdev = pdev;
Oren Weil5b881e32011-11-13 09:41:14 +0200973 pci_set_drvdata(pdev, dev);
974
975
976 schedule_delayed_work(&dev->timer_work, HZ);
977
978 mutex_unlock(&mei_mutex);
979
Tomas Winkler2f3d2b42012-03-19 22:38:13 +0200980 pr_debug("initialization successful.\n");
Oren Weil5b881e32011-11-13 09:41:14 +0200981
982 return 0;
983
984release_irq:
985 /* disable interrupts */
986 dev->host_hw_state = mei_hcsr_read(dev);
987 mei_disable_interrupts(dev);
988 flush_scheduled_work();
989 free_irq(pdev->irq, dev);
Samuel Ortiz169dc382012-06-11 12:18:30 +0300990disable_msi:
Oren Weil5b881e32011-11-13 09:41:14 +0200991 pci_disable_msi(pdev);
Oren Weil5b881e32011-11-13 09:41:14 +0200992 pci_iounmap(pdev, dev->mem_addr);
993free_device:
994 kfree(dev);
995release_regions:
996 pci_release_regions(pdev);
997disable_device:
998 pci_disable_device(pdev);
999end:
1000 mutex_unlock(&mei_mutex);
Tomas Winkler32c826b2012-05-08 23:04:56 +03001001 dev_err(&pdev->dev, "initialization failed.\n");
Oren Weil5b881e32011-11-13 09:41:14 +02001002 return err;
1003}
1004
1005/**
1006 * mei_remove - Device Removal Routine
1007 *
1008 * @pdev: PCI device structure
1009 *
1010 * mei_remove is called by the PCI subsystem to alert the driver
1011 * that it should release a PCI device.
1012 */
1013static void __devexit mei_remove(struct pci_dev *pdev)
1014{
1015 struct mei_device *dev;
1016
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001017 if (mei_pdev != pdev)
Oren Weil5b881e32011-11-13 09:41:14 +02001018 return;
1019
1020 dev = pci_get_drvdata(pdev);
1021 if (!dev)
1022 return;
1023
1024 mutex_lock(&dev->device_lock);
1025
Tomas Winklerc216fde2012-08-16 19:39:43 +03001026 cancel_delayed_work(&dev->timer_work);
1027
1028 mei_wd_stop(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001029
Tomas Winklerdaed6b52012-08-17 09:54:23 +03001030 mei_pdev = NULL;
Oren Weil5b881e32011-11-13 09:41:14 +02001031
1032 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1033 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1034 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1035 }
1036 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1037 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1038 mei_disconnect_host_client(dev, &dev->wd_cl);
1039 }
1040
1041 /* Unregistering watchdog device */
Tomas Winkler70cd5332011-12-22 18:50:50 +02001042 mei_watchdog_unregister(dev);
Oren Weil5b881e32011-11-13 09:41:14 +02001043
1044 /* remove entry if already in list */
1045 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1046 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1047 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1048
1049 dev->iamthif_current_cb = NULL;
1050 dev->me_clients_num = 0;
1051
1052 mutex_unlock(&dev->device_lock);
1053
1054 flush_scheduled_work();
1055
1056 /* disable interrupts */
1057 mei_disable_interrupts(dev);
1058
1059 free_irq(pdev->irq, dev);
1060 pci_disable_msi(pdev);
1061 pci_set_drvdata(pdev, NULL);
1062
1063 if (dev->mem_addr)
1064 pci_iounmap(pdev, dev->mem_addr);
1065
1066 kfree(dev);
1067
1068 pci_release_regions(pdev);
1069 pci_disable_device(pdev);
Tomas Winklera44cab42012-05-29 16:39:11 +03001070
1071 misc_deregister(&mei_misc_device);
Oren Weil5b881e32011-11-13 09:41:14 +02001072}
Oren Weilab841162011-05-15 13:43:41 +03001073#ifdef CONFIG_PM
1074static int mei_pci_suspend(struct device *device)
1075{
1076 struct pci_dev *pdev = to_pci_dev(device);
1077 struct mei_device *dev = pci_get_drvdata(pdev);
1078 int err;
1079
1080 if (!dev)
1081 return -ENODEV;
1082 mutex_lock(&dev->device_lock);
Tomas Winklerc216fde2012-08-16 19:39:43 +03001083
1084 cancel_delayed_work(&dev->timer_work);
1085
Oren Weilab841162011-05-15 13:43:41 +03001086 /* Stop watchdog if exists */
Tomas Winklerc216fde2012-08-16 19:39:43 +03001087 err = mei_wd_stop(dev);
Oren Weilab841162011-05-15 13:43:41 +03001088 /* Set new mei state */
Tomas Winklerb210d752012-08-07 00:03:56 +03001089 if (dev->dev_state == MEI_DEV_ENABLED ||
1090 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1091 dev->dev_state = MEI_DEV_POWER_DOWN;
Oren Weilab841162011-05-15 13:43:41 +03001092 mei_reset(dev, 0);
1093 }
1094 mutex_unlock(&dev->device_lock);
1095
1096 free_irq(pdev->irq, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001097 pci_disable_msi(pdev);
Oren Weilab841162011-05-15 13:43:41 +03001098
1099 return err;
1100}
1101
1102static int mei_pci_resume(struct device *device)
1103{
1104 struct pci_dev *pdev = to_pci_dev(device);
1105 struct mei_device *dev;
1106 int err;
1107
1108 dev = pci_get_drvdata(pdev);
1109 if (!dev)
1110 return -ENODEV;
1111
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001112 pci_enable_msi(pdev);
1113
1114 /* request and enable interrupt */
1115 if (pci_dev_msi_enabled(pdev))
1116 err = request_threaded_irq(pdev->irq,
1117 NULL,
1118 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001119 IRQF_ONESHOT, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001120 else
1121 err = request_threaded_irq(pdev->irq,
Oren Weilab841162011-05-15 13:43:41 +03001122 mei_interrupt_quick_handler,
1123 mei_interrupt_thread_handler,
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001124 IRQF_SHARED, KBUILD_MODNAME, dev);
Tomas Winkler4f61a7a2011-07-14 20:11:25 +03001125
Oren Weilab841162011-05-15 13:43:41 +03001126 if (err) {
Tomas Winkler32c826b2012-05-08 23:04:56 +03001127 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1128 pdev->irq);
Oren Weilab841162011-05-15 13:43:41 +03001129 return err;
1130 }
1131
1132 mutex_lock(&dev->device_lock);
Tomas Winklerb210d752012-08-07 00:03:56 +03001133 dev->dev_state = MEI_DEV_POWER_UP;
Oren Weilab841162011-05-15 13:43:41 +03001134 mei_reset(dev, 1);
1135 mutex_unlock(&dev->device_lock);
1136
Oren Weil6d70e932011-09-07 09:03:14 +03001137 /* Start timer if stopped in suspend */
1138 schedule_delayed_work(&dev->timer_work, HZ);
1139
Oren Weilab841162011-05-15 13:43:41 +03001140 return err;
1141}
1142static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1143#define MEI_PM_OPS (&mei_pm_ops)
1144#else
Randy Dunlap2d990362011-05-19 08:52:34 -07001145#define MEI_PM_OPS NULL
Oren Weilab841162011-05-15 13:43:41 +03001146#endif /* CONFIG_PM */
1147/*
1148 * PCI driver structure
1149 */
1150static struct pci_driver mei_driver = {
Tomas Winkler068c0ae2012-08-07 00:03:54 +03001151 .name = KBUILD_MODNAME,
Oren Weilab841162011-05-15 13:43:41 +03001152 .id_table = mei_pci_tbl,
1153 .probe = mei_probe,
1154 .remove = __devexit_p(mei_remove),
1155 .shutdown = __devexit_p(mei_remove),
1156 .driver.pm = MEI_PM_OPS,
1157};
1158
Tomas Winkler60781882012-07-19 09:45:32 +03001159module_pci_driver(mei_driver);
Oren Weilab841162011-05-15 13:43:41 +03001160
1161MODULE_AUTHOR("Intel Corporation");
1162MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1163MODULE_LICENSE("GPL v2");