vio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. static struct property * __init new_property(const char *name, int length,
  46. const void *value)
  47. {
  48. struct property *np = kzalloc(sizeof(*np) + strlen(name) + 1 + length,
  49. GFP_KERNEL);
  50. if (!np)
  51. return NULL;
  52. np->name = (char *)(np + 1);
  53. np->value = np->name + strlen(name) + 1;
  54. strcpy(np->name, name);
  55. memcpy(np->value, value, length);
  56. np->length = length;
  57. return np;
  58. }
  59. static void __init free_property(struct property *np)
  60. {
  61. kfree(np);
  62. }
  63. static struct device_node * __init new_node(const char *path,
  64. struct device_node *parent)
  65. {
  66. struct device_node *np = kzalloc(sizeof(*np), GFP_KERNEL);
  67. if (!np)
  68. return NULL;
  69. np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
  70. if (!np->full_name) {
  71. kfree(np);
  72. return NULL;
  73. }
  74. strcpy(np->full_name, path);
  75. of_node_set_flag(np, OF_DYNAMIC);
  76. kref_init(&np->kref);
  77. np->parent = of_node_get(parent);
  78. return np;
  79. }
  80. static void __init free_node(struct device_node *np)
  81. {
  82. struct property *next;
  83. struct property *prop;
  84. next = np->properties;
  85. while (next) {
  86. prop = next;
  87. next = prop->next;
  88. free_property(prop);
  89. }
  90. of_node_put(np->parent);
  91. kfree(np->full_name);
  92. kfree(np);
  93. }
  94. static int __init add_string_property(struct device_node *np, const char *name,
  95. const char *value)
  96. {
  97. struct property *nprop = new_property(name, strlen(value) + 1, value);
  98. if (!nprop)
  99. return 0;
  100. prom_add_property(np, nprop);
  101. return 1;
  102. }
  103. static int __init add_raw_property(struct device_node *np, const char *name,
  104. int length, const void *value)
  105. {
  106. struct property *nprop = new_property(name, length, value);
  107. if (!nprop)
  108. return 0;
  109. prom_add_property(np, nprop);
  110. return 1;
  111. }
  112. struct viocd_waitevent {
  113. struct completion com;
  114. int rc;
  115. u16 sub_result;
  116. };
  117. struct cdrom_info {
  118. char rsrcname[10];
  119. char type[4];
  120. char model[3];
  121. };
  122. static void __init handle_cd_event(struct HvLpEvent *event)
  123. {
  124. struct viocdlpevent *bevent;
  125. struct viocd_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 viocd_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 viocd_waitevent we;
  161. struct cdrom_info *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. static int __init iseries_vio_init(void)
  254. {
  255. struct device_node *vio_root;
  256. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  257. return -ENODEV;
  258. iommu_vio_init();
  259. vio_root = of_find_node_by_path("/vdevice");
  260. if (!vio_root)
  261. return -ENODEV;
  262. if (viopath_hostLp == HvLpIndexInvalid) {
  263. vio_set_hostlp();
  264. /* If we don't have a host, bail out */
  265. if (viopath_hostLp == HvLpIndexInvalid)
  266. goto put_node;
  267. }
  268. get_viocd_info(vio_root);
  269. return 0;
  270. put_node:
  271. of_node_put(vio_root);
  272. return -ENODEV;
  273. }
  274. arch_initcall(iseries_vio_init);