cmd.c 45 KB

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