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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [scsi/] [scsi.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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