virtio_console.c 48 KB

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