main.c 41 KB

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