virtio_console.c 41 KB

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