mthca_qp.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. *
  35. * $Id: mthca_qp.c 1355 2004-12-17 15:23:43Z roland $
  36. */
  37. #include <linux/init.h>
  38. #include <linux/string.h>
  39. #include <linux/slab.h>
  40. #include <rdma/ib_verbs.h>
  41. #include <rdma/ib_cache.h>
  42. #include <rdma/ib_pack.h>
  43. #include "mthca_dev.h"
  44. #include "mthca_cmd.h"
  45. #include "mthca_memfree.h"
  46. #include "mthca_wqe.h"
  47. enum {
  48. MTHCA_MAX_DIRECT_QP_SIZE = 4 * PAGE_SIZE,
  49. MTHCA_ACK_REQ_FREQ = 10,
  50. MTHCA_FLIGHT_LIMIT = 9,
  51. MTHCA_UD_HEADER_SIZE = 72, /* largest UD header possible */
  52. MTHCA_INLINE_HEADER_SIZE = 4, /* data segment overhead for inline */
  53. MTHCA_INLINE_CHUNK_SIZE = 16 /* inline data segment chunk */
  54. };
  55. enum {
  56. MTHCA_QP_STATE_RST = 0,
  57. MTHCA_QP_STATE_INIT = 1,
  58. MTHCA_QP_STATE_RTR = 2,
  59. MTHCA_QP_STATE_RTS = 3,
  60. MTHCA_QP_STATE_SQE = 4,
  61. MTHCA_QP_STATE_SQD = 5,
  62. MTHCA_QP_STATE_ERR = 6,
  63. MTHCA_QP_STATE_DRAINING = 7
  64. };
  65. enum {
  66. MTHCA_QP_ST_RC = 0x0,
  67. MTHCA_QP_ST_UC = 0x1,
  68. MTHCA_QP_ST_RD = 0x2,
  69. MTHCA_QP_ST_UD = 0x3,
  70. MTHCA_QP_ST_MLX = 0x7
  71. };
  72. enum {
  73. MTHCA_QP_PM_MIGRATED = 0x3,
  74. MTHCA_QP_PM_ARMED = 0x0,
  75. MTHCA_QP_PM_REARM = 0x1
  76. };
  77. enum {
  78. /* qp_context flags */
  79. MTHCA_QP_BIT_DE = 1 << 8,
  80. /* params1 */
  81. MTHCA_QP_BIT_SRE = 1 << 15,
  82. MTHCA_QP_BIT_SWE = 1 << 14,
  83. MTHCA_QP_BIT_SAE = 1 << 13,
  84. MTHCA_QP_BIT_SIC = 1 << 4,
  85. MTHCA_QP_BIT_SSC = 1 << 3,
  86. /* params2 */
  87. MTHCA_QP_BIT_RRE = 1 << 15,
  88. MTHCA_QP_BIT_RWE = 1 << 14,
  89. MTHCA_QP_BIT_RAE = 1 << 13,
  90. MTHCA_QP_BIT_RIC = 1 << 4,
  91. MTHCA_QP_BIT_RSC = 1 << 3
  92. };
  93. struct mthca_qp_path {
  94. __be32 port_pkey;
  95. u8 rnr_retry;
  96. u8 g_mylmc;
  97. __be16 rlid;
  98. u8 ackto;
  99. u8 mgid_index;
  100. u8 static_rate;
  101. u8 hop_limit;
  102. __be32 sl_tclass_flowlabel;
  103. u8 rgid[16];
  104. } __attribute__((packed));
  105. struct mthca_qp_context {
  106. __be32 flags;
  107. __be32 tavor_sched_queue; /* Reserved on Arbel */
  108. u8 mtu_msgmax;
  109. u8 rq_size_stride; /* Reserved on Tavor */
  110. u8 sq_size_stride; /* Reserved on Tavor */
  111. u8 rlkey_arbel_sched_queue; /* Reserved on Tavor */
  112. __be32 usr_page;
  113. __be32 local_qpn;
  114. __be32 remote_qpn;
  115. u32 reserved1[2];
  116. struct mthca_qp_path pri_path;
  117. struct mthca_qp_path alt_path;
  118. __be32 rdd;
  119. __be32 pd;
  120. __be32 wqe_base;
  121. __be32 wqe_lkey;
  122. __be32 params1;
  123. __be32 reserved2;
  124. __be32 next_send_psn;
  125. __be32 cqn_snd;
  126. __be32 snd_wqe_base_l; /* Next send WQE on Tavor */
  127. __be32 snd_db_index; /* (debugging only entries) */
  128. __be32 last_acked_psn;
  129. __be32 ssn;
  130. __be32 params2;
  131. __be32 rnr_nextrecvpsn;
  132. __be32 ra_buff_indx;
  133. __be32 cqn_rcv;
  134. __be32 rcv_wqe_base_l; /* Next recv WQE on Tavor */
  135. __be32 rcv_db_index; /* (debugging only entries) */
  136. __be32 qkey;
  137. __be32 srqn;
  138. __be32 rmsn;
  139. __be16 rq_wqe_counter; /* reserved on Tavor */
  140. __be16 sq_wqe_counter; /* reserved on Tavor */
  141. u32 reserved3[18];
  142. } __attribute__((packed));
  143. struct mthca_qp_param {
  144. __be32 opt_param_mask;
  145. u32 reserved1;
  146. struct mthca_qp_context context;
  147. u32 reserved2[62];
  148. } __attribute__((packed));
  149. enum {
  150. MTHCA_QP_OPTPAR_ALT_ADDR_PATH = 1 << 0,
  151. MTHCA_QP_OPTPAR_RRE = 1 << 1,
  152. MTHCA_QP_OPTPAR_RAE = 1 << 2,
  153. MTHCA_QP_OPTPAR_RWE = 1 << 3,
  154. MTHCA_QP_OPTPAR_PKEY_INDEX = 1 << 4,
  155. MTHCA_QP_OPTPAR_Q_KEY = 1 << 5,
  156. MTHCA_QP_OPTPAR_RNR_TIMEOUT = 1 << 6,
  157. MTHCA_QP_OPTPAR_PRIMARY_ADDR_PATH = 1 << 7,
  158. MTHCA_QP_OPTPAR_SRA_MAX = 1 << 8,
  159. MTHCA_QP_OPTPAR_RRA_MAX = 1 << 9,
  160. MTHCA_QP_OPTPAR_PM_STATE = 1 << 10,
  161. MTHCA_QP_OPTPAR_PORT_NUM = 1 << 11,
  162. MTHCA_QP_OPTPAR_RETRY_COUNT = 1 << 12,
  163. MTHCA_QP_OPTPAR_ALT_RNR_RETRY = 1 << 13,
  164. MTHCA_QP_OPTPAR_ACK_TIMEOUT = 1 << 14,
  165. MTHCA_QP_OPTPAR_RNR_RETRY = 1 << 15,
  166. MTHCA_QP_OPTPAR_SCHED_QUEUE = 1 << 16
  167. };
  168. static const u8 mthca_opcode[] = {
  169. [IB_WR_SEND] = MTHCA_OPCODE_SEND,
  170. [IB_WR_SEND_WITH_IMM] = MTHCA_OPCODE_SEND_IMM,
  171. [IB_WR_RDMA_WRITE] = MTHCA_OPCODE_RDMA_WRITE,
  172. [IB_WR_RDMA_WRITE_WITH_IMM] = MTHCA_OPCODE_RDMA_WRITE_IMM,
  173. [IB_WR_RDMA_READ] = MTHCA_OPCODE_RDMA_READ,
  174. [IB_WR_ATOMIC_CMP_AND_SWP] = MTHCA_OPCODE_ATOMIC_CS,
  175. [IB_WR_ATOMIC_FETCH_AND_ADD] = MTHCA_OPCODE_ATOMIC_FA,
  176. };
  177. static int is_sqp(struct mthca_dev *dev, struct mthca_qp *qp)
  178. {
  179. return qp->qpn >= dev->qp_table.sqp_start &&
  180. qp->qpn <= dev->qp_table.sqp_start + 3;
  181. }
  182. static int is_qp0(struct mthca_dev *dev, struct mthca_qp *qp)
  183. {
  184. return qp->qpn >= dev->qp_table.sqp_start &&
  185. qp->qpn <= dev->qp_table.sqp_start + 1;
  186. }
  187. static void *get_recv_wqe(struct mthca_qp *qp, int n)
  188. {
  189. if (qp->is_direct)
  190. return qp->queue.direct.buf + (n << qp->rq.wqe_shift);
  191. else
  192. return qp->queue.page_list[(n << qp->rq.wqe_shift) >> PAGE_SHIFT].buf +
  193. ((n << qp->rq.wqe_shift) & (PAGE_SIZE - 1));
  194. }
  195. static void *get_send_wqe(struct mthca_qp *qp, int n)
  196. {
  197. if (qp->is_direct)
  198. return qp->queue.direct.buf + qp->send_wqe_offset +
  199. (n << qp->sq.wqe_shift);
  200. else
  201. return qp->queue.page_list[(qp->send_wqe_offset +
  202. (n << qp->sq.wqe_shift)) >>
  203. PAGE_SHIFT].buf +
  204. ((qp->send_wqe_offset + (n << qp->sq.wqe_shift)) &
  205. (PAGE_SIZE - 1));
  206. }
  207. static void mthca_wq_init(struct mthca_wq *wq)
  208. {
  209. spin_lock_init(&wq->lock);
  210. wq->next_ind = 0;
  211. wq->last_comp = wq->max - 1;
  212. wq->head = 0;
  213. wq->tail = 0;
  214. }
  215. void mthca_qp_event(struct mthca_dev *dev, u32 qpn,
  216. enum ib_event_type event_type)
  217. {
  218. struct mthca_qp *qp;
  219. struct ib_event event;
  220. spin_lock(&dev->qp_table.lock);
  221. qp = mthca_array_get(&dev->qp_table.qp, qpn & (dev->limits.num_qps - 1));
  222. if (qp)
  223. atomic_inc(&qp->refcount);
  224. spin_unlock(&dev->qp_table.lock);
  225. if (!qp) {
  226. mthca_warn(dev, "Async event for bogus QP %08x\n", qpn);
  227. return;
  228. }
  229. event.device = &dev->ib_dev;
  230. event.event = event_type;
  231. event.element.qp = &qp->ibqp;
  232. if (qp->ibqp.event_handler)
  233. qp->ibqp.event_handler(&event, qp->ibqp.qp_context);
  234. if (atomic_dec_and_test(&qp->refcount))
  235. wake_up(&qp->wait);
  236. }
  237. static int to_mthca_state(enum ib_qp_state ib_state)
  238. {
  239. switch (ib_state) {
  240. case IB_QPS_RESET: return MTHCA_QP_STATE_RST;
  241. case IB_QPS_INIT: return MTHCA_QP_STATE_INIT;
  242. case IB_QPS_RTR: return MTHCA_QP_STATE_RTR;
  243. case IB_QPS_RTS: return MTHCA_QP_STATE_RTS;
  244. case IB_QPS_SQD: return MTHCA_QP_STATE_SQD;
  245. case IB_QPS_SQE: return MTHCA_QP_STATE_SQE;
  246. case IB_QPS_ERR: return MTHCA_QP_STATE_ERR;
  247. default: return -1;
  248. }
  249. }
  250. enum { RC, UC, UD, RD, RDEE, MLX, NUM_TRANS };
  251. static int to_mthca_st(int transport)
  252. {
  253. switch (transport) {
  254. case RC: return MTHCA_QP_ST_RC;
  255. case UC: return MTHCA_QP_ST_UC;
  256. case UD: return MTHCA_QP_ST_UD;
  257. case RD: return MTHCA_QP_ST_RD;
  258. case MLX: return MTHCA_QP_ST_MLX;
  259. default: return -1;
  260. }
  261. }
  262. static const struct {
  263. int trans;
  264. u32 req_param[NUM_TRANS];
  265. u32 opt_param[NUM_TRANS];
  266. } state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
  267. [IB_QPS_RESET] = {
  268. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  269. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  270. [IB_QPS_INIT] = {
  271. .trans = MTHCA_TRANS_RST2INIT,
  272. .req_param = {
  273. [UD] = (IB_QP_PKEY_INDEX |
  274. IB_QP_PORT |
  275. IB_QP_QKEY),
  276. [UC] = (IB_QP_PKEY_INDEX |
  277. IB_QP_PORT |
  278. IB_QP_ACCESS_FLAGS),
  279. [RC] = (IB_QP_PKEY_INDEX |
  280. IB_QP_PORT |
  281. IB_QP_ACCESS_FLAGS),
  282. [MLX] = (IB_QP_PKEY_INDEX |
  283. IB_QP_QKEY),
  284. },
  285. /* bug-for-bug compatibility with VAPI: */
  286. .opt_param = {
  287. [MLX] = IB_QP_PORT
  288. }
  289. },
  290. },
  291. [IB_QPS_INIT] = {
  292. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  293. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  294. [IB_QPS_INIT] = {
  295. .trans = MTHCA_TRANS_INIT2INIT,
  296. .opt_param = {
  297. [UD] = (IB_QP_PKEY_INDEX |
  298. IB_QP_PORT |
  299. IB_QP_QKEY),
  300. [UC] = (IB_QP_PKEY_INDEX |
  301. IB_QP_PORT |
  302. IB_QP_ACCESS_FLAGS),
  303. [RC] = (IB_QP_PKEY_INDEX |
  304. IB_QP_PORT |
  305. IB_QP_ACCESS_FLAGS),
  306. [MLX] = (IB_QP_PKEY_INDEX |
  307. IB_QP_QKEY),
  308. }
  309. },
  310. [IB_QPS_RTR] = {
  311. .trans = MTHCA_TRANS_INIT2RTR,
  312. .req_param = {
  313. [UC] = (IB_QP_AV |
  314. IB_QP_PATH_MTU |
  315. IB_QP_DEST_QPN |
  316. IB_QP_RQ_PSN),
  317. [RC] = (IB_QP_AV |
  318. IB_QP_PATH_MTU |
  319. IB_QP_DEST_QPN |
  320. IB_QP_RQ_PSN |
  321. IB_QP_MAX_DEST_RD_ATOMIC |
  322. IB_QP_MIN_RNR_TIMER),
  323. },
  324. .opt_param = {
  325. [UD] = (IB_QP_PKEY_INDEX |
  326. IB_QP_QKEY),
  327. [UC] = (IB_QP_ALT_PATH |
  328. IB_QP_ACCESS_FLAGS |
  329. IB_QP_PKEY_INDEX),
  330. [RC] = (IB_QP_ALT_PATH |
  331. IB_QP_ACCESS_FLAGS |
  332. IB_QP_PKEY_INDEX),
  333. [MLX] = (IB_QP_PKEY_INDEX |
  334. IB_QP_QKEY),
  335. }
  336. }
  337. },
  338. [IB_QPS_RTR] = {
  339. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  340. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  341. [IB_QPS_RTS] = {
  342. .trans = MTHCA_TRANS_RTR2RTS,
  343. .req_param = {
  344. [UD] = IB_QP_SQ_PSN,
  345. [UC] = IB_QP_SQ_PSN,
  346. [RC] = (IB_QP_TIMEOUT |
  347. IB_QP_RETRY_CNT |
  348. IB_QP_RNR_RETRY |
  349. IB_QP_SQ_PSN |
  350. IB_QP_MAX_QP_RD_ATOMIC),
  351. [MLX] = IB_QP_SQ_PSN,
  352. },
  353. .opt_param = {
  354. [UD] = (IB_QP_CUR_STATE |
  355. IB_QP_QKEY),
  356. [UC] = (IB_QP_CUR_STATE |
  357. IB_QP_ALT_PATH |
  358. IB_QP_ACCESS_FLAGS |
  359. IB_QP_PKEY_INDEX |
  360. IB_QP_PATH_MIG_STATE),
  361. [RC] = (IB_QP_CUR_STATE |
  362. IB_QP_ALT_PATH |
  363. IB_QP_ACCESS_FLAGS |
  364. IB_QP_PKEY_INDEX |
  365. IB_QP_MIN_RNR_TIMER |
  366. IB_QP_PATH_MIG_STATE),
  367. [MLX] = (IB_QP_CUR_STATE |
  368. IB_QP_QKEY),
  369. }
  370. }
  371. },
  372. [IB_QPS_RTS] = {
  373. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  374. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  375. [IB_QPS_RTS] = {
  376. .trans = MTHCA_TRANS_RTS2RTS,
  377. .opt_param = {
  378. [UD] = (IB_QP_CUR_STATE |
  379. IB_QP_QKEY),
  380. [UC] = (IB_QP_ACCESS_FLAGS |
  381. IB_QP_ALT_PATH |
  382. IB_QP_PATH_MIG_STATE),
  383. [RC] = (IB_QP_ACCESS_FLAGS |
  384. IB_QP_ALT_PATH |
  385. IB_QP_PATH_MIG_STATE |
  386. IB_QP_MIN_RNR_TIMER),
  387. [MLX] = (IB_QP_CUR_STATE |
  388. IB_QP_QKEY),
  389. }
  390. },
  391. [IB_QPS_SQD] = {
  392. .trans = MTHCA_TRANS_RTS2SQD,
  393. },
  394. },
  395. [IB_QPS_SQD] = {
  396. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  397. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  398. [IB_QPS_RTS] = {
  399. .trans = MTHCA_TRANS_SQD2RTS,
  400. .opt_param = {
  401. [UD] = (IB_QP_CUR_STATE |
  402. IB_QP_QKEY),
  403. [UC] = (IB_QP_CUR_STATE |
  404. IB_QP_ALT_PATH |
  405. IB_QP_ACCESS_FLAGS |
  406. IB_QP_PATH_MIG_STATE),
  407. [RC] = (IB_QP_CUR_STATE |
  408. IB_QP_ALT_PATH |
  409. IB_QP_ACCESS_FLAGS |
  410. IB_QP_MIN_RNR_TIMER |
  411. IB_QP_PATH_MIG_STATE),
  412. [MLX] = (IB_QP_CUR_STATE |
  413. IB_QP_QKEY),
  414. }
  415. },
  416. [IB_QPS_SQD] = {
  417. .trans = MTHCA_TRANS_SQD2SQD,
  418. .opt_param = {
  419. [UD] = (IB_QP_PKEY_INDEX |
  420. IB_QP_QKEY),
  421. [UC] = (IB_QP_AV |
  422. IB_QP_CUR_STATE |
  423. IB_QP_ALT_PATH |
  424. IB_QP_ACCESS_FLAGS |
  425. IB_QP_PKEY_INDEX |
  426. IB_QP_PATH_MIG_STATE),
  427. [RC] = (IB_QP_AV |
  428. IB_QP_TIMEOUT |
  429. IB_QP_RETRY_CNT |
  430. IB_QP_RNR_RETRY |
  431. IB_QP_MAX_QP_RD_ATOMIC |
  432. IB_QP_MAX_DEST_RD_ATOMIC |
  433. IB_QP_CUR_STATE |
  434. IB_QP_ALT_PATH |
  435. IB_QP_ACCESS_FLAGS |
  436. IB_QP_PKEY_INDEX |
  437. IB_QP_MIN_RNR_TIMER |
  438. IB_QP_PATH_MIG_STATE),
  439. [MLX] = (IB_QP_PKEY_INDEX |
  440. IB_QP_QKEY),
  441. }
  442. }
  443. },
  444. [IB_QPS_SQE] = {
  445. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  446. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR },
  447. [IB_QPS_RTS] = {
  448. .trans = MTHCA_TRANS_SQERR2RTS,
  449. .opt_param = {
  450. [UD] = (IB_QP_CUR_STATE |
  451. IB_QP_QKEY),
  452. [UC] = IB_QP_CUR_STATE,
  453. [RC] = (IB_QP_CUR_STATE |
  454. IB_QP_MIN_RNR_TIMER),
  455. [MLX] = (IB_QP_CUR_STATE |
  456. IB_QP_QKEY),
  457. }
  458. }
  459. },
  460. [IB_QPS_ERR] = {
  461. [IB_QPS_RESET] = { .trans = MTHCA_TRANS_ANY2RST },
  462. [IB_QPS_ERR] = { .trans = MTHCA_TRANS_ANY2ERR }
  463. }
  464. };
  465. static void store_attrs(struct mthca_sqp *sqp, struct ib_qp_attr *attr,
  466. int attr_mask)
  467. {
  468. if (attr_mask & IB_QP_PKEY_INDEX)
  469. sqp->pkey_index = attr->pkey_index;
  470. if (attr_mask & IB_QP_QKEY)
  471. sqp->qkey = attr->qkey;
  472. if (attr_mask & IB_QP_SQ_PSN)
  473. sqp->send_psn = attr->sq_psn;
  474. }
  475. static void init_port(struct mthca_dev *dev, int port)
  476. {
  477. int err;
  478. u8 status;
  479. struct mthca_init_ib_param param;
  480. memset(&param, 0, sizeof param);
  481. param.port_width = dev->limits.port_width_cap;
  482. param.vl_cap = dev->limits.vl_cap;
  483. param.mtu_cap = dev->limits.mtu_cap;
  484. param.gid_cap = dev->limits.gid_table_len;
  485. param.pkey_cap = dev->limits.pkey_table_len;
  486. err = mthca_INIT_IB(dev, &param, port, &status);
  487. if (err)
  488. mthca_warn(dev, "INIT_IB failed, return code %d.\n", err);
  489. if (status)
  490. mthca_warn(dev, "INIT_IB returned status %02x.\n", status);
  491. }
  492. static __be32 get_hw_access_flags(struct mthca_qp *qp, struct ib_qp_attr *attr,
  493. int attr_mask)
  494. {
  495. u8 dest_rd_atomic;
  496. u32 access_flags;
  497. u32 hw_access_flags = 0;
  498. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  499. dest_rd_atomic = attr->max_dest_rd_atomic;
  500. else
  501. dest_rd_atomic = qp->resp_depth;
  502. if (attr_mask & IB_QP_ACCESS_FLAGS)
  503. access_flags = attr->qp_access_flags;
  504. else
  505. access_flags = qp->atomic_rd_en;
  506. if (!dest_rd_atomic)
  507. access_flags &= IB_ACCESS_REMOTE_WRITE;
  508. if (access_flags & IB_ACCESS_REMOTE_READ)
  509. hw_access_flags |= MTHCA_QP_BIT_RRE;
  510. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  511. hw_access_flags |= MTHCA_QP_BIT_RAE;
  512. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  513. hw_access_flags |= MTHCA_QP_BIT_RWE;
  514. return cpu_to_be32(hw_access_flags);
  515. }
  516. int mthca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask)
  517. {
  518. struct mthca_dev *dev = to_mdev(ibqp->device);
  519. struct mthca_qp *qp = to_mqp(ibqp);
  520. enum ib_qp_state cur_state, new_state;
  521. struct mthca_mailbox *mailbox;
  522. struct mthca_qp_param *qp_param;
  523. struct mthca_qp_context *qp_context;
  524. u32 req_param, opt_param;
  525. u8 status;
  526. int err;
  527. if (attr_mask & IB_QP_CUR_STATE) {
  528. if (attr->cur_qp_state != IB_QPS_RTR &&
  529. attr->cur_qp_state != IB_QPS_RTS &&
  530. attr->cur_qp_state != IB_QPS_SQD &&
  531. attr->cur_qp_state != IB_QPS_SQE)
  532. return -EINVAL;
  533. else
  534. cur_state = attr->cur_qp_state;
  535. } else {
  536. spin_lock_irq(&qp->sq.lock);
  537. spin_lock(&qp->rq.lock);
  538. cur_state = qp->state;
  539. spin_unlock(&qp->rq.lock);
  540. spin_unlock_irq(&qp->sq.lock);
  541. }
  542. if (attr_mask & IB_QP_STATE) {
  543. if (attr->qp_state < 0 || attr->qp_state > IB_QPS_ERR)
  544. return -EINVAL;
  545. new_state = attr->qp_state;
  546. } else
  547. new_state = cur_state;
  548. if (state_table[cur_state][new_state].trans == MTHCA_TRANS_INVALID) {
  549. mthca_dbg(dev, "Illegal QP transition "
  550. "%d->%d\n", cur_state, new_state);
  551. return -EINVAL;
  552. }
  553. req_param = state_table[cur_state][new_state].req_param[qp->transport];
  554. opt_param = state_table[cur_state][new_state].opt_param[qp->transport];
  555. if ((req_param & attr_mask) != req_param) {
  556. mthca_dbg(dev, "QP transition "
  557. "%d->%d missing req attr 0x%08x\n",
  558. cur_state, new_state,
  559. req_param & ~attr_mask);
  560. return -EINVAL;
  561. }
  562. if (attr_mask & ~(req_param | opt_param | IB_QP_STATE)) {
  563. mthca_dbg(dev, "QP transition (transport %d) "
  564. "%d->%d has extra attr 0x%08x\n",
  565. qp->transport,
  566. cur_state, new_state,
  567. attr_mask & ~(req_param | opt_param |
  568. IB_QP_STATE));
  569. return -EINVAL;
  570. }
  571. if ((attr_mask & IB_QP_PKEY_INDEX) &&
  572. attr->pkey_index >= dev->limits.pkey_table_len) {
  573. mthca_dbg(dev, "PKey index (%u) too large. max is %d\n",
  574. attr->pkey_index,dev->limits.pkey_table_len-1);
  575. return -EINVAL;
  576. }
  577. if ((attr_mask & IB_QP_PORT) &&
  578. (attr->port_num == 0 || attr->port_num > dev->limits.num_ports)) {
  579. mthca_dbg(dev, "Port number (%u) is invalid\n", attr->port_num);
  580. return -EINVAL;
  581. }
  582. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  583. attr->max_rd_atomic > dev->limits.max_qp_init_rdma) {
  584. mthca_dbg(dev, "Max rdma_atomic as initiator %u too large (max is %d)\n",
  585. attr->max_rd_atomic, dev->limits.max_qp_init_rdma);
  586. return -EINVAL;
  587. }
  588. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  589. attr->max_dest_rd_atomic > 1 << dev->qp_table.rdb_shift) {
  590. mthca_dbg(dev, "Max rdma_atomic as responder %u too large (max %d)\n",
  591. attr->max_dest_rd_atomic, 1 << dev->qp_table.rdb_shift);
  592. return -EINVAL;
  593. }
  594. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  595. if (IS_ERR(mailbox))
  596. return PTR_ERR(mailbox);
  597. qp_param = mailbox->buf;
  598. qp_context = &qp_param->context;
  599. memset(qp_param, 0, sizeof *qp_param);
  600. qp_context->flags = cpu_to_be32((to_mthca_state(new_state) << 28) |
  601. (to_mthca_st(qp->transport) << 16));
  602. qp_context->flags |= cpu_to_be32(MTHCA_QP_BIT_DE);
  603. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  604. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_MIGRATED << 11);
  605. else {
  606. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PM_STATE);
  607. switch (attr->path_mig_state) {
  608. case IB_MIG_MIGRATED:
  609. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_MIGRATED << 11);
  610. break;
  611. case IB_MIG_REARM:
  612. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_REARM << 11);
  613. break;
  614. case IB_MIG_ARMED:
  615. qp_context->flags |= cpu_to_be32(MTHCA_QP_PM_ARMED << 11);
  616. break;
  617. }
  618. }
  619. /* leave tavor_sched_queue as 0 */
  620. if (qp->transport == MLX || qp->transport == UD)
  621. qp_context->mtu_msgmax = (IB_MTU_2048 << 5) | 11;
  622. else if (attr_mask & IB_QP_PATH_MTU)
  623. qp_context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  624. if (mthca_is_memfree(dev)) {
  625. if (qp->rq.max)
  626. qp_context->rq_size_stride = long_log2(qp->rq.max) << 3;
  627. qp_context->rq_size_stride |= qp->rq.wqe_shift - 4;
  628. if (qp->sq.max)
  629. qp_context->sq_size_stride = long_log2(qp->sq.max) << 3;
  630. qp_context->sq_size_stride |= qp->sq.wqe_shift - 4;
  631. }
  632. /* leave arbel_sched_queue as 0 */
  633. if (qp->ibqp.uobject)
  634. qp_context->usr_page =
  635. cpu_to_be32(to_mucontext(qp->ibqp.uobject->context)->uar.index);
  636. else
  637. qp_context->usr_page = cpu_to_be32(dev->driver_uar.index);
  638. qp_context->local_qpn = cpu_to_be32(qp->qpn);
  639. if (attr_mask & IB_QP_DEST_QPN) {
  640. qp_context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  641. }
  642. if (qp->transport == MLX)
  643. qp_context->pri_path.port_pkey |=
  644. cpu_to_be32(to_msqp(qp)->port << 24);
  645. else {
  646. if (attr_mask & IB_QP_PORT) {
  647. qp_context->pri_path.port_pkey |=
  648. cpu_to_be32(attr->port_num << 24);
  649. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PORT_NUM);
  650. }
  651. }
  652. if (attr_mask & IB_QP_PKEY_INDEX) {
  653. qp_context->pri_path.port_pkey |=
  654. cpu_to_be32(attr->pkey_index);
  655. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PKEY_INDEX);
  656. }
  657. if (attr_mask & IB_QP_RNR_RETRY) {
  658. qp_context->pri_path.rnr_retry = attr->rnr_retry << 5;
  659. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RNR_RETRY);
  660. }
  661. if (attr_mask & IB_QP_AV) {
  662. qp_context->pri_path.g_mylmc = attr->ah_attr.src_path_bits & 0x7f;
  663. qp_context->pri_path.rlid = cpu_to_be16(attr->ah_attr.dlid);
  664. qp_context->pri_path.static_rate = !!attr->ah_attr.static_rate;
  665. if (attr->ah_attr.ah_flags & IB_AH_GRH) {
  666. qp_context->pri_path.g_mylmc |= 1 << 7;
  667. qp_context->pri_path.mgid_index = attr->ah_attr.grh.sgid_index;
  668. qp_context->pri_path.hop_limit = attr->ah_attr.grh.hop_limit;
  669. qp_context->pri_path.sl_tclass_flowlabel =
  670. cpu_to_be32((attr->ah_attr.sl << 28) |
  671. (attr->ah_attr.grh.traffic_class << 20) |
  672. (attr->ah_attr.grh.flow_label));
  673. memcpy(qp_context->pri_path.rgid,
  674. attr->ah_attr.grh.dgid.raw, 16);
  675. } else {
  676. qp_context->pri_path.sl_tclass_flowlabel =
  677. cpu_to_be32(attr->ah_attr.sl << 28);
  678. }
  679. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_PRIMARY_ADDR_PATH);
  680. }
  681. if (attr_mask & IB_QP_TIMEOUT) {
  682. qp_context->pri_path.ackto = attr->timeout << 3;
  683. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_ACK_TIMEOUT);
  684. }
  685. /* XXX alt_path */
  686. /* leave rdd as 0 */
  687. qp_context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pd_num);
  688. /* leave wqe_base as 0 (we always create an MR based at 0 for WQs) */
  689. qp_context->wqe_lkey = cpu_to_be32(qp->mr.ibmr.lkey);
  690. qp_context->params1 = cpu_to_be32((MTHCA_ACK_REQ_FREQ << 28) |
  691. (MTHCA_FLIGHT_LIMIT << 24) |
  692. MTHCA_QP_BIT_SWE);
  693. if (qp->sq_policy == IB_SIGNAL_ALL_WR)
  694. qp_context->params1 |= cpu_to_be32(MTHCA_QP_BIT_SSC);
  695. if (attr_mask & IB_QP_RETRY_CNT) {
  696. qp_context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  697. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RETRY_COUNT);
  698. }
  699. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  700. if (attr->max_rd_atomic) {
  701. qp_context->params1 |=
  702. cpu_to_be32(MTHCA_QP_BIT_SRE |
  703. MTHCA_QP_BIT_SAE);
  704. qp_context->params1 |=
  705. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  706. }
  707. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_SRA_MAX);
  708. }
  709. if (attr_mask & IB_QP_SQ_PSN)
  710. qp_context->next_send_psn = cpu_to_be32(attr->sq_psn);
  711. qp_context->cqn_snd = cpu_to_be32(to_mcq(ibqp->send_cq)->cqn);
  712. if (mthca_is_memfree(dev)) {
  713. qp_context->snd_wqe_base_l = cpu_to_be32(qp->send_wqe_offset);
  714. qp_context->snd_db_index = cpu_to_be32(qp->sq.db_index);
  715. }
  716. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  717. if (attr->max_dest_rd_atomic)
  718. qp_context->params2 |=
  719. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  720. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RRA_MAX);
  721. }
  722. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  723. qp_context->params2 |= get_hw_access_flags(qp, attr, attr_mask);
  724. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RWE |
  725. MTHCA_QP_OPTPAR_RRE |
  726. MTHCA_QP_OPTPAR_RAE);
  727. }
  728. qp_context->params2 |= cpu_to_be32(MTHCA_QP_BIT_RSC);
  729. if (ibqp->srq)
  730. qp_context->params2 |= cpu_to_be32(MTHCA_QP_BIT_RIC);
  731. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  732. qp_context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  733. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_RNR_TIMEOUT);
  734. }
  735. if (attr_mask & IB_QP_RQ_PSN)
  736. qp_context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  737. qp_context->ra_buff_indx =
  738. cpu_to_be32(dev->qp_table.rdb_base +
  739. ((qp->qpn & (dev->limits.num_qps - 1)) * MTHCA_RDB_ENTRY_SIZE <<
  740. dev->qp_table.rdb_shift));
  741. qp_context->cqn_rcv = cpu_to_be32(to_mcq(ibqp->recv_cq)->cqn);
  742. if (mthca_is_memfree(dev))
  743. qp_context->rcv_db_index = cpu_to_be32(qp->rq.db_index);
  744. if (attr_mask & IB_QP_QKEY) {
  745. qp_context->qkey = cpu_to_be32(attr->qkey);
  746. qp_param->opt_param_mask |= cpu_to_be32(MTHCA_QP_OPTPAR_Q_KEY);
  747. }
  748. if (ibqp->srq)
  749. qp_context->srqn = cpu_to_be32(1 << 24 |
  750. to_msrq(ibqp->srq)->srqn);
  751. err = mthca_MODIFY_QP(dev, state_table[cur_state][new_state].trans,
  752. qp->qpn, 0, mailbox, 0, &status);
  753. if (status) {
  754. mthca_warn(dev, "modify QP %d returned status %02x.\n",
  755. state_table[cur_state][new_state].trans, status);
  756. err = -EINVAL;
  757. }
  758. if (!err) {
  759. qp->state = new_state;
  760. if (attr_mask & IB_QP_ACCESS_FLAGS)
  761. qp->atomic_rd_en = attr->qp_access_flags;
  762. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  763. qp->resp_depth = attr->max_dest_rd_atomic;
  764. }
  765. mthca_free_mailbox(dev, mailbox);
  766. if (is_sqp(dev, qp))
  767. store_attrs(to_msqp(qp), attr, attr_mask);
  768. /*
  769. * If we moved QP0 to RTR, bring the IB link up; if we moved
  770. * QP0 to RESET or ERROR, bring the link back down.
  771. */
  772. if (is_qp0(dev, qp)) {
  773. if (cur_state != IB_QPS_RTR &&
  774. new_state == IB_QPS_RTR)
  775. init_port(dev, to_msqp(qp)->port);
  776. if (cur_state != IB_QPS_RESET &&
  777. cur_state != IB_QPS_ERR &&
  778. (new_state == IB_QPS_RESET ||
  779. new_state == IB_QPS_ERR))
  780. mthca_CLOSE_IB(dev, to_msqp(qp)->port, &status);
  781. }
  782. /*
  783. * If we moved a kernel QP to RESET, clean up all old CQ
  784. * entries and reinitialize the QP.
  785. */
  786. if (!err && new_state == IB_QPS_RESET && !qp->ibqp.uobject) {
  787. mthca_cq_clean(dev, to_mcq(qp->ibqp.send_cq)->cqn, qp->qpn,
  788. qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
  789. if (qp->ibqp.send_cq != qp->ibqp.recv_cq)
  790. mthca_cq_clean(dev, to_mcq(qp->ibqp.recv_cq)->cqn, qp->qpn,
  791. qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
  792. mthca_wq_init(&qp->sq);
  793. qp->sq.last = get_send_wqe(qp, qp->sq.max - 1);
  794. mthca_wq_init(&qp->rq);
  795. qp->rq.last = get_recv_wqe(qp, qp->rq.max - 1);
  796. if (mthca_is_memfree(dev)) {
  797. *qp->sq.db = 0;
  798. *qp->rq.db = 0;
  799. }
  800. }
  801. return err;
  802. }
  803. static void mthca_adjust_qp_caps(struct mthca_dev *dev,
  804. struct mthca_pd *pd,
  805. struct mthca_qp *qp)
  806. {
  807. int max_data_size;
  808. /*
  809. * Calculate the maximum size of WQE s/g segments, excluding
  810. * the next segment and other non-data segments.
  811. */
  812. max_data_size = min(dev->limits.max_desc_sz, 1 << qp->sq.wqe_shift) -
  813. sizeof (struct mthca_next_seg);
  814. switch (qp->transport) {
  815. case MLX:
  816. max_data_size -= 2 * sizeof (struct mthca_data_seg);
  817. break;
  818. case UD:
  819. if (mthca_is_memfree(dev))
  820. max_data_size -= sizeof (struct mthca_arbel_ud_seg);
  821. else
  822. max_data_size -= sizeof (struct mthca_tavor_ud_seg);
  823. break;
  824. default:
  825. max_data_size -= sizeof (struct mthca_raddr_seg);
  826. break;
  827. }
  828. /* We don't support inline data for kernel QPs (yet). */
  829. if (!pd->ibpd.uobject)
  830. qp->max_inline_data = 0;
  831. else
  832. qp->max_inline_data = max_data_size - MTHCA_INLINE_HEADER_SIZE;
  833. qp->sq.max_gs = min_t(int, dev->limits.max_sg,
  834. max_data_size / sizeof (struct mthca_data_seg));
  835. qp->rq.max_gs = min_t(int, dev->limits.max_sg,
  836. (min(dev->limits.max_desc_sz, 1 << qp->rq.wqe_shift) -
  837. sizeof (struct mthca_next_seg)) /
  838. sizeof (struct mthca_data_seg));
  839. }
  840. /*
  841. * Allocate and register buffer for WQEs. qp->rq.max, sq.max,
  842. * rq.max_gs and sq.max_gs must all be assigned.
  843. * mthca_alloc_wqe_buf will calculate rq.wqe_shift and
  844. * sq.wqe_shift (as well as send_wqe_offset, is_direct, and
  845. * queue)
  846. */
  847. static int mthca_alloc_wqe_buf(struct mthca_dev *dev,
  848. struct mthca_pd *pd,
  849. struct mthca_qp *qp)
  850. {
  851. int size;
  852. int err = -ENOMEM;
  853. size = sizeof (struct mthca_next_seg) +
  854. qp->rq.max_gs * sizeof (struct mthca_data_seg);
  855. if (size > dev->limits.max_desc_sz)
  856. return -EINVAL;
  857. for (qp->rq.wqe_shift = 6; 1 << qp->rq.wqe_shift < size;
  858. qp->rq.wqe_shift++)
  859. ; /* nothing */
  860. size = qp->sq.max_gs * sizeof (struct mthca_data_seg);
  861. switch (qp->transport) {
  862. case MLX:
  863. size += 2 * sizeof (struct mthca_data_seg);
  864. break;
  865. case UD:
  866. size += mthca_is_memfree(dev) ?
  867. sizeof (struct mthca_arbel_ud_seg) :
  868. sizeof (struct mthca_tavor_ud_seg);
  869. break;
  870. case UC:
  871. size += sizeof (struct mthca_raddr_seg);
  872. break;
  873. case RC:
  874. size += sizeof (struct mthca_raddr_seg);
  875. /*
  876. * An atomic op will require an atomic segment, a
  877. * remote address segment and one scatter entry.
  878. */
  879. size = max_t(int, size,
  880. sizeof (struct mthca_atomic_seg) +
  881. sizeof (struct mthca_raddr_seg) +
  882. sizeof (struct mthca_data_seg));
  883. break;
  884. default:
  885. break;
  886. }
  887. /* Make sure that we have enough space for a bind request */
  888. size = max_t(int, size, sizeof (struct mthca_bind_seg));
  889. size += sizeof (struct mthca_next_seg);
  890. if (size > dev->limits.max_desc_sz)
  891. return -EINVAL;
  892. for (qp->sq.wqe_shift = 6; 1 << qp->sq.wqe_shift < size;
  893. qp->sq.wqe_shift++)
  894. ; /* nothing */
  895. qp->send_wqe_offset = ALIGN(qp->rq.max << qp->rq.wqe_shift,
  896. 1 << qp->sq.wqe_shift);
  897. /*
  898. * If this is a userspace QP, we don't actually have to
  899. * allocate anything. All we need is to calculate the WQE
  900. * sizes and the send_wqe_offset, so we're done now.
  901. */
  902. if (pd->ibpd.uobject)
  903. return 0;
  904. size = PAGE_ALIGN(qp->send_wqe_offset +
  905. (qp->sq.max << qp->sq.wqe_shift));
  906. qp->wrid = kmalloc((qp->rq.max + qp->sq.max) * sizeof (u64),
  907. GFP_KERNEL);
  908. if (!qp->wrid)
  909. goto err_out;
  910. err = mthca_buf_alloc(dev, size, MTHCA_MAX_DIRECT_QP_SIZE,
  911. &qp->queue, &qp->is_direct, pd, 0, &qp->mr);
  912. if (err)
  913. goto err_out;
  914. return 0;
  915. err_out:
  916. kfree(qp->wrid);
  917. return err;
  918. }
  919. static void mthca_free_wqe_buf(struct mthca_dev *dev,
  920. struct mthca_qp *qp)
  921. {
  922. mthca_buf_free(dev, PAGE_ALIGN(qp->send_wqe_offset +
  923. (qp->sq.max << qp->sq.wqe_shift)),
  924. &qp->queue, qp->is_direct, &qp->mr);
  925. kfree(qp->wrid);
  926. }
  927. static int mthca_map_memfree(struct mthca_dev *dev,
  928. struct mthca_qp *qp)
  929. {
  930. int ret;
  931. if (mthca_is_memfree(dev)) {
  932. ret = mthca_table_get(dev, dev->qp_table.qp_table, qp->qpn);
  933. if (ret)
  934. return ret;
  935. ret = mthca_table_get(dev, dev->qp_table.eqp_table, qp->qpn);
  936. if (ret)
  937. goto err_qpc;
  938. ret = mthca_table_get(dev, dev->qp_table.rdb_table,
  939. qp->qpn << dev->qp_table.rdb_shift);
  940. if (ret)
  941. goto err_eqpc;
  942. }
  943. return 0;
  944. err_eqpc:
  945. mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
  946. err_qpc:
  947. mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
  948. return ret;
  949. }
  950. static void mthca_unmap_memfree(struct mthca_dev *dev,
  951. struct mthca_qp *qp)
  952. {
  953. mthca_table_put(dev, dev->qp_table.rdb_table,
  954. qp->qpn << dev->qp_table.rdb_shift);
  955. mthca_table_put(dev, dev->qp_table.eqp_table, qp->qpn);
  956. mthca_table_put(dev, dev->qp_table.qp_table, qp->qpn);
  957. }
  958. static int mthca_alloc_memfree(struct mthca_dev *dev,
  959. struct mthca_qp *qp)
  960. {
  961. int ret = 0;
  962. if (mthca_is_memfree(dev)) {
  963. qp->rq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_RQ,
  964. qp->qpn, &qp->rq.db);
  965. if (qp->rq.db_index < 0)
  966. return ret;
  967. qp->sq.db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SQ,
  968. qp->qpn, &qp->sq.db);
  969. if (qp->sq.db_index < 0)
  970. mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
  971. }
  972. return ret;
  973. }
  974. static void mthca_free_memfree(struct mthca_dev *dev,
  975. struct mthca_qp *qp)
  976. {
  977. if (mthca_is_memfree(dev)) {
  978. mthca_free_db(dev, MTHCA_DB_TYPE_SQ, qp->sq.db_index);
  979. mthca_free_db(dev, MTHCA_DB_TYPE_RQ, qp->rq.db_index);
  980. }
  981. }
  982. static int mthca_alloc_qp_common(struct mthca_dev *dev,
  983. struct mthca_pd *pd,
  984. struct mthca_cq *send_cq,
  985. struct mthca_cq *recv_cq,
  986. enum ib_sig_type send_policy,
  987. struct mthca_qp *qp)
  988. {
  989. int ret;
  990. int i;
  991. atomic_set(&qp->refcount, 1);
  992. init_waitqueue_head(&qp->wait);
  993. qp->state = IB_QPS_RESET;
  994. qp->atomic_rd_en = 0;
  995. qp->resp_depth = 0;
  996. qp->sq_policy = send_policy;
  997. mthca_wq_init(&qp->sq);
  998. mthca_wq_init(&qp->rq);
  999. ret = mthca_map_memfree(dev, qp);
  1000. if (ret)
  1001. return ret;
  1002. ret = mthca_alloc_wqe_buf(dev, pd, qp);
  1003. if (ret) {
  1004. mthca_unmap_memfree(dev, qp);
  1005. return ret;
  1006. }
  1007. mthca_adjust_qp_caps(dev, pd, qp);
  1008. /*
  1009. * If this is a userspace QP, we're done now. The doorbells
  1010. * will be allocated and buffers will be initialized in
  1011. * userspace.
  1012. */
  1013. if (pd->ibpd.uobject)
  1014. return 0;
  1015. ret = mthca_alloc_memfree(dev, qp);
  1016. if (ret) {
  1017. mthca_free_wqe_buf(dev, qp);
  1018. mthca_unmap_memfree(dev, qp);
  1019. return ret;
  1020. }
  1021. if (mthca_is_memfree(dev)) {
  1022. struct mthca_next_seg *next;
  1023. struct mthca_data_seg *scatter;
  1024. int size = (sizeof (struct mthca_next_seg) +
  1025. qp->rq.max_gs * sizeof (struct mthca_data_seg)) / 16;
  1026. for (i = 0; i < qp->rq.max; ++i) {
  1027. next = get_recv_wqe(qp, i);
  1028. next->nda_op = cpu_to_be32(((i + 1) & (qp->rq.max - 1)) <<
  1029. qp->rq.wqe_shift);
  1030. next->ee_nds = cpu_to_be32(size);
  1031. for (scatter = (void *) (next + 1);
  1032. (void *) scatter < (void *) next + (1 << qp->rq.wqe_shift);
  1033. ++scatter)
  1034. scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  1035. }
  1036. for (i = 0; i < qp->sq.max; ++i) {
  1037. next = get_send_wqe(qp, i);
  1038. next->nda_op = cpu_to_be32((((i + 1) & (qp->sq.max - 1)) <<
  1039. qp->sq.wqe_shift) +
  1040. qp->send_wqe_offset);
  1041. }
  1042. }
  1043. qp->sq.last = get_send_wqe(qp, qp->sq.max - 1);
  1044. qp->rq.last = get_recv_wqe(qp, qp->rq.max - 1);
  1045. return 0;
  1046. }
  1047. static int mthca_set_qp_size(struct mthca_dev *dev, struct ib_qp_cap *cap,
  1048. struct mthca_qp *qp)
  1049. {
  1050. /* Sanity check QP size before proceeding */
  1051. if (cap->max_send_wr > dev->limits.max_wqes ||
  1052. cap->max_recv_wr > dev->limits.max_wqes ||
  1053. cap->max_send_sge > dev->limits.max_sg ||
  1054. cap->max_recv_sge > dev->limits.max_sg)
  1055. return -EINVAL;
  1056. if (mthca_is_memfree(dev)) {
  1057. qp->rq.max = cap->max_recv_wr ?
  1058. roundup_pow_of_two(cap->max_recv_wr) : 0;
  1059. qp->sq.max = cap->max_send_wr ?
  1060. roundup_pow_of_two(cap->max_send_wr) : 0;
  1061. } else {
  1062. qp->rq.max = cap->max_recv_wr;
  1063. qp->sq.max = cap->max_send_wr;
  1064. }
  1065. qp->rq.max_gs = cap->max_recv_sge;
  1066. qp->sq.max_gs = max_t(int, cap->max_send_sge,
  1067. ALIGN(cap->max_inline_data + MTHCA_INLINE_HEADER_SIZE,
  1068. MTHCA_INLINE_CHUNK_SIZE) /
  1069. sizeof (struct mthca_data_seg));
  1070. /*
  1071. * For MLX transport we need 2 extra S/G entries:
  1072. * one for the header and one for the checksum at the end
  1073. */
  1074. if ((qp->transport == MLX && qp->sq.max_gs + 2 > dev->limits.max_sg) ||
  1075. qp->sq.max_gs > dev->limits.max_sg || qp->rq.max_gs > dev->limits.max_sg)
  1076. return -EINVAL;
  1077. return 0;
  1078. }
  1079. int mthca_alloc_qp(struct mthca_dev *dev,
  1080. struct mthca_pd *pd,
  1081. struct mthca_cq *send_cq,
  1082. struct mthca_cq *recv_cq,
  1083. enum ib_qp_type type,
  1084. enum ib_sig_type send_policy,
  1085. struct ib_qp_cap *cap,
  1086. struct mthca_qp *qp)
  1087. {
  1088. int err;
  1089. err = mthca_set_qp_size(dev, cap, qp);
  1090. if (err)
  1091. return err;
  1092. switch (type) {
  1093. case IB_QPT_RC: qp->transport = RC; break;
  1094. case IB_QPT_UC: qp->transport = UC; break;
  1095. case IB_QPT_UD: qp->transport = UD; break;
  1096. default: return -EINVAL;
  1097. }
  1098. qp->qpn = mthca_alloc(&dev->qp_table.alloc);
  1099. if (qp->qpn == -1)
  1100. return -ENOMEM;
  1101. err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
  1102. send_policy, qp);
  1103. if (err) {
  1104. mthca_free(&dev->qp_table.alloc, qp->qpn);
  1105. return err;
  1106. }
  1107. spin_lock_irq(&dev->qp_table.lock);
  1108. mthca_array_set(&dev->qp_table.qp,
  1109. qp->qpn & (dev->limits.num_qps - 1), qp);
  1110. spin_unlock_irq(&dev->qp_table.lock);
  1111. return 0;
  1112. }
  1113. int mthca_alloc_sqp(struct mthca_dev *dev,
  1114. struct mthca_pd *pd,
  1115. struct mthca_cq *send_cq,
  1116. struct mthca_cq *recv_cq,
  1117. enum ib_sig_type send_policy,
  1118. struct ib_qp_cap *cap,
  1119. int qpn,
  1120. int port,
  1121. struct mthca_sqp *sqp)
  1122. {
  1123. u32 mqpn = qpn * 2 + dev->qp_table.sqp_start + port - 1;
  1124. int err;
  1125. err = mthca_set_qp_size(dev, cap, &sqp->qp);
  1126. if (err)
  1127. return err;
  1128. sqp->header_buf_size = sqp->qp.sq.max * MTHCA_UD_HEADER_SIZE;
  1129. sqp->header_buf = dma_alloc_coherent(&dev->pdev->dev, sqp->header_buf_size,
  1130. &sqp->header_dma, GFP_KERNEL);
  1131. if (!sqp->header_buf)
  1132. return -ENOMEM;
  1133. spin_lock_irq(&dev->qp_table.lock);
  1134. if (mthca_array_get(&dev->qp_table.qp, mqpn))
  1135. err = -EBUSY;
  1136. else
  1137. mthca_array_set(&dev->qp_table.qp, mqpn, sqp);
  1138. spin_unlock_irq(&dev->qp_table.lock);
  1139. if (err)
  1140. goto err_out;
  1141. sqp->port = port;
  1142. sqp->qp.qpn = mqpn;
  1143. sqp->qp.transport = MLX;
  1144. err = mthca_alloc_qp_common(dev, pd, send_cq, recv_cq,
  1145. send_policy, &sqp->qp);
  1146. if (err)
  1147. goto err_out_free;
  1148. atomic_inc(&pd->sqp_count);
  1149. return 0;
  1150. err_out_free:
  1151. /*
  1152. * Lock CQs here, so that CQ polling code can do QP lookup
  1153. * without taking a lock.
  1154. */
  1155. spin_lock_irq(&send_cq->lock);
  1156. if (send_cq != recv_cq)
  1157. spin_lock(&recv_cq->lock);
  1158. spin_lock(&dev->qp_table.lock);
  1159. mthca_array_clear(&dev->qp_table.qp, mqpn);
  1160. spin_unlock(&dev->qp_table.lock);
  1161. if (send_cq != recv_cq)
  1162. spin_unlock(&recv_cq->lock);
  1163. spin_unlock_irq(&send_cq->lock);
  1164. err_out:
  1165. dma_free_coherent(&dev->pdev->dev, sqp->header_buf_size,
  1166. sqp->header_buf, sqp->header_dma);
  1167. return err;
  1168. }
  1169. void mthca_free_qp(struct mthca_dev *dev,
  1170. struct mthca_qp *qp)
  1171. {
  1172. u8 status;
  1173. struct mthca_cq *send_cq;
  1174. struct mthca_cq *recv_cq;
  1175. send_cq = to_mcq(qp->ibqp.send_cq);
  1176. recv_cq = to_mcq(qp->ibqp.recv_cq);
  1177. /*
  1178. * Lock CQs here, so that CQ polling code can do QP lookup
  1179. * without taking a lock.
  1180. */
  1181. spin_lock_irq(&send_cq->lock);
  1182. if (send_cq != recv_cq)
  1183. spin_lock(&recv_cq->lock);
  1184. spin_lock(&dev->qp_table.lock);
  1185. mthca_array_clear(&dev->qp_table.qp,
  1186. qp->qpn & (dev->limits.num_qps - 1));
  1187. spin_unlock(&dev->qp_table.lock);
  1188. if (send_cq != recv_cq)
  1189. spin_unlock(&recv_cq->lock);
  1190. spin_unlock_irq(&send_cq->lock);
  1191. atomic_dec(&qp->refcount);
  1192. wait_event(qp->wait, !atomic_read(&qp->refcount));
  1193. if (qp->state != IB_QPS_RESET)
  1194. mthca_MODIFY_QP(dev, MTHCA_TRANS_ANY2RST, qp->qpn, 0, NULL, 0, &status);
  1195. /*
  1196. * If this is a userspace QP, the buffers, MR, CQs and so on
  1197. * will be cleaned up in userspace, so all we have to do is
  1198. * unref the mem-free tables and free the QPN in our table.
  1199. */
  1200. if (!qp->ibqp.uobject) {
  1201. mthca_cq_clean(dev, to_mcq(qp->ibqp.send_cq)->cqn, qp->qpn,
  1202. qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
  1203. if (qp->ibqp.send_cq != qp->ibqp.recv_cq)
  1204. mthca_cq_clean(dev, to_mcq(qp->ibqp.recv_cq)->cqn, qp->qpn,
  1205. qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
  1206. mthca_free_memfree(dev, qp);
  1207. mthca_free_wqe_buf(dev, qp);
  1208. }
  1209. mthca_unmap_memfree(dev, qp);
  1210. if (is_sqp(dev, qp)) {
  1211. atomic_dec(&(to_mpd(qp->ibqp.pd)->sqp_count));
  1212. dma_free_coherent(&dev->pdev->dev,
  1213. to_msqp(qp)->header_buf_size,
  1214. to_msqp(qp)->header_buf,
  1215. to_msqp(qp)->header_dma);
  1216. } else
  1217. mthca_free(&dev->qp_table.alloc, qp->qpn);
  1218. }
  1219. /* Create UD header for an MLX send and build a data segment for it */
  1220. static int build_mlx_header(struct mthca_dev *dev, struct mthca_sqp *sqp,
  1221. int ind, struct ib_send_wr *wr,
  1222. struct mthca_mlx_seg *mlx,
  1223. struct mthca_data_seg *data)
  1224. {
  1225. int header_size;
  1226. int err;
  1227. u16 pkey;
  1228. ib_ud_header_init(256, /* assume a MAD */
  1229. sqp->ud_header.grh_present,
  1230. &sqp->ud_header);
  1231. err = mthca_read_ah(dev, to_mah(wr->wr.ud.ah), &sqp->ud_header);
  1232. if (err)
  1233. return err;
  1234. mlx->flags &= ~cpu_to_be32(MTHCA_NEXT_SOLICIT | 1);
  1235. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MTHCA_MLX_VL15 : 0) |
  1236. (sqp->ud_header.lrh.destination_lid ==
  1237. IB_LID_PERMISSIVE ? MTHCA_MLX_SLR : 0) |
  1238. (sqp->ud_header.lrh.service_level << 8));
  1239. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1240. mlx->vcrc = 0;
  1241. switch (wr->opcode) {
  1242. case IB_WR_SEND:
  1243. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1244. sqp->ud_header.immediate_present = 0;
  1245. break;
  1246. case IB_WR_SEND_WITH_IMM:
  1247. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1248. sqp->ud_header.immediate_present = 1;
  1249. sqp->ud_header.immediate_data = wr->imm_data;
  1250. break;
  1251. default:
  1252. return -EINVAL;
  1253. }
  1254. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  1255. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  1256. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  1257. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1258. if (!sqp->qp.ibqp.qp_num)
  1259. ib_get_cached_pkey(&dev->ib_dev, sqp->port,
  1260. sqp->pkey_index, &pkey);
  1261. else
  1262. ib_get_cached_pkey(&dev->ib_dev, sqp->port,
  1263. wr->wr.ud.pkey_index, &pkey);
  1264. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  1265. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1266. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1267. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  1268. sqp->qkey : wr->wr.ud.remote_qkey);
  1269. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  1270. header_size = ib_ud_header_pack(&sqp->ud_header,
  1271. sqp->header_buf +
  1272. ind * MTHCA_UD_HEADER_SIZE);
  1273. data->byte_count = cpu_to_be32(header_size);
  1274. data->lkey = cpu_to_be32(to_mpd(sqp->qp.ibqp.pd)->ntmr.ibmr.lkey);
  1275. data->addr = cpu_to_be64(sqp->header_dma +
  1276. ind * MTHCA_UD_HEADER_SIZE);
  1277. return 0;
  1278. }
  1279. static inline int mthca_wq_overflow(struct mthca_wq *wq, int nreq,
  1280. struct ib_cq *ib_cq)
  1281. {
  1282. unsigned cur;
  1283. struct mthca_cq *cq;
  1284. cur = wq->head - wq->tail;
  1285. if (likely(cur + nreq < wq->max))
  1286. return 0;
  1287. cq = to_mcq(ib_cq);
  1288. spin_lock(&cq->lock);
  1289. cur = wq->head - wq->tail;
  1290. spin_unlock(&cq->lock);
  1291. return cur + nreq >= wq->max;
  1292. }
  1293. int mthca_tavor_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1294. struct ib_send_wr **bad_wr)
  1295. {
  1296. struct mthca_dev *dev = to_mdev(ibqp->device);
  1297. struct mthca_qp *qp = to_mqp(ibqp);
  1298. void *wqe;
  1299. void *prev_wqe;
  1300. unsigned long flags;
  1301. int err = 0;
  1302. int nreq;
  1303. int i;
  1304. int size;
  1305. int size0 = 0;
  1306. u32 f0 = 0;
  1307. int ind;
  1308. u8 op0 = 0;
  1309. spin_lock_irqsave(&qp->sq.lock, flags);
  1310. /* XXX check that state is OK to post send */
  1311. ind = qp->sq.next_ind;
  1312. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1313. if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1314. mthca_err(dev, "SQ %06x full (%u head, %u tail,"
  1315. " %d max, %d nreq)\n", qp->qpn,
  1316. qp->sq.head, qp->sq.tail,
  1317. qp->sq.max, nreq);
  1318. err = -ENOMEM;
  1319. *bad_wr = wr;
  1320. goto out;
  1321. }
  1322. wqe = get_send_wqe(qp, ind);
  1323. prev_wqe = qp->sq.last;
  1324. qp->sq.last = wqe;
  1325. ((struct mthca_next_seg *) wqe)->nda_op = 0;
  1326. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  1327. ((struct mthca_next_seg *) wqe)->flags =
  1328. ((wr->send_flags & IB_SEND_SIGNALED) ?
  1329. cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
  1330. ((wr->send_flags & IB_SEND_SOLICITED) ?
  1331. cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
  1332. cpu_to_be32(1);
  1333. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1334. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1335. ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
  1336. wqe += sizeof (struct mthca_next_seg);
  1337. size = sizeof (struct mthca_next_seg) / 16;
  1338. switch (qp->transport) {
  1339. case RC:
  1340. switch (wr->opcode) {
  1341. case IB_WR_ATOMIC_CMP_AND_SWP:
  1342. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1343. ((struct mthca_raddr_seg *) wqe)->raddr =
  1344. cpu_to_be64(wr->wr.atomic.remote_addr);
  1345. ((struct mthca_raddr_seg *) wqe)->rkey =
  1346. cpu_to_be32(wr->wr.atomic.rkey);
  1347. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1348. wqe += sizeof (struct mthca_raddr_seg);
  1349. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1350. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1351. cpu_to_be64(wr->wr.atomic.swap);
  1352. ((struct mthca_atomic_seg *) wqe)->compare =
  1353. cpu_to_be64(wr->wr.atomic.compare_add);
  1354. } else {
  1355. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1356. cpu_to_be64(wr->wr.atomic.compare_add);
  1357. ((struct mthca_atomic_seg *) wqe)->compare = 0;
  1358. }
  1359. wqe += sizeof (struct mthca_atomic_seg);
  1360. size += (sizeof (struct mthca_raddr_seg) +
  1361. sizeof (struct mthca_atomic_seg)) / 16;
  1362. break;
  1363. case IB_WR_RDMA_WRITE:
  1364. case IB_WR_RDMA_WRITE_WITH_IMM:
  1365. case IB_WR_RDMA_READ:
  1366. ((struct mthca_raddr_seg *) wqe)->raddr =
  1367. cpu_to_be64(wr->wr.rdma.remote_addr);
  1368. ((struct mthca_raddr_seg *) wqe)->rkey =
  1369. cpu_to_be32(wr->wr.rdma.rkey);
  1370. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1371. wqe += sizeof (struct mthca_raddr_seg);
  1372. size += sizeof (struct mthca_raddr_seg) / 16;
  1373. break;
  1374. default:
  1375. /* No extra segments required for sends */
  1376. break;
  1377. }
  1378. break;
  1379. case UC:
  1380. switch (wr->opcode) {
  1381. case IB_WR_RDMA_WRITE:
  1382. case IB_WR_RDMA_WRITE_WITH_IMM:
  1383. ((struct mthca_raddr_seg *) wqe)->raddr =
  1384. cpu_to_be64(wr->wr.rdma.remote_addr);
  1385. ((struct mthca_raddr_seg *) wqe)->rkey =
  1386. cpu_to_be32(wr->wr.rdma.rkey);
  1387. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1388. wqe += sizeof (struct mthca_raddr_seg);
  1389. size += sizeof (struct mthca_raddr_seg) / 16;
  1390. break;
  1391. default:
  1392. /* No extra segments required for sends */
  1393. break;
  1394. }
  1395. break;
  1396. case UD:
  1397. ((struct mthca_tavor_ud_seg *) wqe)->lkey =
  1398. cpu_to_be32(to_mah(wr->wr.ud.ah)->key);
  1399. ((struct mthca_tavor_ud_seg *) wqe)->av_addr =
  1400. cpu_to_be64(to_mah(wr->wr.ud.ah)->avdma);
  1401. ((struct mthca_tavor_ud_seg *) wqe)->dqpn =
  1402. cpu_to_be32(wr->wr.ud.remote_qpn);
  1403. ((struct mthca_tavor_ud_seg *) wqe)->qkey =
  1404. cpu_to_be32(wr->wr.ud.remote_qkey);
  1405. wqe += sizeof (struct mthca_tavor_ud_seg);
  1406. size += sizeof (struct mthca_tavor_ud_seg) / 16;
  1407. break;
  1408. case MLX:
  1409. err = build_mlx_header(dev, to_msqp(qp), ind, wr,
  1410. wqe - sizeof (struct mthca_next_seg),
  1411. wqe);
  1412. if (err) {
  1413. *bad_wr = wr;
  1414. goto out;
  1415. }
  1416. wqe += sizeof (struct mthca_data_seg);
  1417. size += sizeof (struct mthca_data_seg) / 16;
  1418. break;
  1419. }
  1420. if (wr->num_sge > qp->sq.max_gs) {
  1421. mthca_err(dev, "too many gathers\n");
  1422. err = -EINVAL;
  1423. *bad_wr = wr;
  1424. goto out;
  1425. }
  1426. for (i = 0; i < wr->num_sge; ++i) {
  1427. ((struct mthca_data_seg *) wqe)->byte_count =
  1428. cpu_to_be32(wr->sg_list[i].length);
  1429. ((struct mthca_data_seg *) wqe)->lkey =
  1430. cpu_to_be32(wr->sg_list[i].lkey);
  1431. ((struct mthca_data_seg *) wqe)->addr =
  1432. cpu_to_be64(wr->sg_list[i].addr);
  1433. wqe += sizeof (struct mthca_data_seg);
  1434. size += sizeof (struct mthca_data_seg) / 16;
  1435. }
  1436. /* Add one more inline data segment for ICRC */
  1437. if (qp->transport == MLX) {
  1438. ((struct mthca_data_seg *) wqe)->byte_count =
  1439. cpu_to_be32((1 << 31) | 4);
  1440. ((u32 *) wqe)[1] = 0;
  1441. wqe += sizeof (struct mthca_data_seg);
  1442. size += sizeof (struct mthca_data_seg) / 16;
  1443. }
  1444. qp->wrid[ind + qp->rq.max] = wr->wr_id;
  1445. if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
  1446. mthca_err(dev, "opcode invalid\n");
  1447. err = -EINVAL;
  1448. *bad_wr = wr;
  1449. goto out;
  1450. }
  1451. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  1452. cpu_to_be32(((ind << qp->sq.wqe_shift) +
  1453. qp->send_wqe_offset) |
  1454. mthca_opcode[wr->opcode]);
  1455. wmb();
  1456. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  1457. cpu_to_be32((size0 ? 0 : MTHCA_NEXT_DBD) | size);
  1458. if (!size0) {
  1459. size0 = size;
  1460. op0 = mthca_opcode[wr->opcode];
  1461. }
  1462. ++ind;
  1463. if (unlikely(ind >= qp->sq.max))
  1464. ind -= qp->sq.max;
  1465. }
  1466. out:
  1467. if (likely(nreq)) {
  1468. __be32 doorbell[2];
  1469. doorbell[0] = cpu_to_be32(((qp->sq.next_ind << qp->sq.wqe_shift) +
  1470. qp->send_wqe_offset) | f0 | op0);
  1471. doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
  1472. wmb();
  1473. mthca_write64(doorbell,
  1474. dev->kar + MTHCA_SEND_DOORBELL,
  1475. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1476. }
  1477. qp->sq.next_ind = ind;
  1478. qp->sq.head += nreq;
  1479. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1480. return err;
  1481. }
  1482. int mthca_tavor_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1483. struct ib_recv_wr **bad_wr)
  1484. {
  1485. struct mthca_dev *dev = to_mdev(ibqp->device);
  1486. struct mthca_qp *qp = to_mqp(ibqp);
  1487. __be32 doorbell[2];
  1488. unsigned long flags;
  1489. int err = 0;
  1490. int nreq;
  1491. int i;
  1492. int size;
  1493. int size0 = 0;
  1494. int ind;
  1495. void *wqe;
  1496. void *prev_wqe;
  1497. spin_lock_irqsave(&qp->rq.lock, flags);
  1498. /* XXX check that state is OK to post receive */
  1499. ind = qp->rq.next_ind;
  1500. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1501. if (unlikely(nreq == MTHCA_TAVOR_MAX_WQES_PER_RECV_DB)) {
  1502. nreq = 0;
  1503. doorbell[0] = cpu_to_be32((qp->rq.next_ind << qp->rq.wqe_shift) | size0);
  1504. doorbell[1] = cpu_to_be32(qp->qpn << 8);
  1505. wmb();
  1506. mthca_write64(doorbell,
  1507. dev->kar + MTHCA_RECEIVE_DOORBELL,
  1508. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1509. qp->rq.head += MTHCA_TAVOR_MAX_WQES_PER_RECV_DB;
  1510. size0 = 0;
  1511. }
  1512. if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1513. mthca_err(dev, "RQ %06x full (%u head, %u tail,"
  1514. " %d max, %d nreq)\n", qp->qpn,
  1515. qp->rq.head, qp->rq.tail,
  1516. qp->rq.max, nreq);
  1517. err = -ENOMEM;
  1518. *bad_wr = wr;
  1519. goto out;
  1520. }
  1521. wqe = get_recv_wqe(qp, ind);
  1522. prev_wqe = qp->rq.last;
  1523. qp->rq.last = wqe;
  1524. ((struct mthca_next_seg *) wqe)->nda_op = 0;
  1525. ((struct mthca_next_seg *) wqe)->ee_nds =
  1526. cpu_to_be32(MTHCA_NEXT_DBD);
  1527. ((struct mthca_next_seg *) wqe)->flags = 0;
  1528. wqe += sizeof (struct mthca_next_seg);
  1529. size = sizeof (struct mthca_next_seg) / 16;
  1530. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1531. err = -EINVAL;
  1532. *bad_wr = wr;
  1533. goto out;
  1534. }
  1535. for (i = 0; i < wr->num_sge; ++i) {
  1536. ((struct mthca_data_seg *) wqe)->byte_count =
  1537. cpu_to_be32(wr->sg_list[i].length);
  1538. ((struct mthca_data_seg *) wqe)->lkey =
  1539. cpu_to_be32(wr->sg_list[i].lkey);
  1540. ((struct mthca_data_seg *) wqe)->addr =
  1541. cpu_to_be64(wr->sg_list[i].addr);
  1542. wqe += sizeof (struct mthca_data_seg);
  1543. size += sizeof (struct mthca_data_seg) / 16;
  1544. }
  1545. qp->wrid[ind] = wr->wr_id;
  1546. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  1547. cpu_to_be32((ind << qp->rq.wqe_shift) | 1);
  1548. wmb();
  1549. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  1550. cpu_to_be32(MTHCA_NEXT_DBD | size);
  1551. if (!size0)
  1552. size0 = size;
  1553. ++ind;
  1554. if (unlikely(ind >= qp->rq.max))
  1555. ind -= qp->rq.max;
  1556. }
  1557. out:
  1558. if (likely(nreq)) {
  1559. doorbell[0] = cpu_to_be32((qp->rq.next_ind << qp->rq.wqe_shift) | size0);
  1560. doorbell[1] = cpu_to_be32((qp->qpn << 8) | nreq);
  1561. wmb();
  1562. mthca_write64(doorbell,
  1563. dev->kar + MTHCA_RECEIVE_DOORBELL,
  1564. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1565. }
  1566. qp->rq.next_ind = ind;
  1567. qp->rq.head += nreq;
  1568. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1569. return err;
  1570. }
  1571. int mthca_arbel_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1572. struct ib_send_wr **bad_wr)
  1573. {
  1574. struct mthca_dev *dev = to_mdev(ibqp->device);
  1575. struct mthca_qp *qp = to_mqp(ibqp);
  1576. __be32 doorbell[2];
  1577. void *wqe;
  1578. void *prev_wqe;
  1579. unsigned long flags;
  1580. int err = 0;
  1581. int nreq;
  1582. int i;
  1583. int size;
  1584. int size0 = 0;
  1585. u32 f0 = 0;
  1586. int ind;
  1587. u8 op0 = 0;
  1588. spin_lock_irqsave(&qp->sq.lock, flags);
  1589. /* XXX check that state is OK to post send */
  1590. ind = qp->sq.head & (qp->sq.max - 1);
  1591. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1592. if (unlikely(nreq == MTHCA_ARBEL_MAX_WQES_PER_SEND_DB)) {
  1593. nreq = 0;
  1594. doorbell[0] = cpu_to_be32((MTHCA_ARBEL_MAX_WQES_PER_SEND_DB << 24) |
  1595. ((qp->sq.head & 0xffff) << 8) |
  1596. f0 | op0);
  1597. doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
  1598. qp->sq.head += MTHCA_ARBEL_MAX_WQES_PER_SEND_DB;
  1599. size0 = 0;
  1600. /*
  1601. * Make sure that descriptors are written before
  1602. * doorbell record.
  1603. */
  1604. wmb();
  1605. *qp->sq.db = cpu_to_be32(qp->sq.head & 0xffff);
  1606. /*
  1607. * Make sure doorbell record is written before we
  1608. * write MMIO send doorbell.
  1609. */
  1610. wmb();
  1611. mthca_write64(doorbell,
  1612. dev->kar + MTHCA_SEND_DOORBELL,
  1613. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1614. }
  1615. if (mthca_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1616. mthca_err(dev, "SQ %06x full (%u head, %u tail,"
  1617. " %d max, %d nreq)\n", qp->qpn,
  1618. qp->sq.head, qp->sq.tail,
  1619. qp->sq.max, nreq);
  1620. err = -ENOMEM;
  1621. *bad_wr = wr;
  1622. goto out;
  1623. }
  1624. wqe = get_send_wqe(qp, ind);
  1625. prev_wqe = qp->sq.last;
  1626. qp->sq.last = wqe;
  1627. ((struct mthca_next_seg *) wqe)->flags =
  1628. ((wr->send_flags & IB_SEND_SIGNALED) ?
  1629. cpu_to_be32(MTHCA_NEXT_CQ_UPDATE) : 0) |
  1630. ((wr->send_flags & IB_SEND_SOLICITED) ?
  1631. cpu_to_be32(MTHCA_NEXT_SOLICIT) : 0) |
  1632. cpu_to_be32(1);
  1633. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  1634. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  1635. ((struct mthca_next_seg *) wqe)->imm = wr->imm_data;
  1636. wqe += sizeof (struct mthca_next_seg);
  1637. size = sizeof (struct mthca_next_seg) / 16;
  1638. switch (qp->transport) {
  1639. case RC:
  1640. switch (wr->opcode) {
  1641. case IB_WR_ATOMIC_CMP_AND_SWP:
  1642. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1643. ((struct mthca_raddr_seg *) wqe)->raddr =
  1644. cpu_to_be64(wr->wr.atomic.remote_addr);
  1645. ((struct mthca_raddr_seg *) wqe)->rkey =
  1646. cpu_to_be32(wr->wr.atomic.rkey);
  1647. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1648. wqe += sizeof (struct mthca_raddr_seg);
  1649. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1650. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1651. cpu_to_be64(wr->wr.atomic.swap);
  1652. ((struct mthca_atomic_seg *) wqe)->compare =
  1653. cpu_to_be64(wr->wr.atomic.compare_add);
  1654. } else {
  1655. ((struct mthca_atomic_seg *) wqe)->swap_add =
  1656. cpu_to_be64(wr->wr.atomic.compare_add);
  1657. ((struct mthca_atomic_seg *) wqe)->compare = 0;
  1658. }
  1659. wqe += sizeof (struct mthca_atomic_seg);
  1660. size += (sizeof (struct mthca_raddr_seg) +
  1661. sizeof (struct mthca_atomic_seg)) / 16;
  1662. break;
  1663. case IB_WR_RDMA_READ:
  1664. case IB_WR_RDMA_WRITE:
  1665. case IB_WR_RDMA_WRITE_WITH_IMM:
  1666. ((struct mthca_raddr_seg *) wqe)->raddr =
  1667. cpu_to_be64(wr->wr.rdma.remote_addr);
  1668. ((struct mthca_raddr_seg *) wqe)->rkey =
  1669. cpu_to_be32(wr->wr.rdma.rkey);
  1670. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1671. wqe += sizeof (struct mthca_raddr_seg);
  1672. size += sizeof (struct mthca_raddr_seg) / 16;
  1673. break;
  1674. default:
  1675. /* No extra segments required for sends */
  1676. break;
  1677. }
  1678. break;
  1679. case UC:
  1680. switch (wr->opcode) {
  1681. case IB_WR_RDMA_WRITE:
  1682. case IB_WR_RDMA_WRITE_WITH_IMM:
  1683. ((struct mthca_raddr_seg *) wqe)->raddr =
  1684. cpu_to_be64(wr->wr.rdma.remote_addr);
  1685. ((struct mthca_raddr_seg *) wqe)->rkey =
  1686. cpu_to_be32(wr->wr.rdma.rkey);
  1687. ((struct mthca_raddr_seg *) wqe)->reserved = 0;
  1688. wqe += sizeof (struct mthca_raddr_seg);
  1689. size += sizeof (struct mthca_raddr_seg) / 16;
  1690. break;
  1691. default:
  1692. /* No extra segments required for sends */
  1693. break;
  1694. }
  1695. break;
  1696. case UD:
  1697. memcpy(((struct mthca_arbel_ud_seg *) wqe)->av,
  1698. to_mah(wr->wr.ud.ah)->av, MTHCA_AV_SIZE);
  1699. ((struct mthca_arbel_ud_seg *) wqe)->dqpn =
  1700. cpu_to_be32(wr->wr.ud.remote_qpn);
  1701. ((struct mthca_arbel_ud_seg *) wqe)->qkey =
  1702. cpu_to_be32(wr->wr.ud.remote_qkey);
  1703. wqe += sizeof (struct mthca_arbel_ud_seg);
  1704. size += sizeof (struct mthca_arbel_ud_seg) / 16;
  1705. break;
  1706. case MLX:
  1707. err = build_mlx_header(dev, to_msqp(qp), ind, wr,
  1708. wqe - sizeof (struct mthca_next_seg),
  1709. wqe);
  1710. if (err) {
  1711. *bad_wr = wr;
  1712. goto out;
  1713. }
  1714. wqe += sizeof (struct mthca_data_seg);
  1715. size += sizeof (struct mthca_data_seg) / 16;
  1716. break;
  1717. }
  1718. if (wr->num_sge > qp->sq.max_gs) {
  1719. mthca_err(dev, "too many gathers\n");
  1720. err = -EINVAL;
  1721. *bad_wr = wr;
  1722. goto out;
  1723. }
  1724. for (i = 0; i < wr->num_sge; ++i) {
  1725. ((struct mthca_data_seg *) wqe)->byte_count =
  1726. cpu_to_be32(wr->sg_list[i].length);
  1727. ((struct mthca_data_seg *) wqe)->lkey =
  1728. cpu_to_be32(wr->sg_list[i].lkey);
  1729. ((struct mthca_data_seg *) wqe)->addr =
  1730. cpu_to_be64(wr->sg_list[i].addr);
  1731. wqe += sizeof (struct mthca_data_seg);
  1732. size += sizeof (struct mthca_data_seg) / 16;
  1733. }
  1734. /* Add one more inline data segment for ICRC */
  1735. if (qp->transport == MLX) {
  1736. ((struct mthca_data_seg *) wqe)->byte_count =
  1737. cpu_to_be32((1 << 31) | 4);
  1738. ((u32 *) wqe)[1] = 0;
  1739. wqe += sizeof (struct mthca_data_seg);
  1740. size += sizeof (struct mthca_data_seg) / 16;
  1741. }
  1742. qp->wrid[ind + qp->rq.max] = wr->wr_id;
  1743. if (wr->opcode >= ARRAY_SIZE(mthca_opcode)) {
  1744. mthca_err(dev, "opcode invalid\n");
  1745. err = -EINVAL;
  1746. *bad_wr = wr;
  1747. goto out;
  1748. }
  1749. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  1750. cpu_to_be32(((ind << qp->sq.wqe_shift) +
  1751. qp->send_wqe_offset) |
  1752. mthca_opcode[wr->opcode]);
  1753. wmb();
  1754. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  1755. cpu_to_be32(MTHCA_NEXT_DBD | size);
  1756. if (!size0) {
  1757. size0 = size;
  1758. op0 = mthca_opcode[wr->opcode];
  1759. }
  1760. ++ind;
  1761. if (unlikely(ind >= qp->sq.max))
  1762. ind -= qp->sq.max;
  1763. }
  1764. out:
  1765. if (likely(nreq)) {
  1766. doorbell[0] = cpu_to_be32((nreq << 24) |
  1767. ((qp->sq.head & 0xffff) << 8) |
  1768. f0 | op0);
  1769. doorbell[1] = cpu_to_be32((qp->qpn << 8) | size0);
  1770. qp->sq.head += nreq;
  1771. /*
  1772. * Make sure that descriptors are written before
  1773. * doorbell record.
  1774. */
  1775. wmb();
  1776. *qp->sq.db = cpu_to_be32(qp->sq.head & 0xffff);
  1777. /*
  1778. * Make sure doorbell record is written before we
  1779. * write MMIO send doorbell.
  1780. */
  1781. wmb();
  1782. mthca_write64(doorbell,
  1783. dev->kar + MTHCA_SEND_DOORBELL,
  1784. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  1785. }
  1786. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1787. return err;
  1788. }
  1789. int mthca_arbel_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1790. struct ib_recv_wr **bad_wr)
  1791. {
  1792. struct mthca_dev *dev = to_mdev(ibqp->device);
  1793. struct mthca_qp *qp = to_mqp(ibqp);
  1794. unsigned long flags;
  1795. int err = 0;
  1796. int nreq;
  1797. int ind;
  1798. int i;
  1799. void *wqe;
  1800. spin_lock_irqsave(&qp->rq.lock, flags);
  1801. /* XXX check that state is OK to post receive */
  1802. ind = qp->rq.head & (qp->rq.max - 1);
  1803. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1804. if (mthca_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1805. mthca_err(dev, "RQ %06x full (%u head, %u tail,"
  1806. " %d max, %d nreq)\n", qp->qpn,
  1807. qp->rq.head, qp->rq.tail,
  1808. qp->rq.max, nreq);
  1809. err = -ENOMEM;
  1810. *bad_wr = wr;
  1811. goto out;
  1812. }
  1813. wqe = get_recv_wqe(qp, ind);
  1814. ((struct mthca_next_seg *) wqe)->flags = 0;
  1815. wqe += sizeof (struct mthca_next_seg);
  1816. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1817. err = -EINVAL;
  1818. *bad_wr = wr;
  1819. goto out;
  1820. }
  1821. for (i = 0; i < wr->num_sge; ++i) {
  1822. ((struct mthca_data_seg *) wqe)->byte_count =
  1823. cpu_to_be32(wr->sg_list[i].length);
  1824. ((struct mthca_data_seg *) wqe)->lkey =
  1825. cpu_to_be32(wr->sg_list[i].lkey);
  1826. ((struct mthca_data_seg *) wqe)->addr =
  1827. cpu_to_be64(wr->sg_list[i].addr);
  1828. wqe += sizeof (struct mthca_data_seg);
  1829. }
  1830. if (i < qp->rq.max_gs) {
  1831. ((struct mthca_data_seg *) wqe)->byte_count = 0;
  1832. ((struct mthca_data_seg *) wqe)->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  1833. ((struct mthca_data_seg *) wqe)->addr = 0;
  1834. }
  1835. qp->wrid[ind] = wr->wr_id;
  1836. ++ind;
  1837. if (unlikely(ind >= qp->rq.max))
  1838. ind -= qp->rq.max;
  1839. }
  1840. out:
  1841. if (likely(nreq)) {
  1842. qp->rq.head += nreq;
  1843. /*
  1844. * Make sure that descriptors are written before
  1845. * doorbell record.
  1846. */
  1847. wmb();
  1848. *qp->rq.db = cpu_to_be32(qp->rq.head & 0xffff);
  1849. }
  1850. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1851. return err;
  1852. }
  1853. int mthca_free_err_wqe(struct mthca_dev *dev, struct mthca_qp *qp, int is_send,
  1854. int index, int *dbd, __be32 *new_wqe)
  1855. {
  1856. struct mthca_next_seg *next;
  1857. /*
  1858. * For SRQs, all WQEs generate a CQE, so we're always at the
  1859. * end of the doorbell chain.
  1860. */
  1861. if (qp->ibqp.srq) {
  1862. *new_wqe = 0;
  1863. return 0;
  1864. }
  1865. if (is_send)
  1866. next = get_send_wqe(qp, index);
  1867. else
  1868. next = get_recv_wqe(qp, index);
  1869. *dbd = !!(next->ee_nds & cpu_to_be32(MTHCA_NEXT_DBD));
  1870. if (next->ee_nds & cpu_to_be32(0x3f))
  1871. *new_wqe = (next->nda_op & cpu_to_be32(~0x3f)) |
  1872. (next->ee_nds & cpu_to_be32(0x3f));
  1873. else
  1874. *new_wqe = 0;
  1875. return 0;
  1876. }
  1877. int __devinit mthca_init_qp_table(struct mthca_dev *dev)
  1878. {
  1879. int err;
  1880. u8 status;
  1881. int i;
  1882. spin_lock_init(&dev->qp_table.lock);
  1883. /*
  1884. * We reserve 2 extra QPs per port for the special QPs. The
  1885. * special QP for port 1 has to be even, so round up.
  1886. */
  1887. dev->qp_table.sqp_start = (dev->limits.reserved_qps + 1) & ~1UL;
  1888. err = mthca_alloc_init(&dev->qp_table.alloc,
  1889. dev->limits.num_qps,
  1890. (1 << 24) - 1,
  1891. dev->qp_table.sqp_start +
  1892. MTHCA_MAX_PORTS * 2);
  1893. if (err)
  1894. return err;
  1895. err = mthca_array_init(&dev->qp_table.qp,
  1896. dev->limits.num_qps);
  1897. if (err) {
  1898. mthca_alloc_cleanup(&dev->qp_table.alloc);
  1899. return err;
  1900. }
  1901. for (i = 0; i < 2; ++i) {
  1902. err = mthca_CONF_SPECIAL_QP(dev, i ? IB_QPT_GSI : IB_QPT_SMI,
  1903. dev->qp_table.sqp_start + i * 2,
  1904. &status);
  1905. if (err)
  1906. goto err_out;
  1907. if (status) {
  1908. mthca_warn(dev, "CONF_SPECIAL_QP returned "
  1909. "status %02x, aborting.\n",
  1910. status);
  1911. err = -EINVAL;
  1912. goto err_out;
  1913. }
  1914. }
  1915. return 0;
  1916. err_out:
  1917. for (i = 0; i < 2; ++i)
  1918. mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
  1919. mthca_array_cleanup(&dev->qp_table.qp, dev->limits.num_qps);
  1920. mthca_alloc_cleanup(&dev->qp_table.alloc);
  1921. return err;
  1922. }
  1923. void __devexit mthca_cleanup_qp_table(struct mthca_dev *dev)
  1924. {
  1925. int i;
  1926. u8 status;
  1927. for (i = 0; i < 2; ++i)
  1928. mthca_CONF_SPECIAL_QP(dev, i, 0, &status);
  1929. mthca_array_cleanup(&dev->qp_table.qp, dev->limits.num_qps);
  1930. mthca_alloc_cleanup(&dev->qp_table.alloc);
  1931. }