qed_rdma.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015-2017 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/types.h>
  33. #include <asm/byteorder.h>
  34. #include <linux/bitops.h>
  35. #include <linux/delay.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/errno.h>
  38. #include <linux/io.h>
  39. #include <linux/kernel.h>
  40. #include <linux/list.h>
  41. #include <linux/module.h>
  42. #include <linux/mutex.h>
  43. #include <linux/pci.h>
  44. #include <linux/slab.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/string.h>
  47. #include "qed.h"
  48. #include "qed_cxt.h"
  49. #include "qed_hsi.h"
  50. #include "qed_hw.h"
  51. #include "qed_init_ops.h"
  52. #include "qed_int.h"
  53. #include "qed_ll2.h"
  54. #include "qed_mcp.h"
  55. #include "qed_reg_addr.h"
  56. #include <linux/qed/qed_rdma_if.h>
  57. #include "qed_rdma.h"
  58. #include "qed_roce.h"
  59. #include "qed_sp.h"
  60. int qed_rdma_bmap_alloc(struct qed_hwfn *p_hwfn,
  61. struct qed_bmap *bmap, u32 max_count, char *name)
  62. {
  63. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "max_count = %08x\n", max_count);
  64. bmap->max_count = max_count;
  65. bmap->bitmap = kcalloc(BITS_TO_LONGS(max_count), sizeof(long),
  66. GFP_KERNEL);
  67. if (!bmap->bitmap)
  68. return -ENOMEM;
  69. snprintf(bmap->name, QED_RDMA_MAX_BMAP_NAME, "%s", name);
  70. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "0\n");
  71. return 0;
  72. }
  73. int qed_rdma_bmap_alloc_id(struct qed_hwfn *p_hwfn,
  74. struct qed_bmap *bmap, u32 *id_num)
  75. {
  76. *id_num = find_first_zero_bit(bmap->bitmap, bmap->max_count);
  77. if (*id_num >= bmap->max_count)
  78. return -EINVAL;
  79. __set_bit(*id_num, bmap->bitmap);
  80. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "%s bitmap: allocated id %d\n",
  81. bmap->name, *id_num);
  82. return 0;
  83. }
  84. void qed_bmap_set_id(struct qed_hwfn *p_hwfn,
  85. struct qed_bmap *bmap, u32 id_num)
  86. {
  87. if (id_num >= bmap->max_count)
  88. return;
  89. __set_bit(id_num, bmap->bitmap);
  90. }
  91. void qed_bmap_release_id(struct qed_hwfn *p_hwfn,
  92. struct qed_bmap *bmap, u32 id_num)
  93. {
  94. bool b_acquired;
  95. if (id_num >= bmap->max_count)
  96. return;
  97. b_acquired = test_and_clear_bit(id_num, bmap->bitmap);
  98. if (!b_acquired) {
  99. DP_NOTICE(p_hwfn, "%s bitmap: id %d already released\n",
  100. bmap->name, id_num);
  101. return;
  102. }
  103. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "%s bitmap: released id %d\n",
  104. bmap->name, id_num);
  105. }
  106. int qed_bmap_test_id(struct qed_hwfn *p_hwfn,
  107. struct qed_bmap *bmap, u32 id_num)
  108. {
  109. if (id_num >= bmap->max_count)
  110. return -1;
  111. return test_bit(id_num, bmap->bitmap);
  112. }
  113. static bool qed_bmap_is_empty(struct qed_bmap *bmap)
  114. {
  115. return bmap->max_count == find_first_bit(bmap->bitmap, bmap->max_count);
  116. }
  117. u32 qed_rdma_get_sb_id(void *p_hwfn, u32 rel_sb_id)
  118. {
  119. /* First sb id for RoCE is after all the l2 sb */
  120. return FEAT_NUM((struct qed_hwfn *)p_hwfn, QED_PF_L2_QUE) + rel_sb_id;
  121. }
  122. static int qed_rdma_alloc(struct qed_hwfn *p_hwfn,
  123. struct qed_ptt *p_ptt,
  124. struct qed_rdma_start_in_params *params)
  125. {
  126. struct qed_rdma_info *p_rdma_info;
  127. u32 num_cons, num_tasks;
  128. int rc = -ENOMEM;
  129. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocating RDMA\n");
  130. /* Allocate a struct with current pf rdma info */
  131. p_rdma_info = kzalloc(sizeof(*p_rdma_info), GFP_KERNEL);
  132. if (!p_rdma_info)
  133. return rc;
  134. p_hwfn->p_rdma_info = p_rdma_info;
  135. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  136. p_rdma_info->proto = PROTOCOLID_IWARP;
  137. else
  138. p_rdma_info->proto = PROTOCOLID_ROCE;
  139. num_cons = qed_cxt_get_proto_cid_count(p_hwfn, p_rdma_info->proto,
  140. NULL);
  141. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  142. p_rdma_info->num_qps = num_cons;
  143. else
  144. p_rdma_info->num_qps = num_cons / 2; /* 2 cids per qp */
  145. num_tasks = qed_cxt_get_proto_tid_count(p_hwfn, PROTOCOLID_ROCE);
  146. /* Each MR uses a single task */
  147. p_rdma_info->num_mrs = num_tasks;
  148. /* Queue zone lines are shared between RoCE and L2 in such a way that
  149. * they can be used by each without obstructing the other.
  150. */
  151. p_rdma_info->queue_zone_base = (u16)RESC_START(p_hwfn, QED_L2_QUEUE);
  152. p_rdma_info->max_queue_zones = (u16)RESC_NUM(p_hwfn, QED_L2_QUEUE);
  153. /* Allocate a struct with device params and fill it */
  154. p_rdma_info->dev = kzalloc(sizeof(*p_rdma_info->dev), GFP_KERNEL);
  155. if (!p_rdma_info->dev)
  156. goto free_rdma_info;
  157. /* Allocate a struct with port params and fill it */
  158. p_rdma_info->port = kzalloc(sizeof(*p_rdma_info->port), GFP_KERNEL);
  159. if (!p_rdma_info->port)
  160. goto free_rdma_dev;
  161. /* Allocate bit map for pd's */
  162. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->pd_map, RDMA_MAX_PDS,
  163. "PD");
  164. if (rc) {
  165. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  166. "Failed to allocate pd_map, rc = %d\n",
  167. rc);
  168. goto free_rdma_port;
  169. }
  170. /* Allocate DPI bitmap */
  171. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->dpi_map,
  172. p_hwfn->dpi_count, "DPI");
  173. if (rc) {
  174. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  175. "Failed to allocate DPI bitmap, rc = %d\n", rc);
  176. goto free_pd_map;
  177. }
  178. /* Allocate bitmap for cq's. The maximum number of CQs is bound to
  179. * the number of connections we support. (num_qps in iWARP or
  180. * num_qps/2 in RoCE).
  181. */
  182. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->cq_map, num_cons, "CQ");
  183. if (rc) {
  184. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  185. "Failed to allocate cq bitmap, rc = %d\n", rc);
  186. goto free_dpi_map;
  187. }
  188. /* Allocate bitmap for toggle bit for cq icids
  189. * We toggle the bit every time we create or resize cq for a given icid.
  190. * Size needs to equal the size of the cq bmap.
  191. */
  192. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->toggle_bits,
  193. num_cons, "Toggle");
  194. if (rc) {
  195. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  196. "Failed to allocate toogle bits, rc = %d\n", rc);
  197. goto free_cq_map;
  198. }
  199. /* Allocate bitmap for itids */
  200. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->tid_map,
  201. p_rdma_info->num_mrs, "MR");
  202. if (rc) {
  203. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  204. "Failed to allocate itids bitmaps, rc = %d\n", rc);
  205. goto free_toggle_map;
  206. }
  207. /* Allocate bitmap for cids used for qps. */
  208. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->cid_map, num_cons,
  209. "CID");
  210. if (rc) {
  211. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  212. "Failed to allocate cid bitmap, rc = %d\n", rc);
  213. goto free_tid_map;
  214. }
  215. /* Allocate bitmap for cids used for responders/requesters. */
  216. rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->real_cid_map, num_cons,
  217. "REAL_CID");
  218. if (rc) {
  219. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  220. "Failed to allocate real cid bitmap, rc = %d\n", rc);
  221. goto free_cid_map;
  222. }
  223. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  224. rc = qed_iwarp_alloc(p_hwfn);
  225. if (rc)
  226. goto free_cid_map;
  227. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocation successful\n");
  228. return 0;
  229. free_cid_map:
  230. kfree(p_rdma_info->cid_map.bitmap);
  231. free_tid_map:
  232. kfree(p_rdma_info->tid_map.bitmap);
  233. free_toggle_map:
  234. kfree(p_rdma_info->toggle_bits.bitmap);
  235. free_cq_map:
  236. kfree(p_rdma_info->cq_map.bitmap);
  237. free_dpi_map:
  238. kfree(p_rdma_info->dpi_map.bitmap);
  239. free_pd_map:
  240. kfree(p_rdma_info->pd_map.bitmap);
  241. free_rdma_port:
  242. kfree(p_rdma_info->port);
  243. free_rdma_dev:
  244. kfree(p_rdma_info->dev);
  245. free_rdma_info:
  246. kfree(p_rdma_info);
  247. return rc;
  248. }
  249. void qed_rdma_bmap_free(struct qed_hwfn *p_hwfn,
  250. struct qed_bmap *bmap, bool check)
  251. {
  252. int weight = bitmap_weight(bmap->bitmap, bmap->max_count);
  253. int last_line = bmap->max_count / (64 * 8);
  254. int last_item = last_line * 8 +
  255. DIV_ROUND_UP(bmap->max_count % (64 * 8), 64);
  256. u64 *pmap = (u64 *)bmap->bitmap;
  257. int line, item, offset;
  258. u8 str_last_line[200] = { 0 };
  259. if (!weight || !check)
  260. goto end;
  261. DP_NOTICE(p_hwfn,
  262. "%s bitmap not free - size=%d, weight=%d, 512 bits per line\n",
  263. bmap->name, bmap->max_count, weight);
  264. /* print aligned non-zero lines, if any */
  265. for (item = 0, line = 0; line < last_line; line++, item += 8)
  266. if (bitmap_weight((unsigned long *)&pmap[item], 64 * 8))
  267. DP_NOTICE(p_hwfn,
  268. "line 0x%04x: 0x%016llx 0x%016llx 0x%016llx 0x%016llx 0x%016llx 0x%016llx 0x%016llx 0x%016llx\n",
  269. line,
  270. pmap[item],
  271. pmap[item + 1],
  272. pmap[item + 2],
  273. pmap[item + 3],
  274. pmap[item + 4],
  275. pmap[item + 5],
  276. pmap[item + 6], pmap[item + 7]);
  277. /* print last unaligned non-zero line, if any */
  278. if ((bmap->max_count % (64 * 8)) &&
  279. (bitmap_weight((unsigned long *)&pmap[item],
  280. bmap->max_count - item * 64))) {
  281. offset = sprintf(str_last_line, "line 0x%04x: ", line);
  282. for (; item < last_item; item++)
  283. offset += sprintf(str_last_line + offset,
  284. "0x%016llx ", pmap[item]);
  285. DP_NOTICE(p_hwfn, "%s\n", str_last_line);
  286. }
  287. end:
  288. kfree(bmap->bitmap);
  289. bmap->bitmap = NULL;
  290. }
  291. static void qed_rdma_resc_free(struct qed_hwfn *p_hwfn)
  292. {
  293. struct qed_rdma_info *p_rdma_info = p_hwfn->p_rdma_info;
  294. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  295. qed_iwarp_resc_free(p_hwfn);
  296. qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->cid_map, 1);
  297. qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->pd_map, 1);
  298. qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->dpi_map, 1);
  299. qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->cq_map, 1);
  300. qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->toggle_bits, 0);
  301. qed_rdma_bmap_free(p_hwfn, &p_hwfn->p_rdma_info->tid_map, 1);
  302. kfree(p_rdma_info->port);
  303. kfree(p_rdma_info->dev);
  304. kfree(p_rdma_info);
  305. }
  306. static void qed_rdma_free_tid(void *rdma_cxt, u32 itid)
  307. {
  308. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  309. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "itid = %08x\n", itid);
  310. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  311. qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->tid_map, itid);
  312. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  313. }
  314. static void qed_rdma_free_reserved_lkey(struct qed_hwfn *p_hwfn)
  315. {
  316. qed_rdma_free_tid(p_hwfn, p_hwfn->p_rdma_info->dev->reserved_lkey);
  317. }
  318. static void qed_rdma_free(struct qed_hwfn *p_hwfn)
  319. {
  320. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Freeing RDMA\n");
  321. qed_rdma_free_reserved_lkey(p_hwfn);
  322. qed_cxt_free_proto_ilt(p_hwfn, p_hwfn->p_rdma_info->proto);
  323. qed_rdma_resc_free(p_hwfn);
  324. }
  325. static void qed_rdma_get_guid(struct qed_hwfn *p_hwfn, u8 *guid)
  326. {
  327. guid[0] = p_hwfn->hw_info.hw_mac_addr[0] ^ 2;
  328. guid[1] = p_hwfn->hw_info.hw_mac_addr[1];
  329. guid[2] = p_hwfn->hw_info.hw_mac_addr[2];
  330. guid[3] = 0xff;
  331. guid[4] = 0xfe;
  332. guid[5] = p_hwfn->hw_info.hw_mac_addr[3];
  333. guid[6] = p_hwfn->hw_info.hw_mac_addr[4];
  334. guid[7] = p_hwfn->hw_info.hw_mac_addr[5];
  335. }
  336. static void qed_rdma_init_events(struct qed_hwfn *p_hwfn,
  337. struct qed_rdma_start_in_params *params)
  338. {
  339. struct qed_rdma_events *events;
  340. events = &p_hwfn->p_rdma_info->events;
  341. events->unaffiliated_event = params->events->unaffiliated_event;
  342. events->affiliated_event = params->events->affiliated_event;
  343. events->context = params->events->context;
  344. }
  345. static void qed_rdma_init_devinfo(struct qed_hwfn *p_hwfn,
  346. struct qed_rdma_start_in_params *params)
  347. {
  348. struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
  349. struct qed_dev *cdev = p_hwfn->cdev;
  350. u32 pci_status_control;
  351. u32 num_qps;
  352. /* Vendor specific information */
  353. dev->vendor_id = cdev->vendor_id;
  354. dev->vendor_part_id = cdev->device_id;
  355. dev->hw_ver = 0;
  356. dev->fw_ver = (FW_MAJOR_VERSION << 24) | (FW_MINOR_VERSION << 16) |
  357. (FW_REVISION_VERSION << 8) | (FW_ENGINEERING_VERSION);
  358. qed_rdma_get_guid(p_hwfn, (u8 *)&dev->sys_image_guid);
  359. dev->node_guid = dev->sys_image_guid;
  360. dev->max_sge = min_t(u32, RDMA_MAX_SGE_PER_SQ_WQE,
  361. RDMA_MAX_SGE_PER_RQ_WQE);
  362. if (cdev->rdma_max_sge)
  363. dev->max_sge = min_t(u32, cdev->rdma_max_sge, dev->max_sge);
  364. dev->max_inline = ROCE_REQ_MAX_INLINE_DATA_SIZE;
  365. dev->max_inline = (cdev->rdma_max_inline) ?
  366. min_t(u32, cdev->rdma_max_inline, dev->max_inline) :
  367. dev->max_inline;
  368. dev->max_wqe = QED_RDMA_MAX_WQE;
  369. dev->max_cnq = (u8)FEAT_NUM(p_hwfn, QED_RDMA_CNQ);
  370. /* The number of QPs may be higher than QED_ROCE_MAX_QPS, because
  371. * it is up-aligned to 16 and then to ILT page size within qed cxt.
  372. * This is OK in terms of ILT but we don't want to configure the FW
  373. * above its abilities
  374. */
  375. num_qps = ROCE_MAX_QPS;
  376. num_qps = min_t(u64, num_qps, p_hwfn->p_rdma_info->num_qps);
  377. dev->max_qp = num_qps;
  378. /* CQs uses the same icids that QPs use hence they are limited by the
  379. * number of icids. There are two icids per QP.
  380. */
  381. dev->max_cq = num_qps * 2;
  382. /* The number of mrs is smaller by 1 since the first is reserved */
  383. dev->max_mr = p_hwfn->p_rdma_info->num_mrs - 1;
  384. dev->max_mr_size = QED_RDMA_MAX_MR_SIZE;
  385. /* The maximum CQE capacity per CQ supported.
  386. * max number of cqes will be in two layer pbl,
  387. * 8 is the pointer size in bytes
  388. * 32 is the size of cq element in bytes
  389. */
  390. if (params->cq_mode == QED_RDMA_CQ_MODE_32_BITS)
  391. dev->max_cqe = QED_RDMA_MAX_CQE_32_BIT;
  392. else
  393. dev->max_cqe = QED_RDMA_MAX_CQE_16_BIT;
  394. dev->max_mw = 0;
  395. dev->max_fmr = QED_RDMA_MAX_FMR;
  396. dev->max_mr_mw_fmr_pbl = (PAGE_SIZE / 8) * (PAGE_SIZE / 8);
  397. dev->max_mr_mw_fmr_size = dev->max_mr_mw_fmr_pbl * PAGE_SIZE;
  398. dev->max_pkey = QED_RDMA_MAX_P_KEY;
  399. dev->max_qp_resp_rd_atomic_resc = RDMA_RING_PAGE_SIZE /
  400. (RDMA_RESP_RD_ATOMIC_ELM_SIZE * 2);
  401. dev->max_qp_req_rd_atomic_resc = RDMA_RING_PAGE_SIZE /
  402. RDMA_REQ_RD_ATOMIC_ELM_SIZE;
  403. dev->max_dev_resp_rd_atomic_resc = dev->max_qp_resp_rd_atomic_resc *
  404. p_hwfn->p_rdma_info->num_qps;
  405. dev->page_size_caps = QED_RDMA_PAGE_SIZE_CAPS;
  406. dev->dev_ack_delay = QED_RDMA_ACK_DELAY;
  407. dev->max_pd = RDMA_MAX_PDS;
  408. dev->max_ah = p_hwfn->p_rdma_info->num_qps;
  409. dev->max_stats_queues = (u8)RESC_NUM(p_hwfn, QED_RDMA_STATS_QUEUE);
  410. /* Set capablities */
  411. dev->dev_caps = 0;
  412. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_RNR_NAK, 1);
  413. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_PORT_ACTIVE_EVENT, 1);
  414. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_PORT_CHANGE_EVENT, 1);
  415. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_RESIZE_CQ, 1);
  416. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_BASE_MEMORY_EXT, 1);
  417. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_BASE_QUEUE_EXT, 1);
  418. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_ZBVA, 1);
  419. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_LOCAL_INV_FENCE, 1);
  420. /* Check atomic operations support in PCI configuration space. */
  421. pci_read_config_dword(cdev->pdev,
  422. cdev->pdev->pcie_cap + PCI_EXP_DEVCTL2,
  423. &pci_status_control);
  424. if (pci_status_control & PCI_EXP_DEVCTL2_LTR_EN)
  425. SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_ATOMIC_OP, 1);
  426. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  427. qed_iwarp_init_devinfo(p_hwfn);
  428. }
  429. static void qed_rdma_init_port(struct qed_hwfn *p_hwfn)
  430. {
  431. struct qed_rdma_port *port = p_hwfn->p_rdma_info->port;
  432. struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
  433. port->port_state = p_hwfn->mcp_info->link_output.link_up ?
  434. QED_RDMA_PORT_UP : QED_RDMA_PORT_DOWN;
  435. port->max_msg_size = min_t(u64,
  436. (dev->max_mr_mw_fmr_size *
  437. p_hwfn->cdev->rdma_max_sge),
  438. BIT(31));
  439. port->pkey_bad_counter = 0;
  440. }
  441. static int qed_rdma_init_hw(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  442. {
  443. int rc = 0;
  444. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Initializing HW\n");
  445. p_hwfn->b_rdma_enabled_in_prs = false;
  446. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  447. qed_iwarp_init_hw(p_hwfn, p_ptt);
  448. else
  449. rc = qed_roce_init_hw(p_hwfn, p_ptt);
  450. return rc;
  451. }
  452. static int qed_rdma_start_fw(struct qed_hwfn *p_hwfn,
  453. struct qed_rdma_start_in_params *params,
  454. struct qed_ptt *p_ptt)
  455. {
  456. struct rdma_init_func_ramrod_data *p_ramrod;
  457. struct qed_rdma_cnq_params *p_cnq_pbl_list;
  458. struct rdma_init_func_hdr *p_params_header;
  459. struct rdma_cnq_params *p_cnq_params;
  460. struct qed_sp_init_data init_data;
  461. struct qed_spq_entry *p_ent;
  462. u32 cnq_id, sb_id;
  463. u16 igu_sb_id;
  464. int rc;
  465. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Starting FW\n");
  466. /* Save the number of cnqs for the function close ramrod */
  467. p_hwfn->p_rdma_info->num_cnqs = params->desired_cnq;
  468. /* Get SPQ entry */
  469. memset(&init_data, 0, sizeof(init_data));
  470. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  471. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  472. rc = qed_sp_init_request(p_hwfn, &p_ent, RDMA_RAMROD_FUNC_INIT,
  473. p_hwfn->p_rdma_info->proto, &init_data);
  474. if (rc)
  475. return rc;
  476. if (QED_IS_IWARP_PERSONALITY(p_hwfn)) {
  477. qed_iwarp_init_fw_ramrod(p_hwfn,
  478. &p_ent->ramrod.iwarp_init_func);
  479. p_ramrod = &p_ent->ramrod.iwarp_init_func.rdma;
  480. } else {
  481. p_ramrod = &p_ent->ramrod.roce_init_func.rdma;
  482. }
  483. p_params_header = &p_ramrod->params_header;
  484. p_params_header->cnq_start_offset = (u8)RESC_START(p_hwfn,
  485. QED_RDMA_CNQ_RAM);
  486. p_params_header->num_cnqs = params->desired_cnq;
  487. if (params->cq_mode == QED_RDMA_CQ_MODE_16_BITS)
  488. p_params_header->cq_ring_mode = 1;
  489. else
  490. p_params_header->cq_ring_mode = 0;
  491. for (cnq_id = 0; cnq_id < params->desired_cnq; cnq_id++) {
  492. sb_id = qed_rdma_get_sb_id(p_hwfn, cnq_id);
  493. igu_sb_id = qed_get_igu_sb_id(p_hwfn, sb_id);
  494. p_ramrod->cnq_params[cnq_id].sb_num = cpu_to_le16(igu_sb_id);
  495. p_cnq_params = &p_ramrod->cnq_params[cnq_id];
  496. p_cnq_pbl_list = &params->cnq_pbl_list[cnq_id];
  497. p_cnq_params->sb_index = p_hwfn->pf_params.rdma_pf_params.gl_pi;
  498. p_cnq_params->num_pbl_pages = p_cnq_pbl_list->num_pbl_pages;
  499. DMA_REGPAIR_LE(p_cnq_params->pbl_base_addr,
  500. p_cnq_pbl_list->pbl_ptr);
  501. /* we assume here that cnq_id and qz_offset are the same */
  502. p_cnq_params->queue_zone_num =
  503. cpu_to_le16(p_hwfn->p_rdma_info->queue_zone_base +
  504. cnq_id);
  505. }
  506. return qed_spq_post(p_hwfn, p_ent, NULL);
  507. }
  508. static int qed_rdma_alloc_tid(void *rdma_cxt, u32 *itid)
  509. {
  510. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  511. int rc;
  512. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocate TID\n");
  513. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  514. rc = qed_rdma_bmap_alloc_id(p_hwfn,
  515. &p_hwfn->p_rdma_info->tid_map, itid);
  516. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  517. if (rc)
  518. goto out;
  519. rc = qed_cxt_dynamic_ilt_alloc(p_hwfn, QED_ELEM_TASK, *itid);
  520. out:
  521. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocate TID - done, rc = %d\n", rc);
  522. return rc;
  523. }
  524. static int qed_rdma_reserve_lkey(struct qed_hwfn *p_hwfn)
  525. {
  526. struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
  527. /* Tid 0 will be used as the key for "reserved MR".
  528. * The driver should allocate memory for it so it can be loaded but no
  529. * ramrod should be passed on it.
  530. */
  531. qed_rdma_alloc_tid(p_hwfn, &dev->reserved_lkey);
  532. if (dev->reserved_lkey != RDMA_RESERVED_LKEY) {
  533. DP_NOTICE(p_hwfn,
  534. "Reserved lkey should be equal to RDMA_RESERVED_LKEY\n");
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. static int qed_rdma_setup(struct qed_hwfn *p_hwfn,
  540. struct qed_ptt *p_ptt,
  541. struct qed_rdma_start_in_params *params)
  542. {
  543. int rc;
  544. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA setup\n");
  545. spin_lock_init(&p_hwfn->p_rdma_info->lock);
  546. qed_rdma_init_devinfo(p_hwfn, params);
  547. qed_rdma_init_port(p_hwfn);
  548. qed_rdma_init_events(p_hwfn, params);
  549. rc = qed_rdma_reserve_lkey(p_hwfn);
  550. if (rc)
  551. return rc;
  552. rc = qed_rdma_init_hw(p_hwfn, p_ptt);
  553. if (rc)
  554. return rc;
  555. if (QED_IS_IWARP_PERSONALITY(p_hwfn)) {
  556. rc = qed_iwarp_setup(p_hwfn, p_ptt, params);
  557. if (rc)
  558. return rc;
  559. } else {
  560. rc = qed_roce_setup(p_hwfn);
  561. if (rc)
  562. return rc;
  563. }
  564. return qed_rdma_start_fw(p_hwfn, params, p_ptt);
  565. }
  566. int qed_rdma_stop(void *rdma_cxt)
  567. {
  568. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  569. struct rdma_close_func_ramrod_data *p_ramrod;
  570. struct qed_sp_init_data init_data;
  571. struct qed_spq_entry *p_ent;
  572. struct qed_ptt *p_ptt;
  573. u32 ll2_ethertype_en;
  574. int rc = -EBUSY;
  575. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA stop\n");
  576. p_ptt = qed_ptt_acquire(p_hwfn);
  577. if (!p_ptt) {
  578. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Failed to acquire PTT\n");
  579. return rc;
  580. }
  581. /* Disable RoCE search */
  582. qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 0);
  583. p_hwfn->b_rdma_enabled_in_prs = false;
  584. qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF, 0);
  585. ll2_ethertype_en = qed_rd(p_hwfn, p_ptt, PRS_REG_LIGHT_L2_ETHERTYPE_EN);
  586. qed_wr(p_hwfn, p_ptt, PRS_REG_LIGHT_L2_ETHERTYPE_EN,
  587. (ll2_ethertype_en & 0xFFFE));
  588. if (QED_IS_IWARP_PERSONALITY(p_hwfn)) {
  589. rc = qed_iwarp_stop(p_hwfn, p_ptt);
  590. if (rc) {
  591. qed_ptt_release(p_hwfn, p_ptt);
  592. return rc;
  593. }
  594. } else {
  595. qed_roce_stop(p_hwfn);
  596. }
  597. qed_ptt_release(p_hwfn, p_ptt);
  598. /* Get SPQ entry */
  599. memset(&init_data, 0, sizeof(init_data));
  600. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  601. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  602. /* Stop RoCE */
  603. rc = qed_sp_init_request(p_hwfn, &p_ent, RDMA_RAMROD_FUNC_CLOSE,
  604. p_hwfn->p_rdma_info->proto, &init_data);
  605. if (rc)
  606. goto out;
  607. p_ramrod = &p_ent->ramrod.rdma_close_func;
  608. p_ramrod->num_cnqs = p_hwfn->p_rdma_info->num_cnqs;
  609. p_ramrod->cnq_start_offset = (u8)RESC_START(p_hwfn, QED_RDMA_CNQ_RAM);
  610. rc = qed_spq_post(p_hwfn, p_ent, NULL);
  611. out:
  612. qed_rdma_free(p_hwfn);
  613. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA stop done, rc = %d\n", rc);
  614. return rc;
  615. }
  616. static int qed_rdma_add_user(void *rdma_cxt,
  617. struct qed_rdma_add_user_out_params *out_params)
  618. {
  619. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  620. u32 dpi_start_offset;
  621. u32 returned_id = 0;
  622. int rc;
  623. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Adding User\n");
  624. /* Allocate DPI */
  625. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  626. rc = qed_rdma_bmap_alloc_id(p_hwfn, &p_hwfn->p_rdma_info->dpi_map,
  627. &returned_id);
  628. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  629. out_params->dpi = (u16)returned_id;
  630. /* Calculate the corresponding DPI address */
  631. dpi_start_offset = p_hwfn->dpi_start_offset;
  632. out_params->dpi_addr = (u64)((u8 __iomem *)p_hwfn->doorbells +
  633. dpi_start_offset +
  634. ((out_params->dpi) * p_hwfn->dpi_size));
  635. out_params->dpi_phys_addr = p_hwfn->cdev->db_phys_addr +
  636. dpi_start_offset +
  637. ((out_params->dpi) * p_hwfn->dpi_size);
  638. out_params->dpi_size = p_hwfn->dpi_size;
  639. out_params->wid_count = p_hwfn->wid_count;
  640. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Adding user - done, rc = %d\n", rc);
  641. return rc;
  642. }
  643. static struct qed_rdma_port *qed_rdma_query_port(void *rdma_cxt)
  644. {
  645. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  646. struct qed_rdma_port *p_port = p_hwfn->p_rdma_info->port;
  647. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA Query port\n");
  648. /* Link may have changed */
  649. p_port->port_state = p_hwfn->mcp_info->link_output.link_up ?
  650. QED_RDMA_PORT_UP : QED_RDMA_PORT_DOWN;
  651. p_port->link_speed = p_hwfn->mcp_info->link_output.speed;
  652. p_port->max_msg_size = RDMA_MAX_DATA_SIZE_IN_WQE;
  653. return p_port;
  654. }
  655. static struct qed_rdma_device *qed_rdma_query_device(void *rdma_cxt)
  656. {
  657. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  658. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Query device\n");
  659. /* Return struct with device parameters */
  660. return p_hwfn->p_rdma_info->dev;
  661. }
  662. static void qed_rdma_cnq_prod_update(void *rdma_cxt, u8 qz_offset, u16 prod)
  663. {
  664. struct qed_hwfn *p_hwfn;
  665. u16 qz_num;
  666. u32 addr;
  667. p_hwfn = (struct qed_hwfn *)rdma_cxt;
  668. if (qz_offset > p_hwfn->p_rdma_info->max_queue_zones) {
  669. DP_NOTICE(p_hwfn,
  670. "queue zone offset %d is too large (max is %d)\n",
  671. qz_offset, p_hwfn->p_rdma_info->max_queue_zones);
  672. return;
  673. }
  674. qz_num = p_hwfn->p_rdma_info->queue_zone_base + qz_offset;
  675. addr = GTT_BAR0_MAP_REG_USDM_RAM +
  676. USTORM_COMMON_QUEUE_CONS_OFFSET(qz_num);
  677. REG_WR16(p_hwfn, addr, prod);
  678. /* keep prod updates ordered */
  679. wmb();
  680. }
  681. static int qed_fill_rdma_dev_info(struct qed_dev *cdev,
  682. struct qed_dev_rdma_info *info)
  683. {
  684. struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
  685. memset(info, 0, sizeof(*info));
  686. info->rdma_type = QED_IS_ROCE_PERSONALITY(p_hwfn) ?
  687. QED_RDMA_TYPE_ROCE : QED_RDMA_TYPE_IWARP;
  688. info->user_dpm_enabled = (p_hwfn->db_bar_no_edpm == 0);
  689. qed_fill_dev_info(cdev, &info->common);
  690. return 0;
  691. }
  692. static int qed_rdma_get_sb_start(struct qed_dev *cdev)
  693. {
  694. int feat_num;
  695. if (cdev->num_hwfns > 1)
  696. feat_num = FEAT_NUM(QED_LEADING_HWFN(cdev), QED_PF_L2_QUE);
  697. else
  698. feat_num = FEAT_NUM(QED_LEADING_HWFN(cdev), QED_PF_L2_QUE) *
  699. cdev->num_hwfns;
  700. return feat_num;
  701. }
  702. static int qed_rdma_get_min_cnq_msix(struct qed_dev *cdev)
  703. {
  704. int n_cnq = FEAT_NUM(QED_LEADING_HWFN(cdev), QED_RDMA_CNQ);
  705. int n_msix = cdev->int_params.rdma_msix_cnt;
  706. return min_t(int, n_cnq, n_msix);
  707. }
  708. static int qed_rdma_set_int(struct qed_dev *cdev, u16 cnt)
  709. {
  710. int limit = 0;
  711. /* Mark the fastpath as free/used */
  712. cdev->int_params.fp_initialized = cnt ? true : false;
  713. if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX) {
  714. DP_ERR(cdev,
  715. "qed roce supports only MSI-X interrupts (detected %d).\n",
  716. cdev->int_params.out.int_mode);
  717. return -EINVAL;
  718. } else if (cdev->int_params.fp_msix_cnt) {
  719. limit = cdev->int_params.rdma_msix_cnt;
  720. }
  721. if (!limit)
  722. return -ENOMEM;
  723. return min_t(int, cnt, limit);
  724. }
  725. static int qed_rdma_get_int(struct qed_dev *cdev, struct qed_int_info *info)
  726. {
  727. memset(info, 0, sizeof(*info));
  728. if (!cdev->int_params.fp_initialized) {
  729. DP_INFO(cdev,
  730. "Protocol driver requested interrupt information, but its support is not yet configured\n");
  731. return -EINVAL;
  732. }
  733. if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
  734. int msix_base = cdev->int_params.rdma_msix_base;
  735. info->msix_cnt = cdev->int_params.rdma_msix_cnt;
  736. info->msix = &cdev->int_params.msix_table[msix_base];
  737. DP_VERBOSE(cdev, QED_MSG_RDMA, "msix_cnt = %d msix_base=%d\n",
  738. info->msix_cnt, msix_base);
  739. }
  740. return 0;
  741. }
  742. static int qed_rdma_alloc_pd(void *rdma_cxt, u16 *pd)
  743. {
  744. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  745. u32 returned_id;
  746. int rc;
  747. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Alloc PD\n");
  748. /* Allocates an unused protection domain */
  749. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  750. rc = qed_rdma_bmap_alloc_id(p_hwfn,
  751. &p_hwfn->p_rdma_info->pd_map, &returned_id);
  752. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  753. *pd = (u16)returned_id;
  754. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Alloc PD - done, rc = %d\n", rc);
  755. return rc;
  756. }
  757. static void qed_rdma_free_pd(void *rdma_cxt, u16 pd)
  758. {
  759. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  760. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "pd = %08x\n", pd);
  761. /* Returns a previously allocated protection domain for reuse */
  762. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  763. qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->pd_map, pd);
  764. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  765. }
  766. static enum qed_rdma_toggle_bit
  767. qed_rdma_toggle_bit_create_resize_cq(struct qed_hwfn *p_hwfn, u16 icid)
  768. {
  769. struct qed_rdma_info *p_info = p_hwfn->p_rdma_info;
  770. enum qed_rdma_toggle_bit toggle_bit;
  771. u32 bmap_id;
  772. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "icid = %08x\n", icid);
  773. /* the function toggle the bit that is related to a given icid
  774. * and returns the new toggle bit's value
  775. */
  776. bmap_id = icid - qed_cxt_get_proto_cid_start(p_hwfn, p_info->proto);
  777. spin_lock_bh(&p_info->lock);
  778. toggle_bit = !test_and_change_bit(bmap_id,
  779. p_info->toggle_bits.bitmap);
  780. spin_unlock_bh(&p_info->lock);
  781. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QED_RDMA_TOGGLE_BIT_= %d\n",
  782. toggle_bit);
  783. return toggle_bit;
  784. }
  785. static int qed_rdma_create_cq(void *rdma_cxt,
  786. struct qed_rdma_create_cq_in_params *params,
  787. u16 *icid)
  788. {
  789. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  790. struct qed_rdma_info *p_info = p_hwfn->p_rdma_info;
  791. struct rdma_create_cq_ramrod_data *p_ramrod;
  792. enum qed_rdma_toggle_bit toggle_bit;
  793. struct qed_sp_init_data init_data;
  794. struct qed_spq_entry *p_ent;
  795. u32 returned_id, start_cid;
  796. int rc;
  797. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "cq_handle = %08x%08x\n",
  798. params->cq_handle_hi, params->cq_handle_lo);
  799. /* Allocate icid */
  800. spin_lock_bh(&p_info->lock);
  801. rc = qed_rdma_bmap_alloc_id(p_hwfn, &p_info->cq_map, &returned_id);
  802. spin_unlock_bh(&p_info->lock);
  803. if (rc) {
  804. DP_NOTICE(p_hwfn, "Can't create CQ, rc = %d\n", rc);
  805. return rc;
  806. }
  807. start_cid = qed_cxt_get_proto_cid_start(p_hwfn,
  808. p_info->proto);
  809. *icid = returned_id + start_cid;
  810. /* Check if icid requires a page allocation */
  811. rc = qed_cxt_dynamic_ilt_alloc(p_hwfn, QED_ELEM_CXT, *icid);
  812. if (rc)
  813. goto err;
  814. /* Get SPQ entry */
  815. memset(&init_data, 0, sizeof(init_data));
  816. init_data.cid = *icid;
  817. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  818. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  819. /* Send create CQ ramrod */
  820. rc = qed_sp_init_request(p_hwfn, &p_ent,
  821. RDMA_RAMROD_CREATE_CQ,
  822. p_info->proto, &init_data);
  823. if (rc)
  824. goto err;
  825. p_ramrod = &p_ent->ramrod.rdma_create_cq;
  826. p_ramrod->cq_handle.hi = cpu_to_le32(params->cq_handle_hi);
  827. p_ramrod->cq_handle.lo = cpu_to_le32(params->cq_handle_lo);
  828. p_ramrod->dpi = cpu_to_le16(params->dpi);
  829. p_ramrod->is_two_level_pbl = params->pbl_two_level;
  830. p_ramrod->max_cqes = cpu_to_le32(params->cq_size);
  831. DMA_REGPAIR_LE(p_ramrod->pbl_addr, params->pbl_ptr);
  832. p_ramrod->pbl_num_pages = cpu_to_le16(params->pbl_num_pages);
  833. p_ramrod->cnq_id = (u8)RESC_START(p_hwfn, QED_RDMA_CNQ_RAM) +
  834. params->cnq_id;
  835. p_ramrod->int_timeout = params->int_timeout;
  836. /* toggle the bit for every resize or create cq for a given icid */
  837. toggle_bit = qed_rdma_toggle_bit_create_resize_cq(p_hwfn, *icid);
  838. p_ramrod->toggle_bit = toggle_bit;
  839. rc = qed_spq_post(p_hwfn, p_ent, NULL);
  840. if (rc) {
  841. /* restore toggle bit */
  842. qed_rdma_toggle_bit_create_resize_cq(p_hwfn, *icid);
  843. goto err;
  844. }
  845. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Created CQ, rc = %d\n", rc);
  846. return rc;
  847. err:
  848. /* release allocated icid */
  849. spin_lock_bh(&p_info->lock);
  850. qed_bmap_release_id(p_hwfn, &p_info->cq_map, returned_id);
  851. spin_unlock_bh(&p_info->lock);
  852. DP_NOTICE(p_hwfn, "Create CQ failed, rc = %d\n", rc);
  853. return rc;
  854. }
  855. static int
  856. qed_rdma_destroy_cq(void *rdma_cxt,
  857. struct qed_rdma_destroy_cq_in_params *in_params,
  858. struct qed_rdma_destroy_cq_out_params *out_params)
  859. {
  860. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  861. struct rdma_destroy_cq_output_params *p_ramrod_res;
  862. struct rdma_destroy_cq_ramrod_data *p_ramrod;
  863. struct qed_sp_init_data init_data;
  864. struct qed_spq_entry *p_ent;
  865. dma_addr_t ramrod_res_phys;
  866. enum protocol_type proto;
  867. int rc = -ENOMEM;
  868. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "icid = %08x\n", in_params->icid);
  869. p_ramrod_res =
  870. (struct rdma_destroy_cq_output_params *)
  871. dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  872. sizeof(struct rdma_destroy_cq_output_params),
  873. &ramrod_res_phys, GFP_KERNEL);
  874. if (!p_ramrod_res) {
  875. DP_NOTICE(p_hwfn,
  876. "qed destroy cq failed: cannot allocate memory (ramrod)\n");
  877. return rc;
  878. }
  879. /* Get SPQ entry */
  880. memset(&init_data, 0, sizeof(init_data));
  881. init_data.cid = in_params->icid;
  882. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  883. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  884. proto = p_hwfn->p_rdma_info->proto;
  885. /* Send destroy CQ ramrod */
  886. rc = qed_sp_init_request(p_hwfn, &p_ent,
  887. RDMA_RAMROD_DESTROY_CQ,
  888. proto, &init_data);
  889. if (rc)
  890. goto err;
  891. p_ramrod = &p_ent->ramrod.rdma_destroy_cq;
  892. DMA_REGPAIR_LE(p_ramrod->output_params_addr, ramrod_res_phys);
  893. rc = qed_spq_post(p_hwfn, p_ent, NULL);
  894. if (rc)
  895. goto err;
  896. out_params->num_cq_notif = le16_to_cpu(p_ramrod_res->cnq_num);
  897. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  898. sizeof(struct rdma_destroy_cq_output_params),
  899. p_ramrod_res, ramrod_res_phys);
  900. /* Free icid */
  901. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  902. qed_bmap_release_id(p_hwfn,
  903. &p_hwfn->p_rdma_info->cq_map,
  904. (in_params->icid -
  905. qed_cxt_get_proto_cid_start(p_hwfn, proto)));
  906. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  907. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Destroyed CQ, rc = %d\n", rc);
  908. return rc;
  909. err: dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  910. sizeof(struct rdma_destroy_cq_output_params),
  911. p_ramrod_res, ramrod_res_phys);
  912. return rc;
  913. }
  914. void qed_rdma_set_fw_mac(u16 *p_fw_mac, u8 *p_qed_mac)
  915. {
  916. p_fw_mac[0] = cpu_to_le16((p_qed_mac[0] << 8) + p_qed_mac[1]);
  917. p_fw_mac[1] = cpu_to_le16((p_qed_mac[2] << 8) + p_qed_mac[3]);
  918. p_fw_mac[2] = cpu_to_le16((p_qed_mac[4] << 8) + p_qed_mac[5]);
  919. }
  920. static int qed_rdma_query_qp(void *rdma_cxt,
  921. struct qed_rdma_qp *qp,
  922. struct qed_rdma_query_qp_out_params *out_params)
  923. {
  924. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  925. int rc = 0;
  926. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "icid = %08x\n", qp->icid);
  927. /* The following fields are filled in from qp and not FW as they can't
  928. * be modified by FW
  929. */
  930. out_params->mtu = qp->mtu;
  931. out_params->dest_qp = qp->dest_qp;
  932. out_params->incoming_atomic_en = qp->incoming_atomic_en;
  933. out_params->e2e_flow_control_en = qp->e2e_flow_control_en;
  934. out_params->incoming_rdma_read_en = qp->incoming_rdma_read_en;
  935. out_params->incoming_rdma_write_en = qp->incoming_rdma_write_en;
  936. out_params->dgid = qp->dgid;
  937. out_params->flow_label = qp->flow_label;
  938. out_params->hop_limit_ttl = qp->hop_limit_ttl;
  939. out_params->traffic_class_tos = qp->traffic_class_tos;
  940. out_params->timeout = qp->ack_timeout;
  941. out_params->rnr_retry = qp->rnr_retry_cnt;
  942. out_params->retry_cnt = qp->retry_cnt;
  943. out_params->min_rnr_nak_timer = qp->min_rnr_nak_timer;
  944. out_params->pkey_index = 0;
  945. out_params->max_rd_atomic = qp->max_rd_atomic_req;
  946. out_params->max_dest_rd_atomic = qp->max_rd_atomic_resp;
  947. out_params->sqd_async = qp->sqd_async;
  948. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  949. qed_iwarp_query_qp(qp, out_params);
  950. else
  951. rc = qed_roce_query_qp(p_hwfn, qp, out_params);
  952. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Query QP, rc = %d\n", rc);
  953. return rc;
  954. }
  955. static int qed_rdma_destroy_qp(void *rdma_cxt, struct qed_rdma_qp *qp)
  956. {
  957. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  958. int rc = 0;
  959. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "icid = %08x\n", qp->icid);
  960. if (QED_IS_IWARP_PERSONALITY(p_hwfn))
  961. rc = qed_iwarp_destroy_qp(p_hwfn, qp);
  962. else
  963. rc = qed_roce_destroy_qp(p_hwfn, qp);
  964. /* free qp params struct */
  965. kfree(qp);
  966. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "QP destroyed\n");
  967. return rc;
  968. }
  969. static struct qed_rdma_qp *
  970. qed_rdma_create_qp(void *rdma_cxt,
  971. struct qed_rdma_create_qp_in_params *in_params,
  972. struct qed_rdma_create_qp_out_params *out_params)
  973. {
  974. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  975. struct qed_rdma_qp *qp;
  976. u8 max_stats_queues;
  977. int rc;
  978. if (!rdma_cxt || !in_params || !out_params || !p_hwfn->p_rdma_info) {
  979. DP_ERR(p_hwfn->cdev,
  980. "qed roce create qp failed due to NULL entry (rdma_cxt=%p, in=%p, out=%p, roce_info=?\n",
  981. rdma_cxt, in_params, out_params);
  982. return NULL;
  983. }
  984. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  985. "qed rdma create qp called with qp_handle = %08x%08x\n",
  986. in_params->qp_handle_hi, in_params->qp_handle_lo);
  987. /* Some sanity checks... */
  988. max_stats_queues = p_hwfn->p_rdma_info->dev->max_stats_queues;
  989. if (in_params->stats_queue >= max_stats_queues) {
  990. DP_ERR(p_hwfn->cdev,
  991. "qed rdma create qp failed due to invalid statistics queue %d. maximum is %d\n",
  992. in_params->stats_queue, max_stats_queues);
  993. return NULL;
  994. }
  995. if (QED_IS_IWARP_PERSONALITY(p_hwfn)) {
  996. if (in_params->sq_num_pages * sizeof(struct regpair) >
  997. IWARP_SHARED_QUEUE_PAGE_SQ_PBL_MAX_SIZE) {
  998. DP_NOTICE(p_hwfn->cdev,
  999. "Sq num pages: %d exceeds maximum\n",
  1000. in_params->sq_num_pages);
  1001. return NULL;
  1002. }
  1003. if (in_params->rq_num_pages * sizeof(struct regpair) >
  1004. IWARP_SHARED_QUEUE_PAGE_RQ_PBL_MAX_SIZE) {
  1005. DP_NOTICE(p_hwfn->cdev,
  1006. "Rq num pages: %d exceeds maximum\n",
  1007. in_params->rq_num_pages);
  1008. return NULL;
  1009. }
  1010. }
  1011. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  1012. if (!qp)
  1013. return NULL;
  1014. qp->cur_state = QED_ROCE_QP_STATE_RESET;
  1015. qp->qp_handle.hi = cpu_to_le32(in_params->qp_handle_hi);
  1016. qp->qp_handle.lo = cpu_to_le32(in_params->qp_handle_lo);
  1017. qp->qp_handle_async.hi = cpu_to_le32(in_params->qp_handle_async_hi);
  1018. qp->qp_handle_async.lo = cpu_to_le32(in_params->qp_handle_async_lo);
  1019. qp->use_srq = in_params->use_srq;
  1020. qp->signal_all = in_params->signal_all;
  1021. qp->fmr_and_reserved_lkey = in_params->fmr_and_reserved_lkey;
  1022. qp->pd = in_params->pd;
  1023. qp->dpi = in_params->dpi;
  1024. qp->sq_cq_id = in_params->sq_cq_id;
  1025. qp->sq_num_pages = in_params->sq_num_pages;
  1026. qp->sq_pbl_ptr = in_params->sq_pbl_ptr;
  1027. qp->rq_cq_id = in_params->rq_cq_id;
  1028. qp->rq_num_pages = in_params->rq_num_pages;
  1029. qp->rq_pbl_ptr = in_params->rq_pbl_ptr;
  1030. qp->srq_id = in_params->srq_id;
  1031. qp->req_offloaded = false;
  1032. qp->resp_offloaded = false;
  1033. qp->e2e_flow_control_en = qp->use_srq ? false : true;
  1034. qp->stats_queue = in_params->stats_queue;
  1035. if (QED_IS_IWARP_PERSONALITY(p_hwfn)) {
  1036. rc = qed_iwarp_create_qp(p_hwfn, qp, out_params);
  1037. qp->qpid = qp->icid;
  1038. } else {
  1039. rc = qed_roce_alloc_cid(p_hwfn, &qp->icid);
  1040. qp->qpid = ((0xFF << 16) | qp->icid);
  1041. }
  1042. if (rc) {
  1043. kfree(qp);
  1044. return NULL;
  1045. }
  1046. out_params->icid = qp->icid;
  1047. out_params->qp_id = qp->qpid;
  1048. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Create QP, rc = %d\n", rc);
  1049. return qp;
  1050. }
  1051. static int qed_rdma_modify_qp(void *rdma_cxt,
  1052. struct qed_rdma_qp *qp,
  1053. struct qed_rdma_modify_qp_in_params *params)
  1054. {
  1055. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  1056. enum qed_roce_qp_state prev_state;
  1057. int rc = 0;
  1058. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "icid = %08x params->new_state=%d\n",
  1059. qp->icid, params->new_state);
  1060. if (rc) {
  1061. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = %d\n", rc);
  1062. return rc;
  1063. }
  1064. if (GET_FIELD(params->modify_flags,
  1065. QED_RDMA_MODIFY_QP_VALID_RDMA_OPS_EN)) {
  1066. qp->incoming_rdma_read_en = params->incoming_rdma_read_en;
  1067. qp->incoming_rdma_write_en = params->incoming_rdma_write_en;
  1068. qp->incoming_atomic_en = params->incoming_atomic_en;
  1069. }
  1070. /* Update QP structure with the updated values */
  1071. if (GET_FIELD(params->modify_flags, QED_ROCE_MODIFY_QP_VALID_ROCE_MODE))
  1072. qp->roce_mode = params->roce_mode;
  1073. if (GET_FIELD(params->modify_flags, QED_ROCE_MODIFY_QP_VALID_PKEY))
  1074. qp->pkey = params->pkey;
  1075. if (GET_FIELD(params->modify_flags,
  1076. QED_ROCE_MODIFY_QP_VALID_E2E_FLOW_CONTROL_EN))
  1077. qp->e2e_flow_control_en = params->e2e_flow_control_en;
  1078. if (GET_FIELD(params->modify_flags, QED_ROCE_MODIFY_QP_VALID_DEST_QP))
  1079. qp->dest_qp = params->dest_qp;
  1080. if (GET_FIELD(params->modify_flags,
  1081. QED_ROCE_MODIFY_QP_VALID_ADDRESS_VECTOR)) {
  1082. /* Indicates that the following parameters have changed:
  1083. * Traffic class, flow label, hop limit, source GID,
  1084. * destination GID, loopback indicator
  1085. */
  1086. qp->traffic_class_tos = params->traffic_class_tos;
  1087. qp->flow_label = params->flow_label;
  1088. qp->hop_limit_ttl = params->hop_limit_ttl;
  1089. qp->sgid = params->sgid;
  1090. qp->dgid = params->dgid;
  1091. qp->udp_src_port = 0;
  1092. qp->vlan_id = params->vlan_id;
  1093. qp->mtu = params->mtu;
  1094. qp->lb_indication = params->lb_indication;
  1095. memcpy((u8 *)&qp->remote_mac_addr[0],
  1096. (u8 *)&params->remote_mac_addr[0], ETH_ALEN);
  1097. if (params->use_local_mac) {
  1098. memcpy((u8 *)&qp->local_mac_addr[0],
  1099. (u8 *)&params->local_mac_addr[0], ETH_ALEN);
  1100. } else {
  1101. memcpy((u8 *)&qp->local_mac_addr[0],
  1102. (u8 *)&p_hwfn->hw_info.hw_mac_addr, ETH_ALEN);
  1103. }
  1104. }
  1105. if (GET_FIELD(params->modify_flags, QED_ROCE_MODIFY_QP_VALID_RQ_PSN))
  1106. qp->rq_psn = params->rq_psn;
  1107. if (GET_FIELD(params->modify_flags, QED_ROCE_MODIFY_QP_VALID_SQ_PSN))
  1108. qp->sq_psn = params->sq_psn;
  1109. if (GET_FIELD(params->modify_flags,
  1110. QED_RDMA_MODIFY_QP_VALID_MAX_RD_ATOMIC_REQ))
  1111. qp->max_rd_atomic_req = params->max_rd_atomic_req;
  1112. if (GET_FIELD(params->modify_flags,
  1113. QED_RDMA_MODIFY_QP_VALID_MAX_RD_ATOMIC_RESP))
  1114. qp->max_rd_atomic_resp = params->max_rd_atomic_resp;
  1115. if (GET_FIELD(params->modify_flags,
  1116. QED_ROCE_MODIFY_QP_VALID_ACK_TIMEOUT))
  1117. qp->ack_timeout = params->ack_timeout;
  1118. if (GET_FIELD(params->modify_flags, QED_ROCE_MODIFY_QP_VALID_RETRY_CNT))
  1119. qp->retry_cnt = params->retry_cnt;
  1120. if (GET_FIELD(params->modify_flags,
  1121. QED_ROCE_MODIFY_QP_VALID_RNR_RETRY_CNT))
  1122. qp->rnr_retry_cnt = params->rnr_retry_cnt;
  1123. if (GET_FIELD(params->modify_flags,
  1124. QED_ROCE_MODIFY_QP_VALID_MIN_RNR_NAK_TIMER))
  1125. qp->min_rnr_nak_timer = params->min_rnr_nak_timer;
  1126. qp->sqd_async = params->sqd_async;
  1127. prev_state = qp->cur_state;
  1128. if (GET_FIELD(params->modify_flags,
  1129. QED_RDMA_MODIFY_QP_VALID_NEW_STATE)) {
  1130. qp->cur_state = params->new_state;
  1131. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "qp->cur_state=%d\n",
  1132. qp->cur_state);
  1133. }
  1134. if (QED_IS_IWARP_PERSONALITY(p_hwfn)) {
  1135. enum qed_iwarp_qp_state new_state =
  1136. qed_roce2iwarp_state(qp->cur_state);
  1137. rc = qed_iwarp_modify_qp(p_hwfn, qp, new_state, 0);
  1138. } else {
  1139. rc = qed_roce_modify_qp(p_hwfn, qp, prev_state, params);
  1140. }
  1141. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Modify QP, rc = %d\n", rc);
  1142. return rc;
  1143. }
  1144. static int
  1145. qed_rdma_register_tid(void *rdma_cxt,
  1146. struct qed_rdma_register_tid_in_params *params)
  1147. {
  1148. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  1149. struct rdma_register_tid_ramrod_data *p_ramrod;
  1150. struct qed_sp_init_data init_data;
  1151. struct qed_spq_entry *p_ent;
  1152. enum rdma_tid_type tid_type;
  1153. u8 fw_return_code;
  1154. int rc;
  1155. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "itid = %08x\n", params->itid);
  1156. /* Get SPQ entry */
  1157. memset(&init_data, 0, sizeof(init_data));
  1158. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  1159. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  1160. rc = qed_sp_init_request(p_hwfn, &p_ent, RDMA_RAMROD_REGISTER_MR,
  1161. p_hwfn->p_rdma_info->proto, &init_data);
  1162. if (rc) {
  1163. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = %d\n", rc);
  1164. return rc;
  1165. }
  1166. if (p_hwfn->p_rdma_info->last_tid < params->itid)
  1167. p_hwfn->p_rdma_info->last_tid = params->itid;
  1168. p_ramrod = &p_ent->ramrod.rdma_register_tid;
  1169. p_ramrod->flags = 0;
  1170. SET_FIELD(p_ramrod->flags,
  1171. RDMA_REGISTER_TID_RAMROD_DATA_TWO_LEVEL_PBL,
  1172. params->pbl_two_level);
  1173. SET_FIELD(p_ramrod->flags,
  1174. RDMA_REGISTER_TID_RAMROD_DATA_ZERO_BASED, params->zbva);
  1175. SET_FIELD(p_ramrod->flags,
  1176. RDMA_REGISTER_TID_RAMROD_DATA_PHY_MR, params->phy_mr);
  1177. /* Don't initialize D/C field, as it may override other bits. */
  1178. if (!(params->tid_type == QED_RDMA_TID_FMR) && !(params->dma_mr))
  1179. SET_FIELD(p_ramrod->flags,
  1180. RDMA_REGISTER_TID_RAMROD_DATA_PAGE_SIZE_LOG,
  1181. params->page_size_log - 12);
  1182. SET_FIELD(p_ramrod->flags,
  1183. RDMA_REGISTER_TID_RAMROD_DATA_REMOTE_READ,
  1184. params->remote_read);
  1185. SET_FIELD(p_ramrod->flags,
  1186. RDMA_REGISTER_TID_RAMROD_DATA_REMOTE_WRITE,
  1187. params->remote_write);
  1188. SET_FIELD(p_ramrod->flags,
  1189. RDMA_REGISTER_TID_RAMROD_DATA_REMOTE_ATOMIC,
  1190. params->remote_atomic);
  1191. SET_FIELD(p_ramrod->flags,
  1192. RDMA_REGISTER_TID_RAMROD_DATA_LOCAL_WRITE,
  1193. params->local_write);
  1194. SET_FIELD(p_ramrod->flags,
  1195. RDMA_REGISTER_TID_RAMROD_DATA_LOCAL_READ, params->local_read);
  1196. SET_FIELD(p_ramrod->flags,
  1197. RDMA_REGISTER_TID_RAMROD_DATA_ENABLE_MW_BIND,
  1198. params->mw_bind);
  1199. SET_FIELD(p_ramrod->flags1,
  1200. RDMA_REGISTER_TID_RAMROD_DATA_PBL_PAGE_SIZE_LOG,
  1201. params->pbl_page_size_log - 12);
  1202. SET_FIELD(p_ramrod->flags2,
  1203. RDMA_REGISTER_TID_RAMROD_DATA_DMA_MR, params->dma_mr);
  1204. switch (params->tid_type) {
  1205. case QED_RDMA_TID_REGISTERED_MR:
  1206. tid_type = RDMA_TID_REGISTERED_MR;
  1207. break;
  1208. case QED_RDMA_TID_FMR:
  1209. tid_type = RDMA_TID_FMR;
  1210. break;
  1211. case QED_RDMA_TID_MW_TYPE1:
  1212. tid_type = RDMA_TID_MW_TYPE1;
  1213. break;
  1214. case QED_RDMA_TID_MW_TYPE2A:
  1215. tid_type = RDMA_TID_MW_TYPE2A;
  1216. break;
  1217. default:
  1218. rc = -EINVAL;
  1219. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = %d\n", rc);
  1220. return rc;
  1221. }
  1222. SET_FIELD(p_ramrod->flags1,
  1223. RDMA_REGISTER_TID_RAMROD_DATA_TID_TYPE, tid_type);
  1224. p_ramrod->itid = cpu_to_le32(params->itid);
  1225. p_ramrod->key = params->key;
  1226. p_ramrod->pd = cpu_to_le16(params->pd);
  1227. p_ramrod->length_hi = (u8)(params->length >> 32);
  1228. p_ramrod->length_lo = DMA_LO_LE(params->length);
  1229. if (params->zbva) {
  1230. /* Lower 32 bits of the registered MR address.
  1231. * In case of zero based MR, will hold FBO
  1232. */
  1233. p_ramrod->va.hi = 0;
  1234. p_ramrod->va.lo = cpu_to_le32(params->fbo);
  1235. } else {
  1236. DMA_REGPAIR_LE(p_ramrod->va, params->vaddr);
  1237. }
  1238. DMA_REGPAIR_LE(p_ramrod->pbl_base, params->pbl_ptr);
  1239. /* DIF */
  1240. if (params->dif_enabled) {
  1241. SET_FIELD(p_ramrod->flags2,
  1242. RDMA_REGISTER_TID_RAMROD_DATA_DIF_ON_HOST_FLG, 1);
  1243. DMA_REGPAIR_LE(p_ramrod->dif_error_addr,
  1244. params->dif_error_addr);
  1245. DMA_REGPAIR_LE(p_ramrod->dif_runt_addr, params->dif_runt_addr);
  1246. }
  1247. rc = qed_spq_post(p_hwfn, p_ent, &fw_return_code);
  1248. if (rc)
  1249. return rc;
  1250. if (fw_return_code != RDMA_RETURN_OK) {
  1251. DP_NOTICE(p_hwfn, "fw_return_code = %d\n", fw_return_code);
  1252. return -EINVAL;
  1253. }
  1254. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Register TID, rc = %d\n", rc);
  1255. return rc;
  1256. }
  1257. static int qed_rdma_deregister_tid(void *rdma_cxt, u32 itid)
  1258. {
  1259. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  1260. struct rdma_deregister_tid_ramrod_data *p_ramrod;
  1261. struct qed_sp_init_data init_data;
  1262. struct qed_spq_entry *p_ent;
  1263. struct qed_ptt *p_ptt;
  1264. u8 fw_return_code;
  1265. int rc;
  1266. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "itid = %08x\n", itid);
  1267. /* Get SPQ entry */
  1268. memset(&init_data, 0, sizeof(init_data));
  1269. init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
  1270. init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
  1271. rc = qed_sp_init_request(p_hwfn, &p_ent, RDMA_RAMROD_DEREGISTER_MR,
  1272. p_hwfn->p_rdma_info->proto, &init_data);
  1273. if (rc) {
  1274. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = %d\n", rc);
  1275. return rc;
  1276. }
  1277. p_ramrod = &p_ent->ramrod.rdma_deregister_tid;
  1278. p_ramrod->itid = cpu_to_le32(itid);
  1279. rc = qed_spq_post(p_hwfn, p_ent, &fw_return_code);
  1280. if (rc) {
  1281. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "rc = %d\n", rc);
  1282. return rc;
  1283. }
  1284. if (fw_return_code == RDMA_RETURN_DEREGISTER_MR_BAD_STATE_ERR) {
  1285. DP_NOTICE(p_hwfn, "fw_return_code = %d\n", fw_return_code);
  1286. return -EINVAL;
  1287. } else if (fw_return_code == RDMA_RETURN_NIG_DRAIN_REQ) {
  1288. /* Bit indicating that the TID is in use and a nig drain is
  1289. * required before sending the ramrod again
  1290. */
  1291. p_ptt = qed_ptt_acquire(p_hwfn);
  1292. if (!p_ptt) {
  1293. rc = -EBUSY;
  1294. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  1295. "Failed to acquire PTT\n");
  1296. return rc;
  1297. }
  1298. rc = qed_mcp_drain(p_hwfn, p_ptt);
  1299. if (rc) {
  1300. qed_ptt_release(p_hwfn, p_ptt);
  1301. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  1302. "Drain failed\n");
  1303. return rc;
  1304. }
  1305. qed_ptt_release(p_hwfn, p_ptt);
  1306. /* Resend the ramrod */
  1307. rc = qed_sp_init_request(p_hwfn, &p_ent,
  1308. RDMA_RAMROD_DEREGISTER_MR,
  1309. p_hwfn->p_rdma_info->proto,
  1310. &init_data);
  1311. if (rc) {
  1312. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  1313. "Failed to init sp-element\n");
  1314. return rc;
  1315. }
  1316. rc = qed_spq_post(p_hwfn, p_ent, &fw_return_code);
  1317. if (rc) {
  1318. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  1319. "Ramrod failed\n");
  1320. return rc;
  1321. }
  1322. if (fw_return_code != RDMA_RETURN_OK) {
  1323. DP_NOTICE(p_hwfn, "fw_return_code = %d\n",
  1324. fw_return_code);
  1325. return rc;
  1326. }
  1327. }
  1328. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "De-registered TID, rc = %d\n", rc);
  1329. return rc;
  1330. }
  1331. static void *qed_rdma_get_rdma_ctx(struct qed_dev *cdev)
  1332. {
  1333. return QED_LEADING_HWFN(cdev);
  1334. }
  1335. bool qed_rdma_allocated_qps(struct qed_hwfn *p_hwfn)
  1336. {
  1337. bool result;
  1338. /* if rdma info has not been allocated, naturally there are no qps */
  1339. if (!p_hwfn->p_rdma_info)
  1340. return false;
  1341. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  1342. if (!p_hwfn->p_rdma_info->cid_map.bitmap)
  1343. result = false;
  1344. else
  1345. result = !qed_bmap_is_empty(&p_hwfn->p_rdma_info->cid_map);
  1346. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  1347. return result;
  1348. }
  1349. void qed_rdma_dpm_conf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  1350. {
  1351. u32 val;
  1352. val = (p_hwfn->dcbx_no_edpm || p_hwfn->db_bar_no_edpm) ? 0 : 1;
  1353. qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DPM_ENABLE, val);
  1354. DP_VERBOSE(p_hwfn, (QED_MSG_DCB | QED_MSG_RDMA),
  1355. "Changing DPM_EN state to %d (DCBX=%d, DB_BAR=%d)\n",
  1356. val, p_hwfn->dcbx_no_edpm, p_hwfn->db_bar_no_edpm);
  1357. }
  1358. void qed_rdma_dpm_bar(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  1359. {
  1360. p_hwfn->db_bar_no_edpm = true;
  1361. qed_rdma_dpm_conf(p_hwfn, p_ptt);
  1362. }
  1363. static int qed_rdma_start(void *rdma_cxt,
  1364. struct qed_rdma_start_in_params *params)
  1365. {
  1366. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  1367. struct qed_ptt *p_ptt;
  1368. int rc = -EBUSY;
  1369. DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
  1370. "desired_cnq = %08x\n", params->desired_cnq);
  1371. p_ptt = qed_ptt_acquire(p_hwfn);
  1372. if (!p_ptt)
  1373. goto err;
  1374. rc = qed_rdma_alloc(p_hwfn, p_ptt, params);
  1375. if (rc)
  1376. goto err1;
  1377. rc = qed_rdma_setup(p_hwfn, p_ptt, params);
  1378. if (rc)
  1379. goto err2;
  1380. qed_ptt_release(p_hwfn, p_ptt);
  1381. return rc;
  1382. err2:
  1383. qed_rdma_free(p_hwfn);
  1384. err1:
  1385. qed_ptt_release(p_hwfn, p_ptt);
  1386. err:
  1387. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA start - error, rc = %d\n", rc);
  1388. return rc;
  1389. }
  1390. static int qed_rdma_init(struct qed_dev *cdev,
  1391. struct qed_rdma_start_in_params *params)
  1392. {
  1393. return qed_rdma_start(QED_LEADING_HWFN(cdev), params);
  1394. }
  1395. static void qed_rdma_remove_user(void *rdma_cxt, u16 dpi)
  1396. {
  1397. struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
  1398. DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "dpi = %08x\n", dpi);
  1399. spin_lock_bh(&p_hwfn->p_rdma_info->lock);
  1400. qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->dpi_map, dpi);
  1401. spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
  1402. }
  1403. static int qed_roce_ll2_set_mac_filter(struct qed_dev *cdev,
  1404. u8 *old_mac_address,
  1405. u8 *new_mac_address)
  1406. {
  1407. struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
  1408. struct qed_ptt *p_ptt;
  1409. int rc = 0;
  1410. p_ptt = qed_ptt_acquire(p_hwfn);
  1411. if (!p_ptt) {
  1412. DP_ERR(cdev,
  1413. "qed roce ll2 mac filter set: failed to acquire PTT\n");
  1414. return -EINVAL;
  1415. }
  1416. if (old_mac_address)
  1417. qed_llh_remove_mac_filter(p_hwfn, p_ptt, old_mac_address);
  1418. if (new_mac_address)
  1419. rc = qed_llh_add_mac_filter(p_hwfn, p_ptt, new_mac_address);
  1420. qed_ptt_release(p_hwfn, p_ptt);
  1421. if (rc)
  1422. DP_ERR(cdev,
  1423. "qed roce ll2 mac filter set: failed to add MAC filter\n");
  1424. return rc;
  1425. }
  1426. static const struct qed_rdma_ops qed_rdma_ops_pass = {
  1427. .common = &qed_common_ops_pass,
  1428. .fill_dev_info = &qed_fill_rdma_dev_info,
  1429. .rdma_get_rdma_ctx = &qed_rdma_get_rdma_ctx,
  1430. .rdma_init = &qed_rdma_init,
  1431. .rdma_add_user = &qed_rdma_add_user,
  1432. .rdma_remove_user = &qed_rdma_remove_user,
  1433. .rdma_stop = &qed_rdma_stop,
  1434. .rdma_query_port = &qed_rdma_query_port,
  1435. .rdma_query_device = &qed_rdma_query_device,
  1436. .rdma_get_start_sb = &qed_rdma_get_sb_start,
  1437. .rdma_get_rdma_int = &qed_rdma_get_int,
  1438. .rdma_set_rdma_int = &qed_rdma_set_int,
  1439. .rdma_get_min_cnq_msix = &qed_rdma_get_min_cnq_msix,
  1440. .rdma_cnq_prod_update = &qed_rdma_cnq_prod_update,
  1441. .rdma_alloc_pd = &qed_rdma_alloc_pd,
  1442. .rdma_dealloc_pd = &qed_rdma_free_pd,
  1443. .rdma_create_cq = &qed_rdma_create_cq,
  1444. .rdma_destroy_cq = &qed_rdma_destroy_cq,
  1445. .rdma_create_qp = &qed_rdma_create_qp,
  1446. .rdma_modify_qp = &qed_rdma_modify_qp,
  1447. .rdma_query_qp = &qed_rdma_query_qp,
  1448. .rdma_destroy_qp = &qed_rdma_destroy_qp,
  1449. .rdma_alloc_tid = &qed_rdma_alloc_tid,
  1450. .rdma_free_tid = &qed_rdma_free_tid,
  1451. .rdma_register_tid = &qed_rdma_register_tid,
  1452. .rdma_deregister_tid = &qed_rdma_deregister_tid,
  1453. .ll2_acquire_connection = &qed_ll2_acquire_connection,
  1454. .ll2_establish_connection = &qed_ll2_establish_connection,
  1455. .ll2_terminate_connection = &qed_ll2_terminate_connection,
  1456. .ll2_release_connection = &qed_ll2_release_connection,
  1457. .ll2_post_rx_buffer = &qed_ll2_post_rx_buffer,
  1458. .ll2_prepare_tx_packet = &qed_ll2_prepare_tx_packet,
  1459. .ll2_set_fragment_of_tx_packet = &qed_ll2_set_fragment_of_tx_packet,
  1460. .ll2_set_mac_filter = &qed_roce_ll2_set_mac_filter,
  1461. .ll2_get_stats = &qed_ll2_get_stats,
  1462. .iwarp_connect = &qed_iwarp_connect,
  1463. .iwarp_create_listen = &qed_iwarp_create_listen,
  1464. .iwarp_destroy_listen = &qed_iwarp_destroy_listen,
  1465. .iwarp_accept = &qed_iwarp_accept,
  1466. .iwarp_reject = &qed_iwarp_reject,
  1467. .iwarp_send_rtr = &qed_iwarp_send_rtr,
  1468. };
  1469. const struct qed_rdma_ops *qed_get_rdma_ops(void)
  1470. {
  1471. return &qed_rdma_ops_pass;
  1472. }
  1473. EXPORT_SYMBOL(qed_get_rdma_ops);