mthca_qp.c 62 KB

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