Commit | Line | Data |
---|---|---|
85b4aa49 RL |
1 | /* |
2 | * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | * | |
17 | * Maintained at www.Open-FCoE.org | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/version.h> | |
85b4aa49 | 22 | #include <linux/spinlock.h> |
85b4aa49 RL |
23 | #include <linux/netdevice.h> |
24 | #include <linux/etherdevice.h> | |
25 | #include <linux/ethtool.h> | |
26 | #include <linux/if_ether.h> | |
27 | #include <linux/if_vlan.h> | |
85b4aa49 RL |
28 | #include <linux/crc32.h> |
29 | #include <linux/cpu.h> | |
30 | #include <linux/fs.h> | |
31 | #include <linux/sysfs.h> | |
32 | #include <linux/ctype.h> | |
33 | #include <scsi/scsi_tcq.h> | |
34 | #include <scsi/scsicam.h> | |
35 | #include <scsi/scsi_transport.h> | |
36 | #include <scsi/scsi_transport_fc.h> | |
37 | #include <net/rtnetlink.h> | |
38 | ||
39 | #include <scsi/fc/fc_encaps.h> | |
97c8389d | 40 | #include <scsi/fc/fc_fip.h> |
85b4aa49 RL |
41 | |
42 | #include <scsi/libfc.h> | |
43 | #include <scsi/fc_frame.h> | |
44 | #include <scsi/libfcoe.h> | |
85b4aa49 | 45 | |
fdd78027 | 46 | #include "fcoe.h" |
7f349142 | 47 | |
fdd78027 | 48 | static int debug_fcoe; |
7f349142 | 49 | |
85b4aa49 RL |
50 | MODULE_AUTHOR("Open-FCoE.org"); |
51 | MODULE_DESCRIPTION("FCoE"); | |
9b34ecff | 52 | MODULE_LICENSE("GPL v2"); |
85b4aa49 RL |
53 | |
54 | /* fcoe host list */ | |
55 | LIST_HEAD(fcoe_hostlist); | |
56 | DEFINE_RWLOCK(fcoe_hostlist_lock); | |
57 | DEFINE_TIMER(fcoe_timer, NULL, 0, 0); | |
5e5e92df | 58 | DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu); |
85b4aa49 | 59 | |
85b4aa49 | 60 | /* Function Prototyes */ |
fdd78027 VD |
61 | static int fcoe_reset(struct Scsi_Host *shost); |
62 | static int fcoe_xmit(struct fc_lport *, struct fc_frame *); | |
63 | static int fcoe_rcv(struct sk_buff *, struct net_device *, | |
64 | struct packet_type *, struct net_device *); | |
65 | static int fcoe_percpu_receive_thread(void *arg); | |
66 | static void fcoe_clean_pending_queue(struct fc_lport *lp); | |
67 | static void fcoe_percpu_clean(struct fc_lport *lp); | |
68 | static int fcoe_link_ok(struct fc_lport *lp); | |
69 | ||
70 | static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *); | |
71 | static int fcoe_hostlist_add(const struct fc_lport *); | |
72 | static int fcoe_hostlist_remove(const struct fc_lport *); | |
73 | ||
85b4aa49 | 74 | static int fcoe_check_wait_queue(struct fc_lport *); |
85b4aa49 RL |
75 | static int fcoe_device_notification(struct notifier_block *, ulong, void *); |
76 | static void fcoe_dev_setup(void); | |
77 | static void fcoe_dev_cleanup(void); | |
78 | ||
79 | /* notification function from net device */ | |
80 | static struct notifier_block fcoe_notifier = { | |
81 | .notifier_call = fcoe_device_notification, | |
82 | }; | |
83 | ||
7f349142 VD |
84 | static struct scsi_transport_template *scsi_transport_fcoe_sw; |
85 | ||
86 | struct fc_function_template fcoe_transport_function = { | |
87 | .show_host_node_name = 1, | |
88 | .show_host_port_name = 1, | |
89 | .show_host_supported_classes = 1, | |
90 | .show_host_supported_fc4s = 1, | |
91 | .show_host_active_fc4s = 1, | |
92 | .show_host_maxframe_size = 1, | |
93 | ||
94 | .show_host_port_id = 1, | |
95 | .show_host_supported_speeds = 1, | |
96 | .get_host_speed = fc_get_host_speed, | |
97 | .show_host_speed = 1, | |
98 | .show_host_port_type = 1, | |
99 | .get_host_port_state = fc_get_host_port_state, | |
100 | .show_host_port_state = 1, | |
101 | .show_host_symbolic_name = 1, | |
102 | ||
103 | .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv), | |
104 | .show_rport_maxframe_size = 1, | |
105 | .show_rport_supported_classes = 1, | |
106 | ||
107 | .show_host_fabric_name = 1, | |
108 | .show_starget_node_name = 1, | |
109 | .show_starget_port_name = 1, | |
110 | .show_starget_port_id = 1, | |
111 | .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo, | |
112 | .show_rport_dev_loss_tmo = 1, | |
113 | .get_fc_host_stats = fc_get_host_stats, | |
114 | .issue_fc_host_lip = fcoe_reset, | |
115 | ||
116 | .terminate_rport_io = fc_rport_terminate_io, | |
117 | }; | |
118 | ||
119 | static struct scsi_host_template fcoe_shost_template = { | |
120 | .module = THIS_MODULE, | |
121 | .name = "FCoE Driver", | |
122 | .proc_name = FCOE_NAME, | |
123 | .queuecommand = fc_queuecommand, | |
124 | .eh_abort_handler = fc_eh_abort, | |
125 | .eh_device_reset_handler = fc_eh_device_reset, | |
126 | .eh_host_reset_handler = fc_eh_host_reset, | |
127 | .slave_alloc = fc_slave_alloc, | |
128 | .change_queue_depth = fc_change_queue_depth, | |
129 | .change_queue_type = fc_change_queue_type, | |
130 | .this_id = -1, | |
131 | .cmd_per_lun = 32, | |
132 | .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS, | |
133 | .use_clustering = ENABLE_CLUSTERING, | |
134 | .sg_tablesize = SG_ALL, | |
135 | .max_sectors = 0xffff, | |
136 | }; | |
137 | ||
ab6b85c1 VD |
138 | /** |
139 | * fcoe_fip_recv - handle a received FIP frame. | |
140 | * @skb: the receive skb | |
141 | * @dev: associated &net_device | |
142 | * @ptype: the &packet_type structure which was used to register this handler. | |
143 | * @orig_dev: original receive &net_device, in case @dev is a bond. | |
144 | * | |
145 | * Returns: 0 for success | |
146 | */ | |
147 | static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev, | |
148 | struct packet_type *ptype, | |
149 | struct net_device *orig_dev) | |
150 | { | |
151 | struct fcoe_softc *fc; | |
152 | ||
153 | fc = container_of(ptype, struct fcoe_softc, fip_packet_type); | |
154 | fcoe_ctlr_recv(&fc->ctlr, skb); | |
155 | return 0; | |
156 | } | |
157 | ||
158 | /** | |
159 | * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame. | |
160 | * @fip: FCoE controller. | |
161 | * @skb: FIP Packet. | |
162 | */ | |
163 | static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb) | |
164 | { | |
165 | skb->dev = fcoe_from_ctlr(fip)->real_dev; | |
166 | dev_queue_xmit(skb); | |
167 | } | |
168 | ||
169 | /** | |
170 | * fcoe_update_src_mac() - Update Ethernet MAC filters. | |
171 | * @fip: FCoE controller. | |
172 | * @old: Unicast MAC address to delete if the MAC is non-zero. | |
173 | * @new: Unicast MAC address to add. | |
174 | * | |
175 | * Remove any previously-set unicast MAC filter. | |
176 | * Add secondary FCoE MAC address filter for our OUI. | |
177 | */ | |
178 | static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new) | |
179 | { | |
180 | struct fcoe_softc *fc; | |
181 | ||
182 | fc = fcoe_from_ctlr(fip); | |
183 | rtnl_lock(); | |
184 | if (!is_zero_ether_addr(old)) | |
185 | dev_unicast_delete(fc->real_dev, old, ETH_ALEN); | |
186 | dev_unicast_add(fc->real_dev, new, ETH_ALEN); | |
187 | rtnl_unlock(); | |
188 | } | |
189 | ||
7f349142 VD |
190 | /** |
191 | * fcoe_lport_config() - sets up the fc_lport | |
192 | * @lp: ptr to the fc_lport | |
193 | * @shost: ptr to the parent scsi host | |
194 | * | |
195 | * Returns: 0 for success | |
196 | */ | |
197 | static int fcoe_lport_config(struct fc_lport *lp) | |
198 | { | |
199 | lp->link_up = 0; | |
200 | lp->qfull = 0; | |
201 | lp->max_retry_count = 3; | |
202 | lp->e_d_tov = 2 * 1000; /* FC-FS default */ | |
203 | lp->r_a_tov = 2 * 2 * 1000; | |
204 | lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS | | |
205 | FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL); | |
206 | ||
207 | fc_lport_init_stats(lp); | |
208 | ||
209 | /* lport fc_lport related configuration */ | |
210 | fc_lport_config(lp); | |
211 | ||
212 | /* offload related configuration */ | |
213 | lp->crc_offload = 0; | |
214 | lp->seq_offload = 0; | |
215 | lp->lro_enabled = 0; | |
216 | lp->lro_xid = 0; | |
217 | lp->lso_max = 0; | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
ab6b85c1 VD |
222 | /** |
223 | * fcoe_netdev_cleanup() - clean up netdev configurations | |
224 | * @fc: ptr to the fcoe_softc | |
225 | */ | |
226 | void fcoe_netdev_cleanup(struct fcoe_softc *fc) | |
227 | { | |
228 | u8 flogi_maddr[ETH_ALEN]; | |
229 | ||
230 | /* Don't listen for Ethernet packets anymore */ | |
231 | dev_remove_pack(&fc->fcoe_packet_type); | |
232 | dev_remove_pack(&fc->fip_packet_type); | |
233 | ||
234 | /* Delete secondary MAC addresses */ | |
235 | rtnl_lock(); | |
236 | memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN); | |
237 | dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN); | |
238 | if (!is_zero_ether_addr(fc->ctlr.data_src_addr)) | |
239 | dev_unicast_delete(fc->real_dev, | |
240 | fc->ctlr.data_src_addr, ETH_ALEN); | |
241 | dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0); | |
242 | rtnl_unlock(); | |
243 | } | |
244 | ||
7f349142 VD |
245 | /** |
246 | * fcoe_netdev_config() - Set up netdev for SW FCoE | |
247 | * @lp : ptr to the fc_lport | |
248 | * @netdev : ptr to the associated netdevice struct | |
249 | * | |
250 | * Must be called after fcoe_lport_config() as it will use lport mutex | |
251 | * | |
252 | * Returns : 0 for success | |
253 | */ | |
254 | static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev) | |
255 | { | |
256 | u32 mfs; | |
257 | u64 wwnn, wwpn; | |
258 | struct fcoe_softc *fc; | |
259 | u8 flogi_maddr[ETH_ALEN]; | |
260 | ||
261 | /* Setup lport private data to point to fcoe softc */ | |
262 | fc = lport_priv(lp); | |
97c8389d | 263 | fc->ctlr.lp = lp; |
7f349142 VD |
264 | fc->real_dev = netdev; |
265 | fc->phys_dev = netdev; | |
266 | ||
267 | /* Require support for get_pauseparam ethtool op. */ | |
268 | if (netdev->priv_flags & IFF_802_1Q_VLAN) | |
269 | fc->phys_dev = vlan_dev_real_dev(netdev); | |
270 | ||
271 | /* Do not support for bonding device */ | |
272 | if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) || | |
273 | (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) || | |
274 | (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) { | |
275 | return -EOPNOTSUPP; | |
276 | } | |
277 | ||
278 | /* | |
279 | * Determine max frame size based on underlying device and optional | |
280 | * user-configured limit. If the MFS is too low, fcoe_link_ok() | |
281 | * will return 0, so do this first. | |
282 | */ | |
283 | mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) + | |
284 | sizeof(struct fcoe_crc_eof)); | |
285 | if (fc_set_mfs(lp, mfs)) | |
286 | return -EINVAL; | |
287 | ||
7f349142 VD |
288 | /* offload features support */ |
289 | if (fc->real_dev->features & NETIF_F_SG) | |
290 | lp->sg_supp = 1; | |
291 | ||
292 | #ifdef NETIF_F_FCOE_CRC | |
293 | if (netdev->features & NETIF_F_FCOE_CRC) { | |
294 | lp->crc_offload = 1; | |
295 | printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n", | |
296 | netdev->name); | |
297 | } | |
298 | #endif | |
299 | #ifdef NETIF_F_FSO | |
300 | if (netdev->features & NETIF_F_FSO) { | |
301 | lp->seq_offload = 1; | |
302 | lp->lso_max = netdev->gso_max_size; | |
303 | printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n", | |
304 | netdev->name, lp->lso_max); | |
305 | } | |
306 | #endif | |
307 | if (netdev->fcoe_ddp_xid) { | |
308 | lp->lro_enabled = 1; | |
309 | lp->lro_xid = netdev->fcoe_ddp_xid; | |
310 | printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n", | |
311 | netdev->name, lp->lro_xid); | |
312 | } | |
313 | skb_queue_head_init(&fc->fcoe_pending_queue); | |
314 | fc->fcoe_pending_queue_active = 0; | |
315 | ||
316 | /* setup Source Mac Address */ | |
97c8389d | 317 | memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr, |
7f349142 VD |
318 | fc->real_dev->addr_len); |
319 | ||
320 | wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0); | |
321 | fc_set_wwnn(lp, wwnn); | |
322 | /* XXX - 3rd arg needs to be vlan id */ | |
323 | wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0); | |
324 | fc_set_wwpn(lp, wwpn); | |
325 | ||
326 | /* | |
327 | * Add FCoE MAC address as second unicast MAC address | |
328 | * or enter promiscuous mode if not capable of listening | |
329 | * for multiple unicast MACs. | |
330 | */ | |
331 | rtnl_lock(); | |
332 | memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN); | |
333 | dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN); | |
334 | rtnl_unlock(); | |
335 | ||
336 | /* | |
337 | * setup the receive function from ethernet driver | |
338 | * on the ethertype for the given device | |
339 | */ | |
340 | fc->fcoe_packet_type.func = fcoe_rcv; | |
341 | fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE); | |
342 | fc->fcoe_packet_type.dev = fc->real_dev; | |
343 | dev_add_pack(&fc->fcoe_packet_type); | |
344 | ||
ab6b85c1 VD |
345 | fc->fip_packet_type.func = fcoe_fip_recv; |
346 | fc->fip_packet_type.type = htons(ETH_P_FIP); | |
347 | fc->fip_packet_type.dev = fc->real_dev; | |
348 | dev_add_pack(&fc->fip_packet_type); | |
349 | ||
7f349142 VD |
350 | return 0; |
351 | } | |
352 | ||
353 | /** | |
354 | * fcoe_shost_config() - Sets up fc_lport->host | |
355 | * @lp : ptr to the fc_lport | |
356 | * @shost : ptr to the associated scsi host | |
357 | * @dev : device associated to scsi host | |
358 | * | |
359 | * Must be called after fcoe_lport_config() and fcoe_netdev_config() | |
360 | * | |
361 | * Returns : 0 for success | |
362 | */ | |
363 | static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost, | |
364 | struct device *dev) | |
365 | { | |
366 | int rc = 0; | |
367 | ||
368 | /* lport scsi host config */ | |
369 | lp->host = shost; | |
370 | ||
371 | lp->host->max_lun = FCOE_MAX_LUN; | |
372 | lp->host->max_id = FCOE_MAX_FCP_TARGET; | |
373 | lp->host->max_channel = 0; | |
374 | lp->host->transportt = scsi_transport_fcoe_sw; | |
375 | ||
376 | /* add the new host to the SCSI-ml */ | |
377 | rc = scsi_add_host(lp->host, dev); | |
378 | if (rc) { | |
379 | FC_DBG("fcoe_shost_config:error on scsi_add_host\n"); | |
380 | return rc; | |
381 | } | |
382 | sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s", | |
383 | FCOE_NAME, FCOE_VERSION, | |
384 | fcoe_netdev(lp)->name); | |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
389 | /** | |
390 | * fcoe_em_config() - allocates em for this lport | |
391 | * @lp: the port that em is to allocated for | |
392 | * | |
393 | * Returns : 0 on success | |
394 | */ | |
395 | static inline int fcoe_em_config(struct fc_lport *lp) | |
396 | { | |
397 | BUG_ON(lp->emp); | |
398 | ||
399 | lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3, | |
400 | FCOE_MIN_XID, FCOE_MAX_XID); | |
401 | if (!lp->emp) | |
402 | return -ENOMEM; | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
407 | /** | |
408 | * fcoe_if_destroy() - FCoE software HBA tear-down function | |
409 | * @netdev: ptr to the associated net_device | |
410 | * | |
411 | * Returns: 0 if link is OK for use by FCoE. | |
412 | */ | |
413 | static int fcoe_if_destroy(struct net_device *netdev) | |
414 | { | |
415 | struct fc_lport *lp = NULL; | |
416 | struct fcoe_softc *fc; | |
7f349142 VD |
417 | |
418 | BUG_ON(!netdev); | |
419 | ||
420 | printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n", | |
421 | netdev->name); | |
422 | ||
423 | lp = fcoe_hostlist_lookup(netdev); | |
424 | if (!lp) | |
425 | return -ENODEV; | |
426 | ||
427 | fc = lport_priv(lp); | |
428 | ||
429 | /* Logout of the fabric */ | |
430 | fc_fabric_logoff(lp); | |
431 | ||
432 | /* Remove the instance from fcoe's list */ | |
433 | fcoe_hostlist_remove(lp); | |
434 | ||
ab6b85c1 VD |
435 | /* clean up netdev configurations */ |
436 | fcoe_netdev_cleanup(fc); | |
437 | ||
438 | /* tear-down the FCoE controller */ | |
97c8389d | 439 | fcoe_ctlr_destroy(&fc->ctlr); |
7f349142 VD |
440 | |
441 | /* Cleanup the fc_lport */ | |
442 | fc_lport_destroy(lp); | |
443 | fc_fcp_destroy(lp); | |
444 | ||
445 | /* Detach from the scsi-ml */ | |
446 | fc_remove_host(lp->host); | |
447 | scsi_remove_host(lp->host); | |
448 | ||
449 | /* There are no more rports or I/O, free the EM */ | |
450 | if (lp->emp) | |
451 | fc_exch_mgr_free(lp->emp); | |
452 | ||
7f349142 VD |
453 | /* Free the per-CPU revieve threads */ |
454 | fcoe_percpu_clean(lp); | |
455 | ||
456 | /* Free existing skbs */ | |
457 | fcoe_clean_pending_queue(lp); | |
458 | ||
459 | /* Free memory used by statistical counters */ | |
460 | fc_lport_free_stats(lp); | |
461 | ||
462 | /* Release the net_device and Scsi_Host */ | |
463 | dev_put(fc->real_dev); | |
464 | scsi_host_put(lp->host); | |
465 | ||
466 | return 0; | |
467 | } | |
468 | ||
469 | /* | |
470 | * fcoe_ddp_setup - calls LLD's ddp_setup through net_device | |
471 | * @lp: the corresponding fc_lport | |
472 | * @xid: the exchange id for this ddp transfer | |
473 | * @sgl: the scatterlist describing this transfer | |
474 | * @sgc: number of sg items | |
475 | * | |
476 | * Returns : 0 no ddp | |
477 | */ | |
478 | static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid, | |
479 | struct scatterlist *sgl, unsigned int sgc) | |
480 | { | |
481 | struct net_device *n = fcoe_netdev(lp); | |
482 | ||
483 | if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup) | |
484 | return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc); | |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
489 | /* | |
490 | * fcoe_ddp_done - calls LLD's ddp_done through net_device | |
491 | * @lp: the corresponding fc_lport | |
492 | * @xid: the exchange id for this ddp transfer | |
493 | * | |
494 | * Returns : the length of data that have been completed by ddp | |
495 | */ | |
496 | static int fcoe_ddp_done(struct fc_lport *lp, u16 xid) | |
497 | { | |
498 | struct net_device *n = fcoe_netdev(lp); | |
499 | ||
500 | if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done) | |
501 | return n->netdev_ops->ndo_fcoe_ddp_done(n, xid); | |
502 | return 0; | |
503 | } | |
504 | ||
505 | static struct libfc_function_template fcoe_libfc_fcn_templ = { | |
506 | .frame_send = fcoe_xmit, | |
507 | .ddp_setup = fcoe_ddp_setup, | |
508 | .ddp_done = fcoe_ddp_done, | |
509 | }; | |
510 | ||
511 | /** | |
512 | * fcoe_if_create() - this function creates the fcoe interface | |
513 | * @netdev: pointer the associated netdevice | |
514 | * | |
515 | * Creates fc_lport struct and scsi_host for lport, configures lport | |
516 | * and starts fabric login. | |
517 | * | |
518 | * Returns : 0 on success | |
519 | */ | |
520 | static int fcoe_if_create(struct net_device *netdev) | |
521 | { | |
522 | int rc; | |
523 | struct fc_lport *lp = NULL; | |
524 | struct fcoe_softc *fc; | |
525 | struct Scsi_Host *shost; | |
526 | ||
527 | BUG_ON(!netdev); | |
528 | ||
529 | printk(KERN_DEBUG "fcoe_if_create:interface on %s\n", | |
530 | netdev->name); | |
531 | ||
532 | lp = fcoe_hostlist_lookup(netdev); | |
533 | if (lp) | |
534 | return -EEXIST; | |
535 | ||
a0a25da2 VD |
536 | shost = libfc_host_alloc(&fcoe_shost_template, |
537 | sizeof(struct fcoe_softc)); | |
7f349142 VD |
538 | if (!shost) { |
539 | FC_DBG("Could not allocate host structure\n"); | |
540 | return -ENOMEM; | |
541 | } | |
542 | lp = shost_priv(shost); | |
543 | fc = lport_priv(lp); | |
544 | ||
545 | /* configure fc_lport, e.g., em */ | |
546 | rc = fcoe_lport_config(lp); | |
547 | if (rc) { | |
548 | FC_DBG("Could not configure lport\n"); | |
549 | goto out_host_put; | |
550 | } | |
551 | ||
97c8389d JE |
552 | /* |
553 | * Initialize FIP. | |
554 | */ | |
555 | fcoe_ctlr_init(&fc->ctlr); | |
556 | fc->ctlr.send = fcoe_fip_send; | |
557 | fc->ctlr.update_mac = fcoe_update_src_mac; | |
558 | ||
ab6b85c1 VD |
559 | /* configure lport network properties */ |
560 | rc = fcoe_netdev_config(lp, netdev); | |
561 | if (rc) { | |
562 | FC_DBG("Could not configure netdev for the interface\n"); | |
563 | goto out_netdev_cleanup; | |
564 | } | |
97c8389d | 565 | |
7f349142 VD |
566 | /* configure lport scsi host properties */ |
567 | rc = fcoe_shost_config(lp, shost, &netdev->dev); | |
568 | if (rc) { | |
569 | FC_DBG("Could not configure shost for lport\n"); | |
ab6b85c1 | 570 | goto out_netdev_cleanup; |
7f349142 VD |
571 | } |
572 | ||
573 | /* lport exch manager allocation */ | |
574 | rc = fcoe_em_config(lp); | |
575 | if (rc) { | |
576 | FC_DBG("Could not configure em for lport\n"); | |
ab6b85c1 | 577 | goto out_netdev_cleanup; |
7f349142 VD |
578 | } |
579 | ||
580 | /* Initialize the library */ | |
581 | rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ); | |
582 | if (rc) { | |
583 | FC_DBG("Could not configure libfc for lport!\n"); | |
584 | goto out_lp_destroy; | |
585 | } | |
586 | ||
587 | /* add to lports list */ | |
588 | fcoe_hostlist_add(lp); | |
589 | ||
590 | lp->boot_time = jiffies; | |
591 | ||
592 | fc_fabric_login(lp); | |
593 | ||
97c8389d JE |
594 | if (!fcoe_link_ok(lp)) |
595 | fcoe_ctlr_link_up(&fc->ctlr); | |
596 | ||
7f349142 VD |
597 | dev_hold(netdev); |
598 | ||
599 | return rc; | |
600 | ||
601 | out_lp_destroy: | |
602 | fc_exch_mgr_free(lp->emp); /* Free the EM */ | |
ab6b85c1 VD |
603 | out_netdev_cleanup: |
604 | fcoe_netdev_cleanup(fc); | |
7f349142 VD |
605 | out_host_put: |
606 | scsi_host_put(lp->host); | |
607 | return rc; | |
608 | } | |
609 | ||
610 | /** | |
611 | * fcoe_if_init() - attach to scsi transport | |
612 | * | |
613 | * Returns : 0 on success | |
614 | */ | |
615 | static int __init fcoe_if_init(void) | |
616 | { | |
617 | /* attach to scsi transport */ | |
618 | scsi_transport_fcoe_sw = | |
619 | fc_attach_transport(&fcoe_transport_function); | |
620 | ||
621 | if (!scsi_transport_fcoe_sw) { | |
622 | printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n"); | |
623 | return -ENODEV; | |
624 | } | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
629 | /** | |
630 | * fcoe_if_exit() - detach from scsi transport | |
631 | * | |
632 | * Returns : 0 on success | |
633 | */ | |
634 | int __exit fcoe_if_exit(void) | |
635 | { | |
636 | fc_release_transport(scsi_transport_fcoe_sw); | |
637 | return 0; | |
638 | } | |
639 | ||
8976f424 RL |
640 | /** |
641 | * fcoe_percpu_thread_create() - Create a receive thread for an online cpu | |
642 | * @cpu: cpu index for the online cpu | |
643 | */ | |
644 | static void fcoe_percpu_thread_create(unsigned int cpu) | |
645 | { | |
646 | struct fcoe_percpu_s *p; | |
647 | struct task_struct *thread; | |
648 | ||
649 | p = &per_cpu(fcoe_percpu, cpu); | |
650 | ||
651 | thread = kthread_create(fcoe_percpu_receive_thread, | |
652 | (void *)p, "fcoethread/%d", cpu); | |
653 | ||
654 | if (likely(!IS_ERR(p->thread))) { | |
655 | kthread_bind(thread, cpu); | |
656 | wake_up_process(thread); | |
657 | ||
658 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
659 | p->thread = thread; | |
660 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
661 | } | |
662 | } | |
663 | ||
664 | /** | |
665 | * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu | |
666 | * @cpu: cpu index the rx thread is to be removed | |
667 | * | |
668 | * Destroys a per-CPU Rx thread. Any pending skbs are moved to the | |
669 | * current CPU's Rx thread. If the thread being destroyed is bound to | |
670 | * the CPU processing this context the skbs will be freed. | |
671 | */ | |
672 | static void fcoe_percpu_thread_destroy(unsigned int cpu) | |
673 | { | |
674 | struct fcoe_percpu_s *p; | |
675 | struct task_struct *thread; | |
676 | struct page *crc_eof; | |
677 | struct sk_buff *skb; | |
678 | #ifdef CONFIG_SMP | |
679 | struct fcoe_percpu_s *p0; | |
680 | unsigned targ_cpu = smp_processor_id(); | |
681 | #endif /* CONFIG_SMP */ | |
682 | ||
683 | printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu); | |
684 | ||
685 | /* Prevent any new skbs from being queued for this CPU. */ | |
686 | p = &per_cpu(fcoe_percpu, cpu); | |
687 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
688 | thread = p->thread; | |
689 | p->thread = NULL; | |
690 | crc_eof = p->crc_eof_page; | |
691 | p->crc_eof_page = NULL; | |
692 | p->crc_eof_offset = 0; | |
693 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
694 | ||
695 | #ifdef CONFIG_SMP | |
696 | /* | |
697 | * Don't bother moving the skb's if this context is running | |
698 | * on the same CPU that is having its thread destroyed. This | |
699 | * can easily happen when the module is removed. | |
700 | */ | |
701 | if (cpu != targ_cpu) { | |
702 | p0 = &per_cpu(fcoe_percpu, targ_cpu); | |
703 | spin_lock_bh(&p0->fcoe_rx_list.lock); | |
704 | if (p0->thread) { | |
705 | FC_DBG("Moving frames from CPU %d to CPU %d\n", | |
706 | cpu, targ_cpu); | |
707 | ||
708 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
709 | __skb_queue_tail(&p0->fcoe_rx_list, skb); | |
710 | spin_unlock_bh(&p0->fcoe_rx_list.lock); | |
711 | } else { | |
712 | /* | |
713 | * The targeted CPU is not initialized and cannot accept | |
714 | * new skbs. Unlock the targeted CPU and drop the skbs | |
715 | * on the CPU that is going offline. | |
716 | */ | |
717 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
718 | kfree_skb(skb); | |
719 | spin_unlock_bh(&p0->fcoe_rx_list.lock); | |
720 | } | |
721 | } else { | |
722 | /* | |
723 | * This scenario occurs when the module is being removed | |
724 | * and all threads are being destroyed. skbs will continue | |
725 | * to be shifted from the CPU thread that is being removed | |
726 | * to the CPU thread associated with the CPU that is processing | |
727 | * the module removal. Once there is only one CPU Rx thread it | |
728 | * will reach this case and we will drop all skbs and later | |
729 | * stop the thread. | |
730 | */ | |
731 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
732 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
733 | kfree_skb(skb); | |
734 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
735 | } | |
736 | #else | |
737 | /* | |
738 | * This a non-SMP scenario where the singluar Rx thread is | |
739 | * being removed. Free all skbs and stop the thread. | |
740 | */ | |
741 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
742 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
743 | kfree_skb(skb); | |
744 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
745 | #endif | |
746 | ||
747 | if (thread) | |
748 | kthread_stop(thread); | |
749 | ||
750 | if (crc_eof) | |
751 | put_page(crc_eof); | |
752 | } | |
753 | ||
754 | /** | |
755 | * fcoe_cpu_callback() - fcoe cpu hotplug event callback | |
756 | * @nfb: callback data block | |
757 | * @action: event triggering the callback | |
758 | * @hcpu: index for the cpu of this event | |
759 | * | |
760 | * This creates or destroys per cpu data for fcoe | |
761 | * | |
762 | * Returns NOTIFY_OK always. | |
763 | */ | |
764 | static int fcoe_cpu_callback(struct notifier_block *nfb, | |
765 | unsigned long action, void *hcpu) | |
766 | { | |
767 | unsigned cpu = (unsigned long)hcpu; | |
768 | ||
769 | switch (action) { | |
770 | case CPU_ONLINE: | |
771 | case CPU_ONLINE_FROZEN: | |
772 | FC_DBG("CPU %x online: Create Rx thread\n", cpu); | |
773 | fcoe_percpu_thread_create(cpu); | |
774 | break; | |
775 | case CPU_DEAD: | |
776 | case CPU_DEAD_FROZEN: | |
777 | FC_DBG("CPU %x offline: Remove Rx thread\n", cpu); | |
778 | fcoe_percpu_thread_destroy(cpu); | |
779 | break; | |
780 | default: | |
781 | break; | |
782 | } | |
783 | return NOTIFY_OK; | |
784 | } | |
785 | ||
786 | static struct notifier_block fcoe_cpu_notifier = { | |
787 | .notifier_call = fcoe_cpu_callback, | |
788 | }; | |
789 | ||
85b4aa49 | 790 | /** |
34f42a07 | 791 | * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ |
85b4aa49 RL |
792 | * @skb: the receive skb |
793 | * @dev: associated net device | |
794 | * @ptype: context | |
795 | * @odldev: last device | |
796 | * | |
797 | * this function will receive the packet and build fc frame and pass it up | |
798 | * | |
799 | * Returns: 0 for success | |
34f42a07 | 800 | */ |
85b4aa49 RL |
801 | int fcoe_rcv(struct sk_buff *skb, struct net_device *dev, |
802 | struct packet_type *ptype, struct net_device *olddev) | |
803 | { | |
804 | struct fc_lport *lp; | |
805 | struct fcoe_rcv_info *fr; | |
806 | struct fcoe_softc *fc; | |
85b4aa49 | 807 | struct fc_frame_header *fh; |
85b4aa49 | 808 | struct fcoe_percpu_s *fps; |
38eccabd | 809 | unsigned short oxid; |
8976f424 | 810 | unsigned int cpu = 0; |
85b4aa49 RL |
811 | |
812 | fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type); | |
97c8389d | 813 | lp = fc->ctlr.lp; |
85b4aa49 RL |
814 | if (unlikely(lp == NULL)) { |
815 | FC_DBG("cannot find hba structure"); | |
816 | goto err2; | |
817 | } | |
97c8389d JE |
818 | if (!lp->link_up) |
819 | goto err2; | |
85b4aa49 RL |
820 | |
821 | if (unlikely(debug_fcoe)) { | |
822 | FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p " | |
823 | "end:%p sum:%d dev:%s", skb->len, skb->data_len, | |
824 | skb->head, skb->data, skb_tail_pointer(skb), | |
825 | skb_end_pointer(skb), skb->csum, | |
826 | skb->dev ? skb->dev->name : "<NULL>"); | |
827 | ||
828 | } | |
829 | ||
830 | /* check for FCOE packet type */ | |
831 | if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) { | |
832 | FC_DBG("wrong FC type frame"); | |
833 | goto err; | |
834 | } | |
835 | ||
836 | /* | |
837 | * Check for minimum frame length, and make sure required FCoE | |
838 | * and FC headers are pulled into the linear data area. | |
839 | */ | |
840 | if (unlikely((skb->len < FCOE_MIN_FRAME) || | |
841 | !pskb_may_pull(skb, FCOE_HEADER_LEN))) | |
842 | goto err; | |
843 | ||
844 | skb_set_transport_header(skb, sizeof(struct fcoe_hdr)); | |
845 | fh = (struct fc_frame_header *) skb_transport_header(skb); | |
846 | ||
847 | oxid = ntohs(fh->fh_ox_id); | |
848 | ||
849 | fr = fcoe_dev_from_skb(skb); | |
850 | fr->fr_dev = lp; | |
851 | fr->ptype = ptype; | |
5e5e92df | 852 | |
85b4aa49 RL |
853 | #ifdef CONFIG_SMP |
854 | /* | |
855 | * The incoming frame exchange id(oxid) is ANDed with num of online | |
8976f424 RL |
856 | * cpu bits to get cpu and then this cpu is used for selecting |
857 | * a per cpu kernel thread from fcoe_percpu. | |
85b4aa49 | 858 | */ |
8976f424 | 859 | cpu = oxid & (num_online_cpus() - 1); |
85b4aa49 | 860 | #endif |
5e5e92df | 861 | |
8976f424 | 862 | fps = &per_cpu(fcoe_percpu, cpu); |
85b4aa49 | 863 | spin_lock_bh(&fps->fcoe_rx_list.lock); |
8976f424 RL |
864 | if (unlikely(!fps->thread)) { |
865 | /* | |
866 | * The targeted CPU is not ready, let's target | |
867 | * the first CPU now. For non-SMP systems this | |
868 | * will check the same CPU twice. | |
869 | */ | |
870 | FC_DBG("CPU is online, but no receive thread ready " | |
871 | "for incoming skb- using first online CPU.\n"); | |
872 | ||
873 | spin_unlock_bh(&fps->fcoe_rx_list.lock); | |
874 | cpu = first_cpu(cpu_online_map); | |
875 | fps = &per_cpu(fcoe_percpu, cpu); | |
876 | spin_lock_bh(&fps->fcoe_rx_list.lock); | |
877 | if (!fps->thread) { | |
878 | spin_unlock_bh(&fps->fcoe_rx_list.lock); | |
879 | goto err; | |
880 | } | |
881 | } | |
882 | ||
883 | /* | |
884 | * We now have a valid CPU that we're targeting for | |
885 | * this skb. We also have this receive thread locked, | |
886 | * so we're free to queue skbs into it's queue. | |
887 | */ | |
85b4aa49 RL |
888 | __skb_queue_tail(&fps->fcoe_rx_list, skb); |
889 | if (fps->fcoe_rx_list.qlen == 1) | |
890 | wake_up_process(fps->thread); | |
891 | ||
892 | spin_unlock_bh(&fps->fcoe_rx_list.lock); | |
893 | ||
894 | return 0; | |
895 | err: | |
582b45bc | 896 | fc_lport_get_stats(lp)->ErrorFrames++; |
85b4aa49 RL |
897 | |
898 | err2: | |
899 | kfree_skb(skb); | |
900 | return -1; | |
901 | } | |
902 | EXPORT_SYMBOL_GPL(fcoe_rcv); | |
903 | ||
904 | /** | |
34f42a07 | 905 | * fcoe_start_io() - pass to netdev to start xmit for fcoe |
85b4aa49 RL |
906 | * @skb: the skb to be xmitted |
907 | * | |
908 | * Returns: 0 for success | |
34f42a07 | 909 | */ |
85b4aa49 RL |
910 | static inline int fcoe_start_io(struct sk_buff *skb) |
911 | { | |
912 | int rc; | |
913 | ||
914 | skb_get(skb); | |
915 | rc = dev_queue_xmit(skb); | |
916 | if (rc != 0) | |
917 | return rc; | |
918 | kfree_skb(skb); | |
919 | return 0; | |
920 | } | |
921 | ||
922 | /** | |
34f42a07 | 923 | * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof |
85b4aa49 RL |
924 | * @skb: the skb to be xmitted |
925 | * @tlen: total len | |
926 | * | |
927 | * Returns: 0 for success | |
34f42a07 | 928 | */ |
85b4aa49 RL |
929 | static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen) |
930 | { | |
931 | struct fcoe_percpu_s *fps; | |
932 | struct page *page; | |
85b4aa49 | 933 | |
5e5e92df | 934 | fps = &get_cpu_var(fcoe_percpu); |
85b4aa49 RL |
935 | page = fps->crc_eof_page; |
936 | if (!page) { | |
937 | page = alloc_page(GFP_ATOMIC); | |
938 | if (!page) { | |
5e5e92df | 939 | put_cpu_var(fcoe_percpu); |
85b4aa49 RL |
940 | return -ENOMEM; |
941 | } | |
942 | fps->crc_eof_page = page; | |
8976f424 | 943 | fps->crc_eof_offset = 0; |
85b4aa49 RL |
944 | } |
945 | ||
946 | get_page(page); | |
947 | skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, | |
948 | fps->crc_eof_offset, tlen); | |
949 | skb->len += tlen; | |
950 | skb->data_len += tlen; | |
951 | skb->truesize += tlen; | |
952 | fps->crc_eof_offset += sizeof(struct fcoe_crc_eof); | |
953 | ||
954 | if (fps->crc_eof_offset >= PAGE_SIZE) { | |
955 | fps->crc_eof_page = NULL; | |
956 | fps->crc_eof_offset = 0; | |
957 | put_page(page); | |
958 | } | |
5e5e92df | 959 | put_cpu_var(fcoe_percpu); |
85b4aa49 RL |
960 | return 0; |
961 | } | |
962 | ||
963 | /** | |
34f42a07 | 964 | * fcoe_fc_crc() - calculates FC CRC in this fcoe skb |
85b4aa49 RL |
965 | * @fp: the fc_frame containg data to be checksummed |
966 | * | |
967 | * This uses crc32() to calculate the crc for fc frame | |
968 | * Return : 32 bit crc | |
34f42a07 | 969 | */ |
85b4aa49 RL |
970 | u32 fcoe_fc_crc(struct fc_frame *fp) |
971 | { | |
972 | struct sk_buff *skb = fp_skb(fp); | |
973 | struct skb_frag_struct *frag; | |
974 | unsigned char *data; | |
975 | unsigned long off, len, clen; | |
976 | u32 crc; | |
977 | unsigned i; | |
978 | ||
979 | crc = crc32(~0, skb->data, skb_headlen(skb)); | |
980 | ||
981 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
982 | frag = &skb_shinfo(skb)->frags[i]; | |
983 | off = frag->page_offset; | |
984 | len = frag->size; | |
985 | while (len > 0) { | |
986 | clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK)); | |
987 | data = kmap_atomic(frag->page + (off >> PAGE_SHIFT), | |
988 | KM_SKB_DATA_SOFTIRQ); | |
989 | crc = crc32(crc, data + (off & ~PAGE_MASK), clen); | |
990 | kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ); | |
991 | off += clen; | |
992 | len -= clen; | |
993 | } | |
994 | } | |
995 | return crc; | |
996 | } | |
85b4aa49 RL |
997 | |
998 | /** | |
34f42a07 | 999 | * fcoe_xmit() - FCoE frame transmit function |
85b4aa49 RL |
1000 | * @lp: the associated local port |
1001 | * @fp: the fc_frame to be transmitted | |
1002 | * | |
1003 | * Return : 0 for success | |
34f42a07 | 1004 | */ |
85b4aa49 RL |
1005 | int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp) |
1006 | { | |
1007 | int wlen, rc = 0; | |
1008 | u32 crc; | |
1009 | struct ethhdr *eh; | |
1010 | struct fcoe_crc_eof *cp; | |
1011 | struct sk_buff *skb; | |
1012 | struct fcoe_dev_stats *stats; | |
1013 | struct fc_frame_header *fh; | |
1014 | unsigned int hlen; /* header length implies the version */ | |
1015 | unsigned int tlen; /* trailer length */ | |
1016 | unsigned int elen; /* eth header, may include vlan */ | |
85b4aa49 RL |
1017 | struct fcoe_softc *fc; |
1018 | u8 sof, eof; | |
1019 | struct fcoe_hdr *hp; | |
1020 | ||
1021 | WARN_ON((fr_len(fp) % sizeof(u32)) != 0); | |
1022 | ||
fc47ff6b | 1023 | fc = lport_priv(lp); |
85b4aa49 | 1024 | fh = fc_frame_header_get(fp); |
97c8389d JE |
1025 | skb = fp_skb(fp); |
1026 | wlen = skb->len / FCOE_WORD_TO_BYTE; | |
1027 | ||
1028 | if (!lp->link_up) { | |
1029 | kfree(skb); | |
1030 | return 0; | |
85b4aa49 RL |
1031 | } |
1032 | ||
97c8389d JE |
1033 | if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) && |
1034 | fcoe_ctlr_els_send(&fc->ctlr, skb)) | |
1035 | return 0; | |
1036 | ||
85b4aa49 RL |
1037 | sof = fr_sof(fp); |
1038 | eof = fr_eof(fp); | |
1039 | ||
1040 | elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ? | |
1041 | sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr); | |
1042 | hlen = sizeof(struct fcoe_hdr); | |
1043 | tlen = sizeof(struct fcoe_crc_eof); | |
1044 | wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE; | |
1045 | ||
1046 | /* crc offload */ | |
1047 | if (likely(lp->crc_offload)) { | |
39ca9a06 | 1048 | skb->ip_summed = CHECKSUM_PARTIAL; |
85b4aa49 RL |
1049 | skb->csum_start = skb_headroom(skb); |
1050 | skb->csum_offset = skb->len; | |
1051 | crc = 0; | |
1052 | } else { | |
1053 | skb->ip_summed = CHECKSUM_NONE; | |
1054 | crc = fcoe_fc_crc(fp); | |
1055 | } | |
1056 | ||
1057 | /* copy fc crc and eof to the skb buff */ | |
1058 | if (skb_is_nonlinear(skb)) { | |
1059 | skb_frag_t *frag; | |
1060 | if (fcoe_get_paged_crc_eof(skb, tlen)) { | |
e9041581 | 1061 | kfree_skb(skb); |
85b4aa49 RL |
1062 | return -ENOMEM; |
1063 | } | |
1064 | frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1]; | |
1065 | cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ) | |
1066 | + frag->page_offset; | |
1067 | } else { | |
1068 | cp = (struct fcoe_crc_eof *)skb_put(skb, tlen); | |
1069 | } | |
1070 | ||
1071 | memset(cp, 0, sizeof(*cp)); | |
1072 | cp->fcoe_eof = eof; | |
1073 | cp->fcoe_crc32 = cpu_to_le32(~crc); | |
1074 | ||
1075 | if (skb_is_nonlinear(skb)) { | |
1076 | kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ); | |
1077 | cp = NULL; | |
1078 | } | |
1079 | ||
1080 | /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */ | |
1081 | skb_push(skb, elen + hlen); | |
1082 | skb_reset_mac_header(skb); | |
1083 | skb_reset_network_header(skb); | |
1084 | skb->mac_len = elen; | |
211c738d | 1085 | skb->protocol = htons(ETH_P_FCOE); |
85b4aa49 RL |
1086 | skb->dev = fc->real_dev; |
1087 | ||
1088 | /* fill up mac and fcoe headers */ | |
1089 | eh = eth_hdr(skb); | |
1090 | eh->h_proto = htons(ETH_P_FCOE); | |
97c8389d | 1091 | if (fc->ctlr.map_dest) |
85b4aa49 RL |
1092 | fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id); |
1093 | else | |
1094 | /* insert GW address */ | |
97c8389d | 1095 | memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN); |
85b4aa49 | 1096 | |
97c8389d JE |
1097 | if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN)) |
1098 | memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN); | |
85b4aa49 | 1099 | else |
97c8389d | 1100 | memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN); |
85b4aa49 RL |
1101 | |
1102 | hp = (struct fcoe_hdr *)(eh + 1); | |
1103 | memset(hp, 0, sizeof(*hp)); | |
1104 | if (FC_FCOE_VER) | |
1105 | FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER); | |
1106 | hp->fcoe_sof = sof; | |
1107 | ||
39ca9a06 YZ |
1108 | #ifdef NETIF_F_FSO |
1109 | /* fcoe lso, mss is in max_payload which is non-zero for FCP data */ | |
1110 | if (lp->seq_offload && fr_max_payload(fp)) { | |
1111 | skb_shinfo(skb)->gso_type = SKB_GSO_FCOE; | |
1112 | skb_shinfo(skb)->gso_size = fr_max_payload(fp); | |
1113 | } else { | |
1114 | skb_shinfo(skb)->gso_type = 0; | |
1115 | skb_shinfo(skb)->gso_size = 0; | |
1116 | } | |
1117 | #endif | |
85b4aa49 | 1118 | /* update tx stats: regardless if LLD fails */ |
582b45bc RL |
1119 | stats = fc_lport_get_stats(lp); |
1120 | stats->TxFrames++; | |
1121 | stats->TxWords += wlen; | |
85b4aa49 RL |
1122 | |
1123 | /* send down to lld */ | |
1124 | fr_dev(fp) = lp; | |
1125 | if (fc->fcoe_pending_queue.qlen) | |
1126 | rc = fcoe_check_wait_queue(lp); | |
1127 | ||
1128 | if (rc == 0) | |
1129 | rc = fcoe_start_io(skb); | |
1130 | ||
1131 | if (rc) { | |
55c8bafb CL |
1132 | spin_lock_bh(&fc->fcoe_pending_queue.lock); |
1133 | __skb_queue_tail(&fc->fcoe_pending_queue, skb); | |
1134 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
85b4aa49 | 1135 | if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH) |
bc0e17f6 | 1136 | lp->qfull = 1; |
85b4aa49 RL |
1137 | } |
1138 | ||
1139 | return 0; | |
1140 | } | |
1141 | EXPORT_SYMBOL_GPL(fcoe_xmit); | |
1142 | ||
34f42a07 RL |
1143 | /** |
1144 | * fcoe_percpu_receive_thread() - recv thread per cpu | |
85b4aa49 RL |
1145 | * @arg: ptr to the fcoe per cpu struct |
1146 | * | |
1147 | * Return: 0 for success | |
85b4aa49 RL |
1148 | */ |
1149 | int fcoe_percpu_receive_thread(void *arg) | |
1150 | { | |
1151 | struct fcoe_percpu_s *p = arg; | |
1152 | u32 fr_len; | |
1153 | struct fc_lport *lp; | |
1154 | struct fcoe_rcv_info *fr; | |
1155 | struct fcoe_dev_stats *stats; | |
1156 | struct fc_frame_header *fh; | |
1157 | struct sk_buff *skb; | |
1158 | struct fcoe_crc_eof crc_eof; | |
1159 | struct fc_frame *fp; | |
1160 | u8 *mac = NULL; | |
1161 | struct fcoe_softc *fc; | |
1162 | struct fcoe_hdr *hp; | |
1163 | ||
4469c195 | 1164 | set_user_nice(current, -20); |
85b4aa49 RL |
1165 | |
1166 | while (!kthread_should_stop()) { | |
1167 | ||
1168 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
1169 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) { | |
1170 | set_current_state(TASK_INTERRUPTIBLE); | |
1171 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
1172 | schedule(); | |
1173 | set_current_state(TASK_RUNNING); | |
1174 | if (kthread_should_stop()) | |
1175 | return 0; | |
1176 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
1177 | } | |
1178 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
1179 | fr = fcoe_dev_from_skb(skb); | |
1180 | lp = fr->fr_dev; | |
1181 | if (unlikely(lp == NULL)) { | |
1182 | FC_DBG("invalid HBA Structure"); | |
1183 | kfree_skb(skb); | |
1184 | continue; | |
1185 | } | |
1186 | ||
85b4aa49 RL |
1187 | if (unlikely(debug_fcoe)) { |
1188 | FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p " | |
1189 | "tail:%p end:%p sum:%d dev:%s", | |
1190 | skb->len, skb->data_len, | |
1191 | skb->head, skb->data, skb_tail_pointer(skb), | |
1192 | skb_end_pointer(skb), skb->csum, | |
1193 | skb->dev ? skb->dev->name : "<NULL>"); | |
1194 | } | |
1195 | ||
1196 | /* | |
1197 | * Save source MAC address before discarding header. | |
1198 | */ | |
1199 | fc = lport_priv(lp); | |
85b4aa49 RL |
1200 | if (skb_is_nonlinear(skb)) |
1201 | skb_linearize(skb); /* not ideal */ | |
97c8389d | 1202 | mac = eth_hdr(skb)->h_source; |
85b4aa49 RL |
1203 | |
1204 | /* | |
1205 | * Frame length checks and setting up the header pointers | |
1206 | * was done in fcoe_rcv already. | |
1207 | */ | |
1208 | hp = (struct fcoe_hdr *) skb_network_header(skb); | |
1209 | fh = (struct fc_frame_header *) skb_transport_header(skb); | |
1210 | ||
582b45bc | 1211 | stats = fc_lport_get_stats(lp); |
85b4aa49 | 1212 | if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) { |
582b45bc RL |
1213 | if (stats->ErrorFrames < 5) |
1214 | printk(KERN_WARNING "FCoE version " | |
1215 | "mismatch: The frame has " | |
1216 | "version %x, but the " | |
1217 | "initiator supports version " | |
1218 | "%x\n", FC_FCOE_DECAPS_VER(hp), | |
1219 | FC_FCOE_VER); | |
1220 | stats->ErrorFrames++; | |
85b4aa49 RL |
1221 | kfree_skb(skb); |
1222 | continue; | |
1223 | } | |
1224 | ||
1225 | skb_pull(skb, sizeof(struct fcoe_hdr)); | |
1226 | fr_len = skb->len - sizeof(struct fcoe_crc_eof); | |
1227 | ||
582b45bc RL |
1228 | stats->RxFrames++; |
1229 | stats->RxWords += fr_len / FCOE_WORD_TO_BYTE; | |
85b4aa49 RL |
1230 | |
1231 | fp = (struct fc_frame *)skb; | |
1232 | fc_frame_init(fp); | |
1233 | fr_dev(fp) = lp; | |
1234 | fr_sof(fp) = hp->fcoe_sof; | |
1235 | ||
1236 | /* Copy out the CRC and EOF trailer for access */ | |
1237 | if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) { | |
1238 | kfree_skb(skb); | |
1239 | continue; | |
1240 | } | |
1241 | fr_eof(fp) = crc_eof.fcoe_eof; | |
1242 | fr_crc(fp) = crc_eof.fcoe_crc32; | |
1243 | if (pskb_trim(skb, fr_len)) { | |
1244 | kfree_skb(skb); | |
1245 | continue; | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * We only check CRC if no offload is available and if it is | |
1250 | * it's solicited data, in which case, the FCP layer would | |
1251 | * check it during the copy. | |
1252 | */ | |
07c00ec4 | 1253 | if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY) |
85b4aa49 RL |
1254 | fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED; |
1255 | else | |
1256 | fr_flags(fp) |= FCPHF_CRC_UNCHECKED; | |
1257 | ||
1258 | fh = fc_frame_header_get(fp); | |
1259 | if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && | |
1260 | fh->fh_type == FC_TYPE_FCP) { | |
1261 | fc_exch_recv(lp, lp->emp, fp); | |
1262 | continue; | |
1263 | } | |
1264 | if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) { | |
1265 | if (le32_to_cpu(fr_crc(fp)) != | |
1266 | ~crc32(~0, skb->data, fr_len)) { | |
1267 | if (debug_fcoe || stats->InvalidCRCCount < 5) | |
1268 | printk(KERN_WARNING "fcoe: dropping " | |
1269 | "frame with CRC error\n"); | |
1270 | stats->InvalidCRCCount++; | |
1271 | stats->ErrorFrames++; | |
1272 | fc_frame_free(fp); | |
1273 | continue; | |
1274 | } | |
1275 | fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED; | |
1276 | } | |
97c8389d JE |
1277 | if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) && |
1278 | fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) { | |
1279 | fc_frame_free(fp); | |
1280 | continue; | |
1281 | } | |
85b4aa49 RL |
1282 | fc_exch_recv(lp, lp->emp, fp); |
1283 | } | |
1284 | return 0; | |
1285 | } | |
1286 | ||
85b4aa49 | 1287 | /** |
34f42a07 | 1288 | * fcoe_watchdog() - fcoe timer callback |
85b4aa49 RL |
1289 | * @vp: |
1290 | * | |
bc0e17f6 | 1291 | * This checks the pending queue length for fcoe and set lport qfull |
85b4aa49 RL |
1292 | * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the |
1293 | * fcoe_hostlist. | |
1294 | * | |
1295 | * Returns: 0 for success | |
34f42a07 | 1296 | */ |
85b4aa49 RL |
1297 | void fcoe_watchdog(ulong vp) |
1298 | { | |
85b4aa49 | 1299 | struct fcoe_softc *fc; |
85b4aa49 RL |
1300 | |
1301 | read_lock(&fcoe_hostlist_lock); | |
1302 | list_for_each_entry(fc, &fcoe_hostlist, list) { | |
97c8389d JE |
1303 | if (fc->ctlr.lp) |
1304 | fcoe_check_wait_queue(fc->ctlr.lp); | |
85b4aa49 RL |
1305 | } |
1306 | read_unlock(&fcoe_hostlist_lock); | |
1307 | ||
1308 | fcoe_timer.expires = jiffies + (1 * HZ); | |
1309 | add_timer(&fcoe_timer); | |
1310 | } | |
1311 | ||
1312 | ||
1313 | /** | |
34f42a07 | 1314 | * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue |
85b4aa49 RL |
1315 | * @lp: the fc_port for this skb |
1316 | * @skb: the associated skb to be xmitted | |
1317 | * | |
1318 | * This empties the wait_queue, dequeue the head of the wait_queue queue | |
1319 | * and calls fcoe_start_io() for each packet, if all skb have been | |
c826a314 VD |
1320 | * transmitted, return qlen or -1 if a error occurs, then restore |
1321 | * wait_queue and try again later. | |
85b4aa49 RL |
1322 | * |
1323 | * The wait_queue is used when the skb transmit fails. skb will go | |
1324 | * in the wait_queue which will be emptied by the time function OR | |
1325 | * by the next skb transmit. | |
1326 | * | |
1327 | * Returns: 0 for success | |
34f42a07 | 1328 | */ |
85b4aa49 RL |
1329 | static int fcoe_check_wait_queue(struct fc_lport *lp) |
1330 | { | |
55c8bafb | 1331 | struct fcoe_softc *fc = lport_priv(lp); |
85b4aa49 | 1332 | struct sk_buff *skb; |
c826a314 | 1333 | int rc = -1; |
85b4aa49 | 1334 | |
85b4aa49 | 1335 | spin_lock_bh(&fc->fcoe_pending_queue.lock); |
c826a314 VD |
1336 | if (fc->fcoe_pending_queue_active) |
1337 | goto out; | |
1338 | fc->fcoe_pending_queue_active = 1; | |
55c8bafb CL |
1339 | |
1340 | while (fc->fcoe_pending_queue.qlen) { | |
1341 | /* keep qlen > 0 until fcoe_start_io succeeds */ | |
1342 | fc->fcoe_pending_queue.qlen++; | |
1343 | skb = __skb_dequeue(&fc->fcoe_pending_queue); | |
1344 | ||
1345 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
1346 | rc = fcoe_start_io(skb); | |
1347 | spin_lock_bh(&fc->fcoe_pending_queue.lock); | |
1348 | ||
1349 | if (rc) { | |
1350 | __skb_queue_head(&fc->fcoe_pending_queue, skb); | |
1351 | /* undo temporary increment above */ | |
1352 | fc->fcoe_pending_queue.qlen--; | |
1353 | break; | |
85b4aa49 | 1354 | } |
55c8bafb CL |
1355 | /* undo temporary increment above */ |
1356 | fc->fcoe_pending_queue.qlen--; | |
85b4aa49 | 1357 | } |
55c8bafb CL |
1358 | |
1359 | if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH) | |
1360 | lp->qfull = 0; | |
c826a314 VD |
1361 | fc->fcoe_pending_queue_active = 0; |
1362 | rc = fc->fcoe_pending_queue.qlen; | |
1363 | out: | |
85b4aa49 | 1364 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); |
c826a314 | 1365 | return rc; |
85b4aa49 RL |
1366 | } |
1367 | ||
85b4aa49 | 1368 | /** |
34f42a07 RL |
1369 | * fcoe_dev_setup() - setup link change notification interface |
1370 | */ | |
1371 | static void fcoe_dev_setup() | |
85b4aa49 RL |
1372 | { |
1373 | /* | |
1374 | * here setup a interface specific wd time to | |
1375 | * monitor the link state | |
1376 | */ | |
1377 | register_netdevice_notifier(&fcoe_notifier); | |
1378 | } | |
1379 | ||
1380 | /** | |
34f42a07 RL |
1381 | * fcoe_dev_setup() - cleanup link change notification interface |
1382 | */ | |
85b4aa49 RL |
1383 | static void fcoe_dev_cleanup(void) |
1384 | { | |
1385 | unregister_netdevice_notifier(&fcoe_notifier); | |
1386 | } | |
1387 | ||
1388 | /** | |
34f42a07 | 1389 | * fcoe_device_notification() - netdev event notification callback |
85b4aa49 RL |
1390 | * @notifier: context of the notification |
1391 | * @event: type of event | |
1392 | * @ptr: fixed array for output parsed ifname | |
1393 | * | |
1394 | * This function is called by the ethernet driver in case of link change event | |
1395 | * | |
1396 | * Returns: 0 for success | |
34f42a07 | 1397 | */ |
85b4aa49 RL |
1398 | static int fcoe_device_notification(struct notifier_block *notifier, |
1399 | ulong event, void *ptr) | |
1400 | { | |
1401 | struct fc_lport *lp = NULL; | |
1402 | struct net_device *real_dev = ptr; | |
1403 | struct fcoe_softc *fc; | |
1404 | struct fcoe_dev_stats *stats; | |
97c8389d | 1405 | u32 link_possible = 1; |
85b4aa49 RL |
1406 | u32 mfs; |
1407 | int rc = NOTIFY_OK; | |
1408 | ||
1409 | read_lock(&fcoe_hostlist_lock); | |
1410 | list_for_each_entry(fc, &fcoe_hostlist, list) { | |
1411 | if (fc->real_dev == real_dev) { | |
97c8389d | 1412 | lp = fc->ctlr.lp; |
85b4aa49 RL |
1413 | break; |
1414 | } | |
1415 | } | |
1416 | read_unlock(&fcoe_hostlist_lock); | |
1417 | if (lp == NULL) { | |
1418 | rc = NOTIFY_DONE; | |
1419 | goto out; | |
1420 | } | |
1421 | ||
85b4aa49 RL |
1422 | switch (event) { |
1423 | case NETDEV_DOWN: | |
1424 | case NETDEV_GOING_DOWN: | |
97c8389d | 1425 | link_possible = 0; |
85b4aa49 RL |
1426 | break; |
1427 | case NETDEV_UP: | |
1428 | case NETDEV_CHANGE: | |
85b4aa49 RL |
1429 | break; |
1430 | case NETDEV_CHANGEMTU: | |
1431 | mfs = fc->real_dev->mtu - | |
1432 | (sizeof(struct fcoe_hdr) + | |
1433 | sizeof(struct fcoe_crc_eof)); | |
1434 | if (mfs >= FC_MIN_MAX_FRAME) | |
1435 | fc_set_mfs(lp, mfs); | |
85b4aa49 RL |
1436 | break; |
1437 | case NETDEV_REGISTER: | |
1438 | break; | |
1439 | default: | |
97c8389d | 1440 | FC_DBG("Unknown event %ld from netdev netlink\n", event); |
85b4aa49 | 1441 | } |
97c8389d JE |
1442 | if (link_possible && !fcoe_link_ok(lp)) |
1443 | fcoe_ctlr_link_up(&fc->ctlr); | |
1444 | else if (fcoe_ctlr_link_down(&fc->ctlr)) { | |
1445 | stats = fc_lport_get_stats(lp); | |
1446 | stats->LinkFailureCount++; | |
1447 | fcoe_clean_pending_queue(lp); | |
85b4aa49 RL |
1448 | } |
1449 | out: | |
1450 | return rc; | |
1451 | } | |
1452 | ||
1453 | /** | |
34f42a07 | 1454 | * fcoe_if_to_netdev() - parse a name buffer to get netdev |
85b4aa49 RL |
1455 | * @ifname: fixed array for output parsed ifname |
1456 | * @buffer: incoming buffer to be copied | |
1457 | * | |
1458 | * Returns: NULL or ptr to netdeive | |
34f42a07 | 1459 | */ |
85b4aa49 RL |
1460 | static struct net_device *fcoe_if_to_netdev(const char *buffer) |
1461 | { | |
1462 | char *cp; | |
1463 | char ifname[IFNAMSIZ + 2]; | |
1464 | ||
1465 | if (buffer) { | |
1466 | strlcpy(ifname, buffer, IFNAMSIZ); | |
1467 | cp = ifname + strlen(ifname); | |
1468 | while (--cp >= ifname && *cp == '\n') | |
1469 | *cp = '\0'; | |
1470 | return dev_get_by_name(&init_net, ifname); | |
1471 | } | |
1472 | return NULL; | |
1473 | } | |
1474 | ||
1475 | /** | |
34f42a07 | 1476 | * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev |
85b4aa49 RL |
1477 | * @netdev: the target netdev |
1478 | * | |
1479 | * Returns: ptr to the struct module, NULL for failure | |
34f42a07 | 1480 | */ |
b2ab99c9 RL |
1481 | static struct module * |
1482 | fcoe_netdev_to_module_owner(const struct net_device *netdev) | |
85b4aa49 RL |
1483 | { |
1484 | struct device *dev; | |
1485 | ||
1486 | if (!netdev) | |
1487 | return NULL; | |
1488 | ||
1489 | dev = netdev->dev.parent; | |
1490 | if (!dev) | |
1491 | return NULL; | |
1492 | ||
1493 | if (!dev->driver) | |
1494 | return NULL; | |
1495 | ||
1496 | return dev->driver->owner; | |
1497 | } | |
1498 | ||
1499 | /** | |
34f42a07 | 1500 | * fcoe_ethdrv_get() - Hold the Ethernet driver |
85b4aa49 RL |
1501 | * @netdev: the target netdev |
1502 | * | |
34f42a07 RL |
1503 | * Holds the Ethernet driver module by try_module_get() for |
1504 | * the corresponding netdev. | |
1505 | * | |
85b4aa49 | 1506 | * Returns: 0 for succsss |
34f42a07 | 1507 | */ |
85b4aa49 RL |
1508 | static int fcoe_ethdrv_get(const struct net_device *netdev) |
1509 | { | |
1510 | struct module *owner; | |
1511 | ||
1512 | owner = fcoe_netdev_to_module_owner(netdev); | |
1513 | if (owner) { | |
56b854bb JB |
1514 | printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n", |
1515 | module_name(owner), netdev->name); | |
85b4aa49 RL |
1516 | return try_module_get(owner); |
1517 | } | |
1518 | return -ENODEV; | |
1519 | } | |
1520 | ||
1521 | /** | |
34f42a07 | 1522 | * fcoe_ethdrv_put() - Release the Ethernet driver |
85b4aa49 RL |
1523 | * @netdev: the target netdev |
1524 | * | |
34f42a07 RL |
1525 | * Releases the Ethernet driver module by module_put for |
1526 | * the corresponding netdev. | |
1527 | * | |
85b4aa49 | 1528 | * Returns: 0 for succsss |
34f42a07 | 1529 | */ |
85b4aa49 RL |
1530 | static int fcoe_ethdrv_put(const struct net_device *netdev) |
1531 | { | |
1532 | struct module *owner; | |
1533 | ||
1534 | owner = fcoe_netdev_to_module_owner(netdev); | |
1535 | if (owner) { | |
56b854bb JB |
1536 | printk(KERN_DEBUG "fcoe:release driver module %s for %s\n", |
1537 | module_name(owner), netdev->name); | |
85b4aa49 RL |
1538 | module_put(owner); |
1539 | return 0; | |
1540 | } | |
1541 | return -ENODEV; | |
1542 | } | |
1543 | ||
1544 | /** | |
34f42a07 | 1545 | * fcoe_destroy() - handles the destroy from sysfs |
85b4aa49 RL |
1546 | * @buffer: expcted to be a eth if name |
1547 | * @kp: associated kernel param | |
1548 | * | |
1549 | * Returns: 0 for success | |
34f42a07 | 1550 | */ |
85b4aa49 RL |
1551 | static int fcoe_destroy(const char *buffer, struct kernel_param *kp) |
1552 | { | |
1553 | int rc; | |
1554 | struct net_device *netdev; | |
1555 | ||
1556 | netdev = fcoe_if_to_netdev(buffer); | |
1557 | if (!netdev) { | |
1558 | rc = -ENODEV; | |
1559 | goto out_nodev; | |
1560 | } | |
1561 | /* look for existing lport */ | |
1562 | if (!fcoe_hostlist_lookup(netdev)) { | |
1563 | rc = -ENODEV; | |
1564 | goto out_putdev; | |
1565 | } | |
7f349142 | 1566 | rc = fcoe_if_destroy(netdev); |
85b4aa49 | 1567 | if (rc) { |
7f349142 | 1568 | printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n", |
85b4aa49 RL |
1569 | netdev->name); |
1570 | rc = -EIO; | |
1571 | goto out_putdev; | |
1572 | } | |
1573 | fcoe_ethdrv_put(netdev); | |
1574 | rc = 0; | |
1575 | out_putdev: | |
1576 | dev_put(netdev); | |
1577 | out_nodev: | |
1578 | return rc; | |
1579 | } | |
1580 | ||
1581 | /** | |
34f42a07 | 1582 | * fcoe_create() - Handles the create call from sysfs |
85b4aa49 RL |
1583 | * @buffer: expcted to be a eth if name |
1584 | * @kp: associated kernel param | |
1585 | * | |
1586 | * Returns: 0 for success | |
34f42a07 | 1587 | */ |
85b4aa49 RL |
1588 | static int fcoe_create(const char *buffer, struct kernel_param *kp) |
1589 | { | |
1590 | int rc; | |
1591 | struct net_device *netdev; | |
1592 | ||
1593 | netdev = fcoe_if_to_netdev(buffer); | |
1594 | if (!netdev) { | |
1595 | rc = -ENODEV; | |
1596 | goto out_nodev; | |
1597 | } | |
1598 | /* look for existing lport */ | |
1599 | if (fcoe_hostlist_lookup(netdev)) { | |
1600 | rc = -EEXIST; | |
1601 | goto out_putdev; | |
1602 | } | |
1603 | fcoe_ethdrv_get(netdev); | |
1604 | ||
7f349142 | 1605 | rc = fcoe_if_create(netdev); |
85b4aa49 | 1606 | if (rc) { |
7f349142 | 1607 | printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n", |
85b4aa49 RL |
1608 | netdev->name); |
1609 | fcoe_ethdrv_put(netdev); | |
1610 | rc = -EIO; | |
1611 | goto out_putdev; | |
1612 | } | |
1613 | rc = 0; | |
1614 | out_putdev: | |
1615 | dev_put(netdev); | |
1616 | out_nodev: | |
1617 | return rc; | |
1618 | } | |
1619 | ||
1620 | module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR); | |
1621 | __MODULE_PARM_TYPE(create, "string"); | |
1622 | MODULE_PARM_DESC(create, "Create fcoe port using net device passed in."); | |
1623 | module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR); | |
1624 | __MODULE_PARM_TYPE(destroy, "string"); | |
1625 | MODULE_PARM_DESC(destroy, "Destroy fcoe port"); | |
1626 | ||
34f42a07 RL |
1627 | /** |
1628 | * fcoe_link_ok() - Check if link is ok for the fc_lport | |
85b4aa49 RL |
1629 | * @lp: ptr to the fc_lport |
1630 | * | |
1631 | * Any permanently-disqualifying conditions have been previously checked. | |
1632 | * This also updates the speed setting, which may change with link for 100/1000. | |
1633 | * | |
1634 | * This function should probably be checking for PAUSE support at some point | |
1635 | * in the future. Currently Per-priority-pause is not determinable using | |
1636 | * ethtool, so we shouldn't be restrictive until that problem is resolved. | |
1637 | * | |
1638 | * Returns: 0 if link is OK for use by FCoE. | |
1639 | * | |
1640 | */ | |
1641 | int fcoe_link_ok(struct fc_lport *lp) | |
1642 | { | |
fc47ff6b | 1643 | struct fcoe_softc *fc = lport_priv(lp); |
85b4aa49 RL |
1644 | struct net_device *dev = fc->real_dev; |
1645 | struct ethtool_cmd ecmd = { ETHTOOL_GSET }; | |
1646 | int rc = 0; | |
1647 | ||
1648 | if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) { | |
1649 | dev = fc->phys_dev; | |
1650 | if (dev->ethtool_ops->get_settings) { | |
1651 | dev->ethtool_ops->get_settings(dev, &ecmd); | |
1652 | lp->link_supported_speeds &= | |
1653 | ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT); | |
1654 | if (ecmd.supported & (SUPPORTED_1000baseT_Half | | |
1655 | SUPPORTED_1000baseT_Full)) | |
1656 | lp->link_supported_speeds |= FC_PORTSPEED_1GBIT; | |
1657 | if (ecmd.supported & SUPPORTED_10000baseT_Full) | |
1658 | lp->link_supported_speeds |= | |
1659 | FC_PORTSPEED_10GBIT; | |
1660 | if (ecmd.speed == SPEED_1000) | |
1661 | lp->link_speed = FC_PORTSPEED_1GBIT; | |
1662 | if (ecmd.speed == SPEED_10000) | |
1663 | lp->link_speed = FC_PORTSPEED_10GBIT; | |
1664 | } | |
1665 | } else | |
1666 | rc = -1; | |
1667 | ||
1668 | return rc; | |
1669 | } | |
1670 | EXPORT_SYMBOL_GPL(fcoe_link_ok); | |
1671 | ||
34f42a07 RL |
1672 | /** |
1673 | * fcoe_percpu_clean() - Clear the pending skbs for an lport | |
85b4aa49 RL |
1674 | * @lp: the fc_lport |
1675 | */ | |
1676 | void fcoe_percpu_clean(struct fc_lport *lp) | |
1677 | { | |
85b4aa49 RL |
1678 | struct fcoe_percpu_s *pp; |
1679 | struct fcoe_rcv_info *fr; | |
1680 | struct sk_buff_head *list; | |
1681 | struct sk_buff *skb, *next; | |
1682 | struct sk_buff *head; | |
5e5e92df | 1683 | unsigned int cpu; |
85b4aa49 | 1684 | |
5e5e92df RL |
1685 | for_each_possible_cpu(cpu) { |
1686 | pp = &per_cpu(fcoe_percpu, cpu); | |
1687 | spin_lock_bh(&pp->fcoe_rx_list.lock); | |
1688 | list = &pp->fcoe_rx_list; | |
1689 | head = list->next; | |
1690 | for (skb = head; skb != (struct sk_buff *)list; | |
1691 | skb = next) { | |
1692 | next = skb->next; | |
1693 | fr = fcoe_dev_from_skb(skb); | |
1694 | if (fr->fr_dev == lp) { | |
1695 | __skb_unlink(skb, list); | |
1696 | kfree_skb(skb); | |
85b4aa49 | 1697 | } |
85b4aa49 | 1698 | } |
5e5e92df | 1699 | spin_unlock_bh(&pp->fcoe_rx_list.lock); |
85b4aa49 RL |
1700 | } |
1701 | } | |
1702 | EXPORT_SYMBOL_GPL(fcoe_percpu_clean); | |
1703 | ||
1704 | /** | |
34f42a07 | 1705 | * fcoe_clean_pending_queue() - Dequeue a skb and free it |
85b4aa49 RL |
1706 | * @lp: the corresponding fc_lport |
1707 | * | |
1708 | * Returns: none | |
34f42a07 | 1709 | */ |
85b4aa49 RL |
1710 | void fcoe_clean_pending_queue(struct fc_lport *lp) |
1711 | { | |
1712 | struct fcoe_softc *fc = lport_priv(lp); | |
1713 | struct sk_buff *skb; | |
1714 | ||
1715 | spin_lock_bh(&fc->fcoe_pending_queue.lock); | |
1716 | while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) { | |
1717 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
1718 | kfree_skb(skb); | |
1719 | spin_lock_bh(&fc->fcoe_pending_queue.lock); | |
1720 | } | |
1721 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
1722 | } | |
1723 | EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue); | |
1724 | ||
34f42a07 RL |
1725 | /** |
1726 | * fcoe_reset() - Resets the fcoe | |
85b4aa49 RL |
1727 | * @shost: shost the reset is from |
1728 | * | |
1729 | * Returns: always 0 | |
1730 | */ | |
1731 | int fcoe_reset(struct Scsi_Host *shost) | |
1732 | { | |
1733 | struct fc_lport *lport = shost_priv(shost); | |
1734 | fc_lport_reset(lport); | |
1735 | return 0; | |
1736 | } | |
1737 | EXPORT_SYMBOL_GPL(fcoe_reset); | |
1738 | ||
34f42a07 RL |
1739 | /** |
1740 | * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device | |
85b4aa49 RL |
1741 | * @device: this is currently ptr to net_device |
1742 | * | |
1743 | * Returns: NULL or the located fcoe_softc | |
1744 | */ | |
b2ab99c9 RL |
1745 | static struct fcoe_softc * |
1746 | fcoe_hostlist_lookup_softc(const struct net_device *dev) | |
85b4aa49 RL |
1747 | { |
1748 | struct fcoe_softc *fc; | |
1749 | ||
1750 | read_lock(&fcoe_hostlist_lock); | |
1751 | list_for_each_entry(fc, &fcoe_hostlist, list) { | |
1752 | if (fc->real_dev == dev) { | |
1753 | read_unlock(&fcoe_hostlist_lock); | |
1754 | return fc; | |
1755 | } | |
1756 | } | |
1757 | read_unlock(&fcoe_hostlist_lock); | |
1758 | return NULL; | |
1759 | } | |
1760 | ||
34f42a07 RL |
1761 | /** |
1762 | * fcoe_hostlist_lookup() - Find the corresponding lport by netdev | |
85b4aa49 RL |
1763 | * @netdev: ptr to net_device |
1764 | * | |
1765 | * Returns: 0 for success | |
1766 | */ | |
1767 | struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev) | |
1768 | { | |
1769 | struct fcoe_softc *fc; | |
1770 | ||
1771 | fc = fcoe_hostlist_lookup_softc(netdev); | |
1772 | ||
97c8389d | 1773 | return (fc) ? fc->ctlr.lp : NULL; |
85b4aa49 RL |
1774 | } |
1775 | EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup); | |
1776 | ||
34f42a07 RL |
1777 | /** |
1778 | * fcoe_hostlist_add() - Add a lport to lports list | |
85b4aa49 RL |
1779 | * @lp: ptr to the fc_lport to badded |
1780 | * | |
1781 | * Returns: 0 for success | |
1782 | */ | |
1783 | int fcoe_hostlist_add(const struct fc_lport *lp) | |
1784 | { | |
1785 | struct fcoe_softc *fc; | |
1786 | ||
1787 | fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp)); | |
1788 | if (!fc) { | |
fc47ff6b | 1789 | fc = lport_priv(lp); |
85b4aa49 RL |
1790 | write_lock_bh(&fcoe_hostlist_lock); |
1791 | list_add_tail(&fc->list, &fcoe_hostlist); | |
1792 | write_unlock_bh(&fcoe_hostlist_lock); | |
1793 | } | |
1794 | return 0; | |
1795 | } | |
1796 | EXPORT_SYMBOL_GPL(fcoe_hostlist_add); | |
1797 | ||
34f42a07 RL |
1798 | /** |
1799 | * fcoe_hostlist_remove() - remove a lport from lports list | |
85b4aa49 RL |
1800 | * @lp: ptr to the fc_lport to badded |
1801 | * | |
1802 | * Returns: 0 for success | |
1803 | */ | |
1804 | int fcoe_hostlist_remove(const struct fc_lport *lp) | |
1805 | { | |
1806 | struct fcoe_softc *fc; | |
1807 | ||
1808 | fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp)); | |
1809 | BUG_ON(!fc); | |
1810 | write_lock_bh(&fcoe_hostlist_lock); | |
1811 | list_del(&fc->list); | |
1812 | write_unlock_bh(&fcoe_hostlist_lock); | |
1813 | ||
1814 | return 0; | |
1815 | } | |
1816 | EXPORT_SYMBOL_GPL(fcoe_hostlist_remove); | |
1817 | ||
85b4aa49 | 1818 | /** |
34f42a07 | 1819 | * fcoe_init() - fcoe module loading initialization |
85b4aa49 | 1820 | * |
85b4aa49 | 1821 | * Returns 0 on success, negative on failure |
34f42a07 | 1822 | */ |
85b4aa49 RL |
1823 | static int __init fcoe_init(void) |
1824 | { | |
38eccabd | 1825 | unsigned int cpu; |
8976f424 | 1826 | int rc = 0; |
85b4aa49 RL |
1827 | struct fcoe_percpu_s *p; |
1828 | ||
85b4aa49 RL |
1829 | INIT_LIST_HEAD(&fcoe_hostlist); |
1830 | rwlock_init(&fcoe_hostlist_lock); | |
1831 | ||
38eccabd | 1832 | for_each_possible_cpu(cpu) { |
5e5e92df | 1833 | p = &per_cpu(fcoe_percpu, cpu); |
38eccabd RL |
1834 | skb_queue_head_init(&p->fcoe_rx_list); |
1835 | } | |
1836 | ||
8976f424 RL |
1837 | for_each_online_cpu(cpu) |
1838 | fcoe_percpu_thread_create(cpu); | |
85b4aa49 | 1839 | |
8976f424 RL |
1840 | /* Initialize per CPU interrupt thread */ |
1841 | rc = register_hotcpu_notifier(&fcoe_cpu_notifier); | |
1842 | if (rc) | |
1843 | goto out_free; | |
85b4aa49 | 1844 | |
8976f424 | 1845 | /* Setup link change notification */ |
85b4aa49 RL |
1846 | fcoe_dev_setup(); |
1847 | ||
a468f328 RL |
1848 | setup_timer(&fcoe_timer, fcoe_watchdog, 0); |
1849 | ||
1850 | mod_timer(&fcoe_timer, jiffies + (10 * HZ)); | |
85b4aa49 | 1851 | |
7f349142 | 1852 | fcoe_if_init(); |
85b4aa49 RL |
1853 | |
1854 | return 0; | |
8976f424 RL |
1855 | |
1856 | out_free: | |
1857 | for_each_online_cpu(cpu) { | |
1858 | fcoe_percpu_thread_destroy(cpu); | |
1859 | } | |
1860 | ||
1861 | return rc; | |
85b4aa49 RL |
1862 | } |
1863 | module_init(fcoe_init); | |
1864 | ||
1865 | /** | |
34f42a07 | 1866 | * fcoe_exit() - fcoe module unloading cleanup |
85b4aa49 RL |
1867 | * |
1868 | * Returns 0 on success, negative on failure | |
34f42a07 | 1869 | */ |
85b4aa49 RL |
1870 | static void __exit fcoe_exit(void) |
1871 | { | |
5e5e92df | 1872 | unsigned int cpu; |
85b4aa49 | 1873 | struct fcoe_softc *fc, *tmp; |
85b4aa49 | 1874 | |
85b4aa49 RL |
1875 | fcoe_dev_cleanup(); |
1876 | ||
582b45bc | 1877 | /* Stop the timer */ |
85b4aa49 RL |
1878 | del_timer_sync(&fcoe_timer); |
1879 | ||
5919a595 | 1880 | /* releases the associated fcoe hosts */ |
85b4aa49 | 1881 | list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list) |
7f349142 | 1882 | fcoe_if_destroy(fc->real_dev); |
85b4aa49 | 1883 | |
8976f424 RL |
1884 | unregister_hotcpu_notifier(&fcoe_cpu_notifier); |
1885 | ||
1886 | for_each_online_cpu(cpu) { | |
1887 | fcoe_percpu_thread_destroy(cpu); | |
85b4aa49 RL |
1888 | } |
1889 | ||
7f349142 VD |
1890 | /* detach from scsi transport */ |
1891 | fcoe_if_exit(); | |
85b4aa49 RL |
1892 | } |
1893 | module_exit(fcoe_exit); |