virtio_console.c 38 KB

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