1
2
3
4
5
6
7
8#include <linux/module.h>
9#include <linux/spinlock.h>
10#include <linux/netdevice.h>
11#include <linux/etherdevice.h>
12#include <linux/ethtool.h>
13#include <linux/if_ether.h>
14#include <linux/if_vlan.h>
15#include <linux/crc32.h>
16#include <linux/slab.h>
17#include <linux/cpu.h>
18#include <linux/fs.h>
19#include <linux/sysfs.h>
20#include <linux/ctype.h>
21#include <linux/workqueue.h>
22#include <net/dcbnl.h>
23#include <net/dcbevent.h>
24#include <scsi/scsi_tcq.h>
25#include <scsi/scsicam.h>
26#include <scsi/scsi_transport.h>
27#include <scsi/scsi_transport_fc.h>
28#include <net/rtnetlink.h>
29
30#include <scsi/fc/fc_encaps.h>
31#include <scsi/fc/fc_fip.h>
32#include <scsi/fc/fc_fcoe.h>
33
34#include <scsi/libfc.h>
35#include <scsi/fc_frame.h>
36#include <scsi/libfcoe.h>
37
38#include "fcoe.h"
39
40MODULE_AUTHOR("Open-FCoE.org");
41MODULE_DESCRIPTION("FCoE");
42MODULE_LICENSE("GPL v2");
43
44
45static unsigned int fcoe_ddp_min = 4096;
46module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
47MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
48 "Direct Data Placement (DDP).");
49
50unsigned int fcoe_debug_logging;
51module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
53
54static unsigned int fcoe_e_d_tov = 2 * 1000;
55module_param_named(e_d_tov, fcoe_e_d_tov, int, S_IRUGO|S_IWUSR);
56MODULE_PARM_DESC(e_d_tov, "E_D_TOV in ms, default 2000");
57
58static unsigned int fcoe_r_a_tov = 2 * 2 * 1000;
59module_param_named(r_a_tov, fcoe_r_a_tov, int, S_IRUGO|S_IWUSR);
60MODULE_PARM_DESC(r_a_tov, "R_A_TOV in ms, default 4000");
61
62static DEFINE_MUTEX(fcoe_config_mutex);
63
64static struct workqueue_struct *fcoe_wq;
65
66
67
68static LIST_HEAD(fcoe_hostlist);
69static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
70
71
72static int fcoe_reset(struct Scsi_Host *);
73static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
74static int fcoe_rcv(struct sk_buff *, struct net_device *,
75 struct packet_type *, struct net_device *);
76static void fcoe_percpu_clean(struct fc_lport *);
77static int fcoe_link_ok(struct fc_lport *);
78
79static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
80static int fcoe_hostlist_add(const struct fc_lport *);
81static void fcoe_hostlist_del(const struct fc_lport *);
82
83static int fcoe_device_notification(struct notifier_block *, ulong, void *);
84static void fcoe_dev_setup(void);
85static void fcoe_dev_cleanup(void);
86static struct fcoe_interface
87*fcoe_hostlist_lookup_port(const struct net_device *);
88
89static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
90 struct packet_type *, struct net_device *);
91static int fcoe_fip_vlan_recv(struct sk_buff *, struct net_device *,
92 struct packet_type *, struct net_device *);
93
94static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
95static void fcoe_update_src_mac(struct fc_lport *, u8 *);
96static u8 *fcoe_get_src_mac(struct fc_lport *);
97static void fcoe_destroy_work(struct work_struct *);
98
99static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
100 unsigned int);
101static int fcoe_ddp_done(struct fc_lport *, u16);
102static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
103 unsigned int);
104static int fcoe_dcb_app_notification(struct notifier_block *notifier,
105 ulong event, void *ptr);
106
107static bool fcoe_match(struct net_device *netdev);
108static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode);
109static int fcoe_destroy(struct net_device *netdev);
110static int fcoe_enable(struct net_device *netdev);
111static int fcoe_disable(struct net_device *netdev);
112
113
114static int fcoe_ctlr_alloc(struct net_device *netdev);
115static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev);
116static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev);
117
118static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
119 u32 did, struct fc_frame *,
120 unsigned int op,
121 void (*resp)(struct fc_seq *,
122 struct fc_frame *,
123 void *),
124 void *, u32 timeout);
125static void fcoe_recv_frame(struct sk_buff *skb);
126
127
128static struct notifier_block fcoe_notifier = {
129 .notifier_call = fcoe_device_notification,
130};
131
132
133static struct notifier_block dcb_notifier = {
134 .notifier_call = fcoe_dcb_app_notification,
135};
136
137static struct scsi_transport_template *fcoe_nport_scsi_transport;
138static struct scsi_transport_template *fcoe_vport_scsi_transport;
139
140static int fcoe_vport_destroy(struct fc_vport *);
141static int fcoe_vport_create(struct fc_vport *, bool disabled);
142static int fcoe_vport_disable(struct fc_vport *, bool disable);
143static void fcoe_set_vport_symbolic_name(struct fc_vport *);
144static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
145static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
146static void fcoe_vport_remove(struct fc_lport *);
147
148static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
149 .set_fcoe_ctlr_mode = fcoe_ctlr_mode,
150 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
151 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
152 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
153 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
154 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
155 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
156 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
157
158 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
159 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
160};
161
162static struct libfc_function_template fcoe_libfc_fcn_templ = {
163 .frame_send = fcoe_xmit,
164 .ddp_setup = fcoe_ddp_setup,
165 .ddp_done = fcoe_ddp_done,
166 .ddp_target = fcoe_ddp_target,
167 .elsct_send = fcoe_elsct_send,
168 .get_lesb = fcoe_get_lesb,
169 .lport_set_port_id = fcoe_set_port_id,
170};
171
172static struct fc_function_template fcoe_nport_fc_functions = {
173 .show_host_node_name = 1,
174 .show_host_port_name = 1,
175 .show_host_supported_classes = 1,
176 .show_host_supported_fc4s = 1,
177 .show_host_active_fc4s = 1,
178 .show_host_maxframe_size = 1,
179 .show_host_serial_number = 1,
180 .show_host_manufacturer = 1,
181 .show_host_model = 1,
182 .show_host_model_description = 1,
183 .show_host_hardware_version = 1,
184 .show_host_driver_version = 1,
185 .show_host_firmware_version = 1,
186 .show_host_optionrom_version = 1,
187
188 .show_host_port_id = 1,
189 .show_host_supported_speeds = 1,
190 .get_host_speed = fc_get_host_speed,
191 .show_host_speed = 1,
192 .show_host_port_type = 1,
193 .get_host_port_state = fc_get_host_port_state,
194 .show_host_port_state = 1,
195 .show_host_symbolic_name = 1,
196
197 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
198 .show_rport_maxframe_size = 1,
199 .show_rport_supported_classes = 1,
200
201 .show_host_fabric_name = 1,
202 .show_starget_node_name = 1,
203 .show_starget_port_name = 1,
204 .show_starget_port_id = 1,
205 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
206 .show_rport_dev_loss_tmo = 1,
207 .get_fc_host_stats = fc_get_host_stats,
208 .issue_fc_host_lip = fcoe_reset,
209
210 .terminate_rport_io = fc_rport_terminate_io,
211
212 .vport_create = fcoe_vport_create,
213 .vport_delete = fcoe_vport_destroy,
214 .vport_disable = fcoe_vport_disable,
215 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
216
217 .bsg_request = fc_lport_bsg_request,
218};
219
220static struct fc_function_template fcoe_vport_fc_functions = {
221 .show_host_node_name = 1,
222 .show_host_port_name = 1,
223 .show_host_supported_classes = 1,
224 .show_host_supported_fc4s = 1,
225 .show_host_active_fc4s = 1,
226 .show_host_maxframe_size = 1,
227 .show_host_serial_number = 1,
228 .show_host_manufacturer = 1,
229 .show_host_model = 1,
230 .show_host_model_description = 1,
231 .show_host_hardware_version = 1,
232 .show_host_driver_version = 1,
233 .show_host_firmware_version = 1,
234 .show_host_optionrom_version = 1,
235
236 .show_host_port_id = 1,
237 .show_host_supported_speeds = 1,
238 .get_host_speed = fc_get_host_speed,
239 .show_host_speed = 1,
240 .show_host_port_type = 1,
241 .get_host_port_state = fc_get_host_port_state,
242 .show_host_port_state = 1,
243 .show_host_symbolic_name = 1,
244
245 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
246 .show_rport_maxframe_size = 1,
247 .show_rport_supported_classes = 1,
248
249 .show_host_fabric_name = 1,
250 .show_starget_node_name = 1,
251 .show_starget_port_name = 1,
252 .show_starget_port_id = 1,
253 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
254 .show_rport_dev_loss_tmo = 1,
255 .get_fc_host_stats = fc_get_host_stats,
256 .issue_fc_host_lip = fcoe_reset,
257
258 .terminate_rport_io = fc_rport_terminate_io,
259
260 .bsg_request = fc_lport_bsg_request,
261};
262
263static struct scsi_host_template fcoe_shost_template = {
264 .module = THIS_MODULE,
265 .name = "FCoE Driver",
266 .proc_name = FCOE_NAME,
267 .queuecommand = fc_queuecommand,
268 .eh_timed_out = fc_eh_timed_out,
269 .eh_abort_handler = fc_eh_abort,
270 .eh_device_reset_handler = fc_eh_device_reset,
271 .eh_host_reset_handler = fc_eh_host_reset,
272 .slave_alloc = fc_slave_alloc,
273 .change_queue_depth = scsi_change_queue_depth,
274 .this_id = -1,
275 .cmd_per_lun = 3,
276 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
277 .sg_tablesize = SG_ALL,
278 .max_sectors = 0xffff,
279 .track_queue_depth = 1,
280};
281
282
283
284
285
286
287
288
289
290static int fcoe_interface_setup(struct fcoe_interface *fcoe,
291 struct net_device *netdev)
292{
293 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
294 struct netdev_hw_addr *ha;
295 struct net_device *real_dev;
296 static const u8 flogi_maddr[ETH_ALEN] = FC_FCOE_FLOGI_MAC;
297 const struct net_device_ops *ops;
298
299 fcoe->netdev = netdev;
300
301
302 ops = netdev->netdev_ops;
303 if (ops->ndo_fcoe_enable) {
304 if (ops->ndo_fcoe_enable(netdev))
305 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
306 " specific feature for LLD.\n");
307 }
308
309
310 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
311 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
312 return -EOPNOTSUPP;
313 }
314
315
316
317 real_dev = is_vlan_dev(netdev) ? vlan_dev_real_dev(netdev) : netdev;
318 fcoe->realdev = real_dev;
319 rcu_read_lock();
320 for_each_dev_addr(real_dev, ha) {
321 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
322 (is_valid_ether_addr(ha->addr))) {
323 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
324 fip->spma = 1;
325 break;
326 }
327 }
328 rcu_read_unlock();
329
330
331 if (!fip->spma)
332 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
333
334
335
336
337
338
339 dev_uc_add(netdev, flogi_maddr);
340 if (fip->spma)
341 dev_uc_add(netdev, fip->ctl_src_addr);
342 if (fip->mode == FIP_MODE_VN2VN) {
343 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
344 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
345 } else
346 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
347
348
349
350
351
352 fcoe->fcoe_packet_type.func = fcoe_rcv;
353 fcoe->fcoe_packet_type.type = htons(ETH_P_FCOE);
354 fcoe->fcoe_packet_type.dev = netdev;
355 dev_add_pack(&fcoe->fcoe_packet_type);
356
357 fcoe->fip_packet_type.func = fcoe_fip_recv;
358 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
359 fcoe->fip_packet_type.dev = netdev;
360 dev_add_pack(&fcoe->fip_packet_type);
361
362 if (netdev != real_dev) {
363 fcoe->fip_vlan_packet_type.func = fcoe_fip_vlan_recv;
364 fcoe->fip_vlan_packet_type.type = htons(ETH_P_FIP);
365 fcoe->fip_vlan_packet_type.dev = real_dev;
366 dev_add_pack(&fcoe->fip_vlan_packet_type);
367 }
368 return 0;
369}
370
371
372
373
374
375
376
377
378static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
379 enum fip_mode fip_mode)
380{
381 struct fcoe_ctlr_device *ctlr_dev;
382 struct fcoe_ctlr *ctlr;
383 struct fcoe_interface *fcoe;
384 int size;
385 int err;
386
387 if (!try_module_get(THIS_MODULE)) {
388 FCOE_NETDEV_DBG(netdev,
389 "Could not get a reference to the module\n");
390 fcoe = ERR_PTR(-EBUSY);
391 goto out;
392 }
393
394 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
395 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
396 size);
397 if (!ctlr_dev) {
398 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
399 fcoe = ERR_PTR(-ENOMEM);
400 goto out_putmod;
401 }
402
403 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
404 ctlr->cdev = ctlr_dev;
405 fcoe = fcoe_ctlr_priv(ctlr);
406
407 dev_hold(netdev);
408
409
410
411
412 fcoe_ctlr_init(ctlr, fip_mode);
413 ctlr->send = fcoe_fip_send;
414 ctlr->update_mac = fcoe_update_src_mac;
415 ctlr->get_src_addr = fcoe_get_src_mac;
416
417 err = fcoe_interface_setup(fcoe, netdev);
418 if (err) {
419 fcoe_ctlr_destroy(ctlr);
420 fcoe_ctlr_device_delete(ctlr_dev);
421 dev_put(netdev);
422 fcoe = ERR_PTR(err);
423 goto out_putmod;
424 }
425
426 goto out;
427
428out_putmod:
429 module_put(THIS_MODULE);
430out:
431 return fcoe;
432}
433
434
435
436
437
438
439
440static void fcoe_interface_remove(struct fcoe_interface *fcoe)
441{
442 struct net_device *netdev = fcoe->netdev;
443 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
444 static const u8 flogi_maddr[ETH_ALEN] = FC_FCOE_FLOGI_MAC;
445 const struct net_device_ops *ops;
446
447
448
449
450
451
452
453 __dev_remove_pack(&fcoe->fcoe_packet_type);
454 __dev_remove_pack(&fcoe->fip_packet_type);
455 if (netdev != fcoe->realdev)
456 __dev_remove_pack(&fcoe->fip_vlan_packet_type);
457 synchronize_net();
458
459
460 dev_uc_del(netdev, flogi_maddr);
461 if (fip->spma)
462 dev_uc_del(netdev, fip->ctl_src_addr);
463 if (fip->mode == FIP_MODE_VN2VN) {
464 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
465 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
466 } else
467 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
468
469
470 ops = netdev->netdev_ops;
471 if (ops->ndo_fcoe_disable) {
472 if (ops->ndo_fcoe_disable(netdev))
473 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
474 " specific feature for LLD.\n");
475 }
476 fcoe->removed = 1;
477}
478
479
480
481
482
483
484static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
485{
486 struct net_device *netdev = fcoe->netdev;
487 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
488
489
490
491 fcoe_ctlr_destroy(fip);
492 scsi_host_put(fip->lp->host);
493 dev_put(netdev);
494 module_put(THIS_MODULE);
495}
496
497
498
499
500
501
502
503
504
505
506
507static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
508 struct packet_type *ptype,
509 struct net_device *orig_dev)
510{
511 struct fcoe_interface *fcoe;
512 struct fcoe_ctlr *ctlr;
513
514 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
515 ctlr = fcoe_to_ctlr(fcoe);
516 fcoe_ctlr_recv(ctlr, skb);
517 return 0;
518}
519
520
521
522
523
524
525
526
527
528
529
530static int fcoe_fip_vlan_recv(struct sk_buff *skb, struct net_device *netdev,
531 struct packet_type *ptype,
532 struct net_device *orig_dev)
533{
534 struct fcoe_interface *fcoe;
535 struct fcoe_ctlr *ctlr;
536
537 fcoe = container_of(ptype, struct fcoe_interface, fip_vlan_packet_type);
538 ctlr = fcoe_to_ctlr(fcoe);
539 fcoe_ctlr_recv(ctlr, skb);
540 return 0;
541}
542
543
544
545
546
547
548static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
549{
550 if (port->fcoe_pending_queue.qlen)
551 fcoe_check_wait_queue(port->lport, skb);
552 else if (fcoe_start_io(skb))
553 fcoe_check_wait_queue(port->lport, skb);
554}
555
556
557
558
559
560
561static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
562{
563 struct fcoe_interface *fcoe = fcoe_from_ctlr(fip);
564 struct fip_frame {
565 struct ethhdr eth;
566 struct fip_header fip;
567 } __packed *frame;
568
569
570
571
572 frame = (struct fip_frame *)skb->data;
573 if (ntohs(frame->eth.h_proto) == ETH_P_FIP &&
574 ntohs(frame->fip.fip_op) == FIP_OP_VLAN &&
575 fcoe->realdev != fcoe->netdev)
576 skb->dev = fcoe->realdev;
577 else
578 skb->dev = fcoe->netdev;
579 fcoe_port_send(lport_priv(fip->lp), skb);
580}
581
582
583
584
585
586
587
588
589
590static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
591{
592 struct fcoe_port *port = lport_priv(lport);
593 struct fcoe_interface *fcoe = port->priv;
594
595 if (!is_zero_ether_addr(port->data_src_addr))
596 dev_uc_del(fcoe->netdev, port->data_src_addr);
597 if (!is_zero_ether_addr(addr))
598 dev_uc_add(fcoe->netdev, addr);
599 memcpy(port->data_src_addr, addr, ETH_ALEN);
600}
601
602
603
604
605
606static u8 *fcoe_get_src_mac(struct fc_lport *lport)
607{
608 struct fcoe_port *port = lport_priv(lport);
609
610 return port->data_src_addr;
611}
612
613
614
615
616
617
618
619static int fcoe_lport_config(struct fc_lport *lport)
620{
621 lport->link_up = 0;
622 lport->qfull = 0;
623 lport->max_retry_count = 3;
624 lport->max_rport_retry_count = 3;
625 lport->e_d_tov = fcoe_e_d_tov;
626 lport->r_a_tov = fcoe_r_a_tov;
627 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
628 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
629 lport->does_npiv = 1;
630
631 fc_lport_init_stats(lport);
632
633
634 fc_lport_config(lport);
635
636
637 lport->crc_offload = 0;
638 lport->seq_offload = 0;
639 lport->lro_enabled = 0;
640 lport->lro_xid = 0;
641 lport->lso_max = 0;
642
643 return 0;
644}
645
646
647
648
649
650static void fcoe_netdev_features_change(struct fc_lport *lport,
651 struct net_device *netdev)
652{
653 mutex_lock(&lport->lp_mutex);
654
655 if (netdev->features & NETIF_F_SG)
656 lport->sg_supp = 1;
657 else
658 lport->sg_supp = 0;
659
660 if (netdev->features & NETIF_F_FCOE_CRC) {
661 lport->crc_offload = 1;
662 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
663 } else {
664 lport->crc_offload = 0;
665 }
666
667 if (netdev->features & NETIF_F_FSO) {
668 lport->seq_offload = 1;
669 lport->lso_max = netdev->gso_max_size;
670 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
671 lport->lso_max);
672 } else {
673 lport->seq_offload = 0;
674 lport->lso_max = 0;
675 }
676
677 if (netdev->fcoe_ddp_xid) {
678 lport->lro_enabled = 1;
679 lport->lro_xid = netdev->fcoe_ddp_xid;
680 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
681 lport->lro_xid);
682 } else {
683 lport->lro_enabled = 0;
684 lport->lro_xid = 0;
685 }
686
687 mutex_unlock(&lport->lp_mutex);
688}
689
690
691
692
693
694
695
696
697
698
699static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
700{
701 u32 mfs;
702 u64 wwnn, wwpn;
703 struct fcoe_interface *fcoe;
704 struct fcoe_ctlr *ctlr;
705 struct fcoe_port *port;
706
707
708 port = lport_priv(lport);
709 fcoe = port->priv;
710 ctlr = fcoe_to_ctlr(fcoe);
711
712
713 if (is_vlan_dev(netdev))
714 lport->vlan = vlan_dev_vlan_id(netdev);
715 else
716 lport->vlan = 0;
717
718
719
720
721
722
723 mfs = netdev->mtu;
724 if (netdev->features & NETIF_F_FCOE_MTU) {
725 mfs = FCOE_MTU;
726 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
727 }
728 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
729 if (fc_set_mfs(lport, mfs))
730 return -EINVAL;
731
732
733 fcoe_netdev_features_change(lport, netdev);
734
735 skb_queue_head_init(&port->fcoe_pending_queue);
736 port->fcoe_pending_queue_active = 0;
737 timer_setup(&port->timer, fcoe_queue_timer, 0);
738
739 fcoe_link_speed_update(lport);
740
741 if (!lport->vport) {
742 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
743 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
744 fc_set_wwnn(lport, wwnn);
745 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
746 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
747 2, 0);
748 fc_set_wwpn(lport, wwpn);
749 }
750
751 return 0;
752}
753
754
755
756
757
758
759
760
761
762
763static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
764{
765 int rc = 0;
766
767
768 lport->host->max_lun = FCOE_MAX_LUN;
769 lport->host->max_id = FCOE_MAX_FCP_TARGET;
770 lport->host->max_channel = 0;
771 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
772
773 if (lport->vport)
774 lport->host->transportt = fcoe_vport_scsi_transport;
775 else
776 lport->host->transportt = fcoe_nport_scsi_transport;
777
778
779 rc = scsi_add_host(lport->host, dev);
780 if (rc) {
781 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
782 "error on scsi_add_host\n");
783 return rc;
784 }
785
786 if (!lport->vport)
787 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
788
789 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
790 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
791 fcoe_netdev(lport)->name);
792
793 return 0;
794}
795
796
797
798
799
800
801
802
803
804
805static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
806{
807 struct fcoe_interface *fcoe;
808 struct fcoe_port *port;
809 struct net_device *realdev;
810 int rc;
811
812 port = lport_priv(lport);
813 fcoe = port->priv;
814 realdev = fcoe->realdev;
815
816
817 if (lport->vport)
818 return;
819
820 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
821 struct netdev_fcoe_hbainfo *fdmi;
822 fdmi = kzalloc(sizeof(*fdmi), GFP_KERNEL);
823 if (!fdmi)
824 return;
825
826 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
827 fdmi);
828 if (rc) {
829 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
830 "information from netdev.\n");
831 return;
832 }
833
834 snprintf(fc_host_serial_number(lport->host),
835 FC_SERIAL_NUMBER_SIZE,
836 "%s",
837 fdmi->serial_number);
838 snprintf(fc_host_manufacturer(lport->host),
839 FC_SERIAL_NUMBER_SIZE,
840 "%s",
841 fdmi->manufacturer);
842 snprintf(fc_host_model(lport->host),
843 FC_SYMBOLIC_NAME_SIZE,
844 "%s",
845 fdmi->model);
846 snprintf(fc_host_model_description(lport->host),
847 FC_SYMBOLIC_NAME_SIZE,
848 "%s",
849 fdmi->model_description);
850 snprintf(fc_host_hardware_version(lport->host),
851 FC_VERSION_STRING_SIZE,
852 "%s",
853 fdmi->hardware_version);
854 snprintf(fc_host_driver_version(lport->host),
855 FC_VERSION_STRING_SIZE,
856 "%s",
857 fdmi->driver_version);
858 snprintf(fc_host_optionrom_version(lport->host),
859 FC_VERSION_STRING_SIZE,
860 "%s",
861 fdmi->optionrom_version);
862 snprintf(fc_host_firmware_version(lport->host),
863 FC_VERSION_STRING_SIZE,
864 "%s",
865 fdmi->firmware_version);
866
867
868 lport->fdmi_enabled = 1;
869 kfree(fdmi);
870 } else {
871 lport->fdmi_enabled = 0;
872 printk(KERN_INFO "fcoe: No FDMI support.\n");
873 }
874}
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894static bool fcoe_oem_match(struct fc_frame *fp)
895{
896 struct fc_frame_header *fh = fc_frame_header_get(fp);
897 struct fcp_cmnd *fcp;
898
899 if (fc_fcp_is_read(fr_fsp(fp)) &&
900 (fr_fsp(fp)->data_len > fcoe_ddp_min))
901 return true;
902 else if ((fr_fsp(fp) == NULL) &&
903 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
904 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
905 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
906 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
907 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
908 return true;
909 }
910 return false;
911}
912
913
914
915
916
917
918
919static inline int fcoe_em_config(struct fc_lport *lport)
920{
921 struct fcoe_port *port = lport_priv(lport);
922 struct fcoe_interface *fcoe = port->priv;
923 struct fcoe_interface *oldfcoe = NULL;
924 struct net_device *old_real_dev, *cur_real_dev;
925 u16 min_xid = FCOE_MIN_XID;
926 u16 max_xid = FCOE_MAX_XID;
927
928
929
930
931
932 if (!lport->lro_enabled || !lport->lro_xid ||
933 (lport->lro_xid >= max_xid)) {
934 lport->lro_xid = 0;
935 goto skip_oem;
936 }
937
938
939
940
941
942 if (is_vlan_dev(fcoe->netdev))
943 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
944 else
945 cur_real_dev = fcoe->netdev;
946
947 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
948 if (is_vlan_dev(oldfcoe->netdev))
949 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
950 else
951 old_real_dev = oldfcoe->netdev;
952
953 if (cur_real_dev == old_real_dev) {
954 fcoe->oem = oldfcoe->oem;
955 break;
956 }
957 }
958
959 if (fcoe->oem) {
960 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
961 printk(KERN_ERR "fcoe_em_config: failed to add "
962 "offload em:%p on interface:%s\n",
963 fcoe->oem, fcoe->netdev->name);
964 return -ENOMEM;
965 }
966 } else {
967 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
968 FCOE_MIN_XID, lport->lro_xid,
969 fcoe_oem_match);
970 if (!fcoe->oem) {
971 printk(KERN_ERR "fcoe_em_config: failed to allocate "
972 "em for offload exches on interface:%s\n",
973 fcoe->netdev->name);
974 return -ENOMEM;
975 }
976 }
977
978
979
980
981 min_xid += lport->lro_xid + 1;
982
983skip_oem:
984 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
985 printk(KERN_ERR "fcoe_em_config: failed to "
986 "allocate em on interface %s\n", fcoe->netdev->name);
987 return -ENOMEM;
988 }
989
990 return 0;
991}
992
993
994
995
996
997
998
999
1000static void fcoe_if_destroy(struct fc_lport *lport)
1001{
1002 struct fcoe_port *port = lport_priv(lport);
1003 struct fcoe_interface *fcoe = port->priv;
1004 struct net_device *netdev = fcoe->netdev;
1005
1006 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
1007
1008
1009 fc_fabric_logoff(lport);
1010
1011
1012 fc_lport_destroy(lport);
1013
1014
1015 del_timer_sync(&port->timer);
1016
1017
1018 fcoe_clean_pending_queue(lport);
1019
1020 if (!is_zero_ether_addr(port->data_src_addr))
1021 dev_uc_del(netdev, port->data_src_addr);
1022 if (lport->vport)
1023 synchronize_net();
1024 else
1025 fcoe_interface_remove(fcoe);
1026
1027
1028 fcoe_percpu_clean(lport);
1029
1030
1031 fc_remove_host(lport->host);
1032 scsi_remove_host(lport->host);
1033
1034
1035 fc_fcp_destroy(lport);
1036
1037
1038 fc_exch_mgr_free(lport);
1039
1040
1041 fc_lport_free_stats(lport);
1042
1043
1044
1045
1046
1047 if (lport->vport)
1048 scsi_host_put(lport->host);
1049}
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1061 struct scatterlist *sgl, unsigned int sgc)
1062{
1063 struct net_device *netdev = fcoe_netdev(lport);
1064
1065 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1066 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1067 xid, sgl,
1068 sgc);
1069
1070 return 0;
1071}
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1083 struct scatterlist *sgl, unsigned int sgc)
1084{
1085 struct net_device *netdev = fcoe_netdev(lport);
1086
1087 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1088 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1089 sgl, sgc);
1090
1091 return 0;
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1103{
1104 struct net_device *netdev = fcoe_netdev(lport);
1105
1106 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1107 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1108 return 0;
1109}
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
1122 struct device *parent, int npiv)
1123{
1124 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1125 struct net_device *netdev = fcoe->netdev;
1126 struct fc_lport *lport, *n_port;
1127 struct fcoe_port *port;
1128 struct Scsi_Host *shost;
1129 int rc;
1130
1131
1132
1133
1134 struct fc_vport *vport = dev_to_vport(parent);
1135
1136 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
1137
1138 if (!npiv)
1139 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1140 else
1141 lport = libfc_vport_create(vport, sizeof(*port));
1142
1143 if (!lport) {
1144 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1145 rc = -ENOMEM;
1146 goto out;
1147 }
1148 port = lport_priv(lport);
1149 port->lport = lport;
1150 port->priv = fcoe;
1151 port->get_netdev = fcoe_netdev;
1152 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1153 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
1154 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
1155
1156
1157
1158
1159
1160 fcoe_hostlist_add(lport);
1161
1162
1163 rc = fcoe_lport_config(lport);
1164 if (rc) {
1165 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1166 "interface\n");
1167 goto out_host_put;
1168 }
1169
1170 if (npiv) {
1171 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1172 "%16.16llx %16.16llx\n",
1173 vport->node_name, vport->port_name);
1174 fc_set_wwnn(lport, vport->node_name);
1175 fc_set_wwpn(lport, vport->port_name);
1176 }
1177
1178
1179 rc = fcoe_netdev_config(lport, netdev);
1180 if (rc) {
1181 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1182 "interface\n");
1183 goto out_lp_destroy;
1184 }
1185
1186
1187 rc = fcoe_shost_config(lport, parent);
1188 if (rc) {
1189 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1190 "interface\n");
1191 goto out_lp_destroy;
1192 }
1193
1194
1195 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
1196 if (rc) {
1197 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1198 "interface\n");
1199 goto out_lp_destroy;
1200 }
1201
1202
1203 fcoe_fdmi_info(lport, netdev);
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 if (!npiv)
1215
1216 rc = fcoe_em_config(lport);
1217 else {
1218 shost = vport_to_shost(vport);
1219 n_port = shost_priv(shost);
1220 rc = fc_exch_mgr_list_clone(n_port, lport);
1221 }
1222
1223 if (rc) {
1224 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1225 goto out_lp_destroy;
1226 }
1227
1228 return lport;
1229
1230out_lp_destroy:
1231 fc_exch_mgr_free(lport);
1232out_host_put:
1233 fcoe_hostlist_del(lport);
1234 scsi_host_put(lport->host);
1235out:
1236 return ERR_PTR(rc);
1237}
1238
1239
1240
1241
1242
1243
1244
1245
1246static int __init fcoe_if_init(void)
1247{
1248
1249 fcoe_nport_scsi_transport =
1250 fc_attach_transport(&fcoe_nport_fc_functions);
1251 if (!fcoe_nport_scsi_transport)
1252 goto err;
1253
1254 fcoe_vport_scsi_transport =
1255 fc_attach_transport(&fcoe_vport_fc_functions);
1256 if (!fcoe_vport_scsi_transport)
1257 goto err_vport;
1258
1259 return 0;
1260
1261err_vport:
1262 fc_release_transport(fcoe_nport_scsi_transport);
1263err:
1264 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
1265 return -ENODEV;
1266}
1267
1268
1269
1270
1271
1272
1273
1274
1275static int __exit fcoe_if_exit(void)
1276{
1277 fc_release_transport(fcoe_nport_scsi_transport);
1278 fc_release_transport(fcoe_vport_scsi_transport);
1279 fcoe_nport_scsi_transport = NULL;
1280 fcoe_vport_scsi_transport = NULL;
1281 return 0;
1282}
1283
1284static void fcoe_thread_cleanup_local(unsigned int cpu)
1285{
1286 struct page *crc_eof;
1287 struct fcoe_percpu_s *p;
1288
1289 p = per_cpu_ptr(&fcoe_percpu, cpu);
1290 spin_lock_bh(&p->fcoe_rx_list.lock);
1291 crc_eof = p->crc_eof_page;
1292 p->crc_eof_page = NULL;
1293 p->crc_eof_offset = 0;
1294 spin_unlock_bh(&p->fcoe_rx_list.lock);
1295
1296 if (crc_eof)
1297 put_page(crc_eof);
1298 flush_work(&p->work);
1299}
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310static inline unsigned int fcoe_select_cpu(void)
1311{
1312 static unsigned int selected_cpu;
1313
1314 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1315 if (selected_cpu >= nr_cpu_ids)
1316 selected_cpu = cpumask_first(cpu_online_mask);
1317
1318 return selected_cpu;
1319}
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1334 struct packet_type *ptype, struct net_device *olddev)
1335{
1336 struct fc_lport *lport;
1337 struct fcoe_rcv_info *fr;
1338 struct fcoe_ctlr *ctlr;
1339 struct fcoe_interface *fcoe;
1340 struct fc_frame_header *fh;
1341 struct fcoe_percpu_s *fps;
1342 struct ethhdr *eh;
1343 unsigned int cpu;
1344
1345 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1346 ctlr = fcoe_to_ctlr(fcoe);
1347 lport = ctlr->lp;
1348 if (unlikely(!lport)) {
1349 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure\n");
1350 goto err2;
1351 }
1352 if (!lport->link_up)
1353 goto err2;
1354
1355 FCOE_NETDEV_DBG(netdev,
1356 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
1357 skb->len, skb->data_len, skb->head, skb->data,
1358 skb_tail_pointer(skb), skb_end_pointer(skb),
1359 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1360
1361
1362 skb = skb_share_check(skb, GFP_ATOMIC);
1363
1364 if (skb == NULL)
1365 return NET_RX_DROP;
1366
1367 eh = eth_hdr(skb);
1368
1369 if (is_fip_mode(ctlr) &&
1370 !ether_addr_equal(eh->h_source, ctlr->dest_addr)) {
1371 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1372 eh->h_source);
1373 goto err;
1374 }
1375
1376
1377
1378
1379
1380 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1381 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1382 goto err;
1383
1384 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1385 fh = (struct fc_frame_header *) skb_transport_header(skb);
1386
1387 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1388 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1389 eh->h_dest);
1390 goto err;
1391 }
1392
1393 fr = fcoe_dev_from_skb(skb);
1394 fr->fr_dev = lport;
1395
1396
1397
1398
1399
1400
1401
1402
1403 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1404 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1405 else {
1406 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1407 cpu = fcoe_select_cpu();
1408 else
1409 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
1410 }
1411
1412 if (cpu >= nr_cpu_ids)
1413 goto err;
1414
1415 fps = &per_cpu(fcoe_percpu, cpu);
1416 spin_lock(&fps->fcoe_rx_list.lock);
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1431 schedule_work_on(cpu, &fps->work);
1432 spin_unlock(&fps->fcoe_rx_list.lock);
1433
1434 return NET_RX_SUCCESS;
1435err:
1436 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
1437 put_cpu();
1438err2:
1439 kfree_skb(skb);
1440 return NET_RX_DROP;
1441}
1442
1443
1444
1445
1446
1447
1448
1449
1450static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
1451{
1452 struct fcoe_percpu_s *fps;
1453 int rc;
1454
1455 fps = &get_cpu_var(fcoe_percpu);
1456 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
1457 put_cpu_var(fcoe_percpu);
1458
1459 return rc;
1460}
1461
1462
1463
1464
1465
1466
1467
1468
1469static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1470{
1471 int wlen;
1472 u32 crc;
1473 struct ethhdr *eh;
1474 struct fcoe_crc_eof *cp;
1475 struct sk_buff *skb;
1476 struct fc_stats *stats;
1477 struct fc_frame_header *fh;
1478 unsigned int hlen;
1479 unsigned int tlen;
1480 unsigned int elen;
1481 struct fcoe_port *port = lport_priv(lport);
1482 struct fcoe_interface *fcoe = port->priv;
1483 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1484 u8 sof, eof;
1485 struct fcoe_hdr *hp;
1486
1487 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1488
1489 fh = fc_frame_header_get(fp);
1490 skb = fp_skb(fp);
1491 wlen = skb->len / FCOE_WORD_TO_BYTE;
1492
1493 if (!lport->link_up) {
1494 kfree_skb(skb);
1495 return 0;
1496 }
1497
1498 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
1499 fcoe_ctlr_els_send(ctlr, lport, skb))
1500 return 0;
1501
1502 sof = fr_sof(fp);
1503 eof = fr_eof(fp);
1504
1505 elen = sizeof(struct ethhdr);
1506 hlen = sizeof(struct fcoe_hdr);
1507 tlen = sizeof(struct fcoe_crc_eof);
1508 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1509
1510
1511 if (likely(lport->crc_offload)) {
1512 skb->ip_summed = CHECKSUM_PARTIAL;
1513 skb->csum_start = skb_headroom(skb);
1514 skb->csum_offset = skb->len;
1515 crc = 0;
1516 } else {
1517 skb->ip_summed = CHECKSUM_NONE;
1518 crc = fcoe_fc_crc(fp);
1519 }
1520
1521
1522 if (skb_is_nonlinear(skb)) {
1523 skb_frag_t *frag;
1524 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
1525 kfree_skb(skb);
1526 return -ENOMEM;
1527 }
1528 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1529 cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
1530 } else {
1531 cp = skb_put(skb, tlen);
1532 }
1533
1534 memset(cp, 0, sizeof(*cp));
1535 cp->fcoe_eof = eof;
1536 cp->fcoe_crc32 = cpu_to_le32(~crc);
1537
1538 if (skb_is_nonlinear(skb)) {
1539 kunmap_atomic(cp);
1540 cp = NULL;
1541 }
1542
1543
1544 skb_push(skb, elen + hlen);
1545 skb_reset_mac_header(skb);
1546 skb_reset_network_header(skb);
1547 skb->mac_len = elen;
1548 skb->protocol = htons(ETH_P_FCOE);
1549 skb->priority = fcoe->priority;
1550
1551 if (is_vlan_dev(fcoe->netdev) &&
1552 fcoe->realdev->features & NETIF_F_HW_VLAN_CTAG_TX) {
1553
1554 skb->dev = fcoe->realdev;
1555 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1556 vlan_dev_vlan_id(fcoe->netdev));
1557 } else
1558 skb->dev = fcoe->netdev;
1559
1560
1561 eh = eth_hdr(skb);
1562 eh->h_proto = htons(ETH_P_FCOE);
1563 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1564 if (ctlr->map_dest)
1565 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1566
1567 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1568 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
1569 else
1570 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
1571
1572 hp = (struct fcoe_hdr *)(eh + 1);
1573 memset(hp, 0, sizeof(*hp));
1574 if (FC_FCOE_VER)
1575 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1576 hp->fcoe_sof = sof;
1577
1578
1579 if (lport->seq_offload && fr_max_payload(fp)) {
1580 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1581 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1582 } else {
1583 skb_shinfo(skb)->gso_type = 0;
1584 skb_shinfo(skb)->gso_size = 0;
1585 }
1586
1587 stats = per_cpu_ptr(lport->stats, get_cpu());
1588 stats->TxFrames++;
1589 stats->TxWords += wlen;
1590 put_cpu();
1591
1592
1593 fr_dev(fp) = lport;
1594 fcoe_port_send(port, skb);
1595 return 0;
1596}
1597
1598
1599
1600
1601
1602
1603
1604
1605static inline int fcoe_filter_frames(struct fc_lport *lport,
1606 struct fc_frame *fp)
1607{
1608 struct fcoe_ctlr *ctlr;
1609 struct fcoe_interface *fcoe;
1610 struct fc_frame_header *fh;
1611 struct sk_buff *skb = (struct sk_buff *)fp;
1612 struct fc_stats *stats;
1613
1614
1615
1616
1617
1618
1619 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1620 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1621 else
1622 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1623
1624 fh = fc_frame_header_get(fp);
1625 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1626 return 0;
1627
1628 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
1629 ctlr = fcoe_to_ctlr(fcoe);
1630 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1631 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1632 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1633 return -EINVAL;
1634 }
1635
1636 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
1637 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1638 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1639 return 0;
1640 }
1641
1642 stats = per_cpu_ptr(lport->stats, get_cpu());
1643 stats->InvalidCRCCount++;
1644 if (stats->InvalidCRCCount < 5)
1645 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1646 put_cpu();
1647 return -EINVAL;
1648}
1649
1650
1651
1652
1653
1654static void fcoe_recv_frame(struct sk_buff *skb)
1655{
1656 u32 fr_len;
1657 struct fc_lport *lport;
1658 struct fcoe_rcv_info *fr;
1659 struct fc_stats *stats;
1660 struct fcoe_crc_eof crc_eof;
1661 struct fc_frame *fp;
1662 struct fcoe_hdr *hp;
1663
1664 fr = fcoe_dev_from_skb(skb);
1665 lport = fr->fr_dev;
1666 if (unlikely(!lport)) {
1667 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb\n");
1668 kfree_skb(skb);
1669 return;
1670 }
1671
1672 FCOE_NETDEV_DBG(skb->dev,
1673 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
1674 skb->len, skb->data_len,
1675 skb->head, skb->data, skb_tail_pointer(skb),
1676 skb_end_pointer(skb), skb->csum,
1677 skb->dev ? skb->dev->name : "<NULL>");
1678
1679 skb_linearize(skb);
1680
1681
1682
1683
1684
1685 hp = (struct fcoe_hdr *) skb_network_header(skb);
1686
1687 stats = per_cpu_ptr(lport->stats, get_cpu());
1688 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1689 if (stats->ErrorFrames < 5)
1690 printk(KERN_WARNING "fcoe: FCoE version "
1691 "mismatch: The frame has "
1692 "version %x, but the "
1693 "initiator supports version "
1694 "%x\n", FC_FCOE_DECAPS_VER(hp),
1695 FC_FCOE_VER);
1696 goto drop;
1697 }
1698
1699 skb_pull(skb, sizeof(struct fcoe_hdr));
1700 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1701
1702 stats->RxFrames++;
1703 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1704
1705 fp = (struct fc_frame *)skb;
1706 fc_frame_init(fp);
1707 fr_dev(fp) = lport;
1708 fr_sof(fp) = hp->fcoe_sof;
1709
1710
1711 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1712 goto drop;
1713 fr_eof(fp) = crc_eof.fcoe_eof;
1714 fr_crc(fp) = crc_eof.fcoe_crc32;
1715 if (pskb_trim(skb, fr_len))
1716 goto drop;
1717
1718 if (!fcoe_filter_frames(lport, fp)) {
1719 put_cpu();
1720 fc_exch_recv(lport, fp);
1721 return;
1722 }
1723drop:
1724 stats->ErrorFrames++;
1725 put_cpu();
1726 kfree_skb(skb);
1727}
1728
1729
1730
1731
1732
1733
1734static void fcoe_receive_work(struct work_struct *work)
1735{
1736 struct fcoe_percpu_s *p;
1737 struct sk_buff *skb;
1738 struct sk_buff_head tmp;
1739
1740 p = container_of(work, struct fcoe_percpu_s, work);
1741 skb_queue_head_init(&tmp);
1742
1743 spin_lock_bh(&p->fcoe_rx_list.lock);
1744 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1745 spin_unlock_bh(&p->fcoe_rx_list.lock);
1746
1747 if (!skb_queue_len(&tmp))
1748 return;
1749
1750 while ((skb = __skb_dequeue(&tmp)))
1751 fcoe_recv_frame(skb);
1752}
1753
1754
1755
1756
1757static void fcoe_dev_setup(void)
1758{
1759 register_dcbevent_notifier(&dcb_notifier);
1760 register_netdevice_notifier(&fcoe_notifier);
1761}
1762
1763
1764
1765
1766static void fcoe_dev_cleanup(void)
1767{
1768 unregister_dcbevent_notifier(&dcb_notifier);
1769 unregister_netdevice_notifier(&fcoe_notifier);
1770}
1771
1772static struct fcoe_interface *
1773fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1774{
1775 struct fcoe_interface *fcoe;
1776 struct net_device *real_dev;
1777
1778 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1779 if (is_vlan_dev(fcoe->netdev))
1780 real_dev = vlan_dev_real_dev(fcoe->netdev);
1781 else
1782 real_dev = fcoe->netdev;
1783
1784 if (netdev == real_dev)
1785 return fcoe;
1786 }
1787 return NULL;
1788}
1789
1790static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1791 ulong event, void *ptr)
1792{
1793 struct dcb_app_type *entry = ptr;
1794 struct fcoe_ctlr *ctlr;
1795 struct fcoe_interface *fcoe;
1796 struct net_device *netdev;
1797 int prio;
1798
1799 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1800 return NOTIFY_OK;
1801
1802 netdev = dev_get_by_index(&init_net, entry->ifindex);
1803 if (!netdev)
1804 return NOTIFY_OK;
1805
1806 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1807 dev_put(netdev);
1808 if (!fcoe)
1809 return NOTIFY_OK;
1810
1811 ctlr = fcoe_to_ctlr(fcoe);
1812
1813 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1814 prio = ffs(entry->app.priority) - 1;
1815 else
1816 prio = entry->app.priority;
1817
1818 if (prio < 0)
1819 return NOTIFY_OK;
1820
1821 if (entry->app.protocol == ETH_P_FIP ||
1822 entry->app.protocol == ETH_P_FCOE)
1823 ctlr->priority = prio;
1824
1825 if (entry->app.protocol == ETH_P_FCOE)
1826 fcoe->priority = prio;
1827
1828 return NOTIFY_OK;
1829}
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841static int fcoe_device_notification(struct notifier_block *notifier,
1842 ulong event, void *ptr)
1843{
1844 struct fcoe_ctlr_device *cdev;
1845 struct fc_lport *lport = NULL;
1846 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1847 struct fcoe_ctlr *ctlr;
1848 struct fcoe_interface *fcoe;
1849 struct fc_stats *stats;
1850 u32 link_possible = 1;
1851 u32 mfs;
1852 int rc = NOTIFY_OK;
1853
1854 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1855 if (fcoe->netdev == netdev) {
1856 ctlr = fcoe_to_ctlr(fcoe);
1857 lport = ctlr->lp;
1858 break;
1859 }
1860 }
1861 if (!lport) {
1862 rc = NOTIFY_DONE;
1863 goto out;
1864 }
1865
1866 switch (event) {
1867 case NETDEV_DOWN:
1868 case NETDEV_GOING_DOWN:
1869 link_possible = 0;
1870 break;
1871 case NETDEV_UP:
1872 case NETDEV_CHANGE:
1873 break;
1874 case NETDEV_CHANGEMTU:
1875 if (netdev->features & NETIF_F_FCOE_MTU)
1876 break;
1877 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1878 sizeof(struct fcoe_crc_eof));
1879 if (mfs >= FC_MIN_MAX_FRAME)
1880 fc_set_mfs(lport, mfs);
1881 break;
1882 case NETDEV_REGISTER:
1883 break;
1884 case NETDEV_UNREGISTER:
1885 list_del(&fcoe->list);
1886 fcoe_vport_remove(lport);
1887 mutex_lock(&fcoe_config_mutex);
1888 fcoe_if_destroy(lport);
1889 if (!fcoe->removed)
1890 fcoe_interface_remove(fcoe);
1891 fcoe_interface_cleanup(fcoe);
1892 mutex_unlock(&fcoe_config_mutex);
1893 fcoe_ctlr_device_delete(fcoe_ctlr_to_ctlr_dev(ctlr));
1894 goto out;
1895 case NETDEV_FEAT_CHANGE:
1896 fcoe_netdev_features_change(lport, netdev);
1897 break;
1898 default:
1899 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1900 "from netdev netlink\n", event);
1901 }
1902
1903 fcoe_link_speed_update(lport);
1904
1905 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
1906
1907 if (link_possible && !fcoe_link_ok(lport)) {
1908 switch (cdev->enabled) {
1909 case FCOE_CTLR_DISABLED:
1910 pr_info("Link up while interface is disabled.\n");
1911 break;
1912 case FCOE_CTLR_ENABLED:
1913 case FCOE_CTLR_UNUSED:
1914 fcoe_ctlr_link_up(ctlr);
1915 }
1916 } else if (fcoe_ctlr_link_down(ctlr)) {
1917 switch (cdev->enabled) {
1918 case FCOE_CTLR_DISABLED:
1919 pr_info("Link down while interface is disabled.\n");
1920 break;
1921 case FCOE_CTLR_ENABLED:
1922 case FCOE_CTLR_UNUSED:
1923 stats = per_cpu_ptr(lport->stats, get_cpu());
1924 stats->LinkFailureCount++;
1925 put_cpu();
1926 fcoe_clean_pending_queue(lport);
1927 }
1928 }
1929out:
1930 return rc;
1931}
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943static int fcoe_disable(struct net_device *netdev)
1944{
1945 struct fcoe_ctlr *ctlr;
1946 struct fcoe_interface *fcoe;
1947 int rc = 0;
1948
1949 mutex_lock(&fcoe_config_mutex);
1950
1951 rtnl_lock();
1952 fcoe = fcoe_hostlist_lookup_port(netdev);
1953 rtnl_unlock();
1954
1955 if (fcoe) {
1956 ctlr = fcoe_to_ctlr(fcoe);
1957 fcoe_ctlr_link_down(ctlr);
1958 fcoe_clean_pending_queue(ctlr->lp);
1959 } else
1960 rc = -ENODEV;
1961
1962 mutex_unlock(&fcoe_config_mutex);
1963 return rc;
1964}
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974static int fcoe_enable(struct net_device *netdev)
1975{
1976 struct fcoe_ctlr *ctlr;
1977 struct fcoe_interface *fcoe;
1978 int rc = 0;
1979
1980 mutex_lock(&fcoe_config_mutex);
1981 rtnl_lock();
1982 fcoe = fcoe_hostlist_lookup_port(netdev);
1983 rtnl_unlock();
1984
1985 if (!fcoe) {
1986 rc = -ENODEV;
1987 goto out;
1988 }
1989
1990 ctlr = fcoe_to_ctlr(fcoe);
1991
1992 if (!fcoe_link_ok(ctlr->lp))
1993 fcoe_ctlr_link_up(ctlr);
1994
1995out:
1996 mutex_unlock(&fcoe_config_mutex);
1997 return rc;
1998}
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2011{
2012 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2013 struct fc_lport *lport = ctlr->lp;
2014 struct net_device *netdev = fcoe_netdev(lport);
2015
2016 switch (cdev->enabled) {
2017 case FCOE_CTLR_ENABLED:
2018 return fcoe_enable(netdev);
2019 case FCOE_CTLR_DISABLED:
2020 return fcoe_disable(netdev);
2021 case FCOE_CTLR_UNUSED:
2022 default:
2023 return -ENOTSUPP;
2024 }
2025}
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev)
2036{
2037 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2038 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2039
2040 if (ctlr_dev->mode == FIP_CONN_TYPE_VN2VN &&
2041 ctlr->mode != FIP_MODE_VN2VN) {
2042 dev_mc_del(fcoe->netdev, FIP_ALL_ENODE_MACS);
2043 dev_mc_add(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2044 dev_mc_add(fcoe->netdev, FIP_ALL_P2P_MACS);
2045 } else if (ctlr->mode != FIP_MODE_FABRIC) {
2046 dev_mc_del(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2047 dev_mc_del(fcoe->netdev, FIP_ALL_P2P_MACS);
2048 dev_mc_add(fcoe->netdev, FIP_ALL_ENODE_MACS);
2049 }
2050 fcoe_ctlr_set_fip_mode(ctlr_dev);
2051}
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061static int fcoe_destroy(struct net_device *netdev)
2062{
2063 struct fcoe_ctlr *ctlr;
2064 struct fcoe_interface *fcoe;
2065 struct fc_lport *lport;
2066 struct fcoe_port *port;
2067 int rc = 0;
2068
2069 mutex_lock(&fcoe_config_mutex);
2070 rtnl_lock();
2071 fcoe = fcoe_hostlist_lookup_port(netdev);
2072 if (!fcoe) {
2073 rc = -ENODEV;
2074 goto out_nodev;
2075 }
2076 ctlr = fcoe_to_ctlr(fcoe);
2077 lport = ctlr->lp;
2078 port = lport_priv(lport);
2079 list_del(&fcoe->list);
2080 queue_work(fcoe_wq, &port->destroy_work);
2081out_nodev:
2082 rtnl_unlock();
2083 mutex_unlock(&fcoe_config_mutex);
2084 return rc;
2085}
2086
2087
2088
2089
2090
2091static void fcoe_destroy_work(struct work_struct *work)
2092{
2093 struct fcoe_ctlr_device *cdev;
2094 struct fcoe_ctlr *ctlr;
2095 struct fcoe_port *port;
2096 struct fcoe_interface *fcoe;
2097
2098 port = container_of(work, struct fcoe_port, destroy_work);
2099
2100 fcoe_vport_remove(port->lport);
2101
2102 mutex_lock(&fcoe_config_mutex);
2103
2104 fcoe = port->priv;
2105 ctlr = fcoe_to_ctlr(fcoe);
2106 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2107
2108 rtnl_lock();
2109 fcoe_if_destroy(port->lport);
2110 if (!fcoe->removed)
2111 fcoe_interface_remove(fcoe);
2112 rtnl_unlock();
2113 fcoe_interface_cleanup(fcoe);
2114
2115 mutex_unlock(&fcoe_config_mutex);
2116
2117 fcoe_ctlr_device_delete(cdev);
2118}
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129static bool fcoe_match(struct net_device *netdev)
2130{
2131 return true;
2132}
2133
2134
2135
2136
2137
2138static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2139{
2140 int ctlr_prio = TC_PRIO_BESTEFFORT;
2141 int fcoe_prio = TC_PRIO_INTERACTIVE;
2142 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2143#ifdef CONFIG_DCB
2144 int dcbx;
2145 u8 fup, up;
2146 struct net_device *netdev = fcoe->realdev;
2147 struct dcb_app app = {
2148 .priority = 0,
2149 .protocol = ETH_P_FCOE
2150 };
2151
2152
2153 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2154 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2155
2156 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2157 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2158 up = dcb_ieee_getapp_mask(netdev, &app);
2159 app.protocol = ETH_P_FIP;
2160 fup = dcb_ieee_getapp_mask(netdev, &app);
2161 } else {
2162 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2163 up = dcb_getapp(netdev, &app);
2164 app.protocol = ETH_P_FIP;
2165 fup = dcb_getapp(netdev, &app);
2166 }
2167
2168 fcoe_prio = ffs(up) ? ffs(up) - 1 : 0;
2169 ctlr_prio = ffs(fup) ? ffs(fup) - 1 : fcoe_prio;
2170 }
2171#endif
2172 fcoe->priority = fcoe_prio;
2173 ctlr->priority = ctlr_prio;
2174}
2175
2176enum fcoe_create_link_state {
2177 FCOE_CREATE_LINK_DOWN,
2178 FCOE_CREATE_LINK_UP,
2179};
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194static int _fcoe_create(struct net_device *netdev, enum fip_mode fip_mode,
2195 enum fcoe_create_link_state link_state)
2196{
2197 int rc = 0;
2198 struct fcoe_ctlr_device *ctlr_dev;
2199 struct fcoe_ctlr *ctlr;
2200 struct fcoe_interface *fcoe;
2201 struct fc_lport *lport;
2202
2203 mutex_lock(&fcoe_config_mutex);
2204 rtnl_lock();
2205
2206
2207 if (fcoe_hostlist_lookup(netdev)) {
2208 rc = -EEXIST;
2209 goto out_nodev;
2210 }
2211
2212 fcoe = fcoe_interface_create(netdev, fip_mode);
2213 if (IS_ERR(fcoe)) {
2214 rc = PTR_ERR(fcoe);
2215 goto out_nodev;
2216 }
2217
2218 ctlr = fcoe_to_ctlr(fcoe);
2219 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2220 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
2221 if (IS_ERR(lport)) {
2222 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
2223 netdev->name);
2224 rc = -EIO;
2225 if (!fcoe->removed)
2226 fcoe_interface_remove(fcoe);
2227 rtnl_unlock();
2228 fcoe_interface_cleanup(fcoe);
2229 mutex_unlock(&fcoe_config_mutex);
2230 fcoe_ctlr_device_delete(ctlr_dev);
2231 return rc;
2232 }
2233
2234
2235 ctlr->lp = lport;
2236
2237
2238 fcoe_dcb_create(fcoe);
2239
2240
2241 lport->boot_time = jiffies;
2242 fc_fabric_login(lport);
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256 if (link_state == FCOE_CREATE_LINK_UP)
2257 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2258 else
2259 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2260
2261 if (link_state == FCOE_CREATE_LINK_UP &&
2262 !fcoe_link_ok(lport)) {
2263 rtnl_unlock();
2264 fcoe_ctlr_link_up(ctlr);
2265 mutex_unlock(&fcoe_config_mutex);
2266 return rc;
2267 }
2268
2269out_nodev:
2270 rtnl_unlock();
2271 mutex_unlock(&fcoe_config_mutex);
2272
2273 return rc;
2274}
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode)
2286{
2287 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2288}
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301static int fcoe_ctlr_alloc(struct net_device *netdev)
2302{
2303 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2304 FCOE_CREATE_LINK_DOWN);
2305}
2306
2307
2308
2309
2310
2311
2312
2313
2314static int fcoe_link_ok(struct fc_lport *lport)
2315{
2316 struct net_device *netdev = fcoe_netdev(lport);
2317
2318 if (netif_oper_up(netdev))
2319 return 0;
2320 return -1;
2321}
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333static void fcoe_percpu_clean(struct fc_lport *lport)
2334{
2335 struct fcoe_percpu_s *pp;
2336 unsigned int cpu;
2337
2338 for_each_possible_cpu(cpu) {
2339 pp = &per_cpu(fcoe_percpu, cpu);
2340
2341 flush_work(&pp->work);
2342 }
2343}
2344
2345
2346
2347
2348
2349
2350
2351static int fcoe_reset(struct Scsi_Host *shost)
2352{
2353 struct fc_lport *lport = shost_priv(shost);
2354 struct fcoe_port *port = lport_priv(lport);
2355 struct fcoe_interface *fcoe = port->priv;
2356 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2357 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2358
2359 fcoe_ctlr_link_down(ctlr);
2360 fcoe_clean_pending_queue(ctlr->lp);
2361
2362 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2363 !fcoe_link_ok(ctlr->lp))
2364 fcoe_ctlr_link_up(ctlr);
2365 return 0;
2366}
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376static struct fcoe_interface *
2377fcoe_hostlist_lookup_port(const struct net_device *netdev)
2378{
2379 struct fcoe_interface *fcoe;
2380
2381 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
2382 if (fcoe->netdev == netdev)
2383 return fcoe;
2384 }
2385 return NULL;
2386}
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
2398{
2399 struct fcoe_ctlr *ctlr;
2400 struct fcoe_interface *fcoe;
2401
2402 fcoe = fcoe_hostlist_lookup_port(netdev);
2403 ctlr = fcoe_to_ctlr(fcoe);
2404 return (fcoe) ? ctlr->lp : NULL;
2405}
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416static int fcoe_hostlist_add(const struct fc_lport *lport)
2417{
2418 struct fcoe_interface *fcoe;
2419 struct fcoe_port *port;
2420
2421 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2422 if (!fcoe) {
2423 port = lport_priv(lport);
2424 fcoe = port->priv;
2425 list_add_tail(&fcoe->list, &fcoe_hostlist);
2426 }
2427 return 0;
2428}
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438static void fcoe_hostlist_del(const struct fc_lport *lport)
2439{
2440 struct fcoe_interface *fcoe;
2441 struct fcoe_port *port;
2442
2443 port = lport_priv(lport);
2444 fcoe = port->priv;
2445 list_del(&fcoe->list);
2446 return;
2447}
2448
2449static struct fcoe_transport fcoe_sw_transport = {
2450 .name = {FCOE_TRANSPORT_DEFAULT},
2451 .attached = false,
2452 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2453 .match = fcoe_match,
2454 .alloc = fcoe_ctlr_alloc,
2455 .create = fcoe_create,
2456 .destroy = fcoe_destroy,
2457 .enable = fcoe_enable,
2458 .disable = fcoe_disable,
2459};
2460
2461
2462
2463
2464
2465
2466static int __init fcoe_init(void)
2467{
2468 struct fcoe_percpu_s *p;
2469 unsigned int cpu;
2470 int rc = 0;
2471
2472 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2473 if (!fcoe_wq)
2474 return -ENOMEM;
2475
2476
2477 rc = fcoe_transport_attach(&fcoe_sw_transport);
2478 if (rc) {
2479 printk(KERN_ERR "failed to register an fcoe transport, check "
2480 "if libfcoe is loaded\n");
2481 goto out_destroy;
2482 }
2483
2484 mutex_lock(&fcoe_config_mutex);
2485
2486 for_each_possible_cpu(cpu) {
2487 p = per_cpu_ptr(&fcoe_percpu, cpu);
2488 INIT_WORK(&p->work, fcoe_receive_work);
2489 skb_queue_head_init(&p->fcoe_rx_list);
2490 }
2491
2492
2493 fcoe_dev_setup();
2494
2495 rc = fcoe_if_init();
2496 if (rc)
2497 goto out_free;
2498
2499 mutex_unlock(&fcoe_config_mutex);
2500 return 0;
2501
2502out_free:
2503 mutex_unlock(&fcoe_config_mutex);
2504out_destroy:
2505 destroy_workqueue(fcoe_wq);
2506 return rc;
2507}
2508module_init(fcoe_init);
2509
2510
2511
2512
2513
2514
2515static void __exit fcoe_exit(void)
2516{
2517 struct fcoe_interface *fcoe, *tmp;
2518 struct fcoe_ctlr *ctlr;
2519 struct fcoe_port *port;
2520 unsigned int cpu;
2521
2522 mutex_lock(&fcoe_config_mutex);
2523
2524 fcoe_dev_cleanup();
2525
2526
2527 rtnl_lock();
2528 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
2529 ctlr = fcoe_to_ctlr(fcoe);
2530 port = lport_priv(ctlr->lp);
2531 fcoe_hostlist_del(port->lport);
2532 queue_work(fcoe_wq, &port->destroy_work);
2533 }
2534 rtnl_unlock();
2535
2536 for_each_possible_cpu(cpu)
2537 fcoe_thread_cleanup_local(cpu);
2538
2539 mutex_unlock(&fcoe_config_mutex);
2540
2541
2542
2543
2544
2545 destroy_workqueue(fcoe_wq);
2546
2547
2548
2549
2550
2551
2552 fcoe_if_exit();
2553
2554
2555 fcoe_transport_detach(&fcoe_sw_transport);
2556}
2557module_exit(fcoe_exit);
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2569{
2570 struct fcoe_ctlr *fip = arg;
2571 struct fc_exch *exch = fc_seq_exch(seq);
2572 struct fc_lport *lport = exch->lp;
2573 u8 *mac;
2574
2575 if (IS_ERR(fp))
2576 goto done;
2577
2578 mac = fr_cb(fp)->granted_mac;
2579
2580 if (is_zero_ether_addr(mac))
2581 fcoe_ctlr_recv_flogi(fip, lport, fp);
2582 if (!is_zero_ether_addr(mac))
2583 fcoe_update_src_mac(lport, mac);
2584done:
2585 fc_lport_flogi_resp(seq, fp, lport);
2586}
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2598{
2599 struct fc_lport *lport = arg;
2600 static u8 zero_mac[ETH_ALEN] = { 0 };
2601
2602 if (!IS_ERR(fp))
2603 fcoe_update_src_mac(lport, zero_mac);
2604 fc_lport_logo_resp(seq, fp, lport);
2605}
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2617 struct fc_frame *fp, unsigned int op,
2618 void (*resp)(struct fc_seq *,
2619 struct fc_frame *,
2620 void *),
2621 void *arg, u32 timeout)
2622{
2623 struct fcoe_port *port = lport_priv(lport);
2624 struct fcoe_interface *fcoe = port->priv;
2625 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
2626 struct fc_frame_header *fh = fc_frame_header_get(fp);
2627
2628 switch (op) {
2629 case ELS_FLOGI:
2630 case ELS_FDISC:
2631 if (lport->point_to_multipoint)
2632 break;
2633 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2634 fip, timeout);
2635 case ELS_LOGO:
2636
2637 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2638 break;
2639 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2640 lport, timeout);
2641 }
2642 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2643}
2644
2645
2646
2647
2648
2649
2650
2651
2652static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2653{
2654 struct Scsi_Host *shost = vport_to_shost(vport);
2655 struct fc_lport *n_port = shost_priv(shost);
2656 struct fcoe_port *port = lport_priv(n_port);
2657 struct fcoe_interface *fcoe = port->priv;
2658 struct net_device *netdev = fcoe->netdev;
2659 struct fc_lport *vn_port;
2660 int rc;
2661 char buf[32];
2662
2663 rc = fcoe_validate_vport_create(vport);
2664 if (rc) {
2665 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
2666 printk(KERN_ERR "fcoe: Failed to create vport, "
2667 "WWPN (0x%s) already exists\n",
2668 buf);
2669 return rc;
2670 }
2671
2672 mutex_lock(&fcoe_config_mutex);
2673 rtnl_lock();
2674 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2675 rtnl_unlock();
2676 mutex_unlock(&fcoe_config_mutex);
2677
2678 if (IS_ERR(vn_port)) {
2679 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2680 netdev->name);
2681 return -EIO;
2682 }
2683
2684 if (disabled) {
2685 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2686 } else {
2687 vn_port->boot_time = jiffies;
2688 fc_fabric_login(vn_port);
2689 fc_vport_setlink(vn_port);
2690 }
2691 return 0;
2692}
2693
2694
2695
2696
2697
2698
2699
2700static int fcoe_vport_destroy(struct fc_vport *vport)
2701{
2702 struct Scsi_Host *shost = vport_to_shost(vport);
2703 struct fc_lport *n_port = shost_priv(shost);
2704 struct fc_lport *vn_port = vport->dd_data;
2705
2706 mutex_lock(&n_port->lp_mutex);
2707 list_del(&vn_port->list);
2708 mutex_unlock(&n_port->lp_mutex);
2709
2710 mutex_lock(&fcoe_config_mutex);
2711 rtnl_lock();
2712 fcoe_if_destroy(vn_port);
2713 rtnl_unlock();
2714 mutex_unlock(&fcoe_config_mutex);
2715
2716 return 0;
2717}
2718
2719
2720
2721
2722
2723static void fcoe_vport_remove(struct fc_lport *lport)
2724{
2725 struct Scsi_Host *shost;
2726 struct fc_host_attrs *fc_host;
2727 unsigned long flags;
2728 struct fc_vport *vport;
2729 struct fc_vport *next_vport;
2730
2731 shost = lport->host;
2732 fc_host = shost_to_fc_host(shost);
2733
2734
2735 spin_lock_irqsave(shost->host_lock, flags);
2736 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2737 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2738 continue;
2739 } else {
2740 vport->flags |= FC_VPORT_DELETING;
2741 queue_work(fc_host_work_q(shost),
2742 &vport->vport_delete_work);
2743 }
2744 }
2745 spin_unlock_irqrestore(shost->host_lock, flags);
2746
2747 flush_workqueue(fc_host_work_q(shost));
2748}
2749
2750
2751
2752
2753
2754
2755static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2756{
2757 struct fc_lport *lport = vport->dd_data;
2758
2759 if (disable) {
2760 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2761 fc_fabric_logoff(lport);
2762 } else {
2763 lport->boot_time = jiffies;
2764 fc_fabric_login(lport);
2765 fc_vport_setlink(lport);
2766 }
2767
2768 return 0;
2769}
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2780{
2781 struct fc_lport *lport = vport->dd_data;
2782 struct fc_frame *fp;
2783 size_t len;
2784
2785 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2786 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2787 fcoe_netdev(lport)->name, vport->symbolic_name);
2788
2789 if (lport->state != LPORT_ST_READY)
2790 return;
2791
2792 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2793 fp = fc_frame_alloc(lport,
2794 sizeof(struct fc_ct_hdr) +
2795 sizeof(struct fc_ns_rspn) + len);
2796 if (!fp)
2797 return;
2798 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2799 NULL, NULL, 3 * lport->r_a_tov);
2800}
2801
2802static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2803{
2804 struct fcoe_ctlr_device *ctlr_dev =
2805 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2806 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2807 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2808
2809 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2810}
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824static void fcoe_set_port_id(struct fc_lport *lport,
2825 u32 port_id, struct fc_frame *fp)
2826{
2827 struct fcoe_port *port = lport_priv(lport);
2828 struct fcoe_interface *fcoe = port->priv;
2829 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
2830
2831 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2832 fcoe_ctlr_recv_flogi(ctlr, lport, fp);
2833}
2834