virtio_console.c 37 KB

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