virtio_console.c 55 KB

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