cmd.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  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. int ret;
  691. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  692. return mlx4_internal_err_ret_value(dev, op,
  693. op_modifier);
  694. down_read(&mlx4_priv(dev)->cmd.switch_sem);
  695. if (mlx4_priv(dev)->cmd.use_events)
  696. ret = mlx4_cmd_wait(dev, in_param, out_param,
  697. out_is_imm, in_modifier,
  698. op_modifier, op, timeout);
  699. else
  700. ret = mlx4_cmd_poll(dev, in_param, out_param,
  701. out_is_imm, in_modifier,
  702. op_modifier, op, timeout);
  703. up_read(&mlx4_priv(dev)->cmd.switch_sem);
  704. return ret;
  705. }
  706. return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
  707. in_modifier, op_modifier, op, timeout);
  708. }
  709. EXPORT_SYMBOL_GPL(__mlx4_cmd);
  710. int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
  711. {
  712. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
  713. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  714. }
  715. static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
  716. int slave, u64 slave_addr,
  717. int size, int is_read)
  718. {
  719. u64 in_param;
  720. u64 out_param;
  721. if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
  722. (slave & ~0x7f) | (size & 0xff)) {
  723. mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
  724. slave_addr, master_addr, slave, size);
  725. return -EINVAL;
  726. }
  727. if (is_read) {
  728. in_param = (u64) slave | slave_addr;
  729. out_param = (u64) dev->caps.function | master_addr;
  730. } else {
  731. in_param = (u64) dev->caps.function | master_addr;
  732. out_param = (u64) slave | slave_addr;
  733. }
  734. return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
  735. MLX4_CMD_ACCESS_MEM,
  736. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  737. }
  738. static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
  739. struct mlx4_cmd_mailbox *inbox,
  740. struct mlx4_cmd_mailbox *outbox)
  741. {
  742. struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
  743. struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
  744. int err;
  745. int i;
  746. if (index & 0x1f)
  747. return -EINVAL;
  748. in_mad->attr_mod = cpu_to_be32(index / 32);
  749. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  750. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  751. MLX4_CMD_NATIVE);
  752. if (err)
  753. return err;
  754. for (i = 0; i < 32; ++i)
  755. pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
  756. return err;
  757. }
  758. static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
  759. struct mlx4_cmd_mailbox *inbox,
  760. struct mlx4_cmd_mailbox *outbox)
  761. {
  762. int i;
  763. int err;
  764. for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
  765. err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
  766. if (err)
  767. return err;
  768. }
  769. return 0;
  770. }
  771. #define PORT_CAPABILITY_LOCATION_IN_SMP 20
  772. #define PORT_STATE_OFFSET 32
  773. static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
  774. {
  775. if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
  776. return IB_PORT_ACTIVE;
  777. else
  778. return IB_PORT_DOWN;
  779. }
  780. static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
  781. struct mlx4_vhcr *vhcr,
  782. struct mlx4_cmd_mailbox *inbox,
  783. struct mlx4_cmd_mailbox *outbox,
  784. struct mlx4_cmd_info *cmd)
  785. {
  786. struct ib_smp *smp = inbox->buf;
  787. u32 index;
  788. u8 port, slave_port;
  789. u8 opcode_modifier;
  790. u16 *table;
  791. int err;
  792. int vidx, pidx;
  793. int network_view;
  794. struct mlx4_priv *priv = mlx4_priv(dev);
  795. struct ib_smp *outsmp = outbox->buf;
  796. __be16 *outtab = (__be16 *)(outsmp->data);
  797. __be32 slave_cap_mask;
  798. __be64 slave_node_guid;
  799. slave_port = vhcr->in_modifier;
  800. port = mlx4_slave_convert_port(dev, slave, slave_port);
  801. /* network-view bit is for driver use only, and should not be passed to FW */
  802. opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
  803. network_view = !!(vhcr->op_modifier & 0x8);
  804. if (smp->base_version == 1 &&
  805. smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  806. smp->class_version == 1) {
  807. /* host view is paravirtualized */
  808. if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
  809. if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  810. index = be32_to_cpu(smp->attr_mod);
  811. if (port < 1 || port > dev->caps.num_ports)
  812. return -EINVAL;
  813. table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
  814. sizeof(*table) * 32, GFP_KERNEL);
  815. if (!table)
  816. return -ENOMEM;
  817. /* need to get the full pkey table because the paravirtualized
  818. * pkeys may be scattered among several pkey blocks.
  819. */
  820. err = get_full_pkey_table(dev, port, table, inbox, outbox);
  821. if (!err) {
  822. for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
  823. pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
  824. outtab[vidx % 32] = cpu_to_be16(table[pidx]);
  825. }
  826. }
  827. kfree(table);
  828. return err;
  829. }
  830. if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
  831. /*get the slave specific caps:*/
  832. /*do the command */
  833. smp->attr_mod = cpu_to_be32(port);
  834. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  835. port, opcode_modifier,
  836. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  837. /* modify the response for slaves */
  838. if (!err && slave != mlx4_master_func_num(dev)) {
  839. u8 *state = outsmp->data + PORT_STATE_OFFSET;
  840. *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
  841. slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  842. memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
  843. }
  844. return err;
  845. }
  846. if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
  847. __be64 guid = mlx4_get_admin_guid(dev, slave,
  848. port);
  849. /* set the PF admin guid to the FW/HW burned
  850. * GUID, if it wasn't yet set
  851. */
  852. if (slave == 0 && guid == 0) {
  853. smp->attr_mod = 0;
  854. err = mlx4_cmd_box(dev,
  855. inbox->dma,
  856. outbox->dma,
  857. vhcr->in_modifier,
  858. opcode_modifier,
  859. vhcr->op,
  860. MLX4_CMD_TIME_CLASS_C,
  861. MLX4_CMD_NATIVE);
  862. if (err)
  863. return err;
  864. mlx4_set_admin_guid(dev,
  865. *(__be64 *)outsmp->
  866. data, slave, port);
  867. } else {
  868. memcpy(outsmp->data, &guid, 8);
  869. }
  870. /* clean all other gids */
  871. memset(outsmp->data + 8, 0, 56);
  872. return 0;
  873. }
  874. if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
  875. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  876. port, opcode_modifier,
  877. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  878. if (!err) {
  879. slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
  880. memcpy(outsmp->data + 12, &slave_node_guid, 8);
  881. }
  882. return err;
  883. }
  884. }
  885. }
  886. /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
  887. * These are the MADs used by ib verbs (such as ib_query_gids).
  888. */
  889. if (slave != mlx4_master_func_num(dev) &&
  890. !mlx4_vf_smi_enabled(dev, slave, port)) {
  891. if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  892. smp->method == IB_MGMT_METHOD_GET) || network_view) {
  893. 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",
  894. slave, smp->mgmt_class, smp->method,
  895. network_view ? "Network" : "Host",
  896. be16_to_cpu(smp->attr_id));
  897. return -EPERM;
  898. }
  899. }
  900. return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  901. vhcr->in_modifier, opcode_modifier,
  902. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  903. }
  904. static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
  905. struct mlx4_vhcr *vhcr,
  906. struct mlx4_cmd_mailbox *inbox,
  907. struct mlx4_cmd_mailbox *outbox,
  908. struct mlx4_cmd_info *cmd)
  909. {
  910. return -EPERM;
  911. }
  912. int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
  913. struct mlx4_vhcr *vhcr,
  914. struct mlx4_cmd_mailbox *inbox,
  915. struct mlx4_cmd_mailbox *outbox,
  916. struct mlx4_cmd_info *cmd)
  917. {
  918. u64 in_param;
  919. u64 out_param;
  920. int err;
  921. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  922. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  923. if (cmd->encode_slave_id) {
  924. in_param &= 0xffffffffffffff00ll;
  925. in_param |= slave;
  926. }
  927. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  928. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  929. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  930. if (cmd->out_is_imm)
  931. vhcr->out_param = out_param;
  932. return err;
  933. }
  934. static struct mlx4_cmd_info cmd_info[] = {
  935. {
  936. .opcode = MLX4_CMD_QUERY_FW,
  937. .has_inbox = false,
  938. .has_outbox = true,
  939. .out_is_imm = false,
  940. .encode_slave_id = false,
  941. .verify = NULL,
  942. .wrapper = mlx4_QUERY_FW_wrapper
  943. },
  944. {
  945. .opcode = MLX4_CMD_QUERY_HCA,
  946. .has_inbox = false,
  947. .has_outbox = true,
  948. .out_is_imm = false,
  949. .encode_slave_id = false,
  950. .verify = NULL,
  951. .wrapper = NULL
  952. },
  953. {
  954. .opcode = MLX4_CMD_QUERY_DEV_CAP,
  955. .has_inbox = false,
  956. .has_outbox = true,
  957. .out_is_imm = false,
  958. .encode_slave_id = false,
  959. .verify = NULL,
  960. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  961. },
  962. {
  963. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  964. .has_inbox = false,
  965. .has_outbox = true,
  966. .out_is_imm = false,
  967. .encode_slave_id = false,
  968. .verify = NULL,
  969. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  970. },
  971. {
  972. .opcode = MLX4_CMD_QUERY_ADAPTER,
  973. .has_inbox = false,
  974. .has_outbox = true,
  975. .out_is_imm = false,
  976. .encode_slave_id = false,
  977. .verify = NULL,
  978. .wrapper = NULL
  979. },
  980. {
  981. .opcode = MLX4_CMD_INIT_PORT,
  982. .has_inbox = false,
  983. .has_outbox = false,
  984. .out_is_imm = false,
  985. .encode_slave_id = false,
  986. .verify = NULL,
  987. .wrapper = mlx4_INIT_PORT_wrapper
  988. },
  989. {
  990. .opcode = MLX4_CMD_CLOSE_PORT,
  991. .has_inbox = false,
  992. .has_outbox = false,
  993. .out_is_imm = false,
  994. .encode_slave_id = false,
  995. .verify = NULL,
  996. .wrapper = mlx4_CLOSE_PORT_wrapper
  997. },
  998. {
  999. .opcode = MLX4_CMD_QUERY_PORT,
  1000. .has_inbox = false,
  1001. .has_outbox = true,
  1002. .out_is_imm = false,
  1003. .encode_slave_id = false,
  1004. .verify = NULL,
  1005. .wrapper = mlx4_QUERY_PORT_wrapper
  1006. },
  1007. {
  1008. .opcode = MLX4_CMD_SET_PORT,
  1009. .has_inbox = true,
  1010. .has_outbox = false,
  1011. .out_is_imm = false,
  1012. .encode_slave_id = false,
  1013. .verify = NULL,
  1014. .wrapper = mlx4_SET_PORT_wrapper
  1015. },
  1016. {
  1017. .opcode = MLX4_CMD_MAP_EQ,
  1018. .has_inbox = false,
  1019. .has_outbox = false,
  1020. .out_is_imm = false,
  1021. .encode_slave_id = false,
  1022. .verify = NULL,
  1023. .wrapper = mlx4_MAP_EQ_wrapper
  1024. },
  1025. {
  1026. .opcode = MLX4_CMD_SW2HW_EQ,
  1027. .has_inbox = true,
  1028. .has_outbox = false,
  1029. .out_is_imm = false,
  1030. .encode_slave_id = true,
  1031. .verify = NULL,
  1032. .wrapper = mlx4_SW2HW_EQ_wrapper
  1033. },
  1034. {
  1035. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  1036. .has_inbox = false,
  1037. .has_outbox = false,
  1038. .out_is_imm = false,
  1039. .encode_slave_id = false,
  1040. .verify = NULL,
  1041. .wrapper = NULL
  1042. },
  1043. {
  1044. .opcode = MLX4_CMD_NOP,
  1045. .has_inbox = false,
  1046. .has_outbox = false,
  1047. .out_is_imm = false,
  1048. .encode_slave_id = false,
  1049. .verify = NULL,
  1050. .wrapper = NULL
  1051. },
  1052. {
  1053. .opcode = MLX4_CMD_CONFIG_DEV,
  1054. .has_inbox = false,
  1055. .has_outbox = true,
  1056. .out_is_imm = false,
  1057. .encode_slave_id = false,
  1058. .verify = NULL,
  1059. .wrapper = mlx4_CONFIG_DEV_wrapper
  1060. },
  1061. {
  1062. .opcode = MLX4_CMD_ALLOC_RES,
  1063. .has_inbox = false,
  1064. .has_outbox = false,
  1065. .out_is_imm = true,
  1066. .encode_slave_id = false,
  1067. .verify = NULL,
  1068. .wrapper = mlx4_ALLOC_RES_wrapper
  1069. },
  1070. {
  1071. .opcode = MLX4_CMD_FREE_RES,
  1072. .has_inbox = false,
  1073. .has_outbox = false,
  1074. .out_is_imm = false,
  1075. .encode_slave_id = false,
  1076. .verify = NULL,
  1077. .wrapper = mlx4_FREE_RES_wrapper
  1078. },
  1079. {
  1080. .opcode = MLX4_CMD_SW2HW_MPT,
  1081. .has_inbox = true,
  1082. .has_outbox = false,
  1083. .out_is_imm = false,
  1084. .encode_slave_id = true,
  1085. .verify = NULL,
  1086. .wrapper = mlx4_SW2HW_MPT_wrapper
  1087. },
  1088. {
  1089. .opcode = MLX4_CMD_QUERY_MPT,
  1090. .has_inbox = false,
  1091. .has_outbox = true,
  1092. .out_is_imm = false,
  1093. .encode_slave_id = false,
  1094. .verify = NULL,
  1095. .wrapper = mlx4_QUERY_MPT_wrapper
  1096. },
  1097. {
  1098. .opcode = MLX4_CMD_HW2SW_MPT,
  1099. .has_inbox = false,
  1100. .has_outbox = false,
  1101. .out_is_imm = false,
  1102. .encode_slave_id = false,
  1103. .verify = NULL,
  1104. .wrapper = mlx4_HW2SW_MPT_wrapper
  1105. },
  1106. {
  1107. .opcode = MLX4_CMD_READ_MTT,
  1108. .has_inbox = false,
  1109. .has_outbox = true,
  1110. .out_is_imm = false,
  1111. .encode_slave_id = false,
  1112. .verify = NULL,
  1113. .wrapper = NULL
  1114. },
  1115. {
  1116. .opcode = MLX4_CMD_WRITE_MTT,
  1117. .has_inbox = true,
  1118. .has_outbox = false,
  1119. .out_is_imm = false,
  1120. .encode_slave_id = false,
  1121. .verify = NULL,
  1122. .wrapper = mlx4_WRITE_MTT_wrapper
  1123. },
  1124. {
  1125. .opcode = MLX4_CMD_SYNC_TPT,
  1126. .has_inbox = true,
  1127. .has_outbox = false,
  1128. .out_is_imm = false,
  1129. .encode_slave_id = false,
  1130. .verify = NULL,
  1131. .wrapper = NULL
  1132. },
  1133. {
  1134. .opcode = MLX4_CMD_HW2SW_EQ,
  1135. .has_inbox = false,
  1136. .has_outbox = false,
  1137. .out_is_imm = false,
  1138. .encode_slave_id = true,
  1139. .verify = NULL,
  1140. .wrapper = mlx4_HW2SW_EQ_wrapper
  1141. },
  1142. {
  1143. .opcode = MLX4_CMD_QUERY_EQ,
  1144. .has_inbox = false,
  1145. .has_outbox = true,
  1146. .out_is_imm = false,
  1147. .encode_slave_id = true,
  1148. .verify = NULL,
  1149. .wrapper = mlx4_QUERY_EQ_wrapper
  1150. },
  1151. {
  1152. .opcode = MLX4_CMD_SW2HW_CQ,
  1153. .has_inbox = true,
  1154. .has_outbox = false,
  1155. .out_is_imm = false,
  1156. .encode_slave_id = true,
  1157. .verify = NULL,
  1158. .wrapper = mlx4_SW2HW_CQ_wrapper
  1159. },
  1160. {
  1161. .opcode = MLX4_CMD_HW2SW_CQ,
  1162. .has_inbox = false,
  1163. .has_outbox = false,
  1164. .out_is_imm = false,
  1165. .encode_slave_id = false,
  1166. .verify = NULL,
  1167. .wrapper = mlx4_HW2SW_CQ_wrapper
  1168. },
  1169. {
  1170. .opcode = MLX4_CMD_QUERY_CQ,
  1171. .has_inbox = false,
  1172. .has_outbox = true,
  1173. .out_is_imm = false,
  1174. .encode_slave_id = false,
  1175. .verify = NULL,
  1176. .wrapper = mlx4_QUERY_CQ_wrapper
  1177. },
  1178. {
  1179. .opcode = MLX4_CMD_MODIFY_CQ,
  1180. .has_inbox = true,
  1181. .has_outbox = false,
  1182. .out_is_imm = true,
  1183. .encode_slave_id = false,
  1184. .verify = NULL,
  1185. .wrapper = mlx4_MODIFY_CQ_wrapper
  1186. },
  1187. {
  1188. .opcode = MLX4_CMD_SW2HW_SRQ,
  1189. .has_inbox = true,
  1190. .has_outbox = false,
  1191. .out_is_imm = false,
  1192. .encode_slave_id = true,
  1193. .verify = NULL,
  1194. .wrapper = mlx4_SW2HW_SRQ_wrapper
  1195. },
  1196. {
  1197. .opcode = MLX4_CMD_HW2SW_SRQ,
  1198. .has_inbox = false,
  1199. .has_outbox = false,
  1200. .out_is_imm = false,
  1201. .encode_slave_id = false,
  1202. .verify = NULL,
  1203. .wrapper = mlx4_HW2SW_SRQ_wrapper
  1204. },
  1205. {
  1206. .opcode = MLX4_CMD_QUERY_SRQ,
  1207. .has_inbox = false,
  1208. .has_outbox = true,
  1209. .out_is_imm = false,
  1210. .encode_slave_id = false,
  1211. .verify = NULL,
  1212. .wrapper = mlx4_QUERY_SRQ_wrapper
  1213. },
  1214. {
  1215. .opcode = MLX4_CMD_ARM_SRQ,
  1216. .has_inbox = false,
  1217. .has_outbox = false,
  1218. .out_is_imm = false,
  1219. .encode_slave_id = false,
  1220. .verify = NULL,
  1221. .wrapper = mlx4_ARM_SRQ_wrapper
  1222. },
  1223. {
  1224. .opcode = MLX4_CMD_RST2INIT_QP,
  1225. .has_inbox = true,
  1226. .has_outbox = false,
  1227. .out_is_imm = false,
  1228. .encode_slave_id = true,
  1229. .verify = NULL,
  1230. .wrapper = mlx4_RST2INIT_QP_wrapper
  1231. },
  1232. {
  1233. .opcode = MLX4_CMD_INIT2INIT_QP,
  1234. .has_inbox = true,
  1235. .has_outbox = false,
  1236. .out_is_imm = false,
  1237. .encode_slave_id = false,
  1238. .verify = NULL,
  1239. .wrapper = mlx4_INIT2INIT_QP_wrapper
  1240. },
  1241. {
  1242. .opcode = MLX4_CMD_INIT2RTR_QP,
  1243. .has_inbox = true,
  1244. .has_outbox = false,
  1245. .out_is_imm = false,
  1246. .encode_slave_id = false,
  1247. .verify = NULL,
  1248. .wrapper = mlx4_INIT2RTR_QP_wrapper
  1249. },
  1250. {
  1251. .opcode = MLX4_CMD_RTR2RTS_QP,
  1252. .has_inbox = true,
  1253. .has_outbox = false,
  1254. .out_is_imm = false,
  1255. .encode_slave_id = false,
  1256. .verify = NULL,
  1257. .wrapper = mlx4_RTR2RTS_QP_wrapper
  1258. },
  1259. {
  1260. .opcode = MLX4_CMD_RTS2RTS_QP,
  1261. .has_inbox = true,
  1262. .has_outbox = false,
  1263. .out_is_imm = false,
  1264. .encode_slave_id = false,
  1265. .verify = NULL,
  1266. .wrapper = mlx4_RTS2RTS_QP_wrapper
  1267. },
  1268. {
  1269. .opcode = MLX4_CMD_SQERR2RTS_QP,
  1270. .has_inbox = true,
  1271. .has_outbox = false,
  1272. .out_is_imm = false,
  1273. .encode_slave_id = false,
  1274. .verify = NULL,
  1275. .wrapper = mlx4_SQERR2RTS_QP_wrapper
  1276. },
  1277. {
  1278. .opcode = MLX4_CMD_2ERR_QP,
  1279. .has_inbox = false,
  1280. .has_outbox = false,
  1281. .out_is_imm = false,
  1282. .encode_slave_id = false,
  1283. .verify = NULL,
  1284. .wrapper = mlx4_GEN_QP_wrapper
  1285. },
  1286. {
  1287. .opcode = MLX4_CMD_RTS2SQD_QP,
  1288. .has_inbox = false,
  1289. .has_outbox = false,
  1290. .out_is_imm = false,
  1291. .encode_slave_id = false,
  1292. .verify = NULL,
  1293. .wrapper = mlx4_GEN_QP_wrapper
  1294. },
  1295. {
  1296. .opcode = MLX4_CMD_SQD2SQD_QP,
  1297. .has_inbox = true,
  1298. .has_outbox = false,
  1299. .out_is_imm = false,
  1300. .encode_slave_id = false,
  1301. .verify = NULL,
  1302. .wrapper = mlx4_SQD2SQD_QP_wrapper
  1303. },
  1304. {
  1305. .opcode = MLX4_CMD_SQD2RTS_QP,
  1306. .has_inbox = true,
  1307. .has_outbox = false,
  1308. .out_is_imm = false,
  1309. .encode_slave_id = false,
  1310. .verify = NULL,
  1311. .wrapper = mlx4_SQD2RTS_QP_wrapper
  1312. },
  1313. {
  1314. .opcode = MLX4_CMD_2RST_QP,
  1315. .has_inbox = false,
  1316. .has_outbox = false,
  1317. .out_is_imm = false,
  1318. .encode_slave_id = false,
  1319. .verify = NULL,
  1320. .wrapper = mlx4_2RST_QP_wrapper
  1321. },
  1322. {
  1323. .opcode = MLX4_CMD_QUERY_QP,
  1324. .has_inbox = false,
  1325. .has_outbox = true,
  1326. .out_is_imm = false,
  1327. .encode_slave_id = false,
  1328. .verify = NULL,
  1329. .wrapper = mlx4_GEN_QP_wrapper
  1330. },
  1331. {
  1332. .opcode = MLX4_CMD_SUSPEND_QP,
  1333. .has_inbox = false,
  1334. .has_outbox = false,
  1335. .out_is_imm = false,
  1336. .encode_slave_id = false,
  1337. .verify = NULL,
  1338. .wrapper = mlx4_GEN_QP_wrapper
  1339. },
  1340. {
  1341. .opcode = MLX4_CMD_UNSUSPEND_QP,
  1342. .has_inbox = false,
  1343. .has_outbox = false,
  1344. .out_is_imm = false,
  1345. .encode_slave_id = false,
  1346. .verify = NULL,
  1347. .wrapper = mlx4_GEN_QP_wrapper
  1348. },
  1349. {
  1350. .opcode = MLX4_CMD_UPDATE_QP,
  1351. .has_inbox = true,
  1352. .has_outbox = false,
  1353. .out_is_imm = false,
  1354. .encode_slave_id = false,
  1355. .verify = NULL,
  1356. .wrapper = mlx4_UPDATE_QP_wrapper
  1357. },
  1358. {
  1359. .opcode = MLX4_CMD_GET_OP_REQ,
  1360. .has_inbox = false,
  1361. .has_outbox = false,
  1362. .out_is_imm = false,
  1363. .encode_slave_id = false,
  1364. .verify = NULL,
  1365. .wrapper = mlx4_CMD_EPERM_wrapper,
  1366. },
  1367. {
  1368. .opcode = MLX4_CMD_ALLOCATE_VPP,
  1369. .has_inbox = false,
  1370. .has_outbox = true,
  1371. .out_is_imm = false,
  1372. .encode_slave_id = false,
  1373. .verify = NULL,
  1374. .wrapper = mlx4_CMD_EPERM_wrapper,
  1375. },
  1376. {
  1377. .opcode = MLX4_CMD_SET_VPORT_QOS,
  1378. .has_inbox = false,
  1379. .has_outbox = true,
  1380. .out_is_imm = false,
  1381. .encode_slave_id = false,
  1382. .verify = NULL,
  1383. .wrapper = mlx4_CMD_EPERM_wrapper,
  1384. },
  1385. {
  1386. .opcode = MLX4_CMD_CONF_SPECIAL_QP,
  1387. .has_inbox = false,
  1388. .has_outbox = false,
  1389. .out_is_imm = false,
  1390. .encode_slave_id = false,
  1391. .verify = NULL, /* XXX verify: only demux can do this */
  1392. .wrapper = NULL
  1393. },
  1394. {
  1395. .opcode = MLX4_CMD_MAD_IFC,
  1396. .has_inbox = true,
  1397. .has_outbox = true,
  1398. .out_is_imm = false,
  1399. .encode_slave_id = false,
  1400. .verify = NULL,
  1401. .wrapper = mlx4_MAD_IFC_wrapper
  1402. },
  1403. {
  1404. .opcode = MLX4_CMD_MAD_DEMUX,
  1405. .has_inbox = false,
  1406. .has_outbox = false,
  1407. .out_is_imm = false,
  1408. .encode_slave_id = false,
  1409. .verify = NULL,
  1410. .wrapper = mlx4_CMD_EPERM_wrapper
  1411. },
  1412. {
  1413. .opcode = MLX4_CMD_QUERY_IF_STAT,
  1414. .has_inbox = false,
  1415. .has_outbox = true,
  1416. .out_is_imm = false,
  1417. .encode_slave_id = false,
  1418. .verify = NULL,
  1419. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  1420. },
  1421. {
  1422. .opcode = MLX4_CMD_ACCESS_REG,
  1423. .has_inbox = true,
  1424. .has_outbox = true,
  1425. .out_is_imm = false,
  1426. .encode_slave_id = false,
  1427. .verify = NULL,
  1428. .wrapper = mlx4_ACCESS_REG_wrapper,
  1429. },
  1430. {
  1431. .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
  1432. .has_inbox = false,
  1433. .has_outbox = false,
  1434. .out_is_imm = false,
  1435. .encode_slave_id = false,
  1436. .verify = NULL,
  1437. .wrapper = mlx4_CMD_EPERM_wrapper,
  1438. },
  1439. /* Native multicast commands are not available for guests */
  1440. {
  1441. .opcode = MLX4_CMD_QP_ATTACH,
  1442. .has_inbox = true,
  1443. .has_outbox = false,
  1444. .out_is_imm = false,
  1445. .encode_slave_id = false,
  1446. .verify = NULL,
  1447. .wrapper = mlx4_QP_ATTACH_wrapper
  1448. },
  1449. {
  1450. .opcode = MLX4_CMD_PROMISC,
  1451. .has_inbox = false,
  1452. .has_outbox = false,
  1453. .out_is_imm = false,
  1454. .encode_slave_id = false,
  1455. .verify = NULL,
  1456. .wrapper = mlx4_PROMISC_wrapper
  1457. },
  1458. /* Ethernet specific commands */
  1459. {
  1460. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  1461. .has_inbox = true,
  1462. .has_outbox = false,
  1463. .out_is_imm = false,
  1464. .encode_slave_id = false,
  1465. .verify = NULL,
  1466. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  1467. },
  1468. {
  1469. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  1470. .has_inbox = false,
  1471. .has_outbox = false,
  1472. .out_is_imm = false,
  1473. .encode_slave_id = false,
  1474. .verify = NULL,
  1475. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  1476. },
  1477. {
  1478. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  1479. .has_inbox = false,
  1480. .has_outbox = true,
  1481. .out_is_imm = false,
  1482. .encode_slave_id = false,
  1483. .verify = NULL,
  1484. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  1485. },
  1486. {
  1487. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  1488. .has_inbox = false,
  1489. .has_outbox = false,
  1490. .out_is_imm = false,
  1491. .encode_slave_id = false,
  1492. .verify = NULL,
  1493. .wrapper = NULL
  1494. },
  1495. /* flow steering commands */
  1496. {
  1497. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1498. .has_inbox = true,
  1499. .has_outbox = false,
  1500. .out_is_imm = true,
  1501. .encode_slave_id = false,
  1502. .verify = NULL,
  1503. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1504. },
  1505. {
  1506. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1507. .has_inbox = false,
  1508. .has_outbox = false,
  1509. .out_is_imm = false,
  1510. .encode_slave_id = false,
  1511. .verify = NULL,
  1512. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1513. },
  1514. {
  1515. .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
  1516. .has_inbox = false,
  1517. .has_outbox = false,
  1518. .out_is_imm = false,
  1519. .encode_slave_id = false,
  1520. .verify = NULL,
  1521. .wrapper = mlx4_CMD_EPERM_wrapper
  1522. },
  1523. {
  1524. .opcode = MLX4_CMD_VIRT_PORT_MAP,
  1525. .has_inbox = false,
  1526. .has_outbox = false,
  1527. .out_is_imm = false,
  1528. .encode_slave_id = false,
  1529. .verify = NULL,
  1530. .wrapper = mlx4_CMD_EPERM_wrapper
  1531. },
  1532. };
  1533. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1534. struct mlx4_vhcr_cmd *in_vhcr)
  1535. {
  1536. struct mlx4_priv *priv = mlx4_priv(dev);
  1537. struct mlx4_cmd_info *cmd = NULL;
  1538. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1539. struct mlx4_vhcr *vhcr;
  1540. struct mlx4_cmd_mailbox *inbox = NULL;
  1541. struct mlx4_cmd_mailbox *outbox = NULL;
  1542. u64 in_param;
  1543. u64 out_param;
  1544. int ret = 0;
  1545. int i;
  1546. int err = 0;
  1547. /* Create sw representation of Virtual HCR */
  1548. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1549. if (!vhcr)
  1550. return -ENOMEM;
  1551. /* DMA in the vHCR */
  1552. if (!in_vhcr) {
  1553. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1554. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1555. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1556. MLX4_ACCESS_MEM_ALIGN), 1);
  1557. if (ret) {
  1558. if (!(dev->persist->state &
  1559. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1560. mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
  1561. __func__, ret);
  1562. kfree(vhcr);
  1563. return ret;
  1564. }
  1565. }
  1566. /* Fill SW VHCR fields */
  1567. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1568. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1569. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1570. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1571. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1572. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1573. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1574. /* Lookup command */
  1575. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1576. if (vhcr->op == cmd_info[i].opcode) {
  1577. cmd = &cmd_info[i];
  1578. break;
  1579. }
  1580. }
  1581. if (!cmd) {
  1582. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1583. vhcr->op, slave);
  1584. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1585. goto out_status;
  1586. }
  1587. /* Read inbox */
  1588. if (cmd->has_inbox) {
  1589. vhcr->in_param &= INBOX_MASK;
  1590. inbox = mlx4_alloc_cmd_mailbox(dev);
  1591. if (IS_ERR(inbox)) {
  1592. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1593. inbox = NULL;
  1594. goto out_status;
  1595. }
  1596. ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1597. vhcr->in_param,
  1598. MLX4_MAILBOX_SIZE, 1);
  1599. if (ret) {
  1600. if (!(dev->persist->state &
  1601. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1602. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1603. __func__, cmd->opcode);
  1604. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1605. goto out_status;
  1606. }
  1607. }
  1608. /* Apply permission and bound checks if applicable */
  1609. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1610. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
  1611. vhcr->op, slave, vhcr->in_modifier);
  1612. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1613. goto out_status;
  1614. }
  1615. /* Allocate outbox */
  1616. if (cmd->has_outbox) {
  1617. outbox = mlx4_alloc_cmd_mailbox(dev);
  1618. if (IS_ERR(outbox)) {
  1619. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1620. outbox = NULL;
  1621. goto out_status;
  1622. }
  1623. }
  1624. /* Execute the command! */
  1625. if (cmd->wrapper) {
  1626. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1627. cmd);
  1628. if (cmd->out_is_imm)
  1629. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1630. } else {
  1631. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1632. vhcr->in_param;
  1633. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1634. vhcr->out_param;
  1635. err = __mlx4_cmd(dev, in_param, &out_param,
  1636. cmd->out_is_imm, vhcr->in_modifier,
  1637. vhcr->op_modifier, vhcr->op,
  1638. MLX4_CMD_TIME_CLASS_A,
  1639. MLX4_CMD_NATIVE);
  1640. if (cmd->out_is_imm) {
  1641. vhcr->out_param = out_param;
  1642. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1643. }
  1644. }
  1645. if (err) {
  1646. if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1647. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
  1648. vhcr->op, slave, vhcr->errno, err);
  1649. vhcr_cmd->status = mlx4_errno_to_status(err);
  1650. goto out_status;
  1651. }
  1652. /* Write outbox if command completed successfully */
  1653. if (cmd->has_outbox && !vhcr_cmd->status) {
  1654. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1655. vhcr->out_param,
  1656. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1657. if (ret) {
  1658. /* If we failed to write back the outbox after the
  1659. *command was successfully executed, we must fail this
  1660. * slave, as it is now in undefined state */
  1661. if (!(dev->persist->state &
  1662. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1663. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1664. goto out;
  1665. }
  1666. }
  1667. out_status:
  1668. /* DMA back vhcr result */
  1669. if (!in_vhcr) {
  1670. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1671. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1672. ALIGN(sizeof(struct mlx4_vhcr),
  1673. MLX4_ACCESS_MEM_ALIGN),
  1674. MLX4_CMD_WRAPPED);
  1675. if (ret)
  1676. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1677. __func__);
  1678. else if (vhcr->e_bit &&
  1679. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1680. mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
  1681. slave);
  1682. }
  1683. out:
  1684. kfree(vhcr);
  1685. mlx4_free_cmd_mailbox(dev, inbox);
  1686. mlx4_free_cmd_mailbox(dev, outbox);
  1687. return ret;
  1688. }
  1689. static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
  1690. int slave, int port)
  1691. {
  1692. struct mlx4_vport_oper_state *vp_oper;
  1693. struct mlx4_vport_state *vp_admin;
  1694. struct mlx4_vf_immed_vlan_work *work;
  1695. struct mlx4_dev *dev = &(priv->dev);
  1696. int err;
  1697. int admin_vlan_ix = NO_INDX;
  1698. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1699. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1700. if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
  1701. vp_oper->state.default_qos == vp_admin->default_qos &&
  1702. vp_oper->state.vlan_proto == vp_admin->vlan_proto &&
  1703. vp_oper->state.link_state == vp_admin->link_state &&
  1704. vp_oper->state.qos_vport == vp_admin->qos_vport)
  1705. return 0;
  1706. if (!(priv->mfunc.master.slave_state[slave].active &&
  1707. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
  1708. /* even if the UPDATE_QP command isn't supported, we still want
  1709. * to set this VF link according to the admin directive
  1710. */
  1711. vp_oper->state.link_state = vp_admin->link_state;
  1712. return -1;
  1713. }
  1714. mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
  1715. slave, port);
  1716. mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
  1717. vp_admin->default_vlan, vp_admin->default_qos,
  1718. vp_admin->link_state);
  1719. work = kzalloc(sizeof(*work), GFP_KERNEL);
  1720. if (!work)
  1721. return -ENOMEM;
  1722. if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
  1723. if (MLX4_VGT != vp_admin->default_vlan) {
  1724. err = __mlx4_register_vlan(&priv->dev, port,
  1725. vp_admin->default_vlan,
  1726. &admin_vlan_ix);
  1727. if (err) {
  1728. kfree(work);
  1729. mlx4_warn(&priv->dev,
  1730. "No vlan resources slave %d, port %d\n",
  1731. slave, port);
  1732. return err;
  1733. }
  1734. } else {
  1735. admin_vlan_ix = NO_INDX;
  1736. }
  1737. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
  1738. mlx4_dbg(&priv->dev,
  1739. "alloc vlan %d idx %d slave %d port %d\n",
  1740. (int)(vp_admin->default_vlan),
  1741. admin_vlan_ix, slave, port);
  1742. }
  1743. /* save original vlan ix and vlan id */
  1744. work->orig_vlan_id = vp_oper->state.default_vlan;
  1745. work->orig_vlan_ix = vp_oper->vlan_idx;
  1746. /* handle new qos */
  1747. if (vp_oper->state.default_qos != vp_admin->default_qos)
  1748. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
  1749. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
  1750. vp_oper->vlan_idx = admin_vlan_ix;
  1751. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1752. vp_oper->state.default_qos = vp_admin->default_qos;
  1753. vp_oper->state.vlan_proto = vp_admin->vlan_proto;
  1754. vp_oper->state.link_state = vp_admin->link_state;
  1755. vp_oper->state.qos_vport = vp_admin->qos_vport;
  1756. if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
  1757. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
  1758. /* iterate over QPs owned by this slave, using UPDATE_QP */
  1759. work->port = port;
  1760. work->slave = slave;
  1761. work->qos = vp_oper->state.default_qos;
  1762. work->qos_vport = vp_oper->state.qos_vport;
  1763. work->vlan_id = vp_oper->state.default_vlan;
  1764. work->vlan_ix = vp_oper->vlan_idx;
  1765. work->vlan_proto = vp_oper->state.vlan_proto;
  1766. work->priv = priv;
  1767. INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
  1768. queue_work(priv->mfunc.master.comm_wq, &work->work);
  1769. return 0;
  1770. }
  1771. static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
  1772. {
  1773. struct mlx4_qos_manager *port_qos_ctl;
  1774. struct mlx4_priv *priv = mlx4_priv(dev);
  1775. port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
  1776. bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
  1777. /* Enable only default prio at PF init routine */
  1778. set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
  1779. }
  1780. static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
  1781. {
  1782. int i;
  1783. int err;
  1784. int num_vfs;
  1785. u16 availible_vpp;
  1786. u8 vpp_param[MLX4_NUM_UP];
  1787. struct mlx4_qos_manager *port_qos;
  1788. struct mlx4_priv *priv = mlx4_priv(dev);
  1789. err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
  1790. if (err) {
  1791. mlx4_info(dev, "Failed query availible VPPs\n");
  1792. return;
  1793. }
  1794. port_qos = &priv->mfunc.master.qos_ctl[port];
  1795. num_vfs = (availible_vpp /
  1796. bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
  1797. for (i = 0; i < MLX4_NUM_UP; i++) {
  1798. if (test_bit(i, port_qos->priority_bm))
  1799. vpp_param[i] = num_vfs;
  1800. }
  1801. err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
  1802. if (err) {
  1803. mlx4_info(dev, "Failed allocating VPPs\n");
  1804. return;
  1805. }
  1806. /* Query actual allocated VPP, just to make sure */
  1807. err = mlx4_ALLOCATE_VPP_get(dev, port, &availible_vpp, vpp_param);
  1808. if (err) {
  1809. mlx4_info(dev, "Failed query availible VPPs\n");
  1810. return;
  1811. }
  1812. port_qos->num_of_qos_vfs = num_vfs;
  1813. mlx4_dbg(dev, "Port %d Availible VPPs %d\n", port, availible_vpp);
  1814. for (i = 0; i < MLX4_NUM_UP; i++)
  1815. mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
  1816. vpp_param[i]);
  1817. }
  1818. static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
  1819. {
  1820. int port, err;
  1821. struct mlx4_vport_state *vp_admin;
  1822. struct mlx4_vport_oper_state *vp_oper;
  1823. struct mlx4_slave_state *slave_state =
  1824. &priv->mfunc.master.slave_state[slave];
  1825. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1826. &priv->dev, slave);
  1827. int min_port = find_first_bit(actv_ports.ports,
  1828. priv->dev.caps.num_ports) + 1;
  1829. int max_port = min_port - 1 +
  1830. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1831. for (port = min_port; port <= max_port; port++) {
  1832. if (!test_bit(port - 1, actv_ports.ports))
  1833. continue;
  1834. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1835. priv->mfunc.master.vf_admin[slave].enable_smi[port];
  1836. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1837. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1838. if (vp_admin->vlan_proto != htons(ETH_P_8021AD) ||
  1839. slave_state->vst_qinq_supported) {
  1840. vp_oper->state.vlan_proto = vp_admin->vlan_proto;
  1841. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1842. vp_oper->state.default_qos = vp_admin->default_qos;
  1843. }
  1844. vp_oper->state.link_state = vp_admin->link_state;
  1845. vp_oper->state.mac = vp_admin->mac;
  1846. vp_oper->state.spoofchk = vp_admin->spoofchk;
  1847. vp_oper->state.tx_rate = vp_admin->tx_rate;
  1848. vp_oper->state.qos_vport = vp_admin->qos_vport;
  1849. vp_oper->state.guid = vp_admin->guid;
  1850. if (MLX4_VGT != vp_admin->default_vlan) {
  1851. err = __mlx4_register_vlan(&priv->dev, port,
  1852. vp_admin->default_vlan, &(vp_oper->vlan_idx));
  1853. if (err) {
  1854. vp_oper->vlan_idx = NO_INDX;
  1855. vp_oper->state.default_vlan = MLX4_VGT;
  1856. vp_oper->state.vlan_proto = htons(ETH_P_8021Q);
  1857. mlx4_warn(&priv->dev,
  1858. "No vlan resources slave %d, port %d\n",
  1859. slave, port);
  1860. return err;
  1861. }
  1862. mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
  1863. (int)(vp_oper->state.default_vlan),
  1864. vp_oper->vlan_idx, slave, port);
  1865. }
  1866. if (vp_admin->spoofchk) {
  1867. vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
  1868. port,
  1869. vp_admin->mac);
  1870. if (0 > vp_oper->mac_idx) {
  1871. err = vp_oper->mac_idx;
  1872. vp_oper->mac_idx = NO_INDX;
  1873. mlx4_warn(&priv->dev,
  1874. "No mac resources slave %d, port %d\n",
  1875. slave, port);
  1876. return err;
  1877. }
  1878. mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
  1879. vp_oper->state.mac, vp_oper->mac_idx, slave, port);
  1880. }
  1881. }
  1882. return 0;
  1883. }
  1884. static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
  1885. {
  1886. int port;
  1887. struct mlx4_vport_oper_state *vp_oper;
  1888. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1889. &priv->dev, slave);
  1890. int min_port = find_first_bit(actv_ports.ports,
  1891. priv->dev.caps.num_ports) + 1;
  1892. int max_port = min_port - 1 +
  1893. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1894. for (port = min_port; port <= max_port; port++) {
  1895. if (!test_bit(port - 1, actv_ports.ports))
  1896. continue;
  1897. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1898. MLX4_VF_SMI_DISABLED;
  1899. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1900. if (NO_INDX != vp_oper->vlan_idx) {
  1901. __mlx4_unregister_vlan(&priv->dev,
  1902. port, vp_oper->state.default_vlan);
  1903. vp_oper->vlan_idx = NO_INDX;
  1904. }
  1905. if (NO_INDX != vp_oper->mac_idx) {
  1906. __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
  1907. vp_oper->mac_idx = NO_INDX;
  1908. }
  1909. }
  1910. return;
  1911. }
  1912. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1913. u16 param, u8 toggle)
  1914. {
  1915. struct mlx4_priv *priv = mlx4_priv(dev);
  1916. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1917. u32 reply;
  1918. u8 is_going_down = 0;
  1919. int i;
  1920. unsigned long flags;
  1921. slave_state[slave].comm_toggle ^= 1;
  1922. reply = (u32) slave_state[slave].comm_toggle << 31;
  1923. if (toggle != slave_state[slave].comm_toggle) {
  1924. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
  1925. toggle, slave);
  1926. goto reset_slave;
  1927. }
  1928. if (cmd == MLX4_COMM_CMD_RESET) {
  1929. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1930. slave_state[slave].active = false;
  1931. slave_state[slave].old_vlan_api = false;
  1932. slave_state[slave].vst_qinq_supported = false;
  1933. mlx4_master_deactivate_admin_state(priv, slave);
  1934. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1935. slave_state[slave].event_eq[i].eqn = -1;
  1936. slave_state[slave].event_eq[i].token = 0;
  1937. }
  1938. /*check if we are in the middle of FLR process,
  1939. if so return "retry" status to the slave*/
  1940. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1941. goto inform_slave_state;
  1942. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1943. /* write the version in the event field */
  1944. reply |= mlx4_comm_get_version();
  1945. goto reset_slave;
  1946. }
  1947. /*command from slave in the middle of FLR*/
  1948. if (cmd != MLX4_COMM_CMD_RESET &&
  1949. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1950. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
  1951. slave, cmd);
  1952. return;
  1953. }
  1954. switch (cmd) {
  1955. case MLX4_COMM_CMD_VHCR0:
  1956. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1957. goto reset_slave;
  1958. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1959. priv->mfunc.master.slave_state[slave].cookie = 0;
  1960. break;
  1961. case MLX4_COMM_CMD_VHCR1:
  1962. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1963. goto reset_slave;
  1964. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1965. break;
  1966. case MLX4_COMM_CMD_VHCR2:
  1967. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1968. goto reset_slave;
  1969. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1970. break;
  1971. case MLX4_COMM_CMD_VHCR_EN:
  1972. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1973. goto reset_slave;
  1974. slave_state[slave].vhcr_dma |= param;
  1975. if (mlx4_master_activate_admin_state(priv, slave))
  1976. goto reset_slave;
  1977. slave_state[slave].active = true;
  1978. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1979. break;
  1980. case MLX4_COMM_CMD_VHCR_POST:
  1981. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1982. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
  1983. mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
  1984. slave, cmd, slave_state[slave].last_cmd);
  1985. goto reset_slave;
  1986. }
  1987. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1988. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1989. mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
  1990. slave);
  1991. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1992. goto reset_slave;
  1993. }
  1994. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1995. break;
  1996. default:
  1997. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1998. goto reset_slave;
  1999. }
  2000. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  2001. if (!slave_state[slave].is_slave_going_down)
  2002. slave_state[slave].last_cmd = cmd;
  2003. else
  2004. is_going_down = 1;
  2005. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  2006. if (is_going_down) {
  2007. mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
  2008. cmd, slave);
  2009. return;
  2010. }
  2011. __raw_writel((__force u32) cpu_to_be32(reply),
  2012. &priv->mfunc.comm[slave].slave_read);
  2013. mmiowb();
  2014. return;
  2015. reset_slave:
  2016. /* cleanup any slave resources */
  2017. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
  2018. mlx4_delete_all_resources_for_slave(dev, slave);
  2019. if (cmd != MLX4_COMM_CMD_RESET) {
  2020. mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
  2021. slave, cmd);
  2022. /* Turn on internal error letting slave reset itself immeditaly,
  2023. * otherwise it might take till timeout on command is passed
  2024. */
  2025. reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
  2026. }
  2027. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  2028. if (!slave_state[slave].is_slave_going_down)
  2029. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  2030. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  2031. /*with slave in the middle of flr, no need to clean resources again.*/
  2032. inform_slave_state:
  2033. memset(&slave_state[slave].event_eq, 0,
  2034. sizeof(struct mlx4_slave_event_eq_info));
  2035. __raw_writel((__force u32) cpu_to_be32(reply),
  2036. &priv->mfunc.comm[slave].slave_read);
  2037. wmb();
  2038. }
  2039. /* master command processing */
  2040. void mlx4_master_comm_channel(struct work_struct *work)
  2041. {
  2042. struct mlx4_mfunc_master_ctx *master =
  2043. container_of(work,
  2044. struct mlx4_mfunc_master_ctx,
  2045. comm_work);
  2046. struct mlx4_mfunc *mfunc =
  2047. container_of(master, struct mlx4_mfunc, master);
  2048. struct mlx4_priv *priv =
  2049. container_of(mfunc, struct mlx4_priv, mfunc);
  2050. struct mlx4_dev *dev = &priv->dev;
  2051. __be32 *bit_vec;
  2052. u32 comm_cmd;
  2053. u32 vec;
  2054. int i, j, slave;
  2055. int toggle;
  2056. int served = 0;
  2057. int reported = 0;
  2058. u32 slt;
  2059. bit_vec = master->comm_arm_bit_vector;
  2060. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  2061. vec = be32_to_cpu(bit_vec[i]);
  2062. for (j = 0; j < 32; j++) {
  2063. if (!(vec & (1 << j)))
  2064. continue;
  2065. ++reported;
  2066. slave = (i * 32) + j;
  2067. comm_cmd = swab32(readl(
  2068. &mfunc->comm[slave].slave_write));
  2069. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  2070. >> 31;
  2071. toggle = comm_cmd >> 31;
  2072. if (toggle != slt) {
  2073. if (master->slave_state[slave].comm_toggle
  2074. != slt) {
  2075. pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
  2076. slave, slt,
  2077. master->slave_state[slave].comm_toggle);
  2078. master->slave_state[slave].comm_toggle =
  2079. slt;
  2080. }
  2081. mlx4_master_do_cmd(dev, slave,
  2082. comm_cmd >> 16 & 0xff,
  2083. comm_cmd & 0xffff, toggle);
  2084. ++served;
  2085. }
  2086. }
  2087. }
  2088. if (reported && reported != served)
  2089. mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
  2090. reported, served);
  2091. if (mlx4_ARM_COMM_CHANNEL(dev))
  2092. mlx4_warn(dev, "Failed to arm comm channel events\n");
  2093. }
  2094. static int sync_toggles(struct mlx4_dev *dev)
  2095. {
  2096. struct mlx4_priv *priv = mlx4_priv(dev);
  2097. u32 wr_toggle;
  2098. u32 rd_toggle;
  2099. unsigned long end;
  2100. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
  2101. if (wr_toggle == 0xffffffff)
  2102. end = jiffies + msecs_to_jiffies(30000);
  2103. else
  2104. end = jiffies + msecs_to_jiffies(5000);
  2105. while (time_before(jiffies, end)) {
  2106. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
  2107. if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
  2108. /* PCI might be offline */
  2109. /* If device removal has been requested,
  2110. * do not continue retrying.
  2111. */
  2112. if (dev->persist->interface_state &
  2113. MLX4_INTERFACE_STATE_NOWAIT) {
  2114. mlx4_warn(dev,
  2115. "communication channel is offline\n");
  2116. return -EIO;
  2117. }
  2118. msleep(100);
  2119. wr_toggle = swab32(readl(&priv->mfunc.comm->
  2120. slave_write));
  2121. continue;
  2122. }
  2123. if (rd_toggle >> 31 == wr_toggle >> 31) {
  2124. priv->cmd.comm_toggle = rd_toggle >> 31;
  2125. return 0;
  2126. }
  2127. cond_resched();
  2128. }
  2129. /*
  2130. * we could reach here if for example the previous VM using this
  2131. * function misbehaved and left the channel with unsynced state. We
  2132. * should fix this here and give this VM a chance to use a properly
  2133. * synced channel
  2134. */
  2135. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  2136. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  2137. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  2138. priv->cmd.comm_toggle = 0;
  2139. return 0;
  2140. }
  2141. int mlx4_multi_func_init(struct mlx4_dev *dev)
  2142. {
  2143. struct mlx4_priv *priv = mlx4_priv(dev);
  2144. struct mlx4_slave_state *s_state;
  2145. int i, j, err, port;
  2146. if (mlx4_is_master(dev))
  2147. priv->mfunc.comm =
  2148. ioremap(pci_resource_start(dev->persist->pdev,
  2149. priv->fw.comm_bar) +
  2150. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  2151. else
  2152. priv->mfunc.comm =
  2153. ioremap(pci_resource_start(dev->persist->pdev, 2) +
  2154. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  2155. if (!priv->mfunc.comm) {
  2156. mlx4_err(dev, "Couldn't map communication vector\n");
  2157. goto err_vhcr;
  2158. }
  2159. if (mlx4_is_master(dev)) {
  2160. struct mlx4_vf_oper_state *vf_oper;
  2161. struct mlx4_vf_admin_state *vf_admin;
  2162. priv->mfunc.master.slave_state =
  2163. kzalloc(dev->num_slaves *
  2164. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  2165. if (!priv->mfunc.master.slave_state)
  2166. goto err_comm;
  2167. priv->mfunc.master.vf_admin =
  2168. kzalloc(dev->num_slaves *
  2169. sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
  2170. if (!priv->mfunc.master.vf_admin)
  2171. goto err_comm_admin;
  2172. priv->mfunc.master.vf_oper =
  2173. kzalloc(dev->num_slaves *
  2174. sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
  2175. if (!priv->mfunc.master.vf_oper)
  2176. goto err_comm_oper;
  2177. for (i = 0; i < dev->num_slaves; ++i) {
  2178. vf_admin = &priv->mfunc.master.vf_admin[i];
  2179. vf_oper = &priv->mfunc.master.vf_oper[i];
  2180. s_state = &priv->mfunc.master.slave_state[i];
  2181. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  2182. s_state->vst_qinq_supported = false;
  2183. mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
  2184. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  2185. s_state->event_eq[j].eqn = -1;
  2186. __raw_writel((__force u32) 0,
  2187. &priv->mfunc.comm[i].slave_write);
  2188. __raw_writel((__force u32) 0,
  2189. &priv->mfunc.comm[i].slave_read);
  2190. mmiowb();
  2191. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  2192. struct mlx4_vport_state *admin_vport;
  2193. struct mlx4_vport_state *oper_vport;
  2194. s_state->vlan_filter[port] =
  2195. kzalloc(sizeof(struct mlx4_vlan_fltr),
  2196. GFP_KERNEL);
  2197. if (!s_state->vlan_filter[port]) {
  2198. if (--port)
  2199. kfree(s_state->vlan_filter[port]);
  2200. goto err_slaves;
  2201. }
  2202. admin_vport = &vf_admin->vport[port];
  2203. oper_vport = &vf_oper->vport[port].state;
  2204. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  2205. admin_vport->default_vlan = MLX4_VGT;
  2206. oper_vport->default_vlan = MLX4_VGT;
  2207. admin_vport->qos_vport =
  2208. MLX4_VPP_DEFAULT_VPORT;
  2209. oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
  2210. admin_vport->vlan_proto = htons(ETH_P_8021Q);
  2211. oper_vport->vlan_proto = htons(ETH_P_8021Q);
  2212. vf_oper->vport[port].vlan_idx = NO_INDX;
  2213. vf_oper->vport[port].mac_idx = NO_INDX;
  2214. mlx4_set_random_admin_guid(dev, i, port);
  2215. }
  2216. spin_lock_init(&s_state->lock);
  2217. }
  2218. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
  2219. for (port = 1; port <= dev->caps.num_ports; port++) {
  2220. if (mlx4_is_eth(dev, port)) {
  2221. mlx4_set_default_port_qos(dev, port);
  2222. mlx4_allocate_port_vpps(dev, port);
  2223. }
  2224. }
  2225. }
  2226. memset(&priv->mfunc.master.cmd_eqe, 0, sizeof(struct mlx4_eqe));
  2227. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  2228. INIT_WORK(&priv->mfunc.master.comm_work,
  2229. mlx4_master_comm_channel);
  2230. INIT_WORK(&priv->mfunc.master.slave_event_work,
  2231. mlx4_gen_slave_eqe);
  2232. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  2233. mlx4_master_handle_slave_flr);
  2234. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  2235. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  2236. priv->mfunc.master.comm_wq =
  2237. create_singlethread_workqueue("mlx4_comm");
  2238. if (!priv->mfunc.master.comm_wq)
  2239. goto err_slaves;
  2240. if (mlx4_init_resource_tracker(dev))
  2241. goto err_thread;
  2242. } else {
  2243. err = sync_toggles(dev);
  2244. if (err) {
  2245. mlx4_err(dev, "Couldn't sync toggles\n");
  2246. goto err_comm;
  2247. }
  2248. }
  2249. return 0;
  2250. err_thread:
  2251. flush_workqueue(priv->mfunc.master.comm_wq);
  2252. destroy_workqueue(priv->mfunc.master.comm_wq);
  2253. err_slaves:
  2254. while (i--) {
  2255. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  2256. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  2257. }
  2258. kfree(priv->mfunc.master.vf_oper);
  2259. err_comm_oper:
  2260. kfree(priv->mfunc.master.vf_admin);
  2261. err_comm_admin:
  2262. kfree(priv->mfunc.master.slave_state);
  2263. err_comm:
  2264. iounmap(priv->mfunc.comm);
  2265. priv->mfunc.comm = NULL;
  2266. err_vhcr:
  2267. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  2268. priv->mfunc.vhcr,
  2269. priv->mfunc.vhcr_dma);
  2270. priv->mfunc.vhcr = NULL;
  2271. return -ENOMEM;
  2272. }
  2273. int mlx4_cmd_init(struct mlx4_dev *dev)
  2274. {
  2275. struct mlx4_priv *priv = mlx4_priv(dev);
  2276. int flags = 0;
  2277. if (!priv->cmd.initialized) {
  2278. init_rwsem(&priv->cmd.switch_sem);
  2279. mutex_init(&priv->cmd.slave_cmd_mutex);
  2280. sema_init(&priv->cmd.poll_sem, 1);
  2281. priv->cmd.use_events = 0;
  2282. priv->cmd.toggle = 1;
  2283. priv->cmd.initialized = 1;
  2284. flags |= MLX4_CMD_CLEANUP_STRUCT;
  2285. }
  2286. if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
  2287. priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
  2288. 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
  2289. if (!priv->cmd.hcr) {
  2290. mlx4_err(dev, "Couldn't map command register\n");
  2291. goto err;
  2292. }
  2293. flags |= MLX4_CMD_CLEANUP_HCR;
  2294. }
  2295. if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
  2296. priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
  2297. PAGE_SIZE,
  2298. &priv->mfunc.vhcr_dma,
  2299. GFP_KERNEL);
  2300. if (!priv->mfunc.vhcr)
  2301. goto err;
  2302. flags |= MLX4_CMD_CLEANUP_VHCR;
  2303. }
  2304. if (!priv->cmd.pool) {
  2305. priv->cmd.pool = pci_pool_create("mlx4_cmd",
  2306. dev->persist->pdev,
  2307. MLX4_MAILBOX_SIZE,
  2308. MLX4_MAILBOX_SIZE, 0);
  2309. if (!priv->cmd.pool)
  2310. goto err;
  2311. flags |= MLX4_CMD_CLEANUP_POOL;
  2312. }
  2313. return 0;
  2314. err:
  2315. mlx4_cmd_cleanup(dev, flags);
  2316. return -ENOMEM;
  2317. }
  2318. void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
  2319. {
  2320. struct mlx4_priv *priv = mlx4_priv(dev);
  2321. int slave;
  2322. u32 slave_read;
  2323. /* If the comm channel has not yet been initialized,
  2324. * skip reporting the internal error event to all
  2325. * the communication channels.
  2326. */
  2327. if (!priv->mfunc.comm)
  2328. return;
  2329. /* Report an internal error event to all
  2330. * communication channels.
  2331. */
  2332. for (slave = 0; slave < dev->num_slaves; slave++) {
  2333. slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
  2334. slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
  2335. __raw_writel((__force u32)cpu_to_be32(slave_read),
  2336. &priv->mfunc.comm[slave].slave_read);
  2337. /* Make sure that our comm channel write doesn't
  2338. * get mixed in with writes from another CPU.
  2339. */
  2340. mmiowb();
  2341. }
  2342. }
  2343. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  2344. {
  2345. struct mlx4_priv *priv = mlx4_priv(dev);
  2346. int i, port;
  2347. if (mlx4_is_master(dev)) {
  2348. flush_workqueue(priv->mfunc.master.comm_wq);
  2349. destroy_workqueue(priv->mfunc.master.comm_wq);
  2350. for (i = 0; i < dev->num_slaves; i++) {
  2351. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  2352. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  2353. }
  2354. kfree(priv->mfunc.master.slave_state);
  2355. kfree(priv->mfunc.master.vf_admin);
  2356. kfree(priv->mfunc.master.vf_oper);
  2357. dev->num_slaves = 0;
  2358. }
  2359. iounmap(priv->mfunc.comm);
  2360. priv->mfunc.comm = NULL;
  2361. }
  2362. void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
  2363. {
  2364. struct mlx4_priv *priv = mlx4_priv(dev);
  2365. if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
  2366. pci_pool_destroy(priv->cmd.pool);
  2367. priv->cmd.pool = NULL;
  2368. }
  2369. if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
  2370. (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
  2371. iounmap(priv->cmd.hcr);
  2372. priv->cmd.hcr = NULL;
  2373. }
  2374. if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
  2375. (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
  2376. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  2377. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  2378. priv->mfunc.vhcr = NULL;
  2379. }
  2380. if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
  2381. priv->cmd.initialized = 0;
  2382. }
  2383. /*
  2384. * Switch to using events to issue FW commands (can only be called
  2385. * after event queue for command events has been initialized).
  2386. */
  2387. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  2388. {
  2389. struct mlx4_priv *priv = mlx4_priv(dev);
  2390. int i;
  2391. int err = 0;
  2392. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  2393. sizeof (struct mlx4_cmd_context),
  2394. GFP_KERNEL);
  2395. if (!priv->cmd.context)
  2396. return -ENOMEM;
  2397. down_write(&priv->cmd.switch_sem);
  2398. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2399. priv->cmd.context[i].token = i;
  2400. priv->cmd.context[i].next = i + 1;
  2401. /* To support fatal error flow, initialize all
  2402. * cmd contexts to allow simulating completions
  2403. * with complete() at any time.
  2404. */
  2405. init_completion(&priv->cmd.context[i].done);
  2406. }
  2407. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  2408. priv->cmd.free_head = 0;
  2409. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  2410. for (priv->cmd.token_mask = 1;
  2411. priv->cmd.token_mask < priv->cmd.max_cmds;
  2412. priv->cmd.token_mask <<= 1)
  2413. ; /* nothing */
  2414. --priv->cmd.token_mask;
  2415. down(&priv->cmd.poll_sem);
  2416. priv->cmd.use_events = 1;
  2417. up_write(&priv->cmd.switch_sem);
  2418. return err;
  2419. }
  2420. /*
  2421. * Switch back to polling (used when shutting down the device)
  2422. */
  2423. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  2424. {
  2425. struct mlx4_priv *priv = mlx4_priv(dev);
  2426. int i;
  2427. down_write(&priv->cmd.switch_sem);
  2428. priv->cmd.use_events = 0;
  2429. for (i = 0; i < priv->cmd.max_cmds; ++i)
  2430. down(&priv->cmd.event_sem);
  2431. kfree(priv->cmd.context);
  2432. up(&priv->cmd.poll_sem);
  2433. up_write(&priv->cmd.switch_sem);
  2434. }
  2435. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  2436. {
  2437. struct mlx4_cmd_mailbox *mailbox;
  2438. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  2439. if (!mailbox)
  2440. return ERR_PTR(-ENOMEM);
  2441. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  2442. &mailbox->dma);
  2443. if (!mailbox->buf) {
  2444. kfree(mailbox);
  2445. return ERR_PTR(-ENOMEM);
  2446. }
  2447. memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
  2448. return mailbox;
  2449. }
  2450. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  2451. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  2452. struct mlx4_cmd_mailbox *mailbox)
  2453. {
  2454. if (!mailbox)
  2455. return;
  2456. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  2457. kfree(mailbox);
  2458. }
  2459. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  2460. u32 mlx4_comm_get_version(void)
  2461. {
  2462. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  2463. }
  2464. static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
  2465. {
  2466. if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
  2467. mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
  2468. vf, dev->persist->num_vfs);
  2469. return -EINVAL;
  2470. }
  2471. return vf+1;
  2472. }
  2473. int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
  2474. {
  2475. if (slave < 1 || slave > dev->persist->num_vfs) {
  2476. mlx4_err(dev,
  2477. "Bad slave number:%d (number of activated slaves: %lu)\n",
  2478. slave, dev->num_slaves);
  2479. return -EINVAL;
  2480. }
  2481. return slave - 1;
  2482. }
  2483. void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
  2484. {
  2485. struct mlx4_priv *priv = mlx4_priv(dev);
  2486. struct mlx4_cmd_context *context;
  2487. int i;
  2488. spin_lock(&priv->cmd.context_lock);
  2489. if (priv->cmd.context) {
  2490. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2491. context = &priv->cmd.context[i];
  2492. context->fw_status = CMD_STAT_INTERNAL_ERR;
  2493. context->result =
  2494. mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  2495. complete(&context->done);
  2496. }
  2497. }
  2498. spin_unlock(&priv->cmd.context_lock);
  2499. }
  2500. struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
  2501. {
  2502. struct mlx4_active_ports actv_ports;
  2503. int vf;
  2504. bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
  2505. if (slave == 0) {
  2506. bitmap_fill(actv_ports.ports, dev->caps.num_ports);
  2507. return actv_ports;
  2508. }
  2509. vf = mlx4_get_vf_indx(dev, slave);
  2510. if (vf < 0)
  2511. return actv_ports;
  2512. bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
  2513. min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
  2514. dev->caps.num_ports));
  2515. return actv_ports;
  2516. }
  2517. EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
  2518. int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
  2519. {
  2520. unsigned n;
  2521. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2522. unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2523. if (port <= 0 || port > m)
  2524. return -EINVAL;
  2525. n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2526. if (port <= n)
  2527. port = n + 1;
  2528. return port;
  2529. }
  2530. EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
  2531. int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
  2532. {
  2533. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2534. if (test_bit(port - 1, actv_ports.ports))
  2535. return port -
  2536. find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2537. return -1;
  2538. }
  2539. EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
  2540. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
  2541. int port)
  2542. {
  2543. unsigned i;
  2544. struct mlx4_slaves_pport slaves_pport;
  2545. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2546. if (port <= 0 || port > dev->caps.num_ports)
  2547. return slaves_pport;
  2548. for (i = 0; i < dev->persist->num_vfs + 1; i++) {
  2549. struct mlx4_active_ports actv_ports =
  2550. mlx4_get_active_ports(dev, i);
  2551. if (test_bit(port - 1, actv_ports.ports))
  2552. set_bit(i, slaves_pport.slaves);
  2553. }
  2554. return slaves_pport;
  2555. }
  2556. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
  2557. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
  2558. struct mlx4_dev *dev,
  2559. const struct mlx4_active_ports *crit_ports)
  2560. {
  2561. unsigned i;
  2562. struct mlx4_slaves_pport slaves_pport;
  2563. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2564. for (i = 0; i < dev->persist->num_vfs + 1; i++) {
  2565. struct mlx4_active_ports actv_ports =
  2566. mlx4_get_active_ports(dev, i);
  2567. if (bitmap_equal(crit_ports->ports, actv_ports.ports,
  2568. dev->caps.num_ports))
  2569. set_bit(i, slaves_pport.slaves);
  2570. }
  2571. return slaves_pport;
  2572. }
  2573. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
  2574. static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
  2575. {
  2576. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2577. int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
  2578. + 1;
  2579. int max_port = min_port +
  2580. bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2581. if (port < min_port)
  2582. port = min_port;
  2583. else if (port >= max_port)
  2584. port = max_port - 1;
  2585. return port;
  2586. }
  2587. static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
  2588. int max_tx_rate)
  2589. {
  2590. int i;
  2591. int err;
  2592. struct mlx4_qos_manager *port_qos;
  2593. struct mlx4_dev *dev = &priv->dev;
  2594. struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
  2595. port_qos = &priv->mfunc.master.qos_ctl[port];
  2596. memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
  2597. if (slave > port_qos->num_of_qos_vfs) {
  2598. mlx4_info(dev, "No availible VPP resources for this VF\n");
  2599. return -EINVAL;
  2600. }
  2601. /* Query for default QoS values from Vport 0 is needed */
  2602. err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
  2603. if (err) {
  2604. mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
  2605. return err;
  2606. }
  2607. for (i = 0; i < MLX4_NUM_UP; i++) {
  2608. if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
  2609. vpp_qos[i].max_avg_bw = max_tx_rate;
  2610. vpp_qos[i].enable = 1;
  2611. } else {
  2612. /* if user supplied tx_rate == 0, meaning no rate limit
  2613. * configuration is required. so we are leaving the
  2614. * value of max_avg_bw as queried from Vport 0.
  2615. */
  2616. vpp_qos[i].enable = 0;
  2617. }
  2618. }
  2619. err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
  2620. if (err) {
  2621. mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
  2622. return err;
  2623. }
  2624. return 0;
  2625. }
  2626. static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
  2627. struct mlx4_vport_state *vf_admin)
  2628. {
  2629. struct mlx4_qos_manager *info;
  2630. struct mlx4_priv *priv = mlx4_priv(dev);
  2631. if (!mlx4_is_master(dev) ||
  2632. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
  2633. return false;
  2634. info = &priv->mfunc.master.qos_ctl[port];
  2635. if (vf_admin->default_vlan != MLX4_VGT &&
  2636. test_bit(vf_admin->default_qos, info->priority_bm))
  2637. return true;
  2638. return false;
  2639. }
  2640. static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
  2641. struct mlx4_vport_state *vf_admin,
  2642. int vlan, int qos)
  2643. {
  2644. struct mlx4_vport_state dummy_admin = {0};
  2645. if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
  2646. !vf_admin->tx_rate)
  2647. return true;
  2648. dummy_admin.default_qos = qos;
  2649. dummy_admin.default_vlan = vlan;
  2650. /* VF wants to move to other VST state which is valid with current
  2651. * rate limit. Either differnt default vlan in VST or other
  2652. * supported QoS priority. Otherwise we don't allow this change when
  2653. * the TX rate is still configured.
  2654. */
  2655. if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
  2656. return true;
  2657. mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
  2658. (vlan == MLX4_VGT) ? "VGT" : "VST");
  2659. if (vlan != MLX4_VGT)
  2660. mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
  2661. mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
  2662. return false;
  2663. }
  2664. int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
  2665. {
  2666. struct mlx4_priv *priv = mlx4_priv(dev);
  2667. struct mlx4_vport_state *s_info;
  2668. int slave;
  2669. if (!mlx4_is_master(dev))
  2670. return -EPROTONOSUPPORT;
  2671. slave = mlx4_get_slave_indx(dev, vf);
  2672. if (slave < 0)
  2673. return -EINVAL;
  2674. port = mlx4_slaves_closest_port(dev, slave, port);
  2675. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2676. s_info->mac = mac;
  2677. mlx4_info(dev, "default mac on vf %d port %d to %llX will take effect only after vf restart\n",
  2678. vf, port, s_info->mac);
  2679. return 0;
  2680. }
  2681. EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
  2682. int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos,
  2683. __be16 proto)
  2684. {
  2685. struct mlx4_priv *priv = mlx4_priv(dev);
  2686. struct mlx4_vport_state *vf_admin;
  2687. struct mlx4_slave_state *slave_state;
  2688. struct mlx4_vport_oper_state *vf_oper;
  2689. int slave;
  2690. if ((!mlx4_is_master(dev)) ||
  2691. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2692. return -EPROTONOSUPPORT;
  2693. if ((vlan > 4095) || (qos > 7))
  2694. return -EINVAL;
  2695. if (proto == htons(ETH_P_8021AD) &&
  2696. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SVLAN_BY_QP))
  2697. return -EPROTONOSUPPORT;
  2698. if (proto != htons(ETH_P_8021Q) &&
  2699. proto != htons(ETH_P_8021AD))
  2700. return -EINVAL;
  2701. if ((proto == htons(ETH_P_8021AD)) &&
  2702. ((vlan == 0) || (vlan == MLX4_VGT)))
  2703. return -EINVAL;
  2704. slave = mlx4_get_slave_indx(dev, vf);
  2705. if (slave < 0)
  2706. return -EINVAL;
  2707. slave_state = &priv->mfunc.master.slave_state[slave];
  2708. if ((proto == htons(ETH_P_8021AD)) && (slave_state->active) &&
  2709. (!slave_state->vst_qinq_supported)) {
  2710. mlx4_err(dev, "vf %d does not support VST QinQ mode\n", vf);
  2711. return -EPROTONOSUPPORT;
  2712. }
  2713. port = mlx4_slaves_closest_port(dev, slave, port);
  2714. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2715. vf_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2716. if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
  2717. return -EPERM;
  2718. if ((0 == vlan) && (0 == qos))
  2719. vf_admin->default_vlan = MLX4_VGT;
  2720. else
  2721. vf_admin->default_vlan = vlan;
  2722. vf_admin->default_qos = qos;
  2723. vf_admin->vlan_proto = proto;
  2724. /* If rate was configured prior to VST, we saved the configured rate
  2725. * in vf_admin->rate and now, if priority supported we enforce the QoS
  2726. */
  2727. if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
  2728. vf_admin->tx_rate)
  2729. vf_admin->qos_vport = slave;
  2730. /* Try to activate new vf state without restart,
  2731. * this option is not supported while moving to VST QinQ mode.
  2732. */
  2733. if ((proto == htons(ETH_P_8021AD) &&
  2734. vf_oper->state.vlan_proto != proto) ||
  2735. mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2736. mlx4_info(dev,
  2737. "updating vf %d port %d config will take effect on next VF restart\n",
  2738. vf, port);
  2739. return 0;
  2740. }
  2741. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2742. int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
  2743. int max_tx_rate)
  2744. {
  2745. int err;
  2746. int slave;
  2747. struct mlx4_vport_state *vf_admin;
  2748. struct mlx4_priv *priv = mlx4_priv(dev);
  2749. if (!mlx4_is_master(dev) ||
  2750. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
  2751. return -EPROTONOSUPPORT;
  2752. if (min_tx_rate) {
  2753. mlx4_info(dev, "Minimum BW share not supported\n");
  2754. return -EPROTONOSUPPORT;
  2755. }
  2756. slave = mlx4_get_slave_indx(dev, vf);
  2757. if (slave < 0)
  2758. return -EINVAL;
  2759. port = mlx4_slaves_closest_port(dev, slave, port);
  2760. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2761. err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
  2762. if (err) {
  2763. mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
  2764. max_tx_rate);
  2765. return err;
  2766. }
  2767. vf_admin->tx_rate = max_tx_rate;
  2768. /* if VF is not in supported mode (VST with supported prio),
  2769. * we do not change vport configuration for its QPs, but save
  2770. * the rate, so it will be enforced when it moves to supported
  2771. * mode next time.
  2772. */
  2773. if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
  2774. mlx4_info(dev,
  2775. "rate set for VF %d when not in valid state\n", vf);
  2776. if (vf_admin->default_vlan != MLX4_VGT)
  2777. mlx4_info(dev, "VST priority not supported by QoS\n");
  2778. else
  2779. mlx4_info(dev, "VF in VGT mode (needed VST)\n");
  2780. mlx4_info(dev,
  2781. "rate %d take affect when VF moves to valid state\n",
  2782. max_tx_rate);
  2783. return 0;
  2784. }
  2785. /* If user sets rate 0 assigning default vport for its QPs */
  2786. vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
  2787. if (priv->mfunc.master.slave_state[slave].active &&
  2788. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
  2789. mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
  2790. return 0;
  2791. }
  2792. EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
  2793. /* mlx4_get_slave_default_vlan -
  2794. * return true if VST ( default vlan)
  2795. * if VST, will return vlan & qos (if not NULL)
  2796. */
  2797. bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
  2798. u16 *vlan, u8 *qos)
  2799. {
  2800. struct mlx4_vport_oper_state *vp_oper;
  2801. struct mlx4_priv *priv;
  2802. priv = mlx4_priv(dev);
  2803. port = mlx4_slaves_closest_port(dev, slave, port);
  2804. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2805. if (MLX4_VGT != vp_oper->state.default_vlan) {
  2806. if (vlan)
  2807. *vlan = vp_oper->state.default_vlan;
  2808. if (qos)
  2809. *qos = vp_oper->state.default_qos;
  2810. return true;
  2811. }
  2812. return false;
  2813. }
  2814. EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
  2815. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2816. {
  2817. struct mlx4_priv *priv = mlx4_priv(dev);
  2818. struct mlx4_vport_state *s_info;
  2819. int slave;
  2820. if ((!mlx4_is_master(dev)) ||
  2821. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  2822. return -EPROTONOSUPPORT;
  2823. slave = mlx4_get_slave_indx(dev, vf);
  2824. if (slave < 0)
  2825. return -EINVAL;
  2826. port = mlx4_slaves_closest_port(dev, slave, port);
  2827. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2828. s_info->spoofchk = setting;
  2829. return 0;
  2830. }
  2831. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2832. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2833. {
  2834. struct mlx4_priv *priv = mlx4_priv(dev);
  2835. struct mlx4_vport_state *s_info;
  2836. int slave;
  2837. if (!mlx4_is_master(dev))
  2838. return -EPROTONOSUPPORT;
  2839. slave = mlx4_get_slave_indx(dev, vf);
  2840. if (slave < 0)
  2841. return -EINVAL;
  2842. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2843. ivf->vf = vf;
  2844. /* need to convert it to a func */
  2845. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2846. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2847. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2848. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2849. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2850. ivf->mac[5] = ((s_info->mac) & 0xff);
  2851. ivf->vlan = s_info->default_vlan;
  2852. ivf->qos = s_info->default_qos;
  2853. ivf->vlan_proto = s_info->vlan_proto;
  2854. if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
  2855. ivf->max_tx_rate = s_info->tx_rate;
  2856. else
  2857. ivf->max_tx_rate = 0;
  2858. ivf->min_tx_rate = 0;
  2859. ivf->spoofchk = s_info->spoofchk;
  2860. ivf->linkstate = s_info->link_state;
  2861. return 0;
  2862. }
  2863. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2864. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2865. {
  2866. struct mlx4_priv *priv = mlx4_priv(dev);
  2867. struct mlx4_vport_state *s_info;
  2868. int slave;
  2869. u8 link_stat_event;
  2870. slave = mlx4_get_slave_indx(dev, vf);
  2871. if (slave < 0)
  2872. return -EINVAL;
  2873. port = mlx4_slaves_closest_port(dev, slave, port);
  2874. switch (link_state) {
  2875. case IFLA_VF_LINK_STATE_AUTO:
  2876. /* get current link state */
  2877. if (!priv->sense.do_sense_port[port])
  2878. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2879. else
  2880. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2881. break;
  2882. case IFLA_VF_LINK_STATE_ENABLE:
  2883. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2884. break;
  2885. case IFLA_VF_LINK_STATE_DISABLE:
  2886. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2887. break;
  2888. default:
  2889. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2890. link_state, slave, port);
  2891. return -EINVAL;
  2892. };
  2893. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2894. s_info->link_state = link_state;
  2895. /* send event */
  2896. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2897. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2898. mlx4_dbg(dev,
  2899. "updating vf %d port %d no link state HW enforcment\n",
  2900. vf, port);
  2901. return 0;
  2902. }
  2903. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
  2904. int mlx4_get_counter_stats(struct mlx4_dev *dev, int counter_index,
  2905. struct mlx4_counter *counter_stats, int reset)
  2906. {
  2907. struct mlx4_cmd_mailbox *mailbox = NULL;
  2908. struct mlx4_counter *tmp_counter;
  2909. int err;
  2910. u32 if_stat_in_mod;
  2911. if (!counter_stats)
  2912. return -EINVAL;
  2913. if (counter_index == MLX4_SINK_COUNTER_INDEX(dev))
  2914. return 0;
  2915. mailbox = mlx4_alloc_cmd_mailbox(dev);
  2916. if (IS_ERR(mailbox))
  2917. return PTR_ERR(mailbox);
  2918. memset(mailbox->buf, 0, sizeof(struct mlx4_counter));
  2919. if_stat_in_mod = counter_index;
  2920. if (reset)
  2921. if_stat_in_mod |= MLX4_QUERY_IF_STAT_RESET;
  2922. err = mlx4_cmd_box(dev, 0, mailbox->dma,
  2923. if_stat_in_mod, 0,
  2924. MLX4_CMD_QUERY_IF_STAT,
  2925. MLX4_CMD_TIME_CLASS_C,
  2926. MLX4_CMD_NATIVE);
  2927. if (err) {
  2928. mlx4_dbg(dev, "%s: failed to read statistics for counter index %d\n",
  2929. __func__, counter_index);
  2930. goto if_stat_out;
  2931. }
  2932. tmp_counter = (struct mlx4_counter *)mailbox->buf;
  2933. counter_stats->counter_mode = tmp_counter->counter_mode;
  2934. if (counter_stats->counter_mode == 0) {
  2935. counter_stats->rx_frames =
  2936. cpu_to_be64(be64_to_cpu(counter_stats->rx_frames) +
  2937. be64_to_cpu(tmp_counter->rx_frames));
  2938. counter_stats->tx_frames =
  2939. cpu_to_be64(be64_to_cpu(counter_stats->tx_frames) +
  2940. be64_to_cpu(tmp_counter->tx_frames));
  2941. counter_stats->rx_bytes =
  2942. cpu_to_be64(be64_to_cpu(counter_stats->rx_bytes) +
  2943. be64_to_cpu(tmp_counter->rx_bytes));
  2944. counter_stats->tx_bytes =
  2945. cpu_to_be64(be64_to_cpu(counter_stats->tx_bytes) +
  2946. be64_to_cpu(tmp_counter->tx_bytes));
  2947. }
  2948. if_stat_out:
  2949. mlx4_free_cmd_mailbox(dev, mailbox);
  2950. return err;
  2951. }
  2952. EXPORT_SYMBOL_GPL(mlx4_get_counter_stats);
  2953. int mlx4_get_vf_stats(struct mlx4_dev *dev, int port, int vf_idx,
  2954. struct ifla_vf_stats *vf_stats)
  2955. {
  2956. struct mlx4_counter tmp_vf_stats;
  2957. int slave;
  2958. int err = 0;
  2959. if (!vf_stats)
  2960. return -EINVAL;
  2961. if (!mlx4_is_master(dev))
  2962. return -EPROTONOSUPPORT;
  2963. slave = mlx4_get_slave_indx(dev, vf_idx);
  2964. if (slave < 0)
  2965. return -EINVAL;
  2966. port = mlx4_slaves_closest_port(dev, slave, port);
  2967. err = mlx4_calc_vf_counters(dev, slave, port, &tmp_vf_stats);
  2968. if (!err && tmp_vf_stats.counter_mode == 0) {
  2969. vf_stats->rx_packets = be64_to_cpu(tmp_vf_stats.rx_frames);
  2970. vf_stats->tx_packets = be64_to_cpu(tmp_vf_stats.tx_frames);
  2971. vf_stats->rx_bytes = be64_to_cpu(tmp_vf_stats.rx_bytes);
  2972. vf_stats->tx_bytes = be64_to_cpu(tmp_vf_stats.tx_bytes);
  2973. }
  2974. return err;
  2975. }
  2976. EXPORT_SYMBOL_GPL(mlx4_get_vf_stats);
  2977. int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
  2978. {
  2979. struct mlx4_priv *priv = mlx4_priv(dev);
  2980. if (slave < 1 || slave >= dev->num_slaves ||
  2981. port < 1 || port > MLX4_MAX_PORTS)
  2982. return 0;
  2983. return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
  2984. MLX4_VF_SMI_ENABLED;
  2985. }
  2986. EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
  2987. int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
  2988. {
  2989. struct mlx4_priv *priv = mlx4_priv(dev);
  2990. if (slave == mlx4_master_func_num(dev))
  2991. return 1;
  2992. if (slave < 1 || slave >= dev->num_slaves ||
  2993. port < 1 || port > MLX4_MAX_PORTS)
  2994. return 0;
  2995. return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
  2996. MLX4_VF_SMI_ENABLED;
  2997. }
  2998. EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
  2999. int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
  3000. int enabled)
  3001. {
  3002. struct mlx4_priv *priv = mlx4_priv(dev);
  3003. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  3004. &priv->dev, slave);
  3005. int min_port = find_first_bit(actv_ports.ports,
  3006. priv->dev.caps.num_ports) + 1;
  3007. int max_port = min_port - 1 +
  3008. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  3009. if (slave == mlx4_master_func_num(dev))
  3010. return 0;
  3011. if (slave < 1 || slave >= dev->num_slaves ||
  3012. port < 1 || port > MLX4_MAX_PORTS ||
  3013. enabled < 0 || enabled > 1)
  3014. return -EINVAL;
  3015. if (min_port == max_port && dev->caps.num_ports > 1) {
  3016. mlx4_info(dev, "SMI access disallowed for single ported VFs\n");
  3017. return -EPROTONOSUPPORT;
  3018. }
  3019. priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
  3020. return 0;
  3021. }
  3022. EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);