virtio_console.c 39 KB

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