cmd.c 69 KB

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