8250_pci: Fix missing const from merges
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
blobab255fbc7f36332f43583f7c546d459dd61fcbd0
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 - 2010 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)
15 #include "bnx2fc.h"
17 static struct list_head adapter_list;
18 static u32 adapter_count;
19 static DEFINE_MUTEX(bnx2fc_dev_lock);
20 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22 #define DRV_MODULE_NAME "bnx2fc"
23 #define DRV_MODULE_VERSION BNX2FC_VERSION
24 #define DRV_MODULE_RELDATE "Mar 17, 2011"
27 static char version[] __devinitdata =
28 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
29 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
32 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
33 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(DRV_MODULE_VERSION);
37 #define BNX2FC_MAX_QUEUE_DEPTH 256
38 #define BNX2FC_MIN_QUEUE_DEPTH 32
39 #define FCOE_WORD_TO_BYTE 4
41 static struct scsi_transport_template *bnx2fc_transport_template;
42 static struct scsi_transport_template *bnx2fc_vport_xport_template;
44 struct workqueue_struct *bnx2fc_wq;
46 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
47 * Here the io threads are per cpu but the l2 thread is just one
49 struct fcoe_percpu_s bnx2fc_global;
50 DEFINE_SPINLOCK(bnx2fc_global_lock);
52 static struct cnic_ulp_ops bnx2fc_cnic_cb;
53 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
54 static struct scsi_host_template bnx2fc_shost_template;
55 static struct fc_function_template bnx2fc_transport_function;
56 static struct fc_function_template bnx2fc_vport_xport_function;
57 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
58 static int bnx2fc_destroy(struct net_device *net_device);
59 static int bnx2fc_enable(struct net_device *netdev);
60 static int bnx2fc_disable(struct net_device *netdev);
62 static void bnx2fc_recv_frame(struct sk_buff *skb);
64 static void bnx2fc_start_disc(struct bnx2fc_hba *hba);
65 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
66 static int bnx2fc_net_config(struct fc_lport *lp);
67 static int bnx2fc_lport_config(struct fc_lport *lport);
68 static int bnx2fc_em_config(struct fc_lport *lport);
69 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
70 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
71 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
72 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
73 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
74 struct device *parent, int npiv);
75 static void bnx2fc_destroy_work(struct work_struct *work);
77 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
78 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
80 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
81 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
83 static void bnx2fc_port_shutdown(struct fc_lport *lport);
84 static void bnx2fc_stop(struct bnx2fc_hba *hba);
85 static int __init bnx2fc_mod_init(void);
86 static void __exit bnx2fc_mod_exit(void);
88 unsigned int bnx2fc_debug_level;
89 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
91 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
92 unsigned long action, void *hcpu);
93 /* notification function for CPU hotplug events */
94 static struct notifier_block bnx2fc_cpu_notifier = {
95 .notifier_call = bnx2fc_cpu_callback,
98 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
100 struct fcoe_percpu_s *bg;
101 struct fcoe_rcv_info *fr;
102 struct sk_buff_head *list;
103 struct sk_buff *skb, *next;
104 struct sk_buff *head;
106 bg = &bnx2fc_global;
107 spin_lock_bh(&bg->fcoe_rx_list.lock);
108 list = &bg->fcoe_rx_list;
109 head = list->next;
110 for (skb = head; skb != (struct sk_buff *)list;
111 skb = next) {
112 next = skb->next;
113 fr = fcoe_dev_from_skb(skb);
114 if (fr->fr_dev == lp) {
115 __skb_unlink(skb, list);
116 kfree_skb(skb);
119 spin_unlock_bh(&bg->fcoe_rx_list.lock);
122 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
124 int rc;
125 spin_lock(&bnx2fc_global_lock);
126 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
127 spin_unlock(&bnx2fc_global_lock);
129 return rc;
132 static void bnx2fc_abort_io(struct fc_lport *lport)
135 * This function is no-op for bnx2fc, but we do
136 * not want to leave it as NULL either, as libfc
137 * can call the default function which is
138 * fc_fcp_abort_io.
142 static void bnx2fc_cleanup(struct fc_lport *lport)
144 struct fcoe_port *port = lport_priv(lport);
145 struct bnx2fc_hba *hba = port->priv;
146 struct bnx2fc_rport *tgt;
147 int i;
149 BNX2FC_MISC_DBG("Entered %s\n", __func__);
150 mutex_lock(&hba->hba_mutex);
151 spin_lock_bh(&hba->hba_lock);
152 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
153 tgt = hba->tgt_ofld_list[i];
154 if (tgt) {
155 /* Cleanup IOs belonging to requested vport */
156 if (tgt->port == port) {
157 spin_unlock_bh(&hba->hba_lock);
158 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
159 bnx2fc_flush_active_ios(tgt);
160 spin_lock_bh(&hba->hba_lock);
164 spin_unlock_bh(&hba->hba_lock);
165 mutex_unlock(&hba->hba_mutex);
168 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
169 struct fc_frame *fp)
171 struct fc_rport_priv *rdata = tgt->rdata;
172 struct fc_frame_header *fh;
173 int rc = 0;
175 fh = fc_frame_header_get(fp);
176 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
177 "r_ctl = 0x%x\n", rdata->ids.port_id,
178 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
179 if ((fh->fh_type == FC_TYPE_ELS) &&
180 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
182 switch (fc_frame_payload_op(fp)) {
183 case ELS_ADISC:
184 rc = bnx2fc_send_adisc(tgt, fp);
185 break;
186 case ELS_LOGO:
187 rc = bnx2fc_send_logo(tgt, fp);
188 break;
189 case ELS_RLS:
190 rc = bnx2fc_send_rls(tgt, fp);
191 break;
192 default:
193 break;
195 } else if ((fh->fh_type == FC_TYPE_BLS) &&
196 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
197 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
198 else {
199 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
200 "rctl 0x%x thru non-offload path\n",
201 fh->fh_type, fh->fh_r_ctl);
202 return -ENODEV;
204 if (rc)
205 return -ENOMEM;
206 else
207 return 0;
211 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
213 * @lport: the associated local port
214 * @fp: the fc_frame to be transmitted
216 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
218 struct ethhdr *eh;
219 struct fcoe_crc_eof *cp;
220 struct sk_buff *skb;
221 struct fc_frame_header *fh;
222 struct bnx2fc_hba *hba;
223 struct fcoe_port *port;
224 struct fcoe_hdr *hp;
225 struct bnx2fc_rport *tgt;
226 struct fcoe_dev_stats *stats;
227 u8 sof, eof;
228 u32 crc;
229 unsigned int hlen, tlen, elen;
230 int wlen, rc = 0;
232 port = (struct fcoe_port *)lport_priv(lport);
233 hba = port->priv;
235 fh = fc_frame_header_get(fp);
237 skb = fp_skb(fp);
238 if (!lport->link_up) {
239 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
240 kfree_skb(skb);
241 return 0;
244 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
245 if (!hba->ctlr.sel_fcf) {
246 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
247 kfree_skb(skb);
248 return -EINVAL;
250 if (fcoe_ctlr_els_send(&hba->ctlr, lport, skb))
251 return 0;
254 sof = fr_sof(fp);
255 eof = fr_eof(fp);
258 * Snoop the frame header to check if the frame is for
259 * an offloaded session
262 * tgt_ofld_list access is synchronized using
263 * both hba mutex and hba lock. Atleast hba mutex or
264 * hba lock needs to be held for read access.
267 spin_lock_bh(&hba->hba_lock);
268 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
269 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
270 /* This frame is for offloaded session */
271 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
272 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
273 spin_unlock_bh(&hba->hba_lock);
274 rc = bnx2fc_xmit_l2_frame(tgt, fp);
275 if (rc != -ENODEV) {
276 kfree_skb(skb);
277 return rc;
279 } else {
280 spin_unlock_bh(&hba->hba_lock);
283 elen = sizeof(struct ethhdr);
284 hlen = sizeof(struct fcoe_hdr);
285 tlen = sizeof(struct fcoe_crc_eof);
286 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
288 skb->ip_summed = CHECKSUM_NONE;
289 crc = fcoe_fc_crc(fp);
291 /* copy port crc and eof to the skb buff */
292 if (skb_is_nonlinear(skb)) {
293 skb_frag_t *frag;
294 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
295 kfree_skb(skb);
296 return -ENOMEM;
298 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
299 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
300 + frag->page_offset;
301 } else {
302 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
305 memset(cp, 0, sizeof(*cp));
306 cp->fcoe_eof = eof;
307 cp->fcoe_crc32 = cpu_to_le32(~crc);
308 if (skb_is_nonlinear(skb)) {
309 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
310 cp = NULL;
313 /* adjust skb network/transport offsets to match mac/fcoe/port */
314 skb_push(skb, elen + hlen);
315 skb_reset_mac_header(skb);
316 skb_reset_network_header(skb);
317 skb->mac_len = elen;
318 skb->protocol = htons(ETH_P_FCOE);
319 skb->dev = hba->netdev;
321 /* fill up mac and fcoe headers */
322 eh = eth_hdr(skb);
323 eh->h_proto = htons(ETH_P_FCOE);
324 if (hba->ctlr.map_dest)
325 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
326 else
327 /* insert GW address */
328 memcpy(eh->h_dest, hba->ctlr.dest_addr, ETH_ALEN);
330 if (unlikely(hba->ctlr.flogi_oxid != FC_XID_UNKNOWN))
331 memcpy(eh->h_source, hba->ctlr.ctl_src_addr, ETH_ALEN);
332 else
333 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
335 hp = (struct fcoe_hdr *)(eh + 1);
336 memset(hp, 0, sizeof(*hp));
337 if (FC_FCOE_VER)
338 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
339 hp->fcoe_sof = sof;
341 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
342 if (lport->seq_offload && fr_max_payload(fp)) {
343 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
344 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
345 } else {
346 skb_shinfo(skb)->gso_type = 0;
347 skb_shinfo(skb)->gso_size = 0;
350 /*update tx stats */
351 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
352 stats->TxFrames++;
353 stats->TxWords += wlen;
354 put_cpu();
356 /* send down to lld */
357 fr_dev(fp) = lport;
358 if (port->fcoe_pending_queue.qlen)
359 fcoe_check_wait_queue(lport, skb);
360 else if (fcoe_start_io(skb))
361 fcoe_check_wait_queue(lport, skb);
363 return 0;
367 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
369 * @skb: the receive socket buffer
370 * @dev: associated net device
371 * @ptype: context
372 * @olddev: last device
374 * This function receives the packet and builds FC frame and passes it up
376 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
377 struct packet_type *ptype, struct net_device *olddev)
379 struct fc_lport *lport;
380 struct bnx2fc_hba *hba;
381 struct fc_frame_header *fh;
382 struct fcoe_rcv_info *fr;
383 struct fcoe_percpu_s *bg;
384 unsigned short oxid;
386 hba = container_of(ptype, struct bnx2fc_hba, fcoe_packet_type);
387 lport = hba->ctlr.lp;
389 if (unlikely(lport == NULL)) {
390 printk(KERN_ALERT PFX "bnx2fc_rcv: lport is NULL\n");
391 goto err;
394 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
395 printk(KERN_ALERT PFX "bnx2fc_rcv: Wrong FC type frame\n");
396 goto err;
400 * Check for minimum frame length, and make sure required FCoE
401 * and FC headers are pulled into the linear data area.
403 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
404 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
405 goto err;
407 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
408 fh = (struct fc_frame_header *) skb_transport_header(skb);
410 oxid = ntohs(fh->fh_ox_id);
412 fr = fcoe_dev_from_skb(skb);
413 fr->fr_dev = lport;
414 fr->ptype = ptype;
416 bg = &bnx2fc_global;
417 spin_lock_bh(&bg->fcoe_rx_list.lock);
419 __skb_queue_tail(&bg->fcoe_rx_list, skb);
420 if (bg->fcoe_rx_list.qlen == 1)
421 wake_up_process(bg->thread);
423 spin_unlock_bh(&bg->fcoe_rx_list.lock);
425 return 0;
426 err:
427 kfree_skb(skb);
428 return -1;
431 static int bnx2fc_l2_rcv_thread(void *arg)
433 struct fcoe_percpu_s *bg = arg;
434 struct sk_buff *skb;
436 set_user_nice(current, -20);
437 set_current_state(TASK_INTERRUPTIBLE);
438 while (!kthread_should_stop()) {
439 schedule();
440 spin_lock_bh(&bg->fcoe_rx_list.lock);
441 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
442 spin_unlock_bh(&bg->fcoe_rx_list.lock);
443 bnx2fc_recv_frame(skb);
444 spin_lock_bh(&bg->fcoe_rx_list.lock);
446 __set_current_state(TASK_INTERRUPTIBLE);
447 spin_unlock_bh(&bg->fcoe_rx_list.lock);
449 __set_current_state(TASK_RUNNING);
450 return 0;
454 static void bnx2fc_recv_frame(struct sk_buff *skb)
456 u32 fr_len;
457 struct fc_lport *lport;
458 struct fcoe_rcv_info *fr;
459 struct fcoe_dev_stats *stats;
460 struct fc_frame_header *fh;
461 struct fcoe_crc_eof crc_eof;
462 struct fc_frame *fp;
463 struct fc_lport *vn_port;
464 struct fcoe_port *port;
465 u8 *mac = NULL;
466 u8 *dest_mac = NULL;
467 struct fcoe_hdr *hp;
469 fr = fcoe_dev_from_skb(skb);
470 lport = fr->fr_dev;
471 if (unlikely(lport == NULL)) {
472 printk(KERN_ALERT PFX "Invalid lport struct\n");
473 kfree_skb(skb);
474 return;
477 if (skb_is_nonlinear(skb))
478 skb_linearize(skb);
479 mac = eth_hdr(skb)->h_source;
480 dest_mac = eth_hdr(skb)->h_dest;
482 /* Pull the header */
483 hp = (struct fcoe_hdr *) skb_network_header(skb);
484 fh = (struct fc_frame_header *) skb_transport_header(skb);
485 skb_pull(skb, sizeof(struct fcoe_hdr));
486 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
488 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
489 stats->RxFrames++;
490 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
492 fp = (struct fc_frame *)skb;
493 fc_frame_init(fp);
494 fr_dev(fp) = lport;
495 fr_sof(fp) = hp->fcoe_sof;
496 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
497 put_cpu();
498 kfree_skb(skb);
499 return;
501 fr_eof(fp) = crc_eof.fcoe_eof;
502 fr_crc(fp) = crc_eof.fcoe_crc32;
503 if (pskb_trim(skb, fr_len)) {
504 put_cpu();
505 kfree_skb(skb);
506 return;
509 fh = fc_frame_header_get(fp);
511 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
512 if (vn_port) {
513 port = lport_priv(vn_port);
514 if (compare_ether_addr(port->data_src_addr, dest_mac)
515 != 0) {
516 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
517 put_cpu();
518 kfree_skb(skb);
519 return;
522 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
523 fh->fh_type == FC_TYPE_FCP) {
524 /* Drop FCP data. We dont this in L2 path */
525 put_cpu();
526 kfree_skb(skb);
527 return;
529 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
530 fh->fh_type == FC_TYPE_ELS) {
531 switch (fc_frame_payload_op(fp)) {
532 case ELS_LOGO:
533 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
534 /* drop non-FIP LOGO */
535 put_cpu();
536 kfree_skb(skb);
537 return;
539 break;
542 if (le32_to_cpu(fr_crc(fp)) !=
543 ~crc32(~0, skb->data, fr_len)) {
544 if (stats->InvalidCRCCount < 5)
545 printk(KERN_WARNING PFX "dropping frame with "
546 "CRC error\n");
547 stats->InvalidCRCCount++;
548 put_cpu();
549 kfree_skb(skb);
550 return;
552 put_cpu();
553 fc_exch_recv(lport, fp);
557 * bnx2fc_percpu_io_thread - thread per cpu for ios
559 * @arg: ptr to bnx2fc_percpu_info structure
561 int bnx2fc_percpu_io_thread(void *arg)
563 struct bnx2fc_percpu_s *p = arg;
564 struct bnx2fc_work *work, *tmp;
565 LIST_HEAD(work_list);
567 set_user_nice(current, -20);
568 set_current_state(TASK_INTERRUPTIBLE);
569 while (!kthread_should_stop()) {
570 schedule();
571 spin_lock_bh(&p->fp_work_lock);
572 while (!list_empty(&p->work_list)) {
573 list_splice_init(&p->work_list, &work_list);
574 spin_unlock_bh(&p->fp_work_lock);
576 list_for_each_entry_safe(work, tmp, &work_list, list) {
577 list_del_init(&work->list);
578 bnx2fc_process_cq_compl(work->tgt, work->wqe);
579 kfree(work);
582 spin_lock_bh(&p->fp_work_lock);
584 __set_current_state(TASK_INTERRUPTIBLE);
585 spin_unlock_bh(&p->fp_work_lock);
587 __set_current_state(TASK_RUNNING);
589 return 0;
592 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
594 struct fc_host_statistics *bnx2fc_stats;
595 struct fc_lport *lport = shost_priv(shost);
596 struct fcoe_port *port = lport_priv(lport);
597 struct bnx2fc_hba *hba = port->priv;
598 struct fcoe_statistics_params *fw_stats;
599 int rc = 0;
601 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
602 if (!fw_stats)
603 return NULL;
605 bnx2fc_stats = fc_get_host_stats(shost);
607 init_completion(&hba->stat_req_done);
608 if (bnx2fc_send_stat_req(hba))
609 return bnx2fc_stats;
610 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
611 if (!rc) {
612 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
613 return bnx2fc_stats;
615 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat1.fc_crc_cnt;
616 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
617 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
618 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
619 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
621 bnx2fc_stats->dumped_frames = 0;
622 bnx2fc_stats->lip_count = 0;
623 bnx2fc_stats->nos_count = 0;
624 bnx2fc_stats->loss_of_sync_count = 0;
625 bnx2fc_stats->loss_of_signal_count = 0;
626 bnx2fc_stats->prim_seq_protocol_err_count = 0;
628 return bnx2fc_stats;
631 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
633 struct fcoe_port *port = lport_priv(lport);
634 struct bnx2fc_hba *hba = port->priv;
635 struct Scsi_Host *shost = lport->host;
636 int rc = 0;
638 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
639 shost->max_lun = BNX2FC_MAX_LUN;
640 shost->max_id = BNX2FC_MAX_FCP_TGT;
641 shost->max_channel = 0;
642 if (lport->vport)
643 shost->transportt = bnx2fc_vport_xport_template;
644 else
645 shost->transportt = bnx2fc_transport_template;
647 /* Add the new host to SCSI-ml */
648 rc = scsi_add_host(lport->host, dev);
649 if (rc) {
650 printk(KERN_ERR PFX "Error on scsi_add_host\n");
651 return rc;
653 if (!lport->vport)
654 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
655 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
656 BNX2FC_NAME, BNX2FC_VERSION,
657 hba->netdev->name);
659 return 0;
662 static void bnx2fc_link_speed_update(struct fc_lport *lport)
664 struct fcoe_port *port = lport_priv(lport);
665 struct bnx2fc_hba *hba = port->priv;
666 struct net_device *netdev = hba->netdev;
667 struct ethtool_cmd ecmd;
669 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
670 lport->link_supported_speeds &=
671 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
672 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
673 SUPPORTED_1000baseT_Full))
674 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
675 if (ecmd.supported & SUPPORTED_10000baseT_Full)
676 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
678 switch (ethtool_cmd_speed(&ecmd)) {
679 case SPEED_1000:
680 lport->link_speed = FC_PORTSPEED_1GBIT;
681 break;
682 case SPEED_10000:
683 lport->link_speed = FC_PORTSPEED_10GBIT;
684 break;
688 static int bnx2fc_link_ok(struct fc_lport *lport)
690 struct fcoe_port *port = lport_priv(lport);
691 struct bnx2fc_hba *hba = port->priv;
692 struct net_device *dev = hba->phys_dev;
693 int rc = 0;
695 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
696 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
697 else {
698 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
699 rc = -1;
701 return rc;
705 * bnx2fc_get_link_state - get network link state
707 * @hba: adapter instance pointer
709 * updates adapter structure flag based on netdev state
711 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
713 if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
714 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
715 else
716 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
719 static int bnx2fc_net_config(struct fc_lport *lport)
721 struct bnx2fc_hba *hba;
722 struct fcoe_port *port;
723 u64 wwnn, wwpn;
725 port = lport_priv(lport);
726 hba = port->priv;
728 /* require support for get_pauseparam ethtool op. */
729 if (!hba->phys_dev->ethtool_ops ||
730 !hba->phys_dev->ethtool_ops->get_pauseparam)
731 return -EOPNOTSUPP;
733 if (fc_set_mfs(lport, BNX2FC_MFS))
734 return -EINVAL;
736 skb_queue_head_init(&port->fcoe_pending_queue);
737 port->fcoe_pending_queue_active = 0;
738 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
740 bnx2fc_link_speed_update(lport);
742 if (!lport->vport) {
743 wwnn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 1, 0);
744 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
745 fc_set_wwnn(lport, wwnn);
747 wwpn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 2, 0);
748 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
749 fc_set_wwpn(lport, wwpn);
752 return 0;
755 static void bnx2fc_destroy_timer(unsigned long data)
757 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
759 BNX2FC_HBA_DBG(hba->ctlr.lp, "ERROR:bnx2fc_destroy_timer - "
760 "Destroy compl not received!!\n");
761 hba->flags |= BNX2FC_FLAG_DESTROY_CMPL;
762 wake_up_interruptible(&hba->destroy_wait);
766 * bnx2fc_indicate_netevent - Generic netdev event handler
768 * @context: adapter structure pointer
769 * @event: event type
771 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
772 * NETDEV_CHANGE_MTU events
774 static void bnx2fc_indicate_netevent(void *context, unsigned long event)
776 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
777 struct fc_lport *lport = hba->ctlr.lp;
778 struct fc_lport *vport;
779 u32 link_possible = 1;
781 if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
782 BNX2FC_MISC_DBG("driver not ready. event=%s %ld\n",
783 hba->netdev->name, event);
784 return;
788 * ASSUMPTION:
789 * indicate_netevent cannot be called from cnic unless bnx2fc
790 * does register_device
792 BUG_ON(!lport);
794 BNX2FC_HBA_DBG(lport, "enter netevent handler - event=%s %ld\n",
795 hba->netdev->name, event);
797 switch (event) {
798 case NETDEV_UP:
799 BNX2FC_HBA_DBG(lport, "Port up, adapter_state = %ld\n",
800 hba->adapter_state);
801 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
802 printk(KERN_ERR "indicate_netevent: "\
803 "adapter is not UP!!\n");
804 break;
806 case NETDEV_DOWN:
807 BNX2FC_HBA_DBG(lport, "Port down\n");
808 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
809 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
810 link_possible = 0;
811 break;
813 case NETDEV_GOING_DOWN:
814 BNX2FC_HBA_DBG(lport, "Port going down\n");
815 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
816 link_possible = 0;
817 break;
819 case NETDEV_CHANGE:
820 BNX2FC_HBA_DBG(lport, "NETDEV_CHANGE\n");
821 break;
823 default:
824 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
825 return;
828 bnx2fc_link_speed_update(lport);
830 if (link_possible && !bnx2fc_link_ok(lport)) {
831 printk(KERN_ERR "indicate_netevent: call ctlr_link_up\n");
832 fcoe_ctlr_link_up(&hba->ctlr);
833 } else {
834 printk(KERN_ERR "indicate_netevent: call ctlr_link_down\n");
835 if (fcoe_ctlr_link_down(&hba->ctlr)) {
836 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
837 mutex_lock(&lport->lp_mutex);
838 list_for_each_entry(vport, &lport->vports, list)
839 fc_host_port_type(vport->host) =
840 FC_PORTTYPE_UNKNOWN;
841 mutex_unlock(&lport->lp_mutex);
842 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
843 per_cpu_ptr(lport->dev_stats,
844 get_cpu())->LinkFailureCount++;
845 put_cpu();
846 fcoe_clean_pending_queue(lport);
848 init_waitqueue_head(&hba->shutdown_wait);
849 BNX2FC_HBA_DBG(lport, "indicate_netevent "
850 "num_ofld_sess = %d\n",
851 hba->num_ofld_sess);
852 hba->wait_for_link_down = 1;
853 BNX2FC_HBA_DBG(lport, "waiting for uploads to "
854 "compl proc = %s\n",
855 current->comm);
856 wait_event_interruptible(hba->shutdown_wait,
857 (hba->num_ofld_sess == 0));
858 BNX2FC_HBA_DBG(lport, "wakeup - num_ofld_sess = %d\n",
859 hba->num_ofld_sess);
860 hba->wait_for_link_down = 0;
862 if (signal_pending(current))
863 flush_signals(current);
868 static int bnx2fc_libfc_config(struct fc_lport *lport)
871 /* Set the function pointers set by bnx2fc driver */
872 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
873 sizeof(struct libfc_function_template));
874 fc_elsct_init(lport);
875 fc_exch_init(lport);
876 fc_rport_init(lport);
877 fc_disc_init(lport);
878 return 0;
881 static int bnx2fc_em_config(struct fc_lport *lport)
883 struct fcoe_port *port = lport_priv(lport);
884 struct bnx2fc_hba *hba = port->priv;
886 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
887 FCOE_MAX_XID, NULL)) {
888 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
889 return -ENOMEM;
892 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
893 BNX2FC_MAX_XID);
895 if (!hba->cmd_mgr) {
896 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
897 fc_exch_mgr_free(lport);
898 return -ENOMEM;
900 return 0;
903 static int bnx2fc_lport_config(struct fc_lport *lport)
905 lport->link_up = 0;
906 lport->qfull = 0;
907 lport->max_retry_count = 3;
908 lport->max_rport_retry_count = 3;
909 lport->e_d_tov = 2 * 1000;
910 lport->r_a_tov = 10 * 1000;
912 /* REVISIT: enable when supporting tape devices
913 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
914 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
916 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS);
917 lport->does_npiv = 1;
919 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
920 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
922 /* alloc stats structure */
923 if (fc_lport_init_stats(lport))
924 return -ENOMEM;
926 /* Finish fc_lport configuration */
927 fc_lport_config(lport);
929 return 0;
933 * bnx2fc_fip_recv - handle a received FIP frame.
935 * @skb: the received skb
936 * @dev: associated &net_device
937 * @ptype: the &packet_type structure which was used to register this handler.
938 * @orig_dev: original receive &net_device, in case @ dev is a bond.
940 * Returns: 0 for success
942 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
943 struct packet_type *ptype,
944 struct net_device *orig_dev)
946 struct bnx2fc_hba *hba;
947 hba = container_of(ptype, struct bnx2fc_hba, fip_packet_type);
948 fcoe_ctlr_recv(&hba->ctlr, skb);
949 return 0;
953 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
955 * @fip: FCoE controller.
956 * @old: Unicast MAC address to delete if the MAC is non-zero.
957 * @new: Unicast MAC address to add.
959 * Remove any previously-set unicast MAC filter.
960 * Add secondary FCoE MAC address filter for our OUI.
962 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
964 struct fcoe_port *port = lport_priv(lport);
966 memcpy(port->data_src_addr, addr, ETH_ALEN);
970 * bnx2fc_get_src_mac - return the ethernet source address for an lport
972 * @lport: libfc port
974 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
976 struct fcoe_port *port;
978 port = (struct fcoe_port *)lport_priv(lport);
979 return port->data_src_addr;
983 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
985 * @fip: FCoE controller.
986 * @skb: FIP Packet.
988 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
990 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
991 dev_queue_xmit(skb);
994 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
996 struct Scsi_Host *shost = vport_to_shost(vport);
997 struct fc_lport *n_port = shost_priv(shost);
998 struct fcoe_port *port = lport_priv(n_port);
999 struct bnx2fc_hba *hba = port->priv;
1000 struct net_device *netdev = hba->netdev;
1001 struct fc_lport *vn_port;
1003 if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1004 printk(KERN_ERR PFX "vn ports cannot be created on"
1005 "this hba\n");
1006 return -EIO;
1008 mutex_lock(&bnx2fc_dev_lock);
1009 vn_port = bnx2fc_if_create(hba, &vport->dev, 1);
1010 mutex_unlock(&bnx2fc_dev_lock);
1012 if (IS_ERR(vn_port)) {
1013 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1014 netdev->name);
1015 return -EIO;
1018 if (disabled) {
1019 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1020 } else {
1021 vn_port->boot_time = jiffies;
1022 fc_lport_init(vn_port);
1023 fc_fabric_login(vn_port);
1024 fc_vport_setlink(vn_port);
1026 return 0;
1029 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1031 struct Scsi_Host *shost = vport_to_shost(vport);
1032 struct fc_lport *n_port = shost_priv(shost);
1033 struct fc_lport *vn_port = vport->dd_data;
1034 struct fcoe_port *port = lport_priv(vn_port);
1036 mutex_lock(&n_port->lp_mutex);
1037 list_del(&vn_port->list);
1038 mutex_unlock(&n_port->lp_mutex);
1039 queue_work(bnx2fc_wq, &port->destroy_work);
1040 return 0;
1043 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1045 struct fc_lport *lport = vport->dd_data;
1047 if (disable) {
1048 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1049 fc_fabric_logoff(lport);
1050 } else {
1051 lport->boot_time = jiffies;
1052 fc_fabric_login(lport);
1053 fc_vport_setlink(lport);
1055 return 0;
1059 static int bnx2fc_netdev_setup(struct bnx2fc_hba *hba)
1061 struct net_device *netdev = hba->netdev;
1062 struct net_device *physdev = hba->phys_dev;
1063 struct netdev_hw_addr *ha;
1064 int sel_san_mac = 0;
1066 /* setup Source MAC Address */
1067 rcu_read_lock();
1068 for_each_dev_addr(physdev, ha) {
1069 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1070 ha->type);
1071 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1072 ha->addr[1], ha->addr[2], ha->addr[3],
1073 ha->addr[4], ha->addr[5]);
1075 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1076 (is_valid_ether_addr(ha->addr))) {
1077 memcpy(hba->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
1078 sel_san_mac = 1;
1079 BNX2FC_MISC_DBG("Found SAN MAC\n");
1082 rcu_read_unlock();
1084 if (!sel_san_mac)
1085 return -ENODEV;
1087 hba->fip_packet_type.func = bnx2fc_fip_recv;
1088 hba->fip_packet_type.type = htons(ETH_P_FIP);
1089 hba->fip_packet_type.dev = netdev;
1090 dev_add_pack(&hba->fip_packet_type);
1092 hba->fcoe_packet_type.func = bnx2fc_rcv;
1093 hba->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1094 hba->fcoe_packet_type.dev = netdev;
1095 dev_add_pack(&hba->fcoe_packet_type);
1097 return 0;
1100 static int bnx2fc_attach_transport(void)
1102 bnx2fc_transport_template =
1103 fc_attach_transport(&bnx2fc_transport_function);
1105 if (bnx2fc_transport_template == NULL) {
1106 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1107 return -ENODEV;
1110 bnx2fc_vport_xport_template =
1111 fc_attach_transport(&bnx2fc_vport_xport_function);
1112 if (bnx2fc_vport_xport_template == NULL) {
1113 printk(KERN_ERR PFX
1114 "Failed to attach FC transport for vport\n");
1115 fc_release_transport(bnx2fc_transport_template);
1116 bnx2fc_transport_template = NULL;
1117 return -ENODEV;
1119 return 0;
1121 static void bnx2fc_release_transport(void)
1123 fc_release_transport(bnx2fc_transport_template);
1124 fc_release_transport(bnx2fc_vport_xport_template);
1125 bnx2fc_transport_template = NULL;
1126 bnx2fc_vport_xport_template = NULL;
1129 static void bnx2fc_interface_release(struct kref *kref)
1131 struct bnx2fc_hba *hba;
1132 struct net_device *netdev;
1133 struct net_device *phys_dev;
1135 hba = container_of(kref, struct bnx2fc_hba, kref);
1136 BNX2FC_MISC_DBG("Interface is being released\n");
1138 netdev = hba->netdev;
1139 phys_dev = hba->phys_dev;
1141 /* tear-down FIP controller */
1142 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done))
1143 fcoe_ctlr_destroy(&hba->ctlr);
1145 /* Free the command manager */
1146 if (hba->cmd_mgr) {
1147 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1148 hba->cmd_mgr = NULL;
1150 dev_put(netdev);
1151 module_put(THIS_MODULE);
1154 static inline void bnx2fc_interface_get(struct bnx2fc_hba *hba)
1156 kref_get(&hba->kref);
1159 static inline void bnx2fc_interface_put(struct bnx2fc_hba *hba)
1161 kref_put(&hba->kref, bnx2fc_interface_release);
1163 static void bnx2fc_interface_destroy(struct bnx2fc_hba *hba)
1165 bnx2fc_unbind_pcidev(hba);
1166 kfree(hba);
1170 * bnx2fc_interface_create - create a new fcoe instance
1172 * @cnic: pointer to cnic device
1174 * Creates a new FCoE instance on the given device which include allocating
1175 * hba structure, scsi_host and lport structures.
1177 static struct bnx2fc_hba *bnx2fc_interface_create(struct cnic_dev *cnic)
1179 struct bnx2fc_hba *hba;
1180 int rc;
1182 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1183 if (!hba) {
1184 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1185 return NULL;
1187 spin_lock_init(&hba->hba_lock);
1188 mutex_init(&hba->hba_mutex);
1190 hba->cnic = cnic;
1191 rc = bnx2fc_bind_pcidev(hba);
1192 if (rc)
1193 goto bind_err;
1194 hba->phys_dev = cnic->netdev;
1195 /* will get overwritten after we do vlan discovery */
1196 hba->netdev = hba->phys_dev;
1198 init_waitqueue_head(&hba->shutdown_wait);
1199 init_waitqueue_head(&hba->destroy_wait);
1201 return hba;
1202 bind_err:
1203 printk(KERN_ERR PFX "create_interface: bind error\n");
1204 kfree(hba);
1205 return NULL;
1208 static int bnx2fc_interface_setup(struct bnx2fc_hba *hba,
1209 enum fip_state fip_mode)
1211 int rc = 0;
1212 struct net_device *netdev = hba->netdev;
1213 struct fcoe_ctlr *fip = &hba->ctlr;
1215 dev_hold(netdev);
1216 kref_init(&hba->kref);
1218 hba->flags = 0;
1220 /* Initialize FIP */
1221 memset(fip, 0, sizeof(*fip));
1222 fcoe_ctlr_init(fip, fip_mode);
1223 hba->ctlr.send = bnx2fc_fip_send;
1224 hba->ctlr.update_mac = bnx2fc_update_src_mac;
1225 hba->ctlr.get_src_addr = bnx2fc_get_src_mac;
1226 set_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done);
1228 rc = bnx2fc_netdev_setup(hba);
1229 if (rc)
1230 goto setup_err;
1232 hba->next_conn_id = 0;
1234 memset(hba->tgt_ofld_list, 0, sizeof(hba->tgt_ofld_list));
1235 hba->num_ofld_sess = 0;
1237 return 0;
1239 setup_err:
1240 fcoe_ctlr_destroy(&hba->ctlr);
1241 dev_put(netdev);
1242 bnx2fc_interface_put(hba);
1243 return rc;
1247 * bnx2fc_if_create - Create FCoE instance on a given interface
1249 * @hba: FCoE interface to create a local port on
1250 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1251 * @npiv: Indicates if the port is vport or not
1253 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1255 * Returns: Allocated fc_lport or an error pointer
1257 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
1258 struct device *parent, int npiv)
1260 struct fc_lport *lport, *n_port;
1261 struct fcoe_port *port;
1262 struct Scsi_Host *shost;
1263 struct fc_vport *vport = dev_to_vport(parent);
1264 int rc = 0;
1266 /* Allocate Scsi_Host structure */
1267 if (!npiv)
1268 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1269 else
1270 lport = libfc_vport_create(vport, sizeof(*port));
1272 if (!lport) {
1273 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1274 return NULL;
1276 shost = lport->host;
1277 port = lport_priv(lport);
1278 port->lport = lport;
1279 port->priv = hba;
1280 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1282 /* Configure fcoe_port */
1283 rc = bnx2fc_lport_config(lport);
1284 if (rc)
1285 goto lp_config_err;
1287 if (npiv) {
1288 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1289 vport->node_name, vport->port_name);
1290 fc_set_wwnn(lport, vport->node_name);
1291 fc_set_wwpn(lport, vport->port_name);
1293 /* Configure netdev and networking properties of the lport */
1294 rc = bnx2fc_net_config(lport);
1295 if (rc) {
1296 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1297 goto lp_config_err;
1300 rc = bnx2fc_shost_config(lport, parent);
1301 if (rc) {
1302 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1303 hba->netdev->name);
1304 goto lp_config_err;
1307 /* Initialize the libfc library */
1308 rc = bnx2fc_libfc_config(lport);
1309 if (rc) {
1310 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1311 goto shost_err;
1313 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1315 /* Allocate exchange manager */
1316 if (!npiv)
1317 rc = bnx2fc_em_config(lport);
1318 else {
1319 shost = vport_to_shost(vport);
1320 n_port = shost_priv(shost);
1321 rc = fc_exch_mgr_list_clone(n_port, lport);
1324 if (rc) {
1325 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1326 goto shost_err;
1329 bnx2fc_interface_get(hba);
1330 return lport;
1332 shost_err:
1333 scsi_remove_host(shost);
1334 lp_config_err:
1335 scsi_host_put(lport->host);
1336 return NULL;
1339 static void bnx2fc_netdev_cleanup(struct bnx2fc_hba *hba)
1341 /* Dont listen for Ethernet packets anymore */
1342 __dev_remove_pack(&hba->fcoe_packet_type);
1343 __dev_remove_pack(&hba->fip_packet_type);
1344 synchronize_net();
1347 static void bnx2fc_if_destroy(struct fc_lport *lport)
1349 struct fcoe_port *port = lport_priv(lport);
1350 struct bnx2fc_hba *hba = port->priv;
1352 BNX2FC_HBA_DBG(hba->ctlr.lp, "ENTERED bnx2fc_if_destroy\n");
1353 /* Stop the transmit retry timer */
1354 del_timer_sync(&port->timer);
1356 /* Free existing transmit skbs */
1357 fcoe_clean_pending_queue(lport);
1359 /* Free queued packets for the receive thread */
1360 bnx2fc_clean_rx_queue(lport);
1362 /* Detach from scsi-ml */
1363 fc_remove_host(lport->host);
1364 scsi_remove_host(lport->host);
1367 * Note that only the physical lport will have the exchange manager.
1368 * for vports, this function is NOP
1370 fc_exch_mgr_free(lport);
1372 /* Free memory used by statistical counters */
1373 fc_lport_free_stats(lport);
1375 /* Release Scsi_Host */
1376 scsi_host_put(lport->host);
1378 bnx2fc_interface_put(hba);
1382 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1384 * @buffer: The name of the Ethernet interface to be destroyed
1385 * @kp: The associated kernel parameter
1387 * Called from sysfs.
1389 * Returns: 0 for success
1391 static int bnx2fc_destroy(struct net_device *netdev)
1393 struct bnx2fc_hba *hba = NULL;
1394 struct net_device *phys_dev;
1395 int rc = 0;
1397 rtnl_lock();
1399 mutex_lock(&bnx2fc_dev_lock);
1400 /* obtain physical netdev */
1401 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1402 phys_dev = vlan_dev_real_dev(netdev);
1403 else {
1404 printk(KERN_ERR PFX "Not a vlan device\n");
1405 rc = -ENODEV;
1406 goto netdev_err;
1409 hba = bnx2fc_hba_lookup(phys_dev);
1410 if (!hba || !hba->ctlr.lp) {
1411 rc = -ENODEV;
1412 printk(KERN_ERR PFX "bnx2fc_destroy: hba or lport not found\n");
1413 goto netdev_err;
1416 if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1417 printk(KERN_ERR PFX "bnx2fc_destroy: Create not called\n");
1418 goto netdev_err;
1421 bnx2fc_netdev_cleanup(hba);
1423 bnx2fc_stop(hba);
1425 bnx2fc_if_destroy(hba->ctlr.lp);
1427 destroy_workqueue(hba->timer_work_queue);
1429 if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
1430 bnx2fc_fw_destroy(hba);
1432 clear_bit(BNX2FC_CREATE_DONE, &hba->init_done);
1433 netdev_err:
1434 mutex_unlock(&bnx2fc_dev_lock);
1435 rtnl_unlock();
1436 return rc;
1439 static void bnx2fc_destroy_work(struct work_struct *work)
1441 struct fcoe_port *port;
1442 struct fc_lport *lport;
1444 port = container_of(work, struct fcoe_port, destroy_work);
1445 lport = port->lport;
1447 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1449 bnx2fc_port_shutdown(lport);
1450 rtnl_lock();
1451 mutex_lock(&bnx2fc_dev_lock);
1452 bnx2fc_if_destroy(lport);
1453 mutex_unlock(&bnx2fc_dev_lock);
1454 rtnl_unlock();
1457 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1459 bnx2fc_free_fw_resc(hba);
1460 bnx2fc_free_task_ctx(hba);
1464 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1465 * pci structure
1467 * @hba: Adapter instance
1469 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1471 if (bnx2fc_setup_task_ctx(hba))
1472 goto mem_err;
1474 if (bnx2fc_setup_fw_resc(hba))
1475 goto mem_err;
1477 return 0;
1478 mem_err:
1479 bnx2fc_unbind_adapter_devices(hba);
1480 return -ENOMEM;
1483 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1485 struct cnic_dev *cnic;
1487 if (!hba->cnic) {
1488 printk(KERN_ERR PFX "cnic is NULL\n");
1489 return -ENODEV;
1491 cnic = hba->cnic;
1492 hba->pcidev = cnic->pcidev;
1493 if (hba->pcidev)
1494 pci_dev_get(hba->pcidev);
1496 return 0;
1499 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1501 if (hba->pcidev)
1502 pci_dev_put(hba->pcidev);
1503 hba->pcidev = NULL;
1509 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1511 * @handle: transport handle pointing to adapter struture
1513 * This function maps adapter structure to pcidev structure and initiates
1514 * firmware handshake to enable/initialize on-chip FCoE components.
1515 * This bnx2fc - cnic interface api callback is used after following
1516 * conditions are met -
1517 * a) underlying network interface is up (marked by event NETDEV_UP
1518 * from netdev
1519 * b) bnx2fc adatper structure is registered.
1521 static void bnx2fc_ulp_start(void *handle)
1523 struct bnx2fc_hba *hba = handle;
1524 struct fc_lport *lport = hba->ctlr.lp;
1526 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1527 mutex_lock(&bnx2fc_dev_lock);
1529 if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
1530 goto start_disc;
1532 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done))
1533 bnx2fc_fw_init(hba);
1535 start_disc:
1536 mutex_unlock(&bnx2fc_dev_lock);
1538 BNX2FC_MISC_DBG("bnx2fc started.\n");
1540 /* Kick off Fabric discovery*/
1541 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1542 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1543 lport->tt.frame_send = bnx2fc_xmit;
1544 bnx2fc_start_disc(hba);
1548 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1550 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1551 fc_fabric_logoff(lport);
1552 fc_lport_destroy(lport);
1555 static void bnx2fc_stop(struct bnx2fc_hba *hba)
1557 struct fc_lport *lport;
1558 struct fc_lport *vport;
1560 BNX2FC_MISC_DBG("ENTERED %s - init_done = %ld\n", __func__,
1561 hba->init_done);
1562 if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done) &&
1563 test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1564 lport = hba->ctlr.lp;
1565 bnx2fc_port_shutdown(lport);
1566 BNX2FC_HBA_DBG(lport, "bnx2fc_stop: waiting for %d "
1567 "offloaded sessions\n",
1568 hba->num_ofld_sess);
1569 wait_event_interruptible(hba->shutdown_wait,
1570 (hba->num_ofld_sess == 0));
1571 mutex_lock(&lport->lp_mutex);
1572 list_for_each_entry(vport, &lport->vports, list)
1573 fc_host_port_type(vport->host) = FC_PORTTYPE_UNKNOWN;
1574 mutex_unlock(&lport->lp_mutex);
1575 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1576 fcoe_ctlr_link_down(&hba->ctlr);
1577 fcoe_clean_pending_queue(lport);
1579 mutex_lock(&hba->hba_mutex);
1580 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1581 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
1583 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1584 mutex_unlock(&hba->hba_mutex);
1588 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1590 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1591 int rc = -1;
1592 int i = HZ;
1594 rc = bnx2fc_bind_adapter_devices(hba);
1595 if (rc) {
1596 printk(KERN_ALERT PFX
1597 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1598 goto err_out;
1601 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1602 if (rc) {
1603 printk(KERN_ALERT PFX
1604 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1605 goto err_unbind;
1609 * Wait until the adapter init message is complete, and adapter
1610 * state is UP.
1612 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1613 msleep(BNX2FC_INIT_POLL_TIME);
1615 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1616 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1617 "Ignoring...\n",
1618 hba->cnic->netdev->name);
1619 rc = -1;
1620 goto err_unbind;
1624 /* Mark HBA to indicate that the FW INIT is done */
1625 set_bit(BNX2FC_FW_INIT_DONE, &hba->init_done);
1626 return 0;
1628 err_unbind:
1629 bnx2fc_unbind_adapter_devices(hba);
1630 err_out:
1631 return rc;
1634 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1636 if (test_and_clear_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1637 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1638 init_timer(&hba->destroy_timer);
1639 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1640 jiffies;
1641 hba->destroy_timer.function = bnx2fc_destroy_timer;
1642 hba->destroy_timer.data = (unsigned long)hba;
1643 add_timer(&hba->destroy_timer);
1644 wait_event_interruptible(hba->destroy_wait,
1645 (hba->flags &
1646 BNX2FC_FLAG_DESTROY_CMPL));
1647 /* This should never happen */
1648 if (signal_pending(current))
1649 flush_signals(current);
1651 del_timer_sync(&hba->destroy_timer);
1653 bnx2fc_unbind_adapter_devices(hba);
1658 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1660 * @handle: transport handle pointing to adapter structure
1662 * Driver checks if adapter is already in shutdown mode, if not start
1663 * the shutdown process.
1665 static void bnx2fc_ulp_stop(void *handle)
1667 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)handle;
1669 printk(KERN_ERR "ULP_STOP\n");
1671 mutex_lock(&bnx2fc_dev_lock);
1672 bnx2fc_stop(hba);
1673 bnx2fc_fw_destroy(hba);
1674 mutex_unlock(&bnx2fc_dev_lock);
1677 static void bnx2fc_start_disc(struct bnx2fc_hba *hba)
1679 struct fc_lport *lport;
1680 int wait_cnt = 0;
1682 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1683 /* Kick off FIP/FLOGI */
1684 if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1685 printk(KERN_ERR PFX "Init not done yet\n");
1686 return;
1689 lport = hba->ctlr.lp;
1690 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1692 if (!bnx2fc_link_ok(lport)) {
1693 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1694 fcoe_ctlr_link_up(&hba->ctlr);
1695 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1696 set_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1699 /* wait for the FCF to be selected before issuing FLOGI */
1700 while (!hba->ctlr.sel_fcf) {
1701 msleep(250);
1702 /* give up after 3 secs */
1703 if (++wait_cnt > 12)
1704 break;
1706 fc_lport_init(lport);
1707 fc_fabric_login(lport);
1712 * bnx2fc_ulp_init - Initialize an adapter instance
1714 * @dev : cnic device handle
1715 * Called from cnic_register_driver() context to initialize all
1716 * enumerated cnic devices. This routine allocates adapter structure
1717 * and other device specific resources.
1719 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1721 struct bnx2fc_hba *hba;
1722 int rc = 0;
1724 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1725 /* bnx2fc works only when bnx2x is loaded */
1726 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1727 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1728 " flags: %lx\n",
1729 dev->netdev->name, dev->flags);
1730 return;
1733 /* Configure FCoE interface */
1734 hba = bnx2fc_interface_create(dev);
1735 if (!hba) {
1736 printk(KERN_ERR PFX "hba initialization failed\n");
1737 return;
1740 /* Add HBA to the adapter list */
1741 mutex_lock(&bnx2fc_dev_lock);
1742 list_add_tail(&hba->link, &adapter_list);
1743 adapter_count++;
1744 mutex_unlock(&bnx2fc_dev_lock);
1746 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1747 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1748 (void *) hba);
1749 if (rc)
1750 printk(KERN_ALERT PFX "register_device failed, rc = %d\n", rc);
1751 else
1752 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1756 static int bnx2fc_disable(struct net_device *netdev)
1758 struct bnx2fc_hba *hba;
1759 struct net_device *phys_dev;
1760 struct ethtool_drvinfo drvinfo;
1761 int rc = 0;
1763 rtnl_lock();
1765 mutex_lock(&bnx2fc_dev_lock);
1767 /* obtain physical netdev */
1768 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1769 phys_dev = vlan_dev_real_dev(netdev);
1770 else {
1771 printk(KERN_ERR PFX "Not a vlan device\n");
1772 rc = -ENODEV;
1773 goto nodev;
1776 /* verify if the physical device is a netxtreme2 device */
1777 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1778 memset(&drvinfo, 0, sizeof(drvinfo));
1779 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1780 if (strcmp(drvinfo.driver, "bnx2x")) {
1781 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1782 rc = -ENODEV;
1783 goto nodev;
1785 } else {
1786 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1787 rc = -ENODEV;
1788 goto nodev;
1791 printk(KERN_ERR PFX "phys_dev is netxtreme2 device\n");
1793 /* obtain hba and initialize rest of the structure */
1794 hba = bnx2fc_hba_lookup(phys_dev);
1795 if (!hba || !hba->ctlr.lp) {
1796 rc = -ENODEV;
1797 printk(KERN_ERR PFX "bnx2fc_disable: hba or lport not found\n");
1798 } else {
1799 fcoe_ctlr_link_down(&hba->ctlr);
1800 fcoe_clean_pending_queue(hba->ctlr.lp);
1803 nodev:
1804 mutex_unlock(&bnx2fc_dev_lock);
1805 rtnl_unlock();
1806 return rc;
1810 static int bnx2fc_enable(struct net_device *netdev)
1812 struct bnx2fc_hba *hba;
1813 struct net_device *phys_dev;
1814 struct ethtool_drvinfo drvinfo;
1815 int rc = 0;
1817 rtnl_lock();
1819 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1820 mutex_lock(&bnx2fc_dev_lock);
1822 /* obtain physical netdev */
1823 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1824 phys_dev = vlan_dev_real_dev(netdev);
1825 else {
1826 printk(KERN_ERR PFX "Not a vlan device\n");
1827 rc = -ENODEV;
1828 goto nodev;
1830 /* verify if the physical device is a netxtreme2 device */
1831 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1832 memset(&drvinfo, 0, sizeof(drvinfo));
1833 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1834 if (strcmp(drvinfo.driver, "bnx2x")) {
1835 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1836 rc = -ENODEV;
1837 goto nodev;
1839 } else {
1840 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1841 rc = -ENODEV;
1842 goto nodev;
1845 /* obtain hba and initialize rest of the structure */
1846 hba = bnx2fc_hba_lookup(phys_dev);
1847 if (!hba || !hba->ctlr.lp) {
1848 rc = -ENODEV;
1849 printk(KERN_ERR PFX "bnx2fc_enable: hba or lport not found\n");
1850 } else if (!bnx2fc_link_ok(hba->ctlr.lp))
1851 fcoe_ctlr_link_up(&hba->ctlr);
1853 nodev:
1854 mutex_unlock(&bnx2fc_dev_lock);
1855 rtnl_unlock();
1856 return rc;
1860 * bnx2fc_create - Create bnx2fc FCoE interface
1862 * @buffer: The name of Ethernet interface to create on
1863 * @kp: The associated kernel param
1865 * Called from sysfs.
1867 * Returns: 0 for success
1869 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1871 struct bnx2fc_hba *hba;
1872 struct net_device *phys_dev;
1873 struct fc_lport *lport;
1874 struct ethtool_drvinfo drvinfo;
1875 int rc = 0;
1876 int vlan_id;
1878 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1879 if (fip_mode != FIP_MODE_FABRIC) {
1880 printk(KERN_ERR "fip mode not FABRIC\n");
1881 return -EIO;
1884 rtnl_lock();
1886 mutex_lock(&bnx2fc_dev_lock);
1888 if (!try_module_get(THIS_MODULE)) {
1889 rc = -EINVAL;
1890 goto mod_err;
1893 /* obtain physical netdev */
1894 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1895 phys_dev = vlan_dev_real_dev(netdev);
1896 vlan_id = vlan_dev_vlan_id(netdev);
1897 } else {
1898 printk(KERN_ERR PFX "Not a vlan device\n");
1899 rc = -EINVAL;
1900 goto netdev_err;
1902 /* verify if the physical device is a netxtreme2 device */
1903 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1904 memset(&drvinfo, 0, sizeof(drvinfo));
1905 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1906 if (strcmp(drvinfo.driver, "bnx2x")) {
1907 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1908 rc = -EINVAL;
1909 goto netdev_err;
1911 } else {
1912 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1913 rc = -EINVAL;
1914 goto netdev_err;
1917 /* obtain hba and initialize rest of the structure */
1918 hba = bnx2fc_hba_lookup(phys_dev);
1919 if (!hba) {
1920 rc = -ENODEV;
1921 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1922 goto netdev_err;
1925 if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1926 rc = bnx2fc_fw_init(hba);
1927 if (rc)
1928 goto netdev_err;
1931 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1932 rc = -EEXIST;
1933 goto netdev_err;
1936 /* update netdev with vlan netdev */
1937 hba->netdev = netdev;
1938 hba->vlan_id = vlan_id;
1939 hba->vlan_enabled = 1;
1941 rc = bnx2fc_interface_setup(hba, fip_mode);
1942 if (rc) {
1943 printk(KERN_ERR PFX "bnx2fc_interface_setup failed\n");
1944 goto ifput_err;
1947 hba->timer_work_queue =
1948 create_singlethread_workqueue("bnx2fc_timer_wq");
1949 if (!hba->timer_work_queue) {
1950 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
1951 rc = -EINVAL;
1952 goto ifput_err;
1955 lport = bnx2fc_if_create(hba, &hba->pcidev->dev, 0);
1956 if (!lport) {
1957 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
1958 netdev->name);
1959 bnx2fc_netdev_cleanup(hba);
1960 rc = -EINVAL;
1961 goto if_create_err;
1964 lport->boot_time = jiffies;
1966 /* Make this master N_port */
1967 hba->ctlr.lp = lport;
1969 set_bit(BNX2FC_CREATE_DONE, &hba->init_done);
1970 printk(KERN_ERR PFX "create: START DISC\n");
1971 bnx2fc_start_disc(hba);
1973 * Release from kref_init in bnx2fc_interface_setup, on success
1974 * lport should be holding a reference taken in bnx2fc_if_create
1976 bnx2fc_interface_put(hba);
1977 /* put netdev that was held while calling dev_get_by_name */
1978 mutex_unlock(&bnx2fc_dev_lock);
1979 rtnl_unlock();
1980 return 0;
1982 if_create_err:
1983 destroy_workqueue(hba->timer_work_queue);
1984 ifput_err:
1985 bnx2fc_interface_put(hba);
1986 netdev_err:
1987 module_put(THIS_MODULE);
1988 mod_err:
1989 mutex_unlock(&bnx2fc_dev_lock);
1990 rtnl_unlock();
1991 return rc;
1995 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc adapter instance
1997 * @cnic: Pointer to cnic device instance
2000 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2002 struct list_head *list;
2003 struct list_head *temp;
2004 struct bnx2fc_hba *hba;
2006 /* Called with bnx2fc_dev_lock held */
2007 list_for_each_safe(list, temp, &adapter_list) {
2008 hba = (struct bnx2fc_hba *)list;
2009 if (hba->cnic == cnic)
2010 return hba;
2012 return NULL;
2015 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev)
2017 struct list_head *list;
2018 struct list_head *temp;
2019 struct bnx2fc_hba *hba;
2021 /* Called with bnx2fc_dev_lock held */
2022 list_for_each_safe(list, temp, &adapter_list) {
2023 hba = (struct bnx2fc_hba *)list;
2024 if (hba->phys_dev == phys_dev)
2025 return hba;
2027 printk(KERN_ERR PFX "hba_lookup: hba NULL\n");
2028 return NULL;
2032 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2034 * @dev cnic device handle
2036 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2038 struct bnx2fc_hba *hba;
2040 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2042 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2043 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2044 dev->netdev->name, dev->flags);
2045 return;
2048 mutex_lock(&bnx2fc_dev_lock);
2049 hba = bnx2fc_find_hba_for_cnic(dev);
2050 if (!hba) {
2051 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2052 dev);
2053 mutex_unlock(&bnx2fc_dev_lock);
2054 return;
2057 list_del_init(&hba->link);
2058 adapter_count--;
2060 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
2061 /* destroy not called yet, move to quiesced list */
2062 bnx2fc_netdev_cleanup(hba);
2063 bnx2fc_if_destroy(hba->ctlr.lp);
2065 mutex_unlock(&bnx2fc_dev_lock);
2067 bnx2fc_ulp_stop(hba);
2068 /* unregister cnic device */
2069 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2070 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2071 bnx2fc_interface_destroy(hba);
2075 * bnx2fc_fcoe_reset - Resets the fcoe
2077 * @shost: shost the reset is from
2079 * Returns: always 0
2081 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2083 struct fc_lport *lport = shost_priv(shost);
2084 fc_lport_reset(lport);
2085 return 0;
2089 static bool bnx2fc_match(struct net_device *netdev)
2091 mutex_lock(&bnx2fc_dev_lock);
2092 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2093 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2095 if (bnx2fc_hba_lookup(phys_dev)) {
2096 mutex_unlock(&bnx2fc_dev_lock);
2097 return true;
2100 mutex_unlock(&bnx2fc_dev_lock);
2101 return false;
2105 static struct fcoe_transport bnx2fc_transport = {
2106 .name = {"bnx2fc"},
2107 .attached = false,
2108 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2109 .match = bnx2fc_match,
2110 .create = bnx2fc_create,
2111 .destroy = bnx2fc_destroy,
2112 .enable = bnx2fc_enable,
2113 .disable = bnx2fc_disable,
2117 * bnx2fc_percpu_thread_create - Create a receive thread for an
2118 * online CPU
2120 * @cpu: cpu index for the online cpu
2122 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2124 struct bnx2fc_percpu_s *p;
2125 struct task_struct *thread;
2127 p = &per_cpu(bnx2fc_percpu, cpu);
2129 thread = kthread_create(bnx2fc_percpu_io_thread,
2130 (void *)p,
2131 "bnx2fc_thread/%d", cpu);
2132 /* bind thread to the cpu */
2133 if (likely(!IS_ERR(p->iothread))) {
2134 kthread_bind(thread, cpu);
2135 p->iothread = thread;
2136 wake_up_process(thread);
2140 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2142 struct bnx2fc_percpu_s *p;
2143 struct task_struct *thread;
2144 struct bnx2fc_work *work, *tmp;
2145 LIST_HEAD(work_list);
2147 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2149 /* Prevent any new work from being queued for this CPU */
2150 p = &per_cpu(bnx2fc_percpu, cpu);
2151 spin_lock_bh(&p->fp_work_lock);
2152 thread = p->iothread;
2153 p->iothread = NULL;
2156 /* Free all work in the list */
2157 list_for_each_entry_safe(work, tmp, &work_list, list) {
2158 list_del_init(&work->list);
2159 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2160 kfree(work);
2163 spin_unlock_bh(&p->fp_work_lock);
2165 if (thread)
2166 kthread_stop(thread);
2170 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2172 * @nfb: The callback data block
2173 * @action: The event triggering the callback
2174 * @hcpu: The index of the CPU that the event is for
2176 * This creates or destroys per-CPU data for fcoe
2178 * Returns NOTIFY_OK always.
2180 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2181 unsigned long action, void *hcpu)
2183 unsigned cpu = (unsigned long)hcpu;
2185 switch (action) {
2186 case CPU_ONLINE:
2187 case CPU_ONLINE_FROZEN:
2188 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2189 bnx2fc_percpu_thread_create(cpu);
2190 break;
2191 case CPU_DEAD:
2192 case CPU_DEAD_FROZEN:
2193 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2194 bnx2fc_percpu_thread_destroy(cpu);
2195 break;
2196 default:
2197 break;
2199 return NOTIFY_OK;
2203 * bnx2fc_mod_init - module init entry point
2205 * Initialize driver wide global data structures, and register
2206 * with cnic module
2208 static int __init bnx2fc_mod_init(void)
2210 struct fcoe_percpu_s *bg;
2211 struct task_struct *l2_thread;
2212 int rc = 0;
2213 unsigned int cpu = 0;
2214 struct bnx2fc_percpu_s *p;
2216 printk(KERN_INFO PFX "%s", version);
2218 /* register as a fcoe transport */
2219 rc = fcoe_transport_attach(&bnx2fc_transport);
2220 if (rc) {
2221 printk(KERN_ERR "failed to register an fcoe transport, check "
2222 "if libfcoe is loaded\n");
2223 goto out;
2226 INIT_LIST_HEAD(&adapter_list);
2227 mutex_init(&bnx2fc_dev_lock);
2228 adapter_count = 0;
2230 /* Attach FC transport template */
2231 rc = bnx2fc_attach_transport();
2232 if (rc)
2233 goto detach_ft;
2235 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2236 if (!bnx2fc_wq) {
2237 rc = -ENOMEM;
2238 goto release_bt;
2241 bg = &bnx2fc_global;
2242 skb_queue_head_init(&bg->fcoe_rx_list);
2243 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2244 (void *)bg,
2245 "bnx2fc_l2_thread");
2246 if (IS_ERR(l2_thread)) {
2247 rc = PTR_ERR(l2_thread);
2248 goto free_wq;
2250 wake_up_process(l2_thread);
2251 spin_lock_bh(&bg->fcoe_rx_list.lock);
2252 bg->thread = l2_thread;
2253 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2255 for_each_possible_cpu(cpu) {
2256 p = &per_cpu(bnx2fc_percpu, cpu);
2257 INIT_LIST_HEAD(&p->work_list);
2258 spin_lock_init(&p->fp_work_lock);
2261 for_each_online_cpu(cpu) {
2262 bnx2fc_percpu_thread_create(cpu);
2265 /* Initialize per CPU interrupt thread */
2266 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2268 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2270 return 0;
2272 free_wq:
2273 destroy_workqueue(bnx2fc_wq);
2274 release_bt:
2275 bnx2fc_release_transport();
2276 detach_ft:
2277 fcoe_transport_detach(&bnx2fc_transport);
2278 out:
2279 return rc;
2282 static void __exit bnx2fc_mod_exit(void)
2284 LIST_HEAD(to_be_deleted);
2285 struct bnx2fc_hba *hba, *next;
2286 struct fcoe_percpu_s *bg;
2287 struct task_struct *l2_thread;
2288 struct sk_buff *skb;
2289 unsigned int cpu = 0;
2292 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2293 * it will have higher precedence than bnx2fc_dev_lock.
2294 * unregister_device() cannot be called with bnx2fc_dev_lock
2295 * held.
2297 mutex_lock(&bnx2fc_dev_lock);
2298 list_splice(&adapter_list, &to_be_deleted);
2299 INIT_LIST_HEAD(&adapter_list);
2300 adapter_count = 0;
2301 mutex_unlock(&bnx2fc_dev_lock);
2303 /* Unregister with cnic */
2304 list_for_each_entry_safe(hba, next, &to_be_deleted, link) {
2305 list_del_init(&hba->link);
2306 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p, kref = %d\n",
2307 hba, atomic_read(&hba->kref.refcount));
2308 bnx2fc_ulp_stop(hba);
2309 /* unregister cnic device */
2310 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2311 &hba->reg_with_cnic))
2312 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2313 bnx2fc_interface_destroy(hba);
2315 cnic_unregister_driver(CNIC_ULP_FCOE);
2317 /* Destroy global thread */
2318 bg = &bnx2fc_global;
2319 spin_lock_bh(&bg->fcoe_rx_list.lock);
2320 l2_thread = bg->thread;
2321 bg->thread = NULL;
2322 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2323 kfree_skb(skb);
2325 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2327 if (l2_thread)
2328 kthread_stop(l2_thread);
2330 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2332 /* Destroy per cpu threads */
2333 for_each_online_cpu(cpu) {
2334 bnx2fc_percpu_thread_destroy(cpu);
2337 destroy_workqueue(bnx2fc_wq);
2339 * detach from scsi transport
2340 * must happen after all destroys are done
2342 bnx2fc_release_transport();
2344 /* detach from fcoe transport */
2345 fcoe_transport_detach(&bnx2fc_transport);
2348 module_init(bnx2fc_mod_init);
2349 module_exit(bnx2fc_mod_exit);
2351 static struct fc_function_template bnx2fc_transport_function = {
2352 .show_host_node_name = 1,
2353 .show_host_port_name = 1,
2354 .show_host_supported_classes = 1,
2355 .show_host_supported_fc4s = 1,
2356 .show_host_active_fc4s = 1,
2357 .show_host_maxframe_size = 1,
2359 .show_host_port_id = 1,
2360 .show_host_supported_speeds = 1,
2361 .get_host_speed = fc_get_host_speed,
2362 .show_host_speed = 1,
2363 .show_host_port_type = 1,
2364 .get_host_port_state = fc_get_host_port_state,
2365 .show_host_port_state = 1,
2366 .show_host_symbolic_name = 1,
2368 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2369 sizeof(struct bnx2fc_rport)),
2370 .show_rport_maxframe_size = 1,
2371 .show_rport_supported_classes = 1,
2373 .show_host_fabric_name = 1,
2374 .show_starget_node_name = 1,
2375 .show_starget_port_name = 1,
2376 .show_starget_port_id = 1,
2377 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2378 .show_rport_dev_loss_tmo = 1,
2379 .get_fc_host_stats = bnx2fc_get_host_stats,
2381 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2383 .terminate_rport_io = fc_rport_terminate_io,
2385 .vport_create = bnx2fc_vport_create,
2386 .vport_delete = bnx2fc_vport_destroy,
2387 .vport_disable = bnx2fc_vport_disable,
2390 static struct fc_function_template bnx2fc_vport_xport_function = {
2391 .show_host_node_name = 1,
2392 .show_host_port_name = 1,
2393 .show_host_supported_classes = 1,
2394 .show_host_supported_fc4s = 1,
2395 .show_host_active_fc4s = 1,
2396 .show_host_maxframe_size = 1,
2398 .show_host_port_id = 1,
2399 .show_host_supported_speeds = 1,
2400 .get_host_speed = fc_get_host_speed,
2401 .show_host_speed = 1,
2402 .show_host_port_type = 1,
2403 .get_host_port_state = fc_get_host_port_state,
2404 .show_host_port_state = 1,
2405 .show_host_symbolic_name = 1,
2407 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2408 sizeof(struct bnx2fc_rport)),
2409 .show_rport_maxframe_size = 1,
2410 .show_rport_supported_classes = 1,
2412 .show_host_fabric_name = 1,
2413 .show_starget_node_name = 1,
2414 .show_starget_port_name = 1,
2415 .show_starget_port_id = 1,
2416 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2417 .show_rport_dev_loss_tmo = 1,
2418 .get_fc_host_stats = fc_get_host_stats,
2419 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2420 .terminate_rport_io = fc_rport_terminate_io,
2424 * scsi_host_template structure used while registering with SCSI-ml
2426 static struct scsi_host_template bnx2fc_shost_template = {
2427 .module = THIS_MODULE,
2428 .name = "Broadcom Offload FCoE Initiator",
2429 .queuecommand = bnx2fc_queuecommand,
2430 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2431 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2432 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2433 .eh_host_reset_handler = fc_eh_host_reset,
2434 .slave_alloc = fc_slave_alloc,
2435 .change_queue_depth = fc_change_queue_depth,
2436 .change_queue_type = fc_change_queue_type,
2437 .this_id = -1,
2438 .cmd_per_lun = 3,
2439 .can_queue = BNX2FC_CAN_QUEUE,
2440 .use_clustering = ENABLE_CLUSTERING,
2441 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2442 .max_sectors = 512,
2445 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2446 .frame_send = bnx2fc_xmit,
2447 .elsct_send = bnx2fc_elsct_send,
2448 .fcp_abort_io = bnx2fc_abort_io,
2449 .fcp_cleanup = bnx2fc_cleanup,
2450 .rport_event_callback = bnx2fc_rport_event_handler,
2454 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2455 * structure carrying callback function pointers
2457 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2458 .owner = THIS_MODULE,
2459 .cnic_init = bnx2fc_ulp_init,
2460 .cnic_exit = bnx2fc_ulp_exit,
2461 .cnic_start = bnx2fc_ulp_start,
2462 .cnic_stop = bnx2fc_ulp_stop,
2463 .indicate_kcqes = bnx2fc_indicate_kcqe,
2464 .indicate_netevent = bnx2fc_indicate_netevent,