blob: 1cf3a72f295493d689ff44a8d4363ccf7b65be9a [file] [log] [blame]
Samuel Ortize5354102013-03-27 17:29:53 +02001/*
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>
Samuel Ortiz3e833292013-03-27 17:29:55 +020019#include <linux/sched.h>
Samuel Ortize5354102013-03-27 17:29:53 +020020#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"
Samuel Ortiz3e833292013-03-27 17:29:55 +020029#include "client.h"
Samuel Ortize5354102013-03-27 17:29:53 +020030
31#define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
32#define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
33
34static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
35{
36 struct mei_cl_device *device = to_mei_cl_device(dev);
37 struct mei_cl_driver *driver = to_mei_cl_driver(drv);
38 const struct mei_cl_device_id *id;
39
40 if (!device)
41 return 0;
42
43 if (!driver || !driver->id_table)
44 return 0;
45
46 id = driver->id_table;
47
48 while (id->name[0]) {
Tomas Winkler8b613bb2013-07-24 16:22:59 +030049 if (!strncmp(dev_name(dev), id->name, sizeof(id->name)))
Samuel Ortize5354102013-03-27 17:29:53 +020050 return 1;
51
52 id++;
53 }
54
55 return 0;
56}
57
58static int mei_cl_device_probe(struct device *dev)
59{
60 struct mei_cl_device *device = to_mei_cl_device(dev);
61 struct mei_cl_driver *driver;
62 struct mei_cl_device_id id;
63
64 if (!device)
65 return 0;
66
67 driver = to_mei_cl_driver(dev->driver);
68 if (!driver || !driver->probe)
69 return -ENODEV;
70
71 dev_dbg(dev, "Device probe\n");
72
Alexander Usyskincfda2792014-08-25 16:46:53 +030073 strlcpy(id.name, dev_name(dev), sizeof(id.name));
Samuel Ortize5354102013-03-27 17:29:53 +020074
75 return driver->probe(device, &id);
76}
77
78static int mei_cl_device_remove(struct device *dev)
79{
80 struct mei_cl_device *device = to_mei_cl_device(dev);
81 struct mei_cl_driver *driver;
82
83 if (!device || !dev->driver)
84 return 0;
85
Samuel Ortiz3e833292013-03-27 17:29:55 +020086 if (device->event_cb) {
87 device->event_cb = NULL;
88 cancel_work_sync(&device->event_work);
89 }
90
Samuel Ortize5354102013-03-27 17:29:53 +020091 driver = to_mei_cl_driver(dev->driver);
92 if (!driver->remove) {
93 dev->driver = NULL;
94
95 return 0;
96 }
97
98 return driver->remove(device);
99}
100
101static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
102 char *buf)
103{
104 int len;
105
106 len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
107
108 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
109}
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700110static DEVICE_ATTR_RO(modalias);
Samuel Ortize5354102013-03-27 17:29:53 +0200111
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700112static struct attribute *mei_cl_dev_attrs[] = {
113 &dev_attr_modalias.attr,
114 NULL,
Samuel Ortize5354102013-03-27 17:29:53 +0200115};
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700116ATTRIBUTE_GROUPS(mei_cl_dev);
Samuel Ortize5354102013-03-27 17:29:53 +0200117
118static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
119{
120 if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
121 return -ENOMEM;
122
123 return 0;
124}
125
126static struct bus_type mei_cl_bus_type = {
127 .name = "mei",
Greg Kroah-Hartman32f389ec2013-08-23 14:24:39 -0700128 .dev_groups = mei_cl_dev_groups,
Samuel Ortize5354102013-03-27 17:29:53 +0200129 .match = mei_cl_device_match,
130 .probe = mei_cl_device_probe,
131 .remove = mei_cl_device_remove,
132 .uevent = mei_cl_uevent,
133};
134
135static void mei_cl_dev_release(struct device *dev)
136{
137 kfree(to_mei_cl_device(dev));
138}
139
140static struct device_type mei_cl_device_type = {
141 .release = mei_cl_dev_release,
142};
143
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200144static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
145 uuid_le uuid)
146{
Tomas Winkler31f88f52014-02-17 15:13:25 +0200147 struct mei_cl *cl;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200148
Tomas Winkler31f88f52014-02-17 15:13:25 +0200149 list_for_each_entry(cl, &dev->device_list, device_link) {
Tomas Winklerd880f322014-08-21 14:29:15 +0300150 if (!uuid_le_cmp(uuid, cl->cl_uuid))
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200151 return cl;
152 }
153
154 return NULL;
155}
156struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
Samuel Ortize46980a2013-04-09 01:51:38 +0300157 uuid_le uuid, char *name,
158 struct mei_cl_ops *ops)
Samuel Ortize5354102013-03-27 17:29:53 +0200159{
160 struct mei_cl_device *device;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200161 struct mei_cl *cl;
Samuel Ortize5354102013-03-27 17:29:53 +0200162 int status;
163
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200164 cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
165 if (cl == NULL)
166 return NULL;
167
Samuel Ortize5354102013-03-27 17:29:53 +0200168 device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
169 if (!device)
170 return NULL;
171
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200172 device->cl = cl;
Samuel Ortize46980a2013-04-09 01:51:38 +0300173 device->ops = ops;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200174
175 device->dev.parent = &dev->pdev->dev;
Samuel Ortize5354102013-03-27 17:29:53 +0200176 device->dev.bus = &mei_cl_bus_type;
177 device->dev.type = &mei_cl_device_type;
178
179 dev_set_name(&device->dev, "%s", name);
180
181 status = device_register(&device->dev);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200182 if (status) {
183 dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
184 kfree(device);
185 return NULL;
186 }
187
188 cl->device = device;
Samuel Ortize5354102013-03-27 17:29:53 +0200189
190 dev_dbg(&device->dev, "client %s registered\n", name);
191
192 return device;
Samuel Ortize5354102013-03-27 17:29:53 +0200193}
194EXPORT_SYMBOL_GPL(mei_cl_add_device);
195
196void mei_cl_remove_device(struct mei_cl_device *device)
197{
198 device_unregister(&device->dev);
199}
200EXPORT_SYMBOL_GPL(mei_cl_remove_device);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200201
202int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
203{
204 int err;
205
206 driver->driver.name = driver->name;
207 driver->driver.owner = owner;
208 driver->driver.bus = &mei_cl_bus_type;
209
210 err = driver_register(&driver->driver);
211 if (err)
212 return err;
213
214 pr_debug("mei: driver [%s] registered\n", driver->driver.name);
215
216 return 0;
217}
218EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
219
220void mei_cl_driver_unregister(struct mei_cl_driver *driver)
221{
222 driver_unregister(&driver->driver);
223
224 pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
225}
226EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200227
Samuel Ortiz44d88d92013-03-27 17:29:58 +0200228static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
229 bool blocking)
Samuel Ortiz3e833292013-03-27 17:29:55 +0200230{
231 struct mei_device *dev;
Tomas Winklerd3208322014-08-24 12:08:55 +0300232 struct mei_me_client *me_cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200233 struct mei_cl_cb *cb;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300234 int rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200235
236 if (WARN_ON(!cl || !cl->dev))
237 return -ENODEV;
238
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300239 dev = cl->dev;
240
Samuel Ortiz3e833292013-03-27 17:29:55 +0200241 if (cl->state != MEI_FILE_CONNECTED)
242 return -ENODEV;
243
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300244 /* Check if we have an ME client device */
Tomas Winklerd880f322014-08-21 14:29:15 +0300245 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
Tomas Winklerd3208322014-08-24 12:08:55 +0300246 if (!me_cl)
247 return -ENOTTY;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300248
Tomas Winklerd3208322014-08-24 12:08:55 +0300249 if (length > me_cl->props.max_msg_length)
Alexander Usyskin86113502014-03-31 17:59:24 +0300250 return -EFBIG;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300251
Samuel Ortiz3e833292013-03-27 17:29:55 +0200252 cb = mei_io_cb_init(cl, NULL);
253 if (!cb)
254 return -ENOMEM;
255
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300256 rets = mei_io_cb_alloc_req_buf(cb, length);
257 if (rets < 0) {
Samuel Ortiz3e833292013-03-27 17:29:55 +0200258 mei_io_cb_free(cb);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300259 return rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200260 }
261
262 memcpy(cb->request_buffer.data, buf, length);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200263
264 mutex_lock(&dev->device_lock);
265
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300266 rets = mei_cl_write(cl, cb, blocking);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200267
268 mutex_unlock(&dev->device_lock);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300269 if (rets < 0)
270 mei_io_cb_free(cb);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200271
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300272 return rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200273}
274
275int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
276{
277 struct mei_device *dev;
278 struct mei_cl_cb *cb;
279 size_t r_length;
280 int err;
281
282 if (WARN_ON(!cl || !cl->dev))
283 return -ENODEV;
284
285 dev = cl->dev;
286
287 mutex_lock(&dev->device_lock);
288
289 if (!cl->read_cb) {
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300290 err = mei_cl_read_start(cl, length);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200291 if (err < 0) {
292 mutex_unlock(&dev->device_lock);
293 return err;
294 }
295 }
296
297 if (cl->reading_state != MEI_READ_COMPLETE &&
298 !waitqueue_active(&cl->rx_wait)) {
Tomas Winklere2b31642013-09-02 13:29:46 +0300299
Samuel Ortiz3e833292013-03-27 17:29:55 +0200300 mutex_unlock(&dev->device_lock);
301
302 if (wait_event_interruptible(cl->rx_wait,
Tomas Winklere2b31642013-09-02 13:29:46 +0300303 cl->reading_state == MEI_READ_COMPLETE ||
304 mei_cl_is_transitioning(cl))) {
305
Samuel Ortiz3e833292013-03-27 17:29:55 +0200306 if (signal_pending(current))
307 return -EINTR;
308 return -ERESTARTSYS;
309 }
310
311 mutex_lock(&dev->device_lock);
312 }
313
314 cb = cl->read_cb;
315
316 if (cl->reading_state != MEI_READ_COMPLETE) {
317 r_length = 0;
318 goto out;
319 }
320
321 r_length = min_t(size_t, length, cb->buf_idx);
322
323 memcpy(buf, cb->response_buffer.data, r_length);
324
325 mei_io_cb_free(cb);
326 cl->reading_state = MEI_IDLE;
327 cl->read_cb = NULL;
328
329out:
330 mutex_unlock(&dev->device_lock);
331
332 return r_length;
333}
334
Samuel Ortiz44d88d92013-03-27 17:29:58 +0200335inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
336{
337 return ___mei_cl_send(cl, buf, length, 0);
338}
339
340inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
341{
342 return ___mei_cl_send(cl, buf, length, 1);
343}
344
Samuel Ortiz3e833292013-03-27 17:29:55 +0200345int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
346{
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200347 struct mei_cl *cl = device->cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200348
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200349 if (cl == NULL)
350 return -ENODEV;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200351
352 if (device->ops && device->ops->send)
353 return device->ops->send(device, buf, length);
354
355 return __mei_cl_send(cl, buf, length);
356}
357EXPORT_SYMBOL_GPL(mei_cl_send);
358
359int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
360{
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200361 struct mei_cl *cl = device->cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200362
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200363 if (cl == NULL)
364 return -ENODEV;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200365
366 if (device->ops && device->ops->recv)
367 return device->ops->recv(device, buf, length);
368
369 return __mei_cl_recv(cl, buf, length);
370}
371EXPORT_SYMBOL_GPL(mei_cl_recv);
372
373static void mei_bus_event_work(struct work_struct *work)
374{
375 struct mei_cl_device *device;
376
377 device = container_of(work, struct mei_cl_device, event_work);
378
379 if (device->event_cb)
380 device->event_cb(device, device->events, device->event_context);
381
382 device->events = 0;
383
384 /* Prepare for the next read */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300385 mei_cl_read_start(device->cl, 0);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200386}
387
388int mei_cl_register_event_cb(struct mei_cl_device *device,
389 mei_cl_event_cb_t event_cb, void *context)
390{
391 if (device->event_cb)
392 return -EALREADY;
393
394 device->events = 0;
395 device->event_cb = event_cb;
396 device->event_context = context;
397 INIT_WORK(&device->event_work, mei_bus_event_work);
398
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300399 mei_cl_read_start(device->cl, 0);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200400
401 return 0;
402}
403EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200404
Samuel Ortizaa6aef22013-03-27 17:29:59 +0200405void *mei_cl_get_drvdata(const struct mei_cl_device *device)
406{
407 return dev_get_drvdata(&device->dev);
408}
409EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
410
411void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
412{
413 dev_set_drvdata(&device->dev, data);
414}
415EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
416
Samuel Ortize46980a2013-04-09 01:51:38 +0300417int mei_cl_enable_device(struct mei_cl_device *device)
418{
419 int err;
420 struct mei_device *dev;
421 struct mei_cl *cl = device->cl;
422
423 if (cl == NULL)
424 return -ENODEV;
425
426 dev = cl->dev;
427
428 mutex_lock(&dev->device_lock);
429
Samuel Ortize46980a2013-04-09 01:51:38 +0300430 err = mei_cl_connect(cl, NULL);
431 if (err < 0) {
432 mutex_unlock(&dev->device_lock);
433 dev_err(&dev->pdev->dev, "Could not connect to the ME client");
434
435 return err;
436 }
437
438 mutex_unlock(&dev->device_lock);
439
440 if (device->event_cb && !cl->read_cb)
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300441 mei_cl_read_start(device->cl, 0);
Samuel Ortize46980a2013-04-09 01:51:38 +0300442
443 if (!device->ops || !device->ops->enable)
444 return 0;
445
446 return device->ops->enable(device);
447}
448EXPORT_SYMBOL_GPL(mei_cl_enable_device);
449
450int mei_cl_disable_device(struct mei_cl_device *device)
451{
452 int err;
453 struct mei_device *dev;
454 struct mei_cl *cl = device->cl;
455
456 if (cl == NULL)
457 return -ENODEV;
458
459 dev = cl->dev;
460
461 mutex_lock(&dev->device_lock);
462
463 if (cl->state != MEI_FILE_CONNECTED) {
464 mutex_unlock(&dev->device_lock);
465 dev_err(&dev->pdev->dev, "Already disconnected");
466
467 return 0;
468 }
469
470 cl->state = MEI_FILE_DISCONNECTING;
471
472 err = mei_cl_disconnect(cl);
473 if (err < 0) {
474 mutex_unlock(&dev->device_lock);
475 dev_err(&dev->pdev->dev,
476 "Could not disconnect from the ME client");
477
478 return err;
479 }
480
481 /* Flush queues and remove any pending read */
482 mei_cl_flush_queues(cl);
483
484 if (cl->read_cb) {
485 struct mei_cl_cb *cb = NULL;
486
487 cb = mei_cl_find_read_cb(cl);
488 /* Remove entry from read list */
489 if (cb)
490 list_del(&cb->list);
491
492 cb = cl->read_cb;
493 cl->read_cb = NULL;
494
495 if (cb) {
496 mei_io_cb_free(cb);
497 cb = NULL;
498 }
499 }
500
Samuel Ortizbbedf2f2013-05-21 18:52:09 +0200501 device->event_cb = NULL;
502
Samuel Ortize46980a2013-04-09 01:51:38 +0300503 mutex_unlock(&dev->device_lock);
504
505 if (!device->ops || !device->ops->disable)
506 return 0;
507
508 return device->ops->disable(device);
509}
510EXPORT_SYMBOL_GPL(mei_cl_disable_device);
511
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200512void mei_cl_bus_rx_event(struct mei_cl *cl)
513{
514 struct mei_cl_device *device = cl->device;
515
516 if (!device || !device->event_cb)
517 return;
518
519 set_bit(MEI_CL_EVENT_RX, &device->events);
520
521 schedule_work(&device->event_work);
522}
523
Tomas Winkler48705692014-02-17 15:13:19 +0200524void mei_cl_bus_remove_devices(struct mei_device *dev)
525{
526 struct mei_cl *cl, *next;
527
528 mutex_lock(&dev->device_lock);
529 list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
530 if (cl->device)
531 mei_cl_remove_device(cl->device);
532
533 list_del(&cl->device_link);
534 mei_cl_unlink(cl);
535 kfree(cl);
536 }
537 mutex_unlock(&dev->device_lock);
538}
539
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200540int __init mei_cl_bus_init(void)
541{
542 return bus_register(&mei_cl_bus_type);
543}
544
545void __exit mei_cl_bus_exit(void)
546{
547 bus_unregister(&mei_cl_bus_type);
548}