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