main.c 57 KB

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