hns_roce_device.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. u32 max_extend_sg;
  578. int num_qps; /* 256k */
  579. int reserved_qps;
  580. u32 max_wqes; /* 16k */
  581. u32 max_sq_desc_sz; /* 64 */
  582. u32 max_rq_desc_sz; /* 64 */
  583. u32 max_srq_desc_sz;
  584. int max_qp_init_rdma;
  585. int max_qp_dest_rdma;
  586. int num_cqs;
  587. int max_cqes;
  588. int min_cqes;
  589. u32 min_wqes;
  590. int reserved_cqs;
  591. int num_aeq_vectors; /* 1 */
  592. int num_comp_vectors;
  593. int num_other_vectors;
  594. int num_mtpts;
  595. u32 num_mtt_segs;
  596. u32 num_cqe_segs;
  597. int reserved_mrws;
  598. int reserved_uars;
  599. int num_pds;
  600. int reserved_pds;
  601. u32 mtt_entry_sz;
  602. u32 cq_entry_sz;
  603. u32 page_size_cap;
  604. u32 reserved_lkey;
  605. int mtpt_entry_sz;
  606. int qpc_entry_sz;
  607. int irrl_entry_sz;
  608. int trrl_entry_sz;
  609. int cqc_entry_sz;
  610. u32 pbl_ba_pg_sz;
  611. u32 pbl_buf_pg_sz;
  612. u32 pbl_hop_num;
  613. int aeqe_depth;
  614. int ceqe_depth;
  615. enum ib_mtu max_mtu;
  616. u32 qpc_bt_num;
  617. u32 srqc_bt_num;
  618. u32 cqc_bt_num;
  619. u32 mpt_bt_num;
  620. u32 qpc_ba_pg_sz;
  621. u32 qpc_buf_pg_sz;
  622. u32 qpc_hop_num;
  623. u32 srqc_ba_pg_sz;
  624. u32 srqc_buf_pg_sz;
  625. u32 srqc_hop_num;
  626. u32 cqc_ba_pg_sz;
  627. u32 cqc_buf_pg_sz;
  628. u32 cqc_hop_num;
  629. u32 mpt_ba_pg_sz;
  630. u32 mpt_buf_pg_sz;
  631. u32 mpt_hop_num;
  632. u32 mtt_ba_pg_sz;
  633. u32 mtt_buf_pg_sz;
  634. u32 mtt_hop_num;
  635. u32 cqe_ba_pg_sz;
  636. u32 cqe_buf_pg_sz;
  637. u32 cqe_hop_num;
  638. u32 eqe_ba_pg_sz;
  639. u32 eqe_buf_pg_sz;
  640. u32 eqe_hop_num;
  641. u32 sl_num;
  642. u32 tsq_buf_pg_sz;
  643. u32 tpq_buf_pg_sz;
  644. u32 chunk_sz; /* chunk size in non multihop mode*/
  645. u64 flags;
  646. };
  647. struct hns_roce_work {
  648. struct hns_roce_dev *hr_dev;
  649. struct work_struct work;
  650. u32 qpn;
  651. int event_type;
  652. int sub_type;
  653. };
  654. struct hns_roce_hw {
  655. int (*reset)(struct hns_roce_dev *hr_dev, bool enable);
  656. int (*cmq_init)(struct hns_roce_dev *hr_dev);
  657. void (*cmq_exit)(struct hns_roce_dev *hr_dev);
  658. int (*hw_profile)(struct hns_roce_dev *hr_dev);
  659. int (*hw_init)(struct hns_roce_dev *hr_dev);
  660. void (*hw_exit)(struct hns_roce_dev *hr_dev);
  661. int (*post_mbox)(struct hns_roce_dev *hr_dev, u64 in_param,
  662. u64 out_param, u32 in_modifier, u8 op_modifier, u16 op,
  663. u16 token, int event);
  664. int (*chk_mbox)(struct hns_roce_dev *hr_dev, unsigned long timeout);
  665. int (*set_gid)(struct hns_roce_dev *hr_dev, u8 port, int gid_index,
  666. const union ib_gid *gid, const struct ib_gid_attr *attr);
  667. int (*set_mac)(struct hns_roce_dev *hr_dev, u8 phy_port, u8 *addr);
  668. void (*set_mtu)(struct hns_roce_dev *hr_dev, u8 phy_port,
  669. enum ib_mtu mtu);
  670. int (*write_mtpt)(void *mb_buf, struct hns_roce_mr *mr,
  671. unsigned long mtpt_idx);
  672. int (*rereg_write_mtpt)(struct hns_roce_dev *hr_dev,
  673. struct hns_roce_mr *mr, int flags, u32 pdn,
  674. int mr_access_flags, u64 iova, u64 size,
  675. void *mb_buf);
  676. void (*write_cqc)(struct hns_roce_dev *hr_dev,
  677. struct hns_roce_cq *hr_cq, void *mb_buf, u64 *mtts,
  678. dma_addr_t dma_handle, int nent, u32 vector);
  679. int (*set_hem)(struct hns_roce_dev *hr_dev,
  680. struct hns_roce_hem_table *table, int obj, int step_idx);
  681. int (*clear_hem)(struct hns_roce_dev *hr_dev,
  682. struct hns_roce_hem_table *table, int obj,
  683. int step_idx);
  684. int (*query_qp)(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
  685. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr);
  686. int (*modify_qp)(struct ib_qp *ibqp, const struct ib_qp_attr *attr,
  687. int attr_mask, enum ib_qp_state cur_state,
  688. enum ib_qp_state new_state);
  689. int (*destroy_qp)(struct ib_qp *ibqp);
  690. int (*post_send)(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  691. const struct ib_send_wr **bad_wr);
  692. int (*post_recv)(struct ib_qp *qp, const struct ib_recv_wr *recv_wr,
  693. const struct ib_recv_wr **bad_recv_wr);
  694. int (*req_notify_cq)(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
  695. int (*poll_cq)(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
  696. int (*dereg_mr)(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr);
  697. int (*destroy_cq)(struct ib_cq *ibcq);
  698. int (*modify_cq)(struct ib_cq *cq, u16 cq_count, u16 cq_period);
  699. int (*init_eq)(struct hns_roce_dev *hr_dev);
  700. void (*cleanup_eq)(struct hns_roce_dev *hr_dev);
  701. };
  702. struct hns_roce_dev {
  703. struct ib_device ib_dev;
  704. struct platform_device *pdev;
  705. struct pci_dev *pci_dev;
  706. struct device *dev;
  707. struct hns_roce_uar priv_uar;
  708. const char *irq_names[HNS_ROCE_MAX_IRQ_NUM];
  709. spinlock_t sm_lock;
  710. spinlock_t bt_cmd_lock;
  711. bool active;
  712. bool is_reset;
  713. struct hns_roce_ib_iboe iboe;
  714. struct list_head pgdir_list;
  715. struct mutex pgdir_mutex;
  716. int irq[HNS_ROCE_MAX_IRQ_NUM];
  717. u8 __iomem *reg_base;
  718. struct hns_roce_caps caps;
  719. struct radix_tree_root qp_table_tree;
  720. unsigned char dev_addr[HNS_ROCE_MAX_PORTS][MAC_ADDR_OCTET_NUM];
  721. u64 sys_image_guid;
  722. u32 vendor_id;
  723. u32 vendor_part_id;
  724. u32 hw_rev;
  725. void __iomem *priv_addr;
  726. struct hns_roce_cmdq cmd;
  727. struct hns_roce_bitmap pd_bitmap;
  728. struct hns_roce_uar_table uar_table;
  729. struct hns_roce_mr_table mr_table;
  730. struct hns_roce_cq_table cq_table;
  731. struct hns_roce_qp_table qp_table;
  732. struct hns_roce_eq_table eq_table;
  733. int cmd_mod;
  734. int loop_idc;
  735. u32 sdb_offset;
  736. u32 odb_offset;
  737. dma_addr_t tptr_dma_addr; /*only for hw v1*/
  738. u32 tptr_size; /*only for hw v1*/
  739. const struct hns_roce_hw *hw;
  740. void *priv;
  741. struct workqueue_struct *irq_workq;
  742. };
  743. static inline struct hns_roce_dev *to_hr_dev(struct ib_device *ib_dev)
  744. {
  745. return container_of(ib_dev, struct hns_roce_dev, ib_dev);
  746. }
  747. static inline struct hns_roce_ucontext
  748. *to_hr_ucontext(struct ib_ucontext *ibucontext)
  749. {
  750. return container_of(ibucontext, struct hns_roce_ucontext, ibucontext);
  751. }
  752. static inline struct hns_roce_pd *to_hr_pd(struct ib_pd *ibpd)
  753. {
  754. return container_of(ibpd, struct hns_roce_pd, ibpd);
  755. }
  756. static inline struct hns_roce_ah *to_hr_ah(struct ib_ah *ibah)
  757. {
  758. return container_of(ibah, struct hns_roce_ah, ibah);
  759. }
  760. static inline struct hns_roce_mr *to_hr_mr(struct ib_mr *ibmr)
  761. {
  762. return container_of(ibmr, struct hns_roce_mr, ibmr);
  763. }
  764. static inline struct hns_roce_qp *to_hr_qp(struct ib_qp *ibqp)
  765. {
  766. return container_of(ibqp, struct hns_roce_qp, ibqp);
  767. }
  768. static inline struct hns_roce_cq *to_hr_cq(struct ib_cq *ib_cq)
  769. {
  770. return container_of(ib_cq, struct hns_roce_cq, ib_cq);
  771. }
  772. static inline struct hns_roce_srq *to_hr_srq(struct ib_srq *ibsrq)
  773. {
  774. return container_of(ibsrq, struct hns_roce_srq, ibsrq);
  775. }
  776. static inline struct hns_roce_sqp *hr_to_hr_sqp(struct hns_roce_qp *hr_qp)
  777. {
  778. return container_of(hr_qp, struct hns_roce_sqp, hr_qp);
  779. }
  780. static inline void hns_roce_write64_k(__le32 val[2], void __iomem *dest)
  781. {
  782. __raw_writeq(*(u64 *) val, dest);
  783. }
  784. static inline struct hns_roce_qp
  785. *__hns_roce_qp_lookup(struct hns_roce_dev *hr_dev, u32 qpn)
  786. {
  787. return radix_tree_lookup(&hr_dev->qp_table_tree,
  788. qpn & (hr_dev->caps.num_qps - 1));
  789. }
  790. static inline void *hns_roce_buf_offset(struct hns_roce_buf *buf, int offset)
  791. {
  792. u32 page_size = 1 << buf->page_shift;
  793. if (buf->nbufs == 1)
  794. return (char *)(buf->direct.buf) + offset;
  795. else
  796. return (char *)(buf->page_list[offset >> buf->page_shift].buf) +
  797. (offset & (page_size - 1));
  798. }
  799. int hns_roce_init_uar_table(struct hns_roce_dev *dev);
  800. int hns_roce_uar_alloc(struct hns_roce_dev *dev, struct hns_roce_uar *uar);
  801. void hns_roce_uar_free(struct hns_roce_dev *dev, struct hns_roce_uar *uar);
  802. void hns_roce_cleanup_uar_table(struct hns_roce_dev *dev);
  803. int hns_roce_cmd_init(struct hns_roce_dev *hr_dev);
  804. void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev);
  805. void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
  806. u64 out_param);
  807. int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev);
  808. void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev);
  809. int hns_roce_mtt_init(struct hns_roce_dev *hr_dev, int npages, int page_shift,
  810. struct hns_roce_mtt *mtt);
  811. void hns_roce_mtt_cleanup(struct hns_roce_dev *hr_dev,
  812. struct hns_roce_mtt *mtt);
  813. int hns_roce_buf_write_mtt(struct hns_roce_dev *hr_dev,
  814. struct hns_roce_mtt *mtt, struct hns_roce_buf *buf);
  815. int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev);
  816. int hns_roce_init_mr_table(struct hns_roce_dev *hr_dev);
  817. int hns_roce_init_eq_table(struct hns_roce_dev *hr_dev);
  818. int hns_roce_init_cq_table(struct hns_roce_dev *hr_dev);
  819. int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev);
  820. void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev);
  821. void hns_roce_cleanup_mr_table(struct hns_roce_dev *hr_dev);
  822. void hns_roce_cleanup_eq_table(struct hns_roce_dev *hr_dev);
  823. void hns_roce_cleanup_cq_table(struct hns_roce_dev *hr_dev);
  824. void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev);
  825. int hns_roce_bitmap_alloc(struct hns_roce_bitmap *bitmap, unsigned long *obj);
  826. void hns_roce_bitmap_free(struct hns_roce_bitmap *bitmap, unsigned long obj,
  827. int rr);
  828. int hns_roce_bitmap_init(struct hns_roce_bitmap *bitmap, u32 num, u32 mask,
  829. u32 reserved_bot, u32 resetrved_top);
  830. void hns_roce_bitmap_cleanup(struct hns_roce_bitmap *bitmap);
  831. void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev);
  832. int hns_roce_bitmap_alloc_range(struct hns_roce_bitmap *bitmap, int cnt,
  833. int align, unsigned long *obj);
  834. void hns_roce_bitmap_free_range(struct hns_roce_bitmap *bitmap,
  835. unsigned long obj, int cnt,
  836. int rr);
  837. struct ib_ah *hns_roce_create_ah(struct ib_pd *pd,
  838. struct rdma_ah_attr *ah_attr,
  839. struct ib_udata *udata);
  840. int hns_roce_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr);
  841. int hns_roce_destroy_ah(struct ib_ah *ah);
  842. struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev,
  843. struct ib_ucontext *context,
  844. struct ib_udata *udata);
  845. int hns_roce_dealloc_pd(struct ib_pd *pd);
  846. struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc);
  847. struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  848. u64 virt_addr, int access_flags,
  849. struct ib_udata *udata);
  850. int hns_roce_rereg_user_mr(struct ib_mr *mr, int flags, u64 start, u64 length,
  851. u64 virt_addr, int mr_access_flags, struct ib_pd *pd,
  852. struct ib_udata *udata);
  853. int hns_roce_dereg_mr(struct ib_mr *ibmr);
  854. int hns_roce_hw2sw_mpt(struct hns_roce_dev *hr_dev,
  855. struct hns_roce_cmd_mailbox *mailbox,
  856. unsigned long mpt_index);
  857. unsigned long key_to_hw_index(u32 key);
  858. void hns_roce_buf_free(struct hns_roce_dev *hr_dev, u32 size,
  859. struct hns_roce_buf *buf);
  860. int hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size, u32 max_direct,
  861. struct hns_roce_buf *buf, u32 page_shift);
  862. int hns_roce_ib_umem_write_mtt(struct hns_roce_dev *hr_dev,
  863. struct hns_roce_mtt *mtt, struct ib_umem *umem);
  864. struct ib_qp *hns_roce_create_qp(struct ib_pd *ib_pd,
  865. struct ib_qp_init_attr *init_attr,
  866. struct ib_udata *udata);
  867. int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  868. int attr_mask, struct ib_udata *udata);
  869. void *get_recv_wqe(struct hns_roce_qp *hr_qp, int n);
  870. void *get_send_wqe(struct hns_roce_qp *hr_qp, int n);
  871. void *get_send_extend_sge(struct hns_roce_qp *hr_qp, int n);
  872. bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, int nreq,
  873. struct ib_cq *ib_cq);
  874. enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state);
  875. void hns_roce_lock_cqs(struct hns_roce_cq *send_cq,
  876. struct hns_roce_cq *recv_cq);
  877. void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
  878. struct hns_roce_cq *recv_cq);
  879. void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp);
  880. void hns_roce_qp_free(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp);
  881. void hns_roce_release_range_qp(struct hns_roce_dev *hr_dev, int base_qpn,
  882. int cnt);
  883. __be32 send_ieth(const struct ib_send_wr *wr);
  884. int to_hr_qp_type(int qp_type);
  885. struct ib_cq *hns_roce_ib_create_cq(struct ib_device *ib_dev,
  886. const struct ib_cq_init_attr *attr,
  887. struct ib_ucontext *context,
  888. struct ib_udata *udata);
  889. int hns_roce_ib_destroy_cq(struct ib_cq *ib_cq);
  890. void hns_roce_free_cq(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq);
  891. int hns_roce_db_map_user(struct hns_roce_ucontext *context, unsigned long virt,
  892. struct hns_roce_db *db);
  893. void hns_roce_db_unmap_user(struct hns_roce_ucontext *context,
  894. struct hns_roce_db *db);
  895. int hns_roce_alloc_db(struct hns_roce_dev *hr_dev, struct hns_roce_db *db,
  896. int order);
  897. void hns_roce_free_db(struct hns_roce_dev *hr_dev, struct hns_roce_db *db);
  898. void hns_roce_cq_completion(struct hns_roce_dev *hr_dev, u32 cqn);
  899. void hns_roce_cq_event(struct hns_roce_dev *hr_dev, u32 cqn, int event_type);
  900. void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type);
  901. int hns_get_gid_index(struct hns_roce_dev *hr_dev, u8 port, int gid_index);
  902. int hns_roce_init(struct hns_roce_dev *hr_dev);
  903. void hns_roce_exit(struct hns_roce_dev *hr_dev);
  904. #endif /* _HNS_ROCE_DEVICE_H */