virtio_console.c 44 KB

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