virtio_console.c 44 KB

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