virtio_console.c 38 KB

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