blob: 79e5c94107a9cc44fe8269f55ab72e8150005e0b [file] [log] [blame]
Saurav Kashyap17d87c42014-07-03 08:18:28 -04001/* bnx2fc_fcoe.c: QLogic NetXtreme II Linux FCoE offload driver.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002 * 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
Saurav Kashyap17d87c42014-07-03 08:18:28 -04007 * Copyright (c) 2014, QLogic Corporation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 *
13 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
14 */
15
16#include "bnx2fc.h"
17
18static struct list_head adapter_list;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070019static struct list_head if_list;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080020static u32 adapter_count;
21static DEFINE_MUTEX(bnx2fc_dev_lock);
22DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
23
24#define DRV_MODULE_NAME "bnx2fc"
25#define DRV_MODULE_VERSION BNX2FC_VERSION
Eddie Wai38e83bf2013-12-11 15:30:23 -080026#define DRV_MODULE_RELDATE "Dec 11, 2013"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080027
28
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080029static char version[] =
Saurav Kashyap17d87c42014-07-03 08:18:28 -040030 "QLogic NetXtreme II FCoE Driver " DRV_MODULE_NAME \
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080031 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
32
33
34MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
Saurav Kashyap17d87c42014-07-03 08:18:28 -040035MODULE_DESCRIPTION("QLogic NetXtreme II BCM57710 FCoE Driver");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080036MODULE_LICENSE("GPL");
37MODULE_VERSION(DRV_MODULE_VERSION);
38
39#define BNX2FC_MAX_QUEUE_DEPTH 256
40#define BNX2FC_MIN_QUEUE_DEPTH 32
41#define FCOE_WORD_TO_BYTE 4
42
43static struct scsi_transport_template *bnx2fc_transport_template;
44static struct scsi_transport_template *bnx2fc_vport_xport_template;
45
46struct workqueue_struct *bnx2fc_wq;
47
48/* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
49 * Here the io threads are per cpu but the l2 thread is just one
50 */
51struct fcoe_percpu_s bnx2fc_global;
52DEFINE_SPINLOCK(bnx2fc_global_lock);
53
54static struct cnic_ulp_ops bnx2fc_cnic_cb;
55static struct libfc_function_template bnx2fc_libfc_fcn_templ;
56static struct scsi_host_template bnx2fc_shost_template;
57static struct fc_function_template bnx2fc_transport_function;
Robert Love8d55e502012-05-22 19:06:26 -070058static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080059static struct fc_function_template bnx2fc_vport_xport_function;
60static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -070061static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080062static int bnx2fc_destroy(struct net_device *net_device);
63static int bnx2fc_enable(struct net_device *netdev);
64static int bnx2fc_disable(struct net_device *netdev);
65
Robert Love6e89ea32012-11-27 06:53:40 +000066/* fcoe_syfs control interface handlers */
67static int bnx2fc_ctlr_alloc(struct net_device *netdev);
68static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
69
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080070static void bnx2fc_recv_frame(struct sk_buff *skb);
71
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070072static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080073static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080074static int bnx2fc_lport_config(struct fc_lport *lport);
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +000075static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080076static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
77static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
78static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
79static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070080static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080081 struct device *parent, int npiv);
82static void bnx2fc_destroy_work(struct work_struct *work);
83
84static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070085static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
86 *phys_dev);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -070087static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080088static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
89
90static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
91static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
92
93static void bnx2fc_port_shutdown(struct fc_lport *lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070094static void bnx2fc_stop(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080095static int __init bnx2fc_mod_init(void);
96static void __exit bnx2fc_mod_exit(void);
97
98unsigned int bnx2fc_debug_level;
99module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
100
101static int bnx2fc_cpu_callback(struct notifier_block *nfb,
102 unsigned long action, void *hcpu);
103/* notification function for CPU hotplug events */
104static struct notifier_block bnx2fc_cpu_notifier = {
105 .notifier_call = bnx2fc_cpu_callback,
106};
107
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -0700108static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
109{
110 return ((struct bnx2fc_interface *)
111 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
112}
113
Robert Love8d55e502012-05-22 19:06:26 -0700114static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
115{
116 struct fcoe_ctlr_device *ctlr_dev =
117 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
118 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
119 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
120
121 fcf_dev->vlan_id = fcoe->vlan_id;
122}
123
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800124static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
125{
126 struct fcoe_percpu_s *bg;
127 struct fcoe_rcv_info *fr;
128 struct sk_buff_head *list;
129 struct sk_buff *skb, *next;
130 struct sk_buff *head;
131
132 bg = &bnx2fc_global;
133 spin_lock_bh(&bg->fcoe_rx_list.lock);
134 list = &bg->fcoe_rx_list;
135 head = list->next;
136 for (skb = head; skb != (struct sk_buff *)list;
137 skb = next) {
138 next = skb->next;
139 fr = fcoe_dev_from_skb(skb);
140 if (fr->fr_dev == lp) {
141 __skb_unlink(skb, list);
142 kfree_skb(skb);
143 }
144 }
145 spin_unlock_bh(&bg->fcoe_rx_list.lock);
146}
147
148int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
149{
150 int rc;
151 spin_lock(&bnx2fc_global_lock);
152 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
153 spin_unlock(&bnx2fc_global_lock);
154
155 return rc;
156}
157
158static void bnx2fc_abort_io(struct fc_lport *lport)
159{
160 /*
161 * This function is no-op for bnx2fc, but we do
162 * not want to leave it as NULL either, as libfc
163 * can call the default function which is
164 * fc_fcp_abort_io.
165 */
166}
167
168static void bnx2fc_cleanup(struct fc_lport *lport)
169{
170 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700171 struct bnx2fc_interface *interface = port->priv;
172 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800173 struct bnx2fc_rport *tgt;
174 int i;
175
176 BNX2FC_MISC_DBG("Entered %s\n", __func__);
177 mutex_lock(&hba->hba_mutex);
178 spin_lock_bh(&hba->hba_lock);
179 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
180 tgt = hba->tgt_ofld_list[i];
181 if (tgt) {
182 /* Cleanup IOs belonging to requested vport */
183 if (tgt->port == port) {
184 spin_unlock_bh(&hba->hba_lock);
185 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
186 bnx2fc_flush_active_ios(tgt);
187 spin_lock_bh(&hba->hba_lock);
188 }
189 }
190 }
191 spin_unlock_bh(&hba->hba_lock);
192 mutex_unlock(&hba->hba_mutex);
193}
194
195static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
196 struct fc_frame *fp)
197{
198 struct fc_rport_priv *rdata = tgt->rdata;
199 struct fc_frame_header *fh;
200 int rc = 0;
201
202 fh = fc_frame_header_get(fp);
203 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
204 "r_ctl = 0x%x\n", rdata->ids.port_id,
205 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
206 if ((fh->fh_type == FC_TYPE_ELS) &&
207 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
208
209 switch (fc_frame_payload_op(fp)) {
210 case ELS_ADISC:
211 rc = bnx2fc_send_adisc(tgt, fp);
212 break;
213 case ELS_LOGO:
214 rc = bnx2fc_send_logo(tgt, fp);
215 break;
216 case ELS_RLS:
217 rc = bnx2fc_send_rls(tgt, fp);
218 break;
219 default:
220 break;
221 }
222 } else if ((fh->fh_type == FC_TYPE_BLS) &&
223 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
224 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
225 else {
226 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
227 "rctl 0x%x thru non-offload path\n",
228 fh->fh_type, fh->fh_r_ctl);
229 return -ENODEV;
230 }
231 if (rc)
232 return -ENOMEM;
233 else
234 return 0;
235}
236
237/**
238 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
239 *
240 * @lport: the associated local port
241 * @fp: the fc_frame to be transmitted
242 */
243static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
244{
245 struct ethhdr *eh;
246 struct fcoe_crc_eof *cp;
247 struct sk_buff *skb;
248 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700249 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700250 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700251 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800252 struct fcoe_port *port;
253 struct fcoe_hdr *hp;
254 struct bnx2fc_rport *tgt;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700255 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800256 u8 sof, eof;
257 u32 crc;
258 unsigned int hlen, tlen, elen;
259 int wlen, rc = 0;
260
261 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700262 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700263 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700264 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800265
266 fh = fc_frame_header_get(fp);
267
268 skb = fp_skb(fp);
269 if (!lport->link_up) {
270 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
271 kfree_skb(skb);
272 return 0;
273 }
274
275 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Robert Lovefd8f8902012-05-22 19:06:16 -0700276 if (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800277 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
278 kfree_skb(skb);
279 return -EINVAL;
280 }
Robert Lovefd8f8902012-05-22 19:06:16 -0700281 if (fcoe_ctlr_els_send(ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800282 return 0;
283 }
284
285 sof = fr_sof(fp);
286 eof = fr_eof(fp);
287
288 /*
289 * Snoop the frame header to check if the frame is for
290 * an offloaded session
291 */
292 /*
293 * tgt_ofld_list access is synchronized using
294 * both hba mutex and hba lock. Atleast hba mutex or
295 * hba lock needs to be held for read access.
296 */
297
298 spin_lock_bh(&hba->hba_lock);
299 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
300 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
301 /* This frame is for offloaded session */
302 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
303 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
304 spin_unlock_bh(&hba->hba_lock);
305 rc = bnx2fc_xmit_l2_frame(tgt, fp);
306 if (rc != -ENODEV) {
307 kfree_skb(skb);
308 return rc;
309 }
310 } else {
311 spin_unlock_bh(&hba->hba_lock);
312 }
313
314 elen = sizeof(struct ethhdr);
315 hlen = sizeof(struct fcoe_hdr);
316 tlen = sizeof(struct fcoe_crc_eof);
317 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
318
319 skb->ip_summed = CHECKSUM_NONE;
320 crc = fcoe_fc_crc(fp);
321
322 /* copy port crc and eof to the skb buff */
323 if (skb_is_nonlinear(skb)) {
324 skb_frag_t *frag;
325 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
326 kfree_skb(skb);
327 return -ENOMEM;
328 }
329 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800330 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800331 } else {
332 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
333 }
334
335 memset(cp, 0, sizeof(*cp));
336 cp->fcoe_eof = eof;
337 cp->fcoe_crc32 = cpu_to_le32(~crc);
338 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800339 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800340 cp = NULL;
341 }
342
343 /* adjust skb network/transport offsets to match mac/fcoe/port */
344 skb_push(skb, elen + hlen);
345 skb_reset_mac_header(skb);
346 skb_reset_network_header(skb);
347 skb->mac_len = elen;
348 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700349 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800350
351 /* fill up mac and fcoe headers */
352 eh = eth_hdr(skb);
353 eh->h_proto = htons(ETH_P_FCOE);
Robert Lovefd8f8902012-05-22 19:06:16 -0700354 if (ctlr->map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800355 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
356 else
357 /* insert GW address */
Robert Lovefd8f8902012-05-22 19:06:16 -0700358 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800359
Robert Lovefd8f8902012-05-22 19:06:16 -0700360 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
361 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800362 else
363 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
364
365 hp = (struct fcoe_hdr *)(eh + 1);
366 memset(hp, 0, sizeof(*hp));
367 if (FC_FCOE_VER)
368 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
369 hp->fcoe_sof = sof;
370
371 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
372 if (lport->seq_offload && fr_max_payload(fp)) {
373 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
374 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
375 } else {
376 skb_shinfo(skb)->gso_type = 0;
377 skb_shinfo(skb)->gso_size = 0;
378 }
379
380 /*update tx stats */
Vasu Dev1bd49b42012-05-25 10:26:43 -0700381 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800382 stats->TxFrames++;
383 stats->TxWords += wlen;
384 put_cpu();
385
386 /* send down to lld */
387 fr_dev(fp) = lport;
388 if (port->fcoe_pending_queue.qlen)
389 fcoe_check_wait_queue(lport, skb);
390 else if (fcoe_start_io(skb))
391 fcoe_check_wait_queue(lport, skb);
392
393 return 0;
394}
395
396/**
397 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
398 *
399 * @skb: the receive socket buffer
400 * @dev: associated net device
401 * @ptype: context
402 * @olddev: last device
403 *
404 * This function receives the packet and builds FC frame and passes it up
405 */
406static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
407 struct packet_type *ptype, struct net_device *olddev)
408{
409 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700410 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700411 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800412 struct fc_frame_header *fh;
413 struct fcoe_rcv_info *fr;
414 struct fcoe_percpu_s *bg;
415 unsigned short oxid;
416
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700417 interface = container_of(ptype, struct bnx2fc_interface,
418 fcoe_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -0700419 ctlr = bnx2fc_to_ctlr(interface);
420 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800421
422 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700423 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800424 goto err;
425 }
426
427 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700428 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800429 goto err;
430 }
431
432 /*
433 * Check for minimum frame length, and make sure required FCoE
434 * and FC headers are pulled into the linear data area.
435 */
436 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
437 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
438 goto err;
439
440 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
441 fh = (struct fc_frame_header *) skb_transport_header(skb);
442
443 oxid = ntohs(fh->fh_ox_id);
444
445 fr = fcoe_dev_from_skb(skb);
446 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800447
448 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800449 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800450
451 __skb_queue_tail(&bg->fcoe_rx_list, skb);
452 if (bg->fcoe_rx_list.qlen == 1)
453 wake_up_process(bg->thread);
454
Neil Hormanfc05ab72012-03-09 14:50:13 -0800455 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800456
457 return 0;
458err:
459 kfree_skb(skb);
460 return -1;
461}
462
463static int bnx2fc_l2_rcv_thread(void *arg)
464{
465 struct fcoe_percpu_s *bg = arg;
466 struct sk_buff *skb;
467
Dongsheng Yang8698a742014-03-11 18:09:12 +0800468 set_user_nice(current, MIN_NICE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800469 set_current_state(TASK_INTERRUPTIBLE);
470 while (!kthread_should_stop()) {
471 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800472 spin_lock_bh(&bg->fcoe_rx_list.lock);
473 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
474 spin_unlock_bh(&bg->fcoe_rx_list.lock);
475 bnx2fc_recv_frame(skb);
476 spin_lock_bh(&bg->fcoe_rx_list.lock);
477 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700478 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800479 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800480 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700481 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800482 return 0;
483}
484
485
486static void bnx2fc_recv_frame(struct sk_buff *skb)
487{
488 u32 fr_len;
489 struct fc_lport *lport;
490 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700491 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800492 struct fc_frame_header *fh;
493 struct fcoe_crc_eof crc_eof;
494 struct fc_frame *fp;
495 struct fc_lport *vn_port;
496 struct fcoe_port *port;
497 u8 *mac = NULL;
498 u8 *dest_mac = NULL;
499 struct fcoe_hdr *hp;
500
501 fr = fcoe_dev_from_skb(skb);
502 lport = fr->fr_dev;
503 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700504 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800505 kfree_skb(skb);
506 return;
507 }
508
509 if (skb_is_nonlinear(skb))
510 skb_linearize(skb);
511 mac = eth_hdr(skb)->h_source;
512 dest_mac = eth_hdr(skb)->h_dest;
513
514 /* Pull the header */
515 hp = (struct fcoe_hdr *) skb_network_header(skb);
516 fh = (struct fc_frame_header *) skb_transport_header(skb);
517 skb_pull(skb, sizeof(struct fcoe_hdr));
518 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
519
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800520 fp = (struct fc_frame *)skb;
521 fc_frame_init(fp);
522 fr_dev(fp) = lport;
523 fr_sof(fp) = hp->fcoe_sof;
524 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800525 kfree_skb(skb);
526 return;
527 }
528 fr_eof(fp) = crc_eof.fcoe_eof;
529 fr_crc(fp) = crc_eof.fcoe_crc32;
530 if (pskb_trim(skb, fr_len)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800531 kfree_skb(skb);
532 return;
533 }
534
535 fh = fc_frame_header_get(fp);
536
537 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
538 if (vn_port) {
539 port = lport_priv(vn_port);
Joe Perches6942df72013-09-02 03:32:33 +0000540 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800541 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800542 kfree_skb(skb);
543 return;
544 }
545 }
546 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
547 fh->fh_type == FC_TYPE_FCP) {
548 /* Drop FCP data. We dont this in L2 path */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800549 kfree_skb(skb);
550 return;
551 }
552 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
553 fh->fh_type == FC_TYPE_ELS) {
554 switch (fc_frame_payload_op(fp)) {
555 case ELS_LOGO:
556 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
557 /* drop non-FIP LOGO */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800558 kfree_skb(skb);
559 return;
560 }
561 break;
562 }
563 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700564
565 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
566 /* Drop incoming ABTS */
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700567 kfree_skb(skb);
568 return;
569 }
570
Neil Hormand576a5e2014-06-23 10:41:02 -0400571 stats = per_cpu_ptr(lport->stats, smp_processor_id());
572 stats->RxFrames++;
573 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
574
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800575 if (le32_to_cpu(fr_crc(fp)) !=
576 ~crc32(~0, skb->data, fr_len)) {
577 if (stats->InvalidCRCCount < 5)
578 printk(KERN_WARNING PFX "dropping frame with "
579 "CRC error\n");
580 stats->InvalidCRCCount++;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800581 kfree_skb(skb);
582 return;
583 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800584 fc_exch_recv(lport, fp);
585}
586
587/**
588 * bnx2fc_percpu_io_thread - thread per cpu for ios
589 *
590 * @arg: ptr to bnx2fc_percpu_info structure
591 */
592int bnx2fc_percpu_io_thread(void *arg)
593{
594 struct bnx2fc_percpu_s *p = arg;
595 struct bnx2fc_work *work, *tmp;
596 LIST_HEAD(work_list);
597
Dongsheng Yang8698a742014-03-11 18:09:12 +0800598 set_user_nice(current, MIN_NICE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800599 set_current_state(TASK_INTERRUPTIBLE);
600 while (!kthread_should_stop()) {
601 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800602 spin_lock_bh(&p->fp_work_lock);
603 while (!list_empty(&p->work_list)) {
604 list_splice_init(&p->work_list, &work_list);
605 spin_unlock_bh(&p->fp_work_lock);
606
607 list_for_each_entry_safe(work, tmp, &work_list, list) {
608 list_del_init(&work->list);
609 bnx2fc_process_cq_compl(work->tgt, work->wqe);
610 kfree(work);
611 }
612
613 spin_lock_bh(&p->fp_work_lock);
614 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700615 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800616 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800617 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700618 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800619
620 return 0;
621}
622
623static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
624{
625 struct fc_host_statistics *bnx2fc_stats;
626 struct fc_lport *lport = shost_priv(shost);
627 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700628 struct bnx2fc_interface *interface = port->priv;
629 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800630 struct fcoe_statistics_params *fw_stats;
631 int rc = 0;
632
633 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
634 if (!fw_stats)
635 return NULL;
636
637 bnx2fc_stats = fc_get_host_stats(shost);
638
639 init_completion(&hba->stat_req_done);
640 if (bnx2fc_send_stat_req(hba))
641 return bnx2fc_stats;
642 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
643 if (!rc) {
644 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
645 return bnx2fc_stats;
646 }
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800647 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
648 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
649 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
650 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
651 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
652 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
653 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
654 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
655 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
656 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800657
658 bnx2fc_stats->dumped_frames = 0;
659 bnx2fc_stats->lip_count = 0;
660 bnx2fc_stats->nos_count = 0;
661 bnx2fc_stats->loss_of_sync_count = 0;
662 bnx2fc_stats->loss_of_signal_count = 0;
663 bnx2fc_stats->prim_seq_protocol_err_count = 0;
664
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800665 memcpy(&hba->prev_stats, hba->stats_buffer,
666 sizeof(struct fcoe_statistics_params));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800667 return bnx2fc_stats;
668}
669
670static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
671{
672 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700673 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800674 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800675 struct Scsi_Host *shost = lport->host;
676 int rc = 0;
677
678 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
679 shost->max_lun = BNX2FC_MAX_LUN;
680 shost->max_id = BNX2FC_MAX_FCP_TGT;
681 shost->max_channel = 0;
682 if (lport->vport)
683 shost->transportt = bnx2fc_vport_xport_template;
684 else
685 shost->transportt = bnx2fc_transport_template;
686
687 /* Add the new host to SCSI-ml */
688 rc = scsi_add_host(lport->host, dev);
689 if (rc) {
690 printk(KERN_ERR PFX "Error on scsi_add_host\n");
691 return rc;
692 }
693 if (!lport->vport)
694 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800695 snprintf(fc_host_symbolic_name(lport->host), 256,
Saurav Kashyap17d87c42014-07-03 08:18:28 -0400696 "%s (QLogic %s) v%s over %s",
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800697 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700698 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800699
700 return 0;
701}
702
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800703static int bnx2fc_link_ok(struct fc_lport *lport)
704{
705 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700706 struct bnx2fc_interface *interface = port->priv;
707 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800708 struct net_device *dev = hba->phys_dev;
709 int rc = 0;
710
711 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
712 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
713 else {
714 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
715 rc = -1;
716 }
717 return rc;
718}
719
720/**
721 * bnx2fc_get_link_state - get network link state
722 *
723 * @hba: adapter instance pointer
724 *
725 * updates adapter structure flag based on netdev state
726 */
727void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
728{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700729 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800730 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
731 else
732 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
733}
734
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700735static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800736{
737 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700738 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700739 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800740 struct fcoe_port *port;
741 u64 wwnn, wwpn;
742
743 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700744 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700745 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700746 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800747
748 /* require support for get_pauseparam ethtool op. */
749 if (!hba->phys_dev->ethtool_ops ||
750 !hba->phys_dev->ethtool_ops->get_pauseparam)
751 return -EOPNOTSUPP;
752
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700753 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800754 return -EINVAL;
755
756 skb_queue_head_init(&port->fcoe_pending_queue);
757 port->fcoe_pending_queue_active = 0;
758 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
759
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000760 fcoe_link_speed_update(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800761
762 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700763 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700764 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700765 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800766 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
767 fc_set_wwnn(lport, wwnn);
768
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700769 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700770 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700771 2, 0);
772
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800773 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
774 fc_set_wwpn(lport, wwpn);
775 }
776
777 return 0;
778}
779
780static void bnx2fc_destroy_timer(unsigned long data)
781{
782 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
783
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700784 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
785 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700786 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800787 wake_up_interruptible(&hba->destroy_wait);
788}
789
790/**
791 * bnx2fc_indicate_netevent - Generic netdev event handler
792 *
793 * @context: adapter structure pointer
794 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000795 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800796 *
797 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700798 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800799 */
Michael Chan415199f2011-07-20 14:55:24 +0000800static void bnx2fc_indicate_netevent(void *context, unsigned long event,
801 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800802{
803 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Robert Love6e89ea32012-11-27 06:53:40 +0000804 struct fcoe_ctlr_device *cdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700805 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800806 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700807 struct bnx2fc_interface *interface, *tmp;
Robert Lovefd8f8902012-05-22 19:06:16 -0700808 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700809 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800810 u32 link_possible = 1;
811
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700812 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000813 return;
814
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800815 switch (event) {
816 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800817 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
818 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700819 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800820 break;
821
822 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800823 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
824 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
825 link_possible = 0;
826 break;
827
828 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800829 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
830 link_possible = 0;
831 break;
832
833 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800834 break;
835
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700836 case NETDEV_UNREGISTER:
837 if (!vlan_id)
838 return;
839 mutex_lock(&bnx2fc_dev_lock);
840 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700841 if (interface->hba == hba &&
842 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
843 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700844 }
845 mutex_unlock(&bnx2fc_dev_lock);
Eddie Wai06c4f202013-12-11 15:30:21 -0800846
847 /* Ensure ALL destroy work has been completed before return */
848 flush_workqueue(bnx2fc_wq);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700849 return;
850
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800851 default:
Masanari Iida59e13d42012-04-25 00:24:16 +0900852 printk(KERN_ERR PFX "Unknown netevent %ld", event);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800853 return;
854 }
855
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700856 mutex_lock(&bnx2fc_dev_lock);
857 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800858
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700859 if (interface->hba != hba)
860 continue;
861
Robert Lovefd8f8902012-05-22 19:06:16 -0700862 ctlr = bnx2fc_to_ctlr(interface);
863 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700864 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
865 interface->netdev->name, event);
866
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000867 fcoe_link_speed_update(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700868
Robert Love6e89ea32012-11-27 06:53:40 +0000869 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700870
871 if (link_possible && !bnx2fc_link_ok(lport)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000872 switch (cdev->enabled) {
873 case FCOE_CTLR_DISABLED:
874 pr_info("Link up while interface is disabled.\n");
875 break;
876 case FCOE_CTLR_ENABLED:
877 case FCOE_CTLR_UNUSED:
878 /* Reset max recv frame size to default */
879 fc_set_mfs(lport, BNX2FC_MFS);
880 /*
881 * ctlr link up will only be handled during
882 * enable to avoid sending discovery
883 * solicitation on a stale vlan
884 */
885 if (interface->enabled)
886 fcoe_ctlr_link_up(ctlr);
887 };
Robert Lovefd8f8902012-05-22 19:06:16 -0700888 } else if (fcoe_ctlr_link_down(ctlr)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000889 switch (cdev->enabled) {
890 case FCOE_CTLR_DISABLED:
891 pr_info("Link down while interface is disabled.\n");
892 break;
893 case FCOE_CTLR_ENABLED:
894 case FCOE_CTLR_UNUSED:
895 mutex_lock(&lport->lp_mutex);
896 list_for_each_entry(vport, &lport->vports, list)
897 fc_host_port_type(vport->host) =
898 FC_PORTTYPE_UNKNOWN;
899 mutex_unlock(&lport->lp_mutex);
900 fc_host_port_type(lport->host) =
901 FC_PORTTYPE_UNKNOWN;
902 per_cpu_ptr(lport->stats,
903 get_cpu())->LinkFailureCount++;
904 put_cpu();
905 fcoe_clean_pending_queue(lport);
906 wait_for_upload = 1;
907 };
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800908 }
909 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700910 mutex_unlock(&bnx2fc_dev_lock);
911
912 if (wait_for_upload) {
913 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
914 init_waitqueue_head(&hba->shutdown_wait);
915 BNX2FC_MISC_DBG("indicate_netevent "
916 "num_ofld_sess = %d\n",
917 hba->num_ofld_sess);
918 hba->wait_for_link_down = 1;
919 wait_event_interruptible(hba->shutdown_wait,
920 (hba->num_ofld_sess == 0));
921 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
922 hba->num_ofld_sess);
923 hba->wait_for_link_down = 0;
924
925 if (signal_pending(current))
926 flush_signals(current);
927 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800928}
929
930static int bnx2fc_libfc_config(struct fc_lport *lport)
931{
932
933 /* Set the function pointers set by bnx2fc driver */
934 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
935 sizeof(struct libfc_function_template));
936 fc_elsct_init(lport);
937 fc_exch_init(lport);
938 fc_rport_init(lport);
Robert Love08076192013-03-25 11:00:28 -0700939 fc_disc_init(lport);
940 fc_disc_config(lport, lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800941 return 0;
942}
943
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000944static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800945{
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000946 int fcoe_min_xid, fcoe_max_xid;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800947
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000948 fcoe_min_xid = hba->max_xid + 1;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800949 if (nr_cpu_ids <= 2)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000950 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800951 else
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000952 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
953 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
954 fcoe_max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800955 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
956 return -ENOMEM;
957 }
958
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800959 return 0;
960}
961
962static int bnx2fc_lport_config(struct fc_lport *lport)
963{
964 lport->link_up = 0;
965 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800966 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
967 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800968 lport->e_d_tov = 2 * 1000;
969 lport->r_a_tov = 10 * 1000;
970
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800971 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
972 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800973 lport->does_npiv = 1;
974
975 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
976 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
977
978 /* alloc stats structure */
979 if (fc_lport_init_stats(lport))
980 return -ENOMEM;
981
982 /* Finish fc_lport configuration */
983 fc_lport_config(lport);
984
985 return 0;
986}
987
988/**
989 * bnx2fc_fip_recv - handle a received FIP frame.
990 *
991 * @skb: the received skb
992 * @dev: associated &net_device
993 * @ptype: the &packet_type structure which was used to register this handler.
994 * @orig_dev: original receive &net_device, in case @ dev is a bond.
995 *
996 * Returns: 0 for success
997 */
998static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
999 struct packet_type *ptype,
1000 struct net_device *orig_dev)
1001{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001002 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001003 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001004 interface = container_of(ptype, struct bnx2fc_interface,
1005 fip_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -07001006 ctlr = bnx2fc_to_ctlr(interface);
1007 fcoe_ctlr_recv(ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001008 return 0;
1009}
1010
1011/**
1012 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1013 *
1014 * @fip: FCoE controller.
1015 * @old: Unicast MAC address to delete if the MAC is non-zero.
1016 * @new: Unicast MAC address to add.
1017 *
1018 * Remove any previously-set unicast MAC filter.
1019 * Add secondary FCoE MAC address filter for our OUI.
1020 */
1021static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1022{
1023 struct fcoe_port *port = lport_priv(lport);
1024
1025 memcpy(port->data_src_addr, addr, ETH_ALEN);
1026}
1027
1028/**
1029 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1030 *
1031 * @lport: libfc port
1032 */
1033static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1034{
1035 struct fcoe_port *port;
1036
1037 port = (struct fcoe_port *)lport_priv(lport);
1038 return port->data_src_addr;
1039}
1040
1041/**
1042 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1043 *
1044 * @fip: FCoE controller.
1045 * @skb: FIP Packet.
1046 */
1047static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1048{
1049 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1050 dev_queue_xmit(skb);
1051}
1052
1053static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1054{
1055 struct Scsi_Host *shost = vport_to_shost(vport);
1056 struct fc_lport *n_port = shost_priv(shost);
1057 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001058 struct bnx2fc_interface *interface = port->priv;
1059 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001060 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001061 int rc;
1062 char buf[32];
1063
1064 rc = fcoe_validate_vport_create(vport);
1065 if (rc) {
1066 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1067 printk(KERN_ERR PFX "Failed to create vport, "
1068 "WWPN (0x%s) already exists\n",
1069 buf);
1070 return rc;
1071 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001072
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001073 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001074 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001075 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001076 return -EIO;
1077 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001078 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001079 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001080 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001081 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001082 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001083
1084 if (IS_ERR(vn_port)) {
1085 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1086 netdev->name);
1087 return -EIO;
1088 }
1089
1090 if (disabled) {
1091 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1092 } else {
1093 vn_port->boot_time = jiffies;
1094 fc_lport_init(vn_port);
1095 fc_fabric_login(vn_port);
1096 fc_vport_setlink(vn_port);
1097 }
1098 return 0;
1099}
1100
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001101static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1102{
1103 struct bnx2fc_lport *blport, *tmp;
1104
1105 spin_lock_bh(&hba->hba_lock);
1106 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1107 if (blport->lport == lport) {
1108 list_del(&blport->list);
1109 kfree(blport);
1110 }
1111 }
1112 spin_unlock_bh(&hba->hba_lock);
1113}
1114
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001115static int bnx2fc_vport_destroy(struct fc_vport *vport)
1116{
1117 struct Scsi_Host *shost = vport_to_shost(vport);
1118 struct fc_lport *n_port = shost_priv(shost);
1119 struct fc_lport *vn_port = vport->dd_data;
1120 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001121 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001122 struct fc_lport *v_port;
1123 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001124
1125 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001126 list_for_each_entry(v_port, &n_port->vports, list)
1127 if (v_port->vport == vport) {
1128 found = true;
1129 break;
1130 }
1131
1132 if (!found) {
1133 mutex_unlock(&n_port->lp_mutex);
1134 return -ENOENT;
1135 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001136 list_del(&vn_port->list);
1137 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001138 bnx2fc_free_vport(interface->hba, port->lport);
1139 bnx2fc_port_shutdown(port->lport);
1140 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001141 queue_work(bnx2fc_wq, &port->destroy_work);
1142 return 0;
1143}
1144
1145static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1146{
1147 struct fc_lport *lport = vport->dd_data;
1148
1149 if (disable) {
1150 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1151 fc_fabric_logoff(lport);
1152 } else {
1153 lport->boot_time = jiffies;
1154 fc_fabric_login(lport);
1155 fc_vport_setlink(lport);
1156 }
1157 return 0;
1158}
1159
1160
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001161static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001162{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001163 struct net_device *netdev = interface->netdev;
1164 struct net_device *physdev = interface->hba->phys_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001165 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001166 struct netdev_hw_addr *ha;
1167 int sel_san_mac = 0;
1168
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001169 /* setup Source MAC Address */
1170 rcu_read_lock();
1171 for_each_dev_addr(physdev, ha) {
1172 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1173 ha->type);
1174 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1175 ha->addr[1], ha->addr[2], ha->addr[3],
1176 ha->addr[4], ha->addr[5]);
1177
1178 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1179 (is_valid_ether_addr(ha->addr))) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001180 memcpy(ctlr->ctl_src_addr, ha->addr,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001181 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001182 sel_san_mac = 1;
1183 BNX2FC_MISC_DBG("Found SAN MAC\n");
1184 }
1185 }
1186 rcu_read_unlock();
1187
1188 if (!sel_san_mac)
1189 return -ENODEV;
1190
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001191 interface->fip_packet_type.func = bnx2fc_fip_recv;
1192 interface->fip_packet_type.type = htons(ETH_P_FIP);
1193 interface->fip_packet_type.dev = netdev;
1194 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001195
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001196 interface->fcoe_packet_type.func = bnx2fc_rcv;
1197 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1198 interface->fcoe_packet_type.dev = netdev;
1199 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001200
1201 return 0;
1202}
1203
1204static int bnx2fc_attach_transport(void)
1205{
1206 bnx2fc_transport_template =
1207 fc_attach_transport(&bnx2fc_transport_function);
1208
1209 if (bnx2fc_transport_template == NULL) {
1210 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1211 return -ENODEV;
1212 }
1213
1214 bnx2fc_vport_xport_template =
1215 fc_attach_transport(&bnx2fc_vport_xport_function);
1216 if (bnx2fc_vport_xport_template == NULL) {
1217 printk(KERN_ERR PFX
1218 "Failed to attach FC transport for vport\n");
1219 fc_release_transport(bnx2fc_transport_template);
1220 bnx2fc_transport_template = NULL;
1221 return -ENODEV;
1222 }
1223 return 0;
1224}
1225static void bnx2fc_release_transport(void)
1226{
1227 fc_release_transport(bnx2fc_transport_template);
1228 fc_release_transport(bnx2fc_vport_xport_template);
1229 bnx2fc_transport_template = NULL;
1230 bnx2fc_vport_xport_template = NULL;
1231}
1232
1233static void bnx2fc_interface_release(struct kref *kref)
1234{
Robert Love8d55e502012-05-22 19:06:26 -07001235 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001236 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001237 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001238 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001239
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001240 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001241 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001242
Robert Lovefd8f8902012-05-22 19:06:16 -07001243 ctlr = bnx2fc_to_ctlr(interface);
Robert Love8d55e502012-05-22 19:06:26 -07001244 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001245 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001246
1247 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001248 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
Robert Lovefd8f8902012-05-22 19:06:16 -07001249 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001250
Robert Love8d55e502012-05-22 19:06:26 -07001251 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001252
1253 dev_put(netdev);
1254 module_put(THIS_MODULE);
1255}
1256
1257static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1258{
1259 kref_get(&interface->kref);
1260}
1261
1262static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1263{
1264 kref_put(&interface->kref, bnx2fc_interface_release);
1265}
1266static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1267{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001268 /* Free the command manager */
1269 if (hba->cmd_mgr) {
1270 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1271 hba->cmd_mgr = NULL;
1272 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001273 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001274 bnx2fc_unbind_pcidev(hba);
1275 kfree(hba);
1276}
1277
1278/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001279 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001280 *
1281 * @cnic: pointer to cnic device
1282 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001283 * Creates a new FCoE hba on the given device.
1284 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001285 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001286static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001287{
1288 struct bnx2fc_hba *hba;
Barak Witkowski2e499d32012-06-26 01:31:19 +00001289 struct fcoe_capabilities *fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001290 int rc;
1291
1292 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1293 if (!hba) {
1294 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1295 return NULL;
1296 }
1297 spin_lock_init(&hba->hba_lock);
1298 mutex_init(&hba->hba_mutex);
1299
1300 hba->cnic = cnic;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001301
1302 hba->max_tasks = cnic->max_fcoe_exchanges;
1303 hba->elstm_xids = (hba->max_tasks / 2);
1304 hba->max_outstanding_cmds = hba->elstm_xids;
1305 hba->max_xid = (hba->max_tasks - 1);
1306
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001307 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001308 if (rc) {
1309 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001310 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001311 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001312 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001313 hba->next_conn_id = 0;
1314
1315 hba->tgt_ofld_list =
1316 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1317 GFP_KERNEL);
1318 if (!hba->tgt_ofld_list) {
1319 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1320 goto tgtofld_err;
1321 }
1322
1323 hba->num_ofld_sess = 0;
1324
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001325 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001326 if (!hba->cmd_mgr) {
1327 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1328 goto cmgr_err;
1329 }
Barak Witkowski2e499d32012-06-26 01:31:19 +00001330 fcoe_cap = &hba->fcoe_cap;
1331
1332 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1333 FCOE_IOS_PER_CONNECTION_SHIFT;
1334 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1335 FCOE_LOGINS_PER_PORT_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001336 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001337 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1338 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1339 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1340 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1341 FCOE_TARGETS_SUPPORTED_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001342 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001343 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1344 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001345
1346 init_waitqueue_head(&hba->shutdown_wait);
1347 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001348 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001349
1350 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001351
1352cmgr_err:
1353 kfree(hba->tgt_ofld_list);
1354tgtofld_err:
1355 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001356bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001357 kfree(hba);
1358 return NULL;
1359}
1360
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001361struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1362 struct net_device *netdev,
1363 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001364{
Robert Love8d55e502012-05-22 19:06:26 -07001365 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001366 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001367 struct fcoe_ctlr *ctlr;
1368 int size;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001369 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001370
Robert Lovefd8f8902012-05-22 19:06:16 -07001371 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
Robert Love8d55e502012-05-22 19:06:26 -07001372 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1373 size);
1374 if (!ctlr_dev) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001375 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1376 return NULL;
1377 }
Robert Love8d55e502012-05-22 19:06:26 -07001378 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love9d348762013-09-05 07:47:27 +00001379 ctlr->cdev = ctlr_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001380 interface = fcoe_ctlr_priv(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001381 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001382 kref_init(&interface->kref);
1383 interface->hba = hba;
1384 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001385
1386 /* Initialize FIP */
Robert Lovefd8f8902012-05-22 19:06:16 -07001387 fcoe_ctlr_init(ctlr, fip_mode);
1388 ctlr->send = bnx2fc_fip_send;
1389 ctlr->update_mac = bnx2fc_update_src_mac;
1390 ctlr->get_src_addr = bnx2fc_get_src_mac;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001391 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001392
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001393 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001394 if (!rc)
1395 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001396
Robert Lovefd8f8902012-05-22 19:06:16 -07001397 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001398 dev_put(netdev);
Robert Love8d55e502012-05-22 19:06:26 -07001399 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001400 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001401}
1402
1403/**
1404 * bnx2fc_if_create - Create FCoE instance on a given interface
1405 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001406 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001407 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1408 * @npiv: Indicates if the port is vport or not
1409 *
1410 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1411 *
1412 * Returns: Allocated fc_lport or an error pointer
1413 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001414static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001415 struct device *parent, int npiv)
1416{
Robert Lovefd8f8902012-05-22 19:06:16 -07001417 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Vasu Dev134a4e22011-04-28 15:55:44 -07001418 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001419 struct fcoe_port *port;
1420 struct Scsi_Host *shost;
1421 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001422 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001423 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001424 int rc = 0;
1425
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001426 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1427 if (!blport) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001428 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001429 return NULL;
1430 }
1431
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001432 /* Allocate Scsi_Host structure */
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001433 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
Vasu Dev134a4e22011-04-28 15:55:44 -07001434 if (!npiv)
1435 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1436 else
1437 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001438
1439 if (!lport) {
1440 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001441 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001442 }
1443 shost = lport->host;
1444 port = lport_priv(lport);
1445 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001446 port->priv = interface;
Yi Zouc3d79092012-12-06 06:24:29 +00001447 port->get_netdev = bnx2fc_netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001448 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1449
1450 /* Configure fcoe_port */
1451 rc = bnx2fc_lport_config(lport);
1452 if (rc)
1453 goto lp_config_err;
1454
1455 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001456 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1457 vport->node_name, vport->port_name);
1458 fc_set_wwnn(lport, vport->node_name);
1459 fc_set_wwpn(lport, vport->port_name);
1460 }
1461 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001462 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001463 if (rc) {
1464 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1465 goto lp_config_err;
1466 }
1467
1468 rc = bnx2fc_shost_config(lport, parent);
1469 if (rc) {
1470 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001471 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001472 goto lp_config_err;
1473 }
1474
1475 /* Initialize the libfc library */
1476 rc = bnx2fc_libfc_config(lport);
1477 if (rc) {
1478 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1479 goto shost_err;
1480 }
1481 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1482
1483 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001484 if (!npiv)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001485 rc = bnx2fc_em_config(lport, hba);
Vasu Dev134a4e22011-04-28 15:55:44 -07001486 else {
1487 shost = vport_to_shost(vport);
1488 n_port = shost_priv(shost);
1489 rc = fc_exch_mgr_list_clone(n_port, lport);
1490 }
1491
1492 if (rc) {
1493 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1494 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001495 }
1496
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001497 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001498
1499 spin_lock_bh(&hba->hba_lock);
1500 blport->lport = lport;
1501 list_add_tail(&blport->list, &hba->vports);
1502 spin_unlock_bh(&hba->hba_lock);
1503
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001504 return lport;
1505
1506shost_err:
1507 scsi_remove_host(shost);
1508lp_config_err:
1509 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001510free_blport:
1511 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001512 return NULL;
1513}
1514
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001515static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001516{
1517 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001518 __dev_remove_pack(&interface->fcoe_packet_type);
1519 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001520 synchronize_net();
1521}
1522
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001523static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001524{
Robert Lovefd8f8902012-05-22 19:06:16 -07001525 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1526 struct fc_lport *lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001527 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001528 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001529
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001530 /* Stop the transmit retry timer */
1531 del_timer_sync(&port->timer);
1532
1533 /* Free existing transmit skbs */
1534 fcoe_clean_pending_queue(lport);
1535
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001536 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001537
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001538 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001539}
1540
1541static void bnx2fc_if_destroy(struct fc_lport *lport)
1542{
1543
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001544 /* Free queued packets for the receive thread */
1545 bnx2fc_clean_rx_queue(lport);
1546
1547 /* Detach from scsi-ml */
1548 fc_remove_host(lport->host);
1549 scsi_remove_host(lport->host);
1550
1551 /*
1552 * Note that only the physical lport will have the exchange manager.
1553 * for vports, this function is NOP
1554 */
1555 fc_exch_mgr_free(lport);
1556
1557 /* Free memory used by statistical counters */
1558 fc_lport_free_stats(lport);
1559
1560 /* Release Scsi_Host */
1561 scsi_host_put(lport->host);
1562}
1563
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001564static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001565{
Robert Lovefd8f8902012-05-22 19:06:16 -07001566 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1567 struct fc_lport *lport = ctlr->lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001568 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001569
1570 bnx2fc_interface_cleanup(interface);
1571 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001572 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001573 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001574 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001575}
1576
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001577/**
1578 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1579 *
1580 * @buffer: The name of the Ethernet interface to be destroyed
1581 * @kp: The associated kernel parameter
1582 *
1583 * Called from sysfs.
1584 *
1585 * Returns: 0 for success
1586 */
1587static int bnx2fc_destroy(struct net_device *netdev)
1588{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001589 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001590 struct workqueue_struct *timer_work_queue;
Robert Lovefd8f8902012-05-22 19:06:16 -07001591 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001592 int rc = 0;
1593
Nithin Sujir6702ca12011-03-17 17:13:27 -07001594 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001595 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001596
1597 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001598 ctlr = bnx2fc_to_ctlr(interface);
1599 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001600 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001601 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001602 goto netdev_err;
1603 }
1604
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001605 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001606 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001607 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001608
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001609netdev_err:
1610 mutex_unlock(&bnx2fc_dev_lock);
1611 rtnl_unlock();
1612 return rc;
1613}
1614
1615static void bnx2fc_destroy_work(struct work_struct *work)
1616{
1617 struct fcoe_port *port;
1618 struct fc_lport *lport;
1619
1620 port = container_of(work, struct fcoe_port, destroy_work);
1621 lport = port->lport;
1622
1623 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1624
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001625 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001626}
1627
1628static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1629{
1630 bnx2fc_free_fw_resc(hba);
1631 bnx2fc_free_task_ctx(hba);
1632}
1633
1634/**
1635 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1636 * pci structure
1637 *
1638 * @hba: Adapter instance
1639 */
1640static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1641{
1642 if (bnx2fc_setup_task_ctx(hba))
1643 goto mem_err;
1644
1645 if (bnx2fc_setup_fw_resc(hba))
1646 goto mem_err;
1647
1648 return 0;
1649mem_err:
1650 bnx2fc_unbind_adapter_devices(hba);
1651 return -ENOMEM;
1652}
1653
1654static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1655{
1656 struct cnic_dev *cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001657 struct pci_dev *pdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001658
1659 if (!hba->cnic) {
1660 printk(KERN_ERR PFX "cnic is NULL\n");
1661 return -ENODEV;
1662 }
1663 cnic = hba->cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001664 pdev = hba->pcidev = cnic->pcidev;
1665 if (!hba->pcidev)
1666 return -ENODEV;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001667
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001668 switch (pdev->device) {
1669 case PCI_DEVICE_ID_NX2_57710:
1670 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1671 break;
1672 case PCI_DEVICE_ID_NX2_57711:
1673 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1674 break;
1675 case PCI_DEVICE_ID_NX2_57712:
1676 case PCI_DEVICE_ID_NX2_57712_MF:
1677 case PCI_DEVICE_ID_NX2_57712_VF:
1678 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1679 break;
1680 case PCI_DEVICE_ID_NX2_57800:
1681 case PCI_DEVICE_ID_NX2_57800_MF:
1682 case PCI_DEVICE_ID_NX2_57800_VF:
1683 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1684 break;
1685 case PCI_DEVICE_ID_NX2_57810:
1686 case PCI_DEVICE_ID_NX2_57810_MF:
1687 case PCI_DEVICE_ID_NX2_57810_VF:
1688 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1689 break;
1690 case PCI_DEVICE_ID_NX2_57840:
1691 case PCI_DEVICE_ID_NX2_57840_MF:
1692 case PCI_DEVICE_ID_NX2_57840_VF:
1693 case PCI_DEVICE_ID_NX2_57840_2_20:
1694 case PCI_DEVICE_ID_NX2_57840_4_10:
1695 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1696 break;
1697 default:
1698 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1699 break;
1700 }
1701 pci_dev_get(hba->pcidev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001702 return 0;
1703}
1704
1705static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1706{
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001707 if (hba->pcidev) {
1708 hba->chip_num[0] = '\0';
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001709 pci_dev_put(hba->pcidev);
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001710 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001711 hba->pcidev = NULL;
1712}
1713
Barak Witkowski2e499d32012-06-26 01:31:19 +00001714/**
1715 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1716 *
1717 * @handle: transport handle pointing to adapter struture
1718 */
1719static int bnx2fc_ulp_get_stats(void *handle)
1720{
1721 struct bnx2fc_hba *hba = handle;
1722 struct cnic_dev *cnic;
1723 struct fcoe_stats_info *stats_addr;
1724
1725 if (!hba)
1726 return -EINVAL;
1727
1728 cnic = hba->cnic;
1729 stats_addr = &cnic->stats_addr->fcoe_stat;
1730 if (!stats_addr)
1731 return -EINVAL;
1732
1733 strncpy(stats_addr->version, BNX2FC_VERSION,
1734 sizeof(stats_addr->version));
1735 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1736 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1737
1738 return 0;
1739}
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001740
1741
1742/**
1743 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1744 *
Masanari Iida59e13d42012-04-25 00:24:16 +09001745 * @handle: transport handle pointing to adapter structure
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001746 *
1747 * This function maps adapter structure to pcidev structure and initiates
1748 * firmware handshake to enable/initialize on-chip FCoE components.
1749 * This bnx2fc - cnic interface api callback is used after following
1750 * conditions are met -
1751 * a) underlying network interface is up (marked by event NETDEV_UP
1752 * from netdev
1753 * b) bnx2fc adatper structure is registered.
1754 */
1755static void bnx2fc_ulp_start(void *handle)
1756{
1757 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001758 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001759 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001760 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001761
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001762 mutex_lock(&bnx2fc_dev_lock);
1763
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001764 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001765 bnx2fc_fw_init(hba);
1766
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001767 BNX2FC_MISC_DBG("bnx2fc started.\n");
1768
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001769 list_for_each_entry(interface, &if_list, list) {
1770 if (interface->hba == hba) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001771 ctlr = bnx2fc_to_ctlr(interface);
1772 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001773 /* Kick off Fabric discovery*/
1774 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1775 lport->tt.frame_send = bnx2fc_xmit;
1776 bnx2fc_start_disc(interface);
1777 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001778 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001779
1780 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001781}
1782
1783static void bnx2fc_port_shutdown(struct fc_lport *lport)
1784{
1785 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1786 fc_fabric_logoff(lport);
1787 fc_lport_destroy(lport);
1788}
1789
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001790static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001791{
Robert Lovefd8f8902012-05-22 19:06:16 -07001792 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001793 struct fc_lport *lport;
1794 struct fc_lport *vport;
1795
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001796 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1797 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001798
Robert Lovefd8f8902012-05-22 19:06:16 -07001799 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001800 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001801
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001802 mutex_lock(&lport->lp_mutex);
1803 list_for_each_entry(vport, &lport->vports, list)
1804 fc_host_port_type(vport->host) =
1805 FC_PORTTYPE_UNKNOWN;
1806 mutex_unlock(&lport->lp_mutex);
1807 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
Robert Lovefd8f8902012-05-22 19:06:16 -07001808 fcoe_ctlr_link_down(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001809 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001810}
1811
1812static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1813{
1814#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1815 int rc = -1;
1816 int i = HZ;
1817
1818 rc = bnx2fc_bind_adapter_devices(hba);
1819 if (rc) {
1820 printk(KERN_ALERT PFX
1821 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1822 goto err_out;
1823 }
1824
1825 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1826 if (rc) {
1827 printk(KERN_ALERT PFX
1828 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1829 goto err_unbind;
1830 }
1831
1832 /*
1833 * Wait until the adapter init message is complete, and adapter
1834 * state is UP.
1835 */
1836 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1837 msleep(BNX2FC_INIT_POLL_TIME);
1838
1839 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1840 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1841 "Ignoring...\n",
1842 hba->cnic->netdev->name);
1843 rc = -1;
1844 goto err_unbind;
1845 }
1846
1847
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001848 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001849 return 0;
1850
1851err_unbind:
1852 bnx2fc_unbind_adapter_devices(hba);
1853err_out:
1854 return rc;
1855}
1856
1857static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1858{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001859 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001860 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1861 init_timer(&hba->destroy_timer);
1862 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1863 jiffies;
1864 hba->destroy_timer.function = bnx2fc_destroy_timer;
1865 hba->destroy_timer.data = (unsigned long)hba;
1866 add_timer(&hba->destroy_timer);
1867 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001868 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1869 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001870 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001871 /* This should never happen */
1872 if (signal_pending(current))
1873 flush_signals(current);
1874
1875 del_timer_sync(&hba->destroy_timer);
1876 }
1877 bnx2fc_unbind_adapter_devices(hba);
1878 }
1879}
1880
1881/**
1882 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1883 *
1884 * @handle: transport handle pointing to adapter structure
1885 *
1886 * Driver checks if adapter is already in shutdown mode, if not start
1887 * the shutdown process.
1888 */
1889static void bnx2fc_ulp_stop(void *handle)
1890{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001891 struct bnx2fc_hba *hba = handle;
1892 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001893
1894 printk(KERN_ERR "ULP_STOP\n");
1895
1896 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001897 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1898 goto exit;
1899 list_for_each_entry(interface, &if_list, list) {
1900 if (interface->hba == hba)
1901 bnx2fc_stop(interface);
1902 }
1903 BUG_ON(hba->num_ofld_sess != 0);
1904
1905 mutex_lock(&hba->hba_mutex);
1906 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1907 clear_bit(ADAPTER_STATE_GOING_DOWN,
1908 &hba->adapter_state);
1909
1910 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1911 mutex_unlock(&hba->hba_mutex);
1912
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001913 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001914exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001915 mutex_unlock(&bnx2fc_dev_lock);
1916}
1917
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001918static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001919{
Robert Lovefd8f8902012-05-22 19:06:16 -07001920 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001921 struct fc_lport *lport;
1922 int wait_cnt = 0;
1923
1924 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1925 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001926 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001927 printk(KERN_ERR PFX "Init not done yet\n");
1928 return;
1929 }
1930
Robert Lovefd8f8902012-05-22 19:06:16 -07001931 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001932 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1933
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001934 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001935 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001936 fcoe_ctlr_link_up(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001937 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001938 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001939 }
1940
1941 /* wait for the FCF to be selected before issuing FLOGI */
Robert Lovefd8f8902012-05-22 19:06:16 -07001942 while (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001943 msleep(250);
1944 /* give up after 3 secs */
1945 if (++wait_cnt > 12)
1946 break;
1947 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001948
1949 /* Reset max receive frame size to default */
1950 if (fc_set_mfs(lport, BNX2FC_MFS))
1951 return;
1952
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001953 fc_lport_init(lport);
1954 fc_fabric_login(lport);
1955}
1956
1957
1958/**
1959 * bnx2fc_ulp_init - Initialize an adapter instance
1960 *
1961 * @dev : cnic device handle
1962 * Called from cnic_register_driver() context to initialize all
1963 * enumerated cnic devices. This routine allocates adapter structure
1964 * and other device specific resources.
1965 */
1966static void bnx2fc_ulp_init(struct cnic_dev *dev)
1967{
1968 struct bnx2fc_hba *hba;
1969 int rc = 0;
1970
1971 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1972 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001973 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1974 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001975 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001976 " flags: %lx fcoe_conn: %d\n",
1977 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001978 return;
1979 }
1980
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001981 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001982 if (!hba) {
1983 printk(KERN_ERR PFX "hba initialization failed\n");
1984 return;
1985 }
1986
1987 /* Add HBA to the adapter list */
1988 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001989 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001990 adapter_count++;
1991 mutex_unlock(&bnx2fc_dev_lock);
1992
Barak Witkowski2e499d32012-06-26 01:31:19 +00001993 dev->fcoe_cap = &hba->fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001994 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1995 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1996 (void *) hba);
1997 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001998 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001999 else
2000 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2001}
2002
Eddie Wai06808102013-09-25 22:01:20 -07002003/* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2004static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2005{
2006 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2007
2008 if (interface->enabled == true) {
2009 if (!ctlr->lp) {
2010 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2011 return -ENODEV;
2012 } else {
2013 interface->enabled = false;
2014 fcoe_ctlr_link_down(ctlr);
2015 fcoe_clean_pending_queue(ctlr->lp);
2016 }
2017 }
2018 return 0;
2019}
2020
Robert Love6e89ea32012-11-27 06:53:40 +00002021/**
2022 * Deperecated: Use bnx2fc_enabled()
2023 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002024static int bnx2fc_disable(struct net_device *netdev)
2025{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002026 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002027 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002028 int rc = 0;
2029
Nithin Sujir6702ca12011-03-17 17:13:27 -07002030 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002031 mutex_lock(&bnx2fc_dev_lock);
2032
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002033 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002034 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002035
Eddie Wai06808102013-09-25 22:01:20 -07002036 if (!interface) {
2037 rc = -ENODEV;
2038 pr_err(PFX "bnx2fc_disable: interface not found\n");
2039 } else {
2040 rc = __bnx2fc_disable(ctlr);
2041 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002042 mutex_unlock(&bnx2fc_dev_lock);
2043 rtnl_unlock();
2044 return rc;
2045}
2046
Eddie Wai06808102013-09-25 22:01:20 -07002047static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2048{
2049 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2050
2051 if (interface->enabled == false) {
2052 if (!ctlr->lp) {
2053 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2054 return -ENODEV;
2055 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2056 fcoe_ctlr_link_up(ctlr);
2057 interface->enabled = true;
2058 }
2059 }
2060 return 0;
2061}
2062
Robert Love6e89ea32012-11-27 06:53:40 +00002063/**
2064 * Deprecated: Use bnx2fc_enabled()
2065 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002066static int bnx2fc_enable(struct net_device *netdev)
2067{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002068 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002069 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002070 int rc = 0;
2071
Nithin Sujir6702ca12011-03-17 17:13:27 -07002072 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002073 mutex_lock(&bnx2fc_dev_lock);
2074
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002075 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002076 ctlr = bnx2fc_to_ctlr(interface);
Eddie Wai06808102013-09-25 22:01:20 -07002077 if (!interface) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002078 rc = -ENODEV;
Eddie Wai06808102013-09-25 22:01:20 -07002079 pr_err(PFX "bnx2fc_enable: interface not found\n");
2080 } else {
2081 rc = __bnx2fc_enable(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002082 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002083
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002084 mutex_unlock(&bnx2fc_dev_lock);
2085 rtnl_unlock();
2086 return rc;
2087}
2088
2089/**
Robert Love6e89ea32012-11-27 06:53:40 +00002090 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2091 * @cdev: The FCoE Controller that is being enabled or disabled
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002092 *
Robert Love6e89ea32012-11-27 06:53:40 +00002093 * fcoe_sysfs will ensure that the state of 'enabled' has
2094 * changed, so no checking is necessary here. This routine simply
2095 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2096 * When those routines are removed the functionality can be merged
2097 * here.
2098 */
2099static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2100{
2101 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
Robert Love6e89ea32012-11-27 06:53:40 +00002102
2103 switch (cdev->enabled) {
2104 case FCOE_CTLR_ENABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002105 return __bnx2fc_enable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002106 case FCOE_CTLR_DISABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002107 return __bnx2fc_disable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002108 case FCOE_CTLR_UNUSED:
2109 default:
2110 return -ENOTSUPP;
2111 };
2112}
2113
2114enum bnx2fc_create_link_state {
2115 BNX2FC_CREATE_LINK_DOWN,
2116 BNX2FC_CREATE_LINK_UP,
2117};
2118
2119/**
2120 * _bnx2fc_create() - Create bnx2fc FCoE interface
2121 * @netdev : The net_device object the Ethernet interface to create on
2122 * @fip_mode: The FIP mode for this creation
2123 * @link_state: The ctlr link state on creation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002124 *
Robert Love6e89ea32012-11-27 06:53:40 +00002125 * Called from either the libfcoe 'create' module parameter
2126 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2127 *
2128 * libfcoe's 'create' module parameter is deprecated so some
2129 * consolidation of code can be done when that interface is
2130 * removed.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002131 *
2132 * Returns: 0 for success
2133 */
Robert Love6e89ea32012-11-27 06:53:40 +00002134static int _bnx2fc_create(struct net_device *netdev,
2135 enum fip_state fip_mode,
2136 enum bnx2fc_create_link_state link_state)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002137{
Robert Love6e89ea32012-11-27 06:53:40 +00002138 struct fcoe_ctlr_device *cdev;
Robert Lovefd8f8902012-05-22 19:06:16 -07002139 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002140 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002141 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002142 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002143 struct fc_lport *lport;
2144 struct ethtool_drvinfo drvinfo;
2145 int rc = 0;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002146 int vlan_id = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002147
2148 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2149 if (fip_mode != FIP_MODE_FABRIC) {
2150 printk(KERN_ERR "fip mode not FABRIC\n");
2151 return -EIO;
2152 }
2153
Nithin Sujir6702ca12011-03-17 17:13:27 -07002154 rtnl_lock();
2155
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002156 mutex_lock(&bnx2fc_dev_lock);
2157
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002158 if (!try_module_get(THIS_MODULE)) {
2159 rc = -EINVAL;
2160 goto mod_err;
2161 }
2162
2163 /* obtain physical netdev */
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002164 if (netdev->priv_flags & IFF_802_1Q_VLAN)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002165 phys_dev = vlan_dev_real_dev(netdev);
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002166
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002167 /* verify if the physical device is a netxtreme2 device */
2168 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2169 memset(&drvinfo, 0, sizeof(drvinfo));
2170 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002171 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002172 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2173 rc = -EINVAL;
2174 goto netdev_err;
2175 }
2176 } else {
2177 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2178 rc = -EINVAL;
2179 goto netdev_err;
2180 }
2181
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002182 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002183 hba = bnx2fc_hba_lookup(phys_dev);
2184 if (!hba) {
2185 rc = -ENODEV;
2186 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2187 goto netdev_err;
2188 }
2189
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002190 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002191 rc = -EEXIST;
2192 goto netdev_err;
2193 }
2194
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002195 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2196 if (!interface) {
2197 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002198 goto ifput_err;
2199 }
2200
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002201 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2202 vlan_id = vlan_dev_vlan_id(netdev);
2203 interface->vlan_enabled = 1;
2204 }
2205
Robert Lovefd8f8902012-05-22 19:06:16 -07002206 ctlr = bnx2fc_to_ctlr(interface);
Robert Love01bdcb62013-03-25 11:00:26 -07002207 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002208 interface->vlan_id = vlan_id;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002209
2210 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002211 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002212 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002213 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2214 rc = -EINVAL;
2215 goto ifput_err;
2216 }
2217
Robert Love01bdcb62013-03-25 11:00:26 -07002218 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002219 if (!lport) {
2220 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2221 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002222 rc = -EINVAL;
2223 goto if_create_err;
2224 }
2225
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002226 /* Add interface to if_list */
2227 list_add_tail(&interface->list, &if_list);
2228
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002229 lport->boot_time = jiffies;
2230
2231 /* Make this master N_port */
Robert Lovefd8f8902012-05-22 19:06:16 -07002232 ctlr->lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002233
Robert Love6e89ea32012-11-27 06:53:40 +00002234 if (link_state == BNX2FC_CREATE_LINK_UP)
2235 cdev->enabled = FCOE_CTLR_ENABLED;
2236 else
2237 cdev->enabled = FCOE_CTLR_DISABLED;
2238
2239 if (link_state == BNX2FC_CREATE_LINK_UP &&
2240 !bnx2fc_link_ok(lport)) {
Robert Lovefd8f8902012-05-22 19:06:16 -07002241 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002242 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2243 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2244 }
2245
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002246 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2247 bnx2fc_start_disc(interface);
Robert Love6e89ea32012-11-27 06:53:40 +00002248
2249 if (link_state == BNX2FC_CREATE_LINK_UP)
2250 interface->enabled = true;
2251
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002252 /*
2253 * Release from kref_init in bnx2fc_interface_setup, on success
2254 * lport should be holding a reference taken in bnx2fc_if_create
2255 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002256 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002257 /* put netdev that was held while calling dev_get_by_name */
2258 mutex_unlock(&bnx2fc_dev_lock);
2259 rtnl_unlock();
2260 return 0;
2261
2262if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002263 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002264ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002265 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002266 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002267 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002268netdev_err:
2269 module_put(THIS_MODULE);
2270mod_err:
2271 mutex_unlock(&bnx2fc_dev_lock);
2272 rtnl_unlock();
2273 return rc;
2274}
2275
2276/**
Robert Love6e89ea32012-11-27 06:53:40 +00002277 * bnx2fc_create() - Create a bnx2fc interface
2278 * @netdev : The net_device object the Ethernet interface to create on
2279 * @fip_mode: The FIP mode for this creation
2280 *
2281 * Called from fcoe transport
2282 *
2283 * Returns: 0 for success
2284 */
2285static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2286{
2287 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2288}
2289
2290/**
2291 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2292 * @netdev: The net_device to be used by the allocated FCoE Controller
2293 *
2294 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2295 * in a link_down state. The allows the user an opportunity to configure
2296 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2297 *
2298 * Creating in with this routine starts the FCoE Controller in Fabric
2299 * mode. The user can change to VN2VN or another mode before enabling.
2300 */
2301static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2302{
2303 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2304 BNX2FC_CREATE_LINK_DOWN);
2305}
2306
2307/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002308 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002309 *
2310 * @cnic: Pointer to cnic device instance
2311 *
2312 **/
2313static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2314{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002315 struct bnx2fc_hba *hba;
2316
2317 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -07002318 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002319 if (hba->cnic == cnic)
2320 return hba;
2321 }
2322 return NULL;
2323}
2324
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002325static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2326 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002327{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002328 struct bnx2fc_interface *interface;
2329
2330 /* Called with bnx2fc_dev_lock held */
2331 list_for_each_entry(interface, &if_list, list) {
2332 if (interface->netdev == netdev)
2333 return interface;
2334 }
2335 return NULL;
2336}
2337
2338static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2339 *phys_dev)
2340{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002341 struct bnx2fc_hba *hba;
2342
2343 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002344 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002345 if (hba->phys_dev == phys_dev)
2346 return hba;
2347 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002348 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002349 return NULL;
2350}
2351
2352/**
2353 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2354 *
2355 * @dev cnic device handle
2356 */
2357static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2358{
2359 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002360 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002361
2362 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2363
2364 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2365 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2366 dev->netdev->name, dev->flags);
2367 return;
2368 }
2369
2370 mutex_lock(&bnx2fc_dev_lock);
2371 hba = bnx2fc_find_hba_for_cnic(dev);
2372 if (!hba) {
2373 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2374 dev);
2375 mutex_unlock(&bnx2fc_dev_lock);
2376 return;
2377 }
2378
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002379 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002380 adapter_count--;
2381
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002382 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002383 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002384 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002385 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002386 mutex_unlock(&bnx2fc_dev_lock);
2387
Eddie Wai06c4f202013-12-11 15:30:21 -08002388 /* Ensure ALL destroy work has been completed before return */
2389 flush_workqueue(bnx2fc_wq);
2390
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002391 bnx2fc_ulp_stop(hba);
2392 /* unregister cnic device */
2393 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2394 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002395 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002396}
2397
2398/**
2399 * bnx2fc_fcoe_reset - Resets the fcoe
2400 *
2401 * @shost: shost the reset is from
2402 *
2403 * Returns: always 0
2404 */
2405static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2406{
2407 struct fc_lport *lport = shost_priv(shost);
2408 fc_lport_reset(lport);
2409 return 0;
2410}
2411
2412
2413static bool bnx2fc_match(struct net_device *netdev)
2414{
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002415 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002416
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002417 mutex_lock(&bnx2fc_dev_lock);
2418 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2419 phys_dev = vlan_dev_real_dev(netdev);
2420
2421 if (bnx2fc_hba_lookup(phys_dev)) {
2422 mutex_unlock(&bnx2fc_dev_lock);
2423 return true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002424 }
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002425
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002426 mutex_unlock(&bnx2fc_dev_lock);
2427 return false;
2428}
2429
2430
2431static struct fcoe_transport bnx2fc_transport = {
2432 .name = {"bnx2fc"},
2433 .attached = false,
2434 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
Robert Love6e89ea32012-11-27 06:53:40 +00002435 .alloc = bnx2fc_ctlr_alloc,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002436 .match = bnx2fc_match,
2437 .create = bnx2fc_create,
2438 .destroy = bnx2fc_destroy,
2439 .enable = bnx2fc_enable,
2440 .disable = bnx2fc_disable,
2441};
2442
2443/**
2444 * bnx2fc_percpu_thread_create - Create a receive thread for an
2445 * online CPU
2446 *
2447 * @cpu: cpu index for the online cpu
2448 */
2449static void bnx2fc_percpu_thread_create(unsigned int cpu)
2450{
2451 struct bnx2fc_percpu_s *p;
2452 struct task_struct *thread;
2453
2454 p = &per_cpu(bnx2fc_percpu, cpu);
2455
Eric Dumazet69614272012-06-04 16:15:42 -07002456 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2457 (void *)p, cpu_to_node(cpu),
2458 "bnx2fc_thread/%d", cpu);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002459 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002460 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002461 kthread_bind(thread, cpu);
2462 p->iothread = thread;
2463 wake_up_process(thread);
2464 }
2465}
2466
2467static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2468{
2469 struct bnx2fc_percpu_s *p;
2470 struct task_struct *thread;
2471 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002472
2473 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2474
2475 /* Prevent any new work from being queued for this CPU */
2476 p = &per_cpu(bnx2fc_percpu, cpu);
2477 spin_lock_bh(&p->fp_work_lock);
2478 thread = p->iothread;
2479 p->iothread = NULL;
2480
2481
2482 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002483 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002484 list_del_init(&work->list);
2485 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2486 kfree(work);
2487 }
2488
2489 spin_unlock_bh(&p->fp_work_lock);
2490
2491 if (thread)
2492 kthread_stop(thread);
2493}
2494
2495/**
2496 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2497 *
2498 * @nfb: The callback data block
2499 * @action: The event triggering the callback
2500 * @hcpu: The index of the CPU that the event is for
2501 *
2502 * This creates or destroys per-CPU data for fcoe
2503 *
2504 * Returns NOTIFY_OK always.
2505 */
2506static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2507 unsigned long action, void *hcpu)
2508{
2509 unsigned cpu = (unsigned long)hcpu;
2510
2511 switch (action) {
2512 case CPU_ONLINE:
2513 case CPU_ONLINE_FROZEN:
2514 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2515 bnx2fc_percpu_thread_create(cpu);
2516 break;
2517 case CPU_DEAD:
2518 case CPU_DEAD_FROZEN:
2519 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2520 bnx2fc_percpu_thread_destroy(cpu);
2521 break;
2522 default:
2523 break;
2524 }
2525 return NOTIFY_OK;
2526}
2527
2528/**
2529 * bnx2fc_mod_init - module init entry point
2530 *
2531 * Initialize driver wide global data structures, and register
2532 * with cnic module
2533 **/
2534static int __init bnx2fc_mod_init(void)
2535{
2536 struct fcoe_percpu_s *bg;
2537 struct task_struct *l2_thread;
2538 int rc = 0;
2539 unsigned int cpu = 0;
2540 struct bnx2fc_percpu_s *p;
2541
2542 printk(KERN_INFO PFX "%s", version);
2543
2544 /* register as a fcoe transport */
2545 rc = fcoe_transport_attach(&bnx2fc_transport);
2546 if (rc) {
2547 printk(KERN_ERR "failed to register an fcoe transport, check "
2548 "if libfcoe is loaded\n");
2549 goto out;
2550 }
2551
2552 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002553 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002554 mutex_init(&bnx2fc_dev_lock);
2555 adapter_count = 0;
2556
2557 /* Attach FC transport template */
2558 rc = bnx2fc_attach_transport();
2559 if (rc)
2560 goto detach_ft;
2561
2562 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2563 if (!bnx2fc_wq) {
2564 rc = -ENOMEM;
2565 goto release_bt;
2566 }
2567
2568 bg = &bnx2fc_global;
2569 skb_queue_head_init(&bg->fcoe_rx_list);
2570 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2571 (void *)bg,
2572 "bnx2fc_l2_thread");
2573 if (IS_ERR(l2_thread)) {
2574 rc = PTR_ERR(l2_thread);
2575 goto free_wq;
2576 }
2577 wake_up_process(l2_thread);
2578 spin_lock_bh(&bg->fcoe_rx_list.lock);
2579 bg->thread = l2_thread;
2580 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2581
2582 for_each_possible_cpu(cpu) {
2583 p = &per_cpu(bnx2fc_percpu, cpu);
2584 INIT_LIST_HEAD(&p->work_list);
2585 spin_lock_init(&p->fp_work_lock);
2586 }
2587
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302588 cpu_notifier_register_begin();
2589
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002590 for_each_online_cpu(cpu) {
2591 bnx2fc_percpu_thread_create(cpu);
2592 }
2593
2594 /* Initialize per CPU interrupt thread */
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302595 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2596
2597 cpu_notifier_register_done();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002598
2599 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2600
2601 return 0;
2602
2603free_wq:
2604 destroy_workqueue(bnx2fc_wq);
2605release_bt:
2606 bnx2fc_release_transport();
2607detach_ft:
2608 fcoe_transport_detach(&bnx2fc_transport);
2609out:
2610 return rc;
2611}
2612
2613static void __exit bnx2fc_mod_exit(void)
2614{
2615 LIST_HEAD(to_be_deleted);
2616 struct bnx2fc_hba *hba, *next;
2617 struct fcoe_percpu_s *bg;
2618 struct task_struct *l2_thread;
2619 struct sk_buff *skb;
2620 unsigned int cpu = 0;
2621
2622 /*
2623 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2624 * it will have higher precedence than bnx2fc_dev_lock.
2625 * unregister_device() cannot be called with bnx2fc_dev_lock
2626 * held.
2627 */
2628 mutex_lock(&bnx2fc_dev_lock);
2629 list_splice(&adapter_list, &to_be_deleted);
2630 INIT_LIST_HEAD(&adapter_list);
2631 adapter_count = 0;
2632 mutex_unlock(&bnx2fc_dev_lock);
2633
2634 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002635 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2636 list_del_init(&hba->list);
2637 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2638 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002639 bnx2fc_ulp_stop(hba);
2640 /* unregister cnic device */
2641 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2642 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002643 hba->cnic->unregister_device(hba->cnic,
2644 CNIC_ULP_FCOE);
2645 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002646 }
2647 cnic_unregister_driver(CNIC_ULP_FCOE);
2648
2649 /* Destroy global thread */
2650 bg = &bnx2fc_global;
2651 spin_lock_bh(&bg->fcoe_rx_list.lock);
2652 l2_thread = bg->thread;
2653 bg->thread = NULL;
2654 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2655 kfree_skb(skb);
2656
2657 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2658
2659 if (l2_thread)
2660 kthread_stop(l2_thread);
2661
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302662 cpu_notifier_register_begin();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002663
2664 /* Destroy per cpu threads */
2665 for_each_online_cpu(cpu) {
2666 bnx2fc_percpu_thread_destroy(cpu);
2667 }
2668
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302669 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2670
2671 cpu_notifier_register_done();
2672
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002673 destroy_workqueue(bnx2fc_wq);
2674 /*
2675 * detach from scsi transport
2676 * must happen after all destroys are done
2677 */
2678 bnx2fc_release_transport();
2679
2680 /* detach from fcoe transport */
2681 fcoe_transport_detach(&bnx2fc_transport);
2682}
2683
2684module_init(bnx2fc_mod_init);
2685module_exit(bnx2fc_mod_exit);
2686
Robert Love8d55e502012-05-22 19:06:26 -07002687static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
Robert Love6e89ea32012-11-27 06:53:40 +00002688 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
Yi Zoub8b3e692012-12-06 06:24:59 +00002689 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2690 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2691 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2692 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2693 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2694 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
Robert Love8d55e502012-05-22 19:06:26 -07002695
2696 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2697 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2698};
2699
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002700static struct fc_function_template bnx2fc_transport_function = {
2701 .show_host_node_name = 1,
2702 .show_host_port_name = 1,
2703 .show_host_supported_classes = 1,
2704 .show_host_supported_fc4s = 1,
2705 .show_host_active_fc4s = 1,
2706 .show_host_maxframe_size = 1,
2707
2708 .show_host_port_id = 1,
2709 .show_host_supported_speeds = 1,
2710 .get_host_speed = fc_get_host_speed,
2711 .show_host_speed = 1,
2712 .show_host_port_type = 1,
2713 .get_host_port_state = fc_get_host_port_state,
2714 .show_host_port_state = 1,
2715 .show_host_symbolic_name = 1,
2716
2717 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2718 sizeof(struct bnx2fc_rport)),
2719 .show_rport_maxframe_size = 1,
2720 .show_rport_supported_classes = 1,
2721
2722 .show_host_fabric_name = 1,
2723 .show_starget_node_name = 1,
2724 .show_starget_port_name = 1,
2725 .show_starget_port_id = 1,
2726 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2727 .show_rport_dev_loss_tmo = 1,
2728 .get_fc_host_stats = bnx2fc_get_host_stats,
2729
2730 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2731
2732 .terminate_rport_io = fc_rport_terminate_io,
2733
2734 .vport_create = bnx2fc_vport_create,
2735 .vport_delete = bnx2fc_vport_destroy,
2736 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002737 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002738};
2739
2740static struct fc_function_template bnx2fc_vport_xport_function = {
2741 .show_host_node_name = 1,
2742 .show_host_port_name = 1,
2743 .show_host_supported_classes = 1,
2744 .show_host_supported_fc4s = 1,
2745 .show_host_active_fc4s = 1,
2746 .show_host_maxframe_size = 1,
2747
2748 .show_host_port_id = 1,
2749 .show_host_supported_speeds = 1,
2750 .get_host_speed = fc_get_host_speed,
2751 .show_host_speed = 1,
2752 .show_host_port_type = 1,
2753 .get_host_port_state = fc_get_host_port_state,
2754 .show_host_port_state = 1,
2755 .show_host_symbolic_name = 1,
2756
2757 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2758 sizeof(struct bnx2fc_rport)),
2759 .show_rport_maxframe_size = 1,
2760 .show_rport_supported_classes = 1,
2761
2762 .show_host_fabric_name = 1,
2763 .show_starget_node_name = 1,
2764 .show_starget_port_name = 1,
2765 .show_starget_port_id = 1,
2766 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2767 .show_rport_dev_loss_tmo = 1,
2768 .get_fc_host_stats = fc_get_host_stats,
2769 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2770 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002771 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002772};
2773
2774/**
2775 * scsi_host_template structure used while registering with SCSI-ml
2776 */
2777static struct scsi_host_template bnx2fc_shost_template = {
2778 .module = THIS_MODULE,
Saurav Kashyap17d87c42014-07-03 08:18:28 -04002779 .name = "QLogic Offload FCoE Initiator",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002780 .queuecommand = bnx2fc_queuecommand,
2781 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2782 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2783 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2784 .eh_host_reset_handler = fc_eh_host_reset,
2785 .slave_alloc = fc_slave_alloc,
2786 .change_queue_depth = fc_change_queue_depth,
2787 .change_queue_type = fc_change_queue_type,
2788 .this_id = -1,
2789 .cmd_per_lun = 3,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002790 .use_clustering = ENABLE_CLUSTERING,
2791 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
Bhanu Prakash Gollapudid450d772012-12-21 19:40:33 -08002792 .max_sectors = 1024,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002793};
2794
2795static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2796 .frame_send = bnx2fc_xmit,
2797 .elsct_send = bnx2fc_elsct_send,
2798 .fcp_abort_io = bnx2fc_abort_io,
2799 .fcp_cleanup = bnx2fc_cleanup,
Yi Zoub8b3e692012-12-06 06:24:59 +00002800 .get_lesb = fcoe_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002801 .rport_event_callback = bnx2fc_rport_event_handler,
2802};
2803
2804/**
2805 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2806 * structure carrying callback function pointers
2807 */
2808static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2809 .owner = THIS_MODULE,
2810 .cnic_init = bnx2fc_ulp_init,
2811 .cnic_exit = bnx2fc_ulp_exit,
2812 .cnic_start = bnx2fc_ulp_start,
2813 .cnic_stop = bnx2fc_ulp_stop,
2814 .indicate_kcqes = bnx2fc_indicate_kcqe,
2815 .indicate_netevent = bnx2fc_indicate_netevent,
Barak Witkowski2e499d32012-06-26 01:31:19 +00002816 .cnic_get_stats = bnx2fc_ulp_get_stats,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002817};