virtio_console.c 56 KB

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