hvc_iseries.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * iSeries vio driver interface to hvc_console.c
  3. *
  4. * This code is based heavily on hvc_vio.c and viocons.c
  5. *
  6. * Copyright (C) 2006 Stephen Rothwell, IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <stdarg.h>
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/console.h>
  29. #include <asm/hvconsole.h>
  30. #include <asm/vio.h>
  31. #include <asm/prom.h>
  32. #include <asm/firmware.h>
  33. #include <asm/iseries/vio.h>
  34. #include <asm/iseries/hv_call.h>
  35. #include <asm/iseries/hv_lp_config.h>
  36. #include <asm/iseries/hv_lp_event.h>
  37. #include "hvc_console.h"
  38. #define VTTY_PORTS 10
  39. static DEFINE_SPINLOCK(consolelock);
  40. static DEFINE_SPINLOCK(consoleloglock);
  41. static const char hvc_driver_name[] = "hvc_console";
  42. #define IN_BUF_SIZE 200
  43. /*
  44. * Our port information.
  45. */
  46. static struct port_info {
  47. HvLpIndex lp;
  48. u64 seq; /* sequence number of last HV send */
  49. u64 ack; /* last ack from HV */
  50. struct hvc_struct *hp;
  51. int in_start;
  52. int in_end;
  53. unsigned char in_buf[IN_BUF_SIZE];
  54. } port_info[VTTY_PORTS] = {
  55. [ 0 ... VTTY_PORTS - 1 ] = {
  56. .lp = HvLpIndexInvalid
  57. }
  58. };
  59. #define viochar_is_console(pi) ((pi) == &port_info[0])
  60. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  61. {"serial", "IBM,iSeries-vty"},
  62. { "", "" }
  63. };
  64. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  65. static void hvlog(char *fmt, ...)
  66. {
  67. int i;
  68. unsigned long flags;
  69. va_list args;
  70. static char buf[256];
  71. spin_lock_irqsave(&consoleloglock, flags);
  72. va_start(args, fmt);
  73. i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
  74. va_end(args);
  75. buf[i++] = '\r';
  76. HvCall_writeLogBuffer(buf, i);
  77. spin_unlock_irqrestore(&consoleloglock, flags);
  78. }
  79. /*
  80. * Initialize the common fields in a charLpEvent
  81. */
  82. static void init_data_event(struct viocharlpevent *viochar, HvLpIndex lp)
  83. {
  84. struct HvLpEvent *hev = &viochar->event;
  85. memset(viochar, 0, sizeof(struct viocharlpevent));
  86. hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
  87. HV_LP_EVENT_INT;
  88. hev->xType = HvLpEvent_Type_VirtualIo;
  89. hev->xSubtype = viomajorsubtype_chario | viochardata;
  90. hev->xSourceLp = HvLpConfig_getLpIndex();
  91. hev->xTargetLp = lp;
  92. hev->xSizeMinus1 = sizeof(struct viocharlpevent);
  93. hev->xSourceInstanceId = viopath_sourceinst(lp);
  94. hev->xTargetInstanceId = viopath_targetinst(lp);
  95. }
  96. static int get_chars(uint32_t vtermno, char *buf, int count)
  97. {
  98. struct port_info *pi;
  99. int n = 0;
  100. unsigned long flags;
  101. if (vtermno >= VTTY_PORTS)
  102. return -EINVAL;
  103. if (count == 0)
  104. return 0;
  105. pi = &port_info[vtermno];
  106. spin_lock_irqsave(&consolelock, flags);
  107. if (pi->in_end == 0)
  108. goto done;
  109. n = pi->in_end - pi->in_start;
  110. if (n > count)
  111. n = count;
  112. memcpy(buf, &pi->in_buf[pi->in_start], n);
  113. pi->in_start += n;
  114. if (pi->in_start == pi->in_end) {
  115. pi->in_start = 0;
  116. pi->in_end = 0;
  117. }
  118. done:
  119. spin_unlock_irqrestore(&consolelock, flags);
  120. return n;
  121. }
  122. static int put_chars(uint32_t vtermno, const char *buf, int count)
  123. {
  124. struct viocharlpevent *viochar;
  125. struct port_info *pi;
  126. HvLpEvent_Rc hvrc;
  127. unsigned long flags;
  128. int sent = 0;
  129. if (vtermno >= VTTY_PORTS)
  130. return -EINVAL;
  131. pi = &port_info[vtermno];
  132. spin_lock_irqsave(&consolelock, flags);
  133. if (viochar_is_console(pi) && !viopath_isactive(pi->lp)) {
  134. HvCall_writeLogBuffer(buf, count);
  135. sent = count;
  136. goto done;
  137. }
  138. viochar = vio_get_event_buffer(viomajorsubtype_chario);
  139. if (viochar == NULL) {
  140. hvlog("\n\rviocons: Can't get viochar buffer.");
  141. goto done;
  142. }
  143. while ((count > 0) && ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
  144. int len;
  145. len = (count > VIOCHAR_MAX_DATA) ? VIOCHAR_MAX_DATA : count;
  146. if (viochar_is_console(pi))
  147. HvCall_writeLogBuffer(buf, len);
  148. init_data_event(viochar, pi->lp);
  149. viochar->len = len;
  150. viochar->event.xCorrelationToken = pi->seq++;
  151. viochar->event.xSizeMinus1 =
  152. offsetof(struct viocharlpevent, data) + len;
  153. memcpy(viochar->data, buf, len);
  154. hvrc = HvCallEvent_signalLpEvent(&viochar->event);
  155. if (hvrc)
  156. hvlog("\n\rerror sending event! return code %d\n\r",
  157. (int)hvrc);
  158. sent += len;
  159. count -= len;
  160. buf += len;
  161. }
  162. vio_free_event_buffer(viomajorsubtype_chario, viochar);
  163. done:
  164. spin_unlock_irqrestore(&consolelock, flags);
  165. return sent;
  166. }
  167. static const struct hv_ops hvc_get_put_ops = {
  168. .get_chars = get_chars,
  169. .put_chars = put_chars,
  170. .notifier_add = notifier_add_irq,
  171. .notifier_del = notifier_del_irq,
  172. .notifier_hangup = notifier_hangup_irq,
  173. };
  174. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  175. const struct vio_device_id *id)
  176. {
  177. struct hvc_struct *hp;
  178. struct port_info *pi;
  179. /* probed with invalid parameters. */
  180. if (!vdev || !id)
  181. return -EPERM;
  182. if (vdev->unit_address >= VTTY_PORTS)
  183. return -ENODEV;
  184. pi = &port_info[vdev->unit_address];
  185. hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
  186. VIOCHAR_MAX_DATA);
  187. if (IS_ERR(hp))
  188. return PTR_ERR(hp);
  189. pi->hp = hp;
  190. dev_set_drvdata(&vdev->dev, pi);
  191. return 0;
  192. }
  193. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  194. {
  195. struct port_info *pi = dev_get_drvdata(&vdev->dev);
  196. struct hvc_struct *hp = pi->hp;
  197. return hvc_remove(hp);
  198. }
  199. static struct vio_driver hvc_vio_driver = {
  200. .id_table = hvc_driver_table,
  201. .probe = hvc_vio_probe,
  202. .remove = __devexit_p(hvc_vio_remove),
  203. .driver = {
  204. .name = hvc_driver_name,
  205. .owner = THIS_MODULE,
  206. }
  207. };
  208. static void hvc_open_event(struct HvLpEvent *event)
  209. {
  210. unsigned long flags;
  211. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  212. u8 port = cevent->virtual_device;
  213. struct port_info *pi;
  214. int reject = 0;
  215. if (hvlpevent_is_ack(event)) {
  216. if (port >= VTTY_PORTS)
  217. return;
  218. spin_lock_irqsave(&consolelock, flags);
  219. pi = &port_info[port];
  220. if (event->xRc == HvLpEvent_Rc_Good) {
  221. pi->seq = pi->ack = 0;
  222. /*
  223. * This line allows connections from the primary
  224. * partition but once one is connected from the
  225. * primary partition nothing short of a reboot
  226. * of linux will allow access from the hosting
  227. * partition again without a required iSeries fix.
  228. */
  229. pi->lp = event->xTargetLp;
  230. }
  231. spin_unlock_irqrestore(&consolelock, flags);
  232. if (event->xRc != HvLpEvent_Rc_Good)
  233. printk(KERN_WARNING
  234. "hvc: handle_open_event: event->xRc == (%d).\n",
  235. event->xRc);
  236. if (event->xCorrelationToken != 0) {
  237. atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
  238. atomic_set(aptr, 1);
  239. } else
  240. printk(KERN_WARNING
  241. "hvc: weird...got open ack without atomic\n");
  242. return;
  243. }
  244. /* This had better require an ack, otherwise complain */
  245. if (!hvlpevent_need_ack(event)) {
  246. printk(KERN_WARNING "hvc: viocharopen without ack bit!\n");
  247. return;
  248. }
  249. spin_lock_irqsave(&consolelock, flags);
  250. /* Make sure this is a good virtual tty */
  251. if (port >= VTTY_PORTS) {
  252. event->xRc = HvLpEvent_Rc_SubtypeError;
  253. cevent->subtype_result_code = viorc_openRejected;
  254. /*
  255. * Flag state here since we can't printk while holding
  256. * the consolelock spinlock.
  257. */
  258. reject = 1;
  259. } else {
  260. pi = &port_info[port];
  261. if ((pi->lp != HvLpIndexInvalid) &&
  262. (pi->lp != event->xSourceLp)) {
  263. /*
  264. * If this is tty is already connected to a different
  265. * partition, fail.
  266. */
  267. event->xRc = HvLpEvent_Rc_SubtypeError;
  268. cevent->subtype_result_code = viorc_openRejected;
  269. reject = 2;
  270. } else {
  271. pi->lp = event->xSourceLp;
  272. event->xRc = HvLpEvent_Rc_Good;
  273. cevent->subtype_result_code = viorc_good;
  274. pi->seq = pi->ack = 0;
  275. }
  276. }
  277. spin_unlock_irqrestore(&consolelock, flags);
  278. if (reject == 1)
  279. printk(KERN_WARNING "hvc: open rejected: bad virtual tty.\n");
  280. else if (reject == 2)
  281. printk(KERN_WARNING "hvc: open rejected: console in exclusive "
  282. "use by another partition.\n");
  283. /* Return the acknowledgement */
  284. HvCallEvent_ackLpEvent(event);
  285. }
  286. /*
  287. * Handle a close charLpEvent. This should ONLY be an Interrupt because the
  288. * virtual console should never actually issue a close event to the hypervisor
  289. * because the virtual console never goes away. A close event coming from the
  290. * hypervisor simply means that there are no client consoles connected to the
  291. * virtual console.
  292. */
  293. static void hvc_close_event(struct HvLpEvent *event)
  294. {
  295. unsigned long flags;
  296. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  297. u8 port = cevent->virtual_device;
  298. if (!hvlpevent_is_int(event)) {
  299. printk(KERN_WARNING
  300. "hvc: got unexpected close acknowledgement\n");
  301. return;
  302. }
  303. if (port >= VTTY_PORTS) {
  304. printk(KERN_WARNING
  305. "hvc: close message from invalid virtual device.\n");
  306. return;
  307. }
  308. /* For closes, just mark the console partition invalid */
  309. spin_lock_irqsave(&consolelock, flags);
  310. if (port_info[port].lp == event->xSourceLp)
  311. port_info[port].lp = HvLpIndexInvalid;
  312. spin_unlock_irqrestore(&consolelock, flags);
  313. }
  314. static void hvc_data_event(struct HvLpEvent *event)
  315. {
  316. unsigned long flags;
  317. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  318. struct port_info *pi;
  319. int n;
  320. u8 port = cevent->virtual_device;
  321. if (port >= VTTY_PORTS) {
  322. printk(KERN_WARNING "hvc: data on invalid virtual device %d\n",
  323. port);
  324. return;
  325. }
  326. if (cevent->len == 0)
  327. return;
  328. /*
  329. * Change 05/01/2003 - Ryan Arnold: If a partition other than
  330. * the current exclusive partition tries to send us data
  331. * events then just drop them on the floor because we don't
  332. * want his stinking data. He isn't authorized to receive
  333. * data because he wasn't the first one to get the console,
  334. * therefore he shouldn't be allowed to send data either.
  335. * This will work without an iSeries fix.
  336. */
  337. pi = &port_info[port];
  338. if (pi->lp != event->xSourceLp)
  339. return;
  340. spin_lock_irqsave(&consolelock, flags);
  341. n = IN_BUF_SIZE - pi->in_end;
  342. if (n > cevent->len)
  343. n = cevent->len;
  344. if (n > 0) {
  345. memcpy(&pi->in_buf[pi->in_end], cevent->data, n);
  346. pi->in_end += n;
  347. }
  348. spin_unlock_irqrestore(&consolelock, flags);
  349. if (n == 0)
  350. printk(KERN_WARNING "hvc: input buffer overflow\n");
  351. }
  352. static void hvc_ack_event(struct HvLpEvent *event)
  353. {
  354. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  355. unsigned long flags;
  356. u8 port = cevent->virtual_device;
  357. if (port >= VTTY_PORTS) {
  358. printk(KERN_WARNING "hvc: data on invalid virtual device\n");
  359. return;
  360. }
  361. spin_lock_irqsave(&consolelock, flags);
  362. port_info[port].ack = event->xCorrelationToken;
  363. spin_unlock_irqrestore(&consolelock, flags);
  364. }
  365. static void hvc_config_event(struct HvLpEvent *event)
  366. {
  367. struct viocharlpevent *cevent = (struct viocharlpevent *)event;
  368. if (cevent->data[0] == 0x01)
  369. printk(KERN_INFO "hvc: window resized to %d: %d: %d: %d\n",
  370. cevent->data[1], cevent->data[2],
  371. cevent->data[3], cevent->data[4]);
  372. else
  373. printk(KERN_WARNING "hvc: unknown config event\n");
  374. }
  375. static void hvc_handle_event(struct HvLpEvent *event)
  376. {
  377. int charminor;
  378. if (event == NULL)
  379. return;
  380. charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
  381. switch (charminor) {
  382. case viocharopen:
  383. hvc_open_event(event);
  384. break;
  385. case viocharclose:
  386. hvc_close_event(event);
  387. break;
  388. case viochardata:
  389. hvc_data_event(event);
  390. break;
  391. case viocharack:
  392. hvc_ack_event(event);
  393. break;
  394. case viocharconfig:
  395. hvc_config_event(event);
  396. break;
  397. default:
  398. if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
  399. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  400. HvCallEvent_ackLpEvent(event);
  401. }
  402. }
  403. }
  404. static int __init send_open(HvLpIndex remoteLp, void *sem)
  405. {
  406. return HvCallEvent_signalLpEventFast(remoteLp,
  407. HvLpEvent_Type_VirtualIo,
  408. viomajorsubtype_chario | viocharopen,
  409. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  410. viopath_sourceinst(remoteLp),
  411. viopath_targetinst(remoteLp),
  412. (u64)(unsigned long)sem, VIOVERSION << 16,
  413. 0, 0, 0, 0);
  414. }
  415. static int __init hvc_vio_init(void)
  416. {
  417. atomic_t wait_flag;
  418. int rc;
  419. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  420. return -EIO;
  421. /* +2 for fudge */
  422. rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
  423. viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
  424. if (rc)
  425. printk(KERN_WARNING "hvc: error opening to primary %d\n", rc);
  426. if (viopath_hostLp == HvLpIndexInvalid)
  427. vio_set_hostlp();
  428. /*
  429. * And if the primary is not the same as the hosting LP, open to the
  430. * hosting lp
  431. */
  432. if ((viopath_hostLp != HvLpIndexInvalid) &&
  433. (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
  434. printk(KERN_INFO "hvc: open path to hosting (%d)\n",
  435. viopath_hostLp);
  436. rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
  437. VIOCHAR_WINDOW + 2); /* +2 for fudge */
  438. if (rc)
  439. printk(KERN_WARNING
  440. "error opening to partition %d: %d\n",
  441. viopath_hostLp, rc);
  442. }
  443. if (vio_setHandler(viomajorsubtype_chario, hvc_handle_event) < 0)
  444. printk(KERN_WARNING
  445. "hvc: error seting handler for console events!\n");
  446. /*
  447. * First, try to open the console to the hosting lp.
  448. * Wait on a semaphore for the response.
  449. */
  450. atomic_set(&wait_flag, 0);
  451. if ((viopath_isactive(viopath_hostLp)) &&
  452. (send_open(viopath_hostLp, &wait_flag) == 0)) {
  453. printk(KERN_INFO "hvc: hosting partition %d\n", viopath_hostLp);
  454. while (atomic_read(&wait_flag) == 0)
  455. mb();
  456. atomic_set(&wait_flag, 0);
  457. }
  458. /*
  459. * If we don't have an active console, try the primary
  460. */
  461. if ((!viopath_isactive(port_info[0].lp)) &&
  462. (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
  463. (send_open(HvLpConfig_getPrimaryLpIndex(), &wait_flag) == 0)) {
  464. printk(KERN_INFO "hvc: opening console to primary partition\n");
  465. while (atomic_read(&wait_flag) == 0)
  466. mb();
  467. }
  468. /* Register as a vio device to receive callbacks */
  469. rc = vio_register_driver(&hvc_vio_driver);
  470. return rc;
  471. }
  472. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  473. static void __exit hvc_vio_exit(void)
  474. {
  475. vio_unregister_driver(&hvc_vio_driver);
  476. }
  477. module_exit(hvc_vio_exit);
  478. /* the device tree order defines our numbering */
  479. static int __init hvc_find_vtys(void)
  480. {
  481. struct device_node *vty;
  482. int num_found = 0;
  483. for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
  484. vty = of_find_node_by_name(vty, "vty")) {
  485. const uint32_t *vtermno;
  486. /* We have statically defined space for only a certain number
  487. * of console adapters.
  488. */
  489. if ((num_found >= MAX_NR_HVC_CONSOLES) ||
  490. (num_found >= VTTY_PORTS)) {
  491. of_node_put(vty);
  492. break;
  493. }
  494. vtermno = of_get_property(vty, "reg", NULL);
  495. if (!vtermno)
  496. continue;
  497. if (!of_device_is_compatible(vty, "IBM,iSeries-vty"))
  498. continue;
  499. if (num_found == 0)
  500. add_preferred_console("hvc", 0, NULL);
  501. hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
  502. ++num_found;
  503. }
  504. return num_found;
  505. }
  506. console_initcall(hvc_find_vtys);