virtio_console.c 47 KB

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