hvsi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2004 Hollis Blanchard <[email protected]>, 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 int hvsi_write(struct tty_struct *tty,
  742. const unsigned char *buf, int count)
  743. {
  744. struct hvsi_struct *hp = tty->driver_data;
  745. const char *source = buf;
  746. unsigned long flags;
  747. int total = 0;
  748. int origcount = count;
  749. spin_lock_irqsave(&hp->lock, flags);
  750. pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
  751. if (!is_open(hp)) {
  752. /* we're either closing or not yet open; don't accept data */
  753. pr_debug("%s: not open\n", __func__);
  754. goto out;
  755. }
  756. /*
  757. * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
  758. * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
  759. * will see there is no room in outbuf and return.
  760. */
  761. while ((count > 0) && (hvsi_write_room(tty) > 0)) {
  762. int chunksize = min_t(int, count, hvsi_write_room(tty));
  763. BUG_ON(hp->n_outbuf < 0);
  764. memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
  765. hp->n_outbuf += chunksize;
  766. total += chunksize;
  767. source += chunksize;
  768. count -= chunksize;
  769. hvsi_push(hp);
  770. }
  771. if (hp->n_outbuf > 0) {
  772. /*
  773. * we weren't able to write it all to the hypervisor.
  774. * schedule another push attempt.
  775. */
  776. schedule_delayed_work(&hp->writer, 10);
  777. }
  778. out:
  779. spin_unlock_irqrestore(&hp->lock, flags);
  780. if (total != origcount)
  781. pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
  782. total);
  783. return total;
  784. }
  785. /*
  786. * I have never seen throttle or unthrottle called, so this little throttle
  787. * buffering scheme may or may not work.
  788. */
  789. static void hvsi_throttle(struct tty_struct *tty)
  790. {
  791. struct hvsi_struct *hp = tty->driver_data;
  792. pr_debug("%s\n", __func__);
  793. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
  794. }
  795. static void hvsi_unthrottle(struct tty_struct *tty)
  796. {
  797. struct hvsi_struct *hp = tty->driver_data;
  798. unsigned long flags;
  799. pr_debug("%s\n", __func__);
  800. spin_lock_irqsave(&hp->lock, flags);
  801. if (hp->n_throttle) {
  802. hvsi_send_overflow(hp);
  803. tty_flip_buffer_push(&hp->port);
  804. }
  805. spin_unlock_irqrestore(&hp->lock, flags);
  806. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  807. }
  808. static int hvsi_tiocmget(struct tty_struct *tty)
  809. {
  810. struct hvsi_struct *hp = tty->driver_data;
  811. hvsi_get_mctrl(hp);
  812. return hp->mctrl;
  813. }
  814. static int hvsi_tiocmset(struct tty_struct *tty,
  815. unsigned int set, unsigned int clear)
  816. {
  817. struct hvsi_struct *hp = tty->driver_data;
  818. unsigned long flags;
  819. uint16_t new_mctrl;
  820. /* we can only alter DTR */
  821. clear &= TIOCM_DTR;
  822. set &= TIOCM_DTR;
  823. spin_lock_irqsave(&hp->lock, flags);
  824. new_mctrl = (hp->mctrl & ~clear) | set;
  825. if (hp->mctrl != new_mctrl) {
  826. hvsi_set_mctrl(hp, new_mctrl);
  827. hp->mctrl = new_mctrl;
  828. }
  829. spin_unlock_irqrestore(&hp->lock, flags);
  830. return 0;
  831. }
  832. static const struct tty_operations hvsi_ops = {
  833. .open = hvsi_open,
  834. .close = hvsi_close,
  835. .write = hvsi_write,
  836. .hangup = hvsi_hangup,
  837. .write_room = hvsi_write_room,
  838. .chars_in_buffer = hvsi_chars_in_buffer,
  839. .throttle = hvsi_throttle,
  840. .unthrottle = hvsi_unthrottle,
  841. .tiocmget = hvsi_tiocmget,
  842. .tiocmset = hvsi_tiocmset,
  843. };
  844. static int __init hvsi_init(void)
  845. {
  846. struct tty_driver *driver;
  847. int i, ret;
  848. driver = tty_alloc_driver(hvsi_count, TTY_DRIVER_REAL_RAW);
  849. if (IS_ERR(driver))
  850. return PTR_ERR(driver);
  851. driver->driver_name = "hvsi";
  852. driver->name = "hvsi";
  853. driver->major = HVSI_MAJOR;
  854. driver->minor_start = HVSI_MINOR;
  855. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  856. driver->init_termios = tty_std_termios;
  857. driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
  858. driver->init_termios.c_ispeed = 9600;
  859. driver->init_termios.c_ospeed = 9600;
  860. tty_set_operations(driver, &hvsi_ops);
  861. for (i=0; i < hvsi_count; i++) {
  862. struct hvsi_struct *hp = &hvsi_ports[i];
  863. int ret = 1;
  864. tty_port_link_device(&hp->port, driver, i);
  865. ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
  866. if (ret)
  867. printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
  868. hp->virq, ret);
  869. }
  870. hvsi_wait = wait_for_state; /* irqs active now */
  871. ret = tty_register_driver(driver);
  872. if (ret) {
  873. pr_err("Couldn't register hvsi console driver\n");
  874. goto err_free_irq;
  875. }
  876. hvsi_driver = driver;
  877. printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
  878. return 0;
  879. err_free_irq:
  880. hvsi_wait = poll_for_state;
  881. for (i = 0; i < hvsi_count; i++) {
  882. struct hvsi_struct *hp = &hvsi_ports[i];
  883. free_irq(hp->virq, hp);
  884. }
  885. tty_driver_kref_put(driver);
  886. return ret;
  887. }
  888. device_initcall(hvsi_init);
  889. /***** console (not tty) code: *****/
  890. static void hvsi_console_print(struct console *console, const char *buf,
  891. unsigned int count)
  892. {
  893. struct hvsi_struct *hp = &hvsi_ports[console->index];
  894. char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
  895. unsigned int i = 0, n = 0;
  896. int ret, donecr = 0;
  897. mb();
  898. if (!is_open(hp))
  899. return;
  900. /*
  901. * ugh, we have to translate LF -> CRLF ourselves, in place.
  902. * copied from hvc_console.c:
  903. */
  904. while (count > 0 || i > 0) {
  905. if (count > 0 && i < sizeof(c)) {
  906. if (buf[n] == '\n' && !donecr) {
  907. c[i++] = '\r';
  908. donecr = 1;
  909. } else {
  910. c[i++] = buf[n++];
  911. donecr = 0;
  912. --count;
  913. }
  914. } else {
  915. ret = hvsi_put_chars(hp, c, i);
  916. if (ret < 0)
  917. i = 0;
  918. i -= ret;
  919. }
  920. }
  921. }
  922. static struct tty_driver *hvsi_console_device(struct console *console,
  923. int *index)
  924. {
  925. *index = console->index;
  926. return hvsi_driver;
  927. }
  928. static int __init hvsi_console_setup(struct console *console, char *options)
  929. {
  930. struct hvsi_struct *hp;
  931. int ret;
  932. if (console->index < 0 || console->index >= hvsi_count)
  933. return -EINVAL;
  934. hp = &hvsi_ports[console->index];
  935. /* give the FSP a chance to change the baud rate when we re-open */
  936. hvsi_close_protocol(hp);
  937. ret = hvsi_handshake(hp);
  938. if (ret < 0)
  939. return ret;
  940. ret = hvsi_get_mctrl(hp);
  941. if (ret < 0)
  942. return ret;
  943. ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
  944. if (ret < 0)
  945. return ret;
  946. hp->flags |= HVSI_CONSOLE;
  947. return 0;
  948. }
  949. static struct console hvsi_console = {
  950. .name = "hvsi",
  951. .write = hvsi_console_print,
  952. .device = hvsi_console_device,
  953. .setup = hvsi_console_setup,
  954. .flags = CON_PRINTBUFFER,
  955. .index = -1,
  956. };
  957. static int __init hvsi_console_init(void)
  958. {
  959. struct device_node *vty;
  960. hvsi_wait = poll_for_state; /* no irqs yet; must poll */
  961. /* search device tree for vty nodes */
  962. for_each_compatible_node(vty, "serial", "hvterm-protocol") {
  963. struct hvsi_struct *hp;
  964. const __be32 *vtermno, *irq;
  965. vtermno = of_get_property(vty, "reg", NULL);
  966. irq = of_get_property(vty, "interrupts", NULL);
  967. if (!vtermno || !irq)
  968. continue;
  969. if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
  970. of_node_put(vty);
  971. break;
  972. }
  973. hp = &hvsi_ports[hvsi_count];
  974. INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
  975. INIT_WORK(&hp->handshaker, hvsi_handshaker);
  976. init_waitqueue_head(&hp->emptyq);
  977. init_waitqueue_head(&hp->stateq);
  978. spin_lock_init(&hp->lock);
  979. tty_port_init(&hp->port);
  980. hp->index = hvsi_count;
  981. hp->inbuf_end = hp->inbuf;
  982. hp->state = HVSI_CLOSED;
  983. hp->vtermno = be32_to_cpup(vtermno);
  984. hp->virq = irq_create_mapping(NULL, be32_to_cpup(irq));
  985. if (hp->virq == 0) {
  986. printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
  987. __func__, be32_to_cpup(irq));
  988. tty_port_destroy(&hp->port);
  989. continue;
  990. }
  991. hvsi_count++;
  992. }
  993. if (hvsi_count)
  994. register_console(&hvsi_console);
  995. return 0;
  996. }
  997. console_initcall(hvsi_console_init);