hvsi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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 > (sizeof(state_names)/sizeof(char*)))
  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. /* can't use hvc_get_chars because that strips CRs */
  250. static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
  251. {
  252. unsigned long got;
  253. if (plpar_hcall(H_GET_TERM_CHAR, hp->vtermno, 0, 0, 0, &got,
  254. (unsigned long *)buf, (unsigned long *)buf+1) == H_Success)
  255. return got;
  256. return 0;
  257. }
  258. static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
  259. struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
  260. {
  261. struct hvsi_control *header = (struct hvsi_control *)packet;
  262. switch (header->verb) {
  263. case VSV_MODEM_CTL_UPDATE:
  264. if ((header->word & HVSI_TSCD) == 0) {
  265. /* CD went away; no more connection */
  266. pr_debug("hvsi%i: CD dropped\n", hp->index);
  267. hp->mctrl &= TIOCM_CD;
  268. if (!(hp->tty->flags & CLOCAL))
  269. *to_hangup = hp->tty;
  270. }
  271. break;
  272. case VSV_CLOSE_PROTOCOL:
  273. pr_debug("hvsi%i: service processor came back\n", hp->index);
  274. if (hp->state != HVSI_CLOSED) {
  275. *to_handshake = hp;
  276. }
  277. break;
  278. default:
  279. printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
  280. hp->index);
  281. dump_packet(packet);
  282. break;
  283. }
  284. }
  285. static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
  286. {
  287. struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
  288. switch (hp->state) {
  289. case HVSI_WAIT_FOR_VER_RESPONSE:
  290. __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
  291. break;
  292. case HVSI_WAIT_FOR_MCTRL_RESPONSE:
  293. hp->mctrl = 0;
  294. if (resp->u.mctrl_word & HVSI_TSDTR)
  295. hp->mctrl |= TIOCM_DTR;
  296. if (resp->u.mctrl_word & HVSI_TSCD)
  297. hp->mctrl |= TIOCM_CD;
  298. __set_state(hp, HVSI_OPEN);
  299. break;
  300. default:
  301. printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
  302. dump_packet(packet);
  303. break;
  304. }
  305. }
  306. /* respond to service processor's version query */
  307. static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
  308. {
  309. struct hvsi_query_response packet __ALIGNED__;
  310. int wrote;
  311. packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
  312. packet.len = sizeof(struct hvsi_query_response);
  313. packet.seqno = atomic_inc_return(&hp->seqno);
  314. packet.verb = VSV_SEND_VERSION_NUMBER;
  315. packet.u.version = HVSI_VERSION;
  316. packet.query_seqno = query_seqno+1;
  317. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  318. dbg_dump_hex((uint8_t*)&packet, packet.len);
  319. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  320. if (wrote != packet.len) {
  321. printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
  322. hp->index);
  323. return -EIO;
  324. }
  325. return 0;
  326. }
  327. static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
  328. {
  329. struct hvsi_query *query = (struct hvsi_query *)packet;
  330. switch (hp->state) {
  331. case HVSI_WAIT_FOR_VER_QUERY:
  332. hvsi_version_respond(hp, query->seqno);
  333. __set_state(hp, HVSI_OPEN);
  334. break;
  335. default:
  336. printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
  337. dump_packet(packet);
  338. break;
  339. }
  340. }
  341. static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
  342. {
  343. int i;
  344. for (i=0; i < len; i++) {
  345. char c = buf[i];
  346. #ifdef CONFIG_MAGIC_SYSRQ
  347. if (c == '\0') {
  348. hp->sysrq = 1;
  349. continue;
  350. } else if (hp->sysrq) {
  351. handle_sysrq(c, NULL, hp->tty);
  352. hp->sysrq = 0;
  353. continue;
  354. }
  355. #endif /* CONFIG_MAGIC_SYSRQ */
  356. tty_insert_flip_char(hp->tty, c, 0);
  357. }
  358. }
  359. /*
  360. * We could get 252 bytes of data at once here. But the tty layer only
  361. * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
  362. * it. Accordingly we won't send more than 128 bytes at a time to the flip
  363. * buffer, which will give the tty buffer a chance to throttle us. Should the
  364. * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
  365. * revisited.
  366. */
  367. #define TTY_THRESHOLD_THROTTLE 128
  368. static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
  369. const uint8_t *packet)
  370. {
  371. const struct hvsi_header *header = (const struct hvsi_header *)packet;
  372. const uint8_t *data = packet + sizeof(struct hvsi_header);
  373. int datalen = header->len - sizeof(struct hvsi_header);
  374. int overflow = datalen - TTY_THRESHOLD_THROTTLE;
  375. pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
  376. if (datalen == 0)
  377. return NULL;
  378. if (overflow > 0) {
  379. pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __FUNCTION__);
  380. datalen = TTY_THRESHOLD_THROTTLE;
  381. }
  382. hvsi_insert_chars(hp, data, datalen);
  383. if (overflow > 0) {
  384. /*
  385. * we still have more data to deliver, so we need to save off the
  386. * overflow and send it later
  387. */
  388. pr_debug("%s: deferring overflow\n", __FUNCTION__);
  389. memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
  390. hp->n_throttle = overflow;
  391. }
  392. return hp->tty;
  393. }
  394. /*
  395. * Returns true/false indicating data successfully read from hypervisor.
  396. * Used both to get packets for tty connections and to advance the state
  397. * machine during console handshaking (in which case tty = NULL and we ignore
  398. * incoming data).
  399. */
  400. static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
  401. struct tty_struct **hangup, struct hvsi_struct **handshake)
  402. {
  403. uint8_t *packet = hp->inbuf;
  404. int chunklen;
  405. *flip = NULL;
  406. *hangup = NULL;
  407. *handshake = NULL;
  408. chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
  409. if (chunklen == 0) {
  410. pr_debug("%s: 0-length read\n", __FUNCTION__);
  411. return 0;
  412. }
  413. pr_debug("%s: got %i bytes\n", __FUNCTION__, chunklen);
  414. dbg_dump_hex(hp->inbuf_end, chunklen);
  415. hp->inbuf_end += chunklen;
  416. /* handle all completed packets */
  417. while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
  418. struct hvsi_header *header = (struct hvsi_header *)packet;
  419. if (!is_header(packet)) {
  420. printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
  421. /* skip bytes until we find a header or run out of data */
  422. while ((packet < hp->inbuf_end) && (!is_header(packet)))
  423. packet++;
  424. continue;
  425. }
  426. pr_debug("%s: handling %i-byte packet\n", __FUNCTION__,
  427. len_packet(packet));
  428. dbg_dump_packet(packet);
  429. switch (header->type) {
  430. case VS_DATA_PACKET_HEADER:
  431. if (!is_open(hp))
  432. break;
  433. if (hp->tty == NULL)
  434. break; /* no tty buffer to put data in */
  435. *flip = hvsi_recv_data(hp, packet);
  436. break;
  437. case VS_CONTROL_PACKET_HEADER:
  438. hvsi_recv_control(hp, packet, hangup, handshake);
  439. break;
  440. case VS_QUERY_RESPONSE_PACKET_HEADER:
  441. hvsi_recv_response(hp, packet);
  442. break;
  443. case VS_QUERY_PACKET_HEADER:
  444. hvsi_recv_query(hp, packet);
  445. break;
  446. default:
  447. printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
  448. hp->index, header->type);
  449. dump_packet(packet);
  450. break;
  451. }
  452. packet += len_packet(packet);
  453. if (*hangup || *handshake) {
  454. pr_debug("%s: hangup or handshake\n", __FUNCTION__);
  455. /*
  456. * we need to send the hangup now before receiving any more data.
  457. * If we get "data, hangup, data", we can't deliver the second
  458. * data before the hangup.
  459. */
  460. break;
  461. }
  462. }
  463. compact_inbuf(hp, packet);
  464. return 1;
  465. }
  466. static void hvsi_send_overflow(struct hvsi_struct *hp)
  467. {
  468. pr_debug("%s: delivering %i bytes overflow\n", __FUNCTION__,
  469. hp->n_throttle);
  470. hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
  471. hp->n_throttle = 0;
  472. }
  473. /*
  474. * must get all pending data because we only get an irq on empty->non-empty
  475. * transition
  476. */
  477. static irqreturn_t hvsi_interrupt(int irq, void *arg, struct pt_regs *regs)
  478. {
  479. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  480. struct tty_struct *flip;
  481. struct tty_struct *hangup;
  482. struct hvsi_struct *handshake;
  483. unsigned long flags;
  484. int again = 1;
  485. pr_debug("%s\n", __FUNCTION__);
  486. while (again) {
  487. spin_lock_irqsave(&hp->lock, flags);
  488. again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
  489. spin_unlock_irqrestore(&hp->lock, flags);
  490. /*
  491. * we have to call tty_flip_buffer_push() and tty_hangup() outside our
  492. * spinlock. But we also have to keep going until we've read all the
  493. * available data.
  494. */
  495. if (flip) {
  496. /* there was data put in the tty flip buffer */
  497. tty_flip_buffer_push(flip);
  498. flip = NULL;
  499. }
  500. if (hangup) {
  501. tty_hangup(hangup);
  502. }
  503. if (handshake) {
  504. pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
  505. schedule_work(&handshake->handshaker);
  506. }
  507. }
  508. spin_lock_irqsave(&hp->lock, flags);
  509. if (hp->tty && hp->n_throttle
  510. && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
  511. /* we weren't hung up and we weren't throttled, so we can deliver the
  512. * rest now */
  513. flip = hp->tty;
  514. hvsi_send_overflow(hp);
  515. }
  516. spin_unlock_irqrestore(&hp->lock, flags);
  517. if (flip) {
  518. tty_flip_buffer_push(flip);
  519. }
  520. return IRQ_HANDLED;
  521. }
  522. /* for boot console, before the irq handler is running */
  523. static int __init poll_for_state(struct hvsi_struct *hp, int state)
  524. {
  525. unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
  526. for (;;) {
  527. hvsi_interrupt(hp->virq, (void *)hp, NULL); /* get pending data */
  528. if (hp->state == state)
  529. return 0;
  530. mdelay(5);
  531. if (time_after(jiffies, end_jiffies))
  532. return -EIO;
  533. }
  534. }
  535. /* wait for irq handler to change our state */
  536. static int wait_for_state(struct hvsi_struct *hp, int state)
  537. {
  538. int ret = 0;
  539. if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
  540. ret = -EIO;
  541. return ret;
  542. }
  543. static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
  544. {
  545. struct hvsi_query packet __ALIGNED__;
  546. int wrote;
  547. packet.type = VS_QUERY_PACKET_HEADER;
  548. packet.len = sizeof(struct hvsi_query);
  549. packet.seqno = atomic_inc_return(&hp->seqno);
  550. packet.verb = verb;
  551. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  552. dbg_dump_hex((uint8_t*)&packet, packet.len);
  553. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  554. if (wrote != packet.len) {
  555. printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
  556. wrote);
  557. return -EIO;
  558. }
  559. return 0;
  560. }
  561. static int hvsi_get_mctrl(struct hvsi_struct *hp)
  562. {
  563. int ret;
  564. set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
  565. hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
  566. ret = hvsi_wait(hp, HVSI_OPEN);
  567. if (ret < 0) {
  568. printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
  569. set_state(hp, HVSI_OPEN);
  570. return ret;
  571. }
  572. pr_debug("%s: mctrl 0x%x\n", __FUNCTION__, hp->mctrl);
  573. return 0;
  574. }
  575. /* note that we can only set DTR */
  576. static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
  577. {
  578. struct hvsi_control packet __ALIGNED__;
  579. int wrote;
  580. packet.type = VS_CONTROL_PACKET_HEADER,
  581. packet.seqno = atomic_inc_return(&hp->seqno);
  582. packet.len = sizeof(struct hvsi_control);
  583. packet.verb = VSV_SET_MODEM_CTL;
  584. packet.mask = HVSI_TSDTR;
  585. if (mctrl & TIOCM_DTR)
  586. packet.word = HVSI_TSDTR;
  587. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  588. dbg_dump_hex((uint8_t*)&packet, packet.len);
  589. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  590. if (wrote != packet.len) {
  591. printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
  592. return -EIO;
  593. }
  594. return 0;
  595. }
  596. static void hvsi_drain_input(struct hvsi_struct *hp)
  597. {
  598. uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
  599. unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
  600. while (time_before(end_jiffies, jiffies))
  601. if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
  602. break;
  603. }
  604. static int hvsi_handshake(struct hvsi_struct *hp)
  605. {
  606. int ret;
  607. /*
  608. * We could have a CLOSE or other data waiting for us before we even try
  609. * to open; try to throw it all away so we don't get confused. (CLOSE
  610. * is the first message sent up the pipe when the FSP comes online. We
  611. * need to distinguish between "it came up a while ago and we're the first
  612. * user" and "it was just reset before it saw our handshake packet".)
  613. */
  614. hvsi_drain_input(hp);
  615. set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
  616. ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
  617. if (ret < 0) {
  618. printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
  619. return ret;
  620. }
  621. ret = hvsi_wait(hp, HVSI_OPEN);
  622. if (ret < 0)
  623. return ret;
  624. return 0;
  625. }
  626. static void hvsi_handshaker(void *arg)
  627. {
  628. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  629. if (hvsi_handshake(hp) >= 0)
  630. return;
  631. printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
  632. if (is_console(hp)) {
  633. /*
  634. * ttys will re-attempt the handshake via hvsi_open, but
  635. * the console will not.
  636. */
  637. printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
  638. }
  639. }
  640. static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
  641. {
  642. struct hvsi_data packet __ALIGNED__;
  643. int ret;
  644. BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
  645. packet.type = VS_DATA_PACKET_HEADER;
  646. packet.seqno = atomic_inc_return(&hp->seqno);
  647. packet.len = count + sizeof(struct hvsi_header);
  648. memcpy(&packet.data, buf, count);
  649. ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  650. if (ret == packet.len) {
  651. /* return the number of chars written, not the packet length */
  652. return count;
  653. }
  654. return ret; /* return any errors */
  655. }
  656. static void hvsi_close_protocol(struct hvsi_struct *hp)
  657. {
  658. struct hvsi_control packet __ALIGNED__;
  659. packet.type = VS_CONTROL_PACKET_HEADER;
  660. packet.seqno = atomic_inc_return(&hp->seqno);
  661. packet.len = 6;
  662. packet.verb = VSV_CLOSE_PROTOCOL;
  663. pr_debug("%s: sending %i bytes\n", __FUNCTION__, packet.len);
  664. dbg_dump_hex((uint8_t*)&packet, packet.len);
  665. hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
  666. }
  667. static int hvsi_open(struct tty_struct *tty, struct file *filp)
  668. {
  669. struct hvsi_struct *hp;
  670. unsigned long flags;
  671. int line = tty->index;
  672. int ret;
  673. pr_debug("%s\n", __FUNCTION__);
  674. if (line < 0 || line >= hvsi_count)
  675. return -ENODEV;
  676. hp = &hvsi_ports[line];
  677. tty->driver_data = hp;
  678. tty->low_latency = 1; /* avoid throttle/tty_flip_buffer_push race */
  679. mb();
  680. if (hp->state == HVSI_FSP_DIED)
  681. return -EIO;
  682. spin_lock_irqsave(&hp->lock, flags);
  683. hp->tty = tty;
  684. hp->count++;
  685. atomic_set(&hp->seqno, 0);
  686. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  687. spin_unlock_irqrestore(&hp->lock, flags);
  688. if (is_console(hp))
  689. return 0; /* this has already been handshaked as the console */
  690. ret = hvsi_handshake(hp);
  691. if (ret < 0) {
  692. printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
  693. return ret;
  694. }
  695. ret = hvsi_get_mctrl(hp);
  696. if (ret < 0) {
  697. printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
  698. return ret;
  699. }
  700. ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
  701. if (ret < 0) {
  702. printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
  703. return ret;
  704. }
  705. return 0;
  706. }
  707. /* wait for hvsi_write_worker to empty hp->outbuf */
  708. static void hvsi_flush_output(struct hvsi_struct *hp)
  709. {
  710. wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
  711. /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
  712. cancel_delayed_work(&hp->writer);
  713. flush_scheduled_work();
  714. /*
  715. * it's also possible that our timeout expired and hvsi_write_worker
  716. * didn't manage to push outbuf. poof.
  717. */
  718. hp->n_outbuf = 0;
  719. }
  720. static void hvsi_close(struct tty_struct *tty, struct file *filp)
  721. {
  722. struct hvsi_struct *hp = tty->driver_data;
  723. unsigned long flags;
  724. pr_debug("%s\n", __FUNCTION__);
  725. if (tty_hung_up_p(filp))
  726. return;
  727. spin_lock_irqsave(&hp->lock, flags);
  728. if (--hp->count == 0) {
  729. hp->tty = NULL;
  730. hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
  731. /* only close down connection if it is not the console */
  732. if (!is_console(hp)) {
  733. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
  734. __set_state(hp, HVSI_CLOSED);
  735. /*
  736. * any data delivered to the tty layer after this will be
  737. * discarded (except for XON/XOFF)
  738. */
  739. tty->closing = 1;
  740. spin_unlock_irqrestore(&hp->lock, flags);
  741. /* let any existing irq handlers finish. no more will start. */
  742. synchronize_irq(hp->virq);
  743. /* hvsi_write_worker will re-schedule until outbuf is empty. */
  744. hvsi_flush_output(hp);
  745. /* tell FSP to stop sending data */
  746. hvsi_close_protocol(hp);
  747. /*
  748. * drain anything FSP is still in the middle of sending, and let
  749. * hvsi_handshake drain the rest on the next open.
  750. */
  751. hvsi_drain_input(hp);
  752. spin_lock_irqsave(&hp->lock, flags);
  753. }
  754. } else if (hp->count < 0)
  755. printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
  756. hp - hvsi_ports, hp->count);
  757. spin_unlock_irqrestore(&hp->lock, flags);
  758. }
  759. static void hvsi_hangup(struct tty_struct *tty)
  760. {
  761. struct hvsi_struct *hp = tty->driver_data;
  762. unsigned long flags;
  763. pr_debug("%s\n", __FUNCTION__);
  764. spin_lock_irqsave(&hp->lock, flags);
  765. hp->count = 0;
  766. hp->n_outbuf = 0;
  767. hp->tty = NULL;
  768. spin_unlock_irqrestore(&hp->lock, flags);
  769. }
  770. /* called with hp->lock held */
  771. static void hvsi_push(struct hvsi_struct *hp)
  772. {
  773. int n;
  774. if (hp->n_outbuf <= 0)
  775. return;
  776. n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
  777. if (n > 0) {
  778. /* success */
  779. pr_debug("%s: wrote %i chars\n", __FUNCTION__, n);
  780. hp->n_outbuf = 0;
  781. } else if (n == -EIO) {
  782. __set_state(hp, HVSI_FSP_DIED);
  783. printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
  784. }
  785. }
  786. /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
  787. static void hvsi_write_worker(void *arg)
  788. {
  789. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  790. unsigned long flags;
  791. #ifdef DEBUG
  792. static long start_j = 0;
  793. if (start_j == 0)
  794. start_j = jiffies;
  795. #endif /* DEBUG */
  796. spin_lock_irqsave(&hp->lock, flags);
  797. pr_debug("%s: %i chars in buffer\n", __FUNCTION__, hp->n_outbuf);
  798. if (!is_open(hp)) {
  799. /*
  800. * We could have a non-open connection if the service processor died
  801. * while we were busily scheduling ourselves. In that case, it could
  802. * be minutes before the service processor comes back, so only try
  803. * again once a second.
  804. */
  805. schedule_delayed_work(&hp->writer, HZ);
  806. goto out;
  807. }
  808. hvsi_push(hp);
  809. if (hp->n_outbuf > 0)
  810. schedule_delayed_work(&hp->writer, 10);
  811. else {
  812. #ifdef DEBUG
  813. pr_debug("%s: outbuf emptied after %li jiffies\n", __FUNCTION__,
  814. jiffies - start_j);
  815. start_j = 0;
  816. #endif /* DEBUG */
  817. wake_up_all(&hp->emptyq);
  818. if (test_bit(TTY_DO_WRITE_WAKEUP, &hp->tty->flags)
  819. && hp->tty->ldisc.write_wakeup)
  820. hp->tty->ldisc.write_wakeup(hp->tty);
  821. wake_up_interruptible(&hp->tty->write_wait);
  822. }
  823. out:
  824. spin_unlock_irqrestore(&hp->lock, flags);
  825. }
  826. static int hvsi_write_room(struct tty_struct *tty)
  827. {
  828. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  829. return N_OUTBUF - hp->n_outbuf;
  830. }
  831. static int hvsi_chars_in_buffer(struct tty_struct *tty)
  832. {
  833. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  834. return hp->n_outbuf;
  835. }
  836. static int hvsi_write(struct tty_struct *tty,
  837. const unsigned char *buf, int count)
  838. {
  839. struct hvsi_struct *hp = tty->driver_data;
  840. const char *source = buf;
  841. unsigned long flags;
  842. int total = 0;
  843. int origcount = count;
  844. spin_lock_irqsave(&hp->lock, flags);
  845. pr_debug("%s: %i chars in buffer\n", __FUNCTION__, hp->n_outbuf);
  846. if (!is_open(hp)) {
  847. /* we're either closing or not yet open; don't accept data */
  848. pr_debug("%s: not open\n", __FUNCTION__);
  849. goto out;
  850. }
  851. /*
  852. * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
  853. * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
  854. * will see there is no room in outbuf and return.
  855. */
  856. while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
  857. int chunksize = min(count, hvsi_write_room(hp->tty));
  858. BUG_ON(hp->n_outbuf < 0);
  859. memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
  860. hp->n_outbuf += chunksize;
  861. total += chunksize;
  862. source += chunksize;
  863. count -= chunksize;
  864. hvsi_push(hp);
  865. }
  866. if (hp->n_outbuf > 0) {
  867. /*
  868. * we weren't able to write it all to the hypervisor.
  869. * schedule another push attempt.
  870. */
  871. schedule_delayed_work(&hp->writer, 10);
  872. }
  873. out:
  874. spin_unlock_irqrestore(&hp->lock, flags);
  875. if (total != origcount)
  876. pr_debug("%s: wanted %i, only wrote %i\n", __FUNCTION__, origcount,
  877. total);
  878. return total;
  879. }
  880. /*
  881. * I have never seen throttle or unthrottle called, so this little throttle
  882. * buffering scheme may or may not work.
  883. */
  884. static void hvsi_throttle(struct tty_struct *tty)
  885. {
  886. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  887. pr_debug("%s\n", __FUNCTION__);
  888. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
  889. }
  890. static void hvsi_unthrottle(struct tty_struct *tty)
  891. {
  892. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  893. unsigned long flags;
  894. int shouldflip = 0;
  895. pr_debug("%s\n", __FUNCTION__);
  896. spin_lock_irqsave(&hp->lock, flags);
  897. if (hp->n_throttle) {
  898. hvsi_send_overflow(hp);
  899. shouldflip = 1;
  900. }
  901. spin_unlock_irqrestore(&hp->lock, flags);
  902. if (shouldflip)
  903. tty_flip_buffer_push(hp->tty);
  904. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  905. }
  906. static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
  907. {
  908. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  909. hvsi_get_mctrl(hp);
  910. return hp->mctrl;
  911. }
  912. static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
  913. unsigned int set, unsigned int clear)
  914. {
  915. struct hvsi_struct *hp = (struct hvsi_struct *)tty->driver_data;
  916. unsigned long flags;
  917. uint16_t new_mctrl;
  918. /* we can only alter DTR */
  919. clear &= TIOCM_DTR;
  920. set &= TIOCM_DTR;
  921. spin_lock_irqsave(&hp->lock, flags);
  922. new_mctrl = (hp->mctrl & ~clear) | set;
  923. if (hp->mctrl != new_mctrl) {
  924. hvsi_set_mctrl(hp, new_mctrl);
  925. hp->mctrl = new_mctrl;
  926. }
  927. spin_unlock_irqrestore(&hp->lock, flags);
  928. return 0;
  929. }
  930. static struct tty_operations hvsi_ops = {
  931. .open = hvsi_open,
  932. .close = hvsi_close,
  933. .write = hvsi_write,
  934. .hangup = hvsi_hangup,
  935. .write_room = hvsi_write_room,
  936. .chars_in_buffer = hvsi_chars_in_buffer,
  937. .throttle = hvsi_throttle,
  938. .unthrottle = hvsi_unthrottle,
  939. .tiocmget = hvsi_tiocmget,
  940. .tiocmset = hvsi_tiocmset,
  941. };
  942. static int __init hvsi_init(void)
  943. {
  944. int i;
  945. hvsi_driver = alloc_tty_driver(hvsi_count);
  946. if (!hvsi_driver)
  947. return -ENOMEM;
  948. hvsi_driver->owner = THIS_MODULE;
  949. hvsi_driver->devfs_name = "hvsi/";
  950. hvsi_driver->driver_name = "hvsi";
  951. hvsi_driver->name = "hvsi";
  952. hvsi_driver->major = HVSI_MAJOR;
  953. hvsi_driver->minor_start = HVSI_MINOR;
  954. hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  955. hvsi_driver->init_termios = tty_std_termios;
  956. hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
  957. hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
  958. tty_set_operations(hvsi_driver, &hvsi_ops);
  959. for (i=0; i < hvsi_count; i++) {
  960. struct hvsi_struct *hp = &hvsi_ports[i];
  961. int ret = 1;
  962. ret = request_irq(hp->virq, hvsi_interrupt, SA_INTERRUPT, "hvsi", hp);
  963. if (ret)
  964. printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
  965. hp->virq, ret);
  966. }
  967. hvsi_wait = wait_for_state; /* irqs active now */
  968. if (tty_register_driver(hvsi_driver))
  969. panic("Couldn't register hvsi console driver\n");
  970. printk(KERN_INFO "HVSI: registered %i devices\n", hvsi_count);
  971. return 0;
  972. }
  973. device_initcall(hvsi_init);
  974. /***** console (not tty) code: *****/
  975. static void hvsi_console_print(struct console *console, const char *buf,
  976. unsigned int count)
  977. {
  978. struct hvsi_struct *hp = &hvsi_ports[console->index];
  979. char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
  980. unsigned int i = 0, n = 0;
  981. int ret, donecr = 0;
  982. mb();
  983. if (!is_open(hp))
  984. return;
  985. /*
  986. * ugh, we have to translate LF -> CRLF ourselves, in place.
  987. * copied from hvc_console.c:
  988. */
  989. while (count > 0 || i > 0) {
  990. if (count > 0 && i < sizeof(c)) {
  991. if (buf[n] == '\n' && !donecr) {
  992. c[i++] = '\r';
  993. donecr = 1;
  994. } else {
  995. c[i++] = buf[n++];
  996. donecr = 0;
  997. --count;
  998. }
  999. } else {
  1000. ret = hvsi_put_chars(hp, c, i);
  1001. if (ret < 0)
  1002. i = 0;
  1003. i -= ret;
  1004. }
  1005. }
  1006. }
  1007. static struct tty_driver *hvsi_console_device(struct console *console,
  1008. int *index)
  1009. {
  1010. *index = console->index;
  1011. return hvsi_driver;
  1012. }
  1013. static int __init hvsi_console_setup(struct console *console, char *options)
  1014. {
  1015. struct hvsi_struct *hp = &hvsi_ports[console->index];
  1016. int ret;
  1017. if (console->index < 0 || console->index >= hvsi_count)
  1018. return -1;
  1019. /* give the FSP a chance to change the baud rate when we re-open */
  1020. hvsi_close_protocol(hp);
  1021. ret = hvsi_handshake(hp);
  1022. if (ret < 0)
  1023. return ret;
  1024. ret = hvsi_get_mctrl(hp);
  1025. if (ret < 0)
  1026. return ret;
  1027. ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
  1028. if (ret < 0)
  1029. return ret;
  1030. hp->flags |= HVSI_CONSOLE;
  1031. return 0;
  1032. }
  1033. static struct console hvsi_con_driver = {
  1034. .name = "hvsi",
  1035. .write = hvsi_console_print,
  1036. .device = hvsi_console_device,
  1037. .setup = hvsi_console_setup,
  1038. .flags = CON_PRINTBUFFER,
  1039. .index = -1,
  1040. };
  1041. static int __init hvsi_console_init(void)
  1042. {
  1043. struct device_node *vty;
  1044. hvsi_wait = poll_for_state; /* no irqs yet; must poll */
  1045. /* search device tree for vty nodes */
  1046. for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
  1047. vty != NULL;
  1048. vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
  1049. struct hvsi_struct *hp;
  1050. uint32_t *vtermno;
  1051. uint32_t *irq;
  1052. vtermno = (uint32_t *)get_property(vty, "reg", NULL);
  1053. irq = (uint32_t *)get_property(vty, "interrupts", NULL);
  1054. if (!vtermno || !irq)
  1055. continue;
  1056. if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
  1057. of_node_put(vty);
  1058. break;
  1059. }
  1060. hp = &hvsi_ports[hvsi_count];
  1061. INIT_WORK(&hp->writer, hvsi_write_worker, hp);
  1062. INIT_WORK(&hp->handshaker, hvsi_handshaker, hp);
  1063. init_waitqueue_head(&hp->emptyq);
  1064. init_waitqueue_head(&hp->stateq);
  1065. spin_lock_init(&hp->lock);
  1066. hp->index = hvsi_count;
  1067. hp->inbuf_end = hp->inbuf;
  1068. hp->state = HVSI_CLOSED;
  1069. hp->vtermno = *vtermno;
  1070. hp->virq = virt_irq_create_mapping(irq[0]);
  1071. if (hp->virq == NO_IRQ) {
  1072. printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
  1073. __FUNCTION__, hp->virq);
  1074. continue;
  1075. } else
  1076. hp->virq = irq_offset_up(hp->virq);
  1077. hvsi_count++;
  1078. }
  1079. if (hvsi_count)
  1080. register_console(&hvsi_con_driver);
  1081. return 0;
  1082. }
  1083. console_initcall(hvsi_console_init);