virtio_console.c 49 KB

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