qib7322.c 72KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <assert.h>
  24. #include <ipxe/io.h>
  25. #include <ipxe/pci.h>
  26. #include <ipxe/infiniband.h>
  27. #include <ipxe/i2c.h>
  28. #include <ipxe/bitbash.h>
  29. #include <ipxe/malloc.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/pcibackup.h>
  32. #include "qib7322.h"
  33. /**
  34. * @file
  35. *
  36. * QLogic QIB7322 Infiniband HCA
  37. *
  38. */
  39. /** A QIB7322 send buffer set */
  40. struct qib7322_send_buffers {
  41. /** Offset within register space of the first send buffer */
  42. unsigned long base;
  43. /** Send buffer size */
  44. unsigned int size;
  45. /** Index of first send buffer */
  46. unsigned int start;
  47. /** Number of send buffers
  48. *
  49. * Must be a power of two.
  50. */
  51. unsigned int count;
  52. /** Send buffer availability producer counter */
  53. unsigned int prod;
  54. /** Send buffer availability consumer counter */
  55. unsigned int cons;
  56. /** Send buffer availability */
  57. uint16_t avail[0];
  58. };
  59. /** A QIB7322 send work queue */
  60. struct qib7322_send_work_queue {
  61. /** Send buffer set */
  62. struct qib7322_send_buffers *send_bufs;
  63. /** Send buffer usage */
  64. uint16_t *used;
  65. /** Producer index */
  66. unsigned int prod;
  67. /** Consumer index */
  68. unsigned int cons;
  69. };
  70. /** A QIB7322 receive work queue */
  71. struct qib7322_recv_work_queue {
  72. /** Receive header ring */
  73. void *header;
  74. /** Receive header producer offset (written by hardware) */
  75. struct QIB_7322_scalar header_prod;
  76. /** Receive header consumer offset */
  77. unsigned int header_cons;
  78. /** Offset within register space of the eager array */
  79. unsigned long eager_array;
  80. /** Number of entries in eager array */
  81. unsigned int eager_entries;
  82. /** Eager array producer index */
  83. unsigned int eager_prod;
  84. /** Eager array consumer index */
  85. unsigned int eager_cons;
  86. };
  87. /** A QIB7322 HCA */
  88. struct qib7322 {
  89. /** Registers */
  90. void *regs;
  91. /** In-use contexts */
  92. uint8_t used_ctx[QIB7322_NUM_CONTEXTS];
  93. /** Send work queues */
  94. struct qib7322_send_work_queue send_wq[QIB7322_NUM_CONTEXTS];
  95. /** Receive work queues */
  96. struct qib7322_recv_work_queue recv_wq[QIB7322_NUM_CONTEXTS];
  97. /** Send buffer availability (reported by hardware) */
  98. struct QIB_7322_SendBufAvail *sendbufavail;
  99. /** Small send buffers */
  100. struct qib7322_send_buffers *send_bufs_small;
  101. /** VL15 port 0 send buffers */
  102. struct qib7322_send_buffers *send_bufs_vl15_port0;
  103. /** VL15 port 1 send buffers */
  104. struct qib7322_send_buffers *send_bufs_vl15_port1;
  105. /** I2C bit-bashing interface */
  106. struct i2c_bit_basher i2c;
  107. /** I2C serial EEPROM */
  108. struct i2c_device eeprom;
  109. /** Base GUID */
  110. struct ib_gid_half guid;
  111. /** Infiniband devices */
  112. struct ib_device *ibdev[QIB7322_MAX_PORTS];
  113. };
  114. /***************************************************************************
  115. *
  116. * QIB7322 register access
  117. *
  118. ***************************************************************************
  119. *
  120. * This card requires atomic 64-bit accesses. Strange things happen
  121. * if you try to use 32-bit accesses; sometimes they work, sometimes
  122. * they don't, sometimes you get random data.
  123. *
  124. * These accessors use the "movq" MMX instruction, and so won't work
  125. * on really old Pentiums (which won't have PCIe anyway, so this is
  126. * something of a moot point).
  127. */
  128. /**
  129. * Read QIB7322 qword register
  130. *
  131. * @v qib7322 QIB7322 device
  132. * @v dwords Register buffer to read into
  133. * @v offset Register offset
  134. */
  135. static void qib7322_readq ( struct qib7322 *qib7322, uint32_t *dwords,
  136. unsigned long offset ) {
  137. void *addr = ( qib7322->regs + offset );
  138. __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
  139. "movq %%mm0, (%0)\n\t"
  140. : : "r" ( dwords ), "r" ( addr ) : "memory" );
  141. DBGIO ( "[%08lx] => %08x%08x\n",
  142. virt_to_phys ( addr ), dwords[1], dwords[0] );
  143. }
  144. #define qib7322_readq( _qib7322, _ptr, _offset ) \
  145. qib7322_readq ( (_qib7322), (_ptr)->u.dwords, (_offset) )
  146. #define qib7322_readq_array8b( _qib7322, _ptr, _offset, _idx ) \
  147. qib7322_readq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
  148. #define qib7322_readq_array64k( _qib7322, _ptr, _offset, _idx ) \
  149. qib7322_readq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
  150. #define qib7322_readq_port( _qib7322, _ptr, _offset, _port ) \
  151. qib7322_readq ( (_qib7322), (_ptr), ( (_offset) + ( (_port) * 4096 ) ) )
  152. /**
  153. * Write QIB7322 qword register
  154. *
  155. * @v qib7322 QIB7322 device
  156. * @v dwords Register buffer to write
  157. * @v offset Register offset
  158. */
  159. static void qib7322_writeq ( struct qib7322 *qib7322, const uint32_t *dwords,
  160. unsigned long offset ) {
  161. void *addr = ( qib7322->regs + offset );
  162. DBGIO ( "[%08lx] <= %08x%08x\n",
  163. virt_to_phys ( addr ), dwords[1], dwords[0] );
  164. __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
  165. "movq %%mm0, (%1)\n\t"
  166. : : "r" ( dwords ), "r" ( addr ) : "memory" );
  167. }
  168. #define qib7322_writeq( _qib7322, _ptr, _offset ) \
  169. qib7322_writeq ( (_qib7322), (_ptr)->u.dwords, (_offset) )
  170. #define qib7322_writeq_array8b( _qib7322, _ptr, _offset, _idx ) \
  171. qib7322_writeq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
  172. #define qib7322_writeq_array64k( _qib7322, _ptr, _offset, _idx ) \
  173. qib7322_writeq ( (_qib7322), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ))
  174. #define qib7322_writeq_port( _qib7322, _ptr, _offset, _port ) \
  175. qib7322_writeq ( (_qib7322), (_ptr), ( (_offset) + ( (_port) * 4096 ) ))
  176. /**
  177. * Write QIB7322 dword register
  178. *
  179. * @v qib7322 QIB7322 device
  180. * @v dword Value to write
  181. * @v offset Register offset
  182. */
  183. static void qib7322_writel ( struct qib7322 *qib7322, uint32_t dword,
  184. unsigned long offset ) {
  185. writel ( dword, ( qib7322->regs + offset ) );
  186. }
  187. /***************************************************************************
  188. *
  189. * Link state management
  190. *
  191. ***************************************************************************
  192. */
  193. /**
  194. * Textual representation of link state
  195. *
  196. * @v link_state Link state
  197. * @ret link_text Link state text
  198. */
  199. static const char * qib7322_link_state_text ( unsigned int link_state ) {
  200. switch ( link_state ) {
  201. case QIB7322_LINK_STATE_DOWN: return "DOWN";
  202. case QIB7322_LINK_STATE_INIT: return "INIT";
  203. case QIB7322_LINK_STATE_ARM: return "ARM";
  204. case QIB7322_LINK_STATE_ACTIVE: return "ACTIVE";
  205. case QIB7322_LINK_STATE_ACT_DEFER: return "ACT_DEFER";
  206. default: return "UNKNOWN";
  207. }
  208. }
  209. /**
  210. * Handle link state change
  211. *
  212. * @v qib7322 QIB7322 device
  213. */
  214. static void qib7322_link_state_changed ( struct ib_device *ibdev ) {
  215. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  216. struct QIB_7322_IBCStatusA_0 ibcstatusa;
  217. struct QIB_7322_EXTCtrl extctrl;
  218. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  219. unsigned int link_training_state;
  220. unsigned int link_state;
  221. unsigned int link_width;
  222. unsigned int link_speed;
  223. unsigned int link_speed_qdr;
  224. unsigned int green;
  225. unsigned int yellow;
  226. /* Read link state */
  227. qib7322_readq_port ( qib7322, &ibcstatusa,
  228. QIB_7322_IBCStatusA_0_offset, port );
  229. link_training_state = BIT_GET ( &ibcstatusa, LinkTrainingState );
  230. link_state = BIT_GET ( &ibcstatusa, LinkState );
  231. link_width = BIT_GET ( &ibcstatusa, LinkWidthActive );
  232. link_speed = BIT_GET ( &ibcstatusa, LinkSpeedActive );
  233. link_speed_qdr = BIT_GET ( &ibcstatusa, LinkSpeedQDR );
  234. DBGC ( qib7322, "QIB7322 %p port %d training state %#x link state %s "
  235. "(%s %s)\n", qib7322, port, link_training_state,
  236. qib7322_link_state_text ( link_state ),
  237. ( link_speed_qdr ? "QDR" : ( link_speed ? "DDR" : "SDR" ) ),
  238. ( link_width ? "x4" : "x1" ) );
  239. /* Set LEDs according to link state */
  240. qib7322_readq ( qib7322, &extctrl, QIB_7322_EXTCtrl_offset );
  241. green = ( ( link_state >= QIB7322_LINK_STATE_INIT ) ? 1 : 0 );
  242. yellow = ( ( link_state >= QIB7322_LINK_STATE_ACTIVE ) ? 1 : 0 );
  243. if ( port == 0 ) {
  244. BIT_SET ( &extctrl, LEDPort0GreenOn, green );
  245. BIT_SET ( &extctrl, LEDPort0YellowOn, yellow );
  246. } else {
  247. BIT_SET ( &extctrl, LEDPort1GreenOn, green );
  248. BIT_SET ( &extctrl, LEDPort1YellowOn, yellow );
  249. }
  250. qib7322_writeq ( qib7322, &extctrl, QIB_7322_EXTCtrl_offset );
  251. /* Notify Infiniband core of link state change */
  252. ibdev->port_state = ( link_state + 1 );
  253. ibdev->link_width_active =
  254. ( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
  255. ibdev->link_speed_active =
  256. ( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
  257. ib_link_state_changed ( ibdev );
  258. }
  259. /**
  260. * Wait for link state change to take effect
  261. *
  262. * @v ibdev Infiniband device
  263. * @v new_link_state Expected link state
  264. * @ret rc Return status code
  265. */
  266. static int qib7322_link_state_check ( struct ib_device *ibdev,
  267. unsigned int new_link_state ) {
  268. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  269. struct QIB_7322_IBCStatusA_0 ibcstatusa;
  270. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  271. unsigned int link_state;
  272. unsigned int i;
  273. for ( i = 0 ; i < QIB7322_LINK_STATE_MAX_WAIT_US ; i++ ) {
  274. qib7322_readq_port ( qib7322, &ibcstatusa,
  275. QIB_7322_IBCStatusA_0_offset, port );
  276. link_state = BIT_GET ( &ibcstatusa, LinkState );
  277. if ( link_state == new_link_state )
  278. return 0;
  279. udelay ( 1 );
  280. }
  281. DBGC ( qib7322, "QIB7322 %p port %d timed out waiting for link state "
  282. "%s\n", qib7322, port, qib7322_link_state_text ( link_state ) );
  283. return -ETIMEDOUT;
  284. }
  285. /**
  286. * Set port information
  287. *
  288. * @v ibdev Infiniband device
  289. * @v mad Set port information MAD
  290. */
  291. static int qib7322_set_port_info ( struct ib_device *ibdev,
  292. union ib_mad *mad ) {
  293. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  294. struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
  295. struct QIB_7322_IBCCtrlA_0 ibcctrla;
  296. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  297. unsigned int port_state;
  298. unsigned int link_state;
  299. /* Set new link state */
  300. port_state = ( port_info->link_speed_supported__port_state & 0xf );
  301. if ( port_state ) {
  302. link_state = ( port_state - 1 );
  303. DBGC ( qib7322, "QIB7322 %p set link state to %s (%x)\n",
  304. qib7322, qib7322_link_state_text ( link_state ),
  305. link_state );
  306. qib7322_readq_port ( qib7322, &ibcctrla,
  307. QIB_7322_IBCCtrlA_0_offset, port );
  308. BIT_SET ( &ibcctrla, LinkCmd, link_state );
  309. qib7322_writeq_port ( qib7322, &ibcctrla,
  310. QIB_7322_IBCCtrlA_0_offset, port );
  311. /* Wait for link state change to take effect. Ignore
  312. * errors; the current link state will be returned via
  313. * the GetResponse MAD.
  314. */
  315. qib7322_link_state_check ( ibdev, link_state );
  316. }
  317. /* Detect and report link state change */
  318. qib7322_link_state_changed ( ibdev );
  319. return 0;
  320. }
  321. /**
  322. * Set partition key table
  323. *
  324. * @v ibdev Infiniband device
  325. * @v mad Set partition key table MAD
  326. */
  327. static int qib7322_set_pkey_table ( struct ib_device *ibdev __unused,
  328. union ib_mad *mad __unused ) {
  329. /* Nothing to do */
  330. return 0;
  331. }
  332. /***************************************************************************
  333. *
  334. * Context allocation
  335. *
  336. ***************************************************************************
  337. */
  338. /**
  339. * Allocate a context and set queue pair number
  340. *
  341. * @v ibdev Infiniband device
  342. * @v qp Queue pair
  343. * @ret rc Return status code
  344. */
  345. static int qib7322_alloc_ctx ( struct ib_device *ibdev,
  346. struct ib_queue_pair *qp ) {
  347. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  348. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  349. unsigned int ctx;
  350. for ( ctx = port ; ctx < QIB7322_NUM_CONTEXTS ; ctx += 2 ) {
  351. if ( ! qib7322->used_ctx[ctx] ) {
  352. qib7322->used_ctx[ctx] = 1;
  353. qp->qpn = ( ctx & ~0x01 );
  354. DBGC2 ( qib7322, "QIB7322 %p port %d QPN %ld is CTX "
  355. "%d\n", qib7322, port, qp->qpn, ctx );
  356. return 0;
  357. }
  358. }
  359. DBGC ( qib7322, "QIB7322 %p port %d out of available contexts\n",
  360. qib7322, port );
  361. return -ENOENT;
  362. }
  363. /**
  364. * Get queue pair context number
  365. *
  366. * @v ibdev Infiniband device
  367. * @v qp Queue pair
  368. * @ret ctx Context index
  369. */
  370. static unsigned int qib7322_ctx ( struct ib_device *ibdev,
  371. struct ib_queue_pair *qp ) {
  372. return ( qp->qpn + ( ibdev->port - QIB7322_PORT_BASE ) );
  373. }
  374. /**
  375. * Free a context
  376. *
  377. * @v qib7322 QIB7322 device
  378. * @v ctx Context index
  379. */
  380. static void qib7322_free_ctx ( struct ib_device *ibdev,
  381. struct ib_queue_pair *qp ) {
  382. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  383. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  384. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  385. qib7322->used_ctx[ctx] = 0;
  386. DBGC2 ( qib7322, "QIB7322 %p port %d CTX %d freed\n",
  387. qib7322, port, ctx );
  388. }
  389. /***************************************************************************
  390. *
  391. * Send datapath
  392. *
  393. ***************************************************************************
  394. */
  395. /** Send buffer toggle bit
  396. *
  397. * We encode send buffers as 15 bits of send buffer index plus a
  398. * single bit which should match the "check" bit in the SendBufAvail
  399. * array.
  400. */
  401. #define QIB7322_SEND_BUF_TOGGLE 0x8000
  402. /**
  403. * Create send buffer set
  404. *
  405. * @v qib7322 QIB7322 device
  406. * @v base Send buffer base offset
  407. * @v size Send buffer size
  408. * @v start Index of first send buffer
  409. * @v count Number of send buffers
  410. * @ret send_bufs Send buffer set
  411. */
  412. static struct qib7322_send_buffers *
  413. qib7322_create_send_bufs ( struct qib7322 *qib7322, unsigned long base,
  414. unsigned int size, unsigned int start,
  415. unsigned int count ) {
  416. struct qib7322_send_buffers *send_bufs;
  417. unsigned int i;
  418. /* Allocate send buffer set */
  419. send_bufs = zalloc ( sizeof ( *send_bufs ) +
  420. ( count * sizeof ( send_bufs->avail[0] ) ) );
  421. if ( ! send_bufs )
  422. return NULL;
  423. /* Populate send buffer set */
  424. send_bufs->base = base;
  425. send_bufs->size = size;
  426. send_bufs->start = start;
  427. send_bufs->count = count;
  428. for ( i = 0 ; i < count ; i++ )
  429. send_bufs->avail[i] = ( start + i );
  430. DBGC2 ( qib7322, "QIB7322 %p send buffer set %p [%d,%d] at %lx\n",
  431. qib7322, send_bufs, start, ( start + count - 1 ),
  432. send_bufs->base );
  433. return send_bufs;
  434. }
  435. /**
  436. * Destroy send buffer set
  437. *
  438. * @v qib7322 QIB7322 device
  439. * @v send_bufs Send buffer set
  440. */
  441. static void qib7322_destroy_send_bufs ( struct qib7322 *qib7322 __unused,
  442. struct qib7322_send_buffers *send_bufs ){
  443. free ( send_bufs );
  444. }
  445. /**
  446. * Allocate a send buffer
  447. *
  448. * @v qib7322 QIB7322 device
  449. * @v send_bufs Send buffer set
  450. * @ret send_buf Send buffer, or negative error
  451. */
  452. static int qib7322_alloc_send_buf ( struct qib7322 *qib7322,
  453. struct qib7322_send_buffers *send_bufs ) {
  454. unsigned int used;
  455. unsigned int mask;
  456. unsigned int send_buf;
  457. used = ( send_bufs->cons - send_bufs->prod );
  458. if ( used >= send_bufs->count ) {
  459. DBGC ( qib7322, "QIB7322 %p send buffer set %p out of "
  460. "buffers\n", qib7322, send_bufs );
  461. return -ENOBUFS;
  462. }
  463. mask = ( send_bufs->count - 1 );
  464. send_buf = send_bufs->avail[ send_bufs->cons++ & mask ];
  465. send_buf ^= QIB7322_SEND_BUF_TOGGLE;
  466. return send_buf;
  467. }
  468. /**
  469. * Free a send buffer
  470. *
  471. * @v qib7322 QIB7322 device
  472. * @v send_bufs Send buffer set
  473. * @v send_buf Send buffer
  474. */
  475. static void qib7322_free_send_buf ( struct qib7322 *qib7322 __unused,
  476. struct qib7322_send_buffers *send_bufs,
  477. unsigned int send_buf ) {
  478. unsigned int mask;
  479. mask = ( send_bufs->count - 1 );
  480. send_bufs->avail[ send_bufs->prod++ & mask ] = send_buf;
  481. }
  482. /**
  483. * Check to see if send buffer is in use
  484. *
  485. * @v qib7322 QIB7322 device
  486. * @v send_buf Send buffer
  487. * @ret in_use Send buffer is in use
  488. */
  489. static int qib7322_send_buf_in_use ( struct qib7322 *qib7322,
  490. unsigned int send_buf ) {
  491. unsigned int send_idx;
  492. unsigned int send_check;
  493. unsigned int inusecheck;
  494. unsigned int inuse;
  495. unsigned int check;
  496. send_idx = ( send_buf & ~QIB7322_SEND_BUF_TOGGLE );
  497. send_check = ( !! ( send_buf & QIB7322_SEND_BUF_TOGGLE ) );
  498. inusecheck = BIT_GET ( qib7322->sendbufavail, InUseCheck[send_idx] );
  499. inuse = ( !! ( inusecheck & 0x02 ) );
  500. check = ( !! ( inusecheck & 0x01 ) );
  501. return ( inuse || ( check != send_check ) );
  502. }
  503. /**
  504. * Calculate starting offset for send buffer
  505. *
  506. * @v qib7322 QIB7322 device
  507. * @v send_buf Send buffer
  508. * @ret offset Starting offset
  509. */
  510. static unsigned long
  511. qib7322_send_buffer_offset ( struct qib7322 *qib7322 __unused,
  512. struct qib7322_send_buffers *send_bufs,
  513. unsigned int send_buf ) {
  514. unsigned int index;
  515. index = ( ( send_buf & ~QIB7322_SEND_BUF_TOGGLE ) - send_bufs->start );
  516. return ( send_bufs->base + ( index * send_bufs->size ) );
  517. }
  518. /**
  519. * Create send work queue
  520. *
  521. * @v ibdev Infiniband device
  522. * @v qp Queue pair
  523. */
  524. static int qib7322_create_send_wq ( struct ib_device *ibdev,
  525. struct ib_queue_pair *qp ) {
  526. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  527. struct ib_work_queue *wq = &qp->send;
  528. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  529. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  530. /* Select send buffer set */
  531. if ( qp->type == IB_QPT_SMI ) {
  532. if ( port == 0 ) {
  533. qib7322_wq->send_bufs = qib7322->send_bufs_vl15_port0;
  534. } else {
  535. qib7322_wq->send_bufs = qib7322->send_bufs_vl15_port1;
  536. }
  537. } else {
  538. qib7322_wq->send_bufs = qib7322->send_bufs_small;
  539. }
  540. /* Allocate space for send buffer usage list */
  541. qib7322_wq->used = zalloc ( qp->send.num_wqes *
  542. sizeof ( qib7322_wq->used[0] ) );
  543. if ( ! qib7322_wq->used )
  544. return -ENOMEM;
  545. /* Reset work queue */
  546. qib7322_wq->prod = 0;
  547. qib7322_wq->cons = 0;
  548. return 0;
  549. }
  550. /**
  551. * Destroy send work queue
  552. *
  553. * @v ibdev Infiniband device
  554. * @v qp Queue pair
  555. */
  556. static void qib7322_destroy_send_wq ( struct ib_device *ibdev __unused,
  557. struct ib_queue_pair *qp ) {
  558. struct ib_work_queue *wq = &qp->send;
  559. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  560. free ( qib7322_wq->used );
  561. }
  562. /**
  563. * Initialise send datapath
  564. *
  565. * @v qib7322 QIB7322 device
  566. * @ret rc Return status code
  567. */
  568. static int qib7322_init_send ( struct qib7322 *qib7322 ) {
  569. struct QIB_7322_SendBufBase sendbufbase;
  570. struct QIB_7322_SendBufAvailAddr sendbufavailaddr;
  571. struct QIB_7322_SendCtrl sendctrl;
  572. struct QIB_7322_SendCtrl_0 sendctrlp;
  573. unsigned long baseaddr_smallpio;
  574. unsigned long baseaddr_largepio;
  575. unsigned long baseaddr_vl15_port0;
  576. unsigned long baseaddr_vl15_port1;
  577. int rc;
  578. /* Create send buffer sets */
  579. qib7322_readq ( qib7322, &sendbufbase, QIB_7322_SendBufBase_offset );
  580. baseaddr_smallpio = BIT_GET ( &sendbufbase, BaseAddr_SmallPIO );
  581. baseaddr_largepio = BIT_GET ( &sendbufbase, BaseAddr_LargePIO );
  582. baseaddr_vl15_port0 = ( baseaddr_largepio +
  583. ( QIB7322_LARGE_SEND_BUF_SIZE *
  584. QIB7322_LARGE_SEND_BUF_COUNT ) );
  585. baseaddr_vl15_port1 = ( baseaddr_vl15_port0 +
  586. QIB7322_VL15_PORT0_SEND_BUF_SIZE );
  587. qib7322->send_bufs_small =
  588. qib7322_create_send_bufs ( qib7322, baseaddr_smallpio,
  589. QIB7322_SMALL_SEND_BUF_SIZE,
  590. QIB7322_SMALL_SEND_BUF_START,
  591. QIB7322_SMALL_SEND_BUF_USED );
  592. if ( ! qib7322->send_bufs_small ) {
  593. rc = -ENOMEM;
  594. goto err_create_send_bufs_small;
  595. }
  596. qib7322->send_bufs_vl15_port0 =
  597. qib7322_create_send_bufs ( qib7322, baseaddr_vl15_port0,
  598. QIB7322_VL15_PORT0_SEND_BUF_SIZE,
  599. QIB7322_VL15_PORT0_SEND_BUF_START,
  600. QIB7322_VL15_PORT0_SEND_BUF_COUNT );
  601. if ( ! qib7322->send_bufs_vl15_port0 ) {
  602. rc = -ENOMEM;
  603. goto err_create_send_bufs_vl15_port0;
  604. }
  605. qib7322->send_bufs_vl15_port1 =
  606. qib7322_create_send_bufs ( qib7322, baseaddr_vl15_port1,
  607. QIB7322_VL15_PORT1_SEND_BUF_SIZE,
  608. QIB7322_VL15_PORT1_SEND_BUF_START,
  609. QIB7322_VL15_PORT1_SEND_BUF_COUNT );
  610. if ( ! qib7322->send_bufs_vl15_port1 ) {
  611. rc = -ENOMEM;
  612. goto err_create_send_bufs_vl15_port1;
  613. }
  614. /* Allocate space for the SendBufAvail array */
  615. qib7322->sendbufavail = malloc_dma ( sizeof ( *qib7322->sendbufavail ),
  616. QIB7322_SENDBUFAVAIL_ALIGN );
  617. if ( ! qib7322->sendbufavail ) {
  618. rc = -ENOMEM;
  619. goto err_alloc_sendbufavail;
  620. }
  621. memset ( qib7322->sendbufavail, 0, sizeof ( qib7322->sendbufavail ) );
  622. /* Program SendBufAvailAddr into the hardware */
  623. memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
  624. BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
  625. ( virt_to_bus ( qib7322->sendbufavail ) >> 6 ) );
  626. qib7322_writeq ( qib7322, &sendbufavailaddr,
  627. QIB_7322_SendBufAvailAddr_offset );
  628. /* Enable sending */
  629. memset ( &sendctrlp, 0, sizeof ( sendctrlp ) );
  630. BIT_FILL_1 ( &sendctrlp, SendEnable, 1 );
  631. qib7322_writeq ( qib7322, &sendctrlp, QIB_7322_SendCtrl_0_offset );
  632. qib7322_writeq ( qib7322, &sendctrlp, QIB_7322_SendCtrl_1_offset );
  633. /* Enable DMA of SendBufAvail */
  634. memset ( &sendctrl, 0, sizeof ( sendctrl ) );
  635. BIT_FILL_1 ( &sendctrl, SendBufAvailUpd, 1 );
  636. qib7322_writeq ( qib7322, &sendctrl, QIB_7322_SendCtrl_offset );
  637. return 0;
  638. free_dma ( qib7322->sendbufavail, sizeof ( *qib7322->sendbufavail ) );
  639. err_alloc_sendbufavail:
  640. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port1 );
  641. err_create_send_bufs_vl15_port1:
  642. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port0 );
  643. err_create_send_bufs_vl15_port0:
  644. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_small );
  645. err_create_send_bufs_small:
  646. return rc;
  647. }
  648. /**
  649. * Shut down send datapath
  650. *
  651. * @v qib7322 QIB7322 device
  652. */
  653. static void qib7322_fini_send ( struct qib7322 *qib7322 ) {
  654. struct QIB_7322_SendCtrl sendctrl;
  655. /* Disable sending and DMA of SendBufAvail */
  656. memset ( &sendctrl, 0, sizeof ( sendctrl ) );
  657. qib7322_writeq ( qib7322, &sendctrl, QIB_7322_SendCtrl_offset );
  658. mb();
  659. /* Ensure hardware has seen this disable */
  660. qib7322_readq ( qib7322, &sendctrl, QIB_7322_SendCtrl_offset );
  661. free_dma ( qib7322->sendbufavail, sizeof ( *qib7322->sendbufavail ) );
  662. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port1 );
  663. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_vl15_port0 );
  664. qib7322_destroy_send_bufs ( qib7322, qib7322->send_bufs_small );
  665. }
  666. /***************************************************************************
  667. *
  668. * Receive datapath
  669. *
  670. ***************************************************************************
  671. */
  672. /**
  673. * Create receive work queue
  674. *
  675. * @v ibdev Infiniband device
  676. * @v qp Queue pair
  677. * @ret rc Return status code
  678. */
  679. static int qib7322_create_recv_wq ( struct ib_device *ibdev,
  680. struct ib_queue_pair *qp ) {
  681. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  682. struct ib_work_queue *wq = &qp->recv;
  683. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  684. struct QIB_7322_RcvHdrAddr0 rcvhdraddr;
  685. struct QIB_7322_RcvHdrTailAddr0 rcvhdrtailaddr;
  686. struct QIB_7322_RcvHdrHead0 rcvhdrhead;
  687. struct QIB_7322_scalar rcvegrindexhead;
  688. struct QIB_7322_RcvCtrl rcvctrl;
  689. struct QIB_7322_RcvCtrl_P rcvctrlp;
  690. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  691. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  692. int rc;
  693. /* Reset context information */
  694. memset ( &qib7322_wq->header_prod, 0,
  695. sizeof ( qib7322_wq->header_prod ) );
  696. qib7322_wq->header_cons = 0;
  697. qib7322_wq->eager_prod = 0;
  698. qib7322_wq->eager_cons = 0;
  699. /* Allocate receive header buffer */
  700. qib7322_wq->header = malloc_dma ( QIB7322_RECV_HEADERS_SIZE,
  701. QIB7322_RECV_HEADERS_ALIGN );
  702. if ( ! qib7322_wq->header ) {
  703. rc = -ENOMEM;
  704. goto err_alloc_header;
  705. }
  706. /* Enable context in hardware */
  707. memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
  708. BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr,
  709. ( virt_to_bus ( qib7322_wq->header ) >> 2 ) );
  710. qib7322_writeq_array8b ( qib7322, &rcvhdraddr,
  711. QIB_7322_RcvHdrAddr0_offset, ctx );
  712. memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
  713. BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr,
  714. ( virt_to_bus ( &qib7322_wq->header_prod ) >> 2 ) );
  715. qib7322_writeq_array8b ( qib7322, &rcvhdrtailaddr,
  716. QIB_7322_RcvHdrTailAddr0_offset, ctx );
  717. memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
  718. BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
  719. qib7322_writeq_array64k ( qib7322, &rcvhdrhead,
  720. QIB_7322_RcvHdrHead0_offset, ctx );
  721. memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
  722. BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
  723. qib7322_writeq_array64k ( qib7322, &rcvegrindexhead,
  724. QIB_7322_RcvEgrIndexHead0_offset, ctx );
  725. qib7322_readq_port ( qib7322, &rcvctrlp,
  726. QIB_7322_RcvCtrl_0_offset, port );
  727. BIT_SET ( &rcvctrlp, ContextEnable[ctx], 1 );
  728. qib7322_writeq_port ( qib7322, &rcvctrlp,
  729. QIB_7322_RcvCtrl_0_offset, port );
  730. qib7322_readq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  731. BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
  732. qib7322_writeq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  733. DBGC ( qib7322, "QIB7322 %p port %d QPN %ld CTX %d hdrs [%lx,%lx) prod "
  734. "%lx\n", qib7322, port, qp->qpn, ctx,
  735. virt_to_bus ( qib7322_wq->header ),
  736. ( virt_to_bus ( qib7322_wq->header )
  737. + QIB7322_RECV_HEADERS_SIZE ),
  738. virt_to_bus ( &qib7322_wq->header_prod ) );
  739. return 0;
  740. free_dma ( qib7322_wq->header, QIB7322_RECV_HEADERS_SIZE );
  741. err_alloc_header:
  742. return rc;
  743. }
  744. /**
  745. * Destroy receive work queue
  746. *
  747. * @v ibdev Infiniband device
  748. * @v qp Queue pair
  749. */
  750. static void qib7322_destroy_recv_wq ( struct ib_device *ibdev,
  751. struct ib_queue_pair *qp ) {
  752. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  753. struct ib_work_queue *wq = &qp->recv;
  754. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  755. struct QIB_7322_RcvCtrl rcvctrl;
  756. struct QIB_7322_RcvCtrl_P rcvctrlp;
  757. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  758. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  759. /* Disable context in hardware */
  760. qib7322_readq_port ( qib7322, &rcvctrlp,
  761. QIB_7322_RcvCtrl_0_offset, port );
  762. BIT_SET ( &rcvctrlp, ContextEnable[ctx], 0 );
  763. qib7322_writeq_port ( qib7322, &rcvctrlp,
  764. QIB_7322_RcvCtrl_0_offset, port );
  765. qib7322_readq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  766. BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
  767. qib7322_writeq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  768. /* Make sure the hardware has seen that the context is disabled */
  769. qib7322_readq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  770. mb();
  771. /* Free headers ring */
  772. free_dma ( qib7322_wq->header, QIB7322_RECV_HEADERS_SIZE );
  773. }
  774. /**
  775. * Initialise receive datapath
  776. *
  777. * @v qib7322 QIB7322 device
  778. * @ret rc Return status code
  779. */
  780. static int qib7322_init_recv ( struct qib7322 *qib7322 ) {
  781. struct QIB_7322_RcvCtrl rcvctrl;
  782. struct QIB_7322_RcvCtrl_0 rcvctrlp;
  783. struct QIB_7322_RcvQPMapTableA_0 rcvqpmaptablea0;
  784. struct QIB_7322_RcvQPMapTableB_0 rcvqpmaptableb0;
  785. struct QIB_7322_RcvQPMapTableA_1 rcvqpmaptablea1;
  786. struct QIB_7322_RcvQPMapTableB_1 rcvqpmaptableb1;
  787. struct QIB_7322_RcvQPMulticastContext_0 rcvqpmcastctx0;
  788. struct QIB_7322_RcvQPMulticastContext_1 rcvqpmcastctx1;
  789. struct QIB_7322_scalar rcvegrbase;
  790. struct QIB_7322_scalar rcvhdrentsize;
  791. struct QIB_7322_scalar rcvhdrcnt;
  792. struct QIB_7322_RcvBTHQP_0 rcvbthqp;
  793. struct QIB_7322_RxCreditVL0_0 rxcreditvl;
  794. unsigned int contextcfg;
  795. unsigned long egrbase;
  796. unsigned int eager_array_size_kernel;
  797. unsigned int eager_array_size_user;
  798. unsigned int user_context_mask;
  799. unsigned int ctx;
  800. /* Select configuration based on number of contexts */
  801. switch ( QIB7322_NUM_CONTEXTS ) {
  802. case 6:
  803. contextcfg = QIB7322_CONTEXTCFG_6CTX;
  804. eager_array_size_kernel = QIB7322_EAGER_ARRAY_SIZE_6CTX_KERNEL;
  805. eager_array_size_user = QIB7322_EAGER_ARRAY_SIZE_6CTX_USER;
  806. user_context_mask = 0x000f;
  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. user_context_mask = 0x00ff;
  813. break;
  814. case 18:
  815. contextcfg = QIB7322_CONTEXTCFG_18CTX;
  816. eager_array_size_kernel = QIB7322_EAGER_ARRAY_SIZE_18CTX_KERNEL;
  817. eager_array_size_user = QIB7322_EAGER_ARRAY_SIZE_18CTX_USER;
  818. user_context_mask = 0xffff;
  819. break;
  820. default:
  821. linker_assert ( 0, invalid_QIB7322_NUM_CONTEXTS );
  822. return -EINVAL;
  823. }
  824. /* Configure number of contexts */
  825. memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
  826. BIT_FILL_2 ( &rcvctrl,
  827. TailUpd, 1,
  828. ContextCfg, contextcfg );
  829. qib7322_writeq ( qib7322, &rcvctrl, QIB_7322_RcvCtrl_offset );
  830. /* Map QPNs to contexts */
  831. memset ( &rcvctrlp, 0, sizeof ( rcvctrlp ) );
  832. BIT_FILL_3 ( &rcvctrlp,
  833. RcvIBPortEnable, 1,
  834. RcvQPMapEnable, 1,
  835. RcvPartitionKeyDisable, 1 );
  836. qib7322_writeq ( qib7322, &rcvctrlp, QIB_7322_RcvCtrl_0_offset );
  837. qib7322_writeq ( qib7322, &rcvctrlp, QIB_7322_RcvCtrl_1_offset );
  838. memset ( &rcvqpmaptablea0, 0, sizeof ( rcvqpmaptablea0 ) );
  839. BIT_FILL_6 ( &rcvqpmaptablea0,
  840. RcvQPMapContext0, 0,
  841. RcvQPMapContext1, 2,
  842. RcvQPMapContext2, 4,
  843. RcvQPMapContext3, 6,
  844. RcvQPMapContext4, 8,
  845. RcvQPMapContext5, 10 );
  846. qib7322_writeq ( qib7322, &rcvqpmaptablea0,
  847. QIB_7322_RcvQPMapTableA_0_offset );
  848. memset ( &rcvqpmaptableb0, 0, sizeof ( rcvqpmaptableb0 ) );
  849. BIT_FILL_3 ( &rcvqpmaptableb0,
  850. RcvQPMapContext6, 12,
  851. RcvQPMapContext7, 14,
  852. RcvQPMapContext8, 16 );
  853. qib7322_writeq ( qib7322, &rcvqpmaptableb0,
  854. QIB_7322_RcvQPMapTableB_0_offset );
  855. memset ( &rcvqpmaptablea1, 0, sizeof ( rcvqpmaptablea1 ) );
  856. BIT_FILL_6 ( &rcvqpmaptablea1,
  857. RcvQPMapContext0, 1,
  858. RcvQPMapContext1, 3,
  859. RcvQPMapContext2, 5,
  860. RcvQPMapContext3, 7,
  861. RcvQPMapContext4, 9,
  862. RcvQPMapContext5, 11 );
  863. qib7322_writeq ( qib7322, &rcvqpmaptablea1,
  864. QIB_7322_RcvQPMapTableA_1_offset );
  865. memset ( &rcvqpmaptableb1, 0, sizeof ( rcvqpmaptableb1 ) );
  866. BIT_FILL_3 ( &rcvqpmaptableb1,
  867. RcvQPMapContext6, 13,
  868. RcvQPMapContext7, 15,
  869. RcvQPMapContext8, 17 );
  870. qib7322_writeq ( qib7322, &rcvqpmaptableb1,
  871. QIB_7322_RcvQPMapTableB_1_offset );
  872. /* Map multicast QPNs to contexts */
  873. memset ( &rcvqpmcastctx0, 0, sizeof ( rcvqpmcastctx0 ) );
  874. BIT_FILL_1 ( &rcvqpmcastctx0, RcvQpMcContext, 0 );
  875. qib7322_writeq ( qib7322, &rcvqpmcastctx0,
  876. QIB_7322_RcvQPMulticastContext_0_offset );
  877. memset ( &rcvqpmcastctx1, 0, sizeof ( rcvqpmcastctx1 ) );
  878. BIT_FILL_1 ( &rcvqpmcastctx1, RcvQpMcContext, 1 );
  879. qib7322_writeq ( qib7322, &rcvqpmcastctx1,
  880. QIB_7322_RcvQPMulticastContext_1_offset );
  881. /* Configure receive header buffer sizes */
  882. memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
  883. BIT_FILL_1 ( &rcvhdrcnt, Value, QIB7322_RECV_HEADER_COUNT );
  884. qib7322_writeq ( qib7322, &rcvhdrcnt, QIB_7322_RcvHdrCnt_offset );
  885. memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
  886. BIT_FILL_1 ( &rcvhdrentsize, Value, ( QIB7322_RECV_HEADER_SIZE >> 2 ) );
  887. qib7322_writeq ( qib7322, &rcvhdrentsize,
  888. QIB_7322_RcvHdrEntSize_offset );
  889. /* Calculate eager array start addresses for each context */
  890. qib7322_readq ( qib7322, &rcvegrbase, QIB_7322_RcvEgrBase_offset );
  891. egrbase = BIT_GET ( &rcvegrbase, Value );
  892. for ( ctx = 0 ; ctx < QIB7322_MAX_PORTS ; ctx++ ) {
  893. qib7322->recv_wq[ctx].eager_array = egrbase;
  894. qib7322->recv_wq[ctx].eager_entries = eager_array_size_kernel;
  895. egrbase += ( eager_array_size_kernel *
  896. sizeof ( struct QIB_7322_RcvEgr ) );
  897. }
  898. for ( ; ctx < QIB7322_NUM_CONTEXTS ; ctx++ ) {
  899. qib7322->recv_wq[ctx].eager_array = egrbase;
  900. qib7322->recv_wq[ctx].eager_entries = eager_array_size_user;
  901. egrbase += ( eager_array_size_user *
  902. sizeof ( struct QIB_7322_RcvEgr ) );
  903. }
  904. for ( ctx = 0 ; ctx < QIB7322_NUM_CONTEXTS ; ctx++ ) {
  905. DBGC ( qib7322, "QIB7322 %p CTX %d eager array at %lx (%d "
  906. "entries)\n", qib7322, ctx,
  907. qib7322->recv_wq[ctx].eager_array,
  908. qib7322->recv_wq[ctx].eager_entries );
  909. }
  910. /* Set the BTH QP for Infinipath packets to an unused value */
  911. memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
  912. BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, QIB7322_QP_IDETH );
  913. qib7322_writeq ( qib7322, &rcvbthqp, QIB_7322_RcvBTHQP_0_offset );
  914. qib7322_writeq ( qib7322, &rcvbthqp, QIB_7322_RcvBTHQP_1_offset );
  915. /* Assign initial credits */
  916. memset ( &rxcreditvl, 0, sizeof ( rxcreditvl ) );
  917. BIT_FILL_1 ( &rxcreditvl, RxMaxCreditVL, QIB7322_MAX_CREDITS_VL0 );
  918. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  919. QIB_7322_RxCreditVL0_0_offset, 0 );
  920. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  921. QIB_7322_RxCreditVL0_1_offset, 0 );
  922. BIT_FILL_1 ( &rxcreditvl, RxMaxCreditVL, QIB7322_MAX_CREDITS_VL15 );
  923. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  924. QIB_7322_RxCreditVL0_0_offset, 15 );
  925. qib7322_writeq_array8b ( qib7322, &rxcreditvl,
  926. QIB_7322_RxCreditVL0_1_offset, 15 );
  927. return 0;
  928. }
  929. /**
  930. * Shut down receive datapath
  931. *
  932. * @v qib7322 QIB7322 device
  933. */
  934. static void qib7322_fini_recv ( struct qib7322 *qib7322 __unused ) {
  935. /* Nothing to do; all contexts were already disabled when the
  936. * queue pairs were destroyed
  937. */
  938. }
  939. /***************************************************************************
  940. *
  941. * Completion queue operations
  942. *
  943. ***************************************************************************
  944. */
  945. /**
  946. * Create completion queue
  947. *
  948. * @v ibdev Infiniband device
  949. * @v cq Completion queue
  950. * @ret rc Return status code
  951. */
  952. static int qib7322_create_cq ( struct ib_device *ibdev,
  953. struct ib_completion_queue *cq ) {
  954. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  955. static int cqn;
  956. /* The hardware has no concept of completion queues. We
  957. * simply use the association between CQs and WQs (already
  958. * handled by the IB core) to decide which WQs to poll.
  959. *
  960. * We do set a CQN, just to avoid confusing debug messages
  961. * from the IB core.
  962. */
  963. cq->cqn = ++cqn;
  964. DBGC ( qib7322, "QIB7322 %p CQN %ld created\n", qib7322, cq->cqn );
  965. return 0;
  966. }
  967. /**
  968. * Destroy completion queue
  969. *
  970. * @v ibdev Infiniband device
  971. * @v cq Completion queue
  972. */
  973. static void qib7322_destroy_cq ( struct ib_device *ibdev,
  974. struct ib_completion_queue *cq ) {
  975. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  976. /* Nothing to do */
  977. DBGC ( qib7322, "QIB7322 %p CQN %ld destroyed\n", qib7322, cq->cqn );
  978. }
  979. /***************************************************************************
  980. *
  981. * Queue pair operations
  982. *
  983. ***************************************************************************
  984. */
  985. /**
  986. * Create queue pair
  987. *
  988. * @v ibdev Infiniband device
  989. * @v qp Queue pair
  990. * @ret rc Return status code
  991. */
  992. static int qib7322_create_qp ( struct ib_device *ibdev,
  993. struct ib_queue_pair *qp ) {
  994. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  995. unsigned int ctx;
  996. int rc;
  997. /* Allocate a context and QPN */
  998. if ( ( rc = qib7322_alloc_ctx ( ibdev, qp ) ) != 0 )
  999. goto err_alloc_ctx;
  1000. ctx = qib7322_ctx ( ibdev, qp );
  1001. /* Set work-queue private data pointers */
  1002. ib_wq_set_drvdata ( &qp->send, &qib7322->send_wq[ctx] );
  1003. ib_wq_set_drvdata ( &qp->recv, &qib7322->recv_wq[ctx] );
  1004. /* Create receive work queue */
  1005. if ( ( rc = qib7322_create_recv_wq ( ibdev, qp ) ) != 0 )
  1006. goto err_create_recv_wq;
  1007. /* Create send work queue */
  1008. if ( ( rc = qib7322_create_send_wq ( ibdev, qp ) ) != 0 )
  1009. goto err_create_send_wq;
  1010. return 0;
  1011. qib7322_destroy_send_wq ( ibdev, qp );
  1012. err_create_send_wq:
  1013. qib7322_destroy_recv_wq ( ibdev, qp );
  1014. err_create_recv_wq:
  1015. qib7322_free_ctx ( ibdev, qp );
  1016. err_alloc_ctx:
  1017. return rc;
  1018. }
  1019. /**
  1020. * Modify queue pair
  1021. *
  1022. * @v ibdev Infiniband device
  1023. * @v qp Queue pair
  1024. * @ret rc Return status code
  1025. */
  1026. static int qib7322_modify_qp ( struct ib_device *ibdev,
  1027. struct ib_queue_pair *qp ) {
  1028. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1029. /* Nothing to do; the hardware doesn't have a notion of queue
  1030. * keys
  1031. */
  1032. DBGC2 ( qib7322, "QIB7322 %p QPN %ld modified\n", qib7322, qp->qpn );
  1033. return 0;
  1034. }
  1035. /**
  1036. * Destroy queue pair
  1037. *
  1038. * @v ibdev Infiniband device
  1039. * @v qp Queue pair
  1040. */
  1041. static void qib7322_destroy_qp ( struct ib_device *ibdev,
  1042. struct ib_queue_pair *qp ) {
  1043. qib7322_destroy_send_wq ( ibdev, qp );
  1044. qib7322_destroy_recv_wq ( ibdev, qp );
  1045. qib7322_free_ctx ( ibdev, qp );
  1046. }
  1047. /***************************************************************************
  1048. *
  1049. * Work request operations
  1050. *
  1051. ***************************************************************************
  1052. */
  1053. /**
  1054. * Post send work queue entry
  1055. *
  1056. * @v ibdev Infiniband device
  1057. * @v qp Queue pair
  1058. * @v av Address vector
  1059. * @v iobuf I/O buffer
  1060. * @ret rc Return status code
  1061. */
  1062. static int qib7322_post_send ( struct ib_device *ibdev,
  1063. struct ib_queue_pair *qp,
  1064. struct ib_address_vector *av,
  1065. struct io_buffer *iobuf ) {
  1066. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1067. struct ib_work_queue *wq = &qp->send;
  1068. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1069. struct QIB_7322_SendPbc sendpbc;
  1070. unsigned int port = ( ibdev->port - QIB7322_PORT_BASE );
  1071. uint8_t header_buf[IB_MAX_HEADER_SIZE];
  1072. struct io_buffer headers;
  1073. int send_buf;
  1074. unsigned long start_offset;
  1075. unsigned long offset;
  1076. size_t len;
  1077. ssize_t frag_len;
  1078. uint32_t *data;
  1079. /* Allocate send buffer and calculate offset */
  1080. send_buf = qib7322_alloc_send_buf ( qib7322, qib7322_wq->send_bufs );
  1081. if ( send_buf < 0 )
  1082. return send_buf;
  1083. start_offset = offset =
  1084. qib7322_send_buffer_offset ( qib7322, qib7322_wq->send_bufs,
  1085. send_buf );
  1086. /* Store I/O buffer and send buffer index */
  1087. assert ( wq->iobufs[qib7322_wq->prod] == NULL );
  1088. wq->iobufs[qib7322_wq->prod] = iobuf;
  1089. qib7322_wq->used[qib7322_wq->prod] = send_buf;
  1090. /* Construct headers */
  1091. iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
  1092. iob_reserve ( &headers, sizeof ( header_buf ) );
  1093. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), av );
  1094. /* Calculate packet length */
  1095. len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
  1096. iob_len ( iobuf ) + 3 ) & ~3 );
  1097. /* Construct send per-buffer control word */
  1098. memset ( &sendpbc, 0, sizeof ( sendpbc ) );
  1099. BIT_FILL_3 ( &sendpbc,
  1100. LengthP1_toibc, ( ( len >> 2 ) - 1 ),
  1101. Port, port,
  1102. VL15, ( ( qp->type == IB_QPT_SMI ) ? 1 : 0 ) );
  1103. /* Write SendPbc */
  1104. DBG_DISABLE ( DBGLVL_IO );
  1105. qib7322_writeq ( qib7322, &sendpbc, offset );
  1106. offset += sizeof ( sendpbc );
  1107. /* Write headers */
  1108. for ( data = headers.data, frag_len = iob_len ( &headers ) ;
  1109. frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
  1110. qib7322_writel ( qib7322, *data, offset );
  1111. }
  1112. /* Write data */
  1113. for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
  1114. frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
  1115. qib7322_writel ( qib7322, *data, offset );
  1116. }
  1117. DBG_ENABLE ( DBGLVL_IO );
  1118. assert ( ( start_offset + len ) == offset );
  1119. DBGC2 ( qib7322, "QIB7322 %p QPN %ld TX %04x(%04x) posted [%lx,%lx)\n",
  1120. qib7322, qp->qpn, send_buf, qib7322_wq->prod,
  1121. start_offset, offset );
  1122. /* Increment producer counter */
  1123. qib7322_wq->prod = ( ( qib7322_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
  1124. return 0;
  1125. }
  1126. /**
  1127. * Complete send work queue entry
  1128. *
  1129. * @v ibdev Infiniband device
  1130. * @v qp Queue pair
  1131. * @v wqe_idx Work queue entry index
  1132. */
  1133. static void qib7322_complete_send ( struct ib_device *ibdev,
  1134. struct ib_queue_pair *qp,
  1135. unsigned int wqe_idx ) {
  1136. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1137. struct ib_work_queue *wq = &qp->send;
  1138. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1139. struct io_buffer *iobuf;
  1140. unsigned int send_buf;
  1141. /* Parse completion */
  1142. send_buf = qib7322_wq->used[wqe_idx];
  1143. DBGC2 ( qib7322, "QIB7322 %p QPN %ld TX %04x(%04x) complete\n",
  1144. qib7322, qp->qpn, send_buf, wqe_idx );
  1145. /* Complete work queue entry */
  1146. iobuf = wq->iobufs[wqe_idx];
  1147. assert ( iobuf != NULL );
  1148. ib_complete_send ( ibdev, qp, iobuf, 0 );
  1149. wq->iobufs[wqe_idx] = NULL;
  1150. /* Free send buffer */
  1151. qib7322_free_send_buf ( qib7322, qib7322_wq->send_bufs, send_buf );
  1152. }
  1153. /**
  1154. * Poll send work queue
  1155. *
  1156. * @v ibdev Infiniband device
  1157. * @v qp Queue pair
  1158. */
  1159. static void qib7322_poll_send_wq ( struct ib_device *ibdev,
  1160. struct ib_queue_pair *qp ) {
  1161. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1162. struct ib_work_queue *wq = &qp->send;
  1163. struct qib7322_send_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1164. unsigned int send_buf;
  1165. /* Look for completions */
  1166. while ( wq->fill ) {
  1167. /* Check to see if send buffer has completed */
  1168. send_buf = qib7322_wq->used[qib7322_wq->cons];
  1169. if ( qib7322_send_buf_in_use ( qib7322, send_buf ) )
  1170. break;
  1171. /* Complete this buffer */
  1172. qib7322_complete_send ( ibdev, qp, qib7322_wq->cons );
  1173. /* Increment consumer counter */
  1174. qib7322_wq->cons = ( ( qib7322_wq->cons + 1 ) &
  1175. ( wq->num_wqes - 1 ) );
  1176. }
  1177. }
  1178. /**
  1179. * Post receive work queue entry
  1180. *
  1181. * @v ibdev Infiniband device
  1182. * @v qp Queue pair
  1183. * @v iobuf I/O buffer
  1184. * @ret rc Return status code
  1185. */
  1186. static int qib7322_post_recv ( struct ib_device *ibdev,
  1187. struct ib_queue_pair *qp,
  1188. struct io_buffer *iobuf ) {
  1189. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1190. struct ib_work_queue *wq = &qp->recv;
  1191. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1192. struct QIB_7322_RcvEgr rcvegr;
  1193. struct QIB_7322_scalar rcvegrindexhead;
  1194. unsigned int ctx = qib7322_ctx ( ibdev, qp );
  1195. physaddr_t addr;
  1196. size_t len;
  1197. unsigned int wqe_idx;
  1198. unsigned int bufsize;
  1199. /* Sanity checks */
  1200. addr = virt_to_bus ( iobuf->data );
  1201. len = iob_tailroom ( iobuf );
  1202. if ( addr & ( QIB7322_EAGER_BUFFER_ALIGN - 1 ) ) {
  1203. DBGC ( qib7322, "QIB7322 %p QPN %ld misaligned RX buffer "
  1204. "(%08lx)\n", qib7322, qp->qpn, addr );
  1205. return -EINVAL;
  1206. }
  1207. if ( len != QIB7322_RECV_PAYLOAD_SIZE ) {
  1208. DBGC ( qib7322, "QIB7322 %p QPN %ld wrong RX buffer size "
  1209. "(%zd)\n", qib7322, qp->qpn, len );
  1210. return -EINVAL;
  1211. }
  1212. /* Calculate eager producer index and WQE index */
  1213. wqe_idx = ( qib7322_wq->eager_prod & ( wq->num_wqes - 1 ) );
  1214. assert ( wq->iobufs[wqe_idx] == NULL );
  1215. /* Store I/O buffer */
  1216. wq->iobufs[wqe_idx] = iobuf;
  1217. /* Calculate buffer size */
  1218. switch ( QIB7322_RECV_PAYLOAD_SIZE ) {
  1219. case 2048: bufsize = QIB7322_EAGER_BUFFER_2K; break;
  1220. case 4096: bufsize = QIB7322_EAGER_BUFFER_4K; break;
  1221. case 8192: bufsize = QIB7322_EAGER_BUFFER_8K; break;
  1222. case 16384: bufsize = QIB7322_EAGER_BUFFER_16K; break;
  1223. case 32768: bufsize = QIB7322_EAGER_BUFFER_32K; break;
  1224. case 65536: bufsize = QIB7322_EAGER_BUFFER_64K; break;
  1225. default: linker_assert ( 0, invalid_rx_payload_size );
  1226. bufsize = QIB7322_EAGER_BUFFER_NONE;
  1227. }
  1228. /* Post eager buffer */
  1229. memset ( &rcvegr, 0, sizeof ( rcvegr ) );
  1230. BIT_FILL_2 ( &rcvegr,
  1231. Addr, ( addr >> 11 ),
  1232. BufSize, bufsize );
  1233. qib7322_writeq_array8b ( qib7322, &rcvegr, qib7322_wq->eager_array,
  1234. qib7322_wq->eager_prod );
  1235. DBGC2 ( qib7322, "QIB7322 %p QPN %ld RX egr %04x(%04x) posted "
  1236. "[%lx,%lx)\n", qib7322, qp->qpn, qib7322_wq->eager_prod,
  1237. wqe_idx, addr, ( addr + len ) );
  1238. /* Increment producer index */
  1239. qib7322_wq->eager_prod = ( ( qib7322_wq->eager_prod + 1 ) &
  1240. ( qib7322_wq->eager_entries - 1 ) );
  1241. /* Update head index */
  1242. memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
  1243. BIT_FILL_1 ( &rcvegrindexhead,
  1244. Value, ( ( qib7322_wq->eager_prod + 1 ) &
  1245. ( qib7322_wq->eager_entries - 1 ) ) );
  1246. qib7322_writeq_array64k ( qib7322, &rcvegrindexhead,
  1247. QIB_7322_RcvEgrIndexHead0_offset, ctx );
  1248. return 0;
  1249. }
  1250. /**
  1251. * Complete receive work queue entry
  1252. *
  1253. * @v ibdev Infiniband device
  1254. * @v qp Queue pair
  1255. * @v header_offs Header offset
  1256. */
  1257. static void qib7322_complete_recv ( struct ib_device *ibdev,
  1258. struct ib_queue_pair *qp,
  1259. unsigned int header_offs ) {
  1260. struct qib7322 *qib7322 = ib_get_drvdata ( ibdev );
  1261. struct ib_work_queue *wq = &qp->recv;
  1262. struct qib7322_recv_work_queue *qib7322_wq = ib_wq_get_drvdata ( wq );
  1263. struct QIB_7322_RcvHdrFlags *rcvhdrflags;
  1264. struct QIB_7322_RcvEgr rcvegr;
  1265. struct io_buffer headers;
  1266. struct io_buffer *iobuf;
  1267. struct ib_queue_pair *intended_qp;
  1268. struct ib_address_vector av;
  1269. unsigned int rcvtype;
  1270. unsigned int pktlen;
  1271. unsigned int egrindex;
  1272. unsigned int useegrbfr;
  1273. unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
  1274. unsigned int lenerr, parityerr, vcrcerr, icrcerr;
  1275. unsigned int err;
  1276. unsigned int hdrqoffset;
  1277. unsigned int header_len;
  1278. unsigned int padded_payload_len;
  1279. unsigned int wqe_idx;
  1280. size_t payload_len;
  1281. int qp0;
  1282. int rc;
  1283. /* RcvHdrFlags are at the end of the header entry */
  1284. rcvhdrflags = ( qib7322_wq->header + header_offs +
  1285. QIB7322_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
  1286. rcvtype = BIT_GET ( rcvhdrflags, RcvType );
  1287. pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
  1288. egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
  1289. useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
  1290. hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
  1291. iberr = BIT_GET ( rcvhdrflags, IBErr );
  1292. mkerr = BIT_GET ( rcvhdrflags, MKErr );
  1293. tiderr = BIT_GET ( rcvhdrflags, TIDErr );
  1294. khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
  1295. mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
  1296. lenerr = BIT_GET ( rcvhdrflags, LenErr );
  1297. parityerr = BIT_GET ( rcvhdrflags, ParityErr );
  1298. vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
  1299. icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
  1300. header_len = ( QIB7322_RECV_HEADER_SIZE - hdrqoffset -
  1301. sizeof ( *rcvhdrflags ) );
  1302. padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
  1303. err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
  1304. lenerr | parityerr | vcrcerr | icrcerr );
  1305. /* IB header is placed immediately before RcvHdrFlags */
  1306. iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
  1307. header_len, header_len );
  1308. /* Dump diagnostic information */
  1309. DBGC2 ( qib7322, "QIB7322 %p QPN %ld RX egr %04x%s hdr %d type %d len "
  1310. "%d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", qib7322, qp->qpn,
  1311. egrindex, ( useegrbfr ? "" : "(unused)" ),
  1312. ( header_offs / QIB7322_RECV_HEADER_SIZE ),
  1313. rcvtype, pktlen, header_len, padded_payload_len,
  1314. ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
  1315. ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
  1316. ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
  1317. ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
  1318. ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
  1319. ( err ? "]" : "" ) );
  1320. DBGCP_HDA ( qib7322, hdrqoffset, headers.data,
  1321. ( header_len + sizeof ( *rcvhdrflags ) ) );
  1322. /* Parse header to generate address vector */
  1323. qp0 = ( qp->qpn == 0 );
  1324. intended_qp = NULL;
  1325. if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
  1326. &payload_len, &av ) ) != 0 ) {
  1327. DBGC ( qib7322, "QIB7322 %p could not parse headers: %s\n",
  1328. qib7322, strerror ( rc ) );
  1329. err = 1;
  1330. }
  1331. if ( ! intended_qp )
  1332. intended_qp = qp;
  1333. /* Complete this buffer and any skipped buffers. Note that
  1334. * when the hardware runs out of buffers, it will repeatedly
  1335. * report the same buffer (the tail) as a TID error, and that
  1336. * it also has a habit of sometimes skipping over several
  1337. * buffers at once.
  1338. */
  1339. while ( 1 ) {
  1340. /* If we have caught up to the producer counter, stop.
  1341. * This will happen when the hardware first runs out
  1342. * of buffers and starts reporting TID errors against
  1343. * the eager buffer it wants to use next.
  1344. */
  1345. if ( qib7322_wq->eager_cons == qib7322_wq->eager_prod )
  1346. break;
  1347. /* If we have caught up to where we should be after
  1348. * completing this egrindex, stop. We phrase the test
  1349. * this way to avoid completing the entire ring when
  1350. * we receive the same egrindex twice in a row.
  1351. */
  1352. if ( ( qib7322_wq->eager_cons ==
  1353. ( ( egrindex + 1 ) & ( qib7322_wq->eager_entries - 1 ))))
  1354. break;
  1355. /* Identify work queue entry and corresponding I/O
  1356. * buffer.
  1357. */
  1358. wqe_idx = ( qib7322_wq->eager_cons & ( wq->num_wqes - 1 ) );
  1359. iobuf = wq->iobufs[wqe_idx];
  1360. assert ( iobuf != NULL );
  1361. wq->iobufs[wqe_idx] = NULL;
  1362. /* Complete the eager buffer */
  1363. if ( qib7322_wq->eager_cons == egrindex ) {
  1364. /* Completing the eager buffer described in
  1365. * this header entry.
  1366. */
  1367. iob_put ( iobuf, payload_len );
  1368. rc = ( err ? -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
  1369. /* Redirect to target QP if necessary */
  1370. if ( qp != intended_qp ) {
  1371. DBGC2 ( qib7322, "QIB7322 %p redirecting QPN "
  1372. "%ld => %ld\n",
  1373. qib7322, qp->qpn, intended_qp->qpn );
  1374. /* Compensate for incorrect fill levels */
  1375. qp->recv.fill--;
  1376. intended_qp->recv.fill++;
  1377. }
  1378. ib_complete_recv ( ibdev, intended_qp, &av, iobuf, rc);
  1379. } else {
  1380. /* Completing on a skipped-over eager buffer */
  1381. ib_complete_recv ( ibdev, qp, &av, iobuf, -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. struct 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. struct 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. struct ib_gid_half *guid = &qib7322->guid;
  1741. int rc;
  1742. /* Read GUID */
  1743. if ( ( rc = i2c->read ( i2c, &qib7322->eeprom,
  1744. QIB7322_EEPROM_GUID_OFFSET, guid->u.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 %02x:%02x:%02x:%02x:%02x:%02x:"
  1751. "%02x:%02x\n", qib7322, guid->u.bytes[0], guid->u.bytes[1],
  1752. guid->u.bytes[2], guid->u.bytes[3], guid->u.bytes[4],
  1753. guid->u.bytes[5], guid->u.bytes[6], guid->u.bytes[7] );
  1754. /* Read serial number (debug only) */
  1755. if ( DBG_LOG ) {
  1756. uint8_t serial[QIB7322_EEPROM_SERIAL_SIZE + 1];
  1757. serial[ sizeof ( serial ) - 1 ] = '\0';
  1758. if ( ( rc = i2c->read ( i2c, &qib7322->eeprom,
  1759. QIB7322_EEPROM_SERIAL_OFFSET, serial,
  1760. ( sizeof ( serial ) - 1 ) ) ) != 0 ) {
  1761. DBGC ( qib7322, "QIB7322 %p could not read serial: "
  1762. "%s\n", qib7322, strerror ( rc ) );
  1763. return rc;
  1764. }
  1765. DBGC2 ( qib7322, "QIB7322 %p has serial number \"%s\"\n",
  1766. qib7322, serial );
  1767. }
  1768. return 0;
  1769. }
  1770. /***************************************************************************
  1771. *
  1772. * Advanced High-performance Bus (AHB) access
  1773. *
  1774. ***************************************************************************
  1775. */
  1776. /**
  1777. * Wait for AHB transaction to complete
  1778. *
  1779. * @v qib7322 QIB7322 device
  1780. * @ret rc Return status code
  1781. */
  1782. static int qib7322_ahb_wait ( struct qib7322 *qib7322 ) {
  1783. struct QIB_7322_ahb_transaction_reg transaction;
  1784. unsigned int i;
  1785. /* Wait for Ready bit to be asserted */
  1786. for ( i = 0 ; i < QIB7322_AHB_MAX_WAIT_US ; i++ ) {
  1787. qib7322_readq ( qib7322, &transaction,
  1788. QIB_7322_ahb_transaction_reg_offset );
  1789. if ( BIT_GET ( &transaction, ahb_rdy ) )
  1790. return 0;
  1791. udelay ( 1 );
  1792. }
  1793. DBGC ( qib7322, "QIB7322 %p timed out waiting for AHB transaction\n",
  1794. qib7322 );
  1795. return -ETIMEDOUT;
  1796. }
  1797. /**
  1798. * Request ownership of the AHB
  1799. *
  1800. * @v qib7322 QIB7322 device
  1801. * @v location AHB location
  1802. * @ret rc Return status code
  1803. */
  1804. static int qib7322_ahb_request ( struct qib7322 *qib7322,
  1805. unsigned int location ) {
  1806. struct QIB_7322_ahb_access_ctrl access;
  1807. int rc;
  1808. /* Request ownership */
  1809. memset ( &access, 0, sizeof ( access ) );
  1810. BIT_FILL_2 ( &access,
  1811. sw_ahb_sel, 1,
  1812. sw_sel_ahb_trgt, QIB7322_AHB_LOC_TARGET ( location ) );
  1813. qib7322_writeq ( qib7322, &access, QIB_7322_ahb_access_ctrl_offset );
  1814. /* Wait for ownership to be granted */
  1815. if ( ( rc = qib7322_ahb_wait ( qib7322 ) ) != 0 ) {
  1816. DBGC ( qib7322, "QIB7322 %p could not obtain AHB ownership: "
  1817. "%s\n", qib7322, strerror ( rc ) );
  1818. return rc;
  1819. }
  1820. return 0;
  1821. }
  1822. /**
  1823. * Release ownership of the AHB
  1824. *
  1825. * @v qib7322 QIB7322 device
  1826. */
  1827. static void qib7322_ahb_release ( struct qib7322 *qib7322 ) {
  1828. struct QIB_7322_ahb_access_ctrl access;
  1829. memset ( &access, 0, sizeof ( access ) );
  1830. qib7322_writeq ( qib7322, &access, QIB_7322_ahb_access_ctrl_offset );
  1831. }
  1832. /**
  1833. * Read data via AHB
  1834. *
  1835. * @v qib7322 QIB7322 device
  1836. * @v location AHB location
  1837. * @v data Data to read
  1838. * @ret rc Return status code
  1839. *
  1840. * You must have already acquired ownership of the AHB.
  1841. */
  1842. static int qib7322_ahb_read ( struct qib7322 *qib7322, unsigned int location,
  1843. uint32_t *data ) {
  1844. struct QIB_7322_ahb_transaction_reg xact;
  1845. int rc;
  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. const struct pci_device_id *id __unused ) {
  2034. struct qib7322 *qib7322;
  2035. struct QIB_7322_Revision revision;
  2036. struct ib_device *ibdev;
  2037. unsigned int link_speed_supported;
  2038. int i;
  2039. int rc;
  2040. /* Allocate QIB7322 device */
  2041. qib7322 = zalloc ( sizeof ( *qib7322 ) );
  2042. if ( ! qib7322 ) {
  2043. rc = -ENOMEM;
  2044. goto err_alloc_qib7322;
  2045. }
  2046. pci_set_drvdata ( pci, qib7322 );
  2047. /* Fix up PCI device */
  2048. adjust_pci_device ( pci );
  2049. /* Get PCI BARs */
  2050. qib7322->regs = ioremap ( pci->membase, QIB7322_BAR0_SIZE );
  2051. DBGC2 ( qib7322, "QIB7322 %p has BAR at %08lx\n",
  2052. qib7322, pci->membase );
  2053. /* Reset device */
  2054. qib7322_reset ( qib7322, pci );
  2055. /* Print some general data */
  2056. qib7322_readq ( qib7322, &revision, QIB_7322_Revision_offset );
  2057. DBGC2 ( qib7322, "QIB7322 %p board %02lx v%ld.%ld.%ld.%ld\n", qib7322,
  2058. BIT_GET ( &revision, BoardID ),
  2059. BIT_GET ( &revision, R_SW ),
  2060. BIT_GET ( &revision, R_Arch ),
  2061. BIT_GET ( &revision, R_ChipRevMajor ),
  2062. BIT_GET ( &revision, R_ChipRevMinor ) );
  2063. /* Initialise I2C subsystem */
  2064. if ( ( rc = qib7322_init_i2c ( qib7322 ) ) != 0 )
  2065. goto err_init_i2c;
  2066. /* Read EEPROM parameters */
  2067. if ( ( rc = qib7322_read_eeprom ( qib7322 ) ) != 0 )
  2068. goto err_read_eeprom;
  2069. /* Initialise send datapath */
  2070. if ( ( rc = qib7322_init_send ( qib7322 ) ) != 0 )
  2071. goto err_init_send;
  2072. /* Initialise receive datapath */
  2073. if ( ( rc = qib7322_init_recv ( qib7322 ) ) != 0 )
  2074. goto err_init_recv;
  2075. /* Initialise the IB SerDes */
  2076. if ( ( rc = qib7322_init_ib_serdes ( qib7322 ) ) != 0 )
  2077. goto err_init_ib_serdes;
  2078. /* Allocate Infiniband devices */
  2079. for ( i = 0 ; i < QIB7322_MAX_PORTS ; i++ ) {
  2080. link_speed_supported =
  2081. qib7322_link_speed_supported ( qib7322, i );
  2082. if ( ! link_speed_supported )
  2083. continue;
  2084. ibdev = alloc_ibdev ( 0 );
  2085. if ( ! ibdev ) {
  2086. rc = -ENOMEM;
  2087. goto err_alloc_ibdev;
  2088. }
  2089. qib7322->ibdev[i] = ibdev;
  2090. ibdev->dev = &pci->dev;
  2091. ibdev->op = &qib7322_ib_operations;
  2092. ibdev->port = ( QIB7322_PORT_BASE + i );
  2093. ibdev->link_width_enabled = ibdev->link_width_supported =
  2094. IB_LINK_WIDTH_4X; /* 1x does not work */
  2095. ibdev->link_speed_enabled = ibdev->link_speed_supported =
  2096. IB_LINK_SPEED_SDR; /* to avoid need for link tuning */
  2097. memcpy ( &ibdev->gid.u.half[1], &qib7322->guid,
  2098. sizeof ( ibdev->gid.u.half[1] ) );
  2099. assert ( ( ibdev->gid.u.half[1].u.bytes[7] & i ) == 0 );
  2100. ibdev->gid.u.half[1].u.bytes[7] |= i;
  2101. ib_set_drvdata ( ibdev, qib7322 );
  2102. }
  2103. /* Register Infiniband devices */
  2104. for ( i = 0 ; i < QIB7322_MAX_PORTS ; i++ ) {
  2105. if ( ! qib7322->ibdev[i] )
  2106. continue;
  2107. if ( ( rc = register_ibdev ( qib7322->ibdev[i] ) ) != 0 ) {
  2108. DBGC ( qib7322, "QIB7322 %p port %d could not register "
  2109. "IB device: %s\n", qib7322, i, strerror ( rc ) );
  2110. goto err_register_ibdev;
  2111. }
  2112. }
  2113. return 0;
  2114. i = QIB7322_MAX_PORTS;
  2115. err_register_ibdev:
  2116. for ( i-- ; i >= 0 ; i-- ) {
  2117. if ( qib7322->ibdev[i] )
  2118. unregister_ibdev ( qib7322->ibdev[i] );
  2119. }
  2120. i = QIB7322_MAX_PORTS;
  2121. err_alloc_ibdev:
  2122. for ( i-- ; i >= 0 ; i-- )
  2123. ibdev_put ( qib7322->ibdev[i] );
  2124. err_init_ib_serdes:
  2125. qib7322_fini_send ( qib7322 );
  2126. err_init_send:
  2127. qib7322_fini_recv ( qib7322 );
  2128. err_init_recv:
  2129. err_read_eeprom:
  2130. err_init_i2c:
  2131. free ( qib7322 );
  2132. err_alloc_qib7322:
  2133. return rc;
  2134. }
  2135. /**
  2136. * Remove PCI device
  2137. *
  2138. * @v pci PCI device
  2139. */
  2140. static void qib7322_remove ( struct pci_device *pci ) {
  2141. struct qib7322 *qib7322 = pci_get_drvdata ( pci );
  2142. int i;
  2143. for ( i = ( QIB7322_MAX_PORTS - 1 ) ; i >= 0 ; i-- ) {
  2144. if ( qib7322->ibdev[i] )
  2145. unregister_ibdev ( qib7322->ibdev[i] );
  2146. }
  2147. for ( i = ( QIB7322_MAX_PORTS - 1 ) ; i >= 0 ; i-- )
  2148. ibdev_put ( qib7322->ibdev[i] );
  2149. qib7322_fini_send ( qib7322 );
  2150. qib7322_fini_recv ( qib7322 );
  2151. free ( qib7322 );
  2152. }
  2153. static struct pci_device_id qib7322_nics[] = {
  2154. PCI_ROM ( 0x1077, 0x7322, "iba7322", "IBA7322 QDR InfiniBand HCA", 0 ),
  2155. };
  2156. struct pci_driver qib7322_driver __pci_driver = {
  2157. .ids = qib7322_nics,
  2158. .id_count = ( sizeof ( qib7322_nics ) / sizeof ( qib7322_nics[0] ) ),
  2159. .probe = qib7322_probe,
  2160. .remove = qib7322_remove,
  2161. };