cmd.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  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 <asm/io.h>
  44. #include "mlx4.h"
  45. #include "fw.h"
  46. #define CMD_POLL_TOKEN 0xffff
  47. #define INBOX_MASK 0xffffffffffffff00ULL
  48. #define CMD_CHAN_VER 1
  49. #define CMD_CHAN_IF_REV 1
  50. enum {
  51. /* command completed successfully: */
  52. CMD_STAT_OK = 0x00,
  53. /* Internal error (such as a bus error) occurred while processing command: */
  54. CMD_STAT_INTERNAL_ERR = 0x01,
  55. /* Operation/command not supported or opcode modifier not supported: */
  56. CMD_STAT_BAD_OP = 0x02,
  57. /* Parameter not supported or parameter out of range: */
  58. CMD_STAT_BAD_PARAM = 0x03,
  59. /* System not enabled or bad system state: */
  60. CMD_STAT_BAD_SYS_STATE = 0x04,
  61. /* Attempt to access reserved or unallocaterd resource: */
  62. CMD_STAT_BAD_RESOURCE = 0x05,
  63. /* Requested resource is currently executing a command, or is otherwise busy: */
  64. CMD_STAT_RESOURCE_BUSY = 0x06,
  65. /* Required capability exceeds device limits: */
  66. CMD_STAT_EXCEED_LIM = 0x08,
  67. /* Resource is not in the appropriate state or ownership: */
  68. CMD_STAT_BAD_RES_STATE = 0x09,
  69. /* Index out of range: */
  70. CMD_STAT_BAD_INDEX = 0x0a,
  71. /* FW image corrupted: */
  72. CMD_STAT_BAD_NVMEM = 0x0b,
  73. /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
  74. CMD_STAT_ICM_ERROR = 0x0c,
  75. /* Attempt to modify a QP/EE which is not in the presumed state: */
  76. CMD_STAT_BAD_QP_STATE = 0x10,
  77. /* Bad segment parameters (Address/Size): */
  78. CMD_STAT_BAD_SEG_PARAM = 0x20,
  79. /* Memory Region has Memory Windows bound to: */
  80. CMD_STAT_REG_BOUND = 0x21,
  81. /* HCA local attached memory not present: */
  82. CMD_STAT_LAM_NOT_PRE = 0x22,
  83. /* Bad management packet (silently discarded): */
  84. CMD_STAT_BAD_PKT = 0x30,
  85. /* More outstanding CQEs in CQ than new CQ size: */
  86. CMD_STAT_BAD_SIZE = 0x40,
  87. /* Multi Function device support required: */
  88. CMD_STAT_MULTI_FUNC_REQ = 0x50,
  89. };
  90. enum {
  91. HCR_IN_PARAM_OFFSET = 0x00,
  92. HCR_IN_MODIFIER_OFFSET = 0x08,
  93. HCR_OUT_PARAM_OFFSET = 0x0c,
  94. HCR_TOKEN_OFFSET = 0x14,
  95. HCR_STATUS_OFFSET = 0x18,
  96. HCR_OPMOD_SHIFT = 12,
  97. HCR_T_BIT = 21,
  98. HCR_E_BIT = 22,
  99. HCR_GO_BIT = 23
  100. };
  101. enum {
  102. GO_BIT_TIMEOUT_MSECS = 10000
  103. };
  104. enum mlx4_vlan_transition {
  105. MLX4_VLAN_TRANSITION_VST_VST = 0,
  106. MLX4_VLAN_TRANSITION_VST_VGT = 1,
  107. MLX4_VLAN_TRANSITION_VGT_VST = 2,
  108. MLX4_VLAN_TRANSITION_VGT_VGT = 3,
  109. };
  110. struct mlx4_cmd_context {
  111. struct completion done;
  112. int result;
  113. int next;
  114. u64 out_param;
  115. u16 token;
  116. u8 fw_status;
  117. };
  118. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  119. struct mlx4_vhcr_cmd *in_vhcr);
  120. static int mlx4_status_to_errno(u8 status)
  121. {
  122. static const int trans_table[] = {
  123. [CMD_STAT_INTERNAL_ERR] = -EIO,
  124. [CMD_STAT_BAD_OP] = -EPERM,
  125. [CMD_STAT_BAD_PARAM] = -EINVAL,
  126. [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
  127. [CMD_STAT_BAD_RESOURCE] = -EBADF,
  128. [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
  129. [CMD_STAT_EXCEED_LIM] = -ENOMEM,
  130. [CMD_STAT_BAD_RES_STATE] = -EBADF,
  131. [CMD_STAT_BAD_INDEX] = -EBADF,
  132. [CMD_STAT_BAD_NVMEM] = -EFAULT,
  133. [CMD_STAT_ICM_ERROR] = -ENFILE,
  134. [CMD_STAT_BAD_QP_STATE] = -EINVAL,
  135. [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
  136. [CMD_STAT_REG_BOUND] = -EBUSY,
  137. [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
  138. [CMD_STAT_BAD_PKT] = -EINVAL,
  139. [CMD_STAT_BAD_SIZE] = -ENOMEM,
  140. [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
  141. };
  142. if (status >= ARRAY_SIZE(trans_table) ||
  143. (status != CMD_STAT_OK && trans_table[status] == 0))
  144. return -EIO;
  145. return trans_table[status];
  146. }
  147. static u8 mlx4_errno_to_status(int errno)
  148. {
  149. switch (errno) {
  150. case -EPERM:
  151. return CMD_STAT_BAD_OP;
  152. case -EINVAL:
  153. return CMD_STAT_BAD_PARAM;
  154. case -ENXIO:
  155. return CMD_STAT_BAD_SYS_STATE;
  156. case -EBUSY:
  157. return CMD_STAT_RESOURCE_BUSY;
  158. case -ENOMEM:
  159. return CMD_STAT_EXCEED_LIM;
  160. case -ENFILE:
  161. return CMD_STAT_ICM_ERROR;
  162. default:
  163. return CMD_STAT_INTERNAL_ERR;
  164. }
  165. }
  166. static int comm_pending(struct mlx4_dev *dev)
  167. {
  168. struct mlx4_priv *priv = mlx4_priv(dev);
  169. u32 status = readl(&priv->mfunc.comm->slave_read);
  170. return (swab32(status) >> 31) != priv->cmd.comm_toggle;
  171. }
  172. static void mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
  173. {
  174. struct mlx4_priv *priv = mlx4_priv(dev);
  175. u32 val;
  176. priv->cmd.comm_toggle ^= 1;
  177. val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
  178. __raw_writel((__force u32) cpu_to_be32(val),
  179. &priv->mfunc.comm->slave_write);
  180. mmiowb();
  181. }
  182. static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
  183. unsigned long timeout)
  184. {
  185. struct mlx4_priv *priv = mlx4_priv(dev);
  186. unsigned long end;
  187. int err = 0;
  188. int ret_from_pending = 0;
  189. /* First, verify that the master reports correct status */
  190. if (comm_pending(dev)) {
  191. mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
  192. priv->cmd.comm_toggle, cmd);
  193. return -EAGAIN;
  194. }
  195. /* Write command */
  196. down(&priv->cmd.poll_sem);
  197. mlx4_comm_cmd_post(dev, cmd, param);
  198. end = msecs_to_jiffies(timeout) + jiffies;
  199. while (comm_pending(dev) && time_before(jiffies, end))
  200. cond_resched();
  201. ret_from_pending = comm_pending(dev);
  202. if (ret_from_pending) {
  203. /* check if the slave is trying to boot in the middle of
  204. * FLR process. The only non-zero result in the RESET command
  205. * is MLX4_DELAY_RESET_SLAVE*/
  206. if ((MLX4_COMM_CMD_RESET == cmd)) {
  207. err = MLX4_DELAY_RESET_SLAVE;
  208. } else {
  209. mlx4_warn(dev, "Communication channel timed out\n");
  210. err = -ETIMEDOUT;
  211. }
  212. }
  213. up(&priv->cmd.poll_sem);
  214. return err;
  215. }
  216. static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 op,
  217. u16 param, unsigned long timeout)
  218. {
  219. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  220. struct mlx4_cmd_context *context;
  221. unsigned long end;
  222. int err = 0;
  223. down(&cmd->event_sem);
  224. spin_lock(&cmd->context_lock);
  225. BUG_ON(cmd->free_head < 0);
  226. context = &cmd->context[cmd->free_head];
  227. context->token += cmd->token_mask + 1;
  228. cmd->free_head = context->next;
  229. spin_unlock(&cmd->context_lock);
  230. init_completion(&context->done);
  231. mlx4_comm_cmd_post(dev, op, param);
  232. if (!wait_for_completion_timeout(&context->done,
  233. msecs_to_jiffies(timeout))) {
  234. mlx4_warn(dev, "communication channel command 0x%x timed out\n",
  235. op);
  236. err = -EBUSY;
  237. goto out;
  238. }
  239. err = context->result;
  240. if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
  241. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  242. op, context->fw_status);
  243. goto out;
  244. }
  245. out:
  246. /* wait for comm channel ready
  247. * this is necessary for prevention the race
  248. * when switching between event to polling mode
  249. */
  250. end = msecs_to_jiffies(timeout) + jiffies;
  251. while (comm_pending(dev) && time_before(jiffies, end))
  252. cond_resched();
  253. spin_lock(&cmd->context_lock);
  254. context->next = cmd->free_head;
  255. cmd->free_head = context - cmd->context;
  256. spin_unlock(&cmd->context_lock);
  257. up(&cmd->event_sem);
  258. return err;
  259. }
  260. int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
  261. unsigned long timeout)
  262. {
  263. if (mlx4_priv(dev)->cmd.use_events)
  264. return mlx4_comm_cmd_wait(dev, cmd, param, timeout);
  265. return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
  266. }
  267. static int cmd_pending(struct mlx4_dev *dev)
  268. {
  269. u32 status;
  270. if (pci_channel_offline(dev->pdev))
  271. return -EIO;
  272. status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
  273. return (status & swab32(1 << HCR_GO_BIT)) ||
  274. (mlx4_priv(dev)->cmd.toggle ==
  275. !!(status & swab32(1 << HCR_T_BIT)));
  276. }
  277. static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
  278. u32 in_modifier, u8 op_modifier, u16 op, u16 token,
  279. int event)
  280. {
  281. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  282. u32 __iomem *hcr = cmd->hcr;
  283. int ret = -EAGAIN;
  284. unsigned long end;
  285. mutex_lock(&cmd->hcr_mutex);
  286. if (pci_channel_offline(dev->pdev)) {
  287. /*
  288. * Device is going through error recovery
  289. * and cannot accept commands.
  290. */
  291. ret = -EIO;
  292. goto out;
  293. }
  294. end = jiffies;
  295. if (event)
  296. end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
  297. while (cmd_pending(dev)) {
  298. if (pci_channel_offline(dev->pdev)) {
  299. /*
  300. * Device is going through error recovery
  301. * and cannot accept commands.
  302. */
  303. ret = -EIO;
  304. goto out;
  305. }
  306. if (time_after_eq(jiffies, end)) {
  307. mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
  308. goto out;
  309. }
  310. cond_resched();
  311. }
  312. /*
  313. * We use writel (instead of something like memcpy_toio)
  314. * because writes of less than 32 bits to the HCR don't work
  315. * (and some architectures such as ia64 implement memcpy_toio
  316. * in terms of writeb).
  317. */
  318. __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
  319. __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
  320. __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
  321. __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
  322. __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
  323. __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
  324. /* __raw_writel may not order writes. */
  325. wmb();
  326. __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
  327. (cmd->toggle << HCR_T_BIT) |
  328. (event ? (1 << HCR_E_BIT) : 0) |
  329. (op_modifier << HCR_OPMOD_SHIFT) |
  330. op), hcr + 6);
  331. /*
  332. * Make sure that our HCR writes don't get mixed in with
  333. * writes from another CPU starting a FW command.
  334. */
  335. mmiowb();
  336. cmd->toggle = cmd->toggle ^ 1;
  337. ret = 0;
  338. out:
  339. mutex_unlock(&cmd->hcr_mutex);
  340. return ret;
  341. }
  342. static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  343. int out_is_imm, u32 in_modifier, u8 op_modifier,
  344. u16 op, unsigned long timeout)
  345. {
  346. struct mlx4_priv *priv = mlx4_priv(dev);
  347. struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
  348. int ret;
  349. mutex_lock(&priv->cmd.slave_cmd_mutex);
  350. vhcr->in_param = cpu_to_be64(in_param);
  351. vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
  352. vhcr->in_modifier = cpu_to_be32(in_modifier);
  353. vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
  354. vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
  355. vhcr->status = 0;
  356. vhcr->flags = !!(priv->cmd.use_events) << 6;
  357. if (mlx4_is_master(dev)) {
  358. ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
  359. if (!ret) {
  360. if (out_is_imm) {
  361. if (out_param)
  362. *out_param =
  363. be64_to_cpu(vhcr->out_param);
  364. else {
  365. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  366. op);
  367. vhcr->status = CMD_STAT_BAD_PARAM;
  368. }
  369. }
  370. ret = mlx4_status_to_errno(vhcr->status);
  371. }
  372. } else {
  373. ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0,
  374. MLX4_COMM_TIME + timeout);
  375. if (!ret) {
  376. if (out_is_imm) {
  377. if (out_param)
  378. *out_param =
  379. be64_to_cpu(vhcr->out_param);
  380. else {
  381. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  382. op);
  383. vhcr->status = CMD_STAT_BAD_PARAM;
  384. }
  385. }
  386. ret = mlx4_status_to_errno(vhcr->status);
  387. } else
  388. mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n",
  389. op);
  390. }
  391. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  392. return ret;
  393. }
  394. static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  395. int out_is_imm, u32 in_modifier, u8 op_modifier,
  396. u16 op, unsigned long timeout)
  397. {
  398. struct mlx4_priv *priv = mlx4_priv(dev);
  399. void __iomem *hcr = priv->cmd.hcr;
  400. int err = 0;
  401. unsigned long end;
  402. u32 stat;
  403. down(&priv->cmd.poll_sem);
  404. if (pci_channel_offline(dev->pdev)) {
  405. /*
  406. * Device is going through error recovery
  407. * and cannot accept commands.
  408. */
  409. err = -EIO;
  410. goto out;
  411. }
  412. if (out_is_imm && !out_param) {
  413. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  414. op);
  415. err = -EINVAL;
  416. goto out;
  417. }
  418. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  419. in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
  420. if (err)
  421. goto out;
  422. end = msecs_to_jiffies(timeout) + jiffies;
  423. while (cmd_pending(dev) && time_before(jiffies, end)) {
  424. if (pci_channel_offline(dev->pdev)) {
  425. /*
  426. * Device is going through error recovery
  427. * and cannot accept commands.
  428. */
  429. err = -EIO;
  430. goto out;
  431. }
  432. cond_resched();
  433. }
  434. if (cmd_pending(dev)) {
  435. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  436. op);
  437. err = -ETIMEDOUT;
  438. goto out;
  439. }
  440. if (out_is_imm)
  441. *out_param =
  442. (u64) be32_to_cpu((__force __be32)
  443. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
  444. (u64) be32_to_cpu((__force __be32)
  445. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
  446. stat = be32_to_cpu((__force __be32)
  447. __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
  448. err = mlx4_status_to_errno(stat);
  449. if (err)
  450. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  451. op, stat);
  452. out:
  453. up(&priv->cmd.poll_sem);
  454. return err;
  455. }
  456. void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
  457. {
  458. struct mlx4_priv *priv = mlx4_priv(dev);
  459. struct mlx4_cmd_context *context =
  460. &priv->cmd.context[token & priv->cmd.token_mask];
  461. /* previously timed out command completing at long last */
  462. if (token != context->token)
  463. return;
  464. context->fw_status = status;
  465. context->result = mlx4_status_to_errno(status);
  466. context->out_param = out_param;
  467. complete(&context->done);
  468. }
  469. static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  470. int out_is_imm, u32 in_modifier, u8 op_modifier,
  471. u16 op, unsigned long timeout)
  472. {
  473. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  474. struct mlx4_cmd_context *context;
  475. int err = 0;
  476. down(&cmd->event_sem);
  477. spin_lock(&cmd->context_lock);
  478. BUG_ON(cmd->free_head < 0);
  479. context = &cmd->context[cmd->free_head];
  480. context->token += cmd->token_mask + 1;
  481. cmd->free_head = context->next;
  482. spin_unlock(&cmd->context_lock);
  483. if (out_is_imm && !out_param) {
  484. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  485. op);
  486. err = -EINVAL;
  487. goto out;
  488. }
  489. init_completion(&context->done);
  490. mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  491. in_modifier, op_modifier, op, context->token, 1);
  492. if (!wait_for_completion_timeout(&context->done,
  493. msecs_to_jiffies(timeout))) {
  494. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  495. op);
  496. err = -EBUSY;
  497. goto out;
  498. }
  499. err = context->result;
  500. if (err) {
  501. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  502. op, context->fw_status);
  503. goto out;
  504. }
  505. if (out_is_imm)
  506. *out_param = context->out_param;
  507. out:
  508. spin_lock(&cmd->context_lock);
  509. context->next = cmd->free_head;
  510. cmd->free_head = context - cmd->context;
  511. spin_unlock(&cmd->context_lock);
  512. up(&cmd->event_sem);
  513. return err;
  514. }
  515. int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  516. int out_is_imm, u32 in_modifier, u8 op_modifier,
  517. u16 op, unsigned long timeout, int native)
  518. {
  519. if (pci_channel_offline(dev->pdev))
  520. return -EIO;
  521. if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
  522. if (mlx4_priv(dev)->cmd.use_events)
  523. return mlx4_cmd_wait(dev, in_param, out_param,
  524. out_is_imm, in_modifier,
  525. op_modifier, op, timeout);
  526. else
  527. return mlx4_cmd_poll(dev, in_param, out_param,
  528. out_is_imm, in_modifier,
  529. op_modifier, op, timeout);
  530. }
  531. return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
  532. in_modifier, op_modifier, op, timeout);
  533. }
  534. EXPORT_SYMBOL_GPL(__mlx4_cmd);
  535. static int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
  536. {
  537. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
  538. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  539. }
  540. static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
  541. int slave, u64 slave_addr,
  542. int size, int is_read)
  543. {
  544. u64 in_param;
  545. u64 out_param;
  546. if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
  547. (slave & ~0x7f) | (size & 0xff)) {
  548. mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
  549. slave_addr, master_addr, slave, size);
  550. return -EINVAL;
  551. }
  552. if (is_read) {
  553. in_param = (u64) slave | slave_addr;
  554. out_param = (u64) dev->caps.function | master_addr;
  555. } else {
  556. in_param = (u64) dev->caps.function | master_addr;
  557. out_param = (u64) slave | slave_addr;
  558. }
  559. return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
  560. MLX4_CMD_ACCESS_MEM,
  561. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  562. }
  563. static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
  564. struct mlx4_cmd_mailbox *inbox,
  565. struct mlx4_cmd_mailbox *outbox)
  566. {
  567. struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
  568. struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
  569. int err;
  570. int i;
  571. if (index & 0x1f)
  572. return -EINVAL;
  573. in_mad->attr_mod = cpu_to_be32(index / 32);
  574. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  575. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  576. MLX4_CMD_NATIVE);
  577. if (err)
  578. return err;
  579. for (i = 0; i < 32; ++i)
  580. pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
  581. return err;
  582. }
  583. static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
  584. struct mlx4_cmd_mailbox *inbox,
  585. struct mlx4_cmd_mailbox *outbox)
  586. {
  587. int i;
  588. int err;
  589. for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
  590. err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
  591. if (err)
  592. return err;
  593. }
  594. return 0;
  595. }
  596. #define PORT_CAPABILITY_LOCATION_IN_SMP 20
  597. #define PORT_STATE_OFFSET 32
  598. static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
  599. {
  600. if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
  601. return IB_PORT_ACTIVE;
  602. else
  603. return IB_PORT_DOWN;
  604. }
  605. static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
  606. struct mlx4_vhcr *vhcr,
  607. struct mlx4_cmd_mailbox *inbox,
  608. struct mlx4_cmd_mailbox *outbox,
  609. struct mlx4_cmd_info *cmd)
  610. {
  611. struct ib_smp *smp = inbox->buf;
  612. u32 index;
  613. u8 port;
  614. u8 opcode_modifier;
  615. u16 *table;
  616. int err;
  617. int vidx, pidx;
  618. int network_view;
  619. struct mlx4_priv *priv = mlx4_priv(dev);
  620. struct ib_smp *outsmp = outbox->buf;
  621. __be16 *outtab = (__be16 *)(outsmp->data);
  622. __be32 slave_cap_mask;
  623. __be64 slave_node_guid;
  624. port = vhcr->in_modifier;
  625. /* network-view bit is for driver use only, and should not be passed to FW */
  626. opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
  627. network_view = !!(vhcr->op_modifier & 0x8);
  628. if (smp->base_version == 1 &&
  629. smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  630. smp->class_version == 1) {
  631. /* host view is paravirtualized */
  632. if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
  633. if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  634. index = be32_to_cpu(smp->attr_mod);
  635. if (port < 1 || port > dev->caps.num_ports)
  636. return -EINVAL;
  637. table = kcalloc(dev->caps.pkey_table_len[port], sizeof *table, GFP_KERNEL);
  638. if (!table)
  639. return -ENOMEM;
  640. /* need to get the full pkey table because the paravirtualized
  641. * pkeys may be scattered among several pkey blocks.
  642. */
  643. err = get_full_pkey_table(dev, port, table, inbox, outbox);
  644. if (!err) {
  645. for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
  646. pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
  647. outtab[vidx % 32] = cpu_to_be16(table[pidx]);
  648. }
  649. }
  650. kfree(table);
  651. return err;
  652. }
  653. if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
  654. /*get the slave specific caps:*/
  655. /*do the command */
  656. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  657. vhcr->in_modifier, opcode_modifier,
  658. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  659. /* modify the response for slaves */
  660. if (!err && slave != mlx4_master_func_num(dev)) {
  661. u8 *state = outsmp->data + PORT_STATE_OFFSET;
  662. *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
  663. slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  664. memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
  665. }
  666. return err;
  667. }
  668. if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
  669. /* compute slave's gid block */
  670. smp->attr_mod = cpu_to_be32(slave / 8);
  671. /* execute cmd */
  672. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  673. vhcr->in_modifier, opcode_modifier,
  674. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  675. if (!err) {
  676. /* if needed, move slave gid to index 0 */
  677. if (slave % 8)
  678. memcpy(outsmp->data,
  679. outsmp->data + (slave % 8) * 8, 8);
  680. /* delete all other gids */
  681. memset(outsmp->data + 8, 0, 56);
  682. }
  683. return err;
  684. }
  685. if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
  686. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  687. vhcr->in_modifier, opcode_modifier,
  688. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  689. if (!err) {
  690. slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
  691. memcpy(outsmp->data + 12, &slave_node_guid, 8);
  692. }
  693. return err;
  694. }
  695. }
  696. }
  697. /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
  698. * These are the MADs used by ib verbs (such as ib_query_gids).
  699. */
  700. if (slave != mlx4_master_func_num(dev) &&
  701. !mlx4_vf_smi_enabled(dev, slave, port)) {
  702. if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  703. smp->method == IB_MGMT_METHOD_GET) || network_view) {
  704. 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",
  705. slave, smp->method, smp->mgmt_class,
  706. network_view ? "Network" : "Host",
  707. be16_to_cpu(smp->attr_id));
  708. return -EPERM;
  709. }
  710. }
  711. return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  712. vhcr->in_modifier, opcode_modifier,
  713. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  714. }
  715. static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
  716. struct mlx4_vhcr *vhcr,
  717. struct mlx4_cmd_mailbox *inbox,
  718. struct mlx4_cmd_mailbox *outbox,
  719. struct mlx4_cmd_info *cmd)
  720. {
  721. return -EPERM;
  722. }
  723. int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
  724. struct mlx4_vhcr *vhcr,
  725. struct mlx4_cmd_mailbox *inbox,
  726. struct mlx4_cmd_mailbox *outbox,
  727. struct mlx4_cmd_info *cmd)
  728. {
  729. u64 in_param;
  730. u64 out_param;
  731. int err;
  732. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  733. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  734. if (cmd->encode_slave_id) {
  735. in_param &= 0xffffffffffffff00ll;
  736. in_param |= slave;
  737. }
  738. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  739. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  740. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  741. if (cmd->out_is_imm)
  742. vhcr->out_param = out_param;
  743. return err;
  744. }
  745. static struct mlx4_cmd_info cmd_info[] = {
  746. {
  747. .opcode = MLX4_CMD_QUERY_FW,
  748. .has_inbox = false,
  749. .has_outbox = true,
  750. .out_is_imm = false,
  751. .encode_slave_id = false,
  752. .verify = NULL,
  753. .wrapper = mlx4_QUERY_FW_wrapper
  754. },
  755. {
  756. .opcode = MLX4_CMD_QUERY_HCA,
  757. .has_inbox = false,
  758. .has_outbox = true,
  759. .out_is_imm = false,
  760. .encode_slave_id = false,
  761. .verify = NULL,
  762. .wrapper = NULL
  763. },
  764. {
  765. .opcode = MLX4_CMD_QUERY_DEV_CAP,
  766. .has_inbox = false,
  767. .has_outbox = true,
  768. .out_is_imm = false,
  769. .encode_slave_id = false,
  770. .verify = NULL,
  771. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  772. },
  773. {
  774. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  775. .has_inbox = false,
  776. .has_outbox = true,
  777. .out_is_imm = false,
  778. .encode_slave_id = false,
  779. .verify = NULL,
  780. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  781. },
  782. {
  783. .opcode = MLX4_CMD_QUERY_ADAPTER,
  784. .has_inbox = false,
  785. .has_outbox = true,
  786. .out_is_imm = false,
  787. .encode_slave_id = false,
  788. .verify = NULL,
  789. .wrapper = NULL
  790. },
  791. {
  792. .opcode = MLX4_CMD_INIT_PORT,
  793. .has_inbox = false,
  794. .has_outbox = false,
  795. .out_is_imm = false,
  796. .encode_slave_id = false,
  797. .verify = NULL,
  798. .wrapper = mlx4_INIT_PORT_wrapper
  799. },
  800. {
  801. .opcode = MLX4_CMD_CLOSE_PORT,
  802. .has_inbox = false,
  803. .has_outbox = false,
  804. .out_is_imm = false,
  805. .encode_slave_id = false,
  806. .verify = NULL,
  807. .wrapper = mlx4_CLOSE_PORT_wrapper
  808. },
  809. {
  810. .opcode = MLX4_CMD_QUERY_PORT,
  811. .has_inbox = false,
  812. .has_outbox = true,
  813. .out_is_imm = false,
  814. .encode_slave_id = false,
  815. .verify = NULL,
  816. .wrapper = mlx4_QUERY_PORT_wrapper
  817. },
  818. {
  819. .opcode = MLX4_CMD_SET_PORT,
  820. .has_inbox = true,
  821. .has_outbox = false,
  822. .out_is_imm = false,
  823. .encode_slave_id = false,
  824. .verify = NULL,
  825. .wrapper = mlx4_SET_PORT_wrapper
  826. },
  827. {
  828. .opcode = MLX4_CMD_MAP_EQ,
  829. .has_inbox = false,
  830. .has_outbox = false,
  831. .out_is_imm = false,
  832. .encode_slave_id = false,
  833. .verify = NULL,
  834. .wrapper = mlx4_MAP_EQ_wrapper
  835. },
  836. {
  837. .opcode = MLX4_CMD_SW2HW_EQ,
  838. .has_inbox = true,
  839. .has_outbox = false,
  840. .out_is_imm = false,
  841. .encode_slave_id = true,
  842. .verify = NULL,
  843. .wrapper = mlx4_SW2HW_EQ_wrapper
  844. },
  845. {
  846. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  847. .has_inbox = false,
  848. .has_outbox = false,
  849. .out_is_imm = false,
  850. .encode_slave_id = false,
  851. .verify = NULL,
  852. .wrapper = NULL
  853. },
  854. {
  855. .opcode = MLX4_CMD_NOP,
  856. .has_inbox = false,
  857. .has_outbox = false,
  858. .out_is_imm = false,
  859. .encode_slave_id = false,
  860. .verify = NULL,
  861. .wrapper = NULL
  862. },
  863. {
  864. .opcode = MLX4_CMD_CONFIG_DEV,
  865. .has_inbox = false,
  866. .has_outbox = false,
  867. .out_is_imm = false,
  868. .encode_slave_id = false,
  869. .verify = NULL,
  870. .wrapper = mlx4_CMD_EPERM_wrapper
  871. },
  872. {
  873. .opcode = MLX4_CMD_ALLOC_RES,
  874. .has_inbox = false,
  875. .has_outbox = false,
  876. .out_is_imm = true,
  877. .encode_slave_id = false,
  878. .verify = NULL,
  879. .wrapper = mlx4_ALLOC_RES_wrapper
  880. },
  881. {
  882. .opcode = MLX4_CMD_FREE_RES,
  883. .has_inbox = false,
  884. .has_outbox = false,
  885. .out_is_imm = false,
  886. .encode_slave_id = false,
  887. .verify = NULL,
  888. .wrapper = mlx4_FREE_RES_wrapper
  889. },
  890. {
  891. .opcode = MLX4_CMD_SW2HW_MPT,
  892. .has_inbox = true,
  893. .has_outbox = false,
  894. .out_is_imm = false,
  895. .encode_slave_id = true,
  896. .verify = NULL,
  897. .wrapper = mlx4_SW2HW_MPT_wrapper
  898. },
  899. {
  900. .opcode = MLX4_CMD_QUERY_MPT,
  901. .has_inbox = false,
  902. .has_outbox = true,
  903. .out_is_imm = false,
  904. .encode_slave_id = false,
  905. .verify = NULL,
  906. .wrapper = mlx4_QUERY_MPT_wrapper
  907. },
  908. {
  909. .opcode = MLX4_CMD_HW2SW_MPT,
  910. .has_inbox = false,
  911. .has_outbox = false,
  912. .out_is_imm = false,
  913. .encode_slave_id = false,
  914. .verify = NULL,
  915. .wrapper = mlx4_HW2SW_MPT_wrapper
  916. },
  917. {
  918. .opcode = MLX4_CMD_READ_MTT,
  919. .has_inbox = false,
  920. .has_outbox = true,
  921. .out_is_imm = false,
  922. .encode_slave_id = false,
  923. .verify = NULL,
  924. .wrapper = NULL
  925. },
  926. {
  927. .opcode = MLX4_CMD_WRITE_MTT,
  928. .has_inbox = true,
  929. .has_outbox = false,
  930. .out_is_imm = false,
  931. .encode_slave_id = false,
  932. .verify = NULL,
  933. .wrapper = mlx4_WRITE_MTT_wrapper
  934. },
  935. {
  936. .opcode = MLX4_CMD_SYNC_TPT,
  937. .has_inbox = true,
  938. .has_outbox = false,
  939. .out_is_imm = false,
  940. .encode_slave_id = false,
  941. .verify = NULL,
  942. .wrapper = NULL
  943. },
  944. {
  945. .opcode = MLX4_CMD_HW2SW_EQ,
  946. .has_inbox = false,
  947. .has_outbox = true,
  948. .out_is_imm = false,
  949. .encode_slave_id = true,
  950. .verify = NULL,
  951. .wrapper = mlx4_HW2SW_EQ_wrapper
  952. },
  953. {
  954. .opcode = MLX4_CMD_QUERY_EQ,
  955. .has_inbox = false,
  956. .has_outbox = true,
  957. .out_is_imm = false,
  958. .encode_slave_id = true,
  959. .verify = NULL,
  960. .wrapper = mlx4_QUERY_EQ_wrapper
  961. },
  962. {
  963. .opcode = MLX4_CMD_SW2HW_CQ,
  964. .has_inbox = true,
  965. .has_outbox = false,
  966. .out_is_imm = false,
  967. .encode_slave_id = true,
  968. .verify = NULL,
  969. .wrapper = mlx4_SW2HW_CQ_wrapper
  970. },
  971. {
  972. .opcode = MLX4_CMD_HW2SW_CQ,
  973. .has_inbox = false,
  974. .has_outbox = false,
  975. .out_is_imm = false,
  976. .encode_slave_id = false,
  977. .verify = NULL,
  978. .wrapper = mlx4_HW2SW_CQ_wrapper
  979. },
  980. {
  981. .opcode = MLX4_CMD_QUERY_CQ,
  982. .has_inbox = false,
  983. .has_outbox = true,
  984. .out_is_imm = false,
  985. .encode_slave_id = false,
  986. .verify = NULL,
  987. .wrapper = mlx4_QUERY_CQ_wrapper
  988. },
  989. {
  990. .opcode = MLX4_CMD_MODIFY_CQ,
  991. .has_inbox = true,
  992. .has_outbox = false,
  993. .out_is_imm = true,
  994. .encode_slave_id = false,
  995. .verify = NULL,
  996. .wrapper = mlx4_MODIFY_CQ_wrapper
  997. },
  998. {
  999. .opcode = MLX4_CMD_SW2HW_SRQ,
  1000. .has_inbox = true,
  1001. .has_outbox = false,
  1002. .out_is_imm = false,
  1003. .encode_slave_id = true,
  1004. .verify = NULL,
  1005. .wrapper = mlx4_SW2HW_SRQ_wrapper
  1006. },
  1007. {
  1008. .opcode = MLX4_CMD_HW2SW_SRQ,
  1009. .has_inbox = false,
  1010. .has_outbox = false,
  1011. .out_is_imm = false,
  1012. .encode_slave_id = false,
  1013. .verify = NULL,
  1014. .wrapper = mlx4_HW2SW_SRQ_wrapper
  1015. },
  1016. {
  1017. .opcode = MLX4_CMD_QUERY_SRQ,
  1018. .has_inbox = false,
  1019. .has_outbox = true,
  1020. .out_is_imm = false,
  1021. .encode_slave_id = false,
  1022. .verify = NULL,
  1023. .wrapper = mlx4_QUERY_SRQ_wrapper
  1024. },
  1025. {
  1026. .opcode = MLX4_CMD_ARM_SRQ,
  1027. .has_inbox = false,
  1028. .has_outbox = false,
  1029. .out_is_imm = false,
  1030. .encode_slave_id = false,
  1031. .verify = NULL,
  1032. .wrapper = mlx4_ARM_SRQ_wrapper
  1033. },
  1034. {
  1035. .opcode = MLX4_CMD_RST2INIT_QP,
  1036. .has_inbox = true,
  1037. .has_outbox = false,
  1038. .out_is_imm = false,
  1039. .encode_slave_id = true,
  1040. .verify = NULL,
  1041. .wrapper = mlx4_RST2INIT_QP_wrapper
  1042. },
  1043. {
  1044. .opcode = MLX4_CMD_INIT2INIT_QP,
  1045. .has_inbox = true,
  1046. .has_outbox = false,
  1047. .out_is_imm = false,
  1048. .encode_slave_id = false,
  1049. .verify = NULL,
  1050. .wrapper = mlx4_INIT2INIT_QP_wrapper
  1051. },
  1052. {
  1053. .opcode = MLX4_CMD_INIT2RTR_QP,
  1054. .has_inbox = true,
  1055. .has_outbox = false,
  1056. .out_is_imm = false,
  1057. .encode_slave_id = false,
  1058. .verify = NULL,
  1059. .wrapper = mlx4_INIT2RTR_QP_wrapper
  1060. },
  1061. {
  1062. .opcode = MLX4_CMD_RTR2RTS_QP,
  1063. .has_inbox = true,
  1064. .has_outbox = false,
  1065. .out_is_imm = false,
  1066. .encode_slave_id = false,
  1067. .verify = NULL,
  1068. .wrapper = mlx4_RTR2RTS_QP_wrapper
  1069. },
  1070. {
  1071. .opcode = MLX4_CMD_RTS2RTS_QP,
  1072. .has_inbox = true,
  1073. .has_outbox = false,
  1074. .out_is_imm = false,
  1075. .encode_slave_id = false,
  1076. .verify = NULL,
  1077. .wrapper = mlx4_RTS2RTS_QP_wrapper
  1078. },
  1079. {
  1080. .opcode = MLX4_CMD_SQERR2RTS_QP,
  1081. .has_inbox = true,
  1082. .has_outbox = false,
  1083. .out_is_imm = false,
  1084. .encode_slave_id = false,
  1085. .verify = NULL,
  1086. .wrapper = mlx4_SQERR2RTS_QP_wrapper
  1087. },
  1088. {
  1089. .opcode = MLX4_CMD_2ERR_QP,
  1090. .has_inbox = false,
  1091. .has_outbox = false,
  1092. .out_is_imm = false,
  1093. .encode_slave_id = false,
  1094. .verify = NULL,
  1095. .wrapper = mlx4_GEN_QP_wrapper
  1096. },
  1097. {
  1098. .opcode = MLX4_CMD_RTS2SQD_QP,
  1099. .has_inbox = false,
  1100. .has_outbox = false,
  1101. .out_is_imm = false,
  1102. .encode_slave_id = false,
  1103. .verify = NULL,
  1104. .wrapper = mlx4_GEN_QP_wrapper
  1105. },
  1106. {
  1107. .opcode = MLX4_CMD_SQD2SQD_QP,
  1108. .has_inbox = true,
  1109. .has_outbox = false,
  1110. .out_is_imm = false,
  1111. .encode_slave_id = false,
  1112. .verify = NULL,
  1113. .wrapper = mlx4_SQD2SQD_QP_wrapper
  1114. },
  1115. {
  1116. .opcode = MLX4_CMD_SQD2RTS_QP,
  1117. .has_inbox = true,
  1118. .has_outbox = false,
  1119. .out_is_imm = false,
  1120. .encode_slave_id = false,
  1121. .verify = NULL,
  1122. .wrapper = mlx4_SQD2RTS_QP_wrapper
  1123. },
  1124. {
  1125. .opcode = MLX4_CMD_2RST_QP,
  1126. .has_inbox = false,
  1127. .has_outbox = false,
  1128. .out_is_imm = false,
  1129. .encode_slave_id = false,
  1130. .verify = NULL,
  1131. .wrapper = mlx4_2RST_QP_wrapper
  1132. },
  1133. {
  1134. .opcode = MLX4_CMD_QUERY_QP,
  1135. .has_inbox = false,
  1136. .has_outbox = true,
  1137. .out_is_imm = false,
  1138. .encode_slave_id = false,
  1139. .verify = NULL,
  1140. .wrapper = mlx4_GEN_QP_wrapper
  1141. },
  1142. {
  1143. .opcode = MLX4_CMD_SUSPEND_QP,
  1144. .has_inbox = false,
  1145. .has_outbox = false,
  1146. .out_is_imm = false,
  1147. .encode_slave_id = false,
  1148. .verify = NULL,
  1149. .wrapper = mlx4_GEN_QP_wrapper
  1150. },
  1151. {
  1152. .opcode = MLX4_CMD_UNSUSPEND_QP,
  1153. .has_inbox = false,
  1154. .has_outbox = false,
  1155. .out_is_imm = false,
  1156. .encode_slave_id = false,
  1157. .verify = NULL,
  1158. .wrapper = mlx4_GEN_QP_wrapper
  1159. },
  1160. {
  1161. .opcode = MLX4_CMD_UPDATE_QP,
  1162. .has_inbox = true,
  1163. .has_outbox = false,
  1164. .out_is_imm = false,
  1165. .encode_slave_id = false,
  1166. .verify = NULL,
  1167. .wrapper = mlx4_UPDATE_QP_wrapper
  1168. },
  1169. {
  1170. .opcode = MLX4_CMD_GET_OP_REQ,
  1171. .has_inbox = false,
  1172. .has_outbox = false,
  1173. .out_is_imm = false,
  1174. .encode_slave_id = false,
  1175. .verify = NULL,
  1176. .wrapper = mlx4_CMD_EPERM_wrapper,
  1177. },
  1178. {
  1179. .opcode = MLX4_CMD_CONF_SPECIAL_QP,
  1180. .has_inbox = false,
  1181. .has_outbox = false,
  1182. .out_is_imm = false,
  1183. .encode_slave_id = false,
  1184. .verify = NULL, /* XXX verify: only demux can do this */
  1185. .wrapper = NULL
  1186. },
  1187. {
  1188. .opcode = MLX4_CMD_MAD_IFC,
  1189. .has_inbox = true,
  1190. .has_outbox = true,
  1191. .out_is_imm = false,
  1192. .encode_slave_id = false,
  1193. .verify = NULL,
  1194. .wrapper = mlx4_MAD_IFC_wrapper
  1195. },
  1196. {
  1197. .opcode = MLX4_CMD_MAD_DEMUX,
  1198. .has_inbox = false,
  1199. .has_outbox = false,
  1200. .out_is_imm = false,
  1201. .encode_slave_id = false,
  1202. .verify = NULL,
  1203. .wrapper = mlx4_CMD_EPERM_wrapper
  1204. },
  1205. {
  1206. .opcode = MLX4_CMD_QUERY_IF_STAT,
  1207. .has_inbox = false,
  1208. .has_outbox = true,
  1209. .out_is_imm = false,
  1210. .encode_slave_id = false,
  1211. .verify = NULL,
  1212. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  1213. },
  1214. /* Native multicast commands are not available for guests */
  1215. {
  1216. .opcode = MLX4_CMD_QP_ATTACH,
  1217. .has_inbox = true,
  1218. .has_outbox = false,
  1219. .out_is_imm = false,
  1220. .encode_slave_id = false,
  1221. .verify = NULL,
  1222. .wrapper = mlx4_QP_ATTACH_wrapper
  1223. },
  1224. {
  1225. .opcode = MLX4_CMD_PROMISC,
  1226. .has_inbox = false,
  1227. .has_outbox = false,
  1228. .out_is_imm = false,
  1229. .encode_slave_id = false,
  1230. .verify = NULL,
  1231. .wrapper = mlx4_PROMISC_wrapper
  1232. },
  1233. /* Ethernet specific commands */
  1234. {
  1235. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  1236. .has_inbox = true,
  1237. .has_outbox = false,
  1238. .out_is_imm = false,
  1239. .encode_slave_id = false,
  1240. .verify = NULL,
  1241. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  1242. },
  1243. {
  1244. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  1245. .has_inbox = false,
  1246. .has_outbox = false,
  1247. .out_is_imm = false,
  1248. .encode_slave_id = false,
  1249. .verify = NULL,
  1250. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  1251. },
  1252. {
  1253. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  1254. .has_inbox = false,
  1255. .has_outbox = true,
  1256. .out_is_imm = false,
  1257. .encode_slave_id = false,
  1258. .verify = NULL,
  1259. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  1260. },
  1261. {
  1262. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  1263. .has_inbox = false,
  1264. .has_outbox = false,
  1265. .out_is_imm = false,
  1266. .encode_slave_id = false,
  1267. .verify = NULL,
  1268. .wrapper = NULL
  1269. },
  1270. /* flow steering commands */
  1271. {
  1272. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1273. .has_inbox = true,
  1274. .has_outbox = false,
  1275. .out_is_imm = true,
  1276. .encode_slave_id = false,
  1277. .verify = NULL,
  1278. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1279. },
  1280. {
  1281. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1282. .has_inbox = false,
  1283. .has_outbox = false,
  1284. .out_is_imm = false,
  1285. .encode_slave_id = false,
  1286. .verify = NULL,
  1287. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1288. },
  1289. {
  1290. .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
  1291. .has_inbox = false,
  1292. .has_outbox = false,
  1293. .out_is_imm = false,
  1294. .encode_slave_id = false,
  1295. .verify = NULL,
  1296. .wrapper = mlx4_CMD_EPERM_wrapper
  1297. },
  1298. };
  1299. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1300. struct mlx4_vhcr_cmd *in_vhcr)
  1301. {
  1302. struct mlx4_priv *priv = mlx4_priv(dev);
  1303. struct mlx4_cmd_info *cmd = NULL;
  1304. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1305. struct mlx4_vhcr *vhcr;
  1306. struct mlx4_cmd_mailbox *inbox = NULL;
  1307. struct mlx4_cmd_mailbox *outbox = NULL;
  1308. u64 in_param;
  1309. u64 out_param;
  1310. int ret = 0;
  1311. int i;
  1312. int err = 0;
  1313. /* Create sw representation of Virtual HCR */
  1314. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1315. if (!vhcr)
  1316. return -ENOMEM;
  1317. /* DMA in the vHCR */
  1318. if (!in_vhcr) {
  1319. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1320. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1321. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1322. MLX4_ACCESS_MEM_ALIGN), 1);
  1323. if (ret) {
  1324. mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
  1325. __func__, ret);
  1326. kfree(vhcr);
  1327. return ret;
  1328. }
  1329. }
  1330. /* Fill SW VHCR fields */
  1331. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1332. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1333. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1334. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1335. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1336. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1337. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1338. /* Lookup command */
  1339. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1340. if (vhcr->op == cmd_info[i].opcode) {
  1341. cmd = &cmd_info[i];
  1342. break;
  1343. }
  1344. }
  1345. if (!cmd) {
  1346. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1347. vhcr->op, slave);
  1348. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1349. goto out_status;
  1350. }
  1351. /* Read inbox */
  1352. if (cmd->has_inbox) {
  1353. vhcr->in_param &= INBOX_MASK;
  1354. inbox = mlx4_alloc_cmd_mailbox(dev);
  1355. if (IS_ERR(inbox)) {
  1356. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1357. inbox = NULL;
  1358. goto out_status;
  1359. }
  1360. if (mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1361. vhcr->in_param,
  1362. MLX4_MAILBOX_SIZE, 1)) {
  1363. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1364. __func__, cmd->opcode);
  1365. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1366. goto out_status;
  1367. }
  1368. }
  1369. /* Apply permission and bound checks if applicable */
  1370. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1371. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
  1372. vhcr->op, slave, vhcr->in_modifier);
  1373. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1374. goto out_status;
  1375. }
  1376. /* Allocate outbox */
  1377. if (cmd->has_outbox) {
  1378. outbox = mlx4_alloc_cmd_mailbox(dev);
  1379. if (IS_ERR(outbox)) {
  1380. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1381. outbox = NULL;
  1382. goto out_status;
  1383. }
  1384. }
  1385. /* Execute the command! */
  1386. if (cmd->wrapper) {
  1387. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1388. cmd);
  1389. if (cmd->out_is_imm)
  1390. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1391. } else {
  1392. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1393. vhcr->in_param;
  1394. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1395. vhcr->out_param;
  1396. err = __mlx4_cmd(dev, in_param, &out_param,
  1397. cmd->out_is_imm, vhcr->in_modifier,
  1398. vhcr->op_modifier, vhcr->op,
  1399. MLX4_CMD_TIME_CLASS_A,
  1400. MLX4_CMD_NATIVE);
  1401. if (cmd->out_is_imm) {
  1402. vhcr->out_param = out_param;
  1403. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1404. }
  1405. }
  1406. if (err) {
  1407. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
  1408. vhcr->op, slave, vhcr->errno, err);
  1409. vhcr_cmd->status = mlx4_errno_to_status(err);
  1410. goto out_status;
  1411. }
  1412. /* Write outbox if command completed successfully */
  1413. if (cmd->has_outbox && !vhcr_cmd->status) {
  1414. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1415. vhcr->out_param,
  1416. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1417. if (ret) {
  1418. /* If we failed to write back the outbox after the
  1419. *command was successfully executed, we must fail this
  1420. * slave, as it is now in undefined state */
  1421. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1422. goto out;
  1423. }
  1424. }
  1425. out_status:
  1426. /* DMA back vhcr result */
  1427. if (!in_vhcr) {
  1428. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1429. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1430. ALIGN(sizeof(struct mlx4_vhcr),
  1431. MLX4_ACCESS_MEM_ALIGN),
  1432. MLX4_CMD_WRAPPED);
  1433. if (ret)
  1434. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1435. __func__);
  1436. else if (vhcr->e_bit &&
  1437. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1438. mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
  1439. slave);
  1440. }
  1441. out:
  1442. kfree(vhcr);
  1443. mlx4_free_cmd_mailbox(dev, inbox);
  1444. mlx4_free_cmd_mailbox(dev, outbox);
  1445. return ret;
  1446. }
  1447. static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
  1448. int slave, int port)
  1449. {
  1450. struct mlx4_vport_oper_state *vp_oper;
  1451. struct mlx4_vport_state *vp_admin;
  1452. struct mlx4_vf_immed_vlan_work *work;
  1453. struct mlx4_dev *dev = &(priv->dev);
  1454. int err;
  1455. int admin_vlan_ix = NO_INDX;
  1456. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1457. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1458. if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
  1459. vp_oper->state.default_qos == vp_admin->default_qos &&
  1460. vp_oper->state.link_state == vp_admin->link_state)
  1461. return 0;
  1462. if (!(priv->mfunc.master.slave_state[slave].active &&
  1463. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
  1464. /* even if the UPDATE_QP command isn't supported, we still want
  1465. * to set this VF link according to the admin directive
  1466. */
  1467. vp_oper->state.link_state = vp_admin->link_state;
  1468. return -1;
  1469. }
  1470. mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
  1471. slave, port);
  1472. mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
  1473. vp_admin->default_vlan, vp_admin->default_qos,
  1474. vp_admin->link_state);
  1475. work = kzalloc(sizeof(*work), GFP_KERNEL);
  1476. if (!work)
  1477. return -ENOMEM;
  1478. if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
  1479. if (MLX4_VGT != vp_admin->default_vlan) {
  1480. err = __mlx4_register_vlan(&priv->dev, port,
  1481. vp_admin->default_vlan,
  1482. &admin_vlan_ix);
  1483. if (err) {
  1484. kfree(work);
  1485. mlx4_warn(&priv->dev,
  1486. "No vlan resources slave %d, port %d\n",
  1487. slave, port);
  1488. return err;
  1489. }
  1490. } else {
  1491. admin_vlan_ix = NO_INDX;
  1492. }
  1493. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
  1494. mlx4_dbg(&priv->dev,
  1495. "alloc vlan %d idx %d slave %d port %d\n",
  1496. (int)(vp_admin->default_vlan),
  1497. admin_vlan_ix, slave, port);
  1498. }
  1499. /* save original vlan ix and vlan id */
  1500. work->orig_vlan_id = vp_oper->state.default_vlan;
  1501. work->orig_vlan_ix = vp_oper->vlan_idx;
  1502. /* handle new qos */
  1503. if (vp_oper->state.default_qos != vp_admin->default_qos)
  1504. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
  1505. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
  1506. vp_oper->vlan_idx = admin_vlan_ix;
  1507. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1508. vp_oper->state.default_qos = vp_admin->default_qos;
  1509. vp_oper->state.link_state = vp_admin->link_state;
  1510. if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
  1511. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
  1512. /* iterate over QPs owned by this slave, using UPDATE_QP */
  1513. work->port = port;
  1514. work->slave = slave;
  1515. work->qos = vp_oper->state.default_qos;
  1516. work->vlan_id = vp_oper->state.default_vlan;
  1517. work->vlan_ix = vp_oper->vlan_idx;
  1518. work->priv = priv;
  1519. INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
  1520. queue_work(priv->mfunc.master.comm_wq, &work->work);
  1521. return 0;
  1522. }
  1523. static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
  1524. {
  1525. int port, err;
  1526. struct mlx4_vport_state *vp_admin;
  1527. struct mlx4_vport_oper_state *vp_oper;
  1528. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1529. &priv->dev, slave);
  1530. int min_port = find_first_bit(actv_ports.ports,
  1531. priv->dev.caps.num_ports) + 1;
  1532. int max_port = min_port - 1 +
  1533. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1534. for (port = min_port; port <= max_port; port++) {
  1535. if (!test_bit(port - 1, actv_ports.ports))
  1536. continue;
  1537. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1538. priv->mfunc.master.vf_admin[slave].enable_smi[port];
  1539. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1540. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1541. vp_oper->state = *vp_admin;
  1542. if (MLX4_VGT != vp_admin->default_vlan) {
  1543. err = __mlx4_register_vlan(&priv->dev, port,
  1544. vp_admin->default_vlan, &(vp_oper->vlan_idx));
  1545. if (err) {
  1546. vp_oper->vlan_idx = NO_INDX;
  1547. mlx4_warn(&priv->dev,
  1548. "No vlan resorces slave %d, port %d\n",
  1549. slave, port);
  1550. return err;
  1551. }
  1552. mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
  1553. (int)(vp_oper->state.default_vlan),
  1554. vp_oper->vlan_idx, slave, port);
  1555. }
  1556. if (vp_admin->spoofchk) {
  1557. vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
  1558. port,
  1559. vp_admin->mac);
  1560. if (0 > vp_oper->mac_idx) {
  1561. err = vp_oper->mac_idx;
  1562. vp_oper->mac_idx = NO_INDX;
  1563. mlx4_warn(&priv->dev,
  1564. "No mac resorces slave %d, port %d\n",
  1565. slave, port);
  1566. return err;
  1567. }
  1568. mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
  1569. vp_oper->state.mac, vp_oper->mac_idx, slave, port);
  1570. }
  1571. }
  1572. return 0;
  1573. }
  1574. static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
  1575. {
  1576. int port;
  1577. struct mlx4_vport_oper_state *vp_oper;
  1578. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1579. &priv->dev, slave);
  1580. int min_port = find_first_bit(actv_ports.ports,
  1581. priv->dev.caps.num_ports) + 1;
  1582. int max_port = min_port - 1 +
  1583. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1584. for (port = min_port; port <= max_port; port++) {
  1585. if (!test_bit(port - 1, actv_ports.ports))
  1586. continue;
  1587. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1588. MLX4_VF_SMI_DISABLED;
  1589. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1590. if (NO_INDX != vp_oper->vlan_idx) {
  1591. __mlx4_unregister_vlan(&priv->dev,
  1592. port, vp_oper->state.default_vlan);
  1593. vp_oper->vlan_idx = NO_INDX;
  1594. }
  1595. if (NO_INDX != vp_oper->mac_idx) {
  1596. __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
  1597. vp_oper->mac_idx = NO_INDX;
  1598. }
  1599. }
  1600. return;
  1601. }
  1602. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1603. u16 param, u8 toggle)
  1604. {
  1605. struct mlx4_priv *priv = mlx4_priv(dev);
  1606. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1607. u32 reply;
  1608. u8 is_going_down = 0;
  1609. int i;
  1610. unsigned long flags;
  1611. slave_state[slave].comm_toggle ^= 1;
  1612. reply = (u32) slave_state[slave].comm_toggle << 31;
  1613. if (toggle != slave_state[slave].comm_toggle) {
  1614. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
  1615. toggle, slave);
  1616. goto reset_slave;
  1617. }
  1618. if (cmd == MLX4_COMM_CMD_RESET) {
  1619. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1620. slave_state[slave].active = false;
  1621. slave_state[slave].old_vlan_api = false;
  1622. mlx4_master_deactivate_admin_state(priv, slave);
  1623. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1624. slave_state[slave].event_eq[i].eqn = -1;
  1625. slave_state[slave].event_eq[i].token = 0;
  1626. }
  1627. /*check if we are in the middle of FLR process,
  1628. if so return "retry" status to the slave*/
  1629. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1630. goto inform_slave_state;
  1631. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1632. /* write the version in the event field */
  1633. reply |= mlx4_comm_get_version();
  1634. goto reset_slave;
  1635. }
  1636. /*command from slave in the middle of FLR*/
  1637. if (cmd != MLX4_COMM_CMD_RESET &&
  1638. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1639. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
  1640. slave, cmd);
  1641. return;
  1642. }
  1643. switch (cmd) {
  1644. case MLX4_COMM_CMD_VHCR0:
  1645. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1646. goto reset_slave;
  1647. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1648. priv->mfunc.master.slave_state[slave].cookie = 0;
  1649. mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
  1650. break;
  1651. case MLX4_COMM_CMD_VHCR1:
  1652. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1653. goto reset_slave;
  1654. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1655. break;
  1656. case MLX4_COMM_CMD_VHCR2:
  1657. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1658. goto reset_slave;
  1659. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1660. break;
  1661. case MLX4_COMM_CMD_VHCR_EN:
  1662. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1663. goto reset_slave;
  1664. slave_state[slave].vhcr_dma |= param;
  1665. if (mlx4_master_activate_admin_state(priv, slave))
  1666. goto reset_slave;
  1667. slave_state[slave].active = true;
  1668. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1669. break;
  1670. case MLX4_COMM_CMD_VHCR_POST:
  1671. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1672. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
  1673. goto reset_slave;
  1674. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1675. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1676. mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
  1677. slave);
  1678. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1679. goto reset_slave;
  1680. }
  1681. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1682. break;
  1683. default:
  1684. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1685. goto reset_slave;
  1686. }
  1687. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1688. if (!slave_state[slave].is_slave_going_down)
  1689. slave_state[slave].last_cmd = cmd;
  1690. else
  1691. is_going_down = 1;
  1692. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1693. if (is_going_down) {
  1694. mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
  1695. cmd, slave);
  1696. return;
  1697. }
  1698. __raw_writel((__force u32) cpu_to_be32(reply),
  1699. &priv->mfunc.comm[slave].slave_read);
  1700. mmiowb();
  1701. return;
  1702. reset_slave:
  1703. /* cleanup any slave resources */
  1704. mlx4_delete_all_resources_for_slave(dev, slave);
  1705. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1706. if (!slave_state[slave].is_slave_going_down)
  1707. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  1708. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1709. /*with slave in the middle of flr, no need to clean resources again.*/
  1710. inform_slave_state:
  1711. memset(&slave_state[slave].event_eq, 0,
  1712. sizeof(struct mlx4_slave_event_eq_info));
  1713. __raw_writel((__force u32) cpu_to_be32(reply),
  1714. &priv->mfunc.comm[slave].slave_read);
  1715. wmb();
  1716. }
  1717. /* master command processing */
  1718. void mlx4_master_comm_channel(struct work_struct *work)
  1719. {
  1720. struct mlx4_mfunc_master_ctx *master =
  1721. container_of(work,
  1722. struct mlx4_mfunc_master_ctx,
  1723. comm_work);
  1724. struct mlx4_mfunc *mfunc =
  1725. container_of(master, struct mlx4_mfunc, master);
  1726. struct mlx4_priv *priv =
  1727. container_of(mfunc, struct mlx4_priv, mfunc);
  1728. struct mlx4_dev *dev = &priv->dev;
  1729. __be32 *bit_vec;
  1730. u32 comm_cmd;
  1731. u32 vec;
  1732. int i, j, slave;
  1733. int toggle;
  1734. int served = 0;
  1735. int reported = 0;
  1736. u32 slt;
  1737. bit_vec = master->comm_arm_bit_vector;
  1738. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  1739. vec = be32_to_cpu(bit_vec[i]);
  1740. for (j = 0; j < 32; j++) {
  1741. if (!(vec & (1 << j)))
  1742. continue;
  1743. ++reported;
  1744. slave = (i * 32) + j;
  1745. comm_cmd = swab32(readl(
  1746. &mfunc->comm[slave].slave_write));
  1747. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  1748. >> 31;
  1749. toggle = comm_cmd >> 31;
  1750. if (toggle != slt) {
  1751. if (master->slave_state[slave].comm_toggle
  1752. != slt) {
  1753. pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
  1754. slave, slt,
  1755. master->slave_state[slave].comm_toggle);
  1756. master->slave_state[slave].comm_toggle =
  1757. slt;
  1758. }
  1759. mlx4_master_do_cmd(dev, slave,
  1760. comm_cmd >> 16 & 0xff,
  1761. comm_cmd & 0xffff, toggle);
  1762. ++served;
  1763. }
  1764. }
  1765. }
  1766. if (reported && reported != served)
  1767. mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
  1768. reported, served);
  1769. if (mlx4_ARM_COMM_CHANNEL(dev))
  1770. mlx4_warn(dev, "Failed to arm comm channel events\n");
  1771. }
  1772. static int sync_toggles(struct mlx4_dev *dev)
  1773. {
  1774. struct mlx4_priv *priv = mlx4_priv(dev);
  1775. int wr_toggle;
  1776. int rd_toggle;
  1777. unsigned long end;
  1778. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
  1779. end = jiffies + msecs_to_jiffies(5000);
  1780. while (time_before(jiffies, end)) {
  1781. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
  1782. if (rd_toggle == wr_toggle) {
  1783. priv->cmd.comm_toggle = rd_toggle;
  1784. return 0;
  1785. }
  1786. cond_resched();
  1787. }
  1788. /*
  1789. * we could reach here if for example the previous VM using this
  1790. * function misbehaved and left the channel with unsynced state. We
  1791. * should fix this here and give this VM a chance to use a properly
  1792. * synced channel
  1793. */
  1794. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  1795. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  1796. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  1797. priv->cmd.comm_toggle = 0;
  1798. return 0;
  1799. }
  1800. int mlx4_multi_func_init(struct mlx4_dev *dev)
  1801. {
  1802. struct mlx4_priv *priv = mlx4_priv(dev);
  1803. struct mlx4_slave_state *s_state;
  1804. int i, j, err, port;
  1805. if (mlx4_is_master(dev))
  1806. priv->mfunc.comm =
  1807. ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
  1808. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  1809. else
  1810. priv->mfunc.comm =
  1811. ioremap(pci_resource_start(dev->pdev, 2) +
  1812. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  1813. if (!priv->mfunc.comm) {
  1814. mlx4_err(dev, "Couldn't map communication vector\n");
  1815. goto err_vhcr;
  1816. }
  1817. if (mlx4_is_master(dev)) {
  1818. priv->mfunc.master.slave_state =
  1819. kzalloc(dev->num_slaves *
  1820. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  1821. if (!priv->mfunc.master.slave_state)
  1822. goto err_comm;
  1823. priv->mfunc.master.vf_admin =
  1824. kzalloc(dev->num_slaves *
  1825. sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
  1826. if (!priv->mfunc.master.vf_admin)
  1827. goto err_comm_admin;
  1828. priv->mfunc.master.vf_oper =
  1829. kzalloc(dev->num_slaves *
  1830. sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
  1831. if (!priv->mfunc.master.vf_oper)
  1832. goto err_comm_oper;
  1833. for (i = 0; i < dev->num_slaves; ++i) {
  1834. s_state = &priv->mfunc.master.slave_state[i];
  1835. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  1836. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  1837. s_state->event_eq[j].eqn = -1;
  1838. __raw_writel((__force u32) 0,
  1839. &priv->mfunc.comm[i].slave_write);
  1840. __raw_writel((__force u32) 0,
  1841. &priv->mfunc.comm[i].slave_read);
  1842. mmiowb();
  1843. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1844. s_state->vlan_filter[port] =
  1845. kzalloc(sizeof(struct mlx4_vlan_fltr),
  1846. GFP_KERNEL);
  1847. if (!s_state->vlan_filter[port]) {
  1848. if (--port)
  1849. kfree(s_state->vlan_filter[port]);
  1850. goto err_slaves;
  1851. }
  1852. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  1853. priv->mfunc.master.vf_admin[i].vport[port].default_vlan = MLX4_VGT;
  1854. priv->mfunc.master.vf_oper[i].vport[port].state.default_vlan = MLX4_VGT;
  1855. priv->mfunc.master.vf_oper[i].vport[port].vlan_idx = NO_INDX;
  1856. priv->mfunc.master.vf_oper[i].vport[port].mac_idx = NO_INDX;
  1857. }
  1858. spin_lock_init(&s_state->lock);
  1859. }
  1860. memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
  1861. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  1862. INIT_WORK(&priv->mfunc.master.comm_work,
  1863. mlx4_master_comm_channel);
  1864. INIT_WORK(&priv->mfunc.master.slave_event_work,
  1865. mlx4_gen_slave_eqe);
  1866. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  1867. mlx4_master_handle_slave_flr);
  1868. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  1869. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  1870. priv->mfunc.master.comm_wq =
  1871. create_singlethread_workqueue("mlx4_comm");
  1872. if (!priv->mfunc.master.comm_wq)
  1873. goto err_slaves;
  1874. if (mlx4_init_resource_tracker(dev))
  1875. goto err_thread;
  1876. err = mlx4_ARM_COMM_CHANNEL(dev);
  1877. if (err) {
  1878. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  1879. err);
  1880. goto err_resource;
  1881. }
  1882. } else {
  1883. err = sync_toggles(dev);
  1884. if (err) {
  1885. mlx4_err(dev, "Couldn't sync toggles\n");
  1886. goto err_comm;
  1887. }
  1888. }
  1889. return 0;
  1890. err_resource:
  1891. mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
  1892. err_thread:
  1893. flush_workqueue(priv->mfunc.master.comm_wq);
  1894. destroy_workqueue(priv->mfunc.master.comm_wq);
  1895. err_slaves:
  1896. while (--i) {
  1897. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1898. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1899. }
  1900. kfree(priv->mfunc.master.vf_oper);
  1901. err_comm_oper:
  1902. kfree(priv->mfunc.master.vf_admin);
  1903. err_comm_admin:
  1904. kfree(priv->mfunc.master.slave_state);
  1905. err_comm:
  1906. iounmap(priv->mfunc.comm);
  1907. err_vhcr:
  1908. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1909. priv->mfunc.vhcr,
  1910. priv->mfunc.vhcr_dma);
  1911. priv->mfunc.vhcr = NULL;
  1912. return -ENOMEM;
  1913. }
  1914. int mlx4_cmd_init(struct mlx4_dev *dev)
  1915. {
  1916. struct mlx4_priv *priv = mlx4_priv(dev);
  1917. mutex_init(&priv->cmd.hcr_mutex);
  1918. mutex_init(&priv->cmd.slave_cmd_mutex);
  1919. sema_init(&priv->cmd.poll_sem, 1);
  1920. priv->cmd.use_events = 0;
  1921. priv->cmd.toggle = 1;
  1922. priv->cmd.hcr = NULL;
  1923. priv->mfunc.vhcr = NULL;
  1924. if (!mlx4_is_slave(dev)) {
  1925. priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
  1926. MLX4_HCR_BASE, MLX4_HCR_SIZE);
  1927. if (!priv->cmd.hcr) {
  1928. mlx4_err(dev, "Couldn't map command register\n");
  1929. return -ENOMEM;
  1930. }
  1931. }
  1932. if (mlx4_is_mfunc(dev)) {
  1933. priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1934. &priv->mfunc.vhcr_dma,
  1935. GFP_KERNEL);
  1936. if (!priv->mfunc.vhcr)
  1937. goto err_hcr;
  1938. }
  1939. priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
  1940. MLX4_MAILBOX_SIZE,
  1941. MLX4_MAILBOX_SIZE, 0);
  1942. if (!priv->cmd.pool)
  1943. goto err_vhcr;
  1944. return 0;
  1945. err_vhcr:
  1946. if (mlx4_is_mfunc(dev))
  1947. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1948. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1949. priv->mfunc.vhcr = NULL;
  1950. err_hcr:
  1951. if (!mlx4_is_slave(dev))
  1952. iounmap(priv->cmd.hcr);
  1953. return -ENOMEM;
  1954. }
  1955. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  1956. {
  1957. struct mlx4_priv *priv = mlx4_priv(dev);
  1958. int i, port;
  1959. if (mlx4_is_master(dev)) {
  1960. flush_workqueue(priv->mfunc.master.comm_wq);
  1961. destroy_workqueue(priv->mfunc.master.comm_wq);
  1962. for (i = 0; i < dev->num_slaves; i++) {
  1963. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1964. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1965. }
  1966. kfree(priv->mfunc.master.slave_state);
  1967. kfree(priv->mfunc.master.vf_admin);
  1968. kfree(priv->mfunc.master.vf_oper);
  1969. }
  1970. iounmap(priv->mfunc.comm);
  1971. }
  1972. void mlx4_cmd_cleanup(struct mlx4_dev *dev)
  1973. {
  1974. struct mlx4_priv *priv = mlx4_priv(dev);
  1975. pci_pool_destroy(priv->cmd.pool);
  1976. if (!mlx4_is_slave(dev))
  1977. iounmap(priv->cmd.hcr);
  1978. if (mlx4_is_mfunc(dev))
  1979. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1980. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1981. priv->mfunc.vhcr = NULL;
  1982. }
  1983. /*
  1984. * Switch to using events to issue FW commands (can only be called
  1985. * after event queue for command events has been initialized).
  1986. */
  1987. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  1988. {
  1989. struct mlx4_priv *priv = mlx4_priv(dev);
  1990. int i;
  1991. int err = 0;
  1992. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  1993. sizeof (struct mlx4_cmd_context),
  1994. GFP_KERNEL);
  1995. if (!priv->cmd.context)
  1996. return -ENOMEM;
  1997. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  1998. priv->cmd.context[i].token = i;
  1999. priv->cmd.context[i].next = i + 1;
  2000. }
  2001. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  2002. priv->cmd.free_head = 0;
  2003. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  2004. spin_lock_init(&priv->cmd.context_lock);
  2005. for (priv->cmd.token_mask = 1;
  2006. priv->cmd.token_mask < priv->cmd.max_cmds;
  2007. priv->cmd.token_mask <<= 1)
  2008. ; /* nothing */
  2009. --priv->cmd.token_mask;
  2010. down(&priv->cmd.poll_sem);
  2011. priv->cmd.use_events = 1;
  2012. return err;
  2013. }
  2014. /*
  2015. * Switch back to polling (used when shutting down the device)
  2016. */
  2017. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  2018. {
  2019. struct mlx4_priv *priv = mlx4_priv(dev);
  2020. int i;
  2021. priv->cmd.use_events = 0;
  2022. for (i = 0; i < priv->cmd.max_cmds; ++i)
  2023. down(&priv->cmd.event_sem);
  2024. kfree(priv->cmd.context);
  2025. up(&priv->cmd.poll_sem);
  2026. }
  2027. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  2028. {
  2029. struct mlx4_cmd_mailbox *mailbox;
  2030. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  2031. if (!mailbox)
  2032. return ERR_PTR(-ENOMEM);
  2033. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  2034. &mailbox->dma);
  2035. if (!mailbox->buf) {
  2036. kfree(mailbox);
  2037. return ERR_PTR(-ENOMEM);
  2038. }
  2039. memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
  2040. return mailbox;
  2041. }
  2042. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  2043. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  2044. struct mlx4_cmd_mailbox *mailbox)
  2045. {
  2046. if (!mailbox)
  2047. return;
  2048. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  2049. kfree(mailbox);
  2050. }
  2051. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  2052. u32 mlx4_comm_get_version(void)
  2053. {
  2054. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  2055. }
  2056. static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
  2057. {
  2058. if ((vf < 0) || (vf >= dev->num_vfs)) {
  2059. mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n", vf, dev->num_vfs);
  2060. return -EINVAL;
  2061. }
  2062. return vf+1;
  2063. }
  2064. int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
  2065. {
  2066. if (slave < 1 || slave > dev->num_vfs) {
  2067. mlx4_err(dev,
  2068. "Bad slave number:%d (number of activated slaves: %lu)\n",
  2069. slave, dev->num_slaves);
  2070. return -EINVAL;
  2071. }
  2072. return slave - 1;
  2073. }
  2074. struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
  2075. {
  2076. struct mlx4_active_ports actv_ports;
  2077. int vf;
  2078. bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
  2079. if (slave == 0) {
  2080. bitmap_fill(actv_ports.ports, dev->caps.num_ports);
  2081. return actv_ports;
  2082. }
  2083. vf = mlx4_get_vf_indx(dev, slave);
  2084. if (vf < 0)
  2085. return actv_ports;
  2086. bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
  2087. min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
  2088. dev->caps.num_ports));
  2089. return actv_ports;
  2090. }
  2091. EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
  2092. int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
  2093. {
  2094. unsigned n;
  2095. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2096. unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2097. if (port <= 0 || port > m)
  2098. return -EINVAL;
  2099. n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2100. if (port <= n)
  2101. port = n + 1;
  2102. return port;
  2103. }
  2104. EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
  2105. int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
  2106. {
  2107. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2108. if (test_bit(port - 1, actv_ports.ports))
  2109. return port -
  2110. find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2111. return -1;
  2112. }
  2113. EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
  2114. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
  2115. int port)
  2116. {
  2117. unsigned i;
  2118. struct mlx4_slaves_pport slaves_pport;
  2119. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2120. if (port <= 0 || port > dev->caps.num_ports)
  2121. return slaves_pport;
  2122. for (i = 0; i < dev->num_vfs + 1; i++) {
  2123. struct mlx4_active_ports actv_ports =
  2124. mlx4_get_active_ports(dev, i);
  2125. if (test_bit(port - 1, actv_ports.ports))
  2126. set_bit(i, slaves_pport.slaves);
  2127. }
  2128. return slaves_pport;
  2129. }
  2130. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
  2131. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
  2132. struct mlx4_dev *dev,
  2133. const struct mlx4_active_ports *crit_ports)
  2134. {
  2135. unsigned i;
  2136. struct mlx4_slaves_pport slaves_pport;
  2137. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2138. for (i = 0; i < dev->num_vfs + 1; i++) {
  2139. struct mlx4_active_ports actv_ports =
  2140. mlx4_get_active_ports(dev, i);
  2141. if (bitmap_equal(crit_ports->ports, actv_ports.ports,
  2142. dev->caps.num_ports))
  2143. set_bit(i, slaves_pport.slaves);
  2144. }
  2145. return slaves_pport;
  2146. }
  2147. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
  2148. static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
  2149. {
  2150. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2151. int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
  2152. + 1;
  2153. int max_port = min_port +
  2154. bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2155. if (port < min_port)
  2156. port = min_port;
  2157. else if (port >= max_port)
  2158. port = max_port - 1;
  2159. return port;
  2160. }
  2161. int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
  2162. {
  2163. struct mlx4_priv *priv = mlx4_priv(dev);
  2164. struct mlx4_vport_state *s_info;
  2165. int slave;
  2166. if (!mlx4_is_master(dev))
  2167. return -EPROTONOSUPPORT;
  2168. slave = mlx4_get_slave_indx(dev, vf);
  2169. if (slave < 0)
  2170. return -EINVAL;
  2171. port = mlx4_slaves_closest_port(dev, slave, port);
  2172. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2173. s_info->mac = mac;
  2174. mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
  2175. vf, port, s_info->mac);
  2176. return 0;
  2177. }
  2178. EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
  2179. int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
  2180. {
  2181. struct mlx4_priv *priv = mlx4_priv(dev);
  2182. struct mlx4_vport_state *vf_admin;
  2183. int slave;
  2184. if ((!mlx4_is_master(dev)) ||
  2185. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2186. return -EPROTONOSUPPORT;
  2187. if ((vlan > 4095) || (qos > 7))
  2188. return -EINVAL;
  2189. slave = mlx4_get_slave_indx(dev, vf);
  2190. if (slave < 0)
  2191. return -EINVAL;
  2192. port = mlx4_slaves_closest_port(dev, slave, port);
  2193. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2194. if ((0 == vlan) && (0 == qos))
  2195. vf_admin->default_vlan = MLX4_VGT;
  2196. else
  2197. vf_admin->default_vlan = vlan;
  2198. vf_admin->default_qos = qos;
  2199. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2200. mlx4_info(dev,
  2201. "updating vf %d port %d config will take effect on next VF restart\n",
  2202. vf, port);
  2203. return 0;
  2204. }
  2205. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2206. /* mlx4_get_slave_default_vlan -
  2207. * return true if VST ( default vlan)
  2208. * if VST, will return vlan & qos (if not NULL)
  2209. */
  2210. bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
  2211. u16 *vlan, u8 *qos)
  2212. {
  2213. struct mlx4_vport_oper_state *vp_oper;
  2214. struct mlx4_priv *priv;
  2215. priv = mlx4_priv(dev);
  2216. port = mlx4_slaves_closest_port(dev, slave, port);
  2217. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2218. if (MLX4_VGT != vp_oper->state.default_vlan) {
  2219. if (vlan)
  2220. *vlan = vp_oper->state.default_vlan;
  2221. if (qos)
  2222. *qos = vp_oper->state.default_qos;
  2223. return true;
  2224. }
  2225. return false;
  2226. }
  2227. EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
  2228. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2229. {
  2230. struct mlx4_priv *priv = mlx4_priv(dev);
  2231. struct mlx4_vport_state *s_info;
  2232. int slave;
  2233. if ((!mlx4_is_master(dev)) ||
  2234. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  2235. return -EPROTONOSUPPORT;
  2236. slave = mlx4_get_slave_indx(dev, vf);
  2237. if (slave < 0)
  2238. return -EINVAL;
  2239. port = mlx4_slaves_closest_port(dev, slave, port);
  2240. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2241. s_info->spoofchk = setting;
  2242. return 0;
  2243. }
  2244. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2245. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2246. {
  2247. struct mlx4_priv *priv = mlx4_priv(dev);
  2248. struct mlx4_vport_state *s_info;
  2249. int slave;
  2250. if (!mlx4_is_master(dev))
  2251. return -EPROTONOSUPPORT;
  2252. slave = mlx4_get_slave_indx(dev, vf);
  2253. if (slave < 0)
  2254. return -EINVAL;
  2255. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2256. ivf->vf = vf;
  2257. /* need to convert it to a func */
  2258. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2259. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2260. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2261. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2262. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2263. ivf->mac[5] = ((s_info->mac) & 0xff);
  2264. ivf->vlan = s_info->default_vlan;
  2265. ivf->qos = s_info->default_qos;
  2266. ivf->max_tx_rate = s_info->tx_rate;
  2267. ivf->min_tx_rate = 0;
  2268. ivf->spoofchk = s_info->spoofchk;
  2269. ivf->linkstate = s_info->link_state;
  2270. return 0;
  2271. }
  2272. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2273. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2274. {
  2275. struct mlx4_priv *priv = mlx4_priv(dev);
  2276. struct mlx4_vport_state *s_info;
  2277. int slave;
  2278. u8 link_stat_event;
  2279. slave = mlx4_get_slave_indx(dev, vf);
  2280. if (slave < 0)
  2281. return -EINVAL;
  2282. port = mlx4_slaves_closest_port(dev, slave, port);
  2283. switch (link_state) {
  2284. case IFLA_VF_LINK_STATE_AUTO:
  2285. /* get current link state */
  2286. if (!priv->sense.do_sense_port[port])
  2287. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2288. else
  2289. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2290. break;
  2291. case IFLA_VF_LINK_STATE_ENABLE:
  2292. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2293. break;
  2294. case IFLA_VF_LINK_STATE_DISABLE:
  2295. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2296. break;
  2297. default:
  2298. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2299. link_state, slave, port);
  2300. return -EINVAL;
  2301. };
  2302. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2303. s_info->link_state = link_state;
  2304. /* send event */
  2305. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2306. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2307. mlx4_dbg(dev,
  2308. "updating vf %d port %d no link state HW enforcment\n",
  2309. vf, port);
  2310. return 0;
  2311. }
  2312. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
  2313. int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
  2314. {
  2315. struct mlx4_priv *priv = mlx4_priv(dev);
  2316. if (slave < 1 || slave >= dev->num_slaves ||
  2317. port < 1 || port > MLX4_MAX_PORTS)
  2318. return 0;
  2319. return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
  2320. MLX4_VF_SMI_ENABLED;
  2321. }
  2322. EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
  2323. int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
  2324. {
  2325. struct mlx4_priv *priv = mlx4_priv(dev);
  2326. if (slave == mlx4_master_func_num(dev))
  2327. return 1;
  2328. if (slave < 1 || slave >= dev->num_slaves ||
  2329. port < 1 || port > MLX4_MAX_PORTS)
  2330. return 0;
  2331. return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
  2332. MLX4_VF_SMI_ENABLED;
  2333. }
  2334. EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
  2335. int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
  2336. int enabled)
  2337. {
  2338. struct mlx4_priv *priv = mlx4_priv(dev);
  2339. if (slave == mlx4_master_func_num(dev))
  2340. return 0;
  2341. if (slave < 1 || slave >= dev->num_slaves ||
  2342. port < 1 || port > MLX4_MAX_PORTS ||
  2343. enabled < 0 || enabled > 1)
  2344. return -EINVAL;
  2345. priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
  2346. return 0;
  2347. }
  2348. EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);