main.c 66 KB

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