virtio_console.c 54 KB

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