ehca_main.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 "0029"
  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 = -1;
  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 (default), "
  91. "1: port one only, 2: two ports)");
  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. 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=%lli",
  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=%lli",
  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. shca->ib_device.dma_ops = &ehca_dma_mapping_ops;
  447. if (EHCA_BMASK_GET(HCA_CAP_SRQ, shca->hca_cap)) {
  448. shca->ib_device.uverbs_cmd_mask |=
  449. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  450. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  451. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  452. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  453. shca->ib_device.create_srq = ehca_create_srq;
  454. shca->ib_device.modify_srq = ehca_modify_srq;
  455. shca->ib_device.query_srq = ehca_query_srq;
  456. shca->ib_device.destroy_srq = ehca_destroy_srq;
  457. shca->ib_device.post_srq_recv = ehca_post_srq_recv;
  458. }
  459. return ret;
  460. }
  461. static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
  462. {
  463. struct ehca_sport *sport = &shca->sport[port - 1];
  464. struct ib_cq *ibcq;
  465. struct ib_qp *ibqp;
  466. struct ib_qp_init_attr qp_init_attr;
  467. int ret;
  468. if (sport->ibcq_aqp1) {
  469. ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
  470. return -EPERM;
  471. }
  472. ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void *)(-1), 10, 0);
  473. if (IS_ERR(ibcq)) {
  474. ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
  475. return PTR_ERR(ibcq);
  476. }
  477. sport->ibcq_aqp1 = ibcq;
  478. if (sport->ibqp_sqp[IB_QPT_GSI]) {
  479. ehca_err(&shca->ib_device, "AQP1 QP is already created.");
  480. ret = -EPERM;
  481. goto create_aqp1;
  482. }
  483. memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
  484. qp_init_attr.send_cq = ibcq;
  485. qp_init_attr.recv_cq = ibcq;
  486. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  487. qp_init_attr.cap.max_send_wr = 100;
  488. qp_init_attr.cap.max_recv_wr = 100;
  489. qp_init_attr.cap.max_send_sge = 2;
  490. qp_init_attr.cap.max_recv_sge = 1;
  491. qp_init_attr.qp_type = IB_QPT_GSI;
  492. qp_init_attr.port_num = port;
  493. qp_init_attr.qp_context = NULL;
  494. qp_init_attr.event_handler = NULL;
  495. qp_init_attr.srq = NULL;
  496. ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
  497. if (IS_ERR(ibqp)) {
  498. ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
  499. ret = PTR_ERR(ibqp);
  500. goto create_aqp1;
  501. }
  502. sport->ibqp_sqp[IB_QPT_GSI] = ibqp;
  503. return 0;
  504. create_aqp1:
  505. ib_destroy_cq(sport->ibcq_aqp1);
  506. return ret;
  507. }
  508. static int ehca_destroy_aqp1(struct ehca_sport *sport)
  509. {
  510. int ret;
  511. ret = ib_destroy_qp(sport->ibqp_sqp[IB_QPT_GSI]);
  512. if (ret) {
  513. ehca_gen_err("Cannot destroy AQP1 QP. ret=%i", ret);
  514. return ret;
  515. }
  516. ret = ib_destroy_cq(sport->ibcq_aqp1);
  517. if (ret)
  518. ehca_gen_err("Cannot destroy AQP1 CQ. ret=%i", ret);
  519. return ret;
  520. }
  521. static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
  522. {
  523. return snprintf(buf, PAGE_SIZE, "%d\n", ehca_debug_level);
  524. }
  525. static ssize_t ehca_store_debug_level(struct device_driver *ddp,
  526. const char *buf, size_t count)
  527. {
  528. int value = (*buf) - '0';
  529. if (value >= 0 && value <= 9)
  530. ehca_debug_level = value;
  531. return 1;
  532. }
  533. static DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
  534. ehca_show_debug_level, ehca_store_debug_level);
  535. static struct attribute *ehca_drv_attrs[] = {
  536. &driver_attr_debug_level.attr,
  537. NULL
  538. };
  539. static struct attribute_group ehca_drv_attr_grp = {
  540. .attrs = ehca_drv_attrs
  541. };
  542. static const struct attribute_group *ehca_drv_attr_groups[] = {
  543. &ehca_drv_attr_grp,
  544. NULL,
  545. };
  546. #define EHCA_RESOURCE_ATTR(name) \
  547. static ssize_t ehca_show_##name(struct device *dev, \
  548. struct device_attribute *attr, \
  549. char *buf) \
  550. { \
  551. struct ehca_shca *shca; \
  552. struct hipz_query_hca *rblock; \
  553. int data; \
  554. \
  555. shca = dev_get_drvdata(dev); \
  556. \
  557. rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL); \
  558. if (!rblock) { \
  559. dev_err(dev, "Can't allocate rblock memory.\n"); \
  560. return 0; \
  561. } \
  562. \
  563. if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
  564. dev_err(dev, "Can't query device properties\n"); \
  565. ehca_free_fw_ctrlblock(rblock); \
  566. return 0; \
  567. } \
  568. \
  569. data = rblock->name; \
  570. ehca_free_fw_ctrlblock(rblock); \
  571. \
  572. if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1)) \
  573. return snprintf(buf, 256, "1\n"); \
  574. else \
  575. return snprintf(buf, 256, "%d\n", data); \
  576. \
  577. } \
  578. static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
  579. EHCA_RESOURCE_ATTR(num_ports);
  580. EHCA_RESOURCE_ATTR(hw_ver);
  581. EHCA_RESOURCE_ATTR(max_eq);
  582. EHCA_RESOURCE_ATTR(cur_eq);
  583. EHCA_RESOURCE_ATTR(max_cq);
  584. EHCA_RESOURCE_ATTR(cur_cq);
  585. EHCA_RESOURCE_ATTR(max_qp);
  586. EHCA_RESOURCE_ATTR(cur_qp);
  587. EHCA_RESOURCE_ATTR(max_mr);
  588. EHCA_RESOURCE_ATTR(cur_mr);
  589. EHCA_RESOURCE_ATTR(max_mw);
  590. EHCA_RESOURCE_ATTR(cur_mw);
  591. EHCA_RESOURCE_ATTR(max_pd);
  592. EHCA_RESOURCE_ATTR(max_ah);
  593. static ssize_t ehca_show_adapter_handle(struct device *dev,
  594. struct device_attribute *attr,
  595. char *buf)
  596. {
  597. struct ehca_shca *shca = dev_get_drvdata(dev);
  598. return sprintf(buf, "%llx\n", shca->ipz_hca_handle.handle);
  599. }
  600. static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
  601. static struct attribute *ehca_dev_attrs[] = {
  602. &dev_attr_adapter_handle.attr,
  603. &dev_attr_num_ports.attr,
  604. &dev_attr_hw_ver.attr,
  605. &dev_attr_max_eq.attr,
  606. &dev_attr_cur_eq.attr,
  607. &dev_attr_max_cq.attr,
  608. &dev_attr_cur_cq.attr,
  609. &dev_attr_max_qp.attr,
  610. &dev_attr_cur_qp.attr,
  611. &dev_attr_max_mr.attr,
  612. &dev_attr_cur_mr.attr,
  613. &dev_attr_max_mw.attr,
  614. &dev_attr_cur_mw.attr,
  615. &dev_attr_max_pd.attr,
  616. &dev_attr_max_ah.attr,
  617. NULL
  618. };
  619. static struct attribute_group ehca_dev_attr_grp = {
  620. .attrs = ehca_dev_attrs
  621. };
  622. static int __devinit ehca_probe(struct of_device *dev,
  623. const struct of_device_id *id)
  624. {
  625. struct ehca_shca *shca;
  626. const u64 *handle;
  627. struct ib_pd *ibpd;
  628. int ret, i, eq_size;
  629. unsigned long flags;
  630. handle = of_get_property(dev->node, "ibm,hca-handle", NULL);
  631. if (!handle) {
  632. ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
  633. dev->node->full_name);
  634. return -ENODEV;
  635. }
  636. if (!(*handle)) {
  637. ehca_gen_err("Wrong eHCA handle for adapter: %s.",
  638. dev->node->full_name);
  639. return -ENODEV;
  640. }
  641. shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
  642. if (!shca) {
  643. ehca_gen_err("Cannot allocate shca memory.");
  644. return -ENOMEM;
  645. }
  646. mutex_init(&shca->modify_mutex);
  647. atomic_set(&shca->num_cqs, 0);
  648. atomic_set(&shca->num_qps, 0);
  649. shca->max_num_qps = ehca_max_qp;
  650. shca->max_num_cqs = ehca_max_cq;
  651. for (i = 0; i < ARRAY_SIZE(shca->sport); i++)
  652. spin_lock_init(&shca->sport[i].mod_sqp_lock);
  653. shca->ofdev = dev;
  654. shca->ipz_hca_handle.handle = *handle;
  655. dev_set_drvdata(&dev->dev, shca);
  656. ret = ehca_sense_attributes(shca);
  657. if (ret < 0) {
  658. ehca_gen_err("Cannot sense eHCA attributes.");
  659. goto probe1;
  660. }
  661. ret = ehca_init_device(shca);
  662. if (ret) {
  663. ehca_gen_err("Cannot init ehca device struct");
  664. goto probe1;
  665. }
  666. eq_size = 2 * shca->max_num_cqs + 4 * shca->max_num_qps;
  667. /* create event queues */
  668. ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, eq_size);
  669. if (ret) {
  670. ehca_err(&shca->ib_device, "Cannot create EQ.");
  671. goto probe1;
  672. }
  673. ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
  674. if (ret) {
  675. ehca_err(&shca->ib_device, "Cannot create NEQ.");
  676. goto probe3;
  677. }
  678. /* create internal protection domain */
  679. ibpd = ehca_alloc_pd(&shca->ib_device, (void *)(-1), NULL);
  680. if (IS_ERR(ibpd)) {
  681. ehca_err(&shca->ib_device, "Cannot create internal PD.");
  682. ret = PTR_ERR(ibpd);
  683. goto probe4;
  684. }
  685. shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
  686. shca->pd->ib_pd.device = &shca->ib_device;
  687. /* create internal max MR */
  688. ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
  689. if (ret) {
  690. ehca_err(&shca->ib_device, "Cannot create internal MR ret=%i",
  691. ret);
  692. goto probe5;
  693. }
  694. ret = ib_register_device(&shca->ib_device);
  695. if (ret) {
  696. ehca_err(&shca->ib_device,
  697. "ib_register_device() failed ret=%i", ret);
  698. goto probe6;
  699. }
  700. /* create AQP1 for port 1 */
  701. if (ehca_open_aqp1 == 1) {
  702. shca->sport[0].port_state = IB_PORT_DOWN;
  703. ret = ehca_create_aqp1(shca, 1);
  704. if (ret) {
  705. ehca_err(&shca->ib_device,
  706. "Cannot create AQP1 for port 1.");
  707. goto probe7;
  708. }
  709. }
  710. /* create AQP1 for port 2 */
  711. if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
  712. shca->sport[1].port_state = IB_PORT_DOWN;
  713. ret = ehca_create_aqp1(shca, 2);
  714. if (ret) {
  715. ehca_err(&shca->ib_device,
  716. "Cannot create AQP1 for port 2.");
  717. goto probe8;
  718. }
  719. }
  720. ret = sysfs_create_group(&dev->dev.kobj, &ehca_dev_attr_grp);
  721. if (ret) /* only complain; we can live without attributes */
  722. ehca_err(&shca->ib_device,
  723. "Cannot create device attributes ret=%d", ret);
  724. spin_lock_irqsave(&shca_list_lock, flags);
  725. list_add(&shca->shca_list, &shca_list);
  726. spin_unlock_irqrestore(&shca_list_lock, flags);
  727. return 0;
  728. probe8:
  729. ret = ehca_destroy_aqp1(&shca->sport[0]);
  730. if (ret)
  731. ehca_err(&shca->ib_device,
  732. "Cannot destroy AQP1 for port 1. ret=%i", ret);
  733. probe7:
  734. ib_unregister_device(&shca->ib_device);
  735. probe6:
  736. ret = ehca_dereg_internal_maxmr(shca);
  737. if (ret)
  738. ehca_err(&shca->ib_device,
  739. "Cannot destroy internal MR. ret=%x", ret);
  740. probe5:
  741. ret = ehca_dealloc_pd(&shca->pd->ib_pd);
  742. if (ret)
  743. ehca_err(&shca->ib_device,
  744. "Cannot destroy internal PD. ret=%x", ret);
  745. probe4:
  746. ret = ehca_destroy_eq(shca, &shca->neq);
  747. if (ret)
  748. ehca_err(&shca->ib_device,
  749. "Cannot destroy NEQ. ret=%x", ret);
  750. probe3:
  751. ret = ehca_destroy_eq(shca, &shca->eq);
  752. if (ret)
  753. ehca_err(&shca->ib_device,
  754. "Cannot destroy EQ. ret=%x", ret);
  755. probe1:
  756. ib_dealloc_device(&shca->ib_device);
  757. return -EINVAL;
  758. }
  759. static int __devexit ehca_remove(struct of_device *dev)
  760. {
  761. struct ehca_shca *shca = dev_get_drvdata(&dev->dev);
  762. unsigned long flags;
  763. int ret;
  764. sysfs_remove_group(&dev->dev.kobj, &ehca_dev_attr_grp);
  765. if (ehca_open_aqp1 == 1) {
  766. int i;
  767. for (i = 0; i < shca->num_ports; i++) {
  768. ret = ehca_destroy_aqp1(&shca->sport[i]);
  769. if (ret)
  770. ehca_err(&shca->ib_device,
  771. "Cannot destroy AQP1 for port %x "
  772. "ret=%i", ret, i);
  773. }
  774. }
  775. ib_unregister_device(&shca->ib_device);
  776. ret = ehca_dereg_internal_maxmr(shca);
  777. if (ret)
  778. ehca_err(&shca->ib_device,
  779. "Cannot destroy internal MR. ret=%i", ret);
  780. ret = ehca_dealloc_pd(&shca->pd->ib_pd);
  781. if (ret)
  782. ehca_err(&shca->ib_device,
  783. "Cannot destroy internal PD. ret=%i", ret);
  784. ret = ehca_destroy_eq(shca, &shca->eq);
  785. if (ret)
  786. ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%i", ret);
  787. ret = ehca_destroy_eq(shca, &shca->neq);
  788. if (ret)
  789. ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%i", ret);
  790. ib_dealloc_device(&shca->ib_device);
  791. spin_lock_irqsave(&shca_list_lock, flags);
  792. list_del(&shca->shca_list);
  793. spin_unlock_irqrestore(&shca_list_lock, flags);
  794. return ret;
  795. }
  796. static struct of_device_id ehca_device_table[] =
  797. {
  798. {
  799. .name = "lhca",
  800. .compatible = "IBM,lhca",
  801. },
  802. {},
  803. };
  804. MODULE_DEVICE_TABLE(of, ehca_device_table);
  805. static struct of_platform_driver ehca_driver = {
  806. .name = "ehca",
  807. .match_table = ehca_device_table,
  808. .probe = ehca_probe,
  809. .remove = ehca_remove,
  810. .driver = {
  811. .groups = ehca_drv_attr_groups,
  812. },
  813. };
  814. void ehca_poll_eqs(unsigned long data)
  815. {
  816. struct ehca_shca *shca;
  817. spin_lock(&shca_list_lock);
  818. list_for_each_entry(shca, &shca_list, shca_list) {
  819. if (shca->eq.is_initialized) {
  820. /* call deadman proc only if eq ptr does not change */
  821. struct ehca_eq *eq = &shca->eq;
  822. int max = 3;
  823. volatile u64 q_ofs, q_ofs2;
  824. unsigned long flags;
  825. spin_lock_irqsave(&eq->spinlock, flags);
  826. q_ofs = eq->ipz_queue.current_q_offset;
  827. spin_unlock_irqrestore(&eq->spinlock, flags);
  828. do {
  829. spin_lock_irqsave(&eq->spinlock, flags);
  830. q_ofs2 = eq->ipz_queue.current_q_offset;
  831. spin_unlock_irqrestore(&eq->spinlock, flags);
  832. max--;
  833. } while (q_ofs == q_ofs2 && max > 0);
  834. if (q_ofs == q_ofs2)
  835. ehca_process_eq(shca, 0);
  836. }
  837. }
  838. mod_timer(&poll_eqs_timer, round_jiffies(jiffies + HZ));
  839. spin_unlock(&shca_list_lock);
  840. }
  841. static int ehca_mem_notifier(struct notifier_block *nb,
  842. unsigned long action, void *data)
  843. {
  844. static unsigned long ehca_dmem_warn_time;
  845. unsigned long flags;
  846. switch (action) {
  847. case MEM_CANCEL_OFFLINE:
  848. case MEM_CANCEL_ONLINE:
  849. case MEM_ONLINE:
  850. case MEM_OFFLINE:
  851. return NOTIFY_OK;
  852. case MEM_GOING_ONLINE:
  853. case MEM_GOING_OFFLINE:
  854. /* only ok if no hca is attached to the lpar */
  855. spin_lock_irqsave(&shca_list_lock, flags);
  856. if (list_empty(&shca_list)) {
  857. spin_unlock_irqrestore(&shca_list_lock, flags);
  858. return NOTIFY_OK;
  859. } else {
  860. spin_unlock_irqrestore(&shca_list_lock, flags);
  861. if (printk_timed_ratelimit(&ehca_dmem_warn_time,
  862. 30 * 1000))
  863. ehca_gen_err("DMEM operations are not allowed"
  864. "in conjunction with eHCA");
  865. return NOTIFY_BAD;
  866. }
  867. }
  868. return NOTIFY_OK;
  869. }
  870. static struct notifier_block ehca_mem_nb = {
  871. .notifier_call = ehca_mem_notifier,
  872. };
  873. static int __init ehca_module_init(void)
  874. {
  875. int ret;
  876. printk(KERN_INFO "eHCA Infiniband Device Driver "
  877. "(Version " HCAD_VERSION ")\n");
  878. ret = ehca_create_comp_pool();
  879. if (ret) {
  880. ehca_gen_err("Cannot create comp pool.");
  881. return ret;
  882. }
  883. ret = ehca_create_slab_caches();
  884. if (ret) {
  885. ehca_gen_err("Cannot create SLAB caches");
  886. ret = -ENOMEM;
  887. goto module_init1;
  888. }
  889. ret = ehca_create_busmap();
  890. if (ret) {
  891. ehca_gen_err("Cannot create busmap.");
  892. goto module_init2;
  893. }
  894. ret = ibmebus_register_driver(&ehca_driver);
  895. if (ret) {
  896. ehca_gen_err("Cannot register eHCA device driver");
  897. ret = -EINVAL;
  898. goto module_init3;
  899. }
  900. ret = register_memory_notifier(&ehca_mem_nb);
  901. if (ret) {
  902. ehca_gen_err("Failed registering memory add/remove notifier");
  903. goto module_init4;
  904. }
  905. if (ehca_poll_all_eqs != 1) {
  906. ehca_gen_err("WARNING!!!");
  907. ehca_gen_err("It is possible to lose interrupts.");
  908. } else {
  909. init_timer(&poll_eqs_timer);
  910. poll_eqs_timer.function = ehca_poll_eqs;
  911. poll_eqs_timer.expires = jiffies + HZ;
  912. add_timer(&poll_eqs_timer);
  913. }
  914. return 0;
  915. module_init4:
  916. ibmebus_unregister_driver(&ehca_driver);
  917. module_init3:
  918. ehca_destroy_busmap();
  919. module_init2:
  920. ehca_destroy_slab_caches();
  921. module_init1:
  922. ehca_destroy_comp_pool();
  923. return ret;
  924. };
  925. static void __exit ehca_module_exit(void)
  926. {
  927. if (ehca_poll_all_eqs == 1)
  928. del_timer_sync(&poll_eqs_timer);
  929. ibmebus_unregister_driver(&ehca_driver);
  930. unregister_memory_notifier(&ehca_mem_nb);
  931. ehca_destroy_busmap();
  932. ehca_destroy_slab_caches();
  933. ehca_destroy_comp_pool();
  934. idr_destroy(&ehca_cq_idr);
  935. idr_destroy(&ehca_qp_idr);
  936. };
  937. module_init(ehca_module_init);
  938. module_exit(ehca_module_exit);