virtio_console.c 30 KB

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