hvc_console.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  4. * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
  5. * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  6. * Copyright (C) 2004 IBM Corporation
  7. *
  8. * Additional Author(s):
  9. * Ryan S. Arnold <rsa@us.ibm.com>
  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__(sizeof(long))))
  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 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. /* reserve all indices up to and including this index */
  257. if (last_hvc < index)
  258. last_hvc = index;
  259. /* check if we need to re-register the kernel console */
  260. hvc_check_console(index);
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(hvc_instantiate);
  264. /* Wake the sleeping khvcd */
  265. void hvc_kick(void)
  266. {
  267. hvc_kicked = 1;
  268. wake_up_process(hvc_task);
  269. }
  270. EXPORT_SYMBOL_GPL(hvc_kick);
  271. static void hvc_unthrottle(struct tty_struct *tty)
  272. {
  273. hvc_kick();
  274. }
  275. static int hvc_install(struct tty_driver *driver, struct tty_struct *tty)
  276. {
  277. struct hvc_struct *hp;
  278. int rc;
  279. /* Auto increments kref reference if found. */
  280. hp = hvc_get_by_index(tty->index);
  281. if (!hp)
  282. return -ENODEV;
  283. tty->driver_data = hp;
  284. rc = tty_port_install(&hp->port, driver, tty);
  285. if (rc)
  286. tty_port_put(&hp->port);
  287. return rc;
  288. }
  289. /*
  290. * The TTY interface won't be used until after the vio layer has exposed the vty
  291. * adapter to the kernel.
  292. */
  293. static int hvc_open(struct tty_struct *tty, struct file * filp)
  294. {
  295. struct hvc_struct *hp = tty->driver_data;
  296. unsigned long flags;
  297. int rc = 0;
  298. spin_lock_irqsave(&hp->port.lock, flags);
  299. /* Check and then increment for fast path open. */
  300. if (hp->port.count++ > 0) {
  301. spin_unlock_irqrestore(&hp->port.lock, flags);
  302. hvc_kick();
  303. return 0;
  304. } /* else count == 0 */
  305. spin_unlock_irqrestore(&hp->port.lock, flags);
  306. tty_port_tty_set(&hp->port, tty);
  307. if (hp->ops->notifier_add)
  308. rc = hp->ops->notifier_add(hp, hp->data);
  309. /*
  310. * If the notifier fails we return an error. The tty layer
  311. * will call hvc_close() after a failed open but we don't want to clean
  312. * up there so we'll clean up here and clear out the previously set
  313. * tty fields and return the kref reference.
  314. */
  315. if (rc) {
  316. tty_port_tty_set(&hp->port, NULL);
  317. tty->driver_data = NULL;
  318. tty_port_put(&hp->port);
  319. printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
  320. } else
  321. /* We are ready... raise DTR/RTS */
  322. if (C_BAUD(tty))
  323. if (hp->ops->dtr_rts)
  324. hp->ops->dtr_rts(hp, 1);
  325. /* Force wakeup of the polling thread */
  326. hvc_kick();
  327. return rc;
  328. }
  329. static void hvc_close(struct tty_struct *tty, struct file * filp)
  330. {
  331. struct hvc_struct *hp;
  332. unsigned long flags;
  333. if (tty_hung_up_p(filp))
  334. return;
  335. /*
  336. * No driver_data means that this close was issued after a failed
  337. * hvc_open by the tty layer's release_dev() function and we can just
  338. * exit cleanly because the kref reference wasn't made.
  339. */
  340. if (!tty->driver_data)
  341. return;
  342. hp = tty->driver_data;
  343. spin_lock_irqsave(&hp->port.lock, flags);
  344. if (--hp->port.count == 0) {
  345. spin_unlock_irqrestore(&hp->port.lock, flags);
  346. /* We are done with the tty pointer now. */
  347. tty_port_tty_set(&hp->port, NULL);
  348. if (C_HUPCL(tty))
  349. if (hp->ops->dtr_rts)
  350. hp->ops->dtr_rts(hp, 0);
  351. if (hp->ops->notifier_del)
  352. hp->ops->notifier_del(hp, hp->data);
  353. /* cancel pending tty resize work */
  354. cancel_work_sync(&hp->tty_resize);
  355. /*
  356. * Chain calls chars_in_buffer() and returns immediately if
  357. * there is no buffered data otherwise sleeps on a wait queue
  358. * waking periodically to check chars_in_buffer().
  359. */
  360. tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
  361. } else {
  362. if (hp->port.count < 0)
  363. printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
  364. hp->vtermno, hp->port.count);
  365. spin_unlock_irqrestore(&hp->port.lock, flags);
  366. }
  367. }
  368. static void hvc_cleanup(struct tty_struct *tty)
  369. {
  370. struct hvc_struct *hp = tty->driver_data;
  371. tty_port_put(&hp->port);
  372. }
  373. static void hvc_hangup(struct tty_struct *tty)
  374. {
  375. struct hvc_struct *hp = tty->driver_data;
  376. unsigned long flags;
  377. if (!hp)
  378. return;
  379. /* cancel pending tty resize work */
  380. cancel_work_sync(&hp->tty_resize);
  381. spin_lock_irqsave(&hp->port.lock, flags);
  382. /*
  383. * The N_TTY line discipline has problems such that in a close vs
  384. * open->hangup case this can be called after the final close so prevent
  385. * that from happening for now.
  386. */
  387. if (hp->port.count <= 0) {
  388. spin_unlock_irqrestore(&hp->port.lock, flags);
  389. return;
  390. }
  391. hp->port.count = 0;
  392. spin_unlock_irqrestore(&hp->port.lock, flags);
  393. tty_port_tty_set(&hp->port, NULL);
  394. hp->n_outbuf = 0;
  395. if (hp->ops->notifier_hangup)
  396. hp->ops->notifier_hangup(hp, hp->data);
  397. }
  398. /*
  399. * Push buffered characters whether they were just recently buffered or waiting
  400. * on a blocked hypervisor. Call this function with hp->lock held.
  401. */
  402. static int hvc_push(struct hvc_struct *hp)
  403. {
  404. int n;
  405. n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
  406. if (n <= 0) {
  407. if (n == 0 || n == -EAGAIN) {
  408. hp->do_wakeup = 1;
  409. return 0;
  410. }
  411. /* throw away output on error; this happens when
  412. there is no session connected to the vterm. */
  413. hp->n_outbuf = 0;
  414. } else
  415. hp->n_outbuf -= n;
  416. if (hp->n_outbuf > 0)
  417. memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
  418. else
  419. hp->do_wakeup = 1;
  420. return n;
  421. }
  422. static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
  423. {
  424. struct hvc_struct *hp = tty->driver_data;
  425. unsigned long flags;
  426. int rsize, written = 0;
  427. /* This write was probably executed during a tty close. */
  428. if (!hp)
  429. return -EPIPE;
  430. /* FIXME what's this (unprotected) check for? */
  431. if (hp->port.count <= 0)
  432. return -EIO;
  433. while (count > 0) {
  434. int ret = 0;
  435. spin_lock_irqsave(&hp->lock, flags);
  436. rsize = hp->outbuf_size - hp->n_outbuf;
  437. if (rsize) {
  438. if (rsize > count)
  439. rsize = count;
  440. memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
  441. count -= rsize;
  442. buf += rsize;
  443. hp->n_outbuf += rsize;
  444. written += rsize;
  445. }
  446. if (hp->n_outbuf > 0)
  447. ret = hvc_push(hp);
  448. spin_unlock_irqrestore(&hp->lock, flags);
  449. if (!ret)
  450. break;
  451. if (count) {
  452. if (hp->n_outbuf > 0)
  453. hvc_flush(hp);
  454. cond_resched();
  455. }
  456. }
  457. /*
  458. * Racy, but harmless, kick thread if there is still pending data.
  459. */
  460. if (hp->n_outbuf)
  461. hvc_kick();
  462. return written;
  463. }
  464. /**
  465. * hvc_set_winsz() - Resize the hvc tty terminal window.
  466. * @work: work structure.
  467. *
  468. * The routine shall not be called within an atomic context because it
  469. * might sleep.
  470. *
  471. * Locking: hp->lock
  472. */
  473. static void hvc_set_winsz(struct work_struct *work)
  474. {
  475. struct hvc_struct *hp;
  476. unsigned long hvc_flags;
  477. struct tty_struct *tty;
  478. struct winsize ws;
  479. hp = container_of(work, struct hvc_struct, tty_resize);
  480. tty = tty_port_tty_get(&hp->port);
  481. if (!tty)
  482. return;
  483. spin_lock_irqsave(&hp->lock, hvc_flags);
  484. ws = hp->ws;
  485. spin_unlock_irqrestore(&hp->lock, hvc_flags);
  486. tty_do_resize(tty, &ws);
  487. tty_kref_put(tty);
  488. }
  489. /*
  490. * This is actually a contract between the driver and the tty layer outlining
  491. * how much write room the driver can guarantee will be sent OR BUFFERED. This
  492. * driver MUST honor the return value.
  493. */
  494. static int hvc_write_room(struct tty_struct *tty)
  495. {
  496. struct hvc_struct *hp = tty->driver_data;
  497. if (!hp)
  498. return 0;
  499. return hp->outbuf_size - hp->n_outbuf;
  500. }
  501. static int hvc_chars_in_buffer(struct tty_struct *tty)
  502. {
  503. struct hvc_struct *hp = tty->driver_data;
  504. if (!hp)
  505. return 0;
  506. return hp->n_outbuf;
  507. }
  508. /*
  509. * timeout will vary between the MIN and MAX values defined here. By default
  510. * and during console activity we will use a default MIN_TIMEOUT of 10. When
  511. * the console is idle, we increase the timeout value on each pass through
  512. * msleep until we reach the max. This may be noticeable as a brief (average
  513. * one second) delay on the console before the console responds to input when
  514. * there has been no input for some time.
  515. */
  516. #define MIN_TIMEOUT (10)
  517. #define MAX_TIMEOUT (2000)
  518. static u32 timeout = MIN_TIMEOUT;
  519. /*
  520. * Maximum number of bytes to get from the console driver if hvc_poll is
  521. * called from driver (and can't sleep). Any more than this and we break
  522. * and start polling with khvcd. This value was derived from from an OpenBMC
  523. * console with the OPAL driver that results in about 0.25ms interrupts off
  524. * latency.
  525. */
  526. #define HVC_ATOMIC_READ_MAX 128
  527. #define HVC_POLL_READ 0x00000001
  528. #define HVC_POLL_WRITE 0x00000002
  529. static int __hvc_poll(struct hvc_struct *hp, bool may_sleep)
  530. {
  531. struct tty_struct *tty;
  532. int i, n, count, poll_mask = 0;
  533. char buf[N_INBUF] __ALIGNED__;
  534. unsigned long flags;
  535. int read_total = 0;
  536. int written_total = 0;
  537. spin_lock_irqsave(&hp->lock, flags);
  538. /* Push pending writes */
  539. if (hp->n_outbuf > 0)
  540. written_total = hvc_push(hp);
  541. /* Reschedule us if still some write pending */
  542. if (hp->n_outbuf > 0) {
  543. poll_mask |= HVC_POLL_WRITE;
  544. /* If hvc_push() was not able to write, sleep a few msecs */
  545. timeout = (written_total) ? 0 : MIN_TIMEOUT;
  546. }
  547. if (may_sleep) {
  548. spin_unlock_irqrestore(&hp->lock, flags);
  549. cond_resched();
  550. spin_lock_irqsave(&hp->lock, flags);
  551. }
  552. /* No tty attached, just skip */
  553. tty = tty_port_tty_get(&hp->port);
  554. if (tty == NULL)
  555. goto bail;
  556. /* Now check if we can get data (are we throttled ?) */
  557. if (tty_throttled(tty))
  558. goto out;
  559. /* If we aren't notifier driven and aren't throttled, we always
  560. * request a reschedule
  561. */
  562. if (!hp->irq_requested)
  563. poll_mask |= HVC_POLL_READ;
  564. read_again:
  565. /* Read data if any */
  566. count = tty_buffer_request_room(&hp->port, N_INBUF);
  567. /* If flip is full, just reschedule a later read */
  568. if (count == 0) {
  569. poll_mask |= HVC_POLL_READ;
  570. goto out;
  571. }
  572. n = hp->ops->get_chars(hp->vtermno, buf, count);
  573. if (n <= 0) {
  574. /* Hangup the tty when disconnected from host */
  575. if (n == -EPIPE) {
  576. spin_unlock_irqrestore(&hp->lock, flags);
  577. tty_hangup(tty);
  578. spin_lock_irqsave(&hp->lock, flags);
  579. } else if ( n == -EAGAIN ) {
  580. /*
  581. * Some back-ends can only ensure a certain min
  582. * num of bytes read, which may be > 'count'.
  583. * Let the tty clear the flip buff to make room.
  584. */
  585. poll_mask |= HVC_POLL_READ;
  586. }
  587. goto out;
  588. }
  589. for (i = 0; i < n; ++i) {
  590. #ifdef CONFIG_MAGIC_SYSRQ
  591. if (hp->index == hvc_console.index) {
  592. /* Handle the SysRq Hack */
  593. /* XXX should support a sequence */
  594. if (buf[i] == '\x0f') { /* ^O */
  595. /* if ^O is pressed again, reset
  596. * sysrq_pressed and flip ^O char */
  597. sysrq_pressed = !sysrq_pressed;
  598. if (sysrq_pressed)
  599. continue;
  600. } else if (sysrq_pressed) {
  601. handle_sysrq(buf[i]);
  602. sysrq_pressed = 0;
  603. continue;
  604. }
  605. }
  606. #endif /* CONFIG_MAGIC_SYSRQ */
  607. tty_insert_flip_char(&hp->port, buf[i], 0);
  608. }
  609. read_total += n;
  610. if (may_sleep) {
  611. /* Keep going until the flip is full */
  612. spin_unlock_irqrestore(&hp->lock, flags);
  613. cond_resched();
  614. spin_lock_irqsave(&hp->lock, flags);
  615. goto read_again;
  616. } else if (read_total < HVC_ATOMIC_READ_MAX) {
  617. /* Break and defer if it's a large read in atomic */
  618. goto read_again;
  619. }
  620. /*
  621. * Latency break, schedule another poll immediately.
  622. */
  623. poll_mask |= HVC_POLL_READ;
  624. out:
  625. /* Wakeup write queue if necessary */
  626. if (hp->do_wakeup) {
  627. hp->do_wakeup = 0;
  628. tty_wakeup(tty);
  629. }
  630. bail:
  631. spin_unlock_irqrestore(&hp->lock, flags);
  632. if (read_total) {
  633. /* Activity is occurring, so reset the polling backoff value to
  634. a minimum for performance. */
  635. timeout = MIN_TIMEOUT;
  636. tty_flip_buffer_push(&hp->port);
  637. }
  638. tty_kref_put(tty);
  639. return poll_mask;
  640. }
  641. int hvc_poll(struct hvc_struct *hp)
  642. {
  643. return __hvc_poll(hp, false);
  644. }
  645. EXPORT_SYMBOL_GPL(hvc_poll);
  646. /**
  647. * __hvc_resize() - Update terminal window size information.
  648. * @hp: HVC console pointer
  649. * @ws: Terminal window size structure
  650. *
  651. * Stores the specified window size information in the hvc structure of @hp.
  652. * The function schedule the tty resize update.
  653. *
  654. * Locking: Locking free; the function MUST be called holding hp->lock
  655. */
  656. void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
  657. {
  658. hp->ws = ws;
  659. schedule_work(&hp->tty_resize);
  660. }
  661. EXPORT_SYMBOL_GPL(__hvc_resize);
  662. /*
  663. * This kthread is either polling or interrupt driven. This is determined by
  664. * calling hvc_poll() who determines whether a console adapter support
  665. * interrupts.
  666. */
  667. static int khvcd(void *unused)
  668. {
  669. int poll_mask;
  670. struct hvc_struct *hp;
  671. set_freezable();
  672. do {
  673. poll_mask = 0;
  674. hvc_kicked = 0;
  675. try_to_freeze();
  676. wmb();
  677. if (!cpus_are_in_xmon()) {
  678. mutex_lock(&hvc_structs_mutex);
  679. list_for_each_entry(hp, &hvc_structs, next) {
  680. poll_mask |= __hvc_poll(hp, true);
  681. cond_resched();
  682. }
  683. mutex_unlock(&hvc_structs_mutex);
  684. } else
  685. poll_mask |= HVC_POLL_READ;
  686. if (hvc_kicked)
  687. continue;
  688. set_current_state(TASK_INTERRUPTIBLE);
  689. if (!hvc_kicked) {
  690. if (poll_mask == 0)
  691. schedule();
  692. else {
  693. unsigned long j_timeout;
  694. if (timeout < MAX_TIMEOUT)
  695. timeout += (timeout >> 6) + 1;
  696. /*
  697. * We don't use msleep_interruptible otherwise
  698. * "kick" will fail to wake us up
  699. */
  700. j_timeout = msecs_to_jiffies(timeout) + 1;
  701. schedule_timeout_interruptible(j_timeout);
  702. }
  703. }
  704. __set_current_state(TASK_RUNNING);
  705. } while (!kthread_should_stop());
  706. return 0;
  707. }
  708. static int hvc_tiocmget(struct tty_struct *tty)
  709. {
  710. struct hvc_struct *hp = tty->driver_data;
  711. if (!hp || !hp->ops->tiocmget)
  712. return -EINVAL;
  713. return hp->ops->tiocmget(hp);
  714. }
  715. static int hvc_tiocmset(struct tty_struct *tty,
  716. unsigned int set, unsigned int clear)
  717. {
  718. struct hvc_struct *hp = tty->driver_data;
  719. if (!hp || !hp->ops->tiocmset)
  720. return -EINVAL;
  721. return hp->ops->tiocmset(hp, set, clear);
  722. }
  723. #ifdef CONFIG_CONSOLE_POLL
  724. static int hvc_poll_init(struct tty_driver *driver, int line, char *options)
  725. {
  726. return 0;
  727. }
  728. static int hvc_poll_get_char(struct tty_driver *driver, int line)
  729. {
  730. struct tty_struct *tty = driver->ttys[0];
  731. struct hvc_struct *hp = tty->driver_data;
  732. int n;
  733. char ch;
  734. n = hp->ops->get_chars(hp->vtermno, &ch, 1);
  735. if (n <= 0)
  736. return NO_POLL_CHAR;
  737. return ch;
  738. }
  739. static void hvc_poll_put_char(struct tty_driver *driver, int line, char ch)
  740. {
  741. struct tty_struct *tty = driver->ttys[0];
  742. struct hvc_struct *hp = tty->driver_data;
  743. int n;
  744. do {
  745. n = hp->ops->put_chars(hp->vtermno, &ch, 1);
  746. } while (n <= 0);
  747. }
  748. #endif
  749. static const struct tty_operations hvc_ops = {
  750. .install = hvc_install,
  751. .open = hvc_open,
  752. .close = hvc_close,
  753. .cleanup = hvc_cleanup,
  754. .write = hvc_write,
  755. .hangup = hvc_hangup,
  756. .unthrottle = hvc_unthrottle,
  757. .write_room = hvc_write_room,
  758. .chars_in_buffer = hvc_chars_in_buffer,
  759. .tiocmget = hvc_tiocmget,
  760. .tiocmset = hvc_tiocmset,
  761. #ifdef CONFIG_CONSOLE_POLL
  762. .poll_init = hvc_poll_init,
  763. .poll_get_char = hvc_poll_get_char,
  764. .poll_put_char = hvc_poll_put_char,
  765. #endif
  766. };
  767. static const struct tty_port_operations hvc_port_ops = {
  768. .destruct = hvc_port_destruct,
  769. };
  770. struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
  771. const struct hv_ops *ops,
  772. int outbuf_size)
  773. {
  774. struct hvc_struct *hp;
  775. int i;
  776. /* We wait until a driver actually comes along */
  777. if (atomic_inc_not_zero(&hvc_needs_init)) {
  778. int err = hvc_init();
  779. if (err)
  780. return ERR_PTR(err);
  781. }
  782. hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
  783. GFP_KERNEL);
  784. if (!hp)
  785. return ERR_PTR(-ENOMEM);
  786. hp->vtermno = vtermno;
  787. hp->data = data;
  788. hp->ops = ops;
  789. hp->outbuf_size = outbuf_size;
  790. hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
  791. tty_port_init(&hp->port);
  792. hp->port.ops = &hvc_port_ops;
  793. INIT_WORK(&hp->tty_resize, hvc_set_winsz);
  794. spin_lock_init(&hp->lock);
  795. mutex_lock(&hvc_structs_mutex);
  796. /*
  797. * find index to use:
  798. * see if this vterm id matches one registered for console.
  799. */
  800. for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
  801. if (vtermnos[i] == hp->vtermno &&
  802. cons_ops[i] == hp->ops)
  803. break;
  804. /* no matching slot, just use a counter */
  805. if (i >= MAX_NR_HVC_CONSOLES)
  806. i = ++last_hvc;
  807. hp->index = i;
  808. cons_ops[i] = ops;
  809. vtermnos[i] = vtermno;
  810. list_add_tail(&(hp->next), &hvc_structs);
  811. mutex_unlock(&hvc_structs_mutex);
  812. /* check if we need to re-register the kernel console */
  813. hvc_check_console(i);
  814. return hp;
  815. }
  816. EXPORT_SYMBOL_GPL(hvc_alloc);
  817. int hvc_remove(struct hvc_struct *hp)
  818. {
  819. unsigned long flags;
  820. struct tty_struct *tty;
  821. tty = tty_port_tty_get(&hp->port);
  822. console_lock();
  823. spin_lock_irqsave(&hp->lock, flags);
  824. if (hp->index < MAX_NR_HVC_CONSOLES) {
  825. vtermnos[hp->index] = -1;
  826. cons_ops[hp->index] = NULL;
  827. }
  828. /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
  829. spin_unlock_irqrestore(&hp->lock, flags);
  830. console_unlock();
  831. /*
  832. * We 'put' the instance that was grabbed when the kref instance
  833. * was initialized using kref_init(). Let the last holder of this
  834. * kref cause it to be removed, which will probably be the tty_vhangup
  835. * below.
  836. */
  837. tty_port_put(&hp->port);
  838. /*
  839. * This function call will auto chain call hvc_hangup.
  840. */
  841. if (tty) {
  842. tty_vhangup(tty);
  843. tty_kref_put(tty);
  844. }
  845. return 0;
  846. }
  847. EXPORT_SYMBOL_GPL(hvc_remove);
  848. /* Driver initialization: called as soon as someone uses hvc_alloc(). */
  849. static int hvc_init(void)
  850. {
  851. struct tty_driver *drv;
  852. int err;
  853. /* We need more than hvc_count adapters due to hotplug additions. */
  854. drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
  855. if (!drv) {
  856. err = -ENOMEM;
  857. goto out;
  858. }
  859. drv->driver_name = "hvc";
  860. drv->name = "hvc";
  861. drv->major = HVC_MAJOR;
  862. drv->minor_start = HVC_MINOR;
  863. drv->type = TTY_DRIVER_TYPE_SYSTEM;
  864. drv->init_termios = tty_std_termios;
  865. drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  866. tty_set_operations(drv, &hvc_ops);
  867. /* Always start the kthread because there can be hotplug vty adapters
  868. * added later. */
  869. hvc_task = kthread_run(khvcd, NULL, "khvcd");
  870. if (IS_ERR(hvc_task)) {
  871. printk(KERN_ERR "Couldn't create kthread for console.\n");
  872. err = PTR_ERR(hvc_task);
  873. goto put_tty;
  874. }
  875. err = tty_register_driver(drv);
  876. if (err) {
  877. printk(KERN_ERR "Couldn't register hvc console driver\n");
  878. goto stop_thread;
  879. }
  880. /*
  881. * Make sure tty is fully registered before allowing it to be
  882. * found by hvc_console_device.
  883. */
  884. smp_mb();
  885. hvc_driver = drv;
  886. return 0;
  887. stop_thread:
  888. kthread_stop(hvc_task);
  889. hvc_task = NULL;
  890. put_tty:
  891. put_tty_driver(drv);
  892. out:
  893. return err;
  894. }