You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hermon.c 114KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. * Copyright (C) 2008 Mellanox Technologies Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. FILE_LICENCE ( GPL2_OR_LATER );
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <strings.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/io.h>
  30. #include <ipxe/pci.h>
  31. #include <ipxe/pcibackup.h>
  32. #include <ipxe/malloc.h>
  33. #include <ipxe/umalloc.h>
  34. #include <ipxe/iobuf.h>
  35. #include <ipxe/netdevice.h>
  36. #include <ipxe/infiniband.h>
  37. #include <ipxe/ib_smc.h>
  38. #include <ipxe/if_ether.h>
  39. #include <ipxe/ethernet.h>
  40. #include <ipxe/fcoe.h>
  41. #include <ipxe/vlan.h>
  42. #include <ipxe/bofm.h>
  43. #include "hermon.h"
  44. /**
  45. * @file
  46. *
  47. * Mellanox Hermon Infiniband HCA
  48. *
  49. */
  50. /***************************************************************************
  51. *
  52. * Queue number allocation
  53. *
  54. ***************************************************************************
  55. */
  56. /**
  57. * Allocate offsets within usage bitmask
  58. *
  59. * @v bits Usage bitmask
  60. * @v bits_len Length of usage bitmask
  61. * @v num_bits Number of contiguous bits to allocate within bitmask
  62. * @ret bit First free bit within bitmask, or negative error
  63. */
  64. static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
  65. unsigned int bits_len,
  66. unsigned int num_bits ) {
  67. unsigned int bit = 0;
  68. hermon_bitmask_t mask = 1;
  69. unsigned int found = 0;
  70. /* Search bits for num_bits contiguous free bits */
  71. while ( bit < bits_len ) {
  72. if ( ( mask & *bits ) == 0 ) {
  73. if ( ++found == num_bits )
  74. goto found;
  75. } else {
  76. found = 0;
  77. }
  78. bit++;
  79. mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
  80. if ( mask == 1 )
  81. bits++;
  82. }
  83. return -ENFILE;
  84. found:
  85. /* Mark bits as in-use */
  86. do {
  87. *bits |= mask;
  88. if ( mask == 1 )
  89. bits--;
  90. mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
  91. } while ( --found );
  92. return ( bit - num_bits + 1 );
  93. }
  94. /**
  95. * Free offsets within usage bitmask
  96. *
  97. * @v bits Usage bitmask
  98. * @v bit Starting bit within bitmask
  99. * @v num_bits Number of contiguous bits to free within bitmask
  100. */
  101. static void hermon_bitmask_free ( hermon_bitmask_t *bits,
  102. int bit, unsigned int num_bits ) {
  103. hermon_bitmask_t mask;
  104. for ( ; num_bits ; bit++, num_bits-- ) {
  105. mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
  106. bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
  107. }
  108. }
  109. /***************************************************************************
  110. *
  111. * HCA commands
  112. *
  113. ***************************************************************************
  114. */
  115. /**
  116. * Wait for Hermon command completion
  117. *
  118. * @v hermon Hermon device
  119. * @v hcr HCA command registers
  120. * @ret rc Return status code
  121. */
  122. static int hermon_cmd_wait ( struct hermon *hermon,
  123. struct hermonprm_hca_command_register *hcr ) {
  124. unsigned int wait;
  125. for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
  126. hcr->u.dwords[6] =
  127. readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
  128. if ( ( MLX_GET ( hcr, go ) == 0 ) &&
  129. ( MLX_GET ( hcr, t ) == hermon->toggle ) )
  130. return 0;
  131. mdelay ( 1 );
  132. }
  133. return -EBUSY;
  134. }
  135. /**
  136. * Issue HCA command
  137. *
  138. * @v hermon Hermon device
  139. * @v command Command opcode, flags and input/output lengths
  140. * @v op_mod Opcode modifier (0 if no modifier applicable)
  141. * @v in Input parameters
  142. * @v in_mod Input modifier (0 if no modifier applicable)
  143. * @v out Output parameters
  144. * @ret rc Return status code
  145. */
  146. static int hermon_cmd ( struct hermon *hermon, unsigned long command,
  147. unsigned int op_mod, const void *in,
  148. unsigned int in_mod, void *out ) {
  149. struct hermonprm_hca_command_register hcr;
  150. unsigned int opcode = HERMON_HCR_OPCODE ( command );
  151. size_t in_len = HERMON_HCR_IN_LEN ( command );
  152. size_t out_len = HERMON_HCR_OUT_LEN ( command );
  153. void *in_buffer;
  154. void *out_buffer;
  155. unsigned int status;
  156. unsigned int i;
  157. int rc;
  158. assert ( in_len <= HERMON_MBOX_SIZE );
  159. assert ( out_len <= HERMON_MBOX_SIZE );
  160. DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
  161. hermon, opcode, in_len,
  162. ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
  163. ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
  164. /* Check that HCR is free */
  165. if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
  166. DBGC ( hermon, "Hermon %p command interface locked\n",
  167. hermon );
  168. return rc;
  169. }
  170. /* Flip HCR toggle */
  171. hermon->toggle = ( 1 - hermon->toggle );
  172. /* Prepare HCR */
  173. memset ( &hcr, 0, sizeof ( hcr ) );
  174. in_buffer = &hcr.u.dwords[0];
  175. if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
  176. memset ( hermon->mailbox_in, 0, HERMON_MBOX_SIZE );
  177. in_buffer = hermon->mailbox_in;
  178. MLX_FILL_H ( &hcr, 0, in_param_h, virt_to_bus ( in_buffer ) );
  179. MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
  180. }
  181. memcpy ( in_buffer, in, in_len );
  182. MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
  183. out_buffer = &hcr.u.dwords[3];
  184. if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
  185. out_buffer = hermon->mailbox_out;
  186. MLX_FILL_H ( &hcr, 3, out_param_h,
  187. virt_to_bus ( out_buffer ) );
  188. MLX_FILL_1 ( &hcr, 4, out_param_l,
  189. virt_to_bus ( out_buffer ) );
  190. }
  191. MLX_FILL_4 ( &hcr, 6,
  192. opcode, opcode,
  193. opcode_modifier, op_mod,
  194. go, 1,
  195. t, hermon->toggle );
  196. DBGC ( hermon, "Hermon %p issuing command %04x\n",
  197. hermon, opcode );
  198. DBGC2_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  199. &hcr, sizeof ( hcr ) );
  200. if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
  201. DBGC2 ( hermon, "Input mailbox:\n" );
  202. DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
  203. ( ( in_len < 512 ) ? in_len : 512 ) );
  204. }
  205. /* Issue command */
  206. for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
  207. i++ ) {
  208. writel ( hcr.u.dwords[i],
  209. hermon->config + HERMON_HCR_REG ( i ) );
  210. barrier();
  211. }
  212. /* Wait for command completion */
  213. if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
  214. DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
  215. hermon );
  216. DBGC_HDA ( hermon,
  217. virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  218. &hcr, sizeof ( hcr ) );
  219. return rc;
  220. }
  221. /* Check command status */
  222. status = MLX_GET ( &hcr, status );
  223. if ( status != 0 ) {
  224. DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
  225. hermon, status );
  226. DBGC_HDA ( hermon,
  227. virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  228. &hcr, sizeof ( hcr ) );
  229. return -EIO;
  230. }
  231. /* Read output parameters, if any */
  232. hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
  233. hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
  234. memcpy ( out, out_buffer, out_len );
  235. if ( out_len ) {
  236. DBGC2 ( hermon, "Output%s:\n",
  237. ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
  238. DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
  239. ( ( out_len < 512 ) ? out_len : 512 ) );
  240. }
  241. return 0;
  242. }
  243. static inline int
  244. hermon_cmd_query_dev_cap ( struct hermon *hermon,
  245. struct hermonprm_query_dev_cap *dev_cap ) {
  246. return hermon_cmd ( hermon,
  247. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
  248. 1, sizeof ( *dev_cap ) ),
  249. 0, NULL, 0, dev_cap );
  250. }
  251. static inline int
  252. hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
  253. return hermon_cmd ( hermon,
  254. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
  255. 1, sizeof ( *fw ) ),
  256. 0, NULL, 0, fw );
  257. }
  258. static inline int
  259. hermon_cmd_init_hca ( struct hermon *hermon,
  260. const struct hermonprm_init_hca *init_hca ) {
  261. return hermon_cmd ( hermon,
  262. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
  263. 1, sizeof ( *init_hca ) ),
  264. 0, init_hca, 0, NULL );
  265. }
  266. static inline int
  267. hermon_cmd_close_hca ( struct hermon *hermon ) {
  268. return hermon_cmd ( hermon,
  269. HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
  270. 0, NULL, 0, NULL );
  271. }
  272. static inline int
  273. hermon_cmd_init_port ( struct hermon *hermon, unsigned int port ) {
  274. return hermon_cmd ( hermon,
  275. HERMON_HCR_VOID_CMD ( HERMON_HCR_INIT_PORT ),
  276. 0, NULL, port, NULL );
  277. }
  278. static inline int
  279. hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
  280. return hermon_cmd ( hermon,
  281. HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
  282. 0, NULL, port, NULL );
  283. }
  284. static inline int
  285. hermon_cmd_set_port ( struct hermon *hermon, int is_ethernet,
  286. unsigned int port_selector,
  287. const union hermonprm_set_port *set_port ) {
  288. return hermon_cmd ( hermon,
  289. HERMON_HCR_IN_CMD ( HERMON_HCR_SET_PORT,
  290. 1, sizeof ( *set_port ) ),
  291. is_ethernet, set_port, port_selector, NULL );
  292. }
  293. static inline int
  294. hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
  295. const struct hermonprm_mpt *mpt ) {
  296. return hermon_cmd ( hermon,
  297. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
  298. 1, sizeof ( *mpt ) ),
  299. 0, mpt, index, NULL );
  300. }
  301. static inline int
  302. hermon_cmd_write_mtt ( struct hermon *hermon,
  303. const struct hermonprm_write_mtt *write_mtt ) {
  304. return hermon_cmd ( hermon,
  305. HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
  306. 1, sizeof ( *write_mtt ) ),
  307. 0, write_mtt, 1, NULL );
  308. }
  309. static inline int
  310. hermon_cmd_map_eq ( struct hermon *hermon, unsigned long index_map,
  311. const struct hermonprm_event_mask *mask ) {
  312. return hermon_cmd ( hermon,
  313. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_EQ,
  314. 0, sizeof ( *mask ) ),
  315. 0, mask, index_map, NULL );
  316. }
  317. static inline int
  318. hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
  319. const struct hermonprm_eqc *eqctx ) {
  320. return hermon_cmd ( hermon,
  321. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
  322. 1, sizeof ( *eqctx ) ),
  323. 0, eqctx, index, NULL );
  324. }
  325. static inline int
  326. hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index,
  327. struct hermonprm_eqc *eqctx ) {
  328. return hermon_cmd ( hermon,
  329. HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_EQ,
  330. 1, sizeof ( *eqctx ) ),
  331. 1, NULL, index, eqctx );
  332. }
  333. static inline int
  334. hermon_cmd_query_eq ( struct hermon *hermon, unsigned int index,
  335. struct hermonprm_eqc *eqctx ) {
  336. return hermon_cmd ( hermon,
  337. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_EQ,
  338. 1, sizeof ( *eqctx ) ),
  339. 0, NULL, index, eqctx );
  340. }
  341. static inline int
  342. hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
  343. const struct hermonprm_completion_queue_context *cqctx ){
  344. return hermon_cmd ( hermon,
  345. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
  346. 1, sizeof ( *cqctx ) ),
  347. 0, cqctx, cqn, NULL );
  348. }
  349. static inline int
  350. hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
  351. struct hermonprm_completion_queue_context *cqctx ) {
  352. return hermon_cmd ( hermon,
  353. HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
  354. 1, sizeof ( *cqctx ) ),
  355. 0, NULL, cqn, cqctx );
  356. }
  357. static inline int
  358. hermon_cmd_query_cq ( struct hermon *hermon, unsigned long cqn,
  359. struct hermonprm_completion_queue_context *cqctx ) {
  360. return hermon_cmd ( hermon,
  361. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_CQ,
  362. 1, sizeof ( *cqctx ) ),
  363. 0, NULL, cqn, cqctx );
  364. }
  365. static inline int
  366. hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
  367. const struct hermonprm_qp_ee_state_transitions *ctx ){
  368. return hermon_cmd ( hermon,
  369. HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
  370. 1, sizeof ( *ctx ) ),
  371. 0, ctx, qpn, NULL );
  372. }
  373. static inline int
  374. hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
  375. const struct hermonprm_qp_ee_state_transitions *ctx ){
  376. return hermon_cmd ( hermon,
  377. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
  378. 1, sizeof ( *ctx ) ),
  379. 0, ctx, qpn, NULL );
  380. }
  381. static inline int
  382. hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
  383. const struct hermonprm_qp_ee_state_transitions *ctx ) {
  384. return hermon_cmd ( hermon,
  385. HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
  386. 1, sizeof ( *ctx ) ),
  387. 0, ctx, qpn, NULL );
  388. }
  389. static inline int
  390. hermon_cmd_rts2rts_qp ( struct hermon *hermon, unsigned long qpn,
  391. const struct hermonprm_qp_ee_state_transitions *ctx ) {
  392. return hermon_cmd ( hermon,
  393. HERMON_HCR_IN_CMD ( HERMON_HCR_RTS2RTS_QP,
  394. 1, sizeof ( *ctx ) ),
  395. 0, ctx, qpn, NULL );
  396. }
  397. static inline int
  398. hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
  399. return hermon_cmd ( hermon,
  400. HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
  401. 0x03, NULL, qpn, NULL );
  402. }
  403. static inline int
  404. hermon_cmd_query_qp ( struct hermon *hermon, unsigned long qpn,
  405. struct hermonprm_qp_ee_state_transitions *ctx ) {
  406. return hermon_cmd ( hermon,
  407. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_QP,
  408. 1, sizeof ( *ctx ) ),
  409. 0, NULL, qpn, ctx );
  410. }
  411. static inline int
  412. hermon_cmd_conf_special_qp ( struct hermon *hermon, unsigned int internal_qps,
  413. unsigned long base_qpn ) {
  414. return hermon_cmd ( hermon,
  415. HERMON_HCR_VOID_CMD ( HERMON_HCR_CONF_SPECIAL_QP ),
  416. internal_qps, NULL, base_qpn, NULL );
  417. }
  418. static inline int
  419. hermon_cmd_mad_ifc ( struct hermon *hermon, unsigned int port,
  420. union hermonprm_mad *mad ) {
  421. return hermon_cmd ( hermon,
  422. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
  423. 1, sizeof ( *mad ),
  424. 1, sizeof ( *mad ) ),
  425. 0x03, mad, port, mad );
  426. }
  427. static inline int
  428. hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
  429. struct hermonprm_mcg_entry *mcg ) {
  430. return hermon_cmd ( hermon,
  431. HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
  432. 1, sizeof ( *mcg ) ),
  433. 0, NULL, index, mcg );
  434. }
  435. static inline int
  436. hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
  437. const struct hermonprm_mcg_entry *mcg ) {
  438. return hermon_cmd ( hermon,
  439. HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
  440. 1, sizeof ( *mcg ) ),
  441. 0, mcg, index, NULL );
  442. }
  443. static inline int
  444. hermon_cmd_mgid_hash ( struct hermon *hermon, const union ib_gid *gid,
  445. struct hermonprm_mgm_hash *hash ) {
  446. return hermon_cmd ( hermon,
  447. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
  448. 1, sizeof ( *gid ),
  449. 0, sizeof ( *hash ) ),
  450. 0, gid, 0, hash );
  451. }
  452. static inline int
  453. hermon_cmd_mod_stat_cfg ( struct hermon *hermon, unsigned int mode,
  454. unsigned int input_mod,
  455. struct hermonprm_scalar_parameter *portion ) {
  456. return hermon_cmd ( hermon,
  457. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MOD_STAT_CFG,
  458. 0, sizeof ( *portion ),
  459. 0, sizeof ( *portion ) ),
  460. mode, portion, input_mod, portion );
  461. }
  462. static inline int
  463. hermon_cmd_query_port ( struct hermon *hermon, unsigned int port,
  464. struct hermonprm_query_port_cap *query_port ) {
  465. return hermon_cmd ( hermon,
  466. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_PORT,
  467. 1, sizeof ( *query_port ) ),
  468. 0, NULL, port, query_port );
  469. }
  470. static inline int
  471. hermon_cmd_sense_port ( struct hermon *hermon, unsigned int port,
  472. struct hermonprm_sense_port *port_type ) {
  473. return hermon_cmd ( hermon,
  474. HERMON_HCR_OUT_CMD ( HERMON_HCR_SENSE_PORT,
  475. 0, sizeof ( *port_type ) ),
  476. 0, NULL, port, port_type );
  477. }
  478. static inline int
  479. hermon_cmd_run_fw ( struct hermon *hermon ) {
  480. return hermon_cmd ( hermon,
  481. HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
  482. 0, NULL, 0, NULL );
  483. }
  484. static inline int
  485. hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
  486. const struct hermonprm_scalar_parameter *offset ) {
  487. return hermon_cmd ( hermon,
  488. HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
  489. 0, sizeof ( *offset ) ),
  490. 0, offset, page_count, NULL );
  491. }
  492. static inline int
  493. hermon_cmd_map_icm ( struct hermon *hermon,
  494. const struct hermonprm_virtual_physical_mapping *map ) {
  495. return hermon_cmd ( hermon,
  496. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
  497. 1, sizeof ( *map ) ),
  498. 0, map, 1, NULL );
  499. }
  500. static inline int
  501. hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
  502. return hermon_cmd ( hermon,
  503. HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
  504. 0, NULL, 0, NULL );
  505. }
  506. static inline int
  507. hermon_cmd_map_icm_aux ( struct hermon *hermon,
  508. const struct hermonprm_virtual_physical_mapping *map ) {
  509. return hermon_cmd ( hermon,
  510. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
  511. 1, sizeof ( *map ) ),
  512. 0, map, 1, NULL );
  513. }
  514. static inline int
  515. hermon_cmd_set_icm_size ( struct hermon *hermon,
  516. const struct hermonprm_scalar_parameter *icm_size,
  517. struct hermonprm_scalar_parameter *icm_aux_size ) {
  518. return hermon_cmd ( hermon,
  519. HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
  520. 0, sizeof ( *icm_size ),
  521. 0, sizeof (*icm_aux_size) ),
  522. 0, icm_size, 0, icm_aux_size );
  523. }
  524. static inline int
  525. hermon_cmd_unmap_fa ( struct hermon *hermon ) {
  526. return hermon_cmd ( hermon,
  527. HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
  528. 0, NULL, 0, NULL );
  529. }
  530. static inline int
  531. hermon_cmd_map_fa ( struct hermon *hermon,
  532. const struct hermonprm_virtual_physical_mapping *map ) {
  533. return hermon_cmd ( hermon,
  534. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
  535. 1, sizeof ( *map ) ),
  536. 0, map, 1, NULL );
  537. }
  538. /***************************************************************************
  539. *
  540. * Memory translation table operations
  541. *
  542. ***************************************************************************
  543. */
  544. /**
  545. * Allocate MTT entries
  546. *
  547. * @v hermon Hermon device
  548. * @v memory Memory to map into MTT
  549. * @v len Length of memory to map
  550. * @v mtt MTT descriptor to fill in
  551. * @ret rc Return status code
  552. */
  553. static int hermon_alloc_mtt ( struct hermon *hermon,
  554. const void *memory, size_t len,
  555. struct hermon_mtt *mtt ) {
  556. struct hermonprm_write_mtt write_mtt;
  557. physaddr_t start;
  558. physaddr_t addr;
  559. unsigned int page_offset;
  560. unsigned int num_pages;
  561. int mtt_offset;
  562. unsigned int mtt_base_addr;
  563. unsigned int i;
  564. int rc;
  565. /* Find available MTT entries */
  566. start = virt_to_phys ( memory );
  567. page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
  568. start -= page_offset;
  569. len += page_offset;
  570. num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
  571. mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
  572. num_pages );
  573. if ( mtt_offset < 0 ) {
  574. DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
  575. hermon, num_pages );
  576. rc = mtt_offset;
  577. goto err_mtt_offset;
  578. }
  579. mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
  580. hermon->cap.mtt_entry_size );
  581. addr = start;
  582. /* Fill in MTT structure */
  583. mtt->mtt_offset = mtt_offset;
  584. mtt->num_pages = num_pages;
  585. mtt->mtt_base_addr = mtt_base_addr;
  586. mtt->page_offset = page_offset;
  587. /* Construct and issue WRITE_MTT commands */
  588. for ( i = 0 ; i < num_pages ; i++ ) {
  589. memset ( &write_mtt, 0, sizeof ( write_mtt ) );
  590. MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
  591. value, mtt_base_addr );
  592. MLX_FILL_H ( &write_mtt.mtt, 0, ptag_h, addr );
  593. MLX_FILL_2 ( &write_mtt.mtt, 1,
  594. p, 1,
  595. ptag_l, ( addr >> 3 ) );
  596. if ( ( rc = hermon_cmd_write_mtt ( hermon,
  597. &write_mtt ) ) != 0 ) {
  598. DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
  599. hermon, mtt_base_addr );
  600. goto err_write_mtt;
  601. }
  602. addr += HERMON_PAGE_SIZE;
  603. mtt_base_addr += hermon->cap.mtt_entry_size;
  604. }
  605. DBGC ( hermon, "Hermon %p MTT entries [%#x,%#x] for "
  606. "[%08lx,%08lx,%08lx,%08lx)\n", hermon, mtt->mtt_offset,
  607. ( mtt->mtt_offset + mtt->num_pages - 1 ), start,
  608. ( start + page_offset ), ( start + len ), addr );
  609. return 0;
  610. err_write_mtt:
  611. hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
  612. err_mtt_offset:
  613. return rc;
  614. }
  615. /**
  616. * Free MTT entries
  617. *
  618. * @v hermon Hermon device
  619. * @v mtt MTT descriptor
  620. */
  621. static void hermon_free_mtt ( struct hermon *hermon,
  622. struct hermon_mtt *mtt ) {
  623. DBGC ( hermon, "Hermon %p MTT entries [%#x,%#x] freed\n",
  624. hermon, mtt->mtt_offset,
  625. ( mtt->mtt_offset + mtt->num_pages - 1 ) );
  626. hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
  627. mtt->num_pages );
  628. }
  629. /***************************************************************************
  630. *
  631. * Static configuration operations
  632. *
  633. ***************************************************************************
  634. */
  635. /**
  636. * Calculate offset within static configuration
  637. *
  638. * @v field Field
  639. * @ret offset Offset
  640. */
  641. #define HERMON_MOD_STAT_CFG_OFFSET( field ) \
  642. ( ( MLX_BIT_OFFSET ( struct hermonprm_mod_stat_cfg_st, field ) / 8 ) \
  643. & ~( sizeof ( struct hermonprm_scalar_parameter ) - 1 ) )
  644. /**
  645. * Query or modify static configuration
  646. *
  647. * @v hermon Hermon device
  648. * @v port Port
  649. * @v mode Command mode
  650. * @v offset Offset within static configuration
  651. * @v stat_cfg Static configuration
  652. * @ret rc Return status code
  653. */
  654. static int hermon_mod_stat_cfg ( struct hermon *hermon, unsigned int port,
  655. unsigned int mode, unsigned int offset,
  656. struct hermonprm_mod_stat_cfg *stat_cfg ) {
  657. struct hermonprm_scalar_parameter *portion =
  658. ( ( void * ) &stat_cfg->u.bytes[offset] );
  659. struct hermonprm_mod_stat_cfg_input_mod mod;
  660. int rc;
  661. /* Sanity check */
  662. assert ( ( offset % sizeof ( *portion ) ) == 0 );
  663. /* Construct input modifier */
  664. memset ( &mod, 0, sizeof ( mod ) );
  665. MLX_FILL_2 ( &mod, 0,
  666. portnum, port,
  667. offset, offset );
  668. /* Issue command */
  669. if ( ( rc = hermon_cmd_mod_stat_cfg ( hermon, mode,
  670. be32_to_cpu ( mod.u.dwords[0] ),
  671. portion ) ) != 0 )
  672. return rc;
  673. return 0;
  674. }
  675. /***************************************************************************
  676. *
  677. * MAD operations
  678. *
  679. ***************************************************************************
  680. */
  681. /**
  682. * Issue management datagram
  683. *
  684. * @v ibdev Infiniband device
  685. * @v mad Management datagram
  686. * @ret rc Return status code
  687. */
  688. static int hermon_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
  689. struct hermon *hermon = ib_get_drvdata ( ibdev );
  690. union hermonprm_mad mad_ifc;
  691. int rc;
  692. linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
  693. mad_size_mismatch );
  694. /* Copy in request packet */
  695. memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );
  696. /* Issue MAD */
  697. if ( ( rc = hermon_cmd_mad_ifc ( hermon, ibdev->port,
  698. &mad_ifc ) ) != 0 ) {
  699. DBGC ( hermon, "Hermon %p port %d could not issue MAD IFC: "
  700. "%s\n", hermon, ibdev->port, strerror ( rc ) );
  701. return rc;
  702. }
  703. /* Copy out reply packet */
  704. memcpy ( mad, &mad_ifc.mad, sizeof ( *mad ) );
  705. if ( mad->hdr.status != 0 ) {
  706. DBGC ( hermon, "Hermon %p port %d MAD IFC status %04x\n",
  707. hermon, ibdev->port, ntohs ( mad->hdr.status ) );
  708. return -EIO;
  709. }
  710. return 0;
  711. }
  712. /***************************************************************************
  713. *
  714. * Completion queue operations
  715. *
  716. ***************************************************************************
  717. */
  718. /**
  719. * Dump completion queue context (for debugging only)
  720. *
  721. * @v hermon Hermon device
  722. * @v cq Completion queue
  723. * @ret rc Return status code
  724. */
  725. static __attribute__ (( unused )) int
  726. hermon_dump_cqctx ( struct hermon *hermon, struct ib_completion_queue *cq ) {
  727. struct hermonprm_completion_queue_context cqctx;
  728. int rc;
  729. memset ( &cqctx, 0, sizeof ( cqctx ) );
  730. if ( ( rc = hermon_cmd_query_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  731. DBGC ( hermon, "Hermon %p CQN %#lx QUERY_CQ failed: %s\n",
  732. hermon, cq->cqn, strerror ( rc ) );
  733. return rc;
  734. }
  735. DBGC ( hermon, "Hermon %p CQN %#lx context:\n", hermon, cq->cqn );
  736. DBGC_HDA ( hermon, 0, &cqctx, sizeof ( cqctx ) );
  737. return 0;
  738. }
  739. /**
  740. * Create completion queue
  741. *
  742. * @v ibdev Infiniband device
  743. * @v cq Completion queue
  744. * @ret rc Return status code
  745. */
  746. static int hermon_create_cq ( struct ib_device *ibdev,
  747. struct ib_completion_queue *cq ) {
  748. struct hermon *hermon = ib_get_drvdata ( ibdev );
  749. struct hermon_completion_queue *hermon_cq;
  750. struct hermonprm_completion_queue_context cqctx;
  751. int cqn_offset;
  752. unsigned int i;
  753. int rc;
  754. /* Find a free completion queue number */
  755. cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
  756. HERMON_MAX_CQS, 1 );
  757. if ( cqn_offset < 0 ) {
  758. DBGC ( hermon, "Hermon %p out of completion queues\n",
  759. hermon );
  760. rc = cqn_offset;
  761. goto err_cqn_offset;
  762. }
  763. cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
  764. /* Allocate control structures */
  765. hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
  766. if ( ! hermon_cq ) {
  767. rc = -ENOMEM;
  768. goto err_hermon_cq;
  769. }
  770. /* Allocate doorbell */
  771. hermon_cq->doorbell = malloc_dma ( sizeof ( hermon_cq->doorbell[0] ),
  772. sizeof ( hermon_cq->doorbell[0] ) );
  773. if ( ! hermon_cq->doorbell ) {
  774. rc = -ENOMEM;
  775. goto err_doorbell;
  776. }
  777. memset ( hermon_cq->doorbell, 0, sizeof ( hermon_cq->doorbell[0] ) );
  778. /* Allocate completion queue itself */
  779. hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
  780. hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
  781. sizeof ( hermon_cq->cqe[0] ) );
  782. if ( ! hermon_cq->cqe ) {
  783. rc = -ENOMEM;
  784. goto err_cqe;
  785. }
  786. memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
  787. for ( i = 0 ; i < cq->num_cqes ; i++ ) {
  788. MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
  789. }
  790. barrier();
  791. /* Allocate MTT entries */
  792. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
  793. hermon_cq->cqe_size,
  794. &hermon_cq->mtt ) ) != 0 )
  795. goto err_alloc_mtt;
  796. /* Hand queue over to hardware */
  797. memset ( &cqctx, 0, sizeof ( cqctx ) );
  798. MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
  799. MLX_FILL_1 ( &cqctx, 2,
  800. page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
  801. MLX_FILL_2 ( &cqctx, 3,
  802. usr_page, HERMON_UAR_NON_EQ_PAGE,
  803. log_cq_size, fls ( cq->num_cqes - 1 ) );
  804. MLX_FILL_1 ( &cqctx, 5, c_eqn, hermon->eq.eqn );
  805. MLX_FILL_H ( &cqctx, 6, mtt_base_addr_h,
  806. hermon_cq->mtt.mtt_base_addr );
  807. MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
  808. ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
  809. MLX_FILL_H ( &cqctx, 14, db_record_addr_h,
  810. virt_to_phys ( hermon_cq->doorbell ) );
  811. MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
  812. ( virt_to_phys ( hermon_cq->doorbell ) >> 3 ) );
  813. if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  814. DBGC ( hermon, "Hermon %p CQN %#lx SW2HW_CQ failed: %s\n",
  815. hermon, cq->cqn, strerror ( rc ) );
  816. goto err_sw2hw_cq;
  817. }
  818. DBGC ( hermon, "Hermon %p CQN %#lx ring [%08lx,%08lx), doorbell "
  819. "%08lx\n", hermon, cq->cqn, virt_to_phys ( hermon_cq->cqe ),
  820. ( virt_to_phys ( hermon_cq->cqe ) + hermon_cq->cqe_size ),
  821. virt_to_phys ( hermon_cq->doorbell ) );
  822. ib_cq_set_drvdata ( cq, hermon_cq );
  823. return 0;
  824. err_sw2hw_cq:
  825. hermon_free_mtt ( hermon, &hermon_cq->mtt );
  826. err_alloc_mtt:
  827. free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
  828. err_cqe:
  829. free_dma ( hermon_cq->doorbell, sizeof ( hermon_cq->doorbell[0] ) );
  830. err_doorbell:
  831. free ( hermon_cq );
  832. err_hermon_cq:
  833. hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
  834. err_cqn_offset:
  835. return rc;
  836. }
  837. /**
  838. * Destroy completion queue
  839. *
  840. * @v ibdev Infiniband device
  841. * @v cq Completion queue
  842. */
  843. static void hermon_destroy_cq ( struct ib_device *ibdev,
  844. struct ib_completion_queue *cq ) {
  845. struct hermon *hermon = ib_get_drvdata ( ibdev );
  846. struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
  847. struct hermonprm_completion_queue_context cqctx;
  848. int cqn_offset;
  849. int rc;
  850. /* Take ownership back from hardware */
  851. if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  852. DBGC ( hermon, "Hermon %p CQN %#lx FATAL HW2SW_CQ failed: "
  853. "%s\n", hermon, cq->cqn, strerror ( rc ) );
  854. /* Leak memory and return; at least we avoid corruption */
  855. return;
  856. }
  857. /* Free MTT entries */
  858. hermon_free_mtt ( hermon, &hermon_cq->mtt );
  859. /* Free memory */
  860. free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
  861. free_dma ( hermon_cq->doorbell, sizeof ( hermon_cq->doorbell[0] ) );
  862. free ( hermon_cq );
  863. /* Mark queue number as free */
  864. cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
  865. hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
  866. ib_cq_set_drvdata ( cq, NULL );
  867. }
  868. /***************************************************************************
  869. *
  870. * Queue pair operations
  871. *
  872. ***************************************************************************
  873. */
  874. /**
  875. * Assign queue pair number
  876. *
  877. * @v ibdev Infiniband device
  878. * @v qp Queue pair
  879. * @ret rc Return status code
  880. */
  881. static int hermon_alloc_qpn ( struct ib_device *ibdev,
  882. struct ib_queue_pair *qp ) {
  883. struct hermon *hermon = ib_get_drvdata ( ibdev );
  884. unsigned int port_offset;
  885. int qpn_offset;
  886. /* Calculate queue pair number */
  887. port_offset = ( ibdev->port - HERMON_PORT_BASE );
  888. switch ( qp->type ) {
  889. case IB_QPT_SMI:
  890. qp->qpn = ( hermon->special_qpn_base + port_offset );
  891. return 0;
  892. case IB_QPT_GSI:
  893. qp->qpn = ( hermon->special_qpn_base + 2 + port_offset );
  894. return 0;
  895. case IB_QPT_UD:
  896. case IB_QPT_RC:
  897. case IB_QPT_ETH:
  898. /* Find a free queue pair number */
  899. qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
  900. HERMON_MAX_QPS, 1 );
  901. if ( qpn_offset < 0 ) {
  902. DBGC ( hermon, "Hermon %p out of queue pairs\n",
  903. hermon );
  904. return qpn_offset;
  905. }
  906. qp->qpn = ( ( random() & HERMON_QPN_RANDOM_MASK ) |
  907. ( hermon->qpn_base + qpn_offset ) );
  908. return 0;
  909. default:
  910. DBGC ( hermon, "Hermon %p unsupported QP type %d\n",
  911. hermon, qp->type );
  912. return -ENOTSUP;
  913. }
  914. }
  915. /**
  916. * Free queue pair number
  917. *
  918. * @v ibdev Infiniband device
  919. * @v qp Queue pair
  920. */
  921. static void hermon_free_qpn ( struct ib_device *ibdev,
  922. struct ib_queue_pair *qp ) {
  923. struct hermon *hermon = ib_get_drvdata ( ibdev );
  924. int qpn_offset;
  925. qpn_offset = ( ( qp->qpn & ~HERMON_QPN_RANDOM_MASK )
  926. - hermon->qpn_base );
  927. if ( qpn_offset >= 0 )
  928. hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
  929. }
  930. /**
  931. * Calculate transmission rate
  932. *
  933. * @v av Address vector
  934. * @ret hermon_rate Hermon rate
  935. */
  936. static unsigned int hermon_rate ( struct ib_address_vector *av ) {
  937. return ( ( ( av->rate >= IB_RATE_2_5 ) && ( av->rate <= IB_RATE_120 ) )
  938. ? ( av->rate + 5 ) : 0 );
  939. }
  940. /**
  941. * Calculate schedule queue
  942. *
  943. * @v ibdev Infiniband device
  944. * @v qp Queue pair
  945. * @ret sched_queue Schedule queue
  946. */
  947. static unsigned int hermon_sched_queue ( struct ib_device *ibdev,
  948. struct ib_queue_pair *qp ) {
  949. return ( ( ( qp->type == IB_QPT_SMI ) ?
  950. HERMON_SCHED_QP0 : HERMON_SCHED_DEFAULT ) |
  951. ( ( ibdev->port - 1 ) << 6 ) );
  952. }
  953. /** Queue pair transport service type map */
  954. static uint8_t hermon_qp_st[] = {
  955. [IB_QPT_SMI] = HERMON_ST_MLX,
  956. [IB_QPT_GSI] = HERMON_ST_MLX,
  957. [IB_QPT_UD] = HERMON_ST_UD,
  958. [IB_QPT_RC] = HERMON_ST_RC,
  959. [IB_QPT_ETH] = HERMON_ST_MLX,
  960. };
  961. /**
  962. * Dump queue pair context (for debugging only)
  963. *
  964. * @v hermon Hermon device
  965. * @v qp Queue pair
  966. * @ret rc Return status code
  967. */
  968. static __attribute__ (( unused )) int
  969. hermon_dump_qpctx ( struct hermon *hermon, struct ib_queue_pair *qp ) {
  970. struct hermonprm_qp_ee_state_transitions qpctx;
  971. int rc;
  972. memset ( &qpctx, 0, sizeof ( qpctx ) );
  973. if ( ( rc = hermon_cmd_query_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ) {
  974. DBGC ( hermon, "Hermon %p QPN %#lx QUERY_QP failed: %s\n",
  975. hermon, qp->qpn, strerror ( rc ) );
  976. return rc;
  977. }
  978. DBGC ( hermon, "Hermon %p QPN %#lx context:\n", hermon, qp->qpn );
  979. DBGC_HDA ( hermon, 0, &qpctx.u.dwords[2], ( sizeof ( qpctx ) - 8 ) );
  980. return 0;
  981. }
  982. /**
  983. * Create queue pair
  984. *
  985. * @v ibdev Infiniband device
  986. * @v qp Queue pair
  987. * @ret rc Return status code
  988. */
  989. static int hermon_create_qp ( struct ib_device *ibdev,
  990. struct ib_queue_pair *qp ) {
  991. struct hermon *hermon = ib_get_drvdata ( ibdev );
  992. struct hermon_queue_pair *hermon_qp;
  993. struct hermonprm_qp_ee_state_transitions qpctx;
  994. int rc;
  995. /* Calculate queue pair number */
  996. if ( ( rc = hermon_alloc_qpn ( ibdev, qp ) ) != 0 )
  997. goto err_alloc_qpn;
  998. /* Allocate control structures */
  999. hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
  1000. if ( ! hermon_qp ) {
  1001. rc = -ENOMEM;
  1002. goto err_hermon_qp;
  1003. }
  1004. /* Allocate doorbells */
  1005. hermon_qp->recv.doorbell =
  1006. malloc_dma ( sizeof ( hermon_qp->recv.doorbell[0] ),
  1007. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1008. if ( ! hermon_qp->recv.doorbell ) {
  1009. rc = -ENOMEM;
  1010. goto err_recv_doorbell;
  1011. }
  1012. memset ( hermon_qp->recv.doorbell, 0,
  1013. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1014. hermon_qp->send.doorbell =
  1015. ( hermon->uar + HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE +
  1016. HERMON_DB_POST_SND_OFFSET );
  1017. /* Allocate work queue buffer */
  1018. hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
  1019. ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
  1020. hermon_qp->send.num_wqes =
  1021. ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
  1022. hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
  1023. sizeof ( hermon_qp->send.wqe[0] ) );
  1024. hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
  1025. sizeof ( hermon_qp->recv.wqe[0] ) );
  1026. hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
  1027. hermon_qp->recv.wqe_size );
  1028. hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
  1029. sizeof ( hermon_qp->send.wqe[0] ) );
  1030. if ( ! hermon_qp->wqe ) {
  1031. rc = -ENOMEM;
  1032. goto err_alloc_wqe;
  1033. }
  1034. hermon_qp->send.wqe = hermon_qp->wqe;
  1035. memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
  1036. hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
  1037. memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
  1038. /* Allocate MTT entries */
  1039. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
  1040. hermon_qp->wqe_size,
  1041. &hermon_qp->mtt ) ) != 0 ) {
  1042. goto err_alloc_mtt;
  1043. }
  1044. /* Transition queue to INIT state */
  1045. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1046. MLX_FILL_2 ( &qpctx, 2,
  1047. qpc_eec_data.pm_state, HERMON_PM_STATE_MIGRATED,
  1048. qpc_eec_data.st, hermon_qp_st[qp->type] );
  1049. MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
  1050. MLX_FILL_4 ( &qpctx, 4,
  1051. qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
  1052. qpc_eec_data.log_rq_stride,
  1053. ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
  1054. qpc_eec_data.log_sq_size,
  1055. fls ( hermon_qp->send.num_wqes - 1 ),
  1056. qpc_eec_data.log_sq_stride,
  1057. ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
  1058. MLX_FILL_1 ( &qpctx, 5,
  1059. qpc_eec_data.usr_page, HERMON_UAR_NON_EQ_PAGE );
  1060. MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
  1061. MLX_FILL_4 ( &qpctx, 38,
  1062. qpc_eec_data.rre, 1,
  1063. qpc_eec_data.rwe, 1,
  1064. qpc_eec_data.rae, 1,
  1065. qpc_eec_data.page_offset,
  1066. ( hermon_qp->mtt.page_offset >> 6 ) );
  1067. MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
  1068. MLX_FILL_H ( &qpctx, 42, qpc_eec_data.db_record_addr_h,
  1069. virt_to_phys ( hermon_qp->recv.doorbell ) );
  1070. MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
  1071. ( virt_to_phys ( hermon_qp->recv.doorbell ) >> 2 ) );
  1072. MLX_FILL_H ( &qpctx, 52, qpc_eec_data.mtt_base_addr_h,
  1073. hermon_qp->mtt.mtt_base_addr );
  1074. MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
  1075. ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
  1076. if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
  1077. &qpctx ) ) != 0 ) {
  1078. DBGC ( hermon, "Hermon %p QPN %#lx RST2INIT_QP failed: %s\n",
  1079. hermon, qp->qpn, strerror ( rc ) );
  1080. goto err_rst2init_qp;
  1081. }
  1082. hermon_qp->state = HERMON_QP_ST_INIT;
  1083. DBGC ( hermon, "Hermon %p QPN %#lx send ring [%08lx,%08lx), doorbell "
  1084. "%08lx\n", hermon, qp->qpn,
  1085. virt_to_phys ( hermon_qp->send.wqe ),
  1086. ( virt_to_phys ( hermon_qp->send.wqe ) +
  1087. hermon_qp->send.wqe_size ),
  1088. virt_to_phys ( hermon_qp->send.doorbell ) );
  1089. DBGC ( hermon, "Hermon %p QPN %#lx receive ring [%08lx,%08lx), "
  1090. "doorbell %08lx\n", hermon, qp->qpn,
  1091. virt_to_phys ( hermon_qp->recv.wqe ),
  1092. ( virt_to_phys ( hermon_qp->recv.wqe ) +
  1093. hermon_qp->recv.wqe_size ),
  1094. virt_to_phys ( hermon_qp->recv.doorbell ) );
  1095. DBGC ( hermon, "Hermon %p QPN %#lx send CQN %#lx receive CQN %#lx\n",
  1096. hermon, qp->qpn, qp->send.cq->cqn, qp->recv.cq->cqn );
  1097. ib_qp_set_drvdata ( qp, hermon_qp );
  1098. return 0;
  1099. hermon_cmd_2rst_qp ( hermon, qp->qpn );
  1100. err_rst2init_qp:
  1101. hermon_free_mtt ( hermon, &hermon_qp->mtt );
  1102. err_alloc_mtt:
  1103. free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
  1104. err_alloc_wqe:
  1105. free_dma ( hermon_qp->recv.doorbell,
  1106. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1107. err_recv_doorbell:
  1108. free ( hermon_qp );
  1109. err_hermon_qp:
  1110. hermon_free_qpn ( ibdev, qp );
  1111. err_alloc_qpn:
  1112. return rc;
  1113. }
  1114. /**
  1115. * Modify queue pair
  1116. *
  1117. * @v ibdev Infiniband device
  1118. * @v qp Queue pair
  1119. * @ret rc Return status code
  1120. */
  1121. static int hermon_modify_qp ( struct ib_device *ibdev,
  1122. struct ib_queue_pair *qp ) {
  1123. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1124. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1125. struct hermonprm_qp_ee_state_transitions qpctx;
  1126. int rc;
  1127. /* Transition queue to RTR state, if applicable */
  1128. if ( hermon_qp->state < HERMON_QP_ST_RTR ) {
  1129. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1130. MLX_FILL_2 ( &qpctx, 4,
  1131. qpc_eec_data.mtu,
  1132. ( ( qp->type == IB_QPT_ETH ) ?
  1133. HERMON_MTU_ETH : HERMON_MTU_2048 ),
  1134. qpc_eec_data.msg_max, 31 );
  1135. MLX_FILL_1 ( &qpctx, 7,
  1136. qpc_eec_data.remote_qpn_een, qp->av.qpn );
  1137. MLX_FILL_1 ( &qpctx, 9,
  1138. qpc_eec_data.primary_address_path.rlid,
  1139. qp->av.lid );
  1140. MLX_FILL_1 ( &qpctx, 10,
  1141. qpc_eec_data.primary_address_path.max_stat_rate,
  1142. hermon_rate ( &qp->av ) );
  1143. memcpy ( &qpctx.u.dwords[12], &qp->av.gid,
  1144. sizeof ( qp->av.gid ) );
  1145. MLX_FILL_1 ( &qpctx, 16,
  1146. qpc_eec_data.primary_address_path.sched_queue,
  1147. hermon_sched_queue ( ibdev, qp ) );
  1148. MLX_FILL_1 ( &qpctx, 39,
  1149. qpc_eec_data.next_rcv_psn, qp->recv.psn );
  1150. if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
  1151. &qpctx ) ) != 0 ) {
  1152. DBGC ( hermon, "Hermon %p QPN %#lx INIT2RTR_QP failed:"
  1153. " %s\n", hermon, qp->qpn, strerror ( rc ) );
  1154. return rc;
  1155. }
  1156. hermon_qp->state = HERMON_QP_ST_RTR;
  1157. }
  1158. /* Transition queue to RTS state */
  1159. if ( hermon_qp->state < HERMON_QP_ST_RTS ) {
  1160. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1161. MLX_FILL_1 ( &qpctx, 10,
  1162. qpc_eec_data.primary_address_path.ack_timeout,
  1163. 14 /* 4.096us * 2^(14) = 67ms */ );
  1164. MLX_FILL_2 ( &qpctx, 30,
  1165. qpc_eec_data.retry_count, HERMON_RETRY_MAX,
  1166. qpc_eec_data.rnr_retry, HERMON_RETRY_MAX );
  1167. MLX_FILL_1 ( &qpctx, 32,
  1168. qpc_eec_data.next_send_psn, qp->send.psn );
  1169. if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn,
  1170. &qpctx ) ) != 0 ) {
  1171. DBGC ( hermon, "Hermon %p QPN %#lx RTR2RTS_QP failed: "
  1172. "%s\n", hermon, qp->qpn, strerror ( rc ) );
  1173. return rc;
  1174. }
  1175. hermon_qp->state = HERMON_QP_ST_RTS;
  1176. }
  1177. /* Update parameters in RTS state */
  1178. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1179. MLX_FILL_1 ( &qpctx, 0, opt_param_mask, HERMON_QP_OPT_PARAM_QKEY );
  1180. MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
  1181. if ( ( rc = hermon_cmd_rts2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
  1182. DBGC ( hermon, "Hermon %p QPN %#lx RTS2RTS_QP failed: %s\n",
  1183. hermon, qp->qpn, strerror ( rc ) );
  1184. return rc;
  1185. }
  1186. return 0;
  1187. }
  1188. /**
  1189. * Destroy queue pair
  1190. *
  1191. * @v ibdev Infiniband device
  1192. * @v qp Queue pair
  1193. */
  1194. static void hermon_destroy_qp ( struct ib_device *ibdev,
  1195. struct ib_queue_pair *qp ) {
  1196. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1197. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1198. int rc;
  1199. /* Take ownership back from hardware */
  1200. if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
  1201. DBGC ( hermon, "Hermon %p QPN %#lx FATAL 2RST_QP failed: %s\n",
  1202. hermon, qp->qpn, strerror ( rc ) );
  1203. /* Leak memory and return; at least we avoid corruption */
  1204. return;
  1205. }
  1206. /* Free MTT entries */
  1207. hermon_free_mtt ( hermon, &hermon_qp->mtt );
  1208. /* Free memory */
  1209. free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
  1210. free_dma ( hermon_qp->recv.doorbell,
  1211. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1212. free ( hermon_qp );
  1213. /* Mark queue number as free */
  1214. hermon_free_qpn ( ibdev, qp );
  1215. ib_qp_set_drvdata ( qp, NULL );
  1216. }
  1217. /***************************************************************************
  1218. *
  1219. * Work request operations
  1220. *
  1221. ***************************************************************************
  1222. */
  1223. /**
  1224. * Construct UD send work queue entry
  1225. *
  1226. * @v ibdev Infiniband device
  1227. * @v qp Queue pair
  1228. * @v av Address vector
  1229. * @v iobuf I/O buffer
  1230. * @v wqe Send work queue entry
  1231. * @ret opcode Control opcode
  1232. */
  1233. static __attribute__ (( unused )) unsigned int
  1234. hermon_fill_nop_send_wqe ( struct ib_device *ibdev __unused,
  1235. struct ib_queue_pair *qp __unused,
  1236. struct ib_address_vector *av __unused,
  1237. struct io_buffer *iobuf __unused,
  1238. union hermon_send_wqe *wqe ) {
  1239. MLX_FILL_1 ( &wqe->ctrl, 1, ds, ( sizeof ( wqe->ctrl ) / 16 ) );
  1240. MLX_FILL_1 ( &wqe->ctrl, 2, c, 0x03 /* generate completion */ );
  1241. return HERMON_OPCODE_NOP;
  1242. }
  1243. /**
  1244. * Construct UD send work queue entry
  1245. *
  1246. * @v ibdev Infiniband device
  1247. * @v qp Queue pair
  1248. * @v av Address vector
  1249. * @v iobuf I/O buffer
  1250. * @v wqe Send work queue entry
  1251. * @ret opcode Control opcode
  1252. */
  1253. static unsigned int
  1254. hermon_fill_ud_send_wqe ( struct ib_device *ibdev,
  1255. struct ib_queue_pair *qp __unused,
  1256. struct ib_address_vector *av,
  1257. struct io_buffer *iobuf,
  1258. union hermon_send_wqe *wqe ) {
  1259. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1260. MLX_FILL_1 ( &wqe->ud.ctrl, 1, ds,
  1261. ( ( offsetof ( typeof ( wqe->ud ), data[1] ) / 16 ) ) );
  1262. MLX_FILL_1 ( &wqe->ud.ctrl, 2, c, 0x03 /* generate completion */ );
  1263. MLX_FILL_2 ( &wqe->ud.ud, 0,
  1264. ud_address_vector.pd, HERMON_GLOBAL_PD,
  1265. ud_address_vector.port_number, ibdev->port );
  1266. MLX_FILL_2 ( &wqe->ud.ud, 1,
  1267. ud_address_vector.rlid, av->lid,
  1268. ud_address_vector.g, av->gid_present );
  1269. MLX_FILL_1 ( &wqe->ud.ud, 2,
  1270. ud_address_vector.max_stat_rate, hermon_rate ( av ) );
  1271. MLX_FILL_1 ( &wqe->ud.ud, 3, ud_address_vector.sl, av->sl );
  1272. memcpy ( &wqe->ud.ud.u.dwords[4], &av->gid, sizeof ( av->gid ) );
  1273. MLX_FILL_1 ( &wqe->ud.ud, 8, destination_qp, av->qpn );
  1274. MLX_FILL_1 ( &wqe->ud.ud, 9, q_key, av->qkey );
  1275. MLX_FILL_1 ( &wqe->ud.data[0], 0, byte_count, iob_len ( iobuf ) );
  1276. MLX_FILL_1 ( &wqe->ud.data[0], 1, l_key, hermon->lkey );
  1277. MLX_FILL_H ( &wqe->ud.data[0], 2,
  1278. local_address_h, virt_to_bus ( iobuf->data ) );
  1279. MLX_FILL_1 ( &wqe->ud.data[0], 3,
  1280. local_address_l, virt_to_bus ( iobuf->data ) );
  1281. return HERMON_OPCODE_SEND;
  1282. }
  1283. /**
  1284. * Construct MLX send work queue entry
  1285. *
  1286. * @v ibdev Infiniband device
  1287. * @v qp Queue pair
  1288. * @v av Address vector
  1289. * @v iobuf I/O buffer
  1290. * @v wqe Send work queue entry
  1291. * @ret opcode Control opcode
  1292. */
  1293. static unsigned int
  1294. hermon_fill_mlx_send_wqe ( struct ib_device *ibdev,
  1295. struct ib_queue_pair *qp,
  1296. struct ib_address_vector *av,
  1297. struct io_buffer *iobuf,
  1298. union hermon_send_wqe *wqe ) {
  1299. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1300. struct io_buffer headers;
  1301. /* Construct IB headers */
  1302. iob_populate ( &headers, &wqe->mlx.headers, 0,
  1303. sizeof ( wqe->mlx.headers ) );
  1304. iob_reserve ( &headers, sizeof ( wqe->mlx.headers ) );
  1305. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), av );
  1306. /* Fill work queue entry */
  1307. MLX_FILL_1 ( &wqe->mlx.ctrl, 1, ds,
  1308. ( ( offsetof ( typeof ( wqe->mlx ), data[2] ) / 16 ) ) );
  1309. MLX_FILL_5 ( &wqe->mlx.ctrl, 2,
  1310. c, 0x03 /* generate completion */,
  1311. icrc, 0 /* generate ICRC */,
  1312. max_statrate, hermon_rate ( av ),
  1313. slr, 0,
  1314. v15, ( ( qp->ext_qpn == IB_QPN_SMI ) ? 1 : 0 ) );
  1315. MLX_FILL_1 ( &wqe->mlx.ctrl, 3, rlid, av->lid );
  1316. MLX_FILL_1 ( &wqe->mlx.data[0], 0,
  1317. byte_count, iob_len ( &headers ) );
  1318. MLX_FILL_1 ( &wqe->mlx.data[0], 1, l_key, hermon->lkey );
  1319. MLX_FILL_H ( &wqe->mlx.data[0], 2,
  1320. local_address_h, virt_to_bus ( headers.data ) );
  1321. MLX_FILL_1 ( &wqe->mlx.data[0], 3,
  1322. local_address_l, virt_to_bus ( headers.data ) );
  1323. MLX_FILL_1 ( &wqe->mlx.data[1], 0,
  1324. byte_count, ( iob_len ( iobuf ) + 4 /* ICRC */ ) );
  1325. MLX_FILL_1 ( &wqe->mlx.data[1], 1, l_key, hermon->lkey );
  1326. MLX_FILL_H ( &wqe->mlx.data[1], 2,
  1327. local_address_h, virt_to_bus ( iobuf->data ) );
  1328. MLX_FILL_1 ( &wqe->mlx.data[1], 3,
  1329. local_address_l, virt_to_bus ( iobuf->data ) );
  1330. return HERMON_OPCODE_SEND;
  1331. }
  1332. /**
  1333. * Construct RC send work queue entry
  1334. *
  1335. * @v ibdev Infiniband device
  1336. * @v qp Queue pair
  1337. * @v av Address vector
  1338. * @v iobuf I/O buffer
  1339. * @v wqe Send work queue entry
  1340. * @ret opcode Control opcode
  1341. */
  1342. static unsigned int
  1343. hermon_fill_rc_send_wqe ( struct ib_device *ibdev,
  1344. struct ib_queue_pair *qp __unused,
  1345. struct ib_address_vector *av __unused,
  1346. struct io_buffer *iobuf,
  1347. union hermon_send_wqe *wqe ) {
  1348. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1349. MLX_FILL_1 ( &wqe->rc.ctrl, 1, ds,
  1350. ( ( offsetof ( typeof ( wqe->rc ), data[1] ) / 16 ) ) );
  1351. MLX_FILL_1 ( &wqe->rc.ctrl, 2, c, 0x03 /* generate completion */ );
  1352. MLX_FILL_1 ( &wqe->rc.data[0], 0, byte_count, iob_len ( iobuf ) );
  1353. MLX_FILL_1 ( &wqe->rc.data[0], 1, l_key, hermon->lkey );
  1354. MLX_FILL_H ( &wqe->rc.data[0], 2,
  1355. local_address_h, virt_to_bus ( iobuf->data ) );
  1356. MLX_FILL_1 ( &wqe->rc.data[0], 3,
  1357. local_address_l, virt_to_bus ( iobuf->data ) );
  1358. return HERMON_OPCODE_SEND;
  1359. }
  1360. /**
  1361. * Construct Ethernet send work queue entry
  1362. *
  1363. * @v ibdev Infiniband device
  1364. * @v qp Queue pair
  1365. * @v av Address vector
  1366. * @v iobuf I/O buffer
  1367. * @v wqe Send work queue entry
  1368. * @ret opcode Control opcode
  1369. */
  1370. static unsigned int
  1371. hermon_fill_eth_send_wqe ( struct ib_device *ibdev,
  1372. struct ib_queue_pair *qp __unused,
  1373. struct ib_address_vector *av __unused,
  1374. struct io_buffer *iobuf,
  1375. union hermon_send_wqe *wqe ) {
  1376. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1377. /* Fill work queue entry */
  1378. MLX_FILL_1 ( &wqe->eth.ctrl, 1, ds,
  1379. ( ( offsetof ( typeof ( wqe->mlx ), data[1] ) / 16 ) ) );
  1380. MLX_FILL_2 ( &wqe->eth.ctrl, 2,
  1381. c, 0x03 /* generate completion */,
  1382. s, 1 /* inhibit ICRC */ );
  1383. MLX_FILL_1 ( &wqe->eth.data[0], 0,
  1384. byte_count, iob_len ( iobuf ) );
  1385. MLX_FILL_1 ( &wqe->eth.data[0], 1, l_key, hermon->lkey );
  1386. MLX_FILL_H ( &wqe->eth.data[0], 2,
  1387. local_address_h, virt_to_bus ( iobuf->data ) );
  1388. MLX_FILL_1 ( &wqe->eth.data[0], 3,
  1389. local_address_l, virt_to_bus ( iobuf->data ) );
  1390. return HERMON_OPCODE_SEND;
  1391. }
  1392. /** Work queue entry constructors */
  1393. static unsigned int
  1394. ( * hermon_fill_send_wqe[] ) ( struct ib_device *ibdev,
  1395. struct ib_queue_pair *qp,
  1396. struct ib_address_vector *av,
  1397. struct io_buffer *iobuf,
  1398. union hermon_send_wqe *wqe ) = {
  1399. [IB_QPT_SMI] = hermon_fill_mlx_send_wqe,
  1400. [IB_QPT_GSI] = hermon_fill_mlx_send_wqe,
  1401. [IB_QPT_UD] = hermon_fill_ud_send_wqe,
  1402. [IB_QPT_RC] = hermon_fill_rc_send_wqe,
  1403. [IB_QPT_ETH] = hermon_fill_eth_send_wqe,
  1404. };
  1405. /**
  1406. * Post send work queue entry
  1407. *
  1408. * @v ibdev Infiniband device
  1409. * @v qp Queue pair
  1410. * @v av Address vector
  1411. * @v iobuf I/O buffer
  1412. * @ret rc Return status code
  1413. */
  1414. static int hermon_post_send ( struct ib_device *ibdev,
  1415. struct ib_queue_pair *qp,
  1416. struct ib_address_vector *av,
  1417. struct io_buffer *iobuf ) {
  1418. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1419. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1420. struct ib_work_queue *wq = &qp->send;
  1421. struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
  1422. union hermon_send_wqe *wqe;
  1423. union hermonprm_doorbell_register db_reg;
  1424. unsigned long wqe_idx_mask;
  1425. unsigned long wqe_idx;
  1426. unsigned int owner;
  1427. unsigned int opcode;
  1428. /* Allocate work queue entry */
  1429. wqe_idx = ( wq->next_idx & ( hermon_send_wq->num_wqes - 1 ) );
  1430. owner = ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 );
  1431. wqe_idx_mask = ( wq->num_wqes - 1 );
  1432. if ( wq->iobufs[ wqe_idx & wqe_idx_mask ] ) {
  1433. DBGC ( hermon, "Hermon %p QPN %#lx send queue full",
  1434. hermon, qp->qpn );
  1435. return -ENOBUFS;
  1436. }
  1437. wq->iobufs[ wqe_idx & wqe_idx_mask ] = iobuf;
  1438. wqe = &hermon_send_wq->wqe[wqe_idx];
  1439. /* Construct work queue entry */
  1440. memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
  1441. ( sizeof ( *wqe ) - 4 ) );
  1442. assert ( qp->type < ( sizeof ( hermon_fill_send_wqe ) /
  1443. sizeof ( hermon_fill_send_wqe[0] ) ) );
  1444. assert ( hermon_fill_send_wqe[qp->type] != NULL );
  1445. opcode = hermon_fill_send_wqe[qp->type] ( ibdev, qp, av, iobuf, wqe );
  1446. barrier();
  1447. MLX_FILL_2 ( &wqe->ctrl, 0,
  1448. opcode, opcode,
  1449. owner, owner );
  1450. DBGCP ( hermon, "Hermon %p QPN %#lx posting send WQE %#lx:\n",
  1451. hermon, qp->qpn, wqe_idx );
  1452. DBGCP_HDA ( hermon, virt_to_phys ( wqe ), wqe, sizeof ( *wqe ) );
  1453. /* Ring doorbell register */
  1454. MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
  1455. barrier();
  1456. writel ( db_reg.dword[0], hermon_send_wq->doorbell );
  1457. /* Update work queue's index */
  1458. wq->next_idx++;
  1459. return 0;
  1460. }
  1461. /**
  1462. * Post receive work queue entry
  1463. *
  1464. * @v ibdev Infiniband device
  1465. * @v qp Queue pair
  1466. * @v iobuf I/O buffer
  1467. * @ret rc Return status code
  1468. */
  1469. static int hermon_post_recv ( struct ib_device *ibdev,
  1470. struct ib_queue_pair *qp,
  1471. struct io_buffer *iobuf ) {
  1472. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1473. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1474. struct ib_work_queue *wq = &qp->recv;
  1475. struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
  1476. struct hermonprm_recv_wqe *wqe;
  1477. unsigned int wqe_idx_mask;
  1478. /* Allocate work queue entry */
  1479. wqe_idx_mask = ( wq->num_wqes - 1 );
  1480. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  1481. DBGC ( hermon, "Hermon %p QPN %#lx receive queue full",
  1482. hermon, qp->qpn );
  1483. return -ENOBUFS;
  1484. }
  1485. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  1486. wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
  1487. /* Construct work queue entry */
  1488. MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
  1489. MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->lkey );
  1490. MLX_FILL_H ( &wqe->data[0], 2,
  1491. local_address_h, virt_to_bus ( iobuf->data ) );
  1492. MLX_FILL_1 ( &wqe->data[0], 3,
  1493. local_address_l, virt_to_bus ( iobuf->data ) );
  1494. /* Update work queue's index */
  1495. wq->next_idx++;
  1496. /* Update doorbell record */
  1497. barrier();
  1498. MLX_FILL_1 ( hermon_recv_wq->doorbell, 0, receive_wqe_counter,
  1499. ( wq->next_idx & 0xffff ) );
  1500. return 0;
  1501. }
  1502. /**
  1503. * Handle completion
  1504. *
  1505. * @v ibdev Infiniband device
  1506. * @v cq Completion queue
  1507. * @v cqe Hardware completion queue entry
  1508. * @ret rc Return status code
  1509. */
  1510. static int hermon_complete ( struct ib_device *ibdev,
  1511. struct ib_completion_queue *cq,
  1512. union hermonprm_completion_entry *cqe ) {
  1513. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1514. struct ib_work_queue *wq;
  1515. struct ib_queue_pair *qp;
  1516. struct io_buffer *iobuf;
  1517. struct ib_address_vector recv_av;
  1518. struct ib_global_route_header *grh;
  1519. struct ib_address_vector *av;
  1520. unsigned int opcode;
  1521. unsigned long qpn;
  1522. int is_send;
  1523. unsigned long wqe_idx;
  1524. unsigned long wqe_idx_mask;
  1525. size_t len;
  1526. int rc = 0;
  1527. /* Parse completion */
  1528. qpn = MLX_GET ( &cqe->normal, qpn );
  1529. is_send = MLX_GET ( &cqe->normal, s_r );
  1530. opcode = MLX_GET ( &cqe->normal, opcode );
  1531. if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
  1532. /* "s" field is not valid for error opcodes */
  1533. is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
  1534. DBGC ( hermon, "Hermon %p CQN %#lx syndrome %x vendor %x\n",
  1535. hermon, cq->cqn, MLX_GET ( &cqe->error, syndrome ),
  1536. MLX_GET ( &cqe->error, vendor_error_syndrome ) );
  1537. rc = -EIO;
  1538. /* Don't return immediately; propagate error to completer */
  1539. }
  1540. /* Identify work queue */
  1541. wq = ib_find_wq ( cq, qpn, is_send );
  1542. if ( ! wq ) {
  1543. DBGC ( hermon, "Hermon %p CQN %#lx unknown %s QPN %#lx\n",
  1544. hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
  1545. return -EIO;
  1546. }
  1547. qp = wq->qp;
  1548. /* Identify work queue entry */
  1549. wqe_idx = MLX_GET ( &cqe->normal, wqe_counter );
  1550. wqe_idx_mask = ( wq->num_wqes - 1 );
  1551. DBGCP ( hermon, "Hermon %p CQN %#lx QPN %#lx %s WQE %#lx completed:\n",
  1552. hermon, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
  1553. wqe_idx );
  1554. DBGCP_HDA ( hermon, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
  1555. /* Identify I/O buffer */
  1556. iobuf = wq->iobufs[ wqe_idx & wqe_idx_mask ];
  1557. if ( ! iobuf ) {
  1558. DBGC ( hermon, "Hermon %p CQN %#lx QPN %#lx empty %s WQE "
  1559. "%#lx\n", hermon, cq->cqn, qp->qpn,
  1560. ( is_send ? "send" : "recv" ), wqe_idx );
  1561. return -EIO;
  1562. }
  1563. wq->iobufs[ wqe_idx & wqe_idx_mask ] = NULL;
  1564. if ( is_send ) {
  1565. /* Hand off to completion handler */
  1566. ib_complete_send ( ibdev, qp, iobuf, rc );
  1567. } else {
  1568. /* Set received length */
  1569. len = MLX_GET ( &cqe->normal, byte_cnt );
  1570. assert ( len <= iob_tailroom ( iobuf ) );
  1571. iob_put ( iobuf, len );
  1572. memset ( &recv_av, 0, sizeof ( recv_av ) );
  1573. switch ( qp->type ) {
  1574. case IB_QPT_SMI:
  1575. case IB_QPT_GSI:
  1576. case IB_QPT_UD:
  1577. assert ( iob_len ( iobuf ) >= sizeof ( *grh ) );
  1578. grh = iobuf->data;
  1579. iob_pull ( iobuf, sizeof ( *grh ) );
  1580. /* Construct address vector */
  1581. av = &recv_av;
  1582. av->qpn = MLX_GET ( &cqe->normal, srq_rqpn );
  1583. av->lid = MLX_GET ( &cqe->normal, slid_smac47_32 );
  1584. av->sl = MLX_GET ( &cqe->normal, sl );
  1585. av->gid_present = MLX_GET ( &cqe->normal, g );
  1586. memcpy ( &av->gid, &grh->sgid, sizeof ( av->gid ) );
  1587. break;
  1588. case IB_QPT_RC:
  1589. av = &qp->av;
  1590. break;
  1591. case IB_QPT_ETH:
  1592. /* Construct address vector */
  1593. av = &recv_av;
  1594. av->vlan_present = MLX_GET ( &cqe->normal, vlan );
  1595. av->vlan = MLX_GET ( &cqe->normal, vid );
  1596. break;
  1597. default:
  1598. assert ( 0 );
  1599. return -EINVAL;
  1600. }
  1601. /* Hand off to completion handler */
  1602. ib_complete_recv ( ibdev, qp, av, iobuf, rc );
  1603. }
  1604. return rc;
  1605. }
  1606. /**
  1607. * Poll completion queue
  1608. *
  1609. * @v ibdev Infiniband device
  1610. * @v cq Completion queue
  1611. */
  1612. static void hermon_poll_cq ( struct ib_device *ibdev,
  1613. struct ib_completion_queue *cq ) {
  1614. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1615. struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
  1616. union hermonprm_completion_entry *cqe;
  1617. unsigned int cqe_idx_mask;
  1618. int rc;
  1619. while ( 1 ) {
  1620. /* Look for completion entry */
  1621. cqe_idx_mask = ( cq->num_cqes - 1 );
  1622. cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
  1623. if ( MLX_GET ( &cqe->normal, owner ) ^
  1624. ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
  1625. /* Entry still owned by hardware; end of poll */
  1626. break;
  1627. }
  1628. /* Handle completion */
  1629. if ( ( rc = hermon_complete ( ibdev, cq, cqe ) ) != 0 ) {
  1630. DBGC ( hermon, "Hermon %p CQN %#lx failed to complete:"
  1631. " %s\n", hermon, cq->cqn, strerror ( rc ) );
  1632. DBGC_HDA ( hermon, virt_to_phys ( cqe ),
  1633. cqe, sizeof ( *cqe ) );
  1634. }
  1635. /* Update completion queue's index */
  1636. cq->next_idx++;
  1637. /* Update doorbell record */
  1638. MLX_FILL_1 ( hermon_cq->doorbell, 0, update_ci,
  1639. ( cq->next_idx & 0x00ffffffUL ) );
  1640. }
  1641. }
  1642. /***************************************************************************
  1643. *
  1644. * Event queues
  1645. *
  1646. ***************************************************************************
  1647. */
  1648. /**
  1649. * Create event queue
  1650. *
  1651. * @v hermon Hermon device
  1652. * @ret rc Return status code
  1653. */
  1654. static int hermon_create_eq ( struct hermon *hermon ) {
  1655. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1656. struct hermonprm_eqc eqctx;
  1657. struct hermonprm_event_mask mask;
  1658. unsigned int i;
  1659. int rc;
  1660. /* Select event queue number */
  1661. hermon_eq->eqn = ( 4 * hermon->cap.reserved_uars );
  1662. if ( hermon_eq->eqn < hermon->cap.reserved_eqs )
  1663. hermon_eq->eqn = hermon->cap.reserved_eqs;
  1664. /* Calculate doorbell address */
  1665. hermon_eq->doorbell =
  1666. ( hermon->uar + HERMON_DB_EQ_OFFSET ( hermon_eq->eqn ) );
  1667. /* Allocate event queue itself */
  1668. hermon_eq->eqe_size =
  1669. ( HERMON_NUM_EQES * sizeof ( hermon_eq->eqe[0] ) );
  1670. hermon_eq->eqe = malloc_dma ( hermon_eq->eqe_size,
  1671. sizeof ( hermon_eq->eqe[0] ) );
  1672. if ( ! hermon_eq->eqe ) {
  1673. rc = -ENOMEM;
  1674. goto err_eqe;
  1675. }
  1676. memset ( hermon_eq->eqe, 0, hermon_eq->eqe_size );
  1677. for ( i = 0 ; i < HERMON_NUM_EQES ; i++ ) {
  1678. MLX_FILL_1 ( &hermon_eq->eqe[i].generic, 7, owner, 1 );
  1679. }
  1680. barrier();
  1681. /* Allocate MTT entries */
  1682. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_eq->eqe,
  1683. hermon_eq->eqe_size,
  1684. &hermon_eq->mtt ) ) != 0 )
  1685. goto err_alloc_mtt;
  1686. /* Hand queue over to hardware */
  1687. memset ( &eqctx, 0, sizeof ( eqctx ) );
  1688. MLX_FILL_2 ( &eqctx, 0,
  1689. st, 0xa /* "Fired" */,
  1690. oi, 1 );
  1691. MLX_FILL_1 ( &eqctx, 2,
  1692. page_offset, ( hermon_eq->mtt.page_offset >> 5 ) );
  1693. MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( HERMON_NUM_EQES - 1 ) );
  1694. MLX_FILL_H ( &eqctx, 6, mtt_base_addr_h,
  1695. hermon_eq->mtt.mtt_base_addr );
  1696. MLX_FILL_1 ( &eqctx, 7, mtt_base_addr_l,
  1697. ( hermon_eq->mtt.mtt_base_addr >> 3 ) );
  1698. if ( ( rc = hermon_cmd_sw2hw_eq ( hermon, hermon_eq->eqn,
  1699. &eqctx ) ) != 0 ) {
  1700. DBGC ( hermon, "Hermon %p EQN %#lx SW2HW_EQ failed: %s\n",
  1701. hermon, hermon_eq->eqn, strerror ( rc ) );
  1702. goto err_sw2hw_eq;
  1703. }
  1704. /* Map all events to this event queue */
  1705. memset ( &mask, 0xff, sizeof ( mask ) );
  1706. if ( ( rc = hermon_cmd_map_eq ( hermon,
  1707. ( HERMON_MAP_EQ | hermon_eq->eqn ),
  1708. &mask ) ) != 0 ) {
  1709. DBGC ( hermon, "Hermon %p EQN %#lx MAP_EQ failed: %s\n",
  1710. hermon, hermon_eq->eqn, strerror ( rc ) );
  1711. goto err_map_eq;
  1712. }
  1713. DBGC ( hermon, "Hermon %p EQN %#lx ring [%08lx,%08lx), doorbell "
  1714. "%08lx\n", hermon, hermon_eq->eqn,
  1715. virt_to_phys ( hermon_eq->eqe ),
  1716. ( virt_to_phys ( hermon_eq->eqe ) + hermon_eq->eqe_size ),
  1717. virt_to_phys ( hermon_eq->doorbell ) );
  1718. return 0;
  1719. err_map_eq:
  1720. hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn, &eqctx );
  1721. err_sw2hw_eq:
  1722. hermon_free_mtt ( hermon, &hermon_eq->mtt );
  1723. err_alloc_mtt:
  1724. free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
  1725. err_eqe:
  1726. memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
  1727. return rc;
  1728. }
  1729. /**
  1730. * Destroy event queue
  1731. *
  1732. * @v hermon Hermon device
  1733. */
  1734. static void hermon_destroy_eq ( struct hermon *hermon ) {
  1735. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1736. struct hermonprm_eqc eqctx;
  1737. struct hermonprm_event_mask mask;
  1738. int rc;
  1739. /* Unmap events from event queue */
  1740. memset ( &mask, 0xff, sizeof ( mask ) );
  1741. if ( ( rc = hermon_cmd_map_eq ( hermon,
  1742. ( HERMON_UNMAP_EQ | hermon_eq->eqn ),
  1743. &mask ) ) != 0 ) {
  1744. DBGC ( hermon, "Hermon %p EQN %#lx FATAL MAP_EQ failed to "
  1745. "unmap: %s\n", hermon, hermon_eq->eqn, strerror ( rc ) );
  1746. /* Continue; HCA may die but system should survive */
  1747. }
  1748. /* Take ownership back from hardware */
  1749. if ( ( rc = hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn,
  1750. &eqctx ) ) != 0 ) {
  1751. DBGC ( hermon, "Hermon %p EQN %#lx FATAL HW2SW_EQ failed: %s\n",
  1752. hermon, hermon_eq->eqn, strerror ( rc ) );
  1753. /* Leak memory and return; at least we avoid corruption */
  1754. return;
  1755. }
  1756. /* Free MTT entries */
  1757. hermon_free_mtt ( hermon, &hermon_eq->mtt );
  1758. /* Free memory */
  1759. free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
  1760. memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
  1761. }
  1762. /**
  1763. * Handle port state event
  1764. *
  1765. * @v hermon Hermon device
  1766. * @v eqe Port state change event queue entry
  1767. */
  1768. static void hermon_event_port_state_change ( struct hermon *hermon,
  1769. union hermonprm_event_entry *eqe){
  1770. unsigned int port;
  1771. int link_up;
  1772. /* Get port and link status */
  1773. port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
  1774. link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
  1775. DBGC ( hermon, "Hermon %p port %d link %s\n", hermon, ( port + 1 ),
  1776. ( link_up ? "up" : "down" ) );
  1777. /* Sanity check */
  1778. if ( port >= hermon->cap.num_ports ) {
  1779. DBGC ( hermon, "Hermon %p port %d does not exist!\n",
  1780. hermon, ( port + 1 ) );
  1781. return;
  1782. }
  1783. /* Notify device of port state change */
  1784. hermon->port[port].type->state_change ( hermon, &hermon->port[port],
  1785. link_up );
  1786. }
  1787. /**
  1788. * Poll event queue
  1789. *
  1790. * @v ibdev Infiniband device
  1791. */
  1792. static void hermon_poll_eq ( struct ib_device *ibdev ) {
  1793. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1794. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1795. union hermonprm_event_entry *eqe;
  1796. union hermonprm_doorbell_register db_reg;
  1797. unsigned int eqe_idx_mask;
  1798. unsigned int event_type;
  1799. /* No event is generated upon reaching INIT, so we must poll
  1800. * separately for link state changes while we remain DOWN.
  1801. */
  1802. if ( ib_is_open ( ibdev ) &&
  1803. ( ibdev->port_state == IB_PORT_STATE_DOWN ) ) {
  1804. ib_smc_update ( ibdev, hermon_mad );
  1805. }
  1806. /* Poll event queue */
  1807. while ( 1 ) {
  1808. /* Look for event entry */
  1809. eqe_idx_mask = ( HERMON_NUM_EQES - 1 );
  1810. eqe = &hermon_eq->eqe[hermon_eq->next_idx & eqe_idx_mask];
  1811. if ( MLX_GET ( &eqe->generic, owner ) ^
  1812. ( ( hermon_eq->next_idx & HERMON_NUM_EQES ) ? 1 : 0 ) ) {
  1813. /* Entry still owned by hardware; end of poll */
  1814. break;
  1815. }
  1816. DBGCP ( hermon, "Hermon %p EQN %#lx event:\n",
  1817. hermon, hermon_eq->eqn );
  1818. DBGCP_HDA ( hermon, virt_to_phys ( eqe ),
  1819. eqe, sizeof ( *eqe ) );
  1820. /* Handle event */
  1821. event_type = MLX_GET ( &eqe->generic, event_type );
  1822. switch ( event_type ) {
  1823. case HERMON_EV_PORT_STATE_CHANGE:
  1824. hermon_event_port_state_change ( hermon, eqe );
  1825. break;
  1826. default:
  1827. DBGC ( hermon, "Hermon %p EQN %#lx unrecognised event "
  1828. "type %#x:\n",
  1829. hermon, hermon_eq->eqn, event_type );
  1830. DBGC_HDA ( hermon, virt_to_phys ( eqe ),
  1831. eqe, sizeof ( *eqe ) );
  1832. break;
  1833. }
  1834. /* Update event queue's index */
  1835. hermon_eq->next_idx++;
  1836. /* Ring doorbell */
  1837. MLX_FILL_1 ( &db_reg.event, 0,
  1838. ci, ( hermon_eq->next_idx & 0x00ffffffUL ) );
  1839. writel ( db_reg.dword[0], hermon_eq->doorbell );
  1840. }
  1841. }
  1842. /***************************************************************************
  1843. *
  1844. * Firmware control
  1845. *
  1846. ***************************************************************************
  1847. */
  1848. /**
  1849. * Map virtual to physical address for firmware usage
  1850. *
  1851. * @v hermon Hermon device
  1852. * @v map Mapping function
  1853. * @v va Virtual address
  1854. * @v pa Physical address
  1855. * @v len Length of region
  1856. * @ret rc Return status code
  1857. */
  1858. static int hermon_map_vpm ( struct hermon *hermon,
  1859. int ( *map ) ( struct hermon *hermon,
  1860. const struct hermonprm_virtual_physical_mapping* ),
  1861. uint64_t va, physaddr_t pa, size_t len ) {
  1862. struct hermonprm_virtual_physical_mapping mapping;
  1863. physaddr_t start;
  1864. physaddr_t low;
  1865. physaddr_t high;
  1866. physaddr_t end;
  1867. size_t size;
  1868. int rc;
  1869. /* Sanity checks */
  1870. assert ( ( va & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1871. assert ( ( pa & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1872. assert ( ( len & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1873. /* Calculate starting points */
  1874. start = pa;
  1875. end = ( start + len );
  1876. size = ( 1UL << ( fls ( start ^ end ) - 1 ) );
  1877. low = high = ( end & ~( size - 1 ) );
  1878. assert ( start < low );
  1879. assert ( high <= end );
  1880. /* These mappings tend to generate huge volumes of
  1881. * uninteresting debug data, which basically makes it
  1882. * impossible to use debugging otherwise.
  1883. */
  1884. DBG_DISABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1885. /* Map blocks in descending order of size */
  1886. while ( size >= HERMON_PAGE_SIZE ) {
  1887. /* Find the next candidate block */
  1888. if ( ( low - size ) >= start ) {
  1889. low -= size;
  1890. pa = low;
  1891. } else if ( ( high + size ) <= end ) {
  1892. pa = high;
  1893. high += size;
  1894. } else {
  1895. size >>= 1;
  1896. continue;
  1897. }
  1898. assert ( ( va & ( size - 1 ) ) == 0 );
  1899. assert ( ( pa & ( size - 1 ) ) == 0 );
  1900. /* Map this block */
  1901. memset ( &mapping, 0, sizeof ( mapping ) );
  1902. MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
  1903. MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
  1904. MLX_FILL_H ( &mapping, 2, pa_h, pa );
  1905. MLX_FILL_2 ( &mapping, 3,
  1906. log2size, ( ( fls ( size ) - 1 ) - 12 ),
  1907. pa_l, ( pa >> 12 ) );
  1908. if ( ( rc = map ( hermon, &mapping ) ) != 0 ) {
  1909. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1910. DBGC ( hermon, "Hermon %p could not map %08llx+%zx to "
  1911. "%08lx: %s\n",
  1912. hermon, va, size, pa, strerror ( rc ) );
  1913. return rc;
  1914. }
  1915. va += size;
  1916. }
  1917. assert ( low == start );
  1918. assert ( high == end );
  1919. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1920. return 0;
  1921. }
  1922. /**
  1923. * Start firmware running
  1924. *
  1925. * @v hermon Hermon device
  1926. * @ret rc Return status code
  1927. */
  1928. static int hermon_start_firmware ( struct hermon *hermon ) {
  1929. struct hermonprm_query_fw fw;
  1930. unsigned int fw_pages;
  1931. size_t fw_len;
  1932. physaddr_t fw_base;
  1933. int rc;
  1934. /* Get firmware parameters */
  1935. if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
  1936. DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
  1937. hermon, strerror ( rc ) );
  1938. goto err_query_fw;
  1939. }
  1940. DBGC ( hermon, "Hermon %p firmware version %d.%d.%d\n", hermon,
  1941. MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
  1942. MLX_GET ( &fw, fw_rev_subminor ) );
  1943. fw_pages = MLX_GET ( &fw, fw_pages );
  1944. DBGC ( hermon, "Hermon %p requires %d pages (%d kB) for firmware\n",
  1945. hermon, fw_pages, ( fw_pages * 4 ) );
  1946. /* Allocate firmware pages and map firmware area */
  1947. fw_len = ( fw_pages * HERMON_PAGE_SIZE );
  1948. if ( ! hermon->firmware_area ) {
  1949. hermon->firmware_len = fw_len;
  1950. hermon->firmware_area = umalloc ( hermon->firmware_len );
  1951. if ( ! hermon->firmware_area ) {
  1952. rc = -ENOMEM;
  1953. goto err_alloc_fa;
  1954. }
  1955. } else {
  1956. assert ( hermon->firmware_len == fw_len );
  1957. }
  1958. fw_base = user_to_phys ( hermon->firmware_area, 0 );
  1959. DBGC ( hermon, "Hermon %p firmware area at physical [%08lx,%08lx)\n",
  1960. hermon, fw_base, ( fw_base + fw_len ) );
  1961. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_fa,
  1962. 0, fw_base, fw_len ) ) != 0 ) {
  1963. DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
  1964. hermon, strerror ( rc ) );
  1965. goto err_map_fa;
  1966. }
  1967. /* Start firmware */
  1968. if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
  1969. DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
  1970. hermon, strerror ( rc ) );
  1971. goto err_run_fw;
  1972. }
  1973. DBGC ( hermon, "Hermon %p firmware started\n", hermon );
  1974. return 0;
  1975. err_run_fw:
  1976. err_map_fa:
  1977. hermon_cmd_unmap_fa ( hermon );
  1978. err_alloc_fa:
  1979. err_query_fw:
  1980. return rc;
  1981. }
  1982. /**
  1983. * Stop firmware running
  1984. *
  1985. * @v hermon Hermon device
  1986. */
  1987. static void hermon_stop_firmware ( struct hermon *hermon ) {
  1988. int rc;
  1989. if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
  1990. DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
  1991. hermon, strerror ( rc ) );
  1992. /* Leak memory and return; at least we avoid corruption */
  1993. hermon->firmware_area = UNULL;
  1994. return;
  1995. }
  1996. }
  1997. /***************************************************************************
  1998. *
  1999. * Infinihost Context Memory management
  2000. *
  2001. ***************************************************************************
  2002. */
  2003. /**
  2004. * Get device limits
  2005. *
  2006. * @v hermon Hermon device
  2007. * @ret rc Return status code
  2008. */
  2009. static int hermon_get_cap ( struct hermon *hermon ) {
  2010. struct hermonprm_query_dev_cap dev_cap;
  2011. int rc;
  2012. if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
  2013. DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
  2014. hermon, strerror ( rc ) );
  2015. return rc;
  2016. }
  2017. hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
  2018. hermon->cap.reserved_qps =
  2019. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
  2020. hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
  2021. hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
  2022. hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
  2023. hermon->cap.reserved_srqs =
  2024. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
  2025. hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
  2026. hermon->cap.reserved_cqs =
  2027. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
  2028. hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
  2029. hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
  2030. if ( hermon->cap.reserved_eqs == 0 ) {
  2031. /* Backward compatibility */
  2032. hermon->cap.reserved_eqs =
  2033. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_eqs ) );
  2034. }
  2035. hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
  2036. hermon->cap.reserved_mtts =
  2037. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
  2038. hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
  2039. hermon->cap.reserved_mrws =
  2040. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
  2041. hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
  2042. hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
  2043. hermon->cap.num_ports = MLX_GET ( &dev_cap, num_ports );
  2044. hermon->cap.dpdp = MLX_GET ( &dev_cap, dpdp );
  2045. /* Sanity check */
  2046. if ( hermon->cap.num_ports > HERMON_MAX_PORTS ) {
  2047. DBGC ( hermon, "Hermon %p has %d ports (only %d supported)\n",
  2048. hermon, hermon->cap.num_ports, HERMON_MAX_PORTS );
  2049. hermon->cap.num_ports = HERMON_MAX_PORTS;
  2050. }
  2051. return 0;
  2052. }
  2053. /**
  2054. * Align ICM table
  2055. *
  2056. * @v icm_offset Current ICM offset
  2057. * @v len ICM table length
  2058. * @ret icm_offset ICM offset
  2059. */
  2060. static uint64_t icm_align ( uint64_t icm_offset, size_t len ) {
  2061. /* Round up to a multiple of the table size */
  2062. assert ( len == ( 1UL << ( fls ( len ) - 1 ) ) );
  2063. return ( ( icm_offset + len - 1 ) & ~( ( ( uint64_t ) len ) - 1 ) );
  2064. }
  2065. /**
  2066. * Map ICM (allocating if necessary)
  2067. *
  2068. * @v hermon Hermon device
  2069. * @v init_hca INIT_HCA structure to fill in
  2070. * @ret rc Return status code
  2071. */
  2072. static int hermon_map_icm ( struct hermon *hermon,
  2073. struct hermonprm_init_hca *init_hca ) {
  2074. struct hermonprm_scalar_parameter icm_size;
  2075. struct hermonprm_scalar_parameter icm_aux_size;
  2076. uint64_t icm_offset = 0;
  2077. unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
  2078. unsigned int log_num_mtts, log_num_mpts, log_num_mcs;
  2079. size_t cmpt_max_len;
  2080. size_t icm_len, icm_aux_len;
  2081. size_t len;
  2082. physaddr_t icm_phys;
  2083. int i;
  2084. int rc;
  2085. /*
  2086. * Start by carving up the ICM virtual address space
  2087. *
  2088. */
  2089. /* Calculate number of each object type within ICM */
  2090. log_num_qps = fls ( hermon->cap.reserved_qps +
  2091. HERMON_RSVD_SPECIAL_QPS + HERMON_MAX_QPS - 1 );
  2092. log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
  2093. log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
  2094. log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
  2095. log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
  2096. log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
  2097. log_num_mcs = HERMON_LOG_MULTICAST_HASH_SIZE;
  2098. /* ICM starts with the cMPT tables, which are sparse */
  2099. cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
  2100. ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
  2101. len = ( ( ( ( 1 << log_num_qps ) * hermon->cap.cmpt_entry_size ) +
  2102. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2103. hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
  2104. hermon->icm_map[HERMON_ICM_QP_CMPT].len = len;
  2105. icm_offset += cmpt_max_len;
  2106. len = ( ( ( ( 1 << log_num_srqs ) * hermon->cap.cmpt_entry_size ) +
  2107. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2108. hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
  2109. hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = len;
  2110. icm_offset += cmpt_max_len;
  2111. len = ( ( ( ( 1 << log_num_cqs ) * hermon->cap.cmpt_entry_size ) +
  2112. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2113. hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
  2114. hermon->icm_map[HERMON_ICM_CQ_CMPT].len = len;
  2115. icm_offset += cmpt_max_len;
  2116. len = ( ( ( ( 1 << log_num_eqs ) * hermon->cap.cmpt_entry_size ) +
  2117. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2118. hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
  2119. hermon->icm_map[HERMON_ICM_EQ_CMPT].len = len;
  2120. icm_offset += cmpt_max_len;
  2121. hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
  2122. /* Queue pair contexts */
  2123. len = ( ( 1 << log_num_qps ) * hermon->cap.qpc_entry_size );
  2124. icm_offset = icm_align ( icm_offset, len );
  2125. MLX_FILL_1 ( init_hca, 12,
  2126. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
  2127. ( icm_offset >> 32 ) );
  2128. MLX_FILL_2 ( init_hca, 13,
  2129. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
  2130. ( icm_offset >> 5 ),
  2131. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
  2132. log_num_qps );
  2133. DBGC ( hermon, "Hermon %p ICM QPC is %d x %#zx at [%08llx,%08llx)\n",
  2134. hermon, ( 1 << log_num_qps ), hermon->cap.qpc_entry_size,
  2135. icm_offset, ( icm_offset + len ) );
  2136. icm_offset += len;
  2137. /* Extended alternate path contexts */
  2138. len = ( ( 1 << log_num_qps ) * hermon->cap.altc_entry_size );
  2139. icm_offset = icm_align ( icm_offset, len );
  2140. MLX_FILL_1 ( init_hca, 24,
  2141. qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
  2142. ( icm_offset >> 32 ) );
  2143. MLX_FILL_1 ( init_hca, 25,
  2144. qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
  2145. icm_offset );
  2146. DBGC ( hermon, "Hermon %p ICM ALTC is %d x %#zx at [%08llx,%08llx)\n",
  2147. hermon, ( 1 << log_num_qps ), hermon->cap.altc_entry_size,
  2148. icm_offset, ( icm_offset + len ) );
  2149. icm_offset += len;
  2150. /* Extended auxiliary contexts */
  2151. len = ( ( 1 << log_num_qps ) * hermon->cap.auxc_entry_size );
  2152. icm_offset = icm_align ( icm_offset, len );
  2153. MLX_FILL_1 ( init_hca, 28,
  2154. qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
  2155. ( icm_offset >> 32 ) );
  2156. MLX_FILL_1 ( init_hca, 29,
  2157. qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
  2158. icm_offset );
  2159. DBGC ( hermon, "Hermon %p ICM AUXC is %d x %#zx at [%08llx,%08llx)\n",
  2160. hermon, ( 1 << log_num_qps ), hermon->cap.auxc_entry_size,
  2161. icm_offset, ( icm_offset + len ) );
  2162. icm_offset += len;
  2163. /* Shared receive queue contexts */
  2164. len = ( ( 1 << log_num_srqs ) * hermon->cap.srqc_entry_size );
  2165. icm_offset = icm_align ( icm_offset, len );
  2166. MLX_FILL_1 ( init_hca, 18,
  2167. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
  2168. ( icm_offset >> 32 ) );
  2169. MLX_FILL_2 ( init_hca, 19,
  2170. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
  2171. ( icm_offset >> 5 ),
  2172. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
  2173. log_num_srqs );
  2174. DBGC ( hermon, "Hermon %p ICM SRQC is %d x %#zx at [%08llx,%08llx)\n",
  2175. hermon, ( 1 << log_num_srqs ), hermon->cap.srqc_entry_size,
  2176. icm_offset, ( icm_offset + len ) );
  2177. icm_offset += len;
  2178. /* Completion queue contexts */
  2179. len = ( ( 1 << log_num_cqs ) * hermon->cap.cqc_entry_size );
  2180. icm_offset = icm_align ( icm_offset, len );
  2181. MLX_FILL_1 ( init_hca, 20,
  2182. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
  2183. ( icm_offset >> 32 ) );
  2184. MLX_FILL_2 ( init_hca, 21,
  2185. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
  2186. ( icm_offset >> 5 ),
  2187. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
  2188. log_num_cqs );
  2189. DBGC ( hermon, "Hermon %p ICM CQC is %d x %#zx at [%08llx,%08llx)\n",
  2190. hermon, ( 1 << log_num_cqs ), hermon->cap.cqc_entry_size,
  2191. icm_offset, ( icm_offset + len ) );
  2192. icm_offset += len;
  2193. /* Event queue contexts */
  2194. len = ( ( 1 << log_num_eqs ) * hermon->cap.eqc_entry_size );
  2195. icm_offset = icm_align ( icm_offset, len );
  2196. MLX_FILL_1 ( init_hca, 32,
  2197. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
  2198. ( icm_offset >> 32 ) );
  2199. MLX_FILL_2 ( init_hca, 33,
  2200. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
  2201. ( icm_offset >> 5 ),
  2202. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
  2203. log_num_eqs );
  2204. DBGC ( hermon, "Hermon %p ICM EQC is %d x %#zx at [%08llx,%08llx)\n",
  2205. hermon, ( 1 << log_num_eqs ), hermon->cap.eqc_entry_size,
  2206. icm_offset, ( icm_offset + len ) );
  2207. icm_offset += len;
  2208. /* Memory translation table */
  2209. len = ( ( 1 << log_num_mtts ) * hermon->cap.mtt_entry_size );
  2210. icm_offset = icm_align ( icm_offset, len );
  2211. MLX_FILL_1 ( init_hca, 64,
  2212. tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
  2213. MLX_FILL_1 ( init_hca, 65,
  2214. tpt_parameters.mtt_base_addr_l, icm_offset );
  2215. DBGC ( hermon, "Hermon %p ICM MTT is %d x %#zx at [%08llx,%08llx)\n",
  2216. hermon, ( 1 << log_num_mtts ), hermon->cap.mtt_entry_size,
  2217. icm_offset, ( icm_offset + len ) );
  2218. icm_offset += len;
  2219. /* Memory protection table */
  2220. len = ( ( 1 << log_num_mpts ) * hermon->cap.dmpt_entry_size );
  2221. icm_offset = icm_align ( icm_offset, len );
  2222. MLX_FILL_1 ( init_hca, 60,
  2223. tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
  2224. MLX_FILL_1 ( init_hca, 61,
  2225. tpt_parameters.dmpt_base_adr_l, icm_offset );
  2226. MLX_FILL_1 ( init_hca, 62,
  2227. tpt_parameters.log_dmpt_sz, log_num_mpts );
  2228. DBGC ( hermon, "Hermon %p ICM DMPT is %d x %#zx at [%08llx,%08llx)\n",
  2229. hermon, ( 1 << log_num_mpts ), hermon->cap.dmpt_entry_size,
  2230. icm_offset, ( icm_offset + len ) );
  2231. icm_offset += len;
  2232. /* Multicast table */
  2233. len = ( ( 1 << log_num_mcs ) * sizeof ( struct hermonprm_mcg_entry ) );
  2234. icm_offset = icm_align ( icm_offset, len );
  2235. MLX_FILL_1 ( init_hca, 48,
  2236. multicast_parameters.mc_base_addr_h,
  2237. ( icm_offset >> 32 ) );
  2238. MLX_FILL_1 ( init_hca, 49,
  2239. multicast_parameters.mc_base_addr_l, icm_offset );
  2240. MLX_FILL_1 ( init_hca, 52,
  2241. multicast_parameters.log_mc_table_entry_sz,
  2242. fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
  2243. MLX_FILL_1 ( init_hca, 53,
  2244. multicast_parameters.log_mc_table_hash_sz, log_num_mcs );
  2245. MLX_FILL_1 ( init_hca, 54,
  2246. multicast_parameters.log_mc_table_sz, log_num_mcs );
  2247. DBGC ( hermon, "Hermon %p ICM MC is %d x %#zx at [%08llx,%08llx)\n",
  2248. hermon, ( 1 << log_num_mcs ),
  2249. sizeof ( struct hermonprm_mcg_entry ),
  2250. icm_offset, ( icm_offset + len ) );
  2251. icm_offset += len;
  2252. hermon->icm_map[HERMON_ICM_OTHER].len =
  2253. ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
  2254. /*
  2255. * Allocate and map physical memory for (portions of) ICM
  2256. *
  2257. * Map is:
  2258. * ICM AUX area (aligned to its own size)
  2259. * cMPT areas
  2260. * Other areas
  2261. */
  2262. /* Calculate physical memory required for ICM */
  2263. icm_len = 0;
  2264. for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
  2265. icm_len += hermon->icm_map[i].len;
  2266. }
  2267. /* Get ICM auxiliary area size */
  2268. memset ( &icm_size, 0, sizeof ( icm_size ) );
  2269. MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
  2270. MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
  2271. if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
  2272. &icm_aux_size ) ) != 0 ) {
  2273. DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
  2274. hermon, strerror ( rc ) );
  2275. goto err_set_icm_size;
  2276. }
  2277. icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
  2278. /* Allocate ICM data and auxiliary area */
  2279. DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
  2280. hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
  2281. if ( ! hermon->icm ) {
  2282. hermon->icm_len = icm_len;
  2283. hermon->icm_aux_len = icm_aux_len;
  2284. hermon->icm = umalloc ( hermon->icm_aux_len + hermon->icm_len );
  2285. if ( ! hermon->icm ) {
  2286. rc = -ENOMEM;
  2287. goto err_alloc;
  2288. }
  2289. } else {
  2290. assert ( hermon->icm_len == icm_len );
  2291. assert ( hermon->icm_aux_len == icm_aux_len );
  2292. }
  2293. icm_phys = user_to_phys ( hermon->icm, 0 );
  2294. /* Map ICM auxiliary area */
  2295. DBGC ( hermon, "Hermon %p mapping ICM AUX => %08lx\n",
  2296. hermon, icm_phys );
  2297. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm_aux,
  2298. 0, icm_phys, icm_aux_len ) ) != 0 ) {
  2299. DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
  2300. hermon, strerror ( rc ) );
  2301. goto err_map_icm_aux;
  2302. }
  2303. icm_phys += icm_aux_len;
  2304. /* MAP ICM area */
  2305. for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
  2306. DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx => %08lx\n",
  2307. hermon, hermon->icm_map[i].offset,
  2308. hermon->icm_map[i].len, icm_phys );
  2309. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm,
  2310. hermon->icm_map[i].offset,
  2311. icm_phys,
  2312. hermon->icm_map[i].len ) ) != 0 ){
  2313. DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
  2314. hermon, strerror ( rc ) );
  2315. goto err_map_icm;
  2316. }
  2317. icm_phys += hermon->icm_map[i].len;
  2318. }
  2319. return 0;
  2320. err_map_icm:
  2321. assert ( i == 0 ); /* We don't handle partial failure at present */
  2322. err_map_icm_aux:
  2323. hermon_cmd_unmap_icm_aux ( hermon );
  2324. err_alloc:
  2325. err_set_icm_size:
  2326. return rc;
  2327. }
  2328. /**
  2329. * Unmap ICM
  2330. *
  2331. * @v hermon Hermon device
  2332. */
  2333. static void hermon_unmap_icm ( struct hermon *hermon ) {
  2334. struct hermonprm_scalar_parameter unmap_icm;
  2335. int i;
  2336. for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
  2337. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2338. MLX_FILL_1 ( &unmap_icm, 0, value_hi,
  2339. ( hermon->icm_map[i].offset >> 32 ) );
  2340. MLX_FILL_1 ( &unmap_icm, 1, value,
  2341. hermon->icm_map[i].offset );
  2342. hermon_cmd_unmap_icm ( hermon,
  2343. ( 1 << fls ( ( hermon->icm_map[i].len /
  2344. HERMON_PAGE_SIZE ) - 1)),
  2345. &unmap_icm );
  2346. }
  2347. hermon_cmd_unmap_icm_aux ( hermon );
  2348. }
  2349. /***************************************************************************
  2350. *
  2351. * Initialisation and teardown
  2352. *
  2353. ***************************************************************************
  2354. */
  2355. /**
  2356. * Reset device
  2357. *
  2358. * @v hermon Hermon device
  2359. */
  2360. static void hermon_reset ( struct hermon *hermon ) {
  2361. struct pci_device *pci = hermon->pci;
  2362. struct pci_config_backup backup;
  2363. static const uint8_t backup_exclude[] =
  2364. PCI_CONFIG_BACKUP_EXCLUDE ( 0x58, 0x5c );
  2365. /* Perform device reset and preserve PCI configuration */
  2366. pci_backup ( pci, &backup, backup_exclude );
  2367. writel ( HERMON_RESET_MAGIC,
  2368. ( hermon->config + HERMON_RESET_OFFSET ) );
  2369. mdelay ( HERMON_RESET_WAIT_TIME_MS );
  2370. pci_restore ( pci, &backup, backup_exclude );
  2371. /* Reset command interface toggle */
  2372. hermon->toggle = 0;
  2373. }
  2374. /**
  2375. * Set up memory protection table
  2376. *
  2377. * @v hermon Hermon device
  2378. * @ret rc Return status code
  2379. */
  2380. static int hermon_setup_mpt ( struct hermon *hermon ) {
  2381. struct hermonprm_mpt mpt;
  2382. uint32_t key;
  2383. int rc;
  2384. /* Derive key */
  2385. key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
  2386. hermon->lkey = ( ( key << 8 ) | ( key >> 24 ) );
  2387. /* Initialise memory protection table */
  2388. memset ( &mpt, 0, sizeof ( mpt ) );
  2389. MLX_FILL_7 ( &mpt, 0,
  2390. atomic, 1,
  2391. rw, 1,
  2392. rr, 1,
  2393. lw, 1,
  2394. lr, 1,
  2395. pa, 1,
  2396. r_w, 1 );
  2397. MLX_FILL_1 ( &mpt, 2, mem_key, key );
  2398. MLX_FILL_1 ( &mpt, 3,
  2399. pd, HERMON_GLOBAL_PD );
  2400. MLX_FILL_1 ( &mpt, 10, len64, 1 );
  2401. if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
  2402. hermon->cap.reserved_mrws,
  2403. &mpt ) ) != 0 ) {
  2404. DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
  2405. hermon, strerror ( rc ) );
  2406. return rc;
  2407. }
  2408. return 0;
  2409. }
  2410. /**
  2411. * Configure special queue pairs
  2412. *
  2413. * @v hermon Hermon device
  2414. * @ret rc Return status code
  2415. */
  2416. static int hermon_configure_special_qps ( struct hermon *hermon ) {
  2417. int rc;
  2418. /* Special QP block must be aligned on its own size */
  2419. hermon->special_qpn_base = ( ( hermon->cap.reserved_qps +
  2420. HERMON_NUM_SPECIAL_QPS - 1 )
  2421. & ~( HERMON_NUM_SPECIAL_QPS - 1 ) );
  2422. hermon->qpn_base = ( hermon->special_qpn_base +
  2423. HERMON_NUM_SPECIAL_QPS );
  2424. DBGC ( hermon, "Hermon %p special QPs at [%lx,%lx]\n", hermon,
  2425. hermon->special_qpn_base, ( hermon->qpn_base - 1 ) );
  2426. /* Issue command to configure special QPs */
  2427. if ( ( rc = hermon_cmd_conf_special_qp ( hermon, 0x00,
  2428. hermon->special_qpn_base ) ) != 0 ) {
  2429. DBGC ( hermon, "Hermon %p could not configure special QPs: "
  2430. "%s\n", hermon, strerror ( rc ) );
  2431. return rc;
  2432. }
  2433. return 0;
  2434. }
  2435. /**
  2436. * Start Hermon device
  2437. *
  2438. * @v hermon Hermon device
  2439. * @v running Firmware is already running
  2440. * @ret rc Return status code
  2441. */
  2442. static int hermon_start ( struct hermon *hermon, int running ) {
  2443. struct hermonprm_init_hca init_hca;
  2444. unsigned int i;
  2445. int rc;
  2446. /* Start firmware if not already running */
  2447. if ( ! running ) {
  2448. if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
  2449. goto err_start_firmware;
  2450. }
  2451. /* Allocate and map ICM */
  2452. memset ( &init_hca, 0, sizeof ( init_hca ) );
  2453. if ( ( rc = hermon_map_icm ( hermon, &init_hca ) ) != 0 )
  2454. goto err_map_icm;
  2455. /* Initialise HCA */
  2456. MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
  2457. MLX_FILL_1 ( &init_hca, 5, udp, 1 );
  2458. MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
  2459. if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
  2460. DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
  2461. hermon, strerror ( rc ) );
  2462. goto err_init_hca;
  2463. }
  2464. /* Set up memory protection */
  2465. if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
  2466. goto err_setup_mpt;
  2467. for ( i = 0 ; i < hermon->cap.num_ports ; i++ )
  2468. hermon->port[i].ibdev->rdma_key = hermon->lkey;
  2469. /* Set up event queue */
  2470. if ( ( rc = hermon_create_eq ( hermon ) ) != 0 )
  2471. goto err_create_eq;
  2472. /* Configure special QPs */
  2473. if ( ( rc = hermon_configure_special_qps ( hermon ) ) != 0 )
  2474. goto err_conf_special_qps;
  2475. return 0;
  2476. err_conf_special_qps:
  2477. hermon_destroy_eq ( hermon );
  2478. err_create_eq:
  2479. err_setup_mpt:
  2480. hermon_cmd_close_hca ( hermon );
  2481. err_init_hca:
  2482. hermon_unmap_icm ( hermon );
  2483. err_map_icm:
  2484. hermon_stop_firmware ( hermon );
  2485. err_start_firmware:
  2486. return rc;
  2487. }
  2488. /**
  2489. * Stop Hermon device
  2490. *
  2491. * @v hermon Hermon device
  2492. */
  2493. static void hermon_stop ( struct hermon *hermon ) {
  2494. hermon_destroy_eq ( hermon );
  2495. hermon_cmd_close_hca ( hermon );
  2496. hermon_unmap_icm ( hermon );
  2497. hermon_stop_firmware ( hermon );
  2498. hermon_reset ( hermon );
  2499. }
  2500. /**
  2501. * Open Hermon device
  2502. *
  2503. * @v hermon Hermon device
  2504. * @ret rc Return status code
  2505. */
  2506. static int hermon_open ( struct hermon *hermon ) {
  2507. int rc;
  2508. /* Start device if applicable */
  2509. if ( hermon->open_count == 0 ) {
  2510. if ( ( rc = hermon_start ( hermon, 0 ) ) != 0 )
  2511. return rc;
  2512. }
  2513. /* Increment open counter */
  2514. hermon->open_count++;
  2515. return 0;
  2516. }
  2517. /**
  2518. * Close Hermon device
  2519. *
  2520. * @v hermon Hermon device
  2521. */
  2522. static void hermon_close ( struct hermon *hermon ) {
  2523. /* Decrement open counter */
  2524. assert ( hermon->open_count != 0 );
  2525. hermon->open_count--;
  2526. /* Stop device if applicable */
  2527. if ( hermon->open_count == 0 )
  2528. hermon_stop ( hermon );
  2529. }
  2530. /***************************************************************************
  2531. *
  2532. * Infiniband link-layer operations
  2533. *
  2534. ***************************************************************************
  2535. */
  2536. /**
  2537. * Initialise Infiniband link
  2538. *
  2539. * @v ibdev Infiniband device
  2540. * @ret rc Return status code
  2541. */
  2542. static int hermon_ib_open ( struct ib_device *ibdev ) {
  2543. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2544. union hermonprm_set_port set_port;
  2545. int rc;
  2546. /* Open hardware */
  2547. if ( ( rc = hermon_open ( hermon ) ) != 0 )
  2548. goto err_open;
  2549. /* Set port parameters */
  2550. memset ( &set_port, 0, sizeof ( set_port ) );
  2551. MLX_FILL_8 ( &set_port.ib, 0,
  2552. mmc, 1,
  2553. mvc, 1,
  2554. mp, 1,
  2555. mg, 1,
  2556. mtu_cap, IB_MTU_2048,
  2557. vl_cap, IB_VL_0,
  2558. rcm, 1,
  2559. lss, 1 );
  2560. MLX_FILL_2 ( &set_port.ib, 10,
  2561. max_pkey, 1,
  2562. max_gid, 1 );
  2563. MLX_FILL_1 ( &set_port.ib, 28,
  2564. link_speed_supported, 1 );
  2565. if ( ( rc = hermon_cmd_set_port ( hermon, 0, ibdev->port,
  2566. &set_port ) ) != 0 ) {
  2567. DBGC ( hermon, "Hermon %p port %d could not set port: %s\n",
  2568. hermon, ibdev->port, strerror ( rc ) );
  2569. goto err_set_port;
  2570. }
  2571. /* Initialise port */
  2572. if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port ) ) != 0 ) {
  2573. DBGC ( hermon, "Hermon %p port %d could not initialise port: "
  2574. "%s\n", hermon, ibdev->port, strerror ( rc ) );
  2575. goto err_init_port;
  2576. }
  2577. /* Update MAD parameters */
  2578. ib_smc_update ( ibdev, hermon_mad );
  2579. return 0;
  2580. err_init_port:
  2581. err_set_port:
  2582. hermon_close ( hermon );
  2583. err_open:
  2584. return rc;
  2585. }
  2586. /**
  2587. * Close Infiniband link
  2588. *
  2589. * @v ibdev Infiniband device
  2590. */
  2591. static void hermon_ib_close ( struct ib_device *ibdev ) {
  2592. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2593. int rc;
  2594. /* Close port */
  2595. if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
  2596. DBGC ( hermon, "Hermon %p port %d could not close port: %s\n",
  2597. hermon, ibdev->port, strerror ( rc ) );
  2598. /* Nothing we can do about this */
  2599. }
  2600. /* Close hardware */
  2601. hermon_close ( hermon );
  2602. }
  2603. /**
  2604. * Inform embedded subnet management agent of a received MAD
  2605. *
  2606. * @v ibdev Infiniband device
  2607. * @v mad MAD
  2608. * @ret rc Return status code
  2609. */
  2610. static int hermon_inform_sma ( struct ib_device *ibdev,
  2611. union ib_mad *mad ) {
  2612. int rc;
  2613. /* Send the MAD to the embedded SMA */
  2614. if ( ( rc = hermon_mad ( ibdev, mad ) ) != 0 )
  2615. return rc;
  2616. /* Update parameters held in software */
  2617. ib_smc_update ( ibdev, hermon_mad );
  2618. return 0;
  2619. }
  2620. /***************************************************************************
  2621. *
  2622. * Multicast group operations
  2623. *
  2624. ***************************************************************************
  2625. */
  2626. /**
  2627. * Attach to multicast group
  2628. *
  2629. * @v ibdev Infiniband device
  2630. * @v qp Queue pair
  2631. * @v gid Multicast GID
  2632. * @ret rc Return status code
  2633. */
  2634. static int hermon_mcast_attach ( struct ib_device *ibdev,
  2635. struct ib_queue_pair *qp,
  2636. union ib_gid *gid ) {
  2637. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2638. struct hermonprm_mgm_hash hash;
  2639. struct hermonprm_mcg_entry mcg;
  2640. unsigned int index;
  2641. int rc;
  2642. /* Generate hash table index */
  2643. if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
  2644. DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
  2645. hermon, strerror ( rc ) );
  2646. return rc;
  2647. }
  2648. index = MLX_GET ( &hash, hash );
  2649. /* Check for existing hash table entry */
  2650. if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
  2651. DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
  2652. hermon, index, strerror ( rc ) );
  2653. return rc;
  2654. }
  2655. if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
  2656. /* FIXME: this implementation allows only a single QP
  2657. * per multicast group, and doesn't handle hash
  2658. * collisions. Sufficient for IPoIB but may need to
  2659. * be extended in future.
  2660. */
  2661. DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
  2662. hermon, index );
  2663. return -EBUSY;
  2664. }
  2665. /* Update hash table entry */
  2666. MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
  2667. MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
  2668. memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
  2669. if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
  2670. DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
  2671. hermon, index, strerror ( rc ) );
  2672. return rc;
  2673. }
  2674. return 0;
  2675. }
  2676. /**
  2677. * Detach from multicast group
  2678. *
  2679. * @v ibdev Infiniband device
  2680. * @v qp Queue pair
  2681. * @v gid Multicast GID
  2682. */
  2683. static void hermon_mcast_detach ( struct ib_device *ibdev,
  2684. struct ib_queue_pair *qp __unused,
  2685. union ib_gid *gid ) {
  2686. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2687. struct hermonprm_mgm_hash hash;
  2688. struct hermonprm_mcg_entry mcg;
  2689. unsigned int index;
  2690. int rc;
  2691. /* Generate hash table index */
  2692. if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
  2693. DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
  2694. hermon, strerror ( rc ) );
  2695. return;
  2696. }
  2697. index = MLX_GET ( &hash, hash );
  2698. /* Clear hash table entry */
  2699. memset ( &mcg, 0, sizeof ( mcg ) );
  2700. if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
  2701. DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
  2702. hermon, index, strerror ( rc ) );
  2703. return;
  2704. }
  2705. }
  2706. /** Hermon Infiniband operations */
  2707. static struct ib_device_operations hermon_ib_operations = {
  2708. .create_cq = hermon_create_cq,
  2709. .destroy_cq = hermon_destroy_cq,
  2710. .create_qp = hermon_create_qp,
  2711. .modify_qp = hermon_modify_qp,
  2712. .destroy_qp = hermon_destroy_qp,
  2713. .post_send = hermon_post_send,
  2714. .post_recv = hermon_post_recv,
  2715. .poll_cq = hermon_poll_cq,
  2716. .poll_eq = hermon_poll_eq,
  2717. .open = hermon_ib_open,
  2718. .close = hermon_ib_close,
  2719. .mcast_attach = hermon_mcast_attach,
  2720. .mcast_detach = hermon_mcast_detach,
  2721. .set_port_info = hermon_inform_sma,
  2722. .set_pkey_table = hermon_inform_sma,
  2723. };
  2724. /**
  2725. * Register Hermon Infiniband device
  2726. *
  2727. * @v hermon Hermon device
  2728. * @v port Hermon port
  2729. * @ret rc Return status code
  2730. */
  2731. static int hermon_register_ibdev ( struct hermon *hermon,
  2732. struct hermon_port *port ) {
  2733. struct ib_device *ibdev = port->ibdev;
  2734. int rc;
  2735. /* Initialise parameters using SMC */
  2736. ib_smc_init ( ibdev, hermon_mad );
  2737. /* Register Infiniband device */
  2738. if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
  2739. DBGC ( hermon, "Hermon %p port %d could not register IB "
  2740. "device: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2741. return rc;
  2742. }
  2743. return 0;
  2744. }
  2745. /**
  2746. * Handle Hermon Infiniband device port state change
  2747. *
  2748. * @v hermon Hermon device
  2749. * @v port Hermon port
  2750. * @v link_up Link is up
  2751. */
  2752. static void hermon_state_change_ibdev ( struct hermon *hermon __unused,
  2753. struct hermon_port *port,
  2754. int link_up __unused ) {
  2755. struct ib_device *ibdev = port->ibdev;
  2756. /* Update MAD parameters */
  2757. ib_smc_update ( ibdev, hermon_mad );
  2758. }
  2759. /**
  2760. * Unregister Hermon Infiniband device
  2761. *
  2762. * @v hermon Hermon device
  2763. * @v port Hermon port
  2764. */
  2765. static void hermon_unregister_ibdev ( struct hermon *hermon __unused,
  2766. struct hermon_port *port ) {
  2767. struct ib_device *ibdev = port->ibdev;
  2768. unregister_ibdev ( ibdev );
  2769. }
  2770. /** Hermon Infiniband port type */
  2771. static struct hermon_port_type hermon_port_type_ib = {
  2772. .register_dev = hermon_register_ibdev,
  2773. .state_change = hermon_state_change_ibdev,
  2774. .unregister_dev = hermon_unregister_ibdev,
  2775. };
  2776. /***************************************************************************
  2777. *
  2778. * Ethernet operation
  2779. *
  2780. ***************************************************************************
  2781. */
  2782. /** Number of Hermon Ethernet send work queue entries */
  2783. #define HERMON_ETH_NUM_SEND_WQES 2
  2784. /** Number of Hermon Ethernet receive work queue entries */
  2785. #define HERMON_ETH_NUM_RECV_WQES 4
  2786. /** Number of Hermon Ethernet completion entries */
  2787. #define HERMON_ETH_NUM_CQES 8
  2788. /**
  2789. * Transmit packet via Hermon Ethernet device
  2790. *
  2791. * @v netdev Network device
  2792. * @v iobuf I/O buffer
  2793. * @ret rc Return status code
  2794. */
  2795. static int hermon_eth_transmit ( struct net_device *netdev,
  2796. struct io_buffer *iobuf ) {
  2797. struct hermon_port *port = netdev->priv;
  2798. struct ib_device *ibdev = port->ibdev;
  2799. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2800. int rc;
  2801. /* Transmit packet */
  2802. if ( ( rc = ib_post_send ( ibdev, port->eth_qp, NULL,
  2803. iobuf ) ) != 0 ) {
  2804. DBGC ( hermon, "Hermon %p port %d could not transmit: %s\n",
  2805. hermon, ibdev->port, strerror ( rc ) );
  2806. return rc;
  2807. }
  2808. return 0;
  2809. }
  2810. /** Hermon Ethernet queue pair operations */
  2811. static struct ib_queue_pair_operations hermon_eth_qp_op = {
  2812. .alloc_iob = alloc_iob,
  2813. };
  2814. /**
  2815. * Handle Hermon Ethernet device send completion
  2816. *
  2817. * @v ibdev Infiniband device
  2818. * @v qp Queue pair
  2819. * @v iobuf I/O buffer
  2820. * @v rc Completion status code
  2821. */
  2822. static void hermon_eth_complete_send ( struct ib_device *ibdev __unused,
  2823. struct ib_queue_pair *qp,
  2824. struct io_buffer *iobuf, int rc ) {
  2825. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  2826. netdev_tx_complete_err ( netdev, iobuf, rc );
  2827. }
  2828. /**
  2829. * Handle Hermon Ethernet device receive completion
  2830. *
  2831. * @v ibdev Infiniband device
  2832. * @v qp Queue pair
  2833. * @v av Address vector, or NULL
  2834. * @v iobuf I/O buffer
  2835. * @v rc Completion status code
  2836. */
  2837. static void hermon_eth_complete_recv ( struct ib_device *ibdev __unused,
  2838. struct ib_queue_pair *qp,
  2839. struct ib_address_vector *av,
  2840. struct io_buffer *iobuf, int rc ) {
  2841. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  2842. struct net_device *vlan;
  2843. /* Find VLAN device, if applicable */
  2844. if ( av->vlan_present ) {
  2845. if ( ( vlan = vlan_find ( netdev, av->vlan ) ) != NULL ) {
  2846. netdev = vlan;
  2847. } else if ( rc == 0 ) {
  2848. rc = -ENODEV;
  2849. }
  2850. }
  2851. /* Hand off to network layer */
  2852. if ( rc == 0 ) {
  2853. netdev_rx ( netdev, iobuf );
  2854. } else {
  2855. netdev_rx_err ( netdev, iobuf, rc );
  2856. }
  2857. }
  2858. /** Hermon Ethernet device completion operations */
  2859. static struct ib_completion_queue_operations hermon_eth_cq_op = {
  2860. .complete_send = hermon_eth_complete_send,
  2861. .complete_recv = hermon_eth_complete_recv,
  2862. };
  2863. /**
  2864. * Poll Hermon Ethernet device
  2865. *
  2866. * @v netdev Network device
  2867. */
  2868. static void hermon_eth_poll ( struct net_device *netdev ) {
  2869. struct hermon_port *port = netdev->priv;
  2870. struct ib_device *ibdev = port->ibdev;
  2871. ib_poll_eq ( ibdev );
  2872. }
  2873. /**
  2874. * Open Hermon Ethernet device
  2875. *
  2876. * @v netdev Network device
  2877. * @ret rc Return status code
  2878. */
  2879. static int hermon_eth_open ( struct net_device *netdev ) {
  2880. struct hermon_port *port = netdev->priv;
  2881. struct ib_device *ibdev = port->ibdev;
  2882. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2883. union hermonprm_set_port set_port;
  2884. int rc;
  2885. /* Open hardware */
  2886. if ( ( rc = hermon_open ( hermon ) ) != 0 )
  2887. goto err_open;
  2888. /* Allocate completion queue */
  2889. port->eth_cq = ib_create_cq ( ibdev, HERMON_ETH_NUM_CQES,
  2890. &hermon_eth_cq_op );
  2891. if ( ! port->eth_cq ) {
  2892. DBGC ( hermon, "Hermon %p port %d could not create completion "
  2893. "queue\n", hermon, ibdev->port );
  2894. rc = -ENOMEM;
  2895. goto err_create_cq;
  2896. }
  2897. /* Allocate queue pair */
  2898. port->eth_qp = ib_create_qp ( ibdev, IB_QPT_ETH,
  2899. HERMON_ETH_NUM_SEND_WQES, port->eth_cq,
  2900. HERMON_ETH_NUM_RECV_WQES, port->eth_cq,
  2901. &hermon_eth_qp_op );
  2902. if ( ! port->eth_qp ) {
  2903. DBGC ( hermon, "Hermon %p port %d could not create queue "
  2904. "pair\n", hermon, ibdev->port );
  2905. rc = -ENOMEM;
  2906. goto err_create_qp;
  2907. }
  2908. ib_qp_set_ownerdata ( port->eth_qp, netdev );
  2909. /* Activate queue pair */
  2910. if ( ( rc = ib_modify_qp ( ibdev, port->eth_qp ) ) != 0 ) {
  2911. DBGC ( hermon, "Hermon %p port %d could not modify queue "
  2912. "pair: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2913. goto err_modify_qp;
  2914. }
  2915. /* Fill receive rings */
  2916. ib_refill_recv ( ibdev, port->eth_qp );
  2917. /* Set port general parameters */
  2918. memset ( &set_port, 0, sizeof ( set_port ) );
  2919. MLX_FILL_3 ( &set_port.general, 0,
  2920. v_mtu, 1,
  2921. v_pprx, 1,
  2922. v_pptx, 1 );
  2923. MLX_FILL_1 ( &set_port.general, 1,
  2924. mtu, ( ETH_FRAME_LEN + 40 /* Used by card */ ) );
  2925. MLX_FILL_1 ( &set_port.general, 2,
  2926. pfctx, ( 1 << FCOE_VLAN_PRIORITY ) );
  2927. MLX_FILL_1 ( &set_port.general, 3,
  2928. pfcrx, ( 1 << FCOE_VLAN_PRIORITY ) );
  2929. if ( ( rc = hermon_cmd_set_port ( hermon, 1,
  2930. ( HERMON_SET_PORT_GENERAL_PARAM |
  2931. ibdev->port ),
  2932. &set_port ) ) != 0 ) {
  2933. DBGC ( hermon, "Hermon %p port %d could not set port general "
  2934. "parameters: %s\n",
  2935. hermon, ibdev->port, strerror ( rc ) );
  2936. goto err_set_port_general_params;
  2937. }
  2938. /* Set port receive QP */
  2939. memset ( &set_port, 0, sizeof ( set_port ) );
  2940. MLX_FILL_1 ( &set_port.rqp_calc, 0, base_qpn, port->eth_qp->qpn );
  2941. MLX_FILL_1 ( &set_port.rqp_calc, 2,
  2942. mac_miss_index, 128 /* MAC misses go to promisc QP */ );
  2943. MLX_FILL_2 ( &set_port.rqp_calc, 3,
  2944. vlan_miss_index, 127 /* VLAN misses go to promisc QP */,
  2945. no_vlan_index, 126 /* VLAN-free go to promisc QP */ );
  2946. MLX_FILL_2 ( &set_port.rqp_calc, 5,
  2947. promisc_qpn, port->eth_qp->qpn,
  2948. en_uc_promisc, 1 );
  2949. MLX_FILL_2 ( &set_port.rqp_calc, 6,
  2950. def_mcast_qpn, port->eth_qp->qpn,
  2951. mc_promisc_mode, 2 /* Receive all multicasts */ );
  2952. if ( ( rc = hermon_cmd_set_port ( hermon, 1,
  2953. ( HERMON_SET_PORT_RECEIVE_QP |
  2954. ibdev->port ),
  2955. &set_port ) ) != 0 ) {
  2956. DBGC ( hermon, "Hermon %p port %d could not set port receive "
  2957. "QP: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2958. goto err_set_port_receive_qp;
  2959. }
  2960. /* Initialise port */
  2961. if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port ) ) != 0 ) {
  2962. DBGC ( hermon, "Hermon %p port %d could not initialise port: "
  2963. "%s\n", hermon, ibdev->port, strerror ( rc ) );
  2964. goto err_init_port;
  2965. }
  2966. return 0;
  2967. err_init_port:
  2968. err_set_port_receive_qp:
  2969. err_set_port_general_params:
  2970. err_modify_qp:
  2971. ib_destroy_qp ( ibdev, port->eth_qp );
  2972. err_create_qp:
  2973. ib_destroy_cq ( ibdev, port->eth_cq );
  2974. err_create_cq:
  2975. hermon_close ( hermon );
  2976. err_open:
  2977. return rc;
  2978. }
  2979. /**
  2980. * Close Hermon Ethernet device
  2981. *
  2982. * @v netdev Network device
  2983. */
  2984. static void hermon_eth_close ( struct net_device *netdev ) {
  2985. struct hermon_port *port = netdev->priv;
  2986. struct ib_device *ibdev = port->ibdev;
  2987. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2988. int rc;
  2989. /* Close port */
  2990. if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
  2991. DBGC ( hermon, "Hermon %p port %d could not close port: %s\n",
  2992. hermon, ibdev->port, strerror ( rc ) );
  2993. /* Nothing we can do about this */
  2994. }
  2995. /* Tear down the queues */
  2996. ib_destroy_qp ( ibdev, port->eth_qp );
  2997. ib_destroy_cq ( ibdev, port->eth_cq );
  2998. /* Close hardware */
  2999. hermon_close ( hermon );
  3000. }
  3001. /** Hermon Ethernet network device operations */
  3002. static struct net_device_operations hermon_eth_operations = {
  3003. .open = hermon_eth_open,
  3004. .close = hermon_eth_close,
  3005. .transmit = hermon_eth_transmit,
  3006. .poll = hermon_eth_poll,
  3007. };
  3008. /**
  3009. * Register Hermon Ethernet device
  3010. *
  3011. * @v hermon Hermon device
  3012. * @v port Hermon port
  3013. * @ret rc Return status code
  3014. */
  3015. static int hermon_register_netdev ( struct hermon *hermon,
  3016. struct hermon_port *port ) {
  3017. struct net_device *netdev = port->netdev;
  3018. struct ib_device *ibdev = port->ibdev;
  3019. struct hermonprm_query_port_cap query_port;
  3020. union {
  3021. uint8_t bytes[8];
  3022. uint32_t dwords[2];
  3023. } mac;
  3024. int rc;
  3025. /* Retrieve MAC address */
  3026. if ( ( rc = hermon_cmd_query_port ( hermon, ibdev->port,
  3027. &query_port ) ) != 0 ) {
  3028. DBGC ( hermon, "Hermon %p port %d could not query port: %s\n",
  3029. hermon, ibdev->port, strerror ( rc ) );
  3030. return rc;
  3031. }
  3032. mac.dwords[0] = htonl ( MLX_GET ( &query_port, mac_47_32 ) );
  3033. mac.dwords[1] = htonl ( MLX_GET ( &query_port, mac_31_0 ) );
  3034. memcpy ( netdev->hw_addr,
  3035. &mac.bytes[ sizeof ( mac.bytes ) - ETH_ALEN ], ETH_ALEN );
  3036. /* Register network device */
  3037. if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
  3038. DBGC ( hermon, "Hermon %p port %d could not register network "
  3039. "device: %s\n", hermon, ibdev->port, strerror ( rc ) );
  3040. return rc;
  3041. }
  3042. return 0;
  3043. }
  3044. /**
  3045. * Handle Hermon Ethernet device port state change
  3046. *
  3047. * @v hermon Hermon device
  3048. * @v port Hermon port
  3049. * @v link_up Link is up
  3050. */
  3051. static void hermon_state_change_netdev ( struct hermon *hermon __unused,
  3052. struct hermon_port *port,
  3053. int link_up ) {
  3054. struct net_device *netdev = port->netdev;
  3055. if ( link_up ) {
  3056. netdev_link_up ( netdev );
  3057. } else {
  3058. netdev_link_down ( netdev );
  3059. }
  3060. }
  3061. /**
  3062. * Unregister Hermon Ethernet device
  3063. *
  3064. * @v hermon Hermon device
  3065. * @v port Hermon port
  3066. */
  3067. static void hermon_unregister_netdev ( struct hermon *hermon __unused,
  3068. struct hermon_port *port ) {
  3069. struct net_device *netdev = port->netdev;
  3070. unregister_netdev ( netdev );
  3071. }
  3072. /** Hermon Ethernet port type */
  3073. static struct hermon_port_type hermon_port_type_eth = {
  3074. .register_dev = hermon_register_netdev,
  3075. .state_change = hermon_state_change_netdev,
  3076. .unregister_dev = hermon_unregister_netdev,
  3077. };
  3078. /***************************************************************************
  3079. *
  3080. * Port type detection
  3081. *
  3082. ***************************************************************************
  3083. */
  3084. /** Timeout for port sensing */
  3085. #define HERMON_SENSE_PORT_TIMEOUT ( TICKS_PER_SEC / 2 )
  3086. /**
  3087. * Name port type
  3088. *
  3089. * @v port_type Port type
  3090. * @v port_type_name Port type name
  3091. */
  3092. static inline const char * hermon_name_port_type ( unsigned int port_type ) {
  3093. switch ( port_type ) {
  3094. case HERMON_PORT_TYPE_UNKNOWN: return "unknown";
  3095. case HERMON_PORT_TYPE_IB: return "Infiniband";
  3096. case HERMON_PORT_TYPE_ETH: return "Ethernet";
  3097. default: return "INVALID";
  3098. }
  3099. }
  3100. /**
  3101. * Sense port type
  3102. *
  3103. * @v hermon Hermon device
  3104. * @v port Hermon port
  3105. * @ret port_type Port type, or negative error
  3106. */
  3107. static int hermon_sense_port_type ( struct hermon *hermon,
  3108. struct hermon_port *port ) {
  3109. struct ib_device *ibdev = port->ibdev;
  3110. struct hermonprm_sense_port sense_port;
  3111. int port_type;
  3112. int rc;
  3113. /* If DPDP is not supported, always assume Infiniband */
  3114. if ( ! hermon->cap.dpdp ) {
  3115. port_type = HERMON_PORT_TYPE_IB;
  3116. DBGC ( hermon, "Hermon %p port %d does not support DPDP; "
  3117. "assuming an %s network\n", hermon, ibdev->port,
  3118. hermon_name_port_type ( port_type ) );
  3119. return port_type;
  3120. }
  3121. /* Sense the port type */
  3122. if ( ( rc = hermon_cmd_sense_port ( hermon, ibdev->port,
  3123. &sense_port ) ) != 0 ) {
  3124. DBGC ( hermon, "Hermon %p port %d sense failed: %s\n",
  3125. hermon, ibdev->port, strerror ( rc ) );
  3126. return rc;
  3127. }
  3128. port_type = MLX_GET ( &sense_port, port_type );
  3129. DBGC ( hermon, "Hermon %p port %d sensed an %s network\n",
  3130. hermon, ibdev->port, hermon_name_port_type ( port_type ) );
  3131. return port_type;
  3132. }
  3133. /**
  3134. * Set port type
  3135. *
  3136. * @v hermon Hermon device
  3137. * @v port Hermon port
  3138. * @ret rc Return status code
  3139. */
  3140. static int hermon_set_port_type ( struct hermon *hermon,
  3141. struct hermon_port *port ) {
  3142. struct ib_device *ibdev = port->ibdev;
  3143. struct hermonprm_query_port_cap query_port;
  3144. int ib_supported;
  3145. int eth_supported;
  3146. int port_type;
  3147. unsigned long start;
  3148. unsigned long elapsed;
  3149. int rc;
  3150. /* Check to see which types are supported */
  3151. if ( ( rc = hermon_cmd_query_port ( hermon, ibdev->port,
  3152. &query_port ) ) != 0 ) {
  3153. DBGC ( hermon, "Hermon %p port %d could not query port: %s\n",
  3154. hermon, ibdev->port, strerror ( rc ) );
  3155. return rc;
  3156. }
  3157. ib_supported = MLX_GET ( &query_port, ib );
  3158. eth_supported = MLX_GET ( &query_port, eth );
  3159. DBGC ( hermon, "Hermon %p port %d supports%s%s%s\n",
  3160. hermon, ibdev->port, ( ib_supported ? " Infiniband" : "" ),
  3161. ( ( ib_supported && eth_supported ) ? " and" : "" ),
  3162. ( eth_supported ? " Ethernet" : "" ) );
  3163. /* Sense network, if applicable */
  3164. if ( ib_supported && eth_supported ) {
  3165. /* Both types are supported; try sensing network */
  3166. start = currticks();
  3167. do {
  3168. /* Try sensing port */
  3169. port_type = hermon_sense_port_type ( hermon, port );
  3170. if ( port_type < 0 ) {
  3171. rc = port_type;
  3172. return rc;
  3173. }
  3174. } while ( ( port_type == HERMON_PORT_TYPE_UNKNOWN ) &&
  3175. ( ( elapsed = ( currticks() - start ) ) <
  3176. HERMON_SENSE_PORT_TIMEOUT ) );
  3177. /* Set port type based on sensed network, defaulting
  3178. * to Infiniband if nothing was sensed.
  3179. */
  3180. switch ( port_type ) {
  3181. case HERMON_PORT_TYPE_ETH:
  3182. port->type = &hermon_port_type_eth;
  3183. break;
  3184. case HERMON_PORT_TYPE_IB:
  3185. case HERMON_PORT_TYPE_UNKNOWN:
  3186. port->type = &hermon_port_type_ib;
  3187. break;
  3188. default:
  3189. return -EINVAL;
  3190. }
  3191. } else if ( eth_supported ) {
  3192. port->type = &hermon_port_type_eth;
  3193. } else {
  3194. port->type = &hermon_port_type_ib;
  3195. }
  3196. assert ( port->type != NULL );
  3197. return 0;
  3198. }
  3199. /***************************************************************************
  3200. *
  3201. * BOFM interface
  3202. *
  3203. ***************************************************************************
  3204. */
  3205. /**
  3206. * Harvest Ethernet MAC for BOFM
  3207. *
  3208. * @v bofm BOFM device
  3209. * @v mport Multi-port index
  3210. * @v mac MAC to fill in
  3211. * @ret rc Return status code
  3212. */
  3213. static int hermon_bofm_harvest ( struct bofm_device *bofm, unsigned int mport,
  3214. uint8_t *mac ) {
  3215. struct hermon *hermon = container_of ( bofm, struct hermon, bofm );
  3216. struct hermonprm_mod_stat_cfg stat_cfg;
  3217. union {
  3218. uint8_t bytes[8];
  3219. uint32_t dwords[2];
  3220. } buf;
  3221. int rc;
  3222. /* Query static configuration */
  3223. if ( ( rc = hermon_mod_stat_cfg ( hermon, mport,
  3224. HERMON_MOD_STAT_CFG_QUERY,
  3225. HERMON_MOD_STAT_CFG_OFFSET ( mac_m ),
  3226. &stat_cfg ) ) != 0 ) {
  3227. DBGC ( hermon, "Hermon %p port %d could not query "
  3228. "configuration: %s\n", hermon, mport, strerror ( rc ) );
  3229. return rc;
  3230. }
  3231. /* Retrieve MAC address */
  3232. buf.dwords[0] = htonl ( MLX_GET ( &stat_cfg, mac_high ) );
  3233. buf.dwords[1] = htonl ( MLX_GET ( &stat_cfg, mac_low ) );
  3234. memcpy ( mac, &buf.bytes[ sizeof ( buf.bytes ) - ETH_ALEN ],
  3235. ETH_ALEN );
  3236. DBGC ( hermon, "Hermon %p port %d harvested MAC address %s\n",
  3237. hermon, mport, eth_ntoa ( mac ) );
  3238. return 0;
  3239. }
  3240. /**
  3241. * Update Ethernet MAC for BOFM
  3242. *
  3243. * @v bofm BOFM device
  3244. * @v mport Multi-port index
  3245. * @v mac MAC to fill in
  3246. * @ret rc Return status code
  3247. */
  3248. static int hermon_bofm_update ( struct bofm_device *bofm, unsigned int mport,
  3249. const uint8_t *mac ) {
  3250. struct hermon *hermon = container_of ( bofm, struct hermon, bofm );
  3251. struct hermonprm_mod_stat_cfg stat_cfg;
  3252. union {
  3253. uint8_t bytes[8];
  3254. uint32_t dwords[2];
  3255. } buf;
  3256. int rc;
  3257. /* Prepare MAC address */
  3258. memset ( &buf, 0, sizeof ( buf ) );
  3259. memcpy ( &buf.bytes[ sizeof ( buf.bytes ) - ETH_ALEN ], mac,
  3260. ETH_ALEN );
  3261. /* Modify static configuration */
  3262. memset ( &stat_cfg, 0, sizeof ( stat_cfg ) );
  3263. MLX_FILL_2 ( &stat_cfg, 36,
  3264. mac_m, 1,
  3265. mac_high, ntohl ( buf.dwords[0] ) );
  3266. MLX_FILL_1 ( &stat_cfg, 37, mac_low, ntohl ( buf.dwords[1] ) );
  3267. if ( ( rc = hermon_mod_stat_cfg ( hermon, mport,
  3268. HERMON_MOD_STAT_CFG_SET,
  3269. HERMON_MOD_STAT_CFG_OFFSET ( mac_m ),
  3270. &stat_cfg ) ) != 0 ) {
  3271. DBGC ( hermon, "Hermon %p port %d could not modify "
  3272. "configuration: %s\n", hermon, mport, strerror ( rc ) );
  3273. return rc;
  3274. }
  3275. DBGC ( hermon, "Hermon %p port %d updated MAC address to %s\n",
  3276. hermon, mport, eth_ntoa ( mac ) );
  3277. return 0;
  3278. }
  3279. /** Hermon BOFM operations */
  3280. static struct bofm_operations hermon_bofm_operations = {
  3281. .harvest = hermon_bofm_harvest,
  3282. .update = hermon_bofm_update,
  3283. };
  3284. /***************************************************************************
  3285. *
  3286. * PCI interface
  3287. *
  3288. ***************************************************************************
  3289. */
  3290. /**
  3291. * Allocate Hermon device
  3292. *
  3293. * @v pci PCI device
  3294. * @v id PCI ID
  3295. * @ret rc Return status code
  3296. */
  3297. static struct hermon * hermon_alloc ( void ) {
  3298. struct hermon *hermon;
  3299. /* Allocate Hermon device */
  3300. hermon = zalloc ( sizeof ( *hermon ) );
  3301. if ( ! hermon )
  3302. goto err_hermon;
  3303. /* Allocate space for mailboxes */
  3304. hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
  3305. HERMON_MBOX_ALIGN );
  3306. if ( ! hermon->mailbox_in )
  3307. goto err_mailbox_in;
  3308. hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
  3309. HERMON_MBOX_ALIGN );
  3310. if ( ! hermon->mailbox_out )
  3311. goto err_mailbox_out;
  3312. return hermon;
  3313. free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
  3314. err_mailbox_out:
  3315. free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
  3316. err_mailbox_in:
  3317. free ( hermon );
  3318. err_hermon:
  3319. return NULL;
  3320. }
  3321. /**
  3322. * Free Hermon device
  3323. *
  3324. * @v hermon Hermon device
  3325. */
  3326. static void hermon_free ( struct hermon *hermon ) {
  3327. ufree ( hermon->icm );
  3328. ufree ( hermon->firmware_area );
  3329. free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
  3330. free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
  3331. free ( hermon );
  3332. }
  3333. /**
  3334. * Initialise Hermon PCI parameters
  3335. *
  3336. * @v hermon Hermon device
  3337. */
  3338. static void hermon_pci_init ( struct hermon *hermon ) {
  3339. struct pci_device *pci = hermon->pci;
  3340. /* Fix up PCI device */
  3341. adjust_pci_device ( pci );
  3342. /* Get PCI BARs */
  3343. hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR),
  3344. HERMON_PCI_CONFIG_BAR_SIZE );
  3345. hermon->uar = ioremap ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ),
  3346. HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE );
  3347. }
  3348. /**
  3349. * Probe PCI device
  3350. *
  3351. * @v pci PCI device
  3352. * @v id PCI ID
  3353. * @ret rc Return status code
  3354. */
  3355. static int hermon_probe ( struct pci_device *pci ) {
  3356. struct hermon *hermon;
  3357. struct ib_device *ibdev;
  3358. struct net_device *netdev;
  3359. struct hermon_port *port;
  3360. unsigned int i;
  3361. int rc;
  3362. /* Allocate Hermon device */
  3363. hermon = hermon_alloc();
  3364. if ( ! hermon ) {
  3365. rc = -ENOMEM;
  3366. goto err_alloc;
  3367. }
  3368. pci_set_drvdata ( pci, hermon );
  3369. hermon->pci = pci;
  3370. /* Initialise PCI parameters */
  3371. hermon_pci_init ( hermon );
  3372. /* Reset device */
  3373. hermon_reset ( hermon );
  3374. /* Start firmware */
  3375. if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
  3376. goto err_start_firmware;
  3377. /* Get device limits */
  3378. if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
  3379. goto err_get_cap;
  3380. /* Allocate Infiniband devices */
  3381. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3382. ibdev = alloc_ibdev ( 0 );
  3383. if ( ! ibdev ) {
  3384. rc = -ENOMEM;
  3385. goto err_alloc_ibdev;
  3386. }
  3387. hermon->port[i].ibdev = ibdev;
  3388. ibdev->op = &hermon_ib_operations;
  3389. ibdev->dev = &pci->dev;
  3390. ibdev->port = ( HERMON_PORT_BASE + i );
  3391. ib_set_drvdata ( ibdev, hermon );
  3392. }
  3393. /* Allocate network devices */
  3394. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3395. netdev = alloc_etherdev ( 0 );
  3396. if ( ! netdev ) {
  3397. rc = -ENOMEM;
  3398. goto err_alloc_netdev;
  3399. }
  3400. hermon->port[i].netdev = netdev;
  3401. netdev_init ( netdev, &hermon_eth_operations );
  3402. netdev->dev = &pci->dev;
  3403. netdev->priv = &hermon->port[i];
  3404. }
  3405. /* Start device */
  3406. if ( ( rc = hermon_start ( hermon, 1 ) ) != 0 )
  3407. goto err_start;
  3408. /* Determine port types */
  3409. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3410. port = &hermon->port[i];
  3411. if ( ( rc = hermon_set_port_type ( hermon, port ) ) != 0 )
  3412. goto err_set_port_type;
  3413. }
  3414. /* Register devices */
  3415. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3416. port = &hermon->port[i];
  3417. if ( ( rc = port->type->register_dev ( hermon, port ) ) != 0 )
  3418. goto err_register;
  3419. }
  3420. /* Leave device quiescent until opened */
  3421. if ( hermon->open_count == 0 )
  3422. hermon_stop ( hermon );
  3423. return 0;
  3424. i = hermon->cap.num_ports;
  3425. err_register:
  3426. for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
  3427. port = &hermon->port[i];
  3428. port->type->unregister_dev ( hermon, port );
  3429. }
  3430. err_set_port_type:
  3431. hermon_stop ( hermon );
  3432. err_start:
  3433. i = hermon->cap.num_ports;
  3434. err_alloc_netdev:
  3435. for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
  3436. netdev_nullify ( hermon->port[i].netdev );
  3437. netdev_put ( hermon->port[i].netdev );
  3438. }
  3439. i = hermon->cap.num_ports;
  3440. err_alloc_ibdev:
  3441. for ( i-- ; ( signed int ) i >= 0 ; i-- )
  3442. ibdev_put ( hermon->port[i].ibdev );
  3443. err_get_cap:
  3444. hermon_stop_firmware ( hermon );
  3445. err_start_firmware:
  3446. hermon_free ( hermon );
  3447. err_alloc:
  3448. return rc;
  3449. }
  3450. /**
  3451. * Remove PCI device
  3452. *
  3453. * @v pci PCI device
  3454. */
  3455. static void hermon_remove ( struct pci_device *pci ) {
  3456. struct hermon *hermon = pci_get_drvdata ( pci );
  3457. struct hermon_port *port;
  3458. int i;
  3459. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- ) {
  3460. port = &hermon->port[i];
  3461. port->type->unregister_dev ( hermon, port );
  3462. }
  3463. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- ) {
  3464. netdev_nullify ( hermon->port[i].netdev );
  3465. netdev_put ( hermon->port[i].netdev );
  3466. }
  3467. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- )
  3468. ibdev_put ( hermon->port[i].ibdev );
  3469. hermon_free ( hermon );
  3470. }
  3471. /**
  3472. * Probe PCI device for BOFM
  3473. *
  3474. * @v pci PCI device
  3475. * @v id PCI ID
  3476. * @ret rc Return status code
  3477. */
  3478. static int hermon_bofm_probe ( struct pci_device *pci ) {
  3479. struct hermon *hermon;
  3480. int rc;
  3481. /* Allocate Hermon device */
  3482. hermon = hermon_alloc();
  3483. if ( ! hermon ) {
  3484. rc = -ENOMEM;
  3485. goto err_alloc;
  3486. }
  3487. pci_set_drvdata ( pci, hermon );
  3488. hermon->pci = pci;
  3489. /* Initialise PCI parameters */
  3490. hermon_pci_init ( hermon );
  3491. /* Initialise BOFM device */
  3492. bofm_init ( &hermon->bofm, pci, &hermon_bofm_operations );
  3493. /* Register BOFM device */
  3494. if ( ( rc = bofm_register ( &hermon->bofm ) ) != 0 ) {
  3495. DBGC ( hermon, "Hermon %p could not register BOFM device: "
  3496. "%s\n", hermon, strerror ( rc ) );
  3497. goto err_bofm_register;
  3498. }
  3499. return 0;
  3500. err_bofm_register:
  3501. hermon_free ( hermon );
  3502. err_alloc:
  3503. return rc;
  3504. }
  3505. /**
  3506. * Remove PCI device for BOFM
  3507. *
  3508. * @v pci PCI device
  3509. */
  3510. static void hermon_bofm_remove ( struct pci_device *pci ) {
  3511. struct hermon *hermon = pci_get_drvdata ( pci );
  3512. bofm_unregister ( &hermon->bofm );
  3513. hermon_free ( hermon );
  3514. }
  3515. static struct pci_device_id hermon_nics[] = {
  3516. PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver", 0 ),
  3517. PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver", 0 ),
  3518. PCI_ROM ( 0x15b3, 0x6732, "mt26418", "MT26418 HCA driver", 0 ),
  3519. PCI_ROM ( 0x15b3, 0x673c, "mt26428", "MT26428 HCA driver", 0 ),
  3520. PCI_ROM ( 0x15b3, 0x6746, "mt26438", "MT26438 HCA driver", 0 ),
  3521. PCI_ROM ( 0x15b3, 0x6778, "mt26488", "MT26488 HCA driver", 0 ),
  3522. PCI_ROM ( 0x15b3, 0x6368, "mt25448", "MT25448 HCA driver", 0 ),
  3523. PCI_ROM ( 0x15b3, 0x6750, "mt26448", "MT26448 HCA driver", 0 ),
  3524. PCI_ROM ( 0x15b3, 0x6372, "mt25458", "MT25458 HCA driver", 0 ),
  3525. PCI_ROM ( 0x15b3, 0x675a, "mt26458", "MT26458 HCA driver", 0 ),
  3526. PCI_ROM ( 0x15b3, 0x6764, "mt26468", "MT26468 HCA driver", 0 ),
  3527. PCI_ROM ( 0x15b3, 0x676e, "mt26478", "MT26478 HCA driver", 0 ),
  3528. };
  3529. struct pci_driver hermon_driver __pci_driver = {
  3530. .ids = hermon_nics,
  3531. .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
  3532. .probe = hermon_probe,
  3533. .remove = hermon_remove,
  3534. };
  3535. struct pci_driver hermon_bofm_driver __bofm_driver = {
  3536. .ids = hermon_nics,
  3537. .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
  3538. .probe = hermon_bofm_probe,
  3539. .remove = hermon_bofm_remove,
  3540. };