blob: 2ce92a6a9b54a205ac1bbeae94888aef542e44a5 [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 "hw-me.h"
30#include "client.h"
Samuel Ortize5354102013-03-27 17:29:53 +020031
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
35static 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
59static 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
79static 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
Samuel Ortiz3e833292013-03-27 17:29:55 +020087 if (device->event_cb) {
88 device->event_cb = NULL;
89 cancel_work_sync(&device->event_work);
90 }
91
Samuel Ortize5354102013-03-27 17:29:53 +020092 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
102static 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}
Greg Kroah-Hartman32f389e2013-08-23 14:24:39 -0700111static DEVICE_ATTR_RO(modalias);
Samuel Ortize5354102013-03-27 17:29:53 +0200112
Greg Kroah-Hartman32f389e2013-08-23 14:24:39 -0700113static struct attribute *mei_cl_dev_attrs[] = {
114 &dev_attr_modalias.attr,
115 NULL,
Samuel Ortize5354102013-03-27 17:29:53 +0200116};
Greg Kroah-Hartman32f389e2013-08-23 14:24:39 -0700117ATTRIBUTE_GROUPS(mei_cl_dev);
Samuel Ortize5354102013-03-27 17:29:53 +0200118
119static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
120{
121 if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
122 return -ENOMEM;
123
124 return 0;
125}
126
127static struct bus_type mei_cl_bus_type = {
128 .name = "mei",
Greg Kroah-Hartman32f389e2013-08-23 14:24:39 -0700129 .dev_groups = mei_cl_dev_groups,
Samuel Ortize5354102013-03-27 17:29:53 +0200130 .match = mei_cl_device_match,
131 .probe = mei_cl_device_probe,
132 .remove = mei_cl_device_remove,
133 .uevent = mei_cl_uevent,
134};
135
136static void mei_cl_dev_release(struct device *dev)
137{
138 kfree(to_mei_cl_device(dev));
139}
140
141static struct device_type mei_cl_device_type = {
142 .release = mei_cl_dev_release,
143};
144
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200145static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
146 uuid_le uuid)
147{
148 struct mei_cl *cl, *next;
149
150 list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
151 if (!uuid_le_cmp(uuid, cl->device_uuid))
152 return cl;
153 }
154
155 return NULL;
156}
157struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
Samuel Ortize46980a2013-04-09 01:51:38 +0300158 uuid_le uuid, char *name,
159 struct mei_cl_ops *ops)
Samuel Ortize5354102013-03-27 17:29:53 +0200160{
161 struct mei_cl_device *device;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200162 struct mei_cl *cl;
Samuel Ortize5354102013-03-27 17:29:53 +0200163 int status;
164
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200165 cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
166 if (cl == NULL)
167 return NULL;
168
Samuel Ortize5354102013-03-27 17:29:53 +0200169 device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
170 if (!device)
171 return NULL;
172
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200173 device->cl = cl;
Samuel Ortize46980a2013-04-09 01:51:38 +0300174 device->ops = ops;
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200175
176 device->dev.parent = &dev->pdev->dev;
Samuel Ortize5354102013-03-27 17:29:53 +0200177 device->dev.bus = &mei_cl_bus_type;
178 device->dev.type = &mei_cl_device_type;
179
180 dev_set_name(&device->dev, "%s", name);
181
182 status = device_register(&device->dev);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200183 if (status) {
184 dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
185 kfree(device);
186 return NULL;
187 }
188
189 cl->device = device;
Samuel Ortize5354102013-03-27 17:29:53 +0200190
191 dev_dbg(&device->dev, "client %s registered\n", name);
192
193 return device;
Samuel Ortize5354102013-03-27 17:29:53 +0200194}
195EXPORT_SYMBOL_GPL(mei_cl_add_device);
196
197void mei_cl_remove_device(struct mei_cl_device *device)
198{
199 device_unregister(&device->dev);
200}
201EXPORT_SYMBOL_GPL(mei_cl_remove_device);
Samuel Ortiz333e4ee2013-03-27 17:29:54 +0200202
203int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
204{
205 int err;
206
207 driver->driver.name = driver->name;
208 driver->driver.owner = owner;
209 driver->driver.bus = &mei_cl_bus_type;
210
211 err = driver_register(&driver->driver);
212 if (err)
213 return err;
214
215 pr_debug("mei: driver [%s] registered\n", driver->driver.name);
216
217 return 0;
218}
219EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
220
221void mei_cl_driver_unregister(struct mei_cl_driver *driver)
222{
223 driver_unregister(&driver->driver);
224
225 pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
226}
227EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200228
Samuel Ortiz44d88d92013-03-27 17:29:58 +0200229static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
230 bool blocking)
Samuel Ortiz3e833292013-03-27 17:29:55 +0200231{
232 struct mei_device *dev;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200233 struct mei_cl_cb *cb;
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300234 int id;
235 int rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200236
237 if (WARN_ON(!cl || !cl->dev))
238 return -ENODEV;
239
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300240 dev = cl->dev;
241
Samuel Ortiz3e833292013-03-27 17:29:55 +0200242 if (cl->state != MEI_FILE_CONNECTED)
243 return -ENODEV;
244
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300245 /* Check if we have an ME client device */
246 id = mei_me_cl_by_id(dev, cl->me_client_id);
247 if (id < 0)
248 return -ENODEV;
249
250 if (length > dev->me_clients[id].props.max_msg_length)
251 return -EINVAL;
252
Samuel Ortiz3e833292013-03-27 17:29:55 +0200253 cb = mei_io_cb_init(cl, NULL);
254 if (!cb)
255 return -ENOMEM;
256
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300257 rets = mei_io_cb_alloc_req_buf(cb, length);
258 if (rets < 0) {
Samuel Ortiz3e833292013-03-27 17:29:55 +0200259 mei_io_cb_free(cb);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300260 return rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200261 }
262
263 memcpy(cb->request_buffer.data, buf, length);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200264
265 mutex_lock(&dev->device_lock);
266
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300267 rets = mei_cl_write(cl, cb, blocking);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200268
269 mutex_unlock(&dev->device_lock);
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300270 if (rets < 0)
271 mei_io_cb_free(cb);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200272
Tomas Winkler4234a6d2013-04-08 21:56:37 +0300273 return rets;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200274}
275
276int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
277{
278 struct mei_device *dev;
279 struct mei_cl_cb *cb;
280 size_t r_length;
281 int err;
282
283 if (WARN_ON(!cl || !cl->dev))
284 return -ENODEV;
285
286 dev = cl->dev;
287
288 mutex_lock(&dev->device_lock);
289
290 if (!cl->read_cb) {
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300291 err = mei_cl_read_start(cl, length);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200292 if (err < 0) {
293 mutex_unlock(&dev->device_lock);
294 return err;
295 }
296 }
297
298 if (cl->reading_state != MEI_READ_COMPLETE &&
299 !waitqueue_active(&cl->rx_wait)) {
300 mutex_unlock(&dev->device_lock);
301
302 if (wait_event_interruptible(cl->rx_wait,
303 (MEI_READ_COMPLETE == cl->reading_state))) {
304 if (signal_pending(current))
305 return -EINTR;
306 return -ERESTARTSYS;
307 }
308
309 mutex_lock(&dev->device_lock);
310 }
311
312 cb = cl->read_cb;
313
314 if (cl->reading_state != MEI_READ_COMPLETE) {
315 r_length = 0;
316 goto out;
317 }
318
319 r_length = min_t(size_t, length, cb->buf_idx);
320
321 memcpy(buf, cb->response_buffer.data, r_length);
322
323 mei_io_cb_free(cb);
324 cl->reading_state = MEI_IDLE;
325 cl->read_cb = NULL;
326
327out:
328 mutex_unlock(&dev->device_lock);
329
330 return r_length;
331}
332
Samuel Ortiz44d88d92013-03-27 17:29:58 +0200333inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
334{
335 return ___mei_cl_send(cl, buf, length, 0);
336}
337
338inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
339{
340 return ___mei_cl_send(cl, buf, length, 1);
341}
342
Samuel Ortiz3e833292013-03-27 17:29:55 +0200343int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
344{
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200345 struct mei_cl *cl = device->cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200346
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200347 if (cl == NULL)
348 return -ENODEV;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200349
350 if (device->ops && device->ops->send)
351 return device->ops->send(device, buf, length);
352
353 return __mei_cl_send(cl, buf, length);
354}
355EXPORT_SYMBOL_GPL(mei_cl_send);
356
357int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
358{
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200359 struct mei_cl *cl = device->cl;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200360
Samuel Ortiza7b71bc2013-03-27 17:29:56 +0200361 if (cl == NULL)
362 return -ENODEV;
Samuel Ortiz3e833292013-03-27 17:29:55 +0200363
364 if (device->ops && device->ops->recv)
365 return device->ops->recv(device, buf, length);
366
367 return __mei_cl_recv(cl, buf, length);
368}
369EXPORT_SYMBOL_GPL(mei_cl_recv);
370
371static void mei_bus_event_work(struct work_struct *work)
372{
373 struct mei_cl_device *device;
374
375 device = container_of(work, struct mei_cl_device, event_work);
376
377 if (device->event_cb)
378 device->event_cb(device, device->events, device->event_context);
379
380 device->events = 0;
381
382 /* Prepare for the next read */
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300383 mei_cl_read_start(device->cl, 0);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200384}
385
386int mei_cl_register_event_cb(struct mei_cl_device *device,
387 mei_cl_event_cb_t event_cb, void *context)
388{
389 if (device->event_cb)
390 return -EALREADY;
391
392 device->events = 0;
393 device->event_cb = event_cb;
394 device->event_context = context;
395 INIT_WORK(&device->event_work, mei_bus_event_work);
396
Tomas Winklerfcb136e2013-04-19 22:01:35 +0300397 mei_cl_read_start(device->cl, 0);
Samuel Ortiz3e833292013-03-27 17:29:55 +0200398
399 return 0;
400}
401EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
Samuel Ortizcf3baef2013-03-27 17:29:57 +0200402
Samuel Ortizaa6aef22013-03-27 17:29:59 +0200403void *mei_cl_get_drvdata(const struct mei_cl_device *device)
404{
405 return dev_get_drvdata(&device->dev);
406}
407EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
408
409void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
410{
411 dev_set_drvdata(&device->dev, data);
412}
413EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
414
Samuel Ortize46980a2013-04-09 01:51:38 +0300415int mei_cl_enable_device(struct mei_cl_device *device)
416{
417 int err;
418 struct mei_device *dev;
419 struct mei_cl *cl = device->cl;
420
421 if (cl == NULL)
422 return -ENODEV;
423
424 dev = cl->dev;
425
426 mutex_lock(&dev->device_lock);
427
428 cl->state = MEI_FILE_CONNECTING;
429
430 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
524int __init mei_cl_bus_init(void)
525{
526 return bus_register(&mei_cl_bus_type);
527}
528
529void __exit mei_cl_bus_exit(void)
530{
531 bus_unregister(&mei_cl_bus_type);
532}