cmd.c 61 KB

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