hns_roce_device.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * Copyright (c) 2016 Hisilicon Limited.
  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. #ifndef _HNS_ROCE_DEVICE_H
  33. #define _HNS_ROCE_DEVICE_H
  34. #include <rdma/ib_verbs.h>
  35. #define DRV_NAME "hns_roce"
  36. #define HNS_ROCE_HW_VER1 ('h' << 24 | 'i' << 16 | '0' << 8 | '6')
  37. #define MAC_ADDR_OCTET_NUM 6
  38. #define HNS_ROCE_MAX_MSG_LEN 0x80000000
  39. #define HNS_ROCE_ALOGN_UP(a, b) ((((a) + (b) - 1) / (b)) * (b))
  40. #define HNS_ROCE_IB_MIN_SQ_STRIDE 6
  41. #define HNS_ROCE_BA_SIZE (32 * 4096)
  42. /* Hardware specification only for v1 engine */
  43. #define HNS_ROCE_MIN_CQE_NUM 0x40
  44. #define HNS_ROCE_MIN_WQE_NUM 0x20
  45. /* Hardware specification only for v1 engine */
  46. #define HNS_ROCE_MAX_INNER_MTPT_NUM 0x7
  47. #define HNS_ROCE_MAX_MTPT_PBL_NUM 0x100000
  48. #define HNS_ROCE_EACH_FREE_CQ_WAIT_MSECS 20
  49. #define HNS_ROCE_MAX_FREE_CQ_WAIT_CNT \
  50. (5000 / HNS_ROCE_EACH_FREE_CQ_WAIT_MSECS)
  51. #define HNS_ROCE_CQE_WCMD_EMPTY_BIT 0x2
  52. #define HNS_ROCE_MIN_CQE_CNT 16
  53. #define HNS_ROCE_MAX_IRQ_NUM 128
  54. #define EQ_ENABLE 1
  55. #define EQ_DISABLE 0
  56. #define HNS_ROCE_CEQ 0
  57. #define HNS_ROCE_AEQ 1
  58. #define HNS_ROCE_CEQ_ENTRY_SIZE 0x4
  59. #define HNS_ROCE_AEQ_ENTRY_SIZE 0x10
  60. /* 4G/4K = 1M */
  61. #define HNS_ROCE_SL_SHIFT 28
  62. #define HNS_ROCE_TCLASS_SHIFT 20
  63. #define HNS_ROCE_FLOW_LABEL_MASK 0xfffff
  64. #define HNS_ROCE_MAX_PORTS 6
  65. #define HNS_ROCE_MAX_GID_NUM 16
  66. #define HNS_ROCE_GID_SIZE 16
  67. #define HNS_ROCE_HOP_NUM_0 0xff
  68. #define BITMAP_NO_RR 0
  69. #define BITMAP_RR 1
  70. #define MR_TYPE_MR 0x00
  71. #define MR_TYPE_DMA 0x03
  72. #define PKEY_ID 0xffff
  73. #define GUID_LEN 8
  74. #define NODE_DESC_SIZE 64
  75. #define DB_REG_OFFSET 0x1000
  76. #define SERV_TYPE_RC 0
  77. #define SERV_TYPE_RD 1
  78. #define SERV_TYPE_UC 2
  79. #define SERV_TYPE_UD 3
  80. /* Configure to HW for PAGE_SIZE larger than 4KB */
  81. #define PG_SHIFT_OFFSET (PAGE_SHIFT - 12)
  82. #define PAGES_SHIFT_8 8
  83. #define PAGES_SHIFT_16 16
  84. #define PAGES_SHIFT_24 24
  85. #define PAGES_SHIFT_32 32
  86. enum {
  87. HNS_ROCE_SUPPORT_RQ_RECORD_DB = 1 << 0,
  88. HNS_ROCE_SUPPORT_SQ_RECORD_DB = 1 << 1,
  89. };
  90. enum {
  91. HNS_ROCE_SUPPORT_CQ_RECORD_DB = 1 << 0,
  92. };
  93. enum hns_roce_qp_state {
  94. HNS_ROCE_QP_STATE_RST,
  95. HNS_ROCE_QP_STATE_INIT,
  96. HNS_ROCE_QP_STATE_RTR,
  97. HNS_ROCE_QP_STATE_RTS,
  98. HNS_ROCE_QP_STATE_SQD,
  99. HNS_ROCE_QP_STATE_ERR,
  100. HNS_ROCE_QP_NUM_STATE,
  101. };
  102. enum hns_roce_event {
  103. HNS_ROCE_EVENT_TYPE_PATH_MIG = 0x01,
  104. HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED = 0x02,
  105. HNS_ROCE_EVENT_TYPE_COMM_EST = 0x03,
  106. HNS_ROCE_EVENT_TYPE_SQ_DRAINED = 0x04,
  107. HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR = 0x05,
  108. HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR = 0x06,
  109. HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR = 0x07,
  110. HNS_ROCE_EVENT_TYPE_SRQ_LIMIT_REACH = 0x08,
  111. HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH = 0x09,
  112. HNS_ROCE_EVENT_TYPE_SRQ_CATAS_ERROR = 0x0a,
  113. HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR = 0x0b,
  114. HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW = 0x0c,
  115. HNS_ROCE_EVENT_TYPE_CQ_ID_INVALID = 0x0d,
  116. HNS_ROCE_EVENT_TYPE_PORT_CHANGE = 0x0f,
  117. /* 0x10 and 0x11 is unused in currently application case */
  118. HNS_ROCE_EVENT_TYPE_DB_OVERFLOW = 0x12,
  119. HNS_ROCE_EVENT_TYPE_MB = 0x13,
  120. HNS_ROCE_EVENT_TYPE_CEQ_OVERFLOW = 0x14,
  121. HNS_ROCE_EVENT_TYPE_FLR = 0x15,
  122. };
  123. /* Local Work Queue Catastrophic Error,SUBTYPE 0x5 */
  124. enum {
  125. HNS_ROCE_LWQCE_QPC_ERROR = 1,
  126. HNS_ROCE_LWQCE_MTU_ERROR = 2,
  127. HNS_ROCE_LWQCE_WQE_BA_ADDR_ERROR = 3,
  128. HNS_ROCE_LWQCE_WQE_ADDR_ERROR = 4,
  129. HNS_ROCE_LWQCE_SQ_WQE_SHIFT_ERROR = 5,
  130. HNS_ROCE_LWQCE_SL_ERROR = 6,
  131. HNS_ROCE_LWQCE_PORT_ERROR = 7,
  132. };
  133. /* Local Access Violation Work Queue Error,SUBTYPE 0x7 */
  134. enum {
  135. HNS_ROCE_LAVWQE_R_KEY_VIOLATION = 1,
  136. HNS_ROCE_LAVWQE_LENGTH_ERROR = 2,
  137. HNS_ROCE_LAVWQE_VA_ERROR = 3,
  138. HNS_ROCE_LAVWQE_PD_ERROR = 4,
  139. HNS_ROCE_LAVWQE_RW_ACC_ERROR = 5,
  140. HNS_ROCE_LAVWQE_KEY_STATE_ERROR = 6,
  141. HNS_ROCE_LAVWQE_MR_OPERATION_ERROR = 7,
  142. };
  143. /* DOORBELL overflow subtype */
  144. enum {
  145. HNS_ROCE_DB_SUBTYPE_SDB_OVF = 1,
  146. HNS_ROCE_DB_SUBTYPE_SDB_ALM_OVF = 2,
  147. HNS_ROCE_DB_SUBTYPE_ODB_OVF = 3,
  148. HNS_ROCE_DB_SUBTYPE_ODB_ALM_OVF = 4,
  149. HNS_ROCE_DB_SUBTYPE_SDB_ALM_EMP = 5,
  150. HNS_ROCE_DB_SUBTYPE_ODB_ALM_EMP = 6,
  151. };
  152. enum {
  153. /* RQ&SRQ related operations */
  154. HNS_ROCE_OPCODE_SEND_DATA_RECEIVE = 0x06,
  155. HNS_ROCE_OPCODE_RDMA_WITH_IMM_RECEIVE = 0x07,
  156. };
  157. enum {
  158. HNS_ROCE_CAP_FLAG_REREG_MR = BIT(0),
  159. HNS_ROCE_CAP_FLAG_ROCE_V1_V2 = BIT(1),
  160. HNS_ROCE_CAP_FLAG_RQ_INLINE = BIT(2),
  161. HNS_ROCE_CAP_FLAG_RECORD_DB = BIT(3),
  162. HNS_ROCE_CAP_FLAG_SQ_RECORD_DB = BIT(4),
  163. };
  164. enum hns_roce_mtt_type {
  165. MTT_TYPE_WQE,
  166. MTT_TYPE_CQE,
  167. };
  168. enum {
  169. HNS_ROCE_DB_PER_PAGE = PAGE_SIZE / 4
  170. };
  171. #define HNS_ROCE_CMD_SUCCESS 1
  172. #define HNS_ROCE_PORT_DOWN 0
  173. #define HNS_ROCE_PORT_UP 1
  174. #define HNS_ROCE_MTT_ENTRY_PER_SEG 8
  175. #define PAGE_ADDR_SHIFT 12
  176. struct hns_roce_uar {
  177. u64 pfn;
  178. unsigned long index;
  179. unsigned long logic_idx;
  180. };
  181. struct hns_roce_vma_data {
  182. struct list_head list;
  183. struct vm_area_struct *vma;
  184. struct mutex *vma_list_mutex;
  185. };
  186. struct hns_roce_ucontext {
  187. struct ib_ucontext ibucontext;
  188. struct hns_roce_uar uar;
  189. struct list_head page_list;
  190. struct mutex page_mutex;
  191. struct list_head vma_list;
  192. struct mutex vma_list_mutex;
  193. };
  194. struct hns_roce_pd {
  195. struct ib_pd ibpd;
  196. unsigned long pdn;
  197. };
  198. struct hns_roce_bitmap {
  199. /* Bitmap Traversal last a bit which is 1 */
  200. unsigned long last;
  201. unsigned long top;
  202. unsigned long max;
  203. unsigned long reserved_top;
  204. unsigned long mask;
  205. spinlock_t lock;
  206. unsigned long *table;
  207. };
  208. /* Order bitmap length -- bit num compute formula: 1 << (max_order - order) */
  209. /* Order = 0: bitmap is biggest, order = max bitmap is least (only a bit) */
  210. /* Every bit repesent to a partner free/used status in bitmap */
  211. /*
  212. * Initial, bits of other bitmap are all 0 except that a bit of max_order is 1
  213. * Bit = 1 represent to idle and available; bit = 0: not available
  214. */
  215. struct hns_roce_buddy {
  216. /* Members point to every order level bitmap */
  217. unsigned long **bits;
  218. /* Represent to avail bits of the order level bitmap */
  219. u32 *num_free;
  220. int max_order;
  221. spinlock_t lock;
  222. };
  223. /* For Hardware Entry Memory */
  224. struct hns_roce_hem_table {
  225. /* HEM type: 0 = qpc, 1 = mtt, 2 = cqc, 3 = srq, 4 = other */
  226. u32 type;
  227. /* HEM array elment num */
  228. unsigned long num_hem;
  229. /* HEM entry record obj total num */
  230. unsigned long num_obj;
  231. /*Single obj size */
  232. unsigned long obj_size;
  233. unsigned long table_chunk_size;
  234. int lowmem;
  235. struct mutex mutex;
  236. struct hns_roce_hem **hem;
  237. u64 **bt_l1;
  238. dma_addr_t *bt_l1_dma_addr;
  239. u64 **bt_l0;
  240. dma_addr_t *bt_l0_dma_addr;
  241. };
  242. struct hns_roce_mtt {
  243. unsigned long first_seg;
  244. int order;
  245. int page_shift;
  246. enum hns_roce_mtt_type mtt_type;
  247. };
  248. /* Only support 4K page size for mr register */
  249. #define MR_SIZE_4K 0
  250. struct hns_roce_mr {
  251. struct ib_mr ibmr;
  252. struct ib_umem *umem;
  253. u64 iova; /* MR's virtual orignal addr */
  254. u64 size; /* Address range of MR */
  255. u32 key; /* Key of MR */
  256. u32 pd; /* PD num of MR */
  257. u32 access;/* Access permission of MR */
  258. int enabled; /* MR's active status */
  259. int type; /* MR's register type */
  260. u64 *pbl_buf;/* MR's PBL space */
  261. dma_addr_t pbl_dma_addr; /* MR's PBL space PA */
  262. u32 pbl_size;/* PA number in the PBL */
  263. u64 pbl_ba;/* page table address */
  264. u32 l0_chunk_last_num;/* L0 last number */
  265. u32 l1_chunk_last_num;/* L1 last number */
  266. u64 **pbl_bt_l2;/* PBL BT L2 */
  267. u64 **pbl_bt_l1;/* PBL BT L1 */
  268. u64 *pbl_bt_l0;/* PBL BT L0 */
  269. dma_addr_t *pbl_l2_dma_addr;/* PBL BT L2 dma addr */
  270. dma_addr_t *pbl_l1_dma_addr;/* PBL BT L1 dma addr */
  271. dma_addr_t pbl_l0_dma_addr;/* PBL BT L0 dma addr */
  272. u32 pbl_ba_pg_sz;/* BT chunk page size */
  273. u32 pbl_buf_pg_sz;/* buf chunk page size */
  274. u32 pbl_hop_num;/* multi-hop number */
  275. };
  276. struct hns_roce_mr_table {
  277. struct hns_roce_bitmap mtpt_bitmap;
  278. struct hns_roce_buddy mtt_buddy;
  279. struct hns_roce_hem_table mtt_table;
  280. struct hns_roce_hem_table mtpt_table;
  281. struct hns_roce_buddy mtt_cqe_buddy;
  282. struct hns_roce_hem_table mtt_cqe_table;
  283. };
  284. struct hns_roce_wq {
  285. u64 *wrid; /* Work request ID */
  286. spinlock_t lock;
  287. int wqe_cnt; /* WQE num */
  288. u32 max_post;
  289. int max_gs;
  290. int offset;
  291. int wqe_shift;/* WQE size */
  292. u32 head;
  293. u32 tail;
  294. void __iomem *db_reg_l;
  295. };
  296. struct hns_roce_sge {
  297. int sge_cnt; /* SGE num */
  298. int offset;
  299. int sge_shift;/* SGE size */
  300. };
  301. struct hns_roce_buf_list {
  302. void *buf;
  303. dma_addr_t map;
  304. };
  305. struct hns_roce_buf {
  306. struct hns_roce_buf_list direct;
  307. struct hns_roce_buf_list *page_list;
  308. int nbufs;
  309. u32 npages;
  310. int page_shift;
  311. };
  312. struct hns_roce_db_pgdir {
  313. struct list_head list;
  314. DECLARE_BITMAP(order0, HNS_ROCE_DB_PER_PAGE);
  315. DECLARE_BITMAP(order1, HNS_ROCE_DB_PER_PAGE / 2);
  316. unsigned long *bits[2];
  317. u32 *page;
  318. dma_addr_t db_dma;
  319. };
  320. struct hns_roce_user_db_page {
  321. struct list_head list;
  322. struct ib_umem *umem;
  323. unsigned long user_virt;
  324. refcount_t refcount;
  325. };
  326. struct hns_roce_db {
  327. u32 *db_record;
  328. union {
  329. struct hns_roce_db_pgdir *pgdir;
  330. struct hns_roce_user_db_page *user_page;
  331. } u;
  332. dma_addr_t dma;
  333. void *virt_addr;
  334. int index;
  335. int order;
  336. };
  337. struct hns_roce_cq_buf {
  338. struct hns_roce_buf hr_buf;
  339. struct hns_roce_mtt hr_mtt;
  340. };
  341. struct hns_roce_cq {
  342. struct ib_cq ib_cq;
  343. struct hns_roce_cq_buf hr_buf;
  344. struct hns_roce_db db;
  345. u8 db_en;
  346. spinlock_t lock;
  347. struct ib_umem *umem;
  348. void (*comp)(struct hns_roce_cq *cq);
  349. void (*event)(struct hns_roce_cq *cq, enum hns_roce_event event_type);
  350. struct hns_roce_uar *uar;
  351. u32 cq_depth;
  352. u32 cons_index;
  353. u32 *set_ci_db;
  354. void __iomem *cq_db_l;
  355. u16 *tptr_addr;
  356. int arm_sn;
  357. unsigned long cqn;
  358. u32 vector;
  359. atomic_t refcount;
  360. struct completion free;
  361. };
  362. struct hns_roce_srq {
  363. struct ib_srq ibsrq;
  364. int srqn;
  365. };
  366. struct hns_roce_uar_table {
  367. struct hns_roce_bitmap bitmap;
  368. };
  369. struct hns_roce_qp_table {
  370. struct hns_roce_bitmap bitmap;
  371. spinlock_t lock;
  372. struct hns_roce_hem_table qp_table;
  373. struct hns_roce_hem_table irrl_table;
  374. struct hns_roce_hem_table trrl_table;
  375. };
  376. struct hns_roce_cq_table {
  377. struct hns_roce_bitmap bitmap;
  378. spinlock_t lock;
  379. struct radix_tree_root tree;
  380. struct hns_roce_hem_table table;
  381. };
  382. struct hns_roce_raq_table {
  383. struct hns_roce_buf_list *e_raq_buf;
  384. };
  385. struct hns_roce_av {
  386. __le32 port_pd;
  387. u8 gid_index;
  388. u8 stat_rate;
  389. u8 hop_limit;
  390. __le32 sl_tclass_flowlabel;
  391. u8 dgid[HNS_ROCE_GID_SIZE];
  392. u8 mac[6];
  393. __le16 vlan;
  394. };
  395. struct hns_roce_ah {
  396. struct ib_ah ibah;
  397. struct hns_roce_av av;
  398. };
  399. struct hns_roce_cmd_context {
  400. struct completion done;
  401. int result;
  402. int next;
  403. u64 out_param;
  404. u16 token;
  405. };
  406. struct hns_roce_cmdq {
  407. struct dma_pool *pool;
  408. struct mutex hcr_mutex;
  409. struct semaphore poll_sem;
  410. /*
  411. * Event mode: cmd register mutex protection,
  412. * ensure to not exceed max_cmds and user use limit region
  413. */
  414. struct semaphore event_sem;
  415. int max_cmds;
  416. spinlock_t context_lock;
  417. int free_head;
  418. struct hns_roce_cmd_context *context;
  419. /*
  420. * Result of get integer part
  421. * which max_comds compute according a power of 2
  422. */
  423. u16 token_mask;
  424. /*
  425. * Process whether use event mode, init default non-zero
  426. * After the event queue of cmd event ready,
  427. * can switch into event mode
  428. * close device, switch into poll mode(non event mode)
  429. */
  430. u8 use_events;
  431. u8 toggle;
  432. };
  433. struct hns_roce_cmd_mailbox {
  434. void *buf;
  435. dma_addr_t dma;
  436. };
  437. struct hns_roce_dev;
  438. struct hns_roce_rinl_sge {
  439. void *addr;
  440. u32 len;
  441. };
  442. struct hns_roce_rinl_wqe {
  443. struct hns_roce_rinl_sge *sg_list;
  444. u32 sge_cnt;
  445. };
  446. struct hns_roce_rinl_buf {
  447. struct hns_roce_rinl_wqe *wqe_list;
  448. u32 wqe_cnt;
  449. };
  450. struct hns_roce_qp {
  451. struct ib_qp ibqp;
  452. struct hns_roce_buf hr_buf;
  453. struct hns_roce_wq rq;
  454. struct hns_roce_db rdb;
  455. struct hns_roce_db sdb;
  456. u8 rdb_en;
  457. u8 sdb_en;
  458. u32 doorbell_qpn;
  459. __le32 sq_signal_bits;
  460. u32 sq_next_wqe;
  461. int sq_max_wqes_per_wr;
  462. int sq_spare_wqes;
  463. struct hns_roce_wq sq;
  464. struct ib_umem *umem;
  465. struct hns_roce_mtt mtt;
  466. u32 buff_size;
  467. struct mutex mutex;
  468. u8 port;
  469. u8 phy_port;
  470. u8 sl;
  471. u8 resp_depth;
  472. u8 state;
  473. u32 access_flags;
  474. u32 atomic_rd_en;
  475. u32 pkey_index;
  476. u32 qkey;
  477. void (*event)(struct hns_roce_qp *qp,
  478. enum hns_roce_event event_type);
  479. unsigned long qpn;
  480. atomic_t refcount;
  481. struct completion free;
  482. struct hns_roce_sge sge;
  483. u32 next_sge;
  484. struct hns_roce_rinl_buf rq_inl_buf;
  485. };
  486. struct hns_roce_sqp {
  487. struct hns_roce_qp hr_qp;
  488. };
  489. struct hns_roce_ib_iboe {
  490. spinlock_t lock;
  491. struct net_device *netdevs[HNS_ROCE_MAX_PORTS];
  492. struct notifier_block nb;
  493. u8 phy_port[HNS_ROCE_MAX_PORTS];
  494. };
  495. enum {
  496. HNS_ROCE_EQ_STAT_INVALID = 0,
  497. HNS_ROCE_EQ_STAT_VALID = 2,
  498. };
  499. struct hns_roce_ceqe {
  500. u32 comp;
  501. };
  502. struct hns_roce_aeqe {
  503. __le32 asyn;
  504. union {
  505. struct {
  506. __le32 qp;
  507. u32 rsv0;
  508. u32 rsv1;
  509. } qp_event;
  510. struct {
  511. __le32 cq;
  512. u32 rsv0;
  513. u32 rsv1;
  514. } cq_event;
  515. struct {
  516. __le32 ceqe;
  517. u32 rsv0;
  518. u32 rsv1;
  519. } ce_event;
  520. struct {
  521. __le64 out_param;
  522. __le16 token;
  523. u8 status;
  524. u8 rsv0;
  525. } __packed cmd;
  526. } event;
  527. };
  528. struct hns_roce_eq {
  529. struct hns_roce_dev *hr_dev;
  530. void __iomem *doorbell;
  531. int type_flag;/* Aeq:1 ceq:0 */
  532. int eqn;
  533. u32 entries;
  534. int log_entries;
  535. int eqe_size;
  536. int irq;
  537. int log_page_size;
  538. int cons_index;
  539. struct hns_roce_buf_list *buf_list;
  540. int over_ignore;
  541. int coalesce;
  542. int arm_st;
  543. u64 eqe_ba;
  544. int eqe_ba_pg_sz;
  545. int eqe_buf_pg_sz;
  546. int hop_num;
  547. u64 *bt_l0; /* Base address table for L0 */
  548. u64 **bt_l1; /* Base address table for L1 */
  549. u64 **buf;
  550. dma_addr_t l0_dma;
  551. dma_addr_t *l1_dma;
  552. dma_addr_t *buf_dma;
  553. u32 l0_last_num; /* L0 last chunk num */
  554. u32 l1_last_num; /* L1 last chunk num */
  555. int eq_max_cnt;
  556. int eq_period;
  557. int shift;
  558. dma_addr_t cur_eqe_ba;
  559. dma_addr_t nxt_eqe_ba;
  560. int event_type;
  561. int sub_type;
  562. };
  563. struct hns_roce_eq_table {
  564. struct hns_roce_eq *eq;
  565. void __iomem **eqc_base; /* only for hw v1 */
  566. };
  567. struct hns_roce_caps {
  568. u8 num_ports;
  569. int gid_table_len[HNS_ROCE_MAX_PORTS];
  570. int pkey_table_len[HNS_ROCE_MAX_PORTS];
  571. int local_ca_ack_delay;
  572. int num_uars;
  573. u32 phy_num_uars;
  574. u32 max_sq_sg; /* 2 */
  575. u32 max_sq_inline; /* 32 */
  576. u32 max_rq_sg; /* 2 */
  577. int num_qps; /* 256k */
  578. u32 max_wqes; /* 16k */
  579. u32 max_sq_desc_sz; /* 64 */
  580. u32 max_rq_desc_sz; /* 64 */
  581. u32 max_srq_desc_sz;
  582. int max_qp_init_rdma;
  583. int max_qp_dest_rdma;
  584. int num_cqs;
  585. int max_cqes;
  586. int min_cqes;
  587. u32 min_wqes;
  588. int reserved_cqs;
  589. int num_aeq_vectors; /* 1 */
  590. int num_comp_vectors;
  591. int num_other_vectors;
  592. int num_mtpts;
  593. u32 num_mtt_segs;
  594. u32 num_cqe_segs;
  595. int reserved_mrws;
  596. int reserved_uars;
  597. int num_pds;
  598. int reserved_pds;
  599. u32 mtt_entry_sz;
  600. u32 cq_entry_sz;
  601. u32 page_size_cap;
  602. u32 reserved_lkey;
  603. int mtpt_entry_sz;
  604. int qpc_entry_sz;
  605. int irrl_entry_sz;
  606. int trrl_entry_sz;
  607. int cqc_entry_sz;
  608. u32 pbl_ba_pg_sz;
  609. u32 pbl_buf_pg_sz;
  610. u32 pbl_hop_num;
  611. int aeqe_depth;
  612. int ceqe_depth;
  613. enum ib_mtu max_mtu;
  614. u32 qpc_bt_num;
  615. u32 srqc_bt_num;
  616. u32 cqc_bt_num;
  617. u32 mpt_bt_num;
  618. u32 qpc_ba_pg_sz;
  619. u32 qpc_buf_pg_sz;
  620. u32 qpc_hop_num;
  621. u32 srqc_ba_pg_sz;
  622. u32 srqc_buf_pg_sz;
  623. u32 srqc_hop_num;
  624. u32 cqc_ba_pg_sz;
  625. u32 cqc_buf_pg_sz;
  626. u32 cqc_hop_num;
  627. u32 mpt_ba_pg_sz;
  628. u32 mpt_buf_pg_sz;
  629. u32 mpt_hop_num;
  630. u32 mtt_ba_pg_sz;
  631. u32 mtt_buf_pg_sz;
  632. u32 mtt_hop_num;
  633. u32 cqe_ba_pg_sz;
  634. u32 cqe_buf_pg_sz;
  635. u32 cqe_hop_num;
  636. u32 eqe_ba_pg_sz;
  637. u32 eqe_buf_pg_sz;
  638. u32 eqe_hop_num;
  639. u32 sl_num;
  640. u32 tsq_buf_pg_sz;
  641. u32 tpq_buf_pg_sz;
  642. u32 chunk_sz; /* chunk size in non multihop mode*/
  643. u64 flags;
  644. };
  645. struct hns_roce_work {
  646. struct hns_roce_dev *hr_dev;
  647. struct work_struct work;
  648. u32 qpn;
  649. int event_type;
  650. int sub_type;
  651. };
  652. struct hns_roce_hw {
  653. int (*reset)(struct hns_roce_dev *hr_dev, bool enable);
  654. int (*cmq_init)(struct hns_roce_dev *hr_dev);
  655. void (*cmq_exit)(struct hns_roce_dev *hr_dev);
  656. int (*hw_profile)(struct hns_roce_dev *hr_dev);
  657. int (*hw_init)(struct hns_roce_dev *hr_dev);
  658. void (*hw_exit)(struct hns_roce_dev *hr_dev);
  659. int (*post_mbox)(struct hns_roce_dev *hr_dev, u64 in_param,
  660. u64 out_param, u32 in_modifier, u8 op_modifier, u16 op,
  661. u16 token, int event);
  662. int (*chk_mbox)(struct hns_roce_dev *hr_dev, unsigned long timeout);
  663. int (*set_gid)(struct hns_roce_dev *hr_dev, u8 port, int gid_index,
  664. const union ib_gid *gid, const struct ib_gid_attr *attr);
  665. int (*set_mac)(struct hns_roce_dev *hr_dev, u8 phy_port, u8 *addr);
  666. void (*set_mtu)(struct hns_roce_dev *hr_dev, u8 phy_port,
  667. enum ib_mtu mtu);
  668. int (*write_mtpt)(void *mb_buf, struct hns_roce_mr *mr,
  669. unsigned long mtpt_idx);
  670. int (*rereg_write_mtpt)(struct hns_roce_dev *hr_dev,
  671. struct hns_roce_mr *mr, int flags, u32 pdn,
  672. int mr_access_flags, u64 iova, u64 size,
  673. void *mb_buf);
  674. void (*write_cqc)(struct hns_roce_dev *hr_dev,
  675. struct hns_roce_cq *hr_cq, void *mb_buf, u64 *mtts,
  676. dma_addr_t dma_handle, int nent, u32 vector);
  677. int (*set_hem)(struct hns_roce_dev *hr_dev,
  678. struct hns_roce_hem_table *table, int obj, int step_idx);
  679. int (*clear_hem)(struct hns_roce_dev *hr_dev,
  680. struct hns_roce_hem_table *table, int obj,
  681. int step_idx);
  682. int (*query_qp)(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
  683. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr);
  684. int (*modify_qp)(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
  685. int attr_mask, enum ib_qp_state cur_state,
  686. enum ib_qp_state new_state);
  687. int (*destroy_qp)(struct ib_qp *ibqp);
  688. int (*post_send)(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  689. const struct ib_send_wr **bad_wr);
  690. int (*post_recv)(struct ib_qp *qp, const struct ib_recv_wr *recv_wr,
  691. const struct ib_recv_wr **bad_recv_wr);
  692. int (*req_notify_cq)(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
  693. int (*poll_cq)(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
  694. int (*dereg_mr)(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr);
  695. int (*destroy_cq)(struct ib_cq *ibcq);
  696. int (*modify_cq)(struct ib_cq *cq, u16 cq_count, u16 cq_period);
  697. int (*init_eq)(struct hns_roce_dev *hr_dev);
  698. void (*cleanup_eq)(struct hns_roce_dev *hr_dev);
  699. };
  700. struct hns_roce_dev {
  701. struct ib_device ib_dev;
  702. struct platform_device *pdev;
  703. struct pci_dev *pci_dev;
  704. struct device *dev;
  705. struct hns_roce_uar priv_uar;
  706. const char *irq_names[HNS_ROCE_MAX_IRQ_NUM];
  707. spinlock_t sm_lock;
  708. spinlock_t bt_cmd_lock;
  709. bool active;
  710. bool is_reset;
  711. struct hns_roce_ib_iboe iboe;
  712. struct list_head pgdir_list;
  713. struct mutex pgdir_mutex;
  714. int irq[HNS_ROCE_MAX_IRQ_NUM];
  715. u8 __iomem *reg_base;
  716. struct hns_roce_caps caps;
  717. struct radix_tree_root qp_table_tree;
  718. unsigned char dev_addr[HNS_ROCE_MAX_PORTS][MAC_ADDR_OCTET_NUM];
  719. u64 sys_image_guid;
  720. u32 vendor_id;
  721. u32 vendor_part_id;
  722. u32 hw_rev;
  723. void __iomem *priv_addr;
  724. struct hns_roce_cmdq cmd;
  725. struct hns_roce_bitmap pd_bitmap;
  726. struct hns_roce_uar_table uar_table;
  727. struct hns_roce_mr_table mr_table;
  728. struct hns_roce_cq_table cq_table;
  729. struct hns_roce_qp_table qp_table;
  730. struct hns_roce_eq_table eq_table;
  731. int cmd_mod;
  732. int loop_idc;
  733. u32 sdb_offset;
  734. u32 odb_offset;
  735. dma_addr_t tptr_dma_addr; /*only for hw v1*/
  736. u32 tptr_size; /*only for hw v1*/
  737. const struct hns_roce_hw *hw;
  738. void *priv;
  739. struct workqueue_struct *irq_workq;
  740. };
  741. static inline struct hns_roce_dev *to_hr_dev(struct ib_device *ib_dev)
  742. {
  743. return container_of(ib_dev, struct hns_roce_dev, ib_dev);
  744. }
  745. static inline struct hns_roce_ucontext
  746. *to_hr_ucontext(struct ib_ucontext *ibucontext)
  747. {
  748. return container_of(ibucontext, struct hns_roce_ucontext, ibucontext);
  749. }
  750. static inline struct hns_roce_pd *to_hr_pd(struct ib_pd *ibpd)
  751. {
  752. return container_of(ibpd, struct hns_roce_pd, ibpd);
  753. }
  754. static inline struct hns_roce_ah *to_hr_ah(struct ib_ah *ibah)
  755. {
  756. return container_of(ibah, struct hns_roce_ah, ibah);
  757. }
  758. static inline struct hns_roce_mr *to_hr_mr(struct ib_mr *ibmr)
  759. {
  760. return container_of(ibmr, struct hns_roce_mr, ibmr);
  761. }
  762. static inline struct hns_roce_qp *to_hr_qp(struct ib_qp *ibqp)
  763. {
  764. return container_of(ibqp, struct hns_roce_qp, ibqp);
  765. }
  766. static inline struct hns_roce_cq *to_hr_cq(struct ib_cq *ib_cq)
  767. {
  768. return container_of(ib_cq, struct hns_roce_cq, ib_cq);
  769. }
  770. static inline struct hns_roce_srq *to_hr_srq(struct ib_srq *ibsrq)
  771. {
  772. return container_of(ibsrq, struct hns_roce_srq, ibsrq);
  773. }
  774. static inline struct hns_roce_sqp *hr_to_hr_sqp(struct hns_roce_qp *hr_qp)
  775. {
  776. return container_of(hr_qp, struct hns_roce_sqp, hr_qp);
  777. }
  778. static inline void hns_roce_write64_k(__le32 val[2], void __iomem *dest)
  779. {
  780. __raw_writeq(*(u64 *) val, dest);
  781. }
  782. static inline struct hns_roce_qp
  783. *__hns_roce_qp_lookup(struct hns_roce_dev *hr_dev, u32 qpn)
  784. {
  785. return radix_tree_lookup(&hr_dev->qp_table_tree,
  786. qpn & (hr_dev->caps.num_qps - 1));
  787. }
  788. static inline void *hns_roce_buf_offset(struct hns_roce_buf *buf, int offset)
  789. {
  790. u32 page_size = 1 << buf->page_shift;
  791. if (buf->nbufs == 1)
  792. return (char *)(buf->direct.buf) + offset;
  793. else
  794. return (char *)(buf->page_list[offset >> buf->page_shift].buf) +
  795. (offset & (page_size - 1));
  796. }
  797. int hns_roce_init_uar_table(struct hns_roce_dev *dev);
  798. int hns_roce_uar_alloc(struct hns_roce_dev *dev, struct hns_roce_uar *uar);
  799. void hns_roce_uar_free(struct hns_roce_dev *dev, struct hns_roce_uar *uar);
  800. void hns_roce_cleanup_uar_table(struct hns_roce_dev *dev);
  801. int hns_roce_cmd_init(struct hns_roce_dev *hr_dev);
  802. void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev);
  803. void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
  804. u64 out_param);
  805. int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev);
  806. void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev);
  807. int hns_roce_mtt_init(struct hns_roce_dev *hr_dev, int npages, int page_shift,
  808. struct hns_roce_mtt *mtt);
  809. void hns_roce_mtt_cleanup(struct hns_roce_dev *hr_dev,
  810. struct hns_roce_mtt *mtt);
  811. int hns_roce_buf_write_mtt(struct hns_roce_dev *hr_dev,
  812. struct hns_roce_mtt *mtt, struct hns_roce_buf *buf);
  813. int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev);
  814. int hns_roce_init_mr_table(struct hns_roce_dev *hr_dev);
  815. int hns_roce_init_eq_table(struct hns_roce_dev *hr_dev);
  816. int hns_roce_init_cq_table(struct hns_roce_dev *hr_dev);
  817. int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev);
  818. void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev);
  819. void hns_roce_cleanup_mr_table(struct hns_roce_dev *hr_dev);
  820. void hns_roce_cleanup_eq_table(struct hns_roce_dev *hr_dev);
  821. void hns_roce_cleanup_cq_table(struct hns_roce_dev *hr_dev);
  822. void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev);
  823. int hns_roce_bitmap_alloc(struct hns_roce_bitmap *bitmap, unsigned long *obj);
  824. void hns_roce_bitmap_free(struct hns_roce_bitmap *bitmap, unsigned long obj,
  825. int rr);
  826. int hns_roce_bitmap_init(struct hns_roce_bitmap *bitmap, u32 num, u32 mask,
  827. u32 reserved_bot, u32 resetrved_top);
  828. void hns_roce_bitmap_cleanup(struct hns_roce_bitmap *bitmap);
  829. void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev);
  830. int hns_roce_bitmap_alloc_range(struct hns_roce_bitmap *bitmap, int cnt,
  831. int align, unsigned long *obj);
  832. void hns_roce_bitmap_free_range(struct hns_roce_bitmap *bitmap,
  833. unsigned long obj, int cnt,
  834. int rr);
  835. struct ib_ah *hns_roce_create_ah(struct ib_pd *pd,
  836. struct rdma_ah_attr *ah_attr,
  837. struct ib_udata *udata);
  838. int hns_roce_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr);
  839. int hns_roce_destroy_ah(struct ib_ah *ah);
  840. struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev,
  841. struct ib_ucontext *context,
  842. struct ib_udata *udata);
  843. int hns_roce_dealloc_pd(struct ib_pd *pd);
  844. struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc);
  845. struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  846. u64 virt_addr, int access_flags,
  847. struct ib_udata *udata);
  848. int hns_roce_rereg_user_mr(struct ib_mr *mr, int flags, u64 start, u64 length,
  849. u64 virt_addr, int mr_access_flags, struct ib_pd *pd,
  850. struct ib_udata *udata);
  851. int hns_roce_dereg_mr(struct ib_mr *ibmr);
  852. int hns_roce_hw2sw_mpt(struct hns_roce_dev *hr_dev,
  853. struct hns_roce_cmd_mailbox *mailbox,
  854. unsigned long mpt_index);
  855. unsigned long key_to_hw_index(u32 key);
  856. void hns_roce_buf_free(struct hns_roce_dev *hr_dev, u32 size,
  857. struct hns_roce_buf *buf);
  858. int hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size, u32 max_direct,
  859. struct hns_roce_buf *buf, u32 page_shift);
  860. int hns_roce_ib_umem_write_mtt(struct hns_roce_dev *hr_dev,
  861. struct hns_roce_mtt *mtt, struct ib_umem *umem);
  862. struct ib_qp *hns_roce_create_qp(struct ib_pd *ib_pd,
  863. struct ib_qp_init_attr *init_attr,
  864. struct ib_udata *udata);
  865. int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  866. int attr_mask, struct ib_udata *udata);
  867. void *get_recv_wqe(struct hns_roce_qp *hr_qp, int n);
  868. void *get_send_wqe(struct hns_roce_qp *hr_qp, int n);
  869. void *get_send_extend_sge(struct hns_roce_qp *hr_qp, int n);
  870. bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, int nreq,
  871. struct ib_cq *ib_cq);
  872. enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state);
  873. void hns_roce_lock_cqs(struct hns_roce_cq *send_cq,
  874. struct hns_roce_cq *recv_cq);
  875. void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
  876. struct hns_roce_cq *recv_cq);
  877. void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp);
  878. void hns_roce_qp_free(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp);
  879. void hns_roce_release_range_qp(struct hns_roce_dev *hr_dev, int base_qpn,
  880. int cnt);
  881. __be32 send_ieth(const struct ib_send_wr *wr);
  882. int to_hr_qp_type(int qp_type);
  883. struct ib_cq *hns_roce_ib_create_cq(struct ib_device *ib_dev,
  884. const struct ib_cq_init_attr *attr,
  885. struct ib_ucontext *context,
  886. struct ib_udata *udata);
  887. int hns_roce_ib_destroy_cq(struct ib_cq *ib_cq);
  888. void hns_roce_free_cq(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq);
  889. int hns_roce_db_map_user(struct hns_roce_ucontext *context, unsigned long virt,
  890. struct hns_roce_db *db);
  891. void hns_roce_db_unmap_user(struct hns_roce_ucontext *context,
  892. struct hns_roce_db *db);
  893. int hns_roce_alloc_db(struct hns_roce_dev *hr_dev, struct hns_roce_db *db,
  894. int order);
  895. void hns_roce_free_db(struct hns_roce_dev *hr_dev, struct hns_roce_db *db);
  896. void hns_roce_cq_completion(struct hns_roce_dev *hr_dev, u32 cqn);
  897. void hns_roce_cq_event(struct hns_roce_dev *hr_dev, u32 cqn, int event_type);
  898. void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type);
  899. int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index);
  900. int hns_roce_init(struct hns_roce_dev *hr_dev);
  901. void hns_roce_exit(struct hns_roce_dev *hr_dev);
  902. #endif /* _HNS_ROCE_DEVICE_H */