virtio_console.c 38 KB

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