virtio_console.c 56 KB

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