virtio_console.c 44 KB

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