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

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