1 /* bnx2fc_fcoe.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * This file contains the code that interacts with libfc, libfcoe,
3 * cnic modules to create FCoE instances, send/receive non-offloaded
4 * FIP/FCoE packets, listen to link events etc.
6 * Copyright (c) 2008 - 2013 Broadcom Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
17 static struct list_head adapter_list;
18 static struct list_head if_list;
19 static u32 adapter_count;
20 static DEFINE_MUTEX(bnx2fc_dev_lock);
21 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
23 #define DRV_MODULE_NAME "bnx2fc"
24 #define DRV_MODULE_VERSION BNX2FC_VERSION
25 #define DRV_MODULE_RELDATE "Dec 11, 2013"
28 static char version[] =
29 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
33 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35 MODULE_LICENSE("GPL");
36 MODULE_VERSION(DRV_MODULE_VERSION);
38 #define BNX2FC_MAX_QUEUE_DEPTH 256
39 #define BNX2FC_MIN_QUEUE_DEPTH 32
40 #define FCOE_WORD_TO_BYTE 4
42 static struct scsi_transport_template *bnx2fc_transport_template;
43 static struct scsi_transport_template *bnx2fc_vport_xport_template;
45 struct workqueue_struct *bnx2fc_wq;
47 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
48 * Here the io threads are per cpu but the l2 thread is just one
50 struct fcoe_percpu_s bnx2fc_global;
51 DEFINE_SPINLOCK(bnx2fc_global_lock);
53 static struct cnic_ulp_ops bnx2fc_cnic_cb;
54 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55 static struct scsi_host_template bnx2fc_shost_template;
56 static struct fc_function_template bnx2fc_transport_function;
57 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
58 static struct fc_function_template bnx2fc_vport_xport_function;
59 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
60 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
61 static int bnx2fc_destroy(struct net_device *net_device);
62 static int bnx2fc_enable(struct net_device *netdev);
63 static int bnx2fc_disable(struct net_device *netdev);
65 /* fcoe_syfs control interface handlers */
66 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
67 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
69 static void bnx2fc_recv_frame(struct sk_buff *skb);
71 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
72 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
73 static int bnx2fc_lport_config(struct fc_lport *lport);
74 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
75 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
76 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
77 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
78 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
79 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
80 struct device *parent, int npiv);
81 static void bnx2fc_destroy_work(struct work_struct *work);
83 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
84 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
86 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
87 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
89 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
90 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
92 static void bnx2fc_port_shutdown(struct fc_lport *lport);
93 static void bnx2fc_stop(struct bnx2fc_interface *interface);
94 static int __init bnx2fc_mod_init(void);
95 static void __exit bnx2fc_mod_exit(void);
97 unsigned int bnx2fc_debug_level;
98 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
100 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
101 unsigned long action, void *hcpu);
102 /* notification function for CPU hotplug events */
103 static struct notifier_block bnx2fc_cpu_notifier = {
104 .notifier_call = bnx2fc_cpu_callback,
107 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
109 return ((struct bnx2fc_interface *)
110 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
113 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
115 struct fcoe_ctlr_device *ctlr_dev =
116 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
117 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
118 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
120 fcf_dev->vlan_id = fcoe->vlan_id;
123 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
125 struct fcoe_percpu_s *bg;
126 struct fcoe_rcv_info *fr;
127 struct sk_buff_head *list;
128 struct sk_buff *skb, *next;
129 struct sk_buff *head;
132 spin_lock_bh(&bg->fcoe_rx_list.lock);
133 list = &bg->fcoe_rx_list;
135 for (skb = head; skb != (struct sk_buff *)list;
138 fr = fcoe_dev_from_skb(skb);
139 if (fr->fr_dev == lp) {
140 __skb_unlink(skb, list);
144 spin_unlock_bh(&bg->fcoe_rx_list.lock);
147 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
150 spin_lock(&bnx2fc_global_lock);
151 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
152 spin_unlock(&bnx2fc_global_lock);
157 static void bnx2fc_abort_io(struct fc_lport *lport)
160 * This function is no-op for bnx2fc, but we do
161 * not want to leave it as NULL either, as libfc
162 * can call the default function which is
167 static void bnx2fc_cleanup(struct fc_lport *lport)
169 struct fcoe_port *port = lport_priv(lport);
170 struct bnx2fc_interface *interface = port->priv;
171 struct bnx2fc_hba *hba = interface->hba;
172 struct bnx2fc_rport *tgt;
175 BNX2FC_MISC_DBG("Entered %s\n", __func__);
176 mutex_lock(&hba->hba_mutex);
177 spin_lock_bh(&hba->hba_lock);
178 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
179 tgt = hba->tgt_ofld_list[i];
181 /* Cleanup IOs belonging to requested vport */
182 if (tgt->port == port) {
183 spin_unlock_bh(&hba->hba_lock);
184 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
185 bnx2fc_flush_active_ios(tgt);
186 spin_lock_bh(&hba->hba_lock);
190 spin_unlock_bh(&hba->hba_lock);
191 mutex_unlock(&hba->hba_mutex);
194 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
197 struct fc_rport_priv *rdata = tgt->rdata;
198 struct fc_frame_header *fh;
201 fh = fc_frame_header_get(fp);
202 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
203 "r_ctl = 0x%x\n", rdata->ids.port_id,
204 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
205 if ((fh->fh_type == FC_TYPE_ELS) &&
206 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
208 switch (fc_frame_payload_op(fp)) {
210 rc = bnx2fc_send_adisc(tgt, fp);
213 rc = bnx2fc_send_logo(tgt, fp);
216 rc = bnx2fc_send_rls(tgt, fp);
221 } else if ((fh->fh_type == FC_TYPE_BLS) &&
222 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
223 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
225 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
226 "rctl 0x%x thru non-offload path\n",
227 fh->fh_type, fh->fh_r_ctl);
237 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
239 * @lport: the associated local port
240 * @fp: the fc_frame to be transmitted
242 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
245 struct fcoe_crc_eof *cp;
247 struct fc_frame_header *fh;
248 struct bnx2fc_interface *interface;
249 struct fcoe_ctlr *ctlr;
250 struct bnx2fc_hba *hba;
251 struct fcoe_port *port;
253 struct bnx2fc_rport *tgt;
254 struct fc_stats *stats;
257 unsigned int hlen, tlen, elen;
260 port = (struct fcoe_port *)lport_priv(lport);
261 interface = port->priv;
262 ctlr = bnx2fc_to_ctlr(interface);
263 hba = interface->hba;
265 fh = fc_frame_header_get(fp);
268 if (!lport->link_up) {
269 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
274 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
275 if (!ctlr->sel_fcf) {
276 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
280 if (fcoe_ctlr_els_send(ctlr, lport, skb))
288 * Snoop the frame header to check if the frame is for
289 * an offloaded session
292 * tgt_ofld_list access is synchronized using
293 * both hba mutex and hba lock. Atleast hba mutex or
294 * hba lock needs to be held for read access.
297 spin_lock_bh(&hba->hba_lock);
298 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
299 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
300 /* This frame is for offloaded session */
301 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
302 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
303 spin_unlock_bh(&hba->hba_lock);
304 rc = bnx2fc_xmit_l2_frame(tgt, fp);
310 spin_unlock_bh(&hba->hba_lock);
313 elen = sizeof(struct ethhdr);
314 hlen = sizeof(struct fcoe_hdr);
315 tlen = sizeof(struct fcoe_crc_eof);
316 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
318 skb->ip_summed = CHECKSUM_NONE;
319 crc = fcoe_fc_crc(fp);
321 /* copy port crc and eof to the skb buff */
322 if (skb_is_nonlinear(skb)) {
324 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
328 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
329 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
331 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
334 memset(cp, 0, sizeof(*cp));
336 cp->fcoe_crc32 = cpu_to_le32(~crc);
337 if (skb_is_nonlinear(skb)) {
342 /* adjust skb network/transport offsets to match mac/fcoe/port */
343 skb_push(skb, elen + hlen);
344 skb_reset_mac_header(skb);
345 skb_reset_network_header(skb);
347 skb->protocol = htons(ETH_P_FCOE);
348 skb->dev = interface->netdev;
350 /* fill up mac and fcoe headers */
352 eh->h_proto = htons(ETH_P_FCOE);
354 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
356 /* insert GW address */
357 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
359 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
360 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
362 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
364 hp = (struct fcoe_hdr *)(eh + 1);
365 memset(hp, 0, sizeof(*hp));
367 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
370 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
371 if (lport->seq_offload && fr_max_payload(fp)) {
372 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
373 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
375 skb_shinfo(skb)->gso_type = 0;
376 skb_shinfo(skb)->gso_size = 0;
380 stats = per_cpu_ptr(lport->stats, get_cpu());
382 stats->TxWords += wlen;
385 /* send down to lld */
387 if (port->fcoe_pending_queue.qlen)
388 fcoe_check_wait_queue(lport, skb);
389 else if (fcoe_start_io(skb))
390 fcoe_check_wait_queue(lport, skb);
396 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
398 * @skb: the receive socket buffer
399 * @dev: associated net device
401 * @olddev: last device
403 * This function receives the packet and builds FC frame and passes it up
405 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
406 struct packet_type *ptype, struct net_device *olddev)
408 struct fc_lport *lport;
409 struct bnx2fc_interface *interface;
410 struct fcoe_ctlr *ctlr;
411 struct fc_frame_header *fh;
412 struct fcoe_rcv_info *fr;
413 struct fcoe_percpu_s *bg;
416 interface = container_of(ptype, struct bnx2fc_interface,
418 ctlr = bnx2fc_to_ctlr(interface);
421 if (unlikely(lport == NULL)) {
422 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
426 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
427 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
432 * Check for minimum frame length, and make sure required FCoE
433 * and FC headers are pulled into the linear data area.
435 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
436 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
439 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
440 fh = (struct fc_frame_header *) skb_transport_header(skb);
442 oxid = ntohs(fh->fh_ox_id);
444 fr = fcoe_dev_from_skb(skb);
448 spin_lock(&bg->fcoe_rx_list.lock);
450 __skb_queue_tail(&bg->fcoe_rx_list, skb);
451 if (bg->fcoe_rx_list.qlen == 1)
452 wake_up_process(bg->thread);
454 spin_unlock(&bg->fcoe_rx_list.lock);
462 static int bnx2fc_l2_rcv_thread(void *arg)
464 struct fcoe_percpu_s *bg = arg;
467 set_user_nice(current, MIN_NICE);
468 set_current_state(TASK_INTERRUPTIBLE);
469 while (!kthread_should_stop()) {
471 spin_lock_bh(&bg->fcoe_rx_list.lock);
472 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
473 spin_unlock_bh(&bg->fcoe_rx_list.lock);
474 bnx2fc_recv_frame(skb);
475 spin_lock_bh(&bg->fcoe_rx_list.lock);
477 __set_current_state(TASK_INTERRUPTIBLE);
478 spin_unlock_bh(&bg->fcoe_rx_list.lock);
480 __set_current_state(TASK_RUNNING);
485 static void bnx2fc_recv_frame(struct sk_buff *skb)
488 struct fc_lport *lport;
489 struct fcoe_rcv_info *fr;
490 struct fc_stats *stats;
491 struct fc_frame_header *fh;
492 struct fcoe_crc_eof crc_eof;
494 struct fc_lport *vn_port;
495 struct fcoe_port *port;
500 fr = fcoe_dev_from_skb(skb);
502 if (unlikely(lport == NULL)) {
503 printk(KERN_ERR PFX "Invalid lport struct\n");
508 if (skb_is_nonlinear(skb))
510 mac = eth_hdr(skb)->h_source;
511 dest_mac = eth_hdr(skb)->h_dest;
513 /* Pull the header */
514 hp = (struct fcoe_hdr *) skb_network_header(skb);
515 fh = (struct fc_frame_header *) skb_transport_header(skb);
516 skb_pull(skb, sizeof(struct fcoe_hdr));
517 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
519 fp = (struct fc_frame *)skb;
522 fr_sof(fp) = hp->fcoe_sof;
523 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
527 fr_eof(fp) = crc_eof.fcoe_eof;
528 fr_crc(fp) = crc_eof.fcoe_crc32;
529 if (pskb_trim(skb, fr_len)) {
534 fh = fc_frame_header_get(fp);
536 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
538 port = lport_priv(vn_port);
539 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
540 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
545 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
546 fh->fh_type == FC_TYPE_FCP) {
547 /* Drop FCP data. We dont this in L2 path */
551 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
552 fh->fh_type == FC_TYPE_ELS) {
553 switch (fc_frame_payload_op(fp)) {
555 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
556 /* drop non-FIP LOGO */
564 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
565 /* Drop incoming ABTS */
570 stats = per_cpu_ptr(lport->stats, smp_processor_id());
572 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
574 if (le32_to_cpu(fr_crc(fp)) !=
575 ~crc32(~0, skb->data, fr_len)) {
576 if (stats->InvalidCRCCount < 5)
577 printk(KERN_WARNING PFX "dropping frame with "
579 stats->InvalidCRCCount++;
583 fc_exch_recv(lport, fp);
587 * bnx2fc_percpu_io_thread - thread per cpu for ios
589 * @arg: ptr to bnx2fc_percpu_info structure
591 int bnx2fc_percpu_io_thread(void *arg)
593 struct bnx2fc_percpu_s *p = arg;
594 struct bnx2fc_work *work, *tmp;
595 LIST_HEAD(work_list);
597 set_user_nice(current, MIN_NICE);
598 set_current_state(TASK_INTERRUPTIBLE);
599 while (!kthread_should_stop()) {
601 spin_lock_bh(&p->fp_work_lock);
602 while (!list_empty(&p->work_list)) {
603 list_splice_init(&p->work_list, &work_list);
604 spin_unlock_bh(&p->fp_work_lock);
606 list_for_each_entry_safe(work, tmp, &work_list, list) {
607 list_del_init(&work->list);
608 bnx2fc_process_cq_compl(work->tgt, work->wqe);
612 spin_lock_bh(&p->fp_work_lock);
614 __set_current_state(TASK_INTERRUPTIBLE);
615 spin_unlock_bh(&p->fp_work_lock);
617 __set_current_state(TASK_RUNNING);
622 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
624 struct fc_host_statistics *bnx2fc_stats;
625 struct fc_lport *lport = shost_priv(shost);
626 struct fcoe_port *port = lport_priv(lport);
627 struct bnx2fc_interface *interface = port->priv;
628 struct bnx2fc_hba *hba = interface->hba;
629 struct fcoe_statistics_params *fw_stats;
632 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
636 bnx2fc_stats = fc_get_host_stats(shost);
638 init_completion(&hba->stat_req_done);
639 if (bnx2fc_send_stat_req(hba))
641 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
643 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
646 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
647 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
648 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
649 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
650 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
651 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
652 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
653 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
654 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
655 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
657 bnx2fc_stats->dumped_frames = 0;
658 bnx2fc_stats->lip_count = 0;
659 bnx2fc_stats->nos_count = 0;
660 bnx2fc_stats->loss_of_sync_count = 0;
661 bnx2fc_stats->loss_of_signal_count = 0;
662 bnx2fc_stats->prim_seq_protocol_err_count = 0;
664 memcpy(&hba->prev_stats, hba->stats_buffer,
665 sizeof(struct fcoe_statistics_params));
669 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
671 struct fcoe_port *port = lport_priv(lport);
672 struct bnx2fc_interface *interface = port->priv;
673 struct bnx2fc_hba *hba = interface->hba;
674 struct Scsi_Host *shost = lport->host;
677 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
678 shost->max_lun = BNX2FC_MAX_LUN;
679 shost->max_id = BNX2FC_MAX_FCP_TGT;
680 shost->max_channel = 0;
682 shost->transportt = bnx2fc_vport_xport_template;
684 shost->transportt = bnx2fc_transport_template;
686 /* Add the new host to SCSI-ml */
687 rc = scsi_add_host(lport->host, dev);
689 printk(KERN_ERR PFX "Error on scsi_add_host\n");
693 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
694 snprintf(fc_host_symbolic_name(lport->host), 256,
695 "%s (Broadcom %s) v%s over %s",
696 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
697 interface->netdev->name);
702 static int bnx2fc_link_ok(struct fc_lport *lport)
704 struct fcoe_port *port = lport_priv(lport);
705 struct bnx2fc_interface *interface = port->priv;
706 struct bnx2fc_hba *hba = interface->hba;
707 struct net_device *dev = hba->phys_dev;
710 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
711 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
713 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
720 * bnx2fc_get_link_state - get network link state
722 * @hba: adapter instance pointer
724 * updates adapter structure flag based on netdev state
726 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
728 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
729 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
731 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
734 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
736 struct bnx2fc_hba *hba;
737 struct bnx2fc_interface *interface;
738 struct fcoe_ctlr *ctlr;
739 struct fcoe_port *port;
742 port = lport_priv(lport);
743 interface = port->priv;
744 ctlr = bnx2fc_to_ctlr(interface);
745 hba = interface->hba;
747 /* require support for get_pauseparam ethtool op. */
748 if (!hba->phys_dev->ethtool_ops ||
749 !hba->phys_dev->ethtool_ops->get_pauseparam)
752 if (fc_set_mfs(lport, BNX2FC_MFS))
755 skb_queue_head_init(&port->fcoe_pending_queue);
756 port->fcoe_pending_queue_active = 0;
757 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
759 fcoe_link_speed_update(lport);
762 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
763 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
765 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
766 fc_set_wwnn(lport, wwnn);
768 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
769 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
772 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
773 fc_set_wwpn(lport, wwpn);
779 static void bnx2fc_destroy_timer(unsigned long data)
781 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
783 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
784 "Destroy compl not received!!\n");
785 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
786 wake_up_interruptible(&hba->destroy_wait);
790 * bnx2fc_indicate_netevent - Generic netdev event handler
792 * @context: adapter structure pointer
794 * @vlan_id: vlan id - associated vlan id with this event
796 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
797 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
799 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
802 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
803 struct fcoe_ctlr_device *cdev;
804 struct fc_lport *lport;
805 struct fc_lport *vport;
806 struct bnx2fc_interface *interface, *tmp;
807 struct fcoe_ctlr *ctlr;
808 int wait_for_upload = 0;
809 u32 link_possible = 1;
811 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
816 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
817 printk(KERN_ERR "indicate_netevent: "\
818 "hba is not UP!!\n");
822 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
823 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
827 case NETDEV_GOING_DOWN:
828 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
835 case NETDEV_UNREGISTER:
838 mutex_lock(&bnx2fc_dev_lock);
839 list_for_each_entry_safe(interface, tmp, &if_list, list) {
840 if (interface->hba == hba &&
841 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
842 __bnx2fc_destroy(interface);
844 mutex_unlock(&bnx2fc_dev_lock);
846 /* Ensure ALL destroy work has been completed before return */
847 flush_workqueue(bnx2fc_wq);
851 printk(KERN_ERR PFX "Unknown netevent %ld", event);
855 mutex_lock(&bnx2fc_dev_lock);
856 list_for_each_entry(interface, &if_list, list) {
858 if (interface->hba != hba)
861 ctlr = bnx2fc_to_ctlr(interface);
863 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
864 interface->netdev->name, event);
866 fcoe_link_speed_update(lport);
868 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
870 if (link_possible && !bnx2fc_link_ok(lport)) {
871 switch (cdev->enabled) {
872 case FCOE_CTLR_DISABLED:
873 pr_info("Link up while interface is disabled.\n");
875 case FCOE_CTLR_ENABLED:
876 case FCOE_CTLR_UNUSED:
877 /* Reset max recv frame size to default */
878 fc_set_mfs(lport, BNX2FC_MFS);
880 * ctlr link up will only be handled during
881 * enable to avoid sending discovery
882 * solicitation on a stale vlan
884 if (interface->enabled)
885 fcoe_ctlr_link_up(ctlr);
887 } else if (fcoe_ctlr_link_down(ctlr)) {
888 switch (cdev->enabled) {
889 case FCOE_CTLR_DISABLED:
890 pr_info("Link down while interface is disabled.\n");
892 case FCOE_CTLR_ENABLED:
893 case FCOE_CTLR_UNUSED:
894 mutex_lock(&lport->lp_mutex);
895 list_for_each_entry(vport, &lport->vports, list)
896 fc_host_port_type(vport->host) =
898 mutex_unlock(&lport->lp_mutex);
899 fc_host_port_type(lport->host) =
901 per_cpu_ptr(lport->stats,
902 get_cpu())->LinkFailureCount++;
904 fcoe_clean_pending_queue(lport);
909 mutex_unlock(&bnx2fc_dev_lock);
911 if (wait_for_upload) {
912 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
913 init_waitqueue_head(&hba->shutdown_wait);
914 BNX2FC_MISC_DBG("indicate_netevent "
915 "num_ofld_sess = %d\n",
917 hba->wait_for_link_down = 1;
918 wait_event_interruptible(hba->shutdown_wait,
919 (hba->num_ofld_sess == 0));
920 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
922 hba->wait_for_link_down = 0;
924 if (signal_pending(current))
925 flush_signals(current);
929 static int bnx2fc_libfc_config(struct fc_lport *lport)
932 /* Set the function pointers set by bnx2fc driver */
933 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
934 sizeof(struct libfc_function_template));
935 fc_elsct_init(lport);
937 fc_rport_init(lport);
939 fc_disc_config(lport, lport);
943 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
945 int fcoe_min_xid, fcoe_max_xid;
947 fcoe_min_xid = hba->max_xid + 1;
949 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
951 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
952 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
953 fcoe_max_xid, NULL)) {
954 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
961 static int bnx2fc_lport_config(struct fc_lport *lport)
965 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
966 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
967 lport->e_d_tov = 2 * 1000;
968 lport->r_a_tov = 10 * 1000;
970 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
971 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
972 lport->does_npiv = 1;
974 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
975 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
977 /* alloc stats structure */
978 if (fc_lport_init_stats(lport))
981 /* Finish fc_lport configuration */
982 fc_lport_config(lport);
988 * bnx2fc_fip_recv - handle a received FIP frame.
990 * @skb: the received skb
991 * @dev: associated &net_device
992 * @ptype: the &packet_type structure which was used to register this handler.
993 * @orig_dev: original receive &net_device, in case @ dev is a bond.
995 * Returns: 0 for success
997 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
998 struct packet_type *ptype,
999 struct net_device *orig_dev)
1001 struct bnx2fc_interface *interface;
1002 struct fcoe_ctlr *ctlr;
1003 interface = container_of(ptype, struct bnx2fc_interface,
1005 ctlr = bnx2fc_to_ctlr(interface);
1006 fcoe_ctlr_recv(ctlr, skb);
1011 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1013 * @fip: FCoE controller.
1014 * @old: Unicast MAC address to delete if the MAC is non-zero.
1015 * @new: Unicast MAC address to add.
1017 * Remove any previously-set unicast MAC filter.
1018 * Add secondary FCoE MAC address filter for our OUI.
1020 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1022 struct fcoe_port *port = lport_priv(lport);
1024 memcpy(port->data_src_addr, addr, ETH_ALEN);
1028 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1030 * @lport: libfc port
1032 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1034 struct fcoe_port *port;
1036 port = (struct fcoe_port *)lport_priv(lport);
1037 return port->data_src_addr;
1041 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1043 * @fip: FCoE controller.
1046 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1048 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1049 dev_queue_xmit(skb);
1052 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1054 struct Scsi_Host *shost = vport_to_shost(vport);
1055 struct fc_lport *n_port = shost_priv(shost);
1056 struct fcoe_port *port = lport_priv(n_port);
1057 struct bnx2fc_interface *interface = port->priv;
1058 struct net_device *netdev = interface->netdev;
1059 struct fc_lport *vn_port;
1063 rc = fcoe_validate_vport_create(vport);
1065 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1066 printk(KERN_ERR PFX "Failed to create vport, "
1067 "WWPN (0x%s) already exists\n",
1072 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1073 printk(KERN_ERR PFX "vn ports cannot be created on"
1074 "this interface\n");
1078 mutex_lock(&bnx2fc_dev_lock);
1079 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1080 mutex_unlock(&bnx2fc_dev_lock);
1083 if (IS_ERR(vn_port)) {
1084 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1090 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1092 vn_port->boot_time = jiffies;
1093 fc_lport_init(vn_port);
1094 fc_fabric_login(vn_port);
1095 fc_vport_setlink(vn_port);
1100 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1102 struct bnx2fc_lport *blport, *tmp;
1104 spin_lock_bh(&hba->hba_lock);
1105 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1106 if (blport->lport == lport) {
1107 list_del(&blport->list);
1111 spin_unlock_bh(&hba->hba_lock);
1114 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1116 struct Scsi_Host *shost = vport_to_shost(vport);
1117 struct fc_lport *n_port = shost_priv(shost);
1118 struct fc_lport *vn_port = vport->dd_data;
1119 struct fcoe_port *port = lport_priv(vn_port);
1120 struct bnx2fc_interface *interface = port->priv;
1121 struct fc_lport *v_port;
1124 mutex_lock(&n_port->lp_mutex);
1125 list_for_each_entry(v_port, &n_port->vports, list)
1126 if (v_port->vport == vport) {
1132 mutex_unlock(&n_port->lp_mutex);
1135 list_del(&vn_port->list);
1136 mutex_unlock(&n_port->lp_mutex);
1137 bnx2fc_free_vport(interface->hba, port->lport);
1138 bnx2fc_port_shutdown(port->lport);
1139 bnx2fc_interface_put(interface);
1140 queue_work(bnx2fc_wq, &port->destroy_work);
1144 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1146 struct fc_lport *lport = vport->dd_data;
1149 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1150 fc_fabric_logoff(lport);
1152 lport->boot_time = jiffies;
1153 fc_fabric_login(lport);
1154 fc_vport_setlink(lport);
1160 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1162 struct net_device *netdev = interface->netdev;
1163 struct net_device *physdev = interface->hba->phys_dev;
1164 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1165 struct netdev_hw_addr *ha;
1166 int sel_san_mac = 0;
1168 /* setup Source MAC Address */
1170 for_each_dev_addr(physdev, ha) {
1171 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1173 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1174 ha->addr[1], ha->addr[2], ha->addr[3],
1175 ha->addr[4], ha->addr[5]);
1177 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1178 (is_valid_ether_addr(ha->addr))) {
1179 memcpy(ctlr->ctl_src_addr, ha->addr,
1182 BNX2FC_MISC_DBG("Found SAN MAC\n");
1190 interface->fip_packet_type.func = bnx2fc_fip_recv;
1191 interface->fip_packet_type.type = htons(ETH_P_FIP);
1192 interface->fip_packet_type.dev = netdev;
1193 dev_add_pack(&interface->fip_packet_type);
1195 interface->fcoe_packet_type.func = bnx2fc_rcv;
1196 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1197 interface->fcoe_packet_type.dev = netdev;
1198 dev_add_pack(&interface->fcoe_packet_type);
1203 static int bnx2fc_attach_transport(void)
1205 bnx2fc_transport_template =
1206 fc_attach_transport(&bnx2fc_transport_function);
1208 if (bnx2fc_transport_template == NULL) {
1209 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1213 bnx2fc_vport_xport_template =
1214 fc_attach_transport(&bnx2fc_vport_xport_function);
1215 if (bnx2fc_vport_xport_template == NULL) {
1217 "Failed to attach FC transport for vport\n");
1218 fc_release_transport(bnx2fc_transport_template);
1219 bnx2fc_transport_template = NULL;
1224 static void bnx2fc_release_transport(void)
1226 fc_release_transport(bnx2fc_transport_template);
1227 fc_release_transport(bnx2fc_vport_xport_template);
1228 bnx2fc_transport_template = NULL;
1229 bnx2fc_vport_xport_template = NULL;
1232 static void bnx2fc_interface_release(struct kref *kref)
1234 struct fcoe_ctlr_device *ctlr_dev;
1235 struct bnx2fc_interface *interface;
1236 struct fcoe_ctlr *ctlr;
1237 struct net_device *netdev;
1239 interface = container_of(kref, struct bnx2fc_interface, kref);
1240 BNX2FC_MISC_DBG("Interface is being released\n");
1242 ctlr = bnx2fc_to_ctlr(interface);
1243 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1244 netdev = interface->netdev;
1246 /* tear-down FIP controller */
1247 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1248 fcoe_ctlr_destroy(ctlr);
1250 fcoe_ctlr_device_delete(ctlr_dev);
1253 module_put(THIS_MODULE);
1256 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1258 kref_get(&interface->kref);
1261 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1263 kref_put(&interface->kref, bnx2fc_interface_release);
1265 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1267 /* Free the command manager */
1269 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1270 hba->cmd_mgr = NULL;
1272 kfree(hba->tgt_ofld_list);
1273 bnx2fc_unbind_pcidev(hba);
1278 * bnx2fc_hba_create - create a new bnx2fc hba
1280 * @cnic: pointer to cnic device
1282 * Creates a new FCoE hba on the given device.
1285 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1287 struct bnx2fc_hba *hba;
1288 struct fcoe_capabilities *fcoe_cap;
1291 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1293 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1296 spin_lock_init(&hba->hba_lock);
1297 mutex_init(&hba->hba_mutex);
1301 hba->max_tasks = cnic->max_fcoe_exchanges;
1302 hba->elstm_xids = (hba->max_tasks / 2);
1303 hba->max_outstanding_cmds = hba->elstm_xids;
1304 hba->max_xid = (hba->max_tasks - 1);
1306 rc = bnx2fc_bind_pcidev(hba);
1308 printk(KERN_ERR PFX "create_adapter: bind error\n");
1311 hba->phys_dev = cnic->netdev;
1312 hba->next_conn_id = 0;
1314 hba->tgt_ofld_list =
1315 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1317 if (!hba->tgt_ofld_list) {
1318 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1322 hba->num_ofld_sess = 0;
1324 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1325 if (!hba->cmd_mgr) {
1326 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1329 fcoe_cap = &hba->fcoe_cap;
1331 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1332 FCOE_IOS_PER_CONNECTION_SHIFT;
1333 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1334 FCOE_LOGINS_PER_PORT_SHIFT;
1335 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1336 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1337 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1338 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1339 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1340 FCOE_TARGETS_SUPPORTED_SHIFT;
1341 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1342 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1343 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1345 init_waitqueue_head(&hba->shutdown_wait);
1346 init_waitqueue_head(&hba->destroy_wait);
1347 INIT_LIST_HEAD(&hba->vports);
1352 kfree(hba->tgt_ofld_list);
1354 bnx2fc_unbind_pcidev(hba);
1360 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1361 struct net_device *netdev,
1362 enum fip_state fip_mode)
1364 struct fcoe_ctlr_device *ctlr_dev;
1365 struct bnx2fc_interface *interface;
1366 struct fcoe_ctlr *ctlr;
1370 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1371 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1374 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1377 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1378 ctlr->cdev = ctlr_dev;
1379 interface = fcoe_ctlr_priv(ctlr);
1381 kref_init(&interface->kref);
1382 interface->hba = hba;
1383 interface->netdev = netdev;
1385 /* Initialize FIP */
1386 fcoe_ctlr_init(ctlr, fip_mode);
1387 ctlr->send = bnx2fc_fip_send;
1388 ctlr->update_mac = bnx2fc_update_src_mac;
1389 ctlr->get_src_addr = bnx2fc_get_src_mac;
1390 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1392 rc = bnx2fc_interface_setup(interface);
1396 fcoe_ctlr_destroy(ctlr);
1398 fcoe_ctlr_device_delete(ctlr_dev);
1403 * bnx2fc_if_create - Create FCoE instance on a given interface
1405 * @interface: FCoE interface to create a local port on
1406 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1407 * @npiv: Indicates if the port is vport or not
1409 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1411 * Returns: Allocated fc_lport or an error pointer
1413 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1414 struct device *parent, int npiv)
1416 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1417 struct fc_lport *lport, *n_port;
1418 struct fcoe_port *port;
1419 struct Scsi_Host *shost;
1420 struct fc_vport *vport = dev_to_vport(parent);
1421 struct bnx2fc_lport *blport;
1422 struct bnx2fc_hba *hba = interface->hba;
1425 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1427 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1431 /* Allocate Scsi_Host structure */
1432 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1434 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1436 lport = libfc_vport_create(vport, sizeof(*port));
1439 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1442 shost = lport->host;
1443 port = lport_priv(lport);
1444 port->lport = lport;
1445 port->priv = interface;
1446 port->get_netdev = bnx2fc_netdev;
1447 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1449 /* Configure fcoe_port */
1450 rc = bnx2fc_lport_config(lport);
1455 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1456 vport->node_name, vport->port_name);
1457 fc_set_wwnn(lport, vport->node_name);
1458 fc_set_wwpn(lport, vport->port_name);
1460 /* Configure netdev and networking properties of the lport */
1461 rc = bnx2fc_net_config(lport, interface->netdev);
1463 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1467 rc = bnx2fc_shost_config(lport, parent);
1469 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1470 interface->netdev->name);
1474 /* Initialize the libfc library */
1475 rc = bnx2fc_libfc_config(lport);
1477 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1480 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1482 /* Allocate exchange manager */
1484 rc = bnx2fc_em_config(lport, hba);
1486 shost = vport_to_shost(vport);
1487 n_port = shost_priv(shost);
1488 rc = fc_exch_mgr_list_clone(n_port, lport);
1492 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1496 bnx2fc_interface_get(interface);
1498 spin_lock_bh(&hba->hba_lock);
1499 blport->lport = lport;
1500 list_add_tail(&blport->list, &hba->vports);
1501 spin_unlock_bh(&hba->hba_lock);
1506 scsi_remove_host(shost);
1508 scsi_host_put(lport->host);
1514 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1516 /* Dont listen for Ethernet packets anymore */
1517 __dev_remove_pack(&interface->fcoe_packet_type);
1518 __dev_remove_pack(&interface->fip_packet_type);
1522 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1524 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1525 struct fc_lport *lport = ctlr->lp;
1526 struct fcoe_port *port = lport_priv(lport);
1527 struct bnx2fc_hba *hba = interface->hba;
1529 /* Stop the transmit retry timer */
1530 del_timer_sync(&port->timer);
1532 /* Free existing transmit skbs */
1533 fcoe_clean_pending_queue(lport);
1535 bnx2fc_net_cleanup(interface);
1537 bnx2fc_free_vport(hba, lport);
1540 static void bnx2fc_if_destroy(struct fc_lport *lport)
1543 /* Free queued packets for the receive thread */
1544 bnx2fc_clean_rx_queue(lport);
1546 /* Detach from scsi-ml */
1547 fc_remove_host(lport->host);
1548 scsi_remove_host(lport->host);
1551 * Note that only the physical lport will have the exchange manager.
1552 * for vports, this function is NOP
1554 fc_exch_mgr_free(lport);
1556 /* Free memory used by statistical counters */
1557 fc_lport_free_stats(lport);
1559 /* Release Scsi_Host */
1560 scsi_host_put(lport->host);
1563 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1565 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1566 struct fc_lport *lport = ctlr->lp;
1567 struct fcoe_port *port = lport_priv(lport);
1569 bnx2fc_interface_cleanup(interface);
1570 bnx2fc_stop(interface);
1571 list_del(&interface->list);
1572 bnx2fc_interface_put(interface);
1573 queue_work(bnx2fc_wq, &port->destroy_work);
1577 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1579 * @buffer: The name of the Ethernet interface to be destroyed
1580 * @kp: The associated kernel parameter
1582 * Called from sysfs.
1584 * Returns: 0 for success
1586 static int bnx2fc_destroy(struct net_device *netdev)
1588 struct bnx2fc_interface *interface = NULL;
1589 struct workqueue_struct *timer_work_queue;
1590 struct fcoe_ctlr *ctlr;
1594 mutex_lock(&bnx2fc_dev_lock);
1596 interface = bnx2fc_interface_lookup(netdev);
1597 ctlr = bnx2fc_to_ctlr(interface);
1598 if (!interface || !ctlr->lp) {
1600 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1604 timer_work_queue = interface->timer_work_queue;
1605 __bnx2fc_destroy(interface);
1606 destroy_workqueue(timer_work_queue);
1609 mutex_unlock(&bnx2fc_dev_lock);
1614 static void bnx2fc_destroy_work(struct work_struct *work)
1616 struct fcoe_port *port;
1617 struct fc_lport *lport;
1619 port = container_of(work, struct fcoe_port, destroy_work);
1620 lport = port->lport;
1622 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1624 bnx2fc_if_destroy(lport);
1627 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1629 bnx2fc_free_fw_resc(hba);
1630 bnx2fc_free_task_ctx(hba);
1634 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1637 * @hba: Adapter instance
1639 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1641 if (bnx2fc_setup_task_ctx(hba))
1644 if (bnx2fc_setup_fw_resc(hba))
1649 bnx2fc_unbind_adapter_devices(hba);
1653 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1655 struct cnic_dev *cnic;
1656 struct pci_dev *pdev;
1659 printk(KERN_ERR PFX "cnic is NULL\n");
1663 pdev = hba->pcidev = cnic->pcidev;
1667 switch (pdev->device) {
1668 case PCI_DEVICE_ID_NX2_57710:
1669 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1671 case PCI_DEVICE_ID_NX2_57711:
1672 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1674 case PCI_DEVICE_ID_NX2_57712:
1675 case PCI_DEVICE_ID_NX2_57712_MF:
1676 case PCI_DEVICE_ID_NX2_57712_VF:
1677 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1679 case PCI_DEVICE_ID_NX2_57800:
1680 case PCI_DEVICE_ID_NX2_57800_MF:
1681 case PCI_DEVICE_ID_NX2_57800_VF:
1682 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1684 case PCI_DEVICE_ID_NX2_57810:
1685 case PCI_DEVICE_ID_NX2_57810_MF:
1686 case PCI_DEVICE_ID_NX2_57810_VF:
1687 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1689 case PCI_DEVICE_ID_NX2_57840:
1690 case PCI_DEVICE_ID_NX2_57840_MF:
1691 case PCI_DEVICE_ID_NX2_57840_VF:
1692 case PCI_DEVICE_ID_NX2_57840_2_20:
1693 case PCI_DEVICE_ID_NX2_57840_4_10:
1694 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1697 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1700 pci_dev_get(hba->pcidev);
1704 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1707 hba->chip_num[0] = '\0';
1708 pci_dev_put(hba->pcidev);
1714 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1716 * @handle: transport handle pointing to adapter struture
1718 static int bnx2fc_ulp_get_stats(void *handle)
1720 struct bnx2fc_hba *hba = handle;
1721 struct cnic_dev *cnic;
1722 struct fcoe_stats_info *stats_addr;
1728 stats_addr = &cnic->stats_addr->fcoe_stat;
1732 strncpy(stats_addr->version, BNX2FC_VERSION,
1733 sizeof(stats_addr->version));
1734 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1735 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1742 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1744 * @handle: transport handle pointing to adapter structure
1746 * This function maps adapter structure to pcidev structure and initiates
1747 * firmware handshake to enable/initialize on-chip FCoE components.
1748 * This bnx2fc - cnic interface api callback is used after following
1749 * conditions are met -
1750 * a) underlying network interface is up (marked by event NETDEV_UP
1752 * b) bnx2fc adatper structure is registered.
1754 static void bnx2fc_ulp_start(void *handle)
1756 struct bnx2fc_hba *hba = handle;
1757 struct bnx2fc_interface *interface;
1758 struct fcoe_ctlr *ctlr;
1759 struct fc_lport *lport;
1761 mutex_lock(&bnx2fc_dev_lock);
1763 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1764 bnx2fc_fw_init(hba);
1766 BNX2FC_MISC_DBG("bnx2fc started.\n");
1768 list_for_each_entry(interface, &if_list, list) {
1769 if (interface->hba == hba) {
1770 ctlr = bnx2fc_to_ctlr(interface);
1772 /* Kick off Fabric discovery*/
1773 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1774 lport->tt.frame_send = bnx2fc_xmit;
1775 bnx2fc_start_disc(interface);
1779 mutex_unlock(&bnx2fc_dev_lock);
1782 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1784 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1785 fc_fabric_logoff(lport);
1786 fc_lport_destroy(lport);
1789 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1791 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1792 struct fc_lport *lport;
1793 struct fc_lport *vport;
1795 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1799 bnx2fc_port_shutdown(lport);
1801 mutex_lock(&lport->lp_mutex);
1802 list_for_each_entry(vport, &lport->vports, list)
1803 fc_host_port_type(vport->host) =
1804 FC_PORTTYPE_UNKNOWN;
1805 mutex_unlock(&lport->lp_mutex);
1806 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1807 fcoe_ctlr_link_down(ctlr);
1808 fcoe_clean_pending_queue(lport);
1811 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1813 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1817 rc = bnx2fc_bind_adapter_devices(hba);
1819 printk(KERN_ALERT PFX
1820 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1824 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1826 printk(KERN_ALERT PFX
1827 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1832 * Wait until the adapter init message is complete, and adapter
1835 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1836 msleep(BNX2FC_INIT_POLL_TIME);
1838 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1839 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1841 hba->cnic->netdev->name);
1847 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1851 bnx2fc_unbind_adapter_devices(hba);
1856 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1858 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1859 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1860 init_timer(&hba->destroy_timer);
1861 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1863 hba->destroy_timer.function = bnx2fc_destroy_timer;
1864 hba->destroy_timer.data = (unsigned long)hba;
1865 add_timer(&hba->destroy_timer);
1866 wait_event_interruptible(hba->destroy_wait,
1867 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1869 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1870 /* This should never happen */
1871 if (signal_pending(current))
1872 flush_signals(current);
1874 del_timer_sync(&hba->destroy_timer);
1876 bnx2fc_unbind_adapter_devices(hba);
1881 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1883 * @handle: transport handle pointing to adapter structure
1885 * Driver checks if adapter is already in shutdown mode, if not start
1886 * the shutdown process.
1888 static void bnx2fc_ulp_stop(void *handle)
1890 struct bnx2fc_hba *hba = handle;
1891 struct bnx2fc_interface *interface;
1893 printk(KERN_ERR "ULP_STOP\n");
1895 mutex_lock(&bnx2fc_dev_lock);
1896 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1898 list_for_each_entry(interface, &if_list, list) {
1899 if (interface->hba == hba)
1900 bnx2fc_stop(interface);
1902 BUG_ON(hba->num_ofld_sess != 0);
1904 mutex_lock(&hba->hba_mutex);
1905 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1906 clear_bit(ADAPTER_STATE_GOING_DOWN,
1907 &hba->adapter_state);
1909 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1910 mutex_unlock(&hba->hba_mutex);
1912 bnx2fc_fw_destroy(hba);
1914 mutex_unlock(&bnx2fc_dev_lock);
1917 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1919 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1920 struct fc_lport *lport;
1923 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1924 /* Kick off FIP/FLOGI */
1925 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1926 printk(KERN_ERR PFX "Init not done yet\n");
1931 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1933 if (!bnx2fc_link_ok(lport) && interface->enabled) {
1934 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1935 fcoe_ctlr_link_up(ctlr);
1936 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1937 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1940 /* wait for the FCF to be selected before issuing FLOGI */
1941 while (!ctlr->sel_fcf) {
1943 /* give up after 3 secs */
1944 if (++wait_cnt > 12)
1948 /* Reset max receive frame size to default */
1949 if (fc_set_mfs(lport, BNX2FC_MFS))
1952 fc_lport_init(lport);
1953 fc_fabric_login(lport);
1958 * bnx2fc_ulp_init - Initialize an adapter instance
1960 * @dev : cnic device handle
1961 * Called from cnic_register_driver() context to initialize all
1962 * enumerated cnic devices. This routine allocates adapter structure
1963 * and other device specific resources.
1965 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1967 struct bnx2fc_hba *hba;
1970 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1971 /* bnx2fc works only when bnx2x is loaded */
1972 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1973 (dev->max_fcoe_conn == 0)) {
1974 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1975 " flags: %lx fcoe_conn: %d\n",
1976 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1980 hba = bnx2fc_hba_create(dev);
1982 printk(KERN_ERR PFX "hba initialization failed\n");
1986 /* Add HBA to the adapter list */
1987 mutex_lock(&bnx2fc_dev_lock);
1988 list_add_tail(&hba->list, &adapter_list);
1990 mutex_unlock(&bnx2fc_dev_lock);
1992 dev->fcoe_cap = &hba->fcoe_cap;
1993 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1994 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1997 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
1999 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2002 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2003 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2005 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2007 if (interface->enabled == true) {
2009 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2012 interface->enabled = false;
2013 fcoe_ctlr_link_down(ctlr);
2014 fcoe_clean_pending_queue(ctlr->lp);
2021 * Deperecated: Use bnx2fc_enabled()
2023 static int bnx2fc_disable(struct net_device *netdev)
2025 struct bnx2fc_interface *interface;
2026 struct fcoe_ctlr *ctlr;
2030 mutex_lock(&bnx2fc_dev_lock);
2032 interface = bnx2fc_interface_lookup(netdev);
2033 ctlr = bnx2fc_to_ctlr(interface);
2037 pr_err(PFX "bnx2fc_disable: interface not found\n");
2039 rc = __bnx2fc_disable(ctlr);
2041 mutex_unlock(&bnx2fc_dev_lock);
2046 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2048 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2050 if (interface->enabled == false) {
2052 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2054 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2055 fcoe_ctlr_link_up(ctlr);
2056 interface->enabled = true;
2063 * Deprecated: Use bnx2fc_enabled()
2065 static int bnx2fc_enable(struct net_device *netdev)
2067 struct bnx2fc_interface *interface;
2068 struct fcoe_ctlr *ctlr;
2072 mutex_lock(&bnx2fc_dev_lock);
2074 interface = bnx2fc_interface_lookup(netdev);
2075 ctlr = bnx2fc_to_ctlr(interface);
2078 pr_err(PFX "bnx2fc_enable: interface not found\n");
2080 rc = __bnx2fc_enable(ctlr);
2083 mutex_unlock(&bnx2fc_dev_lock);
2089 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2090 * @cdev: The FCoE Controller that is being enabled or disabled
2092 * fcoe_sysfs will ensure that the state of 'enabled' has
2093 * changed, so no checking is necessary here. This routine simply
2094 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2095 * When those routines are removed the functionality can be merged
2098 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2100 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2102 switch (cdev->enabled) {
2103 case FCOE_CTLR_ENABLED:
2104 return __bnx2fc_enable(ctlr);
2105 case FCOE_CTLR_DISABLED:
2106 return __bnx2fc_disable(ctlr);
2107 case FCOE_CTLR_UNUSED:
2113 enum bnx2fc_create_link_state {
2114 BNX2FC_CREATE_LINK_DOWN,
2115 BNX2FC_CREATE_LINK_UP,
2119 * _bnx2fc_create() - Create bnx2fc FCoE interface
2120 * @netdev : The net_device object the Ethernet interface to create on
2121 * @fip_mode: The FIP mode for this creation
2122 * @link_state: The ctlr link state on creation
2124 * Called from either the libfcoe 'create' module parameter
2125 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2127 * libfcoe's 'create' module parameter is deprecated so some
2128 * consolidation of code can be done when that interface is
2131 * Returns: 0 for success
2133 static int _bnx2fc_create(struct net_device *netdev,
2134 enum fip_state fip_mode,
2135 enum bnx2fc_create_link_state link_state)
2137 struct fcoe_ctlr_device *cdev;
2138 struct fcoe_ctlr *ctlr;
2139 struct bnx2fc_interface *interface;
2140 struct bnx2fc_hba *hba;
2141 struct net_device *phys_dev = netdev;
2142 struct fc_lport *lport;
2143 struct ethtool_drvinfo drvinfo;
2147 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2148 if (fip_mode != FIP_MODE_FABRIC) {
2149 printk(KERN_ERR "fip mode not FABRIC\n");
2155 mutex_lock(&bnx2fc_dev_lock);
2157 if (!try_module_get(THIS_MODULE)) {
2162 /* obtain physical netdev */
2163 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2164 phys_dev = vlan_dev_real_dev(netdev);
2166 /* verify if the physical device is a netxtreme2 device */
2167 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2168 memset(&drvinfo, 0, sizeof(drvinfo));
2169 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2170 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2171 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2176 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2181 /* obtain interface and initialize rest of the structure */
2182 hba = bnx2fc_hba_lookup(phys_dev);
2185 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2189 if (bnx2fc_interface_lookup(netdev)) {
2194 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2196 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2200 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2201 vlan_id = vlan_dev_vlan_id(netdev);
2202 interface->vlan_enabled = 1;
2205 ctlr = bnx2fc_to_ctlr(interface);
2206 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2207 interface->vlan_id = vlan_id;
2209 interface->timer_work_queue =
2210 create_singlethread_workqueue("bnx2fc_timer_wq");
2211 if (!interface->timer_work_queue) {
2212 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2217 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2219 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2225 /* Add interface to if_list */
2226 list_add_tail(&interface->list, &if_list);
2228 lport->boot_time = jiffies;
2230 /* Make this master N_port */
2233 if (link_state == BNX2FC_CREATE_LINK_UP)
2234 cdev->enabled = FCOE_CTLR_ENABLED;
2236 cdev->enabled = FCOE_CTLR_DISABLED;
2238 if (link_state == BNX2FC_CREATE_LINK_UP &&
2239 !bnx2fc_link_ok(lport)) {
2240 fcoe_ctlr_link_up(ctlr);
2241 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2242 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2245 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2246 bnx2fc_start_disc(interface);
2248 if (link_state == BNX2FC_CREATE_LINK_UP)
2249 interface->enabled = true;
2252 * Release from kref_init in bnx2fc_interface_setup, on success
2253 * lport should be holding a reference taken in bnx2fc_if_create
2255 bnx2fc_interface_put(interface);
2256 /* put netdev that was held while calling dev_get_by_name */
2257 mutex_unlock(&bnx2fc_dev_lock);
2262 destroy_workqueue(interface->timer_work_queue);
2264 bnx2fc_net_cleanup(interface);
2265 bnx2fc_interface_put(interface);
2268 module_put(THIS_MODULE);
2270 mutex_unlock(&bnx2fc_dev_lock);
2276 * bnx2fc_create() - Create a bnx2fc interface
2277 * @netdev : The net_device object the Ethernet interface to create on
2278 * @fip_mode: The FIP mode for this creation
2280 * Called from fcoe transport
2282 * Returns: 0 for success
2284 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2286 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2290 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2291 * @netdev: The net_device to be used by the allocated FCoE Controller
2293 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2294 * in a link_down state. The allows the user an opportunity to configure
2295 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2297 * Creating in with this routine starts the FCoE Controller in Fabric
2298 * mode. The user can change to VN2VN or another mode before enabling.
2300 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2302 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2303 BNX2FC_CREATE_LINK_DOWN);
2307 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2309 * @cnic: Pointer to cnic device instance
2312 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2314 struct bnx2fc_hba *hba;
2316 /* Called with bnx2fc_dev_lock held */
2317 list_for_each_entry(hba, &adapter_list, list) {
2318 if (hba->cnic == cnic)
2324 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2327 struct bnx2fc_interface *interface;
2329 /* Called with bnx2fc_dev_lock held */
2330 list_for_each_entry(interface, &if_list, list) {
2331 if (interface->netdev == netdev)
2337 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2340 struct bnx2fc_hba *hba;
2342 /* Called with bnx2fc_dev_lock held */
2343 list_for_each_entry(hba, &adapter_list, list) {
2344 if (hba->phys_dev == phys_dev)
2347 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2352 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2354 * @dev cnic device handle
2356 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2358 struct bnx2fc_hba *hba;
2359 struct bnx2fc_interface *interface, *tmp;
2361 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2363 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2364 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2365 dev->netdev->name, dev->flags);
2369 mutex_lock(&bnx2fc_dev_lock);
2370 hba = bnx2fc_find_hba_for_cnic(dev);
2372 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2374 mutex_unlock(&bnx2fc_dev_lock);
2378 list_del_init(&hba->list);
2381 list_for_each_entry_safe(interface, tmp, &if_list, list)
2382 /* destroy not called yet, move to quiesced list */
2383 if (interface->hba == hba)
2384 __bnx2fc_destroy(interface);
2385 mutex_unlock(&bnx2fc_dev_lock);
2387 /* Ensure ALL destroy work has been completed before return */
2388 flush_workqueue(bnx2fc_wq);
2390 bnx2fc_ulp_stop(hba);
2391 /* unregister cnic device */
2392 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2393 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2394 bnx2fc_hba_destroy(hba);
2398 * bnx2fc_fcoe_reset - Resets the fcoe
2400 * @shost: shost the reset is from
2404 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2406 struct fc_lport *lport = shost_priv(shost);
2407 fc_lport_reset(lport);
2412 static bool bnx2fc_match(struct net_device *netdev)
2414 struct net_device *phys_dev = netdev;
2416 mutex_lock(&bnx2fc_dev_lock);
2417 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2418 phys_dev = vlan_dev_real_dev(netdev);
2420 if (bnx2fc_hba_lookup(phys_dev)) {
2421 mutex_unlock(&bnx2fc_dev_lock);
2425 mutex_unlock(&bnx2fc_dev_lock);
2430 static struct fcoe_transport bnx2fc_transport = {
2433 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2434 .alloc = bnx2fc_ctlr_alloc,
2435 .match = bnx2fc_match,
2436 .create = bnx2fc_create,
2437 .destroy = bnx2fc_destroy,
2438 .enable = bnx2fc_enable,
2439 .disable = bnx2fc_disable,
2443 * bnx2fc_percpu_thread_create - Create a receive thread for an
2446 * @cpu: cpu index for the online cpu
2448 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2450 struct bnx2fc_percpu_s *p;
2451 struct task_struct *thread;
2453 p = &per_cpu(bnx2fc_percpu, cpu);
2455 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2456 (void *)p, cpu_to_node(cpu),
2457 "bnx2fc_thread/%d", cpu);
2458 /* bind thread to the cpu */
2459 if (likely(!IS_ERR(thread))) {
2460 kthread_bind(thread, cpu);
2461 p->iothread = thread;
2462 wake_up_process(thread);
2466 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2468 struct bnx2fc_percpu_s *p;
2469 struct task_struct *thread;
2470 struct bnx2fc_work *work, *tmp;
2472 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2474 /* Prevent any new work from being queued for this CPU */
2475 p = &per_cpu(bnx2fc_percpu, cpu);
2476 spin_lock_bh(&p->fp_work_lock);
2477 thread = p->iothread;
2481 /* Free all work in the list */
2482 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2483 list_del_init(&work->list);
2484 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2488 spin_unlock_bh(&p->fp_work_lock);
2491 kthread_stop(thread);
2495 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2497 * @nfb: The callback data block
2498 * @action: The event triggering the callback
2499 * @hcpu: The index of the CPU that the event is for
2501 * This creates or destroys per-CPU data for fcoe
2503 * Returns NOTIFY_OK always.
2505 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2506 unsigned long action, void *hcpu)
2508 unsigned cpu = (unsigned long)hcpu;
2512 case CPU_ONLINE_FROZEN:
2513 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2514 bnx2fc_percpu_thread_create(cpu);
2517 case CPU_DEAD_FROZEN:
2518 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2519 bnx2fc_percpu_thread_destroy(cpu);
2528 * bnx2fc_mod_init - module init entry point
2530 * Initialize driver wide global data structures, and register
2533 static int __init bnx2fc_mod_init(void)
2535 struct fcoe_percpu_s *bg;
2536 struct task_struct *l2_thread;
2538 unsigned int cpu = 0;
2539 struct bnx2fc_percpu_s *p;
2541 printk(KERN_INFO PFX "%s", version);
2543 /* register as a fcoe transport */
2544 rc = fcoe_transport_attach(&bnx2fc_transport);
2546 printk(KERN_ERR "failed to register an fcoe transport, check "
2547 "if libfcoe is loaded\n");
2551 INIT_LIST_HEAD(&adapter_list);
2552 INIT_LIST_HEAD(&if_list);
2553 mutex_init(&bnx2fc_dev_lock);
2556 /* Attach FC transport template */
2557 rc = bnx2fc_attach_transport();
2561 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2567 bg = &bnx2fc_global;
2568 skb_queue_head_init(&bg->fcoe_rx_list);
2569 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2571 "bnx2fc_l2_thread");
2572 if (IS_ERR(l2_thread)) {
2573 rc = PTR_ERR(l2_thread);
2576 wake_up_process(l2_thread);
2577 spin_lock_bh(&bg->fcoe_rx_list.lock);
2578 bg->thread = l2_thread;
2579 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2581 for_each_possible_cpu(cpu) {
2582 p = &per_cpu(bnx2fc_percpu, cpu);
2583 INIT_LIST_HEAD(&p->work_list);
2584 spin_lock_init(&p->fp_work_lock);
2587 cpu_notifier_register_begin();
2589 for_each_online_cpu(cpu) {
2590 bnx2fc_percpu_thread_create(cpu);
2593 /* Initialize per CPU interrupt thread */
2594 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2596 cpu_notifier_register_done();
2598 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2603 destroy_workqueue(bnx2fc_wq);
2605 bnx2fc_release_transport();
2607 fcoe_transport_detach(&bnx2fc_transport);
2612 static void __exit bnx2fc_mod_exit(void)
2614 LIST_HEAD(to_be_deleted);
2615 struct bnx2fc_hba *hba, *next;
2616 struct fcoe_percpu_s *bg;
2617 struct task_struct *l2_thread;
2618 struct sk_buff *skb;
2619 unsigned int cpu = 0;
2622 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2623 * it will have higher precedence than bnx2fc_dev_lock.
2624 * unregister_device() cannot be called with bnx2fc_dev_lock
2627 mutex_lock(&bnx2fc_dev_lock);
2628 list_splice(&adapter_list, &to_be_deleted);
2629 INIT_LIST_HEAD(&adapter_list);
2631 mutex_unlock(&bnx2fc_dev_lock);
2633 /* Unregister with cnic */
2634 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2635 list_del_init(&hba->list);
2636 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2638 bnx2fc_ulp_stop(hba);
2639 /* unregister cnic device */
2640 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2641 &hba->reg_with_cnic))
2642 hba->cnic->unregister_device(hba->cnic,
2644 bnx2fc_hba_destroy(hba);
2646 cnic_unregister_driver(CNIC_ULP_FCOE);
2648 /* Destroy global thread */
2649 bg = &bnx2fc_global;
2650 spin_lock_bh(&bg->fcoe_rx_list.lock);
2651 l2_thread = bg->thread;
2653 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2656 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2659 kthread_stop(l2_thread);
2661 cpu_notifier_register_begin();
2663 /* Destroy per cpu threads */
2664 for_each_online_cpu(cpu) {
2665 bnx2fc_percpu_thread_destroy(cpu);
2668 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2670 cpu_notifier_register_done();
2672 destroy_workqueue(bnx2fc_wq);
2674 * detach from scsi transport
2675 * must happen after all destroys are done
2677 bnx2fc_release_transport();
2679 /* detach from fcoe transport */
2680 fcoe_transport_detach(&bnx2fc_transport);
2683 module_init(bnx2fc_mod_init);
2684 module_exit(bnx2fc_mod_exit);
2686 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2687 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2688 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2689 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2690 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2691 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2692 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2693 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2695 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2696 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2699 static struct fc_function_template bnx2fc_transport_function = {
2700 .show_host_node_name = 1,
2701 .show_host_port_name = 1,
2702 .show_host_supported_classes = 1,
2703 .show_host_supported_fc4s = 1,
2704 .show_host_active_fc4s = 1,
2705 .show_host_maxframe_size = 1,
2707 .show_host_port_id = 1,
2708 .show_host_supported_speeds = 1,
2709 .get_host_speed = fc_get_host_speed,
2710 .show_host_speed = 1,
2711 .show_host_port_type = 1,
2712 .get_host_port_state = fc_get_host_port_state,
2713 .show_host_port_state = 1,
2714 .show_host_symbolic_name = 1,
2716 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2717 sizeof(struct bnx2fc_rport)),
2718 .show_rport_maxframe_size = 1,
2719 .show_rport_supported_classes = 1,
2721 .show_host_fabric_name = 1,
2722 .show_starget_node_name = 1,
2723 .show_starget_port_name = 1,
2724 .show_starget_port_id = 1,
2725 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2726 .show_rport_dev_loss_tmo = 1,
2727 .get_fc_host_stats = bnx2fc_get_host_stats,
2729 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2731 .terminate_rport_io = fc_rport_terminate_io,
2733 .vport_create = bnx2fc_vport_create,
2734 .vport_delete = bnx2fc_vport_destroy,
2735 .vport_disable = bnx2fc_vport_disable,
2736 .bsg_request = fc_lport_bsg_request,
2739 static struct fc_function_template bnx2fc_vport_xport_function = {
2740 .show_host_node_name = 1,
2741 .show_host_port_name = 1,
2742 .show_host_supported_classes = 1,
2743 .show_host_supported_fc4s = 1,
2744 .show_host_active_fc4s = 1,
2745 .show_host_maxframe_size = 1,
2747 .show_host_port_id = 1,
2748 .show_host_supported_speeds = 1,
2749 .get_host_speed = fc_get_host_speed,
2750 .show_host_speed = 1,
2751 .show_host_port_type = 1,
2752 .get_host_port_state = fc_get_host_port_state,
2753 .show_host_port_state = 1,
2754 .show_host_symbolic_name = 1,
2756 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2757 sizeof(struct bnx2fc_rport)),
2758 .show_rport_maxframe_size = 1,
2759 .show_rport_supported_classes = 1,
2761 .show_host_fabric_name = 1,
2762 .show_starget_node_name = 1,
2763 .show_starget_port_name = 1,
2764 .show_starget_port_id = 1,
2765 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2766 .show_rport_dev_loss_tmo = 1,
2767 .get_fc_host_stats = fc_get_host_stats,
2768 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2769 .terminate_rport_io = fc_rport_terminate_io,
2770 .bsg_request = fc_lport_bsg_request,
2774 * scsi_host_template structure used while registering with SCSI-ml
2776 static struct scsi_host_template bnx2fc_shost_template = {
2777 .module = THIS_MODULE,
2778 .name = "Broadcom Offload FCoE Initiator",
2779 .queuecommand = bnx2fc_queuecommand,
2780 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2781 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2782 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2783 .eh_host_reset_handler = fc_eh_host_reset,
2784 .slave_alloc = fc_slave_alloc,
2785 .change_queue_depth = fc_change_queue_depth,
2786 .change_queue_type = fc_change_queue_type,
2789 .use_clustering = ENABLE_CLUSTERING,
2790 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2791 .max_sectors = 1024,
2794 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2795 .frame_send = bnx2fc_xmit,
2796 .elsct_send = bnx2fc_elsct_send,
2797 .fcp_abort_io = bnx2fc_abort_io,
2798 .fcp_cleanup = bnx2fc_cleanup,
2799 .get_lesb = fcoe_get_lesb,
2800 .rport_event_callback = bnx2fc_rport_event_handler,
2804 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2805 * structure carrying callback function pointers
2807 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2808 .owner = THIS_MODULE,
2809 .cnic_init = bnx2fc_ulp_init,
2810 .cnic_exit = bnx2fc_ulp_exit,
2811 .cnic_start = bnx2fc_ulp_start,
2812 .cnic_stop = bnx2fc_ulp_stop,
2813 .indicate_kcqes = bnx2fc_indicate_kcqe,
2814 .indicate_netevent = bnx2fc_indicate_netevent,
2815 .cnic_get_stats = bnx2fc_ulp_get_stats,