mthca_qp.c 56 KB

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