cmd.c 62 KB

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