main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/errno.h>
  38. #include <linux/pci.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/slab.h>
  41. #include <linux/mlx4/device.h>
  42. #include <linux/mlx4/doorbell.h>
  43. #include "mlx4.h"
  44. #include "fw.h"
  45. #include "icm.h"
  46. MODULE_AUTHOR("Roland Dreier");
  47. MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
  48. MODULE_LICENSE("Dual BSD/GPL");
  49. MODULE_VERSION(DRV_VERSION);
  50. struct workqueue_struct *mlx4_wq;
  51. #ifdef CONFIG_MLX4_DEBUG
  52. int mlx4_debug_level = 0;
  53. module_param_named(debug_level, mlx4_debug_level, int, 0644);
  54. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  55. #endif /* CONFIG_MLX4_DEBUG */
  56. #ifdef CONFIG_PCI_MSI
  57. static int msi_x = 1;
  58. module_param(msi_x, int, 0444);
  59. MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
  60. #else /* CONFIG_PCI_MSI */
  61. #define msi_x (0)
  62. #endif /* CONFIG_PCI_MSI */
  63. static char mlx4_version[] __devinitdata =
  64. DRV_NAME ": Mellanox ConnectX core driver v"
  65. DRV_VERSION " (" DRV_RELDATE ")\n";
  66. static struct mlx4_profile default_profile = {
  67. .num_qp = 1 << 17,
  68. .num_srq = 1 << 16,
  69. .rdmarc_per_qp = 1 << 4,
  70. .num_cq = 1 << 16,
  71. .num_mcg = 1 << 13,
  72. .num_mpt = 1 << 17,
  73. .num_mtt = 1 << 20,
  74. };
  75. static int log_num_mac = 2;
  76. module_param_named(log_num_mac, log_num_mac, int, 0444);
  77. MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
  78. static int log_num_vlan;
  79. module_param_named(log_num_vlan, log_num_vlan, int, 0444);
  80. MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
  81. static int use_prio;
  82. module_param_named(use_prio, use_prio, bool, 0444);
  83. MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports "
  84. "(0/1, default 0)");
  85. static int log_mtts_per_seg = ilog2(MLX4_MTT_ENTRY_PER_SEG);
  86. module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
  87. MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment (1-5)");
  88. int mlx4_check_port_params(struct mlx4_dev *dev,
  89. enum mlx4_port_type *port_type)
  90. {
  91. int i;
  92. for (i = 0; i < dev->caps.num_ports - 1; i++) {
  93. if (port_type[i] != port_type[i + 1]) {
  94. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
  95. mlx4_err(dev, "Only same port types supported "
  96. "on this HCA, aborting.\n");
  97. return -EINVAL;
  98. }
  99. if (port_type[i] == MLX4_PORT_TYPE_ETH &&
  100. port_type[i + 1] == MLX4_PORT_TYPE_IB)
  101. return -EINVAL;
  102. }
  103. }
  104. for (i = 0; i < dev->caps.num_ports; i++) {
  105. if (!(port_type[i] & dev->caps.supported_type[i+1])) {
  106. mlx4_err(dev, "Requested port type for port %d is not "
  107. "supported on this HCA\n", i + 1);
  108. return -EINVAL;
  109. }
  110. }
  111. return 0;
  112. }
  113. static void mlx4_set_port_mask(struct mlx4_dev *dev)
  114. {
  115. int i;
  116. dev->caps.port_mask = 0;
  117. for (i = 1; i <= dev->caps.num_ports; ++i)
  118. if (dev->caps.port_type[i] == MLX4_PORT_TYPE_IB)
  119. dev->caps.port_mask |= 1 << (i - 1);
  120. }
  121. static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
  122. {
  123. int err;
  124. int i;
  125. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  126. if (err) {
  127. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  128. return err;
  129. }
  130. if (dev_cap->min_page_sz > PAGE_SIZE) {
  131. mlx4_err(dev, "HCA minimum page size of %d bigger than "
  132. "kernel PAGE_SIZE of %ld, aborting.\n",
  133. dev_cap->min_page_sz, PAGE_SIZE);
  134. return -ENODEV;
  135. }
  136. if (dev_cap->num_ports > MLX4_MAX_PORTS) {
  137. mlx4_err(dev, "HCA has %d ports, but we only support %d, "
  138. "aborting.\n",
  139. dev_cap->num_ports, MLX4_MAX_PORTS);
  140. return -ENODEV;
  141. }
  142. if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
  143. mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
  144. "PCI resource 2 size of 0x%llx, aborting.\n",
  145. dev_cap->uar_size,
  146. (unsigned long long) pci_resource_len(dev->pdev, 2));
  147. return -ENODEV;
  148. }
  149. dev->caps.num_ports = dev_cap->num_ports;
  150. for (i = 1; i <= dev->caps.num_ports; ++i) {
  151. dev->caps.vl_cap[i] = dev_cap->max_vl[i];
  152. dev->caps.ib_mtu_cap[i] = dev_cap->ib_mtu[i];
  153. dev->caps.gid_table_len[i] = dev_cap->max_gids[i];
  154. dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
  155. dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
  156. dev->caps.eth_mtu_cap[i] = dev_cap->eth_mtu[i];
  157. dev->caps.def_mac[i] = dev_cap->def_mac[i];
  158. dev->caps.supported_type[i] = dev_cap->supported_port_types[i];
  159. }
  160. dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
  161. dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
  162. dev->caps.bf_reg_size = dev_cap->bf_reg_size;
  163. dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
  164. dev->caps.max_sq_sg = dev_cap->max_sq_sg;
  165. dev->caps.max_rq_sg = dev_cap->max_rq_sg;
  166. dev->caps.max_wqes = dev_cap->max_qp_sz;
  167. dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
  168. dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
  169. dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
  170. dev->caps.reserved_srqs = dev_cap->reserved_srqs;
  171. dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
  172. dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
  173. dev->caps.num_qp_per_mgm = MLX4_QP_PER_MGM;
  174. /*
  175. * Subtract 1 from the limit because we need to allocate a
  176. * spare CQE so the HCA HW can tell the difference between an
  177. * empty CQ and a full CQ.
  178. */
  179. dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
  180. dev->caps.reserved_cqs = dev_cap->reserved_cqs;
  181. dev->caps.reserved_eqs = dev_cap->reserved_eqs;
  182. dev->caps.mtts_per_seg = 1 << log_mtts_per_seg;
  183. dev->caps.reserved_mtts = DIV_ROUND_UP(dev_cap->reserved_mtts,
  184. dev->caps.mtts_per_seg);
  185. dev->caps.reserved_mrws = dev_cap->reserved_mrws;
  186. dev->caps.reserved_uars = dev_cap->reserved_uars;
  187. dev->caps.reserved_pds = dev_cap->reserved_pds;
  188. dev->caps.mtt_entry_sz = dev->caps.mtts_per_seg * dev_cap->mtt_entry_sz;
  189. dev->caps.max_msg_sz = dev_cap->max_msg_sz;
  190. dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
  191. dev->caps.flags = dev_cap->flags;
  192. dev->caps.bmme_flags = dev_cap->bmme_flags;
  193. dev->caps.reserved_lkey = dev_cap->reserved_lkey;
  194. dev->caps.stat_rate_support = dev_cap->stat_rate_support;
  195. dev->caps.max_gso_sz = dev_cap->max_gso_sz;
  196. dev->caps.log_num_macs = log_num_mac;
  197. dev->caps.log_num_vlans = log_num_vlan;
  198. dev->caps.log_num_prios = use_prio ? 3 : 0;
  199. for (i = 1; i <= dev->caps.num_ports; ++i) {
  200. if (dev->caps.supported_type[i] != MLX4_PORT_TYPE_ETH)
  201. dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
  202. else
  203. dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
  204. dev->caps.possible_type[i] = dev->caps.port_type[i];
  205. mlx4_priv(dev)->sense.sense_allowed[i] =
  206. dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO;
  207. if (dev->caps.log_num_macs > dev_cap->log_max_macs[i]) {
  208. dev->caps.log_num_macs = dev_cap->log_max_macs[i];
  209. mlx4_warn(dev, "Requested number of MACs is too much "
  210. "for port %d, reducing to %d.\n",
  211. i, 1 << dev->caps.log_num_macs);
  212. }
  213. if (dev->caps.log_num_vlans > dev_cap->log_max_vlans[i]) {
  214. dev->caps.log_num_vlans = dev_cap->log_max_vlans[i];
  215. mlx4_warn(dev, "Requested number of VLANs is too much "
  216. "for port %d, reducing to %d.\n",
  217. i, 1 << dev->caps.log_num_vlans);
  218. }
  219. }
  220. mlx4_set_port_mask(dev);
  221. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
  222. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
  223. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
  224. (1 << dev->caps.log_num_macs) *
  225. (1 << dev->caps.log_num_vlans) *
  226. (1 << dev->caps.log_num_prios) *
  227. dev->caps.num_ports;
  228. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
  229. dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
  230. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
  231. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
  232. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
  233. return 0;
  234. }
  235. /*
  236. * Change the port configuration of the device.
  237. * Every user of this function must hold the port mutex.
  238. */
  239. int mlx4_change_port_types(struct mlx4_dev *dev,
  240. enum mlx4_port_type *port_types)
  241. {
  242. int err = 0;
  243. int change = 0;
  244. int port;
  245. for (port = 0; port < dev->caps.num_ports; port++) {
  246. /* Change the port type only if the new type is different
  247. * from the current, and not set to Auto */
  248. if (port_types[port] != dev->caps.port_type[port + 1]) {
  249. change = 1;
  250. dev->caps.port_type[port + 1] = port_types[port];
  251. }
  252. }
  253. if (change) {
  254. mlx4_unregister_device(dev);
  255. for (port = 1; port <= dev->caps.num_ports; port++) {
  256. mlx4_CLOSE_PORT(dev, port);
  257. err = mlx4_SET_PORT(dev, port);
  258. if (err) {
  259. mlx4_err(dev, "Failed to set port %d, "
  260. "aborting\n", port);
  261. goto out;
  262. }
  263. }
  264. mlx4_set_port_mask(dev);
  265. err = mlx4_register_device(dev);
  266. }
  267. out:
  268. return err;
  269. }
  270. static ssize_t show_port_type(struct device *dev,
  271. struct device_attribute *attr,
  272. char *buf)
  273. {
  274. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  275. port_attr);
  276. struct mlx4_dev *mdev = info->dev;
  277. char type[8];
  278. sprintf(type, "%s",
  279. (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
  280. "ib" : "eth");
  281. if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
  282. sprintf(buf, "auto (%s)\n", type);
  283. else
  284. sprintf(buf, "%s\n", type);
  285. return strlen(buf);
  286. }
  287. static ssize_t set_port_type(struct device *dev,
  288. struct device_attribute *attr,
  289. const char *buf, size_t count)
  290. {
  291. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  292. port_attr);
  293. struct mlx4_dev *mdev = info->dev;
  294. struct mlx4_priv *priv = mlx4_priv(mdev);
  295. enum mlx4_port_type types[MLX4_MAX_PORTS];
  296. enum mlx4_port_type new_types[MLX4_MAX_PORTS];
  297. int i;
  298. int err = 0;
  299. if (!strcmp(buf, "ib\n"))
  300. info->tmp_type = MLX4_PORT_TYPE_IB;
  301. else if (!strcmp(buf, "eth\n"))
  302. info->tmp_type = MLX4_PORT_TYPE_ETH;
  303. else if (!strcmp(buf, "auto\n"))
  304. info->tmp_type = MLX4_PORT_TYPE_AUTO;
  305. else {
  306. mlx4_err(mdev, "%s is not supported port type\n", buf);
  307. return -EINVAL;
  308. }
  309. mlx4_stop_sense(mdev);
  310. mutex_lock(&priv->port_mutex);
  311. /* Possible type is always the one that was delivered */
  312. mdev->caps.possible_type[info->port] = info->tmp_type;
  313. for (i = 0; i < mdev->caps.num_ports; i++) {
  314. types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
  315. mdev->caps.possible_type[i+1];
  316. if (types[i] == MLX4_PORT_TYPE_AUTO)
  317. types[i] = mdev->caps.port_type[i+1];
  318. }
  319. if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
  320. for (i = 1; i <= mdev->caps.num_ports; i++) {
  321. if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
  322. mdev->caps.possible_type[i] = mdev->caps.port_type[i];
  323. err = -EINVAL;
  324. }
  325. }
  326. }
  327. if (err) {
  328. mlx4_err(mdev, "Auto sensing is not supported on this HCA. "
  329. "Set only 'eth' or 'ib' for both ports "
  330. "(should be the same)\n");
  331. goto out;
  332. }
  333. mlx4_do_sense_ports(mdev, new_types, types);
  334. err = mlx4_check_port_params(mdev, new_types);
  335. if (err)
  336. goto out;
  337. /* We are about to apply the changes after the configuration
  338. * was verified, no need to remember the temporary types
  339. * any more */
  340. for (i = 0; i < mdev->caps.num_ports; i++)
  341. priv->port[i + 1].tmp_type = 0;
  342. err = mlx4_change_port_types(mdev, new_types);
  343. out:
  344. mlx4_start_sense(mdev);
  345. mutex_unlock(&priv->port_mutex);
  346. return err ? err : count;
  347. }
  348. static int mlx4_load_fw(struct mlx4_dev *dev)
  349. {
  350. struct mlx4_priv *priv = mlx4_priv(dev);
  351. int err;
  352. priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
  353. GFP_HIGHUSER | __GFP_NOWARN, 0);
  354. if (!priv->fw.fw_icm) {
  355. mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
  356. return -ENOMEM;
  357. }
  358. err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
  359. if (err) {
  360. mlx4_err(dev, "MAP_FA command failed, aborting.\n");
  361. goto err_free;
  362. }
  363. err = mlx4_RUN_FW(dev);
  364. if (err) {
  365. mlx4_err(dev, "RUN_FW command failed, aborting.\n");
  366. goto err_unmap_fa;
  367. }
  368. return 0;
  369. err_unmap_fa:
  370. mlx4_UNMAP_FA(dev);
  371. err_free:
  372. mlx4_free_icm(dev, priv->fw.fw_icm, 0);
  373. return err;
  374. }
  375. static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
  376. int cmpt_entry_sz)
  377. {
  378. struct mlx4_priv *priv = mlx4_priv(dev);
  379. int err;
  380. err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
  381. cmpt_base +
  382. ((u64) (MLX4_CMPT_TYPE_QP *
  383. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  384. cmpt_entry_sz, dev->caps.num_qps,
  385. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  386. 0, 0);
  387. if (err)
  388. goto err;
  389. err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
  390. cmpt_base +
  391. ((u64) (MLX4_CMPT_TYPE_SRQ *
  392. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  393. cmpt_entry_sz, dev->caps.num_srqs,
  394. dev->caps.reserved_srqs, 0, 0);
  395. if (err)
  396. goto err_qp;
  397. err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
  398. cmpt_base +
  399. ((u64) (MLX4_CMPT_TYPE_CQ *
  400. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  401. cmpt_entry_sz, dev->caps.num_cqs,
  402. dev->caps.reserved_cqs, 0, 0);
  403. if (err)
  404. goto err_srq;
  405. err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
  406. cmpt_base +
  407. ((u64) (MLX4_CMPT_TYPE_EQ *
  408. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  409. cmpt_entry_sz,
  410. dev->caps.num_eqs, dev->caps.num_eqs, 0, 0);
  411. if (err)
  412. goto err_cq;
  413. return 0;
  414. err_cq:
  415. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  416. err_srq:
  417. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  418. err_qp:
  419. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  420. err:
  421. return err;
  422. }
  423. static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  424. struct mlx4_init_hca_param *init_hca, u64 icm_size)
  425. {
  426. struct mlx4_priv *priv = mlx4_priv(dev);
  427. u64 aux_pages;
  428. int err;
  429. err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
  430. if (err) {
  431. mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
  432. return err;
  433. }
  434. mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
  435. (unsigned long long) icm_size >> 10,
  436. (unsigned long long) aux_pages << 2);
  437. priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
  438. GFP_HIGHUSER | __GFP_NOWARN, 0);
  439. if (!priv->fw.aux_icm) {
  440. mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
  441. return -ENOMEM;
  442. }
  443. err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
  444. if (err) {
  445. mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
  446. goto err_free_aux;
  447. }
  448. err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
  449. if (err) {
  450. mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
  451. goto err_unmap_aux;
  452. }
  453. err = mlx4_init_icm_table(dev, &priv->eq_table.table,
  454. init_hca->eqc_base, dev_cap->eqc_entry_sz,
  455. dev->caps.num_eqs, dev->caps.num_eqs,
  456. 0, 0);
  457. if (err) {
  458. mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
  459. goto err_unmap_cmpt;
  460. }
  461. /*
  462. * Reserved MTT entries must be aligned up to a cacheline
  463. * boundary, since the FW will write to them, while the driver
  464. * writes to all other MTT entries. (The variable
  465. * dev->caps.mtt_entry_sz below is really the MTT segment
  466. * size, not the raw entry size)
  467. */
  468. dev->caps.reserved_mtts =
  469. ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
  470. dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
  471. err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
  472. init_hca->mtt_base,
  473. dev->caps.mtt_entry_sz,
  474. dev->caps.num_mtt_segs,
  475. dev->caps.reserved_mtts, 1, 0);
  476. if (err) {
  477. mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
  478. goto err_unmap_eq;
  479. }
  480. err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
  481. init_hca->dmpt_base,
  482. dev_cap->dmpt_entry_sz,
  483. dev->caps.num_mpts,
  484. dev->caps.reserved_mrws, 1, 1);
  485. if (err) {
  486. mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
  487. goto err_unmap_mtt;
  488. }
  489. err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
  490. init_hca->qpc_base,
  491. dev_cap->qpc_entry_sz,
  492. dev->caps.num_qps,
  493. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  494. 0, 0);
  495. if (err) {
  496. mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
  497. goto err_unmap_dmpt;
  498. }
  499. err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
  500. init_hca->auxc_base,
  501. dev_cap->aux_entry_sz,
  502. dev->caps.num_qps,
  503. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  504. 0, 0);
  505. if (err) {
  506. mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
  507. goto err_unmap_qp;
  508. }
  509. err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
  510. init_hca->altc_base,
  511. dev_cap->altc_entry_sz,
  512. dev->caps.num_qps,
  513. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  514. 0, 0);
  515. if (err) {
  516. mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
  517. goto err_unmap_auxc;
  518. }
  519. err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
  520. init_hca->rdmarc_base,
  521. dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
  522. dev->caps.num_qps,
  523. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  524. 0, 0);
  525. if (err) {
  526. mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
  527. goto err_unmap_altc;
  528. }
  529. err = mlx4_init_icm_table(dev, &priv->cq_table.table,
  530. init_hca->cqc_base,
  531. dev_cap->cqc_entry_sz,
  532. dev->caps.num_cqs,
  533. dev->caps.reserved_cqs, 0, 0);
  534. if (err) {
  535. mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
  536. goto err_unmap_rdmarc;
  537. }
  538. err = mlx4_init_icm_table(dev, &priv->srq_table.table,
  539. init_hca->srqc_base,
  540. dev_cap->srq_entry_sz,
  541. dev->caps.num_srqs,
  542. dev->caps.reserved_srqs, 0, 0);
  543. if (err) {
  544. mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
  545. goto err_unmap_cq;
  546. }
  547. /*
  548. * It's not strictly required, but for simplicity just map the
  549. * whole multicast group table now. The table isn't very big
  550. * and it's a lot easier than trying to track ref counts.
  551. */
  552. err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
  553. init_hca->mc_base, MLX4_MGM_ENTRY_SIZE,
  554. dev->caps.num_mgms + dev->caps.num_amgms,
  555. dev->caps.num_mgms + dev->caps.num_amgms,
  556. 0, 0);
  557. if (err) {
  558. mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
  559. goto err_unmap_srq;
  560. }
  561. return 0;
  562. err_unmap_srq:
  563. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  564. err_unmap_cq:
  565. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  566. err_unmap_rdmarc:
  567. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  568. err_unmap_altc:
  569. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  570. err_unmap_auxc:
  571. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  572. err_unmap_qp:
  573. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  574. err_unmap_dmpt:
  575. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  576. err_unmap_mtt:
  577. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  578. err_unmap_eq:
  579. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  580. err_unmap_cmpt:
  581. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  582. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  583. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  584. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  585. err_unmap_aux:
  586. mlx4_UNMAP_ICM_AUX(dev);
  587. err_free_aux:
  588. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  589. return err;
  590. }
  591. static void mlx4_free_icms(struct mlx4_dev *dev)
  592. {
  593. struct mlx4_priv *priv = mlx4_priv(dev);
  594. mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
  595. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  596. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  597. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  598. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  599. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  600. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  601. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  602. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  603. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  604. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  605. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  606. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  607. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  608. mlx4_UNMAP_ICM_AUX(dev);
  609. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  610. }
  611. static void mlx4_close_hca(struct mlx4_dev *dev)
  612. {
  613. mlx4_CLOSE_HCA(dev, 0);
  614. mlx4_free_icms(dev);
  615. mlx4_UNMAP_FA(dev);
  616. mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
  617. }
  618. static int mlx4_init_hca(struct mlx4_dev *dev)
  619. {
  620. struct mlx4_priv *priv = mlx4_priv(dev);
  621. struct mlx4_adapter adapter;
  622. struct mlx4_dev_cap dev_cap;
  623. struct mlx4_mod_stat_cfg mlx4_cfg;
  624. struct mlx4_profile profile;
  625. struct mlx4_init_hca_param init_hca;
  626. u64 icm_size;
  627. int err;
  628. err = mlx4_QUERY_FW(dev);
  629. if (err) {
  630. if (err == -EACCES)
  631. mlx4_info(dev, "non-primary physical function, skipping.\n");
  632. else
  633. mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
  634. return err;
  635. }
  636. err = mlx4_load_fw(dev);
  637. if (err) {
  638. mlx4_err(dev, "Failed to start FW, aborting.\n");
  639. return err;
  640. }
  641. mlx4_cfg.log_pg_sz_m = 1;
  642. mlx4_cfg.log_pg_sz = 0;
  643. err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
  644. if (err)
  645. mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
  646. err = mlx4_dev_cap(dev, &dev_cap);
  647. if (err) {
  648. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  649. goto err_stop_fw;
  650. }
  651. profile = default_profile;
  652. icm_size = mlx4_make_profile(dev, &profile, &dev_cap, &init_hca);
  653. if ((long long) icm_size < 0) {
  654. err = icm_size;
  655. goto err_stop_fw;
  656. }
  657. init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
  658. err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
  659. if (err)
  660. goto err_stop_fw;
  661. err = mlx4_INIT_HCA(dev, &init_hca);
  662. if (err) {
  663. mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
  664. goto err_free_icm;
  665. }
  666. err = mlx4_QUERY_ADAPTER(dev, &adapter);
  667. if (err) {
  668. mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
  669. goto err_close;
  670. }
  671. priv->eq_table.inta_pin = adapter.inta_pin;
  672. memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
  673. return 0;
  674. err_close:
  675. mlx4_CLOSE_HCA(dev, 0);
  676. err_free_icm:
  677. mlx4_free_icms(dev);
  678. err_stop_fw:
  679. mlx4_UNMAP_FA(dev);
  680. mlx4_free_icm(dev, priv->fw.fw_icm, 0);
  681. return err;
  682. }
  683. static int mlx4_setup_hca(struct mlx4_dev *dev)
  684. {
  685. struct mlx4_priv *priv = mlx4_priv(dev);
  686. int err;
  687. int port;
  688. __be32 ib_port_default_caps;
  689. err = mlx4_init_uar_table(dev);
  690. if (err) {
  691. mlx4_err(dev, "Failed to initialize "
  692. "user access region table, aborting.\n");
  693. return err;
  694. }
  695. err = mlx4_uar_alloc(dev, &priv->driver_uar);
  696. if (err) {
  697. mlx4_err(dev, "Failed to allocate driver access region, "
  698. "aborting.\n");
  699. goto err_uar_table_free;
  700. }
  701. priv->kar = ioremap(priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  702. if (!priv->kar) {
  703. mlx4_err(dev, "Couldn't map kernel access region, "
  704. "aborting.\n");
  705. err = -ENOMEM;
  706. goto err_uar_free;
  707. }
  708. err = mlx4_init_pd_table(dev);
  709. if (err) {
  710. mlx4_err(dev, "Failed to initialize "
  711. "protection domain table, aborting.\n");
  712. goto err_kar_unmap;
  713. }
  714. err = mlx4_init_mr_table(dev);
  715. if (err) {
  716. mlx4_err(dev, "Failed to initialize "
  717. "memory region table, aborting.\n");
  718. goto err_pd_table_free;
  719. }
  720. err = mlx4_init_eq_table(dev);
  721. if (err) {
  722. mlx4_err(dev, "Failed to initialize "
  723. "event queue table, aborting.\n");
  724. goto err_mr_table_free;
  725. }
  726. err = mlx4_cmd_use_events(dev);
  727. if (err) {
  728. mlx4_err(dev, "Failed to switch to event-driven "
  729. "firmware commands, aborting.\n");
  730. goto err_eq_table_free;
  731. }
  732. err = mlx4_NOP(dev);
  733. if (err) {
  734. if (dev->flags & MLX4_FLAG_MSI_X) {
  735. mlx4_warn(dev, "NOP command failed to generate MSI-X "
  736. "interrupt IRQ %d).\n",
  737. priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
  738. mlx4_warn(dev, "Trying again without MSI-X.\n");
  739. } else {
  740. mlx4_err(dev, "NOP command failed to generate interrupt "
  741. "(IRQ %d), aborting.\n",
  742. priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
  743. mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
  744. }
  745. goto err_cmd_poll;
  746. }
  747. mlx4_dbg(dev, "NOP command IRQ test passed\n");
  748. err = mlx4_init_cq_table(dev);
  749. if (err) {
  750. mlx4_err(dev, "Failed to initialize "
  751. "completion queue table, aborting.\n");
  752. goto err_cmd_poll;
  753. }
  754. err = mlx4_init_srq_table(dev);
  755. if (err) {
  756. mlx4_err(dev, "Failed to initialize "
  757. "shared receive queue table, aborting.\n");
  758. goto err_cq_table_free;
  759. }
  760. err = mlx4_init_qp_table(dev);
  761. if (err) {
  762. mlx4_err(dev, "Failed to initialize "
  763. "queue pair table, aborting.\n");
  764. goto err_srq_table_free;
  765. }
  766. err = mlx4_init_mcg_table(dev);
  767. if (err) {
  768. mlx4_err(dev, "Failed to initialize "
  769. "multicast group table, aborting.\n");
  770. goto err_qp_table_free;
  771. }
  772. for (port = 1; port <= dev->caps.num_ports; port++) {
  773. ib_port_default_caps = 0;
  774. err = mlx4_get_port_ib_caps(dev, port, &ib_port_default_caps);
  775. if (err)
  776. mlx4_warn(dev, "failed to get port %d default "
  777. "ib capabilities (%d). Continuing with "
  778. "caps = 0\n", port, err);
  779. dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
  780. err = mlx4_SET_PORT(dev, port);
  781. if (err) {
  782. mlx4_err(dev, "Failed to set port %d, aborting\n",
  783. port);
  784. goto err_mcg_table_free;
  785. }
  786. }
  787. return 0;
  788. err_mcg_table_free:
  789. mlx4_cleanup_mcg_table(dev);
  790. err_qp_table_free:
  791. mlx4_cleanup_qp_table(dev);
  792. err_srq_table_free:
  793. mlx4_cleanup_srq_table(dev);
  794. err_cq_table_free:
  795. mlx4_cleanup_cq_table(dev);
  796. err_cmd_poll:
  797. mlx4_cmd_use_polling(dev);
  798. err_eq_table_free:
  799. mlx4_cleanup_eq_table(dev);
  800. err_mr_table_free:
  801. mlx4_cleanup_mr_table(dev);
  802. err_pd_table_free:
  803. mlx4_cleanup_pd_table(dev);
  804. err_kar_unmap:
  805. iounmap(priv->kar);
  806. err_uar_free:
  807. mlx4_uar_free(dev, &priv->driver_uar);
  808. err_uar_table_free:
  809. mlx4_cleanup_uar_table(dev);
  810. return err;
  811. }
  812. static void mlx4_enable_msi_x(struct mlx4_dev *dev)
  813. {
  814. struct mlx4_priv *priv = mlx4_priv(dev);
  815. struct msix_entry *entries;
  816. int nreq;
  817. int err;
  818. int i;
  819. if (msi_x) {
  820. nreq = min_t(int, dev->caps.num_eqs - dev->caps.reserved_eqs,
  821. num_possible_cpus() + 1);
  822. entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
  823. if (!entries)
  824. goto no_msi;
  825. for (i = 0; i < nreq; ++i)
  826. entries[i].entry = i;
  827. retry:
  828. err = pci_enable_msix(dev->pdev, entries, nreq);
  829. if (err) {
  830. /* Try again if at least 2 vectors are available */
  831. if (err > 1) {
  832. mlx4_info(dev, "Requested %d vectors, "
  833. "but only %d MSI-X vectors available, "
  834. "trying again\n", nreq, err);
  835. nreq = err;
  836. goto retry;
  837. }
  838. kfree(entries);
  839. goto no_msi;
  840. }
  841. dev->caps.num_comp_vectors = nreq - 1;
  842. for (i = 0; i < nreq; ++i)
  843. priv->eq_table.eq[i].irq = entries[i].vector;
  844. dev->flags |= MLX4_FLAG_MSI_X;
  845. kfree(entries);
  846. return;
  847. }
  848. no_msi:
  849. dev->caps.num_comp_vectors = 1;
  850. for (i = 0; i < 2; ++i)
  851. priv->eq_table.eq[i].irq = dev->pdev->irq;
  852. }
  853. static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
  854. {
  855. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  856. int err = 0;
  857. info->dev = dev;
  858. info->port = port;
  859. mlx4_init_mac_table(dev, &info->mac_table);
  860. mlx4_init_vlan_table(dev, &info->vlan_table);
  861. sprintf(info->dev_name, "mlx4_port%d", port);
  862. info->port_attr.attr.name = info->dev_name;
  863. info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
  864. info->port_attr.show = show_port_type;
  865. info->port_attr.store = set_port_type;
  866. sysfs_attr_init(&info->port_attr.attr);
  867. err = device_create_file(&dev->pdev->dev, &info->port_attr);
  868. if (err) {
  869. mlx4_err(dev, "Failed to create file for port %d\n", port);
  870. info->port = -1;
  871. }
  872. return err;
  873. }
  874. static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
  875. {
  876. if (info->port < 0)
  877. return;
  878. device_remove_file(&info->dev->pdev->dev, &info->port_attr);
  879. }
  880. static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  881. {
  882. struct mlx4_priv *priv;
  883. struct mlx4_dev *dev;
  884. int err;
  885. int port;
  886. printk(KERN_INFO PFX "Initializing %s\n",
  887. pci_name(pdev));
  888. err = pci_enable_device(pdev);
  889. if (err) {
  890. dev_err(&pdev->dev, "Cannot enable PCI device, "
  891. "aborting.\n");
  892. return err;
  893. }
  894. /*
  895. * Check for BARs. We expect 0: 1MB
  896. */
  897. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  898. pci_resource_len(pdev, 0) != 1 << 20) {
  899. dev_err(&pdev->dev, "Missing DCS, aborting.\n");
  900. err = -ENODEV;
  901. goto err_disable_pdev;
  902. }
  903. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  904. dev_err(&pdev->dev, "Missing UAR, aborting.\n");
  905. err = -ENODEV;
  906. goto err_disable_pdev;
  907. }
  908. err = pci_request_regions(pdev, DRV_NAME);
  909. if (err) {
  910. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  911. goto err_disable_pdev;
  912. }
  913. pci_set_master(pdev);
  914. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  915. if (err) {
  916. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
  917. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  918. if (err) {
  919. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
  920. goto err_release_regions;
  921. }
  922. }
  923. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  924. if (err) {
  925. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
  926. "consistent PCI DMA mask.\n");
  927. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  928. if (err) {
  929. dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
  930. "aborting.\n");
  931. goto err_release_regions;
  932. }
  933. }
  934. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  935. if (!priv) {
  936. dev_err(&pdev->dev, "Device struct alloc failed, "
  937. "aborting.\n");
  938. err = -ENOMEM;
  939. goto err_release_regions;
  940. }
  941. dev = &priv->dev;
  942. dev->pdev = pdev;
  943. INIT_LIST_HEAD(&priv->ctx_list);
  944. spin_lock_init(&priv->ctx_lock);
  945. mutex_init(&priv->port_mutex);
  946. INIT_LIST_HEAD(&priv->pgdir_list);
  947. mutex_init(&priv->pgdir_mutex);
  948. /*
  949. * Now reset the HCA before we touch the PCI capabilities or
  950. * attempt a firmware command, since a boot ROM may have left
  951. * the HCA in an undefined state.
  952. */
  953. err = mlx4_reset(dev);
  954. if (err) {
  955. mlx4_err(dev, "Failed to reset HCA, aborting.\n");
  956. goto err_free_dev;
  957. }
  958. if (mlx4_cmd_init(dev)) {
  959. mlx4_err(dev, "Failed to init command interface, aborting.\n");
  960. goto err_free_dev;
  961. }
  962. err = mlx4_init_hca(dev);
  963. if (err)
  964. goto err_cmd;
  965. err = mlx4_alloc_eq_table(dev);
  966. if (err)
  967. goto err_close;
  968. mlx4_enable_msi_x(dev);
  969. err = mlx4_setup_hca(dev);
  970. if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X)) {
  971. dev->flags &= ~MLX4_FLAG_MSI_X;
  972. pci_disable_msix(pdev);
  973. err = mlx4_setup_hca(dev);
  974. }
  975. if (err)
  976. goto err_free_eq;
  977. for (port = 1; port <= dev->caps.num_ports; port++) {
  978. err = mlx4_init_port_info(dev, port);
  979. if (err)
  980. goto err_port;
  981. }
  982. err = mlx4_register_device(dev);
  983. if (err)
  984. goto err_port;
  985. mlx4_sense_init(dev);
  986. mlx4_start_sense(dev);
  987. pci_set_drvdata(pdev, dev);
  988. return 0;
  989. err_port:
  990. for (--port; port >= 1; --port)
  991. mlx4_cleanup_port_info(&priv->port[port]);
  992. mlx4_cleanup_mcg_table(dev);
  993. mlx4_cleanup_qp_table(dev);
  994. mlx4_cleanup_srq_table(dev);
  995. mlx4_cleanup_cq_table(dev);
  996. mlx4_cmd_use_polling(dev);
  997. mlx4_cleanup_eq_table(dev);
  998. mlx4_cleanup_mr_table(dev);
  999. mlx4_cleanup_pd_table(dev);
  1000. mlx4_cleanup_uar_table(dev);
  1001. err_free_eq:
  1002. mlx4_free_eq_table(dev);
  1003. err_close:
  1004. if (dev->flags & MLX4_FLAG_MSI_X)
  1005. pci_disable_msix(pdev);
  1006. mlx4_close_hca(dev);
  1007. err_cmd:
  1008. mlx4_cmd_cleanup(dev);
  1009. err_free_dev:
  1010. kfree(priv);
  1011. err_release_regions:
  1012. pci_release_regions(pdev);
  1013. err_disable_pdev:
  1014. pci_disable_device(pdev);
  1015. pci_set_drvdata(pdev, NULL);
  1016. return err;
  1017. }
  1018. static int __devinit mlx4_init_one(struct pci_dev *pdev,
  1019. const struct pci_device_id *id)
  1020. {
  1021. static int mlx4_version_printed;
  1022. if (!mlx4_version_printed) {
  1023. printk(KERN_INFO "%s", mlx4_version);
  1024. ++mlx4_version_printed;
  1025. }
  1026. return __mlx4_init_one(pdev, id);
  1027. }
  1028. static void mlx4_remove_one(struct pci_dev *pdev)
  1029. {
  1030. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  1031. struct mlx4_priv *priv = mlx4_priv(dev);
  1032. int p;
  1033. if (dev) {
  1034. mlx4_stop_sense(dev);
  1035. mlx4_unregister_device(dev);
  1036. for (p = 1; p <= dev->caps.num_ports; p++) {
  1037. mlx4_cleanup_port_info(&priv->port[p]);
  1038. mlx4_CLOSE_PORT(dev, p);
  1039. }
  1040. mlx4_cleanup_mcg_table(dev);
  1041. mlx4_cleanup_qp_table(dev);
  1042. mlx4_cleanup_srq_table(dev);
  1043. mlx4_cleanup_cq_table(dev);
  1044. mlx4_cmd_use_polling(dev);
  1045. mlx4_cleanup_eq_table(dev);
  1046. mlx4_cleanup_mr_table(dev);
  1047. mlx4_cleanup_pd_table(dev);
  1048. iounmap(priv->kar);
  1049. mlx4_uar_free(dev, &priv->driver_uar);
  1050. mlx4_cleanup_uar_table(dev);
  1051. mlx4_free_eq_table(dev);
  1052. mlx4_close_hca(dev);
  1053. mlx4_cmd_cleanup(dev);
  1054. if (dev->flags & MLX4_FLAG_MSI_X)
  1055. pci_disable_msix(pdev);
  1056. kfree(priv);
  1057. pci_release_regions(pdev);
  1058. pci_disable_device(pdev);
  1059. pci_set_drvdata(pdev, NULL);
  1060. }
  1061. }
  1062. int mlx4_restart_one(struct pci_dev *pdev)
  1063. {
  1064. mlx4_remove_one(pdev);
  1065. return __mlx4_init_one(pdev, NULL);
  1066. }
  1067. static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
  1068. { PCI_VDEVICE(MELLANOX, 0x6340) }, /* MT25408 "Hermon" SDR */
  1069. { PCI_VDEVICE(MELLANOX, 0x634a) }, /* MT25408 "Hermon" DDR */
  1070. { PCI_VDEVICE(MELLANOX, 0x6354) }, /* MT25408 "Hermon" QDR */
  1071. { PCI_VDEVICE(MELLANOX, 0x6732) }, /* MT25408 "Hermon" DDR PCIe gen2 */
  1072. { PCI_VDEVICE(MELLANOX, 0x673c) }, /* MT25408 "Hermon" QDR PCIe gen2 */
  1073. { PCI_VDEVICE(MELLANOX, 0x6368) }, /* MT25408 "Hermon" EN 10GigE */
  1074. { PCI_VDEVICE(MELLANOX, 0x6750) }, /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
  1075. { PCI_VDEVICE(MELLANOX, 0x6372) }, /* MT25458 ConnectX EN 10GBASE-T 10GigE */
  1076. { PCI_VDEVICE(MELLANOX, 0x675a) }, /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
  1077. { PCI_VDEVICE(MELLANOX, 0x6764) }, /* MT26468 ConnectX EN 10GigE PCIe gen2*/
  1078. { PCI_VDEVICE(MELLANOX, 0x6746) }, /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
  1079. { PCI_VDEVICE(MELLANOX, 0x676e) }, /* MT26478 ConnectX2 40GigE PCIe gen2 */
  1080. { 0, }
  1081. };
  1082. MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
  1083. static struct pci_driver mlx4_driver = {
  1084. .name = DRV_NAME,
  1085. .id_table = mlx4_pci_table,
  1086. .probe = mlx4_init_one,
  1087. .remove = __devexit_p(mlx4_remove_one)
  1088. };
  1089. static int __init mlx4_verify_params(void)
  1090. {
  1091. if ((log_num_mac < 0) || (log_num_mac > 7)) {
  1092. printk(KERN_WARNING "mlx4_core: bad num_mac: %d\n", log_num_mac);
  1093. return -1;
  1094. }
  1095. if ((log_num_vlan < 0) || (log_num_vlan > 7)) {
  1096. printk(KERN_WARNING "mlx4_core: bad num_vlan: %d\n", log_num_vlan);
  1097. return -1;
  1098. }
  1099. if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 5)) {
  1100. printk(KERN_WARNING "mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
  1101. return -1;
  1102. }
  1103. return 0;
  1104. }
  1105. static int __init mlx4_init(void)
  1106. {
  1107. int ret;
  1108. if (mlx4_verify_params())
  1109. return -EINVAL;
  1110. mlx4_catas_init();
  1111. mlx4_wq = create_singlethread_workqueue("mlx4");
  1112. if (!mlx4_wq)
  1113. return -ENOMEM;
  1114. ret = pci_register_driver(&mlx4_driver);
  1115. return ret < 0 ? ret : 0;
  1116. }
  1117. static void __exit mlx4_cleanup(void)
  1118. {
  1119. pci_unregister_driver(&mlx4_driver);
  1120. destroy_workqueue(mlx4_wq);
  1121. }
  1122. module_init(mlx4_init);
  1123. module_exit(mlx4_cleanup);