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