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