virtio_console.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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/device.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/poll.h>
  26. #include <linux/sched.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/virtio.h>
  29. #include <linux/virtio_console.h>
  30. #include <linux/wait.h>
  31. #include <linux/workqueue.h>
  32. #include "hvc_console.h"
  33. /*
  34. * This is a global struct for storing common data for all the devices
  35. * this driver handles.
  36. *
  37. * Mainly, it has a linked list for all the consoles in one place so
  38. * that callbacks from hvc for get_chars(), put_chars() work properly
  39. * across multiple devices and multiple ports per device.
  40. */
  41. struct ports_driver_data {
  42. /* Used for registering chardevs */
  43. struct class *class;
  44. /* Number of devices this driver is handling */
  45. unsigned int index;
  46. /*
  47. * This is used to keep track of the number of hvc consoles
  48. * spawned by this driver. This number is given as the first
  49. * argument to hvc_alloc(). To correctly map an initial
  50. * console spawned via hvc_instantiate to the console being
  51. * hooked up via hvc_alloc, we need to pass the same vtermno.
  52. *
  53. * We also just assume the first console being initialised was
  54. * the first one that got used as the initial console.
  55. */
  56. unsigned int next_vtermno;
  57. /* All the console devices handled by this driver */
  58. struct list_head consoles;
  59. };
  60. static struct ports_driver_data pdrvdata;
  61. DEFINE_SPINLOCK(pdrvdata_lock);
  62. /* This struct holds information that's relevant only for console ports */
  63. struct console {
  64. /* We'll place all consoles in a list in the pdrvdata struct */
  65. struct list_head list;
  66. /* The hvc device associated with this console port */
  67. struct hvc_struct *hvc;
  68. /*
  69. * This number identifies the number that we used to register
  70. * with hvc in hvc_instantiate() and hvc_alloc(); this is the
  71. * number passed on by the hvc callbacks to us to
  72. * differentiate between the other console ports handled by
  73. * this driver
  74. */
  75. u32 vtermno;
  76. };
  77. struct port_buffer {
  78. char *buf;
  79. /* size of the buffer in *buf above */
  80. size_t size;
  81. /* used length of the buffer */
  82. size_t len;
  83. /* offset in the buf from which to consume data */
  84. size_t offset;
  85. };
  86. /*
  87. * This is a per-device struct that stores data common to all the
  88. * ports for that device (vdev->priv).
  89. */
  90. struct ports_device {
  91. /*
  92. * Workqueue handlers where we process deferred work after
  93. * notification
  94. */
  95. struct work_struct control_work;
  96. struct list_head ports;
  97. /* To protect the list of ports */
  98. spinlock_t ports_lock;
  99. /* To protect the vq operations for the control channel */
  100. spinlock_t cvq_lock;
  101. /* The current config space is stored here */
  102. struct virtio_console_config config;
  103. /* The virtio device we're associated with */
  104. struct virtio_device *vdev;
  105. /*
  106. * A couple of virtqueues for the control channel: one for
  107. * guest->host transfers, one for host->guest transfers
  108. */
  109. struct virtqueue *c_ivq, *c_ovq;
  110. /* Array of per-port IO virtqueues */
  111. struct virtqueue **in_vqs, **out_vqs;
  112. /* Used for numbering devices for sysfs and debugfs */
  113. unsigned int drv_index;
  114. /* Major number for this device. Ports will be created as minors. */
  115. int chr_major;
  116. };
  117. /* This struct holds the per-port data */
  118. struct port {
  119. /* Next port in the list, head is in the ports_device */
  120. struct list_head list;
  121. /* Pointer to the parent virtio_console device */
  122. struct ports_device *portdev;
  123. /* The current buffer from which data has to be fed to readers */
  124. struct port_buffer *inbuf;
  125. /*
  126. * To protect the operations on the in_vq associated with this
  127. * port. Has to be a spinlock because it can be called from
  128. * interrupt context (get_char()).
  129. */
  130. spinlock_t inbuf_lock;
  131. /* The IO vqs for this port */
  132. struct virtqueue *in_vq, *out_vq;
  133. /*
  134. * The entries in this struct will be valid if this port is
  135. * hooked up to an hvc console
  136. */
  137. struct console cons;
  138. /* Each port associates with a separate char device */
  139. struct cdev cdev;
  140. struct device *dev;
  141. /* A waitqueue for poll() or blocking read operations */
  142. wait_queue_head_t waitqueue;
  143. /* The 'id' to identify the port with the Host */
  144. u32 id;
  145. /* Is the host device open */
  146. bool host_connected;
  147. };
  148. /* This is the very early arch-specified put chars function. */
  149. static int (*early_put_chars)(u32, const char *, int);
  150. static struct port *find_port_by_vtermno(u32 vtermno)
  151. {
  152. struct port *port;
  153. struct console *cons;
  154. unsigned long flags;
  155. spin_lock_irqsave(&pdrvdata_lock, flags);
  156. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  157. if (cons->vtermno == vtermno) {
  158. port = container_of(cons, struct port, cons);
  159. goto out;
  160. }
  161. }
  162. port = NULL;
  163. out:
  164. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  165. return port;
  166. }
  167. static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
  168. {
  169. struct port *port;
  170. unsigned long flags;
  171. spin_lock_irqsave(&portdev->ports_lock, flags);
  172. list_for_each_entry(port, &portdev->ports, list)
  173. if (port->id == id)
  174. goto out;
  175. port = NULL;
  176. out:
  177. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  178. return port;
  179. }
  180. static struct port *find_port_by_vq(struct ports_device *portdev,
  181. struct virtqueue *vq)
  182. {
  183. struct port *port;
  184. unsigned long flags;
  185. spin_lock_irqsave(&portdev->ports_lock, flags);
  186. list_for_each_entry(port, &portdev->ports, list)
  187. if (port->in_vq == vq || port->out_vq == vq)
  188. goto out;
  189. port = NULL;
  190. out:
  191. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  192. return port;
  193. }
  194. static bool is_console_port(struct port *port)
  195. {
  196. if (port->cons.hvc)
  197. return true;
  198. return false;
  199. }
  200. static inline bool use_multiport(struct ports_device *portdev)
  201. {
  202. /*
  203. * This condition can be true when put_chars is called from
  204. * early_init
  205. */
  206. if (!portdev->vdev)
  207. return 0;
  208. return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
  209. }
  210. static void free_buf(struct port_buffer *buf)
  211. {
  212. kfree(buf->buf);
  213. kfree(buf);
  214. }
  215. static struct port_buffer *alloc_buf(size_t buf_size)
  216. {
  217. struct port_buffer *buf;
  218. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  219. if (!buf)
  220. goto fail;
  221. buf->buf = kzalloc(buf_size, GFP_KERNEL);
  222. if (!buf->buf)
  223. goto free_buf;
  224. buf->len = 0;
  225. buf->offset = 0;
  226. buf->size = buf_size;
  227. return buf;
  228. free_buf:
  229. kfree(buf);
  230. fail:
  231. return NULL;
  232. }
  233. /* Callers should take appropriate locks */
  234. static void *get_inbuf(struct port *port)
  235. {
  236. struct port_buffer *buf;
  237. struct virtqueue *vq;
  238. unsigned int len;
  239. vq = port->in_vq;
  240. buf = vq->vq_ops->get_buf(vq, &len);
  241. if (buf) {
  242. buf->len = len;
  243. buf->offset = 0;
  244. }
  245. return buf;
  246. }
  247. /*
  248. * Create a scatter-gather list representing our input buffer and put
  249. * it in the queue.
  250. *
  251. * Callers should take appropriate locks.
  252. */
  253. static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
  254. {
  255. struct scatterlist sg[1];
  256. int ret;
  257. sg_init_one(sg, buf->buf, buf->size);
  258. ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
  259. vq->vq_ops->kick(vq);
  260. return ret;
  261. }
  262. static bool port_has_data(struct port *port)
  263. {
  264. unsigned long flags;
  265. bool ret;
  266. ret = false;
  267. spin_lock_irqsave(&port->inbuf_lock, flags);
  268. if (port->inbuf)
  269. ret = true;
  270. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  271. return ret;
  272. }
  273. static ssize_t send_control_msg(struct port *port, unsigned int event,
  274. unsigned int value)
  275. {
  276. struct scatterlist sg[1];
  277. struct virtio_console_control cpkt;
  278. struct virtqueue *vq;
  279. int len;
  280. if (!use_multiport(port->portdev))
  281. return 0;
  282. cpkt.id = port->id;
  283. cpkt.event = event;
  284. cpkt.value = value;
  285. vq = port->portdev->c_ovq;
  286. sg_init_one(sg, &cpkt, sizeof(cpkt));
  287. if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
  288. vq->vq_ops->kick(vq);
  289. while (!vq->vq_ops->get_buf(vq, &len))
  290. cpu_relax();
  291. }
  292. return 0;
  293. }
  294. static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
  295. {
  296. struct scatterlist sg[1];
  297. struct virtqueue *out_vq;
  298. ssize_t ret;
  299. unsigned int len;
  300. out_vq = port->out_vq;
  301. sg_init_one(sg, in_buf, in_count);
  302. ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
  303. /* Tell Host to go! */
  304. out_vq->vq_ops->kick(out_vq);
  305. if (ret < 0) {
  306. len = 0;
  307. goto fail;
  308. }
  309. /*
  310. * Wait till the host acknowledges it pushed out the data we
  311. * sent. Also ensure we return to userspace the number of
  312. * bytes that were successfully consumed by the host.
  313. */
  314. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  315. cpu_relax();
  316. fail:
  317. /* We're expected to return the amount of data we wrote */
  318. return len;
  319. }
  320. /*
  321. * Give out the data that's requested from the buffer that we have
  322. * queued up.
  323. */
  324. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
  325. bool to_user)
  326. {
  327. struct port_buffer *buf;
  328. unsigned long flags;
  329. if (!out_count || !port_has_data(port))
  330. return 0;
  331. buf = port->inbuf;
  332. out_count = min(out_count, buf->len - buf->offset);
  333. if (to_user) {
  334. ssize_t ret;
  335. ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
  336. if (ret)
  337. return -EFAULT;
  338. } else {
  339. memcpy(out_buf, buf->buf + buf->offset, out_count);
  340. }
  341. buf->offset += out_count;
  342. if (buf->offset == buf->len) {
  343. /*
  344. * We're done using all the data in this buffer.
  345. * Re-queue so that the Host can send us more data.
  346. */
  347. spin_lock_irqsave(&port->inbuf_lock, flags);
  348. port->inbuf = NULL;
  349. if (add_inbuf(port->in_vq, buf) < 0)
  350. dev_warn(port->dev, "failed add_buf\n");
  351. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  352. }
  353. /* Return the number of bytes actually copied */
  354. return out_count;
  355. }
  356. /* The condition that must be true for polling to end */
  357. static bool wait_is_over(struct port *port)
  358. {
  359. return port_has_data(port) || !port->host_connected;
  360. }
  361. static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
  362. size_t count, loff_t *offp)
  363. {
  364. struct port *port;
  365. ssize_t ret;
  366. port = filp->private_data;
  367. if (!port_has_data(port)) {
  368. /*
  369. * If nothing's connected on the host just return 0 in
  370. * case of list_empty; this tells the userspace app
  371. * that there's no connection
  372. */
  373. if (!port->host_connected)
  374. return 0;
  375. if (filp->f_flags & O_NONBLOCK)
  376. return -EAGAIN;
  377. ret = wait_event_interruptible(port->waitqueue,
  378. wait_is_over(port));
  379. if (ret < 0)
  380. return ret;
  381. }
  382. /*
  383. * We could've received a disconnection message while we were
  384. * waiting for more data.
  385. *
  386. * This check is not clubbed in the if() statement above as we
  387. * might receive some data as well as the host could get
  388. * disconnected after we got woken up from our wait. So we
  389. * really want to give off whatever data we have and only then
  390. * check for host_connected.
  391. */
  392. if (!port_has_data(port) && !port->host_connected)
  393. return 0;
  394. return fill_readbuf(port, ubuf, count, true);
  395. }
  396. static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
  397. size_t count, loff_t *offp)
  398. {
  399. struct port *port;
  400. char *buf;
  401. ssize_t ret;
  402. port = filp->private_data;
  403. count = min((size_t)(32 * 1024), count);
  404. buf = kmalloc(count, GFP_KERNEL);
  405. if (!buf)
  406. return -ENOMEM;
  407. ret = copy_from_user(buf, ubuf, count);
  408. if (ret) {
  409. ret = -EFAULT;
  410. goto free_buf;
  411. }
  412. ret = send_buf(port, buf, count);
  413. free_buf:
  414. kfree(buf);
  415. return ret;
  416. }
  417. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  418. {
  419. struct port *port;
  420. unsigned int ret;
  421. port = filp->private_data;
  422. poll_wait(filp, &port->waitqueue, wait);
  423. ret = 0;
  424. if (port->inbuf)
  425. ret |= POLLIN | POLLRDNORM;
  426. if (port->host_connected)
  427. ret |= POLLOUT;
  428. if (!port->host_connected)
  429. ret |= POLLHUP;
  430. return ret;
  431. }
  432. static int port_fops_release(struct inode *inode, struct file *filp)
  433. {
  434. struct port *port;
  435. port = filp->private_data;
  436. /* Notify host of port being closed */
  437. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  438. return 0;
  439. }
  440. static int port_fops_open(struct inode *inode, struct file *filp)
  441. {
  442. struct cdev *cdev = inode->i_cdev;
  443. struct port *port;
  444. port = container_of(cdev, struct port, cdev);
  445. filp->private_data = port;
  446. /*
  447. * Don't allow opening of console port devices -- that's done
  448. * via /dev/hvc
  449. */
  450. if (is_console_port(port))
  451. return -ENXIO;
  452. /* Notify host of port being opened */
  453. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  454. return 0;
  455. }
  456. /*
  457. * The file operations that we support: programs in the guest can open
  458. * a console device, read from it, write to it, poll for data and
  459. * close it. The devices are at
  460. * /dev/vport<device number>p<port number>
  461. */
  462. static const struct file_operations port_fops = {
  463. .owner = THIS_MODULE,
  464. .open = port_fops_open,
  465. .read = port_fops_read,
  466. .write = port_fops_write,
  467. .poll = port_fops_poll,
  468. .release = port_fops_release,
  469. };
  470. /*
  471. * The put_chars() callback is pretty straightforward.
  472. *
  473. * We turn the characters into a scatter-gather list, add it to the
  474. * output queue and then kick the Host. Then we sit here waiting for
  475. * it to finish: inefficient in theory, but in practice
  476. * implementations will do it immediately (lguest's Launcher does).
  477. */
  478. static int put_chars(u32 vtermno, const char *buf, int count)
  479. {
  480. struct port *port;
  481. port = find_port_by_vtermno(vtermno);
  482. if (!port)
  483. return 0;
  484. if (unlikely(early_put_chars))
  485. return early_put_chars(vtermno, buf, count);
  486. return send_buf(port, (void *)buf, count);
  487. }
  488. /*
  489. * get_chars() is the callback from the hvc_console infrastructure
  490. * when an interrupt is received.
  491. *
  492. * We call out to fill_readbuf that gets us the required data from the
  493. * buffers that are queued up.
  494. */
  495. static int get_chars(u32 vtermno, char *buf, int count)
  496. {
  497. struct port *port;
  498. port = find_port_by_vtermno(vtermno);
  499. if (!port)
  500. return 0;
  501. /* If we don't have an input queue yet, we can't get input. */
  502. BUG_ON(!port->in_vq);
  503. return fill_readbuf(port, buf, count, false);
  504. }
  505. static void resize_console(struct port *port)
  506. {
  507. struct virtio_device *vdev;
  508. struct winsize ws;
  509. vdev = port->portdev->vdev;
  510. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
  511. vdev->config->get(vdev,
  512. offsetof(struct virtio_console_config, cols),
  513. &ws.ws_col, sizeof(u16));
  514. vdev->config->get(vdev,
  515. offsetof(struct virtio_console_config, rows),
  516. &ws.ws_row, sizeof(u16));
  517. hvc_resize(port->cons.hvc, ws);
  518. }
  519. }
  520. static void virtcons_apply_config(struct virtio_device *vdev)
  521. {
  522. resize_console(find_port_by_vtermno(0));
  523. }
  524. /* We set the configuration at this point, since we now have a tty */
  525. static int notifier_add_vio(struct hvc_struct *hp, int data)
  526. {
  527. struct port *port;
  528. port = find_port_by_vtermno(hp->vtermno);
  529. if (!port)
  530. return -EINVAL;
  531. hp->irq_requested = 1;
  532. resize_console(port);
  533. return 0;
  534. }
  535. static void notifier_del_vio(struct hvc_struct *hp, int data)
  536. {
  537. hp->irq_requested = 0;
  538. }
  539. /* The operations for console ports. */
  540. static const struct hv_ops hv_ops = {
  541. .get_chars = get_chars,
  542. .put_chars = put_chars,
  543. .notifier_add = notifier_add_vio,
  544. .notifier_del = notifier_del_vio,
  545. .notifier_hangup = notifier_del_vio,
  546. };
  547. /*
  548. * Console drivers are initialized very early so boot messages can go
  549. * out, so we do things slightly differently from the generic virtio
  550. * initialization of the net and block drivers.
  551. *
  552. * At this stage, the console is output-only. It's too early to set
  553. * up a virtqueue, so we let the drivers do some boutique early-output
  554. * thing.
  555. */
  556. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  557. {
  558. early_put_chars = put_chars;
  559. return hvc_instantiate(0, 0, &hv_ops);
  560. }
  561. int init_port_console(struct port *port)
  562. {
  563. int ret;
  564. /*
  565. * The Host's telling us this port is a console port. Hook it
  566. * up with an hvc console.
  567. *
  568. * To set up and manage our virtual console, we call
  569. * hvc_alloc().
  570. *
  571. * The first argument of hvc_alloc() is the virtual console
  572. * number. The second argument is the parameter for the
  573. * notification mechanism (like irq number). We currently
  574. * leave this as zero, virtqueues have implicit notifications.
  575. *
  576. * The third argument is a "struct hv_ops" containing the
  577. * put_chars() get_chars(), notifier_add() and notifier_del()
  578. * pointers. The final argument is the output buffer size: we
  579. * can do any size, so we put PAGE_SIZE here.
  580. */
  581. port->cons.vtermno = pdrvdata.next_vtermno;
  582. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  583. if (IS_ERR(port->cons.hvc)) {
  584. ret = PTR_ERR(port->cons.hvc);
  585. port->cons.hvc = NULL;
  586. return ret;
  587. }
  588. spin_lock_irq(&pdrvdata_lock);
  589. pdrvdata.next_vtermno++;
  590. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  591. spin_unlock_irq(&pdrvdata_lock);
  592. /* Notify host of port being opened */
  593. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  594. return 0;
  595. }
  596. /* Any private messages that the Host and Guest want to share */
  597. static void handle_control_message(struct ports_device *portdev,
  598. struct port_buffer *buf)
  599. {
  600. struct virtio_console_control *cpkt;
  601. struct port *port;
  602. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  603. port = find_port_by_id(portdev, cpkt->id);
  604. if (!port) {
  605. /* No valid header at start of buffer. Drop it. */
  606. dev_dbg(&portdev->vdev->dev,
  607. "Invalid index %u in control packet\n", cpkt->id);
  608. return;
  609. }
  610. switch (cpkt->event) {
  611. case VIRTIO_CONSOLE_CONSOLE_PORT:
  612. if (!cpkt->value)
  613. break;
  614. if (is_console_port(port))
  615. break;
  616. init_port_console(port);
  617. /*
  618. * Could remove the port here in case init fails - but
  619. * have to notify the host first.
  620. */
  621. break;
  622. case VIRTIO_CONSOLE_RESIZE:
  623. if (!is_console_port(port))
  624. break;
  625. port->cons.hvc->irq_requested = 1;
  626. resize_console(port);
  627. break;
  628. case VIRTIO_CONSOLE_PORT_OPEN:
  629. port->host_connected = cpkt->value;
  630. wake_up_interruptible(&port->waitqueue);
  631. break;
  632. }
  633. }
  634. static void control_work_handler(struct work_struct *work)
  635. {
  636. struct ports_device *portdev;
  637. struct virtqueue *vq;
  638. struct port_buffer *buf;
  639. unsigned int len;
  640. portdev = container_of(work, struct ports_device, control_work);
  641. vq = portdev->c_ivq;
  642. spin_lock(&portdev->cvq_lock);
  643. while ((buf = vq->vq_ops->get_buf(vq, &len))) {
  644. spin_unlock(&portdev->cvq_lock);
  645. buf->len = len;
  646. buf->offset = 0;
  647. handle_control_message(portdev, buf);
  648. spin_lock(&portdev->cvq_lock);
  649. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  650. dev_warn(&portdev->vdev->dev,
  651. "Error adding buffer to queue\n");
  652. free_buf(buf);
  653. }
  654. }
  655. spin_unlock(&portdev->cvq_lock);
  656. }
  657. static void in_intr(struct virtqueue *vq)
  658. {
  659. struct port *port;
  660. unsigned long flags;
  661. port = find_port_by_vq(vq->vdev->priv, vq);
  662. if (!port)
  663. return;
  664. spin_lock_irqsave(&port->inbuf_lock, flags);
  665. port->inbuf = get_inbuf(port);
  666. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  667. wake_up_interruptible(&port->waitqueue);
  668. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  669. hvc_kick();
  670. }
  671. static void control_intr(struct virtqueue *vq)
  672. {
  673. struct ports_device *portdev;
  674. portdev = vq->vdev->priv;
  675. schedule_work(&portdev->control_work);
  676. }
  677. static void fill_queue(struct virtqueue *vq, spinlock_t *lock)
  678. {
  679. struct port_buffer *buf;
  680. int ret;
  681. do {
  682. buf = alloc_buf(PAGE_SIZE);
  683. if (!buf)
  684. break;
  685. spin_lock_irq(lock);
  686. ret = add_inbuf(vq, buf);
  687. if (ret < 0) {
  688. spin_unlock_irq(lock);
  689. free_buf(buf);
  690. break;
  691. }
  692. spin_unlock_irq(lock);
  693. } while (ret > 0);
  694. }
  695. static int add_port(struct ports_device *portdev, u32 id)
  696. {
  697. struct port *port;
  698. struct port_buffer *inbuf;
  699. dev_t devt;
  700. int err;
  701. port = kmalloc(sizeof(*port), GFP_KERNEL);
  702. if (!port) {
  703. err = -ENOMEM;
  704. goto fail;
  705. }
  706. port->portdev = portdev;
  707. port->id = id;
  708. port->inbuf = NULL;
  709. port->cons.hvc = NULL;
  710. port->host_connected = false;
  711. port->in_vq = portdev->in_vqs[port->id];
  712. port->out_vq = portdev->out_vqs[port->id];
  713. cdev_init(&port->cdev, &port_fops);
  714. devt = MKDEV(portdev->chr_major, id);
  715. err = cdev_add(&port->cdev, devt, 1);
  716. if (err < 0) {
  717. dev_err(&port->portdev->vdev->dev,
  718. "Error %d adding cdev for port %u\n", err, id);
  719. goto free_port;
  720. }
  721. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  722. devt, port, "vport%up%u",
  723. port->portdev->drv_index, id);
  724. if (IS_ERR(port->dev)) {
  725. err = PTR_ERR(port->dev);
  726. dev_err(&port->portdev->vdev->dev,
  727. "Error %d creating device for port %u\n",
  728. err, id);
  729. goto free_cdev;
  730. }
  731. spin_lock_init(&port->inbuf_lock);
  732. init_waitqueue_head(&port->waitqueue);
  733. inbuf = alloc_buf(PAGE_SIZE);
  734. if (!inbuf) {
  735. err = -ENOMEM;
  736. goto free_device;
  737. }
  738. /* Register the input buffer the first time. */
  739. add_inbuf(port->in_vq, inbuf);
  740. /*
  741. * If we're not using multiport support, this has to be a console port
  742. */
  743. if (!use_multiport(port->portdev)) {
  744. err = init_port_console(port);
  745. if (err)
  746. goto free_inbuf;
  747. }
  748. spin_lock_irq(&portdev->ports_lock);
  749. list_add_tail(&port->list, &port->portdev->ports);
  750. spin_unlock_irq(&portdev->ports_lock);
  751. /*
  752. * Tell the Host we're set so that it can send us various
  753. * configuration parameters for this port (eg, port name,
  754. * caching, whether this is a console port, etc.)
  755. */
  756. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  757. return 0;
  758. free_inbuf:
  759. free_buf(inbuf);
  760. free_device:
  761. device_destroy(pdrvdata.class, port->dev->devt);
  762. free_cdev:
  763. cdev_del(&port->cdev);
  764. free_port:
  765. kfree(port);
  766. fail:
  767. return err;
  768. }
  769. static int init_vqs(struct ports_device *portdev)
  770. {
  771. vq_callback_t **io_callbacks;
  772. char **io_names;
  773. struct virtqueue **vqs;
  774. u32 i, j, nr_ports, nr_queues;
  775. int err;
  776. nr_ports = portdev->config.max_nr_ports;
  777. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  778. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  779. if (!vqs) {
  780. err = -ENOMEM;
  781. goto fail;
  782. }
  783. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  784. if (!io_callbacks) {
  785. err = -ENOMEM;
  786. goto free_vqs;
  787. }
  788. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  789. if (!io_names) {
  790. err = -ENOMEM;
  791. goto free_callbacks;
  792. }
  793. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  794. GFP_KERNEL);
  795. if (!portdev->in_vqs) {
  796. err = -ENOMEM;
  797. goto free_names;
  798. }
  799. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  800. GFP_KERNEL);
  801. if (!portdev->out_vqs) {
  802. err = -ENOMEM;
  803. goto free_invqs;
  804. }
  805. /*
  806. * For backward compat (newer host but older guest), the host
  807. * spawns a console port first and also inits the vqs for port
  808. * 0 before others.
  809. */
  810. j = 0;
  811. io_callbacks[j] = in_intr;
  812. io_callbacks[j + 1] = NULL;
  813. io_names[j] = "input";
  814. io_names[j + 1] = "output";
  815. j += 2;
  816. if (use_multiport(portdev)) {
  817. io_callbacks[j] = control_intr;
  818. io_callbacks[j + 1] = NULL;
  819. io_names[j] = "control-i";
  820. io_names[j + 1] = "control-o";
  821. for (i = 1; i < nr_ports; i++) {
  822. j += 2;
  823. io_callbacks[j] = in_intr;
  824. io_callbacks[j + 1] = NULL;
  825. io_names[j] = "input";
  826. io_names[j + 1] = "output";
  827. }
  828. }
  829. /* Find the queues. */
  830. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  831. io_callbacks,
  832. (const char **)io_names);
  833. if (err)
  834. goto free_outvqs;
  835. j = 0;
  836. portdev->in_vqs[0] = vqs[0];
  837. portdev->out_vqs[0] = vqs[1];
  838. j += 2;
  839. if (use_multiport(portdev)) {
  840. portdev->c_ivq = vqs[j];
  841. portdev->c_ovq = vqs[j + 1];
  842. for (i = 1; i < nr_ports; i++) {
  843. j += 2;
  844. portdev->in_vqs[i] = vqs[j];
  845. portdev->out_vqs[i] = vqs[j + 1];
  846. }
  847. }
  848. kfree(io_callbacks);
  849. kfree(io_names);
  850. kfree(vqs);
  851. return 0;
  852. free_names:
  853. kfree(io_names);
  854. free_callbacks:
  855. kfree(io_callbacks);
  856. free_outvqs:
  857. kfree(portdev->out_vqs);
  858. free_invqs:
  859. kfree(portdev->in_vqs);
  860. free_vqs:
  861. kfree(vqs);
  862. fail:
  863. return err;
  864. }
  865. static const struct file_operations portdev_fops = {
  866. .owner = THIS_MODULE,
  867. };
  868. /*
  869. * Once we're further in boot, we get probed like any other virtio
  870. * device.
  871. *
  872. * If the host also supports multiple console ports, we check the
  873. * config space to see how many ports the host has spawned. We
  874. * initialize each port found.
  875. */
  876. static int __devinit virtcons_probe(struct virtio_device *vdev)
  877. {
  878. struct ports_device *portdev;
  879. u32 i;
  880. int err;
  881. bool multiport;
  882. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  883. if (!portdev) {
  884. err = -ENOMEM;
  885. goto fail;
  886. }
  887. /* Attach this portdev to this virtio_device, and vice-versa. */
  888. portdev->vdev = vdev;
  889. vdev->priv = portdev;
  890. spin_lock_irq(&pdrvdata_lock);
  891. portdev->drv_index = pdrvdata.index++;
  892. spin_unlock_irq(&pdrvdata_lock);
  893. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  894. &portdev_fops);
  895. if (portdev->chr_major < 0) {
  896. dev_err(&vdev->dev,
  897. "Error %d registering chrdev for device %u\n",
  898. portdev->chr_major, portdev->drv_index);
  899. err = portdev->chr_major;
  900. goto free;
  901. }
  902. multiport = false;
  903. portdev->config.nr_ports = 1;
  904. portdev->config.max_nr_ports = 1;
  905. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
  906. multiport = true;
  907. vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
  908. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  909. nr_ports),
  910. &portdev->config.nr_ports,
  911. sizeof(portdev->config.nr_ports));
  912. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  913. max_nr_ports),
  914. &portdev->config.max_nr_ports,
  915. sizeof(portdev->config.max_nr_ports));
  916. if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
  917. dev_warn(&vdev->dev,
  918. "More ports (%u) specified than allowed (%u). Will init %u ports.",
  919. portdev->config.nr_ports,
  920. portdev->config.max_nr_ports,
  921. portdev->config.max_nr_ports);
  922. portdev->config.nr_ports = portdev->config.max_nr_ports;
  923. }
  924. }
  925. /* Let the Host know we support multiple ports.*/
  926. vdev->config->finalize_features(vdev);
  927. err = init_vqs(portdev);
  928. if (err < 0) {
  929. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  930. goto free_chrdev;
  931. }
  932. spin_lock_init(&portdev->ports_lock);
  933. INIT_LIST_HEAD(&portdev->ports);
  934. if (multiport) {
  935. spin_lock_init(&portdev->cvq_lock);
  936. INIT_WORK(&portdev->control_work, &control_work_handler);
  937. fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  938. }
  939. for (i = 0; i < portdev->config.nr_ports; i++)
  940. add_port(portdev, i);
  941. /* Start using the new console output. */
  942. early_put_chars = NULL;
  943. return 0;
  944. free_chrdev:
  945. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  946. free:
  947. kfree(portdev);
  948. fail:
  949. return err;
  950. }
  951. static struct virtio_device_id id_table[] = {
  952. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  953. { 0 },
  954. };
  955. static unsigned int features[] = {
  956. VIRTIO_CONSOLE_F_SIZE,
  957. VIRTIO_CONSOLE_F_MULTIPORT,
  958. };
  959. static struct virtio_driver virtio_console = {
  960. .feature_table = features,
  961. .feature_table_size = ARRAY_SIZE(features),
  962. .driver.name = KBUILD_MODNAME,
  963. .driver.owner = THIS_MODULE,
  964. .id_table = id_table,
  965. .probe = virtcons_probe,
  966. .config_changed = virtcons_apply_config,
  967. };
  968. static int __init init(void)
  969. {
  970. int err;
  971. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  972. if (IS_ERR(pdrvdata.class)) {
  973. err = PTR_ERR(pdrvdata.class);
  974. pr_err("Error %d creating virtio-ports class\n", err);
  975. return err;
  976. }
  977. INIT_LIST_HEAD(&pdrvdata.consoles);
  978. return register_virtio_driver(&virtio_console);
  979. }
  980. module_init(init);
  981. MODULE_DEVICE_TABLE(virtio, id_table);
  982. MODULE_DESCRIPTION("Virtio console driver");
  983. MODULE_LICENSE("GPL");