virtio_console.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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 = kzalloc(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. return ret;
  730. }
  731. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  732. {
  733. struct port *port;
  734. unsigned int ret;
  735. port = filp->private_data;
  736. poll_wait(filp, &port->waitqueue, wait);
  737. if (!port->guest_connected) {
  738. /* Port got unplugged */
  739. return POLLHUP;
  740. }
  741. ret = 0;
  742. if (!will_read_block(port))
  743. ret |= POLLIN | POLLRDNORM;
  744. if (!will_write_block(port))
  745. ret |= POLLOUT;
  746. if (!port->host_connected)
  747. ret |= POLLHUP;
  748. return ret;
  749. }
  750. static void remove_port(struct kref *kref);
  751. static int port_fops_release(struct inode *inode, struct file *filp)
  752. {
  753. struct port *port;
  754. port = filp->private_data;
  755. /* Notify host of port being closed */
  756. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  757. spin_lock_irq(&port->inbuf_lock);
  758. port->guest_connected = false;
  759. discard_port_data(port);
  760. spin_unlock_irq(&port->inbuf_lock);
  761. spin_lock_irq(&port->outvq_lock);
  762. reclaim_consumed_buffers(port);
  763. spin_unlock_irq(&port->outvq_lock);
  764. /*
  765. * Locks aren't necessary here as a port can't be opened after
  766. * unplug, and if a port isn't unplugged, a kref would already
  767. * exist for the port. Plus, taking ports_lock here would
  768. * create a dependency on other locks taken by functions
  769. * inside remove_port if we're the last holder of the port,
  770. * creating many problems.
  771. */
  772. kref_put(&port->kref, remove_port);
  773. return 0;
  774. }
  775. static int port_fops_open(struct inode *inode, struct file *filp)
  776. {
  777. struct cdev *cdev = inode->i_cdev;
  778. struct port *port;
  779. int ret;
  780. port = find_port_by_devt(cdev->dev);
  781. filp->private_data = port;
  782. /* Prevent against a port getting hot-unplugged at the same time */
  783. spin_lock_irq(&port->portdev->ports_lock);
  784. kref_get(&port->kref);
  785. spin_unlock_irq(&port->portdev->ports_lock);
  786. /*
  787. * Don't allow opening of console port devices -- that's done
  788. * via /dev/hvc
  789. */
  790. if (is_console_port(port)) {
  791. ret = -ENXIO;
  792. goto out;
  793. }
  794. /* Allow only one process to open a particular port at a time */
  795. spin_lock_irq(&port->inbuf_lock);
  796. if (port->guest_connected) {
  797. spin_unlock_irq(&port->inbuf_lock);
  798. ret = -EMFILE;
  799. goto out;
  800. }
  801. port->guest_connected = true;
  802. spin_unlock_irq(&port->inbuf_lock);
  803. spin_lock_irq(&port->outvq_lock);
  804. /*
  805. * There might be a chance that we missed reclaiming a few
  806. * buffers in the window of the port getting previously closed
  807. * and opening now.
  808. */
  809. reclaim_consumed_buffers(port);
  810. spin_unlock_irq(&port->outvq_lock);
  811. nonseekable_open(inode, filp);
  812. /* Notify host of port being opened */
  813. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  814. return 0;
  815. out:
  816. kref_put(&port->kref, remove_port);
  817. return ret;
  818. }
  819. static int port_fops_fasync(int fd, struct file *filp, int mode)
  820. {
  821. struct port *port;
  822. port = filp->private_data;
  823. return fasync_helper(fd, filp, mode, &port->async_queue);
  824. }
  825. /*
  826. * The file operations that we support: programs in the guest can open
  827. * a console device, read from it, write to it, poll for data and
  828. * close it. The devices are at
  829. * /dev/vport<device number>p<port number>
  830. */
  831. static const struct file_operations port_fops = {
  832. .owner = THIS_MODULE,
  833. .open = port_fops_open,
  834. .read = port_fops_read,
  835. .write = port_fops_write,
  836. .splice_write = port_fops_splice_write,
  837. .poll = port_fops_poll,
  838. .release = port_fops_release,
  839. .fasync = port_fops_fasync,
  840. .llseek = no_llseek,
  841. };
  842. /*
  843. * The put_chars() callback is pretty straightforward.
  844. *
  845. * We turn the characters into a scatter-gather list, add it to the
  846. * output queue and then kick the Host. Then we sit here waiting for
  847. * it to finish: inefficient in theory, but in practice
  848. * implementations will do it immediately (lguest's Launcher does).
  849. */
  850. static int put_chars(u32 vtermno, const char *buf, int count)
  851. {
  852. struct port *port;
  853. if (unlikely(early_put_chars))
  854. return early_put_chars(vtermno, buf, count);
  855. port = find_port_by_vtermno(vtermno);
  856. if (!port)
  857. return -EPIPE;
  858. return send_buf(port, (void *)buf, count, false);
  859. }
  860. /*
  861. * get_chars() is the callback from the hvc_console infrastructure
  862. * when an interrupt is received.
  863. *
  864. * We call out to fill_readbuf that gets us the required data from the
  865. * buffers that are queued up.
  866. */
  867. static int get_chars(u32 vtermno, char *buf, int count)
  868. {
  869. struct port *port;
  870. /* If we've not set up the port yet, we have no input to give. */
  871. if (unlikely(early_put_chars))
  872. return 0;
  873. port = find_port_by_vtermno(vtermno);
  874. if (!port)
  875. return -EPIPE;
  876. /* If we don't have an input queue yet, we can't get input. */
  877. BUG_ON(!port->in_vq);
  878. return fill_readbuf(port, buf, count, false);
  879. }
  880. static void resize_console(struct port *port)
  881. {
  882. struct virtio_device *vdev;
  883. /* The port could have been hot-unplugged */
  884. if (!port || !is_console_port(port))
  885. return;
  886. vdev = port->portdev->vdev;
  887. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
  888. hvc_resize(port->cons.hvc, port->cons.ws);
  889. }
  890. /* We set the configuration at this point, since we now have a tty */
  891. static int notifier_add_vio(struct hvc_struct *hp, int data)
  892. {
  893. struct port *port;
  894. port = find_port_by_vtermno(hp->vtermno);
  895. if (!port)
  896. return -EINVAL;
  897. hp->irq_requested = 1;
  898. resize_console(port);
  899. return 0;
  900. }
  901. static void notifier_del_vio(struct hvc_struct *hp, int data)
  902. {
  903. hp->irq_requested = 0;
  904. }
  905. /* The operations for console ports. */
  906. static const struct hv_ops hv_ops = {
  907. .get_chars = get_chars,
  908. .put_chars = put_chars,
  909. .notifier_add = notifier_add_vio,
  910. .notifier_del = notifier_del_vio,
  911. .notifier_hangup = notifier_del_vio,
  912. };
  913. /*
  914. * Console drivers are initialized very early so boot messages can go
  915. * out, so we do things slightly differently from the generic virtio
  916. * initialization of the net and block drivers.
  917. *
  918. * At this stage, the console is output-only. It's too early to set
  919. * up a virtqueue, so we let the drivers do some boutique early-output
  920. * thing.
  921. */
  922. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  923. {
  924. early_put_chars = put_chars;
  925. return hvc_instantiate(0, 0, &hv_ops);
  926. }
  927. int init_port_console(struct port *port)
  928. {
  929. int ret;
  930. /*
  931. * The Host's telling us this port is a console port. Hook it
  932. * up with an hvc console.
  933. *
  934. * To set up and manage our virtual console, we call
  935. * hvc_alloc().
  936. *
  937. * The first argument of hvc_alloc() is the virtual console
  938. * number. The second argument is the parameter for the
  939. * notification mechanism (like irq number). We currently
  940. * leave this as zero, virtqueues have implicit notifications.
  941. *
  942. * The third argument is a "struct hv_ops" containing the
  943. * put_chars() get_chars(), notifier_add() and notifier_del()
  944. * pointers. The final argument is the output buffer size: we
  945. * can do any size, so we put PAGE_SIZE here.
  946. */
  947. port->cons.vtermno = pdrvdata.next_vtermno;
  948. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  949. if (IS_ERR(port->cons.hvc)) {
  950. ret = PTR_ERR(port->cons.hvc);
  951. dev_err(port->dev,
  952. "error %d allocating hvc for port\n", ret);
  953. port->cons.hvc = NULL;
  954. return ret;
  955. }
  956. spin_lock_irq(&pdrvdata_lock);
  957. pdrvdata.next_vtermno++;
  958. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  959. spin_unlock_irq(&pdrvdata_lock);
  960. port->guest_connected = true;
  961. /*
  962. * Start using the new console output if this is the first
  963. * console to come up.
  964. */
  965. if (early_put_chars)
  966. early_put_chars = NULL;
  967. /* Notify host of port being opened */
  968. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  969. return 0;
  970. }
  971. static ssize_t show_port_name(struct device *dev,
  972. struct device_attribute *attr, char *buffer)
  973. {
  974. struct port *port;
  975. port = dev_get_drvdata(dev);
  976. return sprintf(buffer, "%s\n", port->name);
  977. }
  978. static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
  979. static struct attribute *port_sysfs_entries[] = {
  980. &dev_attr_name.attr,
  981. NULL
  982. };
  983. static struct attribute_group port_attribute_group = {
  984. .name = NULL, /* put in device directory */
  985. .attrs = port_sysfs_entries,
  986. };
  987. static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
  988. size_t count, loff_t *offp)
  989. {
  990. struct port *port;
  991. char *buf;
  992. ssize_t ret, out_offset, out_count;
  993. out_count = 1024;
  994. buf = kmalloc(out_count, GFP_KERNEL);
  995. if (!buf)
  996. return -ENOMEM;
  997. port = filp->private_data;
  998. out_offset = 0;
  999. out_offset += snprintf(buf + out_offset, out_count,
  1000. "name: %s\n", port->name ? port->name : "");
  1001. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1002. "guest_connected: %d\n", port->guest_connected);
  1003. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1004. "host_connected: %d\n", port->host_connected);
  1005. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1006. "outvq_full: %d\n", port->outvq_full);
  1007. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1008. "bytes_sent: %lu\n", port->stats.bytes_sent);
  1009. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1010. "bytes_received: %lu\n",
  1011. port->stats.bytes_received);
  1012. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1013. "bytes_discarded: %lu\n",
  1014. port->stats.bytes_discarded);
  1015. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1016. "is_console: %s\n",
  1017. is_console_port(port) ? "yes" : "no");
  1018. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1019. "console_vtermno: %u\n", port->cons.vtermno);
  1020. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  1021. kfree(buf);
  1022. return ret;
  1023. }
  1024. static const struct file_operations port_debugfs_ops = {
  1025. .owner = THIS_MODULE,
  1026. .open = simple_open,
  1027. .read = debugfs_read,
  1028. };
  1029. static void set_console_size(struct port *port, u16 rows, u16 cols)
  1030. {
  1031. if (!port || !is_console_port(port))
  1032. return;
  1033. port->cons.ws.ws_row = rows;
  1034. port->cons.ws.ws_col = cols;
  1035. }
  1036. static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
  1037. {
  1038. struct port_buffer *buf;
  1039. unsigned int nr_added_bufs;
  1040. int ret;
  1041. nr_added_bufs = 0;
  1042. do {
  1043. buf = alloc_buf(PAGE_SIZE);
  1044. if (!buf)
  1045. break;
  1046. spin_lock_irq(lock);
  1047. ret = add_inbuf(vq, buf);
  1048. if (ret < 0) {
  1049. spin_unlock_irq(lock);
  1050. free_buf(buf);
  1051. break;
  1052. }
  1053. nr_added_bufs++;
  1054. spin_unlock_irq(lock);
  1055. } while (ret > 0);
  1056. return nr_added_bufs;
  1057. }
  1058. static void send_sigio_to_port(struct port *port)
  1059. {
  1060. if (port->async_queue && port->guest_connected)
  1061. kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
  1062. }
  1063. static int add_port(struct ports_device *portdev, u32 id)
  1064. {
  1065. char debugfs_name[16];
  1066. struct port *port;
  1067. struct port_buffer *buf;
  1068. dev_t devt;
  1069. unsigned int nr_added_bufs;
  1070. int err;
  1071. port = kmalloc(sizeof(*port), GFP_KERNEL);
  1072. if (!port) {
  1073. err = -ENOMEM;
  1074. goto fail;
  1075. }
  1076. kref_init(&port->kref);
  1077. port->portdev = portdev;
  1078. port->id = id;
  1079. port->name = NULL;
  1080. port->inbuf = NULL;
  1081. port->cons.hvc = NULL;
  1082. port->async_queue = NULL;
  1083. port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
  1084. port->host_connected = port->guest_connected = false;
  1085. port->stats = (struct port_stats) { 0 };
  1086. port->outvq_full = false;
  1087. port->in_vq = portdev->in_vqs[port->id];
  1088. port->out_vq = portdev->out_vqs[port->id];
  1089. port->cdev = cdev_alloc();
  1090. if (!port->cdev) {
  1091. dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
  1092. err = -ENOMEM;
  1093. goto free_port;
  1094. }
  1095. port->cdev->ops = &port_fops;
  1096. devt = MKDEV(portdev->chr_major, id);
  1097. err = cdev_add(port->cdev, devt, 1);
  1098. if (err < 0) {
  1099. dev_err(&port->portdev->vdev->dev,
  1100. "Error %d adding cdev for port %u\n", err, id);
  1101. goto free_cdev;
  1102. }
  1103. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  1104. devt, port, "vport%up%u",
  1105. port->portdev->drv_index, id);
  1106. if (IS_ERR(port->dev)) {
  1107. err = PTR_ERR(port->dev);
  1108. dev_err(&port->portdev->vdev->dev,
  1109. "Error %d creating device for port %u\n",
  1110. err, id);
  1111. goto free_cdev;
  1112. }
  1113. spin_lock_init(&port->inbuf_lock);
  1114. spin_lock_init(&port->outvq_lock);
  1115. init_waitqueue_head(&port->waitqueue);
  1116. /* Fill the in_vq with buffers so the host can send us data. */
  1117. nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
  1118. if (!nr_added_bufs) {
  1119. dev_err(port->dev, "Error allocating inbufs\n");
  1120. err = -ENOMEM;
  1121. goto free_device;
  1122. }
  1123. /*
  1124. * If we're not using multiport support, this has to be a console port
  1125. */
  1126. if (!use_multiport(port->portdev)) {
  1127. err = init_port_console(port);
  1128. if (err)
  1129. goto free_inbufs;
  1130. }
  1131. spin_lock_irq(&portdev->ports_lock);
  1132. list_add_tail(&port->list, &port->portdev->ports);
  1133. spin_unlock_irq(&portdev->ports_lock);
  1134. /*
  1135. * Tell the Host we're set so that it can send us various
  1136. * configuration parameters for this port (eg, port name,
  1137. * caching, whether this is a console port, etc.)
  1138. */
  1139. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1140. if (pdrvdata.debugfs_dir) {
  1141. /*
  1142. * Finally, create the debugfs file that we can use to
  1143. * inspect a port's state at any time
  1144. */
  1145. sprintf(debugfs_name, "vport%up%u",
  1146. port->portdev->drv_index, id);
  1147. port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
  1148. pdrvdata.debugfs_dir,
  1149. port,
  1150. &port_debugfs_ops);
  1151. }
  1152. return 0;
  1153. free_inbufs:
  1154. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  1155. free_buf(buf);
  1156. free_device:
  1157. device_destroy(pdrvdata.class, port->dev->devt);
  1158. free_cdev:
  1159. cdev_del(port->cdev);
  1160. free_port:
  1161. kfree(port);
  1162. fail:
  1163. /* The host might want to notify management sw about port add failure */
  1164. __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
  1165. return err;
  1166. }
  1167. /* No users remain, remove all port-specific data. */
  1168. static void remove_port(struct kref *kref)
  1169. {
  1170. struct port *port;
  1171. port = container_of(kref, struct port, kref);
  1172. sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
  1173. device_destroy(pdrvdata.class, port->dev->devt);
  1174. cdev_del(port->cdev);
  1175. kfree(port->name);
  1176. debugfs_remove(port->debugfs_file);
  1177. kfree(port);
  1178. }
  1179. static void remove_port_data(struct port *port)
  1180. {
  1181. struct port_buffer *buf;
  1182. /* Remove unused data this port might have received. */
  1183. discard_port_data(port);
  1184. reclaim_consumed_buffers(port);
  1185. /* Remove buffers we queued up for the Host to send us data in. */
  1186. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  1187. free_buf(buf);
  1188. }
  1189. /*
  1190. * Port got unplugged. Remove port from portdev's list and drop the
  1191. * kref reference. If no userspace has this port opened, it will
  1192. * result in immediate removal the port.
  1193. */
  1194. static void unplug_port(struct port *port)
  1195. {
  1196. spin_lock_irq(&port->portdev->ports_lock);
  1197. list_del(&port->list);
  1198. spin_unlock_irq(&port->portdev->ports_lock);
  1199. if (port->guest_connected) {
  1200. port->guest_connected = false;
  1201. port->host_connected = false;
  1202. wake_up_interruptible(&port->waitqueue);
  1203. /* Let the app know the port is going down. */
  1204. send_sigio_to_port(port);
  1205. }
  1206. if (is_console_port(port)) {
  1207. spin_lock_irq(&pdrvdata_lock);
  1208. list_del(&port->cons.list);
  1209. spin_unlock_irq(&pdrvdata_lock);
  1210. hvc_remove(port->cons.hvc);
  1211. }
  1212. remove_port_data(port);
  1213. /*
  1214. * We should just assume the device itself has gone off --
  1215. * else a close on an open port later will try to send out a
  1216. * control message.
  1217. */
  1218. port->portdev = NULL;
  1219. /*
  1220. * Locks around here are not necessary - a port can't be
  1221. * opened after we removed the port struct from ports_list
  1222. * above.
  1223. */
  1224. kref_put(&port->kref, remove_port);
  1225. }
  1226. /* Any private messages that the Host and Guest want to share */
  1227. static void handle_control_message(struct ports_device *portdev,
  1228. struct port_buffer *buf)
  1229. {
  1230. struct virtio_console_control *cpkt;
  1231. struct port *port;
  1232. size_t name_size;
  1233. int err;
  1234. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  1235. port = find_port_by_id(portdev, cpkt->id);
  1236. if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
  1237. /* No valid header at start of buffer. Drop it. */
  1238. dev_dbg(&portdev->vdev->dev,
  1239. "Invalid index %u in control packet\n", cpkt->id);
  1240. return;
  1241. }
  1242. switch (cpkt->event) {
  1243. case VIRTIO_CONSOLE_PORT_ADD:
  1244. if (port) {
  1245. dev_dbg(&portdev->vdev->dev,
  1246. "Port %u already added\n", port->id);
  1247. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1248. break;
  1249. }
  1250. if (cpkt->id >= portdev->config.max_nr_ports) {
  1251. dev_warn(&portdev->vdev->dev,
  1252. "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
  1253. cpkt->id, portdev->config.max_nr_ports - 1);
  1254. break;
  1255. }
  1256. add_port(portdev, cpkt->id);
  1257. break;
  1258. case VIRTIO_CONSOLE_PORT_REMOVE:
  1259. unplug_port(port);
  1260. break;
  1261. case VIRTIO_CONSOLE_CONSOLE_PORT:
  1262. if (!cpkt->value)
  1263. break;
  1264. if (is_console_port(port))
  1265. break;
  1266. init_port_console(port);
  1267. complete(&early_console_added);
  1268. /*
  1269. * Could remove the port here in case init fails - but
  1270. * have to notify the host first.
  1271. */
  1272. break;
  1273. case VIRTIO_CONSOLE_RESIZE: {
  1274. struct {
  1275. __u16 rows;
  1276. __u16 cols;
  1277. } size;
  1278. if (!is_console_port(port))
  1279. break;
  1280. memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
  1281. sizeof(size));
  1282. set_console_size(port, size.rows, size.cols);
  1283. port->cons.hvc->irq_requested = 1;
  1284. resize_console(port);
  1285. break;
  1286. }
  1287. case VIRTIO_CONSOLE_PORT_OPEN:
  1288. port->host_connected = cpkt->value;
  1289. wake_up_interruptible(&port->waitqueue);
  1290. /*
  1291. * If the host port got closed and the host had any
  1292. * unconsumed buffers, we'll be able to reclaim them
  1293. * now.
  1294. */
  1295. spin_lock_irq(&port->outvq_lock);
  1296. reclaim_consumed_buffers(port);
  1297. spin_unlock_irq(&port->outvq_lock);
  1298. /*
  1299. * If the guest is connected, it'll be interested in
  1300. * knowing the host connection state changed.
  1301. */
  1302. send_sigio_to_port(port);
  1303. break;
  1304. case VIRTIO_CONSOLE_PORT_NAME:
  1305. /*
  1306. * If we woke up after hibernation, we can get this
  1307. * again. Skip it in that case.
  1308. */
  1309. if (port->name)
  1310. break;
  1311. /*
  1312. * Skip the size of the header and the cpkt to get the size
  1313. * of the name that was sent
  1314. */
  1315. name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
  1316. port->name = kmalloc(name_size, GFP_KERNEL);
  1317. if (!port->name) {
  1318. dev_err(port->dev,
  1319. "Not enough space to store port name\n");
  1320. break;
  1321. }
  1322. strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
  1323. name_size - 1);
  1324. port->name[name_size - 1] = 0;
  1325. /*
  1326. * Since we only have one sysfs attribute, 'name',
  1327. * create it only if we have a name for the port.
  1328. */
  1329. err = sysfs_create_group(&port->dev->kobj,
  1330. &port_attribute_group);
  1331. if (err) {
  1332. dev_err(port->dev,
  1333. "Error %d creating sysfs device attributes\n",
  1334. err);
  1335. } else {
  1336. /*
  1337. * Generate a udev event so that appropriate
  1338. * symlinks can be created based on udev
  1339. * rules.
  1340. */
  1341. kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
  1342. }
  1343. break;
  1344. }
  1345. }
  1346. static void control_work_handler(struct work_struct *work)
  1347. {
  1348. struct ports_device *portdev;
  1349. struct virtqueue *vq;
  1350. struct port_buffer *buf;
  1351. unsigned int len;
  1352. portdev = container_of(work, struct ports_device, control_work);
  1353. vq = portdev->c_ivq;
  1354. spin_lock(&portdev->cvq_lock);
  1355. while ((buf = virtqueue_get_buf(vq, &len))) {
  1356. spin_unlock(&portdev->cvq_lock);
  1357. buf->len = len;
  1358. buf->offset = 0;
  1359. handle_control_message(portdev, buf);
  1360. spin_lock(&portdev->cvq_lock);
  1361. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  1362. dev_warn(&portdev->vdev->dev,
  1363. "Error adding buffer to queue\n");
  1364. free_buf(buf);
  1365. }
  1366. }
  1367. spin_unlock(&portdev->cvq_lock);
  1368. }
  1369. static void out_intr(struct virtqueue *vq)
  1370. {
  1371. struct port *port;
  1372. port = find_port_by_vq(vq->vdev->priv, vq);
  1373. if (!port)
  1374. return;
  1375. wake_up_interruptible(&port->waitqueue);
  1376. }
  1377. static void in_intr(struct virtqueue *vq)
  1378. {
  1379. struct port *port;
  1380. unsigned long flags;
  1381. port = find_port_by_vq(vq->vdev->priv, vq);
  1382. if (!port)
  1383. return;
  1384. spin_lock_irqsave(&port->inbuf_lock, flags);
  1385. port->inbuf = get_inbuf(port);
  1386. /*
  1387. * Don't queue up data when port is closed. This condition
  1388. * can be reached when a console port is not yet connected (no
  1389. * tty is spawned) and the host sends out data to console
  1390. * ports. For generic serial ports, the host won't
  1391. * (shouldn't) send data till the guest is connected.
  1392. */
  1393. if (!port->guest_connected)
  1394. discard_port_data(port);
  1395. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  1396. wake_up_interruptible(&port->waitqueue);
  1397. /* Send a SIGIO indicating new data in case the process asked for it */
  1398. send_sigio_to_port(port);
  1399. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  1400. hvc_kick();
  1401. }
  1402. static void control_intr(struct virtqueue *vq)
  1403. {
  1404. struct ports_device *portdev;
  1405. portdev = vq->vdev->priv;
  1406. schedule_work(&portdev->control_work);
  1407. }
  1408. static void config_intr(struct virtio_device *vdev)
  1409. {
  1410. struct ports_device *portdev;
  1411. portdev = vdev->priv;
  1412. if (!use_multiport(portdev)) {
  1413. struct port *port;
  1414. u16 rows, cols;
  1415. vdev->config->get(vdev,
  1416. offsetof(struct virtio_console_config, cols),
  1417. &cols, sizeof(u16));
  1418. vdev->config->get(vdev,
  1419. offsetof(struct virtio_console_config, rows),
  1420. &rows, sizeof(u16));
  1421. port = find_port_by_id(portdev, 0);
  1422. set_console_size(port, rows, cols);
  1423. /*
  1424. * We'll use this way of resizing only for legacy
  1425. * support. For newer userspace
  1426. * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
  1427. * to indicate console size changes so that it can be
  1428. * done per-port.
  1429. */
  1430. resize_console(port);
  1431. }
  1432. }
  1433. static int init_vqs(struct ports_device *portdev)
  1434. {
  1435. vq_callback_t **io_callbacks;
  1436. char **io_names;
  1437. struct virtqueue **vqs;
  1438. u32 i, j, nr_ports, nr_queues;
  1439. int err;
  1440. nr_ports = portdev->config.max_nr_ports;
  1441. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  1442. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  1443. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  1444. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  1445. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1446. GFP_KERNEL);
  1447. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1448. GFP_KERNEL);
  1449. if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
  1450. !portdev->out_vqs) {
  1451. err = -ENOMEM;
  1452. goto free;
  1453. }
  1454. /*
  1455. * For backward compat (newer host but older guest), the host
  1456. * spawns a console port first and also inits the vqs for port
  1457. * 0 before others.
  1458. */
  1459. j = 0;
  1460. io_callbacks[j] = in_intr;
  1461. io_callbacks[j + 1] = out_intr;
  1462. io_names[j] = "input";
  1463. io_names[j + 1] = "output";
  1464. j += 2;
  1465. if (use_multiport(portdev)) {
  1466. io_callbacks[j] = control_intr;
  1467. io_callbacks[j + 1] = NULL;
  1468. io_names[j] = "control-i";
  1469. io_names[j + 1] = "control-o";
  1470. for (i = 1; i < nr_ports; i++) {
  1471. j += 2;
  1472. io_callbacks[j] = in_intr;
  1473. io_callbacks[j + 1] = out_intr;
  1474. io_names[j] = "input";
  1475. io_names[j + 1] = "output";
  1476. }
  1477. }
  1478. /* Find the queues. */
  1479. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  1480. io_callbacks,
  1481. (const char **)io_names);
  1482. if (err)
  1483. goto free;
  1484. j = 0;
  1485. portdev->in_vqs[0] = vqs[0];
  1486. portdev->out_vqs[0] = vqs[1];
  1487. j += 2;
  1488. if (use_multiport(portdev)) {
  1489. portdev->c_ivq = vqs[j];
  1490. portdev->c_ovq = vqs[j + 1];
  1491. for (i = 1; i < nr_ports; i++) {
  1492. j += 2;
  1493. portdev->in_vqs[i] = vqs[j];
  1494. portdev->out_vqs[i] = vqs[j + 1];
  1495. }
  1496. }
  1497. kfree(io_names);
  1498. kfree(io_callbacks);
  1499. kfree(vqs);
  1500. return 0;
  1501. free:
  1502. kfree(portdev->out_vqs);
  1503. kfree(portdev->in_vqs);
  1504. kfree(io_names);
  1505. kfree(io_callbacks);
  1506. kfree(vqs);
  1507. return err;
  1508. }
  1509. static const struct file_operations portdev_fops = {
  1510. .owner = THIS_MODULE,
  1511. };
  1512. static void remove_vqs(struct ports_device *portdev)
  1513. {
  1514. portdev->vdev->config->del_vqs(portdev->vdev);
  1515. kfree(portdev->in_vqs);
  1516. kfree(portdev->out_vqs);
  1517. }
  1518. static void remove_controlq_data(struct ports_device *portdev)
  1519. {
  1520. struct port_buffer *buf;
  1521. unsigned int len;
  1522. if (!use_multiport(portdev))
  1523. return;
  1524. while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
  1525. free_buf(buf);
  1526. while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
  1527. free_buf(buf);
  1528. }
  1529. /*
  1530. * Once we're further in boot, we get probed like any other virtio
  1531. * device.
  1532. *
  1533. * If the host also supports multiple console ports, we check the
  1534. * config space to see how many ports the host has spawned. We
  1535. * initialize each port found.
  1536. */
  1537. static int __devinit virtcons_probe(struct virtio_device *vdev)
  1538. {
  1539. struct ports_device *portdev;
  1540. int err;
  1541. bool multiport;
  1542. bool early = early_put_chars != NULL;
  1543. /* Ensure to read early_put_chars now */
  1544. barrier();
  1545. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  1546. if (!portdev) {
  1547. err = -ENOMEM;
  1548. goto fail;
  1549. }
  1550. /* Attach this portdev to this virtio_device, and vice-versa. */
  1551. portdev->vdev = vdev;
  1552. vdev->priv = portdev;
  1553. spin_lock_irq(&pdrvdata_lock);
  1554. portdev->drv_index = pdrvdata.index++;
  1555. spin_unlock_irq(&pdrvdata_lock);
  1556. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  1557. &portdev_fops);
  1558. if (portdev->chr_major < 0) {
  1559. dev_err(&vdev->dev,
  1560. "Error %d registering chrdev for device %u\n",
  1561. portdev->chr_major, portdev->drv_index);
  1562. err = portdev->chr_major;
  1563. goto free;
  1564. }
  1565. multiport = false;
  1566. portdev->config.max_nr_ports = 1;
  1567. if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
  1568. offsetof(struct virtio_console_config,
  1569. max_nr_ports),
  1570. &portdev->config.max_nr_ports) == 0)
  1571. multiport = true;
  1572. err = init_vqs(portdev);
  1573. if (err < 0) {
  1574. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  1575. goto free_chrdev;
  1576. }
  1577. spin_lock_init(&portdev->ports_lock);
  1578. INIT_LIST_HEAD(&portdev->ports);
  1579. if (multiport) {
  1580. unsigned int nr_added_bufs;
  1581. spin_lock_init(&portdev->cvq_lock);
  1582. INIT_WORK(&portdev->control_work, &control_work_handler);
  1583. nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  1584. if (!nr_added_bufs) {
  1585. dev_err(&vdev->dev,
  1586. "Error allocating buffers for control queue\n");
  1587. err = -ENOMEM;
  1588. goto free_vqs;
  1589. }
  1590. } else {
  1591. /*
  1592. * For backward compatibility: Create a console port
  1593. * if we're running on older host.
  1594. */
  1595. add_port(portdev, 0);
  1596. }
  1597. spin_lock_irq(&pdrvdata_lock);
  1598. list_add_tail(&portdev->list, &pdrvdata.portdevs);
  1599. spin_unlock_irq(&pdrvdata_lock);
  1600. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1601. VIRTIO_CONSOLE_DEVICE_READY, 1);
  1602. /*
  1603. * If there was an early virtio console, assume that there are no
  1604. * other consoles. We need to wait until the hvc_alloc matches the
  1605. * hvc_instantiate, otherwise tty_open will complain, resulting in
  1606. * a "Warning: unable to open an initial console" boot failure.
  1607. * Without multiport this is done in add_port above. With multiport
  1608. * this might take some host<->guest communication - thus we have to
  1609. * wait.
  1610. */
  1611. if (multiport && early)
  1612. wait_for_completion(&early_console_added);
  1613. return 0;
  1614. free_vqs:
  1615. /* The host might want to notify mgmt sw about device add failure */
  1616. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1617. VIRTIO_CONSOLE_DEVICE_READY, 0);
  1618. remove_vqs(portdev);
  1619. free_chrdev:
  1620. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1621. free:
  1622. kfree(portdev);
  1623. fail:
  1624. return err;
  1625. }
  1626. static void virtcons_remove(struct virtio_device *vdev)
  1627. {
  1628. struct ports_device *portdev;
  1629. struct port *port, *port2;
  1630. portdev = vdev->priv;
  1631. spin_lock_irq(&pdrvdata_lock);
  1632. list_del(&portdev->list);
  1633. spin_unlock_irq(&pdrvdata_lock);
  1634. /* Disable interrupts for vqs */
  1635. vdev->config->reset(vdev);
  1636. /* Finish up work that's lined up */
  1637. cancel_work_sync(&portdev->control_work);
  1638. list_for_each_entry_safe(port, port2, &portdev->ports, list)
  1639. unplug_port(port);
  1640. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1641. /*
  1642. * When yanking out a device, we immediately lose the
  1643. * (device-side) queues. So there's no point in keeping the
  1644. * guest side around till we drop our final reference. This
  1645. * also means that any ports which are in an open state will
  1646. * have to just stop using the port, as the vqs are going
  1647. * away.
  1648. */
  1649. remove_controlq_data(portdev);
  1650. remove_vqs(portdev);
  1651. kfree(portdev);
  1652. }
  1653. static struct virtio_device_id id_table[] = {
  1654. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  1655. { 0 },
  1656. };
  1657. static unsigned int features[] = {
  1658. VIRTIO_CONSOLE_F_SIZE,
  1659. VIRTIO_CONSOLE_F_MULTIPORT,
  1660. };
  1661. #ifdef CONFIG_PM
  1662. static int virtcons_freeze(struct virtio_device *vdev)
  1663. {
  1664. struct ports_device *portdev;
  1665. struct port *port;
  1666. portdev = vdev->priv;
  1667. vdev->config->reset(vdev);
  1668. virtqueue_disable_cb(portdev->c_ivq);
  1669. cancel_work_sync(&portdev->control_work);
  1670. /*
  1671. * Once more: if control_work_handler() was running, it would
  1672. * enable the cb as the last step.
  1673. */
  1674. virtqueue_disable_cb(portdev->c_ivq);
  1675. remove_controlq_data(portdev);
  1676. list_for_each_entry(port, &portdev->ports, list) {
  1677. virtqueue_disable_cb(port->in_vq);
  1678. virtqueue_disable_cb(port->out_vq);
  1679. /*
  1680. * We'll ask the host later if the new invocation has
  1681. * the port opened or closed.
  1682. */
  1683. port->host_connected = false;
  1684. remove_port_data(port);
  1685. }
  1686. remove_vqs(portdev);
  1687. return 0;
  1688. }
  1689. static int virtcons_restore(struct virtio_device *vdev)
  1690. {
  1691. struct ports_device *portdev;
  1692. struct port *port;
  1693. int ret;
  1694. portdev = vdev->priv;
  1695. ret = init_vqs(portdev);
  1696. if (ret)
  1697. return ret;
  1698. if (use_multiport(portdev))
  1699. fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  1700. list_for_each_entry(port, &portdev->ports, list) {
  1701. port->in_vq = portdev->in_vqs[port->id];
  1702. port->out_vq = portdev->out_vqs[port->id];
  1703. fill_queue(port->in_vq, &port->inbuf_lock);
  1704. /* Get port open/close status on the host */
  1705. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1706. /*
  1707. * If a port was open at the time of suspending, we
  1708. * have to let the host know that it's still open.
  1709. */
  1710. if (port->guest_connected)
  1711. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  1712. }
  1713. return 0;
  1714. }
  1715. #endif
  1716. static struct virtio_driver virtio_console = {
  1717. .feature_table = features,
  1718. .feature_table_size = ARRAY_SIZE(features),
  1719. .driver.name = KBUILD_MODNAME,
  1720. .driver.owner = THIS_MODULE,
  1721. .id_table = id_table,
  1722. .probe = virtcons_probe,
  1723. .remove = virtcons_remove,
  1724. .config_changed = config_intr,
  1725. #ifdef CONFIG_PM
  1726. .freeze = virtcons_freeze,
  1727. .restore = virtcons_restore,
  1728. #endif
  1729. };
  1730. static int __init init(void)
  1731. {
  1732. int err;
  1733. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  1734. if (IS_ERR(pdrvdata.class)) {
  1735. err = PTR_ERR(pdrvdata.class);
  1736. pr_err("Error %d creating virtio-ports class\n", err);
  1737. return err;
  1738. }
  1739. pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
  1740. if (!pdrvdata.debugfs_dir) {
  1741. pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
  1742. PTR_ERR(pdrvdata.debugfs_dir));
  1743. }
  1744. INIT_LIST_HEAD(&pdrvdata.consoles);
  1745. INIT_LIST_HEAD(&pdrvdata.portdevs);
  1746. err = register_virtio_driver(&virtio_console);
  1747. if (err < 0) {
  1748. pr_err("Error %d registering virtio driver\n", err);
  1749. goto free;
  1750. }
  1751. return 0;
  1752. free:
  1753. if (pdrvdata.debugfs_dir)
  1754. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1755. class_destroy(pdrvdata.class);
  1756. return err;
  1757. }
  1758. static void __exit fini(void)
  1759. {
  1760. unregister_virtio_driver(&virtio_console);
  1761. class_destroy(pdrvdata.class);
  1762. if (pdrvdata.debugfs_dir)
  1763. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1764. }
  1765. module_init(init);
  1766. module_exit(fini);
  1767. MODULE_DEVICE_TABLE(virtio, id_table);
  1768. MODULE_DESCRIPTION("Virtio console driver");
  1769. MODULE_LICENSE("GPL");