mthca_qp.c 60 KB

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