virtio_console.c 50 KB

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