cmd.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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. 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. int mlx4_DMA_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. u64 in_param;
  713. u64 out_param;
  714. int err;
  715. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  716. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  717. if (cmd->encode_slave_id) {
  718. in_param &= 0xffffffffffffff00ll;
  719. in_param |= slave;
  720. }
  721. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  722. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  723. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  724. if (cmd->out_is_imm)
  725. vhcr->out_param = out_param;
  726. return err;
  727. }
  728. static struct mlx4_cmd_info cmd_info[] = {
  729. {
  730. .opcode = MLX4_CMD_QUERY_FW,
  731. .has_inbox = false,
  732. .has_outbox = true,
  733. .out_is_imm = false,
  734. .encode_slave_id = false,
  735. .verify = NULL,
  736. .wrapper = mlx4_QUERY_FW_wrapper
  737. },
  738. {
  739. .opcode = MLX4_CMD_QUERY_HCA,
  740. .has_inbox = false,
  741. .has_outbox = true,
  742. .out_is_imm = false,
  743. .encode_slave_id = false,
  744. .verify = NULL,
  745. .wrapper = NULL
  746. },
  747. {
  748. .opcode = MLX4_CMD_QUERY_DEV_CAP,
  749. .has_inbox = false,
  750. .has_outbox = true,
  751. .out_is_imm = false,
  752. .encode_slave_id = false,
  753. .verify = NULL,
  754. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  755. },
  756. {
  757. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  758. .has_inbox = false,
  759. .has_outbox = true,
  760. .out_is_imm = false,
  761. .encode_slave_id = false,
  762. .verify = NULL,
  763. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  764. },
  765. {
  766. .opcode = MLX4_CMD_QUERY_ADAPTER,
  767. .has_inbox = false,
  768. .has_outbox = true,
  769. .out_is_imm = false,
  770. .encode_slave_id = false,
  771. .verify = NULL,
  772. .wrapper = NULL
  773. },
  774. {
  775. .opcode = MLX4_CMD_INIT_PORT,
  776. .has_inbox = false,
  777. .has_outbox = false,
  778. .out_is_imm = false,
  779. .encode_slave_id = false,
  780. .verify = NULL,
  781. .wrapper = mlx4_INIT_PORT_wrapper
  782. },
  783. {
  784. .opcode = MLX4_CMD_CLOSE_PORT,
  785. .has_inbox = false,
  786. .has_outbox = false,
  787. .out_is_imm = false,
  788. .encode_slave_id = false,
  789. .verify = NULL,
  790. .wrapper = mlx4_CLOSE_PORT_wrapper
  791. },
  792. {
  793. .opcode = MLX4_CMD_QUERY_PORT,
  794. .has_inbox = false,
  795. .has_outbox = true,
  796. .out_is_imm = false,
  797. .encode_slave_id = false,
  798. .verify = NULL,
  799. .wrapper = mlx4_QUERY_PORT_wrapper
  800. },
  801. {
  802. .opcode = MLX4_CMD_SET_PORT,
  803. .has_inbox = true,
  804. .has_outbox = false,
  805. .out_is_imm = false,
  806. .encode_slave_id = false,
  807. .verify = NULL,
  808. .wrapper = mlx4_SET_PORT_wrapper
  809. },
  810. {
  811. .opcode = MLX4_CMD_MAP_EQ,
  812. .has_inbox = false,
  813. .has_outbox = false,
  814. .out_is_imm = false,
  815. .encode_slave_id = false,
  816. .verify = NULL,
  817. .wrapper = mlx4_MAP_EQ_wrapper
  818. },
  819. {
  820. .opcode = MLX4_CMD_SW2HW_EQ,
  821. .has_inbox = true,
  822. .has_outbox = false,
  823. .out_is_imm = false,
  824. .encode_slave_id = true,
  825. .verify = NULL,
  826. .wrapper = mlx4_SW2HW_EQ_wrapper
  827. },
  828. {
  829. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  830. .has_inbox = false,
  831. .has_outbox = false,
  832. .out_is_imm = false,
  833. .encode_slave_id = false,
  834. .verify = NULL,
  835. .wrapper = NULL
  836. },
  837. {
  838. .opcode = MLX4_CMD_NOP,
  839. .has_inbox = false,
  840. .has_outbox = false,
  841. .out_is_imm = false,
  842. .encode_slave_id = false,
  843. .verify = NULL,
  844. .wrapper = NULL
  845. },
  846. {
  847. .opcode = MLX4_CMD_ALLOC_RES,
  848. .has_inbox = false,
  849. .has_outbox = false,
  850. .out_is_imm = true,
  851. .encode_slave_id = false,
  852. .verify = NULL,
  853. .wrapper = mlx4_ALLOC_RES_wrapper
  854. },
  855. {
  856. .opcode = MLX4_CMD_FREE_RES,
  857. .has_inbox = false,
  858. .has_outbox = false,
  859. .out_is_imm = false,
  860. .encode_slave_id = false,
  861. .verify = NULL,
  862. .wrapper = mlx4_FREE_RES_wrapper
  863. },
  864. {
  865. .opcode = MLX4_CMD_SW2HW_MPT,
  866. .has_inbox = true,
  867. .has_outbox = false,
  868. .out_is_imm = false,
  869. .encode_slave_id = true,
  870. .verify = NULL,
  871. .wrapper = mlx4_SW2HW_MPT_wrapper
  872. },
  873. {
  874. .opcode = MLX4_CMD_QUERY_MPT,
  875. .has_inbox = false,
  876. .has_outbox = true,
  877. .out_is_imm = false,
  878. .encode_slave_id = false,
  879. .verify = NULL,
  880. .wrapper = mlx4_QUERY_MPT_wrapper
  881. },
  882. {
  883. .opcode = MLX4_CMD_HW2SW_MPT,
  884. .has_inbox = false,
  885. .has_outbox = false,
  886. .out_is_imm = false,
  887. .encode_slave_id = false,
  888. .verify = NULL,
  889. .wrapper = mlx4_HW2SW_MPT_wrapper
  890. },
  891. {
  892. .opcode = MLX4_CMD_READ_MTT,
  893. .has_inbox = false,
  894. .has_outbox = true,
  895. .out_is_imm = false,
  896. .encode_slave_id = false,
  897. .verify = NULL,
  898. .wrapper = NULL
  899. },
  900. {
  901. .opcode = MLX4_CMD_WRITE_MTT,
  902. .has_inbox = true,
  903. .has_outbox = false,
  904. .out_is_imm = false,
  905. .encode_slave_id = false,
  906. .verify = NULL,
  907. .wrapper = mlx4_WRITE_MTT_wrapper
  908. },
  909. {
  910. .opcode = MLX4_CMD_SYNC_TPT,
  911. .has_inbox = true,
  912. .has_outbox = false,
  913. .out_is_imm = false,
  914. .encode_slave_id = false,
  915. .verify = NULL,
  916. .wrapper = NULL
  917. },
  918. {
  919. .opcode = MLX4_CMD_HW2SW_EQ,
  920. .has_inbox = false,
  921. .has_outbox = true,
  922. .out_is_imm = false,
  923. .encode_slave_id = true,
  924. .verify = NULL,
  925. .wrapper = mlx4_HW2SW_EQ_wrapper
  926. },
  927. {
  928. .opcode = MLX4_CMD_QUERY_EQ,
  929. .has_inbox = false,
  930. .has_outbox = true,
  931. .out_is_imm = false,
  932. .encode_slave_id = true,
  933. .verify = NULL,
  934. .wrapper = mlx4_QUERY_EQ_wrapper
  935. },
  936. {
  937. .opcode = MLX4_CMD_SW2HW_CQ,
  938. .has_inbox = true,
  939. .has_outbox = false,
  940. .out_is_imm = false,
  941. .encode_slave_id = true,
  942. .verify = NULL,
  943. .wrapper = mlx4_SW2HW_CQ_wrapper
  944. },
  945. {
  946. .opcode = MLX4_CMD_HW2SW_CQ,
  947. .has_inbox = false,
  948. .has_outbox = false,
  949. .out_is_imm = false,
  950. .encode_slave_id = false,
  951. .verify = NULL,
  952. .wrapper = mlx4_HW2SW_CQ_wrapper
  953. },
  954. {
  955. .opcode = MLX4_CMD_QUERY_CQ,
  956. .has_inbox = false,
  957. .has_outbox = true,
  958. .out_is_imm = false,
  959. .encode_slave_id = false,
  960. .verify = NULL,
  961. .wrapper = mlx4_QUERY_CQ_wrapper
  962. },
  963. {
  964. .opcode = MLX4_CMD_MODIFY_CQ,
  965. .has_inbox = true,
  966. .has_outbox = false,
  967. .out_is_imm = true,
  968. .encode_slave_id = false,
  969. .verify = NULL,
  970. .wrapper = mlx4_MODIFY_CQ_wrapper
  971. },
  972. {
  973. .opcode = MLX4_CMD_SW2HW_SRQ,
  974. .has_inbox = true,
  975. .has_outbox = false,
  976. .out_is_imm = false,
  977. .encode_slave_id = true,
  978. .verify = NULL,
  979. .wrapper = mlx4_SW2HW_SRQ_wrapper
  980. },
  981. {
  982. .opcode = MLX4_CMD_HW2SW_SRQ,
  983. .has_inbox = false,
  984. .has_outbox = false,
  985. .out_is_imm = false,
  986. .encode_slave_id = false,
  987. .verify = NULL,
  988. .wrapper = mlx4_HW2SW_SRQ_wrapper
  989. },
  990. {
  991. .opcode = MLX4_CMD_QUERY_SRQ,
  992. .has_inbox = false,
  993. .has_outbox = true,
  994. .out_is_imm = false,
  995. .encode_slave_id = false,
  996. .verify = NULL,
  997. .wrapper = mlx4_QUERY_SRQ_wrapper
  998. },
  999. {
  1000. .opcode = MLX4_CMD_ARM_SRQ,
  1001. .has_inbox = false,
  1002. .has_outbox = false,
  1003. .out_is_imm = false,
  1004. .encode_slave_id = false,
  1005. .verify = NULL,
  1006. .wrapper = mlx4_ARM_SRQ_wrapper
  1007. },
  1008. {
  1009. .opcode = MLX4_CMD_RST2INIT_QP,
  1010. .has_inbox = true,
  1011. .has_outbox = false,
  1012. .out_is_imm = false,
  1013. .encode_slave_id = true,
  1014. .verify = NULL,
  1015. .wrapper = mlx4_RST2INIT_QP_wrapper
  1016. },
  1017. {
  1018. .opcode = MLX4_CMD_INIT2INIT_QP,
  1019. .has_inbox = true,
  1020. .has_outbox = false,
  1021. .out_is_imm = false,
  1022. .encode_slave_id = false,
  1023. .verify = NULL,
  1024. .wrapper = mlx4_INIT2INIT_QP_wrapper
  1025. },
  1026. {
  1027. .opcode = MLX4_CMD_INIT2RTR_QP,
  1028. .has_inbox = true,
  1029. .has_outbox = false,
  1030. .out_is_imm = false,
  1031. .encode_slave_id = false,
  1032. .verify = NULL,
  1033. .wrapper = mlx4_INIT2RTR_QP_wrapper
  1034. },
  1035. {
  1036. .opcode = MLX4_CMD_RTR2RTS_QP,
  1037. .has_inbox = true,
  1038. .has_outbox = false,
  1039. .out_is_imm = false,
  1040. .encode_slave_id = false,
  1041. .verify = NULL,
  1042. .wrapper = mlx4_RTR2RTS_QP_wrapper
  1043. },
  1044. {
  1045. .opcode = MLX4_CMD_RTS2RTS_QP,
  1046. .has_inbox = true,
  1047. .has_outbox = false,
  1048. .out_is_imm = false,
  1049. .encode_slave_id = false,
  1050. .verify = NULL,
  1051. .wrapper = mlx4_RTS2RTS_QP_wrapper
  1052. },
  1053. {
  1054. .opcode = MLX4_CMD_SQERR2RTS_QP,
  1055. .has_inbox = true,
  1056. .has_outbox = false,
  1057. .out_is_imm = false,
  1058. .encode_slave_id = false,
  1059. .verify = NULL,
  1060. .wrapper = mlx4_SQERR2RTS_QP_wrapper
  1061. },
  1062. {
  1063. .opcode = MLX4_CMD_2ERR_QP,
  1064. .has_inbox = false,
  1065. .has_outbox = false,
  1066. .out_is_imm = false,
  1067. .encode_slave_id = false,
  1068. .verify = NULL,
  1069. .wrapper = mlx4_GEN_QP_wrapper
  1070. },
  1071. {
  1072. .opcode = MLX4_CMD_RTS2SQD_QP,
  1073. .has_inbox = false,
  1074. .has_outbox = false,
  1075. .out_is_imm = false,
  1076. .encode_slave_id = false,
  1077. .verify = NULL,
  1078. .wrapper = mlx4_GEN_QP_wrapper
  1079. },
  1080. {
  1081. .opcode = MLX4_CMD_SQD2SQD_QP,
  1082. .has_inbox = true,
  1083. .has_outbox = false,
  1084. .out_is_imm = false,
  1085. .encode_slave_id = false,
  1086. .verify = NULL,
  1087. .wrapper = mlx4_SQD2SQD_QP_wrapper
  1088. },
  1089. {
  1090. .opcode = MLX4_CMD_SQD2RTS_QP,
  1091. .has_inbox = true,
  1092. .has_outbox = false,
  1093. .out_is_imm = false,
  1094. .encode_slave_id = false,
  1095. .verify = NULL,
  1096. .wrapper = mlx4_SQD2RTS_QP_wrapper
  1097. },
  1098. {
  1099. .opcode = MLX4_CMD_2RST_QP,
  1100. .has_inbox = false,
  1101. .has_outbox = false,
  1102. .out_is_imm = false,
  1103. .encode_slave_id = false,
  1104. .verify = NULL,
  1105. .wrapper = mlx4_2RST_QP_wrapper
  1106. },
  1107. {
  1108. .opcode = MLX4_CMD_QUERY_QP,
  1109. .has_inbox = false,
  1110. .has_outbox = true,
  1111. .out_is_imm = false,
  1112. .encode_slave_id = false,
  1113. .verify = NULL,
  1114. .wrapper = mlx4_GEN_QP_wrapper
  1115. },
  1116. {
  1117. .opcode = MLX4_CMD_SUSPEND_QP,
  1118. .has_inbox = false,
  1119. .has_outbox = false,
  1120. .out_is_imm = false,
  1121. .encode_slave_id = false,
  1122. .verify = NULL,
  1123. .wrapper = mlx4_GEN_QP_wrapper
  1124. },
  1125. {
  1126. .opcode = MLX4_CMD_UNSUSPEND_QP,
  1127. .has_inbox = false,
  1128. .has_outbox = false,
  1129. .out_is_imm = false,
  1130. .encode_slave_id = false,
  1131. .verify = NULL,
  1132. .wrapper = mlx4_GEN_QP_wrapper
  1133. },
  1134. {
  1135. .opcode = MLX4_CMD_UPDATE_QP,
  1136. .has_inbox = false,
  1137. .has_outbox = false,
  1138. .out_is_imm = false,
  1139. .encode_slave_id = false,
  1140. .verify = NULL,
  1141. .wrapper = MLX4_CMD_UPDATE_QP_wrapper
  1142. },
  1143. {
  1144. .opcode = MLX4_CMD_CONF_SPECIAL_QP,
  1145. .has_inbox = false,
  1146. .has_outbox = false,
  1147. .out_is_imm = false,
  1148. .encode_slave_id = false,
  1149. .verify = NULL, /* XXX verify: only demux can do this */
  1150. .wrapper = NULL
  1151. },
  1152. {
  1153. .opcode = MLX4_CMD_MAD_IFC,
  1154. .has_inbox = true,
  1155. .has_outbox = true,
  1156. .out_is_imm = false,
  1157. .encode_slave_id = false,
  1158. .verify = NULL,
  1159. .wrapper = mlx4_MAD_IFC_wrapper
  1160. },
  1161. {
  1162. .opcode = MLX4_CMD_QUERY_IF_STAT,
  1163. .has_inbox = false,
  1164. .has_outbox = true,
  1165. .out_is_imm = false,
  1166. .encode_slave_id = false,
  1167. .verify = NULL,
  1168. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  1169. },
  1170. /* Native multicast commands are not available for guests */
  1171. {
  1172. .opcode = MLX4_CMD_QP_ATTACH,
  1173. .has_inbox = true,
  1174. .has_outbox = false,
  1175. .out_is_imm = false,
  1176. .encode_slave_id = false,
  1177. .verify = NULL,
  1178. .wrapper = mlx4_QP_ATTACH_wrapper
  1179. },
  1180. {
  1181. .opcode = MLX4_CMD_PROMISC,
  1182. .has_inbox = false,
  1183. .has_outbox = false,
  1184. .out_is_imm = false,
  1185. .encode_slave_id = false,
  1186. .verify = NULL,
  1187. .wrapper = mlx4_PROMISC_wrapper
  1188. },
  1189. /* Ethernet specific commands */
  1190. {
  1191. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  1192. .has_inbox = true,
  1193. .has_outbox = false,
  1194. .out_is_imm = false,
  1195. .encode_slave_id = false,
  1196. .verify = NULL,
  1197. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  1198. },
  1199. {
  1200. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  1201. .has_inbox = false,
  1202. .has_outbox = false,
  1203. .out_is_imm = false,
  1204. .encode_slave_id = false,
  1205. .verify = NULL,
  1206. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  1207. },
  1208. {
  1209. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  1210. .has_inbox = false,
  1211. .has_outbox = true,
  1212. .out_is_imm = false,
  1213. .encode_slave_id = false,
  1214. .verify = NULL,
  1215. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  1216. },
  1217. {
  1218. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  1219. .has_inbox = false,
  1220. .has_outbox = false,
  1221. .out_is_imm = false,
  1222. .encode_slave_id = false,
  1223. .verify = NULL,
  1224. .wrapper = NULL
  1225. },
  1226. /* flow steering commands */
  1227. {
  1228. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1229. .has_inbox = true,
  1230. .has_outbox = false,
  1231. .out_is_imm = true,
  1232. .encode_slave_id = false,
  1233. .verify = NULL,
  1234. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1235. },
  1236. {
  1237. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1238. .has_inbox = false,
  1239. .has_outbox = false,
  1240. .out_is_imm = false,
  1241. .encode_slave_id = false,
  1242. .verify = NULL,
  1243. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1244. },
  1245. };
  1246. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1247. struct mlx4_vhcr_cmd *in_vhcr)
  1248. {
  1249. struct mlx4_priv *priv = mlx4_priv(dev);
  1250. struct mlx4_cmd_info *cmd = NULL;
  1251. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1252. struct mlx4_vhcr *vhcr;
  1253. struct mlx4_cmd_mailbox *inbox = NULL;
  1254. struct mlx4_cmd_mailbox *outbox = NULL;
  1255. u64 in_param;
  1256. u64 out_param;
  1257. int ret = 0;
  1258. int i;
  1259. int err = 0;
  1260. /* Create sw representation of Virtual HCR */
  1261. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1262. if (!vhcr)
  1263. return -ENOMEM;
  1264. /* DMA in the vHCR */
  1265. if (!in_vhcr) {
  1266. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1267. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1268. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1269. MLX4_ACCESS_MEM_ALIGN), 1);
  1270. if (ret) {
  1271. mlx4_err(dev, "%s:Failed reading vhcr"
  1272. "ret: 0x%x\n", __func__, ret);
  1273. kfree(vhcr);
  1274. return ret;
  1275. }
  1276. }
  1277. /* Fill SW VHCR fields */
  1278. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1279. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1280. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1281. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1282. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1283. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1284. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1285. /* Lookup command */
  1286. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1287. if (vhcr->op == cmd_info[i].opcode) {
  1288. cmd = &cmd_info[i];
  1289. break;
  1290. }
  1291. }
  1292. if (!cmd) {
  1293. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1294. vhcr->op, slave);
  1295. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1296. goto out_status;
  1297. }
  1298. /* Read inbox */
  1299. if (cmd->has_inbox) {
  1300. vhcr->in_param &= INBOX_MASK;
  1301. inbox = mlx4_alloc_cmd_mailbox(dev);
  1302. if (IS_ERR(inbox)) {
  1303. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1304. inbox = NULL;
  1305. goto out_status;
  1306. }
  1307. if (mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1308. vhcr->in_param,
  1309. MLX4_MAILBOX_SIZE, 1)) {
  1310. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1311. __func__, cmd->opcode);
  1312. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1313. goto out_status;
  1314. }
  1315. }
  1316. /* Apply permission and bound checks if applicable */
  1317. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1318. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection "
  1319. "checks for resource_id:%d\n", vhcr->op, slave,
  1320. vhcr->in_modifier);
  1321. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1322. goto out_status;
  1323. }
  1324. /* Allocate outbox */
  1325. if (cmd->has_outbox) {
  1326. outbox = mlx4_alloc_cmd_mailbox(dev);
  1327. if (IS_ERR(outbox)) {
  1328. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1329. outbox = NULL;
  1330. goto out_status;
  1331. }
  1332. }
  1333. /* Execute the command! */
  1334. if (cmd->wrapper) {
  1335. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1336. cmd);
  1337. if (cmd->out_is_imm)
  1338. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1339. } else {
  1340. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1341. vhcr->in_param;
  1342. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1343. vhcr->out_param;
  1344. err = __mlx4_cmd(dev, in_param, &out_param,
  1345. cmd->out_is_imm, vhcr->in_modifier,
  1346. vhcr->op_modifier, vhcr->op,
  1347. MLX4_CMD_TIME_CLASS_A,
  1348. MLX4_CMD_NATIVE);
  1349. if (cmd->out_is_imm) {
  1350. vhcr->out_param = out_param;
  1351. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1352. }
  1353. }
  1354. if (err) {
  1355. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with"
  1356. " error:%d, status %d\n",
  1357. vhcr->op, slave, vhcr->errno, err);
  1358. vhcr_cmd->status = mlx4_errno_to_status(err);
  1359. goto out_status;
  1360. }
  1361. /* Write outbox if command completed successfully */
  1362. if (cmd->has_outbox && !vhcr_cmd->status) {
  1363. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1364. vhcr->out_param,
  1365. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1366. if (ret) {
  1367. /* If we failed to write back the outbox after the
  1368. *command was successfully executed, we must fail this
  1369. * slave, as it is now in undefined state */
  1370. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1371. goto out;
  1372. }
  1373. }
  1374. out_status:
  1375. /* DMA back vhcr result */
  1376. if (!in_vhcr) {
  1377. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1378. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1379. ALIGN(sizeof(struct mlx4_vhcr),
  1380. MLX4_ACCESS_MEM_ALIGN),
  1381. MLX4_CMD_WRAPPED);
  1382. if (ret)
  1383. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1384. __func__);
  1385. else if (vhcr->e_bit &&
  1386. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1387. mlx4_warn(dev, "Failed to generate command completion "
  1388. "eqe for slave %d\n", slave);
  1389. }
  1390. out:
  1391. kfree(vhcr);
  1392. mlx4_free_cmd_mailbox(dev, inbox);
  1393. mlx4_free_cmd_mailbox(dev, outbox);
  1394. return ret;
  1395. }
  1396. static int calculate_transition(u16 oper_vlan, u16 admin_vlan)
  1397. {
  1398. return (2 * (oper_vlan == MLX4_VGT) + (admin_vlan == MLX4_VGT));
  1399. }
  1400. int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
  1401. int slave, int port)
  1402. {
  1403. struct mlx4_vport_oper_state *vp_oper;
  1404. struct mlx4_vport_state *vp_admin;
  1405. struct mlx4_vf_immed_vlan_work *work;
  1406. struct mlx4_dev *dev = &(priv->dev);
  1407. int err;
  1408. int admin_vlan_ix = NO_INDX;
  1409. enum mlx4_vlan_transition vlan_trans;
  1410. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1411. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1412. if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
  1413. vp_oper->state.default_qos == vp_admin->default_qos &&
  1414. vp_oper->state.link_state == vp_admin->link_state)
  1415. return 0;
  1416. vlan_trans = calculate_transition(vp_oper->state.default_vlan,
  1417. vp_admin->default_vlan);
  1418. if (!(priv->mfunc.master.slave_state[slave].active &&
  1419. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP &&
  1420. vlan_trans == MLX4_VLAN_TRANSITION_VST_VST)) {
  1421. /* even if the UPDATE_QP command isn't supported, we still want
  1422. * to set this VF link according to the admin directive
  1423. */
  1424. vp_oper->state.link_state = vp_admin->link_state;
  1425. return -1;
  1426. }
  1427. mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
  1428. slave, port);
  1429. mlx4_dbg(dev, "vlan %d QoS %d link down %d\n", vp_admin->default_vlan,
  1430. vp_admin->default_qos, vp_admin->link_state);
  1431. work = kzalloc(sizeof(*work), GFP_KERNEL);
  1432. if (!work)
  1433. return -ENOMEM;
  1434. if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
  1435. err = __mlx4_register_vlan(&priv->dev, port,
  1436. vp_admin->default_vlan,
  1437. &admin_vlan_ix);
  1438. if (err) {
  1439. kfree(work);
  1440. mlx4_warn((&priv->dev),
  1441. "No vlan resources slave %d, port %d\n",
  1442. slave, port);
  1443. return err;
  1444. }
  1445. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
  1446. mlx4_dbg((&(priv->dev)),
  1447. "alloc vlan %d idx %d slave %d port %d\n",
  1448. (int)(vp_admin->default_vlan),
  1449. admin_vlan_ix, slave, port);
  1450. }
  1451. /* save original vlan ix and vlan id */
  1452. work->orig_vlan_id = vp_oper->state.default_vlan;
  1453. work->orig_vlan_ix = vp_oper->vlan_idx;
  1454. /* handle new qos */
  1455. if (vp_oper->state.default_qos != vp_admin->default_qos)
  1456. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
  1457. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
  1458. vp_oper->vlan_idx = admin_vlan_ix;
  1459. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1460. vp_oper->state.default_qos = vp_admin->default_qos;
  1461. vp_oper->state.link_state = vp_admin->link_state;
  1462. if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
  1463. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
  1464. /* iterate over QPs owned by this slave, using UPDATE_QP */
  1465. work->port = port;
  1466. work->slave = slave;
  1467. work->qos = vp_oper->state.default_qos;
  1468. work->vlan_id = vp_oper->state.default_vlan;
  1469. work->vlan_ix = vp_oper->vlan_idx;
  1470. work->priv = priv;
  1471. INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
  1472. queue_work(priv->mfunc.master.comm_wq, &work->work);
  1473. return 0;
  1474. }
  1475. static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
  1476. {
  1477. int port, err;
  1478. struct mlx4_vport_state *vp_admin;
  1479. struct mlx4_vport_oper_state *vp_oper;
  1480. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1481. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1482. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1483. vp_oper->state = *vp_admin;
  1484. if (MLX4_VGT != vp_admin->default_vlan) {
  1485. err = __mlx4_register_vlan(&priv->dev, port,
  1486. vp_admin->default_vlan, &(vp_oper->vlan_idx));
  1487. if (err) {
  1488. vp_oper->vlan_idx = NO_INDX;
  1489. mlx4_warn((&priv->dev),
  1490. "No vlan resorces slave %d, port %d\n",
  1491. slave, port);
  1492. return err;
  1493. }
  1494. mlx4_dbg((&(priv->dev)), "alloc vlan %d idx %d slave %d port %d\n",
  1495. (int)(vp_oper->state.default_vlan),
  1496. vp_oper->vlan_idx, slave, port);
  1497. }
  1498. if (vp_admin->spoofchk) {
  1499. vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
  1500. port,
  1501. vp_admin->mac);
  1502. if (0 > vp_oper->mac_idx) {
  1503. err = vp_oper->mac_idx;
  1504. vp_oper->mac_idx = NO_INDX;
  1505. mlx4_warn((&priv->dev),
  1506. "No mac resorces slave %d, port %d\n",
  1507. slave, port);
  1508. return err;
  1509. }
  1510. mlx4_dbg((&(priv->dev)), "alloc mac %llx idx %d slave %d port %d\n",
  1511. vp_oper->state.mac, vp_oper->mac_idx, slave, port);
  1512. }
  1513. }
  1514. return 0;
  1515. }
  1516. static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
  1517. {
  1518. int port;
  1519. struct mlx4_vport_oper_state *vp_oper;
  1520. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1521. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1522. if (NO_INDX != vp_oper->vlan_idx) {
  1523. __mlx4_unregister_vlan(&priv->dev,
  1524. port, vp_oper->vlan_idx);
  1525. vp_oper->vlan_idx = NO_INDX;
  1526. }
  1527. if (NO_INDX != vp_oper->mac_idx) {
  1528. __mlx4_unregister_mac(&priv->dev, port, vp_oper->mac_idx);
  1529. vp_oper->mac_idx = NO_INDX;
  1530. }
  1531. }
  1532. return;
  1533. }
  1534. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1535. u16 param, u8 toggle)
  1536. {
  1537. struct mlx4_priv *priv = mlx4_priv(dev);
  1538. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1539. u32 reply;
  1540. u8 is_going_down = 0;
  1541. int i;
  1542. unsigned long flags;
  1543. slave_state[slave].comm_toggle ^= 1;
  1544. reply = (u32) slave_state[slave].comm_toggle << 31;
  1545. if (toggle != slave_state[slave].comm_toggle) {
  1546. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER"
  1547. "STATE COMPROMISIED ***\n", toggle, slave);
  1548. goto reset_slave;
  1549. }
  1550. if (cmd == MLX4_COMM_CMD_RESET) {
  1551. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1552. slave_state[slave].active = false;
  1553. mlx4_master_deactivate_admin_state(priv, slave);
  1554. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1555. slave_state[slave].event_eq[i].eqn = -1;
  1556. slave_state[slave].event_eq[i].token = 0;
  1557. }
  1558. /*check if we are in the middle of FLR process,
  1559. if so return "retry" status to the slave*/
  1560. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1561. goto inform_slave_state;
  1562. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1563. /* write the version in the event field */
  1564. reply |= mlx4_comm_get_version();
  1565. goto reset_slave;
  1566. }
  1567. /*command from slave in the middle of FLR*/
  1568. if (cmd != MLX4_COMM_CMD_RESET &&
  1569. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1570. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) "
  1571. "in the middle of FLR\n", slave, cmd);
  1572. return;
  1573. }
  1574. switch (cmd) {
  1575. case MLX4_COMM_CMD_VHCR0:
  1576. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1577. goto reset_slave;
  1578. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1579. priv->mfunc.master.slave_state[slave].cookie = 0;
  1580. mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
  1581. break;
  1582. case MLX4_COMM_CMD_VHCR1:
  1583. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1584. goto reset_slave;
  1585. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1586. break;
  1587. case MLX4_COMM_CMD_VHCR2:
  1588. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1589. goto reset_slave;
  1590. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1591. break;
  1592. case MLX4_COMM_CMD_VHCR_EN:
  1593. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1594. goto reset_slave;
  1595. slave_state[slave].vhcr_dma |= param;
  1596. if (mlx4_master_activate_admin_state(priv, slave))
  1597. goto reset_slave;
  1598. slave_state[slave].active = true;
  1599. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1600. break;
  1601. case MLX4_COMM_CMD_VHCR_POST:
  1602. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1603. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
  1604. goto reset_slave;
  1605. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1606. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1607. mlx4_err(dev, "Failed processing vhcr for slave:%d,"
  1608. " resetting slave.\n", slave);
  1609. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1610. goto reset_slave;
  1611. }
  1612. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1613. break;
  1614. default:
  1615. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1616. goto reset_slave;
  1617. }
  1618. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1619. if (!slave_state[slave].is_slave_going_down)
  1620. slave_state[slave].last_cmd = cmd;
  1621. else
  1622. is_going_down = 1;
  1623. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1624. if (is_going_down) {
  1625. mlx4_warn(dev, "Slave is going down aborting command(%d)"
  1626. " executing from slave:%d\n",
  1627. cmd, slave);
  1628. return;
  1629. }
  1630. __raw_writel((__force u32) cpu_to_be32(reply),
  1631. &priv->mfunc.comm[slave].slave_read);
  1632. mmiowb();
  1633. return;
  1634. reset_slave:
  1635. /* cleanup any slave resources */
  1636. mlx4_delete_all_resources_for_slave(dev, slave);
  1637. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1638. if (!slave_state[slave].is_slave_going_down)
  1639. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  1640. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1641. /*with slave in the middle of flr, no need to clean resources again.*/
  1642. inform_slave_state:
  1643. memset(&slave_state[slave].event_eq, 0,
  1644. sizeof(struct mlx4_slave_event_eq_info));
  1645. __raw_writel((__force u32) cpu_to_be32(reply),
  1646. &priv->mfunc.comm[slave].slave_read);
  1647. wmb();
  1648. }
  1649. /* master command processing */
  1650. void mlx4_master_comm_channel(struct work_struct *work)
  1651. {
  1652. struct mlx4_mfunc_master_ctx *master =
  1653. container_of(work,
  1654. struct mlx4_mfunc_master_ctx,
  1655. comm_work);
  1656. struct mlx4_mfunc *mfunc =
  1657. container_of(master, struct mlx4_mfunc, master);
  1658. struct mlx4_priv *priv =
  1659. container_of(mfunc, struct mlx4_priv, mfunc);
  1660. struct mlx4_dev *dev = &priv->dev;
  1661. __be32 *bit_vec;
  1662. u32 comm_cmd;
  1663. u32 vec;
  1664. int i, j, slave;
  1665. int toggle;
  1666. int served = 0;
  1667. int reported = 0;
  1668. u32 slt;
  1669. bit_vec = master->comm_arm_bit_vector;
  1670. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  1671. vec = be32_to_cpu(bit_vec[i]);
  1672. for (j = 0; j < 32; j++) {
  1673. if (!(vec & (1 << j)))
  1674. continue;
  1675. ++reported;
  1676. slave = (i * 32) + j;
  1677. comm_cmd = swab32(readl(
  1678. &mfunc->comm[slave].slave_write));
  1679. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  1680. >> 31;
  1681. toggle = comm_cmd >> 31;
  1682. if (toggle != slt) {
  1683. if (master->slave_state[slave].comm_toggle
  1684. != slt) {
  1685. printk(KERN_INFO "slave %d out of sync."
  1686. " read toggle %d, state toggle %d. "
  1687. "Resynching.\n", slave, slt,
  1688. master->slave_state[slave].comm_toggle);
  1689. master->slave_state[slave].comm_toggle =
  1690. slt;
  1691. }
  1692. mlx4_master_do_cmd(dev, slave,
  1693. comm_cmd >> 16 & 0xff,
  1694. comm_cmd & 0xffff, toggle);
  1695. ++served;
  1696. }
  1697. }
  1698. }
  1699. if (reported && reported != served)
  1700. mlx4_warn(dev, "Got command event with bitmask from %d slaves"
  1701. " but %d were served\n",
  1702. reported, served);
  1703. if (mlx4_ARM_COMM_CHANNEL(dev))
  1704. mlx4_warn(dev, "Failed to arm comm channel events\n");
  1705. }
  1706. static int sync_toggles(struct mlx4_dev *dev)
  1707. {
  1708. struct mlx4_priv *priv = mlx4_priv(dev);
  1709. int wr_toggle;
  1710. int rd_toggle;
  1711. unsigned long end;
  1712. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
  1713. end = jiffies + msecs_to_jiffies(5000);
  1714. while (time_before(jiffies, end)) {
  1715. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
  1716. if (rd_toggle == wr_toggle) {
  1717. priv->cmd.comm_toggle = rd_toggle;
  1718. return 0;
  1719. }
  1720. cond_resched();
  1721. }
  1722. /*
  1723. * we could reach here if for example the previous VM using this
  1724. * function misbehaved and left the channel with unsynced state. We
  1725. * should fix this here and give this VM a chance to use a properly
  1726. * synced channel
  1727. */
  1728. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  1729. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  1730. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  1731. priv->cmd.comm_toggle = 0;
  1732. return 0;
  1733. }
  1734. int mlx4_multi_func_init(struct mlx4_dev *dev)
  1735. {
  1736. struct mlx4_priv *priv = mlx4_priv(dev);
  1737. struct mlx4_slave_state *s_state;
  1738. int i, j, err, port;
  1739. if (mlx4_is_master(dev))
  1740. priv->mfunc.comm =
  1741. ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
  1742. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  1743. else
  1744. priv->mfunc.comm =
  1745. ioremap(pci_resource_start(dev->pdev, 2) +
  1746. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  1747. if (!priv->mfunc.comm) {
  1748. mlx4_err(dev, "Couldn't map communication vector.\n");
  1749. goto err_vhcr;
  1750. }
  1751. if (mlx4_is_master(dev)) {
  1752. priv->mfunc.master.slave_state =
  1753. kzalloc(dev->num_slaves *
  1754. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  1755. if (!priv->mfunc.master.slave_state)
  1756. goto err_comm;
  1757. priv->mfunc.master.vf_admin =
  1758. kzalloc(dev->num_slaves *
  1759. sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
  1760. if (!priv->mfunc.master.vf_admin)
  1761. goto err_comm_admin;
  1762. priv->mfunc.master.vf_oper =
  1763. kzalloc(dev->num_slaves *
  1764. sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
  1765. if (!priv->mfunc.master.vf_oper)
  1766. goto err_comm_oper;
  1767. for (i = 0; i < dev->num_slaves; ++i) {
  1768. s_state = &priv->mfunc.master.slave_state[i];
  1769. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  1770. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  1771. s_state->event_eq[j].eqn = -1;
  1772. __raw_writel((__force u32) 0,
  1773. &priv->mfunc.comm[i].slave_write);
  1774. __raw_writel((__force u32) 0,
  1775. &priv->mfunc.comm[i].slave_read);
  1776. mmiowb();
  1777. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1778. s_state->vlan_filter[port] =
  1779. kzalloc(sizeof(struct mlx4_vlan_fltr),
  1780. GFP_KERNEL);
  1781. if (!s_state->vlan_filter[port]) {
  1782. if (--port)
  1783. kfree(s_state->vlan_filter[port]);
  1784. goto err_slaves;
  1785. }
  1786. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  1787. priv->mfunc.master.vf_admin[i].vport[port].default_vlan = MLX4_VGT;
  1788. priv->mfunc.master.vf_oper[i].vport[port].state.default_vlan = MLX4_VGT;
  1789. priv->mfunc.master.vf_oper[i].vport[port].vlan_idx = NO_INDX;
  1790. priv->mfunc.master.vf_oper[i].vport[port].mac_idx = NO_INDX;
  1791. }
  1792. spin_lock_init(&s_state->lock);
  1793. }
  1794. memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
  1795. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  1796. INIT_WORK(&priv->mfunc.master.comm_work,
  1797. mlx4_master_comm_channel);
  1798. INIT_WORK(&priv->mfunc.master.slave_event_work,
  1799. mlx4_gen_slave_eqe);
  1800. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  1801. mlx4_master_handle_slave_flr);
  1802. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  1803. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  1804. priv->mfunc.master.comm_wq =
  1805. create_singlethread_workqueue("mlx4_comm");
  1806. if (!priv->mfunc.master.comm_wq)
  1807. goto err_slaves;
  1808. if (mlx4_init_resource_tracker(dev))
  1809. goto err_thread;
  1810. err = mlx4_ARM_COMM_CHANNEL(dev);
  1811. if (err) {
  1812. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  1813. err);
  1814. goto err_resource;
  1815. }
  1816. } else {
  1817. err = sync_toggles(dev);
  1818. if (err) {
  1819. mlx4_err(dev, "Couldn't sync toggles\n");
  1820. goto err_comm;
  1821. }
  1822. }
  1823. return 0;
  1824. err_resource:
  1825. mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
  1826. err_thread:
  1827. flush_workqueue(priv->mfunc.master.comm_wq);
  1828. destroy_workqueue(priv->mfunc.master.comm_wq);
  1829. err_slaves:
  1830. while (--i) {
  1831. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1832. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1833. }
  1834. kfree(priv->mfunc.master.vf_oper);
  1835. err_comm_oper:
  1836. kfree(priv->mfunc.master.vf_admin);
  1837. err_comm_admin:
  1838. kfree(priv->mfunc.master.slave_state);
  1839. err_comm:
  1840. iounmap(priv->mfunc.comm);
  1841. err_vhcr:
  1842. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1843. priv->mfunc.vhcr,
  1844. priv->mfunc.vhcr_dma);
  1845. priv->mfunc.vhcr = NULL;
  1846. return -ENOMEM;
  1847. }
  1848. int mlx4_cmd_init(struct mlx4_dev *dev)
  1849. {
  1850. struct mlx4_priv *priv = mlx4_priv(dev);
  1851. mutex_init(&priv->cmd.hcr_mutex);
  1852. mutex_init(&priv->cmd.slave_cmd_mutex);
  1853. sema_init(&priv->cmd.poll_sem, 1);
  1854. priv->cmd.use_events = 0;
  1855. priv->cmd.toggle = 1;
  1856. priv->cmd.hcr = NULL;
  1857. priv->mfunc.vhcr = NULL;
  1858. if (!mlx4_is_slave(dev)) {
  1859. priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
  1860. MLX4_HCR_BASE, MLX4_HCR_SIZE);
  1861. if (!priv->cmd.hcr) {
  1862. mlx4_err(dev, "Couldn't map command register.\n");
  1863. return -ENOMEM;
  1864. }
  1865. }
  1866. if (mlx4_is_mfunc(dev)) {
  1867. priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1868. &priv->mfunc.vhcr_dma,
  1869. GFP_KERNEL);
  1870. if (!priv->mfunc.vhcr)
  1871. goto err_hcr;
  1872. }
  1873. priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
  1874. MLX4_MAILBOX_SIZE,
  1875. MLX4_MAILBOX_SIZE, 0);
  1876. if (!priv->cmd.pool)
  1877. goto err_vhcr;
  1878. return 0;
  1879. err_vhcr:
  1880. if (mlx4_is_mfunc(dev))
  1881. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1882. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1883. priv->mfunc.vhcr = NULL;
  1884. err_hcr:
  1885. if (!mlx4_is_slave(dev))
  1886. iounmap(priv->cmd.hcr);
  1887. return -ENOMEM;
  1888. }
  1889. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  1890. {
  1891. struct mlx4_priv *priv = mlx4_priv(dev);
  1892. int i, port;
  1893. if (mlx4_is_master(dev)) {
  1894. flush_workqueue(priv->mfunc.master.comm_wq);
  1895. destroy_workqueue(priv->mfunc.master.comm_wq);
  1896. for (i = 0; i < dev->num_slaves; i++) {
  1897. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1898. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1899. }
  1900. kfree(priv->mfunc.master.slave_state);
  1901. kfree(priv->mfunc.master.vf_admin);
  1902. kfree(priv->mfunc.master.vf_oper);
  1903. }
  1904. iounmap(priv->mfunc.comm);
  1905. }
  1906. void mlx4_cmd_cleanup(struct mlx4_dev *dev)
  1907. {
  1908. struct mlx4_priv *priv = mlx4_priv(dev);
  1909. pci_pool_destroy(priv->cmd.pool);
  1910. if (!mlx4_is_slave(dev))
  1911. iounmap(priv->cmd.hcr);
  1912. if (mlx4_is_mfunc(dev))
  1913. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1914. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  1915. priv->mfunc.vhcr = NULL;
  1916. }
  1917. /*
  1918. * Switch to using events to issue FW commands (can only be called
  1919. * after event queue for command events has been initialized).
  1920. */
  1921. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  1922. {
  1923. struct mlx4_priv *priv = mlx4_priv(dev);
  1924. int i;
  1925. int err = 0;
  1926. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  1927. sizeof (struct mlx4_cmd_context),
  1928. GFP_KERNEL);
  1929. if (!priv->cmd.context)
  1930. return -ENOMEM;
  1931. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  1932. priv->cmd.context[i].token = i;
  1933. priv->cmd.context[i].next = i + 1;
  1934. }
  1935. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  1936. priv->cmd.free_head = 0;
  1937. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  1938. spin_lock_init(&priv->cmd.context_lock);
  1939. for (priv->cmd.token_mask = 1;
  1940. priv->cmd.token_mask < priv->cmd.max_cmds;
  1941. priv->cmd.token_mask <<= 1)
  1942. ; /* nothing */
  1943. --priv->cmd.token_mask;
  1944. down(&priv->cmd.poll_sem);
  1945. priv->cmd.use_events = 1;
  1946. return err;
  1947. }
  1948. /*
  1949. * Switch back to polling (used when shutting down the device)
  1950. */
  1951. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  1952. {
  1953. struct mlx4_priv *priv = mlx4_priv(dev);
  1954. int i;
  1955. priv->cmd.use_events = 0;
  1956. for (i = 0; i < priv->cmd.max_cmds; ++i)
  1957. down(&priv->cmd.event_sem);
  1958. kfree(priv->cmd.context);
  1959. up(&priv->cmd.poll_sem);
  1960. }
  1961. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  1962. {
  1963. struct mlx4_cmd_mailbox *mailbox;
  1964. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  1965. if (!mailbox)
  1966. return ERR_PTR(-ENOMEM);
  1967. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  1968. &mailbox->dma);
  1969. if (!mailbox->buf) {
  1970. kfree(mailbox);
  1971. return ERR_PTR(-ENOMEM);
  1972. }
  1973. return mailbox;
  1974. }
  1975. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  1976. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  1977. struct mlx4_cmd_mailbox *mailbox)
  1978. {
  1979. if (!mailbox)
  1980. return;
  1981. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  1982. kfree(mailbox);
  1983. }
  1984. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  1985. u32 mlx4_comm_get_version(void)
  1986. {
  1987. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  1988. }
  1989. static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
  1990. {
  1991. if ((vf < 0) || (vf >= dev->num_vfs)) {
  1992. mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n", vf, dev->num_vfs);
  1993. return -EINVAL;
  1994. }
  1995. return vf+1;
  1996. }
  1997. int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
  1998. {
  1999. struct mlx4_priv *priv = mlx4_priv(dev);
  2000. struct mlx4_vport_state *s_info;
  2001. int slave;
  2002. if (!mlx4_is_master(dev))
  2003. return -EPROTONOSUPPORT;
  2004. slave = mlx4_get_slave_indx(dev, vf);
  2005. if (slave < 0)
  2006. return -EINVAL;
  2007. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2008. s_info->mac = mac;
  2009. mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
  2010. vf, port, s_info->mac);
  2011. return 0;
  2012. }
  2013. EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
  2014. int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
  2015. {
  2016. struct mlx4_priv *priv = mlx4_priv(dev);
  2017. struct mlx4_vport_oper_state *vf_oper;
  2018. struct mlx4_vport_state *vf_admin;
  2019. int slave;
  2020. if ((!mlx4_is_master(dev)) ||
  2021. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2022. return -EPROTONOSUPPORT;
  2023. if ((vlan > 4095) || (qos > 7))
  2024. return -EINVAL;
  2025. slave = mlx4_get_slave_indx(dev, vf);
  2026. if (slave < 0)
  2027. return -EINVAL;
  2028. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2029. vf_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2030. if ((0 == vlan) && (0 == qos))
  2031. vf_admin->default_vlan = MLX4_VGT;
  2032. else
  2033. vf_admin->default_vlan = vlan;
  2034. vf_admin->default_qos = qos;
  2035. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2036. mlx4_info(dev,
  2037. "updating vf %d port %d config will take effect on next VF restart\n",
  2038. vf, port);
  2039. return 0;
  2040. }
  2041. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2042. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2043. {
  2044. struct mlx4_priv *priv = mlx4_priv(dev);
  2045. struct mlx4_vport_state *s_info;
  2046. int slave;
  2047. if ((!mlx4_is_master(dev)) ||
  2048. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  2049. return -EPROTONOSUPPORT;
  2050. slave = mlx4_get_slave_indx(dev, vf);
  2051. if (slave < 0)
  2052. return -EINVAL;
  2053. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2054. s_info->spoofchk = setting;
  2055. return 0;
  2056. }
  2057. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2058. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2059. {
  2060. struct mlx4_priv *priv = mlx4_priv(dev);
  2061. struct mlx4_vport_state *s_info;
  2062. int slave;
  2063. if (!mlx4_is_master(dev))
  2064. return -EPROTONOSUPPORT;
  2065. slave = mlx4_get_slave_indx(dev, vf);
  2066. if (slave < 0)
  2067. return -EINVAL;
  2068. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2069. ivf->vf = vf;
  2070. /* need to convert it to a func */
  2071. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2072. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2073. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2074. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2075. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2076. ivf->mac[5] = ((s_info->mac) & 0xff);
  2077. ivf->vlan = s_info->default_vlan;
  2078. ivf->qos = s_info->default_qos;
  2079. ivf->tx_rate = s_info->tx_rate;
  2080. ivf->spoofchk = s_info->spoofchk;
  2081. ivf->linkstate = s_info->link_state;
  2082. return 0;
  2083. }
  2084. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2085. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2086. {
  2087. struct mlx4_priv *priv = mlx4_priv(dev);
  2088. struct mlx4_vport_state *s_info;
  2089. int slave;
  2090. u8 link_stat_event;
  2091. slave = mlx4_get_slave_indx(dev, vf);
  2092. if (slave < 0)
  2093. return -EINVAL;
  2094. switch (link_state) {
  2095. case IFLA_VF_LINK_STATE_AUTO:
  2096. /* get current link state */
  2097. if (!priv->sense.do_sense_port[port])
  2098. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2099. else
  2100. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2101. break;
  2102. case IFLA_VF_LINK_STATE_ENABLE:
  2103. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2104. break;
  2105. case IFLA_VF_LINK_STATE_DISABLE:
  2106. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2107. break;
  2108. default:
  2109. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2110. link_state, slave, port);
  2111. return -EINVAL;
  2112. };
  2113. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2114. s_info->link_state = link_state;
  2115. /* send event */
  2116. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2117. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2118. mlx4_dbg(dev,
  2119. "updating vf %d port %d no link state HW enforcment\n",
  2120. vf, port);
  2121. return 0;
  2122. }
  2123. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);