viocons.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /* -*- linux-c -*-
  2. *
  3. * drivers/char/viocons.c
  4. *
  5. * iSeries Virtual Terminal
  6. *
  7. * Authors: Dave Boutcher <boutcher@us.ibm.com>
  8. * Ryan Arnold <ryanarn@us.ibm.com>
  9. * Colin Devilbiss <devilbis@us.ibm.com>
  10. * Stephen Rothwell <sfr@au1.ibm.com>
  11. *
  12. * (C) Copyright 2000, 2001, 2002, 2003, 2004 IBM Corporation
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of the
  17. * License, or (at your option) anyu later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/errno.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/mm.h>
  33. #include <linux/console.h>
  34. #include <linux/module.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/init.h>
  37. #include <linux/wait.h>
  38. #include <linux/spinlock.h>
  39. #include <asm/ioctls.h>
  40. #include <linux/kd.h>
  41. #include <linux/tty.h>
  42. #include <linux/tty_flip.h>
  43. #include <linux/sysrq.h>
  44. #include <asm/firmware.h>
  45. #include <asm/iseries/vio.h>
  46. #include <asm/iseries/hv_lp_event.h>
  47. #include <asm/iseries/hv_call_event.h>
  48. #include <asm/iseries/hv_lp_config.h>
  49. #include <asm/iseries/hv_call.h>
  50. #ifdef CONFIG_VT
  51. #error You must turn off CONFIG_VT to use CONFIG_VIOCONS
  52. #endif
  53. #define VIOTTY_MAGIC (0x0DCB)
  54. #define VTTY_PORTS 10
  55. #define VIOCONS_KERN_WARN KERN_WARNING "viocons: "
  56. #define VIOCONS_KERN_INFO KERN_INFO "viocons: "
  57. static DEFINE_SPINLOCK(consolelock);
  58. static DEFINE_SPINLOCK(consoleloglock);
  59. static int vio_sysrq_pressed;
  60. #define VIOCHAR_NUM_BUF 16
  61. /*
  62. * Our port information. We store a pointer to one entry in the
  63. * tty_driver_data
  64. */
  65. static struct port_info {
  66. int magic;
  67. struct tty_struct *tty;
  68. HvLpIndex lp;
  69. u8 vcons;
  70. u64 seq; /* sequence number of last HV send */
  71. u64 ack; /* last ack from HV */
  72. /*
  73. * When we get writes faster than we can send it to the partition,
  74. * buffer the data here. Note that used is a bit map of used buffers.
  75. * It had better have enough bits to hold VIOCHAR_NUM_BUF the bitops assume
  76. * it is a multiple of unsigned long
  77. */
  78. unsigned long used;
  79. u8 *buffer[VIOCHAR_NUM_BUF];
  80. int bufferBytes[VIOCHAR_NUM_BUF];
  81. int curbuf;
  82. int bufferOverflow;
  83. int overflowMessage;
  84. } port_info[VTTY_PORTS];
  85. #define viochar_is_console(pi) ((pi) == &port_info[0])
  86. #define viochar_port(pi) ((pi) - &port_info[0])
  87. static void initDataEvent(struct viocharlpevent *viochar, HvLpIndex lp);
  88. static struct tty_driver *viotty_driver;
  89. static void hvlog(char *fmt, ...)
  90. {
  91. int i;
  92. unsigned long flags;
  93. va_list args;
  94. static char buf[256];
  95. spin_lock_irqsave(&consoleloglock, flags);
  96. va_start(args, fmt);
  97. i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
  98. va_end(args);
  99. buf[i++] = '\r';
  100. HvCall_writeLogBuffer(buf, i);
  101. spin_unlock_irqrestore(&consoleloglock, flags);
  102. }
  103. static void hvlogOutput(const char *buf, int count)
  104. {
  105. unsigned long flags;
  106. int begin;
  107. int index;
  108. static const char cr = '\r';
  109. begin = 0;
  110. spin_lock_irqsave(&consoleloglock, flags);
  111. for (index = 0; index < count; index++) {
  112. if (buf[index] == '\n') {
  113. /*
  114. * Start right after the last '\n' or at the zeroth
  115. * array position and output the number of characters
  116. * including the newline.
  117. */
  118. HvCall_writeLogBuffer(&buf[begin], index - begin + 1);
  119. begin = index + 1;
  120. HvCall_writeLogBuffer(&cr, 1);
  121. }
  122. }
  123. if ((index - begin) > 0)
  124. HvCall_writeLogBuffer(&buf[begin], index - begin);
  125. spin_unlock_irqrestore(&consoleloglock, flags);
  126. }
  127. /*
  128. * Make sure we're pointing to a valid port_info structure. Shamelessly
  129. * plagerized from serial.c
  130. */
  131. static inline int viotty_paranoia_check(struct port_info *pi,
  132. char *name, const char *routine)
  133. {
  134. static const char *bad_pi_addr = VIOCONS_KERN_WARN
  135. "warning: bad address for port_info struct (%s) in %s\n";
  136. static const char *badmagic = VIOCONS_KERN_WARN
  137. "warning: bad magic number for port_info struct (%s) in %s\n";
  138. if ((pi < &port_info[0]) || (viochar_port(pi) > VTTY_PORTS)) {
  139. printk(bad_pi_addr, name, routine);
  140. return 1;
  141. }
  142. if (pi->magic != VIOTTY_MAGIC) {
  143. printk(badmagic, name, routine);
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. /*
  149. * Add data to our pending-send buffers.
  150. *
  151. * NOTE: Don't use printk in here because it gets nastily recursive.
  152. * hvlog can be used to log to the hypervisor buffer
  153. */
  154. static int buffer_add(struct port_info *pi, const char *buf, size_t len)
  155. {
  156. size_t bleft;
  157. size_t curlen;
  158. const char *curbuf;
  159. int nextbuf;
  160. curbuf = buf;
  161. bleft = len;
  162. while (bleft > 0) {
  163. /*
  164. * If there is no space left in the current buffer, we have
  165. * filled everything up, so return. If we filled the previous
  166. * buffer we would already have moved to the next one.
  167. */
  168. if (pi->bufferBytes[pi->curbuf] == VIOCHAR_MAX_DATA) {
  169. hvlog ("\n\rviocons: No overflow buffer available for memcpy().\n");
  170. pi->bufferOverflow++;
  171. pi->overflowMessage = 1;
  172. break;
  173. }
  174. /*
  175. * Turn on the "used" bit for this buffer. If it's already on,
  176. * that's fine.
  177. */
  178. set_bit(pi->curbuf, &pi->used);
  179. /*
  180. * See if this buffer has been allocated. If not, allocate it.
  181. */
  182. if (pi->buffer[pi->curbuf] == NULL) {
  183. pi->buffer[pi->curbuf] =
  184. kmalloc(VIOCHAR_MAX_DATA, GFP_ATOMIC);
  185. if (pi->buffer[pi->curbuf] == NULL) {
  186. hvlog("\n\rviocons: kmalloc failed allocating spaces for buffer %d.",
  187. pi->curbuf);
  188. break;
  189. }
  190. }
  191. /* Figure out how much we can copy into this buffer. */
  192. if (bleft < (VIOCHAR_MAX_DATA - pi->bufferBytes[pi->curbuf]))
  193. curlen = bleft;
  194. else
  195. curlen = VIOCHAR_MAX_DATA - pi->bufferBytes[pi->curbuf];
  196. /* Copy the data into the buffer. */
  197. memcpy(pi->buffer[pi->curbuf] + pi->bufferBytes[pi->curbuf],
  198. curbuf, curlen);
  199. pi->bufferBytes[pi->curbuf] += curlen;
  200. curbuf += curlen;
  201. bleft -= curlen;
  202. /*
  203. * Now see if we've filled this buffer. If not then
  204. * we'll try to use it again later. If we've filled it
  205. * up then we'll advance the curbuf to the next in the
  206. * circular queue.
  207. */
  208. if (pi->bufferBytes[pi->curbuf] == VIOCHAR_MAX_DATA) {
  209. nextbuf = (pi->curbuf + 1) % VIOCHAR_NUM_BUF;
  210. /*
  211. * Move to the next buffer if it hasn't been used yet
  212. */
  213. if (test_bit(nextbuf, &pi->used) == 0)
  214. pi->curbuf = nextbuf;
  215. }
  216. }
  217. return len - bleft;
  218. }
  219. /*
  220. * Send pending data
  221. *
  222. * NOTE: Don't use printk in here because it gets nastily recursive.
  223. * hvlog can be used to log to the hypervisor buffer
  224. */
  225. static void send_buffers(struct port_info *pi)
  226. {
  227. HvLpEvent_Rc hvrc;
  228. int nextbuf;
  229. struct viocharlpevent *viochar;
  230. unsigned long flags;
  231. spin_lock_irqsave(&consolelock, flags);
  232. viochar = (struct viocharlpevent *)
  233. vio_get_event_buffer(viomajorsubtype_chario);
  234. /* Make sure we got a buffer */
  235. if (viochar == NULL) {
  236. hvlog("\n\rviocons: Can't get viochar buffer in sendBuffers().");
  237. spin_unlock_irqrestore(&consolelock, flags);
  238. return;
  239. }
  240. if (pi->used == 0) {
  241. hvlog("\n\rviocons: in sendbuffers(), but no buffers used.\n");
  242. vio_free_event_buffer(viomajorsubtype_chario, viochar);
  243. spin_unlock_irqrestore(&consolelock, flags);
  244. return;
  245. }
  246. /*
  247. * curbuf points to the buffer we're filling. We want to
  248. * start sending AFTER this one.
  249. */
  250. nextbuf = (pi->curbuf + 1) % VIOCHAR_NUM_BUF;
  251. /*
  252. * Loop until we find a buffer with the used bit on
  253. */
  254. while (test_bit(nextbuf, &pi->used) == 0)
  255. nextbuf = (nextbuf + 1) % VIOCHAR_NUM_BUF;
  256. initDataEvent(viochar, pi->lp);
  257. /*
  258. * While we have buffers with data, and our send window
  259. * is open, send them
  260. */
  261. while ((test_bit(nextbuf, &pi->used)) &&
  262. ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
  263. viochar->len = pi->bufferBytes[nextbuf];
  264. viochar->event.xCorrelationToken = pi->seq++;
  265. viochar->event.xSizeMinus1 =
  266. offsetof(struct viocharlpevent, data) + viochar->len;
  267. memcpy(viochar->data, pi->buffer[nextbuf], viochar->len);
  268. hvrc = HvCallEvent_signalLpEvent(&viochar->event);
  269. if (hvrc) {
  270. /*
  271. * MUST unlock the spinlock before doing a printk
  272. */
  273. vio_free_event_buffer(viomajorsubtype_chario, viochar);
  274. spin_unlock_irqrestore(&consolelock, flags);
  275. printk(VIOCONS_KERN_WARN
  276. "error sending event! return code %d\n",
  277. (int)hvrc);
  278. return;
  279. }
  280. /*
  281. * clear the used bit, zero the number of bytes in
  282. * this buffer, and move to the next buffer
  283. */
  284. clear_bit(nextbuf, &pi->used);
  285. pi->bufferBytes[nextbuf] = 0;
  286. nextbuf = (nextbuf + 1) % VIOCHAR_NUM_BUF;
  287. }
  288. /*
  289. * If we have emptied all the buffers, start at 0 again.
  290. * this will re-use any allocated buffers
  291. */
  292. if (pi->used == 0) {
  293. pi->curbuf = 0;
  294. if (pi->overflowMessage)
  295. pi->overflowMessage = 0;
  296. if (pi->tty) {
  297. tty_wakeup(pi->tty);
  298. }
  299. }
  300. vio_free_event_buffer(viomajorsubtype_chario, viochar);
  301. spin_unlock_irqrestore(&consolelock, flags);
  302. }
  303. /*
  304. * Our internal writer. Gets called both from the console device and
  305. * the tty device. the tty pointer will be NULL if called from the console.
  306. * Return total number of bytes "written".
  307. *
  308. * NOTE: Don't use printk in here because it gets nastily recursive. hvlog
  309. * can be used to log to the hypervisor buffer
  310. */
  311. static int internal_write(struct port_info *pi, const char *buf, size_t len)
  312. {
  313. HvLpEvent_Rc hvrc;
  314. size_t bleft;
  315. size_t curlen;
  316. const char *curbuf;
  317. unsigned long flags;
  318. struct viocharlpevent *viochar;
  319. /*
  320. * Write to the hvlog of inbound data are now done prior to
  321. * calling internal_write() since internal_write() is only called in
  322. * the event that an lp event path is active, which isn't the case for
  323. * logging attempts prior to console initialization.
  324. *
  325. * If there is already data queued for this port, send it prior to
  326. * attempting to send any new data.
  327. */
  328. if (pi->used)
  329. send_buffers(pi);
  330. spin_lock_irqsave(&consolelock, flags);
  331. viochar = vio_get_event_buffer(viomajorsubtype_chario);
  332. if (viochar == NULL) {
  333. spin_unlock_irqrestore(&consolelock, flags);
  334. hvlog("\n\rviocons: Can't get vio buffer in internal_write().");
  335. return -EAGAIN;
  336. }
  337. initDataEvent(viochar, pi->lp);
  338. curbuf = buf;
  339. bleft = len;
  340. while ((bleft > 0) && (pi->used == 0) &&
  341. ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
  342. if (bleft > VIOCHAR_MAX_DATA)
  343. curlen = VIOCHAR_MAX_DATA;
  344. else
  345. curlen = bleft;
  346. viochar->event.xCorrelationToken = pi->seq++;
  347. memcpy(viochar->data, curbuf, curlen);
  348. viochar->len = curlen;
  349. viochar->event.xSizeMinus1 =
  350. offsetof(struct viocharlpevent, data) + curlen;
  351. hvrc = HvCallEvent_signalLpEvent(&viochar->event);
  352. if (hvrc) {
  353. hvlog("viocons: error sending event! %d\n", (int)hvrc);
  354. goto out;
  355. }
  356. curbuf += curlen;
  357. bleft -= curlen;
  358. }
  359. /* If we didn't send it all, buffer as much of it as we can. */
  360. if (bleft > 0)
  361. bleft -= buffer_add(pi, curbuf, bleft);
  362. out:
  363. vio_free_event_buffer(viomajorsubtype_chario, viochar);
  364. spin_unlock_irqrestore(&consolelock, flags);
  365. return len - bleft;
  366. }
  367. static struct port_info *get_port_data(struct tty_struct *tty)
  368. {
  369. unsigned long flags;
  370. struct port_info *pi;
  371. spin_lock_irqsave(&consolelock, flags);
  372. if (tty) {
  373. pi = (struct port_info *)tty->driver_data;
  374. if (!pi || viotty_paranoia_check(pi, tty->name,
  375. "get_port_data")) {
  376. pi = NULL;
  377. }
  378. } else
  379. /*
  380. * If this is the console device, use the lp from
  381. * the first port entry
  382. */
  383. pi = &port_info[0];
  384. spin_unlock_irqrestore(&consolelock, flags);
  385. return pi;
  386. }
  387. /*
  388. * Initialize the common fields in a charLpEvent
  389. */
  390. static void initDataEvent(struct viocharlpevent *viochar, HvLpIndex lp)
  391. {
  392. struct HvLpEvent *hev = &viochar->event;
  393. memset(viochar, 0, sizeof(struct viocharlpevent));
  394. hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
  395. HV_LP_EVENT_INT;
  396. hev->xType = HvLpEvent_Type_VirtualIo;
  397. hev->xSubtype = viomajorsubtype_chario | viochardata;
  398. hev->xSourceLp = HvLpConfig_getLpIndex();
  399. hev->xTargetLp = lp;
  400. hev->xSizeMinus1 = sizeof(struct viocharlpevent);
  401. hev->xSourceInstanceId = viopath_sourceinst(lp);
  402. hev->xTargetInstanceId = viopath_targetinst(lp);
  403. }
  404. /*
  405. * early console device write
  406. */
  407. static void viocons_write_early(struct console *co, const char *s, unsigned count)
  408. {
  409. hvlogOutput(s, count);
  410. }
  411. /*
  412. * console device write
  413. */
  414. static void viocons_write(struct console *co, const char *s, unsigned count)
  415. {
  416. int index;
  417. int begin;
  418. struct port_info *pi;
  419. static const char cr = '\r';
  420. /*
  421. * Check port data first because the target LP might be valid but
  422. * simply not active, in which case we want to hvlog the output.
  423. */
  424. pi = get_port_data(NULL);
  425. if (pi == NULL) {
  426. hvlog("\n\rviocons_write: unable to get port data.");
  427. return;
  428. }
  429. hvlogOutput(s, count);
  430. if (!viopath_isactive(pi->lp))
  431. return;
  432. /*
  433. * Any newline character found will cause a
  434. * carriage return character to be emitted as well.
  435. */
  436. begin = 0;
  437. for (index = 0; index < count; index++) {
  438. if (s[index] == '\n') {
  439. /*
  440. * Newline found. Print everything up to and
  441. * including the newline
  442. */
  443. internal_write(pi, &s[begin], index - begin + 1);
  444. begin = index + 1;
  445. /* Emit a carriage return as well */
  446. internal_write(pi, &cr, 1);
  447. }
  448. }
  449. /* If any characters left to write, write them now */
  450. if ((index - begin) > 0)
  451. internal_write(pi, &s[begin], index - begin);
  452. }
  453. /*
  454. * Work out the device associate with this console
  455. */
  456. static struct tty_driver *viocons_device(struct console *c, int *index)
  457. {
  458. *index = c->index;
  459. return viotty_driver;
  460. }
  461. /*
  462. * console device I/O methods
  463. */
  464. static struct console viocons_early = {
  465. .name = "viocons",
  466. .write = viocons_write_early,
  467. .flags = CON_PRINTBUFFER,
  468. .index = -1,
  469. };
  470. static struct console viocons = {
  471. .name = "viocons",
  472. .write = viocons_write,
  473. .device = viocons_device,
  474. .flags = CON_PRINTBUFFER,
  475. .index = -1,
  476. };
  477. /*
  478. * TTY Open method
  479. */
  480. static int viotty_open(struct tty_struct *tty, struct file *filp)
  481. {
  482. int port;
  483. unsigned long flags;
  484. struct port_info *pi;
  485. port = tty->index;
  486. if ((port < 0) || (port >= VTTY_PORTS))
  487. return -ENODEV;
  488. spin_lock_irqsave(&consolelock, flags);
  489. pi = &port_info[port];
  490. /* If some other TTY is already connected here, reject the open */
  491. if ((pi->tty) && (pi->tty != tty)) {
  492. spin_unlock_irqrestore(&consolelock, flags);
  493. printk(VIOCONS_KERN_WARN
  494. "attempt to open device twice from different ttys\n");
  495. return -EBUSY;
  496. }
  497. tty->driver_data = pi;
  498. pi->tty = tty;
  499. spin_unlock_irqrestore(&consolelock, flags);
  500. return 0;
  501. }
  502. /*
  503. * TTY Close method
  504. */
  505. static void viotty_close(struct tty_struct *tty, struct file *filp)
  506. {
  507. unsigned long flags;
  508. struct port_info *pi;
  509. spin_lock_irqsave(&consolelock, flags);
  510. pi = (struct port_info *)tty->driver_data;
  511. if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_close")) {
  512. spin_unlock_irqrestore(&consolelock, flags);
  513. return;
  514. }
  515. if (tty->count == 1)
  516. pi->tty = NULL;
  517. spin_unlock_irqrestore(&consolelock, flags);
  518. }
  519. /*
  520. * TTY Write method
  521. */
  522. static int viotty_write(struct tty_struct *tty, const unsigned char *buf,
  523. int count)
  524. {
  525. struct port_info *pi;
  526. pi = get_port_data(tty);
  527. if (pi == NULL) {
  528. hvlog("\n\rviotty_write: no port data.");
  529. return -ENODEV;
  530. }
  531. if (viochar_is_console(pi))
  532. hvlogOutput(buf, count);
  533. /*
  534. * If the path to this LP is closed, don't bother doing anything more.
  535. * just dump the data on the floor and return count. For some reason
  536. * some user level programs will attempt to probe available tty's and
  537. * they'll attempt a viotty_write on an invalid port which maps to an
  538. * invalid target lp. If this is the case then ignore the
  539. * viotty_write call and, since the viopath isn't active to this
  540. * partition, return count.
  541. */
  542. if (!viopath_isactive(pi->lp))
  543. return count;
  544. return internal_write(pi, buf, count);
  545. }
  546. /*
  547. * TTY put_char method
  548. */
  549. static void viotty_put_char(struct tty_struct *tty, unsigned char ch)
  550. {
  551. struct port_info *pi;
  552. pi = get_port_data(tty);
  553. if (pi == NULL)
  554. return;
  555. /* This will append '\r' as well if the char is '\n' */
  556. if (viochar_is_console(pi))
  557. hvlogOutput(&ch, 1);
  558. if (viopath_isactive(pi->lp))
  559. internal_write(pi, &ch, 1);
  560. }
  561. /*
  562. * TTY write_room method
  563. */
  564. static int viotty_write_room(struct tty_struct *tty)
  565. {
  566. int i;
  567. int room = 0;
  568. struct port_info *pi;
  569. unsigned long flags;
  570. spin_lock_irqsave(&consolelock, flags);
  571. pi = (struct port_info *)tty->driver_data;
  572. if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_write_room")) {
  573. spin_unlock_irqrestore(&consolelock, flags);
  574. return 0;
  575. }
  576. /* If no buffers are used, return the max size. */
  577. if (pi->used == 0) {
  578. spin_unlock_irqrestore(&consolelock, flags);
  579. return VIOCHAR_MAX_DATA * VIOCHAR_NUM_BUF;
  580. }
  581. /*
  582. * We retain the spinlock because we want to get an accurate
  583. * count and it can change on us between each operation if we
  584. * don't hold the spinlock.
  585. */
  586. for (i = 0; ((i < VIOCHAR_NUM_BUF) && (room < VIOCHAR_MAX_DATA)); i++)
  587. room += (VIOCHAR_MAX_DATA - pi->bufferBytes[i]);
  588. spin_unlock_irqrestore(&consolelock, flags);
  589. if (room > VIOCHAR_MAX_DATA)
  590. room = VIOCHAR_MAX_DATA;
  591. return room;
  592. }
  593. /*
  594. * TTY chars_in_buffer method
  595. */
  596. static int viotty_chars_in_buffer(struct tty_struct *tty)
  597. {
  598. return 0;
  599. }
  600. static int viotty_ioctl(struct tty_struct *tty, struct file *file,
  601. unsigned int cmd, unsigned long arg)
  602. {
  603. switch (cmd) {
  604. /*
  605. * the ioctls below read/set the flags usually shown in the leds
  606. * don't use them - they will go away without warning
  607. */
  608. case KDGETLED:
  609. case KDGKBLED:
  610. return put_user(0, (char *)arg);
  611. case KDSKBLED:
  612. return 0;
  613. }
  614. return n_tty_ioctl(tty, file, cmd, arg);
  615. }
  616. /*
  617. * Handle an open charLpEvent. Could be either interrupt or ack
  618. */
  619. static void vioHandleOpenEvent(struct HvLpEvent *event)
  620. {
  621. unsigned long flags;
  622. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  623. u8 port = cevent->virtual_device;
  624. struct port_info *pi;
  625. int reject = 0;
  626. if (hvlpevent_is_ack(event)) {
  627. if (port >= VTTY_PORTS)
  628. return;
  629. spin_lock_irqsave(&consolelock, flags);
  630. /* Got the lock, don't cause console output */
  631. pi = &port_info[port];
  632. if (event->xRc == HvLpEvent_Rc_Good) {
  633. pi->seq = pi->ack = 0;
  634. /*
  635. * This line allows connections from the primary
  636. * partition but once one is connected from the
  637. * primary partition nothing short of a reboot
  638. * of linux will allow access from the hosting
  639. * partition again without a required iSeries fix.
  640. */
  641. pi->lp = event->xTargetLp;
  642. }
  643. spin_unlock_irqrestore(&consolelock, flags);
  644. if (event->xRc != HvLpEvent_Rc_Good)
  645. printk(VIOCONS_KERN_WARN
  646. "handle_open_event: event->xRc == (%d).\n",
  647. event->xRc);
  648. if (event->xCorrelationToken != 0) {
  649. atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
  650. atomic_set(aptr, 1);
  651. } else
  652. printk(VIOCONS_KERN_WARN
  653. "weird...got open ack without atomic\n");
  654. return;
  655. }
  656. /* This had better require an ack, otherwise complain */
  657. if (!hvlpevent_need_ack(event)) {
  658. printk(VIOCONS_KERN_WARN "viocharopen without ack bit!\n");
  659. return;
  660. }
  661. spin_lock_irqsave(&consolelock, flags);
  662. /* Got the lock, don't cause console output */
  663. /* Make sure this is a good virtual tty */
  664. if (port >= VTTY_PORTS) {
  665. event->xRc = HvLpEvent_Rc_SubtypeError;
  666. cevent->subtype_result_code = viorc_openRejected;
  667. /*
  668. * Flag state here since we can't printk while holding
  669. * a spinlock.
  670. */
  671. reject = 1;
  672. } else {
  673. pi = &port_info[port];
  674. if ((pi->lp != HvLpIndexInvalid) &&
  675. (pi->lp != event->xSourceLp)) {
  676. /*
  677. * If this is tty is already connected to a different
  678. * partition, fail.
  679. */
  680. event->xRc = HvLpEvent_Rc_SubtypeError;
  681. cevent->subtype_result_code = viorc_openRejected;
  682. reject = 2;
  683. } else {
  684. pi->lp = event->xSourceLp;
  685. event->xRc = HvLpEvent_Rc_Good;
  686. cevent->subtype_result_code = viorc_good;
  687. pi->seq = pi->ack = 0;
  688. reject = 0;
  689. }
  690. }
  691. spin_unlock_irqrestore(&consolelock, flags);
  692. if (reject == 1)
  693. printk(VIOCONS_KERN_WARN "open rejected: bad virtual tty.\n");
  694. else if (reject == 2)
  695. printk(VIOCONS_KERN_WARN
  696. "open rejected: console in exclusive use by another partition.\n");
  697. /* Return the acknowledgement */
  698. HvCallEvent_ackLpEvent(event);
  699. }
  700. /*
  701. * Handle a close charLpEvent. This should ONLY be an Interrupt because the
  702. * virtual console should never actually issue a close event to the hypervisor
  703. * because the virtual console never goes away. A close event coming from the
  704. * hypervisor simply means that there are no client consoles connected to the
  705. * virtual console.
  706. *
  707. * Regardless of the number of connections masqueraded on the other side of
  708. * the hypervisor ONLY ONE close event should be called to accompany the ONE
  709. * open event that is called. The close event should ONLY be called when NO
  710. * MORE connections (masqueraded or not) exist on the other side of the
  711. * hypervisor.
  712. */
  713. static void vioHandleCloseEvent(struct HvLpEvent *event)
  714. {
  715. unsigned long flags;
  716. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  717. u8 port = cevent->virtual_device;
  718. if (hvlpevent_is_int(event)) {
  719. if (port >= VTTY_PORTS) {
  720. printk(VIOCONS_KERN_WARN
  721. "close message from invalid virtual device.\n");
  722. return;
  723. }
  724. /* For closes, just mark the console partition invalid */
  725. spin_lock_irqsave(&consolelock, flags);
  726. /* Got the lock, don't cause console output */
  727. if (port_info[port].lp == event->xSourceLp)
  728. port_info[port].lp = HvLpIndexInvalid;
  729. spin_unlock_irqrestore(&consolelock, flags);
  730. printk(VIOCONS_KERN_INFO "close from %d\n", event->xSourceLp);
  731. } else
  732. printk(VIOCONS_KERN_WARN
  733. "got unexpected close acknowlegement\n");
  734. }
  735. /*
  736. * Handle a config charLpEvent. Could be either interrupt or ack
  737. */
  738. static void vioHandleConfig(struct HvLpEvent *event)
  739. {
  740. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  741. HvCall_writeLogBuffer(cevent->data, cevent->len);
  742. if (cevent->data[0] == 0x01)
  743. printk(VIOCONS_KERN_INFO "window resized to %d: %d: %d: %d\n",
  744. cevent->data[1], cevent->data[2],
  745. cevent->data[3], cevent->data[4]);
  746. else
  747. printk(VIOCONS_KERN_WARN "unknown config event\n");
  748. }
  749. /*
  750. * Handle a data charLpEvent.
  751. */
  752. static void vioHandleData(struct HvLpEvent *event)
  753. {
  754. struct tty_struct *tty;
  755. unsigned long flags;
  756. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  757. struct port_info *pi;
  758. int index;
  759. int num_pushed;
  760. u8 port = cevent->virtual_device;
  761. if (port >= VTTY_PORTS) {
  762. printk(VIOCONS_KERN_WARN "data on invalid virtual device %d\n",
  763. port);
  764. return;
  765. }
  766. /*
  767. * Hold the spinlock so that we don't take an interrupt that
  768. * changes tty between the time we fetch the port_info
  769. * pointer and the time we paranoia check.
  770. */
  771. spin_lock_irqsave(&consolelock, flags);
  772. pi = &port_info[port];
  773. /*
  774. * Change 05/01/2003 - Ryan Arnold: If a partition other than
  775. * the current exclusive partition tries to send us data
  776. * events then just drop them on the floor because we don't
  777. * want his stinking data. He isn't authorized to receive
  778. * data because he wasn't the first one to get the console,
  779. * therefore he shouldn't be allowed to send data either.
  780. * This will work without an iSeries fix.
  781. */
  782. if (pi->lp != event->xSourceLp) {
  783. spin_unlock_irqrestore(&consolelock, flags);
  784. return;
  785. }
  786. tty = pi->tty;
  787. if (tty == NULL) {
  788. spin_unlock_irqrestore(&consolelock, flags);
  789. printk(VIOCONS_KERN_WARN "no tty for virtual device %d\n",
  790. port);
  791. return;
  792. }
  793. if (tty->magic != TTY_MAGIC) {
  794. spin_unlock_irqrestore(&consolelock, flags);
  795. printk(VIOCONS_KERN_WARN "tty bad magic\n");
  796. return;
  797. }
  798. /*
  799. * Just to be paranoid, make sure the tty points back to this port
  800. */
  801. pi = (struct port_info *)tty->driver_data;
  802. if (!pi || viotty_paranoia_check(pi, tty->name, "vioHandleData")) {
  803. spin_unlock_irqrestore(&consolelock, flags);
  804. return;
  805. }
  806. spin_unlock_irqrestore(&consolelock, flags);
  807. /*
  808. * Change 07/21/2003 - Ryan Arnold: functionality added to
  809. * support sysrq utilizing ^O as the sysrq key. The sysrq
  810. * functionality will only work if built into the kernel and
  811. * then only if sysrq is enabled through the proc filesystem.
  812. */
  813. num_pushed = 0;
  814. for (index = 0; index < cevent->len; index++) {
  815. /*
  816. * Will be optimized away if !CONFIG_MAGIC_SYSRQ:
  817. */
  818. if (sysrq_on()) {
  819. /* 0x0f is the ascii character for ^O */
  820. if (cevent->data[index] == '\x0f') {
  821. vio_sysrq_pressed = 1;
  822. /*
  823. * continue because we don't want to add
  824. * the sysrq key into the data string.
  825. */
  826. continue;
  827. } else if (vio_sysrq_pressed) {
  828. handle_sysrq(cevent->data[index], tty);
  829. vio_sysrq_pressed = 0;
  830. /*
  831. * continue because we don't want to add
  832. * the sysrq sequence into the data string.
  833. */
  834. continue;
  835. }
  836. }
  837. /*
  838. * The sysrq sequence isn't included in this check if
  839. * sysrq is enabled and compiled into the kernel because
  840. * the sequence will never get inserted into the buffer.
  841. * Don't attempt to copy more data into the buffer than we
  842. * have room for because it would fail without indication.
  843. */
  844. if(tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL) == 0) {
  845. printk(VIOCONS_KERN_WARN "input buffer overflow!\n");
  846. break;
  847. }
  848. num_pushed++;
  849. }
  850. if (num_pushed)
  851. tty_flip_buffer_push(tty);
  852. }
  853. /*
  854. * Handle an ack charLpEvent.
  855. */
  856. static void vioHandleAck(struct HvLpEvent *event)
  857. {
  858. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  859. unsigned long flags;
  860. u8 port = cevent->virtual_device;
  861. if (port >= VTTY_PORTS) {
  862. printk(VIOCONS_KERN_WARN "data on invalid virtual device\n");
  863. return;
  864. }
  865. spin_lock_irqsave(&consolelock, flags);
  866. port_info[port].ack = event->xCorrelationToken;
  867. spin_unlock_irqrestore(&consolelock, flags);
  868. if (port_info[port].used)
  869. send_buffers(&port_info[port]);
  870. }
  871. /*
  872. * Handle charLpEvents and route to the appropriate routine
  873. */
  874. static void vioHandleCharEvent(struct HvLpEvent *event)
  875. {
  876. int charminor;
  877. if (event == NULL)
  878. return;
  879. charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  880. switch (charminor) {
  881. case viocharopen:
  882. vioHandleOpenEvent(event);
  883. break;
  884. case viocharclose:
  885. vioHandleCloseEvent(event);
  886. break;
  887. case viochardata:
  888. vioHandleData(event);
  889. break;
  890. case viocharack:
  891. vioHandleAck(event);
  892. break;
  893. case viocharconfig:
  894. vioHandleConfig(event);
  895. break;
  896. default:
  897. if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
  898. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  899. HvCallEvent_ackLpEvent(event);
  900. }
  901. }
  902. }
  903. /*
  904. * Send an open event
  905. */
  906. static int send_open(HvLpIndex remoteLp, void *sem)
  907. {
  908. return HvCallEvent_signalLpEventFast(remoteLp,
  909. HvLpEvent_Type_VirtualIo,
  910. viomajorsubtype_chario | viocharopen,
  911. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  912. viopath_sourceinst(remoteLp),
  913. viopath_targetinst(remoteLp),
  914. (u64)(unsigned long)sem, VIOVERSION << 16,
  915. 0, 0, 0, 0);
  916. }
  917. static const struct tty_operations serial_ops = {
  918. .open = viotty_open,
  919. .close = viotty_close,
  920. .write = viotty_write,
  921. .put_char = viotty_put_char,
  922. .write_room = viotty_write_room,
  923. .chars_in_buffer = viotty_chars_in_buffer,
  924. .ioctl = viotty_ioctl,
  925. };
  926. static int __init viocons_init2(void)
  927. {
  928. atomic_t wait_flag;
  929. int rc;
  930. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  931. return -ENODEV;
  932. /* +2 for fudge */
  933. rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
  934. viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
  935. if (rc)
  936. printk(VIOCONS_KERN_WARN "error opening to primary %d\n", rc);
  937. if (viopath_hostLp == HvLpIndexInvalid)
  938. vio_set_hostlp();
  939. /*
  940. * And if the primary is not the same as the hosting LP, open to the
  941. * hosting lp
  942. */
  943. if ((viopath_hostLp != HvLpIndexInvalid) &&
  944. (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
  945. printk(VIOCONS_KERN_INFO "open path to hosting (%d)\n",
  946. viopath_hostLp);
  947. rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
  948. VIOCHAR_WINDOW + 2); /* +2 for fudge */
  949. if (rc)
  950. printk(VIOCONS_KERN_WARN
  951. "error opening to partition %d: %d\n",
  952. viopath_hostLp, rc);
  953. }
  954. if (vio_setHandler(viomajorsubtype_chario, vioHandleCharEvent) < 0)
  955. printk(VIOCONS_KERN_WARN
  956. "error seting handler for console events!\n");
  957. /*
  958. * First, try to open the console to the hosting lp.
  959. * Wait on a semaphore for the response.
  960. */
  961. atomic_set(&wait_flag, 0);
  962. if ((viopath_isactive(viopath_hostLp)) &&
  963. (send_open(viopath_hostLp, (void *)&wait_flag) == 0)) {
  964. printk(VIOCONS_KERN_INFO "hosting partition %d\n",
  965. viopath_hostLp);
  966. while (atomic_read(&wait_flag) == 0)
  967. mb();
  968. atomic_set(&wait_flag, 0);
  969. }
  970. /*
  971. * If we don't have an active console, try the primary
  972. */
  973. if ((!viopath_isactive(port_info[0].lp)) &&
  974. (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
  975. (send_open(HvLpConfig_getPrimaryLpIndex(), (void *)&wait_flag)
  976. == 0)) {
  977. printk(VIOCONS_KERN_INFO "opening console to primary partition\n");
  978. while (atomic_read(&wait_flag) == 0)
  979. mb();
  980. }
  981. /* Initialize the tty_driver structure */
  982. viotty_driver = alloc_tty_driver(VTTY_PORTS);
  983. viotty_driver->owner = THIS_MODULE;
  984. viotty_driver->driver_name = "vioconsole";
  985. viotty_driver->name = "tty";
  986. viotty_driver->name_base = 1;
  987. viotty_driver->major = TTY_MAJOR;
  988. viotty_driver->minor_start = 1;
  989. viotty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  990. viotty_driver->subtype = 1;
  991. viotty_driver->init_termios = tty_std_termios;
  992. viotty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  993. tty_set_operations(viotty_driver, &serial_ops);
  994. if (tty_register_driver(viotty_driver)) {
  995. printk(VIOCONS_KERN_WARN "couldn't register console driver\n");
  996. put_tty_driver(viotty_driver);
  997. viotty_driver = NULL;
  998. }
  999. unregister_console(&viocons_early);
  1000. register_console(&viocons);
  1001. return 0;
  1002. }
  1003. static int __init viocons_init(void)
  1004. {
  1005. int i;
  1006. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  1007. return -ENODEV;
  1008. printk(VIOCONS_KERN_INFO "registering console\n");
  1009. for (i = 0; i < VTTY_PORTS; i++) {
  1010. port_info[i].lp = HvLpIndexInvalid;
  1011. port_info[i].magic = VIOTTY_MAGIC;
  1012. }
  1013. HvCall_setLogBufferFormatAndCodepage(HvCall_LogBuffer_ASCII, 437);
  1014. add_preferred_console("viocons", 0, NULL);
  1015. register_console(&viocons_early);
  1016. return 0;
  1017. }
  1018. console_initcall(viocons_init);
  1019. module_init(viocons_init2);