mthca_qp.c 62 KB

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