cmd.c 50 KB

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