main.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  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/delay.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/mlx4/device.h>
  45. #include <linux/mlx4/doorbell.h>
  46. #include "mlx4.h"
  47. #include "fw.h"
  48. #include "icm.h"
  49. MODULE_AUTHOR("Roland Dreier");
  50. MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. MODULE_VERSION(DRV_VERSION);
  53. struct workqueue_struct *mlx4_wq;
  54. #ifdef CONFIG_MLX4_DEBUG
  55. int mlx4_debug_level = 0;
  56. module_param_named(debug_level, mlx4_debug_level, int, 0644);
  57. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  58. #endif /* CONFIG_MLX4_DEBUG */
  59. #ifdef CONFIG_PCI_MSI
  60. static int msi_x = 1;
  61. module_param(msi_x, int, 0444);
  62. MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
  63. #else /* CONFIG_PCI_MSI */
  64. #define msi_x (0)
  65. #endif /* CONFIG_PCI_MSI */
  66. static int num_vfs;
  67. module_param(num_vfs, int, 0444);
  68. MODULE_PARM_DESC(num_vfs, "enable #num_vfs functions if num_vfs > 0");
  69. static int probe_vf;
  70. module_param(probe_vf, int, 0644);
  71. MODULE_PARM_DESC(probe_vf, "number of vfs to probe by pf driver (num_vfs > 0)");
  72. int mlx4_log_num_mgm_entry_size = 10;
  73. module_param_named(log_num_mgm_entry_size,
  74. mlx4_log_num_mgm_entry_size, int, 0444);
  75. MODULE_PARM_DESC(log_num_mgm_entry_size, "log mgm size, that defines the num"
  76. " of qp per mcg, for example:"
  77. " 10 gives 248.range: 9<="
  78. " log_num_mgm_entry_size <= 12."
  79. " Not in use with device managed"
  80. " flow steering");
  81. #define HCA_GLOBAL_CAP_MASK 0
  82. #define PF_CONTEXT_BEHAVIOUR_MASK 0
  83. static char mlx4_version[] =
  84. DRV_NAME ": Mellanox ConnectX core driver v"
  85. DRV_VERSION " (" DRV_RELDATE ")\n";
  86. static struct mlx4_profile default_profile = {
  87. .num_qp = 1 << 18,
  88. .num_srq = 1 << 16,
  89. .rdmarc_per_qp = 1 << 4,
  90. .num_cq = 1 << 16,
  91. .num_mcg = 1 << 13,
  92. .num_mpt = 1 << 19,
  93. .num_mtt = 1 << 20, /* It is really num mtt segements */
  94. };
  95. static int log_num_mac = 7;
  96. module_param_named(log_num_mac, log_num_mac, int, 0444);
  97. MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
  98. static int log_num_vlan;
  99. module_param_named(log_num_vlan, log_num_vlan, int, 0444);
  100. MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
  101. /* Log2 max number of VLANs per ETH port (0-7) */
  102. #define MLX4_LOG_NUM_VLANS 7
  103. static bool use_prio;
  104. module_param_named(use_prio, use_prio, bool, 0444);
  105. MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports "
  106. "(0/1, default 0)");
  107. int log_mtts_per_seg = ilog2(MLX4_MTT_ENTRY_PER_SEG);
  108. module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
  109. MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment (1-7)");
  110. static int port_type_array[2] = {MLX4_PORT_TYPE_NONE, MLX4_PORT_TYPE_NONE};
  111. static int arr_argc = 2;
  112. module_param_array(port_type_array, int, &arr_argc, 0444);
  113. MODULE_PARM_DESC(port_type_array, "Array of port types: HW_DEFAULT (0) is default "
  114. "1 for IB, 2 for Ethernet");
  115. struct mlx4_port_config {
  116. struct list_head list;
  117. enum mlx4_port_type port_type[MLX4_MAX_PORTS + 1];
  118. struct pci_dev *pdev;
  119. };
  120. int mlx4_check_port_params(struct mlx4_dev *dev,
  121. enum mlx4_port_type *port_type)
  122. {
  123. int i;
  124. for (i = 0; i < dev->caps.num_ports - 1; i++) {
  125. if (port_type[i] != port_type[i + 1]) {
  126. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
  127. mlx4_err(dev, "Only same port types supported "
  128. "on this HCA, aborting.\n");
  129. return -EINVAL;
  130. }
  131. }
  132. }
  133. for (i = 0; i < dev->caps.num_ports; i++) {
  134. if (!(port_type[i] & dev->caps.supported_type[i+1])) {
  135. mlx4_err(dev, "Requested port type for port %d is not "
  136. "supported on this HCA\n", i + 1);
  137. return -EINVAL;
  138. }
  139. }
  140. return 0;
  141. }
  142. static void mlx4_set_port_mask(struct mlx4_dev *dev)
  143. {
  144. int i;
  145. for (i = 1; i <= dev->caps.num_ports; ++i)
  146. dev->caps.port_mask[i] = dev->caps.port_type[i];
  147. }
  148. static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
  149. {
  150. int err;
  151. int i;
  152. err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
  153. if (err) {
  154. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  155. return err;
  156. }
  157. if (dev_cap->min_page_sz > PAGE_SIZE) {
  158. mlx4_err(dev, "HCA minimum page size of %d bigger than "
  159. "kernel PAGE_SIZE of %ld, aborting.\n",
  160. dev_cap->min_page_sz, PAGE_SIZE);
  161. return -ENODEV;
  162. }
  163. if (dev_cap->num_ports > MLX4_MAX_PORTS) {
  164. mlx4_err(dev, "HCA has %d ports, but we only support %d, "
  165. "aborting.\n",
  166. dev_cap->num_ports, MLX4_MAX_PORTS);
  167. return -ENODEV;
  168. }
  169. if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
  170. mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
  171. "PCI resource 2 size of 0x%llx, aborting.\n",
  172. dev_cap->uar_size,
  173. (unsigned long long) pci_resource_len(dev->pdev, 2));
  174. return -ENODEV;
  175. }
  176. dev->caps.num_ports = dev_cap->num_ports;
  177. dev->phys_caps.num_phys_eqs = MLX4_MAX_EQ_NUM;
  178. for (i = 1; i <= dev->caps.num_ports; ++i) {
  179. dev->caps.vl_cap[i] = dev_cap->max_vl[i];
  180. dev->caps.ib_mtu_cap[i] = dev_cap->ib_mtu[i];
  181. dev->phys_caps.gid_phys_table_len[i] = dev_cap->max_gids[i];
  182. dev->phys_caps.pkey_phys_table_len[i] = dev_cap->max_pkeys[i];
  183. /* set gid and pkey table operating lengths by default
  184. * to non-sriov values */
  185. dev->caps.gid_table_len[i] = dev_cap->max_gids[i];
  186. dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
  187. dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
  188. dev->caps.eth_mtu_cap[i] = dev_cap->eth_mtu[i];
  189. dev->caps.def_mac[i] = dev_cap->def_mac[i];
  190. dev->caps.supported_type[i] = dev_cap->supported_port_types[i];
  191. dev->caps.suggested_type[i] = dev_cap->suggested_type[i];
  192. dev->caps.default_sense[i] = dev_cap->default_sense[i];
  193. dev->caps.trans_type[i] = dev_cap->trans_type[i];
  194. dev->caps.vendor_oui[i] = dev_cap->vendor_oui[i];
  195. dev->caps.wavelength[i] = dev_cap->wavelength[i];
  196. dev->caps.trans_code[i] = dev_cap->trans_code[i];
  197. }
  198. dev->caps.uar_page_size = PAGE_SIZE;
  199. dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
  200. dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
  201. dev->caps.bf_reg_size = dev_cap->bf_reg_size;
  202. dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
  203. dev->caps.max_sq_sg = dev_cap->max_sq_sg;
  204. dev->caps.max_rq_sg = dev_cap->max_rq_sg;
  205. dev->caps.max_wqes = dev_cap->max_qp_sz;
  206. dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
  207. dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
  208. dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
  209. dev->caps.reserved_srqs = dev_cap->reserved_srqs;
  210. dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
  211. dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
  212. /*
  213. * Subtract 1 from the limit because we need to allocate a
  214. * spare CQE so the HCA HW can tell the difference between an
  215. * empty CQ and a full CQ.
  216. */
  217. dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
  218. dev->caps.reserved_cqs = dev_cap->reserved_cqs;
  219. dev->caps.reserved_eqs = dev_cap->reserved_eqs;
  220. dev->caps.reserved_mtts = dev_cap->reserved_mtts;
  221. dev->caps.reserved_mrws = dev_cap->reserved_mrws;
  222. /* The first 128 UARs are used for EQ doorbells */
  223. dev->caps.reserved_uars = max_t(int, 128, dev_cap->reserved_uars);
  224. dev->caps.reserved_pds = dev_cap->reserved_pds;
  225. dev->caps.reserved_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
  226. dev_cap->reserved_xrcds : 0;
  227. dev->caps.max_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
  228. dev_cap->max_xrcds : 0;
  229. dev->caps.mtt_entry_sz = dev_cap->mtt_entry_sz;
  230. dev->caps.max_msg_sz = dev_cap->max_msg_sz;
  231. dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
  232. dev->caps.flags = dev_cap->flags;
  233. dev->caps.flags2 = dev_cap->flags2;
  234. dev->caps.bmme_flags = dev_cap->bmme_flags;
  235. dev->caps.reserved_lkey = dev_cap->reserved_lkey;
  236. dev->caps.stat_rate_support = dev_cap->stat_rate_support;
  237. dev->caps.max_gso_sz = dev_cap->max_gso_sz;
  238. dev->caps.max_rss_tbl_sz = dev_cap->max_rss_tbl_sz;
  239. if (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_FS_EN) {
  240. dev->caps.steering_mode = MLX4_STEERING_MODE_DEVICE_MANAGED;
  241. dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
  242. dev->caps.fs_log_max_ucast_qp_range_size =
  243. dev_cap->fs_log_max_ucast_qp_range_size;
  244. } else {
  245. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER &&
  246. dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) {
  247. dev->caps.steering_mode = MLX4_STEERING_MODE_B0;
  248. } else {
  249. dev->caps.steering_mode = MLX4_STEERING_MODE_A0;
  250. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER ||
  251. dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
  252. mlx4_warn(dev, "Must have UC_STEER and MC_STEER flags "
  253. "set to use B0 steering. Falling back to A0 steering mode.\n");
  254. }
  255. dev->caps.num_qp_per_mgm = mlx4_get_qp_per_mgm(dev);
  256. }
  257. mlx4_dbg(dev, "Steering mode is: %s\n",
  258. mlx4_steering_mode_str(dev->caps.steering_mode));
  259. /* Sense port always allowed on supported devices for ConnectX-1 and -2 */
  260. if (mlx4_priv(dev)->pci_dev_data & MLX4_PCI_DEV_FORCE_SENSE_PORT)
  261. dev->caps.flags |= MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
  262. /* Don't do sense port on multifunction devices (for now at least) */
  263. if (mlx4_is_mfunc(dev))
  264. dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
  265. dev->caps.log_num_macs = log_num_mac;
  266. dev->caps.log_num_vlans = MLX4_LOG_NUM_VLANS;
  267. dev->caps.log_num_prios = use_prio ? 3 : 0;
  268. for (i = 1; i <= dev->caps.num_ports; ++i) {
  269. dev->caps.port_type[i] = MLX4_PORT_TYPE_NONE;
  270. if (dev->caps.supported_type[i]) {
  271. /* if only ETH is supported - assign ETH */
  272. if (dev->caps.supported_type[i] == MLX4_PORT_TYPE_ETH)
  273. dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
  274. /* if only IB is supported, assign IB */
  275. else if (dev->caps.supported_type[i] ==
  276. MLX4_PORT_TYPE_IB)
  277. dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
  278. else {
  279. /* if IB and ETH are supported, we set the port
  280. * type according to user selection of port type;
  281. * if user selected none, take the FW hint */
  282. if (port_type_array[i - 1] == MLX4_PORT_TYPE_NONE)
  283. dev->caps.port_type[i] = dev->caps.suggested_type[i] ?
  284. MLX4_PORT_TYPE_ETH : MLX4_PORT_TYPE_IB;
  285. else
  286. dev->caps.port_type[i] = port_type_array[i - 1];
  287. }
  288. }
  289. /*
  290. * Link sensing is allowed on the port if 3 conditions are true:
  291. * 1. Both protocols are supported on the port.
  292. * 2. Different types are supported on the port
  293. * 3. FW declared that it supports link sensing
  294. */
  295. mlx4_priv(dev)->sense.sense_allowed[i] =
  296. ((dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO) &&
  297. (dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
  298. (dev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT));
  299. /*
  300. * If "default_sense" bit is set, we move the port to "AUTO" mode
  301. * and perform sense_port FW command to try and set the correct
  302. * port type from beginning
  303. */
  304. if (mlx4_priv(dev)->sense.sense_allowed[i] && dev->caps.default_sense[i]) {
  305. enum mlx4_port_type sensed_port = MLX4_PORT_TYPE_NONE;
  306. dev->caps.possible_type[i] = MLX4_PORT_TYPE_AUTO;
  307. mlx4_SENSE_PORT(dev, i, &sensed_port);
  308. if (sensed_port != MLX4_PORT_TYPE_NONE)
  309. dev->caps.port_type[i] = sensed_port;
  310. } else {
  311. dev->caps.possible_type[i] = dev->caps.port_type[i];
  312. }
  313. if (dev->caps.log_num_macs > dev_cap->log_max_macs[i]) {
  314. dev->caps.log_num_macs = dev_cap->log_max_macs[i];
  315. mlx4_warn(dev, "Requested number of MACs is too much "
  316. "for port %d, reducing to %d.\n",
  317. i, 1 << dev->caps.log_num_macs);
  318. }
  319. if (dev->caps.log_num_vlans > dev_cap->log_max_vlans[i]) {
  320. dev->caps.log_num_vlans = dev_cap->log_max_vlans[i];
  321. mlx4_warn(dev, "Requested number of VLANs is too much "
  322. "for port %d, reducing to %d.\n",
  323. i, 1 << dev->caps.log_num_vlans);
  324. }
  325. }
  326. dev->caps.max_counters = 1 << ilog2(dev_cap->max_counters);
  327. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
  328. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
  329. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
  330. (1 << dev->caps.log_num_macs) *
  331. (1 << dev->caps.log_num_vlans) *
  332. (1 << dev->caps.log_num_prios) *
  333. dev->caps.num_ports;
  334. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
  335. dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
  336. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
  337. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
  338. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
  339. dev->caps.sqp_demux = (mlx4_is_master(dev)) ? MLX4_MAX_NUM_SLAVES : 0;
  340. return 0;
  341. }
  342. /*The function checks if there are live vf, return the num of them*/
  343. static int mlx4_how_many_lives_vf(struct mlx4_dev *dev)
  344. {
  345. struct mlx4_priv *priv = mlx4_priv(dev);
  346. struct mlx4_slave_state *s_state;
  347. int i;
  348. int ret = 0;
  349. for (i = 1/*the ppf is 0*/; i < dev->num_slaves; ++i) {
  350. s_state = &priv->mfunc.master.slave_state[i];
  351. if (s_state->active && s_state->last_cmd !=
  352. MLX4_COMM_CMD_RESET) {
  353. mlx4_warn(dev, "%s: slave: %d is still active\n",
  354. __func__, i);
  355. ret++;
  356. }
  357. }
  358. return ret;
  359. }
  360. int mlx4_get_parav_qkey(struct mlx4_dev *dev, u32 qpn, u32 *qkey)
  361. {
  362. u32 qk = MLX4_RESERVED_QKEY_BASE;
  363. if (qpn >= dev->phys_caps.base_tunnel_sqpn + 8 * MLX4_MFUNC_MAX ||
  364. qpn < dev->phys_caps.base_proxy_sqpn)
  365. return -EINVAL;
  366. if (qpn >= dev->phys_caps.base_tunnel_sqpn)
  367. /* tunnel qp */
  368. qk += qpn - dev->phys_caps.base_tunnel_sqpn;
  369. else
  370. qk += qpn - dev->phys_caps.base_proxy_sqpn;
  371. *qkey = qk;
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(mlx4_get_parav_qkey);
  375. void mlx4_sync_pkey_table(struct mlx4_dev *dev, int slave, int port, int i, int val)
  376. {
  377. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  378. if (!mlx4_is_master(dev))
  379. return;
  380. priv->virt2phys_pkey[slave][port - 1][i] = val;
  381. }
  382. EXPORT_SYMBOL(mlx4_sync_pkey_table);
  383. void mlx4_put_slave_node_guid(struct mlx4_dev *dev, int slave, __be64 guid)
  384. {
  385. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  386. if (!mlx4_is_master(dev))
  387. return;
  388. priv->slave_node_guids[slave] = guid;
  389. }
  390. EXPORT_SYMBOL(mlx4_put_slave_node_guid);
  391. __be64 mlx4_get_slave_node_guid(struct mlx4_dev *dev, int slave)
  392. {
  393. struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
  394. if (!mlx4_is_master(dev))
  395. return 0;
  396. return priv->slave_node_guids[slave];
  397. }
  398. EXPORT_SYMBOL(mlx4_get_slave_node_guid);
  399. int mlx4_is_slave_active(struct mlx4_dev *dev, int slave)
  400. {
  401. struct mlx4_priv *priv = mlx4_priv(dev);
  402. struct mlx4_slave_state *s_slave;
  403. if (!mlx4_is_master(dev))
  404. return 0;
  405. s_slave = &priv->mfunc.master.slave_state[slave];
  406. return !!s_slave->active;
  407. }
  408. EXPORT_SYMBOL(mlx4_is_slave_active);
  409. static int mlx4_slave_cap(struct mlx4_dev *dev)
  410. {
  411. int err;
  412. u32 page_size;
  413. struct mlx4_dev_cap dev_cap;
  414. struct mlx4_func_cap func_cap;
  415. struct mlx4_init_hca_param hca_param;
  416. int i;
  417. memset(&hca_param, 0, sizeof(hca_param));
  418. err = mlx4_QUERY_HCA(dev, &hca_param);
  419. if (err) {
  420. mlx4_err(dev, "QUERY_HCA command failed, aborting.\n");
  421. return err;
  422. }
  423. /*fail if the hca has an unknown capability */
  424. if ((hca_param.global_caps | HCA_GLOBAL_CAP_MASK) !=
  425. HCA_GLOBAL_CAP_MASK) {
  426. mlx4_err(dev, "Unknown hca global capabilities\n");
  427. return -ENOSYS;
  428. }
  429. mlx4_log_num_mgm_entry_size = hca_param.log_mc_entry_sz;
  430. memset(&dev_cap, 0, sizeof(dev_cap));
  431. dev->caps.max_qp_dest_rdma = 1 << hca_param.log_rd_per_qp;
  432. err = mlx4_dev_cap(dev, &dev_cap);
  433. if (err) {
  434. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  435. return err;
  436. }
  437. err = mlx4_QUERY_FW(dev);
  438. if (err)
  439. mlx4_err(dev, "QUERY_FW command failed: could not get FW version.\n");
  440. page_size = ~dev->caps.page_size_cap + 1;
  441. mlx4_warn(dev, "HCA minimum page size:%d\n", page_size);
  442. if (page_size > PAGE_SIZE) {
  443. mlx4_err(dev, "HCA minimum page size of %d bigger than "
  444. "kernel PAGE_SIZE of %ld, aborting.\n",
  445. page_size, PAGE_SIZE);
  446. return -ENODEV;
  447. }
  448. /* slave gets uar page size from QUERY_HCA fw command */
  449. dev->caps.uar_page_size = 1 << (hca_param.uar_page_sz + 12);
  450. /* TODO: relax this assumption */
  451. if (dev->caps.uar_page_size != PAGE_SIZE) {
  452. mlx4_err(dev, "UAR size:%d != kernel PAGE_SIZE of %ld\n",
  453. dev->caps.uar_page_size, PAGE_SIZE);
  454. return -ENODEV;
  455. }
  456. memset(&func_cap, 0, sizeof(func_cap));
  457. err = mlx4_QUERY_FUNC_CAP(dev, 0, &func_cap);
  458. if (err) {
  459. mlx4_err(dev, "QUERY_FUNC_CAP general command failed, aborting (%d).\n",
  460. err);
  461. return err;
  462. }
  463. if ((func_cap.pf_context_behaviour | PF_CONTEXT_BEHAVIOUR_MASK) !=
  464. PF_CONTEXT_BEHAVIOUR_MASK) {
  465. mlx4_err(dev, "Unknown pf context behaviour\n");
  466. return -ENOSYS;
  467. }
  468. dev->caps.num_ports = func_cap.num_ports;
  469. dev->caps.num_qps = func_cap.qp_quota;
  470. dev->caps.num_srqs = func_cap.srq_quota;
  471. dev->caps.num_cqs = func_cap.cq_quota;
  472. dev->caps.num_eqs = func_cap.max_eq;
  473. dev->caps.reserved_eqs = func_cap.reserved_eq;
  474. dev->caps.num_mpts = func_cap.mpt_quota;
  475. dev->caps.num_mtts = func_cap.mtt_quota;
  476. dev->caps.num_pds = MLX4_NUM_PDS;
  477. dev->caps.num_mgms = 0;
  478. dev->caps.num_amgms = 0;
  479. if (dev->caps.num_ports > MLX4_MAX_PORTS) {
  480. mlx4_err(dev, "HCA has %d ports, but we only support %d, "
  481. "aborting.\n", dev->caps.num_ports, MLX4_MAX_PORTS);
  482. return -ENODEV;
  483. }
  484. dev->caps.qp0_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  485. dev->caps.qp0_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  486. dev->caps.qp1_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  487. dev->caps.qp1_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  488. if (!dev->caps.qp0_tunnel || !dev->caps.qp0_proxy ||
  489. !dev->caps.qp1_tunnel || !dev->caps.qp1_proxy) {
  490. err = -ENOMEM;
  491. goto err_mem;
  492. }
  493. for (i = 1; i <= dev->caps.num_ports; ++i) {
  494. err = mlx4_QUERY_FUNC_CAP(dev, (u32) i, &func_cap);
  495. if (err) {
  496. mlx4_err(dev, "QUERY_FUNC_CAP port command failed for"
  497. " port %d, aborting (%d).\n", i, err);
  498. goto err_mem;
  499. }
  500. dev->caps.qp0_tunnel[i - 1] = func_cap.qp0_tunnel_qpn;
  501. dev->caps.qp0_proxy[i - 1] = func_cap.qp0_proxy_qpn;
  502. dev->caps.qp1_tunnel[i - 1] = func_cap.qp1_tunnel_qpn;
  503. dev->caps.qp1_proxy[i - 1] = func_cap.qp1_proxy_qpn;
  504. dev->caps.port_mask[i] = dev->caps.port_type[i];
  505. if (mlx4_get_slave_pkey_gid_tbl_len(dev, i,
  506. &dev->caps.gid_table_len[i],
  507. &dev->caps.pkey_table_len[i]))
  508. goto err_mem;
  509. }
  510. if (dev->caps.uar_page_size * (dev->caps.num_uars -
  511. dev->caps.reserved_uars) >
  512. pci_resource_len(dev->pdev, 2)) {
  513. mlx4_err(dev, "HCA reported UAR region size of 0x%x bigger than "
  514. "PCI resource 2 size of 0x%llx, aborting.\n",
  515. dev->caps.uar_page_size * dev->caps.num_uars,
  516. (unsigned long long) pci_resource_len(dev->pdev, 2));
  517. goto err_mem;
  518. }
  519. return 0;
  520. err_mem:
  521. kfree(dev->caps.qp0_tunnel);
  522. kfree(dev->caps.qp0_proxy);
  523. kfree(dev->caps.qp1_tunnel);
  524. kfree(dev->caps.qp1_proxy);
  525. dev->caps.qp0_tunnel = dev->caps.qp0_proxy =
  526. dev->caps.qp1_tunnel = dev->caps.qp1_proxy = NULL;
  527. return err;
  528. }
  529. /*
  530. * Change the port configuration of the device.
  531. * Every user of this function must hold the port mutex.
  532. */
  533. int mlx4_change_port_types(struct mlx4_dev *dev,
  534. enum mlx4_port_type *port_types)
  535. {
  536. int err = 0;
  537. int change = 0;
  538. int port;
  539. for (port = 0; port < dev->caps.num_ports; port++) {
  540. /* Change the port type only if the new type is different
  541. * from the current, and not set to Auto */
  542. if (port_types[port] != dev->caps.port_type[port + 1])
  543. change = 1;
  544. }
  545. if (change) {
  546. mlx4_unregister_device(dev);
  547. for (port = 1; port <= dev->caps.num_ports; port++) {
  548. mlx4_CLOSE_PORT(dev, port);
  549. dev->caps.port_type[port] = port_types[port - 1];
  550. err = mlx4_SET_PORT(dev, port, -1);
  551. if (err) {
  552. mlx4_err(dev, "Failed to set port %d, "
  553. "aborting\n", port);
  554. goto out;
  555. }
  556. }
  557. mlx4_set_port_mask(dev);
  558. err = mlx4_register_device(dev);
  559. }
  560. out:
  561. return err;
  562. }
  563. static ssize_t show_port_type(struct device *dev,
  564. struct device_attribute *attr,
  565. char *buf)
  566. {
  567. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  568. port_attr);
  569. struct mlx4_dev *mdev = info->dev;
  570. char type[8];
  571. sprintf(type, "%s",
  572. (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
  573. "ib" : "eth");
  574. if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
  575. sprintf(buf, "auto (%s)\n", type);
  576. else
  577. sprintf(buf, "%s\n", type);
  578. return strlen(buf);
  579. }
  580. static ssize_t set_port_type(struct device *dev,
  581. struct device_attribute *attr,
  582. const char *buf, size_t count)
  583. {
  584. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  585. port_attr);
  586. struct mlx4_dev *mdev = info->dev;
  587. struct mlx4_priv *priv = mlx4_priv(mdev);
  588. enum mlx4_port_type types[MLX4_MAX_PORTS];
  589. enum mlx4_port_type new_types[MLX4_MAX_PORTS];
  590. int i;
  591. int err = 0;
  592. if (!strcmp(buf, "ib\n"))
  593. info->tmp_type = MLX4_PORT_TYPE_IB;
  594. else if (!strcmp(buf, "eth\n"))
  595. info->tmp_type = MLX4_PORT_TYPE_ETH;
  596. else if (!strcmp(buf, "auto\n"))
  597. info->tmp_type = MLX4_PORT_TYPE_AUTO;
  598. else {
  599. mlx4_err(mdev, "%s is not supported port type\n", buf);
  600. return -EINVAL;
  601. }
  602. mlx4_stop_sense(mdev);
  603. mutex_lock(&priv->port_mutex);
  604. /* Possible type is always the one that was delivered */
  605. mdev->caps.possible_type[info->port] = info->tmp_type;
  606. for (i = 0; i < mdev->caps.num_ports; i++) {
  607. types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
  608. mdev->caps.possible_type[i+1];
  609. if (types[i] == MLX4_PORT_TYPE_AUTO)
  610. types[i] = mdev->caps.port_type[i+1];
  611. }
  612. if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
  613. !(mdev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT)) {
  614. for (i = 1; i <= mdev->caps.num_ports; i++) {
  615. if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
  616. mdev->caps.possible_type[i] = mdev->caps.port_type[i];
  617. err = -EINVAL;
  618. }
  619. }
  620. }
  621. if (err) {
  622. mlx4_err(mdev, "Auto sensing is not supported on this HCA. "
  623. "Set only 'eth' or 'ib' for both ports "
  624. "(should be the same)\n");
  625. goto out;
  626. }
  627. mlx4_do_sense_ports(mdev, new_types, types);
  628. err = mlx4_check_port_params(mdev, new_types);
  629. if (err)
  630. goto out;
  631. /* We are about to apply the changes after the configuration
  632. * was verified, no need to remember the temporary types
  633. * any more */
  634. for (i = 0; i < mdev->caps.num_ports; i++)
  635. priv->port[i + 1].tmp_type = 0;
  636. err = mlx4_change_port_types(mdev, new_types);
  637. out:
  638. mlx4_start_sense(mdev);
  639. mutex_unlock(&priv->port_mutex);
  640. return err ? err : count;
  641. }
  642. enum ibta_mtu {
  643. IB_MTU_256 = 1,
  644. IB_MTU_512 = 2,
  645. IB_MTU_1024 = 3,
  646. IB_MTU_2048 = 4,
  647. IB_MTU_4096 = 5
  648. };
  649. static inline int int_to_ibta_mtu(int mtu)
  650. {
  651. switch (mtu) {
  652. case 256: return IB_MTU_256;
  653. case 512: return IB_MTU_512;
  654. case 1024: return IB_MTU_1024;
  655. case 2048: return IB_MTU_2048;
  656. case 4096: return IB_MTU_4096;
  657. default: return -1;
  658. }
  659. }
  660. static inline int ibta_mtu_to_int(enum ibta_mtu mtu)
  661. {
  662. switch (mtu) {
  663. case IB_MTU_256: return 256;
  664. case IB_MTU_512: return 512;
  665. case IB_MTU_1024: return 1024;
  666. case IB_MTU_2048: return 2048;
  667. case IB_MTU_4096: return 4096;
  668. default: return -1;
  669. }
  670. }
  671. static ssize_t show_port_ib_mtu(struct device *dev,
  672. struct device_attribute *attr,
  673. char *buf)
  674. {
  675. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  676. port_mtu_attr);
  677. struct mlx4_dev *mdev = info->dev;
  678. if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH)
  679. mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
  680. sprintf(buf, "%d\n",
  681. ibta_mtu_to_int(mdev->caps.port_ib_mtu[info->port]));
  682. return strlen(buf);
  683. }
  684. static ssize_t set_port_ib_mtu(struct device *dev,
  685. struct device_attribute *attr,
  686. const char *buf, size_t count)
  687. {
  688. struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
  689. port_mtu_attr);
  690. struct mlx4_dev *mdev = info->dev;
  691. struct mlx4_priv *priv = mlx4_priv(mdev);
  692. int err, port, mtu, ibta_mtu = -1;
  693. if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH) {
  694. mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
  695. return -EINVAL;
  696. }
  697. err = sscanf(buf, "%d", &mtu);
  698. if (err > 0)
  699. ibta_mtu = int_to_ibta_mtu(mtu);
  700. if (err <= 0 || ibta_mtu < 0) {
  701. mlx4_err(mdev, "%s is invalid IBTA mtu\n", buf);
  702. return -EINVAL;
  703. }
  704. mdev->caps.port_ib_mtu[info->port] = ibta_mtu;
  705. mlx4_stop_sense(mdev);
  706. mutex_lock(&priv->port_mutex);
  707. mlx4_unregister_device(mdev);
  708. for (port = 1; port <= mdev->caps.num_ports; port++) {
  709. mlx4_CLOSE_PORT(mdev, port);
  710. err = mlx4_SET_PORT(mdev, port, -1);
  711. if (err) {
  712. mlx4_err(mdev, "Failed to set port %d, "
  713. "aborting\n", port);
  714. goto err_set_port;
  715. }
  716. }
  717. err = mlx4_register_device(mdev);
  718. err_set_port:
  719. mutex_unlock(&priv->port_mutex);
  720. mlx4_start_sense(mdev);
  721. return err ? err : count;
  722. }
  723. static int mlx4_load_fw(struct mlx4_dev *dev)
  724. {
  725. struct mlx4_priv *priv = mlx4_priv(dev);
  726. int err;
  727. priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
  728. GFP_HIGHUSER | __GFP_NOWARN, 0);
  729. if (!priv->fw.fw_icm) {
  730. mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
  731. return -ENOMEM;
  732. }
  733. err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
  734. if (err) {
  735. mlx4_err(dev, "MAP_FA command failed, aborting.\n");
  736. goto err_free;
  737. }
  738. err = mlx4_RUN_FW(dev);
  739. if (err) {
  740. mlx4_err(dev, "RUN_FW command failed, aborting.\n");
  741. goto err_unmap_fa;
  742. }
  743. return 0;
  744. err_unmap_fa:
  745. mlx4_UNMAP_FA(dev);
  746. err_free:
  747. mlx4_free_icm(dev, priv->fw.fw_icm, 0);
  748. return err;
  749. }
  750. static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
  751. int cmpt_entry_sz)
  752. {
  753. struct mlx4_priv *priv = mlx4_priv(dev);
  754. int err;
  755. int num_eqs;
  756. err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
  757. cmpt_base +
  758. ((u64) (MLX4_CMPT_TYPE_QP *
  759. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  760. cmpt_entry_sz, dev->caps.num_qps,
  761. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  762. 0, 0);
  763. if (err)
  764. goto err;
  765. err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
  766. cmpt_base +
  767. ((u64) (MLX4_CMPT_TYPE_SRQ *
  768. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  769. cmpt_entry_sz, dev->caps.num_srqs,
  770. dev->caps.reserved_srqs, 0, 0);
  771. if (err)
  772. goto err_qp;
  773. err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
  774. cmpt_base +
  775. ((u64) (MLX4_CMPT_TYPE_CQ *
  776. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  777. cmpt_entry_sz, dev->caps.num_cqs,
  778. dev->caps.reserved_cqs, 0, 0);
  779. if (err)
  780. goto err_srq;
  781. num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
  782. dev->caps.num_eqs;
  783. err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
  784. cmpt_base +
  785. ((u64) (MLX4_CMPT_TYPE_EQ *
  786. cmpt_entry_sz) << MLX4_CMPT_SHIFT),
  787. cmpt_entry_sz, num_eqs, num_eqs, 0, 0);
  788. if (err)
  789. goto err_cq;
  790. return 0;
  791. err_cq:
  792. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  793. err_srq:
  794. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  795. err_qp:
  796. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  797. err:
  798. return err;
  799. }
  800. static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
  801. struct mlx4_init_hca_param *init_hca, u64 icm_size)
  802. {
  803. struct mlx4_priv *priv = mlx4_priv(dev);
  804. u64 aux_pages;
  805. int num_eqs;
  806. int err;
  807. err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
  808. if (err) {
  809. mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
  810. return err;
  811. }
  812. mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
  813. (unsigned long long) icm_size >> 10,
  814. (unsigned long long) aux_pages << 2);
  815. priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
  816. GFP_HIGHUSER | __GFP_NOWARN, 0);
  817. if (!priv->fw.aux_icm) {
  818. mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
  819. return -ENOMEM;
  820. }
  821. err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
  822. if (err) {
  823. mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
  824. goto err_free_aux;
  825. }
  826. err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
  827. if (err) {
  828. mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
  829. goto err_unmap_aux;
  830. }
  831. num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
  832. dev->caps.num_eqs;
  833. err = mlx4_init_icm_table(dev, &priv->eq_table.table,
  834. init_hca->eqc_base, dev_cap->eqc_entry_sz,
  835. num_eqs, num_eqs, 0, 0);
  836. if (err) {
  837. mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
  838. goto err_unmap_cmpt;
  839. }
  840. /*
  841. * Reserved MTT entries must be aligned up to a cacheline
  842. * boundary, since the FW will write to them, while the driver
  843. * writes to all other MTT entries. (The variable
  844. * dev->caps.mtt_entry_sz below is really the MTT segment
  845. * size, not the raw entry size)
  846. */
  847. dev->caps.reserved_mtts =
  848. ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
  849. dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
  850. err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
  851. init_hca->mtt_base,
  852. dev->caps.mtt_entry_sz,
  853. dev->caps.num_mtts,
  854. dev->caps.reserved_mtts, 1, 0);
  855. if (err) {
  856. mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
  857. goto err_unmap_eq;
  858. }
  859. err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
  860. init_hca->dmpt_base,
  861. dev_cap->dmpt_entry_sz,
  862. dev->caps.num_mpts,
  863. dev->caps.reserved_mrws, 1, 1);
  864. if (err) {
  865. mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
  866. goto err_unmap_mtt;
  867. }
  868. err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
  869. init_hca->qpc_base,
  870. dev_cap->qpc_entry_sz,
  871. dev->caps.num_qps,
  872. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  873. 0, 0);
  874. if (err) {
  875. mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
  876. goto err_unmap_dmpt;
  877. }
  878. err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
  879. init_hca->auxc_base,
  880. dev_cap->aux_entry_sz,
  881. dev->caps.num_qps,
  882. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  883. 0, 0);
  884. if (err) {
  885. mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
  886. goto err_unmap_qp;
  887. }
  888. err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
  889. init_hca->altc_base,
  890. dev_cap->altc_entry_sz,
  891. dev->caps.num_qps,
  892. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  893. 0, 0);
  894. if (err) {
  895. mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
  896. goto err_unmap_auxc;
  897. }
  898. err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
  899. init_hca->rdmarc_base,
  900. dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
  901. dev->caps.num_qps,
  902. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  903. 0, 0);
  904. if (err) {
  905. mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
  906. goto err_unmap_altc;
  907. }
  908. err = mlx4_init_icm_table(dev, &priv->cq_table.table,
  909. init_hca->cqc_base,
  910. dev_cap->cqc_entry_sz,
  911. dev->caps.num_cqs,
  912. dev->caps.reserved_cqs, 0, 0);
  913. if (err) {
  914. mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
  915. goto err_unmap_rdmarc;
  916. }
  917. err = mlx4_init_icm_table(dev, &priv->srq_table.table,
  918. init_hca->srqc_base,
  919. dev_cap->srq_entry_sz,
  920. dev->caps.num_srqs,
  921. dev->caps.reserved_srqs, 0, 0);
  922. if (err) {
  923. mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
  924. goto err_unmap_cq;
  925. }
  926. /*
  927. * For flow steering device managed mode it is required to use
  928. * mlx4_init_icm_table. For B0 steering mode it's not strictly
  929. * required, but for simplicity just map the whole multicast
  930. * group table now. The table isn't very big and it's a lot
  931. * easier than trying to track ref counts.
  932. */
  933. err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
  934. init_hca->mc_base,
  935. mlx4_get_mgm_entry_size(dev),
  936. dev->caps.num_mgms + dev->caps.num_amgms,
  937. dev->caps.num_mgms + dev->caps.num_amgms,
  938. 0, 0);
  939. if (err) {
  940. mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
  941. goto err_unmap_srq;
  942. }
  943. return 0;
  944. err_unmap_srq:
  945. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  946. err_unmap_cq:
  947. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  948. err_unmap_rdmarc:
  949. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  950. err_unmap_altc:
  951. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  952. err_unmap_auxc:
  953. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  954. err_unmap_qp:
  955. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  956. err_unmap_dmpt:
  957. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  958. err_unmap_mtt:
  959. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  960. err_unmap_eq:
  961. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  962. err_unmap_cmpt:
  963. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  964. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  965. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  966. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  967. err_unmap_aux:
  968. mlx4_UNMAP_ICM_AUX(dev);
  969. err_free_aux:
  970. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  971. return err;
  972. }
  973. static void mlx4_free_icms(struct mlx4_dev *dev)
  974. {
  975. struct mlx4_priv *priv = mlx4_priv(dev);
  976. mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
  977. mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
  978. mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
  979. mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
  980. mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
  981. mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
  982. mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
  983. mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
  984. mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
  985. mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
  986. mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
  987. mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
  988. mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
  989. mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
  990. mlx4_UNMAP_ICM_AUX(dev);
  991. mlx4_free_icm(dev, priv->fw.aux_icm, 0);
  992. }
  993. static void mlx4_slave_exit(struct mlx4_dev *dev)
  994. {
  995. struct mlx4_priv *priv = mlx4_priv(dev);
  996. mutex_lock(&priv->cmd.slave_cmd_mutex);
  997. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_TIME))
  998. mlx4_warn(dev, "Failed to close slave function.\n");
  999. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1000. }
  1001. static int map_bf_area(struct mlx4_dev *dev)
  1002. {
  1003. struct mlx4_priv *priv = mlx4_priv(dev);
  1004. resource_size_t bf_start;
  1005. resource_size_t bf_len;
  1006. int err = 0;
  1007. if (!dev->caps.bf_reg_size)
  1008. return -ENXIO;
  1009. bf_start = pci_resource_start(dev->pdev, 2) +
  1010. (dev->caps.num_uars << PAGE_SHIFT);
  1011. bf_len = pci_resource_len(dev->pdev, 2) -
  1012. (dev->caps.num_uars << PAGE_SHIFT);
  1013. priv->bf_mapping = io_mapping_create_wc(bf_start, bf_len);
  1014. if (!priv->bf_mapping)
  1015. err = -ENOMEM;
  1016. return err;
  1017. }
  1018. static void unmap_bf_area(struct mlx4_dev *dev)
  1019. {
  1020. if (mlx4_priv(dev)->bf_mapping)
  1021. io_mapping_free(mlx4_priv(dev)->bf_mapping);
  1022. }
  1023. static void mlx4_close_hca(struct mlx4_dev *dev)
  1024. {
  1025. unmap_bf_area(dev);
  1026. if (mlx4_is_slave(dev))
  1027. mlx4_slave_exit(dev);
  1028. else {
  1029. mlx4_CLOSE_HCA(dev, 0);
  1030. mlx4_free_icms(dev);
  1031. mlx4_UNMAP_FA(dev);
  1032. mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
  1033. }
  1034. }
  1035. static int mlx4_init_slave(struct mlx4_dev *dev)
  1036. {
  1037. struct mlx4_priv *priv = mlx4_priv(dev);
  1038. u64 dma = (u64) priv->mfunc.vhcr_dma;
  1039. int num_of_reset_retries = NUM_OF_RESET_RETRIES;
  1040. int ret_from_reset = 0;
  1041. u32 slave_read;
  1042. u32 cmd_channel_ver;
  1043. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1044. priv->cmd.max_cmds = 1;
  1045. mlx4_warn(dev, "Sending reset\n");
  1046. ret_from_reset = mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0,
  1047. MLX4_COMM_TIME);
  1048. /* if we are in the middle of flr the slave will try
  1049. * NUM_OF_RESET_RETRIES times before leaving.*/
  1050. if (ret_from_reset) {
  1051. if (MLX4_DELAY_RESET_SLAVE == ret_from_reset) {
  1052. msleep(SLEEP_TIME_IN_RESET);
  1053. while (ret_from_reset && num_of_reset_retries) {
  1054. mlx4_warn(dev, "slave is currently in the"
  1055. "middle of FLR. retrying..."
  1056. "(try num:%d)\n",
  1057. (NUM_OF_RESET_RETRIES -
  1058. num_of_reset_retries + 1));
  1059. ret_from_reset =
  1060. mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET,
  1061. 0, MLX4_COMM_TIME);
  1062. num_of_reset_retries = num_of_reset_retries - 1;
  1063. }
  1064. } else
  1065. goto err;
  1066. }
  1067. /* check the driver version - the slave I/F revision
  1068. * must match the master's */
  1069. slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
  1070. cmd_channel_ver = mlx4_comm_get_version();
  1071. if (MLX4_COMM_GET_IF_REV(cmd_channel_ver) !=
  1072. MLX4_COMM_GET_IF_REV(slave_read)) {
  1073. mlx4_err(dev, "slave driver version is not supported"
  1074. " by the master\n");
  1075. goto err;
  1076. }
  1077. mlx4_warn(dev, "Sending vhcr0\n");
  1078. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR0, dma >> 48,
  1079. MLX4_COMM_TIME))
  1080. goto err;
  1081. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR1, dma >> 32,
  1082. MLX4_COMM_TIME))
  1083. goto err;
  1084. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR2, dma >> 16,
  1085. MLX4_COMM_TIME))
  1086. goto err;
  1087. if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_EN, dma, MLX4_COMM_TIME))
  1088. goto err;
  1089. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1090. return 0;
  1091. err:
  1092. mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, 0);
  1093. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1094. return -EIO;
  1095. }
  1096. static void mlx4_parav_master_pf_caps(struct mlx4_dev *dev)
  1097. {
  1098. int i;
  1099. for (i = 1; i <= dev->caps.num_ports; i++) {
  1100. dev->caps.gid_table_len[i] = 1;
  1101. dev->caps.pkey_table_len[i] =
  1102. dev->phys_caps.pkey_phys_table_len[i] - 1;
  1103. }
  1104. }
  1105. static int mlx4_init_hca(struct mlx4_dev *dev)
  1106. {
  1107. struct mlx4_priv *priv = mlx4_priv(dev);
  1108. struct mlx4_adapter adapter;
  1109. struct mlx4_dev_cap dev_cap;
  1110. struct mlx4_mod_stat_cfg mlx4_cfg;
  1111. struct mlx4_profile profile;
  1112. struct mlx4_init_hca_param init_hca;
  1113. u64 icm_size;
  1114. int err;
  1115. if (!mlx4_is_slave(dev)) {
  1116. err = mlx4_QUERY_FW(dev);
  1117. if (err) {
  1118. if (err == -EACCES)
  1119. mlx4_info(dev, "non-primary physical function, skipping.\n");
  1120. else
  1121. mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
  1122. return err;
  1123. }
  1124. err = mlx4_load_fw(dev);
  1125. if (err) {
  1126. mlx4_err(dev, "Failed to start FW, aborting.\n");
  1127. return err;
  1128. }
  1129. mlx4_cfg.log_pg_sz_m = 1;
  1130. mlx4_cfg.log_pg_sz = 0;
  1131. err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
  1132. if (err)
  1133. mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
  1134. err = mlx4_dev_cap(dev, &dev_cap);
  1135. if (err) {
  1136. mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
  1137. goto err_stop_fw;
  1138. }
  1139. if (mlx4_is_master(dev))
  1140. mlx4_parav_master_pf_caps(dev);
  1141. priv->fs_hash_mode = MLX4_FS_L2_HASH;
  1142. switch (priv->fs_hash_mode) {
  1143. case MLX4_FS_L2_HASH:
  1144. init_hca.fs_hash_enable_bits = 0;
  1145. break;
  1146. case MLX4_FS_L2_L3_L4_HASH:
  1147. /* Enable flow steering with
  1148. * udp unicast and tcp unicast
  1149. */
  1150. init_hca.fs_hash_enable_bits =
  1151. MLX4_FS_UDP_UC_EN | MLX4_FS_TCP_UC_EN;
  1152. break;
  1153. }
  1154. profile = default_profile;
  1155. if (dev->caps.steering_mode ==
  1156. MLX4_STEERING_MODE_DEVICE_MANAGED)
  1157. profile.num_mcg = MLX4_FS_NUM_MCG;
  1158. icm_size = mlx4_make_profile(dev, &profile, &dev_cap,
  1159. &init_hca);
  1160. if ((long long) icm_size < 0) {
  1161. err = icm_size;
  1162. goto err_stop_fw;
  1163. }
  1164. dev->caps.max_fmr_maps = (1 << (32 - ilog2(dev->caps.num_mpts))) - 1;
  1165. init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
  1166. init_hca.uar_page_sz = PAGE_SHIFT - 12;
  1167. err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
  1168. if (err)
  1169. goto err_stop_fw;
  1170. err = mlx4_INIT_HCA(dev, &init_hca);
  1171. if (err) {
  1172. mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
  1173. goto err_free_icm;
  1174. }
  1175. } else {
  1176. err = mlx4_init_slave(dev);
  1177. if (err) {
  1178. mlx4_err(dev, "Failed to initialize slave\n");
  1179. return err;
  1180. }
  1181. err = mlx4_slave_cap(dev);
  1182. if (err) {
  1183. mlx4_err(dev, "Failed to obtain slave caps\n");
  1184. goto err_close;
  1185. }
  1186. }
  1187. if (map_bf_area(dev))
  1188. mlx4_dbg(dev, "Failed to map blue flame area\n");
  1189. /*Only the master set the ports, all the rest got it from it.*/
  1190. if (!mlx4_is_slave(dev))
  1191. mlx4_set_port_mask(dev);
  1192. err = mlx4_QUERY_ADAPTER(dev, &adapter);
  1193. if (err) {
  1194. mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
  1195. goto unmap_bf;
  1196. }
  1197. priv->eq_table.inta_pin = adapter.inta_pin;
  1198. memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
  1199. return 0;
  1200. unmap_bf:
  1201. unmap_bf_area(dev);
  1202. err_close:
  1203. if (mlx4_is_slave(dev))
  1204. mlx4_slave_exit(dev);
  1205. else
  1206. mlx4_CLOSE_HCA(dev, 0);
  1207. err_free_icm:
  1208. if (!mlx4_is_slave(dev))
  1209. mlx4_free_icms(dev);
  1210. err_stop_fw:
  1211. if (!mlx4_is_slave(dev)) {
  1212. mlx4_UNMAP_FA(dev);
  1213. mlx4_free_icm(dev, priv->fw.fw_icm, 0);
  1214. }
  1215. return err;
  1216. }
  1217. static int mlx4_init_counters_table(struct mlx4_dev *dev)
  1218. {
  1219. struct mlx4_priv *priv = mlx4_priv(dev);
  1220. int nent;
  1221. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  1222. return -ENOENT;
  1223. nent = dev->caps.max_counters;
  1224. return mlx4_bitmap_init(&priv->counters_bitmap, nent, nent - 1, 0, 0);
  1225. }
  1226. static void mlx4_cleanup_counters_table(struct mlx4_dev *dev)
  1227. {
  1228. mlx4_bitmap_cleanup(&mlx4_priv(dev)->counters_bitmap);
  1229. }
  1230. int __mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
  1231. {
  1232. struct mlx4_priv *priv = mlx4_priv(dev);
  1233. if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
  1234. return -ENOENT;
  1235. *idx = mlx4_bitmap_alloc(&priv->counters_bitmap);
  1236. if (*idx == -1)
  1237. return -ENOMEM;
  1238. return 0;
  1239. }
  1240. int mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
  1241. {
  1242. u64 out_param;
  1243. int err;
  1244. if (mlx4_is_mfunc(dev)) {
  1245. err = mlx4_cmd_imm(dev, 0, &out_param, RES_COUNTER,
  1246. RES_OP_RESERVE, MLX4_CMD_ALLOC_RES,
  1247. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  1248. if (!err)
  1249. *idx = get_param_l(&out_param);
  1250. return err;
  1251. }
  1252. return __mlx4_counter_alloc(dev, idx);
  1253. }
  1254. EXPORT_SYMBOL_GPL(mlx4_counter_alloc);
  1255. void __mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
  1256. {
  1257. mlx4_bitmap_free(&mlx4_priv(dev)->counters_bitmap, idx);
  1258. return;
  1259. }
  1260. void mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
  1261. {
  1262. u64 in_param;
  1263. if (mlx4_is_mfunc(dev)) {
  1264. set_param_l(&in_param, idx);
  1265. mlx4_cmd(dev, in_param, RES_COUNTER, RES_OP_RESERVE,
  1266. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  1267. MLX4_CMD_WRAPPED);
  1268. return;
  1269. }
  1270. __mlx4_counter_free(dev, idx);
  1271. }
  1272. EXPORT_SYMBOL_GPL(mlx4_counter_free);
  1273. static int mlx4_setup_hca(struct mlx4_dev *dev)
  1274. {
  1275. struct mlx4_priv *priv = mlx4_priv(dev);
  1276. int err;
  1277. int port;
  1278. __be32 ib_port_default_caps;
  1279. err = mlx4_init_uar_table(dev);
  1280. if (err) {
  1281. mlx4_err(dev, "Failed to initialize "
  1282. "user access region table, aborting.\n");
  1283. return err;
  1284. }
  1285. err = mlx4_uar_alloc(dev, &priv->driver_uar);
  1286. if (err) {
  1287. mlx4_err(dev, "Failed to allocate driver access region, "
  1288. "aborting.\n");
  1289. goto err_uar_table_free;
  1290. }
  1291. priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  1292. if (!priv->kar) {
  1293. mlx4_err(dev, "Couldn't map kernel access region, "
  1294. "aborting.\n");
  1295. err = -ENOMEM;
  1296. goto err_uar_free;
  1297. }
  1298. err = mlx4_init_pd_table(dev);
  1299. if (err) {
  1300. mlx4_err(dev, "Failed to initialize "
  1301. "protection domain table, aborting.\n");
  1302. goto err_kar_unmap;
  1303. }
  1304. err = mlx4_init_xrcd_table(dev);
  1305. if (err) {
  1306. mlx4_err(dev, "Failed to initialize "
  1307. "reliable connection domain table, aborting.\n");
  1308. goto err_pd_table_free;
  1309. }
  1310. err = mlx4_init_mr_table(dev);
  1311. if (err) {
  1312. mlx4_err(dev, "Failed to initialize "
  1313. "memory region table, aborting.\n");
  1314. goto err_xrcd_table_free;
  1315. }
  1316. err = mlx4_init_eq_table(dev);
  1317. if (err) {
  1318. mlx4_err(dev, "Failed to initialize "
  1319. "event queue table, aborting.\n");
  1320. goto err_mr_table_free;
  1321. }
  1322. err = mlx4_cmd_use_events(dev);
  1323. if (err) {
  1324. mlx4_err(dev, "Failed to switch to event-driven "
  1325. "firmware commands, aborting.\n");
  1326. goto err_eq_table_free;
  1327. }
  1328. err = mlx4_NOP(dev);
  1329. if (err) {
  1330. if (dev->flags & MLX4_FLAG_MSI_X) {
  1331. mlx4_warn(dev, "NOP command failed to generate MSI-X "
  1332. "interrupt IRQ %d).\n",
  1333. priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
  1334. mlx4_warn(dev, "Trying again without MSI-X.\n");
  1335. } else {
  1336. mlx4_err(dev, "NOP command failed to generate interrupt "
  1337. "(IRQ %d), aborting.\n",
  1338. priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
  1339. mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
  1340. }
  1341. goto err_cmd_poll;
  1342. }
  1343. mlx4_dbg(dev, "NOP command IRQ test passed\n");
  1344. err = mlx4_init_cq_table(dev);
  1345. if (err) {
  1346. mlx4_err(dev, "Failed to initialize "
  1347. "completion queue table, aborting.\n");
  1348. goto err_cmd_poll;
  1349. }
  1350. err = mlx4_init_srq_table(dev);
  1351. if (err) {
  1352. mlx4_err(dev, "Failed to initialize "
  1353. "shared receive queue table, aborting.\n");
  1354. goto err_cq_table_free;
  1355. }
  1356. err = mlx4_init_qp_table(dev);
  1357. if (err) {
  1358. mlx4_err(dev, "Failed to initialize "
  1359. "queue pair table, aborting.\n");
  1360. goto err_srq_table_free;
  1361. }
  1362. if (!mlx4_is_slave(dev)) {
  1363. err = mlx4_init_mcg_table(dev);
  1364. if (err) {
  1365. mlx4_err(dev, "Failed to initialize "
  1366. "multicast group table, aborting.\n");
  1367. goto err_qp_table_free;
  1368. }
  1369. }
  1370. err = mlx4_init_counters_table(dev);
  1371. if (err && err != -ENOENT) {
  1372. mlx4_err(dev, "Failed to initialize counters table, aborting.\n");
  1373. goto err_mcg_table_free;
  1374. }
  1375. if (!mlx4_is_slave(dev)) {
  1376. for (port = 1; port <= dev->caps.num_ports; port++) {
  1377. ib_port_default_caps = 0;
  1378. err = mlx4_get_port_ib_caps(dev, port,
  1379. &ib_port_default_caps);
  1380. if (err)
  1381. mlx4_warn(dev, "failed to get port %d default "
  1382. "ib capabilities (%d). Continuing "
  1383. "with caps = 0\n", port, err);
  1384. dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
  1385. /* initialize per-slave default ib port capabilities */
  1386. if (mlx4_is_master(dev)) {
  1387. int i;
  1388. for (i = 0; i < dev->num_slaves; i++) {
  1389. if (i == mlx4_master_func_num(dev))
  1390. continue;
  1391. priv->mfunc.master.slave_state[i].ib_cap_mask[port] =
  1392. ib_port_default_caps;
  1393. }
  1394. }
  1395. if (mlx4_is_mfunc(dev))
  1396. dev->caps.port_ib_mtu[port] = IB_MTU_2048;
  1397. else
  1398. dev->caps.port_ib_mtu[port] = IB_MTU_4096;
  1399. err = mlx4_SET_PORT(dev, port, mlx4_is_master(dev) ?
  1400. dev->caps.pkey_table_len[port] : -1);
  1401. if (err) {
  1402. mlx4_err(dev, "Failed to set port %d, aborting\n",
  1403. port);
  1404. goto err_counters_table_free;
  1405. }
  1406. }
  1407. }
  1408. return 0;
  1409. err_counters_table_free:
  1410. mlx4_cleanup_counters_table(dev);
  1411. err_mcg_table_free:
  1412. mlx4_cleanup_mcg_table(dev);
  1413. err_qp_table_free:
  1414. mlx4_cleanup_qp_table(dev);
  1415. err_srq_table_free:
  1416. mlx4_cleanup_srq_table(dev);
  1417. err_cq_table_free:
  1418. mlx4_cleanup_cq_table(dev);
  1419. err_cmd_poll:
  1420. mlx4_cmd_use_polling(dev);
  1421. err_eq_table_free:
  1422. mlx4_cleanup_eq_table(dev);
  1423. err_mr_table_free:
  1424. mlx4_cleanup_mr_table(dev);
  1425. err_xrcd_table_free:
  1426. mlx4_cleanup_xrcd_table(dev);
  1427. err_pd_table_free:
  1428. mlx4_cleanup_pd_table(dev);
  1429. err_kar_unmap:
  1430. iounmap(priv->kar);
  1431. err_uar_free:
  1432. mlx4_uar_free(dev, &priv->driver_uar);
  1433. err_uar_table_free:
  1434. mlx4_cleanup_uar_table(dev);
  1435. return err;
  1436. }
  1437. static void mlx4_enable_msi_x(struct mlx4_dev *dev)
  1438. {
  1439. struct mlx4_priv *priv = mlx4_priv(dev);
  1440. struct msix_entry *entries;
  1441. int nreq = min_t(int, dev->caps.num_ports *
  1442. min_t(int, netif_get_num_default_rss_queues() + 1,
  1443. MAX_MSIX_P_PORT) + MSIX_LEGACY_SZ, MAX_MSIX);
  1444. int err;
  1445. int i;
  1446. if (msi_x) {
  1447. /* In multifunction mode each function gets 2 msi-X vectors
  1448. * one for data path completions anf the other for asynch events
  1449. * or command completions */
  1450. if (mlx4_is_mfunc(dev)) {
  1451. nreq = 2;
  1452. } else {
  1453. nreq = min_t(int, dev->caps.num_eqs -
  1454. dev->caps.reserved_eqs, nreq);
  1455. }
  1456. entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
  1457. if (!entries)
  1458. goto no_msi;
  1459. for (i = 0; i < nreq; ++i)
  1460. entries[i].entry = i;
  1461. retry:
  1462. err = pci_enable_msix(dev->pdev, entries, nreq);
  1463. if (err) {
  1464. /* Try again if at least 2 vectors are available */
  1465. if (err > 1) {
  1466. mlx4_info(dev, "Requested %d vectors, "
  1467. "but only %d MSI-X vectors available, "
  1468. "trying again\n", nreq, err);
  1469. nreq = err;
  1470. goto retry;
  1471. }
  1472. kfree(entries);
  1473. goto no_msi;
  1474. }
  1475. if (nreq <
  1476. MSIX_LEGACY_SZ + dev->caps.num_ports * MIN_MSIX_P_PORT) {
  1477. /*Working in legacy mode , all EQ's shared*/
  1478. dev->caps.comp_pool = 0;
  1479. dev->caps.num_comp_vectors = nreq - 1;
  1480. } else {
  1481. dev->caps.comp_pool = nreq - MSIX_LEGACY_SZ;
  1482. dev->caps.num_comp_vectors = MSIX_LEGACY_SZ - 1;
  1483. }
  1484. for (i = 0; i < nreq; ++i)
  1485. priv->eq_table.eq[i].irq = entries[i].vector;
  1486. dev->flags |= MLX4_FLAG_MSI_X;
  1487. kfree(entries);
  1488. return;
  1489. }
  1490. no_msi:
  1491. dev->caps.num_comp_vectors = 1;
  1492. dev->caps.comp_pool = 0;
  1493. for (i = 0; i < 2; ++i)
  1494. priv->eq_table.eq[i].irq = dev->pdev->irq;
  1495. }
  1496. static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
  1497. {
  1498. struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
  1499. int err = 0;
  1500. info->dev = dev;
  1501. info->port = port;
  1502. if (!mlx4_is_slave(dev)) {
  1503. INIT_RADIX_TREE(&info->mac_tree, GFP_KERNEL);
  1504. mlx4_init_mac_table(dev, &info->mac_table);
  1505. mlx4_init_vlan_table(dev, &info->vlan_table);
  1506. info->base_qpn =
  1507. dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
  1508. (port - 1) * (1 << log_num_mac);
  1509. }
  1510. sprintf(info->dev_name, "mlx4_port%d", port);
  1511. info->port_attr.attr.name = info->dev_name;
  1512. if (mlx4_is_mfunc(dev))
  1513. info->port_attr.attr.mode = S_IRUGO;
  1514. else {
  1515. info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
  1516. info->port_attr.store = set_port_type;
  1517. }
  1518. info->port_attr.show = show_port_type;
  1519. sysfs_attr_init(&info->port_attr.attr);
  1520. err = device_create_file(&dev->pdev->dev, &info->port_attr);
  1521. if (err) {
  1522. mlx4_err(dev, "Failed to create file for port %d\n", port);
  1523. info->port = -1;
  1524. }
  1525. sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
  1526. info->port_mtu_attr.attr.name = info->dev_mtu_name;
  1527. if (mlx4_is_mfunc(dev))
  1528. info->port_mtu_attr.attr.mode = S_IRUGO;
  1529. else {
  1530. info->port_mtu_attr.attr.mode = S_IRUGO | S_IWUSR;
  1531. info->port_mtu_attr.store = set_port_ib_mtu;
  1532. }
  1533. info->port_mtu_attr.show = show_port_ib_mtu;
  1534. sysfs_attr_init(&info->port_mtu_attr.attr);
  1535. err = device_create_file(&dev->pdev->dev, &info->port_mtu_attr);
  1536. if (err) {
  1537. mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
  1538. device_remove_file(&info->dev->pdev->dev, &info->port_attr);
  1539. info->port = -1;
  1540. }
  1541. return err;
  1542. }
  1543. static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
  1544. {
  1545. if (info->port < 0)
  1546. return;
  1547. device_remove_file(&info->dev->pdev->dev, &info->port_attr);
  1548. device_remove_file(&info->dev->pdev->dev, &info->port_mtu_attr);
  1549. }
  1550. static int mlx4_init_steering(struct mlx4_dev *dev)
  1551. {
  1552. struct mlx4_priv *priv = mlx4_priv(dev);
  1553. int num_entries = dev->caps.num_ports;
  1554. int i, j;
  1555. priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
  1556. if (!priv->steer)
  1557. return -ENOMEM;
  1558. for (i = 0; i < num_entries; i++)
  1559. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  1560. INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
  1561. INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
  1562. }
  1563. return 0;
  1564. }
  1565. static void mlx4_clear_steering(struct mlx4_dev *dev)
  1566. {
  1567. struct mlx4_priv *priv = mlx4_priv(dev);
  1568. struct mlx4_steer_index *entry, *tmp_entry;
  1569. struct mlx4_promisc_qp *pqp, *tmp_pqp;
  1570. int num_entries = dev->caps.num_ports;
  1571. int i, j;
  1572. for (i = 0; i < num_entries; i++) {
  1573. for (j = 0; j < MLX4_NUM_STEERS; j++) {
  1574. list_for_each_entry_safe(pqp, tmp_pqp,
  1575. &priv->steer[i].promisc_qps[j],
  1576. list) {
  1577. list_del(&pqp->list);
  1578. kfree(pqp);
  1579. }
  1580. list_for_each_entry_safe(entry, tmp_entry,
  1581. &priv->steer[i].steer_entries[j],
  1582. list) {
  1583. list_del(&entry->list);
  1584. list_for_each_entry_safe(pqp, tmp_pqp,
  1585. &entry->duplicates,
  1586. list) {
  1587. list_del(&pqp->list);
  1588. kfree(pqp);
  1589. }
  1590. kfree(entry);
  1591. }
  1592. }
  1593. }
  1594. kfree(priv->steer);
  1595. }
  1596. static int extended_func_num(struct pci_dev *pdev)
  1597. {
  1598. return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
  1599. }
  1600. #define MLX4_OWNER_BASE 0x8069c
  1601. #define MLX4_OWNER_SIZE 4
  1602. static int mlx4_get_ownership(struct mlx4_dev *dev)
  1603. {
  1604. void __iomem *owner;
  1605. u32 ret;
  1606. if (pci_channel_offline(dev->pdev))
  1607. return -EIO;
  1608. owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
  1609. MLX4_OWNER_SIZE);
  1610. if (!owner) {
  1611. mlx4_err(dev, "Failed to obtain ownership bit\n");
  1612. return -ENOMEM;
  1613. }
  1614. ret = readl(owner);
  1615. iounmap(owner);
  1616. return (int) !!ret;
  1617. }
  1618. static void mlx4_free_ownership(struct mlx4_dev *dev)
  1619. {
  1620. void __iomem *owner;
  1621. if (pci_channel_offline(dev->pdev))
  1622. return;
  1623. owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
  1624. MLX4_OWNER_SIZE);
  1625. if (!owner) {
  1626. mlx4_err(dev, "Failed to obtain ownership bit\n");
  1627. return;
  1628. }
  1629. writel(0, owner);
  1630. msleep(1000);
  1631. iounmap(owner);
  1632. }
  1633. static int __mlx4_init_one(struct pci_dev *pdev, int pci_dev_data)
  1634. {
  1635. struct mlx4_priv *priv;
  1636. struct mlx4_dev *dev;
  1637. int err;
  1638. int port;
  1639. pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
  1640. err = pci_enable_device(pdev);
  1641. if (err) {
  1642. dev_err(&pdev->dev, "Cannot enable PCI device, "
  1643. "aborting.\n");
  1644. return err;
  1645. }
  1646. if (num_vfs > MLX4_MAX_NUM_VF) {
  1647. printk(KERN_ERR "There are more VF's (%d) than allowed(%d)\n",
  1648. num_vfs, MLX4_MAX_NUM_VF);
  1649. return -EINVAL;
  1650. }
  1651. /*
  1652. * Check for BARs.
  1653. */
  1654. if (!(pci_dev_data & MLX4_PCI_DEV_IS_VF) &&
  1655. !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1656. dev_err(&pdev->dev, "Missing DCS, aborting."
  1657. "(driver_data: 0x%x, pci_resource_flags(pdev, 0):0x%lx)\n",
  1658. pci_dev_data, pci_resource_flags(pdev, 0));
  1659. err = -ENODEV;
  1660. goto err_disable_pdev;
  1661. }
  1662. if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  1663. dev_err(&pdev->dev, "Missing UAR, aborting.\n");
  1664. err = -ENODEV;
  1665. goto err_disable_pdev;
  1666. }
  1667. err = pci_request_regions(pdev, DRV_NAME);
  1668. if (err) {
  1669. dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
  1670. goto err_disable_pdev;
  1671. }
  1672. pci_set_master(pdev);
  1673. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1674. if (err) {
  1675. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
  1676. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1677. if (err) {
  1678. dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
  1679. goto err_release_regions;
  1680. }
  1681. }
  1682. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1683. if (err) {
  1684. dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
  1685. "consistent PCI DMA mask.\n");
  1686. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1687. if (err) {
  1688. dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
  1689. "aborting.\n");
  1690. goto err_release_regions;
  1691. }
  1692. }
  1693. /* Allow large DMA segments, up to the firmware limit of 1 GB */
  1694. dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
  1695. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  1696. if (!priv) {
  1697. dev_err(&pdev->dev, "Device struct alloc failed, "
  1698. "aborting.\n");
  1699. err = -ENOMEM;
  1700. goto err_release_regions;
  1701. }
  1702. dev = &priv->dev;
  1703. dev->pdev = pdev;
  1704. INIT_LIST_HEAD(&priv->ctx_list);
  1705. spin_lock_init(&priv->ctx_lock);
  1706. mutex_init(&priv->port_mutex);
  1707. INIT_LIST_HEAD(&priv->pgdir_list);
  1708. mutex_init(&priv->pgdir_mutex);
  1709. INIT_LIST_HEAD(&priv->bf_list);
  1710. mutex_init(&priv->bf_mutex);
  1711. dev->rev_id = pdev->revision;
  1712. /* Detect if this device is a virtual function */
  1713. if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
  1714. /* When acting as pf, we normally skip vfs unless explicitly
  1715. * requested to probe them. */
  1716. if (num_vfs && extended_func_num(pdev) > probe_vf) {
  1717. mlx4_warn(dev, "Skipping virtual function:%d\n",
  1718. extended_func_num(pdev));
  1719. err = -ENODEV;
  1720. goto err_free_dev;
  1721. }
  1722. mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
  1723. dev->flags |= MLX4_FLAG_SLAVE;
  1724. } else {
  1725. /* We reset the device and enable SRIOV only for physical
  1726. * devices. Try to claim ownership on the device;
  1727. * if already taken, skip -- do not allow multiple PFs */
  1728. err = mlx4_get_ownership(dev);
  1729. if (err) {
  1730. if (err < 0)
  1731. goto err_free_dev;
  1732. else {
  1733. mlx4_warn(dev, "Multiple PFs not yet supported."
  1734. " Skipping PF.\n");
  1735. err = -EINVAL;
  1736. goto err_free_dev;
  1737. }
  1738. }
  1739. if (num_vfs) {
  1740. mlx4_warn(dev, "Enabling SR-IOV with %d VFs\n", num_vfs);
  1741. err = pci_enable_sriov(pdev, num_vfs);
  1742. if (err) {
  1743. mlx4_err(dev, "Failed to enable SR-IOV, continuing without SR-IOV (err = %d).\n",
  1744. err);
  1745. err = 0;
  1746. } else {
  1747. mlx4_warn(dev, "Running in master mode\n");
  1748. dev->flags |= MLX4_FLAG_SRIOV |
  1749. MLX4_FLAG_MASTER;
  1750. dev->num_vfs = num_vfs;
  1751. }
  1752. }
  1753. /*
  1754. * Now reset the HCA before we touch the PCI capabilities or
  1755. * attempt a firmware command, since a boot ROM may have left
  1756. * the HCA in an undefined state.
  1757. */
  1758. err = mlx4_reset(dev);
  1759. if (err) {
  1760. mlx4_err(dev, "Failed to reset HCA, aborting.\n");
  1761. goto err_rel_own;
  1762. }
  1763. }
  1764. slave_start:
  1765. err = mlx4_cmd_init(dev);
  1766. if (err) {
  1767. mlx4_err(dev, "Failed to init command interface, aborting.\n");
  1768. goto err_sriov;
  1769. }
  1770. /* In slave functions, the communication channel must be initialized
  1771. * before posting commands. Also, init num_slaves before calling
  1772. * mlx4_init_hca */
  1773. if (mlx4_is_mfunc(dev)) {
  1774. if (mlx4_is_master(dev))
  1775. dev->num_slaves = MLX4_MAX_NUM_SLAVES;
  1776. else {
  1777. dev->num_slaves = 0;
  1778. if (mlx4_multi_func_init(dev)) {
  1779. mlx4_err(dev, "Failed to init slave mfunc"
  1780. " interface, aborting.\n");
  1781. goto err_cmd;
  1782. }
  1783. }
  1784. }
  1785. err = mlx4_init_hca(dev);
  1786. if (err) {
  1787. if (err == -EACCES) {
  1788. /* Not primary Physical function
  1789. * Running in slave mode */
  1790. mlx4_cmd_cleanup(dev);
  1791. dev->flags |= MLX4_FLAG_SLAVE;
  1792. dev->flags &= ~MLX4_FLAG_MASTER;
  1793. goto slave_start;
  1794. } else
  1795. goto err_mfunc;
  1796. }
  1797. /* In master functions, the communication channel must be initialized
  1798. * after obtaining its address from fw */
  1799. if (mlx4_is_master(dev)) {
  1800. if (mlx4_multi_func_init(dev)) {
  1801. mlx4_err(dev, "Failed to init master mfunc"
  1802. "interface, aborting.\n");
  1803. goto err_close;
  1804. }
  1805. }
  1806. err = mlx4_alloc_eq_table(dev);
  1807. if (err)
  1808. goto err_master_mfunc;
  1809. priv->msix_ctl.pool_bm = 0;
  1810. mutex_init(&priv->msix_ctl.pool_lock);
  1811. mlx4_enable_msi_x(dev);
  1812. if ((mlx4_is_mfunc(dev)) &&
  1813. !(dev->flags & MLX4_FLAG_MSI_X)) {
  1814. mlx4_err(dev, "INTx is not supported in multi-function mode."
  1815. " aborting.\n");
  1816. goto err_free_eq;
  1817. }
  1818. if (!mlx4_is_slave(dev)) {
  1819. err = mlx4_init_steering(dev);
  1820. if (err)
  1821. goto err_free_eq;
  1822. }
  1823. err = mlx4_setup_hca(dev);
  1824. if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
  1825. !mlx4_is_mfunc(dev)) {
  1826. dev->flags &= ~MLX4_FLAG_MSI_X;
  1827. dev->caps.num_comp_vectors = 1;
  1828. dev->caps.comp_pool = 0;
  1829. pci_disable_msix(pdev);
  1830. err = mlx4_setup_hca(dev);
  1831. }
  1832. if (err)
  1833. goto err_steer;
  1834. for (port = 1; port <= dev->caps.num_ports; port++) {
  1835. err = mlx4_init_port_info(dev, port);
  1836. if (err)
  1837. goto err_port;
  1838. }
  1839. err = mlx4_register_device(dev);
  1840. if (err)
  1841. goto err_port;
  1842. mlx4_sense_init(dev);
  1843. mlx4_start_sense(dev);
  1844. priv->pci_dev_data = pci_dev_data;
  1845. pci_set_drvdata(pdev, dev);
  1846. return 0;
  1847. err_port:
  1848. for (--port; port >= 1; --port)
  1849. mlx4_cleanup_port_info(&priv->port[port]);
  1850. mlx4_cleanup_counters_table(dev);
  1851. mlx4_cleanup_mcg_table(dev);
  1852. mlx4_cleanup_qp_table(dev);
  1853. mlx4_cleanup_srq_table(dev);
  1854. mlx4_cleanup_cq_table(dev);
  1855. mlx4_cmd_use_polling(dev);
  1856. mlx4_cleanup_eq_table(dev);
  1857. mlx4_cleanup_mr_table(dev);
  1858. mlx4_cleanup_xrcd_table(dev);
  1859. mlx4_cleanup_pd_table(dev);
  1860. mlx4_cleanup_uar_table(dev);
  1861. err_steer:
  1862. if (!mlx4_is_slave(dev))
  1863. mlx4_clear_steering(dev);
  1864. err_free_eq:
  1865. mlx4_free_eq_table(dev);
  1866. err_master_mfunc:
  1867. if (mlx4_is_master(dev))
  1868. mlx4_multi_func_cleanup(dev);
  1869. err_close:
  1870. if (dev->flags & MLX4_FLAG_MSI_X)
  1871. pci_disable_msix(pdev);
  1872. mlx4_close_hca(dev);
  1873. err_mfunc:
  1874. if (mlx4_is_slave(dev))
  1875. mlx4_multi_func_cleanup(dev);
  1876. err_cmd:
  1877. mlx4_cmd_cleanup(dev);
  1878. err_sriov:
  1879. if (dev->flags & MLX4_FLAG_SRIOV)
  1880. pci_disable_sriov(pdev);
  1881. err_rel_own:
  1882. if (!mlx4_is_slave(dev))
  1883. mlx4_free_ownership(dev);
  1884. err_free_dev:
  1885. kfree(priv);
  1886. err_release_regions:
  1887. pci_release_regions(pdev);
  1888. err_disable_pdev:
  1889. pci_disable_device(pdev);
  1890. pci_set_drvdata(pdev, NULL);
  1891. return err;
  1892. }
  1893. static int mlx4_init_one(struct pci_dev *pdev,
  1894. const struct pci_device_id *id)
  1895. {
  1896. printk_once(KERN_INFO "%s", mlx4_version);
  1897. return __mlx4_init_one(pdev, id->driver_data);
  1898. }
  1899. static void mlx4_remove_one(struct pci_dev *pdev)
  1900. {
  1901. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  1902. struct mlx4_priv *priv = mlx4_priv(dev);
  1903. int p;
  1904. if (dev) {
  1905. /* in SRIOV it is not allowed to unload the pf's
  1906. * driver while there are alive vf's */
  1907. if (mlx4_is_master(dev)) {
  1908. if (mlx4_how_many_lives_vf(dev))
  1909. printk(KERN_ERR "Removing PF when there are assigned VF's !!!\n");
  1910. }
  1911. mlx4_stop_sense(dev);
  1912. mlx4_unregister_device(dev);
  1913. for (p = 1; p <= dev->caps.num_ports; p++) {
  1914. mlx4_cleanup_port_info(&priv->port[p]);
  1915. mlx4_CLOSE_PORT(dev, p);
  1916. }
  1917. if (mlx4_is_master(dev))
  1918. mlx4_free_resource_tracker(dev,
  1919. RES_TR_FREE_SLAVES_ONLY);
  1920. mlx4_cleanup_counters_table(dev);
  1921. mlx4_cleanup_mcg_table(dev);
  1922. mlx4_cleanup_qp_table(dev);
  1923. mlx4_cleanup_srq_table(dev);
  1924. mlx4_cleanup_cq_table(dev);
  1925. mlx4_cmd_use_polling(dev);
  1926. mlx4_cleanup_eq_table(dev);
  1927. mlx4_cleanup_mr_table(dev);
  1928. mlx4_cleanup_xrcd_table(dev);
  1929. mlx4_cleanup_pd_table(dev);
  1930. if (mlx4_is_master(dev))
  1931. mlx4_free_resource_tracker(dev,
  1932. RES_TR_FREE_STRUCTS_ONLY);
  1933. iounmap(priv->kar);
  1934. mlx4_uar_free(dev, &priv->driver_uar);
  1935. mlx4_cleanup_uar_table(dev);
  1936. if (!mlx4_is_slave(dev))
  1937. mlx4_clear_steering(dev);
  1938. mlx4_free_eq_table(dev);
  1939. if (mlx4_is_master(dev))
  1940. mlx4_multi_func_cleanup(dev);
  1941. mlx4_close_hca(dev);
  1942. if (mlx4_is_slave(dev))
  1943. mlx4_multi_func_cleanup(dev);
  1944. mlx4_cmd_cleanup(dev);
  1945. if (dev->flags & MLX4_FLAG_MSI_X)
  1946. pci_disable_msix(pdev);
  1947. if (dev->flags & MLX4_FLAG_SRIOV) {
  1948. mlx4_warn(dev, "Disabling SR-IOV\n");
  1949. pci_disable_sriov(pdev);
  1950. }
  1951. if (!mlx4_is_slave(dev))
  1952. mlx4_free_ownership(dev);
  1953. kfree(dev->caps.qp0_tunnel);
  1954. kfree(dev->caps.qp0_proxy);
  1955. kfree(dev->caps.qp1_tunnel);
  1956. kfree(dev->caps.qp1_proxy);
  1957. kfree(priv);
  1958. pci_release_regions(pdev);
  1959. pci_disable_device(pdev);
  1960. pci_set_drvdata(pdev, NULL);
  1961. }
  1962. }
  1963. int mlx4_restart_one(struct pci_dev *pdev)
  1964. {
  1965. struct mlx4_dev *dev = pci_get_drvdata(pdev);
  1966. struct mlx4_priv *priv = mlx4_priv(dev);
  1967. int pci_dev_data;
  1968. pci_dev_data = priv->pci_dev_data;
  1969. mlx4_remove_one(pdev);
  1970. return __mlx4_init_one(pdev, pci_dev_data);
  1971. }
  1972. static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
  1973. /* MT25408 "Hermon" SDR */
  1974. { PCI_VDEVICE(MELLANOX, 0x6340), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1975. /* MT25408 "Hermon" DDR */
  1976. { PCI_VDEVICE(MELLANOX, 0x634a), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1977. /* MT25408 "Hermon" QDR */
  1978. { PCI_VDEVICE(MELLANOX, 0x6354), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1979. /* MT25408 "Hermon" DDR PCIe gen2 */
  1980. { PCI_VDEVICE(MELLANOX, 0x6732), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1981. /* MT25408 "Hermon" QDR PCIe gen2 */
  1982. { PCI_VDEVICE(MELLANOX, 0x673c), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1983. /* MT25408 "Hermon" EN 10GigE */
  1984. { PCI_VDEVICE(MELLANOX, 0x6368), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1985. /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
  1986. { PCI_VDEVICE(MELLANOX, 0x6750), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1987. /* MT25458 ConnectX EN 10GBASE-T 10GigE */
  1988. { PCI_VDEVICE(MELLANOX, 0x6372), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1989. /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
  1990. { PCI_VDEVICE(MELLANOX, 0x675a), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1991. /* MT26468 ConnectX EN 10GigE PCIe gen2*/
  1992. { PCI_VDEVICE(MELLANOX, 0x6764), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1993. /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
  1994. { PCI_VDEVICE(MELLANOX, 0x6746), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1995. /* MT26478 ConnectX2 40GigE PCIe gen2 */
  1996. { PCI_VDEVICE(MELLANOX, 0x676e), MLX4_PCI_DEV_FORCE_SENSE_PORT },
  1997. /* MT25400 Family [ConnectX-2 Virtual Function] */
  1998. { PCI_VDEVICE(MELLANOX, 0x1002), MLX4_PCI_DEV_IS_VF },
  1999. /* MT27500 Family [ConnectX-3] */
  2000. { PCI_VDEVICE(MELLANOX, 0x1003), 0 },
  2001. /* MT27500 Family [ConnectX-3 Virtual Function] */
  2002. { PCI_VDEVICE(MELLANOX, 0x1004), MLX4_PCI_DEV_IS_VF },
  2003. { PCI_VDEVICE(MELLANOX, 0x1005), 0 }, /* MT27510 Family */
  2004. { PCI_VDEVICE(MELLANOX, 0x1006), 0 }, /* MT27511 Family */
  2005. { PCI_VDEVICE(MELLANOX, 0x1007), 0 }, /* MT27520 Family */
  2006. { PCI_VDEVICE(MELLANOX, 0x1008), 0 }, /* MT27521 Family */
  2007. { PCI_VDEVICE(MELLANOX, 0x1009), 0 }, /* MT27530 Family */
  2008. { PCI_VDEVICE(MELLANOX, 0x100a), 0 }, /* MT27531 Family */
  2009. { PCI_VDEVICE(MELLANOX, 0x100b), 0 }, /* MT27540 Family */
  2010. { PCI_VDEVICE(MELLANOX, 0x100c), 0 }, /* MT27541 Family */
  2011. { PCI_VDEVICE(MELLANOX, 0x100d), 0 }, /* MT27550 Family */
  2012. { PCI_VDEVICE(MELLANOX, 0x100e), 0 }, /* MT27551 Family */
  2013. { PCI_VDEVICE(MELLANOX, 0x100f), 0 }, /* MT27560 Family */
  2014. { PCI_VDEVICE(MELLANOX, 0x1010), 0 }, /* MT27561 Family */
  2015. { 0, }
  2016. };
  2017. MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
  2018. static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
  2019. pci_channel_state_t state)
  2020. {
  2021. mlx4_remove_one(pdev);
  2022. return state == pci_channel_io_perm_failure ?
  2023. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  2024. }
  2025. static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
  2026. {
  2027. int ret = __mlx4_init_one(pdev, 0);
  2028. return ret ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
  2029. }
  2030. static const struct pci_error_handlers mlx4_err_handler = {
  2031. .error_detected = mlx4_pci_err_detected,
  2032. .slot_reset = mlx4_pci_slot_reset,
  2033. };
  2034. static struct pci_driver mlx4_driver = {
  2035. .name = DRV_NAME,
  2036. .id_table = mlx4_pci_table,
  2037. .probe = mlx4_init_one,
  2038. .remove = mlx4_remove_one,
  2039. .err_handler = &mlx4_err_handler,
  2040. };
  2041. static int __init mlx4_verify_params(void)
  2042. {
  2043. if ((log_num_mac < 0) || (log_num_mac > 7)) {
  2044. pr_warning("mlx4_core: bad num_mac: %d\n", log_num_mac);
  2045. return -1;
  2046. }
  2047. if (log_num_vlan != 0)
  2048. pr_warning("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
  2049. MLX4_LOG_NUM_VLANS);
  2050. if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
  2051. pr_warning("mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
  2052. return -1;
  2053. }
  2054. /* Check if module param for ports type has legal combination */
  2055. if (port_type_array[0] == false && port_type_array[1] == true) {
  2056. printk(KERN_WARNING "Module parameter configuration ETH/IB is not supported. Switching to default configuration IB/IB\n");
  2057. port_type_array[0] = true;
  2058. }
  2059. return 0;
  2060. }
  2061. static int __init mlx4_init(void)
  2062. {
  2063. int ret;
  2064. if (mlx4_verify_params())
  2065. return -EINVAL;
  2066. mlx4_catas_init();
  2067. mlx4_wq = create_singlethread_workqueue("mlx4");
  2068. if (!mlx4_wq)
  2069. return -ENOMEM;
  2070. ret = pci_register_driver(&mlx4_driver);
  2071. return ret < 0 ? ret : 0;
  2072. }
  2073. static void __exit mlx4_cleanup(void)
  2074. {
  2075. pci_unregister_driver(&mlx4_driver);
  2076. destroy_workqueue(mlx4_wq);
  2077. }
  2078. module_init(mlx4_init);
  2079. module_exit(mlx4_cleanup);