hvsi_lib.c 9.6 KB

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