Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

linda.c 68KB

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