virtio_console.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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. cpu_relax();
  481. }
  482. spin_unlock(&portdev->c_ovq_lock);
  483. return 0;
  484. }
  485. static ssize_t send_control_msg(struct port *port, unsigned int event,
  486. unsigned int value)
  487. {
  488. /* Did the port get unplugged before userspace closed it? */
  489. if (port->portdev)
  490. return __send_control_msg(port->portdev, port->id, event, value);
  491. return 0;
  492. }
  493. /* Callers must take the port->outvq_lock */
  494. static void reclaim_consumed_buffers(struct port *port)
  495. {
  496. struct port_buffer *buf;
  497. unsigned int len;
  498. if (!port->portdev) {
  499. /* Device has been unplugged. vqs are already gone. */
  500. return;
  501. }
  502. while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
  503. free_buf(buf, false);
  504. port->outvq_full = false;
  505. }
  506. }
  507. static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
  508. int nents, size_t in_count,
  509. void *data, bool nonblock)
  510. {
  511. struct virtqueue *out_vq;
  512. int err;
  513. unsigned long flags;
  514. unsigned int len;
  515. out_vq = port->out_vq;
  516. spin_lock_irqsave(&port->outvq_lock, flags);
  517. reclaim_consumed_buffers(port);
  518. err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
  519. /* Tell Host to go! */
  520. virtqueue_kick(out_vq);
  521. if (err) {
  522. in_count = 0;
  523. goto done;
  524. }
  525. if (out_vq->num_free == 0)
  526. port->outvq_full = true;
  527. if (nonblock)
  528. goto done;
  529. /*
  530. * Wait till the host acknowledges it pushed out the data we
  531. * sent. This is done for data from the hvc_console; the tty
  532. * operations are performed with spinlocks held so we can't
  533. * sleep here. An alternative would be to copy the data to a
  534. * buffer and relax the spinning requirement. The downside is
  535. * we need to kmalloc a GFP_ATOMIC buffer each time the
  536. * console driver writes something out.
  537. */
  538. while (!virtqueue_get_buf(out_vq, &len))
  539. cpu_relax();
  540. done:
  541. spin_unlock_irqrestore(&port->outvq_lock, flags);
  542. port->stats.bytes_sent += in_count;
  543. /*
  544. * We're expected to return the amount of data we wrote -- all
  545. * of it
  546. */
  547. return in_count;
  548. }
  549. /*
  550. * Give out the data that's requested from the buffer that we have
  551. * queued up.
  552. */
  553. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
  554. bool to_user)
  555. {
  556. struct port_buffer *buf;
  557. unsigned long flags;
  558. if (!out_count || !port_has_data(port))
  559. return 0;
  560. buf = port->inbuf;
  561. out_count = min(out_count, buf->len - buf->offset);
  562. if (to_user) {
  563. ssize_t ret;
  564. ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
  565. if (ret)
  566. return -EFAULT;
  567. } else {
  568. memcpy(out_buf, buf->buf + buf->offset, out_count);
  569. }
  570. buf->offset += out_count;
  571. if (buf->offset == buf->len) {
  572. /*
  573. * We're done using all the data in this buffer.
  574. * Re-queue so that the Host can send us more data.
  575. */
  576. spin_lock_irqsave(&port->inbuf_lock, flags);
  577. port->inbuf = NULL;
  578. if (add_inbuf(port->in_vq, buf) < 0)
  579. dev_warn(port->dev, "failed add_buf\n");
  580. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  581. }
  582. /* Return the number of bytes actually copied */
  583. return out_count;
  584. }
  585. /* The condition that must be true for polling to end */
  586. static bool will_read_block(struct port *port)
  587. {
  588. if (!port->guest_connected) {
  589. /* Port got hot-unplugged. Let's exit. */
  590. return false;
  591. }
  592. return !port_has_data(port) && port->host_connected;
  593. }
  594. static bool will_write_block(struct port *port)
  595. {
  596. bool ret;
  597. if (!port->guest_connected) {
  598. /* Port got hot-unplugged. Let's exit. */
  599. return false;
  600. }
  601. if (!port->host_connected)
  602. return true;
  603. spin_lock_irq(&port->outvq_lock);
  604. /*
  605. * Check if the Host has consumed any buffers since we last
  606. * sent data (this is only applicable for nonblocking ports).
  607. */
  608. reclaim_consumed_buffers(port);
  609. ret = port->outvq_full;
  610. spin_unlock_irq(&port->outvq_lock);
  611. return ret;
  612. }
  613. static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
  614. size_t count, loff_t *offp)
  615. {
  616. struct port *port;
  617. ssize_t ret;
  618. port = filp->private_data;
  619. /* Port is hot-unplugged. */
  620. if (!port->guest_connected)
  621. return -ENODEV;
  622. if (!port_has_data(port)) {
  623. /*
  624. * If nothing's connected on the host just return 0 in
  625. * case of list_empty; this tells the userspace app
  626. * that there's no connection
  627. */
  628. if (!port->host_connected)
  629. return 0;
  630. if (filp->f_flags & O_NONBLOCK)
  631. return -EAGAIN;
  632. ret = wait_event_freezable(port->waitqueue,
  633. !will_read_block(port));
  634. if (ret < 0)
  635. return ret;
  636. }
  637. /* Port got hot-unplugged while we were waiting above. */
  638. if (!port->guest_connected)
  639. return -ENODEV;
  640. /*
  641. * We could've received a disconnection message while we were
  642. * waiting for more data.
  643. *
  644. * This check is not clubbed in the if() statement above as we
  645. * might receive some data as well as the host could get
  646. * disconnected after we got woken up from our wait. So we
  647. * really want to give off whatever data we have and only then
  648. * check for host_connected.
  649. */
  650. if (!port_has_data(port) && !port->host_connected)
  651. return 0;
  652. return fill_readbuf(port, ubuf, count, true);
  653. }
  654. static int wait_port_writable(struct port *port, bool nonblock)
  655. {
  656. int ret;
  657. if (will_write_block(port)) {
  658. if (nonblock)
  659. return -EAGAIN;
  660. ret = wait_event_freezable(port->waitqueue,
  661. !will_write_block(port));
  662. if (ret < 0)
  663. return ret;
  664. }
  665. /* Port got hot-unplugged. */
  666. if (!port->guest_connected)
  667. return -ENODEV;
  668. return 0;
  669. }
  670. static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
  671. size_t count, loff_t *offp)
  672. {
  673. struct port *port;
  674. struct port_buffer *buf;
  675. ssize_t ret;
  676. bool nonblock;
  677. struct scatterlist sg[1];
  678. /* Userspace could be out to fool us */
  679. if (!count)
  680. return 0;
  681. port = filp->private_data;
  682. nonblock = filp->f_flags & O_NONBLOCK;
  683. ret = wait_port_writable(port, nonblock);
  684. if (ret < 0)
  685. return ret;
  686. count = min((size_t)(32 * 1024), count);
  687. buf = alloc_buf(port->out_vq, count, 0);
  688. if (!buf)
  689. return -ENOMEM;
  690. ret = copy_from_user(buf->buf, ubuf, count);
  691. if (ret) {
  692. ret = -EFAULT;
  693. goto free_buf;
  694. }
  695. /*
  696. * We now ask send_buf() to not spin for generic ports -- we
  697. * can re-use the same code path that non-blocking file
  698. * descriptors take for blocking file descriptors since the
  699. * wait is already done and we're certain the write will go
  700. * through to the host.
  701. */
  702. nonblock = true;
  703. sg_init_one(sg, buf->buf, count);
  704. ret = __send_to_port(port, sg, 1, count, buf, nonblock);
  705. if (nonblock && ret > 0)
  706. goto out;
  707. free_buf:
  708. free_buf(buf, true);
  709. out:
  710. return ret;
  711. }
  712. struct sg_list {
  713. unsigned int n;
  714. unsigned int size;
  715. size_t len;
  716. struct scatterlist *sg;
  717. };
  718. static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  719. struct splice_desc *sd)
  720. {
  721. struct sg_list *sgl = sd->u.data;
  722. unsigned int offset, len;
  723. if (sgl->n == sgl->size)
  724. return 0;
  725. /* Try lock this page */
  726. if (buf->ops->steal(pipe, buf) == 0) {
  727. /* Get reference and unlock page for moving */
  728. get_page(buf->page);
  729. unlock_page(buf->page);
  730. len = min(buf->len, sd->len);
  731. sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
  732. } else {
  733. /* Failback to copying a page */
  734. struct page *page = alloc_page(GFP_KERNEL);
  735. char *src = buf->ops->map(pipe, buf, 1);
  736. char *dst;
  737. if (!page)
  738. return -ENOMEM;
  739. dst = kmap(page);
  740. offset = sd->pos & ~PAGE_MASK;
  741. len = sd->len;
  742. if (len + offset > PAGE_SIZE)
  743. len = PAGE_SIZE - offset;
  744. memcpy(dst + offset, src + buf->offset, len);
  745. kunmap(page);
  746. buf->ops->unmap(pipe, buf, src);
  747. sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
  748. }
  749. sgl->n++;
  750. sgl->len += len;
  751. return len;
  752. }
  753. /* Faster zero-copy write by splicing */
  754. static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
  755. struct file *filp, loff_t *ppos,
  756. size_t len, unsigned int flags)
  757. {
  758. struct port *port = filp->private_data;
  759. struct sg_list sgl;
  760. ssize_t ret;
  761. struct port_buffer *buf;
  762. struct splice_desc sd = {
  763. .total_len = len,
  764. .flags = flags,
  765. .pos = *ppos,
  766. .u.data = &sgl,
  767. };
  768. /*
  769. * Rproc_serial does not yet support splice. To support splice
  770. * pipe_to_sg() must allocate dma-buffers and copy content from
  771. * regular pages to dma pages. And alloc_buf and free_buf must
  772. * support allocating and freeing such a list of dma-buffers.
  773. */
  774. if (is_rproc_serial(port->out_vq->vdev))
  775. return -EINVAL;
  776. /*
  777. * pipe->nrbufs == 0 means there are no data to transfer,
  778. * so this returns just 0 for no data.
  779. */
  780. pipe_lock(pipe);
  781. if (!pipe->nrbufs) {
  782. ret = 0;
  783. goto error_out;
  784. }
  785. ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
  786. if (ret < 0)
  787. goto error_out;
  788. buf = alloc_buf(port->out_vq, 0, pipe->nrbufs);
  789. if (!buf) {
  790. ret = -ENOMEM;
  791. goto error_out;
  792. }
  793. sgl.n = 0;
  794. sgl.len = 0;
  795. sgl.size = pipe->nrbufs;
  796. sgl.sg = buf->sg;
  797. sg_init_table(sgl.sg, sgl.size);
  798. ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
  799. pipe_unlock(pipe);
  800. if (likely(ret > 0))
  801. ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
  802. if (unlikely(ret <= 0))
  803. free_buf(buf, true);
  804. return ret;
  805. error_out:
  806. pipe_unlock(pipe);
  807. return ret;
  808. }
  809. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  810. {
  811. struct port *port;
  812. unsigned int ret;
  813. port = filp->private_data;
  814. poll_wait(filp, &port->waitqueue, wait);
  815. if (!port->guest_connected) {
  816. /* Port got unplugged */
  817. return POLLHUP;
  818. }
  819. ret = 0;
  820. if (!will_read_block(port))
  821. ret |= POLLIN | POLLRDNORM;
  822. if (!will_write_block(port))
  823. ret |= POLLOUT;
  824. if (!port->host_connected)
  825. ret |= POLLHUP;
  826. return ret;
  827. }
  828. static void remove_port(struct kref *kref);
  829. static int port_fops_release(struct inode *inode, struct file *filp)
  830. {
  831. struct port *port;
  832. port = filp->private_data;
  833. /* Notify host of port being closed */
  834. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  835. spin_lock_irq(&port->inbuf_lock);
  836. port->guest_connected = false;
  837. discard_port_data(port);
  838. spin_unlock_irq(&port->inbuf_lock);
  839. spin_lock_irq(&port->outvq_lock);
  840. reclaim_consumed_buffers(port);
  841. spin_unlock_irq(&port->outvq_lock);
  842. reclaim_dma_bufs();
  843. /*
  844. * Locks aren't necessary here as a port can't be opened after
  845. * unplug, and if a port isn't unplugged, a kref would already
  846. * exist for the port. Plus, taking ports_lock here would
  847. * create a dependency on other locks taken by functions
  848. * inside remove_port if we're the last holder of the port,
  849. * creating many problems.
  850. */
  851. kref_put(&port->kref, remove_port);
  852. return 0;
  853. }
  854. static int port_fops_open(struct inode *inode, struct file *filp)
  855. {
  856. struct cdev *cdev = inode->i_cdev;
  857. struct port *port;
  858. int ret;
  859. /* We get the port with a kref here */
  860. port = find_port_by_devt(cdev->dev);
  861. if (!port) {
  862. /* Port was unplugged before we could proceed */
  863. return -ENXIO;
  864. }
  865. filp->private_data = port;
  866. /*
  867. * Don't allow opening of console port devices -- that's done
  868. * via /dev/hvc
  869. */
  870. if (is_console_port(port)) {
  871. ret = -ENXIO;
  872. goto out;
  873. }
  874. /* Allow only one process to open a particular port at a time */
  875. spin_lock_irq(&port->inbuf_lock);
  876. if (port->guest_connected) {
  877. spin_unlock_irq(&port->inbuf_lock);
  878. ret = -EBUSY;
  879. goto out;
  880. }
  881. port->guest_connected = true;
  882. spin_unlock_irq(&port->inbuf_lock);
  883. spin_lock_irq(&port->outvq_lock);
  884. /*
  885. * There might be a chance that we missed reclaiming a few
  886. * buffers in the window of the port getting previously closed
  887. * and opening now.
  888. */
  889. reclaim_consumed_buffers(port);
  890. spin_unlock_irq(&port->outvq_lock);
  891. nonseekable_open(inode, filp);
  892. /* Notify host of port being opened */
  893. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  894. return 0;
  895. out:
  896. kref_put(&port->kref, remove_port);
  897. return ret;
  898. }
  899. static int port_fops_fasync(int fd, struct file *filp, int mode)
  900. {
  901. struct port *port;
  902. port = filp->private_data;
  903. return fasync_helper(fd, filp, mode, &port->async_queue);
  904. }
  905. /*
  906. * The file operations that we support: programs in the guest can open
  907. * a console device, read from it, write to it, poll for data and
  908. * close it. The devices are at
  909. * /dev/vport<device number>p<port number>
  910. */
  911. static const struct file_operations port_fops = {
  912. .owner = THIS_MODULE,
  913. .open = port_fops_open,
  914. .read = port_fops_read,
  915. .write = port_fops_write,
  916. .splice_write = port_fops_splice_write,
  917. .poll = port_fops_poll,
  918. .release = port_fops_release,
  919. .fasync = port_fops_fasync,
  920. .llseek = no_llseek,
  921. };
  922. /*
  923. * The put_chars() callback is pretty straightforward.
  924. *
  925. * We turn the characters into a scatter-gather list, add it to the
  926. * output queue and then kick the Host. Then we sit here waiting for
  927. * it to finish: inefficient in theory, but in practice
  928. * implementations will do it immediately (lguest's Launcher does).
  929. */
  930. static int put_chars(u32 vtermno, const char *buf, int count)
  931. {
  932. struct port *port;
  933. struct scatterlist sg[1];
  934. if (unlikely(early_put_chars))
  935. return early_put_chars(vtermno, buf, count);
  936. port = find_port_by_vtermno(vtermno);
  937. if (!port)
  938. return -EPIPE;
  939. sg_init_one(sg, buf, count);
  940. return __send_to_port(port, sg, 1, count, (void *)buf, false);
  941. }
  942. /*
  943. * get_chars() is the callback from the hvc_console infrastructure
  944. * when an interrupt is received.
  945. *
  946. * We call out to fill_readbuf that gets us the required data from the
  947. * buffers that are queued up.
  948. */
  949. static int get_chars(u32 vtermno, char *buf, int count)
  950. {
  951. struct port *port;
  952. /* If we've not set up the port yet, we have no input to give. */
  953. if (unlikely(early_put_chars))
  954. return 0;
  955. port = find_port_by_vtermno(vtermno);
  956. if (!port)
  957. return -EPIPE;
  958. /* If we don't have an input queue yet, we can't get input. */
  959. BUG_ON(!port->in_vq);
  960. return fill_readbuf(port, buf, count, false);
  961. }
  962. static void resize_console(struct port *port)
  963. {
  964. struct virtio_device *vdev;
  965. /* The port could have been hot-unplugged */
  966. if (!port || !is_console_port(port))
  967. return;
  968. vdev = port->portdev->vdev;
  969. /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
  970. if (!is_rproc_serial(vdev) &&
  971. virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
  972. hvc_resize(port->cons.hvc, port->cons.ws);
  973. }
  974. /* We set the configuration at this point, since we now have a tty */
  975. static int notifier_add_vio(struct hvc_struct *hp, int data)
  976. {
  977. struct port *port;
  978. port = find_port_by_vtermno(hp->vtermno);
  979. if (!port)
  980. return -EINVAL;
  981. hp->irq_requested = 1;
  982. resize_console(port);
  983. return 0;
  984. }
  985. static void notifier_del_vio(struct hvc_struct *hp, int data)
  986. {
  987. hp->irq_requested = 0;
  988. }
  989. /* The operations for console ports. */
  990. static const struct hv_ops hv_ops = {
  991. .get_chars = get_chars,
  992. .put_chars = put_chars,
  993. .notifier_add = notifier_add_vio,
  994. .notifier_del = notifier_del_vio,
  995. .notifier_hangup = notifier_del_vio,
  996. };
  997. /*
  998. * Console drivers are initialized very early so boot messages can go
  999. * out, so we do things slightly differently from the generic virtio
  1000. * initialization of the net and block drivers.
  1001. *
  1002. * At this stage, the console is output-only. It's too early to set
  1003. * up a virtqueue, so we let the drivers do some boutique early-output
  1004. * thing.
  1005. */
  1006. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  1007. {
  1008. early_put_chars = put_chars;
  1009. return hvc_instantiate(0, 0, &hv_ops);
  1010. }
  1011. static int init_port_console(struct port *port)
  1012. {
  1013. int ret;
  1014. /*
  1015. * The Host's telling us this port is a console port. Hook it
  1016. * up with an hvc console.
  1017. *
  1018. * To set up and manage our virtual console, we call
  1019. * hvc_alloc().
  1020. *
  1021. * The first argument of hvc_alloc() is the virtual console
  1022. * number. The second argument is the parameter for the
  1023. * notification mechanism (like irq number). We currently
  1024. * leave this as zero, virtqueues have implicit notifications.
  1025. *
  1026. * The third argument is a "struct hv_ops" containing the
  1027. * put_chars() get_chars(), notifier_add() and notifier_del()
  1028. * pointers. The final argument is the output buffer size: we
  1029. * can do any size, so we put PAGE_SIZE here.
  1030. */
  1031. port->cons.vtermno = pdrvdata.next_vtermno;
  1032. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  1033. if (IS_ERR(port->cons.hvc)) {
  1034. ret = PTR_ERR(port->cons.hvc);
  1035. dev_err(port->dev,
  1036. "error %d allocating hvc for port\n", ret);
  1037. port->cons.hvc = NULL;
  1038. return ret;
  1039. }
  1040. spin_lock_irq(&pdrvdata_lock);
  1041. pdrvdata.next_vtermno++;
  1042. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  1043. spin_unlock_irq(&pdrvdata_lock);
  1044. port->guest_connected = true;
  1045. /*
  1046. * Start using the new console output if this is the first
  1047. * console to come up.
  1048. */
  1049. if (early_put_chars)
  1050. early_put_chars = NULL;
  1051. /* Notify host of port being opened */
  1052. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  1053. return 0;
  1054. }
  1055. static ssize_t show_port_name(struct device *dev,
  1056. struct device_attribute *attr, char *buffer)
  1057. {
  1058. struct port *port;
  1059. port = dev_get_drvdata(dev);
  1060. return sprintf(buffer, "%s\n", port->name);
  1061. }
  1062. static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
  1063. static struct attribute *port_sysfs_entries[] = {
  1064. &dev_attr_name.attr,
  1065. NULL
  1066. };
  1067. static struct attribute_group port_attribute_group = {
  1068. .name = NULL, /* put in device directory */
  1069. .attrs = port_sysfs_entries,
  1070. };
  1071. static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
  1072. size_t count, loff_t *offp)
  1073. {
  1074. struct port *port;
  1075. char *buf;
  1076. ssize_t ret, out_offset, out_count;
  1077. out_count = 1024;
  1078. buf = kmalloc(out_count, GFP_KERNEL);
  1079. if (!buf)
  1080. return -ENOMEM;
  1081. port = filp->private_data;
  1082. out_offset = 0;
  1083. out_offset += snprintf(buf + out_offset, out_count,
  1084. "name: %s\n", port->name ? port->name : "");
  1085. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1086. "guest_connected: %d\n", port->guest_connected);
  1087. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1088. "host_connected: %d\n", port->host_connected);
  1089. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1090. "outvq_full: %d\n", port->outvq_full);
  1091. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1092. "bytes_sent: %lu\n", port->stats.bytes_sent);
  1093. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1094. "bytes_received: %lu\n",
  1095. port->stats.bytes_received);
  1096. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1097. "bytes_discarded: %lu\n",
  1098. port->stats.bytes_discarded);
  1099. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1100. "is_console: %s\n",
  1101. is_console_port(port) ? "yes" : "no");
  1102. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1103. "console_vtermno: %u\n", port->cons.vtermno);
  1104. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  1105. kfree(buf);
  1106. return ret;
  1107. }
  1108. static const struct file_operations port_debugfs_ops = {
  1109. .owner = THIS_MODULE,
  1110. .open = simple_open,
  1111. .read = debugfs_read,
  1112. };
  1113. static void set_console_size(struct port *port, u16 rows, u16 cols)
  1114. {
  1115. if (!port || !is_console_port(port))
  1116. return;
  1117. port->cons.ws.ws_row = rows;
  1118. port->cons.ws.ws_col = cols;
  1119. }
  1120. static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
  1121. {
  1122. struct port_buffer *buf;
  1123. unsigned int nr_added_bufs;
  1124. int ret;
  1125. nr_added_bufs = 0;
  1126. do {
  1127. buf = alloc_buf(vq, PAGE_SIZE, 0);
  1128. if (!buf)
  1129. break;
  1130. spin_lock_irq(lock);
  1131. ret = add_inbuf(vq, buf);
  1132. if (ret < 0) {
  1133. spin_unlock_irq(lock);
  1134. free_buf(buf, true);
  1135. break;
  1136. }
  1137. nr_added_bufs++;
  1138. spin_unlock_irq(lock);
  1139. } while (ret > 0);
  1140. return nr_added_bufs;
  1141. }
  1142. static void send_sigio_to_port(struct port *port)
  1143. {
  1144. if (port->async_queue && port->guest_connected)
  1145. kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
  1146. }
  1147. static int add_port(struct ports_device *portdev, u32 id)
  1148. {
  1149. char debugfs_name[16];
  1150. struct port *port;
  1151. struct port_buffer *buf;
  1152. dev_t devt;
  1153. unsigned int nr_added_bufs;
  1154. int err;
  1155. port = kmalloc(sizeof(*port), GFP_KERNEL);
  1156. if (!port) {
  1157. err = -ENOMEM;
  1158. goto fail;
  1159. }
  1160. kref_init(&port->kref);
  1161. port->portdev = portdev;
  1162. port->id = id;
  1163. port->name = NULL;
  1164. port->inbuf = NULL;
  1165. port->cons.hvc = NULL;
  1166. port->async_queue = NULL;
  1167. port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
  1168. port->host_connected = port->guest_connected = false;
  1169. port->stats = (struct port_stats) { 0 };
  1170. port->outvq_full = false;
  1171. port->in_vq = portdev->in_vqs[port->id];
  1172. port->out_vq = portdev->out_vqs[port->id];
  1173. port->cdev = cdev_alloc();
  1174. if (!port->cdev) {
  1175. dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
  1176. err = -ENOMEM;
  1177. goto free_port;
  1178. }
  1179. port->cdev->ops = &port_fops;
  1180. devt = MKDEV(portdev->chr_major, id);
  1181. err = cdev_add(port->cdev, devt, 1);
  1182. if (err < 0) {
  1183. dev_err(&port->portdev->vdev->dev,
  1184. "Error %d adding cdev for port %u\n", err, id);
  1185. goto free_cdev;
  1186. }
  1187. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  1188. devt, port, "vport%up%u",
  1189. port->portdev->vdev->index, id);
  1190. if (IS_ERR(port->dev)) {
  1191. err = PTR_ERR(port->dev);
  1192. dev_err(&port->portdev->vdev->dev,
  1193. "Error %d creating device for port %u\n",
  1194. err, id);
  1195. goto free_cdev;
  1196. }
  1197. spin_lock_init(&port->inbuf_lock);
  1198. spin_lock_init(&port->outvq_lock);
  1199. init_waitqueue_head(&port->waitqueue);
  1200. /* Fill the in_vq with buffers so the host can send us data. */
  1201. nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
  1202. if (!nr_added_bufs) {
  1203. dev_err(port->dev, "Error allocating inbufs\n");
  1204. err = -ENOMEM;
  1205. goto free_device;
  1206. }
  1207. if (is_rproc_serial(port->portdev->vdev))
  1208. /*
  1209. * For rproc_serial assume remote processor is connected.
  1210. * rproc_serial does not want the console port, only
  1211. * the generic port implementation.
  1212. */
  1213. port->host_connected = true;
  1214. else if (!use_multiport(port->portdev)) {
  1215. /*
  1216. * If we're not using multiport support,
  1217. * this has to be a console port.
  1218. */
  1219. err = init_port_console(port);
  1220. if (err)
  1221. goto free_inbufs;
  1222. }
  1223. spin_lock_irq(&portdev->ports_lock);
  1224. list_add_tail(&port->list, &port->portdev->ports);
  1225. spin_unlock_irq(&portdev->ports_lock);
  1226. /*
  1227. * Tell the Host we're set so that it can send us various
  1228. * configuration parameters for this port (eg, port name,
  1229. * caching, whether this is a console port, etc.)
  1230. */
  1231. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1232. if (pdrvdata.debugfs_dir) {
  1233. /*
  1234. * Finally, create the debugfs file that we can use to
  1235. * inspect a port's state at any time
  1236. */
  1237. sprintf(debugfs_name, "vport%up%u",
  1238. port->portdev->vdev->index, id);
  1239. port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
  1240. pdrvdata.debugfs_dir,
  1241. port,
  1242. &port_debugfs_ops);
  1243. }
  1244. return 0;
  1245. free_inbufs:
  1246. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  1247. free_buf(buf, true);
  1248. free_device:
  1249. device_destroy(pdrvdata.class, port->dev->devt);
  1250. free_cdev:
  1251. cdev_del(port->cdev);
  1252. free_port:
  1253. kfree(port);
  1254. fail:
  1255. /* The host might want to notify management sw about port add failure */
  1256. __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
  1257. return err;
  1258. }
  1259. /* No users remain, remove all port-specific data. */
  1260. static void remove_port(struct kref *kref)
  1261. {
  1262. struct port *port;
  1263. port = container_of(kref, struct port, kref);
  1264. kfree(port);
  1265. }
  1266. static void remove_port_data(struct port *port)
  1267. {
  1268. struct port_buffer *buf;
  1269. spin_lock_irq(&port->inbuf_lock);
  1270. /* Remove unused data this port might have received. */
  1271. discard_port_data(port);
  1272. /* Remove buffers we queued up for the Host to send us data in. */
  1273. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  1274. free_buf(buf, true);
  1275. spin_unlock_irq(&port->inbuf_lock);
  1276. spin_lock_irq(&port->outvq_lock);
  1277. reclaim_consumed_buffers(port);
  1278. /* Free pending buffers from the out-queue. */
  1279. while ((buf = virtqueue_detach_unused_buf(port->out_vq)))
  1280. free_buf(buf, true);
  1281. spin_unlock_irq(&port->outvq_lock);
  1282. }
  1283. /*
  1284. * Port got unplugged. Remove port from portdev's list and drop the
  1285. * kref reference. If no userspace has this port opened, it will
  1286. * result in immediate removal the port.
  1287. */
  1288. static void unplug_port(struct port *port)
  1289. {
  1290. spin_lock_irq(&port->portdev->ports_lock);
  1291. list_del(&port->list);
  1292. spin_unlock_irq(&port->portdev->ports_lock);
  1293. spin_lock_irq(&port->inbuf_lock);
  1294. if (port->guest_connected) {
  1295. /* Let the app know the port is going down. */
  1296. send_sigio_to_port(port);
  1297. /* Do this after sigio is actually sent */
  1298. port->guest_connected = false;
  1299. port->host_connected = false;
  1300. wake_up_interruptible(&port->waitqueue);
  1301. }
  1302. spin_unlock_irq(&port->inbuf_lock);
  1303. if (is_console_port(port)) {
  1304. spin_lock_irq(&pdrvdata_lock);
  1305. list_del(&port->cons.list);
  1306. spin_unlock_irq(&pdrvdata_lock);
  1307. hvc_remove(port->cons.hvc);
  1308. }
  1309. remove_port_data(port);
  1310. /*
  1311. * We should just assume the device itself has gone off --
  1312. * else a close on an open port later will try to send out a
  1313. * control message.
  1314. */
  1315. port->portdev = NULL;
  1316. sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
  1317. device_destroy(pdrvdata.class, port->dev->devt);
  1318. cdev_del(port->cdev);
  1319. debugfs_remove(port->debugfs_file);
  1320. kfree(port->name);
  1321. /*
  1322. * Locks around here are not necessary - a port can't be
  1323. * opened after we removed the port struct from ports_list
  1324. * above.
  1325. */
  1326. kref_put(&port->kref, remove_port);
  1327. }
  1328. /* Any private messages that the Host and Guest want to share */
  1329. static void handle_control_message(struct ports_device *portdev,
  1330. struct port_buffer *buf)
  1331. {
  1332. struct virtio_console_control *cpkt;
  1333. struct port *port;
  1334. size_t name_size;
  1335. int err;
  1336. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  1337. port = find_port_by_id(portdev, cpkt->id);
  1338. if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
  1339. /* No valid header at start of buffer. Drop it. */
  1340. dev_dbg(&portdev->vdev->dev,
  1341. "Invalid index %u in control packet\n", cpkt->id);
  1342. return;
  1343. }
  1344. switch (cpkt->event) {
  1345. case VIRTIO_CONSOLE_PORT_ADD:
  1346. if (port) {
  1347. dev_dbg(&portdev->vdev->dev,
  1348. "Port %u already added\n", port->id);
  1349. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1350. break;
  1351. }
  1352. if (cpkt->id >= portdev->config.max_nr_ports) {
  1353. dev_warn(&portdev->vdev->dev,
  1354. "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
  1355. cpkt->id, portdev->config.max_nr_ports - 1);
  1356. break;
  1357. }
  1358. add_port(portdev, cpkt->id);
  1359. break;
  1360. case VIRTIO_CONSOLE_PORT_REMOVE:
  1361. unplug_port(port);
  1362. break;
  1363. case VIRTIO_CONSOLE_CONSOLE_PORT:
  1364. if (!cpkt->value)
  1365. break;
  1366. if (is_console_port(port))
  1367. break;
  1368. init_port_console(port);
  1369. complete(&early_console_added);
  1370. /*
  1371. * Could remove the port here in case init fails - but
  1372. * have to notify the host first.
  1373. */
  1374. break;
  1375. case VIRTIO_CONSOLE_RESIZE: {
  1376. struct {
  1377. __u16 rows;
  1378. __u16 cols;
  1379. } size;
  1380. if (!is_console_port(port))
  1381. break;
  1382. memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
  1383. sizeof(size));
  1384. set_console_size(port, size.rows, size.cols);
  1385. port->cons.hvc->irq_requested = 1;
  1386. resize_console(port);
  1387. break;
  1388. }
  1389. case VIRTIO_CONSOLE_PORT_OPEN:
  1390. port->host_connected = cpkt->value;
  1391. wake_up_interruptible(&port->waitqueue);
  1392. /*
  1393. * If the host port got closed and the host had any
  1394. * unconsumed buffers, we'll be able to reclaim them
  1395. * now.
  1396. */
  1397. spin_lock_irq(&port->outvq_lock);
  1398. reclaim_consumed_buffers(port);
  1399. spin_unlock_irq(&port->outvq_lock);
  1400. /*
  1401. * If the guest is connected, it'll be interested in
  1402. * knowing the host connection state changed.
  1403. */
  1404. spin_lock_irq(&port->inbuf_lock);
  1405. send_sigio_to_port(port);
  1406. spin_unlock_irq(&port->inbuf_lock);
  1407. break;
  1408. case VIRTIO_CONSOLE_PORT_NAME:
  1409. /*
  1410. * If we woke up after hibernation, we can get this
  1411. * again. Skip it in that case.
  1412. */
  1413. if (port->name)
  1414. break;
  1415. /*
  1416. * Skip the size of the header and the cpkt to get the size
  1417. * of the name that was sent
  1418. */
  1419. name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
  1420. port->name = kmalloc(name_size, GFP_KERNEL);
  1421. if (!port->name) {
  1422. dev_err(port->dev,
  1423. "Not enough space to store port name\n");
  1424. break;
  1425. }
  1426. strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
  1427. name_size - 1);
  1428. port->name[name_size - 1] = 0;
  1429. /*
  1430. * Since we only have one sysfs attribute, 'name',
  1431. * create it only if we have a name for the port.
  1432. */
  1433. err = sysfs_create_group(&port->dev->kobj,
  1434. &port_attribute_group);
  1435. if (err) {
  1436. dev_err(port->dev,
  1437. "Error %d creating sysfs device attributes\n",
  1438. err);
  1439. } else {
  1440. /*
  1441. * Generate a udev event so that appropriate
  1442. * symlinks can be created based on udev
  1443. * rules.
  1444. */
  1445. kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
  1446. }
  1447. break;
  1448. }
  1449. }
  1450. static void control_work_handler(struct work_struct *work)
  1451. {
  1452. struct ports_device *portdev;
  1453. struct virtqueue *vq;
  1454. struct port_buffer *buf;
  1455. unsigned int len;
  1456. portdev = container_of(work, struct ports_device, control_work);
  1457. vq = portdev->c_ivq;
  1458. spin_lock(&portdev->c_ivq_lock);
  1459. while ((buf = virtqueue_get_buf(vq, &len))) {
  1460. spin_unlock(&portdev->c_ivq_lock);
  1461. buf->len = len;
  1462. buf->offset = 0;
  1463. handle_control_message(portdev, buf);
  1464. spin_lock(&portdev->c_ivq_lock);
  1465. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  1466. dev_warn(&portdev->vdev->dev,
  1467. "Error adding buffer to queue\n");
  1468. free_buf(buf, false);
  1469. }
  1470. }
  1471. spin_unlock(&portdev->c_ivq_lock);
  1472. }
  1473. static void out_intr(struct virtqueue *vq)
  1474. {
  1475. struct port *port;
  1476. port = find_port_by_vq(vq->vdev->priv, vq);
  1477. if (!port)
  1478. return;
  1479. wake_up_interruptible(&port->waitqueue);
  1480. }
  1481. static void in_intr(struct virtqueue *vq)
  1482. {
  1483. struct port *port;
  1484. unsigned long flags;
  1485. port = find_port_by_vq(vq->vdev->priv, vq);
  1486. if (!port)
  1487. return;
  1488. spin_lock_irqsave(&port->inbuf_lock, flags);
  1489. port->inbuf = get_inbuf(port);
  1490. /*
  1491. * Normally the port should not accept data when the port is
  1492. * closed. For generic serial ports, the host won't (shouldn't)
  1493. * send data till the guest is connected. But this condition
  1494. * can be reached when a console port is not yet connected (no
  1495. * tty is spawned) and the other side sends out data over the
  1496. * vring, or when a remote devices start sending data before
  1497. * the ports are opened.
  1498. *
  1499. * A generic serial port will discard data if not connected,
  1500. * while console ports and rproc-serial ports accepts data at
  1501. * any time. rproc-serial is initiated with guest_connected to
  1502. * false because port_fops_open expects this. Console ports are
  1503. * hooked up with an HVC console and is initialized with
  1504. * guest_connected to true.
  1505. */
  1506. if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
  1507. discard_port_data(port);
  1508. /* Send a SIGIO indicating new data in case the process asked for it */
  1509. send_sigio_to_port(port);
  1510. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  1511. wake_up_interruptible(&port->waitqueue);
  1512. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  1513. hvc_kick();
  1514. }
  1515. static void control_intr(struct virtqueue *vq)
  1516. {
  1517. struct ports_device *portdev;
  1518. portdev = vq->vdev->priv;
  1519. schedule_work(&portdev->control_work);
  1520. }
  1521. static void config_intr(struct virtio_device *vdev)
  1522. {
  1523. struct ports_device *portdev;
  1524. portdev = vdev->priv;
  1525. if (!use_multiport(portdev)) {
  1526. struct port *port;
  1527. u16 rows, cols;
  1528. vdev->config->get(vdev,
  1529. offsetof(struct virtio_console_config, cols),
  1530. &cols, sizeof(u16));
  1531. vdev->config->get(vdev,
  1532. offsetof(struct virtio_console_config, rows),
  1533. &rows, sizeof(u16));
  1534. port = find_port_by_id(portdev, 0);
  1535. set_console_size(port, rows, cols);
  1536. /*
  1537. * We'll use this way of resizing only for legacy
  1538. * support. For newer userspace
  1539. * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
  1540. * to indicate console size changes so that it can be
  1541. * done per-port.
  1542. */
  1543. resize_console(port);
  1544. }
  1545. }
  1546. static int init_vqs(struct ports_device *portdev)
  1547. {
  1548. vq_callback_t **io_callbacks;
  1549. char **io_names;
  1550. struct virtqueue **vqs;
  1551. u32 i, j, nr_ports, nr_queues;
  1552. int err;
  1553. nr_ports = portdev->config.max_nr_ports;
  1554. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  1555. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  1556. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  1557. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  1558. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1559. GFP_KERNEL);
  1560. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1561. GFP_KERNEL);
  1562. if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
  1563. !portdev->out_vqs) {
  1564. err = -ENOMEM;
  1565. goto free;
  1566. }
  1567. /*
  1568. * For backward compat (newer host but older guest), the host
  1569. * spawns a console port first and also inits the vqs for port
  1570. * 0 before others.
  1571. */
  1572. j = 0;
  1573. io_callbacks[j] = in_intr;
  1574. io_callbacks[j + 1] = out_intr;
  1575. io_names[j] = "input";
  1576. io_names[j + 1] = "output";
  1577. j += 2;
  1578. if (use_multiport(portdev)) {
  1579. io_callbacks[j] = control_intr;
  1580. io_callbacks[j + 1] = NULL;
  1581. io_names[j] = "control-i";
  1582. io_names[j + 1] = "control-o";
  1583. for (i = 1; i < nr_ports; i++) {
  1584. j += 2;
  1585. io_callbacks[j] = in_intr;
  1586. io_callbacks[j + 1] = out_intr;
  1587. io_names[j] = "input";
  1588. io_names[j + 1] = "output";
  1589. }
  1590. }
  1591. /* Find the queues. */
  1592. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  1593. io_callbacks,
  1594. (const char **)io_names);
  1595. if (err)
  1596. goto free;
  1597. j = 0;
  1598. portdev->in_vqs[0] = vqs[0];
  1599. portdev->out_vqs[0] = vqs[1];
  1600. j += 2;
  1601. if (use_multiport(portdev)) {
  1602. portdev->c_ivq = vqs[j];
  1603. portdev->c_ovq = vqs[j + 1];
  1604. for (i = 1; i < nr_ports; i++) {
  1605. j += 2;
  1606. portdev->in_vqs[i] = vqs[j];
  1607. portdev->out_vqs[i] = vqs[j + 1];
  1608. }
  1609. }
  1610. kfree(io_names);
  1611. kfree(io_callbacks);
  1612. kfree(vqs);
  1613. return 0;
  1614. free:
  1615. kfree(portdev->out_vqs);
  1616. kfree(portdev->in_vqs);
  1617. kfree(io_names);
  1618. kfree(io_callbacks);
  1619. kfree(vqs);
  1620. return err;
  1621. }
  1622. static const struct file_operations portdev_fops = {
  1623. .owner = THIS_MODULE,
  1624. };
  1625. static void remove_vqs(struct ports_device *portdev)
  1626. {
  1627. portdev->vdev->config->del_vqs(portdev->vdev);
  1628. kfree(portdev->in_vqs);
  1629. kfree(portdev->out_vqs);
  1630. }
  1631. static void remove_controlq_data(struct ports_device *portdev)
  1632. {
  1633. struct port_buffer *buf;
  1634. unsigned int len;
  1635. if (!use_multiport(portdev))
  1636. return;
  1637. while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
  1638. free_buf(buf, true);
  1639. while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
  1640. free_buf(buf, true);
  1641. }
  1642. /*
  1643. * Once we're further in boot, we get probed like any other virtio
  1644. * device.
  1645. *
  1646. * If the host also supports multiple console ports, we check the
  1647. * config space to see how many ports the host has spawned. We
  1648. * initialize each port found.
  1649. */
  1650. static int virtcons_probe(struct virtio_device *vdev)
  1651. {
  1652. struct ports_device *portdev;
  1653. int err;
  1654. bool multiport;
  1655. bool early = early_put_chars != NULL;
  1656. /* Ensure to read early_put_chars now */
  1657. barrier();
  1658. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  1659. if (!portdev) {
  1660. err = -ENOMEM;
  1661. goto fail;
  1662. }
  1663. /* Attach this portdev to this virtio_device, and vice-versa. */
  1664. portdev->vdev = vdev;
  1665. vdev->priv = portdev;
  1666. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  1667. &portdev_fops);
  1668. if (portdev->chr_major < 0) {
  1669. dev_err(&vdev->dev,
  1670. "Error %d registering chrdev for device %u\n",
  1671. portdev->chr_major, vdev->index);
  1672. err = portdev->chr_major;
  1673. goto free;
  1674. }
  1675. multiport = false;
  1676. portdev->config.max_nr_ports = 1;
  1677. /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
  1678. if (!is_rproc_serial(vdev) &&
  1679. virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
  1680. offsetof(struct virtio_console_config,
  1681. max_nr_ports),
  1682. &portdev->config.max_nr_ports) == 0) {
  1683. multiport = true;
  1684. }
  1685. err = init_vqs(portdev);
  1686. if (err < 0) {
  1687. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  1688. goto free_chrdev;
  1689. }
  1690. spin_lock_init(&portdev->ports_lock);
  1691. INIT_LIST_HEAD(&portdev->ports);
  1692. if (multiport) {
  1693. unsigned int nr_added_bufs;
  1694. spin_lock_init(&portdev->c_ivq_lock);
  1695. spin_lock_init(&portdev->c_ovq_lock);
  1696. INIT_WORK(&portdev->control_work, &control_work_handler);
  1697. nr_added_bufs = fill_queue(portdev->c_ivq,
  1698. &portdev->c_ivq_lock);
  1699. if (!nr_added_bufs) {
  1700. dev_err(&vdev->dev,
  1701. "Error allocating buffers for control queue\n");
  1702. err = -ENOMEM;
  1703. goto free_vqs;
  1704. }
  1705. } else {
  1706. /*
  1707. * For backward compatibility: Create a console port
  1708. * if we're running on older host.
  1709. */
  1710. add_port(portdev, 0);
  1711. }
  1712. spin_lock_irq(&pdrvdata_lock);
  1713. list_add_tail(&portdev->list, &pdrvdata.portdevs);
  1714. spin_unlock_irq(&pdrvdata_lock);
  1715. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1716. VIRTIO_CONSOLE_DEVICE_READY, 1);
  1717. /*
  1718. * If there was an early virtio console, assume that there are no
  1719. * other consoles. We need to wait until the hvc_alloc matches the
  1720. * hvc_instantiate, otherwise tty_open will complain, resulting in
  1721. * a "Warning: unable to open an initial console" boot failure.
  1722. * Without multiport this is done in add_port above. With multiport
  1723. * this might take some host<->guest communication - thus we have to
  1724. * wait.
  1725. */
  1726. if (multiport && early)
  1727. wait_for_completion(&early_console_added);
  1728. return 0;
  1729. free_vqs:
  1730. /* The host might want to notify mgmt sw about device add failure */
  1731. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1732. VIRTIO_CONSOLE_DEVICE_READY, 0);
  1733. remove_vqs(portdev);
  1734. free_chrdev:
  1735. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1736. free:
  1737. kfree(portdev);
  1738. fail:
  1739. return err;
  1740. }
  1741. static void virtcons_remove(struct virtio_device *vdev)
  1742. {
  1743. struct ports_device *portdev;
  1744. struct port *port, *port2;
  1745. portdev = vdev->priv;
  1746. spin_lock_irq(&pdrvdata_lock);
  1747. list_del(&portdev->list);
  1748. spin_unlock_irq(&pdrvdata_lock);
  1749. /* Disable interrupts for vqs */
  1750. vdev->config->reset(vdev);
  1751. /* Finish up work that's lined up */
  1752. if (use_multiport(portdev))
  1753. cancel_work_sync(&portdev->control_work);
  1754. list_for_each_entry_safe(port, port2, &portdev->ports, list)
  1755. unplug_port(port);
  1756. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1757. /*
  1758. * When yanking out a device, we immediately lose the
  1759. * (device-side) queues. So there's no point in keeping the
  1760. * guest side around till we drop our final reference. This
  1761. * also means that any ports which are in an open state will
  1762. * have to just stop using the port, as the vqs are going
  1763. * away.
  1764. */
  1765. remove_controlq_data(portdev);
  1766. remove_vqs(portdev);
  1767. kfree(portdev);
  1768. }
  1769. static struct virtio_device_id id_table[] = {
  1770. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  1771. { 0 },
  1772. };
  1773. static unsigned int features[] = {
  1774. VIRTIO_CONSOLE_F_SIZE,
  1775. VIRTIO_CONSOLE_F_MULTIPORT,
  1776. };
  1777. static struct virtio_device_id rproc_serial_id_table[] = {
  1778. #if IS_ENABLED(CONFIG_REMOTEPROC)
  1779. { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
  1780. #endif
  1781. { 0 },
  1782. };
  1783. static unsigned int rproc_serial_features[] = {
  1784. };
  1785. #ifdef CONFIG_PM
  1786. static int virtcons_freeze(struct virtio_device *vdev)
  1787. {
  1788. struct ports_device *portdev;
  1789. struct port *port;
  1790. portdev = vdev->priv;
  1791. vdev->config->reset(vdev);
  1792. virtqueue_disable_cb(portdev->c_ivq);
  1793. cancel_work_sync(&portdev->control_work);
  1794. /*
  1795. * Once more: if control_work_handler() was running, it would
  1796. * enable the cb as the last step.
  1797. */
  1798. virtqueue_disable_cb(portdev->c_ivq);
  1799. remove_controlq_data(portdev);
  1800. list_for_each_entry(port, &portdev->ports, list) {
  1801. virtqueue_disable_cb(port->in_vq);
  1802. virtqueue_disable_cb(port->out_vq);
  1803. /*
  1804. * We'll ask the host later if the new invocation has
  1805. * the port opened or closed.
  1806. */
  1807. port->host_connected = false;
  1808. remove_port_data(port);
  1809. }
  1810. remove_vqs(portdev);
  1811. return 0;
  1812. }
  1813. static int virtcons_restore(struct virtio_device *vdev)
  1814. {
  1815. struct ports_device *portdev;
  1816. struct port *port;
  1817. int ret;
  1818. portdev = vdev->priv;
  1819. ret = init_vqs(portdev);
  1820. if (ret)
  1821. return ret;
  1822. if (use_multiport(portdev))
  1823. fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
  1824. list_for_each_entry(port, &portdev->ports, list) {
  1825. port->in_vq = portdev->in_vqs[port->id];
  1826. port->out_vq = portdev->out_vqs[port->id];
  1827. fill_queue(port->in_vq, &port->inbuf_lock);
  1828. /* Get port open/close status on the host */
  1829. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1830. /*
  1831. * If a port was open at the time of suspending, we
  1832. * have to let the host know that it's still open.
  1833. */
  1834. if (port->guest_connected)
  1835. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  1836. }
  1837. return 0;
  1838. }
  1839. #endif
  1840. static struct virtio_driver virtio_console = {
  1841. .feature_table = features,
  1842. .feature_table_size = ARRAY_SIZE(features),
  1843. .driver.name = KBUILD_MODNAME,
  1844. .driver.owner = THIS_MODULE,
  1845. .id_table = id_table,
  1846. .probe = virtcons_probe,
  1847. .remove = virtcons_remove,
  1848. .config_changed = config_intr,
  1849. #ifdef CONFIG_PM
  1850. .freeze = virtcons_freeze,
  1851. .restore = virtcons_restore,
  1852. #endif
  1853. };
  1854. static struct virtio_driver virtio_rproc_serial = {
  1855. .feature_table = rproc_serial_features,
  1856. .feature_table_size = ARRAY_SIZE(rproc_serial_features),
  1857. .driver.name = "virtio_rproc_serial",
  1858. .driver.owner = THIS_MODULE,
  1859. .id_table = rproc_serial_id_table,
  1860. .probe = virtcons_probe,
  1861. .remove = virtcons_remove,
  1862. };
  1863. static int __init init(void)
  1864. {
  1865. int err;
  1866. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  1867. if (IS_ERR(pdrvdata.class)) {
  1868. err = PTR_ERR(pdrvdata.class);
  1869. pr_err("Error %d creating virtio-ports class\n", err);
  1870. return err;
  1871. }
  1872. pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
  1873. if (!pdrvdata.debugfs_dir)
  1874. pr_warning("Error creating debugfs dir for virtio-ports\n");
  1875. INIT_LIST_HEAD(&pdrvdata.consoles);
  1876. INIT_LIST_HEAD(&pdrvdata.portdevs);
  1877. err = register_virtio_driver(&virtio_console);
  1878. if (err < 0) {
  1879. pr_err("Error %d registering virtio driver\n", err);
  1880. goto free;
  1881. }
  1882. err = register_virtio_driver(&virtio_rproc_serial);
  1883. if (err < 0) {
  1884. pr_err("Error %d registering virtio rproc serial driver\n",
  1885. err);
  1886. goto unregister;
  1887. }
  1888. return 0;
  1889. unregister:
  1890. unregister_virtio_driver(&virtio_console);
  1891. free:
  1892. if (pdrvdata.debugfs_dir)
  1893. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1894. class_destroy(pdrvdata.class);
  1895. return err;
  1896. }
  1897. static void __exit fini(void)
  1898. {
  1899. reclaim_dma_bufs();
  1900. unregister_virtio_driver(&virtio_console);
  1901. unregister_virtio_driver(&virtio_rproc_serial);
  1902. class_destroy(pdrvdata.class);
  1903. if (pdrvdata.debugfs_dir)
  1904. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1905. }
  1906. module_init(init);
  1907. module_exit(fini);
  1908. MODULE_DEVICE_TABLE(virtio, id_table);
  1909. MODULE_DESCRIPTION("Virtio console driver");
  1910. MODULE_LICENSE("GPL");