virtio_console.c 39 KB

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