virtio_console.c 54 KB

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