cmd.c 56 KB

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