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