Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

qib7322.c 72KB

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