main.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404
  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 <net/devlink.h>
  44. #include <rdma/ib_smi.h>
  45. #include <rdma/ib_user_verbs.h>
  46. #include <rdma/ib_addr.h>
  47. #include <rdma/ib_cache.h>
  48. #include <net/bonding.h>
  49. #include <linux/mlx4/driver.h>
  50. #include <linux/mlx4/cmd.h>
  51. #include <linux/mlx4/qp.h>
  52. #include "mlx4_ib.h"
  53. #include <rdma/mlx4-abi.h>
  54. #define DRV_NAME MLX4_IB_DRV_NAME
  55. #define DRV_VERSION "2.2-1"
  56. #define DRV_RELDATE "Feb 2014"
  57. #define MLX4_IB_FLOW_MAX_PRIO 0xFFF
  58. #define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
  59. #define MLX4_IB_CARD_REV_A0 0xA0
  60. MODULE_AUTHOR("Roland Dreier");
  61. MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
  62. MODULE_LICENSE("Dual BSD/GPL");
  63. MODULE_VERSION(DRV_VERSION);
  64. int mlx4_ib_sm_guid_assign = 0;
  65. module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
  66. MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 0)");
  67. static const char mlx4_ib_version[] =
  68. DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
  69. DRV_VERSION " (" DRV_RELDATE ")\n";
  70. static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
  71. static struct workqueue_struct *wq;
  72. static void init_query_mad(struct ib_smp *mad)
  73. {
  74. mad->base_version = 1;
  75. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  76. mad->class_version = 1;
  77. mad->method = IB_MGMT_METHOD_GET;
  78. }
  79. static int check_flow_steering_support(struct mlx4_dev *dev)
  80. {
  81. int eth_num_ports = 0;
  82. int ib_num_ports = 0;
  83. int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
  84. if (dmfs) {
  85. int i;
  86. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
  87. eth_num_ports++;
  88. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  89. ib_num_ports++;
  90. dmfs &= (!ib_num_ports ||
  91. (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB)) &&
  92. (!eth_num_ports ||
  93. (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN));
  94. if (ib_num_ports && mlx4_is_mfunc(dev)) {
  95. pr_warn("Device managed flow steering is unavailable for IB port in multifunction env.\n");
  96. dmfs = 0;
  97. }
  98. }
  99. return dmfs;
  100. }
  101. static int num_ib_ports(struct mlx4_dev *dev)
  102. {
  103. int ib_ports = 0;
  104. int i;
  105. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  106. ib_ports++;
  107. return ib_ports;
  108. }
  109. static struct net_device *mlx4_ib_get_netdev(struct ib_device *device, u8 port_num)
  110. {
  111. struct mlx4_ib_dev *ibdev = to_mdev(device);
  112. struct net_device *dev;
  113. rcu_read_lock();
  114. dev = mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port_num);
  115. if (dev) {
  116. if (mlx4_is_bonded(ibdev->dev)) {
  117. struct net_device *upper = NULL;
  118. upper = netdev_master_upper_dev_get_rcu(dev);
  119. if (upper) {
  120. struct net_device *active;
  121. active = bond_option_active_slave_get_rcu(netdev_priv(upper));
  122. if (active)
  123. dev = active;
  124. }
  125. }
  126. }
  127. if (dev)
  128. dev_hold(dev);
  129. rcu_read_unlock();
  130. return dev;
  131. }
  132. static int mlx4_ib_update_gids_v1(struct gid_entry *gids,
  133. struct mlx4_ib_dev *ibdev,
  134. u8 port_num)
  135. {
  136. struct mlx4_cmd_mailbox *mailbox;
  137. int err;
  138. struct mlx4_dev *dev = ibdev->dev;
  139. int i;
  140. union ib_gid *gid_tbl;
  141. mailbox = mlx4_alloc_cmd_mailbox(dev);
  142. if (IS_ERR(mailbox))
  143. return -ENOMEM;
  144. gid_tbl = mailbox->buf;
  145. for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i)
  146. memcpy(&gid_tbl[i], &gids[i].gid, sizeof(union ib_gid));
  147. err = mlx4_cmd(dev, mailbox->dma,
  148. MLX4_SET_PORT_GID_TABLE << 8 | port_num,
  149. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  150. MLX4_CMD_WRAPPED);
  151. if (mlx4_is_bonded(dev))
  152. err += mlx4_cmd(dev, mailbox->dma,
  153. MLX4_SET_PORT_GID_TABLE << 8 | 2,
  154. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  155. MLX4_CMD_WRAPPED);
  156. mlx4_free_cmd_mailbox(dev, mailbox);
  157. return err;
  158. }
  159. static int mlx4_ib_update_gids_v1_v2(struct gid_entry *gids,
  160. struct mlx4_ib_dev *ibdev,
  161. u8 port_num)
  162. {
  163. struct mlx4_cmd_mailbox *mailbox;
  164. int err;
  165. struct mlx4_dev *dev = ibdev->dev;
  166. int i;
  167. struct {
  168. union ib_gid gid;
  169. __be32 rsrvd1[2];
  170. __be16 rsrvd2;
  171. u8 type;
  172. u8 version;
  173. __be32 rsrvd3;
  174. } *gid_tbl;
  175. mailbox = mlx4_alloc_cmd_mailbox(dev);
  176. if (IS_ERR(mailbox))
  177. return -ENOMEM;
  178. gid_tbl = mailbox->buf;
  179. for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i) {
  180. memcpy(&gid_tbl[i].gid, &gids[i].gid, sizeof(union ib_gid));
  181. if (gids[i].gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
  182. gid_tbl[i].version = 2;
  183. if (!ipv6_addr_v4mapped((struct in6_addr *)&gids[i].gid))
  184. gid_tbl[i].type = 1;
  185. else
  186. memset(&gid_tbl[i].gid, 0, 12);
  187. }
  188. }
  189. err = mlx4_cmd(dev, mailbox->dma,
  190. MLX4_SET_PORT_ROCE_ADDR << 8 | port_num,
  191. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  192. MLX4_CMD_WRAPPED);
  193. if (mlx4_is_bonded(dev))
  194. err += mlx4_cmd(dev, mailbox->dma,
  195. MLX4_SET_PORT_ROCE_ADDR << 8 | 2,
  196. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  197. MLX4_CMD_WRAPPED);
  198. mlx4_free_cmd_mailbox(dev, mailbox);
  199. return err;
  200. }
  201. static int mlx4_ib_update_gids(struct gid_entry *gids,
  202. struct mlx4_ib_dev *ibdev,
  203. u8 port_num)
  204. {
  205. if (ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
  206. return mlx4_ib_update_gids_v1_v2(gids, ibdev, port_num);
  207. return mlx4_ib_update_gids_v1(gids, ibdev, port_num);
  208. }
  209. static int mlx4_ib_add_gid(struct ib_device *device,
  210. u8 port_num,
  211. unsigned int index,
  212. const union ib_gid *gid,
  213. const struct ib_gid_attr *attr,
  214. void **context)
  215. {
  216. struct mlx4_ib_dev *ibdev = to_mdev(device);
  217. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  218. struct mlx4_port_gid_table *port_gid_table;
  219. int free = -1, found = -1;
  220. int ret = 0;
  221. int hw_update = 0;
  222. int i;
  223. struct gid_entry *gids = NULL;
  224. if (!rdma_cap_roce_gid_table(device, port_num))
  225. return -EINVAL;
  226. if (port_num > MLX4_MAX_PORTS)
  227. return -EINVAL;
  228. if (!context)
  229. return -EINVAL;
  230. port_gid_table = &iboe->gids[port_num - 1];
  231. spin_lock_bh(&iboe->lock);
  232. for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i) {
  233. if (!memcmp(&port_gid_table->gids[i].gid, gid, sizeof(*gid)) &&
  234. (port_gid_table->gids[i].gid_type == attr->gid_type)) {
  235. found = i;
  236. break;
  237. }
  238. if (free < 0 && !memcmp(&port_gid_table->gids[i].gid, &zgid, sizeof(*gid)))
  239. free = i; /* HW has space */
  240. }
  241. if (found < 0) {
  242. if (free < 0) {
  243. ret = -ENOSPC;
  244. } else {
  245. port_gid_table->gids[free].ctx = kmalloc(sizeof(*port_gid_table->gids[free].ctx), GFP_ATOMIC);
  246. if (!port_gid_table->gids[free].ctx) {
  247. ret = -ENOMEM;
  248. } else {
  249. *context = port_gid_table->gids[free].ctx;
  250. memcpy(&port_gid_table->gids[free].gid, gid, sizeof(*gid));
  251. port_gid_table->gids[free].gid_type = attr->gid_type;
  252. port_gid_table->gids[free].ctx->real_index = free;
  253. port_gid_table->gids[free].ctx->refcount = 1;
  254. hw_update = 1;
  255. }
  256. }
  257. } else {
  258. struct gid_cache_context *ctx = port_gid_table->gids[found].ctx;
  259. *context = ctx;
  260. ctx->refcount++;
  261. }
  262. if (!ret && hw_update) {
  263. gids = kmalloc(sizeof(*gids) * MLX4_MAX_PORT_GIDS, GFP_ATOMIC);
  264. if (!gids) {
  265. ret = -ENOMEM;
  266. } else {
  267. for (i = 0; i < MLX4_MAX_PORT_GIDS; i++) {
  268. memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
  269. gids[i].gid_type = port_gid_table->gids[i].gid_type;
  270. }
  271. }
  272. }
  273. spin_unlock_bh(&iboe->lock);
  274. if (!ret && hw_update) {
  275. ret = mlx4_ib_update_gids(gids, ibdev, port_num);
  276. kfree(gids);
  277. }
  278. return ret;
  279. }
  280. static int mlx4_ib_del_gid(struct ib_device *device,
  281. u8 port_num,
  282. unsigned int index,
  283. void **context)
  284. {
  285. struct gid_cache_context *ctx = *context;
  286. struct mlx4_ib_dev *ibdev = to_mdev(device);
  287. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  288. struct mlx4_port_gid_table *port_gid_table;
  289. int ret = 0;
  290. int hw_update = 0;
  291. struct gid_entry *gids = NULL;
  292. if (!rdma_cap_roce_gid_table(device, port_num))
  293. return -EINVAL;
  294. if (port_num > MLX4_MAX_PORTS)
  295. return -EINVAL;
  296. port_gid_table = &iboe->gids[port_num - 1];
  297. spin_lock_bh(&iboe->lock);
  298. if (ctx) {
  299. ctx->refcount--;
  300. if (!ctx->refcount) {
  301. unsigned int real_index = ctx->real_index;
  302. memcpy(&port_gid_table->gids[real_index].gid, &zgid, sizeof(zgid));
  303. kfree(port_gid_table->gids[real_index].ctx);
  304. port_gid_table->gids[real_index].ctx = NULL;
  305. hw_update = 1;
  306. }
  307. }
  308. if (!ret && hw_update) {
  309. int i;
  310. gids = kmalloc(sizeof(*gids) * MLX4_MAX_PORT_GIDS, GFP_ATOMIC);
  311. if (!gids) {
  312. ret = -ENOMEM;
  313. } else {
  314. for (i = 0; i < MLX4_MAX_PORT_GIDS; i++)
  315. memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
  316. }
  317. }
  318. spin_unlock_bh(&iboe->lock);
  319. if (!ret && hw_update) {
  320. ret = mlx4_ib_update_gids(gids, ibdev, port_num);
  321. kfree(gids);
  322. }
  323. return ret;
  324. }
  325. int mlx4_ib_gid_index_to_real_index(struct mlx4_ib_dev *ibdev,
  326. u8 port_num, int index)
  327. {
  328. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  329. struct gid_cache_context *ctx = NULL;
  330. union ib_gid gid;
  331. struct mlx4_port_gid_table *port_gid_table;
  332. int real_index = -EINVAL;
  333. int i;
  334. int ret;
  335. unsigned long flags;
  336. struct ib_gid_attr attr;
  337. if (port_num > MLX4_MAX_PORTS)
  338. return -EINVAL;
  339. if (mlx4_is_bonded(ibdev->dev))
  340. port_num = 1;
  341. if (!rdma_cap_roce_gid_table(&ibdev->ib_dev, port_num))
  342. return index;
  343. ret = ib_get_cached_gid(&ibdev->ib_dev, port_num, index, &gid, &attr);
  344. if (ret)
  345. return ret;
  346. if (attr.ndev)
  347. dev_put(attr.ndev);
  348. if (!memcmp(&gid, &zgid, sizeof(gid)))
  349. return -EINVAL;
  350. spin_lock_irqsave(&iboe->lock, flags);
  351. port_gid_table = &iboe->gids[port_num - 1];
  352. for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i)
  353. if (!memcmp(&port_gid_table->gids[i].gid, &gid, sizeof(gid)) &&
  354. attr.gid_type == port_gid_table->gids[i].gid_type) {
  355. ctx = port_gid_table->gids[i].ctx;
  356. break;
  357. }
  358. if (ctx)
  359. real_index = ctx->real_index;
  360. spin_unlock_irqrestore(&iboe->lock, flags);
  361. return real_index;
  362. }
  363. static int mlx4_ib_query_device(struct ib_device *ibdev,
  364. struct ib_device_attr *props,
  365. struct ib_udata *uhw)
  366. {
  367. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  368. struct ib_smp *in_mad = NULL;
  369. struct ib_smp *out_mad = NULL;
  370. int err = -ENOMEM;
  371. int have_ib_ports;
  372. struct mlx4_uverbs_ex_query_device cmd;
  373. struct mlx4_uverbs_ex_query_device_resp resp = {.comp_mask = 0};
  374. struct mlx4_clock_params clock_params;
  375. if (uhw->inlen) {
  376. if (uhw->inlen < sizeof(cmd))
  377. return -EINVAL;
  378. err = ib_copy_from_udata(&cmd, uhw, sizeof(cmd));
  379. if (err)
  380. return err;
  381. if (cmd.comp_mask)
  382. return -EINVAL;
  383. if (cmd.reserved)
  384. return -EINVAL;
  385. }
  386. resp.response_length = offsetof(typeof(resp), response_length) +
  387. sizeof(resp.response_length);
  388. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  389. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  390. if (!in_mad || !out_mad)
  391. goto out;
  392. init_query_mad(in_mad);
  393. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  394. err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
  395. 1, NULL, NULL, in_mad, out_mad);
  396. if (err)
  397. goto out;
  398. memset(props, 0, sizeof *props);
  399. have_ib_ports = num_ib_ports(dev->dev);
  400. props->fw_ver = dev->dev->caps.fw_ver;
  401. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  402. IB_DEVICE_PORT_ACTIVE_EVENT |
  403. IB_DEVICE_SYS_IMAGE_GUID |
  404. IB_DEVICE_RC_RNR_NAK_GEN |
  405. IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  406. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
  407. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  408. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
  409. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  410. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM && have_ib_ports)
  411. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  412. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
  413. props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  414. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
  415. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  416. if (dev->dev->caps.max_gso_sz &&
  417. (dev->dev->rev_id != MLX4_IB_CARD_REV_A0) &&
  418. (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH))
  419. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  420. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
  421. props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
  422. if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
  423. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
  424. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
  425. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  426. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
  427. props->device_cap_flags |= IB_DEVICE_XRC;
  428. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)
  429. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW;
  430. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
  431. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_WIN_TYPE_2B)
  432. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2B;
  433. else
  434. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2A;
  435. }
  436. if (dev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
  437. props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
  438. props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
  439. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  440. 0xffffff;
  441. props->vendor_part_id = dev->dev->persist->pdev->device;
  442. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  443. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  444. props->max_mr_size = ~0ull;
  445. props->page_size_cap = dev->dev->caps.page_size_cap;
  446. props->max_qp = dev->dev->quotas.qp;
  447. props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
  448. props->max_sge = min(dev->dev->caps.max_sq_sg,
  449. dev->dev->caps.max_rq_sg);
  450. props->max_sge_rd = MLX4_MAX_SGE_RD;
  451. props->max_cq = dev->dev->quotas.cq;
  452. props->max_cqe = dev->dev->caps.max_cqes;
  453. props->max_mr = dev->dev->quotas.mpt;
  454. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  455. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  456. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  457. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  458. props->max_srq = dev->dev->quotas.srq;
  459. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  460. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  461. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  462. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  463. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  464. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  465. props->masked_atomic_cap = props->atomic_cap;
  466. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  467. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  468. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  469. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  470. props->max_mcast_grp;
  471. props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
  472. props->hca_core_clock = dev->dev->caps.hca_core_clock * 1000UL;
  473. props->timestamp_mask = 0xFFFFFFFFFFFFULL;
  474. if (!mlx4_is_slave(dev->dev))
  475. err = mlx4_get_internal_clock_params(dev->dev, &clock_params);
  476. if (uhw->outlen >= resp.response_length + sizeof(resp.hca_core_clock_offset)) {
  477. resp.response_length += sizeof(resp.hca_core_clock_offset);
  478. if (!err && !mlx4_is_slave(dev->dev)) {
  479. resp.comp_mask |= QUERY_DEVICE_RESP_MASK_TIMESTAMP;
  480. resp.hca_core_clock_offset = clock_params.offset % PAGE_SIZE;
  481. }
  482. }
  483. if (uhw->outlen) {
  484. err = ib_copy_to_udata(uhw, &resp, resp.response_length);
  485. if (err)
  486. goto out;
  487. }
  488. out:
  489. kfree(in_mad);
  490. kfree(out_mad);
  491. return err;
  492. }
  493. static enum rdma_link_layer
  494. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  495. {
  496. struct mlx4_dev *dev = to_mdev(device)->dev;
  497. return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
  498. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  499. }
  500. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  501. struct ib_port_attr *props, int netw_view)
  502. {
  503. struct ib_smp *in_mad = NULL;
  504. struct ib_smp *out_mad = NULL;
  505. int ext_active_speed;
  506. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  507. int err = -ENOMEM;
  508. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  509. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  510. if (!in_mad || !out_mad)
  511. goto out;
  512. init_query_mad(in_mad);
  513. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  514. in_mad->attr_mod = cpu_to_be32(port);
  515. if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
  516. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  517. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  518. in_mad, out_mad);
  519. if (err)
  520. goto out;
  521. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  522. props->lmc = out_mad->data[34] & 0x7;
  523. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  524. props->sm_sl = out_mad->data[36] & 0xf;
  525. props->state = out_mad->data[32] & 0xf;
  526. props->phys_state = out_mad->data[33] >> 4;
  527. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  528. if (netw_view)
  529. props->gid_tbl_len = out_mad->data[50];
  530. else
  531. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  532. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  533. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  534. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  535. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  536. props->active_width = out_mad->data[31] & 0xf;
  537. props->active_speed = out_mad->data[35] >> 4;
  538. props->max_mtu = out_mad->data[41] & 0xf;
  539. props->active_mtu = out_mad->data[36] >> 4;
  540. props->subnet_timeout = out_mad->data[51] & 0x1f;
  541. props->max_vl_num = out_mad->data[37] >> 4;
  542. props->init_type_reply = out_mad->data[41] >> 4;
  543. /* Check if extended speeds (EDR/FDR/...) are supported */
  544. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  545. ext_active_speed = out_mad->data[62] >> 4;
  546. switch (ext_active_speed) {
  547. case 1:
  548. props->active_speed = IB_SPEED_FDR;
  549. break;
  550. case 2:
  551. props->active_speed = IB_SPEED_EDR;
  552. break;
  553. }
  554. }
  555. /* If reported active speed is QDR, check if is FDR-10 */
  556. if (props->active_speed == IB_SPEED_QDR) {
  557. init_query_mad(in_mad);
  558. in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
  559. in_mad->attr_mod = cpu_to_be32(port);
  560. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
  561. NULL, NULL, in_mad, out_mad);
  562. if (err)
  563. goto out;
  564. /* Checking LinkSpeedActive for FDR-10 */
  565. if (out_mad->data[15] & 0x1)
  566. props->active_speed = IB_SPEED_FDR10;
  567. }
  568. /* Avoid wrong speed value returned by FW if the IB link is down. */
  569. if (props->state == IB_PORT_DOWN)
  570. props->active_speed = IB_SPEED_SDR;
  571. out:
  572. kfree(in_mad);
  573. kfree(out_mad);
  574. return err;
  575. }
  576. static u8 state_to_phys_state(enum ib_port_state state)
  577. {
  578. return state == IB_PORT_ACTIVE ? 5 : 3;
  579. }
  580. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  581. struct ib_port_attr *props, int netw_view)
  582. {
  583. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  584. struct mlx4_ib_iboe *iboe = &mdev->iboe;
  585. struct net_device *ndev;
  586. enum ib_mtu tmp;
  587. struct mlx4_cmd_mailbox *mailbox;
  588. int err = 0;
  589. int is_bonded = mlx4_is_bonded(mdev->dev);
  590. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  591. if (IS_ERR(mailbox))
  592. return PTR_ERR(mailbox);
  593. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
  594. MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
  595. MLX4_CMD_WRAPPED);
  596. if (err)
  597. goto out;
  598. props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ||
  599. (((u8 *)mailbox->buf)[5] == 0x20 /*56Gb*/) ?
  600. IB_WIDTH_4X : IB_WIDTH_1X;
  601. props->active_speed = (((u8 *)mailbox->buf)[5] == 0x20 /*56Gb*/) ?
  602. IB_SPEED_FDR : IB_SPEED_QDR;
  603. props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_IP_BASED_GIDS;
  604. props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
  605. props->max_msg_sz = mdev->dev->caps.max_msg_sz;
  606. props->pkey_tbl_len = 1;
  607. props->max_mtu = IB_MTU_4096;
  608. props->max_vl_num = 2;
  609. props->state = IB_PORT_DOWN;
  610. props->phys_state = state_to_phys_state(props->state);
  611. props->active_mtu = IB_MTU_256;
  612. spin_lock_bh(&iboe->lock);
  613. ndev = iboe->netdevs[port - 1];
  614. if (ndev && is_bonded) {
  615. rcu_read_lock(); /* required to get upper dev */
  616. ndev = netdev_master_upper_dev_get_rcu(ndev);
  617. rcu_read_unlock();
  618. }
  619. if (!ndev)
  620. goto out_unlock;
  621. tmp = iboe_get_mtu(ndev->mtu);
  622. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  623. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  624. IB_PORT_ACTIVE : IB_PORT_DOWN;
  625. props->phys_state = state_to_phys_state(props->state);
  626. out_unlock:
  627. spin_unlock_bh(&iboe->lock);
  628. out:
  629. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  630. return err;
  631. }
  632. int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  633. struct ib_port_attr *props, int netw_view)
  634. {
  635. int err;
  636. memset(props, 0, sizeof *props);
  637. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  638. ib_link_query_port(ibdev, port, props, netw_view) :
  639. eth_link_query_port(ibdev, port, props, netw_view);
  640. return err;
  641. }
  642. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  643. struct ib_port_attr *props)
  644. {
  645. /* returns host view */
  646. return __mlx4_ib_query_port(ibdev, port, props, 0);
  647. }
  648. int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  649. union ib_gid *gid, int netw_view)
  650. {
  651. struct ib_smp *in_mad = NULL;
  652. struct ib_smp *out_mad = NULL;
  653. int err = -ENOMEM;
  654. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  655. int clear = 0;
  656. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  657. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  658. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  659. if (!in_mad || !out_mad)
  660. goto out;
  661. init_query_mad(in_mad);
  662. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  663. in_mad->attr_mod = cpu_to_be32(port);
  664. if (mlx4_is_mfunc(dev->dev) && netw_view)
  665. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  666. err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
  667. if (err)
  668. goto out;
  669. memcpy(gid->raw, out_mad->data + 8, 8);
  670. if (mlx4_is_mfunc(dev->dev) && !netw_view) {
  671. if (index) {
  672. /* For any index > 0, return the null guid */
  673. err = 0;
  674. clear = 1;
  675. goto out;
  676. }
  677. }
  678. init_query_mad(in_mad);
  679. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  680. in_mad->attr_mod = cpu_to_be32(index / 8);
  681. err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
  682. NULL, NULL, in_mad, out_mad);
  683. if (err)
  684. goto out;
  685. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  686. out:
  687. if (clear)
  688. memset(gid->raw + 8, 0, 8);
  689. kfree(in_mad);
  690. kfree(out_mad);
  691. return err;
  692. }
  693. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  694. union ib_gid *gid)
  695. {
  696. int ret;
  697. if (rdma_protocol_ib(ibdev, port))
  698. return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
  699. if (!rdma_protocol_roce(ibdev, port))
  700. return -ENODEV;
  701. if (!rdma_cap_roce_gid_table(ibdev, port))
  702. return -ENODEV;
  703. ret = ib_get_cached_gid(ibdev, port, index, gid, NULL);
  704. if (ret == -EAGAIN) {
  705. memcpy(gid, &zgid, sizeof(*gid));
  706. return 0;
  707. }
  708. return ret;
  709. }
  710. static int mlx4_ib_query_sl2vl(struct ib_device *ibdev, u8 port, u64 *sl2vl_tbl)
  711. {
  712. union sl2vl_tbl_to_u64 sl2vl64;
  713. struct ib_smp *in_mad = NULL;
  714. struct ib_smp *out_mad = NULL;
  715. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  716. int err = -ENOMEM;
  717. int jj;
  718. if (mlx4_is_slave(to_mdev(ibdev)->dev)) {
  719. *sl2vl_tbl = 0;
  720. return 0;
  721. }
  722. in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
  723. out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
  724. if (!in_mad || !out_mad)
  725. goto out;
  726. init_query_mad(in_mad);
  727. in_mad->attr_id = IB_SMP_ATTR_SL_TO_VL_TABLE;
  728. in_mad->attr_mod = 0;
  729. if (mlx4_is_mfunc(to_mdev(ibdev)->dev))
  730. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  731. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  732. in_mad, out_mad);
  733. if (err)
  734. goto out;
  735. for (jj = 0; jj < 8; jj++)
  736. sl2vl64.sl8[jj] = ((struct ib_smp *)out_mad)->data[jj];
  737. *sl2vl_tbl = sl2vl64.sl64;
  738. out:
  739. kfree(in_mad);
  740. kfree(out_mad);
  741. return err;
  742. }
  743. static void mlx4_init_sl2vl_tbl(struct mlx4_ib_dev *mdev)
  744. {
  745. u64 sl2vl;
  746. int i;
  747. int err;
  748. for (i = 1; i <= mdev->dev->caps.num_ports; i++) {
  749. if (mdev->dev->caps.port_type[i] == MLX4_PORT_TYPE_ETH)
  750. continue;
  751. err = mlx4_ib_query_sl2vl(&mdev->ib_dev, i, &sl2vl);
  752. if (err) {
  753. pr_err("Unable to get default sl to vl mapping for port %d. Using all zeroes (%d)\n",
  754. i, err);
  755. sl2vl = 0;
  756. }
  757. atomic64_set(&mdev->sl2vl[i - 1], sl2vl);
  758. }
  759. }
  760. int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  761. u16 *pkey, int netw_view)
  762. {
  763. struct ib_smp *in_mad = NULL;
  764. struct ib_smp *out_mad = NULL;
  765. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  766. int err = -ENOMEM;
  767. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  768. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  769. if (!in_mad || !out_mad)
  770. goto out;
  771. init_query_mad(in_mad);
  772. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  773. in_mad->attr_mod = cpu_to_be32(index / 32);
  774. if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
  775. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  776. err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
  777. in_mad, out_mad);
  778. if (err)
  779. goto out;
  780. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  781. out:
  782. kfree(in_mad);
  783. kfree(out_mad);
  784. return err;
  785. }
  786. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
  787. {
  788. return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
  789. }
  790. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  791. struct ib_device_modify *props)
  792. {
  793. struct mlx4_cmd_mailbox *mailbox;
  794. unsigned long flags;
  795. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  796. return -EOPNOTSUPP;
  797. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  798. return 0;
  799. if (mlx4_is_slave(to_mdev(ibdev)->dev))
  800. return -EOPNOTSUPP;
  801. spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
  802. memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
  803. spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
  804. /*
  805. * If possible, pass node desc to FW, so it can generate
  806. * a 144 trap. If cmd fails, just ignore.
  807. */
  808. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  809. if (IS_ERR(mailbox))
  810. return 0;
  811. memcpy(mailbox->buf, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
  812. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  813. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  814. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  815. return 0;
  816. }
  817. static int mlx4_ib_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  818. u32 cap_mask)
  819. {
  820. struct mlx4_cmd_mailbox *mailbox;
  821. int err;
  822. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  823. if (IS_ERR(mailbox))
  824. return PTR_ERR(mailbox);
  825. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  826. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  827. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  828. } else {
  829. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  830. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  831. }
  832. err = mlx4_cmd(dev->dev, mailbox->dma, port, MLX4_SET_PORT_IB_OPCODE,
  833. MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  834. MLX4_CMD_WRAPPED);
  835. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  836. return err;
  837. }
  838. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  839. struct ib_port_modify *props)
  840. {
  841. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  842. u8 is_eth = mdev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  843. struct ib_port_attr attr;
  844. u32 cap_mask;
  845. int err;
  846. /* return OK if this is RoCE. CM calls ib_modify_port() regardless
  847. * of whether port link layer is ETH or IB. For ETH ports, qkey
  848. * violations and port capabilities are not meaningful.
  849. */
  850. if (is_eth)
  851. return 0;
  852. mutex_lock(&mdev->cap_mask_mutex);
  853. err = mlx4_ib_query_port(ibdev, port, &attr);
  854. if (err)
  855. goto out;
  856. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  857. ~props->clr_port_cap_mask;
  858. err = mlx4_ib_SET_PORT(mdev, port,
  859. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  860. cap_mask);
  861. out:
  862. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  863. return err;
  864. }
  865. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  866. struct ib_udata *udata)
  867. {
  868. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  869. struct mlx4_ib_ucontext *context;
  870. struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
  871. struct mlx4_ib_alloc_ucontext_resp resp;
  872. int err;
  873. if (!dev->ib_active)
  874. return ERR_PTR(-EAGAIN);
  875. if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
  876. resp_v3.qp_tab_size = dev->dev->caps.num_qps;
  877. resp_v3.bf_reg_size = dev->dev->caps.bf_reg_size;
  878. resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  879. } else {
  880. resp.dev_caps = dev->dev->caps.userspace_caps;
  881. resp.qp_tab_size = dev->dev->caps.num_qps;
  882. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  883. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  884. resp.cqe_size = dev->dev->caps.cqe_size;
  885. }
  886. context = kzalloc(sizeof(*context), GFP_KERNEL);
  887. if (!context)
  888. return ERR_PTR(-ENOMEM);
  889. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  890. if (err) {
  891. kfree(context);
  892. return ERR_PTR(err);
  893. }
  894. INIT_LIST_HEAD(&context->db_page_list);
  895. mutex_init(&context->db_page_mutex);
  896. if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
  897. err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
  898. else
  899. err = ib_copy_to_udata(udata, &resp, sizeof(resp));
  900. if (err) {
  901. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  902. kfree(context);
  903. return ERR_PTR(-EFAULT);
  904. }
  905. return &context->ibucontext;
  906. }
  907. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  908. {
  909. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  910. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  911. kfree(context);
  912. return 0;
  913. }
  914. static void mlx4_ib_vma_open(struct vm_area_struct *area)
  915. {
  916. /* vma_open is called when a new VMA is created on top of our VMA.
  917. * This is done through either mremap flow or split_vma (usually due
  918. * to mlock, madvise, munmap, etc.). We do not support a clone of the
  919. * vma, as this VMA is strongly hardware related. Therefore we set the
  920. * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
  921. * calling us again and trying to do incorrect actions. We assume that
  922. * the original vma size is exactly a single page that there will be no
  923. * "splitting" operations on.
  924. */
  925. area->vm_ops = NULL;
  926. }
  927. static void mlx4_ib_vma_close(struct vm_area_struct *area)
  928. {
  929. struct mlx4_ib_vma_private_data *mlx4_ib_vma_priv_data;
  930. /* It's guaranteed that all VMAs opened on a FD are closed before the
  931. * file itself is closed, therefore no sync is needed with the regular
  932. * closing flow. (e.g. mlx4_ib_dealloc_ucontext) However need a sync
  933. * with accessing the vma as part of mlx4_ib_disassociate_ucontext.
  934. * The close operation is usually called under mm->mmap_sem except when
  935. * process is exiting. The exiting case is handled explicitly as part
  936. * of mlx4_ib_disassociate_ucontext.
  937. */
  938. mlx4_ib_vma_priv_data = (struct mlx4_ib_vma_private_data *)
  939. area->vm_private_data;
  940. /* set the vma context pointer to null in the mlx4_ib driver's private
  941. * data to protect against a race condition in mlx4_ib_dissassociate_ucontext().
  942. */
  943. mlx4_ib_vma_priv_data->vma = NULL;
  944. }
  945. static const struct vm_operations_struct mlx4_ib_vm_ops = {
  946. .open = mlx4_ib_vma_open,
  947. .close = mlx4_ib_vma_close
  948. };
  949. static void mlx4_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
  950. {
  951. int i;
  952. int ret = 0;
  953. struct vm_area_struct *vma;
  954. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  955. struct task_struct *owning_process = NULL;
  956. struct mm_struct *owning_mm = NULL;
  957. owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
  958. if (!owning_process)
  959. return;
  960. owning_mm = get_task_mm(owning_process);
  961. if (!owning_mm) {
  962. pr_info("no mm, disassociate ucontext is pending task termination\n");
  963. while (1) {
  964. /* make sure that task is dead before returning, it may
  965. * prevent a rare case of module down in parallel to a
  966. * call to mlx4_ib_vma_close.
  967. */
  968. put_task_struct(owning_process);
  969. msleep(1);
  970. owning_process = get_pid_task(ibcontext->tgid,
  971. PIDTYPE_PID);
  972. if (!owning_process ||
  973. owning_process->state == TASK_DEAD) {
  974. pr_info("disassociate ucontext done, task was terminated\n");
  975. /* in case task was dead need to release the task struct */
  976. if (owning_process)
  977. put_task_struct(owning_process);
  978. return;
  979. }
  980. }
  981. }
  982. /* need to protect from a race on closing the vma as part of
  983. * mlx4_ib_vma_close().
  984. */
  985. down_read(&owning_mm->mmap_sem);
  986. for (i = 0; i < HW_BAR_COUNT; i++) {
  987. vma = context->hw_bar_info[i].vma;
  988. if (!vma)
  989. continue;
  990. ret = zap_vma_ptes(context->hw_bar_info[i].vma,
  991. context->hw_bar_info[i].vma->vm_start,
  992. PAGE_SIZE);
  993. if (ret) {
  994. pr_err("Error: zap_vma_ptes failed for index=%d, ret=%d\n", i, ret);
  995. BUG_ON(1);
  996. }
  997. /* context going to be destroyed, should not access ops any more */
  998. context->hw_bar_info[i].vma->vm_ops = NULL;
  999. }
  1000. up_read(&owning_mm->mmap_sem);
  1001. mmput(owning_mm);
  1002. put_task_struct(owning_process);
  1003. }
  1004. static void mlx4_ib_set_vma_data(struct vm_area_struct *vma,
  1005. struct mlx4_ib_vma_private_data *vma_private_data)
  1006. {
  1007. vma_private_data->vma = vma;
  1008. vma->vm_private_data = vma_private_data;
  1009. vma->vm_ops = &mlx4_ib_vm_ops;
  1010. }
  1011. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  1012. {
  1013. struct mlx4_ib_dev *dev = to_mdev(context->device);
  1014. struct mlx4_ib_ucontext *mucontext = to_mucontext(context);
  1015. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  1016. return -EINVAL;
  1017. if (vma->vm_pgoff == 0) {
  1018. /* We prevent double mmaping on same context */
  1019. if (mucontext->hw_bar_info[HW_BAR_DB].vma)
  1020. return -EINVAL;
  1021. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1022. if (io_remap_pfn_range(vma, vma->vm_start,
  1023. to_mucontext(context)->uar.pfn,
  1024. PAGE_SIZE, vma->vm_page_prot))
  1025. return -EAGAIN;
  1026. mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_DB]);
  1027. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  1028. /* We prevent double mmaping on same context */
  1029. if (mucontext->hw_bar_info[HW_BAR_BF].vma)
  1030. return -EINVAL;
  1031. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  1032. if (io_remap_pfn_range(vma, vma->vm_start,
  1033. to_mucontext(context)->uar.pfn +
  1034. dev->dev->caps.num_uars,
  1035. PAGE_SIZE, vma->vm_page_prot))
  1036. return -EAGAIN;
  1037. mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_BF]);
  1038. } else if (vma->vm_pgoff == 3) {
  1039. struct mlx4_clock_params params;
  1040. int ret;
  1041. /* We prevent double mmaping on same context */
  1042. if (mucontext->hw_bar_info[HW_BAR_CLOCK].vma)
  1043. return -EINVAL;
  1044. ret = mlx4_get_internal_clock_params(dev->dev, &params);
  1045. if (ret)
  1046. return ret;
  1047. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1048. if (io_remap_pfn_range(vma, vma->vm_start,
  1049. (pci_resource_start(dev->dev->persist->pdev,
  1050. params.bar) +
  1051. params.offset)
  1052. >> PAGE_SHIFT,
  1053. PAGE_SIZE, vma->vm_page_prot))
  1054. return -EAGAIN;
  1055. mlx4_ib_set_vma_data(vma,
  1056. &mucontext->hw_bar_info[HW_BAR_CLOCK]);
  1057. } else {
  1058. return -EINVAL;
  1059. }
  1060. return 0;
  1061. }
  1062. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  1063. struct ib_ucontext *context,
  1064. struct ib_udata *udata)
  1065. {
  1066. struct mlx4_ib_pd *pd;
  1067. int err;
  1068. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  1069. if (!pd)
  1070. return ERR_PTR(-ENOMEM);
  1071. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  1072. if (err) {
  1073. kfree(pd);
  1074. return ERR_PTR(err);
  1075. }
  1076. if (context)
  1077. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  1078. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  1079. kfree(pd);
  1080. return ERR_PTR(-EFAULT);
  1081. }
  1082. return &pd->ibpd;
  1083. }
  1084. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  1085. {
  1086. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  1087. kfree(pd);
  1088. return 0;
  1089. }
  1090. static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
  1091. struct ib_ucontext *context,
  1092. struct ib_udata *udata)
  1093. {
  1094. struct mlx4_ib_xrcd *xrcd;
  1095. struct ib_cq_init_attr cq_attr = {};
  1096. int err;
  1097. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  1098. return ERR_PTR(-ENOSYS);
  1099. xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
  1100. if (!xrcd)
  1101. return ERR_PTR(-ENOMEM);
  1102. err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
  1103. if (err)
  1104. goto err1;
  1105. xrcd->pd = ib_alloc_pd(ibdev, 0);
  1106. if (IS_ERR(xrcd->pd)) {
  1107. err = PTR_ERR(xrcd->pd);
  1108. goto err2;
  1109. }
  1110. cq_attr.cqe = 1;
  1111. xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, &cq_attr);
  1112. if (IS_ERR(xrcd->cq)) {
  1113. err = PTR_ERR(xrcd->cq);
  1114. goto err3;
  1115. }
  1116. return &xrcd->ibxrcd;
  1117. err3:
  1118. ib_dealloc_pd(xrcd->pd);
  1119. err2:
  1120. mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
  1121. err1:
  1122. kfree(xrcd);
  1123. return ERR_PTR(err);
  1124. }
  1125. static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  1126. {
  1127. ib_destroy_cq(to_mxrcd(xrcd)->cq);
  1128. ib_dealloc_pd(to_mxrcd(xrcd)->pd);
  1129. mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
  1130. kfree(xrcd);
  1131. return 0;
  1132. }
  1133. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  1134. {
  1135. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1136. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  1137. struct mlx4_ib_gid_entry *ge;
  1138. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  1139. if (!ge)
  1140. return -ENOMEM;
  1141. ge->gid = *gid;
  1142. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  1143. ge->port = mqp->port;
  1144. ge->added = 1;
  1145. }
  1146. mutex_lock(&mqp->mutex);
  1147. list_add_tail(&ge->list, &mqp->gid_list);
  1148. mutex_unlock(&mqp->mutex);
  1149. return 0;
  1150. }
  1151. static void mlx4_ib_delete_counters_table(struct mlx4_ib_dev *ibdev,
  1152. struct mlx4_ib_counters *ctr_table)
  1153. {
  1154. struct counter_index *counter, *tmp_count;
  1155. mutex_lock(&ctr_table->mutex);
  1156. list_for_each_entry_safe(counter, tmp_count, &ctr_table->counters_list,
  1157. list) {
  1158. if (counter->allocated)
  1159. mlx4_counter_free(ibdev->dev, counter->index);
  1160. list_del(&counter->list);
  1161. kfree(counter);
  1162. }
  1163. mutex_unlock(&ctr_table->mutex);
  1164. }
  1165. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  1166. union ib_gid *gid)
  1167. {
  1168. struct net_device *ndev;
  1169. int ret = 0;
  1170. if (!mqp->port)
  1171. return 0;
  1172. spin_lock_bh(&mdev->iboe.lock);
  1173. ndev = mdev->iboe.netdevs[mqp->port - 1];
  1174. if (ndev)
  1175. dev_hold(ndev);
  1176. spin_unlock_bh(&mdev->iboe.lock);
  1177. if (ndev) {
  1178. ret = 1;
  1179. dev_put(ndev);
  1180. }
  1181. return ret;
  1182. }
  1183. struct mlx4_ib_steering {
  1184. struct list_head list;
  1185. struct mlx4_flow_reg_id reg_id;
  1186. union ib_gid gid;
  1187. };
  1188. #define LAST_ETH_FIELD vlan_tag
  1189. #define LAST_IB_FIELD sl
  1190. #define LAST_IPV4_FIELD dst_ip
  1191. #define LAST_TCP_UDP_FIELD src_port
  1192. /* Field is the last supported field */
  1193. #define FIELDS_NOT_SUPPORTED(filter, field)\
  1194. memchr_inv((void *)&filter.field +\
  1195. sizeof(filter.field), 0,\
  1196. sizeof(filter) -\
  1197. offsetof(typeof(filter), field) -\
  1198. sizeof(filter.field))
  1199. static int parse_flow_attr(struct mlx4_dev *dev,
  1200. u32 qp_num,
  1201. union ib_flow_spec *ib_spec,
  1202. struct _rule_hw *mlx4_spec)
  1203. {
  1204. enum mlx4_net_trans_rule_id type;
  1205. switch (ib_spec->type) {
  1206. case IB_FLOW_SPEC_ETH:
  1207. if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
  1208. return -ENOTSUPP;
  1209. type = MLX4_NET_TRANS_RULE_ID_ETH;
  1210. memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
  1211. ETH_ALEN);
  1212. memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
  1213. ETH_ALEN);
  1214. mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
  1215. mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
  1216. break;
  1217. case IB_FLOW_SPEC_IB:
  1218. if (FIELDS_NOT_SUPPORTED(ib_spec->ib.mask, LAST_IB_FIELD))
  1219. return -ENOTSUPP;
  1220. type = MLX4_NET_TRANS_RULE_ID_IB;
  1221. mlx4_spec->ib.l3_qpn =
  1222. cpu_to_be32(qp_num);
  1223. mlx4_spec->ib.qpn_mask =
  1224. cpu_to_be32(MLX4_IB_FLOW_QPN_MASK);
  1225. break;
  1226. case IB_FLOW_SPEC_IPV4:
  1227. if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
  1228. return -ENOTSUPP;
  1229. type = MLX4_NET_TRANS_RULE_ID_IPV4;
  1230. mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
  1231. mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
  1232. mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
  1233. mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
  1234. break;
  1235. case IB_FLOW_SPEC_TCP:
  1236. case IB_FLOW_SPEC_UDP:
  1237. if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, LAST_TCP_UDP_FIELD))
  1238. return -ENOTSUPP;
  1239. type = ib_spec->type == IB_FLOW_SPEC_TCP ?
  1240. MLX4_NET_TRANS_RULE_ID_TCP :
  1241. MLX4_NET_TRANS_RULE_ID_UDP;
  1242. mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
  1243. mlx4_spec->tcp_udp.dst_port_msk = ib_spec->tcp_udp.mask.dst_port;
  1244. mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
  1245. mlx4_spec->tcp_udp.src_port_msk = ib_spec->tcp_udp.mask.src_port;
  1246. break;
  1247. default:
  1248. return -EINVAL;
  1249. }
  1250. if (mlx4_map_sw_to_hw_steering_id(dev, type) < 0 ||
  1251. mlx4_hw_rule_sz(dev, type) < 0)
  1252. return -EINVAL;
  1253. mlx4_spec->id = cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev, type));
  1254. mlx4_spec->size = mlx4_hw_rule_sz(dev, type) >> 2;
  1255. return mlx4_hw_rule_sz(dev, type);
  1256. }
  1257. struct default_rules {
  1258. __u32 mandatory_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
  1259. __u32 mandatory_not_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
  1260. __u32 rules_create_list[IB_FLOW_SPEC_SUPPORT_LAYERS];
  1261. __u8 link_layer;
  1262. };
  1263. static const struct default_rules default_table[] = {
  1264. {
  1265. .mandatory_fields = {IB_FLOW_SPEC_IPV4},
  1266. .mandatory_not_fields = {IB_FLOW_SPEC_ETH},
  1267. .rules_create_list = {IB_FLOW_SPEC_IB},
  1268. .link_layer = IB_LINK_LAYER_INFINIBAND
  1269. }
  1270. };
  1271. static int __mlx4_ib_default_rules_match(struct ib_qp *qp,
  1272. struct ib_flow_attr *flow_attr)
  1273. {
  1274. int i, j, k;
  1275. void *ib_flow;
  1276. const struct default_rules *pdefault_rules = default_table;
  1277. u8 link_layer = rdma_port_get_link_layer(qp->device, flow_attr->port);
  1278. for (i = 0; i < ARRAY_SIZE(default_table); i++, pdefault_rules++) {
  1279. __u32 field_types[IB_FLOW_SPEC_SUPPORT_LAYERS];
  1280. memset(&field_types, 0, sizeof(field_types));
  1281. if (link_layer != pdefault_rules->link_layer)
  1282. continue;
  1283. ib_flow = flow_attr + 1;
  1284. /* we assume the specs are sorted */
  1285. for (j = 0, k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS &&
  1286. j < flow_attr->num_of_specs; k++) {
  1287. union ib_flow_spec *current_flow =
  1288. (union ib_flow_spec *)ib_flow;
  1289. /* same layer but different type */
  1290. if (((current_flow->type & IB_FLOW_SPEC_LAYER_MASK) ==
  1291. (pdefault_rules->mandatory_fields[k] &
  1292. IB_FLOW_SPEC_LAYER_MASK)) &&
  1293. (current_flow->type !=
  1294. pdefault_rules->mandatory_fields[k]))
  1295. goto out;
  1296. /* same layer, try match next one */
  1297. if (current_flow->type ==
  1298. pdefault_rules->mandatory_fields[k]) {
  1299. j++;
  1300. ib_flow +=
  1301. ((union ib_flow_spec *)ib_flow)->size;
  1302. }
  1303. }
  1304. ib_flow = flow_attr + 1;
  1305. for (j = 0; j < flow_attr->num_of_specs;
  1306. j++, ib_flow += ((union ib_flow_spec *)ib_flow)->size)
  1307. for (k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS; k++)
  1308. /* same layer and same type */
  1309. if (((union ib_flow_spec *)ib_flow)->type ==
  1310. pdefault_rules->mandatory_not_fields[k])
  1311. goto out;
  1312. return i;
  1313. }
  1314. out:
  1315. return -1;
  1316. }
  1317. static int __mlx4_ib_create_default_rules(
  1318. struct mlx4_ib_dev *mdev,
  1319. struct ib_qp *qp,
  1320. const struct default_rules *pdefault_rules,
  1321. struct _rule_hw *mlx4_spec) {
  1322. int size = 0;
  1323. int i;
  1324. for (i = 0; i < ARRAY_SIZE(pdefault_rules->rules_create_list); i++) {
  1325. int ret;
  1326. union ib_flow_spec ib_spec;
  1327. switch (pdefault_rules->rules_create_list[i]) {
  1328. case 0:
  1329. /* no rule */
  1330. continue;
  1331. case IB_FLOW_SPEC_IB:
  1332. ib_spec.type = IB_FLOW_SPEC_IB;
  1333. ib_spec.size = sizeof(struct ib_flow_spec_ib);
  1334. break;
  1335. default:
  1336. /* invalid rule */
  1337. return -EINVAL;
  1338. }
  1339. /* We must put empty rule, qpn is being ignored */
  1340. ret = parse_flow_attr(mdev->dev, 0, &ib_spec,
  1341. mlx4_spec);
  1342. if (ret < 0) {
  1343. pr_info("invalid parsing\n");
  1344. return -EINVAL;
  1345. }
  1346. mlx4_spec = (void *)mlx4_spec + ret;
  1347. size += ret;
  1348. }
  1349. return size;
  1350. }
  1351. static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
  1352. int domain,
  1353. enum mlx4_net_trans_promisc_mode flow_type,
  1354. u64 *reg_id)
  1355. {
  1356. int ret, i;
  1357. int size = 0;
  1358. void *ib_flow;
  1359. struct mlx4_ib_dev *mdev = to_mdev(qp->device);
  1360. struct mlx4_cmd_mailbox *mailbox;
  1361. struct mlx4_net_trans_rule_hw_ctrl *ctrl;
  1362. int default_flow;
  1363. static const u16 __mlx4_domain[] = {
  1364. [IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
  1365. [IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
  1366. [IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
  1367. [IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
  1368. };
  1369. if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
  1370. pr_err("Invalid priority value %d\n", flow_attr->priority);
  1371. return -EINVAL;
  1372. }
  1373. if (domain >= IB_FLOW_DOMAIN_NUM) {
  1374. pr_err("Invalid domain value %d\n", domain);
  1375. return -EINVAL;
  1376. }
  1377. if (mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
  1378. return -EINVAL;
  1379. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  1380. if (IS_ERR(mailbox))
  1381. return PTR_ERR(mailbox);
  1382. ctrl = mailbox->buf;
  1383. ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
  1384. flow_attr->priority);
  1385. ctrl->type = mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type);
  1386. ctrl->port = flow_attr->port;
  1387. ctrl->qpn = cpu_to_be32(qp->qp_num);
  1388. ib_flow = flow_attr + 1;
  1389. size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
  1390. /* Add default flows */
  1391. default_flow = __mlx4_ib_default_rules_match(qp, flow_attr);
  1392. if (default_flow >= 0) {
  1393. ret = __mlx4_ib_create_default_rules(
  1394. mdev, qp, default_table + default_flow,
  1395. mailbox->buf + size);
  1396. if (ret < 0) {
  1397. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  1398. return -EINVAL;
  1399. }
  1400. size += ret;
  1401. }
  1402. for (i = 0; i < flow_attr->num_of_specs; i++) {
  1403. ret = parse_flow_attr(mdev->dev, qp->qp_num, ib_flow,
  1404. mailbox->buf + size);
  1405. if (ret < 0) {
  1406. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  1407. return -EINVAL;
  1408. }
  1409. ib_flow += ((union ib_flow_spec *) ib_flow)->size;
  1410. size += ret;
  1411. }
  1412. if (mlx4_is_master(mdev->dev) && flow_type == MLX4_FS_REGULAR &&
  1413. flow_attr->num_of_specs == 1) {
  1414. struct _rule_hw *rule_header = (struct _rule_hw *)(ctrl + 1);
  1415. enum ib_flow_spec_type header_spec =
  1416. ((union ib_flow_spec *)(flow_attr + 1))->type;
  1417. if (header_spec == IB_FLOW_SPEC_ETH)
  1418. mlx4_handle_eth_header_mcast_prio(ctrl, rule_header);
  1419. }
  1420. ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
  1421. MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
  1422. MLX4_CMD_NATIVE);
  1423. if (ret == -ENOMEM)
  1424. pr_err("mcg table is full. Fail to register network rule.\n");
  1425. else if (ret == -ENXIO)
  1426. pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
  1427. else if (ret)
  1428. pr_err("Invalid argument. Fail to register network rule.\n");
  1429. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  1430. return ret;
  1431. }
  1432. static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
  1433. {
  1434. int err;
  1435. err = mlx4_cmd(dev, reg_id, 0, 0,
  1436. MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
  1437. MLX4_CMD_NATIVE);
  1438. if (err)
  1439. pr_err("Fail to detach network rule. registration id = 0x%llx\n",
  1440. reg_id);
  1441. return err;
  1442. }
  1443. static int mlx4_ib_tunnel_steer_add(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
  1444. u64 *reg_id)
  1445. {
  1446. void *ib_flow;
  1447. union ib_flow_spec *ib_spec;
  1448. struct mlx4_dev *dev = to_mdev(qp->device)->dev;
  1449. int err = 0;
  1450. if (dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
  1451. dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
  1452. return 0; /* do nothing */
  1453. ib_flow = flow_attr + 1;
  1454. ib_spec = (union ib_flow_spec *)ib_flow;
  1455. if (ib_spec->type != IB_FLOW_SPEC_ETH || flow_attr->num_of_specs != 1)
  1456. return 0; /* do nothing */
  1457. err = mlx4_tunnel_steer_add(to_mdev(qp->device)->dev, ib_spec->eth.val.dst_mac,
  1458. flow_attr->port, qp->qp_num,
  1459. MLX4_DOMAIN_UVERBS | (flow_attr->priority & 0xff),
  1460. reg_id);
  1461. return err;
  1462. }
  1463. static int mlx4_ib_add_dont_trap_rule(struct mlx4_dev *dev,
  1464. struct ib_flow_attr *flow_attr,
  1465. enum mlx4_net_trans_promisc_mode *type)
  1466. {
  1467. int err = 0;
  1468. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_UC_MC_SNIFFER) ||
  1469. (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC) ||
  1470. (flow_attr->num_of_specs > 1) || (flow_attr->priority != 0)) {
  1471. return -EOPNOTSUPP;
  1472. }
  1473. if (flow_attr->num_of_specs == 0) {
  1474. type[0] = MLX4_FS_MC_SNIFFER;
  1475. type[1] = MLX4_FS_UC_SNIFFER;
  1476. } else {
  1477. union ib_flow_spec *ib_spec;
  1478. ib_spec = (union ib_flow_spec *)(flow_attr + 1);
  1479. if (ib_spec->type != IB_FLOW_SPEC_ETH)
  1480. return -EINVAL;
  1481. /* if all is zero than MC and UC */
  1482. if (is_zero_ether_addr(ib_spec->eth.mask.dst_mac)) {
  1483. type[0] = MLX4_FS_MC_SNIFFER;
  1484. type[1] = MLX4_FS_UC_SNIFFER;
  1485. } else {
  1486. u8 mac[ETH_ALEN] = {ib_spec->eth.mask.dst_mac[0] ^ 0x01,
  1487. ib_spec->eth.mask.dst_mac[1],
  1488. ib_spec->eth.mask.dst_mac[2],
  1489. ib_spec->eth.mask.dst_mac[3],
  1490. ib_spec->eth.mask.dst_mac[4],
  1491. ib_spec->eth.mask.dst_mac[5]};
  1492. /* Above xor was only on MC bit, non empty mask is valid
  1493. * only if this bit is set and rest are zero.
  1494. */
  1495. if (!is_zero_ether_addr(&mac[0]))
  1496. return -EINVAL;
  1497. if (is_multicast_ether_addr(ib_spec->eth.val.dst_mac))
  1498. type[0] = MLX4_FS_MC_SNIFFER;
  1499. else
  1500. type[0] = MLX4_FS_UC_SNIFFER;
  1501. }
  1502. }
  1503. return err;
  1504. }
  1505. static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
  1506. struct ib_flow_attr *flow_attr,
  1507. int domain)
  1508. {
  1509. int err = 0, i = 0, j = 0;
  1510. struct mlx4_ib_flow *mflow;
  1511. enum mlx4_net_trans_promisc_mode type[2];
  1512. struct mlx4_dev *dev = (to_mdev(qp->device))->dev;
  1513. int is_bonded = mlx4_is_bonded(dev);
  1514. if (flow_attr->port < 1 || flow_attr->port > qp->device->phys_port_cnt)
  1515. return ERR_PTR(-EINVAL);
  1516. if ((flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
  1517. (flow_attr->type != IB_FLOW_ATTR_NORMAL))
  1518. return ERR_PTR(-EOPNOTSUPP);
  1519. memset(type, 0, sizeof(type));
  1520. mflow = kzalloc(sizeof(*mflow), GFP_KERNEL);
  1521. if (!mflow) {
  1522. err = -ENOMEM;
  1523. goto err_free;
  1524. }
  1525. switch (flow_attr->type) {
  1526. case IB_FLOW_ATTR_NORMAL:
  1527. /* If dont trap flag (continue match) is set, under specific
  1528. * condition traffic be replicated to given qp,
  1529. * without stealing it
  1530. */
  1531. if (unlikely(flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)) {
  1532. err = mlx4_ib_add_dont_trap_rule(dev,
  1533. flow_attr,
  1534. type);
  1535. if (err)
  1536. goto err_free;
  1537. } else {
  1538. type[0] = MLX4_FS_REGULAR;
  1539. }
  1540. break;
  1541. case IB_FLOW_ATTR_ALL_DEFAULT:
  1542. type[0] = MLX4_FS_ALL_DEFAULT;
  1543. break;
  1544. case IB_FLOW_ATTR_MC_DEFAULT:
  1545. type[0] = MLX4_FS_MC_DEFAULT;
  1546. break;
  1547. case IB_FLOW_ATTR_SNIFFER:
  1548. type[0] = MLX4_FS_MIRROR_RX_PORT;
  1549. type[1] = MLX4_FS_MIRROR_SX_PORT;
  1550. break;
  1551. default:
  1552. err = -EINVAL;
  1553. goto err_free;
  1554. }
  1555. while (i < ARRAY_SIZE(type) && type[i]) {
  1556. err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
  1557. &mflow->reg_id[i].id);
  1558. if (err)
  1559. goto err_create_flow;
  1560. if (is_bonded) {
  1561. /* Application always sees one port so the mirror rule
  1562. * must be on port #2
  1563. */
  1564. flow_attr->port = 2;
  1565. err = __mlx4_ib_create_flow(qp, flow_attr,
  1566. domain, type[j],
  1567. &mflow->reg_id[j].mirror);
  1568. flow_attr->port = 1;
  1569. if (err)
  1570. goto err_create_flow;
  1571. j++;
  1572. }
  1573. i++;
  1574. }
  1575. if (i < ARRAY_SIZE(type) && flow_attr->type == IB_FLOW_ATTR_NORMAL) {
  1576. err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
  1577. &mflow->reg_id[i].id);
  1578. if (err)
  1579. goto err_create_flow;
  1580. if (is_bonded) {
  1581. flow_attr->port = 2;
  1582. err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
  1583. &mflow->reg_id[j].mirror);
  1584. flow_attr->port = 1;
  1585. if (err)
  1586. goto err_create_flow;
  1587. j++;
  1588. }
  1589. /* function to create mirror rule */
  1590. i++;
  1591. }
  1592. return &mflow->ibflow;
  1593. err_create_flow:
  1594. while (i) {
  1595. (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
  1596. mflow->reg_id[i].id);
  1597. i--;
  1598. }
  1599. while (j) {
  1600. (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
  1601. mflow->reg_id[j].mirror);
  1602. j--;
  1603. }
  1604. err_free:
  1605. kfree(mflow);
  1606. return ERR_PTR(err);
  1607. }
  1608. static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
  1609. {
  1610. int err, ret = 0;
  1611. int i = 0;
  1612. struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
  1613. struct mlx4_ib_flow *mflow = to_mflow(flow_id);
  1614. while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i].id) {
  1615. err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i].id);
  1616. if (err)
  1617. ret = err;
  1618. if (mflow->reg_id[i].mirror) {
  1619. err = __mlx4_ib_destroy_flow(mdev->dev,
  1620. mflow->reg_id[i].mirror);
  1621. if (err)
  1622. ret = err;
  1623. }
  1624. i++;
  1625. }
  1626. kfree(mflow);
  1627. return ret;
  1628. }
  1629. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  1630. {
  1631. int err;
  1632. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  1633. struct mlx4_dev *dev = mdev->dev;
  1634. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1635. struct mlx4_ib_steering *ib_steering = NULL;
  1636. enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
  1637. struct mlx4_flow_reg_id reg_id;
  1638. if (mdev->dev->caps.steering_mode ==
  1639. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1640. ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
  1641. if (!ib_steering)
  1642. return -ENOMEM;
  1643. }
  1644. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
  1645. !!(mqp->flags &
  1646. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  1647. prot, &reg_id.id);
  1648. if (err) {
  1649. pr_err("multicast attach op failed, err %d\n", err);
  1650. goto err_malloc;
  1651. }
  1652. reg_id.mirror = 0;
  1653. if (mlx4_is_bonded(dev)) {
  1654. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
  1655. (mqp->port == 1) ? 2 : 1,
  1656. !!(mqp->flags &
  1657. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  1658. prot, &reg_id.mirror);
  1659. if (err)
  1660. goto err_add;
  1661. }
  1662. err = add_gid_entry(ibqp, gid);
  1663. if (err)
  1664. goto err_add;
  1665. if (ib_steering) {
  1666. memcpy(ib_steering->gid.raw, gid->raw, 16);
  1667. ib_steering->reg_id = reg_id;
  1668. mutex_lock(&mqp->mutex);
  1669. list_add(&ib_steering->list, &mqp->steering_rules);
  1670. mutex_unlock(&mqp->mutex);
  1671. }
  1672. return 0;
  1673. err_add:
  1674. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1675. prot, reg_id.id);
  1676. if (reg_id.mirror)
  1677. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1678. prot, reg_id.mirror);
  1679. err_malloc:
  1680. kfree(ib_steering);
  1681. return err;
  1682. }
  1683. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  1684. {
  1685. struct mlx4_ib_gid_entry *ge;
  1686. struct mlx4_ib_gid_entry *tmp;
  1687. struct mlx4_ib_gid_entry *ret = NULL;
  1688. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1689. if (!memcmp(raw, ge->gid.raw, 16)) {
  1690. ret = ge;
  1691. break;
  1692. }
  1693. }
  1694. return ret;
  1695. }
  1696. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  1697. {
  1698. int err;
  1699. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  1700. struct mlx4_dev *dev = mdev->dev;
  1701. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1702. struct net_device *ndev;
  1703. struct mlx4_ib_gid_entry *ge;
  1704. struct mlx4_flow_reg_id reg_id = {0, 0};
  1705. enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
  1706. if (mdev->dev->caps.steering_mode ==
  1707. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1708. struct mlx4_ib_steering *ib_steering;
  1709. mutex_lock(&mqp->mutex);
  1710. list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
  1711. if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
  1712. list_del(&ib_steering->list);
  1713. break;
  1714. }
  1715. }
  1716. mutex_unlock(&mqp->mutex);
  1717. if (&ib_steering->list == &mqp->steering_rules) {
  1718. pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
  1719. return -EINVAL;
  1720. }
  1721. reg_id = ib_steering->reg_id;
  1722. kfree(ib_steering);
  1723. }
  1724. err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1725. prot, reg_id.id);
  1726. if (err)
  1727. return err;
  1728. if (mlx4_is_bonded(dev)) {
  1729. err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  1730. prot, reg_id.mirror);
  1731. if (err)
  1732. return err;
  1733. }
  1734. mutex_lock(&mqp->mutex);
  1735. ge = find_gid_entry(mqp, gid->raw);
  1736. if (ge) {
  1737. spin_lock_bh(&mdev->iboe.lock);
  1738. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  1739. if (ndev)
  1740. dev_hold(ndev);
  1741. spin_unlock_bh(&mdev->iboe.lock);
  1742. if (ndev)
  1743. dev_put(ndev);
  1744. list_del(&ge->list);
  1745. kfree(ge);
  1746. } else
  1747. pr_warn("could not find mgid entry\n");
  1748. mutex_unlock(&mqp->mutex);
  1749. return 0;
  1750. }
  1751. static int init_node_data(struct mlx4_ib_dev *dev)
  1752. {
  1753. struct ib_smp *in_mad = NULL;
  1754. struct ib_smp *out_mad = NULL;
  1755. int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
  1756. int err = -ENOMEM;
  1757. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  1758. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  1759. if (!in_mad || !out_mad)
  1760. goto out;
  1761. init_query_mad(in_mad);
  1762. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  1763. if (mlx4_is_master(dev->dev))
  1764. mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
  1765. err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
  1766. if (err)
  1767. goto out;
  1768. memcpy(dev->ib_dev.node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
  1769. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  1770. err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
  1771. if (err)
  1772. goto out;
  1773. dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  1774. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  1775. out:
  1776. kfree(in_mad);
  1777. kfree(out_mad);
  1778. return err;
  1779. }
  1780. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  1781. char *buf)
  1782. {
  1783. struct mlx4_ib_dev *dev =
  1784. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1785. return sprintf(buf, "MT%d\n", dev->dev->persist->pdev->device);
  1786. }
  1787. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  1788. char *buf)
  1789. {
  1790. struct mlx4_ib_dev *dev =
  1791. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1792. return sprintf(buf, "%x\n", dev->dev->rev_id);
  1793. }
  1794. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  1795. char *buf)
  1796. {
  1797. struct mlx4_ib_dev *dev =
  1798. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  1799. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  1800. dev->dev->board_id);
  1801. }
  1802. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  1803. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  1804. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  1805. static struct device_attribute *mlx4_class_attributes[] = {
  1806. &dev_attr_hw_rev,
  1807. &dev_attr_hca_type,
  1808. &dev_attr_board_id
  1809. };
  1810. struct diag_counter {
  1811. const char *name;
  1812. u32 offset;
  1813. };
  1814. #define DIAG_COUNTER(_name, _offset) \
  1815. { .name = #_name, .offset = _offset }
  1816. static const struct diag_counter diag_basic[] = {
  1817. DIAG_COUNTER(rq_num_lle, 0x00),
  1818. DIAG_COUNTER(sq_num_lle, 0x04),
  1819. DIAG_COUNTER(rq_num_lqpoe, 0x08),
  1820. DIAG_COUNTER(sq_num_lqpoe, 0x0C),
  1821. DIAG_COUNTER(rq_num_lpe, 0x18),
  1822. DIAG_COUNTER(sq_num_lpe, 0x1C),
  1823. DIAG_COUNTER(rq_num_wrfe, 0x20),
  1824. DIAG_COUNTER(sq_num_wrfe, 0x24),
  1825. DIAG_COUNTER(sq_num_mwbe, 0x2C),
  1826. DIAG_COUNTER(sq_num_bre, 0x34),
  1827. DIAG_COUNTER(sq_num_rire, 0x44),
  1828. DIAG_COUNTER(rq_num_rire, 0x48),
  1829. DIAG_COUNTER(sq_num_rae, 0x4C),
  1830. DIAG_COUNTER(rq_num_rae, 0x50),
  1831. DIAG_COUNTER(sq_num_roe, 0x54),
  1832. DIAG_COUNTER(sq_num_tree, 0x5C),
  1833. DIAG_COUNTER(sq_num_rree, 0x64),
  1834. DIAG_COUNTER(rq_num_rnr, 0x68),
  1835. DIAG_COUNTER(sq_num_rnr, 0x6C),
  1836. DIAG_COUNTER(rq_num_oos, 0x100),
  1837. DIAG_COUNTER(sq_num_oos, 0x104),
  1838. };
  1839. static const struct diag_counter diag_ext[] = {
  1840. DIAG_COUNTER(rq_num_dup, 0x130),
  1841. DIAG_COUNTER(sq_num_to, 0x134),
  1842. };
  1843. static const struct diag_counter diag_device_only[] = {
  1844. DIAG_COUNTER(num_cqovf, 0x1A0),
  1845. DIAG_COUNTER(rq_num_udsdprd, 0x118),
  1846. };
  1847. static struct rdma_hw_stats *mlx4_ib_alloc_hw_stats(struct ib_device *ibdev,
  1848. u8 port_num)
  1849. {
  1850. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  1851. struct mlx4_ib_diag_counters *diag = dev->diag_counters;
  1852. if (!diag[!!port_num].name)
  1853. return NULL;
  1854. return rdma_alloc_hw_stats_struct(diag[!!port_num].name,
  1855. diag[!!port_num].num_counters,
  1856. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1857. }
  1858. static int mlx4_ib_get_hw_stats(struct ib_device *ibdev,
  1859. struct rdma_hw_stats *stats,
  1860. u8 port, int index)
  1861. {
  1862. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  1863. struct mlx4_ib_diag_counters *diag = dev->diag_counters;
  1864. u32 hw_value[ARRAY_SIZE(diag_device_only) +
  1865. ARRAY_SIZE(diag_ext) + ARRAY_SIZE(diag_basic)] = {};
  1866. int ret;
  1867. int i;
  1868. ret = mlx4_query_diag_counters(dev->dev,
  1869. MLX4_OP_MOD_QUERY_TRANSPORT_CI_ERRORS,
  1870. diag[!!port].offset, hw_value,
  1871. diag[!!port].num_counters, port);
  1872. if (ret)
  1873. return ret;
  1874. for (i = 0; i < diag[!!port].num_counters; i++)
  1875. stats->value[i] = hw_value[i];
  1876. return diag[!!port].num_counters;
  1877. }
  1878. static int __mlx4_ib_alloc_diag_counters(struct mlx4_ib_dev *ibdev,
  1879. const char ***name,
  1880. u32 **offset,
  1881. u32 *num,
  1882. bool port)
  1883. {
  1884. u32 num_counters;
  1885. num_counters = ARRAY_SIZE(diag_basic);
  1886. if (ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DIAG_PER_PORT)
  1887. num_counters += ARRAY_SIZE(diag_ext);
  1888. if (!port)
  1889. num_counters += ARRAY_SIZE(diag_device_only);
  1890. *name = kcalloc(num_counters, sizeof(**name), GFP_KERNEL);
  1891. if (!*name)
  1892. return -ENOMEM;
  1893. *offset = kcalloc(num_counters, sizeof(**offset), GFP_KERNEL);
  1894. if (!*offset)
  1895. goto err_name;
  1896. *num = num_counters;
  1897. return 0;
  1898. err_name:
  1899. kfree(*name);
  1900. return -ENOMEM;
  1901. }
  1902. static void mlx4_ib_fill_diag_counters(struct mlx4_ib_dev *ibdev,
  1903. const char **name,
  1904. u32 *offset,
  1905. bool port)
  1906. {
  1907. int i;
  1908. int j;
  1909. for (i = 0, j = 0; i < ARRAY_SIZE(diag_basic); i++, j++) {
  1910. name[i] = diag_basic[i].name;
  1911. offset[i] = diag_basic[i].offset;
  1912. }
  1913. if (ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DIAG_PER_PORT) {
  1914. for (i = 0; i < ARRAY_SIZE(diag_ext); i++, j++) {
  1915. name[j] = diag_ext[i].name;
  1916. offset[j] = diag_ext[i].offset;
  1917. }
  1918. }
  1919. if (!port) {
  1920. for (i = 0; i < ARRAY_SIZE(diag_device_only); i++, j++) {
  1921. name[j] = diag_device_only[i].name;
  1922. offset[j] = diag_device_only[i].offset;
  1923. }
  1924. }
  1925. }
  1926. static int mlx4_ib_alloc_diag_counters(struct mlx4_ib_dev *ibdev)
  1927. {
  1928. struct mlx4_ib_diag_counters *diag = ibdev->diag_counters;
  1929. int i;
  1930. int ret;
  1931. bool per_port = !!(ibdev->dev->caps.flags2 &
  1932. MLX4_DEV_CAP_FLAG2_DIAG_PER_PORT);
  1933. if (mlx4_is_slave(ibdev->dev))
  1934. return 0;
  1935. for (i = 0; i < MLX4_DIAG_COUNTERS_TYPES; i++) {
  1936. /* i == 1 means we are building port counters */
  1937. if (i && !per_port)
  1938. continue;
  1939. ret = __mlx4_ib_alloc_diag_counters(ibdev, &diag[i].name,
  1940. &diag[i].offset,
  1941. &diag[i].num_counters, i);
  1942. if (ret)
  1943. goto err_alloc;
  1944. mlx4_ib_fill_diag_counters(ibdev, diag[i].name,
  1945. diag[i].offset, i);
  1946. }
  1947. ibdev->ib_dev.get_hw_stats = mlx4_ib_get_hw_stats;
  1948. ibdev->ib_dev.alloc_hw_stats = mlx4_ib_alloc_hw_stats;
  1949. return 0;
  1950. err_alloc:
  1951. if (i) {
  1952. kfree(diag[i - 1].name);
  1953. kfree(diag[i - 1].offset);
  1954. }
  1955. return ret;
  1956. }
  1957. static void mlx4_ib_diag_cleanup(struct mlx4_ib_dev *ibdev)
  1958. {
  1959. int i;
  1960. for (i = 0; i < MLX4_DIAG_COUNTERS_TYPES; i++) {
  1961. kfree(ibdev->diag_counters[i].offset);
  1962. kfree(ibdev->diag_counters[i].name);
  1963. }
  1964. }
  1965. #define MLX4_IB_INVALID_MAC ((u64)-1)
  1966. static void mlx4_ib_update_qps(struct mlx4_ib_dev *ibdev,
  1967. struct net_device *dev,
  1968. int port)
  1969. {
  1970. u64 new_smac = 0;
  1971. u64 release_mac = MLX4_IB_INVALID_MAC;
  1972. struct mlx4_ib_qp *qp;
  1973. read_lock(&dev_base_lock);
  1974. new_smac = mlx4_mac_to_u64(dev->dev_addr);
  1975. read_unlock(&dev_base_lock);
  1976. atomic64_set(&ibdev->iboe.mac[port - 1], new_smac);
  1977. /* no need for update QP1 and mac registration in non-SRIOV */
  1978. if (!mlx4_is_mfunc(ibdev->dev))
  1979. return;
  1980. mutex_lock(&ibdev->qp1_proxy_lock[port - 1]);
  1981. qp = ibdev->qp1_proxy[port - 1];
  1982. if (qp) {
  1983. int new_smac_index;
  1984. u64 old_smac;
  1985. struct mlx4_update_qp_params update_params;
  1986. mutex_lock(&qp->mutex);
  1987. old_smac = qp->pri.smac;
  1988. if (new_smac == old_smac)
  1989. goto unlock;
  1990. new_smac_index = mlx4_register_mac(ibdev->dev, port, new_smac);
  1991. if (new_smac_index < 0)
  1992. goto unlock;
  1993. update_params.smac_index = new_smac_index;
  1994. if (mlx4_update_qp(ibdev->dev, qp->mqp.qpn, MLX4_UPDATE_QP_SMAC,
  1995. &update_params)) {
  1996. release_mac = new_smac;
  1997. goto unlock;
  1998. }
  1999. /* if old port was zero, no mac was yet registered for this QP */
  2000. if (qp->pri.smac_port)
  2001. release_mac = old_smac;
  2002. qp->pri.smac = new_smac;
  2003. qp->pri.smac_port = port;
  2004. qp->pri.smac_index = new_smac_index;
  2005. }
  2006. unlock:
  2007. if (release_mac != MLX4_IB_INVALID_MAC)
  2008. mlx4_unregister_mac(ibdev->dev, port, release_mac);
  2009. if (qp)
  2010. mutex_unlock(&qp->mutex);
  2011. mutex_unlock(&ibdev->qp1_proxy_lock[port - 1]);
  2012. }
  2013. static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev,
  2014. struct net_device *dev,
  2015. unsigned long event)
  2016. {
  2017. struct mlx4_ib_iboe *iboe;
  2018. int update_qps_port = -1;
  2019. int port;
  2020. ASSERT_RTNL();
  2021. iboe = &ibdev->iboe;
  2022. spin_lock_bh(&iboe->lock);
  2023. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  2024. iboe->netdevs[port - 1] =
  2025. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
  2026. if (dev == iboe->netdevs[port - 1] &&
  2027. (event == NETDEV_CHANGEADDR || event == NETDEV_REGISTER ||
  2028. event == NETDEV_UP || event == NETDEV_CHANGE))
  2029. update_qps_port = port;
  2030. }
  2031. spin_unlock_bh(&iboe->lock);
  2032. if (update_qps_port > 0)
  2033. mlx4_ib_update_qps(ibdev, dev, update_qps_port);
  2034. }
  2035. static int mlx4_ib_netdev_event(struct notifier_block *this,
  2036. unsigned long event, void *ptr)
  2037. {
  2038. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2039. struct mlx4_ib_dev *ibdev;
  2040. if (!net_eq(dev_net(dev), &init_net))
  2041. return NOTIFY_DONE;
  2042. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  2043. mlx4_ib_scan_netdevs(ibdev, dev, event);
  2044. return NOTIFY_DONE;
  2045. }
  2046. static void init_pkeys(struct mlx4_ib_dev *ibdev)
  2047. {
  2048. int port;
  2049. int slave;
  2050. int i;
  2051. if (mlx4_is_master(ibdev->dev)) {
  2052. for (slave = 0; slave <= ibdev->dev->persist->num_vfs;
  2053. ++slave) {
  2054. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
  2055. for (i = 0;
  2056. i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
  2057. ++i) {
  2058. ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
  2059. /* master has the identity virt2phys pkey mapping */
  2060. (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
  2061. ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
  2062. mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
  2063. ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
  2064. }
  2065. }
  2066. }
  2067. /* initialize pkey cache */
  2068. for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
  2069. for (i = 0;
  2070. i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
  2071. ++i)
  2072. ibdev->pkeys.phys_pkey_cache[port-1][i] =
  2073. (i) ? 0 : 0xFFFF;
  2074. }
  2075. }
  2076. }
  2077. static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  2078. {
  2079. int i, j, eq = 0, total_eqs = 0;
  2080. ibdev->eq_table = kcalloc(dev->caps.num_comp_vectors,
  2081. sizeof(ibdev->eq_table[0]), GFP_KERNEL);
  2082. if (!ibdev->eq_table)
  2083. return;
  2084. for (i = 1; i <= dev->caps.num_ports; i++) {
  2085. for (j = 0; j < mlx4_get_eqs_per_port(dev, i);
  2086. j++, total_eqs++) {
  2087. if (i > 1 && mlx4_is_eq_shared(dev, total_eqs))
  2088. continue;
  2089. ibdev->eq_table[eq] = total_eqs;
  2090. if (!mlx4_assign_eq(dev, i,
  2091. &ibdev->eq_table[eq]))
  2092. eq++;
  2093. else
  2094. ibdev->eq_table[eq] = -1;
  2095. }
  2096. }
  2097. for (i = eq; i < dev->caps.num_comp_vectors;
  2098. ibdev->eq_table[i++] = -1)
  2099. ;
  2100. /* Advertise the new number of EQs to clients */
  2101. ibdev->ib_dev.num_comp_vectors = eq;
  2102. }
  2103. static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  2104. {
  2105. int i;
  2106. int total_eqs = ibdev->ib_dev.num_comp_vectors;
  2107. /* no eqs were allocated */
  2108. if (!ibdev->eq_table)
  2109. return;
  2110. /* Reset the advertised EQ number */
  2111. ibdev->ib_dev.num_comp_vectors = 0;
  2112. for (i = 0; i < total_eqs; i++)
  2113. mlx4_release_eq(dev, ibdev->eq_table[i]);
  2114. kfree(ibdev->eq_table);
  2115. ibdev->eq_table = NULL;
  2116. }
  2117. static int mlx4_port_immutable(struct ib_device *ibdev, u8 port_num,
  2118. struct ib_port_immutable *immutable)
  2119. {
  2120. struct ib_port_attr attr;
  2121. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  2122. int err;
  2123. err = mlx4_ib_query_port(ibdev, port_num, &attr);
  2124. if (err)
  2125. return err;
  2126. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  2127. immutable->gid_tbl_len = attr.gid_tbl_len;
  2128. if (mlx4_ib_port_link_layer(ibdev, port_num) == IB_LINK_LAYER_INFINIBAND) {
  2129. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
  2130. } else {
  2131. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE)
  2132. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
  2133. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
  2134. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE |
  2135. RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
  2136. }
  2137. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  2138. return 0;
  2139. }
  2140. static void get_fw_ver_str(struct ib_device *device, char *str,
  2141. size_t str_len)
  2142. {
  2143. struct mlx4_ib_dev *dev =
  2144. container_of(device, struct mlx4_ib_dev, ib_dev);
  2145. snprintf(str, str_len, "%d.%d.%d",
  2146. (int) (dev->dev->caps.fw_ver >> 32),
  2147. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  2148. (int) dev->dev->caps.fw_ver & 0xffff);
  2149. }
  2150. static void *mlx4_ib_add(struct mlx4_dev *dev)
  2151. {
  2152. struct mlx4_ib_dev *ibdev;
  2153. int num_ports = 0;
  2154. int i, j;
  2155. int err;
  2156. struct mlx4_ib_iboe *iboe;
  2157. int ib_num_ports = 0;
  2158. int num_req_counters;
  2159. int allocated;
  2160. u32 counter_index;
  2161. struct counter_index *new_counter_index = NULL;
  2162. pr_info_once("%s", mlx4_ib_version);
  2163. num_ports = 0;
  2164. mlx4_foreach_ib_transport_port(i, dev)
  2165. num_ports++;
  2166. /* No point in registering a device with no ports... */
  2167. if (num_ports == 0)
  2168. return NULL;
  2169. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  2170. if (!ibdev) {
  2171. dev_err(&dev->persist->pdev->dev,
  2172. "Device struct alloc failed\n");
  2173. return NULL;
  2174. }
  2175. iboe = &ibdev->iboe;
  2176. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  2177. goto err_dealloc;
  2178. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  2179. goto err_pd;
  2180. ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
  2181. PAGE_SIZE);
  2182. if (!ibdev->uar_map)
  2183. goto err_uar;
  2184. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  2185. ibdev->dev = dev;
  2186. ibdev->bond_next_port = 0;
  2187. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  2188. ibdev->ib_dev.owner = THIS_MODULE;
  2189. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  2190. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  2191. ibdev->num_ports = num_ports;
  2192. ibdev->ib_dev.phys_port_cnt = mlx4_is_bonded(dev) ?
  2193. 1 : ibdev->num_ports;
  2194. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  2195. ibdev->ib_dev.dma_device = &dev->persist->pdev->dev;
  2196. ibdev->ib_dev.get_netdev = mlx4_ib_get_netdev;
  2197. ibdev->ib_dev.add_gid = mlx4_ib_add_gid;
  2198. ibdev->ib_dev.del_gid = mlx4_ib_del_gid;
  2199. if (dev->caps.userspace_caps)
  2200. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  2201. else
  2202. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
  2203. ibdev->ib_dev.uverbs_cmd_mask =
  2204. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  2205. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  2206. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  2207. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  2208. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  2209. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  2210. (1ull << IB_USER_VERBS_CMD_REREG_MR) |
  2211. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  2212. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  2213. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  2214. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  2215. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  2216. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  2217. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  2218. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  2219. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  2220. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  2221. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  2222. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  2223. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  2224. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  2225. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  2226. (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
  2227. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  2228. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  2229. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  2230. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  2231. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  2232. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  2233. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  2234. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  2235. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  2236. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  2237. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  2238. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  2239. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  2240. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  2241. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  2242. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  2243. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  2244. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  2245. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  2246. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  2247. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  2248. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  2249. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  2250. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  2251. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  2252. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  2253. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  2254. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  2255. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  2256. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  2257. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  2258. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  2259. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  2260. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  2261. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  2262. ibdev->ib_dev.rereg_user_mr = mlx4_ib_rereg_user_mr;
  2263. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  2264. ibdev->ib_dev.alloc_mr = mlx4_ib_alloc_mr;
  2265. ibdev->ib_dev.map_mr_sg = mlx4_ib_map_mr_sg;
  2266. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  2267. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  2268. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  2269. ibdev->ib_dev.get_port_immutable = mlx4_port_immutable;
  2270. ibdev->ib_dev.get_dev_fw_str = get_fw_ver_str;
  2271. ibdev->ib_dev.disassociate_ucontext = mlx4_ib_disassociate_ucontext;
  2272. if (!mlx4_is_slave(ibdev->dev)) {
  2273. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  2274. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  2275. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  2276. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  2277. }
  2278. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
  2279. dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
  2280. ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
  2281. ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
  2282. ibdev->ib_dev.uverbs_cmd_mask |=
  2283. (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
  2284. (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
  2285. }
  2286. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
  2287. ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
  2288. ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
  2289. ibdev->ib_dev.uverbs_cmd_mask |=
  2290. (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
  2291. (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
  2292. }
  2293. if (check_flow_steering_support(dev)) {
  2294. ibdev->steering_support = MLX4_STEERING_MODE_DEVICE_MANAGED;
  2295. ibdev->ib_dev.create_flow = mlx4_ib_create_flow;
  2296. ibdev->ib_dev.destroy_flow = mlx4_ib_destroy_flow;
  2297. ibdev->ib_dev.uverbs_ex_cmd_mask |=
  2298. (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
  2299. (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
  2300. }
  2301. ibdev->ib_dev.uverbs_ex_cmd_mask |=
  2302. (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
  2303. (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
  2304. (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
  2305. mlx4_ib_alloc_eqs(dev, ibdev);
  2306. spin_lock_init(&iboe->lock);
  2307. if (init_node_data(ibdev))
  2308. goto err_map;
  2309. mlx4_init_sl2vl_tbl(ibdev);
  2310. for (i = 0; i < ibdev->num_ports; ++i) {
  2311. mutex_init(&ibdev->counters_table[i].mutex);
  2312. INIT_LIST_HEAD(&ibdev->counters_table[i].counters_list);
  2313. }
  2314. num_req_counters = mlx4_is_bonded(dev) ? 1 : ibdev->num_ports;
  2315. for (i = 0; i < num_req_counters; ++i) {
  2316. mutex_init(&ibdev->qp1_proxy_lock[i]);
  2317. allocated = 0;
  2318. if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
  2319. IB_LINK_LAYER_ETHERNET) {
  2320. err = mlx4_counter_alloc(ibdev->dev, &counter_index);
  2321. /* if failed to allocate a new counter, use default */
  2322. if (err)
  2323. counter_index =
  2324. mlx4_get_default_counter_index(dev,
  2325. i + 1);
  2326. else
  2327. allocated = 1;
  2328. } else { /* IB_LINK_LAYER_INFINIBAND use the default counter */
  2329. counter_index = mlx4_get_default_counter_index(dev,
  2330. i + 1);
  2331. }
  2332. new_counter_index = kmalloc(sizeof(*new_counter_index),
  2333. GFP_KERNEL);
  2334. if (!new_counter_index) {
  2335. if (allocated)
  2336. mlx4_counter_free(ibdev->dev, counter_index);
  2337. goto err_counter;
  2338. }
  2339. new_counter_index->index = counter_index;
  2340. new_counter_index->allocated = allocated;
  2341. list_add_tail(&new_counter_index->list,
  2342. &ibdev->counters_table[i].counters_list);
  2343. ibdev->counters_table[i].default_counter = counter_index;
  2344. pr_info("counter index %d for port %d allocated %d\n",
  2345. counter_index, i + 1, allocated);
  2346. }
  2347. if (mlx4_is_bonded(dev))
  2348. for (i = 1; i < ibdev->num_ports ; ++i) {
  2349. new_counter_index =
  2350. kmalloc(sizeof(struct counter_index),
  2351. GFP_KERNEL);
  2352. if (!new_counter_index)
  2353. goto err_counter;
  2354. new_counter_index->index = counter_index;
  2355. new_counter_index->allocated = 0;
  2356. list_add_tail(&new_counter_index->list,
  2357. &ibdev->counters_table[i].counters_list);
  2358. ibdev->counters_table[i].default_counter =
  2359. counter_index;
  2360. }
  2361. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  2362. ib_num_ports++;
  2363. spin_lock_init(&ibdev->sm_lock);
  2364. mutex_init(&ibdev->cap_mask_mutex);
  2365. INIT_LIST_HEAD(&ibdev->qp_list);
  2366. spin_lock_init(&ibdev->reset_flow_resource_lock);
  2367. if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED &&
  2368. ib_num_ports) {
  2369. ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
  2370. err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
  2371. MLX4_IB_UC_STEER_QPN_ALIGN,
  2372. &ibdev->steer_qpn_base, 0);
  2373. if (err)
  2374. goto err_counter;
  2375. ibdev->ib_uc_qpns_bitmap =
  2376. kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
  2377. sizeof(long),
  2378. GFP_KERNEL);
  2379. if (!ibdev->ib_uc_qpns_bitmap) {
  2380. dev_err(&dev->persist->pdev->dev,
  2381. "bit map alloc failed\n");
  2382. goto err_steer_qp_release;
  2383. }
  2384. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB) {
  2385. bitmap_zero(ibdev->ib_uc_qpns_bitmap,
  2386. ibdev->steer_qpn_count);
  2387. err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
  2388. dev, ibdev->steer_qpn_base,
  2389. ibdev->steer_qpn_base +
  2390. ibdev->steer_qpn_count - 1);
  2391. if (err)
  2392. goto err_steer_free_bitmap;
  2393. } else {
  2394. bitmap_fill(ibdev->ib_uc_qpns_bitmap,
  2395. ibdev->steer_qpn_count);
  2396. }
  2397. }
  2398. for (j = 1; j <= ibdev->dev->caps.num_ports; j++)
  2399. atomic64_set(&iboe->mac[j - 1], ibdev->dev->caps.def_mac[j]);
  2400. if (mlx4_ib_alloc_diag_counters(ibdev))
  2401. goto err_steer_free_bitmap;
  2402. if (ib_register_device(&ibdev->ib_dev, NULL))
  2403. goto err_diag_counters;
  2404. if (mlx4_ib_mad_init(ibdev))
  2405. goto err_reg;
  2406. if (mlx4_ib_init_sriov(ibdev))
  2407. goto err_mad;
  2408. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE ||
  2409. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  2410. if (!iboe->nb.notifier_call) {
  2411. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  2412. err = register_netdevice_notifier(&iboe->nb);
  2413. if (err) {
  2414. iboe->nb.notifier_call = NULL;
  2415. goto err_notif;
  2416. }
  2417. }
  2418. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  2419. err = mlx4_config_roce_v2_port(dev, ROCE_V2_UDP_DPORT);
  2420. if (err) {
  2421. goto err_notif;
  2422. }
  2423. }
  2424. }
  2425. for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
  2426. if (device_create_file(&ibdev->ib_dev.dev,
  2427. mlx4_class_attributes[j]))
  2428. goto err_notif;
  2429. }
  2430. ibdev->ib_active = true;
  2431. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  2432. devlink_port_type_ib_set(mlx4_get_devlink_port(dev, i),
  2433. &ibdev->ib_dev);
  2434. if (mlx4_is_mfunc(ibdev->dev))
  2435. init_pkeys(ibdev);
  2436. /* create paravirt contexts for any VFs which are active */
  2437. if (mlx4_is_master(ibdev->dev)) {
  2438. for (j = 0; j < MLX4_MFUNC_MAX; j++) {
  2439. if (j == mlx4_master_func_num(ibdev->dev))
  2440. continue;
  2441. if (mlx4_is_slave_active(ibdev->dev, j))
  2442. do_slave_init(ibdev, j, 1);
  2443. }
  2444. }
  2445. return ibdev;
  2446. err_notif:
  2447. if (ibdev->iboe.nb.notifier_call) {
  2448. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  2449. pr_warn("failure unregistering notifier\n");
  2450. ibdev->iboe.nb.notifier_call = NULL;
  2451. }
  2452. flush_workqueue(wq);
  2453. mlx4_ib_close_sriov(ibdev);
  2454. err_mad:
  2455. mlx4_ib_mad_cleanup(ibdev);
  2456. err_reg:
  2457. ib_unregister_device(&ibdev->ib_dev);
  2458. err_diag_counters:
  2459. mlx4_ib_diag_cleanup(ibdev);
  2460. err_steer_free_bitmap:
  2461. kfree(ibdev->ib_uc_qpns_bitmap);
  2462. err_steer_qp_release:
  2463. if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
  2464. mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
  2465. ibdev->steer_qpn_count);
  2466. err_counter:
  2467. for (i = 0; i < ibdev->num_ports; ++i)
  2468. mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[i]);
  2469. err_map:
  2470. mlx4_ib_free_eqs(dev, ibdev);
  2471. iounmap(ibdev->uar_map);
  2472. err_uar:
  2473. mlx4_uar_free(dev, &ibdev->priv_uar);
  2474. err_pd:
  2475. mlx4_pd_free(dev, ibdev->priv_pdn);
  2476. err_dealloc:
  2477. ib_dealloc_device(&ibdev->ib_dev);
  2478. return NULL;
  2479. }
  2480. int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
  2481. {
  2482. int offset;
  2483. WARN_ON(!dev->ib_uc_qpns_bitmap);
  2484. offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
  2485. dev->steer_qpn_count,
  2486. get_count_order(count));
  2487. if (offset < 0)
  2488. return offset;
  2489. *qpn = dev->steer_qpn_base + offset;
  2490. return 0;
  2491. }
  2492. void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
  2493. {
  2494. if (!qpn ||
  2495. dev->steering_support != MLX4_STEERING_MODE_DEVICE_MANAGED)
  2496. return;
  2497. BUG_ON(qpn < dev->steer_qpn_base);
  2498. bitmap_release_region(dev->ib_uc_qpns_bitmap,
  2499. qpn - dev->steer_qpn_base,
  2500. get_count_order(count));
  2501. }
  2502. int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  2503. int is_attach)
  2504. {
  2505. int err;
  2506. size_t flow_size;
  2507. struct ib_flow_attr *flow = NULL;
  2508. struct ib_flow_spec_ib *ib_spec;
  2509. if (is_attach) {
  2510. flow_size = sizeof(struct ib_flow_attr) +
  2511. sizeof(struct ib_flow_spec_ib);
  2512. flow = kzalloc(flow_size, GFP_KERNEL);
  2513. if (!flow)
  2514. return -ENOMEM;
  2515. flow->port = mqp->port;
  2516. flow->num_of_specs = 1;
  2517. flow->size = flow_size;
  2518. ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
  2519. ib_spec->type = IB_FLOW_SPEC_IB;
  2520. ib_spec->size = sizeof(struct ib_flow_spec_ib);
  2521. /* Add an empty rule for IB L2 */
  2522. memset(&ib_spec->mask, 0, sizeof(ib_spec->mask));
  2523. err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
  2524. IB_FLOW_DOMAIN_NIC,
  2525. MLX4_FS_REGULAR,
  2526. &mqp->reg_id);
  2527. } else {
  2528. err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
  2529. }
  2530. kfree(flow);
  2531. return err;
  2532. }
  2533. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  2534. {
  2535. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  2536. int p;
  2537. int i;
  2538. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  2539. devlink_port_type_clear(mlx4_get_devlink_port(dev, i));
  2540. ibdev->ib_active = false;
  2541. flush_workqueue(wq);
  2542. mlx4_ib_close_sriov(ibdev);
  2543. mlx4_ib_mad_cleanup(ibdev);
  2544. ib_unregister_device(&ibdev->ib_dev);
  2545. mlx4_ib_diag_cleanup(ibdev);
  2546. if (ibdev->iboe.nb.notifier_call) {
  2547. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  2548. pr_warn("failure unregistering notifier\n");
  2549. ibdev->iboe.nb.notifier_call = NULL;
  2550. }
  2551. if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED) {
  2552. mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
  2553. ibdev->steer_qpn_count);
  2554. kfree(ibdev->ib_uc_qpns_bitmap);
  2555. }
  2556. iounmap(ibdev->uar_map);
  2557. for (p = 0; p < ibdev->num_ports; ++p)
  2558. mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[p]);
  2559. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  2560. mlx4_CLOSE_PORT(dev, p);
  2561. mlx4_ib_free_eqs(dev, ibdev);
  2562. mlx4_uar_free(dev, &ibdev->priv_uar);
  2563. mlx4_pd_free(dev, ibdev->priv_pdn);
  2564. ib_dealloc_device(&ibdev->ib_dev);
  2565. }
  2566. static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
  2567. {
  2568. struct mlx4_ib_demux_work **dm = NULL;
  2569. struct mlx4_dev *dev = ibdev->dev;
  2570. int i;
  2571. unsigned long flags;
  2572. struct mlx4_active_ports actv_ports;
  2573. unsigned int ports;
  2574. unsigned int first_port;
  2575. if (!mlx4_is_master(dev))
  2576. return;
  2577. actv_ports = mlx4_get_active_ports(dev, slave);
  2578. ports = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2579. first_port = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2580. dm = kcalloc(ports, sizeof(*dm), GFP_ATOMIC);
  2581. if (!dm) {
  2582. pr_err("failed to allocate memory for tunneling qp update\n");
  2583. return;
  2584. }
  2585. for (i = 0; i < ports; i++) {
  2586. dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
  2587. if (!dm[i]) {
  2588. pr_err("failed to allocate memory for tunneling qp update work struct\n");
  2589. while (--i >= 0)
  2590. kfree(dm[i]);
  2591. goto out;
  2592. }
  2593. INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
  2594. dm[i]->port = first_port + i + 1;
  2595. dm[i]->slave = slave;
  2596. dm[i]->do_init = do_init;
  2597. dm[i]->dev = ibdev;
  2598. }
  2599. /* initialize or tear down tunnel QPs for the slave */
  2600. spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
  2601. if (!ibdev->sriov.is_going_down) {
  2602. for (i = 0; i < ports; i++)
  2603. queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
  2604. spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
  2605. } else {
  2606. spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
  2607. for (i = 0; i < ports; i++)
  2608. kfree(dm[i]);
  2609. }
  2610. out:
  2611. kfree(dm);
  2612. return;
  2613. }
  2614. static void mlx4_ib_handle_catas_error(struct mlx4_ib_dev *ibdev)
  2615. {
  2616. struct mlx4_ib_qp *mqp;
  2617. unsigned long flags_qp;
  2618. unsigned long flags_cq;
  2619. struct mlx4_ib_cq *send_mcq, *recv_mcq;
  2620. struct list_head cq_notify_list;
  2621. struct mlx4_cq *mcq;
  2622. unsigned long flags;
  2623. pr_warn("mlx4_ib_handle_catas_error was started\n");
  2624. INIT_LIST_HEAD(&cq_notify_list);
  2625. /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
  2626. spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
  2627. list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
  2628. spin_lock_irqsave(&mqp->sq.lock, flags_qp);
  2629. if (mqp->sq.tail != mqp->sq.head) {
  2630. send_mcq = to_mcq(mqp->ibqp.send_cq);
  2631. spin_lock_irqsave(&send_mcq->lock, flags_cq);
  2632. if (send_mcq->mcq.comp &&
  2633. mqp->ibqp.send_cq->comp_handler) {
  2634. if (!send_mcq->mcq.reset_notify_added) {
  2635. send_mcq->mcq.reset_notify_added = 1;
  2636. list_add_tail(&send_mcq->mcq.reset_notify,
  2637. &cq_notify_list);
  2638. }
  2639. }
  2640. spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
  2641. }
  2642. spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
  2643. /* Now, handle the QP's receive queue */
  2644. spin_lock_irqsave(&mqp->rq.lock, flags_qp);
  2645. /* no handling is needed for SRQ */
  2646. if (!mqp->ibqp.srq) {
  2647. if (mqp->rq.tail != mqp->rq.head) {
  2648. recv_mcq = to_mcq(mqp->ibqp.recv_cq);
  2649. spin_lock_irqsave(&recv_mcq->lock, flags_cq);
  2650. if (recv_mcq->mcq.comp &&
  2651. mqp->ibqp.recv_cq->comp_handler) {
  2652. if (!recv_mcq->mcq.reset_notify_added) {
  2653. recv_mcq->mcq.reset_notify_added = 1;
  2654. list_add_tail(&recv_mcq->mcq.reset_notify,
  2655. &cq_notify_list);
  2656. }
  2657. }
  2658. spin_unlock_irqrestore(&recv_mcq->lock,
  2659. flags_cq);
  2660. }
  2661. }
  2662. spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
  2663. }
  2664. list_for_each_entry(mcq, &cq_notify_list, reset_notify) {
  2665. mcq->comp(mcq);
  2666. }
  2667. spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
  2668. pr_warn("mlx4_ib_handle_catas_error ended\n");
  2669. }
  2670. static void handle_bonded_port_state_event(struct work_struct *work)
  2671. {
  2672. struct ib_event_work *ew =
  2673. container_of(work, struct ib_event_work, work);
  2674. struct mlx4_ib_dev *ibdev = ew->ib_dev;
  2675. enum ib_port_state bonded_port_state = IB_PORT_NOP;
  2676. int i;
  2677. struct ib_event ibev;
  2678. kfree(ew);
  2679. spin_lock_bh(&ibdev->iboe.lock);
  2680. for (i = 0; i < MLX4_MAX_PORTS; ++i) {
  2681. struct net_device *curr_netdev = ibdev->iboe.netdevs[i];
  2682. enum ib_port_state curr_port_state;
  2683. if (!curr_netdev)
  2684. continue;
  2685. curr_port_state =
  2686. (netif_running(curr_netdev) &&
  2687. netif_carrier_ok(curr_netdev)) ?
  2688. IB_PORT_ACTIVE : IB_PORT_DOWN;
  2689. bonded_port_state = (bonded_port_state != IB_PORT_ACTIVE) ?
  2690. curr_port_state : IB_PORT_ACTIVE;
  2691. }
  2692. spin_unlock_bh(&ibdev->iboe.lock);
  2693. ibev.device = &ibdev->ib_dev;
  2694. ibev.element.port_num = 1;
  2695. ibev.event = (bonded_port_state == IB_PORT_ACTIVE) ?
  2696. IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  2697. ib_dispatch_event(&ibev);
  2698. }
  2699. void mlx4_ib_sl2vl_update(struct mlx4_ib_dev *mdev, int port)
  2700. {
  2701. u64 sl2vl;
  2702. int err;
  2703. err = mlx4_ib_query_sl2vl(&mdev->ib_dev, port, &sl2vl);
  2704. if (err) {
  2705. pr_err("Unable to get current sl to vl mapping for port %d. Using all zeroes (%d)\n",
  2706. port, err);
  2707. sl2vl = 0;
  2708. }
  2709. atomic64_set(&mdev->sl2vl[port - 1], sl2vl);
  2710. }
  2711. static void ib_sl2vl_update_work(struct work_struct *work)
  2712. {
  2713. struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
  2714. struct mlx4_ib_dev *mdev = ew->ib_dev;
  2715. int port = ew->port;
  2716. mlx4_ib_sl2vl_update(mdev, port);
  2717. kfree(ew);
  2718. }
  2719. void mlx4_sched_ib_sl2vl_update_work(struct mlx4_ib_dev *ibdev,
  2720. int port)
  2721. {
  2722. struct ib_event_work *ew;
  2723. ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
  2724. if (ew) {
  2725. INIT_WORK(&ew->work, ib_sl2vl_update_work);
  2726. ew->port = port;
  2727. ew->ib_dev = ibdev;
  2728. queue_work(wq, &ew->work);
  2729. } else {
  2730. pr_err("failed to allocate memory for sl2vl update work\n");
  2731. }
  2732. }
  2733. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  2734. enum mlx4_dev_event event, unsigned long param)
  2735. {
  2736. struct ib_event ibev;
  2737. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  2738. struct mlx4_eqe *eqe = NULL;
  2739. struct ib_event_work *ew;
  2740. int p = 0;
  2741. if (mlx4_is_bonded(dev) &&
  2742. ((event == MLX4_DEV_EVENT_PORT_UP) ||
  2743. (event == MLX4_DEV_EVENT_PORT_DOWN))) {
  2744. ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
  2745. if (!ew)
  2746. return;
  2747. INIT_WORK(&ew->work, handle_bonded_port_state_event);
  2748. ew->ib_dev = ibdev;
  2749. queue_work(wq, &ew->work);
  2750. return;
  2751. }
  2752. if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
  2753. eqe = (struct mlx4_eqe *)param;
  2754. else
  2755. p = (int) param;
  2756. switch (event) {
  2757. case MLX4_DEV_EVENT_PORT_UP:
  2758. if (p > ibdev->num_ports)
  2759. return;
  2760. if (!mlx4_is_slave(dev) &&
  2761. rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
  2762. IB_LINK_LAYER_INFINIBAND) {
  2763. if (mlx4_is_master(dev))
  2764. mlx4_ib_invalidate_all_guid_record(ibdev, p);
  2765. if (ibdev->dev->flags & MLX4_FLAG_SECURE_HOST &&
  2766. !(ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SL_TO_VL_CHANGE_EVENT))
  2767. mlx4_sched_ib_sl2vl_update_work(ibdev, p);
  2768. }
  2769. ibev.event = IB_EVENT_PORT_ACTIVE;
  2770. break;
  2771. case MLX4_DEV_EVENT_PORT_DOWN:
  2772. if (p > ibdev->num_ports)
  2773. return;
  2774. ibev.event = IB_EVENT_PORT_ERR;
  2775. break;
  2776. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  2777. ibdev->ib_active = false;
  2778. ibev.event = IB_EVENT_DEVICE_FATAL;
  2779. mlx4_ib_handle_catas_error(ibdev);
  2780. break;
  2781. case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
  2782. ew = kmalloc(sizeof *ew, GFP_ATOMIC);
  2783. if (!ew) {
  2784. pr_err("failed to allocate memory for events work\n");
  2785. break;
  2786. }
  2787. INIT_WORK(&ew->work, handle_port_mgmt_change_event);
  2788. memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
  2789. ew->ib_dev = ibdev;
  2790. /* need to queue only for port owner, which uses GEN_EQE */
  2791. if (mlx4_is_master(dev))
  2792. queue_work(wq, &ew->work);
  2793. else
  2794. handle_port_mgmt_change_event(&ew->work);
  2795. return;
  2796. case MLX4_DEV_EVENT_SLAVE_INIT:
  2797. /* here, p is the slave id */
  2798. do_slave_init(ibdev, p, 1);
  2799. if (mlx4_is_master(dev)) {
  2800. int i;
  2801. for (i = 1; i <= ibdev->num_ports; i++) {
  2802. if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
  2803. == IB_LINK_LAYER_INFINIBAND)
  2804. mlx4_ib_slave_alias_guid_event(ibdev,
  2805. p, i,
  2806. 1);
  2807. }
  2808. }
  2809. return;
  2810. case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
  2811. if (mlx4_is_master(dev)) {
  2812. int i;
  2813. for (i = 1; i <= ibdev->num_ports; i++) {
  2814. if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
  2815. == IB_LINK_LAYER_INFINIBAND)
  2816. mlx4_ib_slave_alias_guid_event(ibdev,
  2817. p, i,
  2818. 0);
  2819. }
  2820. }
  2821. /* here, p is the slave id */
  2822. do_slave_init(ibdev, p, 0);
  2823. return;
  2824. default:
  2825. return;
  2826. }
  2827. ibev.device = ibdev_ptr;
  2828. ibev.element.port_num = mlx4_is_bonded(ibdev->dev) ? 1 : (u8)p;
  2829. ib_dispatch_event(&ibev);
  2830. }
  2831. static struct mlx4_interface mlx4_ib_interface = {
  2832. .add = mlx4_ib_add,
  2833. .remove = mlx4_ib_remove,
  2834. .event = mlx4_ib_event,
  2835. .protocol = MLX4_PROT_IB_IPV6,
  2836. .flags = MLX4_INTFF_BONDING
  2837. };
  2838. static int __init mlx4_ib_init(void)
  2839. {
  2840. int err;
  2841. wq = alloc_ordered_workqueue("mlx4_ib", WQ_MEM_RECLAIM);
  2842. if (!wq)
  2843. return -ENOMEM;
  2844. err = mlx4_ib_mcg_init();
  2845. if (err)
  2846. goto clean_wq;
  2847. err = mlx4_register_interface(&mlx4_ib_interface);
  2848. if (err)
  2849. goto clean_mcg;
  2850. return 0;
  2851. clean_mcg:
  2852. mlx4_ib_mcg_destroy();
  2853. clean_wq:
  2854. destroy_workqueue(wq);
  2855. return err;
  2856. }
  2857. static void __exit mlx4_ib_cleanup(void)
  2858. {
  2859. mlx4_unregister_interface(&mlx4_ib_interface);
  2860. mlx4_ib_mcg_destroy();
  2861. destroy_workqueue(wq);
  2862. }
  2863. module_init(mlx4_ib_init);
  2864. module_exit(mlx4_ib_cleanup);