virtio_console.c 22 KB

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