1 /* bnx2fc_fcoe.c: QLogic 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
7 * Copyright (c) 2014, QLogic Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
13 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
18 static struct list_head adapter_list;
19 static struct list_head if_list;
20 static u32 adapter_count;
21 static DEFINE_MUTEX(bnx2fc_dev_lock);
22 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
24 #define DRV_MODULE_NAME "bnx2fc"
25 #define DRV_MODULE_VERSION BNX2FC_VERSION
26 #define DRV_MODULE_RELDATE "Dec 11, 2013"
29 static char version[] =
30 "QLogic NetXtreme II FCoE Driver " DRV_MODULE_NAME \
31 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
34 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
35 MODULE_DESCRIPTION("QLogic NetXtreme II BCM57710 FCoE Driver");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
39 #define BNX2FC_MAX_QUEUE_DEPTH 256
40 #define BNX2FC_MIN_QUEUE_DEPTH 32
41 #define FCOE_WORD_TO_BYTE 4
43 static struct scsi_transport_template *bnx2fc_transport_template;
44 static struct scsi_transport_template *bnx2fc_vport_xport_template;
46 struct workqueue_struct *bnx2fc_wq;
48 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
49 * Here the io threads are per cpu but the l2 thread is just one
51 struct fcoe_percpu_s bnx2fc_global;
52 DEFINE_SPINLOCK(bnx2fc_global_lock);
54 static struct cnic_ulp_ops bnx2fc_cnic_cb;
55 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
56 static struct scsi_host_template bnx2fc_shost_template;
57 static struct fc_function_template bnx2fc_transport_function;
58 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
59 static struct fc_function_template bnx2fc_vport_xport_function;
60 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
61 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
62 static int bnx2fc_destroy(struct net_device *net_device);
63 static int bnx2fc_enable(struct net_device *netdev);
64 static int bnx2fc_disable(struct net_device *netdev);
66 /* fcoe_syfs control interface handlers */
67 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
68 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
70 static void bnx2fc_recv_frame(struct sk_buff *skb);
72 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
73 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
74 static int bnx2fc_lport_config(struct fc_lport *lport);
75 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
76 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
77 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
78 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
79 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
80 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
81 struct device *parent, int npiv);
82 static void bnx2fc_destroy_work(struct work_struct *work);
84 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
85 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
87 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
88 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
90 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
91 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
93 static void bnx2fc_port_shutdown(struct fc_lport *lport);
94 static void bnx2fc_stop(struct bnx2fc_interface *interface);
95 static int __init bnx2fc_mod_init(void);
96 static void __exit bnx2fc_mod_exit(void);
98 unsigned int bnx2fc_debug_level;
99 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
101 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
102 unsigned long action, void *hcpu);
103 /* notification function for CPU hotplug events */
104 static struct notifier_block bnx2fc_cpu_notifier = {
105 .notifier_call = bnx2fc_cpu_callback,
108 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
110 return ((struct bnx2fc_interface *)
111 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
114 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
116 struct fcoe_ctlr_device *ctlr_dev =
117 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
118 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
119 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
121 fcf_dev->vlan_id = fcoe->vlan_id;
124 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
126 struct fcoe_percpu_s *bg;
127 struct fcoe_rcv_info *fr;
128 struct sk_buff_head *list;
129 struct sk_buff *skb, *next;
130 struct sk_buff *head;
133 spin_lock_bh(&bg->fcoe_rx_list.lock);
134 list = &bg->fcoe_rx_list;
136 for (skb = head; skb != (struct sk_buff *)list;
139 fr = fcoe_dev_from_skb(skb);
140 if (fr->fr_dev == lp) {
141 __skb_unlink(skb, list);
145 spin_unlock_bh(&bg->fcoe_rx_list.lock);
148 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
151 spin_lock(&bnx2fc_global_lock);
152 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
153 spin_unlock(&bnx2fc_global_lock);
158 static void bnx2fc_abort_io(struct fc_lport *lport)
161 * This function is no-op for bnx2fc, but we do
162 * not want to leave it as NULL either, as libfc
163 * can call the default function which is
168 static void bnx2fc_cleanup(struct fc_lport *lport)
170 struct fcoe_port *port = lport_priv(lport);
171 struct bnx2fc_interface *interface = port->priv;
172 struct bnx2fc_hba *hba = interface->hba;
173 struct bnx2fc_rport *tgt;
176 BNX2FC_MISC_DBG("Entered %s\n", __func__);
177 mutex_lock(&hba->hba_mutex);
178 spin_lock_bh(&hba->hba_lock);
179 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
180 tgt = hba->tgt_ofld_list[i];
182 /* Cleanup IOs belonging to requested vport */
183 if (tgt->port == port) {
184 spin_unlock_bh(&hba->hba_lock);
185 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
186 bnx2fc_flush_active_ios(tgt);
187 spin_lock_bh(&hba->hba_lock);
191 spin_unlock_bh(&hba->hba_lock);
192 mutex_unlock(&hba->hba_mutex);
195 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
198 struct fc_rport_priv *rdata = tgt->rdata;
199 struct fc_frame_header *fh;
202 fh = fc_frame_header_get(fp);
203 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
204 "r_ctl = 0x%x\n", rdata->ids.port_id,
205 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
206 if ((fh->fh_type == FC_TYPE_ELS) &&
207 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
209 switch (fc_frame_payload_op(fp)) {
211 rc = bnx2fc_send_adisc(tgt, fp);
214 rc = bnx2fc_send_logo(tgt, fp);
217 rc = bnx2fc_send_rls(tgt, fp);
222 } else if ((fh->fh_type == FC_TYPE_BLS) &&
223 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
224 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
226 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
227 "rctl 0x%x thru non-offload path\n",
228 fh->fh_type, fh->fh_r_ctl);
238 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
240 * @lport: the associated local port
241 * @fp: the fc_frame to be transmitted
243 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
246 struct fcoe_crc_eof *cp;
248 struct fc_frame_header *fh;
249 struct bnx2fc_interface *interface;
250 struct fcoe_ctlr *ctlr;
251 struct bnx2fc_hba *hba;
252 struct fcoe_port *port;
254 struct bnx2fc_rport *tgt;
255 struct fc_stats *stats;
258 unsigned int hlen, tlen, elen;
261 port = (struct fcoe_port *)lport_priv(lport);
262 interface = port->priv;
263 ctlr = bnx2fc_to_ctlr(interface);
264 hba = interface->hba;
266 fh = fc_frame_header_get(fp);
269 if (!lport->link_up) {
270 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
275 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
276 if (!ctlr->sel_fcf) {
277 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
281 if (fcoe_ctlr_els_send(ctlr, lport, skb))
289 * Snoop the frame header to check if the frame is for
290 * an offloaded session
293 * tgt_ofld_list access is synchronized using
294 * both hba mutex and hba lock. Atleast hba mutex or
295 * hba lock needs to be held for read access.
298 spin_lock_bh(&hba->hba_lock);
299 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
300 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
301 /* This frame is for offloaded session */
302 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
303 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
304 spin_unlock_bh(&hba->hba_lock);
305 rc = bnx2fc_xmit_l2_frame(tgt, fp);
311 spin_unlock_bh(&hba->hba_lock);
314 elen = sizeof(struct ethhdr);
315 hlen = sizeof(struct fcoe_hdr);
316 tlen = sizeof(struct fcoe_crc_eof);
317 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
319 skb->ip_summed = CHECKSUM_NONE;
320 crc = fcoe_fc_crc(fp);
322 /* copy port crc and eof to the skb buff */
323 if (skb_is_nonlinear(skb)) {
325 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
329 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
330 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
332 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
335 memset(cp, 0, sizeof(*cp));
337 cp->fcoe_crc32 = cpu_to_le32(~crc);
338 if (skb_is_nonlinear(skb)) {
343 /* adjust skb network/transport offsets to match mac/fcoe/port */
344 skb_push(skb, elen + hlen);
345 skb_reset_mac_header(skb);
346 skb_reset_network_header(skb);
348 skb->protocol = htons(ETH_P_FCOE);
349 skb->dev = interface->netdev;
351 /* fill up mac and fcoe headers */
353 eh->h_proto = htons(ETH_P_FCOE);
355 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
357 /* insert GW address */
358 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
360 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
361 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
363 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
365 hp = (struct fcoe_hdr *)(eh + 1);
366 memset(hp, 0, sizeof(*hp));
368 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
371 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
372 if (lport->seq_offload && fr_max_payload(fp)) {
373 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
374 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
376 skb_shinfo(skb)->gso_type = 0;
377 skb_shinfo(skb)->gso_size = 0;
381 stats = per_cpu_ptr(lport->stats, get_cpu());
383 stats->TxWords += wlen;
386 /* send down to lld */
388 if (port->fcoe_pending_queue.qlen)
389 fcoe_check_wait_queue(lport, skb);
390 else if (fcoe_start_io(skb))
391 fcoe_check_wait_queue(lport, skb);
397 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
399 * @skb: the receive socket buffer
400 * @dev: associated net device
402 * @olddev: last device
404 * This function receives the packet and builds FC frame and passes it up
406 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
407 struct packet_type *ptype, struct net_device *olddev)
409 struct fc_lport *lport;
410 struct bnx2fc_interface *interface;
411 struct fcoe_ctlr *ctlr;
412 struct fc_frame_header *fh;
413 struct fcoe_rcv_info *fr;
414 struct fcoe_percpu_s *bg;
417 interface = container_of(ptype, struct bnx2fc_interface,
419 ctlr = bnx2fc_to_ctlr(interface);
422 if (unlikely(lport == NULL)) {
423 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
427 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
428 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
433 * Check for minimum frame length, and make sure required FCoE
434 * and FC headers are pulled into the linear data area.
436 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
437 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
440 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
441 fh = (struct fc_frame_header *) skb_transport_header(skb);
443 oxid = ntohs(fh->fh_ox_id);
445 fr = fcoe_dev_from_skb(skb);
449 spin_lock(&bg->fcoe_rx_list.lock);
451 __skb_queue_tail(&bg->fcoe_rx_list, skb);
452 if (bg->fcoe_rx_list.qlen == 1)
453 wake_up_process(bg->thread);
455 spin_unlock(&bg->fcoe_rx_list.lock);
463 static int bnx2fc_l2_rcv_thread(void *arg)
465 struct fcoe_percpu_s *bg = arg;
468 set_user_nice(current, MIN_NICE);
469 set_current_state(TASK_INTERRUPTIBLE);
470 while (!kthread_should_stop()) {
472 spin_lock_bh(&bg->fcoe_rx_list.lock);
473 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
474 spin_unlock_bh(&bg->fcoe_rx_list.lock);
475 bnx2fc_recv_frame(skb);
476 spin_lock_bh(&bg->fcoe_rx_list.lock);
478 __set_current_state(TASK_INTERRUPTIBLE);
479 spin_unlock_bh(&bg->fcoe_rx_list.lock);
481 __set_current_state(TASK_RUNNING);
486 static void bnx2fc_recv_frame(struct sk_buff *skb)
489 struct fc_lport *lport;
490 struct fcoe_rcv_info *fr;
491 struct fc_stats *stats;
492 struct fc_frame_header *fh;
493 struct fcoe_crc_eof crc_eof;
495 struct fc_lport *vn_port;
496 struct fcoe_port *port;
501 fr = fcoe_dev_from_skb(skb);
503 if (unlikely(lport == NULL)) {
504 printk(KERN_ERR PFX "Invalid lport struct\n");
509 if (skb_is_nonlinear(skb))
511 mac = eth_hdr(skb)->h_source;
512 dest_mac = eth_hdr(skb)->h_dest;
514 /* Pull the header */
515 hp = (struct fcoe_hdr *) skb_network_header(skb);
516 fh = (struct fc_frame_header *) skb_transport_header(skb);
517 skb_pull(skb, sizeof(struct fcoe_hdr));
518 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
520 fp = (struct fc_frame *)skb;
523 fr_sof(fp) = hp->fcoe_sof;
524 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
528 fr_eof(fp) = crc_eof.fcoe_eof;
529 fr_crc(fp) = crc_eof.fcoe_crc32;
530 if (pskb_trim(skb, fr_len)) {
535 fh = fc_frame_header_get(fp);
537 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
539 port = lport_priv(vn_port);
540 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
541 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
546 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
547 fh->fh_type == FC_TYPE_FCP) {
548 /* Drop FCP data. We dont this in L2 path */
552 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
553 fh->fh_type == FC_TYPE_ELS) {
554 switch (fc_frame_payload_op(fp)) {
556 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
557 /* drop non-FIP LOGO */
565 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
566 /* Drop incoming ABTS */
571 stats = per_cpu_ptr(lport->stats, smp_processor_id());
573 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
575 if (le32_to_cpu(fr_crc(fp)) !=
576 ~crc32(~0, skb->data, fr_len)) {
577 if (stats->InvalidCRCCount < 5)
578 printk(KERN_WARNING PFX "dropping frame with "
580 stats->InvalidCRCCount++;
584 fc_exch_recv(lport, fp);
588 * bnx2fc_percpu_io_thread - thread per cpu for ios
590 * @arg: ptr to bnx2fc_percpu_info structure
592 int bnx2fc_percpu_io_thread(void *arg)
594 struct bnx2fc_percpu_s *p = arg;
595 struct bnx2fc_work *work, *tmp;
596 LIST_HEAD(work_list);
598 set_user_nice(current, MIN_NICE);
599 set_current_state(TASK_INTERRUPTIBLE);
600 while (!kthread_should_stop()) {
602 spin_lock_bh(&p->fp_work_lock);
603 while (!list_empty(&p->work_list)) {
604 list_splice_init(&p->work_list, &work_list);
605 spin_unlock_bh(&p->fp_work_lock);
607 list_for_each_entry_safe(work, tmp, &work_list, list) {
608 list_del_init(&work->list);
609 bnx2fc_process_cq_compl(work->tgt, work->wqe);
613 spin_lock_bh(&p->fp_work_lock);
615 __set_current_state(TASK_INTERRUPTIBLE);
616 spin_unlock_bh(&p->fp_work_lock);
618 __set_current_state(TASK_RUNNING);
623 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
625 struct fc_host_statistics *bnx2fc_stats;
626 struct fc_lport *lport = shost_priv(shost);
627 struct fcoe_port *port = lport_priv(lport);
628 struct bnx2fc_interface *interface = port->priv;
629 struct bnx2fc_hba *hba = interface->hba;
630 struct fcoe_statistics_params *fw_stats;
633 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
637 bnx2fc_stats = fc_get_host_stats(shost);
639 init_completion(&hba->stat_req_done);
640 if (bnx2fc_send_stat_req(hba))
642 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
644 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
647 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
648 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
649 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
650 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
651 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
652 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
653 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
654 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
655 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
656 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
658 bnx2fc_stats->dumped_frames = 0;
659 bnx2fc_stats->lip_count = 0;
660 bnx2fc_stats->nos_count = 0;
661 bnx2fc_stats->loss_of_sync_count = 0;
662 bnx2fc_stats->loss_of_signal_count = 0;
663 bnx2fc_stats->prim_seq_protocol_err_count = 0;
665 memcpy(&hba->prev_stats, hba->stats_buffer,
666 sizeof(struct fcoe_statistics_params));
670 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
672 struct fcoe_port *port = lport_priv(lport);
673 struct bnx2fc_interface *interface = port->priv;
674 struct bnx2fc_hba *hba = interface->hba;
675 struct Scsi_Host *shost = lport->host;
678 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
679 shost->max_lun = BNX2FC_MAX_LUN;
680 shost->max_id = BNX2FC_MAX_FCP_TGT;
681 shost->max_channel = 0;
683 shost->transportt = bnx2fc_vport_xport_template;
685 shost->transportt = bnx2fc_transport_template;
687 /* Add the new host to SCSI-ml */
688 rc = scsi_add_host(lport->host, dev);
690 printk(KERN_ERR PFX "Error on scsi_add_host\n");
694 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
695 snprintf(fc_host_symbolic_name(lport->host), 256,
696 "%s (QLogic %s) v%s over %s",
697 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
698 interface->netdev->name);
703 static int bnx2fc_link_ok(struct fc_lport *lport)
705 struct fcoe_port *port = lport_priv(lport);
706 struct bnx2fc_interface *interface = port->priv;
707 struct bnx2fc_hba *hba = interface->hba;
708 struct net_device *dev = hba->phys_dev;
711 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
712 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
714 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
721 * bnx2fc_get_link_state - get network link state
723 * @hba: adapter instance pointer
725 * updates adapter structure flag based on netdev state
727 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
729 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
730 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
732 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
735 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
737 struct bnx2fc_hba *hba;
738 struct bnx2fc_interface *interface;
739 struct fcoe_ctlr *ctlr;
740 struct fcoe_port *port;
743 port = lport_priv(lport);
744 interface = port->priv;
745 ctlr = bnx2fc_to_ctlr(interface);
746 hba = interface->hba;
748 /* require support for get_pauseparam ethtool op. */
749 if (!hba->phys_dev->ethtool_ops ||
750 !hba->phys_dev->ethtool_ops->get_pauseparam)
753 if (fc_set_mfs(lport, BNX2FC_MFS))
756 skb_queue_head_init(&port->fcoe_pending_queue);
757 port->fcoe_pending_queue_active = 0;
758 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
760 fcoe_link_speed_update(lport);
763 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
764 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
766 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
767 fc_set_wwnn(lport, wwnn);
769 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
770 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
773 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
774 fc_set_wwpn(lport, wwpn);
780 static void bnx2fc_destroy_timer(unsigned long data)
782 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
784 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
785 "Destroy compl not received!!\n");
786 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
787 wake_up_interruptible(&hba->destroy_wait);
791 * bnx2fc_indicate_netevent - Generic netdev event handler
793 * @context: adapter structure pointer
795 * @vlan_id: vlan id - associated vlan id with this event
797 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
798 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
800 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
803 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
804 struct fcoe_ctlr_device *cdev;
805 struct fc_lport *lport;
806 struct fc_lport *vport;
807 struct bnx2fc_interface *interface, *tmp;
808 struct fcoe_ctlr *ctlr;
809 int wait_for_upload = 0;
810 u32 link_possible = 1;
812 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
817 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
818 printk(KERN_ERR "indicate_netevent: "\
819 "hba is not UP!!\n");
823 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
824 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
828 case NETDEV_GOING_DOWN:
829 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
836 case NETDEV_UNREGISTER:
839 mutex_lock(&bnx2fc_dev_lock);
840 list_for_each_entry_safe(interface, tmp, &if_list, list) {
841 if (interface->hba == hba &&
842 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
843 __bnx2fc_destroy(interface);
845 mutex_unlock(&bnx2fc_dev_lock);
847 /* Ensure ALL destroy work has been completed before return */
848 flush_workqueue(bnx2fc_wq);
852 printk(KERN_ERR PFX "Unknown netevent %ld", event);
856 mutex_lock(&bnx2fc_dev_lock);
857 list_for_each_entry(interface, &if_list, list) {
859 if (interface->hba != hba)
862 ctlr = bnx2fc_to_ctlr(interface);
864 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
865 interface->netdev->name, event);
867 fcoe_link_speed_update(lport);
869 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
871 if (link_possible && !bnx2fc_link_ok(lport)) {
872 switch (cdev->enabled) {
873 case FCOE_CTLR_DISABLED:
874 pr_info("Link up while interface is disabled.\n");
876 case FCOE_CTLR_ENABLED:
877 case FCOE_CTLR_UNUSED:
878 /* Reset max recv frame size to default */
879 fc_set_mfs(lport, BNX2FC_MFS);
881 * ctlr link up will only be handled during
882 * enable to avoid sending discovery
883 * solicitation on a stale vlan
885 if (interface->enabled)
886 fcoe_ctlr_link_up(ctlr);
888 } else if (fcoe_ctlr_link_down(ctlr)) {
889 switch (cdev->enabled) {
890 case FCOE_CTLR_DISABLED:
891 pr_info("Link down while interface is disabled.\n");
893 case FCOE_CTLR_ENABLED:
894 case FCOE_CTLR_UNUSED:
895 mutex_lock(&lport->lp_mutex);
896 list_for_each_entry(vport, &lport->vports, list)
897 fc_host_port_type(vport->host) =
899 mutex_unlock(&lport->lp_mutex);
900 fc_host_port_type(lport->host) =
902 per_cpu_ptr(lport->stats,
903 get_cpu())->LinkFailureCount++;
905 fcoe_clean_pending_queue(lport);
910 mutex_unlock(&bnx2fc_dev_lock);
912 if (wait_for_upload) {
913 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
914 init_waitqueue_head(&hba->shutdown_wait);
915 BNX2FC_MISC_DBG("indicate_netevent "
916 "num_ofld_sess = %d\n",
918 hba->wait_for_link_down = 1;
919 wait_event_interruptible(hba->shutdown_wait,
920 (hba->num_ofld_sess == 0));
921 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
923 hba->wait_for_link_down = 0;
925 if (signal_pending(current))
926 flush_signals(current);
930 static int bnx2fc_libfc_config(struct fc_lport *lport)
933 /* Set the function pointers set by bnx2fc driver */
934 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
935 sizeof(struct libfc_function_template));
936 fc_elsct_init(lport);
938 fc_rport_init(lport);
940 fc_disc_config(lport, lport);
944 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
946 int fcoe_min_xid, fcoe_max_xid;
948 fcoe_min_xid = hba->max_xid + 1;
950 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
952 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
953 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
954 fcoe_max_xid, NULL)) {
955 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
962 static int bnx2fc_lport_config(struct fc_lport *lport)
966 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
967 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
968 lport->e_d_tov = 2 * 1000;
969 lport->r_a_tov = 10 * 1000;
971 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
972 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
973 lport->does_npiv = 1;
975 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
976 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
978 /* alloc stats structure */
979 if (fc_lport_init_stats(lport))
982 /* Finish fc_lport configuration */
983 fc_lport_config(lport);
989 * bnx2fc_fip_recv - handle a received FIP frame.
991 * @skb: the received skb
992 * @dev: associated &net_device
993 * @ptype: the &packet_type structure which was used to register this handler.
994 * @orig_dev: original receive &net_device, in case @ dev is a bond.
996 * Returns: 0 for success
998 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
999 struct packet_type *ptype,
1000 struct net_device *orig_dev)
1002 struct bnx2fc_interface *interface;
1003 struct fcoe_ctlr *ctlr;
1004 interface = container_of(ptype, struct bnx2fc_interface,
1006 ctlr = bnx2fc_to_ctlr(interface);
1007 fcoe_ctlr_recv(ctlr, skb);
1012 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1014 * @fip: FCoE controller.
1015 * @old: Unicast MAC address to delete if the MAC is non-zero.
1016 * @new: Unicast MAC address to add.
1018 * Remove any previously-set unicast MAC filter.
1019 * Add secondary FCoE MAC address filter for our OUI.
1021 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1023 struct fcoe_port *port = lport_priv(lport);
1025 memcpy(port->data_src_addr, addr, ETH_ALEN);
1029 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1031 * @lport: libfc port
1033 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1035 struct fcoe_port *port;
1037 port = (struct fcoe_port *)lport_priv(lport);
1038 return port->data_src_addr;
1042 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1044 * @fip: FCoE controller.
1047 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1049 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1050 dev_queue_xmit(skb);
1053 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1055 struct Scsi_Host *shost = vport_to_shost(vport);
1056 struct fc_lport *n_port = shost_priv(shost);
1057 struct fcoe_port *port = lport_priv(n_port);
1058 struct bnx2fc_interface *interface = port->priv;
1059 struct net_device *netdev = interface->netdev;
1060 struct fc_lport *vn_port;
1064 rc = fcoe_validate_vport_create(vport);
1066 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1067 printk(KERN_ERR PFX "Failed to create vport, "
1068 "WWPN (0x%s) already exists\n",
1073 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1074 printk(KERN_ERR PFX "vn ports cannot be created on"
1075 "this interface\n");
1079 mutex_lock(&bnx2fc_dev_lock);
1080 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1081 mutex_unlock(&bnx2fc_dev_lock);
1084 if (IS_ERR(vn_port)) {
1085 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1091 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1093 vn_port->boot_time = jiffies;
1094 fc_lport_init(vn_port);
1095 fc_fabric_login(vn_port);
1096 fc_vport_setlink(vn_port);
1101 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1103 struct bnx2fc_lport *blport, *tmp;
1105 spin_lock_bh(&hba->hba_lock);
1106 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1107 if (blport->lport == lport) {
1108 list_del(&blport->list);
1112 spin_unlock_bh(&hba->hba_lock);
1115 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1117 struct Scsi_Host *shost = vport_to_shost(vport);
1118 struct fc_lport *n_port = shost_priv(shost);
1119 struct fc_lport *vn_port = vport->dd_data;
1120 struct fcoe_port *port = lport_priv(vn_port);
1121 struct bnx2fc_interface *interface = port->priv;
1122 struct fc_lport *v_port;
1125 mutex_lock(&n_port->lp_mutex);
1126 list_for_each_entry(v_port, &n_port->vports, list)
1127 if (v_port->vport == vport) {
1133 mutex_unlock(&n_port->lp_mutex);
1136 list_del(&vn_port->list);
1137 mutex_unlock(&n_port->lp_mutex);
1138 bnx2fc_free_vport(interface->hba, port->lport);
1139 bnx2fc_port_shutdown(port->lport);
1140 bnx2fc_interface_put(interface);
1141 queue_work(bnx2fc_wq, &port->destroy_work);
1145 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1147 struct fc_lport *lport = vport->dd_data;
1150 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1151 fc_fabric_logoff(lport);
1153 lport->boot_time = jiffies;
1154 fc_fabric_login(lport);
1155 fc_vport_setlink(lport);
1161 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1163 struct net_device *netdev = interface->netdev;
1164 struct net_device *physdev = interface->hba->phys_dev;
1165 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1166 struct netdev_hw_addr *ha;
1167 int sel_san_mac = 0;
1169 /* setup Source MAC Address */
1171 for_each_dev_addr(physdev, ha) {
1172 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1174 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1175 ha->addr[1], ha->addr[2], ha->addr[3],
1176 ha->addr[4], ha->addr[5]);
1178 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1179 (is_valid_ether_addr(ha->addr))) {
1180 memcpy(ctlr->ctl_src_addr, ha->addr,
1183 BNX2FC_MISC_DBG("Found SAN MAC\n");
1191 interface->fip_packet_type.func = bnx2fc_fip_recv;
1192 interface->fip_packet_type.type = htons(ETH_P_FIP);
1193 interface->fip_packet_type.dev = netdev;
1194 dev_add_pack(&interface->fip_packet_type);
1196 interface->fcoe_packet_type.func = bnx2fc_rcv;
1197 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1198 interface->fcoe_packet_type.dev = netdev;
1199 dev_add_pack(&interface->fcoe_packet_type);
1204 static int bnx2fc_attach_transport(void)
1206 bnx2fc_transport_template =
1207 fc_attach_transport(&bnx2fc_transport_function);
1209 if (bnx2fc_transport_template == NULL) {
1210 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1214 bnx2fc_vport_xport_template =
1215 fc_attach_transport(&bnx2fc_vport_xport_function);
1216 if (bnx2fc_vport_xport_template == NULL) {
1218 "Failed to attach FC transport for vport\n");
1219 fc_release_transport(bnx2fc_transport_template);
1220 bnx2fc_transport_template = NULL;
1225 static void bnx2fc_release_transport(void)
1227 fc_release_transport(bnx2fc_transport_template);
1228 fc_release_transport(bnx2fc_vport_xport_template);
1229 bnx2fc_transport_template = NULL;
1230 bnx2fc_vport_xport_template = NULL;
1233 static void bnx2fc_interface_release(struct kref *kref)
1235 struct fcoe_ctlr_device *ctlr_dev;
1236 struct bnx2fc_interface *interface;
1237 struct fcoe_ctlr *ctlr;
1238 struct net_device *netdev;
1240 interface = container_of(kref, struct bnx2fc_interface, kref);
1241 BNX2FC_MISC_DBG("Interface is being released\n");
1243 ctlr = bnx2fc_to_ctlr(interface);
1244 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1245 netdev = interface->netdev;
1247 /* tear-down FIP controller */
1248 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1249 fcoe_ctlr_destroy(ctlr);
1251 fcoe_ctlr_device_delete(ctlr_dev);
1254 module_put(THIS_MODULE);
1257 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1259 kref_get(&interface->kref);
1262 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1264 kref_put(&interface->kref, bnx2fc_interface_release);
1266 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1268 /* Free the command manager */
1270 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1271 hba->cmd_mgr = NULL;
1273 kfree(hba->tgt_ofld_list);
1274 bnx2fc_unbind_pcidev(hba);
1279 * bnx2fc_hba_create - create a new bnx2fc hba
1281 * @cnic: pointer to cnic device
1283 * Creates a new FCoE hba on the given device.
1286 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1288 struct bnx2fc_hba *hba;
1289 struct fcoe_capabilities *fcoe_cap;
1292 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1294 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1297 spin_lock_init(&hba->hba_lock);
1298 mutex_init(&hba->hba_mutex);
1302 hba->max_tasks = cnic->max_fcoe_exchanges;
1303 hba->elstm_xids = (hba->max_tasks / 2);
1304 hba->max_outstanding_cmds = hba->elstm_xids;
1305 hba->max_xid = (hba->max_tasks - 1);
1307 rc = bnx2fc_bind_pcidev(hba);
1309 printk(KERN_ERR PFX "create_adapter: bind error\n");
1312 hba->phys_dev = cnic->netdev;
1313 hba->next_conn_id = 0;
1315 hba->tgt_ofld_list =
1316 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1318 if (!hba->tgt_ofld_list) {
1319 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1323 hba->num_ofld_sess = 0;
1325 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1326 if (!hba->cmd_mgr) {
1327 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1330 fcoe_cap = &hba->fcoe_cap;
1332 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1333 FCOE_IOS_PER_CONNECTION_SHIFT;
1334 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1335 FCOE_LOGINS_PER_PORT_SHIFT;
1336 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1337 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1338 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1339 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1340 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1341 FCOE_TARGETS_SUPPORTED_SHIFT;
1342 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1343 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1344 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1346 init_waitqueue_head(&hba->shutdown_wait);
1347 init_waitqueue_head(&hba->destroy_wait);
1348 INIT_LIST_HEAD(&hba->vports);
1353 kfree(hba->tgt_ofld_list);
1355 bnx2fc_unbind_pcidev(hba);
1361 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1362 struct net_device *netdev,
1363 enum fip_state fip_mode)
1365 struct fcoe_ctlr_device *ctlr_dev;
1366 struct bnx2fc_interface *interface;
1367 struct fcoe_ctlr *ctlr;
1371 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1372 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1375 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1378 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1379 ctlr->cdev = ctlr_dev;
1380 interface = fcoe_ctlr_priv(ctlr);
1382 kref_init(&interface->kref);
1383 interface->hba = hba;
1384 interface->netdev = netdev;
1386 /* Initialize FIP */
1387 fcoe_ctlr_init(ctlr, fip_mode);
1388 ctlr->send = bnx2fc_fip_send;
1389 ctlr->update_mac = bnx2fc_update_src_mac;
1390 ctlr->get_src_addr = bnx2fc_get_src_mac;
1391 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1393 rc = bnx2fc_interface_setup(interface);
1397 fcoe_ctlr_destroy(ctlr);
1399 fcoe_ctlr_device_delete(ctlr_dev);
1404 * bnx2fc_if_create - Create FCoE instance on a given interface
1406 * @interface: FCoE interface to create a local port on
1407 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1408 * @npiv: Indicates if the port is vport or not
1410 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1412 * Returns: Allocated fc_lport or an error pointer
1414 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1415 struct device *parent, int npiv)
1417 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1418 struct fc_lport *lport, *n_port;
1419 struct fcoe_port *port;
1420 struct Scsi_Host *shost;
1421 struct fc_vport *vport = dev_to_vport(parent);
1422 struct bnx2fc_lport *blport;
1423 struct bnx2fc_hba *hba = interface->hba;
1426 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1428 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1432 /* Allocate Scsi_Host structure */
1433 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1435 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1437 lport = libfc_vport_create(vport, sizeof(*port));
1440 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1443 shost = lport->host;
1444 port = lport_priv(lport);
1445 port->lport = lport;
1446 port->priv = interface;
1447 port->get_netdev = bnx2fc_netdev;
1448 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1450 /* Configure fcoe_port */
1451 rc = bnx2fc_lport_config(lport);
1456 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1457 vport->node_name, vport->port_name);
1458 fc_set_wwnn(lport, vport->node_name);
1459 fc_set_wwpn(lport, vport->port_name);
1461 /* Configure netdev and networking properties of the lport */
1462 rc = bnx2fc_net_config(lport, interface->netdev);
1464 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1468 rc = bnx2fc_shost_config(lport, parent);
1470 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1471 interface->netdev->name);
1475 /* Initialize the libfc library */
1476 rc = bnx2fc_libfc_config(lport);
1478 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1481 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1483 /* Allocate exchange manager */
1485 rc = bnx2fc_em_config(lport, hba);
1487 shost = vport_to_shost(vport);
1488 n_port = shost_priv(shost);
1489 rc = fc_exch_mgr_list_clone(n_port, lport);
1493 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1497 bnx2fc_interface_get(interface);
1499 spin_lock_bh(&hba->hba_lock);
1500 blport->lport = lport;
1501 list_add_tail(&blport->list, &hba->vports);
1502 spin_unlock_bh(&hba->hba_lock);
1507 scsi_remove_host(shost);
1509 scsi_host_put(lport->host);
1515 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1517 /* Dont listen for Ethernet packets anymore */
1518 __dev_remove_pack(&interface->fcoe_packet_type);
1519 __dev_remove_pack(&interface->fip_packet_type);
1523 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1525 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1526 struct fc_lport *lport = ctlr->lp;
1527 struct fcoe_port *port = lport_priv(lport);
1528 struct bnx2fc_hba *hba = interface->hba;
1530 /* Stop the transmit retry timer */
1531 del_timer_sync(&port->timer);
1533 /* Free existing transmit skbs */
1534 fcoe_clean_pending_queue(lport);
1536 bnx2fc_net_cleanup(interface);
1538 bnx2fc_free_vport(hba, lport);
1541 static void bnx2fc_if_destroy(struct fc_lport *lport)
1544 /* Free queued packets for the receive thread */
1545 bnx2fc_clean_rx_queue(lport);
1547 /* Detach from scsi-ml */
1548 fc_remove_host(lport->host);
1549 scsi_remove_host(lport->host);
1552 * Note that only the physical lport will have the exchange manager.
1553 * for vports, this function is NOP
1555 fc_exch_mgr_free(lport);
1557 /* Free memory used by statistical counters */
1558 fc_lport_free_stats(lport);
1560 /* Release Scsi_Host */
1561 scsi_host_put(lport->host);
1564 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1566 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1567 struct fc_lport *lport = ctlr->lp;
1568 struct fcoe_port *port = lport_priv(lport);
1570 bnx2fc_interface_cleanup(interface);
1571 bnx2fc_stop(interface);
1572 list_del(&interface->list);
1573 bnx2fc_interface_put(interface);
1574 queue_work(bnx2fc_wq, &port->destroy_work);
1578 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1580 * @buffer: The name of the Ethernet interface to be destroyed
1581 * @kp: The associated kernel parameter
1583 * Called from sysfs.
1585 * Returns: 0 for success
1587 static int bnx2fc_destroy(struct net_device *netdev)
1589 struct bnx2fc_interface *interface = NULL;
1590 struct workqueue_struct *timer_work_queue;
1591 struct fcoe_ctlr *ctlr;
1595 mutex_lock(&bnx2fc_dev_lock);
1597 interface = bnx2fc_interface_lookup(netdev);
1598 ctlr = bnx2fc_to_ctlr(interface);
1599 if (!interface || !ctlr->lp) {
1601 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1605 timer_work_queue = interface->timer_work_queue;
1606 __bnx2fc_destroy(interface);
1607 destroy_workqueue(timer_work_queue);
1610 mutex_unlock(&bnx2fc_dev_lock);
1615 static void bnx2fc_destroy_work(struct work_struct *work)
1617 struct fcoe_port *port;
1618 struct fc_lport *lport;
1620 port = container_of(work, struct fcoe_port, destroy_work);
1621 lport = port->lport;
1623 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1625 bnx2fc_if_destroy(lport);
1628 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1630 bnx2fc_free_fw_resc(hba);
1631 bnx2fc_free_task_ctx(hba);
1635 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1638 * @hba: Adapter instance
1640 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1642 if (bnx2fc_setup_task_ctx(hba))
1645 if (bnx2fc_setup_fw_resc(hba))
1650 bnx2fc_unbind_adapter_devices(hba);
1654 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1656 struct cnic_dev *cnic;
1657 struct pci_dev *pdev;
1660 printk(KERN_ERR PFX "cnic is NULL\n");
1664 pdev = hba->pcidev = cnic->pcidev;
1668 switch (pdev->device) {
1669 case PCI_DEVICE_ID_NX2_57710:
1670 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1672 case PCI_DEVICE_ID_NX2_57711:
1673 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1675 case PCI_DEVICE_ID_NX2_57712:
1676 case PCI_DEVICE_ID_NX2_57712_MF:
1677 case PCI_DEVICE_ID_NX2_57712_VF:
1678 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1680 case PCI_DEVICE_ID_NX2_57800:
1681 case PCI_DEVICE_ID_NX2_57800_MF:
1682 case PCI_DEVICE_ID_NX2_57800_VF:
1683 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1685 case PCI_DEVICE_ID_NX2_57810:
1686 case PCI_DEVICE_ID_NX2_57810_MF:
1687 case PCI_DEVICE_ID_NX2_57810_VF:
1688 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1690 case PCI_DEVICE_ID_NX2_57840:
1691 case PCI_DEVICE_ID_NX2_57840_MF:
1692 case PCI_DEVICE_ID_NX2_57840_VF:
1693 case PCI_DEVICE_ID_NX2_57840_2_20:
1694 case PCI_DEVICE_ID_NX2_57840_4_10:
1695 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1698 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1701 pci_dev_get(hba->pcidev);
1705 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1708 hba->chip_num[0] = '\0';
1709 pci_dev_put(hba->pcidev);
1715 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1717 * @handle: transport handle pointing to adapter struture
1719 static int bnx2fc_ulp_get_stats(void *handle)
1721 struct bnx2fc_hba *hba = handle;
1722 struct cnic_dev *cnic;
1723 struct fcoe_stats_info *stats_addr;
1729 stats_addr = &cnic->stats_addr->fcoe_stat;
1733 strncpy(stats_addr->version, BNX2FC_VERSION,
1734 sizeof(stats_addr->version));
1735 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1736 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1743 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1745 * @handle: transport handle pointing to adapter structure
1747 * This function maps adapter structure to pcidev structure and initiates
1748 * firmware handshake to enable/initialize on-chip FCoE components.
1749 * This bnx2fc - cnic interface api callback is used after following
1750 * conditions are met -
1751 * a) underlying network interface is up (marked by event NETDEV_UP
1753 * b) bnx2fc adatper structure is registered.
1755 static void bnx2fc_ulp_start(void *handle)
1757 struct bnx2fc_hba *hba = handle;
1758 struct bnx2fc_interface *interface;
1759 struct fcoe_ctlr *ctlr;
1760 struct fc_lport *lport;
1762 mutex_lock(&bnx2fc_dev_lock);
1764 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1765 bnx2fc_fw_init(hba);
1767 BNX2FC_MISC_DBG("bnx2fc started.\n");
1769 list_for_each_entry(interface, &if_list, list) {
1770 if (interface->hba == hba) {
1771 ctlr = bnx2fc_to_ctlr(interface);
1773 /* Kick off Fabric discovery*/
1774 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1775 lport->tt.frame_send = bnx2fc_xmit;
1776 bnx2fc_start_disc(interface);
1780 mutex_unlock(&bnx2fc_dev_lock);
1783 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1785 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1786 fc_fabric_logoff(lport);
1787 fc_lport_destroy(lport);
1790 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1792 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1793 struct fc_lport *lport;
1794 struct fc_lport *vport;
1796 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1800 bnx2fc_port_shutdown(lport);
1802 mutex_lock(&lport->lp_mutex);
1803 list_for_each_entry(vport, &lport->vports, list)
1804 fc_host_port_type(vport->host) =
1805 FC_PORTTYPE_UNKNOWN;
1806 mutex_unlock(&lport->lp_mutex);
1807 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1808 fcoe_ctlr_link_down(ctlr);
1809 fcoe_clean_pending_queue(lport);
1812 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1814 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1818 rc = bnx2fc_bind_adapter_devices(hba);
1820 printk(KERN_ALERT PFX
1821 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1825 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1827 printk(KERN_ALERT PFX
1828 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1833 * Wait until the adapter init message is complete, and adapter
1836 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1837 msleep(BNX2FC_INIT_POLL_TIME);
1839 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1840 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1842 hba->cnic->netdev->name);
1848 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1852 bnx2fc_unbind_adapter_devices(hba);
1857 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1859 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1860 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1861 init_timer(&hba->destroy_timer);
1862 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1864 hba->destroy_timer.function = bnx2fc_destroy_timer;
1865 hba->destroy_timer.data = (unsigned long)hba;
1866 add_timer(&hba->destroy_timer);
1867 wait_event_interruptible(hba->destroy_wait,
1868 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1870 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1871 /* This should never happen */
1872 if (signal_pending(current))
1873 flush_signals(current);
1875 del_timer_sync(&hba->destroy_timer);
1877 bnx2fc_unbind_adapter_devices(hba);
1882 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1884 * @handle: transport handle pointing to adapter structure
1886 * Driver checks if adapter is already in shutdown mode, if not start
1887 * the shutdown process.
1889 static void bnx2fc_ulp_stop(void *handle)
1891 struct bnx2fc_hba *hba = handle;
1892 struct bnx2fc_interface *interface;
1894 printk(KERN_ERR "ULP_STOP\n");
1896 mutex_lock(&bnx2fc_dev_lock);
1897 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1899 list_for_each_entry(interface, &if_list, list) {
1900 if (interface->hba == hba)
1901 bnx2fc_stop(interface);
1903 BUG_ON(hba->num_ofld_sess != 0);
1905 mutex_lock(&hba->hba_mutex);
1906 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1907 clear_bit(ADAPTER_STATE_GOING_DOWN,
1908 &hba->adapter_state);
1910 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1911 mutex_unlock(&hba->hba_mutex);
1913 bnx2fc_fw_destroy(hba);
1915 mutex_unlock(&bnx2fc_dev_lock);
1918 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1920 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1921 struct fc_lport *lport;
1924 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1925 /* Kick off FIP/FLOGI */
1926 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1927 printk(KERN_ERR PFX "Init not done yet\n");
1932 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1934 if (!bnx2fc_link_ok(lport) && interface->enabled) {
1935 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1936 fcoe_ctlr_link_up(ctlr);
1937 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1938 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1941 /* wait for the FCF to be selected before issuing FLOGI */
1942 while (!ctlr->sel_fcf) {
1944 /* give up after 3 secs */
1945 if (++wait_cnt > 12)
1949 /* Reset max receive frame size to default */
1950 if (fc_set_mfs(lport, BNX2FC_MFS))
1953 fc_lport_init(lport);
1954 fc_fabric_login(lport);
1959 * bnx2fc_ulp_init - Initialize an adapter instance
1961 * @dev : cnic device handle
1962 * Called from cnic_register_driver() context to initialize all
1963 * enumerated cnic devices. This routine allocates adapter structure
1964 * and other device specific resources.
1966 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1968 struct bnx2fc_hba *hba;
1971 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1972 /* bnx2fc works only when bnx2x is loaded */
1973 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1974 (dev->max_fcoe_conn == 0)) {
1975 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1976 " flags: %lx fcoe_conn: %d\n",
1977 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1981 hba = bnx2fc_hba_create(dev);
1983 printk(KERN_ERR PFX "hba initialization failed\n");
1987 /* Add HBA to the adapter list */
1988 mutex_lock(&bnx2fc_dev_lock);
1989 list_add_tail(&hba->list, &adapter_list);
1991 mutex_unlock(&bnx2fc_dev_lock);
1993 dev->fcoe_cap = &hba->fcoe_cap;
1994 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1995 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1998 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2000 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2003 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2004 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2006 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2008 if (interface->enabled == true) {
2010 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2013 interface->enabled = false;
2014 fcoe_ctlr_link_down(ctlr);
2015 fcoe_clean_pending_queue(ctlr->lp);
2022 * Deperecated: Use bnx2fc_enabled()
2024 static int bnx2fc_disable(struct net_device *netdev)
2026 struct bnx2fc_interface *interface;
2027 struct fcoe_ctlr *ctlr;
2031 mutex_lock(&bnx2fc_dev_lock);
2033 interface = bnx2fc_interface_lookup(netdev);
2034 ctlr = bnx2fc_to_ctlr(interface);
2038 pr_err(PFX "bnx2fc_disable: interface not found\n");
2040 rc = __bnx2fc_disable(ctlr);
2042 mutex_unlock(&bnx2fc_dev_lock);
2047 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2049 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2051 if (interface->enabled == false) {
2053 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2055 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2056 fcoe_ctlr_link_up(ctlr);
2057 interface->enabled = true;
2064 * Deprecated: Use bnx2fc_enabled()
2066 static int bnx2fc_enable(struct net_device *netdev)
2068 struct bnx2fc_interface *interface;
2069 struct fcoe_ctlr *ctlr;
2073 mutex_lock(&bnx2fc_dev_lock);
2075 interface = bnx2fc_interface_lookup(netdev);
2076 ctlr = bnx2fc_to_ctlr(interface);
2079 pr_err(PFX "bnx2fc_enable: interface not found\n");
2081 rc = __bnx2fc_enable(ctlr);
2084 mutex_unlock(&bnx2fc_dev_lock);
2090 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2091 * @cdev: The FCoE Controller that is being enabled or disabled
2093 * fcoe_sysfs will ensure that the state of 'enabled' has
2094 * changed, so no checking is necessary here. This routine simply
2095 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2096 * When those routines are removed the functionality can be merged
2099 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2101 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2103 switch (cdev->enabled) {
2104 case FCOE_CTLR_ENABLED:
2105 return __bnx2fc_enable(ctlr);
2106 case FCOE_CTLR_DISABLED:
2107 return __bnx2fc_disable(ctlr);
2108 case FCOE_CTLR_UNUSED:
2114 enum bnx2fc_create_link_state {
2115 BNX2FC_CREATE_LINK_DOWN,
2116 BNX2FC_CREATE_LINK_UP,
2120 * _bnx2fc_create() - Create bnx2fc FCoE interface
2121 * @netdev : The net_device object the Ethernet interface to create on
2122 * @fip_mode: The FIP mode for this creation
2123 * @link_state: The ctlr link state on creation
2125 * Called from either the libfcoe 'create' module parameter
2126 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2128 * libfcoe's 'create' module parameter is deprecated so some
2129 * consolidation of code can be done when that interface is
2132 * Returns: 0 for success
2134 static int _bnx2fc_create(struct net_device *netdev,
2135 enum fip_state fip_mode,
2136 enum bnx2fc_create_link_state link_state)
2138 struct fcoe_ctlr_device *cdev;
2139 struct fcoe_ctlr *ctlr;
2140 struct bnx2fc_interface *interface;
2141 struct bnx2fc_hba *hba;
2142 struct net_device *phys_dev = netdev;
2143 struct fc_lport *lport;
2144 struct ethtool_drvinfo drvinfo;
2148 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2149 if (fip_mode != FIP_MODE_FABRIC) {
2150 printk(KERN_ERR "fip mode not FABRIC\n");
2156 mutex_lock(&bnx2fc_dev_lock);
2158 if (!try_module_get(THIS_MODULE)) {
2163 /* obtain physical netdev */
2164 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2165 phys_dev = vlan_dev_real_dev(netdev);
2167 /* verify if the physical device is a netxtreme2 device */
2168 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2169 memset(&drvinfo, 0, sizeof(drvinfo));
2170 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2171 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2172 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2177 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2182 /* obtain interface and initialize rest of the structure */
2183 hba = bnx2fc_hba_lookup(phys_dev);
2186 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2190 if (bnx2fc_interface_lookup(netdev)) {
2195 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2197 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2201 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2202 vlan_id = vlan_dev_vlan_id(netdev);
2203 interface->vlan_enabled = 1;
2206 ctlr = bnx2fc_to_ctlr(interface);
2207 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2208 interface->vlan_id = vlan_id;
2210 interface->timer_work_queue =
2211 create_singlethread_workqueue("bnx2fc_timer_wq");
2212 if (!interface->timer_work_queue) {
2213 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2218 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2220 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2226 /* Add interface to if_list */
2227 list_add_tail(&interface->list, &if_list);
2229 lport->boot_time = jiffies;
2231 /* Make this master N_port */
2234 if (link_state == BNX2FC_CREATE_LINK_UP)
2235 cdev->enabled = FCOE_CTLR_ENABLED;
2237 cdev->enabled = FCOE_CTLR_DISABLED;
2239 if (link_state == BNX2FC_CREATE_LINK_UP &&
2240 !bnx2fc_link_ok(lport)) {
2241 fcoe_ctlr_link_up(ctlr);
2242 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2243 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2246 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2247 bnx2fc_start_disc(interface);
2249 if (link_state == BNX2FC_CREATE_LINK_UP)
2250 interface->enabled = true;
2253 * Release from kref_init in bnx2fc_interface_setup, on success
2254 * lport should be holding a reference taken in bnx2fc_if_create
2256 bnx2fc_interface_put(interface);
2257 /* put netdev that was held while calling dev_get_by_name */
2258 mutex_unlock(&bnx2fc_dev_lock);
2263 destroy_workqueue(interface->timer_work_queue);
2265 bnx2fc_net_cleanup(interface);
2266 bnx2fc_interface_put(interface);
2269 module_put(THIS_MODULE);
2271 mutex_unlock(&bnx2fc_dev_lock);
2277 * bnx2fc_create() - Create a bnx2fc interface
2278 * @netdev : The net_device object the Ethernet interface to create on
2279 * @fip_mode: The FIP mode for this creation
2281 * Called from fcoe transport
2283 * Returns: 0 for success
2285 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2287 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2291 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2292 * @netdev: The net_device to be used by the allocated FCoE Controller
2294 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2295 * in a link_down state. The allows the user an opportunity to configure
2296 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2298 * Creating in with this routine starts the FCoE Controller in Fabric
2299 * mode. The user can change to VN2VN or another mode before enabling.
2301 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2303 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2304 BNX2FC_CREATE_LINK_DOWN);
2308 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2310 * @cnic: Pointer to cnic device instance
2313 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2315 struct bnx2fc_hba *hba;
2317 /* Called with bnx2fc_dev_lock held */
2318 list_for_each_entry(hba, &adapter_list, list) {
2319 if (hba->cnic == cnic)
2325 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2328 struct bnx2fc_interface *interface;
2330 /* Called with bnx2fc_dev_lock held */
2331 list_for_each_entry(interface, &if_list, list) {
2332 if (interface->netdev == netdev)
2338 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2341 struct bnx2fc_hba *hba;
2343 /* Called with bnx2fc_dev_lock held */
2344 list_for_each_entry(hba, &adapter_list, list) {
2345 if (hba->phys_dev == phys_dev)
2348 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2353 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2355 * @dev cnic device handle
2357 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2359 struct bnx2fc_hba *hba;
2360 struct bnx2fc_interface *interface, *tmp;
2362 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2364 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2365 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2366 dev->netdev->name, dev->flags);
2370 mutex_lock(&bnx2fc_dev_lock);
2371 hba = bnx2fc_find_hba_for_cnic(dev);
2373 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2375 mutex_unlock(&bnx2fc_dev_lock);
2379 list_del_init(&hba->list);
2382 list_for_each_entry_safe(interface, tmp, &if_list, list)
2383 /* destroy not called yet, move to quiesced list */
2384 if (interface->hba == hba)
2385 __bnx2fc_destroy(interface);
2386 mutex_unlock(&bnx2fc_dev_lock);
2388 /* Ensure ALL destroy work has been completed before return */
2389 flush_workqueue(bnx2fc_wq);
2391 bnx2fc_ulp_stop(hba);
2392 /* unregister cnic device */
2393 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2394 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2395 bnx2fc_hba_destroy(hba);
2399 * bnx2fc_fcoe_reset - Resets the fcoe
2401 * @shost: shost the reset is from
2405 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2407 struct fc_lport *lport = shost_priv(shost);
2408 fc_lport_reset(lport);
2413 static bool bnx2fc_match(struct net_device *netdev)
2415 struct net_device *phys_dev = netdev;
2417 mutex_lock(&bnx2fc_dev_lock);
2418 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2419 phys_dev = vlan_dev_real_dev(netdev);
2421 if (bnx2fc_hba_lookup(phys_dev)) {
2422 mutex_unlock(&bnx2fc_dev_lock);
2426 mutex_unlock(&bnx2fc_dev_lock);
2431 static struct fcoe_transport bnx2fc_transport = {
2434 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2435 .alloc = bnx2fc_ctlr_alloc,
2436 .match = bnx2fc_match,
2437 .create = bnx2fc_create,
2438 .destroy = bnx2fc_destroy,
2439 .enable = bnx2fc_enable,
2440 .disable = bnx2fc_disable,
2444 * bnx2fc_percpu_thread_create - Create a receive thread for an
2447 * @cpu: cpu index for the online cpu
2449 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2451 struct bnx2fc_percpu_s *p;
2452 struct task_struct *thread;
2454 p = &per_cpu(bnx2fc_percpu, cpu);
2456 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2457 (void *)p, cpu_to_node(cpu),
2458 "bnx2fc_thread/%d", cpu);
2459 /* bind thread to the cpu */
2460 if (likely(!IS_ERR(thread))) {
2461 kthread_bind(thread, cpu);
2462 p->iothread = thread;
2463 wake_up_process(thread);
2467 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2469 struct bnx2fc_percpu_s *p;
2470 struct task_struct *thread;
2471 struct bnx2fc_work *work, *tmp;
2473 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2475 /* Prevent any new work from being queued for this CPU */
2476 p = &per_cpu(bnx2fc_percpu, cpu);
2477 spin_lock_bh(&p->fp_work_lock);
2478 thread = p->iothread;
2482 /* Free all work in the list */
2483 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2484 list_del_init(&work->list);
2485 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2489 spin_unlock_bh(&p->fp_work_lock);
2492 kthread_stop(thread);
2496 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2498 * @nfb: The callback data block
2499 * @action: The event triggering the callback
2500 * @hcpu: The index of the CPU that the event is for
2502 * This creates or destroys per-CPU data for fcoe
2504 * Returns NOTIFY_OK always.
2506 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2507 unsigned long action, void *hcpu)
2509 unsigned cpu = (unsigned long)hcpu;
2513 case CPU_ONLINE_FROZEN:
2514 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2515 bnx2fc_percpu_thread_create(cpu);
2518 case CPU_DEAD_FROZEN:
2519 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2520 bnx2fc_percpu_thread_destroy(cpu);
2529 * bnx2fc_mod_init - module init entry point
2531 * Initialize driver wide global data structures, and register
2534 static int __init bnx2fc_mod_init(void)
2536 struct fcoe_percpu_s *bg;
2537 struct task_struct *l2_thread;
2539 unsigned int cpu = 0;
2540 struct bnx2fc_percpu_s *p;
2542 printk(KERN_INFO PFX "%s", version);
2544 /* register as a fcoe transport */
2545 rc = fcoe_transport_attach(&bnx2fc_transport);
2547 printk(KERN_ERR "failed to register an fcoe transport, check "
2548 "if libfcoe is loaded\n");
2552 INIT_LIST_HEAD(&adapter_list);
2553 INIT_LIST_HEAD(&if_list);
2554 mutex_init(&bnx2fc_dev_lock);
2557 /* Attach FC transport template */
2558 rc = bnx2fc_attach_transport();
2562 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2568 bg = &bnx2fc_global;
2569 skb_queue_head_init(&bg->fcoe_rx_list);
2570 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2572 "bnx2fc_l2_thread");
2573 if (IS_ERR(l2_thread)) {
2574 rc = PTR_ERR(l2_thread);
2577 wake_up_process(l2_thread);
2578 spin_lock_bh(&bg->fcoe_rx_list.lock);
2579 bg->thread = l2_thread;
2580 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2582 for_each_possible_cpu(cpu) {
2583 p = &per_cpu(bnx2fc_percpu, cpu);
2584 INIT_LIST_HEAD(&p->work_list);
2585 spin_lock_init(&p->fp_work_lock);
2588 cpu_notifier_register_begin();
2590 for_each_online_cpu(cpu) {
2591 bnx2fc_percpu_thread_create(cpu);
2594 /* Initialize per CPU interrupt thread */
2595 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2597 cpu_notifier_register_done();
2599 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2604 destroy_workqueue(bnx2fc_wq);
2606 bnx2fc_release_transport();
2608 fcoe_transport_detach(&bnx2fc_transport);
2613 static void __exit bnx2fc_mod_exit(void)
2615 LIST_HEAD(to_be_deleted);
2616 struct bnx2fc_hba *hba, *next;
2617 struct fcoe_percpu_s *bg;
2618 struct task_struct *l2_thread;
2619 struct sk_buff *skb;
2620 unsigned int cpu = 0;
2623 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2624 * it will have higher precedence than bnx2fc_dev_lock.
2625 * unregister_device() cannot be called with bnx2fc_dev_lock
2628 mutex_lock(&bnx2fc_dev_lock);
2629 list_splice(&adapter_list, &to_be_deleted);
2630 INIT_LIST_HEAD(&adapter_list);
2632 mutex_unlock(&bnx2fc_dev_lock);
2634 /* Unregister with cnic */
2635 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2636 list_del_init(&hba->list);
2637 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2639 bnx2fc_ulp_stop(hba);
2640 /* unregister cnic device */
2641 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2642 &hba->reg_with_cnic))
2643 hba->cnic->unregister_device(hba->cnic,
2645 bnx2fc_hba_destroy(hba);
2647 cnic_unregister_driver(CNIC_ULP_FCOE);
2649 /* Destroy global thread */
2650 bg = &bnx2fc_global;
2651 spin_lock_bh(&bg->fcoe_rx_list.lock);
2652 l2_thread = bg->thread;
2654 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2657 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2660 kthread_stop(l2_thread);
2662 cpu_notifier_register_begin();
2664 /* Destroy per cpu threads */
2665 for_each_online_cpu(cpu) {
2666 bnx2fc_percpu_thread_destroy(cpu);
2669 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2671 cpu_notifier_register_done();
2673 destroy_workqueue(bnx2fc_wq);
2675 * detach from scsi transport
2676 * must happen after all destroys are done
2678 bnx2fc_release_transport();
2680 /* detach from fcoe transport */
2681 fcoe_transport_detach(&bnx2fc_transport);
2684 module_init(bnx2fc_mod_init);
2685 module_exit(bnx2fc_mod_exit);
2687 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2688 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2689 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2690 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2691 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2692 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2693 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2694 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2696 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2697 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2700 static struct fc_function_template bnx2fc_transport_function = {
2701 .show_host_node_name = 1,
2702 .show_host_port_name = 1,
2703 .show_host_supported_classes = 1,
2704 .show_host_supported_fc4s = 1,
2705 .show_host_active_fc4s = 1,
2706 .show_host_maxframe_size = 1,
2708 .show_host_port_id = 1,
2709 .show_host_supported_speeds = 1,
2710 .get_host_speed = fc_get_host_speed,
2711 .show_host_speed = 1,
2712 .show_host_port_type = 1,
2713 .get_host_port_state = fc_get_host_port_state,
2714 .show_host_port_state = 1,
2715 .show_host_symbolic_name = 1,
2717 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2718 sizeof(struct bnx2fc_rport)),
2719 .show_rport_maxframe_size = 1,
2720 .show_rport_supported_classes = 1,
2722 .show_host_fabric_name = 1,
2723 .show_starget_node_name = 1,
2724 .show_starget_port_name = 1,
2725 .show_starget_port_id = 1,
2726 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2727 .show_rport_dev_loss_tmo = 1,
2728 .get_fc_host_stats = bnx2fc_get_host_stats,
2730 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2732 .terminate_rport_io = fc_rport_terminate_io,
2734 .vport_create = bnx2fc_vport_create,
2735 .vport_delete = bnx2fc_vport_destroy,
2736 .vport_disable = bnx2fc_vport_disable,
2737 .bsg_request = fc_lport_bsg_request,
2740 static struct fc_function_template bnx2fc_vport_xport_function = {
2741 .show_host_node_name = 1,
2742 .show_host_port_name = 1,
2743 .show_host_supported_classes = 1,
2744 .show_host_supported_fc4s = 1,
2745 .show_host_active_fc4s = 1,
2746 .show_host_maxframe_size = 1,
2748 .show_host_port_id = 1,
2749 .show_host_supported_speeds = 1,
2750 .get_host_speed = fc_get_host_speed,
2751 .show_host_speed = 1,
2752 .show_host_port_type = 1,
2753 .get_host_port_state = fc_get_host_port_state,
2754 .show_host_port_state = 1,
2755 .show_host_symbolic_name = 1,
2757 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2758 sizeof(struct bnx2fc_rport)),
2759 .show_rport_maxframe_size = 1,
2760 .show_rport_supported_classes = 1,
2762 .show_host_fabric_name = 1,
2763 .show_starget_node_name = 1,
2764 .show_starget_port_name = 1,
2765 .show_starget_port_id = 1,
2766 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2767 .show_rport_dev_loss_tmo = 1,
2768 .get_fc_host_stats = fc_get_host_stats,
2769 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2770 .terminate_rport_io = fc_rport_terminate_io,
2771 .bsg_request = fc_lport_bsg_request,
2775 * scsi_host_template structure used while registering with SCSI-ml
2777 static struct scsi_host_template bnx2fc_shost_template = {
2778 .module = THIS_MODULE,
2779 .name = "QLogic Offload FCoE Initiator",
2780 .queuecommand = bnx2fc_queuecommand,
2781 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2782 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2783 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2784 .eh_host_reset_handler = fc_eh_host_reset,
2785 .slave_alloc = fc_slave_alloc,
2786 .change_queue_depth = fc_change_queue_depth,
2787 .change_queue_type = fc_change_queue_type,
2790 .use_clustering = ENABLE_CLUSTERING,
2791 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2792 .max_sectors = 1024,
2795 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2796 .frame_send = bnx2fc_xmit,
2797 .elsct_send = bnx2fc_elsct_send,
2798 .fcp_abort_io = bnx2fc_abort_io,
2799 .fcp_cleanup = bnx2fc_cleanup,
2800 .get_lesb = fcoe_get_lesb,
2801 .rport_event_callback = bnx2fc_rport_event_handler,
2805 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2806 * structure carrying callback function pointers
2808 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2809 .owner = THIS_MODULE,
2810 .cnic_init = bnx2fc_ulp_init,
2811 .cnic_exit = bnx2fc_ulp_exit,
2812 .cnic_start = bnx2fc_ulp_start,
2813 .cnic_stop = bnx2fc_ulp_stop,
2814 .indicate_kcqes = bnx2fc_indicate_kcqe,
2815 .indicate_netevent = bnx2fc_indicate_netevent,
2816 .cnic_get_stats = bnx2fc_ulp_get_stats,