main.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/errno.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/inetdevice.h>
  39. #include <linux/rtnetlink.h>
  40. #include <linux/if_vlan.h>
  41. #include <net/ipv6.h>
  42. #include <net/addrconf.h>
  43. #include <rdma/ib_smi.h>
  44. #include <rdma/ib_user_verbs.h>
  45. #include <rdma/ib_addr.h>
  46. #include <linux/mlx4/driver.h>
  47. #include <linux/mlx4/cmd.h>
  48. #include <linux/mlx4/qp.h>
  49. #include "mlx4_ib.h"
  50. #include "user.h"
  51. #define DRV_NAME MLX4_IB_DRV_NAME
  52. #define DRV_VERSION "2.2-1"
  53. #define DRV_RELDATE "Feb 2014"
  54. #define MLX4_IB_FLOW_MAX_PRIO 0xFFF
  55. #define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
  56. #define MLX4_IB_CARD_REV_A0 0xA0
  57. MODULE_AUTHOR("Roland Dreier");
  58. MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
  59. MODULE_LICENSE("Dual BSD/GPL");
  60. MODULE_VERSION(DRV_VERSION);
  61. int mlx4_ib_sm_guid_assign = 1;
  62. module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
  63. MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 1)");
  64. static const char mlx4_ib_version[] =
  65. DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
  66. DRV_VERSION " (" DRV_RELDATE ")\n";
  67. struct update_gid_work {
  68. struct work_struct work;
  69. union ib_gid gids[128];
  70. struct mlx4_ib_dev *dev;
  71. int port;
  72. };
  73. static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
  74. static struct workqueue_struct *wq;
  75. static void init_query_mad(struct ib_smp *mad)
  76. {
  77. mad->base_version = 1;
  78. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  79. mad->class_version = 1;
  80. mad->method = IB_MGMT_METHOD_GET;
  81. }
  82. static union ib_gid zgid;
  83. static int check_flow_steering_support(struct mlx4_dev *dev)
  84. {
  85. int eth_num_ports = 0;
  86. int ib_num_ports = 0;
  87. int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
  88. if (dmfs) {
  89. int i;
  90. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
  91. eth_num_ports++;
  92. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  93. ib_num_ports++;
  94. dmfs &= (!ib_num_ports ||
  95. (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB)) &&
  96. (!eth_num_ports ||
  97. (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN));
  98. if (ib_num_ports && mlx4_is_mfunc(dev)) {
  99. pr_warn("Device managed flow steering is unavailable for IB port in multifunction env.\n");
  100. dmfs = 0;
  101. }
  102. }
  103. return dmfs;
  104. }
  105. static int num_ib_ports(struct mlx4_dev *dev)
  106. {
  107. int ib_ports = 0;
  108. int i;
  109. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  110. ib_ports++;
  111. return ib_ports;
  112. }
  113. static int mlx4_ib_query_device(struct ib_device *ibdev,
  114. struct ib_device_attr *props)
  115. {
  116. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  117. struct ib_smp *in_mad = NULL;
  118. struct ib_smp *out_mad = NULL;
  119. int err = -ENOMEM;
  120. int have_ib_ports;
  121. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  122. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  123. if (!in_mad || !out_mad)
  124. goto out;
  125. init_query_mad(in_mad);
  126. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  127. err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
  128. 1, NULL, NULL, in_mad, out_mad);
  129. if (err)
  130. goto out;
  131. memset(props, 0, sizeof *props);
  132. have_ib_ports = num_ib_ports(dev->dev);
  133. props->fw_ver = dev->dev->caps.fw_ver;
  134. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  135. IB_DEVICE_PORT_ACTIVE_EVENT |
  136. IB_DEVICE_SYS_IMAGE_GUID |
  137. IB_DEVICE_RC_RNR_NAK_GEN |
  138. IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  139. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
  140. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  141. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
  142. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  143. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM && have_ib_ports)
  144. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  145. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
  146. props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  147. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
  148. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  149. if (dev->dev->caps.max_gso_sz &&
  150. (dev->dev->rev_id != MLX4_IB_CARD_REV_A0) &&
  151. (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH))
  152. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  153. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
  154. props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
  155. if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
  156. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
  157. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
  158. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  159. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
  160. props->device_cap_flags |= IB_DEVICE_XRC;
  161. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)
  162. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW;
  163. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
  164. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_WIN_TYPE_2B)
  165. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2B;
  166. else
  167. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2A;
  168. if (dev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
  169. props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
  170. }
  171. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  172. 0xffffff;
  173. props->vendor_part_id = dev->dev->persist->pdev->device;
  174. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  175. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  176. props->max_mr_size = ~0ull;
  177. props->page_size_cap = dev->dev->caps.page_size_cap;
  178. props->max_qp = dev->dev->quotas.qp;
  179. props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
  180. props->max_sge = min(dev->dev->caps.max_sq_sg,
  181. dev->dev->caps.max_rq_sg);
  182. props->max_cq = dev->dev->quotas.cq;
  183. props->max_cqe = dev->dev->caps.max_cqes;
  184. props->max_mr = dev->dev->quotas.mpt;
  185. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  186. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  187. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  188. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  189. props->max_srq = dev->dev->quotas.srq;
  190. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  191. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  192. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  193. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  194. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  195. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  196. props->masked_atomic_cap = props->atomic_cap;
  197. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  198. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  199. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  200. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  201. props->max_mcast_grp;
  202. props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
  203. out:
  204. kfree(in_mad);
  205. kfree(out_mad);
  206. return err;
  207. }
  208. static enum rdma_link_layer
  209. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  210. {
  211. struct mlx4_dev *dev = to_mdev(device)->dev;
  212. return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
  213. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  214. }
  215. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  216. struct ib_port_attr *props, int netw_view)
  217. {
  218. struct ib_smp *in_mad = NULL;
  219. struct ib_smp *out_mad = NULL;
  220. int ext_active_speed;
  221. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  222. int err = -ENOMEM;
  223. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  224. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  225. if (!in_mad || !out_mad)
  226. goto out;
  227. init_query_mad(in_mad);
  228. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  229. in_mad->attr_mod = cpu_to_be32(port);
  230. if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
  231. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  232. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  233. in_mad, out_mad);
  234. if (err)
  235. goto out;
  236. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  237. props->lmc = out_mad->data[34] & 0x7;
  238. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  239. props->sm_sl = out_mad->data[36] & 0xf;
  240. props->state = out_mad->data[32] & 0xf;
  241. props->phys_state = out_mad->data[33] >> 4;
  242. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  243. if (netw_view)
  244. props->gid_tbl_len = out_mad->data[50];
  245. else
  246. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  247. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  248. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  249. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  250. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  251. props->active_width = out_mad->data[31] & 0xf;
  252. props->active_speed = out_mad->data[35] >> 4;
  253. props->max_mtu = out_mad->data[41] & 0xf;
  254. props->active_mtu = out_mad->data[36] >> 4;
  255. props->subnet_timeout = out_mad->data[51] & 0x1f;
  256. props->max_vl_num = out_mad->data[37] >> 4;
  257. props->init_type_reply = out_mad->data[41] >> 4;
  258. /* Check if extended speeds (EDR/FDR/...) are supported */
  259. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  260. ext_active_speed = out_mad->data[62] >> 4;
  261. switch (ext_active_speed) {
  262. case 1:
  263. props->active_speed = IB_SPEED_FDR;
  264. break;
  265. case 2:
  266. props->active_speed = IB_SPEED_EDR;
  267. break;
  268. }
  269. }
  270. /* If reported active speed is QDR, check if is FDR-10 */
  271. if (props->active_speed == IB_SPEED_QDR) {
  272. init_query_mad(in_mad);
  273. in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
  274. in_mad->attr_mod = cpu_to_be32(port);
  275. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
  276. NULL, NULL, in_mad, out_mad);
  277. if (err)
  278. goto out;
  279. /* Checking LinkSpeedActive for FDR-10 */
  280. if (out_mad->data[15] & 0x1)
  281. props->active_speed = IB_SPEED_FDR10;
  282. }
  283. /* Avoid wrong speed value returned by FW if the IB link is down. */
  284. if (props->state == IB_PORT_DOWN)
  285. props->active_speed = IB_SPEED_SDR;
  286. out:
  287. kfree(in_mad);
  288. kfree(out_mad);
  289. return err;
  290. }
  291. static u8 state_to_phys_state(enum ib_port_state state)
  292. {
  293. return state == IB_PORT_ACTIVE ? 5 : 3;
  294. }
  295. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  296. struct ib_port_attr *props, int netw_view)
  297. {
  298. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  299. struct mlx4_ib_iboe *iboe = &mdev->iboe;
  300. struct net_device *ndev;
  301. enum ib_mtu tmp;
  302. struct mlx4_cmd_mailbox *mailbox;
  303. int err = 0;
  304. int is_bonded = mlx4_is_bonded(mdev->dev);
  305. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  306. if (IS_ERR(mailbox))
  307. return PTR_ERR(mailbox);
  308. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
  309. MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
  310. MLX4_CMD_WRAPPED);
  311. if (err)
  312. goto out;
  313. props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ?
  314. IB_WIDTH_4X : IB_WIDTH_1X;
  315. props->active_speed = IB_SPEED_QDR;
  316. props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_IP_BASED_GIDS;
  317. props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
  318. props->max_msg_sz = mdev->dev->caps.max_msg_sz;
  319. props->pkey_tbl_len = 1;
  320. props->max_mtu = IB_MTU_4096;
  321. props->max_vl_num = 2;
  322. props->state = IB_PORT_DOWN;
  323. props->phys_state = state_to_phys_state(props->state);
  324. props->active_mtu = IB_MTU_256;
  325. if (is_bonded)
  326. rtnl_lock(); /* required to get upper dev */
  327. spin_lock_bh(&iboe->lock);
  328. ndev = iboe->netdevs[port - 1];
  329. if (ndev && is_bonded)
  330. ndev = netdev_master_upper_dev_get(ndev);
  331. if (!ndev)
  332. goto out_unlock;
  333. tmp = iboe_get_mtu(ndev->mtu);
  334. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  335. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  336. IB_PORT_ACTIVE : IB_PORT_DOWN;
  337. props->phys_state = state_to_phys_state(props->state);
  338. out_unlock:
  339. spin_unlock_bh(&iboe->lock);
  340. if (is_bonded)
  341. rtnl_unlock();
  342. out:
  343. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  344. return err;
  345. }
  346. int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  347. struct ib_port_attr *props, int netw_view)
  348. {
  349. int err;
  350. memset(props, 0, sizeof *props);
  351. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  352. ib_link_query_port(ibdev, port, props, netw_view) :
  353. eth_link_query_port(ibdev, port, props, netw_view);
  354. return err;
  355. }
  356. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  357. struct ib_port_attr *props)
  358. {
  359. /* returns host view */
  360. return __mlx4_ib_query_port(ibdev, port, props, 0);
  361. }
  362. int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  363. union ib_gid *gid, int netw_view)
  364. {
  365. struct ib_smp *in_mad = NULL;
  366. struct ib_smp *out_mad = NULL;
  367. int err = -ENOMEM;
  368. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  369. int clear = 0;
  370. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  371. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  372. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  373. if (!in_mad || !out_mad)
  374. goto out;
  375. init_query_mad(in_mad);
  376. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  377. in_mad->attr_mod = cpu_to_be32(port);
  378. if (mlx4_is_mfunc(dev->dev) && netw_view)
  379. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  380. err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
  381. if (err)
  382. goto out;
  383. memcpy(gid->raw, out_mad->data + 8, 8);
  384. if (mlx4_is_mfunc(dev->dev) && !netw_view) {
  385. if (index) {
  386. /* For any index > 0, return the null guid */
  387. err = 0;
  388. clear = 1;
  389. goto out;
  390. }
  391. }
  392. init_query_mad(in_mad);
  393. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  394. in_mad->attr_mod = cpu_to_be32(index / 8);
  395. err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
  396. NULL, NULL, in_mad, out_mad);
  397. if (err)
  398. goto out;
  399. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  400. out:
  401. if (clear)
  402. memset(gid->raw + 8, 0, 8);
  403. kfree(in_mad);
  404. kfree(out_mad);
  405. return err;
  406. }
  407. static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
  408. union ib_gid *gid)
  409. {
  410. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  411. *gid = dev->iboe.gid_table[port - 1][index];
  412. return 0;
  413. }
  414. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  415. union ib_gid *gid)
  416. {
  417. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  418. return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
  419. else
  420. return iboe_query_gid(ibdev, port, index, gid);
  421. }
  422. int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  423. u16 *pkey, int netw_view)
  424. {
  425. struct ib_smp *in_mad = NULL;
  426. struct ib_smp *out_mad = NULL;
  427. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  428. int err = -ENOMEM;
  429. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  430. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  431. if (!in_mad || !out_mad)
  432. goto out;
  433. init_query_mad(in_mad);
  434. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  435. in_mad->attr_mod = cpu_to_be32(index / 32);
  436. if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
  437. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  438. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  439. in_mad, out_mad);
  440. if (err)
  441. goto out;
  442. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  443. out:
  444. kfree(in_mad);
  445. kfree(out_mad);
  446. return err;
  447. }
  448. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
  449. {
  450. return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
  451. }
  452. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  453. struct ib_device_modify *props)
  454. {
  455. struct mlx4_cmd_mailbox *mailbox;
  456. unsigned long flags;
  457. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  458. return -EOPNOTSUPP;
  459. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  460. return 0;
  461. if (mlx4_is_slave(to_mdev(ibdev)->dev))
  462. return -EOPNOTSUPP;
  463. spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
  464. memcpy(ibdev->node_desc, props->node_desc, 64);
  465. spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
  466. /*
  467. * If possible, pass node desc to FW, so it can generate
  468. * a 144 trap. If cmd fails, just ignore.
  469. */
  470. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  471. if (IS_ERR(mailbox))
  472. return 0;
  473. memcpy(mailbox->buf, props->node_desc, 64);
  474. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  475. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  476. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  477. return 0;
  478. }
  479. static int mlx4_ib_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  480. u32 cap_mask)
  481. {
  482. struct mlx4_cmd_mailbox *mailbox;
  483. int err;
  484. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  485. if (IS_ERR(mailbox))
  486. return PTR_ERR(mailbox);
  487. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  488. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  489. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  490. } else {
  491. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  492. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  493. }
  494. err = mlx4_cmd(dev->dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
  495. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  496. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  497. return err;
  498. }
  499. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  500. struct ib_port_modify *props)
  501. {
  502. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  503. u8 is_eth = mdev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  504. struct ib_port_attr attr;
  505. u32 cap_mask;
  506. int err;
  507. /* return OK if this is RoCE. CM calls ib_modify_port() regardless
  508. * of whether port link layer is ETH or IB. For ETH ports, qkey
  509. * violations and port capabilities are not meaningful.
  510. */
  511. if (is_eth)
  512. return 0;
  513. mutex_lock(&mdev->cap_mask_mutex);
  514. err = mlx4_ib_query_port(ibdev, port, &attr);
  515. if (err)
  516. goto out;
  517. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  518. ~props->clr_port_cap_mask;
  519. err = mlx4_ib_SET_PORT(mdev, port,
  520. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  521. cap_mask);
  522. out:
  523. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  524. return err;
  525. }
  526. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  527. struct ib_udata *udata)
  528. {
  529. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  530. struct mlx4_ib_ucontext *context;
  531. struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
  532. struct mlx4_ib_alloc_ucontext_resp resp;
  533. int err;
  534. if (!dev->ib_active)
  535. return ERR_PTR(-EAGAIN);
  536. if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
  537. resp_v3.qp_tab_size = dev->dev->caps.num_qps;
  538. resp_v3.bf_reg_size = dev->dev->caps.bf_reg_size;
  539. resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  540. } else {
  541. resp.dev_caps = dev->dev->caps.userspace_caps;
  542. resp.qp_tab_size = dev->dev->caps.num_qps;
  543. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  544. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  545. resp.cqe_size = dev->dev->caps.cqe_size;
  546. }
  547. context = kmalloc(sizeof *context, GFP_KERNEL);
  548. if (!context)
  549. return ERR_PTR(-ENOMEM);
  550. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  551. if (err) {
  552. kfree(context);
  553. return ERR_PTR(err);
  554. }
  555. INIT_LIST_HEAD(&context->db_page_list);
  556. mutex_init(&context->db_page_mutex);
  557. if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
  558. err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
  559. else
  560. err = ib_copy_to_udata(udata, &resp, sizeof(resp));
  561. if (err) {
  562. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  563. kfree(context);
  564. return ERR_PTR(-EFAULT);
  565. }
  566. return &context->ibucontext;
  567. }
  568. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  569. {
  570. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  571. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  572. kfree(context);
  573. return 0;
  574. }
  575. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  576. {
  577. struct mlx4_ib_dev *dev = to_mdev(context->device);
  578. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  579. return -EINVAL;
  580. if (vma->vm_pgoff == 0) {
  581. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  582. if (io_remap_pfn_range(vma, vma->vm_start,
  583. to_mucontext(context)->uar.pfn,
  584. PAGE_SIZE, vma->vm_page_prot))
  585. return -EAGAIN;
  586. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  587. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  588. if (io_remap_pfn_range(vma, vma->vm_start,
  589. to_mucontext(context)->uar.pfn +
  590. dev->dev->caps.num_uars,
  591. PAGE_SIZE, vma->vm_page_prot))
  592. return -EAGAIN;
  593. } else
  594. return -EINVAL;
  595. return 0;
  596. }
  597. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  598. struct ib_ucontext *context,
  599. struct ib_udata *udata)
  600. {
  601. struct mlx4_ib_pd *pd;
  602. int err;
  603. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  604. if (!pd)
  605. return ERR_PTR(-ENOMEM);
  606. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  607. if (err) {
  608. kfree(pd);
  609. return ERR_PTR(err);
  610. }
  611. if (context)
  612. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  613. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  614. kfree(pd);
  615. return ERR_PTR(-EFAULT);
  616. }
  617. return &pd->ibpd;
  618. }
  619. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  620. {
  621. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  622. kfree(pd);
  623. return 0;
  624. }
  625. static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
  626. struct ib_ucontext *context,
  627. struct ib_udata *udata)
  628. {
  629. struct mlx4_ib_xrcd *xrcd;
  630. int err;
  631. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  632. return ERR_PTR(-ENOSYS);
  633. xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
  634. if (!xrcd)
  635. return ERR_PTR(-ENOMEM);
  636. err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
  637. if (err)
  638. goto err1;
  639. xrcd->pd = ib_alloc_pd(ibdev);
  640. if (IS_ERR(xrcd->pd)) {
  641. err = PTR_ERR(xrcd->pd);
  642. goto err2;
  643. }
  644. xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
  645. if (IS_ERR(xrcd->cq)) {
  646. err = PTR_ERR(xrcd->cq);
  647. goto err3;
  648. }
  649. return &xrcd->ibxrcd;
  650. err3:
  651. ib_dealloc_pd(xrcd->pd);
  652. err2:
  653. mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
  654. err1:
  655. kfree(xrcd);
  656. return ERR_PTR(err);
  657. }
  658. static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  659. {
  660. ib_destroy_cq(to_mxrcd(xrcd)->cq);
  661. ib_dealloc_pd(to_mxrcd(xrcd)->pd);
  662. mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
  663. kfree(xrcd);
  664. return 0;
  665. }
  666. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  667. {
  668. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  669. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  670. struct mlx4_ib_gid_entry *ge;
  671. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  672. if (!ge)
  673. return -ENOMEM;
  674. ge->gid = *gid;
  675. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  676. ge->port = mqp->port;
  677. ge->added = 1;
  678. }
  679. mutex_lock(&mqp->mutex);
  680. list_add_tail(&ge->list, &mqp->gid_list);
  681. mutex_unlock(&mqp->mutex);
  682. return 0;
  683. }
  684. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  685. union ib_gid *gid)
  686. {
  687. struct net_device *ndev;
  688. int ret = 0;
  689. if (!mqp->port)
  690. return 0;
  691. spin_lock_bh(&mdev->iboe.lock);
  692. ndev = mdev->iboe.netdevs[mqp->port - 1];
  693. if (ndev)
  694. dev_hold(ndev);
  695. spin_unlock_bh(&mdev->iboe.lock);
  696. if (ndev) {
  697. ret = 1;
  698. dev_put(ndev);
  699. }
  700. return ret;
  701. }
  702. struct mlx4_ib_steering {
  703. struct list_head list;
  704. struct mlx4_flow_reg_id reg_id;
  705. union ib_gid gid;
  706. };
  707. static int parse_flow_attr(struct mlx4_dev *dev,
  708. u32 qp_num,
  709. union ib_flow_spec *ib_spec,
  710. struct _rule_hw *mlx4_spec)
  711. {
  712. enum mlx4_net_trans_rule_id type;
  713. switch (ib_spec->type) {
  714. case IB_FLOW_SPEC_ETH:
  715. type = MLX4_NET_TRANS_RULE_ID_ETH;
  716. memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
  717. ETH_ALEN);
  718. memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
  719. ETH_ALEN);
  720. mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
  721. mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
  722. break;
  723. case IB_FLOW_SPEC_IB:
  724. type = MLX4_NET_TRANS_RULE_ID_IB;
  725. mlx4_spec->ib.l3_qpn =
  726. cpu_to_be32(qp_num);
  727. mlx4_spec->ib.qpn_mask =
  728. cpu_to_be32(MLX4_IB_FLOW_QPN_MASK);
  729. break;
  730. case IB_FLOW_SPEC_IPV4:
  731. type = MLX4_NET_TRANS_RULE_ID_IPV4;
  732. mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
  733. mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
  734. mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
  735. mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
  736. break;
  737. case IB_FLOW_SPEC_TCP:
  738. case IB_FLOW_SPEC_UDP:
  739. type = ib_spec->type == IB_FLOW_SPEC_TCP ?
  740. MLX4_NET_TRANS_RULE_ID_TCP :
  741. MLX4_NET_TRANS_RULE_ID_UDP;
  742. mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
  743. mlx4_spec->tcp_udp.dst_port_msk = ib_spec->tcp_udp.mask.dst_port;
  744. mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
  745. mlx4_spec->tcp_udp.src_port_msk = ib_spec->tcp_udp.mask.src_port;
  746. break;
  747. default:
  748. return -EINVAL;
  749. }
  750. if (mlx4_map_sw_to_hw_steering_id(dev, type) < 0 ||
  751. mlx4_hw_rule_sz(dev, type) < 0)
  752. return -EINVAL;
  753. mlx4_spec->id = cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev, type));
  754. mlx4_spec->size = mlx4_hw_rule_sz(dev, type) >> 2;
  755. return mlx4_hw_rule_sz(dev, type);
  756. }
  757. struct default_rules {
  758. __u32 mandatory_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
  759. __u32 mandatory_not_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
  760. __u32 rules_create_list[IB_FLOW_SPEC_SUPPORT_LAYERS];
  761. __u8 link_layer;
  762. };
  763. static const struct default_rules default_table[] = {
  764. {
  765. .mandatory_fields = {IB_FLOW_SPEC_IPV4},
  766. .mandatory_not_fields = {IB_FLOW_SPEC_ETH},
  767. .rules_create_list = {IB_FLOW_SPEC_IB},
  768. .link_layer = IB_LINK_LAYER_INFINIBAND
  769. }
  770. };
  771. static int __mlx4_ib_default_rules_match(struct ib_qp *qp,
  772. struct ib_flow_attr *flow_attr)
  773. {
  774. int i, j, k;
  775. void *ib_flow;
  776. const struct default_rules *pdefault_rules = default_table;
  777. u8 link_layer = rdma_port_get_link_layer(qp->device, flow_attr->port);
  778. for (i = 0; i < ARRAY_SIZE(default_table); i++, pdefault_rules++) {
  779. __u32 field_types[IB_FLOW_SPEC_SUPPORT_LAYERS];
  780. memset(&field_types, 0, sizeof(field_types));
  781. if (link_layer != pdefault_rules->link_layer)
  782. continue;
  783. ib_flow = flow_attr + 1;
  784. /* we assume the specs are sorted */
  785. for (j = 0, k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS &&
  786. j < flow_attr->num_of_specs; k++) {
  787. union ib_flow_spec *current_flow =
  788. (union ib_flow_spec *)ib_flow;
  789. /* same layer but different type */
  790. if (((current_flow->type & IB_FLOW_SPEC_LAYER_MASK) ==
  791. (pdefault_rules->mandatory_fields[k] &
  792. IB_FLOW_SPEC_LAYER_MASK)) &&
  793. (current_flow->type !=
  794. pdefault_rules->mandatory_fields[k]))
  795. goto out;
  796. /* same layer, try match next one */
  797. if (current_flow->type ==
  798. pdefault_rules->mandatory_fields[k]) {
  799. j++;
  800. ib_flow +=
  801. ((union ib_flow_spec *)ib_flow)->size;
  802. }
  803. }
  804. ib_flow = flow_attr + 1;
  805. for (j = 0; j < flow_attr->num_of_specs;
  806. j++, ib_flow += ((union ib_flow_spec *)ib_flow)->size)
  807. for (k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS; k++)
  808. /* same layer and same type */
  809. if (((union ib_flow_spec *)ib_flow)->type ==
  810. pdefault_rules->mandatory_not_fields[k])
  811. goto out;
  812. return i;
  813. }
  814. out:
  815. return -1;
  816. }
  817. static int __mlx4_ib_create_default_rules(
  818. struct mlx4_ib_dev *mdev,
  819. struct ib_qp *qp,
  820. const struct default_rules *pdefault_rules,
  821. struct _rule_hw *mlx4_spec) {
  822. int size = 0;
  823. int i;
  824. for (i = 0; i < ARRAY_SIZE(pdefault_rules->rules_create_list); i++) {
  825. int ret;
  826. union ib_flow_spec ib_spec;
  827. switch (pdefault_rules->rules_create_list[i]) {
  828. case 0:
  829. /* no rule */
  830. continue;
  831. case IB_FLOW_SPEC_IB:
  832. ib_spec.type = IB_FLOW_SPEC_IB;
  833. ib_spec.size = sizeof(struct ib_flow_spec_ib);
  834. break;
  835. default:
  836. /* invalid rule */
  837. return -EINVAL;
  838. }
  839. /* We must put empty rule, qpn is being ignored */
  840. ret = parse_flow_attr(mdev->dev, 0, &ib_spec,
  841. mlx4_spec);
  842. if (ret < 0) {
  843. pr_info("invalid parsing\n");
  844. return -EINVAL;
  845. }
  846. mlx4_spec = (void *)mlx4_spec + ret;
  847. size += ret;
  848. }
  849. return size;
  850. }
  851. static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
  852. int domain,
  853. enum mlx4_net_trans_promisc_mode flow_type,
  854. u64 *reg_id)
  855. {
  856. int ret, i;
  857. int size = 0;
  858. void *ib_flow;
  859. struct mlx4_ib_dev *mdev = to_mdev(qp->device);
  860. struct mlx4_cmd_mailbox *mailbox;
  861. struct mlx4_net_trans_rule_hw_ctrl *ctrl;
  862. int default_flow;
  863. static const u16 __mlx4_domain[] = {
  864. [IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
  865. [IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
  866. [IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
  867. [IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
  868. };
  869. if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
  870. pr_err("Invalid priority value %d\n", flow_attr->priority);
  871. return -EINVAL;
  872. }
  873. if (domain >= IB_FLOW_DOMAIN_NUM) {
  874. pr_err("Invalid domain value %d\n", domain);
  875. return -EINVAL;
  876. }
  877. if (mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
  878. return -EINVAL;
  879. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  880. if (IS_ERR(mailbox))
  881. return PTR_ERR(mailbox);
  882. ctrl = mailbox->buf;
  883. ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
  884. flow_attr->priority);
  885. ctrl->type = mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type);
  886. ctrl->port = flow_attr->port;
  887. ctrl->qpn = cpu_to_be32(qp->qp_num);
  888. ib_flow = flow_attr + 1;
  889. size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
  890. /* Add default flows */
  891. default_flow = __mlx4_ib_default_rules_match(qp, flow_attr);
  892. if (default_flow >= 0) {
  893. ret = __mlx4_ib_create_default_rules(
  894. mdev, qp, default_table + default_flow,
  895. mailbox->buf + size);
  896. if (ret < 0) {
  897. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  898. return -EINVAL;
  899. }
  900. size += ret;
  901. }
  902. for (i = 0; i < flow_attr->num_of_specs; i++) {
  903. ret = parse_flow_attr(mdev->dev, qp->qp_num, ib_flow,
  904. mailbox->buf + size);
  905. if (ret < 0) {
  906. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  907. return -EINVAL;
  908. }
  909. ib_flow += ((union ib_flow_spec *) ib_flow)->size;
  910. size += ret;
  911. }
  912. ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
  913. MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
  914. MLX4_CMD_NATIVE);
  915. if (ret == -ENOMEM)
  916. pr_err("mcg table is full. Fail to register network rule.\n");
  917. else if (ret == -ENXIO)
  918. pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
  919. else if (ret)
  920. pr_err("Invalid argumant. Fail to register network rule.\n");
  921. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  922. return ret;
  923. }
  924. static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
  925. {
  926. int err;
  927. err = mlx4_cmd(dev, reg_id, 0, 0,
  928. MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
  929. MLX4_CMD_NATIVE);
  930. if (err)
  931. pr_err("Fail to detach network rule. registration id = 0x%llx\n",
  932. reg_id);
  933. return err;
  934. }
  935. static int mlx4_ib_tunnel_steer_add(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
  936. u64 *reg_id)
  937. {
  938. void *ib_flow;
  939. union ib_flow_spec *ib_spec;
  940. struct mlx4_dev *dev = to_mdev(qp->device)->dev;
  941. int err = 0;
  942. if (dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
  943. dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
  944. return 0; /* do nothing */
  945. ib_flow = flow_attr + 1;
  946. ib_spec = (union ib_flow_spec *)ib_flow;
  947. if (ib_spec->type != IB_FLOW_SPEC_ETH || flow_attr->num_of_specs != 1)
  948. return 0; /* do nothing */
  949. err = mlx4_tunnel_steer_add(to_mdev(qp->device)->dev, ib_spec->eth.val.dst_mac,
  950. flow_attr->port, qp->qp_num,
  951. MLX4_DOMAIN_UVERBS | (flow_attr->priority & 0xff),
  952. reg_id);
  953. return err;
  954. }
  955. static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
  956. struct ib_flow_attr *flow_attr,
  957. int domain)
  958. {
  959. int err = 0, i = 0, j = 0;
  960. struct mlx4_ib_flow *mflow;
  961. enum mlx4_net_trans_promisc_mode type[2];
  962. struct mlx4_dev *dev = (to_mdev(qp->device))->dev;
  963. int is_bonded = mlx4_is_bonded(dev);
  964. memset(type, 0, sizeof(type));
  965. mflow = kzalloc(sizeof(*mflow), GFP_KERNEL);
  966. if (!mflow) {
  967. err = -ENOMEM;
  968. goto err_free;
  969. }
  970. switch (flow_attr->type) {
  971. case IB_FLOW_ATTR_NORMAL:
  972. type[0] = MLX4_FS_REGULAR;
  973. break;
  974. case IB_FLOW_ATTR_ALL_DEFAULT:
  975. type[0] = MLX4_FS_ALL_DEFAULT;
  976. break;
  977. case IB_FLOW_ATTR_MC_DEFAULT:
  978. type[0] = MLX4_FS_MC_DEFAULT;
  979. break;
  980. case IB_FLOW_ATTR_SNIFFER:
  981. type[0] = MLX4_FS_UC_SNIFFER;
  982. type[1] = MLX4_FS_MC_SNIFFER;
  983. break;
  984. default:
  985. err = -EINVAL;
  986. goto err_free;
  987. }
  988. while (i < ARRAY_SIZE(type) && type[i]) {
  989. err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
  990. &mflow->reg_id[i].id);
  991. if (err)
  992. goto err_create_flow;
  993. i++;
  994. if (is_bonded) {
  995. /* Application always sees one port so the mirror rule
  996. * must be on port #2
  997. */
  998. flow_attr->port = 2;
  999. err = __mlx4_ib_create_flow(qp, flow_attr,
  1000. domain, type[j],
  1001. &mflow->reg_id[j].mirror);
  1002. flow_attr->port = 1;
  1003. if (err)
  1004. goto err_create_flow;
  1005. j++;
  1006. }
  1007. }
  1008. if (i < ARRAY_SIZE(type) && flow_attr->type == IB_FLOW_ATTR_NORMAL) {
  1009. err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
  1010. &mflow->reg_id[i].id);
  1011. if (err)
  1012. goto err_create_flow;
  1013. i++;
  1014. if (is_bonded) {
  1015. flow_attr->port = 2;
  1016. err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
  1017. &mflow->reg_id[j].mirror);
  1018. flow_attr->port = 1;
  1019. if (err)
  1020. goto err_create_flow;
  1021. j++;
  1022. }
  1023. /* function to create mirror rule */
  1024. }
  1025. return &mflow->ibflow;
  1026. err_create_flow:
  1027. while (i) {
  1028. (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
  1029. mflow->reg_id[i].id);
  1030. i--;
  1031. }
  1032. while (j) {
  1033. (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
  1034. mflow->reg_id[j].mirror);
  1035. j--;
  1036. }
  1037. err_free:
  1038. kfree(mflow);
  1039. return ERR_PTR(err);
  1040. }
  1041. static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
  1042. {
  1043. int err, ret = 0;
  1044. int i = 0;
  1045. struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
  1046. struct mlx4_ib_flow *mflow = to_mflow(flow_id);
  1047. while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i].id) {
  1048. err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i].id);
  1049. if (err)
  1050. ret = err;
  1051. if (mflow->reg_id[i].mirror) {
  1052. err = __mlx4_ib_destroy_flow(mdev->dev,
  1053. mflow->reg_id[i].mirror);
  1054. if (err)
  1055. ret = err;
  1056. }
  1057. i++;
  1058. }
  1059. kfree(mflow);
  1060. return ret;
  1061. }
  1062. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  1063. {
  1064. int err;
  1065. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  1066. struct mlx4_dev *dev = mdev->dev;
  1067. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1068. struct mlx4_ib_steering *ib_steering = NULL;
  1069. enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
  1070. struct mlx4_flow_reg_id reg_id;
  1071. if (mdev->dev->caps.steering_mode ==
  1072. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1073. ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
  1074. if (!ib_steering)
  1075. return -ENOMEM;
  1076. }
  1077. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
  1078. !!(mqp->flags &
  1079. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  1080. prot, &reg_id.id);
  1081. if (err) {
  1082. pr_err("multicast attach op failed, err %d\n", err);
  1083. goto err_malloc;
  1084. }
  1085. reg_id.mirror = 0;
  1086. if (mlx4_is_bonded(dev)) {
  1087. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
  1088. (mqp->port == 1) ? 2 : 1,
  1089. !!(mqp->flags &
  1090. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  1091. prot, &reg_id.mirror);
  1092. if (err)
  1093. goto err_add;
  1094. }
  1095. err = add_gid_entry(ibqp, gid);
  1096. if (err)
  1097. goto err_add;
  1098. if (ib_steering) {
  1099. memcpy(ib_steering->gid.raw, gid->raw, 16);
  1100. ib_steering->reg_id = reg_id;
  1101. mutex_lock(&mqp->mutex);
  1102. list_add(&ib_steering->list, &mqp->steering_rules);
  1103. mutex_unlock(&mqp->mutex);
  1104. }
  1105. return 0;
  1106. err_add:
  1107. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1108. prot, reg_id.id);
  1109. if (reg_id.mirror)
  1110. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1111. prot, reg_id.mirror);
  1112. err_malloc:
  1113. kfree(ib_steering);
  1114. return err;
  1115. }
  1116. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  1117. {
  1118. struct mlx4_ib_gid_entry *ge;
  1119. struct mlx4_ib_gid_entry *tmp;
  1120. struct mlx4_ib_gid_entry *ret = NULL;
  1121. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1122. if (!memcmp(raw, ge->gid.raw, 16)) {
  1123. ret = ge;
  1124. break;
  1125. }
  1126. }
  1127. return ret;
  1128. }
  1129. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  1130. {
  1131. int err;
  1132. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  1133. struct mlx4_dev *dev = mdev->dev;
  1134. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1135. struct net_device *ndev;
  1136. struct mlx4_ib_gid_entry *ge;
  1137. struct mlx4_flow_reg_id reg_id = {0, 0};
  1138. enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
  1139. if (mdev->dev->caps.steering_mode ==
  1140. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1141. struct mlx4_ib_steering *ib_steering;
  1142. mutex_lock(&mqp->mutex);
  1143. list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
  1144. if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
  1145. list_del(&ib_steering->list);
  1146. break;
  1147. }
  1148. }
  1149. mutex_unlock(&mqp->mutex);
  1150. if (&ib_steering->list == &mqp->steering_rules) {
  1151. pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
  1152. return -EINVAL;
  1153. }
  1154. reg_id = ib_steering->reg_id;
  1155. kfree(ib_steering);
  1156. }
  1157. err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1158. prot, reg_id.id);
  1159. if (err)
  1160. return err;
  1161. if (mlx4_is_bonded(dev)) {
  1162. err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1163. prot, reg_id.mirror);
  1164. if (err)
  1165. return err;
  1166. }
  1167. mutex_lock(&mqp->mutex);
  1168. ge = find_gid_entry(mqp, gid->raw);
  1169. if (ge) {
  1170. spin_lock_bh(&mdev->iboe.lock);
  1171. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  1172. if (ndev)
  1173. dev_hold(ndev);
  1174. spin_unlock_bh(&mdev->iboe.lock);
  1175. if (ndev)
  1176. dev_put(ndev);
  1177. list_del(&ge->list);
  1178. kfree(ge);
  1179. } else
  1180. pr_warn("could not find mgid entry\n");
  1181. mutex_unlock(&mqp->mutex);
  1182. return 0;
  1183. }
  1184. static int init_node_data(struct mlx4_ib_dev *dev)
  1185. {
  1186. struct ib_smp *in_mad = NULL;
  1187. struct ib_smp *out_mad = NULL;
  1188. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  1189. int err = -ENOMEM;
  1190. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  1191. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  1192. if (!in_mad || !out_mad)
  1193. goto out;
  1194. init_query_mad(in_mad);
  1195. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  1196. if (mlx4_is_master(dev->dev))
  1197. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  1198. err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
  1199. if (err)
  1200. goto out;
  1201. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  1202. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  1203. err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
  1204. if (err)
  1205. goto out;
  1206. dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  1207. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  1208. out:
  1209. kfree(in_mad);
  1210. kfree(out_mad);
  1211. return err;
  1212. }
  1213. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  1214. char *buf)
  1215. {
  1216. struct mlx4_ib_dev *dev =
  1217. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1218. return sprintf(buf, "MT%d\n", dev->dev->persist->pdev->device);
  1219. }
  1220. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  1221. char *buf)
  1222. {
  1223. struct mlx4_ib_dev *dev =
  1224. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1225. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  1226. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  1227. (int) dev->dev->caps.fw_ver & 0xffff);
  1228. }
  1229. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  1230. char *buf)
  1231. {
  1232. struct mlx4_ib_dev *dev =
  1233. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1234. return sprintf(buf, "%x\n", dev->dev->rev_id);
  1235. }
  1236. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  1237. char *buf)
  1238. {
  1239. struct mlx4_ib_dev *dev =
  1240. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1241. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  1242. dev->dev->board_id);
  1243. }
  1244. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  1245. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  1246. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  1247. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  1248. static struct device_attribute *mlx4_class_attributes[] = {
  1249. &dev_attr_hw_rev,
  1250. &dev_attr_fw_ver,
  1251. &dev_attr_hca_type,
  1252. &dev_attr_board_id
  1253. };
  1254. static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id,
  1255. struct net_device *dev)
  1256. {
  1257. memcpy(eui, dev->dev_addr, 3);
  1258. memcpy(eui + 5, dev->dev_addr + 3, 3);
  1259. if (vlan_id < 0x1000) {
  1260. eui[3] = vlan_id >> 8;
  1261. eui[4] = vlan_id & 0xff;
  1262. } else {
  1263. eui[3] = 0xff;
  1264. eui[4] = 0xfe;
  1265. }
  1266. eui[0] ^= 2;
  1267. }
  1268. static void update_gids_task(struct work_struct *work)
  1269. {
  1270. struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
  1271. struct mlx4_cmd_mailbox *mailbox;
  1272. union ib_gid *gids;
  1273. int err;
  1274. struct mlx4_dev *dev = gw->dev->dev;
  1275. int is_bonded = mlx4_is_bonded(dev);
  1276. if (!gw->dev->ib_active)
  1277. return;
  1278. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1279. if (IS_ERR(mailbox)) {
  1280. pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
  1281. return;
  1282. }
  1283. gids = mailbox->buf;
  1284. memcpy(gids, gw->gids, sizeof gw->gids);
  1285. err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  1286. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  1287. MLX4_CMD_WRAPPED);
  1288. if (err)
  1289. pr_warn("set port command failed\n");
  1290. else
  1291. if ((gw->port == 1) || !is_bonded)
  1292. mlx4_ib_dispatch_event(gw->dev,
  1293. is_bonded ? 1 : gw->port,
  1294. IB_EVENT_GID_CHANGE);
  1295. mlx4_free_cmd_mailbox(dev, mailbox);
  1296. kfree(gw);
  1297. }
  1298. static void reset_gids_task(struct work_struct *work)
  1299. {
  1300. struct update_gid_work *gw =
  1301. container_of(work, struct update_gid_work, work);
  1302. struct mlx4_cmd_mailbox *mailbox;
  1303. union ib_gid *gids;
  1304. int err;
  1305. struct mlx4_dev *dev = gw->dev->dev;
  1306. if (!gw->dev->ib_active)
  1307. return;
  1308. mailbox = mlx4_alloc_cmd_mailbox(dev);
  1309. if (IS_ERR(mailbox)) {
  1310. pr_warn("reset gid table failed\n");
  1311. goto free;
  1312. }
  1313. gids = mailbox->buf;
  1314. memcpy(gids, gw->gids, sizeof(gw->gids));
  1315. if (mlx4_ib_port_link_layer(&gw->dev->ib_dev, gw->port) ==
  1316. IB_LINK_LAYER_ETHERNET) {
  1317. err = mlx4_cmd(dev, mailbox->dma,
  1318. MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  1319. 1, MLX4_CMD_SET_PORT,
  1320. MLX4_CMD_TIME_CLASS_B,
  1321. MLX4_CMD_WRAPPED);
  1322. if (err)
  1323. pr_warn(KERN_WARNING
  1324. "set port %d command failed\n", gw->port);
  1325. }
  1326. mlx4_free_cmd_mailbox(dev, mailbox);
  1327. free:
  1328. kfree(gw);
  1329. }
  1330. static int update_gid_table(struct mlx4_ib_dev *dev, int port,
  1331. union ib_gid *gid, int clear,
  1332. int default_gid)
  1333. {
  1334. struct update_gid_work *work;
  1335. int i;
  1336. int need_update = 0;
  1337. int free = -1;
  1338. int found = -1;
  1339. int max_gids;
  1340. if (default_gid) {
  1341. free = 0;
  1342. } else {
  1343. max_gids = dev->dev->caps.gid_table_len[port];
  1344. for (i = 1; i < max_gids; ++i) {
  1345. if (!memcmp(&dev->iboe.gid_table[port - 1][i], gid,
  1346. sizeof(*gid)))
  1347. found = i;
  1348. if (clear) {
  1349. if (found >= 0) {
  1350. need_update = 1;
  1351. dev->iboe.gid_table[port - 1][found] =
  1352. zgid;
  1353. break;
  1354. }
  1355. } else {
  1356. if (found >= 0)
  1357. break;
  1358. if (free < 0 &&
  1359. !memcmp(&dev->iboe.gid_table[port - 1][i],
  1360. &zgid, sizeof(*gid)))
  1361. free = i;
  1362. }
  1363. }
  1364. }
  1365. if (found == -1 && !clear && free >= 0) {
  1366. dev->iboe.gid_table[port - 1][free] = *gid;
  1367. need_update = 1;
  1368. }
  1369. if (!need_update)
  1370. return 0;
  1371. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  1372. if (!work)
  1373. return -ENOMEM;
  1374. memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof(work->gids));
  1375. INIT_WORK(&work->work, update_gids_task);
  1376. work->port = port;
  1377. work->dev = dev;
  1378. queue_work(wq, &work->work);
  1379. return 0;
  1380. }
  1381. static void mlx4_make_default_gid(struct net_device *dev, union ib_gid *gid)
  1382. {
  1383. gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  1384. mlx4_addrconf_ifid_eui48(&gid->raw[8], 0xffff, dev);
  1385. }
  1386. static int reset_gid_table(struct mlx4_ib_dev *dev, u8 port)
  1387. {
  1388. struct update_gid_work *work;
  1389. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  1390. if (!work)
  1391. return -ENOMEM;
  1392. memset(dev->iboe.gid_table[port - 1], 0, sizeof(work->gids));
  1393. memset(work->gids, 0, sizeof(work->gids));
  1394. INIT_WORK(&work->work, reset_gids_task);
  1395. work->dev = dev;
  1396. work->port = port;
  1397. queue_work(wq, &work->work);
  1398. return 0;
  1399. }
  1400. static int mlx4_ib_addr_event(int event, struct net_device *event_netdev,
  1401. struct mlx4_ib_dev *ibdev, union ib_gid *gid)
  1402. {
  1403. struct mlx4_ib_iboe *iboe;
  1404. int port = 0;
  1405. struct net_device *real_dev = rdma_vlan_dev_real_dev(event_netdev) ?
  1406. rdma_vlan_dev_real_dev(event_netdev) :
  1407. event_netdev;
  1408. union ib_gid default_gid;
  1409. mlx4_make_default_gid(real_dev, &default_gid);
  1410. if (!memcmp(gid, &default_gid, sizeof(*gid)))
  1411. return 0;
  1412. if (event != NETDEV_DOWN && event != NETDEV_UP)
  1413. return 0;
  1414. if ((real_dev != event_netdev) &&
  1415. (event == NETDEV_DOWN) &&
  1416. rdma_link_local_addr((struct in6_addr *)gid))
  1417. return 0;
  1418. iboe = &ibdev->iboe;
  1419. spin_lock_bh(&iboe->lock);
  1420. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port)
  1421. if ((netif_is_bond_master(real_dev) &&
  1422. (real_dev == iboe->masters[port - 1])) ||
  1423. (!netif_is_bond_master(real_dev) &&
  1424. (real_dev == iboe->netdevs[port - 1])))
  1425. update_gid_table(ibdev, port, gid,
  1426. event == NETDEV_DOWN, 0);
  1427. spin_unlock_bh(&iboe->lock);
  1428. return 0;
  1429. }
  1430. static u8 mlx4_ib_get_dev_port(struct net_device *dev,
  1431. struct mlx4_ib_dev *ibdev)
  1432. {
  1433. u8 port = 0;
  1434. struct mlx4_ib_iboe *iboe;
  1435. struct net_device *real_dev = rdma_vlan_dev_real_dev(dev) ?
  1436. rdma_vlan_dev_real_dev(dev) : dev;
  1437. iboe = &ibdev->iboe;
  1438. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port)
  1439. if ((netif_is_bond_master(real_dev) &&
  1440. (real_dev == iboe->masters[port - 1])) ||
  1441. (!netif_is_bond_master(real_dev) &&
  1442. (real_dev == iboe->netdevs[port - 1])))
  1443. break;
  1444. if ((port == 0) || (port > ibdev->dev->caps.num_ports))
  1445. return 0;
  1446. else
  1447. return port;
  1448. }
  1449. static int mlx4_ib_inet_event(struct notifier_block *this, unsigned long event,
  1450. void *ptr)
  1451. {
  1452. struct mlx4_ib_dev *ibdev;
  1453. struct in_ifaddr *ifa = ptr;
  1454. union ib_gid gid;
  1455. struct net_device *event_netdev = ifa->ifa_dev->dev;
  1456. ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
  1457. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb_inet);
  1458. mlx4_ib_addr_event(event, event_netdev, ibdev, &gid);
  1459. return NOTIFY_DONE;
  1460. }
  1461. #if IS_ENABLED(CONFIG_IPV6)
  1462. static int mlx4_ib_inet6_event(struct notifier_block *this, unsigned long event,
  1463. void *ptr)
  1464. {
  1465. struct mlx4_ib_dev *ibdev;
  1466. struct inet6_ifaddr *ifa = ptr;
  1467. union ib_gid *gid = (union ib_gid *)&ifa->addr;
  1468. struct net_device *event_netdev = ifa->idev->dev;
  1469. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb_inet6);
  1470. mlx4_ib_addr_event(event, event_netdev, ibdev, gid);
  1471. return NOTIFY_DONE;
  1472. }
  1473. #endif
  1474. #define MLX4_IB_INVALID_MAC ((u64)-1)
  1475. static void mlx4_ib_update_qps(struct mlx4_ib_dev *ibdev,
  1476. struct net_device *dev,
  1477. int port)
  1478. {
  1479. u64 new_smac = 0;
  1480. u64 release_mac = MLX4_IB_INVALID_MAC;
  1481. struct mlx4_ib_qp *qp;
  1482. read_lock(&dev_base_lock);
  1483. new_smac = mlx4_mac_to_u64(dev->dev_addr);
  1484. read_unlock(&dev_base_lock);
  1485. atomic64_set(&ibdev->iboe.mac[port - 1], new_smac);
  1486. /* no need for update QP1 and mac registration in non-SRIOV */
  1487. if (!mlx4_is_mfunc(ibdev->dev))
  1488. return;
  1489. mutex_lock(&ibdev->qp1_proxy_lock[port - 1]);
  1490. qp = ibdev->qp1_proxy[port - 1];
  1491. if (qp) {
  1492. int new_smac_index;
  1493. u64 old_smac;
  1494. struct mlx4_update_qp_params update_params;
  1495. mutex_lock(&qp->mutex);
  1496. old_smac = qp->pri.smac;
  1497. if (new_smac == old_smac)
  1498. goto unlock;
  1499. new_smac_index = mlx4_register_mac(ibdev->dev, port, new_smac);
  1500. if (new_smac_index < 0)
  1501. goto unlock;
  1502. update_params.smac_index = new_smac_index;
  1503. if (mlx4_update_qp(ibdev->dev, qp->mqp.qpn, MLX4_UPDATE_QP_SMAC,
  1504. &update_params)) {
  1505. release_mac = new_smac;
  1506. goto unlock;
  1507. }
  1508. /* if old port was zero, no mac was yet registered for this QP */
  1509. if (qp->pri.smac_port)
  1510. release_mac = old_smac;
  1511. qp->pri.smac = new_smac;
  1512. qp->pri.smac_port = port;
  1513. qp->pri.smac_index = new_smac_index;
  1514. }
  1515. unlock:
  1516. if (release_mac != MLX4_IB_INVALID_MAC)
  1517. mlx4_unregister_mac(ibdev->dev, port, release_mac);
  1518. if (qp)
  1519. mutex_unlock(&qp->mutex);
  1520. mutex_unlock(&ibdev->qp1_proxy_lock[port - 1]);
  1521. }
  1522. static void mlx4_ib_get_dev_addr(struct net_device *dev,
  1523. struct mlx4_ib_dev *ibdev, u8 port)
  1524. {
  1525. struct in_device *in_dev;
  1526. #if IS_ENABLED(CONFIG_IPV6)
  1527. struct inet6_dev *in6_dev;
  1528. union ib_gid *pgid;
  1529. struct inet6_ifaddr *ifp;
  1530. union ib_gid default_gid;
  1531. #endif
  1532. union ib_gid gid;
  1533. if ((port == 0) || (port > ibdev->dev->caps.num_ports))
  1534. return;
  1535. /* IPv4 gids */
  1536. in_dev = in_dev_get(dev);
  1537. if (in_dev) {
  1538. for_ifa(in_dev) {
  1539. /*ifa->ifa_address;*/
  1540. ipv6_addr_set_v4mapped(ifa->ifa_address,
  1541. (struct in6_addr *)&gid);
  1542. update_gid_table(ibdev, port, &gid, 0, 0);
  1543. }
  1544. endfor_ifa(in_dev);
  1545. in_dev_put(in_dev);
  1546. }
  1547. #if IS_ENABLED(CONFIG_IPV6)
  1548. mlx4_make_default_gid(dev, &default_gid);
  1549. /* IPv6 gids */
  1550. in6_dev = in6_dev_get(dev);
  1551. if (in6_dev) {
  1552. read_lock_bh(&in6_dev->lock);
  1553. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  1554. pgid = (union ib_gid *)&ifp->addr;
  1555. if (!memcmp(pgid, &default_gid, sizeof(*pgid)))
  1556. continue;
  1557. update_gid_table(ibdev, port, pgid, 0, 0);
  1558. }
  1559. read_unlock_bh(&in6_dev->lock);
  1560. in6_dev_put(in6_dev);
  1561. }
  1562. #endif
  1563. }
  1564. static void mlx4_ib_set_default_gid(struct mlx4_ib_dev *ibdev,
  1565. struct net_device *dev, u8 port)
  1566. {
  1567. union ib_gid gid;
  1568. mlx4_make_default_gid(dev, &gid);
  1569. update_gid_table(ibdev, port, &gid, 0, 1);
  1570. }
  1571. static int mlx4_ib_init_gid_table(struct mlx4_ib_dev *ibdev)
  1572. {
  1573. struct net_device *dev;
  1574. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  1575. int i;
  1576. int err = 0;
  1577. for (i = 1; i <= ibdev->num_ports; ++i) {
  1578. if (rdma_port_get_link_layer(&ibdev->ib_dev, i) ==
  1579. IB_LINK_LAYER_ETHERNET) {
  1580. err = reset_gid_table(ibdev, i);
  1581. if (err)
  1582. goto out;
  1583. }
  1584. }
  1585. read_lock(&dev_base_lock);
  1586. spin_lock_bh(&iboe->lock);
  1587. for_each_netdev(&init_net, dev) {
  1588. u8 port = mlx4_ib_get_dev_port(dev, ibdev);
  1589. /* port will be non-zero only for ETH ports */
  1590. if (port) {
  1591. mlx4_ib_set_default_gid(ibdev, dev, port);
  1592. mlx4_ib_get_dev_addr(dev, ibdev, port);
  1593. }
  1594. }
  1595. spin_unlock_bh(&iboe->lock);
  1596. read_unlock(&dev_base_lock);
  1597. out:
  1598. return err;
  1599. }
  1600. static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev,
  1601. struct net_device *dev,
  1602. unsigned long event)
  1603. {
  1604. struct mlx4_ib_iboe *iboe;
  1605. int update_qps_port = -1;
  1606. int port;
  1607. iboe = &ibdev->iboe;
  1608. spin_lock_bh(&iboe->lock);
  1609. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  1610. enum ib_port_state port_state = IB_PORT_NOP;
  1611. struct net_device *old_master = iboe->masters[port - 1];
  1612. struct net_device *curr_netdev;
  1613. struct net_device *curr_master;
  1614. iboe->netdevs[port - 1] =
  1615. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
  1616. if (iboe->netdevs[port - 1])
  1617. mlx4_ib_set_default_gid(ibdev,
  1618. iboe->netdevs[port - 1], port);
  1619. curr_netdev = iboe->netdevs[port - 1];
  1620. if (iboe->netdevs[port - 1] &&
  1621. netif_is_bond_slave(iboe->netdevs[port - 1])) {
  1622. iboe->masters[port - 1] = netdev_master_upper_dev_get(
  1623. iboe->netdevs[port - 1]);
  1624. } else {
  1625. iboe->masters[port - 1] = NULL;
  1626. }
  1627. curr_master = iboe->masters[port - 1];
  1628. if (dev == iboe->netdevs[port - 1] &&
  1629. (event == NETDEV_CHANGEADDR || event == NETDEV_REGISTER ||
  1630. event == NETDEV_UP || event == NETDEV_CHANGE))
  1631. update_qps_port = port;
  1632. if (curr_netdev) {
  1633. port_state = (netif_running(curr_netdev) && netif_carrier_ok(curr_netdev)) ?
  1634. IB_PORT_ACTIVE : IB_PORT_DOWN;
  1635. mlx4_ib_set_default_gid(ibdev, curr_netdev, port);
  1636. if (curr_master) {
  1637. /* if using bonding/team and a slave port is down, we
  1638. * don't want the bond IP based gids in the table since
  1639. * flows that select port by gid may get the down port.
  1640. */
  1641. if (port_state == IB_PORT_DOWN &&
  1642. !mlx4_is_bonded(ibdev->dev)) {
  1643. reset_gid_table(ibdev, port);
  1644. mlx4_ib_set_default_gid(ibdev,
  1645. curr_netdev,
  1646. port);
  1647. } else {
  1648. /* gids from the upper dev (bond/team)
  1649. * should appear in port's gid table
  1650. */
  1651. mlx4_ib_get_dev_addr(curr_master,
  1652. ibdev, port);
  1653. }
  1654. }
  1655. /* if bonding is used it is possible that we add it to
  1656. * masters only after IP address is assigned to the
  1657. * net bonding interface.
  1658. */
  1659. if (curr_master && (old_master != curr_master)) {
  1660. reset_gid_table(ibdev, port);
  1661. mlx4_ib_set_default_gid(ibdev,
  1662. curr_netdev, port);
  1663. mlx4_ib_get_dev_addr(curr_master, ibdev, port);
  1664. }
  1665. if (!curr_master && (old_master != curr_master)) {
  1666. reset_gid_table(ibdev, port);
  1667. mlx4_ib_set_default_gid(ibdev,
  1668. curr_netdev, port);
  1669. mlx4_ib_get_dev_addr(curr_netdev, ibdev, port);
  1670. }
  1671. } else {
  1672. reset_gid_table(ibdev, port);
  1673. }
  1674. }
  1675. spin_unlock_bh(&iboe->lock);
  1676. if (update_qps_port > 0)
  1677. mlx4_ib_update_qps(ibdev, dev, update_qps_port);
  1678. }
  1679. static int mlx4_ib_netdev_event(struct notifier_block *this,
  1680. unsigned long event, void *ptr)
  1681. {
  1682. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1683. struct mlx4_ib_dev *ibdev;
  1684. if (!net_eq(dev_net(dev), &init_net))
  1685. return NOTIFY_DONE;
  1686. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  1687. mlx4_ib_scan_netdevs(ibdev, dev, event);
  1688. return NOTIFY_DONE;
  1689. }
  1690. static void init_pkeys(struct mlx4_ib_dev *ibdev)
  1691. {
  1692. int port;
  1693. int slave;
  1694. int i;
  1695. if (mlx4_is_master(ibdev->dev)) {
  1696. for (slave = 0; slave <= ibdev->dev->persist->num_vfs;
  1697. ++slave) {
  1698. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
  1699. for (i = 0;
  1700. i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
  1701. ++i) {
  1702. ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
  1703. /* master has the identity virt2phys pkey mapping */
  1704. (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
  1705. ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
  1706. mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
  1707. ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
  1708. }
  1709. }
  1710. }
  1711. /* initialize pkey cache */
  1712. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
  1713. for (i = 0;
  1714. i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
  1715. ++i)
  1716. ibdev->pkeys.phys_pkey_cache[port-1][i] =
  1717. (i) ? 0 : 0xFFFF;
  1718. }
  1719. }
  1720. }
  1721. static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  1722. {
  1723. char name[80];
  1724. int eq_per_port = 0;
  1725. int added_eqs = 0;
  1726. int total_eqs = 0;
  1727. int i, j, eq;
  1728. /* Legacy mode or comp_pool is not large enough */
  1729. if (dev->caps.comp_pool == 0 ||
  1730. dev->caps.num_ports > dev->caps.comp_pool)
  1731. return;
  1732. eq_per_port = dev->caps.comp_pool / dev->caps.num_ports;
  1733. /* Init eq table */
  1734. added_eqs = 0;
  1735. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  1736. added_eqs += eq_per_port;
  1737. total_eqs = dev->caps.num_comp_vectors + added_eqs;
  1738. ibdev->eq_table = kzalloc(total_eqs * sizeof(int), GFP_KERNEL);
  1739. if (!ibdev->eq_table)
  1740. return;
  1741. ibdev->eq_added = added_eqs;
  1742. eq = 0;
  1743. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) {
  1744. for (j = 0; j < eq_per_port; j++) {
  1745. snprintf(name, sizeof(name), "mlx4-ib-%d-%d@%s",
  1746. i, j, dev->persist->pdev->bus->name);
  1747. /* Set IRQ for specific name (per ring) */
  1748. if (mlx4_assign_eq(dev, name, NULL,
  1749. &ibdev->eq_table[eq])) {
  1750. /* Use legacy (same as mlx4_en driver) */
  1751. pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
  1752. ibdev->eq_table[eq] =
  1753. (eq % dev->caps.num_comp_vectors);
  1754. }
  1755. eq++;
  1756. }
  1757. }
  1758. /* Fill the reset of the vector with legacy EQ */
  1759. for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
  1760. ibdev->eq_table[eq++] = i;
  1761. /* Advertise the new number of EQs to clients */
  1762. ibdev->ib_dev.num_comp_vectors = total_eqs;
  1763. }
  1764. static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  1765. {
  1766. int i;
  1767. /* no additional eqs were added */
  1768. if (!ibdev->eq_table)
  1769. return;
  1770. /* Reset the advertised EQ number */
  1771. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  1772. /* Free only the added eqs */
  1773. for (i = 0; i < ibdev->eq_added; i++) {
  1774. /* Don't free legacy eqs if used */
  1775. if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
  1776. continue;
  1777. mlx4_release_eq(dev, ibdev->eq_table[i]);
  1778. }
  1779. kfree(ibdev->eq_table);
  1780. }
  1781. static void *mlx4_ib_add(struct mlx4_dev *dev)
  1782. {
  1783. struct mlx4_ib_dev *ibdev;
  1784. int num_ports = 0;
  1785. int i, j;
  1786. int err;
  1787. struct mlx4_ib_iboe *iboe;
  1788. int ib_num_ports = 0;
  1789. int num_req_counters;
  1790. pr_info_once("%s", mlx4_ib_version);
  1791. num_ports = 0;
  1792. mlx4_foreach_ib_transport_port(i, dev)
  1793. num_ports++;
  1794. /* No point in registering a device with no ports... */
  1795. if (num_ports == 0)
  1796. return NULL;
  1797. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  1798. if (!ibdev) {
  1799. dev_err(&dev->persist->pdev->dev,
  1800. "Device struct alloc failed\n");
  1801. return NULL;
  1802. }
  1803. iboe = &ibdev->iboe;
  1804. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  1805. goto err_dealloc;
  1806. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  1807. goto err_pd;
  1808. ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
  1809. PAGE_SIZE);
  1810. if (!ibdev->uar_map)
  1811. goto err_uar;
  1812. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  1813. ibdev->dev = dev;
  1814. ibdev->bond_next_port = 0;
  1815. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  1816. ibdev->ib_dev.owner = THIS_MODULE;
  1817. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  1818. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  1819. ibdev->num_ports = num_ports;
  1820. ibdev->ib_dev.phys_port_cnt = mlx4_is_bonded(dev) ?
  1821. 1 : ibdev->num_ports;
  1822. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  1823. ibdev->ib_dev.dma_device = &dev->persist->pdev->dev;
  1824. if (dev->caps.userspace_caps)
  1825. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  1826. else
  1827. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
  1828. ibdev->ib_dev.uverbs_cmd_mask =
  1829. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1830. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1831. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1832. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1833. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1834. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1835. (1ull << IB_USER_VERBS_CMD_REREG_MR) |
  1836. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1837. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1838. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1839. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1840. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1841. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1842. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1843. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1844. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1845. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1846. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1847. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1848. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1849. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1850. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  1851. (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
  1852. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  1853. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  1854. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  1855. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  1856. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  1857. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  1858. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  1859. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  1860. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  1861. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  1862. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  1863. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  1864. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  1865. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  1866. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  1867. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  1868. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  1869. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  1870. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  1871. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  1872. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  1873. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  1874. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  1875. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  1876. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  1877. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  1878. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  1879. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  1880. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  1881. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  1882. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  1883. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  1884. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  1885. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  1886. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  1887. ibdev->ib_dev.rereg_user_mr = mlx4_ib_rereg_user_mr;
  1888. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  1889. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  1890. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  1891. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  1892. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  1893. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  1894. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  1895. if (!mlx4_is_slave(ibdev->dev)) {
  1896. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  1897. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  1898. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  1899. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  1900. }
  1901. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
  1902. dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
  1903. ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
  1904. ibdev->ib_dev.bind_mw = mlx4_ib_bind_mw;
  1905. ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
  1906. ibdev->ib_dev.uverbs_cmd_mask |=
  1907. (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
  1908. (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
  1909. }
  1910. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
  1911. ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
  1912. ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
  1913. ibdev->ib_dev.uverbs_cmd_mask |=
  1914. (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
  1915. (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
  1916. }
  1917. if (check_flow_steering_support(dev)) {
  1918. ibdev->steering_support = MLX4_STEERING_MODE_DEVICE_MANAGED;
  1919. ibdev->ib_dev.create_flow = mlx4_ib_create_flow;
  1920. ibdev->ib_dev.destroy_flow = mlx4_ib_destroy_flow;
  1921. ibdev->ib_dev.uverbs_ex_cmd_mask |=
  1922. (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
  1923. (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
  1924. }
  1925. mlx4_ib_alloc_eqs(dev, ibdev);
  1926. spin_lock_init(&iboe->lock);
  1927. if (init_node_data(ibdev))
  1928. goto err_map;
  1929. num_req_counters = mlx4_is_bonded(dev) ? 1 : ibdev->num_ports;
  1930. for (i = 0; i < num_req_counters; ++i) {
  1931. mutex_init(&ibdev->qp1_proxy_lock[i]);
  1932. if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
  1933. IB_LINK_LAYER_ETHERNET) {
  1934. err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
  1935. if (err)
  1936. ibdev->counters[i] = -1;
  1937. } else {
  1938. ibdev->counters[i] = -1;
  1939. }
  1940. }
  1941. if (mlx4_is_bonded(dev))
  1942. for (i = 1; i < ibdev->num_ports ; ++i)
  1943. ibdev->counters[i] = ibdev->counters[0];
  1944. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  1945. ib_num_ports++;
  1946. spin_lock_init(&ibdev->sm_lock);
  1947. mutex_init(&ibdev->cap_mask_mutex);
  1948. INIT_LIST_HEAD(&ibdev->qp_list);
  1949. spin_lock_init(&ibdev->reset_flow_resource_lock);
  1950. if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED &&
  1951. ib_num_ports) {
  1952. ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
  1953. err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
  1954. MLX4_IB_UC_STEER_QPN_ALIGN,
  1955. &ibdev->steer_qpn_base, 0);
  1956. if (err)
  1957. goto err_counter;
  1958. ibdev->ib_uc_qpns_bitmap =
  1959. kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
  1960. sizeof(long),
  1961. GFP_KERNEL);
  1962. if (!ibdev->ib_uc_qpns_bitmap) {
  1963. dev_err(&dev->persist->pdev->dev,
  1964. "bit map alloc failed\n");
  1965. goto err_steer_qp_release;
  1966. }
  1967. bitmap_zero(ibdev->ib_uc_qpns_bitmap, ibdev->steer_qpn_count);
  1968. err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
  1969. dev, ibdev->steer_qpn_base,
  1970. ibdev->steer_qpn_base +
  1971. ibdev->steer_qpn_count - 1);
  1972. if (err)
  1973. goto err_steer_free_bitmap;
  1974. }
  1975. for (j = 1; j <= ibdev->dev->caps.num_ports; j++)
  1976. atomic64_set(&iboe->mac[j - 1], ibdev->dev->caps.def_mac[j]);
  1977. if (ib_register_device(&ibdev->ib_dev, NULL))
  1978. goto err_steer_free_bitmap;
  1979. if (mlx4_ib_mad_init(ibdev))
  1980. goto err_reg;
  1981. if (mlx4_ib_init_sriov(ibdev))
  1982. goto err_mad;
  1983. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE) {
  1984. if (!iboe->nb.notifier_call) {
  1985. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  1986. err = register_netdevice_notifier(&iboe->nb);
  1987. if (err) {
  1988. iboe->nb.notifier_call = NULL;
  1989. goto err_notif;
  1990. }
  1991. }
  1992. if (!iboe->nb_inet.notifier_call) {
  1993. iboe->nb_inet.notifier_call = mlx4_ib_inet_event;
  1994. err = register_inetaddr_notifier(&iboe->nb_inet);
  1995. if (err) {
  1996. iboe->nb_inet.notifier_call = NULL;
  1997. goto err_notif;
  1998. }
  1999. }
  2000. #if IS_ENABLED(CONFIG_IPV6)
  2001. if (!iboe->nb_inet6.notifier_call) {
  2002. iboe->nb_inet6.notifier_call = mlx4_ib_inet6_event;
  2003. err = register_inet6addr_notifier(&iboe->nb_inet6);
  2004. if (err) {
  2005. iboe->nb_inet6.notifier_call = NULL;
  2006. goto err_notif;
  2007. }
  2008. }
  2009. #endif
  2010. if (mlx4_ib_init_gid_table(ibdev))
  2011. goto err_notif;
  2012. }
  2013. for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
  2014. if (device_create_file(&ibdev->ib_dev.dev,
  2015. mlx4_class_attributes[j]))
  2016. goto err_notif;
  2017. }
  2018. ibdev->ib_active = true;
  2019. if (mlx4_is_mfunc(ibdev->dev))
  2020. init_pkeys(ibdev);
  2021. /* create paravirt contexts for any VFs which are active */
  2022. if (mlx4_is_master(ibdev->dev)) {
  2023. for (j = 0; j < MLX4_MFUNC_MAX; j++) {
  2024. if (j == mlx4_master_func_num(ibdev->dev))
  2025. continue;
  2026. if (mlx4_is_slave_active(ibdev->dev, j))
  2027. do_slave_init(ibdev, j, 1);
  2028. }
  2029. }
  2030. return ibdev;
  2031. err_notif:
  2032. if (ibdev->iboe.nb.notifier_call) {
  2033. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  2034. pr_warn("failure unregistering notifier\n");
  2035. ibdev->iboe.nb.notifier_call = NULL;
  2036. }
  2037. if (ibdev->iboe.nb_inet.notifier_call) {
  2038. if (unregister_inetaddr_notifier(&ibdev->iboe.nb_inet))
  2039. pr_warn("failure unregistering notifier\n");
  2040. ibdev->iboe.nb_inet.notifier_call = NULL;
  2041. }
  2042. #if IS_ENABLED(CONFIG_IPV6)
  2043. if (ibdev->iboe.nb_inet6.notifier_call) {
  2044. if (unregister_inet6addr_notifier(&ibdev->iboe.nb_inet6))
  2045. pr_warn("failure unregistering notifier\n");
  2046. ibdev->iboe.nb_inet6.notifier_call = NULL;
  2047. }
  2048. #endif
  2049. flush_workqueue(wq);
  2050. mlx4_ib_close_sriov(ibdev);
  2051. err_mad:
  2052. mlx4_ib_mad_cleanup(ibdev);
  2053. err_reg:
  2054. ib_unregister_device(&ibdev->ib_dev);
  2055. err_steer_free_bitmap:
  2056. kfree(ibdev->ib_uc_qpns_bitmap);
  2057. err_steer_qp_release:
  2058. if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
  2059. mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
  2060. ibdev->steer_qpn_count);
  2061. err_counter:
  2062. for (; i; --i)
  2063. if (ibdev->counters[i - 1] != -1)
  2064. mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
  2065. err_map:
  2066. iounmap(ibdev->uar_map);
  2067. err_uar:
  2068. mlx4_uar_free(dev, &ibdev->priv_uar);
  2069. err_pd:
  2070. mlx4_pd_free(dev, ibdev->priv_pdn);
  2071. err_dealloc:
  2072. ib_dealloc_device(&ibdev->ib_dev);
  2073. return NULL;
  2074. }
  2075. int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
  2076. {
  2077. int offset;
  2078. WARN_ON(!dev->ib_uc_qpns_bitmap);
  2079. offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
  2080. dev->steer_qpn_count,
  2081. get_count_order(count));
  2082. if (offset < 0)
  2083. return offset;
  2084. *qpn = dev->steer_qpn_base + offset;
  2085. return 0;
  2086. }
  2087. void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
  2088. {
  2089. if (!qpn ||
  2090. dev->steering_support != MLX4_STEERING_MODE_DEVICE_MANAGED)
  2091. return;
  2092. BUG_ON(qpn < dev->steer_qpn_base);
  2093. bitmap_release_region(dev->ib_uc_qpns_bitmap,
  2094. qpn - dev->steer_qpn_base,
  2095. get_count_order(count));
  2096. }
  2097. int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  2098. int is_attach)
  2099. {
  2100. int err;
  2101. size_t flow_size;
  2102. struct ib_flow_attr *flow = NULL;
  2103. struct ib_flow_spec_ib *ib_spec;
  2104. if (is_attach) {
  2105. flow_size = sizeof(struct ib_flow_attr) +
  2106. sizeof(struct ib_flow_spec_ib);
  2107. flow = kzalloc(flow_size, GFP_KERNEL);
  2108. if (!flow)
  2109. return -ENOMEM;
  2110. flow->port = mqp->port;
  2111. flow->num_of_specs = 1;
  2112. flow->size = flow_size;
  2113. ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
  2114. ib_spec->type = IB_FLOW_SPEC_IB;
  2115. ib_spec->size = sizeof(struct ib_flow_spec_ib);
  2116. /* Add an empty rule for IB L2 */
  2117. memset(&ib_spec->mask, 0, sizeof(ib_spec->mask));
  2118. err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
  2119. IB_FLOW_DOMAIN_NIC,
  2120. MLX4_FS_REGULAR,
  2121. &mqp->reg_id);
  2122. } else {
  2123. err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
  2124. }
  2125. kfree(flow);
  2126. return err;
  2127. }
  2128. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  2129. {
  2130. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  2131. int p;
  2132. ibdev->ib_active = false;
  2133. flush_workqueue(wq);
  2134. mlx4_ib_close_sriov(ibdev);
  2135. mlx4_ib_mad_cleanup(ibdev);
  2136. ib_unregister_device(&ibdev->ib_dev);
  2137. if (ibdev->iboe.nb.notifier_call) {
  2138. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  2139. pr_warn("failure unregistering notifier\n");
  2140. ibdev->iboe.nb.notifier_call = NULL;
  2141. }
  2142. if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED) {
  2143. mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
  2144. ibdev->steer_qpn_count);
  2145. kfree(ibdev->ib_uc_qpns_bitmap);
  2146. }
  2147. if (ibdev->iboe.nb_inet.notifier_call) {
  2148. if (unregister_inetaddr_notifier(&ibdev->iboe.nb_inet))
  2149. pr_warn("failure unregistering notifier\n");
  2150. ibdev->iboe.nb_inet.notifier_call = NULL;
  2151. }
  2152. #if IS_ENABLED(CONFIG_IPV6)
  2153. if (ibdev->iboe.nb_inet6.notifier_call) {
  2154. if (unregister_inet6addr_notifier(&ibdev->iboe.nb_inet6))
  2155. pr_warn("failure unregistering notifier\n");
  2156. ibdev->iboe.nb_inet6.notifier_call = NULL;
  2157. }
  2158. #endif
  2159. iounmap(ibdev->uar_map);
  2160. for (p = 0; p < ibdev->num_ports; ++p)
  2161. if (ibdev->counters[p] != -1)
  2162. mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
  2163. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  2164. mlx4_CLOSE_PORT(dev, p);
  2165. mlx4_ib_free_eqs(dev, ibdev);
  2166. mlx4_uar_free(dev, &ibdev->priv_uar);
  2167. mlx4_pd_free(dev, ibdev->priv_pdn);
  2168. ib_dealloc_device(&ibdev->ib_dev);
  2169. }
  2170. static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
  2171. {
  2172. struct mlx4_ib_demux_work **dm = NULL;
  2173. struct mlx4_dev *dev = ibdev->dev;
  2174. int i;
  2175. unsigned long flags;
  2176. struct mlx4_active_ports actv_ports;
  2177. unsigned int ports;
  2178. unsigned int first_port;
  2179. if (!mlx4_is_master(dev))
  2180. return;
  2181. actv_ports = mlx4_get_active_ports(dev, slave);
  2182. ports = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2183. first_port = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2184. dm = kcalloc(ports, sizeof(*dm), GFP_ATOMIC);
  2185. if (!dm) {
  2186. pr_err("failed to allocate memory for tunneling qp update\n");
  2187. goto out;
  2188. }
  2189. for (i = 0; i < ports; i++) {
  2190. dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
  2191. if (!dm[i]) {
  2192. pr_err("failed to allocate memory for tunneling qp update work struct\n");
  2193. for (i = 0; i < dev->caps.num_ports; i++) {
  2194. if (dm[i])
  2195. kfree(dm[i]);
  2196. }
  2197. goto out;
  2198. }
  2199. }
  2200. /* initialize or tear down tunnel QPs for the slave */
  2201. for (i = 0; i < ports; i++) {
  2202. INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
  2203. dm[i]->port = first_port + i + 1;
  2204. dm[i]->slave = slave;
  2205. dm[i]->do_init = do_init;
  2206. dm[i]->dev = ibdev;
  2207. spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
  2208. if (!ibdev->sriov.is_going_down)
  2209. queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
  2210. spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
  2211. }
  2212. out:
  2213. kfree(dm);
  2214. return;
  2215. }
  2216. static void mlx4_ib_handle_catas_error(struct mlx4_ib_dev *ibdev)
  2217. {
  2218. struct mlx4_ib_qp *mqp;
  2219. unsigned long flags_qp;
  2220. unsigned long flags_cq;
  2221. struct mlx4_ib_cq *send_mcq, *recv_mcq;
  2222. struct list_head cq_notify_list;
  2223. struct mlx4_cq *mcq;
  2224. unsigned long flags;
  2225. pr_warn("mlx4_ib_handle_catas_error was started\n");
  2226. INIT_LIST_HEAD(&cq_notify_list);
  2227. /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
  2228. spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
  2229. list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
  2230. spin_lock_irqsave(&mqp->sq.lock, flags_qp);
  2231. if (mqp->sq.tail != mqp->sq.head) {
  2232. send_mcq = to_mcq(mqp->ibqp.send_cq);
  2233. spin_lock_irqsave(&send_mcq->lock, flags_cq);
  2234. if (send_mcq->mcq.comp &&
  2235. mqp->ibqp.send_cq->comp_handler) {
  2236. if (!send_mcq->mcq.reset_notify_added) {
  2237. send_mcq->mcq.reset_notify_added = 1;
  2238. list_add_tail(&send_mcq->mcq.reset_notify,
  2239. &cq_notify_list);
  2240. }
  2241. }
  2242. spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
  2243. }
  2244. spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
  2245. /* Now, handle the QP's receive queue */
  2246. spin_lock_irqsave(&mqp->rq.lock, flags_qp);
  2247. /* no handling is needed for SRQ */
  2248. if (!mqp->ibqp.srq) {
  2249. if (mqp->rq.tail != mqp->rq.head) {
  2250. recv_mcq = to_mcq(mqp->ibqp.recv_cq);
  2251. spin_lock_irqsave(&recv_mcq->lock, flags_cq);
  2252. if (recv_mcq->mcq.comp &&
  2253. mqp->ibqp.recv_cq->comp_handler) {
  2254. if (!recv_mcq->mcq.reset_notify_added) {
  2255. recv_mcq->mcq.reset_notify_added = 1;
  2256. list_add_tail(&recv_mcq->mcq.reset_notify,
  2257. &cq_notify_list);
  2258. }
  2259. }
  2260. spin_unlock_irqrestore(&recv_mcq->lock,
  2261. flags_cq);
  2262. }
  2263. }
  2264. spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
  2265. }
  2266. list_for_each_entry(mcq, &cq_notify_list, reset_notify) {
  2267. mcq->comp(mcq);
  2268. }
  2269. spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
  2270. pr_warn("mlx4_ib_handle_catas_error ended\n");
  2271. }
  2272. static void handle_bonded_port_state_event(struct work_struct *work)
  2273. {
  2274. struct ib_event_work *ew =
  2275. container_of(work, struct ib_event_work, work);
  2276. struct mlx4_ib_dev *ibdev = ew->ib_dev;
  2277. enum ib_port_state bonded_port_state = IB_PORT_NOP;
  2278. int i;
  2279. struct ib_event ibev;
  2280. kfree(ew);
  2281. spin_lock_bh(&ibdev->iboe.lock);
  2282. for (i = 0; i < MLX4_MAX_PORTS; ++i) {
  2283. struct net_device *curr_netdev = ibdev->iboe.netdevs[i];
  2284. enum ib_port_state curr_port_state;
  2285. if (!curr_netdev)
  2286. continue;
  2287. curr_port_state =
  2288. (netif_running(curr_netdev) &&
  2289. netif_carrier_ok(curr_netdev)) ?
  2290. IB_PORT_ACTIVE : IB_PORT_DOWN;
  2291. bonded_port_state = (bonded_port_state != IB_PORT_ACTIVE) ?
  2292. curr_port_state : IB_PORT_ACTIVE;
  2293. }
  2294. spin_unlock_bh(&ibdev->iboe.lock);
  2295. ibev.device = &ibdev->ib_dev;
  2296. ibev.element.port_num = 1;
  2297. ibev.event = (bonded_port_state == IB_PORT_ACTIVE) ?
  2298. IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  2299. ib_dispatch_event(&ibev);
  2300. }
  2301. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  2302. enum mlx4_dev_event event, unsigned long param)
  2303. {
  2304. struct ib_event ibev;
  2305. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  2306. struct mlx4_eqe *eqe = NULL;
  2307. struct ib_event_work *ew;
  2308. int p = 0;
  2309. if (mlx4_is_bonded(dev) &&
  2310. ((event == MLX4_DEV_EVENT_PORT_UP) ||
  2311. (event == MLX4_DEV_EVENT_PORT_DOWN))) {
  2312. ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
  2313. if (!ew)
  2314. return;
  2315. INIT_WORK(&ew->work, handle_bonded_port_state_event);
  2316. ew->ib_dev = ibdev;
  2317. queue_work(wq, &ew->work);
  2318. return;
  2319. }
  2320. if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
  2321. eqe = (struct mlx4_eqe *)param;
  2322. else
  2323. p = (int) param;
  2324. switch (event) {
  2325. case MLX4_DEV_EVENT_PORT_UP:
  2326. if (p > ibdev->num_ports)
  2327. return;
  2328. if (mlx4_is_master(dev) &&
  2329. rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
  2330. IB_LINK_LAYER_INFINIBAND) {
  2331. mlx4_ib_invalidate_all_guid_record(ibdev, p);
  2332. }
  2333. ibev.event = IB_EVENT_PORT_ACTIVE;
  2334. break;
  2335. case MLX4_DEV_EVENT_PORT_DOWN:
  2336. if (p > ibdev->num_ports)
  2337. return;
  2338. ibev.event = IB_EVENT_PORT_ERR;
  2339. break;
  2340. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  2341. ibdev->ib_active = false;
  2342. ibev.event = IB_EVENT_DEVICE_FATAL;
  2343. mlx4_ib_handle_catas_error(ibdev);
  2344. break;
  2345. case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
  2346. ew = kmalloc(sizeof *ew, GFP_ATOMIC);
  2347. if (!ew) {
  2348. pr_err("failed to allocate memory for events work\n");
  2349. break;
  2350. }
  2351. INIT_WORK(&ew->work, handle_port_mgmt_change_event);
  2352. memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
  2353. ew->ib_dev = ibdev;
  2354. /* need to queue only for port owner, which uses GEN_EQE */
  2355. if (mlx4_is_master(dev))
  2356. queue_work(wq, &ew->work);
  2357. else
  2358. handle_port_mgmt_change_event(&ew->work);
  2359. return;
  2360. case MLX4_DEV_EVENT_SLAVE_INIT:
  2361. /* here, p is the slave id */
  2362. do_slave_init(ibdev, p, 1);
  2363. return;
  2364. case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
  2365. /* here, p is the slave id */
  2366. do_slave_init(ibdev, p, 0);
  2367. return;
  2368. default:
  2369. return;
  2370. }
  2371. ibev.device = ibdev_ptr;
  2372. ibev.element.port_num = mlx4_is_bonded(ibdev->dev) ? 1 : (u8)p;
  2373. ib_dispatch_event(&ibev);
  2374. }
  2375. static struct mlx4_interface mlx4_ib_interface = {
  2376. .add = mlx4_ib_add,
  2377. .remove = mlx4_ib_remove,
  2378. .event = mlx4_ib_event,
  2379. .protocol = MLX4_PROT_IB_IPV6,
  2380. .flags = MLX4_INTFF_BONDING
  2381. };
  2382. static int __init mlx4_ib_init(void)
  2383. {
  2384. int err;
  2385. wq = create_singlethread_workqueue("mlx4_ib");
  2386. if (!wq)
  2387. return -ENOMEM;
  2388. err = mlx4_ib_mcg_init();
  2389. if (err)
  2390. goto clean_wq;
  2391. err = mlx4_register_interface(&mlx4_ib_interface);
  2392. if (err)
  2393. goto clean_mcg;
  2394. return 0;
  2395. clean_mcg:
  2396. mlx4_ib_mcg_destroy();
  2397. clean_wq:
  2398. destroy_workqueue(wq);
  2399. return err;
  2400. }
  2401. static void __exit mlx4_ib_cleanup(void)
  2402. {
  2403. mlx4_unregister_interface(&mlx4_ib_interface);
  2404. mlx4_ib_mcg_destroy();
  2405. destroy_workqueue(wq);
  2406. }
  2407. module_init(mlx4_ib_init);
  2408. module_exit(mlx4_ib_cleanup);