xref: /OK3568_Linux_fs/kernel/include/scsi/scsi_transport_srp.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef SCSI_TRANSPORT_SRP_H
3*4882a593Smuzhiyun #define SCSI_TRANSPORT_SRP_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/transport_class.h>
6*4882a593Smuzhiyun #include <linux/types.h>
7*4882a593Smuzhiyun #include <linux/mutex.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #define SRP_RPORT_ROLE_INITIATOR 0
10*4882a593Smuzhiyun #define SRP_RPORT_ROLE_TARGET 1
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct srp_rport_identifiers {
13*4882a593Smuzhiyun 	u8 port_id[16];
14*4882a593Smuzhiyun 	u8 roles;
15*4882a593Smuzhiyun };
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * enum srp_rport_state - SRP transport layer state
19*4882a593Smuzhiyun  * @SRP_RPORT_RUNNING:   Transport layer operational.
20*4882a593Smuzhiyun  * @SRP_RPORT_BLOCKED:   Transport layer not operational; fast I/O fail timer
21*4882a593Smuzhiyun  *                       is running and I/O has been blocked.
22*4882a593Smuzhiyun  * @SRP_RPORT_FAIL_FAST: Fast I/O fail timer has expired; fail I/O fast.
23*4882a593Smuzhiyun  * @SRP_RPORT_LOST:      Port is being removed.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun enum srp_rport_state {
26*4882a593Smuzhiyun 	SRP_RPORT_RUNNING,
27*4882a593Smuzhiyun 	SRP_RPORT_BLOCKED,
28*4882a593Smuzhiyun 	SRP_RPORT_FAIL_FAST,
29*4882a593Smuzhiyun 	SRP_RPORT_LOST,
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * struct srp_rport - SRP initiator or target port
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Fields that are relevant for SRP initiator and SRP target drivers:
36*4882a593Smuzhiyun  * @dev:               Device associated with this rport.
37*4882a593Smuzhiyun  * @port_id:           16-byte port identifier.
38*4882a593Smuzhiyun  * @roles:             Role of this port - initiator or target.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * Fields that are only relevant for SRP initiator drivers:
41*4882a593Smuzhiyun  * @lld_data:          LLD private data.
42*4882a593Smuzhiyun  * @mutex:             Protects against concurrent rport reconnect /
43*4882a593Smuzhiyun  *                     fast_io_fail / dev_loss_tmo activity.
44*4882a593Smuzhiyun  * @state:             rport state.
45*4882a593Smuzhiyun  * @reconnect_delay:   Reconnect delay in seconds.
46*4882a593Smuzhiyun  * @failed_reconnects: Number of failed reconnect attempts.
47*4882a593Smuzhiyun  * @reconnect_work:    Work structure used for scheduling reconnect attempts.
48*4882a593Smuzhiyun  * @fast_io_fail_tmo:  Fast I/O fail timeout in seconds.
49*4882a593Smuzhiyun  * @dev_loss_tmo:      Device loss timeout in seconds.
50*4882a593Smuzhiyun  * @fast_io_fail_work: Work structure used for scheduling fast I/O fail work.
51*4882a593Smuzhiyun  * @dev_loss_work:     Work structure used for scheduling device loss work.
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun struct srp_rport {
54*4882a593Smuzhiyun 	/* for initiator and target drivers */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	struct device dev;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	u8 port_id[16];
59*4882a593Smuzhiyun 	u8 roles;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	/* for initiator drivers */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	void			*lld_data;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	struct mutex		mutex;
66*4882a593Smuzhiyun 	enum srp_rport_state	state;
67*4882a593Smuzhiyun 	int			reconnect_delay;
68*4882a593Smuzhiyun 	int			failed_reconnects;
69*4882a593Smuzhiyun 	struct delayed_work	reconnect_work;
70*4882a593Smuzhiyun 	int			fast_io_fail_tmo;
71*4882a593Smuzhiyun 	int			dev_loss_tmo;
72*4882a593Smuzhiyun 	struct delayed_work	fast_io_fail_work;
73*4882a593Smuzhiyun 	struct delayed_work	dev_loss_work;
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /**
77*4882a593Smuzhiyun  * struct srp_function_template
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Fields that are only relevant for SRP initiator drivers:
80*4882a593Smuzhiyun  * @has_rport_state: Whether or not to create the state, fast_io_fail_tmo and
81*4882a593Smuzhiyun  *     dev_loss_tmo sysfs attribute for an rport.
82*4882a593Smuzhiyun  * @reset_timer_if_blocked: Whether or srp_timed_out() should reset the command
83*4882a593Smuzhiyun  *     timer if the device on which it has been queued is blocked.
84*4882a593Smuzhiyun  * @reconnect_delay: If not NULL, points to the default reconnect_delay value.
85*4882a593Smuzhiyun  * @fast_io_fail_tmo: If not NULL, points to the default fast_io_fail_tmo value.
86*4882a593Smuzhiyun  * @dev_loss_tmo: If not NULL, points to the default dev_loss_tmo value.
87*4882a593Smuzhiyun  * @reconnect: Callback function for reconnecting to the target. See also
88*4882a593Smuzhiyun  *     srp_reconnect_rport().
89*4882a593Smuzhiyun  * @terminate_rport_io: Callback function for terminating all outstanding I/O
90*4882a593Smuzhiyun  *     requests for an rport.
91*4882a593Smuzhiyun  * @rport_delete: Callback function that deletes an rport.
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun struct srp_function_template {
94*4882a593Smuzhiyun 	/* for initiator drivers */
95*4882a593Smuzhiyun 	bool has_rport_state;
96*4882a593Smuzhiyun 	bool reset_timer_if_blocked;
97*4882a593Smuzhiyun 	int *reconnect_delay;
98*4882a593Smuzhiyun 	int *fast_io_fail_tmo;
99*4882a593Smuzhiyun 	int *dev_loss_tmo;
100*4882a593Smuzhiyun 	int (*reconnect)(struct srp_rport *rport);
101*4882a593Smuzhiyun 	void (*terminate_rport_io)(struct srp_rport *rport);
102*4882a593Smuzhiyun 	void (*rport_delete)(struct srp_rport *rport);
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun extern struct scsi_transport_template *
106*4882a593Smuzhiyun srp_attach_transport(struct srp_function_template *);
107*4882a593Smuzhiyun extern void srp_release_transport(struct scsi_transport_template *);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun extern void srp_rport_get(struct srp_rport *rport);
110*4882a593Smuzhiyun extern void srp_rport_put(struct srp_rport *rport);
111*4882a593Smuzhiyun extern struct srp_rport *srp_rport_add(struct Scsi_Host *,
112*4882a593Smuzhiyun 				       struct srp_rport_identifiers *);
113*4882a593Smuzhiyun extern void srp_rport_del(struct srp_rport *);
114*4882a593Smuzhiyun extern int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo,
115*4882a593Smuzhiyun 			 long dev_loss_tmo);
116*4882a593Smuzhiyun int srp_parse_tmo(int *tmo, const char *buf);
117*4882a593Smuzhiyun extern int srp_reconnect_rport(struct srp_rport *rport);
118*4882a593Smuzhiyun extern void srp_start_tl_fail_timers(struct srp_rport *rport);
119*4882a593Smuzhiyun extern void srp_remove_host(struct Scsi_Host *);
120*4882a593Smuzhiyun extern void srp_stop_rport_timers(struct srp_rport *rport);
121*4882a593Smuzhiyun enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun  * srp_chkready() - evaluate the transport layer state before I/O
125*4882a593Smuzhiyun  * @rport: SRP target port pointer.
126*4882a593Smuzhiyun  *
127*4882a593Smuzhiyun  * Returns a SCSI result code that can be returned by the LLD queuecommand()
128*4882a593Smuzhiyun  * implementation. The role of this function is similar to that of
129*4882a593Smuzhiyun  * fc_remote_port_chkready().
130*4882a593Smuzhiyun  */
srp_chkready(struct srp_rport * rport)131*4882a593Smuzhiyun static inline int srp_chkready(struct srp_rport *rport)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	switch (rport->state) {
134*4882a593Smuzhiyun 	case SRP_RPORT_RUNNING:
135*4882a593Smuzhiyun 	case SRP_RPORT_BLOCKED:
136*4882a593Smuzhiyun 	default:
137*4882a593Smuzhiyun 		return 0;
138*4882a593Smuzhiyun 	case SRP_RPORT_FAIL_FAST:
139*4882a593Smuzhiyun 		return DID_TRANSPORT_FAILFAST << 16;
140*4882a593Smuzhiyun 	case SRP_RPORT_LOST:
141*4882a593Smuzhiyun 		return DID_NO_CONNECT << 16;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun #endif
146