main.c 40 KB

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