1 /*
2  * Copyright(c) 2007 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 #ifndef _LIBFC_H_
21 #define _LIBFC_H_
22 
23 #include <linux/timer.h>
24 #include <linux/if.h>
25 #include <linux/percpu.h>
26 
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_fc.h>
29 #include <scsi/scsi_bsg_fc.h>
30 
31 #include <scsi/fc/fc_fcp.h>
32 #include <scsi/fc/fc_ns.h>
33 #include <scsi/fc/fc_els.h>
34 #include <scsi/fc/fc_gs.h>
35 
36 #include <scsi/fc_frame.h>
37 
38 #define	FC_FC4_PROV_SIZE	(FC_TYPE_FCP + 1)	/* size of tables */
39 
40 /*
41  * libfc error codes
42  */
43 #define	FC_NO_ERR	0	/* no error */
44 #define	FC_EX_TIMEOUT	1	/* Exchange timeout */
45 #define	FC_EX_CLOSED	2	/* Exchange closed */
46 
47 /**
48  * enum fc_lport_state - Local port states
49  * @LPORT_ST_DISABLED: Disabled
50  * @LPORT_ST_FLOGI:    Fabric login (FLOGI) sent
51  * @LPORT_ST_DNS:      Waiting for name server remote port to become ready
52  * @LPORT_ST_RPN_ID:   Register port name by ID (RPN_ID) sent
53  * @LPORT_ST_RFT_ID:   Register Fibre Channel types by ID (RFT_ID) sent
54  * @LPORT_ST_RFF_ID:   Register FC-4 Features by ID (RFF_ID) sent
55  * @LPORT_ST_SCR:      State Change Register (SCR) sent
56  * @LPORT_ST_READY:    Ready for use
57  * @LPORT_ST_LOGO:     Local port logout (LOGO) sent
58  * @LPORT_ST_RESET:    Local port reset
59  */
60 enum fc_lport_state {
61 	LPORT_ST_DISABLED = 0,
62 	LPORT_ST_FLOGI,
63 	LPORT_ST_DNS,
64 	LPORT_ST_RNN_ID,
65 	LPORT_ST_RSNN_NN,
66 	LPORT_ST_RSPN_ID,
67 	LPORT_ST_RFT_ID,
68 	LPORT_ST_RFF_ID,
69 	LPORT_ST_SCR,
70 	LPORT_ST_READY,
71 	LPORT_ST_LOGO,
72 	LPORT_ST_RESET
73 };
74 
75 enum fc_disc_event {
76 	DISC_EV_NONE = 0,
77 	DISC_EV_SUCCESS,
78 	DISC_EV_FAILED
79 };
80 
81 /**
82  * enum fc_rport_state - Remote port states
83  * @RPORT_ST_INIT:    Initialized
84  * @RPORT_ST_FLOGI:   Waiting for FLOGI completion for point-to-multipoint
85  * @RPORT_ST_PLOGI_WAIT:   Waiting for peer to login for point-to-multipoint
86  * @RPORT_ST_PLOGI:   Waiting for PLOGI completion
87  * @RPORT_ST_PRLI:    Waiting for PRLI completion
88  * @RPORT_ST_RTV:     Waiting for RTV completion
89  * @RPORT_ST_READY:   Ready for use
90  * @RPORT_ST_ADISC:   Discover Address sent
91  * @RPORT_ST_DELETE:  Remote port being deleted
92 */
93 enum fc_rport_state {
94 	RPORT_ST_INIT,
95 	RPORT_ST_FLOGI,
96 	RPORT_ST_PLOGI_WAIT,
97 	RPORT_ST_PLOGI,
98 	RPORT_ST_PRLI,
99 	RPORT_ST_RTV,
100 	RPORT_ST_READY,
101 	RPORT_ST_ADISC,
102 	RPORT_ST_DELETE,
103 };
104 
105 /**
106  * struct fc_disc_port - temporary discovery port to hold rport identifiers
107  * @lp:         Fibre Channel host port instance
108  * @peers:      Node for list management during discovery and RSCN processing
109  * @rport_work: Work struct for starting the rport state machine
110  * @port_id:    Port ID of the discovered port
111  */
112 struct fc_disc_port {
113 	struct fc_lport    *lp;
114 	struct list_head   peers;
115 	struct work_struct rport_work;
116 	u32                port_id;
117 };
118 
119 /**
120  * enum fc_rport_event - Remote port events
121  * @RPORT_EV_NONE:   No event
122  * @RPORT_EV_READY:  Remote port is ready for use
123  * @RPORT_EV_FAILED: State machine failed, remote port is not ready
124  * @RPORT_EV_STOP:   Remote port has been stopped
125  * @RPORT_EV_LOGO:   Remote port logout (LOGO) sent
126  */
127 enum fc_rport_event {
128 	RPORT_EV_NONE = 0,
129 	RPORT_EV_READY,
130 	RPORT_EV_FAILED,
131 	RPORT_EV_STOP,
132 	RPORT_EV_LOGO
133 };
134 
135 struct fc_rport_priv;
136 
137 /**
138  * struct fc_rport_operations - Operations for a remote port
139  * @event_callback: Function to be called for remote port events
140  */
141 struct fc_rport_operations {
142 	void (*event_callback)(struct fc_lport *, struct fc_rport_priv *,
143 			       enum fc_rport_event);
144 };
145 
146 /**
147  * struct fc_rport_libfc_priv - libfc internal information about a remote port
148  * @local_port: The associated local port
149  * @rp_state:   Indicates READY for I/O or DELETE when blocked
150  * @flags:      REC and RETRY supported flags
151  * @e_d_tov:    Error detect timeout value (in msec)
152  * @r_a_tov:    Resource allocation timeout value (in msec)
153  */
154 struct fc_rport_libfc_priv {
155 	struct fc_lport		   *local_port;
156 	enum fc_rport_state        rp_state;
157 	u16			   flags;
158 	#define FC_RP_FLAGS_REC_SUPPORTED	(1 << 0)
159 	#define FC_RP_FLAGS_RETRY		(1 << 1)
160 	#define FC_RP_STARTED			(1 << 2)
161 	#define FC_RP_FLAGS_CONF_REQ		(1 << 3)
162 	unsigned int	           e_d_tov;
163 	unsigned int	           r_a_tov;
164 };
165 
166 /**
167  * struct fc_rport_priv - libfc remote port and discovery info
168  * @local_port:     The associated local port
169  * @rport:          The FC transport remote port
170  * @kref:           Reference counter
171  * @rp_state:       Enumeration that tracks progress of PLOGI, PRLI,
172  *                  and RTV exchanges
173  * @ids:            The remote port identifiers and roles
174  * @flags:          STARTED, REC and RETRY_SUPPORTED flags
175  * @max_seq:        Maximum number of concurrent sequences
176  * @disc_id:        The discovery identifier
177  * @maxframe_size:  The maximum frame size
178  * @retries:        The retry count for the current state
179  * @major_retries:  The retry count for the entire PLOGI/PRLI state machine
180  * @e_d_tov:        Error detect timeout value (in msec)
181  * @r_a_tov:        Resource allocation timeout value (in msec)
182  * @rp_mutex:       The mutex that protects the remote port
183  * @retry_work:     Handle for retries
184  * @event_callback: Callback when READY, FAILED or LOGO states complete
185  * @prli_count:     Count of open PRLI sessions in providers
186  * @rcu:	    Structure used for freeing in an RCU-safe manner
187  */
188 struct fc_rport_priv {
189 	struct fc_lport		    *local_port;
190 	struct fc_rport		    *rport;
191 	struct kref		    kref;
192 	enum fc_rport_state         rp_state;
193 	struct fc_rport_identifiers ids;
194 	u16			    flags;
195 	u16		            max_seq;
196 	u16			    disc_id;
197 	u16			    maxframe_size;
198 	unsigned int	            retries;
199 	unsigned int	            major_retries;
200 	unsigned int	            e_d_tov;
201 	unsigned int	            r_a_tov;
202 	struct mutex                rp_mutex;
203 	struct delayed_work	    retry_work;
204 	enum fc_rport_event         event;
205 	struct fc_rport_operations  *ops;
206 	struct list_head            peers;
207 	struct work_struct          event_work;
208 	u32			    supported_classes;
209 	u16                         prli_count;
210 	struct rcu_head		    rcu;
211 	u16			    sp_features;
212 	u8			    spp_type;
213 	void			    (*lld_event_callback)(struct fc_lport *,
214 						      struct fc_rport_priv *,
215 						      enum fc_rport_event);
216 };
217 
218 /**
219  * struct fcoe_dev_stats - fcoe stats structure
220  * @SecondsSinceLastReset: Seconds since the last reset
221  * @TxFrames:              Number of transmitted frames
222  * @TxWords:               Number of transmitted words
223  * @RxFrames:              Number of received frames
224  * @RxWords:               Number of received words
225  * @ErrorFrames:           Number of received error frames
226  * @DumpedFrames:          Number of dumped frames
227  * @LinkFailureCount:      Number of link failures
228  * @LossOfSignalCount:     Number for signal losses
229  * @InvalidTxWordCount:    Number of invalid transmitted words
230  * @InvalidCRCCount:       Number of invalid CRCs
231  * @InputRequests:         Number of input requests
232  * @OutputRequests:        Number of output requests
233  * @ControlRequests:       Number of control requests
234  * @InputBytes:            Number of received bytes
235  * @OutputBytes:           Number of transmitted bytes
236  * @VLinkFailureCount:     Number of virtual link failures
237  * @MissDiscAdvCount:      Number of missing FIP discovery advertisement
238  */
239 struct fcoe_dev_stats {
240 	u64		SecondsSinceLastReset;
241 	u64		TxFrames;
242 	u64		TxWords;
243 	u64		RxFrames;
244 	u64		RxWords;
245 	u64		ErrorFrames;
246 	u64		DumpedFrames;
247 	u64		LinkFailureCount;
248 	u64		LossOfSignalCount;
249 	u64		InvalidTxWordCount;
250 	u64		InvalidCRCCount;
251 	u64		InputRequests;
252 	u64		OutputRequests;
253 	u64		ControlRequests;
254 	u64		InputBytes;
255 	u64		OutputBytes;
256 	u64		VLinkFailureCount;
257 	u64		MissDiscAdvCount;
258 };
259 
260 /**
261  * struct fc_seq_els_data - ELS data used for passing ELS specific responses
262  * @reason: The reason for rejection
263  * @explan: The explanation of the rejection
264  *
265  * Mainly used by the exchange manager layer.
266  */
267 struct fc_seq_els_data {
268 	enum fc_els_rjt_reason reason;
269 	enum fc_els_rjt_explan explan;
270 };
271 
272 /**
273  * struct fc_fcp_pkt - FCP request structure (one for each scsi_cmnd request)
274  * @lp:              The associated local port
275  * @state:           The state of the I/O
276  * @ref_cnt:         Reference count
277  * @scsi_pkt_lock:   Lock to protect the SCSI packet (must be taken before the
278  *                   host_lock if both are to be held at the same time)
279  * @cmd:             The SCSI command (set and clear with the host_lock held)
280  * @list:            Tracks queued commands (accessed with the host_lock held)
281  * @timer:           The command timer
282  * @tm_done:         Completion indicator
283  * @wait_for_comp:   Indicator to wait for completion of the I/O (in jiffies)
284  * @start_time:      Timestamp indicating the start of the I/O (in jiffies)
285  * @end_time:        Timestamp indicating the end of the I/O (in jiffies)
286  * @last_pkt_time:   Timestamp of the last frame received (in jiffies)
287  * @data_len:        The length of the data
288  * @cdb_cmd:         The CDB command
289  * @xfer_len:        The transfer length
290  * @xfer_ddp:        Indicates if this transfer used DDP (XID of the exchange
291  *                   will be set here if DDP was setup)
292  * @xfer_contig_end: The offset into the buffer if the buffer is contiguous
293  *                   (Tx and Rx)
294  * @max_payload:     The maximum payload size (in bytes)
295  * @io_status:       SCSI result (upper 24 bits)
296  * @cdb_status:      CDB status
297  * @status_code:     FCP I/O status
298  * @scsi_comp_flags: Completion flags (bit 3 Underrun bit 2: overrun)
299  * @req_flags:       Request flags (bit 0: read bit:1 write)
300  * @scsi_resid:      SCSI residule length
301  * @rport:           The remote port that the SCSI command is targeted at
302  * @seq_ptr:         The sequence that will carry the SCSI command
303  * @recov_retry:     Number of recovery retries
304  * @recov_seq:       The sequence for REC or SRR
305  */
306 struct fc_fcp_pkt {
307 	/* Housekeeping information */
308 	struct fc_lport   *lp;
309 	u16		  state;
310 	atomic_t	  ref_cnt;
311 	spinlock_t	  scsi_pkt_lock;
312 
313 	/* SCSI I/O related information */
314 	struct scsi_cmnd  *cmd;
315 	struct list_head  list;
316 
317 	/* Timeout related information */
318 	struct timer_list timer;
319 	struct completion tm_done;
320 	int	          wait_for_comp;
321 	unsigned long	  start_time;
322 	unsigned long	  end_time;
323 	unsigned long	  last_pkt_time;
324 
325 	/* SCSI command and data transfer information */
326 	u32		  data_len;
327 
328 	/* Transport related veriables */
329 	struct fcp_cmnd   cdb_cmd;
330 	size_t		  xfer_len;
331 	u16		  xfer_ddp;
332 	u32		  xfer_contig_end;
333 	u16		  max_payload;
334 
335 	/* SCSI/FCP return status */
336 	u32		  io_status;
337 	u8		  cdb_status;
338 	u8		  status_code;
339 	u8		  scsi_comp_flags;
340 	u32		  req_flags;
341 	u32		  scsi_resid;
342 
343 	/* Associated structures */
344 	struct fc_rport	  *rport;
345 	struct fc_seq	  *seq_ptr;
346 
347 	/* Error Processing information */
348 	u8		  recov_retry;
349 	struct fc_seq	  *recov_seq;
350 };
351 
352 /*
353  * Structure and function definitions for managing Fibre Channel Exchanges
354  * and Sequences
355  *
356  * fc_exch holds state for one exchange and links to its active sequence.
357  *
358  * fc_seq holds the state for an individual sequence.
359  */
360 
361 struct fc_exch_mgr;
362 struct fc_exch_mgr_anchor;
363 extern u16 fc_cpu_mask;	/* cpu mask for possible cpus */
364 
365 /**
366  * struct fc_seq - FC sequence
367  * @id:       The sequence ID
368  * @ssb_stat: Status flags for the sequence status block (SSB)
369  * @cnt:      Number of frames sent so far
370  * @rec_data: FC-4 value for REC
371  */
372 struct fc_seq {
373 	u8  id;
374 	u16 ssb_stat;
375 	u16 cnt;
376 	u32 rec_data;
377 };
378 
379 #define FC_EX_DONE		(1 << 0) /* ep is completed */
380 #define FC_EX_RST_CLEANUP	(1 << 1) /* reset is forcing completion */
381 
382 /**
383  * struct fc_exch - Fibre Channel Exchange
384  * @em:           Exchange manager
385  * @pool:         Exchange pool
386  * @state:        The exchange's state
387  * @xid:          The exchange ID
388  * @ex_list:      Handle used by the EM to track free exchanges
389  * @ex_lock:      Lock that protects the exchange
390  * @ex_refcnt:    Reference count
391  * @timeout_work: Handle for timeout handler
392  * @lp:           The local port that this exchange is on
393  * @oxid:         Originator's exchange ID
394  * @rxid:         Responder's exchange ID
395  * @oid:          Originator's FCID
396  * @sid:          Source FCID
397  * @did:          Destination FCID
398  * @esb_stat:     ESB exchange status
399  * @r_a_tov:      Resouce allocation time out value (in msecs)
400  * @seq_id:       The next sequence ID to use
401  * @encaps:       encapsulation information for lower-level driver
402  * @f_ctl:        F_CTL flags for the sequence
403  * @fh_type:      The frame type
404  * @class:        The class of service
405  * @seq:          The sequence in use on this exchange
406  * @resp:         Callback for responses on this exchange
407  * @destructor:   Called when destroying the exchange
408  * @arg:          Passed as a void pointer to the resp() callback
409  *
410  * Locking notes: The ex_lock protects following items:
411  *	state, esb_stat, f_ctl, seq.ssb_stat
412  *	seq_id
413  *	sequence allocation
414  */
415 struct fc_exch {
416 	struct fc_exch_mgr  *em;
417 	struct fc_exch_pool *pool;
418 	u32		    state;
419 	u16		    xid;
420 	struct list_head    ex_list;
421 	spinlock_t	    ex_lock;
422 	atomic_t	    ex_refcnt;
423 	struct delayed_work timeout_work;
424 	struct fc_lport	    *lp;
425 	u16		    oxid;
426 	u16		    rxid;
427 	u32		    oid;
428 	u32		    sid;
429 	u32		    did;
430 	u32		    esb_stat;
431 	u32		    r_a_tov;
432 	u8		    seq_id;
433 	u8		    encaps;
434 	u32		    f_ctl;
435 	u8		    fh_type;
436 	enum fc_class	    class;
437 	struct fc_seq	    seq;
438 
439 	void		    (*resp)(struct fc_seq *, struct fc_frame *, void *);
440 	void		    *arg;
441 
442 	void		    (*destructor)(struct fc_seq *, void *);
443 
444 };
445 #define	fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
446 
447 
448 struct libfc_function_template {
449 	/*
450 	 * Interface to send a FC frame
451 	 *
452 	 * STATUS: REQUIRED
453 	 */
454 	int (*frame_send)(struct fc_lport *, struct fc_frame *);
455 
456 	/*
457 	 * Interface to send ELS/CT frames
458 	 *
459 	 * STATUS: OPTIONAL
460 	 */
461 	struct fc_seq *(*elsct_send)(struct fc_lport *, u32 did,
462 				     struct fc_frame *, unsigned int op,
463 				     void (*resp)(struct fc_seq *,
464 					     struct fc_frame *, void *arg),
465 				     void *arg, u32 timer_msec);
466 
467 	/*
468 	 * Send the FC frame payload using a new exchange and sequence.
469 	 *
470 	 * The exchange response handler is set in this routine to resp()
471 	 * function pointer. It can be called in two scenarios: if a timeout
472 	 * occurs or if a response frame is received for the exchange. The
473 	 * fc_frame pointer in response handler will also indicate timeout
474 	 * as error using IS_ERR related macros.
475 	 *
476 	 * The exchange destructor handler is also set in this routine.
477 	 * The destructor handler is invoked by EM layer when exchange
478 	 * is about to free, this can be used by caller to free its
479 	 * resources along with exchange free.
480 	 *
481 	 * The arg is passed back to resp and destructor handler.
482 	 *
483 	 * The timeout value (in msec) for an exchange is set if non zero
484 	 * timer_msec argument is specified. The timer is canceled when
485 	 * it fires or when the exchange is done. The exchange timeout handler
486 	 * is registered by EM layer.
487 	 *
488 	 * STATUS: OPTIONAL
489 	 */
490 	struct fc_seq *(*exch_seq_send)(struct fc_lport *, struct fc_frame *,
491 					void (*resp)(struct fc_seq *,
492 						     struct fc_frame *,
493 						     void *),
494 					void (*destructor)(struct fc_seq *,
495 							   void *),
496 					void *, unsigned int timer_msec);
497 
498 	/*
499 	 * Sets up the DDP context for a given exchange id on the given
500 	 * scatterlist if LLD supports DDP for large receive.
501 	 *
502 	 * STATUS: OPTIONAL
503 	 */
504 	int (*ddp_setup)(struct fc_lport *, u16, struct scatterlist *,
505 			 unsigned int);
506 	/*
507 	 * Completes the DDP transfer and returns the length of data DDPed
508 	 * for the given exchange id.
509 	 *
510 	 * STATUS: OPTIONAL
511 	 */
512 	int (*ddp_done)(struct fc_lport *, u16);
513 	/*
514 	 * Allow LLD to fill its own Link Error Status Block
515 	 *
516 	 * STATUS: OPTIONAL
517 	 */
518 	void (*get_lesb)(struct fc_lport *, struct fc_els_lesb *lesb);
519 	/*
520 	 * Send a frame using an existing sequence and exchange.
521 	 *
522 	 * STATUS: OPTIONAL
523 	 */
524 	int (*seq_send)(struct fc_lport *, struct fc_seq *,
525 			struct fc_frame *);
526 
527 	/*
528 	 * Send an ELS response using information from the received frame.
529 	 *
530 	 * STATUS: OPTIONAL
531 	 */
532 	void (*seq_els_rsp_send)(struct fc_frame *, enum fc_els_cmd,
533 				 struct fc_seq_els_data *);
534 
535 	/*
536 	 * Abort an exchange and sequence. Generally called because of a
537 	 * exchange timeout or an abort from the upper layer.
538 	 *
539 	 * A timer_msec can be specified for abort timeout, if non-zero
540 	 * timer_msec value is specified then exchange resp handler
541 	 * will be called with timeout error if no response to abort.
542 	 *
543 	 * STATUS: OPTIONAL
544 	 */
545 	int (*seq_exch_abort)(const struct fc_seq *,
546 			      unsigned int timer_msec);
547 
548 	/*
549 	 * Indicate that an exchange/sequence tuple is complete and the memory
550 	 * allocated for the related objects may be freed.
551 	 *
552 	 * STATUS: OPTIONAL
553 	 */
554 	void (*exch_done)(struct fc_seq *);
555 
556 	/*
557 	 * Start a new sequence on the same exchange/sequence tuple.
558 	 *
559 	 * STATUS: OPTIONAL
560 	 */
561 	struct fc_seq *(*seq_start_next)(struct fc_seq *);
562 
563 	/*
564 	 * Set a response handler for the exchange of the sequence.
565 	 *
566 	 * STATUS: OPTIONAL
567 	 */
568 	void (*seq_set_resp)(struct fc_seq *sp,
569 			     void (*resp)(struct fc_seq *, struct fc_frame *,
570 					  void *),
571 			     void *arg);
572 
573 	/*
574 	 * Assign a sequence for an incoming request frame.
575 	 *
576 	 * STATUS: OPTIONAL
577 	 */
578 	struct fc_seq *(*seq_assign)(struct fc_lport *, struct fc_frame *);
579 
580 	/*
581 	 * Release the reference on the sequence returned by seq_assign().
582 	 *
583 	 * STATUS: OPTIONAL
584 	 */
585 	void (*seq_release)(struct fc_seq *);
586 
587 	/*
588 	 * Reset an exchange manager, completing all sequences and exchanges.
589 	 * If s_id is non-zero, reset only exchanges originating from that FID.
590 	 * If d_id is non-zero, reset only exchanges sending to that FID.
591 	 *
592 	 * STATUS: OPTIONAL
593 	 */
594 	void (*exch_mgr_reset)(struct fc_lport *, u32 s_id, u32 d_id);
595 
596 	/*
597 	 * Flush the rport work queue. Generally used before shutdown.
598 	 *
599 	 * STATUS: OPTIONAL
600 	 */
601 	void (*rport_flush_queue)(void);
602 
603 	/*
604 	 * Receive a frame for a local port.
605 	 *
606 	 * STATUS: OPTIONAL
607 	 */
608 	void (*lport_recv)(struct fc_lport *, struct fc_frame *);
609 
610 	/*
611 	 * Reset the local port.
612 	 *
613 	 * STATUS: OPTIONAL
614 	 */
615 	int (*lport_reset)(struct fc_lport *);
616 
617 	/*
618 	 * Set the local port FC_ID.
619 	 *
620 	 * This may be provided by the LLD to allow it to be
621 	 * notified when the local port is assigned a FC-ID.
622 	 *
623 	 * The frame, if non-NULL, is the incoming frame with the
624 	 * FLOGI LS_ACC or FLOGI, and may contain the granted MAC
625 	 * address for the LLD.  The frame pointer may be NULL if
626 	 * no MAC is associated with this assignment (LOGO or PLOGI).
627 	 *
628 	 * If FC_ID is non-zero, r_a_tov and e_d_tov must be valid.
629 	 *
630 	 * Note: this is called with the local port mutex held.
631 	 *
632 	 * STATUS: OPTIONAL
633 	 */
634 	void (*lport_set_port_id)(struct fc_lport *, u32 port_id,
635 				  struct fc_frame *);
636 
637 	/*
638 	 * Create a remote port with a given port ID
639 	 *
640 	 * STATUS: OPTIONAL
641 	 */
642 	struct fc_rport_priv *(*rport_create)(struct fc_lport *, u32);
643 
644 	/*
645 	 * Initiates the RP state machine. It is called from the LP module.
646 	 * This function will issue the following commands to the N_Port
647 	 * identified by the FC ID provided.
648 	 *
649 	 * - PLOGI
650 	 * - PRLI
651 	 * - RTV
652 	 *
653 	 * STATUS: OPTIONAL
654 	 */
655 	int (*rport_login)(struct fc_rport_priv *);
656 
657 	/*
658 	 * Logoff, and remove the rport from the transport if
659 	 * it had been added. This will send a LOGO to the target.
660 	 *
661 	 * STATUS: OPTIONAL
662 	 */
663 	int (*rport_logoff)(struct fc_rport_priv *);
664 
665 	/*
666 	 * Receive a request from a remote port.
667 	 *
668 	 * STATUS: OPTIONAL
669 	 */
670 	void (*rport_recv_req)(struct fc_lport *, struct fc_frame *);
671 
672 	/*
673 	 * lookup an rport by it's port ID.
674 	 *
675 	 * STATUS: OPTIONAL
676 	 */
677 	struct fc_rport_priv *(*rport_lookup)(const struct fc_lport *, u32);
678 
679 	/*
680 	 * Destroy an rport after final kref_put().
681 	 * The argument is a pointer to the kref inside the fc_rport_priv.
682 	 */
683 	void (*rport_destroy)(struct kref *);
684 
685 	/*
686 	 * Callback routine after the remote port is logged in
687 	 *
688 	 * STATUS: OPTIONAL
689 	 */
690 	void (*rport_event_callback)(struct fc_lport *,
691 				     struct fc_rport_priv *,
692 				     enum fc_rport_event);
693 
694 	/*
695 	 * Send a fcp cmd from fsp pkt.
696 	 * Called with the SCSI host lock unlocked and irqs disabled.
697 	 *
698 	 * The resp handler is called when FCP_RSP received.
699 	 *
700 	 * STATUS: OPTIONAL
701 	 */
702 	int (*fcp_cmd_send)(struct fc_lport *, struct fc_fcp_pkt *,
703 			    void (*resp)(struct fc_seq *, struct fc_frame *,
704 					 void *));
705 
706 	/*
707 	 * Cleanup the FCP layer, used during link down and reset
708 	 *
709 	 * STATUS: OPTIONAL
710 	 */
711 	void (*fcp_cleanup)(struct fc_lport *);
712 
713 	/*
714 	 * Abort all I/O on a local port
715 	 *
716 	 * STATUS: OPTIONAL
717 	 */
718 	void (*fcp_abort_io)(struct fc_lport *);
719 
720 	/*
721 	 * Receive a request for the discovery layer.
722 	 *
723 	 * STATUS: OPTIONAL
724 	 */
725 	void (*disc_recv_req)(struct fc_lport *, struct fc_frame *);
726 
727 	/*
728 	 * Start discovery for a local port.
729 	 *
730 	 * STATUS: OPTIONAL
731 	 */
732 	void (*disc_start)(void (*disc_callback)(struct fc_lport *,
733 						 enum fc_disc_event),
734 			   struct fc_lport *);
735 
736 	/*
737 	 * Stop discovery for a given lport. This will remove
738 	 * all discovered rports
739 	 *
740 	 * STATUS: OPTIONAL
741 	 */
742 	void (*disc_stop) (struct fc_lport *);
743 
744 	/*
745 	 * Stop discovery for a given lport. This will block
746 	 * until all discovered rports are deleted from the
747 	 * FC transport class
748 	 *
749 	 * STATUS: OPTIONAL
750 	 */
751 	void (*disc_stop_final) (struct fc_lport *);
752 };
753 
754 /**
755  * struct fc_disc - Discovery context
756  * @retry_count:   Number of retries
757  * @pending:       1 if discovery is pending, 0 if not
758  * @requested:     1 if discovery has been requested, 0 if not
759  * @seq_count:     Number of sequences used for discovery
760  * @buf_len:       Length of the discovery buffer
761  * @disc_id:       Discovery ID
762  * @rports:        List of discovered remote ports
763  * @priv:          Private pointer for use by discovery code
764  * @disc_mutex:    Mutex that protects the discovery context
765  * @partial_buf:   Partial name buffer (if names are returned
766  *                 in multiple frames)
767  * @disc_work:     handle for delayed work context
768  * @disc_callback: Callback routine called when discovery completes
769  */
770 struct fc_disc {
771 	unsigned char         retry_count;
772 	unsigned char         pending;
773 	unsigned char         requested;
774 	unsigned short        seq_count;
775 	unsigned char         buf_len;
776 	u16                   disc_id;
777 
778 	struct list_head      rports;
779 	void		      *priv;
780 	struct mutex	      disc_mutex;
781 	struct fc_gpn_ft_resp partial_buf;
782 	struct delayed_work   disc_work;
783 
784 	void (*disc_callback)(struct fc_lport *,
785 			      enum fc_disc_event);
786 };
787 
788 /*
789  * Local port notifier and events.
790  */
791 extern struct blocking_notifier_head fc_lport_notifier_head;
792 enum fc_lport_event {
793 	FC_LPORT_EV_ADD,
794 	FC_LPORT_EV_DEL,
795 };
796 
797 /**
798  * struct fc_lport - Local port
799  * @host:                  The SCSI host associated with a local port
800  * @ema_list:              Exchange manager anchor list
801  * @dns_rdata:             The directory server remote port
802  * @ptp_rdata:             Point to point remote port
803  * @scsi_priv:             FCP layer internal data
804  * @disc:                  Discovery context
805  * @vports:                Child vports if N_Port
806  * @vport:                 Parent vport if VN_Port
807  * @tt:                    Libfc function template
808  * @link_up:               Link state (1 = link up, 0 = link down)
809  * @qfull:                 Queue state (1 queue is full, 0 queue is not full)
810  * @state:                 Identifies the state
811  * @boot_time:             Timestamp indicating when the local port came online
812  * @host_stats:            SCSI host statistics
813  * @dev_stats:             FCoE device stats (TODO: libfc should not be
814  *                         FCoE aware)
815  * @retry_count:           Number of retries in the current state
816  * @port_id:               FC Port ID
817  * @wwpn:                  World Wide Port Name
818  * @wwnn:                  World Wide Node Name
819  * @service_params:        Common service parameters
820  * @e_d_tov:               Error detection timeout value
821  * @r_a_tov:               Resouce allocation timeout value
822  * @rnid_gen:              RNID information
823  * @sg_supp:               Indicates if scatter gather is supported
824  * @seq_offload:           Indicates if sequence offload is supported
825  * @crc_offload:           Indicates if CRC offload is supported
826  * @lro_enabled:           Indicates if large receive offload is supported
827  * @does_npiv:             Supports multiple vports
828  * @npiv_enabled:          Switch/fabric allows NPIV
829  * @mfs:                   The maximum Fibre Channel payload size
830  * @max_retry_count:       The maximum retry attempts
831  * @max_rport_retry_count: The maximum remote port retry attempts
832  * @rport_priv_size:       Size needed by driver after struct fc_rport_priv
833  * @lro_xid:               The maximum XID for LRO
834  * @lso_max:               The maximum large offload send size
835  * @fcts:                  FC-4 type mask
836  * @lp_mutex:              Mutex to protect the local port
837  * @list:                  Linkage on list of vport peers
838  * @retry_work:            Handle to local port for delayed retry context
839  * @prov:		   Pointers available for use by passive FC-4 providers
840  * @lport_list:            Linkage on module-wide list of local ports
841  */
842 struct fc_lport {
843 	/* Associations */
844 	struct Scsi_Host	       *host;
845 	struct list_head	       ema_list;
846 	struct fc_rport_priv	       *dns_rdata;
847 	struct fc_rport_priv	       *ptp_rdata;
848 	void			       *scsi_priv;
849 	struct fc_disc                 disc;
850 
851 	/* Virtual port information */
852 	struct list_head	       vports;
853 	struct fc_vport		       *vport;
854 
855 	/* Operational Information */
856 	struct libfc_function_template tt;
857 	u8			       link_up;
858 	u8			       qfull;
859 	enum fc_lport_state	       state;
860 	unsigned long		       boot_time;
861 	struct fc_host_statistics      host_stats;
862 	struct fcoe_dev_stats	       *dev_stats;
863 	u8			       retry_count;
864 
865 	/* Fabric information */
866 	u32                            port_id;
867 	u64			       wwpn;
868 	u64			       wwnn;
869 	unsigned int		       service_params;
870 	unsigned int		       e_d_tov;
871 	unsigned int		       r_a_tov;
872 	struct fc_els_rnid_gen	       rnid_gen;
873 
874 	/* Capabilities */
875 	u32			       sg_supp:1;
876 	u32			       seq_offload:1;
877 	u32			       crc_offload:1;
878 	u32			       lro_enabled:1;
879 	u32			       does_npiv:1;
880 	u32			       npiv_enabled:1;
881 	u32			       point_to_multipoint:1;
882 	u32			       mfs;
883 	u8			       max_retry_count;
884 	u8			       max_rport_retry_count;
885 	u16			       rport_priv_size;
886 	u16			       link_speed;
887 	u16			       link_supported_speeds;
888 	u16			       lro_xid;
889 	unsigned int		       lso_max;
890 	struct fc_ns_fts	       fcts;
891 
892 	/* Miscellaneous */
893 	struct mutex                   lp_mutex;
894 	struct list_head               list;
895 	struct delayed_work	       retry_work;
896 	void			       *prov[FC_FC4_PROV_SIZE];
897 	struct list_head               lport_list;
898 };
899 
900 /**
901  * struct fc4_prov - FC-4 provider registration
902  * @prli:               Handler for incoming PRLI
903  * @prlo:               Handler for session reset
904  * @recv:		Handler for incoming request
905  * @module:		Pointer to module.  May be NULL.
906  */
907 struct fc4_prov {
908 	int (*prli)(struct fc_rport_priv *, u32 spp_len,
909 		    const struct fc_els_spp *spp_in,
910 		    struct fc_els_spp *spp_out);
911 	void (*prlo)(struct fc_rport_priv *);
912 	void (*recv)(struct fc_lport *, struct fc_frame *);
913 	struct module *module;
914 };
915 
916 /*
917  * Register FC-4 provider with libfc.
918  */
919 int fc_fc4_register_provider(enum fc_fh_type type, struct fc4_prov *);
920 void fc_fc4_deregister_provider(enum fc_fh_type type, struct fc4_prov *);
921 
922 /*
923  * FC_LPORT HELPER FUNCTIONS
924  *****************************/
925 
926 /**
927  * fc_lport_test_ready() - Determine if a local port is in the READY state
928  * @lport: The local port to test
929  */
fc_lport_test_ready(struct fc_lport * lport)930 static inline int fc_lport_test_ready(struct fc_lport *lport)
931 {
932 	return lport->state == LPORT_ST_READY;
933 }
934 
935 /**
936  * fc_set_wwnn() - Set the World Wide Node Name of a local port
937  * @lport: The local port whose WWNN is to be set
938  * @wwnn:  The new WWNN
939  */
fc_set_wwnn(struct fc_lport * lport,u64 wwnn)940 static inline void fc_set_wwnn(struct fc_lport *lport, u64 wwnn)
941 {
942 	lport->wwnn = wwnn;
943 }
944 
945 /**
946  * fc_set_wwpn() - Set the World Wide Port Name of a local port
947  * @lport: The local port whose WWPN is to be set
948  * @wwnn:  The new WWPN
949  */
fc_set_wwpn(struct fc_lport * lport,u64 wwnn)950 static inline void fc_set_wwpn(struct fc_lport *lport, u64 wwnn)
951 {
952 	lport->wwpn = wwnn;
953 }
954 
955 /**
956  * fc_lport_state_enter() - Change a local port's state
957  * @lport: The local port whose state is to change
958  * @state: The new state
959  */
fc_lport_state_enter(struct fc_lport * lport,enum fc_lport_state state)960 static inline void fc_lport_state_enter(struct fc_lport *lport,
961 					enum fc_lport_state state)
962 {
963 	if (state != lport->state)
964 		lport->retry_count = 0;
965 	lport->state = state;
966 }
967 
968 /**
969  * fc_lport_init_stats() - Allocate per-CPU statistics for a local port
970  * @lport: The local port whose statistics are to be initialized
971  */
fc_lport_init_stats(struct fc_lport * lport)972 static inline int fc_lport_init_stats(struct fc_lport *lport)
973 {
974 	lport->dev_stats = alloc_percpu(struct fcoe_dev_stats);
975 	if (!lport->dev_stats)
976 		return -ENOMEM;
977 	return 0;
978 }
979 
980 /**
981  * fc_lport_free_stats() - Free memory for a local port's statistics
982  * @lport: The local port whose statistics are to be freed
983  */
fc_lport_free_stats(struct fc_lport * lport)984 static inline void fc_lport_free_stats(struct fc_lport *lport)
985 {
986 	free_percpu(lport->dev_stats);
987 }
988 
989 /**
990  * lport_priv() - Return the private data from a local port
991  * @lport: The local port whose private data is to be retreived
992  */
lport_priv(const struct fc_lport * lport)993 static inline void *lport_priv(const struct fc_lport *lport)
994 {
995 	return (void *)(lport + 1);
996 }
997 
998 /**
999  * libfc_host_alloc() - Allocate a Scsi_Host with room for a local port and
1000  *                      LLD private data
1001  * @sht:       The SCSI host template
1002  * @priv_size: Size of private data
1003  *
1004  * Returns: libfc lport
1005  */
1006 static inline struct fc_lport *
libfc_host_alloc(struct scsi_host_template * sht,int priv_size)1007 libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
1008 {
1009 	struct fc_lport *lport;
1010 	struct Scsi_Host *shost;
1011 
1012 	shost = scsi_host_alloc(sht, sizeof(*lport) + priv_size);
1013 	if (!shost)
1014 		return NULL;
1015 	lport = shost_priv(shost);
1016 	lport->host = shost;
1017 	INIT_LIST_HEAD(&lport->ema_list);
1018 	INIT_LIST_HEAD(&lport->vports);
1019 	return lport;
1020 }
1021 
1022 /*
1023  * FC_FCP HELPER FUNCTIONS
1024  *****************************/
fc_fcp_is_read(const struct fc_fcp_pkt * fsp)1025 static inline bool fc_fcp_is_read(const struct fc_fcp_pkt *fsp)
1026 {
1027 	if (fsp && fsp->cmd)
1028 		return fsp->cmd->sc_data_direction == DMA_FROM_DEVICE;
1029 	return false;
1030 }
1031 
1032 /*
1033  * LOCAL PORT LAYER
1034  *****************************/
1035 int fc_lport_init(struct fc_lport *);
1036 int fc_lport_destroy(struct fc_lport *);
1037 int fc_fabric_logoff(struct fc_lport *);
1038 int fc_fabric_login(struct fc_lport *);
1039 void __fc_linkup(struct fc_lport *);
1040 void fc_linkup(struct fc_lport *);
1041 void __fc_linkdown(struct fc_lport *);
1042 void fc_linkdown(struct fc_lport *);
1043 void fc_vport_setlink(struct fc_lport *);
1044 void fc_vports_linkchange(struct fc_lport *);
1045 int fc_lport_config(struct fc_lport *);
1046 int fc_lport_reset(struct fc_lport *);
1047 int fc_set_mfs(struct fc_lport *, u32 mfs);
1048 struct fc_lport *libfc_vport_create(struct fc_vport *, int privsize);
1049 struct fc_lport *fc_vport_id_lookup(struct fc_lport *, u32 port_id);
1050 int fc_lport_bsg_request(struct fc_bsg_job *);
1051 void fc_lport_set_local_id(struct fc_lport *, u32 port_id);
1052 void fc_lport_iterate(void (*func)(struct fc_lport *, void *), void *);
1053 
1054 /*
1055  * REMOTE PORT LAYER
1056  *****************************/
1057 int fc_rport_init(struct fc_lport *);
1058 void fc_rport_terminate_io(struct fc_rport *);
1059 
1060 /*
1061  * DISCOVERY LAYER
1062  *****************************/
1063 int fc_disc_init(struct fc_lport *);
1064 
fc_disc_lport(struct fc_disc * disc)1065 static inline struct fc_lport *fc_disc_lport(struct fc_disc *disc)
1066 {
1067 	return container_of(disc, struct fc_lport, disc);
1068 }
1069 
1070 /*
1071  * FCP LAYER
1072  *****************************/
1073 int fc_fcp_init(struct fc_lport *);
1074 void fc_fcp_destroy(struct fc_lport *);
1075 
1076 /*
1077  * SCSI INTERACTION LAYER
1078  *****************************/
1079 int fc_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
1080 int fc_eh_abort(struct scsi_cmnd *);
1081 int fc_eh_device_reset(struct scsi_cmnd *);
1082 int fc_eh_host_reset(struct scsi_cmnd *);
1083 int fc_slave_alloc(struct scsi_device *);
1084 int fc_change_queue_depth(struct scsi_device *, int qdepth, int reason);
1085 int fc_change_queue_type(struct scsi_device *, int tag_type);
1086 
1087 /*
1088  * ELS/CT interface
1089  *****************************/
1090 int fc_elsct_init(struct fc_lport *);
1091 struct fc_seq *fc_elsct_send(struct fc_lport *, u32 did,
1092 				    struct fc_frame *,
1093 				    unsigned int op,
1094 				    void (*resp)(struct fc_seq *,
1095 						 struct fc_frame *,
1096 						 void *arg),
1097 				    void *arg, u32 timer_msec);
1098 void fc_lport_flogi_resp(struct fc_seq *, struct fc_frame *, void *);
1099 void fc_lport_logo_resp(struct fc_seq *, struct fc_frame *, void *);
1100 void fc_fill_reply_hdr(struct fc_frame *, const struct fc_frame *,
1101 		       enum fc_rctl, u32 parm_offset);
1102 void fc_fill_hdr(struct fc_frame *, const struct fc_frame *,
1103 		 enum fc_rctl, u32 f_ctl, u16 seq_cnt, u32 parm_offset);
1104 
1105 
1106 /*
1107  * EXCHANGE MANAGER LAYER
1108  *****************************/
1109 int fc_exch_init(struct fc_lport *);
1110 struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *,
1111 					   struct fc_exch_mgr *,
1112 					   bool (*match)(struct fc_frame *));
1113 void fc_exch_mgr_del(struct fc_exch_mgr_anchor *);
1114 int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst);
1115 struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *, enum fc_class class,
1116 				      u16 min_xid, u16 max_xid,
1117 				      bool (*match)(struct fc_frame *));
1118 void fc_exch_mgr_free(struct fc_lport *);
1119 void fc_exch_recv(struct fc_lport *, struct fc_frame *);
1120 void fc_exch_mgr_reset(struct fc_lport *, u32 s_id, u32 d_id);
1121 
1122 /*
1123  * Functions for fc_functions_template
1124  */
1125 void fc_get_host_speed(struct Scsi_Host *);
1126 void fc_get_host_port_state(struct Scsi_Host *);
1127 void fc_set_rport_loss_tmo(struct fc_rport *, u32 timeout);
1128 struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *);
1129 
1130 #endif /* _LIBFC_H_ */
1131