hvsi.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
  19. * and the service processor on IBM pSeries servers. On these servers, there
  20. * are no serial ports under the OS's control, and sometimes there is no other
  21. * console available either. However, the service processor has two standard
  22. * serial ports, so this over-complicated protocol allows the OS to control
  23. * those ports by proxy.
  24. *
  25. * Besides data, the procotol supports the reading/writing of the serial
  26. * port's DTR line, and the reading of the CD line. This is to allow the OS to
  27. * control a modem attached to the service processor's serial port. Note that
  28. * the OS cannot change the speed of the port through this protocol.
  29. */
  30. #undef DEBUG
  31. #include <linux/console.h>
  32. #include <linux/ctype.h>
  33. #include <linux/delay.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/module.h>
  37. #include <linux/major.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/sysrq.h>
  42. #include <linux/tty.h>
  43. #include <linux/tty_flip.h>
  44. #include <asm/hvcall.h>
  45. #include <asm/hvconsole.h>
  46. #include <asm/prom.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/vio.h>
  49. #include <asm/param.h>
  50. #define HVSI_MAJOR 229
  51. #define HVSI_MINOR 128
  52. #define MAX_NR_HVSI_CONSOLES 4
  53. #define HVSI_TIMEOUT (5*HZ)
  54. #define HVSI_VERSION 1
  55. #define HVSI_MAX_PACKET 256
  56. #define HVSI_MAX_READ 16
  57. #define HVSI_MAX_OUTGOING_DATA 12
  58. #define N_OUTBUF 12
  59. /*
  60. * we pass data via two 8-byte registers, so we would like our char arrays
  61. * properly aligned for those loads.
  62. */
  63. #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
  64. struct hvsi_struct {
  65. struct work_struct writer;
  66. struct work_struct handshaker;
  67. wait_queue_head_t emptyq; /* woken when outbuf is emptied */
  68. wait_queue_head_t stateq; /* woken when HVSI state changes */
  69. spinlock_t lock;
  70. int index;
  71. struct tty_struct *tty;
  72. unsigned int count;
  73. uint8_t throttle_buf[128];
  74. uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
  75. /* inbuf is for packet reassembly. leave a little room for leftovers. */
  76. uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
  77. uint8_t *inbuf_end;
  78. int n_throttle;
  79. int n_outbuf;
  80. uint32_t vtermno;
  81. uint32_t virq;
  82. atomic_t seqno; /* HVSI packet sequence number */
  83. uint16_t mctrl;
  84. uint8_t state; /* HVSI protocol state */
  85. uint8_t flags;
  86. #ifdef CONFIG_MAGIC_SYSRQ
  87. uint8_t sysrq;
  88. #endif /* CONFIG_MAGIC_SYSRQ */
  89. };
  90. static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
  91. static struct tty_driver *hvsi_driver;
  92. static int hvsi_count;
  93. static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
  94. enum HVSI_PROTOCOL_STATE {
  95. HVSI_CLOSED,
  96. HVSI_WAIT_FOR_VER_RESPONSE,
  97. HVSI_WAIT_FOR_VER_QUERY,
  98. HVSI_OPEN,
  99. HVSI_WAIT_FOR_MCTRL_RESPONSE,
  100. HVSI_FSP_DIED,
  101. };
  102. #define HVSI_CONSOLE 0x1
  103. #define VS_DATA_PACKET_HEADER 0xff
  104. #define VS_CONTROL_PACKET_HEADER 0xfe
  105. #define VS_QUERY_PACKET_HEADER 0xfd
  106. #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
  107. /* control verbs */
  108. #define VSV_SET_MODEM_CTL 1 /* to service processor only */
  109. #define VSV_MODEM_CTL_UPDATE 2 /* from service processor only */
  110. #define VSV_CLOSE_PROTOCOL 3
  111. /* query verbs */
  112. #define VSV_SEND_VERSION_NUMBER 1
  113. #define VSV_SEND_MODEM_CTL_STATUS 2
  114. /* yes, these masks are not consecutive. */
  115. #define HVSI_TSDTR 0x01
  116. #define HVSI_TSCD 0x20
  117. struct hvsi_header {
  118. uint8_t type;
  119. uint8_t len;
  120. uint16_t seqno;
  121. } __attribute__((packed));
  122. struct hvsi_data {
  123. uint8_t type;
  124. uint8_t len;
  125. uint16_t seqno;
  126. uint8_t data[HVSI_MAX_OUTGOING_DATA];
  127. } __attribute__((packed));
  128. struct hvsi_control {
  129. uint8_t type;
  130. uint8_t len;
  131. uint16_t seqno;
  132. uint16_t verb;
  133. /* optional depending on verb: */
  134. uint32_t word;
  135. uint32_t mask;
  136. } __attribute__((packed));
  137. struct hvsi_query {
  138. uint8_t type;
  139. uint8_t len;
  140. uint16_t seqno;
  141. uint16_t verb;
  142. } __attribute__((packed));
  143. struct hvsi_query_response {
  144. uint8_t type;
  145. uint8_t len;
  146. uint16_t seqno;
  147. uint16_t verb;
  148. uint16_t query_seqno;
  149. union {
  150. uint8_t version;
  151. uint32_t mctrl_word;
  152. } u;
  153. } __attribute__((packed));
  154. static inline int is_console(struct hvsi_struct *hp)
  155. {
  156. return hp->flags & HVSI_CONSOLE;
  157. }
  158. static inline int is_open(struct hvsi_struct *hp)
  159. {
  160. /* if we're waiting for an mctrl then we're already open */
  161. return (hp->state == HVSI_OPEN)
  162. || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
  163. }
  164. static inline void print_state(struct hvsi_struct *hp)
  165. {
  166. #ifdef DEBUG
  167. static const char *state_names[] = {
  168. "HVSI_CLOSED",
  169. "HVSI_WAIT_FOR_VER_RESPONSE",
  170. "HVSI_WAIT_FOR_VER_QUERY",
  171. "HVSI_OPEN",
  172. "HVSI_WAIT_FOR_MCTRL_RESPONSE",
  173. "HVSI_FSP_DIED",
  174. };
  175. const char *name = state_names[hp->state];
  176. if (hp->state > ARRAY_SIZE(state_names))
  177. name = "UNKNOWN";
  178. pr_debug("hvsi%i: state = %s\n", hp->index, name);
  179. #endif /* DEBUG */
  180. }
  181. static inline void __set_state(struct hvsi_struct *hp, int state)
  182. {
  183. hp->state = state;
  184. print_state(hp);
  185. wake_up_all(&hp->stateq);
  186. }
  187. static inline void set_state(struct hvsi_struct *hp, int state)
  188. {
  189. unsigned long flags;
  190. spin_lock_irqsave(&hp->lock, flags);
  191. __set_state(hp, state);
  192. spin_unlock_irqrestore(&hp->lock, flags);
  193. }
  194. static inline int len_packet(const uint8_t *packet)
  195. {
  196. return (int)((struct hvsi_header *)packet)->len;
  197. }
  198. static inline int is_header(const uint8_t *packet)
  199. {
  200. struct hvsi_header *header = (struct hvsi_header *)packet;
  201. return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
  202. }
  203. static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
  204. {
  205. if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
  206. return 0; /* don't even have the packet header */
  207. if (hp->inbuf_end < (packet + len_packet(packet)))
  208. return 0; /* don't have the rest of the packet */
  209. return 1;
  210. }
  211. /* shift remaining bytes in packetbuf down */
  212. static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
  213. {
  214. int remaining = (int)(hp->inbuf_end - read_to);
  215. pr_debug("%s: %i chars remain\n", __FUNCTION__, remaining);
  216. if (read_to != hp->inbuf)
  217. memmove(hp->inbuf, read_to, remaining);
  218. hp->inbuf_end = hp->inbuf + remaining;
  219. }
  220. #ifdef DEBUG
  221. #define dbg_dump_packet(packet) dump_packet(packet)
  222. #define dbg_dump_hex(data, len) dump_hex(data, len)
  223. #else
  224. #define dbg_dump_packet(packet) do { } while (0)
  225. #define dbg_dump_hex(data, len) do { } while (0)
  226. #endif
  227. static void dump_hex(const uint8_t *data, int len)
  228. {
  229. int i;
  230. printk(" ");
  231. for (i=0; i < len; i++)
  232. printk("%.2x", data[i]);
  233. printk("\n ");
  234. for (i=0; i < len; i++) {
  235. if (isprint(data[i]))
  236. printk("%c", data[i]);
  237. else
  238. printk(".");
  239. }
  240. printk("\n");
  241. }
  242. static void dump_packet(uint8_t *packet)
  243. {
  244. struct hvsi_header *header = (struct hvsi_header *)packet;
  245. printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
  246. header->seqno);
  247. dump_hex(packet, header->len);
  248. }
  249. static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
  250. {
  251. unsigned long got;
  252. got = hvc_get_chars(hp->vtermno, buf, count);
  253. return got;
  254. }
  255. static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
  256. struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
  257. {
  258. struct hvsi_control *header = (struct hvsi_control *)packet;
  259. switch (header->verb) {
  260. case VSV_MODEM_CTL_UPDATE:
  261. if ((header->word & HVSI_TSCD) == 0) {
  262. /* CD went away; no more connection */
  263. pr_debug("hvsi%i: CD dropped\n", hp->index);
  264. hp->mctrl &= TIOCM_CD;
  265. if (!(hp->tty->flags & CLOCAL))
  266. *to_hangup = hp->tty;
  267. }
  268. break;
  269. case VSV_CLOSE_PROTOCOL:
  270. pr_debug("hvsi%i: service processor came back\n", hp->index);
  271. if (hp->state != HVSI_CLOSED) {
  272. *to_handshake = hp;
  273. }
  274. break;
  275. default:
  276. printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
  277. hp->index);
  278. dump_packet(packet);
  279. break;
  280. }
  281. }
  282. static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
  283. {
  284. struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
  285. switch (hp->state) {
  286. case HVSI_WAIT_FOR_VER_RESPONSE:
  287. __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
  288. break;
  289. case HVSI_WAIT_FOR_MCTRL_RESPONSE:
  290. hp->mctrl = 0;
  291. if (resp->u.mctrl_word & HVSI_TSDTR)
  292. hp->mctrl |= TIOCM_DTR;
  293. if (resp->u.mctrl_word & HVSI_TSCD)
  294. hp->mctrl |= TIOCM_CD;
  295. __set_state(hp, HVSI_OPEN);
  296. break;
  297. default:
  298. printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
  299. dump_packet(packet);
  300. break;
  301. }
  302. }
  303. /* respond to service processor's version query */
  304. static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
  305. {
  306. struct hvsi_query_response packet __ALIGNED__;
  307. int wrote;
  308. packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
  309. packet.len = sizeof(struct hvsi_query_response);
  310. packet.seqno = atomic_inc_return(&hp->seqno);
  311. packet.verb = VSV_SEND_VERSION_NUMBER;
  312. packet.u.version = HVSI_VERSION;
  313. packet.query_seqno = query_seqno+1;
  314. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  315. dbg_dump_hex((uint8_t*)&packet, packet.len);
  316. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  317. if (wrote != packet.len) {
  318. printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
  319. hp->index);
  320. return -EIO;
  321. }
  322. return 0;
  323. }
  324. static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
  325. {
  326. struct hvsi_query *query = (struct hvsi_query *)packet;
  327. switch (hp->state) {
  328. case HVSI_WAIT_FOR_VER_QUERY:
  329. hvsi_version_respond(hp, query->seqno);
  330. __set_state(hp, HVSI_OPEN);
  331. break;
  332. default:
  333. printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
  334. dump_packet(packet);
  335. break;
  336. }
  337. }
  338. static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
  339. {
  340. int i;
  341. for (i=0; i < len; i++) {
  342. char c = buf[i];
  343. #ifdef CONFIG_MAGIC_SYSRQ
  344. if (c == '\0') {
  345. hp->sysrq = 1;
  346. continue;
  347. } else if (hp->sysrq) {
  348. handle_sysrq(c, NULL, hp->tty);
  349. hp->sysrq = 0;
  350. continue;
  351. }
  352. #endif /* CONFIG_MAGIC_SYSRQ */
  353. tty_insert_flip_char(hp->tty, c, 0);
  354. }
  355. }
  356. /*
  357. * We could get 252 bytes of data at once here. But the tty layer only
  358. * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
  359. * it. Accordingly we won't send more than 128 bytes at a time to the flip
  360. * buffer, which will give the tty buffer a chance to throttle us. Should the
  361. * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
  362. * revisited.
  363. */
  364. #define TTY_THRESHOLD_THROTTLE 128
  365. static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
  366. const uint8_t *packet)
  367. {
  368. const struct hvsi_header *header = (const struct hvsi_header *)packet;
  369. const uint8_t *data = packet + sizeof(struct hvsi_header);
  370. int datalen = header->len - sizeof(struct hvsi_header);
  371. int overflow = datalen - TTY_THRESHOLD_THROTTLE;
  372. pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
  373. if (datalen == 0)
  374. return NULL;
  375. if (overflow > 0) {
  376. pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __FUNCTION__);
  377. datalen = TTY_THRESHOLD_THROTTLE;
  378. }
  379. hvsi_insert_chars(hp, data, datalen);
  380. if (overflow > 0) {
  381. /*
  382. * we still have more data to deliver, so we need to save off the
  383. * overflow and send it later
  384. */
  385. pr_debug("%s: deferring overflow\n", __FUNCTION__);
  386. memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
  387. hp->n_throttle = overflow;
  388. }
  389. return hp->tty;
  390. }
  391. /*
  392. * Returns true/false indicating data successfully read from hypervisor.
  393. * Used both to get packets for tty connections and to advance the state
  394. * machine during console handshaking (in which case tty = NULL and we ignore
  395. * incoming data).
  396. */
  397. static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
  398. struct tty_struct **hangup, struct hvsi_struct **handshake)
  399. {
  400. uint8_t *packet = hp->inbuf;
  401. int chunklen;
  402. *flip = NULL;
  403. *hangup = NULL;
  404. *handshake = NULL;
  405. chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
  406. if (chunklen == 0) {
  407. pr_debug("%s: 0-length read\n", __FUNCTION__);
  408. return 0;
  409. }
  410. pr_debug("%s: got %i bytes\n", __FUNCTION__, chunklen);
  411. dbg_dump_hex(hp->inbuf_end, chunklen);
  412. hp->inbuf_end += chunklen;
  413. /* handle all completed packets */
  414. while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
  415. struct hvsi_header *header = (struct hvsi_header *)packet;
  416. if (!is_header(packet)) {
  417. printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
  418. /* skip bytes until we find a header or run out of data */
  419. while ((packet < hp->inbuf_end) && (!is_header(packet)))
  420. packet++;
  421. continue;
  422. }
  423. pr_debug("%s: handling %i-byte packet\n", __FUNCTION__,
  424. len_packet(packet));
  425. dbg_dump_packet(packet);
  426. switch (header->type) {
  427. case VS_DATA_PACKET_HEADER:
  428. if (!is_open(hp))
  429. break;
  430. if (hp->tty == NULL)
  431. break; /* no tty buffer to put data in */
  432. *flip = hvsi_recv_data(hp, packet);
  433. break;
  434. case VS_CONTROL_PACKET_HEADER:
  435. hvsi_recv_control(hp, packet, hangup, handshake);
  436. break;
  437. case VS_QUERY_RESPONSE_PACKET_HEADER:
  438. hvsi_recv_response(hp, packet);
  439. break;
  440. case VS_QUERY_PACKET_HEADER:
  441. hvsi_recv_query(hp, packet);
  442. break;
  443. default:
  444. printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
  445. hp->index, header->type);
  446. dump_packet(packet);
  447. break;
  448. }
  449. packet += len_packet(packet);
  450. if (*hangup || *handshake) {
  451. pr_debug("%s: hangup or handshake\n", __FUNCTION__);
  452. /*
  453. * we need to send the hangup now before receiving any more data.
  454. * If we get "data, hangup, data", we can't deliver the second
  455. * data before the hangup.
  456. */
  457. break;
  458. }
  459. }
  460. compact_inbuf(hp, packet);
  461. return 1;
  462. }
  463. static void hvsi_send_overflow(struct hvsi_struct *hp)
  464. {
  465. pr_debug("%s: delivering %i bytes overflow\n", __FUNCTION__,
  466. hp->n_throttle);
  467. hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
  468. hp->n_throttle = 0;
  469. }
  470. /*
  471. * must get all pending data because we only get an irq on empty->non-empty
  472. * transition
  473. */
  474. static irqreturn_t hvsi_interrupt(int irq, void *arg, struct pt_regs *regs)
  475. {
  476. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  477. struct tty_struct *flip;
  478. struct tty_struct *hangup;
  479. struct hvsi_struct *handshake;
  480. unsigned long flags;
  481. int again = 1;
  482. pr_debug("%s\n", __FUNCTION__);
  483. while (again) {
  484. spin_lock_irqsave(&hp->lock, flags);
  485. again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
  486. spin_unlock_irqrestore(&hp->lock, flags);
  487. /*
  488. * we have to call tty_flip_buffer_push() and tty_hangup() outside our
  489. * spinlock. But we also have to keep going until we've read all the
  490. * available data.
  491. */
  492. if (flip) {
  493. /* there was data put in the tty flip buffer */
  494. tty_flip_buffer_push(flip);
  495. flip = NULL;
  496. }
  497. if (hangup) {
  498. tty_hangup(hangup);
  499. }
  500. if (handshake) {
  501. pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
  502. schedule_work(&handshake->handshaker);
  503. }
  504. }
  505. spin_lock_irqsave(&hp->lock, flags);
  506. if (hp->tty && hp->n_throttle
  507. && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
  508. /* we weren't hung up and we weren't throttled, so we can deliver the
  509. * rest now */
  510. flip = hp->tty;
  511. hvsi_send_overflow(hp);
  512. }
  513. spin_unlock_irqrestore(&hp->lock, flags);
  514. if (flip) {
  515. tty_flip_buffer_push(flip);
  516. }
  517. return IRQ_HANDLED;
  518. }
  519. /* for boot console, before the irq handler is running */
  520. static int __init poll_for_state(struct hvsi_struct *hp, int state)
  521. {
  522. unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
  523. for (;;) {
  524. hvsi_interrupt(hp->virq, (void *)hp, NULL); /* get pending data */
  525. if (hp->state == state)
  526. return 0;
  527. mdelay(5);
  528. if (time_after(jiffies, end_jiffies))
  529. return -EIO;
  530. }
  531. }
  532. /* wait for irq handler to change our state */
  533. static int wait_for_state(struct hvsi_struct *hp, int state)
  534. {
  535. int ret = 0;
  536. if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
  537. ret = -EIO;
  538. return ret;
  539. }
  540. static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
  541. {
  542. struct hvsi_query packet __ALIGNED__;
  543. int wrote;
  544. packet.type = VS_QUERY_PACKET_HEADER;
  545. packet.len = sizeof(struct hvsi_query);
  546. packet.seqno = atomic_inc_return(&hp->seqno);
  547. packet.verb = verb;
  548. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  549. dbg_dump_hex((uint8_t*)&packet, packet.len);
  550. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  551. if (wrote != packet.len) {
  552. printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
  553. wrote);
  554. return -EIO;
  555. }
  556. return 0;
  557. }
  558. static int hvsi_get_mctrl(struct hvsi_struct *hp)
  559. {
  560. int ret;
  561. set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
  562. hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
  563. ret = hvsi_wait(hp, HVSI_OPEN);
  564. if (ret < 0) {
  565. printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
  566. set_state(hp, HVSI_OPEN);
  567. return ret;
  568. }
  569. pr_debug("%s: mctrl 0x%x\n", __FUNCTION__, hp->mctrl);
  570. return 0;
  571. }
  572. /* note that we can only set DTR */
  573. static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
  574. {
  575. struct hvsi_control packet __ALIGNED__;
  576. int wrote;
  577. packet.type = VS_CONTROL_PACKET_HEADER,
  578. packet.seqno = atomic_inc_return(&hp->seqno);
  579. packet.len = sizeof(struct hvsi_control);
  580. packet.verb = VSV_SET_MODEM_CTL;
  581. packet.mask = HVSI_TSDTR;
  582. if (mctrl & TIOCM_DTR)
  583. packet.word = HVSI_TSDTR;
  584. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  585. dbg_dump_hex((uint8_t*)&packet, packet.len);
  586. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  587. if (wrote != packet.len) {
  588. printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
  589. return -EIO;
  590. }
  591. return 0;
  592. }
  593. static void hvsi_drain_input(struct hvsi_struct *hp)
  594. {
  595. uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
  596. unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
  597. while (time_before(end_jiffies, jiffies))
  598. if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
  599. break;
  600. }
  601. static int hvsi_handshake(struct hvsi_struct *hp)
  602. {
  603. int ret;
  604. /*
  605. * We could have a CLOSE or other data waiting for us before we even try
  606. * to open; try to throw it all away so we don't get confused. (CLOSE
  607. * is the first message sent up the pipe when the FSP comes online. We
  608. * need to distinguish between "it came up a while ago and we're the first
  609. * user" and "it was just reset before it saw our handshake packet".)
  610. */
  611. hvsi_drain_input(hp);
  612. set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
  613. ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
  614. if (ret < 0) {
  615. printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
  616. return ret;
  617. }
  618. ret = hvsi_wait(hp, HVSI_OPEN);
  619. if (ret < 0)
  620. return ret;
  621. return 0;
  622. }
  623. static void hvsi_handshaker(void *arg)
  624. {
  625. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  626. if (hvsi_handshake(hp) >= 0)
  627. return;
  628. printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
  629. if (is_console(hp)) {
  630. /*
  631. * ttys will re-attempt the handshake via hvsi_open, but
  632. * the console will not.
  633. */
  634. printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
  635. }
  636. }
  637. static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
  638. {
  639. struct hvsi_data packet __ALIGNED__;
  640. int ret;
  641. BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
  642. packet.type = VS_DATA_PACKET_HEADER;
  643. packet.seqno = atomic_inc_return(&hp->seqno);
  644. packet.len = count + sizeof(struct hvsi_header);
  645. memcpy(&packet.data, buf, count);
  646. ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  647. if (ret == packet.len) {
  648. /* return the number of chars written, not the packet length */
  649. return count;
  650. }
  651. return ret; /* return any errors */
  652. }
  653. static void hvsi_close_protocol(struct hvsi_struct *hp)
  654. {
  655. struct hvsi_control packet __ALIGNED__;
  656. packet.type = VS_CONTROL_PACKET_HEADER;
  657. packet.seqno = atomic_inc_return(&hp->seqno);
  658. packet.len = 6;
  659. packet.verb = VSV_CLOSE_PROTOCOL;
  660. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  661. dbg_dump_hex((uint8_t*)&packet, packet.len);
  662. hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  663. }
  664. static int hvsi_open(struct tty_struct *tty, struct file *filp)
  665. {
  666. struct hvsi_struct *hp;
  667. unsigned long flags;
  668. int line = tty->index;
  669. int ret;
  670. pr_debug("%s\n", __FUNCTION__);
  671. if (line < 0 || line >= hvsi_count)
  672. return -ENODEV;
  673. hp = &hvsi_ports[line];
  674. tty->driver_data = hp;
  675. tty->low_latency = 1; /* avoid throttle/tty_flip_buffer_push race */
  676. mb();
  677. if (hp->state == HVSI_FSP_DIED)
  678. return -EIO;
  679. spin_lock_irqsave(&hp->lock, flags);
  680. hp->tty = tty;
  681. hp->count++;
  682. atomic_set(&hp->seqno, 0);
  683. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  684. spin_unlock_irqrestore(&hp->lock, flags);
  685. if (is_console(hp))
  686. return 0; /* this has already been handshaked as the console */
  687. ret = hvsi_handshake(hp);
  688. if (ret < 0) {
  689. printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
  690. return ret;
  691. }
  692. ret = hvsi_get_mctrl(hp);
  693. if (ret < 0) {
  694. printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
  695. return ret;
  696. }
  697. ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
  698. if (ret < 0) {
  699. printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
  700. return ret;
  701. }
  702. return 0;
  703. }
  704. /* wait for hvsi_write_worker to empty hp->outbuf */
  705. static void hvsi_flush_output(struct hvsi_struct *hp)
  706. {
  707. wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
  708. /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
  709. cancel_delayed_work(&hp->writer);
  710. flush_scheduled_work();
  711. /*
  712. * it's also possible that our timeout expired and hvsi_write_worker
  713. * didn't manage to push outbuf. poof.
  714. */
  715. hp->n_outbuf = 0;
  716. }
  717. static void hvsi_close(struct tty_struct *tty, struct file *filp)
  718. {
  719. struct hvsi_struct *hp = tty->driver_data;
  720. unsigned long flags;
  721. pr_debug("%s\n", __FUNCTION__);
  722. if (tty_hung_up_p(filp))
  723. return;
  724. spin_lock_irqsave(&hp->lock, flags);
  725. if (--hp->count == 0) {
  726. hp->tty = NULL;
  727. hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
  728. /* only close down connection if it is not the console */
  729. if (!is_console(hp)) {
  730. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
  731. __set_state(hp, HVSI_CLOSED);
  732. /*
  733. * any data delivered to the tty layer after this will be
  734. * discarded (except for XON/XOFF)
  735. */
  736. tty->closing = 1;
  737. spin_unlock_irqrestore(&hp->lock, flags);
  738. /* let any existing irq handlers finish. no more will start. */
  739. synchronize_irq(hp->virq);
  740. /* hvsi_write_worker will re-schedule until outbuf is empty. */
  741. hvsi_flush_output(hp);
  742. /* tell FSP to stop sending data */
  743. hvsi_close_protocol(hp);
  744. /*
  745. * drain anything FSP is still in the middle of sending, and let
  746. * hvsi_handshake drain the rest on the next open.
  747. */
  748. hvsi_drain_input(hp);
  749. spin_lock_irqsave(&hp->lock, flags);
  750. }
  751. } else if (hp->count < 0)
  752. printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
  753. hp - hvsi_ports, hp->count);
  754. spin_unlock_irqrestore(&hp->lock, flags);
  755. }
  756. static void hvsi_hangup(struct tty_struct *tty)
  757. {
  758. struct hvsi_struct *hp = tty->driver_data;
  759. unsigned long flags;
  760. pr_debug("%s\n", __FUNCTION__);
  761. spin_lock_irqsave(&hp->lock, flags);
  762. hp->count = 0;
  763. hp->n_outbuf = 0;
  764. hp->tty = NULL;
  765. spin_unlock_irqrestore(&hp->lock, flags);
  766. }
  767. /* called with hp->lock held */
  768. static void hvsi_push(struct hvsi_struct *hp)
  769. {
  770. int n;
  771. if (hp->n_outbuf <= 0)
  772. return;
  773. n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
  774. if (n > 0) {
  775. /* success */
  776. pr_debug("%s: wrote %i chars\n", __FUNCTION__, n);
  777. hp->n_outbuf = 0;
  778. } else if (n == -EIO) {
  779. __set_state(hp, HVSI_FSP_DIED);
  780. printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
  781. }
  782. }
  783. /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
  784. static void hvsi_write_worker(void *arg)
  785. {
  786. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  787. unsigned long flags;
  788. #ifdef DEBUG
  789. static long start_j = 0;
  790. if (start_j == 0)
  791. start_j = jiffies;
  792. #endif /* DEBUG */
  793. spin_lock_irqsave(&hp->lock, flags);
  794. pr_debug("%s: %i chars in buffer\n", __FUNCTION__, hp->n_outbuf);
  795. if (!is_open(hp)) {
  796. /*
  797. * We could have a non-open connection if the service processor died
  798. * while we were busily scheduling ourselves. In that case, it could
  799. * be minutes before the service processor comes back, so only try
  800. * again once a second.
  801. */
  802. schedule_delayed_work(&hp->writer, HZ);
  803. goto out;
  804. }
  805. hvsi_push(hp);
  806. if (hp->n_outbuf > 0)
  807. schedule_delayed_work(&hp->writer, 10);
  808. else {
  809. #ifdef DEBUG
  810. pr_debug("%s: outbuf emptied after %li jiffies\n", __FUNCTION__,
  811. jiffies - start_j);
  812. start_j = 0;
  813. #endif /* DEBUG */
  814. wake_up_all(&hp->emptyq);
  815. if (test_bit(TTY_DO_WRITE_WAKEUP, &hp->tty->flags)
  816. && hp->tty->ldisc.write_wakeup)
  817. hp->tty->ldisc.write_wakeup(hp->tty);
  818. wake_up_interruptible(&hp->tty->write_wait);
  819. }
  820. out:
  821. spin_unlock_irqrestore(&hp->lock, flags);
  822. }
  823. static int hvsi_write_room(struct tty_struct *tty)
  824. {
  825. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  826. return N_OUTBUF - hp->n_outbuf;
  827. }
  828. static int hvsi_chars_in_buffer(struct tty_struct *tty)
  829. {
  830. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  831. return hp->n_outbuf;
  832. }
  833. static int hvsi_write(struct tty_struct *tty,
  834. const unsigned char *buf, int count)
  835. {
  836. struct hvsi_struct *hp = tty->driver_data;
  837. const char *source = buf;
  838. unsigned long flags;
  839. int total = 0;
  840. int origcount = count;
  841. spin_lock_irqsave(&hp->lock, flags);
  842. pr_debug("%s: %i chars in buffer\n", __FUNCTION__, hp->n_outbuf);
  843. if (!is_open(hp)) {
  844. /* we're either closing or not yet open; don't accept data */
  845. pr_debug("%s: not open\n", __FUNCTION__);
  846. goto out;
  847. }
  848. /*
  849. * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
  850. * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
  851. * will see there is no room in outbuf and return.
  852. */
  853. while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
  854. int chunksize = min(count, hvsi_write_room(hp->tty));
  855. BUG_ON(hp->n_outbuf < 0);
  856. memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
  857. hp->n_outbuf += chunksize;
  858. total += chunksize;
  859. source += chunksize;
  860. count -= chunksize;
  861. hvsi_push(hp);
  862. }
  863. if (hp->n_outbuf > 0) {
  864. /*
  865. * we weren't able to write it all to the hypervisor.
  866. * schedule another push attempt.
  867. */
  868. schedule_delayed_work(&hp->writer, 10);
  869. }
  870. out:
  871. spin_unlock_irqrestore(&hp->lock, flags);
  872. if (total != origcount)
  873. pr_debug("%s: wanted %i, only wrote %i\n", __FUNCTION__, origcount,
  874. total);
  875. return total;
  876. }
  877. /*
  878. * I have never seen throttle or unthrottle called, so this little throttle
  879. * buffering scheme may or may not work.
  880. */
  881. static void hvsi_throttle(struct tty_struct *tty)
  882. {
  883. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  884. pr_debug("%s\n", __FUNCTION__);
  885. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
  886. }
  887. static void hvsi_unthrottle(struct tty_struct *tty)
  888. {
  889. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  890. unsigned long flags;
  891. int shouldflip = 0;
  892. pr_debug("%s\n", __FUNCTION__);
  893. spin_lock_irqsave(&hp->lock, flags);
  894. if (hp->n_throttle) {
  895. hvsi_send_overflow(hp);
  896. shouldflip = 1;
  897. }
  898. spin_unlock_irqrestore(&hp->lock, flags);
  899. if (shouldflip)
  900. tty_flip_buffer_push(hp->tty);
  901. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  902. }
  903. static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
  904. {
  905. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  906. hvsi_get_mctrl(hp);
  907. return hp->mctrl;
  908. }
  909. static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
  910. unsigned int set, unsigned int clear)
  911. {
  912. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  913. unsigned long flags;
  914. uint16_t new_mctrl;
  915. /* we can only alter DTR */
  916. clear &= TIOCM_DTR;
  917. set &= TIOCM_DTR;
  918. spin_lock_irqsave(&hp->lock, flags);
  919. new_mctrl = (hp->mctrl & ~clear) | set;
  920. if (hp->mctrl != new_mctrl) {
  921. hvsi_set_mctrl(hp, new_mctrl);
  922. hp->mctrl = new_mctrl;
  923. }
  924. spin_unlock_irqrestore(&hp->lock, flags);
  925. return 0;
  926. }
  927. static struct tty_operations hvsi_ops = {
  928. .open = hvsi_open,
  929. .close = hvsi_close,
  930. .write = hvsi_write,
  931. .hangup = hvsi_hangup,
  932. .write_room = hvsi_write_room,
  933. .chars_in_buffer = hvsi_chars_in_buffer,
  934. .throttle = hvsi_throttle,
  935. .unthrottle = hvsi_unthrottle,
  936. .tiocmget = hvsi_tiocmget,
  937. .tiocmset = hvsi_tiocmset,
  938. };
  939. static int __init hvsi_init(void)
  940. {
  941. int i;
  942. hvsi_driver = alloc_tty_driver(hvsi_count);
  943. if (!hvsi_driver)
  944. return -ENOMEM;
  945. hvsi_driver->owner = THIS_MODULE;
  946. hvsi_driver->devfs_name = "hvsi/";
  947. hvsi_driver->driver_name = "hvsi";
  948. hvsi_driver->name = "hvsi";
  949. hvsi_driver->major = HVSI_MAJOR;
  950. hvsi_driver->minor_start = HVSI_MINOR;
  951. hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  952. hvsi_driver->init_termios = tty_std_termios;
  953. hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
  954. hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
  955. tty_set_operations(hvsi_driver, &hvsi_ops);
  956. for (i=0; i < hvsi_count; i++) {
  957. struct hvsi_struct *hp = &hvsi_ports[i];
  958. int ret = 1;
  959. ret = request_irq(hp->virq, hvsi_interrupt, SA_INTERRUPT, "hvsi", hp);
  960. if (ret)
  961. printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
  962. hp->virq, ret);
  963. }
  964. hvsi_wait = wait_for_state; /* irqs active now */
  965. if (tty_register_driver(hvsi_driver))
  966. panic("Couldn't register hvsi console driver\n");
  967. printk(KERN_INFO "HVSI: registered %i devices\n", hvsi_count);
  968. return 0;
  969. }
  970. device_initcall(hvsi_init);
  971. /***** console (not tty) code: *****/
  972. static void hvsi_console_print(struct console *console, const char *buf,
  973. unsigned int count)
  974. {
  975. struct hvsi_struct *hp = &hvsi_ports[console->index];
  976. char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
  977. unsigned int i = 0, n = 0;
  978. int ret, donecr = 0;
  979. mb();
  980. if (!is_open(hp))
  981. return;
  982. /*
  983. * ugh, we have to translate LF -> CRLF ourselves, in place.
  984. * copied from hvc_console.c:
  985. */
  986. while (count > 0 || i > 0) {
  987. if (count > 0 && i < sizeof(c)) {
  988. if (buf[n] == '\n' && !donecr) {
  989. c[i++] = '\r';
  990. donecr = 1;
  991. } else {
  992. c[i++] = buf[n++];
  993. donecr = 0;
  994. --count;
  995. }
  996. } else {
  997. ret = hvsi_put_chars(hp, c, i);
  998. if (ret < 0)
  999. i = 0;
  1000. i -= ret;
  1001. }
  1002. }
  1003. }
  1004. static struct tty_driver *hvsi_console_device(struct console *console,
  1005. int *index)
  1006. {
  1007. *index = console->index;
  1008. return hvsi_driver;
  1009. }
  1010. static int __init hvsi_console_setup(struct console *console, char *options)
  1011. {
  1012. struct hvsi_struct *hp = &hvsi_ports[console->index];
  1013. int ret;
  1014. if (console->index < 0 || console->index >= hvsi_count)
  1015. return -1;
  1016. /* give the FSP a chance to change the baud rate when we re-open */
  1017. hvsi_close_protocol(hp);
  1018. ret = hvsi_handshake(hp);
  1019. if (ret < 0)
  1020. return ret;
  1021. ret = hvsi_get_mctrl(hp);
  1022. if (ret < 0)
  1023. return ret;
  1024. ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
  1025. if (ret < 0)
  1026. return ret;
  1027. hp->flags |= HVSI_CONSOLE;
  1028. return 0;
  1029. }
  1030. static struct console hvsi_con_driver = {
  1031. .name = "hvsi",
  1032. .write = hvsi_console_print,
  1033. .device = hvsi_console_device,
  1034. .setup = hvsi_console_setup,
  1035. .flags = CON_PRINTBUFFER,
  1036. .index = -1,
  1037. };
  1038. static int __init hvsi_console_init(void)
  1039. {
  1040. struct device_node *vty;
  1041. hvsi_wait = poll_for_state; /* no irqs yet; must poll */
  1042. /* search device tree for vty nodes */
  1043. for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
  1044. vty != NULL;
  1045. vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
  1046. struct hvsi_struct *hp;
  1047. uint32_t *vtermno;
  1048. uint32_t *irq;
  1049. vtermno = (uint32_t *)get_property(vty, "reg", NULL);
  1050. irq = (uint32_t *)get_property(vty, "interrupts", NULL);
  1051. if (!vtermno || !irq)
  1052. continue;
  1053. if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
  1054. of_node_put(vty);
  1055. break;
  1056. }
  1057. hp = &hvsi_ports[hvsi_count];
  1058. INIT_WORK(&hp->writer, hvsi_write_worker, hp);
  1059. INIT_WORK(&hp->handshaker, hvsi_handshaker, hp);
  1060. init_waitqueue_head(&hp->emptyq);
  1061. init_waitqueue_head(&hp->stateq);
  1062. spin_lock_init(&hp->lock);
  1063. hp->index = hvsi_count;
  1064. hp->inbuf_end = hp->inbuf;
  1065. hp->state = HVSI_CLOSED;
  1066. hp->vtermno = *vtermno;
  1067. hp->virq = virt_irq_create_mapping(irq[0]);
  1068. if (hp->virq == NO_IRQ) {
  1069. printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
  1070. __FUNCTION__, hp->virq);
  1071. continue;
  1072. } else
  1073. hp->virq = irq_offset_up(hp->virq);
  1074. hvsi_count++;
  1075. }
  1076. if (hvsi_count)
  1077. register_console(&hvsi_con_driver);
  1078. return 0;
  1079. }
  1080. console_initcall(hvsi_console_init);