Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 733ba91 | 2012-02-09 19:25:53 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 5 | * |
| 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 Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 19 | #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 Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 33 | #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 Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 38 | #include <linux/miscdevice.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 39 | |
| 40 | #include "mei_dev.h" |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 41 | #include <linux/mei.h> |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 42 | #include "interface.h" |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 43 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 44 | /* AMT device is a singleton on the platform */ |
| 45 | static struct pci_dev *mei_pdev; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 46 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 47 | /* mei_pci_tbl - PCI Device ID Table */ |
| 48 | static 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 Winkler | 9af5142 | 2012-08-29 01:15:50 +0300 | [diff] [blame] | 80 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)}, |
| 81 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)}, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 82 | |
| 83 | /* required last entry */ |
| 84 | {0, } |
| 85 | }; |
| 86 | |
| 87 | MODULE_DEVICE_TABLE(pci, mei_pci_tbl); |
| 88 | |
| 89 | static DEFINE_MUTEX(mei_mutex); |
| 90 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 91 | |
| 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 | */ |
| 105 | static 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 114 | list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 115 | 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 119 | list_del(&cb_pos->list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 120 | /* 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 Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 127 | mei_io_cb_free(cb_pos); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 128 | 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 | */ |
| 146 | static 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 151 | mei_clear_list(dev, file, &dev->amthi_cmd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 152 | if (mei_clear_list(dev, file, |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 153 | &dev->amthi_read_complete_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 154 | removed = true; |
| 155 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 156 | mei_clear_list(dev, file, &dev->ctrl_rd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 157 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 158 | if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 159 | removed = true; |
| 160 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 161 | if (mei_clear_list(dev, file, &dev->write_waiting_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 162 | removed = true; |
| 163 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 164 | if (mei_clear_list(dev, file, &dev->write_list.list)) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 165 | 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 Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 172 | mei_io_cb_free(dev->iamthif_current_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 173 | 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 | */ |
| 187 | static struct mei_cl_cb *find_read_list_entry( |
| 188 | struct mei_device *dev, |
| 189 | struct mei_cl *cl) |
| 190 | { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 191 | struct mei_cl_cb *pos = NULL; |
| 192 | struct mei_cl_cb *next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 193 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 194 | dev_dbg(&dev->pdev->dev, "remove read_list CB\n"); |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 195 | list_for_each_entry_safe(pos, next, &dev->read_list.list, list) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 196 | struct mei_cl *cl_temp; |
| 197 | cl_temp = (struct mei_cl *)pos->file_private; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 198 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 199 | if (mei_cl_cmp_id(cl, cl_temp)) |
| 200 | return pos; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 201 | } |
| 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 | */ |
| 213 | static int mei_open(struct inode *inode, struct file *file) |
| 214 | { |
| 215 | struct mei_cl *cl; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 216 | struct mei_device *dev; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 217 | unsigned long cl_id; |
| 218 | int err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 219 | |
| 220 | err = -ENODEV; |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 221 | if (!mei_pdev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 222 | goto out; |
| 223 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 224 | dev = pci_get_drvdata(mei_pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 225 | if (!dev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 226 | goto out; |
| 227 | |
| 228 | mutex_lock(&dev->device_lock); |
| 229 | err = -ENOMEM; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 230 | cl = mei_cl_allocate(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 231 | if (!cl) |
Alexey Khoroshilov | 303dfbf | 2011-08-31 00:41:14 +0400 | [diff] [blame] | 232 | goto out_unlock; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 233 | |
| 234 | err = -ENODEV; |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 235 | 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 Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 238 | goto out_unlock; |
| 239 | } |
| 240 | err = -EMFILE; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 241 | 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 Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 244 | goto out_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 245 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 246 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 247 | cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX); |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 248 | if (cl_id >= MEI_CLIENTS_MAX) { |
| 249 | dev_err(&dev->pdev->dev, "client_id exceded %d", |
| 250 | MEI_CLIENTS_MAX) ; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 251 | goto out_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 252 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 253 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 254 | cl->host_client_id = cl_id; |
| 255 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 256 | dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id); |
| 257 | |
| 258 | dev->open_handle_count++; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 259 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 260 | 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 Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 269 | return nonseekable_open(inode, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 270 | |
| 271 | out_unlock: |
| 272 | mutex_unlock(&dev->device_lock); |
| 273 | kfree(cl); |
| 274 | out: |
| 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 | */ |
| 286 | static 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 Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 309 | mei_cl_flush_queues(cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 310 | 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 Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 315 | clear_bit(cl->host_client_id, dev->host_clients_map); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 316 | 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 326 | list_del(&cb->list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 327 | |
| 328 | cb = cl->read_cb; |
| 329 | cl->read_cb = NULL; |
| 330 | } |
| 331 | |
| 332 | file->private_data = NULL; |
| 333 | |
| 334 | if (cb) { |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 335 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 336 | 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 Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 349 | dev->iamthif_canceled = true; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 350 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) { |
| 351 | dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n"); |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 352 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 353 | } |
| 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 | */ |
| 375 | static ssize_t mei_read(struct file *file, char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 376 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 377 | { |
| 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 Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 393 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 394 | 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 Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 400 | i = mei_me_cl_by_uuid(dev, &mei_wd_guid); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 401 | if (i >= 0) { |
| 402 | struct mei_me_client *me_client = &dev->me_clients[i]; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 403 | 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 Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 417 | if (cl->read_cb && cl->read_cb->buf_idx > *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 418 | cb = cl->read_cb; |
| 419 | goto copy_buffer; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 420 | } else if (cl->read_cb && cl->read_cb->buf_idx > 0 && |
| 421 | cl->read_cb->buf_idx <= *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 422 | cb = cl->read_cb; |
| 423 | rets = 0; |
| 424 | goto free; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 425 | } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) { |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 426 | /*Offset needs to be cleaned for contiguous reads*/ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 427 | *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 */ |
| 479 | copy_buffer: |
| 480 | dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n", |
| 481 | cb->response_buffer.size); |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 482 | dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx); |
| 483 | if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 484 | rets = -EMSGSIZE; |
| 485 | goto free; |
| 486 | } |
| 487 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 488 | /* 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 Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 491 | |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 492 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 493 | rets = -EFAULT; |
| 494 | goto free; |
| 495 | } |
| 496 | |
| 497 | rets = length; |
| 498 | *offset += length; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 499 | if ((unsigned long)*offset < cb->buf_idx) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 500 | goto out; |
| 501 | |
| 502 | free: |
| 503 | cb_pos = find_read_list_entry(dev, cl); |
| 504 | /* Remove entry from read list */ |
| 505 | if (cb_pos) |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 506 | list_del(&cb_pos->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 507 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 508 | cl->reading_state = MEI_IDLE; |
| 509 | cl->read_cb = NULL; |
| 510 | cl->read_pending = 0; |
| 511 | out: |
| 512 | dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets); |
| 513 | mutex_unlock(&dev->device_lock); |
| 514 | return rets; |
| 515 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 516 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 517 | * 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 | */ |
| 526 | static ssize_t mei_write(struct file *file, const char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 527 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 528 | { |
| 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 Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 544 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 545 | rets = -ENODEV; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 546 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 547 | } |
| 548 | |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 549 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 550 | if (i < 0) { |
| 551 | rets = -ENODEV; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 552 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 553 | } |
| 554 | if (length > dev->me_clients[i].props.max_msg_length || length <= 0) { |
| 555 | rets = -EMSGSIZE; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 556 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 557 | } |
| 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 Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 563 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 564 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 565 | 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 Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame^] | 570 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 571 | |
| 572 | if (time_after(jiffies, timeout) || |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 573 | cl->reading_state == MEI_READ_COMPLETE) { |
| 574 | *offset = 0; |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 575 | list_del(&write_cb->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 576 | mei_io_cb_free(write_cb); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 577 | write_cb = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 578 | } |
| 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 587 | list_del(&write_cb->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 588 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 589 | write_cb = NULL; |
| 590 | cl->reading_state = MEI_IDLE; |
| 591 | cl->read_cb = NULL; |
| 592 | cl->read_pending = 0; |
| 593 | } |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 594 | } else if (cl->reading_state == MEI_IDLE && !cl->read_pending) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 595 | *offset = 0; |
| 596 | |
| 597 | |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 598 | write_cb = mei_io_cb_init(cl, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 599 | if (!write_cb) { |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 600 | dev_err(&dev->pdev->dev, "write cb allocation failed\n"); |
| 601 | rets = -ENOMEM; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 602 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 603 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 604 | rets = mei_io_cb_alloc_req_buf(write_cb, length); |
| 605 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 606 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 607 | |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 608 | dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 609 | |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 610 | rets = copy_from_user(write_cb->request_buffer.data, ubuf, length); |
| 611 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 612 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 613 | |
| 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 Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 624 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 625 | rets = mei_io_cb_alloc_resp_buf(write_cb, dev->iamthif_mtu); |
| 626 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 627 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 628 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 629 | write_cb->major_file_operations = MEI_IOCTL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 630 | |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 631 | if (!list_empty(&dev->amthi_cmd_list.list) || |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 632 | 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 636 | list_add_tail(&write_cb->list, &dev->amthi_cmd_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 637 | } else { |
| 638 | dev_dbg(&dev->pdev->dev, "call amthi write\n"); |
| 639 | rets = amthi_write(dev, write_cb); |
| 640 | |
| 641 | if (rets) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 642 | dev_err(&dev->pdev->dev, "amthi write failed with status = %d\n", |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 643 | rets); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 644 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 645 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 646 | } |
| 647 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 648 | return length; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | write_cb->major_file_operations = MEI_WRITE; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 652 | |
| 653 | dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n", |
| 654 | cl->host_client_id, cl->me_client_id); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 655 | rets = mei_flow_ctrl_creds(dev, cl); |
| 656 | if (rets < 0) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 657 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 658 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 659 | if (rets == 0 || dev->mei_host_buffer_is_empty == false) { |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 660 | write_cb->buf_idx = 0; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 661 | mei_hdr.msg_complete = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 662 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 663 | 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 | |
| 687 | out: |
| 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 Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 695 | list_add_tail(&write_cb->list, &dev->write_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 696 | } |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 697 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 698 | mutex_unlock(&dev->device_lock); |
| 699 | return length; |
| 700 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 701 | err: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 702 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 703 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 704 | 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 | */ |
| 717 | static 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 Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 735 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 736 | 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 | |
| 769 | out: |
| 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 |
| 785 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 786 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 787 | { |
| 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 | */ |
| 801 | static 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 Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 814 | if (dev->dev_state != MEI_DEV_ENABLED) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 815 | 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 Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 826 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 827 | } |
| 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 | |
| 837 | out: |
| 838 | mutex_unlock(&dev->device_lock); |
| 839 | return mask; |
| 840 | } |
| 841 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 842 | /* |
| 843 | * file operations structure will be used for mei char device. |
| 844 | */ |
| 845 | static 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 | */ |
| 863 | static struct miscdevice mei_misc_device = { |
Tomas Winkler | c38ea24 | 2012-04-02 20:32:39 +0300 | [diff] [blame] | 864 | .name = "mei", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 865 | .fops = &mei_fops, |
| 866 | .minor = MISC_DYNAMIC_MINOR, |
| 867 | }; |
| 868 | |
| 869 | /** |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 870 | * 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 | */ |
| 876 | static 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, ®); |
| 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 Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 891 | * 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 | */ |
| 898 | static 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 Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 905 | |
| 906 | if (!mei_quirk_probe(pdev, ent)) { |
| 907 | err = -ENODEV; |
| 908 | goto end; |
| 909 | } |
| 910 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 911 | if (mei_pdev) { |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 912 | err = -EEXIST; |
| 913 | goto end; |
| 914 | } |
| 915 | /* enable pci dev */ |
| 916 | err = pci_enable_device(pdev); |
| 917 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 918 | dev_err(&pdev->dev, "failed to enable pci device.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 919 | goto end; |
| 920 | } |
| 921 | /* set PCI host mastering */ |
| 922 | pci_set_master(pdev); |
| 923 | /* pci request regions for mei driver */ |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 924 | err = pci_request_regions(pdev, KBUILD_MODNAME); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 925 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 926 | dev_err(&pdev->dev, "failed to get pci regions.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 927 | 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 Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 938 | dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 939 | 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 Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 949 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 950 | else |
| 951 | err = request_threaded_irq(pdev->irq, |
| 952 | mei_interrupt_quick_handler, |
| 953 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 954 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 955 | |
| 956 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 957 | dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 958 | pdev->irq); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 959 | goto disable_msi; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 960 | } |
| 961 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 962 | if (mei_hw_init(dev)) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 963 | dev_err(&pdev->dev, "init hw failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 964 | err = -ENODEV; |
| 965 | goto release_irq; |
| 966 | } |
| 967 | |
| 968 | err = misc_register(&mei_misc_device); |
| 969 | if (err) |
| 970 | goto release_irq; |
| 971 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 972 | mei_pdev = pdev; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 973 | pci_set_drvdata(pdev, dev); |
| 974 | |
| 975 | |
| 976 | schedule_delayed_work(&dev->timer_work, HZ); |
| 977 | |
| 978 | mutex_unlock(&mei_mutex); |
| 979 | |
Tomas Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 980 | pr_debug("initialization successful.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 981 | |
| 982 | return 0; |
| 983 | |
| 984 | release_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 Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 990 | disable_msi: |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 991 | pci_disable_msi(pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 992 | pci_iounmap(pdev, dev->mem_addr); |
| 993 | free_device: |
| 994 | kfree(dev); |
| 995 | release_regions: |
| 996 | pci_release_regions(pdev); |
| 997 | disable_device: |
| 998 | pci_disable_device(pdev); |
| 999 | end: |
| 1000 | mutex_unlock(&mei_mutex); |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1001 | dev_err(&pdev->dev, "initialization failed.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1002 | 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 | */ |
| 1013 | static void __devexit mei_remove(struct pci_dev *pdev) |
| 1014 | { |
| 1015 | struct mei_device *dev; |
| 1016 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 1017 | if (mei_pdev != pdev) |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1018 | return; |
| 1019 | |
| 1020 | dev = pci_get_drvdata(pdev); |
| 1021 | if (!dev) |
| 1022 | return; |
| 1023 | |
| 1024 | mutex_lock(&dev->device_lock); |
| 1025 | |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1026 | cancel_delayed_work(&dev->timer_work); |
| 1027 | |
| 1028 | mei_wd_stop(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1029 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 1030 | mei_pdev = NULL; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1031 | |
| 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 Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 1042 | mei_watchdog_unregister(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1043 | |
| 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 Winkler | a44cab4 | 2012-05-29 16:39:11 +0300 | [diff] [blame] | 1070 | |
| 1071 | misc_deregister(&mei_misc_device); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1072 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1073 | #ifdef CONFIG_PM |
| 1074 | static 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 Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1083 | |
| 1084 | cancel_delayed_work(&dev->timer_work); |
| 1085 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1086 | /* Stop watchdog if exists */ |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 1087 | err = mei_wd_stop(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1088 | /* Set new mei state */ |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1089 | 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 Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1092 | mei_reset(dev, 0); |
| 1093 | } |
| 1094 | mutex_unlock(&dev->device_lock); |
| 1095 | |
| 1096 | free_irq(pdev->irq, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1097 | pci_disable_msi(pdev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1098 | |
| 1099 | return err; |
| 1100 | } |
| 1101 | |
| 1102 | static 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 Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1112 | 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 Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1119 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1120 | else |
| 1121 | err = request_threaded_irq(pdev->irq, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1122 | mei_interrupt_quick_handler, |
| 1123 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1124 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1125 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1126 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1127 | dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", |
| 1128 | pdev->irq); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1129 | return err; |
| 1130 | } |
| 1131 | |
| 1132 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1133 | dev->dev_state = MEI_DEV_POWER_UP; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1134 | mei_reset(dev, 1); |
| 1135 | mutex_unlock(&dev->device_lock); |
| 1136 | |
Oren Weil | 6d70e93 | 2011-09-07 09:03:14 +0300 | [diff] [blame] | 1137 | /* Start timer if stopped in suspend */ |
| 1138 | schedule_delayed_work(&dev->timer_work, HZ); |
| 1139 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1140 | return err; |
| 1141 | } |
| 1142 | static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume); |
| 1143 | #define MEI_PM_OPS (&mei_pm_ops) |
| 1144 | #else |
Randy Dunlap | 2d99036 | 2011-05-19 08:52:34 -0700 | [diff] [blame] | 1145 | #define MEI_PM_OPS NULL |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1146 | #endif /* CONFIG_PM */ |
| 1147 | /* |
| 1148 | * PCI driver structure |
| 1149 | */ |
| 1150 | static struct pci_driver mei_driver = { |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1151 | .name = KBUILD_MODNAME, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1152 | .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 Winkler | 6078188 | 2012-07-19 09:45:32 +0300 | [diff] [blame] | 1159 | module_pci_driver(mei_driver); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1160 | |
| 1161 | MODULE_AUTHOR("Intel Corporation"); |
| 1162 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
| 1163 | MODULE_LICENSE("GPL v2"); |