Commit | Line | Data |
---|---|---|
19838fb8 TW |
1 | /* |
2 | * | |
3 | * Intel Management Engine Interface (Intel MEI) Linux driver | |
4 | * Copyright (c) 2003-2012, Intel Corporation. | |
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 | #include <linux/kernel.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/errno.h> | |
20 | #include <linux/types.h> | |
21 | #include <linux/fcntl.h> | |
22 | #include <linux/aio.h> | |
23 | #include <linux/pci.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/ioctl.h> | |
26 | #include <linux/cdev.h> | |
27 | #include <linux/list.h> | |
28 | #include <linux/delay.h> | |
29 | #include <linux/sched.h> | |
30 | #include <linux/uuid.h> | |
31 | #include <linux/jiffies.h> | |
32 | #include <linux/uaccess.h> | |
33 | ||
47a73801 | 34 | #include <linux/mei.h> |
19838fb8 TW |
35 | |
36 | #include "mei_dev.h" | |
19838fb8 TW |
37 | #include "interface.h" |
38 | ||
39 | const uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac, | |
40 | 0xa8, 0x46, 0xe0, 0xff, 0x65, | |
41 | 0x81, 0x4c); | |
42 | ||
43 | /** | |
44 | * mei_amthif_reset_params - initializes mei device iamthif | |
45 | * | |
46 | * @dev: the device structure | |
47 | */ | |
48 | void mei_amthif_reset_params(struct mei_device *dev) | |
49 | { | |
50 | /* reset iamthif parameters. */ | |
51 | dev->iamthif_current_cb = NULL; | |
52 | dev->iamthif_msg_buf_size = 0; | |
53 | dev->iamthif_msg_buf_index = 0; | |
54 | dev->iamthif_canceled = false; | |
55 | dev->iamthif_ioctl = false; | |
56 | dev->iamthif_state = MEI_IAMTHIF_IDLE; | |
57 | dev->iamthif_timer = 0; | |
58 | } | |
59 | ||
60 | /** | |
61 | * mei_amthif_host_init_ - mei initialization amthif client. | |
62 | * | |
63 | * @dev: the device structure | |
64 | * | |
65 | */ | |
66 | void mei_amthif_host_init(struct mei_device *dev) | |
67 | { | |
68 | int i; | |
69 | unsigned char *msg_buf; | |
70 | ||
71 | mei_cl_init(&dev->iamthif_cl, dev); | |
72 | dev->iamthif_cl.state = MEI_FILE_DISCONNECTED; | |
73 | ||
74 | /* find ME amthi client */ | |
ff8b2f4e | 75 | i = mei_me_cl_link(dev, &dev->iamthif_cl, |
19838fb8 TW |
76 | &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID); |
77 | if (i < 0) { | |
ff8b2f4e | 78 | dev_info(&dev->pdev->dev, "failed to find iamthif client.\n"); |
19838fb8 TW |
79 | return; |
80 | } | |
81 | ||
82 | /* Assign iamthif_mtu to the value received from ME */ | |
83 | ||
84 | dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length; | |
85 | dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n", | |
86 | dev->me_clients[i].props.max_msg_length); | |
87 | ||
88 | kfree(dev->iamthif_msg_buf); | |
89 | dev->iamthif_msg_buf = NULL; | |
90 | ||
91 | /* allocate storage for ME message buffer */ | |
92 | msg_buf = kcalloc(dev->iamthif_mtu, | |
93 | sizeof(unsigned char), GFP_KERNEL); | |
94 | if (!msg_buf) { | |
95 | dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n"); | |
96 | return; | |
97 | } | |
98 | ||
99 | dev->iamthif_msg_buf = msg_buf; | |
100 | ||
101 | if (mei_connect(dev, &dev->iamthif_cl)) { | |
102 | dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n"); | |
103 | dev->iamthif_cl.state = MEI_FILE_DISCONNECTED; | |
104 | dev->iamthif_cl.host_client_id = 0; | |
105 | } else { | |
106 | dev->iamthif_cl.timer_count = MEI_CONNECT_TIMEOUT; | |
107 | } | |
108 | } | |
109 | ||
110 | /** | |
111 | * mei_amthif_find_read_list_entry - finds a amthilist entry for current file | |
112 | * | |
113 | * @dev: the device structure | |
114 | * @file: pointer to file object | |
115 | * | |
116 | * returns returned a list entry on success, NULL on failure. | |
117 | */ | |
118 | struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev, | |
119 | struct file *file) | |
120 | { | |
19838fb8 TW |
121 | struct mei_cl_cb *pos = NULL; |
122 | struct mei_cl_cb *next = NULL; | |
123 | ||
124 | list_for_each_entry_safe(pos, next, | |
e773efc4 | 125 | &dev->amthif_rd_complete_list.list, list) { |
db3ed431 | 126 | if (pos->cl && pos->cl == &dev->iamthif_cl && |
19838fb8 TW |
127 | pos->file_object == file) |
128 | return pos; | |
129 | } | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | ||
134 | /** | |
135 | * mei_amthif_read - read data from AMTHIF client | |
136 | * | |
137 | * @dev: the device structure | |
138 | * @if_num: minor number | |
139 | * @file: pointer to file object | |
140 | * @*ubuf: pointer to user data in user space | |
141 | * @length: data length to read | |
142 | * @offset: data read offset | |
143 | * | |
144 | * Locking: called under "dev->device_lock" lock | |
145 | * | |
146 | * returns | |
147 | * returned data length on success, | |
148 | * zero if no data to read, | |
149 | * negative on failure. | |
150 | */ | |
151 | int mei_amthif_read(struct mei_device *dev, struct file *file, | |
152 | char __user *ubuf, size_t length, loff_t *offset) | |
153 | { | |
154 | int rets; | |
155 | int wait_ret; | |
156 | struct mei_cl_cb *cb = NULL; | |
157 | struct mei_cl *cl = file->private_data; | |
158 | unsigned long timeout; | |
159 | int i; | |
160 | ||
161 | /* Only Posible if we are in timeout */ | |
162 | if (!cl || cl != &dev->iamthif_cl) { | |
163 | dev_dbg(&dev->pdev->dev, "bad file ext.\n"); | |
164 | return -ETIMEDOUT; | |
165 | } | |
166 | ||
167 | i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id); | |
168 | ||
169 | if (i < 0) { | |
170 | dev_dbg(&dev->pdev->dev, "amthi client not found.\n"); | |
171 | return -ENODEV; | |
172 | } | |
173 | dev_dbg(&dev->pdev->dev, "checking amthi data\n"); | |
174 | cb = mei_amthif_find_read_list_entry(dev, file); | |
175 | ||
176 | /* Check for if we can block or not*/ | |
177 | if (cb == NULL && file->f_flags & O_NONBLOCK) | |
178 | return -EAGAIN; | |
179 | ||
180 | ||
181 | dev_dbg(&dev->pdev->dev, "waiting for amthi data\n"); | |
182 | while (cb == NULL) { | |
183 | /* unlock the Mutex */ | |
184 | mutex_unlock(&dev->device_lock); | |
185 | ||
186 | wait_ret = wait_event_interruptible(dev->iamthif_cl.wait, | |
187 | (cb = mei_amthif_find_read_list_entry(dev, file))); | |
188 | ||
189 | if (wait_ret) | |
190 | return -ERESTARTSYS; | |
191 | ||
192 | dev_dbg(&dev->pdev->dev, "woke up from sleep\n"); | |
193 | ||
194 | /* Locking again the Mutex */ | |
195 | mutex_lock(&dev->device_lock); | |
196 | } | |
197 | ||
198 | ||
199 | dev_dbg(&dev->pdev->dev, "Got amthi data\n"); | |
200 | dev->iamthif_timer = 0; | |
201 | ||
202 | if (cb) { | |
203 | timeout = cb->read_time + | |
204 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); | |
205 | dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n", | |
206 | timeout); | |
207 | ||
208 | if (time_after(jiffies, timeout)) { | |
209 | dev_dbg(&dev->pdev->dev, "amthi Time out\n"); | |
210 | /* 15 sec for the message has expired */ | |
211 | list_del(&cb->list); | |
212 | rets = -ETIMEDOUT; | |
213 | goto free; | |
214 | } | |
215 | } | |
216 | /* if the whole message will fit remove it from the list */ | |
217 | if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset)) | |
218 | list_del(&cb->list); | |
219 | else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { | |
220 | /* end of the message has been reached */ | |
221 | list_del(&cb->list); | |
222 | rets = 0; | |
223 | goto free; | |
224 | } | |
225 | /* else means that not full buffer will be read and do not | |
226 | * remove message from deletion list | |
227 | */ | |
228 | ||
229 | dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n", | |
230 | cb->response_buffer.size); | |
231 | dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx); | |
232 | ||
233 | /* length is being turncated to PAGE_SIZE, however, | |
234 | * the buf_idx may point beyond */ | |
235 | length = min_t(size_t, length, (cb->buf_idx - *offset)); | |
236 | ||
237 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) | |
238 | rets = -EFAULT; | |
239 | else { | |
240 | rets = length; | |
241 | if ((*offset + length) < cb->buf_idx) { | |
242 | *offset += length; | |
243 | goto out; | |
244 | } | |
245 | } | |
246 | free: | |
247 | dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n"); | |
248 | *offset = 0; | |
249 | mei_io_cb_free(cb); | |
250 | out: | |
251 | return rets; | |
252 | } | |
253 | ||
254 | /** | |
ab5c4a56 | 255 | * mei_amthif_send_cmd - send amthif command to the ME |
19838fb8 TW |
256 | * |
257 | * @dev: the device structure | |
258 | * @cb: mei call back struct | |
259 | * | |
260 | * returns 0 on success, <0 on failure. | |
ab5c4a56 | 261 | * |
19838fb8 | 262 | */ |
ab5c4a56 | 263 | static int mei_amthif_send_cmd(struct mei_device *dev, struct mei_cl_cb *cb) |
19838fb8 TW |
264 | { |
265 | struct mei_msg_hdr mei_hdr; | |
266 | int ret; | |
267 | ||
268 | if (!dev || !cb) | |
269 | return -ENODEV; | |
270 | ||
271 | dev_dbg(&dev->pdev->dev, "write data to amthi client.\n"); | |
272 | ||
273 | dev->iamthif_state = MEI_IAMTHIF_WRITING; | |
274 | dev->iamthif_current_cb = cb; | |
275 | dev->iamthif_file_object = cb->file_object; | |
276 | dev->iamthif_canceled = false; | |
277 | dev->iamthif_ioctl = true; | |
278 | dev->iamthif_msg_buf_size = cb->request_buffer.size; | |
279 | memcpy(dev->iamthif_msg_buf, cb->request_buffer.data, | |
280 | cb->request_buffer.size); | |
281 | ||
282 | ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl); | |
283 | if (ret < 0) | |
284 | return ret; | |
285 | ||
286 | if (ret && dev->mei_host_buffer_is_empty) { | |
287 | ret = 0; | |
288 | dev->mei_host_buffer_is_empty = false; | |
289 | if (cb->request_buffer.size > mei_hbuf_max_data(dev)) { | |
290 | mei_hdr.length = mei_hbuf_max_data(dev); | |
291 | mei_hdr.msg_complete = 0; | |
292 | } else { | |
293 | mei_hdr.length = cb->request_buffer.size; | |
294 | mei_hdr.msg_complete = 1; | |
295 | } | |
296 | ||
297 | mei_hdr.host_addr = dev->iamthif_cl.host_client_id; | |
298 | mei_hdr.me_addr = dev->iamthif_cl.me_client_id; | |
299 | mei_hdr.reserved = 0; | |
300 | dev->iamthif_msg_buf_index += mei_hdr.length; | |
301 | if (mei_write_message(dev, &mei_hdr, | |
438763f3 | 302 | (unsigned char *)dev->iamthif_msg_buf)) |
19838fb8 TW |
303 | return -ENODEV; |
304 | ||
305 | if (mei_hdr.msg_complete) { | |
306 | if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl)) | |
307 | return -ENODEV; | |
308 | dev->iamthif_flow_control_pending = true; | |
309 | dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL; | |
310 | dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n"); | |
311 | dev->iamthif_current_cb = cb; | |
312 | dev->iamthif_file_object = cb->file_object; | |
313 | list_add_tail(&cb->list, &dev->write_waiting_list.list); | |
314 | } else { | |
315 | dev_dbg(&dev->pdev->dev, "message does not complete, so add amthi cb to write list.\n"); | |
316 | list_add_tail(&cb->list, &dev->write_list.list); | |
317 | } | |
318 | } else { | |
319 | if (!(dev->mei_host_buffer_is_empty)) | |
320 | dev_dbg(&dev->pdev->dev, "host buffer is not empty"); | |
321 | ||
322 | dev_dbg(&dev->pdev->dev, "No flow control credentials, so add iamthif cb to write list.\n"); | |
323 | list_add_tail(&cb->list, &dev->write_list.list); | |
324 | } | |
325 | return 0; | |
326 | } | |
327 | ||
ab5c4a56 TW |
328 | /** |
329 | * mei_amthif_write - write amthif data to amthif client | |
330 | * | |
331 | * @dev: the device structure | |
332 | * @cb: mei call back struct | |
333 | * | |
334 | * returns 0 on success, <0 on failure. | |
335 | * | |
336 | */ | |
337 | int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *cb) | |
338 | { | |
339 | int ret; | |
340 | ||
341 | if (!dev || !cb) | |
342 | return -ENODEV; | |
343 | ||
344 | ret = mei_io_cb_alloc_resp_buf(cb, dev->iamthif_mtu); | |
345 | if (ret) | |
346 | return ret; | |
347 | ||
4b8960b4 | 348 | cb->fop_type = MEI_FOP_IOCTL; |
ab5c4a56 | 349 | |
e773efc4 | 350 | if (!list_empty(&dev->amthif_cmd_list.list) || |
ab5c4a56 TW |
351 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { |
352 | dev_dbg(&dev->pdev->dev, | |
353 | "amthif state = %d\n", dev->iamthif_state); | |
354 | dev_dbg(&dev->pdev->dev, "AMTHIF: add cb to the wait list\n"); | |
e773efc4 | 355 | list_add_tail(&cb->list, &dev->amthif_cmd_list.list); |
ab5c4a56 TW |
356 | return 0; |
357 | } | |
358 | return mei_amthif_send_cmd(dev, cb); | |
359 | } | |
19838fb8 TW |
360 | /** |
361 | * mei_amthif_run_next_cmd | |
362 | * | |
363 | * @dev: the device structure | |
364 | * | |
365 | * returns 0 on success, <0 on failure. | |
366 | */ | |
367 | void mei_amthif_run_next_cmd(struct mei_device *dev) | |
368 | { | |
19838fb8 TW |
369 | struct mei_cl_cb *pos = NULL; |
370 | struct mei_cl_cb *next = NULL; | |
371 | int status; | |
372 | ||
373 | if (!dev) | |
374 | return; | |
375 | ||
376 | dev->iamthif_msg_buf_size = 0; | |
377 | dev->iamthif_msg_buf_index = 0; | |
378 | dev->iamthif_canceled = false; | |
379 | dev->iamthif_ioctl = true; | |
380 | dev->iamthif_state = MEI_IAMTHIF_IDLE; | |
381 | dev->iamthif_timer = 0; | |
382 | dev->iamthif_file_object = NULL; | |
383 | ||
384 | dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n"); | |
385 | ||
e773efc4 | 386 | list_for_each_entry_safe(pos, next, &dev->amthif_cmd_list.list, list) { |
19838fb8 | 387 | list_del(&pos->list); |
19838fb8 | 388 | |
db3ed431 | 389 | if (pos->cl && pos->cl == &dev->iamthif_cl) { |
ab5c4a56 | 390 | status = mei_amthif_send_cmd(dev, pos); |
19838fb8 TW |
391 | if (status) { |
392 | dev_dbg(&dev->pdev->dev, | |
393 | "amthi write failed status = %d\n", | |
394 | status); | |
395 | return; | |
396 | } | |
397 | break; | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
744f0f2f TW |
402 | |
403 | unsigned int mei_amthif_poll(struct mei_device *dev, | |
404 | struct file *file, poll_table *wait) | |
405 | { | |
406 | unsigned int mask = 0; | |
407 | mutex_unlock(&dev->device_lock); | |
408 | poll_wait(file, &dev->iamthif_cl.wait, wait); | |
409 | mutex_lock(&dev->device_lock); | |
410 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE && | |
411 | dev->iamthif_file_object == file) { | |
412 | mask |= (POLLIN | POLLRDNORM); | |
413 | dev_dbg(&dev->pdev->dev, "run next amthi cb\n"); | |
414 | mei_amthif_run_next_cmd(dev); | |
415 | } | |
416 | return mask; | |
417 | } | |
418 | ||
419 | ||
420 | ||
19838fb8 TW |
421 | /** |
422 | * mei_amthif_irq_process_completed - processes completed iamthif operation. | |
423 | * | |
424 | * @dev: the device structure. | |
425 | * @slots: free slots. | |
426 | * @cb_pos: callback block. | |
427 | * @cl: private data of the file object. | |
428 | * @cmpl_list: complete list. | |
429 | * | |
430 | * returns 0, OK; otherwise, error. | |
431 | */ | |
24c656e5 TW |
432 | int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots, |
433 | struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list) | |
19838fb8 TW |
434 | { |
435 | struct mei_msg_hdr *mei_hdr; | |
24c656e5 TW |
436 | struct mei_cl *cl = cb->cl; |
437 | size_t len = dev->iamthif_msg_buf_size - dev->iamthif_msg_buf_index; | |
438 | size_t msg_slots = mei_data2slots(len); | |
19838fb8 | 439 | |
24c656e5 TW |
440 | mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0]; |
441 | mei_hdr->host_addr = cl->host_client_id; | |
442 | mei_hdr->me_addr = cl->me_client_id; | |
443 | mei_hdr->reserved = 0; | |
444 | ||
445 | if (*slots >= msg_slots) { | |
446 | mei_hdr->length = len; | |
19838fb8 | 447 | mei_hdr->msg_complete = 1; |
24c656e5 TW |
448 | /* Split the message only if we can write the whole host buffer */ |
449 | } else if (*slots == dev->hbuf_depth) { | |
450 | msg_slots = *slots; | |
451 | len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr); | |
452 | mei_hdr->length = len; | |
453 | mei_hdr->msg_complete = 0; | |
454 | } else { | |
455 | /* wait for next time the host buffer is empty */ | |
456 | return 0; | |
457 | } | |
19838fb8 | 458 | |
15d4acc5 | 459 | dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr)); |
19838fb8 | 460 | |
24c656e5 TW |
461 | *slots -= msg_slots; |
462 | if (mei_write_message(dev, mei_hdr, | |
438763f3 | 463 | dev->iamthif_msg_buf + dev->iamthif_msg_buf_index)) { |
19838fb8 TW |
464 | dev->iamthif_state = MEI_IAMTHIF_IDLE; |
465 | cl->status = -ENODEV; | |
24c656e5 | 466 | list_del(&cb->list); |
19838fb8 | 467 | return -ENODEV; |
24c656e5 | 468 | } |
19838fb8 | 469 | |
24c656e5 TW |
470 | if (mei_flow_ctrl_reduce(dev, cl)) |
471 | return -ENODEV; | |
19838fb8 | 472 | |
24c656e5 TW |
473 | dev->iamthif_msg_buf_index += mei_hdr->length; |
474 | cl->status = 0; | |
19838fb8 | 475 | |
24c656e5 TW |
476 | if (mei_hdr->msg_complete) { |
477 | dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL; | |
478 | dev->iamthif_flow_control_pending = true; | |
479 | ||
480 | /* save iamthif cb sent to amthi client */ | |
481 | cb->buf_idx = dev->iamthif_msg_buf_index; | |
482 | dev->iamthif_current_cb = cb; | |
483 | ||
484 | list_move_tail(&cb->list, &dev->write_waiting_list.list); | |
19838fb8 TW |
485 | } |
486 | ||
24c656e5 | 487 | |
19838fb8 TW |
488 | return 0; |
489 | } | |
490 | ||
491 | /** | |
492 | * mei_amthif_irq_read_message - read routine after ISR to | |
493 | * handle the read amthi message | |
494 | * | |
495 | * @complete_list: An instance of our list structure | |
496 | * @dev: the device structure | |
497 | * @mei_hdr: header of amthi message | |
498 | * | |
499 | * returns 0 on success, <0 on failure. | |
500 | */ | |
501 | int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list, | |
502 | struct mei_device *dev, struct mei_msg_hdr *mei_hdr) | |
503 | { | |
19838fb8 TW |
504 | struct mei_cl_cb *cb; |
505 | unsigned char *buffer; | |
506 | ||
507 | BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id); | |
508 | BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING); | |
509 | ||
510 | buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index; | |
511 | BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length); | |
512 | ||
513 | mei_read_slots(dev, buffer, mei_hdr->length); | |
514 | ||
515 | dev->iamthif_msg_buf_index += mei_hdr->length; | |
516 | ||
517 | if (!mei_hdr->msg_complete) | |
518 | return 0; | |
519 | ||
520 | dev_dbg(&dev->pdev->dev, | |
521 | "amthi_message_buffer_index =%d\n", | |
522 | mei_hdr->length); | |
523 | ||
524 | dev_dbg(&dev->pdev->dev, "completed amthi read.\n "); | |
525 | if (!dev->iamthif_current_cb) | |
526 | return -ENODEV; | |
527 | ||
528 | cb = dev->iamthif_current_cb; | |
529 | dev->iamthif_current_cb = NULL; | |
530 | ||
db3ed431 | 531 | if (!cb->cl) |
19838fb8 TW |
532 | return -ENODEV; |
533 | ||
534 | dev->iamthif_stall_timer = 0; | |
535 | cb->buf_idx = dev->iamthif_msg_buf_index; | |
536 | cb->read_time = jiffies; | |
db3ed431 | 537 | if (dev->iamthif_ioctl && cb->cl == &dev->iamthif_cl) { |
19838fb8 TW |
538 | /* found the iamthif cb */ |
539 | dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n "); | |
540 | dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n "); | |
541 | list_add_tail(&cb->list, &complete_list->list); | |
542 | } | |
543 | return 0; | |
544 | } | |
545 | ||
546 | /** | |
547 | * mei_amthif_irq_read - prepares to read amthif data. | |
548 | * | |
549 | * @dev: the device structure. | |
550 | * @slots: free slots. | |
551 | * | |
552 | * returns 0, OK; otherwise, error. | |
553 | */ | |
554 | int mei_amthif_irq_read(struct mei_device *dev, s32 *slots) | |
555 | { | |
556 | ||
557 | if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr) | |
558 | + sizeof(struct hbm_flow_control))) { | |
559 | return -EMSGSIZE; | |
560 | } | |
561 | *slots -= mei_data2slots(sizeof(struct hbm_flow_control)); | |
562 | if (mei_send_flow_control(dev, &dev->iamthif_cl)) { | |
563 | dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n"); | |
564 | return -EIO; | |
565 | } | |
566 | ||
567 | dev_dbg(&dev->pdev->dev, "iamthif flow control success\n"); | |
568 | dev->iamthif_state = MEI_IAMTHIF_READING; | |
569 | dev->iamthif_flow_control_pending = false; | |
570 | dev->iamthif_msg_buf_index = 0; | |
571 | dev->iamthif_msg_buf_size = 0; | |
572 | dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER; | |
573 | dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev); | |
574 | return 0; | |
575 | } | |
576 | ||
577 | /** | |
578 | * mei_amthif_complete - complete amthif callback. | |
579 | * | |
580 | * @dev: the device structure. | |
581 | * @cb_pos: callback block. | |
582 | */ | |
583 | void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb) | |
584 | { | |
585 | if (dev->iamthif_canceled != 1) { | |
586 | dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE; | |
587 | dev->iamthif_stall_timer = 0; | |
588 | memcpy(cb->response_buffer.data, | |
589 | dev->iamthif_msg_buf, | |
590 | dev->iamthif_msg_buf_index); | |
e773efc4 | 591 | list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list); |
19838fb8 TW |
592 | dev_dbg(&dev->pdev->dev, "amthi read completed\n"); |
593 | dev->iamthif_timer = jiffies; | |
594 | dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n", | |
595 | dev->iamthif_timer); | |
596 | } else { | |
597 | mei_amthif_run_next_cmd(dev); | |
598 | } | |
599 | ||
600 | dev_dbg(&dev->pdev->dev, "completing amthi call back.\n"); | |
601 | wake_up_interruptible(&dev->iamthif_cl.wait); | |
602 | } | |
603 | ||
a562d5c2 TW |
604 | /** |
605 | * mei_clear_list - removes all callbacks associated with file | |
606 | * from mei_cb_list | |
607 | * | |
608 | * @dev: device structure. | |
609 | * @file: file structure | |
610 | * @mei_cb_list: callbacks list | |
611 | * | |
612 | * mei_clear_list is called to clear resources associated with file | |
613 | * when application calls close function or Ctrl-C was pressed | |
614 | * | |
615 | * returns true if callback removed from the list, false otherwise | |
616 | */ | |
617 | static bool mei_clear_list(struct mei_device *dev, | |
618 | const struct file *file, struct list_head *mei_cb_list) | |
619 | { | |
620 | struct mei_cl_cb *cb_pos = NULL; | |
621 | struct mei_cl_cb *cb_next = NULL; | |
622 | bool removed = false; | |
623 | ||
624 | /* list all list member */ | |
625 | list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, list) { | |
626 | /* check if list member associated with a file */ | |
627 | if (file == cb_pos->file_object) { | |
628 | /* remove member from the list */ | |
629 | list_del(&cb_pos->list); | |
630 | /* check if cb equal to current iamthif cb */ | |
631 | if (dev->iamthif_current_cb == cb_pos) { | |
632 | dev->iamthif_current_cb = NULL; | |
633 | /* send flow control to iamthif client */ | |
634 | mei_send_flow_control(dev, &dev->iamthif_cl); | |
635 | } | |
636 | /* free all allocated buffers */ | |
637 | mei_io_cb_free(cb_pos); | |
638 | cb_pos = NULL; | |
639 | removed = true; | |
640 | } | |
641 | } | |
642 | return removed; | |
643 | } | |
644 | ||
645 | /** | |
646 | * mei_clear_lists - removes all callbacks associated with file | |
647 | * | |
648 | * @dev: device structure | |
649 | * @file: file structure | |
650 | * | |
651 | * mei_clear_lists is called to clear resources associated with file | |
652 | * when application calls close function or Ctrl-C was pressed | |
653 | * | |
654 | * returns true if callback removed from the list, false otherwise | |
655 | */ | |
656 | static bool mei_clear_lists(struct mei_device *dev, struct file *file) | |
657 | { | |
658 | bool removed = false; | |
659 | ||
660 | /* remove callbacks associated with a file */ | |
661 | mei_clear_list(dev, file, &dev->amthif_cmd_list.list); | |
662 | if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list)) | |
663 | removed = true; | |
19838fb8 | 664 | |
a562d5c2 TW |
665 | mei_clear_list(dev, file, &dev->ctrl_rd_list.list); |
666 | ||
667 | if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list)) | |
668 | removed = true; | |
669 | ||
670 | if (mei_clear_list(dev, file, &dev->write_waiting_list.list)) | |
671 | removed = true; | |
672 | ||
673 | if (mei_clear_list(dev, file, &dev->write_list.list)) | |
674 | removed = true; | |
675 | ||
676 | /* check if iamthif_current_cb not NULL */ | |
677 | if (dev->iamthif_current_cb && !removed) { | |
678 | /* check file and iamthif current cb association */ | |
679 | if (dev->iamthif_current_cb->file_object == file) { | |
680 | /* remove cb */ | |
681 | mei_io_cb_free(dev->iamthif_current_cb); | |
682 | dev->iamthif_current_cb = NULL; | |
683 | removed = true; | |
684 | } | |
685 | } | |
686 | return removed; | |
687 | } | |
688 | ||
689 | /** | |
690 | * mei_amthif_release - the release function | |
691 | * | |
692 | * @inode: pointer to inode structure | |
693 | * @file: pointer to file structure | |
694 | * | |
695 | * returns 0 on success, <0 on error | |
696 | */ | |
697 | int mei_amthif_release(struct mei_device *dev, struct file *file) | |
698 | { | |
699 | if (dev->open_handle_count > 0) | |
700 | dev->open_handle_count--; | |
701 | ||
702 | if (dev->iamthif_file_object == file && | |
703 | dev->iamthif_state != MEI_IAMTHIF_IDLE) { | |
704 | ||
705 | dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n", | |
706 | dev->iamthif_state); | |
707 | dev->iamthif_canceled = true; | |
708 | if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) { | |
709 | dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n"); | |
710 | mei_amthif_run_next_cmd(dev); | |
711 | } | |
712 | } | |
713 | ||
714 | if (mei_clear_lists(dev, file)) | |
715 | dev->iamthif_state = MEI_IAMTHIF_IDLE; | |
716 | ||
717 | return 0; | |
718 | } |