main.c 41 KB

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