vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 <asm/firmware.h>
  29. #include <asm/iseries/vio.h>
  30. #include <asm/iseries/iommu.h>
  31. #include <asm/iseries/hv_types.h>
  32. #include <asm/iseries/hv_lp_event.h>
  33. #define FIRST_VTY 0
  34. #define NUM_VTYS 1
  35. #define FIRST_VSCSI (FIRST_VTY + NUM_VTYS)
  36. #define NUM_VSCSIS 1
  37. #define FIRST_VLAN (FIRST_VSCSI + NUM_VSCSIS)
  38. #define NUM_VLANS HVMAXARCHITECTEDVIRTUALLANS
  39. #define FIRST_VIODASD (FIRST_VLAN + NUM_VLANS)
  40. #define NUM_VIODASDS HVMAXARCHITECTEDVIRTUALDISKS
  41. #define FIRST_VIOCD (FIRST_VIODASD + NUM_VIODASDS)
  42. #define NUM_VIOCDS HVMAXARCHITECTEDVIRTUALCDROMS
  43. #define FIRST_VIOTAPE (FIRST_VIOCD + NUM_VIOCDS)
  44. #define NUM_VIOTAPES HVMAXARCHITECTEDVIRTUALTAPES
  45. struct vio_waitevent {
  46. struct completion com;
  47. int rc;
  48. u16 sub_result;
  49. };
  50. struct vio_resource {
  51. char rsrcname[10];
  52. char type[4];
  53. char model[3];
  54. };
  55. static struct property * __init new_property(const char *name, int length,
  56. const void *value)
  57. {
  58. struct property *np = kzalloc(sizeof(*np) + strlen(name) + 1 + length,
  59. GFP_KERNEL);
  60. if (!np)
  61. return NULL;
  62. np->name = (char *)(np + 1);
  63. np->value = np->name + strlen(name) + 1;
  64. strcpy(np->name, name);
  65. memcpy(np->value, value, length);
  66. np->length = length;
  67. return np;
  68. }
  69. static void __init free_property(struct property *np)
  70. {
  71. kfree(np);
  72. }
  73. static struct device_node * __init new_node(const char *path,
  74. struct device_node *parent)
  75. {
  76. struct device_node *np = kzalloc(sizeof(*np), GFP_KERNEL);
  77. if (!np)
  78. return NULL;
  79. np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
  80. if (!np->full_name) {
  81. kfree(np);
  82. return NULL;
  83. }
  84. strcpy(np->full_name, path);
  85. of_node_set_flag(np, OF_DYNAMIC);
  86. kref_init(&np->kref);
  87. np->parent = of_node_get(parent);
  88. return np;
  89. }
  90. static void __init free_node(struct device_node *np)
  91. {
  92. struct property *next;
  93. struct property *prop;
  94. next = np->properties;
  95. while (next) {
  96. prop = next;
  97. next = prop->next;
  98. free_property(prop);
  99. }
  100. of_node_put(np->parent);
  101. kfree(np->full_name);
  102. kfree(np);
  103. }
  104. static int __init add_string_property(struct device_node *np, const char *name,
  105. const char *value)
  106. {
  107. struct property *nprop = new_property(name, strlen(value) + 1, value);
  108. if (!nprop)
  109. return 0;
  110. prom_add_property(np, nprop);
  111. return 1;
  112. }
  113. static int __init add_raw_property(struct device_node *np, const char *name,
  114. int length, const void *value)
  115. {
  116. struct property *nprop = new_property(name, length, value);
  117. if (!nprop)
  118. return 0;
  119. prom_add_property(np, nprop);
  120. return 1;
  121. }
  122. static void __init handle_cd_event(struct HvLpEvent *event)
  123. {
  124. struct viocdlpevent *bevent;
  125. struct vio_waitevent *pwe;
  126. if (!event)
  127. /* Notification that a partition went away! */
  128. return;
  129. /* First, we should NEVER get an int here...only acks */
  130. if (hvlpevent_is_int(event)) {
  131. printk(KERN_WARNING "handle_cd_event: got an unexpected int\n");
  132. if (hvlpevent_need_ack(event)) {
  133. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  134. HvCallEvent_ackLpEvent(event);
  135. }
  136. return;
  137. }
  138. bevent = (struct viocdlpevent *)event;
  139. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  140. case viocdgetinfo:
  141. pwe = (struct vio_waitevent *)event->xCorrelationToken;
  142. pwe->rc = event->xRc;
  143. pwe->sub_result = bevent->sub_result;
  144. complete(&pwe->com);
  145. break;
  146. default:
  147. printk(KERN_WARNING "handle_cd_event: "
  148. "message with unexpected subtype %0x04X!\n",
  149. event->xSubtype & VIOMINOR_SUBTYPE_MASK);
  150. if (hvlpevent_need_ack(event)) {
  151. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  152. HvCallEvent_ackLpEvent(event);
  153. }
  154. }
  155. }
  156. static void __init get_viocd_info(struct device_node *vio_root)
  157. {
  158. HvLpEvent_Rc hvrc;
  159. u32 unit;
  160. struct vio_waitevent we;
  161. struct vio_resource *unitinfo;
  162. dma_addr_t unitinfo_dmaaddr;
  163. int ret;
  164. ret = viopath_open(viopath_hostLp, viomajorsubtype_cdio, 2);
  165. if (ret) {
  166. printk(KERN_WARNING
  167. "get_viocd_info: error opening path to host partition %d\n",
  168. viopath_hostLp);
  169. return;
  170. }
  171. /* Initialize our request handler */
  172. vio_setHandler(viomajorsubtype_cdio, handle_cd_event);
  173. unitinfo = iseries_hv_alloc(
  174. sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS,
  175. &unitinfo_dmaaddr, GFP_ATOMIC);
  176. if (!unitinfo) {
  177. printk(KERN_WARNING
  178. "get_viocd_info: error allocating unitinfo\n");
  179. goto clear_handler;
  180. }
  181. memset(unitinfo, 0, sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS);
  182. init_completion(&we.com);
  183. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  184. HvLpEvent_Type_VirtualIo,
  185. viomajorsubtype_cdio | viocdgetinfo,
  186. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  187. viopath_sourceinst(viopath_hostLp),
  188. viopath_targetinst(viopath_hostLp),
  189. (u64)&we, VIOVERSION << 16, unitinfo_dmaaddr, 0,
  190. sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS, 0);
  191. if (hvrc != HvLpEvent_Rc_Good) {
  192. printk(KERN_WARNING
  193. "get_viocd_info: cdrom error sending event. rc %d\n",
  194. (int)hvrc);
  195. goto hv_free;
  196. }
  197. wait_for_completion(&we.com);
  198. if (we.rc) {
  199. printk(KERN_WARNING "get_viocd_info: bad rc %d:0x%04X\n",
  200. we.rc, we.sub_result);
  201. goto hv_free;
  202. }
  203. for (unit = 0; (unit < HVMAXARCHITECTEDVIRTUALCDROMS) &&
  204. unitinfo[unit].rsrcname[0]; unit++) {
  205. struct device_node *np;
  206. char name[64];
  207. u32 reg = FIRST_VIOCD + unit;
  208. snprintf(name, sizeof(name), "/vdevice/viocd@%08x", reg);
  209. np = new_node(name, vio_root);
  210. if (!np)
  211. goto hv_free;
  212. if (!add_string_property(np, "name", "viocd") ||
  213. !add_string_property(np, "device_type", "block") ||
  214. !add_string_property(np, "compatible",
  215. "IBM,iSeries-viocd") ||
  216. !add_raw_property(np, "reg", sizeof(reg), &reg) ||
  217. !add_raw_property(np, "linux,unit_address",
  218. sizeof(unit), &unit) ||
  219. !add_raw_property(np, "linux,vio_rsrcname",
  220. sizeof(unitinfo[unit].rsrcname),
  221. unitinfo[unit].rsrcname) ||
  222. !add_raw_property(np, "linux,vio_type",
  223. sizeof(unitinfo[unit].type),
  224. unitinfo[unit].type) ||
  225. !add_raw_property(np, "linux,vio_model",
  226. sizeof(unitinfo[unit].model),
  227. unitinfo[unit].model))
  228. goto node_free;
  229. np->name = of_get_property(np, "name", NULL);
  230. np->type = of_get_property(np, "device_type", NULL);
  231. of_attach_node(np);
  232. #ifdef CONFIG_PROC_DEVICETREE
  233. if (vio_root->pde) {
  234. struct proc_dir_entry *ent;
  235. ent = proc_mkdir(strrchr(np->full_name, '/') + 1,
  236. vio_root->pde);
  237. if (ent)
  238. proc_device_tree_add_node(np, ent);
  239. }
  240. #endif
  241. continue;
  242. node_free:
  243. free_node(np);
  244. break;
  245. }
  246. hv_free:
  247. iseries_hv_free(sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALCDROMS,
  248. unitinfo, unitinfo_dmaaddr);
  249. clear_handler:
  250. vio_clearHandler(viomajorsubtype_cdio);
  251. viopath_close(viopath_hostLp, viomajorsubtype_cdio, 2);
  252. }
  253. /* Handle interrupt events for tape */
  254. static void __init handle_tape_event(struct HvLpEvent *event)
  255. {
  256. struct vio_waitevent *we;
  257. struct viotapelpevent *tevent = (struct viotapelpevent *)event;
  258. if (event == NULL)
  259. /* Notification that a partition went away! */
  260. return;
  261. we = (struct vio_waitevent *)event->xCorrelationToken;
  262. switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  263. case viotapegetinfo:
  264. we->rc = tevent->sub_type_result;
  265. complete(&we->com);
  266. break;
  267. default:
  268. printk(KERN_WARNING "handle_tape_event: weird ack\n");
  269. }
  270. }
  271. static void __init get_viotape_info(struct device_node *vio_root)
  272. {
  273. HvLpEvent_Rc hvrc;
  274. u32 unit;
  275. struct vio_resource *unitinfo;
  276. dma_addr_t unitinfo_dmaaddr;
  277. size_t len = sizeof(*unitinfo) * HVMAXARCHITECTEDVIRTUALTAPES;
  278. struct vio_waitevent we;
  279. int ret;
  280. ret = viopath_open(viopath_hostLp, viomajorsubtype_tape, 2);
  281. if (ret) {
  282. printk(KERN_WARNING "get_viotape_info: "
  283. "error on viopath_open to hostlp %d\n", ret);
  284. return;
  285. }
  286. vio_setHandler(viomajorsubtype_tape, handle_tape_event);
  287. unitinfo = iseries_hv_alloc(len, &unitinfo_dmaaddr, GFP_ATOMIC);
  288. if (!unitinfo)
  289. goto clear_handler;
  290. memset(unitinfo, 0, len);
  291. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  292. HvLpEvent_Type_VirtualIo,
  293. viomajorsubtype_tape | viotapegetinfo,
  294. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  295. viopath_sourceinst(viopath_hostLp),
  296. viopath_targetinst(viopath_hostLp),
  297. (u64)(unsigned long)&we, VIOVERSION << 16,
  298. unitinfo_dmaaddr, len, 0, 0);
  299. if (hvrc != HvLpEvent_Rc_Good) {
  300. printk(KERN_WARNING "get_viotape_info: hv error on op %d\n",
  301. (int)hvrc);
  302. goto hv_free;
  303. }
  304. wait_for_completion(&we.com);
  305. for (unit = 0; (unit < HVMAXARCHITECTEDVIRTUALTAPES) &&
  306. unitinfo[unit].rsrcname[0]; unit++) {
  307. struct device_node *np;
  308. char name[64];
  309. u32 reg = FIRST_VIOTAPE + unit;
  310. snprintf(name, sizeof(name), "/vdevice/viotape@%08x", reg);
  311. np = new_node(name, vio_root);
  312. if (!np)
  313. goto hv_free;
  314. if (!add_string_property(np, "name", "viotape") ||
  315. !add_string_property(np, "device_type", "byte") ||
  316. !add_string_property(np, "compatible",
  317. "IBM,iSeries-viotape") ||
  318. !add_raw_property(np, "reg", sizeof(reg), &reg) ||
  319. !add_raw_property(np, "linux,unit_address",
  320. sizeof(unit), &unit) ||
  321. !add_raw_property(np, "linux,vio_rsrcname",
  322. sizeof(unitinfo[unit].rsrcname),
  323. unitinfo[unit].rsrcname) ||
  324. !add_raw_property(np, "linux,vio_type",
  325. sizeof(unitinfo[unit].type),
  326. unitinfo[unit].type) ||
  327. !add_raw_property(np, "linux,vio_model",
  328. sizeof(unitinfo[unit].model),
  329. unitinfo[unit].model))
  330. goto node_free;
  331. np->name = of_get_property(np, "name", NULL);
  332. np->type = of_get_property(np, "device_type", NULL);
  333. of_attach_node(np);
  334. #ifdef CONFIG_PROC_DEVICETREE
  335. if (vio_root->pde) {
  336. struct proc_dir_entry *ent;
  337. ent = proc_mkdir(strrchr(np->full_name, '/') + 1,
  338. vio_root->pde);
  339. if (ent)
  340. proc_device_tree_add_node(np, ent);
  341. }
  342. #endif
  343. continue;
  344. node_free:
  345. free_node(np);
  346. break;
  347. }
  348. hv_free:
  349. iseries_hv_free(len, unitinfo, unitinfo_dmaaddr);
  350. clear_handler:
  351. vio_clearHandler(viomajorsubtype_tape);
  352. viopath_close(viopath_hostLp, viomajorsubtype_tape, 2);
  353. }
  354. static int __init iseries_vio_init(void)
  355. {
  356. struct device_node *vio_root;
  357. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  358. return -ENODEV;
  359. iommu_vio_init();
  360. vio_root = of_find_node_by_path("/vdevice");
  361. if (!vio_root)
  362. return -ENODEV;
  363. if (viopath_hostLp == HvLpIndexInvalid) {
  364. vio_set_hostlp();
  365. /* If we don't have a host, bail out */
  366. if (viopath_hostLp == HvLpIndexInvalid)
  367. goto put_node;
  368. }
  369. get_viocd_info(vio_root);
  370. get_viotape_info(vio_root);
  371. return 0;
  372. put_node:
  373. of_node_put(vio_root);
  374. return -ENODEV;
  375. }
  376. arch_initcall(iseries_vio_init);