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 71KB

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