virtio_console.c 56 KB

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