OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [scsi/] [scsi.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
2
 *  scsi.c Copyright (C) 1992 Drew Eckhardt
3
 *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
4
 *
5
 *  generic mid-level SCSI driver
6
 *      Initial versions: Drew Eckhardt
7
 *      Subsequent revisions: Eric Youngdale
8
 *
9
 *  <drew@colorado.edu>
10
 *
11
 *  Bug correction thanks go to :
12
 *      Rik Faith <faith@cs.unc.edu>
13
 *      Tommy Thorn <tthorn>
14
 *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
15
 *
16
 *  Modified by Eric Youngdale eric@aib.com to
17
 *  add scatter-gather, multiple outstanding request, and other
18
 *  enhancements.
19
 *
20
 *  Native multichannel, wide scsi, /proc/scsi and hot plugging
21
 *  support added by Michael Neuffer <mike@i-connect.net>
22
 *
23
 *  Added request_module("scsi_hostadapter") for kerneld:
24
 *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/conf.modules)
25
 *  Bjorn Ekwall  <bj0rn@blox.se>
26
 *
27
 *  Major improvements to the timeout, abort, and reset processing,
28
 *  as well as performance modifications for large queue depths by
29
 *  Leonard N. Zubkoff <lnz@dandelion.com>
30
 */
31
 
32
/*
33
 * Don't import our own symbols, as this would severely mess up our
34
 * symbol tables.
35
 */
36
#define _SCSI_SYMS_VER_
37
 
38
#include <linux/config.h>
39
#include <linux/module.h>
40
 
41
#include <linux/sched.h>
42
#include <linux/timer.h>
43
#include <linux/string.h>
44
#include <linux/malloc.h>
45
#include <linux/ioport.h>
46
#include <linux/kernel.h>
47
#include <linux/stat.h>
48
#include <linux/blk.h>
49
#include <linux/interrupt.h>
50
#include <linux/delay.h>
51
 
52
#include <asm/system.h>
53
#include <asm/irq.h>
54
#include <asm/dma.h>
55
 
56
#include "scsi.h"
57
#include "hosts.h"
58
#include "constants.h"
59
 
60
#ifdef CONFIG_KERNELD
61
#include <linux/kerneld.h>
62
#endif
63
 
64
#undef USE_STATIC_SCSI_MEMORY
65
 
66
/*
67
static const char RCSid[] = "$Header: /home/marcus/revision_ctrl_test/oc_cvs/cvs/or1k/rc203soc/sw/uClinux/drivers/scsi/scsi.c,v 1.1 2005-12-20 10:17:45 jcastillo Exp $";
68
*/
69
 
70
 
71
/* Command groups 3 and 4 are reserved and should never be used.  */
72
const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
73
 
74
#define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
75
 
76
/*
77
 * PAGE_SIZE must be a multiple of the sector size (512).  True
78
 * for all reasonably recent architectures (even the VAX...).
79
 */
80
#define SECTOR_SIZE             512
81
#define SECTORS_PER_PAGE        (PAGE_SIZE/SECTOR_SIZE)
82
 
83
#if SECTORS_PER_PAGE <= 8
84
 typedef unsigned char  FreeSectorBitmap;
85
#elif SECTORS_PER_PAGE <= 32
86
 typedef unsigned int   FreeSectorBitmap;
87
#else
88
# error You lose.
89
#endif
90
 
91
static void scsi_done (Scsi_Cmnd *SCpnt);
92
static int update_timeout (Scsi_Cmnd *, int);
93
static void print_inquiry(unsigned char *data);
94
static void scsi_times_out (Scsi_Cmnd * SCpnt);
95
static int scan_scsis_single (int channel,int dev,int lun,int * max_scsi_dev ,
96
                 int * sparse_lun, Scsi_Device ** SDpnt, Scsi_Cmnd * SCpnt,
97
                 struct Scsi_Host *shpnt, char * scsi_result);
98
void scsi_build_commandblocks(Scsi_Device * SDpnt);
99
 
100
#ifdef CONFIG_MODULES
101
extern struct symbol_table scsi_symbol_table;
102
#endif
103
 
104
static FreeSectorBitmap * dma_malloc_freelist = NULL;
105
static int scsi_need_isa_bounce_buffers;
106
static unsigned int dma_sectors = 0;
107
unsigned int dma_free_sectors = 0;
108
unsigned int need_isa_buffer = 0;
109
static unsigned char ** dma_malloc_pages = NULL;
110
 
111
static int time_start;
112
static int time_elapsed;
113
static volatile struct Scsi_Host * host_active = NULL;
114
#define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
115
                          || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
116
 
117
const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
118
{
119
    "Direct-Access    ",
120
    "Sequential-Access",
121
    "Printer          ",
122
    "Processor        ",
123
    "WORM             ",
124
    "CD-ROM           ",
125
    "Scanner          ",
126
    "Optical Device   ",
127
    "Medium Changer   ",
128
    "Communications   "
129
};
130
 
131
 
132
/*
133
 * global variables :
134
 * scsi_devices an array of these specifying the address for each
135
 * (host, id, LUN)
136
 */
137
 
138
Scsi_Device * scsi_devices = NULL;
139
 
140
/* Process ID of SCSI commands */
141
unsigned long scsi_pid = 0;
142
 
143
static unsigned long serial_number = 0;
144
 
145
static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
146
static void resize_dma_pool(void);
147
 
148
/* This variable is merely a hook so that we can debug the kernel with gdb. */
149
Scsi_Cmnd * last_cmnd = NULL;
150
 
151
/* This is the pointer to the /proc/scsi code.
152
 * It is only initialized to !=0 if the scsi code is present
153
 */
154
#if CONFIG_PROC_FS 
155
extern int (* dispatch_scsi_info_ptr)(int ino, char *buffer, char **start,
156
                                      off_t offset, int length, int inout);
157
extern int dispatch_scsi_info(int ino, char *buffer, char **start,
158
                              off_t offset, int length, int inout);
159
 
160
struct proc_dir_entry proc_scsi_scsi = {
161
    PROC_SCSI_SCSI, 4, "scsi",
162
    S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0,
163
    NULL,
164
    NULL, NULL,
165
    NULL, NULL, NULL
166
};
167
#endif
168
 
169
/*
170
 *  This is the number  of clock ticks we should wait before we time out
171
 *  and abort the command.  This is for  where the scsi.c module generates
172
 *  the command, not where it originates from a higher level, in which
173
 *  case the timeout is specified there.
174
 *
175
 *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
176
 *  respectively.
177
 */
178
 
179
#ifdef DEBUG_TIMEOUT
180
static void scsi_dump_status(void);
181
#endif
182
 
183
 
184
#ifdef DEBUG
185
    #define SCSI_TIMEOUT (5*HZ)
186
#else
187
    #define SCSI_TIMEOUT (2*HZ)
188
#endif
189
 
190
#ifdef DEBUG
191
    #define SENSE_TIMEOUT SCSI_TIMEOUT
192
    #define ABORT_TIMEOUT SCSI_TIMEOUT
193
    #define RESET_TIMEOUT SCSI_TIMEOUT
194
#else
195
    #define SENSE_TIMEOUT (5*HZ/10)
196
    #define RESET_TIMEOUT (5*HZ/10)
197
    #define ABORT_TIMEOUT (5*HZ/10)
198
#endif
199
 
200
#define MIN_RESET_DELAY (2*HZ)
201
 
202
/* Do not call reset on error if we just did a reset within 15 sec. */
203
#define MIN_RESET_PERIOD (15*HZ)
204
 
205
/* The following devices are known not to tolerate a lun != 0 scan for
206
 * one reason or another.  Some will respond to all luns, others will
207
 * lock up.
208
 */
209
 
210
#define BLIST_NOLUN     0x01
211
#define BLIST_FORCELUN  0x02
212
#define BLIST_BORKEN    0x04
213
#define BLIST_KEY       0x08
214
#define BLIST_SINGLELUN 0x10
215
#define BLIST_NOTQ      0x20
216
#define BLIST_SPARSELUN 0x40
217
#define BLIST_MAX5LUN   0x80
218
 
219
struct dev_info{
220
    const char * vendor;
221
    const char * model;
222
    const char * revision; /* Latest revision known to be bad.  Not used yet */
223
    unsigned flags;
224
};
225
 
226
/*
227
 * This is what was previously known as the blacklist.  The concept
228
 * has been expanded so that we can specify other types of things we
229
 * need to be aware of.
230
 */
231
static struct dev_info device_list[] =
232
{
233
{"TEAC","CD-R55S","1.0H", BLIST_NOLUN},         /* Locks up if polled for lun != 0 */
234
{"CHINON","CD-ROM CDS-431","H42", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
235
{"CHINON","CD-ROM CDS-535","Q14", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
236
{"DENON","DRD-25X","V", BLIST_NOLUN},           /* Locks up if probed for lun != 0 */
237
{"HITACHI","DK312C","CM81", BLIST_NOLUN},       /* Responds to all lun - dtg */
238
{"HITACHI","DK314C","CR21" , BLIST_NOLUN},      /* responds to all lun */
239
{"IMS", "CDD521/10","2.06", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
240
{"MAXTOR","XT-3280","PR02", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
241
{"MAXTOR","XT-4380S","B3C", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
242
{"MAXTOR","MXT-1240S","I1.2", BLIST_NOLUN},     /* Locks up when LUN>0 polled */
243
{"MAXTOR","XT-4170S","B5A", BLIST_NOLUN},       /* Locks-up sometimes when LUN>0 polled. */
244
{"MAXTOR","XT-8760S","B7B", BLIST_NOLUN},       /* guess what? */
245
{"MEDIAVIS","RENO CD-ROMX2A","2.03",BLIST_NOLUN},/*Responds to all lun */
246
{"MICROP", "4110", "*", BLIST_NOTQ},            /* Buggy Tagged Queuing */
247
{"NEC","CD-ROM DRIVE:841","1.0", BLIST_NOLUN},  /* Locks-up when LUN>0 polled. */
248
{"RODIME","RO3000S","2.33", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
249
{"SANYO", "CRD-250S", "1.20", BLIST_NOLUN},     /* causes failed REQUEST SENSE on lun 1
250
                                                 * for aha152x controller, which causes
251
                                                 * SCSI code to reset bus.*/
252
{"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},   /* causes failed REQUEST SENSE on lun 1
253
                                                 * for aha152x controller, which causes
254
                                                 * SCSI code to reset bus.*/
255
{"SEAGATE", "ST296","921", BLIST_NOLUN},        /* Responds to all lun */
256
{"SEAGATE","ST1581","6538",BLIST_NOLUN},        /* Responds to all lun */
257
{"SONY","CD-ROM CDU-541","4.3d", BLIST_NOLUN},
258
{"SONY","CD-ROM CDU-55S","1.0i", BLIST_NOLUN},
259
{"SONY","CD-ROM CDU-561","1.7x", BLIST_NOLUN},
260
{"TANDBERG","TDC 3600","U07", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
261
{"TEAC","CD-ROM","1.06", BLIST_NOLUN},          /* causes failed REQUEST SENSE on lun 1
262
                                                 * for seagate controller, which causes
263
                                                 * SCSI code to reset bus.*/
264
{"TEXEL","CD-ROM","1.06", BLIST_NOLUN},         /* causes failed REQUEST SENSE on lun 1
265
                                                 * for seagate controller, which causes
266
                                                 * SCSI code to reset bus.*/
267
{"QUANTUM","LPS525S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
268
{"QUANTUM","PD1225S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
269
{"MEDIAVIS","CDR-H93MV","1.31", BLIST_NOLUN},   /* Locks up if polled for lun != 0 */
270
{"SANKYO", "CP525","6.64", BLIST_NOLUN},        /* causes failed REQ SENSE, extra reset */
271
{"HP", "C1750A", "3226", BLIST_NOLUN},          /* scanjet iic */
272
{"HP", "C1790A", "", BLIST_NOLUN},              /* scanjet iip */
273
{"HP", "C2500A", "", BLIST_NOLUN},              /* scanjet iicx */
274
 
275
/*
276
 * Other types of devices that have special flags.
277
 */
278
{"SONY","CD-ROM CDU-8001","*", BLIST_BORKEN},
279
{"TEXEL","CD-ROM","1.06", BLIST_BORKEN},
280
{"IOMEGA","Io20S         *F","*", BLIST_KEY},
281
{"INSITE","Floptical   F*8I","*", BLIST_KEY},
282
{"INSITE","I325VM","*", BLIST_KEY},
283
{"NRC","MBR-7","*", BLIST_FORCELUN | BLIST_SINGLELUN},
284
{"NRC","MBR-7.4","*", BLIST_FORCELUN | BLIST_SINGLELUN},
285
{"REGAL","CDC-4X","*", BLIST_MAX5LUN | BLIST_SINGLELUN},
286
{"NAKAMICH","MJ-4.8S","*", BLIST_FORCELUN | BLIST_SINGLELUN},
287
{"NAKAMICH","MJ-5.16S","*", BLIST_FORCELUN | BLIST_SINGLELUN},
288
{"PIONEER","CD-ROM DRM-600","*", BLIST_FORCELUN | BLIST_SINGLELUN},
289
{"PIONEER","CD-ROM DRM-602X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
290
{"PIONEER","CD-ROM DRM-604X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
291
{"PIONEER","CD-ROM DRM-1804X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
292
{"EMULEX","MD21/S2     ESDI","*", BLIST_SINGLELUN},
293
{"CANON","IPUBJD","*", BLIST_SPARSELUN},
294
{"MATSHITA","PD","*", BLIST_FORCELUN | BLIST_SINGLELUN},
295
{"YAMAHA","CDR100","1.00", BLIST_NOLUN},        /* Locks up if polled for lun != 0 */
296
{"YAMAHA","CDR102","1.00", BLIST_NOLUN},        /* Locks up if polled for lun != 0 */
297
{"nCipher","Fastness Crypto","*", BLIST_FORCELUN},
298
{"iomega","jaz 1GB","J.86", BLIST_NOTQ | BLIST_NOLUN},
299
/*
300
 * Must be at end of list...
301
 */
302
{NULL, NULL, NULL}
303
};
304
 
305
static int get_device_flags(unsigned char * response_data){
306
    int i = 0;
307
    unsigned char * pnt;
308
    for(i=0; 1; i++){
309
        if(device_list[i].vendor == NULL) return 0;
310
        pnt = &response_data[8];
311
        while(*pnt && *pnt == ' ') pnt++;
312
        if(memcmp(device_list[i].vendor, pnt,
313
                  strlen(device_list[i].vendor))) continue;
314
        pnt = &response_data[16];
315
        while(*pnt && *pnt == ' ') pnt++;
316
        if(memcmp(device_list[i].model, pnt,
317
                  strlen(device_list[i].model))) continue;
318
        return device_list[i].flags;
319
    }
320
    return 0;
321
}
322
 
323
void scsi_make_blocked_list(void)  {
324
    int block_count = 0, index;
325
    unsigned long flags;
326
    struct Scsi_Host * sh[128], * shpnt;
327
 
328
    /*
329
     * Create a circular linked list from the scsi hosts which have
330
     * the "wish_block" field in the Scsi_Host structure set.
331
     * The blocked list should include all the scsi hosts using ISA DMA.
332
     * In some systems, using two dma channels simultaneously causes
333
     * unpredictable results.
334
     * Among the scsi hosts in the blocked list, only one host at a time
335
     * is allowed to have active commands queued. The transition from
336
     * one active host to the next one is allowed only when host_busy == 0
337
     * for the active host (which implies host_busy == 0 for all the hosts
338
     * in the list). Moreover for block devices the transition to a new
339
     * active host is allowed only when a request is completed, since a
340
     * block device request can be divided into multiple scsi commands
341
     * (when there are few sg lists or clustering is disabled).
342
     *
343
     * (DB, 4 Feb 1995)
344
     */
345
 
346
    save_flags(flags);
347
    cli();
348
    host_active = NULL;
349
 
350
    for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {
351
 
352
#if 0
353
        /*
354
         * Is this is a candidate for the blocked list?
355
         * Useful to put into the blocked list all the hosts whose driver
356
         * does not know about the host->block feature.
357
         */
358
        if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
359
#endif
360
 
361
        if (shpnt->wish_block) sh[block_count++] = shpnt;
362
    }
363
 
364
    if (block_count == 1) sh[0]->block = NULL;
365
 
366
    else if (block_count > 1) {
367
 
368
        for(index = 0; index < block_count - 1; index++) {
369
            sh[index]->block = sh[index + 1];
370
            printk("scsi%d : added to blocked host list.\n",
371
                   sh[index]->host_no);
372
        }
373
 
374
        sh[block_count - 1]->block = sh[0];
375
        printk("scsi%d : added to blocked host list.\n",
376
               sh[index]->host_no);
377
    }
378
 
379
    restore_flags(flags);
380
}
381
 
382
static void scan_scsis_done (Scsi_Cmnd * SCpnt)
383
{
384
 
385
#ifdef DEBUG
386
    printk ("scan_scsis_done(%p, %06x)\n", SCpnt->host, SCpnt->result);
387
#endif
388
    SCpnt->request.rq_status = RQ_SCSI_DONE;
389
 
390
    if (SCpnt->request.sem != NULL)
391
        up(SCpnt->request.sem);
392
}
393
 
394
#ifdef CONFIG_SCSI_MULTI_LUN
395
static int max_scsi_luns = 8;
396
#else
397
static int max_scsi_luns = 1;
398
#endif
399
 
400
void scsi_luns_setup(char *str, int *ints) {
401
    if (ints[0] != 1)
402
        printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
403
    else
404
        max_scsi_luns = ints[1];
405
}
406
 
407
/*
408
 *  Detecting SCSI devices :
409
 *  We scan all present host adapter's busses,  from ID 0 to ID (max_id).
410
 *  We use the INQUIRY command, determine device type, and pass the ID /
411
 *  lun address of all sequential devices to the tape driver, all random
412
 *  devices to the disk driver.
413
 */
414
static void scan_scsis (struct Scsi_Host *shpnt, unchar hardcoded,
415
                 unchar hchannel, unchar hid, unchar hlun)
416
{
417
  int dev, lun, channel;
418
  unsigned char scsi_result0[256];
419
  unsigned char *scsi_result;
420
  Scsi_Device *SDpnt;
421
  int max_dev_lun, sparse_lun;
422
  Scsi_Cmnd *SCpnt;
423
 
424
  SCpnt = (Scsi_Cmnd *) scsi_init_malloc (sizeof (Scsi_Cmnd), GFP_ATOMIC | GFP_DMA);
425
  SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
426
  memset (SCpnt, 0, sizeof (Scsi_Cmnd));
427
 
428
 
429
  /* Make sure we have something that is valid for DMA purposes */
430
  scsi_result = ( ( !shpnt->unchecked_isa_dma )
431
                 ? &scsi_result0[0] : scsi_init_malloc (512, GFP_DMA));
432
 
433
  if (scsi_result == NULL) {
434
    printk ("Unable to obtain scsi_result buffer\n");
435
    goto leave;
436
  }
437
 
438
  /* We must chain ourself in the host_queue, so commands can time out */
439
  if(shpnt->host_queue)
440
      shpnt->host_queue->prev = SCpnt;
441
  SCpnt->next = shpnt->host_queue;
442
  SCpnt->prev = NULL;
443
  shpnt->host_queue = SCpnt;
444
 
445
 
446
  if (hardcoded == 1) {
447
    Scsi_Device *oldSDpnt=SDpnt;
448
    struct Scsi_Device_Template * sdtpnt;
449
    channel = hchannel;
450
    if(channel > shpnt->max_channel) goto leave;
451
    dev = hid;
452
    if(dev >= shpnt->max_id) goto leave;
453
    lun = hlun;
454
    if(lun >= shpnt->max_lun) goto leave;
455
    scan_scsis_single (channel, dev, lun, &max_dev_lun, &sparse_lun,
456
                       &SDpnt, SCpnt, shpnt, scsi_result);
457
    if(SDpnt!=oldSDpnt) {
458
 
459
        /* it could happen the blockdevice hasn't yet been inited */
460
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
461
        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
462
 
463
            oldSDpnt->scsi_request_fn = NULL;
464
            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
465
                if(sdtpnt->attach) {
466
                  (*sdtpnt->attach)(oldSDpnt);
467
                  if(oldSDpnt->attached) scsi_build_commandblocks(oldSDpnt);}
468
            resize_dma_pool();
469
 
470
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
471
            if(sdtpnt->finish && sdtpnt->nr_dev)
472
                {(*sdtpnt->finish)();}
473
        }
474
    }
475
 
476
  }
477
  else {
478
    for (channel = 0; channel <= shpnt->max_channel; channel++) {
479
      for (dev = 0; dev < shpnt->max_id; ++dev) {
480
        if (shpnt->this_id != dev) {
481
 
482
          /*
483
           * We need the for so our continue, etc. work fine. We put this in
484
           * a variable so that we can override it during the scan if we
485
           * detect a device *KNOWN* to have multiple logical units.
486
           */
487
          max_dev_lun = (max_scsi_luns < shpnt->max_lun ?
488
                         max_scsi_luns : shpnt->max_lun);
489
          sparse_lun = 0;
490
          for (lun = 0; lun < max_dev_lun; ++lun) {
491
            if (!scan_scsis_single (channel, dev, lun, &max_dev_lun,
492
                                    &sparse_lun, &SDpnt, SCpnt, shpnt,
493
                                    scsi_result)
494
                && !sparse_lun)
495
              break; /* break means don't probe further for luns!=0 */
496
          }                     /* for lun ends */
497
        }                       /* if this_id != id ends */
498
      }                         /* for dev ends */
499
    }                           /* for channel ends */
500
  }                             /* if/else hardcoded */
501
 
502
  leave:
503
 
504
  {/* Unchain SCpnt from host_queue */
505
    Scsi_Cmnd *prev, *next, *hqptr;
506
    for(hqptr = shpnt->host_queue; hqptr != SCpnt; hqptr = hqptr->next) ;
507
    if(hqptr) {
508
      prev = hqptr->prev;
509
      next = hqptr->next;
510
      if(prev)
511
        prev->next = next;
512
      else
513
        shpnt->host_queue = next;
514
      if(next) next->prev = prev;
515
    }
516
  }
517
 
518
     /* Last device block does not exist.  Free memory. */
519
    if (SDpnt != NULL)
520
      scsi_init_free ((char *) SDpnt, sizeof (Scsi_Device));
521
 
522
    if (SCpnt != NULL)
523
      scsi_init_free ((char *) SCpnt, sizeof (Scsi_Cmnd));
524
 
525
    /* If we allocated a buffer so we could do DMA, free it now */
526
    if (scsi_result != &scsi_result0[0] && scsi_result != NULL)
527
      scsi_init_free (scsi_result, 512);
528
 
529
}
530
 
531
/*
532
 * The worker for scan_scsis.
533
 * Returning 0 means Please don't ask further for lun!=0, 1 means OK go on.
534
 * Global variables used : scsi_devices(linked list)
535
 */
536
int scan_scsis_single (int channel, int dev, int lun, int *max_dev_lun,
537
    int *sparse_lun, Scsi_Device **SDpnt2, Scsi_Cmnd * SCpnt,
538
    struct Scsi_Host * shpnt, char *scsi_result)
539
{
540
  unsigned char scsi_cmd[12];
541
  struct Scsi_Device_Template *sdtpnt;
542
  Scsi_Device * SDtail, *SDpnt=*SDpnt2;
543
  int bflags, type=-1;
544
 
545
  SDtail = scsi_devices;
546
  if (scsi_devices)
547
    while (SDtail->next)
548
      SDtail = SDtail->next;
549
 
550
  memset (SDpnt, 0, sizeof (Scsi_Device));
551
  SDpnt->host = shpnt;
552
  SDpnt->id = dev;
553
  SDpnt->lun = lun;
554
  SDpnt->channel = channel;
555
 
556
  /* Some low level driver could use device->type (DB) */
557
  SDpnt->type = -1;
558
 
559
  /*
560
   * Assume that the device will have handshaking problems, and then fix this
561
   * field later if it turns out it doesn't
562
   */
563
  SDpnt->borken = 1;
564
  SDpnt->was_reset = 0;
565
  SDpnt->expecting_cc_ua = 0;
566
 
567
  scsi_cmd[0] = TEST_UNIT_READY;
568
  scsi_cmd[1] = lun << 5;
569
  scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;
570
 
571
  SCpnt->host = SDpnt->host;
572
  SCpnt->device = SDpnt;
573
  SCpnt->target = SDpnt->id;
574
  SCpnt->lun = SDpnt->lun;
575
  SCpnt->channel = SDpnt->channel;
576
  {
577
    struct semaphore sem = MUTEX_LOCKED;
578
    SCpnt->request.sem = &sem;
579
    SCpnt->request.rq_status = RQ_SCSI_BUSY;
580
    scsi_do_cmd (SCpnt, (void *) scsi_cmd,
581
                 (void *) scsi_result,
582
                 256, scan_scsis_done, SCSI_TIMEOUT + 4 * HZ, 5);
583
    down (&sem);
584
  }
585
 
586
#if defined(DEBUG) || defined(DEBUG_INIT)
587
  printk ("scsi: scan_scsis_single id %d lun %d. Return code 0x%08x\n",
588
          dev, lun, SCpnt->result);
589
  print_driverbyte(SCpnt->result); print_hostbyte(SCpnt->result);
590
  printk("\n");
591
#endif
592
 
593
  if (SCpnt->result) {
594
    if (((driver_byte (SCpnt->result) & DRIVER_SENSE) ||
595
         (status_byte (SCpnt->result) & CHECK_CONDITION)) &&
596
        ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
597
      if (((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
598
          ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION) &&
599
          ((SCpnt->sense_buffer[2] & 0xf) != ILLEGAL_REQUEST || lun > 0))
600
        return 1;
601
    }
602
    else
603
      return 0;
604
  }
605
 
606
#if defined (DEBUG) || defined(DEBUG_INIT)
607
  printk ("scsi: performing INQUIRY\n");
608
#endif
609
  /*
610
   * Build an INQUIRY command block.
611
   */
612
  scsi_cmd[0] = INQUIRY;
613
  scsi_cmd[1] = (lun << 5) & 0xe0;
614
  scsi_cmd[2] = 0;
615
  scsi_cmd[3] = 0;
616
  scsi_cmd[4] = 255;
617
  scsi_cmd[5] = 0;
618
  SCpnt->cmd_len = 0;
619
  {
620
    struct semaphore sem = MUTEX_LOCKED;
621
    SCpnt->request.sem = &sem;
622
    SCpnt->request.rq_status = RQ_SCSI_BUSY;
623
    scsi_do_cmd (SCpnt, (void *) scsi_cmd,
624
                 (void *) scsi_result,
625
                 256, scan_scsis_done, SCSI_TIMEOUT, 3);
626
    down (&sem);
627
  }
628
 
629
#if defined(DEBUG) || defined(DEBUG_INIT)
630
  printk ("scsi: INQUIRY %s with code 0x%x\n",
631
          SCpnt->result ? "failed" : "successful", SCpnt->result);
632
#endif
633
 
634
  if (SCpnt->result)
635
    return 0;     /* assume no peripheral if any sort of error */
636
 
637
  /*
638
   * Check the peripheral qualifier field - this tells us whether LUNS
639
   * are supported here or not.
640
   */
641
  if( (scsi_result[0] >> 5) == 3 )
642
    {
643
      return 0;     /* assume no peripheral if any sort of error */
644
    }
645
 
646
  /*
647
   * It would seem some TOSHIBA CDROM gets things wrong
648
   */
649
  if (!strncmp (scsi_result + 8, "TOSHIBA", 7) &&
650
      !strncmp (scsi_result + 16, "CD-ROM", 6) &&
651
      scsi_result[0] == TYPE_DISK) {
652
    scsi_result[0] = TYPE_ROM;
653
    scsi_result[1] |= 0x80;     /* removable */
654
  }
655
 
656
  if (!strncmp (scsi_result + 8, "NEC", 3)) {
657
    if (!strncmp (scsi_result + 16, "CD-ROM DRIVE:84 ", 16) ||
658
        !strncmp (scsi_result + 16, "CD-ROM DRIVE:25", 15))
659
      SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
660
    else
661
      SDpnt->manufacturer = SCSI_MAN_NEC;
662
  }
663
  else if (!strncmp (scsi_result + 8, "TOSHIBA", 7))
664
    SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
665
  else if (!strncmp (scsi_result + 8, "SONY", 4))
666
    SDpnt->manufacturer = SCSI_MAN_SONY;
667
  else if (!strncmp (scsi_result + 8, "PIONEER", 7))
668
    SDpnt->manufacturer = SCSI_MAN_PIONEER;
669
  else
670
    SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
671
 
672
  memcpy (SDpnt->vendor, scsi_result + 8, 8);
673
  memcpy (SDpnt->model, scsi_result + 16, 16);
674
  memcpy (SDpnt->rev, scsi_result + 32, 4);
675
 
676
  SDpnt->removable = (0x80 & scsi_result[1]) >> 7;
677
  SDpnt->lockable = SDpnt->removable;
678
  SDpnt->changed = 0;
679
  SDpnt->access_count = 0;
680
  SDpnt->busy = 0;
681
  SDpnt->has_cmdblocks = 0;
682
  /*
683
   * Currently, all sequential devices are assumed to be tapes, all random
684
   * devices disk, with the appropriate read only flags set for ROM / WORM
685
   * treated as RO.
686
   */
687
  switch (type = (scsi_result[0] & 0x1f)) {
688
  case TYPE_TAPE:
689
  case TYPE_DISK:
690
  case TYPE_MOD:
691
  case TYPE_PROCESSOR:
692
  case TYPE_SCANNER:
693
  case TYPE_MEDIUM_CHANGER:
694
    SDpnt->writeable = 1;
695
    break;
696
  case TYPE_WORM:
697
  case TYPE_ROM:
698
    SDpnt->writeable = 0;
699
    break;
700
  default:
701
    printk ("scsi: unknown type %d\n", type);
702
  }
703
 
704
  SDpnt->single_lun = 0;
705
  SDpnt->soft_reset =
706
    (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
707
  SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
708
  SDpnt->type = (type & 0x1f);
709
 
710
  print_inquiry (scsi_result);
711
 
712
  for (sdtpnt = scsi_devicelist; sdtpnt;
713
       sdtpnt = sdtpnt->next)
714
    if (sdtpnt->detect)
715
      SDpnt->attached +=
716
        (*sdtpnt->detect) (SDpnt);
717
 
718
  SDpnt->scsi_level = scsi_result[2] & 0x07;
719
  if (SDpnt->scsi_level >= 2 ||
720
      (SDpnt->scsi_level == 1 &&
721
       (scsi_result[3] & 0x0f) == 1))
722
    SDpnt->scsi_level++;
723
 
724
  /*
725
   * Accommodate drivers that want to sleep when they should be in a polling
726
   * loop.
727
   */
728
  SDpnt->disconnect = 0;
729
 
730
  /*
731
   * Get any flags for this device.
732
   */
733
  bflags = get_device_flags (scsi_result);
734
 
735
  /*
736
   * Set the tagged_queue flag for SCSI-II devices that purport to support
737
   * tagged queuing in the INQUIRY data.
738
   */
739
  SDpnt->tagged_queue = 0;
740
  if ((SDpnt->scsi_level >= SCSI_2) &&
741
      (scsi_result[7] & 2) &&
742
      !(bflags & BLIST_NOTQ)) {
743
    SDpnt->tagged_supported = 1;
744
    SDpnt->current_tag = 0;
745
  }
746
 
747
  /*
748
   * Some revisions of the Texel CD ROM drives have handshaking problems when
749
   * used with the Seagate controllers.  Before we know what type of device
750
   * we're talking to, we assume it's borken and then change it here if it
751
   * turns out that it isn't a TEXEL drive.
752
   */
753
  if ((bflags & BLIST_BORKEN) == 0)
754
    SDpnt->borken = 0;
755
 
756
  /*
757
   * If we want to only allow I/O to one of the luns attached to this device
758
   * at a time, then we set this flag.
759
   */
760
  if (bflags & BLIST_SINGLELUN)
761
    SDpnt->single_lun = 1;
762
 
763
  /*
764
   * These devices need this "key" to unlock the devices so we can use it
765
   */
766
  if ((bflags & BLIST_KEY) != 0) {
767
    printk ("Unlocked floptical drive.\n");
768
    SDpnt->lockable = 0;
769
    scsi_cmd[0] = MODE_SENSE;
770
    scsi_cmd[1] = (lun << 5) & 0xe0;
771
    scsi_cmd[2] = 0x2e;
772
    scsi_cmd[3] = 0;
773
    scsi_cmd[4] = 0x2a;
774
    scsi_cmd[5] = 0;
775
    SCpnt->cmd_len = 0;
776
    {
777
      struct semaphore sem = MUTEX_LOCKED;
778
      SCpnt->request.rq_status = RQ_SCSI_BUSY;
779
      SCpnt->request.sem = &sem;
780
      scsi_do_cmd (SCpnt, (void *) scsi_cmd,
781
                   (void *) scsi_result, 0x2a,
782
                   scan_scsis_done, SCSI_TIMEOUT, 3);
783
      down (&sem);
784
    }
785
  }
786
  /* Add this device to the linked list at the end */
787
  if (SDtail)
788
    SDtail->next = SDpnt;
789
  else
790
    scsi_devices = SDpnt;
791
  SDtail = SDpnt;
792
 
793
  SDpnt = (Scsi_Device *) scsi_init_malloc (sizeof (Scsi_Device), GFP_ATOMIC);
794
  *SDpnt2=SDpnt;
795
  if (!SDpnt)
796
    printk ("scsi: scan_scsis_single: Cannot malloc\n");
797
 
798
 
799
  /*
800
   * Some scsi devices cannot be polled for lun != 0 due to firmware bugs
801
   */
802
  if (bflags & BLIST_NOLUN)
803
    return 0;                   /* break; */
804
 
805
  /*
806
   * If this device is known to support sparse multiple units, override the
807
   * other settings, and scan all of them.
808
   */
809
  if (bflags & BLIST_SPARSELUN) {
810
    *max_dev_lun = 8;
811
    *sparse_lun = 1;
812
    return 1;
813
  }
814
 
815
  /*
816
   * If this device is known to support multiple units, override the other
817
   * settings, and scan all of them.
818
   */
819
  if (bflags & BLIST_FORCELUN) {
820
    *max_dev_lun = 8;
821
    return 1;
822
  }
823
 
824
  /*
825
   * REGAL CDC-4X: avoid hang after LUN 4
826
   */
827
  if (bflags & BLIST_MAX5LUN) {
828
    *max_dev_lun = 5;
829
    return 1;
830
  }
831
 
832
  /*
833
   * We assume the device can't handle lun!=0 if: - it reports scsi-0 (ANSI
834
   * SCSI Revision 0) (old drives like MAXTOR XT-3280) or - it reports scsi-1
835
   * (ANSI SCSI Revision 1) and Response Data Format 0
836
   */
837
  if (((scsi_result[2] & 0x07) == 0)
838
      ||
839
      ((scsi_result[2] & 0x07) == 1 &&
840
       (scsi_result[3] & 0x0f) == 0))
841
    return 0;
842
  return 1;
843
}
844
 
845
/*
846
 *  Flag bits for the internal_timeout array
847
 */
848
#define NORMAL_TIMEOUT 0
849
#define IN_ABORT  1
850
#define IN_RESET  2
851
#define IN_RESET2 4
852
#define IN_RESET3 8
853
 
854
/*
855
 * This is our time out function, called when the timer expires for a
856
 * given host adapter.  It will attempt to abort the currently executing
857
 * command, that failing perform a kernel panic.
858
 */
859
 
860
static void scsi_times_out (Scsi_Cmnd * SCpnt)
861
{
862
 
863
    switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3))
864
    {
865
    case NORMAL_TIMEOUT:
866
        {
867
#ifdef DEBUG_TIMEOUT
868
            scsi_dump_status();
869
#endif
870
        }
871
 
872
        if (!scsi_abort (SCpnt, DID_TIME_OUT))
873
            return;
874
    case IN_ABORT:
875
        printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
876
               SCpnt->host->host_no, SCpnt->pid);
877
        if (!scsi_reset (SCpnt, SCSI_RESET_ASYNCHRONOUS))
878
            return;
879
    case IN_RESET:
880
    case (IN_ABORT | IN_RESET):
881
        /* This might be controversial, but if there is a bus hang,
882
         * you might conceivably want the machine up and running
883
         * esp if you have an ide disk.
884
         */
885
        printk("SCSI host %d channel %d reset (pid %ld) timed out - "
886
               "trying harder\n",
887
               SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
888
        SCpnt->internal_timeout &= ~IN_RESET;
889
        SCpnt->internal_timeout |= IN_RESET2;
890
        scsi_reset (SCpnt,
891
                    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
892
        return;
893
    case IN_RESET2:
894
    case (IN_ABORT | IN_RESET2):
895
        /* Obviously the bus reset didn't work.
896
         * Let's try even harder and call for an HBA reset.
897
         * Maybe the HBA itself crashed and this will shake it loose.
898
         */
899
        printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
900
               SCpnt->host->host_no, SCpnt->pid);
901
        SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
902
        SCpnt->internal_timeout |= IN_RESET3;
903
        scsi_reset (SCpnt,
904
                    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
905
        return;
906
 
907
    default:
908
        printk("SCSI host %d reset (pid %ld) timed out again -\n",
909
               SCpnt->host->host_no, SCpnt->pid);
910
        printk("probably an unrecoverable SCSI bus or device hang.\n");
911
        return;
912
 
913
    }
914
 
915
}
916
 
917
 
918
/* This function takes a quick look at a request, and decides if it
919
 * can be queued now, or if there would be a stall while waiting for
920
 * something else to finish.  This routine assumes that interrupts are
921
 * turned off when entering the routine.  It is the responsibility
922
 * of the calling code to ensure that this is the case.
923
 */
924
 
925
Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
926
{
927
    Scsi_Cmnd * SCpnt = NULL;
928
    int tablesize;
929
    Scsi_Cmnd * found = NULL;
930
    struct buffer_head * bh, *bhp;
931
 
932
    if (!device)
933
        panic ("No device passed to request_queueable().\n");
934
 
935
    if (req && req->rq_status == RQ_INACTIVE)
936
        panic("Inactive in request_queueable");
937
 
938
    /*
939
     * Look for a free command block.  If we have been instructed not to queue
940
     * multiple commands to multi-lun devices, then check to see what else is
941
     * going for this device first.
942
     */
943
 
944
    if (!device->single_lun) {
945
        SCpnt = device->device_queue;
946
        while(SCpnt){
947
            if(SCpnt->request.rq_status == RQ_INACTIVE) break;
948
            SCpnt = SCpnt->device_next;
949
        }
950
    } else {
951
        SCpnt = device->host->host_queue;
952
        while(SCpnt){
953
            if(SCpnt->channel == device->channel
954
                && SCpnt->target == device->id) {
955
                if (SCpnt->lun == device->lun) {
956
                    if(found == NULL
957
                       && SCpnt->request.rq_status == RQ_INACTIVE)
958
                    {
959
                        found=SCpnt;
960
                    }
961
                }
962
                if(SCpnt->request.rq_status != RQ_INACTIVE) {
963
                    /*
964
                     * I think that we should really limit things to one
965
                     * outstanding command per device - this is what tends
966
                     * to trip up buggy firmware.
967
                     */
968
                    return NULL;
969
                }
970
            }
971
            SCpnt = SCpnt->next;
972
        }
973
        SCpnt = found;
974
    }
975
 
976
    if (!SCpnt) return NULL;
977
 
978
    if (SCSI_BLOCK(device->host)) return NULL;
979
 
980
    if (req) {
981
        memcpy(&SCpnt->request, req, sizeof(struct request));
982
        tablesize = device->host->sg_tablesize;
983
        bhp = bh = req->bh;
984
        if(!tablesize) bh = NULL;
985
        /* Take a quick look through the table to see how big it is.
986
         * We already have our copy of req, so we can mess with that
987
         * if we want to.
988
         */
989
        while(req->nr_sectors && bh){
990
            bhp = bhp->b_reqnext;
991
            if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
992
            req->nr_sectors -= bh->b_size >> 9;
993
            req->sector += bh->b_size >> 9;
994
            if(!tablesize) break;
995
            bh = bhp;
996
        }
997
        if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
998
            SCpnt->request.bhtail = bh;
999
            req->bh = bh->b_reqnext; /* Divide request */
1000
            bh->b_reqnext = NULL;
1001
            bh = req->bh;
1002
 
1003
            /* Now reset things so that req looks OK */
1004
            SCpnt->request.nr_sectors -= req->nr_sectors;
1005
            req->current_nr_sectors = bh->b_size >> 9;
1006
            req->buffer = bh->b_data;
1007
            SCpnt->request.sem = NULL; /* Wait until whole thing done */
1008
        } else {
1009
            req->rq_status = RQ_INACTIVE;
1010
            wake_up(&wait_for_request);
1011
        }
1012
    } else {
1013
        SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Busy, but no request */
1014
        SCpnt->request.sem = NULL;   /* And no one is waiting for the device
1015
                                      * either */
1016
    }
1017
 
1018
    SCpnt->use_sg = 0;               /* Reset the scatter-gather flag */
1019
    SCpnt->old_use_sg  = 0;
1020
    SCpnt->transfersize = 0;
1021
    SCpnt->underflow = 0;
1022
    SCpnt->cmd_len = 0;
1023
 
1024
/* Since not everyone seems to set the device info correctly
1025
 * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
1026
 */
1027
    SCpnt->channel = device->channel;
1028
    SCpnt->lun = device->lun;
1029
    SCpnt->target = device->id;
1030
 
1031
    return SCpnt;
1032
}
1033
 
1034
/* This function returns a structure pointer that will be valid for
1035
 * the device.  The wait parameter tells us whether we should wait for
1036
 * the unit to become free or not.  We are also able to tell this routine
1037
 * not to return a descriptor if the host is unable to accept any more
1038
 * commands for the time being.  We need to keep in mind that there is no
1039
 * guarantee that the host remain not busy.  Keep in mind the
1040
 * request_queueable function also knows the internal allocation scheme
1041
 * of the packets for each device
1042
 */
1043
 
1044
Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
1045
                             int wait)
1046
{
1047
    kdev_t dev;
1048
    struct request * req = NULL;
1049
    int tablesize;
1050
    unsigned long flags;
1051
    struct buffer_head * bh, *bhp;
1052
    struct Scsi_Host * host;
1053
    Scsi_Cmnd * SCpnt = NULL;
1054
    Scsi_Cmnd * SCwait = NULL;
1055
    Scsi_Cmnd * found = NULL;
1056
 
1057
    if (!device)
1058
        panic ("No device passed to allocate_device().\n");
1059
 
1060
    if (reqp) req = *reqp;
1061
 
1062
    /* See if this request has already been queued by an interrupt routine */
1063
    if (req) {
1064
        if(req->rq_status == RQ_INACTIVE) return NULL;
1065
        dev = req->rq_dev;
1066
    } else
1067
        dev = 0;         /* unused */
1068
 
1069
    host = device->host;
1070
 
1071
    if (intr_count && SCSI_BLOCK(host)) return NULL;
1072
 
1073
    while (1==1){
1074
        if (!device->single_lun) {
1075
            SCpnt = device->device_queue;
1076
            while(SCpnt){
1077
                SCwait = SCpnt;
1078
                if(SCpnt->request.rq_status == RQ_INACTIVE) break;
1079
                SCpnt = SCpnt->device_next;
1080
            }
1081
        } else {
1082
            SCpnt = device->host->host_queue;
1083
            while(SCpnt){
1084
                if(SCpnt->channel == device->channel
1085
                   && SCpnt->target == device->id) {
1086
                    if (SCpnt->lun == device->lun) {
1087
                        SCwait = SCpnt;
1088
                        if(found == NULL
1089
                           && SCpnt->request.rq_status == RQ_INACTIVE)
1090
                        {
1091
                            found=SCpnt;
1092
                        }
1093
                    }
1094
                    if(SCpnt->request.rq_status != RQ_INACTIVE) {
1095
                        /*
1096
                         * I think that we should really limit things to one
1097
                         * outstanding command per device - this is what tends
1098
                         * to trip up buggy firmware.
1099
                         */
1100
                        found = NULL;
1101
                        break;
1102
                    }
1103
                }
1104
                SCpnt = SCpnt->next;
1105
            }
1106
            SCpnt = found;
1107
        }
1108
 
1109
        save_flags(flags);
1110
        cli();
1111
        /* See if this request has already been queued by an interrupt routine
1112
         */
1113
        if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
1114
            restore_flags(flags);
1115
            return NULL;
1116
        }
1117
        if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)  /* Might have changed */
1118
        {
1119
#if 1   /* NEW CODE */
1120
                if (wait && SCwait && SCwait->request.rq_status != RQ_INACTIVE){
1121
                        sleep_on(&device->device_wait);
1122
                        restore_flags(flags);
1123
                } else {
1124
                        restore_flags(flags);
1125
                        if (!wait) return NULL;
1126
                        if (!SCwait) {
1127
                                printk("Attempt to allocate device channel %d,"
1128
                                       " target %d, lun %d\n", device->channel,
1129
                                       device->id, device->lun);
1130
                                panic("No device found in allocate_device\n");
1131
                        }
1132
                }
1133
#else   /* ORIGINAL CODE */
1134
                    restore_flags(flags);
1135
                    if(!wait) return NULL;
1136
                    if (!SCwait) {
1137
                        printk("Attempt to allocate device channel %d, target"
1138
                               " %d, lun %d\n", device->channel, device->id,
1139
                               device->lun);
1140
                        panic("No device found in allocate_device\n");
1141
                    }
1142
                    SCSI_SLEEP(&device->device_wait,
1143
                               (SCwait->request.rq_status != RQ_INACTIVE));
1144
#endif
1145
        } else {
1146
            if (req) {
1147
                memcpy(&SCpnt->request, req, sizeof(struct request));
1148
                tablesize = device->host->sg_tablesize;
1149
                bhp = bh = req->bh;
1150
                if(!tablesize) bh = NULL;
1151
                /* Take a quick look through the table to see how big it is.
1152
                 * We already have our copy of req, so we can mess with that
1153
                 * if we want to.
1154
                 */
1155
                while(req->nr_sectors && bh){
1156
                    bhp = bhp->b_reqnext;
1157
                    if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
1158
                    req->nr_sectors -= bh->b_size >> 9;
1159
                    req->sector += bh->b_size >> 9;
1160
                    if(!tablesize) break;
1161
                    bh = bhp;
1162
                }
1163
                if(req->nr_sectors && bh && bh->b_reqnext){/* Any leftovers? */
1164
                    SCpnt->request.bhtail = bh;
1165
                    req->bh = bh->b_reqnext; /* Divide request */
1166
                    bh->b_reqnext = NULL;
1167
                    bh = req->bh;
1168
                    /* Now reset things so that req looks OK */
1169
                    SCpnt->request.nr_sectors -= req->nr_sectors;
1170
                    req->current_nr_sectors = bh->b_size >> 9;
1171
                    req->buffer = bh->b_data;
1172
                    SCpnt->request.sem = NULL; /* Wait until whole thing done*/
1173
                }
1174
                else
1175
                {
1176
                    req->rq_status = RQ_INACTIVE;
1177
                    *reqp = req->next;
1178
                    wake_up(&wait_for_request);
1179
                }
1180
            } else {
1181
                SCpnt->request.rq_status = RQ_SCSI_BUSY;
1182
                SCpnt->request.sem = NULL;   /* And no one is waiting for this
1183
                                              * to complete */
1184
            }
1185
            restore_flags(flags);
1186
            break;
1187
        }
1188
    }
1189
 
1190
    SCpnt->use_sg = 0;            /* Reset the scatter-gather flag */
1191
    SCpnt->old_use_sg  = 0;
1192
    SCpnt->transfersize = 0;      /* No default transfer size */
1193
    SCpnt->cmd_len = 0;
1194
 
1195
    SCpnt->underflow = 0;         /* Do not flag underflow conditions */
1196
 
1197
    /* Since not everyone seems to set the device info correctly
1198
     * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
1199
     */
1200
    SCpnt->channel = device->channel;
1201
    SCpnt->lun = device->lun;
1202
    SCpnt->target = device->id;
1203
 
1204
    return SCpnt;
1205
}
1206
 
1207
/*
1208
 * This is inline because we have stack problemes if we recurse to deeply.
1209
 */
1210
 
1211
inline void internal_cmnd (Scsi_Cmnd * SCpnt)
1212
{
1213
    unsigned long flags, timeout;
1214
    struct Scsi_Host * host;
1215
#ifdef DEBUG_DELAY
1216
    unsigned long clock;
1217
#endif
1218
 
1219
#if DEBUG
1220
    unsigned long *ret = 0;
1221
#ifdef __mips__
1222
    __asm__ __volatile__ ("move\t%0,$31":"=r"(ret));
1223
#else
1224
   ret =  __builtin_return_address(0);
1225
#endif
1226
#endif
1227
 
1228
    host = SCpnt->host;
1229
 
1230
    save_flags(flags);
1231
    cli();
1232
    /* Assign a unique nonzero serial_number. */
1233
    if (++serial_number == 0) serial_number = 1;
1234
    SCpnt->serial_number = serial_number;
1235
 
1236
    /*
1237
     * We will wait MIN_RESET_DELAY clock ticks after the last reset so
1238
     * we can avoid the drive not being ready.
1239
     */
1240
    timeout = host->last_reset + MIN_RESET_DELAY;
1241
    if (jiffies < timeout) {
1242
        int ticks_remaining = timeout - jiffies;
1243
        /*
1244
         * NOTE: This may be executed from within an interrupt
1245
         * handler!  This is bad, but for now, it'll do.  The irq
1246
         * level of the interrupt handler has been masked out by the
1247
         * platform dependent interrupt handling code already, so the
1248
         * sti() here will not cause another call to the SCSI host's
1249
         * interrupt handler (assuming there is one irq-level per
1250
         * host).
1251
         */
1252
        sti();
1253
        while (--ticks_remaining >= 0) udelay(1000000/HZ);
1254
        host->last_reset = jiffies - MIN_RESET_DELAY;
1255
    }
1256
    restore_flags(flags);
1257
 
1258
    update_timeout(SCpnt, SCpnt->timeout_per_command);
1259
 
1260
    /*
1261
     * We will use a queued command if possible, otherwise we will emulate the
1262
     * queuing and calling of completion function ourselves.
1263
     */
1264
#ifdef DEBUG
1265
    printk("internal_cmnd (host = %d, channel = %d, target = %d, "
1266
           "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n",
1267
           SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd,
1268
           SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
1269
#endif
1270
 
1271
    if (host->can_queue)
1272
    {
1273
#ifdef DEBUG
1274
        printk("queuecommand : routine at %p\n",
1275
               host->hostt->queuecommand);
1276
#endif
1277
        /* This locking tries to prevent all sorts of races between
1278
         * queuecommand and the interrupt code.  In effect,
1279
         * we are only allowed to be in queuecommand once at
1280
         * any given time, and we can only be in the interrupt
1281
         * handler and the queuecommand function at the same time
1282
         * when queuecommand is called while servicing the
1283
         * interrupt.
1284
         */
1285
 
1286
        if(!intr_count && SCpnt->host->irq)
1287
            disable_irq(SCpnt->host->irq);
1288
 
1289
        host->hostt->queuecommand (SCpnt, scsi_done);
1290
 
1291
        if(!intr_count && SCpnt->host->irq)
1292
            enable_irq(SCpnt->host->irq);
1293
    }
1294
    else
1295
    {
1296
        int temp;
1297
 
1298
#ifdef DEBUG
1299
        printk("command() :  routine at %p\n", host->hostt->command);
1300
#endif
1301
        temp = host->hostt->command (SCpnt);
1302
        SCpnt->result = temp;
1303
#ifdef DEBUG_DELAY
1304
        clock = jiffies + 4 * HZ;
1305
        while (jiffies < clock) barrier();
1306
        printk("done(host = %d, result = %04x) : routine at %p\n",
1307
               host->host_no, temp, host->hostt->command);
1308
#endif
1309
        scsi_done(SCpnt);
1310
    }
1311
#ifdef DEBUG
1312
    printk("leaving internal_cmnd()\n");
1313
#endif
1314
}
1315
 
1316
static void scsi_request_sense (Scsi_Cmnd * SCpnt)
1317
{
1318
    unsigned long flags;
1319
 
1320
    save_flags(flags);
1321
    cli();
1322
    SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
1323
    update_timeout(SCpnt, SENSE_TIMEOUT);
1324
    restore_flags(flags);
1325
 
1326
 
1327
    memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
1328
            sizeof(generic_sense));
1329
 
1330
    SCpnt->cmnd[1] = SCpnt->lun << 5;
1331
    SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
1332
 
1333
    SCpnt->request_buffer = &SCpnt->sense_buffer;
1334
    SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
1335
    SCpnt->use_sg = 0;
1336
    SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1337
    internal_cmnd (SCpnt);
1338
}
1339
 
1340
 
1341
 
1342
/*
1343
 * scsi_do_cmd sends all the commands out to the low-level driver.  It
1344
 * handles the specifics required for each low level driver - ie queued
1345
 * or non queued.  It also prevents conflicts when different high level
1346
 * drivers go for the same host at the same time.
1347
 */
1348
 
1349
void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
1350
                  void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
1351
                  int timeout, int retries)
1352
{
1353
    unsigned long flags;
1354
    struct Scsi_Host * host = SCpnt->host;
1355
 
1356
#ifdef DEBUG
1357
    {
1358
        int i;
1359
        int target = SCpnt->target;
1360
        printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
1361
                "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
1362
                "retries = %d)\n"
1363
                "command : " , host->host_no, SCpnt->channel, target, buffer,
1364
                bufflen, done, timeout, retries);
1365
        for (i = 0; i < 10; ++i)
1366
            printk ("%02x  ", ((unsigned char *) cmnd)[i]);
1367
        printk("\n");
1368
    }
1369
#endif
1370
 
1371
    if (!host)
1372
    {
1373
        panic ("Invalid or not present host.\n");
1374
    }
1375
 
1376
 
1377
    /*
1378
     * We must prevent reentrancy to the lowlevel host driver.  This prevents
1379
     * it - we enter a loop until the host we want to talk to is not busy.
1380
     * Race conditions are prevented, as interrupts are disabled in between the
1381
     * time we check for the host being not busy, and the time we mark it busy
1382
     * ourselves.
1383
     */
1384
 
1385
    save_flags(flags);
1386
    cli();
1387
    SCpnt->pid = scsi_pid++;
1388
 
1389
    while (SCSI_BLOCK(host)) {
1390
        restore_flags(flags);
1391
        SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
1392
        cli();
1393
    }
1394
 
1395
    if (host->block) host_active = host;
1396
 
1397
    host->host_busy++;
1398
    restore_flags(flags);
1399
 
1400
    /*
1401
     * Our own function scsi_done (which marks the host as not busy, disables
1402
     * the timeout counter, etc) will be called by us or by the
1403
     * scsi_hosts[host].queuecommand() function needs to also call
1404
     * the completion function for the high level driver.
1405
     */
1406
 
1407
    memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
1408
#if 0
1409
    SCpnt->host = host;
1410
    SCpnt->channel = channel;
1411
    SCpnt->target = target;
1412
    SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
1413
#endif
1414
    SCpnt->reset_chain = NULL;
1415
    SCpnt->serial_number = 0;
1416
    SCpnt->bufflen = bufflen;
1417
    SCpnt->buffer = buffer;
1418
    SCpnt->flags = 0;
1419
    SCpnt->retries = 0;
1420
    SCpnt->allowed = retries;
1421
    SCpnt->done = done;
1422
    SCpnt->timeout_per_command = timeout;
1423
 
1424
    memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
1425
    /* Zero the sense buffer.  Some host adapters automatically request
1426
     * sense on error.  0 is not a valid sense code.
1427
     */
1428
    memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1429
    SCpnt->request_buffer = buffer;
1430
    SCpnt->request_bufflen = bufflen;
1431
    SCpnt->old_use_sg = SCpnt->use_sg;
1432
    if (SCpnt->cmd_len == 0)
1433
        SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1434
    SCpnt->old_cmd_len = SCpnt->cmd_len;
1435
 
1436
    /* Start the timer ticking.  */
1437
 
1438
    SCpnt->internal_timeout = NORMAL_TIMEOUT;
1439
    SCpnt->abort_reason = 0;
1440
    internal_cmnd (SCpnt);
1441
 
1442
#ifdef DEBUG
1443
    printk ("Leaving scsi_do_cmd()\n");
1444
#endif
1445
}
1446
 
1447
static int check_sense (Scsi_Cmnd * SCpnt)
1448
{
1449
    /* If there is no sense information, request it.  If we have already
1450
     * requested it, there is no point in asking again - the firmware must
1451
     * be confused.
1452
     */
1453
    if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1454
        if(!(SCpnt->flags & ASKED_FOR_SENSE))
1455
            return SUGGEST_SENSE;
1456
        else
1457
            return SUGGEST_RETRY;
1458
    }
1459
 
1460
    SCpnt->flags &= ~ASKED_FOR_SENSE;
1461
 
1462
#ifdef DEBUG_INIT
1463
    printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
1464
    print_sense("", SCpnt);
1465
    printk("\n");
1466
#endif
1467
    if (SCpnt->sense_buffer[2] & 0xe0)
1468
        return SUGGEST_ABORT;
1469
 
1470
    switch (SCpnt->sense_buffer[2] & 0xf)
1471
    {
1472
    case NO_SENSE:
1473
        return 0;
1474
    case RECOVERED_ERROR:
1475
        return SUGGEST_IS_OK;
1476
 
1477
    case ABORTED_COMMAND:
1478
        return SUGGEST_RETRY;
1479
    case NOT_READY:
1480
    case UNIT_ATTENTION:
1481
        /*
1482
         * If we are expecting a CC/UA because of a bus reset that we
1483
         * performed, treat this just as a retry.  Otherwise this is
1484
         * information that we should pass up to the upper-level driver
1485
         * so that we can deal with it there.
1486
         */
1487
        if( SCpnt->device->expecting_cc_ua )
1488
        {
1489
            SCpnt->device->expecting_cc_ua = 0;
1490
            return SUGGEST_RETRY;
1491
        }
1492
        return SUGGEST_ABORT;
1493
 
1494
    /* these three are not supported */
1495
    case COPY_ABORTED:
1496
    case VOLUME_OVERFLOW:
1497
    case MISCOMPARE:
1498
 
1499
    case MEDIUM_ERROR:
1500
        return SUGGEST_REMAP;
1501
    case BLANK_CHECK:
1502
    case DATA_PROTECT:
1503
    case HARDWARE_ERROR:
1504
    case ILLEGAL_REQUEST:
1505
    default:
1506
        return SUGGEST_ABORT;
1507
    }
1508
}
1509
 
1510
/* This function is the mid-level interrupt routine, which decides how
1511
 *  to handle error conditions.  Each invocation of this function must
1512
 *  do one and *only* one of the following:
1513
 *
1514
 *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1515
 *      normal completion, and indicates that the handling for this
1516
 *      request is complete.
1517
 *  (2) Call internal_cmnd to requeue the command.  This will result in
1518
 *      scsi_done being called again when the retry is complete.
1519
 *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1520
 *      more information about the error condition.  When the information
1521
 *      is available, scsi_done will be called again.
1522
 *  (4) Call reset().  This is sort of a last resort, and the idea is that
1523
 *      this may kick things loose and get the drive working again.  reset()
1524
 *      automatically calls scsi_request_sense, and thus scsi_done will be
1525
 *      called again once the reset is complete.
1526
 *
1527
 *      If none of the above actions are taken, the drive in question
1528
 *      will hang. If more than one of the above actions are taken by
1529
 *      scsi_done, then unpredictable behavior will result.
1530
 */
1531
static void scsi_done (Scsi_Cmnd * SCpnt)
1532
{
1533
    int status=0;
1534
    int exit=0;
1535
    int checked;
1536
    int oldto;
1537
    struct Scsi_Host * host = SCpnt->host;
1538
    int result = SCpnt->result;
1539
    SCpnt->serial_number = 0;
1540
    oldto = update_timeout(SCpnt, 0);
1541
 
1542
#ifdef DEBUG_TIMEOUT
1543
    if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1544
                      result, SCpnt->target, SCpnt->lun);
1545
#endif
1546
 
1547
    /* If we requested an abort, (and we got it) then fix up the return
1548
     *  status to say why
1549
     */
1550
    if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1551
        SCpnt->result = result = (result & 0xff00ffff) |
1552
            (SCpnt->abort_reason << 16);
1553
 
1554
 
1555
#define FINISHED 0
1556
#define MAYREDO  1
1557
#define REDO     3
1558
#define PENDING  4
1559
 
1560
#ifdef DEBUG
1561
    printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1562
#endif
1563
 
1564
    if(SCpnt->flags & WAS_SENSE)
1565
    {
1566
        SCpnt->use_sg = SCpnt->old_use_sg;
1567
        SCpnt->cmd_len = SCpnt->old_cmd_len;
1568
    }
1569
 
1570
    switch (host_byte(result))
1571
    {
1572
    case DID_OK:
1573
        if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1574
            /* Failed to obtain sense information */
1575
        {
1576
            SCpnt->flags &= ~WAS_SENSE;
1577
#if 0   /* This cannot possibly be correct. */
1578
            SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1579
#endif
1580
 
1581
            if (!(SCpnt->flags & WAS_RESET))
1582
            {
1583
                printk("scsi%d : channel %d target %d lun %d request sense"
1584
                       " failed, performing reset.\n",
1585
                       SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
1586
                       SCpnt->lun);
1587
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1588
                return;
1589
            }
1590
            else
1591
            {
1592
                exit = (DRIVER_HARD | SUGGEST_ABORT);
1593
                status = FINISHED;
1594
            }
1595
        }
1596
        else switch(msg_byte(result))
1597
        {
1598
        case COMMAND_COMPLETE:
1599
            switch (status_byte(result))
1600
            {
1601
            case GOOD:
1602
                if (SCpnt->flags & WAS_SENSE)
1603
                {
1604
#ifdef DEBUG
1605
                    printk ("In scsi_done, GOOD status, COMMAND COMPLETE, "
1606
                            "parsing sense information.\n");
1607
#endif
1608
                    SCpnt->flags &= ~WAS_SENSE;
1609
#if 0   /* This cannot possibly be correct. */
1610
                    SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1611
#endif
1612
 
1613
                    switch (checked = check_sense(SCpnt))
1614
                    {
1615
                    case SUGGEST_SENSE:
1616
                    case 0:
1617
#ifdef DEBUG
1618
                        printk("NO SENSE.  status = REDO\n");
1619
#endif
1620
                        update_timeout(SCpnt, oldto);
1621
                        status = REDO;
1622
                        break;
1623
                    case SUGGEST_IS_OK:
1624
                        break;
1625
                    case SUGGEST_REMAP:
1626
#ifdef DEBUG
1627
                        printk("SENSE SUGGEST REMAP - status = FINISHED\n");
1628
#endif
1629
                        status = FINISHED;
1630
                        exit = DRIVER_SENSE | SUGGEST_ABORT;
1631
                        break;
1632
                    case SUGGEST_RETRY:
1633
#ifdef DEBUG
1634
                        printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
1635
#endif
1636
                        status = MAYREDO;
1637
                        exit = DRIVER_SENSE | SUGGEST_RETRY;
1638
                        break;
1639
                    case SUGGEST_ABORT:
1640
#ifdef DEBUG
1641
                        printk("SENSE SUGGEST ABORT - status = FINISHED");
1642
#endif
1643
                        status = FINISHED;
1644
                        exit =  DRIVER_SENSE | SUGGEST_ABORT;
1645
                        break;
1646
                    default:
1647
                        printk ("Internal error %s %d \n", __FILE__,
1648
                                __LINE__);
1649
                    }
1650
                } /* end WAS_SENSE */
1651
                else
1652
                {
1653
#ifdef DEBUG
1654
                    printk("COMMAND COMPLETE message returned, "
1655
                           "status = FINISHED. \n");
1656
#endif
1657
                    exit =  DRIVER_OK;
1658
                    status = FINISHED;
1659
                }
1660
                break;
1661
 
1662
            case CHECK_CONDITION:
1663
            case COMMAND_TERMINATED:
1664
                switch (check_sense(SCpnt))
1665
                {
1666
                case 0:
1667
                    update_timeout(SCpnt, oldto);
1668
                    status = REDO;
1669
                    break;
1670
                case SUGGEST_REMAP:
1671
                    status = FINISHED;
1672
                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
1673
                    break;
1674
                case SUGGEST_RETRY:
1675
                    status = MAYREDO;
1676
                    exit = DRIVER_SENSE | SUGGEST_RETRY;
1677
                    break;
1678
                case SUGGEST_ABORT:
1679
                    status = FINISHED;
1680
                    exit =  DRIVER_SENSE | SUGGEST_ABORT;
1681
                    break;
1682
                case SUGGEST_SENSE:
1683
                    scsi_request_sense (SCpnt);
1684
                    status = PENDING;
1685
                    break;
1686
                }
1687
                break;
1688
 
1689
            case CONDITION_GOOD:
1690
            case INTERMEDIATE_GOOD:
1691
            case INTERMEDIATE_C_GOOD:
1692
                break;
1693
 
1694
            case BUSY:
1695
            case QUEUE_FULL:
1696
                update_timeout(SCpnt, oldto);
1697
                status = REDO;
1698
                break;
1699
 
1700
            case RESERVATION_CONFLICT:
1701
                printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
1702
                       " reset.\n", SCpnt->host->host_no, SCpnt->channel);
1703
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1704
                return;
1705
#if 0
1706
                exit = DRIVER_SOFT | SUGGEST_ABORT;
1707
                status = MAYREDO;
1708
                break;
1709
#endif
1710
            default:
1711
                printk ("Internal error %s %d \n"
1712
                        "status byte = %d \n", __FILE__,
1713
                        __LINE__, status_byte(result));
1714
 
1715
            }
1716
            break;
1717
        default:
1718
            panic("scsi: unsupported message byte %d received\n",
1719
                  msg_byte(result));
1720
        }
1721
        break;
1722
    case DID_TIME_OUT:
1723
#ifdef DEBUG
1724
        printk("Host returned DID_TIME_OUT - ");
1725
#endif
1726
 
1727
        if (SCpnt->flags & WAS_TIMEDOUT)
1728
        {
1729
#ifdef DEBUG
1730
            printk("Aborting\n");
1731
#endif
1732
            /*
1733
              Allow TEST_UNIT_READY and INQUIRY commands to timeout early
1734
              without causing resets.  All other commands should be retried.
1735
            */
1736
            if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
1737
                SCpnt->cmnd[0] != INQUIRY)
1738
                    status = MAYREDO;
1739
            exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1740
        }
1741
        else
1742
        {
1743
#ifdef DEBUG
1744
            printk ("Retrying.\n");
1745
#endif
1746
            SCpnt->flags  |= WAS_TIMEDOUT;
1747
            SCpnt->internal_timeout &= ~IN_ABORT;
1748
            status = REDO;
1749
        }
1750
        break;
1751
    case DID_BUS_BUSY:
1752
    case DID_PARITY:
1753
        status = REDO;
1754
        break;
1755
    case DID_NO_CONNECT:
1756
#ifdef DEBUG
1757
        printk("Couldn't connect.\n");
1758
#endif
1759
        exit  = (DRIVER_HARD | SUGGEST_ABORT);
1760
        break;
1761
    case DID_ERROR:
1762
        status = MAYREDO;
1763
        exit = (DRIVER_HARD | SUGGEST_ABORT);
1764
        break;
1765
    case DID_BAD_TARGET:
1766
    case DID_ABORT:
1767
        exit = (DRIVER_INVALID | SUGGEST_ABORT);
1768
        break;
1769
    case DID_RESET:
1770
        if (SCpnt->flags & IS_RESETTING)
1771
        {
1772
            SCpnt->flags &= ~IS_RESETTING;
1773
            status = REDO;
1774
            break;
1775
        }
1776
 
1777
        if(msg_byte(result) == GOOD &&
1778
           status_byte(result) == CHECK_CONDITION) {
1779
            switch (check_sense(SCpnt)) {
1780
            case 0:
1781
                update_timeout(SCpnt, oldto);
1782
                status = REDO;
1783
                break;
1784
            case SUGGEST_REMAP:
1785
            case SUGGEST_RETRY:
1786
                status = MAYREDO;
1787
                exit = DRIVER_SENSE | SUGGEST_RETRY;
1788
                break;
1789
            case SUGGEST_ABORT:
1790
                status = FINISHED;
1791
                exit =  DRIVER_SENSE | SUGGEST_ABORT;
1792
                break;
1793
            case SUGGEST_SENSE:
1794
                scsi_request_sense (SCpnt);
1795
                status = PENDING;
1796
                break;
1797
            }
1798
        } else {
1799
            status=REDO;
1800
            exit = SUGGEST_RETRY;
1801
        }
1802
        break;
1803
    default :
1804
        exit = (DRIVER_ERROR | SUGGEST_DIE);
1805
    }
1806
 
1807
    switch (status)
1808
    {
1809
    case FINISHED:
1810
    case PENDING:
1811
        break;
1812
    case MAYREDO:
1813
#ifdef DEBUG
1814
        printk("In MAYREDO, allowing %d retries, have %d\n",
1815
               SCpnt->allowed, SCpnt->retries);
1816
#endif
1817
        if ((++SCpnt->retries) < SCpnt->allowed)
1818
        {
1819
            if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1820
                && !(SCpnt->host->last_reset > 0 &&
1821
                     jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
1822
                && !(SCpnt->flags & WAS_RESET))
1823
            {
1824
                printk("scsi%d channel %d : resetting for second half of retries.\n",
1825
                       SCpnt->host->host_no, SCpnt->channel);
1826
                scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1827
                break;
1828
            }
1829
 
1830
        }
1831
        else
1832
        {
1833
            status = FINISHED;
1834
            break;
1835
        }
1836
        /* fall through to REDO */
1837
 
1838
    case REDO:
1839
 
1840
        if (SCpnt->flags & WAS_SENSE)
1841
            scsi_request_sense(SCpnt);
1842
        else
1843
        {
1844
            memcpy ((void *) SCpnt->cmnd,
1845
                    (void*) SCpnt->data_cmnd,
1846
                    sizeof(SCpnt->data_cmnd));
1847
            SCpnt->request_buffer = SCpnt->buffer;
1848
            SCpnt->request_bufflen = SCpnt->bufflen;
1849
            SCpnt->use_sg = SCpnt->old_use_sg;
1850
            SCpnt->cmd_len = SCpnt->old_cmd_len;
1851
            internal_cmnd (SCpnt);
1852
        }
1853
        break;
1854
    default:
1855
        INTERNAL_ERROR;
1856
    }
1857
 
1858
    if (status == FINISHED) {
1859
#ifdef DEBUG
1860
        printk("Calling done function - at address %p\n", SCpnt->done);
1861
#endif
1862
        host->host_busy--; /* Indicate that we are free */
1863
 
1864
        if (host->block && host->host_busy == 0) {
1865
            host_active = NULL;
1866
 
1867
            /* For block devices "wake_up" is done in end_scsi_request */
1868
            if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
1869
                MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
1870
                struct Scsi_Host * next;
1871
 
1872
                for (next = host->block; next != host; next = next->block)
1873
                    wake_up(&next->host_wait);
1874
            }
1875
 
1876
        }
1877
 
1878
        wake_up(&host->host_wait);
1879
        SCpnt->result = result | ((exit & 0xff) << 24);
1880
        SCpnt->use_sg = SCpnt->old_use_sg;
1881
        SCpnt->cmd_len = SCpnt->old_cmd_len;
1882
        SCpnt->done (SCpnt);
1883
    }
1884
 
1885
#undef FINISHED
1886
#undef REDO
1887
#undef MAYREDO
1888
#undef PENDING
1889
}
1890
 
1891
/*
1892
 * The scsi_abort function interfaces with the abort() function of the host
1893
 * we are aborting, and causes the current command to not complete.  The
1894
 * caller should deal with any error messages or status returned on the
1895
 * next call.
1896
 *
1897
 * This will not be called reentrantly for a given host.
1898
 */
1899
 
1900
/*
1901
 * Since we're nice guys and specified that abort() and reset()
1902
 * can be non-reentrant.  The internal_timeout flags are used for
1903
 * this.
1904
 */
1905
 
1906
 
1907
int scsi_abort (Scsi_Cmnd * SCpnt, int why)
1908
{
1909
    int oldto;
1910
    unsigned long flags;
1911
    struct Scsi_Host * host = SCpnt->host;
1912
 
1913
    while(1)
1914
    {
1915
        save_flags(flags);
1916
        cli();
1917
 
1918
        /*
1919
         * Protect against races here.  If the command is done, or we are
1920
         * on a different command forget it.
1921
         */
1922
        if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
1923
            restore_flags(flags);
1924
            return 0;
1925
        }
1926
 
1927
        if (SCpnt->internal_timeout & IN_ABORT)
1928
        {
1929
            restore_flags(flags);
1930
            while (SCpnt->internal_timeout & IN_ABORT)
1931
                barrier();
1932
        }
1933
        else
1934
        {
1935
            SCpnt->internal_timeout |= IN_ABORT;
1936
            oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1937
 
1938
            if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
1939
                /* OK, this command must have died when we did the
1940
                 *  reset.  The device itself must have lied.
1941
                 */
1942
                printk("Stale command on %d %d:%d appears to have died when"
1943
                       " the bus was reset\n",
1944
                       SCpnt->channel, SCpnt->target, SCpnt->lun);
1945
            }
1946
 
1947
            restore_flags(flags);
1948
            if (!host->host_busy) {
1949
                SCpnt->internal_timeout &= ~IN_ABORT;
1950
                update_timeout(SCpnt, oldto);
1951
                return 0;
1952
            }
1953
            printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
1954
                   " channel %d, id %d, lun %d ",
1955
                   SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
1956
                   (int) SCpnt->target, (int) SCpnt->lun);
1957
            print_command (SCpnt->cmnd);
1958
            if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
1959
                return 0;
1960
            SCpnt->abort_reason = why;
1961
            switch(host->hostt->abort(SCpnt)) {
1962
                /* We do not know how to abort.  Try waiting another
1963
                 * time increment and see if this helps. Set the
1964
                 * WAS_TIMEDOUT flag set so we do not try this twice
1965
                 */
1966
            case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1967
                                   * this is too severe
1968
                                   */
1969
            case SCSI_ABORT_SNOOZE:
1970
                if(why == DID_TIME_OUT) {
1971
                    save_flags(flags);
1972
                    cli();
1973
                    SCpnt->internal_timeout &= ~IN_ABORT;
1974
                    if(SCpnt->flags & WAS_TIMEDOUT) {
1975
                        restore_flags(flags);
1976
                        return 1; /* Indicate we cannot handle this.
1977
                                   * We drop down into the reset handler
1978
                                   * and try again
1979
                                   */
1980
                    } else {
1981
                        SCpnt->flags |= WAS_TIMEDOUT;
1982
                        oldto = SCpnt->timeout_per_command;
1983
                        update_timeout(SCpnt, oldto);
1984
                    }
1985
                    restore_flags(flags);
1986
                }
1987
                return 0;
1988
            case SCSI_ABORT_PENDING:
1989
                if(why != DID_TIME_OUT) {
1990
                    save_flags(flags);
1991
                    cli();
1992
                    update_timeout(SCpnt, oldto);
1993
                    restore_flags(flags);
1994
                }
1995
                return 0;
1996
            case SCSI_ABORT_SUCCESS:
1997
                /* We should have already aborted this one.  No
1998
                 * need to adjust timeout
1999
                 */
2000
                 SCpnt->internal_timeout &= ~IN_ABORT;
2001
                 return 0;
2002
            case SCSI_ABORT_NOT_RUNNING:
2003
                SCpnt->internal_timeout &= ~IN_ABORT;
2004
                update_timeout(SCpnt, 0);
2005
                return 0;
2006
            case SCSI_ABORT_ERROR:
2007
            default:
2008
                SCpnt->internal_timeout &= ~IN_ABORT;
2009
                return 1;
2010
            }
2011
        }
2012
    }
2013
}
2014
 
2015
 
2016
/* Mark a single SCSI Device as having been reset. */
2017
 
2018
static inline void scsi_mark_device_reset(Scsi_Device *Device)
2019
{
2020
  Device->was_reset = 1;
2021
  Device->expecting_cc_ua = 1;
2022
}
2023
 
2024
 
2025
/* Mark all SCSI Devices on a specific Host as having been reset. */
2026
 
2027
void scsi_mark_host_reset(struct Scsi_Host *Host)
2028
{
2029
  Scsi_Cmnd *SCpnt;
2030
  for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
2031
      scsi_mark_device_reset(SCpnt->device);
2032
}
2033
 
2034
 
2035
/* Mark all SCSI Devices on a specific Host Bus as having been reset. */
2036
 
2037
void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
2038
{
2039
  Scsi_Cmnd *SCpnt;
2040
  for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
2041
      if (SCpnt->channel == channel)
2042
          scsi_mark_device_reset(SCpnt->device);
2043
}
2044
 
2045
 
2046
int scsi_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
2047
{
2048
    int temp;
2049
    unsigned long flags;
2050
    Scsi_Cmnd * SCpnt1;
2051
    struct Scsi_Host * host = SCpnt->host;
2052
 
2053
    printk("SCSI bus is being reset for host %d channel %d.\n",
2054
           host->host_no, SCpnt->channel);
2055
 
2056
#if 0
2057
    /*
2058
     * First of all, we need to make a recommendation to the low-level
2059
     * driver as to whether a BUS_DEVICE_RESET should be performed,
2060
     * or whether we should do a full BUS_RESET.  There is no simple
2061
     * algorithm here - we basically use a series of heuristics
2062
     * to determine what we should do.
2063
     */
2064
    SCpnt->host->suggest_bus_reset = FALSE;
2065
 
2066
    /*
2067
     * First see if all of the active devices on the bus have
2068
     * been jammed up so that we are attempting resets.  If so,
2069
     * then suggest a bus reset.  Forcing a bus reset could
2070
     * result in some race conditions, but no more than
2071
     * you would usually get with timeouts.  We will cross
2072
     * that bridge when we come to it.
2073
     *
2074
     * This is actually a pretty bad idea, since a sequence of
2075
     * commands will often timeout together and this will cause a
2076
     * Bus Device Reset followed immediately by a SCSI Bus Reset.
2077
     * If all of the active devices really are jammed up, the
2078
     * Bus Device Reset will quickly timeout and scsi_times_out
2079
     * will follow up with a SCSI Bus Reset anyway.
2080
     */
2081
    SCpnt1 = host->host_queue;
2082
    while(SCpnt1) {
2083
        if( SCpnt1->request.rq_status != RQ_INACTIVE
2084
            && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
2085
                break;
2086
        SCpnt1 = SCpnt1->next;
2087
        }
2088
    if( SCpnt1 == NULL ) {
2089
        reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
2090
    }
2091
 
2092
    /*
2093
     * If the code that called us is suggesting a hard reset, then
2094
     * definitely request it.  This usually occurs because a
2095
     * BUS_DEVICE_RESET times out.
2096
     *
2097
     * Passing reset_flags along takes care of this automatically.
2098
     */
2099
    if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET ) {
2100
        SCpnt->host->suggest_bus_reset = TRUE;
2101
    }
2102
#endif
2103
 
2104
    while (1) {
2105
        save_flags(flags);
2106
        cli();
2107
 
2108
        /*
2109
         * Protect against races here.  If the command is done, or we are
2110
         * on a different command forget it.
2111
         */
2112
        if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
2113
          if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
2114
            restore_flags(flags);
2115
            return 0;
2116
          }
2117
 
2118
        if (SCpnt->internal_timeout & IN_RESET)
2119
        {
2120
            restore_flags(flags);
2121
            while (SCpnt->internal_timeout & IN_RESET)
2122
                barrier();
2123
        }
2124
        else
2125
        {
2126
            SCpnt->internal_timeout |= IN_RESET;
2127
            update_timeout(SCpnt, RESET_TIMEOUT);
2128
 
2129
            if (host->host_busy)
2130
            {
2131
                restore_flags(flags);
2132
                SCpnt1 = host->host_queue;
2133
                while(SCpnt1) {
2134
                    if (SCpnt1->request.rq_status != RQ_INACTIVE) {
2135
#if 0
2136
                        if (!(SCpnt1->flags & IS_RESETTING) &&
2137
                            !(SCpnt1->internal_timeout & IN_ABORT))
2138
                            scsi_abort(SCpnt1, DID_RESET);
2139
#endif
2140
                        SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
2141
                    }
2142
                    SCpnt1 = SCpnt1->next;
2143
                }
2144
 
2145
                host->last_reset = jiffies;
2146
                temp = host->hostt->reset(SCpnt, reset_flags);
2147
                /*
2148
                  This test allows the driver to introduce an additional bus
2149
                  settle time delay by setting last_reset up to 20 seconds in
2150
                  the future.  In the normal case where the driver does not
2151
                  modify last_reset, it must be assumed that the actual bus
2152
                  reset occurred immediately prior to the return to this code,
2153
                  and so last_reset must be updated to the current time, so
2154
                  that the delay in internal_cmnd will guarantee at least a
2155
                  MIN_RESET_DELAY bus settle time.
2156
                */
2157
                if ((host->last_reset < jiffies) ||
2158
                    (host->last_reset > (jiffies + 20 * HZ)))
2159
                  host->last_reset = jiffies;
2160
            }
2161
            else
2162
            {
2163
                if (!host->block) host->host_busy++;
2164
                restore_flags(flags);
2165
                host->last_reset = jiffies;
2166
                SCpnt->flags |= (WAS_RESET | IS_RESETTING);
2167
                temp = host->hostt->reset(SCpnt, reset_flags);
2168
                if ((host->last_reset < jiffies) ||
2169
                    (host->last_reset > (jiffies + 20 * HZ)))
2170
                  host->last_reset = jiffies;
2171
                if (!host->block) host->host_busy--;
2172
            }
2173
 
2174
#ifdef DEBUG
2175
            printk("scsi reset function returned %d\n", temp);
2176
#endif
2177
 
2178
            /*
2179
             * Now figure out what we need to do, based upon
2180
             * what the low level driver said that it did.
2181
             * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
2182
             * or SCSI_RESET_WAKEUP, then the low level driver did a
2183
             * bus device reset or bus reset, so we should go through
2184
             * and mark one or all of the devices on that bus
2185
             * as having been reset.
2186
             */
2187
            switch(temp & SCSI_RESET_ACTION) {
2188
            case SCSI_RESET_SUCCESS:
2189
                if (temp & SCSI_RESET_HOST_RESET)
2190
                  scsi_mark_host_reset(host);
2191
                else if (temp & SCSI_RESET_BUS_RESET)
2192
                  scsi_mark_bus_reset(host, SCpnt->channel);
2193
                else scsi_mark_device_reset(SCpnt->device);
2194
                save_flags(flags);
2195
                cli();
2196
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2197
                restore_flags(flags);
2198
                return 0;
2199
            case SCSI_RESET_PENDING:
2200
                if (temp & SCSI_RESET_HOST_RESET)
2201
                  scsi_mark_host_reset(host);
2202
                else if (temp & SCSI_RESET_BUS_RESET)
2203
                  scsi_mark_bus_reset(host, SCpnt->channel);
2204
                else scsi_mark_device_reset(SCpnt->device);
2205
            case SCSI_RESET_NOT_RUNNING:
2206
                return 0;
2207
            case SCSI_RESET_PUNT:
2208
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2209
                scsi_request_sense (SCpnt);
2210
                return 0;
2211
            case SCSI_RESET_WAKEUP:
2212
                if (temp & SCSI_RESET_HOST_RESET)
2213
                  scsi_mark_host_reset(host);
2214
                else if (temp & SCSI_RESET_BUS_RESET)
2215
                  scsi_mark_bus_reset(host, SCpnt->channel);
2216
                else scsi_mark_device_reset(SCpnt->device);
2217
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2218
                scsi_request_sense (SCpnt);
2219
                /*
2220
                 * If a bus reset was performed, we
2221
                 * need to wake up each and every command
2222
                 * that was active on the bus or if it was a HBA
2223
                 * reset all active commands on all channels
2224
                 */
2225
                if( temp & SCSI_RESET_HOST_RESET )
2226
                {
2227
                    SCpnt1 = host->host_queue;
2228
                    while(SCpnt1) {
2229
                        if (SCpnt1->request.rq_status != RQ_INACTIVE
2230
                            && SCpnt1 != SCpnt)
2231
                            scsi_request_sense (SCpnt1);
2232
                        SCpnt1 = SCpnt1->next;
2233
                    }
2234
                } else if( temp & SCSI_RESET_BUS_RESET ) {
2235
                    SCpnt1 = host->host_queue;
2236
                    while(SCpnt1) {
2237
                        if(SCpnt1->request.rq_status != RQ_INACTIVE
2238
                           && SCpnt1 != SCpnt
2239
                           && SCpnt1->channel == SCpnt->channel)
2240
                            scsi_request_sense (SCpnt);
2241
                        SCpnt1 = SCpnt1->next;
2242
                    }
2243
                }
2244
                return 0;
2245
            case SCSI_RESET_SNOOZE:
2246
                /* In this case, we set the timeout field to 0
2247
                 * so that this command does not time out any more,
2248
                 * and we return 1 so that we get a message on the
2249
                 * screen.
2250
                 */
2251
                save_flags(flags);
2252
                cli();
2253
                SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2254
                update_timeout(SCpnt, 0);
2255
                restore_flags(flags);
2256
                /* If you snooze, you lose... */
2257
            case SCSI_RESET_ERROR:
2258
            default:
2259
                return 1;
2260
            }
2261
 
2262
            return temp;
2263
        }
2264
    }
2265
}
2266
 
2267
 
2268
static void scsi_main_timeout(void)
2269
{
2270
    /*
2271
     * We must not enter update_timeout with a timeout condition still pending.
2272
     */
2273
 
2274
    int timed_out;
2275
    unsigned long flags;
2276
    struct Scsi_Host * host;
2277
    Scsi_Cmnd * SCpnt = NULL;
2278
 
2279
    save_flags(flags);
2280
    cli();
2281
 
2282
    update_timeout(NULL, 0);
2283
 
2284
    /*
2285
     * Find all timers such that they have 0 or negative (shouldn't happen)
2286
     * time remaining on them.
2287
     */
2288
    timed_out = 0;
2289
    for (host = scsi_hostlist; host; host = host->next) {
2290
        for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2291
            if (SCpnt->timeout == -1)
2292
              {
2293
                SCpnt->timeout = 0;
2294
                SCpnt->serial_number_at_timeout = SCpnt->serial_number;
2295
                ++timed_out;
2296
              }
2297
    }
2298
    if (timed_out > 0) {
2299
        for (host = scsi_hostlist; host; host = host->next) {
2300
            for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2301
                if (SCpnt->serial_number_at_timeout > 0 &&
2302
                    SCpnt->serial_number_at_timeout == SCpnt->serial_number)
2303
                  {
2304
                    restore_flags(flags);
2305
                    scsi_times_out(SCpnt);
2306
                    SCpnt->serial_number_at_timeout = 0;
2307
                    cli();
2308
                  }
2309
          }
2310
    }
2311
    restore_flags(flags);
2312
}
2313
 
2314
/*
2315
 * The strategy is to cause the timer code to call scsi_times_out()
2316
 * when the soonest timeout is pending.
2317
 * The arguments are used when we are queueing a new command, because
2318
 * we do not want to subtract the time used from this time, but when we
2319
 * set the timer, we want to take this value into account.
2320
 */
2321
 
2322
static int update_timeout(Scsi_Cmnd * SCset, int timeout)
2323
{
2324
    unsigned int least, used;
2325
    unsigned int oldto;
2326
    unsigned long flags;
2327
    struct Scsi_Host * host;
2328
    Scsi_Cmnd * SCpnt = NULL;
2329
 
2330
    save_flags(flags);
2331
    cli();
2332
 
2333
    oldto = 0;
2334
 
2335
    /*
2336
     * This routine can be a performance bottleneck under high loads, since
2337
     * it is called twice per SCSI operation: once when internal_cmnd is
2338
     * called, and again when scsi_done completes the command.  To limit
2339
     * the load this routine can cause, we shortcut processing if no clock
2340
     * ticks have occurred since the last time it was called.
2341
     */
2342
 
2343
    if (jiffies == time_start && timer_table[SCSI_TIMER].expires > 0) {
2344
        if(SCset){
2345
            oldto = SCset->timeout;
2346
            SCset->timeout = timeout;
2347
            if (timeout > 0 &&
2348
                jiffies + timeout < timer_table[SCSI_TIMER].expires)
2349
                    timer_table[SCSI_TIMER].expires = jiffies + timeout;
2350
        }
2351
        restore_flags(flags);
2352
        return oldto;
2353
    }
2354
 
2355
    /*
2356
     * Figure out how much time has passed since the last time the timeouts
2357
     * were updated
2358
     */
2359
    used = (time_start) ? (jiffies - time_start) : 0;
2360
 
2361
    /*
2362
     * Find out what is due to timeout soonest, and adjust all timeouts for
2363
     * the amount of time that has passed since the last time we called
2364
     * update_timeout.
2365
     */
2366
 
2367
    oldto = 0;
2368
 
2369
    if(SCset){
2370
        oldto = SCset->timeout - used;
2371
        SCset->timeout = timeout;
2372
    }
2373
 
2374
    least = 0xffffffff;
2375
 
2376
    for(host = scsi_hostlist; host; host = host->next)
2377
        for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2378
            if (SCpnt->timeout > 0) {
2379
                if (SCpnt != SCset)
2380
                    SCpnt->timeout -= used;
2381
                if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
2382
                if(SCpnt->timeout > 0 && SCpnt->timeout < least)
2383
                    least = SCpnt->timeout;
2384
            }
2385
 
2386
    /*
2387
     * If something is due to timeout again, then we will set the next timeout
2388
     * interrupt to occur.  Otherwise, timeouts are disabled.
2389
     */
2390
 
2391
    if (least != 0xffffffff)
2392
    {
2393
        time_start = jiffies;
2394
        timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
2395
        timer_active |= 1 << SCSI_TIMER;
2396
    }
2397
    else
2398
    {
2399
        timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
2400
        timer_active &= ~(1 << SCSI_TIMER);
2401
    }
2402
    restore_flags(flags);
2403
    return oldto;
2404
}
2405
 
2406
#ifdef CONFIG_MODULES
2407
static int scsi_register_host(Scsi_Host_Template *);
2408
static void scsi_unregister_host(Scsi_Host_Template *);
2409
#endif
2410
 
2411
void *scsi_malloc(unsigned int len)
2412
{
2413
    unsigned int nbits, mask;
2414
    unsigned long flags;
2415
    int i, j;
2416
    if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
2417
        return NULL;
2418
 
2419
    save_flags(flags);
2420
    cli();
2421
    nbits = len >> 9;
2422
    mask = (1 << nbits) - 1;
2423
 
2424
    for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
2425
        for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
2426
            if ((dma_malloc_freelist[i] & (mask << j)) == 0){
2427
                dma_malloc_freelist[i] |= (mask << j);
2428
                restore_flags(flags);
2429
                dma_free_sectors -= nbits;
2430
#ifdef DEBUG
2431
                printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
2432
#endif
2433
                return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
2434
            }
2435
        }
2436
    restore_flags(flags);
2437
    return NULL;  /* Nope.  No more */
2438
}
2439
 
2440
int scsi_free(void *obj, unsigned int len)
2441
{
2442
    unsigned int page, sector, nbits, mask;
2443
    unsigned long flags;
2444
 
2445
#ifdef DEBUG
2446
    unsigned long ret = 0;
2447
 
2448
#ifdef __mips__
2449
    __asm__ __volatile__ ("move\t%0,$31":"=r"(ret));
2450
#else
2451
   ret = __builtin_return_address(0);
2452
#endif
2453
    printk("scsi_free %p %d\n",obj, len);
2454
#endif
2455
 
2456
    for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
2457
        unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
2458
        if ((unsigned long) obj >= page_addr &&
2459
            (unsigned long) obj <  page_addr + PAGE_SIZE)
2460
        {
2461
            sector = (((unsigned long) obj) - page_addr) >> 9;
2462
 
2463
            nbits = len >> 9;
2464
            mask = (1 << nbits) - 1;
2465
 
2466
            if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
2467
                panic ("scsi_free:Bad memory alignment");
2468
 
2469
            save_flags(flags);
2470
            cli();
2471
            if((dma_malloc_freelist[page] &
2472
                (mask << sector)) != (mask<<sector)){
2473
#ifdef DEBUG
2474
                printk("scsi_free(obj=%p, len=%d) called from %08lx\n",
2475
                       obj, len, ret);
2476
#endif
2477
                panic("scsi_free:Trying to free unused memory");
2478
            }
2479
            dma_free_sectors += nbits;
2480
            dma_malloc_freelist[page] &= ~(mask << sector);
2481
            restore_flags(flags);
2482
            return 0;
2483
        }
2484
    }
2485
    panic("scsi_free:Bad offset");
2486
}
2487
 
2488
 
2489
int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
2490
 
2491
void * scsi_init_malloc(unsigned int size, int priority)
2492
{
2493
    void * retval;
2494
 
2495
    /*
2496
     * For buffers used by the DMA pool, we assume page aligned
2497
     * structures.
2498
     */
2499
    if ((size % PAGE_SIZE) == 0) {
2500
        int order, a_size;
2501
        for (order = 0, a_size = PAGE_SIZE;
2502
             a_size < size; order++, a_size <<= 1)
2503
            ;
2504
        retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
2505
                                                    order);
2506
    } else
2507
        retval = kmalloc(size, priority);
2508
 
2509
    if (retval)
2510
        memset(retval, 0, size);
2511
    return retval;
2512
}
2513
 
2514
 
2515
void scsi_init_free(char * ptr, unsigned int size)
2516
{
2517
    /*
2518
     * We need this special code here because the DMA pool assumes
2519
     * page aligned data.  Besides, it is wasteful to allocate
2520
     * page sized chunks with kmalloc.
2521
     */
2522
    if ((size % PAGE_SIZE) == 0) {
2523
        int order, a_size;
2524
 
2525
        for (order = 0, a_size = PAGE_SIZE;
2526
             a_size < size; order++, a_size <<= 1)
2527
            ;
2528
        free_pages((unsigned long)ptr, order);
2529
    } else
2530
        kfree(ptr);
2531
}
2532
 
2533
void scsi_build_commandblocks(Scsi_Device * SDpnt)
2534
{
2535
    struct Scsi_Host *host = SDpnt->host;
2536
    int j;
2537
    Scsi_Cmnd * SCpnt;
2538
 
2539
    if (SDpnt->queue_depth == 0)
2540
        SDpnt->queue_depth = host->cmd_per_lun;
2541
    SDpnt->device_queue = NULL;
2542
 
2543
    for(j=0;j<SDpnt->queue_depth;j++){
2544
      SCpnt = (Scsi_Cmnd *)
2545
              scsi_init_malloc(sizeof(Scsi_Cmnd),
2546
                               GFP_ATOMIC |
2547
                               (host->unchecked_isa_dma ? GFP_DMA : 0));
2548
        SCpnt->host = host;
2549
        SCpnt->device = SDpnt;
2550
        SCpnt->target = SDpnt->id;
2551
        SCpnt->lun = SDpnt->lun;
2552
        SCpnt->channel = SDpnt->channel;
2553
        SCpnt->request.rq_status = RQ_INACTIVE;
2554
        SCpnt->use_sg = 0;
2555
        SCpnt->old_use_sg = 0;
2556
        SCpnt->old_cmd_len = 0;
2557
        SCpnt->timeout = 0;
2558
        SCpnt->underflow = 0;
2559
        SCpnt->transfersize = 0;
2560
        SCpnt->serial_number = 0;
2561
        SCpnt->serial_number_at_timeout = 0;
2562
        SCpnt->host_scribble = NULL;
2563
        if(host->host_queue)
2564
            host->host_queue->prev = SCpnt;
2565
        SCpnt->next = host->host_queue;
2566
        SCpnt->prev = NULL;
2567
        host->host_queue = SCpnt;
2568
        SCpnt->device_next = SDpnt->device_queue;
2569
        SDpnt->device_queue = SCpnt;
2570
    }
2571
    SDpnt->has_cmdblocks = 1;
2572
}
2573
 
2574
/*
2575
 * scsi_dev_init() is our initialization routine, which in turn calls host
2576
 * initialization, bus scanning, and sd/st initialization routines.
2577
 */
2578
 
2579
int scsi_dev_init(void)
2580
{
2581
    Scsi_Device * SDpnt;
2582
    struct Scsi_Host * shpnt;
2583
    struct Scsi_Device_Template * sdtpnt;
2584
#ifdef FOO_ON_YOU
2585
    return;
2586
#endif
2587
 
2588
    /* Yes we're here... */
2589
#if CONFIG_PROC_FS 
2590
    dispatch_scsi_info_ptr = dispatch_scsi_info;
2591
#endif
2592
 
2593
    /* Init a few things so we can "malloc" memory. */
2594
    scsi_loadable_module_flag = 0;
2595
 
2596
    timer_table[SCSI_TIMER].fn = scsi_main_timeout;
2597
    timer_table[SCSI_TIMER].expires = 0;
2598
 
2599
#ifdef CONFIG_MODULES
2600
    register_symtab(&scsi_symbol_table);
2601
#endif    
2602
 
2603
    /* Register the /proc/scsi/scsi entry */
2604
#if CONFIG_PROC_FS 
2605
    proc_scsi_register(0, &proc_scsi_scsi);
2606
#endif
2607
 
2608
    /* initialize all hosts */
2609
    scsi_init();
2610
 
2611
    scsi_devices = (Scsi_Device *) NULL;
2612
 
2613
    for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2614
        scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
2615
        if (shpnt->select_queue_depths != NULL)
2616
            (shpnt->select_queue_depths)(shpnt, scsi_devices);
2617
    }
2618
 
2619
    printk("scsi : detected ");
2620
    for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2621
        if (sdtpnt->dev_noticed && sdtpnt->name)
2622
            printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2623
                   (sdtpnt->dev_noticed != 1) ? "s" : "");
2624
    printk("total.\n");
2625
 
2626
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2627
        if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2628
 
2629
    for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2630
        SDpnt->scsi_request_fn = NULL;
2631
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2632
            if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2633
        if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2634
    }
2635
 
2636
 
2637
    /*
2638
     * This should build the DMA pool.
2639
     */
2640
    resize_dma_pool();
2641
 
2642
    /*
2643
     * OK, now we finish the initialization by doing spin-up, read
2644
     * capacity, etc, etc
2645
     */
2646
    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2647
        if(sdtpnt->finish && sdtpnt->nr_dev)
2648
            (*sdtpnt->finish)();
2649
 
2650
    scsi_loadable_module_flag = 1;
2651
 
2652
    return 0;
2653
}
2654
 
2655
static void print_inquiry(unsigned char *data)
2656
{
2657
    int i;
2658
 
2659
    printk("  Vendor: ");
2660
    for (i = 8; i < 16; i++)
2661
    {
2662
        if (data[i] >= 0x20 && i < data[4] + 5)
2663
            printk("%c", data[i]);
2664
        else
2665
            printk(" ");
2666
    }
2667
 
2668
    printk("  Model: ");
2669
    for (i = 16; i < 32; i++)
2670
    {
2671
        if (data[i] >= 0x20 && i < data[4] + 5)
2672
            printk("%c", data[i]);
2673
        else
2674
            printk(" ");
2675
    }
2676
 
2677
    printk("  Rev: ");
2678
    for (i = 32; i < 36; i++)
2679
    {
2680
        if (data[i] >= 0x20 && i < data[4] + 5)
2681
            printk("%c", data[i]);
2682
        else
2683
            printk(" ");
2684
    }
2685
 
2686
    printk("\n");
2687
 
2688
    i = data[0] & 0x1f;
2689
 
2690
    printk("  Type:   %s ",
2691
           i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2692
    printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2693
    if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2694
        printk(" CCS\n");
2695
    else
2696
        printk("\n");
2697
}
2698
 
2699
 
2700
#ifdef CONFIG_PROC_FS
2701
int scsi_proc_info(char *buffer, char **start, off_t offset, int length,
2702
                    int hostno, int inout)
2703
{
2704
    Scsi_Cmnd *SCpnt;
2705
    struct Scsi_Device_Template *SDTpnt;
2706
    Scsi_Device *scd, *scd_h = NULL;
2707
    struct Scsi_Host *HBA_ptr;
2708
    char *p;
2709
    int   host, channel, id, lun;
2710
    int   size, len = 0;
2711
    off_t begin = 0;
2712
    off_t pos = 0;
2713
 
2714
    scd = scsi_devices;
2715
    HBA_ptr = scsi_hostlist;
2716
 
2717
    if(inout == 0) {
2718
        size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
2719
        len += size;
2720
        pos = begin + len;
2721
        while (HBA_ptr) {
2722
#if 0
2723
            size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no,
2724
                            HBA_ptr->hostt->procname);
2725
            len += size;
2726
            pos = begin + len;
2727
#endif
2728
            scd = scsi_devices;
2729
            while (scd) {
2730
                if (scd->host == HBA_ptr) {
2731
                    proc_print_scsidevice(scd, buffer, &size, len);
2732
                    len += size;
2733
                    pos = begin + len;
2734
 
2735
                    if (pos < offset) {
2736
                        len = 0;
2737
                        begin = pos;
2738
                    }
2739
                    if (pos > offset + length)
2740
                        goto stop_output;
2741
                }
2742
                scd = scd->next;
2743
            }
2744
            HBA_ptr = HBA_ptr->next;
2745
        }
2746
 
2747
    stop_output:
2748
        *start=buffer+(offset-begin);   /* Start of wanted data */
2749
        len-=(offset-begin);            /* Start slop */
2750
        if(len>length)
2751
            len = length;               /* Ending slop */
2752
        return (len);
2753
    }
2754
 
2755
    if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
2756
        return(-EINVAL);
2757
 
2758
    /*
2759
     * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
2760
     * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
2761
     * Consider this feature BETA.
2762
     *     CAUTION: This is not for hotplugging your peripherals. As
2763
     *     SCSI was not designed for this you could damage your
2764
     *     hardware !
2765
     * However perhaps it is legal to switch on an
2766
     * already connected device. It is perhaps not
2767
     * guaranteed this device doesn't corrupt an ongoing data transfer.
2768
     */
2769
    if(!strncmp("add-single-device", buffer + 5, 17)) {
2770
        p = buffer + 23;
2771
 
2772
        host    = simple_strtoul(p, &p, 0);
2773
        channel = simple_strtoul(p+1, &p, 0);
2774
        id      = simple_strtoul(p+1, &p, 0);
2775
        lun     = simple_strtoul(p+1, &p, 0);
2776
 
2777
        printk("scsi singledevice %d %d %d %d\n", host, channel,
2778
                        id, lun);
2779
 
2780
        while(scd && (scd->host->host_no != host
2781
              || scd->channel != channel
2782
              || scd->id != id
2783
              || scd->lun != lun)) {
2784
            scd = scd->next;
2785
        }
2786
        if(scd)
2787
            return(-ENOSYS);  /* We do not yet support unplugging */
2788
        while(HBA_ptr && HBA_ptr->host_no != host)
2789
            HBA_ptr = HBA_ptr->next;
2790
 
2791
        if(!HBA_ptr)
2792
            return(-ENXIO);
2793
 
2794
        scan_scsis (HBA_ptr, 1, channel, id, lun);
2795
        return(length);
2796
 
2797
    }
2798
 
2799
    /*
2800
     * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
2801
     * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
2802
     *
2803
     * Consider this feature pre-BETA.
2804
     *
2805
     *     CAUTION: This is not for hotplugging your peripherals. As
2806
     *     SCSI was not designed for this you could damage your
2807
     *     hardware and thoroughly confuse the SCSI subsystem.
2808
     *
2809
     */
2810
    else if(!strncmp("remove-single-device", buffer + 5, 20)) {
2811
        p = buffer + 26;
2812
 
2813
        host    = simple_strtoul(p, &p, 0);
2814
        channel = simple_strtoul(p+1, &p, 0);
2815
        id      = simple_strtoul(p+1, &p, 0);
2816
        lun     = simple_strtoul(p+1, &p, 0);
2817
 
2818
        while(scd != NULL) {
2819
            if(scd->host->host_no == host
2820
               && scd->channel == channel
2821
               && scd->id == id
2822
               && scd->lun == lun){
2823
                break;
2824
            }
2825
            scd_h = scd;
2826
            scd = scd->next;
2827
        }
2828
 
2829
        if(scd == NULL)
2830
            return(-ENODEV);  /* there is no such device attached */
2831
 
2832
        if(scd->access_count)
2833
            return(-EBUSY);
2834
 
2835
        SDTpnt = scsi_devicelist;
2836
        while(SDTpnt != NULL) {
2837
            if(SDTpnt->detach) (*SDTpnt->detach)(scd);
2838
            SDTpnt = SDTpnt->next;
2839
        }
2840
 
2841
        if(scd->attached == 0) {
2842
            /*
2843
             * Nobody is using this device any more.
2844
             * Free all of the command structures.
2845
             */
2846
            for(SCpnt=scd->host->host_queue; SCpnt; SCpnt = SCpnt->next){
2847
                if(SCpnt->device == scd) {
2848
                    if(SCpnt->prev != NULL)
2849
                        SCpnt->prev->next = SCpnt->next;
2850
                    if(SCpnt->next != NULL)
2851
                        SCpnt->next->prev = SCpnt->prev;
2852
                    if(SCpnt == scd->host->host_queue)
2853
                        scd->host->host_queue = SCpnt->next;
2854
                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
2855
                }
2856
            }
2857
            /* Now we can remove the device structure */
2858
            if(scd_h != NULL) {
2859
                scd_h->next = scd->next;
2860
            } else if (scsi_devices == scd) {
2861
                /* We had a hit on the first entry of the device list */
2862
                scsi_devices = scd->next;
2863
            }
2864
            scsi_init_free((char *) scd, sizeof(Scsi_Device));
2865
        } else {
2866
            return(-EBUSY);
2867
        }
2868
        return(0);
2869
    }
2870
    return(-EINVAL);
2871
}
2872
#endif
2873
 
2874
/*
2875
 * Go through the device list and recompute the most appropriate size
2876
 * for the dma pool.  Then grab more memory (as required).
2877
 */
2878
static void resize_dma_pool(void)
2879
{
2880
    int i;
2881
    unsigned long size;
2882
    struct Scsi_Host * shpnt;
2883
    struct Scsi_Host * host = NULL;
2884
    Scsi_Device * SDpnt;
2885
    unsigned long flags;
2886
    FreeSectorBitmap * new_dma_malloc_freelist = NULL;
2887
    unsigned int new_dma_sectors = 0;
2888
    unsigned int new_need_isa_buffer = 0;
2889
    unsigned char ** new_dma_malloc_pages = NULL;
2890
 
2891
    if( !scsi_devices )
2892
    {
2893
        /*
2894
         * Free up the DMA pool.
2895
         */
2896
        if( dma_free_sectors != dma_sectors )
2897
            panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
2898
 
2899
        for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
2900
            scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
2901
        if (dma_malloc_pages)
2902
            scsi_init_free((char *) dma_malloc_pages,
2903
                           (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
2904
        dma_malloc_pages = NULL;
2905
        if (dma_malloc_freelist)
2906
            scsi_init_free((char *) dma_malloc_freelist,
2907
                           (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
2908
        dma_malloc_freelist = NULL;
2909
        dma_sectors = 0;
2910
        dma_free_sectors = 0;
2911
        return;
2912
    }
2913
    /* Next, check to see if we need to extend the DMA buffer pool */
2914
 
2915
    new_dma_sectors = 2*SECTORS_PER_PAGE;               /* Base value we use */
2916
 
2917
    if (high_memory-1 > ISA_DMA_THRESHOLD)
2918
        scsi_need_isa_bounce_buffers = 1;
2919
    else
2920
        scsi_need_isa_bounce_buffers = 0;
2921
 
2922
    if (scsi_devicelist)
2923
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2924
            new_dma_sectors += SECTORS_PER_PAGE;        /* Increment for each host */
2925
 
2926
    for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2927
        host = SDpnt->host;
2928
 
2929
        /*
2930
         * sd and sr drivers allocate scatterlists.
2931
         * sr drivers may allocate for each command 1x2048 or 2x1024 extra
2932
         * buffers for 2k sector size and 1k fs.
2933
         * sg driver allocates buffers < 4k.
2934
         * st driver does not need buffers from the dma pool.
2935
         * estimate 4k buffer/command for devices of unknown type (should panic).
2936
         */
2937
        if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM ||
2938
            SDpnt->type == TYPE_DISK || SDpnt->type == TYPE_MOD) {
2939
            new_dma_sectors += ((host->sg_tablesize *
2940
                                 sizeof(struct scatterlist) + 511) >> 9) *
2941
                               SDpnt->queue_depth;
2942
            if (SDpnt->type == TYPE_WORM || SDpnt->type == TYPE_ROM)
2943
                new_dma_sectors += (2048 >> 9) * SDpnt->queue_depth;
2944
        }
2945
        else if (SDpnt->type == TYPE_SCANNER ||
2946
                 SDpnt->type == TYPE_PROCESSOR ||
2947
                 SDpnt->type == TYPE_MEDIUM_CHANGER) {
2948
            new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
2949
        }
2950
        else {
2951
            if (SDpnt->type != TYPE_TAPE) {
2952
                printk("resize_dma_pool: unknown device type %d\n", SDpnt->type);
2953
                new_dma_sectors += (4096 >> 9) * SDpnt->queue_depth;
2954
            }
2955
        }
2956
 
2957
        if(host->unchecked_isa_dma &&
2958
           scsi_need_isa_bounce_buffers &&
2959
           SDpnt->type != TYPE_TAPE) {
2960
            new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2961
                                                  SDpnt->queue_depth;
2962
            new_need_isa_buffer++;
2963
        }
2964
    }
2965
 
2966
#ifdef DEBUG_INIT
2967
    printk("resize_dma_pool: needed dma sectors = %d\n", new_dma_sectors);
2968
#endif
2969
 
2970
    /* limit DMA memory to 32MB: */
2971
    new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2972
 
2973
    /*
2974
     * We never shrink the buffers - this leads to
2975
     * race conditions that I would rather not even think
2976
     * about right now.
2977
     */
2978
    if( new_dma_sectors < dma_sectors )
2979
        new_dma_sectors = dma_sectors;
2980
 
2981
    if (new_dma_sectors)
2982
    {
2983
        size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2984
        new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
2985
        memset(new_dma_malloc_freelist, 0, size);
2986
 
2987
        size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
2988
        new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
2989
        memset(new_dma_malloc_pages, 0, size);
2990
    }
2991
 
2992
    /*
2993
     * If we need more buffers, expand the list.
2994
     */
2995
    if( new_dma_sectors > dma_sectors ) {
2996
        for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
2997
            new_dma_malloc_pages[i] = (unsigned char *)
2998
                scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2999
    }
3000
 
3001
    /* When we dick with the actual DMA list, we need to
3002
     * protect things
3003
     */
3004
    save_flags(flags);
3005
    cli();
3006
    if (dma_malloc_freelist)
3007
    {
3008
        size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
3009
        memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
3010
        scsi_init_free((char *) dma_malloc_freelist, size);
3011
    }
3012
    dma_malloc_freelist = new_dma_malloc_freelist;
3013
 
3014
    if (dma_malloc_pages)
3015
    {
3016
        size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
3017
        memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
3018
        scsi_init_free((char *) dma_malloc_pages, size);
3019
    }
3020
 
3021
    dma_free_sectors += new_dma_sectors - dma_sectors;
3022
    dma_malloc_pages = new_dma_malloc_pages;
3023
    dma_sectors = new_dma_sectors;
3024
    need_isa_buffer = new_need_isa_buffer;
3025
    restore_flags(flags);
3026
 
3027
#ifdef DEBUG_INIT
3028
    printk("resize_dma_pool: dma free sectors   = %d\n", dma_free_sectors);
3029
    printk("resize_dma_pool: dma sectors        = %d\n", dma_sectors);
3030
    printk("resize_dma_pool: need isa buffers   = %d\n", need_isa_buffer);
3031
#endif
3032
}
3033
 
3034
#ifdef CONFIG_MODULES           /* a big #ifdef block... */
3035
 
3036
/*
3037
 * This entry point should be called by a loadable module if it is trying
3038
 * add a low level scsi driver to the system.
3039
 */
3040
static int scsi_register_host(Scsi_Host_Template * tpnt)
3041
{
3042
    int pcount;
3043
    struct Scsi_Host * shpnt;
3044
    Scsi_Device * SDpnt;
3045
    struct Scsi_Device_Template * sdtpnt;
3046
    const char * name;
3047
 
3048
    if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
3049
                                               * no detect routine available
3050
                                               */
3051
    pcount = next_scsi_host;
3052
    if ((tpnt->present = tpnt->detect(tpnt)))
3053
    {
3054
        if(pcount == next_scsi_host) {
3055
            if(tpnt->present > 1) {
3056
                printk("Failure to register low-level scsi driver");
3057
                scsi_unregister_host(tpnt);
3058
                return 1;
3059
            }
3060
            /* The low-level driver failed to register a driver.  We
3061
             *  can do this now.
3062
             */
3063
            scsi_register(tpnt,0);
3064
        }
3065
        tpnt->next = scsi_hosts; /* Add to the linked list */
3066
        scsi_hosts = tpnt;
3067
 
3068
        /* Add the new driver to /proc/scsi */
3069
#if CONFIG_PROC_FS 
3070
        build_proc_dir_entries(tpnt);
3071
#endif
3072
 
3073
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
3074
            if(shpnt->hostt == tpnt)
3075
            {
3076
                if(tpnt->info)
3077
                    name = tpnt->info(shpnt);
3078
                else
3079
                    name = tpnt->name;
3080
                printk ("scsi%d : %s\n", /* And print a little message */
3081
                        shpnt->host_no, name);
3082
            }
3083
 
3084
        printk ("scsi : %d host%s.\n", next_scsi_host,
3085
                (next_scsi_host == 1) ? "" : "s");
3086
 
3087
        scsi_make_blocked_list();
3088
 
3089
        /* The next step is to call scan_scsis here.  This generates the
3090
         * Scsi_Devices entries
3091
         */
3092
 
3093
        for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
3094
            if(shpnt->hostt == tpnt) {
3095
              scan_scsis(shpnt,0,0,0,0);
3096
              if (shpnt->select_queue_depths != NULL)
3097
                  (shpnt->select_queue_depths)(shpnt, scsi_devices);
3098
            }
3099
 
3100
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
3101
            if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
3102
 
3103
        /* Next we create the Scsi_Cmnd structures for this host */
3104
 
3105
        for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3106
            if(SDpnt->host->hostt == tpnt)
3107
            {
3108
                for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
3109
                    if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
3110
                if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
3111
            }
3112
 
3113
        /*
3114
         * Now that we have all of the devices, resize the DMA pool,
3115
         * as required.  */
3116
        resize_dma_pool();
3117
 
3118
 
3119
        /* This does any final handling that is required. */
3120
        for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
3121
            if(sdtpnt->finish && sdtpnt->nr_dev)
3122
                (*sdtpnt->finish)();
3123
    }
3124
 
3125
#if defined(USE_STATIC_SCSI_MEMORY)
3126
    printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
3127
            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
3128
            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
3129
            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
3130
#endif
3131
 
3132
    MOD_INC_USE_COUNT;
3133
    return 0;
3134
}
3135
 
3136
/*
3137
 * Similarly, this entry point should be called by a loadable module if it
3138
 * is trying to remove a low level scsi driver from the system.
3139
 */
3140
static void scsi_unregister_host(Scsi_Host_Template * tpnt)
3141
{
3142
    Scsi_Host_Template * SHT, *SHTp;
3143
    Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
3144
    Scsi_Cmnd * SCpnt;
3145
    unsigned long flags;
3146
    struct Scsi_Device_Template * sdtpnt;
3147
    struct Scsi_Host * shpnt, *sh1;
3148
    int pcount;
3149
 
3150
    /* First verify that this host adapter is completely free with no pending
3151
     * commands */
3152
 
3153
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3154
        if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
3155
           && *sdpnt->host->hostt->usage_count) return;
3156
 
3157
    for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3158
    {
3159
        if (shpnt->hostt != tpnt) continue;
3160
        for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3161
        {
3162
            save_flags(flags);
3163
            cli();
3164
            if(SCpnt->request.rq_status != RQ_INACTIVE) {
3165
                restore_flags(flags);
3166
                for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3167
                    if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
3168
                        SCpnt->request.rq_status = RQ_INACTIVE;
3169
                printk("Device busy???\n");
3170
                return;
3171
            }
3172
            SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
3173
            restore_flags(flags);
3174
        }
3175
    }
3176
    /* Next we detach the high level drivers from the Scsi_Device structures */
3177
 
3178
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3179
        if(sdpnt->host->hostt == tpnt)
3180
        {
3181
            for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
3182
                if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
3183
            /* If something still attached, punt */
3184
            if (sdpnt->attached) {
3185
                printk("Attached usage count = %d\n", sdpnt->attached);
3186
                return;
3187
            }
3188
        }
3189
 
3190
    /* Next we free up the Scsi_Cmnd structures for this host */
3191
 
3192
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3193
        if(sdpnt->host->hostt == tpnt)
3194
            while (sdpnt->host->host_queue) {
3195
                SCpnt = sdpnt->host->host_queue->next;
3196
                scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
3197
                sdpnt->host->host_queue = SCpnt;
3198
                if (SCpnt) SCpnt->prev = NULL;
3199
                sdpnt->has_cmdblocks = 0;
3200
            }
3201
 
3202
    /* Next free up the Scsi_Device structures for this host */
3203
 
3204
    sdppnt = NULL;
3205
    for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
3206
    {
3207
        sdpnt1 = sdpnt->next;
3208
        if (sdpnt->host->hostt == tpnt) {
3209
            if (sdppnt)
3210
                sdppnt->next = sdpnt->next;
3211
            else
3212
                scsi_devices = sdpnt->next;
3213
            scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
3214
        } else
3215
            sdppnt = sdpnt;
3216
    }
3217
 
3218
    /* Next we go through and remove the instances of the individual hosts
3219
     * that were detected */
3220
 
3221
    shpnt = scsi_hostlist;
3222
    while(shpnt) {
3223
        sh1 = shpnt->next;
3224
        if(shpnt->hostt == tpnt) {
3225
            if(shpnt->loaded_as_module) {
3226
                pcount = next_scsi_host;
3227
                /* Remove the /proc/scsi directory entry */
3228
#if CONFIG_PROC_FS 
3229
                proc_scsi_unregister(tpnt->proc_dir,
3230
                                     shpnt->host_no + PROC_SCSI_FILE);
3231
#endif   
3232
                if(tpnt->release)
3233
                    (*tpnt->release)(shpnt);
3234
                else {
3235
                    /* This is the default case for the release function.
3236
                     * It should do the right thing for most correctly
3237
                     * written host adapters.
3238
                     */
3239
                    if (shpnt->irq) free_irq(shpnt->irq, NULL);
3240
                    if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
3241
                    if (shpnt->io_port && shpnt->n_io_port)
3242
                        release_region(shpnt->io_port, shpnt->n_io_port);
3243
                }
3244
                if(pcount == next_scsi_host) scsi_unregister(shpnt);
3245
                tpnt->present--;
3246
            }
3247
        }
3248
        shpnt = sh1;
3249
    }
3250
 
3251
    /*
3252
     * If there are absolutely no more hosts left, it is safe
3253
     * to completely nuke the DMA pool.  The resize operation will
3254
     * do the right thing and free everything.
3255
     */
3256
    if( !scsi_devices )
3257
        resize_dma_pool();
3258
 
3259
    printk ("scsi : %d host%s.\n", next_scsi_host,
3260
            (next_scsi_host == 1) ? "" : "s");
3261
 
3262
#if defined(USE_STATIC_SCSI_MEMORY)
3263
    printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
3264
            (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
3265
            (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
3266
            (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
3267
#endif
3268
 
3269
    scsi_make_blocked_list();
3270
 
3271
    /* There were some hosts that were loaded at boot time, so we cannot
3272
       do any more than this */
3273
    if (tpnt->present) return;
3274
 
3275
    /* OK, this is the very last step.  Remove this host adapter from the
3276
       linked list. */
3277
    for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
3278
        if(SHT == tpnt) {
3279
            if(SHTp)
3280
                SHTp->next = SHT->next;
3281
            else
3282
                scsi_hosts = SHT->next;
3283
            SHT->next = NULL;
3284
            break;
3285
        }
3286
 
3287
    /* Rebuild the /proc/scsi directory entries */
3288
#if CONFIG_PROC_FS 
3289
    proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
3290
#endif
3291
    MOD_DEC_USE_COUNT;
3292
}
3293
 
3294
/*
3295
 * This entry point should be called by a loadable module if it is trying
3296
 * add a high level scsi driver to the system.
3297
 */
3298
static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
3299
{
3300
    Scsi_Device * SDpnt;
3301
 
3302
    if (tpnt->next) return 1;
3303
 
3304
    scsi_register_device(tpnt);
3305
    /*
3306
     * First scan the devices that we know about, and see if we notice them.
3307
     */
3308
 
3309
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3310
        if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
3311
 
3312
    /*
3313
     * If any of the devices would match this driver, then perform the
3314
     * init function.
3315
     */
3316
    if(tpnt->init && tpnt->dev_noticed)
3317
        if ((*tpnt->init)()) return 1;
3318
 
3319
    /*
3320
     * Now actually connect the devices to the new driver.
3321
     */
3322
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3323
    {
3324
        if(tpnt->attach)  (*tpnt->attach)(SDpnt);
3325
        /*
3326
         * If this driver attached to the device, and we no longer
3327
         * have anything attached, release the scsi command blocks.
3328
         */
3329
        if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
3330
            scsi_build_commandblocks(SDpnt);
3331
    }
3332
 
3333
    /*
3334
     * This does any final handling that is required.
3335
     */
3336
    if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
3337
    MOD_INC_USE_COUNT;
3338
    return 0;
3339
}
3340
 
3341
static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
3342
{
3343
    Scsi_Device * SDpnt;
3344
    Scsi_Cmnd * SCpnt;
3345
    struct Scsi_Device_Template * spnt;
3346
    struct Scsi_Device_Template * prev_spnt;
3347
 
3348
    /*
3349
     * If we are busy, this is not going to fly.
3350
     */
3351
    if( *tpnt->usage_count != 0) return 0;
3352
    /*
3353
     * Next, detach the devices from the driver.
3354
     */
3355
 
3356
    for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3357
    {
3358
        if(tpnt->detach) (*tpnt->detach)(SDpnt);
3359
        if(SDpnt->attached == 0)
3360
        {
3361
            /*
3362
             * Nobody is using this device any more.  Free all of the
3363
             * command structures.
3364
             */
3365
            for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
3366
            {
3367
                if(SCpnt->device == SDpnt)
3368
                {
3369
                    if(SCpnt->prev != NULL)
3370
                        SCpnt->prev->next = SCpnt->next;
3371
                    if(SCpnt->next != NULL)
3372
                        SCpnt->next->prev = SCpnt->prev;
3373
                    if(SCpnt == SDpnt->host->host_queue)
3374
                        SDpnt->host->host_queue = SCpnt->next;
3375
                    scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
3376
                }
3377
            }
3378
            SDpnt->has_cmdblocks = 0;
3379
        }
3380
    }
3381
    /*
3382
     * Extract the template from the linked list.
3383
     */
3384
    spnt = scsi_devicelist;
3385
    prev_spnt = NULL;
3386
    while(spnt != tpnt)
3387
    {
3388
        prev_spnt = spnt;
3389
        spnt = spnt->next;
3390
    }
3391
    if(prev_spnt == NULL)
3392
        scsi_devicelist = tpnt->next;
3393
    else
3394
        prev_spnt->next = spnt->next;
3395
 
3396
    MOD_DEC_USE_COUNT;
3397
    /*
3398
     * Final cleanup for the driver is done in the driver sources in the
3399
     * cleanup function.
3400
     */
3401
    return 0;
3402
}
3403
 
3404
 
3405
int scsi_register_module(int module_type, void * ptr)
3406
{
3407
    switch(module_type){
3408
    case MODULE_SCSI_HA:
3409
        return scsi_register_host((Scsi_Host_Template *) ptr);
3410
 
3411
        /* Load upper level device handler of some kind */
3412
    case MODULE_SCSI_DEV:
3413
#ifdef CONFIG_KERNELD
3414
        if (scsi_hosts == NULL)
3415
                request_module("scsi_hostadapter");
3416
#endif
3417
        return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
3418
        /* The rest of these are not yet implemented */
3419
 
3420
        /* Load constants.o */
3421
    case MODULE_SCSI_CONST:
3422
 
3423
        /* Load specialized ioctl handler for some device.  Intended for
3424
         * cdroms that have non-SCSI2 audio command sets. */
3425
    case MODULE_SCSI_IOCTL:
3426
 
3427
    default:
3428
        return 1;
3429
    }
3430
}
3431
 
3432
void scsi_unregister_module(int module_type, void * ptr)
3433
{
3434
    switch(module_type) {
3435
    case MODULE_SCSI_HA:
3436
        scsi_unregister_host((Scsi_Host_Template *) ptr);
3437
        break;
3438
    case MODULE_SCSI_DEV:
3439
        scsi_unregister_device((struct Scsi_Device_Template *) ptr);
3440
        break;
3441
        /* The rest of these are not yet implemented. */
3442
    case MODULE_SCSI_CONST:
3443
    case MODULE_SCSI_IOCTL:
3444
        break;
3445
    default:
3446
    }
3447
    return;
3448
}
3449
 
3450
#endif          /* CONFIG_MODULES */
3451
 
3452
#ifdef DEBUG_TIMEOUT
3453
static void
3454
scsi_dump_status(void)
3455
{
3456
    int i;
3457
    struct Scsi_Host * shpnt;
3458
    Scsi_Cmnd * SCpnt;
3459
    printk("Dump of scsi parameters:\n");
3460
    i = 0;
3461
    for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3462
        for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3463
        {
3464
            /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
3465
            printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
3466
                   i++, SCpnt->host->host_no,
3467
                   SCpnt->channel,
3468
                   SCpnt->target,
3469
                   SCpnt->lun,
3470
                   kdevname(SCpnt->request.rq_dev),
3471
                   SCpnt->request.sector,
3472
                   SCpnt->request.nr_sectors,
3473
                   SCpnt->request.current_nr_sectors,
3474
                   SCpnt->use_sg,
3475
                   SCpnt->retries,
3476
                   SCpnt->allowed,
3477
                   SCpnt->flags,
3478
                   SCpnt->timeout_per_command,
3479
                   SCpnt->timeout,
3480
                   SCpnt->internal_timeout,
3481
                   SCpnt->cmnd[0],
3482
                   SCpnt->sense_buffer[2],
3483
                   SCpnt->result);
3484
        }
3485
    printk("wait_for_request = %p\n", wait_for_request);
3486
    /* Now dump the request lists for each block device */
3487
    printk("Dump of pending block device requests\n");
3488
    for(i=0; i<MAX_BLKDEV; i++)
3489
        if(blk_dev[i].current_request)
3490
        {
3491
            struct request * req;
3492
            printk("%d: ", i);
3493
            req = blk_dev[i].current_request;
3494
            while(req) {
3495
                printk("(%s %d %ld %ld %ld) ",
3496
                       kdevname(req->rq_dev),
3497
                       req->cmd,
3498
                       req->sector,
3499
                       req->nr_sectors,
3500
                       req->current_nr_sectors);
3501
                req = req->next;
3502
            }
3503
            printk("\n");
3504
        }
3505
}
3506
#endif
3507
 
3508
#ifdef MODULE
3509
 
3510
int init_module(void) {
3511
    unsigned long size;
3512
 
3513
    /*
3514
     * This makes /proc/scsi visible.
3515
     */
3516
#if CONFIG_PROC_FS
3517
    dispatch_scsi_info_ptr = dispatch_scsi_info;
3518
#endif
3519
 
3520
    timer_table[SCSI_TIMER].fn = scsi_main_timeout;
3521
    timer_table[SCSI_TIMER].expires = 0;
3522
    register_symtab(&scsi_symbol_table);
3523
    scsi_loadable_module_flag = 1;
3524
 
3525
    /* Register the /proc/scsi/scsi entry */
3526
#if CONFIG_PROC_FS
3527
    proc_scsi_register(0, &proc_scsi_scsi);
3528
#endif
3529
 
3530
 
3531
    dma_sectors = PAGE_SIZE / SECTOR_SIZE;
3532
    dma_free_sectors= dma_sectors;
3533
    /*
3534
     * Set up a minimal DMA buffer list - this will be used during scan_scsis
3535
     * in some cases.
3536
     */
3537
 
3538
    /* One bit per sector to indicate free/busy */
3539
    size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
3540
    dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
3541
    memset(dma_malloc_freelist, 0, size);
3542
 
3543
    /* One pointer per page for the page list */
3544
    dma_malloc_pages = (unsigned char **)
3545
        scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
3546
    dma_malloc_pages[0] = (unsigned char *)
3547
        scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
3548
    return 0;
3549
}
3550
 
3551
void cleanup_module( void)
3552
{
3553
#if CONFIG_PROC_FS
3554
    proc_scsi_unregister(0, PROC_SCSI_SCSI);
3555
 
3556
    /* No, we're not here anymore. Don't show the /proc/scsi files. */
3557
    dispatch_scsi_info_ptr = 0L;
3558
#endif
3559
 
3560
    /*
3561
     * Free up the DMA pool.
3562
     */
3563
    resize_dma_pool();
3564
 
3565
    timer_table[SCSI_TIMER].fn = NULL;
3566
    timer_table[SCSI_TIMER].expires = 0;
3567
}
3568
#endif /* MODULE */
3569
 
3570
/*
3571
 * Overrides for Emacs so that we follow Linus's tabbing style.
3572
 * Emacs will notice this stuff at the end of the file and automatically
3573
 * adjust the settings for this buffer only.  This must remain at the end
3574
 * of the file.
3575
 * ---------------------------------------------------------------------------
3576
 * Local variables:
3577
 * c-indent-level: 4
3578
 * c-brace-imaginary-offset: 0
3579
 * c-brace-offset: -4
3580
 * c-argdecl-indent: 4
3581
 * c-label-offset: -4
3582
 * c-continued-statement-offset: 4
3583
 * c-continued-brace-offset: 0
3584
 * indent-tabs-mode: nil
3585
 * tab-width: 8
3586
 * End:
3587
 */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.