These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / scsi / cxgbi / libcxgbi.h
1 /*
2  * libcxgbi.h: Chelsio common library for T3/T4 iSCSI driver.
3  *
4  * Copyright (c) 2010-2015 Chelsio Communications, Inc.
5  *
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.
9  *
10  * Written by: Karen Xie (kxie@chelsio.com)
11  * Written by: Rakesh Ranjan (rranjan@chelsio.com)
12  */
13
14 #ifndef __LIBCXGBI_H__
15 #define __LIBCXGBI_H__
16
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 <scsi/scsi_device.h>
28 #include <scsi/libiscsi_tcp.h>
29
30 enum cxgbi_dbg_flag {
31         CXGBI_DBG_ISCSI,
32         CXGBI_DBG_DDP,
33         CXGBI_DBG_TOE,
34         CXGBI_DBG_SOCK,
35
36         CXGBI_DBG_PDU_TX,
37         CXGBI_DBG_PDU_RX,
38         CXGBI_DBG_DEV,
39 };
40
41 #define log_debug(level, fmt, ...)      \
42         do {    \
43                 if (dbg_level & (level)) \
44                         pr_info(fmt, ##__VA_ARGS__); \
45         } while (0)
46
47 #define pr_info_ipaddr(fmt_trail,                                       \
48                         addr1, addr2, args_trail...)                    \
49 do {                                                                    \
50         if (!((1 << CXGBI_DBG_SOCK) & dbg_level))                       \
51                 break;                                                  \
52         pr_info("%pISpc - %pISpc, " fmt_trail,                          \
53                 addr1, addr2, args_trail);                              \
54 } while (0)
55
56 /* max. connections per adapter */
57 #define CXGBI_MAX_CONN          16384
58
59 /* always allocate rooms for AHS */
60 #define SKB_TX_ISCSI_PDU_HEADER_MAX     \
61         (sizeof(struct iscsi_hdr) + ISCSI_MAX_AHS_SIZE)
62
63 #define ISCSI_PDU_NONPAYLOAD_LEN        312 /* bhs(48) + ahs(256) + digest(8)*/
64
65 /*
66  * align pdu size to multiple of 512 for better performance
67  */
68 #define cxgbi_align_pdu_size(n) do { n = (n) & (~511); } while (0)
69
70 #define ULP2_MODE_ISCSI         2
71
72 #define ULP2_MAX_PKT_SIZE       16224
73 #define ULP2_MAX_PDU_PAYLOAD    \
74         (ULP2_MAX_PKT_SIZE - ISCSI_PDU_NONPAYLOAD_LEN)
75
76 /*
77  * For iscsi connections HW may inserts digest bytes into the pdu. Those digest
78  * bytes are not sent by the host but are part of the TCP payload and therefore
79  * consume TCP sequence space.
80  */
81 static const unsigned int ulp2_extra_len[] = { 0, 4, 4, 8 };
82 static inline unsigned int cxgbi_ulp_extra_len(int submode)
83 {
84         return ulp2_extra_len[submode & 3];
85 }
86
87 /*
88  * struct pagepod_hdr, pagepod - pagepod format
89  */
90
91 #define CPL_RX_DDP_STATUS_DDP_SHIFT     16 /* ddp'able */
92 #define CPL_RX_DDP_STATUS_PAD_SHIFT     19 /* pad error */
93 #define CPL_RX_DDP_STATUS_HCRC_SHIFT    20 /* hcrc error */
94 #define CPL_RX_DDP_STATUS_DCRC_SHIFT    21 /* dcrc error */
95
96 struct cxgbi_pagepod_hdr {
97         u32 vld_tid;
98         u32 pgsz_tag_clr;
99         u32 max_offset;
100         u32 page_offset;
101         u64 rsvd;
102 };
103
104 #define PPOD_PAGES_MAX                  4
105 struct cxgbi_pagepod {
106         struct cxgbi_pagepod_hdr hdr;
107         u64 addr[PPOD_PAGES_MAX + 1];
108 };
109
110 struct cxgbi_tag_format {
111         unsigned char sw_bits;
112         unsigned char rsvd_bits;
113         unsigned char rsvd_shift;
114         unsigned char filler[1];
115         u32 rsvd_mask;
116 };
117
118 struct cxgbi_gather_list {
119         unsigned int tag;
120         unsigned int length;
121         unsigned int offset;
122         unsigned int nelem;
123         struct page **pages;
124         dma_addr_t phys_addr[0];
125 };
126
127 struct cxgbi_ddp_info {
128         struct kref refcnt;
129         struct cxgbi_device *cdev;
130         struct pci_dev *pdev;
131         unsigned int max_txsz;
132         unsigned int max_rxsz;
133         unsigned int llimit;
134         unsigned int ulimit;
135         unsigned int nppods;
136         unsigned int idx_last;
137         unsigned char idx_bits;
138         unsigned char filler[3];
139         unsigned int idx_mask;
140         unsigned int rsvd_tag_mask;
141         spinlock_t map_lock;
142         struct cxgbi_gather_list **gl_map;
143 };
144
145 #define DDP_PGIDX_MAX           4
146 #define DDP_THRESHOLD           2048
147
148 #define PPOD_PAGES_SHIFT        2       /*  4 pages per pod */
149
150 #define PPOD_SIZE               sizeof(struct cxgbi_pagepod)  /*  64 */
151 #define PPOD_SIZE_SHIFT         6
152
153 #define ULPMEM_DSGL_MAX_NPPODS  16      /*  1024/PPOD_SIZE */
154 #define ULPMEM_IDATA_MAX_NPPODS 4       /*  256/PPOD_SIZE */
155 #define PCIE_MEMWIN_MAX_NPPODS  16      /*  1024/PPOD_SIZE */
156
157 #define PPOD_COLOR_SHIFT        0
158 #define PPOD_COLOR(x)           ((x) << PPOD_COLOR_SHIFT)
159
160 #define PPOD_IDX_SHIFT          6
161 #define PPOD_IDX_MAX_SIZE       24
162
163 #define PPOD_TID_SHIFT          0
164 #define PPOD_TID(x)             ((x) << PPOD_TID_SHIFT)
165
166 #define PPOD_TAG_SHIFT          6
167 #define PPOD_TAG(x)             ((x) << PPOD_TAG_SHIFT)
168
169 #define PPOD_VALID_SHIFT        24
170 #define PPOD_VALID(x)           ((x) << PPOD_VALID_SHIFT)
171 #define PPOD_VALID_FLAG         PPOD_VALID(1U)
172
173 /*
174  * sge_opaque_hdr -
175  * Opaque version of structure the SGE stores at skb->head of TX_DATA packets
176  * and for which we must reserve space.
177  */
178 struct sge_opaque_hdr {
179         void *dev;
180         dma_addr_t addr[MAX_SKB_FRAGS + 1];
181 };
182
183 struct cxgbi_sock {
184         struct cxgbi_device *cdev;
185
186         int tid;
187         int atid;
188         unsigned long flags;
189         unsigned int mtu;
190         unsigned short rss_qid;
191         unsigned short txq_idx;
192         unsigned short advmss;
193         unsigned int tx_chan;
194         unsigned int rx_chan;
195         unsigned int mss_idx;
196         unsigned int smac_idx;
197         unsigned char port_id;
198         int wr_max_cred;
199         int wr_cred;
200         int wr_una_cred;
201         unsigned char hcrc_len;
202         unsigned char dcrc_len;
203
204         void *l2t;
205         struct sk_buff *wr_pending_head;
206         struct sk_buff *wr_pending_tail;
207         struct sk_buff *cpl_close;
208         struct sk_buff *cpl_abort_req;
209         struct sk_buff *cpl_abort_rpl;
210         struct sk_buff *skb_ulp_lhdr;
211         spinlock_t lock;
212         struct kref refcnt;
213         unsigned int state;
214         unsigned int csk_family;
215         union {
216                 struct sockaddr_in saddr;
217                 struct sockaddr_in6 saddr6;
218         };
219         union {
220                 struct sockaddr_in daddr;
221                 struct sockaddr_in6 daddr6;
222         };
223         struct dst_entry *dst;
224         struct sk_buff_head receive_queue;
225         struct sk_buff_head write_queue;
226         struct timer_list retry_timer;
227         int err;
228         rwlock_t callback_lock;
229         void *user_data;
230
231         u32 rcv_nxt;
232         u32 copied_seq;
233         u32 rcv_wup;
234         u32 snd_nxt;
235         u32 snd_una;
236         u32 write_seq;
237         u32 snd_win;
238         u32 rcv_win;
239 };
240
241 /*
242  * connection states
243  */
244 enum cxgbi_sock_states{
245         CTP_CLOSED,
246         CTP_CONNECTING,
247         CTP_ACTIVE_OPEN,
248         CTP_ESTABLISHED,
249         CTP_ACTIVE_CLOSE,
250         CTP_PASSIVE_CLOSE,
251         CTP_CLOSE_WAIT_1,
252         CTP_CLOSE_WAIT_2,
253         CTP_ABORTING,
254 };
255
256 /*
257  * Connection flags -- many to track some close related events.
258  */
259 enum cxgbi_sock_flags {
260         CTPF_ABORT_RPL_RCVD,    /*received one ABORT_RPL_RSS message */
261         CTPF_ABORT_REQ_RCVD,    /*received one ABORT_REQ_RSS message */
262         CTPF_ABORT_RPL_PENDING, /* expecting an abort reply */
263         CTPF_TX_DATA_SENT,      /* already sent a TX_DATA WR */
264         CTPF_ACTIVE_CLOSE_NEEDED,/* need to be closed */
265         CTPF_HAS_ATID,          /* reserved atid */
266         CTPF_HAS_TID,           /* reserved hw tid */
267         CTPF_OFFLOAD_DOWN,      /* offload function off */
268 };
269
270 struct cxgbi_skb_rx_cb {
271         __u32 ddigest;
272         __u32 pdulen;
273 };
274
275 struct cxgbi_skb_tx_cb {
276         void *l2t;
277         struct sk_buff *wr_next;
278 };
279
280 enum cxgbi_skcb_flags {
281         SKCBF_TX_NEED_HDR,      /* packet needs a header */
282         SKCBF_RX_COALESCED,     /* received whole pdu */
283         SKCBF_RX_HDR,           /* received pdu header */
284         SKCBF_RX_DATA,          /* received pdu payload */
285         SKCBF_RX_STATUS,        /* received ddp status */
286         SKCBF_RX_DATA_DDPD,     /* pdu payload ddp'd */
287         SKCBF_RX_HCRC_ERR,      /* header digest error */
288         SKCBF_RX_DCRC_ERR,      /* data digest error */
289         SKCBF_RX_PAD_ERR,       /* padding byte error */
290 };
291
292 struct cxgbi_skb_cb {
293         unsigned char ulp_mode;
294         unsigned long flags;
295         unsigned int seq;
296         union {
297                 struct cxgbi_skb_rx_cb rx;
298                 struct cxgbi_skb_tx_cb tx;
299         };
300 };
301
302 #define CXGBI_SKB_CB(skb)       ((struct cxgbi_skb_cb *)&((skb)->cb[0]))
303 #define cxgbi_skcb_flags(skb)           (CXGBI_SKB_CB(skb)->flags)
304 #define cxgbi_skcb_ulp_mode(skb)        (CXGBI_SKB_CB(skb)->ulp_mode)
305 #define cxgbi_skcb_tcp_seq(skb)         (CXGBI_SKB_CB(skb)->seq)
306 #define cxgbi_skcb_rx_ddigest(skb)      (CXGBI_SKB_CB(skb)->rx.ddigest)
307 #define cxgbi_skcb_rx_pdulen(skb)       (CXGBI_SKB_CB(skb)->rx.pdulen)
308 #define cxgbi_skcb_tx_wr_next(skb)      (CXGBI_SKB_CB(skb)->tx.wr_next)
309
310 static inline void cxgbi_skcb_set_flag(struct sk_buff *skb,
311                                         enum cxgbi_skcb_flags flag)
312 {
313         __set_bit(flag, &(cxgbi_skcb_flags(skb)));
314 }
315
316 static inline void cxgbi_skcb_clear_flag(struct sk_buff *skb,
317                                         enum cxgbi_skcb_flags flag)
318 {
319         __clear_bit(flag, &(cxgbi_skcb_flags(skb)));
320 }
321
322 static inline int cxgbi_skcb_test_flag(const struct sk_buff *skb,
323                                        enum cxgbi_skcb_flags flag)
324 {
325         return test_bit(flag, &(cxgbi_skcb_flags(skb)));
326 }
327
328 static inline void cxgbi_sock_set_flag(struct cxgbi_sock *csk,
329                                         enum cxgbi_sock_flags flag)
330 {
331         __set_bit(flag, &csk->flags);
332         log_debug(1 << CXGBI_DBG_SOCK,
333                 "csk 0x%p,%u,0x%lx, bit %d.\n",
334                 csk, csk->state, csk->flags, flag);
335 }
336
337 static inline void cxgbi_sock_clear_flag(struct cxgbi_sock *csk,
338                                         enum cxgbi_sock_flags flag)
339 {
340         __clear_bit(flag, &csk->flags);
341         log_debug(1 << CXGBI_DBG_SOCK,
342                 "csk 0x%p,%u,0x%lx, bit %d.\n",
343                 csk, csk->state, csk->flags, flag);
344 }
345
346 static inline int cxgbi_sock_flag(struct cxgbi_sock *csk,
347                                 enum cxgbi_sock_flags flag)
348 {
349         if (csk == NULL)
350                 return 0;
351         return test_bit(flag, &csk->flags);
352 }
353
354 static inline void cxgbi_sock_set_state(struct cxgbi_sock *csk, int state)
355 {
356         log_debug(1 << CXGBI_DBG_SOCK,
357                 "csk 0x%p,%u,0x%lx, state -> %u.\n",
358                 csk, csk->state, csk->flags, state);
359         csk->state = state;
360 }
361
362 static inline void cxgbi_sock_free(struct kref *kref)
363 {
364         struct cxgbi_sock *csk = container_of(kref,
365                                                 struct cxgbi_sock,
366                                                 refcnt);
367         if (csk) {
368                 log_debug(1 << CXGBI_DBG_SOCK,
369                         "free csk 0x%p, state %u, flags 0x%lx\n",
370                         csk, csk->state, csk->flags);
371                 kfree(csk);
372         }
373 }
374
375 static inline void __cxgbi_sock_put(const char *fn, struct cxgbi_sock *csk)
376 {
377         log_debug(1 << CXGBI_DBG_SOCK,
378                 "%s, put csk 0x%p, ref %u-1.\n",
379                 fn, csk, atomic_read(&csk->refcnt.refcount));
380         kref_put(&csk->refcnt, cxgbi_sock_free);
381 }
382 #define cxgbi_sock_put(csk)     __cxgbi_sock_put(__func__, csk)
383
384 static inline void __cxgbi_sock_get(const char *fn, struct cxgbi_sock *csk)
385 {
386         log_debug(1 << CXGBI_DBG_SOCK,
387                 "%s, get csk 0x%p, ref %u+1.\n",
388                 fn, csk, atomic_read(&csk->refcnt.refcount));
389         kref_get(&csk->refcnt);
390 }
391 #define cxgbi_sock_get(csk)     __cxgbi_sock_get(__func__, csk)
392
393 static inline int cxgbi_sock_is_closing(struct cxgbi_sock *csk)
394 {
395         return csk->state >= CTP_ACTIVE_CLOSE;
396 }
397
398 static inline int cxgbi_sock_is_established(struct cxgbi_sock *csk)
399 {
400         return csk->state == CTP_ESTABLISHED;
401 }
402
403 static inline void cxgbi_sock_purge_write_queue(struct cxgbi_sock *csk)
404 {
405         struct sk_buff *skb;
406
407         while ((skb = __skb_dequeue(&csk->write_queue)))
408                 __kfree_skb(skb);
409 }
410
411 static inline unsigned int cxgbi_sock_compute_wscale(unsigned int win)
412 {
413         unsigned int wscale = 0;
414
415         while (wscale < 14 && (65535 << wscale) < win)
416                 wscale++;
417         return wscale;
418 }
419
420 static inline struct sk_buff *alloc_wr(int wrlen, int dlen, gfp_t gfp)
421 {
422         struct sk_buff *skb = alloc_skb(wrlen + dlen, gfp);
423
424         if (skb) {
425                 __skb_put(skb, wrlen);
426                 memset(skb->head, 0, wrlen + dlen);
427         } else
428                 pr_info("alloc cpl wr skb %u+%u, OOM.\n", wrlen, dlen);
429         return skb;
430 }
431
432
433 /*
434  * The number of WRs needed for an skb depends on the number of fragments
435  * in the skb and whether it has any payload in its main body.  This maps the
436  * length of the gather list represented by an skb into the # of necessary WRs.
437  * The extra two fragments are for iscsi bhs and payload padding.
438  */
439 #define SKB_WR_LIST_SIZE         (MAX_SKB_FRAGS + 2)
440
441 static inline void cxgbi_sock_reset_wr_list(struct cxgbi_sock *csk)
442 {
443         csk->wr_pending_head = csk->wr_pending_tail = NULL;
444 }
445
446 static inline void cxgbi_sock_enqueue_wr(struct cxgbi_sock *csk,
447                                           struct sk_buff *skb)
448 {
449         cxgbi_skcb_tx_wr_next(skb) = NULL;
450         /*
451          * We want to take an extra reference since both us and the driver
452          * need to free the packet before it's really freed. We know there's
453          * just one user currently so we use atomic_set rather than skb_get
454          * to avoid the atomic op.
455          */
456         atomic_set(&skb->users, 2);
457
458         if (!csk->wr_pending_head)
459                 csk->wr_pending_head = skb;
460         else
461                 cxgbi_skcb_tx_wr_next(csk->wr_pending_tail) = skb;
462         csk->wr_pending_tail = skb;
463 }
464
465 static inline int cxgbi_sock_count_pending_wrs(const struct cxgbi_sock *csk)
466 {
467         int n = 0;
468         const struct sk_buff *skb = csk->wr_pending_head;
469
470         while (skb) {
471                 n += skb->csum;
472                 skb = cxgbi_skcb_tx_wr_next(skb);
473         }
474         return n;
475 }
476
477 static inline struct sk_buff *cxgbi_sock_peek_wr(const struct cxgbi_sock *csk)
478 {
479         return csk->wr_pending_head;
480 }
481
482 static inline struct sk_buff *cxgbi_sock_dequeue_wr(struct cxgbi_sock *csk)
483 {
484         struct sk_buff *skb = csk->wr_pending_head;
485
486         if (likely(skb)) {
487                 csk->wr_pending_head = cxgbi_skcb_tx_wr_next(skb);
488                 cxgbi_skcb_tx_wr_next(skb) = NULL;
489         }
490         return skb;
491 }
492
493 void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *);
494 void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *);
495 void cxgbi_sock_skb_entail(struct cxgbi_sock *, struct sk_buff *);
496 void cxgbi_sock_fail_act_open(struct cxgbi_sock *, int);
497 void cxgbi_sock_act_open_req_arp_failure(void *, struct sk_buff *);
498 void cxgbi_sock_closed(struct cxgbi_sock *);
499 void cxgbi_sock_established(struct cxgbi_sock *, unsigned int, unsigned int);
500 void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *);
501 void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *);
502 void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *, u32);
503 void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *, unsigned int, unsigned int,
504                                 int);
505 unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *, unsigned int);
506 void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *);
507
508 struct cxgbi_hba {
509         struct net_device *ndev;
510         struct net_device *vdev;        /* vlan dev */
511         struct Scsi_Host *shost;
512         struct cxgbi_device *cdev;
513         __be32 ipv4addr;
514         unsigned char port_id;
515 };
516
517 struct cxgbi_ports_map {
518         unsigned int max_connect;
519         unsigned int used;
520         unsigned short sport_base;
521         spinlock_t lock;
522         unsigned int next;
523         struct cxgbi_sock **port_csk;
524 };
525
526 #define CXGBI_FLAG_DEV_T3               0x1
527 #define CXGBI_FLAG_DEV_T4               0x2
528 #define CXGBI_FLAG_ADAPTER_RESET        0x4
529 #define CXGBI_FLAG_IPV4_SET             0x10
530 struct cxgbi_device {
531         struct list_head list_head;
532         struct list_head rcu_node;
533         unsigned int flags;
534         struct net_device **ports;
535         void *lldev;
536         struct cxgbi_hba **hbas;
537         const unsigned short *mtus;
538         unsigned char nmtus;
539         unsigned char nports;
540         struct pci_dev *pdev;
541         struct dentry *debugfs_root;
542         struct iscsi_transport *itp;
543
544         unsigned int pfvf;
545         unsigned int rx_credit_thres;
546         unsigned int skb_tx_rsvd;
547         unsigned int skb_rx_extra;      /* for msg coalesced mode */
548         unsigned int tx_max_size;
549         unsigned int rx_max_size;
550         struct cxgbi_ports_map pmap;
551         struct cxgbi_tag_format tag_format;
552         struct cxgbi_ddp_info *ddp;
553
554         void (*dev_ddp_cleanup)(struct cxgbi_device *);
555         int (*csk_ddp_set)(struct cxgbi_sock *, struct cxgbi_pagepod_hdr *,
556                                 unsigned int, unsigned int,
557                                 struct cxgbi_gather_list *);
558         void (*csk_ddp_clear)(struct cxgbi_hba *,
559                                 unsigned int, unsigned int, unsigned int);
560         int (*csk_ddp_setup_digest)(struct cxgbi_sock *,
561                                 unsigned int, int, int, int);
562         int (*csk_ddp_setup_pgidx)(struct cxgbi_sock *,
563                                 unsigned int, int, bool);
564
565         void (*csk_release_offload_resources)(struct cxgbi_sock *);
566         int (*csk_rx_pdu_ready)(struct cxgbi_sock *, struct sk_buff *);
567         u32 (*csk_send_rx_credits)(struct cxgbi_sock *, u32);
568         int (*csk_push_tx_frames)(struct cxgbi_sock *, int);
569         void (*csk_send_abort_req)(struct cxgbi_sock *);
570         void (*csk_send_close_req)(struct cxgbi_sock *);
571         int (*csk_alloc_cpls)(struct cxgbi_sock *);
572         int (*csk_init_act_open)(struct cxgbi_sock *);
573
574         void *dd_data;
575 };
576 #define cxgbi_cdev_priv(cdev)   ((cdev)->dd_data)
577
578 struct cxgbi_conn {
579         struct cxgbi_endpoint *cep;
580         struct iscsi_conn *iconn;
581         struct cxgbi_hba *chba;
582         u32 task_idx_bits;
583 };
584
585 struct cxgbi_endpoint {
586         struct cxgbi_conn *cconn;
587         struct cxgbi_hba *chba;
588         struct cxgbi_sock *csk;
589 };
590
591 #define MAX_PDU_FRAGS   ((ULP2_MAX_PDU_PAYLOAD + 512 - 1) / 512)
592 struct cxgbi_task_data {
593         unsigned short nr_frags;
594         struct page_frag frags[MAX_PDU_FRAGS];
595         struct sk_buff *skb;
596         unsigned int offset;
597         unsigned int count;
598         unsigned int sgoffset;
599 };
600 #define iscsi_task_cxgbi_data(task) \
601         ((task)->dd_data + sizeof(struct iscsi_tcp_task))
602
603 static inline int cxgbi_is_ddp_tag(struct cxgbi_tag_format *tformat, u32 tag)
604 {
605         return !(tag & (1 << (tformat->rsvd_bits + tformat->rsvd_shift - 1)));
606 }
607
608 static inline int cxgbi_sw_tag_usable(struct cxgbi_tag_format *tformat,
609                                         u32 sw_tag)
610 {
611         sw_tag >>= (32 - tformat->rsvd_bits);
612         return !sw_tag;
613 }
614
615 static inline u32 cxgbi_set_non_ddp_tag(struct cxgbi_tag_format *tformat,
616                                         u32 sw_tag)
617 {
618         unsigned char shift = tformat->rsvd_bits + tformat->rsvd_shift - 1;
619         u32 mask = (1 << shift) - 1;
620
621         if (sw_tag && (sw_tag & ~mask)) {
622                 u32 v1 = sw_tag & ((1 << shift) - 1);
623                 u32 v2 = (sw_tag >> (shift - 1)) << shift;
624
625                 return v2 | v1 | 1 << shift;
626         }
627
628         return sw_tag | 1 << shift;
629 }
630
631 static inline u32 cxgbi_ddp_tag_base(struct cxgbi_tag_format *tformat,
632                                         u32 sw_tag)
633 {
634         u32 mask = (1 << tformat->rsvd_shift) - 1;
635
636         if (sw_tag && (sw_tag & ~mask)) {
637                 u32 v1 = sw_tag & mask;
638                 u32 v2 = sw_tag >> tformat->rsvd_shift;
639
640                 v2 <<= tformat->rsvd_bits + tformat->rsvd_shift;
641
642                 return v2 | v1;
643         }
644
645         return sw_tag;
646 }
647
648 static inline u32 cxgbi_tag_rsvd_bits(struct cxgbi_tag_format *tformat,
649                                         u32 tag)
650 {
651         if (cxgbi_is_ddp_tag(tformat, tag))
652                 return (tag >> tformat->rsvd_shift) & tformat->rsvd_mask;
653
654         return 0;
655 }
656
657 static inline u32 cxgbi_tag_nonrsvd_bits(struct cxgbi_tag_format *tformat,
658                                         u32 tag)
659 {
660         unsigned char shift = tformat->rsvd_bits + tformat->rsvd_shift - 1;
661         u32 v1, v2;
662
663         if (cxgbi_is_ddp_tag(tformat, tag)) {
664                 v1 = tag & ((1 << tformat->rsvd_shift) - 1);
665                 v2 = (tag >> (shift + 1)) << tformat->rsvd_shift;
666         } else {
667                 u32 mask = (1 << shift) - 1;
668                 tag &= ~(1 << shift);
669                 v1 = tag & mask;
670                 v2 = (tag >> 1) & ~mask;
671         }
672         return v1 | v2;
673 }
674
675 static inline void *cxgbi_alloc_big_mem(unsigned int size,
676                                         gfp_t gfp)
677 {
678         void *p = kzalloc(size, gfp | __GFP_NOWARN);
679
680         if (!p)
681                 p = vzalloc(size);
682
683         return p;
684 }
685
686 static inline void cxgbi_free_big_mem(void *addr)
687 {
688         kvfree(addr);
689 }
690
691 static inline void cxgbi_set_iscsi_ipv4(struct cxgbi_hba *chba, __be32 ipaddr)
692 {
693         if (chba->cdev->flags & CXGBI_FLAG_IPV4_SET)
694                 chba->ipv4addr = ipaddr;
695         else
696                 pr_info("set iscsi ipv4 NOT supported, using %s ipv4.\n",
697                         chba->ndev->name);
698 }
699
700 struct cxgbi_device *cxgbi_device_register(unsigned int, unsigned int);
701 void cxgbi_device_unregister(struct cxgbi_device *);
702 void cxgbi_device_unregister_all(unsigned int flag);
703 struct cxgbi_device *cxgbi_device_find_by_lldev(void *);
704 struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *, int *);
705 struct cxgbi_device *cxgbi_device_find_by_netdev_rcu(struct net_device *,
706                                                      int *);
707 int cxgbi_hbas_add(struct cxgbi_device *, u64, unsigned int,
708                         struct scsi_host_template *,
709                         struct scsi_transport_template *);
710 void cxgbi_hbas_remove(struct cxgbi_device *);
711
712 int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
713                         unsigned int max_conn);
714 void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev);
715
716 void cxgbi_conn_tx_open(struct cxgbi_sock *);
717 void cxgbi_conn_pdu_ready(struct cxgbi_sock *);
718 int cxgbi_conn_alloc_pdu(struct iscsi_task *, u8);
719 int cxgbi_conn_init_pdu(struct iscsi_task *, unsigned int , unsigned int);
720 int cxgbi_conn_xmit_pdu(struct iscsi_task *);
721
722 void cxgbi_cleanup_task(struct iscsi_task *task);
723
724 umode_t cxgbi_attr_is_visible(int param_type, int param);
725 void cxgbi_get_conn_stats(struct iscsi_cls_conn *, struct iscsi_stats *);
726 int cxgbi_set_conn_param(struct iscsi_cls_conn *,
727                         enum iscsi_param, char *, int);
728 int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param, char *);
729 struct iscsi_cls_conn *cxgbi_create_conn(struct iscsi_cls_session *, u32);
730 int cxgbi_bind_conn(struct iscsi_cls_session *,
731                         struct iscsi_cls_conn *, u64, int);
732 void cxgbi_destroy_session(struct iscsi_cls_session *);
733 struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *,
734                         u16, u16, u32);
735 int cxgbi_set_host_param(struct Scsi_Host *,
736                         enum iscsi_host_param, char *, int);
737 int cxgbi_get_host_param(struct Scsi_Host *, enum iscsi_host_param, char *);
738 struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *,
739                         struct sockaddr *, int);
740 int cxgbi_ep_poll(struct iscsi_endpoint *, int);
741 void cxgbi_ep_disconnect(struct iscsi_endpoint *);
742
743 int cxgbi_iscsi_init(struct iscsi_transport *,
744                         struct scsi_transport_template **);
745 void cxgbi_iscsi_cleanup(struct iscsi_transport *,
746                         struct scsi_transport_template **);
747 void cxgbi_parse_pdu_itt(struct iscsi_conn *, itt_t, int *, int *);
748 int cxgbi_ddp_init(struct cxgbi_device *, unsigned int, unsigned int,
749                         unsigned int, unsigned int);
750 int cxgbi_ddp_cleanup(struct cxgbi_device *);
751 void cxgbi_ddp_page_size_factor(int *);
752 void cxgbi_ddp_ppod_clear(struct cxgbi_pagepod *);
753 void cxgbi_ddp_ppod_set(struct cxgbi_pagepod *, struct cxgbi_pagepod_hdr *,
754                         struct cxgbi_gather_list *, unsigned int);
755 #endif  /*__LIBCXGBI_H__*/