2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
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.
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
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.
17 * Maintained at www.Open-FCoE.org
23 * These comments only apply to the 'port code' which consists of the lport,
24 * disc and rport blocks.
28 * The lport, disc and rport blocks all have mutexes that are used to protect
29 * those objects. The main motivation for these locks is to prevent from
30 * having an lport reset just before we send a frame. In that scenario the
31 * lport's FID would get set to zero and then we'd send a frame with an
32 * invalid SID. We also need to ensure that states don't change unexpectedly
33 * while processing another state.
37 * The following hierarchy defines the locking rules. A greater lock
38 * may be held before acquiring a lesser lock, but a lesser lock should never
39 * be held while attempting to acquire a greater lock. Here is the hierarchy-
41 * lport > disc, lport > rport, disc > rport
45 * The callbacks cause complications with this scheme. There is a callback
46 * from the rport (to either lport or disc) and a callback from disc
49 * As rports exit the rport state machine a callback is made to the owner of
50 * the rport to notify success or failure. Since the callback is likely to
51 * cause the lport or disc to grab its lock we cannot hold the rport lock
52 * while making the callback. To ensure that the rport is not free'd while
53 * processing the callback the rport callbacks are serialized through a
54 * single-threaded workqueue. An rport would never be free'd while in a
55 * callback handler because no other rport work in this queue can be executed
58 * When discovery succeeds or fails a callback is made to the lport as
59 * notification. Currently, successful discovery causes the lport to take no
60 * action. A failure will cause the lport to reset. There is likely a circular
61 * locking problem with this implementation.
67 * The critical sections protected by the lport's mutex are quite broad and
68 * may be improved upon in the future. The lport code and its locking doesn't
69 * influence the I/O path, so excessive locking doesn't penalize I/O
72 * The strategy is to lock whenever processing a request or response. Note
73 * that every _enter_* function corresponds to a state change. They generally
74 * change the lports state and then send a request out on the wire. We lock
75 * before calling any of these functions to protect that state change. This
76 * means that the entry points into the lport block manage the locks while
77 * the state machine can transition between states (i.e. _enter_* functions)
78 * while always staying protected.
80 * When handling responses we also hold the lport mutex broadly. When the
81 * lport receives the response frame it locks the mutex and then calls the
82 * appropriate handler for the particuar response. Generally a response will
83 * trigger a state change and so the lock must already be held.
85 * Retries also have to consider the locking. The retries occur from a work
86 * context and the work function will lock the lport and then retry the state
87 * (i.e. _enter_* function).
90 #include <linux/timer.h>
91 #include <linux/delay.h>
92 #include <linux/module.h>
93 #include <linux/slab.h>
94 #include <asm/unaligned.h>
96 #include <scsi/fc/fc_gs.h>
98 #include <scsi/libfc.h>
99 #include <scsi/fc_encode.h>
100 #include <linux/scatterlist.h>
102 #include "fc_libfc.h"
104 /* Fabric IDs to use for point-to-point mode, chosen on whims. */
105 #define FC_LOCAL_PTP_FID_LO 0x010101
106 #define FC_LOCAL_PTP_FID_HI 0x010102
108 #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
110 static void fc_lport_error(struct fc_lport
*, struct fc_frame
*);
112 static void fc_lport_enter_reset(struct fc_lport
*);
113 static void fc_lport_enter_flogi(struct fc_lport
*);
114 static void fc_lport_enter_dns(struct fc_lport
*);
115 static void fc_lport_enter_ns(struct fc_lport
*, enum fc_lport_state
);
116 static void fc_lport_enter_scr(struct fc_lport
*);
117 static void fc_lport_enter_ready(struct fc_lport
*);
118 static void fc_lport_enter_logo(struct fc_lport
*);
119 static void fc_lport_enter_fdmi(struct fc_lport
*lport
);
120 static void fc_lport_enter_ms(struct fc_lport
*, enum fc_lport_state
);
122 static const char *fc_lport_state_names
[] = {
123 [LPORT_ST_DISABLED
] = "disabled",
124 [LPORT_ST_FLOGI
] = "FLOGI",
125 [LPORT_ST_DNS
] = "dNS",
126 [LPORT_ST_RNN_ID
] = "RNN_ID",
127 [LPORT_ST_RSNN_NN
] = "RSNN_NN",
128 [LPORT_ST_RSPN_ID
] = "RSPN_ID",
129 [LPORT_ST_RFT_ID
] = "RFT_ID",
130 [LPORT_ST_RFF_ID
] = "RFF_ID",
131 [LPORT_ST_FDMI
] = "FDMI",
132 [LPORT_ST_RHBA
] = "RHBA",
133 [LPORT_ST_RPA
] = "RPA",
134 [LPORT_ST_DHBA
] = "DHBA",
135 [LPORT_ST_DPRT
] = "DPRT",
136 [LPORT_ST_SCR
] = "SCR",
137 [LPORT_ST_READY
] = "Ready",
138 [LPORT_ST_LOGO
] = "LOGO",
139 [LPORT_ST_RESET
] = "reset",
143 * struct fc_bsg_info - FC Passthrough managemet structure
144 * @job: The passthrough job
145 * @lport: The local port to pass through a command
146 * @rsp_code: The expected response code
147 * @sg: job->reply_payload.sg_list
148 * @nents: job->reply_payload.sg_cnt
149 * @offset: The offset into the response data
153 struct fc_lport
*lport
;
155 struct scatterlist
*sg
;
161 * fc_frame_drop() - Dummy frame handler
162 * @lport: The local port the frame was received on
163 * @fp: The received frame
165 static int fc_frame_drop(struct fc_lport
*lport
, struct fc_frame
*fp
)
172 * fc_lport_rport_callback() - Event handler for rport events
173 * @lport: The lport which is receiving the event
174 * @rdata: private remote port data
175 * @event: The event that occurred
177 * Locking Note: The rport lock should not be held when calling
180 static void fc_lport_rport_callback(struct fc_lport
*lport
,
181 struct fc_rport_priv
*rdata
,
182 enum fc_rport_event event
)
184 FC_LPORT_DBG(lport
, "Received a %d event for port (%6.6x)\n", event
,
187 mutex_lock(&lport
->lp_mutex
);
190 if (lport
->state
== LPORT_ST_DNS
) {
191 lport
->dns_rdata
= rdata
;
192 fc_lport_enter_ns(lport
, LPORT_ST_RNN_ID
);
193 } else if (lport
->state
== LPORT_ST_FDMI
) {
194 lport
->ms_rdata
= rdata
;
195 fc_lport_enter_ms(lport
, LPORT_ST_DHBA
);
197 FC_LPORT_DBG(lport
, "Received an READY event "
198 "on port (%6.6x) for the directory "
199 "server, but the lport is not "
200 "in the DNS or FDMI state, it's in the "
201 "%d state", rdata
->ids
.port_id
,
203 fc_rport_logoff(rdata
);
207 case RPORT_EV_FAILED
:
209 if (rdata
->ids
.port_id
== FC_FID_DIR_SERV
)
210 lport
->dns_rdata
= NULL
;
211 else if (rdata
->ids
.port_id
== FC_FID_MGMT_SERV
)
212 lport
->ms_rdata
= NULL
;
217 mutex_unlock(&lport
->lp_mutex
);
221 * fc_lport_state() - Return a string which represents the lport's state
222 * @lport: The lport whose state is to converted to a string
224 static const char *fc_lport_state(struct fc_lport
*lport
)
228 cp
= fc_lport_state_names
[lport
->state
];
235 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
236 * @lport: The lport to attach the ptp rport to
237 * @remote_fid: The FID of the ptp rport
238 * @remote_wwpn: The WWPN of the ptp rport
239 * @remote_wwnn: The WWNN of the ptp rport
241 static void fc_lport_ptp_setup(struct fc_lport
*lport
,
242 u32 remote_fid
, u64 remote_wwpn
,
245 lockdep_assert_held(&lport
->lp_mutex
);
247 if (lport
->ptp_rdata
) {
248 fc_rport_logoff(lport
->ptp_rdata
);
249 kref_put(&lport
->ptp_rdata
->kref
, fc_rport_destroy
);
251 mutex_lock(&lport
->disc
.disc_mutex
);
252 lport
->ptp_rdata
= fc_rport_create(lport
, remote_fid
);
253 kref_get(&lport
->ptp_rdata
->kref
);
254 lport
->ptp_rdata
->ids
.port_name
= remote_wwpn
;
255 lport
->ptp_rdata
->ids
.node_name
= remote_wwnn
;
256 mutex_unlock(&lport
->disc
.disc_mutex
);
258 fc_rport_login(lport
->ptp_rdata
);
260 fc_lport_enter_ready(lport
);
264 * fc_get_host_port_state() - Return the port state of the given Scsi_Host
265 * @shost: The SCSI host whose port state is to be determined
267 void fc_get_host_port_state(struct Scsi_Host
*shost
)
269 struct fc_lport
*lport
= shost_priv(shost
);
271 mutex_lock(&lport
->lp_mutex
);
273 fc_host_port_state(shost
) = FC_PORTSTATE_LINKDOWN
;
275 switch (lport
->state
) {
277 fc_host_port_state(shost
) = FC_PORTSTATE_ONLINE
;
280 fc_host_port_state(shost
) = FC_PORTSTATE_OFFLINE
;
282 mutex_unlock(&lport
->lp_mutex
);
284 EXPORT_SYMBOL(fc_get_host_port_state
);
287 * fc_get_host_speed() - Return the speed of the given Scsi_Host
288 * @shost: The SCSI host whose port speed is to be determined
290 void fc_get_host_speed(struct Scsi_Host
*shost
)
292 struct fc_lport
*lport
= shost_priv(shost
);
294 fc_host_speed(shost
) = lport
->link_speed
;
296 EXPORT_SYMBOL(fc_get_host_speed
);
299 * fc_get_host_stats() - Return the Scsi_Host's statistics
300 * @shost: The SCSI host whose statistics are to be returned
302 struct fc_host_statistics
*fc_get_host_stats(struct Scsi_Host
*shost
)
304 struct fc_host_statistics
*fc_stats
;
305 struct fc_lport
*lport
= shost_priv(shost
);
307 u64 fcp_in_bytes
= 0;
308 u64 fcp_out_bytes
= 0;
310 fc_stats
= &lport
->host_stats
;
311 memset(fc_stats
, 0, sizeof(struct fc_host_statistics
));
313 fc_stats
->seconds_since_last_reset
= (jiffies
- lport
->boot_time
) / HZ
;
315 for_each_possible_cpu(cpu
) {
316 struct fc_stats
*stats
;
318 stats
= per_cpu_ptr(lport
->stats
, cpu
);
320 fc_stats
->tx_frames
+= stats
->TxFrames
;
321 fc_stats
->tx_words
+= stats
->TxWords
;
322 fc_stats
->rx_frames
+= stats
->RxFrames
;
323 fc_stats
->rx_words
+= stats
->RxWords
;
324 fc_stats
->error_frames
+= stats
->ErrorFrames
;
325 fc_stats
->invalid_crc_count
+= stats
->InvalidCRCCount
;
326 fc_stats
->fcp_input_requests
+= stats
->InputRequests
;
327 fc_stats
->fcp_output_requests
+= stats
->OutputRequests
;
328 fc_stats
->fcp_control_requests
+= stats
->ControlRequests
;
329 fcp_in_bytes
+= stats
->InputBytes
;
330 fcp_out_bytes
+= stats
->OutputBytes
;
331 fc_stats
->fcp_packet_alloc_failures
+= stats
->FcpPktAllocFails
;
332 fc_stats
->fcp_packet_aborts
+= stats
->FcpPktAborts
;
333 fc_stats
->fcp_frame_alloc_failures
+= stats
->FcpFrameAllocFails
;
334 fc_stats
->link_failure_count
+= stats
->LinkFailureCount
;
336 fc_stats
->fcp_input_megabytes
= div_u64(fcp_in_bytes
, 1000000);
337 fc_stats
->fcp_output_megabytes
= div_u64(fcp_out_bytes
, 1000000);
338 fc_stats
->lip_count
= -1;
339 fc_stats
->nos_count
= -1;
340 fc_stats
->loss_of_sync_count
= -1;
341 fc_stats
->loss_of_signal_count
= -1;
342 fc_stats
->prim_seq_protocol_err_count
= -1;
343 fc_stats
->dumped_frames
= -1;
345 /* update exches stats */
346 fc_exch_update_stats(lport
);
350 EXPORT_SYMBOL(fc_get_host_stats
);
353 * fc_lport_flogi_fill() - Fill in FLOGI command for request
354 * @lport: The local port the FLOGI is for
355 * @flogi: The FLOGI command
358 static void fc_lport_flogi_fill(struct fc_lport
*lport
,
359 struct fc_els_flogi
*flogi
,
362 struct fc_els_csp
*sp
;
363 struct fc_els_cssp
*cp
;
365 memset(flogi
, 0, sizeof(*flogi
));
366 flogi
->fl_cmd
= (u8
) op
;
367 put_unaligned_be64(lport
->wwpn
, &flogi
->fl_wwpn
);
368 put_unaligned_be64(lport
->wwnn
, &flogi
->fl_wwnn
);
370 sp
->sp_hi_ver
= 0x20;
371 sp
->sp_lo_ver
= 0x20;
372 sp
->sp_bb_cred
= htons(10); /* this gets set by gateway */
373 sp
->sp_bb_data
= htons((u16
) lport
->mfs
);
374 cp
= &flogi
->fl_cssp
[3 - 1]; /* class 3 parameters */
375 cp
->cp_class
= htons(FC_CPC_VALID
| FC_CPC_SEQ
);
376 if (op
!= ELS_FLOGI
) {
377 sp
->sp_features
= htons(FC_SP_FT_CIRO
);
378 sp
->sp_tot_seq
= htons(255); /* seq. we accept */
379 sp
->sp_rel_off
= htons(0x1f);
380 sp
->sp_e_d_tov
= htonl(lport
->e_d_tov
);
382 cp
->cp_rdfs
= htons((u16
) lport
->mfs
);
383 cp
->cp_con_seq
= htons(255);
389 * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
390 * @lport: The local port to add a new FC-4 type to
391 * @type: The new FC-4 type
393 static void fc_lport_add_fc4_type(struct fc_lport
*lport
, enum fc_fh_type type
)
397 mp
= &lport
->fcts
.ff_type_map
[type
/ FC_NS_BPW
];
398 *mp
= htonl(ntohl(*mp
) | 1UL << (type
% FC_NS_BPW
));
402 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
403 * @lport: Fibre Channel local port receiving the RLIR
404 * @fp: The RLIR request frame
406 static void fc_lport_recv_rlir_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
408 lockdep_assert_held(&lport
->lp_mutex
);
410 FC_LPORT_DBG(lport
, "Received RLIR request while in state %s\n",
411 fc_lport_state(lport
));
413 fc_seq_els_rsp_send(fp
, ELS_LS_ACC
, NULL
);
418 * fc_lport_recv_echo_req() - Handle received ECHO request
419 * @lport: The local port receiving the ECHO
420 * @fp: ECHO request frame
422 static void fc_lport_recv_echo_req(struct fc_lport
*lport
,
423 struct fc_frame
*in_fp
)
430 lockdep_assert_held(&lport
->lp_mutex
);
432 FC_LPORT_DBG(lport
, "Received ECHO request while in state %s\n",
433 fc_lport_state(lport
));
435 len
= fr_len(in_fp
) - sizeof(struct fc_frame_header
);
436 pp
= fc_frame_payload_get(in_fp
, len
);
438 if (len
< sizeof(__be32
))
439 len
= sizeof(__be32
);
441 fp
= fc_frame_alloc(lport
, len
);
443 dp
= fc_frame_payload_get(fp
, len
);
445 *((__be32
*)dp
) = htonl(ELS_LS_ACC
<< 24);
446 fc_fill_reply_hdr(fp
, in_fp
, FC_RCTL_ELS_REP
, 0);
447 lport
->tt
.frame_send(lport
, fp
);
449 fc_frame_free(in_fp
);
453 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
454 * @lport: The local port receiving the RNID
455 * @fp: The RNID request frame
457 static void fc_lport_recv_rnid_req(struct fc_lport
*lport
,
458 struct fc_frame
*in_fp
)
461 struct fc_els_rnid
*req
;
463 struct fc_els_rnid_resp rnid
;
464 struct fc_els_rnid_cid cid
;
465 struct fc_els_rnid_gen gen
;
467 struct fc_seq_els_data rjt_data
;
471 lockdep_assert_held(&lport
->lp_mutex
);
473 FC_LPORT_DBG(lport
, "Received RNID request while in state %s\n",
474 fc_lport_state(lport
));
476 req
= fc_frame_payload_get(in_fp
, sizeof(*req
));
478 rjt_data
.reason
= ELS_RJT_LOGIC
;
479 rjt_data
.explan
= ELS_EXPL_NONE
;
480 fc_seq_els_rsp_send(in_fp
, ELS_LS_RJT
, &rjt_data
);
484 if (fmt
!= ELS_RNIDF_GEN
||
485 ntohl(lport
->rnid_gen
.rnid_atype
) == 0) {
486 fmt
= ELS_RNIDF_NONE
; /* nothing to provide */
487 len
-= sizeof(rp
->gen
);
489 fp
= fc_frame_alloc(lport
, len
);
491 rp
= fc_frame_payload_get(fp
, len
);
493 rp
->rnid
.rnid_cmd
= ELS_LS_ACC
;
494 rp
->rnid
.rnid_fmt
= fmt
;
495 rp
->rnid
.rnid_cid_len
= sizeof(rp
->cid
);
496 rp
->cid
.rnid_wwpn
= htonll(lport
->wwpn
);
497 rp
->cid
.rnid_wwnn
= htonll(lport
->wwnn
);
498 if (fmt
== ELS_RNIDF_GEN
) {
499 rp
->rnid
.rnid_sid_len
= sizeof(rp
->gen
);
500 memcpy(&rp
->gen
, &lport
->rnid_gen
,
503 fc_fill_reply_hdr(fp
, in_fp
, FC_RCTL_ELS_REP
, 0);
504 lport
->tt
.frame_send(lport
, fp
);
507 fc_frame_free(in_fp
);
511 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
512 * @lport: The local port receiving the LOGO
513 * @fp: The LOGO request frame
515 static void fc_lport_recv_logo_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
517 lockdep_assert_held(&lport
->lp_mutex
);
519 fc_seq_els_rsp_send(fp
, ELS_LS_ACC
, NULL
);
520 fc_lport_enter_reset(lport
);
525 * fc_fabric_login() - Start the lport state machine
526 * @lport: The local port that should log into the fabric
528 * Locking Note: This function should not be called
529 * with the lport lock held.
531 int fc_fabric_login(struct fc_lport
*lport
)
535 mutex_lock(&lport
->lp_mutex
);
536 if (lport
->state
== LPORT_ST_DISABLED
||
537 lport
->state
== LPORT_ST_LOGO
) {
538 fc_lport_state_enter(lport
, LPORT_ST_RESET
);
539 fc_lport_enter_reset(lport
);
542 mutex_unlock(&lport
->lp_mutex
);
546 EXPORT_SYMBOL(fc_fabric_login
);
549 * __fc_linkup() - Handler for transport linkup events
550 * @lport: The lport whose link is up
552 void __fc_linkup(struct fc_lport
*lport
)
554 lockdep_assert_held(&lport
->lp_mutex
);
556 if (!lport
->link_up
) {
559 if (lport
->state
== LPORT_ST_RESET
)
560 fc_lport_enter_flogi(lport
);
565 * fc_linkup() - Handler for transport linkup events
566 * @lport: The local port whose link is up
568 void fc_linkup(struct fc_lport
*lport
)
570 printk(KERN_INFO
"host%d: libfc: Link up on port (%6.6x)\n",
571 lport
->host
->host_no
, lport
->port_id
);
573 mutex_lock(&lport
->lp_mutex
);
575 mutex_unlock(&lport
->lp_mutex
);
577 EXPORT_SYMBOL(fc_linkup
);
580 * __fc_linkdown() - Handler for transport linkdown events
581 * @lport: The lport whose link is down
583 void __fc_linkdown(struct fc_lport
*lport
)
585 lockdep_assert_held(&lport
->lp_mutex
);
587 if (lport
->link_up
) {
589 fc_lport_enter_reset(lport
);
590 lport
->tt
.fcp_cleanup(lport
);
595 * fc_linkdown() - Handler for transport linkdown events
596 * @lport: The local port whose link is down
598 void fc_linkdown(struct fc_lport
*lport
)
600 printk(KERN_INFO
"host%d: libfc: Link down on port (%6.6x)\n",
601 lport
->host
->host_no
, lport
->port_id
);
603 mutex_lock(&lport
->lp_mutex
);
604 __fc_linkdown(lport
);
605 mutex_unlock(&lport
->lp_mutex
);
607 EXPORT_SYMBOL(fc_linkdown
);
610 * fc_fabric_logoff() - Logout of the fabric
611 * @lport: The local port to logoff the fabric
614 * 0 for success, -1 for failure
616 int fc_fabric_logoff(struct fc_lport
*lport
)
618 lport
->tt
.disc_stop_final(lport
);
619 mutex_lock(&lport
->lp_mutex
);
620 if (lport
->dns_rdata
)
621 fc_rport_logoff(lport
->dns_rdata
);
622 mutex_unlock(&lport
->lp_mutex
);
623 fc_rport_flush_queue();
624 mutex_lock(&lport
->lp_mutex
);
625 fc_lport_enter_logo(lport
);
626 mutex_unlock(&lport
->lp_mutex
);
627 cancel_delayed_work_sync(&lport
->retry_work
);
630 EXPORT_SYMBOL(fc_fabric_logoff
);
633 * fc_lport_destroy() - Unregister a fc_lport
634 * @lport: The local port to unregister
637 * exit routine for fc_lport instance
638 * clean-up all the allocated memory
639 * and free up other system resources.
642 int fc_lport_destroy(struct fc_lport
*lport
)
644 mutex_lock(&lport
->lp_mutex
);
645 lport
->state
= LPORT_ST_DISABLED
;
647 lport
->tt
.frame_send
= fc_frame_drop
;
648 mutex_unlock(&lport
->lp_mutex
);
650 lport
->tt
.fcp_abort_io(lport
);
651 lport
->tt
.disc_stop_final(lport
);
652 lport
->tt
.exch_mgr_reset(lport
, 0, 0);
653 cancel_delayed_work_sync(&lport
->retry_work
);
654 fc_fc4_del_lport(lport
);
657 EXPORT_SYMBOL(fc_lport_destroy
);
660 * fc_set_mfs() - Set the maximum frame size for a local port
661 * @lport: The local port to set the MFS for
664 int fc_set_mfs(struct fc_lport
*lport
, u32 mfs
)
666 unsigned int old_mfs
;
669 mutex_lock(&lport
->lp_mutex
);
671 old_mfs
= lport
->mfs
;
673 if (mfs
>= FC_MIN_MAX_FRAME
) {
675 if (mfs
> FC_MAX_FRAME
)
677 mfs
-= sizeof(struct fc_frame_header
);
682 if (!rc
&& mfs
< old_mfs
)
683 fc_lport_enter_reset(lport
);
685 mutex_unlock(&lport
->lp_mutex
);
689 EXPORT_SYMBOL(fc_set_mfs
);
692 * fc_lport_disc_callback() - Callback for discovery events
693 * @lport: The local port receiving the event
694 * @event: The discovery event
696 static void fc_lport_disc_callback(struct fc_lport
*lport
,
697 enum fc_disc_event event
)
700 case DISC_EV_SUCCESS
:
701 FC_LPORT_DBG(lport
, "Discovery succeeded\n");
704 printk(KERN_ERR
"host%d: libfc: "
705 "Discovery failed for port (%6.6x)\n",
706 lport
->host
->host_no
, lport
->port_id
);
707 mutex_lock(&lport
->lp_mutex
);
708 fc_lport_enter_reset(lport
);
709 mutex_unlock(&lport
->lp_mutex
);
718 * fc_rport_enter_ready() - Enter the ready state and start discovery
719 * @lport: The local port that is ready
721 static void fc_lport_enter_ready(struct fc_lport
*lport
)
723 lockdep_assert_held(&lport
->lp_mutex
);
725 FC_LPORT_DBG(lport
, "Entered READY from state %s\n",
726 fc_lport_state(lport
));
728 fc_lport_state_enter(lport
, LPORT_ST_READY
);
730 fc_vport_set_state(lport
->vport
, FC_VPORT_ACTIVE
);
731 fc_vports_linkchange(lport
);
733 if (!lport
->ptp_rdata
)
734 lport
->tt
.disc_start(fc_lport_disc_callback
, lport
);
738 * fc_lport_set_port_id() - set the local port Port ID
739 * @lport: The local port which will have its Port ID set.
740 * @port_id: The new port ID.
741 * @fp: The frame containing the incoming request, or NULL.
743 static void fc_lport_set_port_id(struct fc_lport
*lport
, u32 port_id
,
746 lockdep_assert_held(&lport
->lp_mutex
);
749 printk(KERN_INFO
"host%d: Assigned Port ID %6.6x\n",
750 lport
->host
->host_no
, port_id
);
752 lport
->port_id
= port_id
;
754 /* Update the fc_host */
755 fc_host_port_id(lport
->host
) = port_id
;
757 if (lport
->tt
.lport_set_port_id
)
758 lport
->tt
.lport_set_port_id(lport
, port_id
, fp
);
762 * fc_lport_set_port_id() - set the local port Port ID for point-to-multipoint
763 * @lport: The local port which will have its Port ID set.
764 * @port_id: The new port ID.
766 * Called by the lower-level driver when transport sets the local port_id.
767 * This is used in VN_port to VN_port mode for FCoE, and causes FLOGI and
768 * discovery to be skipped.
770 void fc_lport_set_local_id(struct fc_lport
*lport
, u32 port_id
)
772 mutex_lock(&lport
->lp_mutex
);
774 fc_lport_set_port_id(lport
, port_id
, NULL
);
776 switch (lport
->state
) {
780 fc_lport_enter_ready(lport
);
785 mutex_unlock(&lport
->lp_mutex
);
787 EXPORT_SYMBOL(fc_lport_set_local_id
);
790 * fc_lport_recv_flogi_req() - Receive a FLOGI request
791 * @lport: The local port that received the request
792 * @rx_fp: The FLOGI frame
794 * A received FLOGI request indicates a point-to-point connection.
795 * Accept it with the common service parameters indicating our N port.
796 * Set up to do a PLOGI if we have the higher-number WWPN.
798 static void fc_lport_recv_flogi_req(struct fc_lport
*lport
,
799 struct fc_frame
*rx_fp
)
802 struct fc_frame_header
*fh
;
803 struct fc_els_flogi
*flp
;
804 struct fc_els_flogi
*new_flp
;
809 lockdep_assert_held(&lport
->lp_mutex
);
811 FC_LPORT_DBG(lport
, "Received FLOGI request while in state %s\n",
812 fc_lport_state(lport
));
814 remote_fid
= fc_frame_sid(rx_fp
);
815 flp
= fc_frame_payload_get(rx_fp
, sizeof(*flp
));
818 remote_wwpn
= get_unaligned_be64(&flp
->fl_wwpn
);
819 if (remote_wwpn
== lport
->wwpn
) {
820 printk(KERN_WARNING
"host%d: libfc: Received FLOGI from port "
821 "with same WWPN %16.16llx\n",
822 lport
->host
->host_no
, remote_wwpn
);
825 FC_LPORT_DBG(lport
, "FLOGI from port WWPN %16.16llx\n", remote_wwpn
);
828 * XXX what is the right thing to do for FIDs?
829 * The originator might expect our S_ID to be 0xfffffe.
830 * But if so, both of us could end up with the same FID.
832 local_fid
= FC_LOCAL_PTP_FID_LO
;
833 if (remote_wwpn
< lport
->wwpn
) {
834 local_fid
= FC_LOCAL_PTP_FID_HI
;
835 if (!remote_fid
|| remote_fid
== local_fid
)
836 remote_fid
= FC_LOCAL_PTP_FID_LO
;
837 } else if (!remote_fid
) {
838 remote_fid
= FC_LOCAL_PTP_FID_HI
;
841 fc_lport_set_port_id(lport
, local_fid
, rx_fp
);
843 fp
= fc_frame_alloc(lport
, sizeof(*flp
));
845 new_flp
= fc_frame_payload_get(fp
, sizeof(*flp
));
846 fc_lport_flogi_fill(lport
, new_flp
, ELS_FLOGI
);
847 new_flp
->fl_cmd
= (u8
) ELS_LS_ACC
;
850 * Send the response. If this fails, the originator should
851 * repeat the sequence.
853 fc_fill_reply_hdr(fp
, rx_fp
, FC_RCTL_ELS_REP
, 0);
854 fh
= fc_frame_header_get(fp
);
855 hton24(fh
->fh_s_id
, local_fid
);
856 hton24(fh
->fh_d_id
, remote_fid
);
857 lport
->tt
.frame_send(lport
, fp
);
860 fc_lport_error(lport
, fp
);
862 fc_lport_ptp_setup(lport
, remote_fid
, remote_wwpn
,
863 get_unaligned_be64(&flp
->fl_wwnn
));
865 fc_frame_free(rx_fp
);
869 * fc_lport_recv_els_req() - The generic lport ELS request handler
870 * @lport: The local port that received the request
871 * @fp: The request frame
873 * This function will see if the lport handles the request or
874 * if an rport should handle the request.
876 * Locking Note: This function should not be called with the lport
877 * lock held because it will grab the lock.
879 static void fc_lport_recv_els_req(struct fc_lport
*lport
,
882 mutex_lock(&lport
->lp_mutex
);
885 * Handle special ELS cases like FLOGI, LOGO, and
886 * RSCN here. These don't require a session.
887 * Even if we had a session, it might not be ready.
895 switch (fc_frame_payload_op(fp
)) {
897 if (!lport
->point_to_multipoint
)
898 fc_lport_recv_flogi_req(lport
, fp
);
900 fc_rport_recv_req(lport
, fp
);
903 if (fc_frame_sid(fp
) == FC_FID_FLOGI
)
904 fc_lport_recv_logo_req(lport
, fp
);
906 fc_rport_recv_req(lport
, fp
);
909 lport
->tt
.disc_recv_req(lport
, fp
);
912 fc_lport_recv_echo_req(lport
, fp
);
915 fc_lport_recv_rlir_req(lport
, fp
);
918 fc_lport_recv_rnid_req(lport
, fp
);
921 fc_rport_recv_req(lport
, fp
);
925 mutex_unlock(&lport
->lp_mutex
);
928 static int fc_lport_els_prli(struct fc_rport_priv
*rdata
, u32 spp_len
,
929 const struct fc_els_spp
*spp_in
,
930 struct fc_els_spp
*spp_out
)
932 return FC_SPP_RESP_INVL
;
935 struct fc4_prov fc_lport_els_prov
= {
936 .prli
= fc_lport_els_prli
,
937 .recv
= fc_lport_recv_els_req
,
941 * fc_lport_recv() - The generic lport request handler
942 * @lport: The lport that received the request
943 * @fp: The frame the request is in
945 * Locking Note: This function should not be called with the lport
946 * lock held because it may grab the lock.
948 void fc_lport_recv(struct fc_lport
*lport
, struct fc_frame
*fp
)
950 struct fc_frame_header
*fh
= fc_frame_header_get(fp
);
951 struct fc_seq
*sp
= fr_seq(fp
);
952 struct fc4_prov
*prov
;
955 * Use RCU read lock and module_lock to be sure module doesn't
956 * deregister and get unloaded while we're calling it.
957 * try_module_get() is inlined and accepts a NULL parameter.
958 * Only ELSes and FCP target ops should come through here.
959 * The locking is unfortunate, and a better scheme is being sought.
963 if (fh
->fh_type
>= FC_FC4_PROV_SIZE
)
965 prov
= rcu_dereference(fc_passive_prov
[fh
->fh_type
]);
966 if (!prov
|| !try_module_get(prov
->module
))
969 prov
->recv(lport
, fp
);
970 module_put(prov
->module
);
974 FC_LPORT_DBG(lport
, "dropping unexpected frame type %x\n", fh
->fh_type
);
979 EXPORT_SYMBOL(fc_lport_recv
);
982 * fc_lport_reset() - Reset a local port
983 * @lport: The local port which should be reset
985 * Locking Note: This functions should not be called with the
988 int fc_lport_reset(struct fc_lport
*lport
)
990 cancel_delayed_work_sync(&lport
->retry_work
);
991 mutex_lock(&lport
->lp_mutex
);
992 fc_lport_enter_reset(lport
);
993 mutex_unlock(&lport
->lp_mutex
);
996 EXPORT_SYMBOL(fc_lport_reset
);
999 * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
1000 * @lport: The local port to be reset
1002 static void fc_lport_reset_locked(struct fc_lport
*lport
)
1004 lockdep_assert_held(&lport
->lp_mutex
);
1006 if (lport
->dns_rdata
) {
1007 fc_rport_logoff(lport
->dns_rdata
);
1008 lport
->dns_rdata
= NULL
;
1011 if (lport
->ptp_rdata
) {
1012 fc_rport_logoff(lport
->ptp_rdata
);
1013 kref_put(&lport
->ptp_rdata
->kref
, fc_rport_destroy
);
1014 lport
->ptp_rdata
= NULL
;
1017 lport
->tt
.disc_stop(lport
);
1019 lport
->tt
.exch_mgr_reset(lport
, 0, 0);
1020 fc_host_fabric_name(lport
->host
) = 0;
1022 if (lport
->port_id
&& (!lport
->point_to_multipoint
|| !lport
->link_up
))
1023 fc_lport_set_port_id(lport
, 0, NULL
);
1027 * fc_lport_enter_reset() - Reset the local port
1028 * @lport: The local port to be reset
1030 static void fc_lport_enter_reset(struct fc_lport
*lport
)
1032 lockdep_assert_held(&lport
->lp_mutex
);
1034 FC_LPORT_DBG(lport
, "Entered RESET state from %s state\n",
1035 fc_lport_state(lport
));
1037 if (lport
->state
== LPORT_ST_DISABLED
|| lport
->state
== LPORT_ST_LOGO
)
1042 fc_vport_set_state(lport
->vport
, FC_VPORT_INITIALIZING
);
1044 fc_vport_set_state(lport
->vport
, FC_VPORT_LINKDOWN
);
1046 fc_lport_state_enter(lport
, LPORT_ST_RESET
);
1047 fc_host_post_event(lport
->host
, fc_get_event_number(),
1048 FCH_EVT_LIPRESET
, 0);
1049 fc_vports_linkchange(lport
);
1050 fc_lport_reset_locked(lport
);
1052 fc_lport_enter_flogi(lport
);
1056 * fc_lport_enter_disabled() - Disable the local port
1057 * @lport: The local port to be reset
1059 static void fc_lport_enter_disabled(struct fc_lport
*lport
)
1061 lockdep_assert_held(&lport
->lp_mutex
);
1063 FC_LPORT_DBG(lport
, "Entered disabled state from %s state\n",
1064 fc_lport_state(lport
));
1066 fc_lport_state_enter(lport
, LPORT_ST_DISABLED
);
1067 fc_vports_linkchange(lport
);
1068 fc_lport_reset_locked(lport
);
1072 * fc_lport_error() - Handler for any errors
1073 * @lport: The local port that the error was on
1074 * @fp: The error code encoded in a frame pointer
1076 * If the error was caused by a resource allocation failure
1077 * then wait for half a second and retry, otherwise retry
1078 * after the e_d_tov time.
1080 static void fc_lport_error(struct fc_lport
*lport
, struct fc_frame
*fp
)
1082 unsigned long delay
= 0;
1083 FC_LPORT_DBG(lport
, "Error %ld in state %s, retries %d\n",
1084 IS_ERR(fp
) ? -PTR_ERR(fp
) : 0, fc_lport_state(lport
),
1085 lport
->retry_count
);
1087 if (PTR_ERR(fp
) == -FC_EX_CLOSED
)
1091 * Memory allocation failure, or the exchange timed out
1092 * or we received LS_RJT.
1095 if (lport
->retry_count
< lport
->max_retry_count
) {
1096 lport
->retry_count
++;
1098 delay
= msecs_to_jiffies(500);
1100 delay
= msecs_to_jiffies(lport
->e_d_tov
);
1102 schedule_delayed_work(&lport
->retry_work
, delay
);
1104 fc_lport_enter_reset(lport
);
1108 * fc_lport_ns_resp() - Handle response to a name server
1109 * registration exchange
1110 * @sp: current sequence in exchange
1111 * @fp: response frame
1112 * @lp_arg: Fibre Channel host port instance
1114 * Locking Note: This function will be called without the lport lock
1115 * held, but it will lock, call an _enter_* function or fc_lport_error()
1116 * and then unlock the lport.
1118 static void fc_lport_ns_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1121 struct fc_lport
*lport
= lp_arg
;
1122 struct fc_frame_header
*fh
;
1123 struct fc_ct_hdr
*ct
;
1125 FC_LPORT_DBG(lport
, "Received a ns %s\n", fc_els_resp_type(fp
));
1127 if (fp
== ERR_PTR(-FC_EX_CLOSED
))
1130 mutex_lock(&lport
->lp_mutex
);
1132 if (lport
->state
< LPORT_ST_RNN_ID
|| lport
->state
> LPORT_ST_RFF_ID
) {
1133 FC_LPORT_DBG(lport
, "Received a name server response, "
1134 "but in state %s\n", fc_lport_state(lport
));
1141 fc_lport_error(lport
, fp
);
1145 fh
= fc_frame_header_get(fp
);
1146 ct
= fc_frame_payload_get(fp
, sizeof(*ct
));
1148 if (fh
&& ct
&& fh
->fh_type
== FC_TYPE_CT
&&
1149 ct
->ct_fs_type
== FC_FST_DIR
&&
1150 ct
->ct_fs_subtype
== FC_NS_SUBTYPE
&&
1151 ntohs(ct
->ct_cmd
) == FC_FS_ACC
)
1152 switch (lport
->state
) {
1153 case LPORT_ST_RNN_ID
:
1154 fc_lport_enter_ns(lport
, LPORT_ST_RSNN_NN
);
1156 case LPORT_ST_RSNN_NN
:
1157 fc_lport_enter_ns(lport
, LPORT_ST_RSPN_ID
);
1159 case LPORT_ST_RSPN_ID
:
1160 fc_lport_enter_ns(lport
, LPORT_ST_RFT_ID
);
1162 case LPORT_ST_RFT_ID
:
1163 fc_lport_enter_ns(lport
, LPORT_ST_RFF_ID
);
1165 case LPORT_ST_RFF_ID
:
1166 if (lport
->fdmi_enabled
)
1167 fc_lport_enter_fdmi(lport
);
1169 fc_lport_enter_scr(lport
);
1172 /* should have already been caught by state checks */
1176 fc_lport_error(lport
, fp
);
1180 mutex_unlock(&lport
->lp_mutex
);
1184 * fc_lport_ms_resp() - Handle response to a management server
1186 * @sp: current sequence in exchange
1187 * @fp: response frame
1188 * @lp_arg: Fibre Channel host port instance
1190 * Locking Note: This function will be called without the lport lock
1191 * held, but it will lock, call an _enter_* function or fc_lport_error()
1192 * and then unlock the lport.
1194 static void fc_lport_ms_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1197 struct fc_lport
*lport
= lp_arg
;
1198 struct fc_frame_header
*fh
;
1199 struct fc_ct_hdr
*ct
;
1201 FC_LPORT_DBG(lport
, "Received a ms %s\n", fc_els_resp_type(fp
));
1203 if (fp
== ERR_PTR(-FC_EX_CLOSED
))
1206 mutex_lock(&lport
->lp_mutex
);
1208 if (lport
->state
< LPORT_ST_RHBA
|| lport
->state
> LPORT_ST_DPRT
) {
1209 FC_LPORT_DBG(lport
, "Received a management server response, "
1210 "but in state %s\n", fc_lport_state(lport
));
1217 fc_lport_error(lport
, fp
);
1221 fh
= fc_frame_header_get(fp
);
1222 ct
= fc_frame_payload_get(fp
, sizeof(*ct
));
1224 if (fh
&& ct
&& fh
->fh_type
== FC_TYPE_CT
&&
1225 ct
->ct_fs_type
== FC_FST_MGMT
&&
1226 ct
->ct_fs_subtype
== FC_FDMI_SUBTYPE
) {
1227 FC_LPORT_DBG(lport
, "Received a management server response, "
1228 "reason=%d explain=%d\n",
1232 switch (lport
->state
) {
1234 if (ntohs(ct
->ct_cmd
) == FC_FS_ACC
)
1235 fc_lport_enter_ms(lport
, LPORT_ST_RPA
);
1236 else /* Error Skip RPA */
1237 fc_lport_enter_scr(lport
);
1240 fc_lport_enter_scr(lport
);
1243 fc_lport_enter_ms(lport
, LPORT_ST_RHBA
);
1246 fc_lport_enter_ms(lport
, LPORT_ST_DPRT
);
1249 /* should have already been caught by state checks */
1253 /* Invalid Frame? */
1254 fc_lport_error(lport
, fp
);
1259 mutex_unlock(&lport
->lp_mutex
);
1263 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
1264 * @sp: current sequence in SCR exchange
1265 * @fp: response frame
1266 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1268 * Locking Note: This function will be called without the lport lock
1269 * held, but it will lock, call an _enter_* function or fc_lport_error
1270 * and then unlock the lport.
1272 static void fc_lport_scr_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1275 struct fc_lport
*lport
= lp_arg
;
1278 FC_LPORT_DBG(lport
, "Received a SCR %s\n", fc_els_resp_type(fp
));
1280 if (fp
== ERR_PTR(-FC_EX_CLOSED
))
1283 mutex_lock(&lport
->lp_mutex
);
1285 if (lport
->state
!= LPORT_ST_SCR
) {
1286 FC_LPORT_DBG(lport
, "Received a SCR response, but in state "
1287 "%s\n", fc_lport_state(lport
));
1294 fc_lport_error(lport
, fp
);
1298 op
= fc_frame_payload_op(fp
);
1299 if (op
== ELS_LS_ACC
)
1300 fc_lport_enter_ready(lport
);
1302 fc_lport_error(lport
, fp
);
1307 mutex_unlock(&lport
->lp_mutex
);
1311 * fc_lport_enter_scr() - Send a SCR (State Change Register) request
1312 * @lport: The local port to register for state changes
1314 static void fc_lport_enter_scr(struct fc_lport
*lport
)
1316 struct fc_frame
*fp
;
1318 lockdep_assert_held(&lport
->lp_mutex
);
1320 FC_LPORT_DBG(lport
, "Entered SCR state from %s state\n",
1321 fc_lport_state(lport
));
1323 fc_lport_state_enter(lport
, LPORT_ST_SCR
);
1325 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_scr
));
1327 fc_lport_error(lport
, fp
);
1331 if (!lport
->tt
.elsct_send(lport
, FC_FID_FCTRL
, fp
, ELS_SCR
,
1332 fc_lport_scr_resp
, lport
,
1333 2 * lport
->r_a_tov
))
1334 fc_lport_error(lport
, NULL
);
1338 * fc_lport_enter_ns() - register some object with the name server
1339 * @lport: Fibre Channel local port to register
1341 static void fc_lport_enter_ns(struct fc_lport
*lport
, enum fc_lport_state state
)
1343 struct fc_frame
*fp
;
1345 int size
= sizeof(struct fc_ct_hdr
);
1348 lockdep_assert_held(&lport
->lp_mutex
);
1350 FC_LPORT_DBG(lport
, "Entered %s state from %s state\n",
1351 fc_lport_state_names
[state
],
1352 fc_lport_state(lport
));
1354 fc_lport_state_enter(lport
, state
);
1357 case LPORT_ST_RNN_ID
:
1359 size
+= sizeof(struct fc_ns_rn_id
);
1361 case LPORT_ST_RSNN_NN
:
1362 len
= strnlen(fc_host_symbolic_name(lport
->host
), 255);
1363 /* if there is no symbolic name, skip to RFT_ID */
1365 return fc_lport_enter_ns(lport
, LPORT_ST_RFT_ID
);
1366 cmd
= FC_NS_RSNN_NN
;
1367 size
+= sizeof(struct fc_ns_rsnn
) + len
;
1369 case LPORT_ST_RSPN_ID
:
1370 len
= strnlen(fc_host_symbolic_name(lport
->host
), 255);
1371 /* if there is no symbolic name, skip to RFT_ID */
1373 return fc_lport_enter_ns(lport
, LPORT_ST_RFT_ID
);
1374 cmd
= FC_NS_RSPN_ID
;
1375 size
+= sizeof(struct fc_ns_rspn
) + len
;
1377 case LPORT_ST_RFT_ID
:
1379 size
+= sizeof(struct fc_ns_rft
);
1381 case LPORT_ST_RFF_ID
:
1383 size
+= sizeof(struct fc_ns_rff_id
);
1386 fc_lport_error(lport
, NULL
);
1390 fp
= fc_frame_alloc(lport
, size
);
1392 fc_lport_error(lport
, fp
);
1396 if (!lport
->tt
.elsct_send(lport
, FC_FID_DIR_SERV
, fp
, cmd
,
1398 lport
, 3 * lport
->r_a_tov
))
1399 fc_lport_error(lport
, fp
);
1402 static struct fc_rport_operations fc_lport_rport_ops
= {
1403 .event_callback
= fc_lport_rport_callback
,
1407 * fc_rport_enter_dns() - Create a fc_rport for the name server
1408 * @lport: The local port requesting a remote port for the name server
1410 static void fc_lport_enter_dns(struct fc_lport
*lport
)
1412 struct fc_rport_priv
*rdata
;
1414 lockdep_assert_held(&lport
->lp_mutex
);
1416 FC_LPORT_DBG(lport
, "Entered DNS state from %s state\n",
1417 fc_lport_state(lport
));
1419 fc_lport_state_enter(lport
, LPORT_ST_DNS
);
1421 mutex_lock(&lport
->disc
.disc_mutex
);
1422 rdata
= fc_rport_create(lport
, FC_FID_DIR_SERV
);
1423 mutex_unlock(&lport
->disc
.disc_mutex
);
1427 rdata
->ops
= &fc_lport_rport_ops
;
1428 fc_rport_login(rdata
);
1432 fc_lport_error(lport
, NULL
);
1436 * fc_lport_enter_ms() - management server commands
1437 * @lport: Fibre Channel local port to register
1439 static void fc_lport_enter_ms(struct fc_lport
*lport
, enum fc_lport_state state
)
1441 struct fc_frame
*fp
;
1442 enum fc_fdmi_req cmd
;
1443 int size
= sizeof(struct fc_ct_hdr
);
1447 lockdep_assert_held(&lport
->lp_mutex
);
1449 FC_LPORT_DBG(lport
, "Entered %s state from %s state\n",
1450 fc_lport_state_names
[state
],
1451 fc_lport_state(lport
));
1453 fc_lport_state_enter(lport
, state
);
1458 /* Number of HBA Attributes */
1460 len
= sizeof(struct fc_fdmi_rhba
);
1461 len
-= sizeof(struct fc_fdmi_attr_entry
);
1462 len
+= (numattrs
* FC_FDMI_ATTR_ENTRY_HEADER_LEN
);
1463 len
+= FC_FDMI_HBA_ATTR_NODENAME_LEN
;
1464 len
+= FC_FDMI_HBA_ATTR_MANUFACTURER_LEN
;
1465 len
+= FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN
;
1466 len
+= FC_FDMI_HBA_ATTR_MODEL_LEN
;
1467 len
+= FC_FDMI_HBA_ATTR_MODELDESCR_LEN
;
1468 len
+= FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN
;
1469 len
+= FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN
;
1470 len
+= FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN
;
1471 len
+= FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN
;
1472 len
+= FC_FDMI_HBA_ATTR_OSNAMEVERSION_LEN
;
1478 /* Number of Port Attributes */
1480 len
= sizeof(struct fc_fdmi_rpa
);
1481 len
-= sizeof(struct fc_fdmi_attr_entry
);
1482 len
+= (numattrs
* FC_FDMI_ATTR_ENTRY_HEADER_LEN
);
1483 len
+= FC_FDMI_PORT_ATTR_FC4TYPES_LEN
;
1484 len
+= FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN
;
1485 len
+= FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN
;
1486 len
+= FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN
;
1487 len
+= FC_FDMI_PORT_ATTR_OSDEVICENAME_LEN
;
1488 len
+= FC_FDMI_PORT_ATTR_HOSTNAME_LEN
;
1494 len
= sizeof(struct fc_fdmi_dprt
);
1499 len
= sizeof(struct fc_fdmi_dhba
);
1503 fc_lport_error(lport
, NULL
);
1507 FC_LPORT_DBG(lport
, "Cmd=0x%x Len %d size %d\n",
1508 cmd
, (int)len
, size
);
1509 fp
= fc_frame_alloc(lport
, size
);
1511 fc_lport_error(lport
, fp
);
1515 if (!lport
->tt
.elsct_send(lport
, FC_FID_MGMT_SERV
, fp
, cmd
,
1517 lport
, 3 * lport
->r_a_tov
))
1518 fc_lport_error(lport
, fp
);
1522 * fc_rport_enter_fdmi() - Create a fc_rport for the management server
1523 * @lport: The local port requesting a remote port for the management server
1525 static void fc_lport_enter_fdmi(struct fc_lport
*lport
)
1527 struct fc_rport_priv
*rdata
;
1529 lockdep_assert_held(&lport
->lp_mutex
);
1531 FC_LPORT_DBG(lport
, "Entered FDMI state from %s state\n",
1532 fc_lport_state(lport
));
1534 fc_lport_state_enter(lport
, LPORT_ST_FDMI
);
1536 mutex_lock(&lport
->disc
.disc_mutex
);
1537 rdata
= fc_rport_create(lport
, FC_FID_MGMT_SERV
);
1538 mutex_unlock(&lport
->disc
.disc_mutex
);
1542 rdata
->ops
= &fc_lport_rport_ops
;
1543 fc_rport_login(rdata
);
1547 fc_lport_error(lport
, NULL
);
1551 * fc_lport_timeout() - Handler for the retry_work timer
1552 * @work: The work struct of the local port
1554 static void fc_lport_timeout(struct work_struct
*work
)
1556 struct fc_lport
*lport
=
1557 container_of(work
, struct fc_lport
,
1560 mutex_lock(&lport
->lp_mutex
);
1562 switch (lport
->state
) {
1563 case LPORT_ST_DISABLED
:
1565 case LPORT_ST_READY
:
1567 case LPORT_ST_RESET
:
1569 case LPORT_ST_FLOGI
:
1570 fc_lport_enter_flogi(lport
);
1573 fc_lport_enter_dns(lport
);
1575 case LPORT_ST_RNN_ID
:
1576 case LPORT_ST_RSNN_NN
:
1577 case LPORT_ST_RSPN_ID
:
1578 case LPORT_ST_RFT_ID
:
1579 case LPORT_ST_RFF_ID
:
1580 fc_lport_enter_ns(lport
, lport
->state
);
1583 fc_lport_enter_fdmi(lport
);
1589 FC_LPORT_DBG(lport
, "Skipping lport state %s to SCR\n",
1590 fc_lport_state(lport
));
1593 fc_lport_enter_scr(lport
);
1596 fc_lport_enter_logo(lport
);
1600 mutex_unlock(&lport
->lp_mutex
);
1604 * fc_lport_logo_resp() - Handle response to LOGO request
1605 * @sp: The sequence that the LOGO was on
1606 * @fp: The LOGO frame
1607 * @lp_arg: The lport port that received the LOGO request
1609 * Locking Note: This function will be called without the lport lock
1610 * held, but it will lock, call an _enter_* function or fc_lport_error()
1611 * and then unlock the lport.
1613 void fc_lport_logo_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1616 struct fc_lport
*lport
= lp_arg
;
1619 FC_LPORT_DBG(lport
, "Received a LOGO %s\n", fc_els_resp_type(fp
));
1621 if (fp
== ERR_PTR(-FC_EX_CLOSED
))
1624 mutex_lock(&lport
->lp_mutex
);
1626 if (lport
->state
!= LPORT_ST_LOGO
) {
1627 FC_LPORT_DBG(lport
, "Received a LOGO response, but in state "
1628 "%s\n", fc_lport_state(lport
));
1635 fc_lport_error(lport
, fp
);
1639 op
= fc_frame_payload_op(fp
);
1640 if (op
== ELS_LS_ACC
)
1641 fc_lport_enter_disabled(lport
);
1643 fc_lport_error(lport
, fp
);
1648 mutex_unlock(&lport
->lp_mutex
);
1650 EXPORT_SYMBOL(fc_lport_logo_resp
);
1653 * fc_rport_enter_logo() - Logout of the fabric
1654 * @lport: The local port to be logged out
1656 static void fc_lport_enter_logo(struct fc_lport
*lport
)
1658 struct fc_frame
*fp
;
1659 struct fc_els_logo
*logo
;
1661 lockdep_assert_held(&lport
->lp_mutex
);
1663 FC_LPORT_DBG(lport
, "Entered LOGO state from %s state\n",
1664 fc_lport_state(lport
));
1666 fc_lport_state_enter(lport
, LPORT_ST_LOGO
);
1667 fc_vports_linkchange(lport
);
1669 fp
= fc_frame_alloc(lport
, sizeof(*logo
));
1671 fc_lport_error(lport
, fp
);
1675 if (!lport
->tt
.elsct_send(lport
, FC_FID_FLOGI
, fp
, ELS_LOGO
,
1676 fc_lport_logo_resp
, lport
,
1677 2 * lport
->r_a_tov
))
1678 fc_lport_error(lport
, NULL
);
1682 * fc_lport_flogi_resp() - Handle response to FLOGI request
1683 * @sp: The sequence that the FLOGI was on
1684 * @fp: The FLOGI response frame
1685 * @lp_arg: The lport port that received the FLOGI response
1687 * Locking Note: This function will be called without the lport lock
1688 * held, but it will lock, call an _enter_* function or fc_lport_error()
1689 * and then unlock the lport.
1691 void fc_lport_flogi_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1694 struct fc_lport
*lport
= lp_arg
;
1695 struct fc_frame_header
*fh
;
1696 struct fc_els_flogi
*flp
;
1699 unsigned int r_a_tov
;
1700 unsigned int e_d_tov
;
1703 FC_LPORT_DBG(lport
, "Received a FLOGI %s\n", fc_els_resp_type(fp
));
1705 if (fp
== ERR_PTR(-FC_EX_CLOSED
))
1708 mutex_lock(&lport
->lp_mutex
);
1710 if (lport
->state
!= LPORT_ST_FLOGI
) {
1711 FC_LPORT_DBG(lport
, "Received a FLOGI response, but in state "
1712 "%s\n", fc_lport_state(lport
));
1719 fc_lport_error(lport
, fp
);
1723 fh
= fc_frame_header_get(fp
);
1724 did
= fc_frame_did(fp
);
1725 if (fh
->fh_r_ctl
!= FC_RCTL_ELS_REP
|| did
== 0 ||
1726 fc_frame_payload_op(fp
) != ELS_LS_ACC
) {
1727 FC_LPORT_DBG(lport
, "FLOGI not accepted or bad response\n");
1728 fc_lport_error(lport
, fp
);
1732 flp
= fc_frame_payload_get(fp
, sizeof(*flp
));
1734 FC_LPORT_DBG(lport
, "FLOGI bad response\n");
1735 fc_lport_error(lport
, fp
);
1739 mfs
= ntohs(flp
->fl_csp
.sp_bb_data
) &
1742 if (mfs
< FC_SP_MIN_MAX_PAYLOAD
|| mfs
> FC_SP_MAX_MAX_PAYLOAD
) {
1743 FC_LPORT_DBG(lport
, "FLOGI bad mfs:%hu response, "
1744 "lport->mfs:%hu\n", mfs
, lport
->mfs
);
1745 fc_lport_error(lport
, fp
);
1749 if (mfs
<= lport
->mfs
) {
1751 fc_host_maxframe_size(lport
->host
) = mfs
;
1754 csp_flags
= ntohs(flp
->fl_csp
.sp_features
);
1755 r_a_tov
= ntohl(flp
->fl_csp
.sp_r_a_tov
);
1756 e_d_tov
= ntohl(flp
->fl_csp
.sp_e_d_tov
);
1757 if (csp_flags
& FC_SP_FT_EDTR
)
1760 lport
->npiv_enabled
= !!(csp_flags
& FC_SP_FT_NPIV_ACC
);
1762 if ((csp_flags
& FC_SP_FT_FPORT
) == 0) {
1763 if (e_d_tov
> lport
->e_d_tov
)
1764 lport
->e_d_tov
= e_d_tov
;
1765 lport
->r_a_tov
= 2 * lport
->e_d_tov
;
1766 fc_lport_set_port_id(lport
, did
, fp
);
1767 printk(KERN_INFO
"host%d: libfc: "
1768 "Port (%6.6x) entered "
1769 "point-to-point mode\n",
1770 lport
->host
->host_no
, did
);
1771 fc_lport_ptp_setup(lport
, fc_frame_sid(fp
),
1777 if (e_d_tov
> lport
->e_d_tov
)
1778 lport
->e_d_tov
= e_d_tov
;
1779 if (r_a_tov
> lport
->r_a_tov
)
1780 lport
->r_a_tov
= r_a_tov
;
1781 fc_host_fabric_name(lport
->host
) =
1782 get_unaligned_be64(&flp
->fl_wwnn
);
1783 fc_lport_set_port_id(lport
, did
, fp
);
1784 fc_lport_enter_dns(lport
);
1790 mutex_unlock(&lport
->lp_mutex
);
1792 EXPORT_SYMBOL(fc_lport_flogi_resp
);
1795 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
1796 * @lport: Fibre Channel local port to be logged in to the fabric
1798 static void fc_lport_enter_flogi(struct fc_lport
*lport
)
1800 struct fc_frame
*fp
;
1802 lockdep_assert_held(&lport
->lp_mutex
);
1804 FC_LPORT_DBG(lport
, "Entered FLOGI state from %s state\n",
1805 fc_lport_state(lport
));
1807 fc_lport_state_enter(lport
, LPORT_ST_FLOGI
);
1809 if (lport
->point_to_multipoint
) {
1811 fc_lport_enter_ready(lport
);
1815 fp
= fc_frame_alloc(lport
, sizeof(struct fc_els_flogi
));
1817 return fc_lport_error(lport
, fp
);
1819 if (!lport
->tt
.elsct_send(lport
, FC_FID_FLOGI
, fp
,
1820 lport
->vport
? ELS_FDISC
: ELS_FLOGI
,
1821 fc_lport_flogi_resp
, lport
,
1822 lport
->vport
? 2 * lport
->r_a_tov
:
1824 fc_lport_error(lport
, NULL
);
1828 * fc_lport_config() - Configure a fc_lport
1829 * @lport: The local port to be configured
1831 int fc_lport_config(struct fc_lport
*lport
)
1833 INIT_DELAYED_WORK(&lport
->retry_work
, fc_lport_timeout
);
1834 mutex_init(&lport
->lp_mutex
);
1836 fc_lport_state_enter(lport
, LPORT_ST_DISABLED
);
1838 fc_lport_add_fc4_type(lport
, FC_TYPE_FCP
);
1839 fc_lport_add_fc4_type(lport
, FC_TYPE_CT
);
1840 fc_fc4_conf_lport_params(lport
, FC_TYPE_FCP
);
1844 EXPORT_SYMBOL(fc_lport_config
);
1847 * fc_lport_init() - Initialize the lport layer for a local port
1848 * @lport: The local port to initialize the exchange layer for
1850 int fc_lport_init(struct fc_lport
*lport
)
1852 fc_host_port_type(lport
->host
) = FC_PORTTYPE_NPORT
;
1853 fc_host_node_name(lport
->host
) = lport
->wwnn
;
1854 fc_host_port_name(lport
->host
) = lport
->wwpn
;
1855 fc_host_supported_classes(lport
->host
) = FC_COS_CLASS3
;
1856 memset(fc_host_supported_fc4s(lport
->host
), 0,
1857 sizeof(fc_host_supported_fc4s(lport
->host
)));
1858 fc_host_supported_fc4s(lport
->host
)[2] = 1;
1859 fc_host_supported_fc4s(lport
->host
)[7] = 1;
1861 /* This value is also unchanging */
1862 memset(fc_host_active_fc4s(lport
->host
), 0,
1863 sizeof(fc_host_active_fc4s(lport
->host
)));
1864 fc_host_active_fc4s(lport
->host
)[2] = 1;
1865 fc_host_active_fc4s(lport
->host
)[7] = 1;
1866 fc_host_maxframe_size(lport
->host
) = lport
->mfs
;
1867 fc_host_supported_speeds(lport
->host
) = 0;
1868 if (lport
->link_supported_speeds
& FC_PORTSPEED_1GBIT
)
1869 fc_host_supported_speeds(lport
->host
) |= FC_PORTSPEED_1GBIT
;
1870 if (lport
->link_supported_speeds
& FC_PORTSPEED_10GBIT
)
1871 fc_host_supported_speeds(lport
->host
) |= FC_PORTSPEED_10GBIT
;
1872 fc_fc4_add_lport(lport
);
1876 EXPORT_SYMBOL(fc_lport_init
);
1879 * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
1880 * @sp: The sequence for the FC Passthrough response
1881 * @fp: The response frame
1882 * @info_arg: The BSG info that the response is for
1884 static void fc_lport_bsg_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
1887 struct fc_bsg_info
*info
= info_arg
;
1888 struct bsg_job
*job
= info
->job
;
1889 struct fc_bsg_reply
*bsg_reply
= job
->reply
;
1890 struct fc_lport
*lport
= info
->lport
;
1891 struct fc_frame_header
*fh
;
1896 bsg_reply
->result
= (PTR_ERR(fp
) == -FC_EX_CLOSED
) ?
1897 -ECONNABORTED
: -ETIMEDOUT
;
1898 job
->reply_len
= sizeof(uint32_t);
1899 bsg_job_done(job
, bsg_reply
->result
,
1900 bsg_reply
->reply_payload_rcv_len
);
1905 mutex_lock(&lport
->lp_mutex
);
1906 fh
= fc_frame_header_get(fp
);
1907 len
= fr_len(fp
) - sizeof(*fh
);
1908 buf
= fc_frame_payload_get(fp
, 0);
1910 if (fr_sof(fp
) == FC_SOF_I3
&& !ntohs(fh
->fh_seq_cnt
)) {
1911 /* Get the response code from the first frame payload */
1912 unsigned short cmd
= (info
->rsp_code
== FC_FS_ACC
) ?
1913 ntohs(((struct fc_ct_hdr
*)buf
)->ct_cmd
) :
1914 (unsigned short)fc_frame_payload_op(fp
);
1916 /* Save the reply status of the job */
1917 bsg_reply
->reply_data
.ctels_reply
.status
=
1918 (cmd
== info
->rsp_code
) ?
1919 FC_CTELS_STATUS_OK
: FC_CTELS_STATUS_REJECT
;
1922 bsg_reply
->reply_payload_rcv_len
+=
1923 fc_copy_buffer_to_sglist(buf
, len
, info
->sg
, &info
->nents
,
1924 &info
->offset
, NULL
);
1926 if (fr_eof(fp
) == FC_EOF_T
&&
1927 (ntoh24(fh
->fh_f_ctl
) & (FC_FC_LAST_SEQ
| FC_FC_END_SEQ
)) ==
1928 (FC_FC_LAST_SEQ
| FC_FC_END_SEQ
)) {
1929 if (bsg_reply
->reply_payload_rcv_len
>
1930 job
->reply_payload
.payload_len
)
1931 bsg_reply
->reply_payload_rcv_len
=
1932 job
->reply_payload
.payload_len
;
1933 bsg_reply
->result
= 0;
1934 bsg_job_done(job
, bsg_reply
->result
,
1935 bsg_reply
->reply_payload_rcv_len
);
1939 mutex_unlock(&lport
->lp_mutex
);
1943 * fc_lport_els_request() - Send ELS passthrough request
1944 * @job: The BSG Passthrough job
1945 * @lport: The local port sending the request
1946 * @did: The destination port id
1948 static int fc_lport_els_request(struct bsg_job
*job
,
1949 struct fc_lport
*lport
,
1952 struct fc_bsg_info
*info
;
1953 struct fc_frame
*fp
;
1954 struct fc_frame_header
*fh
;
1958 lockdep_assert_held(&lport
->lp_mutex
);
1960 fp
= fc_frame_alloc(lport
, job
->request_payload
.payload_len
);
1964 len
= job
->request_payload
.payload_len
;
1965 pp
= fc_frame_payload_get(fp
, len
);
1967 sg_copy_to_buffer(job
->request_payload
.sg_list
,
1968 job
->request_payload
.sg_cnt
,
1971 fh
= fc_frame_header_get(fp
);
1972 fh
->fh_r_ctl
= FC_RCTL_ELS_REQ
;
1973 hton24(fh
->fh_d_id
, did
);
1974 hton24(fh
->fh_s_id
, lport
->port_id
);
1975 fh
->fh_type
= FC_TYPE_ELS
;
1976 hton24(fh
->fh_f_ctl
, FC_FCTL_REQ
);
1979 fh
->fh_parm_offset
= 0;
1981 info
= kzalloc(sizeof(struct fc_bsg_info
), GFP_KERNEL
);
1988 info
->lport
= lport
;
1989 info
->rsp_code
= ELS_LS_ACC
;
1990 info
->nents
= job
->reply_payload
.sg_cnt
;
1991 info
->sg
= job
->reply_payload
.sg_list
;
1993 if (!fc_exch_seq_send(lport
, fp
, fc_lport_bsg_resp
,
2002 * fc_lport_ct_request() - Send CT Passthrough request
2003 * @job: The BSG Passthrough job
2004 * @lport: The local port sending the request
2005 * @did: The destination FC-ID
2006 * @tov: The timeout period to wait for the response
2008 static int fc_lport_ct_request(struct bsg_job
*job
,
2009 struct fc_lport
*lport
, u32 did
, u32 tov
)
2011 struct fc_bsg_info
*info
;
2012 struct fc_frame
*fp
;
2013 struct fc_frame_header
*fh
;
2014 struct fc_ct_req
*ct
;
2017 lockdep_assert_held(&lport
->lp_mutex
);
2019 fp
= fc_frame_alloc(lport
, sizeof(struct fc_ct_hdr
) +
2020 job
->request_payload
.payload_len
);
2024 len
= job
->request_payload
.payload_len
;
2025 ct
= fc_frame_payload_get(fp
, len
);
2027 sg_copy_to_buffer(job
->request_payload
.sg_list
,
2028 job
->request_payload
.sg_cnt
,
2031 fh
= fc_frame_header_get(fp
);
2032 fh
->fh_r_ctl
= FC_RCTL_DD_UNSOL_CTL
;
2033 hton24(fh
->fh_d_id
, did
);
2034 hton24(fh
->fh_s_id
, lport
->port_id
);
2035 fh
->fh_type
= FC_TYPE_CT
;
2036 hton24(fh
->fh_f_ctl
, FC_FCTL_REQ
);
2039 fh
->fh_parm_offset
= 0;
2041 info
= kzalloc(sizeof(struct fc_bsg_info
), GFP_KERNEL
);
2048 info
->lport
= lport
;
2049 info
->rsp_code
= FC_FS_ACC
;
2050 info
->nents
= job
->reply_payload
.sg_cnt
;
2051 info
->sg
= job
->reply_payload
.sg_list
;
2053 if (!fc_exch_seq_send(lport
, fp
, fc_lport_bsg_resp
,
2062 * fc_lport_bsg_request() - The common entry point for sending
2063 * FC Passthrough requests
2064 * @job: The BSG passthrough job
2066 int fc_lport_bsg_request(struct bsg_job
*job
)
2068 struct fc_bsg_request
*bsg_request
= job
->request
;
2069 struct fc_bsg_reply
*bsg_reply
= job
->reply
;
2070 struct Scsi_Host
*shost
= fc_bsg_to_shost(job
);
2071 struct fc_lport
*lport
= shost_priv(shost
);
2072 struct fc_rport
*rport
;
2073 struct fc_rport_priv
*rdata
;
2077 bsg_reply
->reply_payload_rcv_len
= 0;
2079 mutex_lock(&lport
->lp_mutex
);
2081 switch (bsg_request
->msgcode
) {
2082 case FC_BSG_RPT_ELS
:
2083 rport
= fc_bsg_to_rport(job
);
2087 rdata
= rport
->dd_data
;
2088 rc
= fc_lport_els_request(job
, lport
, rport
->port_id
,
2093 rport
= fc_bsg_to_rport(job
);
2097 rdata
= rport
->dd_data
;
2098 rc
= fc_lport_ct_request(job
, lport
, rport
->port_id
,
2103 did
= ntoh24(bsg_request
->rqst_data
.h_ct
.port_id
);
2104 if (did
== FC_FID_DIR_SERV
) {
2105 rdata
= lport
->dns_rdata
;
2108 tov
= rdata
->e_d_tov
;
2110 rdata
= fc_rport_lookup(lport
, did
);
2113 tov
= rdata
->e_d_tov
;
2114 kref_put(&rdata
->kref
, fc_rport_destroy
);
2117 rc
= fc_lport_ct_request(job
, lport
, did
, tov
);
2120 case FC_BSG_HST_ELS_NOLOGIN
:
2121 did
= ntoh24(bsg_request
->rqst_data
.h_els
.port_id
);
2122 rc
= fc_lport_els_request(job
, lport
, did
, lport
->e_d_tov
);
2126 mutex_unlock(&lport
->lp_mutex
);
2129 EXPORT_SYMBOL(fc_lport_bsg_request
);