main.c 57 KB

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