virtio_console.c 55 KB

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