ehca_main.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * module start stop, hca detection
  5. *
  6. * Authors: Heiko J Schick <schickhj@de.ibm.com>
  7. * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  8. * Joachim Fenkes <fenkes@de.ibm.com>
  9. *
  10. * Copyright (c) 2005 IBM Corporation
  11. *
  12. * All rights reserved.
  13. *
  14. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  15. * BSD.
  16. *
  17. * OpenIB BSD License
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions are met:
  21. *
  22. * Redistributions of source code must retain the above copyright notice, this
  23. * list of conditions and the following disclaimer.
  24. *
  25. * Redistributions in binary form must reproduce the above copyright notice,
  26. * this list of conditions and the following disclaimer in the documentation
  27. * and/or other materials
  28. * provided with the distribution.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  37. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  38. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. #ifdef CONFIG_PPC_64K_PAGES
  43. #include <linux/slab.h>
  44. #endif
  45. #include <linux/notifier.h>
  46. #include <linux/memory.h>
  47. #include "ehca_classes.h"
  48. #include "ehca_iverbs.h"
  49. #include "ehca_mrmw.h"
  50. #include "ehca_tools.h"
  51. #include "hcp_if.h"
  52. #define HCAD_VERSION "0026"
  53. MODULE_LICENSE("Dual BSD/GPL");
  54. MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
  55. MODULE_DESCRIPTION("IBM eServer HCA InfiniBand Device Driver");
  56. MODULE_VERSION(HCAD_VERSION);
  57. static int ehca_open_aqp1 = 0;
  58. static int ehca_hw_level = 0;
  59. static int ehca_poll_all_eqs = 1;
  60. int ehca_debug_level = 0;
  61. int ehca_nr_ports = 2;
  62. int ehca_use_hp_mr = 0;
  63. int ehca_port_act_time = 30;
  64. int ehca_static_rate = -1;
  65. int ehca_scaling_code = 0;
  66. int ehca_lock_hcalls = -1;
  67. int ehca_max_cq = -1;
  68. int ehca_max_qp = -1;
  69. module_param_named(open_aqp1, ehca_open_aqp1, bool, S_IRUGO);
  70. module_param_named(debug_level, ehca_debug_level, int, S_IRUGO);
  71. module_param_named(hw_level, ehca_hw_level, int, S_IRUGO);
  72. module_param_named(nr_ports, ehca_nr_ports, int, S_IRUGO);
  73. module_param_named(use_hp_mr, ehca_use_hp_mr, bool, S_IRUGO);
  74. module_param_named(port_act_time, ehca_port_act_time, int, S_IRUGO);
  75. module_param_named(poll_all_eqs, ehca_poll_all_eqs, bool, S_IRUGO);
  76. module_param_named(static_rate, ehca_static_rate, int, S_IRUGO);
  77. module_param_named(scaling_code, ehca_scaling_code, bool, S_IRUGO);
  78. module_param_named(lock_hcalls, ehca_lock_hcalls, bool, S_IRUGO);
  79. module_param_named(number_of_cqs, ehca_max_cq, int, S_IRUGO);
  80. module_param_named(number_of_qps, ehca_max_qp, int, S_IRUGO);
  81. MODULE_PARM_DESC(open_aqp1,
  82. "Open AQP1 on startup (default: no)");
  83. MODULE_PARM_DESC(debug_level,
  84. "Amount of debug output (0: none (default), 1: traces, "
  85. "2: some dumps, 3: lots)");
  86. MODULE_PARM_DESC(hw_level,
  87. "Hardware level (0: autosensing (default), "
  88. "0x10..0x14: eHCA, 0x20..0x23: eHCA2)");
  89. MODULE_PARM_DESC(nr_ports,
  90. "number of connected ports (-1: autodetect, 1: port one only, "
  91. "2: two ports (default)");
  92. MODULE_PARM_DESC(use_hp_mr,
  93. "Use high performance MRs (default: no)");
  94. MODULE_PARM_DESC(port_act_time,
  95. "Time to wait for port activation (default: 30 sec)");
  96. MODULE_PARM_DESC(poll_all_eqs,
  97. "Poll all event queues periodically (default: yes)");
  98. MODULE_PARM_DESC(static_rate,
  99. "Set permanent static rate (default: no static rate)");
  100. MODULE_PARM_DESC(scaling_code,
  101. "Enable scaling code (default: no)");
  102. MODULE_PARM_DESC(lock_hcalls,
  103. "Serialize all hCalls made by the driver "
  104. "(default: autodetect)");
  105. MODULE_PARM_DESC(number_of_cqs,
  106. "Max number of CQs which can be allocated "
  107. "(default: autodetect)");
  108. MODULE_PARM_DESC(number_of_qps,
  109. "Max number of QPs which can be allocated "
  110. "(default: autodetect)");
  111. DEFINE_RWLOCK(ehca_qp_idr_lock);
  112. DEFINE_RWLOCK(ehca_cq_idr_lock);
  113. DEFINE_IDR(ehca_qp_idr);
  114. DEFINE_IDR(ehca_cq_idr);
  115. static LIST_HEAD(shca_list); /* list of all registered ehcas */
  116. static DEFINE_SPINLOCK(shca_list_lock);
  117. static struct timer_list poll_eqs_timer;
  118. #ifdef CONFIG_PPC_64K_PAGES
  119. static struct kmem_cache *ctblk_cache;
  120. void *ehca_alloc_fw_ctrlblock(gfp_t flags)
  121. {
  122. void *ret = kmem_cache_zalloc(ctblk_cache, flags);
  123. if (!ret)
  124. ehca_gen_err("Out of memory for ctblk");
  125. return ret;
  126. }
  127. void ehca_free_fw_ctrlblock(void *ptr)
  128. {
  129. if (ptr)
  130. kmem_cache_free(ctblk_cache, ptr);
  131. }
  132. #endif
  133. int ehca2ib_return_code(u64 ehca_rc)
  134. {
  135. switch (ehca_rc) {
  136. case H_SUCCESS:
  137. return 0;
  138. case H_RESOURCE: /* Resource in use */
  139. case H_BUSY:
  140. return -EBUSY;
  141. case H_NOT_ENOUGH_RESOURCES: /* insufficient resources */
  142. case H_CONSTRAINED: /* resource constraint */
  143. case H_NO_MEM:
  144. return -ENOMEM;
  145. default:
  146. return -EINVAL;
  147. }
  148. }
  149. static int ehca_create_slab_caches(void)
  150. {
  151. int ret;
  152. ret = ehca_init_pd_cache();
  153. if (ret) {
  154. ehca_gen_err("Cannot create PD SLAB cache.");
  155. return ret;
  156. }
  157. ret = ehca_init_cq_cache();
  158. if (ret) {
  159. ehca_gen_err("Cannot create CQ SLAB cache.");
  160. goto create_slab_caches2;
  161. }
  162. ret = ehca_init_qp_cache();
  163. if (ret) {
  164. ehca_gen_err("Cannot create QP SLAB cache.");
  165. goto create_slab_caches3;
  166. }
  167. ret = ehca_init_av_cache();
  168. if (ret) {
  169. ehca_gen_err("Cannot create AV SLAB cache.");
  170. goto create_slab_caches4;
  171. }
  172. ret = ehca_init_mrmw_cache();
  173. if (ret) {
  174. ehca_gen_err("Cannot create MR&MW SLAB cache.");
  175. goto create_slab_caches5;
  176. }
  177. ret = ehca_init_small_qp_cache();
  178. if (ret) {
  179. ehca_gen_err("Cannot create small queue SLAB cache.");
  180. goto create_slab_caches6;
  181. }
  182. #ifdef CONFIG_PPC_64K_PAGES
  183. ctblk_cache = kmem_cache_create("ehca_cache_ctblk",
  184. EHCA_PAGESIZE, H_CB_ALIGNMENT,
  185. SLAB_HWCACHE_ALIGN,
  186. NULL);
  187. if (!ctblk_cache) {
  188. ehca_gen_err("Cannot create ctblk SLAB cache.");
  189. ehca_cleanup_small_qp_cache();
  190. goto create_slab_caches6;
  191. }
  192. #endif
  193. return 0;
  194. create_slab_caches6:
  195. ehca_cleanup_mrmw_cache();
  196. create_slab_caches5:
  197. ehca_cleanup_av_cache();
  198. create_slab_caches4:
  199. ehca_cleanup_qp_cache();
  200. create_slab_caches3:
  201. ehca_cleanup_cq_cache();
  202. create_slab_caches2:
  203. ehca_cleanup_pd_cache();
  204. return ret;
  205. }
  206. static void ehca_destroy_slab_caches(void)
  207. {
  208. ehca_cleanup_small_qp_cache();
  209. ehca_cleanup_mrmw_cache();
  210. ehca_cleanup_av_cache();
  211. ehca_cleanup_qp_cache();
  212. ehca_cleanup_cq_cache();
  213. ehca_cleanup_pd_cache();
  214. #ifdef CONFIG_PPC_64K_PAGES
  215. if (ctblk_cache)
  216. kmem_cache_destroy(ctblk_cache);
  217. #endif
  218. }
  219. #define EHCA_HCAAVER EHCA_BMASK_IBM(32, 39)
  220. #define EHCA_REVID EHCA_BMASK_IBM(40, 63)
  221. static struct cap_descr {
  222. u64 mask;
  223. char *descr;
  224. } hca_cap_descr[] = {
  225. { HCA_CAP_AH_PORT_NR_CHECK, "HCA_CAP_AH_PORT_NR_CHECK" },
  226. { HCA_CAP_ATOMIC, "HCA_CAP_ATOMIC" },
  227. { HCA_CAP_AUTO_PATH_MIG, "HCA_CAP_AUTO_PATH_MIG" },
  228. { HCA_CAP_BAD_P_KEY_CTR, "HCA_CAP_BAD_P_KEY_CTR" },
  229. { HCA_CAP_SQD_RTS_PORT_CHANGE, "HCA_CAP_SQD_RTS_PORT_CHANGE" },
  230. { HCA_CAP_CUR_QP_STATE_MOD, "HCA_CAP_CUR_QP_STATE_MOD" },
  231. { HCA_CAP_INIT_TYPE, "HCA_CAP_INIT_TYPE" },
  232. { HCA_CAP_PORT_ACTIVE_EVENT, "HCA_CAP_PORT_ACTIVE_EVENT" },
  233. { HCA_CAP_Q_KEY_VIOL_CTR, "HCA_CAP_Q_KEY_VIOL_CTR" },
  234. { HCA_CAP_WQE_RESIZE, "HCA_CAP_WQE_RESIZE" },
  235. { HCA_CAP_RAW_PACKET_MCAST, "HCA_CAP_RAW_PACKET_MCAST" },
  236. { HCA_CAP_SHUTDOWN_PORT, "HCA_CAP_SHUTDOWN_PORT" },
  237. { HCA_CAP_RC_LL_QP, "HCA_CAP_RC_LL_QP" },
  238. { HCA_CAP_SRQ, "HCA_CAP_SRQ" },
  239. { HCA_CAP_UD_LL_QP, "HCA_CAP_UD_LL_QP" },
  240. { HCA_CAP_RESIZE_MR, "HCA_CAP_RESIZE_MR" },
  241. { HCA_CAP_MINI_QP, "HCA_CAP_MINI_QP" },
  242. { HCA_CAP_H_ALLOC_RES_SYNC, "HCA_CAP_H_ALLOC_RES_SYNC" },
  243. };
  244. static int ehca_sense_attributes(struct ehca_shca *shca)
  245. {
  246. int i, ret = 0;
  247. u64 h_ret;
  248. struct hipz_query_hca *rblock;
  249. struct hipz_query_port *port;
  250. const char *loc_code;
  251. static const u32 pgsize_map[] = {
  252. HCA_CAP_MR_PGSIZE_4K, 0x1000,
  253. HCA_CAP_MR_PGSIZE_64K, 0x10000,
  254. HCA_CAP_MR_PGSIZE_1M, 0x100000,
  255. HCA_CAP_MR_PGSIZE_16M, 0x1000000,
  256. };
  257. ehca_gen_dbg("Probing adapter %s...",
  258. shca->ofdev->node->full_name);
  259. loc_code = of_get_property(shca->ofdev->node, "ibm,loc-code", NULL);
  260. if (loc_code)
  261. ehca_gen_dbg(" ... location lode=%s", loc_code);
  262. rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  263. if (!rblock) {
  264. ehca_gen_err("Cannot allocate rblock memory.");
  265. return -ENOMEM;
  266. }
  267. h_ret = hipz_h_query_hca(shca->ipz_hca_handle, rblock);
  268. if (h_ret != H_SUCCESS) {
  269. ehca_gen_err("Cannot query device properties. h_ret=%li",
  270. h_ret);
  271. ret = -EPERM;
  272. goto sense_attributes1;
  273. }
  274. if (ehca_nr_ports == 1)
  275. shca->num_ports = 1;
  276. else
  277. shca->num_ports = (u8)rblock->num_ports;
  278. ehca_gen_dbg(" ... found %x ports", rblock->num_ports);
  279. if (ehca_hw_level == 0) {
  280. u32 hcaaver;
  281. u32 revid;
  282. hcaaver = EHCA_BMASK_GET(EHCA_HCAAVER, rblock->hw_ver);
  283. revid = EHCA_BMASK_GET(EHCA_REVID, rblock->hw_ver);
  284. ehca_gen_dbg(" ... hardware version=%x:%x", hcaaver, revid);
  285. if (hcaaver == 1) {
  286. if (revid <= 3)
  287. shca->hw_level = 0x10 | (revid + 1);
  288. else
  289. shca->hw_level = 0x14;
  290. } else if (hcaaver == 2) {
  291. if (revid == 0)
  292. shca->hw_level = 0x21;
  293. else if (revid == 0x10)
  294. shca->hw_level = 0x22;
  295. else if (revid == 0x20 || revid == 0x21)
  296. shca->hw_level = 0x23;
  297. }
  298. if (!shca->hw_level) {
  299. ehca_gen_warn("unknown hardware version"
  300. " - assuming default level");
  301. shca->hw_level = 0x22;
  302. }
  303. } else
  304. shca->hw_level = ehca_hw_level;
  305. ehca_gen_dbg(" ... hardware level=%x", shca->hw_level);
  306. shca->hca_cap = rblock->hca_cap_indicators;
  307. ehca_gen_dbg(" ... HCA capabilities:");
  308. for (i = 0; i < ARRAY_SIZE(hca_cap_descr); i++)
  309. if (EHCA_BMASK_GET(hca_cap_descr[i].mask, shca->hca_cap))
  310. ehca_gen_dbg(" %s", hca_cap_descr[i].descr);
  311. /* Autodetect hCall locking -- the "H_ALLOC_RESOURCE synced" flag is
  312. * a firmware property, so it's valid across all adapters
  313. */
  314. if (ehca_lock_hcalls == -1)
  315. ehca_lock_hcalls = !(shca->hca_cap & HCA_CAP_H_ALLOC_RES_SYNC);
  316. /* translate supported MR page sizes; always support 4K */
  317. shca->hca_cap_mr_pgsize = EHCA_PAGESIZE;
  318. for (i = 0; i < ARRAY_SIZE(pgsize_map); i += 2)
  319. if (rblock->memory_page_size_supported & pgsize_map[i])
  320. shca->hca_cap_mr_pgsize |= pgsize_map[i + 1];
  321. /* Set maximum number of CQs and QPs to calculate EQ size */
  322. if (shca->max_num_qps == -1)
  323. shca->max_num_qps = min_t(int, rblock->max_qp,
  324. EHCA_MAX_NUM_QUEUES);
  325. else if (shca->max_num_qps < 1 || shca->max_num_qps > rblock->max_qp) {
  326. ehca_gen_warn("The requested number of QPs is out of range "
  327. "(1 - %i) specified by HW. Value is set to %i",
  328. rblock->max_qp, rblock->max_qp);
  329. shca->max_num_qps = rblock->max_qp;
  330. }
  331. if (shca->max_num_cqs == -1)
  332. shca->max_num_cqs = min_t(int, rblock->max_cq,
  333. EHCA_MAX_NUM_QUEUES);
  334. else if (shca->max_num_cqs < 1 || shca->max_num_cqs > rblock->max_cq) {
  335. ehca_gen_warn("The requested number of CQs is out of range "
  336. "(1 - %i) specified by HW. Value is set to %i",
  337. rblock->max_cq, rblock->max_cq);
  338. }
  339. /* query max MTU from first port -- it's the same for all ports */
  340. port = (struct hipz_query_port *)rblock;
  341. h_ret = hipz_h_query_port(shca->ipz_hca_handle, 1, port);
  342. if (h_ret != H_SUCCESS) {
  343. ehca_gen_err("Cannot query port properties. h_ret=%li",
  344. h_ret);
  345. ret = -EPERM;
  346. goto sense_attributes1;
  347. }
  348. shca->max_mtu = port->max_mtu;
  349. sense_attributes1:
  350. ehca_free_fw_ctrlblock(rblock);
  351. return ret;
  352. }
  353. static int init_node_guid(struct ehca_shca *shca)
  354. {
  355. int ret = 0;
  356. struct hipz_query_hca *rblock;
  357. rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  358. if (!rblock) {
  359. ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
  360. return -ENOMEM;
  361. }
  362. if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
  363. ehca_err(&shca->ib_device, "Can't query device properties");
  364. ret = -EINVAL;
  365. goto init_node_guid1;
  366. }
  367. memcpy(&shca->ib_device.node_guid, &rblock->node_guid, sizeof(u64));
  368. init_node_guid1:
  369. ehca_free_fw_ctrlblock(rblock);
  370. return ret;
  371. }
  372. static int ehca_init_device(struct ehca_shca *shca)
  373. {
  374. int ret;
  375. ret = init_node_guid(shca);
  376. if (ret)
  377. return ret;
  378. strlcpy(shca->ib_device.name, "ehca%d", IB_DEVICE_NAME_MAX);
  379. shca->ib_device.owner = THIS_MODULE;
  380. shca->ib_device.uverbs_abi_ver = 8;
  381. shca->ib_device.uverbs_cmd_mask =
  382. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  383. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  384. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  385. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  386. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  387. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  388. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  389. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  390. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  391. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  392. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  393. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  394. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  395. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  396. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  397. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
  398. shca->ib_device.node_type = RDMA_NODE_IB_CA;
  399. shca->ib_device.phys_port_cnt = shca->num_ports;
  400. shca->ib_device.num_comp_vectors = 1;
  401. shca->ib_device.dma_device = &shca->ofdev->dev;
  402. shca->ib_device.query_device = ehca_query_device;
  403. shca->ib_device.query_port = ehca_query_port;
  404. shca->ib_device.query_gid = ehca_query_gid;
  405. shca->ib_device.query_pkey = ehca_query_pkey;
  406. /* shca->in_device.modify_device = ehca_modify_device */
  407. shca->ib_device.modify_port = ehca_modify_port;
  408. shca->ib_device.alloc_ucontext = ehca_alloc_ucontext;
  409. shca->ib_device.dealloc_ucontext = ehca_dealloc_ucontext;
  410. shca->ib_device.alloc_pd = ehca_alloc_pd;
  411. shca->ib_device.dealloc_pd = ehca_dealloc_pd;
  412. shca->ib_device.create_ah = ehca_create_ah;
  413. /* shca->ib_device.modify_ah = ehca_modify_ah; */
  414. shca->ib_device.query_ah = ehca_query_ah;
  415. shca->ib_device.destroy_ah = ehca_destroy_ah;
  416. shca->ib_device.create_qp = ehca_create_qp;
  417. shca->ib_device.modify_qp = ehca_modify_qp;
  418. shca->ib_device.query_qp = ehca_query_qp;
  419. shca->ib_device.destroy_qp = ehca_destroy_qp;
  420. shca->ib_device.post_send = ehca_post_send;
  421. shca->ib_device.post_recv = ehca_post_recv;
  422. shca->ib_device.create_cq = ehca_create_cq;
  423. shca->ib_device.destroy_cq = ehca_destroy_cq;
  424. shca->ib_device.resize_cq = ehca_resize_cq;
  425. shca->ib_device.poll_cq = ehca_poll_cq;
  426. /* shca->ib_device.peek_cq = ehca_peek_cq; */
  427. shca->ib_device.req_notify_cq = ehca_req_notify_cq;
  428. /* shca->ib_device.req_ncomp_notif = ehca_req_ncomp_notif; */
  429. shca->ib_device.get_dma_mr = ehca_get_dma_mr;
  430. shca->ib_device.reg_phys_mr = ehca_reg_phys_mr;
  431. shca->ib_device.reg_user_mr = ehca_reg_user_mr;
  432. shca->ib_device.query_mr = ehca_query_mr;
  433. shca->ib_device.dereg_mr = ehca_dereg_mr;
  434. shca->ib_device.rereg_phys_mr = ehca_rereg_phys_mr;
  435. shca->ib_device.alloc_mw = ehca_alloc_mw;
  436. shca->ib_device.bind_mw = ehca_bind_mw;
  437. shca->ib_device.dealloc_mw = ehca_dealloc_mw;
  438. shca->ib_device.alloc_fmr = ehca_alloc_fmr;
  439. shca->ib_device.map_phys_fmr = ehca_map_phys_fmr;
  440. shca->ib_device.unmap_fmr = ehca_unmap_fmr;
  441. shca->ib_device.dealloc_fmr = ehca_dealloc_fmr;
  442. shca->ib_device.attach_mcast = ehca_attach_mcast;
  443. shca->ib_device.detach_mcast = ehca_detach_mcast;
  444. shca->ib_device.process_mad = ehca_process_mad;
  445. shca->ib_device.mmap = ehca_mmap;
  446. if (EHCA_BMASK_GET(HCA_CAP_SRQ, shca->hca_cap)) {
  447. shca->ib_device.uverbs_cmd_mask |=
  448. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  449. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  450. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  451. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  452. shca->ib_device.create_srq = ehca_create_srq;
  453. shca->ib_device.modify_srq = ehca_modify_srq;
  454. shca->ib_device.query_srq = ehca_query_srq;
  455. shca->ib_device.destroy_srq = ehca_destroy_srq;
  456. shca->ib_device.post_srq_recv = ehca_post_srq_recv;
  457. }
  458. return ret;
  459. }
  460. static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
  461. {
  462. struct ehca_sport *sport = &shca->sport[port - 1];
  463. struct ib_cq *ibcq;
  464. struct ib_qp *ibqp;
  465. struct ib_qp_init_attr qp_init_attr;
  466. int ret;
  467. if (sport->ibcq_aqp1) {
  468. ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
  469. return -EPERM;
  470. }
  471. ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void *)(-1), 10, 0);
  472. if (IS_ERR(ibcq)) {
  473. ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
  474. return PTR_ERR(ibcq);
  475. }
  476. sport->ibcq_aqp1 = ibcq;
  477. if (sport->ibqp_sqp[IB_QPT_GSI]) {
  478. ehca_err(&shca->ib_device, "AQP1 QP is already created.");
  479. ret = -EPERM;
  480. goto create_aqp1;
  481. }
  482. memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
  483. qp_init_attr.send_cq = ibcq;
  484. qp_init_attr.recv_cq = ibcq;
  485. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  486. qp_init_attr.cap.max_send_wr = 100;
  487. qp_init_attr.cap.max_recv_wr = 100;
  488. qp_init_attr.cap.max_send_sge = 2;
  489. qp_init_attr.cap.max_recv_sge = 1;
  490. qp_init_attr.qp_type = IB_QPT_GSI;
  491. qp_init_attr.port_num = port;
  492. qp_init_attr.qp_context = NULL;
  493. qp_init_attr.event_handler = NULL;
  494. qp_init_attr.srq = NULL;
  495. ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
  496. if (IS_ERR(ibqp)) {
  497. ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
  498. ret = PTR_ERR(ibqp);
  499. goto create_aqp1;
  500. }
  501. sport->ibqp_sqp[IB_QPT_GSI] = ibqp;
  502. return 0;
  503. create_aqp1:
  504. ib_destroy_cq(sport->ibcq_aqp1);
  505. return ret;
  506. }
  507. static int ehca_destroy_aqp1(struct ehca_sport *sport)
  508. {
  509. int ret;
  510. ret = ib_destroy_qp(sport->ibqp_sqp[IB_QPT_GSI]);
  511. if (ret) {
  512. ehca_gen_err("Cannot destroy AQP1 QP. ret=%i", ret);
  513. return ret;
  514. }
  515. ret = ib_destroy_cq(sport->ibcq_aqp1);
  516. if (ret)
  517. ehca_gen_err("Cannot destroy AQP1 CQ. ret=%i", ret);
  518. return ret;
  519. }
  520. static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
  521. {
  522. return snprintf(buf, PAGE_SIZE, "%d\n", ehca_debug_level);
  523. }
  524. static ssize_t ehca_store_debug_level(struct device_driver *ddp,
  525. const char *buf, size_t count)
  526. {
  527. int value = (*buf) - '0';
  528. if (value >= 0 && value <= 9)
  529. ehca_debug_level = value;
  530. return 1;
  531. }
  532. static DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
  533. ehca_show_debug_level, ehca_store_debug_level);
  534. static struct attribute *ehca_drv_attrs[] = {
  535. &driver_attr_debug_level.attr,
  536. NULL
  537. };
  538. static struct attribute_group ehca_drv_attr_grp = {
  539. .attrs = ehca_drv_attrs
  540. };
  541. static struct attribute_group *ehca_drv_attr_groups[] = {
  542. &ehca_drv_attr_grp,
  543. NULL,
  544. };
  545. #define EHCA_RESOURCE_ATTR(name) \
  546. static ssize_t ehca_show_##name(struct device *dev, \
  547. struct device_attribute *attr, \
  548. char *buf) \
  549. { \
  550. struct ehca_shca *shca; \
  551. struct hipz_query_hca *rblock; \
  552. int data; \
  553. \
  554. shca = dev->driver_data; \
  555. \
  556. rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL); \
  557. if (!rblock) { \
  558. dev_err(dev, "Can't allocate rblock memory.\n"); \
  559. return 0; \
  560. } \
  561. \
  562. if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
  563. dev_err(dev, "Can't query device properties\n"); \
  564. ehca_free_fw_ctrlblock(rblock); \
  565. return 0; \
  566. } \
  567. \
  568. data = rblock->name; \
  569. ehca_free_fw_ctrlblock(rblock); \
  570. \
  571. if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1)) \
  572. return snprintf(buf, 256, "1\n"); \
  573. else \
  574. return snprintf(buf, 256, "%d\n", data); \
  575. \
  576. } \
  577. static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
  578. EHCA_RESOURCE_ATTR(num_ports);
  579. EHCA_RESOURCE_ATTR(hw_ver);
  580. EHCA_RESOURCE_ATTR(max_eq);
  581. EHCA_RESOURCE_ATTR(cur_eq);
  582. EHCA_RESOURCE_ATTR(max_cq);
  583. EHCA_RESOURCE_ATTR(cur_cq);
  584. EHCA_RESOURCE_ATTR(max_qp);
  585. EHCA_RESOURCE_ATTR(cur_qp);
  586. EHCA_RESOURCE_ATTR(max_mr);
  587. EHCA_RESOURCE_ATTR(cur_mr);
  588. EHCA_RESOURCE_ATTR(max_mw);
  589. EHCA_RESOURCE_ATTR(cur_mw);
  590. EHCA_RESOURCE_ATTR(max_pd);
  591. EHCA_RESOURCE_ATTR(max_ah);
  592. static ssize_t ehca_show_adapter_handle(struct device *dev,
  593. struct device_attribute *attr,
  594. char *buf)
  595. {
  596. struct ehca_shca *shca = dev->driver_data;
  597. return sprintf(buf, "%lx\n", shca->ipz_hca_handle.handle);
  598. }
  599. static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
  600. static struct attribute *ehca_dev_attrs[] = {
  601. &dev_attr_adapter_handle.attr,
  602. &dev_attr_num_ports.attr,
  603. &dev_attr_hw_ver.attr,
  604. &dev_attr_max_eq.attr,
  605. &dev_attr_cur_eq.attr,
  606. &dev_attr_max_cq.attr,
  607. &dev_attr_cur_cq.attr,
  608. &dev_attr_max_qp.attr,
  609. &dev_attr_cur_qp.attr,
  610. &dev_attr_max_mr.attr,
  611. &dev_attr_cur_mr.attr,
  612. &dev_attr_max_mw.attr,
  613. &dev_attr_cur_mw.attr,
  614. &dev_attr_max_pd.attr,
  615. &dev_attr_max_ah.attr,
  616. NULL
  617. };
  618. static struct attribute_group ehca_dev_attr_grp = {
  619. .attrs = ehca_dev_attrs
  620. };
  621. static int __devinit ehca_probe(struct of_device *dev,
  622. const struct of_device_id *id)
  623. {
  624. struct ehca_shca *shca;
  625. const u64 *handle;
  626. struct ib_pd *ibpd;
  627. int ret, i, eq_size;
  628. unsigned long flags;
  629. handle = of_get_property(dev->node, "ibm,hca-handle", NULL);
  630. if (!handle) {
  631. ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
  632. dev->node->full_name);
  633. return -ENODEV;
  634. }
  635. if (!(*handle)) {
  636. ehca_gen_err("Wrong eHCA handle for adapter: %s.",
  637. dev->node->full_name);
  638. return -ENODEV;
  639. }
  640. shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
  641. if (!shca) {
  642. ehca_gen_err("Cannot allocate shca memory.");
  643. return -ENOMEM;
  644. }
  645. mutex_init(&shca->modify_mutex);
  646. atomic_set(&shca->num_cqs, 0);
  647. atomic_set(&shca->num_qps, 0);
  648. shca->max_num_qps = ehca_max_qp;
  649. shca->max_num_cqs = ehca_max_cq;
  650. for (i = 0; i < ARRAY_SIZE(shca->sport); i++)
  651. spin_lock_init(&shca->sport[i].mod_sqp_lock);
  652. shca->ofdev = dev;
  653. shca->ipz_hca_handle.handle = *handle;
  654. dev->dev.driver_data = shca;
  655. ret = ehca_sense_attributes(shca);
  656. if (ret < 0) {
  657. ehca_gen_err("Cannot sense eHCA attributes.");
  658. goto probe1;
  659. }
  660. ret = ehca_init_device(shca);
  661. if (ret) {
  662. ehca_gen_err("Cannot init ehca device struct");
  663. goto probe1;
  664. }
  665. eq_size = 2 * shca->max_num_cqs + 4 * shca->max_num_qps;
  666. /* create event queues */
  667. ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, eq_size);
  668. if (ret) {
  669. ehca_err(&shca->ib_device, "Cannot create EQ.");
  670. goto probe1;
  671. }
  672. ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
  673. if (ret) {
  674. ehca_err(&shca->ib_device, "Cannot create NEQ.");
  675. goto probe3;
  676. }
  677. /* create internal protection domain */
  678. ibpd = ehca_alloc_pd(&shca->ib_device, (void *)(-1), NULL);
  679. if (IS_ERR(ibpd)) {
  680. ehca_err(&shca->ib_device, "Cannot create internal PD.");
  681. ret = PTR_ERR(ibpd);
  682. goto probe4;
  683. }
  684. shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
  685. shca->pd->ib_pd.device = &shca->ib_device;
  686. /* create internal max MR */
  687. ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
  688. if (ret) {
  689. ehca_err(&shca->ib_device, "Cannot create internal MR ret=%i",
  690. ret);
  691. goto probe5;
  692. }
  693. ret = ib_register_device(&shca->ib_device);
  694. if (ret) {
  695. ehca_err(&shca->ib_device,
  696. "ib_register_device() failed ret=%i", ret);
  697. goto probe6;
  698. }
  699. /* create AQP1 for port 1 */
  700. if (ehca_open_aqp1 == 1) {
  701. shca->sport[0].port_state = IB_PORT_DOWN;
  702. ret = ehca_create_aqp1(shca, 1);
  703. if (ret) {
  704. ehca_err(&shca->ib_device,
  705. "Cannot create AQP1 for port 1.");
  706. goto probe7;
  707. }
  708. }
  709. /* create AQP1 for port 2 */
  710. if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
  711. shca->sport[1].port_state = IB_PORT_DOWN;
  712. ret = ehca_create_aqp1(shca, 2);
  713. if (ret) {
  714. ehca_err(&shca->ib_device,
  715. "Cannot create AQP1 for port 2.");
  716. goto probe8;
  717. }
  718. }
  719. ret = sysfs_create_group(&dev->dev.kobj, &ehca_dev_attr_grp);
  720. if (ret) /* only complain; we can live without attributes */
  721. ehca_err(&shca->ib_device,
  722. "Cannot create device attributes ret=%d", ret);
  723. spin_lock_irqsave(&shca_list_lock, flags);
  724. list_add(&shca->shca_list, &shca_list);
  725. spin_unlock_irqrestore(&shca_list_lock, flags);
  726. return 0;
  727. probe8:
  728. ret = ehca_destroy_aqp1(&shca->sport[0]);
  729. if (ret)
  730. ehca_err(&shca->ib_device,
  731. "Cannot destroy AQP1 for port 1. ret=%i", ret);
  732. probe7:
  733. ib_unregister_device(&shca->ib_device);
  734. probe6:
  735. ret = ehca_dereg_internal_maxmr(shca);
  736. if (ret)
  737. ehca_err(&shca->ib_device,
  738. "Cannot destroy internal MR. ret=%x", ret);
  739. probe5:
  740. ret = ehca_dealloc_pd(&shca->pd->ib_pd);
  741. if (ret)
  742. ehca_err(&shca->ib_device,
  743. "Cannot destroy internal PD. ret=%x", ret);
  744. probe4:
  745. ret = ehca_destroy_eq(shca, &shca->neq);
  746. if (ret)
  747. ehca_err(&shca->ib_device,
  748. "Cannot destroy NEQ. ret=%x", ret);
  749. probe3:
  750. ret = ehca_destroy_eq(shca, &shca->eq);
  751. if (ret)
  752. ehca_err(&shca->ib_device,
  753. "Cannot destroy EQ. ret=%x", ret);
  754. probe1:
  755. ib_dealloc_device(&shca->ib_device);
  756. return -EINVAL;
  757. }
  758. static int __devexit ehca_remove(struct of_device *dev)
  759. {
  760. struct ehca_shca *shca = dev->dev.driver_data;
  761. unsigned long flags;
  762. int ret;
  763. sysfs_remove_group(&dev->dev.kobj, &ehca_dev_attr_grp);
  764. if (ehca_open_aqp1 == 1) {
  765. int i;
  766. for (i = 0; i < shca->num_ports; i++) {
  767. ret = ehca_destroy_aqp1(&shca->sport[i]);
  768. if (ret)
  769. ehca_err(&shca->ib_device,
  770. "Cannot destroy AQP1 for port %x "
  771. "ret=%i", ret, i);
  772. }
  773. }
  774. ib_unregister_device(&shca->ib_device);
  775. ret = ehca_dereg_internal_maxmr(shca);
  776. if (ret)
  777. ehca_err(&shca->ib_device,
  778. "Cannot destroy internal MR. ret=%i", ret);
  779. ret = ehca_dealloc_pd(&shca->pd->ib_pd);
  780. if (ret)
  781. ehca_err(&shca->ib_device,
  782. "Cannot destroy internal PD. ret=%i", ret);
  783. ret = ehca_destroy_eq(shca, &shca->eq);
  784. if (ret)
  785. ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%i", ret);
  786. ret = ehca_destroy_eq(shca, &shca->neq);
  787. if (ret)
  788. ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%i", ret);
  789. ib_dealloc_device(&shca->ib_device);
  790. spin_lock_irqsave(&shca_list_lock, flags);
  791. list_del(&shca->shca_list);
  792. spin_unlock_irqrestore(&shca_list_lock, flags);
  793. return ret;
  794. }
  795. static struct of_device_id ehca_device_table[] =
  796. {
  797. {
  798. .name = "lhca",
  799. .compatible = "IBM,lhca",
  800. },
  801. {},
  802. };
  803. MODULE_DEVICE_TABLE(of, ehca_device_table);
  804. static struct of_platform_driver ehca_driver = {
  805. .name = "ehca",
  806. .match_table = ehca_device_table,
  807. .probe = ehca_probe,
  808. .remove = ehca_remove,
  809. .driver = {
  810. .groups = ehca_drv_attr_groups,
  811. },
  812. };
  813. void ehca_poll_eqs(unsigned long data)
  814. {
  815. struct ehca_shca *shca;
  816. spin_lock(&shca_list_lock);
  817. list_for_each_entry(shca, &shca_list, shca_list) {
  818. if (shca->eq.is_initialized) {
  819. /* call deadman proc only if eq ptr does not change */
  820. struct ehca_eq *eq = &shca->eq;
  821. int max = 3;
  822. volatile u64 q_ofs, q_ofs2;
  823. u64 flags;
  824. spin_lock_irqsave(&eq->spinlock, flags);
  825. q_ofs = eq->ipz_queue.current_q_offset;
  826. spin_unlock_irqrestore(&eq->spinlock, flags);
  827. do {
  828. spin_lock_irqsave(&eq->spinlock, flags);
  829. q_ofs2 = eq->ipz_queue.current_q_offset;
  830. spin_unlock_irqrestore(&eq->spinlock, flags);
  831. max--;
  832. } while (q_ofs == q_ofs2 && max > 0);
  833. if (q_ofs == q_ofs2)
  834. ehca_process_eq(shca, 0);
  835. }
  836. }
  837. mod_timer(&poll_eqs_timer, round_jiffies(jiffies + HZ));
  838. spin_unlock(&shca_list_lock);
  839. }
  840. static int ehca_mem_notifier(struct notifier_block *nb,
  841. unsigned long action, void *data)
  842. {
  843. static unsigned long ehca_dmem_warn_time;
  844. unsigned long flags;
  845. switch (action) {
  846. case MEM_CANCEL_OFFLINE:
  847. case MEM_CANCEL_ONLINE:
  848. case MEM_ONLINE:
  849. case MEM_OFFLINE:
  850. return NOTIFY_OK;
  851. case MEM_GOING_ONLINE:
  852. case MEM_GOING_OFFLINE:
  853. /* only ok if no hca is attached to the lpar */
  854. spin_lock_irqsave(&shca_list_lock, flags);
  855. if (list_empty(&shca_list)) {
  856. spin_unlock_irqrestore(&shca_list_lock, flags);
  857. return NOTIFY_OK;
  858. } else {
  859. spin_unlock_irqrestore(&shca_list_lock, flags);
  860. if (printk_timed_ratelimit(&ehca_dmem_warn_time,
  861. 30 * 1000))
  862. ehca_gen_err("DMEM operations are not allowed"
  863. "in conjunction with eHCA");
  864. return NOTIFY_BAD;
  865. }
  866. }
  867. return NOTIFY_OK;
  868. }
  869. static struct notifier_block ehca_mem_nb = {
  870. .notifier_call = ehca_mem_notifier,
  871. };
  872. static int __init ehca_module_init(void)
  873. {
  874. int ret;
  875. printk(KERN_INFO "eHCA Infiniband Device Driver "
  876. "(Version " HCAD_VERSION ")\n");
  877. ret = ehca_create_comp_pool();
  878. if (ret) {
  879. ehca_gen_err("Cannot create comp pool.");
  880. return ret;
  881. }
  882. ret = ehca_create_slab_caches();
  883. if (ret) {
  884. ehca_gen_err("Cannot create SLAB caches");
  885. ret = -ENOMEM;
  886. goto module_init1;
  887. }
  888. ret = ibmebus_register_driver(&ehca_driver);
  889. if (ret) {
  890. ehca_gen_err("Cannot register eHCA device driver");
  891. ret = -EINVAL;
  892. goto module_init2;
  893. }
  894. ret = register_memory_notifier(&ehca_mem_nb);
  895. if (ret) {
  896. ehca_gen_err("Failed registering memory add/remove notifier");
  897. goto module_init3;
  898. }
  899. if (ehca_poll_all_eqs != 1) {
  900. ehca_gen_err("WARNING!!!");
  901. ehca_gen_err("It is possible to lose interrupts.");
  902. } else {
  903. init_timer(&poll_eqs_timer);
  904. poll_eqs_timer.function = ehca_poll_eqs;
  905. poll_eqs_timer.expires = jiffies + HZ;
  906. add_timer(&poll_eqs_timer);
  907. }
  908. return 0;
  909. module_init3:
  910. ibmebus_unregister_driver(&ehca_driver);
  911. module_init2:
  912. ehca_destroy_slab_caches();
  913. module_init1:
  914. ehca_destroy_comp_pool();
  915. return ret;
  916. };
  917. static void __exit ehca_module_exit(void)
  918. {
  919. if (ehca_poll_all_eqs == 1)
  920. del_timer_sync(&poll_eqs_timer);
  921. ibmebus_unregister_driver(&ehca_driver);
  922. unregister_memory_notifier(&ehca_mem_nb);
  923. ehca_destroy_slab_caches();
  924. ehca_destroy_comp_pool();
  925. idr_destroy(&ehca_cq_idr);
  926. idr_destroy(&ehca_qp_idr);
  927. };
  928. module_init(ehca_module_init);
  929. module_exit(ehca_module_exit);