viocons.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. static 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. static 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. struct HvLpEvent *hev = &viochar->event;
  421. memset(viochar, 0, sizeof(struct viocharlpevent));
  422. hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
  423. HV_LP_EVENT_INT;
  424. hev->xType = HvLpEvent_Type_VirtualIo;
  425. hev->xSubtype = viomajorsubtype_chario | viochardata;
  426. hev->xSourceLp = HvLpConfig_getLpIndex();
  427. hev->xTargetLp = lp;
  428. hev->xSizeMinus1 = sizeof(struct viocharlpevent);
  429. hev->xSourceInstanceId = viopath_sourceinst(lp);
  430. hev->xTargetInstanceId = viopath_targetinst(lp);
  431. }
  432. /*
  433. * early console device write
  434. */
  435. static void viocons_write_early(struct console *co, const char *s, unsigned count)
  436. {
  437. hvlogOutput(s, count);
  438. }
  439. /*
  440. * console device write
  441. */
  442. static void viocons_write(struct console *co, const char *s, unsigned count)
  443. {
  444. int index;
  445. int begin;
  446. struct port_info *pi;
  447. static const char cr = '\r';
  448. /*
  449. * Check port data first because the target LP might be valid but
  450. * simply not active, in which case we want to hvlog the output.
  451. */
  452. pi = get_port_data(NULL);
  453. if (pi == NULL) {
  454. hvlog("\n\rviocons_write: unable to get port data.");
  455. return;
  456. }
  457. hvlogOutput(s, count);
  458. if (!viopath_isactive(pi->lp))
  459. return;
  460. /*
  461. * Any newline character found will cause a
  462. * carriage return character to be emitted as well.
  463. */
  464. begin = 0;
  465. for (index = 0; index < count; index++) {
  466. if (s[index] == '\n') {
  467. /*
  468. * Newline found. Print everything up to and
  469. * including the newline
  470. */
  471. internal_write(pi, &s[begin], index - begin + 1);
  472. begin = index + 1;
  473. /* Emit a carriage return as well */
  474. internal_write(pi, &cr, 1);
  475. }
  476. }
  477. /* If any characters left to write, write them now */
  478. if ((index - begin) > 0)
  479. internal_write(pi, &s[begin], index - begin);
  480. }
  481. /*
  482. * Work out the device associate with this console
  483. */
  484. static struct tty_driver *viocons_device(struct console *c, int *index)
  485. {
  486. *index = c->index;
  487. return viotty_driver;
  488. }
  489. /*
  490. * console device I/O methods
  491. */
  492. static struct console viocons_early = {
  493. .name = "viocons",
  494. .write = viocons_write_early,
  495. .flags = CON_PRINTBUFFER,
  496. .index = -1,
  497. };
  498. static struct console viocons = {
  499. .name = "viocons",
  500. .write = viocons_write,
  501. .device = viocons_device,
  502. .flags = CON_PRINTBUFFER,
  503. .index = -1,
  504. };
  505. /*
  506. * TTY Open method
  507. */
  508. static int viotty_open(struct tty_struct *tty, struct file *filp)
  509. {
  510. int port;
  511. unsigned long flags;
  512. struct port_info *pi;
  513. port = tty->index;
  514. if ((port < 0) || (port >= VTTY_PORTS))
  515. return -ENODEV;
  516. spin_lock_irqsave(&consolelock, flags);
  517. pi = &port_info[port];
  518. /* If some other TTY is already connected here, reject the open */
  519. if ((pi->tty) && (pi->tty != tty)) {
  520. spin_unlock_irqrestore(&consolelock, flags);
  521. printk(VIOCONS_KERN_WARN
  522. "attempt to open device twice from different ttys\n");
  523. return -EBUSY;
  524. }
  525. tty->driver_data = pi;
  526. pi->tty = tty;
  527. spin_unlock_irqrestore(&consolelock, flags);
  528. return 0;
  529. }
  530. /*
  531. * TTY Close method
  532. */
  533. static void viotty_close(struct tty_struct *tty, struct file *filp)
  534. {
  535. unsigned long flags;
  536. struct port_info *pi;
  537. spin_lock_irqsave(&consolelock, flags);
  538. pi = (struct port_info *)tty->driver_data;
  539. if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_close")) {
  540. spin_unlock_irqrestore(&consolelock, flags);
  541. return;
  542. }
  543. if (tty->count == 1)
  544. pi->tty = NULL;
  545. spin_unlock_irqrestore(&consolelock, flags);
  546. }
  547. /*
  548. * TTY Write method
  549. */
  550. static int viotty_write(struct tty_struct *tty, const unsigned char *buf,
  551. int count)
  552. {
  553. struct port_info *pi;
  554. pi = get_port_data(tty);
  555. if (pi == NULL) {
  556. hvlog("\n\rviotty_write: no port data.");
  557. return -ENODEV;
  558. }
  559. if (viochar_is_console(pi))
  560. hvlogOutput(buf, count);
  561. /*
  562. * If the path to this LP is closed, don't bother doing anything more.
  563. * just dump the data on the floor and return count. For some reason
  564. * some user level programs will attempt to probe available tty's and
  565. * they'll attempt a viotty_write on an invalid port which maps to an
  566. * invalid target lp. If this is the case then ignore the
  567. * viotty_write call and, since the viopath isn't active to this
  568. * partition, return count.
  569. */
  570. if (!viopath_isactive(pi->lp))
  571. return count;
  572. return internal_write(pi, buf, count);
  573. }
  574. /*
  575. * TTY put_char method
  576. */
  577. static void viotty_put_char(struct tty_struct *tty, unsigned char ch)
  578. {
  579. struct port_info *pi;
  580. pi = get_port_data(tty);
  581. if (pi == NULL)
  582. return;
  583. /* This will append '\r' as well if the char is '\n' */
  584. if (viochar_is_console(pi))
  585. hvlogOutput(&ch, 1);
  586. if (viopath_isactive(pi->lp))
  587. internal_write(pi, &ch, 1);
  588. }
  589. /*
  590. * TTY write_room method
  591. */
  592. static int viotty_write_room(struct tty_struct *tty)
  593. {
  594. int i;
  595. int room = 0;
  596. struct port_info *pi;
  597. unsigned long flags;
  598. spin_lock_irqsave(&consolelock, flags);
  599. pi = (struct port_info *)tty->driver_data;
  600. if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_write_room")) {
  601. spin_unlock_irqrestore(&consolelock, flags);
  602. return 0;
  603. }
  604. /* If no buffers are used, return the max size. */
  605. if (pi->used == 0) {
  606. spin_unlock_irqrestore(&consolelock, flags);
  607. return VIOCHAR_MAX_DATA * VIOCHAR_NUM_BUF;
  608. }
  609. /*
  610. * We retain the spinlock because we want to get an accurate
  611. * count and it can change on us between each operation if we
  612. * don't hold the spinlock.
  613. */
  614. for (i = 0; ((i < VIOCHAR_NUM_BUF) && (room < VIOCHAR_MAX_DATA)); i++)
  615. room += (VIOCHAR_MAX_DATA - pi->bufferBytes[i]);
  616. spin_unlock_irqrestore(&consolelock, flags);
  617. if (room > VIOCHAR_MAX_DATA)
  618. room = VIOCHAR_MAX_DATA;
  619. return room;
  620. }
  621. /*
  622. * TTY chars_in_buffer method
  623. */
  624. static int viotty_chars_in_buffer(struct tty_struct *tty)
  625. {
  626. return 0;
  627. }
  628. static int viotty_ioctl(struct tty_struct *tty, struct file *file,
  629. unsigned int cmd, unsigned long arg)
  630. {
  631. switch (cmd) {
  632. /*
  633. * the ioctls below read/set the flags usually shown in the leds
  634. * don't use them - they will go away without warning
  635. */
  636. case KDGETLED:
  637. case KDGKBLED:
  638. return put_user(0, (char *)arg);
  639. case KDSKBLED:
  640. return 0;
  641. }
  642. return n_tty_ioctl(tty, file, cmd, arg);
  643. }
  644. /*
  645. * Handle an open charLpEvent. Could be either interrupt or ack
  646. */
  647. static void vioHandleOpenEvent(struct HvLpEvent *event)
  648. {
  649. unsigned long flags;
  650. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  651. u8 port = cevent->virtual_device;
  652. struct port_info *pi;
  653. int reject = 0;
  654. if (hvlpevent_is_ack(event)) {
  655. if (port >= VTTY_PORTS)
  656. return;
  657. spin_lock_irqsave(&consolelock, flags);
  658. /* Got the lock, don't cause console output */
  659. pi = &port_info[port];
  660. if (event->xRc == HvLpEvent_Rc_Good) {
  661. pi->seq = pi->ack = 0;
  662. /*
  663. * This line allows connections from the primary
  664. * partition but once one is connected from the
  665. * primary partition nothing short of a reboot
  666. * of linux will allow access from the hosting
  667. * partition again without a required iSeries fix.
  668. */
  669. pi->lp = event->xTargetLp;
  670. }
  671. spin_unlock_irqrestore(&consolelock, flags);
  672. if (event->xRc != HvLpEvent_Rc_Good)
  673. printk(VIOCONS_KERN_WARN
  674. "handle_open_event: event->xRc == (%d).\n",
  675. event->xRc);
  676. if (event->xCorrelationToken != 0) {
  677. atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
  678. atomic_set(aptr, 1);
  679. } else
  680. printk(VIOCONS_KERN_WARN
  681. "weird...got open ack without atomic\n");
  682. return;
  683. }
  684. /* This had better require an ack, otherwise complain */
  685. if (!hvlpevent_need_ack(event)) {
  686. printk(VIOCONS_KERN_WARN "viocharopen without ack bit!\n");
  687. return;
  688. }
  689. spin_lock_irqsave(&consolelock, flags);
  690. /* Got the lock, don't cause console output */
  691. /* Make sure this is a good virtual tty */
  692. if (port >= VTTY_PORTS) {
  693. event->xRc = HvLpEvent_Rc_SubtypeError;
  694. cevent->subtype_result_code = viorc_openRejected;
  695. /*
  696. * Flag state here since we can't printk while holding
  697. * a spinlock.
  698. */
  699. reject = 1;
  700. } else {
  701. pi = &port_info[port];
  702. if ((pi->lp != HvLpIndexInvalid) &&
  703. (pi->lp != event->xSourceLp)) {
  704. /*
  705. * If this is tty is already connected to a different
  706. * partition, fail.
  707. */
  708. event->xRc = HvLpEvent_Rc_SubtypeError;
  709. cevent->subtype_result_code = viorc_openRejected;
  710. reject = 2;
  711. } else {
  712. pi->lp = event->xSourceLp;
  713. event->xRc = HvLpEvent_Rc_Good;
  714. cevent->subtype_result_code = viorc_good;
  715. pi->seq = pi->ack = 0;
  716. reject = 0;
  717. }
  718. }
  719. spin_unlock_irqrestore(&consolelock, flags);
  720. if (reject == 1)
  721. printk(VIOCONS_KERN_WARN "open rejected: bad virtual tty.\n");
  722. else if (reject == 2)
  723. printk(VIOCONS_KERN_WARN
  724. "open rejected: console in exclusive use by another partition.\n");
  725. /* Return the acknowledgement */
  726. HvCallEvent_ackLpEvent(event);
  727. }
  728. /*
  729. * Handle a close charLpEvent. This should ONLY be an Interrupt because the
  730. * virtual console should never actually issue a close event to the hypervisor
  731. * because the virtual console never goes away. A close event coming from the
  732. * hypervisor simply means that there are no client consoles connected to the
  733. * virtual console.
  734. *
  735. * Regardless of the number of connections masqueraded on the other side of
  736. * the hypervisor ONLY ONE close event should be called to accompany the ONE
  737. * open event that is called. The close event should ONLY be called when NO
  738. * MORE connections (masqueraded or not) exist on the other side of the
  739. * hypervisor.
  740. */
  741. static void vioHandleCloseEvent(struct HvLpEvent *event)
  742. {
  743. unsigned long flags;
  744. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  745. u8 port = cevent->virtual_device;
  746. if (hvlpevent_is_int(event)) {
  747. if (port >= VTTY_PORTS) {
  748. printk(VIOCONS_KERN_WARN
  749. "close message from invalid virtual device.\n");
  750. return;
  751. }
  752. /* For closes, just mark the console partition invalid */
  753. spin_lock_irqsave(&consolelock, flags);
  754. /* Got the lock, don't cause console output */
  755. if (port_info[port].lp == event->xSourceLp)
  756. port_info[port].lp = HvLpIndexInvalid;
  757. spin_unlock_irqrestore(&consolelock, flags);
  758. printk(VIOCONS_KERN_INFO "close from %d\n", event->xSourceLp);
  759. } else
  760. printk(VIOCONS_KERN_WARN
  761. "got unexpected close acknowlegement\n");
  762. }
  763. /*
  764. * Handle a config charLpEvent. Could be either interrupt or ack
  765. */
  766. static void vioHandleConfig(struct HvLpEvent *event)
  767. {
  768. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  769. HvCall_writeLogBuffer(cevent->data, cevent->len);
  770. if (cevent->data[0] == 0x01)
  771. printk(VIOCONS_KERN_INFO "window resized to %d: %d: %d: %d\n",
  772. cevent->data[1], cevent->data[2],
  773. cevent->data[3], cevent->data[4]);
  774. else
  775. printk(VIOCONS_KERN_WARN "unknown config event\n");
  776. }
  777. /*
  778. * Handle a data charLpEvent.
  779. */
  780. static void vioHandleData(struct HvLpEvent *event)
  781. {
  782. struct tty_struct *tty;
  783. unsigned long flags;
  784. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  785. struct port_info *pi;
  786. int index;
  787. int num_pushed;
  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. num_pushed = 0;
  842. for (index = 0; index < cevent->len; index++) {
  843. #ifdef CONFIG_MAGIC_SYSRQ
  844. if (sysrq_enabled) {
  845. /* 0x0f is the ascii character for ^O */
  846. if (cevent->data[index] == '\x0f') {
  847. vio_sysrq_pressed = 1;
  848. /*
  849. * continue because we don't want to add
  850. * the sysrq key into the data string.
  851. */
  852. continue;
  853. } else if (vio_sysrq_pressed) {
  854. handle_sysrq(cevent->data[index], NULL, tty);
  855. vio_sysrq_pressed = 0;
  856. /*
  857. * continue because we don't want to add
  858. * the sysrq sequence into the data string.
  859. */
  860. continue;
  861. }
  862. }
  863. #endif
  864. /*
  865. * The sysrq sequence isn't included in this check if
  866. * sysrq is enabled and compiled into the kernel because
  867. * the sequence will never get inserted into the buffer.
  868. * Don't attempt to copy more data into the buffer than we
  869. * have room for because it would fail without indication.
  870. */
  871. if(tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL) == 0) {
  872. printk(VIOCONS_KERN_WARN "input buffer overflow!\n");
  873. break;
  874. }
  875. num_pushed++;
  876. }
  877. if (num_pushed)
  878. tty_flip_buffer_push(tty);
  879. }
  880. /*
  881. * Handle an ack charLpEvent.
  882. */
  883. static void vioHandleAck(struct HvLpEvent *event)
  884. {
  885. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  886. unsigned long flags;
  887. u8 port = cevent->virtual_device;
  888. if (port >= VTTY_PORTS) {
  889. printk(VIOCONS_KERN_WARN "data on invalid virtual device\n");
  890. return;
  891. }
  892. spin_lock_irqsave(&consolelock, flags);
  893. port_info[port].ack = event->xCorrelationToken;
  894. spin_unlock_irqrestore(&consolelock, flags);
  895. if (port_info[port].used)
  896. send_buffers(&port_info[port]);
  897. }
  898. /*
  899. * Handle charLpEvents and route to the appropriate routine
  900. */
  901. static void vioHandleCharEvent(struct HvLpEvent *event)
  902. {
  903. int charminor;
  904. if (event == NULL)
  905. return;
  906. charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  907. switch (charminor) {
  908. case viocharopen:
  909. vioHandleOpenEvent(event);
  910. break;
  911. case viocharclose:
  912. vioHandleCloseEvent(event);
  913. break;
  914. case viochardata:
  915. vioHandleData(event);
  916. break;
  917. case viocharack:
  918. vioHandleAck(event);
  919. break;
  920. case viocharconfig:
  921. vioHandleConfig(event);
  922. break;
  923. default:
  924. if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
  925. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  926. HvCallEvent_ackLpEvent(event);
  927. }
  928. }
  929. }
  930. /*
  931. * Send an open event
  932. */
  933. static int send_open(HvLpIndex remoteLp, void *sem)
  934. {
  935. return HvCallEvent_signalLpEventFast(remoteLp,
  936. HvLpEvent_Type_VirtualIo,
  937. viomajorsubtype_chario | viocharopen,
  938. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  939. viopath_sourceinst(remoteLp),
  940. viopath_targetinst(remoteLp),
  941. (u64)(unsigned long)sem, VIOVERSION << 16,
  942. 0, 0, 0, 0);
  943. }
  944. static struct tty_operations serial_ops = {
  945. .open = viotty_open,
  946. .close = viotty_close,
  947. .write = viotty_write,
  948. .put_char = viotty_put_char,
  949. .write_room = viotty_write_room,
  950. .chars_in_buffer = viotty_chars_in_buffer,
  951. .ioctl = viotty_ioctl,
  952. };
  953. static int __init viocons_init2(void)
  954. {
  955. atomic_t wait_flag;
  956. int rc;
  957. /* +2 for fudge */
  958. rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
  959. viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
  960. if (rc)
  961. printk(VIOCONS_KERN_WARN "error opening to primary %d\n", rc);
  962. if (viopath_hostLp == HvLpIndexInvalid)
  963. vio_set_hostlp();
  964. /*
  965. * And if the primary is not the same as the hosting LP, open to the
  966. * hosting lp
  967. */
  968. if ((viopath_hostLp != HvLpIndexInvalid) &&
  969. (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
  970. printk(VIOCONS_KERN_INFO "open path to hosting (%d)\n",
  971. viopath_hostLp);
  972. rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
  973. VIOCHAR_WINDOW + 2); /* +2 for fudge */
  974. if (rc)
  975. printk(VIOCONS_KERN_WARN
  976. "error opening to partition %d: %d\n",
  977. viopath_hostLp, rc);
  978. }
  979. if (vio_setHandler(viomajorsubtype_chario, vioHandleCharEvent) < 0)
  980. printk(VIOCONS_KERN_WARN
  981. "error seting handler for console events!\n");
  982. /*
  983. * First, try to open the console to the hosting lp.
  984. * Wait on a semaphore for the response.
  985. */
  986. atomic_set(&wait_flag, 0);
  987. if ((viopath_isactive(viopath_hostLp)) &&
  988. (send_open(viopath_hostLp, (void *)&wait_flag) == 0)) {
  989. printk(VIOCONS_KERN_INFO "hosting partition %d\n",
  990. viopath_hostLp);
  991. while (atomic_read(&wait_flag) == 0)
  992. mb();
  993. atomic_set(&wait_flag, 0);
  994. }
  995. /*
  996. * If we don't have an active console, try the primary
  997. */
  998. if ((!viopath_isactive(port_info[0].lp)) &&
  999. (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
  1000. (send_open(HvLpConfig_getPrimaryLpIndex(), (void *)&wait_flag)
  1001. == 0)) {
  1002. printk(VIOCONS_KERN_INFO "opening console to primary partition\n");
  1003. while (atomic_read(&wait_flag) == 0)
  1004. mb();
  1005. }
  1006. /* Initialize the tty_driver structure */
  1007. viotty_driver = alloc_tty_driver(VTTY_PORTS);
  1008. viotty_driver->owner = THIS_MODULE;
  1009. viotty_driver->driver_name = "vioconsole";
  1010. viotty_driver->devfs_name = "vcs/";
  1011. viotty_driver->name = "tty";
  1012. viotty_driver->name_base = 1;
  1013. viotty_driver->major = TTY_MAJOR;
  1014. viotty_driver->minor_start = 1;
  1015. viotty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  1016. viotty_driver->subtype = 1;
  1017. viotty_driver->init_termios = tty_std_termios;
  1018. viotty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  1019. tty_set_operations(viotty_driver, &serial_ops);
  1020. if (tty_register_driver(viotty_driver)) {
  1021. printk(VIOCONS_KERN_WARN "couldn't register console driver\n");
  1022. put_tty_driver(viotty_driver);
  1023. viotty_driver = NULL;
  1024. }
  1025. unregister_console(&viocons_early);
  1026. register_console(&viocons);
  1027. return 0;
  1028. }
  1029. static int __init viocons_init(void)
  1030. {
  1031. int i;
  1032. printk(VIOCONS_KERN_INFO "registering console\n");
  1033. for (i = 0; i < VTTY_PORTS; i++) {
  1034. port_info[i].lp = HvLpIndexInvalid;
  1035. port_info[i].magic = VIOTTY_MAGIC;
  1036. }
  1037. HvCall_setLogBufferFormatAndCodepage(HvCall_LogBuffer_ASCII, 437);
  1038. register_console(&viocons_early);
  1039. return 0;
  1040. }
  1041. console_initcall(viocons_init);
  1042. module_init(viocons_init2);