virtio_console.c 55 KB

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