hvsi.c 30 KB

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