virtio_console.c 56 KB

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