main.c 91 KB

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