virtio_console.c 24 KB

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