virtio_console.c 41 KB

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