You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qib7322.c 72KB

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