virtio_console.c 56 KB

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