main.c 35 KB

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