vio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Legacy iSeries specific vio initialisation
  3. * that needs to be built in (not a module).
  4. *
  5. * © Copyright 2007 IBM Corporation
  6. * Author: Stephen Rothwell
  7. * Some parts collected from various other files
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/of.h>
  24. #include <linux/init.h>
  25. #include <linux/gfp.h>
  26. #include <linux/completion.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/module.h>
  29. #include <asm/firmware.h>
  30. #include <asm/vio.h>
  31. #include <asm/iseries/vio.h>
  32. #include <asm/iseries/iommu.h>
  33. #include <asm/iseries/hv_types.h>
  34. #include <asm/iseries/hv_lp_event.h>
  35. #define FIRST_VTY 0
  36. #define NUM_VTYS 1
  37. #define FIRST_VSCSI (FIRST_VTY + NUM_VTYS)
  38. #define NUM_VSCSIS 1
  39. #define FIRST_VLAN (FIRST_VSCSI + NUM_VSCSIS)
  40. #define NUM_VLANS HVMAXARCHITECTEDVIRTUALLANS
  41. #define FIRST_VIODASD (FIRST_VLAN + NUM_VLANS)
  42. #define NUM_VIODASDS HVMAXARCHITECTEDVIRTUALDISKS
  43. #define FIRST_VIOCD (FIRST_VIODASD + NUM_VIODASDS)
  44. #define NUM_VIOCDS HVMAXARCHITECTEDVIRTUALCDROMS
  45. #define FIRST_VIOTAPE (FIRST_VIOCD + NUM_VIOCDS)
  46. #define NUM_VIOTAPES HVMAXARCHITECTEDVIRTUALTAPES
  47. struct vio_waitevent {
  48. struct completion com;
  49. int rc;
  50. u16 sub_result;
  51. };
  52. struct vio_resource {
  53. char rsrcname[10];
  54. char type[4];
  55. char model[3];
  56. };
  57. static struct property *new_property(const char *name, int length,
  58. const void *value)
  59. {
  60. struct property *np = kzalloc(sizeof(*np) + strlen(name) + 1 + length,
  61. GFP_KERNEL);
  62. if (!np)
  63. return NULL;
  64. np->name = (char *)(np + 1);
  65. np->value = np->name + strlen(name) + 1;
  66. strcpy(np->name, name);
  67. memcpy(np->value, value, length);
  68. np->length = length;
  69. return np;
  70. }
  71. static void free_property(struct property *np)
  72. {
  73. kfree(np);
  74. }
  75. static struct device_node *new_node(const char *path,
  76. struct device_node *parent)
  77. {
  78. struct device_node *np = kzalloc(sizeof(*np), GFP_KERNEL);
  79. if (!np)
  80. return NULL;
  81. np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
  82. if (!np->full_name) {
  83. kfree(np);
  84. return NULL;
  85. }
  86. strcpy(np->full_name, path);
  87. of_node_set_flag(np, OF_DYNAMIC);
  88. kref_init(&np->kref);
  89. np->parent = of_node_get(parent);
  90. return np;
  91. }
  92. static void free_node(struct device_node *np)
  93. {
  94. struct property *next;
  95. struct property *prop;
  96. next = np->properties;
  97. while (next) {
  98. prop = next;
  99. next = prop->next;
  100. free_property(prop);
  101. }
  102. of_node_put(np->parent);
  103. kfree(np->full_name);
  104. kfree(np);
  105. }
  106. static int add_string_property(struct device_node *np, const char *name,
  107. const char *value)
  108. {
  109. struct property *nprop = new_property(name, strlen(value) + 1, value);
  110. if (!nprop)
  111. return 0;
  112. prom_add_property(np, nprop);
  113. return 1;
  114. }
  115. static int add_raw_property(struct device_node *np, const char *name,
  116. int length, const void *value)
  117. {
  118. struct property *nprop = new_property(name, length, value);
  119. if (!nprop)
  120. return 0;
  121. prom_add_property(np, nprop);
  122. return 1;
  123. }
  124. static struct device_node *do_device_node(struct device_node *parent,
  125. const char *name, u32 reg, u32 unit, const char *type,
  126. const char *compat, struct vio_resource *res)
  127. {
  128. struct device_node *np;
  129. char path[32];
  130. snprintf(path, sizeof(path), "/vdevice/%s@%08x", name, reg);
  131. np = new_node(path, parent);
  132. if (!np)
  133. return NULL;
  134. if (!add_string_property(np, "name", name) ||
  135. !add_string_property(np, "device_type", type) ||
  136. !add_string_property(np, "compatible", compat) ||
  137. !add_raw_property(np, "reg", sizeof(reg), &reg) ||
  138. !add_raw_property(np, "linux,unit_address",
  139. sizeof(unit), &unit)) {
  140. goto node_free;
  141. }
  142. if (res) {
  143. if (!add_raw_property(np, "linux,vio_rsrcname",
  144. sizeof(res->rsrcname), res->rsrcname) ||
  145. !add_raw_property(np, "linux,vio_type",
  146. sizeof(res->type), res->type) ||
  147. !add_raw_property(np, "linux,vio_model",
  148. sizeof(res->model), res->model))
  149. goto node_free;
  150. }
  151. np->name = of_get_property(np, "name", NULL);
  152. np->type = of_get_property(np, "device_type", NULL);
  153. of_attach_node(np);
  154. #ifdef CONFIG_PROC_DEVICETREE
  155. if (parent->pde) {
  156. struct proc_dir_entry *ent;
  157. ent = proc_mkdir(strrchr(np->full_name, '/') + 1, parent->pde);
  158. if (ent)
  159. proc_device_tree_add_node(np, ent);
  160. }
  161. #endif
  162. return np;
  163. node_free:
  164. free_node(np);
  165. return NULL;
  166. }
  167. /*
  168. * This is here so that we can dynamically add viodasd
  169. * devices without exposing all the above infrastructure.
  170. */
  171. struct vio_dev *vio_create_viodasd(u32 unit)
  172. {
  173. struct device_node *vio_root;
  174. struct device_node *np;
  175. struct vio_dev *vdev = NULL;
  176. vio_root = of_find_node_by_path("/vdevice");
  177. if (!vio_root)
  178. return NULL;
  179. np = do_device_node(vio_root, "viodasd", FIRST_VIODASD + unit, unit,
  180. "block", "IBM,iSeries-viodasd", NULL);
  181. of_node_put(vio_root);
  182. if (np) {
  183. vdev = vio_register_device_node(np);
  184. if (!vdev)
  185. free_node(np);
  186. }
  187. return vdev;
  188. }
  189. EXPORT_SYMBOL_GPL(vio_create_viodasd);
  190. static void __init handle_block_event(struct HvLpEvent *event)
  191. {
  192. struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
  193. struct vio_waitevent *pwe;
  194. if (event == NULL)
  195. /* Notification that a partition went away! */
  196. return;
  197. /* First, we should NEVER get an int here...only acks */
  198. if (hvlpevent_is_int(event)) {
  199. printk(KERN_WARNING "handle_viod_request: "
  200. "Yikes! got an int in viodasd event handler!\n");
  201. if (hvlpevent_need_ack(event)) {
  202. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  203. HvCallEvent_ackLpEvent(event);
  204. }
  205. return;
  206. }
  207. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  208. case vioblockopen:
  209. /*
  210. * Handle a response to an open request. We get all the
  211. * disk information in the response, so update it. The
  212. * correlation token contains a pointer to a waitevent
  213. * structure that has a completion in it. update the
  214. * return code in the waitevent structure and post the
  215. * completion to wake up the guy who sent the request
  216. */
  217. pwe = (struct vio_waitevent *)event->xCorrelationToken;
  218. pwe->rc = event->xRc;
  219. pwe->sub_result = bevent->sub_result;
  220. complete(&pwe->com);
  221. break;
  222. case vioblockclose:
  223. break;
  224. default:
  225. printk(KERN_WARNING "handle_viod_request: unexpected subtype!");
  226. if (hvlpevent_need_ack(event)) {
  227. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  228. HvCallEvent_ackLpEvent(event);
  229. }
  230. }
  231. }
  232. static void __init probe_disk(struct device_node *vio_root, u32 unit)
  233. {
  234. HvLpEvent_Rc hvrc;
  235. struct vio_waitevent we;
  236. u16 flags = 0;
  237. retry:
  238. init_completion(&we.com);
  239. /* Send the open event to OS/400 */
  240. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  241. HvLpEvent_Type_VirtualIo,
  242. viomajorsubtype_blockio | vioblockopen,
  243. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  244. viopath_sourceinst(viopath_hostLp),
  245. viopath_targetinst(viopath_hostLp),
  246. (u64)(unsigned long)&we, VIOVERSION << 16,
  247. ((u64)unit << 48) | ((u64)flags<< 32),
  248. 0, 0, 0);
  249. if (hvrc != 0) {
  250. printk(KERN_WARNING "probe_disk: bad rc on HV open %d\n",
  251. (int)hvrc);
  252. return;
  253. }
  254. wait_for_completion(&we.com);
  255. if (we.rc != 0) {
  256. if (flags != 0)
  257. return;
  258. /* try again with read only flag set */
  259. flags = vioblockflags_ro;
  260. goto retry;
  261. }
  262. /* Send the close event to OS/400. We DON'T expect a response */
  263. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  264. HvLpEvent_Type_VirtualIo,
  265. viomajorsubtype_blockio | vioblockclose,
  266. HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  267. viopath_sourceinst(viopath_hostLp),
  268. viopath_targetinst(viopath_hostLp),
  269. 0, VIOVERSION << 16,
  270. ((u64)unit << 48) | ((u64)flags << 32),
  271. 0, 0, 0);
  272. if (hvrc != 0) {
  273. printk(KERN_WARNING "probe_disk: "
  274. "bad rc sending event to OS/400 %d\n", (int)hvrc);
  275. return;
  276. }
  277. do_device_node(vio_root, "viodasd", FIRST_VIODASD + unit, unit,
  278. "block", "IBM,iSeries-viodasd", NULL);
  279. }
  280. static void __init get_viodasd_info(struct device_node *vio_root)
  281. {
  282. int rc;
  283. u32 unit;
  284. rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio, 2);
  285. if (rc) {
  286. printk(KERN_WARNING "get_viodasd_info: "
  287. "error opening path to host partition %d\n",
  288. viopath_hostLp);
  289. return;
  290. }
  291. /* Initialize our request handler */
  292. vio_setHandler(viomajorsubtype_blockio, handle_block_event);
  293. for (unit = 0; unit < HVMAXARCHITECTEDVIRTUALDISKS; unit++)
  294. probe_disk(vio_root, unit);
  295. vio_clearHandler(viomajorsubtype_blockio);
  296. viopath_close(viopath_hostLp, viomajorsubtype_blockio, 2);
  297. }
  298. static void __init handle_cd_event(struct HvLpEvent *event)
  299. {
  300. struct viocdlpevent *bevent;
  301. struct vio_waitevent *pwe;
  302. if (!event)
  303. /* Notification that a partition went away! */
  304. return;
  305. /* First, we should NEVER get an int here...only acks */
  306. if (hvlpevent_is_int(event)) {
  307. printk(KERN_WARNING "handle_cd_event: got an unexpected int\n");
  308. if (hvlpevent_need_ack(event)) {
  309. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  310. HvCallEvent_ackLpEvent(event);
  311. }
  312. return;
  313. }
  314. bevent = (struct viocdlpevent *)event;
  315. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  316. case viocdgetinfo:
  317. pwe = (struct vio_waitevent *)event->xCorrelationToken;
  318. pwe->rc = event->xRc;
  319. pwe->sub_result = bevent->sub_result;
  320. complete(&pwe->com);
  321. break;
  322. default:
  323. printk(KERN_WARNING "handle_cd_event: "
  324. "message with unexpected subtype %0x04X!\n",
  325. event->xSubtype & VIOMINOR_SUBTYPE_MASK);
  326. if (hvlpevent_need_ack(event)) {
  327. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  328. HvCallEvent_ackLpEvent(event);
  329. }
  330. }
  331. }
  332. static void __init get_viocd_info(struct device_node *vio_root)
  333. {
  334. HvLpEvent_Rc hvrc;
  335. u32 unit;
  336. struct vio_waitevent we;
  337. struct vio_resource *unitinfo;
  338. dma_addr_t unitinfo_dmaaddr;
  339. int ret;
  340. ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio, 2);
  341. if (ret) {
  342. printk(KERN_WARNING
  343. "get_viocd_info: error opening path to host partition %d\n",
  344. viopath_hostLp);
  345. return;
  346. }
  347. /* Initialize our request handler */
  348. vio_setHandler(viomajorsubtype_cdio, handle_cd_event);
  349. unitinfo = iseries_hv_alloc(
  350. sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS,
  351. &unitinfo_dmaaddr, GFP_ATOMIC);
  352. if (!unitinfo) {
  353. printk(KERN_WARNING
  354. "get_viocd_info: error allocating unitinfo\n");
  355. goto clear_handler;
  356. }
  357. memset(unitinfo, 0, sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS);
  358. init_completion(&we.com);
  359. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  360. HvLpEvent_Type_VirtualIo,
  361. viomajorsubtype_cdio | viocdgetinfo,
  362. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  363. viopath_sourceinst(viopath_hostLp),
  364. viopath_targetinst(viopath_hostLp),
  365. (u64)&we, VIOVERSION << 16, unitinfo_dmaaddr, 0,
  366. sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS, 0);
  367. if (hvrc != HvLpEvent_Rc_Good) {
  368. printk(KERN_WARNING
  369. "get_viocd_info: cdrom error sending event. rc %d\n",
  370. (int)hvrc);
  371. goto hv_free;
  372. }
  373. wait_for_completion(&we.com);
  374. if (we.rc) {
  375. printk(KERN_WARNING "get_viocd_info: bad rc %d:0x%04X\n",
  376. we.rc, we.sub_result);
  377. goto hv_free;
  378. }
  379. for (unit = 0; (unit < HVMAXARCHITECTEDVIRTUALCDROMS) &&
  380. unitinfo[unit].rsrcname[0]; unit++) {
  381. if (!do_device_node(vio_root, "viocd", FIRST_VIOCD + unit, unit,
  382. "block", "IBM,iSeries-viocd", &unitinfo[unit]))
  383. break;
  384. }
  385. hv_free:
  386. iseries_hv_free(sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS,
  387. unitinfo, unitinfo_dmaaddr);
  388. clear_handler:
  389. vio_clearHandler(viomajorsubtype_cdio);
  390. viopath_close(viopath_hostLp, viomajorsubtype_cdio, 2);
  391. }
  392. /* Handle interrupt events for tape */
  393. static void __init handle_tape_event(struct HvLpEvent *event)
  394. {
  395. struct vio_waitevent *we;
  396. struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  397. if (event == NULL)
  398. /* Notification that a partition went away! */
  399. return;
  400. we = (struct vio_waitevent *)event->xCorrelationToken;
  401. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  402. case viotapegetinfo:
  403. we->rc = tevent->sub_type_result;
  404. complete(&we->com);
  405. break;
  406. default:
  407. printk(KERN_WARNING "handle_tape_event: weird ack\n");
  408. }
  409. }
  410. static void __init get_viotape_info(struct device_node *vio_root)
  411. {
  412. HvLpEvent_Rc hvrc;
  413. u32 unit;
  414. struct vio_resource *unitinfo;
  415. dma_addr_t unitinfo_dmaaddr;
  416. size_t len = sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALTAPES;
  417. struct vio_waitevent we;
  418. int ret;
  419. ret = viopath_open(viopath_hostLp, viomajorsubtype_tape, 2);
  420. if (ret) {
  421. printk(KERN_WARNING "get_viotape_info: "
  422. "error on viopath_open to hostlp %d\n", ret);
  423. return;
  424. }
  425. vio_setHandler(viomajorsubtype_tape, handle_tape_event);
  426. unitinfo = iseries_hv_alloc(len, &unitinfo_dmaaddr, GFP_ATOMIC);
  427. if (!unitinfo)
  428. goto clear_handler;
  429. memset(unitinfo, 0, len);
  430. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  431. HvLpEvent_Type_VirtualIo,
  432. viomajorsubtype_tape | viotapegetinfo,
  433. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  434. viopath_sourceinst(viopath_hostLp),
  435. viopath_targetinst(viopath_hostLp),
  436. (u64)(unsigned long)&we, VIOVERSION << 16,
  437. unitinfo_dmaaddr, len, 0, 0);
  438. if (hvrc != HvLpEvent_Rc_Good) {
  439. printk(KERN_WARNING "get_viotape_info: hv error on op %d\n",
  440. (int)hvrc);
  441. goto hv_free;
  442. }
  443. wait_for_completion(&we.com);
  444. for (unit = 0; (unit < HVMAXARCHITECTEDVIRTUALTAPES) &&
  445. unitinfo[unit].rsrcname[0]; unit++) {
  446. if (!do_device_node(vio_root, "viotape", FIRST_VIOTAPE + unit,
  447. unit, "byte", "IBM,iSeries-viotape",
  448. &unitinfo[unit]))
  449. break;
  450. }
  451. hv_free:
  452. iseries_hv_free(len, unitinfo, unitinfo_dmaaddr);
  453. clear_handler:
  454. vio_clearHandler(viomajorsubtype_tape);
  455. viopath_close(viopath_hostLp, viomajorsubtype_tape, 2);
  456. }
  457. static int __init iseries_vio_init(void)
  458. {
  459. struct device_node *vio_root;
  460. int ret = -ENODEV;
  461. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  462. goto out;
  463. iommu_vio_init();
  464. vio_root = of_find_node_by_path("/vdevice");
  465. if (!vio_root)
  466. goto out;
  467. if (viopath_hostLp == HvLpIndexInvalid) {
  468. vio_set_hostlp();
  469. /* If we don't have a host, bail out */
  470. if (viopath_hostLp == HvLpIndexInvalid)
  471. goto put_node;
  472. }
  473. get_viodasd_info(vio_root);
  474. get_viocd_info(vio_root);
  475. get_viotape_info(vio_root);
  476. ret = 0;
  477. put_node:
  478. of_node_put(vio_root);
  479. out:
  480. return ret;
  481. }
  482. arch_initcall(iseries_vio_init);