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.

linda.c 67KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <assert.h>
  24. #include <gpxe/io.h>
  25. #include <gpxe/pci.h>
  26. #include <gpxe/infiniband.h>
  27. #include <gpxe/i2c.h>
  28. #include <gpxe/bitbash.h>
  29. #include <gpxe/malloc.h>
  30. #include <gpxe/iobuf.h>
  31. #include "linda.h"
  32. /**
  33. * @file
  34. *
  35. * QLogic Linda Infiniband HCA
  36. *
  37. */
  38. /** A Linda send work queue */
  39. struct linda_send_work_queue {
  40. /** Send buffer usage */
  41. uint8_t *send_buf;
  42. /** Producer index */
  43. unsigned int prod;
  44. /** Consumer index */
  45. unsigned int cons;
  46. };
  47. /** A Linda receive work queue */
  48. struct linda_recv_work_queue {
  49. /** Receive header ring */
  50. void *header;
  51. /** Receive header producer offset (written by hardware) */
  52. struct QIB_7220_scalar header_prod;
  53. /** Receive header consumer offset */
  54. unsigned int header_cons;
  55. /** Offset within register space of the eager array */
  56. unsigned long eager_array;
  57. /** Number of entries in eager array */
  58. unsigned int eager_entries;
  59. /** Eager array producer index */
  60. unsigned int eager_prod;
  61. /** Eager array consumer index */
  62. unsigned int eager_cons;
  63. };
  64. /** A Linda HCA */
  65. struct linda {
  66. /** Registers */
  67. void *regs;
  68. /** In-use contexts */
  69. uint8_t used_ctx[LINDA_NUM_CONTEXTS];
  70. /** Send work queues */
  71. struct linda_send_work_queue send_wq[LINDA_NUM_CONTEXTS];
  72. /** Receive work queues */
  73. struct linda_recv_work_queue recv_wq[LINDA_NUM_CONTEXTS];
  74. /** Offset within register space of the first send buffer */
  75. unsigned long send_buffer_base;
  76. /** Send buffer availability (reported by hardware) */
  77. struct QIB_7220_SendBufAvail *sendbufavail;
  78. /** Send buffer availability (maintained by software) */
  79. uint8_t send_buf[LINDA_MAX_SEND_BUFS];
  80. /** Send buffer availability producer counter */
  81. unsigned int send_buf_prod;
  82. /** Send buffer availability consumer counter */
  83. unsigned int send_buf_cons;
  84. /** Number of reserved send buffers (across all QPs) */
  85. unsigned int reserved_send_bufs;
  86. /** I2C bit-bashing interface */
  87. struct i2c_bit_basher i2c;
  88. /** I2C serial EEPROM */
  89. struct i2c_device eeprom;
  90. };
  91. /***************************************************************************
  92. *
  93. * Linda register access
  94. *
  95. ***************************************************************************
  96. *
  97. * This card requires atomic 64-bit accesses. Strange things happen
  98. * if you try to use 32-bit accesses; sometimes they work, sometimes
  99. * they don't, sometimes you get random data.
  100. *
  101. * These accessors use the "movq" MMX instruction, and so won't work
  102. * on really old Pentiums (which won't have PCIe anyway, so this is
  103. * something of a moot point).
  104. */
  105. /**
  106. * Read Linda qword register
  107. *
  108. * @v linda Linda device
  109. * @v dwords Register buffer to read into
  110. * @v offset Register offset
  111. */
  112. static void linda_readq ( struct linda *linda, uint32_t *dwords,
  113. unsigned long offset ) {
  114. void *addr = ( linda->regs + offset );
  115. __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
  116. "movq %%mm0, (%0)\n\t"
  117. : : "r" ( dwords ), "r" ( addr ) : "memory" );
  118. DBGIO ( "[%08lx] => %08x%08x\n",
  119. virt_to_phys ( addr ), dwords[1], dwords[0] );
  120. }
  121. #define linda_readq( _linda, _ptr, _offset ) \
  122. linda_readq ( (_linda), (_ptr)->u.dwords, (_offset) )
  123. #define linda_readq_array8b( _linda, _ptr, _offset, _idx ) \
  124. linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
  125. #define linda_readq_array64k( _linda, _ptr, _offset, _idx ) \
  126. linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
  127. /**
  128. * Write Linda qword register
  129. *
  130. * @v linda Linda device
  131. * @v dwords Register buffer to write
  132. * @v offset Register offset
  133. */
  134. static void linda_writeq ( struct linda *linda, const uint32_t *dwords,
  135. unsigned long offset ) {
  136. void *addr = ( linda->regs + offset );
  137. DBGIO ( "[%08lx] <= %08x%08x\n",
  138. virt_to_phys ( addr ), dwords[1], dwords[0] );
  139. __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
  140. "movq %%mm0, (%1)\n\t"
  141. : : "r" ( dwords ), "r" ( addr ) : "memory" );
  142. }
  143. #define linda_writeq( _linda, _ptr, _offset ) \
  144. linda_writeq ( (_linda), (_ptr)->u.dwords, (_offset) )
  145. #define linda_writeq_array8b( _linda, _ptr, _offset, _idx ) \
  146. linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
  147. #define linda_writeq_array64k( _linda, _ptr, _offset, _idx ) \
  148. linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
  149. /**
  150. * Write Linda dword register
  151. *
  152. * @v linda Linda device
  153. * @v dword Value to write
  154. * @v offset Register offset
  155. */
  156. static void linda_writel ( struct linda *linda, uint32_t dword,
  157. unsigned long offset ) {
  158. writel ( dword, ( linda->regs + offset ) );
  159. }
  160. /***************************************************************************
  161. *
  162. * Link state management
  163. *
  164. ***************************************************************************
  165. */
  166. /**
  167. * Textual representation of link state
  168. *
  169. * @v link_state Link state
  170. * @ret link_text Link state text
  171. */
  172. static const char * linda_link_state_text ( unsigned int link_state ) {
  173. switch ( link_state ) {
  174. case LINDA_LINK_STATE_DOWN: return "DOWN";
  175. case LINDA_LINK_STATE_INIT: return "INIT";
  176. case LINDA_LINK_STATE_ARM: return "ARM";
  177. case LINDA_LINK_STATE_ACTIVE: return "ACTIVE";
  178. case LINDA_LINK_STATE_ACT_DEFER:return "ACT_DEFER";
  179. default: return "UNKNOWN";
  180. }
  181. }
  182. /**
  183. * Handle link state change
  184. *
  185. * @v linda Linda device
  186. */
  187. static void linda_link_state_changed ( struct ib_device *ibdev ) {
  188. struct linda *linda = ib_get_drvdata ( ibdev );
  189. struct QIB_7220_IBCStatus ibcstatus;
  190. struct QIB_7220_EXTCtrl extctrl;
  191. unsigned int link_state;
  192. unsigned int link_width;
  193. unsigned int link_speed;
  194. /* Read link state */
  195. linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
  196. link_state = BIT_GET ( &ibcstatus, LinkState );
  197. link_width = BIT_GET ( &ibcstatus, LinkWidthActive );
  198. link_speed = BIT_GET ( &ibcstatus, LinkSpeedActive );
  199. DBGC ( linda, "Linda %p link state %s (%s %s)\n", linda,
  200. linda_link_state_text ( link_state ),
  201. ( link_speed ? "DDR" : "SDR" ), ( link_width ? "x4" : "x1" ) );
  202. /* Set LEDs according to link state */
  203. linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
  204. BIT_SET ( &extctrl, LEDPriPortGreenOn,
  205. ( ( link_state >= LINDA_LINK_STATE_INIT ) ? 1 : 0 ) );
  206. BIT_SET ( &extctrl, LEDPriPortYellowOn,
  207. ( ( link_state >= LINDA_LINK_STATE_ACTIVE ) ? 1 : 0 ) );
  208. linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
  209. /* Notify Infiniband core of link state change */
  210. ibdev->port_state = ( link_state + 1 );
  211. ibdev->link_width =
  212. ( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
  213. ibdev->link_speed =
  214. ( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
  215. ib_link_state_changed ( ibdev );
  216. }
  217. /**
  218. * Set port information
  219. *
  220. * @v ibdev Infiniband device
  221. * @v port_info New port information
  222. */
  223. static int linda_set_port_info ( struct ib_device *ibdev,
  224. const struct ib_port_info *port_info ) {
  225. struct linda *linda = ib_get_drvdata ( ibdev );
  226. struct QIB_7220_IBCCtrl ibcctrl;
  227. unsigned int port_state;
  228. unsigned int link_state;
  229. /* Set new link state */
  230. port_state = ( port_info->link_speed_supported__port_state & 0xf );
  231. if ( port_state ) {
  232. link_state = ( port_state - 1 );
  233. DBGC ( linda, "Linda %p set link state to %s (%x)\n", linda,
  234. linda_link_state_text ( link_state ), link_state );
  235. linda_readq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
  236. BIT_SET ( &ibcctrl, LinkCmd, link_state );
  237. linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
  238. }
  239. /* Detect and report link state change */
  240. linda_link_state_changed ( ibdev );
  241. return 0;
  242. }
  243. /***************************************************************************
  244. *
  245. * Context allocation
  246. *
  247. ***************************************************************************
  248. */
  249. /**
  250. * Map context number to QPN
  251. *
  252. * @v ctx Context index
  253. * @ret qpn Queue pair number
  254. */
  255. static int linda_ctx_to_qpn ( unsigned int ctx ) {
  256. /* This mapping is fixed by hardware */
  257. return ( ctx * 2 );
  258. }
  259. /**
  260. * Map QPN to context number
  261. *
  262. * @v qpn Queue pair number
  263. * @ret ctx Context index
  264. */
  265. static int linda_qpn_to_ctx ( unsigned int qpn ) {
  266. /* This mapping is fixed by hardware */
  267. return ( qpn / 2 );
  268. }
  269. /**
  270. * Allocate a context
  271. *
  272. * @v linda Linda device
  273. * @ret ctx Context index, or negative error
  274. */
  275. static int linda_alloc_ctx ( struct linda *linda ) {
  276. unsigned int ctx;
  277. for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
  278. if ( ! linda->used_ctx[ctx] ) {
  279. linda->used_ctx[ctx ] = 1;
  280. DBGC2 ( linda, "Linda %p CTX %d allocated\n",
  281. linda, ctx );
  282. return ctx;
  283. }
  284. }
  285. DBGC ( linda, "Linda %p out of available contexts\n", linda );
  286. return -ENOENT;
  287. }
  288. /**
  289. * Free a context
  290. *
  291. * @v linda Linda device
  292. * @v ctx Context index
  293. */
  294. static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
  295. linda->used_ctx[ctx] = 0;
  296. DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
  297. }
  298. /***************************************************************************
  299. *
  300. * Send datapath
  301. *
  302. ***************************************************************************
  303. */
  304. /** Send buffer toggle bit
  305. *
  306. * We encode send buffers as 7 bits of send buffer index plus a single
  307. * bit which should match the "check" bit in the SendBufAvail array.
  308. */
  309. #define LINDA_SEND_BUF_TOGGLE 0x80
  310. /**
  311. * Allocate a send buffer
  312. *
  313. * @v linda Linda device
  314. * @ret send_buf Send buffer
  315. *
  316. * You must guarantee that a send buffer is available. This is done
  317. * by refusing to allocate more TX WQEs in total than the number of
  318. * available send buffers.
  319. */
  320. static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
  321. unsigned int send_buf;
  322. send_buf = linda->send_buf[linda->send_buf_cons];
  323. send_buf ^= LINDA_SEND_BUF_TOGGLE;
  324. linda->send_buf_cons = ( ( linda->send_buf_cons + 1 ) %
  325. LINDA_MAX_SEND_BUFS );
  326. return send_buf;
  327. }
  328. /**
  329. * Free a send buffer
  330. *
  331. * @v linda Linda device
  332. * @v send_buf Send buffer
  333. */
  334. static void linda_free_send_buf ( struct linda *linda,
  335. unsigned int send_buf ) {
  336. linda->send_buf[linda->send_buf_prod] = send_buf;
  337. linda->send_buf_prod = ( ( linda->send_buf_prod + 1 ) %
  338. LINDA_MAX_SEND_BUFS );
  339. }
  340. /**
  341. * Check to see if send buffer is in use
  342. *
  343. * @v linda Linda device
  344. * @v send_buf Send buffer
  345. * @ret in_use Send buffer is in use
  346. */
  347. static int linda_send_buf_in_use ( struct linda *linda,
  348. unsigned int send_buf ) {
  349. unsigned int send_idx;
  350. unsigned int send_check;
  351. unsigned int inusecheck;
  352. unsigned int inuse;
  353. unsigned int check;
  354. send_idx = ( send_buf & ~LINDA_SEND_BUF_TOGGLE );
  355. send_check = ( !! ( send_buf & LINDA_SEND_BUF_TOGGLE ) );
  356. inusecheck = BIT_GET ( linda->sendbufavail, InUseCheck[send_idx] );
  357. inuse = ( !! ( inusecheck & 0x02 ) );
  358. check = ( !! ( inusecheck & 0x01 ) );
  359. return ( inuse || ( check != send_check ) );
  360. }
  361. /**
  362. * Calculate starting offset for send buffer
  363. *
  364. * @v linda Linda device
  365. * @v send_buf Send buffer
  366. * @ret offset Starting offset
  367. */
  368. static unsigned long linda_send_buffer_offset ( struct linda *linda,
  369. unsigned int send_buf ) {
  370. return ( linda->send_buffer_base +
  371. ( ( send_buf & ~LINDA_SEND_BUF_TOGGLE ) *
  372. LINDA_SEND_BUF_SIZE ) );
  373. }
  374. /**
  375. * Create send work queue
  376. *
  377. * @v linda Linda device
  378. * @v qp Queue pair
  379. */
  380. static int linda_create_send_wq ( struct linda *linda,
  381. struct ib_queue_pair *qp ) {
  382. struct ib_work_queue *wq = &qp->send;
  383. struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  384. int rc;
  385. /* Reserve send buffers */
  386. if ( ( linda->reserved_send_bufs + qp->send.num_wqes ) >
  387. LINDA_MAX_SEND_BUFS ) {
  388. DBGC ( linda, "Linda %p out of send buffers (have %d, used "
  389. "%d, need %d)\n", linda, LINDA_MAX_SEND_BUFS,
  390. linda->reserved_send_bufs, qp->send.num_wqes );
  391. rc = -ENOBUFS;
  392. goto err_reserve_bufs;
  393. }
  394. linda->reserved_send_bufs += qp->send.num_wqes;
  395. /* Reset work queue */
  396. linda_wq->prod = 0;
  397. linda_wq->cons = 0;
  398. /* Allocate space for send buffer uasge list */
  399. linda_wq->send_buf = zalloc ( qp->send.num_wqes *
  400. sizeof ( linda_wq->send_buf[0] ) );
  401. if ( ! linda_wq->send_buf ) {
  402. rc = -ENOBUFS;
  403. goto err_alloc_send_buf;
  404. }
  405. return 0;
  406. free ( linda_wq->send_buf );
  407. err_alloc_send_buf:
  408. linda->reserved_send_bufs -= qp->send.num_wqes;
  409. err_reserve_bufs:
  410. return rc;
  411. }
  412. /**
  413. * Destroy send work queue
  414. *
  415. * @v linda Linda device
  416. * @v qp Queue pair
  417. */
  418. static void linda_destroy_send_wq ( struct linda *linda,
  419. struct ib_queue_pair *qp ) {
  420. struct ib_work_queue *wq = &qp->send;
  421. struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  422. free ( linda_wq->send_buf );
  423. linda->reserved_send_bufs -= qp->send.num_wqes;
  424. }
  425. /**
  426. * Initialise send datapath
  427. *
  428. * @v linda Linda device
  429. * @ret rc Return status code
  430. */
  431. static int linda_init_send ( struct linda *linda ) {
  432. struct QIB_7220_SendBufBase sendbufbase;
  433. struct QIB_7220_SendBufAvailAddr sendbufavailaddr;
  434. struct QIB_7220_SendCtrl sendctrl;
  435. unsigned int i;
  436. int rc;
  437. /* Retrieve SendBufBase */
  438. linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
  439. linda->send_buffer_base = BIT_GET ( &sendbufbase,
  440. BaseAddr_SmallPIO );
  441. DBGC ( linda, "Linda %p send buffers at %lx\n",
  442. linda, linda->send_buffer_base );
  443. /* Initialise the send_buf[] array */
  444. for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
  445. linda->send_buf[i] = i;
  446. /* Allocate space for the SendBufAvail array */
  447. linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
  448. LINDA_SENDBUFAVAIL_ALIGN );
  449. if ( ! linda->sendbufavail ) {
  450. rc = -ENOMEM;
  451. goto err_alloc_sendbufavail;
  452. }
  453. memset ( linda->sendbufavail, 0, sizeof ( linda->sendbufavail ) );
  454. /* Program SendBufAvailAddr into the hardware */
  455. memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
  456. BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
  457. ( virt_to_bus ( linda->sendbufavail ) >> 6 ) );
  458. linda_writeq ( linda, &sendbufavailaddr,
  459. QIB_7220_SendBufAvailAddr_offset );
  460. /* Enable sending and DMA of SendBufAvail */
  461. memset ( &sendctrl, 0, sizeof ( sendctrl ) );
  462. BIT_FILL_2 ( &sendctrl,
  463. SendBufAvailUpd, 1,
  464. SPioEnable, 1 );
  465. linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
  466. return 0;
  467. free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
  468. err_alloc_sendbufavail:
  469. return rc;
  470. }
  471. /**
  472. * Shut down send datapath
  473. *
  474. * @v linda Linda device
  475. */
  476. static void linda_fini_send ( struct linda *linda ) {
  477. struct QIB_7220_SendCtrl sendctrl;
  478. /* Disable sending and DMA of SendBufAvail */
  479. memset ( &sendctrl, 0, sizeof ( sendctrl ) );
  480. linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
  481. mb();
  482. /* Ensure hardware has seen this disable */
  483. linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
  484. free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
  485. }
  486. /***************************************************************************
  487. *
  488. * Receive datapath
  489. *
  490. ***************************************************************************
  491. */
  492. /**
  493. * Create receive work queue
  494. *
  495. * @v linda Linda device
  496. * @v qp Queue pair
  497. * @ret rc Return status code
  498. */
  499. static int linda_create_recv_wq ( struct linda *linda,
  500. struct ib_queue_pair *qp ) {
  501. struct ib_work_queue *wq = &qp->recv;
  502. struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  503. struct QIB_7220_RcvHdrAddr0 rcvhdraddr;
  504. struct QIB_7220_RcvHdrTailAddr0 rcvhdrtailaddr;
  505. struct QIB_7220_RcvHdrHead0 rcvhdrhead;
  506. struct QIB_7220_scalar rcvegrindexhead;
  507. struct QIB_7220_RcvCtrl rcvctrl;
  508. unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
  509. int rc;
  510. /* Reset context information */
  511. memset ( &linda_wq->header_prod, 0,
  512. sizeof ( linda_wq->header_prod ) );
  513. linda_wq->header_cons = 0;
  514. linda_wq->eager_prod = 0;
  515. linda_wq->eager_cons = 0;
  516. /* Allocate receive header buffer */
  517. linda_wq->header = malloc_dma ( LINDA_RECV_HEADERS_SIZE,
  518. LINDA_RECV_HEADERS_ALIGN );
  519. if ( ! linda_wq->header ) {
  520. rc = -ENOMEM;
  521. goto err_alloc_header;
  522. }
  523. /* Enable context in hardware */
  524. memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
  525. BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr0,
  526. ( virt_to_bus ( linda_wq->header ) >> 2 ) );
  527. linda_writeq_array8b ( linda, &rcvhdraddr,
  528. QIB_7220_RcvHdrAddr0_offset, ctx );
  529. memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
  530. BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr0,
  531. ( virt_to_bus ( &linda_wq->header_prod ) >> 2 ) );
  532. linda_writeq_array8b ( linda, &rcvhdrtailaddr,
  533. QIB_7220_RcvHdrTailAddr0_offset, ctx );
  534. memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
  535. BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
  536. linda_writeq_array64k ( linda, &rcvhdrhead,
  537. QIB_7220_RcvHdrHead0_offset, ctx );
  538. memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
  539. BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
  540. linda_writeq_array64k ( linda, &rcvegrindexhead,
  541. QIB_7220_RcvEgrIndexHead0_offset, ctx );
  542. linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
  543. BIT_SET ( &rcvctrl, PortEnable[ctx], 1 );
  544. BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
  545. linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
  546. DBGC ( linda, "Linda %p QPN %ld CTX %d hdrs [%lx,%lx) prod %lx\n",
  547. linda, qp->qpn, ctx, virt_to_bus ( linda_wq->header ),
  548. ( virt_to_bus ( linda_wq->header ) + LINDA_RECV_HEADERS_SIZE ),
  549. virt_to_bus ( &linda_wq->header_prod ) );
  550. return 0;
  551. free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
  552. err_alloc_header:
  553. return rc;
  554. }
  555. /**
  556. * Destroy receive work queue
  557. *
  558. * @v linda Linda device
  559. * @v qp Queue pair
  560. */
  561. static void linda_destroy_recv_wq ( struct linda *linda,
  562. struct ib_queue_pair *qp ) {
  563. struct ib_work_queue *wq = &qp->recv;
  564. struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  565. struct QIB_7220_RcvCtrl rcvctrl;
  566. unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
  567. /* Disable context in hardware */
  568. linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
  569. BIT_SET ( &rcvctrl, PortEnable[ctx], 0 );
  570. BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
  571. linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
  572. /* Make sure the hardware has seen that the context is disabled */
  573. linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
  574. mb();
  575. /* Free headers ring */
  576. free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
  577. /* Free context */
  578. linda_free_ctx ( linda, ctx );
  579. }
  580. /**
  581. * Initialise receive datapath
  582. *
  583. * @v linda Linda device
  584. * @ret rc Return status code
  585. */
  586. static int linda_init_recv ( struct linda *linda ) {
  587. struct QIB_7220_RcvCtrl rcvctrl;
  588. struct QIB_7220_scalar rcvegrbase;
  589. struct QIB_7220_scalar rcvhdrentsize;
  590. struct QIB_7220_scalar rcvhdrcnt;
  591. struct QIB_7220_RcvBTHQP rcvbthqp;
  592. unsigned int portcfg;
  593. unsigned long egrbase;
  594. unsigned int eager_array_size_0;
  595. unsigned int eager_array_size_other;
  596. unsigned int ctx;
  597. /* Select configuration based on number of contexts */
  598. switch ( LINDA_NUM_CONTEXTS ) {
  599. case 5:
  600. portcfg = LINDA_PORTCFG_5CTX;
  601. eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_5CTX_0;
  602. eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_5CTX_OTHER;
  603. break;
  604. case 9:
  605. portcfg = LINDA_PORTCFG_9CTX;
  606. eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_9CTX_0;
  607. eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_9CTX_OTHER;
  608. break;
  609. case 17:
  610. portcfg = LINDA_PORTCFG_17CTX;
  611. eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_17CTX_0;
  612. eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_17CTX_OTHER;
  613. break;
  614. default:
  615. linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
  616. return -EINVAL;
  617. }
  618. /* Configure number of contexts */
  619. memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
  620. BIT_FILL_3 ( &rcvctrl,
  621. TailUpd, 1,
  622. PortCfg, portcfg,
  623. RcvQPMapEnable, 1 );
  624. linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
  625. /* Configure receive header buffer sizes */
  626. memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
  627. BIT_FILL_1 ( &rcvhdrcnt, Value, LINDA_RECV_HEADER_COUNT );
  628. linda_writeq ( linda, &rcvhdrcnt, QIB_7220_RcvHdrCnt_offset );
  629. memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
  630. BIT_FILL_1 ( &rcvhdrentsize, Value, ( LINDA_RECV_HEADER_SIZE >> 2 ) );
  631. linda_writeq ( linda, &rcvhdrentsize, QIB_7220_RcvHdrEntSize_offset );
  632. /* Calculate eager array start addresses for each context */
  633. linda_readq ( linda, &rcvegrbase, QIB_7220_RcvEgrBase_offset );
  634. egrbase = BIT_GET ( &rcvegrbase, Value );
  635. linda->recv_wq[0].eager_array = egrbase;
  636. linda->recv_wq[0].eager_entries = eager_array_size_0;
  637. egrbase += ( eager_array_size_0 * sizeof ( struct QIB_7220_RcvEgr ) );
  638. for ( ctx = 1 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
  639. linda->recv_wq[ctx].eager_array = egrbase;
  640. linda->recv_wq[ctx].eager_entries = eager_array_size_other;
  641. egrbase += ( eager_array_size_other *
  642. sizeof ( struct QIB_7220_RcvEgr ) );
  643. }
  644. for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
  645. DBGC ( linda, "Linda %p CTX %d eager array at %lx (%d "
  646. "entries)\n", linda, ctx,
  647. linda->recv_wq[ctx].eager_array,
  648. linda->recv_wq[ctx].eager_entries );
  649. }
  650. /* Set the BTH QP for Infinipath packets to an unused value */
  651. memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
  652. BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, LINDA_QP_IDETH );
  653. linda_writeq ( linda, &rcvbthqp, QIB_7220_RcvBTHQP_offset );
  654. return 0;
  655. }
  656. /**
  657. * Shut down receive datapath
  658. *
  659. * @v linda Linda device
  660. */
  661. static void linda_fini_recv ( struct linda *linda __unused ) {
  662. /* Nothing to do; all contexts were already disabled when the
  663. * queue pairs were destroyed
  664. */
  665. }
  666. /***************************************************************************
  667. *
  668. * Completion queue operations
  669. *
  670. ***************************************************************************
  671. */
  672. /**
  673. * Create completion queue
  674. *
  675. * @v ibdev Infiniband device
  676. * @v cq Completion queue
  677. * @ret rc Return status code
  678. */
  679. static int linda_create_cq ( struct ib_device *ibdev,
  680. struct ib_completion_queue *cq ) {
  681. struct linda *linda = ib_get_drvdata ( ibdev );
  682. static int cqn;
  683. /* The hardware has no concept of completion queues. We
  684. * simply use the association between CQs and WQs (already
  685. * handled by the IB core) to decide which WQs to poll.
  686. *
  687. * We do set a CQN, just to avoid confusing debug messages
  688. * from the IB core.
  689. */
  690. cq->cqn = ++cqn;
  691. DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
  692. return 0;
  693. }
  694. /**
  695. * Destroy completion queue
  696. *
  697. * @v ibdev Infiniband device
  698. * @v cq Completion queue
  699. */
  700. static void linda_destroy_cq ( struct ib_device *ibdev,
  701. struct ib_completion_queue *cq ) {
  702. struct linda *linda = ib_get_drvdata ( ibdev );
  703. /* Nothing to do */
  704. DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
  705. }
  706. /***************************************************************************
  707. *
  708. * Queue pair operations
  709. *
  710. ***************************************************************************
  711. */
  712. /**
  713. * Create queue pair
  714. *
  715. * @v ibdev Infiniband device
  716. * @v qp Queue pair
  717. * @ret rc Return status code
  718. */
  719. static int linda_create_qp ( struct ib_device *ibdev,
  720. struct ib_queue_pair *qp ) {
  721. struct linda *linda = ib_get_drvdata ( ibdev );
  722. int ctx;
  723. int rc;
  724. /* Locate an available context */
  725. ctx = linda_alloc_ctx ( linda );
  726. if ( ctx < 0 ) {
  727. rc = ctx;
  728. goto err_alloc_ctx;
  729. }
  730. /* Set queue pair number based on context index */
  731. qp->qpn = linda_ctx_to_qpn ( ctx );
  732. /* Set work-queue private data pointers */
  733. ib_wq_set_drvdata ( &qp->send, &linda->send_wq[ctx] );
  734. ib_wq_set_drvdata ( &qp->recv, &linda->recv_wq[ctx] );
  735. /* Create receive work queue */
  736. if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
  737. goto err_create_recv_wq;
  738. /* Create send work queue */
  739. if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
  740. goto err_create_send_wq;
  741. return 0;
  742. linda_destroy_send_wq ( linda, qp );
  743. err_create_send_wq:
  744. linda_destroy_recv_wq ( linda, qp );
  745. err_create_recv_wq:
  746. linda_free_ctx ( linda, ctx );
  747. err_alloc_ctx:
  748. return rc;
  749. }
  750. /**
  751. * Modify queue pair
  752. *
  753. * @v ibdev Infiniband device
  754. * @v qp Queue pair
  755. * @v mod_list Modification list
  756. * @ret rc Return status code
  757. */
  758. static int linda_modify_qp ( struct ib_device *ibdev,
  759. struct ib_queue_pair *qp,
  760. unsigned long mod_list __unused ) {
  761. struct linda *linda = ib_get_drvdata ( ibdev );
  762. /* Nothing to do; the hardware doesn't have a notion of queue
  763. * keys
  764. */
  765. DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
  766. return 0;
  767. }
  768. /**
  769. * Destroy queue pair
  770. *
  771. * @v ibdev Infiniband device
  772. * @v qp Queue pair
  773. */
  774. static void linda_destroy_qp ( struct ib_device *ibdev,
  775. struct ib_queue_pair *qp ) {
  776. struct linda *linda = ib_get_drvdata ( ibdev );
  777. linda_destroy_send_wq ( linda, qp );
  778. linda_destroy_recv_wq ( linda, qp );
  779. }
  780. /***************************************************************************
  781. *
  782. * Work request operations
  783. *
  784. ***************************************************************************
  785. */
  786. /**
  787. * Post send work queue entry
  788. *
  789. * @v ibdev Infiniband device
  790. * @v qp Queue pair
  791. * @v av Address vector
  792. * @v iobuf I/O buffer
  793. * @ret rc Return status code
  794. */
  795. static int linda_post_send ( struct ib_device *ibdev,
  796. struct ib_queue_pair *qp,
  797. struct ib_address_vector *av,
  798. struct io_buffer *iobuf ) {
  799. struct linda *linda = ib_get_drvdata ( ibdev );
  800. struct ib_work_queue *wq = &qp->send;
  801. struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  802. struct QIB_7220_SendPbc sendpbc;
  803. uint8_t header_buf[IB_MAX_HEADER_SIZE];
  804. struct io_buffer headers;
  805. unsigned int send_buf;
  806. unsigned long start_offset;
  807. unsigned long offset;
  808. size_t len;
  809. ssize_t frag_len;
  810. uint32_t *data;
  811. /* Allocate send buffer and calculate offset */
  812. send_buf = linda_alloc_send_buf ( linda );
  813. start_offset = offset = linda_send_buffer_offset ( linda, send_buf );
  814. /* Store I/O buffer and send buffer index */
  815. assert ( wq->iobufs[linda_wq->prod] == NULL );
  816. wq->iobufs[linda_wq->prod] = iobuf;
  817. linda_wq->send_buf[linda_wq->prod] = send_buf;
  818. /* Construct headers */
  819. iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
  820. iob_reserve ( &headers, sizeof ( header_buf ) );
  821. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), av );
  822. /* Calculate packet length */
  823. len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
  824. iob_len ( iobuf ) + 3 ) & ~3 );
  825. /* Construct send per-buffer control word */
  826. memset ( &sendpbc, 0, sizeof ( sendpbc ) );
  827. BIT_FILL_2 ( &sendpbc,
  828. LengthP1_toibc, ( ( len >> 2 ) - 1 ),
  829. VL15, 1 );
  830. /* Write SendPbc */
  831. DBG_DISABLE ( DBGLVL_IO );
  832. linda_writeq ( linda, &sendpbc, offset );
  833. offset += sizeof ( sendpbc );
  834. /* Write headers */
  835. for ( data = headers.data, frag_len = iob_len ( &headers ) ;
  836. frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
  837. linda_writel ( linda, *data, offset );
  838. }
  839. /* Write data */
  840. for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
  841. frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
  842. linda_writel ( linda, *data, offset );
  843. }
  844. DBG_ENABLE ( DBGLVL_IO );
  845. assert ( ( start_offset + len ) == offset );
  846. DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) posted [%lx,%lx)\n",
  847. linda, qp->qpn, send_buf, linda_wq->prod,
  848. start_offset, offset );
  849. /* Increment producer counter */
  850. linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
  851. return 0;
  852. }
  853. /**
  854. * Complete send work queue entry
  855. *
  856. * @v ibdev Infiniband device
  857. * @v qp Queue pair
  858. * @v wqe_idx Work queue entry index
  859. */
  860. static void linda_complete_send ( struct ib_device *ibdev,
  861. struct ib_queue_pair *qp,
  862. unsigned int wqe_idx ) {
  863. struct linda *linda = ib_get_drvdata ( ibdev );
  864. struct ib_work_queue *wq = &qp->send;
  865. struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  866. struct io_buffer *iobuf;
  867. unsigned int send_buf;
  868. /* Parse completion */
  869. send_buf = linda_wq->send_buf[wqe_idx];
  870. DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) complete\n",
  871. linda, qp->qpn, send_buf, wqe_idx );
  872. /* Complete work queue entry */
  873. iobuf = wq->iobufs[wqe_idx];
  874. assert ( iobuf != NULL );
  875. ib_complete_send ( ibdev, qp, iobuf, 0 );
  876. wq->iobufs[wqe_idx] = NULL;
  877. /* Free send buffer */
  878. linda_free_send_buf ( linda, send_buf );
  879. }
  880. /**
  881. * Poll send work queue
  882. *
  883. * @v ibdev Infiniband device
  884. * @v qp Queue pair
  885. */
  886. static void linda_poll_send_wq ( struct ib_device *ibdev,
  887. struct ib_queue_pair *qp ) {
  888. struct linda *linda = ib_get_drvdata ( ibdev );
  889. struct ib_work_queue *wq = &qp->send;
  890. struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  891. unsigned int send_buf;
  892. /* Look for completions */
  893. while ( wq->fill ) {
  894. /* Check to see if send buffer has completed */
  895. send_buf = linda_wq->send_buf[linda_wq->cons];
  896. if ( linda_send_buf_in_use ( linda, send_buf ) )
  897. break;
  898. /* Complete this buffer */
  899. linda_complete_send ( ibdev, qp, linda_wq->cons );
  900. /* Increment consumer counter */
  901. linda_wq->cons = ( ( linda_wq->cons + 1 ) &
  902. ( wq->num_wqes - 1 ) );
  903. }
  904. }
  905. /**
  906. * Post receive work queue entry
  907. *
  908. * @v ibdev Infiniband device
  909. * @v qp Queue pair
  910. * @v iobuf I/O buffer
  911. * @ret rc Return status code
  912. */
  913. static int linda_post_recv ( struct ib_device *ibdev,
  914. struct ib_queue_pair *qp,
  915. struct io_buffer *iobuf ) {
  916. struct linda *linda = ib_get_drvdata ( ibdev );
  917. struct ib_work_queue *wq = &qp->recv;
  918. struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  919. struct QIB_7220_RcvEgr rcvegr;
  920. struct QIB_7220_scalar rcvegrindexhead;
  921. unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
  922. physaddr_t addr;
  923. size_t len;
  924. unsigned int wqe_idx;
  925. unsigned int bufsize;
  926. /* Sanity checks */
  927. addr = virt_to_bus ( iobuf->data );
  928. len = iob_tailroom ( iobuf );
  929. if ( addr & ( LINDA_EAGER_BUFFER_ALIGN - 1 ) ) {
  930. DBGC ( linda, "Linda %p QPN %ld misaligned RX buffer "
  931. "(%08lx)\n", linda, qp->qpn, addr );
  932. return -EINVAL;
  933. }
  934. if ( len != LINDA_RECV_PAYLOAD_SIZE ) {
  935. DBGC ( linda, "Linda %p QPN %ld wrong RX buffer size (%zd)\n",
  936. linda, qp->qpn, len );
  937. return -EINVAL;
  938. }
  939. /* Calculate eager producer index and WQE index */
  940. wqe_idx = ( linda_wq->eager_prod & ( wq->num_wqes - 1 ) );
  941. assert ( wq->iobufs[wqe_idx] == NULL );
  942. /* Store I/O buffer */
  943. wq->iobufs[wqe_idx] = iobuf;
  944. /* Calculate buffer size */
  945. switch ( LINDA_RECV_PAYLOAD_SIZE ) {
  946. case 2048: bufsize = LINDA_EAGER_BUFFER_2K; break;
  947. case 4096: bufsize = LINDA_EAGER_BUFFER_4K; break;
  948. case 8192: bufsize = LINDA_EAGER_BUFFER_8K; break;
  949. case 16384: bufsize = LINDA_EAGER_BUFFER_16K; break;
  950. case 32768: bufsize = LINDA_EAGER_BUFFER_32K; break;
  951. case 65536: bufsize = LINDA_EAGER_BUFFER_64K; break;
  952. default: linker_assert ( 0, invalid_rx_payload_size );
  953. bufsize = LINDA_EAGER_BUFFER_NONE;
  954. }
  955. /* Post eager buffer */
  956. memset ( &rcvegr, 0, sizeof ( rcvegr ) );
  957. BIT_FILL_2 ( &rcvegr,
  958. Addr, ( addr >> 11 ),
  959. BufSize, bufsize );
  960. linda_writeq_array8b ( linda, &rcvegr,
  961. linda_wq->eager_array, linda_wq->eager_prod );
  962. DBGC2 ( linda, "Linda %p QPN %ld RX egr %d(%d) posted [%lx,%lx)\n",
  963. linda, qp->qpn, linda_wq->eager_prod, wqe_idx,
  964. addr, ( addr + len ) );
  965. /* Increment producer index */
  966. linda_wq->eager_prod = ( ( linda_wq->eager_prod + 1 ) &
  967. ( linda_wq->eager_entries - 1 ) );
  968. /* Update head index */
  969. memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
  970. BIT_FILL_1 ( &rcvegrindexhead,
  971. Value, ( ( linda_wq->eager_prod + 1 ) &
  972. ( linda_wq->eager_entries - 1 ) ) );
  973. linda_writeq_array64k ( linda, &rcvegrindexhead,
  974. QIB_7220_RcvEgrIndexHead0_offset, ctx );
  975. return 0;
  976. }
  977. /**
  978. * Complete receive work queue entry
  979. *
  980. * @v ibdev Infiniband device
  981. * @v qp Queue pair
  982. * @v header_offs Header offset
  983. */
  984. static void linda_complete_recv ( struct ib_device *ibdev,
  985. struct ib_queue_pair *qp,
  986. unsigned int header_offs ) {
  987. struct linda *linda = ib_get_drvdata ( ibdev );
  988. struct ib_work_queue *wq = &qp->recv;
  989. struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  990. struct QIB_7220_RcvHdrFlags *rcvhdrflags;
  991. struct QIB_7220_RcvEgr rcvegr;
  992. struct io_buffer headers;
  993. struct io_buffer *iobuf;
  994. struct ib_queue_pair *intended_qp;
  995. struct ib_address_vector av;
  996. unsigned int rcvtype;
  997. unsigned int pktlen;
  998. unsigned int egrindex;
  999. unsigned int useegrbfr;
  1000. unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
  1001. unsigned int lenerr, parityerr, vcrcerr, icrcerr;
  1002. unsigned int err;
  1003. unsigned int hdrqoffset;
  1004. unsigned int header_len;
  1005. unsigned int padded_payload_len;
  1006. unsigned int wqe_idx;
  1007. size_t payload_len;
  1008. int qp0;
  1009. int rc;
  1010. /* RcvHdrFlags are at the end of the header entry */
  1011. rcvhdrflags = ( linda_wq->header + header_offs +
  1012. LINDA_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
  1013. rcvtype = BIT_GET ( rcvhdrflags, RcvType );
  1014. pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
  1015. egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
  1016. useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
  1017. hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
  1018. iberr = BIT_GET ( rcvhdrflags, IBErr );
  1019. mkerr = BIT_GET ( rcvhdrflags, MKErr );
  1020. tiderr = BIT_GET ( rcvhdrflags, TIDErr );
  1021. khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
  1022. mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
  1023. lenerr = BIT_GET ( rcvhdrflags, LenErr );
  1024. parityerr = BIT_GET ( rcvhdrflags, ParityErr );
  1025. vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
  1026. icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
  1027. header_len = ( LINDA_RECV_HEADER_SIZE - hdrqoffset -
  1028. sizeof ( *rcvhdrflags ) );
  1029. padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
  1030. err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
  1031. lenerr | parityerr | vcrcerr | icrcerr );
  1032. /* IB header is placed immediately before RcvHdrFlags */
  1033. iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
  1034. header_len, header_len );
  1035. /* Dump diagnostic information */
  1036. if ( err || ( ! useegrbfr ) ) {
  1037. DBGC ( linda, "Linda %p QPN %ld RX egr %d%s hdr %d type %d "
  1038. "len %d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", linda,
  1039. qp->qpn, egrindex, ( useegrbfr ? "" : "(unused)" ),
  1040. ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
  1041. pktlen, header_len, padded_payload_len,
  1042. ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
  1043. ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
  1044. ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
  1045. ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
  1046. ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
  1047. ( err ? "]" : "" ) );
  1048. } else {
  1049. DBGC2 ( linda, "Linda %p QPN %ld RX egr %d hdr %d type %d "
  1050. "len %d(%d+%d+4)\n", linda, qp->qpn, egrindex,
  1051. ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
  1052. pktlen, header_len, padded_payload_len );
  1053. }
  1054. DBGCP_HDA ( linda, hdrqoffset, headers.data,
  1055. ( header_len + sizeof ( *rcvhdrflags ) ) );
  1056. /* Parse header to generate address vector */
  1057. qp0 = ( qp->qpn == 0 );
  1058. intended_qp = NULL;
  1059. if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
  1060. &payload_len, &av ) ) != 0 ) {
  1061. DBGC ( linda, "Linda %p could not parse headers: %s\n",
  1062. linda, strerror ( rc ) );
  1063. err = 1;
  1064. }
  1065. if ( ! intended_qp )
  1066. intended_qp = qp;
  1067. /* Complete this buffer and any skipped buffers. Note that
  1068. * when the hardware runs out of buffers, it will repeatedly
  1069. * report the same buffer (the tail) as a TID error, and that
  1070. * it also has a habit of sometimes skipping over several
  1071. * buffers at once.
  1072. */
  1073. while ( 1 ) {
  1074. /* If we have caught up to the producer counter, stop.
  1075. * This will happen when the hardware first runs out
  1076. * of buffers and starts reporting TID errors against
  1077. * the eager buffer it wants to use next.
  1078. */
  1079. if ( linda_wq->eager_cons == linda_wq->eager_prod )
  1080. break;
  1081. /* If we have caught up to where we should be after
  1082. * completing this egrindex, stop. We phrase the test
  1083. * this way to avoid completing the entire ring when
  1084. * we receive the same egrindex twice in a row.
  1085. */
  1086. if ( ( linda_wq->eager_cons ==
  1087. ( ( egrindex + 1 ) & ( linda_wq->eager_entries - 1 ) )))
  1088. break;
  1089. /* Identify work queue entry and corresponding I/O
  1090. * buffer.
  1091. */
  1092. wqe_idx = ( linda_wq->eager_cons & ( wq->num_wqes - 1 ) );
  1093. iobuf = wq->iobufs[wqe_idx];
  1094. assert ( iobuf != NULL );
  1095. wq->iobufs[wqe_idx] = NULL;
  1096. /* Complete the eager buffer */
  1097. if ( linda_wq->eager_cons == egrindex ) {
  1098. /* Completing the eager buffer described in
  1099. * this header entry.
  1100. */
  1101. iob_put ( iobuf, payload_len );
  1102. rc = ( err ? -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
  1103. /* Redirect to target QP if necessary */
  1104. if ( qp != intended_qp ) {
  1105. DBGC ( linda, "Linda %p redirecting QPN %ld "
  1106. "=> %ld\n",
  1107. linda, qp->qpn, intended_qp->qpn );
  1108. /* Compensate for incorrect fill levels */
  1109. qp->recv.fill--;
  1110. intended_qp->recv.fill++;
  1111. }
  1112. ib_complete_recv ( ibdev, intended_qp, &av, iobuf, rc);
  1113. } else {
  1114. /* Completing on a skipped-over eager buffer */
  1115. ib_complete_recv ( ibdev, qp, &av, iobuf, -ECANCELED );
  1116. }
  1117. /* Clear eager buffer */
  1118. memset ( &rcvegr, 0, sizeof ( rcvegr ) );
  1119. linda_writeq_array8b ( linda, &rcvegr, linda_wq->eager_array,
  1120. linda_wq->eager_cons );
  1121. /* Increment consumer index */
  1122. linda_wq->eager_cons = ( ( linda_wq->eager_cons + 1 ) &
  1123. ( linda_wq->eager_entries - 1 ) );
  1124. }
  1125. }
  1126. /**
  1127. * Poll receive work queue
  1128. *
  1129. * @v ibdev Infiniband device
  1130. * @v qp Queue pair
  1131. */
  1132. static void linda_poll_recv_wq ( struct ib_device *ibdev,
  1133. struct ib_queue_pair *qp ) {
  1134. struct linda *linda = ib_get_drvdata ( ibdev );
  1135. struct ib_work_queue *wq = &qp->recv;
  1136. struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
  1137. struct QIB_7220_RcvHdrHead0 rcvhdrhead;
  1138. unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
  1139. unsigned int header_prod;
  1140. /* Check for received packets */
  1141. header_prod = ( BIT_GET ( &linda_wq->header_prod, Value ) << 2 );
  1142. if ( header_prod == linda_wq->header_cons )
  1143. return;
  1144. /* Process all received packets */
  1145. while ( linda_wq->header_cons != header_prod ) {
  1146. /* Complete the receive */
  1147. linda_complete_recv ( ibdev, qp, linda_wq->header_cons );
  1148. /* Increment the consumer offset */
  1149. linda_wq->header_cons += LINDA_RECV_HEADER_SIZE;
  1150. linda_wq->header_cons %= LINDA_RECV_HEADERS_SIZE;
  1151. }
  1152. /* Update consumer offset */
  1153. memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
  1154. BIT_FILL_2 ( &rcvhdrhead,
  1155. RcvHeadPointer, ( linda_wq->header_cons >> 2 ),
  1156. counter, 1 );
  1157. linda_writeq_array64k ( linda, &rcvhdrhead,
  1158. QIB_7220_RcvHdrHead0_offset, ctx );
  1159. }
  1160. /**
  1161. * Poll completion queue
  1162. *
  1163. * @v ibdev Infiniband device
  1164. * @v cq Completion queue
  1165. */
  1166. static void linda_poll_cq ( struct ib_device *ibdev,
  1167. struct ib_completion_queue *cq ) {
  1168. struct ib_work_queue *wq;
  1169. /* Poll associated send and receive queues */
  1170. list_for_each_entry ( wq, &cq->work_queues, list ) {
  1171. if ( wq->is_send ) {
  1172. linda_poll_send_wq ( ibdev, wq->qp );
  1173. } else {
  1174. linda_poll_recv_wq ( ibdev, wq->qp );
  1175. }
  1176. }
  1177. }
  1178. /***************************************************************************
  1179. *
  1180. * Event queues
  1181. *
  1182. ***************************************************************************
  1183. */
  1184. /**
  1185. * Poll event queue
  1186. *
  1187. * @v ibdev Infiniband device
  1188. */
  1189. static void linda_poll_eq ( struct ib_device *ibdev ) {
  1190. struct linda *linda = ib_get_drvdata ( ibdev );
  1191. struct QIB_7220_ErrStatus errstatus;
  1192. struct QIB_7220_ErrClear errclear;
  1193. /* Check for link status changes */
  1194. DBG_DISABLE ( DBGLVL_IO );
  1195. linda_readq ( linda, &errstatus, QIB_7220_ErrStatus_offset );
  1196. DBG_ENABLE ( DBGLVL_IO );
  1197. if ( BIT_GET ( &errstatus, IBStatusChanged ) ) {
  1198. linda_link_state_changed ( ibdev );
  1199. memset ( &errclear, 0, sizeof ( errclear ) );
  1200. BIT_FILL_1 ( &errclear, IBStatusChangedClear, 1 );
  1201. linda_writeq ( linda, &errclear, QIB_7220_ErrClear_offset );
  1202. }
  1203. }
  1204. /***************************************************************************
  1205. *
  1206. * Infiniband link-layer operations
  1207. *
  1208. ***************************************************************************
  1209. */
  1210. /**
  1211. * Initialise Infiniband link
  1212. *
  1213. * @v ibdev Infiniband device
  1214. * @ret rc Return status code
  1215. */
  1216. static int linda_open ( struct ib_device *ibdev ) {
  1217. struct linda *linda = ib_get_drvdata ( ibdev );
  1218. struct QIB_7220_Control control;
  1219. /* Disable link */
  1220. linda_readq ( linda, &control, QIB_7220_Control_offset );
  1221. BIT_SET ( &control, LinkEn, 1 );
  1222. linda_writeq ( linda, &control, QIB_7220_Control_offset );
  1223. return 0;
  1224. }
  1225. /**
  1226. * Close Infiniband link
  1227. *
  1228. * @v ibdev Infiniband device
  1229. */
  1230. static void linda_close ( struct ib_device *ibdev ) {
  1231. struct linda *linda = ib_get_drvdata ( ibdev );
  1232. struct QIB_7220_Control control;
  1233. /* Disable link */
  1234. linda_readq ( linda, &control, QIB_7220_Control_offset );
  1235. BIT_SET ( &control, LinkEn, 0 );
  1236. linda_writeq ( linda, &control, QIB_7220_Control_offset );
  1237. }
  1238. /***************************************************************************
  1239. *
  1240. * Multicast group operations
  1241. *
  1242. ***************************************************************************
  1243. */
  1244. /**
  1245. * Attach to multicast group
  1246. *
  1247. * @v ibdev Infiniband device
  1248. * @v qp Queue pair
  1249. * @v gid Multicast GID
  1250. * @ret rc Return status code
  1251. */
  1252. static int linda_mcast_attach ( struct ib_device *ibdev,
  1253. struct ib_queue_pair *qp,
  1254. struct ib_gid *gid ) {
  1255. struct linda *linda = ib_get_drvdata ( ibdev );
  1256. ( void ) linda;
  1257. ( void ) qp;
  1258. ( void ) gid;
  1259. return 0;
  1260. }
  1261. /**
  1262. * Detach from multicast group
  1263. *
  1264. * @v ibdev Infiniband device
  1265. * @v qp Queue pair
  1266. * @v gid Multicast GID
  1267. */
  1268. static void linda_mcast_detach ( struct ib_device *ibdev,
  1269. struct ib_queue_pair *qp,
  1270. struct ib_gid *gid ) {
  1271. struct linda *linda = ib_get_drvdata ( ibdev );
  1272. ( void ) linda;
  1273. ( void ) qp;
  1274. ( void ) gid;
  1275. }
  1276. /** Linda Infiniband operations */
  1277. static struct ib_device_operations linda_ib_operations = {
  1278. .create_cq = linda_create_cq,
  1279. .destroy_cq = linda_destroy_cq,
  1280. .create_qp = linda_create_qp,
  1281. .modify_qp = linda_modify_qp,
  1282. .destroy_qp = linda_destroy_qp,
  1283. .post_send = linda_post_send,
  1284. .post_recv = linda_post_recv,
  1285. .poll_cq = linda_poll_cq,
  1286. .poll_eq = linda_poll_eq,
  1287. .open = linda_open,
  1288. .close = linda_close,
  1289. .mcast_attach = linda_mcast_attach,
  1290. .mcast_detach = linda_mcast_detach,
  1291. .set_port_info = linda_set_port_info,
  1292. };
  1293. /***************************************************************************
  1294. *
  1295. * I2C bus operations
  1296. *
  1297. ***************************************************************************
  1298. */
  1299. /** Linda I2C bit to GPIO mappings */
  1300. static unsigned int linda_i2c_bits[] = {
  1301. [I2C_BIT_SCL] = ( 1 << LINDA_GPIO_SCL ),
  1302. [I2C_BIT_SDA] = ( 1 << LINDA_GPIO_SDA ),
  1303. };
  1304. /**
  1305. * Read Linda I2C line status
  1306. *
  1307. * @v basher Bit-bashing interface
  1308. * @v bit_id Bit number
  1309. * @ret zero Input is a logic 0
  1310. * @ret non-zero Input is a logic 1
  1311. */
  1312. static int linda_i2c_read_bit ( struct bit_basher *basher,
  1313. unsigned int bit_id ) {
  1314. struct linda *linda =
  1315. container_of ( basher, struct linda, i2c.basher );
  1316. struct QIB_7220_EXTStatus extstatus;
  1317. unsigned int status;
  1318. DBG_DISABLE ( DBGLVL_IO );
  1319. linda_readq ( linda, &extstatus, QIB_7220_EXTStatus_offset );
  1320. status = ( BIT_GET ( &extstatus, GPIOIn ) & linda_i2c_bits[bit_id] );
  1321. DBG_ENABLE ( DBGLVL_IO );
  1322. return status;
  1323. }
  1324. /**
  1325. * Write Linda I2C line status
  1326. *
  1327. * @v basher Bit-bashing interface
  1328. * @v bit_id Bit number
  1329. * @v data Value to write
  1330. */
  1331. static void linda_i2c_write_bit ( struct bit_basher *basher,
  1332. unsigned int bit_id, unsigned long data ) {
  1333. struct linda *linda =
  1334. container_of ( basher, struct linda, i2c.basher );
  1335. struct QIB_7220_EXTCtrl extctrl;
  1336. struct QIB_7220_GPIO gpioout;
  1337. unsigned int bit = linda_i2c_bits[bit_id];
  1338. unsigned int outputs = 0;
  1339. unsigned int output_enables = 0;
  1340. DBG_DISABLE ( DBGLVL_IO );
  1341. /* Read current GPIO mask and outputs */
  1342. linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
  1343. linda_readq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
  1344. /* Update outputs and output enables. I2C lines are tied
  1345. * high, so we always set the output to 0 and use the output
  1346. * enable to control the line.
  1347. */
  1348. output_enables = BIT_GET ( &extctrl, GPIOOe );
  1349. output_enables = ( ( output_enables & ~bit ) | ( ~data & bit ) );
  1350. outputs = BIT_GET ( &gpioout, GPIO );
  1351. outputs = ( outputs & ~bit );
  1352. BIT_SET ( &extctrl, GPIOOe, output_enables );
  1353. BIT_SET ( &gpioout, GPIO, outputs );
  1354. /* Write the output enable first; that way we avoid logic
  1355. * hazards.
  1356. */
  1357. linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
  1358. linda_writeq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
  1359. mb();
  1360. DBG_ENABLE ( DBGLVL_IO );
  1361. }
  1362. /** Linda I2C bit-bashing interface operations */
  1363. static struct bit_basher_operations linda_i2c_basher_ops = {
  1364. .read = linda_i2c_read_bit,
  1365. .write = linda_i2c_write_bit,
  1366. };
  1367. /**
  1368. * Initialise Linda I2C subsystem
  1369. *
  1370. * @v linda Linda device
  1371. * @ret rc Return status code
  1372. */
  1373. static int linda_init_i2c ( struct linda *linda ) {
  1374. static int try_eeprom_address[] = { 0x51, 0x50 };
  1375. unsigned int i;
  1376. int rc;
  1377. /* Initialise bus */
  1378. if ( ( rc = init_i2c_bit_basher ( &linda->i2c,
  1379. &linda_i2c_basher_ops ) ) != 0 ) {
  1380. DBGC ( linda, "Linda %p could not initialise I2C bus: %s\n",
  1381. linda, strerror ( rc ) );
  1382. return rc;
  1383. }
  1384. /* Probe for devices */
  1385. for ( i = 0 ; i < ( sizeof ( try_eeprom_address ) /
  1386. sizeof ( try_eeprom_address[0] ) ) ; i++ ) {
  1387. init_i2c_eeprom ( &linda->eeprom, try_eeprom_address[i] );
  1388. if ( ( rc = i2c_check_presence ( &linda->i2c.i2c,
  1389. &linda->eeprom ) ) == 0 ) {
  1390. DBGC2 ( linda, "Linda %p found EEPROM at %02x\n",
  1391. linda, try_eeprom_address[i] );
  1392. return 0;
  1393. }
  1394. }
  1395. DBGC ( linda, "Linda %p could not find EEPROM\n", linda );
  1396. return -ENODEV;
  1397. }
  1398. /**
  1399. * Read EEPROM parameters
  1400. *
  1401. * @v linda Linda device
  1402. * @v guid GUID to fill in
  1403. * @ret rc Return status code
  1404. */
  1405. static int linda_read_eeprom ( struct linda *linda,
  1406. struct ib_gid_half *guid ) {
  1407. struct i2c_interface *i2c = &linda->i2c.i2c;
  1408. int rc;
  1409. /* Read GUID */
  1410. if ( ( rc = i2c->read ( i2c, &linda->eeprom, LINDA_EEPROM_GUID_OFFSET,
  1411. guid->bytes, sizeof ( *guid ) ) ) != 0 ) {
  1412. DBGC ( linda, "Linda %p could not read GUID: %s\n",
  1413. linda, strerror ( rc ) );
  1414. return rc;
  1415. }
  1416. DBGC2 ( linda, "Linda %p has GUID %02x:%02x:%02x:%02x:%02x:%02x:"
  1417. "%02x:%02x\n", linda, guid->bytes[0], guid->bytes[1],
  1418. guid->bytes[2], guid->bytes[3], guid->bytes[4],
  1419. guid->bytes[5], guid->bytes[6], guid->bytes[7] );
  1420. /* Read serial number (debug only) */
  1421. if ( DBG_LOG ) {
  1422. uint8_t serial[LINDA_EEPROM_SERIAL_SIZE + 1];
  1423. serial[ sizeof ( serial ) - 1 ] = '\0';
  1424. if ( ( rc = i2c->read ( i2c, &linda->eeprom,
  1425. LINDA_EEPROM_SERIAL_OFFSET, serial,
  1426. ( sizeof ( serial ) - 1 ) ) ) != 0 ) {
  1427. DBGC ( linda, "Linda %p could not read serial: %s\n",
  1428. linda, strerror ( rc ) );
  1429. return rc;
  1430. }
  1431. DBGC2 ( linda, "Linda %p has serial number \"%s\"\n",
  1432. linda, serial );
  1433. }
  1434. return 0;
  1435. }
  1436. /***************************************************************************
  1437. *
  1438. * External parallel bus access
  1439. *
  1440. ***************************************************************************
  1441. */
  1442. /**
  1443. * Request ownership of the IB external parallel bus
  1444. *
  1445. * @v linda Linda device
  1446. * @ret rc Return status code
  1447. */
  1448. static int linda_ib_epb_request ( struct linda *linda ) {
  1449. struct QIB_7220_ibsd_epb_access_ctrl access;
  1450. unsigned int i;
  1451. /* Request ownership */
  1452. memset ( &access, 0, sizeof ( access ) );
  1453. BIT_FILL_1 ( &access, sw_ib_epb_req, 1 );
  1454. linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
  1455. /* Wait for ownership to be granted */
  1456. for ( i = 0 ; i < LINDA_EPB_REQUEST_MAX_WAIT_US ; i++ ) {
  1457. linda_readq ( linda, &access,
  1458. QIB_7220_ibsd_epb_access_ctrl_offset );
  1459. if ( BIT_GET ( &access, sw_ib_epb_req_granted ) )
  1460. return 0;
  1461. udelay ( 1 );
  1462. }
  1463. DBGC ( linda, "Linda %p timed out waiting for IB EPB request\n",
  1464. linda );
  1465. return -ETIMEDOUT;
  1466. }
  1467. /**
  1468. * Wait for IB external parallel bus transaction to complete
  1469. *
  1470. * @v linda Linda device
  1471. * @v xact Buffer to hold transaction result
  1472. * @ret rc Return status code
  1473. */
  1474. static int linda_ib_epb_wait ( struct linda *linda,
  1475. struct QIB_7220_ibsd_epb_transaction_reg *xact ) {
  1476. unsigned int i;
  1477. /* Discard first read to allow for signals crossing clock domains */
  1478. linda_readq ( linda, xact, QIB_7220_ibsd_epb_transaction_reg_offset );
  1479. for ( i = 0 ; i < LINDA_EPB_XACT_MAX_WAIT_US ; i++ ) {
  1480. linda_readq ( linda, xact,
  1481. QIB_7220_ibsd_epb_transaction_reg_offset );
  1482. if ( BIT_GET ( xact, ib_epb_rdy ) ) {
  1483. if ( BIT_GET ( xact, ib_epb_req_error ) ) {
  1484. DBGC ( linda, "Linda %p EPB transaction "
  1485. "failed\n", linda );
  1486. return -EIO;
  1487. } else {
  1488. return 0;
  1489. }
  1490. }
  1491. udelay ( 1 );
  1492. }
  1493. DBGC ( linda, "Linda %p timed out waiting for IB EPB transaction\n",
  1494. linda );
  1495. return -ETIMEDOUT;
  1496. }
  1497. /**
  1498. * Release ownership of the IB external parallel bus
  1499. *
  1500. * @v linda Linda device
  1501. */
  1502. static void linda_ib_epb_release ( struct linda *linda ) {
  1503. struct QIB_7220_ibsd_epb_access_ctrl access;
  1504. memset ( &access, 0, sizeof ( access ) );
  1505. BIT_FILL_1 ( &access, sw_ib_epb_req, 0 );
  1506. linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
  1507. }
  1508. /**
  1509. * Read data via IB external parallel bus
  1510. *
  1511. * @v linda Linda device
  1512. * @v location EPB location
  1513. * @ret data Data read, or negative error
  1514. *
  1515. * You must have already acquired ownership of the IB external
  1516. * parallel bus.
  1517. */
  1518. static int linda_ib_epb_read ( struct linda *linda, unsigned int location ) {
  1519. struct QIB_7220_ibsd_epb_transaction_reg xact;
  1520. unsigned int data;
  1521. int rc;
  1522. /* Ensure no transaction is currently in progress */
  1523. if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
  1524. return rc;
  1525. /* Process data */
  1526. memset ( &xact, 0, sizeof ( xact ) );
  1527. BIT_FILL_3 ( &xact,
  1528. ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
  1529. ib_epb_read_write, LINDA_EPB_READ,
  1530. ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
  1531. linda_writeq ( linda, &xact,
  1532. QIB_7220_ibsd_epb_transaction_reg_offset );
  1533. /* Wait for transaction to complete */
  1534. if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
  1535. return rc;
  1536. data = BIT_GET ( &xact, ib_epb_data );
  1537. return data;
  1538. }
  1539. /**
  1540. * Write data via IB external parallel bus
  1541. *
  1542. * @v linda Linda device
  1543. * @v location EPB location
  1544. * @v data Data to write
  1545. * @ret rc Return status code
  1546. *
  1547. * You must have already acquired ownership of the IB external
  1548. * parallel bus.
  1549. */
  1550. static int linda_ib_epb_write ( struct linda *linda, unsigned int location,
  1551. unsigned int data ) {
  1552. struct QIB_7220_ibsd_epb_transaction_reg xact;
  1553. int rc;
  1554. /* Ensure no transaction is currently in progress */
  1555. if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
  1556. return rc;
  1557. /* Process data */
  1558. memset ( &xact, 0, sizeof ( xact ) );
  1559. BIT_FILL_4 ( &xact,
  1560. ib_epb_data, data,
  1561. ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
  1562. ib_epb_read_write, LINDA_EPB_WRITE,
  1563. ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
  1564. linda_writeq ( linda, &xact,
  1565. QIB_7220_ibsd_epb_transaction_reg_offset );
  1566. /* Wait for transaction to complete */
  1567. if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
  1568. return rc;
  1569. return 0;
  1570. }
  1571. /**
  1572. * Read/modify/write EPB register
  1573. *
  1574. * @v linda Linda device
  1575. * @v cs Chip select
  1576. * @v channel Channel
  1577. * @v element Element
  1578. * @v reg Register
  1579. * @v value Value to set
  1580. * @v mask Mask to apply to old value
  1581. * @ret rc Return status code
  1582. */
  1583. static int linda_ib_epb_mod_reg ( struct linda *linda, unsigned int cs,
  1584. unsigned int channel, unsigned int element,
  1585. unsigned int reg, unsigned int value,
  1586. unsigned int mask ) {
  1587. unsigned int location;
  1588. int old_value;
  1589. int rc;
  1590. DBG_DISABLE ( DBGLVL_IO );
  1591. /* Sanity check */
  1592. assert ( ( value & mask ) == value );
  1593. /* Acquire bus ownership */
  1594. if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
  1595. goto out;
  1596. /* Read existing value, if necessary */
  1597. location = LINDA_EPB_LOC ( cs, channel, element, reg );
  1598. if ( (~mask) & 0xff ) {
  1599. old_value = linda_ib_epb_read ( linda, location );
  1600. if ( old_value < 0 ) {
  1601. rc = old_value;
  1602. goto out_release;
  1603. }
  1604. } else {
  1605. old_value = 0;
  1606. }
  1607. /* Update value */
  1608. value = ( ( old_value & ~mask ) | value );
  1609. DBGCP ( linda, "Linda %p CS %d EPB(%d,%d,%#02x) %#02x => %#02x\n",
  1610. linda, cs, channel, element, reg, old_value, value );
  1611. if ( ( rc = linda_ib_epb_write ( linda, location, value ) ) != 0 )
  1612. goto out_release;
  1613. out_release:
  1614. /* Release bus */
  1615. linda_ib_epb_release ( linda );
  1616. out:
  1617. DBG_ENABLE ( DBGLVL_IO );
  1618. return rc;
  1619. }
  1620. /**
  1621. * Transfer data to/from microcontroller RAM
  1622. *
  1623. * @v linda Linda device
  1624. * @v address Starting address
  1625. * @v write Data to write, or NULL
  1626. * @v read Data to read, or NULL
  1627. * @v len Length of data
  1628. * @ret rc Return status code
  1629. */
  1630. static int linda_ib_epb_ram_xfer ( struct linda *linda, unsigned int address,
  1631. const void *write, void *read,
  1632. size_t len ) {
  1633. unsigned int control;
  1634. unsigned int address_hi;
  1635. unsigned int address_lo;
  1636. int data;
  1637. int rc;
  1638. DBG_DISABLE ( DBGLVL_IO );
  1639. assert ( ! ( write && read ) );
  1640. assert ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
  1641. assert ( ( len % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
  1642. /* Acquire bus ownership */
  1643. if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
  1644. goto out;
  1645. /* Process data */
  1646. while ( len ) {
  1647. /* Reset the address for each new chunk */
  1648. if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
  1649. /* Write the control register */
  1650. control = ( read ? LINDA_EPB_UC_CTL_READ :
  1651. LINDA_EPB_UC_CTL_WRITE );
  1652. if ( ( rc = linda_ib_epb_write ( linda,
  1653. LINDA_EPB_UC_CTL,
  1654. control ) ) != 0 )
  1655. break;
  1656. /* Write the address registers */
  1657. address_hi = ( address >> 8 );
  1658. if ( ( rc = linda_ib_epb_write ( linda,
  1659. LINDA_EPB_UC_ADDR_HI,
  1660. address_hi ) ) != 0 )
  1661. break;
  1662. address_lo = ( address & 0xff );
  1663. if ( ( rc = linda_ib_epb_write ( linda,
  1664. LINDA_EPB_UC_ADDR_LO,
  1665. address_lo ) ) != 0 )
  1666. break;
  1667. }
  1668. /* Read or write the data */
  1669. if ( read ) {
  1670. data = linda_ib_epb_read ( linda, LINDA_EPB_UC_DATA );
  1671. if ( data < 0 ) {
  1672. rc = data;
  1673. break;
  1674. }
  1675. *( ( uint8_t * ) read++ ) = data;
  1676. } else {
  1677. data = *( ( uint8_t * ) write++ );
  1678. if ( ( rc = linda_ib_epb_write ( linda,
  1679. LINDA_EPB_UC_DATA,
  1680. data ) ) != 0 )
  1681. break;
  1682. }
  1683. address++;
  1684. len--;
  1685. /* Reset the control byte after each chunk */
  1686. if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
  1687. if ( ( rc = linda_ib_epb_write ( linda,
  1688. LINDA_EPB_UC_CTL,
  1689. 0 ) ) != 0 )
  1690. break;
  1691. }
  1692. }
  1693. /* Release bus */
  1694. linda_ib_epb_release ( linda );
  1695. out:
  1696. DBG_ENABLE ( DBGLVL_IO );
  1697. return rc;
  1698. }
  1699. /***************************************************************************
  1700. *
  1701. * Infiniband SerDes initialisation
  1702. *
  1703. ***************************************************************************
  1704. */
  1705. /** A Linda SerDes parameter */
  1706. struct linda_serdes_param {
  1707. /** EPB address as constructed by LINDA_EPB_ADDRESS() */
  1708. uint16_t address;
  1709. /** Value to set */
  1710. uint8_t value;
  1711. /** Mask to apply to old value */
  1712. uint8_t mask;
  1713. } __packed;
  1714. /** Magic "all channels" channel number */
  1715. #define LINDA_EPB_ALL_CHANNELS 31
  1716. /** End of SerDes parameter list marker */
  1717. #define LINDA_SERDES_PARAM_END { 0, 0, 0 }
  1718. /**
  1719. * Program IB SerDes register(s)
  1720. *
  1721. * @v linda Linda device
  1722. * @v param SerDes parameter
  1723. * @ret rc Return status code
  1724. */
  1725. static int linda_set_serdes_param ( struct linda *linda,
  1726. struct linda_serdes_param *param ) {
  1727. unsigned int channel;
  1728. unsigned int channel_start;
  1729. unsigned int channel_end;
  1730. unsigned int element;
  1731. unsigned int reg;
  1732. int rc;
  1733. /* Break down the EPB address and determine channels */
  1734. channel = LINDA_EPB_ADDRESS_CHANNEL ( param->address );
  1735. element = LINDA_EPB_ADDRESS_ELEMENT ( param->address );
  1736. reg = LINDA_EPB_ADDRESS_REG ( param->address );
  1737. if ( channel == LINDA_EPB_ALL_CHANNELS ) {
  1738. channel_start = 0;
  1739. channel_end = 3;
  1740. } else {
  1741. channel_start = channel_end = channel;
  1742. }
  1743. /* Modify register for each specified channel */
  1744. for ( channel = channel_start ; channel <= channel_end ; channel++ ) {
  1745. if ( ( rc = linda_ib_epb_mod_reg ( linda, LINDA_EPB_CS_SERDES,
  1746. channel, element, reg,
  1747. param->value,
  1748. param->mask ) ) != 0 )
  1749. return rc;
  1750. }
  1751. return 0;
  1752. }
  1753. /**
  1754. * Program IB SerDes registers
  1755. *
  1756. * @v linda Linda device
  1757. * @v param SerDes parameters
  1758. * @v count Number of parameters
  1759. * @ret rc Return status code
  1760. */
  1761. static int linda_set_serdes_params ( struct linda *linda,
  1762. struct linda_serdes_param *params ) {
  1763. int rc;
  1764. for ( ; params->mask != 0 ; params++ ){
  1765. if ( ( rc = linda_set_serdes_param ( linda,
  1766. params ) ) != 0 )
  1767. return rc;
  1768. }
  1769. return 0;
  1770. }
  1771. #define LINDA_DDS_VAL( amp_d, main_d, ipst_d, ipre_d, \
  1772. amp_s, main_s, ipst_s, ipre_s ) \
  1773. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x00 ), \
  1774. ( ( ( amp_d & 0x1f ) << 1 ) | 1 ), 0xff }, \
  1775. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x01 ), \
  1776. ( ( ( amp_s & 0x1f ) << 1 ) | 1 ), 0xff }, \
  1777. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x09 ), \
  1778. ( ( main_d << 3 ) | 4 | ( ipre_d >> 2 ) ), 0xff }, \
  1779. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x0a ), \
  1780. ( ( main_s << 3 ) | 4 | ( ipre_s >> 2 ) ), 0xff }, \
  1781. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x06 ), \
  1782. ( ( ( ipst_d & 0xf ) << 1 ) | \
  1783. ( ( ipre_d & 3 ) << 6 ) | 0x21 ), 0xff }, \
  1784. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x07 ), \
  1785. ( ( ( ipst_s & 0xf ) << 1 ) | \
  1786. ( ( ipre_s & 3 ) << 6) | 0x21 ), 0xff }
  1787. /**
  1788. * Linda SerDes default parameters
  1789. *
  1790. * These magic start-of-day values are taken from the Linux driver.
  1791. */
  1792. static struct linda_serdes_param linda_serdes_defaults1[] = {
  1793. /* RXHSCTRL0 */
  1794. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x00 ), 0xd4, 0xff },
  1795. /* VCDL_DAC2 */
  1796. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x05 ), 0x2d, 0xff },
  1797. /* VCDL_CTRL2 */
  1798. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x08 ), 0x03, 0x0f },
  1799. /* START_EQ1 */
  1800. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
  1801. /* START_EQ2 */
  1802. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x28 ), 0x30, 0xff },
  1803. /* BACTRL */
  1804. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0e ), 0x40, 0xff },
  1805. /* LDOUTCTRL1 */
  1806. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x04, 0xff },
  1807. /* RXHSSTATUS */
  1808. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0f ), 0x04, 0xff },
  1809. /* End of this block */
  1810. LINDA_SERDES_PARAM_END
  1811. };
  1812. static struct linda_serdes_param linda_serdes_defaults2[] = {
  1813. /* LDOUTCTRL1 */
  1814. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x00, 0xff },
  1815. /* DDS values */
  1816. LINDA_DDS_VAL ( 31, 19, 12, 0, 29, 22, 9, 0 ),
  1817. /* Set Rcv Eq. to Preset node */
  1818. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
  1819. /* DFELTHFDR */
  1820. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x08 ), 0x00, 0xff },
  1821. /* DFELTHHDR */
  1822. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x21 ), 0x00, 0xff },
  1823. /* TLTHFDR */
  1824. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x09 ), 0x02, 0xff },
  1825. /* TLTHHDR */
  1826. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x23 ), 0x02, 0xff },
  1827. /* ZFR */
  1828. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1b ), 0x0c, 0xff },
  1829. /* ZCNT) */
  1830. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1c ), 0x0c, 0xff },
  1831. /* GFR */
  1832. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1e ), 0x10, 0xff },
  1833. /* GHR */
  1834. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1f ), 0x10, 0xff },
  1835. /* VCDL_CTRL0 toggle */
  1836. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x20, 0xff },
  1837. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x00, 0xff },
  1838. /* CMUCTRL5 */
  1839. { LINDA_EPB_ADDRESS ( 7, 0, 0x15 ), 0x80, 0xff },
  1840. /* End of this block */
  1841. LINDA_SERDES_PARAM_END
  1842. };
  1843. static struct linda_serdes_param linda_serdes_defaults3[] = {
  1844. /* START_EQ1 */
  1845. { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x00, 0x38 },
  1846. /* End of this block */
  1847. LINDA_SERDES_PARAM_END
  1848. };
  1849. /**
  1850. * Program the microcontroller RAM
  1851. *
  1852. * @v linda Linda device
  1853. * @ret rc Return status code
  1854. */
  1855. static int linda_program_uc_ram ( struct linda *linda ) {
  1856. int rc;
  1857. if ( ( rc = linda_ib_epb_ram_xfer ( linda, 0, linda_ib_fw, NULL,
  1858. sizeof ( linda_ib_fw ) ) ) != 0 ){
  1859. DBGC ( linda, "Linda %p could not load IB firmware: %s\n",
  1860. linda, strerror ( rc ) );
  1861. return rc;
  1862. }
  1863. return 0;
  1864. }
  1865. /**
  1866. * Verify the microcontroller RAM
  1867. *
  1868. * @v linda Linda device
  1869. * @ret rc Return status code
  1870. */
  1871. static int linda_verify_uc_ram ( struct linda *linda ) {
  1872. uint8_t verify[LINDA_EPB_UC_CHUNK_SIZE];
  1873. unsigned int offset;
  1874. int rc;
  1875. for ( offset = 0 ; offset < sizeof ( linda_ib_fw );
  1876. offset += sizeof ( verify ) ) {
  1877. if ( ( rc = linda_ib_epb_ram_xfer ( linda, offset,
  1878. NULL, verify,
  1879. sizeof (verify) )) != 0 ){
  1880. DBGC ( linda, "Linda %p could not read back IB "
  1881. "firmware: %s\n", linda, strerror ( rc ) );
  1882. return rc;
  1883. }
  1884. if ( memcmp ( ( linda_ib_fw + offset ), verify,
  1885. sizeof ( verify ) ) != 0 ) {
  1886. DBGC ( linda, "Linda %p firmware verification failed "
  1887. "at offset %#x\n", linda, offset );
  1888. DBGC_HDA ( linda, offset, ( linda_ib_fw + offset ),
  1889. sizeof ( verify ) );
  1890. DBGC_HDA ( linda, offset, verify, sizeof ( verify ) );
  1891. return -EIO;
  1892. }
  1893. }
  1894. DBGC2 ( linda, "Linda %p firmware verified ok\n", linda );
  1895. return 0;
  1896. }
  1897. /**
  1898. * Use the microcontroller to trim the IB link
  1899. *
  1900. * @v linda Linda device
  1901. * @ret rc Return status code
  1902. */
  1903. static int linda_trim_ib ( struct linda *linda ) {
  1904. struct QIB_7220_IBSerDesCtrl ctrl;
  1905. struct QIB_7220_IntStatus intstatus;
  1906. unsigned int i;
  1907. int rc;
  1908. /* Bring the microcontroller out of reset */
  1909. linda_readq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
  1910. BIT_SET ( &ctrl, ResetIB_uC_Core, 0 );
  1911. linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
  1912. /* Wait for the "trim done" signal */
  1913. for ( i = 0 ; i < LINDA_TRIM_DONE_MAX_WAIT_MS ; i++ ) {
  1914. linda_readq ( linda, &intstatus, QIB_7220_IntStatus_offset );
  1915. if ( BIT_GET ( &intstatus, IBSerdesTrimDone ) ) {
  1916. rc = 0;
  1917. goto out_reset;
  1918. }
  1919. mdelay ( 1 );
  1920. }
  1921. DBGC ( linda, "Linda %p timed out waiting for trim done\n", linda );
  1922. rc = -ETIMEDOUT;
  1923. out_reset:
  1924. /* Put the microcontroller back into reset */
  1925. BIT_SET ( &ctrl, ResetIB_uC_Core, 1 );
  1926. linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
  1927. return rc;
  1928. }
  1929. /**
  1930. * Initialise the IB SerDes
  1931. *
  1932. * @v linda Linda device
  1933. * @ret rc Return status code
  1934. */
  1935. static int linda_init_ib_serdes ( struct linda *linda ) {
  1936. struct QIB_7220_Control control;
  1937. struct QIB_7220_IBCCtrl ibcctrl;
  1938. struct QIB_7220_IBCDDRCtrl ibcddrctrl;
  1939. struct QIB_7220_XGXSCfg xgxscfg;
  1940. int rc;
  1941. /* Disable link */
  1942. linda_readq ( linda, &control, QIB_7220_Control_offset );
  1943. BIT_SET ( &control, LinkEn, 0 );
  1944. linda_writeq ( linda, &control, QIB_7220_Control_offset );
  1945. /* Configure sensible defaults for IBC */
  1946. memset ( &ibcctrl, 0, sizeof ( ibcctrl ) );
  1947. BIT_FILL_6 ( &ibcctrl, /* Tuning values taken from Linux driver */
  1948. FlowCtrlPeriod, 0x03,
  1949. FlowCtrlWaterMark, 0x05,
  1950. MaxPktLen, ( ( LINDA_RECV_HEADER_SIZE +
  1951. LINDA_RECV_PAYLOAD_SIZE +
  1952. 4 /* ICRC */ ) >> 2 ),
  1953. PhyerrThreshold, 0xf,
  1954. OverrunThreshold, 0xf,
  1955. CreditScale, 0x4 );
  1956. linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
  1957. /* Force SDR only to avoid needing all the DDR tuning,
  1958. * Mellanox compatibiltiy hacks etc. SDR is plenty for
  1959. * boot-time operation.
  1960. */
  1961. linda_readq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
  1962. BIT_SET ( &ibcddrctrl, IB_ENHANCED_MODE, 0 );
  1963. BIT_SET ( &ibcddrctrl, SD_SPEED_SDR, 1 );
  1964. BIT_SET ( &ibcddrctrl, SD_SPEED_DDR, 0 );
  1965. BIT_SET ( &ibcddrctrl, SD_SPEED_QDR, 0 );
  1966. BIT_SET ( &ibcddrctrl, HRTBT_ENB, 0 );
  1967. BIT_SET ( &ibcddrctrl, HRTBT_AUTO, 0 );
  1968. linda_writeq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
  1969. /* Set default SerDes parameters */
  1970. if ( ( rc = linda_set_serdes_params ( linda,
  1971. linda_serdes_defaults1 ) ) != 0 )
  1972. return rc;
  1973. udelay ( 415 ); /* Magic delay while SerDes sorts itself out */
  1974. if ( ( rc = linda_set_serdes_params ( linda,
  1975. linda_serdes_defaults2 ) ) != 0 )
  1976. return rc;
  1977. /* Program the microcontroller RAM */
  1978. if ( ( rc = linda_program_uc_ram ( linda ) ) != 0 )
  1979. return rc;
  1980. /* Verify the microcontroller RAM contents */
  1981. if ( DBGLVL_LOG ) {
  1982. if ( ( rc = linda_verify_uc_ram ( linda ) ) != 0 )
  1983. return rc;
  1984. }
  1985. /* More SerDes tuning */
  1986. if ( ( rc = linda_set_serdes_params ( linda,
  1987. linda_serdes_defaults3 ) ) != 0 )
  1988. return rc;
  1989. /* Use the microcontroller to trim the IB link */
  1990. if ( ( rc = linda_trim_ib ( linda ) ) != 0 )
  1991. return rc;
  1992. /* Bring XGXS out of reset */
  1993. linda_readq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
  1994. BIT_SET ( &xgxscfg, tx_rx_reset, 0 );
  1995. BIT_SET ( &xgxscfg, xcv_reset, 0 );
  1996. linda_writeq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
  1997. return rc;
  1998. }
  1999. /***************************************************************************
  2000. *
  2001. * PCI layer interface
  2002. *
  2003. ***************************************************************************
  2004. */
  2005. /**
  2006. * Probe PCI device
  2007. *
  2008. * @v pci PCI device
  2009. * @v id PCI ID
  2010. * @ret rc Return status code
  2011. */
  2012. static int linda_probe ( struct pci_device *pci,
  2013. const struct pci_device_id *id __unused ) {
  2014. struct ib_device *ibdev;
  2015. struct linda *linda;
  2016. struct QIB_7220_Revision revision;
  2017. int rc;
  2018. /* Allocate Infiniband device */
  2019. ibdev = alloc_ibdev ( sizeof ( *linda ) );
  2020. if ( ! ibdev ) {
  2021. rc = -ENOMEM;
  2022. goto err_alloc_ibdev;
  2023. }
  2024. pci_set_drvdata ( pci, ibdev );
  2025. linda = ib_get_drvdata ( ibdev );
  2026. ibdev->op = &linda_ib_operations;
  2027. ibdev->dev = &pci->dev;
  2028. ibdev->port = 1;
  2029. /* Fix up PCI device */
  2030. adjust_pci_device ( pci );
  2031. /* Get PCI BARs */
  2032. linda->regs = ioremap ( pci->membase, LINDA_BAR0_SIZE );
  2033. DBGC2 ( linda, "Linda %p has BAR at %08lx\n", linda, pci->membase );
  2034. /* Print some general data */
  2035. linda_readq ( linda, &revision, QIB_7220_Revision_offset );
  2036. DBGC2 ( linda, "Linda %p board %02lx v%ld.%ld.%ld.%ld\n", linda,
  2037. BIT_GET ( &revision, BoardID ),
  2038. BIT_GET ( &revision, R_SW ),
  2039. BIT_GET ( &revision, R_Arch ),
  2040. BIT_GET ( &revision, R_ChipRevMajor ),
  2041. BIT_GET ( &revision, R_ChipRevMinor ) );
  2042. /* Initialise I2C subsystem */
  2043. if ( ( rc = linda_init_i2c ( linda ) ) != 0 )
  2044. goto err_init_i2c;
  2045. /* Read EEPROM parameters */
  2046. if ( ( rc = linda_read_eeprom ( linda, &ibdev->gid.u.half[1] ) ) != 0 )
  2047. goto err_read_eeprom;
  2048. /* Initialise send datapath */
  2049. if ( ( rc = linda_init_send ( linda ) ) != 0 )
  2050. goto err_init_send;
  2051. /* Initialise receive datapath */
  2052. if ( ( rc = linda_init_recv ( linda ) ) != 0 )
  2053. goto err_init_recv;
  2054. /* Initialise the IB SerDes */
  2055. if ( ( rc = linda_init_ib_serdes ( linda ) ) != 0 )
  2056. goto err_init_ib_serdes;
  2057. /* Register Infiniband device */
  2058. if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
  2059. DBGC ( linda, "Linda %p could not register IB "
  2060. "device: %s\n", linda, strerror ( rc ) );
  2061. goto err_register_ibdev;
  2062. }
  2063. return 0;
  2064. unregister_ibdev ( ibdev );
  2065. err_register_ibdev:
  2066. linda_fini_recv ( linda );
  2067. err_init_recv:
  2068. linda_fini_send ( linda );
  2069. err_init_send:
  2070. err_init_ib_serdes:
  2071. err_read_eeprom:
  2072. err_init_i2c:
  2073. ibdev_put ( ibdev );
  2074. err_alloc_ibdev:
  2075. return rc;
  2076. }
  2077. /**
  2078. * Remove PCI device
  2079. *
  2080. * @v pci PCI device
  2081. */
  2082. static void linda_remove ( struct pci_device *pci ) {
  2083. struct ib_device *ibdev = pci_get_drvdata ( pci );
  2084. struct linda *linda = ib_get_drvdata ( ibdev );
  2085. unregister_ibdev ( ibdev );
  2086. linda_fini_recv ( linda );
  2087. linda_fini_send ( linda );
  2088. ibdev_put ( ibdev );
  2089. }
  2090. static struct pci_device_id linda_nics[] = {
  2091. PCI_ROM ( 0x1077, 0x7220, "iba7220", "QLE7240/7280 HCA driver", 0 ),
  2092. };
  2093. struct pci_driver linda_driver __pci_driver = {
  2094. .ids = linda_nics,
  2095. .id_count = ( sizeof ( linda_nics ) / sizeof ( linda_nics[0] ) ),
  2096. .probe = linda_probe,
  2097. .remove = linda_remove,
  2098. };