mthca_qp.c 61 KB

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