qed_cxt.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015 QLogic Corporation
  3. *
  4. * This software is available under the terms of the GNU General Public License
  5. * (GPL) Version 2, available from the file COPYING in the main directory of
  6. * this source tree.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/bitops.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/log2.h>
  15. #include <linux/pci.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/bitops.h>
  19. #include "qed.h"
  20. #include "qed_cxt.h"
  21. #include "qed_dev_api.h"
  22. #include "qed_hsi.h"
  23. #include "qed_hw.h"
  24. #include "qed_init_ops.h"
  25. #include "qed_reg_addr.h"
  26. #include "qed_sriov.h"
  27. /* Max number of connection types in HW (DQ/CDU etc.) */
  28. #define MAX_CONN_TYPES PROTOCOLID_COMMON
  29. #define NUM_TASK_TYPES 2
  30. #define NUM_TASK_PF_SEGMENTS 4
  31. #define NUM_TASK_VF_SEGMENTS 1
  32. /* QM constants */
  33. #define QM_PQ_ELEMENT_SIZE 4 /* in bytes */
  34. /* Doorbell-Queue constants */
  35. #define DQ_RANGE_SHIFT 4
  36. #define DQ_RANGE_ALIGN BIT(DQ_RANGE_SHIFT)
  37. /* Searcher constants */
  38. #define SRC_MIN_NUM_ELEMS 256
  39. /* Timers constants */
  40. #define TM_SHIFT 7
  41. #define TM_ALIGN BIT(TM_SHIFT)
  42. #define TM_ELEM_SIZE 4
  43. /* For RoCE we configure to 64K to cover for RoCE max tasks 256K purpose. */
  44. #define ILT_DEFAULT_HW_P_SIZE (IS_ENABLED(CONFIG_QED_RDMA) ? 4 : 3)
  45. #define ILT_PAGE_IN_BYTES(hw_p_size) (1U << ((hw_p_size) + 12))
  46. #define ILT_CFG_REG(cli, reg) PSWRQ2_REG_ ## cli ## _ ## reg ## _RT_OFFSET
  47. /* ILT entry structure */
  48. #define ILT_ENTRY_PHY_ADDR_MASK 0x000FFFFFFFFFFFULL
  49. #define ILT_ENTRY_PHY_ADDR_SHIFT 0
  50. #define ILT_ENTRY_VALID_MASK 0x1ULL
  51. #define ILT_ENTRY_VALID_SHIFT 52
  52. #define ILT_ENTRY_IN_REGS 2
  53. #define ILT_REG_SIZE_IN_BYTES 4
  54. /* connection context union */
  55. union conn_context {
  56. struct core_conn_context core_ctx;
  57. struct eth_conn_context eth_ctx;
  58. struct iscsi_conn_context iscsi_ctx;
  59. struct roce_conn_context roce_ctx;
  60. };
  61. /* TYPE-0 task context - iSCSI */
  62. union type0_task_context {
  63. struct iscsi_task_context iscsi_ctx;
  64. };
  65. /* TYPE-1 task context - ROCE */
  66. union type1_task_context {
  67. struct rdma_task_context roce_ctx;
  68. };
  69. struct src_ent {
  70. u8 opaque[56];
  71. u64 next;
  72. };
  73. #define CDUT_SEG_ALIGNMET 3 /* in 4k chunks */
  74. #define CDUT_SEG_ALIGNMET_IN_BYTES (1 << (CDUT_SEG_ALIGNMET + 12))
  75. #define CONN_CXT_SIZE(p_hwfn) \
  76. ALIGNED_TYPE_SIZE(union conn_context, p_hwfn)
  77. #define SRQ_CXT_SIZE (sizeof(struct rdma_srq_context))
  78. #define TYPE0_TASK_CXT_SIZE(p_hwfn) \
  79. ALIGNED_TYPE_SIZE(union type0_task_context, p_hwfn)
  80. /* Alignment is inherent to the type1_task_context structure */
  81. #define TYPE1_TASK_CXT_SIZE(p_hwfn) sizeof(union type1_task_context)
  82. /* PF per protocl configuration object */
  83. #define TASK_SEGMENTS (NUM_TASK_PF_SEGMENTS + NUM_TASK_VF_SEGMENTS)
  84. #define TASK_SEGMENT_VF (NUM_TASK_PF_SEGMENTS)
  85. struct qed_tid_seg {
  86. u32 count;
  87. u8 type;
  88. bool has_fl_mem;
  89. };
  90. struct qed_conn_type_cfg {
  91. u32 cid_count;
  92. u32 cid_start;
  93. u32 cids_per_vf;
  94. struct qed_tid_seg tid_seg[TASK_SEGMENTS];
  95. };
  96. /* ILT Client configuration, Per connection type (protocol) resources. */
  97. #define ILT_CLI_PF_BLOCKS (1 + NUM_TASK_PF_SEGMENTS * 2)
  98. #define ILT_CLI_VF_BLOCKS (1 + NUM_TASK_VF_SEGMENTS * 2)
  99. #define CDUC_BLK (0)
  100. #define SRQ_BLK (0)
  101. #define CDUT_SEG_BLK(n) (1 + (u8)(n))
  102. #define CDUT_FL_SEG_BLK(n, X) (1 + (n) + NUM_TASK_ ## X ## _SEGMENTS)
  103. enum ilt_clients {
  104. ILT_CLI_CDUC,
  105. ILT_CLI_CDUT,
  106. ILT_CLI_QM,
  107. ILT_CLI_TM,
  108. ILT_CLI_SRC,
  109. ILT_CLI_TSDM,
  110. ILT_CLI_MAX
  111. };
  112. struct ilt_cfg_pair {
  113. u32 reg;
  114. u32 val;
  115. };
  116. struct qed_ilt_cli_blk {
  117. u32 total_size; /* 0 means not active */
  118. u32 real_size_in_page;
  119. u32 start_line;
  120. u32 dynamic_line_cnt;
  121. };
  122. struct qed_ilt_client_cfg {
  123. bool active;
  124. /* ILT boundaries */
  125. struct ilt_cfg_pair first;
  126. struct ilt_cfg_pair last;
  127. struct ilt_cfg_pair p_size;
  128. /* ILT client blocks for PF */
  129. struct qed_ilt_cli_blk pf_blks[ILT_CLI_PF_BLOCKS];
  130. u32 pf_total_lines;
  131. /* ILT client blocks for VFs */
  132. struct qed_ilt_cli_blk vf_blks[ILT_CLI_VF_BLOCKS];
  133. u32 vf_total_lines;
  134. };
  135. /* Per Path -
  136. * ILT shadow table
  137. * Protocol acquired CID lists
  138. * PF start line in ILT
  139. */
  140. struct qed_dma_mem {
  141. dma_addr_t p_phys;
  142. void *p_virt;
  143. size_t size;
  144. };
  145. struct qed_cid_acquired_map {
  146. u32 start_cid;
  147. u32 max_count;
  148. unsigned long *cid_map;
  149. };
  150. struct qed_cxt_mngr {
  151. /* Per protocl configuration */
  152. struct qed_conn_type_cfg conn_cfg[MAX_CONN_TYPES];
  153. /* computed ILT structure */
  154. struct qed_ilt_client_cfg clients[ILT_CLI_MAX];
  155. /* Task type sizes */
  156. u32 task_type_size[NUM_TASK_TYPES];
  157. /* total number of VFs for this hwfn -
  158. * ALL VFs are symmetric in terms of HW resources
  159. */
  160. u32 vf_count;
  161. /* total number of SRQ's for this hwfn */
  162. u32 srq_count;
  163. /* Acquired CIDs */
  164. struct qed_cid_acquired_map acquired[MAX_CONN_TYPES];
  165. /* ILT shadow table */
  166. struct qed_dma_mem *ilt_shadow;
  167. u32 pf_start_line;
  168. /* Mutex for a dynamic ILT allocation */
  169. struct mutex mutex;
  170. /* SRC T2 */
  171. struct qed_dma_mem *t2;
  172. u32 t2_num_pages;
  173. u64 first_free;
  174. u64 last_free;
  175. };
  176. static bool src_proto(enum protocol_type type)
  177. {
  178. return type == PROTOCOLID_ISCSI ||
  179. type == PROTOCOLID_ROCE;
  180. }
  181. static bool tm_cid_proto(enum protocol_type type)
  182. {
  183. return type == PROTOCOLID_ISCSI ||
  184. type == PROTOCOLID_ROCE;
  185. }
  186. /* counts the iids for the CDU/CDUC ILT client configuration */
  187. struct qed_cdu_iids {
  188. u32 pf_cids;
  189. u32 per_vf_cids;
  190. };
  191. static void qed_cxt_cdu_iids(struct qed_cxt_mngr *p_mngr,
  192. struct qed_cdu_iids *iids)
  193. {
  194. u32 type;
  195. for (type = 0; type < MAX_CONN_TYPES; type++) {
  196. iids->pf_cids += p_mngr->conn_cfg[type].cid_count;
  197. iids->per_vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
  198. }
  199. }
  200. /* counts the iids for the Searcher block configuration */
  201. struct qed_src_iids {
  202. u32 pf_cids;
  203. u32 per_vf_cids;
  204. };
  205. static void qed_cxt_src_iids(struct qed_cxt_mngr *p_mngr,
  206. struct qed_src_iids *iids)
  207. {
  208. u32 i;
  209. for (i = 0; i < MAX_CONN_TYPES; i++) {
  210. if (!src_proto(i))
  211. continue;
  212. iids->pf_cids += p_mngr->conn_cfg[i].cid_count;
  213. iids->per_vf_cids += p_mngr->conn_cfg[i].cids_per_vf;
  214. }
  215. }
  216. /* counts the iids for the Timers block configuration */
  217. struct qed_tm_iids {
  218. u32 pf_cids;
  219. u32 pf_tids[NUM_TASK_PF_SEGMENTS]; /* per segment */
  220. u32 pf_tids_total;
  221. u32 per_vf_cids;
  222. u32 per_vf_tids;
  223. };
  224. static void qed_cxt_tm_iids(struct qed_cxt_mngr *p_mngr,
  225. struct qed_tm_iids *iids)
  226. {
  227. u32 i, j;
  228. for (i = 0; i < MAX_CONN_TYPES; i++) {
  229. struct qed_conn_type_cfg *p_cfg = &p_mngr->conn_cfg[i];
  230. if (tm_cid_proto(i)) {
  231. iids->pf_cids += p_cfg->cid_count;
  232. iids->per_vf_cids += p_cfg->cids_per_vf;
  233. }
  234. }
  235. iids->pf_cids = roundup(iids->pf_cids, TM_ALIGN);
  236. iids->per_vf_cids = roundup(iids->per_vf_cids, TM_ALIGN);
  237. iids->per_vf_tids = roundup(iids->per_vf_tids, TM_ALIGN);
  238. for (iids->pf_tids_total = 0, j = 0; j < NUM_TASK_PF_SEGMENTS; j++) {
  239. iids->pf_tids[j] = roundup(iids->pf_tids[j], TM_ALIGN);
  240. iids->pf_tids_total += iids->pf_tids[j];
  241. }
  242. }
  243. static void qed_cxt_qm_iids(struct qed_hwfn *p_hwfn,
  244. struct qed_qm_iids *iids)
  245. {
  246. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  247. struct qed_tid_seg *segs;
  248. u32 vf_cids = 0, type, j;
  249. u32 vf_tids = 0;
  250. for (type = 0; type < MAX_CONN_TYPES; type++) {
  251. iids->cids += p_mngr->conn_cfg[type].cid_count;
  252. vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
  253. segs = p_mngr->conn_cfg[type].tid_seg;
  254. /* for each segment there is at most one
  255. * protocol for which count is not 0.
  256. */
  257. for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
  258. iids->tids += segs[j].count;
  259. /* The last array elelment is for the VFs. As for PF
  260. * segments there can be only one protocol for
  261. * which this value is not 0.
  262. */
  263. vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
  264. }
  265. iids->vf_cids += vf_cids * p_mngr->vf_count;
  266. iids->tids += vf_tids * p_mngr->vf_count;
  267. DP_VERBOSE(p_hwfn, QED_MSG_ILT,
  268. "iids: CIDS %08x vf_cids %08x tids %08x vf_tids %08x\n",
  269. iids->cids, iids->vf_cids, iids->tids, vf_tids);
  270. }
  271. static struct qed_tid_seg *qed_cxt_tid_seg_info(struct qed_hwfn *p_hwfn,
  272. u32 seg)
  273. {
  274. struct qed_cxt_mngr *p_cfg = p_hwfn->p_cxt_mngr;
  275. u32 i;
  276. /* Find the protocol with tid count > 0 for this segment.
  277. * Note: there can only be one and this is already validated.
  278. */
  279. for (i = 0; i < MAX_CONN_TYPES; i++)
  280. if (p_cfg->conn_cfg[i].tid_seg[seg].count)
  281. return &p_cfg->conn_cfg[i].tid_seg[seg];
  282. return NULL;
  283. }
  284. static void qed_cxt_set_srq_count(struct qed_hwfn *p_hwfn, u32 num_srqs)
  285. {
  286. struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
  287. p_mgr->srq_count = num_srqs;
  288. }
  289. static u32 qed_cxt_get_srq_count(struct qed_hwfn *p_hwfn)
  290. {
  291. struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
  292. return p_mgr->srq_count;
  293. }
  294. /* set the iids count per protocol */
  295. static void qed_cxt_set_proto_cid_count(struct qed_hwfn *p_hwfn,
  296. enum protocol_type type,
  297. u32 cid_count, u32 vf_cid_cnt)
  298. {
  299. struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
  300. struct qed_conn_type_cfg *p_conn = &p_mgr->conn_cfg[type];
  301. p_conn->cid_count = roundup(cid_count, DQ_RANGE_ALIGN);
  302. p_conn->cids_per_vf = roundup(vf_cid_cnt, DQ_RANGE_ALIGN);
  303. if (type == PROTOCOLID_ROCE) {
  304. u32 page_sz = p_mgr->clients[ILT_CLI_CDUC].p_size.val;
  305. u32 cxt_size = CONN_CXT_SIZE(p_hwfn);
  306. u32 elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
  307. p_conn->cid_count = roundup(p_conn->cid_count, elems_per_page);
  308. }
  309. }
  310. u32 qed_cxt_get_proto_cid_count(struct qed_hwfn *p_hwfn,
  311. enum protocol_type type, u32 *vf_cid)
  312. {
  313. if (vf_cid)
  314. *vf_cid = p_hwfn->p_cxt_mngr->conn_cfg[type].cids_per_vf;
  315. return p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
  316. }
  317. u32 qed_cxt_get_proto_cid_start(struct qed_hwfn *p_hwfn,
  318. enum protocol_type type)
  319. {
  320. return p_hwfn->p_cxt_mngr->acquired[type].start_cid;
  321. }
  322. u32 qed_cxt_get_proto_tid_count(struct qed_hwfn *p_hwfn,
  323. enum protocol_type type)
  324. {
  325. u32 cnt = 0;
  326. int i;
  327. for (i = 0; i < TASK_SEGMENTS; i++)
  328. cnt += p_hwfn->p_cxt_mngr->conn_cfg[type].tid_seg[i].count;
  329. return cnt;
  330. }
  331. static void qed_cxt_set_proto_tid_count(struct qed_hwfn *p_hwfn,
  332. enum protocol_type proto,
  333. u8 seg,
  334. u8 seg_type, u32 count, bool has_fl)
  335. {
  336. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  337. struct qed_tid_seg *p_seg = &p_mngr->conn_cfg[proto].tid_seg[seg];
  338. p_seg->count = count;
  339. p_seg->has_fl_mem = has_fl;
  340. p_seg->type = seg_type;
  341. }
  342. static void qed_ilt_cli_blk_fill(struct qed_ilt_client_cfg *p_cli,
  343. struct qed_ilt_cli_blk *p_blk,
  344. u32 start_line, u32 total_size, u32 elem_size)
  345. {
  346. u32 ilt_size = ILT_PAGE_IN_BYTES(p_cli->p_size.val);
  347. /* verify thatits called only once for each block */
  348. if (p_blk->total_size)
  349. return;
  350. p_blk->total_size = total_size;
  351. p_blk->real_size_in_page = 0;
  352. if (elem_size)
  353. p_blk->real_size_in_page = (ilt_size / elem_size) * elem_size;
  354. p_blk->start_line = start_line;
  355. }
  356. static void qed_ilt_cli_adv_line(struct qed_hwfn *p_hwfn,
  357. struct qed_ilt_client_cfg *p_cli,
  358. struct qed_ilt_cli_blk *p_blk,
  359. u32 *p_line, enum ilt_clients client_id)
  360. {
  361. if (!p_blk->total_size)
  362. return;
  363. if (!p_cli->active)
  364. p_cli->first.val = *p_line;
  365. p_cli->active = true;
  366. *p_line += DIV_ROUND_UP(p_blk->total_size, p_blk->real_size_in_page);
  367. p_cli->last.val = *p_line - 1;
  368. DP_VERBOSE(p_hwfn, QED_MSG_ILT,
  369. "ILT[Client %d] - Lines: [%08x - %08x]. Block - Size %08x [Real %08x] Start line %d\n",
  370. client_id, p_cli->first.val,
  371. p_cli->last.val, p_blk->total_size,
  372. p_blk->real_size_in_page, p_blk->start_line);
  373. }
  374. static u32 qed_ilt_get_dynamic_line_cnt(struct qed_hwfn *p_hwfn,
  375. enum ilt_clients ilt_client)
  376. {
  377. u32 cid_count = p_hwfn->p_cxt_mngr->conn_cfg[PROTOCOLID_ROCE].cid_count;
  378. struct qed_ilt_client_cfg *p_cli;
  379. u32 lines_to_skip = 0;
  380. u32 cxts_per_p;
  381. if (ilt_client == ILT_CLI_CDUC) {
  382. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
  383. cxts_per_p = ILT_PAGE_IN_BYTES(p_cli->p_size.val) /
  384. (u32) CONN_CXT_SIZE(p_hwfn);
  385. lines_to_skip = cid_count / cxts_per_p;
  386. }
  387. return lines_to_skip;
  388. }
  389. int qed_cxt_cfg_ilt_compute(struct qed_hwfn *p_hwfn)
  390. {
  391. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  392. u32 curr_line, total, i, task_size, line;
  393. struct qed_ilt_client_cfg *p_cli;
  394. struct qed_ilt_cli_blk *p_blk;
  395. struct qed_cdu_iids cdu_iids;
  396. struct qed_src_iids src_iids;
  397. struct qed_qm_iids qm_iids;
  398. struct qed_tm_iids tm_iids;
  399. struct qed_tid_seg *p_seg;
  400. memset(&qm_iids, 0, sizeof(qm_iids));
  401. memset(&cdu_iids, 0, sizeof(cdu_iids));
  402. memset(&src_iids, 0, sizeof(src_iids));
  403. memset(&tm_iids, 0, sizeof(tm_iids));
  404. p_mngr->pf_start_line = RESC_START(p_hwfn, QED_ILT);
  405. DP_VERBOSE(p_hwfn, QED_MSG_ILT,
  406. "hwfn [%d] - Set context manager starting line to be 0x%08x\n",
  407. p_hwfn->my_id, p_hwfn->p_cxt_mngr->pf_start_line);
  408. /* CDUC */
  409. p_cli = &p_mngr->clients[ILT_CLI_CDUC];
  410. curr_line = p_mngr->pf_start_line;
  411. /* CDUC PF */
  412. p_cli->pf_total_lines = 0;
  413. /* get the counters for the CDUC and QM clients */
  414. qed_cxt_cdu_iids(p_mngr, &cdu_iids);
  415. p_blk = &p_cli->pf_blks[CDUC_BLK];
  416. total = cdu_iids.pf_cids * CONN_CXT_SIZE(p_hwfn);
  417. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
  418. total, CONN_CXT_SIZE(p_hwfn));
  419. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
  420. p_cli->pf_total_lines = curr_line - p_blk->start_line;
  421. p_blk->dynamic_line_cnt = qed_ilt_get_dynamic_line_cnt(p_hwfn,
  422. ILT_CLI_CDUC);
  423. /* CDUC VF */
  424. p_blk = &p_cli->vf_blks[CDUC_BLK];
  425. total = cdu_iids.per_vf_cids * CONN_CXT_SIZE(p_hwfn);
  426. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
  427. total, CONN_CXT_SIZE(p_hwfn));
  428. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
  429. p_cli->vf_total_lines = curr_line - p_blk->start_line;
  430. for (i = 1; i < p_mngr->vf_count; i++)
  431. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  432. ILT_CLI_CDUC);
  433. /* CDUT PF */
  434. p_cli = &p_mngr->clients[ILT_CLI_CDUT];
  435. p_cli->first.val = curr_line;
  436. /* first the 'working' task memory */
  437. for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
  438. p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
  439. if (!p_seg || p_seg->count == 0)
  440. continue;
  441. p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(i)];
  442. total = p_seg->count * p_mngr->task_type_size[p_seg->type];
  443. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line, total,
  444. p_mngr->task_type_size[p_seg->type]);
  445. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  446. ILT_CLI_CDUT);
  447. }
  448. /* next the 'init' task memory (forced load memory) */
  449. for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
  450. p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
  451. if (!p_seg || p_seg->count == 0)
  452. continue;
  453. p_blk = &p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)];
  454. if (!p_seg->has_fl_mem) {
  455. /* The segment is active (total size pf 'working'
  456. * memory is > 0) but has no FL (forced-load, Init)
  457. * memory. Thus:
  458. *
  459. * 1. The total-size in the corrsponding FL block of
  460. * the ILT client is set to 0 - No ILT line are
  461. * provisioned and no ILT memory allocated.
  462. *
  463. * 2. The start-line of said block is set to the
  464. * start line of the matching working memory
  465. * block in the ILT client. This is later used to
  466. * configure the CDU segment offset registers and
  467. * results in an FL command for TIDs of this
  468. * segement behaves as regular load commands
  469. * (loading TIDs from the working memory).
  470. */
  471. line = p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line;
  472. qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
  473. continue;
  474. }
  475. total = p_seg->count * p_mngr->task_type_size[p_seg->type];
  476. qed_ilt_cli_blk_fill(p_cli, p_blk,
  477. curr_line, total,
  478. p_mngr->task_type_size[p_seg->type]);
  479. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  480. ILT_CLI_CDUT);
  481. }
  482. p_cli->pf_total_lines = curr_line - p_cli->pf_blks[0].start_line;
  483. /* CDUT VF */
  484. p_seg = qed_cxt_tid_seg_info(p_hwfn, TASK_SEGMENT_VF);
  485. if (p_seg && p_seg->count) {
  486. /* Stricly speaking we need to iterate over all VF
  487. * task segment types, but a VF has only 1 segment
  488. */
  489. /* 'working' memory */
  490. total = p_seg->count * p_mngr->task_type_size[p_seg->type];
  491. p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
  492. qed_ilt_cli_blk_fill(p_cli, p_blk,
  493. curr_line, total,
  494. p_mngr->task_type_size[p_seg->type]);
  495. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  496. ILT_CLI_CDUT);
  497. /* 'init' memory */
  498. p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
  499. if (!p_seg->has_fl_mem) {
  500. /* see comment above */
  501. line = p_cli->vf_blks[CDUT_SEG_BLK(0)].start_line;
  502. qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
  503. } else {
  504. task_size = p_mngr->task_type_size[p_seg->type];
  505. qed_ilt_cli_blk_fill(p_cli, p_blk,
  506. curr_line, total, task_size);
  507. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  508. ILT_CLI_CDUT);
  509. }
  510. p_cli->vf_total_lines = curr_line -
  511. p_cli->vf_blks[0].start_line;
  512. /* Now for the rest of the VFs */
  513. for (i = 1; i < p_mngr->vf_count; i++) {
  514. p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
  515. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  516. ILT_CLI_CDUT);
  517. p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
  518. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  519. ILT_CLI_CDUT);
  520. }
  521. }
  522. /* QM */
  523. p_cli = &p_mngr->clients[ILT_CLI_QM];
  524. p_blk = &p_cli->pf_blks[0];
  525. qed_cxt_qm_iids(p_hwfn, &qm_iids);
  526. total = qed_qm_pf_mem_size(p_hwfn->rel_pf_id, qm_iids.cids,
  527. qm_iids.vf_cids, qm_iids.tids,
  528. p_hwfn->qm_info.num_pqs,
  529. p_hwfn->qm_info.num_vf_pqs);
  530. DP_VERBOSE(p_hwfn,
  531. QED_MSG_ILT,
  532. "QM ILT Info, (cids=%d, vf_cids=%d, tids=%d, num_pqs=%d, num_vf_pqs=%d, memory_size=%d)\n",
  533. qm_iids.cids,
  534. qm_iids.vf_cids,
  535. qm_iids.tids,
  536. p_hwfn->qm_info.num_pqs, p_hwfn->qm_info.num_vf_pqs, total);
  537. qed_ilt_cli_blk_fill(p_cli, p_blk,
  538. curr_line, total * 0x1000,
  539. QM_PQ_ELEMENT_SIZE);
  540. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_QM);
  541. p_cli->pf_total_lines = curr_line - p_blk->start_line;
  542. /* SRC */
  543. p_cli = &p_mngr->clients[ILT_CLI_SRC];
  544. qed_cxt_src_iids(p_mngr, &src_iids);
  545. /* Both the PF and VFs searcher connections are stored in the per PF
  546. * database. Thus sum the PF searcher cids and all the VFs searcher
  547. * cids.
  548. */
  549. total = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
  550. if (total) {
  551. u32 local_max = max_t(u32, total,
  552. SRC_MIN_NUM_ELEMS);
  553. total = roundup_pow_of_two(local_max);
  554. p_blk = &p_cli->pf_blks[0];
  555. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
  556. total * sizeof(struct src_ent),
  557. sizeof(struct src_ent));
  558. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  559. ILT_CLI_SRC);
  560. p_cli->pf_total_lines = curr_line - p_blk->start_line;
  561. }
  562. /* TM PF */
  563. p_cli = &p_mngr->clients[ILT_CLI_TM];
  564. qed_cxt_tm_iids(p_mngr, &tm_iids);
  565. total = tm_iids.pf_cids + tm_iids.pf_tids_total;
  566. if (total) {
  567. p_blk = &p_cli->pf_blks[0];
  568. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
  569. total * TM_ELEM_SIZE, TM_ELEM_SIZE);
  570. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  571. ILT_CLI_TM);
  572. p_cli->pf_total_lines = curr_line - p_blk->start_line;
  573. }
  574. /* TM VF */
  575. total = tm_iids.per_vf_cids + tm_iids.per_vf_tids;
  576. if (total) {
  577. p_blk = &p_cli->vf_blks[0];
  578. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
  579. total * TM_ELEM_SIZE, TM_ELEM_SIZE);
  580. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  581. ILT_CLI_TM);
  582. p_cli->pf_total_lines = curr_line - p_blk->start_line;
  583. for (i = 1; i < p_mngr->vf_count; i++)
  584. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  585. ILT_CLI_TM);
  586. }
  587. /* TSDM (SRQ CONTEXT) */
  588. total = qed_cxt_get_srq_count(p_hwfn);
  589. if (total) {
  590. p_cli = &p_mngr->clients[ILT_CLI_TSDM];
  591. p_blk = &p_cli->pf_blks[SRQ_BLK];
  592. qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
  593. total * SRQ_CXT_SIZE, SRQ_CXT_SIZE);
  594. qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
  595. ILT_CLI_TSDM);
  596. p_cli->pf_total_lines = curr_line - p_blk->start_line;
  597. }
  598. if (curr_line - p_hwfn->p_cxt_mngr->pf_start_line >
  599. RESC_NUM(p_hwfn, QED_ILT)) {
  600. DP_ERR(p_hwfn, "too many ilt lines...#lines=%d\n",
  601. curr_line - p_hwfn->p_cxt_mngr->pf_start_line);
  602. return -EINVAL;
  603. }
  604. return 0;
  605. }
  606. static void qed_cxt_src_t2_free(struct qed_hwfn *p_hwfn)
  607. {
  608. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  609. u32 i;
  610. if (!p_mngr->t2)
  611. return;
  612. for (i = 0; i < p_mngr->t2_num_pages; i++)
  613. if (p_mngr->t2[i].p_virt)
  614. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  615. p_mngr->t2[i].size,
  616. p_mngr->t2[i].p_virt,
  617. p_mngr->t2[i].p_phys);
  618. kfree(p_mngr->t2);
  619. p_mngr->t2 = NULL;
  620. }
  621. static int qed_cxt_src_t2_alloc(struct qed_hwfn *p_hwfn)
  622. {
  623. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  624. u32 conn_num, total_size, ent_per_page, psz, i;
  625. struct qed_ilt_client_cfg *p_src;
  626. struct qed_src_iids src_iids;
  627. struct qed_dma_mem *p_t2;
  628. int rc;
  629. memset(&src_iids, 0, sizeof(src_iids));
  630. /* if the SRC ILT client is inactive - there are no connection
  631. * requiring the searcer, leave.
  632. */
  633. p_src = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_SRC];
  634. if (!p_src->active)
  635. return 0;
  636. qed_cxt_src_iids(p_mngr, &src_iids);
  637. conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
  638. total_size = conn_num * sizeof(struct src_ent);
  639. /* use the same page size as the SRC ILT client */
  640. psz = ILT_PAGE_IN_BYTES(p_src->p_size.val);
  641. p_mngr->t2_num_pages = DIV_ROUND_UP(total_size, psz);
  642. /* allocate t2 */
  643. p_mngr->t2 = kcalloc(p_mngr->t2_num_pages, sizeof(struct qed_dma_mem),
  644. GFP_KERNEL);
  645. if (!p_mngr->t2) {
  646. rc = -ENOMEM;
  647. goto t2_fail;
  648. }
  649. /* allocate t2 pages */
  650. for (i = 0; i < p_mngr->t2_num_pages; i++) {
  651. u32 size = min_t(u32, total_size, psz);
  652. void **p_virt = &p_mngr->t2[i].p_virt;
  653. *p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  654. size,
  655. &p_mngr->t2[i].p_phys, GFP_KERNEL);
  656. if (!p_mngr->t2[i].p_virt) {
  657. rc = -ENOMEM;
  658. goto t2_fail;
  659. }
  660. memset(*p_virt, 0, size);
  661. p_mngr->t2[i].size = size;
  662. total_size -= size;
  663. }
  664. /* Set the t2 pointers */
  665. /* entries per page - must be a power of two */
  666. ent_per_page = psz / sizeof(struct src_ent);
  667. p_mngr->first_free = (u64) p_mngr->t2[0].p_phys;
  668. p_t2 = &p_mngr->t2[(conn_num - 1) / ent_per_page];
  669. p_mngr->last_free = (u64) p_t2->p_phys +
  670. ((conn_num - 1) & (ent_per_page - 1)) * sizeof(struct src_ent);
  671. for (i = 0; i < p_mngr->t2_num_pages; i++) {
  672. u32 ent_num = min_t(u32,
  673. ent_per_page,
  674. conn_num);
  675. struct src_ent *entries = p_mngr->t2[i].p_virt;
  676. u64 p_ent_phys = (u64) p_mngr->t2[i].p_phys, val;
  677. u32 j;
  678. for (j = 0; j < ent_num - 1; j++) {
  679. val = p_ent_phys + (j + 1) * sizeof(struct src_ent);
  680. entries[j].next = cpu_to_be64(val);
  681. }
  682. if (i < p_mngr->t2_num_pages - 1)
  683. val = (u64) p_mngr->t2[i + 1].p_phys;
  684. else
  685. val = 0;
  686. entries[j].next = cpu_to_be64(val);
  687. conn_num -= ent_num;
  688. }
  689. return 0;
  690. t2_fail:
  691. qed_cxt_src_t2_free(p_hwfn);
  692. return rc;
  693. }
  694. #define for_each_ilt_valid_client(pos, clients) \
  695. for (pos = 0; pos < ILT_CLI_MAX; pos++) \
  696. if (!clients[pos].active) { \
  697. continue; \
  698. } else \
  699. /* Total number of ILT lines used by this PF */
  700. static u32 qed_cxt_ilt_shadow_size(struct qed_ilt_client_cfg *ilt_clients)
  701. {
  702. u32 size = 0;
  703. u32 i;
  704. for_each_ilt_valid_client(i, ilt_clients)
  705. size += (ilt_clients[i].last.val - ilt_clients[i].first.val + 1);
  706. return size;
  707. }
  708. static void qed_ilt_shadow_free(struct qed_hwfn *p_hwfn)
  709. {
  710. struct qed_ilt_client_cfg *p_cli = p_hwfn->p_cxt_mngr->clients;
  711. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  712. u32 ilt_size, i;
  713. ilt_size = qed_cxt_ilt_shadow_size(p_cli);
  714. for (i = 0; p_mngr->ilt_shadow && i < ilt_size; i++) {
  715. struct qed_dma_mem *p_dma = &p_mngr->ilt_shadow[i];
  716. if (p_dma->p_virt)
  717. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  718. p_dma->size, p_dma->p_virt,
  719. p_dma->p_phys);
  720. p_dma->p_virt = NULL;
  721. }
  722. kfree(p_mngr->ilt_shadow);
  723. }
  724. static int qed_ilt_blk_alloc(struct qed_hwfn *p_hwfn,
  725. struct qed_ilt_cli_blk *p_blk,
  726. enum ilt_clients ilt_client,
  727. u32 start_line_offset)
  728. {
  729. struct qed_dma_mem *ilt_shadow = p_hwfn->p_cxt_mngr->ilt_shadow;
  730. u32 lines, line, sz_left, lines_to_skip = 0;
  731. /* Special handling for RoCE that supports dynamic allocation */
  732. if ((p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) &&
  733. ((ilt_client == ILT_CLI_CDUT) || ilt_client == ILT_CLI_TSDM))
  734. return 0;
  735. lines_to_skip = p_blk->dynamic_line_cnt;
  736. if (!p_blk->total_size)
  737. return 0;
  738. sz_left = p_blk->total_size;
  739. lines = DIV_ROUND_UP(sz_left, p_blk->real_size_in_page) - lines_to_skip;
  740. line = p_blk->start_line + start_line_offset -
  741. p_hwfn->p_cxt_mngr->pf_start_line + lines_to_skip;
  742. for (; lines; lines--) {
  743. dma_addr_t p_phys;
  744. void *p_virt;
  745. u32 size;
  746. size = min_t(u32, sz_left, p_blk->real_size_in_page);
  747. p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  748. size, &p_phys, GFP_KERNEL);
  749. if (!p_virt)
  750. return -ENOMEM;
  751. memset(p_virt, 0, size);
  752. ilt_shadow[line].p_phys = p_phys;
  753. ilt_shadow[line].p_virt = p_virt;
  754. ilt_shadow[line].size = size;
  755. DP_VERBOSE(p_hwfn, QED_MSG_ILT,
  756. "ILT shadow: Line [%d] Physical 0x%llx Virtual %p Size %d\n",
  757. line, (u64)p_phys, p_virt, size);
  758. sz_left -= size;
  759. line++;
  760. }
  761. return 0;
  762. }
  763. static int qed_ilt_shadow_alloc(struct qed_hwfn *p_hwfn)
  764. {
  765. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  766. struct qed_ilt_client_cfg *clients = p_mngr->clients;
  767. struct qed_ilt_cli_blk *p_blk;
  768. u32 size, i, j, k;
  769. int rc;
  770. size = qed_cxt_ilt_shadow_size(clients);
  771. p_mngr->ilt_shadow = kcalloc(size, sizeof(struct qed_dma_mem),
  772. GFP_KERNEL);
  773. if (!p_mngr->ilt_shadow) {
  774. rc = -ENOMEM;
  775. goto ilt_shadow_fail;
  776. }
  777. DP_VERBOSE(p_hwfn, QED_MSG_ILT,
  778. "Allocated 0x%x bytes for ilt shadow\n",
  779. (u32)(size * sizeof(struct qed_dma_mem)));
  780. for_each_ilt_valid_client(i, clients) {
  781. for (j = 0; j < ILT_CLI_PF_BLOCKS; j++) {
  782. p_blk = &clients[i].pf_blks[j];
  783. rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, 0);
  784. if (rc)
  785. goto ilt_shadow_fail;
  786. }
  787. for (k = 0; k < p_mngr->vf_count; k++) {
  788. for (j = 0; j < ILT_CLI_VF_BLOCKS; j++) {
  789. u32 lines = clients[i].vf_total_lines * k;
  790. p_blk = &clients[i].vf_blks[j];
  791. rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, lines);
  792. if (rc)
  793. goto ilt_shadow_fail;
  794. }
  795. }
  796. }
  797. return 0;
  798. ilt_shadow_fail:
  799. qed_ilt_shadow_free(p_hwfn);
  800. return rc;
  801. }
  802. static void qed_cid_map_free(struct qed_hwfn *p_hwfn)
  803. {
  804. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  805. u32 type;
  806. for (type = 0; type < MAX_CONN_TYPES; type++) {
  807. kfree(p_mngr->acquired[type].cid_map);
  808. p_mngr->acquired[type].max_count = 0;
  809. p_mngr->acquired[type].start_cid = 0;
  810. }
  811. }
  812. static int qed_cid_map_alloc(struct qed_hwfn *p_hwfn)
  813. {
  814. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  815. u32 start_cid = 0;
  816. u32 type;
  817. for (type = 0; type < MAX_CONN_TYPES; type++) {
  818. u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
  819. u32 size;
  820. if (cid_cnt == 0)
  821. continue;
  822. size = DIV_ROUND_UP(cid_cnt,
  823. sizeof(unsigned long) * BITS_PER_BYTE) *
  824. sizeof(unsigned long);
  825. p_mngr->acquired[type].cid_map = kzalloc(size, GFP_KERNEL);
  826. if (!p_mngr->acquired[type].cid_map)
  827. goto cid_map_fail;
  828. p_mngr->acquired[type].max_count = cid_cnt;
  829. p_mngr->acquired[type].start_cid = start_cid;
  830. p_hwfn->p_cxt_mngr->conn_cfg[type].cid_start = start_cid;
  831. DP_VERBOSE(p_hwfn, QED_MSG_CXT,
  832. "Type %08x start: %08x count %08x\n",
  833. type, p_mngr->acquired[type].start_cid,
  834. p_mngr->acquired[type].max_count);
  835. start_cid += cid_cnt;
  836. }
  837. return 0;
  838. cid_map_fail:
  839. qed_cid_map_free(p_hwfn);
  840. return -ENOMEM;
  841. }
  842. int qed_cxt_mngr_alloc(struct qed_hwfn *p_hwfn)
  843. {
  844. struct qed_ilt_client_cfg *clients;
  845. struct qed_cxt_mngr *p_mngr;
  846. u32 i;
  847. p_mngr = kzalloc(sizeof(*p_mngr), GFP_KERNEL);
  848. if (!p_mngr)
  849. return -ENOMEM;
  850. /* Initialize ILT client registers */
  851. clients = p_mngr->clients;
  852. clients[ILT_CLI_CDUC].first.reg = ILT_CFG_REG(CDUC, FIRST_ILT);
  853. clients[ILT_CLI_CDUC].last.reg = ILT_CFG_REG(CDUC, LAST_ILT);
  854. clients[ILT_CLI_CDUC].p_size.reg = ILT_CFG_REG(CDUC, P_SIZE);
  855. clients[ILT_CLI_QM].first.reg = ILT_CFG_REG(QM, FIRST_ILT);
  856. clients[ILT_CLI_QM].last.reg = ILT_CFG_REG(QM, LAST_ILT);
  857. clients[ILT_CLI_QM].p_size.reg = ILT_CFG_REG(QM, P_SIZE);
  858. clients[ILT_CLI_TM].first.reg = ILT_CFG_REG(TM, FIRST_ILT);
  859. clients[ILT_CLI_TM].last.reg = ILT_CFG_REG(TM, LAST_ILT);
  860. clients[ILT_CLI_TM].p_size.reg = ILT_CFG_REG(TM, P_SIZE);
  861. clients[ILT_CLI_SRC].first.reg = ILT_CFG_REG(SRC, FIRST_ILT);
  862. clients[ILT_CLI_SRC].last.reg = ILT_CFG_REG(SRC, LAST_ILT);
  863. clients[ILT_CLI_SRC].p_size.reg = ILT_CFG_REG(SRC, P_SIZE);
  864. clients[ILT_CLI_CDUT].first.reg = ILT_CFG_REG(CDUT, FIRST_ILT);
  865. clients[ILT_CLI_CDUT].last.reg = ILT_CFG_REG(CDUT, LAST_ILT);
  866. clients[ILT_CLI_CDUT].p_size.reg = ILT_CFG_REG(CDUT, P_SIZE);
  867. clients[ILT_CLI_TSDM].first.reg = ILT_CFG_REG(TSDM, FIRST_ILT);
  868. clients[ILT_CLI_TSDM].last.reg = ILT_CFG_REG(TSDM, LAST_ILT);
  869. clients[ILT_CLI_TSDM].p_size.reg = ILT_CFG_REG(TSDM, P_SIZE);
  870. /* default ILT page size for all clients is 32K */
  871. for (i = 0; i < ILT_CLI_MAX; i++)
  872. p_mngr->clients[i].p_size.val = ILT_DEFAULT_HW_P_SIZE;
  873. /* Initialize task sizes */
  874. p_mngr->task_type_size[0] = TYPE0_TASK_CXT_SIZE(p_hwfn);
  875. p_mngr->task_type_size[1] = TYPE1_TASK_CXT_SIZE(p_hwfn);
  876. if (p_hwfn->cdev->p_iov_info)
  877. p_mngr->vf_count = p_hwfn->cdev->p_iov_info->total_vfs;
  878. /* Initialize the dynamic ILT allocation mutex */
  879. mutex_init(&p_mngr->mutex);
  880. /* Set the cxt mangr pointer priori to further allocations */
  881. p_hwfn->p_cxt_mngr = p_mngr;
  882. return 0;
  883. }
  884. int qed_cxt_tables_alloc(struct qed_hwfn *p_hwfn)
  885. {
  886. int rc;
  887. /* Allocate the ILT shadow table */
  888. rc = qed_ilt_shadow_alloc(p_hwfn);
  889. if (rc)
  890. goto tables_alloc_fail;
  891. /* Allocate the T2 table */
  892. rc = qed_cxt_src_t2_alloc(p_hwfn);
  893. if (rc)
  894. goto tables_alloc_fail;
  895. /* Allocate and initialize the acquired cids bitmaps */
  896. rc = qed_cid_map_alloc(p_hwfn);
  897. if (rc)
  898. goto tables_alloc_fail;
  899. return 0;
  900. tables_alloc_fail:
  901. qed_cxt_mngr_free(p_hwfn);
  902. return rc;
  903. }
  904. void qed_cxt_mngr_free(struct qed_hwfn *p_hwfn)
  905. {
  906. if (!p_hwfn->p_cxt_mngr)
  907. return;
  908. qed_cid_map_free(p_hwfn);
  909. qed_cxt_src_t2_free(p_hwfn);
  910. qed_ilt_shadow_free(p_hwfn);
  911. kfree(p_hwfn->p_cxt_mngr);
  912. p_hwfn->p_cxt_mngr = NULL;
  913. }
  914. void qed_cxt_mngr_setup(struct qed_hwfn *p_hwfn)
  915. {
  916. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  917. int type;
  918. /* Reset acquired cids */
  919. for (type = 0; type < MAX_CONN_TYPES; type++) {
  920. u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
  921. if (cid_cnt == 0)
  922. continue;
  923. memset(p_mngr->acquired[type].cid_map, 0,
  924. DIV_ROUND_UP(cid_cnt,
  925. sizeof(unsigned long) * BITS_PER_BYTE) *
  926. sizeof(unsigned long));
  927. }
  928. }
  929. /* CDU Common */
  930. #define CDUC_CXT_SIZE_SHIFT \
  931. CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE_SHIFT
  932. #define CDUC_CXT_SIZE_MASK \
  933. (CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE >> CDUC_CXT_SIZE_SHIFT)
  934. #define CDUC_BLOCK_WASTE_SHIFT \
  935. CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE_SHIFT
  936. #define CDUC_BLOCK_WASTE_MASK \
  937. (CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE >> CDUC_BLOCK_WASTE_SHIFT)
  938. #define CDUC_NCIB_SHIFT \
  939. CDU_REG_CID_ADDR_PARAMS_NCIB_SHIFT
  940. #define CDUC_NCIB_MASK \
  941. (CDU_REG_CID_ADDR_PARAMS_NCIB >> CDUC_NCIB_SHIFT)
  942. #define CDUT_TYPE0_CXT_SIZE_SHIFT \
  943. CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE_SHIFT
  944. #define CDUT_TYPE0_CXT_SIZE_MASK \
  945. (CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE >> \
  946. CDUT_TYPE0_CXT_SIZE_SHIFT)
  947. #define CDUT_TYPE0_BLOCK_WASTE_SHIFT \
  948. CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE_SHIFT
  949. #define CDUT_TYPE0_BLOCK_WASTE_MASK \
  950. (CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE >> \
  951. CDUT_TYPE0_BLOCK_WASTE_SHIFT)
  952. #define CDUT_TYPE0_NCIB_SHIFT \
  953. CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK_SHIFT
  954. #define CDUT_TYPE0_NCIB_MASK \
  955. (CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK >> \
  956. CDUT_TYPE0_NCIB_SHIFT)
  957. #define CDUT_TYPE1_CXT_SIZE_SHIFT \
  958. CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE_SHIFT
  959. #define CDUT_TYPE1_CXT_SIZE_MASK \
  960. (CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE >> \
  961. CDUT_TYPE1_CXT_SIZE_SHIFT)
  962. #define CDUT_TYPE1_BLOCK_WASTE_SHIFT \
  963. CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE_SHIFT
  964. #define CDUT_TYPE1_BLOCK_WASTE_MASK \
  965. (CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE >> \
  966. CDUT_TYPE1_BLOCK_WASTE_SHIFT)
  967. #define CDUT_TYPE1_NCIB_SHIFT \
  968. CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK_SHIFT
  969. #define CDUT_TYPE1_NCIB_MASK \
  970. (CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK >> \
  971. CDUT_TYPE1_NCIB_SHIFT)
  972. static void qed_cdu_init_common(struct qed_hwfn *p_hwfn)
  973. {
  974. u32 page_sz, elems_per_page, block_waste, cxt_size, cdu_params = 0;
  975. /* CDUC - connection configuration */
  976. page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
  977. cxt_size = CONN_CXT_SIZE(p_hwfn);
  978. elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
  979. block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
  980. SET_FIELD(cdu_params, CDUC_CXT_SIZE, cxt_size);
  981. SET_FIELD(cdu_params, CDUC_BLOCK_WASTE, block_waste);
  982. SET_FIELD(cdu_params, CDUC_NCIB, elems_per_page);
  983. STORE_RT_REG(p_hwfn, CDU_REG_CID_ADDR_PARAMS_RT_OFFSET, cdu_params);
  984. /* CDUT - type-0 tasks configuration */
  985. page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT].p_size.val;
  986. cxt_size = p_hwfn->p_cxt_mngr->task_type_size[0];
  987. elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
  988. block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
  989. /* cxt size and block-waste are multipes of 8 */
  990. cdu_params = 0;
  991. SET_FIELD(cdu_params, CDUT_TYPE0_CXT_SIZE, (cxt_size >> 3));
  992. SET_FIELD(cdu_params, CDUT_TYPE0_BLOCK_WASTE, (block_waste >> 3));
  993. SET_FIELD(cdu_params, CDUT_TYPE0_NCIB, elems_per_page);
  994. STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT0_PARAMS_RT_OFFSET, cdu_params);
  995. /* CDUT - type-1 tasks configuration */
  996. cxt_size = p_hwfn->p_cxt_mngr->task_type_size[1];
  997. elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
  998. block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
  999. /* cxt size and block-waste are multipes of 8 */
  1000. cdu_params = 0;
  1001. SET_FIELD(cdu_params, CDUT_TYPE1_CXT_SIZE, (cxt_size >> 3));
  1002. SET_FIELD(cdu_params, CDUT_TYPE1_BLOCK_WASTE, (block_waste >> 3));
  1003. SET_FIELD(cdu_params, CDUT_TYPE1_NCIB, elems_per_page);
  1004. STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT1_PARAMS_RT_OFFSET, cdu_params);
  1005. }
  1006. /* CDU PF */
  1007. #define CDU_SEG_REG_TYPE_SHIFT CDU_SEG_TYPE_OFFSET_REG_TYPE_SHIFT
  1008. #define CDU_SEG_REG_TYPE_MASK 0x1
  1009. #define CDU_SEG_REG_OFFSET_SHIFT 0
  1010. #define CDU_SEG_REG_OFFSET_MASK CDU_SEG_TYPE_OFFSET_REG_OFFSET_MASK
  1011. static void qed_cdu_init_pf(struct qed_hwfn *p_hwfn)
  1012. {
  1013. struct qed_ilt_client_cfg *p_cli;
  1014. struct qed_tid_seg *p_seg;
  1015. u32 cdu_seg_params, offset;
  1016. int i;
  1017. static const u32 rt_type_offset_arr[] = {
  1018. CDU_REG_PF_SEG0_TYPE_OFFSET_RT_OFFSET,
  1019. CDU_REG_PF_SEG1_TYPE_OFFSET_RT_OFFSET,
  1020. CDU_REG_PF_SEG2_TYPE_OFFSET_RT_OFFSET,
  1021. CDU_REG_PF_SEG3_TYPE_OFFSET_RT_OFFSET
  1022. };
  1023. static const u32 rt_type_offset_fl_arr[] = {
  1024. CDU_REG_PF_FL_SEG0_TYPE_OFFSET_RT_OFFSET,
  1025. CDU_REG_PF_FL_SEG1_TYPE_OFFSET_RT_OFFSET,
  1026. CDU_REG_PF_FL_SEG2_TYPE_OFFSET_RT_OFFSET,
  1027. CDU_REG_PF_FL_SEG3_TYPE_OFFSET_RT_OFFSET
  1028. };
  1029. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
  1030. /* There are initializations only for CDUT during pf Phase */
  1031. for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
  1032. /* Segment 0 */
  1033. p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
  1034. if (!p_seg)
  1035. continue;
  1036. /* Note: start_line is already adjusted for the CDU
  1037. * segment register granularity, so we just need to
  1038. * divide. Adjustment is implicit as we assume ILT
  1039. * Page size is larger than 32K!
  1040. */
  1041. offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
  1042. (p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line -
  1043. p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
  1044. cdu_seg_params = 0;
  1045. SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
  1046. SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
  1047. STORE_RT_REG(p_hwfn, rt_type_offset_arr[i], cdu_seg_params);
  1048. offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
  1049. (p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)].start_line -
  1050. p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
  1051. cdu_seg_params = 0;
  1052. SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
  1053. SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
  1054. STORE_RT_REG(p_hwfn, rt_type_offset_fl_arr[i], cdu_seg_params);
  1055. }
  1056. }
  1057. void qed_qm_init_pf(struct qed_hwfn *p_hwfn)
  1058. {
  1059. struct qed_qm_pf_rt_init_params params;
  1060. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  1061. struct qed_qm_iids iids;
  1062. memset(&iids, 0, sizeof(iids));
  1063. qed_cxt_qm_iids(p_hwfn, &iids);
  1064. memset(&params, 0, sizeof(params));
  1065. params.port_id = p_hwfn->port_id;
  1066. params.pf_id = p_hwfn->rel_pf_id;
  1067. params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
  1068. params.is_first_pf = p_hwfn->first_on_engine;
  1069. params.num_pf_cids = iids.cids;
  1070. params.num_vf_cids = iids.vf_cids;
  1071. params.start_pq = qm_info->start_pq;
  1072. params.num_pf_pqs = qm_info->num_pqs - qm_info->num_vf_pqs;
  1073. params.num_vf_pqs = qm_info->num_vf_pqs;
  1074. params.start_vport = qm_info->start_vport;
  1075. params.num_vports = qm_info->num_vports;
  1076. params.pf_wfq = qm_info->pf_wfq;
  1077. params.pf_rl = qm_info->pf_rl;
  1078. params.pq_params = qm_info->qm_pq_params;
  1079. params.vport_params = qm_info->qm_vport_params;
  1080. qed_qm_pf_rt_init(p_hwfn, p_hwfn->p_main_ptt, &params);
  1081. }
  1082. /* CM PF */
  1083. static int qed_cm_init_pf(struct qed_hwfn *p_hwfn)
  1084. {
  1085. union qed_qm_pq_params pq_params;
  1086. u16 pq;
  1087. /* XCM pure-LB queue */
  1088. memset(&pq_params, 0, sizeof(pq_params));
  1089. pq_params.core.tc = LB_TC;
  1090. pq = qed_get_qm_pq(p_hwfn, PROTOCOLID_CORE, &pq_params);
  1091. STORE_RT_REG(p_hwfn, XCM_REG_CON_PHY_Q3_RT_OFFSET, pq);
  1092. return 0;
  1093. }
  1094. /* DQ PF */
  1095. static void qed_dq_init_pf(struct qed_hwfn *p_hwfn)
  1096. {
  1097. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1098. u32 dq_pf_max_cid = 0, dq_vf_max_cid = 0;
  1099. dq_pf_max_cid += (p_mngr->conn_cfg[0].cid_count >> DQ_RANGE_SHIFT);
  1100. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_0_RT_OFFSET, dq_pf_max_cid);
  1101. dq_vf_max_cid += (p_mngr->conn_cfg[0].cids_per_vf >> DQ_RANGE_SHIFT);
  1102. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_0_RT_OFFSET, dq_vf_max_cid);
  1103. dq_pf_max_cid += (p_mngr->conn_cfg[1].cid_count >> DQ_RANGE_SHIFT);
  1104. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_1_RT_OFFSET, dq_pf_max_cid);
  1105. dq_vf_max_cid += (p_mngr->conn_cfg[1].cids_per_vf >> DQ_RANGE_SHIFT);
  1106. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_1_RT_OFFSET, dq_vf_max_cid);
  1107. dq_pf_max_cid += (p_mngr->conn_cfg[2].cid_count >> DQ_RANGE_SHIFT);
  1108. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_2_RT_OFFSET, dq_pf_max_cid);
  1109. dq_vf_max_cid += (p_mngr->conn_cfg[2].cids_per_vf >> DQ_RANGE_SHIFT);
  1110. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_2_RT_OFFSET, dq_vf_max_cid);
  1111. dq_pf_max_cid += (p_mngr->conn_cfg[3].cid_count >> DQ_RANGE_SHIFT);
  1112. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_3_RT_OFFSET, dq_pf_max_cid);
  1113. dq_vf_max_cid += (p_mngr->conn_cfg[3].cids_per_vf >> DQ_RANGE_SHIFT);
  1114. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_3_RT_OFFSET, dq_vf_max_cid);
  1115. dq_pf_max_cid += (p_mngr->conn_cfg[4].cid_count >> DQ_RANGE_SHIFT);
  1116. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_4_RT_OFFSET, dq_pf_max_cid);
  1117. dq_vf_max_cid += (p_mngr->conn_cfg[4].cids_per_vf >> DQ_RANGE_SHIFT);
  1118. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_4_RT_OFFSET, dq_vf_max_cid);
  1119. dq_pf_max_cid += (p_mngr->conn_cfg[5].cid_count >> DQ_RANGE_SHIFT);
  1120. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_5_RT_OFFSET, dq_pf_max_cid);
  1121. dq_vf_max_cid += (p_mngr->conn_cfg[5].cids_per_vf >> DQ_RANGE_SHIFT);
  1122. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_5_RT_OFFSET, dq_vf_max_cid);
  1123. /* Connection types 6 & 7 are not in use, yet they must be configured
  1124. * as the highest possible connection. Not configuring them means the
  1125. * defaults will be used, and with a large number of cids a bug may
  1126. * occur, if the defaults will be smaller than dq_pf_max_cid /
  1127. * dq_vf_max_cid.
  1128. */
  1129. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_6_RT_OFFSET, dq_pf_max_cid);
  1130. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_6_RT_OFFSET, dq_vf_max_cid);
  1131. STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_7_RT_OFFSET, dq_pf_max_cid);
  1132. STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_7_RT_OFFSET, dq_vf_max_cid);
  1133. }
  1134. static void qed_ilt_bounds_init(struct qed_hwfn *p_hwfn)
  1135. {
  1136. struct qed_ilt_client_cfg *ilt_clients;
  1137. int i;
  1138. ilt_clients = p_hwfn->p_cxt_mngr->clients;
  1139. for_each_ilt_valid_client(i, ilt_clients) {
  1140. STORE_RT_REG(p_hwfn,
  1141. ilt_clients[i].first.reg,
  1142. ilt_clients[i].first.val);
  1143. STORE_RT_REG(p_hwfn,
  1144. ilt_clients[i].last.reg, ilt_clients[i].last.val);
  1145. STORE_RT_REG(p_hwfn,
  1146. ilt_clients[i].p_size.reg,
  1147. ilt_clients[i].p_size.val);
  1148. }
  1149. }
  1150. static void qed_ilt_vf_bounds_init(struct qed_hwfn *p_hwfn)
  1151. {
  1152. struct qed_ilt_client_cfg *p_cli;
  1153. u32 blk_factor;
  1154. /* For simplicty we set the 'block' to be an ILT page */
  1155. if (p_hwfn->cdev->p_iov_info) {
  1156. struct qed_hw_sriov_info *p_iov = p_hwfn->cdev->p_iov_info;
  1157. STORE_RT_REG(p_hwfn,
  1158. PSWRQ2_REG_VF_BASE_RT_OFFSET,
  1159. p_iov->first_vf_in_pf);
  1160. STORE_RT_REG(p_hwfn,
  1161. PSWRQ2_REG_VF_LAST_ILT_RT_OFFSET,
  1162. p_iov->first_vf_in_pf + p_iov->total_vfs);
  1163. }
  1164. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
  1165. blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
  1166. if (p_cli->active) {
  1167. STORE_RT_REG(p_hwfn,
  1168. PSWRQ2_REG_CDUC_BLOCKS_FACTOR_RT_OFFSET,
  1169. blk_factor);
  1170. STORE_RT_REG(p_hwfn,
  1171. PSWRQ2_REG_CDUC_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
  1172. p_cli->pf_total_lines);
  1173. STORE_RT_REG(p_hwfn,
  1174. PSWRQ2_REG_CDUC_VF_BLOCKS_RT_OFFSET,
  1175. p_cli->vf_total_lines);
  1176. }
  1177. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
  1178. blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
  1179. if (p_cli->active) {
  1180. STORE_RT_REG(p_hwfn,
  1181. PSWRQ2_REG_CDUT_BLOCKS_FACTOR_RT_OFFSET,
  1182. blk_factor);
  1183. STORE_RT_REG(p_hwfn,
  1184. PSWRQ2_REG_CDUT_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
  1185. p_cli->pf_total_lines);
  1186. STORE_RT_REG(p_hwfn,
  1187. PSWRQ2_REG_CDUT_VF_BLOCKS_RT_OFFSET,
  1188. p_cli->vf_total_lines);
  1189. }
  1190. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TM];
  1191. blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
  1192. if (p_cli->active) {
  1193. STORE_RT_REG(p_hwfn,
  1194. PSWRQ2_REG_TM_BLOCKS_FACTOR_RT_OFFSET, blk_factor);
  1195. STORE_RT_REG(p_hwfn,
  1196. PSWRQ2_REG_TM_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
  1197. p_cli->pf_total_lines);
  1198. STORE_RT_REG(p_hwfn,
  1199. PSWRQ2_REG_TM_VF_BLOCKS_RT_OFFSET,
  1200. p_cli->vf_total_lines);
  1201. }
  1202. }
  1203. /* ILT (PSWRQ2) PF */
  1204. static void qed_ilt_init_pf(struct qed_hwfn *p_hwfn)
  1205. {
  1206. struct qed_ilt_client_cfg *clients;
  1207. struct qed_cxt_mngr *p_mngr;
  1208. struct qed_dma_mem *p_shdw;
  1209. u32 line, rt_offst, i;
  1210. qed_ilt_bounds_init(p_hwfn);
  1211. qed_ilt_vf_bounds_init(p_hwfn);
  1212. p_mngr = p_hwfn->p_cxt_mngr;
  1213. p_shdw = p_mngr->ilt_shadow;
  1214. clients = p_hwfn->p_cxt_mngr->clients;
  1215. for_each_ilt_valid_client(i, clients) {
  1216. /** Client's 1st val and RT array are absolute, ILT shadows'
  1217. * lines are relative.
  1218. */
  1219. line = clients[i].first.val - p_mngr->pf_start_line;
  1220. rt_offst = PSWRQ2_REG_ILT_MEMORY_RT_OFFSET +
  1221. clients[i].first.val * ILT_ENTRY_IN_REGS;
  1222. for (; line <= clients[i].last.val - p_mngr->pf_start_line;
  1223. line++, rt_offst += ILT_ENTRY_IN_REGS) {
  1224. u64 ilt_hw_entry = 0;
  1225. /** p_virt could be NULL incase of dynamic
  1226. * allocation
  1227. */
  1228. if (p_shdw[line].p_virt) {
  1229. SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
  1230. SET_FIELD(ilt_hw_entry, ILT_ENTRY_PHY_ADDR,
  1231. (p_shdw[line].p_phys >> 12));
  1232. DP_VERBOSE(p_hwfn, QED_MSG_ILT,
  1233. "Setting RT[0x%08x] from ILT[0x%08x] [Client is %d] to Physical addr: 0x%llx\n",
  1234. rt_offst, line, i,
  1235. (u64)(p_shdw[line].p_phys >> 12));
  1236. }
  1237. STORE_RT_REG_AGG(p_hwfn, rt_offst, ilt_hw_entry);
  1238. }
  1239. }
  1240. }
  1241. /* SRC (Searcher) PF */
  1242. static void qed_src_init_pf(struct qed_hwfn *p_hwfn)
  1243. {
  1244. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1245. u32 rounded_conn_num, conn_num, conn_max;
  1246. struct qed_src_iids src_iids;
  1247. memset(&src_iids, 0, sizeof(src_iids));
  1248. qed_cxt_src_iids(p_mngr, &src_iids);
  1249. conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
  1250. if (!conn_num)
  1251. return;
  1252. conn_max = max_t(u32, conn_num, SRC_MIN_NUM_ELEMS);
  1253. rounded_conn_num = roundup_pow_of_two(conn_max);
  1254. STORE_RT_REG(p_hwfn, SRC_REG_COUNTFREE_RT_OFFSET, conn_num);
  1255. STORE_RT_REG(p_hwfn, SRC_REG_NUMBER_HASH_BITS_RT_OFFSET,
  1256. ilog2(rounded_conn_num));
  1257. STORE_RT_REG_AGG(p_hwfn, SRC_REG_FIRSTFREE_RT_OFFSET,
  1258. p_hwfn->p_cxt_mngr->first_free);
  1259. STORE_RT_REG_AGG(p_hwfn, SRC_REG_LASTFREE_RT_OFFSET,
  1260. p_hwfn->p_cxt_mngr->last_free);
  1261. }
  1262. /* Timers PF */
  1263. #define TM_CFG_NUM_IDS_SHIFT 0
  1264. #define TM_CFG_NUM_IDS_MASK 0xFFFFULL
  1265. #define TM_CFG_PRE_SCAN_OFFSET_SHIFT 16
  1266. #define TM_CFG_PRE_SCAN_OFFSET_MASK 0x1FFULL
  1267. #define TM_CFG_PARENT_PF_SHIFT 25
  1268. #define TM_CFG_PARENT_PF_MASK 0x7ULL
  1269. #define TM_CFG_CID_PRE_SCAN_ROWS_SHIFT 30
  1270. #define TM_CFG_CID_PRE_SCAN_ROWS_MASK 0x1FFULL
  1271. #define TM_CFG_TID_OFFSET_SHIFT 30
  1272. #define TM_CFG_TID_OFFSET_MASK 0x7FFFFULL
  1273. #define TM_CFG_TID_PRE_SCAN_ROWS_SHIFT 49
  1274. #define TM_CFG_TID_PRE_SCAN_ROWS_MASK 0x1FFULL
  1275. static void qed_tm_init_pf(struct qed_hwfn *p_hwfn)
  1276. {
  1277. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1278. u32 active_seg_mask = 0, tm_offset, rt_reg;
  1279. struct qed_tm_iids tm_iids;
  1280. u64 cfg_word;
  1281. u8 i;
  1282. memset(&tm_iids, 0, sizeof(tm_iids));
  1283. qed_cxt_tm_iids(p_mngr, &tm_iids);
  1284. /* @@@TBD No pre-scan for now */
  1285. /* Note: We assume consecutive VFs for a PF */
  1286. for (i = 0; i < p_mngr->vf_count; i++) {
  1287. cfg_word = 0;
  1288. SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_cids);
  1289. SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
  1290. SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
  1291. SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);
  1292. rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
  1293. (sizeof(cfg_word) / sizeof(u32)) *
  1294. (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
  1295. STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
  1296. }
  1297. cfg_word = 0;
  1298. SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_cids);
  1299. SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
  1300. SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0); /* n/a for PF */
  1301. SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0); /* scan all */
  1302. rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
  1303. (sizeof(cfg_word) / sizeof(u32)) *
  1304. (NUM_OF_VFS(p_hwfn->cdev) + p_hwfn->rel_pf_id);
  1305. STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
  1306. /* enale scan */
  1307. STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_CONN_RT_OFFSET,
  1308. tm_iids.pf_cids ? 0x1 : 0x0);
  1309. /* @@@TBD how to enable the scan for the VFs */
  1310. tm_offset = tm_iids.per_vf_cids;
  1311. /* Note: We assume consecutive VFs for a PF */
  1312. for (i = 0; i < p_mngr->vf_count; i++) {
  1313. cfg_word = 0;
  1314. SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_tids);
  1315. SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
  1316. SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
  1317. SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
  1318. SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
  1319. rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
  1320. (sizeof(cfg_word) / sizeof(u32)) *
  1321. (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
  1322. STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
  1323. }
  1324. tm_offset = tm_iids.pf_cids;
  1325. for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
  1326. cfg_word = 0;
  1327. SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_tids[i]);
  1328. SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
  1329. SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);
  1330. SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
  1331. SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
  1332. rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
  1333. (sizeof(cfg_word) / sizeof(u32)) *
  1334. (NUM_OF_VFS(p_hwfn->cdev) +
  1335. p_hwfn->rel_pf_id * NUM_TASK_PF_SEGMENTS + i);
  1336. STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
  1337. active_seg_mask |= (tm_iids.pf_tids[i] ? BIT(i) : 0);
  1338. tm_offset += tm_iids.pf_tids[i];
  1339. }
  1340. if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE)
  1341. active_seg_mask = 0;
  1342. STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_TASK_RT_OFFSET, active_seg_mask);
  1343. /* @@@TBD how to enable the scan for the VFs */
  1344. }
  1345. void qed_cxt_hw_init_common(struct qed_hwfn *p_hwfn)
  1346. {
  1347. qed_cdu_init_common(p_hwfn);
  1348. }
  1349. void qed_cxt_hw_init_pf(struct qed_hwfn *p_hwfn)
  1350. {
  1351. qed_qm_init_pf(p_hwfn);
  1352. qed_cm_init_pf(p_hwfn);
  1353. qed_dq_init_pf(p_hwfn);
  1354. qed_cdu_init_pf(p_hwfn);
  1355. qed_ilt_init_pf(p_hwfn);
  1356. qed_src_init_pf(p_hwfn);
  1357. qed_tm_init_pf(p_hwfn);
  1358. }
  1359. int qed_cxt_acquire_cid(struct qed_hwfn *p_hwfn,
  1360. enum protocol_type type, u32 *p_cid)
  1361. {
  1362. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1363. u32 rel_cid;
  1364. if (type >= MAX_CONN_TYPES || !p_mngr->acquired[type].cid_map) {
  1365. DP_NOTICE(p_hwfn, "Invalid protocol type %d", type);
  1366. return -EINVAL;
  1367. }
  1368. rel_cid = find_first_zero_bit(p_mngr->acquired[type].cid_map,
  1369. p_mngr->acquired[type].max_count);
  1370. if (rel_cid >= p_mngr->acquired[type].max_count) {
  1371. DP_NOTICE(p_hwfn, "no CID available for protocol %d\n", type);
  1372. return -EINVAL;
  1373. }
  1374. __set_bit(rel_cid, p_mngr->acquired[type].cid_map);
  1375. *p_cid = rel_cid + p_mngr->acquired[type].start_cid;
  1376. return 0;
  1377. }
  1378. static bool qed_cxt_test_cid_acquired(struct qed_hwfn *p_hwfn,
  1379. u32 cid, enum protocol_type *p_type)
  1380. {
  1381. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1382. struct qed_cid_acquired_map *p_map;
  1383. enum protocol_type p;
  1384. u32 rel_cid;
  1385. /* Iterate over protocols and find matching cid range */
  1386. for (p = 0; p < MAX_CONN_TYPES; p++) {
  1387. p_map = &p_mngr->acquired[p];
  1388. if (!p_map->cid_map)
  1389. continue;
  1390. if (cid >= p_map->start_cid &&
  1391. cid < p_map->start_cid + p_map->max_count)
  1392. break;
  1393. }
  1394. *p_type = p;
  1395. if (p == MAX_CONN_TYPES) {
  1396. DP_NOTICE(p_hwfn, "Invalid CID %d", cid);
  1397. return false;
  1398. }
  1399. rel_cid = cid - p_map->start_cid;
  1400. if (!test_bit(rel_cid, p_map->cid_map)) {
  1401. DP_NOTICE(p_hwfn, "CID %d not acquired", cid);
  1402. return false;
  1403. }
  1404. return true;
  1405. }
  1406. void qed_cxt_release_cid(struct qed_hwfn *p_hwfn, u32 cid)
  1407. {
  1408. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1409. enum protocol_type type;
  1410. bool b_acquired;
  1411. u32 rel_cid;
  1412. /* Test acquired and find matching per-protocol map */
  1413. b_acquired = qed_cxt_test_cid_acquired(p_hwfn, cid, &type);
  1414. if (!b_acquired)
  1415. return;
  1416. rel_cid = cid - p_mngr->acquired[type].start_cid;
  1417. __clear_bit(rel_cid, p_mngr->acquired[type].cid_map);
  1418. }
  1419. int qed_cxt_get_cid_info(struct qed_hwfn *p_hwfn, struct qed_cxt_info *p_info)
  1420. {
  1421. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1422. u32 conn_cxt_size, hw_p_size, cxts_per_p, line;
  1423. enum protocol_type type;
  1424. bool b_acquired;
  1425. /* Test acquired and find matching per-protocol map */
  1426. b_acquired = qed_cxt_test_cid_acquired(p_hwfn, p_info->iid, &type);
  1427. if (!b_acquired)
  1428. return -EINVAL;
  1429. /* set the protocl type */
  1430. p_info->type = type;
  1431. /* compute context virtual pointer */
  1432. hw_p_size = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
  1433. conn_cxt_size = CONN_CXT_SIZE(p_hwfn);
  1434. cxts_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / conn_cxt_size;
  1435. line = p_info->iid / cxts_per_p;
  1436. /* Make sure context is allocated (dynamic allocation) */
  1437. if (!p_mngr->ilt_shadow[line].p_virt)
  1438. return -EINVAL;
  1439. p_info->p_cxt = p_mngr->ilt_shadow[line].p_virt +
  1440. p_info->iid % cxts_per_p * conn_cxt_size;
  1441. DP_VERBOSE(p_hwfn, (QED_MSG_ILT | QED_MSG_CXT),
  1442. "Accessing ILT shadow[%d]: CXT pointer is at %p (for iid %d)\n",
  1443. p_info->iid / cxts_per_p, p_info->p_cxt, p_info->iid);
  1444. return 0;
  1445. }
  1446. static void qed_rdma_set_pf_params(struct qed_hwfn *p_hwfn,
  1447. struct qed_rdma_pf_params *p_params)
  1448. {
  1449. u32 num_cons, num_tasks, num_qps, num_mrs, num_srqs;
  1450. enum protocol_type proto;
  1451. num_mrs = min_t(u32, RDMA_MAX_TIDS, p_params->num_mrs);
  1452. num_tasks = num_mrs; /* each mr uses a single task id */
  1453. num_srqs = min_t(u32, 32 * 1024, p_params->num_srqs);
  1454. switch (p_hwfn->hw_info.personality) {
  1455. case QED_PCI_ETH_ROCE:
  1456. num_qps = min_t(u32, ROCE_MAX_QPS, p_params->num_qps);
  1457. num_cons = num_qps * 2; /* each QP requires two connections */
  1458. proto = PROTOCOLID_ROCE;
  1459. break;
  1460. default:
  1461. return;
  1462. }
  1463. if (num_cons && num_tasks) {
  1464. qed_cxt_set_proto_cid_count(p_hwfn, proto, num_cons, 0);
  1465. /* Deliberatly passing ROCE for tasks id. This is because
  1466. * iWARP / RoCE share the task id.
  1467. */
  1468. qed_cxt_set_proto_tid_count(p_hwfn, PROTOCOLID_ROCE,
  1469. QED_CXT_ROCE_TID_SEG, 1,
  1470. num_tasks, false);
  1471. qed_cxt_set_srq_count(p_hwfn, num_srqs);
  1472. } else {
  1473. DP_INFO(p_hwfn->cdev,
  1474. "RDMA personality used without setting params!\n");
  1475. }
  1476. }
  1477. int qed_cxt_set_pf_params(struct qed_hwfn *p_hwfn)
  1478. {
  1479. /* Set the number of required CORE connections */
  1480. u32 core_cids = 1; /* SPQ */
  1481. if (p_hwfn->using_ll2)
  1482. core_cids += 4;
  1483. qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_CORE, core_cids, 0);
  1484. switch (p_hwfn->hw_info.personality) {
  1485. case QED_PCI_ETH_ROCE:
  1486. {
  1487. qed_rdma_set_pf_params(p_hwfn,
  1488. &p_hwfn->
  1489. pf_params.rdma_pf_params);
  1490. /* no need for break since RoCE coexist with Ethernet */
  1491. }
  1492. case QED_PCI_ETH:
  1493. {
  1494. struct qed_eth_pf_params *p_params =
  1495. &p_hwfn->pf_params.eth_pf_params;
  1496. qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
  1497. p_params->num_cons, 1);
  1498. break;
  1499. }
  1500. case QED_PCI_ISCSI:
  1501. {
  1502. struct qed_iscsi_pf_params *p_params;
  1503. p_params = &p_hwfn->pf_params.iscsi_pf_params;
  1504. if (p_params->num_cons && p_params->num_tasks) {
  1505. qed_cxt_set_proto_cid_count(p_hwfn,
  1506. PROTOCOLID_ISCSI,
  1507. p_params->num_cons,
  1508. 0);
  1509. qed_cxt_set_proto_tid_count(p_hwfn,
  1510. PROTOCOLID_ISCSI,
  1511. QED_CXT_ISCSI_TID_SEG,
  1512. 0,
  1513. p_params->num_tasks,
  1514. true);
  1515. } else {
  1516. DP_INFO(p_hwfn->cdev,
  1517. "Iscsi personality used without setting params!\n");
  1518. }
  1519. break;
  1520. }
  1521. default:
  1522. return -EINVAL;
  1523. }
  1524. return 0;
  1525. }
  1526. int qed_cxt_get_tid_mem_info(struct qed_hwfn *p_hwfn,
  1527. struct qed_tid_mem *p_info)
  1528. {
  1529. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1530. u32 proto, seg, total_lines, i, shadow_line;
  1531. struct qed_ilt_client_cfg *p_cli;
  1532. struct qed_ilt_cli_blk *p_fl_seg;
  1533. struct qed_tid_seg *p_seg_info;
  1534. /* Verify the personality */
  1535. switch (p_hwfn->hw_info.personality) {
  1536. case QED_PCI_ISCSI:
  1537. proto = PROTOCOLID_ISCSI;
  1538. seg = QED_CXT_ISCSI_TID_SEG;
  1539. break;
  1540. default:
  1541. return -EINVAL;
  1542. }
  1543. p_cli = &p_mngr->clients[ILT_CLI_CDUT];
  1544. if (!p_cli->active)
  1545. return -EINVAL;
  1546. p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
  1547. if (!p_seg_info->has_fl_mem)
  1548. return -EINVAL;
  1549. p_fl_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
  1550. total_lines = DIV_ROUND_UP(p_fl_seg->total_size,
  1551. p_fl_seg->real_size_in_page);
  1552. for (i = 0; i < total_lines; i++) {
  1553. shadow_line = i + p_fl_seg->start_line -
  1554. p_hwfn->p_cxt_mngr->pf_start_line;
  1555. p_info->blocks[i] = p_mngr->ilt_shadow[shadow_line].p_virt;
  1556. }
  1557. p_info->waste = ILT_PAGE_IN_BYTES(p_cli->p_size.val) -
  1558. p_fl_seg->real_size_in_page;
  1559. p_info->tid_size = p_mngr->task_type_size[p_seg_info->type];
  1560. p_info->num_tids_per_block = p_fl_seg->real_size_in_page /
  1561. p_info->tid_size;
  1562. return 0;
  1563. }
  1564. /* This function is very RoCE oriented, if another protocol in the future
  1565. * will want this feature we'll need to modify the function to be more generic
  1566. */
  1567. int
  1568. qed_cxt_dynamic_ilt_alloc(struct qed_hwfn *p_hwfn,
  1569. enum qed_cxt_elem_type elem_type, u32 iid)
  1570. {
  1571. u32 reg_offset, shadow_line, elem_size, hw_p_size, elems_per_p, line;
  1572. struct qed_ilt_client_cfg *p_cli;
  1573. struct qed_ilt_cli_blk *p_blk;
  1574. struct qed_ptt *p_ptt;
  1575. dma_addr_t p_phys;
  1576. u64 ilt_hw_entry;
  1577. void *p_virt;
  1578. int rc = 0;
  1579. switch (elem_type) {
  1580. case QED_ELEM_CXT:
  1581. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
  1582. elem_size = CONN_CXT_SIZE(p_hwfn);
  1583. p_blk = &p_cli->pf_blks[CDUC_BLK];
  1584. break;
  1585. case QED_ELEM_SRQ:
  1586. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
  1587. elem_size = SRQ_CXT_SIZE;
  1588. p_blk = &p_cli->pf_blks[SRQ_BLK];
  1589. break;
  1590. case QED_ELEM_TASK:
  1591. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
  1592. elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
  1593. p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
  1594. break;
  1595. default:
  1596. DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
  1597. return -EINVAL;
  1598. }
  1599. /* Calculate line in ilt */
  1600. hw_p_size = p_cli->p_size.val;
  1601. elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
  1602. line = p_blk->start_line + (iid / elems_per_p);
  1603. shadow_line = line - p_hwfn->p_cxt_mngr->pf_start_line;
  1604. /* If line is already allocated, do nothing, otherwise allocate it and
  1605. * write it to the PSWRQ2 registers.
  1606. * This section can be run in parallel from different contexts and thus
  1607. * a mutex protection is needed.
  1608. */
  1609. mutex_lock(&p_hwfn->p_cxt_mngr->mutex);
  1610. if (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt)
  1611. goto out0;
  1612. p_ptt = qed_ptt_acquire(p_hwfn);
  1613. if (!p_ptt) {
  1614. DP_NOTICE(p_hwfn,
  1615. "QED_TIME_OUT on ptt acquire - dynamic allocation");
  1616. rc = -EBUSY;
  1617. goto out0;
  1618. }
  1619. p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  1620. p_blk->real_size_in_page,
  1621. &p_phys, GFP_KERNEL);
  1622. if (!p_virt) {
  1623. rc = -ENOMEM;
  1624. goto out1;
  1625. }
  1626. memset(p_virt, 0, p_blk->real_size_in_page);
  1627. /* configuration of refTagMask to 0xF is required for RoCE DIF MR only,
  1628. * to compensate for a HW bug, but it is configured even if DIF is not
  1629. * enabled. This is harmless and allows us to avoid a dedicated API. We
  1630. * configure the field for all of the contexts on the newly allocated
  1631. * page.
  1632. */
  1633. if (elem_type == QED_ELEM_TASK) {
  1634. u32 elem_i;
  1635. u8 *elem_start = (u8 *)p_virt;
  1636. union type1_task_context *elem;
  1637. for (elem_i = 0; elem_i < elems_per_p; elem_i++) {
  1638. elem = (union type1_task_context *)elem_start;
  1639. SET_FIELD(elem->roce_ctx.tdif_context.flags1,
  1640. TDIF_TASK_CONTEXT_REFTAGMASK, 0xf);
  1641. elem_start += TYPE1_TASK_CXT_SIZE(p_hwfn);
  1642. }
  1643. }
  1644. p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt = p_virt;
  1645. p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys = p_phys;
  1646. p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].size =
  1647. p_blk->real_size_in_page;
  1648. /* compute absolute offset */
  1649. reg_offset = PSWRQ2_REG_ILT_MEMORY +
  1650. (line * ILT_REG_SIZE_IN_BYTES * ILT_ENTRY_IN_REGS);
  1651. ilt_hw_entry = 0;
  1652. SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
  1653. SET_FIELD(ilt_hw_entry,
  1654. ILT_ENTRY_PHY_ADDR,
  1655. (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys >> 12));
  1656. /* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a wide-bus */
  1657. qed_dmae_host2grc(p_hwfn, p_ptt, (u64) (uintptr_t)&ilt_hw_entry,
  1658. reg_offset, sizeof(ilt_hw_entry) / sizeof(u32), 0);
  1659. if (elem_type == QED_ELEM_CXT) {
  1660. u32 last_cid_allocated = (1 + (iid / elems_per_p)) *
  1661. elems_per_p;
  1662. /* Update the relevant register in the parser */
  1663. qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF,
  1664. last_cid_allocated - 1);
  1665. if (!p_hwfn->b_rdma_enabled_in_prs) {
  1666. /* Enable RoCE search */
  1667. qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
  1668. p_hwfn->b_rdma_enabled_in_prs = true;
  1669. }
  1670. }
  1671. out1:
  1672. qed_ptt_release(p_hwfn, p_ptt);
  1673. out0:
  1674. mutex_unlock(&p_hwfn->p_cxt_mngr->mutex);
  1675. return rc;
  1676. }
  1677. /* This function is very RoCE oriented, if another protocol in the future
  1678. * will want this feature we'll need to modify the function to be more generic
  1679. */
  1680. static int
  1681. qed_cxt_free_ilt_range(struct qed_hwfn *p_hwfn,
  1682. enum qed_cxt_elem_type elem_type,
  1683. u32 start_iid, u32 count)
  1684. {
  1685. u32 start_line, end_line, shadow_start_line, shadow_end_line;
  1686. u32 reg_offset, elem_size, hw_p_size, elems_per_p;
  1687. struct qed_ilt_client_cfg *p_cli;
  1688. struct qed_ilt_cli_blk *p_blk;
  1689. u32 end_iid = start_iid + count;
  1690. struct qed_ptt *p_ptt;
  1691. u64 ilt_hw_entry = 0;
  1692. u32 i;
  1693. switch (elem_type) {
  1694. case QED_ELEM_CXT:
  1695. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
  1696. elem_size = CONN_CXT_SIZE(p_hwfn);
  1697. p_blk = &p_cli->pf_blks[CDUC_BLK];
  1698. break;
  1699. case QED_ELEM_SRQ:
  1700. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
  1701. elem_size = SRQ_CXT_SIZE;
  1702. p_blk = &p_cli->pf_blks[SRQ_BLK];
  1703. break;
  1704. case QED_ELEM_TASK:
  1705. p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
  1706. elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
  1707. p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
  1708. break;
  1709. default:
  1710. DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
  1711. return -EINVAL;
  1712. }
  1713. /* Calculate line in ilt */
  1714. hw_p_size = p_cli->p_size.val;
  1715. elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
  1716. start_line = p_blk->start_line + (start_iid / elems_per_p);
  1717. end_line = p_blk->start_line + (end_iid / elems_per_p);
  1718. if (((end_iid + 1) / elems_per_p) != (end_iid / elems_per_p))
  1719. end_line--;
  1720. shadow_start_line = start_line - p_hwfn->p_cxt_mngr->pf_start_line;
  1721. shadow_end_line = end_line - p_hwfn->p_cxt_mngr->pf_start_line;
  1722. p_ptt = qed_ptt_acquire(p_hwfn);
  1723. if (!p_ptt) {
  1724. DP_NOTICE(p_hwfn,
  1725. "QED_TIME_OUT on ptt acquire - dynamic allocation");
  1726. return -EBUSY;
  1727. }
  1728. for (i = shadow_start_line; i < shadow_end_line; i++) {
  1729. if (!p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt)
  1730. continue;
  1731. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  1732. p_hwfn->p_cxt_mngr->ilt_shadow[i].size,
  1733. p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt,
  1734. p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys);
  1735. p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt = NULL;
  1736. p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys = 0;
  1737. p_hwfn->p_cxt_mngr->ilt_shadow[i].size = 0;
  1738. /* compute absolute offset */
  1739. reg_offset = PSWRQ2_REG_ILT_MEMORY +
  1740. ((start_line++) * ILT_REG_SIZE_IN_BYTES *
  1741. ILT_ENTRY_IN_REGS);
  1742. /* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a
  1743. * wide-bus.
  1744. */
  1745. qed_dmae_host2grc(p_hwfn, p_ptt,
  1746. (u64) (uintptr_t) &ilt_hw_entry,
  1747. reg_offset,
  1748. sizeof(ilt_hw_entry) / sizeof(u32),
  1749. 0);
  1750. }
  1751. qed_ptt_release(p_hwfn, p_ptt);
  1752. return 0;
  1753. }
  1754. int qed_cxt_free_proto_ilt(struct qed_hwfn *p_hwfn, enum protocol_type proto)
  1755. {
  1756. int rc;
  1757. u32 cid;
  1758. /* Free Connection CXT */
  1759. rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_CXT,
  1760. qed_cxt_get_proto_cid_start(p_hwfn,
  1761. proto),
  1762. qed_cxt_get_proto_cid_count(p_hwfn,
  1763. proto, &cid));
  1764. if (rc)
  1765. return rc;
  1766. /* Free Task CXT */
  1767. rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_TASK, 0,
  1768. qed_cxt_get_proto_tid_count(p_hwfn, proto));
  1769. if (rc)
  1770. return rc;
  1771. /* Free TSDM CXT */
  1772. rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_SRQ, 0,
  1773. qed_cxt_get_srq_count(p_hwfn));
  1774. return rc;
  1775. }
  1776. int qed_cxt_get_task_ctx(struct qed_hwfn *p_hwfn,
  1777. u32 tid, u8 ctx_type, void **pp_task_ctx)
  1778. {
  1779. struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
  1780. struct qed_ilt_client_cfg *p_cli;
  1781. struct qed_ilt_cli_blk *p_seg;
  1782. struct qed_tid_seg *p_seg_info;
  1783. u32 proto, seg;
  1784. u32 total_lines;
  1785. u32 tid_size, ilt_idx;
  1786. u32 num_tids_per_block;
  1787. /* Verify the personality */
  1788. switch (p_hwfn->hw_info.personality) {
  1789. case QED_PCI_ISCSI:
  1790. proto = PROTOCOLID_ISCSI;
  1791. seg = QED_CXT_ISCSI_TID_SEG;
  1792. break;
  1793. default:
  1794. return -EINVAL;
  1795. }
  1796. p_cli = &p_mngr->clients[ILT_CLI_CDUT];
  1797. if (!p_cli->active)
  1798. return -EINVAL;
  1799. p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
  1800. if (ctx_type == QED_CTX_WORKING_MEM) {
  1801. p_seg = &p_cli->pf_blks[CDUT_SEG_BLK(seg)];
  1802. } else if (ctx_type == QED_CTX_FL_MEM) {
  1803. if (!p_seg_info->has_fl_mem)
  1804. return -EINVAL;
  1805. p_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
  1806. } else {
  1807. return -EINVAL;
  1808. }
  1809. total_lines = DIV_ROUND_UP(p_seg->total_size, p_seg->real_size_in_page);
  1810. tid_size = p_mngr->task_type_size[p_seg_info->type];
  1811. num_tids_per_block = p_seg->real_size_in_page / tid_size;
  1812. if (total_lines < tid / num_tids_per_block)
  1813. return -EINVAL;
  1814. ilt_idx = tid / num_tids_per_block + p_seg->start_line -
  1815. p_mngr->pf_start_line;
  1816. *pp_task_ctx = (u8 *)p_mngr->ilt_shadow[ilt_idx].p_virt +
  1817. (tid % num_tids_per_block) * tid_size;
  1818. return 0;
  1819. }