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 79KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <strings.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <byteswap.h>
  28. #include <gpxe/io.h>
  29. #include <gpxe/pci.h>
  30. #include <gpxe/malloc.h>
  31. #include <gpxe/umalloc.h>
  32. #include <gpxe/iobuf.h>
  33. #include <gpxe/netdevice.h>
  34. #include <gpxe/infiniband.h>
  35. #include <gpxe/ib_smc.h>
  36. #include "hermon.h"
  37. /**
  38. * @file
  39. *
  40. * Mellanox Hermon Infiniband HCA
  41. *
  42. */
  43. /***************************************************************************
  44. *
  45. * Queue number allocation
  46. *
  47. ***************************************************************************
  48. */
  49. /**
  50. * Allocate offsets within usage bitmask
  51. *
  52. * @v bits Usage bitmask
  53. * @v bits_len Length of usage bitmask
  54. * @v num_bits Number of contiguous bits to allocate within bitmask
  55. * @ret bit First free bit within bitmask, or negative error
  56. */
  57. static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
  58. unsigned int bits_len,
  59. unsigned int num_bits ) {
  60. unsigned int bit = 0;
  61. hermon_bitmask_t mask = 1;
  62. unsigned int found = 0;
  63. /* Search bits for num_bits contiguous free bits */
  64. while ( bit < bits_len ) {
  65. if ( ( mask & *bits ) == 0 ) {
  66. if ( ++found == num_bits )
  67. goto found;
  68. } else {
  69. found = 0;
  70. }
  71. bit++;
  72. mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
  73. if ( mask == 1 )
  74. bits++;
  75. }
  76. return -ENFILE;
  77. found:
  78. /* Mark bits as in-use */
  79. do {
  80. *bits |= mask;
  81. if ( mask == 1 )
  82. bits--;
  83. mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
  84. } while ( --found );
  85. return ( bit - num_bits + 1 );
  86. }
  87. /**
  88. * Free offsets within usage bitmask
  89. *
  90. * @v bits Usage bitmask
  91. * @v bit Starting bit within bitmask
  92. * @v num_bits Number of contiguous bits to free within bitmask
  93. */
  94. static void hermon_bitmask_free ( hermon_bitmask_t *bits,
  95. int bit, unsigned int num_bits ) {
  96. hermon_bitmask_t mask;
  97. for ( ; num_bits ; bit++, num_bits-- ) {
  98. mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
  99. bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
  100. }
  101. }
  102. /***************************************************************************
  103. *
  104. * HCA commands
  105. *
  106. ***************************************************************************
  107. */
  108. /**
  109. * Wait for Hermon command completion
  110. *
  111. * @v hermon Hermon device
  112. * @v hcr HCA command registers
  113. * @ret rc Return status code
  114. */
  115. static int hermon_cmd_wait ( struct hermon *hermon,
  116. struct hermonprm_hca_command_register *hcr ) {
  117. unsigned int wait;
  118. for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
  119. hcr->u.dwords[6] =
  120. readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
  121. if ( ( MLX_GET ( hcr, go ) == 0 ) &&
  122. ( MLX_GET ( hcr, t ) == hermon->toggle ) )
  123. return 0;
  124. mdelay ( 1 );
  125. }
  126. return -EBUSY;
  127. }
  128. /**
  129. * Issue HCA command
  130. *
  131. * @v hermon Hermon device
  132. * @v command Command opcode, flags and input/output lengths
  133. * @v op_mod Opcode modifier (0 if no modifier applicable)
  134. * @v in Input parameters
  135. * @v in_mod Input modifier (0 if no modifier applicable)
  136. * @v out Output parameters
  137. * @ret rc Return status code
  138. */
  139. static int hermon_cmd ( struct hermon *hermon, unsigned long command,
  140. unsigned int op_mod, const void *in,
  141. unsigned int in_mod, void *out ) {
  142. struct hermonprm_hca_command_register hcr;
  143. unsigned int opcode = HERMON_HCR_OPCODE ( command );
  144. size_t in_len = HERMON_HCR_IN_LEN ( command );
  145. size_t out_len = HERMON_HCR_OUT_LEN ( command );
  146. void *in_buffer;
  147. void *out_buffer;
  148. unsigned int status;
  149. unsigned int i;
  150. int rc;
  151. assert ( in_len <= HERMON_MBOX_SIZE );
  152. assert ( out_len <= HERMON_MBOX_SIZE );
  153. DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
  154. hermon, opcode, in_len,
  155. ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
  156. ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
  157. /* Check that HCR is free */
  158. if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
  159. DBGC ( hermon, "Hermon %p command interface locked\n",
  160. hermon );
  161. return rc;
  162. }
  163. /* Flip HCR toggle */
  164. hermon->toggle = ( 1 - hermon->toggle );
  165. /* Prepare HCR */
  166. memset ( &hcr, 0, sizeof ( hcr ) );
  167. in_buffer = &hcr.u.dwords[0];
  168. if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
  169. in_buffer = hermon->mailbox_in;
  170. MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
  171. }
  172. memcpy ( in_buffer, in, in_len );
  173. MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
  174. out_buffer = &hcr.u.dwords[3];
  175. if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
  176. out_buffer = hermon->mailbox_out;
  177. MLX_FILL_1 ( &hcr, 4, out_param_l,
  178. virt_to_bus ( out_buffer ) );
  179. }
  180. MLX_FILL_4 ( &hcr, 6,
  181. opcode, opcode,
  182. opcode_modifier, op_mod,
  183. go, 1,
  184. t, hermon->toggle );
  185. DBGC ( hermon, "Hermon %p issuing command %04x\n",
  186. hermon, opcode );
  187. DBGC2_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  188. &hcr, sizeof ( hcr ) );
  189. if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
  190. DBGC2 ( hermon, "Input mailbox:\n" );
  191. DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
  192. ( ( in_len < 512 ) ? in_len : 512 ) );
  193. }
  194. /* Issue command */
  195. for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
  196. i++ ) {
  197. writel ( hcr.u.dwords[i],
  198. hermon->config + HERMON_HCR_REG ( i ) );
  199. barrier();
  200. }
  201. /* Wait for command completion */
  202. if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
  203. DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
  204. hermon );
  205. DBGC_HDA ( hermon,
  206. virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  207. &hcr, sizeof ( hcr ) );
  208. return rc;
  209. }
  210. /* Check command status */
  211. status = MLX_GET ( &hcr, status );
  212. if ( status != 0 ) {
  213. DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
  214. hermon, status );
  215. DBGC_HDA ( hermon,
  216. virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  217. &hcr, sizeof ( hcr ) );
  218. return -EIO;
  219. }
  220. /* Read output parameters, if any */
  221. hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
  222. hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
  223. memcpy ( out, out_buffer, out_len );
  224. if ( out_len ) {
  225. DBGC2 ( hermon, "Output%s:\n",
  226. ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
  227. DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
  228. ( ( out_len < 512 ) ? out_len : 512 ) );
  229. }
  230. return 0;
  231. }
  232. static inline int
  233. hermon_cmd_query_dev_cap ( struct hermon *hermon,
  234. struct hermonprm_query_dev_cap *dev_cap ) {
  235. return hermon_cmd ( hermon,
  236. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
  237. 1, sizeof ( *dev_cap ) ),
  238. 0, NULL, 0, dev_cap );
  239. }
  240. static inline int
  241. hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
  242. return hermon_cmd ( hermon,
  243. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
  244. 1, sizeof ( *fw ) ),
  245. 0, NULL, 0, fw );
  246. }
  247. static inline int
  248. hermon_cmd_init_hca ( struct hermon *hermon,
  249. const struct hermonprm_init_hca *init_hca ) {
  250. return hermon_cmd ( hermon,
  251. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
  252. 1, sizeof ( *init_hca ) ),
  253. 0, init_hca, 0, NULL );
  254. }
  255. static inline int
  256. hermon_cmd_close_hca ( struct hermon *hermon ) {
  257. return hermon_cmd ( hermon,
  258. HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
  259. 0, NULL, 0, NULL );
  260. }
  261. static inline int
  262. hermon_cmd_init_port ( struct hermon *hermon, unsigned int port,
  263. const struct hermonprm_init_port *init_port ) {
  264. return hermon_cmd ( hermon,
  265. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_PORT,
  266. 1, sizeof ( *init_port ) ),
  267. 0, init_port, port, NULL );
  268. }
  269. static inline int
  270. hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
  271. return hermon_cmd ( hermon,
  272. HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
  273. 0, NULL, port, NULL );
  274. }
  275. static inline int
  276. hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
  277. const struct hermonprm_mpt *mpt ) {
  278. return hermon_cmd ( hermon,
  279. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
  280. 1, sizeof ( *mpt ) ),
  281. 0, mpt, index, NULL );
  282. }
  283. static inline int
  284. hermon_cmd_write_mtt ( struct hermon *hermon,
  285. const struct hermonprm_write_mtt *write_mtt ) {
  286. return hermon_cmd ( hermon,
  287. HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
  288. 1, sizeof ( *write_mtt ) ),
  289. 0, write_mtt, 1, NULL );
  290. }
  291. static inline int
  292. hermon_cmd_map_eq ( struct hermon *hermon, unsigned long index_map,
  293. const struct hermonprm_event_mask *mask ) {
  294. return hermon_cmd ( hermon,
  295. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_EQ,
  296. 0, sizeof ( *mask ) ),
  297. 0, mask, index_map, NULL );
  298. }
  299. static inline int
  300. hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
  301. const struct hermonprm_eqc *eqctx ) {
  302. return hermon_cmd ( hermon,
  303. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
  304. 1, sizeof ( *eqctx ) ),
  305. 0, eqctx, index, NULL );
  306. }
  307. static inline int
  308. hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index,
  309. struct hermonprm_eqc *eqctx ) {
  310. return hermon_cmd ( hermon,
  311. HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_EQ,
  312. 1, sizeof ( *eqctx ) ),
  313. 1, NULL, index, eqctx );
  314. }
  315. static inline int
  316. hermon_cmd_query_eq ( struct hermon *hermon, unsigned int index,
  317. struct hermonprm_eqc *eqctx ) {
  318. return hermon_cmd ( hermon,
  319. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_EQ,
  320. 1, sizeof ( *eqctx ) ),
  321. 0, NULL, index, eqctx );
  322. }
  323. static inline int
  324. hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
  325. const struct hermonprm_completion_queue_context *cqctx ){
  326. return hermon_cmd ( hermon,
  327. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
  328. 1, sizeof ( *cqctx ) ),
  329. 0, cqctx, cqn, NULL );
  330. }
  331. static inline int
  332. hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
  333. struct hermonprm_completion_queue_context *cqctx) {
  334. return hermon_cmd ( hermon,
  335. HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
  336. 1, sizeof ( *cqctx ) ),
  337. 0, NULL, cqn, cqctx );
  338. }
  339. static inline int
  340. hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
  341. const struct hermonprm_qp_ee_state_transitions *ctx ){
  342. return hermon_cmd ( hermon,
  343. HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
  344. 1, sizeof ( *ctx ) ),
  345. 0, ctx, qpn, NULL );
  346. }
  347. static inline int
  348. hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
  349. const struct hermonprm_qp_ee_state_transitions *ctx ){
  350. return hermon_cmd ( hermon,
  351. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
  352. 1, sizeof ( *ctx ) ),
  353. 0, ctx, qpn, NULL );
  354. }
  355. static inline int
  356. hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
  357. const struct hermonprm_qp_ee_state_transitions *ctx ) {
  358. return hermon_cmd ( hermon,
  359. HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
  360. 1, sizeof ( *ctx ) ),
  361. 0, ctx, qpn, NULL );
  362. }
  363. static inline int
  364. hermon_cmd_rts2rts_qp ( struct hermon *hermon, unsigned long qpn,
  365. const struct hermonprm_qp_ee_state_transitions *ctx ) {
  366. return hermon_cmd ( hermon,
  367. HERMON_HCR_IN_CMD ( HERMON_HCR_RTS2RTS_QP,
  368. 1, sizeof ( *ctx ) ),
  369. 0, ctx, qpn, NULL );
  370. }
  371. static inline int
  372. hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
  373. return hermon_cmd ( hermon,
  374. HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
  375. 0x03, NULL, qpn, NULL );
  376. }
  377. static inline int
  378. hermon_cmd_query_qp ( struct hermon *hermon, unsigned long qpn,
  379. struct hermonprm_qp_ee_state_transitions *ctx ) {
  380. return hermon_cmd ( hermon,
  381. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_QP,
  382. 1, sizeof ( *ctx ) ),
  383. 0, NULL, qpn, ctx );
  384. }
  385. static inline int
  386. hermon_cmd_conf_special_qp ( struct hermon *hermon, unsigned int internal_qps,
  387. unsigned long base_qpn ) {
  388. return hermon_cmd ( hermon,
  389. HERMON_HCR_VOID_CMD ( HERMON_HCR_CONF_SPECIAL_QP ),
  390. internal_qps, NULL, base_qpn, NULL );
  391. }
  392. static inline int
  393. hermon_cmd_mad_ifc ( struct hermon *hermon, unsigned int port,
  394. union hermonprm_mad *mad ) {
  395. return hermon_cmd ( hermon,
  396. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
  397. 1, sizeof ( *mad ),
  398. 1, sizeof ( *mad ) ),
  399. 0x03, mad, port, mad );
  400. }
  401. static inline int
  402. hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
  403. struct hermonprm_mcg_entry *mcg ) {
  404. return hermon_cmd ( hermon,
  405. HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
  406. 1, sizeof ( *mcg ) ),
  407. 0, NULL, index, mcg );
  408. }
  409. static inline int
  410. hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
  411. const struct hermonprm_mcg_entry *mcg ) {
  412. return hermon_cmd ( hermon,
  413. HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
  414. 1, sizeof ( *mcg ) ),
  415. 0, mcg, index, NULL );
  416. }
  417. static inline int
  418. hermon_cmd_mgid_hash ( struct hermon *hermon, const struct ib_gid *gid,
  419. struct hermonprm_mgm_hash *hash ) {
  420. return hermon_cmd ( hermon,
  421. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
  422. 1, sizeof ( *gid ),
  423. 0, sizeof ( *hash ) ),
  424. 0, gid, 0, hash );
  425. }
  426. static inline int
  427. hermon_cmd_run_fw ( struct hermon *hermon ) {
  428. return hermon_cmd ( hermon,
  429. HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
  430. 0, NULL, 0, NULL );
  431. }
  432. static inline int
  433. hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
  434. const struct hermonprm_scalar_parameter *offset ) {
  435. return hermon_cmd ( hermon,
  436. HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
  437. 0, sizeof ( *offset ) ),
  438. 0, offset, page_count, NULL );
  439. }
  440. static inline int
  441. hermon_cmd_map_icm ( struct hermon *hermon,
  442. const struct hermonprm_virtual_physical_mapping *map ) {
  443. return hermon_cmd ( hermon,
  444. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
  445. 1, sizeof ( *map ) ),
  446. 0, map, 1, NULL );
  447. }
  448. static inline int
  449. hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
  450. return hermon_cmd ( hermon,
  451. HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
  452. 0, NULL, 0, NULL );
  453. }
  454. static inline int
  455. hermon_cmd_map_icm_aux ( struct hermon *hermon,
  456. const struct hermonprm_virtual_physical_mapping *map ) {
  457. return hermon_cmd ( hermon,
  458. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
  459. 1, sizeof ( *map ) ),
  460. 0, map, 1, NULL );
  461. }
  462. static inline int
  463. hermon_cmd_set_icm_size ( struct hermon *hermon,
  464. const struct hermonprm_scalar_parameter *icm_size,
  465. struct hermonprm_scalar_parameter *icm_aux_size ) {
  466. return hermon_cmd ( hermon,
  467. HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
  468. 0, sizeof ( *icm_size ),
  469. 0, sizeof (*icm_aux_size) ),
  470. 0, icm_size, 0, icm_aux_size );
  471. }
  472. static inline int
  473. hermon_cmd_unmap_fa ( struct hermon *hermon ) {
  474. return hermon_cmd ( hermon,
  475. HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
  476. 0, NULL, 0, NULL );
  477. }
  478. static inline int
  479. hermon_cmd_map_fa ( struct hermon *hermon,
  480. const struct hermonprm_virtual_physical_mapping *map ) {
  481. return hermon_cmd ( hermon,
  482. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
  483. 1, sizeof ( *map ) ),
  484. 0, map, 1, NULL );
  485. }
  486. static inline int
  487. hermon_cmd_sense_port ( struct hermon *hermon, unsigned int port,
  488. struct hermonprm_sense_port *port_type ) {
  489. return hermon_cmd ( hermon,
  490. HERMON_HCR_OUT_CMD ( HERMON_HCR_SENSE_PORT,
  491. 1, sizeof ( *port_type ) ),
  492. 0, NULL, port, port_type );
  493. }
  494. /***************************************************************************
  495. *
  496. * Memory translation table operations
  497. *
  498. ***************************************************************************
  499. */
  500. /**
  501. * Allocate MTT entries
  502. *
  503. * @v hermon Hermon device
  504. * @v memory Memory to map into MTT
  505. * @v len Length of memory to map
  506. * @v mtt MTT descriptor to fill in
  507. * @ret rc Return status code
  508. */
  509. static int hermon_alloc_mtt ( struct hermon *hermon,
  510. const void *memory, size_t len,
  511. struct hermon_mtt *mtt ) {
  512. struct hermonprm_write_mtt write_mtt;
  513. physaddr_t start;
  514. unsigned int page_offset;
  515. unsigned int num_pages;
  516. int mtt_offset;
  517. unsigned int mtt_base_addr;
  518. unsigned int i;
  519. int rc;
  520. /* Find available MTT entries */
  521. start = virt_to_phys ( memory );
  522. page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
  523. start -= page_offset;
  524. len += page_offset;
  525. num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
  526. mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
  527. num_pages );
  528. if ( mtt_offset < 0 ) {
  529. DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
  530. hermon, num_pages );
  531. rc = mtt_offset;
  532. goto err_mtt_offset;
  533. }
  534. mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
  535. hermon->cap.mtt_entry_size );
  536. /* Fill in MTT structure */
  537. mtt->mtt_offset = mtt_offset;
  538. mtt->num_pages = num_pages;
  539. mtt->mtt_base_addr = mtt_base_addr;
  540. mtt->page_offset = page_offset;
  541. /* Construct and issue WRITE_MTT commands */
  542. for ( i = 0 ; i < num_pages ; i++ ) {
  543. memset ( &write_mtt, 0, sizeof ( write_mtt ) );
  544. MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
  545. value, mtt_base_addr );
  546. MLX_FILL_2 ( &write_mtt.mtt, 1,
  547. p, 1,
  548. ptag_l, ( start >> 3 ) );
  549. if ( ( rc = hermon_cmd_write_mtt ( hermon,
  550. &write_mtt ) ) != 0 ) {
  551. DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
  552. hermon, mtt_base_addr );
  553. goto err_write_mtt;
  554. }
  555. start += HERMON_PAGE_SIZE;
  556. mtt_base_addr += hermon->cap.mtt_entry_size;
  557. }
  558. return 0;
  559. err_write_mtt:
  560. hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
  561. err_mtt_offset:
  562. return rc;
  563. }
  564. /**
  565. * Free MTT entries
  566. *
  567. * @v hermon Hermon device
  568. * @v mtt MTT descriptor
  569. */
  570. static void hermon_free_mtt ( struct hermon *hermon,
  571. struct hermon_mtt *mtt ) {
  572. hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
  573. mtt->num_pages );
  574. }
  575. /***************************************************************************
  576. *
  577. * MAD operations
  578. *
  579. ***************************************************************************
  580. */
  581. /**
  582. * Issue management datagram
  583. *
  584. * @v ibdev Infiniband device
  585. * @v mad Management datagram
  586. * @ret rc Return status code
  587. */
  588. static int hermon_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
  589. struct hermon *hermon = ib_get_drvdata ( ibdev );
  590. union hermonprm_mad mad_ifc;
  591. int rc;
  592. linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
  593. mad_size_mismatch );
  594. /* Copy in request packet */
  595. memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );
  596. /* Issue MAD */
  597. if ( ( rc = hermon_cmd_mad_ifc ( hermon, ibdev->port,
  598. &mad_ifc ) ) != 0 ) {
  599. DBGC ( hermon, "Hermon %p could not issue MAD IFC: %s\n",
  600. hermon, strerror ( rc ) );
  601. return rc;
  602. }
  603. /* Copy out reply packet */
  604. memcpy ( mad, &mad_ifc.mad, sizeof ( *mad ) );
  605. if ( mad->hdr.status != 0 ) {
  606. DBGC ( hermon, "Hermon %p MAD IFC status %04x\n",
  607. hermon, ntohs ( mad->hdr.status ) );
  608. return -EIO;
  609. }
  610. return 0;
  611. }
  612. /***************************************************************************
  613. *
  614. * Completion queue operations
  615. *
  616. ***************************************************************************
  617. */
  618. /**
  619. * Create completion queue
  620. *
  621. * @v ibdev Infiniband device
  622. * @v cq Completion queue
  623. * @ret rc Return status code
  624. */
  625. static int hermon_create_cq ( struct ib_device *ibdev,
  626. struct ib_completion_queue *cq ) {
  627. struct hermon *hermon = ib_get_drvdata ( ibdev );
  628. struct hermon_completion_queue *hermon_cq;
  629. struct hermonprm_completion_queue_context cqctx;
  630. int cqn_offset;
  631. unsigned int i;
  632. int rc;
  633. /* Find a free completion queue number */
  634. cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
  635. HERMON_MAX_CQS, 1 );
  636. if ( cqn_offset < 0 ) {
  637. DBGC ( hermon, "Hermon %p out of completion queues\n",
  638. hermon );
  639. rc = cqn_offset;
  640. goto err_cqn_offset;
  641. }
  642. cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
  643. /* Allocate control structures */
  644. hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
  645. if ( ! hermon_cq ) {
  646. rc = -ENOMEM;
  647. goto err_hermon_cq;
  648. }
  649. /* Allocate completion queue itself */
  650. hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
  651. hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
  652. sizeof ( hermon_cq->cqe[0] ) );
  653. if ( ! hermon_cq->cqe ) {
  654. rc = -ENOMEM;
  655. goto err_cqe;
  656. }
  657. memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
  658. for ( i = 0 ; i < cq->num_cqes ; i++ ) {
  659. MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
  660. }
  661. barrier();
  662. /* Allocate MTT entries */
  663. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
  664. hermon_cq->cqe_size,
  665. &hermon_cq->mtt ) ) != 0 )
  666. goto err_alloc_mtt;
  667. /* Hand queue over to hardware */
  668. memset ( &cqctx, 0, sizeof ( cqctx ) );
  669. MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
  670. MLX_FILL_1 ( &cqctx, 2,
  671. page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
  672. MLX_FILL_2 ( &cqctx, 3,
  673. usr_page, HERMON_UAR_NON_EQ_PAGE,
  674. log_cq_size, fls ( cq->num_cqes - 1 ) );
  675. MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
  676. ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
  677. MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
  678. ( virt_to_phys ( &hermon_cq->doorbell ) >> 3 ) );
  679. if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  680. DBGC ( hermon, "Hermon %p SW2HW_CQ failed: %s\n",
  681. hermon, strerror ( rc ) );
  682. goto err_sw2hw_cq;
  683. }
  684. DBGC ( hermon, "Hermon %p CQN %#lx ring at [%p,%p)\n",
  685. hermon, cq->cqn, hermon_cq->cqe,
  686. ( ( ( void * ) hermon_cq->cqe ) + hermon_cq->cqe_size ) );
  687. ib_cq_set_drvdata ( cq, hermon_cq );
  688. return 0;
  689. err_sw2hw_cq:
  690. hermon_free_mtt ( hermon, &hermon_cq->mtt );
  691. err_alloc_mtt:
  692. free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
  693. err_cqe:
  694. free ( hermon_cq );
  695. err_hermon_cq:
  696. hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
  697. err_cqn_offset:
  698. return rc;
  699. }
  700. /**
  701. * Destroy completion queue
  702. *
  703. * @v ibdev Infiniband device
  704. * @v cq Completion queue
  705. */
  706. static void hermon_destroy_cq ( struct ib_device *ibdev,
  707. struct ib_completion_queue *cq ) {
  708. struct hermon *hermon = ib_get_drvdata ( ibdev );
  709. struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
  710. struct hermonprm_completion_queue_context cqctx;
  711. int cqn_offset;
  712. int rc;
  713. /* Take ownership back from hardware */
  714. if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  715. DBGC ( hermon, "Hermon %p FATAL HW2SW_CQ failed on CQN %#lx: "
  716. "%s\n", hermon, cq->cqn, strerror ( rc ) );
  717. /* Leak memory and return; at least we avoid corruption */
  718. return;
  719. }
  720. /* Free MTT entries */
  721. hermon_free_mtt ( hermon, &hermon_cq->mtt );
  722. /* Free memory */
  723. free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
  724. free ( hermon_cq );
  725. /* Mark queue number as free */
  726. cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
  727. hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
  728. ib_cq_set_drvdata ( cq, NULL );
  729. }
  730. /***************************************************************************
  731. *
  732. * Queue pair operations
  733. *
  734. ***************************************************************************
  735. */
  736. /**
  737. * Assign queue pair number
  738. *
  739. * @v ibdev Infiniband device
  740. * @v qp Queue pair
  741. * @ret rc Return status code
  742. */
  743. static int hermon_alloc_qpn ( struct ib_device *ibdev,
  744. struct ib_queue_pair *qp ) {
  745. struct hermon *hermon = ib_get_drvdata ( ibdev );
  746. unsigned int port_offset;
  747. int qpn_offset;
  748. /* Calculate queue pair number */
  749. port_offset = ( ibdev->port - HERMON_PORT_BASE );
  750. switch ( qp->type ) {
  751. case IB_QPT_SMI:
  752. qp->qpn = ( hermon->special_qpn_base + port_offset );
  753. return 0;
  754. case IB_QPT_GSI:
  755. qp->qpn = ( hermon->special_qpn_base + 2 + port_offset );
  756. return 0;
  757. case IB_QPT_UD:
  758. case IB_QPT_RC:
  759. /* Find a free queue pair number */
  760. qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
  761. HERMON_MAX_QPS, 1 );
  762. if ( qpn_offset < 0 ) {
  763. DBGC ( hermon, "Hermon %p out of queue pairs\n",
  764. hermon );
  765. return qpn_offset;
  766. }
  767. qp->qpn = ( hermon->qpn_base + qpn_offset );
  768. return 0;
  769. default:
  770. DBGC ( hermon, "Hermon %p unsupported QP type %d\n",
  771. hermon, qp->type );
  772. return -ENOTSUP;
  773. }
  774. }
  775. /**
  776. * Free queue pair number
  777. *
  778. * @v ibdev Infiniband device
  779. * @v qp Queue pair
  780. */
  781. static void hermon_free_qpn ( struct ib_device *ibdev,
  782. struct ib_queue_pair *qp ) {
  783. struct hermon *hermon = ib_get_drvdata ( ibdev );
  784. int qpn_offset;
  785. qpn_offset = ( qp->qpn - hermon->qpn_base );
  786. if ( qpn_offset >= 0 )
  787. hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
  788. }
  789. /**
  790. * Calculate transmission rate
  791. *
  792. * @v av Address vector
  793. * @ret hermon_rate Hermon rate
  794. */
  795. static unsigned int hermon_rate ( struct ib_address_vector *av ) {
  796. return ( ( ( av->rate >= IB_RATE_2_5 ) && ( av->rate <= IB_RATE_120 ) )
  797. ? ( av->rate + 5 ) : 0 );
  798. }
  799. /**
  800. * Calculate schedule queue
  801. *
  802. * @v ibdev Infiniband device
  803. * @v qp Queue pair
  804. * @ret sched_queue Schedule queue
  805. */
  806. static unsigned int hermon_sched_queue ( struct ib_device *ibdev,
  807. struct ib_queue_pair *qp ) {
  808. return ( ( ( qp->type == IB_QPT_SMI ) ?
  809. HERMON_SCHED_QP0 : HERMON_SCHED_DEFAULT ) |
  810. ( ( ibdev->port - 1 ) << 6 ) );
  811. }
  812. /** Queue pair transport service type map */
  813. static uint8_t hermon_qp_st[] = {
  814. [IB_QPT_SMI] = HERMON_ST_MLX,
  815. [IB_QPT_GSI] = HERMON_ST_MLX,
  816. [IB_QPT_UD] = HERMON_ST_UD,
  817. [IB_QPT_RC] = HERMON_ST_RC,
  818. };
  819. /**
  820. * Dump queue pair context (for debugging only)
  821. *
  822. * @v hermon Hermon device
  823. * @v qp Queue pair
  824. * @ret rc Return status code
  825. */
  826. static inline int hermon_dump_qpctx ( struct hermon *hermon,
  827. struct ib_queue_pair *qp ) {
  828. struct hermonprm_qp_ee_state_transitions qpctx;
  829. int rc;
  830. memset ( &qpctx, 0, sizeof ( qpctx ) );
  831. if ( ( rc = hermon_cmd_query_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ) {
  832. DBGC ( hermon, "Hermon %p QUERY_QP failed: %s\n",
  833. hermon, strerror ( rc ) );
  834. return rc;
  835. }
  836. DBGC ( hermon, "Hermon %p QPN %lx context:\n", hermon, qp->qpn );
  837. DBGC_HDA ( hermon, 0, &qpctx.u.dwords[2],
  838. ( sizeof ( qpctx ) - 8 ) );
  839. return 0;
  840. }
  841. /**
  842. * Create queue pair
  843. *
  844. * @v ibdev Infiniband device
  845. * @v qp Queue pair
  846. * @ret rc Return status code
  847. */
  848. static int hermon_create_qp ( struct ib_device *ibdev,
  849. struct ib_queue_pair *qp ) {
  850. struct hermon *hermon = ib_get_drvdata ( ibdev );
  851. struct hermon_queue_pair *hermon_qp;
  852. struct hermonprm_qp_ee_state_transitions qpctx;
  853. int rc;
  854. /* Calculate queue pair number */
  855. if ( ( rc = hermon_alloc_qpn ( ibdev, qp ) ) != 0 )
  856. goto err_alloc_qpn;
  857. /* Allocate control structures */
  858. hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
  859. if ( ! hermon_qp ) {
  860. rc = -ENOMEM;
  861. goto err_hermon_qp;
  862. }
  863. /* Calculate doorbell address */
  864. hermon_qp->send.doorbell =
  865. ( hermon->uar + HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE +
  866. HERMON_DB_POST_SND_OFFSET );
  867. /* Allocate work queue buffer */
  868. hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
  869. ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
  870. hermon_qp->send.num_wqes =
  871. ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
  872. hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
  873. sizeof ( hermon_qp->send.wqe[0] ) );
  874. hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
  875. sizeof ( hermon_qp->recv.wqe[0] ) );
  876. hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
  877. hermon_qp->recv.wqe_size );
  878. hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
  879. sizeof ( hermon_qp->send.wqe[0] ) );
  880. if ( ! hermon_qp->wqe ) {
  881. rc = -ENOMEM;
  882. goto err_alloc_wqe;
  883. }
  884. hermon_qp->send.wqe = hermon_qp->wqe;
  885. memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
  886. hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
  887. memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
  888. /* Allocate MTT entries */
  889. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
  890. hermon_qp->wqe_size,
  891. &hermon_qp->mtt ) ) != 0 ) {
  892. goto err_alloc_mtt;
  893. }
  894. /* Transition queue to INIT state */
  895. memset ( &qpctx, 0, sizeof ( qpctx ) );
  896. MLX_FILL_2 ( &qpctx, 2,
  897. qpc_eec_data.pm_state, HERMON_PM_STATE_MIGRATED,
  898. qpc_eec_data.st, hermon_qp_st[qp->type] );
  899. MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
  900. MLX_FILL_4 ( &qpctx, 4,
  901. qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
  902. qpc_eec_data.log_rq_stride,
  903. ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
  904. qpc_eec_data.log_sq_size,
  905. fls ( hermon_qp->send.num_wqes - 1 ),
  906. qpc_eec_data.log_sq_stride,
  907. ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
  908. MLX_FILL_1 ( &qpctx, 5,
  909. qpc_eec_data.usr_page, HERMON_UAR_NON_EQ_PAGE );
  910. MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
  911. MLX_FILL_4 ( &qpctx, 38,
  912. qpc_eec_data.rre, 1,
  913. qpc_eec_data.rwe, 1,
  914. qpc_eec_data.rae, 1,
  915. qpc_eec_data.page_offset,
  916. ( hermon_qp->mtt.page_offset >> 6 ) );
  917. MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
  918. MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
  919. ( virt_to_phys ( &hermon_qp->recv.doorbell ) >> 2 ) );
  920. MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
  921. ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
  922. if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
  923. &qpctx ) ) != 0 ) {
  924. DBGC ( hermon, "Hermon %p RST2INIT_QP failed: %s\n",
  925. hermon, strerror ( rc ) );
  926. goto err_rst2init_qp;
  927. }
  928. DBGC ( hermon, "Hermon %p QPN %#lx send ring at [%p,%p)\n",
  929. hermon, qp->qpn, hermon_qp->send.wqe,
  930. ( ((void *)hermon_qp->send.wqe ) + hermon_qp->send.wqe_size ) );
  931. DBGC ( hermon, "Hermon %p QPN %#lx receive ring at [%p,%p)\n",
  932. hermon, qp->qpn, hermon_qp->recv.wqe,
  933. ( ((void *)hermon_qp->recv.wqe ) + hermon_qp->recv.wqe_size ) );
  934. ib_qp_set_drvdata ( qp, hermon_qp );
  935. return 0;
  936. hermon_cmd_2rst_qp ( hermon, qp->qpn );
  937. err_rst2init_qp:
  938. hermon_free_mtt ( hermon, &hermon_qp->mtt );
  939. err_alloc_mtt:
  940. free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
  941. err_alloc_wqe:
  942. free ( hermon_qp );
  943. err_hermon_qp:
  944. hermon_free_qpn ( ibdev, qp );
  945. err_alloc_qpn:
  946. return rc;
  947. }
  948. /**
  949. * Modify queue pair
  950. *
  951. * @v ibdev Infiniband device
  952. * @v qp Queue pair
  953. * @ret rc Return status code
  954. */
  955. static int hermon_modify_qp ( struct ib_device *ibdev,
  956. struct ib_queue_pair *qp ) {
  957. struct hermon *hermon = ib_get_drvdata ( ibdev );
  958. struct hermonprm_qp_ee_state_transitions qpctx;
  959. int rc;
  960. /* Transition queue to RTR state */
  961. memset ( &qpctx, 0, sizeof ( qpctx ) );
  962. MLX_FILL_1 ( &qpctx, 0, opt_param_mask, HERMON_QP_OPT_PARAM_QKEY );
  963. MLX_FILL_2 ( &qpctx, 4,
  964. qpc_eec_data.mtu, HERMON_MTU_2048,
  965. qpc_eec_data.msg_max, 31 );// 11 /* 2^11 = 2048 */ );
  966. MLX_FILL_1 ( &qpctx, 7, qpc_eec_data.remote_qpn_een, qp->av.qpn );
  967. MLX_FILL_1 ( &qpctx, 9,
  968. qpc_eec_data.primary_address_path.rlid, qp->av.lid );
  969. MLX_FILL_1 ( &qpctx, 10,
  970. qpc_eec_data.primary_address_path.max_stat_rate,
  971. hermon_rate ( &qp->av ) );
  972. memcpy ( &qpctx.u.dwords[12], &qp->av.gid, sizeof ( qp->av.gid ) );
  973. MLX_FILL_1 ( &qpctx, 16,
  974. qpc_eec_data.primary_address_path.sched_queue,
  975. hermon_sched_queue ( ibdev, qp ) );
  976. MLX_FILL_1 ( &qpctx, 39, qpc_eec_data.next_rcv_psn, qp->recv.psn );
  977. MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
  978. if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
  979. &qpctx ) ) != 0 ) {
  980. DBGC ( hermon, "Hermon %p INIT2RTR_QP failed: %s\n",
  981. hermon, strerror ( rc ) );
  982. return rc;
  983. }
  984. /* Transition queue to RTS state */
  985. memset ( &qpctx, 0, sizeof ( qpctx ) );
  986. MLX_FILL_1 ( &qpctx, 10,
  987. qpc_eec_data.primary_address_path.ack_timeout, 0x13 );
  988. MLX_FILL_2 ( &qpctx, 30,
  989. qpc_eec_data.retry_count, HERMON_RETRY_MAX,
  990. qpc_eec_data.rnr_retry, HERMON_RETRY_MAX );
  991. MLX_FILL_1 ( &qpctx, 32, qpc_eec_data.next_send_psn, qp->send.psn );
  992. if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
  993. DBGC ( hermon, "Hermon %p RTR2RTS_QP failed: %s\n",
  994. hermon, strerror ( rc ) );
  995. return rc;
  996. }
  997. return 0;
  998. }
  999. /**
  1000. * Destroy queue pair
  1001. *
  1002. * @v ibdev Infiniband device
  1003. * @v qp Queue pair
  1004. */
  1005. static void hermon_destroy_qp ( struct ib_device *ibdev,
  1006. struct ib_queue_pair *qp ) {
  1007. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1008. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1009. int rc;
  1010. /* Take ownership back from hardware */
  1011. if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
  1012. DBGC ( hermon, "Hermon %p FATAL 2RST_QP failed on QPN %#lx: "
  1013. "%s\n", hermon, qp->qpn, strerror ( rc ) );
  1014. /* Leak memory and return; at least we avoid corruption */
  1015. return;
  1016. }
  1017. /* Free MTT entries */
  1018. hermon_free_mtt ( hermon, &hermon_qp->mtt );
  1019. /* Free memory */
  1020. free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
  1021. free ( hermon_qp );
  1022. /* Mark queue number as free */
  1023. hermon_free_qpn ( ibdev, qp );
  1024. ib_qp_set_drvdata ( qp, NULL );
  1025. }
  1026. /***************************************************************************
  1027. *
  1028. * Work request operations
  1029. *
  1030. ***************************************************************************
  1031. */
  1032. /**
  1033. * Construct UD send work queue entry
  1034. *
  1035. * @v ibdev Infiniband device
  1036. * @v qp Queue pair
  1037. * @v av Address vector
  1038. * @v iobuf I/O buffer
  1039. * @v wqe Send work queue entry
  1040. * @ret opcode Control opcode
  1041. */
  1042. static unsigned int
  1043. hermon_fill_ud_send_wqe ( struct ib_device *ibdev,
  1044. struct ib_queue_pair *qp __unused,
  1045. struct ib_address_vector *av,
  1046. struct io_buffer *iobuf,
  1047. union hermon_send_wqe *wqe ) {
  1048. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1049. MLX_FILL_1 ( &wqe->ud.ctrl, 1, ds,
  1050. ( ( offsetof ( typeof ( wqe->ud ), data[1] ) / 16 ) ) );
  1051. MLX_FILL_1 ( &wqe->ud.ctrl, 2, c, 0x03 /* generate completion */ );
  1052. MLX_FILL_2 ( &wqe->ud.ud, 0,
  1053. ud_address_vector.pd, HERMON_GLOBAL_PD,
  1054. ud_address_vector.port_number, ibdev->port );
  1055. MLX_FILL_2 ( &wqe->ud.ud, 1,
  1056. ud_address_vector.rlid, av->lid,
  1057. ud_address_vector.g, av->gid_present );
  1058. MLX_FILL_1 ( &wqe->ud.ud, 2,
  1059. ud_address_vector.max_stat_rate, hermon_rate ( av ) );
  1060. MLX_FILL_1 ( &wqe->ud.ud, 3, ud_address_vector.sl, av->sl );
  1061. memcpy ( &wqe->ud.ud.u.dwords[4], &av->gid, sizeof ( av->gid ) );
  1062. MLX_FILL_1 ( &wqe->ud.ud, 8, destination_qp, av->qpn );
  1063. MLX_FILL_1 ( &wqe->ud.ud, 9, q_key, av->qkey );
  1064. MLX_FILL_1 ( &wqe->ud.data[0], 0, byte_count, iob_len ( iobuf ) );
  1065. MLX_FILL_1 ( &wqe->ud.data[0], 1, l_key, hermon->lkey );
  1066. MLX_FILL_1 ( &wqe->ud.data[0], 3,
  1067. local_address_l, virt_to_bus ( iobuf->data ) );
  1068. return HERMON_OPCODE_SEND;
  1069. }
  1070. /**
  1071. * Construct MLX send work queue entry
  1072. *
  1073. * @v ibdev Infiniband device
  1074. * @v qp Queue pair
  1075. * @v av Address vector
  1076. * @v iobuf I/O buffer
  1077. * @v wqe Send work queue entry
  1078. * @ret opcode Control opcode
  1079. */
  1080. static unsigned int
  1081. hermon_fill_mlx_send_wqe ( struct ib_device *ibdev,
  1082. struct ib_queue_pair *qp,
  1083. struct ib_address_vector *av,
  1084. struct io_buffer *iobuf,
  1085. union hermon_send_wqe *wqe ) {
  1086. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1087. struct io_buffer headers;
  1088. /* Construct IB headers */
  1089. iob_populate ( &headers, &wqe->mlx.headers, 0,
  1090. sizeof ( wqe->mlx.headers ) );
  1091. iob_reserve ( &headers, sizeof ( wqe->mlx.headers ) );
  1092. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), av );
  1093. /* Fill work queue entry */
  1094. MLX_FILL_1 ( &wqe->mlx.ctrl, 1, ds,
  1095. ( ( offsetof ( typeof ( wqe->mlx ), data[2] ) / 16 ) ) );
  1096. MLX_FILL_5 ( &wqe->mlx.ctrl, 2,
  1097. c, 0x03 /* generate completion */,
  1098. icrc, 0 /* generate ICRC */,
  1099. max_statrate, hermon_rate ( av ),
  1100. slr, 0,
  1101. v15, ( ( qp->ext_qpn == IB_QPN_SMI ) ? 1 : 0 ) );
  1102. MLX_FILL_1 ( &wqe->mlx.ctrl, 3, rlid, av->lid );
  1103. MLX_FILL_1 ( &wqe->mlx.data[0], 0,
  1104. byte_count, iob_len ( &headers ) );
  1105. MLX_FILL_1 ( &wqe->mlx.data[0], 1, l_key, hermon->lkey );
  1106. MLX_FILL_1 ( &wqe->mlx.data[0], 3,
  1107. local_address_l, virt_to_bus ( headers.data ) );
  1108. MLX_FILL_1 ( &wqe->mlx.data[1], 0,
  1109. byte_count, ( iob_len ( iobuf ) + 4 /* ICRC */ ) );
  1110. MLX_FILL_1 ( &wqe->mlx.data[1], 1, l_key, hermon->lkey );
  1111. MLX_FILL_1 ( &wqe->mlx.data[1], 3,
  1112. local_address_l, virt_to_bus ( iobuf->data ) );
  1113. return HERMON_OPCODE_SEND;
  1114. }
  1115. /**
  1116. * Construct RC send work queue entry
  1117. *
  1118. * @v ibdev Infiniband device
  1119. * @v qp Queue pair
  1120. * @v av Address vector
  1121. * @v iobuf I/O buffer
  1122. * @v wqe Send work queue entry
  1123. * @ret opcode Control opcode
  1124. */
  1125. static unsigned int
  1126. hermon_fill_rc_send_wqe ( struct ib_device *ibdev,
  1127. struct ib_queue_pair *qp __unused,
  1128. struct ib_address_vector *av __unused,
  1129. struct io_buffer *iobuf,
  1130. union hermon_send_wqe *wqe ) {
  1131. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1132. MLX_FILL_1 ( &wqe->rc.ctrl, 1, ds,
  1133. ( ( offsetof ( typeof ( wqe->rc ), data[1] ) / 16 ) ) );
  1134. MLX_FILL_1 ( &wqe->rc.ctrl, 2, c, 0x03 /* generate completion */ );
  1135. MLX_FILL_1 ( &wqe->rc.data[0], 0, byte_count, iob_len ( iobuf ) );
  1136. MLX_FILL_1 ( &wqe->rc.data[0], 1, l_key, hermon->lkey );
  1137. MLX_FILL_1 ( &wqe->rc.data[0], 3,
  1138. local_address_l, virt_to_bus ( iobuf->data ) );
  1139. return HERMON_OPCODE_SEND;
  1140. }
  1141. /** Work queue entry constructors */
  1142. static unsigned int
  1143. ( * hermon_fill_send_wqe[] ) ( struct ib_device *ibdev,
  1144. struct ib_queue_pair *qp,
  1145. struct ib_address_vector *av,
  1146. struct io_buffer *iobuf,
  1147. union hermon_send_wqe *wqe ) = {
  1148. [IB_QPT_SMI] = hermon_fill_mlx_send_wqe,
  1149. [IB_QPT_GSI] = hermon_fill_mlx_send_wqe,
  1150. [IB_QPT_UD] = hermon_fill_ud_send_wqe,
  1151. [IB_QPT_RC] = hermon_fill_rc_send_wqe,
  1152. };
  1153. /**
  1154. * Post send work queue entry
  1155. *
  1156. * @v ibdev Infiniband device
  1157. * @v qp Queue pair
  1158. * @v av Address vector
  1159. * @v iobuf I/O buffer
  1160. * @ret rc Return status code
  1161. */
  1162. static int hermon_post_send ( struct ib_device *ibdev,
  1163. struct ib_queue_pair *qp,
  1164. struct ib_address_vector *av,
  1165. struct io_buffer *iobuf ) {
  1166. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1167. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1168. struct ib_work_queue *wq = &qp->send;
  1169. struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
  1170. union hermon_send_wqe *wqe;
  1171. union hermonprm_doorbell_register db_reg;
  1172. unsigned int wqe_idx_mask;
  1173. unsigned int opcode;
  1174. /* Allocate work queue entry */
  1175. wqe_idx_mask = ( wq->num_wqes - 1 );
  1176. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  1177. DBGC ( hermon, "Hermon %p send queue full", hermon );
  1178. return -ENOBUFS;
  1179. }
  1180. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  1181. wqe = &hermon_send_wq->wqe[ wq->next_idx &
  1182. ( hermon_send_wq->num_wqes - 1 ) ];
  1183. /* Construct work queue entry */
  1184. memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
  1185. ( sizeof ( *wqe ) - 4 ) );
  1186. assert ( qp->type < ( sizeof ( hermon_fill_send_wqe ) /
  1187. sizeof ( hermon_fill_send_wqe[0] ) ) );
  1188. assert ( hermon_fill_send_wqe[qp->type] != NULL );
  1189. opcode = hermon_fill_send_wqe[qp->type] ( ibdev, qp, av, iobuf, wqe );
  1190. barrier();
  1191. MLX_FILL_2 ( &wqe->ctrl, 0,
  1192. opcode, opcode,
  1193. owner,
  1194. ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 ) );
  1195. DBGCP ( hermon, "Hermon %p posting send WQE:\n", hermon );
  1196. DBGCP_HD ( hermon, wqe, sizeof ( *wqe ) );
  1197. barrier();
  1198. /* Ring doorbell register */
  1199. MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
  1200. DBGCP ( hermon, "Ringing doorbell %08lx with %08x\n",
  1201. virt_to_phys ( hermon_send_wq->doorbell ), db_reg.dword[0] );
  1202. writel ( db_reg.dword[0], ( hermon_send_wq->doorbell ) );
  1203. /* Update work queue's index */
  1204. wq->next_idx++;
  1205. return 0;
  1206. }
  1207. /**
  1208. * Post receive work queue entry
  1209. *
  1210. * @v ibdev Infiniband device
  1211. * @v qp Queue pair
  1212. * @v iobuf I/O buffer
  1213. * @ret rc Return status code
  1214. */
  1215. static int hermon_post_recv ( struct ib_device *ibdev,
  1216. struct ib_queue_pair *qp,
  1217. struct io_buffer *iobuf ) {
  1218. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1219. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1220. struct ib_work_queue *wq = &qp->recv;
  1221. struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
  1222. struct hermonprm_recv_wqe *wqe;
  1223. unsigned int wqe_idx_mask;
  1224. /* Allocate work queue entry */
  1225. wqe_idx_mask = ( wq->num_wqes - 1 );
  1226. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  1227. DBGC ( hermon, "Hermon %p receive queue full", hermon );
  1228. return -ENOBUFS;
  1229. }
  1230. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  1231. wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
  1232. /* Construct work queue entry */
  1233. MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
  1234. MLX_FILL_1 ( &wqe->data[0], 1, l_key, hermon->lkey );
  1235. MLX_FILL_1 ( &wqe->data[0], 3,
  1236. local_address_l, virt_to_bus ( iobuf->data ) );
  1237. /* Update work queue's index */
  1238. wq->next_idx++;
  1239. /* Update doorbell record */
  1240. barrier();
  1241. MLX_FILL_1 ( &hermon_recv_wq->doorbell, 0, receive_wqe_counter,
  1242. ( wq->next_idx & 0xffff ) );
  1243. return 0;
  1244. }
  1245. /**
  1246. * Handle completion
  1247. *
  1248. * @v ibdev Infiniband device
  1249. * @v cq Completion queue
  1250. * @v cqe Hardware completion queue entry
  1251. * @ret rc Return status code
  1252. */
  1253. static int hermon_complete ( struct ib_device *ibdev,
  1254. struct ib_completion_queue *cq,
  1255. union hermonprm_completion_entry *cqe ) {
  1256. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1257. struct ib_work_queue *wq;
  1258. struct ib_queue_pair *qp;
  1259. struct hermon_queue_pair *hermon_qp;
  1260. struct io_buffer *iobuf;
  1261. struct ib_address_vector recv_av;
  1262. struct ib_global_route_header *grh;
  1263. struct ib_address_vector *av;
  1264. unsigned int opcode;
  1265. unsigned long qpn;
  1266. int is_send;
  1267. unsigned int wqe_idx;
  1268. size_t len;
  1269. int rc = 0;
  1270. /* Parse completion */
  1271. qpn = MLX_GET ( &cqe->normal, qpn );
  1272. is_send = MLX_GET ( &cqe->normal, s_r );
  1273. opcode = MLX_GET ( &cqe->normal, opcode );
  1274. if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
  1275. /* "s" field is not valid for error opcodes */
  1276. is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
  1277. DBGC ( hermon, "Hermon %p CQN %lx syndrome %x vendor %x\n",
  1278. hermon, cq->cqn, MLX_GET ( &cqe->error, syndrome ),
  1279. MLX_GET ( &cqe->error, vendor_error_syndrome ) );
  1280. rc = -EIO;
  1281. /* Don't return immediately; propagate error to completer */
  1282. }
  1283. /* Identify work queue */
  1284. wq = ib_find_wq ( cq, qpn, is_send );
  1285. if ( ! wq ) {
  1286. DBGC ( hermon, "Hermon %p CQN %lx unknown %s QPN %lx\n",
  1287. hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
  1288. return -EIO;
  1289. }
  1290. qp = wq->qp;
  1291. hermon_qp = ib_qp_get_drvdata ( qp );
  1292. /* Identify I/O buffer */
  1293. wqe_idx = ( MLX_GET ( &cqe->normal, wqe_counter ) &
  1294. ( wq->num_wqes - 1 ) );
  1295. iobuf = wq->iobufs[wqe_idx];
  1296. if ( ! iobuf ) {
  1297. DBGC ( hermon, "Hermon %p CQN %lx QPN %lx empty WQE %x\n",
  1298. hermon, cq->cqn, qp->qpn, wqe_idx );
  1299. return -EIO;
  1300. }
  1301. wq->iobufs[wqe_idx] = NULL;
  1302. if ( is_send ) {
  1303. /* Hand off to completion handler */
  1304. ib_complete_send ( ibdev, qp, iobuf, rc );
  1305. } else {
  1306. /* Set received length */
  1307. len = MLX_GET ( &cqe->normal, byte_cnt );
  1308. assert ( len <= iob_tailroom ( iobuf ) );
  1309. iob_put ( iobuf, len );
  1310. switch ( qp->type ) {
  1311. case IB_QPT_SMI:
  1312. case IB_QPT_GSI:
  1313. case IB_QPT_UD:
  1314. assert ( iob_len ( iobuf ) >= sizeof ( *grh ) );
  1315. grh = iobuf->data;
  1316. iob_pull ( iobuf, sizeof ( *grh ) );
  1317. /* Construct address vector */
  1318. av = &recv_av;
  1319. memset ( av, 0, sizeof ( *av ) );
  1320. av->qpn = MLX_GET ( &cqe->normal, srq_rqpn );
  1321. av->lid = MLX_GET ( &cqe->normal, slid_smac47_32 );
  1322. av->sl = MLX_GET ( &cqe->normal, sl );
  1323. av->gid_present = MLX_GET ( &cqe->normal, g );
  1324. memcpy ( &av->gid, &grh->sgid, sizeof ( av->gid ) );
  1325. break;
  1326. case IB_QPT_RC:
  1327. av = &qp->av;
  1328. break;
  1329. default:
  1330. assert ( 0 );
  1331. return -EINVAL;
  1332. }
  1333. /* Hand off to completion handler */
  1334. ib_complete_recv ( ibdev, qp, av, iobuf, rc );
  1335. }
  1336. return rc;
  1337. }
  1338. /**
  1339. * Poll completion queue
  1340. *
  1341. * @v ibdev Infiniband device
  1342. * @v cq Completion queue
  1343. */
  1344. static void hermon_poll_cq ( struct ib_device *ibdev,
  1345. struct ib_completion_queue *cq ) {
  1346. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1347. struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
  1348. union hermonprm_completion_entry *cqe;
  1349. unsigned int cqe_idx_mask;
  1350. int rc;
  1351. while ( 1 ) {
  1352. /* Look for completion entry */
  1353. cqe_idx_mask = ( cq->num_cqes - 1 );
  1354. cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
  1355. if ( MLX_GET ( &cqe->normal, owner ) ^
  1356. ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
  1357. /* Entry still owned by hardware; end of poll */
  1358. break;
  1359. }
  1360. DBGCP ( hermon, "Hermon %p completion:\n", hermon );
  1361. DBGCP_HD ( hermon, cqe, sizeof ( *cqe ) );
  1362. /* Handle completion */
  1363. if ( ( rc = hermon_complete ( ibdev, cq, cqe ) ) != 0 ) {
  1364. DBGC ( hermon, "Hermon %p failed to complete: %s\n",
  1365. hermon, strerror ( rc ) );
  1366. DBGC_HD ( hermon, cqe, sizeof ( *cqe ) );
  1367. }
  1368. /* Update completion queue's index */
  1369. cq->next_idx++;
  1370. /* Update doorbell record */
  1371. MLX_FILL_1 ( &hermon_cq->doorbell, 0, update_ci,
  1372. ( cq->next_idx & 0x00ffffffUL ) );
  1373. }
  1374. }
  1375. /***************************************************************************
  1376. *
  1377. * Event queues
  1378. *
  1379. ***************************************************************************
  1380. */
  1381. /**
  1382. * Create event queue
  1383. *
  1384. * @v hermon Hermon device
  1385. * @ret rc Return status code
  1386. */
  1387. static int hermon_create_eq ( struct hermon *hermon ) {
  1388. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1389. struct hermonprm_eqc eqctx;
  1390. struct hermonprm_event_mask mask;
  1391. unsigned int i;
  1392. int rc;
  1393. /* Select event queue number */
  1394. hermon_eq->eqn = ( 4 * hermon->cap.reserved_uars );
  1395. if ( hermon_eq->eqn < hermon->cap.reserved_eqs )
  1396. hermon_eq->eqn = hermon->cap.reserved_eqs;
  1397. /* Calculate doorbell address */
  1398. hermon_eq->doorbell =
  1399. ( hermon->uar + HERMON_DB_EQ_OFFSET ( hermon_eq->eqn ) );
  1400. /* Allocate event queue itself */
  1401. hermon_eq->eqe_size =
  1402. ( HERMON_NUM_EQES * sizeof ( hermon_eq->eqe[0] ) );
  1403. hermon_eq->eqe = malloc_dma ( hermon_eq->eqe_size,
  1404. sizeof ( hermon_eq->eqe[0] ) );
  1405. if ( ! hermon_eq->eqe ) {
  1406. rc = -ENOMEM;
  1407. goto err_eqe;
  1408. }
  1409. memset ( hermon_eq->eqe, 0, hermon_eq->eqe_size );
  1410. for ( i = 0 ; i < HERMON_NUM_EQES ; i++ ) {
  1411. MLX_FILL_1 ( &hermon_eq->eqe[i].generic, 7, owner, 1 );
  1412. }
  1413. barrier();
  1414. /* Allocate MTT entries */
  1415. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_eq->eqe,
  1416. hermon_eq->eqe_size,
  1417. &hermon_eq->mtt ) ) != 0 )
  1418. goto err_alloc_mtt;
  1419. /* Hand queue over to hardware */
  1420. memset ( &eqctx, 0, sizeof ( eqctx ) );
  1421. MLX_FILL_1 ( &eqctx, 0, st, 0xa /* "Fired" */ );
  1422. MLX_FILL_1 ( &eqctx, 2,
  1423. page_offset, ( hermon_eq->mtt.page_offset >> 5 ) );
  1424. MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( HERMON_NUM_EQES - 1 ) );
  1425. MLX_FILL_1 ( &eqctx, 7, mtt_base_addr_l,
  1426. ( hermon_eq->mtt.mtt_base_addr >> 3 ) );
  1427. if ( ( rc = hermon_cmd_sw2hw_eq ( hermon, hermon_eq->eqn,
  1428. &eqctx ) ) != 0 ) {
  1429. DBGC ( hermon, "Hermon %p SW2HW_EQ failed: %s\n",
  1430. hermon, strerror ( rc ) );
  1431. goto err_sw2hw_eq;
  1432. }
  1433. /* Map events to this event queue */
  1434. memset ( &mask, 0, sizeof ( mask ) );
  1435. MLX_FILL_1 ( &mask, 1, port_state_change, 1 );
  1436. if ( ( rc = hermon_cmd_map_eq ( hermon,
  1437. ( HERMON_MAP_EQ | hermon_eq->eqn ),
  1438. &mask ) ) != 0 ) {
  1439. DBGC ( hermon, "Hermon %p MAP_EQ failed: %s\n",
  1440. hermon, strerror ( rc ) );
  1441. goto err_map_eq;
  1442. }
  1443. DBGC ( hermon, "Hermon %p EQN %#lx ring at [%p,%p])\n",
  1444. hermon, hermon_eq->eqn, hermon_eq->eqe,
  1445. ( ( ( void * ) hermon_eq->eqe ) + hermon_eq->eqe_size ) );
  1446. return 0;
  1447. err_map_eq:
  1448. hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn, &eqctx );
  1449. err_sw2hw_eq:
  1450. hermon_free_mtt ( hermon, &hermon_eq->mtt );
  1451. err_alloc_mtt:
  1452. free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
  1453. err_eqe:
  1454. memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
  1455. return rc;
  1456. }
  1457. /**
  1458. * Destroy event queue
  1459. *
  1460. * @v hermon Hermon device
  1461. */
  1462. static void hermon_destroy_eq ( struct hermon *hermon ) {
  1463. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1464. struct hermonprm_eqc eqctx;
  1465. struct hermonprm_event_mask mask;
  1466. int rc;
  1467. /* Unmap events from event queue */
  1468. memset ( &mask, 0, sizeof ( mask ) );
  1469. MLX_FILL_1 ( &mask, 1, port_state_change, 1 );
  1470. if ( ( rc = hermon_cmd_map_eq ( hermon,
  1471. ( HERMON_UNMAP_EQ | hermon_eq->eqn ),
  1472. &mask ) ) != 0 ) {
  1473. DBGC ( hermon, "Hermon %p FATAL MAP_EQ failed to unmap: %s\n",
  1474. hermon, strerror ( rc ) );
  1475. /* Continue; HCA may die but system should survive */
  1476. }
  1477. /* Take ownership back from hardware */
  1478. if ( ( rc = hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn,
  1479. &eqctx ) ) != 0 ) {
  1480. DBGC ( hermon, "Hermon %p FATAL HW2SW_EQ failed: %s\n",
  1481. hermon, strerror ( rc ) );
  1482. /* Leak memory and return; at least we avoid corruption */
  1483. return;
  1484. }
  1485. /* Free MTT entries */
  1486. hermon_free_mtt ( hermon, &hermon_eq->mtt );
  1487. /* Free memory */
  1488. free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
  1489. memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
  1490. }
  1491. /**
  1492. * Handle port state event
  1493. *
  1494. * @v hermon Hermon device
  1495. * @v eqe Port state change event queue entry
  1496. */
  1497. static void hermon_event_port_state_change ( struct hermon *hermon,
  1498. union hermonprm_event_entry *eqe){
  1499. unsigned int port;
  1500. int link_up;
  1501. /* Get port and link status */
  1502. port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
  1503. link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
  1504. DBGC ( hermon, "Hermon %p port %d link %s\n", hermon, ( port + 1 ),
  1505. ( link_up ? "up" : "down" ) );
  1506. /* Sanity check */
  1507. if ( port >= hermon->cap.num_ports ) {
  1508. DBGC ( hermon, "Hermon %p port %d does not exist!\n",
  1509. hermon, ( port + 1 ) );
  1510. return;
  1511. }
  1512. /* Update MAD parameters */
  1513. ib_smc_update ( hermon->ibdev[port], hermon_mad );
  1514. /* Notify Infiniband core of link state change */
  1515. ib_link_state_changed ( hermon->ibdev[port] );
  1516. }
  1517. /**
  1518. * Poll event queue
  1519. *
  1520. * @v ibdev Infiniband device
  1521. */
  1522. static void hermon_poll_eq ( struct ib_device *ibdev ) {
  1523. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1524. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1525. union hermonprm_event_entry *eqe;
  1526. union hermonprm_doorbell_register db_reg;
  1527. unsigned int eqe_idx_mask;
  1528. unsigned int event_type;
  1529. while ( 1 ) {
  1530. /* Look for event entry */
  1531. eqe_idx_mask = ( HERMON_NUM_EQES - 1 );
  1532. eqe = &hermon_eq->eqe[hermon_eq->next_idx & eqe_idx_mask];
  1533. if ( MLX_GET ( &eqe->generic, owner ) ^
  1534. ( ( hermon_eq->next_idx & HERMON_NUM_EQES ) ? 1 : 0 ) ) {
  1535. /* Entry still owned by hardware; end of poll */
  1536. break;
  1537. }
  1538. DBGCP ( hermon, "Hermon %p event:\n", hermon );
  1539. DBGCP_HD ( hermon, eqe, sizeof ( *eqe ) );
  1540. /* Handle event */
  1541. event_type = MLX_GET ( &eqe->generic, event_type );
  1542. switch ( event_type ) {
  1543. case HERMON_EV_PORT_STATE_CHANGE:
  1544. hermon_event_port_state_change ( hermon, eqe );
  1545. break;
  1546. default:
  1547. DBGC ( hermon, "Hermon %p unrecognised event type "
  1548. "%#x:\n", hermon, event_type );
  1549. DBGC_HD ( hermon, eqe, sizeof ( *eqe ) );
  1550. break;
  1551. }
  1552. /* Update event queue's index */
  1553. hermon_eq->next_idx++;
  1554. /* Ring doorbell */
  1555. MLX_FILL_1 ( &db_reg.event, 0,
  1556. ci, ( hermon_eq->next_idx & 0x00ffffffUL ) );
  1557. DBGCP ( hermon, "Ringing doorbell %08lx with %08x\n",
  1558. virt_to_phys ( hermon_eq->doorbell ),
  1559. db_reg.dword[0] );
  1560. writel ( db_reg.dword[0], hermon_eq->doorbell );
  1561. }
  1562. }
  1563. /***************************************************************************
  1564. *
  1565. * Infiniband link-layer operations
  1566. *
  1567. ***************************************************************************
  1568. */
  1569. /**
  1570. * Sense port type
  1571. *
  1572. * @v ibdev Infiniband device
  1573. * @ret port_type Port type, or negative error
  1574. */
  1575. static int hermon_sense_port_type ( struct ib_device *ibdev ) {
  1576. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1577. struct hermonprm_sense_port sense_port;
  1578. int port_type;
  1579. int rc;
  1580. /* If DPDP is not supported, always assume Infiniband */
  1581. if ( ! hermon->cap.dpdp )
  1582. return HERMON_PORT_TYPE_IB;
  1583. /* Sense the port type */
  1584. if ( ( rc = hermon_cmd_sense_port ( hermon, ibdev->port,
  1585. &sense_port ) ) != 0 ) {
  1586. DBGC ( hermon, "Hermon %p port %d sense failed: %s\n",
  1587. hermon, ibdev->port, strerror ( rc ) );
  1588. return rc;
  1589. }
  1590. port_type = MLX_GET ( &sense_port, port_type );
  1591. DBGC ( hermon, "Hermon %p port %d type %d\n",
  1592. hermon, ibdev->port, port_type );
  1593. return port_type;
  1594. }
  1595. /**
  1596. * Initialise Infiniband link
  1597. *
  1598. * @v ibdev Infiniband device
  1599. * @ret rc Return status code
  1600. */
  1601. static int hermon_open ( struct ib_device *ibdev ) {
  1602. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1603. struct hermonprm_init_port init_port;
  1604. int port_type;
  1605. int rc;
  1606. /* Check we are connected to an Infiniband network */
  1607. if ( ( rc = port_type = hermon_sense_port_type ( ibdev ) ) < 0 )
  1608. return rc;
  1609. if ( port_type != HERMON_PORT_TYPE_IB ) {
  1610. DBGC ( hermon, "Hermon %p port %d not connected to an "
  1611. "Infiniband network", hermon, ibdev->port );
  1612. return -ENOTCONN;
  1613. }
  1614. /* Init Port */
  1615. memset ( &init_port, 0, sizeof ( init_port ) );
  1616. MLX_FILL_2 ( &init_port, 0,
  1617. port_width_cap, 3,
  1618. vl_cap, 1 );
  1619. MLX_FILL_2 ( &init_port, 1,
  1620. mtu, HERMON_MTU_2048,
  1621. max_gid, 1 );
  1622. MLX_FILL_1 ( &init_port, 2, max_pkey, 64 );
  1623. if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port,
  1624. &init_port ) ) != 0 ) {
  1625. DBGC ( hermon, "Hermon %p could not intialise port: %s\n",
  1626. hermon, strerror ( rc ) );
  1627. return rc;
  1628. }
  1629. /* Update MAD parameters */
  1630. ib_smc_update ( ibdev, hermon_mad );
  1631. return 0;
  1632. }
  1633. /**
  1634. * Close Infiniband link
  1635. *
  1636. * @v ibdev Infiniband device
  1637. */
  1638. static void hermon_close ( struct ib_device *ibdev ) {
  1639. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1640. int rc;
  1641. if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
  1642. DBGC ( hermon, "Hermon %p could not close port: %s\n",
  1643. hermon, strerror ( rc ) );
  1644. /* Nothing we can do about this */
  1645. }
  1646. }
  1647. /**
  1648. * Inform embedded subnet management agent of a received MAD
  1649. *
  1650. * @v ibdev Infiniband device
  1651. * @v mad MAD
  1652. * @ret rc Return status code
  1653. */
  1654. static int hermon_inform_sma ( struct ib_device *ibdev,
  1655. union ib_mad *mad ) {
  1656. int rc;
  1657. /* Send the MAD to the embedded SMA */
  1658. if ( ( rc = hermon_mad ( ibdev, mad ) ) != 0 )
  1659. return rc;
  1660. /* Update parameters held in software */
  1661. ib_smc_update ( ibdev, hermon_mad );
  1662. return 0;
  1663. }
  1664. /***************************************************************************
  1665. *
  1666. * Multicast group operations
  1667. *
  1668. ***************************************************************************
  1669. */
  1670. /**
  1671. * Attach to multicast group
  1672. *
  1673. * @v ibdev Infiniband device
  1674. * @v qp Queue pair
  1675. * @v gid Multicast GID
  1676. * @ret rc Return status code
  1677. */
  1678. static int hermon_mcast_attach ( struct ib_device *ibdev,
  1679. struct ib_queue_pair *qp,
  1680. struct ib_gid *gid ) {
  1681. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1682. struct hermonprm_mgm_hash hash;
  1683. struct hermonprm_mcg_entry mcg;
  1684. unsigned int index;
  1685. int rc;
  1686. /* Generate hash table index */
  1687. if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
  1688. DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
  1689. hermon, strerror ( rc ) );
  1690. return rc;
  1691. }
  1692. index = MLX_GET ( &hash, hash );
  1693. /* Check for existing hash table entry */
  1694. if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
  1695. DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
  1696. hermon, index, strerror ( rc ) );
  1697. return rc;
  1698. }
  1699. if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
  1700. /* FIXME: this implementation allows only a single QP
  1701. * per multicast group, and doesn't handle hash
  1702. * collisions. Sufficient for IPoIB but may need to
  1703. * be extended in future.
  1704. */
  1705. DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
  1706. hermon, index );
  1707. return -EBUSY;
  1708. }
  1709. /* Update hash table entry */
  1710. MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
  1711. MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
  1712. memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
  1713. if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
  1714. DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
  1715. hermon, index, strerror ( rc ) );
  1716. return rc;
  1717. }
  1718. return 0;
  1719. }
  1720. /**
  1721. * Detach from multicast group
  1722. *
  1723. * @v ibdev Infiniband device
  1724. * @v qp Queue pair
  1725. * @v gid Multicast GID
  1726. */
  1727. static void hermon_mcast_detach ( struct ib_device *ibdev,
  1728. struct ib_queue_pair *qp __unused,
  1729. struct ib_gid *gid ) {
  1730. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1731. struct hermonprm_mgm_hash hash;
  1732. struct hermonprm_mcg_entry mcg;
  1733. unsigned int index;
  1734. int rc;
  1735. /* Generate hash table index */
  1736. if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
  1737. DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
  1738. hermon, strerror ( rc ) );
  1739. return;
  1740. }
  1741. index = MLX_GET ( &hash, hash );
  1742. /* Clear hash table entry */
  1743. memset ( &mcg, 0, sizeof ( mcg ) );
  1744. if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
  1745. DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
  1746. hermon, index, strerror ( rc ) );
  1747. return;
  1748. }
  1749. }
  1750. /** Hermon Infiniband operations */
  1751. static struct ib_device_operations hermon_ib_operations = {
  1752. .create_cq = hermon_create_cq,
  1753. .destroy_cq = hermon_destroy_cq,
  1754. .create_qp = hermon_create_qp,
  1755. .modify_qp = hermon_modify_qp,
  1756. .destroy_qp = hermon_destroy_qp,
  1757. .post_send = hermon_post_send,
  1758. .post_recv = hermon_post_recv,
  1759. .poll_cq = hermon_poll_cq,
  1760. .poll_eq = hermon_poll_eq,
  1761. .open = hermon_open,
  1762. .close = hermon_close,
  1763. .mcast_attach = hermon_mcast_attach,
  1764. .mcast_detach = hermon_mcast_detach,
  1765. .set_port_info = hermon_inform_sma,
  1766. .set_pkey_table = hermon_inform_sma,
  1767. };
  1768. /***************************************************************************
  1769. *
  1770. * Firmware control
  1771. *
  1772. ***************************************************************************
  1773. */
  1774. /**
  1775. * Map virtual to physical address for firmware usage
  1776. *
  1777. * @v hermon Hermon device
  1778. * @v map Mapping function
  1779. * @v va Virtual address
  1780. * @v pa Physical address
  1781. * @v len Length of region
  1782. * @ret rc Return status code
  1783. */
  1784. static int hermon_map_vpm ( struct hermon *hermon,
  1785. int ( *map ) ( struct hermon *hermon,
  1786. const struct hermonprm_virtual_physical_mapping* ),
  1787. uint64_t va, physaddr_t pa, size_t len ) {
  1788. struct hermonprm_virtual_physical_mapping mapping;
  1789. int rc;
  1790. assert ( ( va & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1791. assert ( ( pa & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1792. assert ( ( len & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1793. /* These mappings tend to generate huge volumes of
  1794. * uninteresting debug data, which basically makes it
  1795. * impossible to use debugging otherwise.
  1796. */
  1797. DBG_DISABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1798. while ( len ) {
  1799. memset ( &mapping, 0, sizeof ( mapping ) );
  1800. MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
  1801. MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
  1802. MLX_FILL_2 ( &mapping, 3,
  1803. log2size, 0,
  1804. pa_l, ( pa >> 12 ) );
  1805. if ( ( rc = map ( hermon, &mapping ) ) != 0 ) {
  1806. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1807. DBGC ( hermon, "Hermon %p could not map %llx => %lx: "
  1808. "%s\n", hermon, va, pa, strerror ( rc ) );
  1809. return rc;
  1810. }
  1811. pa += HERMON_PAGE_SIZE;
  1812. va += HERMON_PAGE_SIZE;
  1813. len -= HERMON_PAGE_SIZE;
  1814. }
  1815. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1816. return 0;
  1817. }
  1818. /**
  1819. * Start firmware running
  1820. *
  1821. * @v hermon Hermon device
  1822. * @ret rc Return status code
  1823. */
  1824. static int hermon_start_firmware ( struct hermon *hermon ) {
  1825. struct hermonprm_query_fw fw;
  1826. unsigned int fw_pages;
  1827. size_t fw_size;
  1828. physaddr_t fw_base;
  1829. int rc;
  1830. /* Get firmware parameters */
  1831. if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
  1832. DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
  1833. hermon, strerror ( rc ) );
  1834. goto err_query_fw;
  1835. }
  1836. DBGC ( hermon, "Hermon %p firmware version %d.%d.%d\n", hermon,
  1837. MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
  1838. MLX_GET ( &fw, fw_rev_subminor ) );
  1839. fw_pages = MLX_GET ( &fw, fw_pages );
  1840. DBGC ( hermon, "Hermon %p requires %d pages (%d kB) for firmware\n",
  1841. hermon, fw_pages, ( fw_pages * ( HERMON_PAGE_SIZE / 1024 ) ) );
  1842. /* Allocate firmware pages and map firmware area */
  1843. fw_size = ( fw_pages * HERMON_PAGE_SIZE );
  1844. hermon->firmware_area = umalloc ( fw_size );
  1845. if ( ! hermon->firmware_area ) {
  1846. rc = -ENOMEM;
  1847. goto err_alloc_fa;
  1848. }
  1849. fw_base = user_to_phys ( hermon->firmware_area, 0 );
  1850. DBGC ( hermon, "Hermon %p firmware area at physical [%lx,%lx)\n",
  1851. hermon, fw_base, ( fw_base + fw_size ) );
  1852. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_fa,
  1853. 0, fw_base, fw_size ) ) != 0 ) {
  1854. DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
  1855. hermon, strerror ( rc ) );
  1856. goto err_map_fa;
  1857. }
  1858. /* Start firmware */
  1859. if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
  1860. DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
  1861. hermon, strerror ( rc ) );
  1862. goto err_run_fw;
  1863. }
  1864. DBGC ( hermon, "Hermon %p firmware started\n", hermon );
  1865. return 0;
  1866. err_run_fw:
  1867. err_map_fa:
  1868. hermon_cmd_unmap_fa ( hermon );
  1869. ufree ( hermon->firmware_area );
  1870. hermon->firmware_area = UNULL;
  1871. err_alloc_fa:
  1872. err_query_fw:
  1873. return rc;
  1874. }
  1875. /**
  1876. * Stop firmware running
  1877. *
  1878. * @v hermon Hermon device
  1879. */
  1880. static void hermon_stop_firmware ( struct hermon *hermon ) {
  1881. int rc;
  1882. if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
  1883. DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
  1884. hermon, strerror ( rc ) );
  1885. /* Leak memory and return; at least we avoid corruption */
  1886. return;
  1887. }
  1888. ufree ( hermon->firmware_area );
  1889. hermon->firmware_area = UNULL;
  1890. }
  1891. /***************************************************************************
  1892. *
  1893. * Infinihost Context Memory management
  1894. *
  1895. ***************************************************************************
  1896. */
  1897. /**
  1898. * Get device limits
  1899. *
  1900. * @v hermon Hermon device
  1901. * @ret rc Return status code
  1902. */
  1903. static int hermon_get_cap ( struct hermon *hermon ) {
  1904. struct hermonprm_query_dev_cap dev_cap;
  1905. int rc;
  1906. if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
  1907. DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
  1908. hermon, strerror ( rc ) );
  1909. return rc;
  1910. }
  1911. hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
  1912. hermon->cap.reserved_qps =
  1913. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
  1914. hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
  1915. hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
  1916. hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
  1917. hermon->cap.reserved_srqs =
  1918. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
  1919. hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
  1920. hermon->cap.reserved_cqs =
  1921. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
  1922. hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
  1923. hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
  1924. hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
  1925. hermon->cap.reserved_mtts =
  1926. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
  1927. hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
  1928. hermon->cap.reserved_mrws =
  1929. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
  1930. hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
  1931. hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
  1932. hermon->cap.num_ports = MLX_GET ( &dev_cap, num_ports );
  1933. hermon->cap.dpdp = MLX_GET ( &dev_cap, dpdp );
  1934. /* Sanity check */
  1935. if ( hermon->cap.num_ports > HERMON_MAX_PORTS ) {
  1936. DBGC ( hermon, "Hermon %p has %d ports (only %d supported)\n",
  1937. hermon, hermon->cap.num_ports, HERMON_MAX_PORTS );
  1938. hermon->cap.num_ports = HERMON_MAX_PORTS;
  1939. }
  1940. return 0;
  1941. }
  1942. /**
  1943. * Get ICM usage
  1944. *
  1945. * @v log_num_entries Log2 of the number of entries
  1946. * @v entry_size Entry size
  1947. * @ret usage Usage size in ICM
  1948. */
  1949. static size_t icm_usage ( unsigned int log_num_entries, size_t entry_size ) {
  1950. size_t usage;
  1951. usage = ( ( 1 << log_num_entries ) * entry_size );
  1952. usage = ( ( usage + HERMON_PAGE_SIZE - 1 ) &
  1953. ~( HERMON_PAGE_SIZE - 1 ) );
  1954. return usage;
  1955. }
  1956. /**
  1957. * Allocate ICM
  1958. *
  1959. * @v hermon Hermon device
  1960. * @v init_hca INIT_HCA structure to fill in
  1961. * @ret rc Return status code
  1962. */
  1963. static int hermon_alloc_icm ( struct hermon *hermon,
  1964. struct hermonprm_init_hca *init_hca ) {
  1965. struct hermonprm_scalar_parameter icm_size;
  1966. struct hermonprm_scalar_parameter icm_aux_size;
  1967. uint64_t icm_offset = 0;
  1968. unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
  1969. unsigned int log_num_mtts, log_num_mpts;
  1970. size_t cmpt_max_len;
  1971. size_t qp_cmpt_len, srq_cmpt_len, cq_cmpt_len, eq_cmpt_len;
  1972. size_t icm_len, icm_aux_len;
  1973. physaddr_t icm_phys;
  1974. int i;
  1975. int rc;
  1976. /*
  1977. * Start by carving up the ICM virtual address space
  1978. *
  1979. */
  1980. /* Calculate number of each object type within ICM */
  1981. log_num_qps = fls ( hermon->cap.reserved_qps +
  1982. HERMON_RSVD_SPECIAL_QPS + HERMON_MAX_QPS - 1 );
  1983. log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
  1984. log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
  1985. log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
  1986. log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
  1987. /* ICM starts with the cMPT tables, which are sparse */
  1988. cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
  1989. ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
  1990. qp_cmpt_len = icm_usage ( log_num_qps, hermon->cap.cmpt_entry_size );
  1991. hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
  1992. hermon->icm_map[HERMON_ICM_QP_CMPT].len = qp_cmpt_len;
  1993. icm_offset += cmpt_max_len;
  1994. srq_cmpt_len = icm_usage ( log_num_srqs, hermon->cap.cmpt_entry_size );
  1995. hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
  1996. hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = srq_cmpt_len;
  1997. icm_offset += cmpt_max_len;
  1998. cq_cmpt_len = icm_usage ( log_num_cqs, hermon->cap.cmpt_entry_size );
  1999. hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
  2000. hermon->icm_map[HERMON_ICM_CQ_CMPT].len = cq_cmpt_len;
  2001. icm_offset += cmpt_max_len;
  2002. eq_cmpt_len = icm_usage ( log_num_eqs, hermon->cap.cmpt_entry_size );
  2003. hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
  2004. hermon->icm_map[HERMON_ICM_EQ_CMPT].len = eq_cmpt_len;
  2005. icm_offset += cmpt_max_len;
  2006. hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
  2007. /* Queue pair contexts */
  2008. MLX_FILL_1 ( init_hca, 12,
  2009. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
  2010. ( icm_offset >> 32 ) );
  2011. MLX_FILL_2 ( init_hca, 13,
  2012. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
  2013. ( icm_offset >> 5 ),
  2014. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
  2015. log_num_qps );
  2016. DBGC ( hermon, "Hermon %p ICM QPC base = %llx\n", hermon, icm_offset );
  2017. icm_offset += icm_usage ( log_num_qps, hermon->cap.qpc_entry_size );
  2018. /* Extended alternate path contexts */
  2019. MLX_FILL_1 ( init_hca, 24,
  2020. qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
  2021. ( icm_offset >> 32 ) );
  2022. MLX_FILL_1 ( init_hca, 25,
  2023. qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
  2024. icm_offset );
  2025. DBGC ( hermon, "Hermon %p ICM ALTC base = %llx\n", hermon, icm_offset);
  2026. icm_offset += icm_usage ( log_num_qps,
  2027. hermon->cap.altc_entry_size );
  2028. /* Extended auxiliary contexts */
  2029. MLX_FILL_1 ( init_hca, 28,
  2030. qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
  2031. ( icm_offset >> 32 ) );
  2032. MLX_FILL_1 ( init_hca, 29,
  2033. qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
  2034. icm_offset );
  2035. DBGC ( hermon, "Hermon %p ICM AUXC base = %llx\n", hermon, icm_offset);
  2036. icm_offset += icm_usage ( log_num_qps,
  2037. hermon->cap.auxc_entry_size );
  2038. /* Shared receive queue contexts */
  2039. MLX_FILL_1 ( init_hca, 18,
  2040. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
  2041. ( icm_offset >> 32 ) );
  2042. MLX_FILL_2 ( init_hca, 19,
  2043. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
  2044. ( icm_offset >> 5 ),
  2045. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
  2046. log_num_srqs );
  2047. DBGC ( hermon, "Hermon %p ICM SRQC base = %llx\n", hermon, icm_offset);
  2048. icm_offset += icm_usage ( log_num_srqs,
  2049. hermon->cap.srqc_entry_size );
  2050. /* Completion queue contexts */
  2051. MLX_FILL_1 ( init_hca, 20,
  2052. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
  2053. ( icm_offset >> 32 ) );
  2054. MLX_FILL_2 ( init_hca, 21,
  2055. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
  2056. ( icm_offset >> 5 ),
  2057. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
  2058. log_num_cqs );
  2059. DBGC ( hermon, "Hermon %p ICM CQC base = %llx\n", hermon, icm_offset );
  2060. icm_offset += icm_usage ( log_num_cqs, hermon->cap.cqc_entry_size );
  2061. /* Event queue contexts */
  2062. MLX_FILL_1 ( init_hca, 32,
  2063. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
  2064. ( icm_offset >> 32 ) );
  2065. MLX_FILL_2 ( init_hca, 33,
  2066. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
  2067. ( icm_offset >> 5 ),
  2068. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
  2069. log_num_eqs );
  2070. DBGC ( hermon, "Hermon %p ICM EQC base = %llx\n", hermon, icm_offset );
  2071. icm_offset += icm_usage ( log_num_eqs, hermon->cap.eqc_entry_size );
  2072. /* Memory translation table */
  2073. MLX_FILL_1 ( init_hca, 64,
  2074. tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
  2075. MLX_FILL_1 ( init_hca, 65,
  2076. tpt_parameters.mtt_base_addr_l, icm_offset );
  2077. DBGC ( hermon, "Hermon %p ICM MTT base = %llx\n", hermon, icm_offset );
  2078. icm_offset += icm_usage ( log_num_mtts,
  2079. hermon->cap.mtt_entry_size );
  2080. /* Memory protection table */
  2081. log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
  2082. MLX_FILL_1 ( init_hca, 60,
  2083. tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
  2084. MLX_FILL_1 ( init_hca, 61,
  2085. tpt_parameters.dmpt_base_adr_l, icm_offset );
  2086. MLX_FILL_1 ( init_hca, 62,
  2087. tpt_parameters.log_dmpt_sz, log_num_mpts );
  2088. DBGC ( hermon, "Hermon %p ICM DMPT base = %llx\n", hermon, icm_offset);
  2089. icm_offset += icm_usage ( log_num_mpts,
  2090. hermon->cap.dmpt_entry_size );
  2091. /* Multicast table */
  2092. MLX_FILL_1 ( init_hca, 48,
  2093. multicast_parameters.mc_base_addr_h,
  2094. ( icm_offset >> 32 ) );
  2095. MLX_FILL_1 ( init_hca, 49,
  2096. multicast_parameters.mc_base_addr_l, icm_offset );
  2097. MLX_FILL_1 ( init_hca, 52,
  2098. multicast_parameters.log_mc_table_entry_sz,
  2099. fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
  2100. MLX_FILL_1 ( init_hca, 53,
  2101. multicast_parameters.log_mc_table_hash_sz, 3 );
  2102. MLX_FILL_1 ( init_hca, 54,
  2103. multicast_parameters.log_mc_table_sz, 3 );
  2104. DBGC ( hermon, "Hermon %p ICM MC base = %llx\n", hermon, icm_offset );
  2105. icm_offset += ( ( 8 * sizeof ( struct hermonprm_mcg_entry ) +
  2106. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2107. hermon->icm_map[HERMON_ICM_OTHER].len =
  2108. ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
  2109. /*
  2110. * Allocate and map physical memory for (portions of) ICM
  2111. *
  2112. * Map is:
  2113. * ICM AUX area (aligned to its own size)
  2114. * cMPT areas
  2115. * Other areas
  2116. */
  2117. /* Calculate physical memory required for ICM */
  2118. icm_len = 0;
  2119. for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
  2120. icm_len += hermon->icm_map[i].len;
  2121. }
  2122. /* Get ICM auxiliary area size */
  2123. memset ( &icm_size, 0, sizeof ( icm_size ) );
  2124. MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
  2125. MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
  2126. if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
  2127. &icm_aux_size ) ) != 0 ) {
  2128. DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
  2129. hermon, strerror ( rc ) );
  2130. goto err_set_icm_size;
  2131. }
  2132. icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
  2133. /* Allocate ICM data and auxiliary area */
  2134. DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
  2135. hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
  2136. hermon->icm = umalloc ( icm_aux_len + icm_len );
  2137. if ( ! hermon->icm ) {
  2138. rc = -ENOMEM;
  2139. goto err_alloc;
  2140. }
  2141. icm_phys = user_to_phys ( hermon->icm, 0 );
  2142. /* Map ICM auxiliary area */
  2143. DBGC ( hermon, "Hermon %p mapping ICM AUX => %08lx\n",
  2144. hermon, icm_phys );
  2145. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm_aux,
  2146. 0, icm_phys, icm_aux_len ) ) != 0 ) {
  2147. DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
  2148. hermon, strerror ( rc ) );
  2149. goto err_map_icm_aux;
  2150. }
  2151. icm_phys += icm_aux_len;
  2152. /* MAP ICM area */
  2153. for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
  2154. DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx => %08lx\n",
  2155. hermon, hermon->icm_map[i].offset,
  2156. hermon->icm_map[i].len, icm_phys );
  2157. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm,
  2158. hermon->icm_map[i].offset,
  2159. icm_phys,
  2160. hermon->icm_map[i].len ) ) != 0 ){
  2161. DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
  2162. hermon, strerror ( rc ) );
  2163. goto err_map_icm;
  2164. }
  2165. icm_phys += hermon->icm_map[i].len;
  2166. }
  2167. return 0;
  2168. err_map_icm:
  2169. assert ( i == 0 ); /* We don't handle partial failure at present */
  2170. err_map_icm_aux:
  2171. hermon_cmd_unmap_icm_aux ( hermon );
  2172. ufree ( hermon->icm );
  2173. hermon->icm = UNULL;
  2174. err_alloc:
  2175. err_set_icm_size:
  2176. return rc;
  2177. }
  2178. /**
  2179. * Free ICM
  2180. *
  2181. * @v hermon Hermon device
  2182. */
  2183. static void hermon_free_icm ( struct hermon *hermon ) {
  2184. struct hermonprm_scalar_parameter unmap_icm;
  2185. int i;
  2186. for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
  2187. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2188. MLX_FILL_1 ( &unmap_icm, 0, value_hi,
  2189. ( hermon->icm_map[i].offset >> 32 ) );
  2190. MLX_FILL_1 ( &unmap_icm, 1, value,
  2191. hermon->icm_map[i].offset );
  2192. hermon_cmd_unmap_icm ( hermon,
  2193. ( 1 << fls ( ( hermon->icm_map[i].len /
  2194. HERMON_PAGE_SIZE ) - 1)),
  2195. &unmap_icm );
  2196. }
  2197. hermon_cmd_unmap_icm_aux ( hermon );
  2198. ufree ( hermon->icm );
  2199. hermon->icm = UNULL;
  2200. }
  2201. /***************************************************************************
  2202. *
  2203. * PCI interface
  2204. *
  2205. ***************************************************************************
  2206. */
  2207. /**
  2208. * Set up memory protection table
  2209. *
  2210. * @v hermon Hermon device
  2211. * @ret rc Return status code
  2212. */
  2213. static int hermon_setup_mpt ( struct hermon *hermon ) {
  2214. struct hermonprm_mpt mpt;
  2215. uint32_t key;
  2216. int rc;
  2217. /* Derive key */
  2218. key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
  2219. hermon->lkey = ( ( key << 8 ) | ( key >> 24 ) );
  2220. /* Initialise memory protection table */
  2221. memset ( &mpt, 0, sizeof ( mpt ) );
  2222. MLX_FILL_7 ( &mpt, 0,
  2223. atomic, 1,
  2224. rw, 1,
  2225. rr, 1,
  2226. lw, 1,
  2227. lr, 1,
  2228. pa, 1,
  2229. r_w, 1 );
  2230. MLX_FILL_1 ( &mpt, 2, mem_key, key );
  2231. MLX_FILL_1 ( &mpt, 3,
  2232. pd, HERMON_GLOBAL_PD );
  2233. MLX_FILL_1 ( &mpt, 10, len64, 1 );
  2234. if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
  2235. hermon->cap.reserved_mrws,
  2236. &mpt ) ) != 0 ) {
  2237. DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
  2238. hermon, strerror ( rc ) );
  2239. return rc;
  2240. }
  2241. return 0;
  2242. }
  2243. /**
  2244. * Configure special queue pairs
  2245. *
  2246. * @v hermon Hermon device
  2247. * @ret rc Return status code
  2248. */
  2249. static int hermon_configure_special_qps ( struct hermon *hermon ) {
  2250. int rc;
  2251. /* Special QP block must be aligned on its own size */
  2252. hermon->special_qpn_base = ( ( HERMON_QPN_BASE +
  2253. hermon->cap.reserved_qps +
  2254. HERMON_NUM_SPECIAL_QPS - 1 )
  2255. & ~( HERMON_NUM_SPECIAL_QPS - 1 ) );
  2256. hermon->qpn_base = ( hermon->special_qpn_base +
  2257. HERMON_NUM_SPECIAL_QPS );
  2258. DBGC ( hermon, "Hermon %p special QPs at [%lx,%lx]\n", hermon,
  2259. hermon->special_qpn_base, ( hermon->qpn_base - 1 ) );
  2260. /* Issue command to configure special QPs */
  2261. if ( ( rc = hermon_cmd_conf_special_qp ( hermon, 0x00,
  2262. hermon->special_qpn_base ) ) != 0 ) {
  2263. DBGC ( hermon, "Hermon %p could not configure special QPs: "
  2264. "%s\n", hermon, strerror ( rc ) );
  2265. return rc;
  2266. }
  2267. return 0;
  2268. }
  2269. /**
  2270. * Probe PCI device
  2271. *
  2272. * @v pci PCI device
  2273. * @v id PCI ID
  2274. * @ret rc Return status code
  2275. */
  2276. static int hermon_probe ( struct pci_device *pci,
  2277. const struct pci_device_id *id __unused ) {
  2278. struct hermon *hermon;
  2279. struct ib_device *ibdev;
  2280. struct hermonprm_init_hca init_hca;
  2281. unsigned int i;
  2282. int rc;
  2283. /* Allocate Hermon device */
  2284. hermon = zalloc ( sizeof ( *hermon ) );
  2285. if ( ! hermon ) {
  2286. rc = -ENOMEM;
  2287. goto err_alloc_hermon;
  2288. }
  2289. pci_set_drvdata ( pci, hermon );
  2290. /* Fix up PCI device */
  2291. adjust_pci_device ( pci );
  2292. /* Get PCI BARs */
  2293. hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR),
  2294. HERMON_PCI_CONFIG_BAR_SIZE );
  2295. hermon->uar = ioremap ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ),
  2296. HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE );
  2297. /* Allocate space for mailboxes */
  2298. hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
  2299. HERMON_MBOX_ALIGN );
  2300. if ( ! hermon->mailbox_in ) {
  2301. rc = -ENOMEM;
  2302. goto err_mailbox_in;
  2303. }
  2304. hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
  2305. HERMON_MBOX_ALIGN );
  2306. if ( ! hermon->mailbox_out ) {
  2307. rc = -ENOMEM;
  2308. goto err_mailbox_out;
  2309. }
  2310. /* Start firmware */
  2311. if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
  2312. goto err_start_firmware;
  2313. /* Get device limits */
  2314. if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
  2315. goto err_get_cap;
  2316. /* Allocate Infiniband devices */
  2317. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  2318. ibdev = alloc_ibdev ( 0 );
  2319. if ( ! ibdev ) {
  2320. rc = -ENOMEM;
  2321. goto err_alloc_ibdev;
  2322. }
  2323. hermon->ibdev[i] = ibdev;
  2324. ibdev->op = &hermon_ib_operations;
  2325. ibdev->dev = &pci->dev;
  2326. ibdev->port = ( HERMON_PORT_BASE + i );
  2327. ib_set_drvdata ( ibdev, hermon );
  2328. }
  2329. /* Allocate ICM */
  2330. memset ( &init_hca, 0, sizeof ( init_hca ) );
  2331. if ( ( rc = hermon_alloc_icm ( hermon, &init_hca ) ) != 0 )
  2332. goto err_alloc_icm;
  2333. /* Initialise HCA */
  2334. MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
  2335. MLX_FILL_1 ( &init_hca, 5, udp, 1 );
  2336. MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
  2337. if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
  2338. DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
  2339. hermon, strerror ( rc ) );
  2340. goto err_init_hca;
  2341. }
  2342. /* Set up memory protection */
  2343. if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
  2344. goto err_setup_mpt;
  2345. for ( i = 0 ; i < hermon->cap.num_ports ; i++ )
  2346. hermon->ibdev[i]->rdma_key = hermon->lkey;
  2347. /* Set up event queue */
  2348. if ( ( rc = hermon_create_eq ( hermon ) ) != 0 )
  2349. goto err_create_eq;
  2350. /* Configure special QPs */
  2351. if ( ( rc = hermon_configure_special_qps ( hermon ) ) != 0 )
  2352. goto err_conf_special_qps;
  2353. /* Update IPoIB MAC address */
  2354. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  2355. ib_smc_update ( hermon->ibdev[i], hermon_mad );
  2356. }
  2357. /* Register Infiniband devices */
  2358. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  2359. if ( ( rc = register_ibdev ( hermon->ibdev[i] ) ) != 0 ) {
  2360. DBGC ( hermon, "Hermon %p could not register IB "
  2361. "device: %s\n", hermon, strerror ( rc ) );
  2362. goto err_register_ibdev;
  2363. }
  2364. }
  2365. return 0;
  2366. i = hermon->cap.num_ports;
  2367. err_register_ibdev:
  2368. for ( i-- ; ( signed int ) i >= 0 ; i-- )
  2369. unregister_ibdev ( hermon->ibdev[i] );
  2370. err_conf_special_qps:
  2371. hermon_destroy_eq ( hermon );
  2372. err_create_eq:
  2373. err_setup_mpt:
  2374. hermon_cmd_close_hca ( hermon );
  2375. err_init_hca:
  2376. hermon_free_icm ( hermon );
  2377. err_alloc_icm:
  2378. i = hermon->cap.num_ports;
  2379. err_alloc_ibdev:
  2380. for ( i-- ; ( signed int ) i >= 0 ; i-- )
  2381. ibdev_put ( hermon->ibdev[i] );
  2382. err_get_cap:
  2383. hermon_stop_firmware ( hermon );
  2384. err_start_firmware:
  2385. free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
  2386. err_mailbox_out:
  2387. free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
  2388. err_mailbox_in:
  2389. free ( hermon );
  2390. err_alloc_hermon:
  2391. return rc;
  2392. }
  2393. /**
  2394. * Remove PCI device
  2395. *
  2396. * @v pci PCI device
  2397. */
  2398. static void hermon_remove ( struct pci_device *pci ) {
  2399. struct hermon *hermon = pci_get_drvdata ( pci );
  2400. int i;
  2401. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- )
  2402. unregister_ibdev ( hermon->ibdev[i] );
  2403. hermon_destroy_eq ( hermon );
  2404. hermon_cmd_close_hca ( hermon );
  2405. hermon_free_icm ( hermon );
  2406. hermon_stop_firmware ( hermon );
  2407. hermon_stop_firmware ( hermon );
  2408. free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
  2409. free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
  2410. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- )
  2411. ibdev_put ( hermon->ibdev[i] );
  2412. free ( hermon );
  2413. }
  2414. static struct pci_device_id hermon_nics[] = {
  2415. PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver", 0 ),
  2416. PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver", 0 ),
  2417. PCI_ROM ( 0x15b3, 0x6732, "mt26418", "MT26418 HCA driver", 0 ),
  2418. PCI_ROM ( 0x15b3, 0x673c, "mt26428", "MT26428 HCA driver", 0 ),
  2419. };
  2420. struct pci_driver hermon_driver __pci_driver = {
  2421. .ids = hermon_nics,
  2422. .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
  2423. .probe = hermon_probe,
  2424. .remove = hermon_remove,
  2425. };