main.c 91 KB

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