hvsi_lib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include <linux/types.h>
  2. #include <linux/delay.h>
  3. #include <linux/slab.h>
  4. #include <linux/console.h>
  5. #include <asm/hvsi.h>
  6. #include "hvc_console.h"
  7. static int hvsi_send_packet(struct hvsi_priv *pv, struct hvsi_header *packet)
  8. {
  9. packet->seqno = cpu_to_be16(atomic_inc_return(&pv->seqno));
  10. /* Assumes that always succeeds, works in practice */
  11. return pv->put_chars(pv->termno, (char *)packet, packet->len);
  12. }
  13. static void hvsi_start_handshake(struct hvsi_priv *pv)
  14. {
  15. struct hvsi_query q;
  16. /* Reset state */
  17. pv->established = 0;
  18. atomic_set(&pv->seqno, 0);
  19. pr_devel("HVSI@%x: Handshaking started\n", pv->termno);
  20. /* Send version query */
  21. q.hdr.type = VS_QUERY_PACKET_HEADER;
  22. q.hdr.len = sizeof(struct hvsi_query);
  23. q.verb = cpu_to_be16(VSV_SEND_VERSION_NUMBER);
  24. hvsi_send_packet(pv, &q.hdr);
  25. }
  26. static int hvsi_send_close(struct hvsi_priv *pv)
  27. {
  28. struct hvsi_control ctrl;
  29. pv->established = 0;
  30. ctrl.hdr.type = VS_CONTROL_PACKET_HEADER;
  31. ctrl.hdr.len = sizeof(struct hvsi_control);
  32. ctrl.verb = cpu_to_be16(VSV_CLOSE_PROTOCOL);
  33. return hvsi_send_packet(pv, &ctrl.hdr);
  34. }
  35. static void hvsi_cd_change(struct hvsi_priv *pv, int cd)
  36. {
  37. if (cd)
  38. pv->mctrl |= TIOCM_CD;
  39. else {
  40. pv->mctrl &= ~TIOCM_CD;
  41. /* We copy the existing hvsi driver semantics
  42. * here which are to trigger a hangup when
  43. * we get a carrier loss.
  44. * Closing our connection to the server will
  45. * do just that.
  46. */
  47. if (!pv->is_console && pv->opened) {
  48. pr_devel("HVSI@%x Carrier lost, hanging up !\n",
  49. pv->termno);
  50. hvsi_send_close(pv);
  51. }
  52. }
  53. }
  54. static void hvsi_got_control(struct hvsi_priv *pv)
  55. {
  56. struct hvsi_control *pkt = (struct hvsi_control *)pv->inbuf;
  57. switch (be16_to_cpu(pkt->verb)) {
  58. case VSV_CLOSE_PROTOCOL:
  59. /* We restart the handshaking */
  60. hvsi_start_handshake(pv);
  61. break;
  62. case VSV_MODEM_CTL_UPDATE:
  63. /* Transition of carrier detect */
  64. hvsi_cd_change(pv, be32_to_cpu(pkt->word) & HVSI_TSCD);
  65. break;
  66. }
  67. }
  68. static void hvsi_got_query(struct hvsi_priv *pv)
  69. {
  70. struct hvsi_query *pkt = (struct hvsi_query *)pv->inbuf;
  71. struct hvsi_query_response r;
  72. /* We only handle version queries */
  73. if (be16_to_cpu(pkt->verb) != VSV_SEND_VERSION_NUMBER)
  74. return;
  75. pr_devel("HVSI@%x: Got version query, sending response...\n",
  76. pv->termno);
  77. /* Send version response */
  78. r.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
  79. r.hdr.len = sizeof(struct hvsi_query_response);
  80. r.verb = cpu_to_be16(VSV_SEND_VERSION_NUMBER);
  81. r.u.version = HVSI_VERSION;
  82. r.query_seqno = pkt->hdr.seqno;
  83. hvsi_send_packet(pv, &r.hdr);
  84. /* Assume protocol is open now */
  85. pv->established = 1;
  86. }
  87. static void hvsi_got_response(struct hvsi_priv *pv)
  88. {
  89. struct hvsi_query_response *r =
  90. (struct hvsi_query_response *)pv->inbuf;
  91. switch(r->verb) {
  92. case VSV_SEND_MODEM_CTL_STATUS:
  93. hvsi_cd_change(pv, be32_to_cpu(r->u.mctrl_word) & HVSI_TSCD);
  94. pv->mctrl_update = 1;
  95. break;
  96. }
  97. }
  98. static int hvsi_check_packet(struct hvsi_priv *pv)
  99. {
  100. u8 len, type;
  101. /* Check header validity. If it's invalid, we ditch
  102. * the whole buffer and hope we eventually resync
  103. */
  104. if (pv->inbuf[0] < 0xfc) {
  105. pv->inbuf_len = pv->inbuf_pktlen = 0;
  106. return 0;
  107. }
  108. type = pv->inbuf[0];
  109. len = pv->inbuf[1];
  110. /* Packet incomplete ? */
  111. if (pv->inbuf_len < len)
  112. return 0;
  113. pr_devel("HVSI@%x: Got packet type %x len %d bytes:\n",
  114. pv->termno, type, len);
  115. /* We have a packet, yay ! Handle it */
  116. switch(type) {
  117. case VS_DATA_PACKET_HEADER:
  118. pv->inbuf_pktlen = len - 4;
  119. pv->inbuf_cur = 4;
  120. return 1;
  121. case VS_CONTROL_PACKET_HEADER:
  122. hvsi_got_control(pv);
  123. break;
  124. case VS_QUERY_PACKET_HEADER:
  125. hvsi_got_query(pv);
  126. break;
  127. case VS_QUERY_RESPONSE_PACKET_HEADER:
  128. hvsi_got_response(pv);
  129. break;
  130. }
  131. /* Swallow packet and retry */
  132. pv->inbuf_len -= len;
  133. memmove(pv->inbuf, &pv->inbuf[len], pv->inbuf_len);
  134. return 1;
  135. }
  136. static int hvsi_get_packet(struct hvsi_priv *pv)
  137. {
  138. /* If we have room in the buffer, ask HV for more */
  139. if (pv->inbuf_len < HVSI_INBUF_SIZE)
  140. pv->inbuf_len += pv->get_chars(pv->termno,
  141. &pv->inbuf[pv->inbuf_len],
  142. HVSI_INBUF_SIZE - pv->inbuf_len);
  143. /*
  144. * If we have at least 4 bytes in the buffer, check for
  145. * a full packet and retry
  146. */
  147. if (pv->inbuf_len >= 4)
  148. return hvsi_check_packet(pv);
  149. return 0;
  150. }
  151. int hvsilib_get_chars(struct hvsi_priv *pv, char *buf, int count)
  152. {
  153. unsigned int tries, read = 0;
  154. if (WARN_ON(!pv))
  155. return -ENXIO;
  156. /* If we aren't open, don't do anything in order to avoid races
  157. * with connection establishment. The hvc core will call this
  158. * before we have returned from notifier_add(), and we need to
  159. * avoid multiple users playing with the receive buffer
  160. */
  161. if (!pv->opened)
  162. return 0;
  163. /* We try twice, once with what data we have and once more
  164. * after we try to fetch some more from the hypervisor
  165. */
  166. for (tries = 1; count && tries < 2; tries++) {
  167. /* Consume existing data packet */
  168. if (pv->inbuf_pktlen) {
  169. unsigned int l = min(count, (int)pv->inbuf_pktlen);
  170. memcpy(&buf[read], &pv->inbuf[pv->inbuf_cur], l);
  171. pv->inbuf_cur += l;
  172. pv->inbuf_pktlen -= l;
  173. count -= l;
  174. read += l;
  175. }
  176. if (count == 0)
  177. break;
  178. /* Data packet fully consumed, move down remaning data */
  179. if (pv->inbuf_cur) {
  180. pv->inbuf_len -= pv->inbuf_cur;
  181. memmove(pv->inbuf, &pv->inbuf[pv->inbuf_cur],
  182. pv->inbuf_len);
  183. pv->inbuf_cur = 0;
  184. }
  185. /* Try to get another packet */
  186. if (hvsi_get_packet(pv))
  187. tries--;
  188. }
  189. if (!pv->established) {
  190. pr_devel("HVSI@%x: returning -EPIPE\n", pv->termno);
  191. return -EPIPE;
  192. }
  193. return read;
  194. }
  195. int hvsilib_put_chars(struct hvsi_priv *pv, const char *buf, int count)
  196. {
  197. struct hvsi_data dp;
  198. int rc, adjcount = min(count, HVSI_MAX_OUTGOING_DATA);
  199. if (WARN_ON(!pv))
  200. return -ENODEV;
  201. dp.hdr.type = VS_DATA_PACKET_HEADER;
  202. dp.hdr.len = adjcount + sizeof(struct hvsi_header);
  203. memcpy(dp.data, buf, adjcount);
  204. rc = hvsi_send_packet(pv, &dp.hdr);
  205. if (rc <= 0)
  206. return rc;
  207. return adjcount;
  208. }
  209. static void maybe_msleep(unsigned long ms)
  210. {
  211. /* During early boot, IRQs are disabled, use mdelay */
  212. if (irqs_disabled())
  213. mdelay(ms);
  214. else
  215. msleep(ms);
  216. }
  217. int hvsilib_read_mctrl(struct hvsi_priv *pv)
  218. {
  219. struct hvsi_query q;
  220. int rc, timeout;
  221. pr_devel("HVSI@%x: Querying modem control status...\n",
  222. pv->termno);
  223. pv->mctrl_update = 0;
  224. q.hdr.type = VS_QUERY_PACKET_HEADER;
  225. q.hdr.len = sizeof(struct hvsi_query);
  226. q.verb = cpu_to_be16(VSV_SEND_MODEM_CTL_STATUS);
  227. rc = hvsi_send_packet(pv, &q.hdr);
  228. if (rc <= 0) {
  229. pr_devel("HVSI@%x: Error %d...\n", pv->termno, rc);
  230. return rc;
  231. }
  232. /* Try for up to 200ms */
  233. for (timeout = 0; timeout < 20; timeout++) {
  234. if (!pv->established)
  235. return -ENXIO;
  236. if (pv->mctrl_update)
  237. return 0;
  238. if (!hvsi_get_packet(pv))
  239. maybe_msleep(10);
  240. }
  241. return -EIO;
  242. }
  243. int hvsilib_write_mctrl(struct hvsi_priv *pv, int dtr)
  244. {
  245. struct hvsi_control ctrl;
  246. unsigned short mctrl;
  247. mctrl = pv->mctrl;
  248. if (dtr)
  249. mctrl |= TIOCM_DTR;
  250. else
  251. mctrl &= ~TIOCM_DTR;
  252. if (mctrl == pv->mctrl)
  253. return 0;
  254. pv->mctrl = mctrl;
  255. pr_devel("HVSI@%x: %s DTR...\n", pv->termno,
  256. dtr ? "Setting" : "Clearing");
  257. ctrl.hdr.type = VS_CONTROL_PACKET_HEADER,
  258. ctrl.hdr.len = sizeof(struct hvsi_control);
  259. ctrl.verb = cpu_to_be16(VSV_SET_MODEM_CTL);
  260. ctrl.mask = cpu_to_be32(HVSI_TSDTR);
  261. ctrl.word = cpu_to_be32(dtr ? HVSI_TSDTR : 0);
  262. return hvsi_send_packet(pv, &ctrl.hdr);
  263. }
  264. void hvsilib_establish(struct hvsi_priv *pv)
  265. {
  266. int timeout;
  267. pr_devel("HVSI@%x: Establishing...\n", pv->termno);
  268. /* Try for up to 200ms, there can be a packet to
  269. * start the process waiting for us...
  270. */
  271. for (timeout = 0; timeout < 20; timeout++) {
  272. if (pv->established)
  273. goto established;
  274. if (!hvsi_get_packet(pv))
  275. maybe_msleep(10);
  276. }
  277. /* Failed, send a close connection packet just
  278. * in case
  279. */
  280. pr_devel("HVSI@%x: ... sending close\n", pv->termno);
  281. hvsi_send_close(pv);
  282. /* Then restart handshake */
  283. pr_devel("HVSI@%x: ... restarting handshake\n", pv->termno);
  284. hvsi_start_handshake(pv);
  285. pr_devel("HVSI@%x: ... waiting handshake\n", pv->termno);
  286. /* Try for up to 400ms */
  287. for (timeout = 0; timeout < 40; timeout++) {
  288. if (pv->established)
  289. goto established;
  290. if (!hvsi_get_packet(pv))
  291. maybe_msleep(10);
  292. }
  293. if (!pv->established) {
  294. pr_devel("HVSI@%x: Timeout handshaking, giving up !\n",
  295. pv->termno);
  296. return;
  297. }
  298. established:
  299. /* Query modem control lines */
  300. pr_devel("HVSI@%x: ... established, reading mctrl\n", pv->termno);
  301. hvsilib_read_mctrl(pv);
  302. /* Set our own DTR */
  303. pr_devel("HVSI@%x: ... setting mctrl\n", pv->termno);
  304. hvsilib_write_mctrl(pv, 1);
  305. /* Set the opened flag so reads are allowed */
  306. wmb();
  307. pv->opened = 1;
  308. }
  309. int hvsilib_open(struct hvsi_priv *pv, struct hvc_struct *hp)
  310. {
  311. pr_devel("HVSI@%x: open !\n", pv->termno);
  312. /* Keep track of the tty data structure */
  313. pv->tty = tty_port_tty_get(&hp->port);
  314. hvsilib_establish(pv);
  315. return 0;
  316. }
  317. void hvsilib_close(struct hvsi_priv *pv, struct hvc_struct *hp)
  318. {
  319. unsigned long flags;
  320. pr_devel("HVSI@%x: close !\n", pv->termno);
  321. if (!pv->is_console) {
  322. pr_devel("HVSI@%x: Not a console, tearing down\n",
  323. pv->termno);
  324. /* Clear opened, synchronize with khvcd */
  325. spin_lock_irqsave(&hp->lock, flags);
  326. pv->opened = 0;
  327. spin_unlock_irqrestore(&hp->lock, flags);
  328. /* Clear our own DTR */
  329. if (!pv->tty || (pv->tty->termios.c_cflag & HUPCL))
  330. hvsilib_write_mctrl(pv, 0);
  331. /* Tear down the connection */
  332. hvsi_send_close(pv);
  333. }
  334. tty_kref_put(pv->tty);
  335. pv->tty = NULL;
  336. }
  337. void hvsilib_init(struct hvsi_priv *pv,
  338. int (*get_chars)(uint32_t termno, char *buf, int count),
  339. int (*put_chars)(uint32_t termno, const char *buf,
  340. int count),
  341. int termno, int is_console)
  342. {
  343. memset(pv, 0, sizeof(*pv));
  344. pv->get_chars = get_chars;
  345. pv->put_chars = put_chars;
  346. pv->termno = termno;
  347. pv->is_console = is_console;
  348. }