main.c 90 KB

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