viocons.c 31 KB

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