virtio_console.c 55 KB

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