main.c 89 KB

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