mthca_qp.c 61 KB

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