xref: /OK3568_Linux_fs/kernel/drivers/scsi/scsi_transport_srp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * SCSI RDMA (SRP) transport class
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/init.h>
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/jiffies.h>
10*4882a593Smuzhiyun #include <linux/err.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <scsi/scsi.h>
15*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h>
16*4882a593Smuzhiyun #include <scsi/scsi_device.h>
17*4882a593Smuzhiyun #include <scsi/scsi_host.h>
18*4882a593Smuzhiyun #include <scsi/scsi_transport.h>
19*4882a593Smuzhiyun #include <scsi/scsi_transport_srp.h>
20*4882a593Smuzhiyun #include "scsi_priv.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct srp_host_attrs {
23*4882a593Smuzhiyun 	atomic_t next_port_id;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun #define to_srp_host_attrs(host)	((struct srp_host_attrs *)(host)->shost_data)
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define SRP_HOST_ATTRS 0
28*4882a593Smuzhiyun #define SRP_RPORT_ATTRS 8
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun struct srp_internal {
31*4882a593Smuzhiyun 	struct scsi_transport_template t;
32*4882a593Smuzhiyun 	struct srp_function_template *f;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
37*4882a593Smuzhiyun 	struct transport_container rport_attr_cont;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static int scsi_is_srp_rport(const struct device *dev);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define	dev_to_rport(d)	container_of(d, struct srp_rport, dev)
45*4882a593Smuzhiyun #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
rport_to_shost(struct srp_rport * r)46*4882a593Smuzhiyun static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	return dev_to_shost(r->dev.parent);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
find_child_rport(struct device * dev,void * data)51*4882a593Smuzhiyun static int find_child_rport(struct device *dev, void *data)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	struct device **child = data;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (scsi_is_srp_rport(dev)) {
56*4882a593Smuzhiyun 		WARN_ON_ONCE(*child);
57*4882a593Smuzhiyun 		*child = dev;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 	return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
shost_to_rport(struct Scsi_Host * shost)62*4882a593Smuzhiyun static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct device *child = NULL;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child,
67*4882a593Smuzhiyun 					   find_child_rport) < 0);
68*4882a593Smuzhiyun 	return child ? dev_to_rport(child) : NULL;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun  * srp_tmo_valid() - check timeout combination validity
73*4882a593Smuzhiyun  * @reconnect_delay: Reconnect delay in seconds.
74*4882a593Smuzhiyun  * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
75*4882a593Smuzhiyun  * @dev_loss_tmo: Device loss timeout in seconds.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * The combination of the timeout parameters must be such that SCSI commands
78*4882a593Smuzhiyun  * are finished in a reasonable time. Hence do not allow the fast I/O fail
79*4882a593Smuzhiyun  * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
80*4882a593Smuzhiyun  * exceed that limit if failing I/O fast has been disabled. Furthermore, these
81*4882a593Smuzhiyun  * parameters must be such that multipath can detect failed paths timely.
82*4882a593Smuzhiyun  * Hence do not allow all three parameters to be disabled simultaneously.
83*4882a593Smuzhiyun  */
srp_tmo_valid(int reconnect_delay,int fast_io_fail_tmo,long dev_loss_tmo)84*4882a593Smuzhiyun int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
87*4882a593Smuzhiyun 		return -EINVAL;
88*4882a593Smuzhiyun 	if (reconnect_delay == 0)
89*4882a593Smuzhiyun 		return -EINVAL;
90*4882a593Smuzhiyun 	if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
91*4882a593Smuzhiyun 		return -EINVAL;
92*4882a593Smuzhiyun 	if (fast_io_fail_tmo < 0 &&
93*4882a593Smuzhiyun 	    dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
94*4882a593Smuzhiyun 		return -EINVAL;
95*4882a593Smuzhiyun 	if (dev_loss_tmo >= LONG_MAX / HZ)
96*4882a593Smuzhiyun 		return -EINVAL;
97*4882a593Smuzhiyun 	if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
98*4882a593Smuzhiyun 	    fast_io_fail_tmo >= dev_loss_tmo)
99*4882a593Smuzhiyun 		return -EINVAL;
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_tmo_valid);
103*4882a593Smuzhiyun 
srp_host_setup(struct transport_container * tc,struct device * dev,struct device * cdev)104*4882a593Smuzhiyun static int srp_host_setup(struct transport_container *tc, struct device *dev,
105*4882a593Smuzhiyun 			  struct device *cdev)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(dev);
108*4882a593Smuzhiyun 	struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	atomic_set(&srp_host->next_port_id, 0);
111*4882a593Smuzhiyun 	return 0;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
115*4882a593Smuzhiyun 			       NULL, NULL);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
118*4882a593Smuzhiyun 			       NULL, NULL, NULL);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun static ssize_t
show_srp_rport_id(struct device * dev,struct device_attribute * attr,char * buf)121*4882a593Smuzhiyun show_srp_rport_id(struct device *dev, struct device_attribute *attr,
122*4882a593Smuzhiyun 		  char *buf)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
125*4882a593Smuzhiyun 	return sprintf(buf, "%16phC\n", rport->port_id);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun static const struct {
131*4882a593Smuzhiyun 	u32 value;
132*4882a593Smuzhiyun 	char *name;
133*4882a593Smuzhiyun } srp_rport_role_names[] = {
134*4882a593Smuzhiyun 	{SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
135*4882a593Smuzhiyun 	{SRP_RPORT_ROLE_TARGET, "SRP Target"},
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun static ssize_t
show_srp_rport_roles(struct device * dev,struct device_attribute * attr,char * buf)139*4882a593Smuzhiyun show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
140*4882a593Smuzhiyun 		     char *buf)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
143*4882a593Smuzhiyun 	int i;
144*4882a593Smuzhiyun 	char *name = NULL;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
147*4882a593Smuzhiyun 		if (srp_rport_role_names[i].value == rport->roles) {
148*4882a593Smuzhiyun 			name = srp_rport_role_names[i].name;
149*4882a593Smuzhiyun 			break;
150*4882a593Smuzhiyun 		}
151*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", name ? : "unknown");
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
155*4882a593Smuzhiyun 
store_srp_rport_delete(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)156*4882a593Smuzhiyun static ssize_t store_srp_rport_delete(struct device *dev,
157*4882a593Smuzhiyun 				      struct device_attribute *attr,
158*4882a593Smuzhiyun 				      const char *buf, size_t count)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
161*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(dev);
162*4882a593Smuzhiyun 	struct srp_internal *i = to_srp_internal(shost->transportt);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	if (i->f->rport_delete) {
165*4882a593Smuzhiyun 		i->f->rport_delete(rport);
166*4882a593Smuzhiyun 		return count;
167*4882a593Smuzhiyun 	} else {
168*4882a593Smuzhiyun 		return -ENOSYS;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
173*4882a593Smuzhiyun 
show_srp_rport_state(struct device * dev,struct device_attribute * attr,char * buf)174*4882a593Smuzhiyun static ssize_t show_srp_rport_state(struct device *dev,
175*4882a593Smuzhiyun 				    struct device_attribute *attr,
176*4882a593Smuzhiyun 				    char *buf)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	static const char *const state_name[] = {
179*4882a593Smuzhiyun 		[SRP_RPORT_RUNNING]	= "running",
180*4882a593Smuzhiyun 		[SRP_RPORT_BLOCKED]	= "blocked",
181*4882a593Smuzhiyun 		[SRP_RPORT_FAIL_FAST]	= "fail-fast",
182*4882a593Smuzhiyun 		[SRP_RPORT_LOST]	= "lost",
183*4882a593Smuzhiyun 	};
184*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
185*4882a593Smuzhiyun 	enum srp_rport_state state = rport->state;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	return sprintf(buf, "%s\n",
188*4882a593Smuzhiyun 		       (unsigned)state < ARRAY_SIZE(state_name) ?
189*4882a593Smuzhiyun 		       state_name[state] : "???");
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
193*4882a593Smuzhiyun 
srp_show_tmo(char * buf,int tmo)194*4882a593Smuzhiyun static ssize_t srp_show_tmo(char *buf, int tmo)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
srp_parse_tmo(int * tmo,const char * buf)199*4882a593Smuzhiyun int srp_parse_tmo(int *tmo, const char *buf)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	int res = 0;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	if (strncmp(buf, "off", 3) != 0)
204*4882a593Smuzhiyun 		res = kstrtoint(buf, 0, tmo);
205*4882a593Smuzhiyun 	else
206*4882a593Smuzhiyun 		*tmo = -1;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	return res;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun EXPORT_SYMBOL(srp_parse_tmo);
211*4882a593Smuzhiyun 
show_reconnect_delay(struct device * dev,struct device_attribute * attr,char * buf)212*4882a593Smuzhiyun static ssize_t show_reconnect_delay(struct device *dev,
213*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	return srp_show_tmo(buf, rport->reconnect_delay);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
store_reconnect_delay(struct device * dev,struct device_attribute * attr,const char * buf,const size_t count)220*4882a593Smuzhiyun static ssize_t store_reconnect_delay(struct device *dev,
221*4882a593Smuzhiyun 				     struct device_attribute *attr,
222*4882a593Smuzhiyun 				     const char *buf, const size_t count)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
225*4882a593Smuzhiyun 	int res, delay;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	res = srp_parse_tmo(&delay, buf);
228*4882a593Smuzhiyun 	if (res)
229*4882a593Smuzhiyun 		goto out;
230*4882a593Smuzhiyun 	res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
231*4882a593Smuzhiyun 			    rport->dev_loss_tmo);
232*4882a593Smuzhiyun 	if (res)
233*4882a593Smuzhiyun 		goto out;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (rport->reconnect_delay <= 0 && delay > 0 &&
236*4882a593Smuzhiyun 	    rport->state != SRP_RPORT_RUNNING) {
237*4882a593Smuzhiyun 		queue_delayed_work(system_long_wq, &rport->reconnect_work,
238*4882a593Smuzhiyun 				   delay * HZ);
239*4882a593Smuzhiyun 	} else if (delay <= 0) {
240*4882a593Smuzhiyun 		cancel_delayed_work(&rport->reconnect_work);
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 	rport->reconnect_delay = delay;
243*4882a593Smuzhiyun 	res = count;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun out:
246*4882a593Smuzhiyun 	return res;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
250*4882a593Smuzhiyun 		   store_reconnect_delay);
251*4882a593Smuzhiyun 
show_failed_reconnects(struct device * dev,struct device_attribute * attr,char * buf)252*4882a593Smuzhiyun static ssize_t show_failed_reconnects(struct device *dev,
253*4882a593Smuzhiyun 				      struct device_attribute *attr, char *buf)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rport->failed_reconnects);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
261*4882a593Smuzhiyun 
show_srp_rport_fast_io_fail_tmo(struct device * dev,struct device_attribute * attr,char * buf)262*4882a593Smuzhiyun static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
263*4882a593Smuzhiyun 					       struct device_attribute *attr,
264*4882a593Smuzhiyun 					       char *buf)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	return srp_show_tmo(buf, rport->fast_io_fail_tmo);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
store_srp_rport_fast_io_fail_tmo(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)271*4882a593Smuzhiyun static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
272*4882a593Smuzhiyun 						struct device_attribute *attr,
273*4882a593Smuzhiyun 						const char *buf, size_t count)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
276*4882a593Smuzhiyun 	int res;
277*4882a593Smuzhiyun 	int fast_io_fail_tmo;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	res = srp_parse_tmo(&fast_io_fail_tmo, buf);
280*4882a593Smuzhiyun 	if (res)
281*4882a593Smuzhiyun 		goto out;
282*4882a593Smuzhiyun 	res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
283*4882a593Smuzhiyun 			    rport->dev_loss_tmo);
284*4882a593Smuzhiyun 	if (res)
285*4882a593Smuzhiyun 		goto out;
286*4882a593Smuzhiyun 	rport->fast_io_fail_tmo = fast_io_fail_tmo;
287*4882a593Smuzhiyun 	res = count;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun out:
290*4882a593Smuzhiyun 	return res;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
294*4882a593Smuzhiyun 		   show_srp_rport_fast_io_fail_tmo,
295*4882a593Smuzhiyun 		   store_srp_rport_fast_io_fail_tmo);
296*4882a593Smuzhiyun 
show_srp_rport_dev_loss_tmo(struct device * dev,struct device_attribute * attr,char * buf)297*4882a593Smuzhiyun static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
298*4882a593Smuzhiyun 					   struct device_attribute *attr,
299*4882a593Smuzhiyun 					   char *buf)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return srp_show_tmo(buf, rport->dev_loss_tmo);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
store_srp_rport_dev_loss_tmo(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)306*4882a593Smuzhiyun static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
307*4882a593Smuzhiyun 					    struct device_attribute *attr,
308*4882a593Smuzhiyun 					    const char *buf, size_t count)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	struct srp_rport *rport = transport_class_to_srp_rport(dev);
311*4882a593Smuzhiyun 	int res;
312*4882a593Smuzhiyun 	int dev_loss_tmo;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	res = srp_parse_tmo(&dev_loss_tmo, buf);
315*4882a593Smuzhiyun 	if (res)
316*4882a593Smuzhiyun 		goto out;
317*4882a593Smuzhiyun 	res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
318*4882a593Smuzhiyun 			    dev_loss_tmo);
319*4882a593Smuzhiyun 	if (res)
320*4882a593Smuzhiyun 		goto out;
321*4882a593Smuzhiyun 	rport->dev_loss_tmo = dev_loss_tmo;
322*4882a593Smuzhiyun 	res = count;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun out:
325*4882a593Smuzhiyun 	return res;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
329*4882a593Smuzhiyun 		   show_srp_rport_dev_loss_tmo,
330*4882a593Smuzhiyun 		   store_srp_rport_dev_loss_tmo);
331*4882a593Smuzhiyun 
srp_rport_set_state(struct srp_rport * rport,enum srp_rport_state new_state)332*4882a593Smuzhiyun static int srp_rport_set_state(struct srp_rport *rport,
333*4882a593Smuzhiyun 			       enum srp_rport_state new_state)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	enum srp_rport_state old_state = rport->state;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	lockdep_assert_held(&rport->mutex);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	switch (new_state) {
340*4882a593Smuzhiyun 	case SRP_RPORT_RUNNING:
341*4882a593Smuzhiyun 		switch (old_state) {
342*4882a593Smuzhiyun 		case SRP_RPORT_LOST:
343*4882a593Smuzhiyun 			goto invalid;
344*4882a593Smuzhiyun 		default:
345*4882a593Smuzhiyun 			break;
346*4882a593Smuzhiyun 		}
347*4882a593Smuzhiyun 		break;
348*4882a593Smuzhiyun 	case SRP_RPORT_BLOCKED:
349*4882a593Smuzhiyun 		switch (old_state) {
350*4882a593Smuzhiyun 		case SRP_RPORT_RUNNING:
351*4882a593Smuzhiyun 			break;
352*4882a593Smuzhiyun 		default:
353*4882a593Smuzhiyun 			goto invalid;
354*4882a593Smuzhiyun 		}
355*4882a593Smuzhiyun 		break;
356*4882a593Smuzhiyun 	case SRP_RPORT_FAIL_FAST:
357*4882a593Smuzhiyun 		switch (old_state) {
358*4882a593Smuzhiyun 		case SRP_RPORT_LOST:
359*4882a593Smuzhiyun 			goto invalid;
360*4882a593Smuzhiyun 		default:
361*4882a593Smuzhiyun 			break;
362*4882a593Smuzhiyun 		}
363*4882a593Smuzhiyun 		break;
364*4882a593Smuzhiyun 	case SRP_RPORT_LOST:
365*4882a593Smuzhiyun 		break;
366*4882a593Smuzhiyun 	}
367*4882a593Smuzhiyun 	rport->state = new_state;
368*4882a593Smuzhiyun 	return 0;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun invalid:
371*4882a593Smuzhiyun 	return -EINVAL;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun /**
375*4882a593Smuzhiyun  * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
376*4882a593Smuzhiyun  * @work: Work structure used for scheduling this operation.
377*4882a593Smuzhiyun  */
srp_reconnect_work(struct work_struct * work)378*4882a593Smuzhiyun static void srp_reconnect_work(struct work_struct *work)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	struct srp_rport *rport = container_of(to_delayed_work(work),
381*4882a593Smuzhiyun 					struct srp_rport, reconnect_work);
382*4882a593Smuzhiyun 	struct Scsi_Host *shost = rport_to_shost(rport);
383*4882a593Smuzhiyun 	int delay, res;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	res = srp_reconnect_rport(rport);
386*4882a593Smuzhiyun 	if (res != 0) {
387*4882a593Smuzhiyun 		shost_printk(KERN_ERR, shost,
388*4882a593Smuzhiyun 			     "reconnect attempt %d failed (%d)\n",
389*4882a593Smuzhiyun 			     ++rport->failed_reconnects, res);
390*4882a593Smuzhiyun 		delay = rport->reconnect_delay *
391*4882a593Smuzhiyun 			min(100, max(1, rport->failed_reconnects - 10));
392*4882a593Smuzhiyun 		if (delay > 0)
393*4882a593Smuzhiyun 			queue_delayed_work(system_long_wq,
394*4882a593Smuzhiyun 					   &rport->reconnect_work, delay * HZ);
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun  * scsi_target_block() must have been called before this function is
400*4882a593Smuzhiyun  * called to guarantee that no .queuecommand() calls are in progress.
401*4882a593Smuzhiyun  */
__rport_fail_io_fast(struct srp_rport * rport)402*4882a593Smuzhiyun static void __rport_fail_io_fast(struct srp_rport *rport)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	struct Scsi_Host *shost = rport_to_shost(rport);
405*4882a593Smuzhiyun 	struct srp_internal *i;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	lockdep_assert_held(&rport->mutex);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
410*4882a593Smuzhiyun 		return;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	/* Involve the LLD if possible to terminate all I/O on the rport. */
415*4882a593Smuzhiyun 	i = to_srp_internal(shost->transportt);
416*4882a593Smuzhiyun 	if (i->f->terminate_rport_io)
417*4882a593Smuzhiyun 		i->f->terminate_rport_io(rport);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun /**
421*4882a593Smuzhiyun  * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
422*4882a593Smuzhiyun  * @work: Work structure used for scheduling this operation.
423*4882a593Smuzhiyun  */
rport_fast_io_fail_timedout(struct work_struct * work)424*4882a593Smuzhiyun static void rport_fast_io_fail_timedout(struct work_struct *work)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	struct srp_rport *rport = container_of(to_delayed_work(work),
427*4882a593Smuzhiyun 					struct srp_rport, fast_io_fail_work);
428*4882a593Smuzhiyun 	struct Scsi_Host *shost = rport_to_shost(rport);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
431*4882a593Smuzhiyun 		dev_name(&rport->dev), dev_name(&shost->shost_gendev));
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	mutex_lock(&rport->mutex);
434*4882a593Smuzhiyun 	if (rport->state == SRP_RPORT_BLOCKED)
435*4882a593Smuzhiyun 		__rport_fail_io_fast(rport);
436*4882a593Smuzhiyun 	mutex_unlock(&rport->mutex);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun  * rport_dev_loss_timedout() - device loss timeout handler
441*4882a593Smuzhiyun  * @work: Work structure used for scheduling this operation.
442*4882a593Smuzhiyun  */
rport_dev_loss_timedout(struct work_struct * work)443*4882a593Smuzhiyun static void rport_dev_loss_timedout(struct work_struct *work)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct srp_rport *rport = container_of(to_delayed_work(work),
446*4882a593Smuzhiyun 					struct srp_rport, dev_loss_work);
447*4882a593Smuzhiyun 	struct Scsi_Host *shost = rport_to_shost(rport);
448*4882a593Smuzhiyun 	struct srp_internal *i = to_srp_internal(shost->transportt);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
451*4882a593Smuzhiyun 		dev_name(&rport->dev), dev_name(&shost->shost_gendev));
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	mutex_lock(&rport->mutex);
454*4882a593Smuzhiyun 	WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
455*4882a593Smuzhiyun 	scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
456*4882a593Smuzhiyun 	mutex_unlock(&rport->mutex);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	i->f->rport_delete(rport);
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
__srp_start_tl_fail_timers(struct srp_rport * rport)461*4882a593Smuzhiyun static void __srp_start_tl_fail_timers(struct srp_rport *rport)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun 	struct Scsi_Host *shost = rport_to_shost(rport);
464*4882a593Smuzhiyun 	int delay, fast_io_fail_tmo, dev_loss_tmo;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	lockdep_assert_held(&rport->mutex);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	delay = rport->reconnect_delay;
469*4882a593Smuzhiyun 	fast_io_fail_tmo = rport->fast_io_fail_tmo;
470*4882a593Smuzhiyun 	dev_loss_tmo = rport->dev_loss_tmo;
471*4882a593Smuzhiyun 	pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
472*4882a593Smuzhiyun 		 rport->state);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	if (rport->state == SRP_RPORT_LOST)
475*4882a593Smuzhiyun 		return;
476*4882a593Smuzhiyun 	if (delay > 0)
477*4882a593Smuzhiyun 		queue_delayed_work(system_long_wq, &rport->reconnect_work,
478*4882a593Smuzhiyun 				   1UL * delay * HZ);
479*4882a593Smuzhiyun 	if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
480*4882a593Smuzhiyun 	    srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
481*4882a593Smuzhiyun 		pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
482*4882a593Smuzhiyun 			 rport->state);
483*4882a593Smuzhiyun 		scsi_target_block(&shost->shost_gendev);
484*4882a593Smuzhiyun 		if (fast_io_fail_tmo >= 0)
485*4882a593Smuzhiyun 			queue_delayed_work(system_long_wq,
486*4882a593Smuzhiyun 					   &rport->fast_io_fail_work,
487*4882a593Smuzhiyun 					   1UL * fast_io_fail_tmo * HZ);
488*4882a593Smuzhiyun 		if (dev_loss_tmo >= 0)
489*4882a593Smuzhiyun 			queue_delayed_work(system_long_wq,
490*4882a593Smuzhiyun 					   &rport->dev_loss_work,
491*4882a593Smuzhiyun 					   1UL * dev_loss_tmo * HZ);
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /**
496*4882a593Smuzhiyun  * srp_start_tl_fail_timers() - start the transport layer failure timers
497*4882a593Smuzhiyun  * @rport: SRP target port.
498*4882a593Smuzhiyun  *
499*4882a593Smuzhiyun  * Start the transport layer fast I/O failure and device loss timers. Do not
500*4882a593Smuzhiyun  * modify a timer that was already started.
501*4882a593Smuzhiyun  */
srp_start_tl_fail_timers(struct srp_rport * rport)502*4882a593Smuzhiyun void srp_start_tl_fail_timers(struct srp_rport *rport)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	mutex_lock(&rport->mutex);
505*4882a593Smuzhiyun 	__srp_start_tl_fail_timers(rport);
506*4882a593Smuzhiyun 	mutex_unlock(&rport->mutex);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun EXPORT_SYMBOL(srp_start_tl_fail_timers);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun  * srp_reconnect_rport() - reconnect to an SRP target port
512*4882a593Smuzhiyun  * @rport: SRP target port.
513*4882a593Smuzhiyun  *
514*4882a593Smuzhiyun  * Blocks SCSI command queueing before invoking reconnect() such that
515*4882a593Smuzhiyun  * queuecommand() won't be invoked concurrently with reconnect() from outside
516*4882a593Smuzhiyun  * the SCSI EH. This is important since a reconnect() implementation may
517*4882a593Smuzhiyun  * reallocate resources needed by queuecommand().
518*4882a593Smuzhiyun  *
519*4882a593Smuzhiyun  * Notes:
520*4882a593Smuzhiyun  * - This function neither waits until outstanding requests have finished nor
521*4882a593Smuzhiyun  *   tries to abort these. It is the responsibility of the reconnect()
522*4882a593Smuzhiyun  *   function to finish outstanding commands before reconnecting to the target
523*4882a593Smuzhiyun  *   port.
524*4882a593Smuzhiyun  * - It is the responsibility of the caller to ensure that the resources
525*4882a593Smuzhiyun  *   reallocated by the reconnect() function won't be used while this function
526*4882a593Smuzhiyun  *   is in progress. One possible strategy is to invoke this function from
527*4882a593Smuzhiyun  *   the context of the SCSI EH thread only. Another possible strategy is to
528*4882a593Smuzhiyun  *   lock the rport mutex inside each SCSI LLD callback that can be invoked by
529*4882a593Smuzhiyun  *   the SCSI EH (the scsi_host_template.eh_*() functions and also the
530*4882a593Smuzhiyun  *   scsi_host_template.queuecommand() function).
531*4882a593Smuzhiyun  */
srp_reconnect_rport(struct srp_rport * rport)532*4882a593Smuzhiyun int srp_reconnect_rport(struct srp_rport *rport)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	struct Scsi_Host *shost = rport_to_shost(rport);
535*4882a593Smuzhiyun 	struct srp_internal *i = to_srp_internal(shost->transportt);
536*4882a593Smuzhiyun 	struct scsi_device *sdev;
537*4882a593Smuzhiyun 	int res;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	res = mutex_lock_interruptible(&rport->mutex);
542*4882a593Smuzhiyun 	if (res)
543*4882a593Smuzhiyun 		goto out;
544*4882a593Smuzhiyun 	if (rport->state != SRP_RPORT_FAIL_FAST && rport->state != SRP_RPORT_LOST)
545*4882a593Smuzhiyun 		/*
546*4882a593Smuzhiyun 		 * sdev state must be SDEV_TRANSPORT_OFFLINE, transition
547*4882a593Smuzhiyun 		 * to SDEV_BLOCK is illegal. Calling scsi_target_unblock()
548*4882a593Smuzhiyun 		 * later is ok though, scsi_internal_device_unblock_nowait()
549*4882a593Smuzhiyun 		 * treats SDEV_TRANSPORT_OFFLINE like SDEV_BLOCK.
550*4882a593Smuzhiyun 		 */
551*4882a593Smuzhiyun 		scsi_target_block(&shost->shost_gendev);
552*4882a593Smuzhiyun 	res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
553*4882a593Smuzhiyun 	pr_debug("%s (state %d): transport.reconnect() returned %d\n",
554*4882a593Smuzhiyun 		 dev_name(&shost->shost_gendev), rport->state, res);
555*4882a593Smuzhiyun 	if (res == 0) {
556*4882a593Smuzhiyun 		cancel_delayed_work(&rport->fast_io_fail_work);
557*4882a593Smuzhiyun 		cancel_delayed_work(&rport->dev_loss_work);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 		rport->failed_reconnects = 0;
560*4882a593Smuzhiyun 		srp_rport_set_state(rport, SRP_RPORT_RUNNING);
561*4882a593Smuzhiyun 		scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
562*4882a593Smuzhiyun 		/*
563*4882a593Smuzhiyun 		 * If the SCSI error handler has offlined one or more devices,
564*4882a593Smuzhiyun 		 * invoking scsi_target_unblock() won't change the state of
565*4882a593Smuzhiyun 		 * these devices into running so do that explicitly.
566*4882a593Smuzhiyun 		 */
567*4882a593Smuzhiyun 		shost_for_each_device(sdev, shost) {
568*4882a593Smuzhiyun 			mutex_lock(&sdev->state_mutex);
569*4882a593Smuzhiyun 			if (sdev->sdev_state == SDEV_OFFLINE)
570*4882a593Smuzhiyun 				sdev->sdev_state = SDEV_RUNNING;
571*4882a593Smuzhiyun 			mutex_unlock(&sdev->state_mutex);
572*4882a593Smuzhiyun 		}
573*4882a593Smuzhiyun 	} else if (rport->state == SRP_RPORT_RUNNING) {
574*4882a593Smuzhiyun 		/*
575*4882a593Smuzhiyun 		 * srp_reconnect_rport() has been invoked with fast_io_fail
576*4882a593Smuzhiyun 		 * and dev_loss off. Mark the port as failed and start the TL
577*4882a593Smuzhiyun 		 * failure timers if these had not yet been started.
578*4882a593Smuzhiyun 		 */
579*4882a593Smuzhiyun 		__rport_fail_io_fast(rport);
580*4882a593Smuzhiyun 		__srp_start_tl_fail_timers(rport);
581*4882a593Smuzhiyun 	} else if (rport->state != SRP_RPORT_BLOCKED) {
582*4882a593Smuzhiyun 		scsi_target_unblock(&shost->shost_gendev,
583*4882a593Smuzhiyun 				    SDEV_TRANSPORT_OFFLINE);
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 	mutex_unlock(&rport->mutex);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun out:
588*4882a593Smuzhiyun 	return res;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun EXPORT_SYMBOL(srp_reconnect_rport);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun /**
593*4882a593Smuzhiyun  * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
594*4882a593Smuzhiyun  * @scmd: SCSI command.
595*4882a593Smuzhiyun  *
596*4882a593Smuzhiyun  * If a timeout occurs while an rport is in the blocked state, ask the SCSI
597*4882a593Smuzhiyun  * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
598*4882a593Smuzhiyun  * handle the timeout (BLK_EH_DONE).
599*4882a593Smuzhiyun  *
600*4882a593Smuzhiyun  * Note: This function is called from soft-IRQ context and with the request
601*4882a593Smuzhiyun  * queue lock held.
602*4882a593Smuzhiyun  */
srp_timed_out(struct scsi_cmnd * scmd)603*4882a593Smuzhiyun enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct scsi_device *sdev = scmd->device;
606*4882a593Smuzhiyun 	struct Scsi_Host *shost = sdev->host;
607*4882a593Smuzhiyun 	struct srp_internal *i = to_srp_internal(shost->transportt);
608*4882a593Smuzhiyun 	struct srp_rport *rport = shost_to_rport(shost);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
611*4882a593Smuzhiyun 	return rport && rport->fast_io_fail_tmo < 0 &&
612*4882a593Smuzhiyun 		rport->dev_loss_tmo < 0 &&
613*4882a593Smuzhiyun 		i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
614*4882a593Smuzhiyun 		BLK_EH_RESET_TIMER : BLK_EH_DONE;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun EXPORT_SYMBOL(srp_timed_out);
617*4882a593Smuzhiyun 
srp_rport_release(struct device * dev)618*4882a593Smuzhiyun static void srp_rport_release(struct device *dev)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	struct srp_rport *rport = dev_to_rport(dev);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	put_device(dev->parent);
623*4882a593Smuzhiyun 	kfree(rport);
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
scsi_is_srp_rport(const struct device * dev)626*4882a593Smuzhiyun static int scsi_is_srp_rport(const struct device *dev)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	return dev->release == srp_rport_release;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
srp_rport_match(struct attribute_container * cont,struct device * dev)631*4882a593Smuzhiyun static int srp_rport_match(struct attribute_container *cont,
632*4882a593Smuzhiyun 			   struct device *dev)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	struct Scsi_Host *shost;
635*4882a593Smuzhiyun 	struct srp_internal *i;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	if (!scsi_is_srp_rport(dev))
638*4882a593Smuzhiyun 		return 0;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	shost = dev_to_shost(dev->parent);
641*4882a593Smuzhiyun 	if (!shost->transportt)
642*4882a593Smuzhiyun 		return 0;
643*4882a593Smuzhiyun 	if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
644*4882a593Smuzhiyun 		return 0;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	i = to_srp_internal(shost->transportt);
647*4882a593Smuzhiyun 	return &i->rport_attr_cont.ac == cont;
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun 
srp_host_match(struct attribute_container * cont,struct device * dev)650*4882a593Smuzhiyun static int srp_host_match(struct attribute_container *cont, struct device *dev)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	struct Scsi_Host *shost;
653*4882a593Smuzhiyun 	struct srp_internal *i;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	if (!scsi_is_host_device(dev))
656*4882a593Smuzhiyun 		return 0;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	shost = dev_to_shost(dev);
659*4882a593Smuzhiyun 	if (!shost->transportt)
660*4882a593Smuzhiyun 		return 0;
661*4882a593Smuzhiyun 	if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
662*4882a593Smuzhiyun 		return 0;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	i = to_srp_internal(shost->transportt);
665*4882a593Smuzhiyun 	return &i->t.host_attrs.ac == cont;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun /**
669*4882a593Smuzhiyun  * srp_rport_get() - increment rport reference count
670*4882a593Smuzhiyun  * @rport: SRP target port.
671*4882a593Smuzhiyun  */
srp_rport_get(struct srp_rport * rport)672*4882a593Smuzhiyun void srp_rport_get(struct srp_rport *rport)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun 	get_device(&rport->dev);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun EXPORT_SYMBOL(srp_rport_get);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /**
679*4882a593Smuzhiyun  * srp_rport_put() - decrement rport reference count
680*4882a593Smuzhiyun  * @rport: SRP target port.
681*4882a593Smuzhiyun  */
srp_rport_put(struct srp_rport * rport)682*4882a593Smuzhiyun void srp_rport_put(struct srp_rport *rport)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	put_device(&rport->dev);
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun EXPORT_SYMBOL(srp_rport_put);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun /**
689*4882a593Smuzhiyun  * srp_rport_add - add a SRP remote port to the device hierarchy
690*4882a593Smuzhiyun  * @shost:	scsi host the remote port is connected to.
691*4882a593Smuzhiyun  * @ids:	The port id for the remote port.
692*4882a593Smuzhiyun  *
693*4882a593Smuzhiyun  * Publishes a port to the rest of the system.
694*4882a593Smuzhiyun  */
srp_rport_add(struct Scsi_Host * shost,struct srp_rport_identifiers * ids)695*4882a593Smuzhiyun struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
696*4882a593Smuzhiyun 				struct srp_rport_identifiers *ids)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	struct srp_rport *rport;
699*4882a593Smuzhiyun 	struct device *parent = &shost->shost_gendev;
700*4882a593Smuzhiyun 	struct srp_internal *i = to_srp_internal(shost->transportt);
701*4882a593Smuzhiyun 	int id, ret;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	rport = kzalloc(sizeof(*rport), GFP_KERNEL);
704*4882a593Smuzhiyun 	if (!rport)
705*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	mutex_init(&rport->mutex);
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	device_initialize(&rport->dev);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	rport->dev.parent = get_device(parent);
712*4882a593Smuzhiyun 	rport->dev.release = srp_rport_release;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
715*4882a593Smuzhiyun 	rport->roles = ids->roles;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	if (i->f->reconnect)
718*4882a593Smuzhiyun 		rport->reconnect_delay = i->f->reconnect_delay ?
719*4882a593Smuzhiyun 			*i->f->reconnect_delay : 10;
720*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
721*4882a593Smuzhiyun 	rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
722*4882a593Smuzhiyun 		*i->f->fast_io_fail_tmo : 15;
723*4882a593Smuzhiyun 	rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
724*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&rport->fast_io_fail_work,
725*4882a593Smuzhiyun 			  rport_fast_io_fail_timedout);
726*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
729*4882a593Smuzhiyun 	dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	transport_setup_device(&rport->dev);
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	ret = device_add(&rport->dev);
734*4882a593Smuzhiyun 	if (ret) {
735*4882a593Smuzhiyun 		transport_destroy_device(&rport->dev);
736*4882a593Smuzhiyun 		put_device(&rport->dev);
737*4882a593Smuzhiyun 		return ERR_PTR(ret);
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	transport_add_device(&rport->dev);
741*4882a593Smuzhiyun 	transport_configure_device(&rport->dev);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	return rport;
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_rport_add);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun /**
748*4882a593Smuzhiyun  * srp_rport_del  -  remove a SRP remote port
749*4882a593Smuzhiyun  * @rport:	SRP remote port to remove
750*4882a593Smuzhiyun  *
751*4882a593Smuzhiyun  * Removes the specified SRP remote port.
752*4882a593Smuzhiyun  */
srp_rport_del(struct srp_rport * rport)753*4882a593Smuzhiyun void srp_rport_del(struct srp_rport *rport)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun 	struct device *dev = &rport->dev;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	transport_remove_device(dev);
758*4882a593Smuzhiyun 	device_del(dev);
759*4882a593Smuzhiyun 	transport_destroy_device(dev);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	put_device(dev);
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_rport_del);
764*4882a593Smuzhiyun 
do_srp_rport_del(struct device * dev,void * data)765*4882a593Smuzhiyun static int do_srp_rport_del(struct device *dev, void *data)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun 	if (scsi_is_srp_rport(dev))
768*4882a593Smuzhiyun 		srp_rport_del(dev_to_rport(dev));
769*4882a593Smuzhiyun 	return 0;
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun /**
773*4882a593Smuzhiyun  * srp_remove_host  -  tear down a Scsi_Host's SRP data structures
774*4882a593Smuzhiyun  * @shost:	Scsi Host that is torn down
775*4882a593Smuzhiyun  *
776*4882a593Smuzhiyun  * Removes all SRP remote ports for a given Scsi_Host.
777*4882a593Smuzhiyun  * Must be called just before scsi_remove_host for SRP HBAs.
778*4882a593Smuzhiyun  */
srp_remove_host(struct Scsi_Host * shost)779*4882a593Smuzhiyun void srp_remove_host(struct Scsi_Host *shost)
780*4882a593Smuzhiyun {
781*4882a593Smuzhiyun 	device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_remove_host);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun /**
786*4882a593Smuzhiyun  * srp_stop_rport_timers - stop the transport layer recovery timers
787*4882a593Smuzhiyun  * @rport: SRP remote port for which to stop the timers.
788*4882a593Smuzhiyun  *
789*4882a593Smuzhiyun  * Must be called after srp_remove_host() and scsi_remove_host(). The caller
790*4882a593Smuzhiyun  * must hold a reference on the rport (rport->dev) and on the SCSI host
791*4882a593Smuzhiyun  * (rport->dev.parent).
792*4882a593Smuzhiyun  */
srp_stop_rport_timers(struct srp_rport * rport)793*4882a593Smuzhiyun void srp_stop_rport_timers(struct srp_rport *rport)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	mutex_lock(&rport->mutex);
796*4882a593Smuzhiyun 	if (rport->state == SRP_RPORT_BLOCKED)
797*4882a593Smuzhiyun 		__rport_fail_io_fast(rport);
798*4882a593Smuzhiyun 	srp_rport_set_state(rport, SRP_RPORT_LOST);
799*4882a593Smuzhiyun 	mutex_unlock(&rport->mutex);
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	cancel_delayed_work_sync(&rport->reconnect_work);
802*4882a593Smuzhiyun 	cancel_delayed_work_sync(&rport->fast_io_fail_work);
803*4882a593Smuzhiyun 	cancel_delayed_work_sync(&rport->dev_loss_work);
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun /**
808*4882a593Smuzhiyun  * srp_attach_transport  -  instantiate SRP transport template
809*4882a593Smuzhiyun  * @ft:		SRP transport class function template
810*4882a593Smuzhiyun  */
811*4882a593Smuzhiyun struct scsi_transport_template *
srp_attach_transport(struct srp_function_template * ft)812*4882a593Smuzhiyun srp_attach_transport(struct srp_function_template *ft)
813*4882a593Smuzhiyun {
814*4882a593Smuzhiyun 	int count;
815*4882a593Smuzhiyun 	struct srp_internal *i;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	i = kzalloc(sizeof(*i), GFP_KERNEL);
818*4882a593Smuzhiyun 	if (!i)
819*4882a593Smuzhiyun 		return NULL;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	i->t.host_size = sizeof(struct srp_host_attrs);
822*4882a593Smuzhiyun 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
823*4882a593Smuzhiyun 	i->t.host_attrs.ac.class = &srp_host_class.class;
824*4882a593Smuzhiyun 	i->t.host_attrs.ac.match = srp_host_match;
825*4882a593Smuzhiyun 	i->host_attrs[0] = NULL;
826*4882a593Smuzhiyun 	transport_container_register(&i->t.host_attrs);
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
829*4882a593Smuzhiyun 	i->rport_attr_cont.ac.class = &srp_rport_class.class;
830*4882a593Smuzhiyun 	i->rport_attr_cont.ac.match = srp_rport_match;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	count = 0;
833*4882a593Smuzhiyun 	i->rport_attrs[count++] = &dev_attr_port_id;
834*4882a593Smuzhiyun 	i->rport_attrs[count++] = &dev_attr_roles;
835*4882a593Smuzhiyun 	if (ft->has_rport_state) {
836*4882a593Smuzhiyun 		i->rport_attrs[count++] = &dev_attr_state;
837*4882a593Smuzhiyun 		i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
838*4882a593Smuzhiyun 		i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 	if (ft->reconnect) {
841*4882a593Smuzhiyun 		i->rport_attrs[count++] = &dev_attr_reconnect_delay;
842*4882a593Smuzhiyun 		i->rport_attrs[count++] = &dev_attr_failed_reconnects;
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 	if (ft->rport_delete)
845*4882a593Smuzhiyun 		i->rport_attrs[count++] = &dev_attr_delete;
846*4882a593Smuzhiyun 	i->rport_attrs[count++] = NULL;
847*4882a593Smuzhiyun 	BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	transport_container_register(&i->rport_attr_cont);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	i->f = ft;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	return &i->t;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_attach_transport);
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun /**
858*4882a593Smuzhiyun  * srp_release_transport  -  release SRP transport template instance
859*4882a593Smuzhiyun  * @t:		transport template instance
860*4882a593Smuzhiyun  */
srp_release_transport(struct scsi_transport_template * t)861*4882a593Smuzhiyun void srp_release_transport(struct scsi_transport_template *t)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun 	struct srp_internal *i = to_srp_internal(t);
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	transport_container_unregister(&i->t.host_attrs);
866*4882a593Smuzhiyun 	transport_container_unregister(&i->rport_attr_cont);
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	kfree(i);
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(srp_release_transport);
871*4882a593Smuzhiyun 
srp_transport_init(void)872*4882a593Smuzhiyun static __init int srp_transport_init(void)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	int ret;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	ret = transport_class_register(&srp_host_class);
877*4882a593Smuzhiyun 	if (ret)
878*4882a593Smuzhiyun 		return ret;
879*4882a593Smuzhiyun 	ret = transport_class_register(&srp_rport_class);
880*4882a593Smuzhiyun 	if (ret)
881*4882a593Smuzhiyun 		goto unregister_host_class;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	return 0;
884*4882a593Smuzhiyun unregister_host_class:
885*4882a593Smuzhiyun 	transport_class_unregister(&srp_host_class);
886*4882a593Smuzhiyun 	return ret;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
srp_transport_exit(void)889*4882a593Smuzhiyun static void __exit srp_transport_exit(void)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun 	transport_class_unregister(&srp_host_class);
892*4882a593Smuzhiyun 	transport_class_unregister(&srp_rport_class);
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun MODULE_AUTHOR("FUJITA Tomonori");
896*4882a593Smuzhiyun MODULE_DESCRIPTION("SRP Transport Attributes");
897*4882a593Smuzhiyun MODULE_LICENSE("GPL");
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun module_init(srp_transport_init);
900*4882a593Smuzhiyun module_exit(srp_transport_exit);
901