cmd.c 43 KB

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