qed_rdma.c 51 KB

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