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 | c38ea24 | 2012-04-02 20:32:39 +0300 | [diff] [blame] | 44 | static const char mei_driver_name[] = "mei"; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 45 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 46 | /* The device pointer */ |
| 47 | /* Currently this driver works as long as there is only a single AMT device. */ |
Oren Weil | 4a3cafd | 2011-09-07 09:03:10 +0300 | [diff] [blame] | 48 | struct pci_dev *mei_device; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 49 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 50 | /* mei_pci_tbl - PCI Device ID Table */ |
| 51 | static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = { |
| 52 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)}, |
| 53 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)}, |
| 54 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)}, |
| 55 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)}, |
| 56 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)}, |
| 57 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)}, |
| 58 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)}, |
| 59 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)}, |
| 60 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)}, |
| 61 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)}, |
| 62 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)}, |
| 63 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)}, |
| 64 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)}, |
| 65 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)}, |
| 66 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)}, |
| 67 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)}, |
| 68 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)}, |
| 69 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)}, |
| 70 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)}, |
| 71 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)}, |
| 72 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)}, |
| 73 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)}, |
| 74 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)}, |
| 75 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)}, |
| 76 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)}, |
| 77 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)}, |
| 78 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)}, |
| 79 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)}, |
| 80 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)}, |
| 81 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)}, |
| 82 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)}, |
| 83 | |
| 84 | /* required last entry */ |
| 85 | {0, } |
| 86 | }; |
| 87 | |
| 88 | MODULE_DEVICE_TABLE(pci, mei_pci_tbl); |
| 89 | |
| 90 | static DEFINE_MUTEX(mei_mutex); |
| 91 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * mei_clear_list - removes all callbacks associated with file |
| 95 | * from mei_cb_list |
| 96 | * |
| 97 | * @dev: device structure. |
| 98 | * @file: file structure |
| 99 | * @mei_cb_list: callbacks list |
| 100 | * |
| 101 | * mei_clear_list is called to clear resources associated with file |
| 102 | * when application calls close function or Ctrl-C was pressed |
| 103 | * |
| 104 | * returns true if callback removed from the list, false otherwise |
| 105 | */ |
| 106 | static bool mei_clear_list(struct mei_device *dev, |
| 107 | struct file *file, struct list_head *mei_cb_list) |
| 108 | { |
| 109 | struct mei_cl_cb *cb_pos = NULL; |
| 110 | struct mei_cl_cb *cb_next = NULL; |
| 111 | struct file *file_temp; |
| 112 | bool removed = false; |
| 113 | |
| 114 | /* list all list member */ |
| 115 | list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) { |
| 116 | file_temp = (struct file *)cb_pos->file_object; |
| 117 | /* check if list member associated with a file */ |
| 118 | if (file_temp == file) { |
| 119 | /* remove member from the list */ |
| 120 | list_del(&cb_pos->cb_list); |
| 121 | /* check if cb equal to current iamthif cb */ |
| 122 | if (dev->iamthif_current_cb == cb_pos) { |
| 123 | dev->iamthif_current_cb = NULL; |
| 124 | /* send flow control to iamthif client */ |
| 125 | mei_send_flow_control(dev, &dev->iamthif_cl); |
| 126 | } |
| 127 | /* free all allocated buffers */ |
| 128 | mei_free_cb_private(cb_pos); |
| 129 | cb_pos = NULL; |
| 130 | removed = true; |
| 131 | } |
| 132 | } |
| 133 | return removed; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * mei_clear_lists - removes all callbacks associated with file |
| 138 | * |
| 139 | * @dev: device structure |
| 140 | * @file: file structure |
| 141 | * |
| 142 | * mei_clear_lists is called to clear resources associated with file |
| 143 | * when application calls close function or Ctrl-C was pressed |
| 144 | * |
| 145 | * returns true if callback removed from the list, false otherwise |
| 146 | */ |
| 147 | static bool mei_clear_lists(struct mei_device *dev, struct file *file) |
| 148 | { |
| 149 | bool removed = false; |
| 150 | |
| 151 | /* remove callbacks associated with a file */ |
| 152 | mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list); |
| 153 | if (mei_clear_list(dev, file, |
| 154 | &dev->amthi_read_complete_list.mei_cb.cb_list)) |
| 155 | removed = true; |
| 156 | |
| 157 | mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list); |
| 158 | |
| 159 | if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list)) |
| 160 | removed = true; |
| 161 | |
| 162 | if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list)) |
| 163 | removed = true; |
| 164 | |
| 165 | if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list)) |
| 166 | removed = true; |
| 167 | |
| 168 | /* check if iamthif_current_cb not NULL */ |
| 169 | if (dev->iamthif_current_cb && !removed) { |
| 170 | /* check file and iamthif current cb association */ |
| 171 | if (dev->iamthif_current_cb->file_object == file) { |
| 172 | /* remove cb */ |
| 173 | mei_free_cb_private(dev->iamthif_current_cb); |
| 174 | dev->iamthif_current_cb = NULL; |
| 175 | removed = true; |
| 176 | } |
| 177 | } |
| 178 | return removed; |
| 179 | } |
| 180 | /** |
| 181 | * find_read_list_entry - find read list entry |
| 182 | * |
| 183 | * @dev: device structure |
| 184 | * @file: pointer to file structure |
| 185 | * |
| 186 | * returns cb on success, NULL on error |
| 187 | */ |
| 188 | static struct mei_cl_cb *find_read_list_entry( |
| 189 | struct mei_device *dev, |
| 190 | struct mei_cl *cl) |
| 191 | { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 192 | struct mei_cl_cb *pos = NULL; |
| 193 | struct mei_cl_cb *next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 194 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 195 | dev_dbg(&dev->pdev->dev, "remove read_list CB\n"); |
| 196 | list_for_each_entry_safe(pos, next, |
| 197 | &dev->read_list.mei_cb.cb_list, cb_list) { |
| 198 | struct mei_cl *cl_temp; |
| 199 | cl_temp = (struct mei_cl *)pos->file_private; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 200 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 201 | if (mei_cl_cmp_id(cl, cl_temp)) |
| 202 | return pos; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * mei_open - the open function |
| 209 | * |
| 210 | * @inode: pointer to inode structure |
| 211 | * @file: pointer to file structure |
| 212 | * |
| 213 | * returns 0 on success, <0 on error |
| 214 | */ |
| 215 | static int mei_open(struct inode *inode, struct file *file) |
| 216 | { |
| 217 | struct mei_cl *cl; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 218 | struct mei_device *dev; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 219 | unsigned long cl_id; |
| 220 | int err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 221 | |
| 222 | err = -ENODEV; |
| 223 | if (!mei_device) |
| 224 | goto out; |
| 225 | |
| 226 | dev = pci_get_drvdata(mei_device); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 227 | if (!dev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 228 | goto out; |
| 229 | |
| 230 | mutex_lock(&dev->device_lock); |
| 231 | err = -ENOMEM; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 232 | cl = mei_cl_allocate(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 233 | if (!cl) |
Alexey Khoroshilov | 303dfbf | 2011-08-31 00:41:14 +0400 | [diff] [blame] | 234 | goto out_unlock; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 235 | |
| 236 | err = -ENODEV; |
| 237 | if (dev->mei_state != MEI_ENABLED) { |
| 238 | dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n", |
| 239 | dev->mei_state); |
| 240 | goto out_unlock; |
| 241 | } |
| 242 | err = -EMFILE; |
| 243 | if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) |
| 244 | goto out_unlock; |
| 245 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 246 | cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX); |
| 247 | if (cl_id >= MEI_CLIENTS_MAX) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 248 | goto out_unlock; |
| 249 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 250 | cl->host_client_id = cl_id; |
| 251 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 252 | dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id); |
| 253 | |
| 254 | dev->open_handle_count++; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 255 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 256 | list_add_tail(&cl->link, &dev->file_list); |
| 257 | |
| 258 | set_bit(cl->host_client_id, dev->host_clients_map); |
| 259 | cl->state = MEI_FILE_INITIALIZING; |
| 260 | cl->sm_state = 0; |
| 261 | |
| 262 | file->private_data = cl; |
| 263 | mutex_unlock(&dev->device_lock); |
| 264 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 265 | return nonseekable_open(inode, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 266 | |
| 267 | out_unlock: |
| 268 | mutex_unlock(&dev->device_lock); |
| 269 | kfree(cl); |
| 270 | out: |
| 271 | return err; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * mei_release - the release function |
| 276 | * |
| 277 | * @inode: pointer to inode structure |
| 278 | * @file: pointer to file structure |
| 279 | * |
| 280 | * returns 0 on success, <0 on error |
| 281 | */ |
| 282 | static int mei_release(struct inode *inode, struct file *file) |
| 283 | { |
| 284 | struct mei_cl *cl = file->private_data; |
| 285 | struct mei_cl_cb *cb; |
| 286 | struct mei_device *dev; |
| 287 | int rets = 0; |
| 288 | |
| 289 | if (WARN_ON(!cl || !cl->dev)) |
| 290 | return -ENODEV; |
| 291 | |
| 292 | dev = cl->dev; |
| 293 | |
| 294 | mutex_lock(&dev->device_lock); |
| 295 | if (cl != &dev->iamthif_cl) { |
| 296 | if (cl->state == MEI_FILE_CONNECTED) { |
| 297 | cl->state = MEI_FILE_DISCONNECTING; |
| 298 | dev_dbg(&dev->pdev->dev, |
| 299 | "disconnecting client host client = %d, " |
| 300 | "ME client = %d\n", |
| 301 | cl->host_client_id, |
| 302 | cl->me_client_id); |
| 303 | rets = mei_disconnect_host_client(dev, cl); |
| 304 | } |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 305 | mei_cl_flush_queues(cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 306 | dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n", |
| 307 | cl->host_client_id, |
| 308 | cl->me_client_id); |
| 309 | |
| 310 | if (dev->open_handle_count > 0) { |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 311 | clear_bit(cl->host_client_id, dev->host_clients_map); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 312 | dev->open_handle_count--; |
| 313 | } |
| 314 | mei_remove_client_from_file_list(dev, cl->host_client_id); |
| 315 | |
| 316 | /* free read cb */ |
| 317 | cb = NULL; |
| 318 | if (cl->read_cb) { |
| 319 | cb = find_read_list_entry(dev, cl); |
| 320 | /* Remove entry from read list */ |
| 321 | if (cb) |
| 322 | list_del(&cb->cb_list); |
| 323 | |
| 324 | cb = cl->read_cb; |
| 325 | cl->read_cb = NULL; |
| 326 | } |
| 327 | |
| 328 | file->private_data = NULL; |
| 329 | |
| 330 | if (cb) { |
| 331 | mei_free_cb_private(cb); |
| 332 | cb = NULL; |
| 333 | } |
| 334 | |
| 335 | kfree(cl); |
| 336 | } else { |
| 337 | if (dev->open_handle_count > 0) |
| 338 | dev->open_handle_count--; |
| 339 | |
| 340 | if (dev->iamthif_file_object == file && |
| 341 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 342 | |
| 343 | dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n", |
| 344 | dev->iamthif_state); |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 345 | dev->iamthif_canceled = true; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 346 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) { |
| 347 | dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n"); |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 348 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | if (mei_clear_lists(dev, file)) |
| 353 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 354 | |
| 355 | } |
| 356 | mutex_unlock(&dev->device_lock); |
| 357 | return rets; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * mei_read - the read function. |
| 363 | * |
| 364 | * @file: pointer to file structure |
| 365 | * @ubuf: pointer to user buffer |
| 366 | * @length: buffer length |
| 367 | * @offset: data offset in buffer |
| 368 | * |
| 369 | * returns >=0 data length on success , <0 on error |
| 370 | */ |
| 371 | static ssize_t mei_read(struct file *file, char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 372 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 373 | { |
| 374 | struct mei_cl *cl = file->private_data; |
| 375 | struct mei_cl_cb *cb_pos = NULL; |
| 376 | struct mei_cl_cb *cb = NULL; |
| 377 | struct mei_device *dev; |
| 378 | int i; |
| 379 | int rets; |
| 380 | int err; |
| 381 | |
| 382 | |
| 383 | if (WARN_ON(!cl || !cl->dev)) |
| 384 | return -ENODEV; |
| 385 | |
| 386 | dev = cl->dev; |
| 387 | |
| 388 | mutex_lock(&dev->device_lock); |
| 389 | if (dev->mei_state != MEI_ENABLED) { |
| 390 | rets = -ENODEV; |
| 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) { |
| 395 | /* Do not allow to read watchdog client */ |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 396 | i = mei_me_cl_by_uuid(dev, &mei_wd_guid); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 397 | if (i >= 0) { |
| 398 | struct mei_me_client *me_client = &dev->me_clients[i]; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 399 | if (cl->me_client_id == me_client->client_id) { |
| 400 | rets = -EBADF; |
| 401 | goto out; |
| 402 | } |
| 403 | } |
| 404 | } else { |
| 405 | cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT; |
| 406 | } |
| 407 | |
| 408 | if (cl == &dev->iamthif_cl) { |
| 409 | rets = amthi_read(dev, file, ubuf, length, offset); |
| 410 | goto out; |
| 411 | } |
| 412 | |
| 413 | if (cl->read_cb && cl->read_cb->information > *offset) { |
| 414 | cb = cl->read_cb; |
| 415 | goto copy_buffer; |
| 416 | } else if (cl->read_cb && cl->read_cb->information > 0 && |
| 417 | cl->read_cb->information <= *offset) { |
| 418 | cb = cl->read_cb; |
| 419 | rets = 0; |
| 420 | goto free; |
| 421 | } else if ((!cl->read_cb || !cl->read_cb->information) && |
| 422 | *offset > 0) { |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 423 | /*Offset needs to be cleaned for contiguous reads*/ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 424 | *offset = 0; |
| 425 | rets = 0; |
| 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | err = mei_start_read(dev, cl); |
| 430 | if (err && err != -EBUSY) { |
| 431 | dev_dbg(&dev->pdev->dev, |
| 432 | "mei start read failure with status = %d\n", err); |
| 433 | rets = err; |
| 434 | goto out; |
| 435 | } |
| 436 | |
| 437 | if (MEI_READ_COMPLETE != cl->reading_state && |
| 438 | !waitqueue_active(&cl->rx_wait)) { |
| 439 | if (file->f_flags & O_NONBLOCK) { |
| 440 | rets = -EAGAIN; |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | mutex_unlock(&dev->device_lock); |
| 445 | |
| 446 | if (wait_event_interruptible(cl->rx_wait, |
| 447 | (MEI_READ_COMPLETE == cl->reading_state || |
| 448 | MEI_FILE_INITIALIZING == cl->state || |
| 449 | MEI_FILE_DISCONNECTED == cl->state || |
| 450 | MEI_FILE_DISCONNECTING == cl->state))) { |
| 451 | if (signal_pending(current)) |
| 452 | return -EINTR; |
| 453 | return -ERESTARTSYS; |
| 454 | } |
| 455 | |
| 456 | mutex_lock(&dev->device_lock); |
| 457 | if (MEI_FILE_INITIALIZING == cl->state || |
| 458 | MEI_FILE_DISCONNECTED == cl->state || |
| 459 | MEI_FILE_DISCONNECTING == cl->state) { |
| 460 | rets = -EBUSY; |
| 461 | goto out; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | cb = cl->read_cb; |
| 466 | |
| 467 | if (!cb) { |
| 468 | rets = -ENODEV; |
| 469 | goto out; |
| 470 | } |
| 471 | if (cl->reading_state != MEI_READ_COMPLETE) { |
| 472 | rets = 0; |
| 473 | goto out; |
| 474 | } |
| 475 | /* now copy the data to user space */ |
| 476 | copy_buffer: |
| 477 | dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n", |
| 478 | cb->response_buffer.size); |
| 479 | dev_dbg(&dev->pdev->dev, "cb->information - %lu\n", |
| 480 | cb->information); |
| 481 | if (length == 0 || ubuf == NULL || *offset > cb->information) { |
| 482 | rets = -EMSGSIZE; |
| 483 | goto free; |
| 484 | } |
| 485 | |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 486 | /* length is being truncated to PAGE_SIZE, however, */ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 487 | /* information size may be longer */ |
| 488 | length = min_t(size_t, length, (cb->information - *offset)); |
| 489 | |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 490 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 491 | rets = -EFAULT; |
| 492 | goto free; |
| 493 | } |
| 494 | |
| 495 | rets = length; |
| 496 | *offset += length; |
| 497 | if ((unsigned long)*offset < cb->information) |
| 498 | goto out; |
| 499 | |
| 500 | free: |
| 501 | cb_pos = find_read_list_entry(dev, cl); |
| 502 | /* Remove entry from read list */ |
| 503 | if (cb_pos) |
| 504 | list_del(&cb_pos->cb_list); |
| 505 | mei_free_cb_private(cb); |
| 506 | cl->reading_state = MEI_IDLE; |
| 507 | cl->read_cb = NULL; |
| 508 | cl->read_pending = 0; |
| 509 | out: |
| 510 | dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets); |
| 511 | mutex_unlock(&dev->device_lock); |
| 512 | return rets; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * mei_write - the write function. |
| 517 | * |
| 518 | * @file: pointer to file structure |
| 519 | * @ubuf: pointer to user buffer |
| 520 | * @length: buffer length |
| 521 | * @offset: data offset in buffer |
| 522 | * |
| 523 | * returns >=0 data length on success , <0 on error |
| 524 | */ |
| 525 | static ssize_t mei_write(struct file *file, const char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 526 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 527 | { |
| 528 | struct mei_cl *cl = file->private_data; |
| 529 | struct mei_cl_cb *write_cb = NULL; |
| 530 | struct mei_msg_hdr mei_hdr; |
| 531 | struct mei_device *dev; |
| 532 | unsigned long timeout = 0; |
| 533 | int rets; |
| 534 | int i; |
| 535 | |
| 536 | if (WARN_ON(!cl || !cl->dev)) |
| 537 | return -ENODEV; |
| 538 | |
| 539 | dev = cl->dev; |
| 540 | |
| 541 | mutex_lock(&dev->device_lock); |
| 542 | |
| 543 | if (dev->mei_state != MEI_ENABLED) { |
| 544 | mutex_unlock(&dev->device_lock); |
| 545 | return -ENODEV; |
| 546 | } |
| 547 | |
| 548 | if (cl == &dev->iamthif_cl) { |
| 549 | write_cb = find_amthi_read_list_entry(dev, file); |
| 550 | |
| 551 | if (write_cb) { |
| 552 | timeout = write_cb->read_time + |
| 553 | msecs_to_jiffies(IAMTHIF_READ_TIMER); |
| 554 | |
| 555 | if (time_after(jiffies, timeout) || |
| 556 | cl->reading_state == MEI_READ_COMPLETE) { |
| 557 | *offset = 0; |
| 558 | list_del(&write_cb->cb_list); |
| 559 | mei_free_cb_private(write_cb); |
| 560 | write_cb = NULL; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* free entry used in read */ |
| 566 | if (cl->reading_state == MEI_READ_COMPLETE) { |
| 567 | *offset = 0; |
| 568 | write_cb = find_read_list_entry(dev, cl); |
| 569 | if (write_cb) { |
| 570 | list_del(&write_cb->cb_list); |
| 571 | mei_free_cb_private(write_cb); |
| 572 | write_cb = NULL; |
| 573 | cl->reading_state = MEI_IDLE; |
| 574 | cl->read_cb = NULL; |
| 575 | cl->read_pending = 0; |
| 576 | } |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 577 | } else if (cl->reading_state == MEI_IDLE && !cl->read_pending) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 578 | *offset = 0; |
| 579 | |
| 580 | |
| 581 | write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL); |
| 582 | if (!write_cb) { |
| 583 | mutex_unlock(&dev->device_lock); |
| 584 | return -ENOMEM; |
| 585 | } |
| 586 | |
| 587 | write_cb->file_object = file; |
| 588 | write_cb->file_private = cl; |
| 589 | write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL); |
| 590 | rets = -ENOMEM; |
| 591 | if (!write_cb->request_buffer.data) |
| 592 | goto unlock_dev; |
| 593 | |
| 594 | dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length); |
| 595 | |
| 596 | rets = -EFAULT; |
| 597 | if (copy_from_user(write_cb->request_buffer.data, ubuf, length)) |
| 598 | goto unlock_dev; |
| 599 | |
| 600 | cl->sm_state = 0; |
| 601 | if (length == 4 && |
| 602 | ((memcmp(mei_wd_state_independence_msg[0], |
| 603 | write_cb->request_buffer.data, 4) == 0) || |
| 604 | (memcmp(mei_wd_state_independence_msg[1], |
| 605 | write_cb->request_buffer.data, 4) == 0) || |
| 606 | (memcmp(mei_wd_state_independence_msg[2], |
| 607 | write_cb->request_buffer.data, 4) == 0))) |
| 608 | cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT; |
| 609 | |
| 610 | INIT_LIST_HEAD(&write_cb->cb_list); |
| 611 | if (cl == &dev->iamthif_cl) { |
| 612 | write_cb->response_buffer.data = |
| 613 | kmalloc(dev->iamthif_mtu, GFP_KERNEL); |
| 614 | if (!write_cb->response_buffer.data) { |
| 615 | rets = -ENOMEM; |
| 616 | goto unlock_dev; |
| 617 | } |
| 618 | if (dev->mei_state != MEI_ENABLED) { |
| 619 | rets = -ENODEV; |
| 620 | goto unlock_dev; |
| 621 | } |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 622 | i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id); |
| 623 | if (i < 0) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 624 | rets = -ENODEV; |
| 625 | goto unlock_dev; |
| 626 | } |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 627 | if (length > dev->me_clients[i].props.max_msg_length || |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 628 | length <= 0) { |
| 629 | rets = -EMSGSIZE; |
| 630 | goto unlock_dev; |
| 631 | } |
| 632 | |
| 633 | write_cb->response_buffer.size = dev->iamthif_mtu; |
| 634 | write_cb->major_file_operations = MEI_IOCTL; |
| 635 | write_cb->information = 0; |
| 636 | write_cb->request_buffer.size = length; |
| 637 | if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) { |
| 638 | rets = -ENODEV; |
| 639 | goto unlock_dev; |
| 640 | } |
| 641 | |
| 642 | if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) || |
| 643 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
| 644 | dev_dbg(&dev->pdev->dev, "amthi_state = %d\n", |
| 645 | (int) dev->iamthif_state); |
| 646 | dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n"); |
| 647 | list_add_tail(&write_cb->cb_list, |
| 648 | &dev->amthi_cmd_list.mei_cb.cb_list); |
| 649 | rets = length; |
| 650 | } else { |
| 651 | dev_dbg(&dev->pdev->dev, "call amthi write\n"); |
| 652 | rets = amthi_write(dev, write_cb); |
| 653 | |
| 654 | if (rets) { |
| 655 | dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n", |
| 656 | rets); |
| 657 | goto unlock_dev; |
| 658 | } |
| 659 | rets = length; |
| 660 | } |
| 661 | mutex_unlock(&dev->device_lock); |
| 662 | return rets; |
| 663 | } |
| 664 | |
| 665 | write_cb->major_file_operations = MEI_WRITE; |
| 666 | /* make sure information is zero before we start */ |
| 667 | |
| 668 | write_cb->information = 0; |
| 669 | write_cb->request_buffer.size = length; |
| 670 | |
| 671 | dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n", |
| 672 | cl->host_client_id, cl->me_client_id); |
| 673 | if (cl->state != MEI_FILE_CONNECTED) { |
| 674 | rets = -ENODEV; |
| 675 | dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d", |
| 676 | cl->host_client_id, |
| 677 | cl->me_client_id); |
| 678 | goto unlock_dev; |
| 679 | } |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 680 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 681 | if (i < 0) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 682 | rets = -ENODEV; |
| 683 | goto unlock_dev; |
| 684 | } |
| 685 | if (length > dev->me_clients[i].props.max_msg_length || length <= 0) { |
| 686 | rets = -EINVAL; |
| 687 | goto unlock_dev; |
| 688 | } |
| 689 | write_cb->file_private = cl; |
| 690 | |
| 691 | rets = mei_flow_ctrl_creds(dev, cl); |
| 692 | if (rets < 0) |
| 693 | goto unlock_dev; |
| 694 | |
| 695 | if (rets && dev->mei_host_buffer_is_empty) { |
| 696 | rets = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 697 | dev->mei_host_buffer_is_empty = false; |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 698 | if (length > mei_hbuf_max_data(dev)) { |
| 699 | mei_hdr.length = mei_hbuf_max_data(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 700 | mei_hdr.msg_complete = 0; |
| 701 | } else { |
| 702 | mei_hdr.length = length; |
| 703 | mei_hdr.msg_complete = 1; |
| 704 | } |
| 705 | mei_hdr.host_addr = cl->host_client_id; |
| 706 | mei_hdr.me_addr = cl->me_client_id; |
| 707 | mei_hdr.reserved = 0; |
| 708 | dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n", |
| 709 | *((u32 *) &mei_hdr)); |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 710 | if (mei_write_message(dev, &mei_hdr, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 711 | (unsigned char *) (write_cb->request_buffer.data), |
| 712 | mei_hdr.length)) { |
| 713 | rets = -ENODEV; |
| 714 | goto unlock_dev; |
| 715 | } |
| 716 | cl->writing_state = MEI_WRITING; |
| 717 | write_cb->information = mei_hdr.length; |
| 718 | if (mei_hdr.msg_complete) { |
| 719 | if (mei_flow_ctrl_reduce(dev, cl)) { |
| 720 | rets = -ENODEV; |
| 721 | goto unlock_dev; |
| 722 | } |
| 723 | list_add_tail(&write_cb->cb_list, |
| 724 | &dev->write_waiting_list.mei_cb.cb_list); |
| 725 | } else { |
| 726 | list_add_tail(&write_cb->cb_list, |
| 727 | &dev->write_list.mei_cb.cb_list); |
| 728 | } |
| 729 | |
| 730 | } else { |
| 731 | |
| 732 | write_cb->information = 0; |
| 733 | cl->writing_state = MEI_WRITING; |
| 734 | list_add_tail(&write_cb->cb_list, |
| 735 | &dev->write_list.mei_cb.cb_list); |
| 736 | } |
| 737 | mutex_unlock(&dev->device_lock); |
| 738 | return length; |
| 739 | |
| 740 | unlock_dev: |
| 741 | mutex_unlock(&dev->device_lock); |
| 742 | mei_free_cb_private(write_cb); |
| 743 | return rets; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | /** |
| 748 | * mei_ioctl - the IOCTL function |
| 749 | * |
| 750 | * @file: pointer to file structure |
| 751 | * @cmd: ioctl command |
| 752 | * @data: pointer to mei message structure |
| 753 | * |
| 754 | * returns 0 on success , <0 on error |
| 755 | */ |
| 756 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 757 | { |
| 758 | struct mei_device *dev; |
| 759 | struct mei_cl *cl = file->private_data; |
| 760 | struct mei_connect_client_data *connect_data = NULL; |
| 761 | int rets; |
| 762 | |
| 763 | if (cmd != IOCTL_MEI_CONNECT_CLIENT) |
| 764 | return -EINVAL; |
| 765 | |
| 766 | if (WARN_ON(!cl || !cl->dev)) |
| 767 | return -ENODEV; |
| 768 | |
| 769 | dev = cl->dev; |
| 770 | |
| 771 | dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd); |
| 772 | |
| 773 | mutex_lock(&dev->device_lock); |
| 774 | if (dev->mei_state != MEI_ENABLED) { |
| 775 | rets = -ENODEV; |
| 776 | goto out; |
| 777 | } |
| 778 | |
| 779 | dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
| 780 | |
| 781 | connect_data = kzalloc(sizeof(struct mei_connect_client_data), |
| 782 | GFP_KERNEL); |
| 783 | if (!connect_data) { |
| 784 | rets = -ENOMEM; |
| 785 | goto out; |
| 786 | } |
| 787 | dev_dbg(&dev->pdev->dev, "copy connect data from user\n"); |
| 788 | if (copy_from_user(connect_data, (char __user *)data, |
| 789 | sizeof(struct mei_connect_client_data))) { |
| 790 | dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n"); |
| 791 | rets = -EFAULT; |
| 792 | goto out; |
| 793 | } |
| 794 | rets = mei_ioctl_connect_client(file, connect_data); |
| 795 | |
| 796 | /* if all is ok, copying the data back to user. */ |
| 797 | if (rets) |
| 798 | goto out; |
| 799 | |
| 800 | dev_dbg(&dev->pdev->dev, "copy connect data to user\n"); |
| 801 | if (copy_to_user((char __user *)data, connect_data, |
| 802 | sizeof(struct mei_connect_client_data))) { |
| 803 | dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n"); |
| 804 | rets = -EFAULT; |
| 805 | goto out; |
| 806 | } |
| 807 | |
| 808 | out: |
| 809 | kfree(connect_data); |
| 810 | mutex_unlock(&dev->device_lock); |
| 811 | return rets; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * mei_compat_ioctl - the compat IOCTL function |
| 816 | * |
| 817 | * @file: pointer to file structure |
| 818 | * @cmd: ioctl command |
| 819 | * @data: pointer to mei message structure |
| 820 | * |
| 821 | * returns 0 on success , <0 on error |
| 822 | */ |
| 823 | #ifdef CONFIG_COMPAT |
| 824 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 825 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 826 | { |
| 827 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); |
| 828 | } |
| 829 | #endif |
| 830 | |
| 831 | |
| 832 | /** |
| 833 | * mei_poll - the poll function |
| 834 | * |
| 835 | * @file: pointer to file structure |
| 836 | * @wait: pointer to poll_table structure |
| 837 | * |
| 838 | * returns poll mask |
| 839 | */ |
| 840 | static unsigned int mei_poll(struct file *file, poll_table *wait) |
| 841 | { |
| 842 | struct mei_cl *cl = file->private_data; |
| 843 | struct mei_device *dev; |
| 844 | unsigned int mask = 0; |
| 845 | |
| 846 | if (WARN_ON(!cl || !cl->dev)) |
| 847 | return mask; |
| 848 | |
| 849 | dev = cl->dev; |
| 850 | |
| 851 | mutex_lock(&dev->device_lock); |
| 852 | |
| 853 | if (dev->mei_state != MEI_ENABLED) |
| 854 | goto out; |
| 855 | |
| 856 | |
| 857 | if (cl == &dev->iamthif_cl) { |
| 858 | mutex_unlock(&dev->device_lock); |
| 859 | poll_wait(file, &dev->iamthif_cl.wait, wait); |
| 860 | mutex_lock(&dev->device_lock); |
| 861 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && |
| 862 | dev->iamthif_file_object == file) { |
| 863 | mask |= (POLLIN | POLLRDNORM); |
| 864 | dev_dbg(&dev->pdev->dev, "run next amthi cb\n"); |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 865 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 866 | } |
| 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | mutex_unlock(&dev->device_lock); |
| 871 | poll_wait(file, &cl->tx_wait, wait); |
| 872 | mutex_lock(&dev->device_lock); |
| 873 | if (MEI_WRITE_COMPLETE == cl->writing_state) |
| 874 | mask |= (POLLIN | POLLRDNORM); |
| 875 | |
| 876 | out: |
| 877 | mutex_unlock(&dev->device_lock); |
| 878 | return mask; |
| 879 | } |
| 880 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 881 | /* |
| 882 | * file operations structure will be used for mei char device. |
| 883 | */ |
| 884 | static const struct file_operations mei_fops = { |
| 885 | .owner = THIS_MODULE, |
| 886 | .read = mei_read, |
| 887 | .unlocked_ioctl = mei_ioctl, |
| 888 | #ifdef CONFIG_COMPAT |
| 889 | .compat_ioctl = mei_compat_ioctl, |
| 890 | #endif |
| 891 | .open = mei_open, |
| 892 | .release = mei_release, |
| 893 | .write = mei_write, |
| 894 | .poll = mei_poll, |
| 895 | .llseek = no_llseek |
| 896 | }; |
| 897 | |
| 898 | |
| 899 | /* |
| 900 | * Misc Device Struct |
| 901 | */ |
| 902 | static struct miscdevice mei_misc_device = { |
Tomas Winkler | c38ea24 | 2012-04-02 20:32:39 +0300 | [diff] [blame] | 903 | .name = "mei", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 904 | .fops = &mei_fops, |
| 905 | .minor = MISC_DYNAMIC_MINOR, |
| 906 | }; |
| 907 | |
| 908 | /** |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame^] | 909 | * mei_quirk_probe - probe for devices that doesn't valid ME interface |
| 910 | * @pdev: PCI device structure |
| 911 | * @ent: entry into pci_device_table |
| 912 | * |
| 913 | * returns true if ME Interface is valid, false otherwise |
| 914 | */ |
| 915 | static bool __devinit mei_quirk_probe(struct pci_dev *pdev, |
| 916 | const struct pci_device_id *ent) |
| 917 | { |
| 918 | u32 reg; |
| 919 | if (ent->device == MEI_DEV_ID_PBG_1) { |
| 920 | pci_read_config_dword(pdev, 0x48, ®); |
| 921 | /* make sure that bit 9 is up and bit 10 is down */ |
| 922 | if ((reg & 0x600) == 0x200) { |
| 923 | dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n"); |
| 924 | return false; |
| 925 | } |
| 926 | } |
| 927 | return true; |
| 928 | } |
| 929 | /** |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 930 | * mei_probe - Device Initialization Routine |
| 931 | * |
| 932 | * @pdev: PCI device structure |
| 933 | * @ent: entry in kcs_pci_tbl |
| 934 | * |
| 935 | * returns 0 on success, <0 on failure. |
| 936 | */ |
| 937 | static int __devinit mei_probe(struct pci_dev *pdev, |
| 938 | const struct pci_device_id *ent) |
| 939 | { |
| 940 | struct mei_device *dev; |
| 941 | int err; |
| 942 | |
| 943 | mutex_lock(&mei_mutex); |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame^] | 944 | |
| 945 | if (!mei_quirk_probe(pdev, ent)) { |
| 946 | err = -ENODEV; |
| 947 | goto end; |
| 948 | } |
| 949 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 950 | if (mei_device) { |
| 951 | err = -EEXIST; |
| 952 | goto end; |
| 953 | } |
| 954 | /* enable pci dev */ |
| 955 | err = pci_enable_device(pdev); |
| 956 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 957 | dev_err(&pdev->dev, "failed to enable pci device.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 958 | goto end; |
| 959 | } |
| 960 | /* set PCI host mastering */ |
| 961 | pci_set_master(pdev); |
| 962 | /* pci request regions for mei driver */ |
| 963 | err = pci_request_regions(pdev, mei_driver_name); |
| 964 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 965 | dev_err(&pdev->dev, "failed to get pci regions.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 966 | goto disable_device; |
| 967 | } |
| 968 | /* allocates and initializes the mei dev structure */ |
| 969 | dev = mei_device_init(pdev); |
| 970 | if (!dev) { |
| 971 | err = -ENOMEM; |
| 972 | goto release_regions; |
| 973 | } |
| 974 | /* mapping IO device memory */ |
| 975 | dev->mem_addr = pci_iomap(pdev, 0, 0); |
| 976 | if (!dev->mem_addr) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 977 | dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 978 | err = -ENOMEM; |
| 979 | goto free_device; |
| 980 | } |
| 981 | pci_enable_msi(pdev); |
| 982 | |
| 983 | /* request and enable interrupt */ |
| 984 | if (pci_dev_msi_enabled(pdev)) |
| 985 | err = request_threaded_irq(pdev->irq, |
| 986 | NULL, |
| 987 | mei_interrupt_thread_handler, |
Tomas Winkler | aa189ec | 2012-05-29 16:39:10 +0300 | [diff] [blame] | 988 | IRQF_ONESHOT, mei_driver_name, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 989 | else |
| 990 | err = request_threaded_irq(pdev->irq, |
| 991 | mei_interrupt_quick_handler, |
| 992 | mei_interrupt_thread_handler, |
| 993 | IRQF_SHARED, mei_driver_name, dev); |
| 994 | |
| 995 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 996 | dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 997 | pdev->irq); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 998 | goto disable_msi; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 999 | } |
| 1000 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 1001 | if (mei_hw_init(dev)) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1002 | dev_err(&pdev->dev, "init hw failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1003 | err = -ENODEV; |
| 1004 | goto release_irq; |
| 1005 | } |
| 1006 | |
| 1007 | err = misc_register(&mei_misc_device); |
| 1008 | if (err) |
| 1009 | goto release_irq; |
| 1010 | |
| 1011 | mei_device = pdev; |
| 1012 | pci_set_drvdata(pdev, dev); |
| 1013 | |
| 1014 | |
| 1015 | schedule_delayed_work(&dev->timer_work, HZ); |
| 1016 | |
| 1017 | mutex_unlock(&mei_mutex); |
| 1018 | |
Tomas Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 1019 | pr_debug("initialization successful.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
| 1022 | |
| 1023 | release_irq: |
| 1024 | /* disable interrupts */ |
| 1025 | dev->host_hw_state = mei_hcsr_read(dev); |
| 1026 | mei_disable_interrupts(dev); |
| 1027 | flush_scheduled_work(); |
| 1028 | free_irq(pdev->irq, dev); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 1029 | disable_msi: |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1030 | pci_disable_msi(pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1031 | pci_iounmap(pdev, dev->mem_addr); |
| 1032 | free_device: |
| 1033 | kfree(dev); |
| 1034 | release_regions: |
| 1035 | pci_release_regions(pdev); |
| 1036 | disable_device: |
| 1037 | pci_disable_device(pdev); |
| 1038 | end: |
| 1039 | mutex_unlock(&mei_mutex); |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1040 | dev_err(&pdev->dev, "initialization failed.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1041 | return err; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * mei_remove - Device Removal Routine |
| 1046 | * |
| 1047 | * @pdev: PCI device structure |
| 1048 | * |
| 1049 | * mei_remove is called by the PCI subsystem to alert the driver |
| 1050 | * that it should release a PCI device. |
| 1051 | */ |
| 1052 | static void __devexit mei_remove(struct pci_dev *pdev) |
| 1053 | { |
| 1054 | struct mei_device *dev; |
| 1055 | |
| 1056 | if (mei_device != pdev) |
| 1057 | return; |
| 1058 | |
| 1059 | dev = pci_get_drvdata(pdev); |
| 1060 | if (!dev) |
| 1061 | return; |
| 1062 | |
| 1063 | mutex_lock(&dev->device_lock); |
| 1064 | |
| 1065 | mei_wd_stop(dev, false); |
| 1066 | |
| 1067 | mei_device = NULL; |
| 1068 | |
| 1069 | if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) { |
| 1070 | dev->iamthif_cl.state = MEI_FILE_DISCONNECTING; |
| 1071 | mei_disconnect_host_client(dev, &dev->iamthif_cl); |
| 1072 | } |
| 1073 | if (dev->wd_cl.state == MEI_FILE_CONNECTED) { |
| 1074 | dev->wd_cl.state = MEI_FILE_DISCONNECTING; |
| 1075 | mei_disconnect_host_client(dev, &dev->wd_cl); |
| 1076 | } |
| 1077 | |
| 1078 | /* Unregistering watchdog device */ |
Tomas Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 1079 | mei_watchdog_unregister(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1080 | |
| 1081 | /* remove entry if already in list */ |
| 1082 | dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n"); |
| 1083 | mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id); |
| 1084 | mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id); |
| 1085 | |
| 1086 | dev->iamthif_current_cb = NULL; |
| 1087 | dev->me_clients_num = 0; |
| 1088 | |
| 1089 | mutex_unlock(&dev->device_lock); |
| 1090 | |
| 1091 | flush_scheduled_work(); |
| 1092 | |
| 1093 | /* disable interrupts */ |
| 1094 | mei_disable_interrupts(dev); |
| 1095 | |
| 1096 | free_irq(pdev->irq, dev); |
| 1097 | pci_disable_msi(pdev); |
| 1098 | pci_set_drvdata(pdev, NULL); |
| 1099 | |
| 1100 | if (dev->mem_addr) |
| 1101 | pci_iounmap(pdev, dev->mem_addr); |
| 1102 | |
| 1103 | kfree(dev); |
| 1104 | |
| 1105 | pci_release_regions(pdev); |
| 1106 | pci_disable_device(pdev); |
Tomas Winkler | a44cab4 | 2012-05-29 16:39:11 +0300 | [diff] [blame] | 1107 | |
| 1108 | misc_deregister(&mei_misc_device); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 1109 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1110 | #ifdef CONFIG_PM |
| 1111 | static int mei_pci_suspend(struct device *device) |
| 1112 | { |
| 1113 | struct pci_dev *pdev = to_pci_dev(device); |
| 1114 | struct mei_device *dev = pci_get_drvdata(pdev); |
| 1115 | int err; |
| 1116 | |
| 1117 | if (!dev) |
| 1118 | return -ENODEV; |
| 1119 | mutex_lock(&dev->device_lock); |
| 1120 | /* Stop watchdog if exists */ |
| 1121 | err = mei_wd_stop(dev, true); |
| 1122 | /* Set new mei state */ |
| 1123 | if (dev->mei_state == MEI_ENABLED || |
| 1124 | dev->mei_state == MEI_RECOVERING_FROM_RESET) { |
| 1125 | dev->mei_state = MEI_POWER_DOWN; |
| 1126 | mei_reset(dev, 0); |
| 1127 | } |
| 1128 | mutex_unlock(&dev->device_lock); |
| 1129 | |
| 1130 | free_irq(pdev->irq, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1131 | pci_disable_msi(pdev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1132 | |
| 1133 | return err; |
| 1134 | } |
| 1135 | |
| 1136 | static int mei_pci_resume(struct device *device) |
| 1137 | { |
| 1138 | struct pci_dev *pdev = to_pci_dev(device); |
| 1139 | struct mei_device *dev; |
| 1140 | int err; |
| 1141 | |
| 1142 | dev = pci_get_drvdata(pdev); |
| 1143 | if (!dev) |
| 1144 | return -ENODEV; |
| 1145 | |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1146 | pci_enable_msi(pdev); |
| 1147 | |
| 1148 | /* request and enable interrupt */ |
| 1149 | if (pci_dev_msi_enabled(pdev)) |
| 1150 | err = request_threaded_irq(pdev->irq, |
| 1151 | NULL, |
| 1152 | mei_interrupt_thread_handler, |
Tomas Winkler | 16a50b1 | 2012-06-27 17:14:20 +0300 | [diff] [blame] | 1153 | IRQF_ONESHOT, mei_driver_name, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1154 | else |
| 1155 | err = request_threaded_irq(pdev->irq, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1156 | mei_interrupt_quick_handler, |
| 1157 | mei_interrupt_thread_handler, |
| 1158 | IRQF_SHARED, mei_driver_name, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1159 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1160 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1161 | dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", |
| 1162 | pdev->irq); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1163 | return err; |
| 1164 | } |
| 1165 | |
| 1166 | mutex_lock(&dev->device_lock); |
| 1167 | dev->mei_state = MEI_POWER_UP; |
| 1168 | mei_reset(dev, 1); |
| 1169 | mutex_unlock(&dev->device_lock); |
| 1170 | |
Oren Weil | 6d70e93 | 2011-09-07 09:03:14 +0300 | [diff] [blame] | 1171 | /* Start timer if stopped in suspend */ |
| 1172 | schedule_delayed_work(&dev->timer_work, HZ); |
| 1173 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1174 | return err; |
| 1175 | } |
| 1176 | static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume); |
| 1177 | #define MEI_PM_OPS (&mei_pm_ops) |
| 1178 | #else |
Randy Dunlap | 2d99036 | 2011-05-19 08:52:34 -0700 | [diff] [blame] | 1179 | #define MEI_PM_OPS NULL |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1180 | #endif /* CONFIG_PM */ |
| 1181 | /* |
| 1182 | * PCI driver structure |
| 1183 | */ |
| 1184 | static struct pci_driver mei_driver = { |
| 1185 | .name = mei_driver_name, |
| 1186 | .id_table = mei_pci_tbl, |
| 1187 | .probe = mei_probe, |
| 1188 | .remove = __devexit_p(mei_remove), |
| 1189 | .shutdown = __devexit_p(mei_remove), |
| 1190 | .driver.pm = MEI_PM_OPS, |
| 1191 | }; |
| 1192 | |
Tomas Winkler | 6078188 | 2012-07-19 09:45:32 +0300 | [diff] [blame] | 1193 | module_pci_driver(mei_driver); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1194 | |
| 1195 | MODULE_AUTHOR("Intel Corporation"); |
| 1196 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
| 1197 | MODULE_LICENSE("GPL v2"); |