virtio_console.c 44 KB

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