hvsi.c 31 KB

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