virtio_console.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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/spinlock.h>
  29. #include <linux/virtio.h>
  30. #include <linux/virtio_console.h>
  31. #include <linux/wait.h>
  32. #include <linux/workqueue.h>
  33. #include "hvc_console.h"
  34. /*
  35. * This is a global struct for storing common data for all the devices
  36. * this driver handles.
  37. *
  38. * Mainly, it has a linked list for all the consoles in one place so
  39. * that callbacks from hvc for get_chars(), put_chars() work properly
  40. * across multiple devices and multiple ports per device.
  41. */
  42. struct ports_driver_data {
  43. /* Used for registering chardevs */
  44. struct class *class;
  45. /* Used for exporting per-port information to debugfs */
  46. struct dentry *debugfs_dir;
  47. /* Number of devices this driver is handling */
  48. unsigned int index;
  49. /*
  50. * This is used to keep track of the number of hvc consoles
  51. * spawned by this driver. This number is given as the first
  52. * argument to hvc_alloc(). To correctly map an initial
  53. * console spawned via hvc_instantiate to the console being
  54. * hooked up via hvc_alloc, we need to pass the same vtermno.
  55. *
  56. * We also just assume the first console being initialised was
  57. * the first one that got used as the initial console.
  58. */
  59. unsigned int next_vtermno;
  60. /* All the console devices handled by this driver */
  61. struct list_head consoles;
  62. };
  63. static struct ports_driver_data pdrvdata;
  64. DEFINE_SPINLOCK(pdrvdata_lock);
  65. /* This struct holds information that's relevant only for console ports */
  66. struct console {
  67. /* We'll place all consoles in a list in the pdrvdata struct */
  68. struct list_head list;
  69. /* The hvc device associated with this console port */
  70. struct hvc_struct *hvc;
  71. /*
  72. * This number identifies the number that we used to register
  73. * with hvc in hvc_instantiate() and hvc_alloc(); this is the
  74. * number passed on by the hvc callbacks to us to
  75. * differentiate between the other console ports handled by
  76. * this driver
  77. */
  78. u32 vtermno;
  79. };
  80. struct port_buffer {
  81. char *buf;
  82. /* size of the buffer in *buf above */
  83. size_t size;
  84. /* used length of the buffer */
  85. size_t len;
  86. /* offset in the buf from which to consume data */
  87. size_t offset;
  88. };
  89. /*
  90. * This is a per-device struct that stores data common to all the
  91. * ports for that device (vdev->priv).
  92. */
  93. struct ports_device {
  94. /*
  95. * Workqueue handlers where we process deferred work after
  96. * notification
  97. */
  98. struct work_struct control_work;
  99. struct work_struct config_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 = vq->vq_ops->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 = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
  269. vq->vq_ops->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 = vq->vq_ops->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 = vq->vq_ops->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 port *port, unsigned int event,
  317. 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(port->portdev))
  324. return 0;
  325. cpkt.id = port->id;
  326. cpkt.event = event;
  327. cpkt.value = value;
  328. vq = port->portdev->c_ovq;
  329. sg_init_one(sg, &cpkt, sizeof(cpkt));
  330. if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
  331. vq->vq_ops->kick(vq);
  332. while (!vq->vq_ops->get_buf(vq, &len))
  333. cpu_relax();
  334. }
  335. return 0;
  336. }
  337. static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
  338. {
  339. struct scatterlist sg[1];
  340. struct virtqueue *out_vq;
  341. ssize_t ret;
  342. unsigned int len;
  343. out_vq = port->out_vq;
  344. sg_init_one(sg, in_buf, in_count);
  345. ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
  346. /* Tell Host to go! */
  347. out_vq->vq_ops->kick(out_vq);
  348. if (ret < 0) {
  349. len = 0;
  350. goto fail;
  351. }
  352. /*
  353. * Wait till the host acknowledges it pushed out the data we
  354. * sent. Also ensure we return to userspace the number of
  355. * bytes that were successfully consumed by the host.
  356. */
  357. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  358. cpu_relax();
  359. fail:
  360. /* We're expected to return the amount of data we wrote */
  361. return len;
  362. }
  363. /*
  364. * Give out the data that's requested from the buffer that we have
  365. * queued up.
  366. */
  367. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
  368. bool to_user)
  369. {
  370. struct port_buffer *buf;
  371. unsigned long flags;
  372. if (!out_count || !port_has_data(port))
  373. return 0;
  374. buf = port->inbuf;
  375. out_count = min(out_count, buf->len - buf->offset);
  376. if (to_user) {
  377. ssize_t ret;
  378. ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
  379. if (ret)
  380. return -EFAULT;
  381. } else {
  382. memcpy(out_buf, buf->buf + buf->offset, out_count);
  383. }
  384. buf->offset += out_count;
  385. if (buf->offset == buf->len) {
  386. /*
  387. * We're done using all the data in this buffer.
  388. * Re-queue so that the Host can send us more data.
  389. */
  390. spin_lock_irqsave(&port->inbuf_lock, flags);
  391. port->inbuf = NULL;
  392. if (add_inbuf(port->in_vq, buf) < 0)
  393. dev_warn(port->dev, "failed add_buf\n");
  394. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  395. }
  396. /* Return the number of bytes actually copied */
  397. return out_count;
  398. }
  399. /* The condition that must be true for polling to end */
  400. static bool wait_is_over(struct port *port)
  401. {
  402. return port_has_data(port) || !port->host_connected;
  403. }
  404. static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
  405. size_t count, loff_t *offp)
  406. {
  407. struct port *port;
  408. ssize_t ret;
  409. port = filp->private_data;
  410. if (!port_has_data(port)) {
  411. /*
  412. * If nothing's connected on the host just return 0 in
  413. * case of list_empty; this tells the userspace app
  414. * that there's no connection
  415. */
  416. if (!port->host_connected)
  417. return 0;
  418. if (filp->f_flags & O_NONBLOCK)
  419. return -EAGAIN;
  420. ret = wait_event_interruptible(port->waitqueue,
  421. wait_is_over(port));
  422. if (ret < 0)
  423. return ret;
  424. }
  425. /*
  426. * We could've received a disconnection message while we were
  427. * waiting for more data.
  428. *
  429. * This check is not clubbed in the if() statement above as we
  430. * might receive some data as well as the host could get
  431. * disconnected after we got woken up from our wait. So we
  432. * really want to give off whatever data we have and only then
  433. * check for host_connected.
  434. */
  435. if (!port_has_data(port) && !port->host_connected)
  436. return 0;
  437. return fill_readbuf(port, ubuf, count, true);
  438. }
  439. static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
  440. size_t count, loff_t *offp)
  441. {
  442. struct port *port;
  443. char *buf;
  444. ssize_t ret;
  445. port = filp->private_data;
  446. count = min((size_t)(32 * 1024), count);
  447. buf = kmalloc(count, GFP_KERNEL);
  448. if (!buf)
  449. return -ENOMEM;
  450. ret = copy_from_user(buf, ubuf, count);
  451. if (ret) {
  452. ret = -EFAULT;
  453. goto free_buf;
  454. }
  455. ret = send_buf(port, buf, count);
  456. free_buf:
  457. kfree(buf);
  458. return ret;
  459. }
  460. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  461. {
  462. struct port *port;
  463. unsigned int ret;
  464. port = filp->private_data;
  465. poll_wait(filp, &port->waitqueue, wait);
  466. ret = 0;
  467. if (port->inbuf)
  468. ret |= POLLIN | POLLRDNORM;
  469. if (port->host_connected)
  470. ret |= POLLOUT;
  471. if (!port->host_connected)
  472. ret |= POLLHUP;
  473. return ret;
  474. }
  475. static int port_fops_release(struct inode *inode, struct file *filp)
  476. {
  477. struct port *port;
  478. port = filp->private_data;
  479. /* Notify host of port being closed */
  480. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  481. spin_lock_irq(&port->inbuf_lock);
  482. port->guest_connected = false;
  483. discard_port_data(port);
  484. spin_unlock_irq(&port->inbuf_lock);
  485. return 0;
  486. }
  487. static int port_fops_open(struct inode *inode, struct file *filp)
  488. {
  489. struct cdev *cdev = inode->i_cdev;
  490. struct port *port;
  491. port = container_of(cdev, struct port, cdev);
  492. filp->private_data = port;
  493. /*
  494. * Don't allow opening of console port devices -- that's done
  495. * via /dev/hvc
  496. */
  497. if (is_console_port(port))
  498. return -ENXIO;
  499. /* Allow only one process to open a particular port at a time */
  500. spin_lock_irq(&port->inbuf_lock);
  501. if (port->guest_connected) {
  502. spin_unlock_irq(&port->inbuf_lock);
  503. return -EMFILE;
  504. }
  505. port->guest_connected = true;
  506. spin_unlock_irq(&port->inbuf_lock);
  507. /* Notify host of port being opened */
  508. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  509. return 0;
  510. }
  511. /*
  512. * The file operations that we support: programs in the guest can open
  513. * a console device, read from it, write to it, poll for data and
  514. * close it. The devices are at
  515. * /dev/vport<device number>p<port number>
  516. */
  517. static const struct file_operations port_fops = {
  518. .owner = THIS_MODULE,
  519. .open = port_fops_open,
  520. .read = port_fops_read,
  521. .write = port_fops_write,
  522. .poll = port_fops_poll,
  523. .release = port_fops_release,
  524. };
  525. /*
  526. * The put_chars() callback is pretty straightforward.
  527. *
  528. * We turn the characters into a scatter-gather list, add it to the
  529. * output queue and then kick the Host. Then we sit here waiting for
  530. * it to finish: inefficient in theory, but in practice
  531. * implementations will do it immediately (lguest's Launcher does).
  532. */
  533. static int put_chars(u32 vtermno, const char *buf, int count)
  534. {
  535. struct port *port;
  536. port = find_port_by_vtermno(vtermno);
  537. if (!port)
  538. return 0;
  539. if (unlikely(early_put_chars))
  540. return early_put_chars(vtermno, buf, count);
  541. return send_buf(port, (void *)buf, count);
  542. }
  543. /*
  544. * get_chars() is the callback from the hvc_console infrastructure
  545. * when an interrupt is received.
  546. *
  547. * We call out to fill_readbuf that gets us the required data from the
  548. * buffers that are queued up.
  549. */
  550. static int get_chars(u32 vtermno, char *buf, int count)
  551. {
  552. struct port *port;
  553. port = find_port_by_vtermno(vtermno);
  554. if (!port)
  555. return 0;
  556. /* If we don't have an input queue yet, we can't get input. */
  557. BUG_ON(!port->in_vq);
  558. return fill_readbuf(port, buf, count, false);
  559. }
  560. static void resize_console(struct port *port)
  561. {
  562. struct virtio_device *vdev;
  563. struct winsize ws;
  564. /* The port could have been hot-unplugged */
  565. if (!port)
  566. return;
  567. vdev = port->portdev->vdev;
  568. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
  569. vdev->config->get(vdev,
  570. offsetof(struct virtio_console_config, cols),
  571. &ws.ws_col, sizeof(u16));
  572. vdev->config->get(vdev,
  573. offsetof(struct virtio_console_config, rows),
  574. &ws.ws_row, sizeof(u16));
  575. hvc_resize(port->cons.hvc, ws);
  576. }
  577. }
  578. /* We set the configuration at this point, since we now have a tty */
  579. static int notifier_add_vio(struct hvc_struct *hp, int data)
  580. {
  581. struct port *port;
  582. port = find_port_by_vtermno(hp->vtermno);
  583. if (!port)
  584. return -EINVAL;
  585. hp->irq_requested = 1;
  586. resize_console(port);
  587. return 0;
  588. }
  589. static void notifier_del_vio(struct hvc_struct *hp, int data)
  590. {
  591. hp->irq_requested = 0;
  592. }
  593. /* The operations for console ports. */
  594. static const struct hv_ops hv_ops = {
  595. .get_chars = get_chars,
  596. .put_chars = put_chars,
  597. .notifier_add = notifier_add_vio,
  598. .notifier_del = notifier_del_vio,
  599. .notifier_hangup = notifier_del_vio,
  600. };
  601. /*
  602. * Console drivers are initialized very early so boot messages can go
  603. * out, so we do things slightly differently from the generic virtio
  604. * initialization of the net and block drivers.
  605. *
  606. * At this stage, the console is output-only. It's too early to set
  607. * up a virtqueue, so we let the drivers do some boutique early-output
  608. * thing.
  609. */
  610. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  611. {
  612. early_put_chars = put_chars;
  613. return hvc_instantiate(0, 0, &hv_ops);
  614. }
  615. int init_port_console(struct port *port)
  616. {
  617. int ret;
  618. /*
  619. * The Host's telling us this port is a console port. Hook it
  620. * up with an hvc console.
  621. *
  622. * To set up and manage our virtual console, we call
  623. * hvc_alloc().
  624. *
  625. * The first argument of hvc_alloc() is the virtual console
  626. * number. The second argument is the parameter for the
  627. * notification mechanism (like irq number). We currently
  628. * leave this as zero, virtqueues have implicit notifications.
  629. *
  630. * The third argument is a "struct hv_ops" containing the
  631. * put_chars() get_chars(), notifier_add() and notifier_del()
  632. * pointers. The final argument is the output buffer size: we
  633. * can do any size, so we put PAGE_SIZE here.
  634. */
  635. port->cons.vtermno = pdrvdata.next_vtermno;
  636. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  637. if (IS_ERR(port->cons.hvc)) {
  638. ret = PTR_ERR(port->cons.hvc);
  639. dev_err(port->dev,
  640. "error %d allocating hvc for port\n", ret);
  641. port->cons.hvc = NULL;
  642. return ret;
  643. }
  644. spin_lock_irq(&pdrvdata_lock);
  645. pdrvdata.next_vtermno++;
  646. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  647. spin_unlock_irq(&pdrvdata_lock);
  648. port->guest_connected = true;
  649. /* Notify host of port being opened */
  650. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  651. return 0;
  652. }
  653. static ssize_t show_port_name(struct device *dev,
  654. struct device_attribute *attr, char *buffer)
  655. {
  656. struct port *port;
  657. port = dev_get_drvdata(dev);
  658. return sprintf(buffer, "%s\n", port->name);
  659. }
  660. static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
  661. static struct attribute *port_sysfs_entries[] = {
  662. &dev_attr_name.attr,
  663. NULL
  664. };
  665. static struct attribute_group port_attribute_group = {
  666. .name = NULL, /* put in device directory */
  667. .attrs = port_sysfs_entries,
  668. };
  669. static int debugfs_open(struct inode *inode, struct file *filp)
  670. {
  671. filp->private_data = inode->i_private;
  672. return 0;
  673. }
  674. static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
  675. size_t count, loff_t *offp)
  676. {
  677. struct port *port;
  678. char *buf;
  679. ssize_t ret, out_offset, out_count;
  680. out_count = 1024;
  681. buf = kmalloc(out_count, GFP_KERNEL);
  682. if (!buf)
  683. return -ENOMEM;
  684. port = filp->private_data;
  685. out_offset = 0;
  686. out_offset += snprintf(buf + out_offset, out_count,
  687. "name: %s\n", port->name ? port->name : "");
  688. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  689. "guest_connected: %d\n", port->guest_connected);
  690. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  691. "host_connected: %d\n", port->host_connected);
  692. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  693. "is_console: %s\n",
  694. is_console_port(port) ? "yes" : "no");
  695. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  696. "console_vtermno: %u\n", port->cons.vtermno);
  697. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  698. kfree(buf);
  699. return ret;
  700. }
  701. static const struct file_operations port_debugfs_ops = {
  702. .owner = THIS_MODULE,
  703. .open = debugfs_open,
  704. .read = debugfs_read,
  705. };
  706. /* Remove all port-specific data. */
  707. static int remove_port(struct port *port)
  708. {
  709. struct port_buffer *buf;
  710. spin_lock_irq(&port->portdev->ports_lock);
  711. list_del(&port->list);
  712. spin_unlock_irq(&port->portdev->ports_lock);
  713. if (is_console_port(port)) {
  714. spin_lock_irq(&pdrvdata_lock);
  715. list_del(&port->cons.list);
  716. spin_unlock_irq(&pdrvdata_lock);
  717. hvc_remove(port->cons.hvc);
  718. }
  719. if (port->guest_connected)
  720. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  721. sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
  722. device_destroy(pdrvdata.class, port->dev->devt);
  723. cdev_del(&port->cdev);
  724. /* Remove unused data this port might have received. */
  725. discard_port_data(port);
  726. /* Remove buffers we queued up for the Host to send us data in. */
  727. while ((buf = port->in_vq->vq_ops->detach_unused_buf(port->in_vq)))
  728. free_buf(buf);
  729. kfree(port->name);
  730. debugfs_remove(port->debugfs_file);
  731. kfree(port);
  732. return 0;
  733. }
  734. /* Any private messages that the Host and Guest want to share */
  735. static void handle_control_message(struct ports_device *portdev,
  736. struct port_buffer *buf)
  737. {
  738. struct virtio_console_control *cpkt;
  739. struct port *port;
  740. size_t name_size;
  741. int err;
  742. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  743. port = find_port_by_id(portdev, cpkt->id);
  744. if (!port) {
  745. /* No valid header at start of buffer. Drop it. */
  746. dev_dbg(&portdev->vdev->dev,
  747. "Invalid index %u in control packet\n", cpkt->id);
  748. return;
  749. }
  750. switch (cpkt->event) {
  751. case VIRTIO_CONSOLE_CONSOLE_PORT:
  752. if (!cpkt->value)
  753. break;
  754. if (is_console_port(port))
  755. break;
  756. init_port_console(port);
  757. /*
  758. * Could remove the port here in case init fails - but
  759. * have to notify the host first.
  760. */
  761. break;
  762. case VIRTIO_CONSOLE_RESIZE:
  763. if (!is_console_port(port))
  764. break;
  765. port->cons.hvc->irq_requested = 1;
  766. resize_console(port);
  767. break;
  768. case VIRTIO_CONSOLE_PORT_OPEN:
  769. port->host_connected = cpkt->value;
  770. wake_up_interruptible(&port->waitqueue);
  771. break;
  772. case VIRTIO_CONSOLE_PORT_NAME:
  773. /*
  774. * Skip the size of the header and the cpkt to get the size
  775. * of the name that was sent
  776. */
  777. name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
  778. port->name = kmalloc(name_size, GFP_KERNEL);
  779. if (!port->name) {
  780. dev_err(port->dev,
  781. "Not enough space to store port name\n");
  782. break;
  783. }
  784. strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
  785. name_size - 1);
  786. port->name[name_size - 1] = 0;
  787. /*
  788. * Since we only have one sysfs attribute, 'name',
  789. * create it only if we have a name for the port.
  790. */
  791. err = sysfs_create_group(&port->dev->kobj,
  792. &port_attribute_group);
  793. if (err) {
  794. dev_err(port->dev,
  795. "Error %d creating sysfs device attributes\n",
  796. err);
  797. } else {
  798. /*
  799. * Generate a udev event so that appropriate
  800. * symlinks can be created based on udev
  801. * rules.
  802. */
  803. kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
  804. }
  805. break;
  806. case VIRTIO_CONSOLE_PORT_REMOVE:
  807. /*
  808. * Hot unplug the port. We don't decrement nr_ports
  809. * since we don't want to deal with extra complexities
  810. * of using the lowest-available port id: We can just
  811. * pick up the nr_ports number as the id and not have
  812. * userspace send it to us. This helps us in two
  813. * ways:
  814. *
  815. * - We don't need to have a 'port_id' field in the
  816. * config space when a port is hot-added. This is a
  817. * good thing as we might queue up multiple hotplug
  818. * requests issued in our workqueue.
  819. *
  820. * - Another way to deal with this would have been to
  821. * use a bitmap of the active ports and select the
  822. * lowest non-active port from that map. That
  823. * bloats the already tight config space and we
  824. * would end up artificially limiting the
  825. * max. number of ports to sizeof(bitmap). Right
  826. * now we can support 2^32 ports (as the port id is
  827. * stored in a u32 type).
  828. *
  829. */
  830. remove_port(port);
  831. break;
  832. }
  833. }
  834. static void control_work_handler(struct work_struct *work)
  835. {
  836. struct ports_device *portdev;
  837. struct virtqueue *vq;
  838. struct port_buffer *buf;
  839. unsigned int len;
  840. portdev = container_of(work, struct ports_device, control_work);
  841. vq = portdev->c_ivq;
  842. spin_lock(&portdev->cvq_lock);
  843. while ((buf = vq->vq_ops->get_buf(vq, &len))) {
  844. spin_unlock(&portdev->cvq_lock);
  845. buf->len = len;
  846. buf->offset = 0;
  847. handle_control_message(portdev, buf);
  848. spin_lock(&portdev->cvq_lock);
  849. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  850. dev_warn(&portdev->vdev->dev,
  851. "Error adding buffer to queue\n");
  852. free_buf(buf);
  853. }
  854. }
  855. spin_unlock(&portdev->cvq_lock);
  856. }
  857. static void in_intr(struct virtqueue *vq)
  858. {
  859. struct port *port;
  860. unsigned long flags;
  861. port = find_port_by_vq(vq->vdev->priv, vq);
  862. if (!port)
  863. return;
  864. spin_lock_irqsave(&port->inbuf_lock, flags);
  865. if (!port->inbuf)
  866. port->inbuf = get_inbuf(port);
  867. /*
  868. * Don't queue up data when port is closed. This condition
  869. * can be reached when a console port is not yet connected (no
  870. * tty is spawned) and the host sends out data to console
  871. * ports. For generic serial ports, the host won't
  872. * (shouldn't) send data till the guest is connected.
  873. */
  874. if (!port->guest_connected)
  875. discard_port_data(port);
  876. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  877. wake_up_interruptible(&port->waitqueue);
  878. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  879. hvc_kick();
  880. }
  881. static void control_intr(struct virtqueue *vq)
  882. {
  883. struct ports_device *portdev;
  884. portdev = vq->vdev->priv;
  885. schedule_work(&portdev->control_work);
  886. }
  887. static void config_intr(struct virtio_device *vdev)
  888. {
  889. struct ports_device *portdev;
  890. portdev = vdev->priv;
  891. if (use_multiport(portdev)) {
  892. /* Handle port hot-add */
  893. schedule_work(&portdev->config_work);
  894. }
  895. /*
  896. * We'll use this way of resizing only for legacy support.
  897. * For newer userspace (VIRTIO_CONSOLE_F_MULTPORT+), use
  898. * control messages to indicate console size changes so that
  899. * it can be done per-port
  900. */
  901. resize_console(find_port_by_id(portdev, 0));
  902. }
  903. static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
  904. {
  905. struct port_buffer *buf;
  906. unsigned int nr_added_bufs;
  907. int ret;
  908. nr_added_bufs = 0;
  909. do {
  910. buf = alloc_buf(PAGE_SIZE);
  911. if (!buf)
  912. break;
  913. spin_lock_irq(lock);
  914. ret = add_inbuf(vq, buf);
  915. if (ret < 0) {
  916. spin_unlock_irq(lock);
  917. free_buf(buf);
  918. break;
  919. }
  920. nr_added_bufs++;
  921. spin_unlock_irq(lock);
  922. } while (ret > 0);
  923. return nr_added_bufs;
  924. }
  925. static int add_port(struct ports_device *portdev, u32 id)
  926. {
  927. char debugfs_name[16];
  928. struct port *port;
  929. struct port_buffer *buf;
  930. dev_t devt;
  931. unsigned int nr_added_bufs;
  932. int err;
  933. port = kmalloc(sizeof(*port), GFP_KERNEL);
  934. if (!port) {
  935. err = -ENOMEM;
  936. goto fail;
  937. }
  938. port->portdev = portdev;
  939. port->id = id;
  940. port->name = NULL;
  941. port->inbuf = NULL;
  942. port->cons.hvc = NULL;
  943. port->host_connected = port->guest_connected = false;
  944. port->in_vq = portdev->in_vqs[port->id];
  945. port->out_vq = portdev->out_vqs[port->id];
  946. cdev_init(&port->cdev, &port_fops);
  947. devt = MKDEV(portdev->chr_major, id);
  948. err = cdev_add(&port->cdev, devt, 1);
  949. if (err < 0) {
  950. dev_err(&port->portdev->vdev->dev,
  951. "Error %d adding cdev for port %u\n", err, id);
  952. goto free_port;
  953. }
  954. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  955. devt, port, "vport%up%u",
  956. port->portdev->drv_index, id);
  957. if (IS_ERR(port->dev)) {
  958. err = PTR_ERR(port->dev);
  959. dev_err(&port->portdev->vdev->dev,
  960. "Error %d creating device for port %u\n",
  961. err, id);
  962. goto free_cdev;
  963. }
  964. spin_lock_init(&port->inbuf_lock);
  965. init_waitqueue_head(&port->waitqueue);
  966. /* Fill the in_vq with buffers so the host can send us data. */
  967. nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
  968. if (!nr_added_bufs) {
  969. dev_err(port->dev, "Error allocating inbufs\n");
  970. err = -ENOMEM;
  971. goto free_device;
  972. }
  973. /*
  974. * If we're not using multiport support, this has to be a console port
  975. */
  976. if (!use_multiport(port->portdev)) {
  977. err = init_port_console(port);
  978. if (err)
  979. goto free_inbufs;
  980. }
  981. spin_lock_irq(&portdev->ports_lock);
  982. list_add_tail(&port->list, &port->portdev->ports);
  983. spin_unlock_irq(&portdev->ports_lock);
  984. /*
  985. * Tell the Host we're set so that it can send us various
  986. * configuration parameters for this port (eg, port name,
  987. * caching, whether this is a console port, etc.)
  988. */
  989. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  990. if (pdrvdata.debugfs_dir) {
  991. /*
  992. * Finally, create the debugfs file that we can use to
  993. * inspect a port's state at any time
  994. */
  995. sprintf(debugfs_name, "vport%up%u",
  996. port->portdev->drv_index, id);
  997. port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
  998. pdrvdata.debugfs_dir,
  999. port,
  1000. &port_debugfs_ops);
  1001. }
  1002. return 0;
  1003. free_inbufs:
  1004. while ((buf = port->in_vq->vq_ops->detach_unused_buf(port->in_vq)))
  1005. free_buf(buf);
  1006. free_device:
  1007. device_destroy(pdrvdata.class, port->dev->devt);
  1008. free_cdev:
  1009. cdev_del(&port->cdev);
  1010. free_port:
  1011. kfree(port);
  1012. fail:
  1013. return err;
  1014. }
  1015. /*
  1016. * The workhandler for config-space updates.
  1017. *
  1018. * This is called when ports are hot-added.
  1019. */
  1020. static void config_work_handler(struct work_struct *work)
  1021. {
  1022. struct virtio_console_config virtconconf;
  1023. struct ports_device *portdev;
  1024. struct virtio_device *vdev;
  1025. int err;
  1026. portdev = container_of(work, struct ports_device, config_work);
  1027. vdev = portdev->vdev;
  1028. vdev->config->get(vdev,
  1029. offsetof(struct virtio_console_config, nr_ports),
  1030. &virtconconf.nr_ports,
  1031. sizeof(virtconconf.nr_ports));
  1032. if (portdev->config.nr_ports == virtconconf.nr_ports) {
  1033. /*
  1034. * Port 0 got hot-added. Since we already did all the
  1035. * other initialisation for it, just tell the Host
  1036. * that the port is ready if we find the port. In
  1037. * case the port was hot-removed earlier, we call
  1038. * add_port to add the port.
  1039. */
  1040. struct port *port;
  1041. port = find_port_by_id(portdev, 0);
  1042. if (!port)
  1043. add_port(portdev, 0);
  1044. else
  1045. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1046. return;
  1047. }
  1048. if (virtconconf.nr_ports > portdev->config.max_nr_ports) {
  1049. dev_warn(&vdev->dev,
  1050. "More ports specified (%u) than allowed (%u)",
  1051. portdev->config.nr_ports + 1,
  1052. portdev->config.max_nr_ports);
  1053. return;
  1054. }
  1055. if (virtconconf.nr_ports < portdev->config.nr_ports)
  1056. return;
  1057. /* Hot-add ports */
  1058. while (virtconconf.nr_ports - portdev->config.nr_ports) {
  1059. err = add_port(portdev, portdev->config.nr_ports);
  1060. if (err)
  1061. break;
  1062. portdev->config.nr_ports++;
  1063. }
  1064. }
  1065. static int init_vqs(struct ports_device *portdev)
  1066. {
  1067. vq_callback_t **io_callbacks;
  1068. char **io_names;
  1069. struct virtqueue **vqs;
  1070. u32 i, j, nr_ports, nr_queues;
  1071. int err;
  1072. nr_ports = portdev->config.max_nr_ports;
  1073. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  1074. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  1075. if (!vqs) {
  1076. err = -ENOMEM;
  1077. goto fail;
  1078. }
  1079. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  1080. if (!io_callbacks) {
  1081. err = -ENOMEM;
  1082. goto free_vqs;
  1083. }
  1084. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  1085. if (!io_names) {
  1086. err = -ENOMEM;
  1087. goto free_callbacks;
  1088. }
  1089. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1090. GFP_KERNEL);
  1091. if (!portdev->in_vqs) {
  1092. err = -ENOMEM;
  1093. goto free_names;
  1094. }
  1095. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1096. GFP_KERNEL);
  1097. if (!portdev->out_vqs) {
  1098. err = -ENOMEM;
  1099. goto free_invqs;
  1100. }
  1101. /*
  1102. * For backward compat (newer host but older guest), the host
  1103. * spawns a console port first and also inits the vqs for port
  1104. * 0 before others.
  1105. */
  1106. j = 0;
  1107. io_callbacks[j] = in_intr;
  1108. io_callbacks[j + 1] = NULL;
  1109. io_names[j] = "input";
  1110. io_names[j + 1] = "output";
  1111. j += 2;
  1112. if (use_multiport(portdev)) {
  1113. io_callbacks[j] = control_intr;
  1114. io_callbacks[j + 1] = NULL;
  1115. io_names[j] = "control-i";
  1116. io_names[j + 1] = "control-o";
  1117. for (i = 1; i < nr_ports; i++) {
  1118. j += 2;
  1119. io_callbacks[j] = in_intr;
  1120. io_callbacks[j + 1] = NULL;
  1121. io_names[j] = "input";
  1122. io_names[j + 1] = "output";
  1123. }
  1124. }
  1125. /* Find the queues. */
  1126. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  1127. io_callbacks,
  1128. (const char **)io_names);
  1129. if (err)
  1130. goto free_outvqs;
  1131. j = 0;
  1132. portdev->in_vqs[0] = vqs[0];
  1133. portdev->out_vqs[0] = vqs[1];
  1134. j += 2;
  1135. if (use_multiport(portdev)) {
  1136. portdev->c_ivq = vqs[j];
  1137. portdev->c_ovq = vqs[j + 1];
  1138. for (i = 1; i < nr_ports; i++) {
  1139. j += 2;
  1140. portdev->in_vqs[i] = vqs[j];
  1141. portdev->out_vqs[i] = vqs[j + 1];
  1142. }
  1143. }
  1144. kfree(io_callbacks);
  1145. kfree(io_names);
  1146. kfree(vqs);
  1147. return 0;
  1148. free_names:
  1149. kfree(io_names);
  1150. free_callbacks:
  1151. kfree(io_callbacks);
  1152. free_outvqs:
  1153. kfree(portdev->out_vqs);
  1154. free_invqs:
  1155. kfree(portdev->in_vqs);
  1156. free_vqs:
  1157. kfree(vqs);
  1158. fail:
  1159. return err;
  1160. }
  1161. static const struct file_operations portdev_fops = {
  1162. .owner = THIS_MODULE,
  1163. };
  1164. /*
  1165. * Once we're further in boot, we get probed like any other virtio
  1166. * device.
  1167. *
  1168. * If the host also supports multiple console ports, we check the
  1169. * config space to see how many ports the host has spawned. We
  1170. * initialize each port found.
  1171. */
  1172. static int __devinit virtcons_probe(struct virtio_device *vdev)
  1173. {
  1174. struct ports_device *portdev;
  1175. u32 i;
  1176. int err;
  1177. bool multiport;
  1178. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  1179. if (!portdev) {
  1180. err = -ENOMEM;
  1181. goto fail;
  1182. }
  1183. /* Attach this portdev to this virtio_device, and vice-versa. */
  1184. portdev->vdev = vdev;
  1185. vdev->priv = portdev;
  1186. spin_lock_irq(&pdrvdata_lock);
  1187. portdev->drv_index = pdrvdata.index++;
  1188. spin_unlock_irq(&pdrvdata_lock);
  1189. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  1190. &portdev_fops);
  1191. if (portdev->chr_major < 0) {
  1192. dev_err(&vdev->dev,
  1193. "Error %d registering chrdev for device %u\n",
  1194. portdev->chr_major, portdev->drv_index);
  1195. err = portdev->chr_major;
  1196. goto free;
  1197. }
  1198. multiport = false;
  1199. portdev->config.nr_ports = 1;
  1200. portdev->config.max_nr_ports = 1;
  1201. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
  1202. multiport = true;
  1203. vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
  1204. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  1205. nr_ports),
  1206. &portdev->config.nr_ports,
  1207. sizeof(portdev->config.nr_ports));
  1208. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  1209. max_nr_ports),
  1210. &portdev->config.max_nr_ports,
  1211. sizeof(portdev->config.max_nr_ports));
  1212. if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
  1213. dev_warn(&vdev->dev,
  1214. "More ports (%u) specified than allowed (%u). Will init %u ports.",
  1215. portdev->config.nr_ports,
  1216. portdev->config.max_nr_ports,
  1217. portdev->config.max_nr_ports);
  1218. portdev->config.nr_ports = portdev->config.max_nr_ports;
  1219. }
  1220. }
  1221. /* Let the Host know we support multiple ports.*/
  1222. vdev->config->finalize_features(vdev);
  1223. err = init_vqs(portdev);
  1224. if (err < 0) {
  1225. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  1226. goto free_chrdev;
  1227. }
  1228. spin_lock_init(&portdev->ports_lock);
  1229. INIT_LIST_HEAD(&portdev->ports);
  1230. if (multiport) {
  1231. unsigned int nr_added_bufs;
  1232. spin_lock_init(&portdev->cvq_lock);
  1233. INIT_WORK(&portdev->control_work, &control_work_handler);
  1234. INIT_WORK(&portdev->config_work, &config_work_handler);
  1235. nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  1236. if (!nr_added_bufs) {
  1237. dev_err(&vdev->dev,
  1238. "Error allocating buffers for control queue\n");
  1239. err = -ENOMEM;
  1240. goto free_vqs;
  1241. }
  1242. }
  1243. for (i = 0; i < portdev->config.nr_ports; i++)
  1244. add_port(portdev, i);
  1245. /* Start using the new console output. */
  1246. early_put_chars = NULL;
  1247. return 0;
  1248. free_vqs:
  1249. vdev->config->del_vqs(vdev);
  1250. kfree(portdev->in_vqs);
  1251. kfree(portdev->out_vqs);
  1252. free_chrdev:
  1253. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1254. free:
  1255. kfree(portdev);
  1256. fail:
  1257. return err;
  1258. }
  1259. static void virtcons_remove(struct virtio_device *vdev)
  1260. {
  1261. struct ports_device *portdev;
  1262. struct port *port, *port2;
  1263. struct port_buffer *buf;
  1264. unsigned int len;
  1265. portdev = vdev->priv;
  1266. cancel_work_sync(&portdev->control_work);
  1267. cancel_work_sync(&portdev->config_work);
  1268. list_for_each_entry_safe(port, port2, &portdev->ports, list)
  1269. remove_port(port);
  1270. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1271. while ((buf = portdev->c_ivq->vq_ops->get_buf(portdev->c_ivq, &len)))
  1272. free_buf(buf);
  1273. while ((buf = portdev->c_ivq->vq_ops->detach_unused_buf(portdev->c_ivq)))
  1274. free_buf(buf);
  1275. vdev->config->del_vqs(vdev);
  1276. kfree(portdev->in_vqs);
  1277. kfree(portdev->out_vqs);
  1278. kfree(portdev);
  1279. }
  1280. static struct virtio_device_id id_table[] = {
  1281. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  1282. { 0 },
  1283. };
  1284. static unsigned int features[] = {
  1285. VIRTIO_CONSOLE_F_SIZE,
  1286. VIRTIO_CONSOLE_F_MULTIPORT,
  1287. };
  1288. static struct virtio_driver virtio_console = {
  1289. .feature_table = features,
  1290. .feature_table_size = ARRAY_SIZE(features),
  1291. .driver.name = KBUILD_MODNAME,
  1292. .driver.owner = THIS_MODULE,
  1293. .id_table = id_table,
  1294. .probe = virtcons_probe,
  1295. .remove = virtcons_remove,
  1296. .config_changed = config_intr,
  1297. };
  1298. static int __init init(void)
  1299. {
  1300. int err;
  1301. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  1302. if (IS_ERR(pdrvdata.class)) {
  1303. err = PTR_ERR(pdrvdata.class);
  1304. pr_err("Error %d creating virtio-ports class\n", err);
  1305. return err;
  1306. }
  1307. pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
  1308. if (!pdrvdata.debugfs_dir) {
  1309. pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
  1310. PTR_ERR(pdrvdata.debugfs_dir));
  1311. }
  1312. INIT_LIST_HEAD(&pdrvdata.consoles);
  1313. return register_virtio_driver(&virtio_console);
  1314. }
  1315. static void __exit fini(void)
  1316. {
  1317. unregister_virtio_driver(&virtio_console);
  1318. class_destroy(pdrvdata.class);
  1319. if (pdrvdata.debugfs_dir)
  1320. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1321. }
  1322. module_init(init);
  1323. module_exit(fini);
  1324. MODULE_DEVICE_TABLE(virtio, id_table);
  1325. MODULE_DESCRIPTION("Virtio console driver");
  1326. MODULE_LICENSE("GPL");