]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/i40e/i40e_fcoe.c
Merge branch 'next/cleanup' into for-next
[karo-tx-linux.git] / drivers / net / ethernet / intel / i40e / i40e_fcoe.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2015 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
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 #include <linux/if_ether.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/fc/fc_fs.h>
31 #include <scsi/fc/fc_fip.h>
32 #include <scsi/fc/fc_fcoe.h>
33 #include <scsi/libfc.h>
34 #include <scsi/libfcoe.h>
35 #include <uapi/linux/dcbnl.h>
36
37 #include "i40e.h"
38 #include "i40e_fcoe.h"
39
40 /**
41  * i40e_rx_is_fcoe - returns true if the rx packet type is FCoE
42  * @ptype: the packet type field from rx descriptor write-back
43  **/
44 static inline bool i40e_rx_is_fcoe(u16 ptype)
45 {
46         return (ptype >= I40E_RX_PTYPE_L2_FCOE_PAY3) &&
47                (ptype <= I40E_RX_PTYPE_L2_FCOE_VFT_FCOTHER);
48 }
49
50 /**
51  * i40e_fcoe_sof_is_class2 - returns true if this is a FC Class 2 SOF
52  * @sof: the FCoE start of frame delimiter
53  **/
54 static inline bool i40e_fcoe_sof_is_class2(u8 sof)
55 {
56         return (sof == FC_SOF_I2) || (sof == FC_SOF_N2);
57 }
58
59 /**
60  * i40e_fcoe_sof_is_class3 - returns true if this is a FC Class 3 SOF
61  * @sof: the FCoE start of frame delimiter
62  **/
63 static inline bool i40e_fcoe_sof_is_class3(u8 sof)
64 {
65         return (sof == FC_SOF_I3) || (sof == FC_SOF_N3);
66 }
67
68 /**
69  * i40e_fcoe_sof_is_supported - returns true if the FC SOF is supported by HW
70  * @sof: the input SOF value from the frame
71  **/
72 static inline bool i40e_fcoe_sof_is_supported(u8 sof)
73 {
74         return i40e_fcoe_sof_is_class2(sof) ||
75                i40e_fcoe_sof_is_class3(sof);
76 }
77
78 /**
79  * i40e_fcoe_fc_sof - pull the SOF from FCoE header in the frame
80  * @skb: the frame whose EOF is to be pulled from
81  **/
82 static inline int i40e_fcoe_fc_sof(struct sk_buff *skb, u8 *sof)
83 {
84         *sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
85
86         if (!i40e_fcoe_sof_is_supported(*sof))
87                 return -EINVAL;
88         return 0;
89 }
90
91 /**
92  * i40e_fcoe_eof_is_supported - returns true if the EOF is supported by HW
93  * @eof:     the input EOF value from the frame
94  **/
95 static inline bool i40e_fcoe_eof_is_supported(u8 eof)
96 {
97         return (eof == FC_EOF_N) || (eof == FC_EOF_T) ||
98                (eof == FC_EOF_NI) || (eof == FC_EOF_A);
99 }
100
101 /**
102  * i40e_fcoe_fc_eof - pull EOF from FCoE trailer in the frame
103  * @skb: the frame whose EOF is to be pulled from
104  **/
105 static inline int i40e_fcoe_fc_eof(struct sk_buff *skb, u8 *eof)
106 {
107         /* the first byte of the last dword is EOF */
108         skb_copy_bits(skb, skb->len - 4, eof, 1);
109
110         if (!i40e_fcoe_eof_is_supported(*eof))
111                 return -EINVAL;
112         return 0;
113 }
114
115 /**
116  * i40e_fcoe_ctxt_eof - convert input FC EOF for descriptor programming
117  * @eof: the input eof value from the frame
118  *
119  * The FC EOF is converted to the value understood by HW for descriptor
120  * programming. Never call this w/o calling i40e_fcoe_eof_is_supported()
121  * first and that already checks for all supported valid eof values.
122  **/
123 static inline u32 i40e_fcoe_ctxt_eof(u8 eof)
124 {
125         switch (eof) {
126         case FC_EOF_N:
127                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_N;
128         case FC_EOF_T:
129                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_T;
130         case FC_EOF_NI:
131                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_NI;
132         case FC_EOF_A:
133                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_A;
134         default:
135                 /* Supported valid eof shall be already checked by
136                  * calling i40e_fcoe_eof_is_supported() first,
137                  * therefore this default case shall never hit.
138                  */
139                 WARN_ON(1);
140                 return -EINVAL;
141         }
142 }
143
144 /**
145  * i40e_fcoe_xid_is_valid - returns true if the exchange id is valid
146  * @xid: the exchange id
147  **/
148 static inline bool i40e_fcoe_xid_is_valid(u16 xid)
149 {
150         return (xid != FC_XID_UNKNOWN) && (xid < I40E_FCOE_DDP_MAX);
151 }
152
153 /**
154  * i40e_fcoe_ddp_unmap - unmap the mapped sglist associated
155  * @pf: pointer to PF
156  * @ddp: sw DDP context
157  *
158  * Unmap the scatter-gather list associated with the given SW DDP context
159  *
160  * Returns: data length already ddp-ed in bytes
161  *
162  **/
163 static inline void i40e_fcoe_ddp_unmap(struct i40e_pf *pf,
164                                        struct i40e_fcoe_ddp *ddp)
165 {
166         if (test_and_set_bit(__I40E_FCOE_DDP_UNMAPPED, &ddp->flags))
167                 return;
168
169         if (ddp->sgl) {
170                 dma_unmap_sg(&pf->pdev->dev, ddp->sgl, ddp->sgc,
171                              DMA_FROM_DEVICE);
172                 ddp->sgl = NULL;
173                 ddp->sgc = 0;
174         }
175
176         if (ddp->pool) {
177                 dma_pool_free(ddp->pool, ddp->udl, ddp->udp);
178                 ddp->pool = NULL;
179         }
180 }
181
182 /**
183  * i40e_fcoe_ddp_clear - clear the given SW DDP context
184  * @ddp - SW DDP context
185  **/
186 static inline void i40e_fcoe_ddp_clear(struct i40e_fcoe_ddp *ddp)
187 {
188         memset(ddp, 0, sizeof(struct i40e_fcoe_ddp));
189         ddp->xid = FC_XID_UNKNOWN;
190         ddp->flags = __I40E_FCOE_DDP_NONE;
191 }
192
193 /**
194  * i40e_fcoe_progid_is_fcoe - check if the prog_id is for FCoE
195  * @id: the prog id for the programming status Rx descriptor write-back
196  **/
197 static inline bool i40e_fcoe_progid_is_fcoe(u8 id)
198 {
199         return (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) ||
200                (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS);
201 }
202
203 /**
204  * i40e_fcoe_fc_get_xid - get xid from the frame header
205  * @fh: the fc frame header
206  *
207  * In case the incoming frame's exchange is originated from
208  * the initiator, then received frame's exchange id is ANDed
209  * with fc_cpu_mask bits to get the same cpu on which exchange
210  * was originated, otherwise just use the current cpu.
211  *
212  * Returns ox_id if exchange originator, rx_id if responder
213  **/
214 static inline u16 i40e_fcoe_fc_get_xid(struct fc_frame_header *fh)
215 {
216         u32 f_ctl = ntoh24(fh->fh_f_ctl);
217
218         return (f_ctl & FC_FC_EX_CTX) ?
219                 be16_to_cpu(fh->fh_ox_id) :
220                 be16_to_cpu(fh->fh_rx_id);
221 }
222
223 /**
224  * i40e_fcoe_fc_frame_header - get fc frame header from skb
225  * @skb: packet
226  *
227  * This checks if there is a VLAN header and returns the data
228  * pointer to the start of the fc_frame_header.
229  *
230  * Returns pointer to the fc_frame_header
231  **/
232 static inline struct fc_frame_header *i40e_fcoe_fc_frame_header(
233         struct sk_buff *skb)
234 {
235         void *fh = skb->data + sizeof(struct fcoe_hdr);
236
237         if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
238                 fh += sizeof(struct vlan_hdr);
239
240         return (struct fc_frame_header *)fh;
241 }
242
243 /**
244  * i40e_fcoe_ddp_put - release the DDP context for a given exchange id
245  * @netdev: the corresponding net_device
246  * @xid: the exchange id that corresponding DDP context will be released
247  *
248  * This is the implementation of net_device_ops.ndo_fcoe_ddp_done
249  * and it is expected to be called by ULD, i.e., FCP layer of libfc
250  * to release the corresponding ddp context when the I/O is done.
251  *
252  * Returns : data length already ddp-ed in bytes
253  **/
254 static int i40e_fcoe_ddp_put(struct net_device *netdev, u16 xid)
255 {
256         struct i40e_netdev_priv *np = netdev_priv(netdev);
257         struct i40e_pf *pf = np->vsi->back;
258         struct i40e_fcoe *fcoe = &pf->fcoe;
259         int len = 0;
260         struct i40e_fcoe_ddp *ddp = &fcoe->ddp[xid];
261
262         if (!fcoe || !ddp)
263                 goto out;
264
265         if (test_bit(__I40E_FCOE_DDP_DONE, &ddp->flags))
266                 len = ddp->len;
267         i40e_fcoe_ddp_unmap(pf, ddp);
268 out:
269         return len;
270 }
271
272 /**
273  * i40e_fcoe_sw_init - sets up the HW for FCoE
274  * @pf: pointer to PF
275  *
276  * Returns 0 if FCoE is supported otherwise the error code
277  **/
278 int i40e_init_pf_fcoe(struct i40e_pf *pf)
279 {
280         struct i40e_hw *hw = &pf->hw;
281         u32 val;
282
283         pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
284         pf->num_fcoe_qps = 0;
285         pf->fcoe_hmc_cntx_num = 0;
286         pf->fcoe_hmc_filt_num = 0;
287
288         if (!pf->hw.func_caps.fcoe) {
289                 dev_info(&pf->pdev->dev, "FCoE capability is disabled\n");
290                 return 0;
291         }
292
293         if (!pf->hw.func_caps.dcb) {
294                 dev_warn(&pf->pdev->dev,
295                          "Hardware is not DCB capable not enabling FCoE.\n");
296                 return 0;
297         }
298
299         /* enable FCoE hash filter */
300         val = rd32(hw, I40E_PFQF_HENA(1));
301         val |= BIT(I40E_FILTER_PCTYPE_FCOE_OX - 32);
302         val |= BIT(I40E_FILTER_PCTYPE_FCOE_RX - 32);
303         val &= I40E_PFQF_HENA_PTYPE_ENA_MASK;
304         wr32(hw, I40E_PFQF_HENA(1), val);
305
306         /* enable flag */
307         pf->flags |= I40E_FLAG_FCOE_ENABLED;
308         pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
309
310         /* Reserve 4K DDP contexts and 20K filter size for FCoE */
311         pf->fcoe_hmc_cntx_num = BIT(I40E_DMA_CNTX_SIZE_4K) *
312                                 I40E_DMA_CNTX_BASE_SIZE;
313         pf->fcoe_hmc_filt_num = pf->fcoe_hmc_cntx_num +
314                                 BIT(I40E_HASH_FILTER_SIZE_16K) *
315                                 I40E_HASH_FILTER_BASE_SIZE;
316
317         /* FCoE object: max 16K filter buckets and 4K DMA contexts */
318         pf->filter_settings.fcoe_filt_num = I40E_HASH_FILTER_SIZE_16K;
319         pf->filter_settings.fcoe_cntx_num = I40E_DMA_CNTX_SIZE_4K;
320
321         /* Setup max frame with FCoE_MTU plus L2 overheads */
322         val = rd32(hw, I40E_GLFCOE_RCTL);
323         val &= ~I40E_GLFCOE_RCTL_MAX_SIZE_MASK;
324         val |= ((FCOE_MTU + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN)
325                  << I40E_GLFCOE_RCTL_MAX_SIZE_SHIFT);
326         wr32(hw, I40E_GLFCOE_RCTL, val);
327
328         dev_info(&pf->pdev->dev, "FCoE is supported.\n");
329         return 0;
330 }
331
332 /**
333  * i40e_get_fcoe_tc_map - Return TC map for FCoE APP
334  * @pf: pointer to PF
335  *
336  **/
337 u8 i40e_get_fcoe_tc_map(struct i40e_pf *pf)
338 {
339         struct i40e_dcb_app_priority_table app;
340         struct i40e_hw *hw = &pf->hw;
341         u8 enabled_tc = 0;
342         u8 tc, i;
343         /* Get the FCoE APP TLV */
344         struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
345
346         for (i = 0; i < dcbcfg->numapps; i++) {
347                 app = dcbcfg->app[i];
348                 if (app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
349                     app.protocolid == ETH_P_FCOE) {
350                         tc = dcbcfg->etscfg.prioritytable[app.priority];
351                         enabled_tc |= BIT(tc);
352                         break;
353                 }
354         }
355
356         /* TC0 if there is no TC defined for FCoE APP TLV */
357         enabled_tc = enabled_tc ? enabled_tc : 0x1;
358
359         return enabled_tc;
360 }
361
362 /**
363  * i40e_fcoe_vsi_init - prepares the VSI context for creating a FCoE VSI
364  * @vsi: pointer to the associated VSI struct
365  * @ctxt: pointer to the associated VSI context to be passed to HW
366  *
367  * Returns 0 on success or < 0 on error
368  **/
369 int i40e_fcoe_vsi_init(struct i40e_vsi *vsi, struct i40e_vsi_context *ctxt)
370 {
371         struct i40e_aqc_vsi_properties_data *info = &ctxt->info;
372         struct i40e_pf *pf = vsi->back;
373         struct i40e_hw *hw = &pf->hw;
374         u8 enabled_tc = 0;
375
376         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED)) {
377                 dev_err(&pf->pdev->dev,
378                         "FCoE is not enabled for this device\n");
379                 return -EPERM;
380         }
381
382         /* initialize the hardware for FCoE */
383         ctxt->pf_num = hw->pf_id;
384         ctxt->vf_num = 0;
385         ctxt->uplink_seid = vsi->uplink_seid;
386         ctxt->connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
387         ctxt->flags = I40E_AQ_VSI_TYPE_PF;
388
389         /* FCoE VSI would need the following sections */
390         info->valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
391
392         /* FCoE VSI does not need these sections */
393         info->valid_sections &= cpu_to_le16(~(I40E_AQ_VSI_PROP_SECURITY_VALID |
394                                             I40E_AQ_VSI_PROP_VLAN_VALID |
395                                             I40E_AQ_VSI_PROP_CAS_PV_VALID |
396                                             I40E_AQ_VSI_PROP_INGRESS_UP_VALID |
397                                             I40E_AQ_VSI_PROP_EGRESS_UP_VALID));
398
399         if (i40e_is_vsi_uplink_mode_veb(vsi)) {
400                 info->valid_sections |=
401                                 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
402                 info->switch_id =
403                                 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
404         }
405         enabled_tc = i40e_get_fcoe_tc_map(pf);
406         i40e_vsi_setup_queue_map(vsi, ctxt, enabled_tc, true);
407
408         /* set up queue option section: only enable FCoE */
409         info->queueing_opt_flags = I40E_AQ_VSI_QUE_OPT_FCOE_ENA;
410
411         return 0;
412 }
413
414 /**
415  * i40e_fcoe_enable - this is the implementation of ndo_fcoe_enable,
416  * indicating the upper FCoE protocol stack is ready to use FCoE
417  * offload features.
418  *
419  * @netdev: pointer to the netdev that FCoE is created on
420  *
421  * Returns 0 on success
422  *
423  * in RTNL
424  *
425  **/
426 int i40e_fcoe_enable(struct net_device *netdev)
427 {
428         struct i40e_netdev_priv *np = netdev_priv(netdev);
429         struct i40e_vsi *vsi = np->vsi;
430         struct i40e_pf *pf = vsi->back;
431         struct i40e_fcoe *fcoe = &pf->fcoe;
432
433         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED)) {
434                 netdev_err(netdev, "HW does not support FCoE.\n");
435                 return -ENODEV;
436         }
437
438         if (vsi->type != I40E_VSI_FCOE) {
439                 netdev_err(netdev, "interface does not support FCoE.\n");
440                 return -EBUSY;
441         }
442
443         atomic_inc(&fcoe->refcnt);
444
445         return 0;
446 }
447
448 /**
449  * i40e_fcoe_disable- disables FCoE for upper FCoE protocol stack.
450  * @dev: pointer to the netdev that FCoE is created on
451  *
452  * Returns 0 on success
453  *
454  **/
455 int i40e_fcoe_disable(struct net_device *netdev)
456 {
457         struct i40e_netdev_priv *np = netdev_priv(netdev);
458         struct i40e_vsi *vsi = np->vsi;
459         struct i40e_pf *pf = vsi->back;
460         struct i40e_fcoe *fcoe = &pf->fcoe;
461
462         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED)) {
463                 netdev_err(netdev, "device does not support FCoE\n");
464                 return -ENODEV;
465         }
466         if (vsi->type != I40E_VSI_FCOE)
467                 return -EBUSY;
468
469         if (!atomic_dec_and_test(&fcoe->refcnt))
470                 return -EINVAL;
471
472         netdev_info(netdev, "FCoE disabled\n");
473
474         return 0;
475 }
476
477 /**
478  * i40e_fcoe_dma_pool_free - free the per cpu pool for FCoE DDP
479  * @fcoe: the FCoE sw object
480  * @dev: the device that the pool is associated with
481  * @cpu: the cpu for this pool
482  *
483  **/
484 static void i40e_fcoe_dma_pool_free(struct i40e_fcoe *fcoe,
485                                     struct device *dev,
486                                     unsigned int cpu)
487 {
488         struct i40e_fcoe_ddp_pool *ddp_pool;
489
490         ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
491         if (!ddp_pool->pool) {
492                 dev_warn(dev, "DDP pool already freed for cpu %d\n", cpu);
493                 return;
494         }
495         dma_pool_destroy(ddp_pool->pool);
496         ddp_pool->pool = NULL;
497 }
498
499 /**
500  * i40e_fcoe_dma_pool_create - per cpu pool for FCoE DDP
501  * @fcoe: the FCoE sw object
502  * @dev: the device that the pool is associated with
503  * @cpu: the cpu for this pool
504  *
505  * Returns 0 on successful or non zero on failure
506  *
507  **/
508 static int i40e_fcoe_dma_pool_create(struct i40e_fcoe *fcoe,
509                                      struct device *dev,
510                                      unsigned int cpu)
511 {
512         struct i40e_fcoe_ddp_pool *ddp_pool;
513         struct dma_pool *pool;
514         char pool_name[32];
515
516         ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
517         if (ddp_pool && ddp_pool->pool) {
518                 dev_warn(dev, "DDP pool already allocated for cpu %d\n", cpu);
519                 return 0;
520         }
521         snprintf(pool_name, sizeof(pool_name), "i40e_fcoe_ddp_%d", cpu);
522         pool = dma_pool_create(pool_name, dev, I40E_FCOE_DDP_PTR_MAX,
523                                I40E_FCOE_DDP_PTR_ALIGN, PAGE_SIZE);
524         if (!pool) {
525                 dev_err(dev, "dma_pool_create %s failed\n", pool_name);
526                 return -ENOMEM;
527         }
528         ddp_pool->pool = pool;
529         return 0;
530 }
531
532 /**
533  * i40e_fcoe_free_ddp_resources - release FCoE DDP resources
534  * @vsi: the vsi FCoE is associated with
535  *
536  **/
537 void i40e_fcoe_free_ddp_resources(struct i40e_vsi *vsi)
538 {
539         struct i40e_pf *pf = vsi->back;
540         struct i40e_fcoe *fcoe = &pf->fcoe;
541         int cpu, i;
542
543         /* do nothing if not FCoE VSI */
544         if (vsi->type != I40E_VSI_FCOE)
545                 return;
546
547         /* do nothing if no DDP pools were allocated */
548         if (!fcoe->ddp_pool)
549                 return;
550
551         for (i = 0; i < I40E_FCOE_DDP_MAX; i++)
552                 i40e_fcoe_ddp_put(vsi->netdev, i);
553
554         for_each_possible_cpu(cpu)
555                 i40e_fcoe_dma_pool_free(fcoe, &pf->pdev->dev, cpu);
556
557         free_percpu(fcoe->ddp_pool);
558         fcoe->ddp_pool = NULL;
559
560         netdev_info(vsi->netdev, "VSI %d,%d FCoE DDP resources released\n",
561                     vsi->id, vsi->seid);
562 }
563
564 /**
565  * i40e_fcoe_setup_ddp_resources - allocate per cpu DDP resources
566  * @vsi: the VSI FCoE is associated with
567  *
568  * Returns 0 on successful or non zero on failure
569  *
570  **/
571 int i40e_fcoe_setup_ddp_resources(struct i40e_vsi *vsi)
572 {
573         struct i40e_pf *pf = vsi->back;
574         struct device *dev = &pf->pdev->dev;
575         struct i40e_fcoe *fcoe = &pf->fcoe;
576         unsigned int cpu;
577         int i;
578
579         if (vsi->type != I40E_VSI_FCOE)
580                 return -ENODEV;
581
582         /* do nothing if no DDP pools were allocated */
583         if (fcoe->ddp_pool)
584                 return -EEXIST;
585
586         /* allocate per CPU memory to track DDP pools */
587         fcoe->ddp_pool = alloc_percpu(struct i40e_fcoe_ddp_pool);
588         if (!fcoe->ddp_pool) {
589                 dev_err(&pf->pdev->dev, "failed to allocate percpu DDP\n");
590                 return -ENOMEM;
591         }
592
593         /* allocate pci pool for each cpu */
594         for_each_possible_cpu(cpu) {
595                 if (!i40e_fcoe_dma_pool_create(fcoe, dev, cpu))
596                         continue;
597
598                 dev_err(dev, "failed to alloc DDP pool on cpu:%d\n", cpu);
599                 i40e_fcoe_free_ddp_resources(vsi);
600                 return -ENOMEM;
601         }
602
603         /* initialize the sw context */
604         for (i = 0; i < I40E_FCOE_DDP_MAX; i++)
605                 i40e_fcoe_ddp_clear(&fcoe->ddp[i]);
606
607         netdev_info(vsi->netdev, "VSI %d,%d FCoE DDP resources allocated\n",
608                     vsi->id, vsi->seid);
609
610         return 0;
611 }
612
613 /**
614  * i40e_fcoe_handle_status - check the Programming Status for FCoE
615  * @rx_ring: the Rx ring for this descriptor
616  * @rx_desc: the Rx descriptor for Programming Status, not a packet descriptor.
617  *
618  * Check if this is the Rx Programming Status descriptor write-back for FCoE.
619  * This is used to verify if the context/filter programming or invalidation
620  * requested by SW to the HW is successful or not and take actions accordingly.
621  **/
622 void i40e_fcoe_handle_status(struct i40e_ring *rx_ring,
623                              union i40e_rx_desc *rx_desc, u8 prog_id)
624 {
625         struct i40e_pf *pf = rx_ring->vsi->back;
626         struct i40e_fcoe *fcoe = &pf->fcoe;
627         struct i40e_fcoe_ddp *ddp;
628         u32 error;
629         u16 xid;
630         u64 qw;
631
632         /* we only care for FCoE here */
633         if (!i40e_fcoe_progid_is_fcoe(prog_id))
634                 return;
635
636         xid = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fcoe_param) &
637               (I40E_FCOE_DDP_MAX - 1);
638
639         if (!i40e_fcoe_xid_is_valid(xid))
640                 return;
641
642         ddp = &fcoe->ddp[xid];
643         WARN_ON(xid != ddp->xid);
644
645         qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
646         error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
647                 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
648
649         /* DDP context programming status: failure or success */
650         if (prog_id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) {
651                 if (I40E_RX_PROG_FCOE_ERROR_TBL_FULL(error)) {
652                         dev_err(&pf->pdev->dev, "xid %x ddp->xid %x TABLE FULL\n",
653                                 xid, ddp->xid);
654                         ddp->prerr |= I40E_RX_PROG_FCOE_ERROR_TBL_FULL_BIT;
655                 }
656                 if (I40E_RX_PROG_FCOE_ERROR_CONFLICT(error)) {
657                         dev_err(&pf->pdev->dev, "xid %x ddp->xid %x CONFLICT\n",
658                                 xid, ddp->xid);
659                         ddp->prerr |= I40E_RX_PROG_FCOE_ERROR_CONFLICT_BIT;
660                 }
661         }
662
663         /* DDP context invalidation status: failure or success */
664         if (prog_id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS) {
665                 if (I40E_RX_PROG_FCOE_ERROR_INVLFAIL(error)) {
666                         dev_err(&pf->pdev->dev, "xid %x ddp->xid %x INVALIDATION FAILURE\n",
667                                 xid, ddp->xid);
668                         ddp->prerr |= I40E_RX_PROG_FCOE_ERROR_INVLFAIL_BIT;
669                 }
670                 /* clear the flag so we can retry invalidation */
671                 clear_bit(__I40E_FCOE_DDP_ABORTED, &ddp->flags);
672         }
673
674         /* unmap DMA */
675         i40e_fcoe_ddp_unmap(pf, ddp);
676         i40e_fcoe_ddp_clear(ddp);
677 }
678
679 /**
680  * i40e_fcoe_handle_offload - check ddp status and mark it done
681  * @adapter: i40e adapter
682  * @rx_desc: advanced rx descriptor
683  * @skb: the skb holding the received data
684  *
685  * This checks ddp status.
686  *
687  * Returns : < 0 indicates an error or not a FCOE ddp, 0 indicates
688  * not passing the skb to ULD, > 0 indicates is the length of data
689  * being ddped.
690  *
691  **/
692 int i40e_fcoe_handle_offload(struct i40e_ring *rx_ring,
693                              union i40e_rx_desc *rx_desc,
694                              struct sk_buff *skb)
695 {
696         struct i40e_pf *pf = rx_ring->vsi->back;
697         struct i40e_fcoe *fcoe = &pf->fcoe;
698         struct fc_frame_header *fh = NULL;
699         struct i40e_fcoe_ddp *ddp = NULL;
700         u32 status, fltstat;
701         u32 error, fcerr;
702         int rc = -EINVAL;
703         u16 ptype;
704         u16 xid;
705         u64 qw;
706
707         /* check this rxd is for programming status */
708         qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
709         /* packet descriptor, check packet type */
710         ptype = (qw & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
711         if (!i40e_rx_is_fcoe(ptype))
712                 goto out_no_ddp;
713
714         error = (qw & I40E_RXD_QW1_ERROR_MASK) >> I40E_RXD_QW1_ERROR_SHIFT;
715         fcerr = (error >> I40E_RX_DESC_ERROR_L3L4E_SHIFT) &
716                  I40E_RX_DESC_FCOE_ERROR_MASK;
717
718         /* check stateless offload error */
719         if (unlikely(fcerr == I40E_RX_DESC_ERROR_L3L4E_PROT)) {
720                 dev_err(&pf->pdev->dev, "Protocol Error\n");
721                 skb->ip_summed = CHECKSUM_NONE;
722         } else {
723                 skb->ip_summed = CHECKSUM_UNNECESSARY;
724         }
725
726         /* check hw status on ddp */
727         status = (qw & I40E_RXD_QW1_STATUS_MASK) >> I40E_RXD_QW1_STATUS_SHIFT;
728         fltstat = (status >> I40E_RX_DESC_STATUS_FLTSTAT_SHIFT) &
729                    I40E_RX_DESC_FLTSTAT_FCMASK;
730
731         /* now we are ready to check DDP */
732         fh = i40e_fcoe_fc_frame_header(skb);
733         xid = i40e_fcoe_fc_get_xid(fh);
734         if (!i40e_fcoe_xid_is_valid(xid))
735                 goto out_no_ddp;
736
737         /* non DDP normal receive, return to the protocol stack */
738         if (fltstat == I40E_RX_DESC_FLTSTAT_NOMTCH)
739                 goto out_no_ddp;
740
741         /* do we have a sw ddp context setup ? */
742         ddp = &fcoe->ddp[xid];
743         if (!ddp->sgl)
744                 goto out_no_ddp;
745
746         /* fetch xid from hw rxd wb, which should match up the sw ctxt */
747         xid = le16_to_cpu(rx_desc->wb.qword0.lo_dword.mirr_fcoe.fcoe_ctx_id);
748         if (ddp->xid != xid) {
749                 dev_err(&pf->pdev->dev, "xid 0x%x does not match ctx_xid 0x%x\n",
750                         ddp->xid, xid);
751                 goto out_put_ddp;
752         }
753
754         /* the same exchange has already errored out */
755         if (ddp->fcerr) {
756                 dev_err(&pf->pdev->dev, "xid 0x%x fcerr 0x%x reported fcer 0x%x\n",
757                         xid, ddp->fcerr, fcerr);
758                 goto out_put_ddp;
759         }
760
761         /* fcoe param is valid by now with correct DDPed length */
762         ddp->len = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fcoe_param);
763         ddp->fcerr = fcerr;
764         /* header posting only, useful only for target mode and debugging */
765         if (fltstat == I40E_RX_DESC_FLTSTAT_DDP) {
766                 /* For target mode, we get header of the last packet but it
767                  * does not have the FCoE trailer field, i.e., CRC and EOF
768                  * Ordered Set since they are offloaded by the HW, so fill
769                  * it up correspondingly to allow the packet to pass through
770                  * to the upper protocol stack.
771                  */
772                 u32 f_ctl = ntoh24(fh->fh_f_ctl);
773
774                 if ((f_ctl & FC_FC_END_SEQ) &&
775                     (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA)) {
776                         struct fcoe_crc_eof *crc = NULL;
777
778                         crc = (struct fcoe_crc_eof *)skb_put(skb, sizeof(*crc));
779                         crc->fcoe_eof = FC_EOF_T;
780                 } else {
781                         /* otherwise, drop the header only frame */
782                         rc = 0;
783                         goto out_no_ddp;
784                 }
785         }
786
787 out_put_ddp:
788         /* either we got RSP or we have an error, unmap DMA in both cases */
789         i40e_fcoe_ddp_unmap(pf, ddp);
790         if (ddp->len && !ddp->fcerr) {
791                 int pkts;
792
793                 rc = ddp->len;
794                 i40e_fcoe_ddp_clear(ddp);
795                 ddp->len = rc;
796                 pkts = DIV_ROUND_UP(rc, 2048);
797                 rx_ring->stats.bytes += rc;
798                 rx_ring->stats.packets += pkts;
799                 rx_ring->q_vector->rx.total_bytes += rc;
800                 rx_ring->q_vector->rx.total_packets += pkts;
801                 set_bit(__I40E_FCOE_DDP_DONE, &ddp->flags);
802         }
803
804 out_no_ddp:
805         return rc;
806 }
807
808 /**
809  * i40e_fcoe_ddp_setup - called to set up ddp context
810  * @netdev: the corresponding net_device
811  * @xid: the exchange id requesting ddp
812  * @sgl: the scatter-gather list for this request
813  * @sgc: the number of scatter-gather items
814  * @target_mode: indicates this is a DDP request for target
815  *
816  * Returns : 1 for success and 0 for no DDP on this I/O
817  **/
818 static int i40e_fcoe_ddp_setup(struct net_device *netdev, u16 xid,
819                                struct scatterlist *sgl, unsigned int sgc,
820                                int target_mode)
821 {
822         static const unsigned int bufflen = I40E_FCOE_DDP_BUF_MIN;
823         struct i40e_netdev_priv *np = netdev_priv(netdev);
824         struct i40e_fcoe_ddp_pool *ddp_pool;
825         struct i40e_pf *pf = np->vsi->back;
826         struct i40e_fcoe *fcoe = &pf->fcoe;
827         unsigned int i, j, dmacount;
828         struct i40e_fcoe_ddp *ddp;
829         unsigned int firstoff = 0;
830         unsigned int thisoff = 0;
831         unsigned int thislen = 0;
832         struct scatterlist *sg;
833         dma_addr_t addr = 0;
834         unsigned int len;
835
836         if (xid >= I40E_FCOE_DDP_MAX) {
837                 dev_warn(&pf->pdev->dev, "xid=0x%x out-of-range\n", xid);
838                 return 0;
839         }
840
841         /* no DDP if we are already down or resetting */
842         if (test_bit(__I40E_DOWN, &pf->state) ||
843             test_bit(__I40E_NEEDS_RESTART, &pf->state)) {
844                 dev_info(&pf->pdev->dev, "xid=0x%x device in reset/down\n",
845                          xid);
846                 return 0;
847         }
848
849         ddp = &fcoe->ddp[xid];
850         if (ddp->sgl) {
851                 dev_info(&pf->pdev->dev, "xid 0x%x w/ non-null sgl=%p nents=%d\n",
852                          xid, ddp->sgl, ddp->sgc);
853                 return 0;
854         }
855         i40e_fcoe_ddp_clear(ddp);
856
857         if (!fcoe->ddp_pool) {
858                 dev_info(&pf->pdev->dev, "No DDP pool, xid 0x%x\n", xid);
859                 return 0;
860         }
861
862         ddp_pool = per_cpu_ptr(fcoe->ddp_pool, get_cpu());
863         if (!ddp_pool->pool) {
864                 dev_info(&pf->pdev->dev, "No percpu ddp pool, xid 0x%x\n", xid);
865                 goto out_noddp;
866         }
867
868         /* setup dma from scsi command sgl */
869         dmacount = dma_map_sg(&pf->pdev->dev, sgl, sgc, DMA_FROM_DEVICE);
870         if (dmacount == 0) {
871                 dev_info(&pf->pdev->dev, "dma_map_sg for sgl %p, sgc %d failed\n",
872                          sgl, sgc);
873                 goto out_noddp_unmap;
874         }
875
876         /* alloc the udl from our ddp pool */
877         ddp->udl = dma_pool_alloc(ddp_pool->pool, GFP_ATOMIC, &ddp->udp);
878         if (!ddp->udl) {
879                 dev_info(&pf->pdev->dev,
880                          "Failed allocated ddp context, xid 0x%x\n", xid);
881                 goto out_noddp_unmap;
882         }
883
884         j = 0;
885         ddp->len = 0;
886         for_each_sg(sgl, sg, dmacount, i) {
887                 addr = sg_dma_address(sg);
888                 len = sg_dma_len(sg);
889                 ddp->len += len;
890                 while (len) {
891                         /* max number of buffers allowed in one DDP context */
892                         if (j >= I40E_FCOE_DDP_BUFFCNT_MAX) {
893                                 dev_info(&pf->pdev->dev,
894                                          "xid=%x:%d,%d,%d:addr=%llx not enough descriptors\n",
895                                          xid, i, j, dmacount, (u64)addr);
896                                 goto out_noddp_free;
897                         }
898
899                         /* get the offset of length of current buffer */
900                         thisoff = addr & ((dma_addr_t)bufflen - 1);
901                         thislen = min_t(unsigned int, (bufflen - thisoff), len);
902                         /* all but the 1st buffer (j == 0)
903                          * must be aligned on bufflen
904                          */
905                         if ((j != 0) && (thisoff))
906                                 goto out_noddp_free;
907
908                         /* all but the last buffer
909                          * ((i == (dmacount - 1)) && (thislen == len))
910                          * must end at bufflen
911                          */
912                         if (((i != (dmacount - 1)) || (thislen != len)) &&
913                             ((thislen + thisoff) != bufflen))
914                                 goto out_noddp_free;
915
916                         ddp->udl[j] = (u64)(addr - thisoff);
917                         /* only the first buffer may have none-zero offset */
918                         if (j == 0)
919                                 firstoff = thisoff;
920                         len -= thislen;
921                         addr += thislen;
922                         j++;
923                 }
924         }
925         /* only the last buffer may have non-full bufflen */
926         ddp->lastsize = thisoff + thislen;
927         ddp->firstoff = firstoff;
928         ddp->list_len = j;
929         ddp->pool = ddp_pool->pool;
930         ddp->sgl = sgl;
931         ddp->sgc = sgc;
932         ddp->xid = xid;
933         if (target_mode)
934                 set_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags);
935         set_bit(__I40E_FCOE_DDP_INITALIZED, &ddp->flags);
936
937         put_cpu();
938         return 1; /* Success */
939
940 out_noddp_free:
941         dma_pool_free(ddp->pool, ddp->udl, ddp->udp);
942         i40e_fcoe_ddp_clear(ddp);
943
944 out_noddp_unmap:
945         dma_unmap_sg(&pf->pdev->dev, sgl, sgc, DMA_FROM_DEVICE);
946 out_noddp:
947         put_cpu();
948         return 0;
949 }
950
951 /**
952  * i40e_fcoe_ddp_get - called to set up ddp context in initiator mode
953  * @netdev: the corresponding net_device
954  * @xid: the exchange id requesting ddp
955  * @sgl: the scatter-gather list for this request
956  * @sgc: the number of scatter-gather items
957  *
958  * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
959  * and is expected to be called from ULD, e.g., FCP layer of libfc
960  * to set up ddp for the corresponding xid of the given sglist for
961  * the corresponding I/O.
962  *
963  * Returns : 1 for success and 0 for no ddp
964  **/
965 static int i40e_fcoe_ddp_get(struct net_device *netdev, u16 xid,
966                              struct scatterlist *sgl, unsigned int sgc)
967 {
968         return i40e_fcoe_ddp_setup(netdev, xid, sgl, sgc, 0);
969 }
970
971 /**
972  * i40e_fcoe_ddp_target - called to set up ddp context in target mode
973  * @netdev: the corresponding net_device
974  * @xid: the exchange id requesting ddp
975  * @sgl: the scatter-gather list for this request
976  * @sgc: the number of scatter-gather items
977  *
978  * This is the implementation of net_device_ops.ndo_fcoe_ddp_target
979  * and is expected to be called from ULD, e.g., FCP layer of libfc
980  * to set up ddp for the corresponding xid of the given sglist for
981  * the corresponding I/O. The DDP in target mode is a write I/O request
982  * from the initiator.
983  *
984  * Returns : 1 for success and 0 for no ddp
985  **/
986 static int i40e_fcoe_ddp_target(struct net_device *netdev, u16 xid,
987                                 struct scatterlist *sgl, unsigned int sgc)
988 {
989         return i40e_fcoe_ddp_setup(netdev, xid, sgl, sgc, 1);
990 }
991
992 /**
993  * i40e_fcoe_program_ddp - programs the HW DDP related descriptors
994  * @tx_ring: transmit ring for this packet
995  * @skb:     the packet to be sent out
996  * @sof: the SOF to indicate class of service
997  *
998  * Determine if it is READ/WRITE command, and finds out if there is
999  * a matching SW DDP context for this command. DDP is applicable
1000  * only in case of READ if initiator or WRITE in case of
1001  * responder (via checking XFER_RDY).
1002  *
1003  * Note: caller checks sof and ddp sw context
1004  *
1005  * Returns : none
1006  *
1007  **/
1008 static void i40e_fcoe_program_ddp(struct i40e_ring *tx_ring,
1009                                   struct sk_buff *skb,
1010                                   struct i40e_fcoe_ddp *ddp, u8 sof)
1011 {
1012         struct i40e_fcoe_filter_context_desc *filter_desc = NULL;
1013         struct i40e_fcoe_queue_context_desc *queue_desc = NULL;
1014         struct i40e_fcoe_ddp_context_desc *ddp_desc = NULL;
1015         struct i40e_pf *pf = tx_ring->vsi->back;
1016         u16 i = tx_ring->next_to_use;
1017         struct fc_frame_header *fh;
1018         u64 flags_rsvd_lanq = 0;
1019         bool target_mode;
1020
1021         /* check if abort is still pending */
1022         if (test_bit(__I40E_FCOE_DDP_ABORTED, &ddp->flags)) {
1023                 dev_warn(&pf->pdev->dev,
1024                          "DDP abort is still pending xid:%hx and ddp->flags:%lx:\n",
1025                          ddp->xid, ddp->flags);
1026                 return;
1027         }
1028
1029         /* set the flag to indicate this is programmed */
1030         if (test_and_set_bit(__I40E_FCOE_DDP_PROGRAMMED, &ddp->flags)) {
1031                 dev_warn(&pf->pdev->dev,
1032                          "DDP is already programmed for xid:%hx and ddp->flags:%lx:\n",
1033                          ddp->xid, ddp->flags);
1034                 return;
1035         }
1036
1037         /* Prepare the DDP context descriptor */
1038         ddp_desc = I40E_DDP_CONTEXT_DESC(tx_ring, i);
1039         i++;
1040         if (i == tx_ring->count)
1041                 i = 0;
1042
1043         ddp_desc->type_cmd_foff_lsize =
1044                                 cpu_to_le64(I40E_TX_DESC_DTYPE_DDP_CTX  |
1045                                 ((u64)I40E_FCOE_DDP_CTX_DESC_BSIZE_4K  <<
1046                                 I40E_FCOE_DDP_CTX_QW1_CMD_SHIFT)        |
1047                                 ((u64)ddp->firstoff                    <<
1048                                 I40E_FCOE_DDP_CTX_QW1_FOFF_SHIFT)       |
1049                                 ((u64)ddp->lastsize                    <<
1050                                 I40E_FCOE_DDP_CTX_QW1_LSIZE_SHIFT));
1051         ddp_desc->rsvd = cpu_to_le64(0);
1052
1053         /* target mode needs last packet in the sequence  */
1054         target_mode = test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags);
1055         if (target_mode)
1056                 ddp_desc->type_cmd_foff_lsize |=
1057                         cpu_to_le64(I40E_FCOE_DDP_CTX_DESC_LASTSEQH);
1058
1059         /* Prepare queue_context descriptor */
1060         queue_desc = I40E_QUEUE_CONTEXT_DESC(tx_ring, i++);
1061         if (i == tx_ring->count)
1062                 i = 0;
1063         queue_desc->dmaindx_fbase = cpu_to_le64(ddp->xid | ((u64)ddp->udp));
1064         queue_desc->flen_tph = cpu_to_le64(ddp->list_len |
1065                                 ((u64)(I40E_FCOE_QUEUE_CTX_DESC_TPHRDESC |
1066                                 I40E_FCOE_QUEUE_CTX_DESC_TPHDATA) <<
1067                                 I40E_FCOE_QUEUE_CTX_QW1_TPH_SHIFT));
1068
1069         /* Prepare filter_context_desc */
1070         filter_desc = I40E_FILTER_CONTEXT_DESC(tx_ring, i);
1071         i++;
1072         if (i == tx_ring->count)
1073                 i = 0;
1074
1075         fh = (struct fc_frame_header *)skb_transport_header(skb);
1076         filter_desc->param = cpu_to_le32(ntohl(fh->fh_parm_offset));
1077         filter_desc->seqn = cpu_to_le16(ntohs(fh->fh_seq_cnt));
1078         filter_desc->rsvd_dmaindx = cpu_to_le16(ddp->xid <<
1079                                 I40E_FCOE_FILTER_CTX_QW0_DMAINDX_SHIFT);
1080
1081         flags_rsvd_lanq = I40E_FCOE_FILTER_CTX_DESC_CTYP_DDP;
1082         flags_rsvd_lanq |= (u64)(target_mode ?
1083                         I40E_FCOE_FILTER_CTX_DESC_ENODE_RSP :
1084                         I40E_FCOE_FILTER_CTX_DESC_ENODE_INIT);
1085
1086         flags_rsvd_lanq |= (u64)((sof == FC_SOF_I2 || sof == FC_SOF_N2) ?
1087                         I40E_FCOE_FILTER_CTX_DESC_FC_CLASS2 :
1088                         I40E_FCOE_FILTER_CTX_DESC_FC_CLASS3);
1089
1090         flags_rsvd_lanq |= ((u64)skb->queue_mapping <<
1091                                 I40E_FCOE_FILTER_CTX_QW1_LANQINDX_SHIFT);
1092         filter_desc->flags_rsvd_lanq = cpu_to_le64(flags_rsvd_lanq);
1093
1094         /* By this time, all offload related descriptors has been programmed */
1095         tx_ring->next_to_use = i;
1096 }
1097
1098 /**
1099  * i40e_fcoe_invalidate_ddp - invalidates DDP in case of abort
1100  * @tx_ring: transmit ring for this packet
1101  * @skb: the packet associated w/ this DDP invalidation, i.e., ABTS
1102  * @ddp: the SW DDP context for this DDP
1103  *
1104  * Programs the Tx context descriptor to do DDP invalidation.
1105  **/
1106 static void i40e_fcoe_invalidate_ddp(struct i40e_ring *tx_ring,
1107                                      struct sk_buff *skb,
1108                                      struct i40e_fcoe_ddp *ddp)
1109 {
1110         struct i40e_tx_context_desc *context_desc;
1111         int i;
1112
1113         if (test_and_set_bit(__I40E_FCOE_DDP_ABORTED, &ddp->flags))
1114                 return;
1115
1116         i = tx_ring->next_to_use;
1117         context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1118         i++;
1119         if (i == tx_ring->count)
1120                 i = 0;
1121
1122         context_desc->tunneling_params = cpu_to_le32(0);
1123         context_desc->l2tag2 = cpu_to_le16(0);
1124         context_desc->rsvd = cpu_to_le16(0);
1125         context_desc->type_cmd_tso_mss = cpu_to_le64(
1126                 I40E_TX_DESC_DTYPE_FCOE_CTX |
1127                 (I40E_FCOE_TX_CTX_DESC_OPCODE_DDP_CTX_INVL <<
1128                 I40E_TXD_CTX_QW1_CMD_SHIFT) |
1129                 (I40E_FCOE_TX_CTX_DESC_OPCODE_SINGLE_SEND <<
1130                 I40E_TXD_CTX_QW1_CMD_SHIFT));
1131         tx_ring->next_to_use = i;
1132 }
1133
1134 /**
1135  * i40e_fcoe_handle_ddp - check we should setup or invalidate DDP
1136  * @tx_ring: transmit ring for this packet
1137  * @skb: the packet to be sent out
1138  * @sof: the SOF to indicate class of service
1139  *
1140  * Determine if it is ABTS/READ/XFER_RDY, and finds out if there is
1141  * a matching SW DDP context for this command. DDP is applicable
1142  * only in case of READ if initiator or WRITE in case of
1143  * responder (via checking XFER_RDY). In case this is an ABTS, send
1144  * just invalidate the context.
1145  **/
1146 static void i40e_fcoe_handle_ddp(struct i40e_ring *tx_ring,
1147                                  struct sk_buff *skb, u8 sof)
1148 {
1149         struct i40e_pf *pf = tx_ring->vsi->back;
1150         struct i40e_fcoe *fcoe = &pf->fcoe;
1151         struct fc_frame_header *fh;
1152         struct i40e_fcoe_ddp *ddp;
1153         u32 f_ctl;
1154         u8 r_ctl;
1155         u16 xid;
1156
1157         fh = (struct fc_frame_header *)skb_transport_header(skb);
1158         f_ctl = ntoh24(fh->fh_f_ctl);
1159         r_ctl = fh->fh_r_ctl;
1160         ddp = NULL;
1161
1162         if ((r_ctl == FC_RCTL_DD_DATA_DESC) && (f_ctl & FC_FC_EX_CTX)) {
1163                 /* exchange responder? if so, XFER_RDY for write */
1164                 xid = ntohs(fh->fh_rx_id);
1165                 if (i40e_fcoe_xid_is_valid(xid)) {
1166                         ddp = &fcoe->ddp[xid];
1167                         if ((ddp->xid == xid) &&
1168                             (test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags)))
1169                                 i40e_fcoe_program_ddp(tx_ring, skb, ddp, sof);
1170                 }
1171         } else if (r_ctl == FC_RCTL_DD_UNSOL_CMD) {
1172                 /* exchange originator, check READ cmd */
1173                 xid = ntohs(fh->fh_ox_id);
1174                 if (i40e_fcoe_xid_is_valid(xid)) {
1175                         ddp = &fcoe->ddp[xid];
1176                         if ((ddp->xid == xid) &&
1177                             (!test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags)))
1178                                 i40e_fcoe_program_ddp(tx_ring, skb, ddp, sof);
1179                 }
1180         } else if (r_ctl == FC_RCTL_BA_ABTS) {
1181                 /* exchange originator, check ABTS */
1182                 xid = ntohs(fh->fh_ox_id);
1183                 if (i40e_fcoe_xid_is_valid(xid)) {
1184                         ddp = &fcoe->ddp[xid];
1185                         if ((ddp->xid == xid) &&
1186                             (!test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags)))
1187                                 i40e_fcoe_invalidate_ddp(tx_ring, skb, ddp);
1188                 }
1189         }
1190 }
1191
1192 /**
1193  * i40e_fcoe_tso - set up FCoE TSO
1194  * @tx_ring:  ring to send buffer on
1195  * @skb:      send buffer
1196  * @tx_flags: collected send information
1197  * @hdr_len:  the tso header length
1198  * @sof: the SOF to indicate class of service
1199  *
1200  * Note must already have sof checked to be either class 2 or class 3 before
1201  * calling this function.
1202  *
1203  * Returns 1 to indicate sequence segmentation offload is properly setup
1204  * or returns 0 to indicate no tso is needed, otherwise returns error
1205  * code to drop the frame.
1206  **/
1207 static int i40e_fcoe_tso(struct i40e_ring *tx_ring,
1208                          struct sk_buff *skb,
1209                          u32 tx_flags, u8 *hdr_len, u8 sof)
1210 {
1211         struct i40e_tx_context_desc *context_desc;
1212         u32 cd_type, cd_cmd, cd_tso_len, cd_mss;
1213         struct fc_frame_header *fh;
1214         u64 cd_type_cmd_tso_mss;
1215
1216         /* must match gso type as FCoE */
1217         if (!skb_is_gso(skb))
1218                 return 0;
1219
1220         /* is it the expected gso type for FCoE ?*/
1221         if (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE) {
1222                 netdev_err(skb->dev,
1223                            "wrong gso type %d:expecting SKB_GSO_FCOE\n",
1224                            skb_shinfo(skb)->gso_type);
1225                 return -EINVAL;
1226         }
1227
1228         /* header and trailer are inserted by hw */
1229         *hdr_len = skb_transport_offset(skb) + sizeof(struct fc_frame_header) +
1230                    sizeof(struct fcoe_crc_eof);
1231
1232         /* check sof to decide a class 2 or 3 TSO */
1233         if (likely(i40e_fcoe_sof_is_class3(sof)))
1234                 cd_cmd = I40E_FCOE_TX_CTX_DESC_OPCODE_TSO_FC_CLASS3;
1235         else
1236                 cd_cmd = I40E_FCOE_TX_CTX_DESC_OPCODE_TSO_FC_CLASS2;
1237
1238         /* param field valid? */
1239         fh = (struct fc_frame_header *)skb_transport_header(skb);
1240         if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
1241                 cd_cmd |= I40E_FCOE_TX_CTX_DESC_RELOFF;
1242
1243         /* fill the field values */
1244         cd_type = I40E_TX_DESC_DTYPE_FCOE_CTX;
1245         cd_tso_len = skb->len - *hdr_len;
1246         cd_mss = skb_shinfo(skb)->gso_size;
1247         cd_type_cmd_tso_mss =
1248                 ((u64)cd_type  << I40E_TXD_CTX_QW1_DTYPE_SHIFT)     |
1249                 ((u64)cd_cmd     << I40E_TXD_CTX_QW1_CMD_SHIFT)     |
1250                 ((u64)cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1251                 ((u64)cd_mss     << I40E_TXD_CTX_QW1_MSS_SHIFT);
1252
1253         /* grab the next descriptor */
1254         context_desc = I40E_TX_CTXTDESC(tx_ring, tx_ring->next_to_use);
1255         tx_ring->next_to_use++;
1256         if (tx_ring->next_to_use == tx_ring->count)
1257                 tx_ring->next_to_use = 0;
1258
1259         context_desc->tunneling_params = 0;
1260         context_desc->l2tag2 = cpu_to_le16((tx_flags & I40E_TX_FLAGS_VLAN_MASK)
1261                                             >> I40E_TX_FLAGS_VLAN_SHIFT);
1262         context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1263
1264         return 1;
1265 }
1266
1267 /**
1268  * i40e_fcoe_tx_map - build the tx descriptor
1269  * @tx_ring:  ring to send buffer on
1270  * @skb:      send buffer
1271  * @first:    first buffer info buffer to use
1272  * @tx_flags: collected send information
1273  * @hdr_len:  ptr to the size of the packet header
1274  * @eof:      the frame eof value
1275  *
1276  * Note, for FCoE, sof and eof are already checked
1277  **/
1278 static void i40e_fcoe_tx_map(struct i40e_ring *tx_ring,
1279                              struct sk_buff *skb,
1280                              struct i40e_tx_buffer *first,
1281                              u32 tx_flags, u8 hdr_len, u8 eof)
1282 {
1283         u32 td_offset = 0;
1284         u32 td_cmd = 0;
1285         u32 maclen;
1286
1287         /* insert CRC */
1288         td_cmd = I40E_TX_DESC_CMD_ICRC;
1289
1290         /* setup MACLEN */
1291         maclen = skb_network_offset(skb);
1292         if (tx_flags & I40E_TX_FLAGS_SW_VLAN)
1293                 maclen += sizeof(struct vlan_hdr);
1294
1295         if (skb->protocol == htons(ETH_P_FCOE)) {
1296                 /* for FCoE, maclen should exclude ether type */
1297                 maclen -= 2;
1298                 /* setup type as FCoE and EOF insertion */
1299                 td_cmd |= (I40E_TX_DESC_CMD_FCOET | i40e_fcoe_ctxt_eof(eof));
1300                 /* setup FCoELEN and FCLEN */
1301                 td_offset |= ((((sizeof(struct fcoe_hdr) + 2) >> 2) <<
1302                                 I40E_TX_DESC_LENGTH_IPLEN_SHIFT) |
1303                               ((sizeof(struct fc_frame_header) >> 2) <<
1304                                 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT));
1305                 /* trim to exclude trailer */
1306                 pskb_trim(skb, skb->len - sizeof(struct fcoe_crc_eof));
1307         }
1308
1309         /* MACLEN is ether header length in words not bytes */
1310         td_offset |= (maclen >> 1) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1311
1312         i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len, td_cmd, td_offset);
1313 }
1314
1315 /**
1316  * i40e_fcoe_set_skb_header - adjust skb header point for FIP/FCoE/FC
1317  * @skb: the skb to be adjusted
1318  *
1319  * Returns true if this skb is a FCoE/FIP or VLAN carried FCoE/FIP and then
1320  * adjusts the skb header pointers correspondingly. Otherwise, returns false.
1321  **/
1322 static inline int i40e_fcoe_set_skb_header(struct sk_buff *skb)
1323 {
1324         __be16 protocol = skb->protocol;
1325
1326         skb_reset_mac_header(skb);
1327         skb->mac_len = sizeof(struct ethhdr);
1328         if (protocol == htons(ETH_P_8021Q)) {
1329                 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)eth_hdr(skb);
1330
1331                 protocol = veth->h_vlan_encapsulated_proto;
1332                 skb->mac_len += sizeof(struct vlan_hdr);
1333         }
1334
1335         /* FCoE or FIP only */
1336         if ((protocol != htons(ETH_P_FIP)) &&
1337             (protocol != htons(ETH_P_FCOE)))
1338                 return -EINVAL;
1339
1340         /* set header to L2 of FCoE/FIP */
1341         skb_set_network_header(skb, skb->mac_len);
1342         if (protocol == htons(ETH_P_FIP))
1343                 return 0;
1344
1345         /* set header to L3 of FC */
1346         skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1347         return 0;
1348 }
1349
1350 /**
1351  * i40e_fcoe_xmit_frame - transmit buffer
1352  * @skb:     send buffer
1353  * @netdev:  the fcoe netdev
1354  *
1355  * Returns 0 if sent, else an error code
1356  **/
1357 static netdev_tx_t i40e_fcoe_xmit_frame(struct sk_buff *skb,
1358                                         struct net_device *netdev)
1359 {
1360         struct i40e_netdev_priv *np = netdev_priv(skb->dev);
1361         struct i40e_vsi *vsi = np->vsi;
1362         struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
1363         struct i40e_tx_buffer *first;
1364         u32 tx_flags = 0;
1365         u8 hdr_len = 0;
1366         u8 sof = 0;
1367         u8 eof = 0;
1368         int fso;
1369
1370         if (i40e_fcoe_set_skb_header(skb))
1371                 goto out_drop;
1372
1373         if (!i40e_xmit_descriptor_count(skb, tx_ring))
1374                 return NETDEV_TX_BUSY;
1375
1376         /* prepare the xmit flags */
1377         if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
1378                 goto out_drop;
1379
1380         /* record the location of the first descriptor for this packet */
1381         first = &tx_ring->tx_bi[tx_ring->next_to_use];
1382
1383         /* FIP is a regular L2 traffic w/o offload */
1384         if (skb->protocol == htons(ETH_P_FIP))
1385                 goto out_send;
1386
1387         /* check sof and eof, only supports FC Class 2 or 3 */
1388         if (i40e_fcoe_fc_sof(skb, &sof) || i40e_fcoe_fc_eof(skb, &eof)) {
1389                 netdev_err(netdev, "SOF/EOF error:%02x - %02x\n", sof, eof);
1390                 goto out_drop;
1391         }
1392
1393         /* always do FCCRC for FCoE */
1394         tx_flags |= I40E_TX_FLAGS_FCCRC;
1395
1396         /* check we should do sequence offload */
1397         fso = i40e_fcoe_tso(tx_ring, skb, tx_flags, &hdr_len, sof);
1398         if (fso < 0)
1399                 goto out_drop;
1400         else if (fso)
1401                 tx_flags |= I40E_TX_FLAGS_FSO;
1402         else
1403                 i40e_fcoe_handle_ddp(tx_ring, skb, sof);
1404
1405 out_send:
1406         /* send out the packet */
1407         i40e_fcoe_tx_map(tx_ring, skb, first, tx_flags, hdr_len, eof);
1408
1409         i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
1410         return NETDEV_TX_OK;
1411
1412 out_drop:
1413         dev_kfree_skb_any(skb);
1414         return NETDEV_TX_OK;
1415 }
1416
1417 /**
1418  * i40e_fcoe_change_mtu - NDO callback to change the Maximum Transfer Unit
1419  * @netdev: network interface device structure
1420  * @new_mtu: new value for maximum frame size
1421  *
1422  * Returns error as operation not permitted
1423  *
1424  **/
1425 static int i40e_fcoe_change_mtu(struct net_device *netdev, int new_mtu)
1426 {
1427         netdev_warn(netdev, "MTU change is not supported on FCoE interfaces\n");
1428         return -EPERM;
1429 }
1430
1431 /**
1432  * i40e_fcoe_set_features - set the netdev feature flags
1433  * @netdev: ptr to the netdev being adjusted
1434  * @features: the feature set that the stack is suggesting
1435  *
1436  **/
1437 static int i40e_fcoe_set_features(struct net_device *netdev,
1438                                   netdev_features_t features)
1439 {
1440         struct i40e_netdev_priv *np = netdev_priv(netdev);
1441         struct i40e_vsi *vsi = np->vsi;
1442
1443         if (features & NETIF_F_HW_VLAN_CTAG_RX)
1444                 i40e_vlan_stripping_enable(vsi);
1445         else
1446                 i40e_vlan_stripping_disable(vsi);
1447
1448         return 0;
1449 }
1450
1451 static const struct net_device_ops i40e_fcoe_netdev_ops = {
1452         .ndo_open               = i40e_open,
1453         .ndo_stop               = i40e_close,
1454         .ndo_get_stats64        = i40e_get_netdev_stats_struct,
1455         .ndo_set_rx_mode        = i40e_set_rx_mode,
1456         .ndo_validate_addr      = eth_validate_addr,
1457         .ndo_set_mac_address    = i40e_set_mac,
1458         .ndo_change_mtu         = i40e_fcoe_change_mtu,
1459         .ndo_do_ioctl           = i40e_ioctl,
1460         .ndo_tx_timeout         = i40e_tx_timeout,
1461         .ndo_vlan_rx_add_vid    = i40e_vlan_rx_add_vid,
1462         .ndo_vlan_rx_kill_vid   = i40e_vlan_rx_kill_vid,
1463         .ndo_setup_tc           = i40e_setup_tc,
1464
1465 #ifdef CONFIG_NET_POLL_CONTROLLER
1466         .ndo_poll_controller    = i40e_netpoll,
1467 #endif
1468         .ndo_start_xmit         = i40e_fcoe_xmit_frame,
1469         .ndo_fcoe_enable        = i40e_fcoe_enable,
1470         .ndo_fcoe_disable       = i40e_fcoe_disable,
1471         .ndo_fcoe_ddp_setup     = i40e_fcoe_ddp_get,
1472         .ndo_fcoe_ddp_done      = i40e_fcoe_ddp_put,
1473         .ndo_fcoe_ddp_target    = i40e_fcoe_ddp_target,
1474         .ndo_set_features       = i40e_fcoe_set_features,
1475 };
1476
1477 /* fcoe network device type */
1478 static struct device_type fcoe_netdev_type = {
1479         .name = "fcoe",
1480 };
1481
1482 /**
1483  * i40e_fcoe_config_netdev - prepares the VSI context for creating a FCoE VSI
1484  * @vsi: pointer to the associated VSI struct
1485  * @ctxt: pointer to the associated VSI context to be passed to HW
1486  *
1487  * Returns 0 on success or < 0 on error
1488  **/
1489 void i40e_fcoe_config_netdev(struct net_device *netdev, struct i40e_vsi *vsi)
1490 {
1491         struct i40e_hw *hw = &vsi->back->hw;
1492         struct i40e_pf *pf = vsi->back;
1493
1494         if (vsi->type != I40E_VSI_FCOE)
1495                 return;
1496
1497         netdev->features = (NETIF_F_HW_VLAN_CTAG_TX |
1498                             NETIF_F_HW_VLAN_CTAG_RX |
1499                             NETIF_F_HW_VLAN_CTAG_FILTER);
1500
1501         netdev->vlan_features = netdev->features;
1502         netdev->vlan_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
1503                                    NETIF_F_HW_VLAN_CTAG_RX |
1504                                    NETIF_F_HW_VLAN_CTAG_FILTER);
1505         netdev->fcoe_ddp_xid = I40E_FCOE_DDP_MAX - 1;
1506         netdev->features |= NETIF_F_ALL_FCOE;
1507         netdev->vlan_features |= NETIF_F_ALL_FCOE;
1508         netdev->hw_features |= netdev->features;
1509         netdev->priv_flags |= IFF_UNICAST_FLT;
1510         netdev->priv_flags |= IFF_SUPP_NOFCS;
1511
1512         strlcpy(netdev->name, "fcoe%d", IFNAMSIZ-1);
1513         netdev->mtu = FCOE_MTU;
1514         SET_NETDEV_DEV(netdev, &pf->pdev->dev);
1515         SET_NETDEV_DEVTYPE(netdev, &fcoe_netdev_type);
1516         /* set different dev_port value 1 for FCoE netdev than the default
1517          * zero dev_port value for PF netdev, this helps biosdevname user
1518          * tool to differentiate them correctly while both attached to the
1519          * same PCI function.
1520          */
1521         netdev->dev_port = 1;
1522         i40e_add_filter(vsi, hw->mac.san_addr, 0, false, false);
1523         i40e_add_filter(vsi, (u8[6]) FC_FCOE_FLOGI_MAC, 0, false, false);
1524         i40e_add_filter(vsi, FIP_ALL_FCOE_MACS, 0, false, false);
1525         i40e_add_filter(vsi, FIP_ALL_ENODE_MACS, 0, false, false);
1526
1527         /* use san mac */
1528         ether_addr_copy(netdev->dev_addr, hw->mac.san_addr);
1529         ether_addr_copy(netdev->perm_addr, hw->mac.san_addr);
1530         /* fcoe netdev ops */
1531         netdev->netdev_ops = &i40e_fcoe_netdev_ops;
1532 }
1533
1534 /**
1535  * i40e_fcoe_vsi_setup - allocate and set up FCoE VSI
1536  * @pf: the PF that VSI is associated with
1537  *
1538  **/
1539 void i40e_fcoe_vsi_setup(struct i40e_pf *pf)
1540 {
1541         struct i40e_vsi *vsi;
1542         u16 seid;
1543         int i;
1544
1545         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED))
1546                 return;
1547
1548         BUG_ON(!pf->vsi[pf->lan_vsi]);
1549
1550         for (i = 0; i < pf->num_alloc_vsi; i++) {
1551                 vsi = pf->vsi[i];
1552                 if (vsi && vsi->type == I40E_VSI_FCOE) {
1553                         dev_warn(&pf->pdev->dev,
1554                                  "FCoE VSI already created\n");
1555                         return;
1556                 }
1557         }
1558
1559         seid = pf->vsi[pf->lan_vsi]->seid;
1560         vsi = i40e_vsi_setup(pf, I40E_VSI_FCOE, seid, 0);
1561         if (vsi) {
1562                 dev_dbg(&pf->pdev->dev,
1563                         "Successfully created FCoE VSI seid %d id %d uplink_seid %d PF seid %d\n",
1564                         vsi->seid, vsi->id, vsi->uplink_seid, seid);
1565         } else {
1566                 dev_info(&pf->pdev->dev, "Failed to create FCoE VSI\n");
1567         }
1568 }