blob: d2bbb8030f7eeb6dd599f04f3d7308437e3b63e0 [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 Gollapudi0a336d62011-09-19 16:52:14 -070025#define DRV_MODULE_RELDATE "Sep 19, 2011"
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
102static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
103{
104 struct fcoe_percpu_s *bg;
105 struct fcoe_rcv_info *fr;
106 struct sk_buff_head *list;
107 struct sk_buff *skb, *next;
108 struct sk_buff *head;
109
110 bg = &bnx2fc_global;
111 spin_lock_bh(&bg->fcoe_rx_list.lock);
112 list = &bg->fcoe_rx_list;
113 head = list->next;
114 for (skb = head; skb != (struct sk_buff *)list;
115 skb = next) {
116 next = skb->next;
117 fr = fcoe_dev_from_skb(skb);
118 if (fr->fr_dev == lp) {
119 __skb_unlink(skb, list);
120 kfree_skb(skb);
121 }
122 }
123 spin_unlock_bh(&bg->fcoe_rx_list.lock);
124}
125
126int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
127{
128 int rc;
129 spin_lock(&bnx2fc_global_lock);
130 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
131 spin_unlock(&bnx2fc_global_lock);
132
133 return rc;
134}
135
136static void bnx2fc_abort_io(struct fc_lport *lport)
137{
138 /*
139 * This function is no-op for bnx2fc, but we do
140 * not want to leave it as NULL either, as libfc
141 * can call the default function which is
142 * fc_fcp_abort_io.
143 */
144}
145
146static void bnx2fc_cleanup(struct fc_lport *lport)
147{
148 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700149 struct bnx2fc_interface *interface = port->priv;
150 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800151 struct bnx2fc_rport *tgt;
152 int i;
153
154 BNX2FC_MISC_DBG("Entered %s\n", __func__);
155 mutex_lock(&hba->hba_mutex);
156 spin_lock_bh(&hba->hba_lock);
157 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
158 tgt = hba->tgt_ofld_list[i];
159 if (tgt) {
160 /* Cleanup IOs belonging to requested vport */
161 if (tgt->port == port) {
162 spin_unlock_bh(&hba->hba_lock);
163 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
164 bnx2fc_flush_active_ios(tgt);
165 spin_lock_bh(&hba->hba_lock);
166 }
167 }
168 }
169 spin_unlock_bh(&hba->hba_lock);
170 mutex_unlock(&hba->hba_mutex);
171}
172
173static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
174 struct fc_frame *fp)
175{
176 struct fc_rport_priv *rdata = tgt->rdata;
177 struct fc_frame_header *fh;
178 int rc = 0;
179
180 fh = fc_frame_header_get(fp);
181 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
182 "r_ctl = 0x%x\n", rdata->ids.port_id,
183 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
184 if ((fh->fh_type == FC_TYPE_ELS) &&
185 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
186
187 switch (fc_frame_payload_op(fp)) {
188 case ELS_ADISC:
189 rc = bnx2fc_send_adisc(tgt, fp);
190 break;
191 case ELS_LOGO:
192 rc = bnx2fc_send_logo(tgt, fp);
193 break;
194 case ELS_RLS:
195 rc = bnx2fc_send_rls(tgt, fp);
196 break;
197 default:
198 break;
199 }
200 } else if ((fh->fh_type == FC_TYPE_BLS) &&
201 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
202 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
203 else {
204 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
205 "rctl 0x%x thru non-offload path\n",
206 fh->fh_type, fh->fh_r_ctl);
207 return -ENODEV;
208 }
209 if (rc)
210 return -ENOMEM;
211 else
212 return 0;
213}
214
215/**
216 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
217 *
218 * @lport: the associated local port
219 * @fp: the fc_frame to be transmitted
220 */
221static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
222{
223 struct ethhdr *eh;
224 struct fcoe_crc_eof *cp;
225 struct sk_buff *skb;
226 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700227 struct bnx2fc_interface *interface;
228 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800229 struct fcoe_port *port;
230 struct fcoe_hdr *hp;
231 struct bnx2fc_rport *tgt;
232 struct fcoe_dev_stats *stats;
233 u8 sof, eof;
234 u32 crc;
235 unsigned int hlen, tlen, elen;
236 int wlen, rc = 0;
237
238 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700239 interface = port->priv;
240 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800241
242 fh = fc_frame_header_get(fp);
243
244 skb = fp_skb(fp);
245 if (!lport->link_up) {
246 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
247 kfree_skb(skb);
248 return 0;
249 }
250
251 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700252 if (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800253 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
254 kfree_skb(skb);
255 return -EINVAL;
256 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700257 if (fcoe_ctlr_els_send(&interface->ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800258 return 0;
259 }
260
261 sof = fr_sof(fp);
262 eof = fr_eof(fp);
263
264 /*
265 * Snoop the frame header to check if the frame is for
266 * an offloaded session
267 */
268 /*
269 * tgt_ofld_list access is synchronized using
270 * both hba mutex and hba lock. Atleast hba mutex or
271 * hba lock needs to be held for read access.
272 */
273
274 spin_lock_bh(&hba->hba_lock);
275 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
276 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
277 /* This frame is for offloaded session */
278 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
279 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
280 spin_unlock_bh(&hba->hba_lock);
281 rc = bnx2fc_xmit_l2_frame(tgt, fp);
282 if (rc != -ENODEV) {
283 kfree_skb(skb);
284 return rc;
285 }
286 } else {
287 spin_unlock_bh(&hba->hba_lock);
288 }
289
290 elen = sizeof(struct ethhdr);
291 hlen = sizeof(struct fcoe_hdr);
292 tlen = sizeof(struct fcoe_crc_eof);
293 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
294
295 skb->ip_summed = CHECKSUM_NONE;
296 crc = fcoe_fc_crc(fp);
297
298 /* copy port crc and eof to the skb buff */
299 if (skb_is_nonlinear(skb)) {
300 skb_frag_t *frag;
301 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
302 kfree_skb(skb);
303 return -ENOMEM;
304 }
305 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
306 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
307 + frag->page_offset;
308 } else {
309 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
310 }
311
312 memset(cp, 0, sizeof(*cp));
313 cp->fcoe_eof = eof;
314 cp->fcoe_crc32 = cpu_to_le32(~crc);
315 if (skb_is_nonlinear(skb)) {
316 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
317 cp = NULL;
318 }
319
320 /* adjust skb network/transport offsets to match mac/fcoe/port */
321 skb_push(skb, elen + hlen);
322 skb_reset_mac_header(skb);
323 skb_reset_network_header(skb);
324 skb->mac_len = elen;
325 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700326 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800327
328 /* fill up mac and fcoe headers */
329 eh = eth_hdr(skb);
330 eh->h_proto = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700331 if (interface->ctlr.map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800332 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
333 else
334 /* insert GW address */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700335 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800336
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700337 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
338 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800339 else
340 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
341
342 hp = (struct fcoe_hdr *)(eh + 1);
343 memset(hp, 0, sizeof(*hp));
344 if (FC_FCOE_VER)
345 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
346 hp->fcoe_sof = sof;
347
348 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
349 if (lport->seq_offload && fr_max_payload(fp)) {
350 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
351 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
352 } else {
353 skb_shinfo(skb)->gso_type = 0;
354 skb_shinfo(skb)->gso_size = 0;
355 }
356
357 /*update tx stats */
358 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
359 stats->TxFrames++;
360 stats->TxWords += wlen;
361 put_cpu();
362
363 /* send down to lld */
364 fr_dev(fp) = lport;
365 if (port->fcoe_pending_queue.qlen)
366 fcoe_check_wait_queue(lport, skb);
367 else if (fcoe_start_io(skb))
368 fcoe_check_wait_queue(lport, skb);
369
370 return 0;
371}
372
373/**
374 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
375 *
376 * @skb: the receive socket buffer
377 * @dev: associated net device
378 * @ptype: context
379 * @olddev: last device
380 *
381 * This function receives the packet and builds FC frame and passes it up
382 */
383static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
384 struct packet_type *ptype, struct net_device *olddev)
385{
386 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700387 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800388 struct fc_frame_header *fh;
389 struct fcoe_rcv_info *fr;
390 struct fcoe_percpu_s *bg;
391 unsigned short oxid;
392
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700393 interface = container_of(ptype, struct bnx2fc_interface,
394 fcoe_packet_type);
395 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800396
397 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700398 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800399 goto err;
400 }
401
402 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700403 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800404 goto err;
405 }
406
407 /*
408 * Check for minimum frame length, and make sure required FCoE
409 * and FC headers are pulled into the linear data area.
410 */
411 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
412 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
413 goto err;
414
415 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
416 fh = (struct fc_frame_header *) skb_transport_header(skb);
417
418 oxid = ntohs(fh->fh_ox_id);
419
420 fr = fcoe_dev_from_skb(skb);
421 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800422
423 bg = &bnx2fc_global;
424 spin_lock_bh(&bg->fcoe_rx_list.lock);
425
426 __skb_queue_tail(&bg->fcoe_rx_list, skb);
427 if (bg->fcoe_rx_list.qlen == 1)
428 wake_up_process(bg->thread);
429
430 spin_unlock_bh(&bg->fcoe_rx_list.lock);
431
432 return 0;
433err:
434 kfree_skb(skb);
435 return -1;
436}
437
438static int bnx2fc_l2_rcv_thread(void *arg)
439{
440 struct fcoe_percpu_s *bg = arg;
441 struct sk_buff *skb;
442
443 set_user_nice(current, -20);
444 set_current_state(TASK_INTERRUPTIBLE);
445 while (!kthread_should_stop()) {
446 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800447 spin_lock_bh(&bg->fcoe_rx_list.lock);
448 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
449 spin_unlock_bh(&bg->fcoe_rx_list.lock);
450 bnx2fc_recv_frame(skb);
451 spin_lock_bh(&bg->fcoe_rx_list.lock);
452 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700453 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800454 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800455 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700456 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800457 return 0;
458}
459
460
461static void bnx2fc_recv_frame(struct sk_buff *skb)
462{
463 u32 fr_len;
464 struct fc_lport *lport;
465 struct fcoe_rcv_info *fr;
466 struct fcoe_dev_stats *stats;
467 struct fc_frame_header *fh;
468 struct fcoe_crc_eof crc_eof;
469 struct fc_frame *fp;
470 struct fc_lport *vn_port;
471 struct fcoe_port *port;
472 u8 *mac = NULL;
473 u8 *dest_mac = NULL;
474 struct fcoe_hdr *hp;
475
476 fr = fcoe_dev_from_skb(skb);
477 lport = fr->fr_dev;
478 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700479 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800480 kfree_skb(skb);
481 return;
482 }
483
484 if (skb_is_nonlinear(skb))
485 skb_linearize(skb);
486 mac = eth_hdr(skb)->h_source;
487 dest_mac = eth_hdr(skb)->h_dest;
488
489 /* Pull the header */
490 hp = (struct fcoe_hdr *) skb_network_header(skb);
491 fh = (struct fc_frame_header *) skb_transport_header(skb);
492 skb_pull(skb, sizeof(struct fcoe_hdr));
493 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
494
495 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
496 stats->RxFrames++;
497 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
498
499 fp = (struct fc_frame *)skb;
500 fc_frame_init(fp);
501 fr_dev(fp) = lport;
502 fr_sof(fp) = hp->fcoe_sof;
503 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
504 put_cpu();
505 kfree_skb(skb);
506 return;
507 }
508 fr_eof(fp) = crc_eof.fcoe_eof;
509 fr_crc(fp) = crc_eof.fcoe_crc32;
510 if (pskb_trim(skb, fr_len)) {
511 put_cpu();
512 kfree_skb(skb);
513 return;
514 }
515
516 fh = fc_frame_header_get(fp);
517
518 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
519 if (vn_port) {
520 port = lport_priv(vn_port);
521 if (compare_ether_addr(port->data_src_addr, dest_mac)
522 != 0) {
523 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
524 put_cpu();
525 kfree_skb(skb);
526 return;
527 }
528 }
529 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
530 fh->fh_type == FC_TYPE_FCP) {
531 /* Drop FCP data. We dont this in L2 path */
532 put_cpu();
533 kfree_skb(skb);
534 return;
535 }
536 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
537 fh->fh_type == FC_TYPE_ELS) {
538 switch (fc_frame_payload_op(fp)) {
539 case ELS_LOGO:
540 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
541 /* drop non-FIP LOGO */
542 put_cpu();
543 kfree_skb(skb);
544 return;
545 }
546 break;
547 }
548 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700549
550 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
551 /* Drop incoming ABTS */
552 put_cpu();
553 kfree_skb(skb);
554 return;
555 }
556
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800557 if (le32_to_cpu(fr_crc(fp)) !=
558 ~crc32(~0, skb->data, fr_len)) {
559 if (stats->InvalidCRCCount < 5)
560 printk(KERN_WARNING PFX "dropping frame with "
561 "CRC error\n");
562 stats->InvalidCRCCount++;
563 put_cpu();
564 kfree_skb(skb);
565 return;
566 }
567 put_cpu();
568 fc_exch_recv(lport, fp);
569}
570
571/**
572 * bnx2fc_percpu_io_thread - thread per cpu for ios
573 *
574 * @arg: ptr to bnx2fc_percpu_info structure
575 */
576int bnx2fc_percpu_io_thread(void *arg)
577{
578 struct bnx2fc_percpu_s *p = arg;
579 struct bnx2fc_work *work, *tmp;
580 LIST_HEAD(work_list);
581
582 set_user_nice(current, -20);
583 set_current_state(TASK_INTERRUPTIBLE);
584 while (!kthread_should_stop()) {
585 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800586 spin_lock_bh(&p->fp_work_lock);
587 while (!list_empty(&p->work_list)) {
588 list_splice_init(&p->work_list, &work_list);
589 spin_unlock_bh(&p->fp_work_lock);
590
591 list_for_each_entry_safe(work, tmp, &work_list, list) {
592 list_del_init(&work->list);
593 bnx2fc_process_cq_compl(work->tgt, work->wqe);
594 kfree(work);
595 }
596
597 spin_lock_bh(&p->fp_work_lock);
598 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700599 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800600 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800601 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700602 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800603
604 return 0;
605}
606
607static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
608{
609 struct fc_host_statistics *bnx2fc_stats;
610 struct fc_lport *lport = shost_priv(shost);
611 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700612 struct bnx2fc_interface *interface = port->priv;
613 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800614 struct fcoe_statistics_params *fw_stats;
615 int rc = 0;
616
617 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
618 if (!fw_stats)
619 return NULL;
620
621 bnx2fc_stats = fc_get_host_stats(shost);
622
623 init_completion(&hba->stat_req_done);
624 if (bnx2fc_send_stat_req(hba))
625 return bnx2fc_stats;
626 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
627 if (!rc) {
628 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
629 return bnx2fc_stats;
630 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300631 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800632 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
633 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
634 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
635 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
636
637 bnx2fc_stats->dumped_frames = 0;
638 bnx2fc_stats->lip_count = 0;
639 bnx2fc_stats->nos_count = 0;
640 bnx2fc_stats->loss_of_sync_count = 0;
641 bnx2fc_stats->loss_of_signal_count = 0;
642 bnx2fc_stats->prim_seq_protocol_err_count = 0;
643
644 return bnx2fc_stats;
645}
646
647static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
648{
649 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700650 struct bnx2fc_interface *interface = port->priv;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800651 struct Scsi_Host *shost = lport->host;
652 int rc = 0;
653
654 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
655 shost->max_lun = BNX2FC_MAX_LUN;
656 shost->max_id = BNX2FC_MAX_FCP_TGT;
657 shost->max_channel = 0;
658 if (lport->vport)
659 shost->transportt = bnx2fc_vport_xport_template;
660 else
661 shost->transportt = bnx2fc_transport_template;
662
663 /* Add the new host to SCSI-ml */
664 rc = scsi_add_host(lport->host, dev);
665 if (rc) {
666 printk(KERN_ERR PFX "Error on scsi_add_host\n");
667 return rc;
668 }
669 if (!lport->vport)
670 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
671 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
672 BNX2FC_NAME, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700673 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800674
675 return 0;
676}
677
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800678static void bnx2fc_link_speed_update(struct fc_lport *lport)
679{
680 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700681 struct bnx2fc_interface *interface = port->priv;
682 struct net_device *netdev = interface->netdev;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000683 struct ethtool_cmd ecmd;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800684
685 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
686 lport->link_supported_speeds &=
687 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
688 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
689 SUPPORTED_1000baseT_Full))
690 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
691 if (ecmd.supported & SUPPORTED_10000baseT_Full)
692 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
693
David Decotigny8ae6dac2011-04-27 18:32:38 +0000694 switch (ethtool_cmd_speed(&ecmd)) {
695 case SPEED_1000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800696 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000697 break;
Bhanu Prakash Gollapudia4dc08c2011-05-27 11:47:25 -0700698 case SPEED_2500:
699 lport->link_speed = FC_PORTSPEED_2GBIT;
700 break;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000701 case SPEED_10000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800702 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000703 break;
704 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800705 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800706}
707static int bnx2fc_link_ok(struct fc_lport *lport)
708{
709 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700710 struct bnx2fc_interface *interface = port->priv;
711 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800712 struct net_device *dev = hba->phys_dev;
713 int rc = 0;
714
715 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
716 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
717 else {
718 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
719 rc = -1;
720 }
721 return rc;
722}
723
724/**
725 * bnx2fc_get_link_state - get network link state
726 *
727 * @hba: adapter instance pointer
728 *
729 * updates adapter structure flag based on netdev state
730 */
731void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
732{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700733 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800734 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
735 else
736 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
737}
738
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700739static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800740{
741 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700742 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800743 struct fcoe_port *port;
744 u64 wwnn, wwpn;
745
746 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700747 interface = port->priv;
748 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800749
750 /* require support for get_pauseparam ethtool op. */
751 if (!hba->phys_dev->ethtool_ops ||
752 !hba->phys_dev->ethtool_ops->get_pauseparam)
753 return -EOPNOTSUPP;
754
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700755 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800756 return -EINVAL;
757
758 skb_queue_head_init(&port->fcoe_pending_queue);
759 port->fcoe_pending_queue_active = 0;
760 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
761
762 bnx2fc_link_speed_update(lport);
763
764 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700765 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
766 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
767 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800768 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
769 fc_set_wwnn(lport, wwnn);
770
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700771 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
772 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
773 2, 0);
774
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800775 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
776 fc_set_wwpn(lport, wwpn);
777 }
778
779 return 0;
780}
781
782static void bnx2fc_destroy_timer(unsigned long data)
783{
784 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
785
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700786 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
787 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700788 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800789 wake_up_interruptible(&hba->destroy_wait);
790}
791
792/**
793 * bnx2fc_indicate_netevent - Generic netdev event handler
794 *
795 * @context: adapter structure pointer
796 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000797 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800798 *
799 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700800 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800801 */
Michael Chan415199f2011-07-20 14:55:24 +0000802static void bnx2fc_indicate_netevent(void *context, unsigned long event,
803 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800804{
805 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700806 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800807 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700808 struct bnx2fc_interface *interface, *tmp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700809 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800810 u32 link_possible = 1;
811
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700812 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000813 return;
814
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800815 switch (event) {
816 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800817 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
818 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700819 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800820 break;
821
822 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800823 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
824 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
825 link_possible = 0;
826 break;
827
828 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800829 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
830 link_possible = 0;
831 break;
832
833 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800834 break;
835
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700836 case NETDEV_UNREGISTER:
837 if (!vlan_id)
838 return;
839 mutex_lock(&bnx2fc_dev_lock);
840 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700841 if (interface->hba == hba &&
842 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
843 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700844 }
845 mutex_unlock(&bnx2fc_dev_lock);
846 return;
847
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800848 default:
849 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
850 return;
851 }
852
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700853 mutex_lock(&bnx2fc_dev_lock);
854 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800855
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700856 if (interface->hba != hba)
857 continue;
858
859 lport = interface->ctlr.lp;
860 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
861 interface->netdev->name, event);
862
863 bnx2fc_link_speed_update(lport);
864
865 if (link_possible && !bnx2fc_link_ok(lport)) {
Bhanu Prakash Gollapudic7806732011-08-30 15:54:47 -0700866 /* Reset max recv frame size to default */
867 fc_set_mfs(lport, BNX2FC_MFS);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -0700868 /*
869 * ctlr link up will only be handled during
870 * enable to avoid sending discovery solicitation
871 * on a stale vlan
872 */
Bhanu Prakash Gollapudifd8fa902011-10-03 16:45:00 -0700873 if (interface->enabled)
874 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700875 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800876 mutex_lock(&lport->lp_mutex);
877 list_for_each_entry(vport, &lport->vports, list)
878 fc_host_port_type(vport->host) =
879 FC_PORTTYPE_UNKNOWN;
880 mutex_unlock(&lport->lp_mutex);
881 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
882 per_cpu_ptr(lport->dev_stats,
883 get_cpu())->LinkFailureCount++;
884 put_cpu();
885 fcoe_clean_pending_queue(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700886 wait_for_upload = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800887 }
888 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700889 mutex_unlock(&bnx2fc_dev_lock);
890
891 if (wait_for_upload) {
892 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
893 init_waitqueue_head(&hba->shutdown_wait);
894 BNX2FC_MISC_DBG("indicate_netevent "
895 "num_ofld_sess = %d\n",
896 hba->num_ofld_sess);
897 hba->wait_for_link_down = 1;
898 wait_event_interruptible(hba->shutdown_wait,
899 (hba->num_ofld_sess == 0));
900 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
901 hba->num_ofld_sess);
902 hba->wait_for_link_down = 0;
903
904 if (signal_pending(current))
905 flush_signals(current);
906 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800907}
908
909static int bnx2fc_libfc_config(struct fc_lport *lport)
910{
911
912 /* Set the function pointers set by bnx2fc driver */
913 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
914 sizeof(struct libfc_function_template));
915 fc_elsct_init(lport);
916 fc_exch_init(lport);
917 fc_rport_init(lport);
918 fc_disc_init(lport);
919 return 0;
920}
921
922static int bnx2fc_em_config(struct fc_lport *lport)
923{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800924 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
925 FCOE_MAX_XID, NULL)) {
926 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
927 return -ENOMEM;
928 }
929
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800930 return 0;
931}
932
933static int bnx2fc_lport_config(struct fc_lport *lport)
934{
935 lport->link_up = 0;
936 lport->qfull = 0;
937 lport->max_retry_count = 3;
938 lport->max_rport_retry_count = 3;
939 lport->e_d_tov = 2 * 1000;
940 lport->r_a_tov = 10 * 1000;
941
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800942 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
943 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800944 lport->does_npiv = 1;
945
946 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
947 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
948
949 /* alloc stats structure */
950 if (fc_lport_init_stats(lport))
951 return -ENOMEM;
952
953 /* Finish fc_lport configuration */
954 fc_lport_config(lport);
955
956 return 0;
957}
958
959/**
960 * bnx2fc_fip_recv - handle a received FIP frame.
961 *
962 * @skb: the received skb
963 * @dev: associated &net_device
964 * @ptype: the &packet_type structure which was used to register this handler.
965 * @orig_dev: original receive &net_device, in case @ dev is a bond.
966 *
967 * Returns: 0 for success
968 */
969static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
970 struct packet_type *ptype,
971 struct net_device *orig_dev)
972{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700973 struct bnx2fc_interface *interface;
974 interface = container_of(ptype, struct bnx2fc_interface,
975 fip_packet_type);
976 fcoe_ctlr_recv(&interface->ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800977 return 0;
978}
979
980/**
981 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
982 *
983 * @fip: FCoE controller.
984 * @old: Unicast MAC address to delete if the MAC is non-zero.
985 * @new: Unicast MAC address to add.
986 *
987 * Remove any previously-set unicast MAC filter.
988 * Add secondary FCoE MAC address filter for our OUI.
989 */
990static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
991{
992 struct fcoe_port *port = lport_priv(lport);
993
994 memcpy(port->data_src_addr, addr, ETH_ALEN);
995}
996
997/**
998 * bnx2fc_get_src_mac - return the ethernet source address for an lport
999 *
1000 * @lport: libfc port
1001 */
1002static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1003{
1004 struct fcoe_port *port;
1005
1006 port = (struct fcoe_port *)lport_priv(lport);
1007 return port->data_src_addr;
1008}
1009
1010/**
1011 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1012 *
1013 * @fip: FCoE controller.
1014 * @skb: FIP Packet.
1015 */
1016static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1017{
1018 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1019 dev_queue_xmit(skb);
1020}
1021
1022static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1023{
1024 struct Scsi_Host *shost = vport_to_shost(vport);
1025 struct fc_lport *n_port = shost_priv(shost);
1026 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001027 struct bnx2fc_interface *interface = port->priv;
1028 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001029 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001030 int rc;
1031 char buf[32];
1032
1033 rc = fcoe_validate_vport_create(vport);
1034 if (rc) {
1035 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1036 printk(KERN_ERR PFX "Failed to create vport, "
1037 "WWPN (0x%s) already exists\n",
1038 buf);
1039 return rc;
1040 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001041
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001042 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001043 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001044 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001045 return -EIO;
1046 }
1047 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001048 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001049 mutex_unlock(&bnx2fc_dev_lock);
1050
1051 if (IS_ERR(vn_port)) {
1052 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1053 netdev->name);
1054 return -EIO;
1055 }
1056
1057 if (disabled) {
1058 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1059 } else {
1060 vn_port->boot_time = jiffies;
1061 fc_lport_init(vn_port);
1062 fc_fabric_login(vn_port);
1063 fc_vport_setlink(vn_port);
1064 }
1065 return 0;
1066}
1067
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001068static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1069{
1070 struct bnx2fc_lport *blport, *tmp;
1071
1072 spin_lock_bh(&hba->hba_lock);
1073 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1074 if (blport->lport == lport) {
1075 list_del(&blport->list);
1076 kfree(blport);
1077 }
1078 }
1079 spin_unlock_bh(&hba->hba_lock);
1080}
1081
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001082static int bnx2fc_vport_destroy(struct fc_vport *vport)
1083{
1084 struct Scsi_Host *shost = vport_to_shost(vport);
1085 struct fc_lport *n_port = shost_priv(shost);
1086 struct fc_lport *vn_port = vport->dd_data;
1087 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001088 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001089 struct fc_lport *v_port;
1090 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001091
1092 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001093 list_for_each_entry(v_port, &n_port->vports, list)
1094 if (v_port->vport == vport) {
1095 found = true;
1096 break;
1097 }
1098
1099 if (!found) {
1100 mutex_unlock(&n_port->lp_mutex);
1101 return -ENOENT;
1102 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001103 list_del(&vn_port->list);
1104 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001105 bnx2fc_free_vport(interface->hba, port->lport);
1106 bnx2fc_port_shutdown(port->lport);
1107 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001108 queue_work(bnx2fc_wq, &port->destroy_work);
1109 return 0;
1110}
1111
1112static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1113{
1114 struct fc_lport *lport = vport->dd_data;
1115
1116 if (disable) {
1117 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1118 fc_fabric_logoff(lport);
1119 } else {
1120 lport->boot_time = jiffies;
1121 fc_fabric_login(lport);
1122 fc_vport_setlink(lport);
1123 }
1124 return 0;
1125}
1126
1127
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001128static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001129{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001130 struct net_device *netdev = interface->netdev;
1131 struct net_device *physdev = interface->hba->phys_dev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001132 struct netdev_hw_addr *ha;
1133 int sel_san_mac = 0;
1134
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001135 /* setup Source MAC Address */
1136 rcu_read_lock();
1137 for_each_dev_addr(physdev, ha) {
1138 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1139 ha->type);
1140 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1141 ha->addr[1], ha->addr[2], ha->addr[3],
1142 ha->addr[4], ha->addr[5]);
1143
1144 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1145 (is_valid_ether_addr(ha->addr))) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001146 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1147 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001148 sel_san_mac = 1;
1149 BNX2FC_MISC_DBG("Found SAN MAC\n");
1150 }
1151 }
1152 rcu_read_unlock();
1153
1154 if (!sel_san_mac)
1155 return -ENODEV;
1156
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001157 interface->fip_packet_type.func = bnx2fc_fip_recv;
1158 interface->fip_packet_type.type = htons(ETH_P_FIP);
1159 interface->fip_packet_type.dev = netdev;
1160 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001161
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001162 interface->fcoe_packet_type.func = bnx2fc_rcv;
1163 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1164 interface->fcoe_packet_type.dev = netdev;
1165 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001166
1167 return 0;
1168}
1169
1170static int bnx2fc_attach_transport(void)
1171{
1172 bnx2fc_transport_template =
1173 fc_attach_transport(&bnx2fc_transport_function);
1174
1175 if (bnx2fc_transport_template == NULL) {
1176 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1177 return -ENODEV;
1178 }
1179
1180 bnx2fc_vport_xport_template =
1181 fc_attach_transport(&bnx2fc_vport_xport_function);
1182 if (bnx2fc_vport_xport_template == NULL) {
1183 printk(KERN_ERR PFX
1184 "Failed to attach FC transport for vport\n");
1185 fc_release_transport(bnx2fc_transport_template);
1186 bnx2fc_transport_template = NULL;
1187 return -ENODEV;
1188 }
1189 return 0;
1190}
1191static void bnx2fc_release_transport(void)
1192{
1193 fc_release_transport(bnx2fc_transport_template);
1194 fc_release_transport(bnx2fc_vport_xport_template);
1195 bnx2fc_transport_template = NULL;
1196 bnx2fc_vport_xport_template = NULL;
1197}
1198
1199static void bnx2fc_interface_release(struct kref *kref)
1200{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001201 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001202 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001203
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001204 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001205 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001206
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001207 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001208
1209 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001210 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1211 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001212
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001213 kfree(interface);
1214
1215 dev_put(netdev);
1216 module_put(THIS_MODULE);
1217}
1218
1219static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1220{
1221 kref_get(&interface->kref);
1222}
1223
1224static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1225{
1226 kref_put(&interface->kref, bnx2fc_interface_release);
1227}
1228static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1229{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001230 /* Free the command manager */
1231 if (hba->cmd_mgr) {
1232 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1233 hba->cmd_mgr = NULL;
1234 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001235 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001236 bnx2fc_unbind_pcidev(hba);
1237 kfree(hba);
1238}
1239
1240/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001241 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001242 *
1243 * @cnic: pointer to cnic device
1244 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001245 * Creates a new FCoE hba on the given device.
1246 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001247 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001248static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001249{
1250 struct bnx2fc_hba *hba;
1251 int rc;
1252
1253 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1254 if (!hba) {
1255 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1256 return NULL;
1257 }
1258 spin_lock_init(&hba->hba_lock);
1259 mutex_init(&hba->hba_mutex);
1260
1261 hba->cnic = cnic;
1262 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001263 if (rc) {
1264 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001265 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001266 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001267 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001268 hba->next_conn_id = 0;
1269
1270 hba->tgt_ofld_list =
1271 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1272 GFP_KERNEL);
1273 if (!hba->tgt_ofld_list) {
1274 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1275 goto tgtofld_err;
1276 }
1277
1278 hba->num_ofld_sess = 0;
1279
1280 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1281 BNX2FC_MAX_XID);
1282 if (!hba->cmd_mgr) {
1283 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1284 goto cmgr_err;
1285 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001286
1287 init_waitqueue_head(&hba->shutdown_wait);
1288 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001289 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001290
1291 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001292
1293cmgr_err:
1294 kfree(hba->tgt_ofld_list);
1295tgtofld_err:
1296 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001297bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001298 kfree(hba);
1299 return NULL;
1300}
1301
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001302struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1303 struct net_device *netdev,
1304 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001305{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001306 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001307 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001308
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001309 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1310 if (!interface) {
1311 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1312 return NULL;
1313 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001314 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001315 kref_init(&interface->kref);
1316 interface->hba = hba;
1317 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001318
1319 /* Initialize FIP */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001320 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1321 interface->ctlr.send = bnx2fc_fip_send;
1322 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1323 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1324 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001325
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001326 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001327 if (!rc)
1328 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001329
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001330 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001331 dev_put(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001332 kfree(interface);
1333 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001334}
1335
1336/**
1337 * bnx2fc_if_create - Create FCoE instance on a given interface
1338 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001339 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001340 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1341 * @npiv: Indicates if the port is vport or not
1342 *
1343 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1344 *
1345 * Returns: Allocated fc_lport or an error pointer
1346 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001347static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001348 struct device *parent, int npiv)
1349{
Vasu Dev134a4e22011-04-28 15:55:44 -07001350 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001351 struct fcoe_port *port;
1352 struct Scsi_Host *shost;
1353 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001354 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001355 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001356 int rc = 0;
1357
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001358 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1359 if (!blport) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001360 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001361 return NULL;
1362 }
1363
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001364 /* Allocate Scsi_Host structure */
Vasu Dev134a4e22011-04-28 15:55:44 -07001365 if (!npiv)
1366 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1367 else
1368 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001369
1370 if (!lport) {
1371 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001372 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001373 }
1374 shost = lport->host;
1375 port = lport_priv(lport);
1376 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001377 port->priv = interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001378 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1379
1380 /* Configure fcoe_port */
1381 rc = bnx2fc_lport_config(lport);
1382 if (rc)
1383 goto lp_config_err;
1384
1385 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001386 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1387 vport->node_name, vport->port_name);
1388 fc_set_wwnn(lport, vport->node_name);
1389 fc_set_wwpn(lport, vport->port_name);
1390 }
1391 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001392 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001393 if (rc) {
1394 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1395 goto lp_config_err;
1396 }
1397
1398 rc = bnx2fc_shost_config(lport, parent);
1399 if (rc) {
1400 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001401 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001402 goto lp_config_err;
1403 }
1404
1405 /* Initialize the libfc library */
1406 rc = bnx2fc_libfc_config(lport);
1407 if (rc) {
1408 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1409 goto shost_err;
1410 }
1411 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1412
1413 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001414 if (!npiv)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001415 rc = bnx2fc_em_config(lport);
Vasu Dev134a4e22011-04-28 15:55:44 -07001416 else {
1417 shost = vport_to_shost(vport);
1418 n_port = shost_priv(shost);
1419 rc = fc_exch_mgr_list_clone(n_port, lport);
1420 }
1421
1422 if (rc) {
1423 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1424 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001425 }
1426
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001427 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001428
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001429 hba = interface->hba;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001430 spin_lock_bh(&hba->hba_lock);
1431 blport->lport = lport;
1432 list_add_tail(&blport->list, &hba->vports);
1433 spin_unlock_bh(&hba->hba_lock);
1434
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001435 return lport;
1436
1437shost_err:
1438 scsi_remove_host(shost);
1439lp_config_err:
1440 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001441free_blport:
1442 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001443 return NULL;
1444}
1445
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001446static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1447{
1448 /* Dont listen for Ethernet packets anymore */
1449 __dev_remove_pack(&interface->fcoe_packet_type);
1450 __dev_remove_pack(&interface->fip_packet_type);
1451 synchronize_net();
1452}
1453
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001454static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001455{
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001456 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001457 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001458 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001459
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001460 /* Stop the transmit retry timer */
1461 del_timer_sync(&port->timer);
1462
1463 /* Free existing transmit skbs */
1464 fcoe_clean_pending_queue(lport);
1465
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001466 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001467
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001468 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001469}
1470
1471static void bnx2fc_if_destroy(struct fc_lport *lport)
1472{
1473
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001474 /* Free queued packets for the receive thread */
1475 bnx2fc_clean_rx_queue(lport);
1476
1477 /* Detach from scsi-ml */
1478 fc_remove_host(lport->host);
1479 scsi_remove_host(lport->host);
1480
1481 /*
1482 * Note that only the physical lport will have the exchange manager.
1483 * for vports, this function is NOP
1484 */
1485 fc_exch_mgr_free(lport);
1486
1487 /* Free memory used by statistical counters */
1488 fc_lport_free_stats(lport);
1489
1490 /* Release Scsi_Host */
1491 scsi_host_put(lport->host);
1492}
1493
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001494static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001495{
1496 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001497 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001498
1499 bnx2fc_interface_cleanup(interface);
1500 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001501 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001502 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001503 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001504}
1505
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001506/**
1507 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1508 *
1509 * @buffer: The name of the Ethernet interface to be destroyed
1510 * @kp: The associated kernel parameter
1511 *
1512 * Called from sysfs.
1513 *
1514 * Returns: 0 for success
1515 */
1516static int bnx2fc_destroy(struct net_device *netdev)
1517{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001518 struct bnx2fc_interface *interface = NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001519 int rc = 0;
1520
Nithin Sujir6702ca12011-03-17 17:13:27 -07001521 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001522 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001523
1524 interface = bnx2fc_interface_lookup(netdev);
1525 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001526 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001527 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001528 goto netdev_err;
1529 }
1530
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001531
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001532 destroy_workqueue(interface->timer_work_queue);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001533 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001534
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001535netdev_err:
1536 mutex_unlock(&bnx2fc_dev_lock);
1537 rtnl_unlock();
1538 return rc;
1539}
1540
1541static void bnx2fc_destroy_work(struct work_struct *work)
1542{
1543 struct fcoe_port *port;
1544 struct fc_lport *lport;
1545
1546 port = container_of(work, struct fcoe_port, destroy_work);
1547 lport = port->lport;
1548
1549 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1550
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001551 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001552}
1553
1554static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1555{
1556 bnx2fc_free_fw_resc(hba);
1557 bnx2fc_free_task_ctx(hba);
1558}
1559
1560/**
1561 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1562 * pci structure
1563 *
1564 * @hba: Adapter instance
1565 */
1566static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1567{
1568 if (bnx2fc_setup_task_ctx(hba))
1569 goto mem_err;
1570
1571 if (bnx2fc_setup_fw_resc(hba))
1572 goto mem_err;
1573
1574 return 0;
1575mem_err:
1576 bnx2fc_unbind_adapter_devices(hba);
1577 return -ENOMEM;
1578}
1579
1580static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1581{
1582 struct cnic_dev *cnic;
1583
1584 if (!hba->cnic) {
1585 printk(KERN_ERR PFX "cnic is NULL\n");
1586 return -ENODEV;
1587 }
1588 cnic = hba->cnic;
1589 hba->pcidev = cnic->pcidev;
1590 if (hba->pcidev)
1591 pci_dev_get(hba->pcidev);
1592
1593 return 0;
1594}
1595
1596static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1597{
1598 if (hba->pcidev)
1599 pci_dev_put(hba->pcidev);
1600 hba->pcidev = NULL;
1601}
1602
1603
1604
1605/**
1606 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1607 *
1608 * @handle: transport handle pointing to adapter struture
1609 *
1610 * This function maps adapter structure to pcidev structure and initiates
1611 * firmware handshake to enable/initialize on-chip FCoE components.
1612 * This bnx2fc - cnic interface api callback is used after following
1613 * conditions are met -
1614 * a) underlying network interface is up (marked by event NETDEV_UP
1615 * from netdev
1616 * b) bnx2fc adatper structure is registered.
1617 */
1618static void bnx2fc_ulp_start(void *handle)
1619{
1620 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001621 struct bnx2fc_interface *interface;
1622 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001623
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001624 mutex_lock(&bnx2fc_dev_lock);
1625
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001626 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001627 bnx2fc_fw_init(hba);
1628
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001629 BNX2FC_MISC_DBG("bnx2fc started.\n");
1630
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001631 list_for_each_entry(interface, &if_list, list) {
1632 if (interface->hba == hba) {
1633 lport = interface->ctlr.lp;
1634 /* Kick off Fabric discovery*/
1635 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1636 lport->tt.frame_send = bnx2fc_xmit;
1637 bnx2fc_start_disc(interface);
1638 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001639 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001640
1641 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001642}
1643
1644static void bnx2fc_port_shutdown(struct fc_lport *lport)
1645{
1646 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1647 fc_fabric_logoff(lport);
1648 fc_lport_destroy(lport);
1649}
1650
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001651static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001652{
1653 struct fc_lport *lport;
1654 struct fc_lport *vport;
1655
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001656 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1657 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001658
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001659 lport = interface->ctlr.lp;
1660 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001661
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001662 mutex_lock(&lport->lp_mutex);
1663 list_for_each_entry(vport, &lport->vports, list)
1664 fc_host_port_type(vport->host) =
1665 FC_PORTTYPE_UNKNOWN;
1666 mutex_unlock(&lport->lp_mutex);
1667 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1668 fcoe_ctlr_link_down(&interface->ctlr);
1669 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001670}
1671
1672static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1673{
1674#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1675 int rc = -1;
1676 int i = HZ;
1677
1678 rc = bnx2fc_bind_adapter_devices(hba);
1679 if (rc) {
1680 printk(KERN_ALERT PFX
1681 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1682 goto err_out;
1683 }
1684
1685 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1686 if (rc) {
1687 printk(KERN_ALERT PFX
1688 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1689 goto err_unbind;
1690 }
1691
1692 /*
1693 * Wait until the adapter init message is complete, and adapter
1694 * state is UP.
1695 */
1696 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1697 msleep(BNX2FC_INIT_POLL_TIME);
1698
1699 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1700 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1701 "Ignoring...\n",
1702 hba->cnic->netdev->name);
1703 rc = -1;
1704 goto err_unbind;
1705 }
1706
1707
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001708 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001709 return 0;
1710
1711err_unbind:
1712 bnx2fc_unbind_adapter_devices(hba);
1713err_out:
1714 return rc;
1715}
1716
1717static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1718{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001719 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001720 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1721 init_timer(&hba->destroy_timer);
1722 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1723 jiffies;
1724 hba->destroy_timer.function = bnx2fc_destroy_timer;
1725 hba->destroy_timer.data = (unsigned long)hba;
1726 add_timer(&hba->destroy_timer);
1727 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001728 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1729 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001730 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001731 /* This should never happen */
1732 if (signal_pending(current))
1733 flush_signals(current);
1734
1735 del_timer_sync(&hba->destroy_timer);
1736 }
1737 bnx2fc_unbind_adapter_devices(hba);
1738 }
1739}
1740
1741/**
1742 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1743 *
1744 * @handle: transport handle pointing to adapter structure
1745 *
1746 * Driver checks if adapter is already in shutdown mode, if not start
1747 * the shutdown process.
1748 */
1749static void bnx2fc_ulp_stop(void *handle)
1750{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001751 struct bnx2fc_hba *hba = handle;
1752 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001753
1754 printk(KERN_ERR "ULP_STOP\n");
1755
1756 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001757 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1758 goto exit;
1759 list_for_each_entry(interface, &if_list, list) {
1760 if (interface->hba == hba)
1761 bnx2fc_stop(interface);
1762 }
1763 BUG_ON(hba->num_ofld_sess != 0);
1764
1765 mutex_lock(&hba->hba_mutex);
1766 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1767 clear_bit(ADAPTER_STATE_GOING_DOWN,
1768 &hba->adapter_state);
1769
1770 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1771 mutex_unlock(&hba->hba_mutex);
1772
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001773 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001774exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001775 mutex_unlock(&bnx2fc_dev_lock);
1776}
1777
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001778static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001779{
1780 struct fc_lport *lport;
1781 int wait_cnt = 0;
1782
1783 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1784 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001785 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001786 printk(KERN_ERR PFX "Init not done yet\n");
1787 return;
1788 }
1789
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001790 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001791 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1792
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001793 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001794 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001795 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001796 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001797 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001798 }
1799
1800 /* wait for the FCF to be selected before issuing FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001801 while (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001802 msleep(250);
1803 /* give up after 3 secs */
1804 if (++wait_cnt > 12)
1805 break;
1806 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001807
1808 /* Reset max receive frame size to default */
1809 if (fc_set_mfs(lport, BNX2FC_MFS))
1810 return;
1811
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001812 fc_lport_init(lport);
1813 fc_fabric_login(lport);
1814}
1815
1816
1817/**
1818 * bnx2fc_ulp_init - Initialize an adapter instance
1819 *
1820 * @dev : cnic device handle
1821 * Called from cnic_register_driver() context to initialize all
1822 * enumerated cnic devices. This routine allocates adapter structure
1823 * and other device specific resources.
1824 */
1825static void bnx2fc_ulp_init(struct cnic_dev *dev)
1826{
1827 struct bnx2fc_hba *hba;
1828 int rc = 0;
1829
1830 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1831 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001832 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1833 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001834 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001835 " flags: %lx fcoe_conn: %d\n",
1836 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001837 return;
1838 }
1839
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001840 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001841 if (!hba) {
1842 printk(KERN_ERR PFX "hba initialization failed\n");
1843 return;
1844 }
1845
1846 /* Add HBA to the adapter list */
1847 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001848 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001849 adapter_count++;
1850 mutex_unlock(&bnx2fc_dev_lock);
1851
1852 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1853 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1854 (void *) hba);
1855 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001856 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001857 else
1858 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1859}
1860
1861
1862static int bnx2fc_disable(struct net_device *netdev)
1863{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001864 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001865 int rc = 0;
1866
Nithin Sujir6702ca12011-03-17 17:13:27 -07001867 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001868 mutex_lock(&bnx2fc_dev_lock);
1869
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001870 interface = bnx2fc_interface_lookup(netdev);
1871 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001872 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001873 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001874 } else {
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001875 interface->enabled = false;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001876 fcoe_ctlr_link_down(&interface->ctlr);
1877 fcoe_clean_pending_queue(interface->ctlr.lp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001878 }
1879
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001880 mutex_unlock(&bnx2fc_dev_lock);
1881 rtnl_unlock();
1882 return rc;
1883}
1884
1885
1886static int bnx2fc_enable(struct net_device *netdev)
1887{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001888 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001889 int rc = 0;
1890
Nithin Sujir6702ca12011-03-17 17:13:27 -07001891 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001892 mutex_lock(&bnx2fc_dev_lock);
1893
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001894 interface = bnx2fc_interface_lookup(netdev);
1895 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001896 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001897 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001898 } else if (!bnx2fc_link_ok(interface->ctlr.lp)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001899 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001900 interface->enabled = true;
1901 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001902
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001903 mutex_unlock(&bnx2fc_dev_lock);
1904 rtnl_unlock();
1905 return rc;
1906}
1907
1908/**
1909 * bnx2fc_create - Create bnx2fc FCoE interface
1910 *
1911 * @buffer: The name of Ethernet interface to create on
1912 * @kp: The associated kernel param
1913 *
1914 * Called from sysfs.
1915 *
1916 * Returns: 0 for success
1917 */
1918static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1919{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001920 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001921 struct bnx2fc_hba *hba;
1922 struct net_device *phys_dev;
1923 struct fc_lport *lport;
1924 struct ethtool_drvinfo drvinfo;
1925 int rc = 0;
1926 int vlan_id;
1927
1928 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1929 if (fip_mode != FIP_MODE_FABRIC) {
1930 printk(KERN_ERR "fip mode not FABRIC\n");
1931 return -EIO;
1932 }
1933
Nithin Sujir6702ca12011-03-17 17:13:27 -07001934 rtnl_lock();
1935
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001936 mutex_lock(&bnx2fc_dev_lock);
1937
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001938 if (!try_module_get(THIS_MODULE)) {
1939 rc = -EINVAL;
1940 goto mod_err;
1941 }
1942
1943 /* obtain physical netdev */
1944 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1945 phys_dev = vlan_dev_real_dev(netdev);
1946 vlan_id = vlan_dev_vlan_id(netdev);
1947 } else {
1948 printk(KERN_ERR PFX "Not a vlan device\n");
1949 rc = -EINVAL;
1950 goto netdev_err;
1951 }
1952 /* verify if the physical device is a netxtreme2 device */
1953 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1954 memset(&drvinfo, 0, sizeof(drvinfo));
1955 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001956 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001957 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1958 rc = -EINVAL;
1959 goto netdev_err;
1960 }
1961 } else {
1962 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1963 rc = -EINVAL;
1964 goto netdev_err;
1965 }
1966
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001967 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001968 hba = bnx2fc_hba_lookup(phys_dev);
1969 if (!hba) {
1970 rc = -ENODEV;
1971 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1972 goto netdev_err;
1973 }
1974
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001975 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001976 rc = -EEXIST;
1977 goto netdev_err;
1978 }
1979
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001980 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
1981 if (!interface) {
1982 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001983 goto ifput_err;
1984 }
1985
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001986 interface->vlan_id = vlan_id;
1987 interface->vlan_enabled = 1;
1988
1989 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001990 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001991 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001992 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
1993 rc = -EINVAL;
1994 goto ifput_err;
1995 }
1996
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001997 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001998 if (!lport) {
1999 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2000 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002001 rc = -EINVAL;
2002 goto if_create_err;
2003 }
2004
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002005 /* Add interface to if_list */
2006 list_add_tail(&interface->list, &if_list);
2007
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002008 lport->boot_time = jiffies;
2009
2010 /* Make this master N_port */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002011 interface->ctlr.lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002012
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002013 if (!bnx2fc_link_ok(lport)) {
2014 fcoe_ctlr_link_up(&interface->ctlr);
2015 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2016 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2017 }
2018
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002019 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2020 bnx2fc_start_disc(interface);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002021 interface->enabled = true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002022 /*
2023 * Release from kref_init in bnx2fc_interface_setup, on success
2024 * lport should be holding a reference taken in bnx2fc_if_create
2025 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002026 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002027 /* put netdev that was held while calling dev_get_by_name */
2028 mutex_unlock(&bnx2fc_dev_lock);
2029 rtnl_unlock();
2030 return 0;
2031
2032if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002033 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002034ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002035 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002036 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002037netdev_err:
2038 module_put(THIS_MODULE);
2039mod_err:
2040 mutex_unlock(&bnx2fc_dev_lock);
2041 rtnl_unlock();
2042 return rc;
2043}
2044
2045/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002046 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002047 *
2048 * @cnic: Pointer to cnic device instance
2049 *
2050 **/
2051static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2052{
2053 struct list_head *list;
2054 struct list_head *temp;
2055 struct bnx2fc_hba *hba;
2056
2057 /* Called with bnx2fc_dev_lock held */
2058 list_for_each_safe(list, temp, &adapter_list) {
2059 hba = (struct bnx2fc_hba *)list;
2060 if (hba->cnic == cnic)
2061 return hba;
2062 }
2063 return NULL;
2064}
2065
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002066static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2067 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002068{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002069 struct bnx2fc_interface *interface;
2070
2071 /* Called with bnx2fc_dev_lock held */
2072 list_for_each_entry(interface, &if_list, list) {
2073 if (interface->netdev == netdev)
2074 return interface;
2075 }
2076 return NULL;
2077}
2078
2079static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2080 *phys_dev)
2081{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002082 struct bnx2fc_hba *hba;
2083
2084 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002085 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002086 if (hba->phys_dev == phys_dev)
2087 return hba;
2088 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002089 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002090 return NULL;
2091}
2092
2093/**
2094 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2095 *
2096 * @dev cnic device handle
2097 */
2098static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2099{
2100 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002101 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002102
2103 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2104
2105 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2106 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2107 dev->netdev->name, dev->flags);
2108 return;
2109 }
2110
2111 mutex_lock(&bnx2fc_dev_lock);
2112 hba = bnx2fc_find_hba_for_cnic(dev);
2113 if (!hba) {
2114 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2115 dev);
2116 mutex_unlock(&bnx2fc_dev_lock);
2117 return;
2118 }
2119
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002120 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002121 adapter_count--;
2122
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002123 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002124 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002125 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002126 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002127 mutex_unlock(&bnx2fc_dev_lock);
2128
2129 bnx2fc_ulp_stop(hba);
2130 /* unregister cnic device */
2131 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2132 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002133 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002134}
2135
2136/**
2137 * bnx2fc_fcoe_reset - Resets the fcoe
2138 *
2139 * @shost: shost the reset is from
2140 *
2141 * Returns: always 0
2142 */
2143static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2144{
2145 struct fc_lport *lport = shost_priv(shost);
2146 fc_lport_reset(lport);
2147 return 0;
2148}
2149
2150
2151static bool bnx2fc_match(struct net_device *netdev)
2152{
2153 mutex_lock(&bnx2fc_dev_lock);
2154 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2155 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2156
2157 if (bnx2fc_hba_lookup(phys_dev)) {
2158 mutex_unlock(&bnx2fc_dev_lock);
2159 return true;
2160 }
2161 }
2162 mutex_unlock(&bnx2fc_dev_lock);
2163 return false;
2164}
2165
2166
2167static struct fcoe_transport bnx2fc_transport = {
2168 .name = {"bnx2fc"},
2169 .attached = false,
2170 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2171 .match = bnx2fc_match,
2172 .create = bnx2fc_create,
2173 .destroy = bnx2fc_destroy,
2174 .enable = bnx2fc_enable,
2175 .disable = bnx2fc_disable,
2176};
2177
2178/**
2179 * bnx2fc_percpu_thread_create - Create a receive thread for an
2180 * online CPU
2181 *
2182 * @cpu: cpu index for the online cpu
2183 */
2184static void bnx2fc_percpu_thread_create(unsigned int cpu)
2185{
2186 struct bnx2fc_percpu_s *p;
2187 struct task_struct *thread;
2188
2189 p = &per_cpu(bnx2fc_percpu, cpu);
2190
2191 thread = kthread_create(bnx2fc_percpu_io_thread,
2192 (void *)p,
2193 "bnx2fc_thread/%d", cpu);
2194 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002195 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002196 kthread_bind(thread, cpu);
2197 p->iothread = thread;
2198 wake_up_process(thread);
2199 }
2200}
2201
2202static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2203{
2204 struct bnx2fc_percpu_s *p;
2205 struct task_struct *thread;
2206 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002207
2208 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2209
2210 /* Prevent any new work from being queued for this CPU */
2211 p = &per_cpu(bnx2fc_percpu, cpu);
2212 spin_lock_bh(&p->fp_work_lock);
2213 thread = p->iothread;
2214 p->iothread = NULL;
2215
2216
2217 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002218 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002219 list_del_init(&work->list);
2220 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2221 kfree(work);
2222 }
2223
2224 spin_unlock_bh(&p->fp_work_lock);
2225
2226 if (thread)
2227 kthread_stop(thread);
2228}
2229
2230/**
2231 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2232 *
2233 * @nfb: The callback data block
2234 * @action: The event triggering the callback
2235 * @hcpu: The index of the CPU that the event is for
2236 *
2237 * This creates or destroys per-CPU data for fcoe
2238 *
2239 * Returns NOTIFY_OK always.
2240 */
2241static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2242 unsigned long action, void *hcpu)
2243{
2244 unsigned cpu = (unsigned long)hcpu;
2245
2246 switch (action) {
2247 case CPU_ONLINE:
2248 case CPU_ONLINE_FROZEN:
2249 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2250 bnx2fc_percpu_thread_create(cpu);
2251 break;
2252 case CPU_DEAD:
2253 case CPU_DEAD_FROZEN:
2254 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2255 bnx2fc_percpu_thread_destroy(cpu);
2256 break;
2257 default:
2258 break;
2259 }
2260 return NOTIFY_OK;
2261}
2262
2263/**
2264 * bnx2fc_mod_init - module init entry point
2265 *
2266 * Initialize driver wide global data structures, and register
2267 * with cnic module
2268 **/
2269static int __init bnx2fc_mod_init(void)
2270{
2271 struct fcoe_percpu_s *bg;
2272 struct task_struct *l2_thread;
2273 int rc = 0;
2274 unsigned int cpu = 0;
2275 struct bnx2fc_percpu_s *p;
2276
2277 printk(KERN_INFO PFX "%s", version);
2278
2279 /* register as a fcoe transport */
2280 rc = fcoe_transport_attach(&bnx2fc_transport);
2281 if (rc) {
2282 printk(KERN_ERR "failed to register an fcoe transport, check "
2283 "if libfcoe is loaded\n");
2284 goto out;
2285 }
2286
2287 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002288 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002289 mutex_init(&bnx2fc_dev_lock);
2290 adapter_count = 0;
2291
2292 /* Attach FC transport template */
2293 rc = bnx2fc_attach_transport();
2294 if (rc)
2295 goto detach_ft;
2296
2297 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2298 if (!bnx2fc_wq) {
2299 rc = -ENOMEM;
2300 goto release_bt;
2301 }
2302
2303 bg = &bnx2fc_global;
2304 skb_queue_head_init(&bg->fcoe_rx_list);
2305 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2306 (void *)bg,
2307 "bnx2fc_l2_thread");
2308 if (IS_ERR(l2_thread)) {
2309 rc = PTR_ERR(l2_thread);
2310 goto free_wq;
2311 }
2312 wake_up_process(l2_thread);
2313 spin_lock_bh(&bg->fcoe_rx_list.lock);
2314 bg->thread = l2_thread;
2315 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2316
2317 for_each_possible_cpu(cpu) {
2318 p = &per_cpu(bnx2fc_percpu, cpu);
2319 INIT_LIST_HEAD(&p->work_list);
2320 spin_lock_init(&p->fp_work_lock);
2321 }
2322
2323 for_each_online_cpu(cpu) {
2324 bnx2fc_percpu_thread_create(cpu);
2325 }
2326
2327 /* Initialize per CPU interrupt thread */
2328 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2329
2330 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2331
2332 return 0;
2333
2334free_wq:
2335 destroy_workqueue(bnx2fc_wq);
2336release_bt:
2337 bnx2fc_release_transport();
2338detach_ft:
2339 fcoe_transport_detach(&bnx2fc_transport);
2340out:
2341 return rc;
2342}
2343
2344static void __exit bnx2fc_mod_exit(void)
2345{
2346 LIST_HEAD(to_be_deleted);
2347 struct bnx2fc_hba *hba, *next;
2348 struct fcoe_percpu_s *bg;
2349 struct task_struct *l2_thread;
2350 struct sk_buff *skb;
2351 unsigned int cpu = 0;
2352
2353 /*
2354 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2355 * it will have higher precedence than bnx2fc_dev_lock.
2356 * unregister_device() cannot be called with bnx2fc_dev_lock
2357 * held.
2358 */
2359 mutex_lock(&bnx2fc_dev_lock);
2360 list_splice(&adapter_list, &to_be_deleted);
2361 INIT_LIST_HEAD(&adapter_list);
2362 adapter_count = 0;
2363 mutex_unlock(&bnx2fc_dev_lock);
2364
2365 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002366 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2367 list_del_init(&hba->list);
2368 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2369 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002370 bnx2fc_ulp_stop(hba);
2371 /* unregister cnic device */
2372 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2373 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002374 hba->cnic->unregister_device(hba->cnic,
2375 CNIC_ULP_FCOE);
2376 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002377 }
2378 cnic_unregister_driver(CNIC_ULP_FCOE);
2379
2380 /* Destroy global thread */
2381 bg = &bnx2fc_global;
2382 spin_lock_bh(&bg->fcoe_rx_list.lock);
2383 l2_thread = bg->thread;
2384 bg->thread = NULL;
2385 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2386 kfree_skb(skb);
2387
2388 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2389
2390 if (l2_thread)
2391 kthread_stop(l2_thread);
2392
2393 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2394
2395 /* Destroy per cpu threads */
2396 for_each_online_cpu(cpu) {
2397 bnx2fc_percpu_thread_destroy(cpu);
2398 }
2399
2400 destroy_workqueue(bnx2fc_wq);
2401 /*
2402 * detach from scsi transport
2403 * must happen after all destroys are done
2404 */
2405 bnx2fc_release_transport();
2406
2407 /* detach from fcoe transport */
2408 fcoe_transport_detach(&bnx2fc_transport);
2409}
2410
2411module_init(bnx2fc_mod_init);
2412module_exit(bnx2fc_mod_exit);
2413
2414static struct fc_function_template bnx2fc_transport_function = {
2415 .show_host_node_name = 1,
2416 .show_host_port_name = 1,
2417 .show_host_supported_classes = 1,
2418 .show_host_supported_fc4s = 1,
2419 .show_host_active_fc4s = 1,
2420 .show_host_maxframe_size = 1,
2421
2422 .show_host_port_id = 1,
2423 .show_host_supported_speeds = 1,
2424 .get_host_speed = fc_get_host_speed,
2425 .show_host_speed = 1,
2426 .show_host_port_type = 1,
2427 .get_host_port_state = fc_get_host_port_state,
2428 .show_host_port_state = 1,
2429 .show_host_symbolic_name = 1,
2430
2431 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2432 sizeof(struct bnx2fc_rport)),
2433 .show_rport_maxframe_size = 1,
2434 .show_rport_supported_classes = 1,
2435
2436 .show_host_fabric_name = 1,
2437 .show_starget_node_name = 1,
2438 .show_starget_port_name = 1,
2439 .show_starget_port_id = 1,
2440 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2441 .show_rport_dev_loss_tmo = 1,
2442 .get_fc_host_stats = bnx2fc_get_host_stats,
2443
2444 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2445
2446 .terminate_rport_io = fc_rport_terminate_io,
2447
2448 .vport_create = bnx2fc_vport_create,
2449 .vport_delete = bnx2fc_vport_destroy,
2450 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002451 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002452};
2453
2454static struct fc_function_template bnx2fc_vport_xport_function = {
2455 .show_host_node_name = 1,
2456 .show_host_port_name = 1,
2457 .show_host_supported_classes = 1,
2458 .show_host_supported_fc4s = 1,
2459 .show_host_active_fc4s = 1,
2460 .show_host_maxframe_size = 1,
2461
2462 .show_host_port_id = 1,
2463 .show_host_supported_speeds = 1,
2464 .get_host_speed = fc_get_host_speed,
2465 .show_host_speed = 1,
2466 .show_host_port_type = 1,
2467 .get_host_port_state = fc_get_host_port_state,
2468 .show_host_port_state = 1,
2469 .show_host_symbolic_name = 1,
2470
2471 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2472 sizeof(struct bnx2fc_rport)),
2473 .show_rport_maxframe_size = 1,
2474 .show_rport_supported_classes = 1,
2475
2476 .show_host_fabric_name = 1,
2477 .show_starget_node_name = 1,
2478 .show_starget_port_name = 1,
2479 .show_starget_port_id = 1,
2480 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2481 .show_rport_dev_loss_tmo = 1,
2482 .get_fc_host_stats = fc_get_host_stats,
2483 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2484 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002485 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002486};
2487
2488/**
2489 * scsi_host_template structure used while registering with SCSI-ml
2490 */
2491static struct scsi_host_template bnx2fc_shost_template = {
2492 .module = THIS_MODULE,
2493 .name = "Broadcom Offload FCoE Initiator",
2494 .queuecommand = bnx2fc_queuecommand,
2495 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2496 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2497 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2498 .eh_host_reset_handler = fc_eh_host_reset,
2499 .slave_alloc = fc_slave_alloc,
2500 .change_queue_depth = fc_change_queue_depth,
2501 .change_queue_type = fc_change_queue_type,
2502 .this_id = -1,
2503 .cmd_per_lun = 3,
Bhanu Gollapudi0ea5c272011-03-17 17:13:29 -07002504 .can_queue = BNX2FC_CAN_QUEUE,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002505 .use_clustering = ENABLE_CLUSTERING,
2506 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2507 .max_sectors = 512,
2508};
2509
2510static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2511 .frame_send = bnx2fc_xmit,
2512 .elsct_send = bnx2fc_elsct_send,
2513 .fcp_abort_io = bnx2fc_abort_io,
2514 .fcp_cleanup = bnx2fc_cleanup,
2515 .rport_event_callback = bnx2fc_rport_event_handler,
2516};
2517
2518/**
2519 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2520 * structure carrying callback function pointers
2521 */
2522static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2523 .owner = THIS_MODULE,
2524 .cnic_init = bnx2fc_ulp_init,
2525 .cnic_exit = bnx2fc_ulp_exit,
2526 .cnic_start = bnx2fc_ulp_start,
2527 .cnic_stop = bnx2fc_ulp_stop,
2528 .indicate_kcqes = bnx2fc_indicate_kcqe,
2529 .indicate_netevent = bnx2fc_indicate_netevent,
2530};