main.c 91 KB

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