~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to xen/drivers/scsi/sym53c8xx_2/sym_glue.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family 
3
 
 * of PCI-SCSI IO processors.
4
 
 *
5
 
 * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6
 
 *
7
 
 * This driver is derived from the Linux sym53c8xx driver.
8
 
 * Copyright (C) 1998-2000  Gerard Roudier
9
 
 *
10
 
 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
11
 
 * a port of the FreeBSD ncr driver to Linux-1.2.13.
12
 
 *
13
 
 * The original ncr driver has been written for 386bsd and FreeBSD by
14
 
 *         Wolfgang Stanglmeier        <wolf@cologne.de>
15
 
 *         Stefan Esser                <se@mi.Uni-Koeln.de>
16
 
 * Copyright (C) 1994  Wolfgang Stanglmeier
17
 
 *
18
 
 * Other major contributions:
19
 
 *
20
 
 * NVRAM detection and reading.
21
 
 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22
 
 *
23
 
 *-----------------------------------------------------------------------------
24
 
 *
25
 
 * Redistribution and use in source and binary forms, with or without
26
 
 * modification, are permitted provided that the following conditions
27
 
 * are met:
28
 
 * 1. Redistributions of source code must retain the above copyright
29
 
 *    notice, this list of conditions and the following disclaimer.
30
 
 * 2. The name of the author may not be used to endorse or promote products
31
 
 *    derived from this software without specific prior written permission.
32
 
 *
33
 
 * Where this Software is combined with software released under the terms of 
34
 
 * the GNU Public License ("GPL") and the terms of the GPL would require the 
35
 
 * combined work to also be released under the terms of the GPL, the terms
36
 
 * and conditions of this License will apply in addition to those of the
37
 
 * GPL with the exception of any terms or conditions of this License that
38
 
 * conflict with, or are expressly prohibited by, the GPL.
39
 
 *
40
 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
41
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
44
 
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 
 * SUCH DAMAGE.
51
 
 */
52
 
#define SYM_GLUE_C
53
 
 
54
 
#include <linux/module.h>
55
 
#include "sym_glue.h"
56
 
 
57
 
#define NAME53C         "sym53c"
58
 
#define NAME53C8XX      "sym53c8xx"
59
 
 
60
 
/*
61
 
 *  Simple Wrapper to kernel PCI bus interface.
62
 
 */
63
 
 
64
 
typedef struct pci_dev *pcidev_t;
65
 
#define PCIDEV_NULL             (0)
66
 
#define PciBusNumber(d)         (d)->bus->number
67
 
#define PciDeviceFn(d)          (d)->devfn
68
 
#define PciVendorId(d)          (d)->vendor
69
 
#define PciDeviceId(d)          (d)->device
70
 
#define PciIrqLine(d)           (d)->irq
71
 
 
72
 
static u_long __init
73
 
pci_get_base_cookie(struct pci_dev *pdev, int index)
74
 
{
75
 
        u_long base;
76
 
 
77
 
#if LINUX_VERSION_CODE > LinuxVersionCode(2,3,12)
78
 
        base = pdev->resource[index].start;
79
 
#else
80
 
        base = pdev->base_address[index];
81
 
#if BITS_PER_LONG > 32
82
 
        if ((base & 0x7) == 0x4)
83
 
                base |= (((u_long)pdev->base_address[++index]) << 32);
84
 
#endif
85
 
#endif
86
 
        return (base & ~0x7ul);
87
 
}
88
 
 
89
 
static int __init
90
 
pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
91
 
{
92
 
        u32 tmp;
93
 
#define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
94
 
 
95
 
        pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
96
 
        *base = tmp;
97
 
        ++index;
98
 
        if ((tmp & 0x7) == 0x4) {
99
 
#if BITS_PER_LONG > 32
100
 
                pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
101
 
                *base |= (((u_long)tmp) << 32);
102
 
#endif
103
 
                ++index;
104
 
        }
105
 
        return index;
106
 
#undef PCI_BAR_OFFSET
107
 
}
108
 
 
109
 
#if LINUX_VERSION_CODE  < LinuxVersionCode(2,4,0)
110
 
#define pci_enable_device(pdev)         (0)
111
 
#endif
112
 
 
113
 
#if LINUX_VERSION_CODE  < LinuxVersionCode(2,4,4)
114
 
#define scsi_set_pci_device(inst, pdev) do { ;} while (0)
115
 
#endif
116
 
 
117
 
/*
118
 
 *  Insert a delay in micro-seconds and milli-seconds.
119
 
 */
120
 
void sym_udelay(int us) { udelay(us); }
121
 
void sym_mdelay(int ms) { mdelay(ms); }
122
 
 
123
 
/*
124
 
 *  SMP threading.
125
 
 *
126
 
 *  The whole SCSI sub-system under Linux is basically single-threaded.
127
 
 *  Everything, including low-level driver interrupt routine, happens 
128
 
 *  with the `io_request_lock' held.
129
 
 *  The sym53c8xx-1.x drivers series ran their interrupt code using a 
130
 
 *  spin mutex per controller. This added complexity without improving 
131
 
 *  scalability significantly. the sym-2 driver still use a spinlock 
132
 
 *  per controller for safety, but basically runs with the damned 
133
 
 *  io_request_lock held.
134
 
 */
135
 
 
136
 
spinlock_t sym53c8xx_lock = SPIN_LOCK_UNLOCKED;
137
 
 
138
 
#define SYM_LOCK_DRIVER(flags)    spin_lock_irqsave(&sym53c8xx_lock, flags)
139
 
#define SYM_UNLOCK_DRIVER(flags)  spin_unlock_irqrestore(&sym53c8xx_lock,flags)
140
 
 
141
 
#define SYM_INIT_LOCK_HCB(np)     spin_lock_init(&np->s.smp_lock);
142
 
#define SYM_LOCK_HCB(np, flags)   spin_lock_irqsave(&np->s.smp_lock, flags)
143
 
#define SYM_UNLOCK_HCB(np, flags) spin_unlock_irqrestore(&np->s.smp_lock, flags)
144
 
 
145
 
#define SYM_LOCK_SCSI(np, flags) \
146
 
                spin_lock_irqsave(&io_request_lock, flags)
147
 
#define SYM_UNLOCK_SCSI(np, flags) \
148
 
                spin_unlock_irqrestore(&io_request_lock, flags)
149
 
 
150
 
/* Ugly, but will make things easier if this locking will ever disappear */
151
 
#define SYM_LOCK_SCSI_NOSAVE(np)        spin_lock_irq(&io_request_lock)
152
 
#define SYM_UNLOCK_SCSI_NORESTORE(np)   spin_unlock_irq(&io_request_lock)
153
 
 
154
 
/*
155
 
 *  These simple macros limit expression involving 
156
 
 *  kernel time values (jiffies) to some that have 
157
 
 *  chance not to be too much incorrect. :-)
158
 
 */
159
 
#define ktime_get(o)            (jiffies + (u_long) o)
160
 
#define ktime_exp(b)            ((long)(jiffies) - (long)(b) >= 0)
161
 
#define ktime_dif(a, b)         ((long)(a) - (long)(b))
162
 
#define ktime_add(a, o)         ((a) + (u_long)(o))
163
 
#define ktime_sub(a, o)         ((a) - (u_long)(o))
164
 
 
165
 
/*
166
 
 *  Wrappers to the generic memory allocator.
167
 
 */
168
 
void *sym_calloc(int size, char *name)
169
 
{
170
 
        u_long flags;
171
 
        void *m;
172
 
        SYM_LOCK_DRIVER(flags);
173
 
        m = sym_calloc_unlocked(size, name);
174
 
        SYM_UNLOCK_DRIVER(flags);
175
 
        return m;
176
 
}
177
 
 
178
 
void sym_mfree(void *m, int size, char *name)
179
 
{
180
 
        u_long flags;
181
 
        SYM_LOCK_DRIVER(flags);
182
 
        sym_mfree_unlocked(m, size, name);
183
 
        SYM_UNLOCK_DRIVER(flags);
184
 
}
185
 
 
186
 
#ifdef  SYM_LINUX_DYNAMIC_DMA_MAPPING
187
 
 
188
 
void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
189
 
{
190
 
        u_long flags;
191
 
        void *m;
192
 
        SYM_LOCK_DRIVER(flags);
193
 
        m = __sym_calloc_dma_unlocked(dev_dmat, size, name);
194
 
        SYM_UNLOCK_DRIVER(flags);
195
 
        return m;
196
 
}
197
 
 
198
 
void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
199
 
{
200
 
        u_long flags;
201
 
        SYM_LOCK_DRIVER(flags);
202
 
        __sym_mfree_dma_unlocked(dev_dmat, m, size, name);
203
 
        SYM_UNLOCK_DRIVER(flags);
204
 
}
205
 
 
206
 
m_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
207
 
{
208
 
        u_long flags;
209
 
        m_addr_t b;
210
 
        SYM_LOCK_DRIVER(flags);
211
 
        b = __vtobus_unlocked(dev_dmat, m);
212
 
        SYM_UNLOCK_DRIVER(flags);
213
 
        return b;
214
 
}
215
 
 
216
 
#endif  /* SYM_LINUX_DYNAMIC_DMA_MAPPING */
217
 
 
218
 
 
219
 
/*
220
 
 *  Map/unmap a PCI memory window.
221
 
 */
222
 
#ifndef SYM_OPT_NO_BUS_MEMORY_MAPPING
223
 
static u_long __init pci_map_mem(u_long base, u_long size)
224
 
{
225
 
        u_long page_base        = ((u_long) base) & PAGE_MASK;
226
 
        u_long page_offs        = ((u_long) base) - page_base;
227
 
        u_long page_remapped    = (u_long) ioremap(page_base, page_offs+size);
228
 
 
229
 
        return page_remapped? (page_remapped + page_offs) : 0UL;
230
 
}
231
 
 
232
 
static void __init pci_unmap_mem(u_long vaddr, u_long size)
233
 
{
234
 
        if (vaddr)
235
 
                iounmap((void *) (vaddr & PAGE_MASK));
236
 
}
237
 
#endif
238
 
 
239
 
/*
240
 
 *  Used to retrieve the host structure when the 
241
 
 *  driver is called from the proc FS.
242
 
 */
243
 
static struct Scsi_Host *first_host = NULL;
244
 
 
245
 
/*
246
 
 *  /proc directory entry and proc_info.
247
 
 */
248
 
#if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
249
 
static struct proc_dir_entry proc_scsi_sym53c8xx = {
250
 
    PROC_SCSI_SYM53C8XX, 9, NAME53C8XX,
251
 
    S_IFDIR | S_IRUGO | S_IXUGO, 2
252
 
};
253
 
#endif
254
 
 
255
 
/*
256
 
 *  Transfer direction
257
 
 *
258
 
 *  Until some linux kernel version near 2.3.40, low-level scsi 
259
 
 *  drivers were not told about data transfer direction.
260
 
 */
261
 
#if LINUX_VERSION_CODE > LinuxVersionCode(2, 3, 40)
262
 
 
263
 
#define scsi_data_direction(cmd)        (cmd->sc_data_direction)
264
 
 
265
 
#else
266
 
 
267
 
static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
268
 
{
269
 
        int direction;
270
 
 
271
 
        switch((int) cmd->cmnd[0]) {
272
 
        case 0x08:  /*  READ(6)                         08 */
273
 
        case 0x28:  /*  READ(10)                        28 */
274
 
        case 0xA8:  /*  READ(12)                        A8 */
275
 
                direction = SCSI_DATA_READ;
276
 
                break;
277
 
        case 0x0A:  /*  WRITE(6)                        0A */
278
 
        case 0x2A:  /*  WRITE(10)                       2A */
279
 
        case 0xAA:  /*  WRITE(12)                       AA */
280
 
                direction = SCSI_DATA_WRITE;
281
 
                break;
282
 
        default:
283
 
                direction = SCSI_DATA_UNKNOWN;
284
 
                break;
285
 
        }
286
 
 
287
 
        return direction;
288
 
}
289
 
 
290
 
#endif
291
 
 
292
 
/*
293
 
 *  Driver host data structure.
294
 
 */
295
 
struct host_data {
296
 
     hcb_p ncb;
297
 
};
298
 
 
299
 
/*
300
 
 * Some type that fit DMA addresses as seen from BUS.
301
 
 */
302
 
#ifndef SYM_LINUX_DYNAMIC_DMA_MAPPING
303
 
typedef u_long          bus_addr_t;
304
 
#else
305
 
#if     SYM_CONF_DMA_ADDRESSING_MODE > 0
306
 
typedef dma64_addr_t    bus_addr_t;
307
 
#else
308
 
typedef dma_addr_t      bus_addr_t;
309
 
#endif
310
 
#endif
311
 
 
312
 
/*
313
 
 *  Used by the eh thread to wait for command completion.
314
 
 *  It is allocated on the eh thread stack.
315
 
 */
316
 
struct sym_eh_wait {
317
 
/* SAE: */
318
 
#ifdef XENO_KILLED
319
 
        struct semaphore sem;
320
 
#endif
321
 
        struct timer_list timer;
322
 
        void (*old_done)(Scsi_Cmnd *);
323
 
        int to_do;
324
 
        int timed_out;
325
 
};
326
 
 
327
 
/*
328
 
 *  Driver private area in the SCSI command structure.
329
 
 */
330
 
struct sym_ucmd {               /* Override the SCSI pointer structure */
331
 
        SYM_QUEHEAD link_cmdq;  /* Must stay at offset ZERO */
332
 
#ifdef SYM_LINUX_DYNAMIC_DMA_MAPPING
333
 
        bus_addr_t data_mapping;
334
 
        u_char  data_mapped;
335
 
#endif
336
 
        struct sym_eh_wait *eh_wait;
337
 
};
338
 
 
339
 
typedef struct sym_ucmd *ucmd_p;
340
 
 
341
 
#define SYM_UCMD_PTR(cmd)  ((ucmd_p)(&(cmd)->SCp))
342
 
#define SYM_SCMD_PTR(ucmd) sym_que_entry(ucmd, Scsi_Cmnd, SCp)
343
 
#define SYM_SOFTC_PTR(cmd) (((struct host_data *)cmd->host->hostdata)->ncb)
344
 
 
345
 
/*
346
 
 *  Deal with DMA mapping/unmapping.
347
 
 */
348
 
 
349
 
#ifndef SYM_LINUX_DYNAMIC_DMA_MAPPING
350
 
 
351
 
/* Linux versions prior to pci bus iommu kernel interface */
352
 
 
353
 
#define __unmap_scsi_data(pdev, cmd)    do {; } while (0)
354
 
#define __map_scsi_single_data(pdev, cmd) (__vtobus(pdev,(cmd)->request_buffer))
355
 
#define __map_scsi_sg_data(pdev, cmd)   ((cmd)->use_sg)
356
 
#define __sync_scsi_data(pdev, cmd)     do {; } while (0)
357
 
 
358
 
#define bus_sg_dma_address(sc)          vtobus((sc)->address)
359
 
#define bus_sg_dma_len(sc)              ((sc)->length)
360
 
 
361
 
#else /* Linux version with pci bus iommu kernel interface */
362
 
 
363
 
#define bus_unmap_sg(pdev, sgptr, sgcnt, dir)           \
364
 
        pci_unmap_sg(pdev, sgptr, sgcnt, dir)
365
 
 
366
 
#define bus_unmap_single(pdev, mapping, bufptr, dir)    \
367
 
        pci_unmap_single(pdev, mapping, bufptr, dir)
368
 
 
369
 
#define bus_map_single(pdev, bufptr, bufsiz, dir)       \
370
 
        pci_map_single(pdev, bufptr, bufsiz, dir)
371
 
 
372
 
#define bus_map_sg(pdev, sgptr, sgcnt, dir)             \
373
 
        pci_map_sg(pdev, sgptr, sgcnt, dir)
374
 
 
375
 
#define bus_dma_sync_sg(pdev, sgptr, sgcnt, dir)        \
376
 
        pci_dma_sync_sg(pdev, sgptr, sgcnt, dir)
377
 
 
378
 
#define bus_dma_sync_single(pdev, mapping, bufsiz, dir) \
379
 
        pci_dma_sync_single(pdev, mapping, bufsiz, dir)
380
 
 
381
 
#define bus_sg_dma_address(sc)  sg_dma_address(sc)
382
 
#define bus_sg_dma_len(sc)      sg_dma_len(sc)
383
 
 
384
 
static void __unmap_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
385
 
{
386
 
        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
387
 
 
388
 
        switch(SYM_UCMD_PTR(cmd)->data_mapped) {
389
 
        case 2:
390
 
                bus_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
391
 
                break;
392
 
        case 1:
393
 
                bus_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
394
 
                                 cmd->request_bufflen, dma_dir);
395
 
                break;
396
 
        }
397
 
        SYM_UCMD_PTR(cmd)->data_mapped = 0;
398
 
}
399
 
 
400
 
static bus_addr_t __map_scsi_single_data(pcidev_t pdev, Scsi_Cmnd *cmd)
401
 
{
402
 
        bus_addr_t mapping;
403
 
        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
404
 
 
405
 
        mapping = bus_map_single(pdev, cmd->request_buffer,
406
 
                                 cmd->request_bufflen, dma_dir);
407
 
        if (mapping) {
408
 
                SYM_UCMD_PTR(cmd)->data_mapped  = 1;
409
 
                SYM_UCMD_PTR(cmd)->data_mapping = mapping;
410
 
        }
411
 
 
412
 
        return mapping;
413
 
}
414
 
 
415
 
static int __map_scsi_sg_data(pcidev_t pdev, Scsi_Cmnd *cmd)
416
 
{
417
 
        int use_sg;
418
 
        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
419
 
 
420
 
        use_sg = bus_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
421
 
        if (use_sg > 0) {
422
 
                SYM_UCMD_PTR(cmd)->data_mapped  = 2;
423
 
                SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
424
 
        }
425
 
 
426
 
        return use_sg;
427
 
}
428
 
 
429
 
static void __sync_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
430
 
{
431
 
        int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
432
 
 
433
 
        switch(SYM_UCMD_PTR(cmd)->data_mapped) {
434
 
        case 2:
435
 
                bus_dma_sync_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
436
 
                break;
437
 
        case 1:
438
 
                bus_dma_sync_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
439
 
                                    cmd->request_bufflen, dma_dir);
440
 
                break;
441
 
        }
442
 
}
443
 
 
444
 
#endif  /* SYM_LINUX_DYNAMIC_DMA_MAPPING */
445
 
 
446
 
#define unmap_scsi_data(np, cmd)        \
447
 
                __unmap_scsi_data(np->s.device, cmd)
448
 
#define map_scsi_single_data(np, cmd)   \
449
 
                __map_scsi_single_data(np->s.device, cmd)
450
 
#define map_scsi_sg_data(np, cmd)       \
451
 
                __map_scsi_sg_data(np->s.device, cmd)
452
 
#define sync_scsi_data(np, cmd)         \
453
 
                __sync_scsi_data(np->s.device, cmd)
454
 
 
455
 
/*
456
 
 *  Complete a pending CAM CCB.
457
 
 */
458
 
void sym_xpt_done(hcb_p np, Scsi_Cmnd *ccb)
459
 
{
460
 
        sym_remque(&SYM_UCMD_PTR(ccb)->link_cmdq);
461
 
        unmap_scsi_data(np, ccb);
462
 
        ccb->scsi_done(ccb);
463
 
}
464
 
 
465
 
void sym_xpt_done2(hcb_p np, Scsi_Cmnd *ccb, int cam_status)
466
 
{
467
 
        sym_set_cam_status(ccb, cam_status);
468
 
        sym_xpt_done(np, ccb);
469
 
}
470
 
 
471
 
 
472
 
/*
473
 
 *  Print something that identifies the IO.
474
 
 */
475
 
void sym_print_addr (ccb_p cp)
476
 
{
477
 
        Scsi_Cmnd *cmd = cp->cam_ccb;
478
 
        if (cmd)
479
 
                printf("%s:%d:%d:", sym_name(SYM_SOFTC_PTR(cmd)),
480
 
                       cmd->target,cmd->lun);
481
 
}
482
 
 
483
 
/*
484
 
 *  Tell the SCSI layer about a BUS RESET.
485
 
 */
486
 
void sym_xpt_async_bus_reset(hcb_p np)
487
 
{
488
 
        printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
489
 
        np->s.settle_time = ktime_get(sym_driver_setup.settle_delay * HZ);
490
 
        np->s.settle_time_valid = 1;
491
 
        if (sym_verbose >= 2)
492
 
                printf_info("%s: command processing suspended for %d seconds\n",
493
 
                            sym_name(np), sym_driver_setup.settle_delay);
494
 
}
495
 
 
496
 
/*
497
 
 *  Tell the SCSI layer about a BUS DEVICE RESET message sent.
498
 
 */
499
 
void sym_xpt_async_sent_bdr(hcb_p np, int target)
500
 
{
501
 
        printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
502
 
}
503
 
 
504
 
/*
505
 
 *  Tell the SCSI layer about the new transfer parameters.
506
 
 */
507
 
void sym_xpt_async_nego_wide(hcb_p np, int target)
508
 
{
509
 
        if (sym_verbose < 3)
510
 
                return;
511
 
        sym_announce_transfer_rate(np, target);
512
 
}
513
 
 
514
 
/*
515
 
 *  Choose the more appropriate CAM status if 
516
 
 *  the IO encountered an extended error.
517
 
 */
518
 
static int sym_xerr_cam_status(int cam_status, int x_status)
519
 
{
520
 
        if (x_status) {
521
 
                if      (x_status & XE_PARITY_ERR)
522
 
                        cam_status = DID_PARITY;
523
 
                else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
524
 
                        cam_status = DID_ERROR;
525
 
                else if (x_status & XE_BAD_PHASE)
526
 
                        cam_status = DID_ERROR;
527
 
                else
528
 
                        cam_status = DID_ERROR;
529
 
        }
530
 
        return cam_status;
531
 
}
532
 
 
533
 
/*
534
 
 *  Build CAM result for a failed or auto-sensed IO.
535
 
 */
536
 
void sym_set_cam_result_error(hcb_p np, ccb_p cp, int resid)
537
 
{
538
 
        Scsi_Cmnd *csio = cp->cam_ccb;
539
 
        u_int cam_status, scsi_status, drv_status;
540
 
 
541
 
        drv_status  = 0;
542
 
        cam_status  = DID_OK;
543
 
        scsi_status = cp->ssss_status;
544
 
 
545
 
        if (cp->host_flags & HF_SENSE) {
546
 
                scsi_status = cp->sv_scsi_status;
547
 
                resid = cp->sv_resid;
548
 
                if (sym_verbose && cp->sv_xerr_status)
549
 
                        sym_print_xerr(cp, cp->sv_xerr_status);
550
 
                if (cp->host_status == HS_COMPLETE &&
551
 
                    cp->ssss_status == S_GOOD &&
552
 
                    cp->xerr_status == 0) {
553
 
                        cam_status = sym_xerr_cam_status(DID_OK,
554
 
                                                         cp->sv_xerr_status);
555
 
                        drv_status = DRIVER_SENSE;
556
 
                        /*
557
 
                         *  Bounce back the sense data to user.
558
 
                         */
559
 
                        bzero(&csio->sense_buffer, sizeof(csio->sense_buffer));
560
 
                        bcopy(cp->sns_bbuf, csio->sense_buffer,
561
 
                              MIN(sizeof(csio->sense_buffer),SYM_SNS_BBUF_LEN));
562
 
#if 0
563
 
                        /*
564
 
                         *  If the device reports a UNIT ATTENTION condition 
565
 
                         *  due to a RESET condition, we should consider all 
566
 
                         *  disconnect CCBs for this unit as aborted.
567
 
                         */
568
 
                        if (1) {
569
 
                                u_char *p;
570
 
                                p  = (u_char *) csio->sense_data;
571
 
                                if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
572
 
                                        sym_clear_tasks(np, DID_ABORT,
573
 
                                                        cp->target,cp->lun, -1);
574
 
                        }
575
 
#endif
576
 
                }
577
 
                else
578
 
                        cam_status = DID_ERROR;
579
 
        }
580
 
        else if (cp->host_status == HS_COMPLETE)        /* Bad SCSI status */
581
 
                cam_status = DID_OK;
582
 
        else if (cp->host_status == HS_SEL_TIMEOUT)     /* Selection timeout */
583
 
                cam_status = DID_NO_CONNECT;
584
 
        else if (cp->host_status == HS_UNEXPECTED)      /* Unexpected BUS FREE*/
585
 
                cam_status = DID_ERROR;
586
 
        else {                                          /* Extended error */
587
 
                if (sym_verbose) {
588
 
                        PRINT_ADDR(cp);
589
 
                        printf ("COMMAND FAILED (%x %x %x).\n",
590
 
                                cp->host_status, cp->ssss_status,
591
 
                                cp->xerr_status);
592
 
                }
593
 
                /*
594
 
                 *  Set the most appropriate value for CAM status.
595
 
                 */
596
 
                cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
597
 
        }
598
 
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,99)
599
 
        csio->resid = resid;
600
 
#endif
601
 
        csio->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
602
 
}
603
 
 
604
 
 
605
 
/*
606
 
 *  Called on successfull INQUIRY response.
607
 
 */
608
 
void sym_sniff_inquiry(hcb_p np, Scsi_Cmnd *cmd, int resid)
609
 
{
610
 
        int retv;
611
 
 
612
 
        if (!cmd || cmd->use_sg)
613
 
                return;
614
 
 
615
 
        sync_scsi_data(np, cmd);
616
 
        retv = __sym_sniff_inquiry(np, cmd->target, cmd->lun,
617
 
                                   (u_char *) cmd->request_buffer,
618
 
                                   cmd->request_bufflen - resid);
619
 
        if (retv < 0)
620
 
                return;
621
 
        else if (retv)
622
 
                sym_update_trans_settings(np, &np->target[cmd->target]);
623
 
}
624
 
 
625
 
/*
626
 
 *  Build the scatter/gather array for an I/O.
627
 
 */
628
 
 
629
 
static int sym_scatter_no_sglist(hcb_p np, ccb_p cp, Scsi_Cmnd *cmd)
630
 
{
631
 
        struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
632
 
        int segment;
633
 
 
634
 
        cp->data_len = cmd->request_bufflen;
635
 
 
636
 
        if (cmd->request_bufflen) {
637
 
                bus_addr_t baddr = map_scsi_single_data(np, cmd);
638
 
                if (baddr) {
639
 
                        sym_build_sge(np, data, baddr, cmd->request_bufflen);
640
 
                        segment = 1;
641
 
                }
642
 
                else
643
 
                        segment = -2;
644
 
        }
645
 
        else
646
 
                segment = 0;
647
 
 
648
 
        return segment;
649
 
}
650
 
 
651
 
static int sym_scatter(hcb_p np, ccb_p cp, Scsi_Cmnd *cmd)
652
 
{
653
 
        int segment;
654
 
        int use_sg = (int) cmd->use_sg;
655
 
 
656
 
        cp->data_len = 0;
657
 
 
658
 
        if (!use_sg)
659
 
                segment = sym_scatter_no_sglist(np, cp, cmd);
660
 
        else if (use_sg > SYM_CONF_MAX_SG)
661
 
                segment = -1;
662
 
        else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
663
 
                struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
664
 
                struct sym_tblmove *data;
665
 
 
666
 
                data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
667
 
 
668
 
                for (segment = 0; segment < use_sg; segment++) {
669
 
                        bus_addr_t baddr = bus_sg_dma_address(&scatter[segment]);
670
 
                        unsigned int len = bus_sg_dma_len(&scatter[segment]);
671
 
 
672
 
                        sym_build_sge(np, &data[segment], baddr, len);
673
 
                        cp->data_len += len;
674
 
                }
675
 
        }
676
 
        else
677
 
                segment = -2;
678
 
 
679
 
        return segment;
680
 
}
681
 
 
682
 
/*
683
 
 *  Queue a SCSI command.
684
 
 */
685
 
static int sym_queue_command(hcb_p np, Scsi_Cmnd *ccb)
686
 
{
687
 
/*      Scsi_Device        *device    = ccb->device; */
688
 
        tcb_p   tp;
689
 
        lcb_p   lp;
690
 
        ccb_p   cp;
691
 
        int     order;
692
 
 
693
 
        /*
694
 
         *  Minimal checkings, so that we will not 
695
 
         *  go outside our tables.
696
 
         */
697
 
        if (ccb->target == np->myaddr ||
698
 
            ccb->target >= SYM_CONF_MAX_TARGET ||
699
 
            ccb->lun    >= SYM_CONF_MAX_LUN) {
700
 
                sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
701
 
                return 0;
702
 
        }
703
 
 
704
 
        /*
705
 
         *  Retreive the target descriptor.
706
 
         */
707
 
        tp = &np->target[ccb->target];
708
 
 
709
 
        /*
710
 
         *  Complete the 1st INQUIRY command with error 
711
 
         *  condition if the device is flagged NOSCAN 
712
 
         *  at BOOT in the NVRAM. This may speed up 
713
 
         *  the boot and maintain coherency with BIOS 
714
 
         *  device numbering. Clearing the flag allows 
715
 
         *  user to rescan skipped devices later.
716
 
         *  We also return error for devices not flagged 
717
 
         *  for SCAN LUNS in the NVRAM since some mono-lun 
718
 
         *  devices behave badly when asked for some non 
719
 
         *  zero LUN. Btw, this is an absolute hack.:-)
720
 
         */
721
 
        if (ccb->cmnd[0] == 0x12 || ccb->cmnd[0] == 0x0) {
722
 
                if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
723
 
                    ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && 
724
 
                     ccb->lun != 0)) {
725
 
                        tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
726
 
                        sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
727
 
                        return 0;
728
 
                }
729
 
        }
730
 
 
731
 
        /*
732
 
         *  Select tagged/untagged.
733
 
         */
734
 
        lp = sym_lp(np, tp, ccb->lun);
735
 
        order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
736
 
 
737
 
        /*
738
 
         *  Queue the SCSI IO.
739
 
         */
740
 
        cp = sym_get_ccb(np, ccb->target, ccb->lun, order);
741
 
        if (!cp)
742
 
                return 1;       /* Means resource shortage */
743
 
        (void) sym_queue_scsiio(np, ccb, cp);
744
 
        return 0;
745
 
}
746
 
 
747
 
/*
748
 
 *  Setup buffers and pointers that address the CDB.
749
 
 */
750
 
static int __inline sym_setup_cdb(hcb_p np, Scsi_Cmnd *ccb, ccb_p cp)
751
 
{
752
 
        u32     cmd_ba;
753
 
        int     cmd_len;
754
 
 
755
 
        /*
756
 
         *  CDB is 16 bytes max.
757
 
         */
758
 
        if (ccb->cmd_len > sizeof(cp->cdb_buf)) {
759
 
                sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
760
 
                return -1;
761
 
        }
762
 
 
763
 
        bcopy(ccb->cmnd, cp->cdb_buf, ccb->cmd_len);
764
 
        cmd_ba  = CCB_BA (cp, cdb_buf[0]);
765
 
        cmd_len = ccb->cmd_len;
766
 
 
767
 
        cp->phys.cmd.addr       = cpu_to_scr(cmd_ba);
768
 
        cp->phys.cmd.size       = cpu_to_scr(cmd_len);
769
 
 
770
 
        return 0;
771
 
}
772
 
 
773
 
/*
774
 
 *  Setup pointers that address the data and start the I/O.
775
 
 */
776
 
int sym_setup_data_and_start(hcb_p np, Scsi_Cmnd *csio, ccb_p cp)
777
 
{
778
 
        int dir;
779
 
        tcb_p tp = &np->target[cp->target];
780
 
        lcb_p lp = sym_lp(np, tp, cp->lun);
781
 
 
782
 
        /*
783
 
         *  Build the CDB.
784
 
         */
785
 
        if (sym_setup_cdb(np, csio, cp))
786
 
                goto out_abort;
787
 
 
788
 
        /*
789
 
         *  No direction means no data.
790
 
         */
791
 
        dir = scsi_data_direction(csio);
792
 
        if (dir != SCSI_DATA_NONE) {
793
 
                cp->segments = sym_scatter (np, cp, csio);
794
 
                if (cp->segments < 0) {
795
 
                        if (cp->segments == -2)
796
 
                                sym_set_cam_status(csio, CAM_RESRC_UNAVAIL);
797
 
                        else
798
 
                                sym_set_cam_status(csio, CAM_REQ_TOO_BIG);
799
 
                        goto out_abort;
800
 
                }
801
 
        }
802
 
        else {
803
 
                cp->data_len = 0;
804
 
                cp->segments = 0;
805
 
        }
806
 
 
807
 
        /*
808
 
         *  Set data pointers.
809
 
         */
810
 
        sym_setup_data_pointers(np, cp, dir);
811
 
 
812
 
        /*
813
 
         *  When `#ifed 1', the code below makes the driver 
814
 
         *  panic on the first attempt to write to a SCSI device.
815
 
         *  It is the first test we want to do after a driver 
816
 
         *  change that does not seem obviously safe. :)
817
 
         */
818
 
#if 0
819
 
        switch (cp->cdb_buf[0]) {
820
 
        case 0x0A: case 0x2A: case 0xAA:
821
 
                panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
822
 
                MDELAY(10000);
823
 
                break;
824
 
        default:
825
 
                break;
826
 
        }
827
 
#endif
828
 
 
829
 
        /*
830
 
         *      activate this job.
831
 
         */
832
 
        if (lp)
833
 
                sym_start_next_ccbs(np, lp, 2);
834
 
        else
835
 
                sym_put_start_queue(np, cp);
836
 
        return 0;
837
 
 
838
 
out_abort:
839
 
        sym_free_ccb(np, cp);
840
 
        sym_xpt_done(np, csio);
841
 
        return 0;
842
 
}
843
 
 
844
 
 
845
 
/*
846
 
 *  timer daemon.
847
 
 *
848
 
 *  Misused to keep the driver running when
849
 
 *  interrupts are not configured correctly.
850
 
 */
851
 
static void sym_timer (hcb_p np)
852
 
{
853
 
        u_long  thistime = ktime_get(0);
854
 
 
855
 
#if LINUX_VERSION_CODE < LinuxVersionCode(2, 4, 0)
856
 
        /*
857
 
         *  If release process in progress, let's go
858
 
         *  Set the release stage from 1 to 2 to synchronize
859
 
         *  with the release process.
860
 
         */
861
 
 
862
 
        if (np->s.release_stage) {
863
 
                if (np->s.release_stage == 1)
864
 
                        np->s.release_stage = 2;
865
 
                return;
866
 
        }
867
 
#endif
868
 
 
869
 
        /*
870
 
         *  Restart the timer.
871
 
         */
872
 
#ifdef SYM_CONF_PCIQ_BROKEN_INTR
873
 
        np->s.timer.expires = ktime_get((HZ+99)/100);
874
 
#else
875
 
        np->s.timer.expires = ktime_get(SYM_CONF_TIMER_INTERVAL);
876
 
#endif
877
 
        add_timer(&np->s.timer);
878
 
 
879
 
        /*
880
 
         *  If we are resetting the ncr, wait for settle_time before 
881
 
         *  clearing it. Then command processing will be resumed.
882
 
         */
883
 
        if (np->s.settle_time_valid) {
884
 
                if (ktime_dif(np->s.settle_time, thistime) <= 0){
885
 
                        if (sym_verbose >= 2 )
886
 
                                printk("%s: command processing resumed\n",
887
 
                                       sym_name(np));
888
 
                        np->s.settle_time_valid = 0;
889
 
                }
890
 
                return;
891
 
        }
892
 
 
893
 
        /*
894
 
         *      Nothing to do for now, but that may come.
895
 
         */
896
 
        if (np->s.lasttime + 4*HZ < thistime) {
897
 
                np->s.lasttime = thistime;
898
 
        }
899
 
 
900
 
#ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
901
 
        /*
902
 
         *  Some way-broken PCI bridges may lead to 
903
 
         *  completions being lost when the clearing 
904
 
         *  of the INTFLY flag by the CPU occurs 
905
 
         *  concurrently with the chip raising this flag.
906
 
         *  If this ever happen, lost completions will 
907
 
         * be reaped here.
908
 
         */
909
 
        sym_wakeup_done(np);
910
 
#endif
911
 
 
912
 
#ifdef SYM_CONF_PCIQ_BROKEN_INTR
913
 
        if (INB(nc_istat) & (INTF|SIP|DIP)) {
914
 
 
915
 
                /*
916
 
                **      Process pending interrupts.
917
 
                */
918
 
                if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
919
 
                sym_interrupt(np);
920
 
                if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
921
 
        }
922
 
#endif /* SYM_CONF_PCIQ_BROKEN_INTR */
923
 
}
924
 
 
925
 
 
926
 
/*
927
 
 *  PCI BUS error handler.
928
 
 */
929
 
void sym_log_bus_error(hcb_p np)
930
 
{
931
 
        u_short pci_sts;
932
 
        pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
933
 
        if (pci_sts & 0xf900) {
934
 
                pci_write_config_word(np->s.device, PCI_STATUS,
935
 
                                         pci_sts);
936
 
                printf("%s: PCI STATUS = 0x%04x\n",
937
 
                        sym_name(np), pci_sts & 0xf900);
938
 
        }
939
 
}
940
 
 
941
 
 
942
 
/*
943
 
 *  Requeue awaiting commands.
944
 
 */
945
 
static void sym_requeue_awaiting_cmds(hcb_p np)
946
 
{
947
 
        Scsi_Cmnd *cmd;
948
 
        ucmd_p ucp = SYM_UCMD_PTR(cmd);
949
 
        SYM_QUEHEAD tmp_cmdq;
950
 
        int sts;
951
 
 
952
 
        sym_que_move(&np->s.wait_cmdq, &tmp_cmdq);
953
 
 
954
 
        while ((ucp = (ucmd_p) sym_remque_head(&tmp_cmdq)) != 0) {
955
 
                sym_insque_tail(&ucp->link_cmdq, &np->s.busy_cmdq);
956
 
                cmd = SYM_SCMD_PTR(ucp);
957
 
                sts = sym_queue_command(np, cmd);
958
 
                if (sts) {
959
 
                        sym_remque(&ucp->link_cmdq);
960
 
                        sym_insque_head(&ucp->link_cmdq, &np->s.wait_cmdq);
961
 
                }
962
 
        }
963
 
}
964
 
 
965
 
/*
966
 
 *  Linux entry point of the queuecommand() function
967
 
 */
968
 
int sym53c8xx_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
969
 
{
970
 
        hcb_p  np  = SYM_SOFTC_PTR(cmd);
971
 
        ucmd_p ucp = SYM_UCMD_PTR(cmd);
972
 
        u_long flags;
973
 
        int sts = 0;
974
 
 
975
 
        cmd->scsi_done     = done;
976
 
        cmd->host_scribble = NULL;
977
 
        memset(ucp, 0, sizeof(*ucp));
978
 
 
979
 
        SYM_LOCK_HCB(np, flags);
980
 
 
981
 
        /*
982
 
         *  Shorten our settle_time if needed for 
983
 
         *  this command not to time out.
984
 
         */
985
 
        if (np->s.settle_time_valid && cmd->timeout_per_command) {
986
 
                u_long tlimit = ktime_get(cmd->timeout_per_command);
987
 
                tlimit = ktime_sub(tlimit, SYM_CONF_TIMER_INTERVAL*2);
988
 
                if (ktime_dif(np->s.settle_time, tlimit) > 0) {
989
 
                        np->s.settle_time = tlimit;
990
 
                }
991
 
        }
992
 
 
993
 
        if (np->s.settle_time_valid || !sym_que_empty(&np->s.wait_cmdq)) {
994
 
                sym_insque_tail(&ucp->link_cmdq, &np->s.wait_cmdq);
995
 
                goto out;
996
 
        }
997
 
 
998
 
        sym_insque_tail(&ucp->link_cmdq, &np->s.busy_cmdq);
999
 
        sts = sym_queue_command(np, cmd);
1000
 
        if (sts) {
1001
 
                sym_remque(&ucp->link_cmdq);
1002
 
                sym_insque_tail(&ucp->link_cmdq, &np->s.wait_cmdq);
1003
 
        }
1004
 
out:
1005
 
        SYM_UNLOCK_HCB(np, flags);
1006
 
 
1007
 
        return 0;
1008
 
}
1009
 
 
1010
 
/*
1011
 
 *  Linux entry point of the interrupt handler.
1012
 
 */
1013
 
static void sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
1014
 
{
1015
 
        unsigned long flags;
1016
 
        unsigned long flags1;
1017
 
        hcb_p np = (hcb_p) dev_id;
1018
 
 
1019
 
        if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
1020
 
 
1021
 
        SYM_LOCK_SCSI(np, flags1);
1022
 
        SYM_LOCK_HCB(np, flags);
1023
 
 
1024
 
        sym_interrupt(np);
1025
 
 
1026
 
        if (!sym_que_empty(&np->s.wait_cmdq) && !np->s.settle_time_valid)
1027
 
                sym_requeue_awaiting_cmds(np);
1028
 
 
1029
 
        SYM_UNLOCK_HCB(np, flags);
1030
 
        SYM_UNLOCK_SCSI(np, flags1);
1031
 
 
1032
 
        if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
1033
 
}
1034
 
 
1035
 
/*
1036
 
 *  Linux entry point of the timer handler
1037
 
 */
1038
 
static void sym53c8xx_timer(unsigned long npref)
1039
 
{
1040
 
        hcb_p np = (hcb_p) npref;
1041
 
        unsigned long flags;
1042
 
        unsigned long flags1;
1043
 
 
1044
 
        SYM_LOCK_SCSI(np, flags1);
1045
 
        SYM_LOCK_HCB(np, flags);
1046
 
 
1047
 
        sym_timer(np);
1048
 
 
1049
 
        if (!sym_que_empty(&np->s.wait_cmdq) && !np->s.settle_time_valid)
1050
 
                sym_requeue_awaiting_cmds(np);
1051
 
 
1052
 
        SYM_UNLOCK_HCB(np, flags);
1053
 
        SYM_UNLOCK_SCSI(np, flags1);
1054
 
}
1055
 
 
1056
 
 
1057
 
/*
1058
 
 *  What the eh thread wants us to perform.
1059
 
 */
1060
 
#define SYM_EH_ABORT            0
1061
 
#define SYM_EH_DEVICE_RESET     1
1062
 
#define SYM_EH_BUS_RESET        2
1063
 
#define SYM_EH_HOST_RESET       3
1064
 
 
1065
 
/*
1066
 
 *  What we will do regarding the involved SCSI command.
1067
 
 */
1068
 
#define SYM_EH_DO_IGNORE        0
1069
 
#define SYM_EH_DO_COMPLETE      1
1070
 
#define SYM_EH_DO_WAIT          2
1071
 
 
1072
 
/*
1073
 
 *  Our general completion handler.
1074
 
 */
1075
 
static void __sym_eh_done(Scsi_Cmnd *cmd, int timed_out)
1076
 
{
1077
 
        struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
1078
 
        if (!ep)
1079
 
                return;
1080
 
 
1081
 
        /* Try to avoid a race here (not 100% safe) */
1082
 
        if (!timed_out) {
1083
 
                ep->timed_out = 0;
1084
 
                if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
1085
 
                        return;
1086
 
        }
1087
 
 
1088
 
        /* Revert everything */
1089
 
        SYM_UCMD_PTR(cmd)->eh_wait = 0;
1090
 
        cmd->scsi_done = ep->old_done;
1091
 
 
1092
 
        /* Wake up the eh thread if it wants to sleep */
1093
 
/* SAE: No sleeping... */
1094
 
#ifdef XENO_KILLED
1095
 
        if (ep->to_do == SYM_EH_DO_WAIT)
1096
 
                up(&ep->sem);
1097
 
#endif
1098
 
 
1099
 
}
1100
 
 
1101
 
/*
1102
 
 *  scsi_done() alias when error recovery is in progress. 
1103
 
 */
1104
 
static void sym_eh_done(Scsi_Cmnd *cmd) { __sym_eh_done(cmd, 0); }
1105
 
 
1106
 
/*
1107
 
 *  Some timeout handler to avoid waiting too long.
1108
 
 */
1109
 
static void sym_eh_timeout(u_long p) { __sym_eh_done((Scsi_Cmnd *)p, 1); }
1110
 
 
1111
 
/*
1112
 
 *  Generic method for our eh processing.
1113
 
 *  The 'op' argument tells what we have to do.
1114
 
 */
1115
 
static int sym_eh_handler(int op, char *opname, Scsi_Cmnd *cmd)
1116
 
{
1117
 
        hcb_p np = SYM_SOFTC_PTR(cmd);
1118
 
        unsigned long flags;
1119
 
        SYM_QUEHEAD *qp;
1120
 
        int to_do = SYM_EH_DO_IGNORE;
1121
 
        int sts = -1;
1122
 
        struct sym_eh_wait eh, *ep = &eh;
1123
 
        char devname[20];
1124
 
 
1125
 
        sprintf(devname, "%s:%d:%d", sym_name(np), cmd->target, cmd->lun);
1126
 
 
1127
 
        printf_warning("%s: %s operation started.\n", devname, opname);
1128
 
 
1129
 
        SYM_LOCK_HCB(np, flags);
1130
 
 
1131
 
#if 0
1132
 
        /* This one should be the result of some race, thus to ignore */
1133
 
        if (cmd->serial_number != cmd->serial_number_at_timeout)
1134
 
                goto prepare;
1135
 
#endif
1136
 
 
1137
 
        /* This one is not queued to the core driver -> to complete here */ 
1138
 
        FOR_EACH_QUEUED_ELEMENT(&np->s.wait_cmdq, qp) {
1139
 
                if (SYM_SCMD_PTR(qp) == cmd) {
1140
 
                        to_do = SYM_EH_DO_COMPLETE;
1141
 
                        goto prepare;
1142
 
                }
1143
 
        }
1144
 
 
1145
 
        /* This one is queued in some place -> to wait for completion */
1146
 
        FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
1147
 
                ccb_p cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
1148
 
                if (cp->cam_ccb == cmd) {
1149
 
                        to_do = SYM_EH_DO_WAIT;
1150
 
                        goto prepare;
1151
 
                }
1152
 
        }
1153
 
 
1154
 
prepare:
1155
 
        /* Prepare stuff to either ignore, complete or wait for completion */
1156
 
        switch(to_do) {
1157
 
        default:
1158
 
        case SYM_EH_DO_IGNORE:
1159
 
                goto finish;
1160
 
                break;
1161
 
        case SYM_EH_DO_WAIT:
1162
 
/* SAE: */
1163
 
#ifdef XENO_KILLED
1164
 
#if LINUX_VERSION_CODE > LinuxVersionCode(2,3,0)
1165
 
                init_MUTEX_LOCKED(&ep->sem);
1166
 
#else
1167
 
                ep->sem = MUTEX_LOCKED;
1168
 
#endif
1169
 
#endif /* XENO_KILLED */
1170
 
                /* SAE: ??? */  
1171
 
                mdelay(10); 
1172
 
                /* fall through */
1173
 
        case SYM_EH_DO_COMPLETE:
1174
 
                ep->old_done = cmd->scsi_done;
1175
 
                cmd->scsi_done = sym_eh_done;
1176
 
                SYM_UCMD_PTR(cmd)->eh_wait = ep;
1177
 
        }
1178
 
 
1179
 
        /* Try to proceed the operation we have been asked for */
1180
 
        sts = -1;
1181
 
        switch(op) {
1182
 
        case SYM_EH_ABORT:
1183
 
                sts = sym_abort_scsiio(np, cmd, 1);
1184
 
                break;
1185
 
        case SYM_EH_DEVICE_RESET:
1186
 
                sts = sym_reset_scsi_target(np, cmd->target);
1187
 
                break;
1188
 
        case SYM_EH_BUS_RESET:
1189
 
                sym_reset_scsi_bus(np, 1);
1190
 
                sts = 0;
1191
 
                break;
1192
 
        case SYM_EH_HOST_RESET:
1193
 
                sym_reset_scsi_bus(np, 0);
1194
 
                sym_start_up (np, 1);
1195
 
                sts = 0;
1196
 
                break;
1197
 
        default:
1198
 
                break;
1199
 
        }
1200
 
 
1201
 
        /* On error, restore everything and cross fingers :) */
1202
 
        if (sts) {
1203
 
                SYM_UCMD_PTR(cmd)->eh_wait = 0;
1204
 
                cmd->scsi_done = ep->old_done;
1205
 
                to_do = SYM_EH_DO_IGNORE;
1206
 
        }
1207
 
 
1208
 
finish:
1209
 
        ep->to_do = to_do;
1210
 
        /* Complete the command with locks held as required by the driver */
1211
 
        if (to_do == SYM_EH_DO_COMPLETE)
1212
 
                sym_xpt_done2(np, cmd, CAM_REQ_ABORTED);
1213
 
 
1214
 
        SYM_UNLOCK_HCB(np, flags);
1215
 
 
1216
 
        /* Wait for completion with locks released, as required by kernel */
1217
 
        if (to_do == SYM_EH_DO_WAIT) {
1218
 
/* SAE: */
1219
 
#ifdef XENO_KILLED
1220
 
                init_timer(&ep->timer);
1221
 
                ep->timer.expires = jiffies + (5*HZ);
1222
 
                ep->timer.function = sym_eh_timeout;
1223
 
                ep->timer.data = (u_long)cmd;
1224
 
                ep->timed_out = 1;      /* Be pessimistic for once :) */
1225
 
                add_timer(&ep->timer);
1226
 
                SYM_UNLOCK_SCSI_NORESTORE(np);
1227
 
                down(&ep->sem);
1228
 
                SYM_LOCK_SCSI_NOSAVE(np);
1229
 
                if (ep->timed_out)
1230
 
                        sts = -2;
1231
 
#endif
1232
 
                /* SAE: ?? */
1233
 
                mdelay(10);
1234
 
        }
1235
 
        printf_warning("%s: %s operation %s.\n", devname, opname,
1236
 
                        sts==0?"complete":sts==-2?"timed-out":"failed");
1237
 
        return sts? SCSI_FAILED : SCSI_SUCCESS;
1238
 
}
1239
 
 
1240
 
 
1241
 
/*
1242
 
 * Error handlers called from the eh thread (one thread per HBA).
1243
 
 */
1244
 
int sym53c8xx_eh_abort_handler(Scsi_Cmnd *cmd)
1245
 
{
1246
 
        return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
1247
 
}
1248
 
 
1249
 
int sym53c8xx_eh_device_reset_handler(Scsi_Cmnd *cmd)
1250
 
{
1251
 
        return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
1252
 
}
1253
 
 
1254
 
int sym53c8xx_eh_bus_reset_handler(Scsi_Cmnd *cmd)
1255
 
{
1256
 
        return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
1257
 
}
1258
 
 
1259
 
int sym53c8xx_eh_host_reset_handler(Scsi_Cmnd *cmd)
1260
 
{
1261
 
        return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
1262
 
}
1263
 
 
1264
 
/*
1265
 
 *  Tune device queuing depth, according to various limits.
1266
 
 */
1267
 
static void 
1268
 
sym_tune_dev_queuing(hcb_p np, int target, int lun, u_short reqtags)
1269
 
{
1270
 
        tcb_p   tp = &np->target[target];
1271
 
        lcb_p   lp = sym_lp(np, tp, lun);
1272
 
        u_short oldtags;
1273
 
 
1274
 
        if (!lp)
1275
 
                return;
1276
 
 
1277
 
        oldtags = lp->s.reqtags;
1278
 
 
1279
 
        if (reqtags > lp->s.scdev_depth)
1280
 
                reqtags = lp->s.scdev_depth;
1281
 
 
1282
 
        lp->started_limit = reqtags ? reqtags : 2;
1283
 
        lp->started_max   = 1;
1284
 
        lp->s.reqtags     = reqtags;
1285
 
 
1286
 
        if (reqtags != oldtags) {
1287
 
                printf_info("%s:%d:%d: "
1288
 
                         "tagged command queuing %s, command queue depth %d.\n",
1289
 
                          sym_name(np), target, lun,
1290
 
                          lp->s.reqtags ? "enabled" : "disabled",
1291
 
                          lp->started_limit);
1292
 
        }
1293
 
}
1294
 
 
1295
 
#ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
1296
 
/*
1297
 
 *  Linux select queue depths function
1298
 
 */
1299
 
#define DEF_DEPTH       (sym_driver_setup.max_tag)
1300
 
#define ALL_TARGETS     -2
1301
 
#define NO_TARGET       -1
1302
 
#define ALL_LUNS        -2
1303
 
#define NO_LUN          -1
1304
 
 
1305
 
static int device_queue_depth(hcb_p np, int target, int lun)
1306
 
{
1307
 
        int c, h, t, u, v;
1308
 
        char *p = sym_driver_setup.tag_ctrl;
1309
 
        char *ep;
1310
 
 
1311
 
        h = -1;
1312
 
        t = NO_TARGET;
1313
 
        u = NO_LUN;
1314
 
        while ((c = *p++) != 0) {
1315
 
                v = simple_strtoul(p, &ep, 0);
1316
 
                switch(c) {
1317
 
                case '/':
1318
 
                        ++h;
1319
 
                        t = ALL_TARGETS;
1320
 
                        u = ALL_LUNS;
1321
 
                        break;
1322
 
                case 't':
1323
 
                        if (t != target)
1324
 
                                t = (target == v) ? v : NO_TARGET;
1325
 
                        u = ALL_LUNS;
1326
 
                        break;
1327
 
                case 'u':
1328
 
                        if (u != lun)
1329
 
                                u = (lun == v) ? v : NO_LUN;
1330
 
                        break;
1331
 
                case 'q':
1332
 
                        if (h == np->s.unit &&
1333
 
                                (t == ALL_TARGETS || t == target) &&
1334
 
                                (u == ALL_LUNS    || u == lun))
1335
 
                                return v;
1336
 
                        break;
1337
 
                case '-':
1338
 
                        t = ALL_TARGETS;
1339
 
                        u = ALL_LUNS;
1340
 
                        break;
1341
 
                default:
1342
 
                        break;
1343
 
                }
1344
 
                p = ep;
1345
 
        }
1346
 
        return DEF_DEPTH;
1347
 
}
1348
 
#else
1349
 
#define device_queue_depth(np, t, l)    (sym_driver_setup.max_tag)
1350
 
#endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
1351
 
 
1352
 
/*
1353
 
 * Linux entry point for device queue sizing.
1354
 
 */
1355
 
static void 
1356
 
sym53c8xx_select_queue_depths(struct Scsi_Host *host, 
1357
 
                              struct scsi_device *devlist)
1358
 
{
1359
 
        struct scsi_device *device;
1360
 
 
1361
 
        for (device = devlist; device; device = device->next) {
1362
 
                hcb_p np;
1363
 
                tcb_p tp;
1364
 
                lcb_p lp;
1365
 
                int reqtags;
1366
 
 
1367
 
                if (device->host != host)
1368
 
                        continue;
1369
 
 
1370
 
                np = ((struct host_data *) host->hostdata)->ncb;
1371
 
                tp = &np->target[device->id];
1372
 
 
1373
 
                /*
1374
 
                 *  Get user settings for transfer parameters.
1375
 
                 */
1376
 
                tp->inq_byte7_valid = (INQ7_SYNC|INQ7_WIDE16);
1377
 
                sym_update_trans_settings(np, tp);
1378
 
 
1379
 
                /*
1380
 
                 *  Allocate the LCB if not yet.
1381
 
                 *  If it fail, we may well be in the sh*t. :)
1382
 
                 */
1383
 
                lp = sym_alloc_lcb(np, device->id, device->lun);
1384
 
                if (!lp) {
1385
 
                        device->queue_depth = 1;
1386
 
                        continue;
1387
 
                }
1388
 
 
1389
 
                /*
1390
 
                 *  Get user flags.
1391
 
                 */
1392
 
                lp->curr_flags = lp->user_flags;
1393
 
 
1394
 
                /*
1395
 
                 *  Select queue depth from driver setup.
1396
 
                 *  Donnot use more than configured by user.
1397
 
                 *  Use at least 2.
1398
 
                 *  Donnot use more than our maximum.
1399
 
                 */
1400
 
                reqtags = device_queue_depth(np, device->id, device->lun);
1401
 
                if (reqtags > tp->usrtags)
1402
 
                        reqtags = tp->usrtags;
1403
 
                if (!device->tagged_supported)
1404
 
                        reqtags = 0;
1405
 
#if 1 /* Avoid to locally queue commands for no good reasons */
1406
 
                if (reqtags > SYM_CONF_MAX_TAG)
1407
 
                        reqtags = SYM_CONF_MAX_TAG;
1408
 
                device->queue_depth = reqtags ? reqtags : 2;
1409
 
#else
1410
 
                device->queue_depth = reqtags ? SYM_CONF_MAX_TAG : 2;
1411
 
#endif
1412
 
                lp->s.scdev_depth = device->queue_depth;
1413
 
                sym_tune_dev_queuing(np, device->id, device->lun, reqtags);
1414
 
        }
1415
 
}
1416
 
 
1417
 
/*
1418
 
 *  Linux entry point for info() function
1419
 
 */
1420
 
const char *sym53c8xx_info (struct Scsi_Host *host)
1421
 
{
1422
 
        return sym_driver_name();
1423
 
}
1424
 
 
1425
 
 
1426
 
#ifdef SYM_LINUX_PROC_INFO_SUPPORT
1427
 
/*
1428
 
 *  Proc file system stuff
1429
 
 *
1430
 
 *  A read operation returns adapter information.
1431
 
 *  A write operation is a control command.
1432
 
 *  The string is parsed in the driver code and the command is passed 
1433
 
 *  to the sym_usercmd() function.
1434
 
 */
1435
 
 
1436
 
#ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1437
 
 
1438
 
struct  sym_usrcmd {
1439
 
        u_long  target;
1440
 
        u_long  lun;
1441
 
        u_long  data;
1442
 
        u_long  cmd;
1443
 
};
1444
 
 
1445
 
#define UC_SETSYNC      10
1446
 
#define UC_SETTAGS      11
1447
 
#define UC_SETDEBUG     12
1448
 
#define UC_SETWIDE      14
1449
 
#define UC_SETFLAG      15
1450
 
#define UC_SETVERBOSE   17
1451
 
#define UC_RESETDEV     18
1452
 
#define UC_CLEARDEV     19
1453
 
 
1454
 
static void sym_exec_user_command (hcb_p np, struct sym_usrcmd *uc)
1455
 
{
1456
 
        tcb_p tp;
1457
 
        int t, l;
1458
 
 
1459
 
        switch (uc->cmd) {
1460
 
        case 0: return;
1461
 
 
1462
 
#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1463
 
        case UC_SETDEBUG:
1464
 
                sym_debug_flags = uc->data;
1465
 
                break;
1466
 
#endif
1467
 
        case UC_SETVERBOSE:
1468
 
                np->verbose = uc->data;
1469
 
                break;
1470
 
        default:
1471
 
                /*
1472
 
                 * We assume that other commands apply to targets.
1473
 
                 * This should always be the case and avoid the below 
1474
 
                 * 4 lines to be repeated 6 times.
1475
 
                 */
1476
 
                for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1477
 
                        if (!((uc->target >> t) & 1))
1478
 
                                continue;
1479
 
                        tp = &np->target[t];
1480
 
 
1481
 
                        switch (uc->cmd) {
1482
 
 
1483
 
                        case UC_SETSYNC:
1484
 
                                if (!uc->data || uc->data >= 255) {
1485
 
                                        tp->tinfo.goal.options = 0;
1486
 
                                        tp->tinfo.goal.offset  = 0;
1487
 
                                        break;
1488
 
                                }
1489
 
                                if (uc->data <= 9 && np->minsync_dt) {
1490
 
                                        if (uc->data < np->minsync_dt)
1491
 
                                                uc->data = np->minsync_dt;
1492
 
                                        tp->tinfo.goal.options = PPR_OPT_DT;
1493
 
                                        tp->tinfo.goal.width   = 1;
1494
 
                                        tp->tinfo.goal.period = uc->data;
1495
 
                                        tp->tinfo.goal.offset = np->maxoffs_dt;
1496
 
                                }
1497
 
                                else {
1498
 
                                        if (uc->data < np->minsync)
1499
 
                                                uc->data = np->minsync;
1500
 
                                        tp->tinfo.goal.options = 0;
1501
 
                                        tp->tinfo.goal.period = uc->data;
1502
 
                                        tp->tinfo.goal.offset = np->maxoffs;
1503
 
                                }
1504
 
                                break;
1505
 
                        case UC_SETWIDE:
1506
 
                                tp->tinfo.goal.width = uc->data ? 1 : 0;
1507
 
                                break;
1508
 
                        case UC_SETTAGS:
1509
 
                                for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1510
 
                                        sym_tune_dev_queuing(np, t,l, uc->data);
1511
 
                                break;
1512
 
                        case UC_RESETDEV:
1513
 
                                tp->to_reset = 1;
1514
 
                                np->istat_sem = SEM;
1515
 
                                OUTB (nc_istat, SIGP|SEM);
1516
 
                                break;
1517
 
                        case UC_CLEARDEV:
1518
 
                                for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1519
 
                                        lcb_p lp = sym_lp(np, tp, l);
1520
 
                                        if (lp) lp->to_clear = 1;
1521
 
                                }
1522
 
                                np->istat_sem = SEM;
1523
 
                                OUTB (nc_istat, SIGP|SEM);
1524
 
                                break;
1525
 
                        case UC_SETFLAG:
1526
 
                                tp->usrflags = uc->data;
1527
 
                                break;
1528
 
                        }
1529
 
                }
1530
 
                break;
1531
 
        }
1532
 
}
1533
 
 
1534
 
#define is_digit(c)     ((c) >= '0' && (c) <= '9')
1535
 
#define digit_to_bin(c) ((c) - '0')
1536
 
#define is_space(c)     ((c) == ' ' || (c) == '\t')
1537
 
 
1538
 
static int skip_spaces(char *ptr, int len)
1539
 
{
1540
 
        int cnt, c;
1541
 
 
1542
 
        for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
1543
 
 
1544
 
        return (len - cnt);
1545
 
}
1546
 
 
1547
 
static int get_int_arg(char *ptr, int len, u_long *pv)
1548
 
{
1549
 
        int     cnt, c;
1550
 
        u_long  v;
1551
 
 
1552
 
        for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
1553
 
                v = (v * 10) + digit_to_bin(c);
1554
 
        }
1555
 
 
1556
 
        if (pv)
1557
 
                *pv = v;
1558
 
 
1559
 
        return (len - cnt);
1560
 
}
1561
 
 
1562
 
static int is_keyword(char *ptr, int len, char *verb)
1563
 
{
1564
 
        int verb_len = strlen(verb);
1565
 
 
1566
 
        if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
1567
 
                return verb_len;
1568
 
        else
1569
 
                return 0;
1570
 
 
1571
 
}
1572
 
 
1573
 
#define SKIP_SPACES(min_spaces)                                         \
1574
 
        if ((arg_len = skip_spaces(ptr, len)) < (min_spaces))           \
1575
 
                return -EINVAL;                                         \
1576
 
        ptr += arg_len; len -= arg_len;
1577
 
 
1578
 
#define GET_INT_ARG(v)                                                  \
1579
 
        if (!(arg_len = get_int_arg(ptr, len, &(v))))                   \
1580
 
                return -EINVAL;                                         \
1581
 
        ptr += arg_len; len -= arg_len;
1582
 
 
1583
 
 
1584
 
/*
1585
 
 * Parse a control command
1586
 
 */
1587
 
 
1588
 
static int sym_user_command(hcb_p np, char *buffer, int length)
1589
 
{
1590
 
        char *ptr       = buffer;
1591
 
        int len         = length;
1592
 
        struct sym_usrcmd cmd, *uc = &cmd;
1593
 
        int             arg_len;
1594
 
        u_long          target;
1595
 
 
1596
 
        bzero(uc, sizeof(*uc));
1597
 
 
1598
 
        if (len > 0 && ptr[len-1] == '\n')
1599
 
                --len;
1600
 
 
1601
 
        if      ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1602
 
                uc->cmd = UC_SETSYNC;
1603
 
        else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1604
 
                uc->cmd = UC_SETTAGS;
1605
 
        else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1606
 
                uc->cmd = UC_SETVERBOSE;
1607
 
        else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1608
 
                uc->cmd = UC_SETWIDE;
1609
 
#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1610
 
        else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1611
 
                uc->cmd = UC_SETDEBUG;
1612
 
#endif
1613
 
        else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1614
 
                uc->cmd = UC_SETFLAG;
1615
 
        else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1616
 
                uc->cmd = UC_RESETDEV;
1617
 
        else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1618
 
                uc->cmd = UC_CLEARDEV;
1619
 
        else
1620
 
                arg_len = 0;
1621
 
 
1622
 
#ifdef DEBUG_PROC_INFO
1623
 
printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1624
 
#endif
1625
 
 
1626
 
        if (!arg_len)
1627
 
                return -EINVAL;
1628
 
        ptr += arg_len; len -= arg_len;
1629
 
 
1630
 
        switch(uc->cmd) {
1631
 
        case UC_SETSYNC:
1632
 
        case UC_SETTAGS:
1633
 
        case UC_SETWIDE:
1634
 
        case UC_SETFLAG:
1635
 
        case UC_RESETDEV:
1636
 
        case UC_CLEARDEV:
1637
 
                SKIP_SPACES(1);
1638
 
                if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1639
 
                        ptr += arg_len; len -= arg_len;
1640
 
                        uc->target = ~0;
1641
 
                } else {
1642
 
                        GET_INT_ARG(target);
1643
 
                        uc->target = (1<<target);
1644
 
#ifdef DEBUG_PROC_INFO
1645
 
printk("sym_user_command: target=%ld\n", target);
1646
 
#endif
1647
 
                }
1648
 
                break;
1649
 
        }
1650
 
 
1651
 
        switch(uc->cmd) {
1652
 
        case UC_SETVERBOSE:
1653
 
        case UC_SETSYNC:
1654
 
        case UC_SETTAGS:
1655
 
        case UC_SETWIDE:
1656
 
                SKIP_SPACES(1);
1657
 
                GET_INT_ARG(uc->data);
1658
 
#ifdef DEBUG_PROC_INFO
1659
 
printk("sym_user_command: data=%ld\n", uc->data);
1660
 
#endif
1661
 
                break;
1662
 
#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1663
 
        case UC_SETDEBUG:
1664
 
                while (len > 0) {
1665
 
                        SKIP_SPACES(1);
1666
 
                        if      ((arg_len = is_keyword(ptr, len, "alloc")))
1667
 
                                uc->data |= DEBUG_ALLOC;
1668
 
                        else if ((arg_len = is_keyword(ptr, len, "phase")))
1669
 
                                uc->data |= DEBUG_PHASE;
1670
 
                        else if ((arg_len = is_keyword(ptr, len, "queue")))
1671
 
                                uc->data |= DEBUG_QUEUE;
1672
 
                        else if ((arg_len = is_keyword(ptr, len, "result")))
1673
 
                                uc->data |= DEBUG_RESULT;
1674
 
                        else if ((arg_len = is_keyword(ptr, len, "scatter")))
1675
 
                                uc->data |= DEBUG_SCATTER;
1676
 
                        else if ((arg_len = is_keyword(ptr, len, "script")))
1677
 
                                uc->data |= DEBUG_SCRIPT;
1678
 
                        else if ((arg_len = is_keyword(ptr, len, "tiny")))
1679
 
                                uc->data |= DEBUG_TINY;
1680
 
                        else if ((arg_len = is_keyword(ptr, len, "timing")))
1681
 
                                uc->data |= DEBUG_TIMING;
1682
 
                        else if ((arg_len = is_keyword(ptr, len, "nego")))
1683
 
                                uc->data |= DEBUG_NEGO;
1684
 
                        else if ((arg_len = is_keyword(ptr, len, "tags")))
1685
 
                                uc->data |= DEBUG_TAGS;
1686
 
                        else if ((arg_len = is_keyword(ptr, len, "pointer")))
1687
 
                                uc->data |= DEBUG_POINTER;
1688
 
                        else
1689
 
                                return -EINVAL;
1690
 
                        ptr += arg_len; len -= arg_len;
1691
 
                }
1692
 
#ifdef DEBUG_PROC_INFO
1693
 
printk("sym_user_command: data=%ld\n", uc->data);
1694
 
#endif
1695
 
                break;
1696
 
#endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1697
 
        case UC_SETFLAG:
1698
 
                while (len > 0) {
1699
 
                        SKIP_SPACES(1);
1700
 
                        if      ((arg_len = is_keyword(ptr, len, "no_disc")))
1701
 
                                uc->data &= ~SYM_DISC_ENABLED;
1702
 
                        else
1703
 
                                return -EINVAL;
1704
 
                        ptr += arg_len; len -= arg_len;
1705
 
                }
1706
 
                break;
1707
 
        default:
1708
 
                break;
1709
 
        }
1710
 
 
1711
 
        if (len)
1712
 
                return -EINVAL;
1713
 
        else {
1714
 
                long flags;
1715
 
 
1716
 
                SYM_LOCK_HCB(np, flags);
1717
 
                sym_exec_user_command (np, uc);
1718
 
                SYM_UNLOCK_HCB(np, flags);
1719
 
        }
1720
 
        return length;
1721
 
}
1722
 
 
1723
 
#endif  /* SYM_LINUX_USER_COMMAND_SUPPORT */
1724
 
 
1725
 
 
1726
 
#ifdef SYM_LINUX_USER_INFO_SUPPORT
1727
 
/*
1728
 
 *  Informations through the proc file system.
1729
 
 */
1730
 
struct info_str {
1731
 
        char *buffer;
1732
 
        int length;
1733
 
        int offset;
1734
 
        int pos;
1735
 
};
1736
 
 
1737
 
static void copy_mem_info(struct info_str *info, char *data, int len)
1738
 
{
1739
 
        if (info->pos + len > info->length)
1740
 
                len = info->length - info->pos;
1741
 
 
1742
 
        if (info->pos + len < info->offset) {
1743
 
                info->pos += len;
1744
 
                return;
1745
 
        }
1746
 
        if (info->pos < info->offset) {
1747
 
                data += (info->offset - info->pos);
1748
 
                len  -= (info->offset - info->pos);
1749
 
        }
1750
 
 
1751
 
        if (len > 0) {
1752
 
                memcpy(info->buffer + info->pos, data, len);
1753
 
                info->pos += len;
1754
 
        }
1755
 
}
1756
 
 
1757
 
static int copy_info(struct info_str *info, char *fmt, ...)
1758
 
{
1759
 
        va_list args;
1760
 
        char buf[81];
1761
 
        int len;
1762
 
 
1763
 
        va_start(args, fmt);
1764
 
        len = vsprintf(buf, fmt, args);
1765
 
        va_end(args);
1766
 
 
1767
 
        copy_mem_info(info, buf, len);
1768
 
        return len;
1769
 
}
1770
 
 
1771
 
/*
1772
 
 *  Copy formatted information into the input buffer.
1773
 
 */
1774
 
/* SAE: Again with the off_t */
1775
 
#ifndef off_t
1776
 
#define off_t int
1777
 
#endif
1778
 
static int sym_host_info(hcb_p np, char *ptr, off_t offset, int len)
1779
 
{
1780
 
        struct info_str info;
1781
 
 
1782
 
        info.buffer     = ptr;
1783
 
        info.length     = len;
1784
 
        info.offset     = offset;
1785
 
        info.pos        = 0;
1786
 
 
1787
 
        copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1788
 
                         "revision id 0x%x\n",
1789
 
                         np->s.chip_name, np->device_id, np->revision_id);
1790
 
        copy_info(&info, "On PCI bus %d, device %d, function %d, "
1791
 
#ifdef __sparc__
1792
 
                "IRQ %s\n",
1793
 
#else
1794
 
                "IRQ %d\n",
1795
 
#endif
1796
 
                np->s.bus, (np->s.device_fn & 0xf8) >> 3, np->s.device_fn & 7,
1797
 
#ifdef __sparc__
1798
 
                __irq_itoa(np->s.irq));
1799
 
#else
1800
 
                (int) np->s.irq);
1801
 
#endif
1802
 
        copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1803
 
                         (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1804
 
                         np->maxwide ? "Wide" : "Narrow",
1805
 
                         np->minsync_dt ? ", DT capable" : "");
1806
 
 
1807
 
        copy_info(&info, "Max. started commands %d, "
1808
 
                         "max. commands per LUN %d\n",
1809
 
                         SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1810
 
 
1811
 
        return info.pos > info.offset? info.pos - info.offset : 0;
1812
 
}
1813
 
#endif /* SYM_LINUX_USER_INFO_SUPPORT */
1814
 
 
1815
 
/*
1816
 
 *  Entry point of the scsi proc fs of the driver.
1817
 
 *  - func = 0 means read  (returns adapter infos)
1818
 
 *  - func = 1 means write (not yet merget from sym53c8xx)
1819
 
 */
1820
 
static int sym53c8xx_proc_info(char *buffer, char **start, off_t offset,
1821
 
                        int length, int hostno, int func)
1822
 
{
1823
 
        struct Scsi_Host *host;
1824
 
        struct host_data *host_data;
1825
 
        hcb_p np = 0;
1826
 
        int retv;
1827
 
 
1828
 
        for (host = first_host; host; host = host->next) {
1829
 
                if (host->hostt != first_host->hostt)
1830
 
                        continue;
1831
 
                if (host->host_no == hostno) {
1832
 
                        host_data = (struct host_data *) host->hostdata;
1833
 
                        np = host_data->ncb;
1834
 
                        break;
1835
 
                }
1836
 
        }
1837
 
 
1838
 
        if (!np)
1839
 
                return -EINVAL;
1840
 
 
1841
 
        if (func) {
1842
 
#ifdef  SYM_LINUX_USER_COMMAND_SUPPORT
1843
 
                retv = sym_user_command(np, buffer, length);
1844
 
#else
1845
 
                retv = -EINVAL;
1846
 
#endif
1847
 
        }
1848
 
        else {
1849
 
                if (start)
1850
 
                        *start = buffer;
1851
 
#ifdef SYM_LINUX_USER_INFO_SUPPORT
1852
 
                retv = sym_host_info(np, buffer, offset, length);
1853
 
#else
1854
 
                retv = -EINVAL;
1855
 
#endif
1856
 
        }
1857
 
 
1858
 
        return retv;
1859
 
}
1860
 
#endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1861
 
 
1862
 
/*
1863
 
 *      Free controller resources.
1864
 
 */
1865
 
static void sym_free_resources(hcb_p np)
1866
 
{
1867
 
        /*
1868
 
         *  Free O/S specific resources.
1869
 
         */
1870
 
        if (np->s.irq)
1871
 
                free_irq(np->s.irq, np);
1872
 
        if (np->s.io_port)
1873
 
                release_region(np->s.io_port, np->s.io_ws);
1874
 
#ifndef SYM_OPT_NO_BUS_MEMORY_MAPPING
1875
 
        if (np->s.mmio_va)
1876
 
                pci_unmap_mem(np->s.mmio_va, np->s.io_ws);
1877
 
        if (np->s.ram_va)
1878
 
                pci_unmap_mem(np->s.ram_va, np->ram_ws);
1879
 
#endif
1880
 
        /*
1881
 
         *  Free O/S independant resources.
1882
 
         */
1883
 
        sym_hcb_free(np);
1884
 
 
1885
 
        sym_mfree_dma(np, sizeof(*np), "HCB");
1886
 
}
1887
 
 
1888
 
/*
1889
 
 *  Ask/tell the system about DMA addressing.
1890
 
 */
1891
 
#ifdef SYM_LINUX_DYNAMIC_DMA_MAPPING
1892
 
static int sym_setup_bus_dma_mask(hcb_p np)
1893
 
{
1894
 
#if LINUX_VERSION_CODE < LinuxVersionCode(2,4,3)
1895
 
        if (!pci_dma_supported(np->s.device, 0xffffffffUL))
1896
 
                goto out_err32;
1897
 
#else
1898
 
#if   SYM_CONF_DMA_ADDRESSING_MODE == 0
1899
 
        if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1900
 
                goto out_err32;
1901
 
#else
1902
 
#if   SYM_CONF_DMA_ADDRESSING_MODE == 1
1903
 
#define PciDmaMask      0xffffffffff
1904
 
#elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1905
 
#define PciDmaMask      0xffffffffffffffff
1906
 
#endif
1907
 
        if (np->features & FE_DAC) {
1908
 
                if (!pci_set_dma_mask(np->s.device, PciDmaMask)) {
1909
 
                        np->use_dac = 1;
1910
 
                        printf_info("%s: using 64 bit DMA addressing\n",
1911
 
                                        sym_name(np));
1912
 
                }
1913
 
                else {
1914
 
                        if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1915
 
                                goto out_err32;
1916
 
                }
1917
 
        }
1918
 
#undef  PciDmaMask
1919
 
#endif
1920
 
#endif
1921
 
        return 0;
1922
 
 
1923
 
out_err32:
1924
 
        printf_warning("%s: 32 BIT DMA ADDRESSING NOT SUPPORTED\n",
1925
 
                        sym_name(np));
1926
 
        return -1;
1927
 
}
1928
 
#endif /* SYM_LINUX_DYNAMIC_DMA_MAPPING */
1929
 
 
1930
 
/*
1931
 
 *  Host attach and initialisations.
1932
 
 *
1933
 
 *  Allocate host data and ncb structure.
1934
 
 *  Request IO region and remap MMIO region.
1935
 
 *  Do chip initialization.
1936
 
 *  If all is OK, install interrupt handling and
1937
 
 *  start the timer daemon.
1938
 
 */
1939
 
static int __init 
1940
 
sym_attach (Scsi_Host_Template *tpnt, int unit, sym_device *dev)
1941
 
{
1942
 
        struct host_data *host_data;
1943
 
        hcb_p np = 0;
1944
 
        struct Scsi_Host *instance = 0;
1945
 
        u_long flags = 0;
1946
 
        sym_nvram *nvram = dev->nvram;
1947
 
        struct sym_fw *fw;
1948
 
 
1949
 
        printk(KERN_INFO
1950
 
                "sym%d: <%s> rev 0x%x on pci bus %d device %d function %d "
1951
 
                "irq %d\n",
1952
 
                unit, dev->chip.name, dev->chip.revision_id,
1953
 
                dev->s.bus, (dev->s.device_fn & 0xf8) >> 3,
1954
 
                dev->s.device_fn & 7,
1955
 
                dev->s.irq);
1956
 
 
1957
 
/* SAE: No sparc in Xen... */
1958
 
#ifdef XENO_KILLED
1959
 
#ifdef __sparc__
1960
 
                "irq %s\n",
1961
 
#else
1962
 
                "irq %d\n",
1963
 
#endif
1964
 
                unit, dev->chip.name, dev->chip.revision_id,
1965
 
                dev->s.bus, (dev->s.device_fn & 0xf8) >> 3,
1966
 
                dev->s.device_fn & 7,
1967
 
#ifdef __sparc__
1968
 
                __irq_itoa(dev->s.irq));
1969
 
#else
1970
 
                dev->s.irq);
1971
 
#endif
1972
 
#endif /* XENO_KILLED */
1973
 
 
1974
 
        /*
1975
 
         *  Get the firmware for this chip.
1976
 
         */
1977
 
        fw = sym_find_firmware(&dev->chip);
1978
 
        if (!fw)
1979
 
                goto attach_failed;
1980
 
 
1981
 
        /*
1982
 
         *      Allocate host_data structure
1983
 
         */
1984
 
        if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
1985
 
                goto attach_failed;
1986
 
        host_data = (struct host_data *) instance->hostdata;
1987
 
 
1988
 
        /*
1989
 
         *  Allocate immediately the host control block, 
1990
 
         *  since we are only expecting to succeed. :)
1991
 
         *  We keep track in the HCB of all the resources that 
1992
 
         *  are to be released on error.
1993
 
         */
1994
 
#ifdef  SYM_LINUX_DYNAMIC_DMA_MAPPING
1995
 
        np = __sym_calloc_dma(dev->pdev, sizeof(*np), "HCB");
1996
 
        if (np) {
1997
 
                np->s.device = dev->pdev;
1998
 
                np->bus_dmat = dev->pdev; /* Result in 1 DMA pool per HBA */
1999
 
        }
2000
 
        else
2001
 
                goto attach_failed;
2002
 
#else
2003
 
        np = sym_calloc_dma(sizeof(*np), "HCB");
2004
 
        if (!np)
2005
 
                goto attach_failed;
2006
 
#endif
2007
 
        host_data->ncb = np;
2008
 
 
2009
 
        SYM_INIT_LOCK_HCB(np);
2010
 
 
2011
 
        /*
2012
 
         *  Copy some useful infos to the HCB.
2013
 
         */
2014
 
        np->hcb_ba      = vtobus(np);
2015
 
        np->verbose     = sym_driver_setup.verbose;
2016
 
        np->s.device    = dev->pdev;
2017
 
        np->s.unit      = unit;
2018
 
        np->device_id   = dev->chip.device_id;
2019
 
        np->revision_id = dev->chip.revision_id;
2020
 
        np->s.bus       = dev->s.bus;
2021
 
        np->s.device_fn = dev->s.device_fn;
2022
 
        np->features    = dev->chip.features;
2023
 
        np->clock_divn  = dev->chip.nr_divisor;
2024
 
        np->maxoffs     = dev->chip.offset_max;
2025
 
        np->maxburst    = dev->chip.burst_max;
2026
 
        np->myaddr      = dev->host_id;
2027
 
 
2028
 
        /*
2029
 
         *  Edit its name.
2030
 
         */
2031
 
        strncpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name)-1);
2032
 
        sprintf(np->s.inst_name, "sym%d", np->s.unit);
2033
 
 
2034
 
        /*
2035
 
         *  Ask/tell the system about DMA addressing.
2036
 
         */
2037
 
#ifdef SYM_LINUX_DYNAMIC_DMA_MAPPING
2038
 
        if (sym_setup_bus_dma_mask(np))
2039
 
                goto attach_failed;
2040
 
#endif
2041
 
 
2042
 
        /*
2043
 
         *  Try to map the controller chip to
2044
 
         *  virtual and physical memory.
2045
 
         */
2046
 
        np->mmio_ba     = (u32)dev->s.base;
2047
 
        np->s.io_ws     = (np->features & FE_IO256)? 256 : 128;
2048
 
 
2049
 
#ifndef SYM_CONF_IOMAPPED
2050
 
        np->s.mmio_va = pci_map_mem(dev->s.base_c, np->s.io_ws);
2051
 
        if (!np->s.mmio_va) {
2052
 
                printf_err("%s: can't map PCI MMIO region\n", sym_name(np));
2053
 
                goto attach_failed;
2054
 
        }
2055
 
        else if (sym_verbose > 1)
2056
 
                printf_info("%s: using memory mapped IO\n", sym_name(np));
2057
 
#endif /* !defined SYM_CONF_IOMAPPED */
2058
 
 
2059
 
        /*
2060
 
         *  Try to map the controller chip into iospace.
2061
 
         */
2062
 
        if (dev->s.io_port) {
2063
 
                request_region(dev->s.io_port, np->s.io_ws, NAME53C8XX);
2064
 
                np->s.io_port = dev->s.io_port;
2065
 
        }
2066
 
 
2067
 
        /*
2068
 
         *  Map on-chip RAM if present and supported.
2069
 
         */
2070
 
        if (!(np->features & FE_RAM))
2071
 
                dev->s.base_2 = 0;
2072
 
        if (dev->s.base_2) {
2073
 
                np->ram_ba = (u32)dev->s.base_2;
2074
 
                if (np->features & FE_RAM8K)
2075
 
                        np->ram_ws = 8192;
2076
 
                else
2077
 
                        np->ram_ws = 4096;
2078
 
#ifndef SYM_OPT_NO_BUS_MEMORY_MAPPING
2079
 
                np->s.ram_va = pci_map_mem(dev->s.base_2_c, np->ram_ws);
2080
 
                if (!np->s.ram_va) {
2081
 
                        printf_err("%s: can't map PCI MEMORY region\n",
2082
 
                               sym_name(np));
2083
 
                        goto attach_failed;
2084
 
                }
2085
 
#endif
2086
 
        }
2087
 
 
2088
 
        /*
2089
 
         *  Perform O/S independant stuff.
2090
 
         */
2091
 
        if (sym_hcb_attach(np, fw, nvram))
2092
 
                goto attach_failed;
2093
 
 
2094
 
 
2095
 
        /*
2096
 
         *  Install the interrupt handler.
2097
 
         *  If we synchonize the C code with SCRIPTS on interrupt, 
2098
 
         *  we donnot want to share the INTR line at all.
2099
 
         */
2100
 
        if (request_irq(dev->s.irq, sym53c8xx_intr, SA_SHIRQ,
2101
 
                        NAME53C8XX, np)) {
2102
 
                printf_err("%s: request irq %d failure\n",
2103
 
                        sym_name(np), dev->s.irq);
2104
 
                goto attach_failed;
2105
 
        }
2106
 
        np->s.irq = dev->s.irq;
2107
 
 
2108
 
        /*
2109
 
         *  After SCSI devices have been opened, we cannot
2110
 
         *  reset the bus safely, so we do it here.
2111
 
         */
2112
 
        SYM_LOCK_HCB(np, flags);
2113
 
        if (sym_reset_scsi_bus(np, 0)) {
2114
 
                printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
2115
 
                           "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
2116
 
                SYM_UNLOCK_HCB(np, flags);
2117
 
                goto attach_failed;
2118
 
        }
2119
 
 
2120
 
        /*
2121
 
         *  Initialize some queue headers.
2122
 
         */
2123
 
        sym_que_init(&np->s.wait_cmdq);
2124
 
        sym_que_init(&np->s.busy_cmdq);
2125
 
 
2126
 
        /*
2127
 
         *  Start the SCRIPTS.
2128
 
         */
2129
 
        sym_start_up (np, 1);
2130
 
 
2131
 
        /*
2132
 
         *  Start the timer daemon
2133
 
         */
2134
 
        init_timer(&np->s.timer);
2135
 
        np->s.timer.data     = (unsigned long) np;
2136
 
        np->s.timer.function = sym53c8xx_timer;
2137
 
        np->s.lasttime=0;
2138
 
        sym_timer (np);
2139
 
 
2140
 
        /*
2141
 
         *  Done.
2142
 
         */
2143
 
        if (!first_host)
2144
 
                first_host = instance;
2145
 
 
2146
 
        /*
2147
 
         *  Fill Linux host instance structure
2148
 
         *  and return success.
2149
 
         */
2150
 
        instance->max_channel   = 0;
2151
 
        instance->this_id       = np->myaddr;
2152
 
        instance->max_id        = np->maxwide ? 16 : 8;
2153
 
        instance->max_lun       = SYM_CONF_MAX_LUN;
2154
 
#ifndef SYM_CONF_IOMAPPED
2155
 
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,29)
2156
 
        instance->base          = (unsigned long) np->s.mmio_va;
2157
 
#else
2158
 
        instance->base          = (char *) np->s.mmio_va;
2159
 
#endif
2160
 
#endif
2161
 
        instance->irq           = np->s.irq;
2162
 
        instance->unique_id     = np->s.io_port;
2163
 
        instance->io_port       = np->s.io_port;
2164
 
        instance->n_io_port     = np->s.io_ws;
2165
 
        instance->dma_channel   = 0;
2166
 
        instance->cmd_per_lun   = SYM_CONF_MAX_TAG;
2167
 
        instance->can_queue     = (SYM_CONF_MAX_START-2);
2168
 
        instance->sg_tablesize  = SYM_CONF_MAX_SG;
2169
 
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
2170
 
        instance->max_cmd_len   = 16;
2171
 
#endif
2172
 
        instance->select_queue_depths = sym53c8xx_select_queue_depths;
2173
 
/* SAE */
2174
 
#ifdef XENO_KILLED
2175
 
        instance->highmem_io    = 1;
2176
 
#endif
2177
 
 
2178
 
        SYM_UNLOCK_HCB(np, flags);
2179
 
 
2180
 
        scsi_set_pci_device(instance, dev->pdev);
2181
 
 
2182
 
        /*
2183
 
         *  Now let the generic SCSI driver
2184
 
         *  look for the SCSI devices on the bus ..
2185
 
         */
2186
 
        return 0;
2187
 
 
2188
 
attach_failed:
2189
 
        if (!instance) return -1;
2190
 
        printf_info("%s: giving up ...\n", sym_name(np));
2191
 
        if (np)
2192
 
                sym_free_resources(np);
2193
 
        scsi_unregister(instance);
2194
 
 
2195
 
        return -1;
2196
 
 }
2197
 
 
2198
 
 
2199
 
/*
2200
 
 *    Detect and try to read SYMBIOS and TEKRAM NVRAM.
2201
 
 */
2202
 
#if SYM_CONF_NVRAM_SUPPORT
2203
 
static void __init sym_get_nvram(sym_device *devp, sym_nvram *nvp)
2204
 
{
2205
 
        if (!nvp)
2206
 
                return;
2207
 
 
2208
 
        devp->nvram = nvp;
2209
 
        devp->device_id = devp->chip.device_id;
2210
 
        nvp->type = 0;
2211
 
 
2212
 
        /*
2213
 
         *  Get access to chip IO registers
2214
 
         */
2215
 
#ifdef SYM_CONF_IOMAPPED
2216
 
        request_region(devp->s.io_port, 128, NAME53C8XX);
2217
 
#else
2218
 
        devp->s.mmio_va = pci_map_mem(devp->s.base_c, 128);
2219
 
        if (!devp->s.mmio_va)
2220
 
                return;
2221
 
#endif
2222
 
 
2223
 
        /*
2224
 
         *  Try to read SYMBIOS|TEKRAM nvram.
2225
 
         */
2226
 
        (void) sym_read_nvram(devp, nvp);
2227
 
 
2228
 
        /*
2229
 
         *  Release access to chip IO registers
2230
 
         */
2231
 
#ifdef SYM_CONF_IOMAPPED
2232
 
        release_region(devp->s.io_port, 128);
2233
 
#else
2234
 
        pci_unmap_mem((u_long) devp->s.mmio_va, 128ul);
2235
 
#endif
2236
 
}
2237
 
#endif  /* SYM_CONF_NVRAM_SUPPORT */
2238
 
 
2239
 
/*
2240
 
 *  Driver setup from the boot command line
2241
 
 */
2242
 
#ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
2243
 
 
2244
 
static struct sym_driver_setup
2245
 
        sym_driver_safe_setup __initdata = SYM_LINUX_DRIVER_SAFE_SETUP;
2246
 
#ifdef  MODULE
2247
 
char *sym53c8xx = 0;    /* command line passed by insmod */
2248
 
MODULE_PARM(sym53c8xx, "s");
2249
 
#endif
2250
 
 
2251
 
static void __init sym53c8xx_print_driver_setup(void)
2252
 
{
2253
 
        printf_info (NAME53C8XX ": setup="
2254
 
                "mpar:%d,spar:%d,tags:%d,sync:%d,burst:%d,"
2255
 
                "led:%d,wide:%d,diff:%d,irqm:%d, buschk:%d\n",
2256
 
                sym_driver_setup.pci_parity,
2257
 
                sym_driver_setup.scsi_parity,
2258
 
                sym_driver_setup.max_tag,
2259
 
                sym_driver_setup.min_sync,
2260
 
                sym_driver_setup.burst_order,
2261
 
                sym_driver_setup.scsi_led,
2262
 
                sym_driver_setup.max_wide,
2263
 
                sym_driver_setup.scsi_diff,
2264
 
                sym_driver_setup.irq_mode,
2265
 
                sym_driver_setup.scsi_bus_check);
2266
 
        printf_info (NAME53C8XX ": setup="
2267
 
                "hostid:%d,offs:%d,luns:%d,pcifix:%d,revprob:%d,"
2268
 
                "verb:%d,debug:0x%x,setlle_delay:%d\n",
2269
 
                sym_driver_setup.host_id,
2270
 
                sym_driver_setup.max_offs,
2271
 
                sym_driver_setup.max_lun,
2272
 
                sym_driver_setup.pci_fix_up,
2273
 
                sym_driver_setup.reverse_probe,
2274
 
                sym_driver_setup.verbose,
2275
 
                sym_driver_setup.debug,
2276
 
                sym_driver_setup.settle_delay);
2277
 
#ifdef DEBUG_2_0_X
2278
 
MDELAY(5000);
2279
 
#endif
2280
 
};
2281
 
 
2282
 
#define OPT_PCI_PARITY          1
2283
 
#define OPT_SCSI_PARITY         2
2284
 
#define OPT_MAX_TAG             3
2285
 
#define OPT_MIN_SYNC            4
2286
 
#define OPT_BURST_ORDER         5
2287
 
#define OPT_SCSI_LED            6
2288
 
#define OPT_MAX_WIDE            7
2289
 
#define OPT_SCSI_DIFF           8
2290
 
#define OPT_IRQ_MODE            9
2291
 
#define OPT_SCSI_BUS_CHECK      10
2292
 
#define OPT_HOST_ID             11
2293
 
#define OPT_MAX_OFFS            12
2294
 
#define OPT_MAX_LUN             13
2295
 
#define OPT_PCI_FIX_UP          14
2296
 
 
2297
 
#define OPT_REVERSE_PROBE       15
2298
 
#define OPT_VERBOSE             16
2299
 
#define OPT_DEBUG               17
2300
 
#define OPT_SETTLE_DELAY        18
2301
 
#define OPT_USE_NVRAM           19
2302
 
#define OPT_EXCLUDE             20
2303
 
#define OPT_SAFE_SETUP          21
2304
 
 
2305
 
static char setup_token[] __initdata =
2306
 
        "mpar:"         "spar:"
2307
 
        "tags:"         "sync:"
2308
 
        "burst:"        "led:"
2309
 
        "wide:"         "diff:"
2310
 
        "irqm:"         "buschk:"
2311
 
        "hostid:"       "offset:"
2312
 
        "luns:"         "pcifix:"
2313
 
        "revprob:"      "verb:"
2314
 
        "debug:"        "settle:"
2315
 
        "nvram:"        "excl:"
2316
 
        "safe:"
2317
 
        ;
2318
 
 
2319
 
#ifdef MODULE
2320
 
#define ARG_SEP ' '
2321
 
#else
2322
 
#define ARG_SEP ','
2323
 
#endif
2324
 
 
2325
 
static int __init get_setup_token(char *p)
2326
 
{
2327
 
        char *cur = setup_token;
2328
 
        char *pc;
2329
 
        int i = 0;
2330
 
 
2331
 
        while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
2332
 
                ++pc;
2333
 
                ++i;
2334
 
                if (!strncmp(p, cur, pc - cur))
2335
 
                        return i;
2336
 
                cur = pc;
2337
 
        }
2338
 
        return 0;
2339
 
}
2340
 
#endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2341
 
 
2342
 
int __init sym53c8xx_setup(char *str)
2343
 
{
2344
 
#ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
2345
 
        char *cur = str;
2346
 
        char *pc, *pv;
2347
 
        unsigned long val;
2348
 
        int i,  c;
2349
 
        int xi = 0;
2350
 
 
2351
 
        while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
2352
 
                char *pe;
2353
 
 
2354
 
                val = 0;
2355
 
                pv = pc;
2356
 
                c = *++pv;
2357
 
 
2358
 
                if      (c == 'n')
2359
 
                        val = 0;
2360
 
                else if (c == 'y')
2361
 
                        val = 1;
2362
 
                else
2363
 
                        val = (int) simple_strtoul(pv, &pe, 0);
2364
 
 
2365
 
                switch (get_setup_token(cur)) {
2366
 
                case OPT_MAX_TAG:
2367
 
                        sym_driver_setup.max_tag = val;
2368
 
                        if (!(pe && *pe == '/'))
2369
 
                                break;
2370
 
                        i = 0;
2371
 
                        while (*pe && *pe != ARG_SEP && 
2372
 
                                i < sizeof(sym_driver_setup.tag_ctrl)-1) {
2373
 
                                sym_driver_setup.tag_ctrl[i++] = *pe++;
2374
 
                        }
2375
 
                        sym_driver_setup.tag_ctrl[i] = '\0';
2376
 
                        break;
2377
 
                case OPT_SAFE_SETUP:
2378
 
                        memcpy(&sym_driver_setup, &sym_driver_safe_setup,
2379
 
                                sizeof(sym_driver_setup));
2380
 
                        break;
2381
 
                case OPT_EXCLUDE:
2382
 
                        if (xi < 8)
2383
 
                                sym_driver_setup.excludes[xi++] = val;
2384
 
                        break;
2385
 
 
2386
 
#define __SIMPLE_OPTION(NAME, name) \
2387
 
                case OPT_ ## NAME :             \
2388
 
                        sym_driver_setup.name = val;\
2389
 
                        break;
2390
 
 
2391
 
                __SIMPLE_OPTION(PCI_PARITY, pci_parity)
2392
 
                __SIMPLE_OPTION(SCSI_PARITY, scsi_parity)
2393
 
                __SIMPLE_OPTION(MIN_SYNC, min_sync)
2394
 
                __SIMPLE_OPTION(BURST_ORDER, burst_order)
2395
 
                __SIMPLE_OPTION(SCSI_LED, scsi_led)
2396
 
                __SIMPLE_OPTION(MAX_WIDE, max_wide)
2397
 
                __SIMPLE_OPTION(SCSI_DIFF, scsi_diff)
2398
 
                __SIMPLE_OPTION(IRQ_MODE, irq_mode)
2399
 
                __SIMPLE_OPTION(SCSI_BUS_CHECK, scsi_bus_check)
2400
 
                __SIMPLE_OPTION(HOST_ID, host_id)
2401
 
                __SIMPLE_OPTION(MAX_OFFS, max_offs)
2402
 
                __SIMPLE_OPTION(MAX_LUN, max_lun)
2403
 
                __SIMPLE_OPTION(PCI_FIX_UP, pci_fix_up)
2404
 
                __SIMPLE_OPTION(REVERSE_PROBE, reverse_probe)
2405
 
                __SIMPLE_OPTION(VERBOSE, verbose)
2406
 
                __SIMPLE_OPTION(DEBUG, debug)
2407
 
                __SIMPLE_OPTION(SETTLE_DELAY, settle_delay)
2408
 
                __SIMPLE_OPTION(USE_NVRAM, use_nvram)
2409
 
 
2410
 
#undef __SIMPLE_OPTION
2411
 
 
2412
 
                default:
2413
 
                        printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
2414
 
                        break;
2415
 
                }
2416
 
 
2417
 
                if ((cur = strchr(cur, ARG_SEP)) != NULL)
2418
 
                        ++cur;
2419
 
        }
2420
 
#endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2421
 
        return 1;
2422
 
}
2423
 
 
2424
 
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,13)
2425
 
#ifndef MODULE
2426
 
__setup("sym53c8xx=", sym53c8xx_setup);
2427
 
#endif
2428
 
#endif
2429
 
 
2430
 
#ifdef  SYM_CONF_PQS_PDS_SUPPORT
2431
 
/*
2432
 
 *  Detect all NCR PQS/PDS boards and keep track of their bus nr.
2433
 
 *
2434
 
 *  The NCR PQS or PDS card is constructed as a DEC bridge
2435
 
 *  behind which sit a proprietary NCR memory controller and
2436
 
 *  four or two 53c875s as separate devices.  In its usual mode
2437
 
 *  of operation, the 875s are slaved to the memory controller
2438
 
 *  for all transfers.  We can tell if an 875 is part of a
2439
 
 *  PQS/PDS or not since if it is, it will be on the same bus
2440
 
 *  as the memory controller.  To operate with the Linux
2441
 
 *  driver, the memory controller is disabled and the 875s
2442
 
 *  freed to function independently.  The only wrinkle is that
2443
 
 *  the preset SCSI ID (which may be zero) must be read in from
2444
 
 *  a special configuration space register of the 875
2445
 
 */
2446
 
#ifndef SYM_CONF_MAX_PQS_BUS
2447
 
#define SYM_CONF_MAX_PQS_BUS 16
2448
 
#endif
2449
 
static int pqs_bus[SYM_CONF_MAX_PQS_BUS] __initdata = { 0 };
2450
 
 
2451
 
static void __init sym_detect_pqs_pds(void)
2452
 
{
2453
 
        short index;
2454
 
        pcidev_t dev = PCIDEV_NULL;
2455
 
 
2456
 
        for(index=0; index < SYM_CONF_MAX_PQS_BUS; index++) {
2457
 
                u_char tmp;
2458
 
 
2459
 
                dev = pci_find_device(0x101a, 0x0009, dev);
2460
 
                if (dev == PCIDEV_NULL) {
2461
 
                        pqs_bus[index] = -1;
2462
 
                        break;
2463
 
                }
2464
 
                printf_info(NAME53C8XX ": NCR PQS/PDS memory controller detected on bus %d\n", PciBusNumber(dev));
2465
 
                pci_read_config_byte(dev, 0x44, &tmp);
2466
 
                /* bit 1: allow individual 875 configuration */
2467
 
                tmp |= 0x2;
2468
 
                pci_write_config_byte(dev, 0x44, tmp);
2469
 
                pci_read_config_byte(dev, 0x45, &tmp);
2470
 
                /* bit 2: drive individual 875 interrupts to the bus */
2471
 
                tmp |= 0x4;
2472
 
                pci_write_config_byte(dev, 0x45, tmp);
2473
 
 
2474
 
                pqs_bus[index] = PciBusNumber(dev);
2475
 
        }
2476
 
}
2477
 
#endif /* SYM_CONF_PQS_PDS_SUPPORT */
2478
 
 
2479
 
/*
2480
 
 *  Read and check the PCI configuration for any detected NCR 
2481
 
 *  boards and save data for attaching after all boards have 
2482
 
 *  been detected.
2483
 
 */
2484
 
static int __init
2485
 
sym53c8xx_pci_init(Scsi_Host_Template *tpnt, pcidev_t pdev, sym_device *device)
2486
 
{
2487
 
        u_short vendor_id, device_id, command, status_reg;
2488
 
        u_char cache_line_size;
2489
 
        u_char suggested_cache_line_size = 0;
2490
 
        u_char pci_fix_up = SYM_SETUP_PCI_FIX_UP;
2491
 
        u_char revision;
2492
 
        u_int irq;
2493
 
        u_long base, base_2, base_io; 
2494
 
        u_long base_c, base_2_c, io_port; 
2495
 
        int i;
2496
 
        sym_chip *chip;
2497
 
 
2498
 
        /* Choose some short name for this device */
2499
 
        sprintf(device->s.inst_name, "sym.%d.%d.%d",
2500
 
                PciBusNumber(pdev),
2501
 
                (int) (PciDeviceFn(pdev) & 0xf8) >> 3,
2502
 
                (int) (PciDeviceFn(pdev) & 7));
2503
 
 
2504
 
        /*
2505
 
         *  Read needed minimal info from the PCI config space.
2506
 
         */
2507
 
        vendor_id = PciVendorId(pdev);
2508
 
        device_id = PciDeviceId(pdev);
2509
 
        irq       = PciIrqLine(pdev);
2510
 
 
2511
 
        i = pci_get_base_address(pdev, 0, &base_io);
2512
 
        io_port = pci_get_base_cookie(pdev, 0);
2513
 
 
2514
 
        base_c = pci_get_base_cookie(pdev, i);
2515
 
        i = pci_get_base_address(pdev, i, &base);
2516
 
 
2517
 
        base_2_c = pci_get_base_cookie(pdev, i);
2518
 
        (void) pci_get_base_address(pdev, i, &base_2);
2519
 
 
2520
 
        io_port &= PCI_BASE_ADDRESS_IO_MASK;
2521
 
        base    &= PCI_BASE_ADDRESS_MEM_MASK;
2522
 
        base_2  &= PCI_BASE_ADDRESS_MEM_MASK;
2523
 
 
2524
 
        pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
2525
 
 
2526
 
        /*
2527
 
         *  If user excluded this chip, donnot initialize it.
2528
 
         */
2529
 
        if (base_io) {
2530
 
                for (i = 0 ; i < 8 ; i++) {
2531
 
                        if (sym_driver_setup.excludes[i] == base_io)
2532
 
                                return -1;
2533
 
                }
2534
 
        }
2535
 
 
2536
 
        /*
2537
 
         *  Leave here if another driver attached the chip.
2538
 
         */
2539
 
        if (io_port && check_region (io_port, 128)) {
2540
 
                printf_info("%s: IO region 0x%lx[0..127] is in use\n",
2541
 
                            sym_name(device), (long) io_port);
2542
 
                return -1;
2543
 
        }
2544
 
 
2545
 
        /*
2546
 
         *  Check if the chip is supported.
2547
 
         */
2548
 
        chip = sym_lookup_pci_chip_table(device_id, revision);
2549
 
        if (!chip) {
2550
 
                printf_info("%s: device not supported\n", sym_name(device));
2551
 
                return -1;
2552
 
        }
2553
 
 
2554
 
        /*
2555
 
         *  Check if the chip has been assigned resources we need.
2556
 
         */
2557
 
#ifdef SYM_CONF_IOMAPPED
2558
 
        if (!io_port) {
2559
 
                printf_info("%s: IO base address disabled.\n",
2560
 
                            sym_name(device));
2561
 
                return -1;
2562
 
        }
2563
 
#else
2564
 
        if (!base) {
2565
 
                printf_info("%s: MMIO base address disabled.\n",
2566
 
                            sym_name(device));
2567
 
                return -1;
2568
 
        }
2569
 
#endif
2570
 
 
2571
 
        /*
2572
 
         *  Ignore Symbios chips controlled by various RAID controllers.
2573
 
         *  These controllers set value 0x52414944 at RAM end - 16.
2574
 
         */
2575
 
#if defined(__i386__) && !defined(SYM_OPT_NO_BUS_MEMORY_MAPPING)
2576
 
        if (base_2_c) {
2577
 
                unsigned int ram_size, ram_val;
2578
 
                u_long ram_ptr;
2579
 
 
2580
 
                if (chip->features & FE_RAM8K)
2581
 
                        ram_size = 8192;
2582
 
                else
2583
 
                        ram_size = 4096;
2584
 
 
2585
 
                ram_ptr = pci_map_mem(base_2_c, ram_size);
2586
 
                if (ram_ptr) {
2587
 
                        ram_val = readl_raw(ram_ptr + ram_size - 16);
2588
 
                        pci_unmap_mem(ram_ptr, ram_size);
2589
 
                        if (ram_val == 0x52414944) {
2590
 
                                printf_info("%s: not initializing, "
2591
 
                                            "driven by RAID controller.\n",
2592
 
                                            sym_name(device));
2593
 
                                return -1;
2594
 
                        }
2595
 
                }
2596
 
        }
2597
 
#endif /* i386 and PCI MEMORY accessible */
2598
 
 
2599
 
        /*
2600
 
         *  Copy the chip description to our device structure, 
2601
 
         *  so we can make it match the actual device and options.
2602
 
         */
2603
 
        bcopy(chip, &device->chip, sizeof(device->chip));
2604
 
        device->chip.revision_id = revision;
2605
 
 
2606
 
        /*
2607
 
         *  Read additionnal info from the configuration space.
2608
 
         */
2609
 
        pci_read_config_word(pdev, PCI_COMMAND,         &command);
2610
 
        pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size);
2611
 
 
2612
 
        /*
2613
 
         * Enable missing capabilities in the PCI COMMAND register.
2614
 
         */
2615
 
#ifdef SYM_CONF_IOMAPPED
2616
 
#define PCI_COMMAND_BITS_TO_ENABLE (PCI_COMMAND_IO | \
2617
 
        PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_PARITY)
2618
 
#else
2619
 
#define PCI_COMMAND_BITS_TO_ENABLE \
2620
 
        (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_PARITY)
2621
 
#endif
2622
 
        if ((command & PCI_COMMAND_BITS_TO_ENABLE)
2623
 
                    != PCI_COMMAND_BITS_TO_ENABLE) {
2624
 
                printf_info("%s: setting%s%s%s%s...\n", sym_name(device),
2625
 
                (command & PCI_COMMAND_IO)     ? "" : " PCI_COMMAND_IO",
2626
 
                (command & PCI_COMMAND_MEMORY) ? "" : " PCI_COMMAND_MEMORY",
2627
 
                (command & PCI_COMMAND_MASTER) ? "" : " PCI_COMMAND_MASTER",
2628
 
                (command & PCI_COMMAND_PARITY) ? "" : " PCI_COMMAND_PARITY");
2629
 
                command |= PCI_COMMAND_BITS_TO_ENABLE;
2630
 
                pci_write_config_word(pdev, PCI_COMMAND, command);
2631
 
        }
2632
 
#undef  PCI_COMMAND_BITS_TO_ENABLE
2633
 
 
2634
 
        /*
2635
 
         *  If cache line size is not configured, suggest
2636
 
         *  a value for well known CPUs.
2637
 
         */
2638
 
#if defined(__i386__) && !defined(MODULE)
2639
 
        if (!cache_line_size && boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
2640
 
                switch(boot_cpu_data.x86) {
2641
 
                case 4: suggested_cache_line_size = 4;   break;
2642
 
                case 6: if (boot_cpu_data.x86_model > 8) break;
2643
 
                case 5: suggested_cache_line_size = 8;   break;
2644
 
                }
2645
 
        }
2646
 
#endif  /* __i386__ */
2647
 
 
2648
 
        /*
2649
 
         *  Some features are required to be enabled in order to 
2650
 
         *  work around some chip problems. :) ;)
2651
 
         *  (ITEM 12 of a DEL about the 896 I haven't yet).
2652
 
         *  We must ensure the chip will use WRITE AND INVALIDATE.
2653
 
         *  The revision number limit is for now arbitrary.
2654
 
         */
2655
 
        if (device_id == PCI_DEVICE_ID_NCR_53C896 && revision < 0x4) {
2656
 
                chip->features  |= (FE_WRIE | FE_CLSE);
2657
 
                pci_fix_up      |=  3;  /* Force appropriate PCI fix-up */
2658
 
        }
2659
 
 
2660
 
#ifdef  SYM_CONF_PCI_FIX_UP
2661
 
        /*
2662
 
         *  Try to fix up PCI config according to wished features.
2663
 
         */
2664
 
        if ((pci_fix_up & 1) && (chip->features & FE_CLSE) && 
2665
 
            !cache_line_size && suggested_cache_line_size) {
2666
 
                cache_line_size = suggested_cache_line_size;
2667
 
                pci_write_config_byte(pdev,
2668
 
                                      PCI_CACHE_LINE_SIZE, cache_line_size);
2669
 
                printf_info("%s: PCI_CACHE_LINE_SIZE set to %d.\n",
2670
 
                            sym_name(device), cache_line_size);
2671
 
        }
2672
 
 
2673
 
        if ((pci_fix_up & 2) && cache_line_size &&
2674
 
            (chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
2675
 
                printf_info("%s: setting PCI_COMMAND_INVALIDATE.\n",
2676
 
                            sym_name(device));
2677
 
                command |= PCI_COMMAND_INVALIDATE;
2678
 
                pci_write_config_word(pdev, PCI_COMMAND, command);
2679
 
        }
2680
 
#endif  /* SYM_CONF_PCI_FIX_UP */
2681
 
 
2682
 
        /*
2683
 
         *  Work around for errant bit in 895A. The 66Mhz
2684
 
         *  capable bit is set erroneously. Clear this bit.
2685
 
         *  (Item 1 DEL 533)
2686
 
         *
2687
 
         *  Make sure Config space and Features agree.
2688
 
         *
2689
 
         *  Recall: writes are not normal to status register -
2690
 
         *  write a 1 to clear and a 0 to leave unchanged.
2691
 
         *  Can only reset bits.
2692
 
         */
2693
 
        pci_read_config_word(pdev, PCI_STATUS, &status_reg);
2694
 
        if (chip->features & FE_66MHZ) {
2695
 
                if (!(status_reg & PCI_STATUS_66MHZ))
2696
 
                        chip->features &= ~FE_66MHZ;
2697
 
        }
2698
 
        else {
2699
 
                if (status_reg & PCI_STATUS_66MHZ) {
2700
 
                        status_reg = PCI_STATUS_66MHZ;
2701
 
                        pci_write_config_word(pdev, PCI_STATUS, status_reg);
2702
 
                        pci_read_config_word(pdev, PCI_STATUS, &status_reg);
2703
 
                }
2704
 
        }
2705
 
 
2706
 
        /*
2707
 
         *  Initialise device structure with items required by sym_attach.
2708
 
         */
2709
 
        device->pdev            = pdev;
2710
 
        device->s.bus           = PciBusNumber(pdev);
2711
 
        device->s.device_fn     = PciDeviceFn(pdev);
2712
 
        device->s.base          = base;
2713
 
        device->s.base_2        = base_2;
2714
 
        device->s.base_c        = base_c;
2715
 
        device->s.base_2_c      = base_2_c;
2716
 
        device->s.io_port       = io_port;
2717
 
        device->s.irq           = irq;
2718
 
        device->attach_done     = 0;
2719
 
 
2720
 
        return 0;
2721
 
}
2722
 
 
2723
 
/*
2724
 
 *  List of supported NCR chip ids
2725
 
 */
2726
 
static u_short sym_chip_ids[] __initdata        = {
2727
 
        PCI_ID_SYM53C810,
2728
 
        PCI_ID_SYM53C815,
2729
 
        PCI_ID_SYM53C825,
2730
 
        PCI_ID_SYM53C860,
2731
 
        PCI_ID_SYM53C875,
2732
 
        PCI_ID_SYM53C875_2,
2733
 
        PCI_ID_SYM53C885,
2734
 
        PCI_ID_SYM53C875A,
2735
 
        PCI_ID_SYM53C895,
2736
 
        PCI_ID_SYM53C896,
2737
 
        PCI_ID_SYM53C895A,
2738
 
        PCI_ID_LSI53C1510D,
2739
 
        PCI_ID_LSI53C1010,
2740
 
        PCI_ID_LSI53C1010_2
2741
 
};
2742
 
 
2743
 
/*
2744
 
 *  Detect all 53c8xx hosts and then attach them.
2745
 
 *
2746
 
 *  If we are using NVRAM, once all hosts are detected, we need to 
2747
 
 *  check any NVRAM for boot order in case detect and boot order 
2748
 
 *  differ and attach them using the order in the NVRAM.
2749
 
 *
2750
 
 *  If no NVRAM is found or data appears invalid attach boards in 
2751
 
 *  the order they are detected.
2752
 
 */
2753
 
int __init sym53c8xx_detect(Scsi_Host_Template *tpnt)
2754
 
{
2755
 
        pcidev_t pcidev;
2756
 
        int i, j, chips, hosts, count;
2757
 
        int attach_count = 0;
2758
 
        sym_device *devtbl, *devp;
2759
 
        sym_nvram  nvram;
2760
 
#if SYM_CONF_NVRAM_SUPPORT
2761
 
        sym_nvram  nvram0, *nvp;
2762
 
#endif
2763
 
 
2764
 
        /*
2765
 
         *  PCI is required.
2766
 
         */
2767
 
        if (!pci_present())
2768
 
                return 0;
2769
 
 
2770
 
        /*
2771
 
         *    Initialize driver general stuff.
2772
 
         */
2773
 
/* SAE: No proc */
2774
 
#ifdef XENO_KILLED
2775
 
#ifdef SYM_LINUX_PROC_INFO_SUPPORT
2776
 
#if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
2777
 
     tpnt->proc_dir  = &proc_scsi_sym53c8xx;
2778
 
#else
2779
 
     tpnt->proc_name = NAME53C8XX;
2780
 
#endif
2781
 
     tpnt->proc_info = sym53c8xx_proc_info;
2782
 
#endif
2783
 
#endif /* XENO_KILLED */
2784
 
 
2785
 
#ifdef SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
2786
 
#ifdef MODULE
2787
 
if (sym53c8xx)
2788
 
        sym53c8xx_setup(sym53c8xx);
2789
 
#endif
2790
 
#ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
2791
 
        sym_debug_flags = sym_driver_setup.debug;
2792
 
#endif
2793
 
        if (boot_verbose >= 2)
2794
 
                sym53c8xx_print_driver_setup();
2795
 
#endif /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2796
 
 
2797
 
        /*
2798
 
         *  Allocate the device table since we donnot want to 
2799
 
         *  overflow the kernel stack.
2800
 
         *  1 x 4K PAGE is enough for more than 40 devices for i386.
2801
 
         */
2802
 
        devtbl = sym_calloc(PAGE_SIZE, "DEVTBL");
2803
 
        if (!devtbl)
2804
 
                return 0;
2805
 
 
2806
 
        /*
2807
 
         *  Detect all NCR PQS/PDS memory controllers.
2808
 
         */
2809
 
#ifdef  SYM_CONF_PQS_PDS_SUPPORT
2810
 
        sym_detect_pqs_pds();
2811
 
#endif
2812
 
 
2813
 
        /* 
2814
 
         *  Detect all 53c8xx hosts.
2815
 
         *  Save the first Symbios NVRAM content if any 
2816
 
         *  for the boot order.
2817
 
         */
2818
 
        chips   = sizeof(sym_chip_ids)  / sizeof(sym_chip_ids[0]);
2819
 
        hosts   = PAGE_SIZE             / sizeof(*devtbl);
2820
 
#if SYM_CONF_NVRAM_SUPPORT
2821
 
        nvp = (sym_driver_setup.use_nvram & 0x1) ? &nvram0 : 0;
2822
 
#endif
2823
 
        j = 0;
2824
 
        count = 0;
2825
 
        pcidev = PCIDEV_NULL;
2826
 
        while (1) {
2827
 
                char *msg = "";
2828
 
                if (count >= hosts)
2829
 
                        break;
2830
 
                if (j >= chips)
2831
 
                        break;
2832
 
                i = sym_driver_setup.reverse_probe ? chips - 1 - j : j;
2833
 
                pcidev = pci_find_device(PCI_VENDOR_ID_NCR, sym_chip_ids[i],
2834
 
                                         pcidev);
2835
 
                if (pcidev == PCIDEV_NULL) {
2836
 
                        ++j;
2837
 
                        continue;
2838
 
                }
2839
 
                /* This one is guaranteed by AC to do nothing :-) */
2840
 
                if (pci_enable_device(pcidev))
2841
 
                        continue;
2842
 
                /* Some HW as the HP LH4 may report twice PCI devices */
2843
 
                for (i = 0; i < count ; i++) {
2844
 
                        if (devtbl[i].s.bus       == PciBusNumber(pcidev) && 
2845
 
                            devtbl[i].s.device_fn == PciDeviceFn(pcidev))
2846
 
                                break;
2847
 
                }
2848
 
                if (i != count) /* Ignore this device if we already have it */
2849
 
                        continue;
2850
 
                devp = &devtbl[count];
2851
 
                devp->host_id = SYM_SETUP_HOST_ID;
2852
 
                devp->attach_done = 0;
2853
 
                if (sym53c8xx_pci_init(tpnt, pcidev, devp)) {
2854
 
                        continue;
2855
 
                }
2856
 
                ++count;
2857
 
#if SYM_CONF_NVRAM_SUPPORT
2858
 
                if (nvp) {
2859
 
                        sym_get_nvram(devp, nvp);
2860
 
                        switch(nvp->type) {
2861
 
                        case SYM_SYMBIOS_NVRAM:
2862
 
                                /*
2863
 
                                 *   Switch to the other nvram buffer, so that 
2864
 
                                 *   nvram0 will contain the first Symbios 
2865
 
                                 *   format NVRAM content with boot order.
2866
 
                                 */
2867
 
                                nvp = &nvram;
2868
 
                                msg = "with Symbios NVRAM";
2869
 
                                break;
2870
 
                        case SYM_TEKRAM_NVRAM:
2871
 
                                msg = "with Tekram NVRAM";
2872
 
                                break;
2873
 
                        }
2874
 
                }
2875
 
#endif
2876
 
#ifdef  SYM_CONF_PQS_PDS_SUPPORT
2877
 
                /*
2878
 
                 *  Match the BUS number for PQS/PDS devices.
2879
 
                 *  Read the SCSI ID from a special register mapped
2880
 
                 *  into the configuration space of the individual
2881
 
                 *  875s.  This register is set up by the PQS bios
2882
 
                 */
2883
 
                for(i = 0; i < SYM_CONF_MAX_PQS_BUS && pqs_bus[i] != -1; i++) {
2884
 
                        u_char tmp;
2885
 
                        if (pqs_bus[i] == PciBusNumber(pcidev)) {
2886
 
                                pci_read_config_byte(pcidev, 0x84, &tmp);
2887
 
                                devp->pqs_pds = 1;
2888
 
                                devp->host_id = tmp;
2889
 
                                break;
2890
 
                        }
2891
 
                }
2892
 
                if (devp->pqs_pds)
2893
 
                        msg = "(NCR PQS/PDS)";
2894
 
#endif
2895
 
                if (boot_verbose)
2896
 
                        printf_info("%s: 53c%s detected %s\n",
2897
 
                                    sym_name(devp), devp->chip.name, msg);
2898
 
        }
2899
 
 
2900
 
        /*
2901
 
         *  If we have found a SYMBIOS NVRAM, use first the NVRAM boot 
2902
 
         *  sequence as device boot order.
2903
 
         *  check devices in the boot record against devices detected. 
2904
 
         *  attach devices if we find a match. boot table records that 
2905
 
         *  do not match any detected devices will be ignored. 
2906
 
         *  devices that do not match any boot table will not be attached
2907
 
         *  here but will attempt to be attached during the device table 
2908
 
         *  rescan.
2909
 
         */
2910
 
#if SYM_CONF_NVRAM_SUPPORT
2911
 
        if (!nvp || nvram0.type != SYM_SYMBIOS_NVRAM)
2912
 
                goto next;
2913
 
        for (i = 0; i < 4; i++) {
2914
 
                Symbios_host *h = &nvram0.data.Symbios.host[i];
2915
 
                for (j = 0 ; j < count ; j++) {
2916
 
                        devp = &devtbl[j];
2917
 
                        if (h->device_fn != devp->s.device_fn ||
2918
 
                            h->bus_nr    != devp->s.bus  ||
2919
 
                            h->device_id != devp->chip.device_id)
2920
 
                                continue;
2921
 
                        if (devp->attach_done)
2922
 
                                continue;
2923
 
                        if (h->flags & SYMBIOS_INIT_SCAN_AT_BOOT) {
2924
 
                                sym_get_nvram(devp, nvp);
2925
 
                                if (!sym_attach (tpnt, attach_count, devp))
2926
 
                                        attach_count++;
2927
 
                        }
2928
 
                        else if (!(sym_driver_setup.use_nvram & 0x80))
2929
 
                                printf_info(
2930
 
                                      "%s: 53c%s state OFF thus not attached\n",
2931
 
                                      sym_name(devp), devp->chip.name);
2932
 
                        else
2933
 
                                continue;
2934
 
 
2935
 
                        devp->attach_done = 1;
2936
 
                        break;
2937
 
                }
2938
 
        }
2939
 
next:
2940
 
#endif
2941
 
 
2942
 
        /* 
2943
 
         *  Rescan device list to make sure all boards attached.
2944
 
         *  Devices without boot records will not be attached yet
2945
 
         *  so try to attach them here.
2946
 
         */
2947
 
        for (i= 0; i < count; i++) {
2948
 
                devp = &devtbl[i];
2949
 
                if (!devp->attach_done) {
2950
 
                        devp->nvram = &nvram;
2951
 
                        nvram.type = 0;
2952
 
#if SYM_CONF_NVRAM_SUPPORT
2953
 
                        sym_get_nvram(devp, nvp);
2954
 
#endif
2955
 
                        if (!sym_attach (tpnt, attach_count, devp))
2956
 
                                attach_count++;
2957
 
                }
2958
 
        }
2959
 
 
2960
 
        sym_mfree(devtbl, PAGE_SIZE, "DEVTBL");
2961
 
 
2962
 
        return attach_count;
2963
 
}
2964
 
 
2965
 
 
2966
 
 
2967
 
#ifdef MODULE
2968
 
/*
2969
 
 *  Linux release module stuff.
2970
 
 *
2971
 
 *  Called before unloading the module.
2972
 
 *  Detach the host.
2973
 
 *  We have to free resources and halt the NCR chip.
2974
 
 *
2975
 
 */
2976
 
static int sym_detach(hcb_p np)
2977
 
{
2978
 
        printk("%s: detaching ...\n", sym_name(np));
2979
 
 
2980
 
        /*
2981
 
         *  Try to delete the timer.
2982
 
         *  In the unlikely situation where this failed,
2983
 
         *  try to synchronize with the timer handler.
2984
 
         */
2985
 
#if LINUX_VERSION_CODE < LinuxVersionCode(2, 4, 0)
2986
 
        np->s.release_stage = 1;
2987
 
        if (!del_timer(&np->s.timer)) {
2988
 
                int i = 1000;
2989
 
                int k = 1;
2990
 
                while (1) {
2991
 
                        u_long flags;
2992
 
                        SYM_LOCK_HCB(np, flags);
2993
 
                        k = np->s.release_stage;
2994
 
                        SYM_UNLOCK_HCB(np, flags);
2995
 
                        if (k == 2 || !--i)
2996
 
                                break;
2997
 
                        MDELAY(5);
2998
 
                }
2999
 
                if (!i)
3000
 
                        printk("%s: failed to kill timer!\n", sym_name(np));
3001
 
        }
3002
 
        np->s.release_stage = 2;
3003
 
#else
3004
 
        (void)del_timer_sync(&np->s.timer);
3005
 
#endif
3006
 
 
3007
 
        /*
3008
 
         *  Reset NCR chip.
3009
 
         *  We should use sym_soft_reset(), but we donnot want to do 
3010
 
         *  so, since we may not be safe if interrupts occur.
3011
 
         */
3012
 
        printk("%s: resetting chip\n", sym_name(np));
3013
 
        OUTB (nc_istat, SRST);
3014
 
        UDELAY (10);
3015
 
        OUTB (nc_istat, 0);
3016
 
 
3017
 
        /*
3018
 
         *  Free host resources
3019
 
         */
3020
 
        sym_free_resources(np);
3021
 
 
3022
 
        return 1;
3023
 
}
3024
 
 
3025
 
int sym53c8xx_release(struct Scsi_Host *host)
3026
 
{
3027
 
     sym_detach(((struct host_data *) host->hostdata)->ncb);
3028
 
 
3029
 
     return 0;
3030
 
}
3031
 
#endif /* MODULE */
3032
 
 
3033
 
/*
3034
 
 * For bigots to keep silent. :)
3035
 
 */
3036
 
#ifdef MODULE_LICENSE
3037
 
MODULE_LICENSE("Dual BSD/GPL");
3038
 
#endif
3039
 
 
3040
 
/*
3041
 
 * Driver host template.
3042
 
 */
3043
 
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
3044
 
static
3045
 
#endif
3046
 
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0) || defined(MODULE)
3047
 
Scsi_Host_Template driver_template = SYM53C8XX;
3048
 
#include "../scsi_module.c.inc"
3049
 
#endif