virtio_console.c 54 KB

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