c2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
  3. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #ifndef __C2_H
  34. #define __C2_H
  35. #include <linux/netdevice.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/kernel.h>
  38. #include <linux/pci.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/idr.h>
  41. #include <asm/semaphore.h>
  42. #include "c2_provider.h"
  43. #include "c2_mq.h"
  44. #include "c2_status.h"
  45. #define DRV_NAME "c2"
  46. #define DRV_VERSION "1.1"
  47. #define PFX DRV_NAME ": "
  48. #define BAR_0 0
  49. #define BAR_2 2
  50. #define BAR_4 4
  51. #define RX_BUF_SIZE (1536 + 8)
  52. #define ETH_JUMBO_MTU 9000
  53. #define C2_MAGIC "CEPHEUS"
  54. #define C2_VERSION 4
  55. #define C2_IVN (18 & 0x7fffffff)
  56. #define C2_REG0_SIZE (16 * 1024)
  57. #define C2_REG2_SIZE (2 * 1024 * 1024)
  58. #define C2_REG4_SIZE (256 * 1024 * 1024)
  59. #define C2_NUM_TX_DESC 341
  60. #define C2_NUM_RX_DESC 256
  61. #define C2_PCI_REGS_OFFSET (0x10000)
  62. #define C2_RXP_HRXDQ_OFFSET (((C2_REG4_SIZE)/2))
  63. #define C2_RXP_HRXDQ_SIZE (4096)
  64. #define C2_TXP_HTXDQ_OFFSET (((C2_REG4_SIZE)/2) + C2_RXP_HRXDQ_SIZE)
  65. #define C2_TXP_HTXDQ_SIZE (4096)
  66. #define C2_TX_TIMEOUT (6*HZ)
  67. /* CEPHEUS */
  68. static const u8 c2_magic[] = {
  69. 0x43, 0x45, 0x50, 0x48, 0x45, 0x55, 0x53
  70. };
  71. enum adapter_pci_regs {
  72. C2_REGS_MAGIC = 0x0000,
  73. C2_REGS_VERS = 0x0008,
  74. C2_REGS_IVN = 0x000C,
  75. C2_REGS_PCI_WINSIZE = 0x0010,
  76. C2_REGS_Q0_QSIZE = 0x0014,
  77. C2_REGS_Q0_MSGSIZE = 0x0018,
  78. C2_REGS_Q0_POOLSTART = 0x001C,
  79. C2_REGS_Q0_SHARED = 0x0020,
  80. C2_REGS_Q1_QSIZE = 0x0024,
  81. C2_REGS_Q1_MSGSIZE = 0x0028,
  82. C2_REGS_Q1_SHARED = 0x0030,
  83. C2_REGS_Q2_QSIZE = 0x0034,
  84. C2_REGS_Q2_MSGSIZE = 0x0038,
  85. C2_REGS_Q2_SHARED = 0x0040,
  86. C2_REGS_ENADDR = 0x004C,
  87. C2_REGS_RDMA_ENADDR = 0x0054,
  88. C2_REGS_HRX_CUR = 0x006C,
  89. };
  90. struct c2_adapter_pci_regs {
  91. char reg_magic[8];
  92. u32 version;
  93. u32 ivn;
  94. u32 pci_window_size;
  95. u32 q0_q_size;
  96. u32 q0_msg_size;
  97. u32 q0_pool_start;
  98. u32 q0_shared;
  99. u32 q1_q_size;
  100. u32 q1_msg_size;
  101. u32 q1_pool_start;
  102. u32 q1_shared;
  103. u32 q2_q_size;
  104. u32 q2_msg_size;
  105. u32 q2_pool_start;
  106. u32 q2_shared;
  107. u32 log_start;
  108. u32 log_size;
  109. u8 host_enaddr[8];
  110. u8 rdma_enaddr[8];
  111. u32 crash_entry;
  112. u32 crash_ready[2];
  113. u32 fw_txd_cur;
  114. u32 fw_hrxd_cur;
  115. u32 fw_rxd_cur;
  116. };
  117. enum pci_regs {
  118. C2_HISR = 0x0000,
  119. C2_DISR = 0x0004,
  120. C2_HIMR = 0x0008,
  121. C2_DIMR = 0x000C,
  122. C2_NISR0 = 0x0010,
  123. C2_NISR1 = 0x0014,
  124. C2_NIMR0 = 0x0018,
  125. C2_NIMR1 = 0x001C,
  126. C2_IDIS = 0x0020,
  127. };
  128. enum {
  129. C2_PCI_HRX_INT = 1 << 8,
  130. C2_PCI_HTX_INT = 1 << 17,
  131. C2_PCI_HRX_QUI = 1 << 31,
  132. };
  133. /*
  134. * Cepheus registers in BAR0.
  135. */
  136. struct c2_pci_regs {
  137. u32 hostisr;
  138. u32 dmaisr;
  139. u32 hostimr;
  140. u32 dmaimr;
  141. u32 netisr0;
  142. u32 netisr1;
  143. u32 netimr0;
  144. u32 netimr1;
  145. u32 int_disable;
  146. };
  147. /* TXP flags */
  148. enum c2_txp_flags {
  149. TXP_HTXD_DONE = 0,
  150. TXP_HTXD_READY = 1 << 0,
  151. TXP_HTXD_UNINIT = 1 << 1,
  152. };
  153. /* RXP flags */
  154. enum c2_rxp_flags {
  155. RXP_HRXD_UNINIT = 0,
  156. RXP_HRXD_READY = 1 << 0,
  157. RXP_HRXD_DONE = 1 << 1,
  158. };
  159. /* RXP status */
  160. enum c2_rxp_status {
  161. RXP_HRXD_ZERO = 0,
  162. RXP_HRXD_OK = 1 << 0,
  163. RXP_HRXD_BUF_OV = 1 << 1,
  164. };
  165. /* TXP descriptor fields */
  166. enum txp_desc {
  167. C2_TXP_FLAGS = 0x0000,
  168. C2_TXP_LEN = 0x0002,
  169. C2_TXP_ADDR = 0x0004,
  170. };
  171. /* RXP descriptor fields */
  172. enum rxp_desc {
  173. C2_RXP_FLAGS = 0x0000,
  174. C2_RXP_STATUS = 0x0002,
  175. C2_RXP_COUNT = 0x0004,
  176. C2_RXP_LEN = 0x0006,
  177. C2_RXP_ADDR = 0x0008,
  178. };
  179. struct c2_txp_desc {
  180. u16 flags;
  181. u16 len;
  182. u64 addr;
  183. } __attribute__ ((packed));
  184. struct c2_rxp_desc {
  185. u16 flags;
  186. u16 status;
  187. u16 count;
  188. u16 len;
  189. u64 addr;
  190. } __attribute__ ((packed));
  191. struct c2_rxp_hdr {
  192. u16 flags;
  193. u16 status;
  194. u16 len;
  195. u16 rsvd;
  196. } __attribute__ ((packed));
  197. struct c2_tx_desc {
  198. u32 len;
  199. u32 status;
  200. dma_addr_t next_offset;
  201. };
  202. struct c2_rx_desc {
  203. u32 len;
  204. u32 status;
  205. dma_addr_t next_offset;
  206. };
  207. struct c2_alloc {
  208. u32 last;
  209. u32 max;
  210. spinlock_t lock;
  211. unsigned long *table;
  212. };
  213. struct c2_array {
  214. struct {
  215. void **page;
  216. int used;
  217. } *page_list;
  218. };
  219. /*
  220. * The MQ shared pointer pool is organized as a linked list of
  221. * chunks. Each chunk contains a linked list of free shared pointers
  222. * that can be allocated to a given user mode client.
  223. *
  224. */
  225. struct sp_chunk {
  226. struct sp_chunk *next;
  227. dma_addr_t dma_addr;
  228. DECLARE_PCI_UNMAP_ADDR(mapping);
  229. u16 head;
  230. u16 shared_ptr[0];
  231. };
  232. struct c2_pd_table {
  233. u32 last;
  234. u32 max;
  235. spinlock_t lock;
  236. unsigned long *table;
  237. };
  238. struct c2_qp_table {
  239. struct idr idr;
  240. spinlock_t lock;
  241. int last;
  242. };
  243. struct c2_element {
  244. struct c2_element *next;
  245. void *ht_desc; /* host descriptor */
  246. void __iomem *hw_desc; /* hardware descriptor */
  247. struct sk_buff *skb;
  248. dma_addr_t mapaddr;
  249. u32 maplen;
  250. };
  251. struct c2_ring {
  252. struct c2_element *to_clean;
  253. struct c2_element *to_use;
  254. struct c2_element *start;
  255. unsigned long count;
  256. };
  257. struct c2_dev {
  258. struct ib_device ibdev;
  259. void __iomem *regs;
  260. void __iomem *mmio_txp_ring; /* remapped adapter memory for hw rings */
  261. void __iomem *mmio_rxp_ring;
  262. spinlock_t lock;
  263. struct pci_dev *pcidev;
  264. struct net_device *netdev;
  265. struct net_device *pseudo_netdev;
  266. unsigned int cur_tx;
  267. unsigned int cur_rx;
  268. u32 adapter_handle;
  269. int device_cap_flags;
  270. void __iomem *kva; /* KVA device memory */
  271. unsigned long pa; /* PA device memory */
  272. void **qptr_array;
  273. kmem_cache_t *host_msg_cache;
  274. struct list_head cca_link; /* adapter list */
  275. struct list_head eh_wakeup_list; /* event wakeup list */
  276. wait_queue_head_t req_vq_wo;
  277. /* Cached RNIC properties */
  278. struct ib_device_attr props;
  279. struct c2_pd_table pd_table;
  280. struct c2_qp_table qp_table;
  281. int ports; /* num of GigE ports */
  282. int devnum;
  283. spinlock_t vqlock; /* sync vbs req MQ */
  284. /* Verbs Queues */
  285. struct c2_mq req_vq; /* Verbs Request MQ */
  286. struct c2_mq rep_vq; /* Verbs Reply MQ */
  287. struct c2_mq aeq; /* Async Events MQ */
  288. /* Kernel client MQs */
  289. struct sp_chunk *kern_mqsp_pool;
  290. /* Device updates these values when posting messages to a host
  291. * target queue */
  292. u16 req_vq_shared;
  293. u16 rep_vq_shared;
  294. u16 aeq_shared;
  295. u16 irq_claimed;
  296. /*
  297. * Shared host target pages for user-accessible MQs.
  298. */
  299. int hthead; /* index of first free entry */
  300. void *htpages; /* kernel vaddr */
  301. int htlen; /* length of htpages memory */
  302. void *htuva; /* user mapped vaddr */
  303. spinlock_t htlock; /* serialize allocation */
  304. u64 adapter_hint_uva; /* access to the activity FIFO */
  305. // spinlock_t aeq_lock;
  306. // spinlock_t rnic_lock;
  307. u16 *hint_count;
  308. dma_addr_t hint_count_dma;
  309. u16 hints_read;
  310. int init; /* TRUE if it's ready */
  311. char ae_cache_name[16];
  312. char vq_cache_name[16];
  313. };
  314. struct c2_port {
  315. u32 msg_enable;
  316. struct c2_dev *c2dev;
  317. struct net_device *netdev;
  318. spinlock_t tx_lock;
  319. u32 tx_avail;
  320. struct c2_ring tx_ring;
  321. struct c2_ring rx_ring;
  322. void *mem; /* PCI memory for host rings */
  323. dma_addr_t dma;
  324. unsigned long mem_size;
  325. u32 rx_buf_size;
  326. struct net_device_stats netstats;
  327. };
  328. /*
  329. * Activity FIFO registers in BAR0.
  330. */
  331. #define PCI_BAR0_HOST_HINT 0x100
  332. #define PCI_BAR0_ADAPTER_HINT 0x2000
  333. /*
  334. * Ammasso PCI vendor id and Cepheus PCI device id.
  335. */
  336. #define CQ_ARMED 0x01
  337. #define CQ_WAIT_FOR_DMA 0x80
  338. /*
  339. * The format of a hint is as follows:
  340. * Lower 16 bits are the count of hints for the queue.
  341. * Next 15 bits are the qp_index
  342. * Upper most bit depends on who reads it:
  343. * If read by producer, then it means Full (1) or Not-Full (0)
  344. * If read by consumer, then it means Empty (1) or Not-Empty (0)
  345. */
  346. #define C2_HINT_MAKE(q_index, hint_count) (((q_index) << 16) | hint_count)
  347. #define C2_HINT_GET_INDEX(hint) (((hint) & 0x7FFF0000) >> 16)
  348. #define C2_HINT_GET_COUNT(hint) ((hint) & 0x0000FFFF)
  349. /*
  350. * The following defines the offset in SDRAM for the c2_adapter_pci_regs_t
  351. * struct.
  352. */
  353. #define C2_ADAPTER_PCI_REGS_OFFSET 0x10000
  354. #ifndef readq
  355. static inline u64 readq(const void __iomem * addr)
  356. {
  357. u64 ret = readl(addr + 4);
  358. ret <<= 32;
  359. ret |= readl(addr);
  360. return ret;
  361. }
  362. #endif
  363. #ifndef writeq
  364. static inline void __raw_writeq(u64 val, void __iomem * addr)
  365. {
  366. __raw_writel((u32) (val), addr);
  367. __raw_writel((u32) (val >> 32), (addr + 4));
  368. }
  369. #endif
  370. #define C2_SET_CUR_RX(c2dev, cur_rx) \
  371. __raw_writel(cpu_to_be32(cur_rx), c2dev->mmio_txp_ring + 4092)
  372. #define C2_GET_CUR_RX(c2dev) \
  373. be32_to_cpu(readl(c2dev->mmio_txp_ring + 4092))
  374. static inline struct c2_dev *to_c2dev(struct ib_device *ibdev)
  375. {
  376. return container_of(ibdev, struct c2_dev, ibdev);
  377. }
  378. static inline int c2_errno(void *reply)
  379. {
  380. switch (c2_wr_get_result(reply)) {
  381. case C2_OK:
  382. return 0;
  383. case CCERR_NO_BUFS:
  384. case CCERR_INSUFFICIENT_RESOURCES:
  385. case CCERR_ZERO_RDMA_READ_RESOURCES:
  386. return -ENOMEM;
  387. case CCERR_MR_IN_USE:
  388. case CCERR_QP_IN_USE:
  389. return -EBUSY;
  390. case CCERR_ADDR_IN_USE:
  391. return -EADDRINUSE;
  392. case CCERR_ADDR_NOT_AVAIL:
  393. return -EADDRNOTAVAIL;
  394. case CCERR_CONN_RESET:
  395. return -ECONNRESET;
  396. case CCERR_NOT_IMPLEMENTED:
  397. case CCERR_INVALID_WQE:
  398. return -ENOSYS;
  399. case CCERR_QP_NOT_PRIVILEGED:
  400. return -EPERM;
  401. case CCERR_STACK_ERROR:
  402. return -EPROTO;
  403. case CCERR_ACCESS_VIOLATION:
  404. case CCERR_BASE_AND_BOUNDS_VIOLATION:
  405. return -EFAULT;
  406. case CCERR_STAG_STATE_NOT_INVALID:
  407. case CCERR_INVALID_ADDRESS:
  408. case CCERR_INVALID_CQ:
  409. case CCERR_INVALID_EP:
  410. case CCERR_INVALID_MODIFIER:
  411. case CCERR_INVALID_MTU:
  412. case CCERR_INVALID_PD_ID:
  413. case CCERR_INVALID_QP:
  414. case CCERR_INVALID_RNIC:
  415. case CCERR_INVALID_STAG:
  416. return -EINVAL;
  417. default:
  418. return -EAGAIN;
  419. }
  420. }
  421. /* Device */
  422. extern int c2_register_device(struct c2_dev *c2dev);
  423. extern void c2_unregister_device(struct c2_dev *c2dev);
  424. extern int c2_rnic_init(struct c2_dev *c2dev);
  425. extern void c2_rnic_term(struct c2_dev *c2dev);
  426. extern void c2_rnic_interrupt(struct c2_dev *c2dev);
  427. extern int c2_del_addr(struct c2_dev *c2dev, u32 inaddr, u32 inmask);
  428. extern int c2_add_addr(struct c2_dev *c2dev, u32 inaddr, u32 inmask);
  429. /* QPs */
  430. extern int c2_alloc_qp(struct c2_dev *c2dev, struct c2_pd *pd,
  431. struct ib_qp_init_attr *qp_attrs, struct c2_qp *qp);
  432. extern void c2_free_qp(struct c2_dev *c2dev, struct c2_qp *qp);
  433. extern struct ib_qp *c2_get_qp(struct ib_device *device, int qpn);
  434. extern int c2_qp_modify(struct c2_dev *c2dev, struct c2_qp *qp,
  435. struct ib_qp_attr *attr, int attr_mask);
  436. extern int c2_qp_set_read_limits(struct c2_dev *c2dev, struct c2_qp *qp,
  437. int ord, int ird);
  438. extern int c2_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
  439. struct ib_send_wr **bad_wr);
  440. extern int c2_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
  441. struct ib_recv_wr **bad_wr);
  442. extern void __devinit c2_init_qp_table(struct c2_dev *c2dev);
  443. extern void __devexit c2_cleanup_qp_table(struct c2_dev *c2dev);
  444. extern void c2_set_qp_state(struct c2_qp *, int);
  445. extern struct c2_qp *c2_find_qpn(struct c2_dev *c2dev, int qpn);
  446. /* PDs */
  447. extern int c2_pd_alloc(struct c2_dev *c2dev, int privileged, struct c2_pd *pd);
  448. extern void c2_pd_free(struct c2_dev *c2dev, struct c2_pd *pd);
  449. extern int __devinit c2_init_pd_table(struct c2_dev *c2dev);
  450. extern void __devexit c2_cleanup_pd_table(struct c2_dev *c2dev);
  451. /* CQs */
  452. extern int c2_init_cq(struct c2_dev *c2dev, int entries,
  453. struct c2_ucontext *ctx, struct c2_cq *cq);
  454. extern void c2_free_cq(struct c2_dev *c2dev, struct c2_cq *cq);
  455. extern void c2_cq_event(struct c2_dev *c2dev, u32 mq_index);
  456. extern void c2_cq_clean(struct c2_dev *c2dev, struct c2_qp *qp, u32 mq_index);
  457. extern int c2_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry);
  458. extern int c2_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify notify);
  459. /* CM */
  460. extern int c2_llp_connect(struct iw_cm_id *cm_id,
  461. struct iw_cm_conn_param *iw_param);
  462. extern int c2_llp_accept(struct iw_cm_id *cm_id,
  463. struct iw_cm_conn_param *iw_param);
  464. extern int c2_llp_reject(struct iw_cm_id *cm_id, const void *pdata,
  465. u8 pdata_len);
  466. extern int c2_llp_service_create(struct iw_cm_id *cm_id, int backlog);
  467. extern int c2_llp_service_destroy(struct iw_cm_id *cm_id);
  468. /* MM */
  469. extern int c2_nsmr_register_phys_kern(struct c2_dev *c2dev, u64 *addr_list,
  470. int page_size, int pbl_depth, u32 length,
  471. u32 off, u64 *va, enum c2_acf acf,
  472. struct c2_mr *mr);
  473. extern int c2_stag_dealloc(struct c2_dev *c2dev, u32 stag_index);
  474. /* AE */
  475. extern void c2_ae_event(struct c2_dev *c2dev, u32 mq_index);
  476. /* MQSP Allocator */
  477. extern int c2_init_mqsp_pool(struct c2_dev *c2dev, gfp_t gfp_mask,
  478. struct sp_chunk **root);
  479. extern void c2_free_mqsp_pool(struct c2_dev *c2dev, struct sp_chunk *root);
  480. extern u16 *c2_alloc_mqsp(struct c2_dev *c2dev, struct sp_chunk *head,
  481. dma_addr_t *dma_addr, gfp_t gfp_mask);
  482. extern void c2_free_mqsp(u16 * mqsp);
  483. #endif