mthca_qp.c 60 KB

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