cmd.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  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. unsigned long flags;
  1377. slave_state[slave].comm_toggle ^= 1;
  1378. reply = (u32) slave_state[slave].comm_toggle << 31;
  1379. if (toggle != slave_state[slave].comm_toggle) {
  1380. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER"
  1381. "STATE COMPROMISIED ***\n", toggle, slave);
  1382. goto reset_slave;
  1383. }
  1384. if (cmd == MLX4_COMM_CMD_RESET) {
  1385. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1386. slave_state[slave].active = false;
  1387. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1388. slave_state[slave].event_eq[i].eqn = -1;
  1389. slave_state[slave].event_eq[i].token = 0;
  1390. }
  1391. /*check if we are in the middle of FLR process,
  1392. if so return "retry" status to the slave*/
  1393. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1394. goto inform_slave_state;
  1395. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1396. /* write the version in the event field */
  1397. reply |= mlx4_comm_get_version();
  1398. goto reset_slave;
  1399. }
  1400. /*command from slave in the middle of FLR*/
  1401. if (cmd != MLX4_COMM_CMD_RESET &&
  1402. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1403. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) "
  1404. "in the middle of FLR\n", slave, cmd);
  1405. return;
  1406. }
  1407. switch (cmd) {
  1408. case MLX4_COMM_CMD_VHCR0:
  1409. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1410. goto reset_slave;
  1411. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1412. priv->mfunc.master.slave_state[slave].cookie = 0;
  1413. mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
  1414. break;
  1415. case MLX4_COMM_CMD_VHCR1:
  1416. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1417. goto reset_slave;
  1418. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1419. break;
  1420. case MLX4_COMM_CMD_VHCR2:
  1421. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1422. goto reset_slave;
  1423. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1424. break;
  1425. case MLX4_COMM_CMD_VHCR_EN:
  1426. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1427. goto reset_slave;
  1428. slave_state[slave].vhcr_dma |= param;
  1429. slave_state[slave].active = true;
  1430. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1431. break;
  1432. case MLX4_COMM_CMD_VHCR_POST:
  1433. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1434. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
  1435. goto reset_slave;
  1436. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1437. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1438. mlx4_err(dev, "Failed processing vhcr for slave:%d,"
  1439. " resetting slave.\n", slave);
  1440. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1441. goto reset_slave;
  1442. }
  1443. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1444. break;
  1445. default:
  1446. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1447. goto reset_slave;
  1448. }
  1449. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1450. if (!slave_state[slave].is_slave_going_down)
  1451. slave_state[slave].last_cmd = cmd;
  1452. else
  1453. is_going_down = 1;
  1454. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1455. if (is_going_down) {
  1456. mlx4_warn(dev, "Slave is going down aborting command(%d)"
  1457. " executing from slave:%d\n",
  1458. cmd, slave);
  1459. return;
  1460. }
  1461. __raw_writel((__force u32) cpu_to_be32(reply),
  1462. &priv->mfunc.comm[slave].slave_read);
  1463. mmiowb();
  1464. return;
  1465. reset_slave:
  1466. /* cleanup any slave resources */
  1467. mlx4_delete_all_resources_for_slave(dev, slave);
  1468. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1469. if (!slave_state[slave].is_slave_going_down)
  1470. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  1471. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1472. /*with slave in the middle of flr, no need to clean resources again.*/
  1473. inform_slave_state:
  1474. memset(&slave_state[slave].event_eq, 0,
  1475. sizeof(struct mlx4_slave_event_eq_info));
  1476. __raw_writel((__force u32) cpu_to_be32(reply),
  1477. &priv->mfunc.comm[slave].slave_read);
  1478. wmb();
  1479. }
  1480. /* master command processing */
  1481. void mlx4_master_comm_channel(struct work_struct *work)
  1482. {
  1483. struct mlx4_mfunc_master_ctx *master =
  1484. container_of(work,
  1485. struct mlx4_mfunc_master_ctx,
  1486. comm_work);
  1487. struct mlx4_mfunc *mfunc =
  1488. container_of(master, struct mlx4_mfunc, master);
  1489. struct mlx4_priv *priv =
  1490. container_of(mfunc, struct mlx4_priv, mfunc);
  1491. struct mlx4_dev *dev = &priv->dev;
  1492. __be32 *bit_vec;
  1493. u32 comm_cmd;
  1494. u32 vec;
  1495. int i, j, slave;
  1496. int toggle;
  1497. int served = 0;
  1498. int reported = 0;
  1499. u32 slt;
  1500. bit_vec = master->comm_arm_bit_vector;
  1501. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  1502. vec = be32_to_cpu(bit_vec[i]);
  1503. for (j = 0; j < 32; j++) {
  1504. if (!(vec & (1 << j)))
  1505. continue;
  1506. ++reported;
  1507. slave = (i * 32) + j;
  1508. comm_cmd = swab32(readl(
  1509. &mfunc->comm[slave].slave_write));
  1510. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  1511. >> 31;
  1512. toggle = comm_cmd >> 31;
  1513. if (toggle != slt) {
  1514. if (master->slave_state[slave].comm_toggle
  1515. != slt) {
  1516. printk(KERN_INFO "slave %d out of sync."
  1517. " read toggle %d, state toggle %d. "
  1518. "Resynching.\n", slave, slt,
  1519. master->slave_state[slave].comm_toggle);
  1520. master->slave_state[slave].comm_toggle =
  1521. slt;
  1522. }
  1523. mlx4_master_do_cmd(dev, slave,
  1524. comm_cmd >> 16 & 0xff,
  1525. comm_cmd & 0xffff, toggle);
  1526. ++served;
  1527. }
  1528. }
  1529. }
  1530. if (reported && reported != served)
  1531. mlx4_warn(dev, "Got command event with bitmask from %d slaves"
  1532. " but %d were served\n",
  1533. reported, served);
  1534. if (mlx4_ARM_COMM_CHANNEL(dev))
  1535. mlx4_warn(dev, "Failed to arm comm channel events\n");
  1536. }
  1537. static int sync_toggles(struct mlx4_dev *dev)
  1538. {
  1539. struct mlx4_priv *priv = mlx4_priv(dev);
  1540. int wr_toggle;
  1541. int rd_toggle;
  1542. unsigned long end;
  1543. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
  1544. end = jiffies + msecs_to_jiffies(5000);
  1545. while (time_before(jiffies, end)) {
  1546. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
  1547. if (rd_toggle == wr_toggle) {
  1548. priv->cmd.comm_toggle = rd_toggle;
  1549. return 0;
  1550. }
  1551. cond_resched();
  1552. }
  1553. /*
  1554. * we could reach here if for example the previous VM using this
  1555. * function misbehaved and left the channel with unsynced state. We
  1556. * should fix this here and give this VM a chance to use a properly
  1557. * synced channel
  1558. */
  1559. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  1560. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  1561. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  1562. priv->cmd.comm_toggle = 0;
  1563. return 0;
  1564. }
  1565. int mlx4_multi_func_init(struct mlx4_dev *dev)
  1566. {
  1567. struct mlx4_priv *priv = mlx4_priv(dev);
  1568. struct mlx4_slave_state *s_state;
  1569. int i, j, err, port;
  1570. if (mlx4_is_master(dev))
  1571. priv->mfunc.comm =
  1572. ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
  1573. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  1574. else
  1575. priv->mfunc.comm =
  1576. ioremap(pci_resource_start(dev->pdev, 2) +
  1577. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  1578. if (!priv->mfunc.comm) {
  1579. mlx4_err(dev, "Couldn't map communication vector.\n");
  1580. goto err_vhcr;
  1581. }
  1582. if (mlx4_is_master(dev)) {
  1583. priv->mfunc.master.slave_state =
  1584. kzalloc(dev->num_slaves *
  1585. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  1586. if (!priv->mfunc.master.slave_state)
  1587. goto err_comm;
  1588. for (i = 0; i < dev->num_slaves; ++i) {
  1589. s_state = &priv->mfunc.master.slave_state[i];
  1590. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  1591. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  1592. s_state->event_eq[j].eqn = -1;
  1593. __raw_writel((__force u32) 0,
  1594. &priv->mfunc.comm[i].slave_write);
  1595. __raw_writel((__force u32) 0,
  1596. &priv->mfunc.comm[i].slave_read);
  1597. mmiowb();
  1598. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1599. s_state->vlan_filter[port] =
  1600. kzalloc(sizeof(struct mlx4_vlan_fltr),
  1601. GFP_KERNEL);
  1602. if (!s_state->vlan_filter[port]) {
  1603. if (--port)
  1604. kfree(s_state->vlan_filter[port]);
  1605. goto err_slaves;
  1606. }
  1607. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  1608. }
  1609. spin_lock_init(&s_state->lock);
  1610. }
  1611. memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
  1612. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  1613. INIT_WORK(&priv->mfunc.master.comm_work,
  1614. mlx4_master_comm_channel);
  1615. INIT_WORK(&priv->mfunc.master.slave_event_work,
  1616. mlx4_gen_slave_eqe);
  1617. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  1618. mlx4_master_handle_slave_flr);
  1619. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  1620. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  1621. priv->mfunc.master.comm_wq =
  1622. create_singlethread_workqueue("mlx4_comm");
  1623. if (!priv->mfunc.master.comm_wq)
  1624. goto err_slaves;
  1625. if (mlx4_init_resource_tracker(dev))
  1626. goto err_thread;
  1627. err = mlx4_ARM_COMM_CHANNEL(dev);
  1628. if (err) {
  1629. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  1630. err);
  1631. goto err_resource;
  1632. }
  1633. } else {
  1634. err = sync_toggles(dev);
  1635. if (err) {
  1636. mlx4_err(dev, "Couldn't sync toggles\n");
  1637. goto err_comm;
  1638. }
  1639. }
  1640. return 0;
  1641. err_resource:
  1642. mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
  1643. err_thread:
  1644. flush_workqueue(priv->mfunc.master.comm_wq);
  1645. destroy_workqueue(priv->mfunc.master.comm_wq);
  1646. err_slaves:
  1647. while (--i) {
  1648. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1649. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1650. }
  1651. kfree(priv->mfunc.master.slave_state);
  1652. err_comm:
  1653. iounmap(priv->mfunc.comm);
  1654. err_vhcr:
  1655. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1656. priv->mfunc.vhcr,
  1657. priv->mfunc.vhcr_dma);
  1658. priv->mfunc.vhcr = NULL;
  1659. return -ENOMEM;
  1660. }
  1661. int mlx4_cmd_init(struct mlx4_dev *dev)
  1662. {
  1663. struct mlx4_priv *priv = mlx4_priv(dev);
  1664. mutex_init(&priv->cmd.hcr_mutex);
  1665. mutex_init(&priv->cmd.slave_cmd_mutex);
  1666. sema_init(&priv->cmd.poll_sem, 1);
  1667. priv->cmd.use_events = 0;
  1668. priv->cmd.toggle = 1;
  1669. priv->cmd.hcr = NULL;
  1670. priv->mfunc.vhcr = NULL;
  1671. if (!mlx4_is_slave(dev)) {
  1672. priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
  1673. MLX4_HCR_BASE, MLX4_HCR_SIZE);
  1674. if (!priv->cmd.hcr) {
  1675. mlx4_err(dev, "Couldn't map command register.\n");
  1676. return -ENOMEM;
  1677. }
  1678. }
  1679. if (mlx4_is_mfunc(dev)) {
  1680. priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1681. &priv->mfunc.vhcr_dma,
  1682. GFP_KERNEL);
  1683. if (!priv->mfunc.vhcr) {
  1684. mlx4_err(dev, "Couldn't allocate VHCR.\n");
  1685. goto err_hcr;
  1686. }
  1687. }
  1688. priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
  1689. MLX4_MAILBOX_SIZE,
  1690. MLX4_MAILBOX_SIZE, 0);
  1691. if (!priv->cmd.pool)
  1692. goto err_vhcr;
  1693. return 0;
  1694. err_vhcr:
  1695. if (mlx4_is_mfunc(dev))
  1696. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1697. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1698. priv->mfunc.vhcr = NULL;
  1699. err_hcr:
  1700. if (!mlx4_is_slave(dev))
  1701. iounmap(priv->cmd.hcr);
  1702. return -ENOMEM;
  1703. }
  1704. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  1705. {
  1706. struct mlx4_priv *priv = mlx4_priv(dev);
  1707. int i, port;
  1708. if (mlx4_is_master(dev)) {
  1709. flush_workqueue(priv->mfunc.master.comm_wq);
  1710. destroy_workqueue(priv->mfunc.master.comm_wq);
  1711. for (i = 0; i < dev->num_slaves; i++) {
  1712. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1713. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1714. }
  1715. kfree(priv->mfunc.master.slave_state);
  1716. }
  1717. iounmap(priv->mfunc.comm);
  1718. }
  1719. void mlx4_cmd_cleanup(struct mlx4_dev *dev)
  1720. {
  1721. struct mlx4_priv *priv = mlx4_priv(dev);
  1722. pci_pool_destroy(priv->cmd.pool);
  1723. if (!mlx4_is_slave(dev))
  1724. iounmap(priv->cmd.hcr);
  1725. if (mlx4_is_mfunc(dev))
  1726. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1727. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1728. priv->mfunc.vhcr = NULL;
  1729. }
  1730. /*
  1731. * Switch to using events to issue FW commands (can only be called
  1732. * after event queue for command events has been initialized).
  1733. */
  1734. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  1735. {
  1736. struct mlx4_priv *priv = mlx4_priv(dev);
  1737. int i;
  1738. int err = 0;
  1739. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  1740. sizeof (struct mlx4_cmd_context),
  1741. GFP_KERNEL);
  1742. if (!priv->cmd.context)
  1743. return -ENOMEM;
  1744. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  1745. priv->cmd.context[i].token = i;
  1746. priv->cmd.context[i].next = i + 1;
  1747. }
  1748. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  1749. priv->cmd.free_head = 0;
  1750. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  1751. spin_lock_init(&priv->cmd.context_lock);
  1752. for (priv->cmd.token_mask = 1;
  1753. priv->cmd.token_mask < priv->cmd.max_cmds;
  1754. priv->cmd.token_mask <<= 1)
  1755. ; /* nothing */
  1756. --priv->cmd.token_mask;
  1757. down(&priv->cmd.poll_sem);
  1758. priv->cmd.use_events = 1;
  1759. return err;
  1760. }
  1761. /*
  1762. * Switch back to polling (used when shutting down the device)
  1763. */
  1764. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  1765. {
  1766. struct mlx4_priv *priv = mlx4_priv(dev);
  1767. int i;
  1768. priv->cmd.use_events = 0;
  1769. for (i = 0; i < priv->cmd.max_cmds; ++i)
  1770. down(&priv->cmd.event_sem);
  1771. kfree(priv->cmd.context);
  1772. up(&priv->cmd.poll_sem);
  1773. }
  1774. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  1775. {
  1776. struct mlx4_cmd_mailbox *mailbox;
  1777. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  1778. if (!mailbox)
  1779. return ERR_PTR(-ENOMEM);
  1780. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  1781. &mailbox->dma);
  1782. if (!mailbox->buf) {
  1783. kfree(mailbox);
  1784. return ERR_PTR(-ENOMEM);
  1785. }
  1786. return mailbox;
  1787. }
  1788. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  1789. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  1790. struct mlx4_cmd_mailbox *mailbox)
  1791. {
  1792. if (!mailbox)
  1793. return;
  1794. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  1795. kfree(mailbox);
  1796. }
  1797. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  1798. u32 mlx4_comm_get_version(void)
  1799. {
  1800. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  1801. }