2 * libcxgbi.h: Chelsio common library for T3/T4 iSCSI driver.
4 * Copyright (c) 2010-2015 Chelsio Communications, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
10 * Written by: Karen Xie (kxie@chelsio.com)
11 * Written by: Rakesh Ranjan (rranjan@chelsio.com)
14 #ifndef __LIBCXGBI_H__
15 #define __LIBCXGBI_H__
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/debugfs.h>
21 #include <linux/list.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_vlan.h>
24 #include <linux/scatterlist.h>
25 #include <linux/skbuff.h>
26 #include <linux/vmalloc.h>
27 #include <linux/version.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/libiscsi_tcp.h>
31 #include <libcxgb_ppm.h>
44 #define log_debug(level, fmt, ...) \
46 if (dbg_level & (level)) \
47 pr_info(fmt, ##__VA_ARGS__); \
50 #define pr_info_ipaddr(fmt_trail, \
51 addr1, addr2, args_trail...) \
53 if (!((1 << CXGBI_DBG_SOCK) & dbg_level)) \
55 pr_info("%pISpc - %pISpc, " fmt_trail, \
56 addr1, addr2, args_trail); \
59 /* max. connections per adapter */
60 #define CXGBI_MAX_CONN 16384
62 /* always allocate rooms for AHS */
63 #define SKB_TX_ISCSI_PDU_HEADER_MAX \
64 (sizeof(struct iscsi_hdr) + ISCSI_MAX_AHS_SIZE)
66 #define ISCSI_PDU_NONPAYLOAD_LEN 312 /* bhs(48) + ahs(256) + digest(8)*/
69 * align pdu size to multiple of 512 for better performance
71 #define cxgbi_align_pdu_size(n) do { n = (n) & (~511); } while (0)
73 #define ULP2_MODE_ISCSI 2
75 #define ULP2_MAX_PKT_SIZE 16224
76 #define ULP2_MAX_PDU_PAYLOAD \
77 (ULP2_MAX_PKT_SIZE - ISCSI_PDU_NONPAYLOAD_LEN)
80 * For iscsi connections HW may inserts digest bytes into the pdu. Those digest
81 * bytes are not sent by the host but are part of the TCP payload and therefore
82 * consume TCP sequence space.
84 static const unsigned int ulp2_extra_len
[] = { 0, 4, 4, 8 };
85 static inline unsigned int cxgbi_ulp_extra_len(int submode
)
87 return ulp2_extra_len
[submode
& 3];
90 #define CPL_RX_DDP_STATUS_DDP_SHIFT 16 /* ddp'able */
91 #define CPL_RX_DDP_STATUS_PAD_SHIFT 19 /* pad error */
92 #define CPL_RX_DDP_STATUS_HCRC_SHIFT 20 /* hcrc error */
93 #define CPL_RX_DDP_STATUS_DCRC_SHIFT 21 /* dcrc error */
97 * Opaque version of structure the SGE stores at skb->head of TX_DATA packets
98 * and for which we must reserve space.
100 struct sge_opaque_hdr
{
102 dma_addr_t addr
[MAX_SKB_FRAGS
+ 1];
106 struct cxgbi_device
*cdev
;
112 unsigned short rss_qid
;
113 unsigned short txq_idx
;
114 unsigned short advmss
;
115 unsigned int tx_chan
;
116 unsigned int rx_chan
;
117 unsigned int mss_idx
;
118 unsigned int smac_idx
;
119 unsigned char port_id
;
123 unsigned char hcrc_len
;
124 unsigned char dcrc_len
;
127 struct sk_buff
*wr_pending_head
;
128 struct sk_buff
*wr_pending_tail
;
129 struct sk_buff
*cpl_close
;
130 struct sk_buff
*cpl_abort_req
;
131 struct sk_buff
*cpl_abort_rpl
;
132 struct sk_buff
*skb_ulp_lhdr
;
136 unsigned int csk_family
;
138 struct sockaddr_in saddr
;
139 struct sockaddr_in6 saddr6
;
142 struct sockaddr_in daddr
;
143 struct sockaddr_in6 daddr6
;
145 struct dst_entry
*dst
;
146 struct sk_buff_head receive_queue
;
147 struct sk_buff_head write_queue
;
148 struct timer_list retry_timer
;
150 rwlock_t callback_lock
;
166 enum cxgbi_sock_states
{
179 * Connection flags -- many to track some close related events.
181 enum cxgbi_sock_flags
{
182 CTPF_ABORT_RPL_RCVD
, /*received one ABORT_RPL_RSS message */
183 CTPF_ABORT_REQ_RCVD
, /*received one ABORT_REQ_RSS message */
184 CTPF_ABORT_RPL_PENDING
, /* expecting an abort reply */
185 CTPF_TX_DATA_SENT
, /* already sent a TX_DATA WR */
186 CTPF_ACTIVE_CLOSE_NEEDED
,/* need to be closed */
187 CTPF_HAS_ATID
, /* reserved atid */
188 CTPF_HAS_TID
, /* reserved hw tid */
189 CTPF_OFFLOAD_DOWN
, /* offload function off */
192 struct cxgbi_skb_rx_cb
{
197 struct cxgbi_skb_tx_cb
{
199 struct sk_buff
*wr_next
;
202 enum cxgbi_skcb_flags
{
203 SKCBF_TX_NEED_HDR
, /* packet needs a header */
204 SKCBF_TX_MEM_WRITE
, /* memory write */
205 SKCBF_TX_FLAG_COMPL
, /* wr completion flag */
206 SKCBF_RX_COALESCED
, /* received whole pdu */
207 SKCBF_RX_HDR
, /* received pdu header */
208 SKCBF_RX_DATA
, /* received pdu payload */
209 SKCBF_RX_STATUS
, /* received ddp status */
210 SKCBF_RX_DATA_DDPD
, /* pdu payload ddp'd */
211 SKCBF_RX_HCRC_ERR
, /* header digest error */
212 SKCBF_RX_DCRC_ERR
, /* data digest error */
213 SKCBF_RX_PAD_ERR
, /* padding byte error */
216 struct cxgbi_skb_cb
{
217 unsigned char ulp_mode
;
221 struct cxgbi_skb_rx_cb rx
;
222 struct cxgbi_skb_tx_cb tx
;
226 #define CXGBI_SKB_CB(skb) ((struct cxgbi_skb_cb *)&((skb)->cb[0]))
227 #define cxgbi_skcb_flags(skb) (CXGBI_SKB_CB(skb)->flags)
228 #define cxgbi_skcb_ulp_mode(skb) (CXGBI_SKB_CB(skb)->ulp_mode)
229 #define cxgbi_skcb_tcp_seq(skb) (CXGBI_SKB_CB(skb)->seq)
230 #define cxgbi_skcb_rx_ddigest(skb) (CXGBI_SKB_CB(skb)->rx.ddigest)
231 #define cxgbi_skcb_rx_pdulen(skb) (CXGBI_SKB_CB(skb)->rx.pdulen)
232 #define cxgbi_skcb_tx_wr_next(skb) (CXGBI_SKB_CB(skb)->tx.wr_next)
234 static inline void cxgbi_skcb_set_flag(struct sk_buff
*skb
,
235 enum cxgbi_skcb_flags flag
)
237 __set_bit(flag
, &(cxgbi_skcb_flags(skb
)));
240 static inline void cxgbi_skcb_clear_flag(struct sk_buff
*skb
,
241 enum cxgbi_skcb_flags flag
)
243 __clear_bit(flag
, &(cxgbi_skcb_flags(skb
)));
246 static inline int cxgbi_skcb_test_flag(const struct sk_buff
*skb
,
247 enum cxgbi_skcb_flags flag
)
249 return test_bit(flag
, &(cxgbi_skcb_flags(skb
)));
252 static inline void cxgbi_sock_set_flag(struct cxgbi_sock
*csk
,
253 enum cxgbi_sock_flags flag
)
255 __set_bit(flag
, &csk
->flags
);
256 log_debug(1 << CXGBI_DBG_SOCK
,
257 "csk 0x%p,%u,0x%lx, bit %d.\n",
258 csk
, csk
->state
, csk
->flags
, flag
);
261 static inline void cxgbi_sock_clear_flag(struct cxgbi_sock
*csk
,
262 enum cxgbi_sock_flags flag
)
264 __clear_bit(flag
, &csk
->flags
);
265 log_debug(1 << CXGBI_DBG_SOCK
,
266 "csk 0x%p,%u,0x%lx, bit %d.\n",
267 csk
, csk
->state
, csk
->flags
, flag
);
270 static inline int cxgbi_sock_flag(struct cxgbi_sock
*csk
,
271 enum cxgbi_sock_flags flag
)
275 return test_bit(flag
, &csk
->flags
);
278 static inline void cxgbi_sock_set_state(struct cxgbi_sock
*csk
, int state
)
280 log_debug(1 << CXGBI_DBG_SOCK
,
281 "csk 0x%p,%u,0x%lx, state -> %u.\n",
282 csk
, csk
->state
, csk
->flags
, state
);
286 static inline void cxgbi_sock_free(struct kref
*kref
)
288 struct cxgbi_sock
*csk
= container_of(kref
,
292 log_debug(1 << CXGBI_DBG_SOCK
,
293 "free csk 0x%p, state %u, flags 0x%lx\n",
294 csk
, csk
->state
, csk
->flags
);
299 static inline void __cxgbi_sock_put(const char *fn
, struct cxgbi_sock
*csk
)
301 log_debug(1 << CXGBI_DBG_SOCK
,
302 "%s, put csk 0x%p, ref %u-1.\n",
303 fn
, csk
, atomic_read(&csk
->refcnt
.refcount
));
304 kref_put(&csk
->refcnt
, cxgbi_sock_free
);
306 #define cxgbi_sock_put(csk) __cxgbi_sock_put(__func__, csk)
308 static inline void __cxgbi_sock_get(const char *fn
, struct cxgbi_sock
*csk
)
310 log_debug(1 << CXGBI_DBG_SOCK
,
311 "%s, get csk 0x%p, ref %u+1.\n",
312 fn
, csk
, atomic_read(&csk
->refcnt
.refcount
));
313 kref_get(&csk
->refcnt
);
315 #define cxgbi_sock_get(csk) __cxgbi_sock_get(__func__, csk)
317 static inline int cxgbi_sock_is_closing(struct cxgbi_sock
*csk
)
319 return csk
->state
>= CTP_ACTIVE_CLOSE
;
322 static inline int cxgbi_sock_is_established(struct cxgbi_sock
*csk
)
324 return csk
->state
== CTP_ESTABLISHED
;
327 static inline void cxgbi_sock_purge_write_queue(struct cxgbi_sock
*csk
)
331 while ((skb
= __skb_dequeue(&csk
->write_queue
)))
335 static inline unsigned int cxgbi_sock_compute_wscale(unsigned int win
)
337 unsigned int wscale
= 0;
339 while (wscale
< 14 && (65535 << wscale
) < win
)
344 static inline struct sk_buff
*alloc_wr(int wrlen
, int dlen
, gfp_t gfp
)
346 struct sk_buff
*skb
= alloc_skb(wrlen
+ dlen
, gfp
);
349 __skb_put(skb
, wrlen
);
350 memset(skb
->head
, 0, wrlen
+ dlen
);
352 pr_info("alloc cpl wr skb %u+%u, OOM.\n", wrlen
, dlen
);
358 * The number of WRs needed for an skb depends on the number of fragments
359 * in the skb and whether it has any payload in its main body. This maps the
360 * length of the gather list represented by an skb into the # of necessary WRs.
361 * The extra two fragments are for iscsi bhs and payload padding.
363 #define SKB_WR_LIST_SIZE (MAX_SKB_FRAGS + 2)
365 static inline void cxgbi_sock_reset_wr_list(struct cxgbi_sock
*csk
)
367 csk
->wr_pending_head
= csk
->wr_pending_tail
= NULL
;
370 static inline void cxgbi_sock_enqueue_wr(struct cxgbi_sock
*csk
,
373 cxgbi_skcb_tx_wr_next(skb
) = NULL
;
375 * We want to take an extra reference since both us and the driver
376 * need to free the packet before it's really freed. We know there's
377 * just one user currently so we use atomic_set rather than skb_get
378 * to avoid the atomic op.
380 atomic_set(&skb
->users
, 2);
382 if (!csk
->wr_pending_head
)
383 csk
->wr_pending_head
= skb
;
385 cxgbi_skcb_tx_wr_next(csk
->wr_pending_tail
) = skb
;
386 csk
->wr_pending_tail
= skb
;
389 static inline int cxgbi_sock_count_pending_wrs(const struct cxgbi_sock
*csk
)
392 const struct sk_buff
*skb
= csk
->wr_pending_head
;
396 skb
= cxgbi_skcb_tx_wr_next(skb
);
401 static inline struct sk_buff
*cxgbi_sock_peek_wr(const struct cxgbi_sock
*csk
)
403 return csk
->wr_pending_head
;
406 static inline struct sk_buff
*cxgbi_sock_dequeue_wr(struct cxgbi_sock
*csk
)
408 struct sk_buff
*skb
= csk
->wr_pending_head
;
411 csk
->wr_pending_head
= cxgbi_skcb_tx_wr_next(skb
);
412 cxgbi_skcb_tx_wr_next(skb
) = NULL
;
417 void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock
*);
418 void cxgbi_sock_purge_wr_queue(struct cxgbi_sock
*);
419 void cxgbi_sock_skb_entail(struct cxgbi_sock
*, struct sk_buff
*);
420 void cxgbi_sock_fail_act_open(struct cxgbi_sock
*, int);
421 void cxgbi_sock_act_open_req_arp_failure(void *, struct sk_buff
*);
422 void cxgbi_sock_closed(struct cxgbi_sock
*);
423 void cxgbi_sock_established(struct cxgbi_sock
*, unsigned int, unsigned int);
424 void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock
*);
425 void cxgbi_sock_rcv_peer_close(struct cxgbi_sock
*);
426 void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock
*, u32
);
427 void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock
*, unsigned int, unsigned int,
429 unsigned int cxgbi_sock_select_mss(struct cxgbi_sock
*, unsigned int);
430 void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock
*);
433 struct net_device
*ndev
;
434 struct net_device
*vdev
; /* vlan dev */
435 struct Scsi_Host
*shost
;
436 struct cxgbi_device
*cdev
;
438 unsigned char port_id
;
441 struct cxgbi_ports_map
{
442 unsigned int max_connect
;
444 unsigned short sport_base
;
447 struct cxgbi_sock
**port_csk
;
450 #define CXGBI_FLAG_DEV_T3 0x1
451 #define CXGBI_FLAG_DEV_T4 0x2
452 #define CXGBI_FLAG_ADAPTER_RESET 0x4
453 #define CXGBI_FLAG_IPV4_SET 0x10
454 #define CXGBI_FLAG_USE_PPOD_OFLDQ 0x40
455 #define CXGBI_FLAG_DDP_OFF 0x100
457 struct cxgbi_device
{
458 struct list_head list_head
;
459 struct list_head rcu_node
;
461 struct net_device
**ports
;
463 struct cxgbi_hba
**hbas
;
464 const unsigned short *mtus
;
466 unsigned char nports
;
467 struct pci_dev
*pdev
;
468 struct dentry
*debugfs_root
;
469 struct iscsi_transport
*itp
;
472 unsigned int rx_credit_thres
;
473 unsigned int skb_tx_rsvd
;
474 unsigned int skb_rx_extra
; /* for msg coalesced mode */
475 unsigned int tx_max_size
;
476 unsigned int rx_max_size
;
477 struct cxgbi_ports_map pmap
;
479 void (*dev_ddp_cleanup
)(struct cxgbi_device
*);
480 struct cxgbi_ppm
* (*cdev2ppm
)(struct cxgbi_device
*);
481 int (*csk_ddp_set_map
)(struct cxgbi_ppm
*, struct cxgbi_sock
*,
482 struct cxgbi_task_tag_info
*);
483 void (*csk_ddp_clear_map
)(struct cxgbi_device
*cdev
,
485 struct cxgbi_task_tag_info
*);
486 int (*csk_ddp_setup_digest
)(struct cxgbi_sock
*,
487 unsigned int, int, int, int);
488 int (*csk_ddp_setup_pgidx
)(struct cxgbi_sock
*,
489 unsigned int, int, bool);
491 void (*csk_release_offload_resources
)(struct cxgbi_sock
*);
492 int (*csk_rx_pdu_ready
)(struct cxgbi_sock
*, struct sk_buff
*);
493 u32 (*csk_send_rx_credits
)(struct cxgbi_sock
*, u32
);
494 int (*csk_push_tx_frames
)(struct cxgbi_sock
*, int);
495 void (*csk_send_abort_req
)(struct cxgbi_sock
*);
496 void (*csk_send_close_req
)(struct cxgbi_sock
*);
497 int (*csk_alloc_cpls
)(struct cxgbi_sock
*);
498 int (*csk_init_act_open
)(struct cxgbi_sock
*);
502 #define cxgbi_cdev_priv(cdev) ((cdev)->dd_data)
505 struct cxgbi_endpoint
*cep
;
506 struct iscsi_conn
*iconn
;
507 struct cxgbi_hba
*chba
;
509 unsigned int ddp_full
;
510 unsigned int ddp_tag_full
;
513 struct cxgbi_endpoint
{
514 struct cxgbi_conn
*cconn
;
515 struct cxgbi_hba
*chba
;
516 struct cxgbi_sock
*csk
;
519 #define MAX_PDU_FRAGS ((ULP2_MAX_PDU_PAYLOAD + 512 - 1) / 512)
520 struct cxgbi_task_data
{
521 unsigned short nr_frags
;
522 struct page_frag frags
[MAX_PDU_FRAGS
];
527 unsigned int sgoffset
;
528 struct cxgbi_task_tag_info ttinfo
;
530 #define iscsi_task_cxgbi_data(task) \
531 ((task)->dd_data + sizeof(struct iscsi_tcp_task))
533 static inline void *cxgbi_alloc_big_mem(unsigned int size
,
536 void *p
= kzalloc(size
, gfp
| __GFP_NOWARN
);
544 static inline void cxgbi_free_big_mem(void *addr
)
549 static inline void cxgbi_set_iscsi_ipv4(struct cxgbi_hba
*chba
, __be32 ipaddr
)
551 if (chba
->cdev
->flags
& CXGBI_FLAG_IPV4_SET
)
552 chba
->ipv4addr
= ipaddr
;
554 pr_info("set iscsi ipv4 NOT supported, using %s ipv4.\n",
558 struct cxgbi_device
*cxgbi_device_register(unsigned int, unsigned int);
559 void cxgbi_device_unregister(struct cxgbi_device
*);
560 void cxgbi_device_unregister_all(unsigned int flag
);
561 struct cxgbi_device
*cxgbi_device_find_by_lldev(void *);
562 struct cxgbi_device
*cxgbi_device_find_by_netdev(struct net_device
*, int *);
563 struct cxgbi_device
*cxgbi_device_find_by_netdev_rcu(struct net_device
*,
565 int cxgbi_hbas_add(struct cxgbi_device
*, u64
, unsigned int,
566 struct scsi_host_template
*,
567 struct scsi_transport_template
*);
568 void cxgbi_hbas_remove(struct cxgbi_device
*);
570 int cxgbi_device_portmap_create(struct cxgbi_device
*cdev
, unsigned int base
,
571 unsigned int max_conn
);
572 void cxgbi_device_portmap_cleanup(struct cxgbi_device
*cdev
);
574 void cxgbi_conn_tx_open(struct cxgbi_sock
*);
575 void cxgbi_conn_pdu_ready(struct cxgbi_sock
*);
576 int cxgbi_conn_alloc_pdu(struct iscsi_task
*, u8
);
577 int cxgbi_conn_init_pdu(struct iscsi_task
*, unsigned int , unsigned int);
578 int cxgbi_conn_xmit_pdu(struct iscsi_task
*);
580 void cxgbi_cleanup_task(struct iscsi_task
*task
);
582 umode_t
cxgbi_attr_is_visible(int param_type
, int param
);
583 void cxgbi_get_conn_stats(struct iscsi_cls_conn
*, struct iscsi_stats
*);
584 int cxgbi_set_conn_param(struct iscsi_cls_conn
*,
585 enum iscsi_param
, char *, int);
586 int cxgbi_get_ep_param(struct iscsi_endpoint
*ep
, enum iscsi_param
, char *);
587 struct iscsi_cls_conn
*cxgbi_create_conn(struct iscsi_cls_session
*, u32
);
588 int cxgbi_bind_conn(struct iscsi_cls_session
*,
589 struct iscsi_cls_conn
*, u64
, int);
590 void cxgbi_destroy_session(struct iscsi_cls_session
*);
591 struct iscsi_cls_session
*cxgbi_create_session(struct iscsi_endpoint
*,
593 int cxgbi_set_host_param(struct Scsi_Host
*,
594 enum iscsi_host_param
, char *, int);
595 int cxgbi_get_host_param(struct Scsi_Host
*, enum iscsi_host_param
, char *);
596 struct iscsi_endpoint
*cxgbi_ep_connect(struct Scsi_Host
*,
597 struct sockaddr
*, int);
598 int cxgbi_ep_poll(struct iscsi_endpoint
*, int);
599 void cxgbi_ep_disconnect(struct iscsi_endpoint
*);
601 int cxgbi_iscsi_init(struct iscsi_transport
*,
602 struct scsi_transport_template
**);
603 void cxgbi_iscsi_cleanup(struct iscsi_transport
*,
604 struct scsi_transport_template
**);
605 void cxgbi_parse_pdu_itt(struct iscsi_conn
*, itt_t
, int *, int *);
606 int cxgbi_ddp_init(struct cxgbi_device
*, unsigned int, unsigned int,
607 unsigned int, unsigned int);
608 int cxgbi_ddp_cleanup(struct cxgbi_device
*);
609 void cxgbi_ddp_page_size_factor(int *);
610 void cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod
*,
611 struct cxgbi_task_tag_info
*,
612 struct scatterlist
**sg_pp
, unsigned int *sg_off
);
613 void cxgbi_ddp_ppm_setup(void **ppm_pp
, struct cxgbi_device
*,
614 struct cxgbi_tag_format
*, unsigned int ppmax
,
615 unsigned int llimit
, unsigned int start
,
616 unsigned int rsvd_factor
);
617 #endif /*__LIBCXGBI_H__*/