GNU Linux-libre 4.19.264-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
182         spin_lock_irqsave(shost->host_lock, flags);
183         if (scsi_host_set_state(shost, SHOST_DEL))
184                 BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
185         spin_unlock_irqrestore(shost->host_lock, flags);
186
187         transport_unregister_device(&shost->shost_gendev);
188         device_unregister(&shost->shost_dev);
189         device_del(&shost->shost_gendev);
190 }
191 EXPORT_SYMBOL(scsi_remove_host);
192
193 /**
194  * scsi_add_host_with_dma - add a scsi host with dma device
195  * @shost:      scsi host pointer to add
196  * @dev:        a struct device of type scsi class
197  * @dma_dev:    dma device for the host
198  *
199  * Note: You rarely need to worry about this unless you're in a
200  * virtualised host environments, so use the simpler scsi_add_host()
201  * function instead.
202  *
203  * Return value: 
204  *      0 on success / != 0 for error
205  **/
206 int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
207                            struct device *dma_dev)
208 {
209         struct scsi_host_template *sht = shost->hostt;
210         int error = -EINVAL;
211
212         shost_printk(KERN_INFO, shost, "%s\n",
213                         sht->info ? sht->info(shost) : sht->name);
214
215         if (!shost->can_queue) {
216                 shost_printk(KERN_ERR, shost,
217                              "can_queue = 0 no longer supported\n");
218                 goto fail;
219         }
220
221         /* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
222         shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
223                                    shost->can_queue);
224
225         error = scsi_init_sense_cache(shost);
226         if (error)
227                 goto fail;
228
229         if (shost_use_blk_mq(shost)) {
230                 error = scsi_mq_setup_tags(shost);
231                 if (error)
232                         goto fail;
233         } else {
234                 shost->bqt = blk_init_tags(shost->can_queue,
235                                 shost->hostt->tag_alloc_policy);
236                 if (!shost->bqt) {
237                         error = -ENOMEM;
238                         goto fail;
239                 }
240         }
241
242         if (!shost->shost_gendev.parent)
243                 shost->shost_gendev.parent = dev ? dev : &platform_bus;
244         if (!dma_dev)
245                 dma_dev = shost->shost_gendev.parent;
246
247         shost->dma_dev = dma_dev;
248
249         /*
250          * Increase usage count temporarily here so that calling
251          * scsi_autopm_put_host() will trigger runtime idle if there is
252          * nothing else preventing suspending the device.
253          */
254         pm_runtime_get_noresume(&shost->shost_gendev);
255         pm_runtime_set_active(&shost->shost_gendev);
256         pm_runtime_enable(&shost->shost_gendev);
257         device_enable_async_suspend(&shost->shost_gendev);
258
259         error = device_add(&shost->shost_gendev);
260         if (error)
261                 goto out_disable_runtime_pm;
262
263         scsi_host_set_state(shost, SHOST_RUNNING);
264         get_device(shost->shost_gendev.parent);
265
266         device_enable_async_suspend(&shost->shost_dev);
267
268         get_device(&shost->shost_gendev);
269         error = device_add(&shost->shost_dev);
270         if (error)
271                 goto out_del_gendev;
272
273         if (shost->transportt->host_size) {
274                 shost->shost_data = kzalloc(shost->transportt->host_size,
275                                          GFP_KERNEL);
276                 if (shost->shost_data == NULL) {
277                         error = -ENOMEM;
278                         goto out_del_dev;
279                 }
280         }
281
282         if (shost->transportt->create_work_queue) {
283                 snprintf(shost->work_q_name, sizeof(shost->work_q_name),
284                          "scsi_wq_%d", shost->host_no);
285                 shost->work_q = create_singlethread_workqueue(
286                                         shost->work_q_name);
287                 if (!shost->work_q) {
288                         error = -EINVAL;
289                         goto out_free_shost_data;
290                 }
291         }
292
293         error = scsi_sysfs_add_host(shost);
294         if (error)
295                 goto out_destroy_host;
296
297         scsi_proc_host_add(shost);
298         scsi_autopm_put_host(shost);
299         return error;
300
301  out_destroy_host:
302         if (shost->work_q)
303                 destroy_workqueue(shost->work_q);
304  out_free_shost_data:
305         kfree(shost->shost_data);
306  out_del_dev:
307         device_del(&shost->shost_dev);
308  out_del_gendev:
309         /*
310          * Host state is SHOST_RUNNING so we have to explicitly release
311          * ->shost_dev.
312          */
313         put_device(&shost->shost_dev);
314         device_del(&shost->shost_gendev);
315  out_disable_runtime_pm:
316         device_disable_async_suspend(&shost->shost_gendev);
317         pm_runtime_disable(&shost->shost_gendev);
318         pm_runtime_set_suspended(&shost->shost_gendev);
319         pm_runtime_put_noidle(&shost->shost_gendev);
320         if (shost_use_blk_mq(shost))
321                 scsi_mq_destroy_tags(shost);
322  fail:
323         return error;
324 }
325 EXPORT_SYMBOL(scsi_add_host_with_dma);
326
327 static void scsi_host_dev_release(struct device *dev)
328 {
329         struct Scsi_Host *shost = dev_to_shost(dev);
330         struct device *parent = dev->parent;
331
332         scsi_proc_hostdir_rm(shost->hostt);
333
334         /* Wait for functions invoked through call_rcu(&shost->rcu, ...) */
335         rcu_barrier();
336
337         if (shost->tmf_work_q)
338                 destroy_workqueue(shost->tmf_work_q);
339         if (shost->ehandler)
340                 kthread_stop(shost->ehandler);
341         if (shost->work_q)
342                 destroy_workqueue(shost->work_q);
343
344         if (shost->shost_state == SHOST_CREATED) {
345                 /*
346                  * Free the shost_dev device name here if scsi_host_alloc()
347                  * and scsi_host_put() have been called but neither
348                  * scsi_host_add() nor scsi_host_remove() has been called.
349                  * This avoids that the memory allocated for the shost_dev
350                  * name is leaked.
351                  */
352                 kfree(dev_name(&shost->shost_dev));
353         }
354
355         if (shost_use_blk_mq(shost)) {
356                 if (shost->tag_set.tags)
357                         scsi_mq_destroy_tags(shost);
358         } else {
359                 if (shost->bqt)
360                         blk_free_tags(shost->bqt);
361         }
362
363         kfree(shost->shost_data);
364
365         ida_simple_remove(&host_index_ida, shost->host_no);
366
367         if (shost->shost_state != SHOST_CREATED)
368                 put_device(parent);
369         kfree(shost);
370 }
371
372 static struct device_type scsi_host_type = {
373         .name =         "scsi_host",
374         .release =      scsi_host_dev_release,
375 };
376
377 /**
378  * scsi_host_alloc - register a scsi host adapter instance.
379  * @sht:        pointer to scsi host template
380  * @privsize:   extra bytes to allocate for driver
381  *
382  * Note:
383  *      Allocate a new Scsi_Host and perform basic initialization.
384  *      The host is not published to the scsi midlayer until scsi_add_host
385  *      is called.
386  *
387  * Return value:
388  *      Pointer to a new Scsi_Host
389  **/
390 struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
391 {
392         struct Scsi_Host *shost;
393         gfp_t gfp_mask = GFP_KERNEL;
394         int index;
395
396         if (sht->unchecked_isa_dma && privsize)
397                 gfp_mask |= __GFP_DMA;
398
399         shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
400         if (!shost)
401                 return NULL;
402
403         shost->host_lock = &shost->default_lock;
404         spin_lock_init(shost->host_lock);
405         shost->shost_state = SHOST_CREATED;
406         INIT_LIST_HEAD(&shost->__devices);
407         INIT_LIST_HEAD(&shost->__targets);
408         INIT_LIST_HEAD(&shost->eh_cmd_q);
409         INIT_LIST_HEAD(&shost->starved_list);
410         init_waitqueue_head(&shost->host_wait);
411         mutex_init(&shost->scan_mutex);
412
413         index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL);
414         if (index < 0) {
415                 kfree(shost);
416                 return NULL;
417         }
418         shost->host_no = index;
419
420         shost->dma_channel = 0xff;
421
422         /* These three are default values which can be overridden */
423         shost->max_channel = 0;
424         shost->max_id = 8;
425         shost->max_lun = 8;
426
427         /* Give each shost a default transportt */
428         shost->transportt = &blank_transport_template;
429
430         /*
431          * All drivers right now should be able to handle 12 byte
432          * commands.  Every so often there are requests for 16 byte
433          * commands, but individual low-level drivers need to certify that
434          * they actually do something sensible with such commands.
435          */
436         shost->max_cmd_len = 12;
437         shost->hostt = sht;
438         shost->this_id = sht->this_id;
439         shost->can_queue = sht->can_queue;
440         shost->sg_tablesize = sht->sg_tablesize;
441         shost->sg_prot_tablesize = sht->sg_prot_tablesize;
442         shost->cmd_per_lun = sht->cmd_per_lun;
443         shost->unchecked_isa_dma = sht->unchecked_isa_dma;
444         shost->use_clustering = sht->use_clustering;
445         shost->no_write_same = sht->no_write_same;
446
447         if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
448                 shost->eh_deadline = -1;
449         else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
450                 shost_printk(KERN_WARNING, shost,
451                              "eh_deadline %u too large, setting to %u\n",
452                              shost_eh_deadline, INT_MAX / HZ);
453                 shost->eh_deadline = INT_MAX;
454         } else
455                 shost->eh_deadline = shost_eh_deadline * HZ;
456
457         if (sht->supported_mode == MODE_UNKNOWN)
458                 /* means we didn't set it ... default to INITIATOR */
459                 shost->active_mode = MODE_INITIATOR;
460         else
461                 shost->active_mode = sht->supported_mode;
462
463         if (sht->max_host_blocked)
464                 shost->max_host_blocked = sht->max_host_blocked;
465         else
466                 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
467
468         /*
469          * If the driver imposes no hard sector transfer limit, start at
470          * machine infinity initially.
471          */
472         if (sht->max_sectors)
473                 shost->max_sectors = sht->max_sectors;
474         else
475                 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
476
477         /*
478          * assume a 4GB boundary, if not set
479          */
480         if (sht->dma_boundary)
481                 shost->dma_boundary = sht->dma_boundary;
482         else
483                 shost->dma_boundary = 0xffffffff;
484
485         shost->use_blk_mq = scsi_use_blk_mq || shost->hostt->force_blk_mq;
486
487         device_initialize(&shost->shost_gendev);
488         dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
489         shost->shost_gendev.bus = &scsi_bus_type;
490         shost->shost_gendev.type = &scsi_host_type;
491
492         device_initialize(&shost->shost_dev);
493         shost->shost_dev.parent = &shost->shost_gendev;
494         shost->shost_dev.class = &shost_class;
495         dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
496         shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
497
498         shost->ehandler = kthread_run(scsi_error_handler, shost,
499                         "scsi_eh_%d", shost->host_no);
500         if (IS_ERR(shost->ehandler)) {
501                 shost_printk(KERN_WARNING, shost,
502                         "error handler thread failed to spawn, error = %ld\n",
503                         PTR_ERR(shost->ehandler));
504                 shost->ehandler = NULL;
505                 goto fail;
506         }
507
508         shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
509                                             WQ_UNBOUND | WQ_MEM_RECLAIM,
510                                            1, shost->host_no);
511         if (!shost->tmf_work_q) {
512                 shost_printk(KERN_WARNING, shost,
513                              "failed to create tmf workq\n");
514                 goto fail;
515         }
516         scsi_proc_hostdir_add(shost->hostt);
517         return shost;
518  fail:
519         /*
520          * Host state is still SHOST_CREATED and that is enough to release
521          * ->shost_gendev. scsi_host_dev_release() will free
522          * dev_name(&shost->shost_dev).
523          */
524         put_device(&shost->shost_gendev);
525
526         return NULL;
527 }
528 EXPORT_SYMBOL(scsi_host_alloc);
529
530 static int __scsi_host_match(struct device *dev, const void *data)
531 {
532         struct Scsi_Host *p;
533         const unsigned short *hostnum = data;
534
535         p = class_to_shost(dev);
536         return p->host_no == *hostnum;
537 }
538
539 /**
540  * scsi_host_lookup - get a reference to a Scsi_Host by host no
541  * @hostnum:    host number to locate
542  *
543  * Return value:
544  *      A pointer to located Scsi_Host or NULL.
545  *
546  *      The caller must do a scsi_host_put() to drop the reference
547  *      that scsi_host_get() took. The put_device() below dropped
548  *      the reference from class_find_device().
549  **/
550 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
551 {
552         struct device *cdev;
553         struct Scsi_Host *shost = NULL;
554
555         cdev = class_find_device(&shost_class, NULL, &hostnum,
556                                  __scsi_host_match);
557         if (cdev) {
558                 shost = scsi_host_get(class_to_shost(cdev));
559                 put_device(cdev);
560         }
561         return shost;
562 }
563 EXPORT_SYMBOL(scsi_host_lookup);
564
565 /**
566  * scsi_host_get - inc a Scsi_Host ref count
567  * @shost:      Pointer to Scsi_Host to inc.
568  **/
569 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
570 {
571         if ((shost->shost_state == SHOST_DEL) ||
572                 !get_device(&shost->shost_gendev))
573                 return NULL;
574         return shost;
575 }
576 EXPORT_SYMBOL(scsi_host_get);
577
578 /**
579  * scsi_host_busy - Return the host busy counter
580  * @shost:      Pointer to Scsi_Host to inc.
581  **/
582 int scsi_host_busy(struct Scsi_Host *shost)
583 {
584         return atomic_read(&shost->host_busy);
585 }
586 EXPORT_SYMBOL(scsi_host_busy);
587
588 /**
589  * scsi_host_put - dec a Scsi_Host ref count
590  * @shost:      Pointer to Scsi_Host to dec.
591  **/
592 void scsi_host_put(struct Scsi_Host *shost)
593 {
594         put_device(&shost->shost_gendev);
595 }
596 EXPORT_SYMBOL(scsi_host_put);
597
598 int scsi_init_hosts(void)
599 {
600         return class_register(&shost_class);
601 }
602
603 void scsi_exit_hosts(void)
604 {
605         class_unregister(&shost_class);
606         ida_destroy(&host_index_ida);
607 }
608
609 int scsi_is_host_device(const struct device *dev)
610 {
611         return dev->type == &scsi_host_type;
612 }
613 EXPORT_SYMBOL(scsi_is_host_device);
614
615 /**
616  * scsi_queue_work - Queue work to the Scsi_Host workqueue.
617  * @shost:      Pointer to Scsi_Host.
618  * @work:       Work to queue for execution.
619  *
620  * Return value:
621  *      1 - work queued for execution
622  *      0 - work is already queued
623  *      -EINVAL - work queue doesn't exist
624  **/
625 int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
626 {
627         if (unlikely(!shost->work_q)) {
628                 shost_printk(KERN_ERR, shost,
629                         "ERROR: Scsi host '%s' attempted to queue scsi-work, "
630                         "when no workqueue created.\n", shost->hostt->name);
631                 dump_stack();
632
633                 return -EINVAL;
634         }
635
636         return queue_work(shost->work_q, work);
637 }
638 EXPORT_SYMBOL_GPL(scsi_queue_work);
639
640 /**
641  * scsi_flush_work - Flush a Scsi_Host's workqueue.
642  * @shost:      Pointer to Scsi_Host.
643  **/
644 void scsi_flush_work(struct Scsi_Host *shost)
645 {
646         if (!shost->work_q) {
647                 shost_printk(KERN_ERR, shost,
648                         "ERROR: Scsi host '%s' attempted to flush scsi-work, "
649                         "when no workqueue created.\n", shost->hostt->name);
650                 dump_stack();
651                 return;
652         }
653
654         flush_workqueue(shost->work_q);
655 }
656 EXPORT_SYMBOL_GPL(scsi_flush_work);