linda.c 68KB

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