hvc_vio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * vio driver interface to hvc_console.c
  3. *
  4. * This code was moved here to allow the remaining code to be reused as a
  5. * generic polling mode with semi-reliable transport driver core to the
  6. * console and tty subsystems.
  7. *
  8. *
  9. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  10. * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
  11. * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  12. * Copyright (C) 2004 IBM Corporation
  13. *
  14. * Additional Author(s):
  15. * Ryan S. Arnold <rsa@us.ibm.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. * TODO:
  32. *
  33. * - handle error in sending hvsi protocol packets
  34. * - retry nego on subsequent sends ?
  35. */
  36. #undef DEBUG
  37. #include <linux/types.h>
  38. #include <linux/init.h>
  39. #include <linux/delay.h>
  40. #include <linux/slab.h>
  41. #include <linux/console.h>
  42. #include <asm/hvconsole.h>
  43. #include <asm/vio.h>
  44. #include <asm/prom.h>
  45. #include <asm/firmware.h>
  46. #include <asm/hvsi.h>
  47. #include <asm/udbg.h>
  48. #include "hvc_console.h"
  49. static const char hvc_driver_name[] = "hvc_console";
  50. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  51. {"serial", "hvterm1"},
  52. #ifndef HVC_OLD_HVSI
  53. {"serial", "hvterm-protocol"},
  54. #endif
  55. { "", "" }
  56. };
  57. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  58. typedef enum hv_protocol {
  59. HV_PROTOCOL_RAW,
  60. HV_PROTOCOL_HVSI
  61. } hv_protocol_t;
  62. #define HV_INBUF_SIZE 255
  63. struct hvterm_priv {
  64. u32 termno; /* HV term number */
  65. hv_protocol_t proto; /* Raw data or HVSI packets */
  66. unsigned int inbuf_len; /* Data in input buffer */
  67. unsigned char inbuf[HV_INBUF_SIZE];
  68. unsigned int inbuf_cur; /* Cursor in input buffer */
  69. unsigned int inbuf_pktlen; /* HVSI packet lenght from cursor */
  70. atomic_t seqno; /* HVSI packet sequence number */
  71. unsigned int opened:1; /* HVSI driver opened */
  72. unsigned int established:1; /* HVSI protocol established */
  73. unsigned int is_console:1; /* Used as a kernel console device */
  74. unsigned int mctrl_update:1; /* HVSI modem control updated */
  75. unsigned short mctrl; /* HVSI modem control */
  76. struct tty_struct *tty; /* TTY structure */
  77. };
  78. static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
  79. /* For early boot console */
  80. static struct hvterm_priv hvterm_priv0;
  81. static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
  82. {
  83. struct hvterm_priv *pv = hvterm_privs[vtermno];
  84. unsigned long got, i;
  85. if (WARN_ON(!pv))
  86. return 0;
  87. /*
  88. * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
  89. * so we play safe and avoid the situation where got > count which could
  90. * overload the flip buffer.
  91. */
  92. if (count < SIZE_VIO_GET_CHARS)
  93. return -EAGAIN;
  94. got = hvc_get_chars(pv->termno, buf, count);
  95. /*
  96. * Work around a HV bug where it gives us a null
  97. * after every \r. -- paulus
  98. */
  99. for (i = 1; i < got; ++i) {
  100. if (buf[i] == 0 && buf[i-1] == '\r') {
  101. --got;
  102. if (i < got)
  103. memmove(&buf[i], &buf[i+1], got - i);
  104. }
  105. }
  106. return got;
  107. }
  108. static int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count)
  109. {
  110. struct hvterm_priv *pv = hvterm_privs[vtermno];
  111. if (WARN_ON(!pv))
  112. return 0;
  113. return hvc_put_chars(pv->termno, buf, count);
  114. }
  115. static const struct hv_ops hvterm_raw_ops = {
  116. .get_chars = hvterm_raw_get_chars,
  117. .put_chars = hvterm_raw_put_chars,
  118. .notifier_add = notifier_add_irq,
  119. .notifier_del = notifier_del_irq,
  120. .notifier_hangup = notifier_hangup_irq,
  121. };
  122. static int hvterm_hvsi_send_packet(struct hvterm_priv *pv, struct hvsi_header *packet)
  123. {
  124. packet->seqno = atomic_inc_return(&pv->seqno);
  125. /* Assumes that always succeeds, works in practice */
  126. return hvc_put_chars(pv->termno, (char *)packet, packet->len);
  127. }
  128. static void hvterm_hvsi_start_handshake(struct hvterm_priv *pv)
  129. {
  130. struct hvsi_query q;
  131. /* Reset state */
  132. pv->established = 0;
  133. atomic_set(&pv->seqno, 0);
  134. pr_devel("HVSI@%x: Handshaking started\n", pv->termno);
  135. /* Send version query */
  136. q.hdr.type = VS_QUERY_PACKET_HEADER;
  137. q.hdr.len = sizeof(struct hvsi_query);
  138. q.verb = VSV_SEND_VERSION_NUMBER;
  139. hvterm_hvsi_send_packet(pv, &q.hdr);
  140. }
  141. static int hvterm_hvsi_send_close(struct hvterm_priv *pv)
  142. {
  143. struct hvsi_control ctrl;
  144. pv->established = 0;
  145. ctrl.hdr.type = VS_CONTROL_PACKET_HEADER;
  146. ctrl.hdr.len = sizeof(struct hvsi_control);
  147. ctrl.verb = VSV_CLOSE_PROTOCOL;
  148. return hvterm_hvsi_send_packet(pv, &ctrl.hdr);
  149. }
  150. static void hvterm_cd_change(struct hvterm_priv *pv, int cd)
  151. {
  152. if (cd)
  153. pv->mctrl |= TIOCM_CD;
  154. else {
  155. pv->mctrl &= ~TIOCM_CD;
  156. /* We copy the existing hvsi driver semantics
  157. * here which are to trigger a hangup when
  158. * we get a carrier loss.
  159. * Closing our connection to the server will
  160. * do just that.
  161. */
  162. if (!pv->is_console && pv->opened) {
  163. pr_devel("HVSI@%x Carrier lost, hanging up !\n",
  164. pv->termno);
  165. hvterm_hvsi_send_close(pv);
  166. }
  167. }
  168. }
  169. static void hvterm_hvsi_got_control(struct hvterm_priv *pv)
  170. {
  171. struct hvsi_control *pkt = (struct hvsi_control *)pv->inbuf;
  172. switch (pkt->verb) {
  173. case VSV_CLOSE_PROTOCOL:
  174. /* We restart the handshaking */
  175. hvterm_hvsi_start_handshake(pv);
  176. break;
  177. case VSV_MODEM_CTL_UPDATE:
  178. /* Transition of carrier detect */
  179. hvterm_cd_change(pv, pkt->word & HVSI_TSCD);
  180. break;
  181. }
  182. }
  183. static void hvterm_hvsi_got_query(struct hvterm_priv *pv)
  184. {
  185. struct hvsi_query *pkt = (struct hvsi_query *)pv->inbuf;
  186. struct hvsi_query_response r;
  187. /* We only handle version queries */
  188. if (pkt->verb != VSV_SEND_VERSION_NUMBER)
  189. return;
  190. pr_devel("HVSI@%x: Got version query, sending response...\n",
  191. pv->termno);
  192. /* Send version response */
  193. r.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
  194. r.hdr.len = sizeof(struct hvsi_query_response);
  195. r.verb = VSV_SEND_VERSION_NUMBER;
  196. r.u.version = HVSI_VERSION;
  197. r.query_seqno = pkt->hdr.seqno;
  198. hvterm_hvsi_send_packet(pv, &r.hdr);
  199. /* Assume protocol is open now */
  200. pv->established = 1;
  201. }
  202. static void hvterm_hvsi_got_response(struct hvterm_priv *pv)
  203. {
  204. struct hvsi_query_response *r = (struct hvsi_query_response *)pv->inbuf;
  205. switch(r->verb) {
  206. case VSV_SEND_MODEM_CTL_STATUS:
  207. hvterm_cd_change(pv, r->u.mctrl_word & HVSI_TSCD);
  208. pv->mctrl_update = 1;
  209. break;
  210. }
  211. }
  212. static int hvterm_hvsi_check_packet(struct hvterm_priv *pv)
  213. {
  214. u8 len, type;
  215. /* Check header validity. If it's invalid, we ditch
  216. * the whole buffer and hope we eventually resync
  217. */
  218. if (pv->inbuf[0] < 0xfc) {
  219. pv->inbuf_len = pv->inbuf_pktlen = 0;
  220. return 0;
  221. }
  222. type = pv->inbuf[0];
  223. len = pv->inbuf[1];
  224. /* Packet incomplete ? */
  225. if (pv->inbuf_len < len)
  226. return 0;
  227. pr_devel("HVSI@%x: Got packet type %x len %d bytes:\n",
  228. pv->termno, type, len);
  229. /* We have a packet, yay ! Handle it */
  230. switch(type) {
  231. case VS_DATA_PACKET_HEADER:
  232. pv->inbuf_pktlen = len - 4;
  233. pv->inbuf_cur = 4;
  234. return 1;
  235. case VS_CONTROL_PACKET_HEADER:
  236. hvterm_hvsi_got_control(pv);
  237. break;
  238. case VS_QUERY_PACKET_HEADER:
  239. hvterm_hvsi_got_query(pv);
  240. break;
  241. case VS_QUERY_RESPONSE_PACKET_HEADER:
  242. hvterm_hvsi_got_response(pv);
  243. break;
  244. }
  245. /* Swallow packet and retry */
  246. pv->inbuf_len -= len;
  247. memmove(pv->inbuf, &pv->inbuf[len], pv->inbuf_len);
  248. return 1;
  249. }
  250. static int hvterm_hvsi_get_packet(struct hvterm_priv *pv)
  251. {
  252. /* If we have room in the buffer, ask HV for more */
  253. if (pv->inbuf_len < HV_INBUF_SIZE)
  254. pv->inbuf_len += hvc_get_chars(pv->termno,
  255. &pv->inbuf[pv->inbuf_len],
  256. HV_INBUF_SIZE - pv->inbuf_len);
  257. /*
  258. * If we have at least 4 bytes in the buffer, check for
  259. * a full packet and retry
  260. */
  261. if (pv->inbuf_len >= 4)
  262. return hvterm_hvsi_check_packet(pv);
  263. return 0;
  264. }
  265. static int hvterm_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
  266. {
  267. struct hvterm_priv *pv = hvterm_privs[vtermno];
  268. unsigned int tries, read = 0;
  269. if (WARN_ON(!pv))
  270. return 0;
  271. /* If we aren't open, dont do anything in order to avoid races
  272. * with connection establishment. The hvc core will call this
  273. * before we have returned from notifier_add(), and we need to
  274. * avoid multiple users playing with the receive buffer
  275. */
  276. if (!pv->opened)
  277. return 0;
  278. /* We try twice, once with what data we have and once more
  279. * after we try to fetch some more from the hypervisor
  280. */
  281. for (tries = 1; count && tries < 2; tries++) {
  282. /* Consume existing data packet */
  283. if (pv->inbuf_pktlen) {
  284. unsigned int l = min(count, (int)pv->inbuf_pktlen);
  285. memcpy(&buf[read], &pv->inbuf[pv->inbuf_cur], l);
  286. pv->inbuf_cur += l;
  287. pv->inbuf_pktlen -= l;
  288. count -= l;
  289. read += l;
  290. }
  291. if (count == 0)
  292. break;
  293. /* Data packet fully consumed, move down remaning data */
  294. if (pv->inbuf_cur) {
  295. pv->inbuf_len -= pv->inbuf_cur;
  296. memmove(pv->inbuf, &pv->inbuf[pv->inbuf_cur], pv->inbuf_len);
  297. pv->inbuf_cur = 0;
  298. }
  299. /* Try to get another packet */
  300. if (hvterm_hvsi_get_packet(pv))
  301. tries--;
  302. }
  303. if (!pv->established) {
  304. pr_devel("HVSI@%x: returning -EPIPE\n", pv->termno);
  305. return -EPIPE;
  306. }
  307. return read;
  308. }
  309. static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  310. {
  311. struct hvterm_priv *pv = hvterm_privs[vtermno];
  312. struct hvsi_data dp;
  313. int rc, adjcount = min(count, HVSI_MAX_OUTGOING_DATA);
  314. if (WARN_ON(!pv))
  315. return 0;
  316. dp.hdr.type = VS_DATA_PACKET_HEADER;
  317. dp.hdr.len = adjcount + sizeof(struct hvsi_header);
  318. memcpy(dp.data, buf, adjcount);
  319. rc = hvterm_hvsi_send_packet(pv, &dp.hdr);
  320. if (rc <= 0)
  321. return rc;
  322. return adjcount;
  323. }
  324. static void maybe_msleep(unsigned long ms)
  325. {
  326. /* During early boot, IRQs are disabled, use mdelay */
  327. if (irqs_disabled())
  328. mdelay(ms);
  329. else
  330. msleep(ms);
  331. }
  332. static int hvterm_hvsi_read_mctrl(struct hvterm_priv *pv)
  333. {
  334. struct hvsi_query q;
  335. int rc, timeout;
  336. pr_devel("HVSI@%x: Querying modem control status...\n",
  337. pv->termno);
  338. pv->mctrl_update = 0;
  339. q.hdr.type = VS_QUERY_PACKET_HEADER;
  340. q.hdr.len = sizeof(struct hvsi_query);
  341. q.hdr.seqno = atomic_inc_return(&pv->seqno);
  342. q.verb = VSV_SEND_MODEM_CTL_STATUS;
  343. rc = hvterm_hvsi_send_packet(pv, &q.hdr);
  344. if (rc <= 0) {
  345. pr_devel("HVSI@%x: Error %d...\n", pv->termno, rc);
  346. return rc;
  347. }
  348. /* Try for up to 1s */
  349. for (timeout = 0; timeout < 1000; timeout++) {
  350. if (!pv->established)
  351. return -ENXIO;
  352. if (pv->mctrl_update)
  353. return 0;
  354. if (!hvterm_hvsi_get_packet(pv))
  355. maybe_msleep(1);
  356. }
  357. return -EIO;
  358. }
  359. static int hvterm_hvsi_write_mctrl(struct hvterm_priv *pv, int dtr)
  360. {
  361. struct hvsi_control ctrl;
  362. pr_devel("HVSI@%x: %s DTR...\n", pv->termno,
  363. dtr ? "Setting" : "Clearing");
  364. ctrl.hdr.type = VS_CONTROL_PACKET_HEADER,
  365. ctrl.hdr.len = sizeof(struct hvsi_control);
  366. ctrl.verb = VSV_SET_MODEM_CTL;
  367. ctrl.mask = HVSI_TSDTR;
  368. ctrl.word = dtr ? HVSI_TSDTR : 0;
  369. if (dtr)
  370. pv->mctrl |= TIOCM_DTR;
  371. else
  372. pv->mctrl &= ~TIOCM_DTR;
  373. return hvterm_hvsi_send_packet(pv, &ctrl.hdr);
  374. }
  375. static void hvterm_hvsi_establish(struct hvterm_priv *pv)
  376. {
  377. int timeout;
  378. /* Try for up to 10ms, there can be a packet to
  379. * start the process waiting for us...
  380. */
  381. for (timeout = 0; timeout < 10; timeout++) {
  382. if (pv->established)
  383. goto established;
  384. if (!hvterm_hvsi_get_packet(pv))
  385. maybe_msleep(1);
  386. }
  387. /* Failed, send a close connection packet just
  388. * in case
  389. */
  390. hvterm_hvsi_send_close(pv);
  391. /* Then restart handshake */
  392. hvterm_hvsi_start_handshake(pv);
  393. /* Try for up to 100ms */
  394. for (timeout = 0; timeout < 100; timeout++) {
  395. if (pv->established)
  396. goto established;
  397. if (!hvterm_hvsi_get_packet(pv))
  398. maybe_msleep(1);
  399. }
  400. if (!pv->established) {
  401. pr_devel("HVSI@%x: Timeout handshaking, giving up !\n",
  402. pv->termno);
  403. return;
  404. }
  405. established:
  406. /* Query modem control lines */
  407. hvterm_hvsi_read_mctrl(pv);
  408. /* Set our own DTR */
  409. hvterm_hvsi_write_mctrl(pv, 1);
  410. /* Set the opened flag so reads are allowed */
  411. wmb();
  412. pv->opened = 1;
  413. }
  414. static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
  415. {
  416. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  417. int rc;
  418. pr_devel("HVSI@%x: open !\n", pv->termno);
  419. rc = notifier_add_irq(hp, data);
  420. if (rc)
  421. return rc;
  422. /* Keep track of the tty data structure */
  423. pv->tty = tty_kref_get(hp->tty);
  424. hvterm_hvsi_establish(pv);
  425. return 0;
  426. }
  427. static void hvterm_hvsi_shutdown(struct hvc_struct *hp, struct hvterm_priv *pv)
  428. {
  429. unsigned long flags;
  430. if (!pv->is_console) {
  431. pr_devel("HVSI@%x: Not a console, tearing down\n",
  432. pv->termno);
  433. /* Clear opened, synchronize with khvcd */
  434. spin_lock_irqsave(&hp->lock, flags);
  435. pv->opened = 0;
  436. spin_unlock_irqrestore(&hp->lock, flags);
  437. /* Clear our own DTR */
  438. if (!pv->tty || (pv->tty->termios->c_cflag & HUPCL))
  439. hvterm_hvsi_write_mctrl(pv, 0);
  440. /* Tear down the connection */
  441. hvterm_hvsi_send_close(pv);
  442. }
  443. if (pv->tty)
  444. tty_kref_put(pv->tty);
  445. pv->tty = NULL;
  446. }
  447. static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
  448. {
  449. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  450. pr_devel("HVSI@%x: close !\n", pv->termno);
  451. hvterm_hvsi_shutdown(hp, pv);
  452. notifier_del_irq(hp, data);
  453. }
  454. void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
  455. {
  456. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  457. pr_devel("HVSI@%x: hangup !\n", pv->termno);
  458. hvterm_hvsi_shutdown(hp, pv);
  459. notifier_hangup_irq(hp, data);
  460. }
  461. static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
  462. {
  463. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  464. if (!pv)
  465. return -EINVAL;
  466. return pv->mctrl;
  467. }
  468. static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  469. unsigned int clear)
  470. {
  471. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  472. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  473. pv->termno, set, clear);
  474. if (set & TIOCM_DTR)
  475. hvterm_hvsi_write_mctrl(pv, 1);
  476. else if (clear & TIOCM_DTR)
  477. hvterm_hvsi_write_mctrl(pv, 0);
  478. return 0;
  479. }
  480. static const struct hv_ops hvterm_hvsi_ops = {
  481. .get_chars = hvterm_hvsi_get_chars,
  482. .put_chars = hvterm_hvsi_put_chars,
  483. .notifier_add = hvterm_hvsi_open,
  484. .notifier_del = hvterm_hvsi_close,
  485. .notifier_hangup = hvterm_hvsi_hangup,
  486. .tiocmget = hvterm_hvsi_tiocmget,
  487. .tiocmset = hvterm_hvsi_tiocmset,
  488. };
  489. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  490. const struct vio_device_id *id)
  491. {
  492. const struct hv_ops *ops;
  493. struct hvc_struct *hp;
  494. struct hvterm_priv *pv;
  495. hv_protocol_t proto;
  496. int i, termno = -1;
  497. /* probed with invalid parameters. */
  498. if (!vdev || !id)
  499. return -EPERM;
  500. if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
  501. proto = HV_PROTOCOL_RAW;
  502. ops = &hvterm_raw_ops;
  503. } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
  504. proto = HV_PROTOCOL_HVSI;
  505. ops = &hvterm_hvsi_ops;
  506. } else {
  507. pr_err("hvc_vio: Unkown protocol for %s\n", vdev->dev.of_node->full_name);
  508. return -ENXIO;
  509. }
  510. pr_devel("hvc_vio_probe() device %s, using %s protocol\n",
  511. vdev->dev.of_node->full_name,
  512. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
  513. /* Is it our boot one ? */
  514. if (hvterm_privs[0] == &hvterm_priv0 &&
  515. vdev->unit_address == hvterm_priv0.termno) {
  516. pv = hvterm_privs[0];
  517. termno = 0;
  518. pr_devel("->boot console, using termno 0\n");
  519. }
  520. /* nope, allocate a new one */
  521. else {
  522. for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
  523. if (!hvterm_privs[i])
  524. termno = i;
  525. pr_devel("->non-boot console, using termno %d\n", termno);
  526. if (termno < 0)
  527. return -ENODEV;
  528. pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
  529. if (!pv)
  530. return -ENOMEM;
  531. pv->termno = vdev->unit_address;
  532. pv->proto = proto;
  533. hvterm_privs[termno] = pv;
  534. }
  535. hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
  536. if (IS_ERR(hp))
  537. return PTR_ERR(hp);
  538. dev_set_drvdata(&vdev->dev, hp);
  539. return 0;
  540. }
  541. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  542. {
  543. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  544. int rc, termno;
  545. termno = hp->vtermno;
  546. rc = hvc_remove(hp);
  547. if (rc == 0) {
  548. if (hvterm_privs[termno] != &hvterm_priv0)
  549. kfree(hvterm_privs[termno]);
  550. hvterm_privs[termno] = NULL;
  551. }
  552. return rc;
  553. }
  554. static struct vio_driver hvc_vio_driver = {
  555. .id_table = hvc_driver_table,
  556. .probe = hvc_vio_probe,
  557. .remove = __devexit_p(hvc_vio_remove),
  558. .driver = {
  559. .name = hvc_driver_name,
  560. .owner = THIS_MODULE,
  561. }
  562. };
  563. static int __init hvc_vio_init(void)
  564. {
  565. int rc;
  566. if (firmware_has_feature(FW_FEATURE_ISERIES))
  567. return -EIO;
  568. /* Register as a vio device to receive callbacks */
  569. rc = vio_register_driver(&hvc_vio_driver);
  570. return rc;
  571. }
  572. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  573. static void __exit hvc_vio_exit(void)
  574. {
  575. vio_unregister_driver(&hvc_vio_driver);
  576. }
  577. module_exit(hvc_vio_exit);
  578. static void udbg_hvc_putc(char c)
  579. {
  580. int count = -1;
  581. if (c == '\n')
  582. udbg_hvc_putc('\r');
  583. do {
  584. switch(hvterm_priv0.proto) {
  585. case HV_PROTOCOL_RAW:
  586. count = hvterm_raw_put_chars(0, &c, 1);
  587. break;
  588. case HV_PROTOCOL_HVSI:
  589. count = hvterm_hvsi_put_chars(0, &c, 1);
  590. break;
  591. }
  592. } while(count == 0);
  593. }
  594. static int udbg_hvc_getc_poll(void)
  595. {
  596. int rc = 0;
  597. char c;
  598. switch(hvterm_priv0.proto) {
  599. case HV_PROTOCOL_RAW:
  600. rc = hvterm_raw_get_chars(0, &c, 1);
  601. break;
  602. case HV_PROTOCOL_HVSI:
  603. rc = hvterm_hvsi_get_chars(0, &c, 1);
  604. break;
  605. }
  606. if (!rc)
  607. return -1;
  608. return c;
  609. }
  610. static int udbg_hvc_getc(void)
  611. {
  612. int ch;
  613. for (;;) {
  614. ch = udbg_hvc_getc_poll();
  615. if (ch == -1) {
  616. /* This shouldn't be needed...but... */
  617. volatile unsigned long delay;
  618. for (delay=0; delay < 2000000; delay++)
  619. ;
  620. } else {
  621. return ch;
  622. }
  623. }
  624. }
  625. void __init hvc_vio_init_early(void)
  626. {
  627. struct device_node *stdout_node;
  628. const u32 *termno;
  629. const char *name;
  630. const struct hv_ops *ops;
  631. /* find the boot console from /chosen/stdout */
  632. if (!of_chosen)
  633. return;
  634. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  635. if (name == NULL)
  636. return;
  637. stdout_node = of_find_node_by_path(name);
  638. if (!stdout_node)
  639. return;
  640. name = of_get_property(stdout_node, "name", NULL);
  641. if (!name) {
  642. printk(KERN_WARNING "stdout node missing 'name' property!\n");
  643. goto out;
  644. }
  645. /* Check if it's a virtual terminal */
  646. if (strncmp(name, "vty", 3) != 0)
  647. goto out;
  648. termno = of_get_property(stdout_node, "reg", NULL);
  649. if (termno == NULL)
  650. goto out;
  651. hvterm_priv0.termno = *termno;
  652. hvterm_priv0.is_console = 1;
  653. hvterm_privs[0] = &hvterm_priv0;
  654. /* Check the protocol */
  655. if (of_device_is_compatible(stdout_node, "hvterm1")) {
  656. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  657. ops = &hvterm_raw_ops;
  658. }
  659. else if (of_device_is_compatible(stdout_node, "hvterm-protocol")) {
  660. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  661. ops = &hvterm_hvsi_ops;
  662. /* HVSI, perform the handshake now */
  663. hvterm_hvsi_establish(&hvterm_priv0);
  664. } else
  665. goto out;
  666. udbg_putc = udbg_hvc_putc;
  667. udbg_getc = udbg_hvc_getc;
  668. udbg_getc_poll = udbg_hvc_getc_poll;
  669. #ifdef HVC_OLD_HVSI
  670. /* When using the old HVSI driver don't register the HVC
  671. * backend for HVSI, only do udbg
  672. */
  673. if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
  674. goto out;
  675. #endif
  676. add_preferred_console("hvc", 0, NULL);
  677. hvc_instantiate(0, 0, ops);
  678. out:
  679. of_node_put(stdout_node);
  680. }
  681. /* call this from early_init() for a working debug console on
  682. * vterm capable LPAR machines
  683. */
  684. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
  685. void __init udbg_init_debug_lpar(void)
  686. {
  687. hvterm_privs[0] = &hvterm_priv0;
  688. hvterm_priv0.termno = 0;
  689. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  690. hvterm_priv0.is_console = 1;
  691. udbg_putc = udbg_hvc_putc;
  692. udbg_getc = udbg_hvc_getc;
  693. udbg_getc_poll = udbg_hvc_getc_poll;
  694. }
  695. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
  696. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
  697. void __init udbg_init_debug_lpar_hvsi(void)
  698. {
  699. hvterm_privs[0] = &hvterm_priv0;
  700. hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
  701. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  702. hvterm_priv0.is_console = 1;
  703. udbg_putc = udbg_hvc_putc;
  704. udbg_getc = udbg_hvc_getc;
  705. udbg_getc_poll = udbg_hvc_getc_poll;
  706. hvterm_hvsi_establish(&hvterm_priv0);
  707. }
  708. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */