mmc: sdhci: Move sdhci_runtime_pm_bus_off|on() to avoid pre-definition
[linux/fpc-iii.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
blobd7029ea5d3193ec63e3a71caec5cb1e42ef1e82f
1 /* bnx2fc_fcoe.c: QLogic Linux FCoE offload driver.
2 * This file contains the code that interacts with libfc, libfcoe,
3 * cnic modules to create FCoE instances, send/receive non-offloaded
4 * FIP/FCoE packets, listen to link events etc.
6 * Copyright (c) 2008-2013 Broadcom Corporation
7 * Copyright (c) 2014-2015 QLogic Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
13 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
16 #include "bnx2fc.h"
18 static struct list_head adapter_list;
19 static struct list_head if_list;
20 static u32 adapter_count;
21 static DEFINE_MUTEX(bnx2fc_dev_lock);
22 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
24 #define DRV_MODULE_NAME "bnx2fc"
25 #define DRV_MODULE_VERSION BNX2FC_VERSION
26 #define DRV_MODULE_RELDATE "October 15, 2015"
29 static char version[] =
30 "QLogic FCoE Driver " DRV_MODULE_NAME \
31 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
34 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
35 MODULE_DESCRIPTION("QLogic FCoE Driver");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
39 #define BNX2FC_MAX_QUEUE_DEPTH 256
40 #define BNX2FC_MIN_QUEUE_DEPTH 32
41 #define FCOE_WORD_TO_BYTE 4
43 static struct scsi_transport_template *bnx2fc_transport_template;
44 static struct scsi_transport_template *bnx2fc_vport_xport_template;
46 struct workqueue_struct *bnx2fc_wq;
48 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
49 * Here the io threads are per cpu but the l2 thread is just one
51 struct fcoe_percpu_s bnx2fc_global;
52 DEFINE_SPINLOCK(bnx2fc_global_lock);
54 static struct cnic_ulp_ops bnx2fc_cnic_cb;
55 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
56 static struct scsi_host_template bnx2fc_shost_template;
57 static struct fc_function_template bnx2fc_transport_function;
58 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
59 static struct fc_function_template bnx2fc_vport_xport_function;
60 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
61 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
62 static int bnx2fc_destroy(struct net_device *net_device);
63 static int bnx2fc_enable(struct net_device *netdev);
64 static int bnx2fc_disable(struct net_device *netdev);
66 /* fcoe_syfs control interface handlers */
67 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
68 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
70 static void bnx2fc_recv_frame(struct sk_buff *skb);
72 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
73 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
74 static int bnx2fc_lport_config(struct fc_lport *lport);
75 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
76 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
77 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
78 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
79 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
80 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
81 struct device *parent, int npiv);
82 static void bnx2fc_destroy_work(struct work_struct *work);
84 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
85 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
86 *phys_dev);
87 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
88 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
90 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
91 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
93 static void bnx2fc_port_shutdown(struct fc_lport *lport);
94 static void bnx2fc_stop(struct bnx2fc_interface *interface);
95 static int __init bnx2fc_mod_init(void);
96 static void __exit bnx2fc_mod_exit(void);
98 unsigned int bnx2fc_debug_level;
99 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(debug_logging,
101 "Option to enable extended logging,\n"
102 "\t\tDefault is 0 - no logging.\n"
103 "\t\t0x01 - SCSI cmd error, cleanup.\n"
104 "\t\t0x02 - Session setup, cleanup, etc.\n"
105 "\t\t0x04 - lport events, link, mtu, etc.\n"
106 "\t\t0x08 - ELS logs.\n"
107 "\t\t0x10 - fcoe L2 fame related logs.\n"
108 "\t\t0xff - LOG all messages.");
110 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
111 unsigned long action, void *hcpu);
112 /* notification function for CPU hotplug events */
113 static struct notifier_block bnx2fc_cpu_notifier = {
114 .notifier_call = bnx2fc_cpu_callback,
117 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
119 return ((struct bnx2fc_interface *)
120 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
123 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
125 struct fcoe_ctlr_device *ctlr_dev =
126 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
127 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
128 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
130 fcf_dev->vlan_id = fcoe->vlan_id;
133 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
135 struct fcoe_percpu_s *bg;
136 struct fcoe_rcv_info *fr;
137 struct sk_buff_head *list;
138 struct sk_buff *skb, *next;
139 struct sk_buff *head;
141 bg = &bnx2fc_global;
142 spin_lock_bh(&bg->fcoe_rx_list.lock);
143 list = &bg->fcoe_rx_list;
144 head = list->next;
145 for (skb = head; skb != (struct sk_buff *)list;
146 skb = next) {
147 next = skb->next;
148 fr = fcoe_dev_from_skb(skb);
149 if (fr->fr_dev == lp) {
150 __skb_unlink(skb, list);
151 kfree_skb(skb);
154 spin_unlock_bh(&bg->fcoe_rx_list.lock);
157 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
159 int rc;
160 spin_lock(&bnx2fc_global_lock);
161 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
162 spin_unlock(&bnx2fc_global_lock);
164 return rc;
167 static void bnx2fc_abort_io(struct fc_lport *lport)
170 * This function is no-op for bnx2fc, but we do
171 * not want to leave it as NULL either, as libfc
172 * can call the default function which is
173 * fc_fcp_abort_io.
177 static void bnx2fc_cleanup(struct fc_lport *lport)
179 struct fcoe_port *port = lport_priv(lport);
180 struct bnx2fc_interface *interface = port->priv;
181 struct bnx2fc_hba *hba = interface->hba;
182 struct bnx2fc_rport *tgt;
183 int i;
185 BNX2FC_MISC_DBG("Entered %s\n", __func__);
186 mutex_lock(&hba->hba_mutex);
187 spin_lock_bh(&hba->hba_lock);
188 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
189 tgt = hba->tgt_ofld_list[i];
190 if (tgt) {
191 /* Cleanup IOs belonging to requested vport */
192 if (tgt->port == port) {
193 spin_unlock_bh(&hba->hba_lock);
194 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
195 bnx2fc_flush_active_ios(tgt);
196 spin_lock_bh(&hba->hba_lock);
200 spin_unlock_bh(&hba->hba_lock);
201 mutex_unlock(&hba->hba_mutex);
204 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
205 struct fc_frame *fp)
207 struct fc_rport_priv *rdata = tgt->rdata;
208 struct fc_frame_header *fh;
209 int rc = 0;
211 fh = fc_frame_header_get(fp);
212 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
213 "r_ctl = 0x%x\n", rdata->ids.port_id,
214 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
215 if ((fh->fh_type == FC_TYPE_ELS) &&
216 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
218 switch (fc_frame_payload_op(fp)) {
219 case ELS_ADISC:
220 rc = bnx2fc_send_adisc(tgt, fp);
221 break;
222 case ELS_LOGO:
223 rc = bnx2fc_send_logo(tgt, fp);
224 break;
225 case ELS_RLS:
226 rc = bnx2fc_send_rls(tgt, fp);
227 break;
228 default:
229 break;
231 } else if ((fh->fh_type == FC_TYPE_BLS) &&
232 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
233 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
234 else {
235 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
236 "rctl 0x%x thru non-offload path\n",
237 fh->fh_type, fh->fh_r_ctl);
238 return -ENODEV;
240 if (rc)
241 return -ENOMEM;
242 else
243 return 0;
247 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
249 * @lport: the associated local port
250 * @fp: the fc_frame to be transmitted
252 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
254 struct ethhdr *eh;
255 struct fcoe_crc_eof *cp;
256 struct sk_buff *skb;
257 struct fc_frame_header *fh;
258 struct bnx2fc_interface *interface;
259 struct fcoe_ctlr *ctlr;
260 struct bnx2fc_hba *hba;
261 struct fcoe_port *port;
262 struct fcoe_hdr *hp;
263 struct bnx2fc_rport *tgt;
264 struct fc_stats *stats;
265 u8 sof, eof;
266 u32 crc;
267 unsigned int hlen, tlen, elen;
268 int wlen, rc = 0;
270 port = (struct fcoe_port *)lport_priv(lport);
271 interface = port->priv;
272 ctlr = bnx2fc_to_ctlr(interface);
273 hba = interface->hba;
275 fh = fc_frame_header_get(fp);
277 skb = fp_skb(fp);
278 if (!lport->link_up) {
279 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
280 kfree_skb(skb);
281 return 0;
284 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
285 if (!ctlr->sel_fcf) {
286 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
287 kfree_skb(skb);
288 return -EINVAL;
290 if (fcoe_ctlr_els_send(ctlr, lport, skb))
291 return 0;
294 sof = fr_sof(fp);
295 eof = fr_eof(fp);
298 * Snoop the frame header to check if the frame is for
299 * an offloaded session
302 * tgt_ofld_list access is synchronized using
303 * both hba mutex and hba lock. Atleast hba mutex or
304 * hba lock needs to be held for read access.
307 spin_lock_bh(&hba->hba_lock);
308 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
309 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
310 /* This frame is for offloaded session */
311 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
312 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
313 spin_unlock_bh(&hba->hba_lock);
314 rc = bnx2fc_xmit_l2_frame(tgt, fp);
315 if (rc != -ENODEV) {
316 kfree_skb(skb);
317 return rc;
319 } else {
320 spin_unlock_bh(&hba->hba_lock);
323 elen = sizeof(struct ethhdr);
324 hlen = sizeof(struct fcoe_hdr);
325 tlen = sizeof(struct fcoe_crc_eof);
326 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
328 skb->ip_summed = CHECKSUM_NONE;
329 crc = fcoe_fc_crc(fp);
331 /* copy port crc and eof to the skb buff */
332 if (skb_is_nonlinear(skb)) {
333 skb_frag_t *frag;
334 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
335 kfree_skb(skb);
336 return -ENOMEM;
338 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
339 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
340 } else {
341 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
344 memset(cp, 0, sizeof(*cp));
345 cp->fcoe_eof = eof;
346 cp->fcoe_crc32 = cpu_to_le32(~crc);
347 if (skb_is_nonlinear(skb)) {
348 kunmap_atomic(cp);
349 cp = NULL;
352 /* adjust skb network/transport offsets to match mac/fcoe/port */
353 skb_push(skb, elen + hlen);
354 skb_reset_mac_header(skb);
355 skb_reset_network_header(skb);
356 skb->mac_len = elen;
357 skb->protocol = htons(ETH_P_FCOE);
358 skb->dev = interface->netdev;
360 /* fill up mac and fcoe headers */
361 eh = eth_hdr(skb);
362 eh->h_proto = htons(ETH_P_FCOE);
363 if (ctlr->map_dest)
364 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
365 else
366 /* insert GW address */
367 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
369 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
370 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
371 else
372 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
374 hp = (struct fcoe_hdr *)(eh + 1);
375 memset(hp, 0, sizeof(*hp));
376 if (FC_FCOE_VER)
377 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
378 hp->fcoe_sof = sof;
380 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
381 if (lport->seq_offload && fr_max_payload(fp)) {
382 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
383 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
384 } else {
385 skb_shinfo(skb)->gso_type = 0;
386 skb_shinfo(skb)->gso_size = 0;
389 /*update tx stats */
390 stats = per_cpu_ptr(lport->stats, get_cpu());
391 stats->TxFrames++;
392 stats->TxWords += wlen;
393 put_cpu();
395 /* send down to lld */
396 fr_dev(fp) = lport;
397 if (port->fcoe_pending_queue.qlen)
398 fcoe_check_wait_queue(lport, skb);
399 else if (fcoe_start_io(skb))
400 fcoe_check_wait_queue(lport, skb);
402 return 0;
406 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
408 * @skb: the receive socket buffer
409 * @dev: associated net device
410 * @ptype: context
411 * @olddev: last device
413 * This function receives the packet and builds FC frame and passes it up
415 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
416 struct packet_type *ptype, struct net_device *olddev)
418 struct fc_lport *lport;
419 struct bnx2fc_interface *interface;
420 struct fcoe_ctlr *ctlr;
421 struct fc_frame_header *fh;
422 struct fcoe_rcv_info *fr;
423 struct fcoe_percpu_s *bg;
424 struct sk_buff *tmp_skb;
425 unsigned short oxid;
427 interface = container_of(ptype, struct bnx2fc_interface,
428 fcoe_packet_type);
429 ctlr = bnx2fc_to_ctlr(interface);
430 lport = ctlr->lp;
432 if (unlikely(lport == NULL)) {
433 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
434 goto err;
437 tmp_skb = skb_share_check(skb, GFP_ATOMIC);
438 if (!tmp_skb)
439 goto err;
441 skb = tmp_skb;
443 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
444 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
445 goto err;
449 * Check for minimum frame length, and make sure required FCoE
450 * and FC headers are pulled into the linear data area.
452 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
453 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
454 goto err;
456 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
457 fh = (struct fc_frame_header *) skb_transport_header(skb);
459 oxid = ntohs(fh->fh_ox_id);
461 fr = fcoe_dev_from_skb(skb);
462 fr->fr_dev = lport;
464 bg = &bnx2fc_global;
465 spin_lock(&bg->fcoe_rx_list.lock);
467 __skb_queue_tail(&bg->fcoe_rx_list, skb);
468 if (bg->fcoe_rx_list.qlen == 1)
469 wake_up_process(bg->thread);
471 spin_unlock(&bg->fcoe_rx_list.lock);
473 return 0;
474 err:
475 kfree_skb(skb);
476 return -1;
479 static int bnx2fc_l2_rcv_thread(void *arg)
481 struct fcoe_percpu_s *bg = arg;
482 struct sk_buff *skb;
484 set_user_nice(current, MIN_NICE);
485 set_current_state(TASK_INTERRUPTIBLE);
486 while (!kthread_should_stop()) {
487 schedule();
488 spin_lock_bh(&bg->fcoe_rx_list.lock);
489 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
490 spin_unlock_bh(&bg->fcoe_rx_list.lock);
491 bnx2fc_recv_frame(skb);
492 spin_lock_bh(&bg->fcoe_rx_list.lock);
494 __set_current_state(TASK_INTERRUPTIBLE);
495 spin_unlock_bh(&bg->fcoe_rx_list.lock);
497 __set_current_state(TASK_RUNNING);
498 return 0;
502 static void bnx2fc_recv_frame(struct sk_buff *skb)
504 u32 fr_len;
505 struct fc_lport *lport;
506 struct fcoe_rcv_info *fr;
507 struct fc_stats *stats;
508 struct fc_frame_header *fh;
509 struct fcoe_crc_eof crc_eof;
510 struct fc_frame *fp;
511 struct fc_lport *vn_port;
512 struct fcoe_port *port;
513 u8 *mac = NULL;
514 u8 *dest_mac = NULL;
515 struct fcoe_hdr *hp;
517 fr = fcoe_dev_from_skb(skb);
518 lport = fr->fr_dev;
519 if (unlikely(lport == NULL)) {
520 printk(KERN_ERR PFX "Invalid lport struct\n");
521 kfree_skb(skb);
522 return;
525 if (skb_is_nonlinear(skb))
526 skb_linearize(skb);
527 mac = eth_hdr(skb)->h_source;
528 dest_mac = eth_hdr(skb)->h_dest;
530 /* Pull the header */
531 hp = (struct fcoe_hdr *) skb_network_header(skb);
532 fh = (struct fc_frame_header *) skb_transport_header(skb);
533 skb_pull(skb, sizeof(struct fcoe_hdr));
534 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
536 fp = (struct fc_frame *)skb;
537 fc_frame_init(fp);
538 fr_dev(fp) = lport;
539 fr_sof(fp) = hp->fcoe_sof;
540 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
541 kfree_skb(skb);
542 return;
544 fr_eof(fp) = crc_eof.fcoe_eof;
545 fr_crc(fp) = crc_eof.fcoe_crc32;
546 if (pskb_trim(skb, fr_len)) {
547 kfree_skb(skb);
548 return;
551 fh = fc_frame_header_get(fp);
553 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
554 if (vn_port) {
555 port = lport_priv(vn_port);
556 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
557 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
558 kfree_skb(skb);
559 return;
562 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
563 fh->fh_type == FC_TYPE_FCP) {
564 /* Drop FCP data. We dont this in L2 path */
565 kfree_skb(skb);
566 return;
568 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
569 fh->fh_type == FC_TYPE_ELS) {
570 switch (fc_frame_payload_op(fp)) {
571 case ELS_LOGO:
572 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
573 /* drop non-FIP LOGO */
574 kfree_skb(skb);
575 return;
577 break;
581 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
582 /* Drop incoming ABTS */
583 kfree_skb(skb);
584 return;
587 stats = per_cpu_ptr(lport->stats, smp_processor_id());
588 stats->RxFrames++;
589 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
591 if (le32_to_cpu(fr_crc(fp)) !=
592 ~crc32(~0, skb->data, fr_len)) {
593 if (stats->InvalidCRCCount < 5)
594 printk(KERN_WARNING PFX "dropping frame with "
595 "CRC error\n");
596 stats->InvalidCRCCount++;
597 kfree_skb(skb);
598 return;
600 fc_exch_recv(lport, fp);
604 * bnx2fc_percpu_io_thread - thread per cpu for ios
606 * @arg: ptr to bnx2fc_percpu_info structure
608 int bnx2fc_percpu_io_thread(void *arg)
610 struct bnx2fc_percpu_s *p = arg;
611 struct bnx2fc_work *work, *tmp;
612 LIST_HEAD(work_list);
614 set_user_nice(current, MIN_NICE);
615 set_current_state(TASK_INTERRUPTIBLE);
616 while (!kthread_should_stop()) {
617 schedule();
618 spin_lock_bh(&p->fp_work_lock);
619 while (!list_empty(&p->work_list)) {
620 list_splice_init(&p->work_list, &work_list);
621 spin_unlock_bh(&p->fp_work_lock);
623 list_for_each_entry_safe(work, tmp, &work_list, list) {
624 list_del_init(&work->list);
625 bnx2fc_process_cq_compl(work->tgt, work->wqe);
626 kfree(work);
629 spin_lock_bh(&p->fp_work_lock);
631 __set_current_state(TASK_INTERRUPTIBLE);
632 spin_unlock_bh(&p->fp_work_lock);
634 __set_current_state(TASK_RUNNING);
636 return 0;
639 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
641 struct fc_host_statistics *bnx2fc_stats;
642 struct fc_lport *lport = shost_priv(shost);
643 struct fcoe_port *port = lport_priv(lport);
644 struct bnx2fc_interface *interface = port->priv;
645 struct bnx2fc_hba *hba = interface->hba;
646 struct fcoe_statistics_params *fw_stats;
647 int rc = 0;
649 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
650 if (!fw_stats)
651 return NULL;
653 bnx2fc_stats = fc_get_host_stats(shost);
655 init_completion(&hba->stat_req_done);
656 if (bnx2fc_send_stat_req(hba))
657 return bnx2fc_stats;
658 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
659 if (!rc) {
660 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
661 return bnx2fc_stats;
663 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
664 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
665 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
666 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
667 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
668 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
669 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
670 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
671 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
672 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
674 bnx2fc_stats->dumped_frames = 0;
675 bnx2fc_stats->lip_count = 0;
676 bnx2fc_stats->nos_count = 0;
677 bnx2fc_stats->loss_of_sync_count = 0;
678 bnx2fc_stats->loss_of_signal_count = 0;
679 bnx2fc_stats->prim_seq_protocol_err_count = 0;
681 memcpy(&hba->prev_stats, hba->stats_buffer,
682 sizeof(struct fcoe_statistics_params));
683 return bnx2fc_stats;
686 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
688 struct fcoe_port *port = lport_priv(lport);
689 struct bnx2fc_interface *interface = port->priv;
690 struct bnx2fc_hba *hba = interface->hba;
691 struct Scsi_Host *shost = lport->host;
692 int rc = 0;
694 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
695 shost->max_lun = BNX2FC_MAX_LUN;
696 shost->max_id = BNX2FC_MAX_FCP_TGT;
697 shost->max_channel = 0;
698 if (lport->vport)
699 shost->transportt = bnx2fc_vport_xport_template;
700 else
701 shost->transportt = bnx2fc_transport_template;
703 /* Add the new host to SCSI-ml */
704 rc = scsi_add_host(lport->host, dev);
705 if (rc) {
706 printk(KERN_ERR PFX "Error on scsi_add_host\n");
707 return rc;
709 if (!lport->vport)
710 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
711 snprintf(fc_host_symbolic_name(lport->host), 256,
712 "%s (QLogic %s) v%s over %s",
713 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
714 interface->netdev->name);
716 return 0;
719 static int bnx2fc_link_ok(struct fc_lport *lport)
721 struct fcoe_port *port = lport_priv(lport);
722 struct bnx2fc_interface *interface = port->priv;
723 struct bnx2fc_hba *hba = interface->hba;
724 struct net_device *dev = hba->phys_dev;
725 int rc = 0;
727 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
728 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
729 else {
730 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
731 rc = -1;
733 return rc;
737 * bnx2fc_get_link_state - get network link state
739 * @hba: adapter instance pointer
741 * updates adapter structure flag based on netdev state
743 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
745 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
746 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
747 else
748 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
751 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
753 struct bnx2fc_hba *hba;
754 struct bnx2fc_interface *interface;
755 struct fcoe_ctlr *ctlr;
756 struct fcoe_port *port;
757 u64 wwnn, wwpn;
759 port = lport_priv(lport);
760 interface = port->priv;
761 ctlr = bnx2fc_to_ctlr(interface);
762 hba = interface->hba;
764 /* require support for get_pauseparam ethtool op. */
765 if (!hba->phys_dev->ethtool_ops ||
766 !hba->phys_dev->ethtool_ops->get_pauseparam)
767 return -EOPNOTSUPP;
769 if (fc_set_mfs(lport, BNX2FC_MFS))
770 return -EINVAL;
772 skb_queue_head_init(&port->fcoe_pending_queue);
773 port->fcoe_pending_queue_active = 0;
774 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
776 fcoe_link_speed_update(lport);
778 if (!lport->vport) {
779 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
780 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
781 1, 0);
782 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
783 fc_set_wwnn(lport, wwnn);
785 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
786 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
787 2, 0);
789 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
790 fc_set_wwpn(lport, wwpn);
793 return 0;
796 static void bnx2fc_destroy_timer(unsigned long data)
798 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
800 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
801 "Destroy compl not received!!\n");
802 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
803 wake_up_interruptible(&hba->destroy_wait);
807 * bnx2fc_indicate_netevent - Generic netdev event handler
809 * @context: adapter structure pointer
810 * @event: event type
811 * @vlan_id: vlan id - associated vlan id with this event
813 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
814 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
816 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
817 u16 vlan_id)
819 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
820 struct fcoe_ctlr_device *cdev;
821 struct fc_lport *lport;
822 struct fc_lport *vport;
823 struct bnx2fc_interface *interface, *tmp;
824 struct fcoe_ctlr *ctlr;
825 int wait_for_upload = 0;
826 u32 link_possible = 1;
828 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
829 return;
831 switch (event) {
832 case NETDEV_UP:
833 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
834 printk(KERN_ERR "indicate_netevent: "\
835 "hba is not UP!!\n");
836 break;
838 case NETDEV_DOWN:
839 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
840 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
841 link_possible = 0;
842 break;
844 case NETDEV_GOING_DOWN:
845 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
846 link_possible = 0;
847 break;
849 case NETDEV_CHANGE:
850 break;
852 case NETDEV_UNREGISTER:
853 if (!vlan_id)
854 return;
855 mutex_lock(&bnx2fc_dev_lock);
856 list_for_each_entry_safe(interface, tmp, &if_list, list) {
857 if (interface->hba == hba &&
858 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
859 __bnx2fc_destroy(interface);
861 mutex_unlock(&bnx2fc_dev_lock);
863 /* Ensure ALL destroy work has been completed before return */
864 flush_workqueue(bnx2fc_wq);
865 return;
867 default:
868 return;
871 mutex_lock(&bnx2fc_dev_lock);
872 list_for_each_entry(interface, &if_list, list) {
874 if (interface->hba != hba)
875 continue;
877 ctlr = bnx2fc_to_ctlr(interface);
878 lport = ctlr->lp;
879 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
880 interface->netdev->name, event);
882 fcoe_link_speed_update(lport);
884 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
886 if (link_possible && !bnx2fc_link_ok(lport)) {
887 switch (cdev->enabled) {
888 case FCOE_CTLR_DISABLED:
889 pr_info("Link up while interface is disabled.\n");
890 break;
891 case FCOE_CTLR_ENABLED:
892 case FCOE_CTLR_UNUSED:
893 /* Reset max recv frame size to default */
894 fc_set_mfs(lport, BNX2FC_MFS);
896 * ctlr link up will only be handled during
897 * enable to avoid sending discovery
898 * solicitation on a stale vlan
900 if (interface->enabled)
901 fcoe_ctlr_link_up(ctlr);
903 } else if (fcoe_ctlr_link_down(ctlr)) {
904 switch (cdev->enabled) {
905 case FCOE_CTLR_DISABLED:
906 pr_info("Link down while interface is disabled.\n");
907 break;
908 case FCOE_CTLR_ENABLED:
909 case FCOE_CTLR_UNUSED:
910 mutex_lock(&lport->lp_mutex);
911 list_for_each_entry(vport, &lport->vports, list)
912 fc_host_port_type(vport->host) =
913 FC_PORTTYPE_UNKNOWN;
914 mutex_unlock(&lport->lp_mutex);
915 fc_host_port_type(lport->host) =
916 FC_PORTTYPE_UNKNOWN;
917 per_cpu_ptr(lport->stats,
918 get_cpu())->LinkFailureCount++;
919 put_cpu();
920 fcoe_clean_pending_queue(lport);
921 wait_for_upload = 1;
925 mutex_unlock(&bnx2fc_dev_lock);
927 if (wait_for_upload) {
928 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
929 init_waitqueue_head(&hba->shutdown_wait);
930 BNX2FC_MISC_DBG("indicate_netevent "
931 "num_ofld_sess = %d\n",
932 hba->num_ofld_sess);
933 hba->wait_for_link_down = 1;
934 wait_event_interruptible(hba->shutdown_wait,
935 (hba->num_ofld_sess == 0));
936 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
937 hba->num_ofld_sess);
938 hba->wait_for_link_down = 0;
940 if (signal_pending(current))
941 flush_signals(current);
945 static int bnx2fc_libfc_config(struct fc_lport *lport)
948 /* Set the function pointers set by bnx2fc driver */
949 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
950 sizeof(struct libfc_function_template));
951 fc_elsct_init(lport);
952 fc_exch_init(lport);
953 fc_rport_init(lport);
954 fc_disc_init(lport);
955 fc_disc_config(lport, lport);
956 return 0;
959 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
961 int fcoe_min_xid, fcoe_max_xid;
963 fcoe_min_xid = hba->max_xid + 1;
964 if (nr_cpu_ids <= 2)
965 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
966 else
967 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
968 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
969 fcoe_max_xid, NULL)) {
970 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
971 return -ENOMEM;
974 return 0;
977 static int bnx2fc_lport_config(struct fc_lport *lport)
979 lport->link_up = 0;
980 lport->qfull = 0;
981 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
982 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
983 lport->e_d_tov = 2 * 1000;
984 lport->r_a_tov = 10 * 1000;
986 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
987 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
988 lport->does_npiv = 1;
990 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
991 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
993 /* alloc stats structure */
994 if (fc_lport_init_stats(lport))
995 return -ENOMEM;
997 /* Finish fc_lport configuration */
998 fc_lport_config(lport);
1000 return 0;
1004 * bnx2fc_fip_recv - handle a received FIP frame.
1006 * @skb: the received skb
1007 * @dev: associated &net_device
1008 * @ptype: the &packet_type structure which was used to register this handler.
1009 * @orig_dev: original receive &net_device, in case @ dev is a bond.
1011 * Returns: 0 for success
1013 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1014 struct packet_type *ptype,
1015 struct net_device *orig_dev)
1017 struct bnx2fc_interface *interface;
1018 struct fcoe_ctlr *ctlr;
1019 interface = container_of(ptype, struct bnx2fc_interface,
1020 fip_packet_type);
1021 ctlr = bnx2fc_to_ctlr(interface);
1022 fcoe_ctlr_recv(ctlr, skb);
1023 return 0;
1027 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1029 * @fip: FCoE controller.
1030 * @old: Unicast MAC address to delete if the MAC is non-zero.
1031 * @new: Unicast MAC address to add.
1033 * Remove any previously-set unicast MAC filter.
1034 * Add secondary FCoE MAC address filter for our OUI.
1036 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1038 struct fcoe_port *port = lport_priv(lport);
1040 memcpy(port->data_src_addr, addr, ETH_ALEN);
1044 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1046 * @lport: libfc port
1048 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1050 struct fcoe_port *port;
1052 port = (struct fcoe_port *)lport_priv(lport);
1053 return port->data_src_addr;
1057 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1059 * @fip: FCoE controller.
1060 * @skb: FIP Packet.
1062 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1064 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1065 dev_queue_xmit(skb);
1068 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1070 struct Scsi_Host *shost = vport_to_shost(vport);
1071 struct fc_lport *n_port = shost_priv(shost);
1072 struct fcoe_port *port = lport_priv(n_port);
1073 struct bnx2fc_interface *interface = port->priv;
1074 struct net_device *netdev = interface->netdev;
1075 struct fc_lport *vn_port;
1076 int rc;
1077 char buf[32];
1079 rc = fcoe_validate_vport_create(vport);
1080 if (rc) {
1081 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1082 printk(KERN_ERR PFX "Failed to create vport, "
1083 "WWPN (0x%s) already exists\n",
1084 buf);
1085 return rc;
1088 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1089 printk(KERN_ERR PFX "vn ports cannot be created on"
1090 "this interface\n");
1091 return -EIO;
1093 rtnl_lock();
1094 mutex_lock(&bnx2fc_dev_lock);
1095 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1096 mutex_unlock(&bnx2fc_dev_lock);
1097 rtnl_unlock();
1099 if (!vn_port) {
1100 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1101 netdev->name);
1102 return -EIO;
1105 if (disabled) {
1106 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1107 } else {
1108 vn_port->boot_time = jiffies;
1109 fc_lport_init(vn_port);
1110 fc_fabric_login(vn_port);
1111 fc_vport_setlink(vn_port);
1113 return 0;
1116 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1118 struct bnx2fc_lport *blport, *tmp;
1120 spin_lock_bh(&hba->hba_lock);
1121 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1122 if (blport->lport == lport) {
1123 list_del(&blport->list);
1124 kfree(blport);
1127 spin_unlock_bh(&hba->hba_lock);
1130 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1132 struct Scsi_Host *shost = vport_to_shost(vport);
1133 struct fc_lport *n_port = shost_priv(shost);
1134 struct fc_lport *vn_port = vport->dd_data;
1135 struct fcoe_port *port = lport_priv(vn_port);
1136 struct bnx2fc_interface *interface = port->priv;
1137 struct fc_lport *v_port;
1138 bool found = false;
1140 mutex_lock(&n_port->lp_mutex);
1141 list_for_each_entry(v_port, &n_port->vports, list)
1142 if (v_port->vport == vport) {
1143 found = true;
1144 break;
1147 if (!found) {
1148 mutex_unlock(&n_port->lp_mutex);
1149 return -ENOENT;
1151 list_del(&vn_port->list);
1152 mutex_unlock(&n_port->lp_mutex);
1153 bnx2fc_free_vport(interface->hba, port->lport);
1154 bnx2fc_port_shutdown(port->lport);
1155 bnx2fc_interface_put(interface);
1156 queue_work(bnx2fc_wq, &port->destroy_work);
1157 return 0;
1160 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1162 struct fc_lport *lport = vport->dd_data;
1164 if (disable) {
1165 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1166 fc_fabric_logoff(lport);
1167 } else {
1168 lport->boot_time = jiffies;
1169 fc_fabric_login(lport);
1170 fc_vport_setlink(lport);
1172 return 0;
1176 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1178 struct net_device *netdev = interface->netdev;
1179 struct net_device *physdev = interface->hba->phys_dev;
1180 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1181 struct netdev_hw_addr *ha;
1182 int sel_san_mac = 0;
1184 /* setup Source MAC Address */
1185 rcu_read_lock();
1186 for_each_dev_addr(physdev, ha) {
1187 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1188 ha->type);
1189 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1190 ha->addr[1], ha->addr[2], ha->addr[3],
1191 ha->addr[4], ha->addr[5]);
1193 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1194 (is_valid_ether_addr(ha->addr))) {
1195 memcpy(ctlr->ctl_src_addr, ha->addr,
1196 ETH_ALEN);
1197 sel_san_mac = 1;
1198 BNX2FC_MISC_DBG("Found SAN MAC\n");
1201 rcu_read_unlock();
1203 if (!sel_san_mac)
1204 return -ENODEV;
1206 interface->fip_packet_type.func = bnx2fc_fip_recv;
1207 interface->fip_packet_type.type = htons(ETH_P_FIP);
1208 interface->fip_packet_type.dev = netdev;
1209 dev_add_pack(&interface->fip_packet_type);
1211 interface->fcoe_packet_type.func = bnx2fc_rcv;
1212 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1213 interface->fcoe_packet_type.dev = netdev;
1214 dev_add_pack(&interface->fcoe_packet_type);
1216 return 0;
1219 static int bnx2fc_attach_transport(void)
1221 bnx2fc_transport_template =
1222 fc_attach_transport(&bnx2fc_transport_function);
1224 if (bnx2fc_transport_template == NULL) {
1225 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1226 return -ENODEV;
1229 bnx2fc_vport_xport_template =
1230 fc_attach_transport(&bnx2fc_vport_xport_function);
1231 if (bnx2fc_vport_xport_template == NULL) {
1232 printk(KERN_ERR PFX
1233 "Failed to attach FC transport for vport\n");
1234 fc_release_transport(bnx2fc_transport_template);
1235 bnx2fc_transport_template = NULL;
1236 return -ENODEV;
1238 return 0;
1240 static void bnx2fc_release_transport(void)
1242 fc_release_transport(bnx2fc_transport_template);
1243 fc_release_transport(bnx2fc_vport_xport_template);
1244 bnx2fc_transport_template = NULL;
1245 bnx2fc_vport_xport_template = NULL;
1248 static void bnx2fc_interface_release(struct kref *kref)
1250 struct fcoe_ctlr_device *ctlr_dev;
1251 struct bnx2fc_interface *interface;
1252 struct fcoe_ctlr *ctlr;
1253 struct net_device *netdev;
1255 interface = container_of(kref, struct bnx2fc_interface, kref);
1256 BNX2FC_MISC_DBG("Interface is being released\n");
1258 ctlr = bnx2fc_to_ctlr(interface);
1259 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1260 netdev = interface->netdev;
1262 /* tear-down FIP controller */
1263 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1264 fcoe_ctlr_destroy(ctlr);
1266 fcoe_ctlr_device_delete(ctlr_dev);
1268 dev_put(netdev);
1269 module_put(THIS_MODULE);
1272 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1274 kref_get(&interface->kref);
1277 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1279 kref_put(&interface->kref, bnx2fc_interface_release);
1281 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1283 /* Free the command manager */
1284 if (hba->cmd_mgr) {
1285 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1286 hba->cmd_mgr = NULL;
1288 kfree(hba->tgt_ofld_list);
1289 bnx2fc_unbind_pcidev(hba);
1290 kfree(hba);
1294 * bnx2fc_hba_create - create a new bnx2fc hba
1296 * @cnic: pointer to cnic device
1298 * Creates a new FCoE hba on the given device.
1301 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1303 struct bnx2fc_hba *hba;
1304 struct fcoe_capabilities *fcoe_cap;
1305 int rc;
1307 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1308 if (!hba) {
1309 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1310 return NULL;
1312 spin_lock_init(&hba->hba_lock);
1313 mutex_init(&hba->hba_mutex);
1315 hba->cnic = cnic;
1317 hba->max_tasks = cnic->max_fcoe_exchanges;
1318 hba->elstm_xids = (hba->max_tasks / 2);
1319 hba->max_outstanding_cmds = hba->elstm_xids;
1320 hba->max_xid = (hba->max_tasks - 1);
1322 rc = bnx2fc_bind_pcidev(hba);
1323 if (rc) {
1324 printk(KERN_ERR PFX "create_adapter: bind error\n");
1325 goto bind_err;
1327 hba->phys_dev = cnic->netdev;
1328 hba->next_conn_id = 0;
1330 hba->tgt_ofld_list =
1331 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1332 GFP_KERNEL);
1333 if (!hba->tgt_ofld_list) {
1334 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1335 goto tgtofld_err;
1338 hba->num_ofld_sess = 0;
1340 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1341 if (!hba->cmd_mgr) {
1342 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1343 goto cmgr_err;
1345 fcoe_cap = &hba->fcoe_cap;
1347 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1348 FCOE_IOS_PER_CONNECTION_SHIFT;
1349 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1350 FCOE_LOGINS_PER_PORT_SHIFT;
1351 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1352 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1353 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1354 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1355 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1356 FCOE_TARGETS_SUPPORTED_SHIFT;
1357 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1358 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1359 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1361 init_waitqueue_head(&hba->shutdown_wait);
1362 init_waitqueue_head(&hba->destroy_wait);
1363 INIT_LIST_HEAD(&hba->vports);
1365 return hba;
1367 cmgr_err:
1368 kfree(hba->tgt_ofld_list);
1369 tgtofld_err:
1370 bnx2fc_unbind_pcidev(hba);
1371 bind_err:
1372 kfree(hba);
1373 return NULL;
1376 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1377 struct net_device *netdev,
1378 enum fip_state fip_mode)
1380 struct fcoe_ctlr_device *ctlr_dev;
1381 struct bnx2fc_interface *interface;
1382 struct fcoe_ctlr *ctlr;
1383 int size;
1384 int rc = 0;
1386 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1387 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1388 size);
1389 if (!ctlr_dev) {
1390 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1391 return NULL;
1393 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1394 ctlr->cdev = ctlr_dev;
1395 interface = fcoe_ctlr_priv(ctlr);
1396 dev_hold(netdev);
1397 kref_init(&interface->kref);
1398 interface->hba = hba;
1399 interface->netdev = netdev;
1401 /* Initialize FIP */
1402 fcoe_ctlr_init(ctlr, fip_mode);
1403 ctlr->send = bnx2fc_fip_send;
1404 ctlr->update_mac = bnx2fc_update_src_mac;
1405 ctlr->get_src_addr = bnx2fc_get_src_mac;
1406 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1408 rc = bnx2fc_interface_setup(interface);
1409 if (!rc)
1410 return interface;
1412 fcoe_ctlr_destroy(ctlr);
1413 dev_put(netdev);
1414 fcoe_ctlr_device_delete(ctlr_dev);
1415 return NULL;
1419 * bnx2fc_if_create - Create FCoE instance on a given interface
1421 * @interface: FCoE interface to create a local port on
1422 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1423 * @npiv: Indicates if the port is vport or not
1425 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1427 * Returns: Allocated fc_lport or an error pointer
1429 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1430 struct device *parent, int npiv)
1432 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1433 struct fc_lport *lport, *n_port;
1434 struct fcoe_port *port;
1435 struct Scsi_Host *shost;
1436 struct fc_vport *vport = dev_to_vport(parent);
1437 struct bnx2fc_lport *blport;
1438 struct bnx2fc_hba *hba = interface->hba;
1439 int rc = 0;
1441 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1442 if (!blport) {
1443 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1444 return NULL;
1447 /* Allocate Scsi_Host structure */
1448 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1449 if (!npiv)
1450 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1451 else
1452 lport = libfc_vport_create(vport, sizeof(*port));
1454 if (!lport) {
1455 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1456 goto free_blport;
1458 shost = lport->host;
1459 port = lport_priv(lport);
1460 port->lport = lport;
1461 port->priv = interface;
1462 port->get_netdev = bnx2fc_netdev;
1463 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1465 /* Configure fcoe_port */
1466 rc = bnx2fc_lport_config(lport);
1467 if (rc)
1468 goto lp_config_err;
1470 if (npiv) {
1471 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1472 vport->node_name, vport->port_name);
1473 fc_set_wwnn(lport, vport->node_name);
1474 fc_set_wwpn(lport, vport->port_name);
1476 /* Configure netdev and networking properties of the lport */
1477 rc = bnx2fc_net_config(lport, interface->netdev);
1478 if (rc) {
1479 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1480 goto lp_config_err;
1483 rc = bnx2fc_shost_config(lport, parent);
1484 if (rc) {
1485 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1486 interface->netdev->name);
1487 goto lp_config_err;
1490 /* Initialize the libfc library */
1491 rc = bnx2fc_libfc_config(lport);
1492 if (rc) {
1493 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1494 goto shost_err;
1496 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1498 /* Allocate exchange manager */
1499 if (!npiv)
1500 rc = bnx2fc_em_config(lport, hba);
1501 else {
1502 shost = vport_to_shost(vport);
1503 n_port = shost_priv(shost);
1504 rc = fc_exch_mgr_list_clone(n_port, lport);
1507 if (rc) {
1508 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1509 goto shost_err;
1512 bnx2fc_interface_get(interface);
1514 spin_lock_bh(&hba->hba_lock);
1515 blport->lport = lport;
1516 list_add_tail(&blport->list, &hba->vports);
1517 spin_unlock_bh(&hba->hba_lock);
1519 return lport;
1521 shost_err:
1522 scsi_remove_host(shost);
1523 lp_config_err:
1524 scsi_host_put(lport->host);
1525 free_blport:
1526 kfree(blport);
1527 return NULL;
1530 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1532 /* Dont listen for Ethernet packets anymore */
1533 __dev_remove_pack(&interface->fcoe_packet_type);
1534 __dev_remove_pack(&interface->fip_packet_type);
1535 synchronize_net();
1538 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1540 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1541 struct fc_lport *lport = ctlr->lp;
1542 struct fcoe_port *port = lport_priv(lport);
1543 struct bnx2fc_hba *hba = interface->hba;
1545 /* Stop the transmit retry timer */
1546 del_timer_sync(&port->timer);
1548 /* Free existing transmit skbs */
1549 fcoe_clean_pending_queue(lport);
1551 bnx2fc_net_cleanup(interface);
1553 bnx2fc_free_vport(hba, lport);
1556 static void bnx2fc_if_destroy(struct fc_lport *lport)
1559 /* Free queued packets for the receive thread */
1560 bnx2fc_clean_rx_queue(lport);
1562 /* Detach from scsi-ml */
1563 fc_remove_host(lport->host);
1564 scsi_remove_host(lport->host);
1567 * Note that only the physical lport will have the exchange manager.
1568 * for vports, this function is NOP
1570 fc_exch_mgr_free(lport);
1572 /* Free memory used by statistical counters */
1573 fc_lport_free_stats(lport);
1575 /* Release Scsi_Host */
1576 scsi_host_put(lport->host);
1579 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1581 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1582 struct fc_lport *lport = ctlr->lp;
1583 struct fcoe_port *port = lport_priv(lport);
1585 bnx2fc_interface_cleanup(interface);
1586 bnx2fc_stop(interface);
1587 list_del(&interface->list);
1588 bnx2fc_interface_put(interface);
1589 queue_work(bnx2fc_wq, &port->destroy_work);
1593 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1595 * @buffer: The name of the Ethernet interface to be destroyed
1596 * @kp: The associated kernel parameter
1598 * Called from sysfs.
1600 * Returns: 0 for success
1602 static int bnx2fc_destroy(struct net_device *netdev)
1604 struct bnx2fc_interface *interface = NULL;
1605 struct workqueue_struct *timer_work_queue;
1606 struct fcoe_ctlr *ctlr;
1607 int rc = 0;
1609 rtnl_lock();
1610 mutex_lock(&bnx2fc_dev_lock);
1612 interface = bnx2fc_interface_lookup(netdev);
1613 ctlr = bnx2fc_to_ctlr(interface);
1614 if (!interface || !ctlr->lp) {
1615 rc = -ENODEV;
1616 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1617 goto netdev_err;
1620 timer_work_queue = interface->timer_work_queue;
1621 __bnx2fc_destroy(interface);
1622 destroy_workqueue(timer_work_queue);
1624 netdev_err:
1625 mutex_unlock(&bnx2fc_dev_lock);
1626 rtnl_unlock();
1627 return rc;
1630 static void bnx2fc_destroy_work(struct work_struct *work)
1632 struct fcoe_port *port;
1633 struct fc_lport *lport;
1635 port = container_of(work, struct fcoe_port, destroy_work);
1636 lport = port->lport;
1638 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1640 bnx2fc_if_destroy(lport);
1643 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1645 bnx2fc_free_fw_resc(hba);
1646 bnx2fc_free_task_ctx(hba);
1650 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1651 * pci structure
1653 * @hba: Adapter instance
1655 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1657 if (bnx2fc_setup_task_ctx(hba))
1658 goto mem_err;
1660 if (bnx2fc_setup_fw_resc(hba))
1661 goto mem_err;
1663 return 0;
1664 mem_err:
1665 bnx2fc_unbind_adapter_devices(hba);
1666 return -ENOMEM;
1669 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1671 struct cnic_dev *cnic;
1672 struct pci_dev *pdev;
1674 if (!hba->cnic) {
1675 printk(KERN_ERR PFX "cnic is NULL\n");
1676 return -ENODEV;
1678 cnic = hba->cnic;
1679 pdev = hba->pcidev = cnic->pcidev;
1680 if (!hba->pcidev)
1681 return -ENODEV;
1683 switch (pdev->device) {
1684 case PCI_DEVICE_ID_NX2_57710:
1685 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1686 break;
1687 case PCI_DEVICE_ID_NX2_57711:
1688 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1689 break;
1690 case PCI_DEVICE_ID_NX2_57712:
1691 case PCI_DEVICE_ID_NX2_57712_MF:
1692 case PCI_DEVICE_ID_NX2_57712_VF:
1693 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1694 break;
1695 case PCI_DEVICE_ID_NX2_57800:
1696 case PCI_DEVICE_ID_NX2_57800_MF:
1697 case PCI_DEVICE_ID_NX2_57800_VF:
1698 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1699 break;
1700 case PCI_DEVICE_ID_NX2_57810:
1701 case PCI_DEVICE_ID_NX2_57810_MF:
1702 case PCI_DEVICE_ID_NX2_57810_VF:
1703 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1704 break;
1705 case PCI_DEVICE_ID_NX2_57840:
1706 case PCI_DEVICE_ID_NX2_57840_MF:
1707 case PCI_DEVICE_ID_NX2_57840_VF:
1708 case PCI_DEVICE_ID_NX2_57840_2_20:
1709 case PCI_DEVICE_ID_NX2_57840_4_10:
1710 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1711 break;
1712 default:
1713 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1714 break;
1716 pci_dev_get(hba->pcidev);
1717 return 0;
1720 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1722 if (hba->pcidev) {
1723 hba->chip_num[0] = '\0';
1724 pci_dev_put(hba->pcidev);
1726 hba->pcidev = NULL;
1730 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1732 * @handle: transport handle pointing to adapter struture
1734 static int bnx2fc_ulp_get_stats(void *handle)
1736 struct bnx2fc_hba *hba = handle;
1737 struct cnic_dev *cnic;
1738 struct fcoe_stats_info *stats_addr;
1740 if (!hba)
1741 return -EINVAL;
1743 cnic = hba->cnic;
1744 stats_addr = &cnic->stats_addr->fcoe_stat;
1745 if (!stats_addr)
1746 return -EINVAL;
1748 strncpy(stats_addr->version, BNX2FC_VERSION,
1749 sizeof(stats_addr->version));
1750 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1751 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1753 return 0;
1758 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1760 * @handle: transport handle pointing to adapter structure
1762 * This function maps adapter structure to pcidev structure and initiates
1763 * firmware handshake to enable/initialize on-chip FCoE components.
1764 * This bnx2fc - cnic interface api callback is used after following
1765 * conditions are met -
1766 * a) underlying network interface is up (marked by event NETDEV_UP
1767 * from netdev
1768 * b) bnx2fc adatper structure is registered.
1770 static void bnx2fc_ulp_start(void *handle)
1772 struct bnx2fc_hba *hba = handle;
1773 struct bnx2fc_interface *interface;
1774 struct fcoe_ctlr *ctlr;
1775 struct fc_lport *lport;
1777 mutex_lock(&bnx2fc_dev_lock);
1779 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1780 bnx2fc_fw_init(hba);
1782 BNX2FC_MISC_DBG("bnx2fc started.\n");
1784 list_for_each_entry(interface, &if_list, list) {
1785 if (interface->hba == hba) {
1786 ctlr = bnx2fc_to_ctlr(interface);
1787 lport = ctlr->lp;
1788 /* Kick off Fabric discovery*/
1789 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1790 lport->tt.frame_send = bnx2fc_xmit;
1791 bnx2fc_start_disc(interface);
1795 mutex_unlock(&bnx2fc_dev_lock);
1798 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1800 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1801 fc_fabric_logoff(lport);
1802 fc_lport_destroy(lport);
1805 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1807 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1808 struct fc_lport *lport;
1809 struct fc_lport *vport;
1811 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1812 return;
1814 lport = ctlr->lp;
1815 bnx2fc_port_shutdown(lport);
1817 mutex_lock(&lport->lp_mutex);
1818 list_for_each_entry(vport, &lport->vports, list)
1819 fc_host_port_type(vport->host) =
1820 FC_PORTTYPE_UNKNOWN;
1821 mutex_unlock(&lport->lp_mutex);
1822 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1823 fcoe_ctlr_link_down(ctlr);
1824 fcoe_clean_pending_queue(lport);
1827 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1829 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1830 int rc = -1;
1831 int i = HZ;
1833 rc = bnx2fc_bind_adapter_devices(hba);
1834 if (rc) {
1835 printk(KERN_ALERT PFX
1836 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1837 goto err_out;
1840 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1841 if (rc) {
1842 printk(KERN_ALERT PFX
1843 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1844 goto err_unbind;
1848 * Wait until the adapter init message is complete, and adapter
1849 * state is UP.
1851 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1852 msleep(BNX2FC_INIT_POLL_TIME);
1854 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1855 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1856 "Ignoring...\n",
1857 hba->cnic->netdev->name);
1858 rc = -1;
1859 goto err_unbind;
1863 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1864 return 0;
1866 err_unbind:
1867 bnx2fc_unbind_adapter_devices(hba);
1868 err_out:
1869 return rc;
1872 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1874 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1875 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1876 init_timer(&hba->destroy_timer);
1877 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1878 jiffies;
1879 hba->destroy_timer.function = bnx2fc_destroy_timer;
1880 hba->destroy_timer.data = (unsigned long)hba;
1881 add_timer(&hba->destroy_timer);
1882 wait_event_interruptible(hba->destroy_wait,
1883 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1884 &hba->flags));
1885 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1886 /* This should never happen */
1887 if (signal_pending(current))
1888 flush_signals(current);
1890 del_timer_sync(&hba->destroy_timer);
1892 bnx2fc_unbind_adapter_devices(hba);
1897 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1899 * @handle: transport handle pointing to adapter structure
1901 * Driver checks if adapter is already in shutdown mode, if not start
1902 * the shutdown process.
1904 static void bnx2fc_ulp_stop(void *handle)
1906 struct bnx2fc_hba *hba = handle;
1907 struct bnx2fc_interface *interface;
1909 printk(KERN_ERR "ULP_STOP\n");
1911 mutex_lock(&bnx2fc_dev_lock);
1912 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1913 goto exit;
1914 list_for_each_entry(interface, &if_list, list) {
1915 if (interface->hba == hba)
1916 bnx2fc_stop(interface);
1918 BUG_ON(hba->num_ofld_sess != 0);
1920 mutex_lock(&hba->hba_mutex);
1921 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1922 clear_bit(ADAPTER_STATE_GOING_DOWN,
1923 &hba->adapter_state);
1925 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1926 mutex_unlock(&hba->hba_mutex);
1928 bnx2fc_fw_destroy(hba);
1929 exit:
1930 mutex_unlock(&bnx2fc_dev_lock);
1933 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1935 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1936 struct fc_lport *lport;
1937 int wait_cnt = 0;
1939 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1940 /* Kick off FIP/FLOGI */
1941 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1942 printk(KERN_ERR PFX "Init not done yet\n");
1943 return;
1946 lport = ctlr->lp;
1947 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1949 if (!bnx2fc_link_ok(lport) && interface->enabled) {
1950 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1951 fcoe_ctlr_link_up(ctlr);
1952 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1953 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1956 /* wait for the FCF to be selected before issuing FLOGI */
1957 while (!ctlr->sel_fcf) {
1958 msleep(250);
1959 /* give up after 3 secs */
1960 if (++wait_cnt > 12)
1961 break;
1964 /* Reset max receive frame size to default */
1965 if (fc_set_mfs(lport, BNX2FC_MFS))
1966 return;
1968 fc_lport_init(lport);
1969 fc_fabric_login(lport);
1974 * bnx2fc_ulp_init - Initialize an adapter instance
1976 * @dev : cnic device handle
1977 * Called from cnic_register_driver() context to initialize all
1978 * enumerated cnic devices. This routine allocates adapter structure
1979 * and other device specific resources.
1981 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1983 struct bnx2fc_hba *hba;
1984 int rc = 0;
1986 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1987 /* bnx2fc works only when bnx2x is loaded */
1988 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1989 (dev->max_fcoe_conn == 0)) {
1990 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1991 " flags: %lx fcoe_conn: %d\n",
1992 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1993 return;
1996 hba = bnx2fc_hba_create(dev);
1997 if (!hba) {
1998 printk(KERN_ERR PFX "hba initialization failed\n");
1999 return;
2002 /* Add HBA to the adapter list */
2003 mutex_lock(&bnx2fc_dev_lock);
2004 list_add_tail(&hba->list, &adapter_list);
2005 adapter_count++;
2006 mutex_unlock(&bnx2fc_dev_lock);
2008 dev->fcoe_cap = &hba->fcoe_cap;
2009 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2010 rc = dev->register_device(dev, CNIC_ULP_FCOE,
2011 (void *) hba);
2012 if (rc)
2013 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2014 else
2015 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2018 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2019 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2021 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2023 if (interface->enabled == true) {
2024 if (!ctlr->lp) {
2025 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2026 return -ENODEV;
2027 } else {
2028 interface->enabled = false;
2029 fcoe_ctlr_link_down(ctlr);
2030 fcoe_clean_pending_queue(ctlr->lp);
2033 return 0;
2037 * Deperecated: Use bnx2fc_enabled()
2039 static int bnx2fc_disable(struct net_device *netdev)
2041 struct bnx2fc_interface *interface;
2042 struct fcoe_ctlr *ctlr;
2043 int rc = 0;
2045 rtnl_lock();
2046 mutex_lock(&bnx2fc_dev_lock);
2048 interface = bnx2fc_interface_lookup(netdev);
2049 ctlr = bnx2fc_to_ctlr(interface);
2051 if (!interface) {
2052 rc = -ENODEV;
2053 pr_err(PFX "bnx2fc_disable: interface not found\n");
2054 } else {
2055 rc = __bnx2fc_disable(ctlr);
2057 mutex_unlock(&bnx2fc_dev_lock);
2058 rtnl_unlock();
2059 return rc;
2062 static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2063 struct cnic_fc_npiv_tbl *npiv_tbl)
2065 struct fc_vport_identifiers vpid;
2066 uint i, created = 0;
2068 if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2069 BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2070 goto done;
2073 /* Sanity check the first entry to make sure it's not 0 */
2074 if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2075 wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2076 BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2077 goto done;
2080 vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2081 vpid.vport_type = FC_PORTTYPE_NPIV;
2082 vpid.disable = false;
2084 for (i = 0; i < npiv_tbl->count; i++) {
2085 vpid.node_name = wwn_to_u64(npiv_tbl->wwnn[i]);
2086 vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2087 scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2088 "NPIV[%u]:%016llx-%016llx",
2089 created, vpid.port_name, vpid.node_name);
2090 if (fc_vport_create(lport->host, 0, &vpid))
2091 created++;
2092 else
2093 BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2095 done:
2096 return created;
2099 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2101 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2102 struct bnx2fc_hba *hba;
2103 struct cnic_fc_npiv_tbl *npiv_tbl;
2104 struct fc_lport *lport;
2106 if (interface->enabled == false) {
2107 if (!ctlr->lp) {
2108 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2109 return -ENODEV;
2110 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2111 fcoe_ctlr_link_up(ctlr);
2112 interface->enabled = true;
2116 /* Create static NPIV ports if any are contained in NVRAM */
2117 hba = interface->hba;
2118 lport = ctlr->lp;
2120 if (!hba)
2121 goto done;
2123 if (!hba->cnic)
2124 goto done;
2126 if (!lport)
2127 goto done;
2129 if (!lport->host)
2130 goto done;
2132 if (!hba->cnic->get_fc_npiv_tbl)
2133 goto done;
2135 npiv_tbl = kzalloc(sizeof(struct cnic_fc_npiv_tbl), GFP_KERNEL);
2136 if (!npiv_tbl)
2137 goto done;
2139 if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl))
2140 goto done_free;
2142 bnx2fc_npiv_create_vports(lport, npiv_tbl);
2143 done_free:
2144 kfree(npiv_tbl);
2145 done:
2146 return 0;
2150 * Deprecated: Use bnx2fc_enabled()
2152 static int bnx2fc_enable(struct net_device *netdev)
2154 struct bnx2fc_interface *interface;
2155 struct fcoe_ctlr *ctlr;
2156 int rc = 0;
2158 rtnl_lock();
2159 mutex_lock(&bnx2fc_dev_lock);
2161 interface = bnx2fc_interface_lookup(netdev);
2162 ctlr = bnx2fc_to_ctlr(interface);
2163 if (!interface) {
2164 rc = -ENODEV;
2165 pr_err(PFX "bnx2fc_enable: interface not found\n");
2166 } else {
2167 rc = __bnx2fc_enable(ctlr);
2170 mutex_unlock(&bnx2fc_dev_lock);
2171 rtnl_unlock();
2172 return rc;
2176 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2177 * @cdev: The FCoE Controller that is being enabled or disabled
2179 * fcoe_sysfs will ensure that the state of 'enabled' has
2180 * changed, so no checking is necessary here. This routine simply
2181 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2182 * When those routines are removed the functionality can be merged
2183 * here.
2185 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2187 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2189 switch (cdev->enabled) {
2190 case FCOE_CTLR_ENABLED:
2191 return __bnx2fc_enable(ctlr);
2192 case FCOE_CTLR_DISABLED:
2193 return __bnx2fc_disable(ctlr);
2194 case FCOE_CTLR_UNUSED:
2195 default:
2196 return -ENOTSUPP;
2200 enum bnx2fc_create_link_state {
2201 BNX2FC_CREATE_LINK_DOWN,
2202 BNX2FC_CREATE_LINK_UP,
2206 * _bnx2fc_create() - Create bnx2fc FCoE interface
2207 * @netdev : The net_device object the Ethernet interface to create on
2208 * @fip_mode: The FIP mode for this creation
2209 * @link_state: The ctlr link state on creation
2211 * Called from either the libfcoe 'create' module parameter
2212 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2214 * libfcoe's 'create' module parameter is deprecated so some
2215 * consolidation of code can be done when that interface is
2216 * removed.
2218 * Returns: 0 for success
2220 static int _bnx2fc_create(struct net_device *netdev,
2221 enum fip_state fip_mode,
2222 enum bnx2fc_create_link_state link_state)
2224 struct fcoe_ctlr_device *cdev;
2225 struct fcoe_ctlr *ctlr;
2226 struct bnx2fc_interface *interface;
2227 struct bnx2fc_hba *hba;
2228 struct net_device *phys_dev = netdev;
2229 struct fc_lport *lport;
2230 struct ethtool_drvinfo drvinfo;
2231 int rc = 0;
2232 int vlan_id = 0;
2234 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2235 if (fip_mode != FIP_MODE_FABRIC) {
2236 printk(KERN_ERR "fip mode not FABRIC\n");
2237 return -EIO;
2240 rtnl_lock();
2242 mutex_lock(&bnx2fc_dev_lock);
2244 if (!try_module_get(THIS_MODULE)) {
2245 rc = -EINVAL;
2246 goto mod_err;
2249 /* obtain physical netdev */
2250 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2251 phys_dev = vlan_dev_real_dev(netdev);
2253 /* verify if the physical device is a netxtreme2 device */
2254 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2255 memset(&drvinfo, 0, sizeof(drvinfo));
2256 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2257 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2258 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2259 rc = -EINVAL;
2260 goto netdev_err;
2262 } else {
2263 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2264 rc = -EINVAL;
2265 goto netdev_err;
2268 /* obtain interface and initialize rest of the structure */
2269 hba = bnx2fc_hba_lookup(phys_dev);
2270 if (!hba) {
2271 rc = -ENODEV;
2272 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2273 goto netdev_err;
2276 if (bnx2fc_interface_lookup(netdev)) {
2277 rc = -EEXIST;
2278 goto netdev_err;
2281 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2282 if (!interface) {
2283 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2284 rc = -ENOMEM;
2285 goto ifput_err;
2288 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2289 vlan_id = vlan_dev_vlan_id(netdev);
2290 interface->vlan_enabled = 1;
2293 ctlr = bnx2fc_to_ctlr(interface);
2294 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2295 interface->vlan_id = vlan_id;
2297 interface->timer_work_queue =
2298 create_singlethread_workqueue("bnx2fc_timer_wq");
2299 if (!interface->timer_work_queue) {
2300 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2301 rc = -EINVAL;
2302 goto ifput_err;
2305 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2306 if (!lport) {
2307 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2308 netdev->name);
2309 rc = -EINVAL;
2310 goto if_create_err;
2313 /* Add interface to if_list */
2314 list_add_tail(&interface->list, &if_list);
2316 lport->boot_time = jiffies;
2318 /* Make this master N_port */
2319 ctlr->lp = lport;
2321 if (link_state == BNX2FC_CREATE_LINK_UP)
2322 cdev->enabled = FCOE_CTLR_ENABLED;
2323 else
2324 cdev->enabled = FCOE_CTLR_DISABLED;
2326 if (link_state == BNX2FC_CREATE_LINK_UP &&
2327 !bnx2fc_link_ok(lport)) {
2328 fcoe_ctlr_link_up(ctlr);
2329 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2330 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2333 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2334 bnx2fc_start_disc(interface);
2336 if (link_state == BNX2FC_CREATE_LINK_UP)
2337 interface->enabled = true;
2340 * Release from kref_init in bnx2fc_interface_setup, on success
2341 * lport should be holding a reference taken in bnx2fc_if_create
2343 bnx2fc_interface_put(interface);
2344 /* put netdev that was held while calling dev_get_by_name */
2345 mutex_unlock(&bnx2fc_dev_lock);
2346 rtnl_unlock();
2347 return 0;
2349 if_create_err:
2350 destroy_workqueue(interface->timer_work_queue);
2351 ifput_err:
2352 bnx2fc_net_cleanup(interface);
2353 bnx2fc_interface_put(interface);
2354 goto mod_err;
2355 netdev_err:
2356 module_put(THIS_MODULE);
2357 mod_err:
2358 mutex_unlock(&bnx2fc_dev_lock);
2359 rtnl_unlock();
2360 return rc;
2364 * bnx2fc_create() - Create a bnx2fc interface
2365 * @netdev : The net_device object the Ethernet interface to create on
2366 * @fip_mode: The FIP mode for this creation
2368 * Called from fcoe transport
2370 * Returns: 0 for success
2372 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2374 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2378 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2379 * @netdev: The net_device to be used by the allocated FCoE Controller
2381 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2382 * in a link_down state. The allows the user an opportunity to configure
2383 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2385 * Creating in with this routine starts the FCoE Controller in Fabric
2386 * mode. The user can change to VN2VN or another mode before enabling.
2388 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2390 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2391 BNX2FC_CREATE_LINK_DOWN);
2395 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2397 * @cnic: Pointer to cnic device instance
2400 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2402 struct bnx2fc_hba *hba;
2404 /* Called with bnx2fc_dev_lock held */
2405 list_for_each_entry(hba, &adapter_list, list) {
2406 if (hba->cnic == cnic)
2407 return hba;
2409 return NULL;
2412 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2413 *netdev)
2415 struct bnx2fc_interface *interface;
2417 /* Called with bnx2fc_dev_lock held */
2418 list_for_each_entry(interface, &if_list, list) {
2419 if (interface->netdev == netdev)
2420 return interface;
2422 return NULL;
2425 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2426 *phys_dev)
2428 struct bnx2fc_hba *hba;
2430 /* Called with bnx2fc_dev_lock held */
2431 list_for_each_entry(hba, &adapter_list, list) {
2432 if (hba->phys_dev == phys_dev)
2433 return hba;
2435 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2436 return NULL;
2440 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2442 * @dev cnic device handle
2444 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2446 struct bnx2fc_hba *hba;
2447 struct bnx2fc_interface *interface, *tmp;
2449 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2451 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2452 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2453 dev->netdev->name, dev->flags);
2454 return;
2457 mutex_lock(&bnx2fc_dev_lock);
2458 hba = bnx2fc_find_hba_for_cnic(dev);
2459 if (!hba) {
2460 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2461 dev);
2462 mutex_unlock(&bnx2fc_dev_lock);
2463 return;
2466 list_del_init(&hba->list);
2467 adapter_count--;
2469 list_for_each_entry_safe(interface, tmp, &if_list, list)
2470 /* destroy not called yet, move to quiesced list */
2471 if (interface->hba == hba)
2472 __bnx2fc_destroy(interface);
2473 mutex_unlock(&bnx2fc_dev_lock);
2475 /* Ensure ALL destroy work has been completed before return */
2476 flush_workqueue(bnx2fc_wq);
2478 bnx2fc_ulp_stop(hba);
2479 /* unregister cnic device */
2480 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2481 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2482 bnx2fc_hba_destroy(hba);
2486 * bnx2fc_fcoe_reset - Resets the fcoe
2488 * @shost: shost the reset is from
2490 * Returns: always 0
2492 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2494 struct fc_lport *lport = shost_priv(shost);
2495 fc_lport_reset(lport);
2496 return 0;
2500 static bool bnx2fc_match(struct net_device *netdev)
2502 struct net_device *phys_dev = netdev;
2504 mutex_lock(&bnx2fc_dev_lock);
2505 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2506 phys_dev = vlan_dev_real_dev(netdev);
2508 if (bnx2fc_hba_lookup(phys_dev)) {
2509 mutex_unlock(&bnx2fc_dev_lock);
2510 return true;
2513 mutex_unlock(&bnx2fc_dev_lock);
2514 return false;
2518 static struct fcoe_transport bnx2fc_transport = {
2519 .name = {"bnx2fc"},
2520 .attached = false,
2521 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2522 .alloc = bnx2fc_ctlr_alloc,
2523 .match = bnx2fc_match,
2524 .create = bnx2fc_create,
2525 .destroy = bnx2fc_destroy,
2526 .enable = bnx2fc_enable,
2527 .disable = bnx2fc_disable,
2531 * bnx2fc_percpu_thread_create - Create a receive thread for an
2532 * online CPU
2534 * @cpu: cpu index for the online cpu
2536 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2538 struct bnx2fc_percpu_s *p;
2539 struct task_struct *thread;
2541 p = &per_cpu(bnx2fc_percpu, cpu);
2543 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2544 (void *)p, cpu_to_node(cpu),
2545 "bnx2fc_thread/%d", cpu);
2546 /* bind thread to the cpu */
2547 if (likely(!IS_ERR(thread))) {
2548 kthread_bind(thread, cpu);
2549 p->iothread = thread;
2550 wake_up_process(thread);
2554 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2556 struct bnx2fc_percpu_s *p;
2557 struct task_struct *thread;
2558 struct bnx2fc_work *work, *tmp;
2560 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2562 /* Prevent any new work from being queued for this CPU */
2563 p = &per_cpu(bnx2fc_percpu, cpu);
2564 spin_lock_bh(&p->fp_work_lock);
2565 thread = p->iothread;
2566 p->iothread = NULL;
2569 /* Free all work in the list */
2570 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2571 list_del_init(&work->list);
2572 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2573 kfree(work);
2576 spin_unlock_bh(&p->fp_work_lock);
2578 if (thread)
2579 kthread_stop(thread);
2583 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2585 * @nfb: The callback data block
2586 * @action: The event triggering the callback
2587 * @hcpu: The index of the CPU that the event is for
2589 * This creates or destroys per-CPU data for fcoe
2591 * Returns NOTIFY_OK always.
2593 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2594 unsigned long action, void *hcpu)
2596 unsigned cpu = (unsigned long)hcpu;
2598 switch (action) {
2599 case CPU_ONLINE:
2600 case CPU_ONLINE_FROZEN:
2601 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2602 bnx2fc_percpu_thread_create(cpu);
2603 break;
2604 case CPU_DEAD:
2605 case CPU_DEAD_FROZEN:
2606 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2607 bnx2fc_percpu_thread_destroy(cpu);
2608 break;
2609 default:
2610 break;
2612 return NOTIFY_OK;
2616 * bnx2fc_mod_init - module init entry point
2618 * Initialize driver wide global data structures, and register
2619 * with cnic module
2621 static int __init bnx2fc_mod_init(void)
2623 struct fcoe_percpu_s *bg;
2624 struct task_struct *l2_thread;
2625 int rc = 0;
2626 unsigned int cpu = 0;
2627 struct bnx2fc_percpu_s *p;
2629 printk(KERN_INFO PFX "%s", version);
2631 /* register as a fcoe transport */
2632 rc = fcoe_transport_attach(&bnx2fc_transport);
2633 if (rc) {
2634 printk(KERN_ERR "failed to register an fcoe transport, check "
2635 "if libfcoe is loaded\n");
2636 goto out;
2639 INIT_LIST_HEAD(&adapter_list);
2640 INIT_LIST_HEAD(&if_list);
2641 mutex_init(&bnx2fc_dev_lock);
2642 adapter_count = 0;
2644 /* Attach FC transport template */
2645 rc = bnx2fc_attach_transport();
2646 if (rc)
2647 goto detach_ft;
2649 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2650 if (!bnx2fc_wq) {
2651 rc = -ENOMEM;
2652 goto release_bt;
2655 bg = &bnx2fc_global;
2656 skb_queue_head_init(&bg->fcoe_rx_list);
2657 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2658 (void *)bg,
2659 "bnx2fc_l2_thread");
2660 if (IS_ERR(l2_thread)) {
2661 rc = PTR_ERR(l2_thread);
2662 goto free_wq;
2664 wake_up_process(l2_thread);
2665 spin_lock_bh(&bg->fcoe_rx_list.lock);
2666 bg->thread = l2_thread;
2667 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2669 for_each_possible_cpu(cpu) {
2670 p = &per_cpu(bnx2fc_percpu, cpu);
2671 INIT_LIST_HEAD(&p->work_list);
2672 spin_lock_init(&p->fp_work_lock);
2675 cpu_notifier_register_begin();
2677 for_each_online_cpu(cpu) {
2678 bnx2fc_percpu_thread_create(cpu);
2681 /* Initialize per CPU interrupt thread */
2682 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2684 cpu_notifier_register_done();
2686 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2688 return 0;
2690 free_wq:
2691 destroy_workqueue(bnx2fc_wq);
2692 release_bt:
2693 bnx2fc_release_transport();
2694 detach_ft:
2695 fcoe_transport_detach(&bnx2fc_transport);
2696 out:
2697 return rc;
2700 static void __exit bnx2fc_mod_exit(void)
2702 LIST_HEAD(to_be_deleted);
2703 struct bnx2fc_hba *hba, *next;
2704 struct fcoe_percpu_s *bg;
2705 struct task_struct *l2_thread;
2706 struct sk_buff *skb;
2707 unsigned int cpu = 0;
2710 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2711 * it will have higher precedence than bnx2fc_dev_lock.
2712 * unregister_device() cannot be called with bnx2fc_dev_lock
2713 * held.
2715 mutex_lock(&bnx2fc_dev_lock);
2716 list_splice(&adapter_list, &to_be_deleted);
2717 INIT_LIST_HEAD(&adapter_list);
2718 adapter_count = 0;
2719 mutex_unlock(&bnx2fc_dev_lock);
2721 /* Unregister with cnic */
2722 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2723 list_del_init(&hba->list);
2724 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2725 hba);
2726 bnx2fc_ulp_stop(hba);
2727 /* unregister cnic device */
2728 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2729 &hba->reg_with_cnic))
2730 hba->cnic->unregister_device(hba->cnic,
2731 CNIC_ULP_FCOE);
2732 bnx2fc_hba_destroy(hba);
2734 cnic_unregister_driver(CNIC_ULP_FCOE);
2736 /* Destroy global thread */
2737 bg = &bnx2fc_global;
2738 spin_lock_bh(&bg->fcoe_rx_list.lock);
2739 l2_thread = bg->thread;
2740 bg->thread = NULL;
2741 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2742 kfree_skb(skb);
2744 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2746 if (l2_thread)
2747 kthread_stop(l2_thread);
2749 cpu_notifier_register_begin();
2751 /* Destroy per cpu threads */
2752 for_each_online_cpu(cpu) {
2753 bnx2fc_percpu_thread_destroy(cpu);
2756 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2758 cpu_notifier_register_done();
2760 destroy_workqueue(bnx2fc_wq);
2762 * detach from scsi transport
2763 * must happen after all destroys are done
2765 bnx2fc_release_transport();
2767 /* detach from fcoe transport */
2768 fcoe_transport_detach(&bnx2fc_transport);
2771 module_init(bnx2fc_mod_init);
2772 module_exit(bnx2fc_mod_exit);
2774 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2775 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2776 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2777 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2778 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2779 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2780 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2781 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2783 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2784 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2787 static struct fc_function_template bnx2fc_transport_function = {
2788 .show_host_node_name = 1,
2789 .show_host_port_name = 1,
2790 .show_host_supported_classes = 1,
2791 .show_host_supported_fc4s = 1,
2792 .show_host_active_fc4s = 1,
2793 .show_host_maxframe_size = 1,
2795 .show_host_port_id = 1,
2796 .show_host_supported_speeds = 1,
2797 .get_host_speed = fc_get_host_speed,
2798 .show_host_speed = 1,
2799 .show_host_port_type = 1,
2800 .get_host_port_state = fc_get_host_port_state,
2801 .show_host_port_state = 1,
2802 .show_host_symbolic_name = 1,
2804 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2805 sizeof(struct bnx2fc_rport)),
2806 .show_rport_maxframe_size = 1,
2807 .show_rport_supported_classes = 1,
2809 .show_host_fabric_name = 1,
2810 .show_starget_node_name = 1,
2811 .show_starget_port_name = 1,
2812 .show_starget_port_id = 1,
2813 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2814 .show_rport_dev_loss_tmo = 1,
2815 .get_fc_host_stats = bnx2fc_get_host_stats,
2817 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2819 .terminate_rport_io = fc_rport_terminate_io,
2821 .vport_create = bnx2fc_vport_create,
2822 .vport_delete = bnx2fc_vport_destroy,
2823 .vport_disable = bnx2fc_vport_disable,
2824 .bsg_request = fc_lport_bsg_request,
2827 static struct fc_function_template bnx2fc_vport_xport_function = {
2828 .show_host_node_name = 1,
2829 .show_host_port_name = 1,
2830 .show_host_supported_classes = 1,
2831 .show_host_supported_fc4s = 1,
2832 .show_host_active_fc4s = 1,
2833 .show_host_maxframe_size = 1,
2835 .show_host_port_id = 1,
2836 .show_host_supported_speeds = 1,
2837 .get_host_speed = fc_get_host_speed,
2838 .show_host_speed = 1,
2839 .show_host_port_type = 1,
2840 .get_host_port_state = fc_get_host_port_state,
2841 .show_host_port_state = 1,
2842 .show_host_symbolic_name = 1,
2844 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2845 sizeof(struct bnx2fc_rport)),
2846 .show_rport_maxframe_size = 1,
2847 .show_rport_supported_classes = 1,
2849 .show_host_fabric_name = 1,
2850 .show_starget_node_name = 1,
2851 .show_starget_port_name = 1,
2852 .show_starget_port_id = 1,
2853 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2854 .show_rport_dev_loss_tmo = 1,
2855 .get_fc_host_stats = fc_get_host_stats,
2856 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2857 .terminate_rport_io = fc_rport_terminate_io,
2858 .bsg_request = fc_lport_bsg_request,
2862 * scsi_host_template structure used while registering with SCSI-ml
2864 static struct scsi_host_template bnx2fc_shost_template = {
2865 .module = THIS_MODULE,
2866 .name = "QLogic Offload FCoE Initiator",
2867 .queuecommand = bnx2fc_queuecommand,
2868 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2869 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2870 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2871 .eh_host_reset_handler = fc_eh_host_reset,
2872 .slave_alloc = fc_slave_alloc,
2873 .change_queue_depth = scsi_change_queue_depth,
2874 .this_id = -1,
2875 .cmd_per_lun = 3,
2876 .use_clustering = ENABLE_CLUSTERING,
2877 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2878 .max_sectors = 1024,
2879 .track_queue_depth = 1,
2882 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2883 .frame_send = bnx2fc_xmit,
2884 .elsct_send = bnx2fc_elsct_send,
2885 .fcp_abort_io = bnx2fc_abort_io,
2886 .fcp_cleanup = bnx2fc_cleanup,
2887 .get_lesb = fcoe_get_lesb,
2888 .rport_event_callback = bnx2fc_rport_event_handler,
2892 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2893 * structure carrying callback function pointers
2895 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2896 .owner = THIS_MODULE,
2897 .cnic_init = bnx2fc_ulp_init,
2898 .cnic_exit = bnx2fc_ulp_exit,
2899 .cnic_start = bnx2fc_ulp_start,
2900 .cnic_stop = bnx2fc_ulp_stop,
2901 .indicate_kcqes = bnx2fc_indicate_kcqe,
2902 .indicate_netevent = bnx2fc_indicate_netevent,
2903 .cnic_get_stats = bnx2fc_ulp_get_stats,