1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
209345f65SFUJITA Tomonori #ifndef SCSI_TRANSPORT_SRP_H
309345f65SFUJITA Tomonori #define SCSI_TRANSPORT_SRP_H
409345f65SFUJITA Tomonori
509345f65SFUJITA Tomonori #include <linux/transport_class.h>
609345f65SFUJITA Tomonori #include <linux/types.h>
709345f65SFUJITA Tomonori #include <linux/mutex.h>
809345f65SFUJITA Tomonori
9aebd5e47SFUJITA Tomonori #define SRP_RPORT_ROLE_INITIATOR 0
10aebd5e47SFUJITA Tomonori #define SRP_RPORT_ROLE_TARGET 1
11aebd5e47SFUJITA Tomonori
1209345f65SFUJITA Tomonori struct srp_rport_identifiers {
1309345f65SFUJITA Tomonori u8 port_id[16];
14aebd5e47SFUJITA Tomonori u8 roles;
1509345f65SFUJITA Tomonori };
1609345f65SFUJITA Tomonori
1729c17324SBart Van Assche /**
1829c17324SBart Van Assche * enum srp_rport_state - SRP transport layer state
1929c17324SBart Van Assche * @SRP_RPORT_RUNNING: Transport layer operational.
2029c17324SBart Van Assche * @SRP_RPORT_BLOCKED: Transport layer not operational; fast I/O fail timer
2129c17324SBart Van Assche * is running and I/O has been blocked.
2229c17324SBart Van Assche * @SRP_RPORT_FAIL_FAST: Fast I/O fail timer has expired; fail I/O fast.
2393079162SBart Van Assche * @SRP_RPORT_LOST: Port is being removed.
2429c17324SBart Van Assche */
2529c17324SBart Van Assche enum srp_rport_state {
2629c17324SBart Van Assche SRP_RPORT_RUNNING,
2729c17324SBart Van Assche SRP_RPORT_BLOCKED,
2829c17324SBart Van Assche SRP_RPORT_FAIL_FAST,
2929c17324SBart Van Assche SRP_RPORT_LOST,
3029c17324SBart Van Assche };
3129c17324SBart Van Assche
3229c17324SBart Van Assche /**
330c7f8218SBart Van Assche * struct srp_rport - SRP initiator or target port
340c7f8218SBart Van Assche *
350c7f8218SBart Van Assche * Fields that are relevant for SRP initiator and SRP target drivers:
360c7f8218SBart Van Assche * @dev: Device associated with this rport.
370c7f8218SBart Van Assche * @port_id: 16-byte port identifier.
380c7f8218SBart Van Assche * @roles: Role of this port - initiator or target.
390c7f8218SBart Van Assche *
400c7f8218SBart Van Assche * Fields that are only relevant for SRP initiator drivers:
4129c17324SBart Van Assche * @lld_data: LLD private data.
420c7f8218SBart Van Assche * @mutex: Protects against concurrent rport reconnect /
430c7f8218SBart Van Assche * fast_io_fail / dev_loss_tmo activity.
440c7f8218SBart Van Assche * @state: rport state.
450c7f8218SBart Van Assche * @reconnect_delay: Reconnect delay in seconds.
460c7f8218SBart Van Assche * @failed_reconnects: Number of failed reconnect attempts.
470c7f8218SBart Van Assche * @reconnect_work: Work structure used for scheduling reconnect attempts.
480c7f8218SBart Van Assche * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
490c7f8218SBart Van Assche * @dev_loss_tmo: Device loss timeout in seconds.
500c7f8218SBart Van Assche * @fast_io_fail_work: Work structure used for scheduling fast I/O fail work.
510c7f8218SBart Van Assche * @dev_loss_work: Work structure used for scheduling device loss work.
5229c17324SBart Van Assche */
5309345f65SFUJITA Tomonori struct srp_rport {
54dc1bdbd9SBart Van Assche /* for initiator and target drivers */
55dc1bdbd9SBart Van Assche
5609345f65SFUJITA Tomonori struct device dev;
5709345f65SFUJITA Tomonori
5809345f65SFUJITA Tomonori u8 port_id[16];
59aebd5e47SFUJITA Tomonori u8 roles;
60dc1bdbd9SBart Van Assche
61dc1bdbd9SBart Van Assche /* for initiator drivers */
62dc1bdbd9SBart Van Assche
6329c17324SBart Van Assche void *lld_data;
6429c17324SBart Van Assche
6529c17324SBart Van Assche struct mutex mutex;
6629c17324SBart Van Assche enum srp_rport_state state;
678c64e453SBart Van Assche int reconnect_delay;
688c64e453SBart Van Assche int failed_reconnects;
698c64e453SBart Van Assche struct delayed_work reconnect_work;
7029c17324SBart Van Assche int fast_io_fail_tmo;
7129c17324SBart Van Assche int dev_loss_tmo;
7229c17324SBart Van Assche struct delayed_work fast_io_fail_work;
7329c17324SBart Van Assche struct delayed_work dev_loss_work;
7409345f65SFUJITA Tomonori };
7509345f65SFUJITA Tomonori
7629c17324SBart Van Assche /**
77*a2530eb7SRandy Dunlap * struct srp_function_template - template for SRP initiator drivers
780c7f8218SBart Van Assche *
790c7f8218SBart Van Assche * Fields that are only relevant for SRP initiator drivers:
8029c17324SBart Van Assche * @has_rport_state: Whether or not to create the state, fast_io_fail_tmo and
8129c17324SBart Van Assche * dev_loss_tmo sysfs attribute for an rport.
8229c17324SBart Van Assche * @reset_timer_if_blocked: Whether or srp_timed_out() should reset the command
8329c17324SBart Van Assche * timer if the device on which it has been queued is blocked.
848c64e453SBart Van Assche * @reconnect_delay: If not NULL, points to the default reconnect_delay value.
8529c17324SBart Van Assche * @fast_io_fail_tmo: If not NULL, points to the default fast_io_fail_tmo value.
8629c17324SBart Van Assche * @dev_loss_tmo: If not NULL, points to the default dev_loss_tmo value.
8729c17324SBart Van Assche * @reconnect: Callback function for reconnecting to the target. See also
8829c17324SBart Van Assche * srp_reconnect_rport().
8929c17324SBart Van Assche * @terminate_rport_io: Callback function for terminating all outstanding I/O
9029c17324SBart Van Assche * requests for an rport.
910c7f8218SBart Van Assche * @rport_delete: Callback function that deletes an rport.
9229c17324SBart Van Assche */
9309345f65SFUJITA Tomonori struct srp_function_template {
94dc1bdbd9SBart Van Assche /* for initiator drivers */
9529c17324SBart Van Assche bool has_rport_state;
9629c17324SBart Van Assche bool reset_timer_if_blocked;
978c64e453SBart Van Assche int *reconnect_delay;
9829c17324SBart Van Assche int *fast_io_fail_tmo;
9929c17324SBart Van Assche int *dev_loss_tmo;
10029c17324SBart Van Assche int (*reconnect)(struct srp_rport *rport);
10129c17324SBart Van Assche void (*terminate_rport_io)(struct srp_rport *rport);
102dc1bdbd9SBart Van Assche void (*rport_delete)(struct srp_rport *rport);
10309345f65SFUJITA Tomonori };
10409345f65SFUJITA Tomonori
10509345f65SFUJITA Tomonori extern struct scsi_transport_template *
10609345f65SFUJITA Tomonori srp_attach_transport(struct srp_function_template *);
10709345f65SFUJITA Tomonori extern void srp_release_transport(struct scsi_transport_template *);
10809345f65SFUJITA Tomonori
1099dd69a60SBart Van Assche extern void srp_rport_get(struct srp_rport *rport);
1109dd69a60SBart Van Assche extern void srp_rport_put(struct srp_rport *rport);
11109345f65SFUJITA Tomonori extern struct srp_rport *srp_rport_add(struct Scsi_Host *,
11209345f65SFUJITA Tomonori struct srp_rport_identifiers *);
11309345f65SFUJITA Tomonori extern void srp_rport_del(struct srp_rport *);
1148c64e453SBart Van Assche extern int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo,
11585aa93beSBart Van Assche long dev_loss_tmo);
1163fdf70acSSagi Grimberg int srp_parse_tmo(int *tmo, const char *buf);
11729c17324SBart Van Assche extern int srp_reconnect_rport(struct srp_rport *rport);
11829c17324SBart Van Assche extern void srp_start_tl_fail_timers(struct srp_rport *rport);
11909345f65SFUJITA Tomonori extern void srp_remove_host(struct Scsi_Host *);
12093079162SBart Van Assche extern void srp_stop_rport_timers(struct srp_rport *rport);
121dee7121eSBart Van Assche enum scsi_timeout_action srp_timed_out(struct scsi_cmnd *scmd);
12209345f65SFUJITA Tomonori
12329c17324SBart Van Assche /**
12429c17324SBart Van Assche * srp_chkready() - evaluate the transport layer state before I/O
1250c7f8218SBart Van Assche * @rport: SRP target port pointer.
12629c17324SBart Van Assche *
127*a2530eb7SRandy Dunlap * Returns: a SCSI result code that can be returned by the LLD queuecommand()
12829c17324SBart Van Assche * implementation. The role of this function is similar to that of
12929c17324SBart Van Assche * fc_remote_port_chkready().
13029c17324SBart Van Assche */
srp_chkready(struct srp_rport * rport)13129c17324SBart Van Assche static inline int srp_chkready(struct srp_rport *rport)
13229c17324SBart Van Assche {
13329c17324SBart Van Assche switch (rport->state) {
13429c17324SBart Van Assche case SRP_RPORT_RUNNING:
13529c17324SBart Van Assche case SRP_RPORT_BLOCKED:
13629c17324SBart Van Assche default:
13729c17324SBart Van Assche return 0;
13829c17324SBart Van Assche case SRP_RPORT_FAIL_FAST:
13929c17324SBart Van Assche return DID_TRANSPORT_FAILFAST << 16;
14029c17324SBart Van Assche case SRP_RPORT_LOST:
14129c17324SBart Van Assche return DID_NO_CONNECT << 16;
14229c17324SBart Van Assche }
14329c17324SBart Van Assche }
14429c17324SBart Van Assche
14509345f65SFUJITA Tomonori #endif
146