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 68KB

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