mei: adjust the copyright notice in the files.
[linux-2.6-block.git] / drivers / misc / mei / main.c
CommitLineData
9fff0425 1// SPDX-License-Identifier: GPL-2.0
ab841160 2/*
1e55b609 3 * Copyright (c) 2003-2018, Intel Corporation. All rights reserved.
ab841160 4 * Intel Management Engine Interface (Intel MEI) Linux driver
ab841160 5 */
9fff0425 6
ab841160
OW
7#include <linux/module.h>
8#include <linux/moduleparam.h>
9#include <linux/kernel.h>
10#include <linux/device.h>
1f180359 11#include <linux/slab.h>
ab841160
OW
12#include <linux/fs.h>
13#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/fcntl.h>
ab841160
OW
16#include <linux/poll.h>
17#include <linux/init.h>
18#include <linux/ioctl.h>
19#include <linux/cdev.h>
174cd4b1 20#include <linux/sched/signal.h>
ab841160
OW
21#include <linux/uuid.h>
22#include <linux/compat.h>
23#include <linux/jiffies.h>
24#include <linux/interrupt.h>
25
4f3afe1d 26#include <linux/mei.h>
47a73801
TW
27
28#include "mei_dev.h"
90e0b5f1 29#include "client.h"
ab841160 30
ab841160
OW
31/**
32 * mei_open - the open function
33 *
34 * @inode: pointer to inode structure
35 * @file: pointer to file structure
83ce0741 36 *
a8605ea2 37 * Return: 0 on success, <0 on error
ab841160
OW
38 */
39static int mei_open(struct inode *inode, struct file *file)
40{
ab841160 41 struct mei_device *dev;
f3d8e878 42 struct mei_cl *cl;
2703d4b2 43
6f37aca8 44 int err;
ab841160 45
f3d8e878 46 dev = container_of(inode->i_cdev, struct mei_device, cdev);
5b881e3c 47 if (!dev)
e036cc57 48 return -ENODEV;
ab841160
OW
49
50 mutex_lock(&dev->device_lock);
e036cc57 51
b210d750 52 if (dev->dev_state != MEI_DEV_ENABLED) {
2bf94cab 53 dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
b210d750 54 mei_dev_state_str(dev->dev_state));
03b8d341 55 err = -ENODEV;
e036cc57 56 goto err_unlock;
1b812947 57 }
ab841160 58
7851e008 59 cl = mei_cl_alloc_linked(dev);
03b8d341
TW
60 if (IS_ERR(cl)) {
61 err = PTR_ERR(cl);
e036cc57 62 goto err_unlock;
03b8d341 63 }
ab841160 64
97d549b4 65 cl->fp = file;
ab841160 66 file->private_data = cl;
e036cc57 67
ab841160
OW
68 mutex_unlock(&dev->device_lock);
69
5b881e3c 70 return nonseekable_open(inode, file);
ab841160 71
e036cc57 72err_unlock:
ab841160 73 mutex_unlock(&dev->device_lock);
ab841160
OW
74 return err;
75}
76
77/**
78 * mei_release - the release function
79 *
80 * @inode: pointer to inode structure
81 * @file: pointer to file structure
82 *
a8605ea2 83 * Return: 0 on success, <0 on error
ab841160
OW
84 */
85static int mei_release(struct inode *inode, struct file *file)
86{
87 struct mei_cl *cl = file->private_data;
ab841160 88 struct mei_device *dev;
3c666182 89 int rets;
ab841160
OW
90
91 if (WARN_ON(!cl || !cl->dev))
92 return -ENODEV;
93
94 dev = cl->dev;
95
96 mutex_lock(&dev->device_lock);
394a77d0 97
3c666182
TW
98 rets = mei_cl_disconnect(cl);
99
a9bed610 100 mei_cl_flush_queues(cl, file);
46922186 101 cl_dbg(dev, cl, "removing\n");
a562d5c2 102
90e0b5f1 103 mei_cl_unlink(cl);
a562d5c2 104
a562d5c2 105 file->private_data = NULL;
ab841160 106
a562d5c2 107 kfree(cl);
394a77d0 108
ab841160
OW
109 mutex_unlock(&dev->device_lock);
110 return rets;
111}
112
113
114/**
115 * mei_read - the read function.
116 *
117 * @file: pointer to file structure
118 * @ubuf: pointer to user buffer
119 * @length: buffer length
120 * @offset: data offset in buffer
121 *
a8605ea2 122 * Return: >=0 data length on success , <0 on error
ab841160
OW
123 */
124static ssize_t mei_read(struct file *file, char __user *ubuf,
441ab50f 125 size_t length, loff_t *offset)
ab841160
OW
126{
127 struct mei_cl *cl = file->private_data;
ab841160 128 struct mei_device *dev;
a9bed610 129 struct mei_cl_cb *cb = NULL;
ff1586a7 130 bool nonblock = !!(file->f_flags & O_NONBLOCK);
5151e2b5 131 ssize_t rets;
ab841160
OW
132
133 if (WARN_ON(!cl || !cl->dev))
134 return -ENODEV;
135
136 dev = cl->dev;
137
dd5de1f1 138
ab841160 139 mutex_lock(&dev->device_lock);
b210d750 140 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
141 rets = -ENODEV;
142 goto out;
143 }
144
dd5de1f1
TW
145 if (length == 0) {
146 rets = 0;
147 goto out;
148 }
149
8b2458f4
AU
150 if (ubuf == NULL) {
151 rets = -EMSGSIZE;
152 goto out;
153 }
154
a9bed610 155 cb = mei_cl_read_cb(cl, file);
8b2458f4
AU
156 if (cb)
157 goto copy_buffer;
158
159 if (*offset > 0)
ab841160 160 *offset = 0;
ab841160 161
ff1586a7
AU
162 rets = mei_cl_read_start(cl, length, file);
163 if (rets && rets != -EBUSY) {
5151e2b5 164 cl_dbg(dev, cl, "mei start read failure status = %zd\n", rets);
ab841160
OW
165 goto out;
166 }
167
ff1586a7
AU
168 if (nonblock) {
169 rets = -EAGAIN;
170 goto out;
171 }
172
cb97fbbc
AU
173 mutex_unlock(&dev->device_lock);
174 if (wait_event_interruptible(cl->rx_wait,
175 !list_empty(&cl->rd_completed) ||
176 !mei_cl_is_connected(cl))) {
177 if (signal_pending(current))
178 return -EINTR;
179 return -ERESTARTSYS;
180 }
181 mutex_lock(&dev->device_lock);
e2b31644 182
cb97fbbc
AU
183 if (!mei_cl_is_connected(cl)) {
184 rets = -ENODEV;
185 goto out;
186 }
ab841160 187
cb97fbbc
AU
188 cb = mei_cl_read_cb(cl, file);
189 if (!cb) {
cb97fbbc
AU
190 rets = 0;
191 goto out;
192 }
3d33ff24 193
ab841160 194copy_buffer:
3d33ff24
TW
195 /* now copy the data to user space */
196 if (cb->status) {
197 rets = cb->status;
5151e2b5 198 cl_dbg(dev, cl, "read operation failed %zd\n", rets);
3d33ff24
TW
199 goto free;
200 }
201
35bf7692 202 cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
8b2458f4
AU
203 cb->buf.size, cb->buf_idx, *offset);
204 if (*offset >= cb->buf_idx) {
205 rets = 0;
ab841160
OW
206 goto free;
207 }
208
ebb108ef
TW
209 /* length is being truncated to PAGE_SIZE,
210 * however buf_idx may point beyond that */
211 length = min_t(size_t, length, cb->buf_idx - *offset);
ab841160 212
5db7514d 213 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
2bf94cab 214 dev_dbg(dev->dev, "failed to copy data to userland\n");
ab841160
OW
215 rets = -EFAULT;
216 goto free;
217 }
218
219 rets = length;
220 *offset += length;
f862b6b2
TW
221 /* not all data was read, keep the cb */
222 if (*offset < cb->buf_idx)
ab841160
OW
223 goto out;
224
225free:
601a1efa 226 mei_io_cb_free(cb);
8b2458f4 227 *offset = 0;
928fa666 228
ab841160 229out:
5151e2b5 230 cl_dbg(dev, cl, "end mei read rets = %zd\n", rets);
ab841160
OW
231 mutex_unlock(&dev->device_lock);
232 return rets;
233}
ab841160
OW
234/**
235 * mei_write - the write function.
236 *
237 * @file: pointer to file structure
238 * @ubuf: pointer to user buffer
239 * @length: buffer length
240 * @offset: data offset in buffer
241 *
a8605ea2 242 * Return: >=0 data length on success , <0 on error
ab841160
OW
243 */
244static ssize_t mei_write(struct file *file, const char __user *ubuf,
441ab50f 245 size_t length, loff_t *offset)
ab841160
OW
246{
247 struct mei_cl *cl = file->private_data;
6cbb097f 248 struct mei_cl_cb *cb;
ab841160 249 struct mei_device *dev;
5151e2b5 250 ssize_t rets;
ab841160
OW
251
252 if (WARN_ON(!cl || !cl->dev))
253 return -ENODEV;
254
255 dev = cl->dev;
256
257 mutex_lock(&dev->device_lock);
258
b210d750 259 if (dev->dev_state != MEI_DEV_ENABLED) {
75f0ee15 260 rets = -ENODEV;
4234a6de 261 goto out;
ab841160
OW
262 }
263
d49ed64a
AU
264 if (!mei_cl_is_connected(cl)) {
265 cl_err(dev, cl, "is not connected");
266 rets = -ENODEV;
4234a6de 267 goto out;
75f0ee15 268 }
dd5de1f1 269
d49ed64a
AU
270 if (!mei_me_cl_is_active(cl->me_cl)) {
271 rets = -ENOTTY;
dd5de1f1
TW
272 goto out;
273 }
274
d49ed64a 275 if (length > mei_cl_mtu(cl)) {
dd5de1f1 276 rets = -EFBIG;
4234a6de 277 goto out;
75f0ee15
TW
278 }
279
d49ed64a
AU
280 if (length == 0) {
281 rets = 0;
4234a6de 282 goto out;
75f0ee15 283 }
d49ed64a 284
af336cab
AU
285 while (cl->tx_cb_queued >= dev->tx_queue_limit) {
286 if (file->f_flags & O_NONBLOCK) {
287 rets = -EAGAIN;
288 goto out;
289 }
290 mutex_unlock(&dev->device_lock);
291 rets = wait_event_interruptible(cl->tx_wait,
292 cl->writing_state == MEI_WRITE_COMPLETE ||
293 (!mei_cl_is_connected(cl)));
294 mutex_lock(&dev->device_lock);
295 if (rets) {
296 if (signal_pending(current))
297 rets = -EINTR;
298 goto out;
299 }
300 if (!mei_cl_is_connected(cl)) {
301 rets = -ENODEV;
302 goto out;
303 }
304 }
305
6cbb097f
AU
306 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
307 if (!cb) {
33d28c92 308 rets = -ENOMEM;
4234a6de 309 goto out;
ab841160 310 }
ab841160 311
6cbb097f 312 rets = copy_from_user(cb->buf.data, ubuf, length);
d8b29efa 313 if (rets) {
2bf94cab 314 dev_dbg(dev->dev, "failed to copy data from userland\n");
d8b29efa 315 rets = -EFAULT;
6cbb097f 316 mei_io_cb_free(cb);
4234a6de 317 goto out;
d8b29efa 318 }
ab841160 319
e0cb6b2f 320 rets = mei_cl_write(cl, cb);
b0d0cf77 321out:
ab841160 322 mutex_unlock(&dev->device_lock);
ab841160
OW
323 return rets;
324}
325
9f81abda
TW
326/**
327 * mei_ioctl_connect_client - the connect to fw client IOCTL function
328 *
9f81abda 329 * @file: private data of the file object
a8605ea2 330 * @data: IOCTL connect data, input and output parameters
9f81abda
TW
331 *
332 * Locking: called under "dev->device_lock" lock
333 *
a8605ea2 334 * Return: 0 on success, <0 on failure.
9f81abda
TW
335 */
336static int mei_ioctl_connect_client(struct file *file,
337 struct mei_connect_client_data *data)
338{
339 struct mei_device *dev;
340 struct mei_client *client;
d320832f 341 struct mei_me_client *me_cl;
9f81abda 342 struct mei_cl *cl;
9f81abda
TW
343 int rets;
344
345 cl = file->private_data;
9f81abda
TW
346 dev = cl->dev;
347
79563db9
TW
348 if (dev->dev_state != MEI_DEV_ENABLED)
349 return -ENODEV;
9f81abda
TW
350
351 if (cl->state != MEI_FILE_INITIALIZING &&
79563db9
TW
352 cl->state != MEI_FILE_DISCONNECTED)
353 return -EBUSY;
9f81abda
TW
354
355 /* find ME client we're trying to connect to */
d320832f 356 me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
f4e06246 357 if (!me_cl) {
2bf94cab 358 dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
d49ed64a 359 &data->in_client_uuid);
f4e06246
AU
360 rets = -ENOTTY;
361 goto end;
362 }
363
364 if (me_cl->props.fixed_address) {
365 bool forbidden = dev->override_fixed_address ?
366 !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
367 if (forbidden) {
368 dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
369 &data->in_client_uuid);
370 rets = -ENOTTY;
371 goto end;
372 }
9f81abda
TW
373 }
374
2bf94cab 375 dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
d49ed64a 376 me_cl->client_id);
2bf94cab 377 dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
d320832f 378 me_cl->props.protocol_version);
2bf94cab 379 dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
d320832f 380 me_cl->props.max_msg_length);
9f81abda 381
9f81abda
TW
382 /* prepare the output buffer */
383 client = &data->out_client_properties;
d320832f
TW
384 client->max_msg_length = me_cl->props.max_msg_length;
385 client->protocol_version = me_cl->props.protocol_version;
2bf94cab 386 dev_dbg(dev->dev, "Can connect?\n");
9f81abda 387
d49ed64a 388 rets = mei_cl_connect(cl, me_cl, file);
9f81abda
TW
389
390end:
79563db9 391 mei_me_cl_put(me_cl);
9f81abda
TW
392 return rets;
393}
394
3c7c8468
TW
395/**
396 * mei_ioctl_client_notify_request -
397 * propagate event notification request to client
398 *
399 * @file: pointer to file structure
400 * @request: 0 - disable, 1 - enable
401 *
402 * Return: 0 on success , <0 on error
403 */
f23e2cc4 404static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
3c7c8468
TW
405{
406 struct mei_cl *cl = file->private_data;
407
7326fffb
AU
408 if (request != MEI_HBM_NOTIFICATION_START &&
409 request != MEI_HBM_NOTIFICATION_STOP)
410 return -EINVAL;
411
412 return mei_cl_notify_request(cl, file, (u8)request);
3c7c8468
TW
413}
414
415/**
416 * mei_ioctl_client_notify_get - wait for notification request
417 *
418 * @file: pointer to file structure
419 * @notify_get: 0 - disable, 1 - enable
420 *
421 * Return: 0 on success , <0 on error
422 */
f23e2cc4 423static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
3c7c8468
TW
424{
425 struct mei_cl *cl = file->private_data;
426 bool notify_ev;
427 bool block = (file->f_flags & O_NONBLOCK) == 0;
428 int rets;
429
430 rets = mei_cl_notify_get(cl, block, &notify_ev);
431 if (rets)
432 return rets;
433
434 *notify_get = notify_ev ? 1 : 0;
435 return 0;
436}
437
ab841160
OW
438/**
439 * mei_ioctl - the IOCTL function
440 *
441 * @file: pointer to file structure
442 * @cmd: ioctl command
443 * @data: pointer to mei message structure
444 *
a8605ea2 445 * Return: 0 on success , <0 on error
ab841160
OW
446 */
447static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
448{
449 struct mei_device *dev;
450 struct mei_cl *cl = file->private_data;
154eb18f 451 struct mei_connect_client_data connect_data;
3c7c8468 452 u32 notify_get, notify_req;
ab841160
OW
453 int rets;
454
ab841160
OW
455
456 if (WARN_ON(!cl || !cl->dev))
457 return -ENODEV;
458
459 dev = cl->dev;
460
2bf94cab 461 dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
ab841160
OW
462
463 mutex_lock(&dev->device_lock);
b210d750 464 if (dev->dev_state != MEI_DEV_ENABLED) {
ab841160
OW
465 rets = -ENODEV;
466 goto out;
467 }
468
4f046e7b
TW
469 switch (cmd) {
470 case IOCTL_MEI_CONNECT_CLIENT:
2bf94cab 471 dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
154eb18f 472 if (copy_from_user(&connect_data, (char __user *)data,
4f046e7b 473 sizeof(struct mei_connect_client_data))) {
2bf94cab 474 dev_dbg(dev->dev, "failed to copy data from userland\n");
4f046e7b
TW
475 rets = -EFAULT;
476 goto out;
477 }
ab841160 478
154eb18f 479 rets = mei_ioctl_connect_client(file, &connect_data);
4f046e7b
TW
480 if (rets)
481 goto out;
ab841160 482
4f046e7b 483 /* if all is ok, copying the data back to user. */
154eb18f 484 if (copy_to_user((char __user *)data, &connect_data,
ab841160 485 sizeof(struct mei_connect_client_data))) {
2bf94cab 486 dev_dbg(dev->dev, "failed to copy data to userland\n");
4f046e7b
TW
487 rets = -EFAULT;
488 goto out;
489 }
490
491 break;
154eb18f 492
3c7c8468
TW
493 case IOCTL_MEI_NOTIFY_SET:
494 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
495 if (copy_from_user(&notify_req,
496 (char __user *)data, sizeof(notify_req))) {
497 dev_dbg(dev->dev, "failed to copy data from userland\n");
498 rets = -EFAULT;
499 goto out;
500 }
501 rets = mei_ioctl_client_notify_request(file, notify_req);
502 break;
503
504 case IOCTL_MEI_NOTIFY_GET:
505 dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
506 rets = mei_ioctl_client_notify_get(file, &notify_get);
507 if (rets)
508 goto out;
509
510 dev_dbg(dev->dev, "copy connect data to user\n");
511 if (copy_to_user((char __user *)data,
512 &notify_get, sizeof(notify_get))) {
513 dev_dbg(dev->dev, "failed to copy data to userland\n");
514 rets = -EFAULT;
515 goto out;
516
517 }
518 break;
519
4f046e7b 520 default:
4f046e7b 521 rets = -ENOIOCTLCMD;
ab841160
OW
522 }
523
524out:
ab841160
OW
525 mutex_unlock(&dev->device_lock);
526 return rets;
527}
528
529/**
530 * mei_compat_ioctl - the compat IOCTL function
531 *
532 * @file: pointer to file structure
533 * @cmd: ioctl command
534 * @data: pointer to mei message structure
535 *
a8605ea2 536 * Return: 0 on success , <0 on error
ab841160
OW
537 */
538#ifdef CONFIG_COMPAT
539static long mei_compat_ioctl(struct file *file,
441ab50f 540 unsigned int cmd, unsigned long data)
ab841160
OW
541{
542 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
543}
544#endif
545
546
547/**
548 * mei_poll - the poll function
549 *
550 * @file: pointer to file structure
551 * @wait: pointer to poll_table structure
552 *
a8605ea2 553 * Return: poll mask
ab841160 554 */
afc9a42b 555static __poll_t mei_poll(struct file *file, poll_table *wait)
ab841160 556{
01699437 557 __poll_t req_events = poll_requested_events(wait);
ab841160
OW
558 struct mei_cl *cl = file->private_data;
559 struct mei_device *dev;
afc9a42b 560 __poll_t mask = 0;
2c84c297 561 bool notify_en;
ab841160
OW
562
563 if (WARN_ON(!cl || !cl->dev))
a9a08845 564 return EPOLLERR;
ab841160
OW
565
566 dev = cl->dev;
567
568 mutex_lock(&dev->device_lock);
569
a9a08845 570 notify_en = cl->notify_en && (req_events & EPOLLPRI);
f3de9b63
TW
571
572 if (dev->dev_state != MEI_DEV_ENABLED ||
573 !mei_cl_is_connected(cl)) {
a9a08845 574 mask = EPOLLERR;
ab841160
OW
575 goto out;
576 }
577
2c84c297
TW
578 if (notify_en) {
579 poll_wait(file, &cl->ev_wait, wait);
580 if (cl->notify_ev)
a9a08845 581 mask |= EPOLLPRI;
2c84c297 582 }
9fa0be8b 583
a9a08845 584 if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1d9013f0
TW
585 poll_wait(file, &cl->rx_wait, wait);
586
587 if (!list_empty(&cl->rd_completed))
a9a08845 588 mask |= EPOLLIN | EPOLLRDNORM;
1d9013f0 589 else
3030dc05 590 mei_cl_read_start(cl, mei_cl_mtu(cl), file);
1d9013f0 591 }
ab841160 592
03b2cbb6 593 if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
af336cab
AU
594 poll_wait(file, &cl->tx_wait, wait);
595 if (cl->tx_cb_queued < dev->tx_queue_limit)
03b2cbb6 596 mask |= EPOLLOUT | EPOLLWRNORM;
af336cab
AU
597 }
598
ab841160
OW
599out:
600 mutex_unlock(&dev->device_lock);
601 return mask;
602}
603
58cde1a6
AU
604/**
605 * mei_cl_is_write_queued - check if the client has pending writes.
606 *
607 * @cl: writing host client
608 *
609 * Return: true if client is writing, false otherwise.
610 */
611static bool mei_cl_is_write_queued(struct mei_cl *cl)
612{
613 struct mei_device *dev = cl->dev;
614 struct mei_cl_cb *cb;
615
616 list_for_each_entry(cb, &dev->write_list, list)
617 if (cb->cl == cl)
618 return true;
619 list_for_each_entry(cb, &dev->write_waiting_list, list)
620 if (cb->cl == cl)
621 return true;
622 return false;
623}
624
625/**
626 * mei_fsync - the fsync handler
627 *
628 * @fp: pointer to file structure
629 * @start: unused
630 * @end: unused
631 * @datasync: unused
632 *
633 * Return: 0 on success, -ENODEV if client is not connected
634 */
635static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
636{
637 struct mei_cl *cl = fp->private_data;
638 struct mei_device *dev;
639 int rets;
640
641 if (WARN_ON(!cl || !cl->dev))
642 return -ENODEV;
643
644 dev = cl->dev;
645
646 mutex_lock(&dev->device_lock);
647
648 if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
649 rets = -ENODEV;
650 goto out;
651 }
652
653 while (mei_cl_is_write_queued(cl)) {
654 mutex_unlock(&dev->device_lock);
655 rets = wait_event_interruptible(cl->tx_wait,
656 cl->writing_state == MEI_WRITE_COMPLETE ||
657 !mei_cl_is_connected(cl));
658 mutex_lock(&dev->device_lock);
659 if (rets) {
660 if (signal_pending(current))
661 rets = -EINTR;
662 goto out;
663 }
664 if (!mei_cl_is_connected(cl)) {
665 rets = -ENODEV;
666 goto out;
667 }
668 }
669 rets = 0;
670out:
671 mutex_unlock(&dev->device_lock);
672 return rets;
673}
674
237092bf
TW
675/**
676 * mei_fasync - asynchronous io support
677 *
678 * @fd: file descriptor
679 * @file: pointer to file structure
680 * @band: band bitmap
681 *
ed6dc538
TW
682 * Return: negative on error,
683 * 0 if it did no changes,
684 * and positive a process was added or deleted
237092bf
TW
685 */
686static int mei_fasync(int fd, struct file *file, int band)
687{
688
689 struct mei_cl *cl = file->private_data;
690
691 if (!mei_cl_is_connected(cl))
ed6dc538 692 return -ENODEV;
237092bf
TW
693
694 return fasync_helper(fd, file, band, &cl->ev_async);
695}
696
55c4e640 697/**
88d1bece 698 * fw_status_show - mei device fw_status attribute show method
55c4e640
TW
699 *
700 * @device: device pointer
701 * @attr: attribute pointer
702 * @buf: char out buffer
703 *
704 * Return: number of the bytes printed into buf or error
705 */
706static ssize_t fw_status_show(struct device *device,
707 struct device_attribute *attr, char *buf)
708{
709 struct mei_device *dev = dev_get_drvdata(device);
710 struct mei_fw_status fw_status;
711 int err, i;
712 ssize_t cnt = 0;
713
714 mutex_lock(&dev->device_lock);
715 err = mei_fw_status(dev, &fw_status);
716 mutex_unlock(&dev->device_lock);
717 if (err) {
718 dev_err(device, "read fw_status error = %d\n", err);
719 return err;
720 }
721
722 for (i = 0; i < fw_status.count; i++)
723 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
724 fw_status.status[i]);
725 return cnt;
726}
727static DEVICE_ATTR_RO(fw_status);
728
88d1bece
AU
729/**
730 * hbm_ver_show - display HBM protocol version negotiated with FW
731 *
732 * @device: device pointer
733 * @attr: attribute pointer
734 * @buf: char out buffer
735 *
736 * Return: number of the bytes printed into buf or error
737 */
738static ssize_t hbm_ver_show(struct device *device,
739 struct device_attribute *attr, char *buf)
740{
741 struct mei_device *dev = dev_get_drvdata(device);
742 struct hbm_version ver;
743
744 mutex_lock(&dev->device_lock);
745 ver = dev->version;
746 mutex_unlock(&dev->device_lock);
747
748 return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
749}
750static DEVICE_ATTR_RO(hbm_ver);
751
752/**
753 * hbm_ver_drv_show - display HBM protocol version advertised by driver
754 *
755 * @device: device pointer
756 * @attr: attribute pointer
757 * @buf: char out buffer
758 *
759 * Return: number of the bytes printed into buf or error
760 */
761static ssize_t hbm_ver_drv_show(struct device *device,
762 struct device_attribute *attr, char *buf)
763{
764 return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
765}
766static DEVICE_ATTR_RO(hbm_ver_drv);
767
af336cab
AU
768static ssize_t tx_queue_limit_show(struct device *device,
769 struct device_attribute *attr, char *buf)
770{
771 struct mei_device *dev = dev_get_drvdata(device);
772 u8 size = 0;
773
774 mutex_lock(&dev->device_lock);
775 size = dev->tx_queue_limit;
776 mutex_unlock(&dev->device_lock);
777
778 return snprintf(buf, PAGE_SIZE, "%u\n", size);
779}
780
781static ssize_t tx_queue_limit_store(struct device *device,
782 struct device_attribute *attr,
783 const char *buf, size_t count)
784{
785 struct mei_device *dev = dev_get_drvdata(device);
786 u8 limit;
787 unsigned int inp;
788 int err;
789
790 err = kstrtouint(buf, 10, &inp);
791 if (err)
792 return err;
793 if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
794 return -EINVAL;
795 limit = inp;
796
797 mutex_lock(&dev->device_lock);
798 dev->tx_queue_limit = limit;
799 mutex_unlock(&dev->device_lock);
800
801 return count;
802}
803static DEVICE_ATTR_RW(tx_queue_limit);
804
3cfaeb33
AU
805/**
806 * fw_ver_show - display ME FW version
807 *
808 * @device: device pointer
809 * @attr: attribute pointer
810 * @buf: char out buffer
811 *
812 * Return: number of the bytes printed into buf or error
813 */
814static ssize_t fw_ver_show(struct device *device,
815 struct device_attribute *attr, char *buf)
816{
817 struct mei_device *dev = dev_get_drvdata(device);
818 struct mei_fw_version *ver;
819 ssize_t cnt = 0;
820 int i;
821
822 ver = dev->fw_ver;
823
824 for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++)
825 cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%u:%u.%u.%u.%u\n",
826 ver[i].platform, ver[i].major, ver[i].minor,
827 ver[i].hotfix, ver[i].buildno);
828 return cnt;
829}
830static DEVICE_ATTR_RO(fw_ver);
831
55c4e640
TW
832static struct attribute *mei_attrs[] = {
833 &dev_attr_fw_status.attr,
88d1bece
AU
834 &dev_attr_hbm_ver.attr,
835 &dev_attr_hbm_ver_drv.attr,
af336cab 836 &dev_attr_tx_queue_limit.attr,
3cfaeb33 837 &dev_attr_fw_ver.attr,
55c4e640
TW
838 NULL
839};
840ATTRIBUTE_GROUPS(mei);
841
5b881e3c
OW
842/*
843 * file operations structure will be used for mei char device.
844 */
845static const struct file_operations mei_fops = {
846 .owner = THIS_MODULE,
847 .read = mei_read,
848 .unlocked_ioctl = mei_ioctl,
849#ifdef CONFIG_COMPAT
850 .compat_ioctl = mei_compat_ioctl,
851#endif
852 .open = mei_open,
853 .release = mei_release,
854 .write = mei_write,
855 .poll = mei_poll,
58cde1a6 856 .fsync = mei_fsync,
237092bf 857 .fasync = mei_fasync,
5b881e3c
OW
858 .llseek = no_llseek
859};
860
f3d8e878
AU
861static struct class *mei_class;
862static dev_t mei_devt;
863#define MEI_MAX_DEVS MINORMASK
864static DEFINE_MUTEX(mei_minor_lock);
865static DEFINE_IDR(mei_idr);
866
867/**
868 * mei_minor_get - obtain next free device minor number
869 *
870 * @dev: device pointer
871 *
a8605ea2 872 * Return: allocated minor, or -ENOSPC if no free minor left
5b881e3c 873 */
f3d8e878
AU
874static int mei_minor_get(struct mei_device *dev)
875{
876 int ret;
877
878 mutex_lock(&mei_minor_lock);
879 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
880 if (ret >= 0)
881 dev->minor = ret;
882 else if (ret == -ENOSPC)
2bf94cab 883 dev_err(dev->dev, "too many mei devices\n");
5b881e3c 884
f3d8e878
AU
885 mutex_unlock(&mei_minor_lock);
886 return ret;
887}
30e53bb8 888
f3d8e878
AU
889/**
890 * mei_minor_free - mark device minor number as free
891 *
892 * @dev: device pointer
893 */
894static void mei_minor_free(struct mei_device *dev)
9a123f19 895{
f3d8e878
AU
896 mutex_lock(&mei_minor_lock);
897 idr_remove(&mei_idr, dev->minor);
898 mutex_unlock(&mei_minor_lock);
899}
900
901int mei_register(struct mei_device *dev, struct device *parent)
902{
903 struct device *clsdev; /* class device */
904 int ret, devno;
905
906 ret = mei_minor_get(dev);
907 if (ret < 0)
30e53bb8
TW
908 return ret;
909
f3d8e878
AU
910 /* Fill in the data structures */
911 devno = MKDEV(MAJOR(mei_devt), dev->minor);
912 cdev_init(&dev->cdev, &mei_fops);
154322f4 913 dev->cdev.owner = parent->driver->owner;
f3d8e878
AU
914
915 /* Add the device */
916 ret = cdev_add(&dev->cdev, devno, 1);
917 if (ret) {
918 dev_err(parent, "unable to add device %d:%d\n",
919 MAJOR(mei_devt), dev->minor);
920 goto err_dev_add;
921 }
922
55c4e640
TW
923 clsdev = device_create_with_groups(mei_class, parent, devno,
924 dev, mei_groups,
925 "mei%d", dev->minor);
f3d8e878
AU
926
927 if (IS_ERR(clsdev)) {
928 dev_err(parent, "unable to create device %d:%d\n",
929 MAJOR(mei_devt), dev->minor);
930 ret = PTR_ERR(clsdev);
931 goto err_dev_create;
932 }
933
934 ret = mei_dbgfs_register(dev, dev_name(clsdev));
935 if (ret) {
936 dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
937 goto err_dev_dbgfs;
938 }
30e53bb8
TW
939
940 return 0;
f3d8e878
AU
941
942err_dev_dbgfs:
943 device_destroy(mei_class, devno);
944err_dev_create:
945 cdev_del(&dev->cdev);
946err_dev_add:
947 mei_minor_free(dev);
948 return ret;
5b881e3c 949}
40e0b67b 950EXPORT_SYMBOL_GPL(mei_register);
5b881e3c 951
30e53bb8 952void mei_deregister(struct mei_device *dev)
5b881e3c 953{
f3d8e878
AU
954 int devno;
955
956 devno = dev->cdev.dev;
957 cdev_del(&dev->cdev);
958
30e53bb8 959 mei_dbgfs_deregister(dev);
f3d8e878
AU
960
961 device_destroy(mei_class, devno);
962
963 mei_minor_free(dev);
5b881e3c 964}
40e0b67b 965EXPORT_SYMBOL_GPL(mei_deregister);
ab841160 966
cf3baefb
SO
967static int __init mei_init(void)
968{
f3d8e878
AU
969 int ret;
970
971 mei_class = class_create(THIS_MODULE, "mei");
972 if (IS_ERR(mei_class)) {
973 pr_err("couldn't create class\n");
974 ret = PTR_ERR(mei_class);
975 goto err;
976 }
977
978 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
979 if (ret < 0) {
980 pr_err("unable to allocate char dev region\n");
981 goto err_class;
982 }
983
984 ret = mei_cl_bus_init();
985 if (ret < 0) {
986 pr_err("unable to initialize bus\n");
987 goto err_chrdev;
988 }
989
990 return 0;
991
992err_chrdev:
993 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
994err_class:
995 class_destroy(mei_class);
996err:
997 return ret;
cf3baefb
SO
998}
999
1000static void __exit mei_exit(void)
1001{
f3d8e878
AU
1002 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
1003 class_destroy(mei_class);
cf3baefb
SO
1004 mei_cl_bus_exit();
1005}
1006
1007module_init(mei_init);
1008module_exit(mei_exit);
1009
40e0b67b
TW
1010MODULE_AUTHOR("Intel Corporation");
1011MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
827eef51 1012MODULE_LICENSE("GPL v2");
ab841160 1013