main.c 94 KB

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