hvc_iseries.c 15 KB

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