GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / scsi / fnic / fnic_fcs.c
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  */
18 #include <linux/errno.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <linux/workqueue.h>
27 #include <scsi/fc/fc_fip.h>
28 #include <scsi/fc/fc_els.h>
29 #include <scsi/fc/fc_fcoe.h>
30 #include <scsi/fc_frame.h>
31 #include <scsi/libfc.h>
32 #include "fnic_io.h"
33 #include "fnic.h"
34 #include "fnic_fip.h"
35 #include "cq_enet_desc.h"
36 #include "cq_exch_desc.h"
37
38 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
39 struct workqueue_struct *fnic_fip_queue;
40 struct workqueue_struct *fnic_event_queue;
41
42 static void fnic_set_eth_mode(struct fnic *);
43 static void fnic_fcoe_send_vlan_req(struct fnic *fnic);
44 static void fnic_fcoe_start_fcf_disc(struct fnic *fnic);
45 static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *);
46 static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag);
47 static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb);
48
49 void fnic_handle_link(struct work_struct *work)
50 {
51         struct fnic *fnic = container_of(work, struct fnic, link_work);
52         unsigned long flags;
53         int old_link_status;
54         u32 old_link_down_cnt;
55
56         spin_lock_irqsave(&fnic->fnic_lock, flags);
57
58         if (fnic->stop_rx_link_events) {
59                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
60                 return;
61         }
62
63         old_link_down_cnt = fnic->link_down_cnt;
64         old_link_status = fnic->link_status;
65         fnic->link_status = vnic_dev_link_status(fnic->vdev);
66         fnic->link_down_cnt = vnic_dev_link_down_cnt(fnic->vdev);
67
68         switch (vnic_dev_port_speed(fnic->vdev)) {
69         case DCEM_PORTSPEED_10G:
70                 fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_10GBIT;
71                 fnic->lport->link_supported_speeds = FC_PORTSPEED_10GBIT;
72                 break;
73         case DCEM_PORTSPEED_25G:
74                 fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_25GBIT;
75                 fnic->lport->link_supported_speeds = FC_PORTSPEED_25GBIT;
76                 break;
77         case DCEM_PORTSPEED_40G:
78         case DCEM_PORTSPEED_4x10G:
79                 fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_40GBIT;
80                 fnic->lport->link_supported_speeds = FC_PORTSPEED_40GBIT;
81                 break;
82         case DCEM_PORTSPEED_100G:
83                 fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_100GBIT;
84                 fnic->lport->link_supported_speeds = FC_PORTSPEED_100GBIT;
85                 break;
86         default:
87                 fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_UNKNOWN;
88                 fnic->lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
89                 break;
90         }
91
92         if (old_link_status == fnic->link_status) {
93                 if (!fnic->link_status) {
94                         /* DOWN -> DOWN */
95                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
96                         fnic_fc_trace_set_data(fnic->lport->host->host_no,
97                                 FNIC_FC_LE, "Link Status: DOWN->DOWN",
98                                 strlen("Link Status: DOWN->DOWN"));
99                 } else {
100                         if (old_link_down_cnt != fnic->link_down_cnt) {
101                                 /* UP -> DOWN -> UP */
102                                 fnic->lport->host_stats.link_failure_count++;
103                                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
104                                 fnic_fc_trace_set_data(
105                                         fnic->lport->host->host_no,
106                                         FNIC_FC_LE,
107                                         "Link Status:UP_DOWN_UP",
108                                         strlen("Link_Status:UP_DOWN_UP")
109                                         );
110                                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
111                                              "link down\n");
112                                 fcoe_ctlr_link_down(&fnic->ctlr);
113                                 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
114                                         /* start FCoE VLAN discovery */
115                                         fnic_fc_trace_set_data(
116                                                 fnic->lport->host->host_no,
117                                                 FNIC_FC_LE,
118                                                 "Link Status: UP_DOWN_UP_VLAN",
119                                                 strlen(
120                                                 "Link Status: UP_DOWN_UP_VLAN")
121                                                 );
122                                         fnic_fcoe_send_vlan_req(fnic);
123                                         return;
124                                 }
125                                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
126                                              "link up\n");
127                                 fcoe_ctlr_link_up(&fnic->ctlr);
128                         } else {
129                                 /* UP -> UP */
130                                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
131                                 fnic_fc_trace_set_data(
132                                         fnic->lport->host->host_no, FNIC_FC_LE,
133                                         "Link Status: UP_UP",
134                                         strlen("Link Status: UP_UP"));
135                         }
136                 }
137         } else if (fnic->link_status) {
138                 /* DOWN -> UP */
139                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
140                 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
141                         /* start FCoE VLAN discovery */
142                                 fnic_fc_trace_set_data(
143                                 fnic->lport->host->host_no,
144                                 FNIC_FC_LE, "Link Status: DOWN_UP_VLAN",
145                                 strlen("Link Status: DOWN_UP_VLAN"));
146                         fnic_fcoe_send_vlan_req(fnic);
147                         return;
148                 }
149                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link up\n");
150                 fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_LE,
151                         "Link Status: DOWN_UP", strlen("Link Status: DOWN_UP"));
152                 fcoe_ctlr_link_up(&fnic->ctlr);
153         } else {
154                 /* UP -> DOWN */
155                 fnic->lport->host_stats.link_failure_count++;
156                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
157                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link down\n");
158                 fnic_fc_trace_set_data(
159                         fnic->lport->host->host_no, FNIC_FC_LE,
160                         "Link Status: UP_DOWN",
161                         strlen("Link Status: UP_DOWN"));
162                 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
163                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
164                                 "deleting fip-timer during link-down\n");
165                         del_timer_sync(&fnic->fip_timer);
166                 }
167                 fcoe_ctlr_link_down(&fnic->ctlr);
168         }
169
170 }
171
172 /*
173  * This function passes incoming fabric frames to libFC
174  */
175 void fnic_handle_frame(struct work_struct *work)
176 {
177         struct fnic *fnic = container_of(work, struct fnic, frame_work);
178         struct fc_lport *lp = fnic->lport;
179         unsigned long flags;
180         struct sk_buff *skb;
181         struct fc_frame *fp;
182
183         while ((skb = skb_dequeue(&fnic->frame_queue))) {
184
185                 spin_lock_irqsave(&fnic->fnic_lock, flags);
186                 if (fnic->stop_rx_link_events) {
187                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
188                         dev_kfree_skb(skb);
189                         return;
190                 }
191                 fp = (struct fc_frame *)skb;
192
193                 /*
194                  * If we're in a transitional state, just re-queue and return.
195                  * The queue will be serviced when we get to a stable state.
196                  */
197                 if (fnic->state != FNIC_IN_FC_MODE &&
198                     fnic->state != FNIC_IN_ETH_MODE) {
199                         skb_queue_head(&fnic->frame_queue, skb);
200                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
201                         return;
202                 }
203                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
204
205                 fc_exch_recv(lp, fp);
206         }
207 }
208
209 void fnic_fcoe_evlist_free(struct fnic *fnic)
210 {
211         struct fnic_event *fevt = NULL;
212         struct fnic_event *next = NULL;
213         unsigned long flags;
214
215         spin_lock_irqsave(&fnic->fnic_lock, flags);
216         if (list_empty(&fnic->evlist)) {
217                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
218                 return;
219         }
220
221         list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
222                 list_del(&fevt->list);
223                 kfree(fevt);
224         }
225         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
226 }
227
228 void fnic_handle_event(struct work_struct *work)
229 {
230         struct fnic *fnic = container_of(work, struct fnic, event_work);
231         struct fnic_event *fevt = NULL;
232         struct fnic_event *next = NULL;
233         unsigned long flags;
234
235         spin_lock_irqsave(&fnic->fnic_lock, flags);
236         if (list_empty(&fnic->evlist)) {
237                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
238                 return;
239         }
240
241         list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
242                 if (fnic->stop_rx_link_events) {
243                         list_del(&fevt->list);
244                         kfree(fevt);
245                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
246                         return;
247                 }
248                 /*
249                  * If we're in a transitional state, just re-queue and return.
250                  * The queue will be serviced when we get to a stable state.
251                  */
252                 if (fnic->state != FNIC_IN_FC_MODE &&
253                     fnic->state != FNIC_IN_ETH_MODE) {
254                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
255                         return;
256                 }
257
258                 list_del(&fevt->list);
259                 switch (fevt->event) {
260                 case FNIC_EVT_START_VLAN_DISC:
261                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
262                         fnic_fcoe_send_vlan_req(fnic);
263                         spin_lock_irqsave(&fnic->fnic_lock, flags);
264                         break;
265                 case FNIC_EVT_START_FCF_DISC:
266                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
267                                   "Start FCF Discovery\n");
268                         fnic_fcoe_start_fcf_disc(fnic);
269                         break;
270                 default:
271                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
272                                   "Unknown event 0x%x\n", fevt->event);
273                         break;
274                 }
275                 kfree(fevt);
276         }
277         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
278 }
279
280 /**
281  * Check if the Received FIP FLOGI frame is rejected
282  * @fip: The FCoE controller that received the frame
283  * @skb: The received FIP frame
284  *
285  * Returns non-zero if the frame is rejected with unsupported cmd with
286  * insufficient resource els explanation.
287  */
288 static inline int is_fnic_fip_flogi_reject(struct fcoe_ctlr *fip,
289                                          struct sk_buff *skb)
290 {
291         struct fc_lport *lport = fip->lp;
292         struct fip_header *fiph;
293         struct fc_frame_header *fh = NULL;
294         struct fip_desc *desc;
295         struct fip_encaps *els;
296         enum fip_desc_type els_dtype = 0;
297         u16 op;
298         u8 els_op;
299         u8 sub;
300
301         size_t els_len = 0;
302         size_t rlen;
303         size_t dlen = 0;
304
305         if (skb_linearize(skb))
306                 return 0;
307
308         if (skb->len < sizeof(*fiph))
309                 return 0;
310
311         fiph = (struct fip_header *)skb->data;
312         op = ntohs(fiph->fip_op);
313         sub = fiph->fip_subcode;
314
315         if (op != FIP_OP_LS)
316                 return 0;
317
318         if (sub != FIP_SC_REP)
319                 return 0;
320
321         rlen = ntohs(fiph->fip_dl_len) * 4;
322         if (rlen + sizeof(*fiph) > skb->len)
323                 return 0;
324
325         desc = (struct fip_desc *)(fiph + 1);
326         dlen = desc->fip_dlen * FIP_BPW;
327
328         if (desc->fip_dtype == FIP_DT_FLOGI) {
329
330                 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
331                         return 0;
332
333                 els_len = dlen - sizeof(*els);
334                 els = (struct fip_encaps *)desc;
335                 fh = (struct fc_frame_header *)(els + 1);
336                 els_dtype = desc->fip_dtype;
337
338                 if (!fh)
339                         return 0;
340
341                 /*
342                  * ELS command code, reason and explanation should be = Reject,
343                  * unsupported command and insufficient resource
344                  */
345                 els_op = *(u8 *)(fh + 1);
346                 if (els_op == ELS_LS_RJT) {
347                         shost_printk(KERN_INFO, lport->host,
348                                   "Flogi Request Rejected by Switch\n");
349                         return 1;
350                 }
351                 shost_printk(KERN_INFO, lport->host,
352                                 "Flogi Request Accepted by Switch\n");
353         }
354         return 0;
355 }
356
357 static void fnic_fcoe_send_vlan_req(struct fnic *fnic)
358 {
359         struct fcoe_ctlr *fip = &fnic->ctlr;
360         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
361         struct sk_buff *skb;
362         char *eth_fr;
363         int fr_len;
364         struct fip_vlan *vlan;
365         u64 vlan_tov;
366
367         fnic_fcoe_reset_vlans(fnic);
368         fnic->set_vlan(fnic, 0);
369
370         if (printk_ratelimit())
371                 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
372                           "Sending VLAN request...\n");
373
374         skb = dev_alloc_skb(sizeof(struct fip_vlan));
375         if (!skb)
376                 return;
377
378         fr_len = sizeof(*vlan);
379         eth_fr = (char *)skb->data;
380         vlan = (struct fip_vlan *)eth_fr;
381
382         memset(vlan, 0, sizeof(*vlan));
383         memcpy(vlan->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
384         memcpy(vlan->eth.h_dest, fcoe_all_fcfs, ETH_ALEN);
385         vlan->eth.h_proto = htons(ETH_P_FIP);
386
387         vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
388         vlan->fip.fip_op = htons(FIP_OP_VLAN);
389         vlan->fip.fip_subcode = FIP_SC_VL_REQ;
390         vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
391
392         vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
393         vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
394         memcpy(&vlan->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
395
396         vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
397         vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
398         put_unaligned_be64(fip->lp->wwnn, &vlan->desc.wwnn.fd_wwn);
399         atomic64_inc(&fnic_stats->vlan_stats.vlan_disc_reqs);
400
401         skb_put(skb, sizeof(*vlan));
402         skb->protocol = htons(ETH_P_FIP);
403         skb_reset_mac_header(skb);
404         skb_reset_network_header(skb);
405         fip->send(fip, skb);
406
407         /* set a timer so that we can retry if there no response */
408         vlan_tov = jiffies + msecs_to_jiffies(FCOE_CTLR_FIPVLAN_TOV);
409         mod_timer(&fnic->fip_timer, round_jiffies(vlan_tov));
410 }
411
412 static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *skb)
413 {
414         struct fcoe_ctlr *fip = &fnic->ctlr;
415         struct fip_header *fiph;
416         struct fip_desc *desc;
417         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
418         u16 vid;
419         size_t rlen;
420         size_t dlen;
421         struct fcoe_vlan *vlan;
422         u64 sol_time;
423         unsigned long flags;
424
425         FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
426                   "Received VLAN response...\n");
427
428         fiph = (struct fip_header *) skb->data;
429
430         FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
431                   "Received VLAN response... OP 0x%x SUB_OP 0x%x\n",
432                   ntohs(fiph->fip_op), fiph->fip_subcode);
433
434         rlen = ntohs(fiph->fip_dl_len) * 4;
435         fnic_fcoe_reset_vlans(fnic);
436         spin_lock_irqsave(&fnic->vlans_lock, flags);
437         desc = (struct fip_desc *)(fiph + 1);
438         while (rlen > 0) {
439                 dlen = desc->fip_dlen * FIP_BPW;
440                 switch (desc->fip_dtype) {
441                 case FIP_DT_VLAN:
442                         vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
443                         shost_printk(KERN_INFO, fnic->lport->host,
444                                   "process_vlan_resp: FIP VLAN %d\n", vid);
445                         vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
446                         if (!vlan) {
447                                 /* retry from timer */
448                                 spin_unlock_irqrestore(&fnic->vlans_lock,
449                                                         flags);
450                                 goto out;
451                         }
452                         vlan->vid = vid & 0x0fff;
453                         vlan->state = FIP_VLAN_AVAIL;
454                         list_add_tail(&vlan->list, &fnic->vlans);
455                         break;
456                 }
457                 desc = (struct fip_desc *)((char *)desc + dlen);
458                 rlen -= dlen;
459         }
460
461         /* any VLAN descriptors present ? */
462         if (list_empty(&fnic->vlans)) {
463                 /* retry from timer */
464                 atomic64_inc(&fnic_stats->vlan_stats.resp_withno_vlanID);
465                 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
466                           "No VLAN descriptors in FIP VLAN response\n");
467                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
468                 goto out;
469         }
470
471         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
472         fnic->set_vlan(fnic, vlan->vid);
473         vlan->state = FIP_VLAN_SENT; /* sent now */
474         vlan->sol_count++;
475         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
476
477         /* start the solicitation */
478         fcoe_ctlr_link_up(fip);
479
480         sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
481         mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
482 out:
483         return;
484 }
485
486 static void fnic_fcoe_start_fcf_disc(struct fnic *fnic)
487 {
488         unsigned long flags;
489         struct fcoe_vlan *vlan;
490         u64 sol_time;
491
492         spin_lock_irqsave(&fnic->vlans_lock, flags);
493         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
494         fnic->set_vlan(fnic, vlan->vid);
495         vlan->state = FIP_VLAN_SENT; /* sent now */
496         vlan->sol_count = 1;
497         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
498
499         /* start the solicitation */
500         fcoe_ctlr_link_up(&fnic->ctlr);
501
502         sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
503         mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
504 }
505
506 static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag)
507 {
508         unsigned long flags;
509         struct fcoe_vlan *fvlan;
510
511         spin_lock_irqsave(&fnic->vlans_lock, flags);
512         if (list_empty(&fnic->vlans)) {
513                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
514                 return -EINVAL;
515         }
516
517         fvlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
518         if (fvlan->state == FIP_VLAN_USED) {
519                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
520                 return 0;
521         }
522
523         if (fvlan->state == FIP_VLAN_SENT) {
524                 fvlan->state = FIP_VLAN_USED;
525                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
526                 return 0;
527         }
528         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
529         return -EINVAL;
530 }
531
532 static void fnic_event_enq(struct fnic *fnic, enum fnic_evt ev)
533 {
534         struct fnic_event *fevt;
535         unsigned long flags;
536
537         fevt = kmalloc(sizeof(*fevt), GFP_ATOMIC);
538         if (!fevt)
539                 return;
540
541         fevt->fnic = fnic;
542         fevt->event = ev;
543
544         spin_lock_irqsave(&fnic->fnic_lock, flags);
545         list_add_tail(&fevt->list, &fnic->evlist);
546         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
547
548         schedule_work(&fnic->event_work);
549 }
550
551 static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb)
552 {
553         struct fip_header *fiph;
554         int ret = 1;
555         u16 op;
556         u8 sub;
557
558         if (!skb || !(skb->data))
559                 return -1;
560
561         if (skb_linearize(skb))
562                 goto drop;
563
564         fiph = (struct fip_header *)skb->data;
565         op = ntohs(fiph->fip_op);
566         sub = fiph->fip_subcode;
567
568         if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
569                 goto drop;
570
571         if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
572                 goto drop;
573
574         if (op == FIP_OP_DISC && sub == FIP_SC_ADV) {
575                 if (fnic_fcoe_vlan_check(fnic, ntohs(fiph->fip_flags)))
576                         goto drop;
577                 /* pass it on to fcoe */
578                 ret = 1;
579         } else if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
580                 /* set the vlan as used */
581                 fnic_fcoe_process_vlan_resp(fnic, skb);
582                 ret = 0;
583         } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
584                 /* received CVL request, restart vlan disc */
585                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
586                 /* pass it on to fcoe */
587                 ret = 1;
588         }
589 drop:
590         return ret;
591 }
592
593 void fnic_handle_fip_frame(struct work_struct *work)
594 {
595         struct fnic *fnic = container_of(work, struct fnic, fip_frame_work);
596         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
597         unsigned long flags;
598         struct sk_buff *skb;
599         struct ethhdr *eh;
600
601         while ((skb = skb_dequeue(&fnic->fip_frame_queue))) {
602                 spin_lock_irqsave(&fnic->fnic_lock, flags);
603                 if (fnic->stop_rx_link_events) {
604                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
605                         dev_kfree_skb(skb);
606                         return;
607                 }
608                 /*
609                  * If we're in a transitional state, just re-queue and return.
610                  * The queue will be serviced when we get to a stable state.
611                  */
612                 if (fnic->state != FNIC_IN_FC_MODE &&
613                     fnic->state != FNIC_IN_ETH_MODE) {
614                         skb_queue_head(&fnic->fip_frame_queue, skb);
615                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
616                         return;
617                 }
618                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
619                 eh = (struct ethhdr *)skb->data;
620                 if (eh->h_proto == htons(ETH_P_FIP)) {
621                         skb_pull(skb, sizeof(*eh));
622                         if (fnic_fcoe_handle_fip_frame(fnic, skb) <= 0) {
623                                 dev_kfree_skb(skb);
624                                 continue;
625                         }
626                         /*
627                          * If there's FLOGI rejects - clear all
628                          * fcf's & restart from scratch
629                          */
630                         if (is_fnic_fip_flogi_reject(&fnic->ctlr, skb)) {
631                                 atomic64_inc(
632                                         &fnic_stats->vlan_stats.flogi_rejects);
633                                 shost_printk(KERN_INFO, fnic->lport->host,
634                                           "Trigger a Link down - VLAN Disc\n");
635                                 fcoe_ctlr_link_down(&fnic->ctlr);
636                                 /* start FCoE VLAN discovery */
637                                 fnic_fcoe_send_vlan_req(fnic);
638                                 dev_kfree_skb(skb);
639                                 continue;
640                         }
641                         fcoe_ctlr_recv(&fnic->ctlr, skb);
642                         continue;
643                 }
644         }
645 }
646
647 /**
648  * fnic_import_rq_eth_pkt() - handle received FCoE or FIP frame.
649  * @fnic:       fnic instance.
650  * @skb:        Ethernet Frame.
651  */
652 static inline int fnic_import_rq_eth_pkt(struct fnic *fnic, struct sk_buff *skb)
653 {
654         struct fc_frame *fp;
655         struct ethhdr *eh;
656         struct fcoe_hdr *fcoe_hdr;
657         struct fcoe_crc_eof *ft;
658
659         /*
660          * Undo VLAN encapsulation if present.
661          */
662         eh = (struct ethhdr *)skb->data;
663         if (eh->h_proto == htons(ETH_P_8021Q)) {
664                 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
665                 eh = skb_pull(skb, VLAN_HLEN);
666                 skb_reset_mac_header(skb);
667         }
668         if (eh->h_proto == htons(ETH_P_FIP)) {
669                 if (!(fnic->config.flags & VFCF_FIP_CAPABLE)) {
670                         printk(KERN_ERR "Dropped FIP frame, as firmware "
671                                         "uses non-FIP mode, Enable FIP "
672                                         "using UCSM\n");
673                         goto drop;
674                 }
675                 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
676                         FNIC_FC_RECV|0x80, (char *)skb->data, skb->len)) != 0) {
677                         printk(KERN_ERR "fnic ctlr frame trace error!!!");
678                 }
679                 skb_queue_tail(&fnic->fip_frame_queue, skb);
680                 queue_work(fnic_fip_queue, &fnic->fip_frame_work);
681                 return 1;               /* let caller know packet was used */
682         }
683         if (eh->h_proto != htons(ETH_P_FCOE))
684                 goto drop;
685         skb_set_network_header(skb, sizeof(*eh));
686         skb_pull(skb, sizeof(*eh));
687
688         fcoe_hdr = (struct fcoe_hdr *)skb->data;
689         if (FC_FCOE_DECAPS_VER(fcoe_hdr) != FC_FCOE_VER)
690                 goto drop;
691
692         fp = (struct fc_frame *)skb;
693         fc_frame_init(fp);
694         fr_sof(fp) = fcoe_hdr->fcoe_sof;
695         skb_pull(skb, sizeof(struct fcoe_hdr));
696         skb_reset_transport_header(skb);
697
698         ft = (struct fcoe_crc_eof *)(skb->data + skb->len - sizeof(*ft));
699         fr_eof(fp) = ft->fcoe_eof;
700         skb_trim(skb, skb->len - sizeof(*ft));
701         return 0;
702 drop:
703         dev_kfree_skb_irq(skb);
704         return -1;
705 }
706
707 /**
708  * fnic_update_mac_locked() - set data MAC address and filters.
709  * @fnic:       fnic instance.
710  * @new:        newly-assigned FCoE MAC address.
711  *
712  * Called with the fnic lock held.
713  */
714 void fnic_update_mac_locked(struct fnic *fnic, u8 *new)
715 {
716         u8 *ctl = fnic->ctlr.ctl_src_addr;
717         u8 *data = fnic->data_src_addr;
718
719         if (is_zero_ether_addr(new))
720                 new = ctl;
721         if (ether_addr_equal(data, new))
722                 return;
723         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "update_mac %pM\n", new);
724         if (!is_zero_ether_addr(data) && !ether_addr_equal(data, ctl))
725                 vnic_dev_del_addr(fnic->vdev, data);
726         memcpy(data, new, ETH_ALEN);
727         if (!ether_addr_equal(new, ctl))
728                 vnic_dev_add_addr(fnic->vdev, new);
729 }
730
731 /**
732  * fnic_update_mac() - set data MAC address and filters.
733  * @lport:      local port.
734  * @new:        newly-assigned FCoE MAC address.
735  */
736 void fnic_update_mac(struct fc_lport *lport, u8 *new)
737 {
738         struct fnic *fnic = lport_priv(lport);
739
740         spin_lock_irq(&fnic->fnic_lock);
741         fnic_update_mac_locked(fnic, new);
742         spin_unlock_irq(&fnic->fnic_lock);
743 }
744
745 /**
746  * fnic_set_port_id() - set the port_ID after successful FLOGI.
747  * @lport:      local port.
748  * @port_id:    assigned FC_ID.
749  * @fp:         received frame containing the FLOGI accept or NULL.
750  *
751  * This is called from libfc when a new FC_ID has been assigned.
752  * This causes us to reset the firmware to FC_MODE and setup the new MAC
753  * address and FC_ID.
754  *
755  * It is also called with FC_ID 0 when we're logged off.
756  *
757  * If the FC_ID is due to point-to-point, fp may be NULL.
758  */
759 void fnic_set_port_id(struct fc_lport *lport, u32 port_id, struct fc_frame *fp)
760 {
761         struct fnic *fnic = lport_priv(lport);
762         u8 *mac;
763         int ret;
764
765         FNIC_FCS_DBG(KERN_DEBUG, lport->host, "set port_id %x fp %p\n",
766                      port_id, fp);
767
768         /*
769          * If we're clearing the FC_ID, change to use the ctl_src_addr.
770          * Set ethernet mode to send FLOGI.
771          */
772         if (!port_id) {
773                 fnic_update_mac(lport, fnic->ctlr.ctl_src_addr);
774                 fnic_set_eth_mode(fnic);
775                 return;
776         }
777
778         if (fp) {
779                 mac = fr_cb(fp)->granted_mac;
780                 if (is_zero_ether_addr(mac)) {
781                         /* non-FIP - FLOGI already accepted - ignore return */
782                         fcoe_ctlr_recv_flogi(&fnic->ctlr, lport, fp);
783                 }
784                 fnic_update_mac(lport, mac);
785         }
786
787         /* Change state to reflect transition to FC mode */
788         spin_lock_irq(&fnic->fnic_lock);
789         if (fnic->state == FNIC_IN_ETH_MODE || fnic->state == FNIC_IN_FC_MODE)
790                 fnic->state = FNIC_IN_ETH_TRANS_FC_MODE;
791         else {
792                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
793                              "Unexpected fnic state %s while"
794                              " processing flogi resp\n",
795                              fnic_state_to_str(fnic->state));
796                 spin_unlock_irq(&fnic->fnic_lock);
797                 return;
798         }
799         spin_unlock_irq(&fnic->fnic_lock);
800
801         /*
802          * Send FLOGI registration to firmware to set up FC mode.
803          * The new address will be set up when registration completes.
804          */
805         ret = fnic_flogi_reg_handler(fnic, port_id);
806
807         if (ret < 0) {
808                 spin_lock_irq(&fnic->fnic_lock);
809                 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE)
810                         fnic->state = FNIC_IN_ETH_MODE;
811                 spin_unlock_irq(&fnic->fnic_lock);
812         }
813 }
814
815 static void fnic_rq_cmpl_frame_recv(struct vnic_rq *rq, struct cq_desc
816                                     *cq_desc, struct vnic_rq_buf *buf,
817                                     int skipped __attribute__((unused)),
818                                     void *opaque)
819 {
820         struct fnic *fnic = vnic_dev_priv(rq->vdev);
821         struct sk_buff *skb;
822         struct fc_frame *fp;
823         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
824         unsigned int eth_hdrs_stripped;
825         u8 type, color, eop, sop, ingress_port, vlan_stripped;
826         u8 fcoe = 0, fcoe_sof, fcoe_eof;
827         u8 fcoe_fc_crc_ok = 1, fcoe_enc_error = 0;
828         u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
829         u8 ipv6, ipv4, ipv4_fragment, rss_type, csum_not_calc;
830         u8 fcs_ok = 1, packet_error = 0;
831         u16 q_number, completed_index, bytes_written = 0, vlan, checksum;
832         u32 rss_hash;
833         u16 exchange_id, tmpl;
834         u8 sof = 0;
835         u8 eof = 0;
836         u32 fcp_bytes_written = 0;
837         unsigned long flags;
838
839         pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
840                          PCI_DMA_FROMDEVICE);
841         skb = buf->os_buf;
842         fp = (struct fc_frame *)skb;
843         buf->os_buf = NULL;
844
845         cq_desc_dec(cq_desc, &type, &color, &q_number, &completed_index);
846         if (type == CQ_DESC_TYPE_RQ_FCP) {
847                 cq_fcp_rq_desc_dec((struct cq_fcp_rq_desc *)cq_desc,
848                                    &type, &color, &q_number, &completed_index,
849                                    &eop, &sop, &fcoe_fc_crc_ok, &exchange_id,
850                                    &tmpl, &fcp_bytes_written, &sof, &eof,
851                                    &ingress_port, &packet_error,
852                                    &fcoe_enc_error, &fcs_ok, &vlan_stripped,
853                                    &vlan);
854                 eth_hdrs_stripped = 1;
855                 skb_trim(skb, fcp_bytes_written);
856                 fr_sof(fp) = sof;
857                 fr_eof(fp) = eof;
858
859         } else if (type == CQ_DESC_TYPE_RQ_ENET) {
860                 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
861                                     &type, &color, &q_number, &completed_index,
862                                     &ingress_port, &fcoe, &eop, &sop,
863                                     &rss_type, &csum_not_calc, &rss_hash,
864                                     &bytes_written, &packet_error,
865                                     &vlan_stripped, &vlan, &checksum,
866                                     &fcoe_sof, &fcoe_fc_crc_ok,
867                                     &fcoe_enc_error, &fcoe_eof,
868                                     &tcp_udp_csum_ok, &udp, &tcp,
869                                     &ipv4_csum_ok, &ipv6, &ipv4,
870                                     &ipv4_fragment, &fcs_ok);
871                 eth_hdrs_stripped = 0;
872                 skb_trim(skb, bytes_written);
873                 if (!fcs_ok) {
874                         atomic64_inc(&fnic_stats->misc_stats.frame_errors);
875                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
876                                      "fcs error.  dropping packet.\n");
877                         goto drop;
878                 }
879                 if (fnic_import_rq_eth_pkt(fnic, skb))
880                         return;
881
882         } else {
883                 /* wrong CQ type*/
884                 shost_printk(KERN_ERR, fnic->lport->host,
885                              "fnic rq_cmpl wrong cq type x%x\n", type);
886                 goto drop;
887         }
888
889         if (!fcs_ok || packet_error || !fcoe_fc_crc_ok || fcoe_enc_error) {
890                 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
891                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
892                              "fnic rq_cmpl fcoe x%x fcsok x%x"
893                              " pkterr x%x fcoe_fc_crc_ok x%x, fcoe_enc_err"
894                              " x%x\n",
895                              fcoe, fcs_ok, packet_error,
896                              fcoe_fc_crc_ok, fcoe_enc_error);
897                 goto drop;
898         }
899
900         spin_lock_irqsave(&fnic->fnic_lock, flags);
901         if (fnic->stop_rx_link_events) {
902                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
903                 goto drop;
904         }
905         fr_dev(fp) = fnic->lport;
906         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
907         if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_RECV,
908                                         (char *)skb->data, skb->len)) != 0) {
909                 printk(KERN_ERR "fnic ctlr frame trace error!!!");
910         }
911
912         skb_queue_tail(&fnic->frame_queue, skb);
913         queue_work(fnic_event_queue, &fnic->frame_work);
914
915         return;
916 drop:
917         dev_kfree_skb_irq(skb);
918 }
919
920 static int fnic_rq_cmpl_handler_cont(struct vnic_dev *vdev,
921                                      struct cq_desc *cq_desc, u8 type,
922                                      u16 q_number, u16 completed_index,
923                                      void *opaque)
924 {
925         struct fnic *fnic = vnic_dev_priv(vdev);
926
927         vnic_rq_service(&fnic->rq[q_number], cq_desc, completed_index,
928                         VNIC_RQ_RETURN_DESC, fnic_rq_cmpl_frame_recv,
929                         NULL);
930         return 0;
931 }
932
933 int fnic_rq_cmpl_handler(struct fnic *fnic, int rq_work_to_do)
934 {
935         unsigned int tot_rq_work_done = 0, cur_work_done;
936         unsigned int i;
937         int err;
938
939         for (i = 0; i < fnic->rq_count; i++) {
940                 cur_work_done = vnic_cq_service(&fnic->cq[i], rq_work_to_do,
941                                                 fnic_rq_cmpl_handler_cont,
942                                                 NULL);
943                 if (cur_work_done) {
944                         err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
945                         if (err)
946                                 shost_printk(KERN_ERR, fnic->lport->host,
947                                              "fnic_alloc_rq_frame can't alloc"
948                                              " frame\n");
949                 }
950                 tot_rq_work_done += cur_work_done;
951         }
952
953         return tot_rq_work_done;
954 }
955
956 /*
957  * This function is called once at init time to allocate and fill RQ
958  * buffers. Subsequently, it is called in the interrupt context after RQ
959  * buffer processing to replenish the buffers in the RQ
960  */
961 int fnic_alloc_rq_frame(struct vnic_rq *rq)
962 {
963         struct fnic *fnic = vnic_dev_priv(rq->vdev);
964         struct sk_buff *skb;
965         u16 len;
966         dma_addr_t pa;
967         int r;
968
969         len = FC_FRAME_HEADROOM + FC_MAX_FRAME + FC_FRAME_TAILROOM;
970         skb = dev_alloc_skb(len);
971         if (!skb) {
972                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
973                              "Unable to allocate RQ sk_buff\n");
974                 return -ENOMEM;
975         }
976         skb_reset_mac_header(skb);
977         skb_reset_transport_header(skb);
978         skb_reset_network_header(skb);
979         skb_put(skb, len);
980         pa = pci_map_single(fnic->pdev, skb->data, len, PCI_DMA_FROMDEVICE);
981
982         if (pci_dma_mapping_error(fnic->pdev, pa)) {
983                 r = -ENOMEM;
984                 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
985                 goto free_skb;
986         }
987
988         fnic_queue_rq_desc(rq, skb, pa, len);
989         return 0;
990
991 free_skb:
992         kfree_skb(skb);
993         return r;
994 }
995
996 void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
997 {
998         struct fc_frame *fp = buf->os_buf;
999         struct fnic *fnic = vnic_dev_priv(rq->vdev);
1000
1001         pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
1002                          PCI_DMA_FROMDEVICE);
1003
1004         dev_kfree_skb(fp_skb(fp));
1005         buf->os_buf = NULL;
1006 }
1007
1008 /**
1009  * fnic_eth_send() - Send Ethernet frame.
1010  * @fip:        fcoe_ctlr instance.
1011  * @skb:        Ethernet Frame, FIP, without VLAN encapsulation.
1012  */
1013 void fnic_eth_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1014 {
1015         struct fnic *fnic = fnic_from_ctlr(fip);
1016         struct vnic_wq *wq = &fnic->wq[0];
1017         dma_addr_t pa;
1018         struct ethhdr *eth_hdr;
1019         struct vlan_ethhdr *vlan_hdr;
1020         unsigned long flags;
1021         int r;
1022
1023         if (!fnic->vlan_hw_insert) {
1024                 eth_hdr = (struct ethhdr *)skb_mac_header(skb);
1025                 vlan_hdr = skb_push(skb, sizeof(*vlan_hdr) - sizeof(*eth_hdr));
1026                 memcpy(vlan_hdr, eth_hdr, 2 * ETH_ALEN);
1027                 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1028                 vlan_hdr->h_vlan_encapsulated_proto = eth_hdr->h_proto;
1029                 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1030                 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
1031                         FNIC_FC_SEND|0x80, (char *)eth_hdr, skb->len)) != 0) {
1032                         printk(KERN_ERR "fnic ctlr frame trace error!!!");
1033                 }
1034         } else {
1035                 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
1036                         FNIC_FC_SEND|0x80, (char *)skb->data, skb->len)) != 0) {
1037                         printk(KERN_ERR "fnic ctlr frame trace error!!!");
1038                 }
1039         }
1040
1041         pa = pci_map_single(fnic->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1042
1043         r = pci_dma_mapping_error(fnic->pdev, pa);
1044         if (r) {
1045                 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
1046                 goto free_skb;
1047         }
1048
1049         spin_lock_irqsave(&fnic->wq_lock[0], flags);
1050         if (!vnic_wq_desc_avail(wq))
1051                 goto irq_restore;
1052
1053         fnic_queue_wq_eth_desc(wq, skb, pa, skb->len,
1054                                0 /* hw inserts cos value */,
1055                                fnic->vlan_id, 1);
1056         spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1057         return;
1058
1059 irq_restore:
1060         spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1061         pci_unmap_single(fnic->pdev, pa, skb->len, PCI_DMA_TODEVICE);
1062 free_skb:
1063         kfree_skb(skb);
1064 }
1065
1066 /*
1067  * Send FC frame.
1068  */
1069 static int fnic_send_frame(struct fnic *fnic, struct fc_frame *fp)
1070 {
1071         struct vnic_wq *wq = &fnic->wq[0];
1072         struct sk_buff *skb;
1073         dma_addr_t pa;
1074         struct ethhdr *eth_hdr;
1075         struct vlan_ethhdr *vlan_hdr;
1076         struct fcoe_hdr *fcoe_hdr;
1077         struct fc_frame_header *fh;
1078         u32 tot_len, eth_hdr_len;
1079         int ret = 0;
1080         unsigned long flags;
1081
1082         fh = fc_frame_header_get(fp);
1083         skb = fp_skb(fp);
1084
1085         if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1086             fcoe_ctlr_els_send(&fnic->ctlr, fnic->lport, skb))
1087                 return 0;
1088
1089         if (!fnic->vlan_hw_insert) {
1090                 eth_hdr_len = sizeof(*vlan_hdr) + sizeof(*fcoe_hdr);
1091                 vlan_hdr = skb_push(skb, eth_hdr_len);
1092                 eth_hdr = (struct ethhdr *)vlan_hdr;
1093                 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1094                 vlan_hdr->h_vlan_encapsulated_proto = htons(ETH_P_FCOE);
1095                 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1096                 fcoe_hdr = (struct fcoe_hdr *)(vlan_hdr + 1);
1097         } else {
1098                 eth_hdr_len = sizeof(*eth_hdr) + sizeof(*fcoe_hdr);
1099                 eth_hdr = skb_push(skb, eth_hdr_len);
1100                 eth_hdr->h_proto = htons(ETH_P_FCOE);
1101                 fcoe_hdr = (struct fcoe_hdr *)(eth_hdr + 1);
1102         }
1103
1104         if (fnic->ctlr.map_dest)
1105                 fc_fcoe_set_mac(eth_hdr->h_dest, fh->fh_d_id);
1106         else
1107                 memcpy(eth_hdr->h_dest, fnic->ctlr.dest_addr, ETH_ALEN);
1108         memcpy(eth_hdr->h_source, fnic->data_src_addr, ETH_ALEN);
1109
1110         tot_len = skb->len;
1111         BUG_ON(tot_len % 4);
1112
1113         memset(fcoe_hdr, 0, sizeof(*fcoe_hdr));
1114         fcoe_hdr->fcoe_sof = fr_sof(fp);
1115         if (FC_FCOE_VER)
1116                 FC_FCOE_ENCAPS_VER(fcoe_hdr, FC_FCOE_VER);
1117
1118         pa = pci_map_single(fnic->pdev, eth_hdr, tot_len, PCI_DMA_TODEVICE);
1119
1120         if (pci_dma_mapping_error(fnic->pdev, pa)) {
1121                 ret = -ENOMEM;
1122                 printk(KERN_ERR "DMA map failed with error %d\n", ret);
1123                 goto free_skb_on_err;
1124         }
1125
1126         if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_SEND,
1127                                 (char *)eth_hdr, tot_len)) != 0) {
1128                 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1129         }
1130
1131         spin_lock_irqsave(&fnic->wq_lock[0], flags);
1132
1133         if (!vnic_wq_desc_avail(wq)) {
1134                 pci_unmap_single(fnic->pdev, pa,
1135                                  tot_len, PCI_DMA_TODEVICE);
1136                 ret = -1;
1137                 goto irq_restore;
1138         }
1139
1140         fnic_queue_wq_desc(wq, skb, pa, tot_len, fr_eof(fp),
1141                            0 /* hw inserts cos value */,
1142                            fnic->vlan_id, 1, 1, 1);
1143
1144 irq_restore:
1145         spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1146
1147 free_skb_on_err:
1148         if (ret)
1149                 dev_kfree_skb_any(fp_skb(fp));
1150
1151         return ret;
1152 }
1153
1154 /*
1155  * fnic_send
1156  * Routine to send a raw frame
1157  */
1158 int fnic_send(struct fc_lport *lp, struct fc_frame *fp)
1159 {
1160         struct fnic *fnic = lport_priv(lp);
1161         unsigned long flags;
1162
1163         if (fnic->in_remove) {
1164                 dev_kfree_skb(fp_skb(fp));
1165                 return -1;
1166         }
1167
1168         /*
1169          * Queue frame if in a transitional state.
1170          * This occurs while registering the Port_ID / MAC address after FLOGI.
1171          */
1172         spin_lock_irqsave(&fnic->fnic_lock, flags);
1173         if (fnic->state != FNIC_IN_FC_MODE && fnic->state != FNIC_IN_ETH_MODE) {
1174                 skb_queue_tail(&fnic->tx_queue, fp_skb(fp));
1175                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1176                 return 0;
1177         }
1178         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1179
1180         return fnic_send_frame(fnic, fp);
1181 }
1182
1183 /**
1184  * fnic_flush_tx() - send queued frames.
1185  * @fnic: fnic device
1186  *
1187  * Send frames that were waiting to go out in FC or Ethernet mode.
1188  * Whenever changing modes we purge queued frames, so these frames should
1189  * be queued for the stable mode that we're in, either FC or Ethernet.
1190  *
1191  * Called without fnic_lock held.
1192  */
1193 void fnic_flush_tx(struct fnic *fnic)
1194 {
1195         struct sk_buff *skb;
1196         struct fc_frame *fp;
1197
1198         while ((skb = skb_dequeue(&fnic->tx_queue))) {
1199                 fp = (struct fc_frame *)skb;
1200                 fnic_send_frame(fnic, fp);
1201         }
1202 }
1203
1204 /**
1205  * fnic_set_eth_mode() - put fnic into ethernet mode.
1206  * @fnic: fnic device
1207  *
1208  * Called without fnic lock held.
1209  */
1210 static void fnic_set_eth_mode(struct fnic *fnic)
1211 {
1212         unsigned long flags;
1213         enum fnic_state old_state;
1214         int ret;
1215
1216         spin_lock_irqsave(&fnic->fnic_lock, flags);
1217 again:
1218         old_state = fnic->state;
1219         switch (old_state) {
1220         case FNIC_IN_FC_MODE:
1221         case FNIC_IN_ETH_TRANS_FC_MODE:
1222         default:
1223                 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1224                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1225
1226                 ret = fnic_fw_reset_handler(fnic);
1227
1228                 spin_lock_irqsave(&fnic->fnic_lock, flags);
1229                 if (fnic->state != FNIC_IN_FC_TRANS_ETH_MODE)
1230                         goto again;
1231                 if (ret)
1232                         fnic->state = old_state;
1233                 break;
1234
1235         case FNIC_IN_FC_TRANS_ETH_MODE:
1236         case FNIC_IN_ETH_MODE:
1237                 break;
1238         }
1239         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1240 }
1241
1242 static void fnic_wq_complete_frame_send(struct vnic_wq *wq,
1243                                         struct cq_desc *cq_desc,
1244                                         struct vnic_wq_buf *buf, void *opaque)
1245 {
1246         struct sk_buff *skb = buf->os_buf;
1247         struct fc_frame *fp = (struct fc_frame *)skb;
1248         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1249
1250         pci_unmap_single(fnic->pdev, buf->dma_addr,
1251                          buf->len, PCI_DMA_TODEVICE);
1252         dev_kfree_skb_irq(fp_skb(fp));
1253         buf->os_buf = NULL;
1254 }
1255
1256 static int fnic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
1257                                      struct cq_desc *cq_desc, u8 type,
1258                                      u16 q_number, u16 completed_index,
1259                                      void *opaque)
1260 {
1261         struct fnic *fnic = vnic_dev_priv(vdev);
1262         unsigned long flags;
1263
1264         spin_lock_irqsave(&fnic->wq_lock[q_number], flags);
1265         vnic_wq_service(&fnic->wq[q_number], cq_desc, completed_index,
1266                         fnic_wq_complete_frame_send, NULL);
1267         spin_unlock_irqrestore(&fnic->wq_lock[q_number], flags);
1268
1269         return 0;
1270 }
1271
1272 int fnic_wq_cmpl_handler(struct fnic *fnic, int work_to_do)
1273 {
1274         unsigned int wq_work_done = 0;
1275         unsigned int i;
1276
1277         for (i = 0; i < fnic->raw_wq_count; i++) {
1278                 wq_work_done  += vnic_cq_service(&fnic->cq[fnic->rq_count+i],
1279                                                  work_to_do,
1280                                                  fnic_wq_cmpl_handler_cont,
1281                                                  NULL);
1282         }
1283
1284         return wq_work_done;
1285 }
1286
1287
1288 void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
1289 {
1290         struct fc_frame *fp = buf->os_buf;
1291         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1292
1293         pci_unmap_single(fnic->pdev, buf->dma_addr,
1294                          buf->len, PCI_DMA_TODEVICE);
1295
1296         dev_kfree_skb(fp_skb(fp));
1297         buf->os_buf = NULL;
1298 }
1299
1300 void fnic_fcoe_reset_vlans(struct fnic *fnic)
1301 {
1302         unsigned long flags;
1303         struct fcoe_vlan *vlan;
1304         struct fcoe_vlan *next;
1305
1306         /*
1307          * indicate a link down to fcoe so that all fcf's are free'd
1308          * might not be required since we did this before sending vlan
1309          * discovery request
1310          */
1311         spin_lock_irqsave(&fnic->vlans_lock, flags);
1312         if (!list_empty(&fnic->vlans)) {
1313                 list_for_each_entry_safe(vlan, next, &fnic->vlans, list) {
1314                         list_del(&vlan->list);
1315                         kfree(vlan);
1316                 }
1317         }
1318         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1319 }
1320
1321 void fnic_handle_fip_timer(struct fnic *fnic)
1322 {
1323         unsigned long flags;
1324         struct fcoe_vlan *vlan;
1325         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1326         u64 sol_time;
1327
1328         spin_lock_irqsave(&fnic->fnic_lock, flags);
1329         if (fnic->stop_rx_link_events) {
1330                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1331                 return;
1332         }
1333         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1334
1335         if (fnic->ctlr.mode == FIP_MODE_NON_FIP)
1336                 return;
1337
1338         spin_lock_irqsave(&fnic->vlans_lock, flags);
1339         if (list_empty(&fnic->vlans)) {
1340                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1341                 /* no vlans available, try again */
1342                 if (printk_ratelimit())
1343                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1344                                   "Start VLAN Discovery\n");
1345                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1346                 return;
1347         }
1348
1349         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
1350         shost_printk(KERN_DEBUG, fnic->lport->host,
1351                   "fip_timer: vlan %d state %d sol_count %d\n",
1352                   vlan->vid, vlan->state, vlan->sol_count);
1353         switch (vlan->state) {
1354         case FIP_VLAN_USED:
1355                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1356                           "FIP VLAN is selected for FC transaction\n");
1357                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1358                 break;
1359         case FIP_VLAN_FAILED:
1360                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1361                 /* if all vlans are in failed state, restart vlan disc */
1362                 if (printk_ratelimit())
1363                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1364                                   "Start VLAN Discovery\n");
1365                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1366                 break;
1367         case FIP_VLAN_SENT:
1368                 if (vlan->sol_count >= FCOE_CTLR_MAX_SOL) {
1369                         /*
1370                          * no response on this vlan, remove  from the list.
1371                          * Try the next vlan
1372                          */
1373                         shost_printk(KERN_INFO, fnic->lport->host,
1374                                   "Dequeue this VLAN ID %d from list\n",
1375                                   vlan->vid);
1376                         list_del(&vlan->list);
1377                         kfree(vlan);
1378                         vlan = NULL;
1379                         if (list_empty(&fnic->vlans)) {
1380                                 /* we exhausted all vlans, restart vlan disc */
1381                                 spin_unlock_irqrestore(&fnic->vlans_lock,
1382                                                         flags);
1383                                 shost_printk(KERN_INFO, fnic->lport->host,
1384                                           "fip_timer: vlan list empty, "
1385                                           "trigger vlan disc\n");
1386                                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1387                                 return;
1388                         }
1389                         /* check the next vlan */
1390                         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan,
1391                                                         list);
1392                         fnic->set_vlan(fnic, vlan->vid);
1393                         vlan->state = FIP_VLAN_SENT; /* sent now */
1394                 }
1395                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1396                 atomic64_inc(&fnic_stats->vlan_stats.sol_expiry_count);
1397                 vlan->sol_count++;
1398                 sol_time = jiffies + msecs_to_jiffies
1399                                         (FCOE_CTLR_START_DELAY);
1400                 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
1401                 break;
1402         }
1403 }