virtio_console.c 44 KB

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