]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/misc/mei/amthif.c
mei: amthif: interrupt reader on link reset
[karo-tx-linux.git] / drivers / misc / mei / amthif.c
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/ioctl.h>
23 #include <linux/cdev.h>
24 #include <linux/list.h>
25 #include <linux/delay.h>
26 #include <linux/sched.h>
27 #include <linux/uuid.h>
28 #include <linux/jiffies.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
31
32 #include <linux/mei.h>
33
34 #include "mei_dev.h"
35 #include "hbm.h"
36 #include "client.h"
37
38 const uuid_le mei_amthif_guid  = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
39                                          0xac, 0xa8, 0x46, 0xe0,
40                                          0xff, 0x65, 0x81, 0x4c);
41
42 /**
43  * mei_amthif_reset_params - initializes mei device iamthif
44  *
45  * @dev: the device structure
46  */
47 void mei_amthif_reset_params(struct mei_device *dev)
48 {
49         /* reset iamthif parameters. */
50         dev->iamthif_current_cb = NULL;
51         dev->iamthif_canceled = false;
52         dev->iamthif_state = MEI_IAMTHIF_IDLE;
53         dev->iamthif_stall_timer = 0;
54         dev->iamthif_open_count = 0;
55 }
56
57 /**
58  * mei_amthif_host_init - mei initialization amthif client.
59  *
60  * @dev: the device structure
61  * @me_cl: me client
62  *
63  * Return: 0 on success, <0 on failure.
64  */
65 int mei_amthif_host_init(struct mei_device *dev, struct mei_me_client *me_cl)
66 {
67         struct mei_cl *cl = &dev->iamthif_cl;
68         int ret;
69
70         dev->iamthif_state = MEI_IAMTHIF_IDLE;
71
72         mei_cl_init(cl, dev);
73
74         ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
75         if (ret < 0) {
76                 dev_err(dev->dev, "amthif: failed cl_link %d\n", ret);
77                 return ret;
78         }
79
80         ret = mei_cl_connect(cl, me_cl, NULL);
81
82         dev->iamthif_state = MEI_IAMTHIF_IDLE;
83
84         return ret;
85 }
86
87 /**
88  * mei_amthif_read - read data from AMTHIF client
89  *
90  * @dev: the device structure
91  * @file: pointer to file object
92  * @ubuf: pointer to user data in user space
93  * @length: data length to read
94  * @offset: data read offset
95  *
96  * Locking: called under "dev->device_lock" lock
97  *
98  * Return:
99  *  returned data length on success,
100  *  zero if no data to read,
101  *  negative on failure.
102  */
103 int mei_amthif_read(struct mei_device *dev, struct file *file,
104                char __user *ubuf, size_t length, loff_t *offset)
105 {
106         struct mei_cl *cl = file->private_data;
107         struct mei_cl_cb *cb;
108         int rets;
109         int wait_ret;
110
111         dev_dbg(dev->dev, "checking amthif data\n");
112         cb = mei_cl_read_cb(cl, file);
113
114         /* Check for if we can block or not*/
115         if (cb == NULL && file->f_flags & O_NONBLOCK)
116                 return -EAGAIN;
117
118
119         dev_dbg(dev->dev, "waiting for amthif data\n");
120         while (cb == NULL) {
121                 /* unlock the Mutex */
122                 mutex_unlock(&dev->device_lock);
123
124                 wait_ret = wait_event_interruptible(cl->rx_wait,
125                                         !list_empty(&cl->rd_completed) ||
126                                         !mei_cl_is_connected(cl));
127
128                 /* Locking again the Mutex */
129                 mutex_lock(&dev->device_lock);
130
131                 if (wait_ret)
132                         return -ERESTARTSYS;
133
134                 if (!mei_cl_is_connected(cl)) {
135                         rets = -EBUSY;
136                         goto out;
137                 }
138
139                 cb = mei_cl_read_cb(cl, file);
140         }
141
142         if (cb->status) {
143                 rets = cb->status;
144                 dev_dbg(dev->dev, "read operation failed %d\n", rets);
145                 goto free;
146         }
147
148         dev_dbg(dev->dev, "Got amthif data\n");
149         /* if the whole message will fit remove it from the list */
150         if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
151                 list_del_init(&cb->list);
152         else if (cb->buf_idx <= *offset) {
153                 /* end of the message has been reached */
154                 list_del_init(&cb->list);
155                 rets = 0;
156                 goto free;
157         }
158                 /* else means that not full buffer will be read and do not
159                  * remove message from deletion list
160                  */
161
162         dev_dbg(dev->dev, "amthif cb->buf.size - %zd cb->buf_idx - %zd\n",
163                 cb->buf.size, cb->buf_idx);
164
165         /* length is being truncated to PAGE_SIZE, however,
166          * the buf_idx may point beyond */
167         length = min_t(size_t, length, (cb->buf_idx - *offset));
168
169         if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
170                 dev_dbg(dev->dev, "failed to copy data to userland\n");
171                 rets = -EFAULT;
172         } else {
173                 rets = length;
174                 if ((*offset + length) < cb->buf_idx) {
175                         *offset += length;
176                         goto out;
177                 }
178         }
179 free:
180         dev_dbg(dev->dev, "free amthif cb memory.\n");
181         *offset = 0;
182         mei_io_cb_free(cb);
183 out:
184         return rets;
185 }
186
187 /**
188  * mei_amthif_read_start - queue message for sending read credential
189  *
190  * @cl: host client
191  * @file: file pointer of message recipient
192  *
193  * Return: 0 on success, <0 on failure.
194  */
195 static int mei_amthif_read_start(struct mei_cl *cl, const struct file *file)
196 {
197         struct mei_device *dev = cl->dev;
198         struct mei_cl_cb *cb;
199         int rets;
200
201         cb = mei_io_cb_init(cl, MEI_FOP_READ, file);
202         if (!cb) {
203                 rets = -ENOMEM;
204                 goto err;
205         }
206
207         rets = mei_io_cb_alloc_buf(cb, mei_cl_mtu(cl));
208         if (rets)
209                 goto err;
210
211         list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
212
213         dev->iamthif_state = MEI_IAMTHIF_READING;
214         dev->iamthif_fp = cb->fp;
215         dev->iamthif_current_cb = cb;
216
217         return 0;
218 err:
219         mei_io_cb_free(cb);
220         return rets;
221 }
222
223 /**
224  * mei_amthif_send_cmd - send amthif command to the ME
225  *
226  * @cl: the host client
227  * @cb: mei call back struct
228  *
229  * Return: 0 on success, <0 on failure.
230  */
231 static int mei_amthif_send_cmd(struct mei_cl *cl, struct mei_cl_cb *cb)
232 {
233         struct mei_device *dev;
234         int ret;
235
236         if (!cl->dev || !cb)
237                 return -ENODEV;
238
239         dev = cl->dev;
240
241         dev->iamthif_state = MEI_IAMTHIF_WRITING;
242         dev->iamthif_current_cb = cb;
243         dev->iamthif_fp = cb->fp;
244         dev->iamthif_canceled = false;
245
246         ret = mei_cl_write(cl, cb, false);
247         if (ret < 0)
248                 return ret;
249
250         if (cb->completed)
251                 cb->status = mei_amthif_read_start(cl, cb->fp);
252
253         return 0;
254 }
255
256 /**
257  * mei_amthif_run_next_cmd - send next amt command from queue
258  *
259  * @dev: the device structure
260  *
261  * Return: 0 on success, <0 on failure.
262  */
263 int mei_amthif_run_next_cmd(struct mei_device *dev)
264 {
265         struct mei_cl *cl = &dev->iamthif_cl;
266         struct mei_cl_cb *cb;
267
268         dev->iamthif_canceled = false;
269         dev->iamthif_state = MEI_IAMTHIF_IDLE;
270         dev->iamthif_fp = NULL;
271
272         dev_dbg(dev->dev, "complete amthif cmd_list cb.\n");
273
274         cb = list_first_entry_or_null(&dev->amthif_cmd_list.list,
275                                         typeof(*cb), list);
276         if (!cb)
277                 return 0;
278
279         list_del_init(&cb->list);
280         return mei_amthif_send_cmd(cl, cb);
281 }
282
283 /**
284  * mei_amthif_write - write amthif data to amthif client
285  *
286  * @cl: host client
287  * @cb: mei call back struct
288  *
289  * Return: 0 on success, <0 on failure.
290  */
291 int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb)
292 {
293
294         struct mei_device *dev = cl->dev;
295
296         list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
297
298         /*
299          * The previous request is still in processing, queue this one.
300          */
301         if (dev->iamthif_state > MEI_IAMTHIF_IDLE &&
302             dev->iamthif_state < MEI_IAMTHIF_READ_COMPLETE)
303                 return 0;
304
305         return mei_amthif_run_next_cmd(dev);
306 }
307
308 /**
309  * mei_amthif_poll - the amthif poll function
310  *
311  * @dev: the device structure
312  * @file: pointer to file structure
313  * @wait: pointer to poll_table structure
314  *
315  * Return: poll mask
316  *
317  * Locking: called under "dev->device_lock" lock
318  */
319
320 unsigned int mei_amthif_poll(struct mei_device *dev,
321                 struct file *file, poll_table *wait)
322 {
323         unsigned int mask = 0;
324
325         poll_wait(file, &dev->iamthif_cl.rx_wait, wait);
326
327         if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
328             dev->iamthif_fp == file) {
329
330                 mask |= POLLIN | POLLRDNORM;
331                 mei_amthif_run_next_cmd(dev);
332         }
333
334         return mask;
335 }
336
337
338
339 /**
340  * mei_amthif_irq_write - write iamthif command in irq thread context.
341  *
342  * @cl: private data of the file object.
343  * @cb: callback block.
344  * @cmpl_list: complete list.
345  *
346  * Return: 0, OK; otherwise, error.
347  */
348 int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
349                          struct mei_cl_cb *cmpl_list)
350 {
351         int ret;
352
353         ret = mei_cl_irq_write(cl, cb, cmpl_list);
354         if (ret)
355                 return ret;
356
357         if (cb->completed)
358                 cb->status = mei_amthif_read_start(cl, cb->fp);
359
360         return 0;
361 }
362
363 /**
364  * mei_amthif_irq_read_msg - read routine after ISR to
365  *                      handle the read amthif message
366  *
367  * @cl: mei client
368  * @mei_hdr: header of amthif message
369  * @cmpl_list: completed callbacks list
370  *
371  * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
372  */
373 int mei_amthif_irq_read_msg(struct mei_cl *cl,
374                             struct mei_msg_hdr *mei_hdr,
375                             struct mei_cl_cb *cmpl_list)
376 {
377         struct mei_device *dev;
378         int ret;
379
380         dev = cl->dev;
381
382         if (dev->iamthif_state != MEI_IAMTHIF_READING)
383                 return 0;
384
385         ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
386         if (ret)
387                 return ret;
388
389         if (!mei_hdr->msg_complete)
390                 return 0;
391
392         dev_dbg(dev->dev, "completed amthif read.\n ");
393         dev->iamthif_current_cb = NULL;
394         dev->iamthif_stall_timer = 0;
395
396         return 0;
397 }
398
399 /**
400  * mei_amthif_complete - complete amthif callback.
401  *
402  * @cl: host client
403  * @cb: callback block.
404  */
405 void mei_amthif_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
406 {
407         struct mei_device *dev = cl->dev;
408
409         if (cb->fop_type == MEI_FOP_WRITE) {
410                 if (!cb->status) {
411                         dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
412                         mei_io_cb_free(cb);
413                         return;
414                 }
415                 /*
416                  * in case of error enqueue the write cb to complete read list
417                  * so it can be propagated to the reader
418                  */
419                 list_add_tail(&cb->list, &cl->rd_completed);
420                 wake_up_interruptible(&cl->rx_wait);
421                 return;
422         }
423
424         if (!dev->iamthif_canceled) {
425                 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
426                 dev->iamthif_stall_timer = 0;
427                 list_add_tail(&cb->list, &cl->rd_completed);
428                 dev_dbg(dev->dev, "amthif read completed\n");
429         } else {
430                 mei_amthif_run_next_cmd(dev);
431         }
432
433         dev_dbg(dev->dev, "completing amthif call back.\n");
434         wake_up_interruptible(&cl->rx_wait);
435 }
436
437 /**
438  * mei_clear_list - removes all callbacks associated with file
439  *              from mei_cb_list
440  *
441  * @dev: device structure.
442  * @file: file structure
443  * @mei_cb_list: callbacks list
444  *
445  * mei_clear_list is called to clear resources associated with file
446  * when application calls close function or Ctrl-C was pressed
447  *
448  * Return: true if callback removed from the list, false otherwise
449  */
450 static bool mei_clear_list(struct mei_device *dev,
451                 const struct file *file, struct list_head *mei_cb_list)
452 {
453         struct mei_cl *cl = &dev->iamthif_cl;
454         struct mei_cl_cb *cb, *next;
455         bool removed = false;
456
457         /* list all list member */
458         list_for_each_entry_safe(cb, next, mei_cb_list, list) {
459                 /* check if list member associated with a file */
460                 if (file == cb->fp) {
461                         /* check if cb equal to current iamthif cb */
462                         if (dev->iamthif_current_cb == cb) {
463                                 dev->iamthif_current_cb = NULL;
464                                 /* send flow control to iamthif client */
465                                 mei_hbm_cl_flow_control_req(dev, cl);
466                         }
467                         /* free all allocated buffers */
468                         mei_io_cb_free(cb);
469                         removed = true;
470                 }
471         }
472         return removed;
473 }
474
475 /**
476  * mei_clear_lists - removes all callbacks associated with file
477  *
478  * @dev: device structure
479  * @file: file structure
480  *
481  * mei_clear_lists is called to clear resources associated with file
482  * when application calls close function or Ctrl-C was pressed
483  *
484  * Return: true if callback removed from the list, false otherwise
485  */
486 static bool mei_clear_lists(struct mei_device *dev, const struct file *file)
487 {
488         bool removed = false;
489         struct mei_cl *cl = &dev->iamthif_cl;
490
491         /* remove callbacks associated with a file */
492         mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
493         if (mei_clear_list(dev, file, &cl->rd_completed))
494                 removed = true;
495
496         mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
497
498         if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
499                 removed = true;
500
501         if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
502                 removed = true;
503
504         if (mei_clear_list(dev, file, &dev->write_list.list))
505                 removed = true;
506
507         /* check if iamthif_current_cb not NULL */
508         if (dev->iamthif_current_cb && !removed) {
509                 /* check file and iamthif current cb association */
510                 if (dev->iamthif_current_cb->fp == file) {
511                         /* remove cb */
512                         mei_io_cb_free(dev->iamthif_current_cb);
513                         dev->iamthif_current_cb = NULL;
514                         removed = true;
515                 }
516         }
517         return removed;
518 }
519
520 /**
521 * mei_amthif_release - the release function
522 *
523 *  @dev: device structure
524 *  @file: pointer to file structure
525 *
526 *  Return: 0 on success, <0 on error
527 */
528 int mei_amthif_release(struct mei_device *dev, struct file *file)
529 {
530         if (dev->iamthif_open_count > 0)
531                 dev->iamthif_open_count--;
532
533         if (dev->iamthif_fp == file &&
534             dev->iamthif_state != MEI_IAMTHIF_IDLE) {
535
536                 dev_dbg(dev->dev, "amthif canceled iamthif state %d\n",
537                     dev->iamthif_state);
538                 dev->iamthif_canceled = true;
539                 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
540                         dev_dbg(dev->dev, "run next amthif iamthif cb\n");
541                         mei_amthif_run_next_cmd(dev);
542                 }
543         }
544
545         if (mei_clear_lists(dev, file))
546                 dev->iamthif_state = MEI_IAMTHIF_IDLE;
547
548         return 0;
549 }