2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
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.
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
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>
35 #include "cq_enet_desc.h"
36 #include "cq_exch_desc.h"
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;
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);
49 void fnic_handle_link(struct work_struct *work)
51 struct fnic *fnic = container_of(work, struct fnic, link_work);
54 u32 old_link_down_cnt;
56 spin_lock_irqsave(&fnic->fnic_lock, flags);
58 if (fnic->stop_rx_link_events) {
59 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
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);
68 if (old_link_status == fnic->link_status) {
69 if (!fnic->link_status) {
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"));
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,
83 "Link Status:UP_DOWN_UP",
84 strlen("Link_Status:UP_DOWN_UP")
86 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
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,
94 "Link Status: UP_DOWN_UP_VLAN",
96 "Link Status: UP_DOWN_UP_VLAN")
98 fnic_fcoe_send_vlan_req(fnic);
101 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
103 fcoe_ctlr_link_up(&fnic->ctlr);
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"));
113 } else if (fnic->link_status) {
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);
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);
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);
143 fcoe_ctlr_link_down(&fnic->ctlr);
149 * This function passes incoming fabric frames to libFC
151 void fnic_handle_frame(struct work_struct *work)
153 struct fnic *fnic = container_of(work, struct fnic, frame_work);
154 struct fc_lport *lp = fnic->lport;
159 while ((skb = skb_dequeue(&fnic->frame_queue))) {
161 spin_lock_irqsave(&fnic->fnic_lock, flags);
162 if (fnic->stop_rx_link_events) {
163 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
167 fp = (struct fc_frame *)skb;
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.
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);
179 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
181 fc_exch_recv(lp, fp);
185 void fnic_fcoe_evlist_free(struct fnic *fnic)
187 struct fnic_event *fevt = NULL;
188 struct fnic_event *next = NULL;
191 spin_lock_irqsave(&fnic->fnic_lock, flags);
192 if (list_empty(&fnic->evlist)) {
193 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
197 list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
198 list_del(&fevt->list);
201 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
204 void fnic_handle_event(struct work_struct *work)
206 struct fnic *fnic = container_of(work, struct fnic, event_work);
207 struct fnic_event *fevt = NULL;
208 struct fnic_event *next = NULL;
211 spin_lock_irqsave(&fnic->fnic_lock, flags);
212 if (list_empty(&fnic->evlist)) {
213 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
217 list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
218 if (fnic->stop_rx_link_events) {
219 list_del(&fevt->list);
221 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
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.
228 if (fnic->state != FNIC_IN_FC_MODE &&
229 fnic->state != FNIC_IN_ETH_MODE) {
230 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
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);
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);
247 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
248 "Unknown event 0x%x\n", fevt->event);
253 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
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
261 * Returns non-zero if the frame is rejected with unsupported cmd with
262 * insufficient resource els explanation.
264 static inline int is_fnic_fip_flogi_reject(struct fcoe_ctlr *fip,
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;
281 if (skb_linearize(skb))
284 if (skb->len < sizeof(*fiph))
287 fiph = (struct fip_header *)skb->data;
288 op = ntohs(fiph->fip_op);
289 sub = fiph->fip_subcode;
294 if (sub != FIP_SC_REP)
297 rlen = ntohs(fiph->fip_dl_len) * 4;
298 if (rlen + sizeof(*fiph) > skb->len)
301 desc = (struct fip_desc *)(fiph + 1);
302 dlen = desc->fip_dlen * FIP_BPW;
304 if (desc->fip_dtype == FIP_DT_FLOGI) {
306 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
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;
318 * ELS command code, reason and explanation should be = Reject,
319 * unsupported command and insufficient resource
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");
327 shost_printk(KERN_INFO, lport->host,
328 "Flogi Request Accepted by Switch\n");
333 static void fnic_fcoe_send_vlan_req(struct fnic *fnic)
335 struct fcoe_ctlr *fip = &fnic->ctlr;
336 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
340 struct fip_vlan *vlan;
343 fnic_fcoe_reset_vlans(fnic);
344 fnic->set_vlan(fnic, 0);
346 if (printk_ratelimit())
347 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
348 "Sending VLAN request...\n");
350 skb = dev_alloc_skb(sizeof(struct fip_vlan));
354 fr_len = sizeof(*vlan);
355 eth_fr = (char *)skb->data;
356 vlan = (struct fip_vlan *)eth_fr;
358 memset(vlan, 0, sizeof(*vlan));
359 memcpy(vlan->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
360 memcpy(vlan->eth.h_dest, fcoe_all_fcfs, ETH_ALEN);
361 vlan->eth.h_proto = htons(ETH_P_FIP);
363 vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
364 vlan->fip.fip_op = htons(FIP_OP_VLAN);
365 vlan->fip.fip_subcode = FIP_SC_VL_REQ;
366 vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
368 vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
369 vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
370 memcpy(&vlan->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
372 vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
373 vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
374 put_unaligned_be64(fip->lp->wwnn, &vlan->desc.wwnn.fd_wwn);
375 atomic64_inc(&fnic_stats->vlan_stats.vlan_disc_reqs);
377 skb_put(skb, sizeof(*vlan));
378 skb->protocol = htons(ETH_P_FIP);
379 skb_reset_mac_header(skb);
380 skb_reset_network_header(skb);
383 /* set a timer so that we can retry if there no response */
384 vlan_tov = jiffies + msecs_to_jiffies(FCOE_CTLR_FIPVLAN_TOV);
385 mod_timer(&fnic->fip_timer, round_jiffies(vlan_tov));
388 static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *skb)
390 struct fcoe_ctlr *fip = &fnic->ctlr;
391 struct fip_header *fiph;
392 struct fip_desc *desc;
393 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
397 struct fcoe_vlan *vlan;
401 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
402 "Received VLAN response...\n");
404 fiph = (struct fip_header *) skb->data;
406 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
407 "Received VLAN response... OP 0x%x SUB_OP 0x%x\n",
408 ntohs(fiph->fip_op), fiph->fip_subcode);
410 rlen = ntohs(fiph->fip_dl_len) * 4;
411 fnic_fcoe_reset_vlans(fnic);
412 spin_lock_irqsave(&fnic->vlans_lock, flags);
413 desc = (struct fip_desc *)(fiph + 1);
415 dlen = desc->fip_dlen * FIP_BPW;
416 switch (desc->fip_dtype) {
418 vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
419 shost_printk(KERN_INFO, fnic->lport->host,
420 "process_vlan_resp: FIP VLAN %d\n", vid);
421 vlan = kmalloc(sizeof(*vlan),
424 /* retry from timer */
425 spin_unlock_irqrestore(&fnic->vlans_lock,
429 memset(vlan, 0, sizeof(struct fcoe_vlan));
430 vlan->vid = vid & 0x0fff;
431 vlan->state = FIP_VLAN_AVAIL;
432 list_add_tail(&vlan->list, &fnic->vlans);
435 desc = (struct fip_desc *)((char *)desc + dlen);
439 /* any VLAN descriptors present ? */
440 if (list_empty(&fnic->vlans)) {
441 /* retry from timer */
442 atomic64_inc(&fnic_stats->vlan_stats.resp_withno_vlanID);
443 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
444 "No VLAN descriptors in FIP VLAN response\n");
445 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
449 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
450 fnic->set_vlan(fnic, vlan->vid);
451 vlan->state = FIP_VLAN_SENT; /* sent now */
453 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
455 /* start the solicitation */
456 fcoe_ctlr_link_up(fip);
458 sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
459 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
464 static void fnic_fcoe_start_fcf_disc(struct fnic *fnic)
467 struct fcoe_vlan *vlan;
470 spin_lock_irqsave(&fnic->vlans_lock, flags);
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 */
475 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
477 /* start the solicitation */
478 fcoe_ctlr_link_up(&fnic->ctlr);
480 sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
481 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
484 static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag)
487 struct fcoe_vlan *fvlan;
489 spin_lock_irqsave(&fnic->vlans_lock, flags);
490 if (list_empty(&fnic->vlans)) {
491 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
495 fvlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
496 if (fvlan->state == FIP_VLAN_USED) {
497 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
501 if (fvlan->state == FIP_VLAN_SENT) {
502 fvlan->state = FIP_VLAN_USED;
503 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
506 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
510 static void fnic_event_enq(struct fnic *fnic, enum fnic_evt ev)
512 struct fnic_event *fevt;
515 fevt = kmalloc(sizeof(*fevt), GFP_ATOMIC);
522 spin_lock_irqsave(&fnic->fnic_lock, flags);
523 list_add_tail(&fevt->list, &fnic->evlist);
524 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
526 schedule_work(&fnic->event_work);
529 static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb)
531 struct fip_header *fiph;
536 if (!skb || !(skb->data))
539 if (skb_linearize(skb))
542 fiph = (struct fip_header *)skb->data;
543 op = ntohs(fiph->fip_op);
544 sub = fiph->fip_subcode;
546 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
549 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
552 if (op == FIP_OP_DISC && sub == FIP_SC_ADV) {
553 if (fnic_fcoe_vlan_check(fnic, ntohs(fiph->fip_flags)))
555 /* pass it on to fcoe */
557 } else if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
558 /* set the vlan as used */
559 fnic_fcoe_process_vlan_resp(fnic, skb);
561 } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
562 /* received CVL request, restart vlan disc */
563 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
564 /* pass it on to fcoe */
571 void fnic_handle_fip_frame(struct work_struct *work)
573 struct fnic *fnic = container_of(work, struct fnic, fip_frame_work);
574 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
579 while ((skb = skb_dequeue(&fnic->fip_frame_queue))) {
580 spin_lock_irqsave(&fnic->fnic_lock, flags);
581 if (fnic->stop_rx_link_events) {
582 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
587 * If we're in a transitional state, just re-queue and return.
588 * The queue will be serviced when we get to a stable state.
590 if (fnic->state != FNIC_IN_FC_MODE &&
591 fnic->state != FNIC_IN_ETH_MODE) {
592 skb_queue_head(&fnic->fip_frame_queue, skb);
593 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
596 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
597 eh = (struct ethhdr *)skb->data;
598 if (eh->h_proto == htons(ETH_P_FIP)) {
599 skb_pull(skb, sizeof(*eh));
600 if (fnic_fcoe_handle_fip_frame(fnic, skb) <= 0) {
605 * If there's FLOGI rejects - clear all
606 * fcf's & restart from scratch
608 if (is_fnic_fip_flogi_reject(&fnic->ctlr, skb)) {
610 &fnic_stats->vlan_stats.flogi_rejects);
611 shost_printk(KERN_INFO, fnic->lport->host,
612 "Trigger a Link down - VLAN Disc\n");
613 fcoe_ctlr_link_down(&fnic->ctlr);
614 /* start FCoE VLAN discovery */
615 fnic_fcoe_send_vlan_req(fnic);
619 fcoe_ctlr_recv(&fnic->ctlr, skb);
626 * fnic_import_rq_eth_pkt() - handle received FCoE or FIP frame.
627 * @fnic: fnic instance.
628 * @skb: Ethernet Frame.
630 static inline int fnic_import_rq_eth_pkt(struct fnic *fnic, struct sk_buff *skb)
634 struct fcoe_hdr *fcoe_hdr;
635 struct fcoe_crc_eof *ft;
638 * Undo VLAN encapsulation if present.
640 eh = (struct ethhdr *)skb->data;
641 if (eh->h_proto == htons(ETH_P_8021Q)) {
642 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
643 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
644 skb_reset_mac_header(skb);
646 if (eh->h_proto == htons(ETH_P_FIP)) {
647 if (!(fnic->config.flags & VFCF_FIP_CAPABLE)) {
648 printk(KERN_ERR "Dropped FIP frame, as firmware "
649 "uses non-FIP mode, Enable FIP "
653 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
654 FNIC_FC_RECV|0x80, (char *)skb->data, skb->len)) != 0) {
655 printk(KERN_ERR "fnic ctlr frame trace error!!!");
657 skb_queue_tail(&fnic->fip_frame_queue, skb);
658 queue_work(fnic_fip_queue, &fnic->fip_frame_work);
659 return 1; /* let caller know packet was used */
661 if (eh->h_proto != htons(ETH_P_FCOE))
663 skb_set_network_header(skb, sizeof(*eh));
664 skb_pull(skb, sizeof(*eh));
666 fcoe_hdr = (struct fcoe_hdr *)skb->data;
667 if (FC_FCOE_DECAPS_VER(fcoe_hdr) != FC_FCOE_VER)
670 fp = (struct fc_frame *)skb;
672 fr_sof(fp) = fcoe_hdr->fcoe_sof;
673 skb_pull(skb, sizeof(struct fcoe_hdr));
674 skb_reset_transport_header(skb);
676 ft = (struct fcoe_crc_eof *)(skb->data + skb->len - sizeof(*ft));
677 fr_eof(fp) = ft->fcoe_eof;
678 skb_trim(skb, skb->len - sizeof(*ft));
681 dev_kfree_skb_irq(skb);
686 * fnic_update_mac_locked() - set data MAC address and filters.
687 * @fnic: fnic instance.
688 * @new: newly-assigned FCoE MAC address.
690 * Called with the fnic lock held.
692 void fnic_update_mac_locked(struct fnic *fnic, u8 *new)
694 u8 *ctl = fnic->ctlr.ctl_src_addr;
695 u8 *data = fnic->data_src_addr;
697 if (is_zero_ether_addr(new))
699 if (ether_addr_equal(data, new))
701 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "update_mac %pM\n", new);
702 if (!is_zero_ether_addr(data) && !ether_addr_equal(data, ctl))
703 vnic_dev_del_addr(fnic->vdev, data);
704 memcpy(data, new, ETH_ALEN);
705 if (!ether_addr_equal(new, ctl))
706 vnic_dev_add_addr(fnic->vdev, new);
710 * fnic_update_mac() - set data MAC address and filters.
711 * @lport: local port.
712 * @new: newly-assigned FCoE MAC address.
714 void fnic_update_mac(struct fc_lport *lport, u8 *new)
716 struct fnic *fnic = lport_priv(lport);
718 spin_lock_irq(&fnic->fnic_lock);
719 fnic_update_mac_locked(fnic, new);
720 spin_unlock_irq(&fnic->fnic_lock);
724 * fnic_set_port_id() - set the port_ID after successful FLOGI.
725 * @lport: local port.
726 * @port_id: assigned FC_ID.
727 * @fp: received frame containing the FLOGI accept or NULL.
729 * This is called from libfc when a new FC_ID has been assigned.
730 * This causes us to reset the firmware to FC_MODE and setup the new MAC
733 * It is also called with FC_ID 0 when we're logged off.
735 * If the FC_ID is due to point-to-point, fp may be NULL.
737 void fnic_set_port_id(struct fc_lport *lport, u32 port_id, struct fc_frame *fp)
739 struct fnic *fnic = lport_priv(lport);
743 FNIC_FCS_DBG(KERN_DEBUG, lport->host, "set port_id %x fp %p\n",
747 * If we're clearing the FC_ID, change to use the ctl_src_addr.
748 * Set ethernet mode to send FLOGI.
751 fnic_update_mac(lport, fnic->ctlr.ctl_src_addr);
752 fnic_set_eth_mode(fnic);
757 mac = fr_cb(fp)->granted_mac;
758 if (is_zero_ether_addr(mac)) {
759 /* non-FIP - FLOGI already accepted - ignore return */
760 fcoe_ctlr_recv_flogi(&fnic->ctlr, lport, fp);
762 fnic_update_mac(lport, mac);
765 /* Change state to reflect transition to FC mode */
766 spin_lock_irq(&fnic->fnic_lock);
767 if (fnic->state == FNIC_IN_ETH_MODE || fnic->state == FNIC_IN_FC_MODE)
768 fnic->state = FNIC_IN_ETH_TRANS_FC_MODE;
770 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
771 "Unexpected fnic state %s while"
772 " processing flogi resp\n",
773 fnic_state_to_str(fnic->state));
774 spin_unlock_irq(&fnic->fnic_lock);
777 spin_unlock_irq(&fnic->fnic_lock);
780 * Send FLOGI registration to firmware to set up FC mode.
781 * The new address will be set up when registration completes.
783 ret = fnic_flogi_reg_handler(fnic, port_id);
786 spin_lock_irq(&fnic->fnic_lock);
787 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE)
788 fnic->state = FNIC_IN_ETH_MODE;
789 spin_unlock_irq(&fnic->fnic_lock);
793 static void fnic_rq_cmpl_frame_recv(struct vnic_rq *rq, struct cq_desc
794 *cq_desc, struct vnic_rq_buf *buf,
795 int skipped __attribute__((unused)),
798 struct fnic *fnic = vnic_dev_priv(rq->vdev);
801 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
802 unsigned int eth_hdrs_stripped;
803 u8 type, color, eop, sop, ingress_port, vlan_stripped;
804 u8 fcoe = 0, fcoe_sof, fcoe_eof;
805 u8 fcoe_fc_crc_ok = 1, fcoe_enc_error = 0;
806 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
807 u8 ipv6, ipv4, ipv4_fragment, rss_type, csum_not_calc;
808 u8 fcs_ok = 1, packet_error = 0;
809 u16 q_number, completed_index, bytes_written = 0, vlan, checksum;
811 u16 exchange_id, tmpl;
814 u32 fcp_bytes_written = 0;
817 pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
820 fp = (struct fc_frame *)skb;
823 cq_desc_dec(cq_desc, &type, &color, &q_number, &completed_index);
824 if (type == CQ_DESC_TYPE_RQ_FCP) {
825 cq_fcp_rq_desc_dec((struct cq_fcp_rq_desc *)cq_desc,
826 &type, &color, &q_number, &completed_index,
827 &eop, &sop, &fcoe_fc_crc_ok, &exchange_id,
828 &tmpl, &fcp_bytes_written, &sof, &eof,
829 &ingress_port, &packet_error,
830 &fcoe_enc_error, &fcs_ok, &vlan_stripped,
832 eth_hdrs_stripped = 1;
833 skb_trim(skb, fcp_bytes_written);
837 } else if (type == CQ_DESC_TYPE_RQ_ENET) {
838 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
839 &type, &color, &q_number, &completed_index,
840 &ingress_port, &fcoe, &eop, &sop,
841 &rss_type, &csum_not_calc, &rss_hash,
842 &bytes_written, &packet_error,
843 &vlan_stripped, &vlan, &checksum,
844 &fcoe_sof, &fcoe_fc_crc_ok,
845 &fcoe_enc_error, &fcoe_eof,
846 &tcp_udp_csum_ok, &udp, &tcp,
847 &ipv4_csum_ok, &ipv6, &ipv4,
848 &ipv4_fragment, &fcs_ok);
849 eth_hdrs_stripped = 0;
850 skb_trim(skb, bytes_written);
852 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
853 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
854 "fcs error. dropping packet.\n");
857 if (fnic_import_rq_eth_pkt(fnic, skb))
862 shost_printk(KERN_ERR, fnic->lport->host,
863 "fnic rq_cmpl wrong cq type x%x\n", type);
867 if (!fcs_ok || packet_error || !fcoe_fc_crc_ok || fcoe_enc_error) {
868 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
869 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
870 "fnic rq_cmpl fcoe x%x fcsok x%x"
871 " pkterr x%x fcoe_fc_crc_ok x%x, fcoe_enc_err"
873 fcoe, fcs_ok, packet_error,
874 fcoe_fc_crc_ok, fcoe_enc_error);
878 spin_lock_irqsave(&fnic->fnic_lock, flags);
879 if (fnic->stop_rx_link_events) {
880 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
883 fr_dev(fp) = fnic->lport;
884 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
885 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_RECV,
886 (char *)skb->data, skb->len)) != 0) {
887 printk(KERN_ERR "fnic ctlr frame trace error!!!");
890 skb_queue_tail(&fnic->frame_queue, skb);
891 queue_work(fnic_event_queue, &fnic->frame_work);
895 dev_kfree_skb_irq(skb);
898 static int fnic_rq_cmpl_handler_cont(struct vnic_dev *vdev,
899 struct cq_desc *cq_desc, u8 type,
900 u16 q_number, u16 completed_index,
903 struct fnic *fnic = vnic_dev_priv(vdev);
905 vnic_rq_service(&fnic->rq[q_number], cq_desc, completed_index,
906 VNIC_RQ_RETURN_DESC, fnic_rq_cmpl_frame_recv,
911 int fnic_rq_cmpl_handler(struct fnic *fnic, int rq_work_to_do)
913 unsigned int tot_rq_work_done = 0, cur_work_done;
917 for (i = 0; i < fnic->rq_count; i++) {
918 cur_work_done = vnic_cq_service(&fnic->cq[i], rq_work_to_do,
919 fnic_rq_cmpl_handler_cont,
922 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
924 shost_printk(KERN_ERR, fnic->lport->host,
925 "fnic_alloc_rq_frame can't alloc"
928 tot_rq_work_done += cur_work_done;
931 return tot_rq_work_done;
935 * This function is called once at init time to allocate and fill RQ
936 * buffers. Subsequently, it is called in the interrupt context after RQ
937 * buffer processing to replenish the buffers in the RQ
939 int fnic_alloc_rq_frame(struct vnic_rq *rq)
941 struct fnic *fnic = vnic_dev_priv(rq->vdev);
947 len = FC_FRAME_HEADROOM + FC_MAX_FRAME + FC_FRAME_TAILROOM;
948 skb = dev_alloc_skb(len);
950 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
951 "Unable to allocate RQ sk_buff\n");
954 skb_reset_mac_header(skb);
955 skb_reset_transport_header(skb);
956 skb_reset_network_header(skb);
958 pa = pci_map_single(fnic->pdev, skb->data, len, PCI_DMA_FROMDEVICE);
960 if (pci_dma_mapping_error(fnic->pdev, pa)) {
962 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
966 fnic_queue_rq_desc(rq, skb, pa, len);
974 void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
976 struct fc_frame *fp = buf->os_buf;
977 struct fnic *fnic = vnic_dev_priv(rq->vdev);
979 pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
982 dev_kfree_skb(fp_skb(fp));
987 * fnic_eth_send() - Send Ethernet frame.
988 * @fip: fcoe_ctlr instance.
989 * @skb: Ethernet Frame, FIP, without VLAN encapsulation.
991 void fnic_eth_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
993 struct fnic *fnic = fnic_from_ctlr(fip);
994 struct vnic_wq *wq = &fnic->wq[0];
996 struct ethhdr *eth_hdr;
997 struct vlan_ethhdr *vlan_hdr;
1001 if (!fnic->vlan_hw_insert) {
1002 eth_hdr = (struct ethhdr *)skb_mac_header(skb);
1003 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb,
1004 sizeof(*vlan_hdr) - sizeof(*eth_hdr));
1005 memcpy(vlan_hdr, eth_hdr, 2 * ETH_ALEN);
1006 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1007 vlan_hdr->h_vlan_encapsulated_proto = eth_hdr->h_proto;
1008 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1009 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
1010 FNIC_FC_SEND|0x80, (char *)eth_hdr, skb->len)) != 0) {
1011 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1014 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
1015 FNIC_FC_SEND|0x80, (char *)skb->data, skb->len)) != 0) {
1016 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1020 pa = pci_map_single(fnic->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1022 r = pci_dma_mapping_error(fnic->pdev, pa);
1024 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
1028 spin_lock_irqsave(&fnic->wq_lock[0], flags);
1029 if (!vnic_wq_desc_avail(wq))
1032 fnic_queue_wq_eth_desc(wq, skb, pa, skb->len,
1033 0 /* hw inserts cos value */,
1035 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1039 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1040 pci_unmap_single(fnic->pdev, pa, skb->len, PCI_DMA_TODEVICE);
1048 static int fnic_send_frame(struct fnic *fnic, struct fc_frame *fp)
1050 struct vnic_wq *wq = &fnic->wq[0];
1051 struct sk_buff *skb;
1053 struct ethhdr *eth_hdr;
1054 struct vlan_ethhdr *vlan_hdr;
1055 struct fcoe_hdr *fcoe_hdr;
1056 struct fc_frame_header *fh;
1057 u32 tot_len, eth_hdr_len;
1059 unsigned long flags;
1061 fh = fc_frame_header_get(fp);
1064 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1065 fcoe_ctlr_els_send(&fnic->ctlr, fnic->lport, skb))
1068 if (!fnic->vlan_hw_insert) {
1069 eth_hdr_len = sizeof(*vlan_hdr) + sizeof(*fcoe_hdr);
1070 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb, eth_hdr_len);
1071 eth_hdr = (struct ethhdr *)vlan_hdr;
1072 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1073 vlan_hdr->h_vlan_encapsulated_proto = htons(ETH_P_FCOE);
1074 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1075 fcoe_hdr = (struct fcoe_hdr *)(vlan_hdr + 1);
1077 eth_hdr_len = sizeof(*eth_hdr) + sizeof(*fcoe_hdr);
1078 eth_hdr = (struct ethhdr *)skb_push(skb, eth_hdr_len);
1079 eth_hdr->h_proto = htons(ETH_P_FCOE);
1080 fcoe_hdr = (struct fcoe_hdr *)(eth_hdr + 1);
1083 if (fnic->ctlr.map_dest)
1084 fc_fcoe_set_mac(eth_hdr->h_dest, fh->fh_d_id);
1086 memcpy(eth_hdr->h_dest, fnic->ctlr.dest_addr, ETH_ALEN);
1087 memcpy(eth_hdr->h_source, fnic->data_src_addr, ETH_ALEN);
1090 BUG_ON(tot_len % 4);
1092 memset(fcoe_hdr, 0, sizeof(*fcoe_hdr));
1093 fcoe_hdr->fcoe_sof = fr_sof(fp);
1095 FC_FCOE_ENCAPS_VER(fcoe_hdr, FC_FCOE_VER);
1097 pa = pci_map_single(fnic->pdev, eth_hdr, tot_len, PCI_DMA_TODEVICE);
1099 if (pci_dma_mapping_error(fnic->pdev, pa)) {
1101 printk(KERN_ERR "DMA map failed with error %d\n", ret);
1102 goto free_skb_on_err;
1105 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_SEND,
1106 (char *)eth_hdr, tot_len)) != 0) {
1107 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1110 spin_lock_irqsave(&fnic->wq_lock[0], flags);
1112 if (!vnic_wq_desc_avail(wq)) {
1113 pci_unmap_single(fnic->pdev, pa,
1114 tot_len, PCI_DMA_TODEVICE);
1119 fnic_queue_wq_desc(wq, skb, pa, tot_len, fr_eof(fp),
1120 0 /* hw inserts cos value */,
1121 fnic->vlan_id, 1, 1, 1);
1124 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1128 dev_kfree_skb_any(fp_skb(fp));
1135 * Routine to send a raw frame
1137 int fnic_send(struct fc_lport *lp, struct fc_frame *fp)
1139 struct fnic *fnic = lport_priv(lp);
1140 unsigned long flags;
1142 if (fnic->in_remove) {
1143 dev_kfree_skb(fp_skb(fp));
1148 * Queue frame if in a transitional state.
1149 * This occurs while registering the Port_ID / MAC address after FLOGI.
1151 spin_lock_irqsave(&fnic->fnic_lock, flags);
1152 if (fnic->state != FNIC_IN_FC_MODE && fnic->state != FNIC_IN_ETH_MODE) {
1153 skb_queue_tail(&fnic->tx_queue, fp_skb(fp));
1154 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1157 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1159 return fnic_send_frame(fnic, fp);
1163 * fnic_flush_tx() - send queued frames.
1164 * @fnic: fnic device
1166 * Send frames that were waiting to go out in FC or Ethernet mode.
1167 * Whenever changing modes we purge queued frames, so these frames should
1168 * be queued for the stable mode that we're in, either FC or Ethernet.
1170 * Called without fnic_lock held.
1172 void fnic_flush_tx(struct fnic *fnic)
1174 struct sk_buff *skb;
1175 struct fc_frame *fp;
1177 while ((skb = skb_dequeue(&fnic->tx_queue))) {
1178 fp = (struct fc_frame *)skb;
1179 fnic_send_frame(fnic, fp);
1184 * fnic_set_eth_mode() - put fnic into ethernet mode.
1185 * @fnic: fnic device
1187 * Called without fnic lock held.
1189 static void fnic_set_eth_mode(struct fnic *fnic)
1191 unsigned long flags;
1192 enum fnic_state old_state;
1195 spin_lock_irqsave(&fnic->fnic_lock, flags);
1197 old_state = fnic->state;
1198 switch (old_state) {
1199 case FNIC_IN_FC_MODE:
1200 case FNIC_IN_ETH_TRANS_FC_MODE:
1202 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1203 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1205 ret = fnic_fw_reset_handler(fnic);
1207 spin_lock_irqsave(&fnic->fnic_lock, flags);
1208 if (fnic->state != FNIC_IN_FC_TRANS_ETH_MODE)
1211 fnic->state = old_state;
1214 case FNIC_IN_FC_TRANS_ETH_MODE:
1215 case FNIC_IN_ETH_MODE:
1218 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1221 static void fnic_wq_complete_frame_send(struct vnic_wq *wq,
1222 struct cq_desc *cq_desc,
1223 struct vnic_wq_buf *buf, void *opaque)
1225 struct sk_buff *skb = buf->os_buf;
1226 struct fc_frame *fp = (struct fc_frame *)skb;
1227 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1229 pci_unmap_single(fnic->pdev, buf->dma_addr,
1230 buf->len, PCI_DMA_TODEVICE);
1231 dev_kfree_skb_irq(fp_skb(fp));
1235 static int fnic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
1236 struct cq_desc *cq_desc, u8 type,
1237 u16 q_number, u16 completed_index,
1240 struct fnic *fnic = vnic_dev_priv(vdev);
1241 unsigned long flags;
1243 spin_lock_irqsave(&fnic->wq_lock[q_number], flags);
1244 vnic_wq_service(&fnic->wq[q_number], cq_desc, completed_index,
1245 fnic_wq_complete_frame_send, NULL);
1246 spin_unlock_irqrestore(&fnic->wq_lock[q_number], flags);
1251 int fnic_wq_cmpl_handler(struct fnic *fnic, int work_to_do)
1253 unsigned int wq_work_done = 0;
1256 for (i = 0; i < fnic->raw_wq_count; i++) {
1257 wq_work_done += vnic_cq_service(&fnic->cq[fnic->rq_count+i],
1259 fnic_wq_cmpl_handler_cont,
1263 return wq_work_done;
1267 void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
1269 struct fc_frame *fp = buf->os_buf;
1270 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1272 pci_unmap_single(fnic->pdev, buf->dma_addr,
1273 buf->len, PCI_DMA_TODEVICE);
1275 dev_kfree_skb(fp_skb(fp));
1279 void fnic_fcoe_reset_vlans(struct fnic *fnic)
1281 unsigned long flags;
1282 struct fcoe_vlan *vlan;
1283 struct fcoe_vlan *next;
1286 * indicate a link down to fcoe so that all fcf's are free'd
1287 * might not be required since we did this before sending vlan
1290 spin_lock_irqsave(&fnic->vlans_lock, flags);
1291 if (!list_empty(&fnic->vlans)) {
1292 list_for_each_entry_safe(vlan, next, &fnic->vlans, list) {
1293 list_del(&vlan->list);
1297 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1300 void fnic_handle_fip_timer(struct fnic *fnic)
1302 unsigned long flags;
1303 struct fcoe_vlan *vlan;
1304 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1307 spin_lock_irqsave(&fnic->fnic_lock, flags);
1308 if (fnic->stop_rx_link_events) {
1309 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1312 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1314 if (fnic->ctlr.mode == FIP_MODE_NON_FIP)
1317 spin_lock_irqsave(&fnic->vlans_lock, flags);
1318 if (list_empty(&fnic->vlans)) {
1319 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1320 /* no vlans available, try again */
1321 if (printk_ratelimit())
1322 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1323 "Start VLAN Discovery\n");
1324 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1328 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
1329 shost_printk(KERN_DEBUG, fnic->lport->host,
1330 "fip_timer: vlan %d state %d sol_count %d\n",
1331 vlan->vid, vlan->state, vlan->sol_count);
1332 switch (vlan->state) {
1334 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1335 "FIP VLAN is selected for FC transaction\n");
1336 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1338 case FIP_VLAN_FAILED:
1339 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1340 /* if all vlans are in failed state, restart vlan disc */
1341 if (printk_ratelimit())
1342 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1343 "Start VLAN Discovery\n");
1344 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1347 if (vlan->sol_count >= FCOE_CTLR_MAX_SOL) {
1349 * no response on this vlan, remove from the list.
1352 shost_printk(KERN_INFO, fnic->lport->host,
1353 "Dequeue this VLAN ID %d from list\n",
1355 list_del(&vlan->list);
1358 if (list_empty(&fnic->vlans)) {
1359 /* we exhausted all vlans, restart vlan disc */
1360 spin_unlock_irqrestore(&fnic->vlans_lock,
1362 shost_printk(KERN_INFO, fnic->lport->host,
1363 "fip_timer: vlan list empty, "
1364 "trigger vlan disc\n");
1365 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1368 /* check the next vlan */
1369 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan,
1371 fnic->set_vlan(fnic, vlan->vid);
1372 vlan->state = FIP_VLAN_SENT; /* sent now */
1374 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1375 atomic64_inc(&fnic_stats->vlan_stats.sol_expiry_count);
1377 sol_time = jiffies + msecs_to_jiffies
1378 (FCOE_CTLR_START_DELAY);
1379 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));