virtio_console.c 45 KB

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