blob: c4ec235baead76b95039a884a9d933543b620ba8 [file] [log] [blame]
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001/* bnx2fc_fcoe.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * This file contains the code that interacts with libfc, libfcoe,
3 * cnic modules to create FCoE instances, send/receive non-offloaded
4 * FIP/FCoE packets, listen to link events etc.
5 *
Bhanu Prakash Gollapudicf122192013-03-08 13:28:53 -08006 * Copyright (c) 2008 - 2013 Broadcom Corporation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 *
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13 */
14
15#include "bnx2fc.h"
16
17static struct list_head adapter_list;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070018static struct list_head if_list;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080019static u32 adapter_count;
20static DEFINE_MUTEX(bnx2fc_dev_lock);
21DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22
23#define DRV_MODULE_NAME "bnx2fc"
24#define DRV_MODULE_VERSION BNX2FC_VERSION
Eddie Waiaa106202013-09-17 22:33:12 -070025#define DRV_MODULE_RELDATE "Sep 17, 2013"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080026
27
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080028static char version[] =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080029 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31
32
33MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35MODULE_LICENSE("GPL");
36MODULE_VERSION(DRV_MODULE_VERSION);
37
38#define BNX2FC_MAX_QUEUE_DEPTH 256
39#define BNX2FC_MIN_QUEUE_DEPTH 32
40#define FCOE_WORD_TO_BYTE 4
41
42static struct scsi_transport_template *bnx2fc_transport_template;
43static struct scsi_transport_template *bnx2fc_vport_xport_template;
44
45struct workqueue_struct *bnx2fc_wq;
46
47/* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
48 * Here the io threads are per cpu but the l2 thread is just one
49 */
50struct fcoe_percpu_s bnx2fc_global;
51DEFINE_SPINLOCK(bnx2fc_global_lock);
52
53static struct cnic_ulp_ops bnx2fc_cnic_cb;
54static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55static struct scsi_host_template bnx2fc_shost_template;
56static struct fc_function_template bnx2fc_transport_function;
Robert Love8d55e502012-05-22 19:06:26 -070057static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080058static struct fc_function_template bnx2fc_vport_xport_function;
59static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -070060static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080061static int bnx2fc_destroy(struct net_device *net_device);
62static int bnx2fc_enable(struct net_device *netdev);
63static int bnx2fc_disable(struct net_device *netdev);
64
Robert Love6e89ea32012-11-27 06:53:40 +000065/* fcoe_syfs control interface handlers */
66static int bnx2fc_ctlr_alloc(struct net_device *netdev);
67static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
68
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080069static void bnx2fc_recv_frame(struct sk_buff *skb);
70
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070071static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080072static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080073static int bnx2fc_lport_config(struct fc_lport *lport);
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +000074static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080075static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
76static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
77static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
78static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070079static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080080 struct device *parent, int npiv);
81static void bnx2fc_destroy_work(struct work_struct *work);
82
83static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070084static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
85 *phys_dev);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -070086static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080087static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
88
89static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
90static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
91
92static void bnx2fc_port_shutdown(struct fc_lport *lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070093static void bnx2fc_stop(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080094static int __init bnx2fc_mod_init(void);
95static void __exit bnx2fc_mod_exit(void);
96
97unsigned int bnx2fc_debug_level;
98module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
99
100static int bnx2fc_cpu_callback(struct notifier_block *nfb,
101 unsigned long action, void *hcpu);
102/* notification function for CPU hotplug events */
103static struct notifier_block bnx2fc_cpu_notifier = {
104 .notifier_call = bnx2fc_cpu_callback,
105};
106
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -0700107static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
108{
109 return ((struct bnx2fc_interface *)
110 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
111}
112
Robert Love8d55e502012-05-22 19:06:26 -0700113static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
114{
115 struct fcoe_ctlr_device *ctlr_dev =
116 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
117 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
118 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
119
120 fcf_dev->vlan_id = fcoe->vlan_id;
121}
122
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800123static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
124{
125 struct fcoe_percpu_s *bg;
126 struct fcoe_rcv_info *fr;
127 struct sk_buff_head *list;
128 struct sk_buff *skb, *next;
129 struct sk_buff *head;
130
131 bg = &bnx2fc_global;
132 spin_lock_bh(&bg->fcoe_rx_list.lock);
133 list = &bg->fcoe_rx_list;
134 head = list->next;
135 for (skb = head; skb != (struct sk_buff *)list;
136 skb = next) {
137 next = skb->next;
138 fr = fcoe_dev_from_skb(skb);
139 if (fr->fr_dev == lp) {
140 __skb_unlink(skb, list);
141 kfree_skb(skb);
142 }
143 }
144 spin_unlock_bh(&bg->fcoe_rx_list.lock);
145}
146
147int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
148{
149 int rc;
150 spin_lock(&bnx2fc_global_lock);
151 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
152 spin_unlock(&bnx2fc_global_lock);
153
154 return rc;
155}
156
157static void bnx2fc_abort_io(struct fc_lport *lport)
158{
159 /*
160 * This function is no-op for bnx2fc, but we do
161 * not want to leave it as NULL either, as libfc
162 * can call the default function which is
163 * fc_fcp_abort_io.
164 */
165}
166
167static void bnx2fc_cleanup(struct fc_lport *lport)
168{
169 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700170 struct bnx2fc_interface *interface = port->priv;
171 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800172 struct bnx2fc_rport *tgt;
173 int i;
174
175 BNX2FC_MISC_DBG("Entered %s\n", __func__);
176 mutex_lock(&hba->hba_mutex);
177 spin_lock_bh(&hba->hba_lock);
178 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
179 tgt = hba->tgt_ofld_list[i];
180 if (tgt) {
181 /* Cleanup IOs belonging to requested vport */
182 if (tgt->port == port) {
183 spin_unlock_bh(&hba->hba_lock);
184 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
185 bnx2fc_flush_active_ios(tgt);
186 spin_lock_bh(&hba->hba_lock);
187 }
188 }
189 }
190 spin_unlock_bh(&hba->hba_lock);
191 mutex_unlock(&hba->hba_mutex);
192}
193
194static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
195 struct fc_frame *fp)
196{
197 struct fc_rport_priv *rdata = tgt->rdata;
198 struct fc_frame_header *fh;
199 int rc = 0;
200
201 fh = fc_frame_header_get(fp);
202 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
203 "r_ctl = 0x%x\n", rdata->ids.port_id,
204 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
205 if ((fh->fh_type == FC_TYPE_ELS) &&
206 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
207
208 switch (fc_frame_payload_op(fp)) {
209 case ELS_ADISC:
210 rc = bnx2fc_send_adisc(tgt, fp);
211 break;
212 case ELS_LOGO:
213 rc = bnx2fc_send_logo(tgt, fp);
214 break;
215 case ELS_RLS:
216 rc = bnx2fc_send_rls(tgt, fp);
217 break;
218 default:
219 break;
220 }
221 } else if ((fh->fh_type == FC_TYPE_BLS) &&
222 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
223 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
224 else {
225 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
226 "rctl 0x%x thru non-offload path\n",
227 fh->fh_type, fh->fh_r_ctl);
228 return -ENODEV;
229 }
230 if (rc)
231 return -ENOMEM;
232 else
233 return 0;
234}
235
236/**
237 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
238 *
239 * @lport: the associated local port
240 * @fp: the fc_frame to be transmitted
241 */
242static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
243{
244 struct ethhdr *eh;
245 struct fcoe_crc_eof *cp;
246 struct sk_buff *skb;
247 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700248 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700249 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700250 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800251 struct fcoe_port *port;
252 struct fcoe_hdr *hp;
253 struct bnx2fc_rport *tgt;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700254 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800255 u8 sof, eof;
256 u32 crc;
257 unsigned int hlen, tlen, elen;
258 int wlen, rc = 0;
259
260 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700261 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700262 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700263 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800264
265 fh = fc_frame_header_get(fp);
266
267 skb = fp_skb(fp);
268 if (!lport->link_up) {
269 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
270 kfree_skb(skb);
271 return 0;
272 }
273
274 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Robert Lovefd8f8902012-05-22 19:06:16 -0700275 if (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800276 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
277 kfree_skb(skb);
278 return -EINVAL;
279 }
Robert Lovefd8f8902012-05-22 19:06:16 -0700280 if (fcoe_ctlr_els_send(ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800281 return 0;
282 }
283
284 sof = fr_sof(fp);
285 eof = fr_eof(fp);
286
287 /*
288 * Snoop the frame header to check if the frame is for
289 * an offloaded session
290 */
291 /*
292 * tgt_ofld_list access is synchronized using
293 * both hba mutex and hba lock. Atleast hba mutex or
294 * hba lock needs to be held for read access.
295 */
296
297 spin_lock_bh(&hba->hba_lock);
298 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
299 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
300 /* This frame is for offloaded session */
301 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
302 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
303 spin_unlock_bh(&hba->hba_lock);
304 rc = bnx2fc_xmit_l2_frame(tgt, fp);
305 if (rc != -ENODEV) {
306 kfree_skb(skb);
307 return rc;
308 }
309 } else {
310 spin_unlock_bh(&hba->hba_lock);
311 }
312
313 elen = sizeof(struct ethhdr);
314 hlen = sizeof(struct fcoe_hdr);
315 tlen = sizeof(struct fcoe_crc_eof);
316 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
317
318 skb->ip_summed = CHECKSUM_NONE;
319 crc = fcoe_fc_crc(fp);
320
321 /* copy port crc and eof to the skb buff */
322 if (skb_is_nonlinear(skb)) {
323 skb_frag_t *frag;
324 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
325 kfree_skb(skb);
326 return -ENOMEM;
327 }
328 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800329 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800330 } else {
331 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
332 }
333
334 memset(cp, 0, sizeof(*cp));
335 cp->fcoe_eof = eof;
336 cp->fcoe_crc32 = cpu_to_le32(~crc);
337 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800338 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800339 cp = NULL;
340 }
341
342 /* adjust skb network/transport offsets to match mac/fcoe/port */
343 skb_push(skb, elen + hlen);
344 skb_reset_mac_header(skb);
345 skb_reset_network_header(skb);
346 skb->mac_len = elen;
347 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700348 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800349
350 /* fill up mac and fcoe headers */
351 eh = eth_hdr(skb);
352 eh->h_proto = htons(ETH_P_FCOE);
Robert Lovefd8f8902012-05-22 19:06:16 -0700353 if (ctlr->map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800354 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
355 else
356 /* insert GW address */
Robert Lovefd8f8902012-05-22 19:06:16 -0700357 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800358
Robert Lovefd8f8902012-05-22 19:06:16 -0700359 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
360 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800361 else
362 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
363
364 hp = (struct fcoe_hdr *)(eh + 1);
365 memset(hp, 0, sizeof(*hp));
366 if (FC_FCOE_VER)
367 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
368 hp->fcoe_sof = sof;
369
370 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
371 if (lport->seq_offload && fr_max_payload(fp)) {
372 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
373 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
374 } else {
375 skb_shinfo(skb)->gso_type = 0;
376 skb_shinfo(skb)->gso_size = 0;
377 }
378
379 /*update tx stats */
Vasu Dev1bd49b42012-05-25 10:26:43 -0700380 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800381 stats->TxFrames++;
382 stats->TxWords += wlen;
383 put_cpu();
384
385 /* send down to lld */
386 fr_dev(fp) = lport;
387 if (port->fcoe_pending_queue.qlen)
388 fcoe_check_wait_queue(lport, skb);
389 else if (fcoe_start_io(skb))
390 fcoe_check_wait_queue(lport, skb);
391
392 return 0;
393}
394
395/**
396 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
397 *
398 * @skb: the receive socket buffer
399 * @dev: associated net device
400 * @ptype: context
401 * @olddev: last device
402 *
403 * This function receives the packet and builds FC frame and passes it up
404 */
405static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
406 struct packet_type *ptype, struct net_device *olddev)
407{
408 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700409 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700410 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800411 struct fc_frame_header *fh;
412 struct fcoe_rcv_info *fr;
413 struct fcoe_percpu_s *bg;
414 unsigned short oxid;
415
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700416 interface = container_of(ptype, struct bnx2fc_interface,
417 fcoe_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -0700418 ctlr = bnx2fc_to_ctlr(interface);
419 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800420
421 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700422 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800423 goto err;
424 }
425
426 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700427 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800428 goto err;
429 }
430
431 /*
432 * Check for minimum frame length, and make sure required FCoE
433 * and FC headers are pulled into the linear data area.
434 */
435 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
436 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
437 goto err;
438
439 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
440 fh = (struct fc_frame_header *) skb_transport_header(skb);
441
442 oxid = ntohs(fh->fh_ox_id);
443
444 fr = fcoe_dev_from_skb(skb);
445 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800446
447 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800448 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800449
450 __skb_queue_tail(&bg->fcoe_rx_list, skb);
451 if (bg->fcoe_rx_list.qlen == 1)
452 wake_up_process(bg->thread);
453
Neil Hormanfc05ab72012-03-09 14:50:13 -0800454 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800455
456 return 0;
457err:
458 kfree_skb(skb);
459 return -1;
460}
461
462static int bnx2fc_l2_rcv_thread(void *arg)
463{
464 struct fcoe_percpu_s *bg = arg;
465 struct sk_buff *skb;
466
467 set_user_nice(current, -20);
468 set_current_state(TASK_INTERRUPTIBLE);
469 while (!kthread_should_stop()) {
470 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800471 spin_lock_bh(&bg->fcoe_rx_list.lock);
472 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
473 spin_unlock_bh(&bg->fcoe_rx_list.lock);
474 bnx2fc_recv_frame(skb);
475 spin_lock_bh(&bg->fcoe_rx_list.lock);
476 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700477 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800478 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800479 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700480 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800481 return 0;
482}
483
484
485static void bnx2fc_recv_frame(struct sk_buff *skb)
486{
487 u32 fr_len;
488 struct fc_lport *lport;
489 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700490 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800491 struct fc_frame_header *fh;
492 struct fcoe_crc_eof crc_eof;
493 struct fc_frame *fp;
494 struct fc_lport *vn_port;
495 struct fcoe_port *port;
496 u8 *mac = NULL;
497 u8 *dest_mac = NULL;
498 struct fcoe_hdr *hp;
499
500 fr = fcoe_dev_from_skb(skb);
501 lport = fr->fr_dev;
502 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700503 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800504 kfree_skb(skb);
505 return;
506 }
507
508 if (skb_is_nonlinear(skb))
509 skb_linearize(skb);
510 mac = eth_hdr(skb)->h_source;
511 dest_mac = eth_hdr(skb)->h_dest;
512
513 /* Pull the header */
514 hp = (struct fcoe_hdr *) skb_network_header(skb);
515 fh = (struct fc_frame_header *) skb_transport_header(skb);
516 skb_pull(skb, sizeof(struct fcoe_hdr));
517 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
518
Vasu Dev1bd49b42012-05-25 10:26:43 -0700519 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800520 stats->RxFrames++;
521 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
522
523 fp = (struct fc_frame *)skb;
524 fc_frame_init(fp);
525 fr_dev(fp) = lport;
526 fr_sof(fp) = hp->fcoe_sof;
527 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
528 put_cpu();
529 kfree_skb(skb);
530 return;
531 }
532 fr_eof(fp) = crc_eof.fcoe_eof;
533 fr_crc(fp) = crc_eof.fcoe_crc32;
534 if (pskb_trim(skb, fr_len)) {
535 put_cpu();
536 kfree_skb(skb);
537 return;
538 }
539
540 fh = fc_frame_header_get(fp);
541
542 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
543 if (vn_port) {
544 port = lport_priv(vn_port);
Joe Perches6942df72013-09-02 03:32:33 +0000545 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800546 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
547 put_cpu();
548 kfree_skb(skb);
549 return;
550 }
551 }
552 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
553 fh->fh_type == FC_TYPE_FCP) {
554 /* Drop FCP data. We dont this in L2 path */
555 put_cpu();
556 kfree_skb(skb);
557 return;
558 }
559 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
560 fh->fh_type == FC_TYPE_ELS) {
561 switch (fc_frame_payload_op(fp)) {
562 case ELS_LOGO:
563 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
564 /* drop non-FIP LOGO */
565 put_cpu();
566 kfree_skb(skb);
567 return;
568 }
569 break;
570 }
571 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700572
573 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
574 /* Drop incoming ABTS */
575 put_cpu();
576 kfree_skb(skb);
577 return;
578 }
579
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800580 if (le32_to_cpu(fr_crc(fp)) !=
581 ~crc32(~0, skb->data, fr_len)) {
582 if (stats->InvalidCRCCount < 5)
583 printk(KERN_WARNING PFX "dropping frame with "
584 "CRC error\n");
585 stats->InvalidCRCCount++;
586 put_cpu();
587 kfree_skb(skb);
588 return;
589 }
590 put_cpu();
591 fc_exch_recv(lport, fp);
592}
593
594/**
595 * bnx2fc_percpu_io_thread - thread per cpu for ios
596 *
597 * @arg: ptr to bnx2fc_percpu_info structure
598 */
599int bnx2fc_percpu_io_thread(void *arg)
600{
601 struct bnx2fc_percpu_s *p = arg;
602 struct bnx2fc_work *work, *tmp;
603 LIST_HEAD(work_list);
604
605 set_user_nice(current, -20);
606 set_current_state(TASK_INTERRUPTIBLE);
607 while (!kthread_should_stop()) {
608 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800609 spin_lock_bh(&p->fp_work_lock);
610 while (!list_empty(&p->work_list)) {
611 list_splice_init(&p->work_list, &work_list);
612 spin_unlock_bh(&p->fp_work_lock);
613
614 list_for_each_entry_safe(work, tmp, &work_list, list) {
615 list_del_init(&work->list);
616 bnx2fc_process_cq_compl(work->tgt, work->wqe);
617 kfree(work);
618 }
619
620 spin_lock_bh(&p->fp_work_lock);
621 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700622 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800623 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800624 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700625 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800626
627 return 0;
628}
629
630static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
631{
632 struct fc_host_statistics *bnx2fc_stats;
633 struct fc_lport *lport = shost_priv(shost);
634 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700635 struct bnx2fc_interface *interface = port->priv;
636 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800637 struct fcoe_statistics_params *fw_stats;
638 int rc = 0;
639
640 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
641 if (!fw_stats)
642 return NULL;
643
644 bnx2fc_stats = fc_get_host_stats(shost);
645
646 init_completion(&hba->stat_req_done);
647 if (bnx2fc_send_stat_req(hba))
648 return bnx2fc_stats;
649 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
650 if (!rc) {
651 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
652 return bnx2fc_stats;
653 }
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800654 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
655 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
656 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
657 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
658 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
659 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
660 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
661 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
662 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
663 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800664
665 bnx2fc_stats->dumped_frames = 0;
666 bnx2fc_stats->lip_count = 0;
667 bnx2fc_stats->nos_count = 0;
668 bnx2fc_stats->loss_of_sync_count = 0;
669 bnx2fc_stats->loss_of_signal_count = 0;
670 bnx2fc_stats->prim_seq_protocol_err_count = 0;
671
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800672 memcpy(&hba->prev_stats, hba->stats_buffer,
673 sizeof(struct fcoe_statistics_params));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800674 return bnx2fc_stats;
675}
676
677static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
678{
679 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700680 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800681 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800682 struct Scsi_Host *shost = lport->host;
683 int rc = 0;
684
685 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
686 shost->max_lun = BNX2FC_MAX_LUN;
687 shost->max_id = BNX2FC_MAX_FCP_TGT;
688 shost->max_channel = 0;
689 if (lport->vport)
690 shost->transportt = bnx2fc_vport_xport_template;
691 else
692 shost->transportt = bnx2fc_transport_template;
693
694 /* Add the new host to SCSI-ml */
695 rc = scsi_add_host(lport->host, dev);
696 if (rc) {
697 printk(KERN_ERR PFX "Error on scsi_add_host\n");
698 return rc;
699 }
700 if (!lport->vport)
701 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800702 snprintf(fc_host_symbolic_name(lport->host), 256,
703 "%s (Broadcom %s) v%s over %s",
704 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700705 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800706
707 return 0;
708}
709
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800710static int bnx2fc_link_ok(struct fc_lport *lport)
711{
712 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700713 struct bnx2fc_interface *interface = port->priv;
714 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800715 struct net_device *dev = hba->phys_dev;
716 int rc = 0;
717
718 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
719 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
720 else {
721 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
722 rc = -1;
723 }
724 return rc;
725}
726
727/**
728 * bnx2fc_get_link_state - get network link state
729 *
730 * @hba: adapter instance pointer
731 *
732 * updates adapter structure flag based on netdev state
733 */
734void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
735{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700736 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800737 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
738 else
739 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
740}
741
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700742static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800743{
744 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700745 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700746 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800747 struct fcoe_port *port;
748 u64 wwnn, wwpn;
749
750 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700751 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700752 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700753 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800754
755 /* require support for get_pauseparam ethtool op. */
756 if (!hba->phys_dev->ethtool_ops ||
757 !hba->phys_dev->ethtool_ops->get_pauseparam)
758 return -EOPNOTSUPP;
759
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700760 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800761 return -EINVAL;
762
763 skb_queue_head_init(&port->fcoe_pending_queue);
764 port->fcoe_pending_queue_active = 0;
765 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
766
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000767 fcoe_link_speed_update(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800768
769 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700770 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700771 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700772 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800773 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
774 fc_set_wwnn(lport, wwnn);
775
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700776 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700777 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700778 2, 0);
779
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800780 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
781 fc_set_wwpn(lport, wwpn);
782 }
783
784 return 0;
785}
786
787static void bnx2fc_destroy_timer(unsigned long data)
788{
789 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
790
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700791 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
792 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700793 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800794 wake_up_interruptible(&hba->destroy_wait);
795}
796
797/**
798 * bnx2fc_indicate_netevent - Generic netdev event handler
799 *
800 * @context: adapter structure pointer
801 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000802 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800803 *
804 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700805 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800806 */
Michael Chan415199f2011-07-20 14:55:24 +0000807static void bnx2fc_indicate_netevent(void *context, unsigned long event,
808 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800809{
810 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Robert Love6e89ea32012-11-27 06:53:40 +0000811 struct fcoe_ctlr_device *cdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700812 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800813 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700814 struct bnx2fc_interface *interface, *tmp;
Robert Lovefd8f8902012-05-22 19:06:16 -0700815 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700816 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800817 u32 link_possible = 1;
818
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700819 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000820 return;
821
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800822 switch (event) {
823 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800824 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
825 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700826 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800827 break;
828
829 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800830 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
831 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
832 link_possible = 0;
833 break;
834
835 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800836 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
837 link_possible = 0;
838 break;
839
840 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800841 break;
842
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700843 case NETDEV_UNREGISTER:
844 if (!vlan_id)
845 return;
846 mutex_lock(&bnx2fc_dev_lock);
847 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700848 if (interface->hba == hba &&
849 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
850 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700851 }
852 mutex_unlock(&bnx2fc_dev_lock);
853 return;
854
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800855 default:
Masanari Iida59e13d42012-04-25 00:24:16 +0900856 printk(KERN_ERR PFX "Unknown netevent %ld", event);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800857 return;
858 }
859
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700860 mutex_lock(&bnx2fc_dev_lock);
861 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800862
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700863 if (interface->hba != hba)
864 continue;
865
Robert Lovefd8f8902012-05-22 19:06:16 -0700866 ctlr = bnx2fc_to_ctlr(interface);
867 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700868 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
869 interface->netdev->name, event);
870
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000871 fcoe_link_speed_update(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700872
Robert Love6e89ea32012-11-27 06:53:40 +0000873 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700874
875 if (link_possible && !bnx2fc_link_ok(lport)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000876 switch (cdev->enabled) {
877 case FCOE_CTLR_DISABLED:
878 pr_info("Link up while interface is disabled.\n");
879 break;
880 case FCOE_CTLR_ENABLED:
881 case FCOE_CTLR_UNUSED:
882 /* Reset max recv frame size to default */
883 fc_set_mfs(lport, BNX2FC_MFS);
884 /*
885 * ctlr link up will only be handled during
886 * enable to avoid sending discovery
887 * solicitation on a stale vlan
888 */
889 if (interface->enabled)
890 fcoe_ctlr_link_up(ctlr);
891 };
Robert Lovefd8f8902012-05-22 19:06:16 -0700892 } else if (fcoe_ctlr_link_down(ctlr)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000893 switch (cdev->enabled) {
894 case FCOE_CTLR_DISABLED:
895 pr_info("Link down while interface is disabled.\n");
896 break;
897 case FCOE_CTLR_ENABLED:
898 case FCOE_CTLR_UNUSED:
899 mutex_lock(&lport->lp_mutex);
900 list_for_each_entry(vport, &lport->vports, list)
901 fc_host_port_type(vport->host) =
902 FC_PORTTYPE_UNKNOWN;
903 mutex_unlock(&lport->lp_mutex);
904 fc_host_port_type(lport->host) =
905 FC_PORTTYPE_UNKNOWN;
906 per_cpu_ptr(lport->stats,
907 get_cpu())->LinkFailureCount++;
908 put_cpu();
909 fcoe_clean_pending_queue(lport);
910 wait_for_upload = 1;
911 };
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800912 }
913 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700914 mutex_unlock(&bnx2fc_dev_lock);
915
916 if (wait_for_upload) {
917 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
918 init_waitqueue_head(&hba->shutdown_wait);
919 BNX2FC_MISC_DBG("indicate_netevent "
920 "num_ofld_sess = %d\n",
921 hba->num_ofld_sess);
922 hba->wait_for_link_down = 1;
923 wait_event_interruptible(hba->shutdown_wait,
924 (hba->num_ofld_sess == 0));
925 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
926 hba->num_ofld_sess);
927 hba->wait_for_link_down = 0;
928
929 if (signal_pending(current))
930 flush_signals(current);
931 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800932}
933
934static int bnx2fc_libfc_config(struct fc_lport *lport)
935{
936
937 /* Set the function pointers set by bnx2fc driver */
938 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
939 sizeof(struct libfc_function_template));
940 fc_elsct_init(lport);
941 fc_exch_init(lport);
942 fc_rport_init(lport);
Robert Love08076192013-03-25 11:00:28 -0700943 fc_disc_init(lport);
944 fc_disc_config(lport, lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800945 return 0;
946}
947
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000948static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800949{
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000950 int fcoe_min_xid, fcoe_max_xid;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800951
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000952 fcoe_min_xid = hba->max_xid + 1;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800953 if (nr_cpu_ids <= 2)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000954 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800955 else
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000956 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
957 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
958 fcoe_max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800959 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
960 return -ENOMEM;
961 }
962
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800963 return 0;
964}
965
966static int bnx2fc_lport_config(struct fc_lport *lport)
967{
968 lport->link_up = 0;
969 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800970 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
971 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800972 lport->e_d_tov = 2 * 1000;
973 lport->r_a_tov = 10 * 1000;
974
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800975 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
976 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800977 lport->does_npiv = 1;
978
979 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
980 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
981
982 /* alloc stats structure */
983 if (fc_lport_init_stats(lport))
984 return -ENOMEM;
985
986 /* Finish fc_lport configuration */
987 fc_lport_config(lport);
988
989 return 0;
990}
991
992/**
993 * bnx2fc_fip_recv - handle a received FIP frame.
994 *
995 * @skb: the received skb
996 * @dev: associated &net_device
997 * @ptype: the &packet_type structure which was used to register this handler.
998 * @orig_dev: original receive &net_device, in case @ dev is a bond.
999 *
1000 * Returns: 0 for success
1001 */
1002static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1003 struct packet_type *ptype,
1004 struct net_device *orig_dev)
1005{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001006 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001007 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001008 interface = container_of(ptype, struct bnx2fc_interface,
1009 fip_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -07001010 ctlr = bnx2fc_to_ctlr(interface);
1011 fcoe_ctlr_recv(ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001012 return 0;
1013}
1014
1015/**
1016 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1017 *
1018 * @fip: FCoE controller.
1019 * @old: Unicast MAC address to delete if the MAC is non-zero.
1020 * @new: Unicast MAC address to add.
1021 *
1022 * Remove any previously-set unicast MAC filter.
1023 * Add secondary FCoE MAC address filter for our OUI.
1024 */
1025static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1026{
1027 struct fcoe_port *port = lport_priv(lport);
1028
1029 memcpy(port->data_src_addr, addr, ETH_ALEN);
1030}
1031
1032/**
1033 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1034 *
1035 * @lport: libfc port
1036 */
1037static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1038{
1039 struct fcoe_port *port;
1040
1041 port = (struct fcoe_port *)lport_priv(lport);
1042 return port->data_src_addr;
1043}
1044
1045/**
1046 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1047 *
1048 * @fip: FCoE controller.
1049 * @skb: FIP Packet.
1050 */
1051static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1052{
1053 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1054 dev_queue_xmit(skb);
1055}
1056
1057static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1058{
1059 struct Scsi_Host *shost = vport_to_shost(vport);
1060 struct fc_lport *n_port = shost_priv(shost);
1061 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001062 struct bnx2fc_interface *interface = port->priv;
1063 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001064 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001065 int rc;
1066 char buf[32];
1067
1068 rc = fcoe_validate_vport_create(vport);
1069 if (rc) {
1070 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1071 printk(KERN_ERR PFX "Failed to create vport, "
1072 "WWPN (0x%s) already exists\n",
1073 buf);
1074 return rc;
1075 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001076
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001077 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001078 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001079 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001080 return -EIO;
1081 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001082 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001083 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001084 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001085 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001086 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001087
1088 if (IS_ERR(vn_port)) {
1089 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1090 netdev->name);
1091 return -EIO;
1092 }
1093
1094 if (disabled) {
1095 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1096 } else {
1097 vn_port->boot_time = jiffies;
1098 fc_lport_init(vn_port);
1099 fc_fabric_login(vn_port);
1100 fc_vport_setlink(vn_port);
1101 }
1102 return 0;
1103}
1104
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001105static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1106{
1107 struct bnx2fc_lport *blport, *tmp;
1108
1109 spin_lock_bh(&hba->hba_lock);
1110 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1111 if (blport->lport == lport) {
1112 list_del(&blport->list);
1113 kfree(blport);
1114 }
1115 }
1116 spin_unlock_bh(&hba->hba_lock);
1117}
1118
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001119static int bnx2fc_vport_destroy(struct fc_vport *vport)
1120{
1121 struct Scsi_Host *shost = vport_to_shost(vport);
1122 struct fc_lport *n_port = shost_priv(shost);
1123 struct fc_lport *vn_port = vport->dd_data;
1124 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001125 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001126 struct fc_lport *v_port;
1127 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001128
1129 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001130 list_for_each_entry(v_port, &n_port->vports, list)
1131 if (v_port->vport == vport) {
1132 found = true;
1133 break;
1134 }
1135
1136 if (!found) {
1137 mutex_unlock(&n_port->lp_mutex);
1138 return -ENOENT;
1139 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001140 list_del(&vn_port->list);
1141 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001142 bnx2fc_free_vport(interface->hba, port->lport);
1143 bnx2fc_port_shutdown(port->lport);
1144 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001145 queue_work(bnx2fc_wq, &port->destroy_work);
1146 return 0;
1147}
1148
1149static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1150{
1151 struct fc_lport *lport = vport->dd_data;
1152
1153 if (disable) {
1154 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1155 fc_fabric_logoff(lport);
1156 } else {
1157 lport->boot_time = jiffies;
1158 fc_fabric_login(lport);
1159 fc_vport_setlink(lport);
1160 }
1161 return 0;
1162}
1163
1164
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001165static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001166{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001167 struct net_device *netdev = interface->netdev;
1168 struct net_device *physdev = interface->hba->phys_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001169 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001170 struct netdev_hw_addr *ha;
1171 int sel_san_mac = 0;
1172
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001173 /* setup Source MAC Address */
1174 rcu_read_lock();
1175 for_each_dev_addr(physdev, ha) {
1176 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1177 ha->type);
1178 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1179 ha->addr[1], ha->addr[2], ha->addr[3],
1180 ha->addr[4], ha->addr[5]);
1181
1182 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1183 (is_valid_ether_addr(ha->addr))) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001184 memcpy(ctlr->ctl_src_addr, ha->addr,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001185 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001186 sel_san_mac = 1;
1187 BNX2FC_MISC_DBG("Found SAN MAC\n");
1188 }
1189 }
1190 rcu_read_unlock();
1191
1192 if (!sel_san_mac)
1193 return -ENODEV;
1194
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001195 interface->fip_packet_type.func = bnx2fc_fip_recv;
1196 interface->fip_packet_type.type = htons(ETH_P_FIP);
1197 interface->fip_packet_type.dev = netdev;
1198 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001199
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001200 interface->fcoe_packet_type.func = bnx2fc_rcv;
1201 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1202 interface->fcoe_packet_type.dev = netdev;
1203 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001204
1205 return 0;
1206}
1207
1208static int bnx2fc_attach_transport(void)
1209{
1210 bnx2fc_transport_template =
1211 fc_attach_transport(&bnx2fc_transport_function);
1212
1213 if (bnx2fc_transport_template == NULL) {
1214 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1215 return -ENODEV;
1216 }
1217
1218 bnx2fc_vport_xport_template =
1219 fc_attach_transport(&bnx2fc_vport_xport_function);
1220 if (bnx2fc_vport_xport_template == NULL) {
1221 printk(KERN_ERR PFX
1222 "Failed to attach FC transport for vport\n");
1223 fc_release_transport(bnx2fc_transport_template);
1224 bnx2fc_transport_template = NULL;
1225 return -ENODEV;
1226 }
1227 return 0;
1228}
1229static void bnx2fc_release_transport(void)
1230{
1231 fc_release_transport(bnx2fc_transport_template);
1232 fc_release_transport(bnx2fc_vport_xport_template);
1233 bnx2fc_transport_template = NULL;
1234 bnx2fc_vport_xport_template = NULL;
1235}
1236
1237static void bnx2fc_interface_release(struct kref *kref)
1238{
Robert Love8d55e502012-05-22 19:06:26 -07001239 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001240 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001241 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001242 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001243
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001244 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001245 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001246
Robert Lovefd8f8902012-05-22 19:06:16 -07001247 ctlr = bnx2fc_to_ctlr(interface);
Robert Love8d55e502012-05-22 19:06:26 -07001248 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001249 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001250
1251 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001252 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
Robert Lovefd8f8902012-05-22 19:06:16 -07001253 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001254
Robert Love8d55e502012-05-22 19:06:26 -07001255 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001256
1257 dev_put(netdev);
1258 module_put(THIS_MODULE);
1259}
1260
1261static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1262{
1263 kref_get(&interface->kref);
1264}
1265
1266static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1267{
1268 kref_put(&interface->kref, bnx2fc_interface_release);
1269}
1270static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1271{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001272 /* Free the command manager */
1273 if (hba->cmd_mgr) {
1274 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1275 hba->cmd_mgr = NULL;
1276 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001277 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001278 bnx2fc_unbind_pcidev(hba);
1279 kfree(hba);
1280}
1281
1282/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001283 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001284 *
1285 * @cnic: pointer to cnic device
1286 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001287 * Creates a new FCoE hba on the given device.
1288 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001289 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001290static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001291{
1292 struct bnx2fc_hba *hba;
Barak Witkowski2e499d32012-06-26 01:31:19 +00001293 struct fcoe_capabilities *fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001294 int rc;
1295
1296 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1297 if (!hba) {
1298 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1299 return NULL;
1300 }
1301 spin_lock_init(&hba->hba_lock);
1302 mutex_init(&hba->hba_mutex);
1303
1304 hba->cnic = cnic;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001305
1306 hba->max_tasks = cnic->max_fcoe_exchanges;
1307 hba->elstm_xids = (hba->max_tasks / 2);
1308 hba->max_outstanding_cmds = hba->elstm_xids;
1309 hba->max_xid = (hba->max_tasks - 1);
1310
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001311 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001312 if (rc) {
1313 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001314 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001315 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001316 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001317 hba->next_conn_id = 0;
1318
1319 hba->tgt_ofld_list =
1320 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1321 GFP_KERNEL);
1322 if (!hba->tgt_ofld_list) {
1323 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1324 goto tgtofld_err;
1325 }
1326
1327 hba->num_ofld_sess = 0;
1328
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001329 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001330 if (!hba->cmd_mgr) {
1331 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1332 goto cmgr_err;
1333 }
Barak Witkowski2e499d32012-06-26 01:31:19 +00001334 fcoe_cap = &hba->fcoe_cap;
1335
1336 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1337 FCOE_IOS_PER_CONNECTION_SHIFT;
1338 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1339 FCOE_LOGINS_PER_PORT_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001340 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001341 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1342 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1343 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1344 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1345 FCOE_TARGETS_SUPPORTED_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001346 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001347 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1348 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001349
1350 init_waitqueue_head(&hba->shutdown_wait);
1351 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001352 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001353
1354 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001355
1356cmgr_err:
1357 kfree(hba->tgt_ofld_list);
1358tgtofld_err:
1359 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001360bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001361 kfree(hba);
1362 return NULL;
1363}
1364
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001365struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1366 struct net_device *netdev,
1367 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001368{
Robert Love8d55e502012-05-22 19:06:26 -07001369 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001370 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001371 struct fcoe_ctlr *ctlr;
1372 int size;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001373 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001374
Robert Lovefd8f8902012-05-22 19:06:16 -07001375 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
Robert Love8d55e502012-05-22 19:06:26 -07001376 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1377 size);
1378 if (!ctlr_dev) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001379 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1380 return NULL;
1381 }
Robert Love8d55e502012-05-22 19:06:26 -07001382 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love9d348762013-09-05 07:47:27 +00001383 ctlr->cdev = ctlr_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001384 interface = fcoe_ctlr_priv(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001385 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001386 kref_init(&interface->kref);
1387 interface->hba = hba;
1388 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001389
1390 /* Initialize FIP */
Robert Lovefd8f8902012-05-22 19:06:16 -07001391 fcoe_ctlr_init(ctlr, fip_mode);
1392 ctlr->send = bnx2fc_fip_send;
1393 ctlr->update_mac = bnx2fc_update_src_mac;
1394 ctlr->get_src_addr = bnx2fc_get_src_mac;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001395 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001396
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001397 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001398 if (!rc)
1399 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001400
Robert Lovefd8f8902012-05-22 19:06:16 -07001401 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001402 dev_put(netdev);
Robert Love8d55e502012-05-22 19:06:26 -07001403 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001404 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001405}
1406
1407/**
1408 * bnx2fc_if_create - Create FCoE instance on a given interface
1409 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001410 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001411 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1412 * @npiv: Indicates if the port is vport or not
1413 *
1414 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1415 *
1416 * Returns: Allocated fc_lport or an error pointer
1417 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001418static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001419 struct device *parent, int npiv)
1420{
Robert Lovefd8f8902012-05-22 19:06:16 -07001421 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Vasu Dev134a4e22011-04-28 15:55:44 -07001422 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001423 struct fcoe_port *port;
1424 struct Scsi_Host *shost;
1425 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001426 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001427 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001428 int rc = 0;
1429
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001430 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1431 if (!blport) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001432 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001433 return NULL;
1434 }
1435
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001436 /* Allocate Scsi_Host structure */
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001437 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
Vasu Dev134a4e22011-04-28 15:55:44 -07001438 if (!npiv)
1439 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1440 else
1441 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001442
1443 if (!lport) {
1444 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001445 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001446 }
1447 shost = lport->host;
1448 port = lport_priv(lport);
1449 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001450 port->priv = interface;
Yi Zouc3d79092012-12-06 06:24:29 +00001451 port->get_netdev = bnx2fc_netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001452 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1453
1454 /* Configure fcoe_port */
1455 rc = bnx2fc_lport_config(lport);
1456 if (rc)
1457 goto lp_config_err;
1458
1459 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001460 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1461 vport->node_name, vport->port_name);
1462 fc_set_wwnn(lport, vport->node_name);
1463 fc_set_wwpn(lport, vport->port_name);
1464 }
1465 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001466 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001467 if (rc) {
1468 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1469 goto lp_config_err;
1470 }
1471
1472 rc = bnx2fc_shost_config(lport, parent);
1473 if (rc) {
1474 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001475 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001476 goto lp_config_err;
1477 }
1478
1479 /* Initialize the libfc library */
1480 rc = bnx2fc_libfc_config(lport);
1481 if (rc) {
1482 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1483 goto shost_err;
1484 }
1485 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1486
1487 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001488 if (!npiv)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001489 rc = bnx2fc_em_config(lport, hba);
Vasu Dev134a4e22011-04-28 15:55:44 -07001490 else {
1491 shost = vport_to_shost(vport);
1492 n_port = shost_priv(shost);
1493 rc = fc_exch_mgr_list_clone(n_port, lport);
1494 }
1495
1496 if (rc) {
1497 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1498 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001499 }
1500
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001501 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001502
1503 spin_lock_bh(&hba->hba_lock);
1504 blport->lport = lport;
1505 list_add_tail(&blport->list, &hba->vports);
1506 spin_unlock_bh(&hba->hba_lock);
1507
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001508 return lport;
1509
1510shost_err:
1511 scsi_remove_host(shost);
1512lp_config_err:
1513 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001514free_blport:
1515 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001516 return NULL;
1517}
1518
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001519static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001520{
1521 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001522 __dev_remove_pack(&interface->fcoe_packet_type);
1523 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001524 synchronize_net();
1525}
1526
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001527static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001528{
Robert Lovefd8f8902012-05-22 19:06:16 -07001529 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1530 struct fc_lport *lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001531 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001532 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001533
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001534 /* Stop the transmit retry timer */
1535 del_timer_sync(&port->timer);
1536
1537 /* Free existing transmit skbs */
1538 fcoe_clean_pending_queue(lport);
1539
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001540 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001541
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001542 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001543}
1544
1545static void bnx2fc_if_destroy(struct fc_lport *lport)
1546{
1547
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001548 /* Free queued packets for the receive thread */
1549 bnx2fc_clean_rx_queue(lport);
1550
1551 /* Detach from scsi-ml */
1552 fc_remove_host(lport->host);
1553 scsi_remove_host(lport->host);
1554
1555 /*
1556 * Note that only the physical lport will have the exchange manager.
1557 * for vports, this function is NOP
1558 */
1559 fc_exch_mgr_free(lport);
1560
1561 /* Free memory used by statistical counters */
1562 fc_lport_free_stats(lport);
1563
1564 /* Release Scsi_Host */
1565 scsi_host_put(lport->host);
1566}
1567
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001568static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001569{
Robert Lovefd8f8902012-05-22 19:06:16 -07001570 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1571 struct fc_lport *lport = ctlr->lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001572 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001573
1574 bnx2fc_interface_cleanup(interface);
1575 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001576 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001577 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001578 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001579}
1580
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001581/**
1582 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1583 *
1584 * @buffer: The name of the Ethernet interface to be destroyed
1585 * @kp: The associated kernel parameter
1586 *
1587 * Called from sysfs.
1588 *
1589 * Returns: 0 for success
1590 */
1591static int bnx2fc_destroy(struct net_device *netdev)
1592{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001593 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001594 struct workqueue_struct *timer_work_queue;
Robert Lovefd8f8902012-05-22 19:06:16 -07001595 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001596 int rc = 0;
1597
Nithin Sujir6702ca12011-03-17 17:13:27 -07001598 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001599 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001600
1601 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001602 ctlr = bnx2fc_to_ctlr(interface);
1603 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001604 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001605 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001606 goto netdev_err;
1607 }
1608
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001609 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001610 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001611 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001612
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001613netdev_err:
1614 mutex_unlock(&bnx2fc_dev_lock);
1615 rtnl_unlock();
1616 return rc;
1617}
1618
1619static void bnx2fc_destroy_work(struct work_struct *work)
1620{
1621 struct fcoe_port *port;
1622 struct fc_lport *lport;
1623
1624 port = container_of(work, struct fcoe_port, destroy_work);
1625 lport = port->lport;
1626
1627 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1628
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001629 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001630}
1631
1632static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1633{
1634 bnx2fc_free_fw_resc(hba);
1635 bnx2fc_free_task_ctx(hba);
1636}
1637
1638/**
1639 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1640 * pci structure
1641 *
1642 * @hba: Adapter instance
1643 */
1644static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1645{
1646 if (bnx2fc_setup_task_ctx(hba))
1647 goto mem_err;
1648
1649 if (bnx2fc_setup_fw_resc(hba))
1650 goto mem_err;
1651
1652 return 0;
1653mem_err:
1654 bnx2fc_unbind_adapter_devices(hba);
1655 return -ENOMEM;
1656}
1657
1658static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1659{
1660 struct cnic_dev *cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001661 struct pci_dev *pdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001662
1663 if (!hba->cnic) {
1664 printk(KERN_ERR PFX "cnic is NULL\n");
1665 return -ENODEV;
1666 }
1667 cnic = hba->cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001668 pdev = hba->pcidev = cnic->pcidev;
1669 if (!hba->pcidev)
1670 return -ENODEV;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001671
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001672 switch (pdev->device) {
1673 case PCI_DEVICE_ID_NX2_57710:
1674 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1675 break;
1676 case PCI_DEVICE_ID_NX2_57711:
1677 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1678 break;
1679 case PCI_DEVICE_ID_NX2_57712:
1680 case PCI_DEVICE_ID_NX2_57712_MF:
1681 case PCI_DEVICE_ID_NX2_57712_VF:
1682 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1683 break;
1684 case PCI_DEVICE_ID_NX2_57800:
1685 case PCI_DEVICE_ID_NX2_57800_MF:
1686 case PCI_DEVICE_ID_NX2_57800_VF:
1687 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1688 break;
1689 case PCI_DEVICE_ID_NX2_57810:
1690 case PCI_DEVICE_ID_NX2_57810_MF:
1691 case PCI_DEVICE_ID_NX2_57810_VF:
1692 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1693 break;
1694 case PCI_DEVICE_ID_NX2_57840:
1695 case PCI_DEVICE_ID_NX2_57840_MF:
1696 case PCI_DEVICE_ID_NX2_57840_VF:
1697 case PCI_DEVICE_ID_NX2_57840_2_20:
1698 case PCI_DEVICE_ID_NX2_57840_4_10:
1699 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1700 break;
1701 default:
1702 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1703 break;
1704 }
1705 pci_dev_get(hba->pcidev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001706 return 0;
1707}
1708
1709static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1710{
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001711 if (hba->pcidev) {
1712 hba->chip_num[0] = '\0';
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001713 pci_dev_put(hba->pcidev);
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001714 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001715 hba->pcidev = NULL;
1716}
1717
Barak Witkowski2e499d32012-06-26 01:31:19 +00001718/**
1719 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1720 *
1721 * @handle: transport handle pointing to adapter struture
1722 */
1723static int bnx2fc_ulp_get_stats(void *handle)
1724{
1725 struct bnx2fc_hba *hba = handle;
1726 struct cnic_dev *cnic;
1727 struct fcoe_stats_info *stats_addr;
1728
1729 if (!hba)
1730 return -EINVAL;
1731
1732 cnic = hba->cnic;
1733 stats_addr = &cnic->stats_addr->fcoe_stat;
1734 if (!stats_addr)
1735 return -EINVAL;
1736
1737 strncpy(stats_addr->version, BNX2FC_VERSION,
1738 sizeof(stats_addr->version));
1739 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1740 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1741
1742 return 0;
1743}
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001744
1745
1746/**
1747 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1748 *
Masanari Iida59e13d42012-04-25 00:24:16 +09001749 * @handle: transport handle pointing to adapter structure
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001750 *
1751 * This function maps adapter structure to pcidev structure and initiates
1752 * firmware handshake to enable/initialize on-chip FCoE components.
1753 * This bnx2fc - cnic interface api callback is used after following
1754 * conditions are met -
1755 * a) underlying network interface is up (marked by event NETDEV_UP
1756 * from netdev
1757 * b) bnx2fc adatper structure is registered.
1758 */
1759static void bnx2fc_ulp_start(void *handle)
1760{
1761 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001762 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001763 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001764 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001765
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001766 mutex_lock(&bnx2fc_dev_lock);
1767
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001768 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001769 bnx2fc_fw_init(hba);
1770
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001771 BNX2FC_MISC_DBG("bnx2fc started.\n");
1772
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001773 list_for_each_entry(interface, &if_list, list) {
1774 if (interface->hba == hba) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001775 ctlr = bnx2fc_to_ctlr(interface);
1776 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001777 /* Kick off Fabric discovery*/
1778 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1779 lport->tt.frame_send = bnx2fc_xmit;
1780 bnx2fc_start_disc(interface);
1781 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001782 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001783
1784 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001785}
1786
1787static void bnx2fc_port_shutdown(struct fc_lport *lport)
1788{
1789 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1790 fc_fabric_logoff(lport);
1791 fc_lport_destroy(lport);
1792}
1793
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001794static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001795{
Robert Lovefd8f8902012-05-22 19:06:16 -07001796 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001797 struct fc_lport *lport;
1798 struct fc_lport *vport;
1799
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001800 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1801 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001802
Robert Lovefd8f8902012-05-22 19:06:16 -07001803 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001804 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001805
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001806 mutex_lock(&lport->lp_mutex);
1807 list_for_each_entry(vport, &lport->vports, list)
1808 fc_host_port_type(vport->host) =
1809 FC_PORTTYPE_UNKNOWN;
1810 mutex_unlock(&lport->lp_mutex);
1811 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
Robert Lovefd8f8902012-05-22 19:06:16 -07001812 fcoe_ctlr_link_down(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001813 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001814}
1815
1816static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1817{
1818#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1819 int rc = -1;
1820 int i = HZ;
1821
1822 rc = bnx2fc_bind_adapter_devices(hba);
1823 if (rc) {
1824 printk(KERN_ALERT PFX
1825 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1826 goto err_out;
1827 }
1828
1829 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1830 if (rc) {
1831 printk(KERN_ALERT PFX
1832 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1833 goto err_unbind;
1834 }
1835
1836 /*
1837 * Wait until the adapter init message is complete, and adapter
1838 * state is UP.
1839 */
1840 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1841 msleep(BNX2FC_INIT_POLL_TIME);
1842
1843 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1844 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1845 "Ignoring...\n",
1846 hba->cnic->netdev->name);
1847 rc = -1;
1848 goto err_unbind;
1849 }
1850
1851
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001852 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001853 return 0;
1854
1855err_unbind:
1856 bnx2fc_unbind_adapter_devices(hba);
1857err_out:
1858 return rc;
1859}
1860
1861static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1862{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001863 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001864 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1865 init_timer(&hba->destroy_timer);
1866 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1867 jiffies;
1868 hba->destroy_timer.function = bnx2fc_destroy_timer;
1869 hba->destroy_timer.data = (unsigned long)hba;
1870 add_timer(&hba->destroy_timer);
1871 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001872 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1873 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001874 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001875 /* This should never happen */
1876 if (signal_pending(current))
1877 flush_signals(current);
1878
1879 del_timer_sync(&hba->destroy_timer);
1880 }
1881 bnx2fc_unbind_adapter_devices(hba);
1882 }
1883}
1884
1885/**
1886 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1887 *
1888 * @handle: transport handle pointing to adapter structure
1889 *
1890 * Driver checks if adapter is already in shutdown mode, if not start
1891 * the shutdown process.
1892 */
1893static void bnx2fc_ulp_stop(void *handle)
1894{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001895 struct bnx2fc_hba *hba = handle;
1896 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001897
1898 printk(KERN_ERR "ULP_STOP\n");
1899
1900 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001901 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1902 goto exit;
1903 list_for_each_entry(interface, &if_list, list) {
1904 if (interface->hba == hba)
1905 bnx2fc_stop(interface);
1906 }
1907 BUG_ON(hba->num_ofld_sess != 0);
1908
1909 mutex_lock(&hba->hba_mutex);
1910 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1911 clear_bit(ADAPTER_STATE_GOING_DOWN,
1912 &hba->adapter_state);
1913
1914 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1915 mutex_unlock(&hba->hba_mutex);
1916
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001917 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001918exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001919 mutex_unlock(&bnx2fc_dev_lock);
1920}
1921
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001922static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001923{
Robert Lovefd8f8902012-05-22 19:06:16 -07001924 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001925 struct fc_lport *lport;
1926 int wait_cnt = 0;
1927
1928 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1929 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001930 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001931 printk(KERN_ERR PFX "Init not done yet\n");
1932 return;
1933 }
1934
Robert Lovefd8f8902012-05-22 19:06:16 -07001935 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001936 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1937
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001938 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001939 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001940 fcoe_ctlr_link_up(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001941 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001942 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001943 }
1944
1945 /* wait for the FCF to be selected before issuing FLOGI */
Robert Lovefd8f8902012-05-22 19:06:16 -07001946 while (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001947 msleep(250);
1948 /* give up after 3 secs */
1949 if (++wait_cnt > 12)
1950 break;
1951 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001952
1953 /* Reset max receive frame size to default */
1954 if (fc_set_mfs(lport, BNX2FC_MFS))
1955 return;
1956
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001957 fc_lport_init(lport);
1958 fc_fabric_login(lport);
1959}
1960
1961
1962/**
1963 * bnx2fc_ulp_init - Initialize an adapter instance
1964 *
1965 * @dev : cnic device handle
1966 * Called from cnic_register_driver() context to initialize all
1967 * enumerated cnic devices. This routine allocates adapter structure
1968 * and other device specific resources.
1969 */
1970static void bnx2fc_ulp_init(struct cnic_dev *dev)
1971{
1972 struct bnx2fc_hba *hba;
1973 int rc = 0;
1974
1975 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1976 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001977 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1978 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001979 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001980 " flags: %lx fcoe_conn: %d\n",
1981 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001982 return;
1983 }
1984
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001985 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001986 if (!hba) {
1987 printk(KERN_ERR PFX "hba initialization failed\n");
1988 return;
1989 }
1990
1991 /* Add HBA to the adapter list */
1992 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001993 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001994 adapter_count++;
1995 mutex_unlock(&bnx2fc_dev_lock);
1996
Barak Witkowski2e499d32012-06-26 01:31:19 +00001997 dev->fcoe_cap = &hba->fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001998 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1999 rc = dev->register_device(dev, CNIC_ULP_FCOE,
2000 (void *) hba);
2001 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07002002 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002003 else
2004 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2005}
2006
Eddie Wai06808102013-09-25 22:01:20 -07002007/* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2008static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2009{
2010 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2011
2012 if (interface->enabled == true) {
2013 if (!ctlr->lp) {
2014 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2015 return -ENODEV;
2016 } else {
2017 interface->enabled = false;
2018 fcoe_ctlr_link_down(ctlr);
2019 fcoe_clean_pending_queue(ctlr->lp);
2020 }
2021 }
2022 return 0;
2023}
2024
Robert Love6e89ea32012-11-27 06:53:40 +00002025/**
2026 * Deperecated: Use bnx2fc_enabled()
2027 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002028static int bnx2fc_disable(struct net_device *netdev)
2029{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002030 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002031 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002032 int rc = 0;
2033
Nithin Sujir6702ca12011-03-17 17:13:27 -07002034 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002035 mutex_lock(&bnx2fc_dev_lock);
2036
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002037 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002038 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002039
Eddie Wai06808102013-09-25 22:01:20 -07002040 if (!interface) {
2041 rc = -ENODEV;
2042 pr_err(PFX "bnx2fc_disable: interface not found\n");
2043 } else {
2044 rc = __bnx2fc_disable(ctlr);
2045 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002046 mutex_unlock(&bnx2fc_dev_lock);
2047 rtnl_unlock();
2048 return rc;
2049}
2050
Eddie Wai06808102013-09-25 22:01:20 -07002051static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2052{
2053 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2054
2055 if (interface->enabled == false) {
2056 if (!ctlr->lp) {
2057 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2058 return -ENODEV;
2059 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2060 fcoe_ctlr_link_up(ctlr);
2061 interface->enabled = true;
2062 }
2063 }
2064 return 0;
2065}
2066
Robert Love6e89ea32012-11-27 06:53:40 +00002067/**
2068 * Deprecated: Use bnx2fc_enabled()
2069 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002070static int bnx2fc_enable(struct net_device *netdev)
2071{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002072 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002073 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002074 int rc = 0;
2075
Nithin Sujir6702ca12011-03-17 17:13:27 -07002076 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002077 mutex_lock(&bnx2fc_dev_lock);
2078
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002079 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002080 ctlr = bnx2fc_to_ctlr(interface);
Eddie Wai06808102013-09-25 22:01:20 -07002081 if (!interface) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002082 rc = -ENODEV;
Eddie Wai06808102013-09-25 22:01:20 -07002083 pr_err(PFX "bnx2fc_enable: interface not found\n");
2084 } else {
2085 rc = __bnx2fc_enable(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002086 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002087
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002088 mutex_unlock(&bnx2fc_dev_lock);
2089 rtnl_unlock();
2090 return rc;
2091}
2092
2093/**
Robert Love6e89ea32012-11-27 06:53:40 +00002094 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2095 * @cdev: The FCoE Controller that is being enabled or disabled
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002096 *
Robert Love6e89ea32012-11-27 06:53:40 +00002097 * fcoe_sysfs will ensure that the state of 'enabled' has
2098 * changed, so no checking is necessary here. This routine simply
2099 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2100 * When those routines are removed the functionality can be merged
2101 * here.
2102 */
2103static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2104{
2105 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
Robert Love6e89ea32012-11-27 06:53:40 +00002106
2107 switch (cdev->enabled) {
2108 case FCOE_CTLR_ENABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002109 return __bnx2fc_enable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002110 case FCOE_CTLR_DISABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002111 return __bnx2fc_disable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002112 case FCOE_CTLR_UNUSED:
2113 default:
2114 return -ENOTSUPP;
2115 };
2116}
2117
2118enum bnx2fc_create_link_state {
2119 BNX2FC_CREATE_LINK_DOWN,
2120 BNX2FC_CREATE_LINK_UP,
2121};
2122
2123/**
2124 * _bnx2fc_create() - Create bnx2fc FCoE interface
2125 * @netdev : The net_device object the Ethernet interface to create on
2126 * @fip_mode: The FIP mode for this creation
2127 * @link_state: The ctlr link state on creation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002128 *
Robert Love6e89ea32012-11-27 06:53:40 +00002129 * Called from either the libfcoe 'create' module parameter
2130 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2131 *
2132 * libfcoe's 'create' module parameter is deprecated so some
2133 * consolidation of code can be done when that interface is
2134 * removed.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002135 *
2136 * Returns: 0 for success
2137 */
Robert Love6e89ea32012-11-27 06:53:40 +00002138static int _bnx2fc_create(struct net_device *netdev,
2139 enum fip_state fip_mode,
2140 enum bnx2fc_create_link_state link_state)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002141{
Robert Love6e89ea32012-11-27 06:53:40 +00002142 struct fcoe_ctlr_device *cdev;
Robert Lovefd8f8902012-05-22 19:06:16 -07002143 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002144 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002145 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002146 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002147 struct fc_lport *lport;
2148 struct ethtool_drvinfo drvinfo;
2149 int rc = 0;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002150 int vlan_id = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002151
2152 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2153 if (fip_mode != FIP_MODE_FABRIC) {
2154 printk(KERN_ERR "fip mode not FABRIC\n");
2155 return -EIO;
2156 }
2157
Nithin Sujir6702ca12011-03-17 17:13:27 -07002158 rtnl_lock();
2159
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002160 mutex_lock(&bnx2fc_dev_lock);
2161
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002162 if (!try_module_get(THIS_MODULE)) {
2163 rc = -EINVAL;
2164 goto mod_err;
2165 }
2166
2167 /* obtain physical netdev */
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002168 if (netdev->priv_flags & IFF_802_1Q_VLAN)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002169 phys_dev = vlan_dev_real_dev(netdev);
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002170
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002171 /* verify if the physical device is a netxtreme2 device */
2172 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2173 memset(&drvinfo, 0, sizeof(drvinfo));
2174 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002175 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002176 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2177 rc = -EINVAL;
2178 goto netdev_err;
2179 }
2180 } else {
2181 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2182 rc = -EINVAL;
2183 goto netdev_err;
2184 }
2185
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002186 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002187 hba = bnx2fc_hba_lookup(phys_dev);
2188 if (!hba) {
2189 rc = -ENODEV;
2190 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2191 goto netdev_err;
2192 }
2193
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002194 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002195 rc = -EEXIST;
2196 goto netdev_err;
2197 }
2198
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002199 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2200 if (!interface) {
2201 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002202 goto ifput_err;
2203 }
2204
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002205 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2206 vlan_id = vlan_dev_vlan_id(netdev);
2207 interface->vlan_enabled = 1;
2208 }
2209
Robert Lovefd8f8902012-05-22 19:06:16 -07002210 ctlr = bnx2fc_to_ctlr(interface);
Robert Love01bdcb62013-03-25 11:00:26 -07002211 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002212 interface->vlan_id = vlan_id;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002213
2214 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002215 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002216 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002217 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2218 rc = -EINVAL;
2219 goto ifput_err;
2220 }
2221
Robert Love01bdcb62013-03-25 11:00:26 -07002222 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002223 if (!lport) {
2224 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2225 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002226 rc = -EINVAL;
2227 goto if_create_err;
2228 }
2229
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002230 /* Add interface to if_list */
2231 list_add_tail(&interface->list, &if_list);
2232
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002233 lport->boot_time = jiffies;
2234
2235 /* Make this master N_port */
Robert Lovefd8f8902012-05-22 19:06:16 -07002236 ctlr->lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002237
Robert Love6e89ea32012-11-27 06:53:40 +00002238 if (link_state == BNX2FC_CREATE_LINK_UP)
2239 cdev->enabled = FCOE_CTLR_ENABLED;
2240 else
2241 cdev->enabled = FCOE_CTLR_DISABLED;
2242
2243 if (link_state == BNX2FC_CREATE_LINK_UP &&
2244 !bnx2fc_link_ok(lport)) {
Robert Lovefd8f8902012-05-22 19:06:16 -07002245 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002246 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2247 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2248 }
2249
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002250 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2251 bnx2fc_start_disc(interface);
Robert Love6e89ea32012-11-27 06:53:40 +00002252
2253 if (link_state == BNX2FC_CREATE_LINK_UP)
2254 interface->enabled = true;
2255
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002256 /*
2257 * Release from kref_init in bnx2fc_interface_setup, on success
2258 * lport should be holding a reference taken in bnx2fc_if_create
2259 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002260 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002261 /* put netdev that was held while calling dev_get_by_name */
2262 mutex_unlock(&bnx2fc_dev_lock);
2263 rtnl_unlock();
2264 return 0;
2265
2266if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002267 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002268ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002269 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002270 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002271 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002272netdev_err:
2273 module_put(THIS_MODULE);
2274mod_err:
2275 mutex_unlock(&bnx2fc_dev_lock);
2276 rtnl_unlock();
2277 return rc;
2278}
2279
2280/**
Robert Love6e89ea32012-11-27 06:53:40 +00002281 * bnx2fc_create() - Create a bnx2fc interface
2282 * @netdev : The net_device object the Ethernet interface to create on
2283 * @fip_mode: The FIP mode for this creation
2284 *
2285 * Called from fcoe transport
2286 *
2287 * Returns: 0 for success
2288 */
2289static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2290{
2291 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2292}
2293
2294/**
2295 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2296 * @netdev: The net_device to be used by the allocated FCoE Controller
2297 *
2298 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2299 * in a link_down state. The allows the user an opportunity to configure
2300 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2301 *
2302 * Creating in with this routine starts the FCoE Controller in Fabric
2303 * mode. The user can change to VN2VN or another mode before enabling.
2304 */
2305static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2306{
2307 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2308 BNX2FC_CREATE_LINK_DOWN);
2309}
2310
2311/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002312 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002313 *
2314 * @cnic: Pointer to cnic device instance
2315 *
2316 **/
2317static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2318{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002319 struct bnx2fc_hba *hba;
2320
2321 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -07002322 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002323 if (hba->cnic == cnic)
2324 return hba;
2325 }
2326 return NULL;
2327}
2328
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002329static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2330 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002331{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002332 struct bnx2fc_interface *interface;
2333
2334 /* Called with bnx2fc_dev_lock held */
2335 list_for_each_entry(interface, &if_list, list) {
2336 if (interface->netdev == netdev)
2337 return interface;
2338 }
2339 return NULL;
2340}
2341
2342static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2343 *phys_dev)
2344{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002345 struct bnx2fc_hba *hba;
2346
2347 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002348 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002349 if (hba->phys_dev == phys_dev)
2350 return hba;
2351 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002352 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002353 return NULL;
2354}
2355
2356/**
2357 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2358 *
2359 * @dev cnic device handle
2360 */
2361static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2362{
2363 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002364 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002365
2366 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2367
2368 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2369 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2370 dev->netdev->name, dev->flags);
2371 return;
2372 }
2373
2374 mutex_lock(&bnx2fc_dev_lock);
2375 hba = bnx2fc_find_hba_for_cnic(dev);
2376 if (!hba) {
2377 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2378 dev);
2379 mutex_unlock(&bnx2fc_dev_lock);
2380 return;
2381 }
2382
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002383 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002384 adapter_count--;
2385
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002386 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002387 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002388 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002389 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002390 mutex_unlock(&bnx2fc_dev_lock);
2391
2392 bnx2fc_ulp_stop(hba);
2393 /* unregister cnic device */
2394 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2395 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002396 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002397}
2398
2399/**
2400 * bnx2fc_fcoe_reset - Resets the fcoe
2401 *
2402 * @shost: shost the reset is from
2403 *
2404 * Returns: always 0
2405 */
2406static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2407{
2408 struct fc_lport *lport = shost_priv(shost);
2409 fc_lport_reset(lport);
2410 return 0;
2411}
2412
2413
2414static bool bnx2fc_match(struct net_device *netdev)
2415{
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002416 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002417
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002418 mutex_lock(&bnx2fc_dev_lock);
2419 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2420 phys_dev = vlan_dev_real_dev(netdev);
2421
2422 if (bnx2fc_hba_lookup(phys_dev)) {
2423 mutex_unlock(&bnx2fc_dev_lock);
2424 return true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002425 }
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002426
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002427 mutex_unlock(&bnx2fc_dev_lock);
2428 return false;
2429}
2430
2431
2432static struct fcoe_transport bnx2fc_transport = {
2433 .name = {"bnx2fc"},
2434 .attached = false,
2435 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
Robert Love6e89ea32012-11-27 06:53:40 +00002436 .alloc = bnx2fc_ctlr_alloc,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002437 .match = bnx2fc_match,
2438 .create = bnx2fc_create,
2439 .destroy = bnx2fc_destroy,
2440 .enable = bnx2fc_enable,
2441 .disable = bnx2fc_disable,
2442};
2443
2444/**
2445 * bnx2fc_percpu_thread_create - Create a receive thread for an
2446 * online CPU
2447 *
2448 * @cpu: cpu index for the online cpu
2449 */
2450static void bnx2fc_percpu_thread_create(unsigned int cpu)
2451{
2452 struct bnx2fc_percpu_s *p;
2453 struct task_struct *thread;
2454
2455 p = &per_cpu(bnx2fc_percpu, cpu);
2456
Eric Dumazet69614272012-06-04 16:15:42 -07002457 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2458 (void *)p, cpu_to_node(cpu),
2459 "bnx2fc_thread/%d", cpu);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002460 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002461 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002462 kthread_bind(thread, cpu);
2463 p->iothread = thread;
2464 wake_up_process(thread);
2465 }
2466}
2467
2468static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2469{
2470 struct bnx2fc_percpu_s *p;
2471 struct task_struct *thread;
2472 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002473
2474 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2475
2476 /* Prevent any new work from being queued for this CPU */
2477 p = &per_cpu(bnx2fc_percpu, cpu);
2478 spin_lock_bh(&p->fp_work_lock);
2479 thread = p->iothread;
2480 p->iothread = NULL;
2481
2482
2483 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002484 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002485 list_del_init(&work->list);
2486 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2487 kfree(work);
2488 }
2489
2490 spin_unlock_bh(&p->fp_work_lock);
2491
2492 if (thread)
2493 kthread_stop(thread);
2494}
2495
2496/**
2497 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2498 *
2499 * @nfb: The callback data block
2500 * @action: The event triggering the callback
2501 * @hcpu: The index of the CPU that the event is for
2502 *
2503 * This creates or destroys per-CPU data for fcoe
2504 *
2505 * Returns NOTIFY_OK always.
2506 */
2507static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2508 unsigned long action, void *hcpu)
2509{
2510 unsigned cpu = (unsigned long)hcpu;
2511
2512 switch (action) {
2513 case CPU_ONLINE:
2514 case CPU_ONLINE_FROZEN:
2515 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2516 bnx2fc_percpu_thread_create(cpu);
2517 break;
2518 case CPU_DEAD:
2519 case CPU_DEAD_FROZEN:
2520 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2521 bnx2fc_percpu_thread_destroy(cpu);
2522 break;
2523 default:
2524 break;
2525 }
2526 return NOTIFY_OK;
2527}
2528
2529/**
2530 * bnx2fc_mod_init - module init entry point
2531 *
2532 * Initialize driver wide global data structures, and register
2533 * with cnic module
2534 **/
2535static int __init bnx2fc_mod_init(void)
2536{
2537 struct fcoe_percpu_s *bg;
2538 struct task_struct *l2_thread;
2539 int rc = 0;
2540 unsigned int cpu = 0;
2541 struct bnx2fc_percpu_s *p;
2542
2543 printk(KERN_INFO PFX "%s", version);
2544
2545 /* register as a fcoe transport */
2546 rc = fcoe_transport_attach(&bnx2fc_transport);
2547 if (rc) {
2548 printk(KERN_ERR "failed to register an fcoe transport, check "
2549 "if libfcoe is loaded\n");
2550 goto out;
2551 }
2552
2553 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002554 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002555 mutex_init(&bnx2fc_dev_lock);
2556 adapter_count = 0;
2557
2558 /* Attach FC transport template */
2559 rc = bnx2fc_attach_transport();
2560 if (rc)
2561 goto detach_ft;
2562
2563 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2564 if (!bnx2fc_wq) {
2565 rc = -ENOMEM;
2566 goto release_bt;
2567 }
2568
2569 bg = &bnx2fc_global;
2570 skb_queue_head_init(&bg->fcoe_rx_list);
2571 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2572 (void *)bg,
2573 "bnx2fc_l2_thread");
2574 if (IS_ERR(l2_thread)) {
2575 rc = PTR_ERR(l2_thread);
2576 goto free_wq;
2577 }
2578 wake_up_process(l2_thread);
2579 spin_lock_bh(&bg->fcoe_rx_list.lock);
2580 bg->thread = l2_thread;
2581 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2582
2583 for_each_possible_cpu(cpu) {
2584 p = &per_cpu(bnx2fc_percpu, cpu);
2585 INIT_LIST_HEAD(&p->work_list);
2586 spin_lock_init(&p->fp_work_lock);
2587 }
2588
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302589 cpu_notifier_register_begin();
2590
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002591 for_each_online_cpu(cpu) {
2592 bnx2fc_percpu_thread_create(cpu);
2593 }
2594
2595 /* Initialize per CPU interrupt thread */
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302596 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2597
2598 cpu_notifier_register_done();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002599
2600 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2601
2602 return 0;
2603
2604free_wq:
2605 destroy_workqueue(bnx2fc_wq);
2606release_bt:
2607 bnx2fc_release_transport();
2608detach_ft:
2609 fcoe_transport_detach(&bnx2fc_transport);
2610out:
2611 return rc;
2612}
2613
2614static void __exit bnx2fc_mod_exit(void)
2615{
2616 LIST_HEAD(to_be_deleted);
2617 struct bnx2fc_hba *hba, *next;
2618 struct fcoe_percpu_s *bg;
2619 struct task_struct *l2_thread;
2620 struct sk_buff *skb;
2621 unsigned int cpu = 0;
2622
2623 /*
2624 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2625 * it will have higher precedence than bnx2fc_dev_lock.
2626 * unregister_device() cannot be called with bnx2fc_dev_lock
2627 * held.
2628 */
2629 mutex_lock(&bnx2fc_dev_lock);
2630 list_splice(&adapter_list, &to_be_deleted);
2631 INIT_LIST_HEAD(&adapter_list);
2632 adapter_count = 0;
2633 mutex_unlock(&bnx2fc_dev_lock);
2634
2635 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002636 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2637 list_del_init(&hba->list);
2638 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2639 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002640 bnx2fc_ulp_stop(hba);
2641 /* unregister cnic device */
2642 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2643 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002644 hba->cnic->unregister_device(hba->cnic,
2645 CNIC_ULP_FCOE);
2646 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002647 }
2648 cnic_unregister_driver(CNIC_ULP_FCOE);
2649
2650 /* Destroy global thread */
2651 bg = &bnx2fc_global;
2652 spin_lock_bh(&bg->fcoe_rx_list.lock);
2653 l2_thread = bg->thread;
2654 bg->thread = NULL;
2655 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2656 kfree_skb(skb);
2657
2658 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2659
2660 if (l2_thread)
2661 kthread_stop(l2_thread);
2662
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302663 cpu_notifier_register_begin();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002664
2665 /* Destroy per cpu threads */
2666 for_each_online_cpu(cpu) {
2667 bnx2fc_percpu_thread_destroy(cpu);
2668 }
2669
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302670 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2671
2672 cpu_notifier_register_done();
2673
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002674 destroy_workqueue(bnx2fc_wq);
2675 /*
2676 * detach from scsi transport
2677 * must happen after all destroys are done
2678 */
2679 bnx2fc_release_transport();
2680
2681 /* detach from fcoe transport */
2682 fcoe_transport_detach(&bnx2fc_transport);
2683}
2684
2685module_init(bnx2fc_mod_init);
2686module_exit(bnx2fc_mod_exit);
2687
Robert Love8d55e502012-05-22 19:06:26 -07002688static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
Robert Love6e89ea32012-11-27 06:53:40 +00002689 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
Yi Zoub8b3e692012-12-06 06:24:59 +00002690 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2691 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2692 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2693 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2694 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2695 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
Robert Love8d55e502012-05-22 19:06:26 -07002696
2697 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2698 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2699};
2700
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002701static struct fc_function_template bnx2fc_transport_function = {
2702 .show_host_node_name = 1,
2703 .show_host_port_name = 1,
2704 .show_host_supported_classes = 1,
2705 .show_host_supported_fc4s = 1,
2706 .show_host_active_fc4s = 1,
2707 .show_host_maxframe_size = 1,
2708
2709 .show_host_port_id = 1,
2710 .show_host_supported_speeds = 1,
2711 .get_host_speed = fc_get_host_speed,
2712 .show_host_speed = 1,
2713 .show_host_port_type = 1,
2714 .get_host_port_state = fc_get_host_port_state,
2715 .show_host_port_state = 1,
2716 .show_host_symbolic_name = 1,
2717
2718 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2719 sizeof(struct bnx2fc_rport)),
2720 .show_rport_maxframe_size = 1,
2721 .show_rport_supported_classes = 1,
2722
2723 .show_host_fabric_name = 1,
2724 .show_starget_node_name = 1,
2725 .show_starget_port_name = 1,
2726 .show_starget_port_id = 1,
2727 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2728 .show_rport_dev_loss_tmo = 1,
2729 .get_fc_host_stats = bnx2fc_get_host_stats,
2730
2731 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2732
2733 .terminate_rport_io = fc_rport_terminate_io,
2734
2735 .vport_create = bnx2fc_vport_create,
2736 .vport_delete = bnx2fc_vport_destroy,
2737 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002738 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002739};
2740
2741static struct fc_function_template bnx2fc_vport_xport_function = {
2742 .show_host_node_name = 1,
2743 .show_host_port_name = 1,
2744 .show_host_supported_classes = 1,
2745 .show_host_supported_fc4s = 1,
2746 .show_host_active_fc4s = 1,
2747 .show_host_maxframe_size = 1,
2748
2749 .show_host_port_id = 1,
2750 .show_host_supported_speeds = 1,
2751 .get_host_speed = fc_get_host_speed,
2752 .show_host_speed = 1,
2753 .show_host_port_type = 1,
2754 .get_host_port_state = fc_get_host_port_state,
2755 .show_host_port_state = 1,
2756 .show_host_symbolic_name = 1,
2757
2758 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2759 sizeof(struct bnx2fc_rport)),
2760 .show_rport_maxframe_size = 1,
2761 .show_rport_supported_classes = 1,
2762
2763 .show_host_fabric_name = 1,
2764 .show_starget_node_name = 1,
2765 .show_starget_port_name = 1,
2766 .show_starget_port_id = 1,
2767 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2768 .show_rport_dev_loss_tmo = 1,
2769 .get_fc_host_stats = fc_get_host_stats,
2770 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2771 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002772 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002773};
2774
2775/**
2776 * scsi_host_template structure used while registering with SCSI-ml
2777 */
2778static struct scsi_host_template bnx2fc_shost_template = {
2779 .module = THIS_MODULE,
2780 .name = "Broadcom Offload FCoE Initiator",
2781 .queuecommand = bnx2fc_queuecommand,
2782 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2783 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2784 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2785 .eh_host_reset_handler = fc_eh_host_reset,
2786 .slave_alloc = fc_slave_alloc,
2787 .change_queue_depth = fc_change_queue_depth,
2788 .change_queue_type = fc_change_queue_type,
2789 .this_id = -1,
2790 .cmd_per_lun = 3,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002791 .use_clustering = ENABLE_CLUSTERING,
2792 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
Bhanu Prakash Gollapudid450d772012-12-21 19:40:33 -08002793 .max_sectors = 1024,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002794};
2795
2796static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2797 .frame_send = bnx2fc_xmit,
2798 .elsct_send = bnx2fc_elsct_send,
2799 .fcp_abort_io = bnx2fc_abort_io,
2800 .fcp_cleanup = bnx2fc_cleanup,
Yi Zoub8b3e692012-12-06 06:24:59 +00002801 .get_lesb = fcoe_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002802 .rport_event_callback = bnx2fc_rport_event_handler,
2803};
2804
2805/**
2806 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2807 * structure carrying callback function pointers
2808 */
2809static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2810 .owner = THIS_MODULE,
2811 .cnic_init = bnx2fc_ulp_init,
2812 .cnic_exit = bnx2fc_ulp_exit,
2813 .cnic_start = bnx2fc_ulp_start,
2814 .cnic_stop = bnx2fc_ulp_stop,
2815 .indicate_kcqes = bnx2fc_indicate_kcqe,
2816 .indicate_netevent = bnx2fc_indicate_netevent,
Barak Witkowski2e499d32012-06-26 01:31:19 +00002817 .cnic_get_stats = bnx2fc_ulp_get_stats,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002818};