cmd.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431
  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 <linux/delay.h>
  44. #include <linux/etherdevice.h>
  45. #include <asm/io.h>
  46. #include "mlx4.h"
  47. #include "fw.h"
  48. #include "fw_qos.h"
  49. #include "mlx4_stats.h"
  50. #define CMD_POLL_TOKEN 0xffff
  51. #define INBOX_MASK 0xffffffffffffff00ULL
  52. #define CMD_CHAN_VER 1
  53. #define CMD_CHAN_IF_REV 1
  54. enum {
  55. /* command completed successfully: */
  56. CMD_STAT_OK = 0x00,
  57. /* Internal error (such as a bus error) occurred while processing command: */
  58. CMD_STAT_INTERNAL_ERR = 0x01,
  59. /* Operation/command not supported or opcode modifier not supported: */
  60. CMD_STAT_BAD_OP = 0x02,
  61. /* Parameter not supported or parameter out of range: */
  62. CMD_STAT_BAD_PARAM = 0x03,
  63. /* System not enabled or bad system state: */
  64. CMD_STAT_BAD_SYS_STATE = 0x04,
  65. /* Attempt to access reserved or unallocaterd resource: */
  66. CMD_STAT_BAD_RESOURCE = 0x05,
  67. /* Requested resource is currently executing a command, or is otherwise busy: */
  68. CMD_STAT_RESOURCE_BUSY = 0x06,
  69. /* Required capability exceeds device limits: */
  70. CMD_STAT_EXCEED_LIM = 0x08,
  71. /* Resource is not in the appropriate state or ownership: */
  72. CMD_STAT_BAD_RES_STATE = 0x09,
  73. /* Index out of range: */
  74. CMD_STAT_BAD_INDEX = 0x0a,
  75. /* FW image corrupted: */
  76. CMD_STAT_BAD_NVMEM = 0x0b,
  77. /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
  78. CMD_STAT_ICM_ERROR = 0x0c,
  79. /* Attempt to modify a QP/EE which is not in the presumed state: */
  80. CMD_STAT_BAD_QP_STATE = 0x10,
  81. /* Bad segment parameters (Address/Size): */
  82. CMD_STAT_BAD_SEG_PARAM = 0x20,
  83. /* Memory Region has Memory Windows bound to: */
  84. CMD_STAT_REG_BOUND = 0x21,
  85. /* HCA local attached memory not present: */
  86. CMD_STAT_LAM_NOT_PRE = 0x22,
  87. /* Bad management packet (silently discarded): */
  88. CMD_STAT_BAD_PKT = 0x30,
  89. /* More outstanding CQEs in CQ than new CQ size: */
  90. CMD_STAT_BAD_SIZE = 0x40,
  91. /* Multi Function device support required: */
  92. CMD_STAT_MULTI_FUNC_REQ = 0x50,
  93. };
  94. enum {
  95. HCR_IN_PARAM_OFFSET = 0x00,
  96. HCR_IN_MODIFIER_OFFSET = 0x08,
  97. HCR_OUT_PARAM_OFFSET = 0x0c,
  98. HCR_TOKEN_OFFSET = 0x14,
  99. HCR_STATUS_OFFSET = 0x18,
  100. HCR_OPMOD_SHIFT = 12,
  101. HCR_T_BIT = 21,
  102. HCR_E_BIT = 22,
  103. HCR_GO_BIT = 23
  104. };
  105. enum {
  106. GO_BIT_TIMEOUT_MSECS = 10000
  107. };
  108. enum mlx4_vlan_transition {
  109. MLX4_VLAN_TRANSITION_VST_VST = 0,
  110. MLX4_VLAN_TRANSITION_VST_VGT = 1,
  111. MLX4_VLAN_TRANSITION_VGT_VST = 2,
  112. MLX4_VLAN_TRANSITION_VGT_VGT = 3,
  113. };
  114. struct mlx4_cmd_context {
  115. struct completion done;
  116. int result;
  117. int next;
  118. u64 out_param;
  119. u16 token;
  120. u8 fw_status;
  121. };
  122. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  123. struct mlx4_vhcr_cmd *in_vhcr);
  124. static int mlx4_status_to_errno(u8 status)
  125. {
  126. static const int trans_table[] = {
  127. [CMD_STAT_INTERNAL_ERR] = -EIO,
  128. [CMD_STAT_BAD_OP] = -EPERM,
  129. [CMD_STAT_BAD_PARAM] = -EINVAL,
  130. [CMD_STAT_BAD_SYS_STATE] = -ENXIO,
  131. [CMD_STAT_BAD_RESOURCE] = -EBADF,
  132. [CMD_STAT_RESOURCE_BUSY] = -EBUSY,
  133. [CMD_STAT_EXCEED_LIM] = -ENOMEM,
  134. [CMD_STAT_BAD_RES_STATE] = -EBADF,
  135. [CMD_STAT_BAD_INDEX] = -EBADF,
  136. [CMD_STAT_BAD_NVMEM] = -EFAULT,
  137. [CMD_STAT_ICM_ERROR] = -ENFILE,
  138. [CMD_STAT_BAD_QP_STATE] = -EINVAL,
  139. [CMD_STAT_BAD_SEG_PARAM] = -EFAULT,
  140. [CMD_STAT_REG_BOUND] = -EBUSY,
  141. [CMD_STAT_LAM_NOT_PRE] = -EAGAIN,
  142. [CMD_STAT_BAD_PKT] = -EINVAL,
  143. [CMD_STAT_BAD_SIZE] = -ENOMEM,
  144. [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
  145. };
  146. if (status >= ARRAY_SIZE(trans_table) ||
  147. (status != CMD_STAT_OK && trans_table[status] == 0))
  148. return -EIO;
  149. return trans_table[status];
  150. }
  151. static u8 mlx4_errno_to_status(int errno)
  152. {
  153. switch (errno) {
  154. case -EPERM:
  155. return CMD_STAT_BAD_OP;
  156. case -EINVAL:
  157. return CMD_STAT_BAD_PARAM;
  158. case -ENXIO:
  159. return CMD_STAT_BAD_SYS_STATE;
  160. case -EBUSY:
  161. return CMD_STAT_RESOURCE_BUSY;
  162. case -ENOMEM:
  163. return CMD_STAT_EXCEED_LIM;
  164. case -ENFILE:
  165. return CMD_STAT_ICM_ERROR;
  166. default:
  167. return CMD_STAT_INTERNAL_ERR;
  168. }
  169. }
  170. static int mlx4_internal_err_ret_value(struct mlx4_dev *dev, u16 op,
  171. u8 op_modifier)
  172. {
  173. switch (op) {
  174. case MLX4_CMD_UNMAP_ICM:
  175. case MLX4_CMD_UNMAP_ICM_AUX:
  176. case MLX4_CMD_UNMAP_FA:
  177. case MLX4_CMD_2RST_QP:
  178. case MLX4_CMD_HW2SW_EQ:
  179. case MLX4_CMD_HW2SW_CQ:
  180. case MLX4_CMD_HW2SW_SRQ:
  181. case MLX4_CMD_HW2SW_MPT:
  182. case MLX4_CMD_CLOSE_HCA:
  183. case MLX4_QP_FLOW_STEERING_DETACH:
  184. case MLX4_CMD_FREE_RES:
  185. case MLX4_CMD_CLOSE_PORT:
  186. return CMD_STAT_OK;
  187. case MLX4_CMD_QP_ATTACH:
  188. /* On Detach case return success */
  189. if (op_modifier == 0)
  190. return CMD_STAT_OK;
  191. return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  192. default:
  193. return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  194. }
  195. }
  196. static int mlx4_closing_cmd_fatal_error(u16 op, u8 fw_status)
  197. {
  198. /* Any error during the closing commands below is considered fatal */
  199. if (op == MLX4_CMD_CLOSE_HCA ||
  200. op == MLX4_CMD_HW2SW_EQ ||
  201. op == MLX4_CMD_HW2SW_CQ ||
  202. op == MLX4_CMD_2RST_QP ||
  203. op == MLX4_CMD_HW2SW_SRQ ||
  204. op == MLX4_CMD_SYNC_TPT ||
  205. op == MLX4_CMD_UNMAP_ICM ||
  206. op == MLX4_CMD_UNMAP_ICM_AUX ||
  207. op == MLX4_CMD_UNMAP_FA)
  208. return 1;
  209. /* Error on MLX4_CMD_HW2SW_MPT is fatal except when fw status equals
  210. * CMD_STAT_REG_BOUND.
  211. * This status indicates that memory region has memory windows bound to it
  212. * which may result from invalid user space usage and is not fatal.
  213. */
  214. if (op == MLX4_CMD_HW2SW_MPT && fw_status != CMD_STAT_REG_BOUND)
  215. return 1;
  216. return 0;
  217. }
  218. static int mlx4_cmd_reset_flow(struct mlx4_dev *dev, u16 op, u8 op_modifier,
  219. int err)
  220. {
  221. /* Only if reset flow is really active return code is based on
  222. * command, otherwise current error code is returned.
  223. */
  224. if (mlx4_internal_err_reset) {
  225. mlx4_enter_error_state(dev->persist);
  226. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  227. }
  228. return err;
  229. }
  230. static int comm_pending(struct mlx4_dev *dev)
  231. {
  232. struct mlx4_priv *priv = mlx4_priv(dev);
  233. u32 status = readl(&priv->mfunc.comm->slave_read);
  234. return (swab32(status) >> 31) != priv->cmd.comm_toggle;
  235. }
  236. static int mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
  237. {
  238. struct mlx4_priv *priv = mlx4_priv(dev);
  239. u32 val;
  240. /* To avoid writing to unknown addresses after the device state was
  241. * changed to internal error and the function was rest,
  242. * check the INTERNAL_ERROR flag which is updated under
  243. * device_state_mutex lock.
  244. */
  245. mutex_lock(&dev->persist->device_state_mutex);
  246. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  247. mutex_unlock(&dev->persist->device_state_mutex);
  248. return -EIO;
  249. }
  250. priv->cmd.comm_toggle ^= 1;
  251. val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
  252. __raw_writel((__force u32) cpu_to_be32(val),
  253. &priv->mfunc.comm->slave_write);
  254. mutex_unlock(&dev->persist->device_state_mutex);
  255. return 0;
  256. }
  257. static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
  258. unsigned long timeout)
  259. {
  260. struct mlx4_priv *priv = mlx4_priv(dev);
  261. unsigned long end;
  262. int err = 0;
  263. int ret_from_pending = 0;
  264. /* First, verify that the master reports correct status */
  265. if (comm_pending(dev)) {
  266. mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
  267. priv->cmd.comm_toggle, cmd);
  268. return -EAGAIN;
  269. }
  270. /* Write command */
  271. down(&priv->cmd.poll_sem);
  272. if (mlx4_comm_cmd_post(dev, cmd, param)) {
  273. /* Only in case the device state is INTERNAL_ERROR,
  274. * mlx4_comm_cmd_post returns with an error
  275. */
  276. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  277. goto out;
  278. }
  279. end = msecs_to_jiffies(timeout) + jiffies;
  280. while (comm_pending(dev) && time_before(jiffies, end))
  281. cond_resched();
  282. ret_from_pending = comm_pending(dev);
  283. if (ret_from_pending) {
  284. /* check if the slave is trying to boot in the middle of
  285. * FLR process. The only non-zero result in the RESET command
  286. * is MLX4_DELAY_RESET_SLAVE*/
  287. if ((MLX4_COMM_CMD_RESET == cmd)) {
  288. err = MLX4_DELAY_RESET_SLAVE;
  289. goto out;
  290. } else {
  291. mlx4_warn(dev, "Communication channel command 0x%x timed out\n",
  292. cmd);
  293. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  294. }
  295. }
  296. if (err)
  297. mlx4_enter_error_state(dev->persist);
  298. out:
  299. up(&priv->cmd.poll_sem);
  300. return err;
  301. }
  302. static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 vhcr_cmd,
  303. u16 param, u16 op, unsigned long timeout)
  304. {
  305. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  306. struct mlx4_cmd_context *context;
  307. unsigned long end;
  308. int err = 0;
  309. down(&cmd->event_sem);
  310. spin_lock(&cmd->context_lock);
  311. BUG_ON(cmd->free_head < 0);
  312. context = &cmd->context[cmd->free_head];
  313. context->token += cmd->token_mask + 1;
  314. cmd->free_head = context->next;
  315. spin_unlock(&cmd->context_lock);
  316. reinit_completion(&context->done);
  317. if (mlx4_comm_cmd_post(dev, vhcr_cmd, param)) {
  318. /* Only in case the device state is INTERNAL_ERROR,
  319. * mlx4_comm_cmd_post returns with an error
  320. */
  321. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  322. goto out;
  323. }
  324. if (!wait_for_completion_timeout(&context->done,
  325. msecs_to_jiffies(timeout))) {
  326. mlx4_warn(dev, "communication channel command 0x%x (op=0x%x) timed out\n",
  327. vhcr_cmd, op);
  328. goto out_reset;
  329. }
  330. err = context->result;
  331. if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
  332. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  333. vhcr_cmd, context->fw_status);
  334. if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
  335. goto out_reset;
  336. }
  337. /* wait for comm channel ready
  338. * this is necessary for prevention the race
  339. * when switching between event to polling mode
  340. * Skipping this section in case the device is in FATAL_ERROR state,
  341. * In this state, no commands are sent via the comm channel until
  342. * the device has returned from reset.
  343. */
  344. if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
  345. end = msecs_to_jiffies(timeout) + jiffies;
  346. while (comm_pending(dev) && time_before(jiffies, end))
  347. cond_resched();
  348. }
  349. goto out;
  350. out_reset:
  351. err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  352. mlx4_enter_error_state(dev->persist);
  353. out:
  354. spin_lock(&cmd->context_lock);
  355. context->next = cmd->free_head;
  356. cmd->free_head = context - cmd->context;
  357. spin_unlock(&cmd->context_lock);
  358. up(&cmd->event_sem);
  359. return err;
  360. }
  361. int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
  362. u16 op, unsigned long timeout)
  363. {
  364. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  365. return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  366. if (mlx4_priv(dev)->cmd.use_events)
  367. return mlx4_comm_cmd_wait(dev, cmd, param, op, timeout);
  368. return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
  369. }
  370. static int cmd_pending(struct mlx4_dev *dev)
  371. {
  372. u32 status;
  373. if (pci_channel_offline(dev->persist->pdev))
  374. return -EIO;
  375. status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
  376. return (status & swab32(1 << HCR_GO_BIT)) ||
  377. (mlx4_priv(dev)->cmd.toggle ==
  378. !!(status & swab32(1 << HCR_T_BIT)));
  379. }
  380. static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
  381. u32 in_modifier, u8 op_modifier, u16 op, u16 token,
  382. int event)
  383. {
  384. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  385. u32 __iomem *hcr = cmd->hcr;
  386. int ret = -EIO;
  387. unsigned long end;
  388. mutex_lock(&dev->persist->device_state_mutex);
  389. /* To avoid writing to unknown addresses after the device state was
  390. * changed to internal error and the chip was reset,
  391. * check the INTERNAL_ERROR flag which is updated under
  392. * device_state_mutex lock.
  393. */
  394. if (pci_channel_offline(dev->persist->pdev) ||
  395. (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
  396. /*
  397. * Device is going through error recovery
  398. * and cannot accept commands.
  399. */
  400. goto out;
  401. }
  402. end = jiffies;
  403. if (event)
  404. end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
  405. while (cmd_pending(dev)) {
  406. if (pci_channel_offline(dev->persist->pdev)) {
  407. /*
  408. * Device is going through error recovery
  409. * and cannot accept commands.
  410. */
  411. goto out;
  412. }
  413. if (time_after_eq(jiffies, end)) {
  414. mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
  415. goto out;
  416. }
  417. cond_resched();
  418. }
  419. /*
  420. * We use writel (instead of something like memcpy_toio)
  421. * because writes of less than 32 bits to the HCR don't work
  422. * (and some architectures such as ia64 implement memcpy_toio
  423. * in terms of writeb).
  424. */
  425. __raw_writel((__force u32) cpu_to_be32(in_param >> 32), hcr + 0);
  426. __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), hcr + 1);
  427. __raw_writel((__force u32) cpu_to_be32(in_modifier), hcr + 2);
  428. __raw_writel((__force u32) cpu_to_be32(out_param >> 32), hcr + 3);
  429. __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
  430. __raw_writel((__force u32) cpu_to_be32(token << 16), hcr + 5);
  431. /* __raw_writel may not order writes. */
  432. wmb();
  433. __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
  434. (cmd->toggle << HCR_T_BIT) |
  435. (event ? (1 << HCR_E_BIT) : 0) |
  436. (op_modifier << HCR_OPMOD_SHIFT) |
  437. op), hcr + 6);
  438. cmd->toggle = cmd->toggle ^ 1;
  439. ret = 0;
  440. out:
  441. if (ret)
  442. mlx4_warn(dev, "Could not post command 0x%x: ret=%d, in_param=0x%llx, in_mod=0x%x, op_mod=0x%x\n",
  443. op, ret, in_param, in_modifier, op_modifier);
  444. mutex_unlock(&dev->persist->device_state_mutex);
  445. return ret;
  446. }
  447. static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  448. int out_is_imm, u32 in_modifier, u8 op_modifier,
  449. u16 op, unsigned long timeout)
  450. {
  451. struct mlx4_priv *priv = mlx4_priv(dev);
  452. struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
  453. int ret;
  454. mutex_lock(&priv->cmd.slave_cmd_mutex);
  455. vhcr->in_param = cpu_to_be64(in_param);
  456. vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
  457. vhcr->in_modifier = cpu_to_be32(in_modifier);
  458. vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
  459. vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
  460. vhcr->status = 0;
  461. vhcr->flags = !!(priv->cmd.use_events) << 6;
  462. if (mlx4_is_master(dev)) {
  463. ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
  464. if (!ret) {
  465. if (out_is_imm) {
  466. if (out_param)
  467. *out_param =
  468. be64_to_cpu(vhcr->out_param);
  469. else {
  470. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  471. op);
  472. vhcr->status = CMD_STAT_BAD_PARAM;
  473. }
  474. }
  475. ret = mlx4_status_to_errno(vhcr->status);
  476. }
  477. if (ret &&
  478. dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  479. ret = mlx4_internal_err_ret_value(dev, op, op_modifier);
  480. } else {
  481. ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0, op,
  482. MLX4_COMM_TIME + timeout);
  483. if (!ret) {
  484. if (out_is_imm) {
  485. if (out_param)
  486. *out_param =
  487. be64_to_cpu(vhcr->out_param);
  488. else {
  489. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  490. op);
  491. vhcr->status = CMD_STAT_BAD_PARAM;
  492. }
  493. }
  494. ret = mlx4_status_to_errno(vhcr->status);
  495. } else {
  496. if (dev->persist->state &
  497. MLX4_DEVICE_STATE_INTERNAL_ERROR)
  498. ret = mlx4_internal_err_ret_value(dev, op,
  499. op_modifier);
  500. else
  501. mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n", op);
  502. }
  503. }
  504. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  505. return ret;
  506. }
  507. static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  508. int out_is_imm, u32 in_modifier, u8 op_modifier,
  509. u16 op, unsigned long timeout)
  510. {
  511. struct mlx4_priv *priv = mlx4_priv(dev);
  512. void __iomem *hcr = priv->cmd.hcr;
  513. int err = 0;
  514. unsigned long end;
  515. u32 stat;
  516. down(&priv->cmd.poll_sem);
  517. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  518. /*
  519. * Device is going through error recovery
  520. * and cannot accept commands.
  521. */
  522. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  523. goto out;
  524. }
  525. if (out_is_imm && !out_param) {
  526. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  527. op);
  528. err = -EINVAL;
  529. goto out;
  530. }
  531. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  532. in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
  533. if (err)
  534. goto out_reset;
  535. end = msecs_to_jiffies(timeout) + jiffies;
  536. while (cmd_pending(dev) && time_before(jiffies, end)) {
  537. if (pci_channel_offline(dev->persist->pdev)) {
  538. /*
  539. * Device is going through error recovery
  540. * and cannot accept commands.
  541. */
  542. err = -EIO;
  543. goto out_reset;
  544. }
  545. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  546. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  547. goto out;
  548. }
  549. cond_resched();
  550. }
  551. if (cmd_pending(dev)) {
  552. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  553. op);
  554. err = -EIO;
  555. goto out_reset;
  556. }
  557. if (out_is_imm)
  558. *out_param =
  559. (u64) be32_to_cpu((__force __be32)
  560. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
  561. (u64) be32_to_cpu((__force __be32)
  562. __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
  563. stat = be32_to_cpu((__force __be32)
  564. __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
  565. err = mlx4_status_to_errno(stat);
  566. if (err) {
  567. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  568. op, stat);
  569. if (mlx4_closing_cmd_fatal_error(op, stat))
  570. goto out_reset;
  571. goto out;
  572. }
  573. out_reset:
  574. if (err)
  575. err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
  576. out:
  577. up(&priv->cmd.poll_sem);
  578. return err;
  579. }
  580. void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
  581. {
  582. struct mlx4_priv *priv = mlx4_priv(dev);
  583. struct mlx4_cmd_context *context =
  584. &priv->cmd.context[token & priv->cmd.token_mask];
  585. /* previously timed out command completing at long last */
  586. if (token != context->token)
  587. return;
  588. context->fw_status = status;
  589. context->result = mlx4_status_to_errno(status);
  590. context->out_param = out_param;
  591. complete(&context->done);
  592. }
  593. static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  594. int out_is_imm, u32 in_modifier, u8 op_modifier,
  595. u16 op, unsigned long timeout)
  596. {
  597. struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
  598. struct mlx4_cmd_context *context;
  599. long ret_wait;
  600. int err = 0;
  601. down(&cmd->event_sem);
  602. spin_lock(&cmd->context_lock);
  603. BUG_ON(cmd->free_head < 0);
  604. context = &cmd->context[cmd->free_head];
  605. context->token += cmd->token_mask + 1;
  606. cmd->free_head = context->next;
  607. spin_unlock(&cmd->context_lock);
  608. if (out_is_imm && !out_param) {
  609. mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
  610. op);
  611. err = -EINVAL;
  612. goto out;
  613. }
  614. reinit_completion(&context->done);
  615. err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
  616. in_modifier, op_modifier, op, context->token, 1);
  617. if (err)
  618. goto out_reset;
  619. if (op == MLX4_CMD_SENSE_PORT) {
  620. ret_wait =
  621. wait_for_completion_interruptible_timeout(&context->done,
  622. msecs_to_jiffies(timeout));
  623. if (ret_wait < 0) {
  624. context->fw_status = 0;
  625. context->out_param = 0;
  626. context->result = 0;
  627. }
  628. } else {
  629. ret_wait = (long)wait_for_completion_timeout(&context->done,
  630. msecs_to_jiffies(timeout));
  631. }
  632. if (!ret_wait) {
  633. mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
  634. op);
  635. if (op == MLX4_CMD_NOP) {
  636. err = -EBUSY;
  637. goto out;
  638. } else {
  639. err = -EIO;
  640. goto out_reset;
  641. }
  642. }
  643. err = context->result;
  644. if (err) {
  645. /* Since we do not want to have this error message always
  646. * displayed at driver start when there are ConnectX2 HCAs
  647. * on the host, we deprecate the error message for this
  648. * specific command/input_mod/opcode_mod/fw-status to be debug.
  649. */
  650. if (op == MLX4_CMD_SET_PORT &&
  651. (in_modifier == 1 || in_modifier == 2) &&
  652. op_modifier == MLX4_SET_PORT_IB_OPCODE &&
  653. context->fw_status == CMD_STAT_BAD_SIZE)
  654. mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
  655. op, context->fw_status);
  656. else
  657. mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
  658. op, context->fw_status);
  659. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  660. err = mlx4_internal_err_ret_value(dev, op, op_modifier);
  661. else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
  662. goto out_reset;
  663. goto out;
  664. }
  665. if (out_is_imm)
  666. *out_param = context->out_param;
  667. out_reset:
  668. if (err)
  669. err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
  670. out:
  671. spin_lock(&cmd->context_lock);
  672. context->next = cmd->free_head;
  673. cmd->free_head = context - cmd->context;
  674. spin_unlock(&cmd->context_lock);
  675. up(&cmd->event_sem);
  676. return err;
  677. }
  678. int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
  679. int out_is_imm, u32 in_modifier, u8 op_modifier,
  680. u16 op, unsigned long timeout, int native)
  681. {
  682. if (pci_channel_offline(dev->persist->pdev))
  683. return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
  684. if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
  685. int ret;
  686. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  687. return mlx4_internal_err_ret_value(dev, op,
  688. op_modifier);
  689. down_read(&mlx4_priv(dev)->cmd.switch_sem);
  690. if (mlx4_priv(dev)->cmd.use_events)
  691. ret = mlx4_cmd_wait(dev, in_param, out_param,
  692. out_is_imm, in_modifier,
  693. op_modifier, op, timeout);
  694. else
  695. ret = mlx4_cmd_poll(dev, in_param, out_param,
  696. out_is_imm, in_modifier,
  697. op_modifier, op, timeout);
  698. up_read(&mlx4_priv(dev)->cmd.switch_sem);
  699. return ret;
  700. }
  701. return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
  702. in_modifier, op_modifier, op, timeout);
  703. }
  704. EXPORT_SYMBOL_GPL(__mlx4_cmd);
  705. int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
  706. {
  707. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
  708. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  709. }
  710. static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
  711. int slave, u64 slave_addr,
  712. int size, int is_read)
  713. {
  714. u64 in_param;
  715. u64 out_param;
  716. if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
  717. (slave & ~0x7f) | (size & 0xff)) {
  718. mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
  719. slave_addr, master_addr, slave, size);
  720. return -EINVAL;
  721. }
  722. if (is_read) {
  723. in_param = (u64) slave | slave_addr;
  724. out_param = (u64) dev->caps.function | master_addr;
  725. } else {
  726. in_param = (u64) dev->caps.function | master_addr;
  727. out_param = (u64) slave | slave_addr;
  728. }
  729. return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
  730. MLX4_CMD_ACCESS_MEM,
  731. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  732. }
  733. static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
  734. struct mlx4_cmd_mailbox *inbox,
  735. struct mlx4_cmd_mailbox *outbox)
  736. {
  737. struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
  738. struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
  739. int err;
  740. int i;
  741. if (index & 0x1f)
  742. return -EINVAL;
  743. in_mad->attr_mod = cpu_to_be32(index / 32);
  744. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
  745. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  746. MLX4_CMD_NATIVE);
  747. if (err)
  748. return err;
  749. for (i = 0; i < 32; ++i)
  750. pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
  751. return err;
  752. }
  753. static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
  754. struct mlx4_cmd_mailbox *inbox,
  755. struct mlx4_cmd_mailbox *outbox)
  756. {
  757. int i;
  758. int err;
  759. for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
  760. err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
  761. if (err)
  762. return err;
  763. }
  764. return 0;
  765. }
  766. #define PORT_CAPABILITY_LOCATION_IN_SMP 20
  767. #define PORT_STATE_OFFSET 32
  768. static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
  769. {
  770. if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
  771. return IB_PORT_ACTIVE;
  772. else
  773. return IB_PORT_DOWN;
  774. }
  775. static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
  776. struct mlx4_vhcr *vhcr,
  777. struct mlx4_cmd_mailbox *inbox,
  778. struct mlx4_cmd_mailbox *outbox,
  779. struct mlx4_cmd_info *cmd)
  780. {
  781. struct ib_smp *smp = inbox->buf;
  782. u32 index;
  783. u8 port, slave_port;
  784. u8 opcode_modifier;
  785. u16 *table;
  786. int err;
  787. int vidx, pidx;
  788. int network_view;
  789. struct mlx4_priv *priv = mlx4_priv(dev);
  790. struct ib_smp *outsmp = outbox->buf;
  791. __be16 *outtab = (__be16 *)(outsmp->data);
  792. __be32 slave_cap_mask;
  793. __be64 slave_node_guid;
  794. slave_port = vhcr->in_modifier;
  795. port = mlx4_slave_convert_port(dev, slave, slave_port);
  796. /* network-view bit is for driver use only, and should not be passed to FW */
  797. opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
  798. network_view = !!(vhcr->op_modifier & 0x8);
  799. if (smp->base_version == 1 &&
  800. smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  801. smp->class_version == 1) {
  802. /* host view is paravirtualized */
  803. if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
  804. if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  805. index = be32_to_cpu(smp->attr_mod);
  806. if (port < 1 || port > dev->caps.num_ports)
  807. return -EINVAL;
  808. table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
  809. sizeof(*table) * 32, GFP_KERNEL);
  810. if (!table)
  811. return -ENOMEM;
  812. /* need to get the full pkey table because the paravirtualized
  813. * pkeys may be scattered among several pkey blocks.
  814. */
  815. err = get_full_pkey_table(dev, port, table, inbox, outbox);
  816. if (!err) {
  817. for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
  818. pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
  819. outtab[vidx % 32] = cpu_to_be16(table[pidx]);
  820. }
  821. }
  822. kfree(table);
  823. return err;
  824. }
  825. if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
  826. /*get the slave specific caps:*/
  827. /*do the command */
  828. smp->attr_mod = cpu_to_be32(port);
  829. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  830. port, opcode_modifier,
  831. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  832. /* modify the response for slaves */
  833. if (!err && slave != mlx4_master_func_num(dev)) {
  834. u8 *state = outsmp->data + PORT_STATE_OFFSET;
  835. *state = (*state & 0xf0) | vf_port_state(dev, port, slave);
  836. slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
  837. memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
  838. }
  839. return err;
  840. }
  841. if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
  842. __be64 guid = mlx4_get_admin_guid(dev, slave,
  843. port);
  844. /* set the PF admin guid to the FW/HW burned
  845. * GUID, if it wasn't yet set
  846. */
  847. if (slave == 0 && guid == 0) {
  848. smp->attr_mod = 0;
  849. err = mlx4_cmd_box(dev,
  850. inbox->dma,
  851. outbox->dma,
  852. vhcr->in_modifier,
  853. opcode_modifier,
  854. vhcr->op,
  855. MLX4_CMD_TIME_CLASS_C,
  856. MLX4_CMD_NATIVE);
  857. if (err)
  858. return err;
  859. mlx4_set_admin_guid(dev,
  860. *(__be64 *)outsmp->
  861. data, slave, port);
  862. } else {
  863. memcpy(outsmp->data, &guid, 8);
  864. }
  865. /* clean all other gids */
  866. memset(outsmp->data + 8, 0, 56);
  867. return 0;
  868. }
  869. if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
  870. err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  871. port, opcode_modifier,
  872. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  873. if (!err) {
  874. slave_node_guid = mlx4_get_slave_node_guid(dev, slave);
  875. memcpy(outsmp->data + 12, &slave_node_guid, 8);
  876. }
  877. return err;
  878. }
  879. }
  880. }
  881. /* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
  882. * These are the MADs used by ib verbs (such as ib_query_gids).
  883. */
  884. if (slave != mlx4_master_func_num(dev) &&
  885. !mlx4_vf_smi_enabled(dev, slave, port)) {
  886. if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
  887. smp->method == IB_MGMT_METHOD_GET) || network_view) {
  888. 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",
  889. slave, smp->mgmt_class, smp->method,
  890. network_view ? "Network" : "Host",
  891. be16_to_cpu(smp->attr_id));
  892. return -EPERM;
  893. }
  894. }
  895. return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
  896. vhcr->in_modifier, opcode_modifier,
  897. vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  898. }
  899. static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
  900. struct mlx4_vhcr *vhcr,
  901. struct mlx4_cmd_mailbox *inbox,
  902. struct mlx4_cmd_mailbox *outbox,
  903. struct mlx4_cmd_info *cmd)
  904. {
  905. return -EPERM;
  906. }
  907. int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
  908. struct mlx4_vhcr *vhcr,
  909. struct mlx4_cmd_mailbox *inbox,
  910. struct mlx4_cmd_mailbox *outbox,
  911. struct mlx4_cmd_info *cmd)
  912. {
  913. u64 in_param;
  914. u64 out_param;
  915. int err;
  916. in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
  917. out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
  918. if (cmd->encode_slave_id) {
  919. in_param &= 0xffffffffffffff00ll;
  920. in_param |= slave;
  921. }
  922. err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
  923. vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
  924. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  925. if (cmd->out_is_imm)
  926. vhcr->out_param = out_param;
  927. return err;
  928. }
  929. static struct mlx4_cmd_info cmd_info[] = {
  930. {
  931. .opcode = MLX4_CMD_QUERY_FW,
  932. .has_inbox = false,
  933. .has_outbox = true,
  934. .out_is_imm = false,
  935. .encode_slave_id = false,
  936. .verify = NULL,
  937. .wrapper = mlx4_QUERY_FW_wrapper
  938. },
  939. {
  940. .opcode = MLX4_CMD_QUERY_HCA,
  941. .has_inbox = false,
  942. .has_outbox = true,
  943. .out_is_imm = false,
  944. .encode_slave_id = false,
  945. .verify = NULL,
  946. .wrapper = NULL
  947. },
  948. {
  949. .opcode = MLX4_CMD_QUERY_DEV_CAP,
  950. .has_inbox = false,
  951. .has_outbox = true,
  952. .out_is_imm = false,
  953. .encode_slave_id = false,
  954. .verify = NULL,
  955. .wrapper = mlx4_QUERY_DEV_CAP_wrapper
  956. },
  957. {
  958. .opcode = MLX4_CMD_QUERY_FUNC_CAP,
  959. .has_inbox = false,
  960. .has_outbox = true,
  961. .out_is_imm = false,
  962. .encode_slave_id = false,
  963. .verify = NULL,
  964. .wrapper = mlx4_QUERY_FUNC_CAP_wrapper
  965. },
  966. {
  967. .opcode = MLX4_CMD_QUERY_ADAPTER,
  968. .has_inbox = false,
  969. .has_outbox = true,
  970. .out_is_imm = false,
  971. .encode_slave_id = false,
  972. .verify = NULL,
  973. .wrapper = NULL
  974. },
  975. {
  976. .opcode = MLX4_CMD_INIT_PORT,
  977. .has_inbox = false,
  978. .has_outbox = false,
  979. .out_is_imm = false,
  980. .encode_slave_id = false,
  981. .verify = NULL,
  982. .wrapper = mlx4_INIT_PORT_wrapper
  983. },
  984. {
  985. .opcode = MLX4_CMD_CLOSE_PORT,
  986. .has_inbox = false,
  987. .has_outbox = false,
  988. .out_is_imm = false,
  989. .encode_slave_id = false,
  990. .verify = NULL,
  991. .wrapper = mlx4_CLOSE_PORT_wrapper
  992. },
  993. {
  994. .opcode = MLX4_CMD_QUERY_PORT,
  995. .has_inbox = false,
  996. .has_outbox = true,
  997. .out_is_imm = false,
  998. .encode_slave_id = false,
  999. .verify = NULL,
  1000. .wrapper = mlx4_QUERY_PORT_wrapper
  1001. },
  1002. {
  1003. .opcode = MLX4_CMD_SET_PORT,
  1004. .has_inbox = true,
  1005. .has_outbox = false,
  1006. .out_is_imm = false,
  1007. .encode_slave_id = false,
  1008. .verify = NULL,
  1009. .wrapper = mlx4_SET_PORT_wrapper
  1010. },
  1011. {
  1012. .opcode = MLX4_CMD_MAP_EQ,
  1013. .has_inbox = false,
  1014. .has_outbox = false,
  1015. .out_is_imm = false,
  1016. .encode_slave_id = false,
  1017. .verify = NULL,
  1018. .wrapper = mlx4_MAP_EQ_wrapper
  1019. },
  1020. {
  1021. .opcode = MLX4_CMD_SW2HW_EQ,
  1022. .has_inbox = true,
  1023. .has_outbox = false,
  1024. .out_is_imm = false,
  1025. .encode_slave_id = true,
  1026. .verify = NULL,
  1027. .wrapper = mlx4_SW2HW_EQ_wrapper
  1028. },
  1029. {
  1030. .opcode = MLX4_CMD_HW_HEALTH_CHECK,
  1031. .has_inbox = false,
  1032. .has_outbox = false,
  1033. .out_is_imm = false,
  1034. .encode_slave_id = false,
  1035. .verify = NULL,
  1036. .wrapper = NULL
  1037. },
  1038. {
  1039. .opcode = MLX4_CMD_NOP,
  1040. .has_inbox = false,
  1041. .has_outbox = false,
  1042. .out_is_imm = false,
  1043. .encode_slave_id = false,
  1044. .verify = NULL,
  1045. .wrapper = NULL
  1046. },
  1047. {
  1048. .opcode = MLX4_CMD_CONFIG_DEV,
  1049. .has_inbox = false,
  1050. .has_outbox = true,
  1051. .out_is_imm = false,
  1052. .encode_slave_id = false,
  1053. .verify = NULL,
  1054. .wrapper = mlx4_CONFIG_DEV_wrapper
  1055. },
  1056. {
  1057. .opcode = MLX4_CMD_ALLOC_RES,
  1058. .has_inbox = false,
  1059. .has_outbox = false,
  1060. .out_is_imm = true,
  1061. .encode_slave_id = false,
  1062. .verify = NULL,
  1063. .wrapper = mlx4_ALLOC_RES_wrapper
  1064. },
  1065. {
  1066. .opcode = MLX4_CMD_FREE_RES,
  1067. .has_inbox = false,
  1068. .has_outbox = false,
  1069. .out_is_imm = false,
  1070. .encode_slave_id = false,
  1071. .verify = NULL,
  1072. .wrapper = mlx4_FREE_RES_wrapper
  1073. },
  1074. {
  1075. .opcode = MLX4_CMD_SW2HW_MPT,
  1076. .has_inbox = true,
  1077. .has_outbox = false,
  1078. .out_is_imm = false,
  1079. .encode_slave_id = true,
  1080. .verify = NULL,
  1081. .wrapper = mlx4_SW2HW_MPT_wrapper
  1082. },
  1083. {
  1084. .opcode = MLX4_CMD_QUERY_MPT,
  1085. .has_inbox = false,
  1086. .has_outbox = true,
  1087. .out_is_imm = false,
  1088. .encode_slave_id = false,
  1089. .verify = NULL,
  1090. .wrapper = mlx4_QUERY_MPT_wrapper
  1091. },
  1092. {
  1093. .opcode = MLX4_CMD_HW2SW_MPT,
  1094. .has_inbox = false,
  1095. .has_outbox = false,
  1096. .out_is_imm = false,
  1097. .encode_slave_id = false,
  1098. .verify = NULL,
  1099. .wrapper = mlx4_HW2SW_MPT_wrapper
  1100. },
  1101. {
  1102. .opcode = MLX4_CMD_READ_MTT,
  1103. .has_inbox = false,
  1104. .has_outbox = true,
  1105. .out_is_imm = false,
  1106. .encode_slave_id = false,
  1107. .verify = NULL,
  1108. .wrapper = NULL
  1109. },
  1110. {
  1111. .opcode = MLX4_CMD_WRITE_MTT,
  1112. .has_inbox = true,
  1113. .has_outbox = false,
  1114. .out_is_imm = false,
  1115. .encode_slave_id = false,
  1116. .verify = NULL,
  1117. .wrapper = mlx4_WRITE_MTT_wrapper
  1118. },
  1119. {
  1120. .opcode = MLX4_CMD_SYNC_TPT,
  1121. .has_inbox = true,
  1122. .has_outbox = false,
  1123. .out_is_imm = false,
  1124. .encode_slave_id = false,
  1125. .verify = NULL,
  1126. .wrapper = NULL
  1127. },
  1128. {
  1129. .opcode = MLX4_CMD_HW2SW_EQ,
  1130. .has_inbox = false,
  1131. .has_outbox = false,
  1132. .out_is_imm = false,
  1133. .encode_slave_id = true,
  1134. .verify = NULL,
  1135. .wrapper = mlx4_HW2SW_EQ_wrapper
  1136. },
  1137. {
  1138. .opcode = MLX4_CMD_QUERY_EQ,
  1139. .has_inbox = false,
  1140. .has_outbox = true,
  1141. .out_is_imm = false,
  1142. .encode_slave_id = true,
  1143. .verify = NULL,
  1144. .wrapper = mlx4_QUERY_EQ_wrapper
  1145. },
  1146. {
  1147. .opcode = MLX4_CMD_SW2HW_CQ,
  1148. .has_inbox = true,
  1149. .has_outbox = false,
  1150. .out_is_imm = false,
  1151. .encode_slave_id = true,
  1152. .verify = NULL,
  1153. .wrapper = mlx4_SW2HW_CQ_wrapper
  1154. },
  1155. {
  1156. .opcode = MLX4_CMD_HW2SW_CQ,
  1157. .has_inbox = false,
  1158. .has_outbox = false,
  1159. .out_is_imm = false,
  1160. .encode_slave_id = false,
  1161. .verify = NULL,
  1162. .wrapper = mlx4_HW2SW_CQ_wrapper
  1163. },
  1164. {
  1165. .opcode = MLX4_CMD_QUERY_CQ,
  1166. .has_inbox = false,
  1167. .has_outbox = true,
  1168. .out_is_imm = false,
  1169. .encode_slave_id = false,
  1170. .verify = NULL,
  1171. .wrapper = mlx4_QUERY_CQ_wrapper
  1172. },
  1173. {
  1174. .opcode = MLX4_CMD_MODIFY_CQ,
  1175. .has_inbox = true,
  1176. .has_outbox = false,
  1177. .out_is_imm = true,
  1178. .encode_slave_id = false,
  1179. .verify = NULL,
  1180. .wrapper = mlx4_MODIFY_CQ_wrapper
  1181. },
  1182. {
  1183. .opcode = MLX4_CMD_SW2HW_SRQ,
  1184. .has_inbox = true,
  1185. .has_outbox = false,
  1186. .out_is_imm = false,
  1187. .encode_slave_id = true,
  1188. .verify = NULL,
  1189. .wrapper = mlx4_SW2HW_SRQ_wrapper
  1190. },
  1191. {
  1192. .opcode = MLX4_CMD_HW2SW_SRQ,
  1193. .has_inbox = false,
  1194. .has_outbox = false,
  1195. .out_is_imm = false,
  1196. .encode_slave_id = false,
  1197. .verify = NULL,
  1198. .wrapper = mlx4_HW2SW_SRQ_wrapper
  1199. },
  1200. {
  1201. .opcode = MLX4_CMD_QUERY_SRQ,
  1202. .has_inbox = false,
  1203. .has_outbox = true,
  1204. .out_is_imm = false,
  1205. .encode_slave_id = false,
  1206. .verify = NULL,
  1207. .wrapper = mlx4_QUERY_SRQ_wrapper
  1208. },
  1209. {
  1210. .opcode = MLX4_CMD_ARM_SRQ,
  1211. .has_inbox = false,
  1212. .has_outbox = false,
  1213. .out_is_imm = false,
  1214. .encode_slave_id = false,
  1215. .verify = NULL,
  1216. .wrapper = mlx4_ARM_SRQ_wrapper
  1217. },
  1218. {
  1219. .opcode = MLX4_CMD_RST2INIT_QP,
  1220. .has_inbox = true,
  1221. .has_outbox = false,
  1222. .out_is_imm = false,
  1223. .encode_slave_id = true,
  1224. .verify = NULL,
  1225. .wrapper = mlx4_RST2INIT_QP_wrapper
  1226. },
  1227. {
  1228. .opcode = MLX4_CMD_INIT2INIT_QP,
  1229. .has_inbox = true,
  1230. .has_outbox = false,
  1231. .out_is_imm = false,
  1232. .encode_slave_id = false,
  1233. .verify = NULL,
  1234. .wrapper = mlx4_INIT2INIT_QP_wrapper
  1235. },
  1236. {
  1237. .opcode = MLX4_CMD_INIT2RTR_QP,
  1238. .has_inbox = true,
  1239. .has_outbox = false,
  1240. .out_is_imm = false,
  1241. .encode_slave_id = false,
  1242. .verify = NULL,
  1243. .wrapper = mlx4_INIT2RTR_QP_wrapper
  1244. },
  1245. {
  1246. .opcode = MLX4_CMD_RTR2RTS_QP,
  1247. .has_inbox = true,
  1248. .has_outbox = false,
  1249. .out_is_imm = false,
  1250. .encode_slave_id = false,
  1251. .verify = NULL,
  1252. .wrapper = mlx4_RTR2RTS_QP_wrapper
  1253. },
  1254. {
  1255. .opcode = MLX4_CMD_RTS2RTS_QP,
  1256. .has_inbox = true,
  1257. .has_outbox = false,
  1258. .out_is_imm = false,
  1259. .encode_slave_id = false,
  1260. .verify = NULL,
  1261. .wrapper = mlx4_RTS2RTS_QP_wrapper
  1262. },
  1263. {
  1264. .opcode = MLX4_CMD_SQERR2RTS_QP,
  1265. .has_inbox = true,
  1266. .has_outbox = false,
  1267. .out_is_imm = false,
  1268. .encode_slave_id = false,
  1269. .verify = NULL,
  1270. .wrapper = mlx4_SQERR2RTS_QP_wrapper
  1271. },
  1272. {
  1273. .opcode = MLX4_CMD_2ERR_QP,
  1274. .has_inbox = false,
  1275. .has_outbox = false,
  1276. .out_is_imm = false,
  1277. .encode_slave_id = false,
  1278. .verify = NULL,
  1279. .wrapper = mlx4_GEN_QP_wrapper
  1280. },
  1281. {
  1282. .opcode = MLX4_CMD_RTS2SQD_QP,
  1283. .has_inbox = false,
  1284. .has_outbox = false,
  1285. .out_is_imm = false,
  1286. .encode_slave_id = false,
  1287. .verify = NULL,
  1288. .wrapper = mlx4_GEN_QP_wrapper
  1289. },
  1290. {
  1291. .opcode = MLX4_CMD_SQD2SQD_QP,
  1292. .has_inbox = true,
  1293. .has_outbox = false,
  1294. .out_is_imm = false,
  1295. .encode_slave_id = false,
  1296. .verify = NULL,
  1297. .wrapper = mlx4_SQD2SQD_QP_wrapper
  1298. },
  1299. {
  1300. .opcode = MLX4_CMD_SQD2RTS_QP,
  1301. .has_inbox = true,
  1302. .has_outbox = false,
  1303. .out_is_imm = false,
  1304. .encode_slave_id = false,
  1305. .verify = NULL,
  1306. .wrapper = mlx4_SQD2RTS_QP_wrapper
  1307. },
  1308. {
  1309. .opcode = MLX4_CMD_2RST_QP,
  1310. .has_inbox = false,
  1311. .has_outbox = false,
  1312. .out_is_imm = false,
  1313. .encode_slave_id = false,
  1314. .verify = NULL,
  1315. .wrapper = mlx4_2RST_QP_wrapper
  1316. },
  1317. {
  1318. .opcode = MLX4_CMD_QUERY_QP,
  1319. .has_inbox = false,
  1320. .has_outbox = true,
  1321. .out_is_imm = false,
  1322. .encode_slave_id = false,
  1323. .verify = NULL,
  1324. .wrapper = mlx4_GEN_QP_wrapper
  1325. },
  1326. {
  1327. .opcode = MLX4_CMD_SUSPEND_QP,
  1328. .has_inbox = false,
  1329. .has_outbox = false,
  1330. .out_is_imm = false,
  1331. .encode_slave_id = false,
  1332. .verify = NULL,
  1333. .wrapper = mlx4_GEN_QP_wrapper
  1334. },
  1335. {
  1336. .opcode = MLX4_CMD_UNSUSPEND_QP,
  1337. .has_inbox = false,
  1338. .has_outbox = false,
  1339. .out_is_imm = false,
  1340. .encode_slave_id = false,
  1341. .verify = NULL,
  1342. .wrapper = mlx4_GEN_QP_wrapper
  1343. },
  1344. {
  1345. .opcode = MLX4_CMD_UPDATE_QP,
  1346. .has_inbox = true,
  1347. .has_outbox = false,
  1348. .out_is_imm = false,
  1349. .encode_slave_id = false,
  1350. .verify = NULL,
  1351. .wrapper = mlx4_UPDATE_QP_wrapper
  1352. },
  1353. {
  1354. .opcode = MLX4_CMD_GET_OP_REQ,
  1355. .has_inbox = false,
  1356. .has_outbox = false,
  1357. .out_is_imm = false,
  1358. .encode_slave_id = false,
  1359. .verify = NULL,
  1360. .wrapper = mlx4_CMD_EPERM_wrapper,
  1361. },
  1362. {
  1363. .opcode = MLX4_CMD_ALLOCATE_VPP,
  1364. .has_inbox = false,
  1365. .has_outbox = true,
  1366. .out_is_imm = false,
  1367. .encode_slave_id = false,
  1368. .verify = NULL,
  1369. .wrapper = mlx4_CMD_EPERM_wrapper,
  1370. },
  1371. {
  1372. .opcode = MLX4_CMD_SET_VPORT_QOS,
  1373. .has_inbox = false,
  1374. .has_outbox = true,
  1375. .out_is_imm = false,
  1376. .encode_slave_id = false,
  1377. .verify = NULL,
  1378. .wrapper = mlx4_CMD_EPERM_wrapper,
  1379. },
  1380. {
  1381. .opcode = MLX4_CMD_CONF_SPECIAL_QP,
  1382. .has_inbox = false,
  1383. .has_outbox = false,
  1384. .out_is_imm = false,
  1385. .encode_slave_id = false,
  1386. .verify = NULL, /* XXX verify: only demux can do this */
  1387. .wrapper = NULL
  1388. },
  1389. {
  1390. .opcode = MLX4_CMD_MAD_IFC,
  1391. .has_inbox = true,
  1392. .has_outbox = true,
  1393. .out_is_imm = false,
  1394. .encode_slave_id = false,
  1395. .verify = NULL,
  1396. .wrapper = mlx4_MAD_IFC_wrapper
  1397. },
  1398. {
  1399. .opcode = MLX4_CMD_MAD_DEMUX,
  1400. .has_inbox = false,
  1401. .has_outbox = false,
  1402. .out_is_imm = false,
  1403. .encode_slave_id = false,
  1404. .verify = NULL,
  1405. .wrapper = mlx4_CMD_EPERM_wrapper
  1406. },
  1407. {
  1408. .opcode = MLX4_CMD_QUERY_IF_STAT,
  1409. .has_inbox = false,
  1410. .has_outbox = true,
  1411. .out_is_imm = false,
  1412. .encode_slave_id = false,
  1413. .verify = NULL,
  1414. .wrapper = mlx4_QUERY_IF_STAT_wrapper
  1415. },
  1416. {
  1417. .opcode = MLX4_CMD_ACCESS_REG,
  1418. .has_inbox = true,
  1419. .has_outbox = true,
  1420. .out_is_imm = false,
  1421. .encode_slave_id = false,
  1422. .verify = NULL,
  1423. .wrapper = mlx4_ACCESS_REG_wrapper,
  1424. },
  1425. {
  1426. .opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
  1427. .has_inbox = false,
  1428. .has_outbox = false,
  1429. .out_is_imm = false,
  1430. .encode_slave_id = false,
  1431. .verify = NULL,
  1432. .wrapper = mlx4_CMD_EPERM_wrapper,
  1433. },
  1434. /* Native multicast commands are not available for guests */
  1435. {
  1436. .opcode = MLX4_CMD_QP_ATTACH,
  1437. .has_inbox = true,
  1438. .has_outbox = false,
  1439. .out_is_imm = false,
  1440. .encode_slave_id = false,
  1441. .verify = NULL,
  1442. .wrapper = mlx4_QP_ATTACH_wrapper
  1443. },
  1444. {
  1445. .opcode = MLX4_CMD_PROMISC,
  1446. .has_inbox = false,
  1447. .has_outbox = false,
  1448. .out_is_imm = false,
  1449. .encode_slave_id = false,
  1450. .verify = NULL,
  1451. .wrapper = mlx4_PROMISC_wrapper
  1452. },
  1453. /* Ethernet specific commands */
  1454. {
  1455. .opcode = MLX4_CMD_SET_VLAN_FLTR,
  1456. .has_inbox = true,
  1457. .has_outbox = false,
  1458. .out_is_imm = false,
  1459. .encode_slave_id = false,
  1460. .verify = NULL,
  1461. .wrapper = mlx4_SET_VLAN_FLTR_wrapper
  1462. },
  1463. {
  1464. .opcode = MLX4_CMD_SET_MCAST_FLTR,
  1465. .has_inbox = false,
  1466. .has_outbox = false,
  1467. .out_is_imm = false,
  1468. .encode_slave_id = false,
  1469. .verify = NULL,
  1470. .wrapper = mlx4_SET_MCAST_FLTR_wrapper
  1471. },
  1472. {
  1473. .opcode = MLX4_CMD_DUMP_ETH_STATS,
  1474. .has_inbox = false,
  1475. .has_outbox = true,
  1476. .out_is_imm = false,
  1477. .encode_slave_id = false,
  1478. .verify = NULL,
  1479. .wrapper = mlx4_DUMP_ETH_STATS_wrapper
  1480. },
  1481. {
  1482. .opcode = MLX4_CMD_INFORM_FLR_DONE,
  1483. .has_inbox = false,
  1484. .has_outbox = false,
  1485. .out_is_imm = false,
  1486. .encode_slave_id = false,
  1487. .verify = NULL,
  1488. .wrapper = NULL
  1489. },
  1490. /* flow steering commands */
  1491. {
  1492. .opcode = MLX4_QP_FLOW_STEERING_ATTACH,
  1493. .has_inbox = true,
  1494. .has_outbox = false,
  1495. .out_is_imm = true,
  1496. .encode_slave_id = false,
  1497. .verify = NULL,
  1498. .wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
  1499. },
  1500. {
  1501. .opcode = MLX4_QP_FLOW_STEERING_DETACH,
  1502. .has_inbox = false,
  1503. .has_outbox = false,
  1504. .out_is_imm = false,
  1505. .encode_slave_id = false,
  1506. .verify = NULL,
  1507. .wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
  1508. },
  1509. {
  1510. .opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
  1511. .has_inbox = false,
  1512. .has_outbox = false,
  1513. .out_is_imm = false,
  1514. .encode_slave_id = false,
  1515. .verify = NULL,
  1516. .wrapper = mlx4_CMD_EPERM_wrapper
  1517. },
  1518. {
  1519. .opcode = MLX4_CMD_VIRT_PORT_MAP,
  1520. .has_inbox = false,
  1521. .has_outbox = false,
  1522. .out_is_imm = false,
  1523. .encode_slave_id = false,
  1524. .verify = NULL,
  1525. .wrapper = mlx4_CMD_EPERM_wrapper
  1526. },
  1527. };
  1528. static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
  1529. struct mlx4_vhcr_cmd *in_vhcr)
  1530. {
  1531. struct mlx4_priv *priv = mlx4_priv(dev);
  1532. struct mlx4_cmd_info *cmd = NULL;
  1533. struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
  1534. struct mlx4_vhcr *vhcr;
  1535. struct mlx4_cmd_mailbox *inbox = NULL;
  1536. struct mlx4_cmd_mailbox *outbox = NULL;
  1537. u64 in_param;
  1538. u64 out_param;
  1539. int ret = 0;
  1540. int i;
  1541. int err = 0;
  1542. /* Create sw representation of Virtual HCR */
  1543. vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
  1544. if (!vhcr)
  1545. return -ENOMEM;
  1546. /* DMA in the vHCR */
  1547. if (!in_vhcr) {
  1548. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1549. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1550. ALIGN(sizeof(struct mlx4_vhcr_cmd),
  1551. MLX4_ACCESS_MEM_ALIGN), 1);
  1552. if (ret) {
  1553. if (!(dev->persist->state &
  1554. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1555. mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
  1556. __func__, ret);
  1557. kfree(vhcr);
  1558. return ret;
  1559. }
  1560. }
  1561. /* Fill SW VHCR fields */
  1562. vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
  1563. vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
  1564. vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
  1565. vhcr->token = be16_to_cpu(vhcr_cmd->token);
  1566. vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
  1567. vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
  1568. vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
  1569. /* Lookup command */
  1570. for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
  1571. if (vhcr->op == cmd_info[i].opcode) {
  1572. cmd = &cmd_info[i];
  1573. break;
  1574. }
  1575. }
  1576. if (!cmd) {
  1577. mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
  1578. vhcr->op, slave);
  1579. vhcr_cmd->status = CMD_STAT_BAD_PARAM;
  1580. goto out_status;
  1581. }
  1582. /* Read inbox */
  1583. if (cmd->has_inbox) {
  1584. vhcr->in_param &= INBOX_MASK;
  1585. inbox = mlx4_alloc_cmd_mailbox(dev);
  1586. if (IS_ERR(inbox)) {
  1587. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1588. inbox = NULL;
  1589. goto out_status;
  1590. }
  1591. ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
  1592. vhcr->in_param,
  1593. MLX4_MAILBOX_SIZE, 1);
  1594. if (ret) {
  1595. if (!(dev->persist->state &
  1596. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1597. mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
  1598. __func__, cmd->opcode);
  1599. vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
  1600. goto out_status;
  1601. }
  1602. }
  1603. /* Apply permission and bound checks if applicable */
  1604. if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
  1605. mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
  1606. vhcr->op, slave, vhcr->in_modifier);
  1607. vhcr_cmd->status = CMD_STAT_BAD_OP;
  1608. goto out_status;
  1609. }
  1610. /* Allocate outbox */
  1611. if (cmd->has_outbox) {
  1612. outbox = mlx4_alloc_cmd_mailbox(dev);
  1613. if (IS_ERR(outbox)) {
  1614. vhcr_cmd->status = CMD_STAT_BAD_SIZE;
  1615. outbox = NULL;
  1616. goto out_status;
  1617. }
  1618. }
  1619. /* Execute the command! */
  1620. if (cmd->wrapper) {
  1621. err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
  1622. cmd);
  1623. if (cmd->out_is_imm)
  1624. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1625. } else {
  1626. in_param = cmd->has_inbox ? (u64) inbox->dma :
  1627. vhcr->in_param;
  1628. out_param = cmd->has_outbox ? (u64) outbox->dma :
  1629. vhcr->out_param;
  1630. err = __mlx4_cmd(dev, in_param, &out_param,
  1631. cmd->out_is_imm, vhcr->in_modifier,
  1632. vhcr->op_modifier, vhcr->op,
  1633. MLX4_CMD_TIME_CLASS_A,
  1634. MLX4_CMD_NATIVE);
  1635. if (cmd->out_is_imm) {
  1636. vhcr->out_param = out_param;
  1637. vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
  1638. }
  1639. }
  1640. if (err) {
  1641. if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
  1642. if (vhcr->op == MLX4_CMD_ALLOC_RES &&
  1643. (vhcr->in_modifier & 0xff) == RES_COUNTER &&
  1644. err == -EDQUOT)
  1645. mlx4_dbg(dev,
  1646. "Unable to allocate counter for slave %d (%d)\n",
  1647. slave, err);
  1648. else
  1649. mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
  1650. vhcr->op, slave, vhcr->errno, err);
  1651. }
  1652. vhcr_cmd->status = mlx4_errno_to_status(err);
  1653. goto out_status;
  1654. }
  1655. /* Write outbox if command completed successfully */
  1656. if (cmd->has_outbox && !vhcr_cmd->status) {
  1657. ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
  1658. vhcr->out_param,
  1659. MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
  1660. if (ret) {
  1661. /* If we failed to write back the outbox after the
  1662. *command was successfully executed, we must fail this
  1663. * slave, as it is now in undefined state */
  1664. if (!(dev->persist->state &
  1665. MLX4_DEVICE_STATE_INTERNAL_ERROR))
  1666. mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
  1667. goto out;
  1668. }
  1669. }
  1670. out_status:
  1671. /* DMA back vhcr result */
  1672. if (!in_vhcr) {
  1673. ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
  1674. priv->mfunc.master.slave_state[slave].vhcr_dma,
  1675. ALIGN(sizeof(struct mlx4_vhcr),
  1676. MLX4_ACCESS_MEM_ALIGN),
  1677. MLX4_CMD_WRAPPED);
  1678. if (ret)
  1679. mlx4_err(dev, "%s:Failed writing vhcr result\n",
  1680. __func__);
  1681. else if (vhcr->e_bit &&
  1682. mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
  1683. mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
  1684. slave);
  1685. }
  1686. out:
  1687. kfree(vhcr);
  1688. mlx4_free_cmd_mailbox(dev, inbox);
  1689. mlx4_free_cmd_mailbox(dev, outbox);
  1690. return ret;
  1691. }
  1692. static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
  1693. int slave, int port)
  1694. {
  1695. struct mlx4_vport_oper_state *vp_oper;
  1696. struct mlx4_vport_state *vp_admin;
  1697. struct mlx4_vf_immed_vlan_work *work;
  1698. struct mlx4_dev *dev = &(priv->dev);
  1699. int err;
  1700. int admin_vlan_ix = NO_INDX;
  1701. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1702. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1703. if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
  1704. vp_oper->state.default_qos == vp_admin->default_qos &&
  1705. vp_oper->state.vlan_proto == vp_admin->vlan_proto &&
  1706. vp_oper->state.link_state == vp_admin->link_state &&
  1707. vp_oper->state.qos_vport == vp_admin->qos_vport)
  1708. return 0;
  1709. if (!(priv->mfunc.master.slave_state[slave].active &&
  1710. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
  1711. /* even if the UPDATE_QP command isn't supported, we still want
  1712. * to set this VF link according to the admin directive
  1713. */
  1714. vp_oper->state.link_state = vp_admin->link_state;
  1715. return -1;
  1716. }
  1717. mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
  1718. slave, port);
  1719. mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
  1720. vp_admin->default_vlan, vp_admin->default_qos,
  1721. vp_admin->link_state);
  1722. work = kzalloc(sizeof(*work), GFP_KERNEL);
  1723. if (!work)
  1724. return -ENOMEM;
  1725. if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
  1726. if (MLX4_VGT != vp_admin->default_vlan) {
  1727. err = __mlx4_register_vlan(&priv->dev, port,
  1728. vp_admin->default_vlan,
  1729. &admin_vlan_ix);
  1730. if (err) {
  1731. kfree(work);
  1732. mlx4_warn(&priv->dev,
  1733. "No vlan resources slave %d, port %d\n",
  1734. slave, port);
  1735. return err;
  1736. }
  1737. } else {
  1738. admin_vlan_ix = NO_INDX;
  1739. }
  1740. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
  1741. mlx4_dbg(&priv->dev,
  1742. "alloc vlan %d idx %d slave %d port %d\n",
  1743. (int)(vp_admin->default_vlan),
  1744. admin_vlan_ix, slave, port);
  1745. }
  1746. /* save original vlan ix and vlan id */
  1747. work->orig_vlan_id = vp_oper->state.default_vlan;
  1748. work->orig_vlan_ix = vp_oper->vlan_idx;
  1749. /* handle new qos */
  1750. if (vp_oper->state.default_qos != vp_admin->default_qos)
  1751. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
  1752. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
  1753. vp_oper->vlan_idx = admin_vlan_ix;
  1754. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1755. vp_oper->state.default_qos = vp_admin->default_qos;
  1756. vp_oper->state.vlan_proto = vp_admin->vlan_proto;
  1757. vp_oper->state.link_state = vp_admin->link_state;
  1758. vp_oper->state.qos_vport = vp_admin->qos_vport;
  1759. if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
  1760. work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
  1761. /* iterate over QPs owned by this slave, using UPDATE_QP */
  1762. work->port = port;
  1763. work->slave = slave;
  1764. work->qos = vp_oper->state.default_qos;
  1765. work->qos_vport = vp_oper->state.qos_vport;
  1766. work->vlan_id = vp_oper->state.default_vlan;
  1767. work->vlan_ix = vp_oper->vlan_idx;
  1768. work->vlan_proto = vp_oper->state.vlan_proto;
  1769. work->priv = priv;
  1770. INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
  1771. queue_work(priv->mfunc.master.comm_wq, &work->work);
  1772. return 0;
  1773. }
  1774. static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
  1775. {
  1776. struct mlx4_qos_manager *port_qos_ctl;
  1777. struct mlx4_priv *priv = mlx4_priv(dev);
  1778. port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
  1779. bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
  1780. /* Enable only default prio at PF init routine */
  1781. set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
  1782. }
  1783. static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
  1784. {
  1785. int i;
  1786. int err;
  1787. int num_vfs;
  1788. u16 available_vpp;
  1789. u8 vpp_param[MLX4_NUM_UP];
  1790. struct mlx4_qos_manager *port_qos;
  1791. struct mlx4_priv *priv = mlx4_priv(dev);
  1792. err = mlx4_ALLOCATE_VPP_get(dev, port, &available_vpp, vpp_param);
  1793. if (err) {
  1794. mlx4_info(dev, "Failed query available VPPs\n");
  1795. return;
  1796. }
  1797. port_qos = &priv->mfunc.master.qos_ctl[port];
  1798. num_vfs = (available_vpp /
  1799. bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
  1800. for (i = 0; i < MLX4_NUM_UP; i++) {
  1801. if (test_bit(i, port_qos->priority_bm))
  1802. vpp_param[i] = num_vfs;
  1803. }
  1804. err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
  1805. if (err) {
  1806. mlx4_info(dev, "Failed allocating VPPs\n");
  1807. return;
  1808. }
  1809. /* Query actual allocated VPP, just to make sure */
  1810. err = mlx4_ALLOCATE_VPP_get(dev, port, &available_vpp, vpp_param);
  1811. if (err) {
  1812. mlx4_info(dev, "Failed query available VPPs\n");
  1813. return;
  1814. }
  1815. port_qos->num_of_qos_vfs = num_vfs;
  1816. mlx4_dbg(dev, "Port %d Available VPPs %d\n", port, available_vpp);
  1817. for (i = 0; i < MLX4_NUM_UP; i++)
  1818. mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
  1819. vpp_param[i]);
  1820. }
  1821. static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
  1822. {
  1823. int port, err;
  1824. struct mlx4_vport_state *vp_admin;
  1825. struct mlx4_vport_oper_state *vp_oper;
  1826. struct mlx4_slave_state *slave_state =
  1827. &priv->mfunc.master.slave_state[slave];
  1828. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1829. &priv->dev, slave);
  1830. int min_port = find_first_bit(actv_ports.ports,
  1831. priv->dev.caps.num_ports) + 1;
  1832. int max_port = min_port - 1 +
  1833. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1834. for (port = min_port; port <= max_port; port++) {
  1835. if (!test_bit(port - 1, actv_ports.ports))
  1836. continue;
  1837. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1838. priv->mfunc.master.vf_admin[slave].enable_smi[port];
  1839. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1840. vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  1841. if (vp_admin->vlan_proto != htons(ETH_P_8021AD) ||
  1842. slave_state->vst_qinq_supported) {
  1843. vp_oper->state.vlan_proto = vp_admin->vlan_proto;
  1844. vp_oper->state.default_vlan = vp_admin->default_vlan;
  1845. vp_oper->state.default_qos = vp_admin->default_qos;
  1846. }
  1847. vp_oper->state.link_state = vp_admin->link_state;
  1848. vp_oper->state.mac = vp_admin->mac;
  1849. vp_oper->state.spoofchk = vp_admin->spoofchk;
  1850. vp_oper->state.tx_rate = vp_admin->tx_rate;
  1851. vp_oper->state.qos_vport = vp_admin->qos_vport;
  1852. vp_oper->state.guid = vp_admin->guid;
  1853. if (MLX4_VGT != vp_admin->default_vlan) {
  1854. err = __mlx4_register_vlan(&priv->dev, port,
  1855. vp_admin->default_vlan, &(vp_oper->vlan_idx));
  1856. if (err) {
  1857. vp_oper->vlan_idx = NO_INDX;
  1858. vp_oper->state.default_vlan = MLX4_VGT;
  1859. vp_oper->state.vlan_proto = htons(ETH_P_8021Q);
  1860. mlx4_warn(&priv->dev,
  1861. "No vlan resources slave %d, port %d\n",
  1862. slave, port);
  1863. return err;
  1864. }
  1865. mlx4_dbg(&priv->dev, "alloc vlan %d idx %d slave %d port %d\n",
  1866. (int)(vp_oper->state.default_vlan),
  1867. vp_oper->vlan_idx, slave, port);
  1868. }
  1869. if (vp_admin->spoofchk) {
  1870. vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
  1871. port,
  1872. vp_admin->mac);
  1873. if (0 > vp_oper->mac_idx) {
  1874. err = vp_oper->mac_idx;
  1875. vp_oper->mac_idx = NO_INDX;
  1876. mlx4_warn(&priv->dev,
  1877. "No mac resources slave %d, port %d\n",
  1878. slave, port);
  1879. return err;
  1880. }
  1881. mlx4_dbg(&priv->dev, "alloc mac %llx idx %d slave %d port %d\n",
  1882. vp_oper->state.mac, vp_oper->mac_idx, slave, port);
  1883. }
  1884. }
  1885. return 0;
  1886. }
  1887. static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
  1888. {
  1889. int port;
  1890. struct mlx4_vport_oper_state *vp_oper;
  1891. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  1892. &priv->dev, slave);
  1893. int min_port = find_first_bit(actv_ports.ports,
  1894. priv->dev.caps.num_ports) + 1;
  1895. int max_port = min_port - 1 +
  1896. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  1897. for (port = min_port; port <= max_port; port++) {
  1898. if (!test_bit(port - 1, actv_ports.ports))
  1899. continue;
  1900. priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
  1901. MLX4_VF_SMI_DISABLED;
  1902. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  1903. if (NO_INDX != vp_oper->vlan_idx) {
  1904. __mlx4_unregister_vlan(&priv->dev,
  1905. port, vp_oper->state.default_vlan);
  1906. vp_oper->vlan_idx = NO_INDX;
  1907. }
  1908. if (NO_INDX != vp_oper->mac_idx) {
  1909. __mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
  1910. vp_oper->mac_idx = NO_INDX;
  1911. }
  1912. }
  1913. return;
  1914. }
  1915. static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
  1916. u16 param, u8 toggle)
  1917. {
  1918. struct mlx4_priv *priv = mlx4_priv(dev);
  1919. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1920. u32 reply;
  1921. u8 is_going_down = 0;
  1922. int i;
  1923. unsigned long flags;
  1924. slave_state[slave].comm_toggle ^= 1;
  1925. reply = (u32) slave_state[slave].comm_toggle << 31;
  1926. if (toggle != slave_state[slave].comm_toggle) {
  1927. mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
  1928. toggle, slave);
  1929. goto reset_slave;
  1930. }
  1931. if (cmd == MLX4_COMM_CMD_RESET) {
  1932. mlx4_warn(dev, "Received reset from slave:%d\n", slave);
  1933. slave_state[slave].active = false;
  1934. slave_state[slave].old_vlan_api = false;
  1935. slave_state[slave].vst_qinq_supported = false;
  1936. mlx4_master_deactivate_admin_state(priv, slave);
  1937. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
  1938. slave_state[slave].event_eq[i].eqn = -1;
  1939. slave_state[slave].event_eq[i].token = 0;
  1940. }
  1941. /*check if we are in the middle of FLR process,
  1942. if so return "retry" status to the slave*/
  1943. if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
  1944. goto inform_slave_state;
  1945. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, slave);
  1946. /* write the version in the event field */
  1947. reply |= mlx4_comm_get_version();
  1948. goto reset_slave;
  1949. }
  1950. /*command from slave in the middle of FLR*/
  1951. if (cmd != MLX4_COMM_CMD_RESET &&
  1952. MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
  1953. mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
  1954. slave, cmd);
  1955. return;
  1956. }
  1957. switch (cmd) {
  1958. case MLX4_COMM_CMD_VHCR0:
  1959. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
  1960. goto reset_slave;
  1961. slave_state[slave].vhcr_dma = ((u64) param) << 48;
  1962. priv->mfunc.master.slave_state[slave].cookie = 0;
  1963. break;
  1964. case MLX4_COMM_CMD_VHCR1:
  1965. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
  1966. goto reset_slave;
  1967. slave_state[slave].vhcr_dma |= ((u64) param) << 32;
  1968. break;
  1969. case MLX4_COMM_CMD_VHCR2:
  1970. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
  1971. goto reset_slave;
  1972. slave_state[slave].vhcr_dma |= ((u64) param) << 16;
  1973. break;
  1974. case MLX4_COMM_CMD_VHCR_EN:
  1975. if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
  1976. goto reset_slave;
  1977. slave_state[slave].vhcr_dma |= param;
  1978. if (mlx4_master_activate_admin_state(priv, slave))
  1979. goto reset_slave;
  1980. slave_state[slave].active = true;
  1981. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, slave);
  1982. break;
  1983. case MLX4_COMM_CMD_VHCR_POST:
  1984. if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
  1985. (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
  1986. mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
  1987. slave, cmd, slave_state[slave].last_cmd);
  1988. goto reset_slave;
  1989. }
  1990. mutex_lock(&priv->cmd.slave_cmd_mutex);
  1991. if (mlx4_master_process_vhcr(dev, slave, NULL)) {
  1992. mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
  1993. slave);
  1994. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1995. goto reset_slave;
  1996. }
  1997. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  1998. break;
  1999. default:
  2000. mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
  2001. goto reset_slave;
  2002. }
  2003. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  2004. if (!slave_state[slave].is_slave_going_down)
  2005. slave_state[slave].last_cmd = cmd;
  2006. else
  2007. is_going_down = 1;
  2008. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  2009. if (is_going_down) {
  2010. mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
  2011. cmd, slave);
  2012. return;
  2013. }
  2014. __raw_writel((__force u32) cpu_to_be32(reply),
  2015. &priv->mfunc.comm[slave].slave_read);
  2016. return;
  2017. reset_slave:
  2018. /* cleanup any slave resources */
  2019. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
  2020. mlx4_delete_all_resources_for_slave(dev, slave);
  2021. if (cmd != MLX4_COMM_CMD_RESET) {
  2022. mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
  2023. slave, cmd);
  2024. /* Turn on internal error letting slave reset itself immeditaly,
  2025. * otherwise it might take till timeout on command is passed
  2026. */
  2027. reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
  2028. }
  2029. spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
  2030. if (!slave_state[slave].is_slave_going_down)
  2031. slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
  2032. spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
  2033. /*with slave in the middle of flr, no need to clean resources again.*/
  2034. inform_slave_state:
  2035. memset(&slave_state[slave].event_eq, 0,
  2036. sizeof(struct mlx4_slave_event_eq_info));
  2037. __raw_writel((__force u32) cpu_to_be32(reply),
  2038. &priv->mfunc.comm[slave].slave_read);
  2039. wmb();
  2040. }
  2041. /* master command processing */
  2042. void mlx4_master_comm_channel(struct work_struct *work)
  2043. {
  2044. struct mlx4_mfunc_master_ctx *master =
  2045. container_of(work,
  2046. struct mlx4_mfunc_master_ctx,
  2047. comm_work);
  2048. struct mlx4_mfunc *mfunc =
  2049. container_of(master, struct mlx4_mfunc, master);
  2050. struct mlx4_priv *priv =
  2051. container_of(mfunc, struct mlx4_priv, mfunc);
  2052. struct mlx4_dev *dev = &priv->dev;
  2053. __be32 *bit_vec;
  2054. u32 comm_cmd;
  2055. u32 vec;
  2056. int i, j, slave;
  2057. int toggle;
  2058. int served = 0;
  2059. int reported = 0;
  2060. u32 slt;
  2061. bit_vec = master->comm_arm_bit_vector;
  2062. for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++) {
  2063. vec = be32_to_cpu(bit_vec[i]);
  2064. for (j = 0; j < 32; j++) {
  2065. if (!(vec & (1 << j)))
  2066. continue;
  2067. ++reported;
  2068. slave = (i * 32) + j;
  2069. comm_cmd = swab32(readl(
  2070. &mfunc->comm[slave].slave_write));
  2071. slt = swab32(readl(&mfunc->comm[slave].slave_read))
  2072. >> 31;
  2073. toggle = comm_cmd >> 31;
  2074. if (toggle != slt) {
  2075. if (master->slave_state[slave].comm_toggle
  2076. != slt) {
  2077. pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
  2078. slave, slt,
  2079. master->slave_state[slave].comm_toggle);
  2080. master->slave_state[slave].comm_toggle =
  2081. slt;
  2082. }
  2083. mlx4_master_do_cmd(dev, slave,
  2084. comm_cmd >> 16 & 0xff,
  2085. comm_cmd & 0xffff, toggle);
  2086. ++served;
  2087. }
  2088. }
  2089. }
  2090. if (reported && reported != served)
  2091. mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
  2092. reported, served);
  2093. if (mlx4_ARM_COMM_CHANNEL(dev))
  2094. mlx4_warn(dev, "Failed to arm comm channel events\n");
  2095. }
  2096. static int sync_toggles(struct mlx4_dev *dev)
  2097. {
  2098. struct mlx4_priv *priv = mlx4_priv(dev);
  2099. u32 wr_toggle;
  2100. u32 rd_toggle;
  2101. unsigned long end;
  2102. wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
  2103. if (wr_toggle == 0xffffffff)
  2104. end = jiffies + msecs_to_jiffies(30000);
  2105. else
  2106. end = jiffies + msecs_to_jiffies(5000);
  2107. while (time_before(jiffies, end)) {
  2108. rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
  2109. if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
  2110. /* PCI might be offline */
  2111. /* If device removal has been requested,
  2112. * do not continue retrying.
  2113. */
  2114. if (dev->persist->interface_state &
  2115. MLX4_INTERFACE_STATE_NOWAIT) {
  2116. mlx4_warn(dev,
  2117. "communication channel is offline\n");
  2118. return -EIO;
  2119. }
  2120. msleep(100);
  2121. wr_toggle = swab32(readl(&priv->mfunc.comm->
  2122. slave_write));
  2123. continue;
  2124. }
  2125. if (rd_toggle >> 31 == wr_toggle >> 31) {
  2126. priv->cmd.comm_toggle = rd_toggle >> 31;
  2127. return 0;
  2128. }
  2129. cond_resched();
  2130. }
  2131. /*
  2132. * we could reach here if for example the previous VM using this
  2133. * function misbehaved and left the channel with unsynced state. We
  2134. * should fix this here and give this VM a chance to use a properly
  2135. * synced channel
  2136. */
  2137. mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
  2138. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
  2139. __raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
  2140. priv->cmd.comm_toggle = 0;
  2141. return 0;
  2142. }
  2143. int mlx4_multi_func_init(struct mlx4_dev *dev)
  2144. {
  2145. struct mlx4_priv *priv = mlx4_priv(dev);
  2146. struct mlx4_slave_state *s_state;
  2147. int i, j, err, port;
  2148. if (mlx4_is_master(dev))
  2149. priv->mfunc.comm =
  2150. ioremap(pci_resource_start(dev->persist->pdev,
  2151. priv->fw.comm_bar) +
  2152. priv->fw.comm_base, MLX4_COMM_PAGESIZE);
  2153. else
  2154. priv->mfunc.comm =
  2155. ioremap(pci_resource_start(dev->persist->pdev, 2) +
  2156. MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
  2157. if (!priv->mfunc.comm) {
  2158. mlx4_err(dev, "Couldn't map communication vector\n");
  2159. goto err_vhcr;
  2160. }
  2161. if (mlx4_is_master(dev)) {
  2162. struct mlx4_vf_oper_state *vf_oper;
  2163. struct mlx4_vf_admin_state *vf_admin;
  2164. priv->mfunc.master.slave_state =
  2165. kcalloc(dev->num_slaves,
  2166. sizeof(struct mlx4_slave_state),
  2167. GFP_KERNEL);
  2168. if (!priv->mfunc.master.slave_state)
  2169. goto err_comm;
  2170. priv->mfunc.master.vf_admin =
  2171. kcalloc(dev->num_slaves,
  2172. sizeof(struct mlx4_vf_admin_state),
  2173. GFP_KERNEL);
  2174. if (!priv->mfunc.master.vf_admin)
  2175. goto err_comm_admin;
  2176. priv->mfunc.master.vf_oper =
  2177. kcalloc(dev->num_slaves,
  2178. sizeof(struct mlx4_vf_oper_state),
  2179. GFP_KERNEL);
  2180. if (!priv->mfunc.master.vf_oper)
  2181. goto err_comm_oper;
  2182. for (i = 0; i < dev->num_slaves; ++i) {
  2183. vf_admin = &priv->mfunc.master.vf_admin[i];
  2184. vf_oper = &priv->mfunc.master.vf_oper[i];
  2185. s_state = &priv->mfunc.master.slave_state[i];
  2186. s_state->last_cmd = MLX4_COMM_CMD_RESET;
  2187. s_state->vst_qinq_supported = false;
  2188. mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
  2189. for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
  2190. s_state->event_eq[j].eqn = -1;
  2191. __raw_writel((__force u32) 0,
  2192. &priv->mfunc.comm[i].slave_write);
  2193. __raw_writel((__force u32) 0,
  2194. &priv->mfunc.comm[i].slave_read);
  2195. for (port = 1; port <= MLX4_MAX_PORTS; port++) {
  2196. struct mlx4_vport_state *admin_vport;
  2197. struct mlx4_vport_state *oper_vport;
  2198. s_state->vlan_filter[port] =
  2199. kzalloc(sizeof(struct mlx4_vlan_fltr),
  2200. GFP_KERNEL);
  2201. if (!s_state->vlan_filter[port]) {
  2202. if (--port)
  2203. kfree(s_state->vlan_filter[port]);
  2204. goto err_slaves;
  2205. }
  2206. admin_vport = &vf_admin->vport[port];
  2207. oper_vport = &vf_oper->vport[port].state;
  2208. INIT_LIST_HEAD(&s_state->mcast_filters[port]);
  2209. admin_vport->default_vlan = MLX4_VGT;
  2210. oper_vport->default_vlan = MLX4_VGT;
  2211. admin_vport->qos_vport =
  2212. MLX4_VPP_DEFAULT_VPORT;
  2213. oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
  2214. admin_vport->vlan_proto = htons(ETH_P_8021Q);
  2215. oper_vport->vlan_proto = htons(ETH_P_8021Q);
  2216. vf_oper->vport[port].vlan_idx = NO_INDX;
  2217. vf_oper->vport[port].mac_idx = NO_INDX;
  2218. mlx4_set_random_admin_guid(dev, i, port);
  2219. }
  2220. spin_lock_init(&s_state->lock);
  2221. }
  2222. if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
  2223. for (port = 1; port <= dev->caps.num_ports; port++) {
  2224. if (mlx4_is_eth(dev, port)) {
  2225. mlx4_set_default_port_qos(dev, port);
  2226. mlx4_allocate_port_vpps(dev, port);
  2227. }
  2228. }
  2229. }
  2230. memset(&priv->mfunc.master.cmd_eqe, 0, sizeof(struct mlx4_eqe));
  2231. priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
  2232. INIT_WORK(&priv->mfunc.master.comm_work,
  2233. mlx4_master_comm_channel);
  2234. INIT_WORK(&priv->mfunc.master.slave_event_work,
  2235. mlx4_gen_slave_eqe);
  2236. INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
  2237. mlx4_master_handle_slave_flr);
  2238. spin_lock_init(&priv->mfunc.master.slave_state_lock);
  2239. spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
  2240. priv->mfunc.master.comm_wq =
  2241. create_singlethread_workqueue("mlx4_comm");
  2242. if (!priv->mfunc.master.comm_wq)
  2243. goto err_slaves;
  2244. if (mlx4_init_resource_tracker(dev))
  2245. goto err_thread;
  2246. } else {
  2247. err = sync_toggles(dev);
  2248. if (err) {
  2249. mlx4_err(dev, "Couldn't sync toggles\n");
  2250. goto err_comm;
  2251. }
  2252. }
  2253. return 0;
  2254. err_thread:
  2255. flush_workqueue(priv->mfunc.master.comm_wq);
  2256. destroy_workqueue(priv->mfunc.master.comm_wq);
  2257. err_slaves:
  2258. while (i--) {
  2259. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  2260. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  2261. }
  2262. kfree(priv->mfunc.master.vf_oper);
  2263. err_comm_oper:
  2264. kfree(priv->mfunc.master.vf_admin);
  2265. err_comm_admin:
  2266. kfree(priv->mfunc.master.slave_state);
  2267. err_comm:
  2268. iounmap(priv->mfunc.comm);
  2269. priv->mfunc.comm = NULL;
  2270. err_vhcr:
  2271. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  2272. priv->mfunc.vhcr,
  2273. priv->mfunc.vhcr_dma);
  2274. priv->mfunc.vhcr = NULL;
  2275. return -ENOMEM;
  2276. }
  2277. int mlx4_cmd_init(struct mlx4_dev *dev)
  2278. {
  2279. struct mlx4_priv *priv = mlx4_priv(dev);
  2280. int flags = 0;
  2281. if (!priv->cmd.initialized) {
  2282. init_rwsem(&priv->cmd.switch_sem);
  2283. mutex_init(&priv->cmd.slave_cmd_mutex);
  2284. sema_init(&priv->cmd.poll_sem, 1);
  2285. priv->cmd.use_events = 0;
  2286. priv->cmd.toggle = 1;
  2287. priv->cmd.initialized = 1;
  2288. flags |= MLX4_CMD_CLEANUP_STRUCT;
  2289. }
  2290. if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
  2291. priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
  2292. 0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
  2293. if (!priv->cmd.hcr) {
  2294. mlx4_err(dev, "Couldn't map command register\n");
  2295. goto err;
  2296. }
  2297. flags |= MLX4_CMD_CLEANUP_HCR;
  2298. }
  2299. if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
  2300. priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
  2301. PAGE_SIZE,
  2302. &priv->mfunc.vhcr_dma,
  2303. GFP_KERNEL);
  2304. if (!priv->mfunc.vhcr)
  2305. goto err;
  2306. flags |= MLX4_CMD_CLEANUP_VHCR;
  2307. }
  2308. if (!priv->cmd.pool) {
  2309. priv->cmd.pool = dma_pool_create("mlx4_cmd",
  2310. &dev->persist->pdev->dev,
  2311. MLX4_MAILBOX_SIZE,
  2312. MLX4_MAILBOX_SIZE, 0);
  2313. if (!priv->cmd.pool)
  2314. goto err;
  2315. flags |= MLX4_CMD_CLEANUP_POOL;
  2316. }
  2317. return 0;
  2318. err:
  2319. mlx4_cmd_cleanup(dev, flags);
  2320. return -ENOMEM;
  2321. }
  2322. void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
  2323. {
  2324. struct mlx4_priv *priv = mlx4_priv(dev);
  2325. int slave;
  2326. u32 slave_read;
  2327. /* If the comm channel has not yet been initialized,
  2328. * skip reporting the internal error event to all
  2329. * the communication channels.
  2330. */
  2331. if (!priv->mfunc.comm)
  2332. return;
  2333. /* Report an internal error event to all
  2334. * communication channels.
  2335. */
  2336. for (slave = 0; slave < dev->num_slaves; slave++) {
  2337. slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
  2338. slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
  2339. __raw_writel((__force u32)cpu_to_be32(slave_read),
  2340. &priv->mfunc.comm[slave].slave_read);
  2341. }
  2342. }
  2343. void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
  2344. {
  2345. struct mlx4_priv *priv = mlx4_priv(dev);
  2346. int i, port;
  2347. if (mlx4_is_master(dev)) {
  2348. flush_workqueue(priv->mfunc.master.comm_wq);
  2349. destroy_workqueue(priv->mfunc.master.comm_wq);
  2350. for (i = 0; i < dev->num_slaves; i++) {
  2351. for (port = 1; port <= MLX4_MAX_PORTS; port++)
  2352. kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
  2353. }
  2354. kfree(priv->mfunc.master.slave_state);
  2355. kfree(priv->mfunc.master.vf_admin);
  2356. kfree(priv->mfunc.master.vf_oper);
  2357. dev->num_slaves = 0;
  2358. }
  2359. iounmap(priv->mfunc.comm);
  2360. priv->mfunc.comm = NULL;
  2361. }
  2362. void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
  2363. {
  2364. struct mlx4_priv *priv = mlx4_priv(dev);
  2365. if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
  2366. dma_pool_destroy(priv->cmd.pool);
  2367. priv->cmd.pool = NULL;
  2368. }
  2369. if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
  2370. (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
  2371. iounmap(priv->cmd.hcr);
  2372. priv->cmd.hcr = NULL;
  2373. }
  2374. if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
  2375. (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
  2376. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  2377. priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
  2378. priv->mfunc.vhcr = NULL;
  2379. }
  2380. if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
  2381. priv->cmd.initialized = 0;
  2382. }
  2383. /*
  2384. * Switch to using events to issue FW commands (can only be called
  2385. * after event queue for command events has been initialized).
  2386. */
  2387. int mlx4_cmd_use_events(struct mlx4_dev *dev)
  2388. {
  2389. struct mlx4_priv *priv = mlx4_priv(dev);
  2390. int i;
  2391. int err = 0;
  2392. priv->cmd.context = kmalloc_array(priv->cmd.max_cmds,
  2393. sizeof(struct mlx4_cmd_context),
  2394. GFP_KERNEL);
  2395. if (!priv->cmd.context)
  2396. return -ENOMEM;
  2397. if (mlx4_is_mfunc(dev))
  2398. mutex_lock(&priv->cmd.slave_cmd_mutex);
  2399. down_write(&priv->cmd.switch_sem);
  2400. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2401. priv->cmd.context[i].token = i;
  2402. priv->cmd.context[i].next = i + 1;
  2403. /* To support fatal error flow, initialize all
  2404. * cmd contexts to allow simulating completions
  2405. * with complete() at any time.
  2406. */
  2407. init_completion(&priv->cmd.context[i].done);
  2408. }
  2409. priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
  2410. priv->cmd.free_head = 0;
  2411. sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
  2412. for (priv->cmd.token_mask = 1;
  2413. priv->cmd.token_mask < priv->cmd.max_cmds;
  2414. priv->cmd.token_mask <<= 1)
  2415. ; /* nothing */
  2416. --priv->cmd.token_mask;
  2417. down(&priv->cmd.poll_sem);
  2418. priv->cmd.use_events = 1;
  2419. up_write(&priv->cmd.switch_sem);
  2420. if (mlx4_is_mfunc(dev))
  2421. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  2422. return err;
  2423. }
  2424. /*
  2425. * Switch back to polling (used when shutting down the device)
  2426. */
  2427. void mlx4_cmd_use_polling(struct mlx4_dev *dev)
  2428. {
  2429. struct mlx4_priv *priv = mlx4_priv(dev);
  2430. int i;
  2431. if (mlx4_is_mfunc(dev))
  2432. mutex_lock(&priv->cmd.slave_cmd_mutex);
  2433. down_write(&priv->cmd.switch_sem);
  2434. priv->cmd.use_events = 0;
  2435. for (i = 0; i < priv->cmd.max_cmds; ++i)
  2436. down(&priv->cmd.event_sem);
  2437. kfree(priv->cmd.context);
  2438. priv->cmd.context = NULL;
  2439. up(&priv->cmd.poll_sem);
  2440. up_write(&priv->cmd.switch_sem);
  2441. if (mlx4_is_mfunc(dev))
  2442. mutex_unlock(&priv->cmd.slave_cmd_mutex);
  2443. }
  2444. struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
  2445. {
  2446. struct mlx4_cmd_mailbox *mailbox;
  2447. mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
  2448. if (!mailbox)
  2449. return ERR_PTR(-ENOMEM);
  2450. mailbox->buf = dma_pool_zalloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
  2451. &mailbox->dma);
  2452. if (!mailbox->buf) {
  2453. kfree(mailbox);
  2454. return ERR_PTR(-ENOMEM);
  2455. }
  2456. return mailbox;
  2457. }
  2458. EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
  2459. void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
  2460. struct mlx4_cmd_mailbox *mailbox)
  2461. {
  2462. if (!mailbox)
  2463. return;
  2464. dma_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
  2465. kfree(mailbox);
  2466. }
  2467. EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
  2468. u32 mlx4_comm_get_version(void)
  2469. {
  2470. return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
  2471. }
  2472. static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
  2473. {
  2474. if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
  2475. mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
  2476. vf, dev->persist->num_vfs);
  2477. return -EINVAL;
  2478. }
  2479. return vf+1;
  2480. }
  2481. int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
  2482. {
  2483. if (slave < 1 || slave > dev->persist->num_vfs) {
  2484. mlx4_err(dev,
  2485. "Bad slave number:%d (number of activated slaves: %lu)\n",
  2486. slave, dev->num_slaves);
  2487. return -EINVAL;
  2488. }
  2489. return slave - 1;
  2490. }
  2491. void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
  2492. {
  2493. struct mlx4_priv *priv = mlx4_priv(dev);
  2494. struct mlx4_cmd_context *context;
  2495. int i;
  2496. spin_lock(&priv->cmd.context_lock);
  2497. if (priv->cmd.context) {
  2498. for (i = 0; i < priv->cmd.max_cmds; ++i) {
  2499. context = &priv->cmd.context[i];
  2500. context->fw_status = CMD_STAT_INTERNAL_ERR;
  2501. context->result =
  2502. mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
  2503. complete(&context->done);
  2504. }
  2505. }
  2506. spin_unlock(&priv->cmd.context_lock);
  2507. }
  2508. struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
  2509. {
  2510. struct mlx4_active_ports actv_ports;
  2511. int vf;
  2512. bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
  2513. if (slave == 0) {
  2514. bitmap_fill(actv_ports.ports, dev->caps.num_ports);
  2515. return actv_ports;
  2516. }
  2517. vf = mlx4_get_vf_indx(dev, slave);
  2518. if (vf < 0)
  2519. return actv_ports;
  2520. bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
  2521. min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
  2522. dev->caps.num_ports));
  2523. return actv_ports;
  2524. }
  2525. EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
  2526. int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
  2527. {
  2528. unsigned n;
  2529. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2530. unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2531. if (port <= 0 || port > m)
  2532. return -EINVAL;
  2533. n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2534. if (port <= n)
  2535. port = n + 1;
  2536. return port;
  2537. }
  2538. EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
  2539. int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
  2540. {
  2541. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2542. if (test_bit(port - 1, actv_ports.ports))
  2543. return port -
  2544. find_first_bit(actv_ports.ports, dev->caps.num_ports);
  2545. return -1;
  2546. }
  2547. EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
  2548. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
  2549. int port)
  2550. {
  2551. unsigned i;
  2552. struct mlx4_slaves_pport slaves_pport;
  2553. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2554. if (port <= 0 || port > dev->caps.num_ports)
  2555. return slaves_pport;
  2556. for (i = 0; i < dev->persist->num_vfs + 1; i++) {
  2557. struct mlx4_active_ports actv_ports =
  2558. mlx4_get_active_ports(dev, i);
  2559. if (test_bit(port - 1, actv_ports.ports))
  2560. set_bit(i, slaves_pport.slaves);
  2561. }
  2562. return slaves_pport;
  2563. }
  2564. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
  2565. struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
  2566. struct mlx4_dev *dev,
  2567. const struct mlx4_active_ports *crit_ports)
  2568. {
  2569. unsigned i;
  2570. struct mlx4_slaves_pport slaves_pport;
  2571. bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
  2572. for (i = 0; i < dev->persist->num_vfs + 1; i++) {
  2573. struct mlx4_active_ports actv_ports =
  2574. mlx4_get_active_ports(dev, i);
  2575. if (bitmap_equal(crit_ports->ports, actv_ports.ports,
  2576. dev->caps.num_ports))
  2577. set_bit(i, slaves_pport.slaves);
  2578. }
  2579. return slaves_pport;
  2580. }
  2581. EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
  2582. static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
  2583. {
  2584. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
  2585. int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
  2586. + 1;
  2587. int max_port = min_port +
  2588. bitmap_weight(actv_ports.ports, dev->caps.num_ports);
  2589. if (port < min_port)
  2590. port = min_port;
  2591. else if (port >= max_port)
  2592. port = max_port - 1;
  2593. return port;
  2594. }
  2595. static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
  2596. int max_tx_rate)
  2597. {
  2598. int i;
  2599. int err;
  2600. struct mlx4_qos_manager *port_qos;
  2601. struct mlx4_dev *dev = &priv->dev;
  2602. struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
  2603. port_qos = &priv->mfunc.master.qos_ctl[port];
  2604. memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
  2605. if (slave > port_qos->num_of_qos_vfs) {
  2606. mlx4_info(dev, "No available VPP resources for this VF\n");
  2607. return -EINVAL;
  2608. }
  2609. /* Query for default QoS values from Vport 0 is needed */
  2610. err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
  2611. if (err) {
  2612. mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
  2613. return err;
  2614. }
  2615. for (i = 0; i < MLX4_NUM_UP; i++) {
  2616. if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
  2617. vpp_qos[i].max_avg_bw = max_tx_rate;
  2618. vpp_qos[i].enable = 1;
  2619. } else {
  2620. /* if user supplied tx_rate == 0, meaning no rate limit
  2621. * configuration is required. so we are leaving the
  2622. * value of max_avg_bw as queried from Vport 0.
  2623. */
  2624. vpp_qos[i].enable = 0;
  2625. }
  2626. }
  2627. err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
  2628. if (err) {
  2629. mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
  2630. return err;
  2631. }
  2632. return 0;
  2633. }
  2634. static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
  2635. struct mlx4_vport_state *vf_admin)
  2636. {
  2637. struct mlx4_qos_manager *info;
  2638. struct mlx4_priv *priv = mlx4_priv(dev);
  2639. if (!mlx4_is_master(dev) ||
  2640. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
  2641. return false;
  2642. info = &priv->mfunc.master.qos_ctl[port];
  2643. if (vf_admin->default_vlan != MLX4_VGT &&
  2644. test_bit(vf_admin->default_qos, info->priority_bm))
  2645. return true;
  2646. return false;
  2647. }
  2648. static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
  2649. struct mlx4_vport_state *vf_admin,
  2650. int vlan, int qos)
  2651. {
  2652. struct mlx4_vport_state dummy_admin = {0};
  2653. if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
  2654. !vf_admin->tx_rate)
  2655. return true;
  2656. dummy_admin.default_qos = qos;
  2657. dummy_admin.default_vlan = vlan;
  2658. /* VF wants to move to other VST state which is valid with current
  2659. * rate limit. Either differnt default vlan in VST or other
  2660. * supported QoS priority. Otherwise we don't allow this change when
  2661. * the TX rate is still configured.
  2662. */
  2663. if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
  2664. return true;
  2665. mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
  2666. (vlan == MLX4_VGT) ? "VGT" : "VST");
  2667. if (vlan != MLX4_VGT)
  2668. mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
  2669. mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
  2670. return false;
  2671. }
  2672. int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u8 *mac)
  2673. {
  2674. struct mlx4_priv *priv = mlx4_priv(dev);
  2675. struct mlx4_vport_state *s_info;
  2676. int slave;
  2677. if (!mlx4_is_master(dev))
  2678. return -EPROTONOSUPPORT;
  2679. if (is_multicast_ether_addr(mac))
  2680. return -EINVAL;
  2681. slave = mlx4_get_slave_indx(dev, vf);
  2682. if (slave < 0)
  2683. return -EINVAL;
  2684. port = mlx4_slaves_closest_port(dev, slave, port);
  2685. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2686. if (s_info->spoofchk && is_zero_ether_addr(mac)) {
  2687. mlx4_info(dev, "MAC invalidation is not allowed when spoofchk is on\n");
  2688. return -EPERM;
  2689. }
  2690. s_info->mac = mlx4_mac_to_u64(mac);
  2691. mlx4_info(dev, "default mac on vf %d port %d to %llX will take effect only after vf restart\n",
  2692. vf, port, s_info->mac);
  2693. return 0;
  2694. }
  2695. EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
  2696. int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos,
  2697. __be16 proto)
  2698. {
  2699. struct mlx4_priv *priv = mlx4_priv(dev);
  2700. struct mlx4_vport_state *vf_admin;
  2701. struct mlx4_slave_state *slave_state;
  2702. struct mlx4_vport_oper_state *vf_oper;
  2703. int slave;
  2704. if ((!mlx4_is_master(dev)) ||
  2705. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
  2706. return -EPROTONOSUPPORT;
  2707. if ((vlan > 4095) || (qos > 7))
  2708. return -EINVAL;
  2709. if (proto == htons(ETH_P_8021AD) &&
  2710. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SVLAN_BY_QP))
  2711. return -EPROTONOSUPPORT;
  2712. if (proto != htons(ETH_P_8021Q) &&
  2713. proto != htons(ETH_P_8021AD))
  2714. return -EINVAL;
  2715. if ((proto == htons(ETH_P_8021AD)) &&
  2716. ((vlan == 0) || (vlan == MLX4_VGT)))
  2717. return -EINVAL;
  2718. slave = mlx4_get_slave_indx(dev, vf);
  2719. if (slave < 0)
  2720. return -EINVAL;
  2721. slave_state = &priv->mfunc.master.slave_state[slave];
  2722. if ((proto == htons(ETH_P_8021AD)) && (slave_state->active) &&
  2723. (!slave_state->vst_qinq_supported)) {
  2724. mlx4_err(dev, "vf %d does not support VST QinQ mode\n", vf);
  2725. return -EPROTONOSUPPORT;
  2726. }
  2727. port = mlx4_slaves_closest_port(dev, slave, port);
  2728. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2729. vf_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2730. if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
  2731. return -EPERM;
  2732. if ((0 == vlan) && (0 == qos))
  2733. vf_admin->default_vlan = MLX4_VGT;
  2734. else
  2735. vf_admin->default_vlan = vlan;
  2736. vf_admin->default_qos = qos;
  2737. vf_admin->vlan_proto = proto;
  2738. /* If rate was configured prior to VST, we saved the configured rate
  2739. * in vf_admin->rate and now, if priority supported we enforce the QoS
  2740. */
  2741. if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
  2742. vf_admin->tx_rate)
  2743. vf_admin->qos_vport = slave;
  2744. /* Try to activate new vf state without restart,
  2745. * this option is not supported while moving to VST QinQ mode.
  2746. */
  2747. if ((proto == htons(ETH_P_8021AD) &&
  2748. vf_oper->state.vlan_proto != proto) ||
  2749. mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2750. mlx4_info(dev,
  2751. "updating vf %d port %d config will take effect on next VF restart\n",
  2752. vf, port);
  2753. return 0;
  2754. }
  2755. EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
  2756. int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
  2757. int max_tx_rate)
  2758. {
  2759. int err;
  2760. int slave;
  2761. struct mlx4_vport_state *vf_admin;
  2762. struct mlx4_priv *priv = mlx4_priv(dev);
  2763. if (!mlx4_is_master(dev) ||
  2764. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
  2765. return -EPROTONOSUPPORT;
  2766. if (min_tx_rate) {
  2767. mlx4_info(dev, "Minimum BW share not supported\n");
  2768. return -EPROTONOSUPPORT;
  2769. }
  2770. slave = mlx4_get_slave_indx(dev, vf);
  2771. if (slave < 0)
  2772. return -EINVAL;
  2773. port = mlx4_slaves_closest_port(dev, slave, port);
  2774. vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
  2775. err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
  2776. if (err) {
  2777. mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
  2778. max_tx_rate);
  2779. return err;
  2780. }
  2781. vf_admin->tx_rate = max_tx_rate;
  2782. /* if VF is not in supported mode (VST with supported prio),
  2783. * we do not change vport configuration for its QPs, but save
  2784. * the rate, so it will be enforced when it moves to supported
  2785. * mode next time.
  2786. */
  2787. if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
  2788. mlx4_info(dev,
  2789. "rate set for VF %d when not in valid state\n", vf);
  2790. if (vf_admin->default_vlan != MLX4_VGT)
  2791. mlx4_info(dev, "VST priority not supported by QoS\n");
  2792. else
  2793. mlx4_info(dev, "VF in VGT mode (needed VST)\n");
  2794. mlx4_info(dev,
  2795. "rate %d take affect when VF moves to valid state\n",
  2796. max_tx_rate);
  2797. return 0;
  2798. }
  2799. /* If user sets rate 0 assigning default vport for its QPs */
  2800. vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
  2801. if (priv->mfunc.master.slave_state[slave].active &&
  2802. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
  2803. mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
  2804. return 0;
  2805. }
  2806. EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
  2807. /* mlx4_get_slave_default_vlan -
  2808. * return true if VST ( default vlan)
  2809. * if VST, will return vlan & qos (if not NULL)
  2810. */
  2811. bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
  2812. u16 *vlan, u8 *qos)
  2813. {
  2814. struct mlx4_vport_oper_state *vp_oper;
  2815. struct mlx4_priv *priv;
  2816. priv = mlx4_priv(dev);
  2817. port = mlx4_slaves_closest_port(dev, slave, port);
  2818. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  2819. if (MLX4_VGT != vp_oper->state.default_vlan) {
  2820. if (vlan)
  2821. *vlan = vp_oper->state.default_vlan;
  2822. if (qos)
  2823. *qos = vp_oper->state.default_qos;
  2824. return true;
  2825. }
  2826. return false;
  2827. }
  2828. EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
  2829. int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
  2830. {
  2831. struct mlx4_priv *priv = mlx4_priv(dev);
  2832. struct mlx4_vport_state *s_info;
  2833. int slave;
  2834. u8 mac[ETH_ALEN];
  2835. if ((!mlx4_is_master(dev)) ||
  2836. !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
  2837. return -EPROTONOSUPPORT;
  2838. slave = mlx4_get_slave_indx(dev, vf);
  2839. if (slave < 0)
  2840. return -EINVAL;
  2841. port = mlx4_slaves_closest_port(dev, slave, port);
  2842. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2843. mlx4_u64_to_mac(mac, s_info->mac);
  2844. if (setting && !is_valid_ether_addr(mac)) {
  2845. mlx4_info(dev, "Illegal MAC with spoofchk\n");
  2846. return -EPERM;
  2847. }
  2848. s_info->spoofchk = setting;
  2849. return 0;
  2850. }
  2851. EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
  2852. int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
  2853. {
  2854. struct mlx4_priv *priv = mlx4_priv(dev);
  2855. struct mlx4_vport_state *s_info;
  2856. int slave;
  2857. if (!mlx4_is_master(dev))
  2858. return -EPROTONOSUPPORT;
  2859. slave = mlx4_get_slave_indx(dev, vf);
  2860. if (slave < 0)
  2861. return -EINVAL;
  2862. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2863. ivf->vf = vf;
  2864. /* need to convert it to a func */
  2865. ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
  2866. ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
  2867. ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
  2868. ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
  2869. ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
  2870. ivf->mac[5] = ((s_info->mac) & 0xff);
  2871. ivf->vlan = s_info->default_vlan;
  2872. ivf->qos = s_info->default_qos;
  2873. ivf->vlan_proto = s_info->vlan_proto;
  2874. if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
  2875. ivf->max_tx_rate = s_info->tx_rate;
  2876. else
  2877. ivf->max_tx_rate = 0;
  2878. ivf->min_tx_rate = 0;
  2879. ivf->spoofchk = s_info->spoofchk;
  2880. ivf->linkstate = s_info->link_state;
  2881. return 0;
  2882. }
  2883. EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
  2884. int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
  2885. {
  2886. struct mlx4_priv *priv = mlx4_priv(dev);
  2887. struct mlx4_vport_state *s_info;
  2888. int slave;
  2889. u8 link_stat_event;
  2890. slave = mlx4_get_slave_indx(dev, vf);
  2891. if (slave < 0)
  2892. return -EINVAL;
  2893. port = mlx4_slaves_closest_port(dev, slave, port);
  2894. switch (link_state) {
  2895. case IFLA_VF_LINK_STATE_AUTO:
  2896. /* get current link state */
  2897. if (!priv->sense.do_sense_port[port])
  2898. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2899. else
  2900. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2901. break;
  2902. case IFLA_VF_LINK_STATE_ENABLE:
  2903. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
  2904. break;
  2905. case IFLA_VF_LINK_STATE_DISABLE:
  2906. link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
  2907. break;
  2908. default:
  2909. mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
  2910. link_state, slave, port);
  2911. return -EINVAL;
  2912. }
  2913. s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
  2914. s_info->link_state = link_state;
  2915. /* send event */
  2916. mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
  2917. if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
  2918. mlx4_dbg(dev,
  2919. "updating vf %d port %d no link state HW enforcement\n",
  2920. vf, port);
  2921. return 0;
  2922. }
  2923. EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
  2924. int mlx4_get_counter_stats(struct mlx4_dev *dev, int counter_index,
  2925. struct mlx4_counter *counter_stats, int reset)
  2926. {
  2927. struct mlx4_cmd_mailbox *mailbox = NULL;
  2928. struct mlx4_counter *tmp_counter;
  2929. int err;
  2930. u32 if_stat_in_mod;
  2931. if (!counter_stats)
  2932. return -EINVAL;
  2933. if (counter_index == MLX4_SINK_COUNTER_INDEX(dev))
  2934. return 0;
  2935. mailbox = mlx4_alloc_cmd_mailbox(dev);
  2936. if (IS_ERR(mailbox))
  2937. return PTR_ERR(mailbox);
  2938. memset(mailbox->buf, 0, sizeof(struct mlx4_counter));
  2939. if_stat_in_mod = counter_index;
  2940. if (reset)
  2941. if_stat_in_mod |= MLX4_QUERY_IF_STAT_RESET;
  2942. err = mlx4_cmd_box(dev, 0, mailbox->dma,
  2943. if_stat_in_mod, 0,
  2944. MLX4_CMD_QUERY_IF_STAT,
  2945. MLX4_CMD_TIME_CLASS_C,
  2946. MLX4_CMD_NATIVE);
  2947. if (err) {
  2948. mlx4_dbg(dev, "%s: failed to read statistics for counter index %d\n",
  2949. __func__, counter_index);
  2950. goto if_stat_out;
  2951. }
  2952. tmp_counter = (struct mlx4_counter *)mailbox->buf;
  2953. counter_stats->counter_mode = tmp_counter->counter_mode;
  2954. if (counter_stats->counter_mode == 0) {
  2955. counter_stats->rx_frames =
  2956. cpu_to_be64(be64_to_cpu(counter_stats->rx_frames) +
  2957. be64_to_cpu(tmp_counter->rx_frames));
  2958. counter_stats->tx_frames =
  2959. cpu_to_be64(be64_to_cpu(counter_stats->tx_frames) +
  2960. be64_to_cpu(tmp_counter->tx_frames));
  2961. counter_stats->rx_bytes =
  2962. cpu_to_be64(be64_to_cpu(counter_stats->rx_bytes) +
  2963. be64_to_cpu(tmp_counter->rx_bytes));
  2964. counter_stats->tx_bytes =
  2965. cpu_to_be64(be64_to_cpu(counter_stats->tx_bytes) +
  2966. be64_to_cpu(tmp_counter->tx_bytes));
  2967. }
  2968. if_stat_out:
  2969. mlx4_free_cmd_mailbox(dev, mailbox);
  2970. return err;
  2971. }
  2972. EXPORT_SYMBOL_GPL(mlx4_get_counter_stats);
  2973. int mlx4_get_vf_stats(struct mlx4_dev *dev, int port, int vf_idx,
  2974. struct ifla_vf_stats *vf_stats)
  2975. {
  2976. struct mlx4_counter tmp_vf_stats;
  2977. int slave;
  2978. int err = 0;
  2979. if (!vf_stats)
  2980. return -EINVAL;
  2981. if (!mlx4_is_master(dev))
  2982. return -EPROTONOSUPPORT;
  2983. slave = mlx4_get_slave_indx(dev, vf_idx);
  2984. if (slave < 0)
  2985. return -EINVAL;
  2986. port = mlx4_slaves_closest_port(dev, slave, port);
  2987. err = mlx4_calc_vf_counters(dev, slave, port, &tmp_vf_stats);
  2988. if (!err && tmp_vf_stats.counter_mode == 0) {
  2989. vf_stats->rx_packets = be64_to_cpu(tmp_vf_stats.rx_frames);
  2990. vf_stats->tx_packets = be64_to_cpu(tmp_vf_stats.tx_frames);
  2991. vf_stats->rx_bytes = be64_to_cpu(tmp_vf_stats.rx_bytes);
  2992. vf_stats->tx_bytes = be64_to_cpu(tmp_vf_stats.tx_bytes);
  2993. }
  2994. return err;
  2995. }
  2996. EXPORT_SYMBOL_GPL(mlx4_get_vf_stats);
  2997. int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
  2998. {
  2999. struct mlx4_priv *priv = mlx4_priv(dev);
  3000. if (slave < 1 || slave >= dev->num_slaves ||
  3001. port < 1 || port > MLX4_MAX_PORTS)
  3002. return 0;
  3003. return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
  3004. MLX4_VF_SMI_ENABLED;
  3005. }
  3006. EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
  3007. int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
  3008. {
  3009. struct mlx4_priv *priv = mlx4_priv(dev);
  3010. if (slave == mlx4_master_func_num(dev))
  3011. return 1;
  3012. if (slave < 1 || slave >= dev->num_slaves ||
  3013. port < 1 || port > MLX4_MAX_PORTS)
  3014. return 0;
  3015. return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
  3016. MLX4_VF_SMI_ENABLED;
  3017. }
  3018. EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
  3019. int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
  3020. int enabled)
  3021. {
  3022. struct mlx4_priv *priv = mlx4_priv(dev);
  3023. struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
  3024. &priv->dev, slave);
  3025. int min_port = find_first_bit(actv_ports.ports,
  3026. priv->dev.caps.num_ports) + 1;
  3027. int max_port = min_port - 1 +
  3028. bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
  3029. if (slave == mlx4_master_func_num(dev))
  3030. return 0;
  3031. if (slave < 1 || slave >= dev->num_slaves ||
  3032. port < 1 || port > MLX4_MAX_PORTS ||
  3033. enabled < 0 || enabled > 1)
  3034. return -EINVAL;
  3035. if (min_port == max_port && dev->caps.num_ports > 1) {
  3036. mlx4_info(dev, "SMI access disallowed for single ported VFs\n");
  3037. return -EPROTONOSUPPORT;
  3038. }
  3039. priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
  3040. return 0;
  3041. }
  3042. EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);