cmd.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  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 - my toggle is %d (cmd:0x%x)\n",
  192. priv->cmd.comm_toggle, cmd);
  193. return -EAGAIN;
  194. }
  195. /* Write command */
  196. down(&priv->cmd.poll_sem);
  197. mlx4_comm_cmd_post(dev, cmd, param);
  198. end = msecs_to_jiffies(timeout) + jiffies;
  199. while (comm_pending(dev) && time_before(jiffies, end))
  200. cond_resched();
  201. ret_from_pending = comm_pending(dev);
  202. if (ret_from_pending) {
  203. /* check if the slave is trying to boot in the middle of
  204. * FLR process. The only non-zero result in the RESET command
  205. * is MLX4_DELAY_RESET_SLAVE*/
  206. if ((MLX4_COMM_CMD_RESET == cmd)) {
  207. err = MLX4_DELAY_RESET_SLAVE;
  208. } else {
  209. mlx4_warn(dev, "Communication channel timed out\n");
  210. err = -ETIMEDOUT;
  211. }
  212. }
  213. up(&priv->cmd.poll_sem);
  214. return err;
  215. }
  216. static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 op,
  217. u16 param, unsigned long timeout)
  218. {
  219. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  220. struct mlx4_cmd_context *context;
  221. unsigned long end;
  222. int err = 0;
  223. down(&cmd->event_sem);
  224. spin_lock(&cmd->context_lock);
  225. BUG_ON(cmd->free_head < 0);
  226. context = &cmd->context[cmd->free_head];
  227. context->token += cmd->token_mask + 1;
  228. cmd->free_head = context->next;
  229. spin_unlock(&cmd->context_lock);
  230. init_completion(&context->done);
  231. mlx4_comm_cmd_post(dev, op, param);
  232. if (!wait_for_completion_timeout(&context->done,
  233. msecs_to_jiffies(timeout))) {
  234. mlx4_warn(dev, "communication channel command 0x%x timed out\n",
  235. op);
  236. err = -EBUSY;
  237. goto out;
  238. }
  239. err = context->result;
  240. if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
  241. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  242. op, context->fw_status);
  243. goto out;
  244. }
  245. out:
  246. /* wait for comm channel ready
  247. * this is necessary for prevention the race
  248. * when switching between event to polling mode
  249. */
  250. end = msecs_to_jiffies(timeout) + jiffies;
  251. while (comm_pending(dev) && time_before(jiffies, end))
  252. cond_resched();
  253. spin_lock(&cmd->context_lock);
  254. context->next = cmd->free_head;
  255. cmd->free_head = context - cmd->context;
  256. spin_unlock(&cmd->context_lock);
  257. up(&cmd->event_sem);
  258. return err;
  259. }
  260. int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
  261. unsigned long timeout)
  262. {
  263. if (mlx4_priv(dev)->cmd.use_events)
  264. return mlx4_comm_cmd_wait(dev, cmd, param, timeout);
  265. return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
  266. }
  267. static int cmd_pending(struct mlx4_dev *dev)
  268. {
  269. u32 status;
  270. if (pci_channel_offline(dev->pdev))
  271. return -EIO;
  272. status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
  273. return (status & swab32(1 << HCR_GO_BIT)) ||
  274. (mlx4_priv(dev)->cmd.toggle ==
  275. !!(status & swab32(1 << HCR_T_BIT)));
  276. }
  277. static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
  278. u32 in_modifier, u8 op_modifier, u16 op, u16 token,
  279. int event)
  280. {
  281. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  282. u32 __iomem *hcr = cmd->hcr;
  283. int ret = -EAGAIN;
  284. unsigned long end;
  285. mutex_lock(&cmd->hcr_mutex);
  286. if (pci_channel_offline(dev->pdev)) {
  287. /*
  288. * Device is going through error recovery
  289. * and cannot accept commands.
  290. */
  291. ret = -EIO;
  292. goto out;
  293. }
  294. end = jiffies;
  295. if (event)
  296. end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
  297. while (cmd_pending(dev)) {
  298. if (pci_channel_offline(dev->pdev)) {
  299. /*
  300. * Device is going through error recovery
  301. * and cannot accept commands.
  302. */
  303. ret = -EIO;
  304. goto out;
  305. }
  306. if (time_after_eq(jiffies, end)) {
  307. mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
  308. goto out;
  309. }
  310. cond_resched();
  311. }
  312. /*
  313. * We use writel (instead of something like memcpy_toio)
  314. * because writes of less than 32 bits to the HCR don't work
  315. * (and some architectures such as ia64 implement memcpy_toio
  316. * in terms of writeb).
  317. */
  318. __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
  319. __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
  320. __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
  321. __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
  322. __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
  323. __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
  324. /* __raw_writel may not order writes. */
  325. wmb();
  326. __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
  327. (cmd->toggle << HCR_T_BIT) |
  328. (event ? (1 << HCR_E_BIT) : 0) |
  329. (op_modifier << HCR_OPMOD_SHIFT) |
  330. op), hcr + 6);
  331. /*
  332. * Make sure that our HCR writes don't get mixed in with
  333. * writes from another CPU starting a FW command.
  334. */
  335. mmiowb();
  336. cmd->toggle = cmd->toggle ^ 1;
  337. ret = 0;
  338. out:
  339. mutex_unlock(&cmd->hcr_mutex);
  340. return ret;
  341. }
  342. static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  343. int out_is_imm, u32 in_modifier, u8 op_modifier,
  344. u16 op, unsigned long timeout)
  345. {
  346. struct mlx4_priv *priv = mlx4_priv(dev);
  347. struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
  348. int ret;
  349. mutex_lock(&priv->cmd.slave_cmd_mutex);
  350. vhcr->in_param = cpu_to_be64(in_param);
  351. vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
  352. vhcr->in_modifier = cpu_to_be32(in_modifier);
  353. vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
  354. vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
  355. vhcr->status = 0;
  356. vhcr->flags = !!(priv->cmd.use_events) << 6;
  357. if (mlx4_is_master(dev)) {
  358. ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
  359. if (!ret) {
  360. if (out_is_imm) {
  361. if (out_param)
  362. *out_param =
  363. be64_to_cpu(vhcr->out_param);
  364. else {
  365. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  366. op);
  367. vhcr->status = CMD_STAT_BAD_PARAM;
  368. }
  369. }
  370. ret = mlx4_status_to_errno(vhcr->status);
  371. }
  372. } else {
  373. ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0,
  374. MLX4_COMM_TIME + timeout);
  375. if (!ret) {
  376. if (out_is_imm) {
  377. if (out_param)
  378. *out_param =
  379. be64_to_cpu(vhcr->out_param);
  380. else {
  381. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  382. op);
  383. vhcr->status = CMD_STAT_BAD_PARAM;
  384. }
  385. }
  386. ret = mlx4_status_to_errno(vhcr->status);
  387. } else
  388. mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n",
  389. op);
  390. }
  391. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  392. return ret;
  393. }
  394. static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  395. int out_is_imm, u32 in_modifier, u8 op_modifier,
  396. u16 op, unsigned long timeout)
  397. {
  398. struct mlx4_priv *priv = mlx4_priv(dev);
  399. void __iomem *hcr = priv->cmd.hcr;
  400. int err = 0;
  401. unsigned long end;
  402. u32 stat;
  403. down(&priv->cmd.poll_sem);
  404. if (pci_channel_offline(dev->pdev)) {
  405. /*
  406. * Device is going through error recovery
  407. * and cannot accept commands.
  408. */
  409. err = -EIO;
  410. goto out;
  411. }
  412. if (out_is_imm && !out_param) {
  413. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  414. op);
  415. err = -EINVAL;
  416. goto out;
  417. }
  418. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  419. in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
  420. if (err)
  421. goto out;
  422. end = msecs_to_jiffies(timeout) + jiffies;
  423. while (cmd_pending(dev) && time_before(jiffies, end)) {
  424. if (pci_channel_offline(dev->pdev)) {
  425. /*
  426. * Device is going through error recovery
  427. * and cannot accept commands.
  428. */
  429. err = -EIO;
  430. goto out;
  431. }
  432. cond_resched();
  433. }
  434. if (cmd_pending(dev)) {
  435. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  436. op);
  437. err = -ETIMEDOUT;
  438. goto out;
  439. }
  440. if (out_is_imm)
  441. *out_param =
  442. (u64) be32_to_cpu((__force __be32)
  443. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
  444. (u64) be32_to_cpu((__force __be32)
  445. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
  446. stat = be32_to_cpu((__force __be32)
  447. __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
  448. err = mlx4_status_to_errno(stat);
  449. if (err)
  450. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  451. op, stat);
  452. out:
  453. up(&priv->cmd.poll_sem);
  454. return err;
  455. }
  456. void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
  457. {
  458. struct mlx4_priv *priv = mlx4_priv(dev);
  459. struct mlx4_cmd_context *context =
  460. &priv->cmd.context[token & priv->cmd.token_mask];
  461. /* previously timed out command completing at long last */
  462. if (token != context->token)
  463. return;
  464. context->fw_status = status;
  465. context->result = mlx4_status_to_errno(status);
  466. context->out_param = out_param;
  467. complete(&context->done);
  468. }
  469. static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  470. int out_is_imm, u32 in_modifier, u8 op_modifier,
  471. u16 op, unsigned long timeout)
  472. {
  473. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  474. struct mlx4_cmd_context *context;
  475. int err = 0;
  476. down(&cmd->event_sem);
  477. spin_lock(&cmd->context_lock);
  478. BUG_ON(cmd->free_head < 0);
  479. context = &cmd->context[cmd->free_head];
  480. context->token += cmd->token_mask + 1;
  481. cmd->free_head = context->next;
  482. spin_unlock(&cmd->context_lock);
  483. if (out_is_imm && !out_param) {
  484. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  485. op);
  486. err = -EINVAL;
  487. goto out;
  488. }
  489. init_completion(&context->done);
  490. mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  491. in_modifier, op_modifier, op, context->token, 1);
  492. if (!wait_for_completion_timeout(&context->done,
  493. msecs_to_jiffies(timeout))) {
  494. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  495. op);
  496. err = -EBUSY;
  497. goto out;
  498. }
  499. err = context->result;
  500. if (err) {
  501. /* Since we do not want to have this error message always
  502. * displayed at driver start when there are ConnectX2 HCAs
  503. * on the host, we deprecate the error message for this
  504. * specific command/input_mod/opcode_mod/fw-status to be debug.
  505. */
  506. if (op == MLX4_CMD_SET_PORT && in_modifier == 1 &&
  507. op_modifier == 0 && context->fw_status == CMD_STAT_BAD_SIZE)
  508. mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
  509. op, context->fw_status);
  510. else
  511. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  512. op, context->fw_status);
  513. goto out;
  514. }
  515. if (out_is_imm)
  516. *out_param = context->out_param;
  517. out:
  518. spin_lock(&cmd->context_lock);
  519. context->next = cmd->free_head;
  520. cmd->free_head = context - cmd->context;
  521. spin_unlock(&cmd->context_lock);
  522. up(&cmd->event_sem);
  523. return err;
  524. }
  525. int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  526. int out_is_imm, u32 in_modifier, u8 op_modifier,
  527. u16 op, unsigned long timeout, int native)
  528. {
  529. if (pci_channel_offline(dev->pdev))
  530. return -EIO;
  531. if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
  532. if (mlx4_priv(dev)->cmd.use_events)
  533. return mlx4_cmd_wait(dev, in_param, out_param,
  534. out_is_imm, in_modifier,
  535. op_modifier, op, timeout);
  536. else
  537. return mlx4_cmd_poll(dev, in_param, out_param,
  538. out_is_imm, in_modifier,
  539. op_modifier, op, timeout);
  540. }
  541. return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
  542. in_modifier, op_modifier, op, timeout);
  543. }
  544. EXPORT_SYMBOL_GPL(__mlx4_cmd);
  545. static int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
  546. {
  547. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
  548. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  549. }
  550. static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
  551. int slave, u64 slave_addr,
  552. int size, int is_read)
  553. {
  554. u64 in_param;
  555. u64 out_param;
  556. if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
  557. (slave & ~0x7f) | (size & 0xff)) {
  558. mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
  559. slave_addr, master_addr, slave, size);
  560. return -EINVAL;
  561. }
  562. if (is_read) {
  563. in_param = (u64) slave | slave_addr;
  564. out_param = (u64) dev->caps.function | master_addr;
  565. } else {
  566. in_param = (u64) dev->caps.function | master_addr;
  567. out_param = (u64) slave | slave_addr;
  568. }
  569. return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
  570. MLX4_CMD_ACCESS_MEM,
  571. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  572. }
  573. static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
  574. struct mlx4_cmd_mailbox *inbox,
  575. struct mlx4_cmd_mailbox *outbox)
  576. {
  577. struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
  578. struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
  579. int err;
  580. int i;
  581. if (index & 0x1f)
  582. return -EINVAL;
  583. in_mad->attr_mod = cpu_to_be32(index / 32);
  584. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  585. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  586. MLX4_CMD_NATIVE);
  587. if (err)
  588. return err;
  589. for (i = 0; i < 32; ++i)
  590. pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
  591. return err;
  592. }
  593. static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
  594. struct mlx4_cmd_mailbox *inbox,
  595. struct mlx4_cmd_mailbox *outbox)
  596. {
  597. int i;
  598. int err;
  599. for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
  600. err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
  601. if (err)
  602. return err;
  603. }
  604. return 0;
  605. }
  606. #define PORT_CAPABILITY_LOCATION_IN_SMP 20
  607. #define PORT_STATE_OFFSET 32
  608. static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
  609. {
  610. if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
  611. return IB_PORT_ACTIVE;
  612. else
  613. return IB_PORT_DOWN;
  614. }
  615. static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
  616. struct mlx4_vhcr *vhcr,
  617. struct mlx4_cmd_mailbox *inbox,
  618. struct mlx4_cmd_mailbox *outbox,
  619. struct mlx4_cmd_info *cmd)
  620. {
  621. struct ib_smp *smp = inbox->buf;
  622. u32 index;
  623. u8 port;
  624. u8 opcode_modifier;
  625. u16 *table;
  626. int err;
  627. int vidx, pidx;
  628. int network_view;
  629. struct mlx4_priv *priv = mlx4_priv(dev);
  630. struct ib_smp *outsmp = outbox->buf;
  631. __be16 *outtab = (__be16 *)(outsmp->data);
  632. __be32 slave_cap_mask;
  633. __be64 slave_node_guid;
  634. port = vhcr->in_modifier;
  635. /* network-view bit is for driver use only, and should not be passed to FW */
  636. opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
  637. network_view = !!(vhcr->op_modifier & 0x8);
  638. if (smp->base_version == 1 &&
  639. smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  640. smp->class_version == 1) {
  641. /* host view is paravirtualized */
  642. if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
  643. if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  644. index = be32_to_cpu(smp->attr_mod);
  645. if (port < 1 || port > dev->caps.num_ports)
  646. return -EINVAL;
  647. table = kcalloc(dev->caps.pkey_table_len[port], sizeof *table, GFP_KERNEL);
  648. if (!table)
  649. return -ENOMEM;
  650. /* need to get the full pkey table because the paravirtualized
  651. * pkeys may be scattered among several pkey blocks.
  652. */
  653. err = get_full_pkey_table(dev, port, table, inbox, outbox);
  654. if (!err) {
  655. for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
  656. pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
  657. outtab[vidx % 32] = cpu_to_be16(table[pidx]);
  658. }
  659. }
  660. kfree(table);
  661. return err;
  662. }
  663. if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
  664. /*get the slave specific caps:*/
  665. /*do the command */
  666. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  667. vhcr->in_modifier, opcode_modifier,
  668. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  669. /* modify the response for slaves */
  670. if (!err && slave != mlx4_master_func_num(dev)) {
  671. u8 *state = outsmp->data + PORT_STATE_OFFSET;
  672. *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
  673. slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  674. memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
  675. }
  676. return err;
  677. }
  678. if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
  679. /* compute slave's gid block */
  680. smp->attr_mod = cpu_to_be32(slave / 8);
  681. /* execute cmd */
  682. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  683. vhcr->in_modifier, opcode_modifier,
  684. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  685. if (!err) {
  686. /* if needed, move slave gid to index 0 */
  687. if (slave % 8)
  688. memcpy(outsmp->data,
  689. outsmp->data + (slave % 8) * 8, 8);
  690. /* delete all other gids */
  691. memset(outsmp->data + 8, 0, 56);
  692. }
  693. return err;
  694. }
  695. if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
  696. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  697. vhcr->in_modifier, opcode_modifier,
  698. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  699. if (!err) {
  700. slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
  701. memcpy(outsmp->data + 12, &slave_node_guid, 8);
  702. }
  703. return err;
  704. }
  705. }
  706. }
  707. /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
  708. * These are the MADs used by ib verbs (such as ib_query_gids).
  709. */
  710. if (slave != mlx4_master_func_num(dev) &&
  711. !mlx4_vf_smi_enabled(dev, slave, port)) {
  712. if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  713. smp->method == IB_MGMT_METHOD_GET) || network_view) {
  714. mlx4_err(dev, "Unprivileged slave %d is trying to execute a Subnet MGMT MAD, class 0x%x, method 0x%x, view=%s for attr 0x%x. Rejecting\n",
  715. slave, smp->method, smp->mgmt_class,
  716. network_view ? "Network" : "Host",
  717. be16_to_cpu(smp->attr_id));
  718. return -EPERM;
  719. }
  720. }
  721. return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  722. vhcr->in_modifier, opcode_modifier,
  723. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  724. }
  725. static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
  726. struct mlx4_vhcr *vhcr,
  727. struct mlx4_cmd_mailbox *inbox,
  728. struct mlx4_cmd_mailbox *outbox,
  729. struct mlx4_cmd_info *cmd)
  730. {
  731. return -EPERM;
  732. }
  733. int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
  734. struct mlx4_vhcr *vhcr,
  735. struct mlx4_cmd_mailbox *inbox,
  736. struct mlx4_cmd_mailbox *outbox,
  737. struct mlx4_cmd_info *cmd)
  738. {
  739. u64 in_param;
  740. u64 out_param;
  741. int err;
  742. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  743. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  744. if (cmd->encode_slave_id) {
  745. in_param &= 0xffffffffffffff00ll;
  746. in_param |= slave;
  747. }
  748. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  749. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  750. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  751. if (cmd->out_is_imm)
  752. vhcr->out_param = out_param;
  753. return err;
  754. }
  755. static struct mlx4_cmd_info cmd_info[] = {
  756. {
  757. .opcode = MLX4_CMD_QUERY_FW,
  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_FW_wrapper
  764. },
  765. {
  766. .opcode = MLX4_CMD_QUERY_HCA,
  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_QUERY_DEV_CAP,
  776. .has_inbox = false,
  777. .has_outbox = true,
  778. .out_is_imm = false,
  779. .encode_slave_id = false,
  780. .verify = NULL,
  781. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  782. },
  783. {
  784. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  785. .has_inbox = false,
  786. .has_outbox = true,
  787. .out_is_imm = false,
  788. .encode_slave_id = false,
  789. .verify = NULL,
  790. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  791. },
  792. {
  793. .opcode = MLX4_CMD_QUERY_ADAPTER,
  794. .has_inbox = false,
  795. .has_outbox = true,
  796. .out_is_imm = false,
  797. .encode_slave_id = false,
  798. .verify = NULL,
  799. .wrapper = NULL
  800. },
  801. {
  802. .opcode = MLX4_CMD_INIT_PORT,
  803. .has_inbox = false,
  804. .has_outbox = false,
  805. .out_is_imm = false,
  806. .encode_slave_id = false,
  807. .verify = NULL,
  808. .wrapper = mlx4_INIT_PORT_wrapper
  809. },
  810. {
  811. .opcode = MLX4_CMD_CLOSE_PORT,
  812. .has_inbox = false,
  813. .has_outbox = false,
  814. .out_is_imm = false,
  815. .encode_slave_id = false,
  816. .verify = NULL,
  817. .wrapper = mlx4_CLOSE_PORT_wrapper
  818. },
  819. {
  820. .opcode = MLX4_CMD_QUERY_PORT,
  821. .has_inbox = false,
  822. .has_outbox = true,
  823. .out_is_imm = false,
  824. .encode_slave_id = false,
  825. .verify = NULL,
  826. .wrapper = mlx4_QUERY_PORT_wrapper
  827. },
  828. {
  829. .opcode = MLX4_CMD_SET_PORT,
  830. .has_inbox = true,
  831. .has_outbox = false,
  832. .out_is_imm = false,
  833. .encode_slave_id = false,
  834. .verify = NULL,
  835. .wrapper = mlx4_SET_PORT_wrapper
  836. },
  837. {
  838. .opcode = MLX4_CMD_MAP_EQ,
  839. .has_inbox = false,
  840. .has_outbox = false,
  841. .out_is_imm = false,
  842. .encode_slave_id = false,
  843. .verify = NULL,
  844. .wrapper = mlx4_MAP_EQ_wrapper
  845. },
  846. {
  847. .opcode = MLX4_CMD_SW2HW_EQ,
  848. .has_inbox = true,
  849. .has_outbox = false,
  850. .out_is_imm = false,
  851. .encode_slave_id = true,
  852. .verify = NULL,
  853. .wrapper = mlx4_SW2HW_EQ_wrapper
  854. },
  855. {
  856. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  857. .has_inbox = false,
  858. .has_outbox = false,
  859. .out_is_imm = false,
  860. .encode_slave_id = false,
  861. .verify = NULL,
  862. .wrapper = NULL
  863. },
  864. {
  865. .opcode = MLX4_CMD_NOP,
  866. .has_inbox = false,
  867. .has_outbox = false,
  868. .out_is_imm = false,
  869. .encode_slave_id = false,
  870. .verify = NULL,
  871. .wrapper = NULL
  872. },
  873. {
  874. .opcode = MLX4_CMD_CONFIG_DEV,
  875. .has_inbox = false,
  876. .has_outbox = true,
  877. .out_is_imm = false,
  878. .encode_slave_id = false,
  879. .verify = NULL,
  880. .wrapper = mlx4_CONFIG_DEV_wrapper
  881. },
  882. {
  883. .opcode = MLX4_CMD_ALLOC_RES,
  884. .has_inbox = false,
  885. .has_outbox = false,
  886. .out_is_imm = true,
  887. .encode_slave_id = false,
  888. .verify = NULL,
  889. .wrapper = mlx4_ALLOC_RES_wrapper
  890. },
  891. {
  892. .opcode = MLX4_CMD_FREE_RES,
  893. .has_inbox = false,
  894. .has_outbox = false,
  895. .out_is_imm = false,
  896. .encode_slave_id = false,
  897. .verify = NULL,
  898. .wrapper = mlx4_FREE_RES_wrapper
  899. },
  900. {
  901. .opcode = MLX4_CMD_SW2HW_MPT,
  902. .has_inbox = true,
  903. .has_outbox = false,
  904. .out_is_imm = false,
  905. .encode_slave_id = true,
  906. .verify = NULL,
  907. .wrapper = mlx4_SW2HW_MPT_wrapper
  908. },
  909. {
  910. .opcode = MLX4_CMD_QUERY_MPT,
  911. .has_inbox = false,
  912. .has_outbox = true,
  913. .out_is_imm = false,
  914. .encode_slave_id = false,
  915. .verify = NULL,
  916. .wrapper = mlx4_QUERY_MPT_wrapper
  917. },
  918. {
  919. .opcode = MLX4_CMD_HW2SW_MPT,
  920. .has_inbox = false,
  921. .has_outbox = false,
  922. .out_is_imm = false,
  923. .encode_slave_id = false,
  924. .verify = NULL,
  925. .wrapper = mlx4_HW2SW_MPT_wrapper
  926. },
  927. {
  928. .opcode = MLX4_CMD_READ_MTT,
  929. .has_inbox = false,
  930. .has_outbox = true,
  931. .out_is_imm = false,
  932. .encode_slave_id = false,
  933. .verify = NULL,
  934. .wrapper = NULL
  935. },
  936. {
  937. .opcode = MLX4_CMD_WRITE_MTT,
  938. .has_inbox = true,
  939. .has_outbox = false,
  940. .out_is_imm = false,
  941. .encode_slave_id = false,
  942. .verify = NULL,
  943. .wrapper = mlx4_WRITE_MTT_wrapper
  944. },
  945. {
  946. .opcode = MLX4_CMD_SYNC_TPT,
  947. .has_inbox = true,
  948. .has_outbox = false,
  949. .out_is_imm = false,
  950. .encode_slave_id = false,
  951. .verify = NULL,
  952. .wrapper = NULL
  953. },
  954. {
  955. .opcode = MLX4_CMD_HW2SW_EQ,
  956. .has_inbox = false,
  957. .has_outbox = true,
  958. .out_is_imm = false,
  959. .encode_slave_id = true,
  960. .verify = NULL,
  961. .wrapper = mlx4_HW2SW_EQ_wrapper
  962. },
  963. {
  964. .opcode = MLX4_CMD_QUERY_EQ,
  965. .has_inbox = false,
  966. .has_outbox = true,
  967. .out_is_imm = false,
  968. .encode_slave_id = true,
  969. .verify = NULL,
  970. .wrapper = mlx4_QUERY_EQ_wrapper
  971. },
  972. {
  973. .opcode = MLX4_CMD_SW2HW_CQ,
  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_CQ_wrapper
  980. },
  981. {
  982. .opcode = MLX4_CMD_HW2SW_CQ,
  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_CQ_wrapper
  989. },
  990. {
  991. .opcode = MLX4_CMD_QUERY_CQ,
  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_CQ_wrapper
  998. },
  999. {
  1000. .opcode = MLX4_CMD_MODIFY_CQ,
  1001. .has_inbox = true,
  1002. .has_outbox = false,
  1003. .out_is_imm = true,
  1004. .encode_slave_id = false,
  1005. .verify = NULL,
  1006. .wrapper = mlx4_MODIFY_CQ_wrapper
  1007. },
  1008. {
  1009. .opcode = MLX4_CMD_SW2HW_SRQ,
  1010. .has_inbox = true,
  1011. .has_outbox = false,
  1012. .out_is_imm = false,
  1013. .encode_slave_id = true,
  1014. .verify = NULL,
  1015. .wrapper = mlx4_SW2HW_SRQ_wrapper
  1016. },
  1017. {
  1018. .opcode = MLX4_CMD_HW2SW_SRQ,
  1019. .has_inbox = false,
  1020. .has_outbox = false,
  1021. .out_is_imm = false,
  1022. .encode_slave_id = false,
  1023. .verify = NULL,
  1024. .wrapper = mlx4_HW2SW_SRQ_wrapper
  1025. },
  1026. {
  1027. .opcode = MLX4_CMD_QUERY_SRQ,
  1028. .has_inbox = false,
  1029. .has_outbox = true,
  1030. .out_is_imm = false,
  1031. .encode_slave_id = false,
  1032. .verify = NULL,
  1033. .wrapper = mlx4_QUERY_SRQ_wrapper
  1034. },
  1035. {
  1036. .opcode = MLX4_CMD_ARM_SRQ,
  1037. .has_inbox = false,
  1038. .has_outbox = false,
  1039. .out_is_imm = false,
  1040. .encode_slave_id = false,
  1041. .verify = NULL,
  1042. .wrapper = mlx4_ARM_SRQ_wrapper
  1043. },
  1044. {
  1045. .opcode = MLX4_CMD_RST2INIT_QP,
  1046. .has_inbox = true,
  1047. .has_outbox = false,
  1048. .out_is_imm = false,
  1049. .encode_slave_id = true,
  1050. .verify = NULL,
  1051. .wrapper = mlx4_RST2INIT_QP_wrapper
  1052. },
  1053. {
  1054. .opcode = MLX4_CMD_INIT2INIT_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_INIT2INIT_QP_wrapper
  1061. },
  1062. {
  1063. .opcode = MLX4_CMD_INIT2RTR_QP,
  1064. .has_inbox = true,
  1065. .has_outbox = false,
  1066. .out_is_imm = false,
  1067. .encode_slave_id = false,
  1068. .verify = NULL,
  1069. .wrapper = mlx4_INIT2RTR_QP_wrapper
  1070. },
  1071. {
  1072. .opcode = MLX4_CMD_RTR2RTS_QP,
  1073. .has_inbox = true,
  1074. .has_outbox = false,
  1075. .out_is_imm = false,
  1076. .encode_slave_id = false,
  1077. .verify = NULL,
  1078. .wrapper = mlx4_RTR2RTS_QP_wrapper
  1079. },
  1080. {
  1081. .opcode = MLX4_CMD_RTS2RTS_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_RTS2RTS_QP_wrapper
  1088. },
  1089. {
  1090. .opcode = MLX4_CMD_SQERR2RTS_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_SQERR2RTS_QP_wrapper
  1097. },
  1098. {
  1099. .opcode = MLX4_CMD_2ERR_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_GEN_QP_wrapper
  1106. },
  1107. {
  1108. .opcode = MLX4_CMD_RTS2SQD_QP,
  1109. .has_inbox = false,
  1110. .has_outbox = false,
  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_SQD2SQD_QP,
  1118. .has_inbox = true,
  1119. .has_outbox = false,
  1120. .out_is_imm = false,
  1121. .encode_slave_id = false,
  1122. .verify = NULL,
  1123. .wrapper = mlx4_SQD2SQD_QP_wrapper
  1124. },
  1125. {
  1126. .opcode = MLX4_CMD_SQD2RTS_QP,
  1127. .has_inbox = true,
  1128. .has_outbox = false,
  1129. .out_is_imm = false,
  1130. .encode_slave_id = false,
  1131. .verify = NULL,
  1132. .wrapper = mlx4_SQD2RTS_QP_wrapper
  1133. },
  1134. {
  1135. .opcode = MLX4_CMD_2RST_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_2RST_QP_wrapper
  1142. },
  1143. {
  1144. .opcode = MLX4_CMD_QUERY_QP,
  1145. .has_inbox = false,
  1146. .has_outbox = true,
  1147. .out_is_imm = false,
  1148. .encode_slave_id = false,
  1149. .verify = NULL,
  1150. .wrapper = mlx4_GEN_QP_wrapper
  1151. },
  1152. {
  1153. .opcode = MLX4_CMD_SUSPEND_QP,
  1154. .has_inbox = false,
  1155. .has_outbox = false,
  1156. .out_is_imm = false,
  1157. .encode_slave_id = false,
  1158. .verify = NULL,
  1159. .wrapper = mlx4_GEN_QP_wrapper
  1160. },
  1161. {
  1162. .opcode = MLX4_CMD_UNSUSPEND_QP,
  1163. .has_inbox = false,
  1164. .has_outbox = false,
  1165. .out_is_imm = false,
  1166. .encode_slave_id = false,
  1167. .verify = NULL,
  1168. .wrapper = mlx4_GEN_QP_wrapper
  1169. },
  1170. {
  1171. .opcode = MLX4_CMD_UPDATE_QP,
  1172. .has_inbox = true,
  1173. .has_outbox = false,
  1174. .out_is_imm = false,
  1175. .encode_slave_id = false,
  1176. .verify = NULL,
  1177. .wrapper = mlx4_UPDATE_QP_wrapper
  1178. },
  1179. {
  1180. .opcode = MLX4_CMD_GET_OP_REQ,
  1181. .has_inbox = false,
  1182. .has_outbox = false,
  1183. .out_is_imm = false,
  1184. .encode_slave_id = false,
  1185. .verify = NULL,
  1186. .wrapper = mlx4_CMD_EPERM_wrapper,
  1187. },
  1188. {
  1189. .opcode = MLX4_CMD_CONF_SPECIAL_QP,
  1190. .has_inbox = false,
  1191. .has_outbox = false,
  1192. .out_is_imm = false,
  1193. .encode_slave_id = false,
  1194. .verify = NULL, /* XXX verify: only demux can do this */
  1195. .wrapper = NULL
  1196. },
  1197. {
  1198. .opcode = MLX4_CMD_MAD_IFC,
  1199. .has_inbox = true,
  1200. .has_outbox = true,
  1201. .out_is_imm = false,
  1202. .encode_slave_id = false,
  1203. .verify = NULL,
  1204. .wrapper = mlx4_MAD_IFC_wrapper
  1205. },
  1206. {
  1207. .opcode = MLX4_CMD_MAD_DEMUX,
  1208. .has_inbox = false,
  1209. .has_outbox = false,
  1210. .out_is_imm = false,
  1211. .encode_slave_id = false,
  1212. .verify = NULL,
  1213. .wrapper = mlx4_CMD_EPERM_wrapper
  1214. },
  1215. {
  1216. .opcode = MLX4_CMD_QUERY_IF_STAT,
  1217. .has_inbox = false,
  1218. .has_outbox = true,
  1219. .out_is_imm = false,
  1220. .encode_slave_id = false,
  1221. .verify = NULL,
  1222. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  1223. },
  1224. {
  1225. .opcode = MLX4_CMD_ACCESS_REG,
  1226. .has_inbox = true,
  1227. .has_outbox = true,
  1228. .out_is_imm = false,
  1229. .encode_slave_id = false,
  1230. .verify = NULL,
  1231. .wrapper = mlx4_ACCESS_REG_wrapper,
  1232. },
  1233. /* Native multicast commands are not available for guests */
  1234. {
  1235. .opcode = MLX4_CMD_QP_ATTACH,
  1236. .has_inbox = true,
  1237. .has_outbox = false,
  1238. .out_is_imm = false,
  1239. .encode_slave_id = false,
  1240. .verify = NULL,
  1241. .wrapper = mlx4_QP_ATTACH_wrapper
  1242. },
  1243. {
  1244. .opcode = MLX4_CMD_PROMISC,
  1245. .has_inbox = false,
  1246. .has_outbox = false,
  1247. .out_is_imm = false,
  1248. .encode_slave_id = false,
  1249. .verify = NULL,
  1250. .wrapper = mlx4_PROMISC_wrapper
  1251. },
  1252. /* Ethernet specific commands */
  1253. {
  1254. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  1255. .has_inbox = true,
  1256. .has_outbox = false,
  1257. .out_is_imm = false,
  1258. .encode_slave_id = false,
  1259. .verify = NULL,
  1260. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  1261. },
  1262. {
  1263. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  1264. .has_inbox = false,
  1265. .has_outbox = false,
  1266. .out_is_imm = false,
  1267. .encode_slave_id = false,
  1268. .verify = NULL,
  1269. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  1270. },
  1271. {
  1272. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  1273. .has_inbox = false,
  1274. .has_outbox = true,
  1275. .out_is_imm = false,
  1276. .encode_slave_id = false,
  1277. .verify = NULL,
  1278. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  1279. },
  1280. {
  1281. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  1282. .has_inbox = false,
  1283. .has_outbox = false,
  1284. .out_is_imm = false,
  1285. .encode_slave_id = false,
  1286. .verify = NULL,
  1287. .wrapper = NULL
  1288. },
  1289. /* flow steering commands */
  1290. {
  1291. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1292. .has_inbox = true,
  1293. .has_outbox = false,
  1294. .out_is_imm = true,
  1295. .encode_slave_id = false,
  1296. .verify = NULL,
  1297. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1298. },
  1299. {
  1300. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1301. .has_inbox = false,
  1302. .has_outbox = false,
  1303. .out_is_imm = false,
  1304. .encode_slave_id = false,
  1305. .verify = NULL,
  1306. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1307. },
  1308. {
  1309. .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
  1310. .has_inbox = false,
  1311. .has_outbox = false,
  1312. .out_is_imm = false,
  1313. .encode_slave_id = false,
  1314. .verify = NULL,
  1315. .wrapper = mlx4_CMD_EPERM_wrapper
  1316. },
  1317. };
  1318. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1319. struct mlx4_vhcr_cmd *in_vhcr)
  1320. {
  1321. struct mlx4_priv *priv = mlx4_priv(dev);
  1322. struct mlx4_cmd_info *cmd = NULL;
  1323. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1324. struct mlx4_vhcr *vhcr;
  1325. struct mlx4_cmd_mailbox *inbox = NULL;
  1326. struct mlx4_cmd_mailbox *outbox = NULL;
  1327. u64 in_param;
  1328. u64 out_param;
  1329. int ret = 0;
  1330. int i;
  1331. int err = 0;
  1332. /* Create sw representation of Virtual HCR */
  1333. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1334. if (!vhcr)
  1335. return -ENOMEM;
  1336. /* DMA in the vHCR */
  1337. if (!in_vhcr) {
  1338. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1339. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1340. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1341. MLX4_ACCESS_MEM_ALIGN), 1);
  1342. if (ret) {
  1343. mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
  1344. __func__, ret);
  1345. kfree(vhcr);
  1346. return ret;
  1347. }
  1348. }
  1349. /* Fill SW VHCR fields */
  1350. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1351. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1352. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1353. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1354. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1355. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1356. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1357. /* Lookup command */
  1358. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1359. if (vhcr->op == cmd_info[i].opcode) {
  1360. cmd = &cmd_info[i];
  1361. break;
  1362. }
  1363. }
  1364. if (!cmd) {
  1365. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1366. vhcr->op, slave);
  1367. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1368. goto out_status;
  1369. }
  1370. /* Read inbox */
  1371. if (cmd->has_inbox) {
  1372. vhcr->in_param &= INBOX_MASK;
  1373. inbox = mlx4_alloc_cmd_mailbox(dev);
  1374. if (IS_ERR(inbox)) {
  1375. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1376. inbox = NULL;
  1377. goto out_status;
  1378. }
  1379. if (mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1380. vhcr->in_param,
  1381. MLX4_MAILBOX_SIZE, 1)) {
  1382. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1383. __func__, cmd->opcode);
  1384. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1385. goto out_status;
  1386. }
  1387. }
  1388. /* Apply permission and bound checks if applicable */
  1389. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1390. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
  1391. vhcr->op, slave, vhcr->in_modifier);
  1392. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1393. goto out_status;
  1394. }
  1395. /* Allocate outbox */
  1396. if (cmd->has_outbox) {
  1397. outbox = mlx4_alloc_cmd_mailbox(dev);
  1398. if (IS_ERR(outbox)) {
  1399. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1400. outbox = NULL;
  1401. goto out_status;
  1402. }
  1403. }
  1404. /* Execute the command! */
  1405. if (cmd->wrapper) {
  1406. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1407. cmd);
  1408. if (cmd->out_is_imm)
  1409. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1410. } else {
  1411. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1412. vhcr->in_param;
  1413. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1414. vhcr->out_param;
  1415. err = __mlx4_cmd(dev, in_param, &out_param,
  1416. cmd->out_is_imm, vhcr->in_modifier,
  1417. vhcr->op_modifier, vhcr->op,
  1418. MLX4_CMD_TIME_CLASS_A,
  1419. MLX4_CMD_NATIVE);
  1420. if (cmd->out_is_imm) {
  1421. vhcr->out_param = out_param;
  1422. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1423. }
  1424. }
  1425. if (err) {
  1426. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
  1427. vhcr->op, slave, vhcr->errno, err);
  1428. vhcr_cmd->status = mlx4_errno_to_status(err);
  1429. goto out_status;
  1430. }
  1431. /* Write outbox if command completed successfully */
  1432. if (cmd->has_outbox && !vhcr_cmd->status) {
  1433. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1434. vhcr->out_param,
  1435. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1436. if (ret) {
  1437. /* If we failed to write back the outbox after the
  1438. *command was successfully executed, we must fail this
  1439. * slave, as it is now in undefined state */
  1440. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1441. goto out;
  1442. }
  1443. }
  1444. out_status:
  1445. /* DMA back vhcr result */
  1446. if (!in_vhcr) {
  1447. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1448. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1449. ALIGN(sizeof(struct mlx4_vhcr),
  1450. MLX4_ACCESS_MEM_ALIGN),
  1451. MLX4_CMD_WRAPPED);
  1452. if (ret)
  1453. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1454. __func__);
  1455. else if (vhcr->e_bit &&
  1456. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1457. mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
  1458. slave);
  1459. }
  1460. out:
  1461. kfree(vhcr);
  1462. mlx4_free_cmd_mailbox(dev, inbox);
  1463. mlx4_free_cmd_mailbox(dev, outbox);
  1464. return ret;
  1465. }
  1466. static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
  1467. int slave, int port)
  1468. {
  1469. struct mlx4_vport_oper_state *vp_oper;
  1470. struct mlx4_vport_state *vp_admin;
  1471. struct mlx4_vf_immed_vlan_work *work;
  1472. struct mlx4_dev *dev = &(priv->dev);
  1473. int err;
  1474. int admin_vlan_ix = NO_INDX;
  1475. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1476. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1477. if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
  1478. vp_oper->state.default_qos == vp_admin->default_qos &&
  1479. vp_oper->state.link_state == vp_admin->link_state)
  1480. return 0;
  1481. if (!(priv->mfunc.master.slave_state[slave].active &&
  1482. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
  1483. /* even if the UPDATE_QP command isn't supported, we still want
  1484. * to set this VF link according to the admin directive
  1485. */
  1486. vp_oper->state.link_state = vp_admin->link_state;
  1487. return -1;
  1488. }
  1489. mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
  1490. slave, port);
  1491. mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
  1492. vp_admin->default_vlan, vp_admin->default_qos,
  1493. vp_admin->link_state);
  1494. work = kzalloc(sizeof(*work), GFP_KERNEL);
  1495. if (!work)
  1496. return -ENOMEM;
  1497. if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
  1498. if (MLX4_VGT != vp_admin->default_vlan) {
  1499. err = __mlx4_register_vlan(&priv->dev, port,
  1500. vp_admin->default_vlan,
  1501. &admin_vlan_ix);
  1502. if (err) {
  1503. kfree(work);
  1504. mlx4_warn(&priv->dev,
  1505. "No vlan resources slave %d, port %d\n",
  1506. slave, port);
  1507. return err;
  1508. }
  1509. } else {
  1510. admin_vlan_ix = NO_INDX;
  1511. }
  1512. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
  1513. mlx4_dbg(&priv->dev,
  1514. "alloc vlan %d idx %d slave %d port %d\n",
  1515. (int)(vp_admin->default_vlan),
  1516. admin_vlan_ix, slave, port);
  1517. }
  1518. /* save original vlan ix and vlan id */
  1519. work->orig_vlan_id = vp_oper->state.default_vlan;
  1520. work->orig_vlan_ix = vp_oper->vlan_idx;
  1521. /* handle new qos */
  1522. if (vp_oper->state.default_qos != vp_admin->default_qos)
  1523. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
  1524. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
  1525. vp_oper->vlan_idx = admin_vlan_ix;
  1526. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1527. vp_oper->state.default_qos = vp_admin->default_qos;
  1528. vp_oper->state.link_state = vp_admin->link_state;
  1529. if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
  1530. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
  1531. /* iterate over QPs owned by this slave, using UPDATE_QP */
  1532. work->port = port;
  1533. work->slave = slave;
  1534. work->qos = vp_oper->state.default_qos;
  1535. work->vlan_id = vp_oper->state.default_vlan;
  1536. work->vlan_ix = vp_oper->vlan_idx;
  1537. work->priv = priv;
  1538. INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
  1539. queue_work(priv->mfunc.master.comm_wq, &work->work);
  1540. return 0;
  1541. }
  1542. static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
  1543. {
  1544. int port, err;
  1545. struct mlx4_vport_state *vp_admin;
  1546. struct mlx4_vport_oper_state *vp_oper;
  1547. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1548. &priv->dev, slave);
  1549. int min_port = find_first_bit(actv_ports.ports,
  1550. priv->dev.caps.num_ports) + 1;
  1551. int max_port = min_port - 1 +
  1552. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1553. for (port = min_port; port <= max_port; port++) {
  1554. if (!test_bit(port - 1, actv_ports.ports))
  1555. continue;
  1556. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1557. priv->mfunc.master.vf_admin[slave].enable_smi[port];
  1558. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1559. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1560. vp_oper->state = *vp_admin;
  1561. if (MLX4_VGT != vp_admin->default_vlan) {
  1562. err = __mlx4_register_vlan(&priv->dev, port,
  1563. vp_admin->default_vlan, &(vp_oper->vlan_idx));
  1564. if (err) {
  1565. vp_oper->vlan_idx = NO_INDX;
  1566. mlx4_warn(&priv->dev,
  1567. "No vlan resources slave %d, port %d\n",
  1568. slave, port);
  1569. return err;
  1570. }
  1571. mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
  1572. (int)(vp_oper->state.default_vlan),
  1573. vp_oper->vlan_idx, slave, port);
  1574. }
  1575. if (vp_admin->spoofchk) {
  1576. vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
  1577. port,
  1578. vp_admin->mac);
  1579. if (0 > vp_oper->mac_idx) {
  1580. err = vp_oper->mac_idx;
  1581. vp_oper->mac_idx = NO_INDX;
  1582. mlx4_warn(&priv->dev,
  1583. "No mac resources slave %d, port %d\n",
  1584. slave, port);
  1585. return err;
  1586. }
  1587. mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
  1588. vp_oper->state.mac, vp_oper->mac_idx, slave, port);
  1589. }
  1590. }
  1591. return 0;
  1592. }
  1593. static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
  1594. {
  1595. int port;
  1596. struct mlx4_vport_oper_state *vp_oper;
  1597. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1598. &priv->dev, slave);
  1599. int min_port = find_first_bit(actv_ports.ports,
  1600. priv->dev.caps.num_ports) + 1;
  1601. int max_port = min_port - 1 +
  1602. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1603. for (port = min_port; port <= max_port; port++) {
  1604. if (!test_bit(port - 1, actv_ports.ports))
  1605. continue;
  1606. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1607. MLX4_VF_SMI_DISABLED;
  1608. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1609. if (NO_INDX != vp_oper->vlan_idx) {
  1610. __mlx4_unregister_vlan(&priv->dev,
  1611. port, vp_oper->state.default_vlan);
  1612. vp_oper->vlan_idx = NO_INDX;
  1613. }
  1614. if (NO_INDX != vp_oper->mac_idx) {
  1615. __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
  1616. vp_oper->mac_idx = NO_INDX;
  1617. }
  1618. }
  1619. return;
  1620. }
  1621. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1622. u16 param, u8 toggle)
  1623. {
  1624. struct mlx4_priv *priv = mlx4_priv(dev);
  1625. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1626. u32 reply;
  1627. u8 is_going_down = 0;
  1628. int i;
  1629. unsigned long flags;
  1630. slave_state[slave].comm_toggle ^= 1;
  1631. reply = (u32) slave_state[slave].comm_toggle << 31;
  1632. if (toggle != slave_state[slave].comm_toggle) {
  1633. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
  1634. toggle, slave);
  1635. goto reset_slave;
  1636. }
  1637. if (cmd == MLX4_COMM_CMD_RESET) {
  1638. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1639. slave_state[slave].active = false;
  1640. slave_state[slave].old_vlan_api = false;
  1641. mlx4_master_deactivate_admin_state(priv, slave);
  1642. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1643. slave_state[slave].event_eq[i].eqn = -1;
  1644. slave_state[slave].event_eq[i].token = 0;
  1645. }
  1646. /*check if we are in the middle of FLR process,
  1647. if so return "retry" status to the slave*/
  1648. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1649. goto inform_slave_state;
  1650. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1651. /* write the version in the event field */
  1652. reply |= mlx4_comm_get_version();
  1653. goto reset_slave;
  1654. }
  1655. /*command from slave in the middle of FLR*/
  1656. if (cmd != MLX4_COMM_CMD_RESET &&
  1657. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1658. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
  1659. slave, cmd);
  1660. return;
  1661. }
  1662. switch (cmd) {
  1663. case MLX4_COMM_CMD_VHCR0:
  1664. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1665. goto reset_slave;
  1666. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1667. priv->mfunc.master.slave_state[slave].cookie = 0;
  1668. mutex_init(&priv->mfunc.master.gen_eqe_mutex[slave]);
  1669. break;
  1670. case MLX4_COMM_CMD_VHCR1:
  1671. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1672. goto reset_slave;
  1673. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1674. break;
  1675. case MLX4_COMM_CMD_VHCR2:
  1676. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1677. goto reset_slave;
  1678. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1679. break;
  1680. case MLX4_COMM_CMD_VHCR_EN:
  1681. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1682. goto reset_slave;
  1683. slave_state[slave].vhcr_dma |= param;
  1684. if (mlx4_master_activate_admin_state(priv, slave))
  1685. goto reset_slave;
  1686. slave_state[slave].active = true;
  1687. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1688. break;
  1689. case MLX4_COMM_CMD_VHCR_POST:
  1690. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1691. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST))
  1692. goto reset_slave;
  1693. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1694. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1695. mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
  1696. slave);
  1697. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1698. goto reset_slave;
  1699. }
  1700. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1701. break;
  1702. default:
  1703. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  1704. goto reset_slave;
  1705. }
  1706. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1707. if (!slave_state[slave].is_slave_going_down)
  1708. slave_state[slave].last_cmd = cmd;
  1709. else
  1710. is_going_down = 1;
  1711. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1712. if (is_going_down) {
  1713. mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
  1714. cmd, slave);
  1715. return;
  1716. }
  1717. __raw_writel((__force u32) cpu_to_be32(reply),
  1718. &priv->mfunc.comm[slave].slave_read);
  1719. mmiowb();
  1720. return;
  1721. reset_slave:
  1722. /* cleanup any slave resources */
  1723. mlx4_delete_all_resources_for_slave(dev, slave);
  1724. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  1725. if (!slave_state[slave].is_slave_going_down)
  1726. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  1727. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  1728. /*with slave in the middle of flr, no need to clean resources again.*/
  1729. inform_slave_state:
  1730. memset(&slave_state[slave].event_eq, 0,
  1731. sizeof(struct mlx4_slave_event_eq_info));
  1732. __raw_writel((__force u32) cpu_to_be32(reply),
  1733. &priv->mfunc.comm[slave].slave_read);
  1734. wmb();
  1735. }
  1736. /* master command processing */
  1737. void mlx4_master_comm_channel(struct work_struct *work)
  1738. {
  1739. struct mlx4_mfunc_master_ctx *master =
  1740. container_of(work,
  1741. struct mlx4_mfunc_master_ctx,
  1742. comm_work);
  1743. struct mlx4_mfunc *mfunc =
  1744. container_of(master, struct mlx4_mfunc, master);
  1745. struct mlx4_priv *priv =
  1746. container_of(mfunc, struct mlx4_priv, mfunc);
  1747. struct mlx4_dev *dev = &priv->dev;
  1748. __be32 *bit_vec;
  1749. u32 comm_cmd;
  1750. u32 vec;
  1751. int i, j, slave;
  1752. int toggle;
  1753. int served = 0;
  1754. int reported = 0;
  1755. u32 slt;
  1756. bit_vec = master->comm_arm_bit_vector;
  1757. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  1758. vec = be32_to_cpu(bit_vec[i]);
  1759. for (j = 0; j < 32; j++) {
  1760. if (!(vec & (1 << j)))
  1761. continue;
  1762. ++reported;
  1763. slave = (i * 32) + j;
  1764. comm_cmd = swab32(readl(
  1765. &mfunc->comm[slave].slave_write));
  1766. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  1767. >> 31;
  1768. toggle = comm_cmd >> 31;
  1769. if (toggle != slt) {
  1770. if (master->slave_state[slave].comm_toggle
  1771. != slt) {
  1772. pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
  1773. slave, slt,
  1774. master->slave_state[slave].comm_toggle);
  1775. master->slave_state[slave].comm_toggle =
  1776. slt;
  1777. }
  1778. mlx4_master_do_cmd(dev, slave,
  1779. comm_cmd >> 16 & 0xff,
  1780. comm_cmd & 0xffff, toggle);
  1781. ++served;
  1782. }
  1783. }
  1784. }
  1785. if (reported && reported != served)
  1786. mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
  1787. reported, served);
  1788. if (mlx4_ARM_COMM_CHANNEL(dev))
  1789. mlx4_warn(dev, "Failed to arm comm channel events\n");
  1790. }
  1791. static int sync_toggles(struct mlx4_dev *dev)
  1792. {
  1793. struct mlx4_priv *priv = mlx4_priv(dev);
  1794. int wr_toggle;
  1795. int rd_toggle;
  1796. unsigned long end;
  1797. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write)) >> 31;
  1798. end = jiffies + msecs_to_jiffies(5000);
  1799. while (time_before(jiffies, end)) {
  1800. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read)) >> 31;
  1801. if (rd_toggle == wr_toggle) {
  1802. priv->cmd.comm_toggle = rd_toggle;
  1803. return 0;
  1804. }
  1805. cond_resched();
  1806. }
  1807. /*
  1808. * we could reach here if for example the previous VM using this
  1809. * function misbehaved and left the channel with unsynced state. We
  1810. * should fix this here and give this VM a chance to use a properly
  1811. * synced channel
  1812. */
  1813. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  1814. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  1815. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  1816. priv->cmd.comm_toggle = 0;
  1817. return 0;
  1818. }
  1819. int mlx4_multi_func_init(struct mlx4_dev *dev)
  1820. {
  1821. struct mlx4_priv *priv = mlx4_priv(dev);
  1822. struct mlx4_slave_state *s_state;
  1823. int i, j, err, port;
  1824. if (mlx4_is_master(dev))
  1825. priv->mfunc.comm =
  1826. ioremap(pci_resource_start(dev->pdev, priv->fw.comm_bar) +
  1827. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  1828. else
  1829. priv->mfunc.comm =
  1830. ioremap(pci_resource_start(dev->pdev, 2) +
  1831. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  1832. if (!priv->mfunc.comm) {
  1833. mlx4_err(dev, "Couldn't map communication vector\n");
  1834. goto err_vhcr;
  1835. }
  1836. if (mlx4_is_master(dev)) {
  1837. priv->mfunc.master.slave_state =
  1838. kzalloc(dev->num_slaves *
  1839. sizeof(struct mlx4_slave_state), GFP_KERNEL);
  1840. if (!priv->mfunc.master.slave_state)
  1841. goto err_comm;
  1842. priv->mfunc.master.vf_admin =
  1843. kzalloc(dev->num_slaves *
  1844. sizeof(struct mlx4_vf_admin_state), GFP_KERNEL);
  1845. if (!priv->mfunc.master.vf_admin)
  1846. goto err_comm_admin;
  1847. priv->mfunc.master.vf_oper =
  1848. kzalloc(dev->num_slaves *
  1849. sizeof(struct mlx4_vf_oper_state), GFP_KERNEL);
  1850. if (!priv->mfunc.master.vf_oper)
  1851. goto err_comm_oper;
  1852. for (i = 0; i < dev->num_slaves; ++i) {
  1853. s_state = &priv->mfunc.master.slave_state[i];
  1854. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  1855. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  1856. s_state->event_eq[j].eqn = -1;
  1857. __raw_writel((__force u32) 0,
  1858. &priv->mfunc.comm[i].slave_write);
  1859. __raw_writel((__force u32) 0,
  1860. &priv->mfunc.comm[i].slave_read);
  1861. mmiowb();
  1862. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  1863. s_state->vlan_filter[port] =
  1864. kzalloc(sizeof(struct mlx4_vlan_fltr),
  1865. GFP_KERNEL);
  1866. if (!s_state->vlan_filter[port]) {
  1867. if (--port)
  1868. kfree(s_state->vlan_filter[port]);
  1869. goto err_slaves;
  1870. }
  1871. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  1872. priv->mfunc.master.vf_admin[i].vport[port].default_vlan = MLX4_VGT;
  1873. priv->mfunc.master.vf_oper[i].vport[port].state.default_vlan = MLX4_VGT;
  1874. priv->mfunc.master.vf_oper[i].vport[port].vlan_idx = NO_INDX;
  1875. priv->mfunc.master.vf_oper[i].vport[port].mac_idx = NO_INDX;
  1876. }
  1877. spin_lock_init(&s_state->lock);
  1878. }
  1879. memset(&priv->mfunc.master.cmd_eqe, 0, dev->caps.eqe_size);
  1880. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  1881. INIT_WORK(&priv->mfunc.master.comm_work,
  1882. mlx4_master_comm_channel);
  1883. INIT_WORK(&priv->mfunc.master.slave_event_work,
  1884. mlx4_gen_slave_eqe);
  1885. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  1886. mlx4_master_handle_slave_flr);
  1887. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  1888. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  1889. priv->mfunc.master.comm_wq =
  1890. create_singlethread_workqueue("mlx4_comm");
  1891. if (!priv->mfunc.master.comm_wq)
  1892. goto err_slaves;
  1893. if (mlx4_init_resource_tracker(dev))
  1894. goto err_thread;
  1895. err = mlx4_ARM_COMM_CHANNEL(dev);
  1896. if (err) {
  1897. mlx4_err(dev, " Failed to arm comm channel eq: %x\n",
  1898. err);
  1899. goto err_resource;
  1900. }
  1901. } else {
  1902. err = sync_toggles(dev);
  1903. if (err) {
  1904. mlx4_err(dev, "Couldn't sync toggles\n");
  1905. goto err_comm;
  1906. }
  1907. }
  1908. return 0;
  1909. err_resource:
  1910. mlx4_free_resource_tracker(dev, RES_TR_FREE_ALL);
  1911. err_thread:
  1912. flush_workqueue(priv->mfunc.master.comm_wq);
  1913. destroy_workqueue(priv->mfunc.master.comm_wq);
  1914. err_slaves:
  1915. while (--i) {
  1916. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1917. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1918. }
  1919. kfree(priv->mfunc.master.vf_oper);
  1920. err_comm_oper:
  1921. kfree(priv->mfunc.master.vf_admin);
  1922. err_comm_admin:
  1923. kfree(priv->mfunc.master.slave_state);
  1924. err_comm:
  1925. iounmap(priv->mfunc.comm);
  1926. err_vhcr:
  1927. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1928. priv->mfunc.vhcr,
  1929. priv->mfunc.vhcr_dma);
  1930. priv->mfunc.vhcr = NULL;
  1931. return -ENOMEM;
  1932. }
  1933. int mlx4_cmd_init(struct mlx4_dev *dev)
  1934. {
  1935. struct mlx4_priv *priv = mlx4_priv(dev);
  1936. int flags = 0;
  1937. if (!priv->cmd.initialized) {
  1938. mutex_init(&priv->cmd.hcr_mutex);
  1939. mutex_init(&priv->cmd.slave_cmd_mutex);
  1940. sema_init(&priv->cmd.poll_sem, 1);
  1941. priv->cmd.use_events = 0;
  1942. priv->cmd.toggle = 1;
  1943. priv->cmd.initialized = 1;
  1944. flags |= MLX4_CMD_CLEANUP_STRUCT;
  1945. }
  1946. if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
  1947. priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) +
  1948. MLX4_HCR_BASE, MLX4_HCR_SIZE);
  1949. if (!priv->cmd.hcr) {
  1950. mlx4_err(dev, "Couldn't map command register\n");
  1951. goto err;
  1952. }
  1953. flags |= MLX4_CMD_CLEANUP_HCR;
  1954. }
  1955. if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
  1956. priv->mfunc.vhcr = dma_alloc_coherent(&(dev->pdev->dev), PAGE_SIZE,
  1957. &priv->mfunc.vhcr_dma,
  1958. GFP_KERNEL);
  1959. if (!priv->mfunc.vhcr)
  1960. goto err;
  1961. flags |= MLX4_CMD_CLEANUP_VHCR;
  1962. }
  1963. if (!priv->cmd.pool) {
  1964. priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
  1965. MLX4_MAILBOX_SIZE,
  1966. MLX4_MAILBOX_SIZE, 0);
  1967. if (!priv->cmd.pool)
  1968. goto err;
  1969. flags |= MLX4_CMD_CLEANUP_POOL;
  1970. }
  1971. return 0;
  1972. err:
  1973. mlx4_cmd_cleanup(dev, flags);
  1974. return -ENOMEM;
  1975. }
  1976. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  1977. {
  1978. struct mlx4_priv *priv = mlx4_priv(dev);
  1979. int i, port;
  1980. if (mlx4_is_master(dev)) {
  1981. flush_workqueue(priv->mfunc.master.comm_wq);
  1982. destroy_workqueue(priv->mfunc.master.comm_wq);
  1983. for (i = 0; i < dev->num_slaves; i++) {
  1984. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  1985. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  1986. }
  1987. kfree(priv->mfunc.master.slave_state);
  1988. kfree(priv->mfunc.master.vf_admin);
  1989. kfree(priv->mfunc.master.vf_oper);
  1990. }
  1991. iounmap(priv->mfunc.comm);
  1992. }
  1993. void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
  1994. {
  1995. struct mlx4_priv *priv = mlx4_priv(dev);
  1996. if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
  1997. pci_pool_destroy(priv->cmd.pool);
  1998. priv->cmd.pool = NULL;
  1999. }
  2000. if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
  2001. (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
  2002. iounmap(priv->cmd.hcr);
  2003. priv->cmd.hcr = NULL;
  2004. }
  2005. if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
  2006. (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
  2007. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  2008. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  2009. priv->mfunc.vhcr = NULL;
  2010. }
  2011. if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
  2012. priv->cmd.initialized = 0;
  2013. }
  2014. /*
  2015. * Switch to using events to issue FW commands (can only be called
  2016. * after event queue for command events has been initialized).
  2017. */
  2018. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  2019. {
  2020. struct mlx4_priv *priv = mlx4_priv(dev);
  2021. int i;
  2022. int err = 0;
  2023. priv->cmd.context = kmalloc(priv->cmd.max_cmds *
  2024. sizeof (struct mlx4_cmd_context),
  2025. GFP_KERNEL);
  2026. if (!priv->cmd.context)
  2027. return -ENOMEM;
  2028. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2029. priv->cmd.context[i].token = i;
  2030. priv->cmd.context[i].next = i + 1;
  2031. }
  2032. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  2033. priv->cmd.free_head = 0;
  2034. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  2035. spin_lock_init(&priv->cmd.context_lock);
  2036. for (priv->cmd.token_mask = 1;
  2037. priv->cmd.token_mask < priv->cmd.max_cmds;
  2038. priv->cmd.token_mask <<= 1)
  2039. ; /* nothing */
  2040. --priv->cmd.token_mask;
  2041. down(&priv->cmd.poll_sem);
  2042. priv->cmd.use_events = 1;
  2043. return err;
  2044. }
  2045. /*
  2046. * Switch back to polling (used when shutting down the device)
  2047. */
  2048. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  2049. {
  2050. struct mlx4_priv *priv = mlx4_priv(dev);
  2051. int i;
  2052. priv->cmd.use_events = 0;
  2053. for (i = 0; i < priv->cmd.max_cmds; ++i)
  2054. down(&priv->cmd.event_sem);
  2055. kfree(priv->cmd.context);
  2056. up(&priv->cmd.poll_sem);
  2057. }
  2058. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  2059. {
  2060. struct mlx4_cmd_mailbox *mailbox;
  2061. mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
  2062. if (!mailbox)
  2063. return ERR_PTR(-ENOMEM);
  2064. mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  2065. &mailbox->dma);
  2066. if (!mailbox->buf) {
  2067. kfree(mailbox);
  2068. return ERR_PTR(-ENOMEM);
  2069. }
  2070. memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
  2071. return mailbox;
  2072. }
  2073. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  2074. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  2075. struct mlx4_cmd_mailbox *mailbox)
  2076. {
  2077. if (!mailbox)
  2078. return;
  2079. pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  2080. kfree(mailbox);
  2081. }
  2082. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  2083. u32 mlx4_comm_get_version(void)
  2084. {
  2085. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  2086. }
  2087. static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
  2088. {
  2089. if ((vf < 0) || (vf >= dev->num_vfs)) {
  2090. mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n", vf, dev->num_vfs);
  2091. return -EINVAL;
  2092. }
  2093. return vf+1;
  2094. }
  2095. int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
  2096. {
  2097. if (slave < 1 || slave > dev->num_vfs) {
  2098. mlx4_err(dev,
  2099. "Bad slave number:%d (number of activated slaves: %lu)\n",
  2100. slave, dev->num_slaves);
  2101. return -EINVAL;
  2102. }
  2103. return slave - 1;
  2104. }
  2105. struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
  2106. {
  2107. struct mlx4_active_ports actv_ports;
  2108. int vf;
  2109. bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
  2110. if (slave == 0) {
  2111. bitmap_fill(actv_ports.ports, dev->caps.num_ports);
  2112. return actv_ports;
  2113. }
  2114. vf = mlx4_get_vf_indx(dev, slave);
  2115. if (vf < 0)
  2116. return actv_ports;
  2117. bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
  2118. min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
  2119. dev->caps.num_ports));
  2120. return actv_ports;
  2121. }
  2122. EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
  2123. int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
  2124. {
  2125. unsigned n;
  2126. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2127. unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2128. if (port <= 0 || port > m)
  2129. return -EINVAL;
  2130. n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2131. if (port <= n)
  2132. port = n + 1;
  2133. return port;
  2134. }
  2135. EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
  2136. int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
  2137. {
  2138. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2139. if (test_bit(port - 1, actv_ports.ports))
  2140. return port -
  2141. find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2142. return -1;
  2143. }
  2144. EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
  2145. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
  2146. int port)
  2147. {
  2148. unsigned i;
  2149. struct mlx4_slaves_pport slaves_pport;
  2150. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2151. if (port <= 0 || port > dev->caps.num_ports)
  2152. return slaves_pport;
  2153. for (i = 0; i < dev->num_vfs + 1; i++) {
  2154. struct mlx4_active_ports actv_ports =
  2155. mlx4_get_active_ports(dev, i);
  2156. if (test_bit(port - 1, actv_ports.ports))
  2157. set_bit(i, slaves_pport.slaves);
  2158. }
  2159. return slaves_pport;
  2160. }
  2161. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
  2162. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
  2163. struct mlx4_dev *dev,
  2164. const struct mlx4_active_ports *crit_ports)
  2165. {
  2166. unsigned i;
  2167. struct mlx4_slaves_pport slaves_pport;
  2168. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2169. for (i = 0; i < dev->num_vfs + 1; i++) {
  2170. struct mlx4_active_ports actv_ports =
  2171. mlx4_get_active_ports(dev, i);
  2172. if (bitmap_equal(crit_ports->ports, actv_ports.ports,
  2173. dev->caps.num_ports))
  2174. set_bit(i, slaves_pport.slaves);
  2175. }
  2176. return slaves_pport;
  2177. }
  2178. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
  2179. static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
  2180. {
  2181. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2182. int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
  2183. + 1;
  2184. int max_port = min_port +
  2185. bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2186. if (port < min_port)
  2187. port = min_port;
  2188. else if (port >= max_port)
  2189. port = max_port - 1;
  2190. return port;
  2191. }
  2192. int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u64 mac)
  2193. {
  2194. struct mlx4_priv *priv = mlx4_priv(dev);
  2195. struct mlx4_vport_state *s_info;
  2196. int slave;
  2197. if (!mlx4_is_master(dev))
  2198. return -EPROTONOSUPPORT;
  2199. slave = mlx4_get_slave_indx(dev, vf);
  2200. if (slave < 0)
  2201. return -EINVAL;
  2202. port = mlx4_slaves_closest_port(dev, slave, port);
  2203. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2204. s_info->mac = mac;
  2205. mlx4_info(dev, "default mac on vf %d port %d to %llX will take afect only after vf restart\n",
  2206. vf, port, s_info->mac);
  2207. return 0;
  2208. }
  2209. EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
  2210. int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos)
  2211. {
  2212. struct mlx4_priv *priv = mlx4_priv(dev);
  2213. struct mlx4_vport_state *vf_admin;
  2214. int slave;
  2215. if ((!mlx4_is_master(dev)) ||
  2216. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2217. return -EPROTONOSUPPORT;
  2218. if ((vlan > 4095) || (qos > 7))
  2219. return -EINVAL;
  2220. slave = mlx4_get_slave_indx(dev, vf);
  2221. if (slave < 0)
  2222. return -EINVAL;
  2223. port = mlx4_slaves_closest_port(dev, slave, port);
  2224. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2225. if ((0 == vlan) && (0 == qos))
  2226. vf_admin->default_vlan = MLX4_VGT;
  2227. else
  2228. vf_admin->default_vlan = vlan;
  2229. vf_admin->default_qos = qos;
  2230. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2231. mlx4_info(dev,
  2232. "updating vf %d port %d config will take effect on next VF restart\n",
  2233. vf, port);
  2234. return 0;
  2235. }
  2236. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2237. /* mlx4_get_slave_default_vlan -
  2238. * return true if VST ( default vlan)
  2239. * if VST, will return vlan & qos (if not NULL)
  2240. */
  2241. bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
  2242. u16 *vlan, u8 *qos)
  2243. {
  2244. struct mlx4_vport_oper_state *vp_oper;
  2245. struct mlx4_priv *priv;
  2246. priv = mlx4_priv(dev);
  2247. port = mlx4_slaves_closest_port(dev, slave, port);
  2248. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2249. if (MLX4_VGT != vp_oper->state.default_vlan) {
  2250. if (vlan)
  2251. *vlan = vp_oper->state.default_vlan;
  2252. if (qos)
  2253. *qos = vp_oper->state.default_qos;
  2254. return true;
  2255. }
  2256. return false;
  2257. }
  2258. EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
  2259. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2260. {
  2261. struct mlx4_priv *priv = mlx4_priv(dev);
  2262. struct mlx4_vport_state *s_info;
  2263. int slave;
  2264. if ((!mlx4_is_master(dev)) ||
  2265. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  2266. return -EPROTONOSUPPORT;
  2267. slave = mlx4_get_slave_indx(dev, vf);
  2268. if (slave < 0)
  2269. return -EINVAL;
  2270. port = mlx4_slaves_closest_port(dev, slave, port);
  2271. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2272. s_info->spoofchk = setting;
  2273. return 0;
  2274. }
  2275. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2276. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2277. {
  2278. struct mlx4_priv *priv = mlx4_priv(dev);
  2279. struct mlx4_vport_state *s_info;
  2280. int slave;
  2281. if (!mlx4_is_master(dev))
  2282. return -EPROTONOSUPPORT;
  2283. slave = mlx4_get_slave_indx(dev, vf);
  2284. if (slave < 0)
  2285. return -EINVAL;
  2286. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2287. ivf->vf = vf;
  2288. /* need to convert it to a func */
  2289. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2290. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2291. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2292. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2293. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2294. ivf->mac[5] = ((s_info->mac) & 0xff);
  2295. ivf->vlan = s_info->default_vlan;
  2296. ivf->qos = s_info->default_qos;
  2297. ivf->max_tx_rate = s_info->tx_rate;
  2298. ivf->min_tx_rate = 0;
  2299. ivf->spoofchk = s_info->spoofchk;
  2300. ivf->linkstate = s_info->link_state;
  2301. return 0;
  2302. }
  2303. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2304. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2305. {
  2306. struct mlx4_priv *priv = mlx4_priv(dev);
  2307. struct mlx4_vport_state *s_info;
  2308. int slave;
  2309. u8 link_stat_event;
  2310. slave = mlx4_get_slave_indx(dev, vf);
  2311. if (slave < 0)
  2312. return -EINVAL;
  2313. port = mlx4_slaves_closest_port(dev, slave, port);
  2314. switch (link_state) {
  2315. case IFLA_VF_LINK_STATE_AUTO:
  2316. /* get current link state */
  2317. if (!priv->sense.do_sense_port[port])
  2318. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2319. else
  2320. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2321. break;
  2322. case IFLA_VF_LINK_STATE_ENABLE:
  2323. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2324. break;
  2325. case IFLA_VF_LINK_STATE_DISABLE:
  2326. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2327. break;
  2328. default:
  2329. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2330. link_state, slave, port);
  2331. return -EINVAL;
  2332. };
  2333. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2334. s_info->link_state = link_state;
  2335. /* send event */
  2336. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2337. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2338. mlx4_dbg(dev,
  2339. "updating vf %d port %d no link state HW enforcment\n",
  2340. vf, port);
  2341. return 0;
  2342. }
  2343. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
  2344. int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
  2345. {
  2346. struct mlx4_priv *priv = mlx4_priv(dev);
  2347. if (slave < 1 || slave >= dev->num_slaves ||
  2348. port < 1 || port > MLX4_MAX_PORTS)
  2349. return 0;
  2350. return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
  2351. MLX4_VF_SMI_ENABLED;
  2352. }
  2353. EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
  2354. int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
  2355. {
  2356. struct mlx4_priv *priv = mlx4_priv(dev);
  2357. if (slave == mlx4_master_func_num(dev))
  2358. return 1;
  2359. if (slave < 1 || slave >= dev->num_slaves ||
  2360. port < 1 || port > MLX4_MAX_PORTS)
  2361. return 0;
  2362. return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
  2363. MLX4_VF_SMI_ENABLED;
  2364. }
  2365. EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
  2366. int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
  2367. int enabled)
  2368. {
  2369. struct mlx4_priv *priv = mlx4_priv(dev);
  2370. if (slave == mlx4_master_func_num(dev))
  2371. return 0;
  2372. if (slave < 1 || slave >= dev->num_slaves ||
  2373. port < 1 || port > MLX4_MAX_PORTS ||
  2374. enabled < 0 || enabled > 1)
  2375. return -EINVAL;
  2376. priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
  2377. return 0;
  2378. }
  2379. EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);