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