You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

linda.c 67KB

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