hvc_iseries.c 15 KB

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