virtio_console.c 44 KB

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