blob: f0c1018bbf31c31fbf95b4ca923de181d2965884 [file] [log] [blame]
Yi Zoud3a2ae62009-05-13 13:10:21 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmorea52055e2011-02-23 09:58:39 +00004 Copyright(c) 1999 - 2011 Intel Corporation.
Yi Zoud3a2ae62009-05-13 13:10:21 +00005
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
Yi Zoud3a2ae62009-05-13 13:10:21 +000028#include "ixgbe.h"
29#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Yi Zoube5d5072010-05-18 16:00:05 +000031#include <linux/if_vlan.h>
Yi Zoud3a2ae62009-05-13 13:10:21 +000032#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/fc/fc_fs.h>
35#include <scsi/fc/fc_fcoe.h>
36#include <scsi/libfc.h>
37#include <scsi/libfcoe.h>
38
39/**
Yi Zoud0ed8932009-05-13 13:11:29 +000040 * ixgbe_rx_is_fcoe - check the rx desc for incoming pkt type
41 * @rx_desc: advanced rx descriptor
42 *
43 * Returns : true if it is FCoE pkt
44 */
45static inline bool ixgbe_rx_is_fcoe(union ixgbe_adv_rx_desc *rx_desc)
46{
47 u16 p;
48
49 p = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info);
50 if (p & IXGBE_RXDADV_PKTTYPE_ETQF) {
51 p &= IXGBE_RXDADV_PKTTYPE_ETQF_MASK;
52 p >>= IXGBE_RXDADV_PKTTYPE_ETQF_SHIFT;
53 return p == IXGBE_ETQF_FILTER_FCOE;
54 }
55 return false;
56}
57
58/**
59 * ixgbe_fcoe_clear_ddp - clear the given ddp context
60 * @ddp - ptr to the ixgbe_fcoe_ddp
61 *
62 * Returns : none
63 *
64 */
65static inline void ixgbe_fcoe_clear_ddp(struct ixgbe_fcoe_ddp *ddp)
66{
67 ddp->len = 0;
Yi Zou8ca371e2010-11-16 19:27:13 -080068 ddp->err = 1;
Yi Zoud0ed8932009-05-13 13:11:29 +000069 ddp->udl = NULL;
70 ddp->udp = 0UL;
71 ddp->sgl = NULL;
72 ddp->sgc = 0;
73}
74
75/**
76 * ixgbe_fcoe_ddp_put - free the ddp context for a given xid
77 * @netdev: the corresponding net_device
78 * @xid: the xid that corresponding ddp will be freed
79 *
80 * This is the implementation of net_device_ops.ndo_fcoe_ddp_done
81 * and it is expected to be called by ULD, i.e., FCP layer of libfc
82 * to release the corresponding ddp context when the I/O is done.
83 *
84 * Returns : data length already ddp-ed in bytes
85 */
86int ixgbe_fcoe_ddp_put(struct net_device *netdev, u16 xid)
87{
88 int len = 0;
89 struct ixgbe_fcoe *fcoe;
90 struct ixgbe_adapter *adapter;
91 struct ixgbe_fcoe_ddp *ddp;
Yi Zou9b55bb02010-11-16 19:27:14 -080092 u32 fcbuff;
Yi Zoud0ed8932009-05-13 13:11:29 +000093
94 if (!netdev)
95 goto out_ddp_put;
96
97 if (xid >= IXGBE_FCOE_DDP_MAX)
98 goto out_ddp_put;
99
100 adapter = netdev_priv(netdev);
101 fcoe = &adapter->fcoe;
102 ddp = &fcoe->ddp[xid];
103 if (!ddp->udl)
104 goto out_ddp_put;
105
106 len = ddp->len;
107 /* if there an error, force to invalidate ddp context */
108 if (ddp->err) {
109 spin_lock_bh(&fcoe->lock);
110 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCFLT, 0);
111 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCFLTRW,
112 (xid | IXGBE_FCFLTRW_WE));
113 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCBUFF, 0);
114 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCDMARW,
115 (xid | IXGBE_FCDMARW_WE));
Yi Zou9b55bb02010-11-16 19:27:14 -0800116
117 /* guaranteed to be invalidated after 100us */
118 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCDMARW,
119 (xid | IXGBE_FCDMARW_RE));
120 fcbuff = IXGBE_READ_REG(&adapter->hw, IXGBE_FCBUFF);
Yi Zoud0ed8932009-05-13 13:11:29 +0000121 spin_unlock_bh(&fcoe->lock);
Yi Zou9b55bb02010-11-16 19:27:14 -0800122 if (fcbuff & IXGBE_FCBUFF_VALID)
123 udelay(100);
Yi Zoud0ed8932009-05-13 13:11:29 +0000124 }
125 if (ddp->sgl)
126 pci_unmap_sg(adapter->pdev, ddp->sgl, ddp->sgc,
127 DMA_FROM_DEVICE);
Vasu Devdadbe852011-05-11 05:41:46 +0000128 if (ddp->pool) {
129 pci_pool_free(ddp->pool, ddp->udl, ddp->udp);
130 ddp->pool = NULL;
131 }
132
Yi Zoud0ed8932009-05-13 13:11:29 +0000133 ixgbe_fcoe_clear_ddp(ddp);
134
135out_ddp_put:
136 return len;
137}
138
Yi Zou68a683c2011-02-01 07:22:16 +0000139
Yi Zoud0ed8932009-05-13 13:11:29 +0000140/**
Yi Zou68a683c2011-02-01 07:22:16 +0000141 * ixgbe_fcoe_ddp_setup - called to set up ddp context
Yi Zoud0ed8932009-05-13 13:11:29 +0000142 * @netdev: the corresponding net_device
143 * @xid: the exchange id requesting ddp
144 * @sgl: the scatter-gather list for this request
145 * @sgc: the number of scatter-gather items
146 *
Yi Zoud0ed8932009-05-13 13:11:29 +0000147 * Returns : 1 for success and 0 for no ddp
148 */
Yi Zou68a683c2011-02-01 07:22:16 +0000149static int ixgbe_fcoe_ddp_setup(struct net_device *netdev, u16 xid,
150 struct scatterlist *sgl, unsigned int sgc,
151 int target_mode)
Yi Zoud0ed8932009-05-13 13:11:29 +0000152{
153 struct ixgbe_adapter *adapter;
154 struct ixgbe_hw *hw;
155 struct ixgbe_fcoe *fcoe;
156 struct ixgbe_fcoe_ddp *ddp;
157 struct scatterlist *sg;
158 unsigned int i, j, dmacount;
159 unsigned int len;
Amir Hananiac6006362011-02-15 09:11:31 +0000160 static const unsigned int bufflen = IXGBE_FCBUFF_MIN;
Yi Zoud0ed8932009-05-13 13:11:29 +0000161 unsigned int firstoff = 0;
162 unsigned int lastsize;
163 unsigned int thisoff = 0;
164 unsigned int thislen = 0;
Yi Zou68a683c2011-02-01 07:22:16 +0000165 u32 fcbuff, fcdmarw, fcfltrw, fcrxctl;
Don Skidmorefbbea322011-01-26 06:04:17 +0000166 dma_addr_t addr = 0;
Vasu Devdadbe852011-05-11 05:41:46 +0000167 struct pci_pool *pool;
Yi Zoud0ed8932009-05-13 13:11:29 +0000168
169 if (!netdev || !sgl)
170 return 0;
171
172 adapter = netdev_priv(netdev);
173 if (xid >= IXGBE_FCOE_DDP_MAX) {
Emil Tantilov396e7992010-07-01 20:05:12 +0000174 e_warn(drv, "xid=0x%x out-of-range\n", xid);
Yi Zoud0ed8932009-05-13 13:11:29 +0000175 return 0;
176 }
177
Yi Zoua41c0592010-11-16 19:27:13 -0800178 /* no DDP if we are already down or resetting */
179 if (test_bit(__IXGBE_DOWN, &adapter->state) ||
180 test_bit(__IXGBE_RESETTING, &adapter->state))
181 return 0;
182
Yi Zoud0ed8932009-05-13 13:11:29 +0000183 fcoe = &adapter->fcoe;
184 if (!fcoe->pool) {
Emil Tantilov396e7992010-07-01 20:05:12 +0000185 e_warn(drv, "xid=0x%x no ddp pool for fcoe\n", xid);
Yi Zoud0ed8932009-05-13 13:11:29 +0000186 return 0;
187 }
188
189 ddp = &fcoe->ddp[xid];
190 if (ddp->sgl) {
Emil Tantilov396e7992010-07-01 20:05:12 +0000191 e_err(drv, "xid 0x%x w/ non-null sgl=%p nents=%d\n",
192 xid, ddp->sgl, ddp->sgc);
Yi Zoud0ed8932009-05-13 13:11:29 +0000193 return 0;
194 }
195 ixgbe_fcoe_clear_ddp(ddp);
196
197 /* setup dma from scsi command sgl */
198 dmacount = pci_map_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
199 if (dmacount == 0) {
Emil Tantilov396e7992010-07-01 20:05:12 +0000200 e_err(drv, "xid 0x%x DMA map error\n", xid);
Yi Zoud0ed8932009-05-13 13:11:29 +0000201 return 0;
202 }
203
Vasu Devdadbe852011-05-11 05:41:46 +0000204 /* alloc the udl from per cpu ddp pool */
205 pool = *per_cpu_ptr(fcoe->pool, get_cpu());
206 ddp->udl = pci_pool_alloc(pool, GFP_ATOMIC, &ddp->udp);
Yi Zoud0ed8932009-05-13 13:11:29 +0000207 if (!ddp->udl) {
Emil Tantilov396e7992010-07-01 20:05:12 +0000208 e_err(drv, "failed allocated ddp context\n");
Yi Zoud0ed8932009-05-13 13:11:29 +0000209 goto out_noddp_unmap;
210 }
Vasu Devdadbe852011-05-11 05:41:46 +0000211 ddp->pool = pool;
Yi Zoud0ed8932009-05-13 13:11:29 +0000212 ddp->sgl = sgl;
213 ddp->sgc = sgc;
214
215 j = 0;
216 for_each_sg(sgl, sg, dmacount, i) {
217 addr = sg_dma_address(sg);
218 len = sg_dma_len(sg);
219 while (len) {
Robert Lovea7551b72010-03-24 10:02:04 +0000220 /* max number of buffers allowed in one DDP context */
221 if (j >= IXGBE_BUFFCNT_MAX) {
Emil Tantilov396e7992010-07-01 20:05:12 +0000222 e_err(drv, "xid=%x:%d,%d,%d:addr=%llx "
Emil Tantilov849c4542010-06-03 16:53:41 +0000223 "not enough descriptors\n",
224 xid, i, j, dmacount, (u64)addr);
Robert Lovea7551b72010-03-24 10:02:04 +0000225 goto out_noddp_free;
226 }
227
Yi Zoud0ed8932009-05-13 13:11:29 +0000228 /* get the offset of length of current buffer */
229 thisoff = addr & ((dma_addr_t)bufflen - 1);
230 thislen = min((bufflen - thisoff), len);
231 /*
232 * all but the 1st buffer (j == 0)
233 * must be aligned on bufflen
234 */
235 if ((j != 0) && (thisoff))
236 goto out_noddp_free;
237 /*
238 * all but the last buffer
239 * ((i == (dmacount - 1)) && (thislen == len))
240 * must end at bufflen
241 */
242 if (((i != (dmacount - 1)) || (thislen != len))
243 && ((thislen + thisoff) != bufflen))
244 goto out_noddp_free;
245
246 ddp->udl[j] = (u64)(addr - thisoff);
247 /* only the first buffer may have none-zero offset */
248 if (j == 0)
249 firstoff = thisoff;
250 len -= thislen;
251 addr += thislen;
252 j++;
Yi Zoud0ed8932009-05-13 13:11:29 +0000253 }
254 }
255 /* only the last buffer may have non-full bufflen */
256 lastsize = thisoff + thislen;
257
Amir Hananiac6006362011-02-15 09:11:31 +0000258 /*
259 * lastsize can not be buffer len.
260 * If it is then adding another buffer with lastsize = 1.
261 */
262 if (lastsize == bufflen) {
263 if (j >= IXGBE_BUFFCNT_MAX) {
264 e_err(drv, "xid=%x:%d,%d,%d:addr=%llx "
265 "not enough user buffers. We need an extra "
266 "buffer because lastsize is bufflen.\n",
267 xid, i, j, dmacount, (u64)addr);
268 goto out_noddp_free;
269 }
270
271 ddp->udl[j] = (u64)(fcoe->extra_ddp_buffer_dma);
272 j++;
273 lastsize = 1;
274 }
Vasu Devdadbe852011-05-11 05:41:46 +0000275 put_cpu();
Amir Hananiac6006362011-02-15 09:11:31 +0000276
Yi Zoud0ed8932009-05-13 13:11:29 +0000277 fcbuff = (IXGBE_FCBUFF_4KB << IXGBE_FCBUFF_BUFFSIZE_SHIFT);
Robert Lovea7551b72010-03-24 10:02:04 +0000278 fcbuff |= ((j & 0xff) << IXGBE_FCBUFF_BUFFCNT_SHIFT);
Yi Zoud0ed8932009-05-13 13:11:29 +0000279 fcbuff |= (firstoff << IXGBE_FCBUFF_OFFSET_SHIFT);
Yi Zou68a683c2011-02-01 07:22:16 +0000280 /* Set WRCONTX bit to allow DDP for target */
281 if (target_mode)
282 fcbuff |= (IXGBE_FCBUFF_WRCONTX);
Yi Zoud0ed8932009-05-13 13:11:29 +0000283 fcbuff |= (IXGBE_FCBUFF_VALID);
284
285 fcdmarw = xid;
286 fcdmarw |= IXGBE_FCDMARW_WE;
287 fcdmarw |= (lastsize << IXGBE_FCDMARW_LASTSIZE_SHIFT);
288
289 fcfltrw = xid;
290 fcfltrw |= IXGBE_FCFLTRW_WE;
291
292 /* program DMA context */
293 hw = &adapter->hw;
294 spin_lock_bh(&fcoe->lock);
Yi Zou68a683c2011-02-01 07:22:16 +0000295
296 /* turn on last frame indication for target mode as FCP_RSPtarget is
297 * supposed to send FCP_RSP when it is done. */
298 if (target_mode && !test_bit(__IXGBE_FCOE_TARGET, &fcoe->mode)) {
299 set_bit(__IXGBE_FCOE_TARGET, &fcoe->mode);
300 fcrxctl = IXGBE_READ_REG(hw, IXGBE_FCRXCTRL);
301 fcrxctl |= IXGBE_FCRXCTRL_LASTSEQH;
302 IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL, fcrxctl);
303 }
304
Andrew Morton8e20ce92009-06-18 16:49:17 -0700305 IXGBE_WRITE_REG(hw, IXGBE_FCPTRL, ddp->udp & DMA_BIT_MASK(32));
Yi Zoud0ed8932009-05-13 13:11:29 +0000306 IXGBE_WRITE_REG(hw, IXGBE_FCPTRH, (u64)ddp->udp >> 32);
307 IXGBE_WRITE_REG(hw, IXGBE_FCBUFF, fcbuff);
308 IXGBE_WRITE_REG(hw, IXGBE_FCDMARW, fcdmarw);
309 /* program filter context */
310 IXGBE_WRITE_REG(hw, IXGBE_FCPARAM, 0);
311 IXGBE_WRITE_REG(hw, IXGBE_FCFLT, IXGBE_FCFLT_VALID);
312 IXGBE_WRITE_REG(hw, IXGBE_FCFLTRW, fcfltrw);
Yi Zou68a683c2011-02-01 07:22:16 +0000313
Yi Zoud0ed8932009-05-13 13:11:29 +0000314 spin_unlock_bh(&fcoe->lock);
315
316 return 1;
317
318out_noddp_free:
Vasu Devdadbe852011-05-11 05:41:46 +0000319 pci_pool_free(pool, ddp->udl, ddp->udp);
Yi Zoud0ed8932009-05-13 13:11:29 +0000320 ixgbe_fcoe_clear_ddp(ddp);
321
322out_noddp_unmap:
323 pci_unmap_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
Vasu Devdadbe852011-05-11 05:41:46 +0000324 put_cpu();
Yi Zoud0ed8932009-05-13 13:11:29 +0000325 return 0;
326}
327
328/**
Yi Zou68a683c2011-02-01 07:22:16 +0000329 * ixgbe_fcoe_ddp_get - called to set up ddp context in initiator mode
330 * @netdev: the corresponding net_device
331 * @xid: the exchange id requesting ddp
332 * @sgl: the scatter-gather list for this request
333 * @sgc: the number of scatter-gather items
334 *
335 * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
336 * and is expected to be called from ULD, e.g., FCP layer of libfc
337 * to set up ddp for the corresponding xid of the given sglist for
338 * the corresponding I/O.
339 *
340 * Returns : 1 for success and 0 for no ddp
341 */
342int ixgbe_fcoe_ddp_get(struct net_device *netdev, u16 xid,
343 struct scatterlist *sgl, unsigned int sgc)
344{
345 return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 0);
346}
347
348/**
349 * ixgbe_fcoe_ddp_target - called to set up ddp context in target mode
350 * @netdev: the corresponding net_device
351 * @xid: the exchange id requesting ddp
352 * @sgl: the scatter-gather list for this request
353 * @sgc: the number of scatter-gather items
354 *
355 * This is the implementation of net_device_ops.ndo_fcoe_ddp_target
356 * and is expected to be called from ULD, e.g., FCP layer of libfc
357 * to set up ddp for the corresponding xid of the given sglist for
358 * the corresponding I/O. The DDP in target mode is a write I/O request
359 * from the initiator.
360 *
361 * Returns : 1 for success and 0 for no ddp
362 */
363int ixgbe_fcoe_ddp_target(struct net_device *netdev, u16 xid,
364 struct scatterlist *sgl, unsigned int sgc)
365{
366 return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 1);
367}
368
369/**
Yi Zoud0ed8932009-05-13 13:11:29 +0000370 * ixgbe_fcoe_ddp - check ddp status and mark it done
371 * @adapter: ixgbe adapter
372 * @rx_desc: advanced rx descriptor
373 * @skb: the skb holding the received data
374 *
375 * This checks ddp status.
376 *
Yi Zou3d8fd382009-06-08 14:38:44 +0000377 * Returns : < 0 indicates an error or not a FCiE ddp, 0 indicates
378 * not passing the skb to ULD, > 0 indicates is the length of data
379 * being ddped.
Yi Zoud0ed8932009-05-13 13:11:29 +0000380 */
381int ixgbe_fcoe_ddp(struct ixgbe_adapter *adapter,
382 union ixgbe_adv_rx_desc *rx_desc,
383 struct sk_buff *skb)
384{
385 u16 xid;
Yi Zoud4ab8812009-09-03 14:56:31 +0000386 u32 fctl;
Yi Zoud0ed8932009-05-13 13:11:29 +0000387 u32 sterr, fceofe, fcerr, fcstat;
388 int rc = -EINVAL;
389 struct ixgbe_fcoe *fcoe;
390 struct ixgbe_fcoe_ddp *ddp;
391 struct fc_frame_header *fh;
Yi Zou68a683c2011-02-01 07:22:16 +0000392 struct fcoe_crc_eof *crc;
Yi Zoud0ed8932009-05-13 13:11:29 +0000393
394 if (!ixgbe_rx_is_fcoe(rx_desc))
395 goto ddp_out;
396
Yi Zoud0ed8932009-05-13 13:11:29 +0000397 sterr = le32_to_cpu(rx_desc->wb.upper.status_error);
398 fcerr = (sterr & IXGBE_RXDADV_ERR_FCERR);
399 fceofe = (sterr & IXGBE_RXDADV_ERR_FCEOFE);
400 if (fcerr == IXGBE_FCERR_BADCRC)
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700401 skb_checksum_none_assert(skb);
402 else
403 skb->ip_summed = CHECKSUM_UNNECESSARY;
Yi Zoud0ed8932009-05-13 13:11:29 +0000404
Yi Zoube5d5072010-05-18 16:00:05 +0000405 if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
406 fh = (struct fc_frame_header *)(skb->data +
407 sizeof(struct vlan_hdr) + sizeof(struct fcoe_hdr));
408 else
409 fh = (struct fc_frame_header *)(skb->data +
410 sizeof(struct fcoe_hdr));
Yi Zoud4ab8812009-09-03 14:56:31 +0000411 fctl = ntoh24(fh->fh_f_ctl);
412 if (fctl & FC_FC_EX_CTX)
413 xid = be16_to_cpu(fh->fh_ox_id);
414 else
415 xid = be16_to_cpu(fh->fh_rx_id);
416
Yi Zoud0ed8932009-05-13 13:11:29 +0000417 if (xid >= IXGBE_FCOE_DDP_MAX)
418 goto ddp_out;
419
420 fcoe = &adapter->fcoe;
421 ddp = &fcoe->ddp[xid];
422 if (!ddp->udl)
423 goto ddp_out;
424
Yi Zou7aba7b02011-04-09 08:34:12 +0000425 if (fcerr | fceofe)
Yi Zoud0ed8932009-05-13 13:11:29 +0000426 goto ddp_out;
427
428 fcstat = (sterr & IXGBE_RXDADV_STAT_FCSTAT);
429 if (fcstat) {
430 /* update length of DDPed data */
431 ddp->len = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
432 /* unmap the sg list when FCP_RSP is received */
433 if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_FCPRSP) {
434 pci_unmap_sg(adapter->pdev, ddp->sgl,
435 ddp->sgc, DMA_FROM_DEVICE);
Yi Zou7aba7b02011-04-09 08:34:12 +0000436 ddp->err = (fcerr | fceofe);
Yi Zoud0ed8932009-05-13 13:11:29 +0000437 ddp->sgl = NULL;
438 ddp->sgc = 0;
439 }
440 /* return 0 to bypass going to ULD for DDPed data */
441 if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_DDP)
442 rc = 0;
Yi Zou17e78b02009-08-13 14:09:58 +0000443 else if (ddp->len)
Yi Zou3d8fd382009-06-08 14:38:44 +0000444 rc = ddp->len;
Yi Zoud0ed8932009-05-13 13:11:29 +0000445 }
Yi Zou68a683c2011-02-01 07:22:16 +0000446 /* In target mode, check the last data frame of the sequence.
447 * For DDP in target mode, data is already DDPed but the header
448 * indication of the last data frame ould allow is to tell if we
449 * got all the data and the ULP can send FCP_RSP back, as this is
450 * not a full fcoe frame, we fill the trailer here so it won't be
451 * dropped by the ULP stack.
452 */
453 if ((fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA) &&
454 (fctl & FC_FC_END_SEQ)) {
455 crc = (struct fcoe_crc_eof *)skb_put(skb, sizeof(*crc));
456 crc->fcoe_eof = FC_EOF_T;
457 }
Yi Zoud0ed8932009-05-13 13:11:29 +0000458ddp_out:
459 return rc;
460}
461
462/**
Yi Zoubc079222009-05-13 13:10:44 +0000463 * ixgbe_fso - ixgbe FCoE Sequence Offload (FSO)
464 * @adapter: ixgbe adapter
465 * @tx_ring: tx desc ring
466 * @skb: associated skb
467 * @tx_flags: tx flags
468 * @hdr_len: hdr_len to be returned
469 *
470 * This sets up large send offload for FCoE
471 *
472 * Returns : 0 indicates no FSO, > 0 for FSO, < 0 for error
473 */
Alexander Duyck897ab152011-05-27 05:31:47 +0000474int ixgbe_fso(struct ixgbe_ring *tx_ring, struct sk_buff *skb,
Yi Zoubc079222009-05-13 13:10:44 +0000475 u32 tx_flags, u8 *hdr_len)
476{
Yi Zoubc079222009-05-13 13:10:44 +0000477 struct fc_frame_header *fh;
Alexander Duyck897ab152011-05-27 05:31:47 +0000478 u32 vlan_macip_lens;
479 u32 fcoe_sof_eof = 0;
480 u32 mss_l4len_idx;
481 u8 sof, eof;
Yi Zoubc079222009-05-13 13:10:44 +0000482
483 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE)) {
Alexander Duyck897ab152011-05-27 05:31:47 +0000484 dev_err(tx_ring->dev, "Wrong gso type %d:expecting SKB_GSO_FCOE\n",
485 skb_shinfo(skb)->gso_type);
Yi Zoubc079222009-05-13 13:10:44 +0000486 return -EINVAL;
487 }
488
489 /* resets the header to point fcoe/fc */
490 skb_set_network_header(skb, skb->mac_len);
491 skb_set_transport_header(skb, skb->mac_len +
492 sizeof(struct fcoe_hdr));
493
494 /* sets up SOF and ORIS */
Yi Zoubc079222009-05-13 13:10:44 +0000495 sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
496 switch (sof) {
497 case FC_SOF_I2:
Alexander Duyck897ab152011-05-27 05:31:47 +0000498 fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_ORIS;
Yi Zoubc079222009-05-13 13:10:44 +0000499 break;
500 case FC_SOF_I3:
Alexander Duyck897ab152011-05-27 05:31:47 +0000501 fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF |
502 IXGBE_ADVTXD_FCOEF_ORIS;
Yi Zoubc079222009-05-13 13:10:44 +0000503 break;
504 case FC_SOF_N2:
505 break;
506 case FC_SOF_N3:
Alexander Duyck897ab152011-05-27 05:31:47 +0000507 fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF;
Yi Zoubc079222009-05-13 13:10:44 +0000508 break;
509 default:
Alexander Duyck897ab152011-05-27 05:31:47 +0000510 dev_warn(tx_ring->dev, "unknown sof = 0x%x\n", sof);
Yi Zoubc079222009-05-13 13:10:44 +0000511 return -EINVAL;
512 }
513
514 /* the first byte of the last dword is EOF */
515 skb_copy_bits(skb, skb->len - 4, &eof, 1);
516 /* sets up EOF and ORIE */
517 switch (eof) {
518 case FC_EOF_N:
519 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N;
520 break;
521 case FC_EOF_T:
522 /* lso needs ORIE */
Alexander Duyck897ab152011-05-27 05:31:47 +0000523 if (skb_is_gso(skb))
524 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N |
525 IXGBE_ADVTXD_FCOEF_ORIE;
526 else
Yi Zoubc079222009-05-13 13:10:44 +0000527 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_T;
Yi Zoubc079222009-05-13 13:10:44 +0000528 break;
529 case FC_EOF_NI:
530 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_NI;
531 break;
532 case FC_EOF_A:
533 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_A;
534 break;
535 default:
Alexander Duyck897ab152011-05-27 05:31:47 +0000536 dev_warn(tx_ring->dev, "unknown eof = 0x%x\n", eof);
Yi Zoubc079222009-05-13 13:10:44 +0000537 return -EINVAL;
538 }
539
540 /* sets up PARINC indicating data offset */
541 fh = (struct fc_frame_header *)skb_transport_header(skb);
542 if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
543 fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_PARINC;
544
Alexander Duyck897ab152011-05-27 05:31:47 +0000545 /* include trailer in headlen as it is replicated per frame */
Yi Zoubc079222009-05-13 13:10:44 +0000546 *hdr_len = sizeof(struct fcoe_crc_eof);
Alexander Duyck897ab152011-05-27 05:31:47 +0000547
548 /* hdr_len includes fc_hdr if FCoE LSO is enabled */
Yi Zoubc079222009-05-13 13:10:44 +0000549 if (skb_is_gso(skb))
550 *hdr_len += (skb_transport_offset(skb) +
551 sizeof(struct fc_frame_header));
Yi Zoubc079222009-05-13 13:10:44 +0000552
Yi Zoubc079222009-05-13 13:10:44 +0000553 /* mss_l4len_id: use 1 for FSO as TSO, no need for L4LEN */
Alexander Duyck897ab152011-05-27 05:31:47 +0000554 mss_l4len_idx = skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
555 mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
556
557 /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
558 vlan_macip_lens = skb_transport_offset(skb) +
559 sizeof(struct fc_frame_header);
560 vlan_macip_lens |= (skb_transport_offset(skb) - 4)
561 << IXGBE_ADVTXD_MACLEN_SHIFT;
562 vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
Yi Zoubc079222009-05-13 13:10:44 +0000563
564 /* write context desc */
Alexander Duyck897ab152011-05-27 05:31:47 +0000565 ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, fcoe_sof_eof,
566 IXGBE_ADVTXT_TUCMD_FCOE, mss_l4len_idx);
Yi Zoubc079222009-05-13 13:10:44 +0000567
568 return skb_is_gso(skb);
569}
570
Vasu Devdadbe852011-05-11 05:41:46 +0000571static void ixgbe_fcoe_ddp_pools_free(struct ixgbe_fcoe *fcoe)
572{
573 unsigned int cpu;
574 struct pci_pool **pool;
575
576 for_each_possible_cpu(cpu) {
577 pool = per_cpu_ptr(fcoe->pool, cpu);
578 if (*pool)
579 pci_pool_destroy(*pool);
580 }
581 free_percpu(fcoe->pool);
582 fcoe->pool = NULL;
583}
584
585static void ixgbe_fcoe_ddp_pools_alloc(struct ixgbe_adapter *adapter)
586{
587 struct ixgbe_fcoe *fcoe = &adapter->fcoe;
588 unsigned int cpu;
589 struct pci_pool **pool;
590 char pool_name[32];
591
592 fcoe->pool = alloc_percpu(struct pci_pool *);
593 if (!fcoe->pool)
594 return;
595
596 /* allocate pci pool for each cpu */
597 for_each_possible_cpu(cpu) {
598 snprintf(pool_name, 32, "ixgbe_fcoe_ddp_%d", cpu);
599 pool = per_cpu_ptr(fcoe->pool, cpu);
600 *pool = pci_pool_create(pool_name,
601 adapter->pdev, IXGBE_FCPTR_MAX,
602 IXGBE_FCPTR_ALIGN, PAGE_SIZE);
603 if (!*pool) {
604 e_err(drv, "failed to alloc DDP pool on cpu:%d\n", cpu);
605 ixgbe_fcoe_ddp_pools_free(fcoe);
606 return;
607 }
608 }
609}
610
Yi Zoubc079222009-05-13 13:10:44 +0000611/**
Yi Zoud3a2ae62009-05-13 13:10:21 +0000612 * ixgbe_configure_fcoe - configures registers for fcoe at start
613 * @adapter: ptr to ixgbe adapter
614 *
615 * This sets up FCoE related registers
616 *
617 * Returns : none
618 */
619void ixgbe_configure_fcoe(struct ixgbe_adapter *adapter)
620{
Yi Zou29ebf6f2009-05-17 12:34:14 +0000621 int i, fcoe_q, fcoe_i;
Yi Zoud3a2ae62009-05-13 13:10:21 +0000622 struct ixgbe_hw *hw = &adapter->hw;
Yi Zoud0ed8932009-05-13 13:11:29 +0000623 struct ixgbe_fcoe *fcoe = &adapter->fcoe;
Yi Zou29ebf6f2009-05-17 12:34:14 +0000624 struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
Yi Zoud3a2ae62009-05-13 13:10:21 +0000625
Yi Zoud0ed8932009-05-13 13:11:29 +0000626 if (!fcoe->pool) {
Yi Zoud0ed8932009-05-13 13:11:29 +0000627 spin_lock_init(&fcoe->lock);
Amir Hananiac6006362011-02-15 09:11:31 +0000628
Vasu Devdadbe852011-05-11 05:41:46 +0000629 ixgbe_fcoe_ddp_pools_alloc(adapter);
630 if (!fcoe->pool) {
631 e_err(drv, "failed to alloc percpu fcoe DDP pools\n");
632 return;
633 }
634
Amir Hananiac6006362011-02-15 09:11:31 +0000635 /* Extra buffer to be shared by all DDPs for HW work around */
636 fcoe->extra_ddp_buffer = kmalloc(IXGBE_FCBUFF_MIN, GFP_ATOMIC);
637 if (fcoe->extra_ddp_buffer == NULL) {
638 e_err(drv, "failed to allocated extra DDP buffer\n");
Vasu Devdadbe852011-05-11 05:41:46 +0000639 goto out_ddp_pools;
Amir Hananiac6006362011-02-15 09:11:31 +0000640 }
641
642 fcoe->extra_ddp_buffer_dma =
643 dma_map_single(&adapter->pdev->dev,
644 fcoe->extra_ddp_buffer,
645 IXGBE_FCBUFF_MIN,
646 DMA_FROM_DEVICE);
647 if (dma_mapping_error(&adapter->pdev->dev,
648 fcoe->extra_ddp_buffer_dma)) {
649 e_err(drv, "failed to map extra DDP buffer\n");
Vasu Devdadbe852011-05-11 05:41:46 +0000650 goto out_extra_ddp_buffer;
Amir Hananiac6006362011-02-15 09:11:31 +0000651 }
Yi Zoud0ed8932009-05-13 13:11:29 +0000652 }
Yi Zou29ebf6f2009-05-17 12:34:14 +0000653
654 /* Enable L2 eth type filter for FCoE */
Yi Zoud3a2ae62009-05-13 13:10:21 +0000655 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FCOE),
656 (ETH_P_FCOE | IXGBE_ETQF_FCOE | IXGBE_ETQF_FILTER_EN));
Chris Leechaf063932010-03-24 12:45:21 +0000657 /* Enable L2 eth type filter for FIP */
658 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FIP),
659 (ETH_P_FIP | IXGBE_ETQF_FILTER_EN));
Yi Zou29ebf6f2009-05-17 12:34:14 +0000660 if (adapter->ring_feature[RING_F_FCOE].indices) {
661 /* Use multiple rx queues for FCoE by redirection table */
662 for (i = 0; i < IXGBE_FCRETA_SIZE; i++) {
663 fcoe_i = f->mask + i % f->indices;
664 fcoe_i &= IXGBE_FCRETA_ENTRY_MASK;
PJ Waskiewicz4a0b9ca2010-02-03 14:19:12 +0000665 fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
Yi Zou29ebf6f2009-05-17 12:34:14 +0000666 IXGBE_WRITE_REG(hw, IXGBE_FCRETA(i), fcoe_q);
667 }
668 IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, IXGBE_FCRECTL_ENA);
669 IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE), 0);
670 } else {
671 /* Use single rx queue for FCoE */
672 fcoe_i = f->mask;
PJ Waskiewicz4a0b9ca2010-02-03 14:19:12 +0000673 fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
Yi Zou29ebf6f2009-05-17 12:34:14 +0000674 IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, 0);
675 IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE),
676 IXGBE_ETQS_QUEUE_EN |
677 (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
678 }
Chris Leechaf063932010-03-24 12:45:21 +0000679 /* send FIP frames to the first FCoE queue */
680 fcoe_i = f->mask;
681 fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
682 IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FIP),
683 IXGBE_ETQS_QUEUE_EN |
684 (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
Yi Zou29ebf6f2009-05-17 12:34:14 +0000685
Yi Zoud3a2ae62009-05-13 13:10:21 +0000686 IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL,
687 IXGBE_FCRXCTRL_FCOELLI |
688 IXGBE_FCRXCTRL_FCCRCBO |
689 (FC_FCOE_VER << IXGBE_FCRXCTRL_FCOEVER_SHIFT));
Amir Hananiac6006362011-02-15 09:11:31 +0000690 return;
691
Vasu Devdadbe852011-05-11 05:41:46 +0000692out_extra_ddp_buffer:
Amir Hananiac6006362011-02-15 09:11:31 +0000693 kfree(fcoe->extra_ddp_buffer);
Vasu Devdadbe852011-05-11 05:41:46 +0000694out_ddp_pools:
695 ixgbe_fcoe_ddp_pools_free(fcoe);
Yi Zoud3a2ae62009-05-13 13:10:21 +0000696}
Yi Zoud0ed8932009-05-13 13:11:29 +0000697
698/**
699 * ixgbe_cleanup_fcoe - release all fcoe ddp context resources
700 * @adapter : ixgbe adapter
701 *
702 * Cleans up outstanding ddp context resources
703 *
704 * Returns : none
705 */
706void ixgbe_cleanup_fcoe(struct ixgbe_adapter *adapter)
707{
708 int i;
709 struct ixgbe_fcoe *fcoe = &adapter->fcoe;
710
Vasu Devdadbe852011-05-11 05:41:46 +0000711 if (!fcoe->pool)
712 return;
713
714 for (i = 0; i < IXGBE_FCOE_DDP_MAX; i++)
715 ixgbe_fcoe_ddp_put(adapter->netdev, i);
716 dma_unmap_single(&adapter->pdev->dev,
717 fcoe->extra_ddp_buffer_dma,
718 IXGBE_FCBUFF_MIN,
719 DMA_FROM_DEVICE);
720 kfree(fcoe->extra_ddp_buffer);
721 ixgbe_fcoe_ddp_pools_free(fcoe);
Yi Zoud0ed8932009-05-13 13:11:29 +0000722}
Yi Zou8450ff82009-08-31 12:32:14 +0000723
724/**
725 * ixgbe_fcoe_enable - turn on FCoE offload feature
726 * @netdev: the corresponding netdev
727 *
728 * Turns on FCoE offload feature in 82599.
729 *
730 * Returns : 0 indicates success or -EINVAL on failure
731 */
732int ixgbe_fcoe_enable(struct net_device *netdev)
733{
734 int rc = -EINVAL;
735 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Yi Zou27ab7602010-10-20 23:00:30 +0000736 struct ixgbe_fcoe *fcoe = &adapter->fcoe;
Yi Zou8450ff82009-08-31 12:32:14 +0000737
738
739 if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
740 goto out_enable;
741
Yi Zou27ab7602010-10-20 23:00:30 +0000742 atomic_inc(&fcoe->refcnt);
Yi Zou8450ff82009-08-31 12:32:14 +0000743 if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
744 goto out_enable;
745
Emil Tantilov396e7992010-07-01 20:05:12 +0000746 e_info(drv, "Enabling FCoE offload features.\n");
Yi Zou8450ff82009-08-31 12:32:14 +0000747 if (netif_running(netdev))
748 netdev->netdev_ops->ndo_stop(netdev);
749
750 ixgbe_clear_interrupt_scheme(adapter);
751
752 adapter->flags |= IXGBE_FLAG_FCOE_ENABLED;
753 adapter->ring_feature[RING_F_FCOE].indices = IXGBE_FCRETA_SIZE;
754 netdev->features |= NETIF_F_FCOE_CRC;
755 netdev->features |= NETIF_F_FSO;
756 netdev->features |= NETIF_F_FCOE_MTU;
Yi Zou8450ff82009-08-31 12:32:14 +0000757 netdev->fcoe_ddp_xid = IXGBE_FCOE_DDP_MAX - 1;
Yi Zou8450ff82009-08-31 12:32:14 +0000758
759 ixgbe_init_interrupt_scheme(adapter);
Vasu Dev936332b2010-03-19 04:33:10 +0000760 netdev_features_change(netdev);
Yi Zou8450ff82009-08-31 12:32:14 +0000761
762 if (netif_running(netdev))
763 netdev->netdev_ops->ndo_open(netdev);
764 rc = 0;
765
766out_enable:
767 return rc;
768}
769
770/**
771 * ixgbe_fcoe_disable - turn off FCoE offload feature
772 * @netdev: the corresponding netdev
773 *
774 * Turns off FCoE offload feature in 82599.
775 *
776 * Returns : 0 indicates success or -EINVAL on failure
777 */
778int ixgbe_fcoe_disable(struct net_device *netdev)
779{
780 int rc = -EINVAL;
781 struct ixgbe_adapter *adapter = netdev_priv(netdev);
Yi Zou27ab7602010-10-20 23:00:30 +0000782 struct ixgbe_fcoe *fcoe = &adapter->fcoe;
Yi Zou8450ff82009-08-31 12:32:14 +0000783
784 if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
785 goto out_disable;
786
787 if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
788 goto out_disable;
789
Yi Zou27ab7602010-10-20 23:00:30 +0000790 if (!atomic_dec_and_test(&fcoe->refcnt))
791 goto out_disable;
792
Emil Tantilov396e7992010-07-01 20:05:12 +0000793 e_info(drv, "Disabling FCoE offload features.\n");
Yi Zou5e09d7f2010-07-19 13:59:52 +0000794 netdev->features &= ~NETIF_F_FCOE_CRC;
795 netdev->features &= ~NETIF_F_FSO;
796 netdev->features &= ~NETIF_F_FCOE_MTU;
797 netdev->fcoe_ddp_xid = 0;
798 netdev_features_change(netdev);
799
Yi Zou8450ff82009-08-31 12:32:14 +0000800 if (netif_running(netdev))
801 netdev->netdev_ops->ndo_stop(netdev);
802
803 ixgbe_clear_interrupt_scheme(adapter);
Yi Zou8450ff82009-08-31 12:32:14 +0000804 adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
805 adapter->ring_feature[RING_F_FCOE].indices = 0;
Yi Zou8450ff82009-08-31 12:32:14 +0000806 ixgbe_cleanup_fcoe(adapter);
Yi Zou8450ff82009-08-31 12:32:14 +0000807 ixgbe_init_interrupt_scheme(adapter);
Vasu Dev936332b2010-03-19 04:33:10 +0000808
Yi Zou8450ff82009-08-31 12:32:14 +0000809 if (netif_running(netdev))
810 netdev->netdev_ops->ndo_open(netdev);
811 rc = 0;
812
813out_disable:
814 return rc;
815}
Yi Zou6ee16522009-08-31 12:34:28 +0000816
Yi Zou61a1fa12009-10-28 18:24:56 +0000817/**
818 * ixgbe_fcoe_get_wwn - get world wide name for the node or the port
819 * @netdev : ixgbe adapter
820 * @wwn : the world wide name
821 * @type: the type of world wide name
822 *
823 * Returns the node or port world wide name if both the prefix and the san
824 * mac address are valid, then the wwn is formed based on the NAA-2 for
825 * IEEE Extended name identifier (ref. to T10 FC-LS Spec., Sec. 15.3).
826 *
827 * Returns : 0 on success
828 */
829int ixgbe_fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
830{
831 int rc = -EINVAL;
832 u16 prefix = 0xffff;
833 struct ixgbe_adapter *adapter = netdev_priv(netdev);
834 struct ixgbe_mac_info *mac = &adapter->hw.mac;
835
836 switch (type) {
837 case NETDEV_FCOE_WWNN:
838 prefix = mac->wwnn_prefix;
839 break;
840 case NETDEV_FCOE_WWPN:
841 prefix = mac->wwpn_prefix;
842 break;
843 default:
844 break;
845 }
846
847 if ((prefix != 0xffff) &&
848 is_valid_ether_addr(mac->san_addr)) {
849 *wwn = ((u64) prefix << 48) |
850 ((u64) mac->san_addr[0] << 40) |
851 ((u64) mac->san_addr[1] << 32) |
852 ((u64) mac->san_addr[2] << 24) |
853 ((u64) mac->san_addr[3] << 16) |
854 ((u64) mac->san_addr[4] << 8) |
855 ((u64) mac->san_addr[5]);
856 rc = 0;
857 }
858 return rc;
859}