Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

linda.c 68KB

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