virtio_console.c 53 KB

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