]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/misc/mei/main.c
mei: drop pr_fmt macros
[karo-tx-linux.git] / drivers / misc / mei / main.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 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/fs.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/aio.h>
25 #include <linux/pci.h>
26 #include <linux/poll.h>
27 #include <linux/init.h>
28 #include <linux/ioctl.h>
29 #include <linux/cdev.h>
30 #include <linux/sched.h>
31 #include <linux/uuid.h>
32 #include <linux/compat.h>
33 #include <linux/jiffies.h>
34 #include <linux/interrupt.h>
35 #include <linux/miscdevice.h>
36
37 #include <linux/mei.h>
38
39 #include "mei_dev.h"
40 #include "client.h"
41
42 /**
43  * mei_open - the open function
44  *
45  * @inode: pointer to inode structure
46  * @file: pointer to file structure
47  *
48  * returns 0 on success, <0 on error
49  */
50 static int mei_open(struct inode *inode, struct file *file)
51 {
52         struct miscdevice *misc = file->private_data;
53         struct pci_dev *pdev;
54         struct mei_cl *cl;
55         struct mei_device *dev;
56
57         int err;
58
59         if (!misc->parent)
60                 return -ENODEV;
61
62         pdev = container_of(misc->parent, struct pci_dev, dev);
63
64         dev = pci_get_drvdata(pdev);
65         if (!dev)
66                 return -ENODEV;
67
68         mutex_lock(&dev->device_lock);
69
70         cl = NULL;
71
72         err = -ENODEV;
73         if (dev->dev_state != MEI_DEV_ENABLED) {
74                 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED  dev_state = %s\n",
75                     mei_dev_state_str(dev->dev_state));
76                 goto err_unlock;
77         }
78
79         err = -ENOMEM;
80         cl = mei_cl_allocate(dev);
81         if (!cl)
82                 goto err_unlock;
83
84         /* open_handle_count check is handled in the mei_cl_link */
85         err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
86         if (err)
87                 goto err_unlock;
88
89         file->private_data = cl;
90
91         mutex_unlock(&dev->device_lock);
92
93         return nonseekable_open(inode, file);
94
95 err_unlock:
96         mutex_unlock(&dev->device_lock);
97         kfree(cl);
98         return err;
99 }
100
101 /**
102  * mei_release - the release function
103  *
104  * @inode: pointer to inode structure
105  * @file: pointer to file structure
106  *
107  * returns 0 on success, <0 on error
108  */
109 static int mei_release(struct inode *inode, struct file *file)
110 {
111         struct mei_cl *cl = file->private_data;
112         struct mei_cl_cb *cb;
113         struct mei_device *dev;
114         int rets = 0;
115
116         if (WARN_ON(!cl || !cl->dev))
117                 return -ENODEV;
118
119         dev = cl->dev;
120
121         mutex_lock(&dev->device_lock);
122         if (cl == &dev->iamthif_cl) {
123                 rets = mei_amthif_release(dev, file);
124                 goto out;
125         }
126         if (cl->state == MEI_FILE_CONNECTED) {
127                 cl->state = MEI_FILE_DISCONNECTING;
128                 dev_dbg(&dev->pdev->dev,
129                         "disconnecting client host client = %d, "
130                     "ME client = %d\n",
131                     cl->host_client_id,
132                     cl->me_client_id);
133                 rets = mei_cl_disconnect(cl);
134         }
135         mei_cl_flush_queues(cl);
136         dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
137             cl->host_client_id,
138             cl->me_client_id);
139
140         mei_cl_unlink(cl);
141
142
143         /* free read cb */
144         cb = NULL;
145         if (cl->read_cb) {
146                 cb = mei_cl_find_read_cb(cl);
147                 /* Remove entry from read list */
148                 if (cb)
149                         list_del(&cb->list);
150
151                 cb = cl->read_cb;
152                 cl->read_cb = NULL;
153         }
154
155         file->private_data = NULL;
156
157         mei_io_cb_free(cb);
158
159         kfree(cl);
160 out:
161         mutex_unlock(&dev->device_lock);
162         return rets;
163 }
164
165
166 /**
167  * mei_read - the read function.
168  *
169  * @file: pointer to file structure
170  * @ubuf: pointer to user buffer
171  * @length: buffer length
172  * @offset: data offset in buffer
173  *
174  * returns >=0 data length on success , <0 on error
175  */
176 static ssize_t mei_read(struct file *file, char __user *ubuf,
177                         size_t length, loff_t *offset)
178 {
179         struct mei_cl *cl = file->private_data;
180         struct mei_cl_cb *cb_pos = NULL;
181         struct mei_cl_cb *cb = NULL;
182         struct mei_device *dev;
183         int rets;
184         int err;
185
186
187         if (WARN_ON(!cl || !cl->dev))
188                 return -ENODEV;
189
190         dev = cl->dev;
191
192
193         mutex_lock(&dev->device_lock);
194         if (dev->dev_state != MEI_DEV_ENABLED) {
195                 rets = -ENODEV;
196                 goto out;
197         }
198
199         if (length == 0) {
200                 rets = 0;
201                 goto out;
202         }
203
204         if (cl == &dev->iamthif_cl) {
205                 rets = mei_amthif_read(dev, file, ubuf, length, offset);
206                 goto out;
207         }
208
209         if (cl->read_cb) {
210                 cb = cl->read_cb;
211                 /* read what left */
212                 if (cb->buf_idx > *offset)
213                         goto copy_buffer;
214                 /* offset is beyond buf_idx we have no more data return 0 */
215                 if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
216                         rets = 0;
217                         goto free;
218                 }
219                 /* Offset needs to be cleaned for contiguous reads*/
220                 if (cb->buf_idx == 0 && *offset > 0)
221                         *offset = 0;
222         } else if (*offset > 0) {
223                 *offset = 0;
224         }
225
226         err = mei_cl_read_start(cl, length);
227         if (err && err != -EBUSY) {
228                 dev_dbg(&dev->pdev->dev,
229                         "mei start read failure with status = %d\n", err);
230                 rets = err;
231                 goto out;
232         }
233
234         if (MEI_READ_COMPLETE != cl->reading_state &&
235                         !waitqueue_active(&cl->rx_wait)) {
236                 if (file->f_flags & O_NONBLOCK) {
237                         rets = -EAGAIN;
238                         goto out;
239                 }
240
241                 mutex_unlock(&dev->device_lock);
242
243                 if (wait_event_interruptible(cl->rx_wait,
244                                 MEI_READ_COMPLETE == cl->reading_state ||
245                                 mei_cl_is_transitioning(cl))) {
246
247                         if (signal_pending(current))
248                                 return -EINTR;
249                         return -ERESTARTSYS;
250                 }
251
252                 mutex_lock(&dev->device_lock);
253                 if (mei_cl_is_transitioning(cl)) {
254                         rets = -EBUSY;
255                         goto out;
256                 }
257         }
258
259         cb = cl->read_cb;
260
261         if (!cb) {
262                 rets = -ENODEV;
263                 goto out;
264         }
265         if (cl->reading_state != MEI_READ_COMPLETE) {
266                 rets = 0;
267                 goto out;
268         }
269         /* now copy the data to user space */
270 copy_buffer:
271         dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
272             cb->response_buffer.size, cb->buf_idx);
273         if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
274                 rets = -EMSGSIZE;
275                 goto free;
276         }
277
278         /* length is being truncated to PAGE_SIZE,
279          * however buf_idx may point beyond that */
280         length = min_t(size_t, length, cb->buf_idx - *offset);
281
282         if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
283                 rets = -EFAULT;
284                 goto free;
285         }
286
287         rets = length;
288         *offset += length;
289         if ((unsigned long)*offset < cb->buf_idx)
290                 goto out;
291
292 free:
293         cb_pos = mei_cl_find_read_cb(cl);
294         /* Remove entry from read list */
295         if (cb_pos)
296                 list_del(&cb_pos->list);
297         mei_io_cb_free(cb);
298         cl->reading_state = MEI_IDLE;
299         cl->read_cb = NULL;
300 out:
301         dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
302         mutex_unlock(&dev->device_lock);
303         return rets;
304 }
305 /**
306  * mei_write - the write function.
307  *
308  * @file: pointer to file structure
309  * @ubuf: pointer to user buffer
310  * @length: buffer length
311  * @offset: data offset in buffer
312  *
313  * returns >=0 data length on success , <0 on error
314  */
315 static ssize_t mei_write(struct file *file, const char __user *ubuf,
316                          size_t length, loff_t *offset)
317 {
318         struct mei_cl *cl = file->private_data;
319         struct mei_cl_cb *write_cb = NULL;
320         struct mei_device *dev;
321         unsigned long timeout = 0;
322         int rets;
323         int id;
324
325         if (WARN_ON(!cl || !cl->dev))
326                 return -ENODEV;
327
328         dev = cl->dev;
329
330         mutex_lock(&dev->device_lock);
331
332         if (dev->dev_state != MEI_DEV_ENABLED) {
333                 rets = -ENODEV;
334                 goto out;
335         }
336
337         id = mei_me_cl_by_id(dev, cl->me_client_id);
338         if (id < 0) {
339                 rets = -ENOTTY;
340                 goto out;
341         }
342
343         if (length == 0) {
344                 rets = 0;
345                 goto out;
346         }
347
348         if (length > dev->me_clients[id].props.max_msg_length) {
349                 rets = -EFBIG;
350                 goto out;
351         }
352
353         if (cl->state != MEI_FILE_CONNECTED) {
354                 dev_err(&dev->pdev->dev, "host client = %d,  is not connected to ME client = %d",
355                         cl->host_client_id, cl->me_client_id);
356                 rets = -ENODEV;
357                 goto out;
358         }
359         if (cl == &dev->iamthif_cl) {
360                 write_cb = mei_amthif_find_read_list_entry(dev, file);
361
362                 if (write_cb) {
363                         timeout = write_cb->read_time +
364                                 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
365
366                         if (time_after(jiffies, timeout) ||
367                             cl->reading_state == MEI_READ_COMPLETE) {
368                                 *offset = 0;
369                                 list_del(&write_cb->list);
370                                 mei_io_cb_free(write_cb);
371                                 write_cb = NULL;
372                         }
373                 }
374         }
375
376         /* free entry used in read */
377         if (cl->reading_state == MEI_READ_COMPLETE) {
378                 *offset = 0;
379                 write_cb = mei_cl_find_read_cb(cl);
380                 if (write_cb) {
381                         list_del(&write_cb->list);
382                         mei_io_cb_free(write_cb);
383                         write_cb = NULL;
384                         cl->reading_state = MEI_IDLE;
385                         cl->read_cb = NULL;
386                 }
387         } else if (cl->reading_state == MEI_IDLE)
388                 *offset = 0;
389
390
391         write_cb = mei_io_cb_init(cl, file);
392         if (!write_cb) {
393                 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
394                 rets = -ENOMEM;
395                 goto out;
396         }
397         rets = mei_io_cb_alloc_req_buf(write_cb, length);
398         if (rets)
399                 goto out;
400
401         rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
402         if (rets) {
403                 dev_err(&dev->pdev->dev, "failed to copy data from userland\n");
404                 rets = -EFAULT;
405                 goto out;
406         }
407
408         if (cl == &dev->iamthif_cl) {
409                 rets = mei_amthif_write(dev, write_cb);
410
411                 if (rets) {
412                         dev_err(&dev->pdev->dev,
413                                 "amthif write failed with status = %d\n", rets);
414                         goto out;
415                 }
416                 mutex_unlock(&dev->device_lock);
417                 return length;
418         }
419
420         rets = mei_cl_write(cl, write_cb, false);
421 out:
422         mutex_unlock(&dev->device_lock);
423         if (rets < 0)
424                 mei_io_cb_free(write_cb);
425         return rets;
426 }
427
428 /**
429  * mei_ioctl_connect_client - the connect to fw client IOCTL function
430  *
431  * @dev: the device structure
432  * @data: IOCTL connect data, input and output parameters
433  * @file: private data of the file object
434  *
435  * Locking: called under "dev->device_lock" lock
436  *
437  * returns 0 on success, <0 on failure.
438  */
439 static int mei_ioctl_connect_client(struct file *file,
440                         struct mei_connect_client_data *data)
441 {
442         struct mei_device *dev;
443         struct mei_client *client;
444         struct mei_cl *cl;
445         int i;
446         int rets;
447
448         cl = file->private_data;
449         if (WARN_ON(!cl || !cl->dev))
450                 return -ENODEV;
451
452         dev = cl->dev;
453
454         if (dev->dev_state != MEI_DEV_ENABLED) {
455                 rets = -ENODEV;
456                 goto end;
457         }
458
459         if (cl->state != MEI_FILE_INITIALIZING &&
460             cl->state != MEI_FILE_DISCONNECTED) {
461                 rets = -EBUSY;
462                 goto end;
463         }
464
465         /* find ME client we're trying to connect to */
466         i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
467         if (i < 0 || dev->me_clients[i].props.fixed_address) {
468                 dev_dbg(&dev->pdev->dev, "Cannot connect to FW Client UUID = %pUl\n",
469                                 &data->in_client_uuid);
470                 rets = -ENOTTY;
471                 goto end;
472         }
473
474         cl->me_client_id = dev->me_clients[i].client_id;
475         cl->state = MEI_FILE_CONNECTING;
476
477         dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
478                         cl->me_client_id);
479         dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
480                         dev->me_clients[i].props.protocol_version);
481         dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
482                         dev->me_clients[i].props.max_msg_length);
483
484         /* if we're connecting to amthif client then we will use the
485          * existing connection
486          */
487         if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
488                 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
489                 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
490                         rets = -ENODEV;
491                         goto end;
492                 }
493                 mei_cl_unlink(cl);
494
495                 kfree(cl);
496                 cl = NULL;
497                 dev->iamthif_open_count++;
498                 file->private_data = &dev->iamthif_cl;
499
500                 client = &data->out_client_properties;
501                 client->max_msg_length =
502                         dev->me_clients[i].props.max_msg_length;
503                 client->protocol_version =
504                         dev->me_clients[i].props.protocol_version;
505                 rets = dev->iamthif_cl.status;
506
507                 goto end;
508         }
509
510
511         /* prepare the output buffer */
512         client = &data->out_client_properties;
513         client->max_msg_length = dev->me_clients[i].props.max_msg_length;
514         client->protocol_version = dev->me_clients[i].props.protocol_version;
515         dev_dbg(&dev->pdev->dev, "Can connect?\n");
516
517
518         rets = mei_cl_connect(cl, file);
519
520 end:
521         return rets;
522 }
523
524
525 /**
526  * mei_ioctl - the IOCTL function
527  *
528  * @file: pointer to file structure
529  * @cmd: ioctl command
530  * @data: pointer to mei message structure
531  *
532  * returns 0 on success , <0 on error
533  */
534 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
535 {
536         struct mei_device *dev;
537         struct mei_cl *cl = file->private_data;
538         struct mei_connect_client_data *connect_data = NULL;
539         int rets;
540
541         if (cmd != IOCTL_MEI_CONNECT_CLIENT)
542                 return -EINVAL;
543
544         if (WARN_ON(!cl || !cl->dev))
545                 return -ENODEV;
546
547         dev = cl->dev;
548
549         dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
550
551         mutex_lock(&dev->device_lock);
552         if (dev->dev_state != MEI_DEV_ENABLED) {
553                 rets = -ENODEV;
554                 goto out;
555         }
556
557         dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
558
559         connect_data = kzalloc(sizeof(struct mei_connect_client_data),
560                                                         GFP_KERNEL);
561         if (!connect_data) {
562                 rets = -ENOMEM;
563                 goto out;
564         }
565         dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
566         if (copy_from_user(connect_data, (char __user *)data,
567                                 sizeof(struct mei_connect_client_data))) {
568                 dev_err(&dev->pdev->dev, "failed to copy data from userland\n");
569                 rets = -EFAULT;
570                 goto out;
571         }
572
573         rets = mei_ioctl_connect_client(file, connect_data);
574
575         /* if all is ok, copying the data back to user. */
576         if (rets)
577                 goto out;
578
579         dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
580         if (copy_to_user((char __user *)data, connect_data,
581                                 sizeof(struct mei_connect_client_data))) {
582                 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
583                 rets = -EFAULT;
584                 goto out;
585         }
586
587 out:
588         kfree(connect_data);
589         mutex_unlock(&dev->device_lock);
590         return rets;
591 }
592
593 /**
594  * mei_compat_ioctl - the compat IOCTL function
595  *
596  * @file: pointer to file structure
597  * @cmd: ioctl command
598  * @data: pointer to mei message structure
599  *
600  * returns 0 on success , <0 on error
601  */
602 #ifdef CONFIG_COMPAT
603 static long mei_compat_ioctl(struct file *file,
604                         unsigned int cmd, unsigned long data)
605 {
606         return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
607 }
608 #endif
609
610
611 /**
612  * mei_poll - the poll function
613  *
614  * @file: pointer to file structure
615  * @wait: pointer to poll_table structure
616  *
617  * returns poll mask
618  */
619 static unsigned int mei_poll(struct file *file, poll_table *wait)
620 {
621         struct mei_cl *cl = file->private_data;
622         struct mei_device *dev;
623         unsigned int mask = 0;
624
625         if (WARN_ON(!cl || !cl->dev))
626                 return POLLERR;
627
628         dev = cl->dev;
629
630         mutex_lock(&dev->device_lock);
631
632         if (!mei_cl_is_connected(cl)) {
633                 mask = POLLERR;
634                 goto out;
635         }
636
637         mutex_unlock(&dev->device_lock);
638
639
640         if (cl == &dev->iamthif_cl)
641                 return mei_amthif_poll(dev, file, wait);
642
643         poll_wait(file, &cl->tx_wait, wait);
644
645         mutex_lock(&dev->device_lock);
646
647         if (!mei_cl_is_connected(cl)) {
648                 mask = POLLERR;
649                 goto out;
650         }
651
652         if (MEI_WRITE_COMPLETE == cl->writing_state)
653                 mask |= (POLLIN | POLLRDNORM);
654
655 out:
656         mutex_unlock(&dev->device_lock);
657         return mask;
658 }
659
660 /*
661  * file operations structure will be used for mei char device.
662  */
663 static const struct file_operations mei_fops = {
664         .owner = THIS_MODULE,
665         .read = mei_read,
666         .unlocked_ioctl = mei_ioctl,
667 #ifdef CONFIG_COMPAT
668         .compat_ioctl = mei_compat_ioctl,
669 #endif
670         .open = mei_open,
671         .release = mei_release,
672         .write = mei_write,
673         .poll = mei_poll,
674         .llseek = no_llseek
675 };
676
677 /*
678  * Misc Device Struct
679  */
680 static struct miscdevice  mei_misc_device = {
681                 .name = "mei",
682                 .fops = &mei_fops,
683                 .minor = MISC_DYNAMIC_MINOR,
684 };
685
686
687 int mei_register(struct mei_device *dev)
688 {
689         int ret;
690         mei_misc_device.parent = &dev->pdev->dev;
691         ret = misc_register(&mei_misc_device);
692         if (ret)
693                 return ret;
694
695         if (mei_dbgfs_register(dev, mei_misc_device.name))
696                 dev_err(&dev->pdev->dev, "cannot register debugfs\n");
697
698         return 0;
699 }
700 EXPORT_SYMBOL_GPL(mei_register);
701
702 void mei_deregister(struct mei_device *dev)
703 {
704         mei_dbgfs_deregister(dev);
705         misc_deregister(&mei_misc_device);
706         mei_misc_device.parent = NULL;
707 }
708 EXPORT_SYMBOL_GPL(mei_deregister);
709
710 static int __init mei_init(void)
711 {
712         return mei_cl_bus_init();
713 }
714
715 static void __exit mei_exit(void)
716 {
717         mei_cl_bus_exit();
718 }
719
720 module_init(mei_init);
721 module_exit(mei_exit);
722
723 MODULE_AUTHOR("Intel Corporation");
724 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
725 MODULE_LICENSE("GPL v2");
726