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