main.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. info->base_qpn = dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
  943. (port - 1) * (1 << log_num_mac);
  944. sprintf(info->dev_name, "mlx4_port%d", port);
  945. info->port_attr.attr.name = info->dev_name;
  946. info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
  947. info->port_attr.show = show_port_type;
  948. info->port_attr.store = set_port_type;
  949. sysfs_attr_init(&info->port_attr.attr);
  950. err = device_create_file(&dev->pdev->dev, &info->port_attr);
  951. if (err) {
  952. mlx4_err(dev, "Failed to create file for port %d\n", port);
  953. info->port = -1;
  954. }
  955. return err;
  956. }
  957. static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
  958. {
  959. if (info->port < 0)
  960. return;
  961. device_remove_file(&info->dev->pdev->dev, &info->port_attr);
  962. }
  963. static int mlx4_init_steering(struct mlx4_dev *dev)
  964. {
  965. struct mlx4_priv *priv = mlx4_priv(dev);
  966. int num_entries = dev->caps.num_ports;
  967. int i, j;
  968. priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
  969. if (!priv->steer)
  970. return -ENOMEM;
  971. for (i = 0; i < num_entries; i++) {
  972. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  973. INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
  974. INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
  975. }
  976. INIT_LIST_HEAD(&priv->steer[i].high_prios);
  977. }
  978. return 0;
  979. }
  980. static void mlx4_clear_steering(struct mlx4_dev *dev)
  981. {
  982. struct mlx4_priv *priv = mlx4_priv(dev);
  983. struct mlx4_steer_index *entry, *tmp_entry;
  984. struct mlx4_promisc_qp *pqp, *tmp_pqp;
  985. int num_entries = dev->caps.num_ports;
  986. int i, j;
  987. for (i = 0; i < num_entries; i++) {
  988. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  989. list_for_each_entry_safe(pqp, tmp_pqp,
  990. &priv->steer[i].promisc_qps[j],
  991. list) {
  992. list_del(&pqp->list);
  993. kfree(pqp);
  994. }
  995. list_for_each_entry_safe(entry, tmp_entry,
  996. &priv->steer[i].steer_entries[j],
  997. list) {
  998. list_del(&entry->list);
  999. list_for_each_entry_safe(pqp, tmp_pqp,
  1000. &entry->duplicates,
  1001. list) {
  1002. list_del(&pqp->list);
  1003. kfree(pqp);
  1004. }
  1005. kfree(entry);
  1006. }
  1007. }
  1008. }
  1009. kfree(priv->steer);
  1010. }
  1011. static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1012. {
  1013. struct mlx4_priv *priv;
  1014. struct mlx4_dev *dev;
  1015. int err;
  1016. int port;
  1017. pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
  1018. err = pci_enable_device(pdev);
  1019. if (err) {
  1020. dev_err(&pdev->dev, "Cannot enable PCI device, "
  1021. "aborting.\n");
  1022. return err;
  1023. }
  1024. /*
  1025. * Check for BARs. We expect 0: 1MB
  1026. */
  1027. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  1028. pci_resource_len(pdev, 0) != 1 << 20) {
  1029. dev_err(&pdev->dev, "Missing DCS, aborting.\n");
  1030. err = -ENODEV;
  1031. goto err_disable_pdev;
  1032. }
  1033. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  1034. dev_err(&pdev->dev, "Missing UAR, aborting.\n");
  1035. err = -ENODEV;
  1036. goto err_disable_pdev;
  1037. }
  1038. err = pci_request_regions(pdev, DRV_NAME);
  1039. if (err) {
  1040. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  1041. goto err_disable_pdev;
  1042. }
  1043. pci_set_master(pdev);
  1044. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1045. if (err) {
  1046. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
  1047. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1048. if (err) {
  1049. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
  1050. goto err_release_regions;
  1051. }
  1052. }
  1053. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1054. if (err) {
  1055. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
  1056. "consistent PCI DMA mask.\n");
  1057. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1058. if (err) {
  1059. dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
  1060. "aborting.\n");
  1061. goto err_release_regions;
  1062. }
  1063. }
  1064. /* Allow large DMA segments, up to the firmware limit of 1 GB */
  1065. dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
  1066. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  1067. if (!priv) {
  1068. dev_err(&pdev->dev, "Device struct alloc failed, "
  1069. "aborting.\n");
  1070. err = -ENOMEM;
  1071. goto err_release_regions;
  1072. }
  1073. dev = &priv->dev;
  1074. dev->pdev = pdev;
  1075. INIT_LIST_HEAD(&priv->ctx_list);
  1076. spin_lock_init(&priv->ctx_lock);
  1077. mutex_init(&priv->port_mutex);
  1078. INIT_LIST_HEAD(&priv->pgdir_list);
  1079. mutex_init(&priv->pgdir_mutex);
  1080. INIT_LIST_HEAD(&priv->bf_list);
  1081. mutex_init(&priv->bf_mutex);
  1082. dev->rev_id = pdev->revision;
  1083. /*
  1084. * Now reset the HCA before we touch the PCI capabilities or
  1085. * attempt a firmware command, since a boot ROM may have left
  1086. * the HCA in an undefined state.
  1087. */
  1088. err = mlx4_reset(dev);
  1089. if (err) {
  1090. mlx4_err(dev, "Failed to reset HCA, aborting.\n");
  1091. goto err_free_dev;
  1092. }
  1093. if (mlx4_cmd_init(dev)) {
  1094. mlx4_err(dev, "Failed to init command interface, aborting.\n");
  1095. goto err_free_dev;
  1096. }
  1097. err = mlx4_init_hca(dev);
  1098. if (err)
  1099. goto err_cmd;
  1100. err = mlx4_alloc_eq_table(dev);
  1101. if (err)
  1102. goto err_close;
  1103. priv->msix_ctl.pool_bm = 0;
  1104. spin_lock_init(&priv->msix_ctl.pool_lock);
  1105. mlx4_enable_msi_x(dev);
  1106. err = mlx4_init_steering(dev);
  1107. if (err)
  1108. goto err_free_eq;
  1109. err = mlx4_setup_hca(dev);
  1110. if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X)) {
  1111. dev->flags &= ~MLX4_FLAG_MSI_X;
  1112. pci_disable_msix(pdev);
  1113. err = mlx4_setup_hca(dev);
  1114. }
  1115. if (err)
  1116. goto err_steer;
  1117. for (port = 1; port <= dev->caps.num_ports; port++) {
  1118. err = mlx4_init_port_info(dev, port);
  1119. if (err)
  1120. goto err_port;
  1121. }
  1122. err = mlx4_register_device(dev);
  1123. if (err)
  1124. goto err_port;
  1125. mlx4_sense_init(dev);
  1126. mlx4_start_sense(dev);
  1127. pci_set_drvdata(pdev, dev);
  1128. return 0;
  1129. err_port:
  1130. for (--port; port >= 1; --port)
  1131. mlx4_cleanup_port_info(&priv->port[port]);
  1132. mlx4_cleanup_counters_table(dev);
  1133. mlx4_cleanup_mcg_table(dev);
  1134. mlx4_cleanup_qp_table(dev);
  1135. mlx4_cleanup_srq_table(dev);
  1136. mlx4_cleanup_cq_table(dev);
  1137. mlx4_cmd_use_polling(dev);
  1138. mlx4_cleanup_eq_table(dev);
  1139. mlx4_cleanup_mr_table(dev);
  1140. mlx4_cleanup_pd_table(dev);
  1141. mlx4_cleanup_uar_table(dev);
  1142. err_steer:
  1143. mlx4_clear_steering(dev);
  1144. err_free_eq:
  1145. mlx4_free_eq_table(dev);
  1146. err_close:
  1147. if (dev->flags & MLX4_FLAG_MSI_X)
  1148. pci_disable_msix(pdev);
  1149. mlx4_close_hca(dev);
  1150. err_cmd:
  1151. mlx4_cmd_cleanup(dev);
  1152. err_free_dev:
  1153. kfree(priv);
  1154. err_release_regions:
  1155. pci_release_regions(pdev);
  1156. err_disable_pdev:
  1157. pci_disable_device(pdev);
  1158. pci_set_drvdata(pdev, NULL);
  1159. return err;
  1160. }
  1161. static int __devinit mlx4_init_one(struct pci_dev *pdev,
  1162. const struct pci_device_id *id)
  1163. {
  1164. printk_once(KERN_INFO "%s", mlx4_version);
  1165. return __mlx4_init_one(pdev, id);
  1166. }
  1167. static void mlx4_remove_one(struct pci_dev *pdev)
  1168. {
  1169. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  1170. struct mlx4_priv *priv = mlx4_priv(dev);
  1171. int p;
  1172. if (dev) {
  1173. mlx4_stop_sense(dev);
  1174. mlx4_unregister_device(dev);
  1175. for (p = 1; p <= dev->caps.num_ports; p++) {
  1176. mlx4_cleanup_port_info(&priv->port[p]);
  1177. mlx4_CLOSE_PORT(dev, p);
  1178. }
  1179. mlx4_cleanup_counters_table(dev);
  1180. mlx4_cleanup_mcg_table(dev);
  1181. mlx4_cleanup_qp_table(dev);
  1182. mlx4_cleanup_srq_table(dev);
  1183. mlx4_cleanup_cq_table(dev);
  1184. mlx4_cmd_use_polling(dev);
  1185. mlx4_cleanup_eq_table(dev);
  1186. mlx4_cleanup_mr_table(dev);
  1187. mlx4_cleanup_pd_table(dev);
  1188. iounmap(priv->kar);
  1189. mlx4_uar_free(dev, &priv->driver_uar);
  1190. mlx4_cleanup_uar_table(dev);
  1191. mlx4_clear_steering(dev);
  1192. mlx4_free_eq_table(dev);
  1193. mlx4_close_hca(dev);
  1194. mlx4_cmd_cleanup(dev);
  1195. if (dev->flags & MLX4_FLAG_MSI_X)
  1196. pci_disable_msix(pdev);
  1197. kfree(priv);
  1198. pci_release_regions(pdev);
  1199. pci_disable_device(pdev);
  1200. pci_set_drvdata(pdev, NULL);
  1201. }
  1202. }
  1203. int mlx4_restart_one(struct pci_dev *pdev)
  1204. {
  1205. mlx4_remove_one(pdev);
  1206. return __mlx4_init_one(pdev, NULL);
  1207. }
  1208. static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
  1209. { PCI_VDEVICE(MELLANOX, 0x6340) }, /* MT25408 "Hermon" SDR */
  1210. { PCI_VDEVICE(MELLANOX, 0x634a) }, /* MT25408 "Hermon" DDR */
  1211. { PCI_VDEVICE(MELLANOX, 0x6354) }, /* MT25408 "Hermon" QDR */
  1212. { PCI_VDEVICE(MELLANOX, 0x6732) }, /* MT25408 "Hermon" DDR PCIe gen2 */
  1213. { PCI_VDEVICE(MELLANOX, 0x673c) }, /* MT25408 "Hermon" QDR PCIe gen2 */
  1214. { PCI_VDEVICE(MELLANOX, 0x6368) }, /* MT25408 "Hermon" EN 10GigE */
  1215. { PCI_VDEVICE(MELLANOX, 0x6750) }, /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
  1216. { PCI_VDEVICE(MELLANOX, 0x6372) }, /* MT25458 ConnectX EN 10GBASE-T 10GigE */
  1217. { PCI_VDEVICE(MELLANOX, 0x675a) }, /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
  1218. { PCI_VDEVICE(MELLANOX, 0x6764) }, /* MT26468 ConnectX EN 10GigE PCIe gen2*/
  1219. { PCI_VDEVICE(MELLANOX, 0x6746) }, /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
  1220. { PCI_VDEVICE(MELLANOX, 0x676e) }, /* MT26478 ConnectX2 40GigE PCIe gen2 */
  1221. { PCI_VDEVICE(MELLANOX, 0x1002) }, /* MT25400 Family [ConnectX-2 Virtual Function] */
  1222. { PCI_VDEVICE(MELLANOX, 0x1003) }, /* MT27500 Family [ConnectX-3] */
  1223. { PCI_VDEVICE(MELLANOX, 0x1004) }, /* MT27500 Family [ConnectX-3 Virtual Function] */
  1224. { PCI_VDEVICE(MELLANOX, 0x1005) }, /* MT27510 Family */
  1225. { PCI_VDEVICE(MELLANOX, 0x1006) }, /* MT27511 Family */
  1226. { PCI_VDEVICE(MELLANOX, 0x1007) }, /* MT27520 Family */
  1227. { PCI_VDEVICE(MELLANOX, 0x1008) }, /* MT27521 Family */
  1228. { PCI_VDEVICE(MELLANOX, 0x1009) }, /* MT27530 Family */
  1229. { PCI_VDEVICE(MELLANOX, 0x100a) }, /* MT27531 Family */
  1230. { PCI_VDEVICE(MELLANOX, 0x100b) }, /* MT27540 Family */
  1231. { PCI_VDEVICE(MELLANOX, 0x100c) }, /* MT27541 Family */
  1232. { PCI_VDEVICE(MELLANOX, 0x100d) }, /* MT27550 Family */
  1233. { PCI_VDEVICE(MELLANOX, 0x100e) }, /* MT27551 Family */
  1234. { PCI_VDEVICE(MELLANOX, 0x100f) }, /* MT27560 Family */
  1235. { PCI_VDEVICE(MELLANOX, 0x1010) }, /* MT27561 Family */
  1236. { 0, }
  1237. };
  1238. MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
  1239. static struct pci_driver mlx4_driver = {
  1240. .name = DRV_NAME,
  1241. .id_table = mlx4_pci_table,
  1242. .probe = mlx4_init_one,
  1243. .remove = __devexit_p(mlx4_remove_one)
  1244. };
  1245. static int __init mlx4_verify_params(void)
  1246. {
  1247. if ((log_num_mac < 0) || (log_num_mac > 7)) {
  1248. pr_warning("mlx4_core: bad num_mac: %d\n", log_num_mac);
  1249. return -1;
  1250. }
  1251. if ((log_num_vlan < 0) || (log_num_vlan > 7)) {
  1252. pr_warning("mlx4_core: bad num_vlan: %d\n", log_num_vlan);
  1253. return -1;
  1254. }
  1255. if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
  1256. pr_warning("mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
  1257. return -1;
  1258. }
  1259. return 0;
  1260. }
  1261. static int __init mlx4_init(void)
  1262. {
  1263. int ret;
  1264. if (mlx4_verify_params())
  1265. return -EINVAL;
  1266. mlx4_catas_init();
  1267. mlx4_wq = create_singlethread_workqueue("mlx4");
  1268. if (!mlx4_wq)
  1269. return -ENOMEM;
  1270. ret = pci_register_driver(&mlx4_driver);
  1271. return ret < 0 ? ret : 0;
  1272. }
  1273. static void __exit mlx4_cleanup(void)
  1274. {
  1275. pci_unregister_driver(&mlx4_driver);
  1276. destroy_workqueue(mlx4_wq);
  1277. }
  1278. module_init(mlx4_init);
  1279. module_exit(mlx4_cleanup);