virtio_console.c 55 KB

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