hvsi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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/spinlock.h>
  27. #include <linux/sysrq.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <asm/hvcall.h>
  31. #include <asm/hvconsole.h>
  32. #include <asm/prom.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 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 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(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. int i, ret;
  847. hvsi_driver = alloc_tty_driver(hvsi_count);
  848. if (!hvsi_driver)
  849. return -ENOMEM;
  850. hvsi_driver->driver_name = "hvsi";
  851. hvsi_driver->name = "hvsi";
  852. hvsi_driver->major = HVSI_MAJOR;
  853. hvsi_driver->minor_start = HVSI_MINOR;
  854. hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  855. hvsi_driver->init_termios = tty_std_termios;
  856. hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
  857. hvsi_driver->init_termios.c_ispeed = 9600;
  858. hvsi_driver->init_termios.c_ospeed = 9600;
  859. hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
  860. tty_set_operations(hvsi_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, hvsi_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(hvsi_driver);
  872. if (ret) {
  873. pr_err("Couldn't register hvsi console driver\n");
  874. goto err_free_irq;
  875. }
  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(hvsi_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);