mthca_qp.c 61 KB

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