mthca_main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/errno.h>
  37. #include <linux/pci.h>
  38. #include <linux/interrupt.h>
  39. #include "mthca_dev.h"
  40. #include "mthca_config_reg.h"
  41. #include "mthca_cmd.h"
  42. #include "mthca_profile.h"
  43. #include "mthca_memfree.h"
  44. #include "mthca_wqe.h"
  45. MODULE_AUTHOR("Roland Dreier");
  46. MODULE_DESCRIPTION("Mellanox InfiniBand HCA low-level driver");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. MODULE_VERSION(DRV_VERSION);
  49. #ifdef CONFIG_INFINIBAND_MTHCA_DEBUG
  50. int mthca_debug_level = 0;
  51. module_param_named(debug_level, mthca_debug_level, int, 0644);
  52. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  53. #endif /* CONFIG_INFINIBAND_MTHCA_DEBUG */
  54. #ifdef CONFIG_PCI_MSI
  55. static int msi_x = 1;
  56. module_param(msi_x, int, 0444);
  57. MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
  58. #else /* CONFIG_PCI_MSI */
  59. #define msi_x (0)
  60. #endif /* CONFIG_PCI_MSI */
  61. static int tune_pci = 0;
  62. module_param(tune_pci, int, 0444);
  63. MODULE_PARM_DESC(tune_pci, "increase PCI burst from the default set by BIOS if nonzero");
  64. DEFINE_MUTEX(mthca_device_mutex);
  65. #define MTHCA_DEFAULT_NUM_QP (1 << 16)
  66. #define MTHCA_DEFAULT_RDB_PER_QP (1 << 2)
  67. #define MTHCA_DEFAULT_NUM_CQ (1 << 16)
  68. #define MTHCA_DEFAULT_NUM_MCG (1 << 13)
  69. #define MTHCA_DEFAULT_NUM_MPT (1 << 17)
  70. #define MTHCA_DEFAULT_NUM_MTT (1 << 20)
  71. #define MTHCA_DEFAULT_NUM_UDAV (1 << 15)
  72. #define MTHCA_DEFAULT_NUM_RESERVED_MTTS (1 << 18)
  73. #define MTHCA_DEFAULT_NUM_UARC_SIZE (1 << 18)
  74. static struct mthca_profile hca_profile = {
  75. .num_qp = MTHCA_DEFAULT_NUM_QP,
  76. .rdb_per_qp = MTHCA_DEFAULT_RDB_PER_QP,
  77. .num_cq = MTHCA_DEFAULT_NUM_CQ,
  78. .num_mcg = MTHCA_DEFAULT_NUM_MCG,
  79. .num_mpt = MTHCA_DEFAULT_NUM_MPT,
  80. .num_mtt = MTHCA_DEFAULT_NUM_MTT,
  81. .num_udav = MTHCA_DEFAULT_NUM_UDAV, /* Tavor only */
  82. .fmr_reserved_mtts = MTHCA_DEFAULT_NUM_RESERVED_MTTS, /* Tavor only */
  83. .uarc_size = MTHCA_DEFAULT_NUM_UARC_SIZE, /* Arbel only */
  84. };
  85. module_param_named(num_qp, hca_profile.num_qp, int, 0444);
  86. MODULE_PARM_DESC(num_qp, "maximum number of QPs per HCA");
  87. module_param_named(rdb_per_qp, hca_profile.rdb_per_qp, int, 0444);
  88. MODULE_PARM_DESC(rdb_per_qp, "number of RDB buffers per QP");
  89. module_param_named(num_cq, hca_profile.num_cq, int, 0444);
  90. MODULE_PARM_DESC(num_cq, "maximum number of CQs per HCA");
  91. module_param_named(num_mcg, hca_profile.num_mcg, int, 0444);
  92. MODULE_PARM_DESC(num_mcg, "maximum number of multicast groups per HCA");
  93. module_param_named(num_mpt, hca_profile.num_mpt, int, 0444);
  94. MODULE_PARM_DESC(num_mpt,
  95. "maximum number of memory protection table entries per HCA");
  96. module_param_named(num_mtt, hca_profile.num_mtt, int, 0444);
  97. MODULE_PARM_DESC(num_mtt,
  98. "maximum number of memory translation table segments per HCA");
  99. module_param_named(num_udav, hca_profile.num_udav, int, 0444);
  100. MODULE_PARM_DESC(num_udav, "maximum number of UD address vectors per HCA");
  101. module_param_named(fmr_reserved_mtts, hca_profile.fmr_reserved_mtts, int, 0444);
  102. MODULE_PARM_DESC(fmr_reserved_mtts,
  103. "number of memory translation table segments reserved for FMR");
  104. static char mthca_version[] __devinitdata =
  105. DRV_NAME ": Mellanox InfiniBand HCA driver v"
  106. DRV_VERSION " (" DRV_RELDATE ")\n";
  107. static int mthca_tune_pci(struct mthca_dev *mdev)
  108. {
  109. if (!tune_pci)
  110. return 0;
  111. /* First try to max out Read Byte Count */
  112. if (pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX)) {
  113. if (pcix_set_mmrbc(mdev->pdev, pcix_get_max_mmrbc(mdev->pdev))) {
  114. mthca_err(mdev, "Couldn't set PCI-X max read count, "
  115. "aborting.\n");
  116. return -ENODEV;
  117. }
  118. } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
  119. mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
  120. if (pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP)) {
  121. if (pcie_set_readrq(mdev->pdev, 4096)) {
  122. mthca_err(mdev, "Couldn't write PCI Express read request, "
  123. "aborting.\n");
  124. return -ENODEV;
  125. }
  126. } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
  127. mthca_info(mdev, "No PCI Express capability, "
  128. "not setting Max Read Request Size.\n");
  129. return 0;
  130. }
  131. static int mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
  132. {
  133. int err;
  134. u8 status;
  135. err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
  136. if (err) {
  137. mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
  138. return err;
  139. }
  140. if (status) {
  141. mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
  142. "aborting.\n", status);
  143. return -EINVAL;
  144. }
  145. if (dev_lim->min_page_sz > PAGE_SIZE) {
  146. mthca_err(mdev, "HCA minimum page size of %d bigger than "
  147. "kernel PAGE_SIZE of %ld, aborting.\n",
  148. dev_lim->min_page_sz, PAGE_SIZE);
  149. return -ENODEV;
  150. }
  151. if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
  152. mthca_err(mdev, "HCA has %d ports, but we only support %d, "
  153. "aborting.\n",
  154. dev_lim->num_ports, MTHCA_MAX_PORTS);
  155. return -ENODEV;
  156. }
  157. if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
  158. mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
  159. "PCI resource 2 size of 0x%llx, aborting.\n",
  160. dev_lim->uar_size,
  161. (unsigned long long)pci_resource_len(mdev->pdev, 2));
  162. return -ENODEV;
  163. }
  164. mdev->limits.num_ports = dev_lim->num_ports;
  165. mdev->limits.vl_cap = dev_lim->max_vl;
  166. mdev->limits.mtu_cap = dev_lim->max_mtu;
  167. mdev->limits.gid_table_len = dev_lim->max_gids;
  168. mdev->limits.pkey_table_len = dev_lim->max_pkeys;
  169. mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
  170. /*
  171. * Need to allow for worst case send WQE overhead and check
  172. * whether max_desc_sz imposes a lower limit than max_sg; UD
  173. * send has the biggest overhead.
  174. */
  175. mdev->limits.max_sg = min_t(int, dev_lim->max_sg,
  176. (dev_lim->max_desc_sz -
  177. sizeof (struct mthca_next_seg) -
  178. (mthca_is_memfree(mdev) ?
  179. sizeof (struct mthca_arbel_ud_seg) :
  180. sizeof (struct mthca_tavor_ud_seg))) /
  181. sizeof (struct mthca_data_seg));
  182. mdev->limits.max_wqes = dev_lim->max_qp_sz;
  183. mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
  184. mdev->limits.reserved_qps = dev_lim->reserved_qps;
  185. mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
  186. mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
  187. mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
  188. mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
  189. mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
  190. /*
  191. * Subtract 1 from the limit because we need to allocate a
  192. * spare CQE so the HCA HW can tell the difference between an
  193. * empty CQ and a full CQ.
  194. */
  195. mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
  196. mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
  197. mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
  198. mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
  199. mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
  200. mdev->limits.reserved_uars = dev_lim->reserved_uars;
  201. mdev->limits.reserved_pds = dev_lim->reserved_pds;
  202. mdev->limits.port_width_cap = dev_lim->max_port_width;
  203. mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
  204. mdev->limits.flags = dev_lim->flags;
  205. /*
  206. * For old FW that doesn't return static rate support, use a
  207. * value of 0x3 (only static rate values of 0 or 1 are handled),
  208. * except on Sinai, where even old FW can handle static rate
  209. * values of 2 and 3.
  210. */
  211. if (dev_lim->stat_rate_support)
  212. mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
  213. else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
  214. mdev->limits.stat_rate_support = 0xf;
  215. else
  216. mdev->limits.stat_rate_support = 0x3;
  217. /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
  218. May be doable since hardware supports it for SRQ.
  219. IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
  220. IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
  221. supported by driver. */
  222. mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  223. IB_DEVICE_PORT_ACTIVE_EVENT |
  224. IB_DEVICE_SYS_IMAGE_GUID |
  225. IB_DEVICE_RC_RNR_NAK_GEN;
  226. if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
  227. mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  228. if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
  229. mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  230. if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
  231. mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
  232. if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
  233. mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  234. if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
  235. mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  236. if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
  237. mdev->mthca_flags |= MTHCA_FLAG_SRQ;
  238. if (mthca_is_memfree(mdev))
  239. if (dev_lim->flags & DEV_LIM_FLAG_IPOIB_CSUM)
  240. mdev->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  241. return 0;
  242. }
  243. static int mthca_init_tavor(struct mthca_dev *mdev)
  244. {
  245. s64 size;
  246. u8 status;
  247. int err;
  248. struct mthca_dev_lim dev_lim;
  249. struct mthca_profile profile;
  250. struct mthca_init_hca_param init_hca;
  251. err = mthca_SYS_EN(mdev, &status);
  252. if (err) {
  253. mthca_err(mdev, "SYS_EN command failed, aborting.\n");
  254. return err;
  255. }
  256. if (status) {
  257. mthca_err(mdev, "SYS_EN returned status 0x%02x, "
  258. "aborting.\n", status);
  259. return -EINVAL;
  260. }
  261. err = mthca_QUERY_FW(mdev, &status);
  262. if (err) {
  263. mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
  264. goto err_disable;
  265. }
  266. if (status) {
  267. mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
  268. "aborting.\n", status);
  269. err = -EINVAL;
  270. goto err_disable;
  271. }
  272. err = mthca_QUERY_DDR(mdev, &status);
  273. if (err) {
  274. mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
  275. goto err_disable;
  276. }
  277. if (status) {
  278. mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
  279. "aborting.\n", status);
  280. err = -EINVAL;
  281. goto err_disable;
  282. }
  283. err = mthca_dev_lim(mdev, &dev_lim);
  284. if (err) {
  285. mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
  286. goto err_disable;
  287. }
  288. profile = hca_profile;
  289. profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
  290. profile.uarc_size = 0;
  291. if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
  292. profile.num_srq = dev_lim.max_srqs;
  293. size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
  294. if (size < 0) {
  295. err = size;
  296. goto err_disable;
  297. }
  298. err = mthca_INIT_HCA(mdev, &init_hca, &status);
  299. if (err) {
  300. mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
  301. goto err_disable;
  302. }
  303. if (status) {
  304. mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
  305. "aborting.\n", status);
  306. err = -EINVAL;
  307. goto err_disable;
  308. }
  309. return 0;
  310. err_disable:
  311. mthca_SYS_DIS(mdev, &status);
  312. return err;
  313. }
  314. static int mthca_load_fw(struct mthca_dev *mdev)
  315. {
  316. u8 status;
  317. int err;
  318. /* FIXME: use HCA-attached memory for FW if present */
  319. mdev->fw.arbel.fw_icm =
  320. mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
  321. GFP_HIGHUSER | __GFP_NOWARN, 0);
  322. if (!mdev->fw.arbel.fw_icm) {
  323. mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
  324. return -ENOMEM;
  325. }
  326. err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
  327. if (err) {
  328. mthca_err(mdev, "MAP_FA command failed, aborting.\n");
  329. goto err_free;
  330. }
  331. if (status) {
  332. mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
  333. err = -EINVAL;
  334. goto err_free;
  335. }
  336. err = mthca_RUN_FW(mdev, &status);
  337. if (err) {
  338. mthca_err(mdev, "RUN_FW command failed, aborting.\n");
  339. goto err_unmap_fa;
  340. }
  341. if (status) {
  342. mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
  343. err = -EINVAL;
  344. goto err_unmap_fa;
  345. }
  346. return 0;
  347. err_unmap_fa:
  348. mthca_UNMAP_FA(mdev, &status);
  349. err_free:
  350. mthca_free_icm(mdev, mdev->fw.arbel.fw_icm, 0);
  351. return err;
  352. }
  353. static int mthca_init_icm(struct mthca_dev *mdev,
  354. struct mthca_dev_lim *dev_lim,
  355. struct mthca_init_hca_param *init_hca,
  356. u64 icm_size)
  357. {
  358. u64 aux_pages;
  359. u8 status;
  360. int err;
  361. err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
  362. if (err) {
  363. mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
  364. return err;
  365. }
  366. if (status) {
  367. mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
  368. "aborting.\n", status);
  369. return -EINVAL;
  370. }
  371. mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
  372. (unsigned long long) icm_size >> 10,
  373. (unsigned long long) aux_pages << 2);
  374. mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
  375. GFP_HIGHUSER | __GFP_NOWARN, 0);
  376. if (!mdev->fw.arbel.aux_icm) {
  377. mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
  378. return -ENOMEM;
  379. }
  380. err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
  381. if (err) {
  382. mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
  383. goto err_free_aux;
  384. }
  385. if (status) {
  386. mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
  387. err = -EINVAL;
  388. goto err_free_aux;
  389. }
  390. err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
  391. if (err) {
  392. mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
  393. goto err_unmap_aux;
  394. }
  395. /* CPU writes to non-reserved MTTs, while HCA might DMA to reserved mtts */
  396. mdev->limits.reserved_mtts = ALIGN(mdev->limits.reserved_mtts * MTHCA_MTT_SEG_SIZE,
  397. dma_get_cache_alignment()) / MTHCA_MTT_SEG_SIZE;
  398. mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
  399. MTHCA_MTT_SEG_SIZE,
  400. mdev->limits.num_mtt_segs,
  401. mdev->limits.reserved_mtts,
  402. 1, 0);
  403. if (!mdev->mr_table.mtt_table) {
  404. mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
  405. err = -ENOMEM;
  406. goto err_unmap_eq;
  407. }
  408. mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
  409. dev_lim->mpt_entry_sz,
  410. mdev->limits.num_mpts,
  411. mdev->limits.reserved_mrws,
  412. 1, 1);
  413. if (!mdev->mr_table.mpt_table) {
  414. mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
  415. err = -ENOMEM;
  416. goto err_unmap_mtt;
  417. }
  418. mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
  419. dev_lim->qpc_entry_sz,
  420. mdev->limits.num_qps,
  421. mdev->limits.reserved_qps,
  422. 0, 0);
  423. if (!mdev->qp_table.qp_table) {
  424. mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
  425. err = -ENOMEM;
  426. goto err_unmap_mpt;
  427. }
  428. mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
  429. dev_lim->eqpc_entry_sz,
  430. mdev->limits.num_qps,
  431. mdev->limits.reserved_qps,
  432. 0, 0);
  433. if (!mdev->qp_table.eqp_table) {
  434. mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
  435. err = -ENOMEM;
  436. goto err_unmap_qp;
  437. }
  438. mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
  439. MTHCA_RDB_ENTRY_SIZE,
  440. mdev->limits.num_qps <<
  441. mdev->qp_table.rdb_shift, 0,
  442. 0, 0);
  443. if (!mdev->qp_table.rdb_table) {
  444. mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
  445. err = -ENOMEM;
  446. goto err_unmap_eqp;
  447. }
  448. mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
  449. dev_lim->cqc_entry_sz,
  450. mdev->limits.num_cqs,
  451. mdev->limits.reserved_cqs,
  452. 0, 0);
  453. if (!mdev->cq_table.table) {
  454. mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
  455. err = -ENOMEM;
  456. goto err_unmap_rdb;
  457. }
  458. if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
  459. mdev->srq_table.table =
  460. mthca_alloc_icm_table(mdev, init_hca->srqc_base,
  461. dev_lim->srq_entry_sz,
  462. mdev->limits.num_srqs,
  463. mdev->limits.reserved_srqs,
  464. 0, 0);
  465. if (!mdev->srq_table.table) {
  466. mthca_err(mdev, "Failed to map SRQ context memory, "
  467. "aborting.\n");
  468. err = -ENOMEM;
  469. goto err_unmap_cq;
  470. }
  471. }
  472. /*
  473. * It's not strictly required, but for simplicity just map the
  474. * whole multicast group table now. The table isn't very big
  475. * and it's a lot easier than trying to track ref counts.
  476. */
  477. mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
  478. MTHCA_MGM_ENTRY_SIZE,
  479. mdev->limits.num_mgms +
  480. mdev->limits.num_amgms,
  481. mdev->limits.num_mgms +
  482. mdev->limits.num_amgms,
  483. 0, 0);
  484. if (!mdev->mcg_table.table) {
  485. mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
  486. err = -ENOMEM;
  487. goto err_unmap_srq;
  488. }
  489. return 0;
  490. err_unmap_srq:
  491. if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
  492. mthca_free_icm_table(mdev, mdev->srq_table.table);
  493. err_unmap_cq:
  494. mthca_free_icm_table(mdev, mdev->cq_table.table);
  495. err_unmap_rdb:
  496. mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
  497. err_unmap_eqp:
  498. mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
  499. err_unmap_qp:
  500. mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
  501. err_unmap_mpt:
  502. mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
  503. err_unmap_mtt:
  504. mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
  505. err_unmap_eq:
  506. mthca_unmap_eq_icm(mdev);
  507. err_unmap_aux:
  508. mthca_UNMAP_ICM_AUX(mdev, &status);
  509. err_free_aux:
  510. mthca_free_icm(mdev, mdev->fw.arbel.aux_icm, 0);
  511. return err;
  512. }
  513. static void mthca_free_icms(struct mthca_dev *mdev)
  514. {
  515. u8 status;
  516. mthca_free_icm_table(mdev, mdev->mcg_table.table);
  517. if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
  518. mthca_free_icm_table(mdev, mdev->srq_table.table);
  519. mthca_free_icm_table(mdev, mdev->cq_table.table);
  520. mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
  521. mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
  522. mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
  523. mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
  524. mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
  525. mthca_unmap_eq_icm(mdev);
  526. mthca_UNMAP_ICM_AUX(mdev, &status);
  527. mthca_free_icm(mdev, mdev->fw.arbel.aux_icm, 0);
  528. }
  529. static int mthca_init_arbel(struct mthca_dev *mdev)
  530. {
  531. struct mthca_dev_lim dev_lim;
  532. struct mthca_profile profile;
  533. struct mthca_init_hca_param init_hca;
  534. s64 icm_size;
  535. u8 status;
  536. int err;
  537. err = mthca_QUERY_FW(mdev, &status);
  538. if (err) {
  539. mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
  540. return err;
  541. }
  542. if (status) {
  543. mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
  544. "aborting.\n", status);
  545. return -EINVAL;
  546. }
  547. err = mthca_ENABLE_LAM(mdev, &status);
  548. if (err) {
  549. mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
  550. return err;
  551. }
  552. if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
  553. mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
  554. mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
  555. } else if (status) {
  556. mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
  557. "aborting.\n", status);
  558. return -EINVAL;
  559. }
  560. err = mthca_load_fw(mdev);
  561. if (err) {
  562. mthca_err(mdev, "Failed to start FW, aborting.\n");
  563. goto err_disable;
  564. }
  565. err = mthca_dev_lim(mdev, &dev_lim);
  566. if (err) {
  567. mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
  568. goto err_stop_fw;
  569. }
  570. profile = hca_profile;
  571. profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
  572. profile.num_udav = 0;
  573. if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
  574. profile.num_srq = dev_lim.max_srqs;
  575. icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
  576. if (icm_size < 0) {
  577. err = icm_size;
  578. goto err_stop_fw;
  579. }
  580. err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
  581. if (err)
  582. goto err_stop_fw;
  583. err = mthca_INIT_HCA(mdev, &init_hca, &status);
  584. if (err) {
  585. mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
  586. goto err_free_icm;
  587. }
  588. if (status) {
  589. mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
  590. "aborting.\n", status);
  591. err = -EINVAL;
  592. goto err_free_icm;
  593. }
  594. return 0;
  595. err_free_icm:
  596. mthca_free_icms(mdev);
  597. err_stop_fw:
  598. mthca_UNMAP_FA(mdev, &status);
  599. mthca_free_icm(mdev, mdev->fw.arbel.fw_icm, 0);
  600. err_disable:
  601. if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
  602. mthca_DISABLE_LAM(mdev, &status);
  603. return err;
  604. }
  605. static void mthca_close_hca(struct mthca_dev *mdev)
  606. {
  607. u8 status;
  608. mthca_CLOSE_HCA(mdev, 0, &status);
  609. if (mthca_is_memfree(mdev)) {
  610. mthca_free_icms(mdev);
  611. mthca_UNMAP_FA(mdev, &status);
  612. mthca_free_icm(mdev, mdev->fw.arbel.fw_icm, 0);
  613. if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
  614. mthca_DISABLE_LAM(mdev, &status);
  615. } else
  616. mthca_SYS_DIS(mdev, &status);
  617. }
  618. static int mthca_init_hca(struct mthca_dev *mdev)
  619. {
  620. u8 status;
  621. int err;
  622. struct mthca_adapter adapter;
  623. if (mthca_is_memfree(mdev))
  624. err = mthca_init_arbel(mdev);
  625. else
  626. err = mthca_init_tavor(mdev);
  627. if (err)
  628. return err;
  629. err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
  630. if (err) {
  631. mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
  632. goto err_close;
  633. }
  634. if (status) {
  635. mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
  636. "aborting.\n", status);
  637. err = -EINVAL;
  638. goto err_close;
  639. }
  640. mdev->eq_table.inta_pin = adapter.inta_pin;
  641. if (!mthca_is_memfree(mdev))
  642. mdev->rev_id = adapter.revision_id;
  643. memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
  644. return 0;
  645. err_close:
  646. mthca_close_hca(mdev);
  647. return err;
  648. }
  649. static int mthca_setup_hca(struct mthca_dev *dev)
  650. {
  651. int err;
  652. u8 status;
  653. MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
  654. err = mthca_init_uar_table(dev);
  655. if (err) {
  656. mthca_err(dev, "Failed to initialize "
  657. "user access region table, aborting.\n");
  658. return err;
  659. }
  660. err = mthca_uar_alloc(dev, &dev->driver_uar);
  661. if (err) {
  662. mthca_err(dev, "Failed to allocate driver access region, "
  663. "aborting.\n");
  664. goto err_uar_table_free;
  665. }
  666. dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  667. if (!dev->kar) {
  668. mthca_err(dev, "Couldn't map kernel access region, "
  669. "aborting.\n");
  670. err = -ENOMEM;
  671. goto err_uar_free;
  672. }
  673. err = mthca_init_pd_table(dev);
  674. if (err) {
  675. mthca_err(dev, "Failed to initialize "
  676. "protection domain table, aborting.\n");
  677. goto err_kar_unmap;
  678. }
  679. err = mthca_init_mr_table(dev);
  680. if (err) {
  681. mthca_err(dev, "Failed to initialize "
  682. "memory region table, aborting.\n");
  683. goto err_pd_table_free;
  684. }
  685. err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
  686. if (err) {
  687. mthca_err(dev, "Failed to create driver PD, "
  688. "aborting.\n");
  689. goto err_mr_table_free;
  690. }
  691. err = mthca_init_eq_table(dev);
  692. if (err) {
  693. mthca_err(dev, "Failed to initialize "
  694. "event queue table, aborting.\n");
  695. goto err_pd_free;
  696. }
  697. err = mthca_cmd_use_events(dev);
  698. if (err) {
  699. mthca_err(dev, "Failed to switch to event-driven "
  700. "firmware commands, aborting.\n");
  701. goto err_eq_table_free;
  702. }
  703. err = mthca_NOP(dev, &status);
  704. if (err || status) {
  705. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  706. mthca_warn(dev, "NOP command failed to generate interrupt "
  707. "(IRQ %d).\n",
  708. dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector);
  709. mthca_warn(dev, "Trying again with MSI-X disabled.\n");
  710. } else {
  711. mthca_err(dev, "NOP command failed to generate interrupt "
  712. "(IRQ %d), aborting.\n",
  713. dev->pdev->irq);
  714. mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
  715. }
  716. goto err_cmd_poll;
  717. }
  718. mthca_dbg(dev, "NOP command IRQ test passed\n");
  719. err = mthca_init_cq_table(dev);
  720. if (err) {
  721. mthca_err(dev, "Failed to initialize "
  722. "completion queue table, aborting.\n");
  723. goto err_cmd_poll;
  724. }
  725. err = mthca_init_srq_table(dev);
  726. if (err) {
  727. mthca_err(dev, "Failed to initialize "
  728. "shared receive queue table, aborting.\n");
  729. goto err_cq_table_free;
  730. }
  731. err = mthca_init_qp_table(dev);
  732. if (err) {
  733. mthca_err(dev, "Failed to initialize "
  734. "queue pair table, aborting.\n");
  735. goto err_srq_table_free;
  736. }
  737. err = mthca_init_av_table(dev);
  738. if (err) {
  739. mthca_err(dev, "Failed to initialize "
  740. "address vector table, aborting.\n");
  741. goto err_qp_table_free;
  742. }
  743. err = mthca_init_mcg_table(dev);
  744. if (err) {
  745. mthca_err(dev, "Failed to initialize "
  746. "multicast group table, aborting.\n");
  747. goto err_av_table_free;
  748. }
  749. return 0;
  750. err_av_table_free:
  751. mthca_cleanup_av_table(dev);
  752. err_qp_table_free:
  753. mthca_cleanup_qp_table(dev);
  754. err_srq_table_free:
  755. mthca_cleanup_srq_table(dev);
  756. err_cq_table_free:
  757. mthca_cleanup_cq_table(dev);
  758. err_cmd_poll:
  759. mthca_cmd_use_polling(dev);
  760. err_eq_table_free:
  761. mthca_cleanup_eq_table(dev);
  762. err_pd_free:
  763. mthca_pd_free(dev, &dev->driver_pd);
  764. err_mr_table_free:
  765. mthca_cleanup_mr_table(dev);
  766. err_pd_table_free:
  767. mthca_cleanup_pd_table(dev);
  768. err_kar_unmap:
  769. iounmap(dev->kar);
  770. err_uar_free:
  771. mthca_uar_free(dev, &dev->driver_uar);
  772. err_uar_table_free:
  773. mthca_cleanup_uar_table(dev);
  774. return err;
  775. }
  776. static int mthca_enable_msi_x(struct mthca_dev *mdev)
  777. {
  778. struct msix_entry entries[3];
  779. int err;
  780. entries[0].entry = 0;
  781. entries[1].entry = 1;
  782. entries[2].entry = 2;
  783. err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
  784. if (err) {
  785. if (err > 0)
  786. mthca_info(mdev, "Only %d MSI-X vectors available, "
  787. "not using MSI-X\n", err);
  788. return err;
  789. }
  790. mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
  791. mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
  792. mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
  793. return 0;
  794. }
  795. /* Types of supported HCA */
  796. enum {
  797. TAVOR, /* MT23108 */
  798. ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
  799. ARBEL_NATIVE, /* MT25208 with extended features */
  800. SINAI /* MT25204 */
  801. };
  802. #define MTHCA_FW_VER(major, minor, subminor) \
  803. (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
  804. static struct {
  805. u64 latest_fw;
  806. u32 flags;
  807. } mthca_hca_table[] = {
  808. [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 5, 0),
  809. .flags = 0 },
  810. [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 8, 200),
  811. .flags = MTHCA_FLAG_PCIE },
  812. [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 3, 0),
  813. .flags = MTHCA_FLAG_MEMFREE |
  814. MTHCA_FLAG_PCIE },
  815. [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 2, 0),
  816. .flags = MTHCA_FLAG_MEMFREE |
  817. MTHCA_FLAG_PCIE |
  818. MTHCA_FLAG_SINAI_OPT }
  819. };
  820. static int __mthca_init_one(struct pci_dev *pdev, int hca_type)
  821. {
  822. int ddr_hidden = 0;
  823. int err;
  824. struct mthca_dev *mdev;
  825. printk(KERN_INFO PFX "Initializing %s\n",
  826. pci_name(pdev));
  827. err = pci_enable_device(pdev);
  828. if (err) {
  829. dev_err(&pdev->dev, "Cannot enable PCI device, "
  830. "aborting.\n");
  831. return err;
  832. }
  833. /*
  834. * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
  835. * be present)
  836. */
  837. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  838. pci_resource_len(pdev, 0) != 1 << 20) {
  839. dev_err(&pdev->dev, "Missing DCS, aborting.\n");
  840. err = -ENODEV;
  841. goto err_disable_pdev;
  842. }
  843. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  844. dev_err(&pdev->dev, "Missing UAR, aborting.\n");
  845. err = -ENODEV;
  846. goto err_disable_pdev;
  847. }
  848. if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
  849. ddr_hidden = 1;
  850. err = pci_request_regions(pdev, DRV_NAME);
  851. if (err) {
  852. dev_err(&pdev->dev, "Cannot obtain PCI resources, "
  853. "aborting.\n");
  854. goto err_disable_pdev;
  855. }
  856. pci_set_master(pdev);
  857. err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  858. if (err) {
  859. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
  860. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  861. if (err) {
  862. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
  863. goto err_free_res;
  864. }
  865. }
  866. err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  867. if (err) {
  868. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
  869. "consistent PCI DMA mask.\n");
  870. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  871. if (err) {
  872. dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
  873. "aborting.\n");
  874. goto err_free_res;
  875. }
  876. }
  877. mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
  878. if (!mdev) {
  879. dev_err(&pdev->dev, "Device struct alloc failed, "
  880. "aborting.\n");
  881. err = -ENOMEM;
  882. goto err_free_res;
  883. }
  884. mdev->pdev = pdev;
  885. mdev->mthca_flags = mthca_hca_table[hca_type].flags;
  886. if (ddr_hidden)
  887. mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
  888. /*
  889. * Now reset the HCA before we touch the PCI capabilities or
  890. * attempt a firmware command, since a boot ROM may have left
  891. * the HCA in an undefined state.
  892. */
  893. err = mthca_reset(mdev);
  894. if (err) {
  895. mthca_err(mdev, "Failed to reset HCA, aborting.\n");
  896. goto err_free_dev;
  897. }
  898. if (mthca_cmd_init(mdev)) {
  899. mthca_err(mdev, "Failed to init command interface, aborting.\n");
  900. goto err_free_dev;
  901. }
  902. err = mthca_tune_pci(mdev);
  903. if (err)
  904. goto err_cmd;
  905. err = mthca_init_hca(mdev);
  906. if (err)
  907. goto err_cmd;
  908. if (mdev->fw_ver < mthca_hca_table[hca_type].latest_fw) {
  909. mthca_warn(mdev, "HCA FW version %d.%d.%03d is old (%d.%d.%03d is current).\n",
  910. (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
  911. (int) (mdev->fw_ver & 0xffff),
  912. (int) (mthca_hca_table[hca_type].latest_fw >> 32),
  913. (int) (mthca_hca_table[hca_type].latest_fw >> 16) & 0xffff,
  914. (int) (mthca_hca_table[hca_type].latest_fw & 0xffff));
  915. mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
  916. }
  917. if (msi_x && !mthca_enable_msi_x(mdev))
  918. mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
  919. err = mthca_setup_hca(mdev);
  920. if (err == -EBUSY && (mdev->mthca_flags & MTHCA_FLAG_MSI_X)) {
  921. if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
  922. pci_disable_msix(pdev);
  923. mdev->mthca_flags &= ~MTHCA_FLAG_MSI_X;
  924. err = mthca_setup_hca(mdev);
  925. }
  926. if (err)
  927. goto err_close;
  928. err = mthca_register_device(mdev);
  929. if (err)
  930. goto err_cleanup;
  931. err = mthca_create_agents(mdev);
  932. if (err)
  933. goto err_unregister;
  934. pci_set_drvdata(pdev, mdev);
  935. mdev->hca_type = hca_type;
  936. return 0;
  937. err_unregister:
  938. mthca_unregister_device(mdev);
  939. err_cleanup:
  940. mthca_cleanup_mcg_table(mdev);
  941. mthca_cleanup_av_table(mdev);
  942. mthca_cleanup_qp_table(mdev);
  943. mthca_cleanup_srq_table(mdev);
  944. mthca_cleanup_cq_table(mdev);
  945. mthca_cmd_use_polling(mdev);
  946. mthca_cleanup_eq_table(mdev);
  947. mthca_pd_free(mdev, &mdev->driver_pd);
  948. mthca_cleanup_mr_table(mdev);
  949. mthca_cleanup_pd_table(mdev);
  950. mthca_cleanup_uar_table(mdev);
  951. err_close:
  952. if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
  953. pci_disable_msix(pdev);
  954. mthca_close_hca(mdev);
  955. err_cmd:
  956. mthca_cmd_cleanup(mdev);
  957. err_free_dev:
  958. ib_dealloc_device(&mdev->ib_dev);
  959. err_free_res:
  960. pci_release_regions(pdev);
  961. err_disable_pdev:
  962. pci_disable_device(pdev);
  963. pci_set_drvdata(pdev, NULL);
  964. return err;
  965. }
  966. static void __mthca_remove_one(struct pci_dev *pdev)
  967. {
  968. struct mthca_dev *mdev = pci_get_drvdata(pdev);
  969. u8 status;
  970. int p;
  971. if (mdev) {
  972. mthca_free_agents(mdev);
  973. mthca_unregister_device(mdev);
  974. for (p = 1; p <= mdev->limits.num_ports; ++p)
  975. mthca_CLOSE_IB(mdev, p, &status);
  976. mthca_cleanup_mcg_table(mdev);
  977. mthca_cleanup_av_table(mdev);
  978. mthca_cleanup_qp_table(mdev);
  979. mthca_cleanup_srq_table(mdev);
  980. mthca_cleanup_cq_table(mdev);
  981. mthca_cmd_use_polling(mdev);
  982. mthca_cleanup_eq_table(mdev);
  983. mthca_pd_free(mdev, &mdev->driver_pd);
  984. mthca_cleanup_mr_table(mdev);
  985. mthca_cleanup_pd_table(mdev);
  986. iounmap(mdev->kar);
  987. mthca_uar_free(mdev, &mdev->driver_uar);
  988. mthca_cleanup_uar_table(mdev);
  989. mthca_close_hca(mdev);
  990. mthca_cmd_cleanup(mdev);
  991. if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
  992. pci_disable_msix(pdev);
  993. ib_dealloc_device(&mdev->ib_dev);
  994. pci_release_regions(pdev);
  995. pci_disable_device(pdev);
  996. pci_set_drvdata(pdev, NULL);
  997. }
  998. }
  999. int __mthca_restart_one(struct pci_dev *pdev)
  1000. {
  1001. struct mthca_dev *mdev;
  1002. int hca_type;
  1003. mdev = pci_get_drvdata(pdev);
  1004. if (!mdev)
  1005. return -ENODEV;
  1006. hca_type = mdev->hca_type;
  1007. __mthca_remove_one(pdev);
  1008. return __mthca_init_one(pdev, hca_type);
  1009. }
  1010. static int __devinit mthca_init_one(struct pci_dev *pdev,
  1011. const struct pci_device_id *id)
  1012. {
  1013. static int mthca_version_printed = 0;
  1014. int ret;
  1015. mutex_lock(&mthca_device_mutex);
  1016. if (!mthca_version_printed) {
  1017. printk(KERN_INFO "%s", mthca_version);
  1018. ++mthca_version_printed;
  1019. }
  1020. if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
  1021. printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
  1022. pci_name(pdev), id->driver_data);
  1023. mutex_unlock(&mthca_device_mutex);
  1024. return -ENODEV;
  1025. }
  1026. ret = __mthca_init_one(pdev, id->driver_data);
  1027. mutex_unlock(&mthca_device_mutex);
  1028. return ret;
  1029. }
  1030. static void __devexit mthca_remove_one(struct pci_dev *pdev)
  1031. {
  1032. mutex_lock(&mthca_device_mutex);
  1033. __mthca_remove_one(pdev);
  1034. mutex_unlock(&mthca_device_mutex);
  1035. }
  1036. static struct pci_device_id mthca_pci_table[] = {
  1037. { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
  1038. .driver_data = TAVOR },
  1039. { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
  1040. .driver_data = TAVOR },
  1041. { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
  1042. .driver_data = ARBEL_COMPAT },
  1043. { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
  1044. .driver_data = ARBEL_COMPAT },
  1045. { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
  1046. .driver_data = ARBEL_NATIVE },
  1047. { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
  1048. .driver_data = ARBEL_NATIVE },
  1049. { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
  1050. .driver_data = SINAI },
  1051. { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
  1052. .driver_data = SINAI },
  1053. { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
  1054. .driver_data = SINAI },
  1055. { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
  1056. .driver_data = SINAI },
  1057. { 0, }
  1058. };
  1059. MODULE_DEVICE_TABLE(pci, mthca_pci_table);
  1060. static struct pci_driver mthca_driver = {
  1061. .name = DRV_NAME,
  1062. .id_table = mthca_pci_table,
  1063. .probe = mthca_init_one,
  1064. .remove = __devexit_p(mthca_remove_one)
  1065. };
  1066. static void __init __mthca_check_profile_val(const char *name, int *pval,
  1067. int pval_default)
  1068. {
  1069. /* value must be positive and power of 2 */
  1070. int old_pval = *pval;
  1071. if (old_pval <= 0)
  1072. *pval = pval_default;
  1073. else
  1074. *pval = roundup_pow_of_two(old_pval);
  1075. if (old_pval != *pval) {
  1076. printk(KERN_WARNING PFX "Invalid value %d for %s in module parameter.\n",
  1077. old_pval, name);
  1078. printk(KERN_WARNING PFX "Corrected %s to %d.\n", name, *pval);
  1079. }
  1080. }
  1081. #define mthca_check_profile_val(name, default) \
  1082. __mthca_check_profile_val(#name, &hca_profile.name, default)
  1083. static void __init mthca_validate_profile(void)
  1084. {
  1085. mthca_check_profile_val(num_qp, MTHCA_DEFAULT_NUM_QP);
  1086. mthca_check_profile_val(rdb_per_qp, MTHCA_DEFAULT_RDB_PER_QP);
  1087. mthca_check_profile_val(num_cq, MTHCA_DEFAULT_NUM_CQ);
  1088. mthca_check_profile_val(num_mcg, MTHCA_DEFAULT_NUM_MCG);
  1089. mthca_check_profile_val(num_mpt, MTHCA_DEFAULT_NUM_MPT);
  1090. mthca_check_profile_val(num_mtt, MTHCA_DEFAULT_NUM_MTT);
  1091. mthca_check_profile_val(num_udav, MTHCA_DEFAULT_NUM_UDAV);
  1092. mthca_check_profile_val(fmr_reserved_mtts, MTHCA_DEFAULT_NUM_RESERVED_MTTS);
  1093. if (hca_profile.fmr_reserved_mtts >= hca_profile.num_mtt) {
  1094. printk(KERN_WARNING PFX "Invalid fmr_reserved_mtts module parameter %d.\n",
  1095. hca_profile.fmr_reserved_mtts);
  1096. printk(KERN_WARNING PFX "(Must be smaller than num_mtt %d)\n",
  1097. hca_profile.num_mtt);
  1098. hca_profile.fmr_reserved_mtts = hca_profile.num_mtt / 2;
  1099. printk(KERN_WARNING PFX "Corrected fmr_reserved_mtts to %d.\n",
  1100. hca_profile.fmr_reserved_mtts);
  1101. }
  1102. }
  1103. static int __init mthca_init(void)
  1104. {
  1105. int ret;
  1106. mthca_validate_profile();
  1107. ret = mthca_catas_init();
  1108. if (ret)
  1109. return ret;
  1110. ret = pci_register_driver(&mthca_driver);
  1111. if (ret < 0) {
  1112. mthca_catas_cleanup();
  1113. return ret;
  1114. }
  1115. return 0;
  1116. }
  1117. static void __exit mthca_cleanup(void)
  1118. {
  1119. pci_unregister_driver(&mthca_driver);
  1120. mthca_catas_cleanup();
  1121. }
  1122. module_init(mthca_init);
  1123. module_exit(mthca_cleanup);