GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / scsi / hosts.c
1 /*
2  *  hosts.c Copyright (C) 1992 Drew Eckhardt
3  *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
4  *          Copyright (C) 2002-2003 Christoph Hellwig
5  *
6  *  mid to lowlevel SCSI driver interface
7  *      Initial versions: Drew Eckhardt
8  *      Subsequent revisions: Eric Youngdale
9  *
10  *  <drew@colorado.edu>
11  *
12  *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
13  *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
14  *     August 4, 1999 Fred Lewis, Intel DuPont
15  *
16  *  Updated to reflect the new initialization scheme for the higher 
17  *  level of scsi drivers (sd/sr/st)
18  *  September 17, 2000 Torben Mathiasen <tmm@image.dk>
19  *
20  *  Restructured scsi_host lists and associated functions.
21  *  September 04, 2002 Mike Anderson (andmike@us.ibm.com)
22  */
23
24 #include <linux/module.h>
25 #include <linux/blkdev.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/kthread.h>
29 #include <linux/string.h>
30 #include <linux/mm.h>
31 #include <linux/init.h>
32 #include <linux/completion.h>
33 #include <linux/transport_class.h>
34 #include <linux/platform_device.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/idr.h>
37 #include <scsi/scsi_device.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_transport.h>
40
41 #include "scsi_priv.h"
42 #include "scsi_logging.h"
43
44
45 static int shost_eh_deadline = -1;
46
47 module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
48 MODULE_PARM_DESC(eh_deadline,
49                  "SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
50
51 static DEFINE_IDA(host_index_ida);
52
53
54 static void scsi_host_cls_release(struct device *dev)
55 {
56         put_device(&class_to_shost(dev)->shost_gendev);
57 }
58
59 static struct class shost_class = {
60         .name           = "scsi_host",
61         .dev_release    = scsi_host_cls_release,
62 };
63
64 /**
65  *      scsi_host_set_state - Take the given host through the host state model.
66  *      @shost: scsi host to change the state of.
67  *      @state: state to change to.
68  *
69  *      Returns zero if unsuccessful or an error if the requested
70  *      transition is illegal.
71  **/
72 int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
73 {
74         enum scsi_host_state oldstate = shost->shost_state;
75
76         if (state == oldstate)
77                 return 0;
78
79         switch (state) {
80         case SHOST_CREATED:
81                 /* There are no legal states that come back to
82                  * created.  This is the manually initialised start
83                  * state */
84                 goto illegal;
85
86         case SHOST_RUNNING:
87                 switch (oldstate) {
88                 case SHOST_CREATED:
89                 case SHOST_RECOVERY:
90                         break;
91                 default:
92                         goto illegal;
93                 }
94                 break;
95
96         case SHOST_RECOVERY:
97                 switch (oldstate) {
98                 case SHOST_RUNNING:
99                         break;
100                 default:
101                         goto illegal;
102                 }
103                 break;
104
105         case SHOST_CANCEL:
106                 switch (oldstate) {
107                 case SHOST_CREATED:
108                 case SHOST_RUNNING:
109                 case SHOST_CANCEL_RECOVERY:
110                         break;
111                 default:
112                         goto illegal;
113                 }
114                 break;
115
116         case SHOST_DEL:
117                 switch (oldstate) {
118                 case SHOST_CANCEL:
119                 case SHOST_DEL_RECOVERY:
120                         break;
121                 default:
122                         goto illegal;
123                 }
124                 break;
125
126         case SHOST_CANCEL_RECOVERY:
127                 switch (oldstate) {
128                 case SHOST_CANCEL:
129                 case SHOST_RECOVERY:
130                         break;
131                 default:
132                         goto illegal;
133                 }
134                 break;
135
136         case SHOST_DEL_RECOVERY:
137                 switch (oldstate) {
138                 case SHOST_CANCEL_RECOVERY:
139                         break;
140                 default:
141                         goto illegal;
142                 }
143                 break;
144         }
145         shost->shost_state = state;
146         return 0;
147
148  illegal:
149         SCSI_LOG_ERROR_RECOVERY(1,
150                                 shost_printk(KERN_ERR, shost,
151                                              "Illegal host state transition"
152                                              "%s->%s\n",
153                                              scsi_host_state_name(oldstate),
154                                              scsi_host_state_name(state)));
155         return -EINVAL;
156 }
157
158 /**
159  * scsi_remove_host - remove a scsi host
160  * @shost:      a pointer to a scsi host to remove
161  **/
162 void scsi_remove_host(struct Scsi_Host *shost)
163 {
164         unsigned long flags;
165
166         mutex_lock(&shost->scan_mutex);
167         spin_lock_irqsave(shost->host_lock, flags);
168         if (scsi_host_set_state(shost, SHOST_CANCEL))
169                 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
170                         spin_unlock_irqrestore(shost->host_lock, flags);
171                         mutex_unlock(&shost->scan_mutex);
172                         return;
173                 }
174         spin_unlock_irqrestore(shost->host_lock, flags);
175
176         scsi_autopm_get_host(shost);
177         flush_workqueue(shost->tmf_work_q);
178         scsi_forget_host(shost);
179         mutex_unlock(&shost->scan_mutex);
180         scsi_proc_host_rm(shost);
181         scsi_proc_hostdir_rm(shost->hostt);
182
183         spin_lock_irqsave(shost->host_lock, flags);
184         if (scsi_host_set_state(shost, SHOST_DEL))
185                 BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
186         spin_unlock_irqrestore(shost->host_lock, flags);
187
188         transport_unregister_device(&shost->shost_gendev);
189         device_unregister(&shost->shost_dev);
190         device_del(&shost->shost_gendev);
191 }
192 EXPORT_SYMBOL(scsi_remove_host);
193
194 /**
195  * scsi_add_host_with_dma - add a scsi host with dma device
196  * @shost:      scsi host pointer to add
197  * @dev:        a struct device of type scsi class
198  * @dma_dev:    dma device for the host
199  *
200  * Note: You rarely need to worry about this unless you're in a
201  * virtualised host environments, so use the simpler scsi_add_host()
202  * function instead.
203  *
204  * Return value: 
205  *      0 on success / != 0 for error
206  **/
207 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
208                            struct device *dma_dev)
209 {
210         struct scsi_host_template *sht = shost->hostt;
211         int error = -EINVAL;
212
213         shost_printk(KERN_INFO, shost, "%s\n",
214                         sht->info ? sht->info(shost) : sht->name);
215
216         if (!shost->can_queue) {
217                 shost_printk(KERN_ERR, shost,
218                              "can_queue = 0 no longer supported\n");
219                 goto fail;
220         }
221
222         /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
223         shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
224                                    shost->can_queue);
225
226         error = scsi_init_sense_cache(shost);
227         if (error)
228                 goto fail;
229
230         if (shost_use_blk_mq(shost)) {
231                 error = scsi_mq_setup_tags(shost);
232                 if (error)
233                         goto fail;
234         } else {
235                 shost->bqt = blk_init_tags(shost->can_queue,
236                                 shost->hostt->tag_alloc_policy);
237                 if (!shost->bqt) {
238                         error = -ENOMEM;
239                         goto fail;
240                 }
241         }
242
243         if (!shost->shost_gendev.parent)
244                 shost->shost_gendev.parent = dev ? dev : &platform_bus;
245         if (!dma_dev)
246                 dma_dev = shost->shost_gendev.parent;
247
248         shost->dma_dev = dma_dev;
249
250         /*
251          * Increase usage count temporarily here so that calling
252          * scsi_autopm_put_host() will trigger runtime idle if there is
253          * nothing else preventing suspending the device.
254          */
255         pm_runtime_get_noresume(&shost->shost_gendev);
256         pm_runtime_set_active(&shost->shost_gendev);
257         pm_runtime_enable(&shost->shost_gendev);
258         device_enable_async_suspend(&shost->shost_gendev);
259
260         error = device_add(&shost->shost_gendev);
261         if (error)
262                 goto out_disable_runtime_pm;
263
264         scsi_host_set_state(shost, SHOST_RUNNING);
265         get_device(shost->shost_gendev.parent);
266
267         device_enable_async_suspend(&shost->shost_dev);
268
269         get_device(&shost->shost_gendev);
270         error = device_add(&shost->shost_dev);
271         if (error)
272                 goto out_del_gendev;
273
274         if (shost->transportt->host_size) {
275                 shost->shost_data = kzalloc(shost->transportt->host_size,
276                                          GFP_KERNEL);
277                 if (shost->shost_data == NULL) {
278                         error = -ENOMEM;
279                         goto out_del_dev;
280                 }
281         }
282
283         if (shost->transportt->create_work_queue) {
284                 snprintf(shost->work_q_name, sizeof(shost->work_q_name),
285                          "scsi_wq_%d", shost->host_no);
286                 shost->work_q = create_singlethread_workqueue(
287                                         shost->work_q_name);
288                 if (!shost->work_q) {
289                         error = -EINVAL;
290                         goto out_free_shost_data;
291                 }
292         }
293
294         error = scsi_sysfs_add_host(shost);
295         if (error)
296                 goto out_destroy_host;
297
298         scsi_proc_host_add(shost);
299         scsi_autopm_put_host(shost);
300         return error;
301
302  out_destroy_host:
303         if (shost->work_q)
304                 destroy_workqueue(shost->work_q);
305  out_free_shost_data:
306         kfree(shost->shost_data);
307  out_del_dev:
308         device_del(&shost->shost_dev);
309  out_del_gendev:
310         /*
311          * Host state is SHOST_RUNNING so we have to explicitly release
312          * ->shost_dev.
313          */
314         put_device(&shost->shost_dev);
315         device_del(&shost->shost_gendev);
316  out_disable_runtime_pm:
317         device_disable_async_suspend(&shost->shost_gendev);
318         pm_runtime_disable(&shost->shost_gendev);
319         pm_runtime_set_suspended(&shost->shost_gendev);
320         pm_runtime_put_noidle(&shost->shost_gendev);
321         if (shost_use_blk_mq(shost))
322                 scsi_mq_destroy_tags(shost);
323  fail:
324         return error;
325 }
326 EXPORT_SYMBOL(scsi_add_host_with_dma);
327
328 static void scsi_host_dev_release(struct device *dev)
329 {
330         struct Scsi_Host *shost = dev_to_shost(dev);
331         struct device *parent = dev->parent;
332
333         /* In case scsi_remove_host() has not been called. */
334         scsi_proc_hostdir_rm(shost->hostt);
335
336         /* Wait for functions invoked through call_rcu(&shost->rcu, ...) */
337         rcu_barrier();
338
339         if (shost->tmf_work_q)
340                 destroy_workqueue(shost->tmf_work_q);
341         if (shost->ehandler)
342                 kthread_stop(shost->ehandler);
343         if (shost->work_q)
344                 destroy_workqueue(shost->work_q);
345
346         if (shost->shost_state == SHOST_CREATED) {
347                 /*
348                  * Free the shost_dev device name here if scsi_host_alloc()
349                  * and scsi_host_put() have been called but neither
350                  * scsi_host_add() nor scsi_host_remove() has been called.
351                  * This avoids that the memory allocated for the shost_dev
352                  * name is leaked.
353                  */
354                 kfree(dev_name(&shost->shost_dev));
355         }
356
357         if (shost_use_blk_mq(shost)) {
358                 if (shost->tag_set.tags)
359                         scsi_mq_destroy_tags(shost);
360         } else {
361                 if (shost->bqt)
362                         blk_free_tags(shost->bqt);
363         }
364
365         kfree(shost->shost_data);
366
367         ida_simple_remove(&host_index_ida, shost->host_no);
368
369         if (shost->shost_state != SHOST_CREATED)
370                 put_device(parent);
371         kfree(shost);
372 }
373
374 static struct device_type scsi_host_type = {
375         .name =         "scsi_host",
376         .release =      scsi_host_dev_release,
377 };
378
379 /**
380  * scsi_host_alloc - register a scsi host adapter instance.
381  * @sht:        pointer to scsi host template
382  * @privsize:   extra bytes to allocate for driver
383  *
384  * Note:
385  *      Allocate a new Scsi_Host and perform basic initialization.
386  *      The host is not published to the scsi midlayer until scsi_add_host
387  *      is called.
388  *
389  * Return value:
390  *      Pointer to a new Scsi_Host
391  **/
392 struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
393 {
394         struct Scsi_Host *shost;
395         gfp_t gfp_mask = GFP_KERNEL;
396         int index;
397
398         if (sht->unchecked_isa_dma && privsize)
399                 gfp_mask |= __GFP_DMA;
400
401         shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
402         if (!shost)
403                 return NULL;
404
405         shost->host_lock = &shost->default_lock;
406         spin_lock_init(shost->host_lock);
407         shost->shost_state = SHOST_CREATED;
408         INIT_LIST_HEAD(&shost->__devices);
409         INIT_LIST_HEAD(&shost->__targets);
410         INIT_LIST_HEAD(&shost->eh_cmd_q);
411         INIT_LIST_HEAD(&shost->starved_list);
412         init_waitqueue_head(&shost->host_wait);
413         mutex_init(&shost->scan_mutex);
414
415         index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL);
416         if (index < 0) {
417                 kfree(shost);
418                 return NULL;
419         }
420         shost->host_no = index;
421
422         shost->dma_channel = 0xff;
423
424         /* These three are default values which can be overridden */
425         shost->max_channel = 0;
426         shost->max_id = 8;
427         shost->max_lun = 8;
428
429         /* Give each shost a default transportt */
430         shost->transportt = &blank_transport_template;
431
432         /*
433          * All drivers right now should be able to handle 12 byte
434          * commands.  Every so often there are requests for 16 byte
435          * commands, but individual low-level drivers need to certify that
436          * they actually do something sensible with such commands.
437          */
438         shost->max_cmd_len = 12;
439         shost->hostt = sht;
440         shost->this_id = sht->this_id;
441         shost->can_queue = sht->can_queue;
442         shost->sg_tablesize = sht->sg_tablesize;
443         shost->sg_prot_tablesize = sht->sg_prot_tablesize;
444         shost->cmd_per_lun = sht->cmd_per_lun;
445         shost->unchecked_isa_dma = sht->unchecked_isa_dma;
446         shost->use_clustering = sht->use_clustering;
447         shost->no_write_same = sht->no_write_same;
448
449         if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
450                 shost->eh_deadline = -1;
451         else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
452                 shost_printk(KERN_WARNING, shost,
453                              "eh_deadline %u too large, setting to %u\n",
454                              shost_eh_deadline, INT_MAX / HZ);
455                 shost->eh_deadline = INT_MAX;
456         } else
457                 shost->eh_deadline = shost_eh_deadline * HZ;
458
459         if (sht->supported_mode == MODE_UNKNOWN)
460                 /* means we didn't set it ... default to INITIATOR */
461                 shost->active_mode = MODE_INITIATOR;
462         else
463                 shost->active_mode = sht->supported_mode;
464
465         if (sht->max_host_blocked)
466                 shost->max_host_blocked = sht->max_host_blocked;
467         else
468                 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
469
470         /*
471          * If the driver imposes no hard sector transfer limit, start at
472          * machine infinity initially.
473          */
474         if (sht->max_sectors)
475                 shost->max_sectors = sht->max_sectors;
476         else
477                 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
478
479         /*
480          * assume a 4GB boundary, if not set
481          */
482         if (sht->dma_boundary)
483                 shost->dma_boundary = sht->dma_boundary;
484         else
485                 shost->dma_boundary = 0xffffffff;
486
487         shost->use_blk_mq = scsi_use_blk_mq || shost->hostt->force_blk_mq;
488
489         device_initialize(&shost->shost_gendev);
490         dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
491         shost->shost_gendev.bus = &scsi_bus_type;
492         shost->shost_gendev.type = &scsi_host_type;
493
494         device_initialize(&shost->shost_dev);
495         shost->shost_dev.parent = &shost->shost_gendev;
496         shost->shost_dev.class = &shost_class;
497         dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
498         shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
499
500         shost->ehandler = kthread_run(scsi_error_handler, shost,
501                         "scsi_eh_%d", shost->host_no);
502         if (IS_ERR(shost->ehandler)) {
503                 shost_printk(KERN_WARNING, shost,
504                         "error handler thread failed to spawn, error = %ld\n",
505                         PTR_ERR(shost->ehandler));
506                 shost->ehandler = NULL;
507                 goto fail;
508         }
509
510         shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
511                                             WQ_UNBOUND | WQ_MEM_RECLAIM,
512                                            1, shost->host_no);
513         if (!shost->tmf_work_q) {
514                 shost_printk(KERN_WARNING, shost,
515                              "failed to create tmf workq\n");
516                 goto fail;
517         }
518         scsi_proc_hostdir_add(shost->hostt);
519         return shost;
520  fail:
521         /*
522          * Host state is still SHOST_CREATED and that is enough to release
523          * ->shost_gendev. scsi_host_dev_release() will free
524          * dev_name(&shost->shost_dev).
525          */
526         put_device(&shost->shost_gendev);
527
528         return NULL;
529 }
530 EXPORT_SYMBOL(scsi_host_alloc);
531
532 static int __scsi_host_match(struct device *dev, const void *data)
533 {
534         struct Scsi_Host *p;
535         const unsigned short *hostnum = data;
536
537         p = class_to_shost(dev);
538         return p->host_no == *hostnum;
539 }
540
541 /**
542  * scsi_host_lookup - get a reference to a Scsi_Host by host no
543  * @hostnum:    host number to locate
544  *
545  * Return value:
546  *      A pointer to located Scsi_Host or NULL.
547  *
548  *      The caller must do a scsi_host_put() to drop the reference
549  *      that scsi_host_get() took. The put_device() below dropped
550  *      the reference from class_find_device().
551  **/
552 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
553 {
554         struct device *cdev;
555         struct Scsi_Host *shost = NULL;
556
557         cdev = class_find_device(&shost_class, NULL, &hostnum,
558                                  __scsi_host_match);
559         if (cdev) {
560                 shost = scsi_host_get(class_to_shost(cdev));
561                 put_device(cdev);
562         }
563         return shost;
564 }
565 EXPORT_SYMBOL(scsi_host_lookup);
566
567 /**
568  * scsi_host_get - inc a Scsi_Host ref count
569  * @shost:      Pointer to Scsi_Host to inc.
570  **/
571 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
572 {
573         if ((shost->shost_state == SHOST_DEL) ||
574                 !get_device(&shost->shost_gendev))
575                 return NULL;
576         return shost;
577 }
578 EXPORT_SYMBOL(scsi_host_get);
579
580 /**
581  * scsi_host_busy - Return the host busy counter
582  * @shost:      Pointer to Scsi_Host to inc.
583  **/
584 int scsi_host_busy(struct Scsi_Host *shost)
585 {
586         return atomic_read(&shost->host_busy);
587 }
588 EXPORT_SYMBOL(scsi_host_busy);
589
590 /**
591  * scsi_host_put - dec a Scsi_Host ref count
592  * @shost:      Pointer to Scsi_Host to dec.
593  **/
594 void scsi_host_put(struct Scsi_Host *shost)
595 {
596         put_device(&shost->shost_gendev);
597 }
598 EXPORT_SYMBOL(scsi_host_put);
599
600 int scsi_init_hosts(void)
601 {
602         return class_register(&shost_class);
603 }
604
605 void scsi_exit_hosts(void)
606 {
607         class_unregister(&shost_class);
608         ida_destroy(&host_index_ida);
609 }
610
611 int scsi_is_host_device(const struct device *dev)
612 {
613         return dev->type == &scsi_host_type;
614 }
615 EXPORT_SYMBOL(scsi_is_host_device);
616
617 /**
618  * scsi_queue_work - Queue work to the Scsi_Host workqueue.
619  * @shost:      Pointer to Scsi_Host.
620  * @work:       Work to queue for execution.
621  *
622  * Return value:
623  *      1 - work queued for execution
624  *      0 - work is already queued
625  *      -EINVAL - work queue doesn't exist
626  **/
627 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
628 {
629         if (unlikely(!shost->work_q)) {
630                 shost_printk(KERN_ERR, shost,
631                         "ERROR: Scsi host '%s' attempted to queue scsi-work, "
632                         "when no workqueue created.\n", shost->hostt->name);
633                 dump_stack();
634
635                 return -EINVAL;
636         }
637
638         return queue_work(shost->work_q, work);
639 }
640 EXPORT_SYMBOL_GPL(scsi_queue_work);
641
642 /**
643  * scsi_flush_work - Flush a Scsi_Host's workqueue.
644  * @shost:      Pointer to Scsi_Host.
645  **/
646 void scsi_flush_work(struct Scsi_Host *shost)
647 {
648         if (!shost->work_q) {
649                 shost_printk(KERN_ERR, shost,
650                         "ERROR: Scsi host '%s' attempted to flush scsi-work, "
651                         "when no workqueue created.\n", shost->hostt->name);
652                 dump_stack();
653                 return;
654         }
655
656         flush_workqueue(shost->work_q);
657 }
658 EXPORT_SYMBOL_GPL(scsi_flush_work);