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 #include <linux/timer.h>
25 #include <linux/percpu.h>
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_fc.h>
30 #include <scsi/fc/fc_fcp.h>
31 #include <scsi/fc/fc_ns.h>
32 #include <scsi/fc/fc_els.h>
33 #include <scsi/fc/fc_gs.h>
35 #include <scsi/fc_frame.h>
40 #define FC_NO_ERR 0 /* no error */
41 #define FC_EX_TIMEOUT 1 /* Exchange timeout */
42 #define FC_EX_CLOSED 2 /* Exchange closed */
44 /* some helpful macros */
46 #define ntohll(x) be64_to_cpu(x)
47 #define htonll(x) cpu_to_be64(x)
49 #define ntoh24(p) (((p)[0] << 16) | ((p)[1] << 8) | ((p)[2]))
51 #define hton24(p, v) do { \
52 p[0] = (((v) >> 16) & 0xFF); \
53 p[1] = (((v) >> 8) & 0xFF); \
54 p[2] = ((v) & 0xFF); \
61 LPORT_ST_DISABLED = 0,
79 RPORT_ST_INIT, /* initialized */
80 RPORT_ST_PLOGI, /* waiting for PLOGI completion */
81 RPORT_ST_PRLI, /* waiting for PRLI completion */
82 RPORT_ST_RTV, /* waiting for RTV completion */
83 RPORT_ST_READY, /* ready for use */
84 RPORT_ST_LOGO, /* port logout sent */
85 RPORT_ST_ADISC, /* Discover Address sent */
86 RPORT_ST_DELETE, /* port being deleted */
87 RPORT_ST_RESTART, /* remote port being deleted and will restart */
91 * struct fc_disc_port - temporary discovery port to hold rport identifiers
92 * @lp: Fibre Channel host port instance
93 * @peers: Node for list management during discovery and RSCN processing
94 * @rport_work: Work struct for starting the rport state machine
95 * @port_id: Port ID of the discovered port
99 struct list_head peers;
100 struct work_struct rport_work;
104 enum fc_rport_event {
112 struct fc_rport_priv;
114 struct fc_rport_operations {
115 void (*event_callback)(struct fc_lport *, struct fc_rport_priv *,
116 enum fc_rport_event);
120 * struct fc_rport_libfc_priv - libfc internal information about a remote port
121 * @local_port: Fibre Channel host port instance
122 * @rp_state: indicates READY for I/O or DELETE when blocked.
123 * @flags: REC and RETRY supported flags
124 * @e_d_tov: error detect timeout value (in msec)
125 * @r_a_tov: resource allocation timeout value (in msec)
127 struct fc_rport_libfc_priv {
128 struct fc_lport *local_port;
129 enum fc_rport_state rp_state;
131 #define FC_RP_FLAGS_REC_SUPPORTED (1 << 0)
132 #define FC_RP_FLAGS_RETRY (1 << 1)
133 unsigned int e_d_tov;
134 unsigned int r_a_tov;
138 * struct fc_rport_priv - libfc rport and discovery info about a remote port
139 * @local_port: Fibre Channel host port instance
140 * @rport: transport remote port
141 * @kref: reference counter
142 * @rp_state: state tracks progress of PLOGI, PRLI, and RTV exchanges
143 * @ids: remote port identifiers and roles
144 * @flags: REC and RETRY supported flags
145 * @max_seq: maximum number of concurrent sequences
146 * @disc_id: discovery identifier
147 * @maxframe_size: maximum frame size
148 * @retries: retry count in current state
149 * @e_d_tov: error detect timeout value (in msec)
150 * @r_a_tov: resource allocation timeout value (in msec)
151 * @rp_mutex: mutex protects rport
153 * @event_callback: Callback for rport READY, FAILED or LOGO
155 struct fc_rport_priv {
156 struct fc_lport *local_port;
157 struct fc_rport *rport;
159 enum fc_rport_state rp_state;
160 struct fc_rport_identifiers ids;
165 unsigned int retries;
166 unsigned int e_d_tov;
167 unsigned int r_a_tov;
168 struct mutex rp_mutex;
169 struct delayed_work retry_work;
170 enum fc_rport_event event;
171 struct fc_rport_operations *ops;
172 struct list_head peers;
173 struct work_struct event_work;
174 u32 supported_classes;
178 * fcoe stats structure
180 struct fcoe_dev_stats {
181 u64 SecondsSinceLastReset;
188 u64 LinkFailureCount;
189 u64 LossOfSignalCount;
190 u64 InvalidTxWordCount;
200 * els data is used for passing ELS respone specific
201 * data to send ELS response mainly using infomation
202 * in exchange and sequence in EM layer.
204 struct fc_seq_els_data {
206 enum fc_els_rjt_reason reason;
207 enum fc_els_rjt_explan explan;
211 * FCP request structure, one for each scsi cmd request
217 struct fc_lport *lp; /* handle to hba struct */
218 u16 state; /* scsi_pkt state state */
219 u16 tgt_flags; /* target flags */
220 atomic_t ref_cnt; /* fcp pkt ref count */
221 spinlock_t scsi_pkt_lock; /* Must be taken before the host lock
222 * if both are held at the same time */
224 * SCSI I/O related stuff
226 struct scsi_cmnd *cmd; /* scsi command pointer. set/clear
228 struct list_head list; /* tracks queued commands. access under
231 * timeout related stuff
233 struct timer_list timer; /* command timer */
234 struct completion tm_done;
236 unsigned long start_time; /* start jiffie */
237 unsigned long end_time; /* end jiffie */
238 unsigned long last_pkt_time; /* jiffies of last frame received */
241 * scsi cmd and data transfer information
245 * transport related veriables
247 struct fcp_cmnd cdb_cmd;
249 u16 xfer_ddp; /* this xfer is ddped */
250 u32 xfer_contig_end; /* offset of end of contiguous xfer */
251 u16 max_payload; /* max payload size in bytes */
254 * scsi/fcp return status
256 u32 io_status; /* SCSI result upper 24 bits */
258 u8 status_code; /* FCP I/O status */
259 /* bit 3 Underrun bit 2: overrun */
261 u32 req_flags; /* bit 0: read bit:1 write */
262 u32 scsi_resid; /* residule length */
264 struct fc_rport *rport; /* remote port pointer */
265 struct fc_seq *seq_ptr; /* current sequence pointer */
269 u8 recov_retry; /* count of recovery retries */
270 struct fc_seq *recov_seq; /* sequence for REC or SRR */
273 * FC_FCP HELPER FUNCTIONS
274 *****************************/
275 static inline bool fc_fcp_is_read(const struct fc_fcp_pkt *fsp)
278 return fsp->cmd->sc_data_direction == DMA_FROM_DEVICE;
283 * Structure and function definitions for managing Fibre Channel Exchanges
286 * fc_exch holds state for one exchange and links to its active sequence.
288 * fc_seq holds the state for an individual sequence.
292 struct fc_exch_mgr_anchor;
293 extern u16 fc_cpu_mask; /* cpu mask for possible cpus */
300 u16 ssb_stat; /* status flags for sequence status block */
301 u16 cnt; /* frames sent so far on sequence */
302 u32 rec_data; /* FC-4 value for REC */
305 #define FC_EX_DONE (1 << 0) /* ep is completed */
306 #define FC_EX_RST_CLEANUP (1 << 1) /* reset is forcing completion */
311 * Locking notes: The ex_lock protects following items:
312 * state, esb_stat, f_ctl, seq.ssb_stat
314 * sequence allocation
317 struct fc_exch_mgr *em; /* exchange manager */
318 struct fc_exch_pool *pool; /* per cpu exches pool */
319 u32 state; /* internal driver state */
320 u16 xid; /* our exchange ID */
321 struct list_head ex_list; /* free or busy list linkage */
322 spinlock_t ex_lock; /* lock covering exchange state */
323 atomic_t ex_refcnt; /* reference counter */
324 struct delayed_work timeout_work; /* timer for upper level protocols */
325 struct fc_lport *lp; /* fc device instance */
326 u16 oxid; /* originator's exchange ID */
327 u16 rxid; /* responder's exchange ID */
328 u32 oid; /* originator's FCID */
329 u32 sid; /* source FCID */
330 u32 did; /* destination FCID */
331 u32 esb_stat; /* exchange status for ESB */
332 u32 r_a_tov; /* r_a_tov from rport (msec) */
333 u8 seq_id; /* next sequence ID to use */
334 u32 f_ctl; /* F_CTL flags for sequences */
335 u8 fh_type; /* frame type */
336 enum fc_class class; /* class of service */
337 struct fc_seq seq; /* single sequence */
339 * Handler for responses to this current exchange.
341 void (*resp)(struct fc_seq *, struct fc_frame *, void *);
342 void (*destructor)(struct fc_seq *, void *);
344 * arg is passed as void pointer to exchange
345 * resp and destructor handlers
349 #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
351 struct libfc_function_template {
354 * Interface to send a FC frame
358 int (*frame_send)(struct fc_lport *lp, struct fc_frame *fp);
361 * Interface to send ELS/CT frames
365 struct fc_seq *(*elsct_send)(struct fc_lport *lport,
369 void (*resp)(struct fc_seq *,
372 void *arg, u32 timer_msec);
375 * Send the FC frame payload using a new exchange and sequence.
377 * The frame pointer with some of the header's fields must be
378 * filled before calling exch_seq_send(), those fields are,
385 * - parameter or relative offset
387 * The exchange response handler is set in this routine to resp()
388 * function pointer. It can be called in two scenarios: if a timeout
389 * occurs or if a response frame is received for the exchange. The
390 * fc_frame pointer in response handler will also indicate timeout
391 * as error using IS_ERR related macros.
393 * The exchange destructor handler is also set in this routine.
394 * The destructor handler is invoked by EM layer when exchange
395 * is about to free, this can be used by caller to free its
396 * resources along with exchange free.
398 * The arg is passed back to resp and destructor handler.
400 * The timeout value (in msec) for an exchange is set if non zero
401 * timer_msec argument is specified. The timer is canceled when
402 * it fires or when the exchange is done. The exchange timeout handler
403 * is registered by EM layer.
407 struct fc_seq *(*exch_seq_send)(struct fc_lport *lp,
409 void (*resp)(struct fc_seq *sp,
412 void (*destructor)(struct fc_seq *sp,
414 void *arg, unsigned int timer_msec);
417 * Sets up the DDP context for a given exchange id on the given
418 * scatterlist if LLD supports DDP for large receive.
422 int (*ddp_setup)(struct fc_lport *lp, u16 xid,
423 struct scatterlist *sgl, unsigned int sgc);
425 * Completes the DDP transfer and returns the length of data DDPed
426 * for the given exchange id.
430 int (*ddp_done)(struct fc_lport *lp, u16 xid);
432 * Send a frame using an existing sequence and exchange.
436 int (*seq_send)(struct fc_lport *lp, struct fc_seq *sp,
437 struct fc_frame *fp);
440 * Send an ELS response using infomation from a previous
441 * exchange and sequence.
445 void (*seq_els_rsp_send)(struct fc_seq *sp, enum fc_els_cmd els_cmd,
446 struct fc_seq_els_data *els_data);
449 * Abort an exchange and sequence. Generally called because of a
450 * exchange timeout or an abort from the upper layer.
452 * A timer_msec can be specified for abort timeout, if non-zero
453 * timer_msec value is specified then exchange resp handler
454 * will be called with timeout error if no response to abort.
458 int (*seq_exch_abort)(const struct fc_seq *req_sp,
459 unsigned int timer_msec);
462 * Indicate that an exchange/sequence tuple is complete and the memory
463 * allocated for the related objects may be freed.
467 void (*exch_done)(struct fc_seq *sp);
470 * Start a new sequence on the same exchange/sequence tuple.
474 struct fc_seq *(*seq_start_next)(struct fc_seq *sp);
477 * Reset an exchange manager, completing all sequences and exchanges.
478 * If s_id is non-zero, reset only exchanges originating from that FID.
479 * If d_id is non-zero, reset only exchanges sending to that FID.
483 void (*exch_mgr_reset)(struct fc_lport *,
487 * Flush the rport work queue. Generally used before shutdown.
491 void (*rport_flush_queue)(void);
494 * Receive a frame for a local port.
498 void (*lport_recv)(struct fc_lport *lp, struct fc_seq *sp,
499 struct fc_frame *fp);
502 * Reset the local port.
506 int (*lport_reset)(struct fc_lport *);
509 * Create a remote port with a given port ID
513 struct fc_rport_priv *(*rport_create)(struct fc_lport *, u32);
516 * Initiates the RP state machine. It is called from the LP module.
517 * This function will issue the following commands to the N_Port
518 * identified by the FC ID provided.
526 int (*rport_login)(struct fc_rport_priv *);
529 * Logoff, and remove the rport from the transport if
530 * it had been added. This will send a LOGO to the target.
534 int (*rport_logoff)(struct fc_rport_priv *);
537 * Recieve a request from a remote port.
541 void (*rport_recv_req)(struct fc_seq *, struct fc_frame *,
545 * lookup an rport by it's port ID.
549 struct fc_rport_priv *(*rport_lookup)(const struct fc_lport *, u32);
552 * Destroy an rport after final kref_put().
553 * The argument is a pointer to the kref inside the fc_rport_priv.
555 void (*rport_destroy)(struct kref *);
558 * Send a fcp cmd from fsp pkt.
559 * Called with the SCSI host lock unlocked and irqs disabled.
561 * The resp handler is called when FCP_RSP received.
565 int (*fcp_cmd_send)(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
566 void (*resp)(struct fc_seq *, struct fc_frame *fp,
570 * Cleanup the FCP layer, used durring link down and reset
574 void (*fcp_cleanup)(struct fc_lport *lp);
577 * Abort all I/O on a local port
581 void (*fcp_abort_io)(struct fc_lport *lp);
584 * Receive a request for the discovery layer.
588 void (*disc_recv_req)(struct fc_seq *,
589 struct fc_frame *, struct fc_lport *);
592 * Start discovery for a local port.
596 void (*disc_start)(void (*disc_callback)(struct fc_lport *,
601 * Stop discovery for a given lport. This will remove
602 * all discovered rports
606 void (*disc_stop) (struct fc_lport *);
609 * Stop discovery for a given lport. This will block
610 * until all discovered rports are deleted from the
615 void (*disc_stop_final) (struct fc_lport *);
618 /* information used by the discovery layer */
620 unsigned char retry_count;
621 unsigned char pending;
622 unsigned char requested;
623 unsigned short seq_count;
624 unsigned char buf_len;
627 void (*disc_callback)(struct fc_lport *,
630 struct list_head rports;
631 struct fc_lport *lport;
632 struct mutex disc_mutex;
633 struct fc_gpn_ft_resp partial_buf; /* partial name buffer */
634 struct delayed_work disc_work;
638 struct list_head list;
641 struct Scsi_Host *host;
642 struct list_head ema_list;
643 struct fc_rport_priv *dns_rp;
644 struct fc_rport_priv *ptp_rp;
648 /* Operational Information */
649 struct libfc_function_template tt;
652 enum fc_lport_state state;
653 unsigned long boot_time;
655 struct fc_host_statistics host_stats;
656 struct fcoe_dev_stats *dev_stats;
663 u32 sg_supp:1; /* scatter gather supported */
664 u32 seq_offload:1; /* seq offload supported */
665 u32 crc_offload:1; /* crc offload supported */
666 u32 lro_enabled:1; /* large receive offload */
667 u32 mfs; /* max FC payload size */
668 unsigned int service_params;
669 unsigned int e_d_tov;
670 unsigned int r_a_tov;
672 u8 max_rport_retry_count;
674 u16 link_supported_speeds;
675 u16 lro_xid; /* max xid for fcoe lro */
676 unsigned int lso_max; /* max large send size */
677 struct fc_ns_fts fcts; /* FC-4 type masks */
678 struct fc_els_rnid_gen rnid_gen; /* RNID information */
681 struct mutex lp_mutex;
684 struct delayed_work retry_work;
688 * FC_LPORT HELPER FUNCTIONS
689 *****************************/
690 static inline int fc_lport_test_ready(struct fc_lport *lp)
692 return lp->state == LPORT_ST_READY;
695 static inline void fc_set_wwnn(struct fc_lport *lp, u64 wwnn)
700 static inline void fc_set_wwpn(struct fc_lport *lp, u64 wwnn)
705 static inline void fc_lport_state_enter(struct fc_lport *lp,
706 enum fc_lport_state state)
708 if (state != lp->state)
713 static inline int fc_lport_init_stats(struct fc_lport *lp)
715 /* allocate per cpu stats block */
716 lp->dev_stats = alloc_percpu(struct fcoe_dev_stats);
722 static inline void fc_lport_free_stats(struct fc_lport *lp)
724 free_percpu(lp->dev_stats);
727 static inline struct fcoe_dev_stats *fc_lport_get_stats(struct fc_lport *lp)
729 return per_cpu_ptr(lp->dev_stats, smp_processor_id());
732 static inline void *lport_priv(const struct fc_lport *lp)
734 return (void *)(lp + 1);
738 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
739 * @sht: ptr to the scsi host templ
740 * @priv_size: size of private data after fc_lport
742 * Returns: libfc lport
744 static inline struct fc_lport *
745 libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
747 struct fc_lport *lport;
748 struct Scsi_Host *shost;
750 shost = scsi_host_alloc(sht, sizeof(*lport) + priv_size);
753 lport = shost_priv(shost);
755 INIT_LIST_HEAD(&lport->ema_list);
761 *****************************/
762 int fc_lport_init(struct fc_lport *lp);
765 * Destroy the specified local port by finding and freeing all
766 * fc_rports associated with it and then by freeing the fc_lport
769 int fc_lport_destroy(struct fc_lport *lp);
772 * Logout the specified local port from the fabric
774 int fc_fabric_logoff(struct fc_lport *lp);
777 * Initiate the LP state machine. This handler will use fc_host_attr
778 * to store the FLOGI service parameters, so fc_host_attr must be
779 * initialized before calling this handler.
781 int fc_fabric_login(struct fc_lport *lp);
784 * The link is up for the given local port.
786 void fc_linkup(struct fc_lport *);
789 * Link is down for the given local port.
791 void fc_linkdown(struct fc_lport *);
794 * Configure the local port.
796 int fc_lport_config(struct fc_lport *);
799 * Reset the local port.
801 int fc_lport_reset(struct fc_lport *);
804 * Set the mfs or reset
806 int fc_set_mfs(struct fc_lport *lp, u32 mfs);
811 *****************************/
812 int fc_rport_init(struct fc_lport *lp);
813 void fc_rport_terminate_io(struct fc_rport *rp);
817 *****************************/
818 int fc_disc_init(struct fc_lport *lp);
823 *****************************/
825 * Initialize the SCSI block of libfc
827 int fc_fcp_init(struct fc_lport *);
830 * This section provides an API which allows direct interaction
831 * with the SCSI-ml. Each of these functions satisfies a function
832 * pointer defined in Scsi_Host and therefore is always called
833 * directly from the SCSI-ml.
835 int fc_queuecommand(struct scsi_cmnd *sc_cmd,
836 void (*done)(struct scsi_cmnd *));
839 * Send an ABTS frame to the target device. The sc_cmd argument
840 * is a pointer to the SCSI command to be aborted.
842 int fc_eh_abort(struct scsi_cmnd *sc_cmd);
845 * Reset a LUN by sending send the tm cmd to the target.
847 int fc_eh_device_reset(struct scsi_cmnd *sc_cmd);
850 * Reset the host adapter.
852 int fc_eh_host_reset(struct scsi_cmnd *sc_cmd);
855 * Check rport status.
857 int fc_slave_alloc(struct scsi_device *sdev);
860 * Adjust the queue depth.
862 int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason);
865 * Change the tag type.
867 int fc_change_queue_type(struct scsi_device *sdev, int tag_type);
870 * Free memory pools used by the FCP layer.
872 void fc_fcp_destroy(struct fc_lport *);
876 *****************************/
878 * Initializes ELS/CT interface
880 int fc_elsct_init(struct fc_lport *lp);
884 * EXCHANGE MANAGER LAYER
885 *****************************/
887 * Initializes Exchange Manager related
888 * function pointers in struct libfc_function_template.
890 int fc_exch_init(struct fc_lport *lp);
893 * Adds Exchange Manager (EM) mp to lport.
895 * Adds specified mp to lport using struct fc_exch_mgr_anchor,
896 * the struct fc_exch_mgr_anchor allows same EM sharing by
897 * more than one lport with their specified match function,
898 * the match function is used in allocating exchange from
901 struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
902 struct fc_exch_mgr *mp,
903 bool (*match)(struct fc_frame *));
906 * Deletes Exchange Manager (EM) from lport by removing
907 * its anchor ema from lport.
909 * If removed anchor ema was the last user of its associated EM
910 * then also destroys associated EM.
912 void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema);
915 * Allocates an Exchange Manager (EM).
917 * The EM manages exchanges for their allocation and
918 * free, also allows exchange lookup for received
921 * The class is used for initializing FC class of
922 * allocated exchange from EM.
924 * The min_xid and max_xid will limit new
925 * exchange ID (XID) within this range for
927 * The LLD may choose to have multiple EMs,
928 * e.g. one EM instance per CPU receive thread in LLD.
930 * Specified match function is used in allocating exchanges
931 * from newly allocated EM.
933 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
937 bool (*match)(struct fc_frame *));
940 * Free all exchange managers of a lport.
942 void fc_exch_mgr_free(struct fc_lport *lport);
945 * Receive a frame on specified local port and exchange manager.
947 void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp);
950 * Reset all EMs of a lport, releasing its all sequences and
951 * exchanges. If sid is non-zero, then reset only exchanges
952 * we sourced from that FID. If did is non-zero, reset only
953 * exchanges destined to that FID.
955 void fc_exch_mgr_reset(struct fc_lport *, u32 s_id, u32 d_id);
958 * Functions for fc_functions_template
960 void fc_get_host_speed(struct Scsi_Host *shost);
961 void fc_get_host_port_type(struct Scsi_Host *shost);
962 void fc_get_host_port_state(struct Scsi_Host *shost);
963 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout);
964 struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *);
966 #endif /* _LIBFC_H_ */