cmd.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  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->state.default_vlan);
  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->state.mac);
  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_state *vf_admin;
  2035. int slave;
  2036. if ((!mlx4_is_master(dev)) ||
  2037. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2038. return -EPROTONOSUPPORT;
  2039. if ((vlan > 4095) || (qos > 7))
  2040. return -EINVAL;
  2041. slave = mlx4_get_slave_indx(dev, vf);
  2042. if (slave < 0)
  2043. return -EINVAL;
  2044. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2045. if ((0 == vlan) && (0 == qos))
  2046. vf_admin->default_vlan = MLX4_VGT;
  2047. else
  2048. vf_admin->default_vlan = vlan;
  2049. vf_admin->default_qos = qos;
  2050. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2051. mlx4_info(dev,
  2052. "updating vf %d port %d config will take effect on next VF restart\n",
  2053. vf, port);
  2054. return 0;
  2055. }
  2056. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2057. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2058. {
  2059. struct mlx4_priv *priv = mlx4_priv(dev);
  2060. struct mlx4_vport_state *s_info;
  2061. int slave;
  2062. if ((!mlx4_is_master(dev)) ||
  2063. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  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. s_info->spoofchk = setting;
  2070. return 0;
  2071. }
  2072. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2073. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2074. {
  2075. struct mlx4_priv *priv = mlx4_priv(dev);
  2076. struct mlx4_vport_state *s_info;
  2077. int slave;
  2078. if (!mlx4_is_master(dev))
  2079. return -EPROTONOSUPPORT;
  2080. slave = mlx4_get_slave_indx(dev, vf);
  2081. if (slave < 0)
  2082. return -EINVAL;
  2083. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2084. ivf->vf = vf;
  2085. /* need to convert it to a func */
  2086. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2087. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2088. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2089. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2090. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2091. ivf->mac[5] = ((s_info->mac) & 0xff);
  2092. ivf->vlan = s_info->default_vlan;
  2093. ivf->qos = s_info->default_qos;
  2094. ivf->tx_rate = s_info->tx_rate;
  2095. ivf->spoofchk = s_info->spoofchk;
  2096. ivf->linkstate = s_info->link_state;
  2097. return 0;
  2098. }
  2099. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2100. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2101. {
  2102. struct mlx4_priv *priv = mlx4_priv(dev);
  2103. struct mlx4_vport_state *s_info;
  2104. int slave;
  2105. u8 link_stat_event;
  2106. slave = mlx4_get_slave_indx(dev, vf);
  2107. if (slave < 0)
  2108. return -EINVAL;
  2109. switch (link_state) {
  2110. case IFLA_VF_LINK_STATE_AUTO:
  2111. /* get current link state */
  2112. if (!priv->sense.do_sense_port[port])
  2113. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2114. else
  2115. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2116. break;
  2117. case IFLA_VF_LINK_STATE_ENABLE:
  2118. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2119. break;
  2120. case IFLA_VF_LINK_STATE_DISABLE:
  2121. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2122. break;
  2123. default:
  2124. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2125. link_state, slave, port);
  2126. return -EINVAL;
  2127. };
  2128. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2129. s_info->link_state = link_state;
  2130. /* send event */
  2131. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2132. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2133. mlx4_dbg(dev,
  2134. "updating vf %d port %d no link state HW enforcment\n",
  2135. vf, port);
  2136. return 0;
  2137. }
  2138. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);