hns_roce_device.h 27 KB

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