virtio_console.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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/device.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/poll.h>
  26. #include <linux/sched.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/virtio.h>
  29. #include <linux/virtio_console.h>
  30. #include <linux/wait.h>
  31. #include <linux/workqueue.h>
  32. #include "hvc_console.h"
  33. /*
  34. * This is a global struct for storing common data for all the devices
  35. * this driver handles.
  36. *
  37. * Mainly, it has a linked list for all the consoles in one place so
  38. * that callbacks from hvc for get_chars(), put_chars() work properly
  39. * across multiple devices and multiple ports per device.
  40. */
  41. struct ports_driver_data {
  42. /* Used for registering chardevs */
  43. struct class *class;
  44. /* Number of devices this driver is handling */
  45. unsigned int index;
  46. /*
  47. * This is used to keep track of the number of hvc consoles
  48. * spawned by this driver. This number is given as the first
  49. * argument to hvc_alloc(). To correctly map an initial
  50. * console spawned via hvc_instantiate to the console being
  51. * hooked up via hvc_alloc, we need to pass the same vtermno.
  52. *
  53. * We also just assume the first console being initialised was
  54. * the first one that got used as the initial console.
  55. */
  56. unsigned int next_vtermno;
  57. /* All the console devices handled by this driver */
  58. struct list_head consoles;
  59. };
  60. static struct ports_driver_data pdrvdata;
  61. DEFINE_SPINLOCK(pdrvdata_lock);
  62. /* This struct holds information that's relevant only for console ports */
  63. struct console {
  64. /* We'll place all consoles in a list in the pdrvdata struct */
  65. struct list_head list;
  66. /* The hvc device associated with this console port */
  67. struct hvc_struct *hvc;
  68. /*
  69. * This number identifies the number that we used to register
  70. * with hvc in hvc_instantiate() and hvc_alloc(); this is the
  71. * number passed on by the hvc callbacks to us to
  72. * differentiate between the other console ports handled by
  73. * this driver
  74. */
  75. u32 vtermno;
  76. };
  77. struct port_buffer {
  78. char *buf;
  79. /* size of the buffer in *buf above */
  80. size_t size;
  81. /* used length of the buffer */
  82. size_t len;
  83. /* offset in the buf from which to consume data */
  84. size_t offset;
  85. };
  86. /*
  87. * This is a per-device struct that stores data common to all the
  88. * ports for that device (vdev->priv).
  89. */
  90. struct ports_device {
  91. /*
  92. * Workqueue handlers where we process deferred work after
  93. * notification
  94. */
  95. struct work_struct control_work;
  96. struct list_head ports;
  97. /* To protect the list of ports */
  98. spinlock_t ports_lock;
  99. /* To protect the vq operations for the control channel */
  100. spinlock_t cvq_lock;
  101. /* The current config space is stored here */
  102. struct virtio_console_config config;
  103. /* The virtio device we're associated with */
  104. struct virtio_device *vdev;
  105. /*
  106. * A couple of virtqueues for the control channel: one for
  107. * guest->host transfers, one for host->guest transfers
  108. */
  109. struct virtqueue *c_ivq, *c_ovq;
  110. /* Array of per-port IO virtqueues */
  111. struct virtqueue **in_vqs, **out_vqs;
  112. /* Used for numbering devices for sysfs and debugfs */
  113. unsigned int drv_index;
  114. /* Major number for this device. Ports will be created as minors. */
  115. int chr_major;
  116. };
  117. /* This struct holds the per-port data */
  118. struct port {
  119. /* Next port in the list, head is in the ports_device */
  120. struct list_head list;
  121. /* Pointer to the parent virtio_console device */
  122. struct ports_device *portdev;
  123. /* The current buffer from which data has to be fed to readers */
  124. struct port_buffer *inbuf;
  125. /*
  126. * To protect the operations on the in_vq associated with this
  127. * port. Has to be a spinlock because it can be called from
  128. * interrupt context (get_char()).
  129. */
  130. spinlock_t inbuf_lock;
  131. /* The IO vqs for this port */
  132. struct virtqueue *in_vq, *out_vq;
  133. /*
  134. * The entries in this struct will be valid if this port is
  135. * hooked up to an hvc console
  136. */
  137. struct console cons;
  138. /* Each port associates with a separate char device */
  139. struct cdev cdev;
  140. struct device *dev;
  141. /* A waitqueue for poll() or blocking read operations */
  142. wait_queue_head_t waitqueue;
  143. /* The 'name' of the port that we expose via sysfs properties */
  144. char *name;
  145. /* The 'id' to identify the port with the Host */
  146. u32 id;
  147. /* Is the host device open */
  148. bool host_connected;
  149. /* We should allow only one process to open a port */
  150. bool guest_connected;
  151. };
  152. /* This is the very early arch-specified put chars function. */
  153. static int (*early_put_chars)(u32, const char *, int);
  154. static struct port *find_port_by_vtermno(u32 vtermno)
  155. {
  156. struct port *port;
  157. struct console *cons;
  158. unsigned long flags;
  159. spin_lock_irqsave(&pdrvdata_lock, flags);
  160. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  161. if (cons->vtermno == vtermno) {
  162. port = container_of(cons, struct port, cons);
  163. goto out;
  164. }
  165. }
  166. port = NULL;
  167. out:
  168. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  169. return port;
  170. }
  171. static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
  172. {
  173. struct port *port;
  174. unsigned long flags;
  175. spin_lock_irqsave(&portdev->ports_lock, flags);
  176. list_for_each_entry(port, &portdev->ports, list)
  177. if (port->id == id)
  178. goto out;
  179. port = NULL;
  180. out:
  181. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  182. return port;
  183. }
  184. static struct port *find_port_by_vq(struct ports_device *portdev,
  185. struct virtqueue *vq)
  186. {
  187. struct port *port;
  188. unsigned long flags;
  189. spin_lock_irqsave(&portdev->ports_lock, flags);
  190. list_for_each_entry(port, &portdev->ports, list)
  191. if (port->in_vq == vq || port->out_vq == vq)
  192. goto out;
  193. port = NULL;
  194. out:
  195. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  196. return port;
  197. }
  198. static bool is_console_port(struct port *port)
  199. {
  200. if (port->cons.hvc)
  201. return true;
  202. return false;
  203. }
  204. static inline bool use_multiport(struct ports_device *portdev)
  205. {
  206. /*
  207. * This condition can be true when put_chars is called from
  208. * early_init
  209. */
  210. if (!portdev->vdev)
  211. return 0;
  212. return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
  213. }
  214. static void free_buf(struct port_buffer *buf)
  215. {
  216. kfree(buf->buf);
  217. kfree(buf);
  218. }
  219. static struct port_buffer *alloc_buf(size_t buf_size)
  220. {
  221. struct port_buffer *buf;
  222. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  223. if (!buf)
  224. goto fail;
  225. buf->buf = kzalloc(buf_size, GFP_KERNEL);
  226. if (!buf->buf)
  227. goto free_buf;
  228. buf->len = 0;
  229. buf->offset = 0;
  230. buf->size = buf_size;
  231. return buf;
  232. free_buf:
  233. kfree(buf);
  234. fail:
  235. return NULL;
  236. }
  237. /* Callers should take appropriate locks */
  238. static void *get_inbuf(struct port *port)
  239. {
  240. struct port_buffer *buf;
  241. struct virtqueue *vq;
  242. unsigned int len;
  243. vq = port->in_vq;
  244. buf = vq->vq_ops->get_buf(vq, &len);
  245. if (buf) {
  246. buf->len = len;
  247. buf->offset = 0;
  248. }
  249. return buf;
  250. }
  251. /*
  252. * Create a scatter-gather list representing our input buffer and put
  253. * it in the queue.
  254. *
  255. * Callers should take appropriate locks.
  256. */
  257. static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
  258. {
  259. struct scatterlist sg[1];
  260. int ret;
  261. sg_init_one(sg, buf->buf, buf->size);
  262. ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
  263. vq->vq_ops->kick(vq);
  264. return ret;
  265. }
  266. static bool port_has_data(struct port *port)
  267. {
  268. unsigned long flags;
  269. bool ret;
  270. ret = false;
  271. spin_lock_irqsave(&port->inbuf_lock, flags);
  272. if (port->inbuf)
  273. ret = true;
  274. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  275. return ret;
  276. }
  277. static ssize_t send_control_msg(struct port *port, unsigned int event,
  278. unsigned int value)
  279. {
  280. struct scatterlist sg[1];
  281. struct virtio_console_control cpkt;
  282. struct virtqueue *vq;
  283. int len;
  284. if (!use_multiport(port->portdev))
  285. return 0;
  286. cpkt.id = port->id;
  287. cpkt.event = event;
  288. cpkt.value = value;
  289. vq = port->portdev->c_ovq;
  290. sg_init_one(sg, &cpkt, sizeof(cpkt));
  291. if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
  292. vq->vq_ops->kick(vq);
  293. while (!vq->vq_ops->get_buf(vq, &len))
  294. cpu_relax();
  295. }
  296. return 0;
  297. }
  298. static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
  299. {
  300. struct scatterlist sg[1];
  301. struct virtqueue *out_vq;
  302. ssize_t ret;
  303. unsigned int len;
  304. out_vq = port->out_vq;
  305. sg_init_one(sg, in_buf, in_count);
  306. ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
  307. /* Tell Host to go! */
  308. out_vq->vq_ops->kick(out_vq);
  309. if (ret < 0) {
  310. len = 0;
  311. goto fail;
  312. }
  313. /*
  314. * Wait till the host acknowledges it pushed out the data we
  315. * sent. Also ensure we return to userspace the number of
  316. * bytes that were successfully consumed by the host.
  317. */
  318. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  319. cpu_relax();
  320. fail:
  321. /* We're expected to return the amount of data we wrote */
  322. return len;
  323. }
  324. /*
  325. * Give out the data that's requested from the buffer that we have
  326. * queued up.
  327. */
  328. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
  329. bool to_user)
  330. {
  331. struct port_buffer *buf;
  332. unsigned long flags;
  333. if (!out_count || !port_has_data(port))
  334. return 0;
  335. buf = port->inbuf;
  336. out_count = min(out_count, buf->len - buf->offset);
  337. if (to_user) {
  338. ssize_t ret;
  339. ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
  340. if (ret)
  341. return -EFAULT;
  342. } else {
  343. memcpy(out_buf, buf->buf + buf->offset, out_count);
  344. }
  345. buf->offset += out_count;
  346. if (buf->offset == buf->len) {
  347. /*
  348. * We're done using all the data in this buffer.
  349. * Re-queue so that the Host can send us more data.
  350. */
  351. spin_lock_irqsave(&port->inbuf_lock, flags);
  352. port->inbuf = NULL;
  353. if (add_inbuf(port->in_vq, buf) < 0)
  354. dev_warn(port->dev, "failed add_buf\n");
  355. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  356. }
  357. /* Return the number of bytes actually copied */
  358. return out_count;
  359. }
  360. /* The condition that must be true for polling to end */
  361. static bool wait_is_over(struct port *port)
  362. {
  363. return port_has_data(port) || !port->host_connected;
  364. }
  365. static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
  366. size_t count, loff_t *offp)
  367. {
  368. struct port *port;
  369. ssize_t ret;
  370. port = filp->private_data;
  371. if (!port_has_data(port)) {
  372. /*
  373. * If nothing's connected on the host just return 0 in
  374. * case of list_empty; this tells the userspace app
  375. * that there's no connection
  376. */
  377. if (!port->host_connected)
  378. return 0;
  379. if (filp->f_flags & O_NONBLOCK)
  380. return -EAGAIN;
  381. ret = wait_event_interruptible(port->waitqueue,
  382. wait_is_over(port));
  383. if (ret < 0)
  384. return ret;
  385. }
  386. /*
  387. * We could've received a disconnection message while we were
  388. * waiting for more data.
  389. *
  390. * This check is not clubbed in the if() statement above as we
  391. * might receive some data as well as the host could get
  392. * disconnected after we got woken up from our wait. So we
  393. * really want to give off whatever data we have and only then
  394. * check for host_connected.
  395. */
  396. if (!port_has_data(port) && !port->host_connected)
  397. return 0;
  398. return fill_readbuf(port, ubuf, count, true);
  399. }
  400. static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
  401. size_t count, loff_t *offp)
  402. {
  403. struct port *port;
  404. char *buf;
  405. ssize_t ret;
  406. port = filp->private_data;
  407. count = min((size_t)(32 * 1024), count);
  408. buf = kmalloc(count, GFP_KERNEL);
  409. if (!buf)
  410. return -ENOMEM;
  411. ret = copy_from_user(buf, ubuf, count);
  412. if (ret) {
  413. ret = -EFAULT;
  414. goto free_buf;
  415. }
  416. ret = send_buf(port, buf, count);
  417. free_buf:
  418. kfree(buf);
  419. return ret;
  420. }
  421. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  422. {
  423. struct port *port;
  424. unsigned int ret;
  425. port = filp->private_data;
  426. poll_wait(filp, &port->waitqueue, wait);
  427. ret = 0;
  428. if (port->inbuf)
  429. ret |= POLLIN | POLLRDNORM;
  430. if (port->host_connected)
  431. ret |= POLLOUT;
  432. if (!port->host_connected)
  433. ret |= POLLHUP;
  434. return ret;
  435. }
  436. static int port_fops_release(struct inode *inode, struct file *filp)
  437. {
  438. struct port *port;
  439. port = filp->private_data;
  440. /* Notify host of port being closed */
  441. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  442. port->guest_connected = false;
  443. return 0;
  444. }
  445. static int port_fops_open(struct inode *inode, struct file *filp)
  446. {
  447. struct cdev *cdev = inode->i_cdev;
  448. struct port *port;
  449. port = container_of(cdev, struct port, cdev);
  450. filp->private_data = port;
  451. /*
  452. * Don't allow opening of console port devices -- that's done
  453. * via /dev/hvc
  454. */
  455. if (is_console_port(port))
  456. return -ENXIO;
  457. /* Allow only one process to open a particular port at a time */
  458. spin_lock_irq(&port->inbuf_lock);
  459. if (port->guest_connected) {
  460. spin_unlock_irq(&port->inbuf_lock);
  461. return -EMFILE;
  462. }
  463. port->guest_connected = true;
  464. spin_unlock_irq(&port->inbuf_lock);
  465. /* Notify host of port being opened */
  466. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  467. return 0;
  468. }
  469. /*
  470. * The file operations that we support: programs in the guest can open
  471. * a console device, read from it, write to it, poll for data and
  472. * close it. The devices are at
  473. * /dev/vport<device number>p<port number>
  474. */
  475. static const struct file_operations port_fops = {
  476. .owner = THIS_MODULE,
  477. .open = port_fops_open,
  478. .read = port_fops_read,
  479. .write = port_fops_write,
  480. .poll = port_fops_poll,
  481. .release = port_fops_release,
  482. };
  483. /*
  484. * The put_chars() callback is pretty straightforward.
  485. *
  486. * We turn the characters into a scatter-gather list, add it to the
  487. * output queue and then kick the Host. Then we sit here waiting for
  488. * it to finish: inefficient in theory, but in practice
  489. * implementations will do it immediately (lguest's Launcher does).
  490. */
  491. static int put_chars(u32 vtermno, const char *buf, int count)
  492. {
  493. struct port *port;
  494. port = find_port_by_vtermno(vtermno);
  495. if (!port)
  496. return 0;
  497. if (unlikely(early_put_chars))
  498. return early_put_chars(vtermno, buf, count);
  499. return send_buf(port, (void *)buf, count);
  500. }
  501. /*
  502. * get_chars() is the callback from the hvc_console infrastructure
  503. * when an interrupt is received.
  504. *
  505. * We call out to fill_readbuf that gets us the required data from the
  506. * buffers that are queued up.
  507. */
  508. static int get_chars(u32 vtermno, char *buf, int count)
  509. {
  510. struct port *port;
  511. port = find_port_by_vtermno(vtermno);
  512. if (!port)
  513. return 0;
  514. /* If we don't have an input queue yet, we can't get input. */
  515. BUG_ON(!port->in_vq);
  516. return fill_readbuf(port, buf, count, false);
  517. }
  518. static void resize_console(struct port *port)
  519. {
  520. struct virtio_device *vdev;
  521. struct winsize ws;
  522. vdev = port->portdev->vdev;
  523. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
  524. vdev->config->get(vdev,
  525. offsetof(struct virtio_console_config, cols),
  526. &ws.ws_col, sizeof(u16));
  527. vdev->config->get(vdev,
  528. offsetof(struct virtio_console_config, rows),
  529. &ws.ws_row, sizeof(u16));
  530. hvc_resize(port->cons.hvc, ws);
  531. }
  532. }
  533. static void virtcons_apply_config(struct virtio_device *vdev)
  534. {
  535. resize_console(find_port_by_vtermno(0));
  536. }
  537. /* We set the configuration at this point, since we now have a tty */
  538. static int notifier_add_vio(struct hvc_struct *hp, int data)
  539. {
  540. struct port *port;
  541. port = find_port_by_vtermno(hp->vtermno);
  542. if (!port)
  543. return -EINVAL;
  544. hp->irq_requested = 1;
  545. resize_console(port);
  546. return 0;
  547. }
  548. static void notifier_del_vio(struct hvc_struct *hp, int data)
  549. {
  550. hp->irq_requested = 0;
  551. }
  552. /* The operations for console ports. */
  553. static const struct hv_ops hv_ops = {
  554. .get_chars = get_chars,
  555. .put_chars = put_chars,
  556. .notifier_add = notifier_add_vio,
  557. .notifier_del = notifier_del_vio,
  558. .notifier_hangup = notifier_del_vio,
  559. };
  560. /*
  561. * Console drivers are initialized very early so boot messages can go
  562. * out, so we do things slightly differently from the generic virtio
  563. * initialization of the net and block drivers.
  564. *
  565. * At this stage, the console is output-only. It's too early to set
  566. * up a virtqueue, so we let the drivers do some boutique early-output
  567. * thing.
  568. */
  569. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  570. {
  571. early_put_chars = put_chars;
  572. return hvc_instantiate(0, 0, &hv_ops);
  573. }
  574. int init_port_console(struct port *port)
  575. {
  576. int ret;
  577. /*
  578. * The Host's telling us this port is a console port. Hook it
  579. * up with an hvc console.
  580. *
  581. * To set up and manage our virtual console, we call
  582. * hvc_alloc().
  583. *
  584. * The first argument of hvc_alloc() is the virtual console
  585. * number. The second argument is the parameter for the
  586. * notification mechanism (like irq number). We currently
  587. * leave this as zero, virtqueues have implicit notifications.
  588. *
  589. * The third argument is a "struct hv_ops" containing the
  590. * put_chars() get_chars(), notifier_add() and notifier_del()
  591. * pointers. The final argument is the output buffer size: we
  592. * can do any size, so we put PAGE_SIZE here.
  593. */
  594. port->cons.vtermno = pdrvdata.next_vtermno;
  595. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  596. if (IS_ERR(port->cons.hvc)) {
  597. ret = PTR_ERR(port->cons.hvc);
  598. port->cons.hvc = NULL;
  599. return ret;
  600. }
  601. spin_lock_irq(&pdrvdata_lock);
  602. pdrvdata.next_vtermno++;
  603. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  604. spin_unlock_irq(&pdrvdata_lock);
  605. port->guest_connected = true;
  606. /* Notify host of port being opened */
  607. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  608. return 0;
  609. }
  610. static ssize_t show_port_name(struct device *dev,
  611. struct device_attribute *attr, char *buffer)
  612. {
  613. struct port *port;
  614. port = dev_get_drvdata(dev);
  615. return sprintf(buffer, "%s\n", port->name);
  616. }
  617. static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
  618. static struct attribute *port_sysfs_entries[] = {
  619. &dev_attr_name.attr,
  620. NULL
  621. };
  622. static struct attribute_group port_attribute_group = {
  623. .name = NULL, /* put in device directory */
  624. .attrs = port_sysfs_entries,
  625. };
  626. /* Any private messages that the Host and Guest want to share */
  627. static void handle_control_message(struct ports_device *portdev,
  628. struct port_buffer *buf)
  629. {
  630. struct virtio_console_control *cpkt;
  631. struct port *port;
  632. size_t name_size;
  633. int err;
  634. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  635. port = find_port_by_id(portdev, cpkt->id);
  636. if (!port) {
  637. /* No valid header at start of buffer. Drop it. */
  638. dev_dbg(&portdev->vdev->dev,
  639. "Invalid index %u in control packet\n", cpkt->id);
  640. return;
  641. }
  642. switch (cpkt->event) {
  643. case VIRTIO_CONSOLE_CONSOLE_PORT:
  644. if (!cpkt->value)
  645. break;
  646. if (is_console_port(port))
  647. break;
  648. init_port_console(port);
  649. /*
  650. * Could remove the port here in case init fails - but
  651. * have to notify the host first.
  652. */
  653. break;
  654. case VIRTIO_CONSOLE_RESIZE:
  655. if (!is_console_port(port))
  656. break;
  657. port->cons.hvc->irq_requested = 1;
  658. resize_console(port);
  659. break;
  660. case VIRTIO_CONSOLE_PORT_OPEN:
  661. port->host_connected = cpkt->value;
  662. wake_up_interruptible(&port->waitqueue);
  663. break;
  664. case VIRTIO_CONSOLE_PORT_NAME:
  665. /*
  666. * Skip the size of the header and the cpkt to get the size
  667. * of the name that was sent
  668. */
  669. name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
  670. port->name = kmalloc(name_size, GFP_KERNEL);
  671. if (!port->name) {
  672. dev_err(port->dev,
  673. "Not enough space to store port name\n");
  674. break;
  675. }
  676. strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
  677. name_size - 1);
  678. port->name[name_size - 1] = 0;
  679. /*
  680. * Since we only have one sysfs attribute, 'name',
  681. * create it only if we have a name for the port.
  682. */
  683. err = sysfs_create_group(&port->dev->kobj,
  684. &port_attribute_group);
  685. if (err)
  686. dev_err(port->dev,
  687. "Error %d creating sysfs device attributes\n",
  688. err);
  689. break;
  690. }
  691. }
  692. static void control_work_handler(struct work_struct *work)
  693. {
  694. struct ports_device *portdev;
  695. struct virtqueue *vq;
  696. struct port_buffer *buf;
  697. unsigned int len;
  698. portdev = container_of(work, struct ports_device, control_work);
  699. vq = portdev->c_ivq;
  700. spin_lock(&portdev->cvq_lock);
  701. while ((buf = vq->vq_ops->get_buf(vq, &len))) {
  702. spin_unlock(&portdev->cvq_lock);
  703. buf->len = len;
  704. buf->offset = 0;
  705. handle_control_message(portdev, buf);
  706. spin_lock(&portdev->cvq_lock);
  707. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  708. dev_warn(&portdev->vdev->dev,
  709. "Error adding buffer to queue\n");
  710. free_buf(buf);
  711. }
  712. }
  713. spin_unlock(&portdev->cvq_lock);
  714. }
  715. static void in_intr(struct virtqueue *vq)
  716. {
  717. struct port *port;
  718. unsigned long flags;
  719. port = find_port_by_vq(vq->vdev->priv, vq);
  720. if (!port)
  721. return;
  722. spin_lock_irqsave(&port->inbuf_lock, flags);
  723. port->inbuf = get_inbuf(port);
  724. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  725. wake_up_interruptible(&port->waitqueue);
  726. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  727. hvc_kick();
  728. }
  729. static void control_intr(struct virtqueue *vq)
  730. {
  731. struct ports_device *portdev;
  732. portdev = vq->vdev->priv;
  733. schedule_work(&portdev->control_work);
  734. }
  735. static void fill_queue(struct virtqueue *vq, spinlock_t *lock)
  736. {
  737. struct port_buffer *buf;
  738. int ret;
  739. do {
  740. buf = alloc_buf(PAGE_SIZE);
  741. if (!buf)
  742. break;
  743. spin_lock_irq(lock);
  744. ret = add_inbuf(vq, buf);
  745. if (ret < 0) {
  746. spin_unlock_irq(lock);
  747. free_buf(buf);
  748. break;
  749. }
  750. spin_unlock_irq(lock);
  751. } while (ret > 0);
  752. }
  753. static int add_port(struct ports_device *portdev, u32 id)
  754. {
  755. struct port *port;
  756. struct port_buffer *inbuf;
  757. dev_t devt;
  758. int err;
  759. port = kmalloc(sizeof(*port), GFP_KERNEL);
  760. if (!port) {
  761. err = -ENOMEM;
  762. goto fail;
  763. }
  764. port->portdev = portdev;
  765. port->id = id;
  766. port->name = NULL;
  767. port->inbuf = NULL;
  768. port->cons.hvc = NULL;
  769. port->host_connected = port->guest_connected = false;
  770. port->in_vq = portdev->in_vqs[port->id];
  771. port->out_vq = portdev->out_vqs[port->id];
  772. cdev_init(&port->cdev, &port_fops);
  773. devt = MKDEV(portdev->chr_major, id);
  774. err = cdev_add(&port->cdev, devt, 1);
  775. if (err < 0) {
  776. dev_err(&port->portdev->vdev->dev,
  777. "Error %d adding cdev for port %u\n", err, id);
  778. goto free_port;
  779. }
  780. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  781. devt, port, "vport%up%u",
  782. port->portdev->drv_index, id);
  783. if (IS_ERR(port->dev)) {
  784. err = PTR_ERR(port->dev);
  785. dev_err(&port->portdev->vdev->dev,
  786. "Error %d creating device for port %u\n",
  787. err, id);
  788. goto free_cdev;
  789. }
  790. spin_lock_init(&port->inbuf_lock);
  791. init_waitqueue_head(&port->waitqueue);
  792. inbuf = alloc_buf(PAGE_SIZE);
  793. if (!inbuf) {
  794. err = -ENOMEM;
  795. goto free_device;
  796. }
  797. /* Register the input buffer the first time. */
  798. add_inbuf(port->in_vq, inbuf);
  799. /*
  800. * If we're not using multiport support, this has to be a console port
  801. */
  802. if (!use_multiport(port->portdev)) {
  803. err = init_port_console(port);
  804. if (err)
  805. goto free_inbuf;
  806. }
  807. spin_lock_irq(&portdev->ports_lock);
  808. list_add_tail(&port->list, &port->portdev->ports);
  809. spin_unlock_irq(&portdev->ports_lock);
  810. /*
  811. * Tell the Host we're set so that it can send us various
  812. * configuration parameters for this port (eg, port name,
  813. * caching, whether this is a console port, etc.)
  814. */
  815. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  816. return 0;
  817. free_inbuf:
  818. free_buf(inbuf);
  819. free_device:
  820. device_destroy(pdrvdata.class, port->dev->devt);
  821. free_cdev:
  822. cdev_del(&port->cdev);
  823. free_port:
  824. kfree(port);
  825. fail:
  826. return err;
  827. }
  828. static int init_vqs(struct ports_device *portdev)
  829. {
  830. vq_callback_t **io_callbacks;
  831. char **io_names;
  832. struct virtqueue **vqs;
  833. u32 i, j, nr_ports, nr_queues;
  834. int err;
  835. nr_ports = portdev->config.max_nr_ports;
  836. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  837. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  838. if (!vqs) {
  839. err = -ENOMEM;
  840. goto fail;
  841. }
  842. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  843. if (!io_callbacks) {
  844. err = -ENOMEM;
  845. goto free_vqs;
  846. }
  847. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  848. if (!io_names) {
  849. err = -ENOMEM;
  850. goto free_callbacks;
  851. }
  852. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  853. GFP_KERNEL);
  854. if (!portdev->in_vqs) {
  855. err = -ENOMEM;
  856. goto free_names;
  857. }
  858. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  859. GFP_KERNEL);
  860. if (!portdev->out_vqs) {
  861. err = -ENOMEM;
  862. goto free_invqs;
  863. }
  864. /*
  865. * For backward compat (newer host but older guest), the host
  866. * spawns a console port first and also inits the vqs for port
  867. * 0 before others.
  868. */
  869. j = 0;
  870. io_callbacks[j] = in_intr;
  871. io_callbacks[j + 1] = NULL;
  872. io_names[j] = "input";
  873. io_names[j + 1] = "output";
  874. j += 2;
  875. if (use_multiport(portdev)) {
  876. io_callbacks[j] = control_intr;
  877. io_callbacks[j + 1] = NULL;
  878. io_names[j] = "control-i";
  879. io_names[j + 1] = "control-o";
  880. for (i = 1; i < nr_ports; i++) {
  881. j += 2;
  882. io_callbacks[j] = in_intr;
  883. io_callbacks[j + 1] = NULL;
  884. io_names[j] = "input";
  885. io_names[j + 1] = "output";
  886. }
  887. }
  888. /* Find the queues. */
  889. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  890. io_callbacks,
  891. (const char **)io_names);
  892. if (err)
  893. goto free_outvqs;
  894. j = 0;
  895. portdev->in_vqs[0] = vqs[0];
  896. portdev->out_vqs[0] = vqs[1];
  897. j += 2;
  898. if (use_multiport(portdev)) {
  899. portdev->c_ivq = vqs[j];
  900. portdev->c_ovq = vqs[j + 1];
  901. for (i = 1; i < nr_ports; i++) {
  902. j += 2;
  903. portdev->in_vqs[i] = vqs[j];
  904. portdev->out_vqs[i] = vqs[j + 1];
  905. }
  906. }
  907. kfree(io_callbacks);
  908. kfree(io_names);
  909. kfree(vqs);
  910. return 0;
  911. free_names:
  912. kfree(io_names);
  913. free_callbacks:
  914. kfree(io_callbacks);
  915. free_outvqs:
  916. kfree(portdev->out_vqs);
  917. free_invqs:
  918. kfree(portdev->in_vqs);
  919. free_vqs:
  920. kfree(vqs);
  921. fail:
  922. return err;
  923. }
  924. static const struct file_operations portdev_fops = {
  925. .owner = THIS_MODULE,
  926. };
  927. /*
  928. * Once we're further in boot, we get probed like any other virtio
  929. * device.
  930. *
  931. * If the host also supports multiple console ports, we check the
  932. * config space to see how many ports the host has spawned. We
  933. * initialize each port found.
  934. */
  935. static int __devinit virtcons_probe(struct virtio_device *vdev)
  936. {
  937. struct ports_device *portdev;
  938. u32 i;
  939. int err;
  940. bool multiport;
  941. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  942. if (!portdev) {
  943. err = -ENOMEM;
  944. goto fail;
  945. }
  946. /* Attach this portdev to this virtio_device, and vice-versa. */
  947. portdev->vdev = vdev;
  948. vdev->priv = portdev;
  949. spin_lock_irq(&pdrvdata_lock);
  950. portdev->drv_index = pdrvdata.index++;
  951. spin_unlock_irq(&pdrvdata_lock);
  952. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  953. &portdev_fops);
  954. if (portdev->chr_major < 0) {
  955. dev_err(&vdev->dev,
  956. "Error %d registering chrdev for device %u\n",
  957. portdev->chr_major, portdev->drv_index);
  958. err = portdev->chr_major;
  959. goto free;
  960. }
  961. multiport = false;
  962. portdev->config.nr_ports = 1;
  963. portdev->config.max_nr_ports = 1;
  964. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
  965. multiport = true;
  966. vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
  967. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  968. nr_ports),
  969. &portdev->config.nr_ports,
  970. sizeof(portdev->config.nr_ports));
  971. vdev->config->get(vdev, offsetof(struct virtio_console_config,
  972. max_nr_ports),
  973. &portdev->config.max_nr_ports,
  974. sizeof(portdev->config.max_nr_ports));
  975. if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
  976. dev_warn(&vdev->dev,
  977. "More ports (%u) specified than allowed (%u). Will init %u ports.",
  978. portdev->config.nr_ports,
  979. portdev->config.max_nr_ports,
  980. portdev->config.max_nr_ports);
  981. portdev->config.nr_ports = portdev->config.max_nr_ports;
  982. }
  983. }
  984. /* Let the Host know we support multiple ports.*/
  985. vdev->config->finalize_features(vdev);
  986. err = init_vqs(portdev);
  987. if (err < 0) {
  988. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  989. goto free_chrdev;
  990. }
  991. spin_lock_init(&portdev->ports_lock);
  992. INIT_LIST_HEAD(&portdev->ports);
  993. if (multiport) {
  994. spin_lock_init(&portdev->cvq_lock);
  995. INIT_WORK(&portdev->control_work, &control_work_handler);
  996. fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  997. }
  998. for (i = 0; i < portdev->config.nr_ports; i++)
  999. add_port(portdev, i);
  1000. /* Start using the new console output. */
  1001. early_put_chars = NULL;
  1002. return 0;
  1003. free_chrdev:
  1004. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1005. free:
  1006. kfree(portdev);
  1007. fail:
  1008. return err;
  1009. }
  1010. static struct virtio_device_id id_table[] = {
  1011. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  1012. { 0 },
  1013. };
  1014. static unsigned int features[] = {
  1015. VIRTIO_CONSOLE_F_SIZE,
  1016. VIRTIO_CONSOLE_F_MULTIPORT,
  1017. };
  1018. static struct virtio_driver virtio_console = {
  1019. .feature_table = features,
  1020. .feature_table_size = ARRAY_SIZE(features),
  1021. .driver.name = KBUILD_MODNAME,
  1022. .driver.owner = THIS_MODULE,
  1023. .id_table = id_table,
  1024. .probe = virtcons_probe,
  1025. .config_changed = virtcons_apply_config,
  1026. };
  1027. static int __init init(void)
  1028. {
  1029. int err;
  1030. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  1031. if (IS_ERR(pdrvdata.class)) {
  1032. err = PTR_ERR(pdrvdata.class);
  1033. pr_err("Error %d creating virtio-ports class\n", err);
  1034. return err;
  1035. }
  1036. INIT_LIST_HEAD(&pdrvdata.consoles);
  1037. return register_virtio_driver(&virtio_console);
  1038. }
  1039. module_init(init);
  1040. MODULE_DEVICE_TABLE(virtio, id_table);
  1041. MODULE_DESCRIPTION("Virtio console driver");
  1042. MODULE_LICENSE("GPL");