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 fcoe_ctlr_link_down(&fnic->ctlr);
144 * This function passes incoming fabric frames to libFC
146 void fnic_handle_frame(struct work_struct *work)
148 struct fnic *fnic = container_of(work, struct fnic, frame_work);
149 struct fc_lport *lp = fnic->lport;
154 while ((skb = skb_dequeue(&fnic->frame_queue))) {
156 spin_lock_irqsave(&fnic->fnic_lock, flags);
157 if (fnic->stop_rx_link_events) {
158 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
162 fp = (struct fc_frame *)skb;
165 * If we're in a transitional state, just re-queue and return.
166 * The queue will be serviced when we get to a stable state.
168 if (fnic->state != FNIC_IN_FC_MODE &&
169 fnic->state != FNIC_IN_ETH_MODE) {
170 skb_queue_head(&fnic->frame_queue, skb);
171 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
174 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
176 fc_exch_recv(lp, fp);
180 void fnic_fcoe_evlist_free(struct fnic *fnic)
182 struct fnic_event *fevt = NULL;
183 struct fnic_event *next = NULL;
186 spin_lock_irqsave(&fnic->fnic_lock, flags);
187 if (list_empty(&fnic->evlist)) {
188 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
192 list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
193 list_del(&fevt->list);
196 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
199 void fnic_handle_event(struct work_struct *work)
201 struct fnic *fnic = container_of(work, struct fnic, event_work);
202 struct fnic_event *fevt = NULL;
203 struct fnic_event *next = NULL;
206 spin_lock_irqsave(&fnic->fnic_lock, flags);
207 if (list_empty(&fnic->evlist)) {
208 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
212 list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
213 if (fnic->stop_rx_link_events) {
214 list_del(&fevt->list);
216 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
220 * If we're in a transitional state, just re-queue and return.
221 * The queue will be serviced when we get to a stable state.
223 if (fnic->state != FNIC_IN_FC_MODE &&
224 fnic->state != FNIC_IN_ETH_MODE) {
225 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
229 list_del(&fevt->list);
230 switch (fevt->event) {
231 case FNIC_EVT_START_VLAN_DISC:
232 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
233 fnic_fcoe_send_vlan_req(fnic);
234 spin_lock_irqsave(&fnic->fnic_lock, flags);
236 case FNIC_EVT_START_FCF_DISC:
237 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
238 "Start FCF Discovery\n");
239 fnic_fcoe_start_fcf_disc(fnic);
242 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
243 "Unknown event 0x%x\n", fevt->event);
248 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
252 * Check if the Received FIP FLOGI frame is rejected
253 * @fip: The FCoE controller that received the frame
254 * @skb: The received FIP frame
256 * Returns non-zero if the frame is rejected with unsupported cmd with
257 * insufficient resource els explanation.
259 static inline int is_fnic_fip_flogi_reject(struct fcoe_ctlr *fip,
262 struct fc_lport *lport = fip->lp;
263 struct fip_header *fiph;
264 struct fc_frame_header *fh = NULL;
265 struct fip_desc *desc;
266 struct fip_encaps *els;
267 enum fip_desc_type els_dtype = 0;
276 if (skb_linearize(skb))
279 if (skb->len < sizeof(*fiph))
282 fiph = (struct fip_header *)skb->data;
283 op = ntohs(fiph->fip_op);
284 sub = fiph->fip_subcode;
289 if (sub != FIP_SC_REP)
292 rlen = ntohs(fiph->fip_dl_len) * 4;
293 if (rlen + sizeof(*fiph) > skb->len)
296 desc = (struct fip_desc *)(fiph + 1);
297 dlen = desc->fip_dlen * FIP_BPW;
299 if (desc->fip_dtype == FIP_DT_FLOGI) {
301 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
304 els_len = dlen - sizeof(*els);
305 els = (struct fip_encaps *)desc;
306 fh = (struct fc_frame_header *)(els + 1);
307 els_dtype = desc->fip_dtype;
313 * ELS command code, reason and explanation should be = Reject,
314 * unsupported command and insufficient resource
316 els_op = *(u8 *)(fh + 1);
317 if (els_op == ELS_LS_RJT) {
318 shost_printk(KERN_INFO, lport->host,
319 "Flogi Request Rejected by Switch\n");
322 shost_printk(KERN_INFO, lport->host,
323 "Flogi Request Accepted by Switch\n");
328 static void fnic_fcoe_send_vlan_req(struct fnic *fnic)
330 struct fcoe_ctlr *fip = &fnic->ctlr;
331 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
335 struct fip_vlan *vlan;
338 fnic_fcoe_reset_vlans(fnic);
339 fnic->set_vlan(fnic, 0);
340 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
341 "Sending VLAN request...\n");
342 skb = dev_alloc_skb(sizeof(struct fip_vlan));
346 fr_len = sizeof(*vlan);
347 eth_fr = (char *)skb->data;
348 vlan = (struct fip_vlan *)eth_fr;
350 memset(vlan, 0, sizeof(*vlan));
351 memcpy(vlan->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
352 memcpy(vlan->eth.h_dest, fcoe_all_fcfs, ETH_ALEN);
353 vlan->eth.h_proto = htons(ETH_P_FIP);
355 vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
356 vlan->fip.fip_op = htons(FIP_OP_VLAN);
357 vlan->fip.fip_subcode = FIP_SC_VL_REQ;
358 vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
360 vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
361 vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
362 memcpy(&vlan->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
364 vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
365 vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
366 put_unaligned_be64(fip->lp->wwnn, &vlan->desc.wwnn.fd_wwn);
367 atomic64_inc(&fnic_stats->vlan_stats.vlan_disc_reqs);
369 skb_put(skb, sizeof(*vlan));
370 skb->protocol = htons(ETH_P_FIP);
371 skb_reset_mac_header(skb);
372 skb_reset_network_header(skb);
375 /* set a timer so that we can retry if there no response */
376 vlan_tov = jiffies + msecs_to_jiffies(FCOE_CTLR_FIPVLAN_TOV);
377 mod_timer(&fnic->fip_timer, round_jiffies(vlan_tov));
380 static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *skb)
382 struct fcoe_ctlr *fip = &fnic->ctlr;
383 struct fip_header *fiph;
384 struct fip_desc *desc;
385 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
389 struct fcoe_vlan *vlan;
393 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
394 "Received VLAN response...\n");
396 fiph = (struct fip_header *) skb->data;
398 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
399 "Received VLAN response... OP 0x%x SUB_OP 0x%x\n",
400 ntohs(fiph->fip_op), fiph->fip_subcode);
402 rlen = ntohs(fiph->fip_dl_len) * 4;
403 fnic_fcoe_reset_vlans(fnic);
404 spin_lock_irqsave(&fnic->vlans_lock, flags);
405 desc = (struct fip_desc *)(fiph + 1);
407 dlen = desc->fip_dlen * FIP_BPW;
408 switch (desc->fip_dtype) {
410 vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
411 shost_printk(KERN_INFO, fnic->lport->host,
412 "process_vlan_resp: FIP VLAN %d\n", vid);
413 vlan = kmalloc(sizeof(*vlan),
416 /* retry from timer */
417 spin_unlock_irqrestore(&fnic->vlans_lock,
421 memset(vlan, 0, sizeof(struct fcoe_vlan));
422 vlan->vid = vid & 0x0fff;
423 vlan->state = FIP_VLAN_AVAIL;
424 list_add_tail(&vlan->list, &fnic->vlans);
427 desc = (struct fip_desc *)((char *)desc + dlen);
431 /* any VLAN descriptors present ? */
432 if (list_empty(&fnic->vlans)) {
433 /* retry from timer */
434 atomic64_inc(&fnic_stats->vlan_stats.resp_withno_vlanID);
435 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
436 "No VLAN descriptors in FIP VLAN response\n");
437 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
441 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
442 fnic->set_vlan(fnic, vlan->vid);
443 vlan->state = FIP_VLAN_SENT; /* sent now */
445 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
447 /* start the solicitation */
448 fcoe_ctlr_link_up(fip);
450 sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
451 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
456 static void fnic_fcoe_start_fcf_disc(struct fnic *fnic)
459 struct fcoe_vlan *vlan;
462 spin_lock_irqsave(&fnic->vlans_lock, flags);
463 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
464 fnic->set_vlan(fnic, vlan->vid);
465 vlan->state = FIP_VLAN_SENT; /* sent now */
467 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
469 /* start the solicitation */
470 fcoe_ctlr_link_up(&fnic->ctlr);
472 sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
473 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
476 static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag)
479 struct fcoe_vlan *fvlan;
481 spin_lock_irqsave(&fnic->vlans_lock, flags);
482 if (list_empty(&fnic->vlans)) {
483 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
487 fvlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
488 if (fvlan->state == FIP_VLAN_USED) {
489 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
493 if (fvlan->state == FIP_VLAN_SENT) {
494 fvlan->state = FIP_VLAN_USED;
495 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
498 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
502 static void fnic_event_enq(struct fnic *fnic, enum fnic_evt ev)
504 struct fnic_event *fevt;
507 fevt = kmalloc(sizeof(*fevt), GFP_ATOMIC);
514 spin_lock_irqsave(&fnic->fnic_lock, flags);
515 list_add_tail(&fevt->list, &fnic->evlist);
516 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
518 schedule_work(&fnic->event_work);
521 static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb)
523 struct fip_header *fiph;
528 if (!skb || !(skb->data))
531 if (skb_linearize(skb))
534 fiph = (struct fip_header *)skb->data;
535 op = ntohs(fiph->fip_op);
536 sub = fiph->fip_subcode;
538 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
541 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
544 if (op == FIP_OP_DISC && sub == FIP_SC_ADV) {
545 if (fnic_fcoe_vlan_check(fnic, ntohs(fiph->fip_flags)))
547 /* pass it on to fcoe */
549 } else if (op == FIP_OP_VLAN && sub == FIP_SC_VL_REP) {
550 /* set the vlan as used */
551 fnic_fcoe_process_vlan_resp(fnic, skb);
553 } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
554 /* received CVL request, restart vlan disc */
555 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
556 /* pass it on to fcoe */
563 void fnic_handle_fip_frame(struct work_struct *work)
565 struct fnic *fnic = container_of(work, struct fnic, fip_frame_work);
566 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
571 while ((skb = skb_dequeue(&fnic->fip_frame_queue))) {
572 spin_lock_irqsave(&fnic->fnic_lock, flags);
573 if (fnic->stop_rx_link_events) {
574 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
579 * If we're in a transitional state, just re-queue and return.
580 * The queue will be serviced when we get to a stable state.
582 if (fnic->state != FNIC_IN_FC_MODE &&
583 fnic->state != FNIC_IN_ETH_MODE) {
584 skb_queue_head(&fnic->fip_frame_queue, skb);
585 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
588 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
589 eh = (struct ethhdr *)skb->data;
590 if (eh->h_proto == htons(ETH_P_FIP)) {
591 skb_pull(skb, sizeof(*eh));
592 if (fnic_fcoe_handle_fip_frame(fnic, skb) <= 0) {
597 * If there's FLOGI rejects - clear all
598 * fcf's & restart from scratch
600 if (is_fnic_fip_flogi_reject(&fnic->ctlr, skb)) {
602 &fnic_stats->vlan_stats.flogi_rejects);
603 shost_printk(KERN_INFO, fnic->lport->host,
604 "Trigger a Link down - VLAN Disc\n");
605 fcoe_ctlr_link_down(&fnic->ctlr);
606 /* start FCoE VLAN discovery */
607 fnic_fcoe_send_vlan_req(fnic);
611 fcoe_ctlr_recv(&fnic->ctlr, skb);
618 * fnic_import_rq_eth_pkt() - handle received FCoE or FIP frame.
619 * @fnic: fnic instance.
620 * @skb: Ethernet Frame.
622 static inline int fnic_import_rq_eth_pkt(struct fnic *fnic, struct sk_buff *skb)
626 struct fcoe_hdr *fcoe_hdr;
627 struct fcoe_crc_eof *ft;
630 * Undo VLAN encapsulation if present.
632 eh = (struct ethhdr *)skb->data;
633 if (eh->h_proto == htons(ETH_P_8021Q)) {
634 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
635 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
636 skb_reset_mac_header(skb);
638 if (eh->h_proto == htons(ETH_P_FIP)) {
639 if (!(fnic->config.flags & VFCF_FIP_CAPABLE)) {
640 printk(KERN_ERR "Dropped FIP frame, as firmware "
641 "uses non-FIP mode, Enable FIP "
645 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
646 FNIC_FC_RECV|0x80, (char *)skb->data, skb->len)) != 0) {
647 printk(KERN_ERR "fnic ctlr frame trace error!!!");
649 skb_queue_tail(&fnic->fip_frame_queue, skb);
650 queue_work(fnic_fip_queue, &fnic->fip_frame_work);
651 return 1; /* let caller know packet was used */
653 if (eh->h_proto != htons(ETH_P_FCOE))
655 skb_set_network_header(skb, sizeof(*eh));
656 skb_pull(skb, sizeof(*eh));
658 fcoe_hdr = (struct fcoe_hdr *)skb->data;
659 if (FC_FCOE_DECAPS_VER(fcoe_hdr) != FC_FCOE_VER)
662 fp = (struct fc_frame *)skb;
664 fr_sof(fp) = fcoe_hdr->fcoe_sof;
665 skb_pull(skb, sizeof(struct fcoe_hdr));
666 skb_reset_transport_header(skb);
668 ft = (struct fcoe_crc_eof *)(skb->data + skb->len - sizeof(*ft));
669 fr_eof(fp) = ft->fcoe_eof;
670 skb_trim(skb, skb->len - sizeof(*ft));
673 dev_kfree_skb_irq(skb);
678 * fnic_update_mac_locked() - set data MAC address and filters.
679 * @fnic: fnic instance.
680 * @new: newly-assigned FCoE MAC address.
682 * Called with the fnic lock held.
684 void fnic_update_mac_locked(struct fnic *fnic, u8 *new)
686 u8 *ctl = fnic->ctlr.ctl_src_addr;
687 u8 *data = fnic->data_src_addr;
689 if (is_zero_ether_addr(new))
691 if (ether_addr_equal(data, new))
693 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "update_mac %pM\n", new);
694 if (!is_zero_ether_addr(data) && !ether_addr_equal(data, ctl))
695 vnic_dev_del_addr(fnic->vdev, data);
696 memcpy(data, new, ETH_ALEN);
697 if (!ether_addr_equal(new, ctl))
698 vnic_dev_add_addr(fnic->vdev, new);
702 * fnic_update_mac() - set data MAC address and filters.
703 * @lport: local port.
704 * @new: newly-assigned FCoE MAC address.
706 void fnic_update_mac(struct fc_lport *lport, u8 *new)
708 struct fnic *fnic = lport_priv(lport);
710 spin_lock_irq(&fnic->fnic_lock);
711 fnic_update_mac_locked(fnic, new);
712 spin_unlock_irq(&fnic->fnic_lock);
716 * fnic_set_port_id() - set the port_ID after successful FLOGI.
717 * @lport: local port.
718 * @port_id: assigned FC_ID.
719 * @fp: received frame containing the FLOGI accept or NULL.
721 * This is called from libfc when a new FC_ID has been assigned.
722 * This causes us to reset the firmware to FC_MODE and setup the new MAC
725 * It is also called with FC_ID 0 when we're logged off.
727 * If the FC_ID is due to point-to-point, fp may be NULL.
729 void fnic_set_port_id(struct fc_lport *lport, u32 port_id, struct fc_frame *fp)
731 struct fnic *fnic = lport_priv(lport);
735 FNIC_FCS_DBG(KERN_DEBUG, lport->host, "set port_id %x fp %p\n",
739 * If we're clearing the FC_ID, change to use the ctl_src_addr.
740 * Set ethernet mode to send FLOGI.
743 fnic_update_mac(lport, fnic->ctlr.ctl_src_addr);
744 fnic_set_eth_mode(fnic);
749 mac = fr_cb(fp)->granted_mac;
750 if (is_zero_ether_addr(mac)) {
751 /* non-FIP - FLOGI already accepted - ignore return */
752 fcoe_ctlr_recv_flogi(&fnic->ctlr, lport, fp);
754 fnic_update_mac(lport, mac);
757 /* Change state to reflect transition to FC mode */
758 spin_lock_irq(&fnic->fnic_lock);
759 if (fnic->state == FNIC_IN_ETH_MODE || fnic->state == FNIC_IN_FC_MODE)
760 fnic->state = FNIC_IN_ETH_TRANS_FC_MODE;
762 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
763 "Unexpected fnic state %s while"
764 " processing flogi resp\n",
765 fnic_state_to_str(fnic->state));
766 spin_unlock_irq(&fnic->fnic_lock);
769 spin_unlock_irq(&fnic->fnic_lock);
772 * Send FLOGI registration to firmware to set up FC mode.
773 * The new address will be set up when registration completes.
775 ret = fnic_flogi_reg_handler(fnic, port_id);
778 spin_lock_irq(&fnic->fnic_lock);
779 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE)
780 fnic->state = FNIC_IN_ETH_MODE;
781 spin_unlock_irq(&fnic->fnic_lock);
785 static void fnic_rq_cmpl_frame_recv(struct vnic_rq *rq, struct cq_desc
786 *cq_desc, struct vnic_rq_buf *buf,
787 int skipped __attribute__((unused)),
790 struct fnic *fnic = vnic_dev_priv(rq->vdev);
793 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
794 unsigned int eth_hdrs_stripped;
795 u8 type, color, eop, sop, ingress_port, vlan_stripped;
796 u8 fcoe = 0, fcoe_sof, fcoe_eof;
797 u8 fcoe_fc_crc_ok = 1, fcoe_enc_error = 0;
798 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
799 u8 ipv6, ipv4, ipv4_fragment, rss_type, csum_not_calc;
800 u8 fcs_ok = 1, packet_error = 0;
801 u16 q_number, completed_index, bytes_written = 0, vlan, checksum;
803 u16 exchange_id, tmpl;
806 u32 fcp_bytes_written = 0;
809 pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
812 fp = (struct fc_frame *)skb;
815 cq_desc_dec(cq_desc, &type, &color, &q_number, &completed_index);
816 if (type == CQ_DESC_TYPE_RQ_FCP) {
817 cq_fcp_rq_desc_dec((struct cq_fcp_rq_desc *)cq_desc,
818 &type, &color, &q_number, &completed_index,
819 &eop, &sop, &fcoe_fc_crc_ok, &exchange_id,
820 &tmpl, &fcp_bytes_written, &sof, &eof,
821 &ingress_port, &packet_error,
822 &fcoe_enc_error, &fcs_ok, &vlan_stripped,
824 eth_hdrs_stripped = 1;
825 skb_trim(skb, fcp_bytes_written);
829 } else if (type == CQ_DESC_TYPE_RQ_ENET) {
830 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
831 &type, &color, &q_number, &completed_index,
832 &ingress_port, &fcoe, &eop, &sop,
833 &rss_type, &csum_not_calc, &rss_hash,
834 &bytes_written, &packet_error,
835 &vlan_stripped, &vlan, &checksum,
836 &fcoe_sof, &fcoe_fc_crc_ok,
837 &fcoe_enc_error, &fcoe_eof,
838 &tcp_udp_csum_ok, &udp, &tcp,
839 &ipv4_csum_ok, &ipv6, &ipv4,
840 &ipv4_fragment, &fcs_ok);
841 eth_hdrs_stripped = 0;
842 skb_trim(skb, bytes_written);
844 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
845 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
846 "fcs error. dropping packet.\n");
849 if (fnic_import_rq_eth_pkt(fnic, skb))
854 shost_printk(KERN_ERR, fnic->lport->host,
855 "fnic rq_cmpl wrong cq type x%x\n", type);
859 if (!fcs_ok || packet_error || !fcoe_fc_crc_ok || fcoe_enc_error) {
860 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
861 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
862 "fnic rq_cmpl fcoe x%x fcsok x%x"
863 " pkterr x%x fcoe_fc_crc_ok x%x, fcoe_enc_err"
865 fcoe, fcs_ok, packet_error,
866 fcoe_fc_crc_ok, fcoe_enc_error);
870 spin_lock_irqsave(&fnic->fnic_lock, flags);
871 if (fnic->stop_rx_link_events) {
872 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
875 fr_dev(fp) = fnic->lport;
876 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
877 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_RECV,
878 (char *)skb->data, skb->len)) != 0) {
879 printk(KERN_ERR "fnic ctlr frame trace error!!!");
882 skb_queue_tail(&fnic->frame_queue, skb);
883 queue_work(fnic_event_queue, &fnic->frame_work);
887 dev_kfree_skb_irq(skb);
890 static int fnic_rq_cmpl_handler_cont(struct vnic_dev *vdev,
891 struct cq_desc *cq_desc, u8 type,
892 u16 q_number, u16 completed_index,
895 struct fnic *fnic = vnic_dev_priv(vdev);
897 vnic_rq_service(&fnic->rq[q_number], cq_desc, completed_index,
898 VNIC_RQ_RETURN_DESC, fnic_rq_cmpl_frame_recv,
903 int fnic_rq_cmpl_handler(struct fnic *fnic, int rq_work_to_do)
905 unsigned int tot_rq_work_done = 0, cur_work_done;
909 for (i = 0; i < fnic->rq_count; i++) {
910 cur_work_done = vnic_cq_service(&fnic->cq[i], rq_work_to_do,
911 fnic_rq_cmpl_handler_cont,
914 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
916 shost_printk(KERN_ERR, fnic->lport->host,
917 "fnic_alloc_rq_frame can't alloc"
920 tot_rq_work_done += cur_work_done;
923 return tot_rq_work_done;
927 * This function is called once at init time to allocate and fill RQ
928 * buffers. Subsequently, it is called in the interrupt context after RQ
929 * buffer processing to replenish the buffers in the RQ
931 int fnic_alloc_rq_frame(struct vnic_rq *rq)
933 struct fnic *fnic = vnic_dev_priv(rq->vdev);
938 len = FC_FRAME_HEADROOM + FC_MAX_FRAME + FC_FRAME_TAILROOM;
939 skb = dev_alloc_skb(len);
941 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
942 "Unable to allocate RQ sk_buff\n");
945 skb_reset_mac_header(skb);
946 skb_reset_transport_header(skb);
947 skb_reset_network_header(skb);
949 pa = pci_map_single(fnic->pdev, skb->data, len, PCI_DMA_FROMDEVICE);
950 fnic_queue_rq_desc(rq, skb, pa, len);
954 void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
956 struct fc_frame *fp = buf->os_buf;
957 struct fnic *fnic = vnic_dev_priv(rq->vdev);
959 pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
962 dev_kfree_skb(fp_skb(fp));
967 * fnic_eth_send() - Send Ethernet frame.
968 * @fip: fcoe_ctlr instance.
969 * @skb: Ethernet Frame, FIP, without VLAN encapsulation.
971 void fnic_eth_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
973 struct fnic *fnic = fnic_from_ctlr(fip);
974 struct vnic_wq *wq = &fnic->wq[0];
976 struct ethhdr *eth_hdr;
977 struct vlan_ethhdr *vlan_hdr;
980 if (!fnic->vlan_hw_insert) {
981 eth_hdr = (struct ethhdr *)skb_mac_header(skb);
982 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb,
983 sizeof(*vlan_hdr) - sizeof(*eth_hdr));
984 memcpy(vlan_hdr, eth_hdr, 2 * ETH_ALEN);
985 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
986 vlan_hdr->h_vlan_encapsulated_proto = eth_hdr->h_proto;
987 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
988 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
989 FNIC_FC_SEND|0x80, (char *)eth_hdr, skb->len)) != 0) {
990 printk(KERN_ERR "fnic ctlr frame trace error!!!");
993 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
994 FNIC_FC_SEND|0x80, (char *)skb->data, skb->len)) != 0) {
995 printk(KERN_ERR "fnic ctlr frame trace error!!!");
999 pa = pci_map_single(fnic->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1001 spin_lock_irqsave(&fnic->wq_lock[0], flags);
1002 if (!vnic_wq_desc_avail(wq)) {
1003 pci_unmap_single(fnic->pdev, pa, skb->len, PCI_DMA_TODEVICE);
1004 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1009 fnic_queue_wq_eth_desc(wq, skb, pa, skb->len,
1010 0 /* hw inserts cos value */,
1012 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1018 static int fnic_send_frame(struct fnic *fnic, struct fc_frame *fp)
1020 struct vnic_wq *wq = &fnic->wq[0];
1021 struct sk_buff *skb;
1023 struct ethhdr *eth_hdr;
1024 struct vlan_ethhdr *vlan_hdr;
1025 struct fcoe_hdr *fcoe_hdr;
1026 struct fc_frame_header *fh;
1027 u32 tot_len, eth_hdr_len;
1029 unsigned long flags;
1031 fh = fc_frame_header_get(fp);
1034 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1035 fcoe_ctlr_els_send(&fnic->ctlr, fnic->lport, skb))
1038 if (!fnic->vlan_hw_insert) {
1039 eth_hdr_len = sizeof(*vlan_hdr) + sizeof(*fcoe_hdr);
1040 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb, eth_hdr_len);
1041 eth_hdr = (struct ethhdr *)vlan_hdr;
1042 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1043 vlan_hdr->h_vlan_encapsulated_proto = htons(ETH_P_FCOE);
1044 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1045 fcoe_hdr = (struct fcoe_hdr *)(vlan_hdr + 1);
1047 eth_hdr_len = sizeof(*eth_hdr) + sizeof(*fcoe_hdr);
1048 eth_hdr = (struct ethhdr *)skb_push(skb, eth_hdr_len);
1049 eth_hdr->h_proto = htons(ETH_P_FCOE);
1050 fcoe_hdr = (struct fcoe_hdr *)(eth_hdr + 1);
1053 if (fnic->ctlr.map_dest)
1054 fc_fcoe_set_mac(eth_hdr->h_dest, fh->fh_d_id);
1056 memcpy(eth_hdr->h_dest, fnic->ctlr.dest_addr, ETH_ALEN);
1057 memcpy(eth_hdr->h_source, fnic->data_src_addr, ETH_ALEN);
1060 BUG_ON(tot_len % 4);
1062 memset(fcoe_hdr, 0, sizeof(*fcoe_hdr));
1063 fcoe_hdr->fcoe_sof = fr_sof(fp);
1065 FC_FCOE_ENCAPS_VER(fcoe_hdr, FC_FCOE_VER);
1067 pa = pci_map_single(fnic->pdev, eth_hdr, tot_len, PCI_DMA_TODEVICE);
1069 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_SEND,
1070 (char *)eth_hdr, tot_len)) != 0) {
1071 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1074 spin_lock_irqsave(&fnic->wq_lock[0], flags);
1076 if (!vnic_wq_desc_avail(wq)) {
1077 pci_unmap_single(fnic->pdev, pa,
1078 tot_len, PCI_DMA_TODEVICE);
1080 goto fnic_send_frame_end;
1083 fnic_queue_wq_desc(wq, skb, pa, tot_len, fr_eof(fp),
1084 0 /* hw inserts cos value */,
1085 fnic->vlan_id, 1, 1, 1);
1086 fnic_send_frame_end:
1087 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1090 dev_kfree_skb_any(fp_skb(fp));
1097 * Routine to send a raw frame
1099 int fnic_send(struct fc_lport *lp, struct fc_frame *fp)
1101 struct fnic *fnic = lport_priv(lp);
1102 unsigned long flags;
1104 if (fnic->in_remove) {
1105 dev_kfree_skb(fp_skb(fp));
1110 * Queue frame if in a transitional state.
1111 * This occurs while registering the Port_ID / MAC address after FLOGI.
1113 spin_lock_irqsave(&fnic->fnic_lock, flags);
1114 if (fnic->state != FNIC_IN_FC_MODE && fnic->state != FNIC_IN_ETH_MODE) {
1115 skb_queue_tail(&fnic->tx_queue, fp_skb(fp));
1116 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1119 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1121 return fnic_send_frame(fnic, fp);
1125 * fnic_flush_tx() - send queued frames.
1126 * @fnic: fnic device
1128 * Send frames that were waiting to go out in FC or Ethernet mode.
1129 * Whenever changing modes we purge queued frames, so these frames should
1130 * be queued for the stable mode that we're in, either FC or Ethernet.
1132 * Called without fnic_lock held.
1134 void fnic_flush_tx(struct fnic *fnic)
1136 struct sk_buff *skb;
1137 struct fc_frame *fp;
1139 while ((skb = skb_dequeue(&fnic->tx_queue))) {
1140 fp = (struct fc_frame *)skb;
1141 fnic_send_frame(fnic, fp);
1146 * fnic_set_eth_mode() - put fnic into ethernet mode.
1147 * @fnic: fnic device
1149 * Called without fnic lock held.
1151 static void fnic_set_eth_mode(struct fnic *fnic)
1153 unsigned long flags;
1154 enum fnic_state old_state;
1157 spin_lock_irqsave(&fnic->fnic_lock, flags);
1159 old_state = fnic->state;
1160 switch (old_state) {
1161 case FNIC_IN_FC_MODE:
1162 case FNIC_IN_ETH_TRANS_FC_MODE:
1164 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1165 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1167 ret = fnic_fw_reset_handler(fnic);
1169 spin_lock_irqsave(&fnic->fnic_lock, flags);
1170 if (fnic->state != FNIC_IN_FC_TRANS_ETH_MODE)
1173 fnic->state = old_state;
1176 case FNIC_IN_FC_TRANS_ETH_MODE:
1177 case FNIC_IN_ETH_MODE:
1180 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1183 static void fnic_wq_complete_frame_send(struct vnic_wq *wq,
1184 struct cq_desc *cq_desc,
1185 struct vnic_wq_buf *buf, void *opaque)
1187 struct sk_buff *skb = buf->os_buf;
1188 struct fc_frame *fp = (struct fc_frame *)skb;
1189 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1191 pci_unmap_single(fnic->pdev, buf->dma_addr,
1192 buf->len, PCI_DMA_TODEVICE);
1193 dev_kfree_skb_irq(fp_skb(fp));
1197 static int fnic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
1198 struct cq_desc *cq_desc, u8 type,
1199 u16 q_number, u16 completed_index,
1202 struct fnic *fnic = vnic_dev_priv(vdev);
1203 unsigned long flags;
1205 spin_lock_irqsave(&fnic->wq_lock[q_number], flags);
1206 vnic_wq_service(&fnic->wq[q_number], cq_desc, completed_index,
1207 fnic_wq_complete_frame_send, NULL);
1208 spin_unlock_irqrestore(&fnic->wq_lock[q_number], flags);
1213 int fnic_wq_cmpl_handler(struct fnic *fnic, int work_to_do)
1215 unsigned int wq_work_done = 0;
1218 for (i = 0; i < fnic->raw_wq_count; i++) {
1219 wq_work_done += vnic_cq_service(&fnic->cq[fnic->rq_count+i],
1221 fnic_wq_cmpl_handler_cont,
1225 return wq_work_done;
1229 void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
1231 struct fc_frame *fp = buf->os_buf;
1232 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1234 pci_unmap_single(fnic->pdev, buf->dma_addr,
1235 buf->len, PCI_DMA_TODEVICE);
1237 dev_kfree_skb(fp_skb(fp));
1241 void fnic_fcoe_reset_vlans(struct fnic *fnic)
1243 unsigned long flags;
1244 struct fcoe_vlan *vlan;
1245 struct fcoe_vlan *next;
1248 * indicate a link down to fcoe so that all fcf's are free'd
1249 * might not be required since we did this before sending vlan
1252 spin_lock_irqsave(&fnic->vlans_lock, flags);
1253 if (!list_empty(&fnic->vlans)) {
1254 list_for_each_entry_safe(vlan, next, &fnic->vlans, list) {
1255 list_del(&vlan->list);
1259 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1262 void fnic_handle_fip_timer(struct fnic *fnic)
1264 unsigned long flags;
1265 struct fcoe_vlan *vlan;
1266 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1269 spin_lock_irqsave(&fnic->fnic_lock, flags);
1270 if (fnic->stop_rx_link_events) {
1271 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1274 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1276 if (fnic->ctlr.mode == FIP_ST_NON_FIP)
1279 spin_lock_irqsave(&fnic->vlans_lock, flags);
1280 if (list_empty(&fnic->vlans)) {
1281 /* no vlans available, try again */
1282 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1283 "Start VLAN Discovery\n");
1284 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1285 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1289 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
1290 shost_printk(KERN_DEBUG, fnic->lport->host,
1291 "fip_timer: vlan %d state %d sol_count %d\n",
1292 vlan->vid, vlan->state, vlan->sol_count);
1293 switch (vlan->state) {
1295 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1296 "FIP VLAN is selected for FC transaction\n");
1297 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1299 case FIP_VLAN_FAILED:
1300 /* if all vlans are in failed state, restart vlan disc */
1301 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1302 "Start VLAN Discovery\n");
1303 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1304 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1307 if (vlan->sol_count >= FCOE_CTLR_MAX_SOL) {
1309 * no response on this vlan, remove from the list.
1312 shost_printk(KERN_INFO, fnic->lport->host,
1313 "Dequeue this VLAN ID %d from list\n",
1315 list_del(&vlan->list);
1318 if (list_empty(&fnic->vlans)) {
1319 /* we exhausted all vlans, restart vlan disc */
1320 spin_unlock_irqrestore(&fnic->vlans_lock,
1322 shost_printk(KERN_INFO, fnic->lport->host,
1323 "fip_timer: vlan list empty, "
1324 "trigger vlan disc\n");
1325 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1328 /* check the next vlan */
1329 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan,
1331 fnic->set_vlan(fnic, vlan->vid);
1332 vlan->state = FIP_VLAN_SENT; /* sent now */
1334 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1335 atomic64_inc(&fnic_stats->vlan_stats.sol_expiry_count);
1337 sol_time = jiffies + msecs_to_jiffies
1338 (FCOE_CTLR_START_DELAY);
1339 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));