viocons.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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
  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 int 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 0;
  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. return 1;
  561. }
  562. /*
  563. * TTY write_room method
  564. */
  565. static int viotty_write_room(struct tty_struct *tty)
  566. {
  567. int i;
  568. int room = 0;
  569. struct port_info *pi;
  570. unsigned long flags;
  571. spin_lock_irqsave(&consolelock, flags);
  572. pi = (struct port_info *)tty->driver_data;
  573. if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_write_room")) {
  574. spin_unlock_irqrestore(&consolelock, flags);
  575. return 0;
  576. }
  577. /* If no buffers are used, return the max size. */
  578. if (pi->used == 0) {
  579. spin_unlock_irqrestore(&consolelock, flags);
  580. return VIOCHAR_MAX_DATA * VIOCHAR_NUM_BUF;
  581. }
  582. /*
  583. * We retain the spinlock because we want to get an accurate
  584. * count and it can change on us between each operation if we
  585. * don't hold the spinlock.
  586. */
  587. for (i = 0; ((i < VIOCHAR_NUM_BUF) && (room < VIOCHAR_MAX_DATA)); i++)
  588. room += (VIOCHAR_MAX_DATA - pi->bufferBytes[i]);
  589. spin_unlock_irqrestore(&consolelock, flags);
  590. if (room > VIOCHAR_MAX_DATA)
  591. room = VIOCHAR_MAX_DATA;
  592. return room;
  593. }
  594. /*
  595. * TTY chars_in_buffer method
  596. */
  597. static int viotty_chars_in_buffer(struct tty_struct *tty)
  598. {
  599. return 0;
  600. }
  601. static int viotty_ioctl(struct tty_struct *tty, struct file *file,
  602. unsigned int cmd, unsigned long arg)
  603. {
  604. switch (cmd) {
  605. /*
  606. * the ioctls below read/set the flags usually shown in the leds
  607. * don't use them - they will go away without warning
  608. */
  609. case KDGETLED:
  610. case KDGKBLED:
  611. return put_user(0, (char *)arg);
  612. case KDSKBLED:
  613. return 0;
  614. }
  615. /* FIXME: WTF is this being called for ??? */
  616. lock_kernel();
  617. ret = n_tty_ioctl(tty, file, cmd, arg);
  618. unlock_kernel();
  619. return ret;
  620. }
  621. /*
  622. * Handle an open charLpEvent. Could be either interrupt or ack
  623. */
  624. static void vioHandleOpenEvent(struct HvLpEvent *event)
  625. {
  626. unsigned long flags;
  627. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  628. u8 port = cevent->virtual_device;
  629. struct port_info *pi;
  630. int reject = 0;
  631. if (hvlpevent_is_ack(event)) {
  632. if (port >= VTTY_PORTS)
  633. return;
  634. spin_lock_irqsave(&consolelock, flags);
  635. /* Got the lock, don't cause console output */
  636. pi = &port_info[port];
  637. if (event->xRc == HvLpEvent_Rc_Good) {
  638. pi->seq = pi->ack = 0;
  639. /*
  640. * This line allows connections from the primary
  641. * partition but once one is connected from the
  642. * primary partition nothing short of a reboot
  643. * of linux will allow access from the hosting
  644. * partition again without a required iSeries fix.
  645. */
  646. pi->lp = event->xTargetLp;
  647. }
  648. spin_unlock_irqrestore(&consolelock, flags);
  649. if (event->xRc != HvLpEvent_Rc_Good)
  650. printk(VIOCONS_KERN_WARN
  651. "handle_open_event: event->xRc == (%d).\n",
  652. event->xRc);
  653. if (event->xCorrelationToken != 0) {
  654. atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
  655. atomic_set(aptr, 1);
  656. } else
  657. printk(VIOCONS_KERN_WARN
  658. "weird...got open ack without atomic\n");
  659. return;
  660. }
  661. /* This had better require an ack, otherwise complain */
  662. if (!hvlpevent_need_ack(event)) {
  663. printk(VIOCONS_KERN_WARN "viocharopen without ack bit!\n");
  664. return;
  665. }
  666. spin_lock_irqsave(&consolelock, flags);
  667. /* Got the lock, don't cause console output */
  668. /* Make sure this is a good virtual tty */
  669. if (port >= VTTY_PORTS) {
  670. event->xRc = HvLpEvent_Rc_SubtypeError;
  671. cevent->subtype_result_code = viorc_openRejected;
  672. /*
  673. * Flag state here since we can't printk while holding
  674. * a spinlock.
  675. */
  676. reject = 1;
  677. } else {
  678. pi = &port_info[port];
  679. if ((pi->lp != HvLpIndexInvalid) &&
  680. (pi->lp != event->xSourceLp)) {
  681. /*
  682. * If this is tty is already connected to a different
  683. * partition, fail.
  684. */
  685. event->xRc = HvLpEvent_Rc_SubtypeError;
  686. cevent->subtype_result_code = viorc_openRejected;
  687. reject = 2;
  688. } else {
  689. pi->lp = event->xSourceLp;
  690. event->xRc = HvLpEvent_Rc_Good;
  691. cevent->subtype_result_code = viorc_good;
  692. pi->seq = pi->ack = 0;
  693. reject = 0;
  694. }
  695. }
  696. spin_unlock_irqrestore(&consolelock, flags);
  697. if (reject == 1)
  698. printk(VIOCONS_KERN_WARN "open rejected: bad virtual tty.\n");
  699. else if (reject == 2)
  700. printk(VIOCONS_KERN_WARN
  701. "open rejected: console in exclusive use by another partition.\n");
  702. /* Return the acknowledgement */
  703. HvCallEvent_ackLpEvent(event);
  704. }
  705. /*
  706. * Handle a close charLpEvent. This should ONLY be an Interrupt because the
  707. * virtual console should never actually issue a close event to the hypervisor
  708. * because the virtual console never goes away. A close event coming from the
  709. * hypervisor simply means that there are no client consoles connected to the
  710. * virtual console.
  711. *
  712. * Regardless of the number of connections masqueraded on the other side of
  713. * the hypervisor ONLY ONE close event should be called to accompany the ONE
  714. * open event that is called. The close event should ONLY be called when NO
  715. * MORE connections (masqueraded or not) exist on the other side of the
  716. * hypervisor.
  717. */
  718. static void vioHandleCloseEvent(struct HvLpEvent *event)
  719. {
  720. unsigned long flags;
  721. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  722. u8 port = cevent->virtual_device;
  723. if (hvlpevent_is_int(event)) {
  724. if (port >= VTTY_PORTS) {
  725. printk(VIOCONS_KERN_WARN
  726. "close message from invalid virtual device.\n");
  727. return;
  728. }
  729. /* For closes, just mark the console partition invalid */
  730. spin_lock_irqsave(&consolelock, flags);
  731. /* Got the lock, don't cause console output */
  732. if (port_info[port].lp == event->xSourceLp)
  733. port_info[port].lp = HvLpIndexInvalid;
  734. spin_unlock_irqrestore(&consolelock, flags);
  735. printk(VIOCONS_KERN_INFO "close from %d\n", event->xSourceLp);
  736. } else
  737. printk(VIOCONS_KERN_WARN
  738. "got unexpected close acknowlegement\n");
  739. }
  740. /*
  741. * Handle a config charLpEvent. Could be either interrupt or ack
  742. */
  743. static void vioHandleConfig(struct HvLpEvent *event)
  744. {
  745. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  746. HvCall_writeLogBuffer(cevent->data, cevent->len);
  747. if (cevent->data[0] == 0x01)
  748. printk(VIOCONS_KERN_INFO "window resized to %d: %d: %d: %d\n",
  749. cevent->data[1], cevent->data[2],
  750. cevent->data[3], cevent->data[4]);
  751. else
  752. printk(VIOCONS_KERN_WARN "unknown config event\n");
  753. }
  754. /*
  755. * Handle a data charLpEvent.
  756. */
  757. static void vioHandleData(struct HvLpEvent *event)
  758. {
  759. struct tty_struct *tty;
  760. unsigned long flags;
  761. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  762. struct port_info *pi;
  763. int index;
  764. int num_pushed;
  765. u8 port = cevent->virtual_device;
  766. if (port >= VTTY_PORTS) {
  767. printk(VIOCONS_KERN_WARN "data on invalid virtual device %d\n",
  768. port);
  769. return;
  770. }
  771. /*
  772. * Hold the spinlock so that we don't take an interrupt that
  773. * changes tty between the time we fetch the port_info
  774. * pointer and the time we paranoia check.
  775. */
  776. spin_lock_irqsave(&consolelock, flags);
  777. pi = &port_info[port];
  778. /*
  779. * Change 05/01/2003 - Ryan Arnold: If a partition other than
  780. * the current exclusive partition tries to send us data
  781. * events then just drop them on the floor because we don't
  782. * want his stinking data. He isn't authorized to receive
  783. * data because he wasn't the first one to get the console,
  784. * therefore he shouldn't be allowed to send data either.
  785. * This will work without an iSeries fix.
  786. */
  787. if (pi->lp != event->xSourceLp) {
  788. spin_unlock_irqrestore(&consolelock, flags);
  789. return;
  790. }
  791. tty = pi->tty;
  792. if (tty == NULL) {
  793. spin_unlock_irqrestore(&consolelock, flags);
  794. printk(VIOCONS_KERN_WARN "no tty for virtual device %d\n",
  795. port);
  796. return;
  797. }
  798. if (tty->magic != TTY_MAGIC) {
  799. spin_unlock_irqrestore(&consolelock, flags);
  800. printk(VIOCONS_KERN_WARN "tty bad magic\n");
  801. return;
  802. }
  803. /*
  804. * Just to be paranoid, make sure the tty points back to this port
  805. */
  806. pi = (struct port_info *)tty->driver_data;
  807. if (!pi || viotty_paranoia_check(pi, tty->name, "vioHandleData")) {
  808. spin_unlock_irqrestore(&consolelock, flags);
  809. return;
  810. }
  811. spin_unlock_irqrestore(&consolelock, flags);
  812. /*
  813. * Change 07/21/2003 - Ryan Arnold: functionality added to
  814. * support sysrq utilizing ^O as the sysrq key. The sysrq
  815. * functionality will only work if built into the kernel and
  816. * then only if sysrq is enabled through the proc filesystem.
  817. */
  818. num_pushed = 0;
  819. for (index = 0; index < cevent->len; index++) {
  820. /*
  821. * Will be optimized away if !CONFIG_MAGIC_SYSRQ:
  822. */
  823. if (sysrq_on()) {
  824. /* 0x0f is the ascii character for ^O */
  825. if (cevent->data[index] == '\x0f') {
  826. vio_sysrq_pressed = 1;
  827. /*
  828. * continue because we don't want to add
  829. * the sysrq key into the data string.
  830. */
  831. continue;
  832. } else if (vio_sysrq_pressed) {
  833. handle_sysrq(cevent->data[index], tty);
  834. vio_sysrq_pressed = 0;
  835. /*
  836. * continue because we don't want to add
  837. * the sysrq sequence into the data string.
  838. */
  839. continue;
  840. }
  841. }
  842. /*
  843. * The sysrq sequence isn't included in this check if
  844. * sysrq is enabled and compiled into the kernel because
  845. * the sequence will never get inserted into the buffer.
  846. * Don't attempt to copy more data into the buffer than we
  847. * have room for because it would fail without indication.
  848. */
  849. if(tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL) == 0) {
  850. printk(VIOCONS_KERN_WARN "input buffer overflow!\n");
  851. break;
  852. }
  853. num_pushed++;
  854. }
  855. if (num_pushed)
  856. tty_flip_buffer_push(tty);
  857. }
  858. /*
  859. * Handle an ack charLpEvent.
  860. */
  861. static void vioHandleAck(struct HvLpEvent *event)
  862. {
  863. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  864. unsigned long flags;
  865. u8 port = cevent->virtual_device;
  866. if (port >= VTTY_PORTS) {
  867. printk(VIOCONS_KERN_WARN "data on invalid virtual device\n");
  868. return;
  869. }
  870. spin_lock_irqsave(&consolelock, flags);
  871. port_info[port].ack = event->xCorrelationToken;
  872. spin_unlock_irqrestore(&consolelock, flags);
  873. if (port_info[port].used)
  874. send_buffers(&port_info[port]);
  875. }
  876. /*
  877. * Handle charLpEvents and route to the appropriate routine
  878. */
  879. static void vioHandleCharEvent(struct HvLpEvent *event)
  880. {
  881. int charminor;
  882. if (event == NULL)
  883. return;
  884. charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  885. switch (charminor) {
  886. case viocharopen:
  887. vioHandleOpenEvent(event);
  888. break;
  889. case viocharclose:
  890. vioHandleCloseEvent(event);
  891. break;
  892. case viochardata:
  893. vioHandleData(event);
  894. break;
  895. case viocharack:
  896. vioHandleAck(event);
  897. break;
  898. case viocharconfig:
  899. vioHandleConfig(event);
  900. break;
  901. default:
  902. if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
  903. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  904. HvCallEvent_ackLpEvent(event);
  905. }
  906. }
  907. }
  908. /*
  909. * Send an open event
  910. */
  911. static int send_open(HvLpIndex remoteLp, void *sem)
  912. {
  913. return HvCallEvent_signalLpEventFast(remoteLp,
  914. HvLpEvent_Type_VirtualIo,
  915. viomajorsubtype_chario | viocharopen,
  916. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  917. viopath_sourceinst(remoteLp),
  918. viopath_targetinst(remoteLp),
  919. (u64)(unsigned long)sem, VIOVERSION << 16,
  920. 0, 0, 0, 0);
  921. }
  922. static const struct tty_operations serial_ops = {
  923. .open = viotty_open,
  924. .close = viotty_close,
  925. .write = viotty_write,
  926. .put_char = viotty_put_char,
  927. .write_room = viotty_write_room,
  928. .chars_in_buffer = viotty_chars_in_buffer,
  929. .ioctl = viotty_ioctl,
  930. };
  931. static int __init viocons_init2(void)
  932. {
  933. atomic_t wait_flag;
  934. int rc;
  935. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  936. return -ENODEV;
  937. /* +2 for fudge */
  938. rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
  939. viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
  940. if (rc)
  941. printk(VIOCONS_KERN_WARN "error opening to primary %d\n", rc);
  942. if (viopath_hostLp == HvLpIndexInvalid)
  943. vio_set_hostlp();
  944. /*
  945. * And if the primary is not the same as the hosting LP, open to the
  946. * hosting lp
  947. */
  948. if ((viopath_hostLp != HvLpIndexInvalid) &&
  949. (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
  950. printk(VIOCONS_KERN_INFO "open path to hosting (%d)\n",
  951. viopath_hostLp);
  952. rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
  953. VIOCHAR_WINDOW + 2); /* +2 for fudge */
  954. if (rc)
  955. printk(VIOCONS_KERN_WARN
  956. "error opening to partition %d: %d\n",
  957. viopath_hostLp, rc);
  958. }
  959. if (vio_setHandler(viomajorsubtype_chario, vioHandleCharEvent) < 0)
  960. printk(VIOCONS_KERN_WARN
  961. "error seting handler for console events!\n");
  962. /*
  963. * First, try to open the console to the hosting lp.
  964. * Wait on a semaphore for the response.
  965. */
  966. atomic_set(&wait_flag, 0);
  967. if ((viopath_isactive(viopath_hostLp)) &&
  968. (send_open(viopath_hostLp, (void *)&wait_flag) == 0)) {
  969. printk(VIOCONS_KERN_INFO "hosting partition %d\n",
  970. viopath_hostLp);
  971. while (atomic_read(&wait_flag) == 0)
  972. mb();
  973. atomic_set(&wait_flag, 0);
  974. }
  975. /*
  976. * If we don't have an active console, try the primary
  977. */
  978. if ((!viopath_isactive(port_info[0].lp)) &&
  979. (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
  980. (send_open(HvLpConfig_getPrimaryLpIndex(), (void *)&wait_flag)
  981. == 0)) {
  982. printk(VIOCONS_KERN_INFO "opening console to primary partition\n");
  983. while (atomic_read(&wait_flag) == 0)
  984. mb();
  985. }
  986. /* Initialize the tty_driver structure */
  987. viotty_driver = alloc_tty_driver(VTTY_PORTS);
  988. viotty_driver->owner = THIS_MODULE;
  989. viotty_driver->driver_name = "vioconsole";
  990. viotty_driver->name = "tty";
  991. viotty_driver->name_base = 1;
  992. viotty_driver->major = TTY_MAJOR;
  993. viotty_driver->minor_start = 1;
  994. viotty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  995. viotty_driver->subtype = 1;
  996. viotty_driver->init_termios = tty_std_termios;
  997. viotty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  998. tty_set_operations(viotty_driver, &serial_ops);
  999. if (tty_register_driver(viotty_driver)) {
  1000. printk(VIOCONS_KERN_WARN "couldn't register console driver\n");
  1001. put_tty_driver(viotty_driver);
  1002. viotty_driver = NULL;
  1003. }
  1004. unregister_console(&viocons_early);
  1005. register_console(&viocons);
  1006. return 0;
  1007. }
  1008. static int __init viocons_init(void)
  1009. {
  1010. int i;
  1011. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  1012. return -ENODEV;
  1013. printk(VIOCONS_KERN_INFO "registering console\n");
  1014. for (i = 0; i < VTTY_PORTS; i++) {
  1015. port_info[i].lp = HvLpIndexInvalid;
  1016. port_info[i].magic = VIOTTY_MAGIC;
  1017. }
  1018. HvCall_setLogBufferFormatAndCodepage(HvCall_LogBuffer_ASCII, 437);
  1019. add_preferred_console("viocons", 0, NULL);
  1020. register_console(&viocons_early);
  1021. return 0;
  1022. }
  1023. console_initcall(viocons_init);
  1024. module_init(viocons_init2);