virtio_console.c 53 KB

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