virtio_console.c 36 KB

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