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 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 93 | * find_read_list_entry - find read list entry |
| 94 | * |
| 95 | * @dev: device structure |
| 96 | * @file: pointer to file structure |
| 97 | * |
| 98 | * returns cb on success, NULL on error |
| 99 | */ |
| 100 | static struct mei_cl_cb *find_read_list_entry( |
| 101 | struct mei_device *dev, |
| 102 | struct mei_cl *cl) |
| 103 | { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 104 | struct mei_cl_cb *pos = NULL; |
| 105 | struct mei_cl_cb *next = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 106 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 107 | dev_dbg(&dev->pdev->dev, "remove read_list CB\n"); |
Tomas Winkler | db3ed43 | 2012-11-11 17:37:59 +0200 | [diff] [blame] | 108 | list_for_each_entry_safe(pos, next, &dev->read_list.list, list) |
| 109 | if (mei_cl_cmp_id(cl, pos->cl)) |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 110 | return pos; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * mei_open - the open function |
| 116 | * |
| 117 | * @inode: pointer to inode structure |
| 118 | * @file: pointer to file structure |
| 119 | * |
| 120 | * returns 0 on success, <0 on error |
| 121 | */ |
| 122 | static int mei_open(struct inode *inode, struct file *file) |
| 123 | { |
| 124 | struct mei_cl *cl; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 125 | struct mei_device *dev; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 126 | unsigned long cl_id; |
| 127 | int err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 128 | |
| 129 | err = -ENODEV; |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 130 | if (!mei_pdev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 131 | goto out; |
| 132 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 133 | dev = pci_get_drvdata(mei_pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 134 | if (!dev) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 135 | goto out; |
| 136 | |
| 137 | mutex_lock(&dev->device_lock); |
| 138 | err = -ENOMEM; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 139 | cl = mei_cl_allocate(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 140 | if (!cl) |
Alexey Khoroshilov | 303dfbf | 2011-08-31 00:41:14 +0400 | [diff] [blame] | 141 | goto out_unlock; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 142 | |
| 143 | err = -ENODEV; |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 144 | if (dev->dev_state != MEI_DEV_ENABLED) { |
| 145 | dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n", |
| 146 | mei_dev_state_str(dev->dev_state)); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 147 | goto out_unlock; |
| 148 | } |
| 149 | err = -EMFILE; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 150 | if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) { |
| 151 | dev_err(&dev->pdev->dev, "open_handle_count exceded %d", |
| 152 | MEI_MAX_OPEN_HANDLE_COUNT); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 153 | goto out_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 154 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 155 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 156 | 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] | 157 | if (cl_id >= MEI_CLIENTS_MAX) { |
| 158 | dev_err(&dev->pdev->dev, "client_id exceded %d", |
| 159 | MEI_CLIENTS_MAX) ; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 160 | goto out_unlock; |
Tomas Winkler | 1b81294 | 2012-09-11 00:43:20 +0300 | [diff] [blame] | 161 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 162 | |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 163 | cl->host_client_id = cl_id; |
| 164 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 165 | dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id); |
| 166 | |
| 167 | dev->open_handle_count++; |
Tomas Winkler | 6f37aca | 2011-11-13 09:41:15 +0200 | [diff] [blame] | 168 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 169 | list_add_tail(&cl->link, &dev->file_list); |
| 170 | |
| 171 | set_bit(cl->host_client_id, dev->host_clients_map); |
| 172 | cl->state = MEI_FILE_INITIALIZING; |
| 173 | cl->sm_state = 0; |
| 174 | |
| 175 | file->private_data = cl; |
| 176 | mutex_unlock(&dev->device_lock); |
| 177 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 178 | return nonseekable_open(inode, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 179 | |
| 180 | out_unlock: |
| 181 | mutex_unlock(&dev->device_lock); |
| 182 | kfree(cl); |
| 183 | out: |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * mei_release - the release function |
| 189 | * |
| 190 | * @inode: pointer to inode structure |
| 191 | * @file: pointer to file structure |
| 192 | * |
| 193 | * returns 0 on success, <0 on error |
| 194 | */ |
| 195 | static int mei_release(struct inode *inode, struct file *file) |
| 196 | { |
| 197 | struct mei_cl *cl = file->private_data; |
| 198 | struct mei_cl_cb *cb; |
| 199 | struct mei_device *dev; |
| 200 | int rets = 0; |
| 201 | |
| 202 | if (WARN_ON(!cl || !cl->dev)) |
| 203 | return -ENODEV; |
| 204 | |
| 205 | dev = cl->dev; |
| 206 | |
| 207 | mutex_lock(&dev->device_lock); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame^] | 208 | if (cl == &dev->iamthif_cl) { |
| 209 | rets = mei_amthif_release(dev, file); |
| 210 | goto out; |
| 211 | } |
| 212 | if (cl->state == MEI_FILE_CONNECTED) { |
| 213 | cl->state = MEI_FILE_DISCONNECTING; |
| 214 | dev_dbg(&dev->pdev->dev, |
| 215 | "disconnecting client host client = %d, " |
| 216 | "ME client = %d\n", |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 217 | cl->host_client_id, |
| 218 | cl->me_client_id); |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame^] | 219 | rets = mei_disconnect_host_client(dev, cl); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 220 | } |
Tomas Winkler | a562d5c | 2012-11-11 17:38:01 +0200 | [diff] [blame^] | 221 | mei_cl_flush_queues(cl); |
| 222 | dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n", |
| 223 | cl->host_client_id, |
| 224 | cl->me_client_id); |
| 225 | |
| 226 | if (dev->open_handle_count > 0) { |
| 227 | clear_bit(cl->host_client_id, dev->host_clients_map); |
| 228 | dev->open_handle_count--; |
| 229 | } |
| 230 | mei_remove_client_from_file_list(dev, cl->host_client_id); |
| 231 | |
| 232 | /* free read cb */ |
| 233 | cb = NULL; |
| 234 | if (cl->read_cb) { |
| 235 | cb = find_read_list_entry(dev, cl); |
| 236 | /* Remove entry from read list */ |
| 237 | if (cb) |
| 238 | list_del(&cb->list); |
| 239 | |
| 240 | cb = cl->read_cb; |
| 241 | cl->read_cb = NULL; |
| 242 | } |
| 243 | |
| 244 | file->private_data = NULL; |
| 245 | |
| 246 | if (cb) { |
| 247 | mei_io_cb_free(cb); |
| 248 | cb = NULL; |
| 249 | } |
| 250 | |
| 251 | kfree(cl); |
| 252 | out: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 253 | mutex_unlock(&dev->device_lock); |
| 254 | return rets; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * mei_read - the read function. |
| 260 | * |
| 261 | * @file: pointer to file structure |
| 262 | * @ubuf: pointer to user buffer |
| 263 | * @length: buffer length |
| 264 | * @offset: data offset in buffer |
| 265 | * |
| 266 | * returns >=0 data length on success , <0 on error |
| 267 | */ |
| 268 | static ssize_t mei_read(struct file *file, char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 269 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 270 | { |
| 271 | struct mei_cl *cl = file->private_data; |
| 272 | struct mei_cl_cb *cb_pos = NULL; |
| 273 | struct mei_cl_cb *cb = NULL; |
| 274 | struct mei_device *dev; |
| 275 | int i; |
| 276 | int rets; |
| 277 | int err; |
| 278 | |
| 279 | |
| 280 | if (WARN_ON(!cl || !cl->dev)) |
| 281 | return -ENODEV; |
| 282 | |
| 283 | dev = cl->dev; |
| 284 | |
| 285 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 286 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 287 | rets = -ENODEV; |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) { |
| 292 | /* Do not allow to read watchdog client */ |
Tomas Winkler | 07b509b | 2012-07-23 14:05:39 +0300 | [diff] [blame] | 293 | i = mei_me_cl_by_uuid(dev, &mei_wd_guid); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 294 | if (i >= 0) { |
| 295 | struct mei_me_client *me_client = &dev->me_clients[i]; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 296 | if (cl->me_client_id == me_client->client_id) { |
| 297 | rets = -EBADF; |
| 298 | goto out; |
| 299 | } |
| 300 | } |
| 301 | } else { |
| 302 | cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT; |
| 303 | } |
| 304 | |
| 305 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 306 | rets = mei_amthif_read(dev, file, ubuf, length, offset); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 307 | goto out; |
| 308 | } |
| 309 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 310 | if (cl->read_cb && cl->read_cb->buf_idx > *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 311 | cb = cl->read_cb; |
| 312 | goto copy_buffer; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 313 | } else if (cl->read_cb && cl->read_cb->buf_idx > 0 && |
| 314 | cl->read_cb->buf_idx <= *offset) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 315 | cb = cl->read_cb; |
| 316 | rets = 0; |
| 317 | goto free; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 318 | } 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] | 319 | /*Offset needs to be cleaned for contiguous reads*/ |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 320 | *offset = 0; |
| 321 | rets = 0; |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | err = mei_start_read(dev, cl); |
| 326 | if (err && err != -EBUSY) { |
| 327 | dev_dbg(&dev->pdev->dev, |
| 328 | "mei start read failure with status = %d\n", err); |
| 329 | rets = err; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | if (MEI_READ_COMPLETE != cl->reading_state && |
| 334 | !waitqueue_active(&cl->rx_wait)) { |
| 335 | if (file->f_flags & O_NONBLOCK) { |
| 336 | rets = -EAGAIN; |
| 337 | goto out; |
| 338 | } |
| 339 | |
| 340 | mutex_unlock(&dev->device_lock); |
| 341 | |
| 342 | if (wait_event_interruptible(cl->rx_wait, |
| 343 | (MEI_READ_COMPLETE == cl->reading_state || |
| 344 | MEI_FILE_INITIALIZING == cl->state || |
| 345 | MEI_FILE_DISCONNECTED == cl->state || |
| 346 | MEI_FILE_DISCONNECTING == cl->state))) { |
| 347 | if (signal_pending(current)) |
| 348 | return -EINTR; |
| 349 | return -ERESTARTSYS; |
| 350 | } |
| 351 | |
| 352 | mutex_lock(&dev->device_lock); |
| 353 | if (MEI_FILE_INITIALIZING == cl->state || |
| 354 | MEI_FILE_DISCONNECTED == cl->state || |
| 355 | MEI_FILE_DISCONNECTING == cl->state) { |
| 356 | rets = -EBUSY; |
| 357 | goto out; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | cb = cl->read_cb; |
| 362 | |
| 363 | if (!cb) { |
| 364 | rets = -ENODEV; |
| 365 | goto out; |
| 366 | } |
| 367 | if (cl->reading_state != MEI_READ_COMPLETE) { |
| 368 | rets = 0; |
| 369 | goto out; |
| 370 | } |
| 371 | /* now copy the data to user space */ |
| 372 | copy_buffer: |
| 373 | dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n", |
| 374 | cb->response_buffer.size); |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 375 | dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx); |
| 376 | if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 377 | rets = -EMSGSIZE; |
| 378 | goto free; |
| 379 | } |
| 380 | |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 381 | /* length is being truncated to PAGE_SIZE, |
| 382 | * however buf_idx may point beyond that */ |
| 383 | length = min_t(size_t, length, cb->buf_idx - *offset); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 384 | |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 385 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 386 | rets = -EFAULT; |
| 387 | goto free; |
| 388 | } |
| 389 | |
| 390 | rets = length; |
| 391 | *offset += length; |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 392 | if ((unsigned long)*offset < cb->buf_idx) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 393 | goto out; |
| 394 | |
| 395 | free: |
| 396 | cb_pos = find_read_list_entry(dev, cl); |
| 397 | /* Remove entry from read list */ |
| 398 | if (cb_pos) |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 399 | list_del(&cb_pos->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 400 | mei_io_cb_free(cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 401 | cl->reading_state = MEI_IDLE; |
| 402 | cl->read_cb = NULL; |
| 403 | cl->read_pending = 0; |
| 404 | out: |
| 405 | dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets); |
| 406 | mutex_unlock(&dev->device_lock); |
| 407 | return rets; |
| 408 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 409 | /** |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 410 | * mei_write - the write function. |
| 411 | * |
| 412 | * @file: pointer to file structure |
| 413 | * @ubuf: pointer to user buffer |
| 414 | * @length: buffer length |
| 415 | * @offset: data offset in buffer |
| 416 | * |
| 417 | * returns >=0 data length on success , <0 on error |
| 418 | */ |
| 419 | static ssize_t mei_write(struct file *file, const char __user *ubuf, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 420 | size_t length, loff_t *offset) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 421 | { |
| 422 | struct mei_cl *cl = file->private_data; |
| 423 | struct mei_cl_cb *write_cb = NULL; |
| 424 | struct mei_msg_hdr mei_hdr; |
| 425 | struct mei_device *dev; |
| 426 | unsigned long timeout = 0; |
| 427 | int rets; |
| 428 | int i; |
| 429 | |
| 430 | if (WARN_ON(!cl || !cl->dev)) |
| 431 | return -ENODEV; |
| 432 | |
| 433 | dev = cl->dev; |
| 434 | |
| 435 | mutex_lock(&dev->device_lock); |
| 436 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 437 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 438 | rets = -ENODEV; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 439 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 440 | } |
| 441 | |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 442 | i = mei_me_cl_by_id(dev, cl->me_client_id); |
| 443 | if (i < 0) { |
| 444 | rets = -ENODEV; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 445 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 446 | } |
| 447 | if (length > dev->me_clients[i].props.max_msg_length || length <= 0) { |
| 448 | rets = -EMSGSIZE; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 449 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (cl->state != MEI_FILE_CONNECTED) { |
| 453 | rets = -ENODEV; |
| 454 | dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d", |
| 455 | cl->host_client_id, cl->me_client_id); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 456 | goto err; |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 457 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 458 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 459 | write_cb = mei_amthif_find_read_list_entry(dev, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 460 | |
| 461 | if (write_cb) { |
| 462 | timeout = write_cb->read_time + |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 463 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 464 | |
| 465 | if (time_after(jiffies, timeout) || |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 466 | cl->reading_state == MEI_READ_COMPLETE) { |
| 467 | *offset = 0; |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 468 | list_del(&write_cb->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 469 | mei_io_cb_free(write_cb); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 470 | write_cb = NULL; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* free entry used in read */ |
| 476 | if (cl->reading_state == MEI_READ_COMPLETE) { |
| 477 | *offset = 0; |
| 478 | write_cb = find_read_list_entry(dev, cl); |
| 479 | if (write_cb) { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 480 | list_del(&write_cb->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 481 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 482 | write_cb = NULL; |
| 483 | cl->reading_state = MEI_IDLE; |
| 484 | cl->read_cb = NULL; |
| 485 | cl->read_pending = 0; |
| 486 | } |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 487 | } else if (cl->reading_state == MEI_IDLE && !cl->read_pending) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 488 | *offset = 0; |
| 489 | |
| 490 | |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 491 | write_cb = mei_io_cb_init(cl, file); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 492 | if (!write_cb) { |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 493 | dev_err(&dev->pdev->dev, "write cb allocation failed\n"); |
| 494 | rets = -ENOMEM; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 495 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 496 | } |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 497 | rets = mei_io_cb_alloc_req_buf(write_cb, length); |
| 498 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 499 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 500 | |
Tomas Winkler | 33d28c9 | 2012-10-09 16:50:17 +0200 | [diff] [blame] | 501 | dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 502 | |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 503 | rets = copy_from_user(write_cb->request_buffer.data, ubuf, length); |
| 504 | if (rets) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 505 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 506 | |
| 507 | cl->sm_state = 0; |
| 508 | if (length == 4 && |
| 509 | ((memcmp(mei_wd_state_independence_msg[0], |
| 510 | write_cb->request_buffer.data, 4) == 0) || |
| 511 | (memcmp(mei_wd_state_independence_msg[1], |
| 512 | write_cb->request_buffer.data, 4) == 0) || |
| 513 | (memcmp(mei_wd_state_independence_msg[2], |
| 514 | write_cb->request_buffer.data, 4) == 0))) |
| 515 | cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT; |
| 516 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 517 | if (cl == &dev->iamthif_cl) { |
Tomas Winkler | ab5c4a5 | 2012-11-01 21:17:18 +0200 | [diff] [blame] | 518 | rets = mei_amthif_write(dev, write_cb); |
| 519 | |
| 520 | if (rets) { |
| 521 | dev_err(&dev->pdev->dev, |
| 522 | "amthi write failed with status = %d\n", rets); |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 523 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 524 | } |
| 525 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 75f0ee1 | 2012-10-09 16:50:18 +0200 | [diff] [blame] | 526 | return length; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 527 | } |
| 528 | |
Tomas Winkler | 4b8960b | 2012-11-11 17:38:00 +0200 | [diff] [blame] | 529 | write_cb->fop_type = MEI_FOP_WRITE; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 530 | |
| 531 | dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n", |
| 532 | cl->host_client_id, cl->me_client_id); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 533 | rets = mei_flow_ctrl_creds(dev, cl); |
| 534 | if (rets < 0) |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 535 | goto err; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 536 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 537 | if (rets == 0 || dev->mei_host_buffer_is_empty == false) { |
Tomas Winkler | ebb108ef | 2012-10-09 16:50:16 +0200 | [diff] [blame] | 538 | write_cb->buf_idx = 0; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 539 | mei_hdr.msg_complete = 0; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 540 | cl->writing_state = MEI_WRITING; |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 541 | goto out; |
| 542 | } |
| 543 | |
| 544 | dev->mei_host_buffer_is_empty = false; |
| 545 | if (length > mei_hbuf_max_data(dev)) { |
| 546 | mei_hdr.length = mei_hbuf_max_data(dev); |
| 547 | mei_hdr.msg_complete = 0; |
| 548 | } else { |
| 549 | mei_hdr.length = length; |
| 550 | mei_hdr.msg_complete = 1; |
| 551 | } |
| 552 | mei_hdr.host_addr = cl->host_client_id; |
| 553 | mei_hdr.me_addr = cl->me_client_id; |
| 554 | mei_hdr.reserved = 0; |
| 555 | dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n", |
| 556 | *((u32 *) &mei_hdr)); |
| 557 | if (mei_write_message(dev, &mei_hdr, |
| 558 | write_cb->request_buffer.data, mei_hdr.length)) { |
| 559 | rets = -ENODEV; |
| 560 | goto err; |
| 561 | } |
| 562 | cl->writing_state = MEI_WRITING; |
| 563 | write_cb->buf_idx = mei_hdr.length; |
| 564 | |
| 565 | out: |
| 566 | if (mei_hdr.msg_complete) { |
| 567 | if (mei_flow_ctrl_reduce(dev, cl)) { |
| 568 | rets = -ENODEV; |
| 569 | goto err; |
| 570 | } |
| 571 | list_add_tail(&write_cb->list, &dev->write_waiting_list.list); |
| 572 | } else { |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 573 | list_add_tail(&write_cb->list, &dev->write_list.list); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 574 | } |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 575 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 576 | mutex_unlock(&dev->device_lock); |
| 577 | return length; |
| 578 | |
Tomas Winkler | b0d0cf7 | 2012-11-01 21:17:13 +0200 | [diff] [blame] | 579 | err: |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 580 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 581 | mei_io_cb_free(write_cb); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 582 | return rets; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | /** |
| 587 | * mei_ioctl - the IOCTL function |
| 588 | * |
| 589 | * @file: pointer to file structure |
| 590 | * @cmd: ioctl command |
| 591 | * @data: pointer to mei message structure |
| 592 | * |
| 593 | * returns 0 on success , <0 on error |
| 594 | */ |
| 595 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) |
| 596 | { |
| 597 | struct mei_device *dev; |
| 598 | struct mei_cl *cl = file->private_data; |
| 599 | struct mei_connect_client_data *connect_data = NULL; |
| 600 | int rets; |
| 601 | |
| 602 | if (cmd != IOCTL_MEI_CONNECT_CLIENT) |
| 603 | return -EINVAL; |
| 604 | |
| 605 | if (WARN_ON(!cl || !cl->dev)) |
| 606 | return -ENODEV; |
| 607 | |
| 608 | dev = cl->dev; |
| 609 | |
| 610 | dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd); |
| 611 | |
| 612 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 613 | if (dev->dev_state != MEI_DEV_ENABLED) { |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 614 | rets = -ENODEV; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
| 619 | |
| 620 | connect_data = kzalloc(sizeof(struct mei_connect_client_data), |
| 621 | GFP_KERNEL); |
| 622 | if (!connect_data) { |
| 623 | rets = -ENOMEM; |
| 624 | goto out; |
| 625 | } |
| 626 | dev_dbg(&dev->pdev->dev, "copy connect data from user\n"); |
| 627 | if (copy_from_user(connect_data, (char __user *)data, |
| 628 | sizeof(struct mei_connect_client_data))) { |
| 629 | dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n"); |
| 630 | rets = -EFAULT; |
| 631 | goto out; |
| 632 | } |
| 633 | rets = mei_ioctl_connect_client(file, connect_data); |
| 634 | |
| 635 | /* if all is ok, copying the data back to user. */ |
| 636 | if (rets) |
| 637 | goto out; |
| 638 | |
| 639 | dev_dbg(&dev->pdev->dev, "copy connect data to user\n"); |
| 640 | if (copy_to_user((char __user *)data, connect_data, |
| 641 | sizeof(struct mei_connect_client_data))) { |
| 642 | dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n"); |
| 643 | rets = -EFAULT; |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | out: |
| 648 | kfree(connect_data); |
| 649 | mutex_unlock(&dev->device_lock); |
| 650 | return rets; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * mei_compat_ioctl - the compat IOCTL function |
| 655 | * |
| 656 | * @file: pointer to file structure |
| 657 | * @cmd: ioctl command |
| 658 | * @data: pointer to mei message structure |
| 659 | * |
| 660 | * returns 0 on success , <0 on error |
| 661 | */ |
| 662 | #ifdef CONFIG_COMPAT |
| 663 | static long mei_compat_ioctl(struct file *file, |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 664 | unsigned int cmd, unsigned long data) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 665 | { |
| 666 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); |
| 667 | } |
| 668 | #endif |
| 669 | |
| 670 | |
| 671 | /** |
| 672 | * mei_poll - the poll function |
| 673 | * |
| 674 | * @file: pointer to file structure |
| 675 | * @wait: pointer to poll_table structure |
| 676 | * |
| 677 | * returns poll mask |
| 678 | */ |
| 679 | static unsigned int mei_poll(struct file *file, poll_table *wait) |
| 680 | { |
| 681 | struct mei_cl *cl = file->private_data; |
| 682 | struct mei_device *dev; |
| 683 | unsigned int mask = 0; |
| 684 | |
| 685 | if (WARN_ON(!cl || !cl->dev)) |
| 686 | return mask; |
| 687 | |
| 688 | dev = cl->dev; |
| 689 | |
| 690 | mutex_lock(&dev->device_lock); |
| 691 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 692 | if (dev->dev_state != MEI_DEV_ENABLED) |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 693 | goto out; |
| 694 | |
| 695 | |
| 696 | if (cl == &dev->iamthif_cl) { |
| 697 | mutex_unlock(&dev->device_lock); |
| 698 | poll_wait(file, &dev->iamthif_cl.wait, wait); |
| 699 | mutex_lock(&dev->device_lock); |
| 700 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && |
| 701 | dev->iamthif_file_object == file) { |
| 702 | mask |= (POLLIN | POLLRDNORM); |
| 703 | dev_dbg(&dev->pdev->dev, "run next amthi cb\n"); |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 704 | mei_amthif_run_next_cmd(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 705 | } |
| 706 | goto out; |
| 707 | } |
| 708 | |
| 709 | mutex_unlock(&dev->device_lock); |
| 710 | poll_wait(file, &cl->tx_wait, wait); |
| 711 | mutex_lock(&dev->device_lock); |
| 712 | if (MEI_WRITE_COMPLETE == cl->writing_state) |
| 713 | mask |= (POLLIN | POLLRDNORM); |
| 714 | |
| 715 | out: |
| 716 | mutex_unlock(&dev->device_lock); |
| 717 | return mask; |
| 718 | } |
| 719 | |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 720 | /* |
| 721 | * file operations structure will be used for mei char device. |
| 722 | */ |
| 723 | static const struct file_operations mei_fops = { |
| 724 | .owner = THIS_MODULE, |
| 725 | .read = mei_read, |
| 726 | .unlocked_ioctl = mei_ioctl, |
| 727 | #ifdef CONFIG_COMPAT |
| 728 | .compat_ioctl = mei_compat_ioctl, |
| 729 | #endif |
| 730 | .open = mei_open, |
| 731 | .release = mei_release, |
| 732 | .write = mei_write, |
| 733 | .poll = mei_poll, |
| 734 | .llseek = no_llseek |
| 735 | }; |
| 736 | |
| 737 | |
| 738 | /* |
| 739 | * Misc Device Struct |
| 740 | */ |
| 741 | static struct miscdevice mei_misc_device = { |
Tomas Winkler | c38ea24 | 2012-04-02 20:32:39 +0300 | [diff] [blame] | 742 | .name = "mei", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 743 | .fops = &mei_fops, |
| 744 | .minor = MISC_DYNAMIC_MINOR, |
| 745 | }; |
| 746 | |
| 747 | /** |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 748 | * mei_quirk_probe - probe for devices that doesn't valid ME interface |
| 749 | * @pdev: PCI device structure |
| 750 | * @ent: entry into pci_device_table |
| 751 | * |
| 752 | * returns true if ME Interface is valid, false otherwise |
| 753 | */ |
| 754 | static bool __devinit mei_quirk_probe(struct pci_dev *pdev, |
| 755 | const struct pci_device_id *ent) |
| 756 | { |
| 757 | u32 reg; |
| 758 | if (ent->device == MEI_DEV_ID_PBG_1) { |
| 759 | pci_read_config_dword(pdev, 0x48, ®); |
| 760 | /* make sure that bit 9 is up and bit 10 is down */ |
| 761 | if ((reg & 0x600) == 0x200) { |
| 762 | dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n"); |
| 763 | return false; |
| 764 | } |
| 765 | } |
| 766 | return true; |
| 767 | } |
| 768 | /** |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 769 | * mei_probe - Device Initialization Routine |
| 770 | * |
| 771 | * @pdev: PCI device structure |
| 772 | * @ent: entry in kcs_pci_tbl |
| 773 | * |
| 774 | * returns 0 on success, <0 on failure. |
| 775 | */ |
| 776 | static int __devinit mei_probe(struct pci_dev *pdev, |
| 777 | const struct pci_device_id *ent) |
| 778 | { |
| 779 | struct mei_device *dev; |
| 780 | int err; |
| 781 | |
| 782 | mutex_lock(&mei_mutex); |
Tomas Winkler | 9a123f1 | 2012-08-06 15:23:55 +0300 | [diff] [blame] | 783 | |
| 784 | if (!mei_quirk_probe(pdev, ent)) { |
| 785 | err = -ENODEV; |
| 786 | goto end; |
| 787 | } |
| 788 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 789 | if (mei_pdev) { |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 790 | err = -EEXIST; |
| 791 | goto end; |
| 792 | } |
| 793 | /* enable pci dev */ |
| 794 | err = pci_enable_device(pdev); |
| 795 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 796 | dev_err(&pdev->dev, "failed to enable pci device.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 797 | goto end; |
| 798 | } |
| 799 | /* set PCI host mastering */ |
| 800 | pci_set_master(pdev); |
| 801 | /* pci request regions for mei driver */ |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 802 | err = pci_request_regions(pdev, KBUILD_MODNAME); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 803 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 804 | dev_err(&pdev->dev, "failed to get pci regions.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 805 | goto disable_device; |
| 806 | } |
| 807 | /* allocates and initializes the mei dev structure */ |
| 808 | dev = mei_device_init(pdev); |
| 809 | if (!dev) { |
| 810 | err = -ENOMEM; |
| 811 | goto release_regions; |
| 812 | } |
| 813 | /* mapping IO device memory */ |
| 814 | dev->mem_addr = pci_iomap(pdev, 0, 0); |
| 815 | if (!dev->mem_addr) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 816 | dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 817 | err = -ENOMEM; |
| 818 | goto free_device; |
| 819 | } |
| 820 | pci_enable_msi(pdev); |
| 821 | |
| 822 | /* request and enable interrupt */ |
| 823 | if (pci_dev_msi_enabled(pdev)) |
| 824 | err = request_threaded_irq(pdev->irq, |
| 825 | NULL, |
| 826 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 827 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 828 | else |
| 829 | err = request_threaded_irq(pdev->irq, |
| 830 | mei_interrupt_quick_handler, |
| 831 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 832 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 833 | |
| 834 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 835 | dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 836 | pdev->irq); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 837 | goto disable_msi; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 838 | } |
| 839 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 840 | if (mei_hw_init(dev)) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 841 | dev_err(&pdev->dev, "init hw failure.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 842 | err = -ENODEV; |
| 843 | goto release_irq; |
| 844 | } |
| 845 | |
| 846 | err = misc_register(&mei_misc_device); |
| 847 | if (err) |
| 848 | goto release_irq; |
| 849 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 850 | mei_pdev = pdev; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 851 | pci_set_drvdata(pdev, dev); |
| 852 | |
| 853 | |
| 854 | schedule_delayed_work(&dev->timer_work, HZ); |
| 855 | |
| 856 | mutex_unlock(&mei_mutex); |
| 857 | |
Tomas Winkler | 2f3d2b4 | 2012-03-19 22:38:13 +0200 | [diff] [blame] | 858 | pr_debug("initialization successful.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 859 | |
| 860 | return 0; |
| 861 | |
| 862 | release_irq: |
| 863 | /* disable interrupts */ |
| 864 | dev->host_hw_state = mei_hcsr_read(dev); |
| 865 | mei_disable_interrupts(dev); |
| 866 | flush_scheduled_work(); |
| 867 | free_irq(pdev->irq, dev); |
Samuel Ortiz | 169dc38 | 2012-06-11 12:18:30 +0300 | [diff] [blame] | 868 | disable_msi: |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 869 | pci_disable_msi(pdev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 870 | pci_iounmap(pdev, dev->mem_addr); |
| 871 | free_device: |
| 872 | kfree(dev); |
| 873 | release_regions: |
| 874 | pci_release_regions(pdev); |
| 875 | disable_device: |
| 876 | pci_disable_device(pdev); |
| 877 | end: |
| 878 | mutex_unlock(&mei_mutex); |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 879 | dev_err(&pdev->dev, "initialization failed.\n"); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 880 | return err; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * mei_remove - Device Removal Routine |
| 885 | * |
| 886 | * @pdev: PCI device structure |
| 887 | * |
| 888 | * mei_remove is called by the PCI subsystem to alert the driver |
| 889 | * that it should release a PCI device. |
| 890 | */ |
| 891 | static void __devexit mei_remove(struct pci_dev *pdev) |
| 892 | { |
| 893 | struct mei_device *dev; |
| 894 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 895 | if (mei_pdev != pdev) |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 896 | return; |
| 897 | |
| 898 | dev = pci_get_drvdata(pdev); |
| 899 | if (!dev) |
| 900 | return; |
| 901 | |
| 902 | mutex_lock(&dev->device_lock); |
| 903 | |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 904 | cancel_delayed_work(&dev->timer_work); |
| 905 | |
| 906 | mei_wd_stop(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 907 | |
Tomas Winkler | daed6b5 | 2012-08-17 09:54:23 +0300 | [diff] [blame] | 908 | mei_pdev = NULL; |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 909 | |
| 910 | if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) { |
| 911 | dev->iamthif_cl.state = MEI_FILE_DISCONNECTING; |
| 912 | mei_disconnect_host_client(dev, &dev->iamthif_cl); |
| 913 | } |
| 914 | if (dev->wd_cl.state == MEI_FILE_CONNECTED) { |
| 915 | dev->wd_cl.state = MEI_FILE_DISCONNECTING; |
| 916 | mei_disconnect_host_client(dev, &dev->wd_cl); |
| 917 | } |
| 918 | |
| 919 | /* Unregistering watchdog device */ |
Tomas Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 920 | mei_watchdog_unregister(dev); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 921 | |
| 922 | /* remove entry if already in list */ |
| 923 | dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n"); |
| 924 | mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id); |
| 925 | mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id); |
| 926 | |
| 927 | dev->iamthif_current_cb = NULL; |
| 928 | dev->me_clients_num = 0; |
| 929 | |
| 930 | mutex_unlock(&dev->device_lock); |
| 931 | |
| 932 | flush_scheduled_work(); |
| 933 | |
| 934 | /* disable interrupts */ |
| 935 | mei_disable_interrupts(dev); |
| 936 | |
| 937 | free_irq(pdev->irq, dev); |
| 938 | pci_disable_msi(pdev); |
| 939 | pci_set_drvdata(pdev, NULL); |
| 940 | |
| 941 | if (dev->mem_addr) |
| 942 | pci_iounmap(pdev, dev->mem_addr); |
| 943 | |
| 944 | kfree(dev); |
| 945 | |
| 946 | pci_release_regions(pdev); |
| 947 | pci_disable_device(pdev); |
Tomas Winkler | a44cab4 | 2012-05-29 16:39:11 +0300 | [diff] [blame] | 948 | |
| 949 | misc_deregister(&mei_misc_device); |
Oren Weil | 5b881e3 | 2011-11-13 09:41:14 +0200 | [diff] [blame] | 950 | } |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 951 | #ifdef CONFIG_PM |
| 952 | static int mei_pci_suspend(struct device *device) |
| 953 | { |
| 954 | struct pci_dev *pdev = to_pci_dev(device); |
| 955 | struct mei_device *dev = pci_get_drvdata(pdev); |
| 956 | int err; |
| 957 | |
| 958 | if (!dev) |
| 959 | return -ENODEV; |
| 960 | mutex_lock(&dev->device_lock); |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 961 | |
| 962 | cancel_delayed_work(&dev->timer_work); |
| 963 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 964 | /* Stop watchdog if exists */ |
Tomas Winkler | c216fde | 2012-08-16 19:39:43 +0300 | [diff] [blame] | 965 | err = mei_wd_stop(dev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 966 | /* Set new mei state */ |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 967 | if (dev->dev_state == MEI_DEV_ENABLED || |
| 968 | dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) { |
| 969 | dev->dev_state = MEI_DEV_POWER_DOWN; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 970 | mei_reset(dev, 0); |
| 971 | } |
| 972 | mutex_unlock(&dev->device_lock); |
| 973 | |
| 974 | free_irq(pdev->irq, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 975 | pci_disable_msi(pdev); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 976 | |
| 977 | return err; |
| 978 | } |
| 979 | |
| 980 | static int mei_pci_resume(struct device *device) |
| 981 | { |
| 982 | struct pci_dev *pdev = to_pci_dev(device); |
| 983 | struct mei_device *dev; |
| 984 | int err; |
| 985 | |
| 986 | dev = pci_get_drvdata(pdev); |
| 987 | if (!dev) |
| 988 | return -ENODEV; |
| 989 | |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 990 | pci_enable_msi(pdev); |
| 991 | |
| 992 | /* request and enable interrupt */ |
| 993 | if (pci_dev_msi_enabled(pdev)) |
| 994 | err = request_threaded_irq(pdev->irq, |
| 995 | NULL, |
| 996 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 997 | IRQF_ONESHOT, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 998 | else |
| 999 | err = request_threaded_irq(pdev->irq, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1000 | mei_interrupt_quick_handler, |
| 1001 | mei_interrupt_thread_handler, |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1002 | IRQF_SHARED, KBUILD_MODNAME, dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1003 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1004 | if (err) { |
Tomas Winkler | 32c826b | 2012-05-08 23:04:56 +0300 | [diff] [blame] | 1005 | dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", |
| 1006 | pdev->irq); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1007 | return err; |
| 1008 | } |
| 1009 | |
| 1010 | mutex_lock(&dev->device_lock); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 1011 | dev->dev_state = MEI_DEV_POWER_UP; |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1012 | mei_reset(dev, 1); |
| 1013 | mutex_unlock(&dev->device_lock); |
| 1014 | |
Oren Weil | 6d70e93 | 2011-09-07 09:03:14 +0300 | [diff] [blame] | 1015 | /* Start timer if stopped in suspend */ |
| 1016 | schedule_delayed_work(&dev->timer_work, HZ); |
| 1017 | |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1018 | return err; |
| 1019 | } |
| 1020 | static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume); |
| 1021 | #define MEI_PM_OPS (&mei_pm_ops) |
| 1022 | #else |
Randy Dunlap | 2d99036 | 2011-05-19 08:52:34 -0700 | [diff] [blame] | 1023 | #define MEI_PM_OPS NULL |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1024 | #endif /* CONFIG_PM */ |
| 1025 | /* |
| 1026 | * PCI driver structure |
| 1027 | */ |
| 1028 | static struct pci_driver mei_driver = { |
Tomas Winkler | 068c0ae | 2012-08-07 00:03:54 +0300 | [diff] [blame] | 1029 | .name = KBUILD_MODNAME, |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1030 | .id_table = mei_pci_tbl, |
| 1031 | .probe = mei_probe, |
| 1032 | .remove = __devexit_p(mei_remove), |
| 1033 | .shutdown = __devexit_p(mei_remove), |
| 1034 | .driver.pm = MEI_PM_OPS, |
| 1035 | }; |
| 1036 | |
Tomas Winkler | 6078188 | 2012-07-19 09:45:32 +0300 | [diff] [blame] | 1037 | module_pci_driver(mei_driver); |
Oren Weil | ab84116 | 2011-05-15 13:43:41 +0300 | [diff] [blame] | 1038 | |
| 1039 | MODULE_AUTHOR("Intel Corporation"); |
| 1040 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
| 1041 | MODULE_LICENSE("GPL v2"); |