blob: 27f2cc4b97a5f2a2ee6c2361fcfc7be2b43cdb6e [file] [log] [blame]
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001/* 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.
5 *
Bhanu Prakash Gollapudicf122192013-03-08 13:28:53 -08006 * Copyright (c) 2008 - 2013 Broadcom Corporation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08007 *
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.
11 *
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13 */
14
15#include "bnx2fc.h"
16
17static struct list_head adapter_list;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070018static struct list_head if_list;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080019static u32 adapter_count;
20static DEFINE_MUTEX(bnx2fc_dev_lock);
21DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22
23#define DRV_MODULE_NAME "bnx2fc"
24#define DRV_MODULE_VERSION BNX2FC_VERSION
Bhanu Prakash Gollapudi0a7d1d32013-03-08 15:53:26 -080025#define DRV_MODULE_RELDATE "Mar 08, 2013"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080026
27
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080028static char version[] =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080029 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31
32
33MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35MODULE_LICENSE("GPL");
36MODULE_VERSION(DRV_MODULE_VERSION);
37
38#define BNX2FC_MAX_QUEUE_DEPTH 256
39#define BNX2FC_MIN_QUEUE_DEPTH 32
40#define FCOE_WORD_TO_BYTE 4
41
42static struct scsi_transport_template *bnx2fc_transport_template;
43static struct scsi_transport_template *bnx2fc_vport_xport_template;
44
45struct workqueue_struct *bnx2fc_wq;
46
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
49 */
50struct fcoe_percpu_s bnx2fc_global;
51DEFINE_SPINLOCK(bnx2fc_global_lock);
52
53static struct cnic_ulp_ops bnx2fc_cnic_cb;
54static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55static struct scsi_host_template bnx2fc_shost_template;
56static struct fc_function_template bnx2fc_transport_function;
Robert Love8d55e502012-05-22 19:06:26 -070057static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080058static struct fc_function_template bnx2fc_vport_xport_function;
59static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -070060static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080061static int bnx2fc_destroy(struct net_device *net_device);
62static int bnx2fc_enable(struct net_device *netdev);
63static int bnx2fc_disable(struct net_device *netdev);
64
Robert Love6e89ea32012-11-27 06:53:40 +000065/* fcoe_syfs control interface handlers */
66static int bnx2fc_ctlr_alloc(struct net_device *netdev);
67static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
68
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080069static void bnx2fc_recv_frame(struct sk_buff *skb);
70
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070071static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080072static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080073static int bnx2fc_lport_config(struct fc_lport *lport);
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +000074static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080075static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
76static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
77static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
78static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070079static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080080 struct device *parent, int npiv);
81static void bnx2fc_destroy_work(struct work_struct *work);
82
83static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070084static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
85 *phys_dev);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -070086static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080087static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
88
89static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
90static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
91
92static void bnx2fc_port_shutdown(struct fc_lport *lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070093static void bnx2fc_stop(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080094static int __init bnx2fc_mod_init(void);
95static void __exit bnx2fc_mod_exit(void);
96
97unsigned int bnx2fc_debug_level;
98module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
99
100static int bnx2fc_cpu_callback(struct notifier_block *nfb,
101 unsigned long action, void *hcpu);
102/* notification function for CPU hotplug events */
103static struct notifier_block bnx2fc_cpu_notifier = {
104 .notifier_call = bnx2fc_cpu_callback,
105};
106
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -0700107static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
108{
109 return ((struct bnx2fc_interface *)
110 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
111}
112
Robert Love8d55e502012-05-22 19:06:26 -0700113static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
114{
115 struct fcoe_ctlr_device *ctlr_dev =
116 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
117 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
118 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
119
120 fcf_dev->vlan_id = fcoe->vlan_id;
121}
122
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800123static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
124{
125 struct fcoe_percpu_s *bg;
126 struct fcoe_rcv_info *fr;
127 struct sk_buff_head *list;
128 struct sk_buff *skb, *next;
129 struct sk_buff *head;
130
131 bg = &bnx2fc_global;
132 spin_lock_bh(&bg->fcoe_rx_list.lock);
133 list = &bg->fcoe_rx_list;
134 head = list->next;
135 for (skb = head; skb != (struct sk_buff *)list;
136 skb = next) {
137 next = skb->next;
138 fr = fcoe_dev_from_skb(skb);
139 if (fr->fr_dev == lp) {
140 __skb_unlink(skb, list);
141 kfree_skb(skb);
142 }
143 }
144 spin_unlock_bh(&bg->fcoe_rx_list.lock);
145}
146
147int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
148{
149 int rc;
150 spin_lock(&bnx2fc_global_lock);
151 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
152 spin_unlock(&bnx2fc_global_lock);
153
154 return rc;
155}
156
157static void bnx2fc_abort_io(struct fc_lport *lport)
158{
159 /*
160 * This function is no-op for bnx2fc, but we do
161 * not want to leave it as NULL either, as libfc
162 * can call the default function which is
163 * fc_fcp_abort_io.
164 */
165}
166
167static void bnx2fc_cleanup(struct fc_lport *lport)
168{
169 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700170 struct bnx2fc_interface *interface = port->priv;
171 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800172 struct bnx2fc_rport *tgt;
173 int i;
174
175 BNX2FC_MISC_DBG("Entered %s\n", __func__);
176 mutex_lock(&hba->hba_mutex);
177 spin_lock_bh(&hba->hba_lock);
178 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
179 tgt = hba->tgt_ofld_list[i];
180 if (tgt) {
181 /* Cleanup IOs belonging to requested vport */
182 if (tgt->port == port) {
183 spin_unlock_bh(&hba->hba_lock);
184 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
185 bnx2fc_flush_active_ios(tgt);
186 spin_lock_bh(&hba->hba_lock);
187 }
188 }
189 }
190 spin_unlock_bh(&hba->hba_lock);
191 mutex_unlock(&hba->hba_mutex);
192}
193
194static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
195 struct fc_frame *fp)
196{
197 struct fc_rport_priv *rdata = tgt->rdata;
198 struct fc_frame_header *fh;
199 int rc = 0;
200
201 fh = fc_frame_header_get(fp);
202 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
203 "r_ctl = 0x%x\n", rdata->ids.port_id,
204 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
205 if ((fh->fh_type == FC_TYPE_ELS) &&
206 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
207
208 switch (fc_frame_payload_op(fp)) {
209 case ELS_ADISC:
210 rc = bnx2fc_send_adisc(tgt, fp);
211 break;
212 case ELS_LOGO:
213 rc = bnx2fc_send_logo(tgt, fp);
214 break;
215 case ELS_RLS:
216 rc = bnx2fc_send_rls(tgt, fp);
217 break;
218 default:
219 break;
220 }
221 } else if ((fh->fh_type == FC_TYPE_BLS) &&
222 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
223 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
224 else {
225 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
226 "rctl 0x%x thru non-offload path\n",
227 fh->fh_type, fh->fh_r_ctl);
228 return -ENODEV;
229 }
230 if (rc)
231 return -ENOMEM;
232 else
233 return 0;
234}
235
236/**
237 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
238 *
239 * @lport: the associated local port
240 * @fp: the fc_frame to be transmitted
241 */
242static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
243{
244 struct ethhdr *eh;
245 struct fcoe_crc_eof *cp;
246 struct sk_buff *skb;
247 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700248 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700249 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700250 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800251 struct fcoe_port *port;
252 struct fcoe_hdr *hp;
253 struct bnx2fc_rport *tgt;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700254 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800255 u8 sof, eof;
256 u32 crc;
257 unsigned int hlen, tlen, elen;
258 int wlen, rc = 0;
259
260 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700261 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700262 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700263 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800264
265 fh = fc_frame_header_get(fp);
266
267 skb = fp_skb(fp);
268 if (!lport->link_up) {
269 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
270 kfree_skb(skb);
271 return 0;
272 }
273
274 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Robert Lovefd8f8902012-05-22 19:06:16 -0700275 if (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800276 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
277 kfree_skb(skb);
278 return -EINVAL;
279 }
Robert Lovefd8f8902012-05-22 19:06:16 -0700280 if (fcoe_ctlr_els_send(ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800281 return 0;
282 }
283
284 sof = fr_sof(fp);
285 eof = fr_eof(fp);
286
287 /*
288 * Snoop the frame header to check if the frame is for
289 * an offloaded session
290 */
291 /*
292 * tgt_ofld_list access is synchronized using
293 * both hba mutex and hba lock. Atleast hba mutex or
294 * hba lock needs to be held for read access.
295 */
296
297 spin_lock_bh(&hba->hba_lock);
298 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
299 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
300 /* This frame is for offloaded session */
301 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
302 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
303 spin_unlock_bh(&hba->hba_lock);
304 rc = bnx2fc_xmit_l2_frame(tgt, fp);
305 if (rc != -ENODEV) {
306 kfree_skb(skb);
307 return rc;
308 }
309 } else {
310 spin_unlock_bh(&hba->hba_lock);
311 }
312
313 elen = sizeof(struct ethhdr);
314 hlen = sizeof(struct fcoe_hdr);
315 tlen = sizeof(struct fcoe_crc_eof);
316 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
317
318 skb->ip_summed = CHECKSUM_NONE;
319 crc = fcoe_fc_crc(fp);
320
321 /* copy port crc and eof to the skb buff */
322 if (skb_is_nonlinear(skb)) {
323 skb_frag_t *frag;
324 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
325 kfree_skb(skb);
326 return -ENOMEM;
327 }
328 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800329 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800330 } else {
331 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
332 }
333
334 memset(cp, 0, sizeof(*cp));
335 cp->fcoe_eof = eof;
336 cp->fcoe_crc32 = cpu_to_le32(~crc);
337 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800338 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800339 cp = NULL;
340 }
341
342 /* adjust skb network/transport offsets to match mac/fcoe/port */
343 skb_push(skb, elen + hlen);
344 skb_reset_mac_header(skb);
345 skb_reset_network_header(skb);
346 skb->mac_len = elen;
347 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700348 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800349
350 /* fill up mac and fcoe headers */
351 eh = eth_hdr(skb);
352 eh->h_proto = htons(ETH_P_FCOE);
Robert Lovefd8f8902012-05-22 19:06:16 -0700353 if (ctlr->map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800354 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
355 else
356 /* insert GW address */
Robert Lovefd8f8902012-05-22 19:06:16 -0700357 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800358
Robert Lovefd8f8902012-05-22 19:06:16 -0700359 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
360 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800361 else
362 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
363
364 hp = (struct fcoe_hdr *)(eh + 1);
365 memset(hp, 0, sizeof(*hp));
366 if (FC_FCOE_VER)
367 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
368 hp->fcoe_sof = sof;
369
370 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
371 if (lport->seq_offload && fr_max_payload(fp)) {
372 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
373 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
374 } else {
375 skb_shinfo(skb)->gso_type = 0;
376 skb_shinfo(skb)->gso_size = 0;
377 }
378
379 /*update tx stats */
Vasu Dev1bd49b42012-05-25 10:26:43 -0700380 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800381 stats->TxFrames++;
382 stats->TxWords += wlen;
383 put_cpu();
384
385 /* send down to lld */
386 fr_dev(fp) = lport;
387 if (port->fcoe_pending_queue.qlen)
388 fcoe_check_wait_queue(lport, skb);
389 else if (fcoe_start_io(skb))
390 fcoe_check_wait_queue(lport, skb);
391
392 return 0;
393}
394
395/**
396 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
397 *
398 * @skb: the receive socket buffer
399 * @dev: associated net device
400 * @ptype: context
401 * @olddev: last device
402 *
403 * This function receives the packet and builds FC frame and passes it up
404 */
405static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
406 struct packet_type *ptype, struct net_device *olddev)
407{
408 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700409 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700410 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800411 struct fc_frame_header *fh;
412 struct fcoe_rcv_info *fr;
413 struct fcoe_percpu_s *bg;
414 unsigned short oxid;
415
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700416 interface = container_of(ptype, struct bnx2fc_interface,
417 fcoe_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -0700418 ctlr = bnx2fc_to_ctlr(interface);
419 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800420
421 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700422 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800423 goto err;
424 }
425
426 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700427 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800428 goto err;
429 }
430
431 /*
432 * Check for minimum frame length, and make sure required FCoE
433 * and FC headers are pulled into the linear data area.
434 */
435 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
436 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
437 goto err;
438
439 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
440 fh = (struct fc_frame_header *) skb_transport_header(skb);
441
442 oxid = ntohs(fh->fh_ox_id);
443
444 fr = fcoe_dev_from_skb(skb);
445 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800446
447 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800448 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800449
450 __skb_queue_tail(&bg->fcoe_rx_list, skb);
451 if (bg->fcoe_rx_list.qlen == 1)
452 wake_up_process(bg->thread);
453
Neil Hormanfc05ab72012-03-09 14:50:13 -0800454 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800455
456 return 0;
457err:
458 kfree_skb(skb);
459 return -1;
460}
461
462static int bnx2fc_l2_rcv_thread(void *arg)
463{
464 struct fcoe_percpu_s *bg = arg;
465 struct sk_buff *skb;
466
467 set_user_nice(current, -20);
468 set_current_state(TASK_INTERRUPTIBLE);
469 while (!kthread_should_stop()) {
470 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800471 spin_lock_bh(&bg->fcoe_rx_list.lock);
472 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
473 spin_unlock_bh(&bg->fcoe_rx_list.lock);
474 bnx2fc_recv_frame(skb);
475 spin_lock_bh(&bg->fcoe_rx_list.lock);
476 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700477 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800478 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800479 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700480 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800481 return 0;
482}
483
484
485static void bnx2fc_recv_frame(struct sk_buff *skb)
486{
487 u32 fr_len;
488 struct fc_lport *lport;
489 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700490 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800491 struct fc_frame_header *fh;
492 struct fcoe_crc_eof crc_eof;
493 struct fc_frame *fp;
494 struct fc_lport *vn_port;
495 struct fcoe_port *port;
496 u8 *mac = NULL;
497 u8 *dest_mac = NULL;
498 struct fcoe_hdr *hp;
499
500 fr = fcoe_dev_from_skb(skb);
501 lport = fr->fr_dev;
502 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700503 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800504 kfree_skb(skb);
505 return;
506 }
507
508 if (skb_is_nonlinear(skb))
509 skb_linearize(skb);
510 mac = eth_hdr(skb)->h_source;
511 dest_mac = eth_hdr(skb)->h_dest;
512
513 /* Pull the header */
514 hp = (struct fcoe_hdr *) skb_network_header(skb);
515 fh = (struct fc_frame_header *) skb_transport_header(skb);
516 skb_pull(skb, sizeof(struct fcoe_hdr));
517 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
518
Vasu Dev1bd49b42012-05-25 10:26:43 -0700519 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800520 stats->RxFrames++;
521 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
522
523 fp = (struct fc_frame *)skb;
524 fc_frame_init(fp);
525 fr_dev(fp) = lport;
526 fr_sof(fp) = hp->fcoe_sof;
527 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
528 put_cpu();
529 kfree_skb(skb);
530 return;
531 }
532 fr_eof(fp) = crc_eof.fcoe_eof;
533 fr_crc(fp) = crc_eof.fcoe_crc32;
534 if (pskb_trim(skb, fr_len)) {
535 put_cpu();
536 kfree_skb(skb);
537 return;
538 }
539
540 fh = fc_frame_header_get(fp);
541
542 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
543 if (vn_port) {
544 port = lport_priv(vn_port);
545 if (compare_ether_addr(port->data_src_addr, dest_mac)
546 != 0) {
547 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
548 put_cpu();
549 kfree_skb(skb);
550 return;
551 }
552 }
553 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
554 fh->fh_type == FC_TYPE_FCP) {
555 /* Drop FCP data. We dont this in L2 path */
556 put_cpu();
557 kfree_skb(skb);
558 return;
559 }
560 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
561 fh->fh_type == FC_TYPE_ELS) {
562 switch (fc_frame_payload_op(fp)) {
563 case ELS_LOGO:
564 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
565 /* drop non-FIP LOGO */
566 put_cpu();
567 kfree_skb(skb);
568 return;
569 }
570 break;
571 }
572 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700573
574 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
575 /* Drop incoming ABTS */
576 put_cpu();
577 kfree_skb(skb);
578 return;
579 }
580
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800581 if (le32_to_cpu(fr_crc(fp)) !=
582 ~crc32(~0, skb->data, fr_len)) {
583 if (stats->InvalidCRCCount < 5)
584 printk(KERN_WARNING PFX "dropping frame with "
585 "CRC error\n");
586 stats->InvalidCRCCount++;
587 put_cpu();
588 kfree_skb(skb);
589 return;
590 }
591 put_cpu();
592 fc_exch_recv(lport, fp);
593}
594
595/**
596 * bnx2fc_percpu_io_thread - thread per cpu for ios
597 *
598 * @arg: ptr to bnx2fc_percpu_info structure
599 */
600int bnx2fc_percpu_io_thread(void *arg)
601{
602 struct bnx2fc_percpu_s *p = arg;
603 struct bnx2fc_work *work, *tmp;
604 LIST_HEAD(work_list);
605
606 set_user_nice(current, -20);
607 set_current_state(TASK_INTERRUPTIBLE);
608 while (!kthread_should_stop()) {
609 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800610 spin_lock_bh(&p->fp_work_lock);
611 while (!list_empty(&p->work_list)) {
612 list_splice_init(&p->work_list, &work_list);
613 spin_unlock_bh(&p->fp_work_lock);
614
615 list_for_each_entry_safe(work, tmp, &work_list, list) {
616 list_del_init(&work->list);
617 bnx2fc_process_cq_compl(work->tgt, work->wqe);
618 kfree(work);
619 }
620
621 spin_lock_bh(&p->fp_work_lock);
622 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700623 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800624 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800625 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700626 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800627
628 return 0;
629}
630
631static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
632{
633 struct fc_host_statistics *bnx2fc_stats;
634 struct fc_lport *lport = shost_priv(shost);
635 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700636 struct bnx2fc_interface *interface = port->priv;
637 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800638 struct fcoe_statistics_params *fw_stats;
639 int rc = 0;
640
641 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
642 if (!fw_stats)
643 return NULL;
644
645 bnx2fc_stats = fc_get_host_stats(shost);
646
647 init_completion(&hba->stat_req_done);
648 if (bnx2fc_send_stat_req(hba))
649 return bnx2fc_stats;
650 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
651 if (!rc) {
652 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
653 return bnx2fc_stats;
654 }
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800655 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
656 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
657 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
658 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
659 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
660 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
661 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
662 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
663 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
664 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800665
666 bnx2fc_stats->dumped_frames = 0;
667 bnx2fc_stats->lip_count = 0;
668 bnx2fc_stats->nos_count = 0;
669 bnx2fc_stats->loss_of_sync_count = 0;
670 bnx2fc_stats->loss_of_signal_count = 0;
671 bnx2fc_stats->prim_seq_protocol_err_count = 0;
672
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800673 memcpy(&hba->prev_stats, hba->stats_buffer,
674 sizeof(struct fcoe_statistics_params));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800675 return bnx2fc_stats;
676}
677
678static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
679{
680 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700681 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800682 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800683 struct Scsi_Host *shost = lport->host;
684 int rc = 0;
685
686 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
687 shost->max_lun = BNX2FC_MAX_LUN;
688 shost->max_id = BNX2FC_MAX_FCP_TGT;
689 shost->max_channel = 0;
690 if (lport->vport)
691 shost->transportt = bnx2fc_vport_xport_template;
692 else
693 shost->transportt = bnx2fc_transport_template;
694
695 /* Add the new host to SCSI-ml */
696 rc = scsi_add_host(lport->host, dev);
697 if (rc) {
698 printk(KERN_ERR PFX "Error on scsi_add_host\n");
699 return rc;
700 }
701 if (!lport->vport)
702 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800703 snprintf(fc_host_symbolic_name(lport->host), 256,
704 "%s (Broadcom %s) v%s over %s",
705 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700706 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800707
708 return 0;
709}
710
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800711static int bnx2fc_link_ok(struct fc_lport *lport)
712{
713 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700714 struct bnx2fc_interface *interface = port->priv;
715 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800716 struct net_device *dev = hba->phys_dev;
717 int rc = 0;
718
719 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
720 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
721 else {
722 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
723 rc = -1;
724 }
725 return rc;
726}
727
728/**
729 * bnx2fc_get_link_state - get network link state
730 *
731 * @hba: adapter instance pointer
732 *
733 * updates adapter structure flag based on netdev state
734 */
735void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
736{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700737 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800738 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
739 else
740 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
741}
742
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700743static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800744{
745 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700746 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700747 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800748 struct fcoe_port *port;
749 u64 wwnn, wwpn;
750
751 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700752 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700753 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700754 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800755
756 /* require support for get_pauseparam ethtool op. */
757 if (!hba->phys_dev->ethtool_ops ||
758 !hba->phys_dev->ethtool_ops->get_pauseparam)
759 return -EOPNOTSUPP;
760
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700761 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800762 return -EINVAL;
763
764 skb_queue_head_init(&port->fcoe_pending_queue);
765 port->fcoe_pending_queue_active = 0;
766 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
767
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000768 fcoe_link_speed_update(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800769
770 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700771 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700772 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700773 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800774 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
775 fc_set_wwnn(lport, wwnn);
776
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700777 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700778 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700779 2, 0);
780
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800781 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
782 fc_set_wwpn(lport, wwpn);
783 }
784
785 return 0;
786}
787
788static void bnx2fc_destroy_timer(unsigned long data)
789{
790 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
791
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700792 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
793 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700794 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800795 wake_up_interruptible(&hba->destroy_wait);
796}
797
798/**
799 * bnx2fc_indicate_netevent - Generic netdev event handler
800 *
801 * @context: adapter structure pointer
802 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000803 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800804 *
805 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700806 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800807 */
Michael Chan415199f2011-07-20 14:55:24 +0000808static void bnx2fc_indicate_netevent(void *context, unsigned long event,
809 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800810{
811 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Robert Love6e89ea32012-11-27 06:53:40 +0000812 struct fcoe_ctlr_device *cdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700813 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800814 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700815 struct bnx2fc_interface *interface, *tmp;
Robert Lovefd8f8902012-05-22 19:06:16 -0700816 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700817 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800818 u32 link_possible = 1;
819
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700820 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000821 return;
822
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800823 switch (event) {
824 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800825 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
826 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700827 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800828 break;
829
830 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800831 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
832 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
833 link_possible = 0;
834 break;
835
836 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800837 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
838 link_possible = 0;
839 break;
840
841 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800842 break;
843
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700844 case NETDEV_UNREGISTER:
845 if (!vlan_id)
846 return;
847 mutex_lock(&bnx2fc_dev_lock);
848 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700849 if (interface->hba == hba &&
850 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
851 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700852 }
853 mutex_unlock(&bnx2fc_dev_lock);
854 return;
855
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800856 default:
Masanari Iida59e13d42012-04-25 00:24:16 +0900857 printk(KERN_ERR PFX "Unknown netevent %ld", event);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800858 return;
859 }
860
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700861 mutex_lock(&bnx2fc_dev_lock);
862 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800863
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700864 if (interface->hba != hba)
865 continue;
866
Robert Lovefd8f8902012-05-22 19:06:16 -0700867 ctlr = bnx2fc_to_ctlr(interface);
868 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700869 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
870 interface->netdev->name, event);
871
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000872 fcoe_link_speed_update(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700873
Robert Love6e89ea32012-11-27 06:53:40 +0000874 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700875
876 if (link_possible && !bnx2fc_link_ok(lport)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000877 switch (cdev->enabled) {
878 case FCOE_CTLR_DISABLED:
879 pr_info("Link up while interface is disabled.\n");
880 break;
881 case FCOE_CTLR_ENABLED:
882 case FCOE_CTLR_UNUSED:
883 /* Reset max recv frame size to default */
884 fc_set_mfs(lport, BNX2FC_MFS);
885 /*
886 * ctlr link up will only be handled during
887 * enable to avoid sending discovery
888 * solicitation on a stale vlan
889 */
890 if (interface->enabled)
891 fcoe_ctlr_link_up(ctlr);
892 };
Robert Lovefd8f8902012-05-22 19:06:16 -0700893 } else if (fcoe_ctlr_link_down(ctlr)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000894 switch (cdev->enabled) {
895 case FCOE_CTLR_DISABLED:
896 pr_info("Link down while interface is disabled.\n");
897 break;
898 case FCOE_CTLR_ENABLED:
899 case FCOE_CTLR_UNUSED:
900 mutex_lock(&lport->lp_mutex);
901 list_for_each_entry(vport, &lport->vports, list)
902 fc_host_port_type(vport->host) =
903 FC_PORTTYPE_UNKNOWN;
904 mutex_unlock(&lport->lp_mutex);
905 fc_host_port_type(lport->host) =
906 FC_PORTTYPE_UNKNOWN;
907 per_cpu_ptr(lport->stats,
908 get_cpu())->LinkFailureCount++;
909 put_cpu();
910 fcoe_clean_pending_queue(lport);
911 wait_for_upload = 1;
912 };
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800913 }
914 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700915 mutex_unlock(&bnx2fc_dev_lock);
916
917 if (wait_for_upload) {
918 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
919 init_waitqueue_head(&hba->shutdown_wait);
920 BNX2FC_MISC_DBG("indicate_netevent "
921 "num_ofld_sess = %d\n",
922 hba->num_ofld_sess);
923 hba->wait_for_link_down = 1;
924 wait_event_interruptible(hba->shutdown_wait,
925 (hba->num_ofld_sess == 0));
926 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
927 hba->num_ofld_sess);
928 hba->wait_for_link_down = 0;
929
930 if (signal_pending(current))
931 flush_signals(current);
932 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800933}
934
935static int bnx2fc_libfc_config(struct fc_lport *lport)
936{
937
938 /* Set the function pointers set by bnx2fc driver */
939 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
940 sizeof(struct libfc_function_template));
941 fc_elsct_init(lport);
942 fc_exch_init(lport);
943 fc_rport_init(lport);
Robert Love08076192013-03-25 11:00:28 -0700944 fc_disc_init(lport);
945 fc_disc_config(lport, lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800946 return 0;
947}
948
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000949static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800950{
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000951 int fcoe_min_xid, fcoe_max_xid;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800952
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000953 fcoe_min_xid = hba->max_xid + 1;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800954 if (nr_cpu_ids <= 2)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000955 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800956 else
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000957 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
958 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
959 fcoe_max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800960 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
961 return -ENOMEM;
962 }
963
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800964 return 0;
965}
966
967static int bnx2fc_lport_config(struct fc_lport *lport)
968{
969 lport->link_up = 0;
970 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800971 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
972 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800973 lport->e_d_tov = 2 * 1000;
974 lport->r_a_tov = 10 * 1000;
975
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800976 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
977 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800978 lport->does_npiv = 1;
979
980 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
981 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
982
983 /* alloc stats structure */
984 if (fc_lport_init_stats(lport))
985 return -ENOMEM;
986
987 /* Finish fc_lport configuration */
988 fc_lport_config(lport);
989
990 return 0;
991}
992
993/**
994 * bnx2fc_fip_recv - handle a received FIP frame.
995 *
996 * @skb: the received skb
997 * @dev: associated &net_device
998 * @ptype: the &packet_type structure which was used to register this handler.
999 * @orig_dev: original receive &net_device, in case @ dev is a bond.
1000 *
1001 * Returns: 0 for success
1002 */
1003static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1004 struct packet_type *ptype,
1005 struct net_device *orig_dev)
1006{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001007 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001008 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001009 interface = container_of(ptype, struct bnx2fc_interface,
1010 fip_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -07001011 ctlr = bnx2fc_to_ctlr(interface);
1012 fcoe_ctlr_recv(ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001013 return 0;
1014}
1015
1016/**
1017 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1018 *
1019 * @fip: FCoE controller.
1020 * @old: Unicast MAC address to delete if the MAC is non-zero.
1021 * @new: Unicast MAC address to add.
1022 *
1023 * Remove any previously-set unicast MAC filter.
1024 * Add secondary FCoE MAC address filter for our OUI.
1025 */
1026static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1027{
1028 struct fcoe_port *port = lport_priv(lport);
1029
1030 memcpy(port->data_src_addr, addr, ETH_ALEN);
1031}
1032
1033/**
1034 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1035 *
1036 * @lport: libfc port
1037 */
1038static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1039{
1040 struct fcoe_port *port;
1041
1042 port = (struct fcoe_port *)lport_priv(lport);
1043 return port->data_src_addr;
1044}
1045
1046/**
1047 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1048 *
1049 * @fip: FCoE controller.
1050 * @skb: FIP Packet.
1051 */
1052static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1053{
1054 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1055 dev_queue_xmit(skb);
1056}
1057
1058static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1059{
1060 struct Scsi_Host *shost = vport_to_shost(vport);
1061 struct fc_lport *n_port = shost_priv(shost);
1062 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001063 struct bnx2fc_interface *interface = port->priv;
1064 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001065 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001066 int rc;
1067 char buf[32];
1068
1069 rc = fcoe_validate_vport_create(vport);
1070 if (rc) {
1071 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1072 printk(KERN_ERR PFX "Failed to create vport, "
1073 "WWPN (0x%s) already exists\n",
1074 buf);
1075 return rc;
1076 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001077
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001078 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001079 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001080 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001081 return -EIO;
1082 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001083 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001084 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001085 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001086 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001087 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001088
1089 if (IS_ERR(vn_port)) {
1090 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1091 netdev->name);
1092 return -EIO;
1093 }
1094
1095 if (disabled) {
1096 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1097 } else {
1098 vn_port->boot_time = jiffies;
1099 fc_lport_init(vn_port);
1100 fc_fabric_login(vn_port);
1101 fc_vport_setlink(vn_port);
1102 }
1103 return 0;
1104}
1105
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001106static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1107{
1108 struct bnx2fc_lport *blport, *tmp;
1109
1110 spin_lock_bh(&hba->hba_lock);
1111 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1112 if (blport->lport == lport) {
1113 list_del(&blport->list);
1114 kfree(blport);
1115 }
1116 }
1117 spin_unlock_bh(&hba->hba_lock);
1118}
1119
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001120static int bnx2fc_vport_destroy(struct fc_vport *vport)
1121{
1122 struct Scsi_Host *shost = vport_to_shost(vport);
1123 struct fc_lport *n_port = shost_priv(shost);
1124 struct fc_lport *vn_port = vport->dd_data;
1125 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001126 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001127 struct fc_lport *v_port;
1128 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001129
1130 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001131 list_for_each_entry(v_port, &n_port->vports, list)
1132 if (v_port->vport == vport) {
1133 found = true;
1134 break;
1135 }
1136
1137 if (!found) {
1138 mutex_unlock(&n_port->lp_mutex);
1139 return -ENOENT;
1140 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001141 list_del(&vn_port->list);
1142 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001143 bnx2fc_free_vport(interface->hba, port->lport);
1144 bnx2fc_port_shutdown(port->lport);
1145 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001146 queue_work(bnx2fc_wq, &port->destroy_work);
1147 return 0;
1148}
1149
1150static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1151{
1152 struct fc_lport *lport = vport->dd_data;
1153
1154 if (disable) {
1155 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1156 fc_fabric_logoff(lport);
1157 } else {
1158 lport->boot_time = jiffies;
1159 fc_fabric_login(lport);
1160 fc_vport_setlink(lport);
1161 }
1162 return 0;
1163}
1164
1165
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001166static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001167{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001168 struct net_device *netdev = interface->netdev;
1169 struct net_device *physdev = interface->hba->phys_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001170 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001171 struct netdev_hw_addr *ha;
1172 int sel_san_mac = 0;
1173
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001174 /* setup Source MAC Address */
1175 rcu_read_lock();
1176 for_each_dev_addr(physdev, ha) {
1177 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1178 ha->type);
1179 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1180 ha->addr[1], ha->addr[2], ha->addr[3],
1181 ha->addr[4], ha->addr[5]);
1182
1183 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1184 (is_valid_ether_addr(ha->addr))) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001185 memcpy(ctlr->ctl_src_addr, ha->addr,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001186 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001187 sel_san_mac = 1;
1188 BNX2FC_MISC_DBG("Found SAN MAC\n");
1189 }
1190 }
1191 rcu_read_unlock();
1192
1193 if (!sel_san_mac)
1194 return -ENODEV;
1195
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001196 interface->fip_packet_type.func = bnx2fc_fip_recv;
1197 interface->fip_packet_type.type = htons(ETH_P_FIP);
1198 interface->fip_packet_type.dev = netdev;
1199 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001200
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001201 interface->fcoe_packet_type.func = bnx2fc_rcv;
1202 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1203 interface->fcoe_packet_type.dev = netdev;
1204 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001205
1206 return 0;
1207}
1208
1209static int bnx2fc_attach_transport(void)
1210{
1211 bnx2fc_transport_template =
1212 fc_attach_transport(&bnx2fc_transport_function);
1213
1214 if (bnx2fc_transport_template == NULL) {
1215 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1216 return -ENODEV;
1217 }
1218
1219 bnx2fc_vport_xport_template =
1220 fc_attach_transport(&bnx2fc_vport_xport_function);
1221 if (bnx2fc_vport_xport_template == NULL) {
1222 printk(KERN_ERR PFX
1223 "Failed to attach FC transport for vport\n");
1224 fc_release_transport(bnx2fc_transport_template);
1225 bnx2fc_transport_template = NULL;
1226 return -ENODEV;
1227 }
1228 return 0;
1229}
1230static void bnx2fc_release_transport(void)
1231{
1232 fc_release_transport(bnx2fc_transport_template);
1233 fc_release_transport(bnx2fc_vport_xport_template);
1234 bnx2fc_transport_template = NULL;
1235 bnx2fc_vport_xport_template = NULL;
1236}
1237
1238static void bnx2fc_interface_release(struct kref *kref)
1239{
Robert Love8d55e502012-05-22 19:06:26 -07001240 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001241 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001242 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001243 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001244
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001245 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001246 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001247
Robert Lovefd8f8902012-05-22 19:06:16 -07001248 ctlr = bnx2fc_to_ctlr(interface);
Robert Love8d55e502012-05-22 19:06:26 -07001249 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001250 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001251
1252 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001253 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
Robert Lovefd8f8902012-05-22 19:06:16 -07001254 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001255
Robert Love8d55e502012-05-22 19:06:26 -07001256 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001257
1258 dev_put(netdev);
1259 module_put(THIS_MODULE);
1260}
1261
1262static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1263{
1264 kref_get(&interface->kref);
1265}
1266
1267static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1268{
1269 kref_put(&interface->kref, bnx2fc_interface_release);
1270}
1271static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1272{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001273 /* Free the command manager */
1274 if (hba->cmd_mgr) {
1275 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1276 hba->cmd_mgr = NULL;
1277 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001278 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001279 bnx2fc_unbind_pcidev(hba);
1280 kfree(hba);
1281}
1282
1283/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001284 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001285 *
1286 * @cnic: pointer to cnic device
1287 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001288 * Creates a new FCoE hba on the given device.
1289 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001290 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001291static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001292{
1293 struct bnx2fc_hba *hba;
Barak Witkowski2e499d32012-06-26 01:31:19 +00001294 struct fcoe_capabilities *fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001295 int rc;
1296
1297 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1298 if (!hba) {
1299 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1300 return NULL;
1301 }
1302 spin_lock_init(&hba->hba_lock);
1303 mutex_init(&hba->hba_mutex);
1304
1305 hba->cnic = cnic;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001306
1307 hba->max_tasks = cnic->max_fcoe_exchanges;
1308 hba->elstm_xids = (hba->max_tasks / 2);
1309 hba->max_outstanding_cmds = hba->elstm_xids;
1310 hba->max_xid = (hba->max_tasks - 1);
1311
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001312 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001313 if (rc) {
1314 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001315 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001316 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001317 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001318 hba->next_conn_id = 0;
1319
1320 hba->tgt_ofld_list =
1321 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1322 GFP_KERNEL);
1323 if (!hba->tgt_ofld_list) {
1324 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1325 goto tgtofld_err;
1326 }
1327
1328 hba->num_ofld_sess = 0;
1329
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001330 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001331 if (!hba->cmd_mgr) {
1332 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1333 goto cmgr_err;
1334 }
Barak Witkowski2e499d32012-06-26 01:31:19 +00001335 fcoe_cap = &hba->fcoe_cap;
1336
1337 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1338 FCOE_IOS_PER_CONNECTION_SHIFT;
1339 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1340 FCOE_LOGINS_PER_PORT_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001341 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001342 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1343 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1344 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1345 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1346 FCOE_TARGETS_SUPPORTED_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001347 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001348 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1349 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001350
1351 init_waitqueue_head(&hba->shutdown_wait);
1352 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001353 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001354
1355 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001356
1357cmgr_err:
1358 kfree(hba->tgt_ofld_list);
1359tgtofld_err:
1360 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001361bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001362 kfree(hba);
1363 return NULL;
1364}
1365
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001366struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1367 struct net_device *netdev,
1368 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001369{
Robert Love8d55e502012-05-22 19:06:26 -07001370 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001371 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001372 struct fcoe_ctlr *ctlr;
1373 int size;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001374 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001375
Robert Lovefd8f8902012-05-22 19:06:16 -07001376 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
Robert Love8d55e502012-05-22 19:06:26 -07001377 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1378 size);
1379 if (!ctlr_dev) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001380 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1381 return NULL;
1382 }
Robert Love8d55e502012-05-22 19:06:26 -07001383 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love9d348762013-09-05 07:47:27 +00001384 ctlr->cdev = ctlr_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001385 interface = fcoe_ctlr_priv(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001386 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001387 kref_init(&interface->kref);
1388 interface->hba = hba;
1389 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001390
1391 /* Initialize FIP */
Robert Lovefd8f8902012-05-22 19:06:16 -07001392 fcoe_ctlr_init(ctlr, fip_mode);
1393 ctlr->send = bnx2fc_fip_send;
1394 ctlr->update_mac = bnx2fc_update_src_mac;
1395 ctlr->get_src_addr = bnx2fc_get_src_mac;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001396 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001397
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001398 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001399 if (!rc)
1400 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001401
Robert Lovefd8f8902012-05-22 19:06:16 -07001402 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001403 dev_put(netdev);
Robert Love8d55e502012-05-22 19:06:26 -07001404 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001405 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001406}
1407
1408/**
1409 * bnx2fc_if_create - Create FCoE instance on a given interface
1410 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001411 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001412 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1413 * @npiv: Indicates if the port is vport or not
1414 *
1415 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1416 *
1417 * Returns: Allocated fc_lport or an error pointer
1418 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001419static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001420 struct device *parent, int npiv)
1421{
Robert Lovefd8f8902012-05-22 19:06:16 -07001422 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Vasu Dev134a4e22011-04-28 15:55:44 -07001423 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001424 struct fcoe_port *port;
1425 struct Scsi_Host *shost;
1426 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001427 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001428 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001429 int rc = 0;
1430
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001431 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1432 if (!blport) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001433 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001434 return NULL;
1435 }
1436
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001437 /* Allocate Scsi_Host structure */
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001438 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
Vasu Dev134a4e22011-04-28 15:55:44 -07001439 if (!npiv)
1440 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1441 else
1442 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001443
1444 if (!lport) {
1445 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001446 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001447 }
1448 shost = lport->host;
1449 port = lport_priv(lport);
1450 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001451 port->priv = interface;
Yi Zouc3d79092012-12-06 06:24:29 +00001452 port->get_netdev = bnx2fc_netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001453 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1454
1455 /* Configure fcoe_port */
1456 rc = bnx2fc_lport_config(lport);
1457 if (rc)
1458 goto lp_config_err;
1459
1460 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001461 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1462 vport->node_name, vport->port_name);
1463 fc_set_wwnn(lport, vport->node_name);
1464 fc_set_wwpn(lport, vport->port_name);
1465 }
1466 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001467 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001468 if (rc) {
1469 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1470 goto lp_config_err;
1471 }
1472
1473 rc = bnx2fc_shost_config(lport, parent);
1474 if (rc) {
1475 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001476 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001477 goto lp_config_err;
1478 }
1479
1480 /* Initialize the libfc library */
1481 rc = bnx2fc_libfc_config(lport);
1482 if (rc) {
1483 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1484 goto shost_err;
1485 }
1486 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1487
1488 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001489 if (!npiv)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001490 rc = bnx2fc_em_config(lport, hba);
Vasu Dev134a4e22011-04-28 15:55:44 -07001491 else {
1492 shost = vport_to_shost(vport);
1493 n_port = shost_priv(shost);
1494 rc = fc_exch_mgr_list_clone(n_port, lport);
1495 }
1496
1497 if (rc) {
1498 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1499 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001500 }
1501
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001502 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001503
1504 spin_lock_bh(&hba->hba_lock);
1505 blport->lport = lport;
1506 list_add_tail(&blport->list, &hba->vports);
1507 spin_unlock_bh(&hba->hba_lock);
1508
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001509 return lport;
1510
1511shost_err:
1512 scsi_remove_host(shost);
1513lp_config_err:
1514 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001515free_blport:
1516 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001517 return NULL;
1518}
1519
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001520static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001521{
1522 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001523 __dev_remove_pack(&interface->fcoe_packet_type);
1524 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001525 synchronize_net();
1526}
1527
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001528static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001529{
Robert Lovefd8f8902012-05-22 19:06:16 -07001530 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1531 struct fc_lport *lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001532 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001533 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001534
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001535 /* Stop the transmit retry timer */
1536 del_timer_sync(&port->timer);
1537
1538 /* Free existing transmit skbs */
1539 fcoe_clean_pending_queue(lport);
1540
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001541 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001542
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001543 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001544}
1545
1546static void bnx2fc_if_destroy(struct fc_lport *lport)
1547{
1548
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001549 /* Free queued packets for the receive thread */
1550 bnx2fc_clean_rx_queue(lport);
1551
1552 /* Detach from scsi-ml */
1553 fc_remove_host(lport->host);
1554 scsi_remove_host(lport->host);
1555
1556 /*
1557 * Note that only the physical lport will have the exchange manager.
1558 * for vports, this function is NOP
1559 */
1560 fc_exch_mgr_free(lport);
1561
1562 /* Free memory used by statistical counters */
1563 fc_lport_free_stats(lport);
1564
1565 /* Release Scsi_Host */
1566 scsi_host_put(lport->host);
1567}
1568
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001569static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001570{
Robert Lovefd8f8902012-05-22 19:06:16 -07001571 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1572 struct fc_lport *lport = ctlr->lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001573 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001574
1575 bnx2fc_interface_cleanup(interface);
1576 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001577 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001578 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001579 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001580}
1581
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001582/**
1583 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1584 *
1585 * @buffer: The name of the Ethernet interface to be destroyed
1586 * @kp: The associated kernel parameter
1587 *
1588 * Called from sysfs.
1589 *
1590 * Returns: 0 for success
1591 */
1592static int bnx2fc_destroy(struct net_device *netdev)
1593{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001594 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001595 struct workqueue_struct *timer_work_queue;
Robert Lovefd8f8902012-05-22 19:06:16 -07001596 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001597 int rc = 0;
1598
Nithin Sujir6702ca12011-03-17 17:13:27 -07001599 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001600 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001601
1602 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001603 ctlr = bnx2fc_to_ctlr(interface);
1604 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001605 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001606 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001607 goto netdev_err;
1608 }
1609
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001610 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001611 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001612 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001613
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001614netdev_err:
1615 mutex_unlock(&bnx2fc_dev_lock);
1616 rtnl_unlock();
1617 return rc;
1618}
1619
1620static void bnx2fc_destroy_work(struct work_struct *work)
1621{
1622 struct fcoe_port *port;
1623 struct fc_lport *lport;
1624
1625 port = container_of(work, struct fcoe_port, destroy_work);
1626 lport = port->lport;
1627
1628 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1629
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001630 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001631}
1632
1633static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1634{
1635 bnx2fc_free_fw_resc(hba);
1636 bnx2fc_free_task_ctx(hba);
1637}
1638
1639/**
1640 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1641 * pci structure
1642 *
1643 * @hba: Adapter instance
1644 */
1645static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1646{
1647 if (bnx2fc_setup_task_ctx(hba))
1648 goto mem_err;
1649
1650 if (bnx2fc_setup_fw_resc(hba))
1651 goto mem_err;
1652
1653 return 0;
1654mem_err:
1655 bnx2fc_unbind_adapter_devices(hba);
1656 return -ENOMEM;
1657}
1658
1659static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1660{
1661 struct cnic_dev *cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001662 struct pci_dev *pdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001663
1664 if (!hba->cnic) {
1665 printk(KERN_ERR PFX "cnic is NULL\n");
1666 return -ENODEV;
1667 }
1668 cnic = hba->cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001669 pdev = hba->pcidev = cnic->pcidev;
1670 if (!hba->pcidev)
1671 return -ENODEV;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001672
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001673 switch (pdev->device) {
1674 case PCI_DEVICE_ID_NX2_57710:
1675 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1676 break;
1677 case PCI_DEVICE_ID_NX2_57711:
1678 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1679 break;
1680 case PCI_DEVICE_ID_NX2_57712:
1681 case PCI_DEVICE_ID_NX2_57712_MF:
1682 case PCI_DEVICE_ID_NX2_57712_VF:
1683 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1684 break;
1685 case PCI_DEVICE_ID_NX2_57800:
1686 case PCI_DEVICE_ID_NX2_57800_MF:
1687 case PCI_DEVICE_ID_NX2_57800_VF:
1688 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1689 break;
1690 case PCI_DEVICE_ID_NX2_57810:
1691 case PCI_DEVICE_ID_NX2_57810_MF:
1692 case PCI_DEVICE_ID_NX2_57810_VF:
1693 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1694 break;
1695 case PCI_DEVICE_ID_NX2_57840:
1696 case PCI_DEVICE_ID_NX2_57840_MF:
1697 case PCI_DEVICE_ID_NX2_57840_VF:
1698 case PCI_DEVICE_ID_NX2_57840_2_20:
1699 case PCI_DEVICE_ID_NX2_57840_4_10:
1700 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1701 break;
1702 default:
1703 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1704 break;
1705 }
1706 pci_dev_get(hba->pcidev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001707 return 0;
1708}
1709
1710static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1711{
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001712 if (hba->pcidev) {
1713 hba->chip_num[0] = '\0';
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001714 pci_dev_put(hba->pcidev);
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001715 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001716 hba->pcidev = NULL;
1717}
1718
Barak Witkowski2e499d32012-06-26 01:31:19 +00001719/**
1720 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1721 *
1722 * @handle: transport handle pointing to adapter struture
1723 */
1724static int bnx2fc_ulp_get_stats(void *handle)
1725{
1726 struct bnx2fc_hba *hba = handle;
1727 struct cnic_dev *cnic;
1728 struct fcoe_stats_info *stats_addr;
1729
1730 if (!hba)
1731 return -EINVAL;
1732
1733 cnic = hba->cnic;
1734 stats_addr = &cnic->stats_addr->fcoe_stat;
1735 if (!stats_addr)
1736 return -EINVAL;
1737
1738 strncpy(stats_addr->version, BNX2FC_VERSION,
1739 sizeof(stats_addr->version));
1740 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1741 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1742
1743 return 0;
1744}
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001745
1746
1747/**
1748 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1749 *
Masanari Iida59e13d42012-04-25 00:24:16 +09001750 * @handle: transport handle pointing to adapter structure
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001751 *
1752 * This function maps adapter structure to pcidev structure and initiates
1753 * firmware handshake to enable/initialize on-chip FCoE components.
1754 * This bnx2fc - cnic interface api callback is used after following
1755 * conditions are met -
1756 * a) underlying network interface is up (marked by event NETDEV_UP
1757 * from netdev
1758 * b) bnx2fc adatper structure is registered.
1759 */
1760static void bnx2fc_ulp_start(void *handle)
1761{
1762 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001763 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001764 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001765 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001766
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001767 mutex_lock(&bnx2fc_dev_lock);
1768
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001769 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001770 bnx2fc_fw_init(hba);
1771
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001772 BNX2FC_MISC_DBG("bnx2fc started.\n");
1773
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001774 list_for_each_entry(interface, &if_list, list) {
1775 if (interface->hba == hba) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001776 ctlr = bnx2fc_to_ctlr(interface);
1777 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001778 /* Kick off Fabric discovery*/
1779 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1780 lport->tt.frame_send = bnx2fc_xmit;
1781 bnx2fc_start_disc(interface);
1782 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001783 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001784
1785 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001786}
1787
1788static void bnx2fc_port_shutdown(struct fc_lport *lport)
1789{
1790 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1791 fc_fabric_logoff(lport);
1792 fc_lport_destroy(lport);
1793}
1794
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001795static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001796{
Robert Lovefd8f8902012-05-22 19:06:16 -07001797 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001798 struct fc_lport *lport;
1799 struct fc_lport *vport;
1800
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001801 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1802 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001803
Robert Lovefd8f8902012-05-22 19:06:16 -07001804 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001805 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001806
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001807 mutex_lock(&lport->lp_mutex);
1808 list_for_each_entry(vport, &lport->vports, list)
1809 fc_host_port_type(vport->host) =
1810 FC_PORTTYPE_UNKNOWN;
1811 mutex_unlock(&lport->lp_mutex);
1812 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
Robert Lovefd8f8902012-05-22 19:06:16 -07001813 fcoe_ctlr_link_down(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001814 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001815}
1816
1817static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1818{
1819#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1820 int rc = -1;
1821 int i = HZ;
1822
1823 rc = bnx2fc_bind_adapter_devices(hba);
1824 if (rc) {
1825 printk(KERN_ALERT PFX
1826 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1827 goto err_out;
1828 }
1829
1830 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1831 if (rc) {
1832 printk(KERN_ALERT PFX
1833 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1834 goto err_unbind;
1835 }
1836
1837 /*
1838 * Wait until the adapter init message is complete, and adapter
1839 * state is UP.
1840 */
1841 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1842 msleep(BNX2FC_INIT_POLL_TIME);
1843
1844 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1845 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1846 "Ignoring...\n",
1847 hba->cnic->netdev->name);
1848 rc = -1;
1849 goto err_unbind;
1850 }
1851
1852
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001853 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001854 return 0;
1855
1856err_unbind:
1857 bnx2fc_unbind_adapter_devices(hba);
1858err_out:
1859 return rc;
1860}
1861
1862static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1863{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001864 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001865 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1866 init_timer(&hba->destroy_timer);
1867 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1868 jiffies;
1869 hba->destroy_timer.function = bnx2fc_destroy_timer;
1870 hba->destroy_timer.data = (unsigned long)hba;
1871 add_timer(&hba->destroy_timer);
1872 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001873 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1874 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001875 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001876 /* This should never happen */
1877 if (signal_pending(current))
1878 flush_signals(current);
1879
1880 del_timer_sync(&hba->destroy_timer);
1881 }
1882 bnx2fc_unbind_adapter_devices(hba);
1883 }
1884}
1885
1886/**
1887 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1888 *
1889 * @handle: transport handle pointing to adapter structure
1890 *
1891 * Driver checks if adapter is already in shutdown mode, if not start
1892 * the shutdown process.
1893 */
1894static void bnx2fc_ulp_stop(void *handle)
1895{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001896 struct bnx2fc_hba *hba = handle;
1897 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001898
1899 printk(KERN_ERR "ULP_STOP\n");
1900
1901 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001902 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1903 goto exit;
1904 list_for_each_entry(interface, &if_list, list) {
1905 if (interface->hba == hba)
1906 bnx2fc_stop(interface);
1907 }
1908 BUG_ON(hba->num_ofld_sess != 0);
1909
1910 mutex_lock(&hba->hba_mutex);
1911 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1912 clear_bit(ADAPTER_STATE_GOING_DOWN,
1913 &hba->adapter_state);
1914
1915 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1916 mutex_unlock(&hba->hba_mutex);
1917
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001918 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001919exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001920 mutex_unlock(&bnx2fc_dev_lock);
1921}
1922
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001923static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001924{
Robert Lovefd8f8902012-05-22 19:06:16 -07001925 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001926 struct fc_lport *lport;
1927 int wait_cnt = 0;
1928
1929 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1930 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001931 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001932 printk(KERN_ERR PFX "Init not done yet\n");
1933 return;
1934 }
1935
Robert Lovefd8f8902012-05-22 19:06:16 -07001936 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001937 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1938
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001939 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001940 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001941 fcoe_ctlr_link_up(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001942 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001943 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001944 }
1945
1946 /* wait for the FCF to be selected before issuing FLOGI */
Robert Lovefd8f8902012-05-22 19:06:16 -07001947 while (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001948 msleep(250);
1949 /* give up after 3 secs */
1950 if (++wait_cnt > 12)
1951 break;
1952 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001953
1954 /* Reset max receive frame size to default */
1955 if (fc_set_mfs(lport, BNX2FC_MFS))
1956 return;
1957
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001958 fc_lport_init(lport);
1959 fc_fabric_login(lport);
1960}
1961
1962
1963/**
1964 * bnx2fc_ulp_init - Initialize an adapter instance
1965 *
1966 * @dev : cnic device handle
1967 * Called from cnic_register_driver() context to initialize all
1968 * enumerated cnic devices. This routine allocates adapter structure
1969 * and other device specific resources.
1970 */
1971static void bnx2fc_ulp_init(struct cnic_dev *dev)
1972{
1973 struct bnx2fc_hba *hba;
1974 int rc = 0;
1975
1976 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1977 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001978 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1979 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001980 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001981 " flags: %lx fcoe_conn: %d\n",
1982 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001983 return;
1984 }
1985
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001986 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001987 if (!hba) {
1988 printk(KERN_ERR PFX "hba initialization failed\n");
1989 return;
1990 }
1991
1992 /* Add HBA to the adapter list */
1993 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001994 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001995 adapter_count++;
1996 mutex_unlock(&bnx2fc_dev_lock);
1997
Barak Witkowski2e499d32012-06-26 01:31:19 +00001998 dev->fcoe_cap = &hba->fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001999 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2000 rc = dev->register_device(dev, CNIC_ULP_FCOE,
2001 (void *) hba);
2002 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07002003 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002004 else
2005 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2006}
2007
Robert Love6e89ea32012-11-27 06:53:40 +00002008/**
2009 * Deperecated: Use bnx2fc_enabled()
2010 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002011static int bnx2fc_disable(struct net_device *netdev)
2012{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002013 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002014 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002015 int rc = 0;
2016
Nithin Sujir6702ca12011-03-17 17:13:27 -07002017 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002018 mutex_lock(&bnx2fc_dev_lock);
2019
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002020 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002021 ctlr = bnx2fc_to_ctlr(interface);
2022 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002023 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002024 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002025 } else {
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002026 interface->enabled = false;
Robert Lovefd8f8902012-05-22 19:06:16 -07002027 fcoe_ctlr_link_down(ctlr);
2028 fcoe_clean_pending_queue(ctlr->lp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002029 }
2030
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002031 mutex_unlock(&bnx2fc_dev_lock);
2032 rtnl_unlock();
2033 return rc;
2034}
2035
Robert Love6e89ea32012-11-27 06:53:40 +00002036/**
2037 * Deprecated: Use bnx2fc_enabled()
2038 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002039static int bnx2fc_enable(struct net_device *netdev)
2040{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002041 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002042 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002043 int rc = 0;
2044
Nithin Sujir6702ca12011-03-17 17:13:27 -07002045 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002046 mutex_lock(&bnx2fc_dev_lock);
2047
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002048 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002049 ctlr = bnx2fc_to_ctlr(interface);
2050 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002051 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002052 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07002053 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2054 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002055 interface->enabled = true;
2056 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002057
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002058 mutex_unlock(&bnx2fc_dev_lock);
2059 rtnl_unlock();
2060 return rc;
2061}
2062
2063/**
Robert Love6e89ea32012-11-27 06:53:40 +00002064 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2065 * @cdev: The FCoE Controller that is being enabled or disabled
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002066 *
Robert Love6e89ea32012-11-27 06:53:40 +00002067 * fcoe_sysfs will ensure that the state of 'enabled' has
2068 * changed, so no checking is necessary here. This routine simply
2069 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2070 * When those routines are removed the functionality can be merged
2071 * here.
2072 */
2073static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2074{
2075 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2076 struct fc_lport *lport = ctlr->lp;
2077 struct net_device *netdev = bnx2fc_netdev(lport);
2078
2079 switch (cdev->enabled) {
2080 case FCOE_CTLR_ENABLED:
2081 return bnx2fc_enable(netdev);
2082 case FCOE_CTLR_DISABLED:
2083 return bnx2fc_disable(netdev);
2084 case FCOE_CTLR_UNUSED:
2085 default:
2086 return -ENOTSUPP;
2087 };
2088}
2089
2090enum bnx2fc_create_link_state {
2091 BNX2FC_CREATE_LINK_DOWN,
2092 BNX2FC_CREATE_LINK_UP,
2093};
2094
2095/**
2096 * _bnx2fc_create() - Create bnx2fc FCoE interface
2097 * @netdev : The net_device object the Ethernet interface to create on
2098 * @fip_mode: The FIP mode for this creation
2099 * @link_state: The ctlr link state on creation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002100 *
Robert Love6e89ea32012-11-27 06:53:40 +00002101 * Called from either the libfcoe 'create' module parameter
2102 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2103 *
2104 * libfcoe's 'create' module parameter is deprecated so some
2105 * consolidation of code can be done when that interface is
2106 * removed.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002107 *
2108 * Returns: 0 for success
2109 */
Robert Love6e89ea32012-11-27 06:53:40 +00002110static int _bnx2fc_create(struct net_device *netdev,
2111 enum fip_state fip_mode,
2112 enum bnx2fc_create_link_state link_state)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002113{
Robert Love6e89ea32012-11-27 06:53:40 +00002114 struct fcoe_ctlr_device *cdev;
Robert Lovefd8f8902012-05-22 19:06:16 -07002115 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002116 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002117 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002118 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002119 struct fc_lport *lport;
2120 struct ethtool_drvinfo drvinfo;
2121 int rc = 0;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002122 int vlan_id = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002123
2124 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2125 if (fip_mode != FIP_MODE_FABRIC) {
2126 printk(KERN_ERR "fip mode not FABRIC\n");
2127 return -EIO;
2128 }
2129
Nithin Sujir6702ca12011-03-17 17:13:27 -07002130 rtnl_lock();
2131
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002132 mutex_lock(&bnx2fc_dev_lock);
2133
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002134 if (!try_module_get(THIS_MODULE)) {
2135 rc = -EINVAL;
2136 goto mod_err;
2137 }
2138
2139 /* obtain physical netdev */
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002140 if (netdev->priv_flags & IFF_802_1Q_VLAN)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002141 phys_dev = vlan_dev_real_dev(netdev);
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002142
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002143 /* verify if the physical device is a netxtreme2 device */
2144 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2145 memset(&drvinfo, 0, sizeof(drvinfo));
2146 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002147 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002148 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2149 rc = -EINVAL;
2150 goto netdev_err;
2151 }
2152 } else {
2153 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2154 rc = -EINVAL;
2155 goto netdev_err;
2156 }
2157
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002158 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002159 hba = bnx2fc_hba_lookup(phys_dev);
2160 if (!hba) {
2161 rc = -ENODEV;
2162 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2163 goto netdev_err;
2164 }
2165
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002166 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002167 rc = -EEXIST;
2168 goto netdev_err;
2169 }
2170
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002171 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2172 if (!interface) {
2173 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002174 goto ifput_err;
2175 }
2176
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002177 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2178 vlan_id = vlan_dev_vlan_id(netdev);
2179 interface->vlan_enabled = 1;
2180 }
2181
Robert Lovefd8f8902012-05-22 19:06:16 -07002182 ctlr = bnx2fc_to_ctlr(interface);
Robert Love01bdcb62013-03-25 11:00:26 -07002183 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002184 interface->vlan_id = vlan_id;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002185
2186 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002187 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002188 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002189 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2190 rc = -EINVAL;
2191 goto ifput_err;
2192 }
2193
Robert Love01bdcb62013-03-25 11:00:26 -07002194 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002195 if (!lport) {
2196 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2197 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002198 rc = -EINVAL;
2199 goto if_create_err;
2200 }
2201
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002202 /* Add interface to if_list */
2203 list_add_tail(&interface->list, &if_list);
2204
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002205 lport->boot_time = jiffies;
2206
2207 /* Make this master N_port */
Robert Lovefd8f8902012-05-22 19:06:16 -07002208 ctlr->lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002209
Robert Love6e89ea32012-11-27 06:53:40 +00002210 if (link_state == BNX2FC_CREATE_LINK_UP)
2211 cdev->enabled = FCOE_CTLR_ENABLED;
2212 else
2213 cdev->enabled = FCOE_CTLR_DISABLED;
2214
2215 if (link_state == BNX2FC_CREATE_LINK_UP &&
2216 !bnx2fc_link_ok(lport)) {
Robert Lovefd8f8902012-05-22 19:06:16 -07002217 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002218 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2219 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2220 }
2221
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002222 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2223 bnx2fc_start_disc(interface);
Robert Love6e89ea32012-11-27 06:53:40 +00002224
2225 if (link_state == BNX2FC_CREATE_LINK_UP)
2226 interface->enabled = true;
2227
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002228 /*
2229 * Release from kref_init in bnx2fc_interface_setup, on success
2230 * lport should be holding a reference taken in bnx2fc_if_create
2231 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002232 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002233 /* put netdev that was held while calling dev_get_by_name */
2234 mutex_unlock(&bnx2fc_dev_lock);
2235 rtnl_unlock();
2236 return 0;
2237
2238if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002239 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002240ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002241 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002242 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002243 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002244netdev_err:
2245 module_put(THIS_MODULE);
2246mod_err:
2247 mutex_unlock(&bnx2fc_dev_lock);
2248 rtnl_unlock();
2249 return rc;
2250}
2251
2252/**
Robert Love6e89ea32012-11-27 06:53:40 +00002253 * bnx2fc_create() - Create a bnx2fc interface
2254 * @netdev : The net_device object the Ethernet interface to create on
2255 * @fip_mode: The FIP mode for this creation
2256 *
2257 * Called from fcoe transport
2258 *
2259 * Returns: 0 for success
2260 */
2261static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2262{
2263 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2264}
2265
2266/**
2267 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2268 * @netdev: The net_device to be used by the allocated FCoE Controller
2269 *
2270 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2271 * in a link_down state. The allows the user an opportunity to configure
2272 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2273 *
2274 * Creating in with this routine starts the FCoE Controller in Fabric
2275 * mode. The user can change to VN2VN or another mode before enabling.
2276 */
2277static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2278{
2279 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2280 BNX2FC_CREATE_LINK_DOWN);
2281}
2282
2283/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002284 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002285 *
2286 * @cnic: Pointer to cnic device instance
2287 *
2288 **/
2289static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2290{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002291 struct bnx2fc_hba *hba;
2292
2293 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -07002294 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002295 if (hba->cnic == cnic)
2296 return hba;
2297 }
2298 return NULL;
2299}
2300
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002301static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2302 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002303{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002304 struct bnx2fc_interface *interface;
2305
2306 /* Called with bnx2fc_dev_lock held */
2307 list_for_each_entry(interface, &if_list, list) {
2308 if (interface->netdev == netdev)
2309 return interface;
2310 }
2311 return NULL;
2312}
2313
2314static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2315 *phys_dev)
2316{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002317 struct bnx2fc_hba *hba;
2318
2319 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002320 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002321 if (hba->phys_dev == phys_dev)
2322 return hba;
2323 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002324 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002325 return NULL;
2326}
2327
2328/**
2329 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2330 *
2331 * @dev cnic device handle
2332 */
2333static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2334{
2335 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002336 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002337
2338 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2339
2340 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2341 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2342 dev->netdev->name, dev->flags);
2343 return;
2344 }
2345
2346 mutex_lock(&bnx2fc_dev_lock);
2347 hba = bnx2fc_find_hba_for_cnic(dev);
2348 if (!hba) {
2349 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2350 dev);
2351 mutex_unlock(&bnx2fc_dev_lock);
2352 return;
2353 }
2354
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002355 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002356 adapter_count--;
2357
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002358 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002359 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002360 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002361 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002362 mutex_unlock(&bnx2fc_dev_lock);
2363
2364 bnx2fc_ulp_stop(hba);
2365 /* unregister cnic device */
2366 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2367 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002368 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002369}
2370
2371/**
2372 * bnx2fc_fcoe_reset - Resets the fcoe
2373 *
2374 * @shost: shost the reset is from
2375 *
2376 * Returns: always 0
2377 */
2378static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2379{
2380 struct fc_lport *lport = shost_priv(shost);
2381 fc_lport_reset(lport);
2382 return 0;
2383}
2384
2385
2386static bool bnx2fc_match(struct net_device *netdev)
2387{
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002388 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002389
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002390 mutex_lock(&bnx2fc_dev_lock);
2391 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2392 phys_dev = vlan_dev_real_dev(netdev);
2393
2394 if (bnx2fc_hba_lookup(phys_dev)) {
2395 mutex_unlock(&bnx2fc_dev_lock);
2396 return true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002397 }
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002398
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002399 mutex_unlock(&bnx2fc_dev_lock);
2400 return false;
2401}
2402
2403
2404static struct fcoe_transport bnx2fc_transport = {
2405 .name = {"bnx2fc"},
2406 .attached = false,
2407 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
Robert Love6e89ea32012-11-27 06:53:40 +00002408 .alloc = bnx2fc_ctlr_alloc,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002409 .match = bnx2fc_match,
2410 .create = bnx2fc_create,
2411 .destroy = bnx2fc_destroy,
2412 .enable = bnx2fc_enable,
2413 .disable = bnx2fc_disable,
2414};
2415
2416/**
2417 * bnx2fc_percpu_thread_create - Create a receive thread for an
2418 * online CPU
2419 *
2420 * @cpu: cpu index for the online cpu
2421 */
2422static void bnx2fc_percpu_thread_create(unsigned int cpu)
2423{
2424 struct bnx2fc_percpu_s *p;
2425 struct task_struct *thread;
2426
2427 p = &per_cpu(bnx2fc_percpu, cpu);
2428
Eric Dumazet69614272012-06-04 16:15:42 -07002429 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2430 (void *)p, cpu_to_node(cpu),
2431 "bnx2fc_thread/%d", cpu);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002432 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002433 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002434 kthread_bind(thread, cpu);
2435 p->iothread = thread;
2436 wake_up_process(thread);
2437 }
2438}
2439
2440static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2441{
2442 struct bnx2fc_percpu_s *p;
2443 struct task_struct *thread;
2444 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002445
2446 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2447
2448 /* Prevent any new work from being queued for this CPU */
2449 p = &per_cpu(bnx2fc_percpu, cpu);
2450 spin_lock_bh(&p->fp_work_lock);
2451 thread = p->iothread;
2452 p->iothread = NULL;
2453
2454
2455 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002456 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002457 list_del_init(&work->list);
2458 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2459 kfree(work);
2460 }
2461
2462 spin_unlock_bh(&p->fp_work_lock);
2463
2464 if (thread)
2465 kthread_stop(thread);
2466}
2467
2468/**
2469 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2470 *
2471 * @nfb: The callback data block
2472 * @action: The event triggering the callback
2473 * @hcpu: The index of the CPU that the event is for
2474 *
2475 * This creates or destroys per-CPU data for fcoe
2476 *
2477 * Returns NOTIFY_OK always.
2478 */
2479static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2480 unsigned long action, void *hcpu)
2481{
2482 unsigned cpu = (unsigned long)hcpu;
2483
2484 switch (action) {
2485 case CPU_ONLINE:
2486 case CPU_ONLINE_FROZEN:
2487 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2488 bnx2fc_percpu_thread_create(cpu);
2489 break;
2490 case CPU_DEAD:
2491 case CPU_DEAD_FROZEN:
2492 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2493 bnx2fc_percpu_thread_destroy(cpu);
2494 break;
2495 default:
2496 break;
2497 }
2498 return NOTIFY_OK;
2499}
2500
2501/**
2502 * bnx2fc_mod_init - module init entry point
2503 *
2504 * Initialize driver wide global data structures, and register
2505 * with cnic module
2506 **/
2507static int __init bnx2fc_mod_init(void)
2508{
2509 struct fcoe_percpu_s *bg;
2510 struct task_struct *l2_thread;
2511 int rc = 0;
2512 unsigned int cpu = 0;
2513 struct bnx2fc_percpu_s *p;
2514
2515 printk(KERN_INFO PFX "%s", version);
2516
2517 /* register as a fcoe transport */
2518 rc = fcoe_transport_attach(&bnx2fc_transport);
2519 if (rc) {
2520 printk(KERN_ERR "failed to register an fcoe transport, check "
2521 "if libfcoe is loaded\n");
2522 goto out;
2523 }
2524
2525 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002526 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002527 mutex_init(&bnx2fc_dev_lock);
2528 adapter_count = 0;
2529
2530 /* Attach FC transport template */
2531 rc = bnx2fc_attach_transport();
2532 if (rc)
2533 goto detach_ft;
2534
2535 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2536 if (!bnx2fc_wq) {
2537 rc = -ENOMEM;
2538 goto release_bt;
2539 }
2540
2541 bg = &bnx2fc_global;
2542 skb_queue_head_init(&bg->fcoe_rx_list);
2543 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2544 (void *)bg,
2545 "bnx2fc_l2_thread");
2546 if (IS_ERR(l2_thread)) {
2547 rc = PTR_ERR(l2_thread);
2548 goto free_wq;
2549 }
2550 wake_up_process(l2_thread);
2551 spin_lock_bh(&bg->fcoe_rx_list.lock);
2552 bg->thread = l2_thread;
2553 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2554
2555 for_each_possible_cpu(cpu) {
2556 p = &per_cpu(bnx2fc_percpu, cpu);
2557 INIT_LIST_HEAD(&p->work_list);
2558 spin_lock_init(&p->fp_work_lock);
2559 }
2560
2561 for_each_online_cpu(cpu) {
2562 bnx2fc_percpu_thread_create(cpu);
2563 }
2564
2565 /* Initialize per CPU interrupt thread */
2566 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2567
2568 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2569
2570 return 0;
2571
2572free_wq:
2573 destroy_workqueue(bnx2fc_wq);
2574release_bt:
2575 bnx2fc_release_transport();
2576detach_ft:
2577 fcoe_transport_detach(&bnx2fc_transport);
2578out:
2579 return rc;
2580}
2581
2582static void __exit bnx2fc_mod_exit(void)
2583{
2584 LIST_HEAD(to_be_deleted);
2585 struct bnx2fc_hba *hba, *next;
2586 struct fcoe_percpu_s *bg;
2587 struct task_struct *l2_thread;
2588 struct sk_buff *skb;
2589 unsigned int cpu = 0;
2590
2591 /*
2592 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2593 * it will have higher precedence than bnx2fc_dev_lock.
2594 * unregister_device() cannot be called with bnx2fc_dev_lock
2595 * held.
2596 */
2597 mutex_lock(&bnx2fc_dev_lock);
2598 list_splice(&adapter_list, &to_be_deleted);
2599 INIT_LIST_HEAD(&adapter_list);
2600 adapter_count = 0;
2601 mutex_unlock(&bnx2fc_dev_lock);
2602
2603 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002604 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2605 list_del_init(&hba->list);
2606 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2607 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002608 bnx2fc_ulp_stop(hba);
2609 /* unregister cnic device */
2610 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2611 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002612 hba->cnic->unregister_device(hba->cnic,
2613 CNIC_ULP_FCOE);
2614 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002615 }
2616 cnic_unregister_driver(CNIC_ULP_FCOE);
2617
2618 /* Destroy global thread */
2619 bg = &bnx2fc_global;
2620 spin_lock_bh(&bg->fcoe_rx_list.lock);
2621 l2_thread = bg->thread;
2622 bg->thread = NULL;
2623 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2624 kfree_skb(skb);
2625
2626 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2627
2628 if (l2_thread)
2629 kthread_stop(l2_thread);
2630
2631 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2632
2633 /* Destroy per cpu threads */
2634 for_each_online_cpu(cpu) {
2635 bnx2fc_percpu_thread_destroy(cpu);
2636 }
2637
2638 destroy_workqueue(bnx2fc_wq);
2639 /*
2640 * detach from scsi transport
2641 * must happen after all destroys are done
2642 */
2643 bnx2fc_release_transport();
2644
2645 /* detach from fcoe transport */
2646 fcoe_transport_detach(&bnx2fc_transport);
2647}
2648
2649module_init(bnx2fc_mod_init);
2650module_exit(bnx2fc_mod_exit);
2651
Robert Love8d55e502012-05-22 19:06:26 -07002652static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
Robert Love6e89ea32012-11-27 06:53:40 +00002653 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
Yi Zoub8b3e692012-12-06 06:24:59 +00002654 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2655 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2656 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2657 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2658 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2659 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
Robert Love8d55e502012-05-22 19:06:26 -07002660
2661 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2662 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2663};
2664
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002665static struct fc_function_template bnx2fc_transport_function = {
2666 .show_host_node_name = 1,
2667 .show_host_port_name = 1,
2668 .show_host_supported_classes = 1,
2669 .show_host_supported_fc4s = 1,
2670 .show_host_active_fc4s = 1,
2671 .show_host_maxframe_size = 1,
2672
2673 .show_host_port_id = 1,
2674 .show_host_supported_speeds = 1,
2675 .get_host_speed = fc_get_host_speed,
2676 .show_host_speed = 1,
2677 .show_host_port_type = 1,
2678 .get_host_port_state = fc_get_host_port_state,
2679 .show_host_port_state = 1,
2680 .show_host_symbolic_name = 1,
2681
2682 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2683 sizeof(struct bnx2fc_rport)),
2684 .show_rport_maxframe_size = 1,
2685 .show_rport_supported_classes = 1,
2686
2687 .show_host_fabric_name = 1,
2688 .show_starget_node_name = 1,
2689 .show_starget_port_name = 1,
2690 .show_starget_port_id = 1,
2691 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2692 .show_rport_dev_loss_tmo = 1,
2693 .get_fc_host_stats = bnx2fc_get_host_stats,
2694
2695 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2696
2697 .terminate_rport_io = fc_rport_terminate_io,
2698
2699 .vport_create = bnx2fc_vport_create,
2700 .vport_delete = bnx2fc_vport_destroy,
2701 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002702 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002703};
2704
2705static struct fc_function_template bnx2fc_vport_xport_function = {
2706 .show_host_node_name = 1,
2707 .show_host_port_name = 1,
2708 .show_host_supported_classes = 1,
2709 .show_host_supported_fc4s = 1,
2710 .show_host_active_fc4s = 1,
2711 .show_host_maxframe_size = 1,
2712
2713 .show_host_port_id = 1,
2714 .show_host_supported_speeds = 1,
2715 .get_host_speed = fc_get_host_speed,
2716 .show_host_speed = 1,
2717 .show_host_port_type = 1,
2718 .get_host_port_state = fc_get_host_port_state,
2719 .show_host_port_state = 1,
2720 .show_host_symbolic_name = 1,
2721
2722 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2723 sizeof(struct bnx2fc_rport)),
2724 .show_rport_maxframe_size = 1,
2725 .show_rport_supported_classes = 1,
2726
2727 .show_host_fabric_name = 1,
2728 .show_starget_node_name = 1,
2729 .show_starget_port_name = 1,
2730 .show_starget_port_id = 1,
2731 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2732 .show_rport_dev_loss_tmo = 1,
2733 .get_fc_host_stats = fc_get_host_stats,
2734 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2735 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002736 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002737};
2738
2739/**
2740 * scsi_host_template structure used while registering with SCSI-ml
2741 */
2742static struct scsi_host_template bnx2fc_shost_template = {
2743 .module = THIS_MODULE,
2744 .name = "Broadcom Offload FCoE Initiator",
2745 .queuecommand = bnx2fc_queuecommand,
2746 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2747 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2748 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2749 .eh_host_reset_handler = fc_eh_host_reset,
2750 .slave_alloc = fc_slave_alloc,
2751 .change_queue_depth = fc_change_queue_depth,
2752 .change_queue_type = fc_change_queue_type,
2753 .this_id = -1,
2754 .cmd_per_lun = 3,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002755 .use_clustering = ENABLE_CLUSTERING,
2756 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
Bhanu Prakash Gollapudid450d772012-12-21 19:40:33 -08002757 .max_sectors = 1024,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002758};
2759
2760static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2761 .frame_send = bnx2fc_xmit,
2762 .elsct_send = bnx2fc_elsct_send,
2763 .fcp_abort_io = bnx2fc_abort_io,
2764 .fcp_cleanup = bnx2fc_cleanup,
Yi Zoub8b3e692012-12-06 06:24:59 +00002765 .get_lesb = fcoe_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002766 .rport_event_callback = bnx2fc_rport_event_handler,
2767};
2768
2769/**
2770 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2771 * structure carrying callback function pointers
2772 */
2773static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2774 .owner = THIS_MODULE,
2775 .cnic_init = bnx2fc_ulp_init,
2776 .cnic_exit = bnx2fc_ulp_exit,
2777 .cnic_start = bnx2fc_ulp_start,
2778 .cnic_stop = bnx2fc_ulp_stop,
2779 .indicate_kcqes = bnx2fc_indicate_kcqe,
2780 .indicate_netevent = bnx2fc_indicate_netevent,
Barak Witkowski2e499d32012-06-26 01:31:19 +00002781 .cnic_get_stats = bnx2fc_ulp_get_stats,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002782};