Merge branch 'v3.10/topic/gator' of git://git.linaro.org/kernel/linux-linaro-stable...
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mei / bus.c
1 /*
2  * Intel Management Engine Interface (Intel MEI) Linux driver
3  * Copyright (c) 2012-2013, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/mei_cl_bus.h>
27
28 #include "mei_dev.h"
29 #include "hw-me.h"
30 #include "client.h"
31
32 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
33 #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
34
35 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
36 {
37         struct mei_cl_device *device = to_mei_cl_device(dev);
38         struct mei_cl_driver *driver = to_mei_cl_driver(drv);
39         const struct mei_cl_device_id *id;
40
41         if (!device)
42                 return 0;
43
44         if (!driver || !driver->id_table)
45                 return 0;
46
47         id = driver->id_table;
48
49         while (id->name[0]) {
50                 if (!strcmp(dev_name(dev), id->name))
51                         return 1;
52
53                 id++;
54         }
55
56         return 0;
57 }
58
59 static int mei_cl_device_probe(struct device *dev)
60 {
61         struct mei_cl_device *device = to_mei_cl_device(dev);
62         struct mei_cl_driver *driver;
63         struct mei_cl_device_id id;
64
65         if (!device)
66                 return 0;
67
68         driver = to_mei_cl_driver(dev->driver);
69         if (!driver || !driver->probe)
70                 return -ENODEV;
71
72         dev_dbg(dev, "Device probe\n");
73
74         strncpy(id.name, dev_name(dev), MEI_CL_NAME_SIZE);
75
76         return driver->probe(device, &id);
77 }
78
79 static int mei_cl_device_remove(struct device *dev)
80 {
81         struct mei_cl_device *device = to_mei_cl_device(dev);
82         struct mei_cl_driver *driver;
83
84         if (!device || !dev->driver)
85                 return 0;
86
87         if (device->event_cb) {
88                 device->event_cb = NULL;
89                 cancel_work_sync(&device->event_work);
90         }
91
92         driver = to_mei_cl_driver(dev->driver);
93         if (!driver->remove) {
94                 dev->driver = NULL;
95
96                 return 0;
97         }
98
99         return driver->remove(device);
100 }
101
102 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
103                              char *buf)
104 {
105         int len;
106
107         len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
108
109         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
110 }
111
112 static struct device_attribute mei_cl_dev_attrs[] = {
113         __ATTR_RO(modalias),
114         __ATTR_NULL,
115 };
116
117 static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
118 {
119         if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
120                 return -ENOMEM;
121
122         return 0;
123 }
124
125 static struct bus_type mei_cl_bus_type = {
126         .name           = "mei",
127         .dev_attrs      = mei_cl_dev_attrs,
128         .match          = mei_cl_device_match,
129         .probe          = mei_cl_device_probe,
130         .remove         = mei_cl_device_remove,
131         .uevent         = mei_cl_uevent,
132 };
133
134 static void mei_cl_dev_release(struct device *dev)
135 {
136         kfree(to_mei_cl_device(dev));
137 }
138
139 static struct device_type mei_cl_device_type = {
140         .release        = mei_cl_dev_release,
141 };
142
143 static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
144                                                 uuid_le uuid)
145 {
146         struct mei_cl *cl, *next;
147
148         list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
149                 if (!uuid_le_cmp(uuid, cl->device_uuid))
150                         return cl;
151         }
152
153         return NULL;
154 }
155 struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
156                                         uuid_le uuid, char *name,
157                                         struct mei_cl_ops *ops)
158 {
159         struct mei_cl_device *device;
160         struct mei_cl *cl;
161         int status;
162
163         cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
164         if (cl == NULL)
165                 return NULL;
166
167         device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
168         if (!device)
169                 return NULL;
170
171         device->cl = cl;
172         device->ops = ops;
173
174         device->dev.parent = &dev->pdev->dev;
175         device->dev.bus = &mei_cl_bus_type;
176         device->dev.type = &mei_cl_device_type;
177
178         dev_set_name(&device->dev, "%s", name);
179
180         status = device_register(&device->dev);
181         if (status) {
182                 dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
183                 kfree(device);
184                 return NULL;
185         }
186
187         cl->device = device;
188
189         dev_dbg(&device->dev, "client %s registered\n", name);
190
191         return device;
192 }
193 EXPORT_SYMBOL_GPL(mei_cl_add_device);
194
195 void mei_cl_remove_device(struct mei_cl_device *device)
196 {
197         device_unregister(&device->dev);
198 }
199 EXPORT_SYMBOL_GPL(mei_cl_remove_device);
200
201 int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
202 {
203         int err;
204
205         driver->driver.name = driver->name;
206         driver->driver.owner = owner;
207         driver->driver.bus = &mei_cl_bus_type;
208
209         err = driver_register(&driver->driver);
210         if (err)
211                 return err;
212
213         pr_debug("mei: driver [%s] registered\n", driver->driver.name);
214
215         return 0;
216 }
217 EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
218
219 void mei_cl_driver_unregister(struct mei_cl_driver *driver)
220 {
221         driver_unregister(&driver->driver);
222
223         pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
224 }
225 EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
226
227 static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
228                         bool blocking)
229 {
230         struct mei_device *dev;
231         struct mei_cl_cb *cb;
232         int id;
233         int rets;
234
235         if (WARN_ON(!cl || !cl->dev))
236                 return -ENODEV;
237
238         dev = cl->dev;
239
240         if (cl->state != MEI_FILE_CONNECTED)
241                 return -ENODEV;
242
243         /* Check if we have an ME client device */
244         id = mei_me_cl_by_id(dev, cl->me_client_id);
245         if (id < 0)
246                 return -ENODEV;
247
248         if (length > dev->me_clients[id].props.max_msg_length)
249                 return -EINVAL;
250
251         cb = mei_io_cb_init(cl, NULL);
252         if (!cb)
253                 return -ENOMEM;
254
255         rets = mei_io_cb_alloc_req_buf(cb, length);
256         if (rets < 0) {
257                 mei_io_cb_free(cb);
258                 return rets;
259         }
260
261         memcpy(cb->request_buffer.data, buf, length);
262
263         mutex_lock(&dev->device_lock);
264
265         rets = mei_cl_write(cl, cb, blocking);
266
267         mutex_unlock(&dev->device_lock);
268         if (rets < 0)
269                 mei_io_cb_free(cb);
270
271         return rets;
272 }
273
274 int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
275 {
276         struct mei_device *dev;
277         struct mei_cl_cb *cb;
278         size_t r_length;
279         int err;
280
281         if (WARN_ON(!cl || !cl->dev))
282                 return -ENODEV;
283
284         dev = cl->dev;
285
286         mutex_lock(&dev->device_lock);
287
288         if (!cl->read_cb) {
289                 err = mei_cl_read_start(cl, length);
290                 if (err < 0) {
291                         mutex_unlock(&dev->device_lock);
292                         return err;
293                 }
294         }
295
296         if (cl->reading_state != MEI_READ_COMPLETE &&
297             !waitqueue_active(&cl->rx_wait)) {
298
299                 mutex_unlock(&dev->device_lock);
300
301                 if (wait_event_interruptible(cl->rx_wait,
302                                 cl->reading_state == MEI_READ_COMPLETE  ||
303                                 mei_cl_is_transitioning(cl))) {
304
305                         if (signal_pending(current))
306                                 return -EINTR;
307                         return -ERESTARTSYS;
308                 }
309
310                 mutex_lock(&dev->device_lock);
311         }
312
313         cb = cl->read_cb;
314
315         if (cl->reading_state != MEI_READ_COMPLETE) {
316                 r_length = 0;
317                 goto out;
318         }
319
320         r_length = min_t(size_t, length, cb->buf_idx);
321
322         memcpy(buf, cb->response_buffer.data, r_length);
323
324         mei_io_cb_free(cb);
325         cl->reading_state = MEI_IDLE;
326         cl->read_cb = NULL;
327
328 out:
329         mutex_unlock(&dev->device_lock);
330
331         return r_length;
332 }
333
334 inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
335 {
336         return ___mei_cl_send(cl, buf, length, 0);
337 }
338
339 inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
340 {
341         return ___mei_cl_send(cl, buf, length, 1);
342 }
343
344 int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
345 {
346         struct mei_cl *cl = device->cl;
347
348         if (cl == NULL)
349                 return -ENODEV;
350
351         if (device->ops && device->ops->send)
352                 return device->ops->send(device, buf, length);
353
354         return __mei_cl_send(cl, buf, length);
355 }
356 EXPORT_SYMBOL_GPL(mei_cl_send);
357
358 int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
359 {
360         struct mei_cl *cl =  device->cl;
361
362         if (cl == NULL)
363                 return -ENODEV;
364
365         if (device->ops && device->ops->recv)
366                 return device->ops->recv(device, buf, length);
367
368         return __mei_cl_recv(cl, buf, length);
369 }
370 EXPORT_SYMBOL_GPL(mei_cl_recv);
371
372 static void mei_bus_event_work(struct work_struct *work)
373 {
374         struct mei_cl_device *device;
375
376         device = container_of(work, struct mei_cl_device, event_work);
377
378         if (device->event_cb)
379                 device->event_cb(device, device->events, device->event_context);
380
381         device->events = 0;
382
383         /* Prepare for the next read */
384         mei_cl_read_start(device->cl, 0);
385 }
386
387 int mei_cl_register_event_cb(struct mei_cl_device *device,
388                           mei_cl_event_cb_t event_cb, void *context)
389 {
390         if (device->event_cb)
391                 return -EALREADY;
392
393         device->events = 0;
394         device->event_cb = event_cb;
395         device->event_context = context;
396         INIT_WORK(&device->event_work, mei_bus_event_work);
397
398         mei_cl_read_start(device->cl, 0);
399
400         return 0;
401 }
402 EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
403
404 void *mei_cl_get_drvdata(const struct mei_cl_device *device)
405 {
406         return dev_get_drvdata(&device->dev);
407 }
408 EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
409
410 void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
411 {
412         dev_set_drvdata(&device->dev, data);
413 }
414 EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
415
416 int mei_cl_enable_device(struct mei_cl_device *device)
417 {
418         int err;
419         struct mei_device *dev;
420         struct mei_cl *cl = device->cl;
421
422         if (cl == NULL)
423                 return -ENODEV;
424
425         dev = cl->dev;
426
427         mutex_lock(&dev->device_lock);
428
429         cl->state = MEI_FILE_CONNECTING;
430
431         err = mei_cl_connect(cl, NULL);
432         if (err < 0) {
433                 mutex_unlock(&dev->device_lock);
434                 dev_err(&dev->pdev->dev, "Could not connect to the ME client");
435
436                 return err;
437         }
438
439         mutex_unlock(&dev->device_lock);
440
441         if (device->event_cb && !cl->read_cb)
442                 mei_cl_read_start(device->cl, 0);
443
444         if (!device->ops || !device->ops->enable)
445                 return 0;
446
447         return device->ops->enable(device);
448 }
449 EXPORT_SYMBOL_GPL(mei_cl_enable_device);
450
451 int mei_cl_disable_device(struct mei_cl_device *device)
452 {
453         int err;
454         struct mei_device *dev;
455         struct mei_cl *cl = device->cl;
456
457         if (cl == NULL)
458                 return -ENODEV;
459
460         dev = cl->dev;
461
462         mutex_lock(&dev->device_lock);
463
464         if (cl->state != MEI_FILE_CONNECTED) {
465                 mutex_unlock(&dev->device_lock);
466                 dev_err(&dev->pdev->dev, "Already disconnected");
467
468                 return 0;
469         }
470
471         cl->state = MEI_FILE_DISCONNECTING;
472
473         err = mei_cl_disconnect(cl);
474         if (err < 0) {
475                 mutex_unlock(&dev->device_lock);
476                 dev_err(&dev->pdev->dev,
477                         "Could not disconnect from the ME client");
478
479                 return err;
480         }
481
482         /* Flush queues and remove any pending read */
483         mei_cl_flush_queues(cl);
484
485         if (cl->read_cb) {
486                 struct mei_cl_cb *cb = NULL;
487
488                 cb = mei_cl_find_read_cb(cl);
489                 /* Remove entry from read list */
490                 if (cb)
491                         list_del(&cb->list);
492
493                 cb = cl->read_cb;
494                 cl->read_cb = NULL;
495
496                 if (cb) {
497                         mei_io_cb_free(cb);
498                         cb = NULL;
499                 }
500         }
501
502         device->event_cb = NULL;
503
504         mutex_unlock(&dev->device_lock);
505
506         if (!device->ops || !device->ops->disable)
507                 return 0;
508
509         return device->ops->disable(device);
510 }
511 EXPORT_SYMBOL_GPL(mei_cl_disable_device);
512
513 void mei_cl_bus_rx_event(struct mei_cl *cl)
514 {
515         struct mei_cl_device *device = cl->device;
516
517         if (!device || !device->event_cb)
518                 return;
519
520         set_bit(MEI_CL_EVENT_RX, &device->events);
521
522         schedule_work(&device->event_work);
523 }
524
525 int __init mei_cl_bus_init(void)
526 {
527         return bus_register(&mei_cl_bus_type);
528 }
529
530 void __exit mei_cl_bus_exit(void)
531 {
532         bus_unregister(&mei_cl_bus_type);
533 }