hvsi.c 30 KB

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