hvc_console.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2001 Anton Blanchard <[email protected]>, IBM
  4. * Copyright (C) 2001 Paul Mackerras <[email protected]>, IBM
  5. * Copyright (C) 2004 Benjamin Herrenschmidt <[email protected]>, IBM Corp.
  6. * Copyright (C) 2004 IBM Corporation
  7. *
  8. * Additional Author(s):
  9. * Ryan S. Arnold <[email protected]>
  10. */
  11. #include <linux/console.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/init.h>
  14. #include <linux/kbd_kern.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kthread.h>
  17. #include <linux/list.h>
  18. #include <linux/major.h>
  19. #include <linux/atomic.h>
  20. #include <linux/sysrq.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/sched.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/delay.h>
  26. #include <linux/freezer.h>
  27. #include <linux/slab.h>
  28. #include <linux/serial_core.h>
  29. #include <linux/uaccess.h>
  30. #include "hvc_console.h"
  31. #define HVC_MAJOR 229
  32. #define HVC_MINOR 0
  33. /*
  34. * Wait this long per iteration while trying to push buffered data to the
  35. * hypervisor before allowing the tty to complete a close operation.
  36. */
  37. #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
  38. /*
  39. * These sizes are most efficient for vio, because they are the
  40. * native transfer size. We could make them selectable in the
  41. * future to better deal with backends that want other buffer sizes.
  42. */
  43. #define N_OUTBUF 16
  44. #define N_INBUF 16
  45. #define __ALIGNED__ __attribute__((__aligned__(L1_CACHE_BYTES)))
  46. static struct tty_driver *hvc_driver;
  47. static struct task_struct *hvc_task;
  48. /* Picks up late kicks after list walk but before schedule() */
  49. static int hvc_kicked;
  50. /* hvc_init is triggered from hvc_alloc, i.e. only when actually used */
  51. static atomic_t hvc_needs_init __read_mostly = ATOMIC_INIT(-1);
  52. static int hvc_init(void);
  53. #ifdef CONFIG_MAGIC_SYSRQ
  54. static int sysrq_pressed;
  55. #endif
  56. /* dynamic list of hvc_struct instances */
  57. static LIST_HEAD(hvc_structs);
  58. /*
  59. * Protect the list of hvc_struct instances from inserts and removals during
  60. * list traversal.
  61. */
  62. static DEFINE_MUTEX(hvc_structs_mutex);
  63. /*
  64. * This value is used to assign a tty->index value to a hvc_struct based
  65. * upon order of exposure via hvc_probe(), when we can not match it to
  66. * a console candidate registered with hvc_instantiate().
  67. */
  68. static int last_hvc = -1;
  69. /*
  70. * Do not call this function with either the hvc_structs_mutex or the hvc_struct
  71. * lock held. If successful, this function increments the kref reference
  72. * count against the target hvc_struct so it should be released when finished.
  73. */
  74. static struct hvc_struct *hvc_get_by_index(int index)
  75. {
  76. struct hvc_struct *hp;
  77. unsigned long flags;
  78. mutex_lock(&hvc_structs_mutex);
  79. list_for_each_entry(hp, &hvc_structs, next) {
  80. spin_lock_irqsave(&hp->lock, flags);
  81. if (hp->index == index) {
  82. tty_port_get(&hp->port);
  83. spin_unlock_irqrestore(&hp->lock, flags);
  84. mutex_unlock(&hvc_structs_mutex);
  85. return hp;
  86. }
  87. spin_unlock_irqrestore(&hp->lock, flags);
  88. }
  89. hp = NULL;
  90. mutex_unlock(&hvc_structs_mutex);
  91. return hp;
  92. }
  93. static int __hvc_flush(const struct hv_ops *ops, uint32_t vtermno, bool wait)
  94. {
  95. if (wait)
  96. might_sleep();
  97. if (ops->flush)
  98. return ops->flush(vtermno, wait);
  99. return 0;
  100. }
  101. static int hvc_console_flush(const struct hv_ops *ops, uint32_t vtermno)
  102. {
  103. return __hvc_flush(ops, vtermno, false);
  104. }
  105. /*
  106. * Wait for the console to flush before writing more to it. This sleeps.
  107. */
  108. static int hvc_flush(struct hvc_struct *hp)
  109. {
  110. return __hvc_flush(hp->ops, hp->vtermno, true);
  111. }
  112. /*
  113. * Initial console vtermnos for console API usage prior to full console
  114. * initialization. Any vty adapter outside this range will not have usable
  115. * console interfaces but can still be used as a tty device. This has to be
  116. * static because kmalloc will not work during early console init.
  117. */
  118. static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
  119. static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
  120. {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
  121. /*
  122. * Console APIs, NOT TTY. These APIs are available immediately when
  123. * hvc_console_setup() finds adapters.
  124. */
  125. static void hvc_console_print(struct console *co, const char *b,
  126. unsigned count)
  127. {
  128. char c[N_OUTBUF] __ALIGNED__;
  129. unsigned i = 0, n = 0;
  130. int r, donecr = 0, index = co->index;
  131. /* Console access attempt outside of acceptable console range. */
  132. if (index >= MAX_NR_HVC_CONSOLES)
  133. return;
  134. /* This console adapter was removed so it is not usable. */
  135. if (vtermnos[index] == -1)
  136. return;
  137. while (count > 0 || i > 0) {
  138. if (count > 0 && i < sizeof(c)) {
  139. if (b[n] == '\n' && !donecr) {
  140. c[i++] = '\r';
  141. donecr = 1;
  142. } else {
  143. c[i++] = b[n++];
  144. donecr = 0;
  145. --count;
  146. }
  147. } else {
  148. r = cons_ops[index]->put_chars(vtermnos[index], c, i);
  149. if (r <= 0) {
  150. /* throw away characters on error
  151. * but spin in case of -EAGAIN */
  152. if (r != -EAGAIN) {
  153. i = 0;
  154. } else {
  155. hvc_console_flush(cons_ops[index],
  156. vtermnos[index]);
  157. }
  158. } else if (r > 0) {
  159. i -= r;
  160. if (i > 0)
  161. memmove(c, c+r, i);
  162. }
  163. }
  164. }
  165. hvc_console_flush(cons_ops[index], vtermnos[index]);
  166. }
  167. static struct tty_driver *hvc_console_device(struct console *c, int *index)
  168. {
  169. if (vtermnos[c->index] == -1)
  170. return NULL;
  171. *index = c->index;
  172. return hvc_driver;
  173. }
  174. static int hvc_console_setup(struct console *co, char *options)
  175. {
  176. if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
  177. return -ENODEV;
  178. if (vtermnos[co->index] == -1)
  179. return -ENODEV;
  180. return 0;
  181. }
  182. static struct console hvc_console = {
  183. .name = "hvc",
  184. .write = hvc_console_print,
  185. .device = hvc_console_device,
  186. .setup = hvc_console_setup,
  187. .flags = CON_PRINTBUFFER,
  188. .index = -1,
  189. };
  190. /*
  191. * Early console initialization. Precedes driver initialization.
  192. *
  193. * (1) we are first, and the user specified another driver
  194. * -- index will remain -1
  195. * (2) we are first and the user specified no driver
  196. * -- index will be set to 0, then we will fail setup.
  197. * (3) we are first and the user specified our driver
  198. * -- index will be set to user specified driver, and we will fail
  199. * (4) we are after driver, and this initcall will register us
  200. * -- if the user didn't specify a driver then the console will match
  201. *
  202. * Note that for cases 2 and 3, we will match later when the io driver
  203. * calls hvc_instantiate() and call register again.
  204. */
  205. static int __init hvc_console_init(void)
  206. {
  207. register_console(&hvc_console);
  208. return 0;
  209. }
  210. console_initcall(hvc_console_init);
  211. /* callback when the kboject ref count reaches zero. */
  212. static void hvc_port_destruct(struct tty_port *port)
  213. {
  214. struct hvc_struct *hp = container_of(port, struct hvc_struct, port);
  215. unsigned long flags;
  216. mutex_lock(&hvc_structs_mutex);
  217. spin_lock_irqsave(&hp->lock, flags);
  218. list_del(&(hp->next));
  219. spin_unlock_irqrestore(&hp->lock, flags);
  220. mutex_unlock(&hvc_structs_mutex);
  221. kfree(hp);
  222. }
  223. static void hvc_check_console(int index)
  224. {
  225. /* Already enabled, bail out */
  226. if (hvc_console.flags & CON_ENABLED)
  227. return;
  228. /* If this index is what the user requested, then register
  229. * now (setup won't fail at this point). It's ok to just
  230. * call register again if previously .setup failed.
  231. */
  232. if (index == hvc_console.index)
  233. register_console(&hvc_console);
  234. }
  235. /*
  236. * hvc_instantiate() is an early console discovery method which locates
  237. * consoles * prior to the vio subsystem discovering them. Hotplugged
  238. * vty adapters do NOT get an hvc_instantiate() callback since they
  239. * appear after early console init.
  240. */
  241. int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
  242. {
  243. struct hvc_struct *hp;
  244. if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
  245. return -1;
  246. if (vtermnos[index] != -1)
  247. return -1;
  248. /* make sure no tty has been registered in this index */
  249. hp = hvc_get_by_index(index);
  250. if (hp) {
  251. tty_port_put(&hp->port);
  252. return -1;
  253. }
  254. vtermnos[index] = vtermno;
  255. cons_ops[index] = ops;
  256. /* check if we need to re-register the kernel console */
  257. hvc_check_console(index);
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(hvc_instantiate);
  261. /* Wake the sleeping khvcd */
  262. void hvc_kick(void)
  263. {
  264. hvc_kicked = 1;
  265. wake_up_process(hvc_task);
  266. }
  267. EXPORT_SYMBOL_GPL(hvc_kick);
  268. static void hvc_unthrottle(struct tty_struct *tty)
  269. {
  270. hvc_kick();
  271. }
  272. static int hvc_install(struct tty_driver *driver, struct tty_struct *tty)
  273. {
  274. struct hvc_struct *hp;
  275. int rc;
  276. /* Auto increments kref reference if found. */
  277. hp = hvc_get_by_index(tty->index);
  278. if (!hp)
  279. return -ENODEV;
  280. tty->driver_data = hp;
  281. rc = tty_port_install(&hp->port, driver, tty);
  282. if (rc)
  283. tty_port_put(&hp->port);
  284. return rc;
  285. }
  286. /*
  287. * The TTY interface won't be used until after the vio layer has exposed the vty
  288. * adapter to the kernel.
  289. */
  290. static int hvc_open(struct tty_struct *tty, struct file * filp)
  291. {
  292. struct hvc_struct *hp = tty->driver_data;
  293. unsigned long flags;
  294. int rc = 0;
  295. spin_lock_irqsave(&hp->port.lock, flags);
  296. /* Check and then increment for fast path open. */
  297. if (hp->port.count++ > 0) {
  298. spin_unlock_irqrestore(&hp->port.lock, flags);
  299. hvc_kick();
  300. return 0;
  301. } /* else count == 0 */
  302. spin_unlock_irqrestore(&hp->port.lock, flags);
  303. tty_port_tty_set(&hp->port, tty);
  304. if (hp->ops->notifier_add)
  305. rc = hp->ops->notifier_add(hp, hp->data);
  306. /*
  307. * If the notifier fails we return an error. The tty layer
  308. * will call hvc_close() after a failed open but we don't want to clean
  309. * up there so we'll clean up here and clear out the previously set
  310. * tty fields and return the kref reference.
  311. */
  312. if (rc) {
  313. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  314. } else {
  315. /* We are ready... raise DTR/RTS */
  316. if (C_BAUD(tty))
  317. if (hp->ops->dtr_rts)
  318. hp->ops->dtr_rts(hp, 1);
  319. tty_port_set_initialized(&hp->port, true);
  320. }
  321. /* Force wakeup of the polling thread */
  322. hvc_kick();
  323. return rc;
  324. }
  325. static void hvc_close(struct tty_struct *tty, struct file * filp)
  326. {
  327. struct hvc_struct *hp = tty->driver_data;
  328. unsigned long flags;
  329. if (tty_hung_up_p(filp))
  330. return;
  331. spin_lock_irqsave(&hp->port.lock, flags);
  332. if (--hp->port.count == 0) {
  333. spin_unlock_irqrestore(&hp->port.lock, flags);
  334. /* We are done with the tty pointer now. */
  335. tty_port_tty_set(&hp->port, NULL);
  336. if (!tty_port_initialized(&hp->port))
  337. return;
  338. if (C_HUPCL(tty))
  339. if (hp->ops->dtr_rts)
  340. hp->ops->dtr_rts(hp, 0);
  341. if (hp->ops->notifier_del)
  342. hp->ops->notifier_del(hp, hp->data);
  343. /* cancel pending tty resize work */
  344. cancel_work_sync(&hp->tty_resize);
  345. /*
  346. * Chain calls chars_in_buffer() and returns immediately if
  347. * there is no buffered data otherwise sleeps on a wait queue
  348. * waking periodically to check chars_in_buffer().
  349. */
  350. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  351. tty_port_set_initialized(&hp->port, false);
  352. } else {
  353. if (hp->port.count < 0)
  354. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  355. hp->vtermno, hp->port.count);
  356. spin_unlock_irqrestore(&hp->port.lock, flags);
  357. }
  358. }
  359. static void hvc_cleanup(struct tty_struct *tty)
  360. {
  361. struct hvc_struct *hp = tty->driver_data;
  362. tty_port_put(&hp->port);
  363. }
  364. static void hvc_hangup(struct tty_struct *tty)
  365. {
  366. struct hvc_struct *hp = tty->driver_data;
  367. unsigned long flags;
  368. if (!hp)
  369. return;
  370. /* cancel pending tty resize work */
  371. cancel_work_sync(&hp->tty_resize);
  372. spin_lock_irqsave(&hp->port.lock, flags);
  373. /*
  374. * The N_TTY line discipline has problems such that in a close vs
  375. * open->hangup case this can be called after the final close so prevent
  376. * that from happening for now.
  377. */
  378. if (hp->port.count <= 0) {
  379. spin_unlock_irqrestore(&hp->port.lock, flags);
  380. return;
  381. }
  382. hp->port.count = 0;
  383. spin_unlock_irqrestore(&hp->port.lock, flags);
  384. tty_port_tty_set(&hp->port, NULL);
  385. hp->n_outbuf = 0;
  386. if (hp->ops->notifier_hangup)
  387. hp->ops->notifier_hangup(hp, hp->data);
  388. }
  389. /*
  390. * Push buffered characters whether they were just recently buffered or waiting
  391. * on a blocked hypervisor. Call this function with hp->lock held.
  392. */
  393. static int hvc_push(struct hvc_struct *hp)
  394. {
  395. int n;
  396. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  397. if (n <= 0) {
  398. if (n == 0 || n == -EAGAIN) {
  399. hp->do_wakeup = 1;
  400. return 0;
  401. }
  402. /* throw away output on error; this happens when
  403. there is no session connected to the vterm. */
  404. hp->n_outbuf = 0;
  405. } else
  406. hp->n_outbuf -= n;
  407. if (hp->n_outbuf > 0)
  408. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  409. else
  410. hp->do_wakeup = 1;
  411. return n;
  412. }
  413. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  414. {
  415. struct hvc_struct *hp = tty->driver_data;
  416. unsigned long flags;
  417. int rsize, written = 0;
  418. /* This write was probably executed during a tty close. */
  419. if (!hp)
  420. return -EPIPE;
  421. /* FIXME what's this (unprotected) check for? */
  422. if (hp->port.count <= 0)
  423. return -EIO;
  424. while (count > 0) {
  425. int ret = 0;
  426. spin_lock_irqsave(&hp->lock, flags);
  427. rsize = hp->outbuf_size - hp->n_outbuf;
  428. if (rsize) {
  429. if (rsize > count)
  430. rsize = count;
  431. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  432. count -= rsize;
  433. buf += rsize;
  434. hp->n_outbuf += rsize;
  435. written += rsize;
  436. }
  437. if (hp->n_outbuf > 0)
  438. ret = hvc_push(hp);
  439. spin_unlock_irqrestore(&hp->lock, flags);
  440. if (!ret)
  441. break;
  442. if (count) {
  443. if (hp->n_outbuf > 0)
  444. hvc_flush(hp);
  445. cond_resched();
  446. }
  447. }
  448. /*
  449. * Racy, but harmless, kick thread if there is still pending data.
  450. */
  451. if (hp->n_outbuf)
  452. hvc_kick();
  453. return written;
  454. }
  455. /**
  456. * hvc_set_winsz() - Resize the hvc tty terminal window.
  457. * @work: work structure.
  458. *
  459. * The routine shall not be called within an atomic context because it
  460. * might sleep.
  461. *
  462. * Locking: hp->lock
  463. */
  464. static void hvc_set_winsz(struct work_struct *work)
  465. {
  466. struct hvc_struct *hp;
  467. unsigned long hvc_flags;
  468. struct tty_struct *tty;
  469. struct winsize ws;
  470. hp = container_of(work, struct hvc_struct, tty_resize);
  471. tty = tty_port_tty_get(&hp->port);
  472. if (!tty)
  473. return;
  474. spin_lock_irqsave(&hp->lock, hvc_flags);
  475. ws = hp->ws;
  476. spin_unlock_irqrestore(&hp->lock, hvc_flags);
  477. tty_do_resize(tty, &ws);
  478. tty_kref_put(tty);
  479. }
  480. /*
  481. * This is actually a contract between the driver and the tty layer outlining
  482. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  483. * driver MUST honor the return value.
  484. */
  485. static unsigned int hvc_write_room(struct tty_struct *tty)
  486. {
  487. struct hvc_struct *hp = tty->driver_data;
  488. if (!hp)
  489. return 0;
  490. return hp->outbuf_size - hp->n_outbuf;
  491. }
  492. static unsigned int hvc_chars_in_buffer(struct tty_struct *tty)
  493. {
  494. struct hvc_struct *hp = tty->driver_data;
  495. if (!hp)
  496. return 0;
  497. return hp->n_outbuf;
  498. }
  499. /*
  500. * timeout will vary between the MIN and MAX values defined here. By default
  501. * and during console activity we will use a default MIN_TIMEOUT of 10. When
  502. * the console is idle, we increase the timeout value on each pass through
  503. * msleep until we reach the max. This may be noticeable as a brief (average
  504. * one second) delay on the console before the console responds to input when
  505. * there has been no input for some time.
  506. */
  507. #define MIN_TIMEOUT (10)
  508. #define MAX_TIMEOUT (2000)
  509. static u32 timeout = MIN_TIMEOUT;
  510. /*
  511. * Maximum number of bytes to get from the console driver if hvc_poll is
  512. * called from driver (and can't sleep). Any more than this and we break
  513. * and start polling with khvcd. This value was derived from an OpenBMC
  514. * console with the OPAL driver that results in about 0.25ms interrupts off
  515. * latency.
  516. */
  517. #define HVC_ATOMIC_READ_MAX 128
  518. #define HVC_POLL_READ 0x00000001
  519. #define HVC_POLL_WRITE 0x00000002
  520. static int __hvc_poll(struct hvc_struct *hp, bool may_sleep)
  521. {
  522. struct tty_struct *tty;
  523. int i, n, count, poll_mask = 0;
  524. char buf[N_INBUF] __ALIGNED__;
  525. unsigned long flags;
  526. int read_total = 0;
  527. int written_total = 0;
  528. spin_lock_irqsave(&hp->lock, flags);
  529. /* Push pending writes */
  530. if (hp->n_outbuf > 0)
  531. written_total = hvc_push(hp);
  532. /* Reschedule us if still some write pending */
  533. if (hp->n_outbuf > 0) {
  534. poll_mask |= HVC_POLL_WRITE;
  535. /* If hvc_push() was not able to write, sleep a few msecs */
  536. timeout = (written_total) ? 0 : MIN_TIMEOUT;
  537. }
  538. if (may_sleep) {
  539. spin_unlock_irqrestore(&hp->lock, flags);
  540. cond_resched();
  541. spin_lock_irqsave(&hp->lock, flags);
  542. }
  543. /* No tty attached, just skip */
  544. tty = tty_port_tty_get(&hp->port);
  545. if (tty == NULL)
  546. goto bail;
  547. /* Now check if we can get data (are we throttled ?) */
  548. if (tty_throttled(tty))
  549. goto out;
  550. /* If we aren't notifier driven and aren't throttled, we always
  551. * request a reschedule
  552. */
  553. if (!hp->irq_requested)
  554. poll_mask |= HVC_POLL_READ;
  555. read_again:
  556. /* Read data if any */
  557. count = tty_buffer_request_room(&hp->port, N_INBUF);
  558. /* If flip is full, just reschedule a later read */
  559. if (count == 0) {
  560. poll_mask |= HVC_POLL_READ;
  561. goto out;
  562. }
  563. n = hp->ops->get_chars(hp->vtermno, buf, count);
  564. if (n <= 0) {
  565. /* Hangup the tty when disconnected from host */
  566. if (n == -EPIPE) {
  567. spin_unlock_irqrestore(&hp->lock, flags);
  568. tty_hangup(tty);
  569. spin_lock_irqsave(&hp->lock, flags);
  570. } else if ( n == -EAGAIN ) {
  571. /*
  572. * Some back-ends can only ensure a certain min
  573. * num of bytes read, which may be > 'count'.
  574. * Let the tty clear the flip buff to make room.
  575. */
  576. poll_mask |= HVC_POLL_READ;
  577. }
  578. goto out;
  579. }
  580. for (i = 0; i < n; ++i) {
  581. #ifdef CONFIG_MAGIC_SYSRQ
  582. if (hp->index == hvc_console.index) {
  583. /* Handle the SysRq Hack */
  584. /* XXX should support a sequence */
  585. if (buf[i] == '\x0f') { /* ^O */
  586. /* if ^O is pressed again, reset
  587. * sysrq_pressed and flip ^O char */
  588. sysrq_pressed = !sysrq_pressed;
  589. if (sysrq_pressed)
  590. continue;
  591. } else if (sysrq_pressed) {
  592. handle_sysrq(buf[i]);
  593. sysrq_pressed = 0;
  594. continue;
  595. }
  596. }
  597. #endif /* CONFIG_MAGIC_SYSRQ */
  598. tty_insert_flip_char(&hp->port, buf[i], 0);
  599. }
  600. read_total += n;
  601. if (may_sleep) {
  602. /* Keep going until the flip is full */
  603. spin_unlock_irqrestore(&hp->lock, flags);
  604. cond_resched();
  605. spin_lock_irqsave(&hp->lock, flags);
  606. goto read_again;
  607. } else if (read_total < HVC_ATOMIC_READ_MAX) {
  608. /* Break and defer if it's a large read in atomic */
  609. goto read_again;
  610. }
  611. /*
  612. * Latency break, schedule another poll immediately.
  613. */
  614. poll_mask |= HVC_POLL_READ;
  615. out:
  616. /* Wakeup write queue if necessary */
  617. if (hp->do_wakeup) {
  618. hp->do_wakeup = 0;
  619. tty_wakeup(tty);
  620. }
  621. bail:
  622. spin_unlock_irqrestore(&hp->lock, flags);
  623. if (read_total) {
  624. /* Activity is occurring, so reset the polling backoff value to
  625. a minimum for performance. */
  626. timeout = MIN_TIMEOUT;
  627. tty_flip_buffer_push(&hp->port);
  628. }
  629. tty_kref_put(tty);
  630. return poll_mask;
  631. }
  632. int hvc_poll(struct hvc_struct *hp)
  633. {
  634. return __hvc_poll(hp, false);
  635. }
  636. EXPORT_SYMBOL_GPL(hvc_poll);
  637. /**
  638. * __hvc_resize() - Update terminal window size information.
  639. * @hp: HVC console pointer
  640. * @ws: Terminal window size structure
  641. *
  642. * Stores the specified window size information in the hvc structure of @hp.
  643. * The function schedule the tty resize update.
  644. *
  645. * Locking: Locking free; the function MUST be called holding hp->lock
  646. */
  647. void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
  648. {
  649. hp->ws = ws;
  650. schedule_work(&hp->tty_resize);
  651. }
  652. EXPORT_SYMBOL_GPL(__hvc_resize);
  653. /*
  654. * This kthread is either polling or interrupt driven. This is determined by
  655. * calling hvc_poll() who determines whether a console adapter support
  656. * interrupts.
  657. */
  658. static int khvcd(void *unused)
  659. {
  660. int poll_mask;
  661. struct hvc_struct *hp;
  662. set_freezable();
  663. do {
  664. poll_mask = 0;
  665. hvc_kicked = 0;
  666. try_to_freeze();
  667. wmb();
  668. if (!cpus_are_in_xmon()) {
  669. mutex_lock(&hvc_structs_mutex);
  670. list_for_each_entry(hp, &hvc_structs, next) {
  671. poll_mask |= __hvc_poll(hp, true);
  672. cond_resched();
  673. }
  674. mutex_unlock(&hvc_structs_mutex);
  675. } else
  676. poll_mask |= HVC_POLL_READ;
  677. if (hvc_kicked)
  678. continue;
  679. set_current_state(TASK_INTERRUPTIBLE);
  680. if (!hvc_kicked) {
  681. if (poll_mask == 0)
  682. schedule();
  683. else {
  684. unsigned long j_timeout;
  685. if (timeout < MAX_TIMEOUT)
  686. timeout += (timeout >> 6) + 1;
  687. /*
  688. * We don't use msleep_interruptible otherwise
  689. * "kick" will fail to wake us up
  690. */
  691. j_timeout = msecs_to_jiffies(timeout) + 1;
  692. schedule_timeout_interruptible(j_timeout);
  693. }
  694. }
  695. __set_current_state(TASK_RUNNING);
  696. } while (!kthread_should_stop());
  697. return 0;
  698. }
  699. static int hvc_tiocmget(struct tty_struct *tty)
  700. {
  701. struct hvc_struct *hp = tty->driver_data;
  702. if (!hp || !hp->ops->tiocmget)
  703. return -EINVAL;
  704. return hp->ops->tiocmget(hp);
  705. }
  706. static int hvc_tiocmset(struct tty_struct *tty,
  707. unsigned int set, unsigned int clear)
  708. {
  709. struct hvc_struct *hp = tty->driver_data;
  710. if (!hp || !hp->ops->tiocmset)
  711. return -EINVAL;
  712. return hp->ops->tiocmset(hp, set, clear);
  713. }
  714. #ifdef CONFIG_CONSOLE_POLL
  715. static int hvc_poll_init(struct tty_driver *driver, int line, char *options)
  716. {
  717. return 0;
  718. }
  719. static int hvc_poll_get_char(struct tty_driver *driver, int line)
  720. {
  721. struct tty_struct *tty = driver->ttys[0];
  722. struct hvc_struct *hp = tty->driver_data;
  723. int n;
  724. char ch;
  725. n = hp->ops->get_chars(hp->vtermno, &ch, 1);
  726. if (n <= 0)
  727. return NO_POLL_CHAR;
  728. return ch;
  729. }
  730. static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
  731. {
  732. struct tty_struct *tty = driver->ttys[0];
  733. struct hvc_struct *hp = tty->driver_data;
  734. int n;
  735. do {
  736. n = hp->ops->put_chars(hp->vtermno, &ch, 1);
  737. } while (n <= 0);
  738. }
  739. #endif
  740. static const struct tty_operations hvc_ops = {
  741. .install = hvc_install,
  742. .open = hvc_open,
  743. .close = hvc_close,
  744. .cleanup = hvc_cleanup,
  745. .write = hvc_write,
  746. .hangup = hvc_hangup,
  747. .unthrottle = hvc_unthrottle,
  748. .write_room = hvc_write_room,
  749. .chars_in_buffer = hvc_chars_in_buffer,
  750. .tiocmget = hvc_tiocmget,
  751. .tiocmset = hvc_tiocmset,
  752. #ifdef CONFIG_CONSOLE_POLL
  753. .poll_init = hvc_poll_init,
  754. .poll_get_char = hvc_poll_get_char,
  755. .poll_put_char = hvc_poll_put_char,
  756. #endif
  757. };
  758. static const struct tty_port_operations hvc_port_ops = {
  759. .destruct = hvc_port_destruct,
  760. };
  761. struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
  762. const struct hv_ops *ops,
  763. int outbuf_size)
  764. {
  765. struct hvc_struct *hp;
  766. int i;
  767. /* We wait until a driver actually comes along */
  768. if (atomic_inc_not_zero(&hvc_needs_init)) {
  769. int err = hvc_init();
  770. if (err)
  771. return ERR_PTR(err);
  772. }
  773. hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  774. GFP_KERNEL);
  775. if (!hp)
  776. return ERR_PTR(-ENOMEM);
  777. hp->vtermno = vtermno;
  778. hp->data = data;
  779. hp->ops = ops;
  780. hp->outbuf_size = outbuf_size;
  781. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  782. tty_port_init(&hp->port);
  783. hp->port.ops = &hvc_port_ops;
  784. INIT_WORK(&hp->tty_resize, hvc_set_winsz);
  785. spin_lock_init(&hp->lock);
  786. mutex_lock(&hvc_structs_mutex);
  787. /*
  788. * find index to use:
  789. * see if this vterm id matches one registered for console.
  790. */
  791. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  792. if (vtermnos[i] == hp->vtermno &&
  793. cons_ops[i] == hp->ops)
  794. break;
  795. if (i >= MAX_NR_HVC_CONSOLES) {
  796. /* find 'empty' slot for console */
  797. for (i = 0; i < MAX_NR_HVC_CONSOLES && vtermnos[i] != -1; i++) {
  798. }
  799. /* no matching slot, just use a counter */
  800. if (i == MAX_NR_HVC_CONSOLES)
  801. i = ++last_hvc + MAX_NR_HVC_CONSOLES;
  802. }
  803. hp->index = i;
  804. if (i < MAX_NR_HVC_CONSOLES) {
  805. cons_ops[i] = ops;
  806. vtermnos[i] = vtermno;
  807. }
  808. list_add_tail(&(hp->next), &hvc_structs);
  809. mutex_unlock(&hvc_structs_mutex);
  810. /* check if we need to re-register the kernel console */
  811. hvc_check_console(i);
  812. return hp;
  813. }
  814. EXPORT_SYMBOL_GPL(hvc_alloc);
  815. int hvc_remove(struct hvc_struct *hp)
  816. {
  817. unsigned long flags;
  818. struct tty_struct *tty;
  819. tty = tty_port_tty_get(&hp->port);
  820. console_lock();
  821. spin_lock_irqsave(&hp->lock, flags);
  822. if (hp->index < MAX_NR_HVC_CONSOLES) {
  823. vtermnos[hp->index] = -1;
  824. cons_ops[hp->index] = NULL;
  825. }
  826. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  827. spin_unlock_irqrestore(&hp->lock, flags);
  828. console_unlock();
  829. /*
  830. * We 'put' the instance that was grabbed when the kref instance
  831. * was initialized using kref_init(). Let the last holder of this
  832. * kref cause it to be removed, which will probably be the tty_vhangup
  833. * below.
  834. */
  835. tty_port_put(&hp->port);
  836. /*
  837. * This function call will auto chain call hvc_hangup.
  838. */
  839. if (tty) {
  840. tty_vhangup(tty);
  841. tty_kref_put(tty);
  842. }
  843. return 0;
  844. }
  845. EXPORT_SYMBOL_GPL(hvc_remove);
  846. /* Driver initialization: called as soon as someone uses hvc_alloc(). */
  847. static int hvc_init(void)
  848. {
  849. struct tty_driver *drv;
  850. int err;
  851. /* We need more than hvc_count adapters due to hotplug additions. */
  852. drv = tty_alloc_driver(HVC_ALLOC_TTY_ADAPTERS, TTY_DRIVER_REAL_RAW |
  853. TTY_DRIVER_RESET_TERMIOS);
  854. if (IS_ERR(drv)) {
  855. err = PTR_ERR(drv);
  856. goto out;
  857. }
  858. drv->driver_name = "hvc";
  859. drv->name = "hvc";
  860. drv->major = HVC_MAJOR;
  861. drv->minor_start = HVC_MINOR;
  862. drv->type = TTY_DRIVER_TYPE_SYSTEM;
  863. drv->init_termios = tty_std_termios;
  864. tty_set_operations(drv, &hvc_ops);
  865. /* Always start the kthread because there can be hotplug vty adapters
  866. * added later. */
  867. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  868. if (IS_ERR(hvc_task)) {
  869. printk(KERN_ERR "Couldn't create kthread for console.\n");
  870. err = PTR_ERR(hvc_task);
  871. goto put_tty;
  872. }
  873. err = tty_register_driver(drv);
  874. if (err) {
  875. printk(KERN_ERR "Couldn't register hvc console driver\n");
  876. goto stop_thread;
  877. }
  878. /*
  879. * Make sure tty is fully registered before allowing it to be
  880. * found by hvc_console_device.
  881. */
  882. smp_mb();
  883. hvc_driver = drv;
  884. return 0;
  885. stop_thread:
  886. kthread_stop(hvc_task);
  887. hvc_task = NULL;
  888. put_tty:
  889. tty_driver_kref_put(drv);
  890. out:
  891. return err;
  892. }