hvc_vio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <linux/module.h>
  43. #include <asm/hvconsole.h>
  44. #include <asm/vio.h>
  45. #include <asm/prom.h>
  46. #include <asm/firmware.h>
  47. #include <asm/hvsi.h>
  48. #include <asm/udbg.h>
  49. #include "hvc_console.h"
  50. static const char hvc_driver_name[] = "hvc_console";
  51. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  52. {"serial", "hvterm1"},
  53. #ifndef HVC_OLD_HVSI
  54. {"serial", "hvterm-protocol"},
  55. #endif
  56. { "", "" }
  57. };
  58. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  59. typedef enum hv_protocol {
  60. HV_PROTOCOL_RAW,
  61. HV_PROTOCOL_HVSI
  62. } hv_protocol_t;
  63. struct hvterm_priv {
  64. u32 termno; /* HV term number */
  65. hv_protocol_t proto; /* Raw data or HVSI packets */
  66. struct hvsi_priv hvsi; /* HVSI specific data */
  67. spinlock_t buf_lock;
  68. char buf[SIZE_VIO_GET_CHARS];
  69. int left;
  70. int offset;
  71. };
  72. static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
  73. /* For early boot console */
  74. static struct hvterm_priv hvterm_priv0;
  75. static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
  76. {
  77. struct hvterm_priv *pv = hvterm_privs[vtermno];
  78. unsigned long i;
  79. unsigned long flags;
  80. int got;
  81. if (WARN_ON(!pv))
  82. return 0;
  83. spin_lock_irqsave(&pv->buf_lock, flags);
  84. if (pv->left == 0) {
  85. pv->offset = 0;
  86. pv->left = hvc_get_chars(pv->termno, pv->buf, count);
  87. /*
  88. * Work around a HV bug where it gives us a null
  89. * after every \r. -- paulus
  90. */
  91. for (i = 1; i < pv->left; ++i) {
  92. if (pv->buf[i] == 0 && pv->buf[i-1] == '\r') {
  93. --pv->left;
  94. if (i < pv->left) {
  95. memmove(&pv->buf[i], &pv->buf[i+1],
  96. pv->left - i);
  97. }
  98. }
  99. }
  100. }
  101. got = min(count, pv->left);
  102. memcpy(buf, &pv->buf[pv->offset], got);
  103. pv->offset += got;
  104. pv->left -= got;
  105. spin_unlock_irqrestore(&pv->buf_lock, flags);
  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_get_chars(uint32_t vtermno, char *buf, int count)
  123. {
  124. struct hvterm_priv *pv = hvterm_privs[vtermno];
  125. if (WARN_ON(!pv))
  126. return 0;
  127. return hvsilib_get_chars(&pv->hvsi, buf, count);
  128. }
  129. static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  130. {
  131. struct hvterm_priv *pv = hvterm_privs[vtermno];
  132. if (WARN_ON(!pv))
  133. return 0;
  134. return hvsilib_put_chars(&pv->hvsi, buf, count);
  135. }
  136. static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
  137. {
  138. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  139. int rc;
  140. pr_devel("HVSI@%x: open !\n", pv->termno);
  141. rc = notifier_add_irq(hp, data);
  142. if (rc)
  143. return rc;
  144. return hvsilib_open(&pv->hvsi, hp);
  145. }
  146. static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
  147. {
  148. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  149. pr_devel("HVSI@%x: do close !\n", pv->termno);
  150. hvsilib_close(&pv->hvsi, hp);
  151. notifier_del_irq(hp, data);
  152. }
  153. void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
  154. {
  155. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  156. pr_devel("HVSI@%x: do hangup !\n", pv->termno);
  157. hvsilib_close(&pv->hvsi, hp);
  158. notifier_hangup_irq(hp, data);
  159. }
  160. static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
  161. {
  162. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  163. if (!pv)
  164. return -EINVAL;
  165. return pv->hvsi.mctrl;
  166. }
  167. static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  168. unsigned int clear)
  169. {
  170. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  171. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  172. pv->termno, set, clear);
  173. if (set & TIOCM_DTR)
  174. hvsilib_write_mctrl(&pv->hvsi, 1);
  175. else if (clear & TIOCM_DTR)
  176. hvsilib_write_mctrl(&pv->hvsi, 0);
  177. return 0;
  178. }
  179. static const struct hv_ops hvterm_hvsi_ops = {
  180. .get_chars = hvterm_hvsi_get_chars,
  181. .put_chars = hvterm_hvsi_put_chars,
  182. .notifier_add = hvterm_hvsi_open,
  183. .notifier_del = hvterm_hvsi_close,
  184. .notifier_hangup = hvterm_hvsi_hangup,
  185. .tiocmget = hvterm_hvsi_tiocmget,
  186. .tiocmset = hvterm_hvsi_tiocmset,
  187. };
  188. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  189. const struct vio_device_id *id)
  190. {
  191. const struct hv_ops *ops;
  192. struct hvc_struct *hp;
  193. struct hvterm_priv *pv;
  194. hv_protocol_t proto;
  195. int i, termno = -1;
  196. /* probed with invalid parameters. */
  197. if (!vdev || !id)
  198. return -EPERM;
  199. if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
  200. proto = HV_PROTOCOL_RAW;
  201. ops = &hvterm_raw_ops;
  202. } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
  203. proto = HV_PROTOCOL_HVSI;
  204. ops = &hvterm_hvsi_ops;
  205. } else {
  206. pr_err("hvc_vio: Unkown protocol for %s\n", vdev->dev.of_node->full_name);
  207. return -ENXIO;
  208. }
  209. pr_devel("hvc_vio_probe() device %s, using %s protocol\n",
  210. vdev->dev.of_node->full_name,
  211. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
  212. /* Is it our boot one ? */
  213. if (hvterm_privs[0] == &hvterm_priv0 &&
  214. vdev->unit_address == hvterm_priv0.termno) {
  215. pv = hvterm_privs[0];
  216. termno = 0;
  217. pr_devel("->boot console, using termno 0\n");
  218. }
  219. /* nope, allocate a new one */
  220. else {
  221. for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
  222. if (!hvterm_privs[i])
  223. termno = i;
  224. pr_devel("->non-boot console, using termno %d\n", termno);
  225. if (termno < 0)
  226. return -ENODEV;
  227. pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
  228. if (!pv)
  229. return -ENOMEM;
  230. pv->termno = vdev->unit_address;
  231. pv->proto = proto;
  232. spin_lock_init(&pv->buf_lock);
  233. hvterm_privs[termno] = pv;
  234. hvsilib_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
  235. pv->termno, 0);
  236. }
  237. hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
  238. if (IS_ERR(hp))
  239. return PTR_ERR(hp);
  240. dev_set_drvdata(&vdev->dev, hp);
  241. return 0;
  242. }
  243. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  244. {
  245. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  246. int rc, termno;
  247. termno = hp->vtermno;
  248. rc = hvc_remove(hp);
  249. if (rc == 0) {
  250. if (hvterm_privs[termno] != &hvterm_priv0)
  251. kfree(hvterm_privs[termno]);
  252. hvterm_privs[termno] = NULL;
  253. }
  254. return rc;
  255. }
  256. static struct vio_driver hvc_vio_driver = {
  257. .id_table = hvc_driver_table,
  258. .probe = hvc_vio_probe,
  259. .remove = __devexit_p(hvc_vio_remove),
  260. .driver = {
  261. .name = hvc_driver_name,
  262. .owner = THIS_MODULE,
  263. }
  264. };
  265. static int __init hvc_vio_init(void)
  266. {
  267. int rc;
  268. if (firmware_has_feature(FW_FEATURE_ISERIES))
  269. return -EIO;
  270. /* Register as a vio device to receive callbacks */
  271. rc = vio_register_driver(&hvc_vio_driver);
  272. return rc;
  273. }
  274. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  275. static void __exit hvc_vio_exit(void)
  276. {
  277. vio_unregister_driver(&hvc_vio_driver);
  278. }
  279. module_exit(hvc_vio_exit);
  280. static void udbg_hvc_putc(char c)
  281. {
  282. int count = -1;
  283. if (c == '\n')
  284. udbg_hvc_putc('\r');
  285. do {
  286. switch(hvterm_priv0.proto) {
  287. case HV_PROTOCOL_RAW:
  288. count = hvterm_raw_put_chars(0, &c, 1);
  289. break;
  290. case HV_PROTOCOL_HVSI:
  291. count = hvterm_hvsi_put_chars(0, &c, 1);
  292. break;
  293. }
  294. } while(count == 0);
  295. }
  296. static int udbg_hvc_getc_poll(void)
  297. {
  298. int rc = 0;
  299. char c;
  300. switch(hvterm_priv0.proto) {
  301. case HV_PROTOCOL_RAW:
  302. rc = hvterm_raw_get_chars(0, &c, 1);
  303. break;
  304. case HV_PROTOCOL_HVSI:
  305. rc = hvterm_hvsi_get_chars(0, &c, 1);
  306. break;
  307. }
  308. if (!rc)
  309. return -1;
  310. return c;
  311. }
  312. static int udbg_hvc_getc(void)
  313. {
  314. int ch;
  315. for (;;) {
  316. ch = udbg_hvc_getc_poll();
  317. if (ch == -1) {
  318. /* This shouldn't be needed...but... */
  319. volatile unsigned long delay;
  320. for (delay=0; delay < 2000000; delay++)
  321. ;
  322. } else {
  323. return ch;
  324. }
  325. }
  326. }
  327. void __init hvc_vio_init_early(void)
  328. {
  329. struct device_node *stdout_node;
  330. const u32 *termno;
  331. const char *name;
  332. const struct hv_ops *ops;
  333. /* find the boot console from /chosen/stdout */
  334. if (!of_chosen)
  335. return;
  336. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  337. if (name == NULL)
  338. return;
  339. stdout_node = of_find_node_by_path(name);
  340. if (!stdout_node)
  341. return;
  342. name = of_get_property(stdout_node, "name", NULL);
  343. if (!name) {
  344. printk(KERN_WARNING "stdout node missing 'name' property!\n");
  345. goto out;
  346. }
  347. /* Check if it's a virtual terminal */
  348. if (strncmp(name, "vty", 3) != 0)
  349. goto out;
  350. termno = of_get_property(stdout_node, "reg", NULL);
  351. if (termno == NULL)
  352. goto out;
  353. hvterm_priv0.termno = *termno;
  354. spin_lock_init(&hvterm_priv0.buf_lock);
  355. hvterm_privs[0] = &hvterm_priv0;
  356. /* Check the protocol */
  357. if (of_device_is_compatible(stdout_node, "hvterm1")) {
  358. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  359. ops = &hvterm_raw_ops;
  360. }
  361. else if (of_device_is_compatible(stdout_node, "hvterm-protocol")) {
  362. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  363. ops = &hvterm_hvsi_ops;
  364. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  365. hvterm_priv0.termno, 1);
  366. /* HVSI, perform the handshake now */
  367. hvsilib_establish(&hvterm_priv0.hvsi);
  368. } else
  369. goto out;
  370. udbg_putc = udbg_hvc_putc;
  371. udbg_getc = udbg_hvc_getc;
  372. udbg_getc_poll = udbg_hvc_getc_poll;
  373. #ifdef HVC_OLD_HVSI
  374. /* When using the old HVSI driver don't register the HVC
  375. * backend for HVSI, only do udbg
  376. */
  377. if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
  378. goto out;
  379. #endif
  380. add_preferred_console("hvc", 0, NULL);
  381. hvc_instantiate(0, 0, ops);
  382. out:
  383. of_node_put(stdout_node);
  384. }
  385. /* call this from early_init() for a working debug console on
  386. * vterm capable LPAR machines
  387. */
  388. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
  389. void __init udbg_init_debug_lpar(void)
  390. {
  391. hvterm_privs[0] = &hvterm_priv0;
  392. hvterm_priv0.termno = 0;
  393. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  394. spin_lock_init(&hvterm_priv0.buf_lock);
  395. udbg_putc = udbg_hvc_putc;
  396. udbg_getc = udbg_hvc_getc;
  397. udbg_getc_poll = udbg_hvc_getc_poll;
  398. }
  399. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
  400. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
  401. void __init udbg_init_debug_lpar_hvsi(void)
  402. {
  403. hvterm_privs[0] = &hvterm_priv0;
  404. hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
  405. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  406. spin_lock_init(&hvterm_priv0.buf_lock);
  407. udbg_putc = udbg_hvc_putc;
  408. udbg_getc = udbg_hvc_getc;
  409. udbg_getc_poll = udbg_hvc_getc_poll;
  410. hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  411. hvterm_priv0.termno, 1);
  412. hvsilib_establish(&hvterm_priv0.hvsi);
  413. }
  414. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */