main.c 42 KB

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