Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 733ba91c | 2012-02-09 19:25:53 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +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 | |
| 17 | |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/kthread.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/jiffies.h> |
| 23 | |
| 24 | #include "mei_dev.h" |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 25 | #include <linux/mei.h> |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 26 | #include "hw.h" |
| 27 | #include "interface.h" |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * mei_interrupt_quick_handler - The ISR of the MEI device |
| 32 | * |
| 33 | * @irq: The irq number |
| 34 | * @dev_id: pointer to the device structure |
| 35 | * |
| 36 | * returns irqreturn_t |
| 37 | */ |
| 38 | irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id) |
| 39 | { |
| 40 | struct mei_device *dev = (struct mei_device *) dev_id; |
| 41 | u32 csr_reg = mei_hcsr_read(dev); |
| 42 | |
| 43 | if ((csr_reg & H_IS) != H_IS) |
| 44 | return IRQ_NONE; |
| 45 | |
| 46 | /* clear H_IS bit in H_CSR */ |
| 47 | mei_reg_write(dev, H_CSR, csr_reg); |
| 48 | |
| 49 | return IRQ_WAKE_THREAD; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * _mei_cmpl - processes completed operation. |
| 54 | * |
| 55 | * @cl: private data of the file object. |
| 56 | * @cb_pos: callback block. |
| 57 | */ |
| 58 | static void _mei_cmpl(struct mei_cl *cl, struct mei_cl_cb *cb_pos) |
| 59 | { |
| 60 | if (cb_pos->major_file_operations == MEI_WRITE) { |
| 61 | mei_free_cb_private(cb_pos); |
| 62 | cb_pos = NULL; |
| 63 | cl->writing_state = MEI_WRITE_COMPLETE; |
| 64 | if (waitqueue_active(&cl->tx_wait)) |
| 65 | wake_up_interruptible(&cl->tx_wait); |
| 66 | |
| 67 | } else if (cb_pos->major_file_operations == MEI_READ && |
| 68 | MEI_READING == cl->reading_state) { |
| 69 | cl->reading_state = MEI_READ_COMPLETE; |
| 70 | if (waitqueue_active(&cl->rx_wait)) |
| 71 | wake_up_interruptible(&cl->rx_wait); |
| 72 | |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * _mei_cmpl_iamthif - processes completed iamthif operation. |
| 78 | * |
| 79 | * @dev: the device structure. |
| 80 | * @cb_pos: callback block. |
| 81 | */ |
| 82 | static void _mei_cmpl_iamthif(struct mei_device *dev, struct mei_cl_cb *cb_pos) |
| 83 | { |
| 84 | if (dev->iamthif_canceled != 1) { |
| 85 | dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE; |
| 86 | dev->iamthif_stall_timer = 0; |
| 87 | memcpy(cb_pos->response_buffer.data, |
| 88 | dev->iamthif_msg_buf, |
| 89 | dev->iamthif_msg_buf_index); |
| 90 | list_add_tail(&cb_pos->cb_list, |
| 91 | &dev->amthi_read_complete_list.mei_cb.cb_list); |
| 92 | dev_dbg(&dev->pdev->dev, "amthi read completed.\n"); |
| 93 | dev->iamthif_timer = jiffies; |
| 94 | dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n", |
| 95 | dev->iamthif_timer); |
| 96 | } else { |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 97 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | dev_dbg(&dev->pdev->dev, "completing amthi call back.\n"); |
| 101 | wake_up_interruptible(&dev->iamthif_cl.wait); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * mei_irq_thread_read_amthi_message - bottom half read routine after ISR to |
| 107 | * handle the read amthi message data processing. |
| 108 | * |
| 109 | * @complete_list: An instance of our list structure |
| 110 | * @dev: the device structure |
| 111 | * @mei_hdr: header of amthi message |
| 112 | * |
| 113 | * returns 0 on success, <0 on failure. |
| 114 | */ |
| 115 | static int mei_irq_thread_read_amthi_message(struct mei_io_list *complete_list, |
| 116 | struct mei_device *dev, |
| 117 | struct mei_msg_hdr *mei_hdr) |
| 118 | { |
| 119 | struct mei_cl *cl; |
| 120 | struct mei_cl_cb *cb; |
| 121 | unsigned char *buffer; |
| 122 | |
| 123 | BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id); |
| 124 | BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING); |
| 125 | |
Tomas Winkler | edf1eed | 2012-02-09 19:25:54 +0200 | [diff] [blame] | 126 | buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 127 | BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length); |
| 128 | |
| 129 | mei_read_slots(dev, buffer, mei_hdr->length); |
| 130 | |
| 131 | dev->iamthif_msg_buf_index += mei_hdr->length; |
| 132 | |
| 133 | if (!mei_hdr->msg_complete) |
| 134 | return 0; |
| 135 | |
| 136 | dev_dbg(&dev->pdev->dev, |
| 137 | "amthi_message_buffer_index =%d\n", |
| 138 | mei_hdr->length); |
| 139 | |
| 140 | dev_dbg(&dev->pdev->dev, "completed amthi read.\n "); |
| 141 | if (!dev->iamthif_current_cb) |
| 142 | return -ENODEV; |
| 143 | |
| 144 | cb = dev->iamthif_current_cb; |
| 145 | dev->iamthif_current_cb = NULL; |
| 146 | |
| 147 | cl = (struct mei_cl *)cb->file_private; |
| 148 | if (!cl) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | dev->iamthif_stall_timer = 0; |
| 152 | cb->information = dev->iamthif_msg_buf_index; |
| 153 | cb->read_time = jiffies; |
| 154 | if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) { |
| 155 | /* found the iamthif cb */ |
| 156 | dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n "); |
| 157 | dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n "); |
| 158 | list_add_tail(&cb->cb_list, |
| 159 | &complete_list->mei_cb.cb_list); |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * _mei_irq_thread_state_ok - checks if mei header matches file private data |
| 166 | * |
| 167 | * @cl: private data of the file object |
| 168 | * @mei_hdr: header of mei client message |
| 169 | * |
| 170 | * returns !=0 if matches, 0 if no match. |
| 171 | */ |
| 172 | static int _mei_irq_thread_state_ok(struct mei_cl *cl, |
| 173 | struct mei_msg_hdr *mei_hdr) |
| 174 | { |
| 175 | return (cl->host_client_id == mei_hdr->host_addr && |
| 176 | cl->me_client_id == mei_hdr->me_addr && |
| 177 | cl->state == MEI_FILE_CONNECTED && |
| 178 | MEI_READ_COMPLETE != cl->reading_state); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * mei_irq_thread_read_client_message - bottom half read routine after ISR to |
| 183 | * handle the read mei client message data processing. |
| 184 | * |
| 185 | * @complete_list: An instance of our list structure |
| 186 | * @dev: the device structure |
| 187 | * @mei_hdr: header of mei client message |
| 188 | * |
| 189 | * returns 0 on success, <0 on failure. |
| 190 | */ |
| 191 | static int mei_irq_thread_read_client_message(struct mei_io_list *complete_list, |
| 192 | struct mei_device *dev, |
| 193 | struct mei_msg_hdr *mei_hdr) |
| 194 | { |
| 195 | struct mei_cl *cl; |
| 196 | struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL; |
Tomas Winkler | 479bc59 | 2011-06-16 00:46:03 +0300 | [diff] [blame] | 197 | unsigned char *buffer = NULL; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 198 | |
| 199 | dev_dbg(&dev->pdev->dev, "start client msg\n"); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 200 | if (list_empty(&dev->read_list.mei_cb.cb_list)) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 201 | goto quit; |
| 202 | |
| 203 | list_for_each_entry_safe(cb_pos, cb_next, |
| 204 | &dev->read_list.mei_cb.cb_list, cb_list) { |
| 205 | cl = (struct mei_cl *)cb_pos->file_private; |
| 206 | if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) { |
| 207 | cl->reading_state = MEI_READING; |
Tomas Winkler | edf1eed | 2012-02-09 19:25:54 +0200 | [diff] [blame] | 208 | buffer = cb_pos->response_buffer.data + cb_pos->information; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 209 | |
| 210 | if (cb_pos->response_buffer.size < |
| 211 | mei_hdr->length + cb_pos->information) { |
| 212 | dev_dbg(&dev->pdev->dev, "message overflow.\n"); |
| 213 | list_del(&cb_pos->cb_list); |
| 214 | return -ENOMEM; |
| 215 | } |
| 216 | if (buffer) |
| 217 | mei_read_slots(dev, buffer, mei_hdr->length); |
| 218 | |
| 219 | cb_pos->information += mei_hdr->length; |
| 220 | if (mei_hdr->msg_complete) { |
| 221 | cl->status = 0; |
| 222 | list_del(&cb_pos->cb_list); |
| 223 | dev_dbg(&dev->pdev->dev, |
| 224 | "completed read host client = %d," |
| 225 | "ME client = %d, " |
| 226 | "data length = %lu\n", |
| 227 | cl->host_client_id, |
| 228 | cl->me_client_id, |
| 229 | cb_pos->information); |
| 230 | |
| 231 | *(cb_pos->response_buffer.data + |
| 232 | cb_pos->information) = '\0'; |
| 233 | dev_dbg(&dev->pdev->dev, "cb_pos->res_buffer - %s\n", |
| 234 | cb_pos->response_buffer.data); |
| 235 | list_add_tail(&cb_pos->cb_list, |
| 236 | &complete_list->mei_cb.cb_list); |
| 237 | } |
| 238 | |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | quit: |
| 245 | dev_dbg(&dev->pdev->dev, "message read\n"); |
| 246 | if (!buffer) { |
Tomas Winkler | edf1eed | 2012-02-09 19:25:54 +0200 | [diff] [blame] | 247 | mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 248 | dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n", |
| 249 | *(u32 *) dev->rd_msg_buf); |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * _mei_irq_thread_iamthif_read - prepares to read iamthif data. |
| 257 | * |
| 258 | * @dev: the device structure. |
| 259 | * @slots: free slots. |
| 260 | * |
| 261 | * returns 0, OK; otherwise, error. |
| 262 | */ |
| 263 | static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots) |
| 264 | { |
| 265 | |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 266 | if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 267 | + sizeof(struct hbm_flow_control))) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 268 | return -EMSGSIZE; |
| 269 | } |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 270 | *slots -= mei_data2slots(sizeof(struct hbm_flow_control)); |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 271 | if (mei_send_flow_control(dev, &dev->iamthif_cl)) { |
| 272 | dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n"); |
| 273 | return -EIO; |
| 274 | } |
| 275 | |
| 276 | dev_dbg(&dev->pdev->dev, "iamthif flow control success\n"); |
| 277 | dev->iamthif_state = MEI_IAMTHIF_READING; |
| 278 | dev->iamthif_flow_control_pending = false; |
| 279 | dev->iamthif_msg_buf_index = 0; |
| 280 | dev->iamthif_msg_buf_size = 0; |
| 281 | dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER; |
Tomas Winkler | 726917f | 2012-06-25 23:46:28 +0300 | [diff] [blame] | 282 | dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev); |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 283 | return 0; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * _mei_irq_thread_close - processes close related operation. |
| 288 | * |
| 289 | * @dev: the device structure. |
| 290 | * @slots: free slots. |
| 291 | * @cb_pos: callback block. |
| 292 | * @cl: private data of the file object. |
| 293 | * @cmpl_list: complete list. |
| 294 | * |
| 295 | * returns 0, OK; otherwise, error. |
| 296 | */ |
| 297 | static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots, |
| 298 | struct mei_cl_cb *cb_pos, |
| 299 | struct mei_cl *cl, |
| 300 | struct mei_io_list *cmpl_list) |
| 301 | { |
Tomas Winkler | b45f3cc | 2012-07-04 19:24:53 +0300 | [diff] [blame^] | 302 | if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) + |
| 303 | sizeof(struct hbm_client_disconnect_request))) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 304 | return -EBADMSG; |
Tomas Winkler | b45f3cc | 2012-07-04 19:24:53 +0300 | [diff] [blame^] | 305 | |
| 306 | *slots -= mei_data2slots(sizeof(struct hbm_client_disconnect_request)); |
| 307 | |
| 308 | if (mei_disconnect(dev, cl)) { |
| 309 | cl->status = 0; |
| 310 | cb_pos->information = 0; |
| 311 | list_move_tail(&cb_pos->cb_list, |
| 312 | &cmpl_list->mei_cb.cb_list); |
| 313 | return -EMSGSIZE; |
| 314 | } else { |
| 315 | cl->state = MEI_FILE_DISCONNECTING; |
| 316 | cl->status = 0; |
| 317 | cb_pos->information = 0; |
| 318 | list_move_tail(&cb_pos->cb_list, |
| 319 | &dev->ctrl_rd_list.mei_cb.cb_list); |
| 320 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * is_treat_specially_client - checks if the message belongs |
| 328 | * to the file private data. |
| 329 | * |
| 330 | * @cl: private data of the file object |
| 331 | * @rs: connect response bus message |
| 332 | * |
| 333 | */ |
| 334 | static bool is_treat_specially_client(struct mei_cl *cl, |
| 335 | struct hbm_client_connect_response *rs) |
| 336 | { |
| 337 | |
| 338 | if (cl->host_client_id == rs->host_addr && |
| 339 | cl->me_client_id == rs->me_addr) { |
| 340 | if (!rs->status) { |
| 341 | cl->state = MEI_FILE_CONNECTED; |
| 342 | cl->status = 0; |
| 343 | |
| 344 | } else { |
| 345 | cl->state = MEI_FILE_DISCONNECTED; |
| 346 | cl->status = -ENODEV; |
| 347 | } |
| 348 | cl->timer_count = 0; |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * mei_client_connect_response - connects to response irq routine |
| 357 | * |
| 358 | * @dev: the device structure |
| 359 | * @rs: connect response bus message |
| 360 | */ |
| 361 | static void mei_client_connect_response(struct mei_device *dev, |
| 362 | struct hbm_client_connect_response *rs) |
| 363 | { |
| 364 | |
| 365 | struct mei_cl *cl; |
| 366 | struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL; |
| 367 | |
| 368 | dev_dbg(&dev->pdev->dev, |
| 369 | "connect_response:\n" |
| 370 | "ME Client = %d\n" |
| 371 | "Host Client = %d\n" |
| 372 | "Status = %d\n", |
| 373 | rs->me_addr, |
| 374 | rs->host_addr, |
| 375 | rs->status); |
| 376 | |
| 377 | /* if WD or iamthif client treat specially */ |
| 378 | |
| 379 | if (is_treat_specially_client(&(dev->wd_cl), rs)) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 380 | dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n"); |
Tomas Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 381 | mei_watchdog_register(dev); |
Oren Weil | 9ce178e | 2011-09-07 09:03:09 +0300 | [diff] [blame] | 382 | |
Tomas Winkler | 70cd533 | 2011-12-22 18:50:50 +0200 | [diff] [blame] | 383 | /* next step in the state maching */ |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 384 | mei_host_init_iamthif(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | if (is_treat_specially_client(&(dev->iamthif_cl), rs)) { |
| 389 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 390 | return; |
| 391 | } |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 392 | list_for_each_entry_safe(cb_pos, cb_next, |
| 393 | &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) { |
| 394 | |
| 395 | cl = (struct mei_cl *)cb_pos->file_private; |
| 396 | if (!cl) { |
| 397 | list_del(&cb_pos->cb_list); |
| 398 | return; |
| 399 | } |
| 400 | if (MEI_IOCTL == cb_pos->major_file_operations) { |
| 401 | if (is_treat_specially_client(cl, rs)) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 402 | list_del(&cb_pos->cb_list); |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 403 | cl->status = 0; |
| 404 | cl->timer_count = 0; |
| 405 | break; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * mei_client_disconnect_response - disconnects from response irq routine |
| 413 | * |
| 414 | * @dev: the device structure |
| 415 | * @rs: disconnect response bus message |
| 416 | */ |
| 417 | static void mei_client_disconnect_response(struct mei_device *dev, |
| 418 | struct hbm_client_connect_response *rs) |
| 419 | { |
| 420 | struct mei_cl *cl; |
| 421 | struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL; |
| 422 | |
| 423 | dev_dbg(&dev->pdev->dev, |
| 424 | "disconnect_response:\n" |
| 425 | "ME Client = %d\n" |
| 426 | "Host Client = %d\n" |
| 427 | "Status = %d\n", |
| 428 | rs->me_addr, |
| 429 | rs->host_addr, |
| 430 | rs->status); |
| 431 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 432 | list_for_each_entry_safe(cb_pos, cb_next, |
| 433 | &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) { |
| 434 | cl = (struct mei_cl *)cb_pos->file_private; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 435 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 436 | if (!cl) { |
| 437 | list_del(&cb_pos->cb_list); |
| 438 | return; |
| 439 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 440 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 441 | dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n"); |
| 442 | if (cl->host_client_id == rs->host_addr && |
| 443 | cl->me_client_id == rs->me_addr) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 444 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 445 | list_del(&cb_pos->cb_list); |
| 446 | if (!rs->status) |
| 447 | cl->state = MEI_FILE_DISCONNECTED; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 448 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 449 | cl->status = 0; |
| 450 | cl->timer_count = 0; |
| 451 | break; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * same_flow_addr - tells if they have the same address. |
| 458 | * |
| 459 | * @file: private data of the file object. |
| 460 | * @flow: flow control. |
| 461 | * |
| 462 | * returns !=0, same; 0,not. |
| 463 | */ |
| 464 | static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow) |
| 465 | { |
| 466 | return (cl->host_client_id == flow->host_addr && |
| 467 | cl->me_client_id == flow->me_addr); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * add_single_flow_creds - adds single buffer credentials. |
| 472 | * |
| 473 | * @file: private data ot the file object. |
| 474 | * @flow: flow control. |
| 475 | */ |
| 476 | static void add_single_flow_creds(struct mei_device *dev, |
| 477 | struct hbm_flow_control *flow) |
| 478 | { |
| 479 | struct mei_me_client *client; |
| 480 | int i; |
| 481 | |
Tomas Winkler | cf9673d | 2011-06-06 10:44:33 +0300 | [diff] [blame] | 482 | for (i = 0; i < dev->me_clients_num; i++) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 483 | client = &dev->me_clients[i]; |
| 484 | if (client && flow->me_addr == client->client_id) { |
| 485 | if (client->props.single_recv_buf) { |
| 486 | client->mei_flow_ctrl_creds++; |
| 487 | dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n", |
| 488 | flow->me_addr); |
| 489 | dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n", |
| 490 | client->mei_flow_ctrl_creds); |
| 491 | } else { |
| 492 | BUG(); /* error in flow control */ |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * mei_client_flow_control_response - flow control response irq routine |
| 500 | * |
| 501 | * @dev: the device structure |
| 502 | * @flow_control: flow control response bus message |
| 503 | */ |
| 504 | static void mei_client_flow_control_response(struct mei_device *dev, |
| 505 | struct hbm_flow_control *flow_control) |
| 506 | { |
| 507 | struct mei_cl *cl_pos = NULL; |
| 508 | struct mei_cl *cl_next = NULL; |
| 509 | |
| 510 | if (!flow_control->host_addr) { |
| 511 | /* single receive buffer */ |
| 512 | add_single_flow_creds(dev, flow_control); |
| 513 | } else { |
| 514 | /* normal connection */ |
| 515 | list_for_each_entry_safe(cl_pos, cl_next, |
| 516 | &dev->file_list, link) { |
| 517 | dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n"); |
| 518 | |
| 519 | dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n", |
| 520 | cl_pos->host_client_id, |
| 521 | cl_pos->me_client_id); |
| 522 | dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n", |
| 523 | flow_control->host_addr, |
| 524 | flow_control->me_addr); |
| 525 | if (same_flow_addr(cl_pos, flow_control)) { |
| 526 | dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n", |
| 527 | flow_control->host_addr, |
| 528 | flow_control->me_addr); |
| 529 | cl_pos->mei_flow_ctrl_creds++; |
| 530 | dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n", |
| 531 | cl_pos->mei_flow_ctrl_creds); |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * same_disconn_addr - tells if they have the same address |
| 540 | * |
| 541 | * @file: private data of the file object. |
| 542 | * @disconn: disconnection request. |
| 543 | * |
| 544 | * returns !=0, same; 0,not. |
| 545 | */ |
| 546 | static int same_disconn_addr(struct mei_cl *cl, |
| 547 | struct hbm_client_disconnect_request *disconn) |
| 548 | { |
| 549 | return (cl->host_client_id == disconn->host_addr && |
| 550 | cl->me_client_id == disconn->me_addr); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * mei_client_disconnect_request - disconnects from request irq routine |
| 555 | * |
| 556 | * @dev: the device structure. |
| 557 | * @disconnect_req: disconnect request bus message. |
| 558 | */ |
| 559 | static void mei_client_disconnect_request(struct mei_device *dev, |
| 560 | struct hbm_client_disconnect_request *disconnect_req) |
| 561 | { |
| 562 | struct mei_msg_hdr *mei_hdr; |
| 563 | struct hbm_client_connect_response *disconnect_res; |
| 564 | struct mei_cl *cl_pos = NULL; |
| 565 | struct mei_cl *cl_next = NULL; |
| 566 | |
| 567 | list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) { |
| 568 | if (same_disconn_addr(cl_pos, disconnect_req)) { |
| 569 | dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n", |
| 570 | disconnect_req->host_addr, |
| 571 | disconnect_req->me_addr); |
| 572 | cl_pos->state = MEI_FILE_DISCONNECTED; |
| 573 | cl_pos->timer_count = 0; |
Tomas Winkler | d242a0a | 2012-07-04 19:24:50 +0300 | [diff] [blame] | 574 | if (cl_pos == &dev->wd_cl) |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 575 | dev->wd_pending = false; |
Tomas Winkler | d242a0a | 2012-07-04 19:24:50 +0300 | [diff] [blame] | 576 | else if (cl_pos == &dev->iamthif_cl) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 577 | dev->iamthif_timer = 0; |
| 578 | |
| 579 | /* prepare disconnect response */ |
| 580 | mei_hdr = |
| 581 | (struct mei_msg_hdr *) &dev->ext_msg_buf[0]; |
| 582 | mei_hdr->host_addr = 0; |
| 583 | mei_hdr->me_addr = 0; |
| 584 | mei_hdr->length = |
| 585 | sizeof(struct hbm_client_connect_response); |
| 586 | mei_hdr->msg_complete = 1; |
| 587 | mei_hdr->reserved = 0; |
| 588 | |
| 589 | disconnect_res = |
| 590 | (struct hbm_client_connect_response *) |
| 591 | &dev->ext_msg_buf[1]; |
| 592 | disconnect_res->host_addr = cl_pos->host_client_id; |
| 593 | disconnect_res->me_addr = cl_pos->me_client_id; |
Tomas Winkler | 1ca7e78 | 2012-02-26 23:18:57 +0200 | [diff] [blame] | 594 | disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 595 | disconnect_res->status = 0; |
| 596 | dev->extra_write_index = 2; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | |
| 603 | /** |
| 604 | * mei_irq_thread_read_bus_message - bottom half read routine after ISR to |
| 605 | * handle the read bus message cmd processing. |
| 606 | * |
| 607 | * @dev: the device structure |
| 608 | * @mei_hdr: header of bus message |
| 609 | */ |
| 610 | static void mei_irq_thread_read_bus_message(struct mei_device *dev, |
| 611 | struct mei_msg_hdr *mei_hdr) |
| 612 | { |
| 613 | struct mei_bus_message *mei_msg; |
| 614 | struct hbm_host_version_response *version_res; |
| 615 | struct hbm_client_connect_response *connect_res; |
| 616 | struct hbm_client_connect_response *disconnect_res; |
| 617 | struct hbm_flow_control *flow_control; |
| 618 | struct hbm_props_response *props_res; |
| 619 | struct hbm_host_enum_response *enum_res; |
| 620 | struct hbm_client_disconnect_request *disconnect_req; |
| 621 | struct hbm_host_stop_request *host_stop_req; |
Oren Weil | abc51b6 | 2011-09-21 16:45:30 +0300 | [diff] [blame] | 622 | int res; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 623 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 624 | |
| 625 | /* read the message to our buffer */ |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 626 | BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf)); |
Tomas Winkler | edf1eed | 2012-02-09 19:25:54 +0200 | [diff] [blame] | 627 | mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length); |
| 628 | mei_msg = (struct mei_bus_message *)dev->rd_msg_buf; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 629 | |
Tomas Winkler | 1ca7e78 | 2012-02-26 23:18:57 +0200 | [diff] [blame] | 630 | switch (mei_msg->hbm_cmd) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 631 | case HOST_START_RES_CMD: |
| 632 | version_res = (struct hbm_host_version_response *) mei_msg; |
| 633 | if (version_res->host_version_supported) { |
| 634 | dev->version.major_version = HBM_MAJOR_VERSION; |
| 635 | dev->version.minor_version = HBM_MINOR_VERSION; |
| 636 | if (dev->mei_state == MEI_INIT_CLIENTS && |
| 637 | dev->init_clients_state == MEI_START_MESSAGE) { |
| 638 | dev->init_clients_timer = 0; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 639 | mei_host_enum_clients_message(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 640 | } else { |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 641 | dev->recvd_msg = false; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 642 | dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n"); |
| 643 | mei_reset(dev, 1); |
| 644 | return; |
| 645 | } |
| 646 | } else { |
| 647 | dev->version = version_res->me_max_version; |
| 648 | /* send stop message */ |
Tomas Winkler | 97d5cb0 | 2012-03-06 22:34:32 +0200 | [diff] [blame] | 649 | mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0]; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 650 | mei_hdr->host_addr = 0; |
| 651 | mei_hdr->me_addr = 0; |
| 652 | mei_hdr->length = sizeof(struct hbm_host_stop_request); |
| 653 | mei_hdr->msg_complete = 1; |
| 654 | mei_hdr->reserved = 0; |
| 655 | |
| 656 | host_stop_req = (struct hbm_host_stop_request *) |
| 657 | &dev->wr_msg_buf[1]; |
| 658 | |
| 659 | memset(host_stop_req, |
| 660 | 0, |
| 661 | sizeof(struct hbm_host_stop_request)); |
Tomas Winkler | 1ca7e78 | 2012-02-26 23:18:57 +0200 | [diff] [blame] | 662 | host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 663 | host_stop_req->reason = DRIVER_STOP_REQUEST; |
| 664 | mei_write_message(dev, mei_hdr, |
| 665 | (unsigned char *) (host_stop_req), |
| 666 | mei_hdr->length); |
| 667 | dev_dbg(&dev->pdev->dev, "version mismatch.\n"); |
| 668 | return; |
| 669 | } |
| 670 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 671 | dev->recvd_msg = true; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 672 | dev_dbg(&dev->pdev->dev, "host start response message received.\n"); |
| 673 | break; |
| 674 | |
| 675 | case CLIENT_CONNECT_RES_CMD: |
| 676 | connect_res = |
| 677 | (struct hbm_client_connect_response *) mei_msg; |
| 678 | mei_client_connect_response(dev, connect_res); |
| 679 | dev_dbg(&dev->pdev->dev, "client connect response message received.\n"); |
| 680 | wake_up(&dev->wait_recvd_msg); |
| 681 | break; |
| 682 | |
| 683 | case CLIENT_DISCONNECT_RES_CMD: |
| 684 | disconnect_res = |
| 685 | (struct hbm_client_connect_response *) mei_msg; |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 686 | mei_client_disconnect_response(dev, disconnect_res); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 687 | dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n"); |
| 688 | wake_up(&dev->wait_recvd_msg); |
| 689 | break; |
| 690 | |
| 691 | case MEI_FLOW_CONTROL_CMD: |
| 692 | flow_control = (struct hbm_flow_control *) mei_msg; |
| 693 | mei_client_flow_control_response(dev, flow_control); |
| 694 | dev_dbg(&dev->pdev->dev, "client flow control response message received.\n"); |
| 695 | break; |
| 696 | |
| 697 | case HOST_CLIENT_PROPERTIES_RES_CMD: |
| 698 | props_res = (struct hbm_props_response *)mei_msg; |
| 699 | if (props_res->status || !dev->me_clients) { |
| 700 | dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n"); |
| 701 | mei_reset(dev, 1); |
| 702 | return; |
| 703 | } |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 704 | if (dev->me_clients[dev->me_client_presentation_num] |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 705 | .client_id == props_res->address) { |
| 706 | |
| 707 | dev->me_clients[dev->me_client_presentation_num].props |
| 708 | = props_res->client_properties; |
| 709 | |
| 710 | if (dev->mei_state == MEI_INIT_CLIENTS && |
| 711 | dev->init_clients_state == |
| 712 | MEI_CLIENT_PROPERTIES_MESSAGE) { |
| 713 | dev->me_client_index++; |
| 714 | dev->me_client_presentation_num++; |
Oren Weil | abc51b6 | 2011-09-21 16:45:30 +0300 | [diff] [blame] | 715 | |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 716 | /** Send Client Properties request **/ |
Oren Weil | abc51b6 | 2011-09-21 16:45:30 +0300 | [diff] [blame] | 717 | res = mei_host_client_properties(dev); |
| 718 | if (res < 0) { |
| 719 | dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed"); |
| 720 | return; |
| 721 | } else if (!res) { |
| 722 | /* |
| 723 | * No more clients to send to. |
| 724 | * Clear Map for indicating now ME clients |
| 725 | * with associated host client |
| 726 | */ |
| 727 | bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX); |
| 728 | dev->open_handle_count = 0; |
| 729 | |
| 730 | /* |
| 731 | * Reserving the first three client IDs |
| 732 | * Client Id 0 - Reserved for MEI Bus Message communications |
| 733 | * Client Id 1 - Reserved for Watchdog |
| 734 | * Client ID 2 - Reserved for AMTHI |
| 735 | */ |
| 736 | bitmap_set(dev->host_clients_map, 0, 3); |
| 737 | dev->mei_state = MEI_ENABLED; |
| 738 | |
| 739 | /* if wd initialization fails, initialization the AMTHI client, |
| 740 | * otherwise the AMTHI client will be initialized after the WD client connect response |
| 741 | * will be received |
| 742 | */ |
| 743 | if (mei_wd_host_init(dev)) |
| 744 | mei_host_init_iamthif(dev); |
| 745 | } |
| 746 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 747 | } else { |
| 748 | dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message"); |
| 749 | mei_reset(dev, 1); |
| 750 | return; |
| 751 | } |
| 752 | } else { |
| 753 | dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n"); |
| 754 | mei_reset(dev, 1); |
| 755 | return; |
| 756 | } |
| 757 | break; |
| 758 | |
| 759 | case HOST_ENUM_RES_CMD: |
| 760 | enum_res = (struct hbm_host_enum_response *) mei_msg; |
| 761 | memcpy(dev->me_clients_map, enum_res->valid_addresses, 32); |
| 762 | if (dev->mei_state == MEI_INIT_CLIENTS && |
| 763 | dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) { |
| 764 | dev->init_clients_timer = 0; |
| 765 | dev->me_client_presentation_num = 0; |
| 766 | dev->me_client_index = 0; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 767 | mei_allocate_me_clients_storage(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 768 | dev->init_clients_state = |
| 769 | MEI_CLIENT_PROPERTIES_MESSAGE; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 770 | mei_host_client_properties(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 771 | } else { |
| 772 | dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n"); |
| 773 | mei_reset(dev, 1); |
| 774 | return; |
| 775 | } |
| 776 | break; |
| 777 | |
| 778 | case HOST_STOP_RES_CMD: |
| 779 | dev->mei_state = MEI_DISABLED; |
| 780 | dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n"); |
| 781 | mei_reset(dev, 1); |
| 782 | break; |
| 783 | |
| 784 | case CLIENT_DISCONNECT_REQ_CMD: |
| 785 | /* search for client */ |
| 786 | disconnect_req = |
| 787 | (struct hbm_client_disconnect_request *) mei_msg; |
| 788 | mei_client_disconnect_request(dev, disconnect_req); |
| 789 | break; |
| 790 | |
| 791 | case ME_STOP_REQ_CMD: |
| 792 | /* prepare stop request */ |
| 793 | mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0]; |
| 794 | mei_hdr->host_addr = 0; |
| 795 | mei_hdr->me_addr = 0; |
| 796 | mei_hdr->length = sizeof(struct hbm_host_stop_request); |
| 797 | mei_hdr->msg_complete = 1; |
| 798 | mei_hdr->reserved = 0; |
| 799 | host_stop_req = |
| 800 | (struct hbm_host_stop_request *) &dev->ext_msg_buf[1]; |
| 801 | memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request)); |
Tomas Winkler | 1ca7e78 | 2012-02-26 23:18:57 +0200 | [diff] [blame] | 802 | host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 803 | host_stop_req->reason = DRIVER_STOP_REQUEST; |
| 804 | host_stop_req->reserved[0] = 0; |
| 805 | host_stop_req->reserved[1] = 0; |
| 806 | dev->extra_write_index = 2; |
| 807 | break; |
| 808 | |
| 809 | default: |
| 810 | BUG(); |
| 811 | break; |
| 812 | |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | |
| 817 | /** |
| 818 | * _mei_hb_read - processes read related operation. |
| 819 | * |
| 820 | * @dev: the device structure. |
| 821 | * @slots: free slots. |
| 822 | * @cb_pos: callback block. |
| 823 | * @cl: private data of the file object. |
| 824 | * @cmpl_list: complete list. |
| 825 | * |
| 826 | * returns 0, OK; otherwise, error. |
| 827 | */ |
| 828 | static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots, |
| 829 | struct mei_cl_cb *cb_pos, |
| 830 | struct mei_cl *cl, |
| 831 | struct mei_io_list *cmpl_list) |
| 832 | { |
| 833 | if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) + |
| 834 | sizeof(struct hbm_flow_control))) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 835 | /* return the cancel routine */ |
| 836 | list_del(&cb_pos->cb_list); |
| 837 | return -EBADMSG; |
| 838 | } |
| 839 | |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 840 | *slots -= mei_data2slots(sizeof(struct hbm_flow_control)); |
| 841 | |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 842 | if (mei_send_flow_control(dev, cl)) { |
| 843 | cl->status = -ENODEV; |
| 844 | cb_pos->information = 0; |
| 845 | list_move_tail(&cb_pos->cb_list, &cmpl_list->mei_cb.cb_list); |
| 846 | return -ENODEV; |
| 847 | } |
| 848 | list_move_tail(&cb_pos->cb_list, &dev->read_list.mei_cb.cb_list); |
| 849 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /** |
| 855 | * _mei_irq_thread_ioctl - processes ioctl related operation. |
| 856 | * |
| 857 | * @dev: the device structure. |
| 858 | * @slots: free slots. |
| 859 | * @cb_pos: callback block. |
| 860 | * @cl: private data of the file object. |
| 861 | * @cmpl_list: complete list. |
| 862 | * |
| 863 | * returns 0, OK; otherwise, error. |
| 864 | */ |
| 865 | static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots, |
| 866 | struct mei_cl_cb *cb_pos, |
| 867 | struct mei_cl *cl, |
| 868 | struct mei_io_list *cmpl_list) |
| 869 | { |
Tomas Winkler | b45f3cc | 2012-07-04 19:24:53 +0300 | [diff] [blame^] | 870 | if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) + |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 871 | sizeof(struct hbm_client_connect_request))) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 872 | /* return the cancel routine */ |
| 873 | list_del(&cb_pos->cb_list); |
| 874 | return -EBADMSG; |
| 875 | } |
| 876 | |
Tomas Winkler | b45f3cc | 2012-07-04 19:24:53 +0300 | [diff] [blame^] | 877 | cl->state = MEI_FILE_CONNECTING; |
| 878 | *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request)); |
| 879 | if (mei_connect(dev, cl)) { |
| 880 | cl->status = -ENODEV; |
| 881 | cb_pos->information = 0; |
| 882 | list_del(&cb_pos->cb_list); |
| 883 | return -ENODEV; |
| 884 | } else { |
| 885 | list_move_tail(&cb_pos->cb_list, |
| 886 | &dev->ctrl_rd_list.mei_cb.cb_list); |
| 887 | cl->timer_count = MEI_CONNECT_TIMEOUT; |
| 888 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * _mei_irq_thread_cmpl - processes completed and no-iamthif operation. |
| 894 | * |
| 895 | * @dev: the device structure. |
| 896 | * @slots: free slots. |
| 897 | * @cb_pos: callback block. |
| 898 | * @cl: private data of the file object. |
| 899 | * @cmpl_list: complete list. |
| 900 | * |
| 901 | * returns 0, OK; otherwise, error. |
| 902 | */ |
| 903 | static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots, |
| 904 | struct mei_cl_cb *cb_pos, |
| 905 | struct mei_cl *cl, |
| 906 | struct mei_io_list *cmpl_list) |
| 907 | { |
| 908 | struct mei_msg_hdr *mei_hdr; |
| 909 | |
| 910 | if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) + |
| 911 | (cb_pos->request_buffer.size - |
| 912 | cb_pos->information))) { |
| 913 | mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0]; |
| 914 | mei_hdr->host_addr = cl->host_client_id; |
| 915 | mei_hdr->me_addr = cl->me_client_id; |
| 916 | mei_hdr->length = cb_pos->request_buffer.size - |
| 917 | cb_pos->information; |
| 918 | mei_hdr->msg_complete = 1; |
| 919 | mei_hdr->reserved = 0; |
| 920 | dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d" |
| 921 | "mei_hdr->msg_complete = %d\n", |
| 922 | cb_pos->request_buffer.size, |
| 923 | mei_hdr->msg_complete); |
| 924 | dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n", |
| 925 | cb_pos->information); |
| 926 | dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", |
| 927 | mei_hdr->length); |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 928 | *slots -= mei_data2slots(mei_hdr->length); |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 929 | if (mei_write_message(dev, mei_hdr, |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 930 | (unsigned char *) |
| 931 | (cb_pos->request_buffer.data + |
| 932 | cb_pos->information), |
| 933 | mei_hdr->length)) { |
| 934 | cl->status = -ENODEV; |
| 935 | list_move_tail(&cb_pos->cb_list, |
| 936 | &cmpl_list->mei_cb.cb_list); |
| 937 | return -ENODEV; |
| 938 | } else { |
| 939 | if (mei_flow_ctrl_reduce(dev, cl)) |
| 940 | return -ENODEV; |
| 941 | cl->status = 0; |
| 942 | cb_pos->information += mei_hdr->length; |
| 943 | list_move_tail(&cb_pos->cb_list, |
| 944 | &dev->write_waiting_list.mei_cb.cb_list); |
| 945 | } |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 946 | } else if (*slots == dev->hbuf_depth) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 947 | /* buffer is still empty */ |
| 948 | mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0]; |
| 949 | mei_hdr->host_addr = cl->host_client_id; |
| 950 | mei_hdr->me_addr = cl->me_client_id; |
| 951 | mei_hdr->length = |
| 952 | (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); |
| 953 | mei_hdr->msg_complete = 0; |
| 954 | mei_hdr->reserved = 0; |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 955 | *slots -= mei_data2slots(mei_hdr->length); |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 956 | if (mei_write_message(dev, mei_hdr, |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 957 | (unsigned char *) |
| 958 | (cb_pos->request_buffer.data + |
| 959 | cb_pos->information), |
| 960 | mei_hdr->length)) { |
| 961 | cl->status = -ENODEV; |
| 962 | list_move_tail(&cb_pos->cb_list, |
| 963 | &cmpl_list->mei_cb.cb_list); |
| 964 | return -ENODEV; |
| 965 | } else { |
| 966 | cb_pos->information += mei_hdr->length; |
| 967 | dev_dbg(&dev->pdev->dev, |
| 968 | "cb_pos->request_buffer.size =%d" |
| 969 | " mei_hdr->msg_complete = %d\n", |
| 970 | cb_pos->request_buffer.size, |
| 971 | mei_hdr->msg_complete); |
| 972 | dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n", |
| 973 | cb_pos->information); |
| 974 | dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", |
| 975 | mei_hdr->length); |
| 976 | } |
| 977 | return -EMSGSIZE; |
| 978 | } else { |
| 979 | return -EBADMSG; |
| 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation. |
| 987 | * |
| 988 | * @dev: the device structure. |
| 989 | * @slots: free slots. |
| 990 | * @cb_pos: callback block. |
| 991 | * @cl: private data of the file object. |
| 992 | * @cmpl_list: complete list. |
| 993 | * |
| 994 | * returns 0, OK; otherwise, error. |
| 995 | */ |
| 996 | static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots, |
| 997 | struct mei_cl_cb *cb_pos, |
| 998 | struct mei_cl *cl, |
| 999 | struct mei_io_list *cmpl_list) |
| 1000 | { |
| 1001 | struct mei_msg_hdr *mei_hdr; |
| 1002 | |
| 1003 | if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) + |
| 1004 | dev->iamthif_msg_buf_size - |
| 1005 | dev->iamthif_msg_buf_index)) { |
| 1006 | mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0]; |
| 1007 | mei_hdr->host_addr = cl->host_client_id; |
| 1008 | mei_hdr->me_addr = cl->me_client_id; |
| 1009 | mei_hdr->length = dev->iamthif_msg_buf_size - |
| 1010 | dev->iamthif_msg_buf_index; |
| 1011 | mei_hdr->msg_complete = 1; |
| 1012 | mei_hdr->reserved = 0; |
| 1013 | |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 1014 | *slots -= mei_data2slots(mei_hdr->length); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1015 | |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 1016 | if (mei_write_message(dev, mei_hdr, |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1017 | (dev->iamthif_msg_buf + |
| 1018 | dev->iamthif_msg_buf_index), |
| 1019 | mei_hdr->length)) { |
| 1020 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 1021 | cl->status = -ENODEV; |
| 1022 | list_del(&cb_pos->cb_list); |
| 1023 | return -ENODEV; |
| 1024 | } else { |
| 1025 | if (mei_flow_ctrl_reduce(dev, cl)) |
| 1026 | return -ENODEV; |
| 1027 | dev->iamthif_msg_buf_index += mei_hdr->length; |
| 1028 | cb_pos->information = dev->iamthif_msg_buf_index; |
| 1029 | cl->status = 0; |
| 1030 | dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 1031 | dev->iamthif_flow_control_pending = true; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1032 | /* save iamthif cb sent to amthi client */ |
| 1033 | dev->iamthif_current_cb = cb_pos; |
| 1034 | list_move_tail(&cb_pos->cb_list, |
| 1035 | &dev->write_waiting_list.mei_cb.cb_list); |
| 1036 | |
| 1037 | } |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 1038 | } else if (*slots == dev->hbuf_depth) { |
| 1039 | /* buffer is still empty */ |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1040 | mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0]; |
| 1041 | mei_hdr->host_addr = cl->host_client_id; |
| 1042 | mei_hdr->me_addr = cl->me_client_id; |
| 1043 | mei_hdr->length = |
| 1044 | (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); |
| 1045 | mei_hdr->msg_complete = 0; |
| 1046 | mei_hdr->reserved = 0; |
| 1047 | |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 1048 | *slots -= mei_data2slots(mei_hdr->length); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1049 | |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 1050 | if (mei_write_message(dev, mei_hdr, |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1051 | (dev->iamthif_msg_buf + |
| 1052 | dev->iamthif_msg_buf_index), |
| 1053 | mei_hdr->length)) { |
| 1054 | cl->status = -ENODEV; |
| 1055 | list_del(&cb_pos->cb_list); |
| 1056 | } else { |
| 1057 | dev->iamthif_msg_buf_index += mei_hdr->length; |
| 1058 | } |
| 1059 | return -EMSGSIZE; |
| 1060 | } else { |
| 1061 | return -EBADMSG; |
| 1062 | } |
| 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * mei_irq_thread_read_handler - bottom half read routine after ISR to |
| 1069 | * handle the read processing. |
| 1070 | * |
| 1071 | * @cmpl_list: An instance of our list structure |
| 1072 | * @dev: the device structure |
| 1073 | * @slots: slots to read. |
| 1074 | * |
| 1075 | * returns 0 on success, <0 on failure. |
| 1076 | */ |
| 1077 | static int mei_irq_thread_read_handler(struct mei_io_list *cmpl_list, |
| 1078 | struct mei_device *dev, |
| 1079 | s32 *slots) |
| 1080 | { |
| 1081 | struct mei_msg_hdr *mei_hdr; |
| 1082 | struct mei_cl *cl_pos = NULL; |
| 1083 | struct mei_cl *cl_next = NULL; |
| 1084 | int ret = 0; |
| 1085 | |
| 1086 | if (!dev->rd_msg_hdr) { |
| 1087 | dev->rd_msg_hdr = mei_mecbrw_read(dev); |
| 1088 | dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots); |
| 1089 | (*slots)--; |
| 1090 | dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots); |
| 1091 | } |
| 1092 | mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr; |
| 1093 | dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length); |
| 1094 | |
| 1095 | if (mei_hdr->reserved || !dev->rd_msg_hdr) { |
| 1096 | dev_dbg(&dev->pdev->dev, "corrupted message header.\n"); |
| 1097 | ret = -EBADMSG; |
| 1098 | goto end; |
| 1099 | } |
| 1100 | |
| 1101 | if (mei_hdr->host_addr || mei_hdr->me_addr) { |
| 1102 | list_for_each_entry_safe(cl_pos, cl_next, |
| 1103 | &dev->file_list, link) { |
| 1104 | dev_dbg(&dev->pdev->dev, |
| 1105 | "list_for_each_entry_safe read host" |
| 1106 | " client = %d, ME client = %d\n", |
| 1107 | cl_pos->host_client_id, |
| 1108 | cl_pos->me_client_id); |
| 1109 | if (cl_pos->host_client_id == mei_hdr->host_addr && |
| 1110 | cl_pos->me_client_id == mei_hdr->me_addr) |
| 1111 | break; |
| 1112 | } |
| 1113 | |
| 1114 | if (&cl_pos->link == &dev->file_list) { |
| 1115 | dev_dbg(&dev->pdev->dev, "corrupted message header\n"); |
| 1116 | ret = -EBADMSG; |
| 1117 | goto end; |
| 1118 | } |
| 1119 | } |
| 1120 | if (((*slots) * sizeof(u32)) < mei_hdr->length) { |
| 1121 | dev_dbg(&dev->pdev->dev, |
| 1122 | "we can't read the message slots =%08x.\n", |
| 1123 | *slots); |
| 1124 | /* we can't read the message */ |
| 1125 | ret = -ERANGE; |
| 1126 | goto end; |
| 1127 | } |
| 1128 | |
| 1129 | /* decide where to read the message too */ |
| 1130 | if (!mei_hdr->host_addr) { |
| 1131 | dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n"); |
| 1132 | mei_irq_thread_read_bus_message(dev, mei_hdr); |
| 1133 | dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n"); |
| 1134 | } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id && |
| 1135 | (MEI_FILE_CONNECTED == dev->iamthif_cl.state) && |
| 1136 | (dev->iamthif_state == MEI_IAMTHIF_READING)) { |
| 1137 | dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n"); |
| 1138 | dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", |
| 1139 | mei_hdr->length); |
| 1140 | ret = mei_irq_thread_read_amthi_message(cmpl_list, |
| 1141 | dev, mei_hdr); |
| 1142 | if (ret) |
| 1143 | goto end; |
| 1144 | |
| 1145 | } else { |
| 1146 | dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n"); |
| 1147 | ret = mei_irq_thread_read_client_message(cmpl_list, |
| 1148 | dev, mei_hdr); |
| 1149 | if (ret) |
| 1150 | goto end; |
| 1151 | |
| 1152 | } |
| 1153 | |
| 1154 | /* reset the number of slots and header */ |
| 1155 | *slots = mei_count_full_read_slots(dev); |
| 1156 | dev->rd_msg_hdr = 0; |
| 1157 | |
| 1158 | if (*slots == -EOVERFLOW) { |
| 1159 | /* overflow - reset */ |
| 1160 | dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n"); |
| 1161 | /* set the event since message has been read */ |
| 1162 | ret = -ERANGE; |
| 1163 | goto end; |
| 1164 | } |
| 1165 | end: |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /** |
| 1171 | * mei_irq_thread_write_handler - bottom half write routine after |
| 1172 | * ISR to handle the write processing. |
| 1173 | * |
| 1174 | * @cmpl_list: An instance of our list structure |
| 1175 | * @dev: the device structure |
| 1176 | * @slots: slots to write. |
| 1177 | * |
| 1178 | * returns 0 on success, <0 on failure. |
| 1179 | */ |
| 1180 | static int mei_irq_thread_write_handler(struct mei_io_list *cmpl_list, |
| 1181 | struct mei_device *dev, |
| 1182 | s32 *slots) |
| 1183 | { |
| 1184 | |
| 1185 | struct mei_cl *cl; |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1186 | struct mei_cl_cb *pos = NULL, *next = NULL; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1187 | struct mei_io_list *list; |
| 1188 | int ret; |
| 1189 | |
Tomas Winkler | 726917f | 2012-06-25 23:46:28 +0300 | [diff] [blame] | 1190 | if (!mei_hbuf_is_empty(dev)) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1191 | dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n"); |
| 1192 | return 0; |
| 1193 | } |
Tomas Winkler | 726917f | 2012-06-25 23:46:28 +0300 | [diff] [blame] | 1194 | *slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | 7d5e0e5 | 2012-06-19 09:13:36 +0300 | [diff] [blame] | 1195 | if (*slots <= 0) |
| 1196 | return -EMSGSIZE; |
| 1197 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1198 | /* complete all waiting for write CB */ |
| 1199 | dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n"); |
| 1200 | |
| 1201 | list = &dev->write_waiting_list; |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1202 | list_for_each_entry_safe(pos, next, |
| 1203 | &list->mei_cb.cb_list, cb_list) { |
| 1204 | cl = (struct mei_cl *)pos->file_private; |
| 1205 | if (cl == NULL) |
| 1206 | continue; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1207 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1208 | cl->status = 0; |
| 1209 | list_del(&pos->cb_list); |
| 1210 | if (MEI_WRITING == cl->writing_state && |
| 1211 | (pos->major_file_operations == MEI_WRITE) && |
| 1212 | (cl != &dev->iamthif_cl)) { |
| 1213 | dev_dbg(&dev->pdev->dev, |
| 1214 | "MEI WRITE COMPLETE\n"); |
| 1215 | cl->writing_state = MEI_WRITE_COMPLETE; |
| 1216 | list_add_tail(&pos->cb_list, |
| 1217 | &cmpl_list->mei_cb.cb_list); |
| 1218 | } |
| 1219 | if (cl == &dev->iamthif_cl) { |
| 1220 | dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n"); |
| 1221 | if (dev->iamthif_flow_control_pending) { |
| 1222 | ret = _mei_irq_thread_iamthif_read( |
| 1223 | dev, slots); |
| 1224 | if (ret) |
| 1225 | return ret; |
| 1226 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | if (dev->stop && !dev->wd_pending) { |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 1231 | dev->wd_stopped = true; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1232 | wake_up_interruptible(&dev->wait_stop_wd); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | if (dev->extra_write_index) { |
| 1237 | dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n", |
| 1238 | dev->extra_write_index); |
| 1239 | mei_write_message(dev, |
| 1240 | (struct mei_msg_hdr *) &dev->ext_msg_buf[0], |
| 1241 | (unsigned char *) &dev->ext_msg_buf[1], |
| 1242 | (dev->extra_write_index - 1) * sizeof(u32)); |
| 1243 | *slots -= dev->extra_write_index; |
| 1244 | dev->extra_write_index = 0; |
| 1245 | } |
| 1246 | if (dev->mei_state == MEI_ENABLED) { |
| 1247 | if (dev->wd_pending && |
| 1248 | mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) { |
| 1249 | if (mei_wd_send(dev)) |
| 1250 | dev_dbg(&dev->pdev->dev, "wd send failed.\n"); |
| 1251 | else |
| 1252 | if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) |
| 1253 | return -ENODEV; |
| 1254 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 1255 | dev->wd_pending = false; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1256 | |
Tomas Winkler | d242a0a | 2012-07-04 19:24:50 +0300 | [diff] [blame] | 1257 | if (dev->wd_timeout) |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 1258 | *slots -= mei_data2slots(MEI_START_WD_DATA_SIZE); |
Tomas Winkler | d242a0a | 2012-07-04 19:24:50 +0300 | [diff] [blame] | 1259 | else |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 1260 | *slots -= mei_data2slots(MEI_START_WD_DATA_SIZE); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | if (dev->stop) |
Devin J. Pohly | dc91e2f | 2012-02-27 20:32:53 +0200 | [diff] [blame] | 1264 | return -ENODEV; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1265 | |
| 1266 | /* complete control write list CB */ |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1267 | dev_dbg(&dev->pdev->dev, "complete control write list cb.\n"); |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1268 | list_for_each_entry_safe(pos, next, |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1269 | &dev->ctrl_wr_list.mei_cb.cb_list, cb_list) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1270 | cl = (struct mei_cl *) pos->file_private; |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1271 | if (!cl) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1272 | list_del(&pos->cb_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1273 | return -ENODEV; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1274 | } |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1275 | switch (pos->major_file_operations) { |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1276 | case MEI_CLOSE: |
| 1277 | /* send disconnect message */ |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1278 | ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1279 | if (ret) |
| 1280 | return ret; |
| 1281 | |
| 1282 | break; |
| 1283 | case MEI_READ: |
| 1284 | /* send flow control message */ |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1285 | ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1286 | if (ret) |
| 1287 | return ret; |
| 1288 | |
| 1289 | break; |
| 1290 | case MEI_IOCTL: |
| 1291 | /* connect message */ |
Natalia Ovsyanikov | e8cd29d | 2011-12-05 00:16:54 +0200 | [diff] [blame] | 1292 | if (mei_other_client_is_connecting(dev, cl)) |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1293 | continue; |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1294 | ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1295 | if (ret) |
| 1296 | return ret; |
| 1297 | |
| 1298 | break; |
| 1299 | |
| 1300 | default: |
| 1301 | BUG(); |
| 1302 | } |
| 1303 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1304 | } |
| 1305 | /* complete write list CB */ |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1306 | dev_dbg(&dev->pdev->dev, "complete write list cb.\n"); |
| 1307 | list_for_each_entry_safe(pos, next, |
| 1308 | &dev->write_list.mei_cb.cb_list, cb_list) { |
| 1309 | cl = (struct mei_cl *)pos->file_private; |
| 1310 | if (cl == NULL) |
| 1311 | continue; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1312 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1313 | if (cl != &dev->iamthif_cl) { |
Tomas Winkler | d204115 | 2012-06-19 09:13:34 +0300 | [diff] [blame] | 1314 | if (mei_flow_ctrl_creds(dev, cl) <= 0) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1315 | dev_dbg(&dev->pdev->dev, |
| 1316 | "No flow control" |
| 1317 | " credentials for client" |
| 1318 | " %d, not sending.\n", |
| 1319 | cl->host_client_id); |
| 1320 | continue; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1321 | } |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1322 | ret = _mei_irq_thread_cmpl(dev, slots, |
| 1323 | pos, |
| 1324 | cl, cmpl_list); |
| 1325 | if (ret) |
| 1326 | return ret; |
| 1327 | |
| 1328 | } else if (cl == &dev->iamthif_cl) { |
| 1329 | /* IAMTHIF IOCTL */ |
| 1330 | dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n"); |
Tomas Winkler | d204115 | 2012-06-19 09:13:34 +0300 | [diff] [blame] | 1331 | if (mei_flow_ctrl_creds(dev, cl) <= 0) { |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1332 | dev_dbg(&dev->pdev->dev, |
| 1333 | "No flow control" |
| 1334 | " credentials for amthi" |
| 1335 | " client %d.\n", |
| 1336 | cl->host_client_id); |
| 1337 | continue; |
| 1338 | } |
| 1339 | ret = _mei_irq_thread_cmpl_iamthif(dev, |
| 1340 | slots, |
| 1341 | pos, |
| 1342 | cl, |
| 1343 | cmpl_list); |
| 1344 | if (ret) |
| 1345 | return ret; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1346 | |
| 1347 | } |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1348 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1349 | } |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | /** |
| 1356 | * mei_timer - timer function. |
| 1357 | * |
| 1358 | * @work: pointer to the work_struct structure |
| 1359 | * |
| 1360 | * NOTE: This function is called by timer interrupt work |
| 1361 | */ |
Oren Weil | a61c653 | 2011-09-07 09:03:13 +0300 | [diff] [blame] | 1362 | void mei_timer(struct work_struct *work) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1363 | { |
| 1364 | unsigned long timeout; |
| 1365 | struct mei_cl *cl_pos = NULL; |
| 1366 | struct mei_cl *cl_next = NULL; |
| 1367 | struct list_head *amthi_complete_list = NULL; |
| 1368 | struct mei_cl_cb *cb_pos = NULL; |
| 1369 | struct mei_cl_cb *cb_next = NULL; |
| 1370 | |
| 1371 | struct mei_device *dev = container_of(work, |
Oren Weil | a61c653 | 2011-09-07 09:03:13 +0300 | [diff] [blame] | 1372 | struct mei_device, timer_work.work); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1373 | |
| 1374 | |
| 1375 | mutex_lock(&dev->device_lock); |
| 1376 | if (dev->mei_state != MEI_ENABLED) { |
| 1377 | if (dev->mei_state == MEI_INIT_CLIENTS) { |
| 1378 | if (dev->init_clients_timer) { |
| 1379 | if (--dev->init_clients_timer == 0) { |
| 1380 | dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n", |
| 1381 | dev->init_clients_state); |
| 1382 | mei_reset(dev, 1); |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | goto out; |
| 1387 | } |
| 1388 | /*** connect/disconnect timeouts ***/ |
| 1389 | list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) { |
| 1390 | if (cl_pos->timer_count) { |
| 1391 | if (--cl_pos->timer_count == 0) { |
| 1392 | dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n"); |
| 1393 | mei_reset(dev, 1); |
| 1394 | goto out; |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1399 | if (dev->iamthif_stall_timer) { |
| 1400 | if (--dev->iamthif_stall_timer == 0) { |
Masanari Iida | 32de21f | 2012-01-25 23:14:56 +0900 | [diff] [blame] | 1401 | dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n"); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1402 | mei_reset(dev, 1); |
| 1403 | dev->iamthif_msg_buf_size = 0; |
| 1404 | dev->iamthif_msg_buf_index = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 1405 | dev->iamthif_canceled = false; |
| 1406 | dev->iamthif_ioctl = true; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1407 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
| 1408 | dev->iamthif_timer = 0; |
| 1409 | |
| 1410 | if (dev->iamthif_current_cb) |
| 1411 | mei_free_cb_private(dev->iamthif_current_cb); |
| 1412 | |
| 1413 | dev->iamthif_file_object = NULL; |
| 1414 | dev->iamthif_current_cb = NULL; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 1415 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | if (dev->iamthif_timer) { |
| 1420 | |
| 1421 | timeout = dev->iamthif_timer + |
| 1422 | msecs_to_jiffies(IAMTHIF_READ_TIMER); |
| 1423 | |
| 1424 | dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n", |
| 1425 | dev->iamthif_timer); |
| 1426 | dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout); |
| 1427 | dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies); |
| 1428 | if (time_after(jiffies, timeout)) { |
| 1429 | /* |
| 1430 | * User didn't read the AMTHI data on time (15sec) |
| 1431 | * freeing AMTHI for other requests |
| 1432 | */ |
| 1433 | |
| 1434 | dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n"); |
| 1435 | |
| 1436 | amthi_complete_list = &dev->amthi_read_complete_list. |
| 1437 | mei_cb.cb_list; |
| 1438 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1439 | list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, cb_list) { |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1440 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1441 | cl_pos = cb_pos->file_object->private_data; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1442 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 1443 | /* Finding the AMTHI entry. */ |
| 1444 | if (cl_pos == &dev->iamthif_cl) |
| 1445 | list_del(&cb_pos->cb_list); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1446 | } |
| 1447 | if (dev->iamthif_current_cb) |
| 1448 | mei_free_cb_private(dev->iamthif_current_cb); |
| 1449 | |
| 1450 | dev->iamthif_file_object->private_data = NULL; |
| 1451 | dev->iamthif_file_object = NULL; |
| 1452 | dev->iamthif_current_cb = NULL; |
| 1453 | dev->iamthif_timer = 0; |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 1454 | mei_run_next_iamthif_cmd(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1455 | |
| 1456 | } |
| 1457 | } |
| 1458 | out: |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 1459 | schedule_delayed_work(&dev->timer_work, 2 * HZ); |
| 1460 | mutex_unlock(&dev->device_lock); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * mei_interrupt_thread_handler - function called after ISR to handle the interrupt |
| 1465 | * processing. |
| 1466 | * |
| 1467 | * @irq: The irq number |
| 1468 | * @dev_id: pointer to the device structure |
| 1469 | * |
| 1470 | * returns irqreturn_t |
| 1471 | * |
| 1472 | */ |
| 1473 | irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id) |
| 1474 | { |
| 1475 | struct mei_device *dev = (struct mei_device *) dev_id; |
| 1476 | struct mei_io_list complete_list; |
| 1477 | struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL; |
| 1478 | struct mei_cl *cl; |
| 1479 | s32 slots; |
| 1480 | int rets; |
| 1481 | bool bus_message_received; |
| 1482 | |
| 1483 | |
| 1484 | dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n"); |
| 1485 | /* initialize our complete list */ |
| 1486 | mutex_lock(&dev->device_lock); |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 1487 | mei_io_list_init(&complete_list); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1488 | dev->host_hw_state = mei_hcsr_read(dev); |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1489 | |
| 1490 | /* Ack the interrupt here |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 1491 | * In case of MSI we don't go through the quick handler */ |
Tomas Winkler | 4f61a7a | 2011-07-14 20:11:25 +0300 | [diff] [blame] | 1492 | if (pci_dev_msi_enabled(dev->pdev)) |
| 1493 | mei_reg_write(dev, H_CSR, dev->host_hw_state); |
| 1494 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1495 | dev->me_hw_state = mei_mecsr_read(dev); |
| 1496 | |
| 1497 | /* check if ME wants a reset */ |
| 1498 | if ((dev->me_hw_state & ME_RDY_HRA) == 0 && |
| 1499 | dev->mei_state != MEI_RESETING && |
| 1500 | dev->mei_state != MEI_INITIALIZING) { |
| 1501 | dev_dbg(&dev->pdev->dev, "FW not ready.\n"); |
| 1502 | mei_reset(dev, 1); |
| 1503 | mutex_unlock(&dev->device_lock); |
| 1504 | return IRQ_HANDLED; |
| 1505 | } |
| 1506 | |
| 1507 | /* check if we need to start the dev */ |
| 1508 | if ((dev->host_hw_state & H_RDY) == 0) { |
| 1509 | if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) { |
| 1510 | dev_dbg(&dev->pdev->dev, "we need to start the dev.\n"); |
| 1511 | dev->host_hw_state |= (H_IE | H_IG | H_RDY); |
| 1512 | mei_hcsr_set(dev); |
| 1513 | dev->mei_state = MEI_INIT_CLIENTS; |
| 1514 | dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n"); |
| 1515 | /* link is established |
| 1516 | * start sending messages. |
| 1517 | */ |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 1518 | mei_host_start_message(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1519 | mutex_unlock(&dev->device_lock); |
| 1520 | return IRQ_HANDLED; |
| 1521 | } else { |
| 1522 | dev_dbg(&dev->pdev->dev, "FW not ready.\n"); |
| 1523 | mutex_unlock(&dev->device_lock); |
| 1524 | return IRQ_HANDLED; |
| 1525 | } |
| 1526 | } |
Justin P. Mattock | 5f9092f3 | 2012-03-12 07:18:09 -0700 | [diff] [blame] | 1527 | /* check slots available for reading */ |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1528 | slots = mei_count_full_read_slots(dev); |
| 1529 | dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n", |
| 1530 | slots, dev->extra_write_index); |
| 1531 | while (slots > 0 && !dev->extra_write_index) { |
| 1532 | dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n", |
| 1533 | slots, dev->extra_write_index); |
| 1534 | dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n"); |
| 1535 | rets = mei_irq_thread_read_handler(&complete_list, dev, &slots); |
| 1536 | if (rets) |
| 1537 | goto end; |
| 1538 | } |
| 1539 | rets = mei_irq_thread_write_handler(&complete_list, dev, &slots); |
| 1540 | end: |
| 1541 | dev_dbg(&dev->pdev->dev, "end of bottom half function.\n"); |
| 1542 | dev->host_hw_state = mei_hcsr_read(dev); |
Tomas Winkler | 726917f | 2012-06-25 23:46:28 +0300 | [diff] [blame] | 1543 | dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1544 | |
| 1545 | bus_message_received = false; |
| 1546 | if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) { |
| 1547 | dev_dbg(&dev->pdev->dev, "received waiting bus message\n"); |
| 1548 | bus_message_received = true; |
| 1549 | } |
| 1550 | mutex_unlock(&dev->device_lock); |
| 1551 | if (bus_message_received) { |
| 1552 | dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n"); |
| 1553 | wake_up_interruptible(&dev->wait_recvd_msg); |
| 1554 | bus_message_received = false; |
| 1555 | } |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 1556 | if (list_empty(&complete_list.mei_cb.cb_list)) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 1557 | return IRQ_HANDLED; |
| 1558 | |
| 1559 | |
| 1560 | list_for_each_entry_safe(cb_pos, cb_next, |
| 1561 | &complete_list.mei_cb.cb_list, cb_list) { |
| 1562 | cl = (struct mei_cl *)cb_pos->file_private; |
| 1563 | list_del(&cb_pos->cb_list); |
| 1564 | if (cl) { |
| 1565 | if (cl != &dev->iamthif_cl) { |
| 1566 | dev_dbg(&dev->pdev->dev, "completing call back.\n"); |
| 1567 | _mei_cmpl(cl, cb_pos); |
| 1568 | cb_pos = NULL; |
| 1569 | } else if (cl == &dev->iamthif_cl) { |
| 1570 | _mei_cmpl_iamthif(dev, cb_pos); |
| 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | return IRQ_HANDLED; |
| 1575 | } |