cmd.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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/semaphore.h>
  41. #include <asm/io.h>
  42. #include "mlx4.h"
  43. #include "fw.h"
  44. #define CMD_POLL_TOKEN 0xffff
  45. #define INBOX_MASK 0xffffffffffffff00ULL
  46. #define CMD_CHAN_VER 1
  47. #define CMD_CHAN_IF_REV 1
  48. enum {
  49. /* command completed successfully: */
  50. CMD_STAT_OK = 0x00,
  51. /* Internal error (such as a bus error) occurred while processing command: */
  52. CMD_STAT_INTERNAL_ERR = 0x01,
  53. /* Operation/command not supported or opcode modifier not supported: */
  54. CMD_STAT_BAD_OP = 0x02,
  55. /* Parameter not supported or parameter out of range: */
  56. CMD_STAT_BAD_PARAM = 0x03,
  57. /* System not enabled or bad system state: */
  58. CMD_STAT_BAD_SYS_STATE = 0x04,
  59. /* Attempt to access reserved or unallocaterd resource: */
  60. CMD_STAT_BAD_RESOURCE = 0x05,
  61. /* Requested resource is currently executing a command, or is otherwise busy: */
  62. CMD_STAT_RESOURCE_BUSY = 0x06,
  63. /* Required capability exceeds device limits: */
  64. CMD_STAT_EXCEED_LIM = 0x08,
  65. /* Resource is not in the appropriate state or ownership: */
  66. CMD_STAT_BAD_RES_STATE = 0x09,
  67. /* Index out of range: */
  68. CMD_STAT_BAD_INDEX = 0x0a,
  69. /* FW image corrupted: */
  70. CMD_STAT_BAD_NVMEM = 0x0b,
  71. /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
  72. CMD_STAT_ICM_ERROR = 0x0c,
  73. /* Attempt to modify a QP/EE which is not in the presumed state: */
  74. CMD_STAT_BAD_QP_STATE = 0x10,
  75. /* Bad segment parameters (Address/Size): */
  76. CMD_STAT_BAD_SEG_PARAM = 0x20,
  77. /* Memory Region has Memory Windows bound to: */
  78. CMD_STAT_REG_BOUND = 0x21,
  79. /* HCA local attached memory not present: */
  80. CMD_STAT_LAM_NOT_PRE = 0x22,
  81. /* Bad management packet (silently discarded): */
  82. CMD_STAT_BAD_PKT = 0x30,
  83. /* More outstanding CQEs in CQ than new CQ size: */
  84. CMD_STAT_BAD_SIZE = 0x40,
  85. /* Multi Function device support required: */
  86. CMD_STAT_MULTI_FUNC_REQ = 0x50,
  87. };
  88. enum {
  89. HCR_IN_PARAM_OFFSET = 0x00,
  90. HCR_IN_MODIFIER_OFFSET = 0x08,
  91. HCR_OUT_PARAM_OFFSET = 0x0c,
  92. HCR_TOKEN_OFFSET = 0x14,
  93. HCR_STATUS_OFFSET = 0x18,
  94. HCR_OPMOD_SHIFT = 12,
  95. HCR_T_BIT = 21,
  96. HCR_E_BIT = 22,
  97. HCR_GO_BIT = 23
  98. };
  99. enum {
  100. GO_BIT_TIMEOUT_MSECS = 10000
  101. };
  102. struct mlx4_cmd_context {
  103. struct completion done;
  104. int result;
  105. int next;
  106. u64 out_param;
  107. u16 token;
  108. u8 fw_status;
  109. };
  110. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  111. struct mlx4_vhcr_cmd *in_vhcr);
  112. static int mlx4_status_to_errno(u8 status)
  113. {
  114. static const int trans_table[] = {
  115. [CMD_STAT_INTERNAL_ERR] = -EIO,
  116. [CMD_STAT_BAD_OP] = -EPERM,
  117. [CMD_STAT_BAD_PARAM] = -EINVAL,
  118. [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
  119. [CMD_STAT_BAD_RESOURCE] = -EBADF,
  120. [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
  121. [CMD_STAT_EXCEED_LIM] = -ENOMEM,
  122. [CMD_STAT_BAD_RES_STATE] = -EBADF,
  123. [CMD_STAT_BAD_INDEX] = -EBADF,
  124. [CMD_STAT_BAD_NVMEM] = -EFAULT,
  125. [CMD_STAT_ICM_ERROR] = -ENFILE,
  126. [CMD_STAT_BAD_QP_STATE] = -EINVAL,
  127. [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
  128. [CMD_STAT_REG_BOUND] = -EBUSY,
  129. [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
  130. [CMD_STAT_BAD_PKT] = -EINVAL,
  131. [CMD_STAT_BAD_SIZE] = -ENOMEM,
  132. [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
  133. };
  134. if (status >= ARRAY_SIZE(trans_table) ||
  135. (status != CMD_STAT_OK && trans_table[status] == 0))
  136. return -EIO;
  137. return trans_table[status];
  138. }
  139. static u8 mlx4_errno_to_status(int errno)
  140. {
  141. switch (errno) {
  142. case -EPERM:
  143. return CMD_STAT_BAD_OP;
  144. case -EINVAL:
  145. return CMD_STAT_BAD_PARAM;
  146. case -ENXIO:
  147. return CMD_STAT_BAD_SYS_STATE;
  148. case -EBUSY:
  149. return CMD_STAT_RESOURCE_BUSY;
  150. case -ENOMEM:
  151. return CMD_STAT_EXCEED_LIM;
  152. case -ENFILE:
  153. return CMD_STAT_ICM_ERROR;
  154. default:
  155. return CMD_STAT_INTERNAL_ERR;
  156. }
  157. }
  158. static int comm_pending(struct mlx4_dev *dev)
  159. {
  160. struct mlx4_priv *priv = mlx4_priv(dev);
  161. u32 status = readl(&priv->mfunc.comm->slave_read);
  162. return (swab32(status) >> 31) != priv->cmd.comm_toggle;
  163. }
  164. static void mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
  165. {
  166. struct mlx4_priv *priv = mlx4_priv(dev);
  167. u32 val;
  168. priv->cmd.comm_toggle ^= 1;
  169. val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
  170. __raw_writel((__force u32) cpu_to_be32(val),
  171. &priv->mfunc.comm->slave_write);
  172. mmiowb();
  173. }
  174. static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
  175. unsigned long timeout)
  176. {
  177. struct mlx4_priv *priv = mlx4_priv(dev);
  178. unsigned long end;
  179. int err = 0;
  180. int ret_from_pending = 0;
  181. /* First, verify that the master reports correct status */
  182. if (comm_pending(dev)) {
  183. mlx4_warn(dev, "Communication channel is not idle."
  184. "my toggle is %d (cmd:0x%x)\n",
  185. priv->cmd.comm_toggle, cmd);
  186. return -EAGAIN;
  187. }
  188. /* Write command */
  189. down(&priv->cmd.poll_sem);
  190. mlx4_comm_cmd_post(dev, cmd, param);
  191. end = msecs_to_jiffies(timeout) + jiffies;
  192. while (comm_pending(dev) && time_before(jiffies, end))
  193. cond_resched();
  194. ret_from_pending = comm_pending(dev);
  195. if (ret_from_pending) {
  196. /* check if the slave is trying to boot in the middle of
  197. * FLR process. The only non-zero result in the RESET command
  198. * is MLX4_DELAY_RESET_SLAVE*/
  199. if ((MLX4_COMM_CMD_RESET == cmd)) {
  200. mlx4_warn(dev, "Got slave FLRed from Communication"
  201. " channel (ret:0x%x)\n", ret_from_pending);
  202. err = MLX4_DELAY_RESET_SLAVE;
  203. } else {
  204. mlx4_warn(dev, "Communication channel timed out\n");
  205. err = -ETIMEDOUT;
  206. }
  207. }
  208. up(&priv->cmd.poll_sem);
  209. return err;
  210. }
  211. static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 op,
  212. u16 param, unsigned long timeout)
  213. {
  214. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  215. struct mlx4_cmd_context *context;
  216. unsigned long end;
  217. int err = 0;
  218. down(&cmd->event_sem);
  219. spin_lock(&cmd->context_lock);
  220. BUG_ON(cmd->free_head < 0);
  221. context = &cmd->context[cmd->free_head];
  222. context->token += cmd->token_mask + 1;
  223. cmd->free_head = context->next;
  224. spin_unlock(&cmd->context_lock);
  225. init_completion(&context->done);
  226. mlx4_comm_cmd_post(dev, op, param);
  227. if (!wait_for_completion_timeout(&context->done,
  228. msecs_to_jiffies(timeout))) {
  229. err = -EBUSY;
  230. goto out;
  231. }
  232. err = context->result;
  233. if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
  234. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  235. op, context->fw_status);
  236. goto out;
  237. }
  238. out:
  239. /* wait for comm channel ready
  240. * this is necessary for prevention the race
  241. * when switching between event to polling mode
  242. */
  243. end = msecs_to_jiffies(timeout) + jiffies;
  244. while (comm_pending(dev) && time_before(jiffies, end))
  245. cond_resched();
  246. spin_lock(&cmd->context_lock);
  247. context->next = cmd->free_head;
  248. cmd->free_head = context - cmd->context;
  249. spin_unlock(&cmd->context_lock);
  250. up(&cmd->event_sem);
  251. return err;
  252. }
  253. int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
  254. unsigned long timeout)
  255. {
  256. if (mlx4_priv(dev)->cmd.use_events)
  257. return mlx4_comm_cmd_wait(dev, cmd, param, timeout);
  258. return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
  259. }
  260. static int cmd_pending(struct mlx4_dev *dev)
  261. {
  262. u32 status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
  263. return (status & swab32(1 << HCR_GO_BIT)) ||
  264. (mlx4_priv(dev)->cmd.toggle ==
  265. !!(status & swab32(1 << HCR_T_BIT)));
  266. }
  267. static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
  268. u32 in_modifier, u8 op_modifier, u16 op, u16 token,
  269. int event)
  270. {
  271. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  272. u32 __iomem *hcr = cmd->hcr;
  273. int ret = -EAGAIN;
  274. unsigned long end;
  275. mutex_lock(&cmd->hcr_mutex);
  276. end = jiffies;
  277. if (event)
  278. end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
  279. while (cmd_pending(dev)) {
  280. if (time_after_eq(jiffies, end)) {
  281. mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
  282. goto out;
  283. }
  284. cond_resched();
  285. }
  286. /*
  287. * We use writel (instead of something like memcpy_toio)
  288. * because writes of less than 32 bits to the HCR don't work
  289. * (and some architectures such as ia64 implement memcpy_toio
  290. * in terms of writeb).
  291. */
  292. __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
  293. __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
  294. __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
  295. __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
  296. __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
  297. __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
  298. /* __raw_writel may not order writes. */
  299. wmb();
  300. __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
  301. (cmd->toggle << HCR_T_BIT) |
  302. (event ? (1 << HCR_E_BIT) : 0) |
  303. (op_modifier << HCR_OPMOD_SHIFT) |
  304. op), hcr + 6);
  305. /*
  306. * Make sure that our HCR writes don't get mixed in with
  307. * writes from another CPU starting a FW command.
  308. */
  309. mmiowb();
  310. cmd->toggle = cmd->toggle ^ 1;
  311. ret = 0;
  312. out:
  313. mutex_unlock(&cmd->hcr_mutex);
  314. return ret;
  315. }
  316. static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  317. int out_is_imm, u32 in_modifier, u8 op_modifier,
  318. u16 op, unsigned long timeout)
  319. {
  320. struct mlx4_priv *priv = mlx4_priv(dev);
  321. struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
  322. int ret;
  323. down(&priv->cmd.slave_sem);
  324. vhcr->in_param = cpu_to_be64(in_param);
  325. vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
  326. vhcr->in_modifier = cpu_to_be32(in_modifier);
  327. vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
  328. vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
  329. vhcr->status = 0;
  330. vhcr->flags = !!(priv->cmd.use_events) << 6;
  331. if (mlx4_is_master(dev)) {
  332. ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
  333. if (!ret) {
  334. if (out_is_imm) {
  335. if (out_param)
  336. *out_param =
  337. be64_to_cpu(vhcr->out_param);
  338. else {
  339. mlx4_err(dev, "response expected while"
  340. "output mailbox is NULL for "
  341. "command 0x%x\n", op);
  342. vhcr->status = CMD_STAT_BAD_PARAM;
  343. }
  344. }
  345. ret = mlx4_status_to_errno(vhcr->status);
  346. }
  347. } else {
  348. ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0,
  349. MLX4_COMM_TIME + timeout);
  350. if (!ret) {
  351. if (out_is_imm) {
  352. if (out_param)
  353. *out_param =
  354. be64_to_cpu(vhcr->out_param);
  355. else {
  356. mlx4_err(dev, "response expected while"
  357. "output mailbox is NULL for "
  358. "command 0x%x\n", op);
  359. vhcr->status = CMD_STAT_BAD_PARAM;
  360. }
  361. }
  362. ret = mlx4_status_to_errno(vhcr->status);
  363. } else
  364. mlx4_err(dev, "failed execution of VHCR_POST command"
  365. "opcode 0x%x\n", op);
  366. }
  367. up(&priv->cmd.slave_sem);
  368. return ret;
  369. }
  370. static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  371. int out_is_imm, u32 in_modifier, u8 op_modifier,
  372. u16 op, unsigned long timeout)
  373. {
  374. struct mlx4_priv *priv = mlx4_priv(dev);
  375. void __iomem *hcr = priv->cmd.hcr;
  376. int err = 0;
  377. unsigned long end;
  378. u32 stat;
  379. down(&priv->cmd.poll_sem);
  380. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  381. in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
  382. if (err)
  383. goto out;
  384. end = msecs_to_jiffies(timeout) + jiffies;
  385. while (cmd_pending(dev) && time_before(jiffies, end))
  386. cond_resched();
  387. if (cmd_pending(dev)) {
  388. err = -ETIMEDOUT;
  389. goto out;
  390. }
  391. if (out_is_imm)
  392. *out_param =
  393. (u64) be32_to_cpu((__force __be32)
  394. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
  395. (u64) be32_to_cpu((__force __be32)
  396. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
  397. stat = be32_to_cpu((__force __be32)
  398. __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
  399. err = mlx4_status_to_errno(stat);
  400. if (err)
  401. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  402. op, stat);
  403. out:
  404. up(&priv->cmd.poll_sem);
  405. return err;
  406. }
  407. void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
  408. {
  409. struct mlx4_priv *priv = mlx4_priv(dev);
  410. struct mlx4_cmd_context *context =
  411. &priv->cmd.context[token & priv->cmd.token_mask];
  412. /* previously timed out command completing at long last */
  413. if (token != context->token)
  414. return;
  415. context->fw_status = status;
  416. context->result = mlx4_status_to_errno(status);
  417. context->out_param = out_param;
  418. complete(&context->done);
  419. }
  420. static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  421. int out_is_imm, u32 in_modifier, u8 op_modifier,
  422. u16 op, unsigned long timeout)
  423. {
  424. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  425. struct mlx4_cmd_context *context;
  426. int err = 0;
  427. down(&cmd->event_sem);
  428. spin_lock(&cmd->context_lock);
  429. BUG_ON(cmd->free_head < 0);
  430. context = &cmd->context[cmd->free_head];
  431. context->token += cmd->token_mask + 1;
  432. cmd->free_head = context->next;
  433. spin_unlock(&cmd->context_lock);
  434. init_completion(&context->done);
  435. mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  436. in_modifier, op_modifier, op, context->token, 1);
  437. if (!wait_for_completion_timeout(&context->done,
  438. msecs_to_jiffies(timeout))) {
  439. err = -EBUSY;
  440. goto out;
  441. }
  442. err = context->result;
  443. if (err) {
  444. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  445. op, context->fw_status);
  446. goto out;
  447. }
  448. if (out_is_imm)
  449. *out_param = context->out_param;
  450. out:
  451. spin_lock(&cmd->context_lock);
  452. context->next = cmd->free_head;
  453. cmd->free_head = context - cmd->context;
  454. spin_unlock(&cmd->context_lock);
  455. up(&cmd->event_sem);
  456. return err;
  457. }
  458. int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  459. int out_is_imm, u32 in_modifier, u8 op_modifier,
  460. u16 op, unsigned long timeout, int native)
  461. {
  462. if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
  463. if (mlx4_priv(dev)->cmd.use_events)
  464. return mlx4_cmd_wait(dev, in_param, out_param,
  465. out_is_imm, in_modifier,
  466. op_modifier, op, timeout);
  467. else
  468. return mlx4_cmd_poll(dev, in_param, out_param,
  469. out_is_imm, in_modifier,
  470. op_modifier, op, timeout);
  471. }
  472. return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
  473. in_modifier, op_modifier, op, timeout);
  474. }
  475. EXPORT_SYMBOL_GPL(__mlx4_cmd);
  476. static int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
  477. {
  478. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
  479. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  480. }
  481. static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
  482. int slave, u64 slave_addr,
  483. int size, int is_read)
  484. {
  485. u64 in_param;
  486. u64 out_param;
  487. if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
  488. (slave & ~0x7f) | (size & 0xff)) {
  489. mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx "
  490. "master_addr:0x%llx slave_id:%d size:%d\n",
  491. slave_addr, master_addr, slave, size);
  492. return -EINVAL;
  493. }
  494. if (is_read) {
  495. in_param = (u64) slave | slave_addr;
  496. out_param = (u64) dev->caps.function | master_addr;
  497. } else {
  498. in_param = (u64) dev->caps.function | master_addr;
  499. out_param = (u64) slave | slave_addr;
  500. }
  501. return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
  502. MLX4_CMD_ACCESS_MEM,
  503. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  504. }
  505. int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
  506. struct mlx4_vhcr *vhcr,
  507. struct mlx4_cmd_mailbox *inbox,
  508. struct mlx4_cmd_mailbox *outbox,
  509. struct mlx4_cmd_info *cmd)
  510. {
  511. u64 in_param;
  512. u64 out_param;
  513. int err;
  514. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  515. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  516. if (cmd->encode_slave_id) {
  517. in_param &= 0xffffffffffffff00ll;
  518. in_param |= slave;
  519. }
  520. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  521. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  522. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  523. if (cmd->out_is_imm)
  524. vhcr->out_param = out_param;
  525. return err;
  526. }
  527. static struct mlx4_cmd_info cmd_info[] = {
  528. {
  529. .opcode = MLX4_CMD_QUERY_FW,
  530. .has_inbox = false,
  531. .has_outbox = true,
  532. .out_is_imm = false,
  533. .encode_slave_id = false,
  534. .verify = NULL,
  535. .wrapper = mlx4_QUERY_FW_wrapper
  536. },
  537. {
  538. .opcode = MLX4_CMD_QUERY_HCA,
  539. .has_inbox = false,
  540. .has_outbox = true,
  541. .out_is_imm = false,
  542. .encode_slave_id = false,
  543. .verify = NULL,
  544. .wrapper = NULL
  545. },
  546. {
  547. .opcode = MLX4_CMD_QUERY_DEV_CAP,
  548. .has_inbox = false,
  549. .has_outbox = true,
  550. .out_is_imm = false,
  551. .encode_slave_id = false,
  552. .verify = NULL,
  553. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  554. },
  555. {
  556. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  557. .has_inbox = false,
  558. .has_outbox = true,
  559. .out_is_imm = false,
  560. .encode_slave_id = false,
  561. .verify = NULL,
  562. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  563. },
  564. {
  565. .opcode = MLX4_CMD_QUERY_ADAPTER,
  566. .has_inbox = false,
  567. .has_outbox = true,
  568. .out_is_imm = false,
  569. .encode_slave_id = false,
  570. .verify = NULL,
  571. .wrapper = NULL
  572. },
  573. {
  574. .opcode = MLX4_CMD_INIT_PORT,
  575. .has_inbox = false,
  576. .has_outbox = false,
  577. .out_is_imm = false,
  578. .encode_slave_id = false,
  579. .verify = NULL,
  580. .wrapper = mlx4_INIT_PORT_wrapper
  581. },
  582. {
  583. .opcode = MLX4_CMD_CLOSE_PORT,
  584. .has_inbox = false,
  585. .has_outbox = false,
  586. .out_is_imm = false,
  587. .encode_slave_id = false,
  588. .verify = NULL,
  589. .wrapper = mlx4_CLOSE_PORT_wrapper
  590. },
  591. {
  592. .opcode = MLX4_CMD_QUERY_PORT,
  593. .has_inbox = false,
  594. .has_outbox = true,
  595. .out_is_imm = false,
  596. .encode_slave_id = false,
  597. .verify = NULL,
  598. .wrapper = mlx4_QUERY_PORT_wrapper
  599. },
  600. {
  601. .opcode = MLX4_CMD_SET_PORT,
  602. .has_inbox = true,
  603. .has_outbox = false,
  604. .out_is_imm = false,
  605. .encode_slave_id = false,
  606. .verify = NULL,
  607. .wrapper = mlx4_SET_PORT_wrapper
  608. },
  609. {
  610. .opcode = MLX4_CMD_MAP_EQ,
  611. .has_inbox = false,
  612. .has_outbox = false,
  613. .out_is_imm = false,
  614. .encode_slave_id = false,
  615. .verify = NULL,
  616. .wrapper = mlx4_MAP_EQ_wrapper
  617. },
  618. {
  619. .opcode = MLX4_CMD_SW2HW_EQ,
  620. .has_inbox = true,
  621. .has_outbox = false,
  622. .out_is_imm = false,
  623. .encode_slave_id = true,
  624. .verify = NULL,
  625. .wrapper = mlx4_SW2HW_EQ_wrapper
  626. },
  627. {
  628. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  629. .has_inbox = false,
  630. .has_outbox = false,
  631. .out_is_imm = false,
  632. .encode_slave_id = false,
  633. .verify = NULL,
  634. .wrapper = NULL
  635. },
  636. {
  637. .opcode = MLX4_CMD_NOP,
  638. .has_inbox = false,
  639. .has_outbox = false,
  640. .out_is_imm = false,
  641. .encode_slave_id = false,
  642. .verify = NULL,
  643. .wrapper = NULL
  644. },
  645. {
  646. .opcode = MLX4_CMD_ALLOC_RES,
  647. .has_inbox = false,
  648. .has_outbox = false,
  649. .out_is_imm = true,
  650. .encode_slave_id = false,
  651. .verify = NULL,
  652. .wrapper = mlx4_ALLOC_RES_wrapper
  653. },
  654. {
  655. .opcode = MLX4_CMD_FREE_RES,
  656. .has_inbox = false,
  657. .has_outbox = false,
  658. .out_is_imm = false,
  659. .encode_slave_id = false,
  660. .verify = NULL,
  661. .wrapper = mlx4_FREE_RES_wrapper
  662. },
  663. {
  664. .opcode = MLX4_CMD_SW2HW_MPT,
  665. .has_inbox = true,
  666. .has_outbox = false,
  667. .out_is_imm = false,
  668. .encode_slave_id = true,
  669. .verify = NULL,
  670. .wrapper = mlx4_SW2HW_MPT_wrapper
  671. },
  672. {
  673. .opcode = MLX4_CMD_QUERY_MPT,
  674. .has_inbox = false,
  675. .has_outbox = true,
  676. .out_is_imm = false,
  677. .encode_slave_id = false,
  678. .verify = NULL,
  679. .wrapper = mlx4_QUERY_MPT_wrapper
  680. },
  681. {
  682. .opcode = MLX4_CMD_HW2SW_MPT,
  683. .has_inbox = false,
  684. .has_outbox = false,
  685. .out_is_imm = false,
  686. .encode_slave_id = false,
  687. .verify = NULL,
  688. .wrapper = mlx4_HW2SW_MPT_wrapper
  689. },
  690. {
  691. .opcode = MLX4_CMD_READ_MTT,
  692. .has_inbox = false,
  693. .has_outbox = true,
  694. .out_is_imm = false,
  695. .encode_slave_id = false,
  696. .verify = NULL,
  697. .wrapper = NULL
  698. },
  699. {
  700. .opcode = MLX4_CMD_WRITE_MTT,
  701. .has_inbox = true,
  702. .has_outbox = false,
  703. .out_is_imm = false,
  704. .encode_slave_id = false,
  705. .verify = NULL,
  706. .wrapper = mlx4_WRITE_MTT_wrapper
  707. },
  708. {
  709. .opcode = MLX4_CMD_SYNC_TPT,
  710. .has_inbox = true,
  711. .has_outbox = false,
  712. .out_is_imm = false,
  713. .encode_slave_id = false,
  714. .verify = NULL,
  715. .wrapper = NULL
  716. },
  717. {
  718. .opcode = MLX4_CMD_HW2SW_EQ,
  719. .has_inbox = false,
  720. .has_outbox = true,
  721. .out_is_imm = false,
  722. .encode_slave_id = true,
  723. .verify = NULL,
  724. .wrapper = mlx4_HW2SW_EQ_wrapper
  725. },
  726. {
  727. .opcode = MLX4_CMD_QUERY_EQ,
  728. .has_inbox = false,
  729. .has_outbox = true,
  730. .out_is_imm = false,
  731. .encode_slave_id = true,
  732. .verify = NULL,
  733. .wrapper = mlx4_QUERY_EQ_wrapper
  734. },
  735. {
  736. .opcode = MLX4_CMD_SW2HW_CQ,
  737. .has_inbox = true,
  738. .has_outbox = false,
  739. .out_is_imm = false,
  740. .encode_slave_id = true,
  741. .verify = NULL,
  742. .wrapper = mlx4_SW2HW_CQ_wrapper
  743. },
  744. {
  745. .opcode = MLX4_CMD_HW2SW_CQ,
  746. .has_inbox = false,
  747. .has_outbox = false,
  748. .out_is_imm = false,
  749. .encode_slave_id = false,
  750. .verify = NULL,
  751. .wrapper = mlx4_HW2SW_CQ_wrapper
  752. },
  753. {
  754. .opcode = MLX4_CMD_QUERY_CQ,
  755. .has_inbox = false,
  756. .has_outbox = true,
  757. .out_is_imm = false,
  758. .encode_slave_id = false,
  759. .verify = NULL,
  760. .wrapper = mlx4_QUERY_CQ_wrapper
  761. },
  762. {
  763. .opcode = MLX4_CMD_MODIFY_CQ,
  764. .has_inbox = true,
  765. .has_outbox = false,
  766. .out_is_imm = true,
  767. .encode_slave_id = false,
  768. .verify = NULL,
  769. .wrapper = mlx4_MODIFY_CQ_wrapper
  770. },
  771. {
  772. .opcode = MLX4_CMD_SW2HW_SRQ,
  773. .has_inbox = true,
  774. .has_outbox = false,
  775. .out_is_imm = false,
  776. .encode_slave_id = true,
  777. .verify = NULL,
  778. .wrapper = mlx4_SW2HW_SRQ_wrapper
  779. },
  780. {
  781. .opcode = MLX4_CMD_HW2SW_SRQ,
  782. .has_inbox = false,
  783. .has_outbox = false,
  784. .out_is_imm = false,
  785. .encode_slave_id = false,
  786. .verify = NULL,
  787. .wrapper = mlx4_HW2SW_SRQ_wrapper
  788. },
  789. {
  790. .opcode = MLX4_CMD_QUERY_SRQ,
  791. .has_inbox = false,
  792. .has_outbox = true,
  793. .out_is_imm = false,
  794. .encode_slave_id = false,
  795. .verify = NULL,
  796. .wrapper = mlx4_QUERY_SRQ_wrapper
  797. },
  798. {
  799. .opcode = MLX4_CMD_ARM_SRQ,
  800. .has_inbox = false,
  801. .has_outbox = false,
  802. .out_is_imm = false,
  803. .encode_slave_id = false,
  804. .verify = NULL,
  805. .wrapper = mlx4_ARM_SRQ_wrapper
  806. },
  807. {
  808. .opcode = MLX4_CMD_RST2INIT_QP,
  809. .has_inbox = true,
  810. .has_outbox = false,
  811. .out_is_imm = false,
  812. .encode_slave_id = true,
  813. .verify = NULL,
  814. .wrapper = mlx4_RST2INIT_QP_wrapper
  815. },
  816. {
  817. .opcode = MLX4_CMD_INIT2INIT_QP,
  818. .has_inbox = true,
  819. .has_outbox = false,
  820. .out_is_imm = false,
  821. .encode_slave_id = false,
  822. .verify = NULL,
  823. .wrapper = mlx4_GEN_QP_wrapper
  824. },
  825. {
  826. .opcode = MLX4_CMD_INIT2RTR_QP,
  827. .has_inbox = true,
  828. .has_outbox = false,
  829. .out_is_imm = false,
  830. .encode_slave_id = false,
  831. .verify = NULL,
  832. .wrapper = mlx4_INIT2RTR_QP_wrapper
  833. },
  834. {
  835. .opcode = MLX4_CMD_RTR2RTS_QP,
  836. .has_inbox = true,
  837. .has_outbox = false,
  838. .out_is_imm = false,
  839. .encode_slave_id = false,
  840. .verify = NULL,
  841. .wrapper = mlx4_GEN_QP_wrapper
  842. },
  843. {
  844. .opcode = MLX4_CMD_RTS2RTS_QP,
  845. .has_inbox = true,
  846. .has_outbox = false,
  847. .out_is_imm = false,
  848. .encode_slave_id = false,
  849. .verify = NULL,
  850. .wrapper = mlx4_GEN_QP_wrapper
  851. },
  852. {
  853. .opcode = MLX4_CMD_SQERR2RTS_QP,
  854. .has_inbox = true,
  855. .has_outbox = false,
  856. .out_is_imm = false,
  857. .encode_slave_id = false,
  858. .verify = NULL,
  859. .wrapper = mlx4_GEN_QP_wrapper
  860. },
  861. {
  862. .opcode = MLX4_CMD_2ERR_QP,
  863. .has_inbox = false,
  864. .has_outbox = false,
  865. .out_is_imm = false,
  866. .encode_slave_id = false,
  867. .verify = NULL,
  868. .wrapper = mlx4_GEN_QP_wrapper
  869. },
  870. {
  871. .opcode = MLX4_CMD_RTS2SQD_QP,
  872. .has_inbox = false,
  873. .has_outbox = false,
  874. .out_is_imm = false,
  875. .encode_slave_id = false,
  876. .verify = NULL,
  877. .wrapper = mlx4_GEN_QP_wrapper
  878. },
  879. {
  880. .opcode = MLX4_CMD_SQD2SQD_QP,
  881. .has_inbox = true,
  882. .has_outbox = false,
  883. .out_is_imm = false,
  884. .encode_slave_id = false,
  885. .verify = NULL,
  886. .wrapper = mlx4_GEN_QP_wrapper
  887. },
  888. {
  889. .opcode = MLX4_CMD_SQD2RTS_QP,
  890. .has_inbox = true,
  891. .has_outbox = false,
  892. .out_is_imm = false,
  893. .encode_slave_id = false,
  894. .verify = NULL,
  895. .wrapper = mlx4_GEN_QP_wrapper
  896. },
  897. {
  898. .opcode = MLX4_CMD_2RST_QP,
  899. .has_inbox = false,
  900. .has_outbox = false,
  901. .out_is_imm = false,
  902. .encode_slave_id = false,
  903. .verify = NULL,
  904. .wrapper = mlx4_2RST_QP_wrapper
  905. },
  906. {
  907. .opcode = MLX4_CMD_QUERY_QP,
  908. .has_inbox = false,
  909. .has_outbox = true,
  910. .out_is_imm = false,
  911. .encode_slave_id = false,
  912. .verify = NULL,
  913. .wrapper = mlx4_GEN_QP_wrapper
  914. },
  915. {
  916. .opcode = MLX4_CMD_SUSPEND_QP,
  917. .has_inbox = false,
  918. .has_outbox = false,
  919. .out_is_imm = false,
  920. .encode_slave_id = false,
  921. .verify = NULL,
  922. .wrapper = mlx4_GEN_QP_wrapper
  923. },
  924. {
  925. .opcode = MLX4_CMD_UNSUSPEND_QP,
  926. .has_inbox = false,
  927. .has_outbox = false,
  928. .out_is_imm = false,
  929. .encode_slave_id = false,
  930. .verify = NULL,
  931. .wrapper = mlx4_GEN_QP_wrapper
  932. },
  933. {
  934. .opcode = MLX4_CMD_QUERY_IF_STAT,
  935. .has_inbox = false,
  936. .has_outbox = true,
  937. .out_is_imm = false,
  938. .encode_slave_id = false,
  939. .verify = NULL,
  940. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  941. },
  942. /* Native multicast commands are not available for guests */
  943. {
  944. .opcode = MLX4_CMD_QP_ATTACH,
  945. .has_inbox = true,
  946. .has_outbox = false,
  947. .out_is_imm = false,
  948. .encode_slave_id = false,
  949. .verify = NULL,
  950. .wrapper = mlx4_QP_ATTACH_wrapper
  951. },
  952. {
  953. .opcode = MLX4_CMD_PROMISC,
  954. .has_inbox = false,
  955. .has_outbox = false,
  956. .out_is_imm = false,
  957. .encode_slave_id = false,
  958. .verify = NULL,
  959. .wrapper = mlx4_PROMISC_wrapper
  960. },
  961. /* Ethernet specific commands */
  962. {
  963. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  964. .has_inbox = true,
  965. .has_outbox = false,
  966. .out_is_imm = false,
  967. .encode_slave_id = false,
  968. .verify = NULL,
  969. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  970. },
  971. {
  972. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  973. .has_inbox = false,
  974. .has_outbox = false,
  975. .out_is_imm = false,
  976. .encode_slave_id = false,
  977. .verify = NULL,
  978. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  979. },
  980. {
  981. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  982. .has_inbox = false,
  983. .has_outbox = true,
  984. .out_is_imm = false,
  985. .encode_slave_id = false,
  986. .verify = NULL,
  987. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  988. },
  989. {
  990. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  991. .has_inbox = false,
  992. .has_outbox = false,
  993. .out_is_imm = false,
  994. .encode_slave_id = false,
  995. .verify = NULL,
  996. .wrapper = NULL
  997. },
  998. /* flow steering commands */
  999. {
  1000. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1001. .has_inbox = true,
  1002. .has_outbox = false,
  1003. .out_is_imm = true,
  1004. .encode_slave_id = false,
  1005. .verify = NULL,
  1006. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1007. },
  1008. {
  1009. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1010. .has_inbox = false,
  1011. .has_outbox = false,
  1012. .out_is_imm = false,
  1013. .encode_slave_id = false,
  1014. .verify = NULL,
  1015. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1016. },
  1017. };
  1018. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1019. struct mlx4_vhcr_cmd *in_vhcr)
  1020. {
  1021. struct mlx4_priv *priv = mlx4_priv(dev);
  1022. struct mlx4_cmd_info *cmd = NULL;
  1023. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1024. struct mlx4_vhcr *vhcr;
  1025. struct mlx4_cmd_mailbox *inbox = NULL;
  1026. struct mlx4_cmd_mailbox *outbox = NULL;
  1027. u64 in_param;
  1028. u64 out_param;
  1029. int ret = 0;
  1030. int i;
  1031. int err = 0;
  1032. /* Create sw representation of Virtual HCR */
  1033. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1034. if (!vhcr)
  1035. return -ENOMEM;
  1036. /* DMA in the vHCR */
  1037. if (!in_vhcr) {
  1038. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1039. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1040. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1041. MLX4_ACCESS_MEM_ALIGN), 1);
  1042. if (ret) {
  1043. mlx4_err(dev, "%s:Failed reading vhcr"
  1044. "ret: 0x%x\n", __func__, ret);
  1045. kfree(vhcr);
  1046. return ret;
  1047. }
  1048. }
  1049. /* Fill SW VHCR fields */
  1050. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1051. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1052. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1053. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1054. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1055. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1056. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1057. /* Lookup command */
  1058. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1059. if (vhcr->op == cmd_info[i].opcode) {
  1060. cmd = &cmd_info[i];
  1061. break;
  1062. }
  1063. }
  1064. if (!cmd) {
  1065. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1066. vhcr->op, slave);
  1067. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1068. goto out_status;
  1069. }
  1070. /* Read inbox */
  1071. if (cmd->has_inbox) {
  1072. vhcr->in_param &= INBOX_MASK;
  1073. inbox = mlx4_alloc_cmd_mailbox(dev);
  1074. if (IS_ERR(inbox)) {
  1075. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1076. inbox = NULL;
  1077. goto out_status;
  1078. }
  1079. if (mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1080. vhcr->in_param,
  1081. MLX4_MAILBOX_SIZE, 1)) {
  1082. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1083. __func__, cmd->opcode);
  1084. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1085. goto out_status;
  1086. }
  1087. }
  1088. /* Apply permission and bound checks if applicable */
  1089. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1090. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection "
  1091. "checks for resource_id:%d\n", vhcr->op, slave,
  1092. vhcr->in_modifier);
  1093. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1094. goto out_status;
  1095. }
  1096. /* Allocate outbox */
  1097. if (cmd->has_outbox) {
  1098. outbox = mlx4_alloc_cmd_mailbox(dev);
  1099. if (IS_ERR(outbox)) {
  1100. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1101. outbox = NULL;
  1102. goto out_status;
  1103. }
  1104. }
  1105. /* Execute the command! */
  1106. if (cmd->wrapper) {
  1107. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1108. cmd);
  1109. if (cmd->out_is_imm)
  1110. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1111. } else {
  1112. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1113. vhcr->in_param;
  1114. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1115. vhcr->out_param;
  1116. err = __mlx4_cmd(dev, in_param, &out_param,
  1117. cmd->out_is_imm, vhcr->in_modifier,
  1118. vhcr->op_modifier, vhcr->op,
  1119. MLX4_CMD_TIME_CLASS_A,
  1120. MLX4_CMD_NATIVE);
  1121. if (cmd->out_is_imm) {
  1122. vhcr->out_param = out_param;
  1123. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1124. }
  1125. }
  1126. if (err) {
  1127. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with"
  1128. " error:%d, status %d\n",
  1129. vhcr->op, slave, vhcr->errno, err);
  1130. vhcr_cmd->status = mlx4_errno_to_status(err);
  1131. goto out_status;
  1132. }
  1133. /* Write outbox if command completed successfully */
  1134. if (cmd->has_outbox && !vhcr_cmd->status) {
  1135. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1136. vhcr->out_param,
  1137. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1138. if (ret) {
  1139. /* If we failed to write back the outbox after the
  1140. *command was successfully executed, we must fail this
  1141. * slave, as it is now in undefined state */
  1142. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1143. goto out;
  1144. }
  1145. }
  1146. out_status:
  1147. /* DMA back vhcr result */
  1148. if (!in_vhcr) {
  1149. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1150. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1151. ALIGN(sizeof(struct mlx4_vhcr),
  1152. MLX4_ACCESS_MEM_ALIGN),
  1153. MLX4_CMD_WRAPPED);
  1154. if (ret)
  1155. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1156. __func__);
  1157. else if (vhcr->e_bit &&
  1158. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1159. mlx4_warn(dev, "Failed to generate command completion "
  1160. "eqe for slave %d\n", slave);
  1161. }
  1162. out:
  1163. kfree(vhcr);
  1164. mlx4_free_cmd_mailbox(dev, inbox);
  1165. mlx4_free_cmd_mailbox(dev, outbox);
  1166. return ret;
  1167. }
  1168. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1169. u16 param, u8 toggle)
  1170. {
  1171. struct mlx4_priv *priv = mlx4_priv(dev);
  1172. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1173. u32 reply;
  1174. u8 is_going_down = 0;
  1175. int i;
  1176. slave_state[slave].comm_toggle ^= 1;
  1177. reply = (u32) slave_state[slave].comm_toggle << 31;
  1178. if (toggle != slave_state[slave].comm_toggle) {
  1179. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER"
  1180. "STATE COMPROMISIED ***\n", toggle, slave);
  1181. goto reset_slave;
  1182. }
  1183. if (cmd == MLX4_COMM_CMD_RESET) {
  1184. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1185. slave_state[slave].active = false;
  1186. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1187. slave_state[slave].event_eq[i].eqn = -1;
  1188. slave_state[slave].event_eq[i].token = 0;
  1189. }
  1190. /*check if we are in the middle of FLR process,
  1191. if so return "retry" status to the slave*/
  1192. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1193. goto inform_slave_state;
  1194. /* write the version in the event field */
  1195. reply |= mlx4_comm_get_version();
  1196. goto reset_slave;
  1197. }
  1198. /*command from slave in the middle of FLR*/
  1199. if (cmd != MLX4_COMM_CMD_RESET &&
  1200. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1201. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) "
  1202. "in the middle of FLR\n", slave, cmd);
  1203. return;
  1204. }
  1205. switch (cmd) {
  1206. case MLX4_COMM_CMD_VHCR0:
  1207. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1208. goto reset_slave;
  1209. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1210. priv->mfunc.master.slave_state[slave].cookie = 0;
  1211. mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
  1212. break;
  1213. case MLX4_COMM_CMD_VHCR1:
  1214. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1215. goto reset_slave;
  1216. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1217. break;
  1218. case MLX4_COMM_CMD_VHCR2:
  1219. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1220. goto reset_slave;
  1221. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1222. break;
  1223. case MLX4_COMM_CMD_VHCR_EN:
  1224. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1225. goto reset_slave;
  1226. slave_state[slave].vhcr_dma |= param;
  1227. slave_state[slave].active = true;
  1228. break;
  1229. case MLX4_COMM_CMD_VHCR_POST:
  1230. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1231. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
  1232. goto reset_slave;
  1233. down(&priv->cmd.slave_sem);
  1234. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1235. mlx4_err(dev, "Failed processing vhcr for slave:%d,"
  1236. " resetting slave.\n", slave);
  1237. up(&priv->cmd.slave_sem);
  1238. goto reset_slave;
  1239. }
  1240. up(&priv->cmd.slave_sem);
  1241. break;
  1242. default:
  1243. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1244. goto reset_slave;
  1245. }
  1246. spin_lock(&priv->mfunc.master.slave_state_lock);
  1247. if (!slave_state[slave].is_slave_going_down)
  1248. slave_state[slave].last_cmd = cmd;
  1249. else
  1250. is_going_down = 1;
  1251. spin_unlock(&priv->mfunc.master.slave_state_lock);
  1252. if (is_going_down) {
  1253. mlx4_warn(dev, "Slave is going down aborting command(%d)"
  1254. " executing from slave:%d\n",
  1255. cmd, slave);
  1256. return;
  1257. }
  1258. __raw_writel((__force u32) cpu_to_be32(reply),
  1259. &priv->mfunc.comm[slave].slave_read);
  1260. mmiowb();
  1261. return;
  1262. reset_slave:
  1263. /* cleanup any slave resources */
  1264. mlx4_delete_all_resources_for_slave(dev, slave);
  1265. spin_lock(&priv->mfunc.master.slave_state_lock);
  1266. if (!slave_state[slave].is_slave_going_down)
  1267. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  1268. spin_unlock(&priv->mfunc.master.slave_state_lock);
  1269. /*with slave in the middle of flr, no need to clean resources again.*/
  1270. inform_slave_state:
  1271. memset(&slave_state[slave].event_eq, 0,
  1272. sizeof(struct mlx4_slave_event_eq_info));
  1273. __raw_writel((__force u32) cpu_to_be32(reply),
  1274. &priv->mfunc.comm[slave].slave_read);
  1275. wmb();
  1276. }
  1277. /* master command processing */
  1278. void mlx4_master_comm_channel(struct work_struct *work)
  1279. {
  1280. struct mlx4_mfunc_master_ctx *master =
  1281. container_of(work,
  1282. struct mlx4_mfunc_master_ctx,
  1283. comm_work);
  1284. struct mlx4_mfunc *mfunc =
  1285. container_of(master, struct mlx4_mfunc, master);
  1286. struct mlx4_priv *priv =
  1287. container_of(mfunc, struct mlx4_priv, mfunc);
  1288. struct mlx4_dev *dev = &priv->dev;
  1289. __be32 *bit_vec;
  1290. u32 comm_cmd;
  1291. u32 vec;
  1292. int i, j, slave;
  1293. int toggle;
  1294. int served = 0;
  1295. int reported = 0;
  1296. u32 slt;
  1297. bit_vec = master->comm_arm_bit_vector;
  1298. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  1299. vec = be32_to_cpu(bit_vec[i]);
  1300. for (j = 0; j < 32; j++) {
  1301. if (!(vec & (1 << j)))
  1302. continue;
  1303. ++reported;
  1304. slave = (i * 32) + j;
  1305. comm_cmd = swab32(readl(
  1306. &mfunc->comm[slave].slave_write));
  1307. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  1308. >> 31;
  1309. toggle = comm_cmd >> 31;
  1310. if (toggle != slt) {
  1311. if (master->slave_state[slave].comm_toggle
  1312. != slt) {
  1313. printk(KERN_INFO "slave %d out of sync."
  1314. " read toggle %d, state toggle %d. "
  1315. "Resynching.\n", slave, slt,
  1316. master->slave_state[slave].comm_toggle);
  1317. master->slave_state[slave].comm_toggle =
  1318. slt;
  1319. }
  1320. mlx4_master_do_cmd(dev, slave,
  1321. comm_cmd >> 16 & 0xff,
  1322. comm_cmd & 0xffff, toggle);
  1323. ++served;
  1324. }
  1325. }
  1326. }
  1327. if (reported && reported != served)
  1328. mlx4_warn(dev, "Got command event with bitmask from %d slaves"
  1329. " but %d were served\n",
  1330. reported, served);
  1331. if (mlx4_ARM_COMM_CHANNEL(dev))
  1332. mlx4_warn(dev, "Failed to arm comm channel events\n");
  1333. }
  1334. static int sync_toggles(struct mlx4_dev *dev)
  1335. {
  1336. struct mlx4_priv *priv = mlx4_priv(dev);
  1337. int wr_toggle;
  1338. int rd_toggle;
  1339. unsigned long end;
  1340. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
  1341. end = jiffies + msecs_to_jiffies(5000);
  1342. while (time_before(jiffies, end)) {
  1343. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
  1344. if (rd_toggle == wr_toggle) {
  1345. priv->cmd.comm_toggle = rd_toggle;
  1346. return 0;
  1347. }
  1348. cond_resched();
  1349. }
  1350. /*
  1351. * we could reach here if for example the previous VM using this
  1352. * function misbehaved and left the channel with unsynced state. We
  1353. * should fix this here and give this VM a chance to use a properly
  1354. * synced channel
  1355. */
  1356. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  1357. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  1358. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  1359. priv->cmd.comm_toggle = 0;
  1360. return 0;
  1361. }
  1362. int mlx4_multi_func_init(struct mlx4_dev *dev)
  1363. {
  1364. struct mlx4_priv *priv = mlx4_priv(dev);
  1365. struct mlx4_slave_state *s_state;
  1366. int i, j, err, port;
  1367. priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1368. &priv->mfunc.vhcr_dma,
  1369. GFP_KERNEL);
  1370. if (!priv->mfunc.vhcr) {
  1371. mlx4_err(dev, "Couldn't allocate vhcr.\n");
  1372. return -ENOMEM;
  1373. }
  1374. if (mlx4_is_master(dev))
  1375. priv->mfunc.comm =
  1376. ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
  1377. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  1378. else
  1379. priv->mfunc.comm =
  1380. ioremap(pci_resource_start(dev->pdev, 2) +
  1381. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  1382. if (!priv->mfunc.comm) {
  1383. mlx4_err(dev, "Couldn't map communication vector.\n");
  1384. goto err_vhcr;
  1385. }
  1386. if (mlx4_is_master(dev)) {
  1387. priv->mfunc.master.slave_state =
  1388. kzalloc(dev->num_slaves *
  1389. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  1390. if (!priv->mfunc.master.slave_state)
  1391. goto err_comm;
  1392. for (i = 0; i < dev->num_slaves; ++i) {
  1393. s_state = &priv->mfunc.master.slave_state[i];
  1394. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  1395. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  1396. s_state->event_eq[j].eqn = -1;
  1397. __raw_writel((__force u32) 0,
  1398. &priv->mfunc.comm[i].slave_write);
  1399. __raw_writel((__force u32) 0,
  1400. &priv->mfunc.comm[i].slave_read);
  1401. mmiowb();
  1402. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1403. s_state->vlan_filter[port] =
  1404. kzalloc(sizeof(struct mlx4_vlan_fltr),
  1405. GFP_KERNEL);
  1406. if (!s_state->vlan_filter[port]) {
  1407. if (--port)
  1408. kfree(s_state->vlan_filter[port]);
  1409. goto err_slaves;
  1410. }
  1411. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  1412. }
  1413. spin_lock_init(&s_state->lock);
  1414. }
  1415. memset(&priv->mfunc.master.cmd_eqe, 0, sizeof(struct mlx4_eqe));
  1416. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  1417. INIT_WORK(&priv->mfunc.master.comm_work,
  1418. mlx4_master_comm_channel);
  1419. INIT_WORK(&priv->mfunc.master.slave_event_work,
  1420. mlx4_gen_slave_eqe);
  1421. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  1422. mlx4_master_handle_slave_flr);
  1423. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  1424. priv->mfunc.master.comm_wq =
  1425. create_singlethread_workqueue("mlx4_comm");
  1426. if (!priv->mfunc.master.comm_wq)
  1427. goto err_slaves;
  1428. if (mlx4_init_resource_tracker(dev))
  1429. goto err_thread;
  1430. sema_init(&priv->cmd.slave_sem, 1);
  1431. err = mlx4_ARM_COMM_CHANNEL(dev);
  1432. if (err) {
  1433. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  1434. err);
  1435. goto err_resource;
  1436. }
  1437. } else {
  1438. err = sync_toggles(dev);
  1439. if (err) {
  1440. mlx4_err(dev, "Couldn't sync toggles\n");
  1441. goto err_comm;
  1442. }
  1443. sema_init(&priv->cmd.slave_sem, 1);
  1444. }
  1445. return 0;
  1446. err_resource:
  1447. mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
  1448. err_thread:
  1449. flush_workqueue(priv->mfunc.master.comm_wq);
  1450. destroy_workqueue(priv->mfunc.master.comm_wq);
  1451. err_slaves:
  1452. while (--i) {
  1453. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1454. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1455. }
  1456. kfree(priv->mfunc.master.slave_state);
  1457. err_comm:
  1458. iounmap(priv->mfunc.comm);
  1459. err_vhcr:
  1460. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1461. priv->mfunc.vhcr,
  1462. priv->mfunc.vhcr_dma);
  1463. priv->mfunc.vhcr = NULL;
  1464. return -ENOMEM;
  1465. }
  1466. int mlx4_cmd_init(struct mlx4_dev *dev)
  1467. {
  1468. struct mlx4_priv *priv = mlx4_priv(dev);
  1469. mutex_init(&priv->cmd.hcr_mutex);
  1470. sema_init(&priv->cmd.poll_sem, 1);
  1471. priv->cmd.use_events = 0;
  1472. priv->cmd.toggle = 1;
  1473. priv->cmd.hcr = NULL;
  1474. priv->mfunc.vhcr = NULL;
  1475. if (!mlx4_is_slave(dev)) {
  1476. priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
  1477. MLX4_HCR_BASE, MLX4_HCR_SIZE);
  1478. if (!priv->cmd.hcr) {
  1479. mlx4_err(dev, "Couldn't map command register.\n");
  1480. return -ENOMEM;
  1481. }
  1482. }
  1483. priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
  1484. MLX4_MAILBOX_SIZE,
  1485. MLX4_MAILBOX_SIZE, 0);
  1486. if (!priv->cmd.pool)
  1487. goto err_hcr;
  1488. return 0;
  1489. err_hcr:
  1490. if (!mlx4_is_slave(dev))
  1491. iounmap(priv->cmd.hcr);
  1492. return -ENOMEM;
  1493. }
  1494. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  1495. {
  1496. struct mlx4_priv *priv = mlx4_priv(dev);
  1497. int i, port;
  1498. if (mlx4_is_master(dev)) {
  1499. flush_workqueue(priv->mfunc.master.comm_wq);
  1500. destroy_workqueue(priv->mfunc.master.comm_wq);
  1501. for (i = 0; i < dev->num_slaves; i++) {
  1502. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1503. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1504. }
  1505. kfree(priv->mfunc.master.slave_state);
  1506. }
  1507. iounmap(priv->mfunc.comm);
  1508. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1509. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1510. priv->mfunc.vhcr = NULL;
  1511. }
  1512. void mlx4_cmd_cleanup(struct mlx4_dev *dev)
  1513. {
  1514. struct mlx4_priv *priv = mlx4_priv(dev);
  1515. pci_pool_destroy(priv->cmd.pool);
  1516. if (!mlx4_is_slave(dev))
  1517. iounmap(priv->cmd.hcr);
  1518. }
  1519. /*
  1520. * Switch to using events to issue FW commands (can only be called
  1521. * after event queue for command events has been initialized).
  1522. */
  1523. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  1524. {
  1525. struct mlx4_priv *priv = mlx4_priv(dev);
  1526. int i;
  1527. int err = 0;
  1528. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  1529. sizeof (struct mlx4_cmd_context),
  1530. GFP_KERNEL);
  1531. if (!priv->cmd.context)
  1532. return -ENOMEM;
  1533. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  1534. priv->cmd.context[i].token = i;
  1535. priv->cmd.context[i].next = i + 1;
  1536. }
  1537. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  1538. priv->cmd.free_head = 0;
  1539. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  1540. spin_lock_init(&priv->cmd.context_lock);
  1541. for (priv->cmd.token_mask = 1;
  1542. priv->cmd.token_mask < priv->cmd.max_cmds;
  1543. priv->cmd.token_mask <<= 1)
  1544. ; /* nothing */
  1545. --priv->cmd.token_mask;
  1546. down(&priv->cmd.poll_sem);
  1547. priv->cmd.use_events = 1;
  1548. return err;
  1549. }
  1550. /*
  1551. * Switch back to polling (used when shutting down the device)
  1552. */
  1553. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  1554. {
  1555. struct mlx4_priv *priv = mlx4_priv(dev);
  1556. int i;
  1557. priv->cmd.use_events = 0;
  1558. for (i = 0; i < priv->cmd.max_cmds; ++i)
  1559. down(&priv->cmd.event_sem);
  1560. kfree(priv->cmd.context);
  1561. up(&priv->cmd.poll_sem);
  1562. }
  1563. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  1564. {
  1565. struct mlx4_cmd_mailbox *mailbox;
  1566. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  1567. if (!mailbox)
  1568. return ERR_PTR(-ENOMEM);
  1569. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  1570. &mailbox->dma);
  1571. if (!mailbox->buf) {
  1572. kfree(mailbox);
  1573. return ERR_PTR(-ENOMEM);
  1574. }
  1575. return mailbox;
  1576. }
  1577. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  1578. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  1579. struct mlx4_cmd_mailbox *mailbox)
  1580. {
  1581. if (!mailbox)
  1582. return;
  1583. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  1584. kfree(mailbox);
  1585. }
  1586. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  1587. u32 mlx4_comm_get_version(void)
  1588. {
  1589. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  1590. }