main.c 93 KB

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