hvsi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 (header->verb) {
  207. case VSV_MODEM_CTL_UPDATE:
  208. if ((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. switch (hp->state) {
  233. case HVSI_WAIT_FOR_VER_RESPONSE:
  234. __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
  235. break;
  236. case HVSI_WAIT_FOR_MCTRL_RESPONSE:
  237. hp->mctrl = 0;
  238. if (resp->u.mctrl_word & HVSI_TSDTR)
  239. hp->mctrl |= TIOCM_DTR;
  240. if (resp->u.mctrl_word & HVSI_TSCD)
  241. hp->mctrl |= TIOCM_CD;
  242. __set_state(hp, HVSI_OPEN);
  243. break;
  244. default:
  245. printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
  246. dump_packet(packet);
  247. break;
  248. }
  249. }
  250. /* respond to service processor's version query */
  251. static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
  252. {
  253. struct hvsi_query_response packet __ALIGNED__;
  254. int wrote;
  255. packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
  256. packet.hdr.len = sizeof(struct hvsi_query_response);
  257. packet.hdr.seqno = atomic_inc_return(&hp->seqno);
  258. packet.verb = VSV_SEND_VERSION_NUMBER;
  259. packet.u.version = HVSI_VERSION;
  260. packet.query_seqno = query_seqno+1;
  261. pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
  262. dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
  263. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
  264. if (wrote != packet.hdr.len) {
  265. printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
  266. hp->index);
  267. return -EIO;
  268. }
  269. return 0;
  270. }
  271. static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
  272. {
  273. struct hvsi_query *query = (struct hvsi_query *)packet;
  274. switch (hp->state) {
  275. case HVSI_WAIT_FOR_VER_QUERY:
  276. hvsi_version_respond(hp, query->hdr.seqno);
  277. __set_state(hp, HVSI_OPEN);
  278. break;
  279. default:
  280. printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
  281. dump_packet(packet);
  282. break;
  283. }
  284. }
  285. static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
  286. {
  287. int i;
  288. for (i=0; i < len; i++) {
  289. char c = buf[i];
  290. #ifdef CONFIG_MAGIC_SYSRQ
  291. if (c == '\0') {
  292. hp->sysrq = 1;
  293. continue;
  294. } else if (hp->sysrq) {
  295. handle_sysrq(c);
  296. hp->sysrq = 0;
  297. continue;
  298. }
  299. #endif /* CONFIG_MAGIC_SYSRQ */
  300. tty_insert_flip_char(&hp->port, c, 0);
  301. }
  302. }
  303. /*
  304. * We could get 252 bytes of data at once here. But the tty layer only
  305. * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
  306. * it. Accordingly we won't send more than 128 bytes at a time to the flip
  307. * buffer, which will give the tty buffer a chance to throttle us. Should the
  308. * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
  309. * revisited.
  310. */
  311. #define TTY_THRESHOLD_THROTTLE 128
  312. static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
  313. {
  314. const struct hvsi_header *header = (const struct hvsi_header *)packet;
  315. const uint8_t *data = packet + sizeof(struct hvsi_header);
  316. int datalen = header->len - sizeof(struct hvsi_header);
  317. int overflow = datalen - TTY_THRESHOLD_THROTTLE;
  318. pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
  319. if (datalen == 0)
  320. return false;
  321. if (overflow > 0) {
  322. pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
  323. datalen = TTY_THRESHOLD_THROTTLE;
  324. }
  325. hvsi_insert_chars(hp, data, datalen);
  326. if (overflow > 0) {
  327. /*
  328. * we still have more data to deliver, so we need to save off the
  329. * overflow and send it later
  330. */
  331. pr_debug("%s: deferring overflow\n", __func__);
  332. memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
  333. hp->n_throttle = overflow;
  334. }
  335. return true;
  336. }
  337. /*
  338. * Returns true/false indicating data successfully read from hypervisor.
  339. * Used both to get packets for tty connections and to advance the state
  340. * machine during console handshaking (in which case tty = NULL and we ignore
  341. * incoming data).
  342. */
  343. static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
  344. struct hvsi_struct **handshake)
  345. {
  346. uint8_t *packet = hp->inbuf;
  347. int chunklen;
  348. bool flip = false;
  349. *handshake = NULL;
  350. chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
  351. if (chunklen == 0) {
  352. pr_debug("%s: 0-length read\n", __func__);
  353. return 0;
  354. }
  355. pr_debug("%s: got %i bytes\n", __func__, chunklen);
  356. dbg_dump_hex(hp->inbuf_end, chunklen);
  357. hp->inbuf_end += chunklen;
  358. /* handle all completed packets */
  359. while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
  360. struct hvsi_header *header = (struct hvsi_header *)packet;
  361. if (!is_header(packet)) {
  362. printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
  363. /* skip bytes until we find a header or run out of data */
  364. while ((packet < hp->inbuf_end) && (!is_header(packet)))
  365. packet++;
  366. continue;
  367. }
  368. pr_debug("%s: handling %i-byte packet\n", __func__,
  369. len_packet(packet));
  370. dbg_dump_packet(packet);
  371. switch (header->type) {
  372. case VS_DATA_PACKET_HEADER:
  373. if (!is_open(hp))
  374. break;
  375. flip = hvsi_recv_data(hp, packet);
  376. break;
  377. case VS_CONTROL_PACKET_HEADER:
  378. hvsi_recv_control(hp, packet, tty, handshake);
  379. break;
  380. case VS_QUERY_RESPONSE_PACKET_HEADER:
  381. hvsi_recv_response(hp, packet);
  382. break;
  383. case VS_QUERY_PACKET_HEADER:
  384. hvsi_recv_query(hp, packet);
  385. break;
  386. default:
  387. printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
  388. hp->index, header->type);
  389. dump_packet(packet);
  390. break;
  391. }
  392. packet += len_packet(packet);
  393. if (*handshake) {
  394. pr_debug("%s: handshake\n", __func__);
  395. break;
  396. }
  397. }
  398. compact_inbuf(hp, packet);
  399. if (flip)
  400. tty_flip_buffer_push(&hp->port);
  401. return 1;
  402. }
  403. static void hvsi_send_overflow(struct hvsi_struct *hp)
  404. {
  405. pr_debug("%s: delivering %i bytes overflow\n", __func__,
  406. hp->n_throttle);
  407. hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
  408. hp->n_throttle = 0;
  409. }
  410. /*
  411. * must get all pending data because we only get an irq on empty->non-empty
  412. * transition
  413. */
  414. static irqreturn_t hvsi_interrupt(int irq, void *arg)
  415. {
  416. struct hvsi_struct *hp = (struct hvsi_struct *)arg;
  417. struct hvsi_struct *handshake;
  418. struct tty_struct *tty;
  419. unsigned long flags;
  420. int again = 1;
  421. pr_debug("%s\n", __func__);
  422. tty = tty_port_tty_get(&hp->port);
  423. while (again) {
  424. spin_lock_irqsave(&hp->lock, flags);
  425. again = hvsi_load_chunk(hp, tty, &handshake);
  426. spin_unlock_irqrestore(&hp->lock, flags);
  427. if (handshake) {
  428. pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
  429. schedule_work(&handshake->handshaker);
  430. }
  431. }
  432. spin_lock_irqsave(&hp->lock, flags);
  433. if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) {
  434. /* we weren't hung up and we weren't throttled, so we can
  435. * deliver the rest now */
  436. hvsi_send_overflow(hp);
  437. tty_flip_buffer_push(&hp->port);
  438. }
  439. spin_unlock_irqrestore(&hp->lock, flags);
  440. tty_kref_put(tty);
  441. return IRQ_HANDLED;
  442. }
  443. /* for boot console, before the irq handler is running */
  444. static int __init poll_for_state(struct hvsi_struct *hp, int state)
  445. {
  446. unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
  447. for (;;) {
  448. hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
  449. if (hp->state == state)
  450. return 0;
  451. mdelay(5);
  452. if (time_after(jiffies, end_jiffies))
  453. return -EIO;
  454. }
  455. }
  456. /* wait for irq handler to change our state */
  457. static int wait_for_state(struct hvsi_struct *hp, int state)
  458. {
  459. int ret = 0;
  460. if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
  461. ret = -EIO;
  462. return ret;
  463. }
  464. static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
  465. {
  466. struct hvsi_query packet __ALIGNED__;
  467. int wrote;
  468. packet.hdr.type = VS_QUERY_PACKET_HEADER;
  469. packet.hdr.len = sizeof(struct hvsi_query);
  470. packet.hdr.seqno = atomic_inc_return(&hp->seqno);
  471. packet.verb = verb;
  472. pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
  473. dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
  474. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
  475. if (wrote != packet.hdr.len) {
  476. printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
  477. wrote);
  478. return -EIO;
  479. }
  480. return 0;
  481. }
  482. static int hvsi_get_mctrl(struct hvsi_struct *hp)
  483. {
  484. int ret;
  485. set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
  486. hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
  487. ret = hvsi_wait(hp, HVSI_OPEN);
  488. if (ret < 0) {
  489. printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
  490. set_state(hp, HVSI_OPEN);
  491. return ret;
  492. }
  493. pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
  494. return 0;
  495. }
  496. /* note that we can only set DTR */
  497. static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
  498. {
  499. struct hvsi_control packet __ALIGNED__;
  500. int wrote;
  501. packet.hdr.type = VS_CONTROL_PACKET_HEADER,
  502. packet.hdr.seqno = atomic_inc_return(&hp->seqno);
  503. packet.hdr.len = sizeof(struct hvsi_control);
  504. packet.verb = VSV_SET_MODEM_CTL;
  505. packet.mask = HVSI_TSDTR;
  506. if (mctrl & TIOCM_DTR)
  507. packet.word = HVSI_TSDTR;
  508. pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
  509. dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
  510. wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
  511. if (wrote != packet.hdr.len) {
  512. printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
  513. return -EIO;
  514. }
  515. return 0;
  516. }
  517. static void hvsi_drain_input(struct hvsi_struct *hp)
  518. {
  519. uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
  520. unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
  521. while (time_before(end_jiffies, jiffies))
  522. if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
  523. break;
  524. }
  525. static int hvsi_handshake(struct hvsi_struct *hp)
  526. {
  527. int ret;
  528. /*
  529. * We could have a CLOSE or other data waiting for us before we even try
  530. * to open; try to throw it all away so we don't get confused. (CLOSE
  531. * is the first message sent up the pipe when the FSP comes online. We
  532. * need to distinguish between "it came up a while ago and we're the first
  533. * user" and "it was just reset before it saw our handshake packet".)
  534. */
  535. hvsi_drain_input(hp);
  536. set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
  537. ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
  538. if (ret < 0) {
  539. printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
  540. return ret;
  541. }
  542. ret = hvsi_wait(hp, HVSI_OPEN);
  543. if (ret < 0)
  544. return ret;
  545. return 0;
  546. }
  547. static void hvsi_handshaker(struct work_struct *work)
  548. {
  549. struct hvsi_struct *hp =
  550. container_of(work, struct hvsi_struct, handshaker);
  551. if (hvsi_handshake(hp) >= 0)
  552. return;
  553. printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
  554. if (is_console(hp)) {
  555. /*
  556. * ttys will re-attempt the handshake via hvsi_open, but
  557. * the console will not.
  558. */
  559. printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
  560. }
  561. }
  562. static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
  563. {
  564. struct hvsi_data packet __ALIGNED__;
  565. int ret;
  566. BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
  567. packet.hdr.type = VS_DATA_PACKET_HEADER;
  568. packet.hdr.seqno = atomic_inc_return(&hp->seqno);
  569. packet.hdr.len = count + sizeof(struct hvsi_header);
  570. memcpy(&packet.data, buf, count);
  571. ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
  572. if (ret == packet.hdr.len) {
  573. /* return the number of chars written, not the packet length */
  574. return count;
  575. }
  576. return ret; /* return any errors */
  577. }
  578. static void hvsi_close_protocol(struct hvsi_struct *hp)
  579. {
  580. struct hvsi_control packet __ALIGNED__;
  581. packet.hdr.type = VS_CONTROL_PACKET_HEADER;
  582. packet.hdr.seqno = atomic_inc_return(&hp->seqno);
  583. packet.hdr.len = 6;
  584. packet.verb = VSV_CLOSE_PROTOCOL;
  585. pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
  586. dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
  587. hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
  588. }
  589. static int hvsi_open(struct tty_struct *tty, struct file *filp)
  590. {
  591. struct hvsi_struct *hp;
  592. unsigned long flags;
  593. int ret;
  594. pr_debug("%s\n", __func__);
  595. hp = &hvsi_ports[tty->index];
  596. tty->driver_data = hp;
  597. mb();
  598. if (hp->state == HVSI_FSP_DIED)
  599. return -EIO;
  600. tty_port_tty_set(&hp->port, tty);
  601. spin_lock_irqsave(&hp->lock, flags);
  602. hp->port.count++;
  603. atomic_set(&hp->seqno, 0);
  604. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  605. spin_unlock_irqrestore(&hp->lock, flags);
  606. if (is_console(hp))
  607. return 0; /* this has already been handshaked as the console */
  608. ret = hvsi_handshake(hp);
  609. if (ret < 0) {
  610. printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
  611. return ret;
  612. }
  613. ret = hvsi_get_mctrl(hp);
  614. if (ret < 0) {
  615. printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
  616. return ret;
  617. }
  618. ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
  619. if (ret < 0) {
  620. printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
  621. return ret;
  622. }
  623. return 0;
  624. }
  625. /* wait for hvsi_write_worker to empty hp->outbuf */
  626. static void hvsi_flush_output(struct hvsi_struct *hp)
  627. {
  628. wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
  629. /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
  630. cancel_delayed_work_sync(&hp->writer);
  631. flush_work(&hp->handshaker);
  632. /*
  633. * it's also possible that our timeout expired and hvsi_write_worker
  634. * didn't manage to push outbuf. poof.
  635. */
  636. hp->n_outbuf = 0;
  637. }
  638. static void hvsi_close(struct tty_struct *tty, struct file *filp)
  639. {
  640. struct hvsi_struct *hp = tty->driver_data;
  641. unsigned long flags;
  642. pr_debug("%s\n", __func__);
  643. if (tty_hung_up_p(filp))
  644. return;
  645. spin_lock_irqsave(&hp->lock, flags);
  646. if (--hp->port.count == 0) {
  647. tty_port_tty_set(&hp->port, NULL);
  648. hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
  649. /* only close down connection if it is not the console */
  650. if (!is_console(hp)) {
  651. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
  652. __set_state(hp, HVSI_CLOSED);
  653. /*
  654. * any data delivered to the tty layer after this will be
  655. * discarded (except for XON/XOFF)
  656. */
  657. tty->closing = 1;
  658. spin_unlock_irqrestore(&hp->lock, flags);
  659. /* let any existing irq handlers finish. no more will start. */
  660. synchronize_irq(hp->virq);
  661. /* hvsi_write_worker will re-schedule until outbuf is empty. */
  662. hvsi_flush_output(hp);
  663. /* tell FSP to stop sending data */
  664. hvsi_close_protocol(hp);
  665. /*
  666. * drain anything FSP is still in the middle of sending, and let
  667. * hvsi_handshake drain the rest on the next open.
  668. */
  669. hvsi_drain_input(hp);
  670. spin_lock_irqsave(&hp->lock, flags);
  671. }
  672. } else if (hp->port.count < 0)
  673. printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
  674. hp - hvsi_ports, hp->port.count);
  675. spin_unlock_irqrestore(&hp->lock, flags);
  676. }
  677. static void hvsi_hangup(struct tty_struct *tty)
  678. {
  679. struct hvsi_struct *hp = tty->driver_data;
  680. unsigned long flags;
  681. pr_debug("%s\n", __func__);
  682. tty_port_tty_set(&hp->port, NULL);
  683. spin_lock_irqsave(&hp->lock, flags);
  684. hp->port.count = 0;
  685. hp->n_outbuf = 0;
  686. spin_unlock_irqrestore(&hp->lock, flags);
  687. }
  688. /* called with hp->lock held */
  689. static void hvsi_push(struct hvsi_struct *hp)
  690. {
  691. int n;
  692. if (hp->n_outbuf <= 0)
  693. return;
  694. n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
  695. if (n > 0) {
  696. /* success */
  697. pr_debug("%s: wrote %i chars\n", __func__, n);
  698. hp->n_outbuf = 0;
  699. } else if (n == -EIO) {
  700. __set_state(hp, HVSI_FSP_DIED);
  701. printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
  702. }
  703. }
  704. /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
  705. static void hvsi_write_worker(struct work_struct *work)
  706. {
  707. struct hvsi_struct *hp =
  708. container_of(work, struct hvsi_struct, writer.work);
  709. unsigned long flags;
  710. #ifdef DEBUG
  711. static long start_j = 0;
  712. if (start_j == 0)
  713. start_j = jiffies;
  714. #endif /* DEBUG */
  715. spin_lock_irqsave(&hp->lock, flags);
  716. pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
  717. if (!is_open(hp)) {
  718. /*
  719. * We could have a non-open connection if the service processor died
  720. * while we were busily scheduling ourselves. In that case, it could
  721. * be minutes before the service processor comes back, so only try
  722. * again once a second.
  723. */
  724. schedule_delayed_work(&hp->writer, HZ);
  725. goto out;
  726. }
  727. hvsi_push(hp);
  728. if (hp->n_outbuf > 0)
  729. schedule_delayed_work(&hp->writer, 10);
  730. else {
  731. #ifdef DEBUG
  732. pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
  733. jiffies - start_j);
  734. start_j = 0;
  735. #endif /* DEBUG */
  736. wake_up_all(&hp->emptyq);
  737. tty_port_tty_wakeup(&hp->port);
  738. }
  739. out:
  740. spin_unlock_irqrestore(&hp->lock, flags);
  741. }
  742. static int hvsi_write_room(struct tty_struct *tty)
  743. {
  744. struct hvsi_struct *hp = tty->driver_data;
  745. return N_OUTBUF - hp->n_outbuf;
  746. }
  747. static int hvsi_chars_in_buffer(struct tty_struct *tty)
  748. {
  749. struct hvsi_struct *hp = tty->driver_data;
  750. return hp->n_outbuf;
  751. }
  752. static int hvsi_write(struct tty_struct *tty,
  753. const unsigned char *buf, int count)
  754. {
  755. struct hvsi_struct *hp = tty->driver_data;
  756. const char *source = buf;
  757. unsigned long flags;
  758. int total = 0;
  759. int origcount = count;
  760. spin_lock_irqsave(&hp->lock, flags);
  761. pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
  762. if (!is_open(hp)) {
  763. /* we're either closing or not yet open; don't accept data */
  764. pr_debug("%s: not open\n", __func__);
  765. goto out;
  766. }
  767. /*
  768. * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
  769. * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
  770. * will see there is no room in outbuf and return.
  771. */
  772. while ((count > 0) && (hvsi_write_room(tty) > 0)) {
  773. int chunksize = min(count, hvsi_write_room(tty));
  774. BUG_ON(hp->n_outbuf < 0);
  775. memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
  776. hp->n_outbuf += chunksize;
  777. total += chunksize;
  778. source += chunksize;
  779. count -= chunksize;
  780. hvsi_push(hp);
  781. }
  782. if (hp->n_outbuf > 0) {
  783. /*
  784. * we weren't able to write it all to the hypervisor.
  785. * schedule another push attempt.
  786. */
  787. schedule_delayed_work(&hp->writer, 10);
  788. }
  789. out:
  790. spin_unlock_irqrestore(&hp->lock, flags);
  791. if (total != origcount)
  792. pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
  793. total);
  794. return total;
  795. }
  796. /*
  797. * I have never seen throttle or unthrottle called, so this little throttle
  798. * buffering scheme may or may not work.
  799. */
  800. static void hvsi_throttle(struct tty_struct *tty)
  801. {
  802. struct hvsi_struct *hp = tty->driver_data;
  803. pr_debug("%s\n", __func__);
  804. h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
  805. }
  806. static void hvsi_unthrottle(struct tty_struct *tty)
  807. {
  808. struct hvsi_struct *hp = tty->driver_data;
  809. unsigned long flags;
  810. pr_debug("%s\n", __func__);
  811. spin_lock_irqsave(&hp->lock, flags);
  812. if (hp->n_throttle) {
  813. hvsi_send_overflow(hp);
  814. tty_flip_buffer_push(&hp->port);
  815. }
  816. spin_unlock_irqrestore(&hp->lock, flags);
  817. h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
  818. }
  819. static int hvsi_tiocmget(struct tty_struct *tty)
  820. {
  821. struct hvsi_struct *hp = tty->driver_data;
  822. hvsi_get_mctrl(hp);
  823. return hp->mctrl;
  824. }
  825. static int hvsi_tiocmset(struct tty_struct *tty,
  826. unsigned int set, unsigned int clear)
  827. {
  828. struct hvsi_struct *hp = tty->driver_data;
  829. unsigned long flags;
  830. uint16_t new_mctrl;
  831. /* we can only alter DTR */
  832. clear &= TIOCM_DTR;
  833. set &= TIOCM_DTR;
  834. spin_lock_irqsave(&hp->lock, flags);
  835. new_mctrl = (hp->mctrl & ~clear) | set;
  836. if (hp->mctrl != new_mctrl) {
  837. hvsi_set_mctrl(hp, new_mctrl);
  838. hp->mctrl = new_mctrl;
  839. }
  840. spin_unlock_irqrestore(&hp->lock, flags);
  841. return 0;
  842. }
  843. static const struct tty_operations hvsi_ops = {
  844. .open = hvsi_open,
  845. .close = hvsi_close,
  846. .write = hvsi_write,
  847. .hangup = hvsi_hangup,
  848. .write_room = hvsi_write_room,
  849. .chars_in_buffer = hvsi_chars_in_buffer,
  850. .throttle = hvsi_throttle,
  851. .unthrottle = hvsi_unthrottle,
  852. .tiocmget = hvsi_tiocmget,
  853. .tiocmset = hvsi_tiocmset,
  854. };
  855. static int __init hvsi_init(void)
  856. {
  857. int i;
  858. hvsi_driver = alloc_tty_driver(hvsi_count);
  859. if (!hvsi_driver)
  860. return -ENOMEM;
  861. hvsi_driver->driver_name = "hvsi";
  862. hvsi_driver->name = "hvsi";
  863. hvsi_driver->major = HVSI_MAJOR;
  864. hvsi_driver->minor_start = HVSI_MINOR;
  865. hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  866. hvsi_driver->init_termios = tty_std_termios;
  867. hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
  868. hvsi_driver->init_termios.c_ispeed = 9600;
  869. hvsi_driver->init_termios.c_ospeed = 9600;
  870. hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
  871. tty_set_operations(hvsi_driver, &hvsi_ops);
  872. for (i=0; i < hvsi_count; i++) {
  873. struct hvsi_struct *hp = &hvsi_ports[i];
  874. int ret = 1;
  875. tty_port_link_device(&hp->port, hvsi_driver, i);
  876. ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
  877. if (ret)
  878. printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
  879. hp->virq, ret);
  880. }
  881. hvsi_wait = wait_for_state; /* irqs active now */
  882. if (tty_register_driver(hvsi_driver))
  883. panic("Couldn't register hvsi console driver\n");
  884. printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
  885. return 0;
  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 -1;
  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 uint32_t *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 = *vtermno;
  983. hp->virq = irq_create_mapping(NULL, irq[0]);
  984. if (hp->virq == 0) {
  985. printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
  986. __func__, irq[0]);
  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);