main.c 41 KB

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