main.c 90 KB

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