cmd.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/sched.h>
  35. #include <linux/slab.h>
  36. #include <linux/export.h>
  37. #include <linux/pci.h>
  38. #include <linux/errno.h>
  39. #include <linux/mlx4/cmd.h>
  40. #include <linux/mlx4/device.h>
  41. #include <linux/semaphore.h>
  42. #include <rdma/ib_smi.h>
  43. #include <linux/delay.h>
  44. #include <asm/io.h>
  45. #include "mlx4.h"
  46. #include "fw.h"
  47. #include "fw_qos.h"
  48. #include "mlx4_stats.h"
  49. #define CMD_POLL_TOKEN 0xffff
  50. #define INBOX_MASK 0xffffffffffffff00ULL
  51. #define CMD_CHAN_VER 1
  52. #define CMD_CHAN_IF_REV 1
  53. enum {
  54. /* command completed successfully: */
  55. CMD_STAT_OK = 0x00,
  56. /* Internal error (such as a bus error) occurred while processing command: */
  57. CMD_STAT_INTERNAL_ERR = 0x01,
  58. /* Operation/command not supported or opcode modifier not supported: */
  59. CMD_STAT_BAD_OP = 0x02,
  60. /* Parameter not supported or parameter out of range: */
  61. CMD_STAT_BAD_PARAM = 0x03,
  62. /* System not enabled or bad system state: */
  63. CMD_STAT_BAD_SYS_STATE = 0x04,
  64. /* Attempt to access reserved or unallocaterd resource: */
  65. CMD_STAT_BAD_RESOURCE = 0x05,
  66. /* Requested resource is currently executing a command, or is otherwise busy: */
  67. CMD_STAT_RESOURCE_BUSY = 0x06,
  68. /* Required capability exceeds device limits: */
  69. CMD_STAT_EXCEED_LIM = 0x08,
  70. /* Resource is not in the appropriate state or ownership: */
  71. CMD_STAT_BAD_RES_STATE = 0x09,
  72. /* Index out of range: */
  73. CMD_STAT_BAD_INDEX = 0x0a,
  74. /* FW image corrupted: */
  75. CMD_STAT_BAD_NVMEM = 0x0b,
  76. /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
  77. CMD_STAT_ICM_ERROR = 0x0c,
  78. /* Attempt to modify a QP/EE which is not in the presumed state: */
  79. CMD_STAT_BAD_QP_STATE = 0x10,
  80. /* Bad segment parameters (Address/Size): */
  81. CMD_STAT_BAD_SEG_PARAM = 0x20,
  82. /* Memory Region has Memory Windows bound to: */
  83. CMD_STAT_REG_BOUND = 0x21,
  84. /* HCA local attached memory not present: */
  85. CMD_STAT_LAM_NOT_PRE = 0x22,
  86. /* Bad management packet (silently discarded): */
  87. CMD_STAT_BAD_PKT = 0x30,
  88. /* More outstanding CQEs in CQ than new CQ size: */
  89. CMD_STAT_BAD_SIZE = 0x40,
  90. /* Multi Function device support required: */
  91. CMD_STAT_MULTI_FUNC_REQ = 0x50,
  92. };
  93. enum {
  94. HCR_IN_PARAM_OFFSET = 0x00,
  95. HCR_IN_MODIFIER_OFFSET = 0x08,
  96. HCR_OUT_PARAM_OFFSET = 0x0c,
  97. HCR_TOKEN_OFFSET = 0x14,
  98. HCR_STATUS_OFFSET = 0x18,
  99. HCR_OPMOD_SHIFT = 12,
  100. HCR_T_BIT = 21,
  101. HCR_E_BIT = 22,
  102. HCR_GO_BIT = 23
  103. };
  104. enum {
  105. GO_BIT_TIMEOUT_MSECS = 10000
  106. };
  107. enum mlx4_vlan_transition {
  108. MLX4_VLAN_TRANSITION_VST_VST = 0,
  109. MLX4_VLAN_TRANSITION_VST_VGT = 1,
  110. MLX4_VLAN_TRANSITION_VGT_VST = 2,
  111. MLX4_VLAN_TRANSITION_VGT_VGT = 3,
  112. };
  113. struct mlx4_cmd_context {
  114. struct completion done;
  115. int result;
  116. int next;
  117. u64 out_param;
  118. u16 token;
  119. u8 fw_status;
  120. };
  121. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  122. struct mlx4_vhcr_cmd *in_vhcr);
  123. static int mlx4_status_to_errno(u8 status)
  124. {
  125. static const int trans_table[] = {
  126. [CMD_STAT_INTERNAL_ERR] = -EIO,
  127. [CMD_STAT_BAD_OP] = -EPERM,
  128. [CMD_STAT_BAD_PARAM] = -EINVAL,
  129. [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
  130. [CMD_STAT_BAD_RESOURCE] = -EBADF,
  131. [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
  132. [CMD_STAT_EXCEED_LIM] = -ENOMEM,
  133. [CMD_STAT_BAD_RES_STATE] = -EBADF,
  134. [CMD_STAT_BAD_INDEX] = -EBADF,
  135. [CMD_STAT_BAD_NVMEM] = -EFAULT,
  136. [CMD_STAT_ICM_ERROR] = -ENFILE,
  137. [CMD_STAT_BAD_QP_STATE] = -EINVAL,
  138. [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
  139. [CMD_STAT_REG_BOUND] = -EBUSY,
  140. [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
  141. [CMD_STAT_BAD_PKT] = -EINVAL,
  142. [CMD_STAT_BAD_SIZE] = -ENOMEM,
  143. [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
  144. };
  145. if (status >= ARRAY_SIZE(trans_table) ||
  146. (status != CMD_STAT_OK && trans_table[status] == 0))
  147. return -EIO;
  148. return trans_table[status];
  149. }
  150. static u8 mlx4_errno_to_status(int errno)
  151. {
  152. switch (errno) {
  153. case -EPERM:
  154. return CMD_STAT_BAD_OP;
  155. case -EINVAL:
  156. return CMD_STAT_BAD_PARAM;
  157. case -ENXIO:
  158. return CMD_STAT_BAD_SYS_STATE;
  159. case -EBUSY:
  160. return CMD_STAT_RESOURCE_BUSY;
  161. case -ENOMEM:
  162. return CMD_STAT_EXCEED_LIM;
  163. case -ENFILE:
  164. return CMD_STAT_ICM_ERROR;
  165. default:
  166. return CMD_STAT_INTERNAL_ERR;
  167. }
  168. }
  169. static int mlx4_internal_err_ret_value(struct mlx4_dev *dev, u16 op,
  170. u8 op_modifier)
  171. {
  172. switch (op) {
  173. case MLX4_CMD_UNMAP_ICM:
  174. case MLX4_CMD_UNMAP_ICM_AUX:
  175. case MLX4_CMD_UNMAP_FA:
  176. case MLX4_CMD_2RST_QP:
  177. case MLX4_CMD_HW2SW_EQ:
  178. case MLX4_CMD_HW2SW_CQ:
  179. case MLX4_CMD_HW2SW_SRQ:
  180. case MLX4_CMD_HW2SW_MPT:
  181. case MLX4_CMD_CLOSE_HCA:
  182. case MLX4_QP_FLOW_STEERING_DETACH:
  183. case MLX4_CMD_FREE_RES:
  184. case MLX4_CMD_CLOSE_PORT:
  185. return CMD_STAT_OK;
  186. case MLX4_CMD_QP_ATTACH:
  187. /* On Detach case return success */
  188. if (op_modifier == 0)
  189. return CMD_STAT_OK;
  190. return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  191. default:
  192. return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  193. }
  194. }
  195. static int mlx4_closing_cmd_fatal_error(u16 op, u8 fw_status)
  196. {
  197. /* Any error during the closing commands below is considered fatal */
  198. if (op == MLX4_CMD_CLOSE_HCA ||
  199. op == MLX4_CMD_HW2SW_EQ ||
  200. op == MLX4_CMD_HW2SW_CQ ||
  201. op == MLX4_CMD_2RST_QP ||
  202. op == MLX4_CMD_HW2SW_SRQ ||
  203. op == MLX4_CMD_SYNC_TPT ||
  204. op == MLX4_CMD_UNMAP_ICM ||
  205. op == MLX4_CMD_UNMAP_ICM_AUX ||
  206. op == MLX4_CMD_UNMAP_FA)
  207. return 1;
  208. /* Error on MLX4_CMD_HW2SW_MPT is fatal except when fw status equals
  209. * CMD_STAT_REG_BOUND.
  210. * This status indicates that memory region has memory windows bound to it
  211. * which may result from invalid user space usage and is not fatal.
  212. */
  213. if (op == MLX4_CMD_HW2SW_MPT && fw_status != CMD_STAT_REG_BOUND)
  214. return 1;
  215. return 0;
  216. }
  217. static int mlx4_cmd_reset_flow(struct mlx4_dev *dev, u16 op, u8 op_modifier,
  218. int err)
  219. {
  220. /* Only if reset flow is really active return code is based on
  221. * command, otherwise current error code is returned.
  222. */
  223. if (mlx4_internal_err_reset) {
  224. mlx4_enter_error_state(dev->persist);
  225. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  226. }
  227. return err;
  228. }
  229. static int comm_pending(struct mlx4_dev *dev)
  230. {
  231. struct mlx4_priv *priv = mlx4_priv(dev);
  232. u32 status = readl(&priv->mfunc.comm->slave_read);
  233. return (swab32(status) >> 31) != priv->cmd.comm_toggle;
  234. }
  235. static int mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
  236. {
  237. struct mlx4_priv *priv = mlx4_priv(dev);
  238. u32 val;
  239. /* To avoid writing to unknown addresses after the device state was
  240. * changed to internal error and the function was rest,
  241. * check the INTERNAL_ERROR flag which is updated under
  242. * device_state_mutex lock.
  243. */
  244. mutex_lock(&dev->persist->device_state_mutex);
  245. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  246. mutex_unlock(&dev->persist->device_state_mutex);
  247. return -EIO;
  248. }
  249. priv->cmd.comm_toggle ^= 1;
  250. val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
  251. __raw_writel((__force u32) cpu_to_be32(val),
  252. &priv->mfunc.comm->slave_write);
  253. mmiowb();
  254. mutex_unlock(&dev->persist->device_state_mutex);
  255. return 0;
  256. }
  257. static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
  258. unsigned long timeout)
  259. {
  260. struct mlx4_priv *priv = mlx4_priv(dev);
  261. unsigned long end;
  262. int err = 0;
  263. int ret_from_pending = 0;
  264. /* First, verify that the master reports correct status */
  265. if (comm_pending(dev)) {
  266. mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
  267. priv->cmd.comm_toggle, cmd);
  268. return -EAGAIN;
  269. }
  270. /* Write command */
  271. down(&priv->cmd.poll_sem);
  272. if (mlx4_comm_cmd_post(dev, cmd, param)) {
  273. /* Only in case the device state is INTERNAL_ERROR,
  274. * mlx4_comm_cmd_post returns with an error
  275. */
  276. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  277. goto out;
  278. }
  279. end = msecs_to_jiffies(timeout) + jiffies;
  280. while (comm_pending(dev) && time_before(jiffies, end))
  281. cond_resched();
  282. ret_from_pending = comm_pending(dev);
  283. if (ret_from_pending) {
  284. /* check if the slave is trying to boot in the middle of
  285. * FLR process. The only non-zero result in the RESET command
  286. * is MLX4_DELAY_RESET_SLAVE*/
  287. if ((MLX4_COMM_CMD_RESET == cmd)) {
  288. err = MLX4_DELAY_RESET_SLAVE;
  289. goto out;
  290. } else {
  291. mlx4_warn(dev, "Communication channel command 0x%x timed out\n",
  292. cmd);
  293. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  294. }
  295. }
  296. if (err)
  297. mlx4_enter_error_state(dev->persist);
  298. out:
  299. up(&priv->cmd.poll_sem);
  300. return err;
  301. }
  302. static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 vhcr_cmd,
  303. u16 param, u16 op, unsigned long timeout)
  304. {
  305. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  306. struct mlx4_cmd_context *context;
  307. unsigned long end;
  308. int err = 0;
  309. down(&cmd->event_sem);
  310. spin_lock(&cmd->context_lock);
  311. BUG_ON(cmd->free_head < 0);
  312. context = &cmd->context[cmd->free_head];
  313. context->token += cmd->token_mask + 1;
  314. cmd->free_head = context->next;
  315. spin_unlock(&cmd->context_lock);
  316. reinit_completion(&context->done);
  317. if (mlx4_comm_cmd_post(dev, vhcr_cmd, param)) {
  318. /* Only in case the device state is INTERNAL_ERROR,
  319. * mlx4_comm_cmd_post returns with an error
  320. */
  321. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  322. goto out;
  323. }
  324. if (!wait_for_completion_timeout(&context->done,
  325. msecs_to_jiffies(timeout))) {
  326. mlx4_warn(dev, "communication channel command 0x%x (op=0x%x) timed out\n",
  327. vhcr_cmd, op);
  328. goto out_reset;
  329. }
  330. err = context->result;
  331. if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
  332. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  333. vhcr_cmd, context->fw_status);
  334. if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
  335. goto out_reset;
  336. }
  337. /* wait for comm channel ready
  338. * this is necessary for prevention the race
  339. * when switching between event to polling mode
  340. * Skipping this section in case the device is in FATAL_ERROR state,
  341. * In this state, no commands are sent via the comm channel until
  342. * the device has returned from reset.
  343. */
  344. if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
  345. end = msecs_to_jiffies(timeout) + jiffies;
  346. while (comm_pending(dev) && time_before(jiffies, end))
  347. cond_resched();
  348. }
  349. goto out;
  350. out_reset:
  351. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  352. mlx4_enter_error_state(dev->persist);
  353. out:
  354. spin_lock(&cmd->context_lock);
  355. context->next = cmd->free_head;
  356. cmd->free_head = context - cmd->context;
  357. spin_unlock(&cmd->context_lock);
  358. up(&cmd->event_sem);
  359. return err;
  360. }
  361. int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
  362. u16 op, unsigned long timeout)
  363. {
  364. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  365. return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  366. if (mlx4_priv(dev)->cmd.use_events)
  367. return mlx4_comm_cmd_wait(dev, cmd, param, op, timeout);
  368. return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
  369. }
  370. static int cmd_pending(struct mlx4_dev *dev)
  371. {
  372. u32 status;
  373. if (pci_channel_offline(dev->persist->pdev))
  374. return -EIO;
  375. status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
  376. return (status & swab32(1 << HCR_GO_BIT)) ||
  377. (mlx4_priv(dev)->cmd.toggle ==
  378. !!(status & swab32(1 << HCR_T_BIT)));
  379. }
  380. static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
  381. u32 in_modifier, u8 op_modifier, u16 op, u16 token,
  382. int event)
  383. {
  384. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  385. u32 __iomem *hcr = cmd->hcr;
  386. int ret = -EIO;
  387. unsigned long end;
  388. mutex_lock(&dev->persist->device_state_mutex);
  389. /* To avoid writing to unknown addresses after the device state was
  390. * changed to internal error and the chip was reset,
  391. * check the INTERNAL_ERROR flag which is updated under
  392. * device_state_mutex lock.
  393. */
  394. if (pci_channel_offline(dev->persist->pdev) ||
  395. (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
  396. /*
  397. * Device is going through error recovery
  398. * and cannot accept commands.
  399. */
  400. goto out;
  401. }
  402. end = jiffies;
  403. if (event)
  404. end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
  405. while (cmd_pending(dev)) {
  406. if (pci_channel_offline(dev->persist->pdev)) {
  407. /*
  408. * Device is going through error recovery
  409. * and cannot accept commands.
  410. */
  411. goto out;
  412. }
  413. if (time_after_eq(jiffies, end)) {
  414. mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
  415. goto out;
  416. }
  417. cond_resched();
  418. }
  419. /*
  420. * We use writel (instead of something like memcpy_toio)
  421. * because writes of less than 32 bits to the HCR don't work
  422. * (and some architectures such as ia64 implement memcpy_toio
  423. * in terms of writeb).
  424. */
  425. __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
  426. __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
  427. __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
  428. __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
  429. __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
  430. __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
  431. /* __raw_writel may not order writes. */
  432. wmb();
  433. __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
  434. (cmd->toggle << HCR_T_BIT) |
  435. (event ? (1 << HCR_E_BIT) : 0) |
  436. (op_modifier << HCR_OPMOD_SHIFT) |
  437. op), hcr + 6);
  438. /*
  439. * Make sure that our HCR writes don't get mixed in with
  440. * writes from another CPU starting a FW command.
  441. */
  442. mmiowb();
  443. cmd->toggle = cmd->toggle ^ 1;
  444. ret = 0;
  445. out:
  446. if (ret)
  447. mlx4_warn(dev, "Could not post command 0x%x: ret=%d, in_param=0x%llx, in_mod=0x%x, op_mod=0x%x\n",
  448. op, ret, in_param, in_modifier, op_modifier);
  449. mutex_unlock(&dev->persist->device_state_mutex);
  450. return ret;
  451. }
  452. static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  453. int out_is_imm, u32 in_modifier, u8 op_modifier,
  454. u16 op, unsigned long timeout)
  455. {
  456. struct mlx4_priv *priv = mlx4_priv(dev);
  457. struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
  458. int ret;
  459. mutex_lock(&priv->cmd.slave_cmd_mutex);
  460. vhcr->in_param = cpu_to_be64(in_param);
  461. vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
  462. vhcr->in_modifier = cpu_to_be32(in_modifier);
  463. vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
  464. vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
  465. vhcr->status = 0;
  466. vhcr->flags = !!(priv->cmd.use_events) << 6;
  467. if (mlx4_is_master(dev)) {
  468. ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
  469. if (!ret) {
  470. if (out_is_imm) {
  471. if (out_param)
  472. *out_param =
  473. be64_to_cpu(vhcr->out_param);
  474. else {
  475. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  476. op);
  477. vhcr->status = CMD_STAT_BAD_PARAM;
  478. }
  479. }
  480. ret = mlx4_status_to_errno(vhcr->status);
  481. }
  482. if (ret &&
  483. dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  484. ret = mlx4_internal_err_ret_value(dev, op, op_modifier);
  485. } else {
  486. ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0, op,
  487. MLX4_COMM_TIME + timeout);
  488. if (!ret) {
  489. if (out_is_imm) {
  490. if (out_param)
  491. *out_param =
  492. be64_to_cpu(vhcr->out_param);
  493. else {
  494. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  495. op);
  496. vhcr->status = CMD_STAT_BAD_PARAM;
  497. }
  498. }
  499. ret = mlx4_status_to_errno(vhcr->status);
  500. } else {
  501. if (dev->persist->state &
  502. MLX4_DEVICE_STATE_INTERNAL_ERROR)
  503. ret = mlx4_internal_err_ret_value(dev, op,
  504. op_modifier);
  505. else
  506. mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n", op);
  507. }
  508. }
  509. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  510. return ret;
  511. }
  512. static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  513. int out_is_imm, u32 in_modifier, u8 op_modifier,
  514. u16 op, unsigned long timeout)
  515. {
  516. struct mlx4_priv *priv = mlx4_priv(dev);
  517. void __iomem *hcr = priv->cmd.hcr;
  518. int err = 0;
  519. unsigned long end;
  520. u32 stat;
  521. down(&priv->cmd.poll_sem);
  522. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  523. /*
  524. * Device is going through error recovery
  525. * and cannot accept commands.
  526. */
  527. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  528. goto out;
  529. }
  530. if (out_is_imm && !out_param) {
  531. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  532. op);
  533. err = -EINVAL;
  534. goto out;
  535. }
  536. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  537. in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
  538. if (err)
  539. goto out_reset;
  540. end = msecs_to_jiffies(timeout) + jiffies;
  541. while (cmd_pending(dev) && time_before(jiffies, end)) {
  542. if (pci_channel_offline(dev->persist->pdev)) {
  543. /*
  544. * Device is going through error recovery
  545. * and cannot accept commands.
  546. */
  547. err = -EIO;
  548. goto out_reset;
  549. }
  550. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  551. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  552. goto out;
  553. }
  554. cond_resched();
  555. }
  556. if (cmd_pending(dev)) {
  557. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  558. op);
  559. err = -EIO;
  560. goto out_reset;
  561. }
  562. if (out_is_imm)
  563. *out_param =
  564. (u64) be32_to_cpu((__force __be32)
  565. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
  566. (u64) be32_to_cpu((__force __be32)
  567. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
  568. stat = be32_to_cpu((__force __be32)
  569. __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
  570. err = mlx4_status_to_errno(stat);
  571. if (err) {
  572. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  573. op, stat);
  574. if (mlx4_closing_cmd_fatal_error(op, stat))
  575. goto out_reset;
  576. goto out;
  577. }
  578. out_reset:
  579. if (err)
  580. err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
  581. out:
  582. up(&priv->cmd.poll_sem);
  583. return err;
  584. }
  585. void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
  586. {
  587. struct mlx4_priv *priv = mlx4_priv(dev);
  588. struct mlx4_cmd_context *context =
  589. &priv->cmd.context[token & priv->cmd.token_mask];
  590. /* previously timed out command completing at long last */
  591. if (token != context->token)
  592. return;
  593. context->fw_status = status;
  594. context->result = mlx4_status_to_errno(status);
  595. context->out_param = out_param;
  596. complete(&context->done);
  597. }
  598. static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  599. int out_is_imm, u32 in_modifier, u8 op_modifier,
  600. u16 op, unsigned long timeout)
  601. {
  602. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  603. struct mlx4_cmd_context *context;
  604. long ret_wait;
  605. int err = 0;
  606. down(&cmd->event_sem);
  607. spin_lock(&cmd->context_lock);
  608. BUG_ON(cmd->free_head < 0);
  609. context = &cmd->context[cmd->free_head];
  610. context->token += cmd->token_mask + 1;
  611. cmd->free_head = context->next;
  612. spin_unlock(&cmd->context_lock);
  613. if (out_is_imm && !out_param) {
  614. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  615. op);
  616. err = -EINVAL;
  617. goto out;
  618. }
  619. reinit_completion(&context->done);
  620. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  621. in_modifier, op_modifier, op, context->token, 1);
  622. if (err)
  623. goto out_reset;
  624. if (op == MLX4_CMD_SENSE_PORT) {
  625. ret_wait =
  626. wait_for_completion_interruptible_timeout(&context->done,
  627. msecs_to_jiffies(timeout));
  628. if (ret_wait < 0) {
  629. context->fw_status = 0;
  630. context->out_param = 0;
  631. context->result = 0;
  632. }
  633. } else {
  634. ret_wait = (long)wait_for_completion_timeout(&context->done,
  635. msecs_to_jiffies(timeout));
  636. }
  637. if (!ret_wait) {
  638. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  639. op);
  640. if (op == MLX4_CMD_NOP) {
  641. err = -EBUSY;
  642. goto out;
  643. } else {
  644. err = -EIO;
  645. goto out_reset;
  646. }
  647. }
  648. err = context->result;
  649. if (err) {
  650. /* Since we do not want to have this error message always
  651. * displayed at driver start when there are ConnectX2 HCAs
  652. * on the host, we deprecate the error message for this
  653. * specific command/input_mod/opcode_mod/fw-status to be debug.
  654. */
  655. if (op == MLX4_CMD_SET_PORT &&
  656. (in_modifier == 1 || in_modifier == 2) &&
  657. op_modifier == MLX4_SET_PORT_IB_OPCODE &&
  658. context->fw_status == CMD_STAT_BAD_SIZE)
  659. mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
  660. op, context->fw_status);
  661. else
  662. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  663. op, context->fw_status);
  664. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  665. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  666. else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
  667. goto out_reset;
  668. goto out;
  669. }
  670. if (out_is_imm)
  671. *out_param = context->out_param;
  672. out_reset:
  673. if (err)
  674. err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
  675. out:
  676. spin_lock(&cmd->context_lock);
  677. context->next = cmd->free_head;
  678. cmd->free_head = context - cmd->context;
  679. spin_unlock(&cmd->context_lock);
  680. up(&cmd->event_sem);
  681. return err;
  682. }
  683. int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  684. int out_is_imm, u32 in_modifier, u8 op_modifier,
  685. u16 op, unsigned long timeout, int native)
  686. {
  687. if (pci_channel_offline(dev->persist->pdev))
  688. return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
  689. if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
  690. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  691. return mlx4_internal_err_ret_value(dev, op,
  692. op_modifier);
  693. if (mlx4_priv(dev)->cmd.use_events)
  694. return mlx4_cmd_wait(dev, in_param, out_param,
  695. out_is_imm, in_modifier,
  696. op_modifier, op, timeout);
  697. else
  698. return mlx4_cmd_poll(dev, in_param, out_param,
  699. out_is_imm, in_modifier,
  700. op_modifier, op, timeout);
  701. }
  702. return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
  703. in_modifier, op_modifier, op, timeout);
  704. }
  705. EXPORT_SYMBOL_GPL(__mlx4_cmd);
  706. int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
  707. {
  708. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
  709. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  710. }
  711. static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
  712. int slave, u64 slave_addr,
  713. int size, int is_read)
  714. {
  715. u64 in_param;
  716. u64 out_param;
  717. if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
  718. (slave & ~0x7f) | (size & 0xff)) {
  719. mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
  720. slave_addr, master_addr, slave, size);
  721. return -EINVAL;
  722. }
  723. if (is_read) {
  724. in_param = (u64) slave | slave_addr;
  725. out_param = (u64) dev->caps.function | master_addr;
  726. } else {
  727. in_param = (u64) dev->caps.function | master_addr;
  728. out_param = (u64) slave | slave_addr;
  729. }
  730. return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
  731. MLX4_CMD_ACCESS_MEM,
  732. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  733. }
  734. static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
  735. struct mlx4_cmd_mailbox *inbox,
  736. struct mlx4_cmd_mailbox *outbox)
  737. {
  738. struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
  739. struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
  740. int err;
  741. int i;
  742. if (index & 0x1f)
  743. return -EINVAL;
  744. in_mad->attr_mod = cpu_to_be32(index / 32);
  745. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  746. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  747. MLX4_CMD_NATIVE);
  748. if (err)
  749. return err;
  750. for (i = 0; i < 32; ++i)
  751. pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
  752. return err;
  753. }
  754. static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
  755. struct mlx4_cmd_mailbox *inbox,
  756. struct mlx4_cmd_mailbox *outbox)
  757. {
  758. int i;
  759. int err;
  760. for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
  761. err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
  762. if (err)
  763. return err;
  764. }
  765. return 0;
  766. }
  767. #define PORT_CAPABILITY_LOCATION_IN_SMP 20
  768. #define PORT_STATE_OFFSET 32
  769. static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
  770. {
  771. if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
  772. return IB_PORT_ACTIVE;
  773. else
  774. return IB_PORT_DOWN;
  775. }
  776. static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
  777. struct mlx4_vhcr *vhcr,
  778. struct mlx4_cmd_mailbox *inbox,
  779. struct mlx4_cmd_mailbox *outbox,
  780. struct mlx4_cmd_info *cmd)
  781. {
  782. struct ib_smp *smp = inbox->buf;
  783. u32 index;
  784. u8 port, slave_port;
  785. u8 opcode_modifier;
  786. u16 *table;
  787. int err;
  788. int vidx, pidx;
  789. int network_view;
  790. struct mlx4_priv *priv = mlx4_priv(dev);
  791. struct ib_smp *outsmp = outbox->buf;
  792. __be16 *outtab = (__be16 *)(outsmp->data);
  793. __be32 slave_cap_mask;
  794. __be64 slave_node_guid;
  795. slave_port = vhcr->in_modifier;
  796. port = mlx4_slave_convert_port(dev, slave, slave_port);
  797. /* network-view bit is for driver use only, and should not be passed to FW */
  798. opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
  799. network_view = !!(vhcr->op_modifier & 0x8);
  800. if (smp->base_version == 1 &&
  801. smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  802. smp->class_version == 1) {
  803. /* host view is paravirtualized */
  804. if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
  805. if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  806. index = be32_to_cpu(smp->attr_mod);
  807. if (port < 1 || port > dev->caps.num_ports)
  808. return -EINVAL;
  809. table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
  810. sizeof(*table) * 32, GFP_KERNEL);
  811. if (!table)
  812. return -ENOMEM;
  813. /* need to get the full pkey table because the paravirtualized
  814. * pkeys may be scattered among several pkey blocks.
  815. */
  816. err = get_full_pkey_table(dev, port, table, inbox, outbox);
  817. if (!err) {
  818. for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
  819. pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
  820. outtab[vidx % 32] = cpu_to_be16(table[pidx]);
  821. }
  822. }
  823. kfree(table);
  824. return err;
  825. }
  826. if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
  827. /*get the slave specific caps:*/
  828. /*do the command */
  829. smp->attr_mod = cpu_to_be32(port);
  830. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  831. port, opcode_modifier,
  832. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  833. /* modify the response for slaves */
  834. if (!err && slave != mlx4_master_func_num(dev)) {
  835. u8 *state = outsmp->data + PORT_STATE_OFFSET;
  836. *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
  837. slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  838. memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
  839. }
  840. return err;
  841. }
  842. if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
  843. __be64 guid = mlx4_get_admin_guid(dev, slave,
  844. port);
  845. /* set the PF admin guid to the FW/HW burned
  846. * GUID, if it wasn't yet set
  847. */
  848. if (slave == 0 && guid == 0) {
  849. smp->attr_mod = 0;
  850. err = mlx4_cmd_box(dev,
  851. inbox->dma,
  852. outbox->dma,
  853. vhcr->in_modifier,
  854. opcode_modifier,
  855. vhcr->op,
  856. MLX4_CMD_TIME_CLASS_C,
  857. MLX4_CMD_NATIVE);
  858. if (err)
  859. return err;
  860. mlx4_set_admin_guid(dev,
  861. *(__be64 *)outsmp->
  862. data, slave, port);
  863. } else {
  864. memcpy(outsmp->data, &guid, 8);
  865. }
  866. /* clean all other gids */
  867. memset(outsmp->data + 8, 0, 56);
  868. return 0;
  869. }
  870. if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
  871. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  872. port, opcode_modifier,
  873. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  874. if (!err) {
  875. slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
  876. memcpy(outsmp->data + 12, &slave_node_guid, 8);
  877. }
  878. return err;
  879. }
  880. }
  881. }
  882. /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
  883. * These are the MADs used by ib verbs (such as ib_query_gids).
  884. */
  885. if (slave != mlx4_master_func_num(dev) &&
  886. !mlx4_vf_smi_enabled(dev, slave, port)) {
  887. if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  888. smp->method == IB_MGMT_METHOD_GET) || network_view) {
  889. mlx4_err(dev, "Unprivileged slave %d is trying to execute a Subnet MGMT MAD, class 0x%x, method 0x%x, view=%s for attr 0x%x. Rejecting\n",
  890. slave, smp->mgmt_class, smp->method,
  891. network_view ? "Network" : "Host",
  892. be16_to_cpu(smp->attr_id));
  893. return -EPERM;
  894. }
  895. }
  896. return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  897. vhcr->in_modifier, opcode_modifier,
  898. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  899. }
  900. static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
  901. struct mlx4_vhcr *vhcr,
  902. struct mlx4_cmd_mailbox *inbox,
  903. struct mlx4_cmd_mailbox *outbox,
  904. struct mlx4_cmd_info *cmd)
  905. {
  906. return -EPERM;
  907. }
  908. int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
  909. struct mlx4_vhcr *vhcr,
  910. struct mlx4_cmd_mailbox *inbox,
  911. struct mlx4_cmd_mailbox *outbox,
  912. struct mlx4_cmd_info *cmd)
  913. {
  914. u64 in_param;
  915. u64 out_param;
  916. int err;
  917. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  918. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  919. if (cmd->encode_slave_id) {
  920. in_param &= 0xffffffffffffff00ll;
  921. in_param |= slave;
  922. }
  923. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  924. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  925. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  926. if (cmd->out_is_imm)
  927. vhcr->out_param = out_param;
  928. return err;
  929. }
  930. static struct mlx4_cmd_info cmd_info[] = {
  931. {
  932. .opcode = MLX4_CMD_QUERY_FW,
  933. .has_inbox = false,
  934. .has_outbox = true,
  935. .out_is_imm = false,
  936. .encode_slave_id = false,
  937. .verify = NULL,
  938. .wrapper = mlx4_QUERY_FW_wrapper
  939. },
  940. {
  941. .opcode = MLX4_CMD_QUERY_HCA,
  942. .has_inbox = false,
  943. .has_outbox = true,
  944. .out_is_imm = false,
  945. .encode_slave_id = false,
  946. .verify = NULL,
  947. .wrapper = NULL
  948. },
  949. {
  950. .opcode = MLX4_CMD_QUERY_DEV_CAP,
  951. .has_inbox = false,
  952. .has_outbox = true,
  953. .out_is_imm = false,
  954. .encode_slave_id = false,
  955. .verify = NULL,
  956. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  957. },
  958. {
  959. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  960. .has_inbox = false,
  961. .has_outbox = true,
  962. .out_is_imm = false,
  963. .encode_slave_id = false,
  964. .verify = NULL,
  965. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  966. },
  967. {
  968. .opcode = MLX4_CMD_QUERY_ADAPTER,
  969. .has_inbox = false,
  970. .has_outbox = true,
  971. .out_is_imm = false,
  972. .encode_slave_id = false,
  973. .verify = NULL,
  974. .wrapper = NULL
  975. },
  976. {
  977. .opcode = MLX4_CMD_INIT_PORT,
  978. .has_inbox = false,
  979. .has_outbox = false,
  980. .out_is_imm = false,
  981. .encode_slave_id = false,
  982. .verify = NULL,
  983. .wrapper = mlx4_INIT_PORT_wrapper
  984. },
  985. {
  986. .opcode = MLX4_CMD_CLOSE_PORT,
  987. .has_inbox = false,
  988. .has_outbox = false,
  989. .out_is_imm = false,
  990. .encode_slave_id = false,
  991. .verify = NULL,
  992. .wrapper = mlx4_CLOSE_PORT_wrapper
  993. },
  994. {
  995. .opcode = MLX4_CMD_QUERY_PORT,
  996. .has_inbox = false,
  997. .has_outbox = true,
  998. .out_is_imm = false,
  999. .encode_slave_id = false,
  1000. .verify = NULL,
  1001. .wrapper = mlx4_QUERY_PORT_wrapper
  1002. },
  1003. {
  1004. .opcode = MLX4_CMD_SET_PORT,
  1005. .has_inbox = true,
  1006. .has_outbox = false,
  1007. .out_is_imm = false,
  1008. .encode_slave_id = false,
  1009. .verify = NULL,
  1010. .wrapper = mlx4_SET_PORT_wrapper
  1011. },
  1012. {
  1013. .opcode = MLX4_CMD_MAP_EQ,
  1014. .has_inbox = false,
  1015. .has_outbox = false,
  1016. .out_is_imm = false,
  1017. .encode_slave_id = false,
  1018. .verify = NULL,
  1019. .wrapper = mlx4_MAP_EQ_wrapper
  1020. },
  1021. {
  1022. .opcode = MLX4_CMD_SW2HW_EQ,
  1023. .has_inbox = true,
  1024. .has_outbox = false,
  1025. .out_is_imm = false,
  1026. .encode_slave_id = true,
  1027. .verify = NULL,
  1028. .wrapper = mlx4_SW2HW_EQ_wrapper
  1029. },
  1030. {
  1031. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  1032. .has_inbox = false,
  1033. .has_outbox = false,
  1034. .out_is_imm = false,
  1035. .encode_slave_id = false,
  1036. .verify = NULL,
  1037. .wrapper = NULL
  1038. },
  1039. {
  1040. .opcode = MLX4_CMD_NOP,
  1041. .has_inbox = false,
  1042. .has_outbox = false,
  1043. .out_is_imm = false,
  1044. .encode_slave_id = false,
  1045. .verify = NULL,
  1046. .wrapper = NULL
  1047. },
  1048. {
  1049. .opcode = MLX4_CMD_CONFIG_DEV,
  1050. .has_inbox = false,
  1051. .has_outbox = true,
  1052. .out_is_imm = false,
  1053. .encode_slave_id = false,
  1054. .verify = NULL,
  1055. .wrapper = mlx4_CONFIG_DEV_wrapper
  1056. },
  1057. {
  1058. .opcode = MLX4_CMD_ALLOC_RES,
  1059. .has_inbox = false,
  1060. .has_outbox = false,
  1061. .out_is_imm = true,
  1062. .encode_slave_id = false,
  1063. .verify = NULL,
  1064. .wrapper = mlx4_ALLOC_RES_wrapper
  1065. },
  1066. {
  1067. .opcode = MLX4_CMD_FREE_RES,
  1068. .has_inbox = false,
  1069. .has_outbox = false,
  1070. .out_is_imm = false,
  1071. .encode_slave_id = false,
  1072. .verify = NULL,
  1073. .wrapper = mlx4_FREE_RES_wrapper
  1074. },
  1075. {
  1076. .opcode = MLX4_CMD_SW2HW_MPT,
  1077. .has_inbox = true,
  1078. .has_outbox = false,
  1079. .out_is_imm = false,
  1080. .encode_slave_id = true,
  1081. .verify = NULL,
  1082. .wrapper = mlx4_SW2HW_MPT_wrapper
  1083. },
  1084. {
  1085. .opcode = MLX4_CMD_QUERY_MPT,
  1086. .has_inbox = false,
  1087. .has_outbox = true,
  1088. .out_is_imm = false,
  1089. .encode_slave_id = false,
  1090. .verify = NULL,
  1091. .wrapper = mlx4_QUERY_MPT_wrapper
  1092. },
  1093. {
  1094. .opcode = MLX4_CMD_HW2SW_MPT,
  1095. .has_inbox = false,
  1096. .has_outbox = false,
  1097. .out_is_imm = false,
  1098. .encode_slave_id = false,
  1099. .verify = NULL,
  1100. .wrapper = mlx4_HW2SW_MPT_wrapper
  1101. },
  1102. {
  1103. .opcode = MLX4_CMD_READ_MTT,
  1104. .has_inbox = false,
  1105. .has_outbox = true,
  1106. .out_is_imm = false,
  1107. .encode_slave_id = false,
  1108. .verify = NULL,
  1109. .wrapper = NULL
  1110. },
  1111. {
  1112. .opcode = MLX4_CMD_WRITE_MTT,
  1113. .has_inbox = true,
  1114. .has_outbox = false,
  1115. .out_is_imm = false,
  1116. .encode_slave_id = false,
  1117. .verify = NULL,
  1118. .wrapper = mlx4_WRITE_MTT_wrapper
  1119. },
  1120. {
  1121. .opcode = MLX4_CMD_SYNC_TPT,
  1122. .has_inbox = true,
  1123. .has_outbox = false,
  1124. .out_is_imm = false,
  1125. .encode_slave_id = false,
  1126. .verify = NULL,
  1127. .wrapper = NULL
  1128. },
  1129. {
  1130. .opcode = MLX4_CMD_HW2SW_EQ,
  1131. .has_inbox = false,
  1132. .has_outbox = false,
  1133. .out_is_imm = false,
  1134. .encode_slave_id = true,
  1135. .verify = NULL,
  1136. .wrapper = mlx4_HW2SW_EQ_wrapper
  1137. },
  1138. {
  1139. .opcode = MLX4_CMD_QUERY_EQ,
  1140. .has_inbox = false,
  1141. .has_outbox = true,
  1142. .out_is_imm = false,
  1143. .encode_slave_id = true,
  1144. .verify = NULL,
  1145. .wrapper = mlx4_QUERY_EQ_wrapper
  1146. },
  1147. {
  1148. .opcode = MLX4_CMD_SW2HW_CQ,
  1149. .has_inbox = true,
  1150. .has_outbox = false,
  1151. .out_is_imm = false,
  1152. .encode_slave_id = true,
  1153. .verify = NULL,
  1154. .wrapper = mlx4_SW2HW_CQ_wrapper
  1155. },
  1156. {
  1157. .opcode = MLX4_CMD_HW2SW_CQ,
  1158. .has_inbox = false,
  1159. .has_outbox = false,
  1160. .out_is_imm = false,
  1161. .encode_slave_id = false,
  1162. .verify = NULL,
  1163. .wrapper = mlx4_HW2SW_CQ_wrapper
  1164. },
  1165. {
  1166. .opcode = MLX4_CMD_QUERY_CQ,
  1167. .has_inbox = false,
  1168. .has_outbox = true,
  1169. .out_is_imm = false,
  1170. .encode_slave_id = false,
  1171. .verify = NULL,
  1172. .wrapper = mlx4_QUERY_CQ_wrapper
  1173. },
  1174. {
  1175. .opcode = MLX4_CMD_MODIFY_CQ,
  1176. .has_inbox = true,
  1177. .has_outbox = false,
  1178. .out_is_imm = true,
  1179. .encode_slave_id = false,
  1180. .verify = NULL,
  1181. .wrapper = mlx4_MODIFY_CQ_wrapper
  1182. },
  1183. {
  1184. .opcode = MLX4_CMD_SW2HW_SRQ,
  1185. .has_inbox = true,
  1186. .has_outbox = false,
  1187. .out_is_imm = false,
  1188. .encode_slave_id = true,
  1189. .verify = NULL,
  1190. .wrapper = mlx4_SW2HW_SRQ_wrapper
  1191. },
  1192. {
  1193. .opcode = MLX4_CMD_HW2SW_SRQ,
  1194. .has_inbox = false,
  1195. .has_outbox = false,
  1196. .out_is_imm = false,
  1197. .encode_slave_id = false,
  1198. .verify = NULL,
  1199. .wrapper = mlx4_HW2SW_SRQ_wrapper
  1200. },
  1201. {
  1202. .opcode = MLX4_CMD_QUERY_SRQ,
  1203. .has_inbox = false,
  1204. .has_outbox = true,
  1205. .out_is_imm = false,
  1206. .encode_slave_id = false,
  1207. .verify = NULL,
  1208. .wrapper = mlx4_QUERY_SRQ_wrapper
  1209. },
  1210. {
  1211. .opcode = MLX4_CMD_ARM_SRQ,
  1212. .has_inbox = false,
  1213. .has_outbox = false,
  1214. .out_is_imm = false,
  1215. .encode_slave_id = false,
  1216. .verify = NULL,
  1217. .wrapper = mlx4_ARM_SRQ_wrapper
  1218. },
  1219. {
  1220. .opcode = MLX4_CMD_RST2INIT_QP,
  1221. .has_inbox = true,
  1222. .has_outbox = false,
  1223. .out_is_imm = false,
  1224. .encode_slave_id = true,
  1225. .verify = NULL,
  1226. .wrapper = mlx4_RST2INIT_QP_wrapper
  1227. },
  1228. {
  1229. .opcode = MLX4_CMD_INIT2INIT_QP,
  1230. .has_inbox = true,
  1231. .has_outbox = false,
  1232. .out_is_imm = false,
  1233. .encode_slave_id = false,
  1234. .verify = NULL,
  1235. .wrapper = mlx4_INIT2INIT_QP_wrapper
  1236. },
  1237. {
  1238. .opcode = MLX4_CMD_INIT2RTR_QP,
  1239. .has_inbox = true,
  1240. .has_outbox = false,
  1241. .out_is_imm = false,
  1242. .encode_slave_id = false,
  1243. .verify = NULL,
  1244. .wrapper = mlx4_INIT2RTR_QP_wrapper
  1245. },
  1246. {
  1247. .opcode = MLX4_CMD_RTR2RTS_QP,
  1248. .has_inbox = true,
  1249. .has_outbox = false,
  1250. .out_is_imm = false,
  1251. .encode_slave_id = false,
  1252. .verify = NULL,
  1253. .wrapper = mlx4_RTR2RTS_QP_wrapper
  1254. },
  1255. {
  1256. .opcode = MLX4_CMD_RTS2RTS_QP,
  1257. .has_inbox = true,
  1258. .has_outbox = false,
  1259. .out_is_imm = false,
  1260. .encode_slave_id = false,
  1261. .verify = NULL,
  1262. .wrapper = mlx4_RTS2RTS_QP_wrapper
  1263. },
  1264. {
  1265. .opcode = MLX4_CMD_SQERR2RTS_QP,
  1266. .has_inbox = true,
  1267. .has_outbox = false,
  1268. .out_is_imm = false,
  1269. .encode_slave_id = false,
  1270. .verify = NULL,
  1271. .wrapper = mlx4_SQERR2RTS_QP_wrapper
  1272. },
  1273. {
  1274. .opcode = MLX4_CMD_2ERR_QP,
  1275. .has_inbox = false,
  1276. .has_outbox = false,
  1277. .out_is_imm = false,
  1278. .encode_slave_id = false,
  1279. .verify = NULL,
  1280. .wrapper = mlx4_GEN_QP_wrapper
  1281. },
  1282. {
  1283. .opcode = MLX4_CMD_RTS2SQD_QP,
  1284. .has_inbox = false,
  1285. .has_outbox = false,
  1286. .out_is_imm = false,
  1287. .encode_slave_id = false,
  1288. .verify = NULL,
  1289. .wrapper = mlx4_GEN_QP_wrapper
  1290. },
  1291. {
  1292. .opcode = MLX4_CMD_SQD2SQD_QP,
  1293. .has_inbox = true,
  1294. .has_outbox = false,
  1295. .out_is_imm = false,
  1296. .encode_slave_id = false,
  1297. .verify = NULL,
  1298. .wrapper = mlx4_SQD2SQD_QP_wrapper
  1299. },
  1300. {
  1301. .opcode = MLX4_CMD_SQD2RTS_QP,
  1302. .has_inbox = true,
  1303. .has_outbox = false,
  1304. .out_is_imm = false,
  1305. .encode_slave_id = false,
  1306. .verify = NULL,
  1307. .wrapper = mlx4_SQD2RTS_QP_wrapper
  1308. },
  1309. {
  1310. .opcode = MLX4_CMD_2RST_QP,
  1311. .has_inbox = false,
  1312. .has_outbox = false,
  1313. .out_is_imm = false,
  1314. .encode_slave_id = false,
  1315. .verify = NULL,
  1316. .wrapper = mlx4_2RST_QP_wrapper
  1317. },
  1318. {
  1319. .opcode = MLX4_CMD_QUERY_QP,
  1320. .has_inbox = false,
  1321. .has_outbox = true,
  1322. .out_is_imm = false,
  1323. .encode_slave_id = false,
  1324. .verify = NULL,
  1325. .wrapper = mlx4_GEN_QP_wrapper
  1326. },
  1327. {
  1328. .opcode = MLX4_CMD_SUSPEND_QP,
  1329. .has_inbox = false,
  1330. .has_outbox = false,
  1331. .out_is_imm = false,
  1332. .encode_slave_id = false,
  1333. .verify = NULL,
  1334. .wrapper = mlx4_GEN_QP_wrapper
  1335. },
  1336. {
  1337. .opcode = MLX4_CMD_UNSUSPEND_QP,
  1338. .has_inbox = false,
  1339. .has_outbox = false,
  1340. .out_is_imm = false,
  1341. .encode_slave_id = false,
  1342. .verify = NULL,
  1343. .wrapper = mlx4_GEN_QP_wrapper
  1344. },
  1345. {
  1346. .opcode = MLX4_CMD_UPDATE_QP,
  1347. .has_inbox = true,
  1348. .has_outbox = false,
  1349. .out_is_imm = false,
  1350. .encode_slave_id = false,
  1351. .verify = NULL,
  1352. .wrapper = mlx4_UPDATE_QP_wrapper
  1353. },
  1354. {
  1355. .opcode = MLX4_CMD_GET_OP_REQ,
  1356. .has_inbox = false,
  1357. .has_outbox = false,
  1358. .out_is_imm = false,
  1359. .encode_slave_id = false,
  1360. .verify = NULL,
  1361. .wrapper = mlx4_CMD_EPERM_wrapper,
  1362. },
  1363. {
  1364. .opcode = MLX4_CMD_ALLOCATE_VPP,
  1365. .has_inbox = false,
  1366. .has_outbox = true,
  1367. .out_is_imm = false,
  1368. .encode_slave_id = false,
  1369. .verify = NULL,
  1370. .wrapper = mlx4_CMD_EPERM_wrapper,
  1371. },
  1372. {
  1373. .opcode = MLX4_CMD_SET_VPORT_QOS,
  1374. .has_inbox = false,
  1375. .has_outbox = true,
  1376. .out_is_imm = false,
  1377. .encode_slave_id = false,
  1378. .verify = NULL,
  1379. .wrapper = mlx4_CMD_EPERM_wrapper,
  1380. },
  1381. {
  1382. .opcode = MLX4_CMD_CONF_SPECIAL_QP,
  1383. .has_inbox = false,
  1384. .has_outbox = false,
  1385. .out_is_imm = false,
  1386. .encode_slave_id = false,
  1387. .verify = NULL, /* XXX verify: only demux can do this */
  1388. .wrapper = NULL
  1389. },
  1390. {
  1391. .opcode = MLX4_CMD_MAD_IFC,
  1392. .has_inbox = true,
  1393. .has_outbox = true,
  1394. .out_is_imm = false,
  1395. .encode_slave_id = false,
  1396. .verify = NULL,
  1397. .wrapper = mlx4_MAD_IFC_wrapper
  1398. },
  1399. {
  1400. .opcode = MLX4_CMD_MAD_DEMUX,
  1401. .has_inbox = false,
  1402. .has_outbox = false,
  1403. .out_is_imm = false,
  1404. .encode_slave_id = false,
  1405. .verify = NULL,
  1406. .wrapper = mlx4_CMD_EPERM_wrapper
  1407. },
  1408. {
  1409. .opcode = MLX4_CMD_QUERY_IF_STAT,
  1410. .has_inbox = false,
  1411. .has_outbox = true,
  1412. .out_is_imm = false,
  1413. .encode_slave_id = false,
  1414. .verify = NULL,
  1415. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  1416. },
  1417. {
  1418. .opcode = MLX4_CMD_ACCESS_REG,
  1419. .has_inbox = true,
  1420. .has_outbox = true,
  1421. .out_is_imm = false,
  1422. .encode_slave_id = false,
  1423. .verify = NULL,
  1424. .wrapper = mlx4_ACCESS_REG_wrapper,
  1425. },
  1426. {
  1427. .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
  1428. .has_inbox = false,
  1429. .has_outbox = false,
  1430. .out_is_imm = false,
  1431. .encode_slave_id = false,
  1432. .verify = NULL,
  1433. .wrapper = mlx4_CMD_EPERM_wrapper,
  1434. },
  1435. /* Native multicast commands are not available for guests */
  1436. {
  1437. .opcode = MLX4_CMD_QP_ATTACH,
  1438. .has_inbox = true,
  1439. .has_outbox = false,
  1440. .out_is_imm = false,
  1441. .encode_slave_id = false,
  1442. .verify = NULL,
  1443. .wrapper = mlx4_QP_ATTACH_wrapper
  1444. },
  1445. {
  1446. .opcode = MLX4_CMD_PROMISC,
  1447. .has_inbox = false,
  1448. .has_outbox = false,
  1449. .out_is_imm = false,
  1450. .encode_slave_id = false,
  1451. .verify = NULL,
  1452. .wrapper = mlx4_PROMISC_wrapper
  1453. },
  1454. /* Ethernet specific commands */
  1455. {
  1456. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  1457. .has_inbox = true,
  1458. .has_outbox = false,
  1459. .out_is_imm = false,
  1460. .encode_slave_id = false,
  1461. .verify = NULL,
  1462. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  1463. },
  1464. {
  1465. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  1466. .has_inbox = false,
  1467. .has_outbox = false,
  1468. .out_is_imm = false,
  1469. .encode_slave_id = false,
  1470. .verify = NULL,
  1471. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  1472. },
  1473. {
  1474. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  1475. .has_inbox = false,
  1476. .has_outbox = true,
  1477. .out_is_imm = false,
  1478. .encode_slave_id = false,
  1479. .verify = NULL,
  1480. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  1481. },
  1482. {
  1483. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  1484. .has_inbox = false,
  1485. .has_outbox = false,
  1486. .out_is_imm = false,
  1487. .encode_slave_id = false,
  1488. .verify = NULL,
  1489. .wrapper = NULL
  1490. },
  1491. /* flow steering commands */
  1492. {
  1493. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1494. .has_inbox = true,
  1495. .has_outbox = false,
  1496. .out_is_imm = true,
  1497. .encode_slave_id = false,
  1498. .verify = NULL,
  1499. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1500. },
  1501. {
  1502. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1503. .has_inbox = false,
  1504. .has_outbox = false,
  1505. .out_is_imm = false,
  1506. .encode_slave_id = false,
  1507. .verify = NULL,
  1508. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1509. },
  1510. {
  1511. .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
  1512. .has_inbox = false,
  1513. .has_outbox = false,
  1514. .out_is_imm = false,
  1515. .encode_slave_id = false,
  1516. .verify = NULL,
  1517. .wrapper = mlx4_CMD_EPERM_wrapper
  1518. },
  1519. {
  1520. .opcode = MLX4_CMD_VIRT_PORT_MAP,
  1521. .has_inbox = false,
  1522. .has_outbox = false,
  1523. .out_is_imm = false,
  1524. .encode_slave_id = false,
  1525. .verify = NULL,
  1526. .wrapper = mlx4_CMD_EPERM_wrapper
  1527. },
  1528. };
  1529. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1530. struct mlx4_vhcr_cmd *in_vhcr)
  1531. {
  1532. struct mlx4_priv *priv = mlx4_priv(dev);
  1533. struct mlx4_cmd_info *cmd = NULL;
  1534. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1535. struct mlx4_vhcr *vhcr;
  1536. struct mlx4_cmd_mailbox *inbox = NULL;
  1537. struct mlx4_cmd_mailbox *outbox = NULL;
  1538. u64 in_param;
  1539. u64 out_param;
  1540. int ret = 0;
  1541. int i;
  1542. int err = 0;
  1543. /* Create sw representation of Virtual HCR */
  1544. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1545. if (!vhcr)
  1546. return -ENOMEM;
  1547. /* DMA in the vHCR */
  1548. if (!in_vhcr) {
  1549. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1550. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1551. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1552. MLX4_ACCESS_MEM_ALIGN), 1);
  1553. if (ret) {
  1554. if (!(dev->persist->state &
  1555. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1556. mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
  1557. __func__, ret);
  1558. kfree(vhcr);
  1559. return ret;
  1560. }
  1561. }
  1562. /* Fill SW VHCR fields */
  1563. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1564. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1565. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1566. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1567. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1568. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1569. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1570. /* Lookup command */
  1571. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1572. if (vhcr->op == cmd_info[i].opcode) {
  1573. cmd = &cmd_info[i];
  1574. break;
  1575. }
  1576. }
  1577. if (!cmd) {
  1578. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1579. vhcr->op, slave);
  1580. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1581. goto out_status;
  1582. }
  1583. /* Read inbox */
  1584. if (cmd->has_inbox) {
  1585. vhcr->in_param &= INBOX_MASK;
  1586. inbox = mlx4_alloc_cmd_mailbox(dev);
  1587. if (IS_ERR(inbox)) {
  1588. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1589. inbox = NULL;
  1590. goto out_status;
  1591. }
  1592. ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1593. vhcr->in_param,
  1594. MLX4_MAILBOX_SIZE, 1);
  1595. if (ret) {
  1596. if (!(dev->persist->state &
  1597. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1598. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1599. __func__, cmd->opcode);
  1600. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1601. goto out_status;
  1602. }
  1603. }
  1604. /* Apply permission and bound checks if applicable */
  1605. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1606. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
  1607. vhcr->op, slave, vhcr->in_modifier);
  1608. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1609. goto out_status;
  1610. }
  1611. /* Allocate outbox */
  1612. if (cmd->has_outbox) {
  1613. outbox = mlx4_alloc_cmd_mailbox(dev);
  1614. if (IS_ERR(outbox)) {
  1615. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1616. outbox = NULL;
  1617. goto out_status;
  1618. }
  1619. }
  1620. /* Execute the command! */
  1621. if (cmd->wrapper) {
  1622. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1623. cmd);
  1624. if (cmd->out_is_imm)
  1625. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1626. } else {
  1627. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1628. vhcr->in_param;
  1629. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1630. vhcr->out_param;
  1631. err = __mlx4_cmd(dev, in_param, &out_param,
  1632. cmd->out_is_imm, vhcr->in_modifier,
  1633. vhcr->op_modifier, vhcr->op,
  1634. MLX4_CMD_TIME_CLASS_A,
  1635. MLX4_CMD_NATIVE);
  1636. if (cmd->out_is_imm) {
  1637. vhcr->out_param = out_param;
  1638. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1639. }
  1640. }
  1641. if (err) {
  1642. if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1643. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
  1644. vhcr->op, slave, vhcr->errno, err);
  1645. vhcr_cmd->status = mlx4_errno_to_status(err);
  1646. goto out_status;
  1647. }
  1648. /* Write outbox if command completed successfully */
  1649. if (cmd->has_outbox && !vhcr_cmd->status) {
  1650. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1651. vhcr->out_param,
  1652. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1653. if (ret) {
  1654. /* If we failed to write back the outbox after the
  1655. *command was successfully executed, we must fail this
  1656. * slave, as it is now in undefined state */
  1657. if (!(dev->persist->state &
  1658. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1659. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1660. goto out;
  1661. }
  1662. }
  1663. out_status:
  1664. /* DMA back vhcr result */
  1665. if (!in_vhcr) {
  1666. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1667. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1668. ALIGN(sizeof(struct mlx4_vhcr),
  1669. MLX4_ACCESS_MEM_ALIGN),
  1670. MLX4_CMD_WRAPPED);
  1671. if (ret)
  1672. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1673. __func__);
  1674. else if (vhcr->e_bit &&
  1675. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1676. mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
  1677. slave);
  1678. }
  1679. out:
  1680. kfree(vhcr);
  1681. mlx4_free_cmd_mailbox(dev, inbox);
  1682. mlx4_free_cmd_mailbox(dev, outbox);
  1683. return ret;
  1684. }
  1685. static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
  1686. int slave, int port)
  1687. {
  1688. struct mlx4_vport_oper_state *vp_oper;
  1689. struct mlx4_vport_state *vp_admin;
  1690. struct mlx4_vf_immed_vlan_work *work;
  1691. struct mlx4_dev *dev = &(priv->dev);
  1692. int err;
  1693. int admin_vlan_ix = NO_INDX;
  1694. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1695. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1696. if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
  1697. vp_oper->state.default_qos == vp_admin->default_qos &&
  1698. vp_oper->state.link_state == vp_admin->link_state &&
  1699. vp_oper->state.qos_vport == vp_admin->qos_vport)
  1700. return 0;
  1701. if (!(priv->mfunc.master.slave_state[slave].active &&
  1702. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
  1703. /* even if the UPDATE_QP command isn't supported, we still want
  1704. * to set this VF link according to the admin directive
  1705. */
  1706. vp_oper->state.link_state = vp_admin->link_state;
  1707. return -1;
  1708. }
  1709. mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
  1710. slave, port);
  1711. mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
  1712. vp_admin->default_vlan, vp_admin->default_qos,
  1713. vp_admin->link_state);
  1714. work = kzalloc(sizeof(*work), GFP_KERNEL);
  1715. if (!work)
  1716. return -ENOMEM;
  1717. if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
  1718. if (MLX4_VGT != vp_admin->default_vlan) {
  1719. err = __mlx4_register_vlan(&priv->dev, port,
  1720. vp_admin->default_vlan,
  1721. &admin_vlan_ix);
  1722. if (err) {
  1723. kfree(work);
  1724. mlx4_warn(&priv->dev,
  1725. "No vlan resources slave %d, port %d\n",
  1726. slave, port);
  1727. return err;
  1728. }
  1729. } else {
  1730. admin_vlan_ix = NO_INDX;
  1731. }
  1732. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
  1733. mlx4_dbg(&priv->dev,
  1734. "alloc vlan %d idx %d slave %d port %d\n",
  1735. (int)(vp_admin->default_vlan),
  1736. admin_vlan_ix, slave, port);
  1737. }
  1738. /* save original vlan ix and vlan id */
  1739. work->orig_vlan_id = vp_oper->state.default_vlan;
  1740. work->orig_vlan_ix = vp_oper->vlan_idx;
  1741. /* handle new qos */
  1742. if (vp_oper->state.default_qos != vp_admin->default_qos)
  1743. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
  1744. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
  1745. vp_oper->vlan_idx = admin_vlan_ix;
  1746. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1747. vp_oper->state.default_qos = vp_admin->default_qos;
  1748. vp_oper->state.link_state = vp_admin->link_state;
  1749. vp_oper->state.qos_vport = vp_admin->qos_vport;
  1750. if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
  1751. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
  1752. /* iterate over QPs owned by this slave, using UPDATE_QP */
  1753. work->port = port;
  1754. work->slave = slave;
  1755. work->qos = vp_oper->state.default_qos;
  1756. work->qos_vport = vp_oper->state.qos_vport;
  1757. work->vlan_id = vp_oper->state.default_vlan;
  1758. work->vlan_ix = vp_oper->vlan_idx;
  1759. work->priv = priv;
  1760. INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
  1761. queue_work(priv->mfunc.master.comm_wq, &work->work);
  1762. return 0;
  1763. }
  1764. static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
  1765. {
  1766. struct mlx4_qos_manager *port_qos_ctl;
  1767. struct mlx4_priv *priv = mlx4_priv(dev);
  1768. port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
  1769. bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
  1770. /* Enable only default prio at PF init routine */
  1771. set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
  1772. }
  1773. static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
  1774. {
  1775. int i;
  1776. int err;
  1777. int num_vfs;
  1778. u16 availible_vpp;
  1779. u8 vpp_param[MLX4_NUM_UP];
  1780. struct mlx4_qos_manager *port_qos;
  1781. struct mlx4_priv *priv = mlx4_priv(dev);
  1782. err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
  1783. if (err) {
  1784. mlx4_info(dev, "Failed query availible VPPs\n");
  1785. return;
  1786. }
  1787. port_qos = &priv->mfunc.master.qos_ctl[port];
  1788. num_vfs = (availible_vpp /
  1789. bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
  1790. for (i = 0; i < MLX4_NUM_UP; i++) {
  1791. if (test_bit(i, port_qos->priority_bm))
  1792. vpp_param[i] = num_vfs;
  1793. }
  1794. err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
  1795. if (err) {
  1796. mlx4_info(dev, "Failed allocating VPPs\n");
  1797. return;
  1798. }
  1799. /* Query actual allocated VPP, just to make sure */
  1800. err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
  1801. if (err) {
  1802. mlx4_info(dev, "Failed query availible VPPs\n");
  1803. return;
  1804. }
  1805. port_qos->num_of_qos_vfs = num_vfs;
  1806. mlx4_dbg(dev, "Port %d Availible VPPs %d\n", port, availible_vpp);
  1807. for (i = 0; i < MLX4_NUM_UP; i++)
  1808. mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
  1809. vpp_param[i]);
  1810. }
  1811. static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
  1812. {
  1813. int port, err;
  1814. struct mlx4_vport_state *vp_admin;
  1815. struct mlx4_vport_oper_state *vp_oper;
  1816. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1817. &priv->dev, slave);
  1818. int min_port = find_first_bit(actv_ports.ports,
  1819. priv->dev.caps.num_ports) + 1;
  1820. int max_port = min_port - 1 +
  1821. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1822. for (port = min_port; port <= max_port; port++) {
  1823. if (!test_bit(port - 1, actv_ports.ports))
  1824. continue;
  1825. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1826. priv->mfunc.master.vf_admin[slave].enable_smi[port];
  1827. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1828. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1829. vp_oper->state = *vp_admin;
  1830. if (MLX4_VGT != vp_admin->default_vlan) {
  1831. err = __mlx4_register_vlan(&priv->dev, port,
  1832. vp_admin->default_vlan, &(vp_oper->vlan_idx));
  1833. if (err) {
  1834. vp_oper->vlan_idx = NO_INDX;
  1835. mlx4_warn(&priv->dev,
  1836. "No vlan resources slave %d, port %d\n",
  1837. slave, port);
  1838. return err;
  1839. }
  1840. mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
  1841. (int)(vp_oper->state.default_vlan),
  1842. vp_oper->vlan_idx, slave, port);
  1843. }
  1844. if (vp_admin->spoofchk) {
  1845. vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
  1846. port,
  1847. vp_admin->mac);
  1848. if (0 > vp_oper->mac_idx) {
  1849. err = vp_oper->mac_idx;
  1850. vp_oper->mac_idx = NO_INDX;
  1851. mlx4_warn(&priv->dev,
  1852. "No mac resources slave %d, port %d\n",
  1853. slave, port);
  1854. return err;
  1855. }
  1856. mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
  1857. vp_oper->state.mac, vp_oper->mac_idx, slave, port);
  1858. }
  1859. }
  1860. return 0;
  1861. }
  1862. static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
  1863. {
  1864. int port;
  1865. struct mlx4_vport_oper_state *vp_oper;
  1866. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1867. &priv->dev, slave);
  1868. int min_port = find_first_bit(actv_ports.ports,
  1869. priv->dev.caps.num_ports) + 1;
  1870. int max_port = min_port - 1 +
  1871. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1872. for (port = min_port; port <= max_port; port++) {
  1873. if (!test_bit(port - 1, actv_ports.ports))
  1874. continue;
  1875. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1876. MLX4_VF_SMI_DISABLED;
  1877. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1878. if (NO_INDX != vp_oper->vlan_idx) {
  1879. __mlx4_unregister_vlan(&priv->dev,
  1880. port, vp_oper->state.default_vlan);
  1881. vp_oper->vlan_idx = NO_INDX;
  1882. }
  1883. if (NO_INDX != vp_oper->mac_idx) {
  1884. __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
  1885. vp_oper->mac_idx = NO_INDX;
  1886. }
  1887. }
  1888. return;
  1889. }
  1890. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1891. u16 param, u8 toggle)
  1892. {
  1893. struct mlx4_priv *priv = mlx4_priv(dev);
  1894. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1895. u32 reply;
  1896. u8 is_going_down = 0;
  1897. int i;
  1898. unsigned long flags;
  1899. slave_state[slave].comm_toggle ^= 1;
  1900. reply = (u32) slave_state[slave].comm_toggle << 31;
  1901. if (toggle != slave_state[slave].comm_toggle) {
  1902. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
  1903. toggle, slave);
  1904. goto reset_slave;
  1905. }
  1906. if (cmd == MLX4_COMM_CMD_RESET) {
  1907. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1908. slave_state[slave].active = false;
  1909. slave_state[slave].old_vlan_api = false;
  1910. mlx4_master_deactivate_admin_state(priv, slave);
  1911. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1912. slave_state[slave].event_eq[i].eqn = -1;
  1913. slave_state[slave].event_eq[i].token = 0;
  1914. }
  1915. /*check if we are in the middle of FLR process,
  1916. if so return "retry" status to the slave*/
  1917. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1918. goto inform_slave_state;
  1919. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1920. /* write the version in the event field */
  1921. reply |= mlx4_comm_get_version();
  1922. goto reset_slave;
  1923. }
  1924. /*command from slave in the middle of FLR*/
  1925. if (cmd != MLX4_COMM_CMD_RESET &&
  1926. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1927. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
  1928. slave, cmd);
  1929. return;
  1930. }
  1931. switch (cmd) {
  1932. case MLX4_COMM_CMD_VHCR0:
  1933. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1934. goto reset_slave;
  1935. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1936. priv->mfunc.master.slave_state[slave].cookie = 0;
  1937. break;
  1938. case MLX4_COMM_CMD_VHCR1:
  1939. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1940. goto reset_slave;
  1941. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1942. break;
  1943. case MLX4_COMM_CMD_VHCR2:
  1944. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1945. goto reset_slave;
  1946. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1947. break;
  1948. case MLX4_COMM_CMD_VHCR_EN:
  1949. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1950. goto reset_slave;
  1951. slave_state[slave].vhcr_dma |= param;
  1952. if (mlx4_master_activate_admin_state(priv, slave))
  1953. goto reset_slave;
  1954. slave_state[slave].active = true;
  1955. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1956. break;
  1957. case MLX4_COMM_CMD_VHCR_POST:
  1958. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1959. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
  1960. mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
  1961. slave, cmd, slave_state[slave].last_cmd);
  1962. goto reset_slave;
  1963. }
  1964. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1965. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1966. mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
  1967. slave);
  1968. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1969. goto reset_slave;
  1970. }
  1971. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1972. break;
  1973. default:
  1974. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1975. goto reset_slave;
  1976. }
  1977. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1978. if (!slave_state[slave].is_slave_going_down)
  1979. slave_state[slave].last_cmd = cmd;
  1980. else
  1981. is_going_down = 1;
  1982. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1983. if (is_going_down) {
  1984. mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
  1985. cmd, slave);
  1986. return;
  1987. }
  1988. __raw_writel((__force u32) cpu_to_be32(reply),
  1989. &priv->mfunc.comm[slave].slave_read);
  1990. mmiowb();
  1991. return;
  1992. reset_slave:
  1993. /* cleanup any slave resources */
  1994. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
  1995. mlx4_delete_all_resources_for_slave(dev, slave);
  1996. if (cmd != MLX4_COMM_CMD_RESET) {
  1997. mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
  1998. slave, cmd);
  1999. /* Turn on internal error letting slave reset itself immeditaly,
  2000. * otherwise it might take till timeout on command is passed
  2001. */
  2002. reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
  2003. }
  2004. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  2005. if (!slave_state[slave].is_slave_going_down)
  2006. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  2007. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  2008. /*with slave in the middle of flr, no need to clean resources again.*/
  2009. inform_slave_state:
  2010. memset(&slave_state[slave].event_eq, 0,
  2011. sizeof(struct mlx4_slave_event_eq_info));
  2012. __raw_writel((__force u32) cpu_to_be32(reply),
  2013. &priv->mfunc.comm[slave].slave_read);
  2014. wmb();
  2015. }
  2016. /* master command processing */
  2017. void mlx4_master_comm_channel(struct work_struct *work)
  2018. {
  2019. struct mlx4_mfunc_master_ctx *master =
  2020. container_of(work,
  2021. struct mlx4_mfunc_master_ctx,
  2022. comm_work);
  2023. struct mlx4_mfunc *mfunc =
  2024. container_of(master, struct mlx4_mfunc, master);
  2025. struct mlx4_priv *priv =
  2026. container_of(mfunc, struct mlx4_priv, mfunc);
  2027. struct mlx4_dev *dev = &priv->dev;
  2028. __be32 *bit_vec;
  2029. u32 comm_cmd;
  2030. u32 vec;
  2031. int i, j, slave;
  2032. int toggle;
  2033. int served = 0;
  2034. int reported = 0;
  2035. u32 slt;
  2036. bit_vec = master->comm_arm_bit_vector;
  2037. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  2038. vec = be32_to_cpu(bit_vec[i]);
  2039. for (j = 0; j < 32; j++) {
  2040. if (!(vec & (1 << j)))
  2041. continue;
  2042. ++reported;
  2043. slave = (i * 32) + j;
  2044. comm_cmd = swab32(readl(
  2045. &mfunc->comm[slave].slave_write));
  2046. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  2047. >> 31;
  2048. toggle = comm_cmd >> 31;
  2049. if (toggle != slt) {
  2050. if (master->slave_state[slave].comm_toggle
  2051. != slt) {
  2052. pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
  2053. slave, slt,
  2054. master->slave_state[slave].comm_toggle);
  2055. master->slave_state[slave].comm_toggle =
  2056. slt;
  2057. }
  2058. mlx4_master_do_cmd(dev, slave,
  2059. comm_cmd >> 16 & 0xff,
  2060. comm_cmd & 0xffff, toggle);
  2061. ++served;
  2062. }
  2063. }
  2064. }
  2065. if (reported && reported != served)
  2066. mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
  2067. reported, served);
  2068. if (mlx4_ARM_COMM_CHANNEL(dev))
  2069. mlx4_warn(dev, "Failed to arm comm channel events\n");
  2070. }
  2071. static int sync_toggles(struct mlx4_dev *dev)
  2072. {
  2073. struct mlx4_priv *priv = mlx4_priv(dev);
  2074. u32 wr_toggle;
  2075. u32 rd_toggle;
  2076. unsigned long end;
  2077. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
  2078. if (wr_toggle == 0xffffffff)
  2079. end = jiffies + msecs_to_jiffies(30000);
  2080. else
  2081. end = jiffies + msecs_to_jiffies(5000);
  2082. while (time_before(jiffies, end)) {
  2083. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
  2084. if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
  2085. /* PCI might be offline */
  2086. msleep(100);
  2087. wr_toggle = swab32(readl(&priv->mfunc.comm->
  2088. slave_write));
  2089. continue;
  2090. }
  2091. if (rd_toggle >> 31 == wr_toggle >> 31) {
  2092. priv->cmd.comm_toggle = rd_toggle >> 31;
  2093. return 0;
  2094. }
  2095. cond_resched();
  2096. }
  2097. /*
  2098. * we could reach here if for example the previous VM using this
  2099. * function misbehaved and left the channel with unsynced state. We
  2100. * should fix this here and give this VM a chance to use a properly
  2101. * synced channel
  2102. */
  2103. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  2104. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  2105. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  2106. priv->cmd.comm_toggle = 0;
  2107. return 0;
  2108. }
  2109. int mlx4_multi_func_init(struct mlx4_dev *dev)
  2110. {
  2111. struct mlx4_priv *priv = mlx4_priv(dev);
  2112. struct mlx4_slave_state *s_state;
  2113. int i, j, err, port;
  2114. if (mlx4_is_master(dev))
  2115. priv->mfunc.comm =
  2116. ioremap(pci_resource_start(dev->persist->pdev,
  2117. priv->fw.comm_bar) +
  2118. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  2119. else
  2120. priv->mfunc.comm =
  2121. ioremap(pci_resource_start(dev->persist->pdev, 2) +
  2122. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  2123. if (!priv->mfunc.comm) {
  2124. mlx4_err(dev, "Couldn't map communication vector\n");
  2125. goto err_vhcr;
  2126. }
  2127. if (mlx4_is_master(dev)) {
  2128. struct mlx4_vf_oper_state *vf_oper;
  2129. struct mlx4_vf_admin_state *vf_admin;
  2130. priv->mfunc.master.slave_state =
  2131. kzalloc(dev->num_slaves *
  2132. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  2133. if (!priv->mfunc.master.slave_state)
  2134. goto err_comm;
  2135. priv->mfunc.master.vf_admin =
  2136. kzalloc(dev->num_slaves *
  2137. sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
  2138. if (!priv->mfunc.master.vf_admin)
  2139. goto err_comm_admin;
  2140. priv->mfunc.master.vf_oper =
  2141. kzalloc(dev->num_slaves *
  2142. sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
  2143. if (!priv->mfunc.master.vf_oper)
  2144. goto err_comm_oper;
  2145. for (i = 0; i < dev->num_slaves; ++i) {
  2146. vf_admin = &priv->mfunc.master.vf_admin[i];
  2147. vf_oper = &priv->mfunc.master.vf_oper[i];
  2148. s_state = &priv->mfunc.master.slave_state[i];
  2149. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  2150. mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
  2151. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  2152. s_state->event_eq[j].eqn = -1;
  2153. __raw_writel((__force u32) 0,
  2154. &priv->mfunc.comm[i].slave_write);
  2155. __raw_writel((__force u32) 0,
  2156. &priv->mfunc.comm[i].slave_read);
  2157. mmiowb();
  2158. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  2159. struct mlx4_vport_state *admin_vport;
  2160. struct mlx4_vport_state *oper_vport;
  2161. s_state->vlan_filter[port] =
  2162. kzalloc(sizeof(struct mlx4_vlan_fltr),
  2163. GFP_KERNEL);
  2164. if (!s_state->vlan_filter[port]) {
  2165. if (--port)
  2166. kfree(s_state->vlan_filter[port]);
  2167. goto err_slaves;
  2168. }
  2169. admin_vport = &vf_admin->vport[port];
  2170. oper_vport = &vf_oper->vport[port].state;
  2171. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  2172. admin_vport->default_vlan = MLX4_VGT;
  2173. oper_vport->default_vlan = MLX4_VGT;
  2174. admin_vport->qos_vport =
  2175. MLX4_VPP_DEFAULT_VPORT;
  2176. oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
  2177. vf_oper->vport[port].vlan_idx = NO_INDX;
  2178. vf_oper->vport[port].mac_idx = NO_INDX;
  2179. mlx4_set_random_admin_guid(dev, i, port);
  2180. }
  2181. spin_lock_init(&s_state->lock);
  2182. }
  2183. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
  2184. for (port = 1; port <= dev->caps.num_ports; port++) {
  2185. if (mlx4_is_eth(dev, port)) {
  2186. mlx4_set_default_port_qos(dev, port);
  2187. mlx4_allocate_port_vpps(dev, port);
  2188. }
  2189. }
  2190. }
  2191. memset(&priv->mfunc.master.cmd_eqe, 0, sizeof(struct mlx4_eqe));
  2192. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  2193. INIT_WORK(&priv->mfunc.master.comm_work,
  2194. mlx4_master_comm_channel);
  2195. INIT_WORK(&priv->mfunc.master.slave_event_work,
  2196. mlx4_gen_slave_eqe);
  2197. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  2198. mlx4_master_handle_slave_flr);
  2199. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  2200. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  2201. priv->mfunc.master.comm_wq =
  2202. create_singlethread_workqueue("mlx4_comm");
  2203. if (!priv->mfunc.master.comm_wq)
  2204. goto err_slaves;
  2205. if (mlx4_init_resource_tracker(dev))
  2206. goto err_thread;
  2207. } else {
  2208. err = sync_toggles(dev);
  2209. if (err) {
  2210. mlx4_err(dev, "Couldn't sync toggles\n");
  2211. goto err_comm;
  2212. }
  2213. }
  2214. return 0;
  2215. err_thread:
  2216. flush_workqueue(priv->mfunc.master.comm_wq);
  2217. destroy_workqueue(priv->mfunc.master.comm_wq);
  2218. err_slaves:
  2219. while (i--) {
  2220. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  2221. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  2222. }
  2223. kfree(priv->mfunc.master.vf_oper);
  2224. err_comm_oper:
  2225. kfree(priv->mfunc.master.vf_admin);
  2226. err_comm_admin:
  2227. kfree(priv->mfunc.master.slave_state);
  2228. err_comm:
  2229. iounmap(priv->mfunc.comm);
  2230. err_vhcr:
  2231. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  2232. priv->mfunc.vhcr,
  2233. priv->mfunc.vhcr_dma);
  2234. priv->mfunc.vhcr = NULL;
  2235. return -ENOMEM;
  2236. }
  2237. int mlx4_cmd_init(struct mlx4_dev *dev)
  2238. {
  2239. struct mlx4_priv *priv = mlx4_priv(dev);
  2240. int flags = 0;
  2241. if (!priv->cmd.initialized) {
  2242. mutex_init(&priv->cmd.slave_cmd_mutex);
  2243. sema_init(&priv->cmd.poll_sem, 1);
  2244. priv->cmd.use_events = 0;
  2245. priv->cmd.toggle = 1;
  2246. priv->cmd.initialized = 1;
  2247. flags |= MLX4_CMD_CLEANUP_STRUCT;
  2248. }
  2249. if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
  2250. priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
  2251. 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
  2252. if (!priv->cmd.hcr) {
  2253. mlx4_err(dev, "Couldn't map command register\n");
  2254. goto err;
  2255. }
  2256. flags |= MLX4_CMD_CLEANUP_HCR;
  2257. }
  2258. if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
  2259. priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
  2260. PAGE_SIZE,
  2261. &priv->mfunc.vhcr_dma,
  2262. GFP_KERNEL);
  2263. if (!priv->mfunc.vhcr)
  2264. goto err;
  2265. flags |= MLX4_CMD_CLEANUP_VHCR;
  2266. }
  2267. if (!priv->cmd.pool) {
  2268. priv->cmd.pool = pci_pool_create("mlx4_cmd",
  2269. dev->persist->pdev,
  2270. MLX4_MAILBOX_SIZE,
  2271. MLX4_MAILBOX_SIZE, 0);
  2272. if (!priv->cmd.pool)
  2273. goto err;
  2274. flags |= MLX4_CMD_CLEANUP_POOL;
  2275. }
  2276. return 0;
  2277. err:
  2278. mlx4_cmd_cleanup(dev, flags);
  2279. return -ENOMEM;
  2280. }
  2281. void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
  2282. {
  2283. struct mlx4_priv *priv = mlx4_priv(dev);
  2284. int slave;
  2285. u32 slave_read;
  2286. /* Report an internal error event to all
  2287. * communication channels.
  2288. */
  2289. for (slave = 0; slave < dev->num_slaves; slave++) {
  2290. slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
  2291. slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
  2292. __raw_writel((__force u32)cpu_to_be32(slave_read),
  2293. &priv->mfunc.comm[slave].slave_read);
  2294. /* Make sure that our comm channel write doesn't
  2295. * get mixed in with writes from another CPU.
  2296. */
  2297. mmiowb();
  2298. }
  2299. }
  2300. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  2301. {
  2302. struct mlx4_priv *priv = mlx4_priv(dev);
  2303. int i, port;
  2304. if (mlx4_is_master(dev)) {
  2305. flush_workqueue(priv->mfunc.master.comm_wq);
  2306. destroy_workqueue(priv->mfunc.master.comm_wq);
  2307. for (i = 0; i < dev->num_slaves; i++) {
  2308. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  2309. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  2310. }
  2311. kfree(priv->mfunc.master.slave_state);
  2312. kfree(priv->mfunc.master.vf_admin);
  2313. kfree(priv->mfunc.master.vf_oper);
  2314. dev->num_slaves = 0;
  2315. }
  2316. iounmap(priv->mfunc.comm);
  2317. }
  2318. void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
  2319. {
  2320. struct mlx4_priv *priv = mlx4_priv(dev);
  2321. if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
  2322. pci_pool_destroy(priv->cmd.pool);
  2323. priv->cmd.pool = NULL;
  2324. }
  2325. if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
  2326. (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
  2327. iounmap(priv->cmd.hcr);
  2328. priv->cmd.hcr = NULL;
  2329. }
  2330. if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
  2331. (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
  2332. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  2333. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  2334. priv->mfunc.vhcr = NULL;
  2335. }
  2336. if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
  2337. priv->cmd.initialized = 0;
  2338. }
  2339. /*
  2340. * Switch to using events to issue FW commands (can only be called
  2341. * after event queue for command events has been initialized).
  2342. */
  2343. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  2344. {
  2345. struct mlx4_priv *priv = mlx4_priv(dev);
  2346. int i;
  2347. int err = 0;
  2348. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  2349. sizeof (struct mlx4_cmd_context),
  2350. GFP_KERNEL);
  2351. if (!priv->cmd.context)
  2352. return -ENOMEM;
  2353. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2354. priv->cmd.context[i].token = i;
  2355. priv->cmd.context[i].next = i + 1;
  2356. /* To support fatal error flow, initialize all
  2357. * cmd contexts to allow simulating completions
  2358. * with complete() at any time.
  2359. */
  2360. init_completion(&priv->cmd.context[i].done);
  2361. }
  2362. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  2363. priv->cmd.free_head = 0;
  2364. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  2365. spin_lock_init(&priv->cmd.context_lock);
  2366. for (priv->cmd.token_mask = 1;
  2367. priv->cmd.token_mask < priv->cmd.max_cmds;
  2368. priv->cmd.token_mask <<= 1)
  2369. ; /* nothing */
  2370. --priv->cmd.token_mask;
  2371. down(&priv->cmd.poll_sem);
  2372. priv->cmd.use_events = 1;
  2373. return err;
  2374. }
  2375. /*
  2376. * Switch back to polling (used when shutting down the device)
  2377. */
  2378. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  2379. {
  2380. struct mlx4_priv *priv = mlx4_priv(dev);
  2381. int i;
  2382. priv->cmd.use_events = 0;
  2383. for (i = 0; i < priv->cmd.max_cmds; ++i)
  2384. down(&priv->cmd.event_sem);
  2385. kfree(priv->cmd.context);
  2386. up(&priv->cmd.poll_sem);
  2387. }
  2388. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  2389. {
  2390. struct mlx4_cmd_mailbox *mailbox;
  2391. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  2392. if (!mailbox)
  2393. return ERR_PTR(-ENOMEM);
  2394. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  2395. &mailbox->dma);
  2396. if (!mailbox->buf) {
  2397. kfree(mailbox);
  2398. return ERR_PTR(-ENOMEM);
  2399. }
  2400. memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
  2401. return mailbox;
  2402. }
  2403. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  2404. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  2405. struct mlx4_cmd_mailbox *mailbox)
  2406. {
  2407. if (!mailbox)
  2408. return;
  2409. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  2410. kfree(mailbox);
  2411. }
  2412. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  2413. u32 mlx4_comm_get_version(void)
  2414. {
  2415. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  2416. }
  2417. static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
  2418. {
  2419. if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
  2420. mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
  2421. vf, dev->persist->num_vfs);
  2422. return -EINVAL;
  2423. }
  2424. return vf+1;
  2425. }
  2426. int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
  2427. {
  2428. if (slave < 1 || slave > dev->persist->num_vfs) {
  2429. mlx4_err(dev,
  2430. "Bad slave number:%d (number of activated slaves: %lu)\n",
  2431. slave, dev->num_slaves);
  2432. return -EINVAL;
  2433. }
  2434. return slave - 1;
  2435. }
  2436. void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
  2437. {
  2438. struct mlx4_priv *priv = mlx4_priv(dev);
  2439. struct mlx4_cmd_context *context;
  2440. int i;
  2441. spin_lock(&priv->cmd.context_lock);
  2442. if (priv->cmd.context) {
  2443. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2444. context = &priv->cmd.context[i];
  2445. context->fw_status = CMD_STAT_INTERNAL_ERR;
  2446. context->result =
  2447. mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  2448. complete(&context->done);
  2449. }
  2450. }
  2451. spin_unlock(&priv->cmd.context_lock);
  2452. }
  2453. struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
  2454. {
  2455. struct mlx4_active_ports actv_ports;
  2456. int vf;
  2457. bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
  2458. if (slave == 0) {
  2459. bitmap_fill(actv_ports.ports, dev->caps.num_ports);
  2460. return actv_ports;
  2461. }
  2462. vf = mlx4_get_vf_indx(dev, slave);
  2463. if (vf < 0)
  2464. return actv_ports;
  2465. bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
  2466. min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
  2467. dev->caps.num_ports));
  2468. return actv_ports;
  2469. }
  2470. EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
  2471. int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
  2472. {
  2473. unsigned n;
  2474. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2475. unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2476. if (port <= 0 || port > m)
  2477. return -EINVAL;
  2478. n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2479. if (port <= n)
  2480. port = n + 1;
  2481. return port;
  2482. }
  2483. EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
  2484. int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
  2485. {
  2486. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2487. if (test_bit(port - 1, actv_ports.ports))
  2488. return port -
  2489. find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2490. return -1;
  2491. }
  2492. EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
  2493. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
  2494. int port)
  2495. {
  2496. unsigned i;
  2497. struct mlx4_slaves_pport slaves_pport;
  2498. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2499. if (port <= 0 || port > dev->caps.num_ports)
  2500. return slaves_pport;
  2501. for (i = 0; i < dev->persist->num_vfs + 1; i++) {
  2502. struct mlx4_active_ports actv_ports =
  2503. mlx4_get_active_ports(dev, i);
  2504. if (test_bit(port - 1, actv_ports.ports))
  2505. set_bit(i, slaves_pport.slaves);
  2506. }
  2507. return slaves_pport;
  2508. }
  2509. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
  2510. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
  2511. struct mlx4_dev *dev,
  2512. const struct mlx4_active_ports *crit_ports)
  2513. {
  2514. unsigned i;
  2515. struct mlx4_slaves_pport slaves_pport;
  2516. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2517. for (i = 0; i < dev->persist->num_vfs + 1; i++) {
  2518. struct mlx4_active_ports actv_ports =
  2519. mlx4_get_active_ports(dev, i);
  2520. if (bitmap_equal(crit_ports->ports, actv_ports.ports,
  2521. dev->caps.num_ports))
  2522. set_bit(i, slaves_pport.slaves);
  2523. }
  2524. return slaves_pport;
  2525. }
  2526. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
  2527. static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
  2528. {
  2529. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2530. int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
  2531. + 1;
  2532. int max_port = min_port +
  2533. bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2534. if (port < min_port)
  2535. port = min_port;
  2536. else if (port >= max_port)
  2537. port = max_port - 1;
  2538. return port;
  2539. }
  2540. static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
  2541. int max_tx_rate)
  2542. {
  2543. int i;
  2544. int err;
  2545. struct mlx4_qos_manager *port_qos;
  2546. struct mlx4_dev *dev = &priv->dev;
  2547. struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
  2548. port_qos = &priv->mfunc.master.qos_ctl[port];
  2549. memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
  2550. if (slave > port_qos->num_of_qos_vfs) {
  2551. mlx4_info(dev, "No availible VPP resources for this VF\n");
  2552. return -EINVAL;
  2553. }
  2554. /* Query for default QoS values from Vport 0 is needed */
  2555. err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
  2556. if (err) {
  2557. mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
  2558. return err;
  2559. }
  2560. for (i = 0; i < MLX4_NUM_UP; i++) {
  2561. if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
  2562. vpp_qos[i].max_avg_bw = max_tx_rate;
  2563. vpp_qos[i].enable = 1;
  2564. } else {
  2565. /* if user supplied tx_rate == 0, meaning no rate limit
  2566. * configuration is required. so we are leaving the
  2567. * value of max_avg_bw as queried from Vport 0.
  2568. */
  2569. vpp_qos[i].enable = 0;
  2570. }
  2571. }
  2572. err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
  2573. if (err) {
  2574. mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
  2575. return err;
  2576. }
  2577. return 0;
  2578. }
  2579. static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
  2580. struct mlx4_vport_state *vf_admin)
  2581. {
  2582. struct mlx4_qos_manager *info;
  2583. struct mlx4_priv *priv = mlx4_priv(dev);
  2584. if (!mlx4_is_master(dev) ||
  2585. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
  2586. return false;
  2587. info = &priv->mfunc.master.qos_ctl[port];
  2588. if (vf_admin->default_vlan != MLX4_VGT &&
  2589. test_bit(vf_admin->default_qos, info->priority_bm))
  2590. return true;
  2591. return false;
  2592. }
  2593. static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
  2594. struct mlx4_vport_state *vf_admin,
  2595. int vlan, int qos)
  2596. {
  2597. struct mlx4_vport_state dummy_admin = {0};
  2598. if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
  2599. !vf_admin->tx_rate)
  2600. return true;
  2601. dummy_admin.default_qos = qos;
  2602. dummy_admin.default_vlan = vlan;
  2603. /* VF wants to move to other VST state which is valid with current
  2604. * rate limit. Either differnt default vlan in VST or other
  2605. * supported QoS priority. Otherwise we don't allow this change when
  2606. * the TX rate is still configured.
  2607. */
  2608. if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
  2609. return true;
  2610. mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
  2611. (vlan == MLX4_VGT) ? "VGT" : "VST");
  2612. if (vlan != MLX4_VGT)
  2613. mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
  2614. mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
  2615. return false;
  2616. }
  2617. int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
  2618. {
  2619. struct mlx4_priv *priv = mlx4_priv(dev);
  2620. struct mlx4_vport_state *s_info;
  2621. int slave;
  2622. if (!mlx4_is_master(dev))
  2623. return -EPROTONOSUPPORT;
  2624. slave = mlx4_get_slave_indx(dev, vf);
  2625. if (slave < 0)
  2626. return -EINVAL;
  2627. port = mlx4_slaves_closest_port(dev, slave, port);
  2628. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2629. s_info->mac = mac;
  2630. mlx4_info(dev, "default mac on vf %d port %d to %llX will take effect only after vf restart\n",
  2631. vf, port, s_info->mac);
  2632. return 0;
  2633. }
  2634. EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
  2635. int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
  2636. {
  2637. struct mlx4_priv *priv = mlx4_priv(dev);
  2638. struct mlx4_vport_state *vf_admin;
  2639. int slave;
  2640. if ((!mlx4_is_master(dev)) ||
  2641. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2642. return -EPROTONOSUPPORT;
  2643. if ((vlan > 4095) || (qos > 7))
  2644. return -EINVAL;
  2645. slave = mlx4_get_slave_indx(dev, vf);
  2646. if (slave < 0)
  2647. return -EINVAL;
  2648. port = mlx4_slaves_closest_port(dev, slave, port);
  2649. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2650. if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
  2651. return -EPERM;
  2652. if ((0 == vlan) && (0 == qos))
  2653. vf_admin->default_vlan = MLX4_VGT;
  2654. else
  2655. vf_admin->default_vlan = vlan;
  2656. vf_admin->default_qos = qos;
  2657. /* If rate was configured prior to VST, we saved the configured rate
  2658. * in vf_admin->rate and now, if priority supported we enforce the QoS
  2659. */
  2660. if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
  2661. vf_admin->tx_rate)
  2662. vf_admin->qos_vport = slave;
  2663. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2664. mlx4_info(dev,
  2665. "updating vf %d port %d config will take effect on next VF restart\n",
  2666. vf, port);
  2667. return 0;
  2668. }
  2669. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2670. int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
  2671. int max_tx_rate)
  2672. {
  2673. int err;
  2674. int slave;
  2675. struct mlx4_vport_state *vf_admin;
  2676. struct mlx4_priv *priv = mlx4_priv(dev);
  2677. if (!mlx4_is_master(dev) ||
  2678. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
  2679. return -EPROTONOSUPPORT;
  2680. if (min_tx_rate) {
  2681. mlx4_info(dev, "Minimum BW share not supported\n");
  2682. return -EPROTONOSUPPORT;
  2683. }
  2684. slave = mlx4_get_slave_indx(dev, vf);
  2685. if (slave < 0)
  2686. return -EINVAL;
  2687. port = mlx4_slaves_closest_port(dev, slave, port);
  2688. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2689. err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
  2690. if (err) {
  2691. mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
  2692. max_tx_rate);
  2693. return err;
  2694. }
  2695. vf_admin->tx_rate = max_tx_rate;
  2696. /* if VF is not in supported mode (VST with supported prio),
  2697. * we do not change vport configuration for its QPs, but save
  2698. * the rate, so it will be enforced when it moves to supported
  2699. * mode next time.
  2700. */
  2701. if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
  2702. mlx4_info(dev,
  2703. "rate set for VF %d when not in valid state\n", vf);
  2704. if (vf_admin->default_vlan != MLX4_VGT)
  2705. mlx4_info(dev, "VST priority not supported by QoS\n");
  2706. else
  2707. mlx4_info(dev, "VF in VGT mode (needed VST)\n");
  2708. mlx4_info(dev,
  2709. "rate %d take affect when VF moves to valid state\n",
  2710. max_tx_rate);
  2711. return 0;
  2712. }
  2713. /* If user sets rate 0 assigning default vport for its QPs */
  2714. vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
  2715. if (priv->mfunc.master.slave_state[slave].active &&
  2716. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
  2717. mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
  2718. return 0;
  2719. }
  2720. EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
  2721. /* mlx4_get_slave_default_vlan -
  2722. * return true if VST ( default vlan)
  2723. * if VST, will return vlan & qos (if not NULL)
  2724. */
  2725. bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
  2726. u16 *vlan, u8 *qos)
  2727. {
  2728. struct mlx4_vport_oper_state *vp_oper;
  2729. struct mlx4_priv *priv;
  2730. priv = mlx4_priv(dev);
  2731. port = mlx4_slaves_closest_port(dev, slave, port);
  2732. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2733. if (MLX4_VGT != vp_oper->state.default_vlan) {
  2734. if (vlan)
  2735. *vlan = vp_oper->state.default_vlan;
  2736. if (qos)
  2737. *qos = vp_oper->state.default_qos;
  2738. return true;
  2739. }
  2740. return false;
  2741. }
  2742. EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
  2743. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2744. {
  2745. struct mlx4_priv *priv = mlx4_priv(dev);
  2746. struct mlx4_vport_state *s_info;
  2747. int slave;
  2748. if ((!mlx4_is_master(dev)) ||
  2749. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  2750. return -EPROTONOSUPPORT;
  2751. slave = mlx4_get_slave_indx(dev, vf);
  2752. if (slave < 0)
  2753. return -EINVAL;
  2754. port = mlx4_slaves_closest_port(dev, slave, port);
  2755. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2756. s_info->spoofchk = setting;
  2757. return 0;
  2758. }
  2759. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2760. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2761. {
  2762. struct mlx4_priv *priv = mlx4_priv(dev);
  2763. struct mlx4_vport_state *s_info;
  2764. int slave;
  2765. if (!mlx4_is_master(dev))
  2766. return -EPROTONOSUPPORT;
  2767. slave = mlx4_get_slave_indx(dev, vf);
  2768. if (slave < 0)
  2769. return -EINVAL;
  2770. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2771. ivf->vf = vf;
  2772. /* need to convert it to a func */
  2773. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2774. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2775. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2776. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2777. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2778. ivf->mac[5] = ((s_info->mac) & 0xff);
  2779. ivf->vlan = s_info->default_vlan;
  2780. ivf->qos = s_info->default_qos;
  2781. if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
  2782. ivf->max_tx_rate = s_info->tx_rate;
  2783. else
  2784. ivf->max_tx_rate = 0;
  2785. ivf->min_tx_rate = 0;
  2786. ivf->spoofchk = s_info->spoofchk;
  2787. ivf->linkstate = s_info->link_state;
  2788. return 0;
  2789. }
  2790. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2791. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2792. {
  2793. struct mlx4_priv *priv = mlx4_priv(dev);
  2794. struct mlx4_vport_state *s_info;
  2795. int slave;
  2796. u8 link_stat_event;
  2797. slave = mlx4_get_slave_indx(dev, vf);
  2798. if (slave < 0)
  2799. return -EINVAL;
  2800. port = mlx4_slaves_closest_port(dev, slave, port);
  2801. switch (link_state) {
  2802. case IFLA_VF_LINK_STATE_AUTO:
  2803. /* get current link state */
  2804. if (!priv->sense.do_sense_port[port])
  2805. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2806. else
  2807. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2808. break;
  2809. case IFLA_VF_LINK_STATE_ENABLE:
  2810. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2811. break;
  2812. case IFLA_VF_LINK_STATE_DISABLE:
  2813. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2814. break;
  2815. default:
  2816. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2817. link_state, slave, port);
  2818. return -EINVAL;
  2819. };
  2820. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2821. s_info->link_state = link_state;
  2822. /* send event */
  2823. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2824. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2825. mlx4_dbg(dev,
  2826. "updating vf %d port %d no link state HW enforcment\n",
  2827. vf, port);
  2828. return 0;
  2829. }
  2830. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
  2831. int mlx4_get_counter_stats(struct mlx4_dev *dev, int counter_index,
  2832. struct mlx4_counter *counter_stats, int reset)
  2833. {
  2834. struct mlx4_cmd_mailbox *mailbox = NULL;
  2835. struct mlx4_counter *tmp_counter;
  2836. int err;
  2837. u32 if_stat_in_mod;
  2838. if (!counter_stats)
  2839. return -EINVAL;
  2840. if (counter_index == MLX4_SINK_COUNTER_INDEX(dev))
  2841. return 0;
  2842. mailbox = mlx4_alloc_cmd_mailbox(dev);
  2843. if (IS_ERR(mailbox))
  2844. return PTR_ERR(mailbox);
  2845. memset(mailbox->buf, 0, sizeof(struct mlx4_counter));
  2846. if_stat_in_mod = counter_index;
  2847. if (reset)
  2848. if_stat_in_mod |= MLX4_QUERY_IF_STAT_RESET;
  2849. err = mlx4_cmd_box(dev, 0, mailbox->dma,
  2850. if_stat_in_mod, 0,
  2851. MLX4_CMD_QUERY_IF_STAT,
  2852. MLX4_CMD_TIME_CLASS_C,
  2853. MLX4_CMD_NATIVE);
  2854. if (err) {
  2855. mlx4_dbg(dev, "%s: failed to read statistics for counter index %d\n",
  2856. __func__, counter_index);
  2857. goto if_stat_out;
  2858. }
  2859. tmp_counter = (struct mlx4_counter *)mailbox->buf;
  2860. counter_stats->counter_mode = tmp_counter->counter_mode;
  2861. if (counter_stats->counter_mode == 0) {
  2862. counter_stats->rx_frames =
  2863. cpu_to_be64(be64_to_cpu(counter_stats->rx_frames) +
  2864. be64_to_cpu(tmp_counter->rx_frames));
  2865. counter_stats->tx_frames =
  2866. cpu_to_be64(be64_to_cpu(counter_stats->tx_frames) +
  2867. be64_to_cpu(tmp_counter->tx_frames));
  2868. counter_stats->rx_bytes =
  2869. cpu_to_be64(be64_to_cpu(counter_stats->rx_bytes) +
  2870. be64_to_cpu(tmp_counter->rx_bytes));
  2871. counter_stats->tx_bytes =
  2872. cpu_to_be64(be64_to_cpu(counter_stats->tx_bytes) +
  2873. be64_to_cpu(tmp_counter->tx_bytes));
  2874. }
  2875. if_stat_out:
  2876. mlx4_free_cmd_mailbox(dev, mailbox);
  2877. return err;
  2878. }
  2879. EXPORT_SYMBOL_GPL(mlx4_get_counter_stats);
  2880. int mlx4_get_vf_stats(struct mlx4_dev *dev, int port, int vf_idx,
  2881. struct ifla_vf_stats *vf_stats)
  2882. {
  2883. struct mlx4_counter tmp_vf_stats;
  2884. int slave;
  2885. int err = 0;
  2886. if (!vf_stats)
  2887. return -EINVAL;
  2888. if (!mlx4_is_master(dev))
  2889. return -EPROTONOSUPPORT;
  2890. slave = mlx4_get_slave_indx(dev, vf_idx);
  2891. if (slave < 0)
  2892. return -EINVAL;
  2893. port = mlx4_slaves_closest_port(dev, slave, port);
  2894. err = mlx4_calc_vf_counters(dev, slave, port, &tmp_vf_stats);
  2895. if (!err && tmp_vf_stats.counter_mode == 0) {
  2896. vf_stats->rx_packets = be64_to_cpu(tmp_vf_stats.rx_frames);
  2897. vf_stats->tx_packets = be64_to_cpu(tmp_vf_stats.tx_frames);
  2898. vf_stats->rx_bytes = be64_to_cpu(tmp_vf_stats.rx_bytes);
  2899. vf_stats->tx_bytes = be64_to_cpu(tmp_vf_stats.tx_bytes);
  2900. }
  2901. return err;
  2902. }
  2903. EXPORT_SYMBOL_GPL(mlx4_get_vf_stats);
  2904. int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
  2905. {
  2906. struct mlx4_priv *priv = mlx4_priv(dev);
  2907. if (slave < 1 || slave >= dev->num_slaves ||
  2908. port < 1 || port > MLX4_MAX_PORTS)
  2909. return 0;
  2910. return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
  2911. MLX4_VF_SMI_ENABLED;
  2912. }
  2913. EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
  2914. int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
  2915. {
  2916. struct mlx4_priv *priv = mlx4_priv(dev);
  2917. if (slave == mlx4_master_func_num(dev))
  2918. return 1;
  2919. if (slave < 1 || slave >= dev->num_slaves ||
  2920. port < 1 || port > MLX4_MAX_PORTS)
  2921. return 0;
  2922. return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
  2923. MLX4_VF_SMI_ENABLED;
  2924. }
  2925. EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
  2926. int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
  2927. int enabled)
  2928. {
  2929. struct mlx4_priv *priv = mlx4_priv(dev);
  2930. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  2931. &priv->dev, slave);
  2932. int min_port = find_first_bit(actv_ports.ports,
  2933. priv->dev.caps.num_ports) + 1;
  2934. int max_port = min_port - 1 +
  2935. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  2936. if (slave == mlx4_master_func_num(dev))
  2937. return 0;
  2938. if (slave < 1 || slave >= dev->num_slaves ||
  2939. port < 1 || port > MLX4_MAX_PORTS ||
  2940. enabled < 0 || enabled > 1)
  2941. return -EINVAL;
  2942. if (min_port == max_port && dev->caps.num_ports > 1) {
  2943. mlx4_info(dev, "SMI access disallowed for single ported VFs\n");
  2944. return -EPROTONOSUPPORT;
  2945. }
  2946. priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
  2947. return 0;
  2948. }
  2949. EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);