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 - 2011 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 "Apr 24, 2012"
28 static char version[] __devinitdata =
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 fc_function_template bnx2fc_vport_xport_function;
58 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
59 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
60 static int bnx2fc_destroy(struct net_device *net_device);
61 static int bnx2fc_enable(struct net_device *netdev);
62 static int bnx2fc_disable(struct net_device *netdev);
64 static void bnx2fc_recv_frame(struct sk_buff *skb);
66 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
67 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
68 static int bnx2fc_lport_config(struct fc_lport *lport);
69 static int bnx2fc_em_config(struct fc_lport *lport);
70 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
71 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
72 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
73 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
74 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
75 struct device *parent, int npiv);
76 static void bnx2fc_destroy_work(struct work_struct *work);
78 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
79 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
81 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
82 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
84 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
85 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
87 static void bnx2fc_port_shutdown(struct fc_lport *lport);
88 static void bnx2fc_stop(struct bnx2fc_interface *interface);
89 static int __init bnx2fc_mod_init(void);
90 static void __exit bnx2fc_mod_exit(void);
92 unsigned int bnx2fc_debug_level;
93 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
95 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
96 unsigned long action, void *hcpu);
97 /* notification function for CPU hotplug events */
98 static struct notifier_block bnx2fc_cpu_notifier = {
99 .notifier_call = bnx2fc_cpu_callback,
102 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
104 return ((struct bnx2fc_interface *)
105 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
109 * bnx2fc_get_lesb() - Fill the FCoE Link Error Status Block
110 * @lport: the local port
111 * @fc_lesb: the link error status block
113 static void bnx2fc_get_lesb(struct fc_lport *lport,
114 struct fc_els_lesb *fc_lesb)
116 struct net_device *netdev = bnx2fc_netdev(lport);
118 __fcoe_get_lesb(lport, fc_lesb, netdev);
121 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
123 struct fcoe_percpu_s *bg;
124 struct fcoe_rcv_info *fr;
125 struct sk_buff_head *list;
126 struct sk_buff *skb, *next;
127 struct sk_buff *head;
130 spin_lock_bh(&bg->fcoe_rx_list.lock);
131 list = &bg->fcoe_rx_list;
133 for (skb = head; skb != (struct sk_buff *)list;
136 fr = fcoe_dev_from_skb(skb);
137 if (fr->fr_dev == lp) {
138 __skb_unlink(skb, list);
142 spin_unlock_bh(&bg->fcoe_rx_list.lock);
145 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
148 spin_lock(&bnx2fc_global_lock);
149 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
150 spin_unlock(&bnx2fc_global_lock);
155 static void bnx2fc_abort_io(struct fc_lport *lport)
158 * This function is no-op for bnx2fc, but we do
159 * not want to leave it as NULL either, as libfc
160 * can call the default function which is
165 static void bnx2fc_cleanup(struct fc_lport *lport)
167 struct fcoe_port *port = lport_priv(lport);
168 struct bnx2fc_interface *interface = port->priv;
169 struct bnx2fc_hba *hba = interface->hba;
170 struct bnx2fc_rport *tgt;
173 BNX2FC_MISC_DBG("Entered %s\n", __func__);
174 mutex_lock(&hba->hba_mutex);
175 spin_lock_bh(&hba->hba_lock);
176 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
177 tgt = hba->tgt_ofld_list[i];
179 /* Cleanup IOs belonging to requested vport */
180 if (tgt->port == port) {
181 spin_unlock_bh(&hba->hba_lock);
182 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
183 bnx2fc_flush_active_ios(tgt);
184 spin_lock_bh(&hba->hba_lock);
188 spin_unlock_bh(&hba->hba_lock);
189 mutex_unlock(&hba->hba_mutex);
192 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
195 struct fc_rport_priv *rdata = tgt->rdata;
196 struct fc_frame_header *fh;
199 fh = fc_frame_header_get(fp);
200 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
201 "r_ctl = 0x%x\n", rdata->ids.port_id,
202 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
203 if ((fh->fh_type == FC_TYPE_ELS) &&
204 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
206 switch (fc_frame_payload_op(fp)) {
208 rc = bnx2fc_send_adisc(tgt, fp);
211 rc = bnx2fc_send_logo(tgt, fp);
214 rc = bnx2fc_send_rls(tgt, fp);
219 } else if ((fh->fh_type == FC_TYPE_BLS) &&
220 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
221 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
223 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
224 "rctl 0x%x thru non-offload path\n",
225 fh->fh_type, fh->fh_r_ctl);
235 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
237 * @lport: the associated local port
238 * @fp: the fc_frame to be transmitted
240 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
243 struct fcoe_crc_eof *cp;
245 struct fc_frame_header *fh;
246 struct bnx2fc_interface *interface;
247 struct fcoe_ctlr *ctlr;
248 struct bnx2fc_hba *hba;
249 struct fcoe_port *port;
251 struct bnx2fc_rport *tgt;
252 struct fcoe_dev_stats *stats;
255 unsigned int hlen, tlen, elen;
258 port = (struct fcoe_port *)lport_priv(lport);
259 interface = port->priv;
260 ctlr = bnx2fc_to_ctlr(interface);
261 hba = interface->hba;
263 fh = fc_frame_header_get(fp);
266 if (!lport->link_up) {
267 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
272 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
273 if (!ctlr->sel_fcf) {
274 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
278 if (fcoe_ctlr_els_send(ctlr, lport, skb))
286 * Snoop the frame header to check if the frame is for
287 * an offloaded session
290 * tgt_ofld_list access is synchronized using
291 * both hba mutex and hba lock. Atleast hba mutex or
292 * hba lock needs to be held for read access.
295 spin_lock_bh(&hba->hba_lock);
296 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
297 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
298 /* This frame is for offloaded session */
299 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
300 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
301 spin_unlock_bh(&hba->hba_lock);
302 rc = bnx2fc_xmit_l2_frame(tgt, fp);
308 spin_unlock_bh(&hba->hba_lock);
311 elen = sizeof(struct ethhdr);
312 hlen = sizeof(struct fcoe_hdr);
313 tlen = sizeof(struct fcoe_crc_eof);
314 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
316 skb->ip_summed = CHECKSUM_NONE;
317 crc = fcoe_fc_crc(fp);
319 /* copy port crc and eof to the skb buff */
320 if (skb_is_nonlinear(skb)) {
322 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
326 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
327 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
329 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
332 memset(cp, 0, sizeof(*cp));
334 cp->fcoe_crc32 = cpu_to_le32(~crc);
335 if (skb_is_nonlinear(skb)) {
340 /* adjust skb network/transport offsets to match mac/fcoe/port */
341 skb_push(skb, elen + hlen);
342 skb_reset_mac_header(skb);
343 skb_reset_network_header(skb);
345 skb->protocol = htons(ETH_P_FCOE);
346 skb->dev = interface->netdev;
348 /* fill up mac and fcoe headers */
350 eh->h_proto = htons(ETH_P_FCOE);
352 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
354 /* insert GW address */
355 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
357 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
358 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
360 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
362 hp = (struct fcoe_hdr *)(eh + 1);
363 memset(hp, 0, sizeof(*hp));
365 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
368 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
369 if (lport->seq_offload && fr_max_payload(fp)) {
370 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
371 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
373 skb_shinfo(skb)->gso_type = 0;
374 skb_shinfo(skb)->gso_size = 0;
378 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
380 stats->TxWords += wlen;
383 /* send down to lld */
385 if (port->fcoe_pending_queue.qlen)
386 fcoe_check_wait_queue(lport, skb);
387 else if (fcoe_start_io(skb))
388 fcoe_check_wait_queue(lport, skb);
394 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
396 * @skb: the receive socket buffer
397 * @dev: associated net device
399 * @olddev: last device
401 * This function receives the packet and builds FC frame and passes it up
403 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
404 struct packet_type *ptype, struct net_device *olddev)
406 struct fc_lport *lport;
407 struct bnx2fc_interface *interface;
408 struct fcoe_ctlr *ctlr;
409 struct fc_frame_header *fh;
410 struct fcoe_rcv_info *fr;
411 struct fcoe_percpu_s *bg;
414 interface = container_of(ptype, struct bnx2fc_interface,
416 ctlr = bnx2fc_to_ctlr(interface);
419 if (unlikely(lport == NULL)) {
420 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
424 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
425 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
430 * Check for minimum frame length, and make sure required FCoE
431 * and FC headers are pulled into the linear data area.
433 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
434 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
437 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
438 fh = (struct fc_frame_header *) skb_transport_header(skb);
440 oxid = ntohs(fh->fh_ox_id);
442 fr = fcoe_dev_from_skb(skb);
446 spin_lock(&bg->fcoe_rx_list.lock);
448 __skb_queue_tail(&bg->fcoe_rx_list, skb);
449 if (bg->fcoe_rx_list.qlen == 1)
450 wake_up_process(bg->thread);
452 spin_unlock(&bg->fcoe_rx_list.lock);
460 static int bnx2fc_l2_rcv_thread(void *arg)
462 struct fcoe_percpu_s *bg = arg;
465 set_user_nice(current, -20);
466 set_current_state(TASK_INTERRUPTIBLE);
467 while (!kthread_should_stop()) {
469 spin_lock_bh(&bg->fcoe_rx_list.lock);
470 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
471 spin_unlock_bh(&bg->fcoe_rx_list.lock);
472 bnx2fc_recv_frame(skb);
473 spin_lock_bh(&bg->fcoe_rx_list.lock);
475 __set_current_state(TASK_INTERRUPTIBLE);
476 spin_unlock_bh(&bg->fcoe_rx_list.lock);
478 __set_current_state(TASK_RUNNING);
483 static void bnx2fc_recv_frame(struct sk_buff *skb)
486 struct fc_lport *lport;
487 struct fcoe_rcv_info *fr;
488 struct fcoe_dev_stats *stats;
489 struct fc_frame_header *fh;
490 struct fcoe_crc_eof crc_eof;
492 struct fc_lport *vn_port;
493 struct fcoe_port *port;
498 fr = fcoe_dev_from_skb(skb);
500 if (unlikely(lport == NULL)) {
501 printk(KERN_ERR PFX "Invalid lport struct\n");
506 if (skb_is_nonlinear(skb))
508 mac = eth_hdr(skb)->h_source;
509 dest_mac = eth_hdr(skb)->h_dest;
511 /* Pull the header */
512 hp = (struct fcoe_hdr *) skb_network_header(skb);
513 fh = (struct fc_frame_header *) skb_transport_header(skb);
514 skb_pull(skb, sizeof(struct fcoe_hdr));
515 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
517 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
519 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
521 fp = (struct fc_frame *)skb;
524 fr_sof(fp) = hp->fcoe_sof;
525 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
530 fr_eof(fp) = crc_eof.fcoe_eof;
531 fr_crc(fp) = crc_eof.fcoe_crc32;
532 if (pskb_trim(skb, fr_len)) {
538 fh = fc_frame_header_get(fp);
540 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
542 port = lport_priv(vn_port);
543 if (compare_ether_addr(port->data_src_addr, dest_mac)
545 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
551 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
552 fh->fh_type == FC_TYPE_FCP) {
553 /* Drop FCP data. We dont this in L2 path */
558 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
559 fh->fh_type == FC_TYPE_ELS) {
560 switch (fc_frame_payload_op(fp)) {
562 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
563 /* drop non-FIP LOGO */
572 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
573 /* Drop incoming ABTS */
579 if (le32_to_cpu(fr_crc(fp)) !=
580 ~crc32(~0, skb->data, fr_len)) {
581 if (stats->InvalidCRCCount < 5)
582 printk(KERN_WARNING PFX "dropping frame with "
584 stats->InvalidCRCCount++;
590 fc_exch_recv(lport, fp);
594 * bnx2fc_percpu_io_thread - thread per cpu for ios
596 * @arg: ptr to bnx2fc_percpu_info structure
598 int bnx2fc_percpu_io_thread(void *arg)
600 struct bnx2fc_percpu_s *p = arg;
601 struct bnx2fc_work *work, *tmp;
602 LIST_HEAD(work_list);
604 set_user_nice(current, -20);
605 set_current_state(TASK_INTERRUPTIBLE);
606 while (!kthread_should_stop()) {
608 spin_lock_bh(&p->fp_work_lock);
609 while (!list_empty(&p->work_list)) {
610 list_splice_init(&p->work_list, &work_list);
611 spin_unlock_bh(&p->fp_work_lock);
613 list_for_each_entry_safe(work, tmp, &work_list, list) {
614 list_del_init(&work->list);
615 bnx2fc_process_cq_compl(work->tgt, work->wqe);
619 spin_lock_bh(&p->fp_work_lock);
621 __set_current_state(TASK_INTERRUPTIBLE);
622 spin_unlock_bh(&p->fp_work_lock);
624 __set_current_state(TASK_RUNNING);
629 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
631 struct fc_host_statistics *bnx2fc_stats;
632 struct fc_lport *lport = shost_priv(shost);
633 struct fcoe_port *port = lport_priv(lport);
634 struct bnx2fc_interface *interface = port->priv;
635 struct bnx2fc_hba *hba = interface->hba;
636 struct fcoe_statistics_params *fw_stats;
639 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
643 bnx2fc_stats = fc_get_host_stats(shost);
645 init_completion(&hba->stat_req_done);
646 if (bnx2fc_send_stat_req(hba))
648 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
650 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
653 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
654 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
655 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
656 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
657 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
659 bnx2fc_stats->dumped_frames = 0;
660 bnx2fc_stats->lip_count = 0;
661 bnx2fc_stats->nos_count = 0;
662 bnx2fc_stats->loss_of_sync_count = 0;
663 bnx2fc_stats->loss_of_signal_count = 0;
664 bnx2fc_stats->prim_seq_protocol_err_count = 0;
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 Scsi_Host *shost = lport->host;
676 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
677 shost->max_lun = BNX2FC_MAX_LUN;
678 shost->max_id = BNX2FC_MAX_FCP_TGT;
679 shost->max_channel = 0;
681 shost->transportt = bnx2fc_vport_xport_template;
683 shost->transportt = bnx2fc_transport_template;
685 /* Add the new host to SCSI-ml */
686 rc = scsi_add_host(lport->host, dev);
688 printk(KERN_ERR PFX "Error on scsi_add_host\n");
692 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
693 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
694 BNX2FC_NAME, BNX2FC_VERSION,
695 interface->netdev->name);
700 static void bnx2fc_link_speed_update(struct fc_lport *lport)
702 struct fcoe_port *port = lport_priv(lport);
703 struct bnx2fc_interface *interface = port->priv;
704 struct net_device *netdev = interface->netdev;
705 struct ethtool_cmd ecmd;
707 if (!__ethtool_get_settings(netdev, &ecmd)) {
708 lport->link_supported_speeds &=
709 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
710 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
711 SUPPORTED_1000baseT_Full))
712 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
713 if (ecmd.supported & SUPPORTED_10000baseT_Full)
714 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
716 switch (ethtool_cmd_speed(&ecmd)) {
718 lport->link_speed = FC_PORTSPEED_1GBIT;
721 lport->link_speed = FC_PORTSPEED_2GBIT;
724 lport->link_speed = FC_PORTSPEED_10GBIT;
729 static int bnx2fc_link_ok(struct fc_lport *lport)
731 struct fcoe_port *port = lport_priv(lport);
732 struct bnx2fc_interface *interface = port->priv;
733 struct bnx2fc_hba *hba = interface->hba;
734 struct net_device *dev = hba->phys_dev;
737 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
738 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
740 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
747 * bnx2fc_get_link_state - get network link state
749 * @hba: adapter instance pointer
751 * updates adapter structure flag based on netdev state
753 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
755 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
756 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
758 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
761 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
763 struct bnx2fc_hba *hba;
764 struct bnx2fc_interface *interface;
765 struct fcoe_ctlr *ctlr;
766 struct fcoe_port *port;
769 port = lport_priv(lport);
770 interface = port->priv;
771 ctlr = bnx2fc_to_ctlr(interface);
772 hba = interface->hba;
774 /* require support for get_pauseparam ethtool op. */
775 if (!hba->phys_dev->ethtool_ops ||
776 !hba->phys_dev->ethtool_ops->get_pauseparam)
779 if (fc_set_mfs(lport, BNX2FC_MFS))
782 skb_queue_head_init(&port->fcoe_pending_queue);
783 port->fcoe_pending_queue_active = 0;
784 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
786 bnx2fc_link_speed_update(lport);
789 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
790 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
792 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
793 fc_set_wwnn(lport, wwnn);
795 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
796 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
799 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
800 fc_set_wwpn(lport, wwpn);
806 static void bnx2fc_destroy_timer(unsigned long data)
808 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
810 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
811 "Destroy compl not received!!\n");
812 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
813 wake_up_interruptible(&hba->destroy_wait);
817 * bnx2fc_indicate_netevent - Generic netdev event handler
819 * @context: adapter structure pointer
821 * @vlan_id: vlan id - associated vlan id with this event
823 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
824 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
826 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
829 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
830 struct fc_lport *lport;
831 struct fc_lport *vport;
832 struct bnx2fc_interface *interface, *tmp;
833 struct fcoe_ctlr *ctlr;
834 int wait_for_upload = 0;
835 u32 link_possible = 1;
837 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
842 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
843 printk(KERN_ERR "indicate_netevent: "\
844 "hba is not UP!!\n");
848 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
849 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
853 case NETDEV_GOING_DOWN:
854 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
861 case NETDEV_UNREGISTER:
864 mutex_lock(&bnx2fc_dev_lock);
865 list_for_each_entry_safe(interface, tmp, &if_list, list) {
866 if (interface->hba == hba &&
867 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
868 __bnx2fc_destroy(interface);
870 mutex_unlock(&bnx2fc_dev_lock);
874 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
878 mutex_lock(&bnx2fc_dev_lock);
879 list_for_each_entry(interface, &if_list, list) {
881 if (interface->hba != hba)
884 ctlr = bnx2fc_to_ctlr(interface);
886 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
887 interface->netdev->name, event);
889 bnx2fc_link_speed_update(lport);
891 if (link_possible && !bnx2fc_link_ok(lport)) {
892 /* Reset max recv frame size to default */
893 fc_set_mfs(lport, BNX2FC_MFS);
895 * ctlr link up will only be handled during
896 * enable to avoid sending discovery solicitation
899 if (interface->enabled)
900 fcoe_ctlr_link_up(ctlr);
901 } else if (fcoe_ctlr_link_down(ctlr)) {
902 mutex_lock(&lport->lp_mutex);
903 list_for_each_entry(vport, &lport->vports, list)
904 fc_host_port_type(vport->host) =
906 mutex_unlock(&lport->lp_mutex);
907 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
908 per_cpu_ptr(lport->dev_stats,
909 get_cpu())->LinkFailureCount++;
911 fcoe_clean_pending_queue(lport);
915 mutex_unlock(&bnx2fc_dev_lock);
917 if (wait_for_upload) {
918 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
919 init_waitqueue_head(&hba->shutdown_wait);
920 BNX2FC_MISC_DBG("indicate_netevent "
921 "num_ofld_sess = %d\n",
923 hba->wait_for_link_down = 1;
924 wait_event_interruptible(hba->shutdown_wait,
925 (hba->num_ofld_sess == 0));
926 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
928 hba->wait_for_link_down = 0;
930 if (signal_pending(current))
931 flush_signals(current);
935 static int bnx2fc_libfc_config(struct fc_lport *lport)
938 /* Set the function pointers set by bnx2fc driver */
939 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
940 sizeof(struct libfc_function_template));
941 fc_elsct_init(lport);
943 fc_rport_init(lport);
948 static int bnx2fc_em_config(struct fc_lport *lport)
953 max_xid = FCOE_XIDS_PER_CPU;
955 max_xid = FCOE_MAX_XID;
956 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
958 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
965 static int bnx2fc_lport_config(struct fc_lport *lport)
969 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
970 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
971 lport->e_d_tov = 2 * 1000;
972 lport->r_a_tov = 10 * 1000;
974 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
975 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
976 lport->does_npiv = 1;
978 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
979 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
981 /* alloc stats structure */
982 if (fc_lport_init_stats(lport))
985 /* Finish fc_lport configuration */
986 fc_lport_config(lport);
992 * bnx2fc_fip_recv - handle a received FIP frame.
994 * @skb: the received skb
995 * @dev: associated &net_device
996 * @ptype: the &packet_type structure which was used to register this handler.
997 * @orig_dev: original receive &net_device, in case @ dev is a bond.
999 * Returns: 0 for success
1001 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1002 struct packet_type *ptype,
1003 struct net_device *orig_dev)
1005 struct bnx2fc_interface *interface;
1006 struct fcoe_ctlr *ctlr;
1007 interface = container_of(ptype, struct bnx2fc_interface,
1009 ctlr = bnx2fc_to_ctlr(interface);
1010 fcoe_ctlr_recv(ctlr, skb);
1015 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1017 * @fip: FCoE controller.
1018 * @old: Unicast MAC address to delete if the MAC is non-zero.
1019 * @new: Unicast MAC address to add.
1021 * Remove any previously-set unicast MAC filter.
1022 * Add secondary FCoE MAC address filter for our OUI.
1024 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1026 struct fcoe_port *port = lport_priv(lport);
1028 memcpy(port->data_src_addr, addr, ETH_ALEN);
1032 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1034 * @lport: libfc port
1036 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1038 struct fcoe_port *port;
1040 port = (struct fcoe_port *)lport_priv(lport);
1041 return port->data_src_addr;
1045 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1047 * @fip: FCoE controller.
1050 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1052 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1053 dev_queue_xmit(skb);
1056 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1058 struct Scsi_Host *shost = vport_to_shost(vport);
1059 struct fc_lport *n_port = shost_priv(shost);
1060 struct fcoe_port *port = lport_priv(n_port);
1061 struct bnx2fc_interface *interface = port->priv;
1062 struct net_device *netdev = interface->netdev;
1063 struct fc_lport *vn_port;
1067 rc = fcoe_validate_vport_create(vport);
1069 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1070 printk(KERN_ERR PFX "Failed to create vport, "
1071 "WWPN (0x%s) already exists\n",
1076 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1077 printk(KERN_ERR PFX "vn ports cannot be created on"
1078 "this interface\n");
1082 mutex_lock(&bnx2fc_dev_lock);
1083 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1084 mutex_unlock(&bnx2fc_dev_lock);
1087 if (IS_ERR(vn_port)) {
1088 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1094 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1096 vn_port->boot_time = jiffies;
1097 fc_lport_init(vn_port);
1098 fc_fabric_login(vn_port);
1099 fc_vport_setlink(vn_port);
1104 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1106 struct bnx2fc_lport *blport, *tmp;
1108 spin_lock_bh(&hba->hba_lock);
1109 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1110 if (blport->lport == lport) {
1111 list_del(&blport->list);
1115 spin_unlock_bh(&hba->hba_lock);
1118 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1120 struct Scsi_Host *shost = vport_to_shost(vport);
1121 struct fc_lport *n_port = shost_priv(shost);
1122 struct fc_lport *vn_port = vport->dd_data;
1123 struct fcoe_port *port = lport_priv(vn_port);
1124 struct bnx2fc_interface *interface = port->priv;
1125 struct fc_lport *v_port;
1128 mutex_lock(&n_port->lp_mutex);
1129 list_for_each_entry(v_port, &n_port->vports, list)
1130 if (v_port->vport == vport) {
1136 mutex_unlock(&n_port->lp_mutex);
1139 list_del(&vn_port->list);
1140 mutex_unlock(&n_port->lp_mutex);
1141 bnx2fc_free_vport(interface->hba, port->lport);
1142 bnx2fc_port_shutdown(port->lport);
1143 bnx2fc_interface_put(interface);
1144 queue_work(bnx2fc_wq, &port->destroy_work);
1148 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1150 struct fc_lport *lport = vport->dd_data;
1153 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1154 fc_fabric_logoff(lport);
1156 lport->boot_time = jiffies;
1157 fc_fabric_login(lport);
1158 fc_vport_setlink(lport);
1164 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1166 struct net_device *netdev = interface->netdev;
1167 struct net_device *physdev = interface->hba->phys_dev;
1168 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1169 struct netdev_hw_addr *ha;
1170 int sel_san_mac = 0;
1172 /* setup Source MAC Address */
1174 for_each_dev_addr(physdev, ha) {
1175 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1177 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1178 ha->addr[1], ha->addr[2], ha->addr[3],
1179 ha->addr[4], ha->addr[5]);
1181 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1182 (is_valid_ether_addr(ha->addr))) {
1183 memcpy(ctlr->ctl_src_addr, ha->addr,
1186 BNX2FC_MISC_DBG("Found SAN MAC\n");
1194 interface->fip_packet_type.func = bnx2fc_fip_recv;
1195 interface->fip_packet_type.type = htons(ETH_P_FIP);
1196 interface->fip_packet_type.dev = netdev;
1197 dev_add_pack(&interface->fip_packet_type);
1199 interface->fcoe_packet_type.func = bnx2fc_rcv;
1200 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1201 interface->fcoe_packet_type.dev = netdev;
1202 dev_add_pack(&interface->fcoe_packet_type);
1207 static int bnx2fc_attach_transport(void)
1209 bnx2fc_transport_template =
1210 fc_attach_transport(&bnx2fc_transport_function);
1212 if (bnx2fc_transport_template == NULL) {
1213 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1217 bnx2fc_vport_xport_template =
1218 fc_attach_transport(&bnx2fc_vport_xport_function);
1219 if (bnx2fc_vport_xport_template == NULL) {
1221 "Failed to attach FC transport for vport\n");
1222 fc_release_transport(bnx2fc_transport_template);
1223 bnx2fc_transport_template = NULL;
1228 static void bnx2fc_release_transport(void)
1230 fc_release_transport(bnx2fc_transport_template);
1231 fc_release_transport(bnx2fc_vport_xport_template);
1232 bnx2fc_transport_template = NULL;
1233 bnx2fc_vport_xport_template = NULL;
1236 static void bnx2fc_interface_release(struct kref *kref)
1238 struct bnx2fc_interface *interface;
1239 struct fcoe_ctlr *ctlr;
1240 struct net_device *netdev;
1242 interface = container_of(kref, struct bnx2fc_interface, kref);
1243 BNX2FC_MISC_DBG("Interface is being released\n");
1245 ctlr = bnx2fc_to_ctlr(interface);
1246 netdev = interface->netdev;
1248 /* tear-down FIP controller */
1249 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1250 fcoe_ctlr_destroy(ctlr);
1255 module_put(THIS_MODULE);
1258 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1260 kref_get(&interface->kref);
1263 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1265 kref_put(&interface->kref, bnx2fc_interface_release);
1267 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1269 /* Free the command manager */
1271 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1272 hba->cmd_mgr = NULL;
1274 kfree(hba->tgt_ofld_list);
1275 bnx2fc_unbind_pcidev(hba);
1280 * bnx2fc_hba_create - create a new bnx2fc hba
1282 * @cnic: pointer to cnic device
1284 * Creates a new FCoE hba on the given device.
1287 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1289 struct bnx2fc_hba *hba;
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);
1301 rc = bnx2fc_bind_pcidev(hba);
1303 printk(KERN_ERR PFX "create_adapter: bind error\n");
1306 hba->phys_dev = cnic->netdev;
1307 hba->next_conn_id = 0;
1309 hba->tgt_ofld_list =
1310 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1312 if (!hba->tgt_ofld_list) {
1313 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1317 hba->num_ofld_sess = 0;
1319 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1321 if (!hba->cmd_mgr) {
1322 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1326 init_waitqueue_head(&hba->shutdown_wait);
1327 init_waitqueue_head(&hba->destroy_wait);
1328 INIT_LIST_HEAD(&hba->vports);
1333 kfree(hba->tgt_ofld_list);
1335 bnx2fc_unbind_pcidev(hba);
1341 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1342 struct net_device *netdev,
1343 enum fip_state fip_mode)
1345 struct bnx2fc_interface *interface;
1346 struct fcoe_ctlr *ctlr;
1350 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1351 ctlr = kzalloc(size, GFP_KERNEL);
1353 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1356 interface = fcoe_ctlr_priv(ctlr);
1358 kref_init(&interface->kref);
1359 interface->hba = hba;
1360 interface->netdev = netdev;
1362 /* Initialize FIP */
1363 fcoe_ctlr_init(ctlr, fip_mode);
1364 ctlr->send = bnx2fc_fip_send;
1365 ctlr->update_mac = bnx2fc_update_src_mac;
1366 ctlr->get_src_addr = bnx2fc_get_src_mac;
1367 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1369 rc = bnx2fc_interface_setup(interface);
1373 fcoe_ctlr_destroy(ctlr);
1380 * bnx2fc_if_create - Create FCoE instance on a given interface
1382 * @interface: FCoE interface to create a local port on
1383 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1384 * @npiv: Indicates if the port is vport or not
1386 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1388 * Returns: Allocated fc_lport or an error pointer
1390 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1391 struct device *parent, int npiv)
1393 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1394 struct fc_lport *lport, *n_port;
1395 struct fcoe_port *port;
1396 struct Scsi_Host *shost;
1397 struct fc_vport *vport = dev_to_vport(parent);
1398 struct bnx2fc_lport *blport;
1399 struct bnx2fc_hba *hba;
1402 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1404 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1408 /* Allocate Scsi_Host structure */
1410 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1412 lport = libfc_vport_create(vport, sizeof(*port));
1415 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1418 shost = lport->host;
1419 port = lport_priv(lport);
1420 port->lport = lport;
1421 port->priv = interface;
1422 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1424 /* Configure fcoe_port */
1425 rc = bnx2fc_lport_config(lport);
1430 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1431 vport->node_name, vport->port_name);
1432 fc_set_wwnn(lport, vport->node_name);
1433 fc_set_wwpn(lport, vport->port_name);
1435 /* Configure netdev and networking properties of the lport */
1436 rc = bnx2fc_net_config(lport, interface->netdev);
1438 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1442 rc = bnx2fc_shost_config(lport, parent);
1444 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1445 interface->netdev->name);
1449 /* Initialize the libfc library */
1450 rc = bnx2fc_libfc_config(lport);
1452 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1455 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1457 /* Allocate exchange manager */
1459 rc = bnx2fc_em_config(lport);
1461 shost = vport_to_shost(vport);
1462 n_port = shost_priv(shost);
1463 rc = fc_exch_mgr_list_clone(n_port, lport);
1467 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1471 bnx2fc_interface_get(interface);
1473 hba = interface->hba;
1474 spin_lock_bh(&hba->hba_lock);
1475 blport->lport = lport;
1476 list_add_tail(&blport->list, &hba->vports);
1477 spin_unlock_bh(&hba->hba_lock);
1482 scsi_remove_host(shost);
1484 scsi_host_put(lport->host);
1490 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1492 /* Dont listen for Ethernet packets anymore */
1493 __dev_remove_pack(&interface->fcoe_packet_type);
1494 __dev_remove_pack(&interface->fip_packet_type);
1498 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1500 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1501 struct fc_lport *lport = ctlr->lp;
1502 struct fcoe_port *port = lport_priv(lport);
1503 struct bnx2fc_hba *hba = interface->hba;
1505 /* Stop the transmit retry timer */
1506 del_timer_sync(&port->timer);
1508 /* Free existing transmit skbs */
1509 fcoe_clean_pending_queue(lport);
1511 bnx2fc_net_cleanup(interface);
1513 bnx2fc_free_vport(hba, lport);
1516 static void bnx2fc_if_destroy(struct fc_lport *lport)
1519 /* Free queued packets for the receive thread */
1520 bnx2fc_clean_rx_queue(lport);
1522 /* Detach from scsi-ml */
1523 fc_remove_host(lport->host);
1524 scsi_remove_host(lport->host);
1527 * Note that only the physical lport will have the exchange manager.
1528 * for vports, this function is NOP
1530 fc_exch_mgr_free(lport);
1532 /* Free memory used by statistical counters */
1533 fc_lport_free_stats(lport);
1535 /* Release Scsi_Host */
1536 scsi_host_put(lport->host);
1539 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1541 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1542 struct fc_lport *lport = ctlr->lp;
1543 struct fcoe_port *port = lport_priv(lport);
1545 bnx2fc_interface_cleanup(interface);
1546 bnx2fc_stop(interface);
1547 list_del(&interface->list);
1548 bnx2fc_interface_put(interface);
1549 queue_work(bnx2fc_wq, &port->destroy_work);
1553 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1555 * @buffer: The name of the Ethernet interface to be destroyed
1556 * @kp: The associated kernel parameter
1558 * Called from sysfs.
1560 * Returns: 0 for success
1562 static int bnx2fc_destroy(struct net_device *netdev)
1564 struct bnx2fc_interface *interface = NULL;
1565 struct workqueue_struct *timer_work_queue;
1566 struct fcoe_ctlr *ctlr;
1570 mutex_lock(&bnx2fc_dev_lock);
1572 interface = bnx2fc_interface_lookup(netdev);
1573 ctlr = bnx2fc_to_ctlr(interface);
1574 if (!interface || !ctlr->lp) {
1576 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1580 timer_work_queue = interface->timer_work_queue;
1581 __bnx2fc_destroy(interface);
1582 destroy_workqueue(timer_work_queue);
1585 mutex_unlock(&bnx2fc_dev_lock);
1590 static void bnx2fc_destroy_work(struct work_struct *work)
1592 struct fcoe_port *port;
1593 struct fc_lport *lport;
1595 port = container_of(work, struct fcoe_port, destroy_work);
1596 lport = port->lport;
1598 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1600 bnx2fc_if_destroy(lport);
1603 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1605 bnx2fc_free_fw_resc(hba);
1606 bnx2fc_free_task_ctx(hba);
1610 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1613 * @hba: Adapter instance
1615 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1617 if (bnx2fc_setup_task_ctx(hba))
1620 if (bnx2fc_setup_fw_resc(hba))
1625 bnx2fc_unbind_adapter_devices(hba);
1629 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1631 struct cnic_dev *cnic;
1634 printk(KERN_ERR PFX "cnic is NULL\n");
1638 hba->pcidev = cnic->pcidev;
1640 pci_dev_get(hba->pcidev);
1645 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1648 pci_dev_put(hba->pcidev);
1655 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1657 * @handle: transport handle pointing to adapter struture
1659 * This function maps adapter structure to pcidev structure and initiates
1660 * firmware handshake to enable/initialize on-chip FCoE components.
1661 * This bnx2fc - cnic interface api callback is used after following
1662 * conditions are met -
1663 * a) underlying network interface is up (marked by event NETDEV_UP
1665 * b) bnx2fc adatper structure is registered.
1667 static void bnx2fc_ulp_start(void *handle)
1669 struct bnx2fc_hba *hba = handle;
1670 struct bnx2fc_interface *interface;
1671 struct fcoe_ctlr *ctlr;
1672 struct fc_lport *lport;
1674 mutex_lock(&bnx2fc_dev_lock);
1676 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1677 bnx2fc_fw_init(hba);
1679 BNX2FC_MISC_DBG("bnx2fc started.\n");
1681 list_for_each_entry(interface, &if_list, list) {
1682 if (interface->hba == hba) {
1683 ctlr = bnx2fc_to_ctlr(interface);
1685 /* Kick off Fabric discovery*/
1686 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1687 lport->tt.frame_send = bnx2fc_xmit;
1688 bnx2fc_start_disc(interface);
1692 mutex_unlock(&bnx2fc_dev_lock);
1695 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1697 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1698 fc_fabric_logoff(lport);
1699 fc_lport_destroy(lport);
1702 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1704 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1705 struct fc_lport *lport;
1706 struct fc_lport *vport;
1708 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1712 bnx2fc_port_shutdown(lport);
1714 mutex_lock(&lport->lp_mutex);
1715 list_for_each_entry(vport, &lport->vports, list)
1716 fc_host_port_type(vport->host) =
1717 FC_PORTTYPE_UNKNOWN;
1718 mutex_unlock(&lport->lp_mutex);
1719 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1720 fcoe_ctlr_link_down(ctlr);
1721 fcoe_clean_pending_queue(lport);
1724 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1726 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1730 rc = bnx2fc_bind_adapter_devices(hba);
1732 printk(KERN_ALERT PFX
1733 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1737 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1739 printk(KERN_ALERT PFX
1740 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1745 * Wait until the adapter init message is complete, and adapter
1748 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1749 msleep(BNX2FC_INIT_POLL_TIME);
1751 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1752 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1754 hba->cnic->netdev->name);
1760 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1764 bnx2fc_unbind_adapter_devices(hba);
1769 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1771 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1772 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1773 init_timer(&hba->destroy_timer);
1774 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1776 hba->destroy_timer.function = bnx2fc_destroy_timer;
1777 hba->destroy_timer.data = (unsigned long)hba;
1778 add_timer(&hba->destroy_timer);
1779 wait_event_interruptible(hba->destroy_wait,
1780 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1782 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1783 /* This should never happen */
1784 if (signal_pending(current))
1785 flush_signals(current);
1787 del_timer_sync(&hba->destroy_timer);
1789 bnx2fc_unbind_adapter_devices(hba);
1794 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1796 * @handle: transport handle pointing to adapter structure
1798 * Driver checks if adapter is already in shutdown mode, if not start
1799 * the shutdown process.
1801 static void bnx2fc_ulp_stop(void *handle)
1803 struct bnx2fc_hba *hba = handle;
1804 struct bnx2fc_interface *interface;
1806 printk(KERN_ERR "ULP_STOP\n");
1808 mutex_lock(&bnx2fc_dev_lock);
1809 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1811 list_for_each_entry(interface, &if_list, list) {
1812 if (interface->hba == hba)
1813 bnx2fc_stop(interface);
1815 BUG_ON(hba->num_ofld_sess != 0);
1817 mutex_lock(&hba->hba_mutex);
1818 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1819 clear_bit(ADAPTER_STATE_GOING_DOWN,
1820 &hba->adapter_state);
1822 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1823 mutex_unlock(&hba->hba_mutex);
1825 bnx2fc_fw_destroy(hba);
1827 mutex_unlock(&bnx2fc_dev_lock);
1830 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1832 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1833 struct fc_lport *lport;
1836 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1837 /* Kick off FIP/FLOGI */
1838 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1839 printk(KERN_ERR PFX "Init not done yet\n");
1844 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1846 if (!bnx2fc_link_ok(lport) && interface->enabled) {
1847 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1848 fcoe_ctlr_link_up(ctlr);
1849 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1850 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1853 /* wait for the FCF to be selected before issuing FLOGI */
1854 while (!ctlr->sel_fcf) {
1856 /* give up after 3 secs */
1857 if (++wait_cnt > 12)
1861 /* Reset max receive frame size to default */
1862 if (fc_set_mfs(lport, BNX2FC_MFS))
1865 fc_lport_init(lport);
1866 fc_fabric_login(lport);
1871 * bnx2fc_ulp_init - Initialize an adapter instance
1873 * @dev : cnic device handle
1874 * Called from cnic_register_driver() context to initialize all
1875 * enumerated cnic devices. This routine allocates adapter structure
1876 * and other device specific resources.
1878 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1880 struct bnx2fc_hba *hba;
1883 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1884 /* bnx2fc works only when bnx2x is loaded */
1885 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1886 (dev->max_fcoe_conn == 0)) {
1887 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1888 " flags: %lx fcoe_conn: %d\n",
1889 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1893 hba = bnx2fc_hba_create(dev);
1895 printk(KERN_ERR PFX "hba initialization failed\n");
1899 /* Add HBA to the adapter list */
1900 mutex_lock(&bnx2fc_dev_lock);
1901 list_add_tail(&hba->list, &adapter_list);
1903 mutex_unlock(&bnx2fc_dev_lock);
1905 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1906 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1909 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
1911 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1915 static int bnx2fc_disable(struct net_device *netdev)
1917 struct bnx2fc_interface *interface;
1918 struct fcoe_ctlr *ctlr;
1922 mutex_lock(&bnx2fc_dev_lock);
1924 interface = bnx2fc_interface_lookup(netdev);
1925 ctlr = bnx2fc_to_ctlr(interface);
1926 if (!interface || !ctlr->lp) {
1928 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
1930 interface->enabled = false;
1931 fcoe_ctlr_link_down(ctlr);
1932 fcoe_clean_pending_queue(ctlr->lp);
1935 mutex_unlock(&bnx2fc_dev_lock);
1941 static int bnx2fc_enable(struct net_device *netdev)
1943 struct bnx2fc_interface *interface;
1944 struct fcoe_ctlr *ctlr;
1948 mutex_lock(&bnx2fc_dev_lock);
1950 interface = bnx2fc_interface_lookup(netdev);
1951 ctlr = bnx2fc_to_ctlr(interface);
1952 if (!interface || !ctlr->lp) {
1954 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
1955 } else if (!bnx2fc_link_ok(ctlr->lp)) {
1956 fcoe_ctlr_link_up(ctlr);
1957 interface->enabled = true;
1960 mutex_unlock(&bnx2fc_dev_lock);
1966 * bnx2fc_create - Create bnx2fc FCoE interface
1968 * @buffer: The name of Ethernet interface to create on
1969 * @kp: The associated kernel param
1971 * Called from sysfs.
1973 * Returns: 0 for success
1975 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1977 struct fcoe_ctlr *ctlr;
1978 struct bnx2fc_interface *interface;
1979 struct bnx2fc_hba *hba;
1980 struct net_device *phys_dev;
1981 struct fc_lport *lport;
1982 struct ethtool_drvinfo drvinfo;
1986 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1987 if (fip_mode != FIP_MODE_FABRIC) {
1988 printk(KERN_ERR "fip mode not FABRIC\n");
1994 mutex_lock(&bnx2fc_dev_lock);
1996 if (!try_module_get(THIS_MODULE)) {
2001 /* obtain physical netdev */
2002 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2003 phys_dev = vlan_dev_real_dev(netdev);
2004 vlan_id = vlan_dev_vlan_id(netdev);
2006 printk(KERN_ERR PFX "Not a vlan device\n");
2010 /* verify if the physical device is a netxtreme2 device */
2011 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2012 memset(&drvinfo, 0, sizeof(drvinfo));
2013 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2014 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2015 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2020 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2025 /* obtain interface and initialize rest of the structure */
2026 hba = bnx2fc_hba_lookup(phys_dev);
2029 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2033 if (bnx2fc_interface_lookup(netdev)) {
2038 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2040 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2044 ctlr = bnx2fc_to_ctlr(interface);
2045 interface->vlan_id = vlan_id;
2046 interface->vlan_enabled = 1;
2048 interface->timer_work_queue =
2049 create_singlethread_workqueue("bnx2fc_timer_wq");
2050 if (!interface->timer_work_queue) {
2051 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2056 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
2058 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2064 /* Add interface to if_list */
2065 list_add_tail(&interface->list, &if_list);
2067 lport->boot_time = jiffies;
2069 /* Make this master N_port */
2072 if (!bnx2fc_link_ok(lport)) {
2073 fcoe_ctlr_link_up(ctlr);
2074 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2075 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2078 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2079 bnx2fc_start_disc(interface);
2080 interface->enabled = true;
2082 * Release from kref_init in bnx2fc_interface_setup, on success
2083 * lport should be holding a reference taken in bnx2fc_if_create
2085 bnx2fc_interface_put(interface);
2086 /* put netdev that was held while calling dev_get_by_name */
2087 mutex_unlock(&bnx2fc_dev_lock);
2092 destroy_workqueue(interface->timer_work_queue);
2094 bnx2fc_net_cleanup(interface);
2095 bnx2fc_interface_put(interface);
2098 module_put(THIS_MODULE);
2100 mutex_unlock(&bnx2fc_dev_lock);
2106 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2108 * @cnic: Pointer to cnic device instance
2111 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2113 struct list_head *list;
2114 struct list_head *temp;
2115 struct bnx2fc_hba *hba;
2117 /* Called with bnx2fc_dev_lock held */
2118 list_for_each_safe(list, temp, &adapter_list) {
2119 hba = (struct bnx2fc_hba *)list;
2120 if (hba->cnic == cnic)
2126 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2129 struct bnx2fc_interface *interface;
2131 /* Called with bnx2fc_dev_lock held */
2132 list_for_each_entry(interface, &if_list, list) {
2133 if (interface->netdev == netdev)
2139 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2142 struct bnx2fc_hba *hba;
2144 /* Called with bnx2fc_dev_lock held */
2145 list_for_each_entry(hba, &adapter_list, list) {
2146 if (hba->phys_dev == phys_dev)
2149 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2154 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2156 * @dev cnic device handle
2158 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2160 struct bnx2fc_hba *hba;
2161 struct bnx2fc_interface *interface, *tmp;
2163 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2165 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2166 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2167 dev->netdev->name, dev->flags);
2171 mutex_lock(&bnx2fc_dev_lock);
2172 hba = bnx2fc_find_hba_for_cnic(dev);
2174 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2176 mutex_unlock(&bnx2fc_dev_lock);
2180 list_del_init(&hba->list);
2183 list_for_each_entry_safe(interface, tmp, &if_list, list)
2184 /* destroy not called yet, move to quiesced list */
2185 if (interface->hba == hba)
2186 __bnx2fc_destroy(interface);
2187 mutex_unlock(&bnx2fc_dev_lock);
2189 bnx2fc_ulp_stop(hba);
2190 /* unregister cnic device */
2191 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2192 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2193 bnx2fc_hba_destroy(hba);
2197 * bnx2fc_fcoe_reset - Resets the fcoe
2199 * @shost: shost the reset is from
2203 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2205 struct fc_lport *lport = shost_priv(shost);
2206 fc_lport_reset(lport);
2211 static bool bnx2fc_match(struct net_device *netdev)
2213 mutex_lock(&bnx2fc_dev_lock);
2214 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2215 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2217 if (bnx2fc_hba_lookup(phys_dev)) {
2218 mutex_unlock(&bnx2fc_dev_lock);
2222 mutex_unlock(&bnx2fc_dev_lock);
2227 static struct fcoe_transport bnx2fc_transport = {
2230 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2231 .match = bnx2fc_match,
2232 .create = bnx2fc_create,
2233 .destroy = bnx2fc_destroy,
2234 .enable = bnx2fc_enable,
2235 .disable = bnx2fc_disable,
2239 * bnx2fc_percpu_thread_create - Create a receive thread for an
2242 * @cpu: cpu index for the online cpu
2244 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2246 struct bnx2fc_percpu_s *p;
2247 struct task_struct *thread;
2249 p = &per_cpu(bnx2fc_percpu, cpu);
2251 thread = kthread_create(bnx2fc_percpu_io_thread,
2253 "bnx2fc_thread/%d", cpu);
2254 /* bind thread to the cpu */
2255 if (likely(!IS_ERR(thread))) {
2256 kthread_bind(thread, cpu);
2257 p->iothread = thread;
2258 wake_up_process(thread);
2262 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2264 struct bnx2fc_percpu_s *p;
2265 struct task_struct *thread;
2266 struct bnx2fc_work *work, *tmp;
2268 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2270 /* Prevent any new work from being queued for this CPU */
2271 p = &per_cpu(bnx2fc_percpu, cpu);
2272 spin_lock_bh(&p->fp_work_lock);
2273 thread = p->iothread;
2277 /* Free all work in the list */
2278 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2279 list_del_init(&work->list);
2280 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2284 spin_unlock_bh(&p->fp_work_lock);
2287 kthread_stop(thread);
2291 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2293 * @nfb: The callback data block
2294 * @action: The event triggering the callback
2295 * @hcpu: The index of the CPU that the event is for
2297 * This creates or destroys per-CPU data for fcoe
2299 * Returns NOTIFY_OK always.
2301 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2302 unsigned long action, void *hcpu)
2304 unsigned cpu = (unsigned long)hcpu;
2308 case CPU_ONLINE_FROZEN:
2309 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2310 bnx2fc_percpu_thread_create(cpu);
2313 case CPU_DEAD_FROZEN:
2314 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2315 bnx2fc_percpu_thread_destroy(cpu);
2324 * bnx2fc_mod_init - module init entry point
2326 * Initialize driver wide global data structures, and register
2329 static int __init bnx2fc_mod_init(void)
2331 struct fcoe_percpu_s *bg;
2332 struct task_struct *l2_thread;
2334 unsigned int cpu = 0;
2335 struct bnx2fc_percpu_s *p;
2337 printk(KERN_INFO PFX "%s", version);
2339 /* register as a fcoe transport */
2340 rc = fcoe_transport_attach(&bnx2fc_transport);
2342 printk(KERN_ERR "failed to register an fcoe transport, check "
2343 "if libfcoe is loaded\n");
2347 INIT_LIST_HEAD(&adapter_list);
2348 INIT_LIST_HEAD(&if_list);
2349 mutex_init(&bnx2fc_dev_lock);
2352 /* Attach FC transport template */
2353 rc = bnx2fc_attach_transport();
2357 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2363 bg = &bnx2fc_global;
2364 skb_queue_head_init(&bg->fcoe_rx_list);
2365 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2367 "bnx2fc_l2_thread");
2368 if (IS_ERR(l2_thread)) {
2369 rc = PTR_ERR(l2_thread);
2372 wake_up_process(l2_thread);
2373 spin_lock_bh(&bg->fcoe_rx_list.lock);
2374 bg->thread = l2_thread;
2375 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2377 for_each_possible_cpu(cpu) {
2378 p = &per_cpu(bnx2fc_percpu, cpu);
2379 INIT_LIST_HEAD(&p->work_list);
2380 spin_lock_init(&p->fp_work_lock);
2383 for_each_online_cpu(cpu) {
2384 bnx2fc_percpu_thread_create(cpu);
2387 /* Initialize per CPU interrupt thread */
2388 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2390 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2395 destroy_workqueue(bnx2fc_wq);
2397 bnx2fc_release_transport();
2399 fcoe_transport_detach(&bnx2fc_transport);
2404 static void __exit bnx2fc_mod_exit(void)
2406 LIST_HEAD(to_be_deleted);
2407 struct bnx2fc_hba *hba, *next;
2408 struct fcoe_percpu_s *bg;
2409 struct task_struct *l2_thread;
2410 struct sk_buff *skb;
2411 unsigned int cpu = 0;
2414 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2415 * it will have higher precedence than bnx2fc_dev_lock.
2416 * unregister_device() cannot be called with bnx2fc_dev_lock
2419 mutex_lock(&bnx2fc_dev_lock);
2420 list_splice(&adapter_list, &to_be_deleted);
2421 INIT_LIST_HEAD(&adapter_list);
2423 mutex_unlock(&bnx2fc_dev_lock);
2425 /* Unregister with cnic */
2426 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2427 list_del_init(&hba->list);
2428 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2430 bnx2fc_ulp_stop(hba);
2431 /* unregister cnic device */
2432 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2433 &hba->reg_with_cnic))
2434 hba->cnic->unregister_device(hba->cnic,
2436 bnx2fc_hba_destroy(hba);
2438 cnic_unregister_driver(CNIC_ULP_FCOE);
2440 /* Destroy global thread */
2441 bg = &bnx2fc_global;
2442 spin_lock_bh(&bg->fcoe_rx_list.lock);
2443 l2_thread = bg->thread;
2445 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2448 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2451 kthread_stop(l2_thread);
2453 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2455 /* Destroy per cpu threads */
2456 for_each_online_cpu(cpu) {
2457 bnx2fc_percpu_thread_destroy(cpu);
2460 destroy_workqueue(bnx2fc_wq);
2462 * detach from scsi transport
2463 * must happen after all destroys are done
2465 bnx2fc_release_transport();
2467 /* detach from fcoe transport */
2468 fcoe_transport_detach(&bnx2fc_transport);
2471 module_init(bnx2fc_mod_init);
2472 module_exit(bnx2fc_mod_exit);
2474 static struct fc_function_template bnx2fc_transport_function = {
2475 .show_host_node_name = 1,
2476 .show_host_port_name = 1,
2477 .show_host_supported_classes = 1,
2478 .show_host_supported_fc4s = 1,
2479 .show_host_active_fc4s = 1,
2480 .show_host_maxframe_size = 1,
2482 .show_host_port_id = 1,
2483 .show_host_supported_speeds = 1,
2484 .get_host_speed = fc_get_host_speed,
2485 .show_host_speed = 1,
2486 .show_host_port_type = 1,
2487 .get_host_port_state = fc_get_host_port_state,
2488 .show_host_port_state = 1,
2489 .show_host_symbolic_name = 1,
2491 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2492 sizeof(struct bnx2fc_rport)),
2493 .show_rport_maxframe_size = 1,
2494 .show_rport_supported_classes = 1,
2496 .show_host_fabric_name = 1,
2497 .show_starget_node_name = 1,
2498 .show_starget_port_name = 1,
2499 .show_starget_port_id = 1,
2500 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2501 .show_rport_dev_loss_tmo = 1,
2502 .get_fc_host_stats = bnx2fc_get_host_stats,
2504 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2506 .terminate_rport_io = fc_rport_terminate_io,
2508 .vport_create = bnx2fc_vport_create,
2509 .vport_delete = bnx2fc_vport_destroy,
2510 .vport_disable = bnx2fc_vport_disable,
2511 .bsg_request = fc_lport_bsg_request,
2514 static struct fc_function_template bnx2fc_vport_xport_function = {
2515 .show_host_node_name = 1,
2516 .show_host_port_name = 1,
2517 .show_host_supported_classes = 1,
2518 .show_host_supported_fc4s = 1,
2519 .show_host_active_fc4s = 1,
2520 .show_host_maxframe_size = 1,
2522 .show_host_port_id = 1,
2523 .show_host_supported_speeds = 1,
2524 .get_host_speed = fc_get_host_speed,
2525 .show_host_speed = 1,
2526 .show_host_port_type = 1,
2527 .get_host_port_state = fc_get_host_port_state,
2528 .show_host_port_state = 1,
2529 .show_host_symbolic_name = 1,
2531 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2532 sizeof(struct bnx2fc_rport)),
2533 .show_rport_maxframe_size = 1,
2534 .show_rport_supported_classes = 1,
2536 .show_host_fabric_name = 1,
2537 .show_starget_node_name = 1,
2538 .show_starget_port_name = 1,
2539 .show_starget_port_id = 1,
2540 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2541 .show_rport_dev_loss_tmo = 1,
2542 .get_fc_host_stats = fc_get_host_stats,
2543 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2544 .terminate_rport_io = fc_rport_terminate_io,
2545 .bsg_request = fc_lport_bsg_request,
2549 * scsi_host_template structure used while registering with SCSI-ml
2551 static struct scsi_host_template bnx2fc_shost_template = {
2552 .module = THIS_MODULE,
2553 .name = "Broadcom Offload FCoE Initiator",
2554 .queuecommand = bnx2fc_queuecommand,
2555 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2556 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2557 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2558 .eh_host_reset_handler = fc_eh_host_reset,
2559 .slave_alloc = fc_slave_alloc,
2560 .change_queue_depth = fc_change_queue_depth,
2561 .change_queue_type = fc_change_queue_type,
2564 .can_queue = BNX2FC_CAN_QUEUE,
2565 .use_clustering = ENABLE_CLUSTERING,
2566 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2570 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2571 .frame_send = bnx2fc_xmit,
2572 .elsct_send = bnx2fc_elsct_send,
2573 .fcp_abort_io = bnx2fc_abort_io,
2574 .fcp_cleanup = bnx2fc_cleanup,
2575 .get_lesb = bnx2fc_get_lesb,
2576 .rport_event_callback = bnx2fc_rport_event_handler,
2580 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2581 * structure carrying callback function pointers
2583 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2584 .owner = THIS_MODULE,
2585 .cnic_init = bnx2fc_ulp_init,
2586 .cnic_exit = bnx2fc_ulp_exit,
2587 .cnic_start = bnx2fc_ulp_start,
2588 .cnic_stop = bnx2fc_ulp_stop,
2589 .indicate_kcqes = bnx2fc_indicate_kcqe,
2590 .indicate_netevent = bnx2fc_indicate_netevent,