/drivers/char/virtio_console.c

https://bitbucket.org/cresqo/cm7-p500-kernel · C · 1683 lines · 1090 code · 278 blank · 315 comment · 127 complexity · f246dda0cd3d73d86a409fe13a0425ed MD5 · raw file

  1. /*
  2. * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
  3. * Copyright (C) 2009, 2010 Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/cdev.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/device.h>
  22. #include <linux/err.h>
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/list.h>
  26. #include <linux/poll.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/virtio.h>
  31. #include <linux/virtio_console.h>
  32. #include <linux/wait.h>
  33. #include <linux/workqueue.h>
  34. #include "hvc_console.h"
  35. /*
  36. * This is a global struct for storing common data for all the devices
  37. * this driver handles.
  38. *
  39. * Mainly, it has a linked list for all the consoles in one place so
  40. * that callbacks from hvc for get_chars(), put_chars() work properly
  41. * across multiple devices and multiple ports per device.
  42. */
  43. struct ports_driver_data {
  44. /* Used for registering chardevs */
  45. struct class *class;
  46. /* Used for exporting per-port information to debugfs */
  47. struct dentry *debugfs_dir;
  48. /* Number of devices this driver is handling */
  49. unsigned int index;
  50. /*
  51. * This is used to keep track of the number of hvc consoles
  52. * spawned by this driver. This number is given as the first
  53. * argument to hvc_alloc(). To correctly map an initial
  54. * console spawned via hvc_instantiate to the console being
  55. * hooked up via hvc_alloc, we need to pass the same vtermno.
  56. *
  57. * We also just assume the first console being initialised was
  58. * the first one that got used as the initial console.
  59. */
  60. unsigned int next_vtermno;
  61. /* All the console devices handled by this driver */
  62. struct list_head consoles;
  63. };
  64. static struct ports_driver_data pdrvdata;
  65. DEFINE_SPINLOCK(pdrvdata_lock);
  66. /* This struct holds information that's relevant only for console ports */
  67. struct console {
  68. /* We'll place all consoles in a list in the pdrvdata struct */
  69. struct list_head list;
  70. /* The hvc device associated with this console port */
  71. struct hvc_struct *hvc;
  72. /* The size of the console */
  73. struct winsize ws;
  74. /*
  75. * This number identifies the number that we used to register
  76. * with hvc in hvc_instantiate() and hvc_alloc(); this is the
  77. * number passed on by the hvc callbacks to us to
  78. * differentiate between the other console ports handled by
  79. * this driver
  80. */
  81. u32 vtermno;
  82. };
  83. struct port_buffer {
  84. char *buf;
  85. /* size of the buffer in *buf above */
  86. size_t size;
  87. /* used length of the buffer */
  88. size_t len;
  89. /* offset in the buf from which to consume data */
  90. size_t offset;
  91. };
  92. /*
  93. * This is a per-device struct that stores data common to all the
  94. * ports for that device (vdev->priv).
  95. */
  96. struct ports_device {
  97. /*
  98. * Workqueue handlers where we process deferred work after
  99. * notification
  100. */
  101. struct work_struct control_work;
  102. struct list_head ports;
  103. /* To protect the list of ports */
  104. spinlock_t ports_lock;
  105. /* To protect the vq operations for the control channel */
  106. spinlock_t cvq_lock;
  107. /* The current config space is stored here */
  108. struct virtio_console_config config;
  109. /* The virtio device we're associated with */
  110. struct virtio_device *vdev;
  111. /*
  112. * A couple of virtqueues for the control channel: one for
  113. * guest->host transfers, one for host->guest transfers
  114. */
  115. struct virtqueue *c_ivq, *c_ovq;
  116. /* Array of per-port IO virtqueues */
  117. struct virtqueue **in_vqs, **out_vqs;
  118. /* Used for numbering devices for sysfs and debugfs */
  119. unsigned int drv_index;
  120. /* Major number for this device. Ports will be created as minors. */
  121. int chr_major;
  122. };
  123. /* This struct holds the per-port data */
  124. struct port {
  125. /* Next port in the list, head is in the ports_device */
  126. struct list_head list;
  127. /* Pointer to the parent virtio_console device */
  128. struct ports_device *portdev;
  129. /* The current buffer from which data has to be fed to readers */
  130. struct port_buffer *inbuf;
  131. /*
  132. * To protect the operations on the in_vq associated with this
  133. * port. Has to be a spinlock because it can be called from
  134. * interrupt context (get_char()).
  135. */
  136. spinlock_t inbuf_lock;
  137. /* Protect the operations on the out_vq. */
  138. spinlock_t outvq_lock;
  139. /* The IO vqs for this port */
  140. struct virtqueue *in_vq, *out_vq;
  141. /* File in the debugfs directory that exposes this port's information */
  142. struct dentry *debugfs_file;
  143. /*
  144. * The entries in this struct will be valid if this port is
  145. * hooked up to an hvc console
  146. */
  147. struct console cons;
  148. /* Each port associates with a separate char device */
  149. struct cdev cdev;
  150. struct device *dev;
  151. /* A waitqueue for poll() or blocking read operations */
  152. wait_queue_head_t waitqueue;
  153. /* The 'name' of the port that we expose via sysfs properties */
  154. char *name;
  155. /* The 'id' to identify the port with the Host */
  156. u32 id;
  157. bool outvq_full;
  158. /* Is the host device open */
  159. bool host_connected;
  160. /* We should allow only one process to open a port */
  161. bool guest_connected;
  162. };
  163. /* This is the very early arch-specified put chars function. */
  164. static int (*early_put_chars)(u32, const char *, int);
  165. static struct port *find_port_by_vtermno(u32 vtermno)
  166. {
  167. struct port *port;
  168. struct console *cons;
  169. unsigned long flags;
  170. spin_lock_irqsave(&pdrvdata_lock, flags);
  171. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  172. if (cons->vtermno == vtermno) {
  173. port = container_of(cons, struct port, cons);
  174. goto out;
  175. }
  176. }
  177. port = NULL;
  178. out:
  179. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  180. return port;
  181. }
  182. static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
  183. {
  184. struct port *port;
  185. unsigned long flags;
  186. spin_lock_irqsave(&portdev->ports_lock, flags);
  187. list_for_each_entry(port, &portdev->ports, list)
  188. if (port->id == id)
  189. goto out;
  190. port = NULL;
  191. out:
  192. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  193. return port;
  194. }
  195. static struct port *find_port_by_vq(struct ports_device *portdev,
  196. struct virtqueue *vq)
  197. {
  198. struct port *port;
  199. unsigned long flags;
  200. spin_lock_irqsave(&portdev->ports_lock, flags);
  201. list_for_each_entry(port, &portdev->ports, list)
  202. if (port->in_vq == vq || port->out_vq == vq)
  203. goto out;
  204. port = NULL;
  205. out:
  206. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  207. return port;
  208. }
  209. static bool is_console_port(struct port *port)
  210. {
  211. if (port->cons.hvc)
  212. return true;
  213. return false;
  214. }
  215. static inline bool use_multiport(struct ports_device *portdev)
  216. {
  217. /*
  218. * This condition can be true when put_chars is called from
  219. * early_init
  220. */
  221. if (!portdev->vdev)
  222. return 0;
  223. return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
  224. }
  225. static void free_buf(struct port_buffer *buf)
  226. {
  227. kfree(buf->buf);
  228. kfree(buf);
  229. }
  230. static struct port_buffer *alloc_buf(size_t buf_size)
  231. {
  232. struct port_buffer *buf;
  233. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  234. if (!buf)
  235. goto fail;
  236. buf->buf = kzalloc(buf_size, GFP_KERNEL);
  237. if (!buf->buf)
  238. goto free_buf;
  239. buf->len = 0;
  240. buf->offset = 0;
  241. buf->size = buf_size;
  242. return buf;
  243. free_buf:
  244. kfree(buf);
  245. fail:
  246. return NULL;
  247. }
  248. /* Callers should take appropriate locks */
  249. static void *get_inbuf(struct port *port)
  250. {
  251. struct port_buffer *buf;
  252. struct virtqueue *vq;
  253. unsigned int len;
  254. vq = port->in_vq;
  255. buf = virtqueue_get_buf(vq, &len);
  256. if (buf) {
  257. buf->len = len;
  258. buf->offset = 0;
  259. }
  260. return buf;
  261. }
  262. /*
  263. * Create a scatter-gather list representing our input buffer and put
  264. * it in the queue.
  265. *
  266. * Callers should take appropriate locks.
  267. */
  268. static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
  269. {
  270. struct scatterlist sg[1];
  271. int ret;
  272. sg_init_one(sg, buf->buf, buf->size);
  273. ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
  274. virtqueue_kick(vq);
  275. return ret;
  276. }
  277. /* Discard any unread data this port has. Callers lockers. */
  278. static void discard_port_data(struct port *port)
  279. {
  280. struct port_buffer *buf;
  281. struct virtqueue *vq;
  282. unsigned int len;
  283. int ret;
  284. vq = port->in_vq;
  285. if (port->inbuf)
  286. buf = port->inbuf;
  287. else
  288. buf = virtqueue_get_buf(vq, &len);
  289. ret = 0;
  290. while (buf) {
  291. if (add_inbuf(vq, buf) < 0) {
  292. ret++;
  293. free_buf(buf);
  294. }
  295. buf = virtqueue_get_buf(vq, &len);
  296. }
  297. port->inbuf = NULL;
  298. if (ret)
  299. dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
  300. ret);
  301. }
  302. static bool port_has_data(struct port *port)
  303. {
  304. unsigned long flags;
  305. bool ret;
  306. spin_lock_irqsave(&port->inbuf_lock, flags);
  307. if (port->inbuf) {
  308. ret = true;
  309. goto out;
  310. }
  311. port->inbuf = get_inbuf(port);
  312. if (port->inbuf) {
  313. ret = true;
  314. goto out;
  315. }
  316. ret = false;
  317. out:
  318. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  319. return ret;
  320. }
  321. static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
  322. unsigned int event, unsigned int value)
  323. {
  324. struct scatterlist sg[1];
  325. struct virtio_console_control cpkt;
  326. struct virtqueue *vq;
  327. unsigned int len;
  328. if (!use_multiport(portdev))
  329. return 0;
  330. cpkt.id = port_id;
  331. cpkt.event = event;
  332. cpkt.value = value;
  333. vq = portdev->c_ovq;
  334. sg_init_one(sg, &cpkt, sizeof(cpkt));
  335. if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
  336. virtqueue_kick(vq);
  337. while (!virtqueue_get_buf(vq, &len))
  338. cpu_relax();
  339. }
  340. return 0;
  341. }
  342. static ssize_t send_control_msg(struct port *port, unsigned int event,
  343. unsigned int value)
  344. {
  345. return __send_control_msg(port->portdev, port->id, event, value);
  346. }
  347. /* Callers must take the port->outvq_lock */
  348. static void reclaim_consumed_buffers(struct port *port)
  349. {
  350. void *buf;
  351. unsigned int len;
  352. while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
  353. kfree(buf);
  354. port->outvq_full = false;
  355. }
  356. }
  357. static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
  358. bool nonblock)
  359. {
  360. struct scatterlist sg[1];
  361. struct virtqueue *out_vq;
  362. ssize_t ret;
  363. unsigned long flags;
  364. unsigned int len;
  365. out_vq = port->out_vq;
  366. spin_lock_irqsave(&port->outvq_lock, flags);
  367. reclaim_consumed_buffers(port);
  368. sg_init_one(sg, in_buf, in_count);
  369. ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
  370. /* Tell Host to go! */
  371. virtqueue_kick(out_vq);
  372. if (ret < 0) {
  373. in_count = 0;
  374. goto done;
  375. }
  376. if (ret == 0)
  377. port->outvq_full = true;
  378. if (nonblock)
  379. goto done;
  380. /*
  381. * Wait till the host acknowledges it pushed out the data we
  382. * sent. This is done for data from the hvc_console; the tty
  383. * operations are performed with spinlocks held so we can't
  384. * sleep here. An alternative would be to copy the data to a
  385. * buffer and relax the spinning requirement. The downside is
  386. * we need to kmalloc a GFP_ATOMIC buffer each time the
  387. * console driver writes something out.
  388. */
  389. while (!virtqueue_get_buf(out_vq, &len))
  390. cpu_relax();
  391. done:
  392. spin_unlock_irqrestore(&port->outvq_lock, flags);
  393. /*
  394. * We're expected to return the amount of data we wrote -- all
  395. * of it
  396. */
  397. return in_count;
  398. }
  399. /*
  400. * Give out the data that's requested from the buffer that we have
  401. * queued up.
  402. */
  403. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
  404. bool to_user)
  405. {
  406. struct port_buffer *buf;
  407. unsigned long flags;
  408. if (!out_count || !port_has_data(port))
  409. return 0;
  410. buf = port->inbuf;
  411. out_count = min(out_count, buf->len - buf->offset);
  412. if (to_user) {
  413. ssize_t ret;
  414. ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
  415. if (ret)
  416. return -EFAULT;
  417. } else {
  418. memcpy(out_buf, buf->buf + buf->offset, out_count);
  419. }
  420. buf->offset += out_count;
  421. if (buf->offset == buf->len) {
  422. /*
  423. * We're done using all the data in this buffer.
  424. * Re-queue so that the Host can send us more data.
  425. */
  426. spin_lock_irqsave(&port->inbuf_lock, flags);
  427. port->inbuf = NULL;
  428. if (add_inbuf(port->in_vq, buf) < 0)
  429. dev_warn(port->dev, "failed add_buf\n");
  430. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  431. }
  432. /* Return the number of bytes actually copied */
  433. return out_count;
  434. }
  435. /* The condition that must be true for polling to end */
  436. static bool will_read_block(struct port *port)
  437. {
  438. return !port_has_data(port) && port->host_connected;
  439. }
  440. static bool will_write_block(struct port *port)
  441. {
  442. bool ret;
  443. if (!port->guest_connected) {
  444. /* Port got hot-unplugged. Let's exit. */
  445. return false;
  446. }
  447. if (!port->host_connected)
  448. return true;
  449. spin_lock_irq(&port->outvq_lock);
  450. /*
  451. * Check if the Host has consumed any buffers since we last
  452. * sent data (this is only applicable for nonblocking ports).
  453. */
  454. reclaim_consumed_buffers(port);
  455. ret = port->outvq_full;
  456. spin_unlock_irq(&port->outvq_lock);
  457. return ret;
  458. }
  459. static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
  460. size_t count, loff_t *offp)
  461. {
  462. struct port *port;
  463. ssize_t ret;
  464. port = filp->private_data;
  465. if (!port_has_data(port)) {
  466. /*
  467. * If nothing's connected on the host just return 0 in
  468. * case of list_empty; this tells the userspace app
  469. * that there's no connection
  470. */
  471. if (!port->host_connected)
  472. return 0;
  473. if (filp->f_flags & O_NONBLOCK)
  474. return -EAGAIN;
  475. ret = wait_event_interruptible(port->waitqueue,
  476. !will_read_block(port));
  477. if (ret < 0)
  478. return ret;
  479. }
  480. /*
  481. * We could've received a disconnection message while we were
  482. * waiting for more data.
  483. *
  484. * This check is not clubbed in the if() statement above as we
  485. * might receive some data as well as the host could get
  486. * disconnected after we got woken up from our wait. So we
  487. * really want to give off whatever data we have and only then
  488. * check for host_connected.
  489. */
  490. if (!port_has_data(port) && !port->host_connected)
  491. return 0;
  492. return fill_readbuf(port, ubuf, count, true);
  493. }
  494. static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
  495. size_t count, loff_t *offp)
  496. {
  497. struct port *port;
  498. char *buf;
  499. ssize_t ret;
  500. bool nonblock;
  501. /* Userspace could be out to fool us */
  502. if (!count)
  503. return 0;
  504. port = filp->private_data;
  505. nonblock = filp->f_flags & O_NONBLOCK;
  506. if (will_write_block(port)) {
  507. if (nonblock)
  508. return -EAGAIN;
  509. ret = wait_event_interruptible(port->waitqueue,
  510. !will_write_block(port));
  511. if (ret < 0)
  512. return ret;
  513. }
  514. count = min((size_t)(32 * 1024), count);
  515. buf = kmalloc(count, GFP_KERNEL);
  516. if (!buf)
  517. return -ENOMEM;
  518. ret = copy_from_user(buf, ubuf, count);
  519. if (ret) {
  520. ret = -EFAULT;
  521. goto free_buf;
  522. }
  523. /*
  524. * We now ask send_buf() to not spin for generic ports -- we
  525. * can re-use the same code path that non-blocking file
  526. * descriptors take for blocking file descriptors since the
  527. * wait is already done and we're certain the write will go
  528. * through to the host.
  529. */
  530. nonblock = true;
  531. ret = send_buf(port, buf, count, nonblock);
  532. if (nonblock && ret > 0)
  533. goto out;
  534. free_buf:
  535. kfree(buf);
  536. out:
  537. return ret;
  538. }
  539. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  540. {
  541. struct port *port;
  542. unsigned int ret;
  543. port = filp->private_data;
  544. poll_wait(filp, &port->waitqueue, wait);
  545. ret = 0;
  546. if (!will_read_block(port))
  547. ret |= POLLIN | POLLRDNORM;
  548. if (!will_write_block(port))
  549. ret |= POLLOUT;
  550. if (!port->host_connected)
  551. ret |= POLLHUP;
  552. return ret;
  553. }
  554. static int port_fops_release(struct inode *inode, struct file *filp)
  555. {
  556. struct port *port;
  557. port = filp->private_data;
  558. /* Notify host of port being closed */
  559. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  560. spin_lock_irq(&port->inbuf_lock);
  561. port->guest_connected = false;
  562. discard_port_data(port);
  563. spin_unlock_irq(&port->inbuf_lock);
  564. spin_lock_irq(&port->outvq_lock);
  565. reclaim_consumed_buffers(port);
  566. spin_unlock_irq(&port->outvq_lock);
  567. return 0;
  568. }
  569. static int port_fops_open(struct inode *inode, struct file *filp)
  570. {
  571. struct cdev *cdev = inode->i_cdev;
  572. struct port *port;
  573. port = container_of(cdev, struct port, cdev);
  574. filp->private_data = port;
  575. /*
  576. * Don't allow opening of console port devices -- that's done
  577. * via /dev/hvc
  578. */
  579. if (is_console_port(port))
  580. return -ENXIO;
  581. /* Allow only one process to open a particular port at a time */
  582. spin_lock_irq(&port->inbuf_lock);
  583. if (port->guest_connected) {
  584. spin_unlock_irq(&port->inbuf_lock);
  585. return -EMFILE;
  586. }
  587. port->guest_connected = true;
  588. spin_unlock_irq(&port->inbuf_lock);
  589. spin_lock_irq(&port->outvq_lock);
  590. /*
  591. * There might be a chance that we missed reclaiming a few
  592. * buffers in the window of the port getting previously closed
  593. * and opening now.
  594. */
  595. reclaim_consumed_buffers(port);
  596. spin_unlock_irq(&port->outvq_lock);
  597. /* Notify host of port being opened */
  598. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  599. return 0;
  600. }
  601. /*
  602. * The file operations that we support: programs in the guest can open
  603. * a console device, read from it, write to it, poll for data and
  604. * close it. The devices are at
  605. * /dev/vport<device number>p<port number>
  606. */
  607. static const struct file_operations port_fops = {
  608. .owner = THIS_MODULE,
  609. .open = port_fops_open,
  610. .read = port_fops_read,
  611. .write = port_fops_write,
  612. .poll = port_fops_poll,
  613. .release = port_fops_release,
  614. };
  615. /*
  616. * The put_chars() callback is pretty straightforward.
  617. *
  618. * We turn the characters into a scatter-gather list, add it to the
  619. * output queue and then kick the Host. Then we sit here waiting for
  620. * it to finish: inefficient in theory, but in practice
  621. * implementations will do it immediately (lguest's Launcher does).
  622. */
  623. static int put_chars(u32 vtermno, const char *buf, int count)
  624. {
  625. struct port *port;
  626. if (unlikely(early_put_chars))
  627. return early_put_chars(vtermno, buf, count);
  628. port = find_port_by_vtermno(vtermno);
  629. if (!port)
  630. return -EPIPE;
  631. return send_buf(port, (void *)buf, count, false);
  632. }
  633. /*
  634. * get_chars() is the callback from the hvc_console infrastructure
  635. * when an interrupt is received.
  636. *
  637. * We call out to fill_readbuf that gets us the required data from the
  638. * buffers that are queued up.
  639. */
  640. static int get_chars(u32 vtermno, char *buf, int count)
  641. {
  642. struct port *port;
  643. /* If we've not set up the port yet, we have no input to give. */
  644. if (unlikely(early_put_chars))
  645. return 0;
  646. port = find_port_by_vtermno(vtermno);
  647. if (!port)
  648. return -EPIPE;
  649. /* If we don't have an input queue yet, we can't get input. */
  650. BUG_ON(!port->in_vq);
  651. return fill_readbuf(port, buf, count, false);
  652. }
  653. static void resize_console(struct port *port)
  654. {
  655. struct virtio_device *vdev;
  656. /* The port could have been hot-unplugged */
  657. if (!port || !is_console_port(port))
  658. return;
  659. vdev = port->portdev->vdev;
  660. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
  661. hvc_resize(port->cons.hvc, port->cons.ws);
  662. }
  663. /* We set the configuration at this point, since we now have a tty */
  664. static int notifier_add_vio(struct hvc_struct *hp, int data)
  665. {
  666. struct port *port;
  667. port = find_port_by_vtermno(hp->vtermno);
  668. if (!port)
  669. return -EINVAL;
  670. hp->irq_requested = 1;
  671. resize_console(port);
  672. return 0;
  673. }
  674. static void notifier_del_vio(struct hvc_struct *hp, int data)
  675. {
  676. hp->irq_requested = 0;
  677. }
  678. /* The operations for console ports. */
  679. static const struct hv_ops hv_ops = {
  680. .get_chars = get_chars,
  681. .put_chars = put_chars,
  682. .notifier_add = notifier_add_vio,
  683. .notifier_del = notifier_del_vio,
  684. .notifier_hangup = notifier_del_vio,
  685. };
  686. /*
  687. * Console drivers are initialized very early so boot messages can go
  688. * out, so we do things slightly differently from the generic virtio
  689. * initialization of the net and block drivers.
  690. *
  691. * At this stage, the console is output-only. It's too early to set
  692. * up a virtqueue, so we let the drivers do some boutique early-output
  693. * thing.
  694. */
  695. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  696. {
  697. early_put_chars = put_chars;
  698. return hvc_instantiate(0, 0, &hv_ops);
  699. }
  700. int init_port_console(struct port *port)
  701. {
  702. int ret;
  703. /*
  704. * The Host's telling us this port is a console port. Hook it
  705. * up with an hvc console.
  706. *
  707. * To set up and manage our virtual console, we call
  708. * hvc_alloc().
  709. *
  710. * The first argument of hvc_alloc() is the virtual console
  711. * number. The second argument is the parameter for the
  712. * notification mechanism (like irq number). We currently
  713. * leave this as zero, virtqueues have implicit notifications.
  714. *
  715. * The third argument is a "struct hv_ops" containing the
  716. * put_chars() get_chars(), notifier_add() and notifier_del()
  717. * pointers. The final argument is the output buffer size: we
  718. * can do any size, so we put PAGE_SIZE here.
  719. */
  720. port->cons.vtermno = pdrvdata.next_vtermno;
  721. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  722. if (IS_ERR(port->cons.hvc)) {
  723. ret = PTR_ERR(port->cons.hvc);
  724. dev_err(port->dev,
  725. "error %d allocating hvc for port\n", ret);
  726. port->cons.hvc = NULL;
  727. return ret;
  728. }
  729. spin_lock_irq(&pdrvdata_lock);
  730. pdrvdata.next_vtermno++;
  731. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  732. spin_unlock_irq(&pdrvdata_lock);
  733. port->guest_connected = true;
  734. /*
  735. * Start using the new console output if this is the first
  736. * console to come up.
  737. */
  738. if (early_put_chars)
  739. early_put_chars = NULL;
  740. /* Notify host of port being opened */
  741. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  742. return 0;
  743. }
  744. static ssize_t show_port_name(struct device *dev,
  745. struct device_attribute *attr, char *buffer)
  746. {
  747. struct port *port;
  748. port = dev_get_drvdata(dev);
  749. return sprintf(buffer, "%s\n", port->name);
  750. }
  751. static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
  752. static struct attribute *port_sysfs_entries[] = {
  753. &dev_attr_name.attr,
  754. NULL
  755. };
  756. static struct attribute_group port_attribute_group = {
  757. .name = NULL, /* put in device directory */
  758. .attrs = port_sysfs_entries,
  759. };
  760. static int debugfs_open(struct inode *inode, struct file *filp)
  761. {
  762. filp->private_data = inode->i_private;
  763. return 0;
  764. }
  765. static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
  766. size_t count, loff_t *offp)
  767. {
  768. struct port *port;
  769. char *buf;
  770. ssize_t ret, out_offset, out_count;
  771. out_count = 1024;
  772. buf = kmalloc(out_count, GFP_KERNEL);
  773. if (!buf)
  774. return -ENOMEM;
  775. port = filp->private_data;
  776. out_offset = 0;
  777. out_offset += snprintf(buf + out_offset, out_count,
  778. "name: %s\n", port->name ? port->name : "");
  779. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  780. "guest_connected: %d\n", port->guest_connected);
  781. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  782. "host_connected: %d\n", port->host_connected);
  783. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  784. "outvq_full: %d\n", port->outvq_full);
  785. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  786. "is_console: %s\n",
  787. is_console_port(port) ? "yes" : "no");
  788. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  789. "console_vtermno: %u\n", port->cons.vtermno);
  790. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  791. kfree(buf);
  792. return ret;
  793. }
  794. static const struct file_operations port_debugfs_ops = {
  795. .owner = THIS_MODULE,
  796. .open = debugfs_open,
  797. .read = debugfs_read,
  798. };
  799. static void set_console_size(struct port *port, u16 rows, u16 cols)
  800. {
  801. if (!port || !is_console_port(port))
  802. return;
  803. port->cons.ws.ws_row = rows;
  804. port->cons.ws.ws_col = cols;
  805. }
  806. static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
  807. {
  808. struct port_buffer *buf;
  809. unsigned int nr_added_bufs;
  810. int ret;
  811. nr_added_bufs = 0;
  812. do {
  813. buf = alloc_buf(PAGE_SIZE);
  814. if (!buf)
  815. break;
  816. spin_lock_irq(lock);
  817. ret = add_inbuf(vq, buf);
  818. if (ret < 0) {
  819. spin_unlock_irq(lock);
  820. free_buf(buf);
  821. break;
  822. }
  823. nr_added_bufs++;
  824. spin_unlock_irq(lock);
  825. } while (ret > 0);
  826. return nr_added_bufs;
  827. }
  828. static int add_port(struct ports_device *portdev, u32 id)
  829. {
  830. char debugfs_name[16];
  831. struct port *port;
  832. struct port_buffer *buf;
  833. dev_t devt;
  834. unsigned int nr_added_bufs;
  835. int err;
  836. port = kmalloc(sizeof(*port), GFP_KERNEL);
  837. if (!port) {
  838. err = -ENOMEM;
  839. goto fail;
  840. }
  841. port->portdev = portdev;
  842. port->id = id;
  843. port->name = NULL;
  844. port->inbuf = NULL;
  845. port->cons.hvc = NULL;
  846. port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
  847. port->host_connected = port->guest_connected = false;
  848. port->outvq_full = false;
  849. port->in_vq = portdev->in_vqs[port->id];
  850. port->out_vq = portdev->out_vqs[port->id];
  851. cdev_init(&port->cdev, &port_fops);
  852. devt = MKDEV(portdev->chr_major, id);
  853. err = cdev_add(&port->cdev, devt, 1);
  854. if (err < 0) {
  855. dev_err(&port->portdev->vdev->dev,
  856. "Error %d adding cdev for port %u\n", err, id);
  857. goto free_port;
  858. }
  859. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  860. devt, port, "vport%up%u",
  861. port->portdev->drv_index, id);
  862. if (IS_ERR(port->dev)) {
  863. err = PTR_ERR(port->dev);
  864. dev_err(&port->portdev->vdev->dev,
  865. "Error %d creating device for port %u\n",
  866. err, id);
  867. goto free_cdev;
  868. }
  869. spin_lock_init(&port->inbuf_lock);
  870. spin_lock_init(&port->outvq_lock);
  871. init_waitqueue_head(&port->waitqueue);
  872. /* Fill the in_vq with buffers so the host can send us data. */
  873. nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
  874. if (!nr_added_bufs) {
  875. dev_err(port->dev, "Error allocating inbufs\n");
  876. err = -ENOMEM;
  877. goto free_device;
  878. }
  879. /*
  880. * If we're not using multiport support, this has to be a console port
  881. */
  882. if (!use_multiport(port->portdev)) {
  883. err = init_port_console(port);
  884. if (err)
  885. goto free_inbufs;
  886. }
  887. spin_lock_irq(&portdev->ports_lock);
  888. list_add_tail(&port->list, &port->portdev->ports);
  889. spin_unlock_irq(&portdev->ports_lock);
  890. /*
  891. * Tell the Host we're set so that it can send us various
  892. * configuration parameters for this port (eg, port name,
  893. * caching, whether this is a console port, etc.)
  894. */
  895. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  896. if (pdrvdata.debugfs_dir) {
  897. /*
  898. * Finally, create the debugfs file that we can use to
  899. * inspect a port's state at any time
  900. */
  901. sprintf(debugfs_name, "vport%up%u",
  902. port->portdev->drv_index, id);
  903. port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
  904. pdrvdata.debugfs_dir,
  905. port,
  906. &port_debugfs_ops);
  907. }
  908. return 0;
  909. free_inbufs:
  910. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  911. free_buf(buf);
  912. free_device:
  913. device_destroy(pdrvdata.class, port->dev->devt);
  914. free_cdev:
  915. cdev_del(&port->cdev);
  916. free_port:
  917. kfree(port);
  918. fail:
  919. /* The host might want to notify management sw about port add failure */
  920. __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
  921. return err;
  922. }
  923. /* Remove all port-specific data. */
  924. static int remove_port(struct port *port)
  925. {
  926. struct port_buffer *buf;
  927. if (port->guest_connected) {
  928. port->guest_connected = false;
  929. port->host_connected = false;
  930. wake_up_interruptible(&port->waitqueue);
  931. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  932. }
  933. spin_lock_irq(&port->portdev->ports_lock);
  934. list_del(&port->list);
  935. spin_unlock_irq(&port->portdev->ports_lock);
  936. if (is_console_port(port)) {
  937. spin_lock_irq(&pdrvdata_lock);
  938. list_del(&port->cons.list);
  939. spin_unlock_irq(&pdrvdata_lock);
  940. #if 0
  941. /*
  942. * hvc_remove() not called as removing one hvc port
  943. * results in other hvc ports getting frozen.
  944. *
  945. * Once this is resolved in hvc, this functionality
  946. * will be enabled. Till that is done, the -EPIPE
  947. * return from get_chars() above will help
  948. * hvc_console.c to clean up on ports we remove here.
  949. */
  950. hvc_remove(port->cons.hvc);
  951. #endif
  952. }
  953. sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
  954. device_destroy(pdrvdata.class, port->dev->devt);
  955. cdev_del(&port->cdev);
  956. /* Remove unused data this port might have received. */
  957. discard_port_data(port);
  958. reclaim_consumed_buffers(port);
  959. /* Remove buffers we queued up for the Host to send us data in. */
  960. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  961. free_buf(buf);
  962. kfree(port->name);
  963. debugfs_remove(port->debugfs_file);
  964. kfree(port);
  965. return 0;
  966. }
  967. /* Any private messages that the Host and Guest want to share */
  968. static void handle_control_message(struct ports_device *portdev,
  969. struct port_buffer *buf)
  970. {
  971. struct virtio_console_control *cpkt;
  972. struct port *port;
  973. size_t name_size;
  974. int err;
  975. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  976. port = find_port_by_id(portdev, cpkt->id);
  977. if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
  978. /* No valid header at start of buffer. Drop it. */
  979. dev_dbg(&portdev->vdev->dev,
  980. "Invalid index %u in control packet\n", cpkt->id);
  981. return;
  982. }
  983. switch (cpkt->event) {
  984. case VIRTIO_CONSOLE_PORT_ADD:
  985. if (port) {
  986. dev_dbg(&portdev->vdev->dev,
  987. "Port %u already added\n", port->id);
  988. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  989. break;
  990. }
  991. if (cpkt->id >= portdev->config.max_nr_ports) {
  992. dev_warn(&portdev->vdev->dev,
  993. "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
  994. cpkt->id, portdev->config.max_nr_ports - 1);
  995. break;
  996. }
  997. add_port(portdev, cpkt->id);
  998. break;
  999. case VIRTIO_CONSOLE_PORT_REMOVE:
  1000. remove_port(port);
  1001. break;
  1002. case VIRTIO_CONSOLE_CONSOLE_PORT:
  1003. if (!cpkt->value)
  1004. break;
  1005. if (is_console_port(port))
  1006. break;
  1007. init_port_console(port);
  1008. /*
  1009. * Could remove the port here in case init fails - but
  1010. * have to notify the host first.
  1011. */
  1012. break;
  1013. case VIRTIO_CONSOLE_RESIZE: {
  1014. struct {
  1015. __u16 rows;
  1016. __u16 cols;
  1017. } size;
  1018. if (!is_console_port(port))
  1019. break;
  1020. memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
  1021. sizeof(size));
  1022. set_console_size(port, size.rows, size.cols);
  1023. port->cons.hvc->irq_requested = 1;
  1024. resize_console(port);
  1025. break;
  1026. }
  1027. case VIRTIO_CONSOLE_PORT_OPEN:
  1028. port->host_connected = cpkt->value;
  1029. wake_up_interruptible(&port->waitqueue);
  1030. /*
  1031. * If the host port got closed and the host had any
  1032. * unconsumed buffers, we'll be able to reclaim them
  1033. * now.
  1034. */
  1035. spin_lock_irq(&port->outvq_lock);
  1036. reclaim_consumed_buffers(port);
  1037. spin_unlock_irq(&port->outvq_lock);
  1038. break;
  1039. case VIRTIO_CONSOLE_PORT_NAME:
  1040. /*
  1041. * Skip the size of the header and the cpkt to get the size
  1042. * of the name that was sent
  1043. */
  1044. name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
  1045. port->name = kmalloc(name_size, GFP_KERNEL);
  1046. if (!port->name) {
  1047. dev_err(port->dev,
  1048. "Not enough space to store port name\n");
  1049. break;
  1050. }
  1051. strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
  1052. name_size - 1);
  1053. port->name[name_size - 1] = 0;
  1054. /*
  1055. * Since we only have one sysfs attribute, 'name',
  1056. * create it only if we have a name for the port.
  1057. */
  1058. err = sysfs_create_group(&port->dev->kobj,
  1059. &port_attribute_group);
  1060. if (err) {
  1061. dev_err(port->dev,
  1062. "Error %d creating sysfs device attributes\n",
  1063. err);
  1064. } else {
  1065. /*
  1066. * Generate a udev event so that appropriate
  1067. * symlinks can be created based on udev
  1068. * rules.
  1069. */
  1070. kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
  1071. }
  1072. break;
  1073. }
  1074. }
  1075. static void control_work_handler(struct work_struct *work)
  1076. {
  1077. struct ports_device *portdev;
  1078. struct virtqueue *vq;
  1079. struct port_buffer *buf;
  1080. unsigned int len;
  1081. portdev = container_of(work, struct ports_device, control_work);
  1082. vq = portdev->c_ivq;
  1083. spin_lock(&portdev->cvq_lock);
  1084. while ((buf = virtqueue_get_buf(vq, &len))) {
  1085. spin_unlock(&portdev->cvq_lock);
  1086. buf->len = len;
  1087. buf->offset = 0;
  1088. handle_control_message(portdev, buf);
  1089. spin_lock(&portdev->cvq_lock);
  1090. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  1091. dev_warn(&portdev->vdev->dev,
  1092. "Error adding buffer to queue\n");
  1093. free_buf(buf);
  1094. }
  1095. }
  1096. spin_unlock(&portdev->cvq_lock);
  1097. }
  1098. static void in_intr(struct virtqueue *vq)
  1099. {
  1100. struct port *port;
  1101. unsigned long flags;
  1102. port = find_port_by_vq(vq->vdev->priv, vq);
  1103. if (!port)
  1104. return;
  1105. spin_lock_irqsave(&port->inbuf_lock, flags);
  1106. if (!port->inbuf)
  1107. port->inbuf = get_inbuf(port);
  1108. /*
  1109. * Don't queue up data when port is closed. This condition
  1110. * can be reached when a console port is not yet connected (no
  1111. * tty is spawned) and the host sends out data to console
  1112. * ports. For generic serial ports, the host won't
  1113. * (shouldn't) send data till the guest is connected.
  1114. */
  1115. if (!port->guest_connected)
  1116. discard_port_data(port);
  1117. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  1118. wake_up_interruptible(&port->waitqueue);
  1119. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  1120. hvc_kick();
  1121. }
  1122. static void control_intr(struct virtqueue *vq)
  1123. {
  1124. struct ports_device *portdev;
  1125. portdev = vq->vdev->priv;
  1126. schedule_work(&portdev->control_work);
  1127. }
  1128. static void config_intr(struct virtio_device *vdev)
  1129. {
  1130. struct ports_device *portdev;
  1131. portdev = vdev->priv;
  1132. if (!use_multiport(portdev)) {
  1133. struct port *port;
  1134. u16 rows, cols;
  1135. vdev->config->get(vdev,
  1136. offsetof(struct virtio_console_config, cols),
  1137. &cols, sizeof(u16));
  1138. vdev->config->get(vdev,
  1139. offsetof(struct virtio_console_config, rows),
  1140. &rows, sizeof(u16));
  1141. port = find_port_by_id(portdev, 0);
  1142. set_console_size(port, rows, cols);
  1143. /*
  1144. * We'll use this way of resizing only for legacy
  1145. * support. For newer userspace
  1146. * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
  1147. * to indicate console size changes so that it can be
  1148. * done per-port.
  1149. */
  1150. resize_console(port);
  1151. }
  1152. }
  1153. static int init_vqs(struct ports_device *portdev)
  1154. {
  1155. vq_callback_t **io_callbacks;
  1156. char **io_names;
  1157. struct virtqueue **vqs;
  1158. u32 i, j, nr_ports, nr_queues;
  1159. int err;
  1160. nr_ports = portdev->config.max_nr_ports;
  1161. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  1162. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  1163. if (!vqs) {
  1164. err = -ENOMEM;
  1165. goto fail;
  1166. }
  1167. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  1168. if (!io_callbacks) {
  1169. err = -ENOMEM;
  1170. goto free_vqs;
  1171. }
  1172. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  1173. if (!io_names) {
  1174. err = -ENOMEM;
  1175. goto free_callbacks;
  1176. }
  1177. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1178. GFP_KERNEL);
  1179. if (!portdev->in_vqs) {
  1180. err = -ENOMEM;
  1181. goto free_names;
  1182. }
  1183. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1184. GFP_KERNEL);
  1185. if (!portdev->out_vqs) {
  1186. err = -ENOMEM;
  1187. goto free_invqs;
  1188. }
  1189. /*
  1190. * For backward compat (newer host but older guest), the host
  1191. * spawns a console port first and also inits the vqs for port
  1192. * 0 before others.
  1193. */
  1194. j = 0;
  1195. io_callbacks[j] = in_intr;
  1196. io_callbacks[j + 1] = NULL;
  1197. io_names[j] = "input";
  1198. io_names[j + 1] = "output";
  1199. j += 2;
  1200. if (use_multiport(portdev)) {
  1201. io_callbacks[j] = control_intr;
  1202. io_callbacks[j + 1] = NULL;
  1203. io_names[j] = "control-i";
  1204. io_names[j + 1] = "control-o";
  1205. for (i = 1; i < nr_ports; i++) {
  1206. j += 2;
  1207. io_callbacks[j] = in_intr;
  1208. io_callbacks[j + 1] = NULL;
  1209. io_names[j] = "input";
  1210. io_names[j + 1] = "output";
  1211. }
  1212. }
  1213. /* Find the queues. */
  1214. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  1215. io_callbacks,
  1216. (const char **)io_names);
  1217. if (err)
  1218. goto free_outvqs;
  1219. j = 0;
  1220. portdev->in_vqs[0] = vqs[0];
  1221. portdev->out_vqs[0] = vqs[1];
  1222. j += 2;
  1223. if (use_multiport(portdev)) {
  1224. portdev->c_ivq = vqs[j];
  1225. portdev->c_ovq = vqs[j + 1];
  1226. for (i = 1; i < nr_ports; i++) {
  1227. j += 2;
  1228. portdev->in_vqs[i] = vqs[j];
  1229. portdev->out_vqs[i] = vqs[j + 1];
  1230. }
  1231. }
  1232. kfree(io_callbacks);
  1233. kfree(io_names);
  1234. kfree(vqs);
  1235. return 0;
  1236. free_names:
  1237. kfree(io_names);
  1238. free_callbacks:
  1239. kfree(io_callbacks);
  1240. free_outvqs:
  1241. kfree(portdev->out_vqs);
  1242. free_invqs:
  1243. kfree(portdev->in_vqs);
  1244. free_vqs:
  1245. kfree(vqs);
  1246. fail:
  1247. return err;
  1248. }
  1249. static const struct file_operations portdev_fops = {
  1250. .owner = THIS_MODULE,
  1251. };
  1252. /*
  1253. * Once we're further in boot, we get probed like any other virtio
  1254. * device.
  1255. *
  1256. * If the host also supports multiple console ports, we check the
  1257. * config space to see how many ports the host has spawned. We
  1258. * initialize each port found.
  1259. */
  1260. static int __devinit virtcons_probe(struct virtio_device *vdev)
  1261. {
  1262. struct ports_device *portdev;
  1263. int err;
  1264. bool multiport;
  1265. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  1266. if (!portdev) {
  1267. err = -ENOMEM;
  1268. goto fail;
  1269. }
  1270. /* Attach this portdev to this virtio_device, and vice-versa. */
  1271. portdev->vdev = vdev;
  1272. vdev->priv = portdev;
  1273. spin_lock_irq(&pdrvdata_lock);
  1274. portdev->drv_index = pdrvdata.index++;
  1275. spin_unlock_irq(&pdrvdata_lock);
  1276. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  1277. &portdev_fops);
  1278. if (portdev->chr_major < 0) {
  1279. dev_err(&vdev->dev,
  1280. "Error %d registering chrdev for device %u\n",
  1281. portdev->chr_major, portdev->drv_index);
  1282. err = portdev->chr_major;
  1283. goto free;
  1284. }
  1285. multiport = false;
  1286. portdev->config.max_nr_ports = 1;
  1287. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
  1288. multiport = true;
  1289. vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
  1290. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  1291. max_nr_ports),
  1292. &portdev->config.max_nr_ports,
  1293. sizeof(portdev->config.max_nr_ports));
  1294. }
  1295. /* Let the Host know we support multiple ports.*/
  1296. vdev->config->finalize_features(vdev);
  1297. err = init_vqs(portdev);
  1298. if (err < 0) {
  1299. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  1300. goto free_chrdev;
  1301. }
  1302. spin_lock_init(&portdev->ports_lock);
  1303. INIT_LIST_HEAD(&portdev->ports);
  1304. if (multiport) {
  1305. unsigned int nr_added_bufs;
  1306. spin_lock_init(&portdev->cvq_lock);
  1307. INIT_WORK(&portdev->control_work, &control_work_handler);
  1308. nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  1309. if (!nr_added_bufs) {
  1310. dev_err(&vdev->dev,
  1311. "Error allocating buffers for control queue\n");
  1312. err = -ENOMEM;
  1313. goto free_vqs;
  1314. }
  1315. } else {
  1316. /*
  1317. * For backward compatibility: Create a console port
  1318. * if we're running on older host.
  1319. */
  1320. add_port(portdev, 0);
  1321. }
  1322. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1323. VIRTIO_CONSOLE_DEVICE_READY, 1);
  1324. return 0;
  1325. free_vqs:
  1326. /* The host might want to notify mgmt sw about device add failure */
  1327. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1328. VIRTIO_CONSOLE_DEVICE_READY, 0);
  1329. vdev->config->del_vqs(vdev);
  1330. kfree(portdev->in_vqs);
  1331. kfree(portdev->out_vqs);
  1332. free_chrdev:
  1333. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1334. free:
  1335. kfree(portdev);
  1336. fail:
  1337. return err;
  1338. }
  1339. static void virtcons_remove(struct virtio_device *vdev)
  1340. {
  1341. struct ports_device *portdev;
  1342. struct port *port, *port2;
  1343. struct port_buffer *buf;
  1344. unsigned int len;
  1345. portdev = vdev->priv;
  1346. cancel_work_sync(&portdev->control_work);
  1347. list_for_each_entry_safe(port, port2, &portdev->ports, list)
  1348. remove_port(port);
  1349. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1350. while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
  1351. free_buf(buf);
  1352. while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
  1353. free_buf(buf);
  1354. vdev->config->del_vqs(vdev);
  1355. kfree(portdev->in_vqs);
  1356. kfree(portdev->out_vqs);
  1357. kfree(portdev);
  1358. }
  1359. static struct virtio_device_id id_table[] = {
  1360. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  1361. { 0 },
  1362. };
  1363. static unsigned int features[] = {
  1364. VIRTIO_CONSOLE_F_SIZE,
  1365. VIRTIO_CONSOLE_F_MULTIPORT,
  1366. };
  1367. static struct virtio_driver virtio_console = {
  1368. .feature_table = features,
  1369. .feature_table_size = ARRAY_SIZE(features),
  1370. .driver.name = KBUILD_MODNAME,
  1371. .driver.owner = THIS_MODULE,
  1372. .id_table = id_table,
  1373. .probe = virtcons_probe,
  1374. .remove = virtcons_remove,
  1375. .config_changed = config_intr,
  1376. };
  1377. static int __init init(void)
  1378. {
  1379. int err;
  1380. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  1381. if (IS_ERR(pdrvdata.class)) {
  1382. err = PTR_ERR(pdrvdata.class);
  1383. pr_err("Error %d creating virtio-ports class\n", err);
  1384. return err;
  1385. }
  1386. pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
  1387. if (!pdrvdata.debugfs_dir) {
  1388. pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
  1389. PTR_ERR(pdrvdata.debugfs_dir));
  1390. }
  1391. INIT_LIST_HEAD(&pdrvdata.consoles);
  1392. return register_virtio_driver(&virtio_console);
  1393. }
  1394. static void __exit fini(void)
  1395. {
  1396. unregister_virtio_driver(&virtio_console);
  1397. class_destroy(pdrvdata.class);
  1398. if (pdrvdata.debugfs_dir)
  1399. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1400. }
  1401. module_init(init);
  1402. module_exit(fini);
  1403. MODULE_DEVICE_TABLE(virtio, id_table);
  1404. MODULE_DESCRIPTION("Virtio console driver");
  1405. MODULE_LICENSE("GPL");