hns_roce_device.h 29 KB

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