[SCSI] bnx2fc: Obtain WWNN/WWPN from the shared memory
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
blob25d3bcd179b11390f818d019719bbcb83808fbdc
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)
15 #include "bnx2fc.h"
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 "Jun 23, 2011"
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, bool schedule);
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
80 *phys_dev);
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 void bnx2fc_clean_rx_queue(struct fc_lport *lp)
104 struct fcoe_percpu_s *bg;
105 struct fcoe_rcv_info *fr;
106 struct sk_buff_head *list;
107 struct sk_buff *skb, *next;
108 struct sk_buff *head;
110 bg = &bnx2fc_global;
111 spin_lock_bh(&bg->fcoe_rx_list.lock);
112 list = &bg->fcoe_rx_list;
113 head = list->next;
114 for (skb = head; skb != (struct sk_buff *)list;
115 skb = next) {
116 next = skb->next;
117 fr = fcoe_dev_from_skb(skb);
118 if (fr->fr_dev == lp) {
119 __skb_unlink(skb, list);
120 kfree_skb(skb);
123 spin_unlock_bh(&bg->fcoe_rx_list.lock);
126 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
128 int rc;
129 spin_lock(&bnx2fc_global_lock);
130 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
131 spin_unlock(&bnx2fc_global_lock);
133 return rc;
136 static void bnx2fc_abort_io(struct fc_lport *lport)
139 * This function is no-op for bnx2fc, but we do
140 * not want to leave it as NULL either, as libfc
141 * can call the default function which is
142 * fc_fcp_abort_io.
146 static void bnx2fc_cleanup(struct fc_lport *lport)
148 struct fcoe_port *port = lport_priv(lport);
149 struct bnx2fc_interface *interface = port->priv;
150 struct bnx2fc_hba *hba = interface->hba;
151 struct bnx2fc_rport *tgt;
152 int i;
154 BNX2FC_MISC_DBG("Entered %s\n", __func__);
155 mutex_lock(&hba->hba_mutex);
156 spin_lock_bh(&hba->hba_lock);
157 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
158 tgt = hba->tgt_ofld_list[i];
159 if (tgt) {
160 /* Cleanup IOs belonging to requested vport */
161 if (tgt->port == port) {
162 spin_unlock_bh(&hba->hba_lock);
163 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
164 bnx2fc_flush_active_ios(tgt);
165 spin_lock_bh(&hba->hba_lock);
169 spin_unlock_bh(&hba->hba_lock);
170 mutex_unlock(&hba->hba_mutex);
173 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
174 struct fc_frame *fp)
176 struct fc_rport_priv *rdata = tgt->rdata;
177 struct fc_frame_header *fh;
178 int rc = 0;
180 fh = fc_frame_header_get(fp);
181 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
182 "r_ctl = 0x%x\n", rdata->ids.port_id,
183 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
184 if ((fh->fh_type == FC_TYPE_ELS) &&
185 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
187 switch (fc_frame_payload_op(fp)) {
188 case ELS_ADISC:
189 rc = bnx2fc_send_adisc(tgt, fp);
190 break;
191 case ELS_LOGO:
192 rc = bnx2fc_send_logo(tgt, fp);
193 break;
194 case ELS_RLS:
195 rc = bnx2fc_send_rls(tgt, fp);
196 break;
197 default:
198 break;
200 } else if ((fh->fh_type == FC_TYPE_BLS) &&
201 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
202 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
203 else {
204 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
205 "rctl 0x%x thru non-offload path\n",
206 fh->fh_type, fh->fh_r_ctl);
207 return -ENODEV;
209 if (rc)
210 return -ENOMEM;
211 else
212 return 0;
216 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
218 * @lport: the associated local port
219 * @fp: the fc_frame to be transmitted
221 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
223 struct ethhdr *eh;
224 struct fcoe_crc_eof *cp;
225 struct sk_buff *skb;
226 struct fc_frame_header *fh;
227 struct bnx2fc_interface *interface;
228 struct bnx2fc_hba *hba;
229 struct fcoe_port *port;
230 struct fcoe_hdr *hp;
231 struct bnx2fc_rport *tgt;
232 struct fcoe_dev_stats *stats;
233 u8 sof, eof;
234 u32 crc;
235 unsigned int hlen, tlen, elen;
236 int wlen, rc = 0;
238 port = (struct fcoe_port *)lport_priv(lport);
239 interface = port->priv;
240 hba = interface->hba;
242 fh = fc_frame_header_get(fp);
244 skb = fp_skb(fp);
245 if (!lport->link_up) {
246 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
247 kfree_skb(skb);
248 return 0;
251 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
252 if (!interface->ctlr.sel_fcf) {
253 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
254 kfree_skb(skb);
255 return -EINVAL;
257 if (fcoe_ctlr_els_send(&interface->ctlr, lport, skb))
258 return 0;
261 sof = fr_sof(fp);
262 eof = fr_eof(fp);
265 * Snoop the frame header to check if the frame is for
266 * an offloaded session
269 * tgt_ofld_list access is synchronized using
270 * both hba mutex and hba lock. Atleast hba mutex or
271 * hba lock needs to be held for read access.
274 spin_lock_bh(&hba->hba_lock);
275 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
276 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
277 /* This frame is for offloaded session */
278 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
279 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
280 spin_unlock_bh(&hba->hba_lock);
281 rc = bnx2fc_xmit_l2_frame(tgt, fp);
282 if (rc != -ENODEV) {
283 kfree_skb(skb);
284 return rc;
286 } else {
287 spin_unlock_bh(&hba->hba_lock);
290 elen = sizeof(struct ethhdr);
291 hlen = sizeof(struct fcoe_hdr);
292 tlen = sizeof(struct fcoe_crc_eof);
293 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
295 skb->ip_summed = CHECKSUM_NONE;
296 crc = fcoe_fc_crc(fp);
298 /* copy port crc and eof to the skb buff */
299 if (skb_is_nonlinear(skb)) {
300 skb_frag_t *frag;
301 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
302 kfree_skb(skb);
303 return -ENOMEM;
305 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
306 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
307 + frag->page_offset;
308 } else {
309 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
312 memset(cp, 0, sizeof(*cp));
313 cp->fcoe_eof = eof;
314 cp->fcoe_crc32 = cpu_to_le32(~crc);
315 if (skb_is_nonlinear(skb)) {
316 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
317 cp = NULL;
320 /* adjust skb network/transport offsets to match mac/fcoe/port */
321 skb_push(skb, elen + hlen);
322 skb_reset_mac_header(skb);
323 skb_reset_network_header(skb);
324 skb->mac_len = elen;
325 skb->protocol = htons(ETH_P_FCOE);
326 skb->dev = interface->netdev;
328 /* fill up mac and fcoe headers */
329 eh = eth_hdr(skb);
330 eh->h_proto = htons(ETH_P_FCOE);
331 if (interface->ctlr.map_dest)
332 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
333 else
334 /* insert GW address */
335 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
337 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
338 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
339 else
340 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
342 hp = (struct fcoe_hdr *)(eh + 1);
343 memset(hp, 0, sizeof(*hp));
344 if (FC_FCOE_VER)
345 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
346 hp->fcoe_sof = sof;
348 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
349 if (lport->seq_offload && fr_max_payload(fp)) {
350 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
351 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
352 } else {
353 skb_shinfo(skb)->gso_type = 0;
354 skb_shinfo(skb)->gso_size = 0;
357 /*update tx stats */
358 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
359 stats->TxFrames++;
360 stats->TxWords += wlen;
361 put_cpu();
363 /* send down to lld */
364 fr_dev(fp) = lport;
365 if (port->fcoe_pending_queue.qlen)
366 fcoe_check_wait_queue(lport, skb);
367 else if (fcoe_start_io(skb))
368 fcoe_check_wait_queue(lport, skb);
370 return 0;
374 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
376 * @skb: the receive socket buffer
377 * @dev: associated net device
378 * @ptype: context
379 * @olddev: last device
381 * This function receives the packet and builds FC frame and passes it up
383 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
384 struct packet_type *ptype, struct net_device *olddev)
386 struct fc_lport *lport;
387 struct bnx2fc_interface *interface;
388 struct fc_frame_header *fh;
389 struct fcoe_rcv_info *fr;
390 struct fcoe_percpu_s *bg;
391 unsigned short oxid;
393 interface = container_of(ptype, struct bnx2fc_interface,
394 fcoe_packet_type);
395 lport = interface->ctlr.lp;
397 if (unlikely(lport == NULL)) {
398 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
399 goto err;
402 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
403 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
404 goto err;
408 * Check for minimum frame length, and make sure required FCoE
409 * and FC headers are pulled into the linear data area.
411 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
412 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
413 goto err;
415 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
416 fh = (struct fc_frame_header *) skb_transport_header(skb);
418 oxid = ntohs(fh->fh_ox_id);
420 fr = fcoe_dev_from_skb(skb);
421 fr->fr_dev = lport;
423 bg = &bnx2fc_global;
424 spin_lock_bh(&bg->fcoe_rx_list.lock);
426 __skb_queue_tail(&bg->fcoe_rx_list, skb);
427 if (bg->fcoe_rx_list.qlen == 1)
428 wake_up_process(bg->thread);
430 spin_unlock_bh(&bg->fcoe_rx_list.lock);
432 return 0;
433 err:
434 kfree_skb(skb);
435 return -1;
438 static int bnx2fc_l2_rcv_thread(void *arg)
440 struct fcoe_percpu_s *bg = arg;
441 struct sk_buff *skb;
443 set_user_nice(current, -20);
444 set_current_state(TASK_INTERRUPTIBLE);
445 while (!kthread_should_stop()) {
446 schedule();
447 spin_lock_bh(&bg->fcoe_rx_list.lock);
448 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
449 spin_unlock_bh(&bg->fcoe_rx_list.lock);
450 bnx2fc_recv_frame(skb);
451 spin_lock_bh(&bg->fcoe_rx_list.lock);
453 __set_current_state(TASK_INTERRUPTIBLE);
454 spin_unlock_bh(&bg->fcoe_rx_list.lock);
456 __set_current_state(TASK_RUNNING);
457 return 0;
461 static void bnx2fc_recv_frame(struct sk_buff *skb)
463 u32 fr_len;
464 struct fc_lport *lport;
465 struct fcoe_rcv_info *fr;
466 struct fcoe_dev_stats *stats;
467 struct fc_frame_header *fh;
468 struct fcoe_crc_eof crc_eof;
469 struct fc_frame *fp;
470 struct fc_lport *vn_port;
471 struct fcoe_port *port;
472 u8 *mac = NULL;
473 u8 *dest_mac = NULL;
474 struct fcoe_hdr *hp;
476 fr = fcoe_dev_from_skb(skb);
477 lport = fr->fr_dev;
478 if (unlikely(lport == NULL)) {
479 printk(KERN_ERR PFX "Invalid lport struct\n");
480 kfree_skb(skb);
481 return;
484 if (skb_is_nonlinear(skb))
485 skb_linearize(skb);
486 mac = eth_hdr(skb)->h_source;
487 dest_mac = eth_hdr(skb)->h_dest;
489 /* Pull the header */
490 hp = (struct fcoe_hdr *) skb_network_header(skb);
491 fh = (struct fc_frame_header *) skb_transport_header(skb);
492 skb_pull(skb, sizeof(struct fcoe_hdr));
493 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
495 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
496 stats->RxFrames++;
497 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
499 fp = (struct fc_frame *)skb;
500 fc_frame_init(fp);
501 fr_dev(fp) = lport;
502 fr_sof(fp) = hp->fcoe_sof;
503 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
504 put_cpu();
505 kfree_skb(skb);
506 return;
508 fr_eof(fp) = crc_eof.fcoe_eof;
509 fr_crc(fp) = crc_eof.fcoe_crc32;
510 if (pskb_trim(skb, fr_len)) {
511 put_cpu();
512 kfree_skb(skb);
513 return;
516 fh = fc_frame_header_get(fp);
518 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
519 if (vn_port) {
520 port = lport_priv(vn_port);
521 if (compare_ether_addr(port->data_src_addr, dest_mac)
522 != 0) {
523 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
524 put_cpu();
525 kfree_skb(skb);
526 return;
529 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
530 fh->fh_type == FC_TYPE_FCP) {
531 /* Drop FCP data. We dont this in L2 path */
532 put_cpu();
533 kfree_skb(skb);
534 return;
536 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
537 fh->fh_type == FC_TYPE_ELS) {
538 switch (fc_frame_payload_op(fp)) {
539 case ELS_LOGO:
540 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
541 /* drop non-FIP LOGO */
542 put_cpu();
543 kfree_skb(skb);
544 return;
546 break;
550 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
551 /* Drop incoming ABTS */
552 put_cpu();
553 kfree_skb(skb);
554 return;
557 if (le32_to_cpu(fr_crc(fp)) !=
558 ~crc32(~0, skb->data, fr_len)) {
559 if (stats->InvalidCRCCount < 5)
560 printk(KERN_WARNING PFX "dropping frame with "
561 "CRC error\n");
562 stats->InvalidCRCCount++;
563 put_cpu();
564 kfree_skb(skb);
565 return;
567 put_cpu();
568 fc_exch_recv(lport, fp);
572 * bnx2fc_percpu_io_thread - thread per cpu for ios
574 * @arg: ptr to bnx2fc_percpu_info structure
576 int bnx2fc_percpu_io_thread(void *arg)
578 struct bnx2fc_percpu_s *p = arg;
579 struct bnx2fc_work *work, *tmp;
580 LIST_HEAD(work_list);
582 set_user_nice(current, -20);
583 set_current_state(TASK_INTERRUPTIBLE);
584 while (!kthread_should_stop()) {
585 schedule();
586 spin_lock_bh(&p->fp_work_lock);
587 while (!list_empty(&p->work_list)) {
588 list_splice_init(&p->work_list, &work_list);
589 spin_unlock_bh(&p->fp_work_lock);
591 list_for_each_entry_safe(work, tmp, &work_list, list) {
592 list_del_init(&work->list);
593 bnx2fc_process_cq_compl(work->tgt, work->wqe);
594 kfree(work);
597 spin_lock_bh(&p->fp_work_lock);
599 __set_current_state(TASK_INTERRUPTIBLE);
600 spin_unlock_bh(&p->fp_work_lock);
602 __set_current_state(TASK_RUNNING);
604 return 0;
607 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
609 struct fc_host_statistics *bnx2fc_stats;
610 struct fc_lport *lport = shost_priv(shost);
611 struct fcoe_port *port = lport_priv(lport);
612 struct bnx2fc_interface *interface = port->priv;
613 struct bnx2fc_hba *hba = interface->hba;
614 struct fcoe_statistics_params *fw_stats;
615 int rc = 0;
617 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
618 if (!fw_stats)
619 return NULL;
621 bnx2fc_stats = fc_get_host_stats(shost);
623 init_completion(&hba->stat_req_done);
624 if (bnx2fc_send_stat_req(hba))
625 return bnx2fc_stats;
626 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
627 if (!rc) {
628 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
629 return bnx2fc_stats;
631 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
632 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
633 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
634 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
635 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
637 bnx2fc_stats->dumped_frames = 0;
638 bnx2fc_stats->lip_count = 0;
639 bnx2fc_stats->nos_count = 0;
640 bnx2fc_stats->loss_of_sync_count = 0;
641 bnx2fc_stats->loss_of_signal_count = 0;
642 bnx2fc_stats->prim_seq_protocol_err_count = 0;
644 return bnx2fc_stats;
647 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
649 struct fcoe_port *port = lport_priv(lport);
650 struct bnx2fc_interface *interface = port->priv;
651 struct Scsi_Host *shost = lport->host;
652 int rc = 0;
654 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
655 shost->max_lun = BNX2FC_MAX_LUN;
656 shost->max_id = BNX2FC_MAX_FCP_TGT;
657 shost->max_channel = 0;
658 if (lport->vport)
659 shost->transportt = bnx2fc_vport_xport_template;
660 else
661 shost->transportt = bnx2fc_transport_template;
663 /* Add the new host to SCSI-ml */
664 rc = scsi_add_host(lport->host, dev);
665 if (rc) {
666 printk(KERN_ERR PFX "Error on scsi_add_host\n");
667 return rc;
669 if (!lport->vport)
670 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
671 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
672 BNX2FC_NAME, BNX2FC_VERSION,
673 interface->netdev->name);
675 return 0;
678 static void bnx2fc_link_speed_update(struct fc_lport *lport)
680 struct fcoe_port *port = lport_priv(lport);
681 struct bnx2fc_interface *interface = port->priv;
682 struct net_device *netdev = interface->netdev;
683 struct ethtool_cmd ecmd;
685 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
686 lport->link_supported_speeds &=
687 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
688 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
689 SUPPORTED_1000baseT_Full))
690 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
691 if (ecmd.supported & SUPPORTED_10000baseT_Full)
692 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
694 switch (ethtool_cmd_speed(&ecmd)) {
695 case SPEED_1000:
696 lport->link_speed = FC_PORTSPEED_1GBIT;
697 break;
698 case SPEED_2500:
699 lport->link_speed = FC_PORTSPEED_2GBIT;
700 break;
701 case SPEED_10000:
702 lport->link_speed = FC_PORTSPEED_10GBIT;
703 break;
707 static int bnx2fc_link_ok(struct fc_lport *lport)
709 struct fcoe_port *port = lport_priv(lport);
710 struct bnx2fc_interface *interface = port->priv;
711 struct bnx2fc_hba *hba = interface->hba;
712 struct net_device *dev = hba->phys_dev;
713 int rc = 0;
715 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
716 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
717 else {
718 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
719 rc = -1;
721 return rc;
725 * bnx2fc_get_link_state - get network link state
727 * @hba: adapter instance pointer
729 * updates adapter structure flag based on netdev state
731 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
733 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
734 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
735 else
736 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
739 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
741 struct bnx2fc_hba *hba;
742 struct bnx2fc_interface *interface;
743 struct fcoe_port *port;
744 u64 wwnn, wwpn;
746 port = lport_priv(lport);
747 interface = port->priv;
748 hba = interface->hba;
750 /* require support for get_pauseparam ethtool op. */
751 if (!hba->phys_dev->ethtool_ops ||
752 !hba->phys_dev->ethtool_ops->get_pauseparam)
753 return -EOPNOTSUPP;
755 if (fc_set_mfs(lport, BNX2FC_MFS))
756 return -EINVAL;
758 skb_queue_head_init(&port->fcoe_pending_queue);
759 port->fcoe_pending_queue_active = 0;
760 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
762 bnx2fc_link_speed_update(lport);
764 if (!lport->vport) {
765 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
766 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
767 1, 0);
768 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
769 fc_set_wwnn(lport, wwnn);
771 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
772 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
773 2, 0);
775 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
776 fc_set_wwpn(lport, wwpn);
779 return 0;
782 static void bnx2fc_destroy_timer(unsigned long data)
784 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
786 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
787 "Destroy compl not received!!\n");
788 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
789 wake_up_interruptible(&hba->destroy_wait);
793 * bnx2fc_indicate_netevent - Generic netdev event handler
795 * @context: adapter structure pointer
796 * @event: event type
797 * @vlan_id: vlan id - associated vlan id with this event
799 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
800 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
802 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
803 u16 vlan_id)
805 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
806 struct fc_lport *lport;
807 struct fc_lport *vport;
808 struct bnx2fc_interface *interface, *tmp;
809 int wait_for_upload = 0;
810 u32 link_possible = 1;
812 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
813 return;
815 switch (event) {
816 case NETDEV_UP:
817 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
818 printk(KERN_ERR "indicate_netevent: "\
819 "hba is not UP!!\n");
820 break;
822 case NETDEV_DOWN:
823 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
824 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
825 link_possible = 0;
826 break;
828 case NETDEV_GOING_DOWN:
829 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
830 link_possible = 0;
831 break;
833 case NETDEV_CHANGE:
834 break;
836 case NETDEV_UNREGISTER:
837 if (!vlan_id)
838 return;
839 mutex_lock(&bnx2fc_dev_lock);
840 list_for_each_entry_safe(interface, tmp, &if_list, list) {
841 if (interface->hba != hba)
842 continue;
843 __bnx2fc_destroy(interface, true);
845 mutex_unlock(&bnx2fc_dev_lock);
846 return;
848 default:
849 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
850 return;
853 mutex_lock(&bnx2fc_dev_lock);
854 list_for_each_entry(interface, &if_list, list) {
856 if (interface->hba != hba)
857 continue;
859 lport = interface->ctlr.lp;
860 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
861 interface->netdev->name, event);
863 bnx2fc_link_speed_update(lport);
865 if (link_possible && !bnx2fc_link_ok(lport)) {
866 printk(KERN_ERR "indicate_netevent: ctlr_link_up\n");
867 fcoe_ctlr_link_up(&interface->ctlr);
868 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
869 mutex_lock(&lport->lp_mutex);
870 list_for_each_entry(vport, &lport->vports, list)
871 fc_host_port_type(vport->host) =
872 FC_PORTTYPE_UNKNOWN;
873 mutex_unlock(&lport->lp_mutex);
874 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
875 per_cpu_ptr(lport->dev_stats,
876 get_cpu())->LinkFailureCount++;
877 put_cpu();
878 fcoe_clean_pending_queue(lport);
879 wait_for_upload = 1;
882 mutex_unlock(&bnx2fc_dev_lock);
884 if (wait_for_upload) {
885 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
886 init_waitqueue_head(&hba->shutdown_wait);
887 BNX2FC_MISC_DBG("indicate_netevent "
888 "num_ofld_sess = %d\n",
889 hba->num_ofld_sess);
890 hba->wait_for_link_down = 1;
891 wait_event_interruptible(hba->shutdown_wait,
892 (hba->num_ofld_sess == 0));
893 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
894 hba->num_ofld_sess);
895 hba->wait_for_link_down = 0;
897 if (signal_pending(current))
898 flush_signals(current);
902 static int bnx2fc_libfc_config(struct fc_lport *lport)
905 /* Set the function pointers set by bnx2fc driver */
906 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
907 sizeof(struct libfc_function_template));
908 fc_elsct_init(lport);
909 fc_exch_init(lport);
910 fc_rport_init(lport);
911 fc_disc_init(lport);
912 return 0;
915 static int bnx2fc_em_config(struct fc_lport *lport)
917 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
918 FCOE_MAX_XID, NULL)) {
919 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
920 return -ENOMEM;
923 return 0;
926 static int bnx2fc_lport_config(struct fc_lport *lport)
928 lport->link_up = 0;
929 lport->qfull = 0;
930 lport->max_retry_count = 3;
931 lport->max_rport_retry_count = 3;
932 lport->e_d_tov = 2 * 1000;
933 lport->r_a_tov = 10 * 1000;
935 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
936 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
937 lport->does_npiv = 1;
939 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
940 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
942 /* alloc stats structure */
943 if (fc_lport_init_stats(lport))
944 return -ENOMEM;
946 /* Finish fc_lport configuration */
947 fc_lport_config(lport);
949 return 0;
953 * bnx2fc_fip_recv - handle a received FIP frame.
955 * @skb: the received skb
956 * @dev: associated &net_device
957 * @ptype: the &packet_type structure which was used to register this handler.
958 * @orig_dev: original receive &net_device, in case @ dev is a bond.
960 * Returns: 0 for success
962 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
963 struct packet_type *ptype,
964 struct net_device *orig_dev)
966 struct bnx2fc_interface *interface;
967 interface = container_of(ptype, struct bnx2fc_interface,
968 fip_packet_type);
969 fcoe_ctlr_recv(&interface->ctlr, skb);
970 return 0;
974 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
976 * @fip: FCoE controller.
977 * @old: Unicast MAC address to delete if the MAC is non-zero.
978 * @new: Unicast MAC address to add.
980 * Remove any previously-set unicast MAC filter.
981 * Add secondary FCoE MAC address filter for our OUI.
983 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
985 struct fcoe_port *port = lport_priv(lport);
987 memcpy(port->data_src_addr, addr, ETH_ALEN);
991 * bnx2fc_get_src_mac - return the ethernet source address for an lport
993 * @lport: libfc port
995 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
997 struct fcoe_port *port;
999 port = (struct fcoe_port *)lport_priv(lport);
1000 return port->data_src_addr;
1004 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1006 * @fip: FCoE controller.
1007 * @skb: FIP Packet.
1009 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1011 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1012 dev_queue_xmit(skb);
1015 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1017 struct Scsi_Host *shost = vport_to_shost(vport);
1018 struct fc_lport *n_port = shost_priv(shost);
1019 struct fcoe_port *port = lport_priv(n_port);
1020 struct bnx2fc_interface *interface = port->priv;
1021 struct net_device *netdev = interface->netdev;
1022 struct fc_lport *vn_port;
1024 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1025 printk(KERN_ERR PFX "vn ports cannot be created on"
1026 "this interface\n");
1027 return -EIO;
1029 mutex_lock(&bnx2fc_dev_lock);
1030 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1031 mutex_unlock(&bnx2fc_dev_lock);
1033 if (IS_ERR(vn_port)) {
1034 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1035 netdev->name);
1036 return -EIO;
1039 if (disabled) {
1040 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1041 } else {
1042 vn_port->boot_time = jiffies;
1043 fc_lport_init(vn_port);
1044 fc_fabric_login(vn_port);
1045 fc_vport_setlink(vn_port);
1047 return 0;
1050 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1052 struct bnx2fc_lport *blport, *tmp;
1054 spin_lock_bh(&hba->hba_lock);
1055 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1056 if (blport->lport == lport) {
1057 list_del(&blport->list);
1058 kfree(blport);
1061 spin_unlock_bh(&hba->hba_lock);
1064 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1066 struct Scsi_Host *shost = vport_to_shost(vport);
1067 struct fc_lport *n_port = shost_priv(shost);
1068 struct fc_lport *vn_port = vport->dd_data;
1069 struct fcoe_port *port = lport_priv(vn_port);
1070 struct bnx2fc_interface *interface = port->priv;
1071 struct fc_lport *v_port;
1072 bool found = false;
1074 mutex_lock(&n_port->lp_mutex);
1075 list_for_each_entry(v_port, &n_port->vports, list)
1076 if (v_port->vport == vport) {
1077 found = true;
1078 break;
1081 if (!found) {
1082 mutex_unlock(&n_port->lp_mutex);
1083 return -ENOENT;
1085 list_del(&vn_port->list);
1086 mutex_unlock(&n_port->lp_mutex);
1087 bnx2fc_free_vport(interface->hba, port->lport);
1088 bnx2fc_port_shutdown(port->lport);
1089 bnx2fc_interface_put(interface);
1090 queue_work(bnx2fc_wq, &port->destroy_work);
1091 return 0;
1094 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1096 struct fc_lport *lport = vport->dd_data;
1098 if (disable) {
1099 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1100 fc_fabric_logoff(lport);
1101 } else {
1102 lport->boot_time = jiffies;
1103 fc_fabric_login(lport);
1104 fc_vport_setlink(lport);
1106 return 0;
1110 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1112 struct net_device *netdev = interface->netdev;
1113 struct net_device *physdev = interface->hba->phys_dev;
1114 struct netdev_hw_addr *ha;
1115 int sel_san_mac = 0;
1117 /* setup Source MAC Address */
1118 rcu_read_lock();
1119 for_each_dev_addr(physdev, ha) {
1120 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1121 ha->type);
1122 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1123 ha->addr[1], ha->addr[2], ha->addr[3],
1124 ha->addr[4], ha->addr[5]);
1126 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1127 (is_valid_ether_addr(ha->addr))) {
1128 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1129 ETH_ALEN);
1130 sel_san_mac = 1;
1131 BNX2FC_MISC_DBG("Found SAN MAC\n");
1134 rcu_read_unlock();
1136 if (!sel_san_mac)
1137 return -ENODEV;
1139 interface->fip_packet_type.func = bnx2fc_fip_recv;
1140 interface->fip_packet_type.type = htons(ETH_P_FIP);
1141 interface->fip_packet_type.dev = netdev;
1142 dev_add_pack(&interface->fip_packet_type);
1144 interface->fcoe_packet_type.func = bnx2fc_rcv;
1145 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1146 interface->fcoe_packet_type.dev = netdev;
1147 dev_add_pack(&interface->fcoe_packet_type);
1149 return 0;
1152 static int bnx2fc_attach_transport(void)
1154 bnx2fc_transport_template =
1155 fc_attach_transport(&bnx2fc_transport_function);
1157 if (bnx2fc_transport_template == NULL) {
1158 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1159 return -ENODEV;
1162 bnx2fc_vport_xport_template =
1163 fc_attach_transport(&bnx2fc_vport_xport_function);
1164 if (bnx2fc_vport_xport_template == NULL) {
1165 printk(KERN_ERR PFX
1166 "Failed to attach FC transport for vport\n");
1167 fc_release_transport(bnx2fc_transport_template);
1168 bnx2fc_transport_template = NULL;
1169 return -ENODEV;
1171 return 0;
1173 static void bnx2fc_release_transport(void)
1175 fc_release_transport(bnx2fc_transport_template);
1176 fc_release_transport(bnx2fc_vport_xport_template);
1177 bnx2fc_transport_template = NULL;
1178 bnx2fc_vport_xport_template = NULL;
1181 static void bnx2fc_interface_release(struct kref *kref)
1183 struct bnx2fc_interface *interface;
1184 struct net_device *netdev;
1186 interface = container_of(kref, struct bnx2fc_interface, kref);
1187 BNX2FC_MISC_DBG("Interface is being released\n");
1189 netdev = interface->netdev;
1191 /* tear-down FIP controller */
1192 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1193 fcoe_ctlr_destroy(&interface->ctlr);
1195 kfree(interface);
1197 dev_put(netdev);
1198 module_put(THIS_MODULE);
1201 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1203 kref_get(&interface->kref);
1206 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1208 kref_put(&interface->kref, bnx2fc_interface_release);
1210 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1212 /* Free the command manager */
1213 if (hba->cmd_mgr) {
1214 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1215 hba->cmd_mgr = NULL;
1217 kfree(hba->tgt_ofld_list);
1218 bnx2fc_unbind_pcidev(hba);
1219 kfree(hba);
1223 * bnx2fc_hba_create - create a new bnx2fc hba
1225 * @cnic: pointer to cnic device
1227 * Creates a new FCoE hba on the given device.
1230 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1232 struct bnx2fc_hba *hba;
1233 int rc;
1235 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1236 if (!hba) {
1237 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1238 return NULL;
1240 spin_lock_init(&hba->hba_lock);
1241 mutex_init(&hba->hba_mutex);
1243 hba->cnic = cnic;
1244 rc = bnx2fc_bind_pcidev(hba);
1245 if (rc) {
1246 printk(KERN_ERR PFX "create_adapter: bind error\n");
1247 goto bind_err;
1249 hba->phys_dev = cnic->netdev;
1250 hba->next_conn_id = 0;
1252 hba->tgt_ofld_list =
1253 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1254 GFP_KERNEL);
1255 if (!hba->tgt_ofld_list) {
1256 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1257 goto tgtofld_err;
1260 hba->num_ofld_sess = 0;
1262 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1263 BNX2FC_MAX_XID);
1264 if (!hba->cmd_mgr) {
1265 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1266 goto cmgr_err;
1269 init_waitqueue_head(&hba->shutdown_wait);
1270 init_waitqueue_head(&hba->destroy_wait);
1271 INIT_LIST_HEAD(&hba->vports);
1273 return hba;
1275 cmgr_err:
1276 kfree(hba->tgt_ofld_list);
1277 tgtofld_err:
1278 bnx2fc_unbind_pcidev(hba);
1279 bind_err:
1280 kfree(hba);
1281 return NULL;
1284 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1285 struct net_device *netdev,
1286 enum fip_state fip_mode)
1288 struct bnx2fc_interface *interface;
1289 int rc = 0;
1291 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1292 if (!interface) {
1293 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1294 return NULL;
1296 dev_hold(netdev);
1297 kref_init(&interface->kref);
1298 interface->hba = hba;
1299 interface->netdev = netdev;
1301 /* Initialize FIP */
1302 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1303 interface->ctlr.send = bnx2fc_fip_send;
1304 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1305 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1306 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1308 rc = bnx2fc_interface_setup(interface);
1309 if (!rc)
1310 return interface;
1312 fcoe_ctlr_destroy(&interface->ctlr);
1313 dev_put(netdev);
1314 kfree(interface);
1315 return NULL;
1319 * bnx2fc_if_create - Create FCoE instance on a given interface
1321 * @interface: FCoE interface to create a local port on
1322 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1323 * @npiv: Indicates if the port is vport or not
1325 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1327 * Returns: Allocated fc_lport or an error pointer
1329 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1330 struct device *parent, int npiv)
1332 struct fc_lport *lport, *n_port;
1333 struct fcoe_port *port;
1334 struct Scsi_Host *shost;
1335 struct fc_vport *vport = dev_to_vport(parent);
1336 struct bnx2fc_lport *blport;
1337 struct bnx2fc_hba *hba;
1338 int rc = 0;
1340 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1341 if (!blport) {
1342 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
1343 return NULL;
1346 /* Allocate Scsi_Host structure */
1347 if (!npiv)
1348 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1349 else
1350 lport = libfc_vport_create(vport, sizeof(*port));
1352 if (!lport) {
1353 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1354 goto free_blport;
1356 shost = lport->host;
1357 port = lport_priv(lport);
1358 port->lport = lport;
1359 port->priv = interface;
1360 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1362 /* Configure fcoe_port */
1363 rc = bnx2fc_lport_config(lport);
1364 if (rc)
1365 goto lp_config_err;
1367 if (npiv) {
1368 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1369 vport->node_name, vport->port_name);
1370 fc_set_wwnn(lport, vport->node_name);
1371 fc_set_wwpn(lport, vport->port_name);
1373 /* Configure netdev and networking properties of the lport */
1374 rc = bnx2fc_net_config(lport, interface->netdev);
1375 if (rc) {
1376 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1377 goto lp_config_err;
1380 rc = bnx2fc_shost_config(lport, parent);
1381 if (rc) {
1382 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1383 interface->netdev->name);
1384 goto lp_config_err;
1387 /* Initialize the libfc library */
1388 rc = bnx2fc_libfc_config(lport);
1389 if (rc) {
1390 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1391 goto shost_err;
1393 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1395 /* Allocate exchange manager */
1396 if (!npiv)
1397 rc = bnx2fc_em_config(lport);
1398 else {
1399 shost = vport_to_shost(vport);
1400 n_port = shost_priv(shost);
1401 rc = fc_exch_mgr_list_clone(n_port, lport);
1404 if (rc) {
1405 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1406 goto shost_err;
1409 bnx2fc_interface_get(interface);
1411 hba = interface->hba;
1412 spin_lock_bh(&hba->hba_lock);
1413 blport->lport = lport;
1414 list_add_tail(&blport->list, &hba->vports);
1415 spin_unlock_bh(&hba->hba_lock);
1417 return lport;
1419 shost_err:
1420 scsi_remove_host(shost);
1421 lp_config_err:
1422 scsi_host_put(lport->host);
1423 free_blport:
1424 kfree(blport);
1425 return NULL;
1428 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1430 struct fc_lport *lport = interface->ctlr.lp;
1431 struct fcoe_port *port = lport_priv(lport);
1432 struct bnx2fc_hba *hba = interface->hba;
1434 /* Stop the transmit retry timer */
1435 del_timer_sync(&port->timer);
1437 /* Free existing transmit skbs */
1438 fcoe_clean_pending_queue(lport);
1440 /* Dont listen for Ethernet packets anymore */
1441 __dev_remove_pack(&interface->fcoe_packet_type);
1442 __dev_remove_pack(&interface->fip_packet_type);
1443 synchronize_net();
1445 bnx2fc_free_vport(hba, lport);
1448 static void bnx2fc_if_destroy(struct fc_lport *lport)
1451 /* Free queued packets for the receive thread */
1452 bnx2fc_clean_rx_queue(lport);
1454 /* Detach from scsi-ml */
1455 fc_remove_host(lport->host);
1456 scsi_remove_host(lport->host);
1459 * Note that only the physical lport will have the exchange manager.
1460 * for vports, this function is NOP
1462 fc_exch_mgr_free(lport);
1464 /* Free memory used by statistical counters */
1465 fc_lport_free_stats(lport);
1467 /* Release Scsi_Host */
1468 scsi_host_put(lport->host);
1471 static void __bnx2fc_destroy(struct bnx2fc_interface *interface, bool schedule)
1473 struct fc_lport *lport = interface->ctlr.lp;
1474 struct fcoe_port *port = lport_priv(lport);
1476 bnx2fc_interface_cleanup(interface);
1477 bnx2fc_stop(interface);
1479 list_del(&interface->list);
1480 lport = interface->ctlr.lp;
1481 bnx2fc_interface_put(interface);
1482 if (schedule)
1483 queue_work(bnx2fc_wq, &port->destroy_work);
1484 else
1485 bnx2fc_if_destroy(lport);
1489 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1491 * @buffer: The name of the Ethernet interface to be destroyed
1492 * @kp: The associated kernel parameter
1494 * Called from sysfs.
1496 * Returns: 0 for success
1498 static int bnx2fc_destroy(struct net_device *netdev)
1500 struct bnx2fc_interface *interface = NULL;
1501 int rc = 0;
1503 rtnl_lock();
1504 mutex_lock(&bnx2fc_dev_lock);
1506 interface = bnx2fc_interface_lookup(netdev);
1507 if (!interface || !interface->ctlr.lp) {
1508 rc = -ENODEV;
1509 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1510 goto netdev_err;
1514 destroy_workqueue(interface->timer_work_queue);
1515 __bnx2fc_destroy(interface, false);
1517 netdev_err:
1518 mutex_unlock(&bnx2fc_dev_lock);
1519 rtnl_unlock();
1520 return rc;
1523 static void bnx2fc_destroy_work(struct work_struct *work)
1525 struct fcoe_port *port;
1526 struct fc_lport *lport;
1528 port = container_of(work, struct fcoe_port, destroy_work);
1529 lport = port->lport;
1531 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1533 rtnl_lock();
1534 mutex_lock(&bnx2fc_dev_lock);
1535 bnx2fc_if_destroy(lport);
1536 mutex_unlock(&bnx2fc_dev_lock);
1537 rtnl_unlock();
1540 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1542 bnx2fc_free_fw_resc(hba);
1543 bnx2fc_free_task_ctx(hba);
1547 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1548 * pci structure
1550 * @hba: Adapter instance
1552 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1554 if (bnx2fc_setup_task_ctx(hba))
1555 goto mem_err;
1557 if (bnx2fc_setup_fw_resc(hba))
1558 goto mem_err;
1560 return 0;
1561 mem_err:
1562 bnx2fc_unbind_adapter_devices(hba);
1563 return -ENOMEM;
1566 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1568 struct cnic_dev *cnic;
1570 if (!hba->cnic) {
1571 printk(KERN_ERR PFX "cnic is NULL\n");
1572 return -ENODEV;
1574 cnic = hba->cnic;
1575 hba->pcidev = cnic->pcidev;
1576 if (hba->pcidev)
1577 pci_dev_get(hba->pcidev);
1579 return 0;
1582 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1584 if (hba->pcidev)
1585 pci_dev_put(hba->pcidev);
1586 hba->pcidev = NULL;
1592 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1594 * @handle: transport handle pointing to adapter struture
1596 * This function maps adapter structure to pcidev structure and initiates
1597 * firmware handshake to enable/initialize on-chip FCoE components.
1598 * This bnx2fc - cnic interface api callback is used after following
1599 * conditions are met -
1600 * a) underlying network interface is up (marked by event NETDEV_UP
1601 * from netdev
1602 * b) bnx2fc adatper structure is registered.
1604 static void bnx2fc_ulp_start(void *handle)
1606 struct bnx2fc_hba *hba = handle;
1607 struct bnx2fc_interface *interface;
1608 struct fc_lport *lport;
1610 mutex_lock(&bnx2fc_dev_lock);
1612 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1613 bnx2fc_fw_init(hba);
1615 BNX2FC_MISC_DBG("bnx2fc started.\n");
1617 list_for_each_entry(interface, &if_list, list) {
1618 if (interface->hba == hba) {
1619 lport = interface->ctlr.lp;
1620 /* Kick off Fabric discovery*/
1621 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1622 lport->tt.frame_send = bnx2fc_xmit;
1623 bnx2fc_start_disc(interface);
1627 mutex_unlock(&bnx2fc_dev_lock);
1630 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1632 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1633 fc_fabric_logoff(lport);
1634 fc_lport_destroy(lport);
1637 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1639 struct fc_lport *lport;
1640 struct fc_lport *vport;
1642 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1643 return;
1645 lport = interface->ctlr.lp;
1646 bnx2fc_port_shutdown(lport);
1648 mutex_lock(&lport->lp_mutex);
1649 list_for_each_entry(vport, &lport->vports, list)
1650 fc_host_port_type(vport->host) =
1651 FC_PORTTYPE_UNKNOWN;
1652 mutex_unlock(&lport->lp_mutex);
1653 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1654 fcoe_ctlr_link_down(&interface->ctlr);
1655 fcoe_clean_pending_queue(lport);
1658 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1660 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1661 int rc = -1;
1662 int i = HZ;
1664 rc = bnx2fc_bind_adapter_devices(hba);
1665 if (rc) {
1666 printk(KERN_ALERT PFX
1667 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1668 goto err_out;
1671 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1672 if (rc) {
1673 printk(KERN_ALERT PFX
1674 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1675 goto err_unbind;
1679 * Wait until the adapter init message is complete, and adapter
1680 * state is UP.
1682 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1683 msleep(BNX2FC_INIT_POLL_TIME);
1685 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1686 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1687 "Ignoring...\n",
1688 hba->cnic->netdev->name);
1689 rc = -1;
1690 goto err_unbind;
1694 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1695 return 0;
1697 err_unbind:
1698 bnx2fc_unbind_adapter_devices(hba);
1699 err_out:
1700 return rc;
1703 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1705 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1706 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1707 init_timer(&hba->destroy_timer);
1708 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1709 jiffies;
1710 hba->destroy_timer.function = bnx2fc_destroy_timer;
1711 hba->destroy_timer.data = (unsigned long)hba;
1712 add_timer(&hba->destroy_timer);
1713 wait_event_interruptible(hba->destroy_wait,
1714 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1715 &hba->flags));
1716 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1717 /* This should never happen */
1718 if (signal_pending(current))
1719 flush_signals(current);
1721 del_timer_sync(&hba->destroy_timer);
1723 bnx2fc_unbind_adapter_devices(hba);
1728 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1730 * @handle: transport handle pointing to adapter structure
1732 * Driver checks if adapter is already in shutdown mode, if not start
1733 * the shutdown process.
1735 static void bnx2fc_ulp_stop(void *handle)
1737 struct bnx2fc_hba *hba = handle;
1738 struct bnx2fc_interface *interface;
1740 printk(KERN_ERR "ULP_STOP\n");
1742 mutex_lock(&bnx2fc_dev_lock);
1743 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1744 goto exit;
1745 list_for_each_entry(interface, &if_list, list) {
1746 if (interface->hba == hba)
1747 bnx2fc_stop(interface);
1749 BUG_ON(hba->num_ofld_sess != 0);
1751 mutex_lock(&hba->hba_mutex);
1752 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1753 clear_bit(ADAPTER_STATE_GOING_DOWN,
1754 &hba->adapter_state);
1756 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1757 mutex_unlock(&hba->hba_mutex);
1759 bnx2fc_fw_destroy(hba);
1760 exit:
1761 mutex_unlock(&bnx2fc_dev_lock);
1764 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1766 struct fc_lport *lport;
1767 int wait_cnt = 0;
1769 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1770 /* Kick off FIP/FLOGI */
1771 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1772 printk(KERN_ERR PFX "Init not done yet\n");
1773 return;
1776 lport = interface->ctlr.lp;
1777 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1779 if (!bnx2fc_link_ok(lport)) {
1780 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1781 fcoe_ctlr_link_up(&interface->ctlr);
1782 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1783 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1786 /* wait for the FCF to be selected before issuing FLOGI */
1787 while (!interface->ctlr.sel_fcf) {
1788 msleep(250);
1789 /* give up after 3 secs */
1790 if (++wait_cnt > 12)
1791 break;
1794 /* Reset max receive frame size to default */
1795 if (fc_set_mfs(lport, BNX2FC_MFS))
1796 return;
1798 fc_lport_init(lport);
1799 fc_fabric_login(lport);
1804 * bnx2fc_ulp_init - Initialize an adapter instance
1806 * @dev : cnic device handle
1807 * Called from cnic_register_driver() context to initialize all
1808 * enumerated cnic devices. This routine allocates adapter structure
1809 * and other device specific resources.
1811 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1813 struct bnx2fc_hba *hba;
1814 int rc = 0;
1816 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1817 /* bnx2fc works only when bnx2x is loaded */
1818 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1819 (dev->max_fcoe_conn == 0)) {
1820 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1821 " flags: %lx fcoe_conn: %d\n",
1822 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1823 return;
1826 hba = bnx2fc_hba_create(dev);
1827 if (!hba) {
1828 printk(KERN_ERR PFX "hba initialization failed\n");
1829 return;
1832 /* Add HBA to the adapter list */
1833 mutex_lock(&bnx2fc_dev_lock);
1834 list_add_tail(&hba->list, &adapter_list);
1835 adapter_count++;
1836 mutex_unlock(&bnx2fc_dev_lock);
1838 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1839 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1840 (void *) hba);
1841 if (rc)
1842 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
1843 else
1844 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1848 static int bnx2fc_disable(struct net_device *netdev)
1850 struct bnx2fc_interface *interface;
1851 int rc = 0;
1853 rtnl_lock();
1854 mutex_lock(&bnx2fc_dev_lock);
1856 interface = bnx2fc_interface_lookup(netdev);
1857 if (!interface || !interface->ctlr.lp) {
1858 rc = -ENODEV;
1859 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
1860 } else {
1861 fcoe_ctlr_link_down(&interface->ctlr);
1862 fcoe_clean_pending_queue(interface->ctlr.lp);
1865 mutex_unlock(&bnx2fc_dev_lock);
1866 rtnl_unlock();
1867 return rc;
1871 static int bnx2fc_enable(struct net_device *netdev)
1873 struct bnx2fc_interface *interface;
1874 int rc = 0;
1876 rtnl_lock();
1877 mutex_lock(&bnx2fc_dev_lock);
1879 interface = bnx2fc_interface_lookup(netdev);
1880 if (!interface || !interface->ctlr.lp) {
1881 rc = -ENODEV;
1882 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
1883 } else if (!bnx2fc_link_ok(interface->ctlr.lp))
1884 fcoe_ctlr_link_up(&interface->ctlr);
1886 mutex_unlock(&bnx2fc_dev_lock);
1887 rtnl_unlock();
1888 return rc;
1892 * bnx2fc_create - Create bnx2fc FCoE interface
1894 * @buffer: The name of Ethernet interface to create on
1895 * @kp: The associated kernel param
1897 * Called from sysfs.
1899 * Returns: 0 for success
1901 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1903 struct bnx2fc_interface *interface;
1904 struct bnx2fc_hba *hba;
1905 struct net_device *phys_dev;
1906 struct fc_lport *lport;
1907 struct ethtool_drvinfo drvinfo;
1908 int rc = 0;
1909 int vlan_id;
1911 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1912 if (fip_mode != FIP_MODE_FABRIC) {
1913 printk(KERN_ERR "fip mode not FABRIC\n");
1914 return -EIO;
1917 rtnl_lock();
1919 mutex_lock(&bnx2fc_dev_lock);
1921 if (!try_module_get(THIS_MODULE)) {
1922 rc = -EINVAL;
1923 goto mod_err;
1926 /* obtain physical netdev */
1927 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1928 phys_dev = vlan_dev_real_dev(netdev);
1929 vlan_id = vlan_dev_vlan_id(netdev);
1930 } else {
1931 printk(KERN_ERR PFX "Not a vlan device\n");
1932 rc = -EINVAL;
1933 goto netdev_err;
1935 /* verify if the physical device is a netxtreme2 device */
1936 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1937 memset(&drvinfo, 0, sizeof(drvinfo));
1938 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1939 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
1940 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1941 rc = -EINVAL;
1942 goto netdev_err;
1944 } else {
1945 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1946 rc = -EINVAL;
1947 goto netdev_err;
1950 /* obtain interface and initialize rest of the structure */
1951 hba = bnx2fc_hba_lookup(phys_dev);
1952 if (!hba) {
1953 rc = -ENODEV;
1954 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1955 goto netdev_err;
1958 if (bnx2fc_interface_lookup(netdev)) {
1959 rc = -EEXIST;
1960 goto netdev_err;
1963 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
1964 if (!interface) {
1965 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
1966 goto ifput_err;
1969 interface->vlan_id = vlan_id;
1970 interface->vlan_enabled = 1;
1972 interface->timer_work_queue =
1973 create_singlethread_workqueue("bnx2fc_timer_wq");
1974 if (!interface->timer_work_queue) {
1975 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
1976 rc = -EINVAL;
1977 goto ifput_err;
1980 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
1981 if (!lport) {
1982 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
1983 netdev->name);
1984 bnx2fc_interface_cleanup(interface);
1985 rc = -EINVAL;
1986 goto if_create_err;
1989 /* Add interface to if_list */
1990 list_add_tail(&interface->list, &if_list);
1992 lport->boot_time = jiffies;
1994 /* Make this master N_port */
1995 interface->ctlr.lp = lport;
1997 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
1998 bnx2fc_start_disc(interface);
2000 * Release from kref_init in bnx2fc_interface_setup, on success
2001 * lport should be holding a reference taken in bnx2fc_if_create
2003 bnx2fc_interface_put(interface);
2004 /* put netdev that was held while calling dev_get_by_name */
2005 mutex_unlock(&bnx2fc_dev_lock);
2006 rtnl_unlock();
2007 return 0;
2009 if_create_err:
2010 destroy_workqueue(interface->timer_work_queue);
2011 ifput_err:
2012 bnx2fc_interface_put(interface);
2013 netdev_err:
2014 module_put(THIS_MODULE);
2015 mod_err:
2016 mutex_unlock(&bnx2fc_dev_lock);
2017 rtnl_unlock();
2018 return rc;
2022 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2024 * @cnic: Pointer to cnic device instance
2027 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2029 struct list_head *list;
2030 struct list_head *temp;
2031 struct bnx2fc_hba *hba;
2033 /* Called with bnx2fc_dev_lock held */
2034 list_for_each_safe(list, temp, &adapter_list) {
2035 hba = (struct bnx2fc_hba *)list;
2036 if (hba->cnic == cnic)
2037 return hba;
2039 return NULL;
2042 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2043 *netdev)
2045 struct bnx2fc_interface *interface;
2047 /* Called with bnx2fc_dev_lock held */
2048 list_for_each_entry(interface, &if_list, list) {
2049 if (interface->netdev == netdev)
2050 return interface;
2052 return NULL;
2055 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2056 *phys_dev)
2058 struct bnx2fc_hba *hba;
2060 /* Called with bnx2fc_dev_lock held */
2061 list_for_each_entry(hba, &adapter_list, list) {
2062 if (hba->phys_dev == phys_dev)
2063 return hba;
2065 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2066 return NULL;
2070 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2072 * @dev cnic device handle
2074 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2076 struct bnx2fc_hba *hba;
2077 struct bnx2fc_interface *interface, *tmp;
2079 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2081 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2082 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2083 dev->netdev->name, dev->flags);
2084 return;
2087 mutex_lock(&bnx2fc_dev_lock);
2088 hba = bnx2fc_find_hba_for_cnic(dev);
2089 if (!hba) {
2090 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2091 dev);
2092 mutex_unlock(&bnx2fc_dev_lock);
2093 return;
2096 list_del_init(&hba->list);
2097 adapter_count--;
2099 list_for_each_entry_safe(interface, tmp, &if_list, list)
2100 /* destroy not called yet, move to quiesced list */
2101 if (interface->hba == hba)
2102 __bnx2fc_destroy(interface, false);
2103 mutex_unlock(&bnx2fc_dev_lock);
2105 bnx2fc_ulp_stop(hba);
2106 /* unregister cnic device */
2107 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2108 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2109 bnx2fc_hba_destroy(hba);
2113 * bnx2fc_fcoe_reset - Resets the fcoe
2115 * @shost: shost the reset is from
2117 * Returns: always 0
2119 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2121 struct fc_lport *lport = shost_priv(shost);
2122 fc_lport_reset(lport);
2123 return 0;
2127 static bool bnx2fc_match(struct net_device *netdev)
2129 mutex_lock(&bnx2fc_dev_lock);
2130 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2131 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2133 if (bnx2fc_hba_lookup(phys_dev)) {
2134 mutex_unlock(&bnx2fc_dev_lock);
2135 return true;
2138 mutex_unlock(&bnx2fc_dev_lock);
2139 return false;
2143 static struct fcoe_transport bnx2fc_transport = {
2144 .name = {"bnx2fc"},
2145 .attached = false,
2146 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2147 .match = bnx2fc_match,
2148 .create = bnx2fc_create,
2149 .destroy = bnx2fc_destroy,
2150 .enable = bnx2fc_enable,
2151 .disable = bnx2fc_disable,
2155 * bnx2fc_percpu_thread_create - Create a receive thread for an
2156 * online CPU
2158 * @cpu: cpu index for the online cpu
2160 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2162 struct bnx2fc_percpu_s *p;
2163 struct task_struct *thread;
2165 p = &per_cpu(bnx2fc_percpu, cpu);
2167 thread = kthread_create(bnx2fc_percpu_io_thread,
2168 (void *)p,
2169 "bnx2fc_thread/%d", cpu);
2170 /* bind thread to the cpu */
2171 if (likely(!IS_ERR(thread))) {
2172 kthread_bind(thread, cpu);
2173 p->iothread = thread;
2174 wake_up_process(thread);
2178 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2180 struct bnx2fc_percpu_s *p;
2181 struct task_struct *thread;
2182 struct bnx2fc_work *work, *tmp;
2184 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2186 /* Prevent any new work from being queued for this CPU */
2187 p = &per_cpu(bnx2fc_percpu, cpu);
2188 spin_lock_bh(&p->fp_work_lock);
2189 thread = p->iothread;
2190 p->iothread = NULL;
2193 /* Free all work in the list */
2194 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2195 list_del_init(&work->list);
2196 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2197 kfree(work);
2200 spin_unlock_bh(&p->fp_work_lock);
2202 if (thread)
2203 kthread_stop(thread);
2207 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2209 * @nfb: The callback data block
2210 * @action: The event triggering the callback
2211 * @hcpu: The index of the CPU that the event is for
2213 * This creates or destroys per-CPU data for fcoe
2215 * Returns NOTIFY_OK always.
2217 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2218 unsigned long action, void *hcpu)
2220 unsigned cpu = (unsigned long)hcpu;
2222 switch (action) {
2223 case CPU_ONLINE:
2224 case CPU_ONLINE_FROZEN:
2225 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2226 bnx2fc_percpu_thread_create(cpu);
2227 break;
2228 case CPU_DEAD:
2229 case CPU_DEAD_FROZEN:
2230 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2231 bnx2fc_percpu_thread_destroy(cpu);
2232 break;
2233 default:
2234 break;
2236 return NOTIFY_OK;
2240 * bnx2fc_mod_init - module init entry point
2242 * Initialize driver wide global data structures, and register
2243 * with cnic module
2245 static int __init bnx2fc_mod_init(void)
2247 struct fcoe_percpu_s *bg;
2248 struct task_struct *l2_thread;
2249 int rc = 0;
2250 unsigned int cpu = 0;
2251 struct bnx2fc_percpu_s *p;
2253 printk(KERN_INFO PFX "%s", version);
2255 /* register as a fcoe transport */
2256 rc = fcoe_transport_attach(&bnx2fc_transport);
2257 if (rc) {
2258 printk(KERN_ERR "failed to register an fcoe transport, check "
2259 "if libfcoe is loaded\n");
2260 goto out;
2263 INIT_LIST_HEAD(&adapter_list);
2264 INIT_LIST_HEAD(&if_list);
2265 mutex_init(&bnx2fc_dev_lock);
2266 adapter_count = 0;
2268 /* Attach FC transport template */
2269 rc = bnx2fc_attach_transport();
2270 if (rc)
2271 goto detach_ft;
2273 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2274 if (!bnx2fc_wq) {
2275 rc = -ENOMEM;
2276 goto release_bt;
2279 bg = &bnx2fc_global;
2280 skb_queue_head_init(&bg->fcoe_rx_list);
2281 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2282 (void *)bg,
2283 "bnx2fc_l2_thread");
2284 if (IS_ERR(l2_thread)) {
2285 rc = PTR_ERR(l2_thread);
2286 goto free_wq;
2288 wake_up_process(l2_thread);
2289 spin_lock_bh(&bg->fcoe_rx_list.lock);
2290 bg->thread = l2_thread;
2291 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2293 for_each_possible_cpu(cpu) {
2294 p = &per_cpu(bnx2fc_percpu, cpu);
2295 INIT_LIST_HEAD(&p->work_list);
2296 spin_lock_init(&p->fp_work_lock);
2299 for_each_online_cpu(cpu) {
2300 bnx2fc_percpu_thread_create(cpu);
2303 /* Initialize per CPU interrupt thread */
2304 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2306 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2308 return 0;
2310 free_wq:
2311 destroy_workqueue(bnx2fc_wq);
2312 release_bt:
2313 bnx2fc_release_transport();
2314 detach_ft:
2315 fcoe_transport_detach(&bnx2fc_transport);
2316 out:
2317 return rc;
2320 static void __exit bnx2fc_mod_exit(void)
2322 LIST_HEAD(to_be_deleted);
2323 struct bnx2fc_hba *hba, *next;
2324 struct fcoe_percpu_s *bg;
2325 struct task_struct *l2_thread;
2326 struct sk_buff *skb;
2327 unsigned int cpu = 0;
2330 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2331 * it will have higher precedence than bnx2fc_dev_lock.
2332 * unregister_device() cannot be called with bnx2fc_dev_lock
2333 * held.
2335 mutex_lock(&bnx2fc_dev_lock);
2336 list_splice(&adapter_list, &to_be_deleted);
2337 INIT_LIST_HEAD(&adapter_list);
2338 adapter_count = 0;
2339 mutex_unlock(&bnx2fc_dev_lock);
2341 /* Unregister with cnic */
2342 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2343 list_del_init(&hba->list);
2344 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2345 hba);
2346 bnx2fc_ulp_stop(hba);
2347 /* unregister cnic device */
2348 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2349 &hba->reg_with_cnic))
2350 hba->cnic->unregister_device(hba->cnic,
2351 CNIC_ULP_FCOE);
2352 bnx2fc_hba_destroy(hba);
2354 cnic_unregister_driver(CNIC_ULP_FCOE);
2356 /* Destroy global thread */
2357 bg = &bnx2fc_global;
2358 spin_lock_bh(&bg->fcoe_rx_list.lock);
2359 l2_thread = bg->thread;
2360 bg->thread = NULL;
2361 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2362 kfree_skb(skb);
2364 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2366 if (l2_thread)
2367 kthread_stop(l2_thread);
2369 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2371 /* Destroy per cpu threads */
2372 for_each_online_cpu(cpu) {
2373 bnx2fc_percpu_thread_destroy(cpu);
2376 destroy_workqueue(bnx2fc_wq);
2378 * detach from scsi transport
2379 * must happen after all destroys are done
2381 bnx2fc_release_transport();
2383 /* detach from fcoe transport */
2384 fcoe_transport_detach(&bnx2fc_transport);
2387 module_init(bnx2fc_mod_init);
2388 module_exit(bnx2fc_mod_exit);
2390 static struct fc_function_template bnx2fc_transport_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 = bnx2fc_get_host_stats,
2420 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2422 .terminate_rport_io = fc_rport_terminate_io,
2424 .vport_create = bnx2fc_vport_create,
2425 .vport_delete = bnx2fc_vport_destroy,
2426 .vport_disable = bnx2fc_vport_disable,
2427 .bsg_request = fc_lport_bsg_request,
2430 static struct fc_function_template bnx2fc_vport_xport_function = {
2431 .show_host_node_name = 1,
2432 .show_host_port_name = 1,
2433 .show_host_supported_classes = 1,
2434 .show_host_supported_fc4s = 1,
2435 .show_host_active_fc4s = 1,
2436 .show_host_maxframe_size = 1,
2438 .show_host_port_id = 1,
2439 .show_host_supported_speeds = 1,
2440 .get_host_speed = fc_get_host_speed,
2441 .show_host_speed = 1,
2442 .show_host_port_type = 1,
2443 .get_host_port_state = fc_get_host_port_state,
2444 .show_host_port_state = 1,
2445 .show_host_symbolic_name = 1,
2447 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2448 sizeof(struct bnx2fc_rport)),
2449 .show_rport_maxframe_size = 1,
2450 .show_rport_supported_classes = 1,
2452 .show_host_fabric_name = 1,
2453 .show_starget_node_name = 1,
2454 .show_starget_port_name = 1,
2455 .show_starget_port_id = 1,
2456 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2457 .show_rport_dev_loss_tmo = 1,
2458 .get_fc_host_stats = fc_get_host_stats,
2459 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2460 .terminate_rport_io = fc_rport_terminate_io,
2461 .bsg_request = fc_lport_bsg_request,
2465 * scsi_host_template structure used while registering with SCSI-ml
2467 static struct scsi_host_template bnx2fc_shost_template = {
2468 .module = THIS_MODULE,
2469 .name = "Broadcom Offload FCoE Initiator",
2470 .queuecommand = bnx2fc_queuecommand,
2471 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2472 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2473 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2474 .eh_host_reset_handler = fc_eh_host_reset,
2475 .slave_alloc = fc_slave_alloc,
2476 .change_queue_depth = fc_change_queue_depth,
2477 .change_queue_type = fc_change_queue_type,
2478 .this_id = -1,
2479 .cmd_per_lun = 3,
2480 .can_queue = BNX2FC_CAN_QUEUE,
2481 .use_clustering = ENABLE_CLUSTERING,
2482 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2483 .max_sectors = 512,
2486 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2487 .frame_send = bnx2fc_xmit,
2488 .elsct_send = bnx2fc_elsct_send,
2489 .fcp_abort_io = bnx2fc_abort_io,
2490 .fcp_cleanup = bnx2fc_cleanup,
2491 .rport_event_callback = bnx2fc_rport_event_handler,
2495 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2496 * structure carrying callback function pointers
2498 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2499 .owner = THIS_MODULE,
2500 .cnic_init = bnx2fc_ulp_init,
2501 .cnic_exit = bnx2fc_ulp_exit,
2502 .cnic_start = bnx2fc_ulp_start,
2503 .cnic_stop = bnx2fc_ulp_stop,
2504 .indicate_kcqes = bnx2fc_indicate_kcqe,
2505 .indicate_netevent = bnx2fc_indicate_netevent,