Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

qib7322.c 72KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. /*
  2. * Copyright (C) 2009 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 <ipxe/pcibackup.h>
  33. #include "qib7322.h"
  34. /**
  35. * @file
  36. *
  37. * QLogic QIB7322 Infiniband HCA
  38. *
  39. */
  40. /** A QIB7322 send buffer set */
  41. struct qib7322_send_buffers {
  42. /** Offset within register space of the first send buffer */
  43. unsigned long base;
  44. /** Send buffer size */
  45. unsigned int size;
  46. /** Index of first send buffer */
  47. unsigned int start;
  48. /** Number of send buffers
  49. *
  50. * Must be a power of two.
  51. */
  52. unsigned int count;
  53. /** Send buffer availability producer counter */
  54. unsigned int prod;
  55. /** Send buffer availability consumer counter */
  56. unsigned int cons;
  57. /** Send buffer availability */
  58. uint16_t avail[0];
  59. };
  60. /** A QIB7322 send work queue */
  61. struct qib7322_send_work_queue {
  62. /** Send buffer set */
  63. struct qib7322_send_buffers *send_bufs;
  64. /** Send buffer usage */
  65. uint16_t *used;
  66. /** Producer index */
  67. unsigned int prod;
  68. /** Consumer index */
  69. unsigned int cons;
  70. };
  71. /** A QIB7322 receive work queue */
  72. struct qib7322_recv_work_queue {
  73. /** Receive header ring */
  74. void *header;
  75. /** Receive header producer offset (written by hardware) */
  76. struct QIB_7322_scalar header_prod;
  77. /** Receive header consumer offset */
  78. unsigned int header_cons;
  79. /** Offset within register space of the eager array */
  80. unsigned long eager_array;
  81. /** Number of entries in eager array */
  82. unsigned int eager_entries;
  83. /** Eager array producer index */
  84. unsigned int eager_prod;
  85. /** Eager array consumer index */
  86. unsigned int eager_cons;
  87. };
  88. /** A QIB7322 HCA */
  89. struct qib7322 {
  90. /** Registers */
  91. void *regs;
  92. /** In-use contexts */
  93. uint8_t used_ctx[QIB7322_NUM_CONTEXTS];
  94. /** Send work queues */
  95. struct qib7322_send_work_queue send_wq[QIB7322_NUM_CONTEXTS];
  96. /** Receive work queues */
  97. struct qib7322_recv_work_queue recv_wq[QIB7322_NUM_CONTEXTS];
  98. /** Send buffer availability (reported by hardware) */
  99. struct QIB_7322_SendBufAvail *sendbufavail;
  100. /** Small send buffers */
  101. struct qib7322_send_buffers *send_bufs_small;
  102. /** VL15 port 0 send buffers */
  103. struct qib7322_send_buffers *send_bufs_vl15_port0;
  104. /** VL15 port 1 send buffers */
  105. struct qib7322_send_buffers *send_bufs_vl15_port1;
  106. /** I2C bit-bashing interface */
  107. struct i2c_bit_basher i2c;
  108. /** I2C serial EEPROM */
  109. struct i2c_device eeprom;
  110. /** Base GUID */
  111. union ib_guid guid;
  112. /** Infiniband devices */
  113. struct ib_device *ibdev[QIB7322_MAX_PORTS];
  114. };
  115. /***************************************************************************
  116. *
  117. * QIB7322 register access
  118. *
  119. ***************************************************************************
  120. *
  121. * This card requires atomic 64-bit accesses. Strange things happen
  122. * if you try to use 32-bit accesses; sometimes they work, sometimes
  123. * they don't, sometimes you get random data.
  124. *
  125. * These accessors use the "movq" MMX instruction, and so won't work
  126. * on really old Pentiums (which won't have PCIe anyway, so this is
  127. * something of a moot point).
  128. */
  129. /**
  130. * Read QIB7322 qword register
  131. *
  132. * @v qib7322 QIB7322 device
  133. * @v dwords Register buffer to read into
  134. * @v offset Register offset
  135. */
  136. static void qib7322_readq ( struct qib7322 *qib7322, uint32_t *dwords,
  137. unsigned long offset ) {
  138. void *addr = ( qib7322->regs + offset );
  139. __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
  140. "movq %%mm0, (%0)\n\t"
  141. : : "r" ( dwords ), "r" ( addr ) : "memory" );
  142. DBGIO ( "[%08lx] => %08x%08x\n",
  143. virt_to_phys ( addr ), dwords[1], dwords[0] );
  144. }
  145. #define qib7322_readq( _qib7322, _ptr, _offset ) \
  146. qib7322_readq ( (_qib7322), (_ptr)->u.dwords, (_offset) )
  147. #define qib7322_readq_array8b( _qib7322, _ptr, _offset, _idx ) \
  148. qib7322_readq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
  149. #define qib7322_readq_array64k( _qib7322, _ptr, _offset, _idx ) \
  150. qib7322_readq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
  151. #define qib7322_readq_port( _qib7322, _ptr, _offset, _port ) \
  152. qib7322_readq ( (_qib7322), (_ptr), ( (_offset) + ( (_port) * 4096 ) ) )
  153. /**
  154. * Write QIB7322 qword register
  155. *
  156. * @v qib7322 QIB7322 device
  157. * @v dwords Register buffer to write
  158. * @v offset Register offset
  159. */
  160. static void qib7322_writeq ( struct qib7322 *qib7322, const uint32_t *dwords,
  161. unsigned long offset ) {
  162. void *addr = ( qib7322->regs + offset );
  163. DBGIO ( "[%08lx] <= %08x%08x\n",
  164. virt_to_phys ( addr ), dwords[1], dwords[0] );
  165. __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
  166. "movq %%mm0, (%1)\n\t"
  167. : : "r" ( dwords ), "r" ( addr ) : "memory" );
  168. }
  169. #define qib7322_writeq( _qib7322, _ptr, _offset ) \
  170. qib7322_writeq ( (_qib7322), (_ptr)->u.dwords, (_offset) )
  171. #define qib7322_writeq_array8b( _qib7322, _ptr, _offset, _idx ) \
  172. qib7322_writeq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
  173. #define qib7322_writeq_array64k( _qib7322, _ptr, _offset, _idx ) \
  174. qib7322_writeq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ))
  175. #define qib7322_writeq_port( _qib7322, _ptr, _offset, _port ) \
  176. qib7322_writeq ( (_qib7322), (_ptr), ( (_offset) + ( (_port) * 4096 ) ))
  177. /**
  178. * Write QIB7322 dword register
  179. *
  180. * @v qib7322 QIB7322 device
  181. * @v dword Value to write
  182. * @v offset Register offset
  183. */
  184. static void qib7322_writel ( struct qib7322 *qib7322, uint32_t dword,
  185. unsigned long offset ) {
  186. writel ( dword, ( qib7322->regs + offset ) );
  187. }
  188. /***************************************************************************
  189. *
  190. * Link state management
  191. *
  192. ***************************************************************************
  193. */
  194. /**
  195. * Textual representation of link state
  196. *
  197. * @v link_state Link state
  198. * @ret link_text Link state text
  199. */
  200. static const char * qib7322_link_state_text ( unsigned int link_state ) {
  201. switch ( link_state ) {
  202. case QIB7322_LINK_STATE_DOWN: return "DOWN";
  203. case QIB7322_LINK_STATE_INIT: return "INIT";
  204. case QIB7322_LINK_STATE_ARM: return "ARM";
  205. case QIB7322_LINK_STATE_ACTIVE: return "ACTIVE";
  206. case QIB7322_LINK_STATE_ACT_DEFER: return "ACT_DEFER";
  207. default: return "UNKNOWN";
  208. }
  209. }
  210. /**
  211. * Handle link state change
  212. *
  213. * @v qib7322 QIB7322 device
  214. */
  215. static void qib7322_link_state_changed ( struct ib_device *ibdev ) {
  216. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  217. struct QIB_7322_IBCStatusA_0 ibcstatusa;
  218. struct QIB_7322_EXTCtrl extctrl;
  219. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  220. unsigned int link_training_state;
  221. unsigned int link_state;
  222. unsigned int link_width;
  223. unsigned int link_speed;
  224. unsigned int link_speed_qdr;
  225. unsigned int green;
  226. unsigned int yellow;
  227. /* Read link state */
  228. qib7322_readq_port ( qib7322, &ibcstatusa,
  229. QIB_7322_IBCStatusA_0_offset, port );
  230. link_training_state = BIT_GET ( &ibcstatusa, LinkTrainingState );
  231. link_state = BIT_GET ( &ibcstatusa, LinkState );
  232. link_width = BIT_GET ( &ibcstatusa, LinkWidthActive );
  233. link_speed = BIT_GET ( &ibcstatusa, LinkSpeedActive );
  234. link_speed_qdr = BIT_GET ( &ibcstatusa, LinkSpeedQDR );
  235. DBGC ( qib7322, "QIB7322 %p port %d training state %#x link state %s "
  236. "(%s %s)\n", qib7322, port, link_training_state,
  237. qib7322_link_state_text ( link_state ),
  238. ( link_speed_qdr ? "QDR" : ( link_speed ? "DDR" : "SDR" ) ),
  239. ( link_width ? "x4" : "x1" ) );
  240. /* Set LEDs according to link state */
  241. qib7322_readq ( qib7322, &extctrl, QIB_7322_EXTCtrl_offset );
  242. green = ( ( link_state >= QIB7322_LINK_STATE_INIT ) ? 1 : 0 );
  243. yellow = ( ( link_state >= QIB7322_LINK_STATE_ACTIVE ) ? 1 : 0 );
  244. if ( port == 0 ) {
  245. BIT_SET ( &extctrl, LEDPort0GreenOn, green );
  246. BIT_SET ( &extctrl, LEDPort0YellowOn, yellow );
  247. } else {
  248. BIT_SET ( &extctrl, LEDPort1GreenOn, green );
  249. BIT_SET ( &extctrl, LEDPort1YellowOn, yellow );
  250. }
  251. qib7322_writeq ( qib7322, &extctrl, QIB_7322_EXTCtrl_offset );
  252. /* Notify Infiniband core of link state change */
  253. ibdev->port_state = ( link_state + 1 );
  254. ibdev->link_width_active =
  255. ( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
  256. ibdev->link_speed_active =
  257. ( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
  258. ib_link_state_changed ( ibdev );
  259. }
  260. /**
  261. * Wait for link state change to take effect
  262. *
  263. * @v ibdev Infiniband device
  264. * @v new_link_state Expected link state
  265. * @ret rc Return status code
  266. */
  267. static int qib7322_link_state_check ( struct ib_device *ibdev,
  268. unsigned int new_link_state ) {
  269. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  270. struct QIB_7322_IBCStatusA_0 ibcstatusa;
  271. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  272. unsigned int link_state;
  273. unsigned int i;
  274. for ( i = 0 ; i < QIB7322_LINK_STATE_MAX_WAIT_US ; i++ ) {
  275. qib7322_readq_port ( qib7322, &ibcstatusa,
  276. QIB_7322_IBCStatusA_0_offset, port );
  277. link_state = BIT_GET ( &ibcstatusa, LinkState );
  278. if ( link_state == new_link_state )
  279. return 0;
  280. udelay ( 1 );
  281. }
  282. DBGC ( qib7322, "QIB7322 %p port %d timed out waiting for link state "
  283. "%s\n", qib7322, port, qib7322_link_state_text ( link_state ) );
  284. return -ETIMEDOUT;
  285. }
  286. /**
  287. * Set port information
  288. *
  289. * @v ibdev Infiniband device
  290. * @v mad Set port information MAD
  291. */
  292. static int qib7322_set_port_info ( struct ib_device *ibdev,
  293. union ib_mad *mad ) {
  294. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  295. struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
  296. struct QIB_7322_IBCCtrlA_0 ibcctrla;
  297. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  298. unsigned int port_state;
  299. unsigned int link_state;
  300. /* Set new link state */
  301. port_state = ( port_info->link_speed_supported__port_state & 0xf );
  302. if ( port_state ) {
  303. link_state = ( port_state - 1 );
  304. DBGC ( qib7322, "QIB7322 %p set link state to %s (%x)\n",
  305. qib7322, qib7322_link_state_text ( link_state ),
  306. link_state );
  307. qib7322_readq_port ( qib7322, &ibcctrla,
  308. QIB_7322_IBCCtrlA_0_offset, port );
  309. BIT_SET ( &ibcctrla, LinkCmd, link_state );
  310. qib7322_writeq_port ( qib7322, &ibcctrla,
  311. QIB_7322_IBCCtrlA_0_offset, port );
  312. /* Wait for link state change to take effect. Ignore
  313. * errors; the current link state will be returned via
  314. * the GetResponse MAD.
  315. */
  316. qib7322_link_state_check ( ibdev, link_state );
  317. }
  318. /* Detect and report link state change */
  319. qib7322_link_state_changed ( ibdev );
  320. return 0;
  321. }
  322. /**
  323. * Set partition key table
  324. *
  325. * @v ibdev Infiniband device
  326. * @v mad Set partition key table MAD
  327. */
  328. static int qib7322_set_pkey_table ( struct ib_device *ibdev __unused,
  329. union ib_mad *mad __unused ) {
  330. /* Nothing to do */
  331. return 0;
  332. }
  333. /***************************************************************************
  334. *
  335. * Context allocation
  336. *
  337. ***************************************************************************
  338. */
  339. /**
  340. * Allocate a context and set queue pair number
  341. *
  342. * @v ibdev Infiniband device
  343. * @v qp Queue pair
  344. * @ret rc Return status code
  345. */
  346. static int qib7322_alloc_ctx ( struct ib_device *ibdev,
  347. struct ib_queue_pair *qp ) {
  348. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  349. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  350. unsigned int ctx;
  351. for ( ctx = port ; ctx < QIB7322_NUM_CONTEXTS ; ctx += 2 ) {
  352. if ( ! qib7322->used_ctx[ctx] ) {
  353. qib7322->used_ctx[ctx] = 1;
  354. qp->qpn = ( ctx & ~0x01 );
  355. DBGC2 ( qib7322, "QIB7322 %p port %d QPN %ld is CTX "
  356. "%d\n", qib7322, port, qp->qpn, ctx );
  357. return 0;
  358. }
  359. }
  360. DBGC ( qib7322, "QIB7322 %p port %d out of available contexts\n",
  361. qib7322, port );
  362. return -ENOENT;
  363. }
  364. /**
  365. * Get queue pair context number
  366. *
  367. * @v ibdev Infiniband device
  368. * @v qp Queue pair
  369. * @ret ctx Context index
  370. */
  371. static unsigned int qib7322_ctx ( struct ib_device *ibdev,
  372. struct ib_queue_pair *qp ) {
  373. return ( qp->qpn + ( ibdev->port - QIB7322_PORT_BASE ) );
  374. }
  375. /**
  376. * Free a context
  377. *
  378. * @v qib7322 QIB7322 device
  379. * @v ctx Context index
  380. */
  381. static void qib7322_free_ctx ( struct ib_device *ibdev,
  382. struct ib_queue_pair *qp ) {
  383. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  384. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  385. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  386. qib7322->used_ctx[ctx] = 0;
  387. DBGC2 ( qib7322, "QIB7322 %p port %d CTX %d freed\n",
  388. qib7322, port, ctx );
  389. }
  390. /***************************************************************************
  391. *
  392. * Send datapath
  393. *
  394. ***************************************************************************
  395. */
  396. /** Send buffer toggle bit
  397. *
  398. * We encode send buffers as 15 bits of send buffer index plus a
  399. * single bit which should match the "check" bit in the SendBufAvail
  400. * array.
  401. */
  402. #define QIB7322_SEND_BUF_TOGGLE 0x8000
  403. /**
  404. * Create send buffer set
  405. *
  406. * @v qib7322 QIB7322 device
  407. * @v base Send buffer base offset
  408. * @v size Send buffer size
  409. * @v start Index of first send buffer
  410. * @v count Number of send buffers
  411. * @ret send_bufs Send buffer set
  412. */
  413. static struct qib7322_send_buffers *
  414. qib7322_create_send_bufs ( struct qib7322 *qib7322, unsigned long base,
  415. unsigned int size, unsigned int start,
  416. unsigned int count ) {
  417. struct qib7322_send_buffers *send_bufs;
  418. unsigned int i;
  419. /* Allocate send buffer set */
  420. send_bufs = zalloc ( sizeof ( *send_bufs ) +
  421. ( count * sizeof ( send_bufs->avail[0] ) ) );
  422. if ( ! send_bufs )
  423. return NULL;
  424. /* Populate send buffer set */
  425. send_bufs->base = base;
  426. send_bufs->size = size;
  427. send_bufs->start = start;
  428. send_bufs->count = count;
  429. for ( i = 0 ; i < count ; i++ )
  430. send_bufs->avail[i] = ( start + i );
  431. DBGC2 ( qib7322, "QIB7322 %p send buffer set %p [%d,%d] at %lx\n",
  432. qib7322, send_bufs, start, ( start + count - 1 ),
  433. send_bufs->base );
  434. return send_bufs;
  435. }
  436. /**
  437. * Destroy send buffer set
  438. *
  439. * @v qib7322 QIB7322 device
  440. * @v send_bufs Send buffer set
  441. */
  442. static void
  443. qib7322_destroy_send_bufs ( struct qib7322 *qib7322 __unused,
  444. struct qib7322_send_buffers *send_bufs ) {
  445. free ( send_bufs );
  446. }
  447. /**
  448. * Allocate a send buffer
  449. *
  450. * @v qib7322 QIB7322 device
  451. * @v send_bufs Send buffer set
  452. * @ret send_buf Send buffer, or negative error
  453. */
  454. static int qib7322_alloc_send_buf ( struct qib7322 *qib7322,
  455. struct qib7322_send_buffers *send_bufs ) {
  456. unsigned int used;
  457. unsigned int mask;
  458. unsigned int send_buf;
  459. used = ( send_bufs->cons - send_bufs->prod );
  460. if ( used >= send_bufs->count ) {
  461. DBGC ( qib7322, "QIB7322 %p send buffer set %p out of "
  462. "buffers\n", qib7322, send_bufs );
  463. return -ENOBUFS;
  464. }
  465. mask = ( send_bufs->count - 1 );
  466. send_buf = send_bufs->avail[ send_bufs->cons++ & mask ];
  467. send_buf ^= QIB7322_SEND_BUF_TOGGLE;
  468. return send_buf;
  469. }
  470. /**
  471. * Free a send buffer
  472. *
  473. * @v qib7322 QIB7322 device
  474. * @v send_bufs Send buffer set
  475. * @v send_buf Send buffer
  476. */
  477. static void qib7322_free_send_buf ( struct qib7322 *qib7322 __unused,
  478. struct qib7322_send_buffers *send_bufs,
  479. unsigned int send_buf ) {
  480. unsigned int mask;
  481. mask = ( send_bufs->count - 1 );
  482. send_bufs->avail[ send_bufs->prod++ & mask ] = send_buf;
  483. }
  484. /**
  485. * Check to see if send buffer is in use
  486. *
  487. * @v qib7322 QIB7322 device
  488. * @v send_buf Send buffer
  489. * @ret in_use Send buffer is in use
  490. */
  491. static int qib7322_send_buf_in_use ( struct qib7322 *qib7322,
  492. unsigned int send_buf ) {
  493. unsigned int send_idx;
  494. unsigned int send_check;
  495. unsigned int inusecheck;
  496. unsigned int inuse;
  497. unsigned int check;
  498. send_idx = ( send_buf & ~QIB7322_SEND_BUF_TOGGLE );
  499. send_check = ( !! ( send_buf & QIB7322_SEND_BUF_TOGGLE ) );
  500. inusecheck = BIT_GET ( qib7322->sendbufavail, InUseCheck[send_idx] );
  501. inuse = ( !! ( inusecheck & 0x02 ) );
  502. check = ( !! ( inusecheck & 0x01 ) );
  503. return ( inuse || ( check != send_check ) );
  504. }
  505. /**
  506. * Calculate starting offset for send buffer
  507. *
  508. * @v qib7322 QIB7322 device
  509. * @v send_buf Send buffer
  510. * @ret offset Starting offset
  511. */
  512. static unsigned long
  513. qib7322_send_buffer_offset ( struct qib7322 *qib7322 __unused,
  514. struct qib7322_send_buffers *send_bufs,
  515. unsigned int send_buf ) {
  516. unsigned int index;
  517. index = ( ( send_buf & ~QIB7322_SEND_BUF_TOGGLE ) - send_bufs->start );
  518. return ( send_bufs->base + ( index * send_bufs->size ) );
  519. }
  520. /**
  521. * Create send work queue
  522. *
  523. * @v ibdev Infiniband device
  524. * @v qp Queue pair
  525. */
  526. static int qib7322_create_send_wq ( struct ib_device *ibdev,
  527. struct ib_queue_pair *qp ) {
  528. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  529. struct ib_work_queue *wq = &qp->send;
  530. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  531. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  532. /* Select send buffer set */
  533. if ( qp->type == IB_QPT_SMI ) {
  534. if ( port == 0 ) {
  535. qib7322_wq->send_bufs = qib7322->send_bufs_vl15_port0;
  536. } else {
  537. qib7322_wq->send_bufs = qib7322->send_bufs_vl15_port1;
  538. }
  539. } else {
  540. qib7322_wq->send_bufs = qib7322->send_bufs_small;
  541. }
  542. /* Allocate space for send buffer usage list */
  543. qib7322_wq->used = zalloc ( qp->send.num_wqes *
  544. sizeof ( qib7322_wq->used[0] ) );
  545. if ( ! qib7322_wq->used )
  546. return -ENOMEM;
  547. /* Reset work queue */
  548. qib7322_wq->prod = 0;
  549. qib7322_wq->cons = 0;
  550. return 0;
  551. }
  552. /**
  553. * Destroy send work queue
  554. *
  555. * @v ibdev Infiniband device
  556. * @v qp Queue pair
  557. */
  558. static void qib7322_destroy_send_wq ( struct ib_device *ibdev __unused,
  559. struct ib_queue_pair *qp ) {
  560. struct ib_work_queue *wq = &qp->send;
  561. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  562. free ( qib7322_wq->used );
  563. }
  564. /**
  565. * Initialise send datapath
  566. *
  567. * @v qib7322 QIB7322 device
  568. * @ret rc Return status code
  569. */
  570. static int qib7322_init_send ( struct qib7322 *qib7322 ) {
  571. struct QIB_7322_SendBufBase sendbufbase;
  572. struct QIB_7322_SendBufAvailAddr sendbufavailaddr;
  573. struct QIB_7322_SendCtrl sendctrl;
  574. struct QIB_7322_SendCtrl_0 sendctrlp;
  575. unsigned long baseaddr_smallpio;
  576. unsigned long baseaddr_largepio;
  577. unsigned long baseaddr_vl15_port0;
  578. unsigned long baseaddr_vl15_port1;
  579. int rc;
  580. /* Create send buffer sets */
  581. qib7322_readq ( qib7322, &sendbufbase, QIB_7322_SendBufBase_offset );
  582. baseaddr_smallpio = BIT_GET ( &sendbufbase, BaseAddr_SmallPIO );
  583. baseaddr_largepio = BIT_GET ( &sendbufbase, BaseAddr_LargePIO );
  584. baseaddr_vl15_port0 = ( baseaddr_largepio +
  585. ( QIB7322_LARGE_SEND_BUF_SIZE *
  586. QIB7322_LARGE_SEND_BUF_COUNT ) );
  587. baseaddr_vl15_port1 = ( baseaddr_vl15_port0 +
  588. QIB7322_VL15_PORT0_SEND_BUF_SIZE );
  589. qib7322->send_bufs_small =
  590. qib7322_create_send_bufs ( qib7322, baseaddr_smallpio,
  591. QIB7322_SMALL_SEND_BUF_SIZE,
  592. QIB7322_SMALL_SEND_BUF_START,
  593. QIB7322_SMALL_SEND_BUF_USED );
  594. if ( ! qib7322->send_bufs_small ) {
  595. rc = -ENOMEM;
  596. goto err_create_send_bufs_small;
  597. }
  598. qib7322->send_bufs_vl15_port0 =
  599. qib7322_create_send_bufs ( qib7322, baseaddr_vl15_port0,
  600. QIB7322_VL15_PORT0_SEND_BUF_SIZE,
  601. QIB7322_VL15_PORT0_SEND_BUF_START,
  602. QIB7322_VL15_PORT0_SEND_BUF_COUNT );
  603. if ( ! qib7322->send_bufs_vl15_port0 ) {
  604. rc = -ENOMEM;
  605. goto err_create_send_bufs_vl15_port0;
  606. }
  607. qib7322->send_bufs_vl15_port1 =
  608. qib7322_create_send_bufs ( qib7322, baseaddr_vl15_port1,
  609. QIB7322_VL15_PORT1_SEND_BUF_SIZE,
  610. QIB7322_VL15_PORT1_SEND_BUF_START,
  611. QIB7322_VL15_PORT1_SEND_BUF_COUNT );
  612. if ( ! qib7322->send_bufs_vl15_port1 ) {
  613. rc = -ENOMEM;
  614. goto err_create_send_bufs_vl15_port1;
  615. }
  616. /* Allocate space for the SendBufAvail array */
  617. qib7322->sendbufavail = malloc_dma ( sizeof ( *qib7322->sendbufavail ),
  618. QIB7322_SENDBUFAVAIL_ALIGN );
  619. if ( ! qib7322->sendbufavail ) {
  620. rc = -ENOMEM;
  621. goto err_alloc_sendbufavail;
  622. }
  623. memset ( qib7322->sendbufavail, 0, sizeof ( qib7322->sendbufavail ) );
  624. /* Program SendBufAvailAddr into the hardware */
  625. memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
  626. BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
  627. ( virt_to_bus ( qib7322->sendbufavail ) >> 6 ) );
  628. qib7322_writeq ( qib7322, &sendbufavailaddr,
  629. QIB_7322_SendBufAvailAddr_offset );
  630. /* Enable sending */
  631. memset ( &sendctrlp, 0, sizeof ( sendctrlp ) );
  632. BIT_FILL_1 ( &sendctrlp, SendEnable, 1 );
  633. qib7322_writeq ( qib7322, &sendctrlp, QIB_7322_SendCtrl_0_offset );
  634. qib7322_writeq ( qib7322, &sendctrlp, QIB_7322_SendCtrl_1_offset );
  635. /* Enable DMA of SendBufAvail */
  636. memset ( &sendctrl, 0, sizeof ( sendctrl ) );
  637. BIT_FILL_1 ( &sendctrl, SendBufAvailUpd, 1 );
  638. qib7322_writeq ( qib7322, &sendctrl, QIB_7322_SendCtrl_offset );
  639. return 0;
  640. free_dma ( qib7322->sendbufavail, sizeof ( *qib7322->sendbufavail ) );
  641. err_alloc_sendbufavail:
  642. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port1 );
  643. err_create_send_bufs_vl15_port1:
  644. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port0 );
  645. err_create_send_bufs_vl15_port0:
  646. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_small );
  647. err_create_send_bufs_small:
  648. return rc;
  649. }
  650. /**
  651. * Shut down send datapath
  652. *
  653. * @v qib7322 QIB7322 device
  654. */
  655. static void qib7322_fini_send ( struct qib7322 *qib7322 ) {
  656. struct QIB_7322_SendCtrl sendctrl;
  657. /* Disable sending and DMA of SendBufAvail */
  658. memset ( &sendctrl, 0, sizeof ( sendctrl ) );
  659. qib7322_writeq ( qib7322, &sendctrl, QIB_7322_SendCtrl_offset );
  660. mb();
  661. /* Ensure hardware has seen this disable */
  662. qib7322_readq ( qib7322, &sendctrl, QIB_7322_SendCtrl_offset );
  663. free_dma ( qib7322->sendbufavail, sizeof ( *qib7322->sendbufavail ) );
  664. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port1 );
  665. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port0 );
  666. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_small );
  667. }
  668. /***************************************************************************
  669. *
  670. * Receive datapath
  671. *
  672. ***************************************************************************
  673. */
  674. /**
  675. * Create receive work queue
  676. *
  677. * @v ibdev Infiniband device
  678. * @v qp Queue pair
  679. * @ret rc Return status code
  680. */
  681. static int qib7322_create_recv_wq ( struct ib_device *ibdev,
  682. struct ib_queue_pair *qp ) {
  683. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  684. struct ib_work_queue *wq = &qp->recv;
  685. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  686. struct QIB_7322_RcvHdrAddr0 rcvhdraddr;
  687. struct QIB_7322_RcvHdrTailAddr0 rcvhdrtailaddr;
  688. struct QIB_7322_RcvHdrHead0 rcvhdrhead;
  689. struct QIB_7322_scalar rcvegrindexhead;
  690. struct QIB_7322_RcvCtrl rcvctrl;
  691. struct QIB_7322_RcvCtrl_P rcvctrlp;
  692. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  693. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  694. int rc;
  695. /* Reset context information */
  696. memset ( &qib7322_wq->header_prod, 0,
  697. sizeof ( qib7322_wq->header_prod ) );
  698. qib7322_wq->header_cons = 0;
  699. qib7322_wq->eager_prod = 0;
  700. qib7322_wq->eager_cons = 0;
  701. /* Allocate receive header buffer */
  702. qib7322_wq->header = malloc_dma ( QIB7322_RECV_HEADERS_SIZE,
  703. QIB7322_RECV_HEADERS_ALIGN );
  704. if ( ! qib7322_wq->header ) {
  705. rc = -ENOMEM;
  706. goto err_alloc_header;
  707. }
  708. /* Enable context in hardware */
  709. memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
  710. BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr,
  711. ( virt_to_bus ( qib7322_wq->header ) >> 2 ) );
  712. qib7322_writeq_array8b ( qib7322, &rcvhdraddr,
  713. QIB_7322_RcvHdrAddr0_offset, ctx );
  714. memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
  715. BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr,
  716. ( virt_to_bus ( &qib7322_wq->header_prod ) >> 2 ) );
  717. qib7322_writeq_array8b ( qib7322, &rcvhdrtailaddr,
  718. QIB_7322_RcvHdrTailAddr0_offset, ctx );
  719. memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
  720. BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
  721. qib7322_writeq_array64k ( qib7322, &rcvhdrhead,
  722. QIB_7322_RcvHdrHead0_offset, ctx );
  723. memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
  724. BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
  725. qib7322_writeq_array64k ( qib7322, &rcvegrindexhead,
  726. QIB_7322_RcvEgrIndexHead0_offset, ctx );
  727. qib7322_readq_port ( qib7322, &rcvctrlp,
  728. QIB_7322_RcvCtrl_0_offset, port );
  729. BIT_SET ( &rcvctrlp, ContextEnable[ctx], 1 );
  730. qib7322_writeq_port ( qib7322, &rcvctrlp,
  731. QIB_7322_RcvCtrl_0_offset, port );
  732. qib7322_readq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  733. BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
  734. qib7322_writeq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  735. DBGC ( qib7322, "QIB7322 %p port %d QPN %ld CTX %d hdrs [%lx,%lx) prod "
  736. "%lx\n", qib7322, port, qp->qpn, ctx,
  737. virt_to_bus ( qib7322_wq->header ),
  738. ( virt_to_bus ( qib7322_wq->header )
  739. + QIB7322_RECV_HEADERS_SIZE ),
  740. virt_to_bus ( &qib7322_wq->header_prod ) );
  741. return 0;
  742. free_dma ( qib7322_wq->header, QIB7322_RECV_HEADERS_SIZE );
  743. err_alloc_header:
  744. return rc;
  745. }
  746. /**
  747. * Destroy receive work queue
  748. *
  749. * @v ibdev Infiniband device
  750. * @v qp Queue pair
  751. */
  752. static void qib7322_destroy_recv_wq ( struct ib_device *ibdev,
  753. struct ib_queue_pair *qp ) {
  754. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  755. struct ib_work_queue *wq = &qp->recv;
  756. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  757. struct QIB_7322_RcvCtrl rcvctrl;
  758. struct QIB_7322_RcvCtrl_P rcvctrlp;
  759. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  760. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  761. /* Disable context in hardware */
  762. qib7322_readq_port ( qib7322, &rcvctrlp,
  763. QIB_7322_RcvCtrl_0_offset, port );
  764. BIT_SET ( &rcvctrlp, ContextEnable[ctx], 0 );
  765. qib7322_writeq_port ( qib7322, &rcvctrlp,
  766. QIB_7322_RcvCtrl_0_offset, port );
  767. qib7322_readq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  768. BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
  769. qib7322_writeq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  770. /* Make sure the hardware has seen that the context is disabled */
  771. qib7322_readq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  772. mb();
  773. /* Free headers ring */
  774. free_dma ( qib7322_wq->header, QIB7322_RECV_HEADERS_SIZE );
  775. }
  776. /**
  777. * Initialise receive datapath
  778. *
  779. * @v qib7322 QIB7322 device
  780. * @ret rc Return status code
  781. */
  782. static int qib7322_init_recv ( struct qib7322 *qib7322 ) {
  783. struct QIB_7322_RcvCtrl rcvctrl;
  784. struct QIB_7322_RcvCtrl_0 rcvctrlp;
  785. struct QIB_7322_RcvQPMapTableA_0 rcvqpmaptablea0;
  786. struct QIB_7322_RcvQPMapTableB_0 rcvqpmaptableb0;
  787. struct QIB_7322_RcvQPMapTableA_1 rcvqpmaptablea1;
  788. struct QIB_7322_RcvQPMapTableB_1 rcvqpmaptableb1;
  789. struct QIB_7322_RcvQPMulticastContext_0 rcvqpmcastctx0;
  790. struct QIB_7322_RcvQPMulticastContext_1 rcvqpmcastctx1;
  791. struct QIB_7322_scalar rcvegrbase;
  792. struct QIB_7322_scalar rcvhdrentsize;
  793. struct QIB_7322_scalar rcvhdrcnt;
  794. struct QIB_7322_RcvBTHQP_0 rcvbthqp;
  795. struct QIB_7322_RxCreditVL0_0 rxcreditvl;
  796. unsigned int contextcfg;
  797. unsigned long egrbase;
  798. unsigned int eager_array_size_kernel;
  799. unsigned int eager_array_size_user;
  800. unsigned int ctx;
  801. /* Select configuration based on number of contexts */
  802. switch ( QIB7322_NUM_CONTEXTS ) {
  803. case 6:
  804. contextcfg = QIB7322_CONTEXTCFG_6CTX;
  805. eager_array_size_kernel = QIB7322_EAGER_ARRAY_SIZE_6CTX_KERNEL;
  806. eager_array_size_user = QIB7322_EAGER_ARRAY_SIZE_6CTX_USER;
  807. break;
  808. case 10:
  809. contextcfg = QIB7322_CONTEXTCFG_10CTX;
  810. eager_array_size_kernel = QIB7322_EAGER_ARRAY_SIZE_10CTX_KERNEL;
  811. eager_array_size_user = QIB7322_EAGER_ARRAY_SIZE_10CTX_USER;
  812. break;
  813. case 18:
  814. contextcfg = QIB7322_CONTEXTCFG_18CTX;
  815. eager_array_size_kernel = QIB7322_EAGER_ARRAY_SIZE_18CTX_KERNEL;
  816. eager_array_size_user = QIB7322_EAGER_ARRAY_SIZE_18CTX_USER;
  817. break;
  818. default:
  819. linker_assert ( 0, invalid_QIB7322_NUM_CONTEXTS );
  820. return -EINVAL;
  821. }
  822. /* Configure number of contexts */
  823. memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
  824. BIT_FILL_2 ( &rcvctrl,
  825. TailUpd, 1,
  826. ContextCfg, contextcfg );
  827. qib7322_writeq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  828. /* Map QPNs to contexts */
  829. memset ( &rcvctrlp, 0, sizeof ( rcvctrlp ) );
  830. BIT_FILL_3 ( &rcvctrlp,
  831. RcvIBPortEnable, 1,
  832. RcvQPMapEnable, 1,
  833. RcvPartitionKeyDisable, 1 );
  834. qib7322_writeq ( qib7322, &rcvctrlp, QIB_7322_RcvCtrl_0_offset );
  835. qib7322_writeq ( qib7322, &rcvctrlp, QIB_7322_RcvCtrl_1_offset );
  836. memset ( &rcvqpmaptablea0, 0, sizeof ( rcvqpmaptablea0 ) );
  837. BIT_FILL_6 ( &rcvqpmaptablea0,
  838. RcvQPMapContext0, 0,
  839. RcvQPMapContext1, 2,
  840. RcvQPMapContext2, 4,
  841. RcvQPMapContext3, 6,
  842. RcvQPMapContext4, 8,
  843. RcvQPMapContext5, 10 );
  844. qib7322_writeq ( qib7322, &rcvqpmaptablea0,
  845. QIB_7322_RcvQPMapTableA_0_offset );
  846. memset ( &rcvqpmaptableb0, 0, sizeof ( rcvqpmaptableb0 ) );
  847. BIT_FILL_3 ( &rcvqpmaptableb0,
  848. RcvQPMapContext6, 12,
  849. RcvQPMapContext7, 14,
  850. RcvQPMapContext8, 16 );
  851. qib7322_writeq ( qib7322, &rcvqpmaptableb0,
  852. QIB_7322_RcvQPMapTableB_0_offset );
  853. memset ( &rcvqpmaptablea1, 0, sizeof ( rcvqpmaptablea1 ) );
  854. BIT_FILL_6 ( &rcvqpmaptablea1,
  855. RcvQPMapContext0, 1,
  856. RcvQPMapContext1, 3,
  857. RcvQPMapContext2, 5,
  858. RcvQPMapContext3, 7,
  859. RcvQPMapContext4, 9,
  860. RcvQPMapContext5, 11 );
  861. qib7322_writeq ( qib7322, &rcvqpmaptablea1,
  862. QIB_7322_RcvQPMapTableA_1_offset );
  863. memset ( &rcvqpmaptableb1, 0, sizeof ( rcvqpmaptableb1 ) );
  864. BIT_FILL_3 ( &rcvqpmaptableb1,
  865. RcvQPMapContext6, 13,
  866. RcvQPMapContext7, 15,
  867. RcvQPMapContext8, 17 );
  868. qib7322_writeq ( qib7322, &rcvqpmaptableb1,
  869. QIB_7322_RcvQPMapTableB_1_offset );
  870. /* Map multicast QPNs to contexts */
  871. memset ( &rcvqpmcastctx0, 0, sizeof ( rcvqpmcastctx0 ) );
  872. BIT_FILL_1 ( &rcvqpmcastctx0, RcvQpMcContext, 0 );
  873. qib7322_writeq ( qib7322, &rcvqpmcastctx0,
  874. QIB_7322_RcvQPMulticastContext_0_offset );
  875. memset ( &rcvqpmcastctx1, 0, sizeof ( rcvqpmcastctx1 ) );
  876. BIT_FILL_1 ( &rcvqpmcastctx1, RcvQpMcContext, 1 );
  877. qib7322_writeq ( qib7322, &rcvqpmcastctx1,
  878. QIB_7322_RcvQPMulticastContext_1_offset );
  879. /* Configure receive header buffer sizes */
  880. memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
  881. BIT_FILL_1 ( &rcvhdrcnt, Value, QIB7322_RECV_HEADER_COUNT );
  882. qib7322_writeq ( qib7322, &rcvhdrcnt, QIB_7322_RcvHdrCnt_offset );
  883. memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
  884. BIT_FILL_1 ( &rcvhdrentsize, Value, ( QIB7322_RECV_HEADER_SIZE >> 2 ) );
  885. qib7322_writeq ( qib7322, &rcvhdrentsize,
  886. QIB_7322_RcvHdrEntSize_offset );
  887. /* Calculate eager array start addresses for each context */
  888. qib7322_readq ( qib7322, &rcvegrbase, QIB_7322_RcvEgrBase_offset );
  889. egrbase = BIT_GET ( &rcvegrbase, Value );
  890. for ( ctx = 0 ; ctx < QIB7322_MAX_PORTS ; ctx++ ) {
  891. qib7322->recv_wq[ctx].eager_array = egrbase;
  892. qib7322->recv_wq[ctx].eager_entries = eager_array_size_kernel;
  893. egrbase += ( eager_array_size_kernel *
  894. sizeof ( struct QIB_7322_RcvEgr ) );
  895. }
  896. for ( ; ctx < QIB7322_NUM_CONTEXTS ; ctx++ ) {
  897. qib7322->recv_wq[ctx].eager_array = egrbase;
  898. qib7322->recv_wq[ctx].eager_entries = eager_array_size_user;
  899. egrbase += ( eager_array_size_user *
  900. sizeof ( struct QIB_7322_RcvEgr ) );
  901. }
  902. for ( ctx = 0 ; ctx < QIB7322_NUM_CONTEXTS ; ctx++ ) {
  903. DBGC ( qib7322, "QIB7322 %p CTX %d eager array at %lx (%d "
  904. "entries)\n", qib7322, ctx,
  905. qib7322->recv_wq[ctx].eager_array,
  906. qib7322->recv_wq[ctx].eager_entries );
  907. }
  908. /* Set the BTH QP for Infinipath packets to an unused value */
  909. memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
  910. BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, QIB7322_QP_IDETH );
  911. qib7322_writeq ( qib7322, &rcvbthqp, QIB_7322_RcvBTHQP_0_offset );
  912. qib7322_writeq ( qib7322, &rcvbthqp, QIB_7322_RcvBTHQP_1_offset );
  913. /* Assign initial credits */
  914. memset ( &rxcreditvl, 0, sizeof ( rxcreditvl ) );
  915. BIT_FILL_1 ( &rxcreditvl, RxMaxCreditVL, QIB7322_MAX_CREDITS_VL0 );
  916. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  917. QIB_7322_RxCreditVL0_0_offset, 0 );
  918. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  919. QIB_7322_RxCreditVL0_1_offset, 0 );
  920. BIT_FILL_1 ( &rxcreditvl, RxMaxCreditVL, QIB7322_MAX_CREDITS_VL15 );
  921. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  922. QIB_7322_RxCreditVL0_0_offset, 15 );
  923. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  924. QIB_7322_RxCreditVL0_1_offset, 15 );
  925. return 0;
  926. }
  927. /**
  928. * Shut down receive datapath
  929. *
  930. * @v qib7322 QIB7322 device
  931. */
  932. static void qib7322_fini_recv ( struct qib7322 *qib7322 __unused ) {
  933. /* Nothing to do; all contexts were already disabled when the
  934. * queue pairs were destroyed
  935. */
  936. }
  937. /***************************************************************************
  938. *
  939. * Completion queue operations
  940. *
  941. ***************************************************************************
  942. */
  943. /**
  944. * Create completion queue
  945. *
  946. * @v ibdev Infiniband device
  947. * @v cq Completion queue
  948. * @ret rc Return status code
  949. */
  950. static int qib7322_create_cq ( struct ib_device *ibdev,
  951. struct ib_completion_queue *cq ) {
  952. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  953. static int cqn;
  954. /* The hardware has no concept of completion queues. We
  955. * simply use the association between CQs and WQs (already
  956. * handled by the IB core) to decide which WQs to poll.
  957. *
  958. * We do set a CQN, just to avoid confusing debug messages
  959. * from the IB core.
  960. */
  961. cq->cqn = ++cqn;
  962. DBGC ( qib7322, "QIB7322 %p CQN %ld created\n", qib7322, cq->cqn );
  963. return 0;
  964. }
  965. /**
  966. * Destroy completion queue
  967. *
  968. * @v ibdev Infiniband device
  969. * @v cq Completion queue
  970. */
  971. static void qib7322_destroy_cq ( struct ib_device *ibdev,
  972. struct ib_completion_queue *cq ) {
  973. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  974. /* Nothing to do */
  975. DBGC ( qib7322, "QIB7322 %p CQN %ld destroyed\n", qib7322, cq->cqn );
  976. }
  977. /***************************************************************************
  978. *
  979. * Queue pair operations
  980. *
  981. ***************************************************************************
  982. */
  983. /**
  984. * Create queue pair
  985. *
  986. * @v ibdev Infiniband device
  987. * @v qp Queue pair
  988. * @ret rc Return status code
  989. */
  990. static int qib7322_create_qp ( struct ib_device *ibdev,
  991. struct ib_queue_pair *qp ) {
  992. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  993. unsigned int ctx;
  994. int rc;
  995. /* Allocate a context and QPN */
  996. if ( ( rc = qib7322_alloc_ctx ( ibdev, qp ) ) != 0 )
  997. goto err_alloc_ctx;
  998. ctx = qib7322_ctx ( ibdev, qp );
  999. /* Set work-queue private data pointers */
  1000. ib_wq_set_drvdata ( &qp->send, &qib7322->send_wq[ctx] );
  1001. ib_wq_set_drvdata ( &qp->recv, &qib7322->recv_wq[ctx] );
  1002. /* Create receive work queue */
  1003. if ( ( rc = qib7322_create_recv_wq ( ibdev, qp ) ) != 0 )
  1004. goto err_create_recv_wq;
  1005. /* Create send work queue */
  1006. if ( ( rc = qib7322_create_send_wq ( ibdev, qp ) ) != 0 )
  1007. goto err_create_send_wq;
  1008. return 0;
  1009. qib7322_destroy_send_wq ( ibdev, qp );
  1010. err_create_send_wq:
  1011. qib7322_destroy_recv_wq ( ibdev, qp );
  1012. err_create_recv_wq:
  1013. qib7322_free_ctx ( ibdev, qp );
  1014. err_alloc_ctx:
  1015. return rc;
  1016. }
  1017. /**
  1018. * Modify queue pair
  1019. *
  1020. * @v ibdev Infiniband device
  1021. * @v qp Queue pair
  1022. * @ret rc Return status code
  1023. */
  1024. static int qib7322_modify_qp ( struct ib_device *ibdev,
  1025. struct ib_queue_pair *qp ) {
  1026. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1027. /* Nothing to do; the hardware doesn't have a notion of queue
  1028. * keys
  1029. */
  1030. DBGC2 ( qib7322, "QIB7322 %p QPN %ld modified\n", qib7322, qp->qpn );
  1031. return 0;
  1032. }
  1033. /**
  1034. * Destroy queue pair
  1035. *
  1036. * @v ibdev Infiniband device
  1037. * @v qp Queue pair
  1038. */
  1039. static void qib7322_destroy_qp ( struct ib_device *ibdev,
  1040. struct ib_queue_pair *qp ) {
  1041. qib7322_destroy_send_wq ( ibdev, qp );
  1042. qib7322_destroy_recv_wq ( ibdev, qp );
  1043. qib7322_free_ctx ( ibdev, qp );
  1044. }
  1045. /***************************************************************************
  1046. *
  1047. * Work request operations
  1048. *
  1049. ***************************************************************************
  1050. */
  1051. /**
  1052. * Post send work queue entry
  1053. *
  1054. * @v ibdev Infiniband device
  1055. * @v qp Queue pair
  1056. * @v dest Destination address vector
  1057. * @v iobuf I/O buffer
  1058. * @ret rc Return status code
  1059. */
  1060. static int qib7322_post_send ( struct ib_device *ibdev,
  1061. struct ib_queue_pair *qp,
  1062. struct ib_address_vector *dest,
  1063. struct io_buffer *iobuf ) {
  1064. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1065. struct ib_work_queue *wq = &qp->send;
  1066. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1067. struct QIB_7322_SendPbc sendpbc;
  1068. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  1069. uint8_t header_buf[IB_MAX_HEADER_SIZE];
  1070. struct io_buffer headers;
  1071. int send_buf;
  1072. unsigned long start_offset;
  1073. unsigned long offset;
  1074. size_t len;
  1075. ssize_t frag_len;
  1076. uint32_t *data;
  1077. /* Allocate send buffer and calculate offset */
  1078. send_buf = qib7322_alloc_send_buf ( qib7322, qib7322_wq->send_bufs );
  1079. if ( send_buf < 0 )
  1080. return send_buf;
  1081. start_offset = offset =
  1082. qib7322_send_buffer_offset ( qib7322, qib7322_wq->send_bufs,
  1083. send_buf );
  1084. /* Store I/O buffer and send buffer index */
  1085. assert ( wq->iobufs[qib7322_wq->prod] == NULL );
  1086. wq->iobufs[qib7322_wq->prod] = iobuf;
  1087. qib7322_wq->used[qib7322_wq->prod] = send_buf;
  1088. /* Construct headers */
  1089. iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
  1090. iob_reserve ( &headers, sizeof ( header_buf ) );
  1091. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
  1092. /* Calculate packet length */
  1093. len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
  1094. iob_len ( iobuf ) + 3 ) & ~3 );
  1095. /* Construct send per-buffer control word */
  1096. memset ( &sendpbc, 0, sizeof ( sendpbc ) );
  1097. BIT_FILL_3 ( &sendpbc,
  1098. LengthP1_toibc, ( ( len >> 2 ) - 1 ),
  1099. Port, port,
  1100. VL15, ( ( qp->type == IB_QPT_SMI ) ? 1 : 0 ) );
  1101. /* Write SendPbc */
  1102. DBG_DISABLE ( DBGLVL_IO );
  1103. qib7322_writeq ( qib7322, &sendpbc, offset );
  1104. offset += sizeof ( sendpbc );
  1105. /* Write headers */
  1106. for ( data = headers.data, frag_len = iob_len ( &headers ) ;
  1107. frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
  1108. qib7322_writel ( qib7322, *data, offset );
  1109. }
  1110. /* Write data */
  1111. for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
  1112. frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
  1113. qib7322_writel ( qib7322, *data, offset );
  1114. }
  1115. DBG_ENABLE ( DBGLVL_IO );
  1116. assert ( ( start_offset + len ) == offset );
  1117. DBGC2 ( qib7322, "QIB7322 %p QPN %ld TX %04x(%04x) posted [%lx,%lx)\n",
  1118. qib7322, qp->qpn, send_buf, qib7322_wq->prod,
  1119. start_offset, offset );
  1120. /* Increment producer counter */
  1121. qib7322_wq->prod = ( ( qib7322_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
  1122. return 0;
  1123. }
  1124. /**
  1125. * Complete send work queue entry
  1126. *
  1127. * @v ibdev Infiniband device
  1128. * @v qp Queue pair
  1129. * @v wqe_idx Work queue entry index
  1130. */
  1131. static void qib7322_complete_send ( struct ib_device *ibdev,
  1132. struct ib_queue_pair *qp,
  1133. unsigned int wqe_idx ) {
  1134. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1135. struct ib_work_queue *wq = &qp->send;
  1136. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1137. struct io_buffer *iobuf;
  1138. unsigned int send_buf;
  1139. /* Parse completion */
  1140. send_buf = qib7322_wq->used[wqe_idx];
  1141. DBGC2 ( qib7322, "QIB7322 %p QPN %ld TX %04x(%04x) complete\n",
  1142. qib7322, qp->qpn, send_buf, wqe_idx );
  1143. /* Complete work queue entry */
  1144. iobuf = wq->iobufs[wqe_idx];
  1145. assert ( iobuf != NULL );
  1146. ib_complete_send ( ibdev, qp, iobuf, 0 );
  1147. wq->iobufs[wqe_idx] = NULL;
  1148. /* Free send buffer */
  1149. qib7322_free_send_buf ( qib7322, qib7322_wq->send_bufs, send_buf );
  1150. }
  1151. /**
  1152. * Poll send work queue
  1153. *
  1154. * @v ibdev Infiniband device
  1155. * @v qp Queue pair
  1156. */
  1157. static void qib7322_poll_send_wq ( struct ib_device *ibdev,
  1158. struct ib_queue_pair *qp ) {
  1159. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1160. struct ib_work_queue *wq = &qp->send;
  1161. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1162. unsigned int send_buf;
  1163. /* Look for completions */
  1164. while ( wq->fill ) {
  1165. /* Check to see if send buffer has completed */
  1166. send_buf = qib7322_wq->used[qib7322_wq->cons];
  1167. if ( qib7322_send_buf_in_use ( qib7322, send_buf ) )
  1168. break;
  1169. /* Complete this buffer */
  1170. qib7322_complete_send ( ibdev, qp, qib7322_wq->cons );
  1171. /* Increment consumer counter */
  1172. qib7322_wq->cons = ( ( qib7322_wq->cons + 1 ) &
  1173. ( wq->num_wqes - 1 ) );
  1174. }
  1175. }
  1176. /**
  1177. * Post receive work queue entry
  1178. *
  1179. * @v ibdev Infiniband device
  1180. * @v qp Queue pair
  1181. * @v iobuf I/O buffer
  1182. * @ret rc Return status code
  1183. */
  1184. static int qib7322_post_recv ( struct ib_device *ibdev,
  1185. struct ib_queue_pair *qp,
  1186. struct io_buffer *iobuf ) {
  1187. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1188. struct ib_work_queue *wq = &qp->recv;
  1189. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1190. struct QIB_7322_RcvEgr rcvegr;
  1191. struct QIB_7322_scalar rcvegrindexhead;
  1192. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  1193. physaddr_t addr;
  1194. size_t len;
  1195. unsigned int wqe_idx;
  1196. unsigned int bufsize;
  1197. /* Sanity checks */
  1198. addr = virt_to_bus ( iobuf->data );
  1199. len = iob_tailroom ( iobuf );
  1200. if ( addr & ( QIB7322_EAGER_BUFFER_ALIGN - 1 ) ) {
  1201. DBGC ( qib7322, "QIB7322 %p QPN %ld misaligned RX buffer "
  1202. "(%08lx)\n", qib7322, qp->qpn, addr );
  1203. return -EINVAL;
  1204. }
  1205. if ( len != QIB7322_RECV_PAYLOAD_SIZE ) {
  1206. DBGC ( qib7322, "QIB7322 %p QPN %ld wrong RX buffer size "
  1207. "(%zd)\n", qib7322, qp->qpn, len );
  1208. return -EINVAL;
  1209. }
  1210. /* Calculate eager producer index and WQE index */
  1211. wqe_idx = ( qib7322_wq->eager_prod & ( wq->num_wqes - 1 ) );
  1212. assert ( wq->iobufs[wqe_idx] == NULL );
  1213. /* Store I/O buffer */
  1214. wq->iobufs[wqe_idx] = iobuf;
  1215. /* Calculate buffer size */
  1216. switch ( QIB7322_RECV_PAYLOAD_SIZE ) {
  1217. case 2048: bufsize = QIB7322_EAGER_BUFFER_2K; break;
  1218. case 4096: bufsize = QIB7322_EAGER_BUFFER_4K; break;
  1219. case 8192: bufsize = QIB7322_EAGER_BUFFER_8K; break;
  1220. case 16384: bufsize = QIB7322_EAGER_BUFFER_16K; break;
  1221. case 32768: bufsize = QIB7322_EAGER_BUFFER_32K; break;
  1222. case 65536: bufsize = QIB7322_EAGER_BUFFER_64K; break;
  1223. default: linker_assert ( 0, invalid_rx_payload_size );
  1224. bufsize = QIB7322_EAGER_BUFFER_NONE;
  1225. }
  1226. /* Post eager buffer */
  1227. memset ( &rcvegr, 0, sizeof ( rcvegr ) );
  1228. BIT_FILL_2 ( &rcvegr,
  1229. Addr, ( addr >> 11 ),
  1230. BufSize, bufsize );
  1231. qib7322_writeq_array8b ( qib7322, &rcvegr, qib7322_wq->eager_array,
  1232. qib7322_wq->eager_prod );
  1233. DBGC2 ( qib7322, "QIB7322 %p QPN %ld RX egr %04x(%04x) posted "
  1234. "[%lx,%lx)\n", qib7322, qp->qpn, qib7322_wq->eager_prod,
  1235. wqe_idx, addr, ( addr + len ) );
  1236. /* Increment producer index */
  1237. qib7322_wq->eager_prod = ( ( qib7322_wq->eager_prod + 1 ) &
  1238. ( qib7322_wq->eager_entries - 1 ) );
  1239. /* Update head index */
  1240. memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
  1241. BIT_FILL_1 ( &rcvegrindexhead,
  1242. Value, ( ( qib7322_wq->eager_prod + 1 ) &
  1243. ( qib7322_wq->eager_entries - 1 ) ) );
  1244. qib7322_writeq_array64k ( qib7322, &rcvegrindexhead,
  1245. QIB_7322_RcvEgrIndexHead0_offset, ctx );
  1246. return 0;
  1247. }
  1248. /**
  1249. * Complete receive work queue entry
  1250. *
  1251. * @v ibdev Infiniband device
  1252. * @v qp Queue pair
  1253. * @v header_offs Header offset
  1254. */
  1255. static void qib7322_complete_recv ( struct ib_device *ibdev,
  1256. struct ib_queue_pair *qp,
  1257. unsigned int header_offs ) {
  1258. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1259. struct ib_work_queue *wq = &qp->recv;
  1260. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1261. struct QIB_7322_RcvHdrFlags *rcvhdrflags;
  1262. struct QIB_7322_RcvEgr rcvegr;
  1263. struct io_buffer headers;
  1264. struct io_buffer *iobuf;
  1265. struct ib_queue_pair *intended_qp;
  1266. struct ib_address_vector source;
  1267. unsigned int rcvtype;
  1268. unsigned int pktlen;
  1269. unsigned int egrindex;
  1270. unsigned int useegrbfr;
  1271. unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
  1272. unsigned int lenerr, parityerr, vcrcerr, icrcerr;
  1273. unsigned int err;
  1274. unsigned int hdrqoffset;
  1275. unsigned int header_len;
  1276. unsigned int padded_payload_len;
  1277. unsigned int wqe_idx;
  1278. size_t payload_len;
  1279. int qp0;
  1280. int rc;
  1281. /* RcvHdrFlags are at the end of the header entry */
  1282. rcvhdrflags = ( qib7322_wq->header + header_offs +
  1283. QIB7322_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
  1284. rcvtype = BIT_GET ( rcvhdrflags, RcvType );
  1285. pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
  1286. egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
  1287. useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
  1288. hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
  1289. iberr = BIT_GET ( rcvhdrflags, IBErr );
  1290. mkerr = BIT_GET ( rcvhdrflags, MKErr );
  1291. tiderr = BIT_GET ( rcvhdrflags, TIDErr );
  1292. khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
  1293. mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
  1294. lenerr = BIT_GET ( rcvhdrflags, LenErr );
  1295. parityerr = BIT_GET ( rcvhdrflags, ParityErr );
  1296. vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
  1297. icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
  1298. header_len = ( QIB7322_RECV_HEADER_SIZE - hdrqoffset -
  1299. sizeof ( *rcvhdrflags ) );
  1300. padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
  1301. err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
  1302. lenerr | parityerr | vcrcerr | icrcerr );
  1303. /* IB header is placed immediately before RcvHdrFlags */
  1304. iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
  1305. header_len, header_len );
  1306. /* Dump diagnostic information */
  1307. DBGC2 ( qib7322, "QIB7322 %p QPN %ld RX egr %04x%s hdr %d type %d len "
  1308. "%d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", qib7322, qp->qpn,
  1309. egrindex, ( useegrbfr ? "" : "(unused)" ),
  1310. ( header_offs / QIB7322_RECV_HEADER_SIZE ),
  1311. rcvtype, pktlen, header_len, padded_payload_len,
  1312. ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
  1313. ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
  1314. ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
  1315. ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
  1316. ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
  1317. ( err ? "]" : "" ) );
  1318. DBGCP_HDA ( qib7322, hdrqoffset, headers.data,
  1319. ( header_len + sizeof ( *rcvhdrflags ) ) );
  1320. /* Parse header to generate address vector */
  1321. qp0 = ( qp->qpn == 0 );
  1322. intended_qp = NULL;
  1323. if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
  1324. &payload_len, &source ) ) != 0 ) {
  1325. DBGC ( qib7322, "QIB7322 %p could not parse headers: %s\n",
  1326. qib7322, strerror ( rc ) );
  1327. err = 1;
  1328. }
  1329. if ( ! intended_qp )
  1330. intended_qp = qp;
  1331. /* Complete this buffer and any skipped buffers. Note that
  1332. * when the hardware runs out of buffers, it will repeatedly
  1333. * report the same buffer (the tail) as a TID error, and that
  1334. * it also has a habit of sometimes skipping over several
  1335. * buffers at once.
  1336. */
  1337. while ( 1 ) {
  1338. /* If we have caught up to the producer counter, stop.
  1339. * This will happen when the hardware first runs out
  1340. * of buffers and starts reporting TID errors against
  1341. * the eager buffer it wants to use next.
  1342. */
  1343. if ( qib7322_wq->eager_cons == qib7322_wq->eager_prod )
  1344. break;
  1345. /* If we have caught up to where we should be after
  1346. * completing this egrindex, stop. We phrase the test
  1347. * this way to avoid completing the entire ring when
  1348. * we receive the same egrindex twice in a row.
  1349. */
  1350. if ( ( qib7322_wq->eager_cons ==
  1351. ( ( egrindex + 1 ) & ( qib7322_wq->eager_entries - 1 ))))
  1352. break;
  1353. /* Identify work queue entry and corresponding I/O
  1354. * buffer.
  1355. */
  1356. wqe_idx = ( qib7322_wq->eager_cons & ( wq->num_wqes - 1 ) );
  1357. iobuf = wq->iobufs[wqe_idx];
  1358. assert ( iobuf != NULL );
  1359. wq->iobufs[wqe_idx] = NULL;
  1360. /* Complete the eager buffer */
  1361. if ( qib7322_wq->eager_cons == egrindex ) {
  1362. /* Completing the eager buffer described in
  1363. * this header entry.
  1364. */
  1365. iob_put ( iobuf, payload_len );
  1366. rc = ( err ? -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
  1367. /* Redirect to target QP if necessary */
  1368. if ( qp != intended_qp ) {
  1369. DBGC2 ( qib7322, "QIB7322 %p redirecting QPN "
  1370. "%ld => %ld\n",
  1371. qib7322, qp->qpn, intended_qp->qpn );
  1372. /* Compensate for incorrect fill levels */
  1373. qp->recv.fill--;
  1374. intended_qp->recv.fill++;
  1375. }
  1376. ib_complete_recv ( ibdev, intended_qp, &source,
  1377. iobuf, rc);
  1378. } else {
  1379. /* Completing on a skipped-over eager buffer */
  1380. ib_complete_recv ( ibdev, qp, &source, iobuf,
  1381. -ECANCELED );
  1382. }
  1383. /* Clear eager buffer */
  1384. memset ( &rcvegr, 0, sizeof ( rcvegr ) );
  1385. qib7322_writeq_array8b ( qib7322, &rcvegr,
  1386. qib7322_wq->eager_array,
  1387. qib7322_wq->eager_cons );
  1388. /* Increment consumer index */
  1389. qib7322_wq->eager_cons = ( ( qib7322_wq->eager_cons + 1 ) &
  1390. ( qib7322_wq->eager_entries - 1 ) );
  1391. }
  1392. }
  1393. /**
  1394. * Poll receive work queue
  1395. *
  1396. * @v ibdev Infiniband device
  1397. * @v qp Queue pair
  1398. */
  1399. static void qib7322_poll_recv_wq ( struct ib_device *ibdev,
  1400. struct ib_queue_pair *qp ) {
  1401. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1402. struct ib_work_queue *wq = &qp->recv;
  1403. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1404. struct QIB_7322_RcvHdrHead0 rcvhdrhead;
  1405. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  1406. unsigned int header_prod;
  1407. /* Check for received packets */
  1408. header_prod = ( BIT_GET ( &qib7322_wq->header_prod, Value ) << 2 );
  1409. if ( header_prod == qib7322_wq->header_cons )
  1410. return;
  1411. /* Process all received packets */
  1412. while ( qib7322_wq->header_cons != header_prod ) {
  1413. /* Complete the receive */
  1414. qib7322_complete_recv ( ibdev, qp, qib7322_wq->header_cons );
  1415. /* Increment the consumer offset */
  1416. qib7322_wq->header_cons += QIB7322_RECV_HEADER_SIZE;
  1417. qib7322_wq->header_cons %= QIB7322_RECV_HEADERS_SIZE;
  1418. /* QIB7322 has only one send buffer per port for VL15,
  1419. * which almost always leads to send buffer exhaustion
  1420. * and dropped MADs. Mitigate this by refusing to
  1421. * process more than one VL15 MAD per poll, which will
  1422. * enforce interleaved TX/RX polls.
  1423. */
  1424. if ( qp->type == IB_QPT_SMI )
  1425. break;
  1426. }
  1427. /* Update consumer offset */
  1428. memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
  1429. BIT_FILL_2 ( &rcvhdrhead,
  1430. RcvHeadPointer, ( qib7322_wq->header_cons >> 2 ),
  1431. counter, 1 );
  1432. qib7322_writeq_array64k ( qib7322, &rcvhdrhead,
  1433. QIB_7322_RcvHdrHead0_offset, ctx );
  1434. }
  1435. /**
  1436. * Poll completion queue
  1437. *
  1438. * @v ibdev Infiniband device
  1439. * @v cq Completion queue
  1440. */
  1441. static void qib7322_poll_cq ( struct ib_device *ibdev,
  1442. struct ib_completion_queue *cq ) {
  1443. struct ib_work_queue *wq;
  1444. /* Poll associated send and receive queues */
  1445. list_for_each_entry ( wq, &cq->work_queues, list ) {
  1446. if ( wq->is_send ) {
  1447. qib7322_poll_send_wq ( ibdev, wq->qp );
  1448. } else {
  1449. qib7322_poll_recv_wq ( ibdev, wq->qp );
  1450. }
  1451. }
  1452. }
  1453. /***************************************************************************
  1454. *
  1455. * Event queues
  1456. *
  1457. ***************************************************************************
  1458. */
  1459. /**
  1460. * Poll event queue
  1461. *
  1462. * @v ibdev Infiniband device
  1463. */
  1464. static void qib7322_poll_eq ( struct ib_device *ibdev ) {
  1465. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1466. struct QIB_7322_ErrStatus_0 errstatus;
  1467. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  1468. /* Check for and clear status bits */
  1469. DBG_DISABLE ( DBGLVL_IO );
  1470. qib7322_readq_port ( qib7322, &errstatus,
  1471. QIB_7322_ErrStatus_0_offset, port );
  1472. if ( errstatus.u.qwords[0] ) {
  1473. DBGC ( qib7322, "QIB7322 %p port %d status %08x%08x\n", qib7322,
  1474. port, errstatus.u.dwords[1], errstatus.u.dwords[0] );
  1475. qib7322_writeq_port ( qib7322, &errstatus,
  1476. QIB_7322_ErrClear_0_offset, port );
  1477. }
  1478. DBG_ENABLE ( DBGLVL_IO );
  1479. /* Check for link status changes */
  1480. if ( BIT_GET ( &errstatus, IBStatusChanged ) )
  1481. qib7322_link_state_changed ( ibdev );
  1482. }
  1483. /***************************************************************************
  1484. *
  1485. * Infiniband link-layer operations
  1486. *
  1487. ***************************************************************************
  1488. */
  1489. /**
  1490. * Determine supported link speeds
  1491. *
  1492. * @v qib7322 QIB7322 device
  1493. * @ret supported Supported link speeds
  1494. */
  1495. static unsigned int qib7322_link_speed_supported ( struct qib7322 *qib7322,
  1496. unsigned int port ) {
  1497. struct QIB_7322_feature_mask features;
  1498. struct QIB_7322_Revision revision;
  1499. unsigned int supported;
  1500. unsigned int boardid;
  1501. /* Read the active feature mask */
  1502. qib7322_readq ( qib7322, &features,
  1503. QIB_7322_active_feature_mask_offset );
  1504. switch ( port ) {
  1505. case 0 :
  1506. supported = BIT_GET ( &features, Port0_Link_Speed_Supported );
  1507. break;
  1508. case 1 :
  1509. supported = BIT_GET ( &features, Port1_Link_Speed_Supported );
  1510. break;
  1511. default:
  1512. DBGC ( qib7322, "QIB7322 %p port %d is invalid\n",
  1513. qib7322, port );
  1514. supported = 0;
  1515. break;
  1516. }
  1517. /* Apply hacks for specific board IDs */
  1518. qib7322_readq ( qib7322, &revision, QIB_7322_Revision_offset );
  1519. boardid = BIT_GET ( &revision, BoardID );
  1520. switch ( boardid ) {
  1521. case QIB7322_BOARD_QMH7342 :
  1522. DBGC2 ( qib7322, "QIB7322 %p is a QMH7342; forcing QDR-only\n",
  1523. qib7322 );
  1524. supported = IB_LINK_SPEED_QDR;
  1525. break;
  1526. default:
  1527. /* Do nothing */
  1528. break;
  1529. }
  1530. DBGC2 ( qib7322, "QIB7322 %p port %d %s%s%s%s\n", qib7322, port,
  1531. ( supported ? "supports" : "disabled" ),
  1532. ( ( supported & IB_LINK_SPEED_SDR ) ? " SDR" : "" ),
  1533. ( ( supported & IB_LINK_SPEED_DDR ) ? " DDR" : "" ),
  1534. ( ( supported & IB_LINK_SPEED_QDR ) ? " QDR" : "" ) );
  1535. return supported;
  1536. }
  1537. /**
  1538. * Initialise Infiniband link
  1539. *
  1540. * @v ibdev Infiniband device
  1541. * @ret rc Return status code
  1542. */
  1543. static int qib7322_open ( struct ib_device *ibdev ) {
  1544. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1545. struct QIB_7322_IBCCtrlA_0 ibcctrla;
  1546. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  1547. /* Enable link */
  1548. qib7322_readq_port ( qib7322, &ibcctrla,
  1549. QIB_7322_IBCCtrlA_0_offset, port );
  1550. BIT_SET ( &ibcctrla, IBLinkEn, 1 );
  1551. qib7322_writeq_port ( qib7322, &ibcctrla,
  1552. QIB_7322_IBCCtrlA_0_offset, port );
  1553. return 0;
  1554. }
  1555. /**
  1556. * Close Infiniband link
  1557. *
  1558. * @v ibdev Infiniband device
  1559. */
  1560. static void qib7322_close ( struct ib_device *ibdev ) {
  1561. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1562. struct QIB_7322_IBCCtrlA_0 ibcctrla;
  1563. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  1564. /* Disable link */
  1565. qib7322_readq_port ( qib7322, &ibcctrla,
  1566. QIB_7322_IBCCtrlA_0_offset, port );
  1567. BIT_SET ( &ibcctrla, IBLinkEn, 0 );
  1568. qib7322_writeq_port ( qib7322, &ibcctrla,
  1569. QIB_7322_IBCCtrlA_0_offset, port );
  1570. }
  1571. /***************************************************************************
  1572. *
  1573. * Multicast group operations
  1574. *
  1575. ***************************************************************************
  1576. */
  1577. /**
  1578. * Attach to multicast group
  1579. *
  1580. * @v ibdev Infiniband device
  1581. * @v qp Queue pair
  1582. * @v gid Multicast GID
  1583. * @ret rc Return status code
  1584. */
  1585. static int qib7322_mcast_attach ( struct ib_device *ibdev,
  1586. struct ib_queue_pair *qp,
  1587. union ib_gid *gid ) {
  1588. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1589. ( void ) qib7322;
  1590. ( void ) qp;
  1591. ( void ) gid;
  1592. return 0;
  1593. }
  1594. /**
  1595. * Detach from multicast group
  1596. *
  1597. * @v ibdev Infiniband device
  1598. * @v qp Queue pair
  1599. * @v gid Multicast GID
  1600. */
  1601. static void qib7322_mcast_detach ( struct ib_device *ibdev,
  1602. struct ib_queue_pair *qp,
  1603. union ib_gid *gid ) {
  1604. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1605. ( void ) qib7322;
  1606. ( void ) qp;
  1607. ( void ) gid;
  1608. }
  1609. /** QIB7322 Infiniband operations */
  1610. static struct ib_device_operations qib7322_ib_operations = {
  1611. .create_cq = qib7322_create_cq,
  1612. .destroy_cq = qib7322_destroy_cq,
  1613. .create_qp = qib7322_create_qp,
  1614. .modify_qp = qib7322_modify_qp,
  1615. .destroy_qp = qib7322_destroy_qp,
  1616. .post_send = qib7322_post_send,
  1617. .post_recv = qib7322_post_recv,
  1618. .poll_cq = qib7322_poll_cq,
  1619. .poll_eq = qib7322_poll_eq,
  1620. .open = qib7322_open,
  1621. .close = qib7322_close,
  1622. .mcast_attach = qib7322_mcast_attach,
  1623. .mcast_detach = qib7322_mcast_detach,
  1624. .set_port_info = qib7322_set_port_info,
  1625. .set_pkey_table = qib7322_set_pkey_table,
  1626. };
  1627. /***************************************************************************
  1628. *
  1629. * I2C bus operations
  1630. *
  1631. ***************************************************************************
  1632. */
  1633. /** QIB7322 I2C bit to GPIO mappings */
  1634. static unsigned int qib7322_i2c_bits[] = {
  1635. [I2C_BIT_SCL] = ( 1 << QIB7322_GPIO_SCL ),
  1636. [I2C_BIT_SDA] = ( 1 << QIB7322_GPIO_SDA ),
  1637. };
  1638. /**
  1639. * Read QIB7322 I2C line status
  1640. *
  1641. * @v basher Bit-bashing interface
  1642. * @v bit_id Bit number
  1643. * @ret zero Input is a logic 0
  1644. * @ret non-zero Input is a logic 1
  1645. */
  1646. static int qib7322_i2c_read_bit ( struct bit_basher *basher,
  1647. unsigned int bit_id ) {
  1648. struct qib7322 *qib7322 =
  1649. container_of ( basher, struct qib7322, i2c.basher );
  1650. struct QIB_7322_EXTStatus extstatus;
  1651. unsigned int status;
  1652. DBG_DISABLE ( DBGLVL_IO );
  1653. qib7322_readq ( qib7322, &extstatus, QIB_7322_EXTStatus_offset );
  1654. status = ( BIT_GET ( &extstatus, GPIOIn ) & qib7322_i2c_bits[bit_id] );
  1655. DBG_ENABLE ( DBGLVL_IO );
  1656. return status;
  1657. }
  1658. /**
  1659. * Write QIB7322 I2C line status
  1660. *
  1661. * @v basher Bit-bashing interface
  1662. * @v bit_id Bit number
  1663. * @v data Value to write
  1664. */
  1665. static void qib7322_i2c_write_bit ( struct bit_basher *basher,
  1666. unsigned int bit_id, unsigned long data ) {
  1667. struct qib7322 *qib7322 =
  1668. container_of ( basher, struct qib7322, i2c.basher );
  1669. struct QIB_7322_EXTCtrl extctrl;
  1670. struct QIB_7322_GPIO gpioout;
  1671. unsigned int bit = qib7322_i2c_bits[bit_id];
  1672. unsigned int outputs = 0;
  1673. unsigned int output_enables = 0;
  1674. DBG_DISABLE ( DBGLVL_IO );
  1675. /* Read current GPIO mask and outputs */
  1676. qib7322_readq ( qib7322, &extctrl, QIB_7322_EXTCtrl_offset );
  1677. qib7322_readq ( qib7322, &gpioout, QIB_7322_GPIOOut_offset );
  1678. /* Update outputs and output enables. I2C lines are tied
  1679. * high, so we always set the output to 0 and use the output
  1680. * enable to control the line.
  1681. */
  1682. output_enables = BIT_GET ( &extctrl, GPIOOe );
  1683. output_enables = ( ( output_enables & ~bit ) | ( ~data & bit ) );
  1684. outputs = BIT_GET ( &gpioout, GPIO );
  1685. outputs = ( outputs & ~bit );
  1686. BIT_SET ( &extctrl, GPIOOe, output_enables );
  1687. BIT_SET ( &gpioout, GPIO, outputs );
  1688. /* Write the output enable first; that way we avoid logic
  1689. * hazards.
  1690. */
  1691. qib7322_writeq ( qib7322, &extctrl, QIB_7322_EXTCtrl_offset );
  1692. qib7322_writeq ( qib7322, &gpioout, QIB_7322_GPIOOut_offset );
  1693. mb();
  1694. DBG_ENABLE ( DBGLVL_IO );
  1695. }
  1696. /** QIB7322 I2C bit-bashing interface operations */
  1697. static struct bit_basher_operations qib7322_i2c_basher_ops = {
  1698. .read = qib7322_i2c_read_bit,
  1699. .write = qib7322_i2c_write_bit,
  1700. };
  1701. /**
  1702. * Initialise QIB7322 I2C subsystem
  1703. *
  1704. * @v qib7322 QIB7322 device
  1705. * @ret rc Return status code
  1706. */
  1707. static int qib7322_init_i2c ( struct qib7322 *qib7322 ) {
  1708. static int try_eeprom_address[] = { 0x51, 0x50 };
  1709. unsigned int i;
  1710. int rc;
  1711. /* Initialise bus */
  1712. if ( ( rc = init_i2c_bit_basher ( &qib7322->i2c,
  1713. &qib7322_i2c_basher_ops ) ) != 0 ) {
  1714. DBGC ( qib7322, "QIB7322 %p could not initialise I2C bus: %s\n",
  1715. qib7322, strerror ( rc ) );
  1716. return rc;
  1717. }
  1718. /* Probe for devices */
  1719. for ( i = 0 ; i < ( sizeof ( try_eeprom_address ) /
  1720. sizeof ( try_eeprom_address[0] ) ) ; i++ ) {
  1721. init_i2c_eeprom ( &qib7322->eeprom, try_eeprom_address[i] );
  1722. if ( ( rc = i2c_check_presence ( &qib7322->i2c.i2c,
  1723. &qib7322->eeprom ) ) == 0 ) {
  1724. DBGC2 ( qib7322, "QIB7322 %p found EEPROM at %02x\n",
  1725. qib7322, try_eeprom_address[i] );
  1726. return 0;
  1727. }
  1728. }
  1729. DBGC ( qib7322, "QIB7322 %p could not find EEPROM\n", qib7322 );
  1730. return -ENODEV;
  1731. }
  1732. /**
  1733. * Read EEPROM parameters
  1734. *
  1735. * @v qib7322 QIB7322 device
  1736. * @ret rc Return status code
  1737. */
  1738. static int qib7322_read_eeprom ( struct qib7322 *qib7322 ) {
  1739. struct i2c_interface *i2c = &qib7322->i2c.i2c;
  1740. union ib_guid *guid = &qib7322->guid;
  1741. int rc;
  1742. /* Read GUID */
  1743. if ( ( rc = i2c->read ( i2c, &qib7322->eeprom,
  1744. QIB7322_EEPROM_GUID_OFFSET, guid->bytes,
  1745. sizeof ( *guid ) ) ) != 0 ) {
  1746. DBGC ( qib7322, "QIB7322 %p could not read GUID: %s\n",
  1747. qib7322, strerror ( rc ) );
  1748. return rc;
  1749. }
  1750. DBGC2 ( qib7322, "QIB7322 %p has GUID " IB_GUID_FMT "\n",
  1751. qib7322, IB_GUID_ARGS ( guid ) );
  1752. /* Read serial number (debug only) */
  1753. if ( DBG_LOG ) {
  1754. uint8_t serial[QIB7322_EEPROM_SERIAL_SIZE + 1];
  1755. serial[ sizeof ( serial ) - 1 ] = '\0';
  1756. if ( ( rc = i2c->read ( i2c, &qib7322->eeprom,
  1757. QIB7322_EEPROM_SERIAL_OFFSET, serial,
  1758. ( sizeof ( serial ) - 1 ) ) ) != 0 ) {
  1759. DBGC ( qib7322, "QIB7322 %p could not read serial: "
  1760. "%s\n", qib7322, strerror ( rc ) );
  1761. return rc;
  1762. }
  1763. DBGC2 ( qib7322, "QIB7322 %p has serial number \"%s\"\n",
  1764. qib7322, serial );
  1765. }
  1766. return 0;
  1767. }
  1768. /***************************************************************************
  1769. *
  1770. * Advanced High-performance Bus (AHB) access
  1771. *
  1772. ***************************************************************************
  1773. */
  1774. /**
  1775. * Wait for AHB transaction to complete
  1776. *
  1777. * @v qib7322 QIB7322 device
  1778. * @ret rc Return status code
  1779. */
  1780. static int qib7322_ahb_wait ( struct qib7322 *qib7322 ) {
  1781. struct QIB_7322_ahb_transaction_reg transaction;
  1782. unsigned int i;
  1783. /* Wait for Ready bit to be asserted */
  1784. for ( i = 0 ; i < QIB7322_AHB_MAX_WAIT_US ; i++ ) {
  1785. qib7322_readq ( qib7322, &transaction,
  1786. QIB_7322_ahb_transaction_reg_offset );
  1787. if ( BIT_GET ( &transaction, ahb_rdy ) )
  1788. return 0;
  1789. udelay ( 1 );
  1790. }
  1791. DBGC ( qib7322, "QIB7322 %p timed out waiting for AHB transaction\n",
  1792. qib7322 );
  1793. return -ETIMEDOUT;
  1794. }
  1795. /**
  1796. * Request ownership of the AHB
  1797. *
  1798. * @v qib7322 QIB7322 device
  1799. * @v location AHB location
  1800. * @ret rc Return status code
  1801. */
  1802. static int qib7322_ahb_request ( struct qib7322 *qib7322,
  1803. unsigned int location ) {
  1804. struct QIB_7322_ahb_access_ctrl access;
  1805. int rc;
  1806. /* Request ownership */
  1807. memset ( &access, 0, sizeof ( access ) );
  1808. BIT_FILL_2 ( &access,
  1809. sw_ahb_sel, 1,
  1810. sw_sel_ahb_trgt, QIB7322_AHB_LOC_TARGET ( location ) );
  1811. qib7322_writeq ( qib7322, &access, QIB_7322_ahb_access_ctrl_offset );
  1812. /* Wait for ownership to be granted */
  1813. if ( ( rc = qib7322_ahb_wait ( qib7322 ) ) != 0 ) {
  1814. DBGC ( qib7322, "QIB7322 %p could not obtain AHB ownership: "
  1815. "%s\n", qib7322, strerror ( rc ) );
  1816. return rc;
  1817. }
  1818. return 0;
  1819. }
  1820. /**
  1821. * Release ownership of the AHB
  1822. *
  1823. * @v qib7322 QIB7322 device
  1824. */
  1825. static void qib7322_ahb_release ( struct qib7322 *qib7322 ) {
  1826. struct QIB_7322_ahb_access_ctrl access;
  1827. memset ( &access, 0, sizeof ( access ) );
  1828. qib7322_writeq ( qib7322, &access, QIB_7322_ahb_access_ctrl_offset );
  1829. }
  1830. /**
  1831. * Read data via AHB
  1832. *
  1833. * @v qib7322 QIB7322 device
  1834. * @v location AHB location
  1835. * @v data Data to read
  1836. * @ret rc Return status code
  1837. *
  1838. * You must have already acquired ownership of the AHB.
  1839. */
  1840. static int qib7322_ahb_read ( struct qib7322 *qib7322, unsigned int location,
  1841. uint32_t *data ) {
  1842. struct QIB_7322_ahb_transaction_reg xact;
  1843. int rc;
  1844. /* Avoid returning uninitialised data on error */
  1845. *data = 0;
  1846. /* Initiate transaction */
  1847. memset ( &xact, 0, sizeof ( xact ) );
  1848. BIT_FILL_2 ( &xact,
  1849. ahb_address, QIB7322_AHB_LOC_ADDRESS ( location ),
  1850. write_not_read, 0 );
  1851. qib7322_writeq ( qib7322, &xact, QIB_7322_ahb_transaction_reg_offset );
  1852. /* Wait for transaction to complete */
  1853. if ( ( rc = qib7322_ahb_wait ( qib7322 ) ) != 0 )
  1854. return rc;
  1855. /* Read transaction data */
  1856. qib7322_readq ( qib7322, &xact, QIB_7322_ahb_transaction_reg_offset );
  1857. *data = BIT_GET ( &xact, ahb_data );
  1858. return 0;
  1859. }
  1860. /**
  1861. * Write data via AHB
  1862. *
  1863. * @v qib7322 QIB7322 device
  1864. * @v location AHB location
  1865. * @v data Data to write
  1866. * @ret rc Return status code
  1867. *
  1868. * You must have already acquired ownership of the AHB.
  1869. */
  1870. static int qib7322_ahb_write ( struct qib7322 *qib7322, unsigned int location,
  1871. uint32_t data ) {
  1872. struct QIB_7322_ahb_transaction_reg xact;
  1873. int rc;
  1874. /* Initiate transaction */
  1875. memset ( &xact, 0, sizeof ( xact ) );
  1876. BIT_FILL_3 ( &xact,
  1877. ahb_address, QIB7322_AHB_LOC_ADDRESS ( location ),
  1878. write_not_read, 1,
  1879. ahb_data, data );
  1880. qib7322_writeq ( qib7322, &xact, QIB_7322_ahb_transaction_reg_offset );
  1881. /* Wait for transaction to complete */
  1882. if ( ( rc = qib7322_ahb_wait ( qib7322 ) ) != 0 )
  1883. return rc;
  1884. return 0;
  1885. }
  1886. /**
  1887. * Read/modify/write AHB register
  1888. *
  1889. * @v qib7322 QIB7322 device
  1890. * @v location AHB location
  1891. * @v value Value to set
  1892. * @v mask Mask to apply to old value
  1893. * @ret rc Return status code
  1894. */
  1895. static int qib7322_ahb_mod_reg ( struct qib7322 *qib7322, unsigned int location,
  1896. uint32_t value, uint32_t mask ) {
  1897. uint32_t old_value;
  1898. uint32_t new_value;
  1899. int rc;
  1900. DBG_DISABLE ( DBGLVL_IO );
  1901. /* Sanity check */
  1902. assert ( ( value & mask ) == value );
  1903. /* Acquire bus ownership */
  1904. if ( ( rc = qib7322_ahb_request ( qib7322, location ) ) != 0 )
  1905. goto out;
  1906. /* Read existing value */
  1907. if ( ( rc = qib7322_ahb_read ( qib7322, location, &old_value ) ) != 0 )
  1908. goto out_release;
  1909. /* Update value */
  1910. new_value = ( ( old_value & ~mask ) | value );
  1911. DBGCP ( qib7322, "QIB7322 %p AHB %x %#08x => %#08x\n",
  1912. qib7322, location, old_value, new_value );
  1913. if ( ( rc = qib7322_ahb_write ( qib7322, location, new_value ) ) != 0 )
  1914. goto out_release;
  1915. out_release:
  1916. /* Release bus */
  1917. qib7322_ahb_release ( qib7322 );
  1918. out:
  1919. DBG_ENABLE ( DBGLVL_IO );
  1920. return rc;
  1921. }
  1922. /**
  1923. * Read/modify/write AHB register across all ports and channels
  1924. *
  1925. * @v qib7322 QIB7322 device
  1926. * @v reg AHB register
  1927. * @v value Value to set
  1928. * @v mask Mask to apply to old value
  1929. * @ret rc Return status code
  1930. */
  1931. static int qib7322_ahb_mod_reg_all ( struct qib7322 *qib7322, unsigned int reg,
  1932. uint32_t value, uint32_t mask ) {
  1933. unsigned int port;
  1934. unsigned int channel;
  1935. unsigned int location;
  1936. int rc;
  1937. for ( port = 0 ; port < QIB7322_MAX_PORTS ; port++ ) {
  1938. for ( channel = 0 ; channel < QIB7322_MAX_WIDTH ; channel++ ) {
  1939. location = QIB7322_AHB_LOCATION ( port, channel, reg );
  1940. if ( ( rc = qib7322_ahb_mod_reg ( qib7322, location,
  1941. value, mask ) ) != 0 )
  1942. return rc;
  1943. }
  1944. }
  1945. return 0;
  1946. }
  1947. /***************************************************************************
  1948. *
  1949. * Infiniband SerDes initialisation
  1950. *
  1951. ***************************************************************************
  1952. */
  1953. /**
  1954. * Initialise the IB SerDes
  1955. *
  1956. * @v qib7322 QIB7322 device
  1957. * @ret rc Return status code
  1958. */
  1959. static int qib7322_init_ib_serdes ( struct qib7322 *qib7322 ) {
  1960. struct QIB_7322_IBCCtrlA_0 ibcctrla;
  1961. struct QIB_7322_IBCCtrlB_0 ibcctrlb;
  1962. struct QIB_7322_IBPCSConfig_0 ibpcsconfig;
  1963. /* Configure sensible defaults for IBC */
  1964. memset ( &ibcctrla, 0, sizeof ( ibcctrla ) );
  1965. BIT_FILL_5 ( &ibcctrla, /* Tuning values taken from Linux driver */
  1966. FlowCtrlPeriod, 0x03,
  1967. FlowCtrlWaterMark, 0x05,
  1968. MaxPktLen, ( ( QIB7322_RECV_HEADER_SIZE +
  1969. QIB7322_RECV_PAYLOAD_SIZE +
  1970. 4 /* ICRC */ ) >> 2 ),
  1971. PhyerrThreshold, 0xf,
  1972. OverrunThreshold, 0xf );
  1973. qib7322_writeq ( qib7322, &ibcctrla, QIB_7322_IBCCtrlA_0_offset );
  1974. qib7322_writeq ( qib7322, &ibcctrla, QIB_7322_IBCCtrlA_1_offset );
  1975. /* Force SDR only to avoid needing all the DDR tuning,
  1976. * Mellanox compatibility hacks etc. SDR is plenty for
  1977. * boot-time operation.
  1978. */
  1979. qib7322_readq ( qib7322, &ibcctrlb, QIB_7322_IBCCtrlB_0_offset );
  1980. BIT_SET ( &ibcctrlb, IB_ENHANCED_MODE, 0 );
  1981. BIT_SET ( &ibcctrlb, SD_SPEED_SDR, 1 );
  1982. BIT_SET ( &ibcctrlb, SD_SPEED_DDR, 0 );
  1983. BIT_SET ( &ibcctrlb, SD_SPEED_QDR, 0 );
  1984. BIT_SET ( &ibcctrlb, IB_NUM_CHANNELS, 1 ); /* 4X only */
  1985. BIT_SET ( &ibcctrlb, IB_LANE_REV_SUPPORTED, 0 );
  1986. BIT_SET ( &ibcctrlb, HRTBT_ENB, 0 );
  1987. BIT_SET ( &ibcctrlb, HRTBT_AUTO, 0 );
  1988. qib7322_writeq ( qib7322, &ibcctrlb, QIB_7322_IBCCtrlB_0_offset );
  1989. qib7322_writeq ( qib7322, &ibcctrlb, QIB_7322_IBCCtrlB_1_offset );
  1990. /* Tune SerDes */
  1991. qib7322_ahb_mod_reg_all ( qib7322, 2, 0, 0x00000e00UL );
  1992. /* Bring XGXS out of reset */
  1993. memset ( &ibpcsconfig, 0, sizeof ( ibpcsconfig ) );
  1994. qib7322_writeq ( qib7322, &ibpcsconfig, QIB_7322_IBPCSConfig_0_offset );
  1995. qib7322_writeq ( qib7322, &ibpcsconfig, QIB_7322_IBPCSConfig_1_offset );
  1996. return 0;
  1997. }
  1998. /***************************************************************************
  1999. *
  2000. * PCI layer interface
  2001. *
  2002. ***************************************************************************
  2003. */
  2004. /**
  2005. * Reset QIB7322
  2006. *
  2007. * @v qib7322 QIB7322 device
  2008. * @v pci PCI device
  2009. * @ret rc Return status code
  2010. */
  2011. static void qib7322_reset ( struct qib7322 *qib7322, struct pci_device *pci ) {
  2012. struct QIB_7322_Control control;
  2013. struct pci_config_backup backup;
  2014. /* Back up PCI configuration space */
  2015. pci_backup ( pci, &backup, NULL );
  2016. /* Assert reset */
  2017. memset ( &control, 0, sizeof ( control ) );
  2018. BIT_FILL_1 ( &control, SyncReset, 1 );
  2019. qib7322_writeq ( qib7322, &control, QIB_7322_Control_offset );
  2020. /* Wait for reset to complete */
  2021. mdelay ( 1000 );
  2022. /* Restore PCI configuration space */
  2023. pci_restore ( pci, &backup, NULL );
  2024. }
  2025. /**
  2026. * Probe PCI device
  2027. *
  2028. * @v pci PCI device
  2029. * @v id PCI ID
  2030. * @ret rc Return status code
  2031. */
  2032. static int qib7322_probe ( struct pci_device *pci ) {
  2033. struct qib7322 *qib7322;
  2034. struct QIB_7322_Revision revision;
  2035. struct ib_device *ibdev;
  2036. unsigned int link_speed_supported;
  2037. int i;
  2038. int rc;
  2039. /* Allocate QIB7322 device */
  2040. qib7322 = zalloc ( sizeof ( *qib7322 ) );
  2041. if ( ! qib7322 ) {
  2042. rc = -ENOMEM;
  2043. goto err_alloc_qib7322;
  2044. }
  2045. pci_set_drvdata ( pci, qib7322 );
  2046. /* Fix up PCI device */
  2047. adjust_pci_device ( pci );
  2048. /* Get PCI BARs */
  2049. qib7322->regs = ioremap ( pci->membase, QIB7322_BAR0_SIZE );
  2050. DBGC2 ( qib7322, "QIB7322 %p has BAR at %08lx\n",
  2051. qib7322, pci->membase );
  2052. /* Reset device */
  2053. qib7322_reset ( qib7322, pci );
  2054. /* Print some general data */
  2055. qib7322_readq ( qib7322, &revision, QIB_7322_Revision_offset );
  2056. DBGC2 ( qib7322, "QIB7322 %p board %02lx v%ld.%ld.%ld.%ld\n", qib7322,
  2057. BIT_GET ( &revision, BoardID ),
  2058. BIT_GET ( &revision, R_SW ),
  2059. BIT_GET ( &revision, R_Arch ),
  2060. BIT_GET ( &revision, R_ChipRevMajor ),
  2061. BIT_GET ( &revision, R_ChipRevMinor ) );
  2062. /* Initialise I2C subsystem */
  2063. if ( ( rc = qib7322_init_i2c ( qib7322 ) ) != 0 )
  2064. goto err_init_i2c;
  2065. /* Read EEPROM parameters */
  2066. if ( ( rc = qib7322_read_eeprom ( qib7322 ) ) != 0 )
  2067. goto err_read_eeprom;
  2068. /* Initialise send datapath */
  2069. if ( ( rc = qib7322_init_send ( qib7322 ) ) != 0 )
  2070. goto err_init_send;
  2071. /* Initialise receive datapath */
  2072. if ( ( rc = qib7322_init_recv ( qib7322 ) ) != 0 )
  2073. goto err_init_recv;
  2074. /* Initialise the IB SerDes */
  2075. if ( ( rc = qib7322_init_ib_serdes ( qib7322 ) ) != 0 )
  2076. goto err_init_ib_serdes;
  2077. /* Allocate Infiniband devices */
  2078. for ( i = 0 ; i < QIB7322_MAX_PORTS ; i++ ) {
  2079. link_speed_supported =
  2080. qib7322_link_speed_supported ( qib7322, i );
  2081. if ( ! link_speed_supported )
  2082. continue;
  2083. ibdev = alloc_ibdev ( 0 );
  2084. if ( ! ibdev ) {
  2085. rc = -ENOMEM;
  2086. goto err_alloc_ibdev;
  2087. }
  2088. qib7322->ibdev[i] = ibdev;
  2089. ibdev->dev = &pci->dev;
  2090. ibdev->op = &qib7322_ib_operations;
  2091. ibdev->port = ( QIB7322_PORT_BASE + i );
  2092. ibdev->link_width_enabled = ibdev->link_width_supported =
  2093. IB_LINK_WIDTH_4X; /* 1x does not work */
  2094. ibdev->link_speed_enabled = ibdev->link_speed_supported =
  2095. IB_LINK_SPEED_SDR; /* to avoid need for link tuning */
  2096. memcpy ( &ibdev->node_guid, &qib7322->guid,
  2097. sizeof ( ibdev->node_guid ) );
  2098. memcpy ( &ibdev->gid.s.guid, &qib7322->guid,
  2099. sizeof ( ibdev->gid.s.guid ) );
  2100. assert ( ( ibdev->gid.s.guid.bytes[7] & i ) == 0 );
  2101. ibdev->gid.s.guid.bytes[7] |= i;
  2102. ib_set_drvdata ( ibdev, qib7322 );
  2103. }
  2104. /* Register Infiniband devices */
  2105. for ( i = 0 ; i < QIB7322_MAX_PORTS ; i++ ) {
  2106. if ( ! qib7322->ibdev[i] )
  2107. continue;
  2108. if ( ( rc = register_ibdev ( qib7322->ibdev[i] ) ) != 0 ) {
  2109. DBGC ( qib7322, "QIB7322 %p port %d could not register "
  2110. "IB device: %s\n", qib7322, i, strerror ( rc ) );
  2111. goto err_register_ibdev;
  2112. }
  2113. }
  2114. return 0;
  2115. i = QIB7322_MAX_PORTS;
  2116. err_register_ibdev:
  2117. for ( i-- ; i >= 0 ; i-- ) {
  2118. if ( qib7322->ibdev[i] )
  2119. unregister_ibdev ( qib7322->ibdev[i] );
  2120. }
  2121. i = QIB7322_MAX_PORTS;
  2122. err_alloc_ibdev:
  2123. for ( i-- ; i >= 0 ; i-- )
  2124. ibdev_put ( qib7322->ibdev[i] );
  2125. err_init_ib_serdes:
  2126. qib7322_fini_send ( qib7322 );
  2127. err_init_send:
  2128. qib7322_fini_recv ( qib7322 );
  2129. err_init_recv:
  2130. err_read_eeprom:
  2131. err_init_i2c:
  2132. free ( qib7322 );
  2133. err_alloc_qib7322:
  2134. return rc;
  2135. }
  2136. /**
  2137. * Remove PCI device
  2138. *
  2139. * @v pci PCI device
  2140. */
  2141. static void qib7322_remove ( struct pci_device *pci ) {
  2142. struct qib7322 *qib7322 = pci_get_drvdata ( pci );
  2143. int i;
  2144. for ( i = ( QIB7322_MAX_PORTS - 1 ) ; i >= 0 ; i-- ) {
  2145. if ( qib7322->ibdev[i] )
  2146. unregister_ibdev ( qib7322->ibdev[i] );
  2147. }
  2148. for ( i = ( QIB7322_MAX_PORTS - 1 ) ; i >= 0 ; i-- )
  2149. ibdev_put ( qib7322->ibdev[i] );
  2150. qib7322_fini_send ( qib7322 );
  2151. qib7322_fini_recv ( qib7322 );
  2152. free ( qib7322 );
  2153. }
  2154. static struct pci_device_id qib7322_nics[] = {
  2155. PCI_ROM ( 0x1077, 0x7322, "iba7322", "IBA7322 QDR InfiniBand HCA", 0 ),
  2156. };
  2157. struct pci_driver qib7322_driver __pci_driver = {
  2158. .ids = qib7322_nics,
  2159. .id_count = ( sizeof ( qib7322_nics ) / sizeof ( qib7322_nics[0] ) ),
  2160. .probe = qib7322_probe,
  2161. .remove = qib7322_remove,
  2162. };