virtio_console.c 45 KB

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