]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/ixgbe/ixgbe_fcoe.c
Merge branch 'fix/asoc' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[mv-sheeva.git] / drivers / net / ixgbe / ixgbe_fcoe.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2010 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26 *******************************************************************************/
27
28 #include "ixgbe.h"
29 #ifdef CONFIG_IXGBE_DCB
30 #include "ixgbe_dcb_82599.h"
31 #endif /* CONFIG_IXGBE_DCB */
32 #include <linux/if_ether.h>
33 #include <linux/gfp.h>
34 #include <linux/if_vlan.h>
35 #include <scsi/scsi_cmnd.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/fc/fc_fs.h>
38 #include <scsi/fc/fc_fcoe.h>
39 #include <scsi/libfc.h>
40 #include <scsi/libfcoe.h>
41
42 /**
43  * ixgbe_rx_is_fcoe - check the rx desc for incoming pkt type
44  * @rx_desc: advanced rx descriptor
45  *
46  * Returns : true if it is FCoE pkt
47  */
48 static inline bool ixgbe_rx_is_fcoe(union ixgbe_adv_rx_desc *rx_desc)
49 {
50         u16 p;
51
52         p = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info);
53         if (p & IXGBE_RXDADV_PKTTYPE_ETQF) {
54                 p &= IXGBE_RXDADV_PKTTYPE_ETQF_MASK;
55                 p >>= IXGBE_RXDADV_PKTTYPE_ETQF_SHIFT;
56                 return p == IXGBE_ETQF_FILTER_FCOE;
57         }
58         return false;
59 }
60
61 /**
62  * ixgbe_fcoe_clear_ddp - clear the given ddp context
63  * @ddp - ptr to the ixgbe_fcoe_ddp
64  *
65  * Returns : none
66  *
67  */
68 static inline void ixgbe_fcoe_clear_ddp(struct ixgbe_fcoe_ddp *ddp)
69 {
70         ddp->len = 0;
71         ddp->err = 1;
72         ddp->udl = NULL;
73         ddp->udp = 0UL;
74         ddp->sgl = NULL;
75         ddp->sgc = 0;
76 }
77
78 /**
79  * ixgbe_fcoe_ddp_put - free the ddp context for a given xid
80  * @netdev: the corresponding net_device
81  * @xid: the xid that corresponding ddp will be freed
82  *
83  * This is the implementation of net_device_ops.ndo_fcoe_ddp_done
84  * and it is expected to be called by ULD, i.e., FCP layer of libfc
85  * to release the corresponding ddp context when the I/O is done.
86  *
87  * Returns : data length already ddp-ed in bytes
88  */
89 int ixgbe_fcoe_ddp_put(struct net_device *netdev, u16 xid)
90 {
91         int len = 0;
92         struct ixgbe_fcoe *fcoe;
93         struct ixgbe_adapter *adapter;
94         struct ixgbe_fcoe_ddp *ddp;
95         u32 fcbuff;
96
97         if (!netdev)
98                 goto out_ddp_put;
99
100         if (xid >= IXGBE_FCOE_DDP_MAX)
101                 goto out_ddp_put;
102
103         adapter = netdev_priv(netdev);
104         fcoe = &adapter->fcoe;
105         ddp = &fcoe->ddp[xid];
106         if (!ddp->udl)
107                 goto out_ddp_put;
108
109         len = ddp->len;
110         /* if there an error, force to invalidate ddp context */
111         if (ddp->err) {
112                 spin_lock_bh(&fcoe->lock);
113                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCFLT, 0);
114                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCFLTRW,
115                                 (xid | IXGBE_FCFLTRW_WE));
116                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCBUFF, 0);
117                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCDMARW,
118                                 (xid | IXGBE_FCDMARW_WE));
119
120                 /* guaranteed to be invalidated after 100us */
121                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCDMARW,
122                                 (xid | IXGBE_FCDMARW_RE));
123                 fcbuff = IXGBE_READ_REG(&adapter->hw, IXGBE_FCBUFF);
124                 spin_unlock_bh(&fcoe->lock);
125                 if (fcbuff & IXGBE_FCBUFF_VALID)
126                         udelay(100);
127         }
128         if (ddp->sgl)
129                 pci_unmap_sg(adapter->pdev, ddp->sgl, ddp->sgc,
130                              DMA_FROM_DEVICE);
131         pci_pool_free(fcoe->pool, ddp->udl, ddp->udp);
132         ixgbe_fcoe_clear_ddp(ddp);
133
134 out_ddp_put:
135         return len;
136 }
137
138 /**
139  * ixgbe_fcoe_ddp_get - called to set up ddp context
140  * @netdev: the corresponding net_device
141  * @xid: the exchange id requesting ddp
142  * @sgl: the scatter-gather list for this request
143  * @sgc: the number of scatter-gather items
144  *
145  * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
146  * and is expected to be called from ULD, e.g., FCP layer of libfc
147  * to set up ddp for the corresponding xid of the given sglist for
148  * the corresponding I/O.
149  *
150  * Returns : 1 for success and 0 for no ddp
151  */
152 int ixgbe_fcoe_ddp_get(struct net_device *netdev, u16 xid,
153                        struct scatterlist *sgl, unsigned int sgc)
154 {
155         struct ixgbe_adapter *adapter;
156         struct ixgbe_hw *hw;
157         struct ixgbe_fcoe *fcoe;
158         struct ixgbe_fcoe_ddp *ddp;
159         struct scatterlist *sg;
160         unsigned int i, j, dmacount;
161         unsigned int len;
162         static const unsigned int bufflen = IXGBE_FCBUFF_MIN;
163         unsigned int firstoff = 0;
164         unsigned int lastsize;
165         unsigned int thisoff = 0;
166         unsigned int thislen = 0;
167         u32 fcbuff, fcdmarw, fcfltrw;
168         dma_addr_t addr = 0;
169
170         if (!netdev || !sgl)
171                 return 0;
172
173         adapter = netdev_priv(netdev);
174         if (xid >= IXGBE_FCOE_DDP_MAX) {
175                 e_warn(drv, "xid=0x%x out-of-range\n", xid);
176                 return 0;
177         }
178
179         /* no DDP if we are already down or resetting */
180         if (test_bit(__IXGBE_DOWN, &adapter->state) ||
181             test_bit(__IXGBE_RESETTING, &adapter->state))
182                 return 0;
183
184         fcoe = &adapter->fcoe;
185         if (!fcoe->pool) {
186                 e_warn(drv, "xid=0x%x no ddp pool for fcoe\n", xid);
187                 return 0;
188         }
189
190         ddp = &fcoe->ddp[xid];
191         if (ddp->sgl) {
192                 e_err(drv, "xid 0x%x w/ non-null sgl=%p nents=%d\n",
193                       xid, ddp->sgl, ddp->sgc);
194                 return 0;
195         }
196         ixgbe_fcoe_clear_ddp(ddp);
197
198         /* setup dma from scsi command sgl */
199         dmacount = pci_map_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
200         if (dmacount == 0) {
201                 e_err(drv, "xid 0x%x DMA map error\n", xid);
202                 return 0;
203         }
204
205         /* alloc the udl from our ddp pool */
206         ddp->udl = pci_pool_alloc(fcoe->pool, GFP_ATOMIC, &ddp->udp);
207         if (!ddp->udl) {
208                 e_err(drv, "failed allocated ddp context\n");
209                 goto out_noddp_unmap;
210         }
211         ddp->sgl = sgl;
212         ddp->sgc = sgc;
213
214         j = 0;
215         for_each_sg(sgl, sg, dmacount, i) {
216                 addr = sg_dma_address(sg);
217                 len = sg_dma_len(sg);
218                 while (len) {
219                         /* max number of buffers allowed in one DDP context */
220                         if (j >= IXGBE_BUFFCNT_MAX) {
221                                 e_err(drv, "xid=%x:%d,%d,%d:addr=%llx "
222                                       "not enough descriptors\n",
223                                       xid, i, j, dmacount, (u64)addr);
224                                 goto out_noddp_free;
225                         }
226
227                         /* get the offset of length of current buffer */
228                         thisoff = addr & ((dma_addr_t)bufflen - 1);
229                         thislen = min((bufflen - thisoff), len);
230                         /*
231                          * all but the 1st buffer (j == 0)
232                          * must be aligned on bufflen
233                          */
234                         if ((j != 0) && (thisoff))
235                                 goto out_noddp_free;
236                         /*
237                          * all but the last buffer
238                          * ((i == (dmacount - 1)) && (thislen == len))
239                          * must end at bufflen
240                          */
241                         if (((i != (dmacount - 1)) || (thislen != len))
242                             && ((thislen + thisoff) != bufflen))
243                                 goto out_noddp_free;
244
245                         ddp->udl[j] = (u64)(addr - thisoff);
246                         /* only the first buffer may have none-zero offset */
247                         if (j == 0)
248                                 firstoff = thisoff;
249                         len -= thislen;
250                         addr += thislen;
251                         j++;
252                 }
253         }
254         /* only the last buffer may have non-full bufflen */
255         lastsize = thisoff + thislen;
256
257         /*
258          * lastsize can not be buffer len.
259          * If it is then adding another buffer with lastsize = 1.
260          */
261         if (lastsize == bufflen) {
262                 if (j >= IXGBE_BUFFCNT_MAX) {
263                         e_err(drv, "xid=%x:%d,%d,%d:addr=%llx "
264                                 "not enough user buffers. We need an extra "
265                                 "buffer because lastsize is bufflen.\n",
266                                 xid, i, j, dmacount, (u64)addr);
267                         goto out_noddp_free;
268                 }
269
270                 ddp->udl[j] = (u64)(fcoe->extra_ddp_buffer_dma);
271                 j++;
272                 lastsize = 1;
273         }
274
275         fcbuff = (IXGBE_FCBUFF_4KB << IXGBE_FCBUFF_BUFFSIZE_SHIFT);
276         fcbuff |= ((j & 0xff) << IXGBE_FCBUFF_BUFFCNT_SHIFT);
277         fcbuff |= (firstoff << IXGBE_FCBUFF_OFFSET_SHIFT);
278         fcbuff |= (IXGBE_FCBUFF_VALID);
279
280         fcdmarw = xid;
281         fcdmarw |= IXGBE_FCDMARW_WE;
282         fcdmarw |= (lastsize << IXGBE_FCDMARW_LASTSIZE_SHIFT);
283
284         fcfltrw = xid;
285         fcfltrw |= IXGBE_FCFLTRW_WE;
286
287         /* program DMA context */
288         hw = &adapter->hw;
289         spin_lock_bh(&fcoe->lock);
290         IXGBE_WRITE_REG(hw, IXGBE_FCPTRL, ddp->udp & DMA_BIT_MASK(32));
291         IXGBE_WRITE_REG(hw, IXGBE_FCPTRH, (u64)ddp->udp >> 32);
292         IXGBE_WRITE_REG(hw, IXGBE_FCBUFF, fcbuff);
293         IXGBE_WRITE_REG(hw, IXGBE_FCDMARW, fcdmarw);
294         /* program filter context */
295         IXGBE_WRITE_REG(hw, IXGBE_FCPARAM, 0);
296         IXGBE_WRITE_REG(hw, IXGBE_FCFLT, IXGBE_FCFLT_VALID);
297         IXGBE_WRITE_REG(hw, IXGBE_FCFLTRW, fcfltrw);
298         spin_unlock_bh(&fcoe->lock);
299
300         return 1;
301
302 out_noddp_free:
303         pci_pool_free(fcoe->pool, ddp->udl, ddp->udp);
304         ixgbe_fcoe_clear_ddp(ddp);
305
306 out_noddp_unmap:
307         pci_unmap_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
308         return 0;
309 }
310
311 /**
312  * ixgbe_fcoe_ddp - check ddp status and mark it done
313  * @adapter: ixgbe adapter
314  * @rx_desc: advanced rx descriptor
315  * @skb: the skb holding the received data
316  *
317  * This checks ddp status.
318  *
319  * Returns : < 0 indicates an error or not a FCiE ddp, 0 indicates
320  * not passing the skb to ULD, > 0 indicates is the length of data
321  * being ddped.
322  */
323 int ixgbe_fcoe_ddp(struct ixgbe_adapter *adapter,
324                    union ixgbe_adv_rx_desc *rx_desc,
325                    struct sk_buff *skb)
326 {
327         u16 xid;
328         u32 fctl;
329         u32 sterr, fceofe, fcerr, fcstat;
330         int rc = -EINVAL;
331         struct ixgbe_fcoe *fcoe;
332         struct ixgbe_fcoe_ddp *ddp;
333         struct fc_frame_header *fh;
334
335         if (!ixgbe_rx_is_fcoe(rx_desc))
336                 goto ddp_out;
337
338         sterr = le32_to_cpu(rx_desc->wb.upper.status_error);
339         fcerr = (sterr & IXGBE_RXDADV_ERR_FCERR);
340         fceofe = (sterr & IXGBE_RXDADV_ERR_FCEOFE);
341         if (fcerr == IXGBE_FCERR_BADCRC)
342                 skb_checksum_none_assert(skb);
343         else
344                 skb->ip_summed = CHECKSUM_UNNECESSARY;
345
346         if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
347                 fh = (struct fc_frame_header *)(skb->data +
348                         sizeof(struct vlan_hdr) + sizeof(struct fcoe_hdr));
349         else
350                 fh = (struct fc_frame_header *)(skb->data +
351                         sizeof(struct fcoe_hdr));
352         fctl = ntoh24(fh->fh_f_ctl);
353         if (fctl & FC_FC_EX_CTX)
354                 xid =  be16_to_cpu(fh->fh_ox_id);
355         else
356                 xid =  be16_to_cpu(fh->fh_rx_id);
357
358         if (xid >= IXGBE_FCOE_DDP_MAX)
359                 goto ddp_out;
360
361         fcoe = &adapter->fcoe;
362         ddp = &fcoe->ddp[xid];
363         if (!ddp->udl)
364                 goto ddp_out;
365
366         ddp->err = (fcerr | fceofe);
367         if (ddp->err)
368                 goto ddp_out;
369
370         fcstat = (sterr & IXGBE_RXDADV_STAT_FCSTAT);
371         if (fcstat) {
372                 /* update length of DDPed data */
373                 ddp->len = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
374                 /* unmap the sg list when FCP_RSP is received */
375                 if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_FCPRSP) {
376                         pci_unmap_sg(adapter->pdev, ddp->sgl,
377                                      ddp->sgc, DMA_FROM_DEVICE);
378                         ddp->sgl = NULL;
379                         ddp->sgc = 0;
380                 }
381                 /* return 0 to bypass going to ULD for DDPed data */
382                 if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_DDP)
383                         rc = 0;
384                 else if (ddp->len)
385                         rc = ddp->len;
386         }
387
388 ddp_out:
389         return rc;
390 }
391
392 /**
393  * ixgbe_fso - ixgbe FCoE Sequence Offload (FSO)
394  * @adapter: ixgbe adapter
395  * @tx_ring: tx desc ring
396  * @skb: associated skb
397  * @tx_flags: tx flags
398  * @hdr_len: hdr_len to be returned
399  *
400  * This sets up large send offload for FCoE
401  *
402  * Returns : 0 indicates no FSO, > 0 for FSO, < 0 for error
403  */
404 int ixgbe_fso(struct ixgbe_adapter *adapter,
405               struct ixgbe_ring *tx_ring, struct sk_buff *skb,
406               u32 tx_flags, u8 *hdr_len)
407 {
408         u8 sof, eof;
409         u32 vlan_macip_lens;
410         u32 fcoe_sof_eof;
411         u32 type_tucmd;
412         u32 mss_l4len_idx;
413         int mss = 0;
414         unsigned int i;
415         struct ixgbe_tx_buffer *tx_buffer_info;
416         struct ixgbe_adv_tx_context_desc *context_desc;
417         struct fc_frame_header *fh;
418
419         if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE)) {
420                 e_err(drv, "Wrong gso type %d:expecting SKB_GSO_FCOE\n",
421                       skb_shinfo(skb)->gso_type);
422                 return -EINVAL;
423         }
424
425         /* resets the header to point fcoe/fc */
426         skb_set_network_header(skb, skb->mac_len);
427         skb_set_transport_header(skb, skb->mac_len +
428                                  sizeof(struct fcoe_hdr));
429
430         /* sets up SOF and ORIS */
431         fcoe_sof_eof = 0;
432         sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
433         switch (sof) {
434         case FC_SOF_I2:
435                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_ORIS;
436                 break;
437         case FC_SOF_I3:
438                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_SOF;
439                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_ORIS;
440                 break;
441         case FC_SOF_N2:
442                 break;
443         case FC_SOF_N3:
444                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_SOF;
445                 break;
446         default:
447                 e_warn(drv, "unknown sof = 0x%x\n", sof);
448                 return -EINVAL;
449         }
450
451         /* the first byte of the last dword is EOF */
452         skb_copy_bits(skb, skb->len - 4, &eof, 1);
453         /* sets up EOF and ORIE */
454         switch (eof) {
455         case FC_EOF_N:
456                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N;
457                 break;
458         case FC_EOF_T:
459                 /* lso needs ORIE */
460                 if (skb_is_gso(skb)) {
461                         fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N;
462                         fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_ORIE;
463                 } else {
464                         fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_T;
465                 }
466                 break;
467         case FC_EOF_NI:
468                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_NI;
469                 break;
470         case FC_EOF_A:
471                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_A;
472                 break;
473         default:
474                 e_warn(drv, "unknown eof = 0x%x\n", eof);
475                 return -EINVAL;
476         }
477
478         /* sets up PARINC indicating data offset */
479         fh = (struct fc_frame_header *)skb_transport_header(skb);
480         if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
481                 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_PARINC;
482
483         /* hdr_len includes fc_hdr if FCoE lso is enabled */
484         *hdr_len = sizeof(struct fcoe_crc_eof);
485         if (skb_is_gso(skb))
486                 *hdr_len += (skb_transport_offset(skb) +
487                              sizeof(struct fc_frame_header));
488         /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
489         vlan_macip_lens = (skb_transport_offset(skb) +
490                           sizeof(struct fc_frame_header));
491         vlan_macip_lens |= ((skb_transport_offset(skb) - 4)
492                            << IXGBE_ADVTXD_MACLEN_SHIFT);
493         vlan_macip_lens |= (tx_flags & IXGBE_TX_FLAGS_VLAN_MASK);
494
495         /* type_tycmd and mss: set TUCMD.FCoE to enable offload */
496         type_tucmd = IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT |
497                      IXGBE_ADVTXT_TUCMD_FCOE;
498         if (skb_is_gso(skb))
499                 mss = skb_shinfo(skb)->gso_size;
500         /* mss_l4len_id: use 1 for FSO as TSO, no need for L4LEN */
501         mss_l4len_idx = (mss << IXGBE_ADVTXD_MSS_SHIFT) |
502                         (1 << IXGBE_ADVTXD_IDX_SHIFT);
503
504         /* write context desc */
505         i = tx_ring->next_to_use;
506         context_desc = IXGBE_TX_CTXTDESC_ADV(tx_ring, i);
507         context_desc->vlan_macip_lens   = cpu_to_le32(vlan_macip_lens);
508         context_desc->seqnum_seed       = cpu_to_le32(fcoe_sof_eof);
509         context_desc->type_tucmd_mlhl   = cpu_to_le32(type_tucmd);
510         context_desc->mss_l4len_idx     = cpu_to_le32(mss_l4len_idx);
511
512         tx_buffer_info = &tx_ring->tx_buffer_info[i];
513         tx_buffer_info->time_stamp = jiffies;
514         tx_buffer_info->next_to_watch = i;
515
516         i++;
517         if (i == tx_ring->count)
518                 i = 0;
519         tx_ring->next_to_use = i;
520
521         return skb_is_gso(skb);
522 }
523
524 /**
525  * ixgbe_configure_fcoe - configures registers for fcoe at start
526  * @adapter: ptr to ixgbe adapter
527  *
528  * This sets up FCoE related registers
529  *
530  * Returns : none
531  */
532 void ixgbe_configure_fcoe(struct ixgbe_adapter *adapter)
533 {
534         int i, fcoe_q, fcoe_i;
535         struct ixgbe_hw *hw = &adapter->hw;
536         struct ixgbe_fcoe *fcoe = &adapter->fcoe;
537         struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
538 #ifdef CONFIG_IXGBE_DCB
539         u8 tc;
540         u32 up2tc;
541 #endif
542
543         /* create the pool for ddp if not created yet */
544         if (!fcoe->pool) {
545                 /* allocate ddp pool */
546                 fcoe->pool = pci_pool_create("ixgbe_fcoe_ddp",
547                                              adapter->pdev, IXGBE_FCPTR_MAX,
548                                              IXGBE_FCPTR_ALIGN, PAGE_SIZE);
549                 if (!fcoe->pool)
550                         e_err(drv, "failed to allocated FCoE DDP pool\n");
551
552                 spin_lock_init(&fcoe->lock);
553
554                 /* Extra buffer to be shared by all DDPs for HW work around */
555                 fcoe->extra_ddp_buffer = kmalloc(IXGBE_FCBUFF_MIN, GFP_ATOMIC);
556                 if (fcoe->extra_ddp_buffer == NULL) {
557                         e_err(drv, "failed to allocated extra DDP buffer\n");
558                         goto out_extra_ddp_buffer_alloc;
559                 }
560
561                 fcoe->extra_ddp_buffer_dma =
562                         dma_map_single(&adapter->pdev->dev,
563                                        fcoe->extra_ddp_buffer,
564                                        IXGBE_FCBUFF_MIN,
565                                        DMA_FROM_DEVICE);
566                 if (dma_mapping_error(&adapter->pdev->dev,
567                                       fcoe->extra_ddp_buffer_dma)) {
568                         e_err(drv, "failed to map extra DDP buffer\n");
569                         goto out_extra_ddp_buffer_dma;
570                 }
571         }
572
573         /* Enable L2 eth type filter for FCoE */
574         IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FCOE),
575                         (ETH_P_FCOE | IXGBE_ETQF_FCOE | IXGBE_ETQF_FILTER_EN));
576         /* Enable L2 eth type filter for FIP */
577         IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FIP),
578                         (ETH_P_FIP | IXGBE_ETQF_FILTER_EN));
579         if (adapter->ring_feature[RING_F_FCOE].indices) {
580                 /* Use multiple rx queues for FCoE by redirection table */
581                 for (i = 0; i < IXGBE_FCRETA_SIZE; i++) {
582                         fcoe_i = f->mask + i % f->indices;
583                         fcoe_i &= IXGBE_FCRETA_ENTRY_MASK;
584                         fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
585                         IXGBE_WRITE_REG(hw, IXGBE_FCRETA(i), fcoe_q);
586                 }
587                 IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, IXGBE_FCRECTL_ENA);
588                 IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE), 0);
589         } else  {
590                 /* Use single rx queue for FCoE */
591                 fcoe_i = f->mask;
592                 fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
593                 IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, 0);
594                 IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE),
595                                 IXGBE_ETQS_QUEUE_EN |
596                                 (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
597         }
598         /* send FIP frames to the first FCoE queue */
599         fcoe_i = f->mask;
600         fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
601         IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FIP),
602                         IXGBE_ETQS_QUEUE_EN |
603                         (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
604
605         IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL,
606                         IXGBE_FCRXCTRL_FCOELLI |
607                         IXGBE_FCRXCTRL_FCCRCBO |
608                         (FC_FCOE_VER << IXGBE_FCRXCTRL_FCOEVER_SHIFT));
609 #ifdef CONFIG_IXGBE_DCB
610         up2tc = IXGBE_READ_REG(&adapter->hw, IXGBE_RTTUP2TC);
611         for (i = 0; i < MAX_USER_PRIORITY; i++) {
612                 tc = (u8)(up2tc >> (i * IXGBE_RTTUP2TC_UP_SHIFT));
613                 tc &= (MAX_TRAFFIC_CLASS - 1);
614                 if (fcoe->tc == tc) {
615                         fcoe->up = i;
616                         break;
617                 }
618         }
619 #endif
620
621         return;
622
623 out_extra_ddp_buffer_dma:
624         kfree(fcoe->extra_ddp_buffer);
625 out_extra_ddp_buffer_alloc:
626         pci_pool_destroy(fcoe->pool);
627         fcoe->pool = NULL;
628 }
629
630 /**
631  * ixgbe_cleanup_fcoe - release all fcoe ddp context resources
632  * @adapter : ixgbe adapter
633  *
634  * Cleans up outstanding ddp context resources
635  *
636  * Returns : none
637  */
638 void ixgbe_cleanup_fcoe(struct ixgbe_adapter *adapter)
639 {
640         int i;
641         struct ixgbe_fcoe *fcoe = &adapter->fcoe;
642
643         /* release ddp resource */
644         if (fcoe->pool) {
645                 for (i = 0; i < IXGBE_FCOE_DDP_MAX; i++)
646                         ixgbe_fcoe_ddp_put(adapter->netdev, i);
647                 dma_unmap_single(&adapter->pdev->dev,
648                                  fcoe->extra_ddp_buffer_dma,
649                                  IXGBE_FCBUFF_MIN,
650                                  DMA_FROM_DEVICE);
651                 kfree(fcoe->extra_ddp_buffer);
652                 pci_pool_destroy(fcoe->pool);
653                 fcoe->pool = NULL;
654         }
655 }
656
657 /**
658  * ixgbe_fcoe_enable - turn on FCoE offload feature
659  * @netdev: the corresponding netdev
660  *
661  * Turns on FCoE offload feature in 82599.
662  *
663  * Returns : 0 indicates success or -EINVAL on failure
664  */
665 int ixgbe_fcoe_enable(struct net_device *netdev)
666 {
667         int rc = -EINVAL;
668         struct ixgbe_adapter *adapter = netdev_priv(netdev);
669         struct ixgbe_fcoe *fcoe = &adapter->fcoe;
670
671
672         if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
673                 goto out_enable;
674
675         atomic_inc(&fcoe->refcnt);
676         if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
677                 goto out_enable;
678
679         e_info(drv, "Enabling FCoE offload features.\n");
680         if (netif_running(netdev))
681                 netdev->netdev_ops->ndo_stop(netdev);
682
683         ixgbe_clear_interrupt_scheme(adapter);
684
685         adapter->flags |= IXGBE_FLAG_FCOE_ENABLED;
686         adapter->ring_feature[RING_F_FCOE].indices = IXGBE_FCRETA_SIZE;
687         netdev->features |= NETIF_F_FCOE_CRC;
688         netdev->features |= NETIF_F_FSO;
689         netdev->features |= NETIF_F_FCOE_MTU;
690         netdev->fcoe_ddp_xid = IXGBE_FCOE_DDP_MAX - 1;
691
692         ixgbe_init_interrupt_scheme(adapter);
693         netdev_features_change(netdev);
694
695         if (netif_running(netdev))
696                 netdev->netdev_ops->ndo_open(netdev);
697         rc = 0;
698
699 out_enable:
700         return rc;
701 }
702
703 /**
704  * ixgbe_fcoe_disable - turn off FCoE offload feature
705  * @netdev: the corresponding netdev
706  *
707  * Turns off FCoE offload feature in 82599.
708  *
709  * Returns : 0 indicates success or -EINVAL on failure
710  */
711 int ixgbe_fcoe_disable(struct net_device *netdev)
712 {
713         int rc = -EINVAL;
714         struct ixgbe_adapter *adapter = netdev_priv(netdev);
715         struct ixgbe_fcoe *fcoe = &adapter->fcoe;
716
717         if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
718                 goto out_disable;
719
720         if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
721                 goto out_disable;
722
723         if (!atomic_dec_and_test(&fcoe->refcnt))
724                 goto out_disable;
725
726         e_info(drv, "Disabling FCoE offload features.\n");
727         netdev->features &= ~NETIF_F_FCOE_CRC;
728         netdev->features &= ~NETIF_F_FSO;
729         netdev->features &= ~NETIF_F_FCOE_MTU;
730         netdev->fcoe_ddp_xid = 0;
731         netdev_features_change(netdev);
732
733         if (netif_running(netdev))
734                 netdev->netdev_ops->ndo_stop(netdev);
735
736         ixgbe_clear_interrupt_scheme(adapter);
737         adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
738         adapter->ring_feature[RING_F_FCOE].indices = 0;
739         ixgbe_cleanup_fcoe(adapter);
740         ixgbe_init_interrupt_scheme(adapter);
741
742         if (netif_running(netdev))
743                 netdev->netdev_ops->ndo_open(netdev);
744         rc = 0;
745
746 out_disable:
747         return rc;
748 }
749
750 #ifdef CONFIG_IXGBE_DCB
751 /**
752  * ixgbe_fcoe_getapp - retrieves current user priority bitmap for FCoE
753  * @adapter : ixgbe adapter
754  *
755  * Finds out the corresponding user priority bitmap from the current
756  * traffic class that FCoE belongs to. Returns 0 as the invalid user
757  * priority bitmap to indicate an error.
758  *
759  * Returns : 802.1p user priority bitmap for FCoE
760  */
761 u8 ixgbe_fcoe_getapp(struct ixgbe_adapter *adapter)
762 {
763         return 1 << adapter->fcoe.up;
764 }
765
766 /**
767  * ixgbe_fcoe_setapp - sets the user priority bitmap for FCoE
768  * @adapter : ixgbe adapter
769  * @up : 802.1p user priority bitmap
770  *
771  * Finds out the traffic class from the input user priority
772  * bitmap for FCoE.
773  *
774  * Returns : 0 on success otherwise returns 1 on error
775  */
776 u8 ixgbe_fcoe_setapp(struct ixgbe_adapter *adapter, u8 up)
777 {
778         int i;
779         u32 up2tc;
780
781         /* valid user priority bitmap must not be 0 */
782         if (up) {
783                 /* from user priority to the corresponding traffic class */
784                 up2tc = IXGBE_READ_REG(&adapter->hw, IXGBE_RTTUP2TC);
785                 for (i = 0; i < MAX_USER_PRIORITY; i++) {
786                         if (up & (1 << i)) {
787                                 up2tc >>= (i * IXGBE_RTTUP2TC_UP_SHIFT);
788                                 up2tc &= (MAX_TRAFFIC_CLASS - 1);
789                                 adapter->fcoe.tc = (u8)up2tc;
790                                 adapter->fcoe.up = i;
791                                 return 0;
792                         }
793                 }
794         }
795
796         return 1;
797 }
798 #endif /* CONFIG_IXGBE_DCB */
799
800 /**
801  * ixgbe_fcoe_get_wwn - get world wide name for the node or the port
802  * @netdev : ixgbe adapter
803  * @wwn : the world wide name
804  * @type: the type of world wide name
805  *
806  * Returns the node or port world wide name if both the prefix and the san
807  * mac address are valid, then the wwn is formed based on the NAA-2 for
808  * IEEE Extended name identifier (ref. to T10 FC-LS Spec., Sec. 15.3).
809  *
810  * Returns : 0 on success
811  */
812 int ixgbe_fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
813 {
814         int rc = -EINVAL;
815         u16 prefix = 0xffff;
816         struct ixgbe_adapter *adapter = netdev_priv(netdev);
817         struct ixgbe_mac_info *mac = &adapter->hw.mac;
818
819         switch (type) {
820         case NETDEV_FCOE_WWNN:
821                 prefix = mac->wwnn_prefix;
822                 break;
823         case NETDEV_FCOE_WWPN:
824                 prefix = mac->wwpn_prefix;
825                 break;
826         default:
827                 break;
828         }
829
830         if ((prefix != 0xffff) &&
831             is_valid_ether_addr(mac->san_addr)) {
832                 *wwn = ((u64) prefix << 48) |
833                        ((u64) mac->san_addr[0] << 40) |
834                        ((u64) mac->san_addr[1] << 32) |
835                        ((u64) mac->san_addr[2] << 24) |
836                        ((u64) mac->san_addr[3] << 16) |
837                        ((u64) mac->san_addr[4] << 8)  |
838                        ((u64) mac->san_addr[5]);
839                 rc = 0;
840         }
841         return rc;
842 }
843
844