vio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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/slab.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 = kstrdup(path, GFP_KERNEL);
  82. if (!np->full_name) {
  83. kfree(np);
  84. return NULL;
  85. }
  86. of_node_set_flag(np, OF_DYNAMIC);
  87. kref_init(&np->kref);
  88. np->parent = of_node_get(parent);
  89. return np;
  90. }
  91. static void free_node(struct device_node *np)
  92. {
  93. struct property *next;
  94. struct property *prop;
  95. next = np->properties;
  96. while (next) {
  97. prop = next;
  98. next = prop->next;
  99. free_property(prop);
  100. }
  101. of_node_put(np->parent);
  102. kfree(np->full_name);
  103. kfree(np);
  104. }
  105. static int add_string_property(struct device_node *np, const char *name,
  106. const char *value)
  107. {
  108. struct property *nprop = new_property(name, strlen(value) + 1, value);
  109. if (!nprop)
  110. return 0;
  111. prom_add_property(np, nprop);
  112. return 1;
  113. }
  114. static int add_raw_property(struct device_node *np, const char *name,
  115. int length, const void *value)
  116. {
  117. struct property *nprop = new_property(name, length, value);
  118. if (!nprop)
  119. return 0;
  120. prom_add_property(np, nprop);
  121. return 1;
  122. }
  123. static struct device_node *do_device_node(struct device_node *parent,
  124. const char *name, u32 reg, u32 unit, const char *type,
  125. const char *compat, struct vio_resource *res)
  126. {
  127. struct device_node *np;
  128. char path[32];
  129. snprintf(path, sizeof(path), "/vdevice/%s@%08x", name, reg);
  130. np = new_node(path, parent);
  131. if (!np)
  132. return NULL;
  133. if (!add_string_property(np, "name", name) ||
  134. !add_string_property(np, "device_type", type) ||
  135. !add_string_property(np, "compatible", compat) ||
  136. !add_raw_property(np, "reg", sizeof(reg), &reg) ||
  137. !add_raw_property(np, "linux,unit_address",
  138. sizeof(unit), &unit)) {
  139. goto node_free;
  140. }
  141. if (res) {
  142. if (!add_raw_property(np, "linux,vio_rsrcname",
  143. sizeof(res->rsrcname), res->rsrcname) ||
  144. !add_raw_property(np, "linux,vio_type",
  145. sizeof(res->type), res->type) ||
  146. !add_raw_property(np, "linux,vio_model",
  147. sizeof(res->model), res->model))
  148. goto node_free;
  149. }
  150. np->name = of_get_property(np, "name", NULL);
  151. np->type = of_get_property(np, "device_type", NULL);
  152. of_attach_node(np);
  153. #ifdef CONFIG_PROC_DEVICETREE
  154. if (parent->pde) {
  155. struct proc_dir_entry *ent;
  156. ent = proc_mkdir(strrchr(np->full_name, '/') + 1, parent->pde);
  157. if (ent)
  158. proc_device_tree_add_node(np, ent);
  159. }
  160. #endif
  161. return np;
  162. node_free:
  163. free_node(np);
  164. return NULL;
  165. }
  166. /*
  167. * This is here so that we can dynamically add viodasd
  168. * devices without exposing all the above infrastructure.
  169. */
  170. struct vio_dev *vio_create_viodasd(u32 unit)
  171. {
  172. struct device_node *vio_root;
  173. struct device_node *np;
  174. struct vio_dev *vdev = NULL;
  175. vio_root = of_find_node_by_path("/vdevice");
  176. if (!vio_root)
  177. return NULL;
  178. np = do_device_node(vio_root, "viodasd", FIRST_VIODASD + unit, unit,
  179. "block", "IBM,iSeries-viodasd", NULL);
  180. of_node_put(vio_root);
  181. if (np) {
  182. vdev = vio_register_device_node(np);
  183. if (!vdev)
  184. free_node(np);
  185. }
  186. return vdev;
  187. }
  188. EXPORT_SYMBOL_GPL(vio_create_viodasd);
  189. static void __init handle_block_event(struct HvLpEvent *event)
  190. {
  191. struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
  192. struct vio_waitevent *pwe;
  193. if (event == NULL)
  194. /* Notification that a partition went away! */
  195. return;
  196. /* First, we should NEVER get an int here...only acks */
  197. if (hvlpevent_is_int(event)) {
  198. printk(KERN_WARNING "handle_viod_request: "
  199. "Yikes! got an int in viodasd event handler!\n");
  200. if (hvlpevent_need_ack(event)) {
  201. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  202. HvCallEvent_ackLpEvent(event);
  203. }
  204. return;
  205. }
  206. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  207. case vioblockopen:
  208. /*
  209. * Handle a response to an open request. We get all the
  210. * disk information in the response, so update it. The
  211. * correlation token contains a pointer to a waitevent
  212. * structure that has a completion in it. update the
  213. * return code in the waitevent structure and post the
  214. * completion to wake up the guy who sent the request
  215. */
  216. pwe = (struct vio_waitevent *)event->xCorrelationToken;
  217. pwe->rc = event->xRc;
  218. pwe->sub_result = bevent->sub_result;
  219. complete(&pwe->com);
  220. break;
  221. case vioblockclose:
  222. break;
  223. default:
  224. printk(KERN_WARNING "handle_viod_request: unexpected subtype!");
  225. if (hvlpevent_need_ack(event)) {
  226. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  227. HvCallEvent_ackLpEvent(event);
  228. }
  229. }
  230. }
  231. static void __init probe_disk(struct device_node *vio_root, u32 unit)
  232. {
  233. HvLpEvent_Rc hvrc;
  234. struct vio_waitevent we;
  235. u16 flags = 0;
  236. retry:
  237. init_completion(&we.com);
  238. /* Send the open event to OS/400 */
  239. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  240. HvLpEvent_Type_VirtualIo,
  241. viomajorsubtype_blockio | vioblockopen,
  242. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  243. viopath_sourceinst(viopath_hostLp),
  244. viopath_targetinst(viopath_hostLp),
  245. (u64)(unsigned long)&we, VIOVERSION << 16,
  246. ((u64)unit << 48) | ((u64)flags<< 32),
  247. 0, 0, 0);
  248. if (hvrc != 0) {
  249. printk(KERN_WARNING "probe_disk: bad rc on HV open %d\n",
  250. (int)hvrc);
  251. return;
  252. }
  253. wait_for_completion(&we.com);
  254. if (we.rc != 0) {
  255. if (flags != 0)
  256. return;
  257. /* try again with read only flag set */
  258. flags = vioblockflags_ro;
  259. goto retry;
  260. }
  261. /* Send the close event to OS/400. We DON'T expect a response */
  262. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  263. HvLpEvent_Type_VirtualIo,
  264. viomajorsubtype_blockio | vioblockclose,
  265. HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  266. viopath_sourceinst(viopath_hostLp),
  267. viopath_targetinst(viopath_hostLp),
  268. 0, VIOVERSION << 16,
  269. ((u64)unit << 48) | ((u64)flags << 32),
  270. 0, 0, 0);
  271. if (hvrc != 0) {
  272. printk(KERN_WARNING "probe_disk: "
  273. "bad rc sending event to OS/400 %d\n", (int)hvrc);
  274. return;
  275. }
  276. do_device_node(vio_root, "viodasd", FIRST_VIODASD + unit, unit,
  277. "block", "IBM,iSeries-viodasd", NULL);
  278. }
  279. static void __init get_viodasd_info(struct device_node *vio_root)
  280. {
  281. int rc;
  282. u32 unit;
  283. rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio, 2);
  284. if (rc) {
  285. printk(KERN_WARNING "get_viodasd_info: "
  286. "error opening path to host partition %d\n",
  287. viopath_hostLp);
  288. return;
  289. }
  290. /* Initialize our request handler */
  291. vio_setHandler(viomajorsubtype_blockio, handle_block_event);
  292. for (unit = 0; unit < HVMAXARCHITECTEDVIRTUALDISKS; unit++)
  293. probe_disk(vio_root, unit);
  294. vio_clearHandler(viomajorsubtype_blockio);
  295. viopath_close(viopath_hostLp, viomajorsubtype_blockio, 2);
  296. }
  297. static void __init handle_cd_event(struct HvLpEvent *event)
  298. {
  299. struct viocdlpevent *bevent;
  300. struct vio_waitevent *pwe;
  301. if (!event)
  302. /* Notification that a partition went away! */
  303. return;
  304. /* First, we should NEVER get an int here...only acks */
  305. if (hvlpevent_is_int(event)) {
  306. printk(KERN_WARNING "handle_cd_event: got an unexpected int\n");
  307. if (hvlpevent_need_ack(event)) {
  308. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  309. HvCallEvent_ackLpEvent(event);
  310. }
  311. return;
  312. }
  313. bevent = (struct viocdlpevent *)event;
  314. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  315. case viocdgetinfo:
  316. pwe = (struct vio_waitevent *)event->xCorrelationToken;
  317. pwe->rc = event->xRc;
  318. pwe->sub_result = bevent->sub_result;
  319. complete(&pwe->com);
  320. break;
  321. default:
  322. printk(KERN_WARNING "handle_cd_event: "
  323. "message with unexpected subtype %0x04X!\n",
  324. event->xSubtype & VIOMINOR_SUBTYPE_MASK);
  325. if (hvlpevent_need_ack(event)) {
  326. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  327. HvCallEvent_ackLpEvent(event);
  328. }
  329. }
  330. }
  331. static void __init get_viocd_info(struct device_node *vio_root)
  332. {
  333. HvLpEvent_Rc hvrc;
  334. u32 unit;
  335. struct vio_waitevent we;
  336. struct vio_resource *unitinfo;
  337. dma_addr_t unitinfo_dmaaddr;
  338. int ret;
  339. ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio, 2);
  340. if (ret) {
  341. printk(KERN_WARNING
  342. "get_viocd_info: error opening path to host partition %d\n",
  343. viopath_hostLp);
  344. return;
  345. }
  346. /* Initialize our request handler */
  347. vio_setHandler(viomajorsubtype_cdio, handle_cd_event);
  348. unitinfo = iseries_hv_alloc(
  349. sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS,
  350. &unitinfo_dmaaddr, GFP_ATOMIC);
  351. if (!unitinfo) {
  352. printk(KERN_WARNING
  353. "get_viocd_info: error allocating unitinfo\n");
  354. goto clear_handler;
  355. }
  356. memset(unitinfo, 0, sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS);
  357. init_completion(&we.com);
  358. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  359. HvLpEvent_Type_VirtualIo,
  360. viomajorsubtype_cdio | viocdgetinfo,
  361. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  362. viopath_sourceinst(viopath_hostLp),
  363. viopath_targetinst(viopath_hostLp),
  364. (u64)&we, VIOVERSION << 16, unitinfo_dmaaddr, 0,
  365. sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS, 0);
  366. if (hvrc != HvLpEvent_Rc_Good) {
  367. printk(KERN_WARNING
  368. "get_viocd_info: cdrom error sending event. rc %d\n",
  369. (int)hvrc);
  370. goto hv_free;
  371. }
  372. wait_for_completion(&we.com);
  373. if (we.rc) {
  374. printk(KERN_WARNING "get_viocd_info: bad rc %d:0x%04X\n",
  375. we.rc, we.sub_result);
  376. goto hv_free;
  377. }
  378. for (unit = 0; (unit < HVMAXARCHITECTEDVIRTUALCDROMS) &&
  379. unitinfo[unit].rsrcname[0]; unit++) {
  380. if (!do_device_node(vio_root, "viocd", FIRST_VIOCD + unit, unit,
  381. "block", "IBM,iSeries-viocd", &unitinfo[unit]))
  382. break;
  383. }
  384. hv_free:
  385. iseries_hv_free(sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS,
  386. unitinfo, unitinfo_dmaaddr);
  387. clear_handler:
  388. vio_clearHandler(viomajorsubtype_cdio);
  389. viopath_close(viopath_hostLp, viomajorsubtype_cdio, 2);
  390. }
  391. /* Handle interrupt events for tape */
  392. static void __init handle_tape_event(struct HvLpEvent *event)
  393. {
  394. struct vio_waitevent *we;
  395. struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  396. if (event == NULL)
  397. /* Notification that a partition went away! */
  398. return;
  399. we = (struct vio_waitevent *)event->xCorrelationToken;
  400. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  401. case viotapegetinfo:
  402. we->rc = tevent->sub_type_result;
  403. complete(&we->com);
  404. break;
  405. default:
  406. printk(KERN_WARNING "handle_tape_event: weird ack\n");
  407. }
  408. }
  409. static void __init get_viotape_info(struct device_node *vio_root)
  410. {
  411. HvLpEvent_Rc hvrc;
  412. u32 unit;
  413. struct vio_resource *unitinfo;
  414. dma_addr_t unitinfo_dmaaddr;
  415. size_t len = sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALTAPES;
  416. struct vio_waitevent we;
  417. int ret;
  418. init_completion(&we.com);
  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);