hvsi.c 30 KB

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