blob: 0f1ca86aff9f01ae4a5d4fbb39de64c222e9d7d5 [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
Bhanu Prakash Gollapudif72f69792011-10-03 16:45:02 -0700102static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
103{
104 return ((struct bnx2fc_interface *)
105 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
106}
107
108/**
109 * bnx2fc_get_lesb() - Fill the FCoE Link Error Status Block
110 * @lport: the local port
111 * @fc_lesb: the link error status block
112 */
113static void bnx2fc_get_lesb(struct fc_lport *lport,
114 struct fc_els_lesb *fc_lesb)
115{
116 struct net_device *netdev = bnx2fc_netdev(lport);
117
118 __fcoe_get_lesb(lport, fc_lesb, netdev);
119}
120
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800121static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
122{
123 struct fcoe_percpu_s *bg;
124 struct fcoe_rcv_info *fr;
125 struct sk_buff_head *list;
126 struct sk_buff *skb, *next;
127 struct sk_buff *head;
128
129 bg = &bnx2fc_global;
130 spin_lock_bh(&bg->fcoe_rx_list.lock);
131 list = &bg->fcoe_rx_list;
132 head = list->next;
133 for (skb = head; skb != (struct sk_buff *)list;
134 skb = next) {
135 next = skb->next;
136 fr = fcoe_dev_from_skb(skb);
137 if (fr->fr_dev == lp) {
138 __skb_unlink(skb, list);
139 kfree_skb(skb);
140 }
141 }
142 spin_unlock_bh(&bg->fcoe_rx_list.lock);
143}
144
145int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
146{
147 int rc;
148 spin_lock(&bnx2fc_global_lock);
149 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
150 spin_unlock(&bnx2fc_global_lock);
151
152 return rc;
153}
154
155static void bnx2fc_abort_io(struct fc_lport *lport)
156{
157 /*
158 * This function is no-op for bnx2fc, but we do
159 * not want to leave it as NULL either, as libfc
160 * can call the default function which is
161 * fc_fcp_abort_io.
162 */
163}
164
165static void bnx2fc_cleanup(struct fc_lport *lport)
166{
167 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700168 struct bnx2fc_interface *interface = port->priv;
169 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800170 struct bnx2fc_rport *tgt;
171 int i;
172
173 BNX2FC_MISC_DBG("Entered %s\n", __func__);
174 mutex_lock(&hba->hba_mutex);
175 spin_lock_bh(&hba->hba_lock);
176 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
177 tgt = hba->tgt_ofld_list[i];
178 if (tgt) {
179 /* Cleanup IOs belonging to requested vport */
180 if (tgt->port == port) {
181 spin_unlock_bh(&hba->hba_lock);
182 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
183 bnx2fc_flush_active_ios(tgt);
184 spin_lock_bh(&hba->hba_lock);
185 }
186 }
187 }
188 spin_unlock_bh(&hba->hba_lock);
189 mutex_unlock(&hba->hba_mutex);
190}
191
192static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
193 struct fc_frame *fp)
194{
195 struct fc_rport_priv *rdata = tgt->rdata;
196 struct fc_frame_header *fh;
197 int rc = 0;
198
199 fh = fc_frame_header_get(fp);
200 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
201 "r_ctl = 0x%x\n", rdata->ids.port_id,
202 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
203 if ((fh->fh_type == FC_TYPE_ELS) &&
204 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
205
206 switch (fc_frame_payload_op(fp)) {
207 case ELS_ADISC:
208 rc = bnx2fc_send_adisc(tgt, fp);
209 break;
210 case ELS_LOGO:
211 rc = bnx2fc_send_logo(tgt, fp);
212 break;
213 case ELS_RLS:
214 rc = bnx2fc_send_rls(tgt, fp);
215 break;
216 default:
217 break;
218 }
219 } else if ((fh->fh_type == FC_TYPE_BLS) &&
220 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
221 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
222 else {
223 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
224 "rctl 0x%x thru non-offload path\n",
225 fh->fh_type, fh->fh_r_ctl);
226 return -ENODEV;
227 }
228 if (rc)
229 return -ENOMEM;
230 else
231 return 0;
232}
233
234/**
235 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
236 *
237 * @lport: the associated local port
238 * @fp: the fc_frame to be transmitted
239 */
240static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
241{
242 struct ethhdr *eh;
243 struct fcoe_crc_eof *cp;
244 struct sk_buff *skb;
245 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700246 struct bnx2fc_interface *interface;
247 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800248 struct fcoe_port *port;
249 struct fcoe_hdr *hp;
250 struct bnx2fc_rport *tgt;
251 struct fcoe_dev_stats *stats;
252 u8 sof, eof;
253 u32 crc;
254 unsigned int hlen, tlen, elen;
255 int wlen, rc = 0;
256
257 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700258 interface = port->priv;
259 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800260
261 fh = fc_frame_header_get(fp);
262
263 skb = fp_skb(fp);
264 if (!lport->link_up) {
265 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
266 kfree_skb(skb);
267 return 0;
268 }
269
270 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700271 if (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800272 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
273 kfree_skb(skb);
274 return -EINVAL;
275 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700276 if (fcoe_ctlr_els_send(&interface->ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800277 return 0;
278 }
279
280 sof = fr_sof(fp);
281 eof = fr_eof(fp);
282
283 /*
284 * Snoop the frame header to check if the frame is for
285 * an offloaded session
286 */
287 /*
288 * tgt_ofld_list access is synchronized using
289 * both hba mutex and hba lock. Atleast hba mutex or
290 * hba lock needs to be held for read access.
291 */
292
293 spin_lock_bh(&hba->hba_lock);
294 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
295 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
296 /* This frame is for offloaded session */
297 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
298 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
299 spin_unlock_bh(&hba->hba_lock);
300 rc = bnx2fc_xmit_l2_frame(tgt, fp);
301 if (rc != -ENODEV) {
302 kfree_skb(skb);
303 return rc;
304 }
305 } else {
306 spin_unlock_bh(&hba->hba_lock);
307 }
308
309 elen = sizeof(struct ethhdr);
310 hlen = sizeof(struct fcoe_hdr);
311 tlen = sizeof(struct fcoe_crc_eof);
312 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
313
314 skb->ip_summed = CHECKSUM_NONE;
315 crc = fcoe_fc_crc(fp);
316
317 /* copy port crc and eof to the skb buff */
318 if (skb_is_nonlinear(skb)) {
319 skb_frag_t *frag;
320 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
321 kfree_skb(skb);
322 return -ENOMEM;
323 }
324 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
325 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
326 + frag->page_offset;
327 } else {
328 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
329 }
330
331 memset(cp, 0, sizeof(*cp));
332 cp->fcoe_eof = eof;
333 cp->fcoe_crc32 = cpu_to_le32(~crc);
334 if (skb_is_nonlinear(skb)) {
335 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
336 cp = NULL;
337 }
338
339 /* adjust skb network/transport offsets to match mac/fcoe/port */
340 skb_push(skb, elen + hlen);
341 skb_reset_mac_header(skb);
342 skb_reset_network_header(skb);
343 skb->mac_len = elen;
344 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700345 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800346
347 /* fill up mac and fcoe headers */
348 eh = eth_hdr(skb);
349 eh->h_proto = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700350 if (interface->ctlr.map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800351 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
352 else
353 /* insert GW address */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700354 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800355
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700356 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
357 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800358 else
359 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
360
361 hp = (struct fcoe_hdr *)(eh + 1);
362 memset(hp, 0, sizeof(*hp));
363 if (FC_FCOE_VER)
364 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
365 hp->fcoe_sof = sof;
366
367 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
368 if (lport->seq_offload && fr_max_payload(fp)) {
369 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
370 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
371 } else {
372 skb_shinfo(skb)->gso_type = 0;
373 skb_shinfo(skb)->gso_size = 0;
374 }
375
376 /*update tx stats */
377 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
378 stats->TxFrames++;
379 stats->TxWords += wlen;
380 put_cpu();
381
382 /* send down to lld */
383 fr_dev(fp) = lport;
384 if (port->fcoe_pending_queue.qlen)
385 fcoe_check_wait_queue(lport, skb);
386 else if (fcoe_start_io(skb))
387 fcoe_check_wait_queue(lport, skb);
388
389 return 0;
390}
391
392/**
393 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
394 *
395 * @skb: the receive socket buffer
396 * @dev: associated net device
397 * @ptype: context
398 * @olddev: last device
399 *
400 * This function receives the packet and builds FC frame and passes it up
401 */
402static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
403 struct packet_type *ptype, struct net_device *olddev)
404{
405 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700406 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800407 struct fc_frame_header *fh;
408 struct fcoe_rcv_info *fr;
409 struct fcoe_percpu_s *bg;
410 unsigned short oxid;
411
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700412 interface = container_of(ptype, struct bnx2fc_interface,
413 fcoe_packet_type);
414 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800415
416 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700417 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800418 goto err;
419 }
420
421 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700422 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800423 goto err;
424 }
425
426 /*
427 * Check for minimum frame length, and make sure required FCoE
428 * and FC headers are pulled into the linear data area.
429 */
430 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
431 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
432 goto err;
433
434 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
435 fh = (struct fc_frame_header *) skb_transport_header(skb);
436
437 oxid = ntohs(fh->fh_ox_id);
438
439 fr = fcoe_dev_from_skb(skb);
440 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800441
442 bg = &bnx2fc_global;
443 spin_lock_bh(&bg->fcoe_rx_list.lock);
444
445 __skb_queue_tail(&bg->fcoe_rx_list, skb);
446 if (bg->fcoe_rx_list.qlen == 1)
447 wake_up_process(bg->thread);
448
449 spin_unlock_bh(&bg->fcoe_rx_list.lock);
450
451 return 0;
452err:
453 kfree_skb(skb);
454 return -1;
455}
456
457static int bnx2fc_l2_rcv_thread(void *arg)
458{
459 struct fcoe_percpu_s *bg = arg;
460 struct sk_buff *skb;
461
462 set_user_nice(current, -20);
463 set_current_state(TASK_INTERRUPTIBLE);
464 while (!kthread_should_stop()) {
465 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800466 spin_lock_bh(&bg->fcoe_rx_list.lock);
467 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
468 spin_unlock_bh(&bg->fcoe_rx_list.lock);
469 bnx2fc_recv_frame(skb);
470 spin_lock_bh(&bg->fcoe_rx_list.lock);
471 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700472 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800473 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800474 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700475 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800476 return 0;
477}
478
479
480static void bnx2fc_recv_frame(struct sk_buff *skb)
481{
482 u32 fr_len;
483 struct fc_lport *lport;
484 struct fcoe_rcv_info *fr;
485 struct fcoe_dev_stats *stats;
486 struct fc_frame_header *fh;
487 struct fcoe_crc_eof crc_eof;
488 struct fc_frame *fp;
489 struct fc_lport *vn_port;
490 struct fcoe_port *port;
491 u8 *mac = NULL;
492 u8 *dest_mac = NULL;
493 struct fcoe_hdr *hp;
494
495 fr = fcoe_dev_from_skb(skb);
496 lport = fr->fr_dev;
497 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700498 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800499 kfree_skb(skb);
500 return;
501 }
502
503 if (skb_is_nonlinear(skb))
504 skb_linearize(skb);
505 mac = eth_hdr(skb)->h_source;
506 dest_mac = eth_hdr(skb)->h_dest;
507
508 /* Pull the header */
509 hp = (struct fcoe_hdr *) skb_network_header(skb);
510 fh = (struct fc_frame_header *) skb_transport_header(skb);
511 skb_pull(skb, sizeof(struct fcoe_hdr));
512 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
513
514 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
515 stats->RxFrames++;
516 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
517
518 fp = (struct fc_frame *)skb;
519 fc_frame_init(fp);
520 fr_dev(fp) = lport;
521 fr_sof(fp) = hp->fcoe_sof;
522 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
523 put_cpu();
524 kfree_skb(skb);
525 return;
526 }
527 fr_eof(fp) = crc_eof.fcoe_eof;
528 fr_crc(fp) = crc_eof.fcoe_crc32;
529 if (pskb_trim(skb, fr_len)) {
530 put_cpu();
531 kfree_skb(skb);
532 return;
533 }
534
535 fh = fc_frame_header_get(fp);
536
537 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
538 if (vn_port) {
539 port = lport_priv(vn_port);
540 if (compare_ether_addr(port->data_src_addr, dest_mac)
541 != 0) {
542 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
543 put_cpu();
544 kfree_skb(skb);
545 return;
546 }
547 }
548 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
549 fh->fh_type == FC_TYPE_FCP) {
550 /* Drop FCP data. We dont this in L2 path */
551 put_cpu();
552 kfree_skb(skb);
553 return;
554 }
555 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
556 fh->fh_type == FC_TYPE_ELS) {
557 switch (fc_frame_payload_op(fp)) {
558 case ELS_LOGO:
559 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
560 /* drop non-FIP LOGO */
561 put_cpu();
562 kfree_skb(skb);
563 return;
564 }
565 break;
566 }
567 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700568
569 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
570 /* Drop incoming ABTS */
571 put_cpu();
572 kfree_skb(skb);
573 return;
574 }
575
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800576 if (le32_to_cpu(fr_crc(fp)) !=
577 ~crc32(~0, skb->data, fr_len)) {
578 if (stats->InvalidCRCCount < 5)
579 printk(KERN_WARNING PFX "dropping frame with "
580 "CRC error\n");
581 stats->InvalidCRCCount++;
582 put_cpu();
583 kfree_skb(skb);
584 return;
585 }
586 put_cpu();
587 fc_exch_recv(lport, fp);
588}
589
590/**
591 * bnx2fc_percpu_io_thread - thread per cpu for ios
592 *
593 * @arg: ptr to bnx2fc_percpu_info structure
594 */
595int bnx2fc_percpu_io_thread(void *arg)
596{
597 struct bnx2fc_percpu_s *p = arg;
598 struct bnx2fc_work *work, *tmp;
599 LIST_HEAD(work_list);
600
601 set_user_nice(current, -20);
602 set_current_state(TASK_INTERRUPTIBLE);
603 while (!kthread_should_stop()) {
604 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800605 spin_lock_bh(&p->fp_work_lock);
606 while (!list_empty(&p->work_list)) {
607 list_splice_init(&p->work_list, &work_list);
608 spin_unlock_bh(&p->fp_work_lock);
609
610 list_for_each_entry_safe(work, tmp, &work_list, list) {
611 list_del_init(&work->list);
612 bnx2fc_process_cq_compl(work->tgt, work->wqe);
613 kfree(work);
614 }
615
616 spin_lock_bh(&p->fp_work_lock);
617 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700618 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800619 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800620 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700621 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800622
623 return 0;
624}
625
626static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
627{
628 struct fc_host_statistics *bnx2fc_stats;
629 struct fc_lport *lport = shost_priv(shost);
630 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700631 struct bnx2fc_interface *interface = port->priv;
632 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800633 struct fcoe_statistics_params *fw_stats;
634 int rc = 0;
635
636 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
637 if (!fw_stats)
638 return NULL;
639
640 bnx2fc_stats = fc_get_host_stats(shost);
641
642 init_completion(&hba->stat_req_done);
643 if (bnx2fc_send_stat_req(hba))
644 return bnx2fc_stats;
645 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
646 if (!rc) {
647 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
648 return bnx2fc_stats;
649 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300650 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800651 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
652 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
653 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
654 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
655
656 bnx2fc_stats->dumped_frames = 0;
657 bnx2fc_stats->lip_count = 0;
658 bnx2fc_stats->nos_count = 0;
659 bnx2fc_stats->loss_of_sync_count = 0;
660 bnx2fc_stats->loss_of_signal_count = 0;
661 bnx2fc_stats->prim_seq_protocol_err_count = 0;
662
663 return bnx2fc_stats;
664}
665
666static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
667{
668 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700669 struct bnx2fc_interface *interface = port->priv;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800670 struct Scsi_Host *shost = lport->host;
671 int rc = 0;
672
673 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
674 shost->max_lun = BNX2FC_MAX_LUN;
675 shost->max_id = BNX2FC_MAX_FCP_TGT;
676 shost->max_channel = 0;
677 if (lport->vport)
678 shost->transportt = bnx2fc_vport_xport_template;
679 else
680 shost->transportt = bnx2fc_transport_template;
681
682 /* Add the new host to SCSI-ml */
683 rc = scsi_add_host(lport->host, dev);
684 if (rc) {
685 printk(KERN_ERR PFX "Error on scsi_add_host\n");
686 return rc;
687 }
688 if (!lport->vport)
689 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
690 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
691 BNX2FC_NAME, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700692 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800693
694 return 0;
695}
696
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800697static void bnx2fc_link_speed_update(struct fc_lport *lport)
698{
699 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700700 struct bnx2fc_interface *interface = port->priv;
701 struct net_device *netdev = interface->netdev;
David Decotigny8ae6daca2011-04-27 18:32:38 +0000702 struct ethtool_cmd ecmd;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800703
704 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
705 lport->link_supported_speeds &=
706 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
707 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
708 SUPPORTED_1000baseT_Full))
709 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
710 if (ecmd.supported & SUPPORTED_10000baseT_Full)
711 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
712
David Decotigny8ae6daca2011-04-27 18:32:38 +0000713 switch (ethtool_cmd_speed(&ecmd)) {
714 case SPEED_1000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800715 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +0000716 break;
Bhanu Prakash Gollapudia4dc08c2011-05-27 11:47:25 -0700717 case SPEED_2500:
718 lport->link_speed = FC_PORTSPEED_2GBIT;
719 break;
David Decotigny8ae6daca2011-04-27 18:32:38 +0000720 case SPEED_10000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800721 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +0000722 break;
723 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800724 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800725}
726static int bnx2fc_link_ok(struct fc_lport *lport)
727{
728 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700729 struct bnx2fc_interface *interface = port->priv;
730 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800731 struct net_device *dev = hba->phys_dev;
732 int rc = 0;
733
734 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
735 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
736 else {
737 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
738 rc = -1;
739 }
740 return rc;
741}
742
743/**
744 * bnx2fc_get_link_state - get network link state
745 *
746 * @hba: adapter instance pointer
747 *
748 * updates adapter structure flag based on netdev state
749 */
750void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
751{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700752 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800753 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
754 else
755 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
756}
757
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700758static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800759{
760 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700761 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800762 struct fcoe_port *port;
763 u64 wwnn, wwpn;
764
765 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700766 interface = port->priv;
767 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800768
769 /* require support for get_pauseparam ethtool op. */
770 if (!hba->phys_dev->ethtool_ops ||
771 !hba->phys_dev->ethtool_ops->get_pauseparam)
772 return -EOPNOTSUPP;
773
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700774 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800775 return -EINVAL;
776
777 skb_queue_head_init(&port->fcoe_pending_queue);
778 port->fcoe_pending_queue_active = 0;
779 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
780
781 bnx2fc_link_speed_update(lport);
782
783 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700784 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
785 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
786 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800787 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
788 fc_set_wwnn(lport, wwnn);
789
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700790 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
791 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
792 2, 0);
793
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800794 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
795 fc_set_wwpn(lport, wwpn);
796 }
797
798 return 0;
799}
800
801static void bnx2fc_destroy_timer(unsigned long data)
802{
803 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
804
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700805 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
806 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700807 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800808 wake_up_interruptible(&hba->destroy_wait);
809}
810
811/**
812 * bnx2fc_indicate_netevent - Generic netdev event handler
813 *
814 * @context: adapter structure pointer
815 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000816 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800817 *
818 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700819 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800820 */
Michael Chan415199f2011-07-20 14:55:24 +0000821static void bnx2fc_indicate_netevent(void *context, unsigned long event,
822 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800823{
824 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700825 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800826 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700827 struct bnx2fc_interface *interface, *tmp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700828 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800829 u32 link_possible = 1;
830
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700831 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000832 return;
833
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800834 switch (event) {
835 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800836 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
837 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700838 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800839 break;
840
841 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800842 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
843 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
844 link_possible = 0;
845 break;
846
847 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800848 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
849 link_possible = 0;
850 break;
851
852 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800853 break;
854
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700855 case NETDEV_UNREGISTER:
856 if (!vlan_id)
857 return;
858 mutex_lock(&bnx2fc_dev_lock);
859 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700860 if (interface->hba == hba &&
861 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
862 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700863 }
864 mutex_unlock(&bnx2fc_dev_lock);
865 return;
866
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800867 default:
868 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
869 return;
870 }
871
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700872 mutex_lock(&bnx2fc_dev_lock);
873 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800874
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700875 if (interface->hba != hba)
876 continue;
877
878 lport = interface->ctlr.lp;
879 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
880 interface->netdev->name, event);
881
882 bnx2fc_link_speed_update(lport);
883
884 if (link_possible && !bnx2fc_link_ok(lport)) {
Bhanu Prakash Gollapudic7806732011-08-30 15:54:47 -0700885 /* Reset max recv frame size to default */
886 fc_set_mfs(lport, BNX2FC_MFS);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -0700887 /*
888 * ctlr link up will only be handled during
889 * enable to avoid sending discovery solicitation
890 * on a stale vlan
891 */
Bhanu Prakash Gollapudifd8fa902011-10-03 16:45:00 -0700892 if (interface->enabled)
893 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700894 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800895 mutex_lock(&lport->lp_mutex);
896 list_for_each_entry(vport, &lport->vports, list)
897 fc_host_port_type(vport->host) =
898 FC_PORTTYPE_UNKNOWN;
899 mutex_unlock(&lport->lp_mutex);
900 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
901 per_cpu_ptr(lport->dev_stats,
902 get_cpu())->LinkFailureCount++;
903 put_cpu();
904 fcoe_clean_pending_queue(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700905 wait_for_upload = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800906 }
907 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700908 mutex_unlock(&bnx2fc_dev_lock);
909
910 if (wait_for_upload) {
911 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
912 init_waitqueue_head(&hba->shutdown_wait);
913 BNX2FC_MISC_DBG("indicate_netevent "
914 "num_ofld_sess = %d\n",
915 hba->num_ofld_sess);
916 hba->wait_for_link_down = 1;
917 wait_event_interruptible(hba->shutdown_wait,
918 (hba->num_ofld_sess == 0));
919 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
920 hba->num_ofld_sess);
921 hba->wait_for_link_down = 0;
922
923 if (signal_pending(current))
924 flush_signals(current);
925 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800926}
927
928static int bnx2fc_libfc_config(struct fc_lport *lport)
929{
930
931 /* Set the function pointers set by bnx2fc driver */
932 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
933 sizeof(struct libfc_function_template));
934 fc_elsct_init(lport);
935 fc_exch_init(lport);
936 fc_rport_init(lport);
937 fc_disc_init(lport);
938 return 0;
939}
940
941static int bnx2fc_em_config(struct fc_lport *lport)
942{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800943 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
944 FCOE_MAX_XID, NULL)) {
945 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
946 return -ENOMEM;
947 }
948
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800949 return 0;
950}
951
952static int bnx2fc_lport_config(struct fc_lport *lport)
953{
954 lport->link_up = 0;
955 lport->qfull = 0;
956 lport->max_retry_count = 3;
957 lport->max_rport_retry_count = 3;
958 lport->e_d_tov = 2 * 1000;
959 lport->r_a_tov = 10 * 1000;
960
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800961 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
962 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800963 lport->does_npiv = 1;
964
965 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
966 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
967
968 /* alloc stats structure */
969 if (fc_lport_init_stats(lport))
970 return -ENOMEM;
971
972 /* Finish fc_lport configuration */
973 fc_lport_config(lport);
974
975 return 0;
976}
977
978/**
979 * bnx2fc_fip_recv - handle a received FIP frame.
980 *
981 * @skb: the received skb
982 * @dev: associated &net_device
983 * @ptype: the &packet_type structure which was used to register this handler.
984 * @orig_dev: original receive &net_device, in case @ dev is a bond.
985 *
986 * Returns: 0 for success
987 */
988static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
989 struct packet_type *ptype,
990 struct net_device *orig_dev)
991{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700992 struct bnx2fc_interface *interface;
993 interface = container_of(ptype, struct bnx2fc_interface,
994 fip_packet_type);
995 fcoe_ctlr_recv(&interface->ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800996 return 0;
997}
998
999/**
1000 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1001 *
1002 * @fip: FCoE controller.
1003 * @old: Unicast MAC address to delete if the MAC is non-zero.
1004 * @new: Unicast MAC address to add.
1005 *
1006 * Remove any previously-set unicast MAC filter.
1007 * Add secondary FCoE MAC address filter for our OUI.
1008 */
1009static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1010{
1011 struct fcoe_port *port = lport_priv(lport);
1012
1013 memcpy(port->data_src_addr, addr, ETH_ALEN);
1014}
1015
1016/**
1017 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1018 *
1019 * @lport: libfc port
1020 */
1021static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1022{
1023 struct fcoe_port *port;
1024
1025 port = (struct fcoe_port *)lport_priv(lport);
1026 return port->data_src_addr;
1027}
1028
1029/**
1030 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1031 *
1032 * @fip: FCoE controller.
1033 * @skb: FIP Packet.
1034 */
1035static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1036{
1037 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1038 dev_queue_xmit(skb);
1039}
1040
1041static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1042{
1043 struct Scsi_Host *shost = vport_to_shost(vport);
1044 struct fc_lport *n_port = shost_priv(shost);
1045 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001046 struct bnx2fc_interface *interface = port->priv;
1047 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001048 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001049 int rc;
1050 char buf[32];
1051
1052 rc = fcoe_validate_vport_create(vport);
1053 if (rc) {
1054 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1055 printk(KERN_ERR PFX "Failed to create vport, "
1056 "WWPN (0x%s) already exists\n",
1057 buf);
1058 return rc;
1059 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001060
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001061 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001062 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001063 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001064 return -EIO;
1065 }
1066 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001067 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001068 mutex_unlock(&bnx2fc_dev_lock);
1069
1070 if (IS_ERR(vn_port)) {
1071 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1072 netdev->name);
1073 return -EIO;
1074 }
1075
1076 if (disabled) {
1077 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1078 } else {
1079 vn_port->boot_time = jiffies;
1080 fc_lport_init(vn_port);
1081 fc_fabric_login(vn_port);
1082 fc_vport_setlink(vn_port);
1083 }
1084 return 0;
1085}
1086
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001087static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1088{
1089 struct bnx2fc_lport *blport, *tmp;
1090
1091 spin_lock_bh(&hba->hba_lock);
1092 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1093 if (blport->lport == lport) {
1094 list_del(&blport->list);
1095 kfree(blport);
1096 }
1097 }
1098 spin_unlock_bh(&hba->hba_lock);
1099}
1100
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001101static int bnx2fc_vport_destroy(struct fc_vport *vport)
1102{
1103 struct Scsi_Host *shost = vport_to_shost(vport);
1104 struct fc_lport *n_port = shost_priv(shost);
1105 struct fc_lport *vn_port = vport->dd_data;
1106 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001107 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001108 struct fc_lport *v_port;
1109 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001110
1111 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001112 list_for_each_entry(v_port, &n_port->vports, list)
1113 if (v_port->vport == vport) {
1114 found = true;
1115 break;
1116 }
1117
1118 if (!found) {
1119 mutex_unlock(&n_port->lp_mutex);
1120 return -ENOENT;
1121 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001122 list_del(&vn_port->list);
1123 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001124 bnx2fc_free_vport(interface->hba, port->lport);
1125 bnx2fc_port_shutdown(port->lport);
1126 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001127 queue_work(bnx2fc_wq, &port->destroy_work);
1128 return 0;
1129}
1130
1131static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1132{
1133 struct fc_lport *lport = vport->dd_data;
1134
1135 if (disable) {
1136 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1137 fc_fabric_logoff(lport);
1138 } else {
1139 lport->boot_time = jiffies;
1140 fc_fabric_login(lport);
1141 fc_vport_setlink(lport);
1142 }
1143 return 0;
1144}
1145
1146
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001147static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001148{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001149 struct net_device *netdev = interface->netdev;
1150 struct net_device *physdev = interface->hba->phys_dev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001151 struct netdev_hw_addr *ha;
1152 int sel_san_mac = 0;
1153
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001154 /* setup Source MAC Address */
1155 rcu_read_lock();
1156 for_each_dev_addr(physdev, ha) {
1157 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1158 ha->type);
1159 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1160 ha->addr[1], ha->addr[2], ha->addr[3],
1161 ha->addr[4], ha->addr[5]);
1162
1163 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1164 (is_valid_ether_addr(ha->addr))) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001165 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1166 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001167 sel_san_mac = 1;
1168 BNX2FC_MISC_DBG("Found SAN MAC\n");
1169 }
1170 }
1171 rcu_read_unlock();
1172
1173 if (!sel_san_mac)
1174 return -ENODEV;
1175
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001176 interface->fip_packet_type.func = bnx2fc_fip_recv;
1177 interface->fip_packet_type.type = htons(ETH_P_FIP);
1178 interface->fip_packet_type.dev = netdev;
1179 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001180
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001181 interface->fcoe_packet_type.func = bnx2fc_rcv;
1182 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1183 interface->fcoe_packet_type.dev = netdev;
1184 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001185
1186 return 0;
1187}
1188
1189static int bnx2fc_attach_transport(void)
1190{
1191 bnx2fc_transport_template =
1192 fc_attach_transport(&bnx2fc_transport_function);
1193
1194 if (bnx2fc_transport_template == NULL) {
1195 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1196 return -ENODEV;
1197 }
1198
1199 bnx2fc_vport_xport_template =
1200 fc_attach_transport(&bnx2fc_vport_xport_function);
1201 if (bnx2fc_vport_xport_template == NULL) {
1202 printk(KERN_ERR PFX
1203 "Failed to attach FC transport for vport\n");
1204 fc_release_transport(bnx2fc_transport_template);
1205 bnx2fc_transport_template = NULL;
1206 return -ENODEV;
1207 }
1208 return 0;
1209}
1210static void bnx2fc_release_transport(void)
1211{
1212 fc_release_transport(bnx2fc_transport_template);
1213 fc_release_transport(bnx2fc_vport_xport_template);
1214 bnx2fc_transport_template = NULL;
1215 bnx2fc_vport_xport_template = NULL;
1216}
1217
1218static void bnx2fc_interface_release(struct kref *kref)
1219{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001220 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001221 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001222
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001223 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001224 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001225
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001226 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001227
1228 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001229 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1230 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001231
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001232 kfree(interface);
1233
1234 dev_put(netdev);
1235 module_put(THIS_MODULE);
1236}
1237
1238static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1239{
1240 kref_get(&interface->kref);
1241}
1242
1243static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1244{
1245 kref_put(&interface->kref, bnx2fc_interface_release);
1246}
1247static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1248{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001249 /* Free the command manager */
1250 if (hba->cmd_mgr) {
1251 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1252 hba->cmd_mgr = NULL;
1253 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001254 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001255 bnx2fc_unbind_pcidev(hba);
1256 kfree(hba);
1257}
1258
1259/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001260 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001261 *
1262 * @cnic: pointer to cnic device
1263 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001264 * Creates a new FCoE hba on the given device.
1265 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001266 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001267static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001268{
1269 struct bnx2fc_hba *hba;
1270 int rc;
1271
1272 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1273 if (!hba) {
1274 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1275 return NULL;
1276 }
1277 spin_lock_init(&hba->hba_lock);
1278 mutex_init(&hba->hba_mutex);
1279
1280 hba->cnic = cnic;
1281 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001282 if (rc) {
1283 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001284 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001285 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001286 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001287 hba->next_conn_id = 0;
1288
1289 hba->tgt_ofld_list =
1290 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1291 GFP_KERNEL);
1292 if (!hba->tgt_ofld_list) {
1293 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1294 goto tgtofld_err;
1295 }
1296
1297 hba->num_ofld_sess = 0;
1298
1299 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1300 BNX2FC_MAX_XID);
1301 if (!hba->cmd_mgr) {
1302 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1303 goto cmgr_err;
1304 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001305
1306 init_waitqueue_head(&hba->shutdown_wait);
1307 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001308 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001309
1310 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001311
1312cmgr_err:
1313 kfree(hba->tgt_ofld_list);
1314tgtofld_err:
1315 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001316bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001317 kfree(hba);
1318 return NULL;
1319}
1320
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001321struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1322 struct net_device *netdev,
1323 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001324{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001325 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001326 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001327
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001328 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1329 if (!interface) {
1330 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1331 return NULL;
1332 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001333 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001334 kref_init(&interface->kref);
1335 interface->hba = hba;
1336 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001337
1338 /* Initialize FIP */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001339 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1340 interface->ctlr.send = bnx2fc_fip_send;
1341 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1342 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1343 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001344
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001345 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001346 if (!rc)
1347 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001348
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001349 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001350 dev_put(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001351 kfree(interface);
1352 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001353}
1354
1355/**
1356 * bnx2fc_if_create - Create FCoE instance on a given interface
1357 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001358 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001359 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1360 * @npiv: Indicates if the port is vport or not
1361 *
1362 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1363 *
1364 * Returns: Allocated fc_lport or an error pointer
1365 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001366static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001367 struct device *parent, int npiv)
1368{
Vasu Dev134a4e22011-04-28 15:55:44 -07001369 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001370 struct fcoe_port *port;
1371 struct Scsi_Host *shost;
1372 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001373 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001374 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001375 int rc = 0;
1376
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001377 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1378 if (!blport) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001379 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001380 return NULL;
1381 }
1382
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001383 /* Allocate Scsi_Host structure */
Vasu Dev134a4e22011-04-28 15:55:44 -07001384 if (!npiv)
1385 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1386 else
1387 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001388
1389 if (!lport) {
1390 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001391 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001392 }
1393 shost = lport->host;
1394 port = lport_priv(lport);
1395 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001396 port->priv = interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001397 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1398
1399 /* Configure fcoe_port */
1400 rc = bnx2fc_lport_config(lport);
1401 if (rc)
1402 goto lp_config_err;
1403
1404 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001405 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1406 vport->node_name, vport->port_name);
1407 fc_set_wwnn(lport, vport->node_name);
1408 fc_set_wwpn(lport, vport->port_name);
1409 }
1410 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001411 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001412 if (rc) {
1413 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1414 goto lp_config_err;
1415 }
1416
1417 rc = bnx2fc_shost_config(lport, parent);
1418 if (rc) {
1419 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001420 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001421 goto lp_config_err;
1422 }
1423
1424 /* Initialize the libfc library */
1425 rc = bnx2fc_libfc_config(lport);
1426 if (rc) {
1427 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1428 goto shost_err;
1429 }
1430 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1431
1432 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001433 if (!npiv)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001434 rc = bnx2fc_em_config(lport);
Vasu Dev134a4e22011-04-28 15:55:44 -07001435 else {
1436 shost = vport_to_shost(vport);
1437 n_port = shost_priv(shost);
1438 rc = fc_exch_mgr_list_clone(n_port, lport);
1439 }
1440
1441 if (rc) {
1442 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1443 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001444 }
1445
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001446 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001447
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001448 hba = interface->hba;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001449 spin_lock_bh(&hba->hba_lock);
1450 blport->lport = lport;
1451 list_add_tail(&blport->list, &hba->vports);
1452 spin_unlock_bh(&hba->hba_lock);
1453
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001454 return lport;
1455
1456shost_err:
1457 scsi_remove_host(shost);
1458lp_config_err:
1459 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001460free_blport:
1461 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001462 return NULL;
1463}
1464
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001465static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1466{
1467 /* Dont listen for Ethernet packets anymore */
1468 __dev_remove_pack(&interface->fcoe_packet_type);
1469 __dev_remove_pack(&interface->fip_packet_type);
1470 synchronize_net();
1471}
1472
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001473static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001474{
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001475 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001476 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001477 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001478
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001479 /* Stop the transmit retry timer */
1480 del_timer_sync(&port->timer);
1481
1482 /* Free existing transmit skbs */
1483 fcoe_clean_pending_queue(lport);
1484
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001485 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001486
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001487 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001488}
1489
1490static void bnx2fc_if_destroy(struct fc_lport *lport)
1491{
1492
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001493 /* Free queued packets for the receive thread */
1494 bnx2fc_clean_rx_queue(lport);
1495
1496 /* Detach from scsi-ml */
1497 fc_remove_host(lport->host);
1498 scsi_remove_host(lport->host);
1499
1500 /*
1501 * Note that only the physical lport will have the exchange manager.
1502 * for vports, this function is NOP
1503 */
1504 fc_exch_mgr_free(lport);
1505
1506 /* Free memory used by statistical counters */
1507 fc_lport_free_stats(lport);
1508
1509 /* Release Scsi_Host */
1510 scsi_host_put(lport->host);
1511}
1512
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001513static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001514{
1515 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001516 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001517
1518 bnx2fc_interface_cleanup(interface);
1519 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001520 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001521 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001522 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001523}
1524
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001525/**
1526 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1527 *
1528 * @buffer: The name of the Ethernet interface to be destroyed
1529 * @kp: The associated kernel parameter
1530 *
1531 * Called from sysfs.
1532 *
1533 * Returns: 0 for success
1534 */
1535static int bnx2fc_destroy(struct net_device *netdev)
1536{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001537 struct bnx2fc_interface *interface = NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001538 int rc = 0;
1539
Nithin Sujir6702ca12011-03-17 17:13:27 -07001540 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001541 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001542
1543 interface = bnx2fc_interface_lookup(netdev);
1544 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001545 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001546 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001547 goto netdev_err;
1548 }
1549
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001550
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001551 destroy_workqueue(interface->timer_work_queue);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001552 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001553
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001554netdev_err:
1555 mutex_unlock(&bnx2fc_dev_lock);
1556 rtnl_unlock();
1557 return rc;
1558}
1559
1560static void bnx2fc_destroy_work(struct work_struct *work)
1561{
1562 struct fcoe_port *port;
1563 struct fc_lport *lport;
1564
1565 port = container_of(work, struct fcoe_port, destroy_work);
1566 lport = port->lport;
1567
1568 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1569
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001570 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001571}
1572
1573static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1574{
1575 bnx2fc_free_fw_resc(hba);
1576 bnx2fc_free_task_ctx(hba);
1577}
1578
1579/**
1580 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1581 * pci structure
1582 *
1583 * @hba: Adapter instance
1584 */
1585static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1586{
1587 if (bnx2fc_setup_task_ctx(hba))
1588 goto mem_err;
1589
1590 if (bnx2fc_setup_fw_resc(hba))
1591 goto mem_err;
1592
1593 return 0;
1594mem_err:
1595 bnx2fc_unbind_adapter_devices(hba);
1596 return -ENOMEM;
1597}
1598
1599static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1600{
1601 struct cnic_dev *cnic;
1602
1603 if (!hba->cnic) {
1604 printk(KERN_ERR PFX "cnic is NULL\n");
1605 return -ENODEV;
1606 }
1607 cnic = hba->cnic;
1608 hba->pcidev = cnic->pcidev;
1609 if (hba->pcidev)
1610 pci_dev_get(hba->pcidev);
1611
1612 return 0;
1613}
1614
1615static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1616{
1617 if (hba->pcidev)
1618 pci_dev_put(hba->pcidev);
1619 hba->pcidev = NULL;
1620}
1621
1622
1623
1624/**
1625 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1626 *
1627 * @handle: transport handle pointing to adapter struture
1628 *
1629 * This function maps adapter structure to pcidev structure and initiates
1630 * firmware handshake to enable/initialize on-chip FCoE components.
1631 * This bnx2fc - cnic interface api callback is used after following
1632 * conditions are met -
1633 * a) underlying network interface is up (marked by event NETDEV_UP
1634 * from netdev
1635 * b) bnx2fc adatper structure is registered.
1636 */
1637static void bnx2fc_ulp_start(void *handle)
1638{
1639 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001640 struct bnx2fc_interface *interface;
1641 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001642
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001643 mutex_lock(&bnx2fc_dev_lock);
1644
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001645 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001646 bnx2fc_fw_init(hba);
1647
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001648 BNX2FC_MISC_DBG("bnx2fc started.\n");
1649
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001650 list_for_each_entry(interface, &if_list, list) {
1651 if (interface->hba == hba) {
1652 lport = interface->ctlr.lp;
1653 /* Kick off Fabric discovery*/
1654 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1655 lport->tt.frame_send = bnx2fc_xmit;
1656 bnx2fc_start_disc(interface);
1657 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001658 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001659
1660 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001661}
1662
1663static void bnx2fc_port_shutdown(struct fc_lport *lport)
1664{
1665 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1666 fc_fabric_logoff(lport);
1667 fc_lport_destroy(lport);
1668}
1669
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001670static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001671{
1672 struct fc_lport *lport;
1673 struct fc_lport *vport;
1674
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001675 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1676 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001677
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001678 lport = interface->ctlr.lp;
1679 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001680
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001681 mutex_lock(&lport->lp_mutex);
1682 list_for_each_entry(vport, &lport->vports, list)
1683 fc_host_port_type(vport->host) =
1684 FC_PORTTYPE_UNKNOWN;
1685 mutex_unlock(&lport->lp_mutex);
1686 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1687 fcoe_ctlr_link_down(&interface->ctlr);
1688 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001689}
1690
1691static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1692{
1693#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1694 int rc = -1;
1695 int i = HZ;
1696
1697 rc = bnx2fc_bind_adapter_devices(hba);
1698 if (rc) {
1699 printk(KERN_ALERT PFX
1700 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1701 goto err_out;
1702 }
1703
1704 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1705 if (rc) {
1706 printk(KERN_ALERT PFX
1707 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1708 goto err_unbind;
1709 }
1710
1711 /*
1712 * Wait until the adapter init message is complete, and adapter
1713 * state is UP.
1714 */
1715 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1716 msleep(BNX2FC_INIT_POLL_TIME);
1717
1718 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1719 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1720 "Ignoring...\n",
1721 hba->cnic->netdev->name);
1722 rc = -1;
1723 goto err_unbind;
1724 }
1725
1726
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001727 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001728 return 0;
1729
1730err_unbind:
1731 bnx2fc_unbind_adapter_devices(hba);
1732err_out:
1733 return rc;
1734}
1735
1736static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1737{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001738 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001739 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1740 init_timer(&hba->destroy_timer);
1741 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1742 jiffies;
1743 hba->destroy_timer.function = bnx2fc_destroy_timer;
1744 hba->destroy_timer.data = (unsigned long)hba;
1745 add_timer(&hba->destroy_timer);
1746 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001747 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1748 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001749 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001750 /* This should never happen */
1751 if (signal_pending(current))
1752 flush_signals(current);
1753
1754 del_timer_sync(&hba->destroy_timer);
1755 }
1756 bnx2fc_unbind_adapter_devices(hba);
1757 }
1758}
1759
1760/**
1761 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1762 *
1763 * @handle: transport handle pointing to adapter structure
1764 *
1765 * Driver checks if adapter is already in shutdown mode, if not start
1766 * the shutdown process.
1767 */
1768static void bnx2fc_ulp_stop(void *handle)
1769{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001770 struct bnx2fc_hba *hba = handle;
1771 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001772
1773 printk(KERN_ERR "ULP_STOP\n");
1774
1775 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001776 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1777 goto exit;
1778 list_for_each_entry(interface, &if_list, list) {
1779 if (interface->hba == hba)
1780 bnx2fc_stop(interface);
1781 }
1782 BUG_ON(hba->num_ofld_sess != 0);
1783
1784 mutex_lock(&hba->hba_mutex);
1785 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1786 clear_bit(ADAPTER_STATE_GOING_DOWN,
1787 &hba->adapter_state);
1788
1789 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1790 mutex_unlock(&hba->hba_mutex);
1791
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001792 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001793exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001794 mutex_unlock(&bnx2fc_dev_lock);
1795}
1796
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001797static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001798{
1799 struct fc_lport *lport;
1800 int wait_cnt = 0;
1801
1802 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1803 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001804 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001805 printk(KERN_ERR PFX "Init not done yet\n");
1806 return;
1807 }
1808
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001809 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001810 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1811
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001812 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001813 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001814 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001815 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001816 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001817 }
1818
1819 /* wait for the FCF to be selected before issuing FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001820 while (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001821 msleep(250);
1822 /* give up after 3 secs */
1823 if (++wait_cnt > 12)
1824 break;
1825 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001826
1827 /* Reset max receive frame size to default */
1828 if (fc_set_mfs(lport, BNX2FC_MFS))
1829 return;
1830
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001831 fc_lport_init(lport);
1832 fc_fabric_login(lport);
1833}
1834
1835
1836/**
1837 * bnx2fc_ulp_init - Initialize an adapter instance
1838 *
1839 * @dev : cnic device handle
1840 * Called from cnic_register_driver() context to initialize all
1841 * enumerated cnic devices. This routine allocates adapter structure
1842 * and other device specific resources.
1843 */
1844static void bnx2fc_ulp_init(struct cnic_dev *dev)
1845{
1846 struct bnx2fc_hba *hba;
1847 int rc = 0;
1848
1849 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1850 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001851 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1852 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001853 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001854 " flags: %lx fcoe_conn: %d\n",
1855 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001856 return;
1857 }
1858
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001859 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001860 if (!hba) {
1861 printk(KERN_ERR PFX "hba initialization failed\n");
1862 return;
1863 }
1864
1865 /* Add HBA to the adapter list */
1866 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001867 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001868 adapter_count++;
1869 mutex_unlock(&bnx2fc_dev_lock);
1870
1871 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1872 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1873 (void *) hba);
1874 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001875 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001876 else
1877 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1878}
1879
1880
1881static int bnx2fc_disable(struct net_device *netdev)
1882{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001883 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001884 int rc = 0;
1885
Nithin Sujir6702ca12011-03-17 17:13:27 -07001886 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001887 mutex_lock(&bnx2fc_dev_lock);
1888
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001889 interface = bnx2fc_interface_lookup(netdev);
1890 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001891 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001892 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001893 } else {
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001894 interface->enabled = false;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001895 fcoe_ctlr_link_down(&interface->ctlr);
1896 fcoe_clean_pending_queue(interface->ctlr.lp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001897 }
1898
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001899 mutex_unlock(&bnx2fc_dev_lock);
1900 rtnl_unlock();
1901 return rc;
1902}
1903
1904
1905static int bnx2fc_enable(struct net_device *netdev)
1906{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001907 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001908 int rc = 0;
1909
Nithin Sujir6702ca12011-03-17 17:13:27 -07001910 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001911 mutex_lock(&bnx2fc_dev_lock);
1912
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001913 interface = bnx2fc_interface_lookup(netdev);
1914 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001915 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001916 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001917 } else if (!bnx2fc_link_ok(interface->ctlr.lp)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001918 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001919 interface->enabled = true;
1920 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001921
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001922 mutex_unlock(&bnx2fc_dev_lock);
1923 rtnl_unlock();
1924 return rc;
1925}
1926
1927/**
1928 * bnx2fc_create - Create bnx2fc FCoE interface
1929 *
1930 * @buffer: The name of Ethernet interface to create on
1931 * @kp: The associated kernel param
1932 *
1933 * Called from sysfs.
1934 *
1935 * Returns: 0 for success
1936 */
1937static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1938{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001939 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001940 struct bnx2fc_hba *hba;
1941 struct net_device *phys_dev;
1942 struct fc_lport *lport;
1943 struct ethtool_drvinfo drvinfo;
1944 int rc = 0;
1945 int vlan_id;
1946
1947 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1948 if (fip_mode != FIP_MODE_FABRIC) {
1949 printk(KERN_ERR "fip mode not FABRIC\n");
1950 return -EIO;
1951 }
1952
Nithin Sujir6702ca12011-03-17 17:13:27 -07001953 rtnl_lock();
1954
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001955 mutex_lock(&bnx2fc_dev_lock);
1956
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001957 if (!try_module_get(THIS_MODULE)) {
1958 rc = -EINVAL;
1959 goto mod_err;
1960 }
1961
1962 /* obtain physical netdev */
1963 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1964 phys_dev = vlan_dev_real_dev(netdev);
1965 vlan_id = vlan_dev_vlan_id(netdev);
1966 } else {
1967 printk(KERN_ERR PFX "Not a vlan device\n");
1968 rc = -EINVAL;
1969 goto netdev_err;
1970 }
1971 /* verify if the physical device is a netxtreme2 device */
1972 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1973 memset(&drvinfo, 0, sizeof(drvinfo));
1974 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001975 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001976 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1977 rc = -EINVAL;
1978 goto netdev_err;
1979 }
1980 } else {
1981 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1982 rc = -EINVAL;
1983 goto netdev_err;
1984 }
1985
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001986 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001987 hba = bnx2fc_hba_lookup(phys_dev);
1988 if (!hba) {
1989 rc = -ENODEV;
1990 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1991 goto netdev_err;
1992 }
1993
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001994 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001995 rc = -EEXIST;
1996 goto netdev_err;
1997 }
1998
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001999 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2000 if (!interface) {
2001 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002002 goto ifput_err;
2003 }
2004
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002005 interface->vlan_id = vlan_id;
2006 interface->vlan_enabled = 1;
2007
2008 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002009 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002010 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002011 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2012 rc = -EINVAL;
2013 goto ifput_err;
2014 }
2015
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002016 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002017 if (!lport) {
2018 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2019 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002020 rc = -EINVAL;
2021 goto if_create_err;
2022 }
2023
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002024 /* Add interface to if_list */
2025 list_add_tail(&interface->list, &if_list);
2026
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002027 lport->boot_time = jiffies;
2028
2029 /* Make this master N_port */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002030 interface->ctlr.lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002031
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002032 if (!bnx2fc_link_ok(lport)) {
2033 fcoe_ctlr_link_up(&interface->ctlr);
2034 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2035 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2036 }
2037
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002038 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2039 bnx2fc_start_disc(interface);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002040 interface->enabled = true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002041 /*
2042 * Release from kref_init in bnx2fc_interface_setup, on success
2043 * lport should be holding a reference taken in bnx2fc_if_create
2044 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002045 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002046 /* put netdev that was held while calling dev_get_by_name */
2047 mutex_unlock(&bnx2fc_dev_lock);
2048 rtnl_unlock();
2049 return 0;
2050
2051if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002052 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002053ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002054 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002055 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002056netdev_err:
2057 module_put(THIS_MODULE);
2058mod_err:
2059 mutex_unlock(&bnx2fc_dev_lock);
2060 rtnl_unlock();
2061 return rc;
2062}
2063
2064/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002065 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002066 *
2067 * @cnic: Pointer to cnic device instance
2068 *
2069 **/
2070static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2071{
2072 struct list_head *list;
2073 struct list_head *temp;
2074 struct bnx2fc_hba *hba;
2075
2076 /* Called with bnx2fc_dev_lock held */
2077 list_for_each_safe(list, temp, &adapter_list) {
2078 hba = (struct bnx2fc_hba *)list;
2079 if (hba->cnic == cnic)
2080 return hba;
2081 }
2082 return NULL;
2083}
2084
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002085static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2086 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002087{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002088 struct bnx2fc_interface *interface;
2089
2090 /* Called with bnx2fc_dev_lock held */
2091 list_for_each_entry(interface, &if_list, list) {
2092 if (interface->netdev == netdev)
2093 return interface;
2094 }
2095 return NULL;
2096}
2097
2098static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2099 *phys_dev)
2100{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002101 struct bnx2fc_hba *hba;
2102
2103 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002104 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002105 if (hba->phys_dev == phys_dev)
2106 return hba;
2107 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002108 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002109 return NULL;
2110}
2111
2112/**
2113 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2114 *
2115 * @dev cnic device handle
2116 */
2117static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2118{
2119 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002120 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002121
2122 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2123
2124 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2125 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2126 dev->netdev->name, dev->flags);
2127 return;
2128 }
2129
2130 mutex_lock(&bnx2fc_dev_lock);
2131 hba = bnx2fc_find_hba_for_cnic(dev);
2132 if (!hba) {
2133 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2134 dev);
2135 mutex_unlock(&bnx2fc_dev_lock);
2136 return;
2137 }
2138
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002139 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002140 adapter_count--;
2141
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002142 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002143 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002144 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002145 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002146 mutex_unlock(&bnx2fc_dev_lock);
2147
2148 bnx2fc_ulp_stop(hba);
2149 /* unregister cnic device */
2150 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2151 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002152 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002153}
2154
2155/**
2156 * bnx2fc_fcoe_reset - Resets the fcoe
2157 *
2158 * @shost: shost the reset is from
2159 *
2160 * Returns: always 0
2161 */
2162static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2163{
2164 struct fc_lport *lport = shost_priv(shost);
2165 fc_lport_reset(lport);
2166 return 0;
2167}
2168
2169
2170static bool bnx2fc_match(struct net_device *netdev)
2171{
2172 mutex_lock(&bnx2fc_dev_lock);
2173 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2174 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2175
2176 if (bnx2fc_hba_lookup(phys_dev)) {
2177 mutex_unlock(&bnx2fc_dev_lock);
2178 return true;
2179 }
2180 }
2181 mutex_unlock(&bnx2fc_dev_lock);
2182 return false;
2183}
2184
2185
2186static struct fcoe_transport bnx2fc_transport = {
2187 .name = {"bnx2fc"},
2188 .attached = false,
2189 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2190 .match = bnx2fc_match,
2191 .create = bnx2fc_create,
2192 .destroy = bnx2fc_destroy,
2193 .enable = bnx2fc_enable,
2194 .disable = bnx2fc_disable,
2195};
2196
2197/**
2198 * bnx2fc_percpu_thread_create - Create a receive thread for an
2199 * online CPU
2200 *
2201 * @cpu: cpu index for the online cpu
2202 */
2203static void bnx2fc_percpu_thread_create(unsigned int cpu)
2204{
2205 struct bnx2fc_percpu_s *p;
2206 struct task_struct *thread;
2207
2208 p = &per_cpu(bnx2fc_percpu, cpu);
2209
2210 thread = kthread_create(bnx2fc_percpu_io_thread,
2211 (void *)p,
2212 "bnx2fc_thread/%d", cpu);
2213 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002214 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002215 kthread_bind(thread, cpu);
2216 p->iothread = thread;
2217 wake_up_process(thread);
2218 }
2219}
2220
2221static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2222{
2223 struct bnx2fc_percpu_s *p;
2224 struct task_struct *thread;
2225 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002226
2227 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2228
2229 /* Prevent any new work from being queued for this CPU */
2230 p = &per_cpu(bnx2fc_percpu, cpu);
2231 spin_lock_bh(&p->fp_work_lock);
2232 thread = p->iothread;
2233 p->iothread = NULL;
2234
2235
2236 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002237 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002238 list_del_init(&work->list);
2239 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2240 kfree(work);
2241 }
2242
2243 spin_unlock_bh(&p->fp_work_lock);
2244
2245 if (thread)
2246 kthread_stop(thread);
2247}
2248
2249/**
2250 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2251 *
2252 * @nfb: The callback data block
2253 * @action: The event triggering the callback
2254 * @hcpu: The index of the CPU that the event is for
2255 *
2256 * This creates or destroys per-CPU data for fcoe
2257 *
2258 * Returns NOTIFY_OK always.
2259 */
2260static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2261 unsigned long action, void *hcpu)
2262{
2263 unsigned cpu = (unsigned long)hcpu;
2264
2265 switch (action) {
2266 case CPU_ONLINE:
2267 case CPU_ONLINE_FROZEN:
2268 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2269 bnx2fc_percpu_thread_create(cpu);
2270 break;
2271 case CPU_DEAD:
2272 case CPU_DEAD_FROZEN:
2273 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2274 bnx2fc_percpu_thread_destroy(cpu);
2275 break;
2276 default:
2277 break;
2278 }
2279 return NOTIFY_OK;
2280}
2281
2282/**
2283 * bnx2fc_mod_init - module init entry point
2284 *
2285 * Initialize driver wide global data structures, and register
2286 * with cnic module
2287 **/
2288static int __init bnx2fc_mod_init(void)
2289{
2290 struct fcoe_percpu_s *bg;
2291 struct task_struct *l2_thread;
2292 int rc = 0;
2293 unsigned int cpu = 0;
2294 struct bnx2fc_percpu_s *p;
2295
2296 printk(KERN_INFO PFX "%s", version);
2297
2298 /* register as a fcoe transport */
2299 rc = fcoe_transport_attach(&bnx2fc_transport);
2300 if (rc) {
2301 printk(KERN_ERR "failed to register an fcoe transport, check "
2302 "if libfcoe is loaded\n");
2303 goto out;
2304 }
2305
2306 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002307 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002308 mutex_init(&bnx2fc_dev_lock);
2309 adapter_count = 0;
2310
2311 /* Attach FC transport template */
2312 rc = bnx2fc_attach_transport();
2313 if (rc)
2314 goto detach_ft;
2315
2316 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2317 if (!bnx2fc_wq) {
2318 rc = -ENOMEM;
2319 goto release_bt;
2320 }
2321
2322 bg = &bnx2fc_global;
2323 skb_queue_head_init(&bg->fcoe_rx_list);
2324 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2325 (void *)bg,
2326 "bnx2fc_l2_thread");
2327 if (IS_ERR(l2_thread)) {
2328 rc = PTR_ERR(l2_thread);
2329 goto free_wq;
2330 }
2331 wake_up_process(l2_thread);
2332 spin_lock_bh(&bg->fcoe_rx_list.lock);
2333 bg->thread = l2_thread;
2334 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2335
2336 for_each_possible_cpu(cpu) {
2337 p = &per_cpu(bnx2fc_percpu, cpu);
2338 INIT_LIST_HEAD(&p->work_list);
2339 spin_lock_init(&p->fp_work_lock);
2340 }
2341
2342 for_each_online_cpu(cpu) {
2343 bnx2fc_percpu_thread_create(cpu);
2344 }
2345
2346 /* Initialize per CPU interrupt thread */
2347 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2348
2349 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2350
2351 return 0;
2352
2353free_wq:
2354 destroy_workqueue(bnx2fc_wq);
2355release_bt:
2356 bnx2fc_release_transport();
2357detach_ft:
2358 fcoe_transport_detach(&bnx2fc_transport);
2359out:
2360 return rc;
2361}
2362
2363static void __exit bnx2fc_mod_exit(void)
2364{
2365 LIST_HEAD(to_be_deleted);
2366 struct bnx2fc_hba *hba, *next;
2367 struct fcoe_percpu_s *bg;
2368 struct task_struct *l2_thread;
2369 struct sk_buff *skb;
2370 unsigned int cpu = 0;
2371
2372 /*
2373 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2374 * it will have higher precedence than bnx2fc_dev_lock.
2375 * unregister_device() cannot be called with bnx2fc_dev_lock
2376 * held.
2377 */
2378 mutex_lock(&bnx2fc_dev_lock);
2379 list_splice(&adapter_list, &to_be_deleted);
2380 INIT_LIST_HEAD(&adapter_list);
2381 adapter_count = 0;
2382 mutex_unlock(&bnx2fc_dev_lock);
2383
2384 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002385 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2386 list_del_init(&hba->list);
2387 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2388 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002389 bnx2fc_ulp_stop(hba);
2390 /* unregister cnic device */
2391 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2392 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002393 hba->cnic->unregister_device(hba->cnic,
2394 CNIC_ULP_FCOE);
2395 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002396 }
2397 cnic_unregister_driver(CNIC_ULP_FCOE);
2398
2399 /* Destroy global thread */
2400 bg = &bnx2fc_global;
2401 spin_lock_bh(&bg->fcoe_rx_list.lock);
2402 l2_thread = bg->thread;
2403 bg->thread = NULL;
2404 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2405 kfree_skb(skb);
2406
2407 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2408
2409 if (l2_thread)
2410 kthread_stop(l2_thread);
2411
2412 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2413
2414 /* Destroy per cpu threads */
2415 for_each_online_cpu(cpu) {
2416 bnx2fc_percpu_thread_destroy(cpu);
2417 }
2418
2419 destroy_workqueue(bnx2fc_wq);
2420 /*
2421 * detach from scsi transport
2422 * must happen after all destroys are done
2423 */
2424 bnx2fc_release_transport();
2425
2426 /* detach from fcoe transport */
2427 fcoe_transport_detach(&bnx2fc_transport);
2428}
2429
2430module_init(bnx2fc_mod_init);
2431module_exit(bnx2fc_mod_exit);
2432
2433static struct fc_function_template bnx2fc_transport_function = {
2434 .show_host_node_name = 1,
2435 .show_host_port_name = 1,
2436 .show_host_supported_classes = 1,
2437 .show_host_supported_fc4s = 1,
2438 .show_host_active_fc4s = 1,
2439 .show_host_maxframe_size = 1,
2440
2441 .show_host_port_id = 1,
2442 .show_host_supported_speeds = 1,
2443 .get_host_speed = fc_get_host_speed,
2444 .show_host_speed = 1,
2445 .show_host_port_type = 1,
2446 .get_host_port_state = fc_get_host_port_state,
2447 .show_host_port_state = 1,
2448 .show_host_symbolic_name = 1,
2449
2450 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2451 sizeof(struct bnx2fc_rport)),
2452 .show_rport_maxframe_size = 1,
2453 .show_rport_supported_classes = 1,
2454
2455 .show_host_fabric_name = 1,
2456 .show_starget_node_name = 1,
2457 .show_starget_port_name = 1,
2458 .show_starget_port_id = 1,
2459 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2460 .show_rport_dev_loss_tmo = 1,
2461 .get_fc_host_stats = bnx2fc_get_host_stats,
2462
2463 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2464
2465 .terminate_rport_io = fc_rport_terminate_io,
2466
2467 .vport_create = bnx2fc_vport_create,
2468 .vport_delete = bnx2fc_vport_destroy,
2469 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002470 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002471};
2472
2473static struct fc_function_template bnx2fc_vport_xport_function = {
2474 .show_host_node_name = 1,
2475 .show_host_port_name = 1,
2476 .show_host_supported_classes = 1,
2477 .show_host_supported_fc4s = 1,
2478 .show_host_active_fc4s = 1,
2479 .show_host_maxframe_size = 1,
2480
2481 .show_host_port_id = 1,
2482 .show_host_supported_speeds = 1,
2483 .get_host_speed = fc_get_host_speed,
2484 .show_host_speed = 1,
2485 .show_host_port_type = 1,
2486 .get_host_port_state = fc_get_host_port_state,
2487 .show_host_port_state = 1,
2488 .show_host_symbolic_name = 1,
2489
2490 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2491 sizeof(struct bnx2fc_rport)),
2492 .show_rport_maxframe_size = 1,
2493 .show_rport_supported_classes = 1,
2494
2495 .show_host_fabric_name = 1,
2496 .show_starget_node_name = 1,
2497 .show_starget_port_name = 1,
2498 .show_starget_port_id = 1,
2499 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2500 .show_rport_dev_loss_tmo = 1,
2501 .get_fc_host_stats = fc_get_host_stats,
2502 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2503 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002504 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002505};
2506
2507/**
2508 * scsi_host_template structure used while registering with SCSI-ml
2509 */
2510static struct scsi_host_template bnx2fc_shost_template = {
2511 .module = THIS_MODULE,
2512 .name = "Broadcom Offload FCoE Initiator",
2513 .queuecommand = bnx2fc_queuecommand,
2514 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2515 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2516 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2517 .eh_host_reset_handler = fc_eh_host_reset,
2518 .slave_alloc = fc_slave_alloc,
2519 .change_queue_depth = fc_change_queue_depth,
2520 .change_queue_type = fc_change_queue_type,
2521 .this_id = -1,
2522 .cmd_per_lun = 3,
Bhanu Gollapudi0ea5c272011-03-17 17:13:29 -07002523 .can_queue = BNX2FC_CAN_QUEUE,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002524 .use_clustering = ENABLE_CLUSTERING,
2525 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2526 .max_sectors = 512,
2527};
2528
2529static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2530 .frame_send = bnx2fc_xmit,
2531 .elsct_send = bnx2fc_elsct_send,
2532 .fcp_abort_io = bnx2fc_abort_io,
2533 .fcp_cleanup = bnx2fc_cleanup,
Bhanu Prakash Gollapudif72f69792011-10-03 16:45:02 -07002534 .get_lesb = bnx2fc_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002535 .rport_event_callback = bnx2fc_rport_event_handler,
2536};
2537
2538/**
2539 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2540 * structure carrying callback function pointers
2541 */
2542static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2543 .owner = THIS_MODULE,
2544 .cnic_init = bnx2fc_ulp_init,
2545 .cnic_exit = bnx2fc_ulp_exit,
2546 .cnic_start = bnx2fc_ulp_start,
2547 .cnic_stop = bnx2fc_ulp_stop,
2548 .indicate_kcqes = bnx2fc_indicate_kcqe,
2549 .indicate_netevent = bnx2fc_indicate_netevent,
2550};