cmd.c 43 KB

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