cmd.c 92 KB

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