blob: cb9bf364170bdbb56050bc2ec520aca067e9801d [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 Gollapudi9b35baa2011-07-27 11:32:13 -07006 * Copyright (c) 2008 - 2011 Broadcom Corporation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 *
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13 */
14
15#include "bnx2fc.h"
16
17static struct list_head adapter_list;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070018static struct list_head if_list;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080019static u32 adapter_count;
20static DEFINE_MUTEX(bnx2fc_dev_lock);
21DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22
23#define DRV_MODULE_NAME "bnx2fc"
24#define DRV_MODULE_VERSION BNX2FC_VERSION
Bhanu Prakash Gollapudie91506d2012-04-24 15:26:04 -070025#define DRV_MODULE_RELDATE "Apr 24, 2012"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080026
27
28static char version[] __devinitdata =
29 "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;
57static struct fc_function_template bnx2fc_vport_xport_function;
58static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -070059static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080060static int bnx2fc_destroy(struct net_device *net_device);
61static int bnx2fc_enable(struct net_device *netdev);
62static int bnx2fc_disable(struct net_device *netdev);
63
64static void bnx2fc_recv_frame(struct sk_buff *skb);
65
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070066static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080067static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080068static int bnx2fc_lport_config(struct fc_lport *lport);
69static int bnx2fc_em_config(struct fc_lport *lport);
70static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
71static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
72static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
73static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070074static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080075 struct device *parent, int npiv);
76static void bnx2fc_destroy_work(struct work_struct *work);
77
78static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070079static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
80 *phys_dev);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -070081static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080082static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
83
84static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
85static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
86
87static void bnx2fc_port_shutdown(struct fc_lport *lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070088static void bnx2fc_stop(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080089static int __init bnx2fc_mod_init(void);
90static void __exit bnx2fc_mod_exit(void);
91
92unsigned int bnx2fc_debug_level;
93module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
94
95static int bnx2fc_cpu_callback(struct notifier_block *nfb,
96 unsigned long action, void *hcpu);
97/* notification function for CPU hotplug events */
98static struct notifier_block bnx2fc_cpu_notifier = {
99 .notifier_call = bnx2fc_cpu_callback,
100};
101
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -0700102static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
103{
104 return ((struct bnx2fc_interface *)
105 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
106}
107
108/**
109 * bnx2fc_get_lesb() - Fill the FCoE Link Error Status Block
110 * @lport: the local port
111 * @fc_lesb: the link error status block
112 */
113static void bnx2fc_get_lesb(struct fc_lport *lport,
114 struct fc_els_lesb *fc_lesb)
115{
116 struct net_device *netdev = bnx2fc_netdev(lport);
117
118 __fcoe_get_lesb(lport, fc_lesb, netdev);
119}
120
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800121static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
122{
123 struct fcoe_percpu_s *bg;
124 struct fcoe_rcv_info *fr;
125 struct sk_buff_head *list;
126 struct sk_buff *skb, *next;
127 struct sk_buff *head;
128
129 bg = &bnx2fc_global;
130 spin_lock_bh(&bg->fcoe_rx_list.lock);
131 list = &bg->fcoe_rx_list;
132 head = list->next;
133 for (skb = head; skb != (struct sk_buff *)list;
134 skb = next) {
135 next = skb->next;
136 fr = fcoe_dev_from_skb(skb);
137 if (fr->fr_dev == lp) {
138 __skb_unlink(skb, list);
139 kfree_skb(skb);
140 }
141 }
142 spin_unlock_bh(&bg->fcoe_rx_list.lock);
143}
144
145int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
146{
147 int rc;
148 spin_lock(&bnx2fc_global_lock);
149 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
150 spin_unlock(&bnx2fc_global_lock);
151
152 return rc;
153}
154
155static void bnx2fc_abort_io(struct fc_lport *lport)
156{
157 /*
158 * This function is no-op for bnx2fc, but we do
159 * not want to leave it as NULL either, as libfc
160 * can call the default function which is
161 * fc_fcp_abort_io.
162 */
163}
164
165static void bnx2fc_cleanup(struct fc_lport *lport)
166{
167 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700168 struct bnx2fc_interface *interface = port->priv;
169 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800170 struct bnx2fc_rport *tgt;
171 int i;
172
173 BNX2FC_MISC_DBG("Entered %s\n", __func__);
174 mutex_lock(&hba->hba_mutex);
175 spin_lock_bh(&hba->hba_lock);
176 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
177 tgt = hba->tgt_ofld_list[i];
178 if (tgt) {
179 /* Cleanup IOs belonging to requested vport */
180 if (tgt->port == port) {
181 spin_unlock_bh(&hba->hba_lock);
182 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
183 bnx2fc_flush_active_ios(tgt);
184 spin_lock_bh(&hba->hba_lock);
185 }
186 }
187 }
188 spin_unlock_bh(&hba->hba_lock);
189 mutex_unlock(&hba->hba_mutex);
190}
191
192static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
193 struct fc_frame *fp)
194{
195 struct fc_rport_priv *rdata = tgt->rdata;
196 struct fc_frame_header *fh;
197 int rc = 0;
198
199 fh = fc_frame_header_get(fp);
200 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
201 "r_ctl = 0x%x\n", rdata->ids.port_id,
202 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
203 if ((fh->fh_type == FC_TYPE_ELS) &&
204 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
205
206 switch (fc_frame_payload_op(fp)) {
207 case ELS_ADISC:
208 rc = bnx2fc_send_adisc(tgt, fp);
209 break;
210 case ELS_LOGO:
211 rc = bnx2fc_send_logo(tgt, fp);
212 break;
213 case ELS_RLS:
214 rc = bnx2fc_send_rls(tgt, fp);
215 break;
216 default:
217 break;
218 }
219 } else if ((fh->fh_type == FC_TYPE_BLS) &&
220 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
221 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
222 else {
223 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
224 "rctl 0x%x thru non-offload path\n",
225 fh->fh_type, fh->fh_r_ctl);
226 return -ENODEV;
227 }
228 if (rc)
229 return -ENOMEM;
230 else
231 return 0;
232}
233
234/**
235 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
236 *
237 * @lport: the associated local port
238 * @fp: the fc_frame to be transmitted
239 */
240static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
241{
242 struct ethhdr *eh;
243 struct fcoe_crc_eof *cp;
244 struct sk_buff *skb;
245 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700246 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700247 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700248 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800249 struct fcoe_port *port;
250 struct fcoe_hdr *hp;
251 struct bnx2fc_rport *tgt;
252 struct fcoe_dev_stats *stats;
253 u8 sof, eof;
254 u32 crc;
255 unsigned int hlen, tlen, elen;
256 int wlen, rc = 0;
257
258 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700259 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700260 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700261 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800262
263 fh = fc_frame_header_get(fp);
264
265 skb = fp_skb(fp);
266 if (!lport->link_up) {
267 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
268 kfree_skb(skb);
269 return 0;
270 }
271
272 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Robert Lovefd8f8902012-05-22 19:06:16 -0700273 if (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800274 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
275 kfree_skb(skb);
276 return -EINVAL;
277 }
Robert Lovefd8f8902012-05-22 19:06:16 -0700278 if (fcoe_ctlr_els_send(ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800279 return 0;
280 }
281
282 sof = fr_sof(fp);
283 eof = fr_eof(fp);
284
285 /*
286 * Snoop the frame header to check if the frame is for
287 * an offloaded session
288 */
289 /*
290 * tgt_ofld_list access is synchronized using
291 * both hba mutex and hba lock. Atleast hba mutex or
292 * hba lock needs to be held for read access.
293 */
294
295 spin_lock_bh(&hba->hba_lock);
296 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
297 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
298 /* This frame is for offloaded session */
299 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
300 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
301 spin_unlock_bh(&hba->hba_lock);
302 rc = bnx2fc_xmit_l2_frame(tgt, fp);
303 if (rc != -ENODEV) {
304 kfree_skb(skb);
305 return rc;
306 }
307 } else {
308 spin_unlock_bh(&hba->hba_lock);
309 }
310
311 elen = sizeof(struct ethhdr);
312 hlen = sizeof(struct fcoe_hdr);
313 tlen = sizeof(struct fcoe_crc_eof);
314 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
315
316 skb->ip_summed = CHECKSUM_NONE;
317 crc = fcoe_fc_crc(fp);
318
319 /* copy port crc and eof to the skb buff */
320 if (skb_is_nonlinear(skb)) {
321 skb_frag_t *frag;
322 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
323 kfree_skb(skb);
324 return -ENOMEM;
325 }
326 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800327 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800328 } else {
329 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
330 }
331
332 memset(cp, 0, sizeof(*cp));
333 cp->fcoe_eof = eof;
334 cp->fcoe_crc32 = cpu_to_le32(~crc);
335 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800336 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800337 cp = NULL;
338 }
339
340 /* adjust skb network/transport offsets to match mac/fcoe/port */
341 skb_push(skb, elen + hlen);
342 skb_reset_mac_header(skb);
343 skb_reset_network_header(skb);
344 skb->mac_len = elen;
345 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700346 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800347
348 /* fill up mac and fcoe headers */
349 eh = eth_hdr(skb);
350 eh->h_proto = htons(ETH_P_FCOE);
Robert Lovefd8f8902012-05-22 19:06:16 -0700351 if (ctlr->map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800352 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
353 else
354 /* insert GW address */
Robert Lovefd8f8902012-05-22 19:06:16 -0700355 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800356
Robert Lovefd8f8902012-05-22 19:06:16 -0700357 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
358 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800359 else
360 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
361
362 hp = (struct fcoe_hdr *)(eh + 1);
363 memset(hp, 0, sizeof(*hp));
364 if (FC_FCOE_VER)
365 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
366 hp->fcoe_sof = sof;
367
368 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
369 if (lport->seq_offload && fr_max_payload(fp)) {
370 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
371 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
372 } else {
373 skb_shinfo(skb)->gso_type = 0;
374 skb_shinfo(skb)->gso_size = 0;
375 }
376
377 /*update tx stats */
378 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
379 stats->TxFrames++;
380 stats->TxWords += wlen;
381 put_cpu();
382
383 /* send down to lld */
384 fr_dev(fp) = lport;
385 if (port->fcoe_pending_queue.qlen)
386 fcoe_check_wait_queue(lport, skb);
387 else if (fcoe_start_io(skb))
388 fcoe_check_wait_queue(lport, skb);
389
390 return 0;
391}
392
393/**
394 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
395 *
396 * @skb: the receive socket buffer
397 * @dev: associated net device
398 * @ptype: context
399 * @olddev: last device
400 *
401 * This function receives the packet and builds FC frame and passes it up
402 */
403static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
404 struct packet_type *ptype, struct net_device *olddev)
405{
406 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700407 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700408 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800409 struct fc_frame_header *fh;
410 struct fcoe_rcv_info *fr;
411 struct fcoe_percpu_s *bg;
412 unsigned short oxid;
413
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700414 interface = container_of(ptype, struct bnx2fc_interface,
415 fcoe_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -0700416 ctlr = bnx2fc_to_ctlr(interface);
417 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800418
419 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700420 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800421 goto err;
422 }
423
424 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700425 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800426 goto err;
427 }
428
429 /*
430 * Check for minimum frame length, and make sure required FCoE
431 * and FC headers are pulled into the linear data area.
432 */
433 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
434 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
435 goto err;
436
437 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
438 fh = (struct fc_frame_header *) skb_transport_header(skb);
439
440 oxid = ntohs(fh->fh_ox_id);
441
442 fr = fcoe_dev_from_skb(skb);
443 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800444
445 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800446 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800447
448 __skb_queue_tail(&bg->fcoe_rx_list, skb);
449 if (bg->fcoe_rx_list.qlen == 1)
450 wake_up_process(bg->thread);
451
Neil Hormanfc05ab72012-03-09 14:50:13 -0800452 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800453
454 return 0;
455err:
456 kfree_skb(skb);
457 return -1;
458}
459
460static int bnx2fc_l2_rcv_thread(void *arg)
461{
462 struct fcoe_percpu_s *bg = arg;
463 struct sk_buff *skb;
464
465 set_user_nice(current, -20);
466 set_current_state(TASK_INTERRUPTIBLE);
467 while (!kthread_should_stop()) {
468 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800469 spin_lock_bh(&bg->fcoe_rx_list.lock);
470 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
471 spin_unlock_bh(&bg->fcoe_rx_list.lock);
472 bnx2fc_recv_frame(skb);
473 spin_lock_bh(&bg->fcoe_rx_list.lock);
474 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700475 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800476 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800477 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700478 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800479 return 0;
480}
481
482
483static void bnx2fc_recv_frame(struct sk_buff *skb)
484{
485 u32 fr_len;
486 struct fc_lport *lport;
487 struct fcoe_rcv_info *fr;
488 struct fcoe_dev_stats *stats;
489 struct fc_frame_header *fh;
490 struct fcoe_crc_eof crc_eof;
491 struct fc_frame *fp;
492 struct fc_lport *vn_port;
493 struct fcoe_port *port;
494 u8 *mac = NULL;
495 u8 *dest_mac = NULL;
496 struct fcoe_hdr *hp;
497
498 fr = fcoe_dev_from_skb(skb);
499 lport = fr->fr_dev;
500 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700501 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800502 kfree_skb(skb);
503 return;
504 }
505
506 if (skb_is_nonlinear(skb))
507 skb_linearize(skb);
508 mac = eth_hdr(skb)->h_source;
509 dest_mac = eth_hdr(skb)->h_dest;
510
511 /* Pull the header */
512 hp = (struct fcoe_hdr *) skb_network_header(skb);
513 fh = (struct fc_frame_header *) skb_transport_header(skb);
514 skb_pull(skb, sizeof(struct fcoe_hdr));
515 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
516
517 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
518 stats->RxFrames++;
519 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
520
521 fp = (struct fc_frame *)skb;
522 fc_frame_init(fp);
523 fr_dev(fp) = lport;
524 fr_sof(fp) = hp->fcoe_sof;
525 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
526 put_cpu();
527 kfree_skb(skb);
528 return;
529 }
530 fr_eof(fp) = crc_eof.fcoe_eof;
531 fr_crc(fp) = crc_eof.fcoe_crc32;
532 if (pskb_trim(skb, fr_len)) {
533 put_cpu();
534 kfree_skb(skb);
535 return;
536 }
537
538 fh = fc_frame_header_get(fp);
539
540 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
541 if (vn_port) {
542 port = lport_priv(vn_port);
543 if (compare_ether_addr(port->data_src_addr, dest_mac)
544 != 0) {
545 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
546 put_cpu();
547 kfree_skb(skb);
548 return;
549 }
550 }
551 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
552 fh->fh_type == FC_TYPE_FCP) {
553 /* Drop FCP data. We dont this in L2 path */
554 put_cpu();
555 kfree_skb(skb);
556 return;
557 }
558 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
559 fh->fh_type == FC_TYPE_ELS) {
560 switch (fc_frame_payload_op(fp)) {
561 case ELS_LOGO:
562 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
563 /* drop non-FIP LOGO */
564 put_cpu();
565 kfree_skb(skb);
566 return;
567 }
568 break;
569 }
570 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700571
572 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
573 /* Drop incoming ABTS */
574 put_cpu();
575 kfree_skb(skb);
576 return;
577 }
578
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800579 if (le32_to_cpu(fr_crc(fp)) !=
580 ~crc32(~0, skb->data, fr_len)) {
581 if (stats->InvalidCRCCount < 5)
582 printk(KERN_WARNING PFX "dropping frame with "
583 "CRC error\n");
584 stats->InvalidCRCCount++;
585 put_cpu();
586 kfree_skb(skb);
587 return;
588 }
589 put_cpu();
590 fc_exch_recv(lport, fp);
591}
592
593/**
594 * bnx2fc_percpu_io_thread - thread per cpu for ios
595 *
596 * @arg: ptr to bnx2fc_percpu_info structure
597 */
598int bnx2fc_percpu_io_thread(void *arg)
599{
600 struct bnx2fc_percpu_s *p = arg;
601 struct bnx2fc_work *work, *tmp;
602 LIST_HEAD(work_list);
603
604 set_user_nice(current, -20);
605 set_current_state(TASK_INTERRUPTIBLE);
606 while (!kthread_should_stop()) {
607 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800608 spin_lock_bh(&p->fp_work_lock);
609 while (!list_empty(&p->work_list)) {
610 list_splice_init(&p->work_list, &work_list);
611 spin_unlock_bh(&p->fp_work_lock);
612
613 list_for_each_entry_safe(work, tmp, &work_list, list) {
614 list_del_init(&work->list);
615 bnx2fc_process_cq_compl(work->tgt, work->wqe);
616 kfree(work);
617 }
618
619 spin_lock_bh(&p->fp_work_lock);
620 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700621 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800622 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800623 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700624 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800625
626 return 0;
627}
628
629static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
630{
631 struct fc_host_statistics *bnx2fc_stats;
632 struct fc_lport *lport = shost_priv(shost);
633 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700634 struct bnx2fc_interface *interface = port->priv;
635 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800636 struct fcoe_statistics_params *fw_stats;
637 int rc = 0;
638
639 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
640 if (!fw_stats)
641 return NULL;
642
643 bnx2fc_stats = fc_get_host_stats(shost);
644
645 init_completion(&hba->stat_req_done);
646 if (bnx2fc_send_stat_req(hba))
647 return bnx2fc_stats;
648 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
649 if (!rc) {
650 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
651 return bnx2fc_stats;
652 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300653 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800654 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
655 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
656 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
657 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
658
659 bnx2fc_stats->dumped_frames = 0;
660 bnx2fc_stats->lip_count = 0;
661 bnx2fc_stats->nos_count = 0;
662 bnx2fc_stats->loss_of_sync_count = 0;
663 bnx2fc_stats->loss_of_signal_count = 0;
664 bnx2fc_stats->prim_seq_protocol_err_count = 0;
665
666 return bnx2fc_stats;
667}
668
669static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
670{
671 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700672 struct bnx2fc_interface *interface = port->priv;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800673 struct Scsi_Host *shost = lport->host;
674 int rc = 0;
675
676 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
677 shost->max_lun = BNX2FC_MAX_LUN;
678 shost->max_id = BNX2FC_MAX_FCP_TGT;
679 shost->max_channel = 0;
680 if (lport->vport)
681 shost->transportt = bnx2fc_vport_xport_template;
682 else
683 shost->transportt = bnx2fc_transport_template;
684
685 /* Add the new host to SCSI-ml */
686 rc = scsi_add_host(lport->host, dev);
687 if (rc) {
688 printk(KERN_ERR PFX "Error on scsi_add_host\n");
689 return rc;
690 }
691 if (!lport->vport)
692 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
693 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
694 BNX2FC_NAME, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700695 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800696
697 return 0;
698}
699
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800700static void bnx2fc_link_speed_update(struct fc_lport *lport)
701{
702 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700703 struct bnx2fc_interface *interface = port->priv;
704 struct net_device *netdev = interface->netdev;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000705 struct ethtool_cmd ecmd;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800706
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000707 if (!__ethtool_get_settings(netdev, &ecmd)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800708 lport->link_supported_speeds &=
709 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
710 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
711 SUPPORTED_1000baseT_Full))
712 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
713 if (ecmd.supported & SUPPORTED_10000baseT_Full)
714 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
715
David Decotigny8ae6dac2011-04-27 18:32:38 +0000716 switch (ethtool_cmd_speed(&ecmd)) {
717 case SPEED_1000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800718 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000719 break;
Bhanu Prakash Gollapudia4dc08c2011-05-27 11:47:25 -0700720 case SPEED_2500:
721 lport->link_speed = FC_PORTSPEED_2GBIT;
722 break;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000723 case SPEED_10000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800724 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000725 break;
726 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800727 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800728}
729static int bnx2fc_link_ok(struct fc_lport *lport)
730{
731 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700732 struct bnx2fc_interface *interface = port->priv;
733 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800734 struct net_device *dev = hba->phys_dev;
735 int rc = 0;
736
737 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
738 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
739 else {
740 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
741 rc = -1;
742 }
743 return rc;
744}
745
746/**
747 * bnx2fc_get_link_state - get network link state
748 *
749 * @hba: adapter instance pointer
750 *
751 * updates adapter structure flag based on netdev state
752 */
753void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
754{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700755 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800756 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
757 else
758 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
759}
760
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700761static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800762{
763 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700764 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700765 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800766 struct fcoe_port *port;
767 u64 wwnn, wwpn;
768
769 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700770 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700771 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700772 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800773
774 /* require support for get_pauseparam ethtool op. */
775 if (!hba->phys_dev->ethtool_ops ||
776 !hba->phys_dev->ethtool_ops->get_pauseparam)
777 return -EOPNOTSUPP;
778
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700779 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800780 return -EINVAL;
781
782 skb_queue_head_init(&port->fcoe_pending_queue);
783 port->fcoe_pending_queue_active = 0;
784 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
785
786 bnx2fc_link_speed_update(lport);
787
788 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700789 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700790 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700791 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800792 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
793 fc_set_wwnn(lport, wwnn);
794
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700795 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700796 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700797 2, 0);
798
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800799 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
800 fc_set_wwpn(lport, wwpn);
801 }
802
803 return 0;
804}
805
806static void bnx2fc_destroy_timer(unsigned long data)
807{
808 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
809
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700810 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
811 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700812 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800813 wake_up_interruptible(&hba->destroy_wait);
814}
815
816/**
817 * bnx2fc_indicate_netevent - Generic netdev event handler
818 *
819 * @context: adapter structure pointer
820 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000821 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800822 *
823 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700824 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800825 */
Michael Chan415199f2011-07-20 14:55:24 +0000826static void bnx2fc_indicate_netevent(void *context, unsigned long event,
827 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800828{
829 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700830 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800831 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700832 struct bnx2fc_interface *interface, *tmp;
Robert Lovefd8f8902012-05-22 19:06:16 -0700833 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700834 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800835 u32 link_possible = 1;
836
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700837 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000838 return;
839
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800840 switch (event) {
841 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800842 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
843 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700844 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800845 break;
846
847 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800848 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
849 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
850 link_possible = 0;
851 break;
852
853 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800854 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
855 link_possible = 0;
856 break;
857
858 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800859 break;
860
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700861 case NETDEV_UNREGISTER:
862 if (!vlan_id)
863 return;
864 mutex_lock(&bnx2fc_dev_lock);
865 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700866 if (interface->hba == hba &&
867 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
868 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700869 }
870 mutex_unlock(&bnx2fc_dev_lock);
871 return;
872
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800873 default:
874 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
875 return;
876 }
877
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700878 mutex_lock(&bnx2fc_dev_lock);
879 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800880
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700881 if (interface->hba != hba)
882 continue;
883
Robert Lovefd8f8902012-05-22 19:06:16 -0700884 ctlr = bnx2fc_to_ctlr(interface);
885 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700886 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
887 interface->netdev->name, event);
888
889 bnx2fc_link_speed_update(lport);
890
891 if (link_possible && !bnx2fc_link_ok(lport)) {
Bhanu Prakash Gollapudic7806732011-08-30 15:54:47 -0700892 /* Reset max recv frame size to default */
893 fc_set_mfs(lport, BNX2FC_MFS);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -0700894 /*
895 * ctlr link up will only be handled during
896 * enable to avoid sending discovery solicitation
897 * on a stale vlan
898 */
Bhanu Prakash Gollapudifd8fa902011-10-03 16:45:00 -0700899 if (interface->enabled)
Robert Lovefd8f8902012-05-22 19:06:16 -0700900 fcoe_ctlr_link_up(ctlr);
901 } else if (fcoe_ctlr_link_down(ctlr)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800902 mutex_lock(&lport->lp_mutex);
903 list_for_each_entry(vport, &lport->vports, list)
904 fc_host_port_type(vport->host) =
905 FC_PORTTYPE_UNKNOWN;
906 mutex_unlock(&lport->lp_mutex);
907 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
908 per_cpu_ptr(lport->dev_stats,
909 get_cpu())->LinkFailureCount++;
910 put_cpu();
911 fcoe_clean_pending_queue(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700912 wait_for_upload = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800913 }
914 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700915 mutex_unlock(&bnx2fc_dev_lock);
916
917 if (wait_for_upload) {
918 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
919 init_waitqueue_head(&hba->shutdown_wait);
920 BNX2FC_MISC_DBG("indicate_netevent "
921 "num_ofld_sess = %d\n",
922 hba->num_ofld_sess);
923 hba->wait_for_link_down = 1;
924 wait_event_interruptible(hba->shutdown_wait,
925 (hba->num_ofld_sess == 0));
926 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
927 hba->num_ofld_sess);
928 hba->wait_for_link_down = 0;
929
930 if (signal_pending(current))
931 flush_signals(current);
932 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800933}
934
935static int bnx2fc_libfc_config(struct fc_lport *lport)
936{
937
938 /* Set the function pointers set by bnx2fc driver */
939 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
940 sizeof(struct libfc_function_template));
941 fc_elsct_init(lport);
942 fc_exch_init(lport);
943 fc_rport_init(lport);
944 fc_disc_init(lport);
945 return 0;
946}
947
948static int bnx2fc_em_config(struct fc_lport *lport)
949{
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800950 int max_xid;
951
952 if (nr_cpu_ids <= 2)
953 max_xid = FCOE_XIDS_PER_CPU;
954 else
955 max_xid = FCOE_MAX_XID;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800956 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800957 max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800958 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
959 return -ENOMEM;
960 }
961
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800962 return 0;
963}
964
965static int bnx2fc_lport_config(struct fc_lport *lport)
966{
967 lport->link_up = 0;
968 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800969 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
970 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800971 lport->e_d_tov = 2 * 1000;
972 lport->r_a_tov = 10 * 1000;
973
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800974 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
975 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800976 lport->does_npiv = 1;
977
978 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
979 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
980
981 /* alloc stats structure */
982 if (fc_lport_init_stats(lport))
983 return -ENOMEM;
984
985 /* Finish fc_lport configuration */
986 fc_lport_config(lport);
987
988 return 0;
989}
990
991/**
992 * bnx2fc_fip_recv - handle a received FIP frame.
993 *
994 * @skb: the received skb
995 * @dev: associated &net_device
996 * @ptype: the &packet_type structure which was used to register this handler.
997 * @orig_dev: original receive &net_device, in case @ dev is a bond.
998 *
999 * Returns: 0 for success
1000 */
1001static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1002 struct packet_type *ptype,
1003 struct net_device *orig_dev)
1004{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001005 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001006 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001007 interface = container_of(ptype, struct bnx2fc_interface,
1008 fip_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -07001009 ctlr = bnx2fc_to_ctlr(interface);
1010 fcoe_ctlr_recv(ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001011 return 0;
1012}
1013
1014/**
1015 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1016 *
1017 * @fip: FCoE controller.
1018 * @old: Unicast MAC address to delete if the MAC is non-zero.
1019 * @new: Unicast MAC address to add.
1020 *
1021 * Remove any previously-set unicast MAC filter.
1022 * Add secondary FCoE MAC address filter for our OUI.
1023 */
1024static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1025{
1026 struct fcoe_port *port = lport_priv(lport);
1027
1028 memcpy(port->data_src_addr, addr, ETH_ALEN);
1029}
1030
1031/**
1032 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1033 *
1034 * @lport: libfc port
1035 */
1036static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1037{
1038 struct fcoe_port *port;
1039
1040 port = (struct fcoe_port *)lport_priv(lport);
1041 return port->data_src_addr;
1042}
1043
1044/**
1045 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1046 *
1047 * @fip: FCoE controller.
1048 * @skb: FIP Packet.
1049 */
1050static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1051{
1052 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1053 dev_queue_xmit(skb);
1054}
1055
1056static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1057{
1058 struct Scsi_Host *shost = vport_to_shost(vport);
1059 struct fc_lport *n_port = shost_priv(shost);
1060 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001061 struct bnx2fc_interface *interface = port->priv;
1062 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001063 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001064 int rc;
1065 char buf[32];
1066
1067 rc = fcoe_validate_vport_create(vport);
1068 if (rc) {
1069 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1070 printk(KERN_ERR PFX "Failed to create vport, "
1071 "WWPN (0x%s) already exists\n",
1072 buf);
1073 return rc;
1074 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001075
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001076 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001077 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001078 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001079 return -EIO;
1080 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001081 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001082 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001083 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001084 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001085 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001086
1087 if (IS_ERR(vn_port)) {
1088 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1089 netdev->name);
1090 return -EIO;
1091 }
1092
1093 if (disabled) {
1094 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1095 } else {
1096 vn_port->boot_time = jiffies;
1097 fc_lport_init(vn_port);
1098 fc_fabric_login(vn_port);
1099 fc_vport_setlink(vn_port);
1100 }
1101 return 0;
1102}
1103
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001104static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1105{
1106 struct bnx2fc_lport *blport, *tmp;
1107
1108 spin_lock_bh(&hba->hba_lock);
1109 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1110 if (blport->lport == lport) {
1111 list_del(&blport->list);
1112 kfree(blport);
1113 }
1114 }
1115 spin_unlock_bh(&hba->hba_lock);
1116}
1117
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001118static int bnx2fc_vport_destroy(struct fc_vport *vport)
1119{
1120 struct Scsi_Host *shost = vport_to_shost(vport);
1121 struct fc_lport *n_port = shost_priv(shost);
1122 struct fc_lport *vn_port = vport->dd_data;
1123 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001124 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001125 struct fc_lport *v_port;
1126 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001127
1128 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001129 list_for_each_entry(v_port, &n_port->vports, list)
1130 if (v_port->vport == vport) {
1131 found = true;
1132 break;
1133 }
1134
1135 if (!found) {
1136 mutex_unlock(&n_port->lp_mutex);
1137 return -ENOENT;
1138 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001139 list_del(&vn_port->list);
1140 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001141 bnx2fc_free_vport(interface->hba, port->lport);
1142 bnx2fc_port_shutdown(port->lport);
1143 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001144 queue_work(bnx2fc_wq, &port->destroy_work);
1145 return 0;
1146}
1147
1148static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1149{
1150 struct fc_lport *lport = vport->dd_data;
1151
1152 if (disable) {
1153 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1154 fc_fabric_logoff(lport);
1155 } else {
1156 lport->boot_time = jiffies;
1157 fc_fabric_login(lport);
1158 fc_vport_setlink(lport);
1159 }
1160 return 0;
1161}
1162
1163
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001164static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001165{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001166 struct net_device *netdev = interface->netdev;
1167 struct net_device *physdev = interface->hba->phys_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001168 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001169 struct netdev_hw_addr *ha;
1170 int sel_san_mac = 0;
1171
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001172 /* setup Source MAC Address */
1173 rcu_read_lock();
1174 for_each_dev_addr(physdev, ha) {
1175 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1176 ha->type);
1177 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1178 ha->addr[1], ha->addr[2], ha->addr[3],
1179 ha->addr[4], ha->addr[5]);
1180
1181 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1182 (is_valid_ether_addr(ha->addr))) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001183 memcpy(ctlr->ctl_src_addr, ha->addr,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001184 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001185 sel_san_mac = 1;
1186 BNX2FC_MISC_DBG("Found SAN MAC\n");
1187 }
1188 }
1189 rcu_read_unlock();
1190
1191 if (!sel_san_mac)
1192 return -ENODEV;
1193
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001194 interface->fip_packet_type.func = bnx2fc_fip_recv;
1195 interface->fip_packet_type.type = htons(ETH_P_FIP);
1196 interface->fip_packet_type.dev = netdev;
1197 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001198
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001199 interface->fcoe_packet_type.func = bnx2fc_rcv;
1200 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1201 interface->fcoe_packet_type.dev = netdev;
1202 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001203
1204 return 0;
1205}
1206
1207static int bnx2fc_attach_transport(void)
1208{
1209 bnx2fc_transport_template =
1210 fc_attach_transport(&bnx2fc_transport_function);
1211
1212 if (bnx2fc_transport_template == NULL) {
1213 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1214 return -ENODEV;
1215 }
1216
1217 bnx2fc_vport_xport_template =
1218 fc_attach_transport(&bnx2fc_vport_xport_function);
1219 if (bnx2fc_vport_xport_template == NULL) {
1220 printk(KERN_ERR PFX
1221 "Failed to attach FC transport for vport\n");
1222 fc_release_transport(bnx2fc_transport_template);
1223 bnx2fc_transport_template = NULL;
1224 return -ENODEV;
1225 }
1226 return 0;
1227}
1228static void bnx2fc_release_transport(void)
1229{
1230 fc_release_transport(bnx2fc_transport_template);
1231 fc_release_transport(bnx2fc_vport_xport_template);
1232 bnx2fc_transport_template = NULL;
1233 bnx2fc_vport_xport_template = NULL;
1234}
1235
1236static void bnx2fc_interface_release(struct kref *kref)
1237{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001238 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001239 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001240 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001241
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001242 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001243 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001244
Robert Lovefd8f8902012-05-22 19:06:16 -07001245 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001246 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001247
1248 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001249 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
Robert Lovefd8f8902012-05-22 19:06:16 -07001250 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001251
Robert Lovefd8f8902012-05-22 19:06:16 -07001252 kfree(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001253
1254 dev_put(netdev);
1255 module_put(THIS_MODULE);
1256}
1257
1258static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1259{
1260 kref_get(&interface->kref);
1261}
1262
1263static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1264{
1265 kref_put(&interface->kref, bnx2fc_interface_release);
1266}
1267static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1268{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001269 /* Free the command manager */
1270 if (hba->cmd_mgr) {
1271 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1272 hba->cmd_mgr = NULL;
1273 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001274 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001275 bnx2fc_unbind_pcidev(hba);
1276 kfree(hba);
1277}
1278
1279/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001280 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001281 *
1282 * @cnic: pointer to cnic device
1283 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001284 * Creates a new FCoE hba on the given device.
1285 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001286 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001287static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001288{
1289 struct bnx2fc_hba *hba;
1290 int rc;
1291
1292 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1293 if (!hba) {
1294 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1295 return NULL;
1296 }
1297 spin_lock_init(&hba->hba_lock);
1298 mutex_init(&hba->hba_mutex);
1299
1300 hba->cnic = cnic;
1301 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001302 if (rc) {
1303 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001304 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001305 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001306 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001307 hba->next_conn_id = 0;
1308
1309 hba->tgt_ofld_list =
1310 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1311 GFP_KERNEL);
1312 if (!hba->tgt_ofld_list) {
1313 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1314 goto tgtofld_err;
1315 }
1316
1317 hba->num_ofld_sess = 0;
1318
1319 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1320 BNX2FC_MAX_XID);
1321 if (!hba->cmd_mgr) {
1322 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1323 goto cmgr_err;
1324 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001325
1326 init_waitqueue_head(&hba->shutdown_wait);
1327 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001328 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001329
1330 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001331
1332cmgr_err:
1333 kfree(hba->tgt_ofld_list);
1334tgtofld_err:
1335 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001336bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001337 kfree(hba);
1338 return NULL;
1339}
1340
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001341struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1342 struct net_device *netdev,
1343 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001344{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001345 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001346 struct fcoe_ctlr *ctlr;
1347 int size;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001348 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001349
Robert Lovefd8f8902012-05-22 19:06:16 -07001350 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1351 ctlr = kzalloc(size, GFP_KERNEL);
1352 if (!ctlr) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001353 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1354 return NULL;
1355 }
Robert Lovefd8f8902012-05-22 19:06:16 -07001356 interface = fcoe_ctlr_priv(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001357 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001358 kref_init(&interface->kref);
1359 interface->hba = hba;
1360 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001361
1362 /* Initialize FIP */
Robert Lovefd8f8902012-05-22 19:06:16 -07001363 fcoe_ctlr_init(ctlr, fip_mode);
1364 ctlr->send = bnx2fc_fip_send;
1365 ctlr->update_mac = bnx2fc_update_src_mac;
1366 ctlr->get_src_addr = bnx2fc_get_src_mac;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001367 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001368
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001369 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001370 if (!rc)
1371 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001372
Robert Lovefd8f8902012-05-22 19:06:16 -07001373 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001374 dev_put(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001375 kfree(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001376 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001377}
1378
1379/**
1380 * bnx2fc_if_create - Create FCoE instance on a given interface
1381 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001382 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001383 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1384 * @npiv: Indicates if the port is vport or not
1385 *
1386 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1387 *
1388 * Returns: Allocated fc_lport or an error pointer
1389 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001390static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001391 struct device *parent, int npiv)
1392{
Robert Lovefd8f8902012-05-22 19:06:16 -07001393 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Vasu Dev134a4e22011-04-28 15:55:44 -07001394 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001395 struct fcoe_port *port;
1396 struct Scsi_Host *shost;
1397 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001398 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001399 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001400 int rc = 0;
1401
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001402 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1403 if (!blport) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001404 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001405 return NULL;
1406 }
1407
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001408 /* Allocate Scsi_Host structure */
Vasu Dev134a4e22011-04-28 15:55:44 -07001409 if (!npiv)
1410 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1411 else
1412 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001413
1414 if (!lport) {
1415 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001416 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001417 }
1418 shost = lport->host;
1419 port = lport_priv(lport);
1420 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001421 port->priv = interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001422 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1423
1424 /* Configure fcoe_port */
1425 rc = bnx2fc_lport_config(lport);
1426 if (rc)
1427 goto lp_config_err;
1428
1429 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001430 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1431 vport->node_name, vport->port_name);
1432 fc_set_wwnn(lport, vport->node_name);
1433 fc_set_wwpn(lport, vport->port_name);
1434 }
1435 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001436 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001437 if (rc) {
1438 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1439 goto lp_config_err;
1440 }
1441
1442 rc = bnx2fc_shost_config(lport, parent);
1443 if (rc) {
1444 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001445 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001446 goto lp_config_err;
1447 }
1448
1449 /* Initialize the libfc library */
1450 rc = bnx2fc_libfc_config(lport);
1451 if (rc) {
1452 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1453 goto shost_err;
1454 }
1455 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1456
1457 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001458 if (!npiv)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001459 rc = bnx2fc_em_config(lport);
Vasu Dev134a4e22011-04-28 15:55:44 -07001460 else {
1461 shost = vport_to_shost(vport);
1462 n_port = shost_priv(shost);
1463 rc = fc_exch_mgr_list_clone(n_port, lport);
1464 }
1465
1466 if (rc) {
1467 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1468 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001469 }
1470
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001471 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001472
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001473 hba = interface->hba;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001474 spin_lock_bh(&hba->hba_lock);
1475 blport->lport = lport;
1476 list_add_tail(&blport->list, &hba->vports);
1477 spin_unlock_bh(&hba->hba_lock);
1478
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001479 return lport;
1480
1481shost_err:
1482 scsi_remove_host(shost);
1483lp_config_err:
1484 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001485free_blport:
1486 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001487 return NULL;
1488}
1489
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001490static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001491{
1492 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001493 __dev_remove_pack(&interface->fcoe_packet_type);
1494 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001495 synchronize_net();
1496}
1497
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001498static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001499{
Robert Lovefd8f8902012-05-22 19:06:16 -07001500 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1501 struct fc_lport *lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001502 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001503 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001504
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001505 /* Stop the transmit retry timer */
1506 del_timer_sync(&port->timer);
1507
1508 /* Free existing transmit skbs */
1509 fcoe_clean_pending_queue(lport);
1510
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001511 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001512
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001513 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001514}
1515
1516static void bnx2fc_if_destroy(struct fc_lport *lport)
1517{
1518
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001519 /* Free queued packets for the receive thread */
1520 bnx2fc_clean_rx_queue(lport);
1521
1522 /* Detach from scsi-ml */
1523 fc_remove_host(lport->host);
1524 scsi_remove_host(lport->host);
1525
1526 /*
1527 * Note that only the physical lport will have the exchange manager.
1528 * for vports, this function is NOP
1529 */
1530 fc_exch_mgr_free(lport);
1531
1532 /* Free memory used by statistical counters */
1533 fc_lport_free_stats(lport);
1534
1535 /* Release Scsi_Host */
1536 scsi_host_put(lport->host);
1537}
1538
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001539static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001540{
Robert Lovefd8f8902012-05-22 19:06:16 -07001541 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1542 struct fc_lport *lport = ctlr->lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001543 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001544
1545 bnx2fc_interface_cleanup(interface);
1546 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001547 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001548 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001549 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001550}
1551
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001552/**
1553 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1554 *
1555 * @buffer: The name of the Ethernet interface to be destroyed
1556 * @kp: The associated kernel parameter
1557 *
1558 * Called from sysfs.
1559 *
1560 * Returns: 0 for success
1561 */
1562static int bnx2fc_destroy(struct net_device *netdev)
1563{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001564 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001565 struct workqueue_struct *timer_work_queue;
Robert Lovefd8f8902012-05-22 19:06:16 -07001566 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001567 int rc = 0;
1568
Nithin Sujir6702ca12011-03-17 17:13:27 -07001569 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001570 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001571
1572 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001573 ctlr = bnx2fc_to_ctlr(interface);
1574 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001575 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001576 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001577 goto netdev_err;
1578 }
1579
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001580 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001581 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001582 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001583
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001584netdev_err:
1585 mutex_unlock(&bnx2fc_dev_lock);
1586 rtnl_unlock();
1587 return rc;
1588}
1589
1590static void bnx2fc_destroy_work(struct work_struct *work)
1591{
1592 struct fcoe_port *port;
1593 struct fc_lport *lport;
1594
1595 port = container_of(work, struct fcoe_port, destroy_work);
1596 lport = port->lport;
1597
1598 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1599
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001600 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001601}
1602
1603static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1604{
1605 bnx2fc_free_fw_resc(hba);
1606 bnx2fc_free_task_ctx(hba);
1607}
1608
1609/**
1610 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1611 * pci structure
1612 *
1613 * @hba: Adapter instance
1614 */
1615static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1616{
1617 if (bnx2fc_setup_task_ctx(hba))
1618 goto mem_err;
1619
1620 if (bnx2fc_setup_fw_resc(hba))
1621 goto mem_err;
1622
1623 return 0;
1624mem_err:
1625 bnx2fc_unbind_adapter_devices(hba);
1626 return -ENOMEM;
1627}
1628
1629static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1630{
1631 struct cnic_dev *cnic;
1632
1633 if (!hba->cnic) {
1634 printk(KERN_ERR PFX "cnic is NULL\n");
1635 return -ENODEV;
1636 }
1637 cnic = hba->cnic;
1638 hba->pcidev = cnic->pcidev;
1639 if (hba->pcidev)
1640 pci_dev_get(hba->pcidev);
1641
1642 return 0;
1643}
1644
1645static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1646{
1647 if (hba->pcidev)
1648 pci_dev_put(hba->pcidev);
1649 hba->pcidev = NULL;
1650}
1651
1652
1653
1654/**
1655 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1656 *
1657 * @handle: transport handle pointing to adapter struture
1658 *
1659 * This function maps adapter structure to pcidev structure and initiates
1660 * firmware handshake to enable/initialize on-chip FCoE components.
1661 * This bnx2fc - cnic interface api callback is used after following
1662 * conditions are met -
1663 * a) underlying network interface is up (marked by event NETDEV_UP
1664 * from netdev
1665 * b) bnx2fc adatper structure is registered.
1666 */
1667static void bnx2fc_ulp_start(void *handle)
1668{
1669 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001670 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001671 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001672 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001673
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001674 mutex_lock(&bnx2fc_dev_lock);
1675
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001676 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001677 bnx2fc_fw_init(hba);
1678
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001679 BNX2FC_MISC_DBG("bnx2fc started.\n");
1680
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001681 list_for_each_entry(interface, &if_list, list) {
1682 if (interface->hba == hba) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001683 ctlr = bnx2fc_to_ctlr(interface);
1684 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001685 /* Kick off Fabric discovery*/
1686 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1687 lport->tt.frame_send = bnx2fc_xmit;
1688 bnx2fc_start_disc(interface);
1689 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001690 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001691
1692 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001693}
1694
1695static void bnx2fc_port_shutdown(struct fc_lport *lport)
1696{
1697 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1698 fc_fabric_logoff(lport);
1699 fc_lport_destroy(lport);
1700}
1701
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001702static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001703{
Robert Lovefd8f8902012-05-22 19:06:16 -07001704 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001705 struct fc_lport *lport;
1706 struct fc_lport *vport;
1707
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001708 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1709 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001710
Robert Lovefd8f8902012-05-22 19:06:16 -07001711 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001712 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001713
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001714 mutex_lock(&lport->lp_mutex);
1715 list_for_each_entry(vport, &lport->vports, list)
1716 fc_host_port_type(vport->host) =
1717 FC_PORTTYPE_UNKNOWN;
1718 mutex_unlock(&lport->lp_mutex);
1719 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
Robert Lovefd8f8902012-05-22 19:06:16 -07001720 fcoe_ctlr_link_down(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001721 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001722}
1723
1724static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1725{
1726#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1727 int rc = -1;
1728 int i = HZ;
1729
1730 rc = bnx2fc_bind_adapter_devices(hba);
1731 if (rc) {
1732 printk(KERN_ALERT PFX
1733 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1734 goto err_out;
1735 }
1736
1737 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1738 if (rc) {
1739 printk(KERN_ALERT PFX
1740 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1741 goto err_unbind;
1742 }
1743
1744 /*
1745 * Wait until the adapter init message is complete, and adapter
1746 * state is UP.
1747 */
1748 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1749 msleep(BNX2FC_INIT_POLL_TIME);
1750
1751 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1752 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1753 "Ignoring...\n",
1754 hba->cnic->netdev->name);
1755 rc = -1;
1756 goto err_unbind;
1757 }
1758
1759
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001760 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001761 return 0;
1762
1763err_unbind:
1764 bnx2fc_unbind_adapter_devices(hba);
1765err_out:
1766 return rc;
1767}
1768
1769static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1770{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001771 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001772 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1773 init_timer(&hba->destroy_timer);
1774 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1775 jiffies;
1776 hba->destroy_timer.function = bnx2fc_destroy_timer;
1777 hba->destroy_timer.data = (unsigned long)hba;
1778 add_timer(&hba->destroy_timer);
1779 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001780 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1781 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001782 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001783 /* This should never happen */
1784 if (signal_pending(current))
1785 flush_signals(current);
1786
1787 del_timer_sync(&hba->destroy_timer);
1788 }
1789 bnx2fc_unbind_adapter_devices(hba);
1790 }
1791}
1792
1793/**
1794 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1795 *
1796 * @handle: transport handle pointing to adapter structure
1797 *
1798 * Driver checks if adapter is already in shutdown mode, if not start
1799 * the shutdown process.
1800 */
1801static void bnx2fc_ulp_stop(void *handle)
1802{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001803 struct bnx2fc_hba *hba = handle;
1804 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001805
1806 printk(KERN_ERR "ULP_STOP\n");
1807
1808 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001809 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1810 goto exit;
1811 list_for_each_entry(interface, &if_list, list) {
1812 if (interface->hba == hba)
1813 bnx2fc_stop(interface);
1814 }
1815 BUG_ON(hba->num_ofld_sess != 0);
1816
1817 mutex_lock(&hba->hba_mutex);
1818 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1819 clear_bit(ADAPTER_STATE_GOING_DOWN,
1820 &hba->adapter_state);
1821
1822 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1823 mutex_unlock(&hba->hba_mutex);
1824
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001825 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001826exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001827 mutex_unlock(&bnx2fc_dev_lock);
1828}
1829
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001830static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001831{
Robert Lovefd8f8902012-05-22 19:06:16 -07001832 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001833 struct fc_lport *lport;
1834 int wait_cnt = 0;
1835
1836 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1837 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001838 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001839 printk(KERN_ERR PFX "Init not done yet\n");
1840 return;
1841 }
1842
Robert Lovefd8f8902012-05-22 19:06:16 -07001843 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001844 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1845
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001846 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001847 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001848 fcoe_ctlr_link_up(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001849 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001850 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001851 }
1852
1853 /* wait for the FCF to be selected before issuing FLOGI */
Robert Lovefd8f8902012-05-22 19:06:16 -07001854 while (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001855 msleep(250);
1856 /* give up after 3 secs */
1857 if (++wait_cnt > 12)
1858 break;
1859 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001860
1861 /* Reset max receive frame size to default */
1862 if (fc_set_mfs(lport, BNX2FC_MFS))
1863 return;
1864
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001865 fc_lport_init(lport);
1866 fc_fabric_login(lport);
1867}
1868
1869
1870/**
1871 * bnx2fc_ulp_init - Initialize an adapter instance
1872 *
1873 * @dev : cnic device handle
1874 * Called from cnic_register_driver() context to initialize all
1875 * enumerated cnic devices. This routine allocates adapter structure
1876 * and other device specific resources.
1877 */
1878static void bnx2fc_ulp_init(struct cnic_dev *dev)
1879{
1880 struct bnx2fc_hba *hba;
1881 int rc = 0;
1882
1883 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1884 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001885 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1886 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001887 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001888 " flags: %lx fcoe_conn: %d\n",
1889 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001890 return;
1891 }
1892
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001893 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001894 if (!hba) {
1895 printk(KERN_ERR PFX "hba initialization failed\n");
1896 return;
1897 }
1898
1899 /* Add HBA to the adapter list */
1900 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001901 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001902 adapter_count++;
1903 mutex_unlock(&bnx2fc_dev_lock);
1904
1905 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1906 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1907 (void *) hba);
1908 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001909 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001910 else
1911 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1912}
1913
1914
1915static int bnx2fc_disable(struct net_device *netdev)
1916{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001917 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001918 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001919 int rc = 0;
1920
Nithin Sujir6702ca12011-03-17 17:13:27 -07001921 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001922 mutex_lock(&bnx2fc_dev_lock);
1923
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001924 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001925 ctlr = bnx2fc_to_ctlr(interface);
1926 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001927 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001928 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001929 } else {
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001930 interface->enabled = false;
Robert Lovefd8f8902012-05-22 19:06:16 -07001931 fcoe_ctlr_link_down(ctlr);
1932 fcoe_clean_pending_queue(ctlr->lp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001933 }
1934
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001935 mutex_unlock(&bnx2fc_dev_lock);
1936 rtnl_unlock();
1937 return rc;
1938}
1939
1940
1941static int bnx2fc_enable(struct net_device *netdev)
1942{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001943 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001944 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001945 int rc = 0;
1946
Nithin Sujir6702ca12011-03-17 17:13:27 -07001947 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001948 mutex_lock(&bnx2fc_dev_lock);
1949
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001950 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001951 ctlr = bnx2fc_to_ctlr(interface);
1952 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001953 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001954 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001955 } else if (!bnx2fc_link_ok(ctlr->lp)) {
1956 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001957 interface->enabled = true;
1958 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001959
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001960 mutex_unlock(&bnx2fc_dev_lock);
1961 rtnl_unlock();
1962 return rc;
1963}
1964
1965/**
1966 * bnx2fc_create - Create bnx2fc FCoE interface
1967 *
1968 * @buffer: The name of Ethernet interface to create on
1969 * @kp: The associated kernel param
1970 *
1971 * Called from sysfs.
1972 *
1973 * Returns: 0 for success
1974 */
1975static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1976{
Robert Lovefd8f8902012-05-22 19:06:16 -07001977 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001978 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001979 struct bnx2fc_hba *hba;
1980 struct net_device *phys_dev;
1981 struct fc_lport *lport;
1982 struct ethtool_drvinfo drvinfo;
1983 int rc = 0;
1984 int vlan_id;
1985
1986 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1987 if (fip_mode != FIP_MODE_FABRIC) {
1988 printk(KERN_ERR "fip mode not FABRIC\n");
1989 return -EIO;
1990 }
1991
Nithin Sujir6702ca12011-03-17 17:13:27 -07001992 rtnl_lock();
1993
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001994 mutex_lock(&bnx2fc_dev_lock);
1995
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001996 if (!try_module_get(THIS_MODULE)) {
1997 rc = -EINVAL;
1998 goto mod_err;
1999 }
2000
2001 /* obtain physical netdev */
2002 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2003 phys_dev = vlan_dev_real_dev(netdev);
2004 vlan_id = vlan_dev_vlan_id(netdev);
2005 } else {
2006 printk(KERN_ERR PFX "Not a vlan device\n");
2007 rc = -EINVAL;
2008 goto netdev_err;
2009 }
2010 /* verify if the physical device is a netxtreme2 device */
2011 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2012 memset(&drvinfo, 0, sizeof(drvinfo));
2013 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002014 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002015 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2016 rc = -EINVAL;
2017 goto netdev_err;
2018 }
2019 } else {
2020 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2021 rc = -EINVAL;
2022 goto netdev_err;
2023 }
2024
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002025 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002026 hba = bnx2fc_hba_lookup(phys_dev);
2027 if (!hba) {
2028 rc = -ENODEV;
2029 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2030 goto netdev_err;
2031 }
2032
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002033 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002034 rc = -EEXIST;
2035 goto netdev_err;
2036 }
2037
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002038 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2039 if (!interface) {
2040 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002041 goto ifput_err;
2042 }
2043
Robert Lovefd8f8902012-05-22 19:06:16 -07002044 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002045 interface->vlan_id = vlan_id;
2046 interface->vlan_enabled = 1;
2047
2048 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002049 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002050 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002051 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2052 rc = -EINVAL;
2053 goto ifput_err;
2054 }
2055
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002056 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002057 if (!lport) {
2058 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2059 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002060 rc = -EINVAL;
2061 goto if_create_err;
2062 }
2063
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002064 /* Add interface to if_list */
2065 list_add_tail(&interface->list, &if_list);
2066
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002067 lport->boot_time = jiffies;
2068
2069 /* Make this master N_port */
Robert Lovefd8f8902012-05-22 19:06:16 -07002070 ctlr->lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002071
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002072 if (!bnx2fc_link_ok(lport)) {
Robert Lovefd8f8902012-05-22 19:06:16 -07002073 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002074 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2075 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2076 }
2077
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002078 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2079 bnx2fc_start_disc(interface);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002080 interface->enabled = true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002081 /*
2082 * Release from kref_init in bnx2fc_interface_setup, on success
2083 * lport should be holding a reference taken in bnx2fc_if_create
2084 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002085 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002086 /* put netdev that was held while calling dev_get_by_name */
2087 mutex_unlock(&bnx2fc_dev_lock);
2088 rtnl_unlock();
2089 return 0;
2090
2091if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002092 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002093ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002094 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002095 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002096 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002097netdev_err:
2098 module_put(THIS_MODULE);
2099mod_err:
2100 mutex_unlock(&bnx2fc_dev_lock);
2101 rtnl_unlock();
2102 return rc;
2103}
2104
2105/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002106 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002107 *
2108 * @cnic: Pointer to cnic device instance
2109 *
2110 **/
2111static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2112{
2113 struct list_head *list;
2114 struct list_head *temp;
2115 struct bnx2fc_hba *hba;
2116
2117 /* Called with bnx2fc_dev_lock held */
2118 list_for_each_safe(list, temp, &adapter_list) {
2119 hba = (struct bnx2fc_hba *)list;
2120 if (hba->cnic == cnic)
2121 return hba;
2122 }
2123 return NULL;
2124}
2125
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002126static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2127 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002128{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002129 struct bnx2fc_interface *interface;
2130
2131 /* Called with bnx2fc_dev_lock held */
2132 list_for_each_entry(interface, &if_list, list) {
2133 if (interface->netdev == netdev)
2134 return interface;
2135 }
2136 return NULL;
2137}
2138
2139static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2140 *phys_dev)
2141{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002142 struct bnx2fc_hba *hba;
2143
2144 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002145 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002146 if (hba->phys_dev == phys_dev)
2147 return hba;
2148 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002149 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002150 return NULL;
2151}
2152
2153/**
2154 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2155 *
2156 * @dev cnic device handle
2157 */
2158static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2159{
2160 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002161 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002162
2163 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2164
2165 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2166 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2167 dev->netdev->name, dev->flags);
2168 return;
2169 }
2170
2171 mutex_lock(&bnx2fc_dev_lock);
2172 hba = bnx2fc_find_hba_for_cnic(dev);
2173 if (!hba) {
2174 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2175 dev);
2176 mutex_unlock(&bnx2fc_dev_lock);
2177 return;
2178 }
2179
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002180 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002181 adapter_count--;
2182
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002183 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002184 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002185 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002186 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002187 mutex_unlock(&bnx2fc_dev_lock);
2188
2189 bnx2fc_ulp_stop(hba);
2190 /* unregister cnic device */
2191 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2192 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002193 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002194}
2195
2196/**
2197 * bnx2fc_fcoe_reset - Resets the fcoe
2198 *
2199 * @shost: shost the reset is from
2200 *
2201 * Returns: always 0
2202 */
2203static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2204{
2205 struct fc_lport *lport = shost_priv(shost);
2206 fc_lport_reset(lport);
2207 return 0;
2208}
2209
2210
2211static bool bnx2fc_match(struct net_device *netdev)
2212{
2213 mutex_lock(&bnx2fc_dev_lock);
2214 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2215 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2216
2217 if (bnx2fc_hba_lookup(phys_dev)) {
2218 mutex_unlock(&bnx2fc_dev_lock);
2219 return true;
2220 }
2221 }
2222 mutex_unlock(&bnx2fc_dev_lock);
2223 return false;
2224}
2225
2226
2227static struct fcoe_transport bnx2fc_transport = {
2228 .name = {"bnx2fc"},
2229 .attached = false,
2230 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2231 .match = bnx2fc_match,
2232 .create = bnx2fc_create,
2233 .destroy = bnx2fc_destroy,
2234 .enable = bnx2fc_enable,
2235 .disable = bnx2fc_disable,
2236};
2237
2238/**
2239 * bnx2fc_percpu_thread_create - Create a receive thread for an
2240 * online CPU
2241 *
2242 * @cpu: cpu index for the online cpu
2243 */
2244static void bnx2fc_percpu_thread_create(unsigned int cpu)
2245{
2246 struct bnx2fc_percpu_s *p;
2247 struct task_struct *thread;
2248
2249 p = &per_cpu(bnx2fc_percpu, cpu);
2250
2251 thread = kthread_create(bnx2fc_percpu_io_thread,
2252 (void *)p,
2253 "bnx2fc_thread/%d", cpu);
2254 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002255 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002256 kthread_bind(thread, cpu);
2257 p->iothread = thread;
2258 wake_up_process(thread);
2259 }
2260}
2261
2262static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2263{
2264 struct bnx2fc_percpu_s *p;
2265 struct task_struct *thread;
2266 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002267
2268 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2269
2270 /* Prevent any new work from being queued for this CPU */
2271 p = &per_cpu(bnx2fc_percpu, cpu);
2272 spin_lock_bh(&p->fp_work_lock);
2273 thread = p->iothread;
2274 p->iothread = NULL;
2275
2276
2277 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002278 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002279 list_del_init(&work->list);
2280 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2281 kfree(work);
2282 }
2283
2284 spin_unlock_bh(&p->fp_work_lock);
2285
2286 if (thread)
2287 kthread_stop(thread);
2288}
2289
2290/**
2291 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2292 *
2293 * @nfb: The callback data block
2294 * @action: The event triggering the callback
2295 * @hcpu: The index of the CPU that the event is for
2296 *
2297 * This creates or destroys per-CPU data for fcoe
2298 *
2299 * Returns NOTIFY_OK always.
2300 */
2301static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2302 unsigned long action, void *hcpu)
2303{
2304 unsigned cpu = (unsigned long)hcpu;
2305
2306 switch (action) {
2307 case CPU_ONLINE:
2308 case CPU_ONLINE_FROZEN:
2309 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2310 bnx2fc_percpu_thread_create(cpu);
2311 break;
2312 case CPU_DEAD:
2313 case CPU_DEAD_FROZEN:
2314 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2315 bnx2fc_percpu_thread_destroy(cpu);
2316 break;
2317 default:
2318 break;
2319 }
2320 return NOTIFY_OK;
2321}
2322
2323/**
2324 * bnx2fc_mod_init - module init entry point
2325 *
2326 * Initialize driver wide global data structures, and register
2327 * with cnic module
2328 **/
2329static int __init bnx2fc_mod_init(void)
2330{
2331 struct fcoe_percpu_s *bg;
2332 struct task_struct *l2_thread;
2333 int rc = 0;
2334 unsigned int cpu = 0;
2335 struct bnx2fc_percpu_s *p;
2336
2337 printk(KERN_INFO PFX "%s", version);
2338
2339 /* register as a fcoe transport */
2340 rc = fcoe_transport_attach(&bnx2fc_transport);
2341 if (rc) {
2342 printk(KERN_ERR "failed to register an fcoe transport, check "
2343 "if libfcoe is loaded\n");
2344 goto out;
2345 }
2346
2347 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002348 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002349 mutex_init(&bnx2fc_dev_lock);
2350 adapter_count = 0;
2351
2352 /* Attach FC transport template */
2353 rc = bnx2fc_attach_transport();
2354 if (rc)
2355 goto detach_ft;
2356
2357 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2358 if (!bnx2fc_wq) {
2359 rc = -ENOMEM;
2360 goto release_bt;
2361 }
2362
2363 bg = &bnx2fc_global;
2364 skb_queue_head_init(&bg->fcoe_rx_list);
2365 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2366 (void *)bg,
2367 "bnx2fc_l2_thread");
2368 if (IS_ERR(l2_thread)) {
2369 rc = PTR_ERR(l2_thread);
2370 goto free_wq;
2371 }
2372 wake_up_process(l2_thread);
2373 spin_lock_bh(&bg->fcoe_rx_list.lock);
2374 bg->thread = l2_thread;
2375 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2376
2377 for_each_possible_cpu(cpu) {
2378 p = &per_cpu(bnx2fc_percpu, cpu);
2379 INIT_LIST_HEAD(&p->work_list);
2380 spin_lock_init(&p->fp_work_lock);
2381 }
2382
2383 for_each_online_cpu(cpu) {
2384 bnx2fc_percpu_thread_create(cpu);
2385 }
2386
2387 /* Initialize per CPU interrupt thread */
2388 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2389
2390 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2391
2392 return 0;
2393
2394free_wq:
2395 destroy_workqueue(bnx2fc_wq);
2396release_bt:
2397 bnx2fc_release_transport();
2398detach_ft:
2399 fcoe_transport_detach(&bnx2fc_transport);
2400out:
2401 return rc;
2402}
2403
2404static void __exit bnx2fc_mod_exit(void)
2405{
2406 LIST_HEAD(to_be_deleted);
2407 struct bnx2fc_hba *hba, *next;
2408 struct fcoe_percpu_s *bg;
2409 struct task_struct *l2_thread;
2410 struct sk_buff *skb;
2411 unsigned int cpu = 0;
2412
2413 /*
2414 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2415 * it will have higher precedence than bnx2fc_dev_lock.
2416 * unregister_device() cannot be called with bnx2fc_dev_lock
2417 * held.
2418 */
2419 mutex_lock(&bnx2fc_dev_lock);
2420 list_splice(&adapter_list, &to_be_deleted);
2421 INIT_LIST_HEAD(&adapter_list);
2422 adapter_count = 0;
2423 mutex_unlock(&bnx2fc_dev_lock);
2424
2425 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002426 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2427 list_del_init(&hba->list);
2428 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2429 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002430 bnx2fc_ulp_stop(hba);
2431 /* unregister cnic device */
2432 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2433 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002434 hba->cnic->unregister_device(hba->cnic,
2435 CNIC_ULP_FCOE);
2436 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002437 }
2438 cnic_unregister_driver(CNIC_ULP_FCOE);
2439
2440 /* Destroy global thread */
2441 bg = &bnx2fc_global;
2442 spin_lock_bh(&bg->fcoe_rx_list.lock);
2443 l2_thread = bg->thread;
2444 bg->thread = NULL;
2445 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2446 kfree_skb(skb);
2447
2448 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2449
2450 if (l2_thread)
2451 kthread_stop(l2_thread);
2452
2453 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2454
2455 /* Destroy per cpu threads */
2456 for_each_online_cpu(cpu) {
2457 bnx2fc_percpu_thread_destroy(cpu);
2458 }
2459
2460 destroy_workqueue(bnx2fc_wq);
2461 /*
2462 * detach from scsi transport
2463 * must happen after all destroys are done
2464 */
2465 bnx2fc_release_transport();
2466
2467 /* detach from fcoe transport */
2468 fcoe_transport_detach(&bnx2fc_transport);
2469}
2470
2471module_init(bnx2fc_mod_init);
2472module_exit(bnx2fc_mod_exit);
2473
2474static struct fc_function_template bnx2fc_transport_function = {
2475 .show_host_node_name = 1,
2476 .show_host_port_name = 1,
2477 .show_host_supported_classes = 1,
2478 .show_host_supported_fc4s = 1,
2479 .show_host_active_fc4s = 1,
2480 .show_host_maxframe_size = 1,
2481
2482 .show_host_port_id = 1,
2483 .show_host_supported_speeds = 1,
2484 .get_host_speed = fc_get_host_speed,
2485 .show_host_speed = 1,
2486 .show_host_port_type = 1,
2487 .get_host_port_state = fc_get_host_port_state,
2488 .show_host_port_state = 1,
2489 .show_host_symbolic_name = 1,
2490
2491 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2492 sizeof(struct bnx2fc_rport)),
2493 .show_rport_maxframe_size = 1,
2494 .show_rport_supported_classes = 1,
2495
2496 .show_host_fabric_name = 1,
2497 .show_starget_node_name = 1,
2498 .show_starget_port_name = 1,
2499 .show_starget_port_id = 1,
2500 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2501 .show_rport_dev_loss_tmo = 1,
2502 .get_fc_host_stats = bnx2fc_get_host_stats,
2503
2504 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2505
2506 .terminate_rport_io = fc_rport_terminate_io,
2507
2508 .vport_create = bnx2fc_vport_create,
2509 .vport_delete = bnx2fc_vport_destroy,
2510 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002511 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002512};
2513
2514static struct fc_function_template bnx2fc_vport_xport_function = {
2515 .show_host_node_name = 1,
2516 .show_host_port_name = 1,
2517 .show_host_supported_classes = 1,
2518 .show_host_supported_fc4s = 1,
2519 .show_host_active_fc4s = 1,
2520 .show_host_maxframe_size = 1,
2521
2522 .show_host_port_id = 1,
2523 .show_host_supported_speeds = 1,
2524 .get_host_speed = fc_get_host_speed,
2525 .show_host_speed = 1,
2526 .show_host_port_type = 1,
2527 .get_host_port_state = fc_get_host_port_state,
2528 .show_host_port_state = 1,
2529 .show_host_symbolic_name = 1,
2530
2531 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2532 sizeof(struct bnx2fc_rport)),
2533 .show_rport_maxframe_size = 1,
2534 .show_rport_supported_classes = 1,
2535
2536 .show_host_fabric_name = 1,
2537 .show_starget_node_name = 1,
2538 .show_starget_port_name = 1,
2539 .show_starget_port_id = 1,
2540 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2541 .show_rport_dev_loss_tmo = 1,
2542 .get_fc_host_stats = fc_get_host_stats,
2543 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2544 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002545 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002546};
2547
2548/**
2549 * scsi_host_template structure used while registering with SCSI-ml
2550 */
2551static struct scsi_host_template bnx2fc_shost_template = {
2552 .module = THIS_MODULE,
2553 .name = "Broadcom Offload FCoE Initiator",
2554 .queuecommand = bnx2fc_queuecommand,
2555 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2556 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2557 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2558 .eh_host_reset_handler = fc_eh_host_reset,
2559 .slave_alloc = fc_slave_alloc,
2560 .change_queue_depth = fc_change_queue_depth,
2561 .change_queue_type = fc_change_queue_type,
2562 .this_id = -1,
2563 .cmd_per_lun = 3,
Bhanu Gollapudi0ea5c272011-03-17 17:13:29 -07002564 .can_queue = BNX2FC_CAN_QUEUE,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002565 .use_clustering = ENABLE_CLUSTERING,
2566 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2567 .max_sectors = 512,
2568};
2569
2570static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2571 .frame_send = bnx2fc_xmit,
2572 .elsct_send = bnx2fc_elsct_send,
2573 .fcp_abort_io = bnx2fc_abort_io,
2574 .fcp_cleanup = bnx2fc_cleanup,
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -07002575 .get_lesb = bnx2fc_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002576 .rport_event_callback = bnx2fc_rport_event_handler,
2577};
2578
2579/**
2580 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2581 * structure carrying callback function pointers
2582 */
2583static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2584 .owner = THIS_MODULE,
2585 .cnic_init = bnx2fc_ulp_init,
2586 .cnic_exit = bnx2fc_ulp_exit,
2587 .cnic_start = bnx2fc_ulp_start,
2588 .cnic_stop = bnx2fc_ulp_stop,
2589 .indicate_kcqes = bnx2fc_indicate_kcqe,
2590 .indicate_netevent = bnx2fc_indicate_netevent,
2591};