virtio_console.c 40 KB

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