]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/advansys.c
f797024120107b945a856f098bdb8f62790fd5af
[karo-tx-linux.git] / drivers / scsi / advansys.c
1 #define DRV_NAME "advansys"
2 #define ASC_VERSION "3.4"       /* AdvanSys Driver Version */
3
4 /*
5  * advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
6  *
7  * Copyright (c) 1995-2000 Advanced System Products, Inc.
8  * Copyright (c) 2000-2001 ConnectCom Solutions, Inc.
9  * Copyright (c) 2007 Matthew Wilcox <matthew@wil.cx>
10  * All Rights Reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  */
17
18 /*
19  * As of March 8, 2000 Advanced System Products, Inc. (AdvanSys)
20  * changed its name to ConnectCom Solutions, Inc.
21  * On June 18, 2001 Initio Corp. acquired ConnectCom's SCSI assets
22  */
23
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/ioport.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <linux/mm.h>
33 #include <linux/proc_fs.h>
34 #include <linux/init.h>
35 #include <linux/blkdev.h>
36 #include <linux/isa.h>
37 #include <linux/eisa.h>
38 #include <linux/pci.h>
39 #include <linux/spinlock.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/firmware.h>
42
43 #include <asm/io.h>
44 #include <asm/dma.h>
45
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_tcq.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_host.h>
51
52 /* FIXME:
53  *
54  *  1. Although all of the necessary command mapping places have the
55  *     appropriate dma_map.. APIs, the driver still processes its internal
56  *     queue using bus_to_virt() and virt_to_bus() which are illegal under
57  *     the API.  The entire queue processing structure will need to be
58  *     altered to fix this.
59  *  2. Need to add memory mapping workaround. Test the memory mapping.
60  *     If it doesn't work revert to I/O port access. Can a test be done
61  *     safely?
62  *  3. Handle an interrupt not working. Keep an interrupt counter in
63  *     the interrupt handler. In the timeout function if the interrupt
64  *     has not occurred then print a message and run in polled mode.
65  *  4. Need to add support for target mode commands, cf. CAM XPT.
66  *  5. check DMA mapping functions for failure
67  *  6. Use scsi_transport_spi
68  *  7. advansys_info is not safe against multiple simultaneous callers
69  *  8. Add module_param to override ISA/VLB ioport array
70  */
71 #warning this driver is still not properly converted to the DMA API
72
73 /* Enable driver /proc statistics. */
74 #define ADVANSYS_STATS
75
76 /* Enable driver tracing. */
77 #undef ADVANSYS_DEBUG
78
79 typedef unsigned char uchar;
80
81 #define isodd_word(val)   ((((uint)val) & (uint)0x0001) != 0)
82
83 #define PCI_VENDOR_ID_ASP               0x10cd
84 #define PCI_DEVICE_ID_ASP_1200A         0x1100
85 #define PCI_DEVICE_ID_ASP_ABP940        0x1200
86 #define PCI_DEVICE_ID_ASP_ABP940U       0x1300
87 #define PCI_DEVICE_ID_ASP_ABP940UW      0x2300
88 #define PCI_DEVICE_ID_38C0800_REV1      0x2500
89 #define PCI_DEVICE_ID_38C1600_REV1      0x2700
90
91 /*
92  * Enable CC_VERY_LONG_SG_LIST to support up to 64K element SG lists.
93  * The SRB structure will have to be changed and the ASC_SRB2SCSIQ()
94  * macro re-defined to be able to obtain a ASC_SCSI_Q pointer from the
95  * SRB structure.
96  */
97 #define CC_VERY_LONG_SG_LIST 0
98 #define ASC_SRB2SCSIQ(srb_ptr)  (srb_ptr)
99
100 #define PortAddr                 unsigned int   /* port address size  */
101 #define inp(port)                inb(port)
102 #define outp(port, byte)         outb((byte), (port))
103
104 #define inpw(port)               inw(port)
105 #define outpw(port, word)        outw((word), (port))
106
107 #define ASC_MAX_SG_QUEUE    7
108 #define ASC_MAX_SG_LIST     255
109
110 #define ASC_CS_TYPE  unsigned short
111
112 #define ASC_IS_ISA          (0x0001)
113 #define ASC_IS_ISAPNP       (0x0081)
114 #define ASC_IS_EISA         (0x0002)
115 #define ASC_IS_PCI          (0x0004)
116 #define ASC_IS_PCI_ULTRA    (0x0104)
117 #define ASC_IS_PCMCIA       (0x0008)
118 #define ASC_IS_MCA          (0x0020)
119 #define ASC_IS_VL           (0x0040)
120 #define ASC_IS_WIDESCSI_16  (0x0100)
121 #define ASC_IS_WIDESCSI_32  (0x0200)
122 #define ASC_IS_BIG_ENDIAN   (0x8000)
123
124 #define ASC_CHIP_MIN_VER_VL      (0x01)
125 #define ASC_CHIP_MAX_VER_VL      (0x07)
126 #define ASC_CHIP_MIN_VER_PCI     (0x09)
127 #define ASC_CHIP_MAX_VER_PCI     (0x0F)
128 #define ASC_CHIP_VER_PCI_BIT     (0x08)
129 #define ASC_CHIP_MIN_VER_ISA     (0x11)
130 #define ASC_CHIP_MIN_VER_ISA_PNP (0x21)
131 #define ASC_CHIP_MAX_VER_ISA     (0x27)
132 #define ASC_CHIP_VER_ISA_BIT     (0x30)
133 #define ASC_CHIP_VER_ISAPNP_BIT  (0x20)
134 #define ASC_CHIP_VER_ASYN_BUG    (0x21)
135 #define ASC_CHIP_VER_PCI             0x08
136 #define ASC_CHIP_VER_PCI_ULTRA_3150  (ASC_CHIP_VER_PCI | 0x02)
137 #define ASC_CHIP_VER_PCI_ULTRA_3050  (ASC_CHIP_VER_PCI | 0x03)
138 #define ASC_CHIP_MIN_VER_EISA (0x41)
139 #define ASC_CHIP_MAX_VER_EISA (0x47)
140 #define ASC_CHIP_VER_EISA_BIT (0x40)
141 #define ASC_CHIP_LATEST_VER_EISA   ((ASC_CHIP_MIN_VER_EISA - 1) + 3)
142 #define ASC_MAX_VL_DMA_COUNT    (0x07FFFFFFL)
143 #define ASC_MAX_PCI_DMA_COUNT   (0xFFFFFFFFL)
144 #define ASC_MAX_ISA_DMA_COUNT   (0x00FFFFFFL)
145
146 #define ASC_SCSI_ID_BITS  3
147 #define ASC_SCSI_TIX_TYPE     uchar
148 #define ASC_ALL_DEVICE_BIT_SET  0xFF
149 #define ASC_SCSI_BIT_ID_TYPE  uchar
150 #define ASC_MAX_TID       7
151 #define ASC_MAX_LUN       7
152 #define ASC_SCSI_WIDTH_BIT_SET  0xFF
153 #define ASC_MAX_SENSE_LEN   32
154 #define ASC_MIN_SENSE_LEN   14
155 #define ASC_SCSI_RESET_HOLD_TIME_US  60
156
157 /*
158  * Narrow boards only support 12-byte commands, while wide boards
159  * extend to 16-byte commands.
160  */
161 #define ASC_MAX_CDB_LEN     12
162 #define ADV_MAX_CDB_LEN     16
163
164 #define MS_SDTR_LEN    0x03
165 #define MS_WDTR_LEN    0x02
166
167 #define ASC_SG_LIST_PER_Q   7
168 #define QS_FREE        0x00
169 #define QS_READY       0x01
170 #define QS_DISC1       0x02
171 #define QS_DISC2       0x04
172 #define QS_BUSY        0x08
173 #define QS_ABORTED     0x40
174 #define QS_DONE        0x80
175 #define QC_NO_CALLBACK   0x01
176 #define QC_SG_SWAP_QUEUE 0x02
177 #define QC_SG_HEAD       0x04
178 #define QC_DATA_IN       0x08
179 #define QC_DATA_OUT      0x10
180 #define QC_URGENT        0x20
181 #define QC_MSG_OUT       0x40
182 #define QC_REQ_SENSE     0x80
183 #define QCSG_SG_XFER_LIST  0x02
184 #define QCSG_SG_XFER_MORE  0x04
185 #define QCSG_SG_XFER_END   0x08
186 #define QD_IN_PROGRESS       0x00
187 #define QD_NO_ERROR          0x01
188 #define QD_ABORTED_BY_HOST   0x02
189 #define QD_WITH_ERROR        0x04
190 #define QD_INVALID_REQUEST   0x80
191 #define QD_INVALID_HOST_NUM  0x81
192 #define QD_INVALID_DEVICE    0x82
193 #define QD_ERR_INTERNAL      0xFF
194 #define QHSTA_NO_ERROR               0x00
195 #define QHSTA_M_SEL_TIMEOUT          0x11
196 #define QHSTA_M_DATA_OVER_RUN        0x12
197 #define QHSTA_M_DATA_UNDER_RUN       0x12
198 #define QHSTA_M_UNEXPECTED_BUS_FREE  0x13
199 #define QHSTA_M_BAD_BUS_PHASE_SEQ    0x14
200 #define QHSTA_D_QDONE_SG_LIST_CORRUPTED 0x21
201 #define QHSTA_D_ASC_DVC_ERROR_CODE_SET  0x22
202 #define QHSTA_D_HOST_ABORT_FAILED       0x23
203 #define QHSTA_D_EXE_SCSI_Q_FAILED       0x24
204 #define QHSTA_D_EXE_SCSI_Q_BUSY_TIMEOUT 0x25
205 #define QHSTA_D_ASPI_NO_BUF_POOL        0x26
206 #define QHSTA_M_WTM_TIMEOUT         0x41
207 #define QHSTA_M_BAD_CMPL_STATUS_IN  0x42
208 #define QHSTA_M_NO_AUTO_REQ_SENSE   0x43
209 #define QHSTA_M_AUTO_REQ_SENSE_FAIL 0x44
210 #define QHSTA_M_TARGET_STATUS_BUSY  0x45
211 #define QHSTA_M_BAD_TAG_CODE        0x46
212 #define QHSTA_M_BAD_QUEUE_FULL_OR_BUSY  0x47
213 #define QHSTA_M_HUNG_REQ_SCSI_BUS_RESET 0x48
214 #define QHSTA_D_LRAM_CMP_ERROR        0x81
215 #define QHSTA_M_MICRO_CODE_ERROR_HALT 0xA1
216 #define ASC_FLAG_SCSIQ_REQ        0x01
217 #define ASC_FLAG_BIOS_SCSIQ_REQ   0x02
218 #define ASC_FLAG_BIOS_ASYNC_IO    0x04
219 #define ASC_FLAG_SRB_LINEAR_ADDR  0x08
220 #define ASC_FLAG_WIN16            0x10
221 #define ASC_FLAG_WIN32            0x20
222 #define ASC_FLAG_ISA_OVER_16MB    0x40
223 #define ASC_FLAG_DOS_VM_CALLBACK  0x80
224 #define ASC_TAG_FLAG_EXTRA_BYTES               0x10
225 #define ASC_TAG_FLAG_DISABLE_DISCONNECT        0x04
226 #define ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX  0x08
227 #define ASC_TAG_FLAG_DISABLE_CHK_COND_INT_HOST 0x40
228 #define ASC_SCSIQ_CPY_BEG              4
229 #define ASC_SCSIQ_SGHD_CPY_BEG         2
230 #define ASC_SCSIQ_B_FWD                0
231 #define ASC_SCSIQ_B_BWD                1
232 #define ASC_SCSIQ_B_STATUS             2
233 #define ASC_SCSIQ_B_QNO                3
234 #define ASC_SCSIQ_B_CNTL               4
235 #define ASC_SCSIQ_B_SG_QUEUE_CNT       5
236 #define ASC_SCSIQ_D_DATA_ADDR          8
237 #define ASC_SCSIQ_D_DATA_CNT          12
238 #define ASC_SCSIQ_B_SENSE_LEN         20
239 #define ASC_SCSIQ_DONE_INFO_BEG       22
240 #define ASC_SCSIQ_D_SRBPTR            22
241 #define ASC_SCSIQ_B_TARGET_IX         26
242 #define ASC_SCSIQ_B_CDB_LEN           28
243 #define ASC_SCSIQ_B_TAG_CODE          29
244 #define ASC_SCSIQ_W_VM_ID             30
245 #define ASC_SCSIQ_DONE_STATUS         32
246 #define ASC_SCSIQ_HOST_STATUS         33
247 #define ASC_SCSIQ_SCSI_STATUS         34
248 #define ASC_SCSIQ_CDB_BEG             36
249 #define ASC_SCSIQ_DW_REMAIN_XFER_ADDR 56
250 #define ASC_SCSIQ_DW_REMAIN_XFER_CNT  60
251 #define ASC_SCSIQ_B_FIRST_SG_WK_QP    48
252 #define ASC_SCSIQ_B_SG_WK_QP          49
253 #define ASC_SCSIQ_B_SG_WK_IX          50
254 #define ASC_SCSIQ_W_ALT_DC1           52
255 #define ASC_SCSIQ_B_LIST_CNT          6
256 #define ASC_SCSIQ_B_CUR_LIST_CNT      7
257 #define ASC_SGQ_B_SG_CNTL             4
258 #define ASC_SGQ_B_SG_HEAD_QP          5
259 #define ASC_SGQ_B_SG_LIST_CNT         6
260 #define ASC_SGQ_B_SG_CUR_LIST_CNT     7
261 #define ASC_SGQ_LIST_BEG              8
262 #define ASC_DEF_SCSI1_QNG    4
263 #define ASC_MAX_SCSI1_QNG    4
264 #define ASC_DEF_SCSI2_QNG    16
265 #define ASC_MAX_SCSI2_QNG    32
266 #define ASC_TAG_CODE_MASK    0x23
267 #define ASC_STOP_REQ_RISC_STOP      0x01
268 #define ASC_STOP_ACK_RISC_STOP      0x03
269 #define ASC_STOP_CLEAN_UP_BUSY_Q    0x10
270 #define ASC_STOP_CLEAN_UP_DISC_Q    0x20
271 #define ASC_STOP_HOST_REQ_RISC_HALT 0x40
272 #define ASC_TIDLUN_TO_IX(tid, lun)  (ASC_SCSI_TIX_TYPE)((tid) + ((lun)<<ASC_SCSI_ID_BITS))
273 #define ASC_TID_TO_TARGET_ID(tid)   (ASC_SCSI_BIT_ID_TYPE)(0x01 << (tid))
274 #define ASC_TIX_TO_TARGET_ID(tix)   (0x01 << ((tix) & ASC_MAX_TID))
275 #define ASC_TIX_TO_TID(tix)         ((tix) & ASC_MAX_TID)
276 #define ASC_TID_TO_TIX(tid)         ((tid) & ASC_MAX_TID)
277 #define ASC_TIX_TO_LUN(tix)         (((tix) >> ASC_SCSI_ID_BITS) & ASC_MAX_LUN)
278 #define ASC_QNO_TO_QADDR(q_no)      ((ASC_QADR_BEG)+((int)(q_no) << 6))
279
280 typedef struct asc_scsiq_1 {
281         uchar status;
282         uchar q_no;
283         uchar cntl;
284         uchar sg_queue_cnt;
285         uchar target_id;
286         uchar target_lun;
287         __le32 data_addr;
288         __le32 data_cnt;
289         __le32 sense_addr;
290         uchar sense_len;
291         uchar extra_bytes;
292 } ASC_SCSIQ_1;
293
294 typedef struct asc_scsiq_2 {
295         u32 srb_tag;
296         uchar target_ix;
297         uchar flag;
298         uchar cdb_len;
299         uchar tag_code;
300         ushort vm_id;
301 } ASC_SCSIQ_2;
302
303 typedef struct asc_scsiq_3 {
304         uchar done_stat;
305         uchar host_stat;
306         uchar scsi_stat;
307         uchar scsi_msg;
308 } ASC_SCSIQ_3;
309
310 typedef struct asc_scsiq_4 {
311         uchar cdb[ASC_MAX_CDB_LEN];
312         uchar y_first_sg_list_qp;
313         uchar y_working_sg_qp;
314         uchar y_working_sg_ix;
315         uchar y_res;
316         ushort x_req_count;
317         ushort x_reconnect_rtn;
318         __le32 x_saved_data_addr;
319         __le32 x_saved_data_cnt;
320 } ASC_SCSIQ_4;
321
322 typedef struct asc_q_done_info {
323         ASC_SCSIQ_2 d2;
324         ASC_SCSIQ_3 d3;
325         uchar q_status;
326         uchar q_no;
327         uchar cntl;
328         uchar sense_len;
329         uchar extra_bytes;
330         uchar res;
331         u32 remain_bytes;
332 } ASC_QDONE_INFO;
333
334 typedef struct asc_sg_list {
335         __le32 addr;
336         __le32 bytes;
337 } ASC_SG_LIST;
338
339 typedef struct asc_sg_head {
340         ushort entry_cnt;
341         ushort queue_cnt;
342         ushort entry_to_copy;
343         ushort res;
344         ASC_SG_LIST sg_list[0];
345 } ASC_SG_HEAD;
346
347 typedef struct asc_scsi_q {
348         ASC_SCSIQ_1 q1;
349         ASC_SCSIQ_2 q2;
350         uchar *cdbptr;
351         ASC_SG_HEAD *sg_head;
352         ushort remain_sg_entry_cnt;
353         ushort next_sg_index;
354 } ASC_SCSI_Q;
355
356 typedef struct asc_scsi_req_q {
357         ASC_SCSIQ_1 r1;
358         ASC_SCSIQ_2 r2;
359         uchar *cdbptr;
360         ASC_SG_HEAD *sg_head;
361         uchar *sense_ptr;
362         ASC_SCSIQ_3 r3;
363         uchar cdb[ASC_MAX_CDB_LEN];
364         uchar sense[ASC_MIN_SENSE_LEN];
365 } ASC_SCSI_REQ_Q;
366
367 typedef struct asc_scsi_bios_req_q {
368         ASC_SCSIQ_1 r1;
369         ASC_SCSIQ_2 r2;
370         uchar *cdbptr;
371         ASC_SG_HEAD *sg_head;
372         uchar *sense_ptr;
373         ASC_SCSIQ_3 r3;
374         uchar cdb[ASC_MAX_CDB_LEN];
375         uchar sense[ASC_MIN_SENSE_LEN];
376 } ASC_SCSI_BIOS_REQ_Q;
377
378 typedef struct asc_risc_q {
379         uchar fwd;
380         uchar bwd;
381         ASC_SCSIQ_1 i1;
382         ASC_SCSIQ_2 i2;
383         ASC_SCSIQ_3 i3;
384         ASC_SCSIQ_4 i4;
385 } ASC_RISC_Q;
386
387 typedef struct asc_sg_list_q {
388         uchar seq_no;
389         uchar q_no;
390         uchar cntl;
391         uchar sg_head_qp;
392         uchar sg_list_cnt;
393         uchar sg_cur_list_cnt;
394 } ASC_SG_LIST_Q;
395
396 typedef struct asc_risc_sg_list_q {
397         uchar fwd;
398         uchar bwd;
399         ASC_SG_LIST_Q sg;
400         ASC_SG_LIST sg_list[7];
401 } ASC_RISC_SG_LIST_Q;
402
403 #define ASCQ_ERR_Q_STATUS             0x0D
404 #define ASCQ_ERR_CUR_QNG              0x17
405 #define ASCQ_ERR_SG_Q_LINKS           0x18
406 #define ASCQ_ERR_ISR_RE_ENTRY         0x1A
407 #define ASCQ_ERR_CRITICAL_RE_ENTRY    0x1B
408 #define ASCQ_ERR_ISR_ON_CRITICAL      0x1C
409
410 /*
411  * Warning code values are set in ASC_DVC_VAR  'warn_code'.
412  */
413 #define ASC_WARN_NO_ERROR             0x0000
414 #define ASC_WARN_IO_PORT_ROTATE       0x0001
415 #define ASC_WARN_EEPROM_CHKSUM        0x0002
416 #define ASC_WARN_IRQ_MODIFIED         0x0004
417 #define ASC_WARN_AUTO_CONFIG          0x0008
418 #define ASC_WARN_CMD_QNG_CONFLICT     0x0010
419 #define ASC_WARN_EEPROM_RECOVER       0x0020
420 #define ASC_WARN_CFG_MSW_RECOVER      0x0040
421
422 /*
423  * Error code values are set in {ASC/ADV}_DVC_VAR  'err_code'.
424  */
425 #define ASC_IERR_NO_CARRIER             0x0001  /* No more carrier memory */
426 #define ASC_IERR_MCODE_CHKSUM           0x0002  /* micro code check sum error */
427 #define ASC_IERR_SET_PC_ADDR            0x0004
428 #define ASC_IERR_START_STOP_CHIP        0x0008  /* start/stop chip failed */
429 #define ASC_IERR_ILLEGAL_CONNECTION     0x0010  /* Illegal cable connection */
430 #define ASC_IERR_SINGLE_END_DEVICE      0x0020  /* SE device on DIFF bus */
431 #define ASC_IERR_REVERSED_CABLE         0x0040  /* Narrow flat cable reversed */
432 #define ASC_IERR_SET_SCSI_ID            0x0080  /* set SCSI ID failed */
433 #define ASC_IERR_HVD_DEVICE             0x0100  /* HVD device on LVD port */
434 #define ASC_IERR_BAD_SIGNATURE          0x0200  /* signature not found */
435 #define ASC_IERR_NO_BUS_TYPE            0x0400
436 #define ASC_IERR_BIST_PRE_TEST          0x0800  /* BIST pre-test error */
437 #define ASC_IERR_BIST_RAM_TEST          0x1000  /* BIST RAM test error */
438 #define ASC_IERR_BAD_CHIPTYPE           0x2000  /* Invalid chip_type setting */
439
440 #define ASC_DEF_MAX_TOTAL_QNG   (0xF0)
441 #define ASC_MIN_TAG_Q_PER_DVC   (0x04)
442 #define ASC_MIN_FREE_Q        (0x02)
443 #define ASC_MIN_TOTAL_QNG     ((ASC_MAX_SG_QUEUE)+(ASC_MIN_FREE_Q))
444 #define ASC_MAX_TOTAL_QNG 240
445 #define ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG 16
446 #define ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG   8
447 #define ASC_MAX_PCI_INRAM_TOTAL_QNG  20
448 #define ASC_MAX_INRAM_TAG_QNG   16
449 #define ASC_IOADR_GAP   0x10
450 #define ASC_SYN_MAX_OFFSET         0x0F
451 #define ASC_DEF_SDTR_OFFSET        0x0F
452 #define ASC_SDTR_ULTRA_PCI_10MB_INDEX  0x02
453 #define ASYN_SDTR_DATA_FIX_PCI_REV_AB 0x41
454
455 /* The narrow chip only supports a limited selection of transfer rates.
456  * These are encoded in the range 0..7 or 0..15 depending whether the chip
457  * is Ultra-capable or not.  These tables let us convert from one to the other.
458  */
459 static const unsigned char asc_syn_xfer_period[8] = {
460         25, 30, 35, 40, 50, 60, 70, 85
461 };
462
463 static const unsigned char asc_syn_ultra_xfer_period[16] = {
464         12, 19, 25, 32, 38, 44, 50, 57, 63, 69, 75, 82, 88, 94, 100, 107
465 };
466
467 typedef struct ext_msg {
468         uchar msg_type;
469         uchar msg_len;
470         uchar msg_req;
471         union {
472                 struct {
473                         uchar sdtr_xfer_period;
474                         uchar sdtr_req_ack_offset;
475                 } sdtr;
476                 struct {
477                         uchar wdtr_width;
478                 } wdtr;
479                 struct {
480                         uchar mdp_b3;
481                         uchar mdp_b2;
482                         uchar mdp_b1;
483                         uchar mdp_b0;
484                 } mdp;
485         } u_ext_msg;
486         uchar res;
487 } EXT_MSG;
488
489 #define xfer_period     u_ext_msg.sdtr.sdtr_xfer_period
490 #define req_ack_offset  u_ext_msg.sdtr.sdtr_req_ack_offset
491 #define wdtr_width      u_ext_msg.wdtr.wdtr_width
492 #define mdp_b3          u_ext_msg.mdp_b3
493 #define mdp_b2          u_ext_msg.mdp_b2
494 #define mdp_b1          u_ext_msg.mdp_b1
495 #define mdp_b0          u_ext_msg.mdp_b0
496
497 typedef struct asc_dvc_cfg {
498         ASC_SCSI_BIT_ID_TYPE can_tagged_qng;
499         ASC_SCSI_BIT_ID_TYPE cmd_qng_enabled;
500         ASC_SCSI_BIT_ID_TYPE disc_enable;
501         ASC_SCSI_BIT_ID_TYPE sdtr_enable;
502         uchar chip_scsi_id;
503         uchar isa_dma_speed;
504         uchar isa_dma_channel;
505         uchar chip_version;
506         ushort mcode_date;
507         ushort mcode_version;
508         uchar max_tag_qng[ASC_MAX_TID + 1];
509         uchar sdtr_period_offset[ASC_MAX_TID + 1];
510         uchar adapter_info[6];
511 } ASC_DVC_CFG;
512
513 #define ASC_DEF_DVC_CNTL       0xFFFF
514 #define ASC_DEF_CHIP_SCSI_ID   7
515 #define ASC_DEF_ISA_DMA_SPEED  4
516 #define ASC_INIT_STATE_BEG_GET_CFG   0x0001
517 #define ASC_INIT_STATE_END_GET_CFG   0x0002
518 #define ASC_INIT_STATE_BEG_SET_CFG   0x0004
519 #define ASC_INIT_STATE_END_SET_CFG   0x0008
520 #define ASC_INIT_STATE_BEG_LOAD_MC   0x0010
521 #define ASC_INIT_STATE_END_LOAD_MC   0x0020
522 #define ASC_INIT_STATE_BEG_INQUIRY   0x0040
523 #define ASC_INIT_STATE_END_INQUIRY   0x0080
524 #define ASC_INIT_RESET_SCSI_DONE     0x0100
525 #define ASC_INIT_STATE_WITHOUT_EEP   0x8000
526 #define ASC_BUG_FIX_IF_NOT_DWB       0x0001
527 #define ASC_BUG_FIX_ASYN_USE_SYN     0x0002
528 #define ASC_MIN_TAGGED_CMD  7
529 #define ASC_MAX_SCSI_RESET_WAIT      30
530 #define ASC_OVERRUN_BSIZE               64
531
532 struct asc_dvc_var;             /* Forward Declaration. */
533
534 typedef struct asc_dvc_var {
535         PortAddr iop_base;
536         ushort err_code;
537         ushort dvc_cntl;
538         ushort bug_fix_cntl;
539         ushort bus_type;
540         ASC_SCSI_BIT_ID_TYPE init_sdtr;
541         ASC_SCSI_BIT_ID_TYPE sdtr_done;
542         ASC_SCSI_BIT_ID_TYPE use_tagged_qng;
543         ASC_SCSI_BIT_ID_TYPE unit_not_ready;
544         ASC_SCSI_BIT_ID_TYPE queue_full_or_busy;
545         ASC_SCSI_BIT_ID_TYPE start_motor;
546         uchar *overrun_buf;
547         dma_addr_t overrun_dma;
548         uchar scsi_reset_wait;
549         uchar chip_no;
550         bool is_in_int;
551         uchar max_total_qng;
552         uchar cur_total_qng;
553         uchar in_critical_cnt;
554         uchar last_q_shortage;
555         ushort init_state;
556         uchar cur_dvc_qng[ASC_MAX_TID + 1];
557         uchar max_dvc_qng[ASC_MAX_TID + 1];
558         ASC_SCSI_Q *scsiq_busy_head[ASC_MAX_TID + 1];
559         ASC_SCSI_Q *scsiq_busy_tail[ASC_MAX_TID + 1];
560         const uchar *sdtr_period_tbl;
561         ASC_DVC_CFG *cfg;
562         ASC_SCSI_BIT_ID_TYPE pci_fix_asyn_xfer_always;
563         char redo_scam;
564         ushort res2;
565         uchar dos_int13_table[ASC_MAX_TID + 1];
566         unsigned int max_dma_count;
567         ASC_SCSI_BIT_ID_TYPE no_scam;
568         ASC_SCSI_BIT_ID_TYPE pci_fix_asyn_xfer;
569         uchar min_sdtr_index;
570         uchar max_sdtr_index;
571         struct asc_board *drv_ptr;
572         unsigned int uc_break;
573 } ASC_DVC_VAR;
574
575 typedef struct asc_dvc_inq_info {
576         uchar type[ASC_MAX_TID + 1][ASC_MAX_LUN + 1];
577 } ASC_DVC_INQ_INFO;
578
579 typedef struct asc_cap_info {
580         u32 lba;
581         u32 blk_size;
582 } ASC_CAP_INFO;
583
584 typedef struct asc_cap_info_array {
585         ASC_CAP_INFO cap_info[ASC_MAX_TID + 1][ASC_MAX_LUN + 1];
586 } ASC_CAP_INFO_ARRAY;
587
588 #define ASC_MCNTL_NO_SEL_TIMEOUT  (ushort)0x0001
589 #define ASC_MCNTL_NULL_TARGET     (ushort)0x0002
590 #define ASC_CNTL_INITIATOR         (ushort)0x0001
591 #define ASC_CNTL_BIOS_GT_1GB       (ushort)0x0002
592 #define ASC_CNTL_BIOS_GT_2_DISK    (ushort)0x0004
593 #define ASC_CNTL_BIOS_REMOVABLE    (ushort)0x0008
594 #define ASC_CNTL_NO_SCAM           (ushort)0x0010
595 #define ASC_CNTL_INT_MULTI_Q       (ushort)0x0080
596 #define ASC_CNTL_NO_LUN_SUPPORT    (ushort)0x0040
597 #define ASC_CNTL_NO_VERIFY_COPY    (ushort)0x0100
598 #define ASC_CNTL_RESET_SCSI        (ushort)0x0200
599 #define ASC_CNTL_INIT_INQUIRY      (ushort)0x0400
600 #define ASC_CNTL_INIT_VERBOSE      (ushort)0x0800
601 #define ASC_CNTL_SCSI_PARITY       (ushort)0x1000
602 #define ASC_CNTL_BURST_MODE        (ushort)0x2000
603 #define ASC_CNTL_SDTR_ENABLE_ULTRA (ushort)0x4000
604 #define ASC_EEP_DVC_CFG_BEG_VL    2
605 #define ASC_EEP_MAX_DVC_ADDR_VL   15
606 #define ASC_EEP_DVC_CFG_BEG      32
607 #define ASC_EEP_MAX_DVC_ADDR     45
608 #define ASC_EEP_MAX_RETRY        20
609
610 /*
611  * These macros keep the chip SCSI id and ISA DMA speed
612  * bitfields in board order. C bitfields aren't portable
613  * between big and little-endian platforms so they are
614  * not used.
615  */
616
617 #define ASC_EEP_GET_CHIP_ID(cfg)    ((cfg)->id_speed & 0x0f)
618 #define ASC_EEP_GET_DMA_SPD(cfg)    (((cfg)->id_speed & 0xf0) >> 4)
619 #define ASC_EEP_SET_CHIP_ID(cfg, sid) \
620    ((cfg)->id_speed = ((cfg)->id_speed & 0xf0) | ((sid) & ASC_MAX_TID))
621 #define ASC_EEP_SET_DMA_SPD(cfg, spd) \
622    ((cfg)->id_speed = ((cfg)->id_speed & 0x0f) | ((spd) & 0x0f) << 4)
623
624 typedef struct asceep_config {
625         ushort cfg_lsw;
626         ushort cfg_msw;
627         uchar init_sdtr;
628         uchar disc_enable;
629         uchar use_cmd_qng;
630         uchar start_motor;
631         uchar max_total_qng;
632         uchar max_tag_qng;
633         uchar bios_scan;
634         uchar power_up_wait;
635         uchar no_scam;
636         uchar id_speed;         /* low order 4 bits is chip scsi id */
637         /* high order 4 bits is isa dma speed */
638         uchar dos_int13_table[ASC_MAX_TID + 1];
639         uchar adapter_info[6];
640         ushort cntl;
641         ushort chksum;
642 } ASCEEP_CONFIG;
643
644 #define ASC_EEP_CMD_READ          0x80
645 #define ASC_EEP_CMD_WRITE         0x40
646 #define ASC_EEP_CMD_WRITE_ABLE    0x30
647 #define ASC_EEP_CMD_WRITE_DISABLE 0x00
648 #define ASCV_MSGOUT_BEG         0x0000
649 #define ASCV_MSGOUT_SDTR_PERIOD (ASCV_MSGOUT_BEG+3)
650 #define ASCV_MSGOUT_SDTR_OFFSET (ASCV_MSGOUT_BEG+4)
651 #define ASCV_BREAK_SAVED_CODE   (ushort)0x0006
652 #define ASCV_MSGIN_BEG          (ASCV_MSGOUT_BEG+8)
653 #define ASCV_MSGIN_SDTR_PERIOD  (ASCV_MSGIN_BEG+3)
654 #define ASCV_MSGIN_SDTR_OFFSET  (ASCV_MSGIN_BEG+4)
655 #define ASCV_SDTR_DATA_BEG      (ASCV_MSGIN_BEG+8)
656 #define ASCV_SDTR_DONE_BEG      (ASCV_SDTR_DATA_BEG+8)
657 #define ASCV_MAX_DVC_QNG_BEG    (ushort)0x0020
658 #define ASCV_BREAK_ADDR           (ushort)0x0028
659 #define ASCV_BREAK_NOTIFY_COUNT   (ushort)0x002A
660 #define ASCV_BREAK_CONTROL        (ushort)0x002C
661 #define ASCV_BREAK_HIT_COUNT      (ushort)0x002E
662
663 #define ASCV_ASCDVC_ERR_CODE_W  (ushort)0x0030
664 #define ASCV_MCODE_CHKSUM_W   (ushort)0x0032
665 #define ASCV_MCODE_SIZE_W     (ushort)0x0034
666 #define ASCV_STOP_CODE_B      (ushort)0x0036
667 #define ASCV_DVC_ERR_CODE_B   (ushort)0x0037
668 #define ASCV_OVERRUN_PADDR_D  (ushort)0x0038
669 #define ASCV_OVERRUN_BSIZE_D  (ushort)0x003C
670 #define ASCV_HALTCODE_W       (ushort)0x0040
671 #define ASCV_CHKSUM_W         (ushort)0x0042
672 #define ASCV_MC_DATE_W        (ushort)0x0044
673 #define ASCV_MC_VER_W         (ushort)0x0046
674 #define ASCV_NEXTRDY_B        (ushort)0x0048
675 #define ASCV_DONENEXT_B       (ushort)0x0049
676 #define ASCV_USE_TAGGED_QNG_B (ushort)0x004A
677 #define ASCV_SCSIBUSY_B       (ushort)0x004B
678 #define ASCV_Q_DONE_IN_PROGRESS_B  (ushort)0x004C
679 #define ASCV_CURCDB_B         (ushort)0x004D
680 #define ASCV_RCLUN_B          (ushort)0x004E
681 #define ASCV_BUSY_QHEAD_B     (ushort)0x004F
682 #define ASCV_DISC1_QHEAD_B    (ushort)0x0050
683 #define ASCV_DISC_ENABLE_B    (ushort)0x0052
684 #define ASCV_CAN_TAGGED_QNG_B (ushort)0x0053
685 #define ASCV_HOSTSCSI_ID_B    (ushort)0x0055
686 #define ASCV_MCODE_CNTL_B     (ushort)0x0056
687 #define ASCV_NULL_TARGET_B    (ushort)0x0057
688 #define ASCV_FREE_Q_HEAD_W    (ushort)0x0058
689 #define ASCV_DONE_Q_TAIL_W    (ushort)0x005A
690 #define ASCV_FREE_Q_HEAD_B    (ushort)(ASCV_FREE_Q_HEAD_W+1)
691 #define ASCV_DONE_Q_TAIL_B    (ushort)(ASCV_DONE_Q_TAIL_W+1)
692 #define ASCV_HOST_FLAG_B      (ushort)0x005D
693 #define ASCV_TOTAL_READY_Q_B  (ushort)0x0064
694 #define ASCV_VER_SERIAL_B     (ushort)0x0065
695 #define ASCV_HALTCODE_SAVED_W (ushort)0x0066
696 #define ASCV_WTM_FLAG_B       (ushort)0x0068
697 #define ASCV_RISC_FLAG_B      (ushort)0x006A
698 #define ASCV_REQ_SG_LIST_QP   (ushort)0x006B
699 #define ASC_HOST_FLAG_IN_ISR        0x01
700 #define ASC_HOST_FLAG_ACK_INT       0x02
701 #define ASC_RISC_FLAG_GEN_INT      0x01
702 #define ASC_RISC_FLAG_REQ_SG_LIST  0x02
703 #define IOP_CTRL         (0x0F)
704 #define IOP_STATUS       (0x0E)
705 #define IOP_INT_ACK      IOP_STATUS
706 #define IOP_REG_IFC      (0x0D)
707 #define IOP_SYN_OFFSET    (0x0B)
708 #define IOP_EXTRA_CONTROL (0x0D)
709 #define IOP_REG_PC        (0x0C)
710 #define IOP_RAM_ADDR      (0x0A)
711 #define IOP_RAM_DATA      (0x08)
712 #define IOP_EEP_DATA      (0x06)
713 #define IOP_EEP_CMD       (0x07)
714 #define IOP_VERSION       (0x03)
715 #define IOP_CONFIG_HIGH   (0x04)
716 #define IOP_CONFIG_LOW    (0x02)
717 #define IOP_SIG_BYTE      (0x01)
718 #define IOP_SIG_WORD      (0x00)
719 #define IOP_REG_DC1      (0x0E)
720 #define IOP_REG_DC0      (0x0C)
721 #define IOP_REG_SB       (0x0B)
722 #define IOP_REG_DA1      (0x0A)
723 #define IOP_REG_DA0      (0x08)
724 #define IOP_REG_SC       (0x09)
725 #define IOP_DMA_SPEED    (0x07)
726 #define IOP_REG_FLAG     (0x07)
727 #define IOP_FIFO_H       (0x06)
728 #define IOP_FIFO_L       (0x04)
729 #define IOP_REG_ID       (0x05)
730 #define IOP_REG_QP       (0x03)
731 #define IOP_REG_IH       (0x02)
732 #define IOP_REG_IX       (0x01)
733 #define IOP_REG_AX       (0x00)
734 #define IFC_REG_LOCK      (0x00)
735 #define IFC_REG_UNLOCK    (0x09)
736 #define IFC_WR_EN_FILTER  (0x10)
737 #define IFC_RD_NO_EEPROM  (0x10)
738 #define IFC_SLEW_RATE     (0x20)
739 #define IFC_ACT_NEG       (0x40)
740 #define IFC_INP_FILTER    (0x80)
741 #define IFC_INIT_DEFAULT  (IFC_ACT_NEG | IFC_REG_UNLOCK)
742 #define SC_SEL   (uchar)(0x80)
743 #define SC_BSY   (uchar)(0x40)
744 #define SC_ACK   (uchar)(0x20)
745 #define SC_REQ   (uchar)(0x10)
746 #define SC_ATN   (uchar)(0x08)
747 #define SC_IO    (uchar)(0x04)
748 #define SC_CD    (uchar)(0x02)
749 #define SC_MSG   (uchar)(0x01)
750 #define SEC_SCSI_CTL         (uchar)(0x80)
751 #define SEC_ACTIVE_NEGATE    (uchar)(0x40)
752 #define SEC_SLEW_RATE        (uchar)(0x20)
753 #define SEC_ENABLE_FILTER    (uchar)(0x10)
754 #define ASC_HALT_EXTMSG_IN     (ushort)0x8000
755 #define ASC_HALT_CHK_CONDITION (ushort)0x8100
756 #define ASC_HALT_SS_QUEUE_FULL (ushort)0x8200
757 #define ASC_HALT_DISABLE_ASYN_USE_SYN_FIX  (ushort)0x8300
758 #define ASC_HALT_ENABLE_ASYN_USE_SYN_FIX   (ushort)0x8400
759 #define ASC_HALT_SDTR_REJECTED (ushort)0x4000
760 #define ASC_HALT_HOST_COPY_SG_LIST_TO_RISC ( ushort )0x2000
761 #define ASC_MAX_QNO        0xF8
762 #define ASC_DATA_SEC_BEG   (ushort)0x0080
763 #define ASC_DATA_SEC_END   (ushort)0x0080
764 #define ASC_CODE_SEC_BEG   (ushort)0x0080
765 #define ASC_CODE_SEC_END   (ushort)0x0080
766 #define ASC_QADR_BEG       (0x4000)
767 #define ASC_QADR_USED      (ushort)(ASC_MAX_QNO * 64)
768 #define ASC_QADR_END       (ushort)0x7FFF
769 #define ASC_QLAST_ADR      (ushort)0x7FC0
770 #define ASC_QBLK_SIZE      0x40
771 #define ASC_BIOS_DATA_QBEG 0xF8
772 #define ASC_MIN_ACTIVE_QNO 0x01
773 #define ASC_QLINK_END      0xFF
774 #define ASC_EEPROM_WORDS   0x10
775 #define ASC_MAX_MGS_LEN    0x10
776 #define ASC_BIOS_ADDR_DEF  0xDC00
777 #define ASC_BIOS_SIZE      0x3800
778 #define ASC_BIOS_RAM_OFF   0x3800
779 #define ASC_BIOS_RAM_SIZE  0x800
780 #define ASC_BIOS_MIN_ADDR  0xC000
781 #define ASC_BIOS_MAX_ADDR  0xEC00
782 #define ASC_BIOS_BANK_SIZE 0x0400
783 #define ASC_MCODE_START_ADDR  0x0080
784 #define ASC_CFG0_HOST_INT_ON    0x0020
785 #define ASC_CFG0_BIOS_ON        0x0040
786 #define ASC_CFG0_VERA_BURST_ON  0x0080
787 #define ASC_CFG0_SCSI_PARITY_ON 0x0800
788 #define ASC_CFG1_SCSI_TARGET_ON 0x0080
789 #define ASC_CFG1_LRAM_8BITS_ON  0x0800
790 #define ASC_CFG_MSW_CLR_MASK    0x3080
791 #define CSW_TEST1             (ASC_CS_TYPE)0x8000
792 #define CSW_AUTO_CONFIG       (ASC_CS_TYPE)0x4000
793 #define CSW_RESERVED1         (ASC_CS_TYPE)0x2000
794 #define CSW_IRQ_WRITTEN       (ASC_CS_TYPE)0x1000
795 #define CSW_33MHZ_SELECTED    (ASC_CS_TYPE)0x0800
796 #define CSW_TEST2             (ASC_CS_TYPE)0x0400
797 #define CSW_TEST3             (ASC_CS_TYPE)0x0200
798 #define CSW_RESERVED2         (ASC_CS_TYPE)0x0100
799 #define CSW_DMA_DONE          (ASC_CS_TYPE)0x0080
800 #define CSW_FIFO_RDY          (ASC_CS_TYPE)0x0040
801 #define CSW_EEP_READ_DONE     (ASC_CS_TYPE)0x0020
802 #define CSW_HALTED            (ASC_CS_TYPE)0x0010
803 #define CSW_SCSI_RESET_ACTIVE (ASC_CS_TYPE)0x0008
804 #define CSW_PARITY_ERR        (ASC_CS_TYPE)0x0004
805 #define CSW_SCSI_RESET_LATCH  (ASC_CS_TYPE)0x0002
806 #define CSW_INT_PENDING       (ASC_CS_TYPE)0x0001
807 #define CIW_CLR_SCSI_RESET_INT (ASC_CS_TYPE)0x1000
808 #define CIW_INT_ACK      (ASC_CS_TYPE)0x0100
809 #define CIW_TEST1        (ASC_CS_TYPE)0x0200
810 #define CIW_TEST2        (ASC_CS_TYPE)0x0400
811 #define CIW_SEL_33MHZ    (ASC_CS_TYPE)0x0800
812 #define CIW_IRQ_ACT      (ASC_CS_TYPE)0x1000
813 #define CC_CHIP_RESET   (uchar)0x80
814 #define CC_SCSI_RESET   (uchar)0x40
815 #define CC_HALT         (uchar)0x20
816 #define CC_SINGLE_STEP  (uchar)0x10
817 #define CC_DMA_ABLE     (uchar)0x08
818 #define CC_TEST         (uchar)0x04
819 #define CC_BANK_ONE     (uchar)0x02
820 #define CC_DIAG         (uchar)0x01
821 #define ASC_1000_ID0W      0x04C1
822 #define ASC_1000_ID0W_FIX  0x00C1
823 #define ASC_1000_ID1B      0x25
824 #define ASC_EISA_REV_IOP_MASK  (0x0C83)
825 #define ASC_EISA_CFG_IOP_MASK  (0x0C86)
826 #define ASC_GET_EISA_SLOT(iop)  (PortAddr)((iop) & 0xF000)
827 #define INS_HALTINT        (ushort)0x6281
828 #define INS_HALT           (ushort)0x6280
829 #define INS_SINT           (ushort)0x6200
830 #define INS_RFLAG_WTM      (ushort)0x7380
831 #define ASC_MC_SAVE_CODE_WSIZE  0x500
832 #define ASC_MC_SAVE_DATA_WSIZE  0x40
833
834 typedef struct asc_mc_saved {
835         ushort data[ASC_MC_SAVE_DATA_WSIZE];
836         ushort code[ASC_MC_SAVE_CODE_WSIZE];
837 } ASC_MC_SAVED;
838
839 #define AscGetQDoneInProgress(port)         AscReadLramByte((port), ASCV_Q_DONE_IN_PROGRESS_B)
840 #define AscPutQDoneInProgress(port, val)    AscWriteLramByte((port), ASCV_Q_DONE_IN_PROGRESS_B, val)
841 #define AscGetVarFreeQHead(port)            AscReadLramWord((port), ASCV_FREE_Q_HEAD_W)
842 #define AscGetVarDoneQTail(port)            AscReadLramWord((port), ASCV_DONE_Q_TAIL_W)
843 #define AscPutVarFreeQHead(port, val)       AscWriteLramWord((port), ASCV_FREE_Q_HEAD_W, val)
844 #define AscPutVarDoneQTail(port, val)       AscWriteLramWord((port), ASCV_DONE_Q_TAIL_W, val)
845 #define AscGetRiscVarFreeQHead(port)        AscReadLramByte((port), ASCV_NEXTRDY_B)
846 #define AscGetRiscVarDoneQTail(port)        AscReadLramByte((port), ASCV_DONENEXT_B)
847 #define AscPutRiscVarFreeQHead(port, val)   AscWriteLramByte((port), ASCV_NEXTRDY_B, val)
848 #define AscPutRiscVarDoneQTail(port, val)   AscWriteLramByte((port), ASCV_DONENEXT_B, val)
849 #define AscPutMCodeSDTRDoneAtID(port, id, data)  AscWriteLramByte((port), (ushort)((ushort)ASCV_SDTR_DONE_BEG+(ushort)id), (data))
850 #define AscGetMCodeSDTRDoneAtID(port, id)        AscReadLramByte((port), (ushort)((ushort)ASCV_SDTR_DONE_BEG+(ushort)id))
851 #define AscPutMCodeInitSDTRAtID(port, id, data)  AscWriteLramByte((port), (ushort)((ushort)ASCV_SDTR_DATA_BEG+(ushort)id), data)
852 #define AscGetMCodeInitSDTRAtID(port, id)        AscReadLramByte((port), (ushort)((ushort)ASCV_SDTR_DATA_BEG+(ushort)id))
853 #define AscGetChipSignatureByte(port)     (uchar)inp((port)+IOP_SIG_BYTE)
854 #define AscGetChipSignatureWord(port)     (ushort)inpw((port)+IOP_SIG_WORD)
855 #define AscGetChipVerNo(port)             (uchar)inp((port)+IOP_VERSION)
856 #define AscGetChipCfgLsw(port)            (ushort)inpw((port)+IOP_CONFIG_LOW)
857 #define AscGetChipCfgMsw(port)            (ushort)inpw((port)+IOP_CONFIG_HIGH)
858 #define AscSetChipCfgLsw(port, data)      outpw((port)+IOP_CONFIG_LOW, data)
859 #define AscSetChipCfgMsw(port, data)      outpw((port)+IOP_CONFIG_HIGH, data)
860 #define AscGetChipEEPCmd(port)            (uchar)inp((port)+IOP_EEP_CMD)
861 #define AscSetChipEEPCmd(port, data)      outp((port)+IOP_EEP_CMD, data)
862 #define AscGetChipEEPData(port)           (ushort)inpw((port)+IOP_EEP_DATA)
863 #define AscSetChipEEPData(port, data)     outpw((port)+IOP_EEP_DATA, data)
864 #define AscGetChipLramAddr(port)          (ushort)inpw((PortAddr)((port)+IOP_RAM_ADDR))
865 #define AscSetChipLramAddr(port, addr)    outpw((PortAddr)((port)+IOP_RAM_ADDR), addr)
866 #define AscGetChipLramData(port)          (ushort)inpw((port)+IOP_RAM_DATA)
867 #define AscSetChipLramData(port, data)    outpw((port)+IOP_RAM_DATA, data)
868 #define AscGetChipIFC(port)               (uchar)inp((port)+IOP_REG_IFC)
869 #define AscSetChipIFC(port, data)          outp((port)+IOP_REG_IFC, data)
870 #define AscGetChipStatus(port)            (ASC_CS_TYPE)inpw((port)+IOP_STATUS)
871 #define AscSetChipStatus(port, cs_val)    outpw((port)+IOP_STATUS, cs_val)
872 #define AscGetChipControl(port)           (uchar)inp((port)+IOP_CTRL)
873 #define AscSetChipControl(port, cc_val)   outp((port)+IOP_CTRL, cc_val)
874 #define AscGetChipSyn(port)               (uchar)inp((port)+IOP_SYN_OFFSET)
875 #define AscSetChipSyn(port, data)         outp((port)+IOP_SYN_OFFSET, data)
876 #define AscSetPCAddr(port, data)          outpw((port)+IOP_REG_PC, data)
877 #define AscGetPCAddr(port)                (ushort)inpw((port)+IOP_REG_PC)
878 #define AscIsIntPending(port)             (AscGetChipStatus(port) & (CSW_INT_PENDING | CSW_SCSI_RESET_LATCH))
879 #define AscGetChipScsiID(port)            ((AscGetChipCfgLsw(port) >> 8) & ASC_MAX_TID)
880 #define AscGetExtraControl(port)          (uchar)inp((port)+IOP_EXTRA_CONTROL)
881 #define AscSetExtraControl(port, data)    outp((port)+IOP_EXTRA_CONTROL, data)
882 #define AscReadChipAX(port)               (ushort)inpw((port)+IOP_REG_AX)
883 #define AscWriteChipAX(port, data)        outpw((port)+IOP_REG_AX, data)
884 #define AscReadChipIX(port)               (uchar)inp((port)+IOP_REG_IX)
885 #define AscWriteChipIX(port, data)        outp((port)+IOP_REG_IX, data)
886 #define AscReadChipIH(port)               (ushort)inpw((port)+IOP_REG_IH)
887 #define AscWriteChipIH(port, data)        outpw((port)+IOP_REG_IH, data)
888 #define AscReadChipQP(port)               (uchar)inp((port)+IOP_REG_QP)
889 #define AscWriteChipQP(port, data)        outp((port)+IOP_REG_QP, data)
890 #define AscReadChipFIFO_L(port)           (ushort)inpw((port)+IOP_REG_FIFO_L)
891 #define AscWriteChipFIFO_L(port, data)    outpw((port)+IOP_REG_FIFO_L, data)
892 #define AscReadChipFIFO_H(port)           (ushort)inpw((port)+IOP_REG_FIFO_H)
893 #define AscWriteChipFIFO_H(port, data)    outpw((port)+IOP_REG_FIFO_H, data)
894 #define AscReadChipDmaSpeed(port)         (uchar)inp((port)+IOP_DMA_SPEED)
895 #define AscWriteChipDmaSpeed(port, data)  outp((port)+IOP_DMA_SPEED, data)
896 #define AscReadChipDA0(port)              (ushort)inpw((port)+IOP_REG_DA0)
897 #define AscWriteChipDA0(port)             outpw((port)+IOP_REG_DA0, data)
898 #define AscReadChipDA1(port)              (ushort)inpw((port)+IOP_REG_DA1)
899 #define AscWriteChipDA1(port)             outpw((port)+IOP_REG_DA1, data)
900 #define AscReadChipDC0(port)              (ushort)inpw((port)+IOP_REG_DC0)
901 #define AscWriteChipDC0(port)             outpw((port)+IOP_REG_DC0, data)
902 #define AscReadChipDC1(port)              (ushort)inpw((port)+IOP_REG_DC1)
903 #define AscWriteChipDC1(port)             outpw((port)+IOP_REG_DC1, data)
904 #define AscReadChipDvcID(port)            (uchar)inp((port)+IOP_REG_ID)
905 #define AscWriteChipDvcID(port, data)     outp((port)+IOP_REG_ID, data)
906
907 /*
908  * These macros are used to convert a virtual address to a
909  * 32-bit value. This currently can be used on Linux Alpha
910  * which uses 64-bit virtual address but a 32-bit bus address.
911  * This is likely to break in the future, but doing this now
912  * will give us time to change the HW and FW to handle 64-bit
913  * addresses.
914  */
915 #define ADV_U32_TO_VADDR   bus_to_virt
916
917 #define AdvPortAddr  void __iomem *     /* Virtual memory address size */
918
919 /*
920  * Define Adv Library required memory access macros.
921  */
922 #define ADV_MEM_READB(addr) readb(addr)
923 #define ADV_MEM_READW(addr) readw(addr)
924 #define ADV_MEM_WRITEB(addr, byte) writeb(byte, addr)
925 #define ADV_MEM_WRITEW(addr, word) writew(word, addr)
926 #define ADV_MEM_WRITEDW(addr, dword) writel(dword, addr)
927
928 /*
929  * Define total number of simultaneous maximum element scatter-gather
930  * request blocks per wide adapter. ASC_DEF_MAX_HOST_QNG (253) is the
931  * maximum number of outstanding commands per wide host adapter. Each
932  * command uses one or more ADV_SG_BLOCK each with 15 scatter-gather
933  * elements. Allow each command to have at least one ADV_SG_BLOCK structure.
934  * This allows about 15 commands to have the maximum 17 ADV_SG_BLOCK
935  * structures or 255 scatter-gather elements.
936  */
937 #define ADV_TOT_SG_BLOCK        ASC_DEF_MAX_HOST_QNG
938
939 /*
940  * Define maximum number of scatter-gather elements per request.
941  */
942 #define ADV_MAX_SG_LIST         255
943 #define NO_OF_SG_PER_BLOCK              15
944
945 #define ADV_EEP_DVC_CFG_BEGIN           (0x00)
946 #define ADV_EEP_DVC_CFG_END             (0x15)
947 #define ADV_EEP_DVC_CTL_BEGIN           (0x16)  /* location of OEM name */
948 #define ADV_EEP_MAX_WORD_ADDR           (0x1E)
949
950 #define ADV_EEP_DELAY_MS                100
951
952 #define ADV_EEPROM_BIG_ENDIAN          0x8000   /* EEPROM Bit 15 */
953 #define ADV_EEPROM_BIOS_ENABLE         0x4000   /* EEPROM Bit 14 */
954 /*
955  * For the ASC3550 Bit 13 is Termination Polarity control bit.
956  * For later ICs Bit 13 controls whether the CIS (Card Information
957  * Service Section) is loaded from EEPROM.
958  */
959 #define ADV_EEPROM_TERM_POL            0x2000   /* EEPROM Bit 13 */
960 #define ADV_EEPROM_CIS_LD              0x2000   /* EEPROM Bit 13 */
961 /*
962  * ASC38C1600 Bit 11
963  *
964  * If EEPROM Bit 11 is 0 for Function 0, then Function 0 will specify
965  * INT A in the PCI Configuration Space Int Pin field. If it is 1, then
966  * Function 0 will specify INT B.
967  *
968  * If EEPROM Bit 11 is 0 for Function 1, then Function 1 will specify
969  * INT B in the PCI Configuration Space Int Pin field. If it is 1, then
970  * Function 1 will specify INT A.
971  */
972 #define ADV_EEPROM_INTAB               0x0800   /* EEPROM Bit 11 */
973
974 typedef struct adveep_3550_config {
975         /* Word Offset, Description */
976
977         ushort cfg_lsw;         /* 00 power up initialization */
978         /*  bit 13 set - Term Polarity Control */
979         /*  bit 14 set - BIOS Enable */
980         /*  bit 15 set - Big Endian Mode */
981         ushort cfg_msw;         /* 01 unused      */
982         ushort disc_enable;     /* 02 disconnect enable */
983         ushort wdtr_able;       /* 03 Wide DTR able */
984         ushort sdtr_able;       /* 04 Synchronous DTR able */
985         ushort start_motor;     /* 05 send start up motor */
986         ushort tagqng_able;     /* 06 tag queuing able */
987         ushort bios_scan;       /* 07 BIOS device control */
988         ushort scam_tolerant;   /* 08 no scam */
989
990         uchar adapter_scsi_id;  /* 09 Host Adapter ID */
991         uchar bios_boot_delay;  /*    power up wait */
992
993         uchar scsi_reset_delay; /* 10 reset delay */
994         uchar bios_id_lun;      /*    first boot device scsi id & lun */
995         /*    high nibble is lun */
996         /*    low nibble is scsi id */
997
998         uchar termination;      /* 11 0 - automatic */
999         /*    1 - low off / high off */
1000         /*    2 - low off / high on */
1001         /*    3 - low on  / high on */
1002         /*    There is no low on  / high off */
1003
1004         uchar reserved1;        /*    reserved byte (not used) */
1005
1006         ushort bios_ctrl;       /* 12 BIOS control bits */
1007         /*  bit 0  BIOS don't act as initiator. */
1008         /*  bit 1  BIOS > 1 GB support */
1009         /*  bit 2  BIOS > 2 Disk Support */
1010         /*  bit 3  BIOS don't support removables */
1011         /*  bit 4  BIOS support bootable CD */
1012         /*  bit 5  BIOS scan enabled */
1013         /*  bit 6  BIOS support multiple LUNs */
1014         /*  bit 7  BIOS display of message */
1015         /*  bit 8  SCAM disabled */
1016         /*  bit 9  Reset SCSI bus during init. */
1017         /*  bit 10 */
1018         /*  bit 11 No verbose initialization. */
1019         /*  bit 12 SCSI parity enabled */
1020         /*  bit 13 */
1021         /*  bit 14 */
1022         /*  bit 15 */
1023         ushort ultra_able;      /* 13 ULTRA speed able */
1024         ushort reserved2;       /* 14 reserved */
1025         uchar max_host_qng;     /* 15 maximum host queuing */
1026         uchar max_dvc_qng;      /*    maximum per device queuing */
1027         ushort dvc_cntl;        /* 16 control bit for driver */
1028         ushort bug_fix;         /* 17 control bit for bug fix */
1029         ushort serial_number_word1;     /* 18 Board serial number word 1 */
1030         ushort serial_number_word2;     /* 19 Board serial number word 2 */
1031         ushort serial_number_word3;     /* 20 Board serial number word 3 */
1032         ushort check_sum;       /* 21 EEP check sum */
1033         uchar oem_name[16];     /* 22 OEM name */
1034         ushort dvc_err_code;    /* 30 last device driver error code */
1035         ushort adv_err_code;    /* 31 last uc and Adv Lib error code */
1036         ushort adv_err_addr;    /* 32 last uc error address */
1037         ushort saved_dvc_err_code;      /* 33 saved last dev. driver error code   */
1038         ushort saved_adv_err_code;      /* 34 saved last uc and Adv Lib error code */
1039         ushort saved_adv_err_addr;      /* 35 saved last uc error address         */
1040         ushort num_of_err;      /* 36 number of error */
1041 } ADVEEP_3550_CONFIG;
1042
1043 typedef struct adveep_38C0800_config {
1044         /* Word Offset, Description */
1045
1046         ushort cfg_lsw;         /* 00 power up initialization */
1047         /*  bit 13 set - Load CIS */
1048         /*  bit 14 set - BIOS Enable */
1049         /*  bit 15 set - Big Endian Mode */
1050         ushort cfg_msw;         /* 01 unused      */
1051         ushort disc_enable;     /* 02 disconnect enable */
1052         ushort wdtr_able;       /* 03 Wide DTR able */
1053         ushort sdtr_speed1;     /* 04 SDTR Speed TID 0-3 */
1054         ushort start_motor;     /* 05 send start up motor */
1055         ushort tagqng_able;     /* 06 tag queuing able */
1056         ushort bios_scan;       /* 07 BIOS device control */
1057         ushort scam_tolerant;   /* 08 no scam */
1058
1059         uchar adapter_scsi_id;  /* 09 Host Adapter ID */
1060         uchar bios_boot_delay;  /*    power up wait */
1061
1062         uchar scsi_reset_delay; /* 10 reset delay */
1063         uchar bios_id_lun;      /*    first boot device scsi id & lun */
1064         /*    high nibble is lun */
1065         /*    low nibble is scsi id */
1066
1067         uchar termination_se;   /* 11 0 - automatic */
1068         /*    1 - low off / high off */
1069         /*    2 - low off / high on */
1070         /*    3 - low on  / high on */
1071         /*    There is no low on  / high off */
1072
1073         uchar termination_lvd;  /* 11 0 - automatic */
1074         /*    1 - low off / high off */
1075         /*    2 - low off / high on */
1076         /*    3 - low on  / high on */
1077         /*    There is no low on  / high off */
1078
1079         ushort bios_ctrl;       /* 12 BIOS control bits */
1080         /*  bit 0  BIOS don't act as initiator. */
1081         /*  bit 1  BIOS > 1 GB support */
1082         /*  bit 2  BIOS > 2 Disk Support */
1083         /*  bit 3  BIOS don't support removables */
1084         /*  bit 4  BIOS support bootable CD */
1085         /*  bit 5  BIOS scan enabled */
1086         /*  bit 6  BIOS support multiple LUNs */
1087         /*  bit 7  BIOS display of message */
1088         /*  bit 8  SCAM disabled */
1089         /*  bit 9  Reset SCSI bus during init. */
1090         /*  bit 10 */
1091         /*  bit 11 No verbose initialization. */
1092         /*  bit 12 SCSI parity enabled */
1093         /*  bit 13 */
1094         /*  bit 14 */
1095         /*  bit 15 */
1096         ushort sdtr_speed2;     /* 13 SDTR speed TID 4-7 */
1097         ushort sdtr_speed3;     /* 14 SDTR speed TID 8-11 */
1098         uchar max_host_qng;     /* 15 maximum host queueing */
1099         uchar max_dvc_qng;      /*    maximum per device queuing */
1100         ushort dvc_cntl;        /* 16 control bit for driver */
1101         ushort sdtr_speed4;     /* 17 SDTR speed 4 TID 12-15 */
1102         ushort serial_number_word1;     /* 18 Board serial number word 1 */
1103         ushort serial_number_word2;     /* 19 Board serial number word 2 */
1104         ushort serial_number_word3;     /* 20 Board serial number word 3 */
1105         ushort check_sum;       /* 21 EEP check sum */
1106         uchar oem_name[16];     /* 22 OEM name */
1107         ushort dvc_err_code;    /* 30 last device driver error code */
1108         ushort adv_err_code;    /* 31 last uc and Adv Lib error code */
1109         ushort adv_err_addr;    /* 32 last uc error address */
1110         ushort saved_dvc_err_code;      /* 33 saved last dev. driver error code   */
1111         ushort saved_adv_err_code;      /* 34 saved last uc and Adv Lib error code */
1112         ushort saved_adv_err_addr;      /* 35 saved last uc error address         */
1113         ushort reserved36;      /* 36 reserved */
1114         ushort reserved37;      /* 37 reserved */
1115         ushort reserved38;      /* 38 reserved */
1116         ushort reserved39;      /* 39 reserved */
1117         ushort reserved40;      /* 40 reserved */
1118         ushort reserved41;      /* 41 reserved */
1119         ushort reserved42;      /* 42 reserved */
1120         ushort reserved43;      /* 43 reserved */
1121         ushort reserved44;      /* 44 reserved */
1122         ushort reserved45;      /* 45 reserved */
1123         ushort reserved46;      /* 46 reserved */
1124         ushort reserved47;      /* 47 reserved */
1125         ushort reserved48;      /* 48 reserved */
1126         ushort reserved49;      /* 49 reserved */
1127         ushort reserved50;      /* 50 reserved */
1128         ushort reserved51;      /* 51 reserved */
1129         ushort reserved52;      /* 52 reserved */
1130         ushort reserved53;      /* 53 reserved */
1131         ushort reserved54;      /* 54 reserved */
1132         ushort reserved55;      /* 55 reserved */
1133         ushort cisptr_lsw;      /* 56 CIS PTR LSW */
1134         ushort cisprt_msw;      /* 57 CIS PTR MSW */
1135         ushort subsysvid;       /* 58 SubSystem Vendor ID */
1136         ushort subsysid;        /* 59 SubSystem ID */
1137         ushort reserved60;      /* 60 reserved */
1138         ushort reserved61;      /* 61 reserved */
1139         ushort reserved62;      /* 62 reserved */
1140         ushort reserved63;      /* 63 reserved */
1141 } ADVEEP_38C0800_CONFIG;
1142
1143 typedef struct adveep_38C1600_config {
1144         /* Word Offset, Description */
1145
1146         ushort cfg_lsw;         /* 00 power up initialization */
1147         /*  bit 11 set - Func. 0 INTB, Func. 1 INTA */
1148         /*       clear - Func. 0 INTA, Func. 1 INTB */
1149         /*  bit 13 set - Load CIS */
1150         /*  bit 14 set - BIOS Enable */
1151         /*  bit 15 set - Big Endian Mode */
1152         ushort cfg_msw;         /* 01 unused */
1153         ushort disc_enable;     /* 02 disconnect enable */
1154         ushort wdtr_able;       /* 03 Wide DTR able */
1155         ushort sdtr_speed1;     /* 04 SDTR Speed TID 0-3 */
1156         ushort start_motor;     /* 05 send start up motor */
1157         ushort tagqng_able;     /* 06 tag queuing able */
1158         ushort bios_scan;       /* 07 BIOS device control */
1159         ushort scam_tolerant;   /* 08 no scam */
1160
1161         uchar adapter_scsi_id;  /* 09 Host Adapter ID */
1162         uchar bios_boot_delay;  /*    power up wait */
1163
1164         uchar scsi_reset_delay; /* 10 reset delay */
1165         uchar bios_id_lun;      /*    first boot device scsi id & lun */
1166         /*    high nibble is lun */
1167         /*    low nibble is scsi id */
1168
1169         uchar termination_se;   /* 11 0 - automatic */
1170         /*    1 - low off / high off */
1171         /*    2 - low off / high on */
1172         /*    3 - low on  / high on */
1173         /*    There is no low on  / high off */
1174
1175         uchar termination_lvd;  /* 11 0 - automatic */
1176         /*    1 - low off / high off */
1177         /*    2 - low off / high on */
1178         /*    3 - low on  / high on */
1179         /*    There is no low on  / high off */
1180
1181         ushort bios_ctrl;       /* 12 BIOS control bits */
1182         /*  bit 0  BIOS don't act as initiator. */
1183         /*  bit 1  BIOS > 1 GB support */
1184         /*  bit 2  BIOS > 2 Disk Support */
1185         /*  bit 3  BIOS don't support removables */
1186         /*  bit 4  BIOS support bootable CD */
1187         /*  bit 5  BIOS scan enabled */
1188         /*  bit 6  BIOS support multiple LUNs */
1189         /*  bit 7  BIOS display of message */
1190         /*  bit 8  SCAM disabled */
1191         /*  bit 9  Reset SCSI bus during init. */
1192         /*  bit 10 Basic Integrity Checking disabled */
1193         /*  bit 11 No verbose initialization. */
1194         /*  bit 12 SCSI parity enabled */
1195         /*  bit 13 AIPP (Asyn. Info. Ph. Prot.) dis. */
1196         /*  bit 14 */
1197         /*  bit 15 */
1198         ushort sdtr_speed2;     /* 13 SDTR speed TID 4-7 */
1199         ushort sdtr_speed3;     /* 14 SDTR speed TID 8-11 */
1200         uchar max_host_qng;     /* 15 maximum host queueing */
1201         uchar max_dvc_qng;      /*    maximum per device queuing */
1202         ushort dvc_cntl;        /* 16 control bit for driver */
1203         ushort sdtr_speed4;     /* 17 SDTR speed 4 TID 12-15 */
1204         ushort serial_number_word1;     /* 18 Board serial number word 1 */
1205         ushort serial_number_word2;     /* 19 Board serial number word 2 */
1206         ushort serial_number_word3;     /* 20 Board serial number word 3 */
1207         ushort check_sum;       /* 21 EEP check sum */
1208         uchar oem_name[16];     /* 22 OEM name */
1209         ushort dvc_err_code;    /* 30 last device driver error code */
1210         ushort adv_err_code;    /* 31 last uc and Adv Lib error code */
1211         ushort adv_err_addr;    /* 32 last uc error address */
1212         ushort saved_dvc_err_code;      /* 33 saved last dev. driver error code   */
1213         ushort saved_adv_err_code;      /* 34 saved last uc and Adv Lib error code */
1214         ushort saved_adv_err_addr;      /* 35 saved last uc error address         */
1215         ushort reserved36;      /* 36 reserved */
1216         ushort reserved37;      /* 37 reserved */
1217         ushort reserved38;      /* 38 reserved */
1218         ushort reserved39;      /* 39 reserved */
1219         ushort reserved40;      /* 40 reserved */
1220         ushort reserved41;      /* 41 reserved */
1221         ushort reserved42;      /* 42 reserved */
1222         ushort reserved43;      /* 43 reserved */
1223         ushort reserved44;      /* 44 reserved */
1224         ushort reserved45;      /* 45 reserved */
1225         ushort reserved46;      /* 46 reserved */
1226         ushort reserved47;      /* 47 reserved */
1227         ushort reserved48;      /* 48 reserved */
1228         ushort reserved49;      /* 49 reserved */
1229         ushort reserved50;      /* 50 reserved */
1230         ushort reserved51;      /* 51 reserved */
1231         ushort reserved52;      /* 52 reserved */
1232         ushort reserved53;      /* 53 reserved */
1233         ushort reserved54;      /* 54 reserved */
1234         ushort reserved55;      /* 55 reserved */
1235         ushort cisptr_lsw;      /* 56 CIS PTR LSW */
1236         ushort cisprt_msw;      /* 57 CIS PTR MSW */
1237         ushort subsysvid;       /* 58 SubSystem Vendor ID */
1238         ushort subsysid;        /* 59 SubSystem ID */
1239         ushort reserved60;      /* 60 reserved */
1240         ushort reserved61;      /* 61 reserved */
1241         ushort reserved62;      /* 62 reserved */
1242         ushort reserved63;      /* 63 reserved */
1243 } ADVEEP_38C1600_CONFIG;
1244
1245 /*
1246  * EEPROM Commands
1247  */
1248 #define ASC_EEP_CMD_DONE             0x0200
1249
1250 /* bios_ctrl */
1251 #define BIOS_CTRL_BIOS               0x0001
1252 #define BIOS_CTRL_EXTENDED_XLAT      0x0002
1253 #define BIOS_CTRL_GT_2_DISK          0x0004
1254 #define BIOS_CTRL_BIOS_REMOVABLE     0x0008
1255 #define BIOS_CTRL_BOOTABLE_CD        0x0010
1256 #define BIOS_CTRL_MULTIPLE_LUN       0x0040
1257 #define BIOS_CTRL_DISPLAY_MSG        0x0080
1258 #define BIOS_CTRL_NO_SCAM            0x0100
1259 #define BIOS_CTRL_RESET_SCSI_BUS     0x0200
1260 #define BIOS_CTRL_INIT_VERBOSE       0x0800
1261 #define BIOS_CTRL_SCSI_PARITY        0x1000
1262 #define BIOS_CTRL_AIPP_DIS           0x2000
1263
1264 #define ADV_3550_MEMSIZE   0x2000       /* 8 KB Internal Memory */
1265
1266 #define ADV_38C0800_MEMSIZE  0x4000     /* 16 KB Internal Memory */
1267
1268 /*
1269  * XXX - Since ASC38C1600 Rev.3 has a local RAM failure issue, there is
1270  * a special 16K Adv Library and Microcode version. After the issue is
1271  * resolved, should restore 32K support.
1272  *
1273  * #define ADV_38C1600_MEMSIZE  0x8000L   * 32 KB Internal Memory *
1274  */
1275 #define ADV_38C1600_MEMSIZE  0x4000     /* 16 KB Internal Memory */
1276
1277 /*
1278  * Byte I/O register address from base of 'iop_base'.
1279  */
1280 #define IOPB_INTR_STATUS_REG    0x00
1281 #define IOPB_CHIP_ID_1          0x01
1282 #define IOPB_INTR_ENABLES       0x02
1283 #define IOPB_CHIP_TYPE_REV      0x03
1284 #define IOPB_RES_ADDR_4         0x04
1285 #define IOPB_RES_ADDR_5         0x05
1286 #define IOPB_RAM_DATA           0x06
1287 #define IOPB_RES_ADDR_7         0x07
1288 #define IOPB_FLAG_REG           0x08
1289 #define IOPB_RES_ADDR_9         0x09
1290 #define IOPB_RISC_CSR           0x0A
1291 #define IOPB_RES_ADDR_B         0x0B
1292 #define IOPB_RES_ADDR_C         0x0C
1293 #define IOPB_RES_ADDR_D         0x0D
1294 #define IOPB_SOFT_OVER_WR       0x0E
1295 #define IOPB_RES_ADDR_F         0x0F
1296 #define IOPB_MEM_CFG            0x10
1297 #define IOPB_RES_ADDR_11        0x11
1298 #define IOPB_GPIO_DATA          0x12
1299 #define IOPB_RES_ADDR_13        0x13
1300 #define IOPB_FLASH_PAGE         0x14
1301 #define IOPB_RES_ADDR_15        0x15
1302 #define IOPB_GPIO_CNTL          0x16
1303 #define IOPB_RES_ADDR_17        0x17
1304 #define IOPB_FLASH_DATA         0x18
1305 #define IOPB_RES_ADDR_19        0x19
1306 #define IOPB_RES_ADDR_1A        0x1A
1307 #define IOPB_RES_ADDR_1B        0x1B
1308 #define IOPB_RES_ADDR_1C        0x1C
1309 #define IOPB_RES_ADDR_1D        0x1D
1310 #define IOPB_RES_ADDR_1E        0x1E
1311 #define IOPB_RES_ADDR_1F        0x1F
1312 #define IOPB_DMA_CFG0           0x20
1313 #define IOPB_DMA_CFG1           0x21
1314 #define IOPB_TICKLE             0x22
1315 #define IOPB_DMA_REG_WR         0x23
1316 #define IOPB_SDMA_STATUS        0x24
1317 #define IOPB_SCSI_BYTE_CNT      0x25
1318 #define IOPB_HOST_BYTE_CNT      0x26
1319 #define IOPB_BYTE_LEFT_TO_XFER  0x27
1320 #define IOPB_BYTE_TO_XFER_0     0x28
1321 #define IOPB_BYTE_TO_XFER_1     0x29
1322 #define IOPB_BYTE_TO_XFER_2     0x2A
1323 #define IOPB_BYTE_TO_XFER_3     0x2B
1324 #define IOPB_ACC_GRP            0x2C
1325 #define IOPB_RES_ADDR_2D        0x2D
1326 #define IOPB_DEV_ID             0x2E
1327 #define IOPB_RES_ADDR_2F        0x2F
1328 #define IOPB_SCSI_DATA          0x30
1329 #define IOPB_RES_ADDR_31        0x31
1330 #define IOPB_RES_ADDR_32        0x32
1331 #define IOPB_SCSI_DATA_HSHK     0x33
1332 #define IOPB_SCSI_CTRL          0x34
1333 #define IOPB_RES_ADDR_35        0x35
1334 #define IOPB_RES_ADDR_36        0x36
1335 #define IOPB_RES_ADDR_37        0x37
1336 #define IOPB_RAM_BIST           0x38
1337 #define IOPB_PLL_TEST           0x39
1338 #define IOPB_PCI_INT_CFG        0x3A
1339 #define IOPB_RES_ADDR_3B        0x3B
1340 #define IOPB_RFIFO_CNT          0x3C
1341 #define IOPB_RES_ADDR_3D        0x3D
1342 #define IOPB_RES_ADDR_3E        0x3E
1343 #define IOPB_RES_ADDR_3F        0x3F
1344
1345 /*
1346  * Word I/O register address from base of 'iop_base'.
1347  */
1348 #define IOPW_CHIP_ID_0          0x00    /* CID0  */
1349 #define IOPW_CTRL_REG           0x02    /* CC    */
1350 #define IOPW_RAM_ADDR           0x04    /* LA    */
1351 #define IOPW_RAM_DATA           0x06    /* LD    */
1352 #define IOPW_RES_ADDR_08        0x08
1353 #define IOPW_RISC_CSR           0x0A    /* CSR   */
1354 #define IOPW_SCSI_CFG0          0x0C    /* CFG0  */
1355 #define IOPW_SCSI_CFG1          0x0E    /* CFG1  */
1356 #define IOPW_RES_ADDR_10        0x10
1357 #define IOPW_SEL_MASK           0x12    /* SM    */
1358 #define IOPW_RES_ADDR_14        0x14
1359 #define IOPW_FLASH_ADDR         0x16    /* FA    */
1360 #define IOPW_RES_ADDR_18        0x18
1361 #define IOPW_EE_CMD             0x1A    /* EC    */
1362 #define IOPW_EE_DATA            0x1C    /* ED    */
1363 #define IOPW_SFIFO_CNT          0x1E    /* SFC   */
1364 #define IOPW_RES_ADDR_20        0x20
1365 #define IOPW_Q_BASE             0x22    /* QB    */
1366 #define IOPW_QP                 0x24    /* QP    */
1367 #define IOPW_IX                 0x26    /* IX    */
1368 #define IOPW_SP                 0x28    /* SP    */
1369 #define IOPW_PC                 0x2A    /* PC    */
1370 #define IOPW_RES_ADDR_2C        0x2C
1371 #define IOPW_RES_ADDR_2E        0x2E
1372 #define IOPW_SCSI_DATA          0x30    /* SD    */
1373 #define IOPW_SCSI_DATA_HSHK     0x32    /* SDH   */
1374 #define IOPW_SCSI_CTRL          0x34    /* SC    */
1375 #define IOPW_HSHK_CFG           0x36    /* HCFG  */
1376 #define IOPW_SXFR_STATUS        0x36    /* SXS   */
1377 #define IOPW_SXFR_CNTL          0x38    /* SXL   */
1378 #define IOPW_SXFR_CNTH          0x3A    /* SXH   */
1379 #define IOPW_RES_ADDR_3C        0x3C
1380 #define IOPW_RFIFO_DATA         0x3E    /* RFD   */
1381
1382 /*
1383  * Doubleword I/O register address from base of 'iop_base'.
1384  */
1385 #define IOPDW_RES_ADDR_0         0x00
1386 #define IOPDW_RAM_DATA           0x04
1387 #define IOPDW_RES_ADDR_8         0x08
1388 #define IOPDW_RES_ADDR_C         0x0C
1389 #define IOPDW_RES_ADDR_10        0x10
1390 #define IOPDW_COMMA              0x14
1391 #define IOPDW_COMMB              0x18
1392 #define IOPDW_RES_ADDR_1C        0x1C
1393 #define IOPDW_SDMA_ADDR0         0x20
1394 #define IOPDW_SDMA_ADDR1         0x24
1395 #define IOPDW_SDMA_COUNT         0x28
1396 #define IOPDW_SDMA_ERROR         0x2C
1397 #define IOPDW_RDMA_ADDR0         0x30
1398 #define IOPDW_RDMA_ADDR1         0x34
1399 #define IOPDW_RDMA_COUNT         0x38
1400 #define IOPDW_RDMA_ERROR         0x3C
1401
1402 #define ADV_CHIP_ID_BYTE         0x25
1403 #define ADV_CHIP_ID_WORD         0x04C1
1404
1405 #define ADV_INTR_ENABLE_HOST_INTR                   0x01
1406 #define ADV_INTR_ENABLE_SEL_INTR                    0x02
1407 #define ADV_INTR_ENABLE_DPR_INTR                    0x04
1408 #define ADV_INTR_ENABLE_RTA_INTR                    0x08
1409 #define ADV_INTR_ENABLE_RMA_INTR                    0x10
1410 #define ADV_INTR_ENABLE_RST_INTR                    0x20
1411 #define ADV_INTR_ENABLE_DPE_INTR                    0x40
1412 #define ADV_INTR_ENABLE_GLOBAL_INTR                 0x80
1413
1414 #define ADV_INTR_STATUS_INTRA            0x01
1415 #define ADV_INTR_STATUS_INTRB            0x02
1416 #define ADV_INTR_STATUS_INTRC            0x04
1417
1418 #define ADV_RISC_CSR_STOP           (0x0000)
1419 #define ADV_RISC_TEST_COND          (0x2000)
1420 #define ADV_RISC_CSR_RUN            (0x4000)
1421 #define ADV_RISC_CSR_SINGLE_STEP    (0x8000)
1422
1423 #define ADV_CTRL_REG_HOST_INTR      0x0100
1424 #define ADV_CTRL_REG_SEL_INTR       0x0200
1425 #define ADV_CTRL_REG_DPR_INTR       0x0400
1426 #define ADV_CTRL_REG_RTA_INTR       0x0800
1427 #define ADV_CTRL_REG_RMA_INTR       0x1000
1428 #define ADV_CTRL_REG_RES_BIT14      0x2000
1429 #define ADV_CTRL_REG_DPE_INTR       0x4000
1430 #define ADV_CTRL_REG_POWER_DONE     0x8000
1431 #define ADV_CTRL_REG_ANY_INTR       0xFF00
1432
1433 #define ADV_CTRL_REG_CMD_RESET             0x00C6
1434 #define ADV_CTRL_REG_CMD_WR_IO_REG         0x00C5
1435 #define ADV_CTRL_REG_CMD_RD_IO_REG         0x00C4
1436 #define ADV_CTRL_REG_CMD_WR_PCI_CFG_SPACE  0x00C3
1437 #define ADV_CTRL_REG_CMD_RD_PCI_CFG_SPACE  0x00C2
1438
1439 #define ADV_TICKLE_NOP                      0x00
1440 #define ADV_TICKLE_A                        0x01
1441 #define ADV_TICKLE_B                        0x02
1442 #define ADV_TICKLE_C                        0x03
1443
1444 #define AdvIsIntPending(port) \
1445     (AdvReadWordRegister(port, IOPW_CTRL_REG) & ADV_CTRL_REG_HOST_INTR)
1446
1447 /*
1448  * SCSI_CFG0 Register bit definitions
1449  */
1450 #define TIMER_MODEAB    0xC000  /* Watchdog, Second, and Select. Timer Ctrl. */
1451 #define PARITY_EN       0x2000  /* Enable SCSI Parity Error detection */
1452 #define EVEN_PARITY     0x1000  /* Select Even Parity */
1453 #define WD_LONG         0x0800  /* Watchdog Interval, 1: 57 min, 0: 13 sec */
1454 #define QUEUE_128       0x0400  /* Queue Size, 1: 128 byte, 0: 64 byte */
1455 #define PRIM_MODE       0x0100  /* Primitive SCSI mode */
1456 #define SCAM_EN         0x0080  /* Enable SCAM selection */
1457 #define SEL_TMO_LONG    0x0040  /* Sel/Resel Timeout, 1: 400 ms, 0: 1.6 ms */
1458 #define CFRM_ID         0x0020  /* SCAM id sel. confirm., 1: fast, 0: 6.4 ms */
1459 #define OUR_ID_EN       0x0010  /* Enable OUR_ID bits */
1460 #define OUR_ID          0x000F  /* SCSI ID */
1461
1462 /*
1463  * SCSI_CFG1 Register bit definitions
1464  */
1465 #define BIG_ENDIAN      0x8000  /* Enable Big Endian Mode MIO:15, EEP:15 */
1466 #define TERM_POL        0x2000  /* Terminator Polarity Ctrl. MIO:13, EEP:13 */
1467 #define SLEW_RATE       0x1000  /* SCSI output buffer slew rate */
1468 #define FILTER_SEL      0x0C00  /* Filter Period Selection */
1469 #define  FLTR_DISABLE    0x0000 /* Input Filtering Disabled */
1470 #define  FLTR_11_TO_20NS 0x0800 /* Input Filtering 11ns to 20ns */
1471 #define  FLTR_21_TO_39NS 0x0C00 /* Input Filtering 21ns to 39ns */
1472 #define ACTIVE_DBL      0x0200  /* Disable Active Negation */
1473 #define DIFF_MODE       0x0100  /* SCSI differential Mode (Read-Only) */
1474 #define DIFF_SENSE      0x0080  /* 1: No SE cables, 0: SE cable (Read-Only) */
1475 #define TERM_CTL_SEL    0x0040  /* Enable TERM_CTL_H and TERM_CTL_L */
1476 #define TERM_CTL        0x0030  /* External SCSI Termination Bits */
1477 #define  TERM_CTL_H      0x0020 /* Enable External SCSI Upper Termination */
1478 #define  TERM_CTL_L      0x0010 /* Enable External SCSI Lower Termination */
1479 #define CABLE_DETECT    0x000F  /* External SCSI Cable Connection Status */
1480
1481 /*
1482  * Addendum for ASC-38C0800 Chip
1483  *
1484  * The ASC-38C1600 Chip uses the same definitions except that the
1485  * bus mode override bits [12:10] have been moved to byte register
1486  * offset 0xE (IOPB_SOFT_OVER_WR) bits [12:10]. The [12:10] bits in
1487  * SCSI_CFG1 are read-only and always available. Bit 14 (DIS_TERM_DRV)
1488  * is not needed. The [12:10] bits in IOPB_SOFT_OVER_WR are write-only.
1489  * Also each ASC-38C1600 function or channel uses only cable bits [5:4]
1490  * and [1:0]. Bits [14], [7:6], [3:2] are unused.
1491  */
1492 #define DIS_TERM_DRV    0x4000  /* 1: Read c_det[3:0], 0: cannot read */
1493 #define HVD_LVD_SE      0x1C00  /* Device Detect Bits */
1494 #define  HVD             0x1000 /* HVD Device Detect */
1495 #define  LVD             0x0800 /* LVD Device Detect */
1496 #define  SE              0x0400 /* SE Device Detect */
1497 #define TERM_LVD        0x00C0  /* LVD Termination Bits */
1498 #define  TERM_LVD_HI     0x0080 /* Enable LVD Upper Termination */
1499 #define  TERM_LVD_LO     0x0040 /* Enable LVD Lower Termination */
1500 #define TERM_SE         0x0030  /* SE Termination Bits */
1501 #define  TERM_SE_HI      0x0020 /* Enable SE Upper Termination */
1502 #define  TERM_SE_LO      0x0010 /* Enable SE Lower Termination */
1503 #define C_DET_LVD       0x000C  /* LVD Cable Detect Bits */
1504 #define  C_DET3          0x0008 /* Cable Detect for LVD External Wide */
1505 #define  C_DET2          0x0004 /* Cable Detect for LVD Internal Wide */
1506 #define C_DET_SE        0x0003  /* SE Cable Detect Bits */
1507 #define  C_DET1          0x0002 /* Cable Detect for SE Internal Wide */
1508 #define  C_DET0          0x0001 /* Cable Detect for SE Internal Narrow */
1509
1510 #define CABLE_ILLEGAL_A 0x7
1511     /* x 0 0 0  | on  on | Illegal (all 3 connectors are used) */
1512
1513 #define CABLE_ILLEGAL_B 0xB
1514     /* 0 x 0 0  | on  on | Illegal (all 3 connectors are used) */
1515
1516 /*
1517  * MEM_CFG Register bit definitions
1518  */
1519 #define BIOS_EN         0x40    /* BIOS Enable MIO:14,EEP:14 */
1520 #define FAST_EE_CLK     0x20    /* Diagnostic Bit */
1521 #define RAM_SZ          0x1C    /* Specify size of RAM to RISC */
1522 #define  RAM_SZ_2KB      0x00   /* 2 KB */
1523 #define  RAM_SZ_4KB      0x04   /* 4 KB */
1524 #define  RAM_SZ_8KB      0x08   /* 8 KB */
1525 #define  RAM_SZ_16KB     0x0C   /* 16 KB */
1526 #define  RAM_SZ_32KB     0x10   /* 32 KB */
1527 #define  RAM_SZ_64KB     0x14   /* 64 KB */
1528
1529 /*
1530  * DMA_CFG0 Register bit definitions
1531  *
1532  * This register is only accessible to the host.
1533  */
1534 #define BC_THRESH_ENB   0x80    /* PCI DMA Start Conditions */
1535 #define FIFO_THRESH     0x70    /* PCI DMA FIFO Threshold */
1536 #define  FIFO_THRESH_16B  0x00  /* 16 bytes */
1537 #define  FIFO_THRESH_32B  0x20  /* 32 bytes */
1538 #define  FIFO_THRESH_48B  0x30  /* 48 bytes */
1539 #define  FIFO_THRESH_64B  0x40  /* 64 bytes */
1540 #define  FIFO_THRESH_80B  0x50  /* 80 bytes (default) */
1541 #define  FIFO_THRESH_96B  0x60  /* 96 bytes */
1542 #define  FIFO_THRESH_112B 0x70  /* 112 bytes */
1543 #define START_CTL       0x0C    /* DMA start conditions */
1544 #define  START_CTL_TH    0x00   /* Wait threshold level (default) */
1545 #define  START_CTL_ID    0x04   /* Wait SDMA/SBUS idle */
1546 #define  START_CTL_THID  0x08   /* Wait threshold and SDMA/SBUS idle */
1547 #define  START_CTL_EMFU  0x0C   /* Wait SDMA FIFO empty/full */
1548 #define READ_CMD        0x03    /* Memory Read Method */
1549 #define  READ_CMD_MR     0x00   /* Memory Read */
1550 #define  READ_CMD_MRL    0x02   /* Memory Read Long */
1551 #define  READ_CMD_MRM    0x03   /* Memory Read Multiple (default) */
1552
1553 /*
1554  * ASC-38C0800 RAM BIST Register bit definitions
1555  */
1556 #define RAM_TEST_MODE         0x80
1557 #define PRE_TEST_MODE         0x40
1558 #define NORMAL_MODE           0x00
1559 #define RAM_TEST_DONE         0x10
1560 #define RAM_TEST_STATUS       0x0F
1561 #define  RAM_TEST_HOST_ERROR   0x08
1562 #define  RAM_TEST_INTRAM_ERROR 0x04
1563 #define  RAM_TEST_RISC_ERROR   0x02
1564 #define  RAM_TEST_SCSI_ERROR   0x01
1565 #define  RAM_TEST_SUCCESS      0x00
1566 #define PRE_TEST_VALUE        0x05
1567 #define NORMAL_VALUE          0x00
1568
1569 /*
1570  * ASC38C1600 Definitions
1571  *
1572  * IOPB_PCI_INT_CFG Bit Field Definitions
1573  */
1574
1575 #define INTAB_LD        0x80    /* Value loaded from EEPROM Bit 11. */
1576
1577 /*
1578  * Bit 1 can be set to change the interrupt for the Function to operate in
1579  * Totem Pole mode. By default Bit 1 is 0 and the interrupt operates in
1580  * Open Drain mode. Both functions of the ASC38C1600 must be set to the same
1581  * mode, otherwise the operating mode is undefined.
1582  */
1583 #define TOTEMPOLE       0x02
1584
1585 /*
1586  * Bit 0 can be used to change the Int Pin for the Function. The value is
1587  * 0 by default for both Functions with Function 0 using INT A and Function
1588  * B using INT B. For Function 0 if set, INT B is used. For Function 1 if set,
1589  * INT A is used.
1590  *
1591  * EEPROM Word 0 Bit 11 for each Function may change the initial Int Pin
1592  * value specified in the PCI Configuration Space.
1593  */
1594 #define INTAB           0x01
1595
1596 /*
1597  * Adv Library Status Definitions
1598  */
1599 #define ADV_TRUE        1
1600 #define ADV_FALSE       0
1601 #define ADV_SUCCESS     1
1602 #define ADV_BUSY        0
1603 #define ADV_ERROR       (-1)
1604
1605 /*
1606  * ADV_DVC_VAR 'warn_code' values
1607  */
1608 #define ASC_WARN_BUSRESET_ERROR         0x0001  /* SCSI Bus Reset error */
1609 #define ASC_WARN_EEPROM_CHKSUM          0x0002  /* EEP check sum error */
1610 #define ASC_WARN_EEPROM_TERMINATION     0x0004  /* EEP termination bad field */
1611 #define ASC_WARN_ERROR                  0xFFFF  /* ADV_ERROR return */
1612
1613 #define ADV_MAX_TID                     15      /* max. target identifier */
1614 #define ADV_MAX_LUN                     7       /* max. logical unit number */
1615
1616 /*
1617  * Fixed locations of microcode operating variables.
1618  */
1619 #define ASC_MC_CODE_BEGIN_ADDR          0x0028  /* microcode start address */
1620 #define ASC_MC_CODE_END_ADDR            0x002A  /* microcode end address */
1621 #define ASC_MC_CODE_CHK_SUM             0x002C  /* microcode code checksum */
1622 #define ASC_MC_VERSION_DATE             0x0038  /* microcode version */
1623 #define ASC_MC_VERSION_NUM              0x003A  /* microcode number */
1624 #define ASC_MC_BIOSMEM                  0x0040  /* BIOS RISC Memory Start */
1625 #define ASC_MC_BIOSLEN                  0x0050  /* BIOS RISC Memory Length */
1626 #define ASC_MC_BIOS_SIGNATURE           0x0058  /* BIOS Signature 0x55AA */
1627 #define ASC_MC_BIOS_VERSION             0x005A  /* BIOS Version (2 bytes) */
1628 #define ASC_MC_SDTR_SPEED1              0x0090  /* SDTR Speed for TID 0-3 */
1629 #define ASC_MC_SDTR_SPEED2              0x0092  /* SDTR Speed for TID 4-7 */
1630 #define ASC_MC_SDTR_SPEED3              0x0094  /* SDTR Speed for TID 8-11 */
1631 #define ASC_MC_SDTR_SPEED4              0x0096  /* SDTR Speed for TID 12-15 */
1632 #define ASC_MC_CHIP_TYPE                0x009A
1633 #define ASC_MC_INTRB_CODE               0x009B
1634 #define ASC_MC_WDTR_ABLE                0x009C
1635 #define ASC_MC_SDTR_ABLE                0x009E
1636 #define ASC_MC_TAGQNG_ABLE              0x00A0
1637 #define ASC_MC_DISC_ENABLE              0x00A2
1638 #define ASC_MC_IDLE_CMD_STATUS          0x00A4
1639 #define ASC_MC_IDLE_CMD                 0x00A6
1640 #define ASC_MC_IDLE_CMD_PARAMETER       0x00A8
1641 #define ASC_MC_DEFAULT_SCSI_CFG0        0x00AC
1642 #define ASC_MC_DEFAULT_SCSI_CFG1        0x00AE
1643 #define ASC_MC_DEFAULT_MEM_CFG          0x00B0
1644 #define ASC_MC_DEFAULT_SEL_MASK         0x00B2
1645 #define ASC_MC_SDTR_DONE                0x00B6
1646 #define ASC_MC_NUMBER_OF_QUEUED_CMD     0x00C0
1647 #define ASC_MC_NUMBER_OF_MAX_CMD        0x00D0
1648 #define ASC_MC_DEVICE_HSHK_CFG_TABLE    0x0100
1649 #define ASC_MC_CONTROL_FLAG             0x0122  /* Microcode control flag. */
1650 #define ASC_MC_WDTR_DONE                0x0124
1651 #define ASC_MC_CAM_MODE_MASK            0x015E  /* CAM mode TID bitmask. */
1652 #define ASC_MC_ICQ                      0x0160
1653 #define ASC_MC_IRQ                      0x0164
1654 #define ASC_MC_PPR_ABLE                 0x017A
1655
1656 /*
1657  * BIOS LRAM variable absolute offsets.
1658  */
1659 #define BIOS_CODESEG    0x54
1660 #define BIOS_CODELEN    0x56
1661 #define BIOS_SIGNATURE  0x58
1662 #define BIOS_VERSION    0x5A
1663
1664 /*
1665  * Microcode Control Flags
1666  *
1667  * Flags set by the Adv Library in RISC variable 'control_flag' (0x122)
1668  * and handled by the microcode.
1669  */
1670 #define CONTROL_FLAG_IGNORE_PERR        0x0001  /* Ignore DMA Parity Errors */
1671 #define CONTROL_FLAG_ENABLE_AIPP        0x0002  /* Enabled AIPP checking. */
1672
1673 /*
1674  * ASC_MC_DEVICE_HSHK_CFG_TABLE microcode table or HSHK_CFG register format
1675  */
1676 #define HSHK_CFG_WIDE_XFR       0x8000
1677 #define HSHK_CFG_RATE           0x0F00
1678 #define HSHK_CFG_OFFSET         0x001F
1679
1680 #define ASC_DEF_MAX_HOST_QNG    0xFD    /* Max. number of host commands (253) */
1681 #define ASC_DEF_MIN_HOST_QNG    0x10    /* Min. number of host commands (16) */
1682 #define ASC_DEF_MAX_DVC_QNG     0x3F    /* Max. number commands per device (63) */
1683 #define ASC_DEF_MIN_DVC_QNG     0x04    /* Min. number commands per device (4) */
1684
1685 #define ASC_QC_DATA_CHECK  0x01 /* Require ASC_QC_DATA_OUT set or clear. */
1686 #define ASC_QC_DATA_OUT    0x02 /* Data out DMA transfer. */
1687 #define ASC_QC_START_MOTOR 0x04 /* Send auto-start motor before request. */
1688 #define ASC_QC_NO_OVERRUN  0x08 /* Don't report overrun. */
1689 #define ASC_QC_FREEZE_TIDQ 0x10 /* Freeze TID queue after request. XXX TBD */
1690
1691 #define ASC_QSC_NO_DISC     0x01        /* Don't allow disconnect for request. */
1692 #define ASC_QSC_NO_TAGMSG   0x02        /* Don't allow tag queuing for request. */
1693 #define ASC_QSC_NO_SYNC     0x04        /* Don't use Synch. transfer on request. */
1694 #define ASC_QSC_NO_WIDE     0x08        /* Don't use Wide transfer on request. */
1695 #define ASC_QSC_REDO_DTR    0x10        /* Renegotiate WDTR/SDTR before request. */
1696 /*
1697  * Note: If a Tag Message is to be sent and neither ASC_QSC_HEAD_TAG or
1698  * ASC_QSC_ORDERED_TAG is set, then a Simple Tag Message (0x20) is used.
1699  */
1700 #define ASC_QSC_HEAD_TAG    0x40        /* Use Head Tag Message (0x21). */
1701 #define ASC_QSC_ORDERED_TAG 0x80        /* Use Ordered Tag Message (0x22). */
1702
1703 /*
1704  * All fields here are accessed by the board microcode and need to be
1705  * little-endian.
1706  */
1707 typedef struct adv_carr_t {
1708         __le32 carr_va; /* Carrier Virtual Address */
1709         __le32 carr_pa; /* Carrier Physical Address */
1710         __le32 areq_vpa;        /* ASC_SCSI_REQ_Q Virtual or Physical Address */
1711         /*
1712          * next_vpa [31:4]            Carrier Virtual or Physical Next Pointer
1713          *
1714          * next_vpa [3:1]             Reserved Bits
1715          * next_vpa [0]               Done Flag set in Response Queue.
1716          */
1717         __le32 next_vpa;
1718 } ADV_CARR_T;
1719
1720 /*
1721  * Mask used to eliminate low 4 bits of carrier 'next_vpa' field.
1722  */
1723 #define ASC_NEXT_VPA_MASK       0xFFFFFFF0
1724
1725 #define ASC_RQ_DONE             0x00000001
1726 #define ASC_RQ_GOOD             0x00000002
1727 #define ASC_CQ_STOPPER          0x00000000
1728
1729 #define ASC_GET_CARRP(carrp) ((carrp) & ASC_NEXT_VPA_MASK)
1730
1731 /*
1732  * Each carrier is 64 bytes, and we need three additional
1733  * carrier for icq, irq, and the termination carrier.
1734  */
1735 #define ADV_CARRIER_COUNT (ASC_DEF_MAX_HOST_QNG + 3)
1736
1737 #define ADV_CARRIER_BUFSIZE \
1738         (ADV_CARRIER_COUNT * sizeof(ADV_CARR_T))
1739
1740 /*
1741  * ASC_SCSI_REQ_Q 'a_flag' definitions
1742  *
1743  * The Adv Library should limit use to the lower nibble (4 bits) of
1744  * a_flag. Drivers are free to use the upper nibble (4 bits) of a_flag.
1745  */
1746 #define ADV_POLL_REQUEST                0x01    /* poll for request completion */
1747 #define ADV_SCSIQ_DONE                  0x02    /* request done */
1748 #define ADV_DONT_RETRY                  0x08    /* don't do retry */
1749
1750 #define ADV_CHIP_ASC3550          0x01  /* Ultra-Wide IC */
1751 #define ADV_CHIP_ASC38C0800       0x02  /* Ultra2-Wide/LVD IC */
1752 #define ADV_CHIP_ASC38C1600       0x03  /* Ultra3-Wide/LVD2 IC */
1753
1754 /*
1755  * Adapter temporary configuration structure
1756  *
1757  * This structure can be discarded after initialization. Don't add
1758  * fields here needed after initialization.
1759  *
1760  * Field naming convention:
1761  *
1762  *  *_enable indicates the field enables or disables a feature. The
1763  *  value of the field is never reset.
1764  */
1765 typedef struct adv_dvc_cfg {
1766         ushort disc_enable;     /* enable disconnection */
1767         uchar chip_version;     /* chip version */
1768         uchar termination;      /* Term. Ctrl. bits 6-5 of SCSI_CFG1 register */
1769         ushort control_flag;    /* Microcode Control Flag */
1770         ushort mcode_date;      /* Microcode date */
1771         ushort mcode_version;   /* Microcode version */
1772         ushort serial1;         /* EEPROM serial number word 1 */
1773         ushort serial2;         /* EEPROM serial number word 2 */
1774         ushort serial3;         /* EEPROM serial number word 3 */
1775 } ADV_DVC_CFG;
1776
1777 struct adv_dvc_var;
1778 struct adv_scsi_req_q;
1779
1780 typedef struct adv_sg_block {
1781         uchar reserved1;
1782         uchar reserved2;
1783         uchar reserved3;
1784         uchar sg_cnt;           /* Valid entries in block. */
1785         __le32 sg_ptr;  /* Pointer to next sg block. */
1786         struct {
1787                 __le32 sg_addr; /* SG element address. */
1788                 __le32 sg_count;        /* SG element count. */
1789         } sg_list[NO_OF_SG_PER_BLOCK];
1790 } ADV_SG_BLOCK;
1791
1792 /*
1793  * ADV_SCSI_REQ_Q - microcode request structure
1794  *
1795  * All fields in this structure up to byte 60 are used by the microcode.
1796  * The microcode makes assumptions about the size and ordering of fields
1797  * in this structure. Do not change the structure definition here without
1798  * coordinating the change with the microcode.
1799  *
1800  * All fields accessed by microcode must be maintained in little_endian
1801  * order.
1802  */
1803 typedef struct adv_scsi_req_q {
1804         uchar cntl;             /* Ucode flags and state (ASC_MC_QC_*). */
1805         uchar target_cmd;
1806         uchar target_id;        /* Device target identifier. */
1807         uchar target_lun;       /* Device target logical unit number. */
1808         __le32 data_addr;       /* Data buffer physical address. */
1809         __le32 data_cnt;        /* Data count. Ucode sets to residual. */
1810         __le32 sense_addr;
1811         __le32 carr_pa;
1812         uchar mflag;
1813         uchar sense_len;
1814         uchar cdb_len;          /* SCSI CDB length. Must <= 16 bytes. */
1815         uchar scsi_cntl;
1816         uchar done_status;      /* Completion status. */
1817         uchar scsi_status;      /* SCSI status byte. */
1818         uchar host_status;      /* Ucode host status. */
1819         uchar sg_working_ix;
1820         uchar cdb[12];          /* SCSI CDB bytes 0-11. */
1821         __le32 sg_real_addr;    /* SG list physical address. */
1822         __le32 scsiq_rptr;
1823         uchar cdb16[4];         /* SCSI CDB bytes 12-15. */
1824         __le32 scsiq_ptr;
1825         __le32 carr_va;
1826         /*
1827          * End of microcode structure - 60 bytes. The rest of the structure
1828          * is used by the Adv Library and ignored by the microcode.
1829          */
1830         u32 srb_tag;
1831         uchar a_flag;
1832         uchar pad[3];           /* Pad out to a word boundary. */
1833         ADV_SG_BLOCK *sg_list_ptr;      /* SG list virtual address. */
1834 } ADV_SCSI_REQ_Q;
1835
1836 /*
1837  * The following two structures are used to process Wide Board requests.
1838  *
1839  * The ADV_SCSI_REQ_Q structure in adv_req_t is passed to the Adv Library
1840  * and microcode with the ADV_SCSI_REQ_Q field 'srb_tag' set to the
1841  * SCSI request tag. The adv_req_t structure 'cmndp' field in turn points
1842  * to the Mid-Level SCSI request structure.
1843  *
1844  * Zero or more ADV_SG_BLOCK are used with each ADV_SCSI_REQ_Q. Each
1845  * ADV_SG_BLOCK structure holds 15 scatter-gather elements. Under Linux
1846  * up to 255 scatter-gather elements may be used per request or
1847  * ADV_SCSI_REQ_Q.
1848  *
1849  * Both structures must be 32 byte aligned.
1850  */
1851 typedef struct adv_sgblk {
1852         ADV_SG_BLOCK sg_block;  /* Sgblock structure. */
1853         dma_addr_t sg_addr;     /* Physical address */
1854         struct adv_sgblk *next_sgblkp;  /* Next scatter-gather structure. */
1855 } adv_sgblk_t;
1856
1857 typedef struct adv_req {
1858         ADV_SCSI_REQ_Q scsi_req_q;      /* Adv Library request structure. */
1859         uchar align[24];        /* Request structure padding. */
1860         struct scsi_cmnd *cmndp;        /* Mid-Level SCSI command pointer. */
1861         dma_addr_t req_addr;
1862         adv_sgblk_t *sgblkp;    /* Adv Library scatter-gather pointer. */
1863 } adv_req_t __aligned(32);
1864
1865 /*
1866  * Adapter operation variable structure.
1867  *
1868  * One structure is required per host adapter.
1869  *
1870  * Field naming convention:
1871  *
1872  *  *_able indicates both whether a feature should be enabled or disabled
1873  *  and whether a device isi capable of the feature. At initialization
1874  *  this field may be set, but later if a device is found to be incapable
1875  *  of the feature, the field is cleared.
1876  */
1877 typedef struct adv_dvc_var {
1878         AdvPortAddr iop_base;   /* I/O port address */
1879         ushort err_code;        /* fatal error code */
1880         ushort bios_ctrl;       /* BIOS control word, EEPROM word 12 */
1881         ushort wdtr_able;       /* try WDTR for a device */
1882         ushort sdtr_able;       /* try SDTR for a device */
1883         ushort ultra_able;      /* try SDTR Ultra speed for a device */
1884         ushort sdtr_speed1;     /* EEPROM SDTR Speed for TID 0-3   */
1885         ushort sdtr_speed2;     /* EEPROM SDTR Speed for TID 4-7   */
1886         ushort sdtr_speed3;     /* EEPROM SDTR Speed for TID 8-11  */
1887         ushort sdtr_speed4;     /* EEPROM SDTR Speed for TID 12-15 */
1888         ushort tagqng_able;     /* try tagged queuing with a device */
1889         ushort ppr_able;        /* PPR message capable per TID bitmask. */
1890         uchar max_dvc_qng;      /* maximum number of tagged commands per device */
1891         ushort start_motor;     /* start motor command allowed */
1892         uchar scsi_reset_wait;  /* delay in seconds after scsi bus reset */
1893         uchar chip_no;          /* should be assigned by caller */
1894         uchar max_host_qng;     /* maximum number of Q'ed command allowed */
1895         ushort no_scam;         /* scam_tolerant of EEPROM */
1896         struct asc_board *drv_ptr;      /* driver pointer to private structure */
1897         uchar chip_scsi_id;     /* chip SCSI target ID */
1898         uchar chip_type;
1899         uchar bist_err_code;
1900         ADV_CARR_T *carrier;
1901         ADV_CARR_T *carr_freelist;      /* Carrier free list. */
1902         dma_addr_t carrier_addr;
1903         ADV_CARR_T *icq_sp;     /* Initiator command queue stopper pointer. */
1904         ADV_CARR_T *irq_sp;     /* Initiator response queue stopper pointer. */
1905         ushort carr_pending_cnt;        /* Count of pending carriers. */
1906         /*
1907          * Note: The following fields will not be used after initialization. The
1908          * driver may discard the buffer after initialization is done.
1909          */
1910         ADV_DVC_CFG *cfg;       /* temporary configuration structure  */
1911 } ADV_DVC_VAR;
1912
1913 /*
1914  * Microcode idle loop commands
1915  */
1916 #define IDLE_CMD_COMPLETED           0
1917 #define IDLE_CMD_STOP_CHIP           0x0001
1918 #define IDLE_CMD_STOP_CHIP_SEND_INT  0x0002
1919 #define IDLE_CMD_SEND_INT            0x0004
1920 #define IDLE_CMD_ABORT               0x0008
1921 #define IDLE_CMD_DEVICE_RESET        0x0010
1922 #define IDLE_CMD_SCSI_RESET_START    0x0020     /* Assert SCSI Bus Reset */
1923 #define IDLE_CMD_SCSI_RESET_END      0x0040     /* Deassert SCSI Bus Reset */
1924 #define IDLE_CMD_SCSIREQ             0x0080
1925
1926 #define IDLE_CMD_STATUS_SUCCESS      0x0001
1927 #define IDLE_CMD_STATUS_FAILURE      0x0002
1928
1929 /*
1930  * AdvSendIdleCmd() flag definitions.
1931  */
1932 #define ADV_NOWAIT     0x01
1933
1934 /*
1935  * Wait loop time out values.
1936  */
1937 #define SCSI_WAIT_100_MSEC           100UL      /* 100 milliseconds */
1938 #define SCSI_US_PER_MSEC             1000       /* microseconds per millisecond */
1939 #define SCSI_MAX_RETRY               10 /* retry count */
1940
1941 #define ADV_ASYNC_RDMA_FAILURE          0x01    /* Fatal RDMA failure. */
1942 #define ADV_ASYNC_SCSI_BUS_RESET_DET    0x02    /* Detected SCSI Bus Reset. */
1943 #define ADV_ASYNC_CARRIER_READY_FAILURE 0x03    /* Carrier Ready failure. */
1944 #define ADV_RDMA_IN_CARR_AND_Q_INVALID  0x04    /* RDMAed-in data invalid. */
1945
1946 #define ADV_HOST_SCSI_BUS_RESET      0x80       /* Host Initiated SCSI Bus Reset. */
1947
1948 /* Read byte from a register. */
1949 #define AdvReadByteRegister(iop_base, reg_off) \
1950      (ADV_MEM_READB((iop_base) + (reg_off)))
1951
1952 /* Write byte to a register. */
1953 #define AdvWriteByteRegister(iop_base, reg_off, byte) \
1954      (ADV_MEM_WRITEB((iop_base) + (reg_off), (byte)))
1955
1956 /* Read word (2 bytes) from a register. */
1957 #define AdvReadWordRegister(iop_base, reg_off) \
1958      (ADV_MEM_READW((iop_base) + (reg_off)))
1959
1960 /* Write word (2 bytes) to a register. */
1961 #define AdvWriteWordRegister(iop_base, reg_off, word) \
1962      (ADV_MEM_WRITEW((iop_base) + (reg_off), (word)))
1963
1964 /* Write dword (4 bytes) to a register. */
1965 #define AdvWriteDWordRegister(iop_base, reg_off, dword) \
1966      (ADV_MEM_WRITEDW((iop_base) + (reg_off), (dword)))
1967
1968 /* Read byte from LRAM. */
1969 #define AdvReadByteLram(iop_base, addr, byte) \
1970 do { \
1971     ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)); \
1972     (byte) = ADV_MEM_READB((iop_base) + IOPB_RAM_DATA); \
1973 } while (0)
1974
1975 /* Write byte to LRAM. */
1976 #define AdvWriteByteLram(iop_base, addr, byte) \
1977     (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1978      ADV_MEM_WRITEB((iop_base) + IOPB_RAM_DATA, (byte)))
1979
1980 /* Read word (2 bytes) from LRAM. */
1981 #define AdvReadWordLram(iop_base, addr, word) \
1982 do { \
1983     ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)); \
1984     (word) = (ADV_MEM_READW((iop_base) + IOPW_RAM_DATA)); \
1985 } while (0)
1986
1987 /* Write word (2 bytes) to LRAM. */
1988 #define AdvWriteWordLram(iop_base, addr, word) \
1989     (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1990      ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, (word)))
1991
1992 /* Write little-endian double word (4 bytes) to LRAM */
1993 /* Because of unspecified C language ordering don't use auto-increment. */
1994 #define AdvWriteDWordLramNoSwap(iop_base, addr, dword) \
1995     ((ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1996       ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, \
1997                      cpu_to_le16((ushort) ((dword) & 0xFFFF)))), \
1998      (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr) + 2), \
1999       ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, \
2000                      cpu_to_le16((ushort) ((dword >> 16) & 0xFFFF)))))
2001
2002 /* Read word (2 bytes) from LRAM assuming that the address is already set. */
2003 #define AdvReadWordAutoIncLram(iop_base) \
2004      (ADV_MEM_READW((iop_base) + IOPW_RAM_DATA))
2005
2006 /* Write word (2 bytes) to LRAM assuming that the address is already set. */
2007 #define AdvWriteWordAutoIncLram(iop_base, word) \
2008      (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, (word)))
2009
2010 /*
2011  * Define macro to check for Condor signature.
2012  *
2013  * Evaluate to ADV_TRUE if a Condor chip is found the specified port
2014  * address 'iop_base'. Otherwise evalue to ADV_FALSE.
2015  */
2016 #define AdvFindSignature(iop_base) \
2017     (((AdvReadByteRegister((iop_base), IOPB_CHIP_ID_1) == \
2018     ADV_CHIP_ID_BYTE) && \
2019      (AdvReadWordRegister((iop_base), IOPW_CHIP_ID_0) == \
2020     ADV_CHIP_ID_WORD)) ?  ADV_TRUE : ADV_FALSE)
2021
2022 /*
2023  * Define macro to Return the version number of the chip at 'iop_base'.
2024  *
2025  * The second parameter 'bus_type' is currently unused.
2026  */
2027 #define AdvGetChipVersion(iop_base, bus_type) \
2028     AdvReadByteRegister((iop_base), IOPB_CHIP_TYPE_REV)
2029
2030 /*
2031  * Abort an SRB in the chip's RISC Memory. The 'srb_tag' argument must
2032  * match the ASC_SCSI_REQ_Q 'srb_tag' field.
2033  *
2034  * If the request has not yet been sent to the device it will simply be
2035  * aborted from RISC memory. If the request is disconnected it will be
2036  * aborted on reselection by sending an Abort Message to the target ID.
2037  *
2038  * Return value:
2039  *      ADV_TRUE(1) - Queue was successfully aborted.
2040  *      ADV_FALSE(0) - Queue was not found on the active queue list.
2041  */
2042 #define AdvAbortQueue(asc_dvc, srb_tag) \
2043      AdvSendIdleCmd((asc_dvc), (ushort) IDLE_CMD_ABORT, \
2044                     (ADV_DCNT) (srb_tag))
2045
2046 /*
2047  * Send a Bus Device Reset Message to the specified target ID.
2048  *
2049  * All outstanding commands will be purged if sending the
2050  * Bus Device Reset Message is successful.
2051  *
2052  * Return Value:
2053  *      ADV_TRUE(1) - All requests on the target are purged.
2054  *      ADV_FALSE(0) - Couldn't issue Bus Device Reset Message; Requests
2055  *                     are not purged.
2056  */
2057 #define AdvResetDevice(asc_dvc, target_id) \
2058      AdvSendIdleCmd((asc_dvc), (ushort) IDLE_CMD_DEVICE_RESET,  \
2059                     (ADV_DCNT) (target_id))
2060
2061 /*
2062  * SCSI Wide Type definition.
2063  */
2064 #define ADV_SCSI_BIT_ID_TYPE   ushort
2065
2066 /*
2067  * AdvInitScsiTarget() 'cntl_flag' options.
2068  */
2069 #define ADV_SCAN_LUN           0x01
2070 #define ADV_CAPINFO_NOLUN      0x02
2071
2072 /*
2073  * Convert target id to target id bit mask.
2074  */
2075 #define ADV_TID_TO_TIDMASK(tid)   (0x01 << ((tid) & ADV_MAX_TID))
2076
2077 /*
2078  * ASC_SCSI_REQ_Q 'done_status' and 'host_status' return values.
2079  */
2080
2081 #define QD_NO_STATUS         0x00       /* Request not completed yet. */
2082 #define QD_NO_ERROR          0x01
2083 #define QD_ABORTED_BY_HOST   0x02
2084 #define QD_WITH_ERROR        0x04
2085
2086 #define QHSTA_NO_ERROR              0x00
2087 #define QHSTA_M_SEL_TIMEOUT         0x11
2088 #define QHSTA_M_DATA_OVER_RUN       0x12
2089 #define QHSTA_M_UNEXPECTED_BUS_FREE 0x13
2090 #define QHSTA_M_QUEUE_ABORTED       0x15
2091 #define QHSTA_M_SXFR_SDMA_ERR       0x16        /* SXFR_STATUS SCSI DMA Error */
2092 #define QHSTA_M_SXFR_SXFR_PERR      0x17        /* SXFR_STATUS SCSI Bus Parity Error */
2093 #define QHSTA_M_RDMA_PERR           0x18        /* RISC PCI DMA parity error */
2094 #define QHSTA_M_SXFR_OFF_UFLW       0x19        /* SXFR_STATUS Offset Underflow */
2095 #define QHSTA_M_SXFR_OFF_OFLW       0x20        /* SXFR_STATUS Offset Overflow */
2096 #define QHSTA_M_SXFR_WD_TMO         0x21        /* SXFR_STATUS Watchdog Timeout */
2097 #define QHSTA_M_SXFR_DESELECTED     0x22        /* SXFR_STATUS Deselected */
2098 /* Note: QHSTA_M_SXFR_XFR_OFLW is identical to QHSTA_M_DATA_OVER_RUN. */
2099 #define QHSTA_M_SXFR_XFR_OFLW       0x12        /* SXFR_STATUS Transfer Overflow */
2100 #define QHSTA_M_SXFR_XFR_PH_ERR     0x24        /* SXFR_STATUS Transfer Phase Error */
2101 #define QHSTA_M_SXFR_UNKNOWN_ERROR  0x25        /* SXFR_STATUS Unknown Error */
2102 #define QHSTA_M_SCSI_BUS_RESET      0x30        /* Request aborted from SBR */
2103 #define QHSTA_M_SCSI_BUS_RESET_UNSOL 0x31       /* Request aborted from unsol. SBR */
2104 #define QHSTA_M_BUS_DEVICE_RESET    0x32        /* Request aborted from BDR */
2105 #define QHSTA_M_DIRECTION_ERR       0x35        /* Data Phase mismatch */
2106 #define QHSTA_M_DIRECTION_ERR_HUNG  0x36        /* Data Phase mismatch and bus hang */
2107 #define QHSTA_M_WTM_TIMEOUT         0x41
2108 #define QHSTA_M_BAD_CMPL_STATUS_IN  0x42
2109 #define QHSTA_M_NO_AUTO_REQ_SENSE   0x43
2110 #define QHSTA_M_AUTO_REQ_SENSE_FAIL 0x44
2111 #define QHSTA_M_INVALID_DEVICE      0x45        /* Bad target ID */
2112 #define QHSTA_M_FROZEN_TIDQ         0x46        /* TID Queue frozen. */
2113 #define QHSTA_M_SGBACKUP_ERROR      0x47        /* Scatter-Gather backup error */
2114
2115 /* Return the address that is aligned at the next doubleword >= to 'addr'. */
2116 #define ADV_32BALIGN(addr)     (((ulong) (addr) + 0x1F) & ~0x1F)
2117
2118 /*
2119  * Total contiguous memory needed for driver SG blocks.
2120  *
2121  * ADV_MAX_SG_LIST must be defined by a driver. It is the maximum
2122  * number of scatter-gather elements the driver supports in a
2123  * single request.
2124  */
2125
2126 #define ADV_SG_LIST_MAX_BYTE_SIZE \
2127          (sizeof(ADV_SG_BLOCK) * \
2128           ((ADV_MAX_SG_LIST + (NO_OF_SG_PER_BLOCK - 1))/NO_OF_SG_PER_BLOCK))
2129
2130 /* struct asc_board flags */
2131 #define ASC_IS_WIDE_BOARD       0x04    /* AdvanSys Wide Board */
2132
2133 #define ASC_NARROW_BOARD(boardp) (((boardp)->flags & ASC_IS_WIDE_BOARD) == 0)
2134
2135 #define NO_ISA_DMA              0xff    /* No ISA DMA Channel Used */
2136
2137 #define ASC_INFO_SIZE           128     /* advansys_info() line size */
2138
2139 /* Asc Library return codes */
2140 #define ASC_TRUE        1
2141 #define ASC_FALSE       0
2142 #define ASC_NOERROR     1
2143 #define ASC_BUSY        0
2144 #define ASC_ERROR       (-1)
2145
2146 /* struct scsi_cmnd function return codes */
2147 #define STATUS_BYTE(byte)   (byte)
2148 #define MSG_BYTE(byte)      ((byte) << 8)
2149 #define HOST_BYTE(byte)     ((byte) << 16)
2150 #define DRIVER_BYTE(byte)   ((byte) << 24)
2151
2152 #define ASC_STATS(shost, counter) ASC_STATS_ADD(shost, counter, 1)
2153 #ifndef ADVANSYS_STATS
2154 #define ASC_STATS_ADD(shost, counter, count)
2155 #else /* ADVANSYS_STATS */
2156 #define ASC_STATS_ADD(shost, counter, count) \
2157         (((struct asc_board *) shost_priv(shost))->asc_stats.counter += (count))
2158 #endif /* ADVANSYS_STATS */
2159
2160 /* If the result wraps when calculating tenths, return 0. */
2161 #define ASC_TENTHS(num, den) \
2162     (((10 * ((num)/(den))) > (((num) * 10)/(den))) ? \
2163     0 : ((((num) * 10)/(den)) - (10 * ((num)/(den)))))
2164
2165 /*
2166  * Display a message to the console.
2167  */
2168 #define ASC_PRINT(s) \
2169     { \
2170         printk("advansys: "); \
2171         printk(s); \
2172     }
2173
2174 #define ASC_PRINT1(s, a1) \
2175     { \
2176         printk("advansys: "); \
2177         printk((s), (a1)); \
2178     }
2179
2180 #define ASC_PRINT2(s, a1, a2) \
2181     { \
2182         printk("advansys: "); \
2183         printk((s), (a1), (a2)); \
2184     }
2185
2186 #define ASC_PRINT3(s, a1, a2, a3) \
2187     { \
2188         printk("advansys: "); \
2189         printk((s), (a1), (a2), (a3)); \
2190     }
2191
2192 #define ASC_PRINT4(s, a1, a2, a3, a4) \
2193     { \
2194         printk("advansys: "); \
2195         printk((s), (a1), (a2), (a3), (a4)); \
2196     }
2197
2198 #ifndef ADVANSYS_DEBUG
2199
2200 #define ASC_DBG(lvl, s...)
2201 #define ASC_DBG_PRT_SCSI_HOST(lvl, s)
2202 #define ASC_DBG_PRT_ASC_SCSI_Q(lvl, scsiqp)
2203 #define ASC_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp)
2204 #define ASC_DBG_PRT_ASC_QDONE_INFO(lvl, qdone)
2205 #define ADV_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp)
2206 #define ASC_DBG_PRT_HEX(lvl, name, start, length)
2207 #define ASC_DBG_PRT_CDB(lvl, cdb, len)
2208 #define ASC_DBG_PRT_SENSE(lvl, sense, len)
2209 #define ASC_DBG_PRT_INQUIRY(lvl, inq, len)
2210
2211 #else /* ADVANSYS_DEBUG */
2212
2213 /*
2214  * Debugging Message Levels:
2215  * 0: Errors Only
2216  * 1: High-Level Tracing
2217  * 2-N: Verbose Tracing
2218  */
2219
2220 #define ASC_DBG(lvl, format, arg...) {                                  \
2221         if (asc_dbglvl >= (lvl))                                        \
2222                 printk(KERN_DEBUG "%s: %s: " format, DRV_NAME,          \
2223                         __func__ , ## arg);                             \
2224 }
2225
2226 #define ASC_DBG_PRT_SCSI_HOST(lvl, s) \
2227     { \
2228         if (asc_dbglvl >= (lvl)) { \
2229             asc_prt_scsi_host(s); \
2230         } \
2231     }
2232
2233 #define ASC_DBG_PRT_ASC_SCSI_Q(lvl, scsiqp) \
2234     { \
2235         if (asc_dbglvl >= (lvl)) { \
2236             asc_prt_asc_scsi_q(scsiqp); \
2237         } \
2238     }
2239
2240 #define ASC_DBG_PRT_ASC_QDONE_INFO(lvl, qdone) \
2241     { \
2242         if (asc_dbglvl >= (lvl)) { \
2243             asc_prt_asc_qdone_info(qdone); \
2244         } \
2245     }
2246
2247 #define ASC_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp) \
2248     { \
2249         if (asc_dbglvl >= (lvl)) { \
2250             asc_prt_adv_scsi_req_q(scsiqp); \
2251         } \
2252     }
2253
2254 #define ASC_DBG_PRT_HEX(lvl, name, start, length) \
2255     { \
2256         if (asc_dbglvl >= (lvl)) { \
2257             asc_prt_hex((name), (start), (length)); \
2258         } \
2259     }
2260
2261 #define ASC_DBG_PRT_CDB(lvl, cdb, len) \
2262         ASC_DBG_PRT_HEX((lvl), "CDB", (uchar *) (cdb), (len));
2263
2264 #define ASC_DBG_PRT_SENSE(lvl, sense, len) \
2265         ASC_DBG_PRT_HEX((lvl), "SENSE", (uchar *) (sense), (len));
2266
2267 #define ASC_DBG_PRT_INQUIRY(lvl, inq, len) \
2268         ASC_DBG_PRT_HEX((lvl), "INQUIRY", (uchar *) (inq), (len));
2269 #endif /* ADVANSYS_DEBUG */
2270
2271 #ifdef ADVANSYS_STATS
2272
2273 /* Per board statistics structure */
2274 struct asc_stats {
2275         /* Driver Entrypoint Statistics */
2276         unsigned int queuecommand;      /* # calls to advansys_queuecommand() */
2277         unsigned int reset;             /* # calls to advansys_eh_bus_reset() */
2278         unsigned int biosparam; /* # calls to advansys_biosparam() */
2279         unsigned int interrupt; /* # advansys_interrupt() calls */
2280         unsigned int callback;  /* # calls to asc/adv_isr_callback() */
2281         unsigned int done;              /* # calls to request's scsi_done function */
2282         unsigned int build_error;       /* # asc/adv_build_req() ASC_ERROR returns. */
2283         unsigned int adv_build_noreq;   /* # adv_build_req() adv_req_t alloc. fail. */
2284         unsigned int adv_build_nosg;    /* # adv_build_req() adv_sgblk_t alloc. fail. */
2285         /* AscExeScsiQueue()/AdvExeScsiQueue() Statistics */
2286         unsigned int exe_noerror;       /* # ASC_NOERROR returns. */
2287         unsigned int exe_busy;  /* # ASC_BUSY returns. */
2288         unsigned int exe_error; /* # ASC_ERROR returns. */
2289         unsigned int exe_unknown;       /* # unknown returns. */
2290         /* Data Transfer Statistics */
2291         unsigned int xfer_cnt;  /* # I/O requests received */
2292         unsigned int xfer_elem; /* # scatter-gather elements */
2293         unsigned int xfer_sect; /* # 512-byte blocks */
2294 };
2295 #endif /* ADVANSYS_STATS */
2296
2297 /*
2298  * Structure allocated for each board.
2299  *
2300  * This structure is allocated by scsi_host_alloc() at the end
2301  * of the 'Scsi_Host' structure starting at the 'hostdata'
2302  * field. It is guaranteed to be allocated from DMA-able memory.
2303  */
2304 struct asc_board {
2305         struct device *dev;
2306         struct Scsi_Host *shost;
2307         uint flags;             /* Board flags */
2308         unsigned int irq;
2309         union {
2310                 ASC_DVC_VAR asc_dvc_var;        /* Narrow board */
2311                 ADV_DVC_VAR adv_dvc_var;        /* Wide board */
2312         } dvc_var;
2313         union {
2314                 ASC_DVC_CFG asc_dvc_cfg;        /* Narrow board */
2315                 ADV_DVC_CFG adv_dvc_cfg;        /* Wide board */
2316         } dvc_cfg;
2317         ushort asc_n_io_port;   /* Number I/O ports. */
2318         ADV_SCSI_BIT_ID_TYPE init_tidmask;      /* Target init./valid mask */
2319         ushort reqcnt[ADV_MAX_TID + 1]; /* Starvation request count */
2320         ADV_SCSI_BIT_ID_TYPE queue_full;        /* Queue full mask */
2321         ushort queue_full_cnt[ADV_MAX_TID + 1]; /* Queue full count */
2322         union {
2323                 ASCEEP_CONFIG asc_eep;  /* Narrow EEPROM config. */
2324                 ADVEEP_3550_CONFIG adv_3550_eep;        /* 3550 EEPROM config. */
2325                 ADVEEP_38C0800_CONFIG adv_38C0800_eep;  /* 38C0800 EEPROM config. */
2326                 ADVEEP_38C1600_CONFIG adv_38C1600_eep;  /* 38C1600 EEPROM config. */
2327         } eep_config;
2328         /* /proc/scsi/advansys/[0...] */
2329 #ifdef ADVANSYS_STATS
2330         struct asc_stats asc_stats;     /* Board statistics */
2331 #endif                          /* ADVANSYS_STATS */
2332         /*
2333          * The following fields are used only for Narrow Boards.
2334          */
2335         uchar sdtr_data[ASC_MAX_TID + 1];       /* SDTR information */
2336         /*
2337          * The following fields are used only for Wide Boards.
2338          */
2339         void __iomem *ioremap_addr;     /* I/O Memory remap address. */
2340         ushort ioport;          /* I/O Port address. */
2341         adv_req_t *adv_reqp;    /* Request structures. */
2342         dma_addr_t adv_reqp_addr;
2343         size_t adv_reqp_size;
2344         struct dma_pool *adv_sgblk_pool;        /* Scatter-gather structures. */
2345         ushort bios_signature;  /* BIOS Signature. */
2346         ushort bios_version;    /* BIOS Version. */
2347         ushort bios_codeseg;    /* BIOS Code Segment. */
2348         ushort bios_codelen;    /* BIOS Code Segment Length. */
2349 };
2350
2351 #define asc_dvc_to_board(asc_dvc) container_of(asc_dvc, struct asc_board, \
2352                                                         dvc_var.asc_dvc_var)
2353 #define adv_dvc_to_board(adv_dvc) container_of(adv_dvc, struct asc_board, \
2354                                                         dvc_var.adv_dvc_var)
2355 #define adv_dvc_to_pdev(adv_dvc) to_pci_dev(adv_dvc_to_board(adv_dvc)->dev)
2356
2357 #ifdef ADVANSYS_DEBUG
2358 static int asc_dbglvl = 3;
2359
2360 /*
2361  * asc_prt_asc_dvc_var()
2362  */
2363 static void asc_prt_asc_dvc_var(ASC_DVC_VAR *h)
2364 {
2365         printk("ASC_DVC_VAR at addr 0x%lx\n", (ulong)h);
2366
2367         printk(" iop_base 0x%x, err_code 0x%x, dvc_cntl 0x%x, bug_fix_cntl "
2368                "%d,\n", h->iop_base, h->err_code, h->dvc_cntl, h->bug_fix_cntl);
2369
2370         printk(" bus_type %d, init_sdtr 0x%x,\n", h->bus_type,
2371                 (unsigned)h->init_sdtr);
2372
2373         printk(" sdtr_done 0x%x, use_tagged_qng 0x%x, unit_not_ready 0x%x, "
2374                "chip_no 0x%x,\n", (unsigned)h->sdtr_done,
2375                (unsigned)h->use_tagged_qng, (unsigned)h->unit_not_ready,
2376                (unsigned)h->chip_no);
2377
2378         printk(" queue_full_or_busy 0x%x, start_motor 0x%x, scsi_reset_wait "
2379                "%u,\n", (unsigned)h->queue_full_or_busy,
2380                (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
2381
2382         printk(" is_in_int %u, max_total_qng %u, cur_total_qng %u, "
2383                "in_critical_cnt %u,\n", (unsigned)h->is_in_int,
2384                (unsigned)h->max_total_qng, (unsigned)h->cur_total_qng,
2385                (unsigned)h->in_critical_cnt);
2386
2387         printk(" last_q_shortage %u, init_state 0x%x, no_scam 0x%x, "
2388                "pci_fix_asyn_xfer 0x%x,\n", (unsigned)h->last_q_shortage,
2389                (unsigned)h->init_state, (unsigned)h->no_scam,
2390                (unsigned)h->pci_fix_asyn_xfer);
2391
2392         printk(" cfg 0x%lx\n", (ulong)h->cfg);
2393 }
2394
2395 /*
2396  * asc_prt_asc_dvc_cfg()
2397  */
2398 static void asc_prt_asc_dvc_cfg(ASC_DVC_CFG *h)
2399 {
2400         printk("ASC_DVC_CFG at addr 0x%lx\n", (ulong)h);
2401
2402         printk(" can_tagged_qng 0x%x, cmd_qng_enabled 0x%x,\n",
2403                h->can_tagged_qng, h->cmd_qng_enabled);
2404         printk(" disc_enable 0x%x, sdtr_enable 0x%x,\n",
2405                h->disc_enable, h->sdtr_enable);
2406
2407         printk(" chip_scsi_id %d, isa_dma_speed %d, isa_dma_channel %d, "
2408                 "chip_version %d,\n", h->chip_scsi_id, h->isa_dma_speed,
2409                 h->isa_dma_channel, h->chip_version);
2410
2411         printk(" mcode_date 0x%x, mcode_version %d\n",
2412                 h->mcode_date, h->mcode_version);
2413 }
2414
2415 /*
2416  * asc_prt_adv_dvc_var()
2417  *
2418  * Display an ADV_DVC_VAR structure.
2419  */
2420 static void asc_prt_adv_dvc_var(ADV_DVC_VAR *h)
2421 {
2422         printk(" ADV_DVC_VAR at addr 0x%lx\n", (ulong)h);
2423
2424         printk("  iop_base 0x%lx, err_code 0x%x, ultra_able 0x%x\n",
2425                (ulong)h->iop_base, h->err_code, (unsigned)h->ultra_able);
2426
2427         printk("  sdtr_able 0x%x, wdtr_able 0x%x\n",
2428                (unsigned)h->sdtr_able, (unsigned)h->wdtr_able);
2429
2430         printk("  start_motor 0x%x, scsi_reset_wait 0x%x\n",
2431                (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
2432
2433         printk("  max_host_qng %u, max_dvc_qng %u, carr_freelist 0x%p\n",
2434                (unsigned)h->max_host_qng, (unsigned)h->max_dvc_qng,
2435                h->carr_freelist);
2436
2437         printk("  icq_sp 0x%p, irq_sp 0x%p\n", h->icq_sp, h->irq_sp);
2438
2439         printk("  no_scam 0x%x, tagqng_able 0x%x\n",
2440                (unsigned)h->no_scam, (unsigned)h->tagqng_able);
2441
2442         printk("  chip_scsi_id 0x%x, cfg 0x%lx\n",
2443                (unsigned)h->chip_scsi_id, (ulong)h->cfg);
2444 }
2445
2446 /*
2447  * asc_prt_adv_dvc_cfg()
2448  *
2449  * Display an ADV_DVC_CFG structure.
2450  */
2451 static void asc_prt_adv_dvc_cfg(ADV_DVC_CFG *h)
2452 {
2453         printk(" ADV_DVC_CFG at addr 0x%lx\n", (ulong)h);
2454
2455         printk("  disc_enable 0x%x, termination 0x%x\n",
2456                h->disc_enable, h->termination);
2457
2458         printk("  chip_version 0x%x, mcode_date 0x%x\n",
2459                h->chip_version, h->mcode_date);
2460
2461         printk("  mcode_version 0x%x, control_flag 0x%x\n",
2462                h->mcode_version, h->control_flag);
2463 }
2464
2465 /*
2466  * asc_prt_scsi_host()
2467  */
2468 static void asc_prt_scsi_host(struct Scsi_Host *s)
2469 {
2470         struct asc_board *boardp = shost_priv(s);
2471
2472         printk("Scsi_Host at addr 0x%p, device %s\n", s, dev_name(boardp->dev));
2473         printk(" host_busy %u, host_no %d,\n",
2474                atomic_read(&s->host_busy), s->host_no);
2475
2476         printk(" base 0x%lx, io_port 0x%lx, irq %d,\n",
2477                (ulong)s->base, (ulong)s->io_port, boardp->irq);
2478
2479         printk(" dma_channel %d, this_id %d, can_queue %d,\n",
2480                s->dma_channel, s->this_id, s->can_queue);
2481
2482         printk(" cmd_per_lun %d, sg_tablesize %d, unchecked_isa_dma %d\n",
2483                s->cmd_per_lun, s->sg_tablesize, s->unchecked_isa_dma);
2484
2485         if (ASC_NARROW_BOARD(boardp)) {
2486                 asc_prt_asc_dvc_var(&boardp->dvc_var.asc_dvc_var);
2487                 asc_prt_asc_dvc_cfg(&boardp->dvc_cfg.asc_dvc_cfg);
2488         } else {
2489                 asc_prt_adv_dvc_var(&boardp->dvc_var.adv_dvc_var);
2490                 asc_prt_adv_dvc_cfg(&boardp->dvc_cfg.adv_dvc_cfg);
2491         }
2492 }
2493
2494 /*
2495  * asc_prt_hex()
2496  *
2497  * Print hexadecimal output in 4 byte groupings 32 bytes
2498  * or 8 double-words per line.
2499  */
2500 static void asc_prt_hex(char *f, uchar *s, int l)
2501 {
2502         int i;
2503         int j;
2504         int k;
2505         int m;
2506
2507         printk("%s: (%d bytes)\n", f, l);
2508
2509         for (i = 0; i < l; i += 32) {
2510
2511                 /* Display a maximum of 8 double-words per line. */
2512                 if ((k = (l - i) / 4) >= 8) {
2513                         k = 8;
2514                         m = 0;
2515                 } else {
2516                         m = (l - i) % 4;
2517                 }
2518
2519                 for (j = 0; j < k; j++) {
2520                         printk(" %2.2X%2.2X%2.2X%2.2X",
2521                                (unsigned)s[i + (j * 4)],
2522                                (unsigned)s[i + (j * 4) + 1],
2523                                (unsigned)s[i + (j * 4) + 2],
2524                                (unsigned)s[i + (j * 4) + 3]);
2525                 }
2526
2527                 switch (m) {
2528                 case 0:
2529                 default:
2530                         break;
2531                 case 1:
2532                         printk(" %2.2X", (unsigned)s[i + (j * 4)]);
2533                         break;
2534                 case 2:
2535                         printk(" %2.2X%2.2X",
2536                                (unsigned)s[i + (j * 4)],
2537                                (unsigned)s[i + (j * 4) + 1]);
2538                         break;
2539                 case 3:
2540                         printk(" %2.2X%2.2X%2.2X",
2541                                (unsigned)s[i + (j * 4) + 1],
2542                                (unsigned)s[i + (j * 4) + 2],
2543                                (unsigned)s[i + (j * 4) + 3]);
2544                         break;
2545                 }
2546
2547                 printk("\n");
2548         }
2549 }
2550
2551 /*
2552  * asc_prt_asc_scsi_q()
2553  */
2554 static void asc_prt_asc_scsi_q(ASC_SCSI_Q *q)
2555 {
2556         ASC_SG_HEAD *sgp;
2557         int i;
2558
2559         printk("ASC_SCSI_Q at addr 0x%lx\n", (ulong)q);
2560
2561         printk
2562             (" target_ix 0x%x, target_lun %u, srb_tag 0x%x, tag_code 0x%x,\n",
2563              q->q2.target_ix, q->q1.target_lun, q->q2.srb_tag,
2564              q->q2.tag_code);
2565
2566         printk
2567             (" data_addr 0x%lx, data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2568              (ulong)le32_to_cpu(q->q1.data_addr),
2569              (ulong)le32_to_cpu(q->q1.data_cnt),
2570              (ulong)le32_to_cpu(q->q1.sense_addr), q->q1.sense_len);
2571
2572         printk(" cdbptr 0x%lx, cdb_len %u, sg_head 0x%lx, sg_queue_cnt %u\n",
2573                (ulong)q->cdbptr, q->q2.cdb_len,
2574                (ulong)q->sg_head, q->q1.sg_queue_cnt);
2575
2576         if (q->sg_head) {
2577                 sgp = q->sg_head;
2578                 printk("ASC_SG_HEAD at addr 0x%lx\n", (ulong)sgp);
2579                 printk(" entry_cnt %u, queue_cnt %u\n", sgp->entry_cnt,
2580                        sgp->queue_cnt);
2581                 for (i = 0; i < sgp->entry_cnt; i++) {
2582                         printk(" [%u]: addr 0x%lx, bytes %lu\n",
2583                                i, (ulong)le32_to_cpu(sgp->sg_list[i].addr),
2584                                (ulong)le32_to_cpu(sgp->sg_list[i].bytes));
2585                 }
2586
2587         }
2588 }
2589
2590 /*
2591  * asc_prt_asc_qdone_info()
2592  */
2593 static void asc_prt_asc_qdone_info(ASC_QDONE_INFO *q)
2594 {
2595         printk("ASC_QDONE_INFO at addr 0x%lx\n", (ulong)q);
2596         printk(" srb_tag 0x%x, target_ix %u, cdb_len %u, tag_code %u,\n",
2597                q->d2.srb_tag, q->d2.target_ix, q->d2.cdb_len,
2598                q->d2.tag_code);
2599         printk
2600             (" done_stat 0x%x, host_stat 0x%x, scsi_stat 0x%x, scsi_msg 0x%x\n",
2601              q->d3.done_stat, q->d3.host_stat, q->d3.scsi_stat, q->d3.scsi_msg);
2602 }
2603
2604 /*
2605  * asc_prt_adv_sgblock()
2606  *
2607  * Display an ADV_SG_BLOCK structure.
2608  */
2609 static void asc_prt_adv_sgblock(int sgblockno, ADV_SG_BLOCK *b)
2610 {
2611         int i;
2612
2613         printk(" ADV_SG_BLOCK at addr 0x%lx (sgblockno %d)\n",
2614                (ulong)b, sgblockno);
2615         printk("  sg_cnt %u, sg_ptr 0x%lx\n",
2616                b->sg_cnt, (ulong)le32_to_cpu(b->sg_ptr));
2617         BUG_ON(b->sg_cnt > NO_OF_SG_PER_BLOCK);
2618         if (b->sg_ptr != 0)
2619                 BUG_ON(b->sg_cnt != NO_OF_SG_PER_BLOCK);
2620         for (i = 0; i < b->sg_cnt; i++) {
2621                 printk("  [%u]: sg_addr 0x%lx, sg_count 0x%lx\n",
2622                        i, (ulong)b->sg_list[i].sg_addr,
2623                        (ulong)b->sg_list[i].sg_count);
2624         }
2625 }
2626
2627 /*
2628  * asc_prt_adv_scsi_req_q()
2629  *
2630  * Display an ADV_SCSI_REQ_Q structure.
2631  */
2632 static void asc_prt_adv_scsi_req_q(ADV_SCSI_REQ_Q *q)
2633 {
2634         int sg_blk_cnt;
2635         struct adv_sg_block *sg_ptr;
2636         adv_sgblk_t *sgblkp;
2637
2638         printk("ADV_SCSI_REQ_Q at addr 0x%lx\n", (ulong)q);
2639
2640         printk("  target_id %u, target_lun %u, srb_tag 0x%x, a_flag 0x%x\n",
2641                q->target_id, q->target_lun, q->srb_tag, q->a_flag);
2642
2643         printk("  cntl 0x%x, data_addr 0x%lx\n",
2644                q->cntl, (ulong)le32_to_cpu(q->data_addr));
2645
2646         printk("  data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2647                (ulong)le32_to_cpu(q->data_cnt),
2648                (ulong)le32_to_cpu(q->sense_addr), q->sense_len);
2649
2650         printk
2651             ("  cdb_len %u, done_status 0x%x, host_status 0x%x, scsi_status 0x%x\n",
2652              q->cdb_len, q->done_status, q->host_status, q->scsi_status);
2653
2654         printk("  sg_working_ix 0x%x, target_cmd %u\n",
2655                q->sg_working_ix, q->target_cmd);
2656
2657         printk("  scsiq_rptr 0x%lx, sg_real_addr 0x%lx, sg_list_ptr 0x%lx\n",
2658                (ulong)le32_to_cpu(q->scsiq_rptr),
2659                (ulong)le32_to_cpu(q->sg_real_addr), (ulong)q->sg_list_ptr);
2660
2661         /* Display the request's ADV_SG_BLOCK structures. */
2662         if (q->sg_list_ptr != NULL) {
2663                 sgblkp = container_of(q->sg_list_ptr, adv_sgblk_t, sg_block);
2664                 sg_blk_cnt = 0;
2665                 while (sgblkp) {
2666                         sg_ptr = &sgblkp->sg_block;
2667                         asc_prt_adv_sgblock(sg_blk_cnt, sg_ptr);
2668                         if (sg_ptr->sg_ptr == 0) {
2669                                 break;
2670                         }
2671                         sgblkp = sgblkp->next_sgblkp;
2672                         sg_blk_cnt++;
2673                 }
2674         }
2675 }
2676 #endif /* ADVANSYS_DEBUG */
2677
2678 /*
2679  * advansys_info()
2680  *
2681  * Return suitable for printing on the console with the argument
2682  * adapter's configuration information.
2683  *
2684  * Note: The information line should not exceed ASC_INFO_SIZE bytes,
2685  * otherwise the static 'info' array will be overrun.
2686  */
2687 static const char *advansys_info(struct Scsi_Host *shost)
2688 {
2689         static char info[ASC_INFO_SIZE];
2690         struct asc_board *boardp = shost_priv(shost);
2691         ASC_DVC_VAR *asc_dvc_varp;
2692         ADV_DVC_VAR *adv_dvc_varp;
2693         char *busname;
2694         char *widename = NULL;
2695
2696         if (ASC_NARROW_BOARD(boardp)) {
2697                 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
2698                 ASC_DBG(1, "begin\n");
2699                 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
2700                         if ((asc_dvc_varp->bus_type & ASC_IS_ISAPNP) ==
2701                             ASC_IS_ISAPNP) {
2702                                 busname = "ISA PnP";
2703                         } else {
2704                                 busname = "ISA";
2705                         }
2706                         sprintf(info,
2707                                 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X, DMA 0x%X",
2708                                 ASC_VERSION, busname,
2709                                 (ulong)shost->io_port,
2710                                 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
2711                                 boardp->irq, shost->dma_channel);
2712                 } else {
2713                         if (asc_dvc_varp->bus_type & ASC_IS_VL) {
2714                                 busname = "VL";
2715                         } else if (asc_dvc_varp->bus_type & ASC_IS_EISA) {
2716                                 busname = "EISA";
2717                         } else if (asc_dvc_varp->bus_type & ASC_IS_PCI) {
2718                                 if ((asc_dvc_varp->bus_type & ASC_IS_PCI_ULTRA)
2719                                     == ASC_IS_PCI_ULTRA) {
2720                                         busname = "PCI Ultra";
2721                                 } else {
2722                                         busname = "PCI";
2723                                 }
2724                         } else {
2725                                 busname = "?";
2726                                 shost_printk(KERN_ERR, shost, "unknown bus "
2727                                         "type %d\n", asc_dvc_varp->bus_type);
2728                         }
2729                         sprintf(info,
2730                                 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X",
2731                                 ASC_VERSION, busname, (ulong)shost->io_port,
2732                                 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
2733                                 boardp->irq);
2734                 }
2735         } else {
2736                 /*
2737                  * Wide Adapter Information
2738                  *
2739                  * Memory-mapped I/O is used instead of I/O space to access
2740                  * the adapter, but display the I/O Port range. The Memory
2741                  * I/O address is displayed through the driver /proc file.
2742                  */
2743                 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
2744                 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
2745                         widename = "Ultra-Wide";
2746                 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
2747                         widename = "Ultra2-Wide";
2748                 } else {
2749                         widename = "Ultra3-Wide";
2750                 }
2751                 sprintf(info,
2752                         "AdvanSys SCSI %s: PCI %s: PCIMEM 0x%lX-0x%lX, IRQ 0x%X",
2753                         ASC_VERSION, widename, (ulong)adv_dvc_varp->iop_base,
2754                         (ulong)adv_dvc_varp->iop_base + boardp->asc_n_io_port - 1, boardp->irq);
2755         }
2756         BUG_ON(strlen(info) >= ASC_INFO_SIZE);
2757         ASC_DBG(1, "end\n");
2758         return info;
2759 }
2760
2761 #ifdef CONFIG_PROC_FS
2762
2763 /*
2764  * asc_prt_board_devices()
2765  *
2766  * Print driver information for devices attached to the board.
2767  */
2768 static void asc_prt_board_devices(struct seq_file *m, struct Scsi_Host *shost)
2769 {
2770         struct asc_board *boardp = shost_priv(shost);
2771         int chip_scsi_id;
2772         int i;
2773
2774         seq_printf(m,
2775                    "\nDevice Information for AdvanSys SCSI Host %d:\n",
2776                    shost->host_no);
2777
2778         if (ASC_NARROW_BOARD(boardp)) {
2779                 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
2780         } else {
2781                 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
2782         }
2783
2784         seq_puts(m, "Target IDs Detected:");
2785         for (i = 0; i <= ADV_MAX_TID; i++) {
2786                 if (boardp->init_tidmask & ADV_TID_TO_TIDMASK(i))
2787                         seq_printf(m, " %X,", i);
2788         }
2789         seq_printf(m, " (%X=Host Adapter)\n", chip_scsi_id);
2790 }
2791
2792 /*
2793  * Display Wide Board BIOS Information.
2794  */
2795 static void asc_prt_adv_bios(struct seq_file *m, struct Scsi_Host *shost)
2796 {
2797         struct asc_board *boardp = shost_priv(shost);
2798         ushort major, minor, letter;
2799
2800         seq_puts(m, "\nROM BIOS Version: ");
2801
2802         /*
2803          * If the BIOS saved a valid signature, then fill in
2804          * the BIOS code segment base address.
2805          */
2806         if (boardp->bios_signature != 0x55AA) {
2807                 seq_puts(m, "Disabled or Pre-3.1\n"
2808                         "BIOS either disabled or Pre-3.1. If it is pre-3.1, then a newer version\n"
2809                         "can be found at the ConnectCom FTP site: ftp://ftp.connectcom.net/pub\n");
2810         } else {
2811                 major = (boardp->bios_version >> 12) & 0xF;
2812                 minor = (boardp->bios_version >> 8) & 0xF;
2813                 letter = (boardp->bios_version & 0xFF);
2814
2815                 seq_printf(m, "%d.%d%c\n",
2816                                    major, minor,
2817                                    letter >= 26 ? '?' : letter + 'A');
2818                 /*
2819                  * Current available ROM BIOS release is 3.1I for UW
2820                  * and 3.2I for U2W. This code doesn't differentiate
2821                  * UW and U2W boards.
2822                  */
2823                 if (major < 3 || (major <= 3 && minor < 1) ||
2824                     (major <= 3 && minor <= 1 && letter < ('I' - 'A'))) {
2825                         seq_puts(m, "Newer version of ROM BIOS is available at the ConnectCom FTP site:\n"
2826                                 "ftp://ftp.connectcom.net/pub\n");
2827                 }
2828         }
2829 }
2830
2831 /*
2832  * Add serial number to information bar if signature AAh
2833  * is found in at bit 15-9 (7 bits) of word 1.
2834  *
2835  * Serial Number consists fo 12 alpha-numeric digits.
2836  *
2837  *       1 - Product type (A,B,C,D..)  Word0: 15-13 (3 bits)
2838  *       2 - MFG Location (A,B,C,D..)  Word0: 12-10 (3 bits)
2839  *     3-4 - Product ID (0-99)         Word0: 9-0 (10 bits)
2840  *       5 - Product revision (A-J)    Word0:  "         "
2841  *
2842  *           Signature                 Word1: 15-9 (7 bits)
2843  *       6 - Year (0-9)                Word1: 8-6 (3 bits) & Word2: 15 (1 bit)
2844  *     7-8 - Week of the year (1-52)   Word1: 5-0 (6 bits)
2845  *
2846  *    9-12 - Serial Number (A001-Z999) Word2: 14-0 (15 bits)
2847  *
2848  * Note 1: Only production cards will have a serial number.
2849  *
2850  * Note 2: Signature is most significant 7 bits (0xFE).
2851  *
2852  * Returns ASC_TRUE if serial number found, otherwise returns ASC_FALSE.
2853  */
2854 static int asc_get_eeprom_string(ushort *serialnum, uchar *cp)
2855 {
2856         ushort w, num;
2857
2858         if ((serialnum[1] & 0xFE00) != ((ushort)0xAA << 8)) {
2859                 return ASC_FALSE;
2860         } else {
2861                 /*
2862                  * First word - 6 digits.
2863                  */
2864                 w = serialnum[0];
2865
2866                 /* Product type - 1st digit. */
2867                 if ((*cp = 'A' + ((w & 0xE000) >> 13)) == 'H') {
2868                         /* Product type is P=Prototype */
2869                         *cp += 0x8;
2870                 }
2871                 cp++;
2872
2873                 /* Manufacturing location - 2nd digit. */
2874                 *cp++ = 'A' + ((w & 0x1C00) >> 10);
2875
2876                 /* Product ID - 3rd, 4th digits. */
2877                 num = w & 0x3FF;
2878                 *cp++ = '0' + (num / 100);
2879                 num %= 100;
2880                 *cp++ = '0' + (num / 10);
2881
2882                 /* Product revision - 5th digit. */
2883                 *cp++ = 'A' + (num % 10);
2884
2885                 /*
2886                  * Second word
2887                  */
2888                 w = serialnum[1];
2889
2890                 /*
2891                  * Year - 6th digit.
2892                  *
2893                  * If bit 15 of third word is set, then the
2894                  * last digit of the year is greater than 7.
2895                  */
2896                 if (serialnum[2] & 0x8000) {
2897                         *cp++ = '8' + ((w & 0x1C0) >> 6);
2898                 } else {
2899                         *cp++ = '0' + ((w & 0x1C0) >> 6);
2900                 }
2901
2902                 /* Week of year - 7th, 8th digits. */
2903                 num = w & 0x003F;
2904                 *cp++ = '0' + num / 10;
2905                 num %= 10;
2906                 *cp++ = '0' + num;
2907
2908                 /*
2909                  * Third word
2910                  */
2911                 w = serialnum[2] & 0x7FFF;
2912
2913                 /* Serial number - 9th digit. */
2914                 *cp++ = 'A' + (w / 1000);
2915
2916                 /* 10th, 11th, 12th digits. */
2917                 num = w % 1000;
2918                 *cp++ = '0' + num / 100;
2919                 num %= 100;
2920                 *cp++ = '0' + num / 10;
2921                 num %= 10;
2922                 *cp++ = '0' + num;
2923
2924                 *cp = '\0';     /* Null Terminate the string. */
2925                 return ASC_TRUE;
2926         }
2927 }
2928
2929 /*
2930  * asc_prt_asc_board_eeprom()
2931  *
2932  * Print board EEPROM configuration.
2933  */
2934 static void asc_prt_asc_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
2935 {
2936         struct asc_board *boardp = shost_priv(shost);
2937         ASC_DVC_VAR *asc_dvc_varp;
2938         ASCEEP_CONFIG *ep;
2939         int i;
2940 #ifdef CONFIG_ISA
2941         int isa_dma_speed[] = { 10, 8, 7, 6, 5, 4, 3, 2 };
2942 #endif /* CONFIG_ISA */
2943         uchar serialstr[13];
2944
2945         asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
2946         ep = &boardp->eep_config.asc_eep;
2947
2948         seq_printf(m,
2949                    "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
2950                    shost->host_no);
2951
2952         if (asc_get_eeprom_string((ushort *)&ep->adapter_info[0], serialstr)
2953             == ASC_TRUE)
2954                 seq_printf(m, " Serial Number: %s\n", serialstr);
2955         else if (ep->adapter_info[5] == 0xBB)
2956                 seq_puts(m,
2957                          " Default Settings Used for EEPROM-less Adapter.\n");
2958         else
2959                 seq_puts(m, " Serial Number Signature Not Present.\n");
2960
2961         seq_printf(m,
2962                    " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
2963                    ASC_EEP_GET_CHIP_ID(ep), ep->max_total_qng,
2964                    ep->max_tag_qng);
2965
2966         seq_printf(m,
2967                    " cntl 0x%x, no_scam 0x%x\n", ep->cntl, ep->no_scam);
2968
2969         seq_puts(m, " Target ID:           ");
2970         for (i = 0; i <= ASC_MAX_TID; i++)
2971                 seq_printf(m, " %d", i);
2972
2973         seq_puts(m, "\n Disconnects:         ");
2974         for (i = 0; i <= ASC_MAX_TID; i++)
2975                 seq_printf(m, " %c",
2976                            (ep->disc_enable & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
2977
2978         seq_puts(m, "\n Command Queuing:     ");
2979         for (i = 0; i <= ASC_MAX_TID; i++)
2980                 seq_printf(m, " %c",
2981                            (ep->use_cmd_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
2982
2983         seq_puts(m, "\n Start Motor:         ");
2984         for (i = 0; i <= ASC_MAX_TID; i++)
2985                 seq_printf(m, " %c",
2986                            (ep->start_motor & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
2987
2988         seq_puts(m, "\n Synchronous Transfer:");
2989         for (i = 0; i <= ASC_MAX_TID; i++)
2990                 seq_printf(m, " %c",
2991                            (ep->init_sdtr & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
2992         seq_putc(m, '\n');
2993
2994 #ifdef CONFIG_ISA
2995         if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
2996                 seq_printf(m,
2997                            " Host ISA DMA speed:   %d MB/S\n",
2998                            isa_dma_speed[ASC_EEP_GET_DMA_SPD(ep)]);
2999         }
3000 #endif /* CONFIG_ISA */
3001 }
3002
3003 /*
3004  * asc_prt_adv_board_eeprom()
3005  *
3006  * Print board EEPROM configuration.
3007  */
3008 static void asc_prt_adv_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
3009 {
3010         struct asc_board *boardp = shost_priv(shost);
3011         ADV_DVC_VAR *adv_dvc_varp;
3012         int i;
3013         char *termstr;
3014         uchar serialstr[13];
3015         ADVEEP_3550_CONFIG *ep_3550 = NULL;
3016         ADVEEP_38C0800_CONFIG *ep_38C0800 = NULL;
3017         ADVEEP_38C1600_CONFIG *ep_38C1600 = NULL;
3018         ushort word;
3019         ushort *wordp;
3020         ushort sdtr_speed = 0;
3021
3022         adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
3023         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3024                 ep_3550 = &boardp->eep_config.adv_3550_eep;
3025         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3026                 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
3027         } else {
3028                 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
3029         }
3030
3031         seq_printf(m,
3032                    "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
3033                    shost->host_no);
3034
3035         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3036                 wordp = &ep_3550->serial_number_word1;
3037         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3038                 wordp = &ep_38C0800->serial_number_word1;
3039         } else {
3040                 wordp = &ep_38C1600->serial_number_word1;
3041         }
3042
3043         if (asc_get_eeprom_string(wordp, serialstr) == ASC_TRUE)
3044                 seq_printf(m, " Serial Number: %s\n", serialstr);
3045         else
3046                 seq_puts(m, " Serial Number Signature Not Present.\n");
3047
3048         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3049                 seq_printf(m,
3050                            " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3051                            ep_3550->adapter_scsi_id,
3052                            ep_3550->max_host_qng, ep_3550->max_dvc_qng);
3053         else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3054                 seq_printf(m,
3055                            " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3056                            ep_38C0800->adapter_scsi_id,
3057                            ep_38C0800->max_host_qng,
3058                            ep_38C0800->max_dvc_qng);
3059         else
3060                 seq_printf(m,
3061                            " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3062                            ep_38C1600->adapter_scsi_id,
3063                            ep_38C1600->max_host_qng,
3064                            ep_38C1600->max_dvc_qng);
3065         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3066                 word = ep_3550->termination;
3067         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3068                 word = ep_38C0800->termination_lvd;
3069         } else {
3070                 word = ep_38C1600->termination_lvd;
3071         }
3072         switch (word) {
3073         case 1:
3074                 termstr = "Low Off/High Off";
3075                 break;
3076         case 2:
3077                 termstr = "Low Off/High On";
3078                 break;
3079         case 3:
3080                 termstr = "Low On/High On";
3081                 break;
3082         default:
3083         case 0:
3084                 termstr = "Automatic";
3085                 break;
3086         }
3087
3088         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3089                 seq_printf(m,
3090                            " termination: %u (%s), bios_ctrl: 0x%x\n",
3091                            ep_3550->termination, termstr,
3092                            ep_3550->bios_ctrl);
3093         else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3094                 seq_printf(m,
3095                            " termination: %u (%s), bios_ctrl: 0x%x\n",
3096                            ep_38C0800->termination_lvd, termstr,
3097                            ep_38C0800->bios_ctrl);
3098         else
3099                 seq_printf(m,
3100                            " termination: %u (%s), bios_ctrl: 0x%x\n",
3101                            ep_38C1600->termination_lvd, termstr,
3102                            ep_38C1600->bios_ctrl);
3103
3104         seq_puts(m, " Target ID:           ");
3105         for (i = 0; i <= ADV_MAX_TID; i++)
3106                 seq_printf(m, " %X", i);
3107         seq_putc(m, '\n');
3108
3109         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3110                 word = ep_3550->disc_enable;
3111         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3112                 word = ep_38C0800->disc_enable;
3113         } else {
3114                 word = ep_38C1600->disc_enable;
3115         }
3116         seq_puts(m, " Disconnects:         ");
3117         for (i = 0; i <= ADV_MAX_TID; i++)
3118                 seq_printf(m, " %c",
3119                            (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3120         seq_putc(m, '\n');
3121
3122         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3123                 word = ep_3550->tagqng_able;
3124         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3125                 word = ep_38C0800->tagqng_able;
3126         } else {
3127                 word = ep_38C1600->tagqng_able;
3128         }
3129         seq_puts(m, " Command Queuing:     ");
3130         for (i = 0; i <= ADV_MAX_TID; i++)
3131                 seq_printf(m, " %c",
3132                            (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3133         seq_putc(m, '\n');
3134
3135         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3136                 word = ep_3550->start_motor;
3137         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3138                 word = ep_38C0800->start_motor;
3139         } else {
3140                 word = ep_38C1600->start_motor;
3141         }
3142         seq_puts(m, " Start Motor:         ");
3143         for (i = 0; i <= ADV_MAX_TID; i++)
3144                 seq_printf(m, " %c",
3145                            (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3146         seq_putc(m, '\n');
3147
3148         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3149                 seq_puts(m, " Synchronous Transfer:");
3150                 for (i = 0; i <= ADV_MAX_TID; i++)
3151                         seq_printf(m, " %c",
3152                                    (ep_3550->sdtr_able & ADV_TID_TO_TIDMASK(i)) ?
3153                                    'Y' : 'N');
3154                 seq_putc(m, '\n');
3155         }
3156
3157         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3158                 seq_puts(m, " Ultra Transfer:      ");
3159                 for (i = 0; i <= ADV_MAX_TID; i++)
3160                         seq_printf(m, " %c",
3161                                    (ep_3550->ultra_able & ADV_TID_TO_TIDMASK(i))
3162                                    ? 'Y' : 'N');
3163                 seq_putc(m, '\n');
3164         }
3165
3166         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3167                 word = ep_3550->wdtr_able;
3168         } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3169                 word = ep_38C0800->wdtr_able;
3170         } else {
3171                 word = ep_38C1600->wdtr_able;
3172         }
3173         seq_puts(m, " Wide Transfer:       ");
3174         for (i = 0; i <= ADV_MAX_TID; i++)
3175                 seq_printf(m, " %c",
3176                            (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3177         seq_putc(m, '\n');
3178
3179         if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800 ||
3180             adv_dvc_varp->chip_type == ADV_CHIP_ASC38C1600) {
3181                 seq_puts(m, " Synchronous Transfer Speed (Mhz):\n  ");
3182                 for (i = 0; i <= ADV_MAX_TID; i++) {
3183                         char *speed_str;
3184
3185                         if (i == 0) {
3186                                 sdtr_speed = adv_dvc_varp->sdtr_speed1;
3187                         } else if (i == 4) {
3188                                 sdtr_speed = adv_dvc_varp->sdtr_speed2;
3189                         } else if (i == 8) {
3190                                 sdtr_speed = adv_dvc_varp->sdtr_speed3;
3191                         } else if (i == 12) {
3192                                 sdtr_speed = adv_dvc_varp->sdtr_speed4;
3193                         }
3194                         switch (sdtr_speed & ADV_MAX_TID) {
3195                         case 0:
3196                                 speed_str = "Off";
3197                                 break;
3198                         case 1:
3199                                 speed_str = "  5";
3200                                 break;
3201                         case 2:
3202                                 speed_str = " 10";
3203                                 break;
3204                         case 3:
3205                                 speed_str = " 20";
3206                                 break;
3207                         case 4:
3208                                 speed_str = " 40";
3209                                 break;
3210                         case 5:
3211                                 speed_str = " 80";
3212                                 break;
3213                         default:
3214                                 speed_str = "Unk";
3215                                 break;
3216                         }
3217                         seq_printf(m, "%X:%s ", i, speed_str);
3218                         if (i == 7)
3219                                 seq_puts(m, "\n  ");
3220                         sdtr_speed >>= 4;
3221                 }
3222                 seq_putc(m, '\n');
3223         }
3224 }
3225
3226 /*
3227  * asc_prt_driver_conf()
3228  */
3229 static void asc_prt_driver_conf(struct seq_file *m, struct Scsi_Host *shost)
3230 {
3231         struct asc_board *boardp = shost_priv(shost);
3232         int chip_scsi_id;
3233
3234         seq_printf(m,
3235                 "\nLinux Driver Configuration and Information for AdvanSys SCSI Host %d:\n",
3236                 shost->host_no);
3237
3238         seq_printf(m,
3239                    " host_busy %u, max_id %u, max_lun %llu, max_channel %u\n",
3240                    atomic_read(&shost->host_busy), shost->max_id,
3241                    shost->max_lun, shost->max_channel);
3242
3243         seq_printf(m,
3244                    " unique_id %d, can_queue %d, this_id %d, sg_tablesize %u, cmd_per_lun %u\n",
3245                    shost->unique_id, shost->can_queue, shost->this_id,
3246                    shost->sg_tablesize, shost->cmd_per_lun);
3247
3248         seq_printf(m,
3249                    " unchecked_isa_dma %d, use_clustering %d\n",
3250                    shost->unchecked_isa_dma, shost->use_clustering);
3251
3252         seq_printf(m,
3253                    " flags 0x%x, last_reset 0x%lx, jiffies 0x%lx, asc_n_io_port 0x%x\n",
3254                    boardp->flags, shost->last_reset, jiffies,
3255                    boardp->asc_n_io_port);
3256
3257         seq_printf(m, " io_port 0x%lx\n", shost->io_port);
3258
3259         if (ASC_NARROW_BOARD(boardp)) {
3260                 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
3261         } else {
3262                 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
3263         }
3264 }
3265
3266 /*
3267  * asc_prt_asc_board_info()
3268  *
3269  * Print dynamic board configuration information.
3270  */
3271 static void asc_prt_asc_board_info(struct seq_file *m, struct Scsi_Host *shost)
3272 {
3273         struct asc_board *boardp = shost_priv(shost);
3274         int chip_scsi_id;
3275         ASC_DVC_VAR *v;
3276         ASC_DVC_CFG *c;
3277         int i;
3278         int renegotiate = 0;
3279
3280         v = &boardp->dvc_var.asc_dvc_var;
3281         c = &boardp->dvc_cfg.asc_dvc_cfg;
3282         chip_scsi_id = c->chip_scsi_id;
3283
3284         seq_printf(m,
3285                    "\nAsc Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3286                    shost->host_no);
3287
3288         seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3289                    "mcode_version 0x%x, err_code %u\n",
3290                    c->chip_version, c->mcode_date, c->mcode_version,
3291                    v->err_code);
3292
3293         /* Current number of commands waiting for the host. */
3294         seq_printf(m,
3295                    " Total Command Pending: %d\n", v->cur_total_qng);
3296
3297         seq_puts(m, " Command Queuing:");
3298         for (i = 0; i <= ASC_MAX_TID; i++) {
3299                 if ((chip_scsi_id == i) ||
3300                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3301                         continue;
3302                 }
3303                 seq_printf(m, " %X:%c",
3304                            i,
3305                            (v->use_tagged_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3306         }
3307
3308         /* Current number of commands waiting for a device. */
3309         seq_puts(m, "\n Command Queue Pending:");
3310         for (i = 0; i <= ASC_MAX_TID; i++) {
3311                 if ((chip_scsi_id == i) ||
3312                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3313                         continue;
3314                 }
3315                 seq_printf(m, " %X:%u", i, v->cur_dvc_qng[i]);
3316         }
3317
3318         /* Current limit on number of commands that can be sent to a device. */
3319         seq_puts(m, "\n Command Queue Limit:");
3320         for (i = 0; i <= ASC_MAX_TID; i++) {
3321                 if ((chip_scsi_id == i) ||
3322                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3323                         continue;
3324                 }
3325                 seq_printf(m, " %X:%u", i, v->max_dvc_qng[i]);
3326         }
3327
3328         /* Indicate whether the device has returned queue full status. */
3329         seq_puts(m, "\n Command Queue Full:");
3330         for (i = 0; i <= ASC_MAX_TID; i++) {
3331                 if ((chip_scsi_id == i) ||
3332                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3333                         continue;
3334                 }
3335                 if (boardp->queue_full & ADV_TID_TO_TIDMASK(i))
3336                         seq_printf(m, " %X:Y-%d",
3337                                    i, boardp->queue_full_cnt[i]);
3338                 else
3339                         seq_printf(m, " %X:N", i);
3340         }
3341
3342         seq_puts(m, "\n Synchronous Transfer:");
3343         for (i = 0; i <= ASC_MAX_TID; i++) {
3344                 if ((chip_scsi_id == i) ||
3345                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3346                         continue;
3347                 }
3348                 seq_printf(m, " %X:%c",
3349                            i,
3350                            (v->sdtr_done & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3351         }
3352         seq_putc(m, '\n');
3353
3354         for (i = 0; i <= ASC_MAX_TID; i++) {
3355                 uchar syn_period_ix;
3356
3357                 if ((chip_scsi_id == i) ||
3358                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3359                     ((v->init_sdtr & ADV_TID_TO_TIDMASK(i)) == 0)) {
3360                         continue;
3361                 }
3362
3363                 seq_printf(m, "  %X:", i);
3364
3365                 if ((boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET) == 0) {
3366                         seq_puts(m, " Asynchronous");
3367                 } else {
3368                         syn_period_ix =
3369                             (boardp->sdtr_data[i] >> 4) & (v->max_sdtr_index -
3370                                                            1);
3371
3372                         seq_printf(m,
3373                                    " Transfer Period Factor: %d (%d.%d Mhz),",
3374                                    v->sdtr_period_tbl[syn_period_ix],
3375                                    250 / v->sdtr_period_tbl[syn_period_ix],
3376                                    ASC_TENTHS(250,
3377                                               v->sdtr_period_tbl[syn_period_ix]));
3378
3379                         seq_printf(m, " REQ/ACK Offset: %d",
3380                                    boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET);
3381                 }
3382
3383                 if ((v->sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
3384                         seq_puts(m, "*\n");
3385                         renegotiate = 1;
3386                 } else {
3387                         seq_putc(m, '\n');
3388                 }
3389         }
3390
3391         if (renegotiate) {
3392                 seq_puts(m, " * = Re-negotiation pending before next command.\n");
3393         }
3394 }
3395
3396 /*
3397  * asc_prt_adv_board_info()
3398  *
3399  * Print dynamic board configuration information.
3400  */
3401 static void asc_prt_adv_board_info(struct seq_file *m, struct Scsi_Host *shost)
3402 {
3403         struct asc_board *boardp = shost_priv(shost);
3404         int i;
3405         ADV_DVC_VAR *v;
3406         ADV_DVC_CFG *c;
3407         AdvPortAddr iop_base;
3408         ushort chip_scsi_id;
3409         ushort lramword;
3410         uchar lrambyte;
3411         ushort tagqng_able;
3412         ushort sdtr_able, wdtr_able;
3413         ushort wdtr_done, sdtr_done;
3414         ushort period = 0;
3415         int renegotiate = 0;
3416
3417         v = &boardp->dvc_var.adv_dvc_var;
3418         c = &boardp->dvc_cfg.adv_dvc_cfg;
3419         iop_base = v->iop_base;
3420         chip_scsi_id = v->chip_scsi_id;
3421
3422         seq_printf(m,
3423                    "\nAdv Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3424                    shost->host_no);
3425
3426         seq_printf(m,
3427                    " iop_base 0x%lx, cable_detect: %X, err_code %u\n",
3428                    (unsigned long)v->iop_base,
3429                    AdvReadWordRegister(iop_base,IOPW_SCSI_CFG1) & CABLE_DETECT,
3430                    v->err_code);
3431
3432         seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3433                    "mcode_version 0x%x\n", c->chip_version,
3434                    c->mcode_date, c->mcode_version);
3435
3436         AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
3437         seq_puts(m, " Queuing Enabled:");
3438         for (i = 0; i <= ADV_MAX_TID; i++) {
3439                 if ((chip_scsi_id == i) ||
3440                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3441                         continue;
3442                 }
3443
3444                 seq_printf(m, " %X:%c",
3445                            i,
3446                            (tagqng_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3447         }
3448
3449         seq_puts(m, "\n Queue Limit:");
3450         for (i = 0; i <= ADV_MAX_TID; i++) {
3451                 if ((chip_scsi_id == i) ||
3452                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3453                         continue;
3454                 }
3455
3456                 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + i,
3457                                 lrambyte);
3458
3459                 seq_printf(m, " %X:%d", i, lrambyte);
3460         }
3461
3462         seq_puts(m, "\n Command Pending:");
3463         for (i = 0; i <= ADV_MAX_TID; i++) {
3464                 if ((chip_scsi_id == i) ||
3465                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3466                         continue;
3467                 }
3468
3469                 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_QUEUED_CMD + i,
3470                                 lrambyte);
3471
3472                 seq_printf(m, " %X:%d", i, lrambyte);
3473         }
3474         seq_putc(m, '\n');
3475
3476         AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
3477         seq_puts(m, " Wide Enabled:");
3478         for (i = 0; i <= ADV_MAX_TID; i++) {
3479                 if ((chip_scsi_id == i) ||
3480                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3481                         continue;
3482                 }
3483
3484                 seq_printf(m, " %X:%c",
3485                            i,
3486                            (wdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3487         }
3488         seq_putc(m, '\n');
3489
3490         AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, wdtr_done);
3491         seq_puts(m, " Transfer Bit Width:");
3492         for (i = 0; i <= ADV_MAX_TID; i++) {
3493                 if ((chip_scsi_id == i) ||
3494                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3495                         continue;
3496                 }
3497
3498                 AdvReadWordLram(iop_base,
3499                                 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3500                                 lramword);
3501
3502                 seq_printf(m, " %X:%d",
3503                            i, (lramword & 0x8000) ? 16 : 8);
3504
3505                 if ((wdtr_able & ADV_TID_TO_TIDMASK(i)) &&
3506                     (wdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
3507                         seq_putc(m, '*');
3508                         renegotiate = 1;
3509                 }
3510         }
3511         seq_putc(m, '\n');
3512
3513         AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
3514         seq_puts(m, " Synchronous Enabled:");
3515         for (i = 0; i <= ADV_MAX_TID; i++) {
3516                 if ((chip_scsi_id == i) ||
3517                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3518                         continue;
3519                 }
3520
3521                 seq_printf(m, " %X:%c",
3522                            i,
3523                            (sdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
3524         }
3525         seq_putc(m, '\n');
3526
3527         AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, sdtr_done);
3528         for (i = 0; i <= ADV_MAX_TID; i++) {
3529
3530                 AdvReadWordLram(iop_base,
3531                                 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3532                                 lramword);
3533                 lramword &= ~0x8000;
3534
3535                 if ((chip_scsi_id == i) ||
3536                     ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3537                     ((sdtr_able & ADV_TID_TO_TIDMASK(i)) == 0)) {
3538                         continue;
3539                 }
3540
3541                 seq_printf(m, "  %X:", i);
3542
3543                 if ((lramword & 0x1F) == 0) {   /* Check for REQ/ACK Offset 0. */
3544                         seq_puts(m, " Asynchronous");
3545                 } else {
3546                         seq_puts(m, " Transfer Period Factor: ");
3547
3548                         if ((lramword & 0x1F00) == 0x1100) {    /* 80 Mhz */
3549                                 seq_puts(m, "9 (80.0 Mhz),");
3550                         } else if ((lramword & 0x1F00) == 0x1000) {     /* 40 Mhz */
3551                                 seq_puts(m, "10 (40.0 Mhz),");
3552                         } else {        /* 20 Mhz or below. */
3553
3554                                 period = (((lramword >> 8) * 25) + 50) / 4;
3555
3556                                 if (period == 0) {      /* Should never happen. */
3557                                         seq_printf(m, "%d (? Mhz), ", period);
3558                                 } else {
3559                                         seq_printf(m,
3560                                                    "%d (%d.%d Mhz),",
3561                                                    period, 250 / period,
3562                                                    ASC_TENTHS(250, period));
3563                                 }
3564                         }
3565
3566                         seq_printf(m, " REQ/ACK Offset: %d",
3567                                    lramword & 0x1F);
3568                 }
3569
3570                 if ((sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
3571                         seq_puts(m, "*\n");
3572                         renegotiate = 1;
3573                 } else {
3574                         seq_putc(m, '\n');
3575                 }
3576         }
3577
3578         if (renegotiate) {
3579                 seq_puts(m, " * = Re-negotiation pending before next command.\n");
3580         }
3581 }
3582
3583 #ifdef ADVANSYS_STATS
3584 /*
3585  * asc_prt_board_stats()
3586  */
3587 static void asc_prt_board_stats(struct seq_file *m, struct Scsi_Host *shost)
3588 {
3589         struct asc_board *boardp = shost_priv(shost);
3590         struct asc_stats *s = &boardp->asc_stats;
3591
3592         seq_printf(m,
3593                    "\nLinux Driver Statistics for AdvanSys SCSI Host %d:\n",
3594                    shost->host_no);
3595
3596         seq_printf(m,
3597                    " queuecommand %u, reset %u, biosparam %u, interrupt %u\n",
3598                    s->queuecommand, s->reset, s->biosparam,
3599                    s->interrupt);
3600
3601         seq_printf(m,
3602                    " callback %u, done %u, build_error %u, build_noreq %u, build_nosg %u\n",
3603                    s->callback, s->done, s->build_error,
3604                    s->adv_build_noreq, s->adv_build_nosg);
3605
3606         seq_printf(m,
3607                    " exe_noerror %u, exe_busy %u, exe_error %u, exe_unknown %u\n",
3608                    s->exe_noerror, s->exe_busy, s->exe_error,
3609                    s->exe_unknown);
3610
3611         /*
3612          * Display data transfer statistics.
3613          */
3614         if (s->xfer_cnt > 0) {
3615                 seq_printf(m, " xfer_cnt %u, xfer_elem %u, ",
3616                            s->xfer_cnt, s->xfer_elem);
3617
3618                 seq_printf(m, "xfer_bytes %u.%01u kb\n",
3619                            s->xfer_sect / 2, ASC_TENTHS(s->xfer_sect, 2));
3620
3621                 /* Scatter gather transfer statistics */
3622                 seq_printf(m, " avg_num_elem %u.%01u, ",
3623                            s->xfer_elem / s->xfer_cnt,
3624                            ASC_TENTHS(s->xfer_elem, s->xfer_cnt));
3625
3626                 seq_printf(m, "avg_elem_size %u.%01u kb, ",
3627                            (s->xfer_sect / 2) / s->xfer_elem,
3628                            ASC_TENTHS((s->xfer_sect / 2), s->xfer_elem));
3629
3630                 seq_printf(m, "avg_xfer_size %u.%01u kb\n",
3631                            (s->xfer_sect / 2) / s->xfer_cnt,
3632                            ASC_TENTHS((s->xfer_sect / 2), s->xfer_cnt));
3633         }
3634 }
3635 #endif /* ADVANSYS_STATS */
3636
3637 /*
3638  * advansys_show_info() - /proc/scsi/advansys/{0,1,2,3,...}
3639  *
3640  * m: seq_file to print into
3641  * shost: Scsi_Host
3642  *
3643  * Return the number of bytes read from or written to a
3644  * /proc/scsi/advansys/[0...] file.
3645  */
3646 static int
3647 advansys_show_info(struct seq_file *m, struct Scsi_Host *shost)
3648 {
3649         struct asc_board *boardp = shost_priv(shost);
3650
3651         ASC_DBG(1, "begin\n");
3652
3653         /*
3654          * User read of /proc/scsi/advansys/[0...] file.
3655          */
3656
3657         /*
3658          * Get board configuration information.
3659          *
3660          * advansys_info() returns the board string from its own static buffer.
3661          */
3662         /* Copy board information. */
3663         seq_printf(m, "%s\n", (char *)advansys_info(shost));
3664         /*
3665          * Display Wide Board BIOS Information.
3666          */
3667         if (!ASC_NARROW_BOARD(boardp))
3668                 asc_prt_adv_bios(m, shost);
3669
3670         /*
3671          * Display driver information for each device attached to the board.
3672          */
3673         asc_prt_board_devices(m, shost);
3674
3675         /*
3676          * Display EEPROM configuration for the board.
3677          */
3678         if (ASC_NARROW_BOARD(boardp))
3679                 asc_prt_asc_board_eeprom(m, shost);
3680         else
3681                 asc_prt_adv_board_eeprom(m, shost);
3682
3683         /*
3684          * Display driver configuration and information for the board.
3685          */
3686         asc_prt_driver_conf(m, shost);
3687
3688 #ifdef ADVANSYS_STATS
3689         /*
3690          * Display driver statistics for the board.
3691          */
3692         asc_prt_board_stats(m, shost);
3693 #endif /* ADVANSYS_STATS */
3694
3695         /*
3696          * Display Asc Library dynamic configuration information
3697          * for the board.
3698          */
3699         if (ASC_NARROW_BOARD(boardp))
3700                 asc_prt_asc_board_info(m, shost);
3701         else
3702                 asc_prt_adv_board_info(m, shost);
3703         return 0;
3704 }
3705 #endif /* CONFIG_PROC_FS */
3706
3707 static void asc_scsi_done(struct scsi_cmnd *scp)
3708 {
3709         scsi_dma_unmap(scp);
3710         ASC_STATS(scp->device->host, done);
3711         scp->scsi_done(scp);
3712 }
3713
3714 static void AscSetBank(PortAddr iop_base, uchar bank)
3715 {
3716         uchar val;
3717
3718         val = AscGetChipControl(iop_base) &
3719             (~
3720              (CC_SINGLE_STEP | CC_TEST | CC_DIAG | CC_SCSI_RESET |
3721               CC_CHIP_RESET));
3722         if (bank == 1) {
3723                 val |= CC_BANK_ONE;
3724         } else if (bank == 2) {
3725                 val |= CC_DIAG | CC_BANK_ONE;
3726         } else {
3727                 val &= ~CC_BANK_ONE;
3728         }
3729         AscSetChipControl(iop_base, val);
3730 }
3731
3732 static void AscSetChipIH(PortAddr iop_base, ushort ins_code)
3733 {
3734         AscSetBank(iop_base, 1);
3735         AscWriteChipIH(iop_base, ins_code);
3736         AscSetBank(iop_base, 0);
3737 }
3738
3739 static int AscStartChip(PortAddr iop_base)
3740 {
3741         AscSetChipControl(iop_base, 0);
3742         if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3743                 return (0);
3744         }
3745         return (1);
3746 }
3747
3748 static bool AscStopChip(PortAddr iop_base)
3749 {
3750         uchar cc_val;
3751
3752         cc_val =
3753             AscGetChipControl(iop_base) &
3754             (~(CC_SINGLE_STEP | CC_TEST | CC_DIAG));
3755         AscSetChipControl(iop_base, (uchar)(cc_val | CC_HALT));
3756         AscSetChipIH(iop_base, INS_HALT);
3757         AscSetChipIH(iop_base, INS_RFLAG_WTM);
3758         if ((AscGetChipStatus(iop_base) & CSW_HALTED) == 0) {
3759                 return false;
3760         }
3761         return true;
3762 }
3763
3764 static bool AscIsChipHalted(PortAddr iop_base)
3765 {
3766         if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3767                 if ((AscGetChipControl(iop_base) & CC_HALT) != 0) {
3768                         return true;
3769                 }
3770         }
3771         return false;
3772 }
3773
3774 static bool AscResetChipAndScsiBus(ASC_DVC_VAR *asc_dvc)
3775 {
3776         PortAddr iop_base;
3777         int i = 10;
3778
3779         iop_base = asc_dvc->iop_base;
3780         while ((AscGetChipStatus(iop_base) & CSW_SCSI_RESET_ACTIVE)
3781                && (i-- > 0)) {
3782                 mdelay(100);
3783         }
3784         AscStopChip(iop_base);
3785         AscSetChipControl(iop_base, CC_CHIP_RESET | CC_SCSI_RESET | CC_HALT);
3786         udelay(60);
3787         AscSetChipIH(iop_base, INS_RFLAG_WTM);
3788         AscSetChipIH(iop_base, INS_HALT);
3789         AscSetChipControl(iop_base, CC_CHIP_RESET | CC_HALT);
3790         AscSetChipControl(iop_base, CC_HALT);
3791         mdelay(200);
3792         AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
3793         AscSetChipStatus(iop_base, 0);
3794         return (AscIsChipHalted(iop_base));
3795 }
3796
3797 static int AscFindSignature(PortAddr iop_base)
3798 {
3799         ushort sig_word;
3800
3801         ASC_DBG(1, "AscGetChipSignatureByte(0x%x) 0x%x\n",
3802                  iop_base, AscGetChipSignatureByte(iop_base));
3803         if (AscGetChipSignatureByte(iop_base) == (uchar)ASC_1000_ID1B) {
3804                 ASC_DBG(1, "AscGetChipSignatureWord(0x%x) 0x%x\n",
3805                          iop_base, AscGetChipSignatureWord(iop_base));
3806                 sig_word = AscGetChipSignatureWord(iop_base);
3807                 if ((sig_word == (ushort)ASC_1000_ID0W) ||
3808                     (sig_word == (ushort)ASC_1000_ID0W_FIX)) {
3809                         return (1);
3810                 }
3811         }
3812         return (0);
3813 }
3814
3815 static void AscEnableInterrupt(PortAddr iop_base)
3816 {
3817         ushort cfg;
3818
3819         cfg = AscGetChipCfgLsw(iop_base);
3820         AscSetChipCfgLsw(iop_base, cfg | ASC_CFG0_HOST_INT_ON);
3821 }
3822
3823 static void AscDisableInterrupt(PortAddr iop_base)
3824 {
3825         ushort cfg;
3826
3827         cfg = AscGetChipCfgLsw(iop_base);
3828         AscSetChipCfgLsw(iop_base, cfg & (~ASC_CFG0_HOST_INT_ON));
3829 }
3830
3831 static uchar AscReadLramByte(PortAddr iop_base, ushort addr)
3832 {
3833         unsigned char byte_data;
3834         unsigned short word_data;
3835
3836         if (isodd_word(addr)) {
3837                 AscSetChipLramAddr(iop_base, addr - 1);
3838                 word_data = AscGetChipLramData(iop_base);
3839                 byte_data = (word_data >> 8) & 0xFF;
3840         } else {
3841                 AscSetChipLramAddr(iop_base, addr);
3842                 word_data = AscGetChipLramData(iop_base);
3843                 byte_data = word_data & 0xFF;
3844         }
3845         return byte_data;
3846 }
3847
3848 static ushort AscReadLramWord(PortAddr iop_base, ushort addr)
3849 {
3850         ushort word_data;
3851
3852         AscSetChipLramAddr(iop_base, addr);
3853         word_data = AscGetChipLramData(iop_base);
3854         return (word_data);
3855 }
3856
3857 #if CC_VERY_LONG_SG_LIST
3858 static u32 AscReadLramDWord(PortAddr iop_base, ushort addr)
3859 {
3860         ushort val_low, val_high;
3861         u32 dword_data;
3862
3863         AscSetChipLramAddr(iop_base, addr);
3864         val_low = AscGetChipLramData(iop_base);
3865         val_high = AscGetChipLramData(iop_base);
3866         dword_data = ((u32) val_high << 16) | (u32) val_low;
3867         return (dword_data);
3868 }
3869 #endif /* CC_VERY_LONG_SG_LIST */
3870
3871 static void
3872 AscMemWordSetLram(PortAddr iop_base, ushort s_addr, ushort set_wval, int words)
3873 {
3874         int i;
3875
3876         AscSetChipLramAddr(iop_base, s_addr);
3877         for (i = 0; i < words; i++) {
3878                 AscSetChipLramData(iop_base, set_wval);
3879         }
3880 }
3881
3882 static void AscWriteLramWord(PortAddr iop_base, ushort addr, ushort word_val)
3883 {
3884         AscSetChipLramAddr(iop_base, addr);
3885         AscSetChipLramData(iop_base, word_val);
3886 }
3887
3888 static void AscWriteLramByte(PortAddr iop_base, ushort addr, uchar byte_val)
3889 {
3890         ushort word_data;
3891
3892         if (isodd_word(addr)) {
3893                 addr--;
3894                 word_data = AscReadLramWord(iop_base, addr);
3895                 word_data &= 0x00FF;
3896                 word_data |= (((ushort)byte_val << 8) & 0xFF00);
3897         } else {
3898                 word_data = AscReadLramWord(iop_base, addr);
3899                 word_data &= 0xFF00;
3900                 word_data |= ((ushort)byte_val & 0x00FF);
3901         }
3902         AscWriteLramWord(iop_base, addr, word_data);
3903 }
3904
3905 /*
3906  * Copy 2 bytes to LRAM.
3907  *
3908  * The source data is assumed to be in little-endian order in memory
3909  * and is maintained in little-endian order when written to LRAM.
3910  */
3911 static void
3912 AscMemWordCopyPtrToLram(PortAddr iop_base, ushort s_addr,
3913                         const uchar *s_buffer, int words)
3914 {
3915         int i;
3916
3917         AscSetChipLramAddr(iop_base, s_addr);
3918         for (i = 0; i < 2 * words; i += 2) {
3919                 /*
3920                  * On a little-endian system the second argument below
3921                  * produces a little-endian ushort which is written to
3922                  * LRAM in little-endian order. On a big-endian system
3923                  * the second argument produces a big-endian ushort which
3924                  * is "transparently" byte-swapped by outpw() and written
3925                  * in little-endian order to LRAM.
3926                  */
3927                 outpw(iop_base + IOP_RAM_DATA,
3928                       ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]);
3929         }
3930 }
3931
3932 /*
3933  * Copy 4 bytes to LRAM.
3934  *
3935  * The source data is assumed to be in little-endian order in memory
3936  * and is maintained in little-endian order when written to LRAM.
3937  */
3938 static void
3939 AscMemDWordCopyPtrToLram(PortAddr iop_base,
3940                          ushort s_addr, uchar *s_buffer, int dwords)
3941 {
3942         int i;
3943
3944         AscSetChipLramAddr(iop_base, s_addr);
3945         for (i = 0; i < 4 * dwords; i += 4) {
3946                 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]);   /* LSW */
3947                 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 3] << 8) | s_buffer[i + 2]);       /* MSW */
3948         }
3949 }
3950
3951 /*
3952  * Copy 2 bytes from LRAM.
3953  *
3954  * The source data is assumed to be in little-endian order in LRAM
3955  * and is maintained in little-endian order when written to memory.
3956  */
3957 static void
3958 AscMemWordCopyPtrFromLram(PortAddr iop_base,
3959                           ushort s_addr, uchar *d_buffer, int words)
3960 {
3961         int i;
3962         ushort word;
3963
3964         AscSetChipLramAddr(iop_base, s_addr);
3965         for (i = 0; i < 2 * words; i += 2) {
3966                 word = inpw(iop_base + IOP_RAM_DATA);
3967                 d_buffer[i] = word & 0xff;
3968                 d_buffer[i + 1] = (word >> 8) & 0xff;
3969         }
3970 }
3971
3972 static u32 AscMemSumLramWord(PortAddr iop_base, ushort s_addr, int words)
3973 {
3974         u32 sum = 0;
3975         int i;
3976
3977         for (i = 0; i < words; i++, s_addr += 2) {
3978                 sum += AscReadLramWord(iop_base, s_addr);
3979         }
3980         return (sum);
3981 }
3982
3983 static void AscInitLram(ASC_DVC_VAR *asc_dvc)
3984 {
3985         uchar i;
3986         ushort s_addr;
3987         PortAddr iop_base;
3988
3989         iop_base = asc_dvc->iop_base;
3990         AscMemWordSetLram(iop_base, ASC_QADR_BEG, 0,
3991                           (ushort)(((int)(asc_dvc->max_total_qng + 2 + 1) *
3992                                     64) >> 1));
3993         i = ASC_MIN_ACTIVE_QNO;
3994         s_addr = ASC_QADR_BEG + ASC_QBLK_SIZE;
3995         AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
3996                          (uchar)(i + 1));
3997         AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
3998                          (uchar)(asc_dvc->max_total_qng));
3999         AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4000                          (uchar)i);
4001         i++;
4002         s_addr += ASC_QBLK_SIZE;
4003         for (; i < asc_dvc->max_total_qng; i++, s_addr += ASC_QBLK_SIZE) {
4004                 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4005                                  (uchar)(i + 1));
4006                 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4007                                  (uchar)(i - 1));
4008                 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4009                                  (uchar)i);
4010         }
4011         AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4012                          (uchar)ASC_QLINK_END);
4013         AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4014                          (uchar)(asc_dvc->max_total_qng - 1));
4015         AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4016                          (uchar)asc_dvc->max_total_qng);
4017         i++;
4018         s_addr += ASC_QBLK_SIZE;
4019         for (; i <= (uchar)(asc_dvc->max_total_qng + 3);
4020              i++, s_addr += ASC_QBLK_SIZE) {
4021                 AscWriteLramByte(iop_base,
4022                                  (ushort)(s_addr + (ushort)ASC_SCSIQ_B_FWD), i);
4023                 AscWriteLramByte(iop_base,
4024                                  (ushort)(s_addr + (ushort)ASC_SCSIQ_B_BWD), i);
4025                 AscWriteLramByte(iop_base,
4026                                  (ushort)(s_addr + (ushort)ASC_SCSIQ_B_QNO), i);
4027         }
4028 }
4029
4030 static u32
4031 AscLoadMicroCode(PortAddr iop_base, ushort s_addr,
4032                  const uchar *mcode_buf, ushort mcode_size)
4033 {
4034         u32 chksum;
4035         ushort mcode_word_size;
4036         ushort mcode_chksum;
4037
4038         /* Write the microcode buffer starting at LRAM address 0. */
4039         mcode_word_size = (ushort)(mcode_size >> 1);
4040         AscMemWordSetLram(iop_base, s_addr, 0, mcode_word_size);
4041         AscMemWordCopyPtrToLram(iop_base, s_addr, mcode_buf, mcode_word_size);
4042
4043         chksum = AscMemSumLramWord(iop_base, s_addr, mcode_word_size);
4044         ASC_DBG(1, "chksum 0x%lx\n", (ulong)chksum);
4045         mcode_chksum = (ushort)AscMemSumLramWord(iop_base,
4046                                                  (ushort)ASC_CODE_SEC_BEG,
4047                                                  (ushort)((mcode_size -
4048                                                            s_addr - (ushort)
4049                                                            ASC_CODE_SEC_BEG) /
4050                                                           2));
4051         ASC_DBG(1, "mcode_chksum 0x%lx\n", (ulong)mcode_chksum);
4052         AscWriteLramWord(iop_base, ASCV_MCODE_CHKSUM_W, mcode_chksum);
4053         AscWriteLramWord(iop_base, ASCV_MCODE_SIZE_W, mcode_size);
4054         return chksum;
4055 }
4056
4057 static void AscInitQLinkVar(ASC_DVC_VAR *asc_dvc)
4058 {
4059         PortAddr iop_base;
4060         int i;
4061         ushort lram_addr;
4062
4063         iop_base = asc_dvc->iop_base;
4064         AscPutRiscVarFreeQHead(iop_base, 1);
4065         AscPutRiscVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4066         AscPutVarFreeQHead(iop_base, 1);
4067         AscPutVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4068         AscWriteLramByte(iop_base, ASCV_BUSY_QHEAD_B,
4069                          (uchar)((int)asc_dvc->max_total_qng + 1));
4070         AscWriteLramByte(iop_base, ASCV_DISC1_QHEAD_B,
4071                          (uchar)((int)asc_dvc->max_total_qng + 2));
4072         AscWriteLramByte(iop_base, (ushort)ASCV_TOTAL_READY_Q_B,
4073                          asc_dvc->max_total_qng);
4074         AscWriteLramWord(iop_base, ASCV_ASCDVC_ERR_CODE_W, 0);
4075         AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
4076         AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, 0);
4077         AscWriteLramByte(iop_base, ASCV_SCSIBUSY_B, 0);
4078         AscWriteLramByte(iop_base, ASCV_WTM_FLAG_B, 0);
4079         AscPutQDoneInProgress(iop_base, 0);
4080         lram_addr = ASC_QADR_BEG;
4081         for (i = 0; i < 32; i++, lram_addr += 2) {
4082                 AscWriteLramWord(iop_base, lram_addr, 0);
4083         }
4084 }
4085
4086 static int AscInitMicroCodeVar(ASC_DVC_VAR *asc_dvc)
4087 {
4088         int i;
4089         int warn_code;
4090         PortAddr iop_base;
4091         __le32 phy_addr;
4092         __le32 phy_size;
4093         struct asc_board *board = asc_dvc_to_board(asc_dvc);
4094
4095         iop_base = asc_dvc->iop_base;
4096         warn_code = 0;
4097         for (i = 0; i <= ASC_MAX_TID; i++) {
4098                 AscPutMCodeInitSDTRAtID(iop_base, i,
4099                                         asc_dvc->cfg->sdtr_period_offset[i]);
4100         }
4101
4102         AscInitQLinkVar(asc_dvc);
4103         AscWriteLramByte(iop_base, ASCV_DISC_ENABLE_B,
4104                          asc_dvc->cfg->disc_enable);
4105         AscWriteLramByte(iop_base, ASCV_HOSTSCSI_ID_B,
4106                          ASC_TID_TO_TARGET_ID(asc_dvc->cfg->chip_scsi_id));
4107
4108         /* Ensure overrun buffer is aligned on an 8 byte boundary. */
4109         BUG_ON((unsigned long)asc_dvc->overrun_buf & 7);
4110         asc_dvc->overrun_dma = dma_map_single(board->dev, asc_dvc->overrun_buf,
4111                                         ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
4112         if (dma_mapping_error(board->dev, asc_dvc->overrun_dma)) {
4113                 warn_code = -ENOMEM;
4114                 goto err_dma_map;
4115         }
4116         phy_addr = cpu_to_le32(asc_dvc->overrun_dma);
4117         AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_PADDR_D,
4118                                  (uchar *)&phy_addr, 1);
4119         phy_size = cpu_to_le32(ASC_OVERRUN_BSIZE);
4120         AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_BSIZE_D,
4121                                  (uchar *)&phy_size, 1);
4122
4123         asc_dvc->cfg->mcode_date =
4124             AscReadLramWord(iop_base, (ushort)ASCV_MC_DATE_W);
4125         asc_dvc->cfg->mcode_version =
4126             AscReadLramWord(iop_base, (ushort)ASCV_MC_VER_W);
4127
4128         AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
4129         if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
4130                 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
4131                 warn_code = -EINVAL;
4132                 goto err_mcode_start;
4133         }
4134         if (AscStartChip(iop_base) != 1) {
4135                 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
4136                 warn_code = -EIO;
4137                 goto err_mcode_start;
4138         }
4139
4140         return warn_code;
4141
4142 err_mcode_start:
4143         dma_unmap_single(board->dev, asc_dvc->overrun_dma,
4144                          ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
4145 err_dma_map:
4146         asc_dvc->overrun_dma = 0;
4147         return warn_code;
4148 }
4149
4150 static int AscInitAsc1000Driver(ASC_DVC_VAR *asc_dvc)
4151 {
4152         const struct firmware *fw;
4153         const char fwname[] = "advansys/mcode.bin";
4154         int err;
4155         unsigned long chksum;
4156         int warn_code;
4157         PortAddr iop_base;
4158
4159         iop_base = asc_dvc->iop_base;
4160         warn_code = 0;
4161         if ((asc_dvc->dvc_cntl & ASC_CNTL_RESET_SCSI) &&
4162             !(asc_dvc->init_state & ASC_INIT_RESET_SCSI_DONE)) {
4163                 AscResetChipAndScsiBus(asc_dvc);
4164                 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4165         }
4166         asc_dvc->init_state |= ASC_INIT_STATE_BEG_LOAD_MC;
4167         if (asc_dvc->err_code != 0)
4168                 return ASC_ERROR;
4169         if (!AscFindSignature(asc_dvc->iop_base)) {
4170                 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
4171                 return warn_code;
4172         }
4173         AscDisableInterrupt(iop_base);
4174         AscInitLram(asc_dvc);
4175
4176         err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4177         if (err) {
4178                 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4179                        fwname, err);
4180                 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
4181                 return err;
4182         }
4183         if (fw->size < 4) {
4184                 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4185                        fw->size, fwname);
4186                 release_firmware(fw);
4187                 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
4188                 return -EINVAL;
4189         }
4190         chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4191                  (fw->data[1] << 8) | fw->data[0];
4192         ASC_DBG(1, "_asc_mcode_chksum 0x%lx\n", (ulong)chksum);
4193         if (AscLoadMicroCode(iop_base, 0, &fw->data[4],
4194                              fw->size - 4) != chksum) {
4195                 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
4196                 release_firmware(fw);
4197                 return warn_code;
4198         }
4199         release_firmware(fw);
4200         warn_code |= AscInitMicroCodeVar(asc_dvc);
4201         if (!asc_dvc->overrun_dma)
4202                 return warn_code;
4203         asc_dvc->init_state |= ASC_INIT_STATE_END_LOAD_MC;
4204         AscEnableInterrupt(iop_base);
4205         return warn_code;
4206 }
4207
4208 /*
4209  * Load the Microcode
4210  *
4211  * Write the microcode image to RISC memory starting at address 0.
4212  *
4213  * The microcode is stored compressed in the following format:
4214  *
4215  *  254 word (508 byte) table indexed by byte code followed
4216  *  by the following byte codes:
4217  *
4218  *    1-Byte Code:
4219  *      00: Emit word 0 in table.
4220  *      01: Emit word 1 in table.
4221  *      .
4222  *      FD: Emit word 253 in table.
4223  *
4224  *    Multi-Byte Code:
4225  *      FE WW WW: (3 byte code) Word to emit is the next word WW WW.
4226  *      FF BB WW WW: (4 byte code) Emit BB count times next word WW WW.
4227  *
4228  * Returns 0 or an error if the checksum doesn't match
4229  */
4230 static int AdvLoadMicrocode(AdvPortAddr iop_base, const unsigned char *buf,
4231                             int size, int memsize, int chksum)
4232 {
4233         int i, j, end, len = 0;
4234         u32 sum;
4235
4236         AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4237
4238         for (i = 253 * 2; i < size; i++) {
4239                 if (buf[i] == 0xff) {
4240                         unsigned short word = (buf[i + 3] << 8) | buf[i + 2];
4241                         for (j = 0; j < buf[i + 1]; j++) {
4242                                 AdvWriteWordAutoIncLram(iop_base, word);
4243                                 len += 2;
4244                         }
4245                         i += 3;
4246                 } else if (buf[i] == 0xfe) {
4247                         unsigned short word = (buf[i + 2] << 8) | buf[i + 1];
4248                         AdvWriteWordAutoIncLram(iop_base, word);
4249                         i += 2;
4250                         len += 2;
4251                 } else {
4252                         unsigned int off = buf[i] * 2;
4253                         unsigned short word = (buf[off + 1] << 8) | buf[off];
4254                         AdvWriteWordAutoIncLram(iop_base, word);
4255                         len += 2;
4256                 }
4257         }
4258
4259         end = len;
4260
4261         while (len < memsize) {
4262                 AdvWriteWordAutoIncLram(iop_base, 0);
4263                 len += 2;
4264         }
4265
4266         /* Verify the microcode checksum. */
4267         sum = 0;
4268         AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4269
4270         for (len = 0; len < end; len += 2) {
4271                 sum += AdvReadWordAutoIncLram(iop_base);
4272         }
4273
4274         if (sum != chksum)
4275                 return ASC_IERR_MCODE_CHKSUM;
4276
4277         return 0;
4278 }
4279
4280 static void AdvBuildCarrierFreelist(struct adv_dvc_var *adv_dvc)
4281 {
4282         off_t carr_offset = 0, next_offset;
4283         dma_addr_t carr_paddr;
4284         int carr_num = ADV_CARRIER_BUFSIZE / sizeof(ADV_CARR_T), i;
4285
4286         for (i = 0; i < carr_num; i++) {
4287                 carr_offset = i * sizeof(ADV_CARR_T);
4288                 /* Get physical address of the carrier 'carrp'. */
4289                 carr_paddr = adv_dvc->carrier_addr + carr_offset;
4290
4291                 adv_dvc->carrier[i].carr_pa = cpu_to_le32(carr_paddr);
4292                 adv_dvc->carrier[i].carr_va = cpu_to_le32(carr_offset);
4293                 adv_dvc->carrier[i].areq_vpa = 0;
4294                 next_offset = carr_offset + sizeof(ADV_CARR_T);
4295                 if (i == carr_num)
4296                         next_offset = ~0;
4297                 adv_dvc->carrier[i].next_vpa = cpu_to_le32(next_offset);
4298         }
4299         /*
4300          * We cannot have a carrier with 'carr_va' of '0', as
4301          * a reference to this carrier would be interpreted as
4302          * list termination.
4303          * So start at carrier 1 with the freelist.
4304          */
4305         adv_dvc->carr_freelist = &adv_dvc->carrier[1];
4306 }
4307
4308 static ADV_CARR_T *adv_get_carrier(struct adv_dvc_var *adv_dvc, u32 offset)
4309 {
4310         int index;
4311
4312         BUG_ON(offset > ADV_CARRIER_BUFSIZE);
4313
4314         index = offset / sizeof(ADV_CARR_T);
4315         return &adv_dvc->carrier[index];
4316 }
4317
4318 static ADV_CARR_T *adv_get_next_carrier(struct adv_dvc_var *adv_dvc)
4319 {
4320         ADV_CARR_T *carrp = adv_dvc->carr_freelist;
4321         u32 next_vpa = le32_to_cpu(carrp->next_vpa);
4322
4323         if (next_vpa == 0 || next_vpa == ~0) {
4324                 ASC_DBG(1, "invalid vpa offset 0x%x\n", next_vpa);
4325                 return NULL;
4326         }
4327
4328         adv_dvc->carr_freelist = adv_get_carrier(adv_dvc, next_vpa);
4329         /*
4330          * insert stopper carrier to terminate list
4331          */
4332         carrp->next_vpa = cpu_to_le32(ASC_CQ_STOPPER);
4333
4334         return carrp;
4335 }
4336
4337 /*
4338  * 'offset' is the index in the request pointer array
4339  */
4340 static adv_req_t * adv_get_reqp(struct adv_dvc_var *adv_dvc, u32 offset)
4341 {
4342         struct asc_board *boardp = adv_dvc->drv_ptr;
4343
4344         BUG_ON(offset > adv_dvc->max_host_qng);
4345         return &boardp->adv_reqp[offset];
4346 }
4347
4348 /*
4349  * Send an idle command to the chip and wait for completion.
4350  *
4351  * Command completion is polled for once per microsecond.
4352  *
4353  * The function can be called from anywhere including an interrupt handler.
4354  * But the function is not re-entrant, so it uses the DvcEnter/LeaveCritical()
4355  * functions to prevent reentrancy.
4356  *
4357  * Return Values:
4358  *   ADV_TRUE - command completed successfully
4359  *   ADV_FALSE - command failed
4360  *   ADV_ERROR - command timed out
4361  */
4362 static int
4363 AdvSendIdleCmd(ADV_DVC_VAR *asc_dvc,
4364                ushort idle_cmd, u32 idle_cmd_parameter)
4365 {
4366         int result, i, j;
4367         AdvPortAddr iop_base;
4368
4369         iop_base = asc_dvc->iop_base;
4370
4371         /*
4372          * Clear the idle command status which is set by the microcode
4373          * to a non-zero value to indicate when the command is completed.
4374          * The non-zero result is one of the IDLE_CMD_STATUS_* values
4375          */
4376         AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS, (ushort)0);
4377
4378         /*
4379          * Write the idle command value after the idle command parameter
4380          * has been written to avoid a race condition. If the order is not
4381          * followed, the microcode may process the idle command before the
4382          * parameters have been written to LRAM.
4383          */
4384         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IDLE_CMD_PARAMETER,
4385                                 cpu_to_le32(idle_cmd_parameter));
4386         AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD, idle_cmd);
4387
4388         /*
4389          * Tickle the RISC to tell it to process the idle command.
4390          */
4391         AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_B);
4392         if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
4393                 /*
4394                  * Clear the tickle value. In the ASC-3550 the RISC flag
4395                  * command 'clr_tickle_b' does not work unless the host
4396                  * value is cleared.
4397                  */
4398                 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_NOP);
4399         }
4400
4401         /* Wait for up to 100 millisecond for the idle command to timeout. */
4402         for (i = 0; i < SCSI_WAIT_100_MSEC; i++) {
4403                 /* Poll once each microsecond for command completion. */
4404                 for (j = 0; j < SCSI_US_PER_MSEC; j++) {
4405                         AdvReadWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS,
4406                                         result);
4407                         if (result != 0)
4408                                 return result;
4409                         udelay(1);
4410                 }
4411         }
4412
4413         BUG();          /* The idle command should never timeout. */
4414         return ADV_ERROR;
4415 }
4416
4417 /*
4418  * Reset SCSI Bus and purge all outstanding requests.
4419  *
4420  * Return Value:
4421  *      ADV_TRUE(1) -   All requests are purged and SCSI Bus is reset.
4422  *      ADV_FALSE(0) -  Microcode command failed.
4423  *      ADV_ERROR(-1) - Microcode command timed-out. Microcode or IC
4424  *                      may be hung which requires driver recovery.
4425  */
4426 static int AdvResetSB(ADV_DVC_VAR *asc_dvc)
4427 {
4428         int status;
4429
4430         /*
4431          * Send the SCSI Bus Reset idle start idle command which asserts
4432          * the SCSI Bus Reset signal.
4433          */
4434         status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_START, 0L);
4435         if (status != ADV_TRUE) {
4436                 return status;
4437         }
4438
4439         /*
4440          * Delay for the specified SCSI Bus Reset hold time.
4441          *
4442          * The hold time delay is done on the host because the RISC has no
4443          * microsecond accurate timer.
4444          */
4445         udelay(ASC_SCSI_RESET_HOLD_TIME_US);
4446
4447         /*
4448          * Send the SCSI Bus Reset end idle command which de-asserts
4449          * the SCSI Bus Reset signal and purges any pending requests.
4450          */
4451         status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_END, 0L);
4452         if (status != ADV_TRUE) {
4453                 return status;
4454         }
4455
4456         mdelay(asc_dvc->scsi_reset_wait * 1000);        /* XXX: msleep? */
4457
4458         return status;
4459 }
4460
4461 /*
4462  * Initialize the ASC-3550.
4463  *
4464  * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4465  *
4466  * For a non-fatal error return a warning code. If there are no warnings
4467  * then 0 is returned.
4468  *
4469  * Needed after initialization for error recovery.
4470  */
4471 static int AdvInitAsc3550Driver(ADV_DVC_VAR *asc_dvc)
4472 {
4473         const struct firmware *fw;
4474         const char fwname[] = "advansys/3550.bin";
4475         AdvPortAddr iop_base;
4476         ushort warn_code;
4477         int begin_addr;
4478         int end_addr;
4479         ushort code_sum;
4480         int word;
4481         int i;
4482         int err;
4483         unsigned long chksum;
4484         ushort scsi_cfg1;
4485         uchar tid;
4486         ushort bios_mem[ASC_MC_BIOSLEN / 2];    /* BIOS RISC Memory 0x40-0x8F. */
4487         ushort wdtr_able = 0, sdtr_able, tagqng_able;
4488         uchar max_cmd[ADV_MAX_TID + 1];
4489
4490         /* If there is already an error, don't continue. */
4491         if (asc_dvc->err_code != 0)
4492                 return ADV_ERROR;
4493
4494         /*
4495          * The caller must set 'chip_type' to ADV_CHIP_ASC3550.
4496          */
4497         if (asc_dvc->chip_type != ADV_CHIP_ASC3550) {
4498                 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
4499                 return ADV_ERROR;
4500         }
4501
4502         warn_code = 0;
4503         iop_base = asc_dvc->iop_base;
4504
4505         /*
4506          * Save the RISC memory BIOS region before writing the microcode.
4507          * The BIOS may already be loaded and using its RISC LRAM region
4508          * so its region must be saved and restored.
4509          *
4510          * Note: This code makes the assumption, which is currently true,
4511          * that a chip reset does not clear RISC LRAM.
4512          */
4513         for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4514                 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4515                                 bios_mem[i]);
4516         }
4517
4518         /*
4519          * Save current per TID negotiated values.
4520          */
4521         if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] == 0x55AA) {
4522                 ushort bios_version, major, minor;
4523
4524                 bios_version =
4525                     bios_mem[(ASC_MC_BIOS_VERSION - ASC_MC_BIOSMEM) / 2];
4526                 major = (bios_version >> 12) & 0xF;
4527                 minor = (bios_version >> 8) & 0xF;
4528                 if (major < 3 || (major == 3 && minor == 1)) {
4529                         /* BIOS 3.1 and earlier location of 'wdtr_able' variable. */
4530                         AdvReadWordLram(iop_base, 0x120, wdtr_able);
4531                 } else {
4532                         AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4533                 }
4534         }
4535         AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4536         AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4537         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4538                 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4539                                 max_cmd[tid]);
4540         }
4541
4542         err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4543         if (err) {
4544                 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4545                        fwname, err);
4546                 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
4547                 return err;
4548         }
4549         if (fw->size < 4) {
4550                 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4551                        fw->size, fwname);
4552                 release_firmware(fw);
4553                 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
4554                 return -EINVAL;
4555         }
4556         chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4557                  (fw->data[1] << 8) | fw->data[0];
4558         asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
4559                                              fw->size - 4, ADV_3550_MEMSIZE,
4560                                              chksum);
4561         release_firmware(fw);
4562         if (asc_dvc->err_code)
4563                 return ADV_ERROR;
4564
4565         /*
4566          * Restore the RISC memory BIOS region.
4567          */
4568         for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4569                 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4570                                  bios_mem[i]);
4571         }
4572
4573         /*
4574          * Calculate and write the microcode code checksum to the microcode
4575          * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
4576          */
4577         AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
4578         AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
4579         code_sum = 0;
4580         AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
4581         for (word = begin_addr; word < end_addr; word += 2) {
4582                 code_sum += AdvReadWordAutoIncLram(iop_base);
4583         }
4584         AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
4585
4586         /*
4587          * Read and save microcode version and date.
4588          */
4589         AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
4590                         asc_dvc->cfg->mcode_date);
4591         AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
4592                         asc_dvc->cfg->mcode_version);
4593
4594         /*
4595          * Set the chip type to indicate the ASC3550.
4596          */
4597         AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC3550);
4598
4599         /*
4600          * If the PCI Configuration Command Register "Parity Error Response
4601          * Control" Bit was clear (0), then set the microcode variable
4602          * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
4603          * to ignore DMA parity errors.
4604          */
4605         if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
4606                 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4607                 word |= CONTROL_FLAG_IGNORE_PERR;
4608                 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4609         }
4610
4611         /*
4612          * For ASC-3550, setting the START_CTL_EMFU [3:2] bits sets a FIFO
4613          * threshold of 128 bytes. This register is only accessible to the host.
4614          */
4615         AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
4616                              START_CTL_EMFU | READ_CMD_MRM);
4617
4618         /*
4619          * Microcode operating variables for WDTR, SDTR, and command tag
4620          * queuing will be set in slave_configure() based on what a
4621          * device reports it is capable of in Inquiry byte 7.
4622          *
4623          * If SCSI Bus Resets have been disabled, then directly set
4624          * SDTR and WDTR from the EEPROM configuration. This will allow
4625          * the BIOS and warm boot to work without a SCSI bus hang on
4626          * the Inquiry caused by host and target mismatched DTR values.
4627          * Without the SCSI Bus Reset, before an Inquiry a device can't
4628          * be assumed to be in Asynchronous, Narrow mode.
4629          */
4630         if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
4631                 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
4632                                  asc_dvc->wdtr_able);
4633                 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
4634                                  asc_dvc->sdtr_able);
4635         }
4636
4637         /*
4638          * Set microcode operating variables for SDTR_SPEED1, SDTR_SPEED2,
4639          * SDTR_SPEED3, and SDTR_SPEED4 based on the ULTRA EEPROM per TID
4640          * bitmask. These values determine the maximum SDTR speed negotiated
4641          * with a device.
4642          *
4643          * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
4644          * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
4645          * without determining here whether the device supports SDTR.
4646          *
4647          * 4-bit speed  SDTR speed name
4648          * ===========  ===============
4649          * 0000b (0x0)  SDTR disabled
4650          * 0001b (0x1)  5 Mhz
4651          * 0010b (0x2)  10 Mhz
4652          * 0011b (0x3)  20 Mhz (Ultra)
4653          * 0100b (0x4)  40 Mhz (LVD/Ultra2)
4654          * 0101b (0x5)  80 Mhz (LVD2/Ultra3)
4655          * 0110b (0x6)  Undefined
4656          * .
4657          * 1111b (0xF)  Undefined
4658          */
4659         word = 0;
4660         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4661                 if (ADV_TID_TO_TIDMASK(tid) & asc_dvc->ultra_able) {
4662                         /* Set Ultra speed for TID 'tid'. */
4663                         word |= (0x3 << (4 * (tid % 4)));
4664                 } else {
4665                         /* Set Fast speed for TID 'tid'. */
4666                         word |= (0x2 << (4 * (tid % 4)));
4667                 }
4668                 if (tid == 3) { /* Check if done with sdtr_speed1. */
4669                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, word);
4670                         word = 0;
4671                 } else if (tid == 7) {  /* Check if done with sdtr_speed2. */
4672                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, word);
4673                         word = 0;
4674                 } else if (tid == 11) { /* Check if done with sdtr_speed3. */
4675                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, word);
4676                         word = 0;
4677                 } else if (tid == 15) { /* Check if done with sdtr_speed4. */
4678                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, word);
4679                         /* End of loop. */
4680                 }
4681         }
4682
4683         /*
4684          * Set microcode operating variable for the disconnect per TID bitmask.
4685          */
4686         AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
4687                          asc_dvc->cfg->disc_enable);
4688
4689         /*
4690          * Set SCSI_CFG0 Microcode Default Value.
4691          *
4692          * The microcode will set the SCSI_CFG0 register using this value
4693          * after it is started below.
4694          */
4695         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
4696                          PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
4697                          asc_dvc->chip_scsi_id);
4698
4699         /*
4700          * Determine SCSI_CFG1 Microcode Default Value.
4701          *
4702          * The microcode will set the SCSI_CFG1 register using this value
4703          * after it is started below.
4704          */
4705
4706         /* Read current SCSI_CFG1 Register value. */
4707         scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
4708
4709         /*
4710          * If all three connectors are in use, return an error.
4711          */
4712         if ((scsi_cfg1 & CABLE_ILLEGAL_A) == 0 ||
4713             (scsi_cfg1 & CABLE_ILLEGAL_B) == 0) {
4714                 asc_dvc->err_code |= ASC_IERR_ILLEGAL_CONNECTION;
4715                 return ADV_ERROR;
4716         }
4717
4718         /*
4719          * If the internal narrow cable is reversed all of the SCSI_CTRL
4720          * register signals will be set. Check for and return an error if
4721          * this condition is found.
4722          */
4723         if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
4724                 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
4725                 return ADV_ERROR;
4726         }
4727
4728         /*
4729          * If this is a differential board and a single-ended device
4730          * is attached to one of the connectors, return an error.
4731          */
4732         if ((scsi_cfg1 & DIFF_MODE) && (scsi_cfg1 & DIFF_SENSE) == 0) {
4733                 asc_dvc->err_code |= ASC_IERR_SINGLE_END_DEVICE;
4734                 return ADV_ERROR;
4735         }
4736
4737         /*
4738          * If automatic termination control is enabled, then set the
4739          * termination value based on a table listed in a_condor.h.
4740          *
4741          * If manual termination was specified with an EEPROM setting
4742          * then 'termination' was set-up in AdvInitFrom3550EEPROM() and
4743          * is ready to be 'ored' into SCSI_CFG1.
4744          */
4745         if (asc_dvc->cfg->termination == 0) {
4746                 /*
4747                  * The software always controls termination by setting TERM_CTL_SEL.
4748                  * If TERM_CTL_SEL were set to 0, the hardware would set termination.
4749                  */
4750                 asc_dvc->cfg->termination |= TERM_CTL_SEL;
4751
4752                 switch (scsi_cfg1 & CABLE_DETECT) {
4753                         /* TERM_CTL_H: on, TERM_CTL_L: on */
4754                 case 0x3:
4755                 case 0x7:
4756                 case 0xB:
4757                 case 0xD:
4758                 case 0xE:
4759                 case 0xF:
4760                         asc_dvc->cfg->termination |= (TERM_CTL_H | TERM_CTL_L);
4761                         break;
4762
4763                         /* TERM_CTL_H: on, TERM_CTL_L: off */
4764                 case 0x1:
4765                 case 0x5:
4766                 case 0x9:
4767                 case 0xA:
4768                 case 0xC:
4769                         asc_dvc->cfg->termination |= TERM_CTL_H;
4770                         break;
4771
4772                         /* TERM_CTL_H: off, TERM_CTL_L: off */
4773                 case 0x2:
4774                 case 0x6:
4775                         break;
4776                 }
4777         }
4778
4779         /*
4780          * Clear any set TERM_CTL_H and TERM_CTL_L bits.
4781          */
4782         scsi_cfg1 &= ~TERM_CTL;
4783
4784         /*
4785          * Invert the TERM_CTL_H and TERM_CTL_L bits and then
4786          * set 'scsi_cfg1'. The TERM_POL bit does not need to be
4787          * referenced, because the hardware internally inverts
4788          * the Termination High and Low bits if TERM_POL is set.
4789          */
4790         scsi_cfg1 |= (TERM_CTL_SEL | (~asc_dvc->cfg->termination & TERM_CTL));
4791
4792         /*
4793          * Set SCSI_CFG1 Microcode Default Value
4794          *
4795          * Set filter value and possibly modified termination control
4796          * bits in the Microcode SCSI_CFG1 Register Value.
4797          *
4798          * The microcode will set the SCSI_CFG1 register using this value
4799          * after it is started below.
4800          */
4801         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1,
4802                          FLTR_DISABLE | scsi_cfg1);
4803
4804         /*
4805          * Set MEM_CFG Microcode Default Value
4806          *
4807          * The microcode will set the MEM_CFG register using this value
4808          * after it is started below.
4809          *
4810          * MEM_CFG may be accessed as a word or byte, but only bits 0-7
4811          * are defined.
4812          *
4813          * ASC-3550 has 8KB internal memory.
4814          */
4815         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
4816                          BIOS_EN | RAM_SZ_8KB);
4817
4818         /*
4819          * Set SEL_MASK Microcode Default Value
4820          *
4821          * The microcode will set the SEL_MASK register using this value
4822          * after it is started below.
4823          */
4824         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
4825                          ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
4826
4827         AdvBuildCarrierFreelist(asc_dvc);
4828
4829         /*
4830          * Set-up the Host->RISC Initiator Command Queue (ICQ).
4831          */
4832
4833         asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
4834         if (!asc_dvc->icq_sp) {
4835                 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4836                 return ADV_ERROR;
4837         }
4838
4839         /*
4840          * Set RISC ICQ physical address start value.
4841          */
4842         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
4843
4844         /*
4845          * Set-up the RISC->Host Initiator Response Queue (IRQ).
4846          */
4847         asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
4848         if (!asc_dvc->irq_sp) {
4849                 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4850                 return ADV_ERROR;
4851         }
4852
4853         /*
4854          * Set RISC IRQ physical address start value.
4855          */
4856         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
4857         asc_dvc->carr_pending_cnt = 0;
4858
4859         AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
4860                              (ADV_INTR_ENABLE_HOST_INTR |
4861                               ADV_INTR_ENABLE_GLOBAL_INTR));
4862
4863         AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
4864         AdvWriteWordRegister(iop_base, IOPW_PC, word);
4865
4866         /* finally, finally, gentlemen, start your engine */
4867         AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
4868
4869         /*
4870          * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
4871          * Resets should be performed. The RISC has to be running
4872          * to issue a SCSI Bus Reset.
4873          */
4874         if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
4875                 /*
4876                  * If the BIOS Signature is present in memory, restore the
4877                  * BIOS Handshake Configuration Table and do not perform
4878                  * a SCSI Bus Reset.
4879                  */
4880                 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
4881                     0x55AA) {
4882                         /*
4883                          * Restore per TID negotiated values.
4884                          */
4885                         AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4886                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4887                         AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
4888                                          tagqng_able);
4889                         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4890                                 AdvWriteByteLram(iop_base,
4891                                                  ASC_MC_NUMBER_OF_MAX_CMD + tid,
4892                                                  max_cmd[tid]);
4893                         }
4894                 } else {
4895                         if (AdvResetSB(asc_dvc) != ADV_TRUE) {
4896                                 warn_code = ASC_WARN_BUSRESET_ERROR;
4897                         }
4898                 }
4899         }
4900
4901         return warn_code;
4902 }
4903
4904 /*
4905  * Initialize the ASC-38C0800.
4906  *
4907  * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4908  *
4909  * For a non-fatal error return a warning code. If there are no warnings
4910  * then 0 is returned.
4911  *
4912  * Needed after initialization for error recovery.
4913  */
4914 static int AdvInitAsc38C0800Driver(ADV_DVC_VAR *asc_dvc)
4915 {
4916         const struct firmware *fw;
4917         const char fwname[] = "advansys/38C0800.bin";
4918         AdvPortAddr iop_base;
4919         ushort warn_code;
4920         int begin_addr;
4921         int end_addr;
4922         ushort code_sum;
4923         int word;
4924         int i;
4925         int err;
4926         unsigned long chksum;
4927         ushort scsi_cfg1;
4928         uchar byte;
4929         uchar tid;
4930         ushort bios_mem[ASC_MC_BIOSLEN / 2];    /* BIOS RISC Memory 0x40-0x8F. */
4931         ushort wdtr_able, sdtr_able, tagqng_able;
4932         uchar max_cmd[ADV_MAX_TID + 1];
4933
4934         /* If there is already an error, don't continue. */
4935         if (asc_dvc->err_code != 0)
4936                 return ADV_ERROR;
4937
4938         /*
4939          * The caller must set 'chip_type' to ADV_CHIP_ASC38C0800.
4940          */
4941         if (asc_dvc->chip_type != ADV_CHIP_ASC38C0800) {
4942                 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
4943                 return ADV_ERROR;
4944         }
4945
4946         warn_code = 0;
4947         iop_base = asc_dvc->iop_base;
4948
4949         /*
4950          * Save the RISC memory BIOS region before writing the microcode.
4951          * The BIOS may already be loaded and using its RISC LRAM region
4952          * so its region must be saved and restored.
4953          *
4954          * Note: This code makes the assumption, which is currently true,
4955          * that a chip reset does not clear RISC LRAM.
4956          */
4957         for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4958                 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4959                                 bios_mem[i]);
4960         }
4961
4962         /*
4963          * Save current per TID negotiated values.
4964          */
4965         AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4966         AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4967         AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4968         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4969                 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4970                                 max_cmd[tid]);
4971         }
4972
4973         /*
4974          * RAM BIST (RAM Built-In Self Test)
4975          *
4976          * Address : I/O base + offset 0x38h register (byte).
4977          * Function: Bit 7-6(RW) : RAM mode
4978          *                          Normal Mode   : 0x00
4979          *                          Pre-test Mode : 0x40
4980          *                          RAM Test Mode : 0x80
4981          *           Bit 5       : unused
4982          *           Bit 4(RO)   : Done bit
4983          *           Bit 3-0(RO) : Status
4984          *                          Host Error    : 0x08
4985          *                          Int_RAM Error : 0x04
4986          *                          RISC Error    : 0x02
4987          *                          SCSI Error    : 0x01
4988          *                          No Error      : 0x00
4989          *
4990          * Note: RAM BIST code should be put right here, before loading the
4991          * microcode and after saving the RISC memory BIOS region.
4992          */
4993
4994         /*
4995          * LRAM Pre-test
4996          *
4997          * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
4998          * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
4999          * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5000          * to NORMAL_MODE, return an error too.
5001          */
5002         for (i = 0; i < 2; i++) {
5003                 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
5004                 mdelay(10);     /* Wait for 10ms before reading back. */
5005                 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5006                 if ((byte & RAM_TEST_DONE) == 0
5007                     || (byte & 0x0F) != PRE_TEST_VALUE) {
5008                         asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5009                         return ADV_ERROR;
5010                 }
5011
5012                 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5013                 mdelay(10);     /* Wait for 10ms before reading back. */
5014                 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5015                     != NORMAL_VALUE) {
5016                         asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5017                         return ADV_ERROR;
5018                 }
5019         }
5020
5021         /*
5022          * LRAM Test - It takes about 1.5 ms to run through the test.
5023          *
5024          * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5025          * If Done bit not set or Status not 0, save register byte, set the
5026          * err_code, and return an error.
5027          */
5028         AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
5029         mdelay(10);     /* Wait for 10ms before checking status. */
5030
5031         byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5032         if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5033                 /* Get here if Done bit not set or Status not 0. */
5034                 asc_dvc->bist_err_code = byte;  /* for BIOS display message */
5035                 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
5036                 return ADV_ERROR;
5037         }
5038
5039         /* We need to reset back to normal mode after LRAM test passes. */
5040         AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5041
5042         err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5043         if (err) {
5044                 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5045                        fwname, err);
5046                 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
5047                 return err;
5048         }
5049         if (fw->size < 4) {
5050                 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5051                        fw->size, fwname);
5052                 release_firmware(fw);
5053                 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
5054                 return -EINVAL;
5055         }
5056         chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5057                  (fw->data[1] << 8) | fw->data[0];
5058         asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5059                                              fw->size - 4, ADV_38C0800_MEMSIZE,
5060                                              chksum);
5061         release_firmware(fw);
5062         if (asc_dvc->err_code)
5063                 return ADV_ERROR;
5064
5065         /*
5066          * Restore the RISC memory BIOS region.
5067          */
5068         for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5069                 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5070                                  bios_mem[i]);
5071         }
5072
5073         /*
5074          * Calculate and write the microcode code checksum to the microcode
5075          * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5076          */
5077         AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5078         AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5079         code_sum = 0;
5080         AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5081         for (word = begin_addr; word < end_addr; word += 2) {
5082                 code_sum += AdvReadWordAutoIncLram(iop_base);
5083         }
5084         AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
5085
5086         /*
5087          * Read microcode version and date.
5088          */
5089         AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5090                         asc_dvc->cfg->mcode_date);
5091         AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5092                         asc_dvc->cfg->mcode_version);
5093
5094         /*
5095          * Set the chip type to indicate the ASC38C0800.
5096          */
5097         AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C0800);
5098
5099         /*
5100          * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5101          * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5102          * cable detection and then we are able to read C_DET[3:0].
5103          *
5104          * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5105          * Microcode Default Value' section below.
5106          */
5107         scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5108         AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5109                              scsi_cfg1 | DIS_TERM_DRV);
5110
5111         /*
5112          * If the PCI Configuration Command Register "Parity Error Response
5113          * Control" Bit was clear (0), then set the microcode variable
5114          * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5115          * to ignore DMA parity errors.
5116          */
5117         if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5118                 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5119                 word |= CONTROL_FLAG_IGNORE_PERR;
5120                 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5121         }
5122
5123         /*
5124          * For ASC-38C0800, set FIFO_THRESH_80B [6:4] bits and START_CTL_TH [3:2]
5125          * bits for the default FIFO threshold.
5126          *
5127          * Note: ASC-38C0800 FIFO threshold has been changed to 256 bytes.
5128          *
5129          * For DMA Errata #4 set the BC_THRESH_ENB bit.
5130          */
5131         AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5132                              BC_THRESH_ENB | FIFO_THRESH_80B | START_CTL_TH |
5133                              READ_CMD_MRM);
5134
5135         /*
5136          * Microcode operating variables for WDTR, SDTR, and command tag
5137          * queuing will be set in slave_configure() based on what a
5138          * device reports it is capable of in Inquiry byte 7.
5139          *
5140          * If SCSI Bus Resets have been disabled, then directly set
5141          * SDTR and WDTR from the EEPROM configuration. This will allow
5142          * the BIOS and warm boot to work without a SCSI bus hang on
5143          * the Inquiry caused by host and target mismatched DTR values.
5144          * Without the SCSI Bus Reset, before an Inquiry a device can't
5145          * be assumed to be in Asynchronous, Narrow mode.
5146          */
5147         if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5148                 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5149                                  asc_dvc->wdtr_able);
5150                 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5151                                  asc_dvc->sdtr_able);
5152         }
5153
5154         /*
5155          * Set microcode operating variables for DISC and SDTR_SPEED1,
5156          * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5157          * configuration values.
5158          *
5159          * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5160          * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5161          * without determining here whether the device supports SDTR.
5162          */
5163         AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5164                          asc_dvc->cfg->disc_enable);
5165         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5166         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5167         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5168         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
5169
5170         /*
5171          * Set SCSI_CFG0 Microcode Default Value.
5172          *
5173          * The microcode will set the SCSI_CFG0 register using this value
5174          * after it is started below.
5175          */
5176         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5177                          PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5178                          asc_dvc->chip_scsi_id);
5179
5180         /*
5181          * Determine SCSI_CFG1 Microcode Default Value.
5182          *
5183          * The microcode will set the SCSI_CFG1 register using this value
5184          * after it is started below.
5185          */
5186
5187         /* Read current SCSI_CFG1 Register value. */
5188         scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5189
5190         /*
5191          * If the internal narrow cable is reversed all of the SCSI_CTRL
5192          * register signals will be set. Check for and return an error if
5193          * this condition is found.
5194          */
5195         if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5196                 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5197                 return ADV_ERROR;
5198         }
5199
5200         /*
5201          * All kind of combinations of devices attached to one of four
5202          * connectors are acceptable except HVD device attached. For example,
5203          * LVD device can be attached to SE connector while SE device attached
5204          * to LVD connector.  If LVD device attached to SE connector, it only
5205          * runs up to Ultra speed.
5206          *
5207          * If an HVD device is attached to one of LVD connectors, return an
5208          * error.  However, there is no way to detect HVD device attached to
5209          * SE connectors.
5210          */
5211         if (scsi_cfg1 & HVD) {
5212                 asc_dvc->err_code = ASC_IERR_HVD_DEVICE;
5213                 return ADV_ERROR;
5214         }
5215
5216         /*
5217          * If either SE or LVD automatic termination control is enabled, then
5218          * set the termination value based on a table listed in a_condor.h.
5219          *
5220          * If manual termination was specified with an EEPROM setting then
5221          * 'termination' was set-up in AdvInitFrom38C0800EEPROM() and is ready
5222          * to be 'ored' into SCSI_CFG1.
5223          */
5224         if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
5225                 /* SE automatic termination control is enabled. */
5226                 switch (scsi_cfg1 & C_DET_SE) {
5227                         /* TERM_SE_HI: on, TERM_SE_LO: on */
5228                 case 0x1:
5229                 case 0x2:
5230                 case 0x3:
5231                         asc_dvc->cfg->termination |= TERM_SE;
5232                         break;
5233
5234                         /* TERM_SE_HI: on, TERM_SE_LO: off */
5235                 case 0x0:
5236                         asc_dvc->cfg->termination |= TERM_SE_HI;
5237                         break;
5238                 }
5239         }
5240
5241         if ((asc_dvc->cfg->termination & TERM_LVD) == 0) {
5242                 /* LVD automatic termination control is enabled. */
5243                 switch (scsi_cfg1 & C_DET_LVD) {
5244                         /* TERM_LVD_HI: on, TERM_LVD_LO: on */
5245                 case 0x4:
5246                 case 0x8:
5247                 case 0xC:
5248                         asc_dvc->cfg->termination |= TERM_LVD;
5249                         break;
5250
5251                         /* TERM_LVD_HI: off, TERM_LVD_LO: off */
5252                 case 0x0:
5253                         break;
5254                 }
5255         }
5256
5257         /*
5258          * Clear any set TERM_SE and TERM_LVD bits.
5259          */
5260         scsi_cfg1 &= (~TERM_SE & ~TERM_LVD);
5261
5262         /*
5263          * Invert the TERM_SE and TERM_LVD bits and then set 'scsi_cfg1'.
5264          */
5265         scsi_cfg1 |= (~asc_dvc->cfg->termination & 0xF0);
5266
5267         /*
5268          * Clear BIG_ENDIAN, DIS_TERM_DRV, Terminator Polarity and HVD/LVD/SE
5269          * bits and set possibly modified termination control bits in the
5270          * Microcode SCSI_CFG1 Register Value.
5271          */
5272         scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL & ~HVD_LVD_SE);
5273
5274         /*
5275          * Set SCSI_CFG1 Microcode Default Value
5276          *
5277          * Set possibly modified termination control and reset DIS_TERM_DRV
5278          * bits in the Microcode SCSI_CFG1 Register Value.
5279          *
5280          * The microcode will set the SCSI_CFG1 register using this value
5281          * after it is started below.
5282          */
5283         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
5284
5285         /*
5286          * Set MEM_CFG Microcode Default Value
5287          *
5288          * The microcode will set the MEM_CFG register using this value
5289          * after it is started below.
5290          *
5291          * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5292          * are defined.
5293          *
5294          * ASC-38C0800 has 16KB internal memory.
5295          */
5296         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5297                          BIOS_EN | RAM_SZ_16KB);
5298
5299         /*
5300          * Set SEL_MASK Microcode Default Value
5301          *
5302          * The microcode will set the SEL_MASK register using this value
5303          * after it is started below.
5304          */
5305         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5306                          ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
5307
5308         AdvBuildCarrierFreelist(asc_dvc);
5309
5310         /*
5311          * Set-up the Host->RISC Initiator Command Queue (ICQ).
5312          */
5313
5314         asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5315         if (!asc_dvc->icq_sp) {
5316                 ASC_DBG(0, "Failed to get ICQ carrier\n");
5317                 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5318                 return ADV_ERROR;
5319         }
5320
5321         /*
5322          * Set RISC ICQ physical address start value.
5323          * carr_pa is LE, must be native before write
5324          */
5325         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
5326
5327         /*
5328          * Set-up the RISC->Host Initiator Response Queue (IRQ).
5329          */
5330         asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5331         if (!asc_dvc->irq_sp) {
5332                 ASC_DBG(0, "Failed to get IRQ carrier\n");
5333                 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5334                 return ADV_ERROR;
5335         }
5336
5337         /*
5338          * Set RISC IRQ physical address start value.
5339          *
5340          * carr_pa is LE, must be native before write *
5341          */
5342         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5343         asc_dvc->carr_pending_cnt = 0;
5344
5345         AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5346                              (ADV_INTR_ENABLE_HOST_INTR |
5347                               ADV_INTR_ENABLE_GLOBAL_INTR));
5348
5349         AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5350         AdvWriteWordRegister(iop_base, IOPW_PC, word);
5351
5352         /* finally, finally, gentlemen, start your engine */
5353         AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
5354
5355         /*
5356          * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5357          * Resets should be performed. The RISC has to be running
5358          * to issue a SCSI Bus Reset.
5359          */
5360         if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5361                 /*
5362                  * If the BIOS Signature is present in memory, restore the
5363                  * BIOS Handshake Configuration Table and do not perform
5364                  * a SCSI Bus Reset.
5365                  */
5366                 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5367                     0x55AA) {
5368                         /*
5369                          * Restore per TID negotiated values.
5370                          */
5371                         AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5372                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5373                         AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5374                                          tagqng_able);
5375                         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5376                                 AdvWriteByteLram(iop_base,
5377                                                  ASC_MC_NUMBER_OF_MAX_CMD + tid,
5378                                                  max_cmd[tid]);
5379                         }
5380                 } else {
5381                         if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5382                                 warn_code = ASC_WARN_BUSRESET_ERROR;
5383                         }
5384                 }
5385         }
5386
5387         return warn_code;
5388 }
5389
5390 /*
5391  * Initialize the ASC-38C1600.
5392  *
5393  * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
5394  *
5395  * For a non-fatal error return a warning code. If there are no warnings
5396  * then 0 is returned.
5397  *
5398  * Needed after initialization for error recovery.
5399  */
5400 static int AdvInitAsc38C1600Driver(ADV_DVC_VAR *asc_dvc)
5401 {
5402         const struct firmware *fw;
5403         const char fwname[] = "advansys/38C1600.bin";
5404         AdvPortAddr iop_base;
5405         ushort warn_code;
5406         int begin_addr;
5407         int end_addr;
5408         ushort code_sum;
5409         long word;
5410         int i;
5411         int err;
5412         unsigned long chksum;
5413         ushort scsi_cfg1;
5414         uchar byte;
5415         uchar tid;
5416         ushort bios_mem[ASC_MC_BIOSLEN / 2];    /* BIOS RISC Memory 0x40-0x8F. */
5417         ushort wdtr_able, sdtr_able, ppr_able, tagqng_able;
5418         uchar max_cmd[ASC_MAX_TID + 1];
5419
5420         /* If there is already an error, don't continue. */
5421         if (asc_dvc->err_code != 0) {
5422                 return ADV_ERROR;
5423         }
5424
5425         /*
5426          * The caller must set 'chip_type' to ADV_CHIP_ASC38C1600.
5427          */
5428         if (asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
5429                 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
5430                 return ADV_ERROR;
5431         }
5432
5433         warn_code = 0;
5434         iop_base = asc_dvc->iop_base;
5435
5436         /*
5437          * Save the RISC memory BIOS region before writing the microcode.
5438          * The BIOS may already be loaded and using its RISC LRAM region
5439          * so its region must be saved and restored.
5440          *
5441          * Note: This code makes the assumption, which is currently true,
5442          * that a chip reset does not clear RISC LRAM.
5443          */
5444         for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5445                 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5446                                 bios_mem[i]);
5447         }
5448
5449         /*
5450          * Save current per TID negotiated values.
5451          */
5452         AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5453         AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5454         AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5455         AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5456         for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5457                 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5458                                 max_cmd[tid]);
5459         }
5460
5461         /*
5462          * RAM BIST (Built-In Self Test)
5463          *
5464          * Address : I/O base + offset 0x38h register (byte).
5465          * Function: Bit 7-6(RW) : RAM mode
5466          *                          Normal Mode   : 0x00
5467          *                          Pre-test Mode : 0x40
5468          *                          RAM Test Mode : 0x80
5469          *           Bit 5       : unused
5470          *           Bit 4(RO)   : Done bit
5471          *           Bit 3-0(RO) : Status
5472          *                          Host Error    : 0x08
5473          *                          Int_RAM Error : 0x04
5474          *                          RISC Error    : 0x02
5475          *                          SCSI Error    : 0x01
5476          *                          No Error      : 0x00
5477          *
5478          * Note: RAM BIST code should be put right here, before loading the
5479          * microcode and after saving the RISC memory BIOS region.
5480          */
5481
5482         /*
5483          * LRAM Pre-test
5484          *
5485          * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
5486          * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
5487          * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5488          * to NORMAL_MODE, return an error too.
5489          */
5490         for (i = 0; i < 2; i++) {
5491                 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
5492                 mdelay(10);     /* Wait for 10ms before reading back. */
5493                 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5494                 if ((byte & RAM_TEST_DONE) == 0
5495                     || (byte & 0x0F) != PRE_TEST_VALUE) {
5496                         asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5497                         return ADV_ERROR;
5498                 }
5499
5500                 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5501                 mdelay(10);     /* Wait for 10ms before reading back. */
5502                 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5503                     != NORMAL_VALUE) {
5504                         asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5505                         return ADV_ERROR;
5506                 }
5507         }
5508
5509         /*
5510          * LRAM Test - It takes about 1.5 ms to run through the test.
5511          *
5512          * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5513          * If Done bit not set or Status not 0, save register byte, set the
5514          * err_code, and return an error.
5515          */
5516         AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
5517         mdelay(10);     /* Wait for 10ms before checking status. */
5518
5519         byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5520         if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5521                 /* Get here if Done bit not set or Status not 0. */
5522                 asc_dvc->bist_err_code = byte;  /* for BIOS display message */
5523                 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
5524                 return ADV_ERROR;
5525         }
5526
5527         /* We need to reset back to normal mode after LRAM test passes. */
5528         AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5529
5530         err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5531         if (err) {
5532                 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5533                        fwname, err);
5534                 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
5535                 return err;
5536         }
5537         if (fw->size < 4) {
5538                 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5539                        fw->size, fwname);
5540                 release_firmware(fw);
5541                 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
5542                 return -EINVAL;
5543         }
5544         chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5545                  (fw->data[1] << 8) | fw->data[0];
5546         asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5547                                              fw->size - 4, ADV_38C1600_MEMSIZE,
5548                                              chksum);
5549         release_firmware(fw);
5550         if (asc_dvc->err_code)
5551                 return ADV_ERROR;
5552
5553         /*
5554          * Restore the RISC memory BIOS region.
5555          */
5556         for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5557                 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5558                                  bios_mem[i]);
5559         }
5560
5561         /*
5562          * Calculate and write the microcode code checksum to the microcode
5563          * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5564          */
5565         AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5566         AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5567         code_sum = 0;
5568         AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5569         for (word = begin_addr; word < end_addr; word += 2) {
5570                 code_sum += AdvReadWordAutoIncLram(iop_base);
5571         }
5572         AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
5573
5574         /*
5575          * Read microcode version and date.
5576          */
5577         AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5578                         asc_dvc->cfg->mcode_date);
5579         AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5580                         asc_dvc->cfg->mcode_version);
5581
5582         /*
5583          * Set the chip type to indicate the ASC38C1600.
5584          */
5585         AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C1600);
5586
5587         /*
5588          * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5589          * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5590          * cable detection and then we are able to read C_DET[3:0].
5591          *
5592          * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5593          * Microcode Default Value' section below.
5594          */
5595         scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5596         AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5597                              scsi_cfg1 | DIS_TERM_DRV);
5598
5599         /*
5600          * If the PCI Configuration Command Register "Parity Error Response
5601          * Control" Bit was clear (0), then set the microcode variable
5602          * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5603          * to ignore DMA parity errors.
5604          */
5605         if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5606                 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5607                 word |= CONTROL_FLAG_IGNORE_PERR;
5608                 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5609         }
5610
5611         /*
5612          * If the BIOS control flag AIPP (Asynchronous Information
5613          * Phase Protection) disable bit is not set, then set the firmware
5614          * 'control_flag' CONTROL_FLAG_ENABLE_AIPP bit to enable
5615          * AIPP checking and encoding.
5616          */
5617         if ((asc_dvc->bios_ctrl & BIOS_CTRL_AIPP_DIS) == 0) {
5618                 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5619                 word |= CONTROL_FLAG_ENABLE_AIPP;
5620                 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5621         }
5622
5623         /*
5624          * For ASC-38C1600 use DMA_CFG0 default values: FIFO_THRESH_80B [6:4],
5625          * and START_CTL_TH [3:2].
5626          */
5627         AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5628                              FIFO_THRESH_80B | START_CTL_TH | READ_CMD_MRM);
5629
5630         /*
5631          * Microcode operating variables for WDTR, SDTR, and command tag
5632          * queuing will be set in slave_configure() based on what a
5633          * device reports it is capable of in Inquiry byte 7.
5634          *
5635          * If SCSI Bus Resets have been disabled, then directly set
5636          * SDTR and WDTR from the EEPROM configuration. This will allow
5637          * the BIOS and warm boot to work without a SCSI bus hang on
5638          * the Inquiry caused by host and target mismatched DTR values.
5639          * Without the SCSI Bus Reset, before an Inquiry a device can't
5640          * be assumed to be in Asynchronous, Narrow mode.
5641          */
5642         if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5643                 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5644                                  asc_dvc->wdtr_able);
5645                 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5646                                  asc_dvc->sdtr_able);
5647         }
5648
5649         /*
5650          * Set microcode operating variables for DISC and SDTR_SPEED1,
5651          * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5652          * configuration values.
5653          *
5654          * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5655          * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5656          * without determining here whether the device supports SDTR.
5657          */
5658         AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5659                          asc_dvc->cfg->disc_enable);
5660         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5661         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5662         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5663         AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
5664
5665         /*
5666          * Set SCSI_CFG0 Microcode Default Value.
5667          *
5668          * The microcode will set the SCSI_CFG0 register using this value
5669          * after it is started below.
5670          */
5671         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5672                          PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5673                          asc_dvc->chip_scsi_id);
5674
5675         /*
5676          * Calculate SCSI_CFG1 Microcode Default Value.
5677          *
5678          * The microcode will set the SCSI_CFG1 register using this value
5679          * after it is started below.
5680          *
5681          * Each ASC-38C1600 function has only two cable detect bits.
5682          * The bus mode override bits are in IOPB_SOFT_OVER_WR.
5683          */
5684         scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5685
5686         /*
5687          * If the cable is reversed all of the SCSI_CTRL register signals
5688          * will be set. Check for and return an error if this condition is
5689          * found.
5690          */
5691         if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5692                 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5693                 return ADV_ERROR;
5694         }
5695
5696         /*
5697          * Each ASC-38C1600 function has two connectors. Only an HVD device
5698          * can not be connected to either connector. An LVD device or SE device
5699          * may be connected to either connecor. If an SE device is connected,
5700          * then at most Ultra speed (20 Mhz) can be used on both connectors.
5701          *
5702          * If an HVD device is attached, return an error.
5703          */
5704         if (scsi_cfg1 & HVD) {
5705                 asc_dvc->err_code |= ASC_IERR_HVD_DEVICE;
5706                 return ADV_ERROR;
5707         }
5708
5709         /*
5710          * Each function in the ASC-38C1600 uses only the SE cable detect and
5711          * termination because there are two connectors for each function. Each
5712          * function may use either LVD or SE mode. Corresponding the SE automatic
5713          * termination control EEPROM bits are used for each function. Each
5714          * function has its own EEPROM. If SE automatic control is enabled for
5715          * the function, then set the termination value based on a table listed
5716          * in a_condor.h.
5717          *
5718          * If manual termination is specified in the EEPROM for the function,
5719          * then 'termination' was set-up in AscInitFrom38C1600EEPROM() and is
5720          * ready to be 'ored' into SCSI_CFG1.
5721          */
5722         if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
5723                 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
5724                 /* SE automatic termination control is enabled. */
5725                 switch (scsi_cfg1 & C_DET_SE) {
5726                         /* TERM_SE_HI: on, TERM_SE_LO: on */
5727                 case 0x1:
5728                 case 0x2:
5729                 case 0x3:
5730                         asc_dvc->cfg->termination |= TERM_SE;
5731                         break;
5732
5733                 case 0x0:
5734                         if (PCI_FUNC(pdev->devfn) == 0) {
5735                                 /* Function 0 - TERM_SE_HI: off, TERM_SE_LO: off */
5736                         } else {
5737                                 /* Function 1 - TERM_SE_HI: on, TERM_SE_LO: off */
5738                                 asc_dvc->cfg->termination |= TERM_SE_HI;
5739                         }
5740                         break;
5741                 }
5742         }
5743
5744         /*
5745          * Clear any set TERM_SE bits.
5746          */
5747         scsi_cfg1 &= ~TERM_SE;
5748
5749         /*
5750          * Invert the TERM_SE bits and then set 'scsi_cfg1'.
5751          */
5752         scsi_cfg1 |= (~asc_dvc->cfg->termination & TERM_SE);
5753
5754         /*
5755          * Clear Big Endian and Terminator Polarity bits and set possibly
5756          * modified termination control bits in the Microcode SCSI_CFG1
5757          * Register Value.
5758          *
5759          * Big Endian bit is not used even on big endian machines.
5760          */
5761         scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL);
5762
5763         /*
5764          * Set SCSI_CFG1 Microcode Default Value
5765          *
5766          * Set possibly modified termination control bits in the Microcode
5767          * SCSI_CFG1 Register Value.
5768          *
5769          * The microcode will set the SCSI_CFG1 register using this value
5770          * after it is started below.
5771          */
5772         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
5773
5774         /*
5775          * Set MEM_CFG Microcode Default Value
5776          *
5777          * The microcode will set the MEM_CFG register using this value
5778          * after it is started below.
5779          *
5780          * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5781          * are defined.
5782          *
5783          * ASC-38C1600 has 32KB internal memory.
5784          *
5785          * XXX - Since ASC38C1600 Rev.3 has a Local RAM failure issue, we come
5786          * out a special 16K Adv Library and Microcode version. After the issue
5787          * resolved, we should turn back to the 32K support. Both a_condor.h and
5788          * mcode.sas files also need to be updated.
5789          *
5790          * AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5791          *  BIOS_EN | RAM_SZ_32KB);
5792          */
5793         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5794                          BIOS_EN | RAM_SZ_16KB);
5795
5796         /*
5797          * Set SEL_MASK Microcode Default Value
5798          *
5799          * The microcode will set the SEL_MASK register using this value
5800          * after it is started below.
5801          */
5802         AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5803                          ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
5804
5805         AdvBuildCarrierFreelist(asc_dvc);
5806
5807         /*
5808          * Set-up the Host->RISC Initiator Command Queue (ICQ).
5809          */
5810         asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5811         if (!asc_dvc->icq_sp) {
5812                 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5813                 return ADV_ERROR;
5814         }
5815
5816         /*
5817          * Set RISC ICQ physical address start value. Initialize the
5818          * COMMA register to the same value otherwise the RISC will
5819          * prematurely detect a command is available.
5820          */
5821         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
5822         AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
5823                               le32_to_cpu(asc_dvc->icq_sp->carr_pa));
5824
5825         /*
5826          * Set-up the RISC->Host Initiator Response Queue (IRQ).
5827          */
5828         asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5829         if (!asc_dvc->irq_sp) {
5830                 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5831                 return ADV_ERROR;
5832         }
5833
5834         /*
5835          * Set RISC IRQ physical address start value.
5836          */
5837         AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5838         asc_dvc->carr_pending_cnt = 0;
5839
5840         AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5841                              (ADV_INTR_ENABLE_HOST_INTR |
5842                               ADV_INTR_ENABLE_GLOBAL_INTR));
5843         AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5844         AdvWriteWordRegister(iop_base, IOPW_PC, word);
5845
5846         /* finally, finally, gentlemen, start your engine */
5847         AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
5848
5849         /*
5850          * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5851          * Resets should be performed. The RISC has to be running
5852          * to issue a SCSI Bus Reset.
5853          */
5854         if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5855                 /*
5856                  * If the BIOS Signature is present in memory, restore the
5857                  * per TID microcode operating variables.
5858                  */
5859                 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5860                     0x55AA) {
5861                         /*
5862                          * Restore per TID negotiated values.
5863                          */
5864                         AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5865                         AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5866                         AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5867                         AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5868                                          tagqng_able);
5869                         for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5870                                 AdvWriteByteLram(iop_base,
5871                                                  ASC_MC_NUMBER_OF_MAX_CMD + tid,
5872                                                  max_cmd[tid]);
5873                         }
5874                 } else {
5875                         if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5876                                 warn_code = ASC_WARN_BUSRESET_ERROR;
5877                         }
5878                 }
5879         }
5880
5881         return warn_code;
5882 }
5883
5884 /*
5885  * Reset chip and SCSI Bus.
5886  *
5887  * Return Value:
5888  *      ADV_TRUE(1) -   Chip re-initialization and SCSI Bus Reset successful.
5889  *      ADV_FALSE(0) -  Chip re-initialization and SCSI Bus Reset failure.
5890  */
5891 static int AdvResetChipAndSB(ADV_DVC_VAR *asc_dvc)
5892 {
5893         int status;
5894         ushort wdtr_able, sdtr_able, tagqng_able;
5895         ushort ppr_able = 0;
5896         uchar tid, max_cmd[ADV_MAX_TID + 1];
5897         AdvPortAddr iop_base;
5898         ushort bios_sig;
5899
5900         iop_base = asc_dvc->iop_base;
5901
5902         /*
5903          * Save current per TID negotiated values.
5904          */
5905         AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5906         AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5907         if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5908                 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5909         }
5910         AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5911         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5912                 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5913                                 max_cmd[tid]);
5914         }
5915
5916         /*
5917          * Force the AdvInitAsc3550/38C0800Driver() function to
5918          * perform a SCSI Bus Reset by clearing the BIOS signature word.
5919          * The initialization functions assumes a SCSI Bus Reset is not
5920          * needed if the BIOS signature word is present.
5921          */
5922         AdvReadWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5923         AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, 0);
5924
5925         /*
5926          * Stop chip and reset it.
5927          */
5928         AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_STOP);
5929         AdvWriteWordRegister(iop_base, IOPW_CTRL_REG, ADV_CTRL_REG_CMD_RESET);
5930         mdelay(100);
5931         AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
5932                              ADV_CTRL_REG_CMD_WR_IO_REG);
5933
5934         /*
5935          * Reset Adv Library error code, if any, and try
5936          * re-initializing the chip.
5937          */
5938         asc_dvc->err_code = 0;
5939         if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5940                 status = AdvInitAsc38C1600Driver(asc_dvc);
5941         } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
5942                 status = AdvInitAsc38C0800Driver(asc_dvc);
5943         } else {
5944                 status = AdvInitAsc3550Driver(asc_dvc);
5945         }
5946
5947         /* Translate initialization return value to status value. */
5948         if (status == 0) {
5949                 status = ADV_TRUE;
5950         } else {
5951                 status = ADV_FALSE;
5952         }
5953
5954         /*
5955          * Restore the BIOS signature word.
5956          */
5957         AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5958
5959         /*
5960          * Restore per TID negotiated values.
5961          */
5962         AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5963         AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5964         if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5965                 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5966         }
5967         AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5968         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5969                 AdvWriteByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5970                                  max_cmd[tid]);
5971         }
5972
5973         return status;
5974 }
5975
5976 /*
5977  * adv_async_callback() - Adv Library asynchronous event callback function.
5978  */
5979 static void adv_async_callback(ADV_DVC_VAR *adv_dvc_varp, uchar code)
5980 {
5981         switch (code) {
5982         case ADV_ASYNC_SCSI_BUS_RESET_DET:
5983                 /*
5984                  * The firmware detected a SCSI Bus reset.
5985                  */
5986                 ASC_DBG(0, "ADV_ASYNC_SCSI_BUS_RESET_DET\n");
5987                 break;
5988
5989         case ADV_ASYNC_RDMA_FAILURE:
5990                 /*
5991                  * Handle RDMA failure by resetting the SCSI Bus and
5992                  * possibly the chip if it is unresponsive. Log the error
5993                  * with a unique code.
5994                  */
5995                 ASC_DBG(0, "ADV_ASYNC_RDMA_FAILURE\n");
5996                 AdvResetChipAndSB(adv_dvc_varp);
5997                 break;
5998
5999         case ADV_HOST_SCSI_BUS_RESET:
6000                 /*
6001                  * Host generated SCSI bus reset occurred.
6002                  */
6003                 ASC_DBG(0, "ADV_HOST_SCSI_BUS_RESET\n");
6004                 break;
6005
6006         default:
6007                 ASC_DBG(0, "unknown code 0x%x\n", code);
6008                 break;
6009         }
6010 }
6011
6012 /*
6013  * adv_isr_callback() - Second Level Interrupt Handler called by AdvISR().
6014  *
6015  * Callback function for the Wide SCSI Adv Library.
6016  */
6017 static void adv_isr_callback(ADV_DVC_VAR *adv_dvc_varp, ADV_SCSI_REQ_Q *scsiqp)
6018 {
6019         struct asc_board *boardp = adv_dvc_varp->drv_ptr;
6020         u32 srb_tag;
6021         adv_req_t *reqp;
6022         adv_sgblk_t *sgblkp;
6023         struct scsi_cmnd *scp;
6024         u32 resid_cnt;
6025         dma_addr_t sense_addr;
6026
6027         ASC_DBG(1, "adv_dvc_varp 0x%p, scsiqp 0x%p\n",
6028                 adv_dvc_varp, scsiqp);
6029         ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
6030
6031         /*
6032          * Get the adv_req_t structure for the command that has been
6033          * completed. The adv_req_t structure actually contains the
6034          * completed ADV_SCSI_REQ_Q structure.
6035          */
6036         srb_tag = le32_to_cpu(scsiqp->srb_tag);
6037         scp = scsi_host_find_tag(boardp->shost, scsiqp->srb_tag);
6038
6039         ASC_DBG(1, "scp 0x%p\n", scp);
6040         if (scp == NULL) {
6041                 ASC_PRINT
6042                     ("adv_isr_callback: scp is NULL; adv_req_t dropped.\n");
6043                 return;
6044         }
6045         ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
6046
6047         reqp = (adv_req_t *)scp->host_scribble;
6048         ASC_DBG(1, "reqp 0x%lx\n", (ulong)reqp);
6049         if (reqp == NULL) {
6050                 ASC_PRINT("adv_isr_callback: reqp is NULL\n");
6051                 return;
6052         }
6053         /*
6054          * Remove backreferences to avoid duplicate
6055          * command completions.
6056          */
6057         scp->host_scribble = NULL;
6058         reqp->cmndp = NULL;
6059
6060         ASC_STATS(boardp->shost, callback);
6061         ASC_DBG(1, "shost 0x%p\n", boardp->shost);
6062
6063         sense_addr = le32_to_cpu(scsiqp->sense_addr);
6064         dma_unmap_single(boardp->dev, sense_addr,
6065                          SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
6066
6067         /*
6068          * 'done_status' contains the command's ending status.
6069          */
6070         switch (scsiqp->done_status) {
6071         case QD_NO_ERROR:
6072                 ASC_DBG(2, "QD_NO_ERROR\n");
6073                 scp->result = 0;
6074
6075                 /*
6076                  * Check for an underrun condition.
6077                  *
6078                  * If there was no error and an underrun condition, then
6079                  * then return the number of underrun bytes.
6080                  */
6081                 resid_cnt = le32_to_cpu(scsiqp->data_cnt);
6082                 if (scsi_bufflen(scp) != 0 && resid_cnt != 0 &&
6083                     resid_cnt <= scsi_bufflen(scp)) {
6084                         ASC_DBG(1, "underrun condition %lu bytes\n",
6085                                  (ulong)resid_cnt);
6086                         scsi_set_resid(scp, resid_cnt);
6087                 }
6088                 break;
6089
6090         case QD_WITH_ERROR:
6091                 ASC_DBG(2, "QD_WITH_ERROR\n");
6092                 switch (scsiqp->host_status) {
6093                 case QHSTA_NO_ERROR:
6094                         if (scsiqp->scsi_status == SAM_STAT_CHECK_CONDITION) {
6095                                 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
6096                                 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
6097                                                   SCSI_SENSE_BUFFERSIZE);
6098                                 /*
6099                                  * Note: The 'status_byte()' macro used by
6100                                  * target drivers defined in scsi.h shifts the
6101                                  * status byte returned by host drivers right
6102                                  * by 1 bit.  This is why target drivers also
6103                                  * use right shifted status byte definitions.
6104                                  * For instance target drivers use
6105                                  * CHECK_CONDITION, defined to 0x1, instead of
6106                                  * the SCSI defined check condition value of
6107                                  * 0x2. Host drivers are supposed to return
6108                                  * the status byte as it is defined by SCSI.
6109                                  */
6110                                 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
6111                                     STATUS_BYTE(scsiqp->scsi_status);
6112                         } else {
6113                                 scp->result = STATUS_BYTE(scsiqp->scsi_status);
6114                         }
6115                         break;
6116
6117                 default:
6118                         /* Some other QHSTA error occurred. */
6119                         ASC_DBG(1, "host_status 0x%x\n", scsiqp->host_status);
6120                         scp->result = HOST_BYTE(DID_BAD_TARGET);
6121                         break;
6122                 }
6123                 break;
6124
6125         case QD_ABORTED_BY_HOST:
6126                 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
6127                 scp->result =
6128                     HOST_BYTE(DID_ABORT) | STATUS_BYTE(scsiqp->scsi_status);
6129                 break;
6130
6131         default:
6132                 ASC_DBG(1, "done_status 0x%x\n", scsiqp->done_status);
6133                 scp->result =
6134                     HOST_BYTE(DID_ERROR) | STATUS_BYTE(scsiqp->scsi_status);
6135                 break;
6136         }
6137
6138         /*
6139          * If the 'init_tidmask' bit isn't already set for the target and the
6140          * current request finished normally, then set the bit for the target
6141          * to indicate that a device is present.
6142          */
6143         if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
6144             scsiqp->done_status == QD_NO_ERROR &&
6145             scsiqp->host_status == QHSTA_NO_ERROR) {
6146                 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
6147         }
6148
6149         asc_scsi_done(scp);
6150
6151         /*
6152          * Free all 'adv_sgblk_t' structures allocated for the request.
6153          */
6154         while ((sgblkp = reqp->sgblkp) != NULL) {
6155                 /* Remove 'sgblkp' from the request list. */
6156                 reqp->sgblkp = sgblkp->next_sgblkp;
6157
6158                 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
6159                               sgblkp->sg_addr);
6160         }
6161
6162         ASC_DBG(1, "done\n");
6163 }
6164
6165 /*
6166  * Adv Library Interrupt Service Routine
6167  *
6168  *  This function is called by a driver's interrupt service routine.
6169  *  The function disables and re-enables interrupts.
6170  *
6171  *  When a microcode idle command is completed, the ADV_DVC_VAR
6172  *  'idle_cmd_done' field is set to ADV_TRUE.
6173  *
6174  *  Note: AdvISR() can be called when interrupts are disabled or even
6175  *  when there is no hardware interrupt condition present. It will
6176  *  always check for completed idle commands and microcode requests.
6177  *  This is an important feature that shouldn't be changed because it
6178  *  allows commands to be completed from polling mode loops.
6179  *
6180  * Return:
6181  *   ADV_TRUE(1) - interrupt was pending
6182  *   ADV_FALSE(0) - no interrupt was pending
6183  */
6184 static int AdvISR(ADV_DVC_VAR *asc_dvc)
6185 {
6186         AdvPortAddr iop_base;
6187         uchar int_stat;
6188         ushort target_bit;
6189         ADV_CARR_T *free_carrp;
6190         __le32 irq_next_vpa;
6191         ADV_SCSI_REQ_Q *scsiq;
6192         adv_req_t *reqp;
6193
6194         iop_base = asc_dvc->iop_base;
6195
6196         /* Reading the register clears the interrupt. */
6197         int_stat = AdvReadByteRegister(iop_base, IOPB_INTR_STATUS_REG);
6198
6199         if ((int_stat & (ADV_INTR_STATUS_INTRA | ADV_INTR_STATUS_INTRB |
6200                          ADV_INTR_STATUS_INTRC)) == 0) {
6201                 return ADV_FALSE;
6202         }
6203
6204         /*
6205          * Notify the driver of an asynchronous microcode condition by
6206          * calling the adv_async_callback function. The function
6207          * is passed the microcode ASC_MC_INTRB_CODE byte value.
6208          */
6209         if (int_stat & ADV_INTR_STATUS_INTRB) {
6210                 uchar intrb_code;
6211
6212                 AdvReadByteLram(iop_base, ASC_MC_INTRB_CODE, intrb_code);
6213
6214                 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
6215                     asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
6216                         if (intrb_code == ADV_ASYNC_CARRIER_READY_FAILURE &&
6217                             asc_dvc->carr_pending_cnt != 0) {
6218                                 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
6219                                                      ADV_TICKLE_A);
6220                                 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
6221                                         AdvWriteByteRegister(iop_base,
6222                                                              IOPB_TICKLE,
6223                                                              ADV_TICKLE_NOP);
6224                                 }
6225                         }
6226                 }
6227
6228                 adv_async_callback(asc_dvc, intrb_code);
6229         }
6230
6231         /*
6232          * Check if the IRQ stopper carrier contains a completed request.
6233          */
6234         while (((irq_next_vpa =
6235                  le32_to_cpu(asc_dvc->irq_sp->next_vpa)) & ASC_RQ_DONE) != 0) {
6236                 /*
6237                  * Get a pointer to the newly completed ADV_SCSI_REQ_Q structure.
6238                  * The RISC will have set 'areq_vpa' to a virtual address.
6239                  *
6240                  * The firmware will have copied the ASC_SCSI_REQ_Q.scsiq_ptr
6241                  * field to the carrier ADV_CARR_T.areq_vpa field. The conversion
6242                  * below complements the conversion of ASC_SCSI_REQ_Q.scsiq_ptr'
6243                  * in AdvExeScsiQueue().
6244                  */
6245                 u32 pa_offset = le32_to_cpu(asc_dvc->irq_sp->areq_vpa);
6246                 ASC_DBG(1, "irq_sp %p areq_vpa %u\n",
6247                         asc_dvc->irq_sp, pa_offset);
6248                 reqp = adv_get_reqp(asc_dvc, pa_offset);
6249                 scsiq = &reqp->scsi_req_q;
6250
6251                 /*
6252                  * Request finished with good status and the queue was not
6253                  * DMAed to host memory by the firmware. Set all status fields
6254                  * to indicate good status.
6255                  */
6256                 if ((irq_next_vpa & ASC_RQ_GOOD) != 0) {
6257                         scsiq->done_status = QD_NO_ERROR;
6258                         scsiq->host_status = scsiq->scsi_status = 0;
6259                         scsiq->data_cnt = 0L;
6260                 }
6261
6262                 /*
6263                  * Advance the stopper pointer to the next carrier
6264                  * ignoring the lower four bits. Free the previous
6265                  * stopper carrier.
6266                  */
6267                 free_carrp = asc_dvc->irq_sp;
6268                 asc_dvc->irq_sp = adv_get_carrier(asc_dvc,
6269                                                   ASC_GET_CARRP(irq_next_vpa));
6270
6271                 free_carrp->next_vpa = asc_dvc->carr_freelist->carr_va;
6272                 asc_dvc->carr_freelist = free_carrp;
6273                 asc_dvc->carr_pending_cnt--;
6274
6275                 target_bit = ADV_TID_TO_TIDMASK(scsiq->target_id);
6276
6277                 /*
6278                  * Clear request microcode control flag.
6279                  */
6280                 scsiq->cntl = 0;
6281
6282                 /*
6283                  * Notify the driver of the completed request by passing
6284                  * the ADV_SCSI_REQ_Q pointer to its callback function.
6285                  */
6286                 scsiq->a_flag |= ADV_SCSIQ_DONE;
6287                 adv_isr_callback(asc_dvc, scsiq);
6288                 /*
6289                  * Note: After the driver callback function is called, 'scsiq'
6290                  * can no longer be referenced.
6291                  *
6292                  * Fall through and continue processing other completed
6293                  * requests...
6294                  */
6295         }
6296         return ADV_TRUE;
6297 }
6298
6299 static int AscSetLibErrorCode(ASC_DVC_VAR *asc_dvc, ushort err_code)
6300 {
6301         if (asc_dvc->err_code == 0) {
6302                 asc_dvc->err_code = err_code;
6303                 AscWriteLramWord(asc_dvc->iop_base, ASCV_ASCDVC_ERR_CODE_W,
6304                                  err_code);
6305         }
6306         return err_code;
6307 }
6308
6309 static void AscAckInterrupt(PortAddr iop_base)
6310 {
6311         uchar host_flag;
6312         uchar risc_flag;
6313         ushort loop;
6314
6315         loop = 0;
6316         do {
6317                 risc_flag = AscReadLramByte(iop_base, ASCV_RISC_FLAG_B);
6318                 if (loop++ > 0x7FFF) {
6319                         break;
6320                 }
6321         } while ((risc_flag & ASC_RISC_FLAG_GEN_INT) != 0);
6322         host_flag =
6323             AscReadLramByte(iop_base,
6324                             ASCV_HOST_FLAG_B) & (~ASC_HOST_FLAG_ACK_INT);
6325         AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
6326                          (uchar)(host_flag | ASC_HOST_FLAG_ACK_INT));
6327         AscSetChipStatus(iop_base, CIW_INT_ACK);
6328         loop = 0;
6329         while (AscGetChipStatus(iop_base) & CSW_INT_PENDING) {
6330                 AscSetChipStatus(iop_base, CIW_INT_ACK);
6331                 if (loop++ > 3) {
6332                         break;
6333                 }
6334         }
6335         AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
6336 }
6337
6338 static uchar AscGetSynPeriodIndex(ASC_DVC_VAR *asc_dvc, uchar syn_time)
6339 {
6340         const uchar *period_table;
6341         int max_index;
6342         int min_index;
6343         int i;
6344
6345         period_table = asc_dvc->sdtr_period_tbl;
6346         max_index = (int)asc_dvc->max_sdtr_index;
6347         min_index = (int)asc_dvc->min_sdtr_index;
6348         if ((syn_time <= period_table[max_index])) {
6349                 for (i = min_index; i < (max_index - 1); i++) {
6350                         if (syn_time <= period_table[i]) {
6351                                 return (uchar)i;
6352                         }
6353                 }
6354                 return (uchar)max_index;
6355         } else {
6356                 return (uchar)(max_index + 1);
6357         }
6358 }
6359
6360 static uchar
6361 AscMsgOutSDTR(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar sdtr_offset)
6362 {
6363         EXT_MSG sdtr_buf;
6364         uchar sdtr_period_index;
6365         PortAddr iop_base;
6366
6367         iop_base = asc_dvc->iop_base;
6368         sdtr_buf.msg_type = EXTENDED_MESSAGE;
6369         sdtr_buf.msg_len = MS_SDTR_LEN;
6370         sdtr_buf.msg_req = EXTENDED_SDTR;
6371         sdtr_buf.xfer_period = sdtr_period;
6372         sdtr_offset &= ASC_SYN_MAX_OFFSET;
6373         sdtr_buf.req_ack_offset = sdtr_offset;
6374         sdtr_period_index = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
6375         if (sdtr_period_index <= asc_dvc->max_sdtr_index) {
6376                 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6377                                         (uchar *)&sdtr_buf,
6378                                         sizeof(EXT_MSG) >> 1);
6379                 return ((sdtr_period_index << 4) | sdtr_offset);
6380         } else {
6381                 sdtr_buf.req_ack_offset = 0;
6382                 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6383                                         (uchar *)&sdtr_buf,
6384                                         sizeof(EXT_MSG) >> 1);
6385                 return 0;
6386         }
6387 }
6388
6389 static uchar
6390 AscCalSDTRData(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar syn_offset)
6391 {
6392         uchar byte;
6393         uchar sdtr_period_ix;
6394
6395         sdtr_period_ix = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
6396         if (sdtr_period_ix > asc_dvc->max_sdtr_index)
6397                 return 0xFF;
6398         byte = (sdtr_period_ix << 4) | (syn_offset & ASC_SYN_MAX_OFFSET);
6399         return byte;
6400 }
6401
6402 static bool AscSetChipSynRegAtID(PortAddr iop_base, uchar id, uchar sdtr_data)
6403 {
6404         ASC_SCSI_BIT_ID_TYPE org_id;
6405         int i;
6406         bool sta = true;
6407
6408         AscSetBank(iop_base, 1);
6409         org_id = AscReadChipDvcID(iop_base);
6410         for (i = 0; i <= ASC_MAX_TID; i++) {
6411                 if (org_id == (0x01 << i))
6412                         break;
6413         }
6414         org_id = (ASC_SCSI_BIT_ID_TYPE) i;
6415         AscWriteChipDvcID(iop_base, id);
6416         if (AscReadChipDvcID(iop_base) == (0x01 << id)) {
6417                 AscSetBank(iop_base, 0);
6418                 AscSetChipSyn(iop_base, sdtr_data);
6419                 if (AscGetChipSyn(iop_base) != sdtr_data) {
6420                         sta = false;
6421                 }
6422         } else {
6423                 sta = false;
6424         }
6425         AscSetBank(iop_base, 1);
6426         AscWriteChipDvcID(iop_base, org_id);
6427         AscSetBank(iop_base, 0);
6428         return (sta);
6429 }
6430
6431 static void AscSetChipSDTR(PortAddr iop_base, uchar sdtr_data, uchar tid_no)
6432 {
6433         AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
6434         AscPutMCodeSDTRDoneAtID(iop_base, tid_no, sdtr_data);
6435 }
6436
6437 static void AscIsrChipHalted(ASC_DVC_VAR *asc_dvc)
6438 {
6439         EXT_MSG ext_msg;
6440         EXT_MSG out_msg;
6441         ushort halt_q_addr;
6442         bool sdtr_accept;
6443         ushort int_halt_code;
6444         ASC_SCSI_BIT_ID_TYPE scsi_busy;
6445         ASC_SCSI_BIT_ID_TYPE target_id;
6446         PortAddr iop_base;
6447         uchar tag_code;
6448         uchar q_status;
6449         uchar halt_qp;
6450         uchar sdtr_data;
6451         uchar target_ix;
6452         uchar q_cntl, tid_no;
6453         uchar cur_dvc_qng;
6454         uchar asyn_sdtr;
6455         uchar scsi_status;
6456         struct asc_board *boardp;
6457
6458         BUG_ON(!asc_dvc->drv_ptr);
6459         boardp = asc_dvc->drv_ptr;
6460
6461         iop_base = asc_dvc->iop_base;
6462         int_halt_code = AscReadLramWord(iop_base, ASCV_HALTCODE_W);
6463
6464         halt_qp = AscReadLramByte(iop_base, ASCV_CURCDB_B);
6465         halt_q_addr = ASC_QNO_TO_QADDR(halt_qp);
6466         target_ix = AscReadLramByte(iop_base,
6467                                     (ushort)(halt_q_addr +
6468                                              (ushort)ASC_SCSIQ_B_TARGET_IX));
6469         q_cntl = AscReadLramByte(iop_base,
6470                             (ushort)(halt_q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6471         tid_no = ASC_TIX_TO_TID(target_ix);
6472         target_id = (uchar)ASC_TID_TO_TARGET_ID(tid_no);
6473         if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6474                 asyn_sdtr = ASYN_SDTR_DATA_FIX_PCI_REV_AB;
6475         } else {
6476                 asyn_sdtr = 0;
6477         }
6478         if (int_halt_code == ASC_HALT_DISABLE_ASYN_USE_SYN_FIX) {
6479                 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6480                         AscSetChipSDTR(iop_base, 0, tid_no);
6481                         boardp->sdtr_data[tid_no] = 0;
6482                 }
6483                 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6484                 return;
6485         } else if (int_halt_code == ASC_HALT_ENABLE_ASYN_USE_SYN_FIX) {
6486                 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6487                         AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6488                         boardp->sdtr_data[tid_no] = asyn_sdtr;
6489                 }
6490                 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6491                 return;
6492         } else if (int_halt_code == ASC_HALT_EXTMSG_IN) {
6493                 AscMemWordCopyPtrFromLram(iop_base,
6494                                           ASCV_MSGIN_BEG,
6495                                           (uchar *)&ext_msg,
6496                                           sizeof(EXT_MSG) >> 1);
6497
6498                 if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6499                     ext_msg.msg_req == EXTENDED_SDTR &&
6500                     ext_msg.msg_len == MS_SDTR_LEN) {
6501                         sdtr_accept = true;
6502                         if ((ext_msg.req_ack_offset > ASC_SYN_MAX_OFFSET)) {
6503
6504                                 sdtr_accept = false;
6505                                 ext_msg.req_ack_offset = ASC_SYN_MAX_OFFSET;
6506                         }
6507                         if ((ext_msg.xfer_period <
6508                              asc_dvc->sdtr_period_tbl[asc_dvc->min_sdtr_index])
6509                             || (ext_msg.xfer_period >
6510                                 asc_dvc->sdtr_period_tbl[asc_dvc->
6511                                                          max_sdtr_index])) {
6512                                 sdtr_accept = false;
6513                                 ext_msg.xfer_period =
6514                                     asc_dvc->sdtr_period_tbl[asc_dvc->
6515                                                              min_sdtr_index];
6516                         }
6517                         if (sdtr_accept) {
6518                                 sdtr_data =
6519                                     AscCalSDTRData(asc_dvc, ext_msg.xfer_period,
6520                                                    ext_msg.req_ack_offset);
6521                                 if ((sdtr_data == 0xFF)) {
6522
6523                                         q_cntl |= QC_MSG_OUT;
6524                                         asc_dvc->init_sdtr &= ~target_id;
6525                                         asc_dvc->sdtr_done &= ~target_id;
6526                                         AscSetChipSDTR(iop_base, asyn_sdtr,
6527                                                        tid_no);
6528                                         boardp->sdtr_data[tid_no] = asyn_sdtr;
6529                                 }
6530                         }
6531                         if (ext_msg.req_ack_offset == 0) {
6532
6533                                 q_cntl &= ~QC_MSG_OUT;
6534                                 asc_dvc->init_sdtr &= ~target_id;
6535                                 asc_dvc->sdtr_done &= ~target_id;
6536                                 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6537                         } else {
6538                                 if (sdtr_accept && (q_cntl & QC_MSG_OUT)) {
6539                                         q_cntl &= ~QC_MSG_OUT;
6540                                         asc_dvc->sdtr_done |= target_id;
6541                                         asc_dvc->init_sdtr |= target_id;
6542                                         asc_dvc->pci_fix_asyn_xfer &=
6543                                             ~target_id;
6544                                         sdtr_data =
6545                                             AscCalSDTRData(asc_dvc,
6546                                                            ext_msg.xfer_period,
6547                                                            ext_msg.
6548                                                            req_ack_offset);
6549                                         AscSetChipSDTR(iop_base, sdtr_data,
6550                                                        tid_no);
6551                                         boardp->sdtr_data[tid_no] = sdtr_data;
6552                                 } else {
6553                                         q_cntl |= QC_MSG_OUT;
6554                                         AscMsgOutSDTR(asc_dvc,
6555                                                       ext_msg.xfer_period,
6556                                                       ext_msg.req_ack_offset);
6557                                         asc_dvc->pci_fix_asyn_xfer &=
6558                                             ~target_id;
6559                                         sdtr_data =
6560                                             AscCalSDTRData(asc_dvc,
6561                                                            ext_msg.xfer_period,
6562                                                            ext_msg.
6563                                                            req_ack_offset);
6564                                         AscSetChipSDTR(iop_base, sdtr_data,
6565                                                        tid_no);
6566                                         boardp->sdtr_data[tid_no] = sdtr_data;
6567                                         asc_dvc->sdtr_done |= target_id;
6568                                         asc_dvc->init_sdtr |= target_id;
6569                                 }
6570                         }
6571
6572                         AscWriteLramByte(iop_base,
6573                                          (ushort)(halt_q_addr +
6574                                                   (ushort)ASC_SCSIQ_B_CNTL),
6575                                          q_cntl);
6576                         AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6577                         return;
6578                 } else if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6579                            ext_msg.msg_req == EXTENDED_WDTR &&
6580                            ext_msg.msg_len == MS_WDTR_LEN) {
6581
6582                         ext_msg.wdtr_width = 0;
6583                         AscMemWordCopyPtrToLram(iop_base,
6584                                                 ASCV_MSGOUT_BEG,
6585                                                 (uchar *)&ext_msg,
6586                                                 sizeof(EXT_MSG) >> 1);
6587                         q_cntl |= QC_MSG_OUT;
6588                         AscWriteLramByte(iop_base,
6589                                          (ushort)(halt_q_addr +
6590                                                   (ushort)ASC_SCSIQ_B_CNTL),
6591                                          q_cntl);
6592                         AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6593                         return;
6594                 } else {
6595
6596                         ext_msg.msg_type = MESSAGE_REJECT;
6597                         AscMemWordCopyPtrToLram(iop_base,
6598                                                 ASCV_MSGOUT_BEG,
6599                                                 (uchar *)&ext_msg,
6600                                                 sizeof(EXT_MSG) >> 1);
6601                         q_cntl |= QC_MSG_OUT;
6602                         AscWriteLramByte(iop_base,
6603                                          (ushort)(halt_q_addr +
6604                                                   (ushort)ASC_SCSIQ_B_CNTL),
6605                                          q_cntl);
6606                         AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6607                         return;
6608                 }
6609         } else if (int_halt_code == ASC_HALT_CHK_CONDITION) {
6610
6611                 q_cntl |= QC_REQ_SENSE;
6612
6613                 if ((asc_dvc->init_sdtr & target_id) != 0) {
6614
6615                         asc_dvc->sdtr_done &= ~target_id;
6616
6617                         sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
6618                         q_cntl |= QC_MSG_OUT;
6619                         AscMsgOutSDTR(asc_dvc,
6620                                       asc_dvc->
6621                                       sdtr_period_tbl[(sdtr_data >> 4) &
6622                                                       (uchar)(asc_dvc->
6623                                                               max_sdtr_index -
6624                                                               1)],
6625                                       (uchar)(sdtr_data & (uchar)
6626                                               ASC_SYN_MAX_OFFSET));
6627                 }
6628
6629                 AscWriteLramByte(iop_base,
6630                                  (ushort)(halt_q_addr +
6631                                           (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6632
6633                 tag_code = AscReadLramByte(iop_base,
6634                                            (ushort)(halt_q_addr + (ushort)
6635                                                     ASC_SCSIQ_B_TAG_CODE));
6636                 tag_code &= 0xDC;
6637                 if ((asc_dvc->pci_fix_asyn_xfer & target_id)
6638                     && !(asc_dvc->pci_fix_asyn_xfer_always & target_id)
6639                     ) {
6640
6641                         tag_code |= (ASC_TAG_FLAG_DISABLE_DISCONNECT
6642                                      | ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);
6643
6644                 }
6645                 AscWriteLramByte(iop_base,
6646                                  (ushort)(halt_q_addr +
6647                                           (ushort)ASC_SCSIQ_B_TAG_CODE),
6648                                  tag_code);
6649
6650                 q_status = AscReadLramByte(iop_base,
6651                                            (ushort)(halt_q_addr + (ushort)
6652                                                     ASC_SCSIQ_B_STATUS));
6653                 q_status |= (QS_READY | QS_BUSY);
6654                 AscWriteLramByte(iop_base,
6655                                  (ushort)(halt_q_addr +
6656                                           (ushort)ASC_SCSIQ_B_STATUS),
6657                                  q_status);
6658
6659                 scsi_busy = AscReadLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B);
6660                 scsi_busy &= ~target_id;
6661                 AscWriteLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6662
6663                 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6664                 return;
6665         } else if (int_halt_code == ASC_HALT_SDTR_REJECTED) {
6666
6667                 AscMemWordCopyPtrFromLram(iop_base,
6668                                           ASCV_MSGOUT_BEG,
6669                                           (uchar *)&out_msg,
6670                                           sizeof(EXT_MSG) >> 1);
6671
6672                 if ((out_msg.msg_type == EXTENDED_MESSAGE) &&
6673                     (out_msg.msg_len == MS_SDTR_LEN) &&
6674                     (out_msg.msg_req == EXTENDED_SDTR)) {
6675
6676                         asc_dvc->init_sdtr &= ~target_id;
6677                         asc_dvc->sdtr_done &= ~target_id;
6678                         AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6679                         boardp->sdtr_data[tid_no] = asyn_sdtr;
6680                 }
6681                 q_cntl &= ~QC_MSG_OUT;
6682                 AscWriteLramByte(iop_base,
6683                                  (ushort)(halt_q_addr +
6684                                           (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6685                 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6686                 return;
6687         } else if (int_halt_code == ASC_HALT_SS_QUEUE_FULL) {
6688
6689                 scsi_status = AscReadLramByte(iop_base,
6690                                               (ushort)((ushort)halt_q_addr +
6691                                                        (ushort)
6692                                                        ASC_SCSIQ_SCSI_STATUS));
6693                 cur_dvc_qng =
6694                     AscReadLramByte(iop_base,
6695                                     (ushort)((ushort)ASC_QADR_BEG +
6696                                              (ushort)target_ix));
6697                 if ((cur_dvc_qng > 0) && (asc_dvc->cur_dvc_qng[tid_no] > 0)) {
6698
6699                         scsi_busy = AscReadLramByte(iop_base,
6700                                                     (ushort)ASCV_SCSIBUSY_B);
6701                         scsi_busy |= target_id;
6702                         AscWriteLramByte(iop_base,
6703                                          (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6704                         asc_dvc->queue_full_or_busy |= target_id;
6705
6706                         if (scsi_status == SAM_STAT_TASK_SET_FULL) {
6707                                 if (cur_dvc_qng > ASC_MIN_TAGGED_CMD) {
6708                                         cur_dvc_qng -= 1;
6709                                         asc_dvc->max_dvc_qng[tid_no] =
6710                                             cur_dvc_qng;
6711
6712                                         AscWriteLramByte(iop_base,
6713                                                          (ushort)((ushort)
6714                                                                   ASCV_MAX_DVC_QNG_BEG
6715                                                                   + (ushort)
6716                                                                   tid_no),
6717                                                          cur_dvc_qng);
6718
6719                                         /*
6720                                          * Set the device queue depth to the
6721                                          * number of active requests when the
6722                                          * QUEUE FULL condition was encountered.
6723                                          */
6724                                         boardp->queue_full |= target_id;
6725                                         boardp->queue_full_cnt[tid_no] =
6726                                             cur_dvc_qng;
6727                                 }
6728                         }
6729                 }
6730                 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6731                 return;
6732         }
6733 #if CC_VERY_LONG_SG_LIST
6734         else if (int_halt_code == ASC_HALT_HOST_COPY_SG_LIST_TO_RISC) {
6735                 uchar q_no;
6736                 ushort q_addr;
6737                 uchar sg_wk_q_no;
6738                 uchar first_sg_wk_q_no;
6739                 ASC_SCSI_Q *scsiq;      /* Ptr to driver request. */
6740                 ASC_SG_HEAD *sg_head;   /* Ptr to driver SG request. */
6741                 ASC_SG_LIST_Q scsi_sg_q;        /* Structure written to queue. */
6742                 ushort sg_list_dwords;
6743                 ushort sg_entry_cnt;
6744                 uchar next_qp;
6745                 int i;
6746
6747                 q_no = AscReadLramByte(iop_base, (ushort)ASCV_REQ_SG_LIST_QP);
6748                 if (q_no == ASC_QLINK_END)
6749                         return 0;
6750
6751                 q_addr = ASC_QNO_TO_QADDR(q_no);
6752
6753                 /*
6754                  * Convert the request's SRB pointer to a host ASC_SCSI_REQ
6755                  * structure pointer using a macro provided by the driver.
6756                  * The ASC_SCSI_REQ pointer provides a pointer to the
6757                  * host ASC_SG_HEAD structure.
6758                  */
6759                 /* Read request's SRB pointer. */
6760                 scsiq = (ASC_SCSI_Q *)
6761                     ASC_SRB2SCSIQ(ASC_U32_TO_VADDR(AscReadLramDWord(iop_base,
6762                                                                     (ushort)
6763                                                                     (q_addr +
6764                                                                      ASC_SCSIQ_D_SRBPTR))));
6765
6766                 /*
6767                  * Get request's first and working SG queue.
6768                  */
6769                 sg_wk_q_no = AscReadLramByte(iop_base,
6770                                              (ushort)(q_addr +
6771                                                       ASC_SCSIQ_B_SG_WK_QP));
6772
6773                 first_sg_wk_q_no = AscReadLramByte(iop_base,
6774                                                    (ushort)(q_addr +
6775                                                             ASC_SCSIQ_B_FIRST_SG_WK_QP));
6776
6777                 /*
6778                  * Reset request's working SG queue back to the
6779                  * first SG queue.
6780                  */
6781                 AscWriteLramByte(iop_base,
6782                                  (ushort)(q_addr +
6783                                           (ushort)ASC_SCSIQ_B_SG_WK_QP),
6784                                  first_sg_wk_q_no);
6785
6786                 sg_head = scsiq->sg_head;
6787
6788                 /*
6789                  * Set sg_entry_cnt to the number of SG elements
6790                  * that will be completed on this interrupt.
6791                  *
6792                  * Note: The allocated SG queues contain ASC_MAX_SG_LIST - 1
6793                  * SG elements. The data_cnt and data_addr fields which
6794                  * add 1 to the SG element capacity are not used when
6795                  * restarting SG handling after a halt.
6796                  */
6797                 if (scsiq->remain_sg_entry_cnt > (ASC_MAX_SG_LIST - 1)) {
6798                         sg_entry_cnt = ASC_MAX_SG_LIST - 1;
6799
6800                         /*
6801                          * Keep track of remaining number of SG elements that
6802                          * will need to be handled on the next interrupt.
6803                          */
6804                         scsiq->remain_sg_entry_cnt -= (ASC_MAX_SG_LIST - 1);
6805                 } else {
6806                         sg_entry_cnt = scsiq->remain_sg_entry_cnt;
6807                         scsiq->remain_sg_entry_cnt = 0;
6808                 }
6809
6810                 /*
6811                  * Copy SG elements into the list of allocated SG queues.
6812                  *
6813                  * Last index completed is saved in scsiq->next_sg_index.
6814                  */
6815                 next_qp = first_sg_wk_q_no;
6816                 q_addr = ASC_QNO_TO_QADDR(next_qp);
6817                 scsi_sg_q.sg_head_qp = q_no;
6818                 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
6819                 for (i = 0; i < sg_head->queue_cnt; i++) {
6820                         scsi_sg_q.seq_no = i + 1;
6821                         if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
6822                                 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
6823                                 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
6824                                 /*
6825                                  * After very first SG queue RISC FW uses next
6826                                  * SG queue first element then checks sg_list_cnt
6827                                  * against zero and then decrements, so set
6828                                  * sg_list_cnt 1 less than number of SG elements
6829                                  * in each SG queue.
6830                                  */
6831                                 scsi_sg_q.sg_list_cnt = ASC_SG_LIST_PER_Q - 1;
6832                                 scsi_sg_q.sg_cur_list_cnt =
6833                                     ASC_SG_LIST_PER_Q - 1;
6834                         } else {
6835                                 /*
6836                                  * This is the last SG queue in the list of
6837                                  * allocated SG queues. If there are more
6838                                  * SG elements than will fit in the allocated
6839                                  * queues, then set the QCSG_SG_XFER_MORE flag.
6840                                  */
6841                                 if (scsiq->remain_sg_entry_cnt != 0) {
6842                                         scsi_sg_q.cntl |= QCSG_SG_XFER_MORE;
6843                                 } else {
6844                                         scsi_sg_q.cntl |= QCSG_SG_XFER_END;
6845                                 }
6846                                 /* equals sg_entry_cnt * 2 */
6847                                 sg_list_dwords = sg_entry_cnt << 1;
6848                                 scsi_sg_q.sg_list_cnt = sg_entry_cnt - 1;
6849                                 scsi_sg_q.sg_cur_list_cnt = sg_entry_cnt - 1;
6850                                 sg_entry_cnt = 0;
6851                         }
6852
6853                         scsi_sg_q.q_no = next_qp;
6854                         AscMemWordCopyPtrToLram(iop_base,
6855                                                 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
6856                                                 (uchar *)&scsi_sg_q,
6857                                                 sizeof(ASC_SG_LIST_Q) >> 1);
6858
6859                         AscMemDWordCopyPtrToLram(iop_base,
6860                                                  q_addr + ASC_SGQ_LIST_BEG,
6861                                                  (uchar *)&sg_head->
6862                                                  sg_list[scsiq->next_sg_index],
6863                                                  sg_list_dwords);
6864
6865                         scsiq->next_sg_index += ASC_SG_LIST_PER_Q;
6866
6867                         /*
6868                          * If the just completed SG queue contained the
6869                          * last SG element, then no more SG queues need
6870                          * to be written.
6871                          */
6872                         if (scsi_sg_q.cntl & QCSG_SG_XFER_END) {
6873                                 break;
6874                         }
6875
6876                         next_qp = AscReadLramByte(iop_base,
6877                                                   (ushort)(q_addr +
6878                                                            ASC_SCSIQ_B_FWD));
6879                         q_addr = ASC_QNO_TO_QADDR(next_qp);
6880                 }
6881
6882                 /*
6883                  * Clear the halt condition so the RISC will be restarted
6884                  * after the return.
6885                  */
6886                 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6887                 return;
6888         }
6889 #endif /* CC_VERY_LONG_SG_LIST */
6890         return;
6891 }
6892
6893 /*
6894  * void
6895  * DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6896  *
6897  * Calling/Exit State:
6898  *    none
6899  *
6900  * Description:
6901  *     Input an ASC_QDONE_INFO structure from the chip
6902  */
6903 static void
6904 DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6905 {
6906         int i;
6907         ushort word;
6908
6909         AscSetChipLramAddr(iop_base, s_addr);
6910         for (i = 0; i < 2 * words; i += 2) {
6911                 if (i == 10) {
6912                         continue;
6913                 }
6914                 word = inpw(iop_base + IOP_RAM_DATA);
6915                 inbuf[i] = word & 0xff;
6916                 inbuf[i + 1] = (word >> 8) & 0xff;
6917         }
6918         ASC_DBG_PRT_HEX(2, "DvcGetQinfo", inbuf, 2 * words);
6919 }
6920
6921 static uchar
6922 _AscCopyLramScsiDoneQ(PortAddr iop_base,
6923                       ushort q_addr,
6924                       ASC_QDONE_INFO *scsiq, unsigned int max_dma_count)
6925 {
6926         ushort _val;
6927         uchar sg_queue_cnt;
6928
6929         DvcGetQinfo(iop_base,
6930                     q_addr + ASC_SCSIQ_DONE_INFO_BEG,
6931                     (uchar *)scsiq,
6932                     (sizeof(ASC_SCSIQ_2) + sizeof(ASC_SCSIQ_3)) / 2);
6933
6934         _val = AscReadLramWord(iop_base,
6935                                (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS));
6936         scsiq->q_status = (uchar)_val;
6937         scsiq->q_no = (uchar)(_val >> 8);
6938         _val = AscReadLramWord(iop_base,
6939                                (ushort)(q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6940         scsiq->cntl = (uchar)_val;
6941         sg_queue_cnt = (uchar)(_val >> 8);
6942         _val = AscReadLramWord(iop_base,
6943                                (ushort)(q_addr +
6944                                         (ushort)ASC_SCSIQ_B_SENSE_LEN));
6945         scsiq->sense_len = (uchar)_val;
6946         scsiq->extra_bytes = (uchar)(_val >> 8);
6947
6948         /*
6949          * Read high word of remain bytes from alternate location.
6950          */
6951         scsiq->remain_bytes = (((u32)AscReadLramWord(iop_base,
6952                                                      (ushort)(q_addr +
6953                                                               (ushort)
6954                                                               ASC_SCSIQ_W_ALT_DC1)))
6955                                << 16);
6956         /*
6957          * Read low word of remain bytes from original location.
6958          */
6959         scsiq->remain_bytes += AscReadLramWord(iop_base,
6960                                                (ushort)(q_addr + (ushort)
6961                                                         ASC_SCSIQ_DW_REMAIN_XFER_CNT));
6962
6963         scsiq->remain_bytes &= max_dma_count;
6964         return sg_queue_cnt;
6965 }
6966
6967 /*
6968  * asc_isr_callback() - Second Level Interrupt Handler called by AscISR().
6969  *
6970  * Interrupt callback function for the Narrow SCSI Asc Library.
6971  */
6972 static void asc_isr_callback(ASC_DVC_VAR *asc_dvc_varp, ASC_QDONE_INFO *qdonep)
6973 {
6974         struct asc_board *boardp = asc_dvc_varp->drv_ptr;
6975         u32 srb_tag;
6976         struct scsi_cmnd *scp;
6977
6978         ASC_DBG(1, "asc_dvc_varp 0x%p, qdonep 0x%p\n", asc_dvc_varp, qdonep);
6979         ASC_DBG_PRT_ASC_QDONE_INFO(2, qdonep);
6980
6981         /*
6982          * Decrease the srb_tag by 1 to find the SCSI command
6983          */
6984         srb_tag = qdonep->d2.srb_tag - 1;
6985         scp = scsi_host_find_tag(boardp->shost, srb_tag);
6986         if (!scp)
6987                 return;
6988
6989         ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
6990
6991         ASC_STATS(boardp->shost, callback);
6992
6993         dma_unmap_single(boardp->dev, scp->SCp.dma_handle,
6994                          SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
6995         /*
6996          * 'qdonep' contains the command's ending status.
6997          */
6998         switch (qdonep->d3.done_stat) {
6999         case QD_NO_ERROR:
7000                 ASC_DBG(2, "QD_NO_ERROR\n");
7001                 scp->result = 0;
7002
7003                 /*
7004                  * Check for an underrun condition.
7005                  *
7006                  * If there was no error and an underrun condition, then
7007                  * return the number of underrun bytes.
7008                  */
7009                 if (scsi_bufflen(scp) != 0 && qdonep->remain_bytes != 0 &&
7010                     qdonep->remain_bytes <= scsi_bufflen(scp)) {
7011                         ASC_DBG(1, "underrun condition %u bytes\n",
7012                                  (unsigned)qdonep->remain_bytes);
7013                         scsi_set_resid(scp, qdonep->remain_bytes);
7014                 }
7015                 break;
7016
7017         case QD_WITH_ERROR:
7018                 ASC_DBG(2, "QD_WITH_ERROR\n");
7019                 switch (qdonep->d3.host_stat) {
7020                 case QHSTA_NO_ERROR:
7021                         if (qdonep->d3.scsi_stat == SAM_STAT_CHECK_CONDITION) {
7022                                 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
7023                                 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
7024                                                   SCSI_SENSE_BUFFERSIZE);
7025                                 /*
7026                                  * Note: The 'status_byte()' macro used by
7027                                  * target drivers defined in scsi.h shifts the
7028                                  * status byte returned by host drivers right
7029                                  * by 1 bit.  This is why target drivers also
7030                                  * use right shifted status byte definitions.
7031                                  * For instance target drivers use
7032                                  * CHECK_CONDITION, defined to 0x1, instead of
7033                                  * the SCSI defined check condition value of
7034                                  * 0x2. Host drivers are supposed to return
7035                                  * the status byte as it is defined by SCSI.
7036                                  */
7037                                 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
7038                                     STATUS_BYTE(qdonep->d3.scsi_stat);
7039                         } else {
7040                                 scp->result = STATUS_BYTE(qdonep->d3.scsi_stat);
7041                         }
7042                         break;
7043
7044                 default:
7045                         /* QHSTA error occurred */
7046                         ASC_DBG(1, "host_stat 0x%x\n", qdonep->d3.host_stat);
7047                         scp->result = HOST_BYTE(DID_BAD_TARGET);
7048                         break;
7049                 }
7050                 break;
7051
7052         case QD_ABORTED_BY_HOST:
7053                 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
7054                 scp->result =
7055                     HOST_BYTE(DID_ABORT) | MSG_BYTE(qdonep->d3.
7056                                                     scsi_msg) |
7057                     STATUS_BYTE(qdonep->d3.scsi_stat);
7058                 break;
7059
7060         default:
7061                 ASC_DBG(1, "done_stat 0x%x\n", qdonep->d3.done_stat);
7062                 scp->result =
7063                     HOST_BYTE(DID_ERROR) | MSG_BYTE(qdonep->d3.
7064                                                     scsi_msg) |
7065                     STATUS_BYTE(qdonep->d3.scsi_stat);
7066                 break;
7067         }
7068
7069         /*
7070          * If the 'init_tidmask' bit isn't already set for the target and the
7071          * current request finished normally, then set the bit for the target
7072          * to indicate that a device is present.
7073          */
7074         if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
7075             qdonep->d3.done_stat == QD_NO_ERROR &&
7076             qdonep->d3.host_stat == QHSTA_NO_ERROR) {
7077                 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
7078         }
7079
7080         asc_scsi_done(scp);
7081 }
7082
7083 static int AscIsrQDone(ASC_DVC_VAR *asc_dvc)
7084 {
7085         uchar next_qp;
7086         uchar n_q_used;
7087         uchar sg_list_qp;
7088         uchar sg_queue_cnt;
7089         uchar q_cnt;
7090         uchar done_q_tail;
7091         uchar tid_no;
7092         ASC_SCSI_BIT_ID_TYPE scsi_busy;
7093         ASC_SCSI_BIT_ID_TYPE target_id;
7094         PortAddr iop_base;
7095         ushort q_addr;
7096         ushort sg_q_addr;
7097         uchar cur_target_qng;
7098         ASC_QDONE_INFO scsiq_buf;
7099         ASC_QDONE_INFO *scsiq;
7100         bool false_overrun;
7101
7102         iop_base = asc_dvc->iop_base;
7103         n_q_used = 1;
7104         scsiq = (ASC_QDONE_INFO *)&scsiq_buf;
7105         done_q_tail = (uchar)AscGetVarDoneQTail(iop_base);
7106         q_addr = ASC_QNO_TO_QADDR(done_q_tail);
7107         next_qp = AscReadLramByte(iop_base,
7108                                   (ushort)(q_addr + (ushort)ASC_SCSIQ_B_FWD));
7109         if (next_qp != ASC_QLINK_END) {
7110                 AscPutVarDoneQTail(iop_base, next_qp);
7111                 q_addr = ASC_QNO_TO_QADDR(next_qp);
7112                 sg_queue_cnt = _AscCopyLramScsiDoneQ(iop_base, q_addr, scsiq,
7113                                                      asc_dvc->max_dma_count);
7114                 AscWriteLramByte(iop_base,
7115                                  (ushort)(q_addr +
7116                                           (ushort)ASC_SCSIQ_B_STATUS),
7117                                  (uchar)(scsiq->
7118                                          q_status & (uchar)~(QS_READY |
7119                                                              QS_ABORTED)));
7120                 tid_no = ASC_TIX_TO_TID(scsiq->d2.target_ix);
7121                 target_id = ASC_TIX_TO_TARGET_ID(scsiq->d2.target_ix);
7122                 if ((scsiq->cntl & QC_SG_HEAD) != 0) {
7123                         sg_q_addr = q_addr;
7124                         sg_list_qp = next_qp;
7125                         for (q_cnt = 0; q_cnt < sg_queue_cnt; q_cnt++) {
7126                                 sg_list_qp = AscReadLramByte(iop_base,
7127                                                              (ushort)(sg_q_addr
7128                                                                       + (ushort)
7129                                                                       ASC_SCSIQ_B_FWD));
7130                                 sg_q_addr = ASC_QNO_TO_QADDR(sg_list_qp);
7131                                 if (sg_list_qp == ASC_QLINK_END) {
7132                                         AscSetLibErrorCode(asc_dvc,
7133                                                            ASCQ_ERR_SG_Q_LINKS);
7134                                         scsiq->d3.done_stat = QD_WITH_ERROR;
7135                                         scsiq->d3.host_stat =
7136                                             QHSTA_D_QDONE_SG_LIST_CORRUPTED;
7137                                         goto FATAL_ERR_QDONE;
7138                                 }
7139                                 AscWriteLramByte(iop_base,
7140                                                  (ushort)(sg_q_addr + (ushort)
7141                                                           ASC_SCSIQ_B_STATUS),
7142                                                  QS_FREE);
7143                         }
7144                         n_q_used = sg_queue_cnt + 1;
7145                         AscPutVarDoneQTail(iop_base, sg_list_qp);
7146                 }
7147                 if (asc_dvc->queue_full_or_busy & target_id) {
7148                         cur_target_qng = AscReadLramByte(iop_base,
7149                                                          (ushort)((ushort)
7150                                                                   ASC_QADR_BEG
7151                                                                   + (ushort)
7152                                                                   scsiq->d2.
7153                                                                   target_ix));
7154                         if (cur_target_qng < asc_dvc->max_dvc_qng[tid_no]) {
7155                                 scsi_busy = AscReadLramByte(iop_base, (ushort)
7156                                                             ASCV_SCSIBUSY_B);
7157                                 scsi_busy &= ~target_id;
7158                                 AscWriteLramByte(iop_base,
7159                                                  (ushort)ASCV_SCSIBUSY_B,
7160                                                  scsi_busy);
7161                                 asc_dvc->queue_full_or_busy &= ~target_id;
7162                         }
7163                 }
7164                 if (asc_dvc->cur_total_qng >= n_q_used) {
7165                         asc_dvc->cur_total_qng -= n_q_used;
7166                         if (asc_dvc->cur_dvc_qng[tid_no] != 0) {
7167                                 asc_dvc->cur_dvc_qng[tid_no]--;
7168                         }
7169                 } else {
7170                         AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CUR_QNG);
7171                         scsiq->d3.done_stat = QD_WITH_ERROR;
7172                         goto FATAL_ERR_QDONE;
7173                 }
7174                 if ((scsiq->d2.srb_tag == 0UL) ||
7175                     ((scsiq->q_status & QS_ABORTED) != 0)) {
7176                         return (0x11);
7177                 } else if (scsiq->q_status == QS_DONE) {
7178                         /*
7179                          * This is also curious.
7180                          * false_overrun will _always_ be set to 'false'
7181                          */
7182                         false_overrun = false;
7183                         if (scsiq->extra_bytes != 0) {
7184                                 scsiq->remain_bytes += scsiq->extra_bytes;
7185                         }
7186                         if (scsiq->d3.done_stat == QD_WITH_ERROR) {
7187                                 if (scsiq->d3.host_stat ==
7188                                     QHSTA_M_DATA_OVER_RUN) {
7189                                         if ((scsiq->
7190                                              cntl & (QC_DATA_IN | QC_DATA_OUT))
7191                                             == 0) {
7192                                                 scsiq->d3.done_stat =
7193                                                     QD_NO_ERROR;
7194                                                 scsiq->d3.host_stat =
7195                                                     QHSTA_NO_ERROR;
7196                                         } else if (false_overrun) {
7197                                                 scsiq->d3.done_stat =
7198                                                     QD_NO_ERROR;
7199                                                 scsiq->d3.host_stat =
7200                                                     QHSTA_NO_ERROR;
7201                                         }
7202                                 } else if (scsiq->d3.host_stat ==
7203                                            QHSTA_M_HUNG_REQ_SCSI_BUS_RESET) {
7204                                         AscStopChip(iop_base);
7205                                         AscSetChipControl(iop_base,
7206                                                           (uchar)(CC_SCSI_RESET
7207                                                                   | CC_HALT));
7208                                         udelay(60);
7209                                         AscSetChipControl(iop_base, CC_HALT);
7210                                         AscSetChipStatus(iop_base,
7211                                                          CIW_CLR_SCSI_RESET_INT);
7212                                         AscSetChipStatus(iop_base, 0);
7213                                         AscSetChipControl(iop_base, 0);
7214                                 }
7215                         }
7216                         if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7217                                 asc_isr_callback(asc_dvc, scsiq);
7218                         } else {
7219                                 if ((AscReadLramByte(iop_base,
7220                                                      (ushort)(q_addr + (ushort)
7221                                                               ASC_SCSIQ_CDB_BEG))
7222                                      == START_STOP)) {
7223                                         asc_dvc->unit_not_ready &= ~target_id;
7224                                         if (scsiq->d3.done_stat != QD_NO_ERROR) {
7225                                                 asc_dvc->start_motor &=
7226                                                     ~target_id;
7227                                         }
7228                                 }
7229                         }
7230                         return (1);
7231                 } else {
7232                         AscSetLibErrorCode(asc_dvc, ASCQ_ERR_Q_STATUS);
7233  FATAL_ERR_QDONE:
7234                         if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7235                                 asc_isr_callback(asc_dvc, scsiq);
7236                         }
7237                         return (0x80);
7238                 }
7239         }
7240         return (0);
7241 }
7242
7243 static int AscISR(ASC_DVC_VAR *asc_dvc)
7244 {
7245         ASC_CS_TYPE chipstat;
7246         PortAddr iop_base;
7247         ushort saved_ram_addr;
7248         uchar ctrl_reg;
7249         uchar saved_ctrl_reg;
7250         int int_pending;
7251         int status;
7252         uchar host_flag;
7253
7254         iop_base = asc_dvc->iop_base;
7255         int_pending = ASC_FALSE;
7256
7257         if (AscIsIntPending(iop_base) == 0)
7258                 return int_pending;
7259
7260         if ((asc_dvc->init_state & ASC_INIT_STATE_END_LOAD_MC) == 0) {
7261                 return ASC_ERROR;
7262         }
7263         if (asc_dvc->in_critical_cnt != 0) {
7264                 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_ON_CRITICAL);
7265                 return ASC_ERROR;
7266         }
7267         if (asc_dvc->is_in_int) {
7268                 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_RE_ENTRY);
7269                 return ASC_ERROR;
7270         }
7271         asc_dvc->is_in_int = true;
7272         ctrl_reg = AscGetChipControl(iop_base);
7273         saved_ctrl_reg = ctrl_reg & (~(CC_SCSI_RESET | CC_CHIP_RESET |
7274                                        CC_SINGLE_STEP | CC_DIAG | CC_TEST));
7275         chipstat = AscGetChipStatus(iop_base);
7276         if (chipstat & CSW_SCSI_RESET_LATCH) {
7277                 if (!(asc_dvc->bus_type & (ASC_IS_VL | ASC_IS_EISA))) {
7278                         int i = 10;
7279                         int_pending = ASC_TRUE;
7280                         asc_dvc->sdtr_done = 0;
7281                         saved_ctrl_reg &= (uchar)(~CC_HALT);
7282                         while ((AscGetChipStatus(iop_base) &
7283                                 CSW_SCSI_RESET_ACTIVE) && (i-- > 0)) {
7284                                 mdelay(100);
7285                         }
7286                         AscSetChipControl(iop_base, (CC_CHIP_RESET | CC_HALT));
7287                         AscSetChipControl(iop_base, CC_HALT);
7288                         AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
7289                         AscSetChipStatus(iop_base, 0);
7290                         chipstat = AscGetChipStatus(iop_base);
7291                 }
7292         }
7293         saved_ram_addr = AscGetChipLramAddr(iop_base);
7294         host_flag = AscReadLramByte(iop_base,
7295                                     ASCV_HOST_FLAG_B) &
7296             (uchar)(~ASC_HOST_FLAG_IN_ISR);
7297         AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
7298                          (uchar)(host_flag | (uchar)ASC_HOST_FLAG_IN_ISR));
7299         if ((chipstat & CSW_INT_PENDING) || (int_pending)) {
7300                 AscAckInterrupt(iop_base);
7301                 int_pending = ASC_TRUE;
7302                 if ((chipstat & CSW_HALTED) && (ctrl_reg & CC_SINGLE_STEP)) {
7303                         AscIsrChipHalted(asc_dvc);
7304                         saved_ctrl_reg &= (uchar)(~CC_HALT);
7305                 } else {
7306                         if ((asc_dvc->dvc_cntl & ASC_CNTL_INT_MULTI_Q) != 0) {
7307                                 while (((status =
7308                                          AscIsrQDone(asc_dvc)) & 0x01) != 0) {
7309                                 }
7310                         } else {
7311                                 do {
7312                                         if ((status =
7313                                              AscIsrQDone(asc_dvc)) == 1) {
7314                                                 break;
7315                                         }
7316                                 } while (status == 0x11);
7317                         }
7318                         if ((status & 0x80) != 0)
7319                                 int_pending = ASC_ERROR;
7320                 }
7321         }
7322         AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
7323         AscSetChipLramAddr(iop_base, saved_ram_addr);
7324         AscSetChipControl(iop_base, saved_ctrl_reg);
7325         asc_dvc->is_in_int = false;
7326         return int_pending;
7327 }
7328
7329 /*
7330  * advansys_reset()
7331  *
7332  * Reset the host associated with the command 'scp'.
7333  *
7334  * This function runs its own thread. Interrupts must be blocked but
7335  * sleeping is allowed and no locking other than for host structures is
7336  * required. Returns SUCCESS or FAILED.
7337  */
7338 static int advansys_reset(struct scsi_cmnd *scp)
7339 {
7340         struct Scsi_Host *shost = scp->device->host;
7341         struct asc_board *boardp = shost_priv(shost);
7342         unsigned long flags;
7343         int status;
7344         int ret = SUCCESS;
7345
7346         ASC_DBG(1, "0x%p\n", scp);
7347
7348         ASC_STATS(shost, reset);
7349
7350         scmd_printk(KERN_INFO, scp, "SCSI host reset started...\n");
7351
7352         if (ASC_NARROW_BOARD(boardp)) {
7353                 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
7354
7355                 /* Reset the chip and SCSI bus. */
7356                 ASC_DBG(1, "before AscInitAsc1000Driver()\n");
7357                 status = AscInitAsc1000Driver(asc_dvc);
7358
7359                 /* Refer to ASC_IERR_* definitions for meaning of 'err_code'. */
7360                 if (asc_dvc->err_code || !asc_dvc->overrun_dma) {
7361                         scmd_printk(KERN_INFO, scp, "SCSI host reset error: "
7362                                     "0x%x, status: 0x%x\n", asc_dvc->err_code,
7363                                     status);
7364                         ret = FAILED;
7365                 } else if (status) {
7366                         scmd_printk(KERN_INFO, scp, "SCSI host reset warning: "
7367                                     "0x%x\n", status);
7368                 } else {
7369                         scmd_printk(KERN_INFO, scp, "SCSI host reset "
7370                                     "successful\n");
7371                 }
7372
7373                 ASC_DBG(1, "after AscInitAsc1000Driver()\n");
7374         } else {
7375                 /*
7376                  * If the suggest reset bus flags are set, then reset the bus.
7377                  * Otherwise only reset the device.
7378                  */
7379                 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
7380
7381                 /*
7382                  * Reset the chip and SCSI bus.
7383                  */
7384                 ASC_DBG(1, "before AdvResetChipAndSB()\n");
7385                 switch (AdvResetChipAndSB(adv_dvc)) {
7386                 case ASC_TRUE:
7387                         scmd_printk(KERN_INFO, scp, "SCSI host reset "
7388                                     "successful\n");
7389                         break;
7390                 case ASC_FALSE:
7391                 default:
7392                         scmd_printk(KERN_INFO, scp, "SCSI host reset error\n");
7393                         ret = FAILED;
7394                         break;
7395                 }
7396                 spin_lock_irqsave(shost->host_lock, flags);
7397                 AdvISR(adv_dvc);
7398                 spin_unlock_irqrestore(shost->host_lock, flags);
7399         }
7400
7401         ASC_DBG(1, "ret %d\n", ret);
7402
7403         return ret;
7404 }
7405
7406 /*
7407  * advansys_biosparam()
7408  *
7409  * Translate disk drive geometry if the "BIOS greater than 1 GB"
7410  * support is enabled for a drive.
7411  *
7412  * ip (information pointer) is an int array with the following definition:
7413  * ip[0]: heads
7414  * ip[1]: sectors
7415  * ip[2]: cylinders
7416  */
7417 static int
7418 advansys_biosparam(struct scsi_device *sdev, struct block_device *bdev,
7419                    sector_t capacity, int ip[])
7420 {
7421         struct asc_board *boardp = shost_priv(sdev->host);
7422
7423         ASC_DBG(1, "begin\n");
7424         ASC_STATS(sdev->host, biosparam);
7425         if (ASC_NARROW_BOARD(boardp)) {
7426                 if ((boardp->dvc_var.asc_dvc_var.dvc_cntl &
7427                      ASC_CNTL_BIOS_GT_1GB) && capacity > 0x200000) {
7428                         ip[0] = 255;
7429                         ip[1] = 63;
7430                 } else {
7431                         ip[0] = 64;
7432                         ip[1] = 32;
7433                 }
7434         } else {
7435                 if ((boardp->dvc_var.adv_dvc_var.bios_ctrl &
7436                      BIOS_CTRL_EXTENDED_XLAT) && capacity > 0x200000) {
7437                         ip[0] = 255;
7438                         ip[1] = 63;
7439                 } else {
7440                         ip[0] = 64;
7441                         ip[1] = 32;
7442                 }
7443         }
7444         ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
7445         ASC_DBG(1, "end\n");
7446         return 0;
7447 }
7448
7449 /*
7450  * First-level interrupt handler.
7451  *
7452  * 'dev_id' is a pointer to the interrupting adapter's Scsi_Host.
7453  */
7454 static irqreturn_t advansys_interrupt(int irq, void *dev_id)
7455 {
7456         struct Scsi_Host *shost = dev_id;
7457         struct asc_board *boardp = shost_priv(shost);
7458         irqreturn_t result = IRQ_NONE;
7459
7460         ASC_DBG(2, "boardp 0x%p\n", boardp);
7461         spin_lock(shost->host_lock);
7462         if (ASC_NARROW_BOARD(boardp)) {
7463                 if (AscIsIntPending(shost->io_port)) {
7464                         result = IRQ_HANDLED;
7465                         ASC_STATS(shost, interrupt);
7466                         ASC_DBG(1, "before AscISR()\n");
7467                         AscISR(&boardp->dvc_var.asc_dvc_var);
7468                 }
7469         } else {
7470                 ASC_DBG(1, "before AdvISR()\n");
7471                 if (AdvISR(&boardp->dvc_var.adv_dvc_var)) {
7472                         result = IRQ_HANDLED;
7473                         ASC_STATS(shost, interrupt);
7474                 }
7475         }
7476         spin_unlock(shost->host_lock);
7477
7478         ASC_DBG(1, "end\n");
7479         return result;
7480 }
7481
7482 static bool AscHostReqRiscHalt(PortAddr iop_base)
7483 {
7484         int count = 0;
7485         bool sta = false;
7486         uchar saved_stop_code;
7487
7488         if (AscIsChipHalted(iop_base))
7489                 return true;
7490         saved_stop_code = AscReadLramByte(iop_base, ASCV_STOP_CODE_B);
7491         AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
7492                          ASC_STOP_HOST_REQ_RISC_HALT | ASC_STOP_REQ_RISC_STOP);
7493         do {
7494                 if (AscIsChipHalted(iop_base)) {
7495                         sta = true;
7496                         break;
7497                 }
7498                 mdelay(100);
7499         } while (count++ < 20);
7500         AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, saved_stop_code);
7501         return sta;
7502 }
7503
7504 static bool
7505 AscSetRunChipSynRegAtID(PortAddr iop_base, uchar tid_no, uchar sdtr_data)
7506 {
7507         bool sta = false;
7508
7509         if (AscHostReqRiscHalt(iop_base)) {
7510                 sta = AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
7511                 AscStartChip(iop_base);
7512         }
7513         return sta;
7514 }
7515
7516 static void AscAsyncFix(ASC_DVC_VAR *asc_dvc, struct scsi_device *sdev)
7517 {
7518         char type = sdev->type;
7519         ASC_SCSI_BIT_ID_TYPE tid_bits = 1 << sdev->id;
7520
7521         if (!(asc_dvc->bug_fix_cntl & ASC_BUG_FIX_ASYN_USE_SYN))
7522                 return;
7523         if (asc_dvc->init_sdtr & tid_bits)
7524                 return;
7525
7526         if ((type == TYPE_ROM) && (strncmp(sdev->vendor, "HP ", 3) == 0))
7527                 asc_dvc->pci_fix_asyn_xfer_always |= tid_bits;
7528
7529         asc_dvc->pci_fix_asyn_xfer |= tid_bits;
7530         if ((type == TYPE_PROCESSOR) || (type == TYPE_SCANNER) ||
7531             (type == TYPE_ROM) || (type == TYPE_TAPE))
7532                 asc_dvc->pci_fix_asyn_xfer &= ~tid_bits;
7533
7534         if (asc_dvc->pci_fix_asyn_xfer & tid_bits)
7535                 AscSetRunChipSynRegAtID(asc_dvc->iop_base, sdev->id,
7536                                         ASYN_SDTR_DATA_FIX_PCI_REV_AB);
7537 }
7538
7539 static void
7540 advansys_narrow_slave_configure(struct scsi_device *sdev, ASC_DVC_VAR *asc_dvc)
7541 {
7542         ASC_SCSI_BIT_ID_TYPE tid_bit = 1 << sdev->id;
7543         ASC_SCSI_BIT_ID_TYPE orig_use_tagged_qng = asc_dvc->use_tagged_qng;
7544
7545         if (sdev->lun == 0) {
7546                 ASC_SCSI_BIT_ID_TYPE orig_init_sdtr = asc_dvc->init_sdtr;
7547                 if ((asc_dvc->cfg->sdtr_enable & tid_bit) && sdev->sdtr) {
7548                         asc_dvc->init_sdtr |= tid_bit;
7549                 } else {
7550                         asc_dvc->init_sdtr &= ~tid_bit;
7551                 }
7552
7553                 if (orig_init_sdtr != asc_dvc->init_sdtr)
7554                         AscAsyncFix(asc_dvc, sdev);
7555         }
7556
7557         if (sdev->tagged_supported) {
7558                 if (asc_dvc->cfg->cmd_qng_enabled & tid_bit) {
7559                         if (sdev->lun == 0) {
7560                                 asc_dvc->cfg->can_tagged_qng |= tid_bit;
7561                                 asc_dvc->use_tagged_qng |= tid_bit;
7562                         }
7563                         scsi_change_queue_depth(sdev, 
7564                                                 asc_dvc->max_dvc_qng[sdev->id]);
7565                 }
7566         } else {
7567                 if (sdev->lun == 0) {
7568                         asc_dvc->cfg->can_tagged_qng &= ~tid_bit;
7569                         asc_dvc->use_tagged_qng &= ~tid_bit;
7570                 }
7571         }
7572
7573         if ((sdev->lun == 0) &&
7574             (orig_use_tagged_qng != asc_dvc->use_tagged_qng)) {
7575                 AscWriteLramByte(asc_dvc->iop_base, ASCV_DISC_ENABLE_B,
7576                                  asc_dvc->cfg->disc_enable);
7577                 AscWriteLramByte(asc_dvc->iop_base, ASCV_USE_TAGGED_QNG_B,
7578                                  asc_dvc->use_tagged_qng);
7579                 AscWriteLramByte(asc_dvc->iop_base, ASCV_CAN_TAGGED_QNG_B,
7580                                  asc_dvc->cfg->can_tagged_qng);
7581
7582                 asc_dvc->max_dvc_qng[sdev->id] =
7583                                         asc_dvc->cfg->max_tag_qng[sdev->id];
7584                 AscWriteLramByte(asc_dvc->iop_base,
7585                                  (ushort)(ASCV_MAX_DVC_QNG_BEG + sdev->id),
7586                                  asc_dvc->max_dvc_qng[sdev->id]);
7587         }
7588 }
7589
7590 /*
7591  * Wide Transfers
7592  *
7593  * If the EEPROM enabled WDTR for the device and the device supports wide
7594  * bus (16 bit) transfers, then turn on the device's 'wdtr_able' bit and
7595  * write the new value to the microcode.
7596  */
7597 static void
7598 advansys_wide_enable_wdtr(AdvPortAddr iop_base, unsigned short tidmask)
7599 {
7600         unsigned short cfg_word;
7601         AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7602         if ((cfg_word & tidmask) != 0)
7603                 return;
7604
7605         cfg_word |= tidmask;
7606         AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7607
7608         /*
7609          * Clear the microcode SDTR and WDTR negotiation done indicators for
7610          * the target to cause it to negotiate with the new setting set above.
7611          * WDTR when accepted causes the target to enter asynchronous mode, so
7612          * SDTR must be negotiated.
7613          */
7614         AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7615         cfg_word &= ~tidmask;
7616         AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7617         AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7618         cfg_word &= ~tidmask;
7619         AdvWriteWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7620 }
7621
7622 /*
7623  * Synchronous Transfers
7624  *
7625  * If the EEPROM enabled SDTR for the device and the device
7626  * supports synchronous transfers, then turn on the device's
7627  * 'sdtr_able' bit. Write the new value to the microcode.
7628  */
7629 static void
7630 advansys_wide_enable_sdtr(AdvPortAddr iop_base, unsigned short tidmask)
7631 {
7632         unsigned short cfg_word;
7633         AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7634         if ((cfg_word & tidmask) != 0)
7635                 return;
7636
7637         cfg_word |= tidmask;
7638         AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7639
7640         /*
7641          * Clear the microcode "SDTR negotiation" done indicator for the
7642          * target to cause it to negotiate with the new setting set above.
7643          */
7644         AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7645         cfg_word &= ~tidmask;
7646         AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7647 }
7648
7649 /*
7650  * PPR (Parallel Protocol Request) Capable
7651  *
7652  * If the device supports DT mode, then it must be PPR capable.
7653  * The PPR message will be used in place of the SDTR and WDTR
7654  * messages to negotiate synchronous speed and offset, transfer
7655  * width, and protocol options.
7656  */
7657 static void advansys_wide_enable_ppr(ADV_DVC_VAR *adv_dvc,
7658                                 AdvPortAddr iop_base, unsigned short tidmask)
7659 {
7660         AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7661         adv_dvc->ppr_able |= tidmask;
7662         AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7663 }
7664
7665 static void
7666 advansys_wide_slave_configure(struct scsi_device *sdev, ADV_DVC_VAR *adv_dvc)
7667 {
7668         AdvPortAddr iop_base = adv_dvc->iop_base;
7669         unsigned short tidmask = 1 << sdev->id;
7670
7671         if (sdev->lun == 0) {
7672                 /*
7673                  * Handle WDTR, SDTR, and Tag Queuing. If the feature
7674                  * is enabled in the EEPROM and the device supports the
7675                  * feature, then enable it in the microcode.
7676                  */
7677
7678                 if ((adv_dvc->wdtr_able & tidmask) && sdev->wdtr)
7679                         advansys_wide_enable_wdtr(iop_base, tidmask);
7680                 if ((adv_dvc->sdtr_able & tidmask) && sdev->sdtr)
7681                         advansys_wide_enable_sdtr(iop_base, tidmask);
7682                 if (adv_dvc->chip_type == ADV_CHIP_ASC38C1600 && sdev->ppr)
7683                         advansys_wide_enable_ppr(adv_dvc, iop_base, tidmask);
7684
7685                 /*
7686                  * Tag Queuing is disabled for the BIOS which runs in polled
7687                  * mode and would see no benefit from Tag Queuing. Also by
7688                  * disabling Tag Queuing in the BIOS devices with Tag Queuing
7689                  * bugs will at least work with the BIOS.
7690                  */
7691                 if ((adv_dvc->tagqng_able & tidmask) &&
7692                     sdev->tagged_supported) {
7693                         unsigned short cfg_word;
7694                         AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, cfg_word);
7695                         cfg_word |= tidmask;
7696                         AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
7697                                          cfg_word);
7698                         AdvWriteByteLram(iop_base,
7699                                          ASC_MC_NUMBER_OF_MAX_CMD + sdev->id,
7700                                          adv_dvc->max_dvc_qng);
7701                 }
7702         }
7703
7704         if ((adv_dvc->tagqng_able & tidmask) && sdev->tagged_supported)
7705                 scsi_change_queue_depth(sdev, adv_dvc->max_dvc_qng);
7706 }
7707
7708 /*
7709  * Set the number of commands to queue per device for the
7710  * specified host adapter.
7711  */
7712 static int advansys_slave_configure(struct scsi_device *sdev)
7713 {
7714         struct asc_board *boardp = shost_priv(sdev->host);
7715
7716         if (ASC_NARROW_BOARD(boardp))
7717                 advansys_narrow_slave_configure(sdev,
7718                                                 &boardp->dvc_var.asc_dvc_var);
7719         else
7720                 advansys_wide_slave_configure(sdev,
7721                                                 &boardp->dvc_var.adv_dvc_var);
7722
7723         return 0;
7724 }
7725
7726 static __le32 advansys_get_sense_buffer_dma(struct scsi_cmnd *scp)
7727 {
7728         struct asc_board *board = shost_priv(scp->device->host);
7729         scp->SCp.dma_handle = dma_map_single(board->dev, scp->sense_buffer,
7730                                              SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
7731         dma_cache_sync(board->dev, scp->sense_buffer,
7732                        SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
7733         return cpu_to_le32(scp->SCp.dma_handle);
7734 }
7735
7736 static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
7737                         struct asc_scsi_q *asc_scsi_q)
7738 {
7739         struct asc_dvc_var *asc_dvc = &boardp->dvc_var.asc_dvc_var;
7740         int use_sg;
7741         u32 srb_tag;
7742
7743         memset(asc_scsi_q, 0, sizeof(*asc_scsi_q));
7744
7745         /*
7746          * Set the srb_tag to the command tag + 1, as
7747          * srb_tag '0' is used internally by the chip.
7748          */
7749         srb_tag = scp->request->tag + 1;
7750         asc_scsi_q->q2.srb_tag = srb_tag;
7751
7752         /*
7753          * Build the ASC_SCSI_Q request.
7754          */
7755         asc_scsi_q->cdbptr = &scp->cmnd[0];
7756         asc_scsi_q->q2.cdb_len = scp->cmd_len;
7757         asc_scsi_q->q1.target_id = ASC_TID_TO_TARGET_ID(scp->device->id);
7758         asc_scsi_q->q1.target_lun = scp->device->lun;
7759         asc_scsi_q->q2.target_ix =
7760             ASC_TIDLUN_TO_IX(scp->device->id, scp->device->lun);
7761         asc_scsi_q->q1.sense_addr = advansys_get_sense_buffer_dma(scp);
7762         asc_scsi_q->q1.sense_len = SCSI_SENSE_BUFFERSIZE;
7763
7764         /*
7765          * If there are any outstanding requests for the current target,
7766          * then every 255th request send an ORDERED request. This heuristic
7767          * tries to retain the benefit of request sorting while preventing
7768          * request starvation. 255 is the max number of tags or pending commands
7769          * a device may have outstanding.
7770          *
7771          * The request count is incremented below for every successfully
7772          * started request.
7773          *
7774          */
7775         if ((asc_dvc->cur_dvc_qng[scp->device->id] > 0) &&
7776             (boardp->reqcnt[scp->device->id] % 255) == 0) {
7777                 asc_scsi_q->q2.tag_code = ORDERED_QUEUE_TAG;
7778         } else {
7779                 asc_scsi_q->q2.tag_code = SIMPLE_QUEUE_TAG;
7780         }
7781
7782         /* Build ASC_SCSI_Q */
7783         use_sg = scsi_dma_map(scp);
7784         if (use_sg != 0) {
7785                 int sgcnt;
7786                 struct scatterlist *slp;
7787                 struct asc_sg_head *asc_sg_head;
7788
7789                 if (use_sg > scp->device->host->sg_tablesize) {
7790                         scmd_printk(KERN_ERR, scp, "use_sg %d > "
7791                                 "sg_tablesize %d\n", use_sg,
7792                                 scp->device->host->sg_tablesize);
7793                         scsi_dma_unmap(scp);
7794                         scp->result = HOST_BYTE(DID_ERROR);
7795                         return ASC_ERROR;
7796                 }
7797
7798                 asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) +
7799                         use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC);
7800                 if (!asc_sg_head) {
7801                         scsi_dma_unmap(scp);
7802                         scp->result = HOST_BYTE(DID_SOFT_ERROR);
7803                         return ASC_ERROR;
7804                 }
7805
7806                 asc_scsi_q->q1.cntl |= QC_SG_HEAD;
7807                 asc_scsi_q->sg_head = asc_sg_head;
7808                 asc_scsi_q->q1.data_cnt = 0;
7809                 asc_scsi_q->q1.data_addr = 0;
7810                 /* This is a byte value, otherwise it would need to be swapped. */
7811                 asc_sg_head->entry_cnt = asc_scsi_q->q1.sg_queue_cnt = use_sg;
7812                 ASC_STATS_ADD(scp->device->host, xfer_elem,
7813                               asc_sg_head->entry_cnt);
7814
7815                 /*
7816                  * Convert scatter-gather list into ASC_SG_HEAD list.
7817                  */
7818                 scsi_for_each_sg(scp, slp, use_sg, sgcnt) {
7819                         asc_sg_head->sg_list[sgcnt].addr =
7820                             cpu_to_le32(sg_dma_address(slp));
7821                         asc_sg_head->sg_list[sgcnt].bytes =
7822                             cpu_to_le32(sg_dma_len(slp));
7823                         ASC_STATS_ADD(scp->device->host, xfer_sect,
7824                                       DIV_ROUND_UP(sg_dma_len(slp), 512));
7825                 }
7826         }
7827
7828         ASC_STATS(scp->device->host, xfer_cnt);
7829
7830         ASC_DBG_PRT_ASC_SCSI_Q(2, asc_scsi_q);
7831         ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7832
7833         return ASC_NOERROR;
7834 }
7835
7836 /*
7837  * Build scatter-gather list for Adv Library (Wide Board).
7838  *
7839  * Additional ADV_SG_BLOCK structures will need to be allocated
7840  * if the total number of scatter-gather elements exceeds
7841  * NO_OF_SG_PER_BLOCK (15). The ADV_SG_BLOCK structures are
7842  * assumed to be physically contiguous.
7843  *
7844  * Return:
7845  *      ADV_SUCCESS(1) - SG List successfully created
7846  *      ADV_ERROR(-1) - SG List creation failed
7847  */
7848 static int
7849 adv_get_sglist(struct asc_board *boardp, adv_req_t *reqp,
7850                ADV_SCSI_REQ_Q *scsiqp, struct scsi_cmnd *scp, int use_sg)
7851 {
7852         adv_sgblk_t *sgblkp, *prev_sgblkp;
7853         struct scatterlist *slp;
7854         int sg_elem_cnt;
7855         ADV_SG_BLOCK *sg_block, *prev_sg_block;
7856         dma_addr_t sgblk_paddr;
7857         int i;
7858
7859         slp = scsi_sglist(scp);
7860         sg_elem_cnt = use_sg;
7861         prev_sgblkp = NULL;
7862         prev_sg_block = NULL;
7863         reqp->sgblkp = NULL;
7864
7865         for (;;) {
7866                 /*
7867                  * Allocate a 'adv_sgblk_t' structure from the board free
7868                  * list. One 'adv_sgblk_t' structure holds NO_OF_SG_PER_BLOCK
7869                  * (15) scatter-gather elements.
7870                  */
7871                 sgblkp = dma_pool_alloc(boardp->adv_sgblk_pool, GFP_ATOMIC,
7872                                         &sgblk_paddr);
7873                 if (!sgblkp) {
7874                         ASC_DBG(1, "no free adv_sgblk_t\n");
7875                         ASC_STATS(scp->device->host, adv_build_nosg);
7876
7877                         /*
7878                          * Allocation failed. Free 'adv_sgblk_t' structures
7879                          * already allocated for the request.
7880                          */
7881                         while ((sgblkp = reqp->sgblkp) != NULL) {
7882                                 /* Remove 'sgblkp' from the request list. */
7883                                 reqp->sgblkp = sgblkp->next_sgblkp;
7884                                 sgblkp->next_sgblkp = NULL;
7885                                 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
7886                                               sgblkp->sg_addr);
7887                         }
7888                         return ASC_BUSY;
7889                 }
7890                 /* Complete 'adv_sgblk_t' board allocation. */
7891                 sgblkp->sg_addr = sgblk_paddr;
7892                 sgblkp->next_sgblkp = NULL;
7893                 sg_block = &sgblkp->sg_block;
7894
7895                 /*
7896                  * Check if this is the first 'adv_sgblk_t' for the
7897                  * request.
7898                  */
7899                 if (reqp->sgblkp == NULL) {
7900                         /* Request's first scatter-gather block. */
7901                         reqp->sgblkp = sgblkp;
7902
7903                         /*
7904                          * Set ADV_SCSI_REQ_T ADV_SG_BLOCK virtual and physical
7905                          * address pointers.
7906                          */
7907                         scsiqp->sg_list_ptr = sg_block;
7908                         scsiqp->sg_real_addr = cpu_to_le32(sgblk_paddr);
7909                 } else {
7910                         /* Request's second or later scatter-gather block. */
7911                         prev_sgblkp->next_sgblkp = sgblkp;
7912
7913                         /*
7914                          * Point the previous ADV_SG_BLOCK structure to
7915                          * the newly allocated ADV_SG_BLOCK structure.
7916                          */
7917                         prev_sg_block->sg_ptr = cpu_to_le32(sgblk_paddr);
7918                 }
7919
7920                 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
7921                         sg_block->sg_list[i].sg_addr =
7922                                         cpu_to_le32(sg_dma_address(slp));
7923                         sg_block->sg_list[i].sg_count =
7924                                         cpu_to_le32(sg_dma_len(slp));
7925                         ASC_STATS_ADD(scp->device->host, xfer_sect,
7926                                       DIV_ROUND_UP(sg_dma_len(slp), 512));
7927
7928                         if (--sg_elem_cnt == 0) {
7929                                 /*
7930                                  * Last ADV_SG_BLOCK and scatter-gather entry.
7931                                  */
7932                                 sg_block->sg_cnt = i + 1;
7933                                 sg_block->sg_ptr = 0L; /* Last ADV_SG_BLOCK in list. */
7934                                 return ADV_SUCCESS;
7935                         }
7936                         slp++;
7937                 }
7938                 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
7939                 prev_sg_block = sg_block;
7940                 prev_sgblkp = sgblkp;
7941         }
7942 }
7943
7944 /*
7945  * Build a request structure for the Adv Library (Wide Board).
7946  *
7947  * If an adv_req_t can not be allocated to issue the request,
7948  * then return ASC_BUSY. If an error occurs, then return ASC_ERROR.
7949  *
7950  * Multi-byte fields in the ASC_SCSI_REQ_Q that are used by the
7951  * microcode for DMA addresses or math operations are byte swapped
7952  * to little-endian order.
7953  */
7954 static int
7955 adv_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
7956               adv_req_t **adv_reqpp)
7957 {
7958         u32 srb_tag = scp->request->tag;
7959         adv_req_t *reqp;
7960         ADV_SCSI_REQ_Q *scsiqp;
7961         int ret;
7962         int use_sg;
7963         dma_addr_t sense_addr;
7964
7965         /*
7966          * Allocate an adv_req_t structure from the board to execute
7967          * the command.
7968          */
7969         reqp = &boardp->adv_reqp[srb_tag];
7970         if (reqp->cmndp && reqp->cmndp != scp ) {
7971                 ASC_DBG(1, "no free adv_req_t\n");
7972                 ASC_STATS(scp->device->host, adv_build_noreq);
7973                 return ASC_BUSY;
7974         }
7975
7976         reqp->req_addr = boardp->adv_reqp_addr + (srb_tag * sizeof(adv_req_t));
7977
7978         scsiqp = &reqp->scsi_req_q;
7979
7980         /*
7981          * Initialize the structure.
7982          */
7983         scsiqp->cntl = scsiqp->scsi_cntl = scsiqp->done_status = 0;
7984
7985         /*
7986          * Set the srb_tag to the command tag.
7987          */
7988         scsiqp->srb_tag = srb_tag;
7989
7990         /*
7991          * Set 'host_scribble' to point to the adv_req_t structure.
7992          */
7993         reqp->cmndp = scp;
7994         scp->host_scribble = (void *)reqp;
7995
7996         /*
7997          * Build the ADV_SCSI_REQ_Q request.
7998          */
7999
8000         /* Set CDB length and copy it to the request structure.  */
8001         scsiqp->cdb_len = scp->cmd_len;
8002         /* Copy first 12 CDB bytes to cdb[]. */
8003         memcpy(scsiqp->cdb, scp->cmnd, scp->cmd_len < 12 ? scp->cmd_len : 12);
8004         /* Copy last 4 CDB bytes, if present, to cdb16[]. */
8005         if (scp->cmd_len > 12) {
8006                 int cdb16_len = scp->cmd_len - 12;
8007
8008                 memcpy(scsiqp->cdb16, &scp->cmnd[12], cdb16_len);
8009         }
8010
8011         scsiqp->target_id = scp->device->id;
8012         scsiqp->target_lun = scp->device->lun;
8013
8014         sense_addr = dma_map_single(boardp->dev, scp->sense_buffer,
8015                                     SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
8016         scsiqp->sense_addr = cpu_to_le32(sense_addr);
8017         scsiqp->sense_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
8018
8019         /* Build ADV_SCSI_REQ_Q */
8020
8021         use_sg = scsi_dma_map(scp);
8022         if (use_sg == 0) {
8023                 /* Zero-length transfer */
8024                 reqp->sgblkp = NULL;
8025                 scsiqp->data_cnt = 0;
8026
8027                 scsiqp->data_addr = 0;
8028                 scsiqp->sg_list_ptr = NULL;
8029                 scsiqp->sg_real_addr = 0;
8030         } else {
8031                 if (use_sg > ADV_MAX_SG_LIST) {
8032                         scmd_printk(KERN_ERR, scp, "use_sg %d > "
8033                                    "ADV_MAX_SG_LIST %d\n", use_sg,
8034                                    scp->device->host->sg_tablesize);
8035                         scsi_dma_unmap(scp);
8036                         scp->result = HOST_BYTE(DID_ERROR);
8037                         reqp->cmndp = NULL;
8038                         scp->host_scribble = NULL;
8039
8040                         return ASC_ERROR;
8041                 }
8042
8043                 scsiqp->data_cnt = cpu_to_le32(scsi_bufflen(scp));
8044
8045                 ret = adv_get_sglist(boardp, reqp, scsiqp, scp, use_sg);
8046                 if (ret != ADV_SUCCESS) {
8047                         scsi_dma_unmap(scp);
8048                         scp->result = HOST_BYTE(DID_ERROR);
8049                         reqp->cmndp = NULL;
8050                         scp->host_scribble = NULL;
8051
8052                         return ret;
8053                 }
8054
8055                 ASC_STATS_ADD(scp->device->host, xfer_elem, use_sg);
8056         }
8057
8058         ASC_STATS(scp->device->host, xfer_cnt);
8059
8060         ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
8061         ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
8062
8063         *adv_reqpp = reqp;
8064
8065         return ASC_NOERROR;
8066 }
8067
8068 static int AscSgListToQueue(int sg_list)
8069 {
8070         int n_sg_list_qs;
8071
8072         n_sg_list_qs = ((sg_list - 1) / ASC_SG_LIST_PER_Q);
8073         if (((sg_list - 1) % ASC_SG_LIST_PER_Q) != 0)
8074                 n_sg_list_qs++;
8075         return n_sg_list_qs + 1;
8076 }
8077
8078 static uint
8079 AscGetNumOfFreeQueue(ASC_DVC_VAR *asc_dvc, uchar target_ix, uchar n_qs)
8080 {
8081         uint cur_used_qs;
8082         uint cur_free_qs;
8083         ASC_SCSI_BIT_ID_TYPE target_id;
8084         uchar tid_no;
8085
8086         target_id = ASC_TIX_TO_TARGET_ID(target_ix);
8087         tid_no = ASC_TIX_TO_TID(target_ix);
8088         if ((asc_dvc->unit_not_ready & target_id) ||
8089             (asc_dvc->queue_full_or_busy & target_id)) {
8090                 return 0;
8091         }
8092         if (n_qs == 1) {
8093                 cur_used_qs = (uint) asc_dvc->cur_total_qng +
8094                     (uint) asc_dvc->last_q_shortage + (uint) ASC_MIN_FREE_Q;
8095         } else {
8096                 cur_used_qs = (uint) asc_dvc->cur_total_qng +
8097                     (uint) ASC_MIN_FREE_Q;
8098         }
8099         if ((uint) (cur_used_qs + n_qs) <= (uint) asc_dvc->max_total_qng) {
8100                 cur_free_qs = (uint) asc_dvc->max_total_qng - cur_used_qs;
8101                 if (asc_dvc->cur_dvc_qng[tid_no] >=
8102                     asc_dvc->max_dvc_qng[tid_no]) {
8103                         return 0;
8104                 }
8105                 return cur_free_qs;
8106         }
8107         if (n_qs > 1) {
8108                 if ((n_qs > asc_dvc->last_q_shortage)
8109                     && (n_qs <= (asc_dvc->max_total_qng - ASC_MIN_FREE_Q))) {
8110                         asc_dvc->last_q_shortage = n_qs;
8111                 }
8112         }
8113         return 0;
8114 }
8115
8116 static uchar AscAllocFreeQueue(PortAddr iop_base, uchar free_q_head)
8117 {
8118         ushort q_addr;
8119         uchar next_qp;
8120         uchar q_status;
8121
8122         q_addr = ASC_QNO_TO_QADDR(free_q_head);
8123         q_status = (uchar)AscReadLramByte(iop_base,
8124                                           (ushort)(q_addr +
8125                                                    ASC_SCSIQ_B_STATUS));
8126         next_qp = AscReadLramByte(iop_base, (ushort)(q_addr + ASC_SCSIQ_B_FWD));
8127         if (((q_status & QS_READY) == 0) && (next_qp != ASC_QLINK_END))
8128                 return next_qp;
8129         return ASC_QLINK_END;
8130 }
8131
8132 static uchar
8133 AscAllocMultipleFreeQueue(PortAddr iop_base, uchar free_q_head, uchar n_free_q)
8134 {
8135         uchar i;
8136
8137         for (i = 0; i < n_free_q; i++) {
8138                 free_q_head = AscAllocFreeQueue(iop_base, free_q_head);
8139                 if (free_q_head == ASC_QLINK_END)
8140                         break;
8141         }
8142         return free_q_head;
8143 }
8144
8145 /*
8146  * void
8147  * DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
8148  *
8149  * Calling/Exit State:
8150  *    none
8151  *
8152  * Description:
8153  *     Output an ASC_SCSI_Q structure to the chip
8154  */
8155 static void
8156 DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
8157 {
8158         int i;
8159
8160         ASC_DBG_PRT_HEX(2, "DvcPutScsiQ", outbuf, 2 * words);
8161         AscSetChipLramAddr(iop_base, s_addr);
8162         for (i = 0; i < 2 * words; i += 2) {
8163                 if (i == 4 || i == 20) {
8164                         continue;
8165                 }
8166                 outpw(iop_base + IOP_RAM_DATA,
8167                       ((ushort)outbuf[i + 1] << 8) | outbuf[i]);
8168         }
8169 }
8170
8171 static int AscPutReadyQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8172 {
8173         ushort q_addr;
8174         uchar tid_no;
8175         uchar sdtr_data;
8176         uchar syn_period_ix;
8177         uchar syn_offset;
8178         PortAddr iop_base;
8179
8180         iop_base = asc_dvc->iop_base;
8181         if (((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) &&
8182             ((asc_dvc->sdtr_done & scsiq->q1.target_id) == 0)) {
8183                 tid_no = ASC_TIX_TO_TID(scsiq->q2.target_ix);
8184                 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8185                 syn_period_ix =
8186                     (sdtr_data >> 4) & (asc_dvc->max_sdtr_index - 1);
8187                 syn_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
8188                 AscMsgOutSDTR(asc_dvc,
8189                               asc_dvc->sdtr_period_tbl[syn_period_ix],
8190                               syn_offset);
8191                 scsiq->q1.cntl |= QC_MSG_OUT;
8192         }
8193         q_addr = ASC_QNO_TO_QADDR(q_no);
8194         if ((scsiq->q1.target_id & asc_dvc->use_tagged_qng) == 0) {
8195                 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
8196         }
8197         scsiq->q1.status = QS_FREE;
8198         AscMemWordCopyPtrToLram(iop_base,
8199                                 q_addr + ASC_SCSIQ_CDB_BEG,
8200                                 (uchar *)scsiq->cdbptr, scsiq->q2.cdb_len >> 1);
8201
8202         DvcPutScsiQ(iop_base,
8203                     q_addr + ASC_SCSIQ_CPY_BEG,
8204                     (uchar *)&scsiq->q1.cntl,
8205                     ((sizeof(ASC_SCSIQ_1) + sizeof(ASC_SCSIQ_2)) / 2) - 1);
8206         AscWriteLramWord(iop_base,
8207                          (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS),
8208                          (ushort)(((ushort)scsiq->q1.
8209                                    q_no << 8) | (ushort)QS_READY));
8210         return 1;
8211 }
8212
8213 static int
8214 AscPutReadySgListQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8215 {
8216         int sta;
8217         int i;
8218         ASC_SG_HEAD *sg_head;
8219         ASC_SG_LIST_Q scsi_sg_q;
8220         __le32 saved_data_addr;
8221         __le32 saved_data_cnt;
8222         PortAddr iop_base;
8223         ushort sg_list_dwords;
8224         ushort sg_index;
8225         ushort sg_entry_cnt;
8226         ushort q_addr;
8227         uchar next_qp;
8228
8229         iop_base = asc_dvc->iop_base;
8230         sg_head = scsiq->sg_head;
8231         saved_data_addr = scsiq->q1.data_addr;
8232         saved_data_cnt = scsiq->q1.data_cnt;
8233         scsiq->q1.data_addr = sg_head->sg_list[0].addr;
8234         scsiq->q1.data_cnt = sg_head->sg_list[0].bytes;
8235 #if CC_VERY_LONG_SG_LIST
8236         /*
8237          * If sg_head->entry_cnt is greater than ASC_MAX_SG_LIST
8238          * then not all SG elements will fit in the allocated queues.
8239          * The rest of the SG elements will be copied when the RISC
8240          * completes the SG elements that fit and halts.
8241          */
8242         if (sg_head->entry_cnt > ASC_MAX_SG_LIST) {
8243                 /*
8244                  * Set sg_entry_cnt to be the number of SG elements that
8245                  * will fit in the allocated SG queues. It is minus 1, because
8246                  * the first SG element is handled above. ASC_MAX_SG_LIST is
8247                  * already inflated by 1 to account for this. For example it
8248                  * may be 50 which is 1 + 7 queues * 7 SG elements.
8249                  */
8250                 sg_entry_cnt = ASC_MAX_SG_LIST - 1;
8251
8252                 /*
8253                  * Keep track of remaining number of SG elements that will
8254                  * need to be handled from a_isr.c.
8255                  */
8256                 scsiq->remain_sg_entry_cnt =
8257                     sg_head->entry_cnt - ASC_MAX_SG_LIST;
8258         } else {
8259 #endif /* CC_VERY_LONG_SG_LIST */
8260                 /*
8261                  * Set sg_entry_cnt to be the number of SG elements that
8262                  * will fit in the allocated SG queues. It is minus 1, because
8263                  * the first SG element is handled above.
8264                  */
8265                 sg_entry_cnt = sg_head->entry_cnt - 1;
8266 #if CC_VERY_LONG_SG_LIST
8267         }
8268 #endif /* CC_VERY_LONG_SG_LIST */
8269         if (sg_entry_cnt != 0) {
8270                 scsiq->q1.cntl |= QC_SG_HEAD;
8271                 q_addr = ASC_QNO_TO_QADDR(q_no);
8272                 sg_index = 1;
8273                 scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
8274                 scsi_sg_q.sg_head_qp = q_no;
8275                 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
8276                 for (i = 0; i < sg_head->queue_cnt; i++) {
8277                         scsi_sg_q.seq_no = i + 1;
8278                         if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
8279                                 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
8280                                 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
8281                                 if (i == 0) {
8282                                         scsi_sg_q.sg_list_cnt =
8283                                             ASC_SG_LIST_PER_Q;
8284                                         scsi_sg_q.sg_cur_list_cnt =
8285                                             ASC_SG_LIST_PER_Q;
8286                                 } else {
8287                                         scsi_sg_q.sg_list_cnt =
8288                                             ASC_SG_LIST_PER_Q - 1;
8289                                         scsi_sg_q.sg_cur_list_cnt =
8290                                             ASC_SG_LIST_PER_Q - 1;
8291                                 }
8292                         } else {
8293 #if CC_VERY_LONG_SG_LIST
8294                                 /*
8295                                  * This is the last SG queue in the list of
8296                                  * allocated SG queues. If there are more
8297                                  * SG elements than will fit in the allocated
8298                                  * queues, then set the QCSG_SG_XFER_MORE flag.
8299                                  */
8300                                 if (sg_head->entry_cnt > ASC_MAX_SG_LIST) {
8301                                         scsi_sg_q.cntl |= QCSG_SG_XFER_MORE;
8302                                 } else {
8303 #endif /* CC_VERY_LONG_SG_LIST */
8304                                         scsi_sg_q.cntl |= QCSG_SG_XFER_END;
8305 #if CC_VERY_LONG_SG_LIST
8306                                 }
8307 #endif /* CC_VERY_LONG_SG_LIST */
8308                                 sg_list_dwords = sg_entry_cnt << 1;
8309                                 if (i == 0) {
8310                                         scsi_sg_q.sg_list_cnt = sg_entry_cnt;
8311                                         scsi_sg_q.sg_cur_list_cnt =
8312                                             sg_entry_cnt;
8313                                 } else {
8314                                         scsi_sg_q.sg_list_cnt =
8315                                             sg_entry_cnt - 1;
8316                                         scsi_sg_q.sg_cur_list_cnt =
8317                                             sg_entry_cnt - 1;
8318                                 }
8319                                 sg_entry_cnt = 0;
8320                         }
8321                         next_qp = AscReadLramByte(iop_base,
8322                                                   (ushort)(q_addr +
8323                                                            ASC_SCSIQ_B_FWD));
8324                         scsi_sg_q.q_no = next_qp;
8325                         q_addr = ASC_QNO_TO_QADDR(next_qp);
8326                         AscMemWordCopyPtrToLram(iop_base,
8327                                                 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
8328                                                 (uchar *)&scsi_sg_q,
8329                                                 sizeof(ASC_SG_LIST_Q) >> 1);
8330                         AscMemDWordCopyPtrToLram(iop_base,
8331                                                  q_addr + ASC_SGQ_LIST_BEG,
8332                                                  (uchar *)&sg_head->
8333                                                  sg_list[sg_index],
8334                                                  sg_list_dwords);
8335                         sg_index += ASC_SG_LIST_PER_Q;
8336                         scsiq->next_sg_index = sg_index;
8337                 }
8338         } else {
8339                 scsiq->q1.cntl &= ~QC_SG_HEAD;
8340         }
8341         sta = AscPutReadyQueue(asc_dvc, scsiq, q_no);
8342         scsiq->q1.data_addr = saved_data_addr;
8343         scsiq->q1.data_cnt = saved_data_cnt;
8344         return (sta);
8345 }
8346
8347 static int
8348 AscSendScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar n_q_required)
8349 {
8350         PortAddr iop_base;
8351         uchar free_q_head;
8352         uchar next_qp;
8353         uchar tid_no;
8354         uchar target_ix;
8355         int sta;
8356
8357         iop_base = asc_dvc->iop_base;
8358         target_ix = scsiq->q2.target_ix;
8359         tid_no = ASC_TIX_TO_TID(target_ix);
8360         sta = 0;
8361         free_q_head = (uchar)AscGetVarFreeQHead(iop_base);
8362         if (n_q_required > 1) {
8363                 next_qp = AscAllocMultipleFreeQueue(iop_base, free_q_head,
8364                                                     (uchar)n_q_required);
8365                 if (next_qp != ASC_QLINK_END) {
8366                         asc_dvc->last_q_shortage = 0;
8367                         scsiq->sg_head->queue_cnt = n_q_required - 1;
8368                         scsiq->q1.q_no = free_q_head;
8369                         sta = AscPutReadySgListQueue(asc_dvc, scsiq,
8370                                                      free_q_head);
8371                 }
8372         } else if (n_q_required == 1) {
8373                 next_qp = AscAllocFreeQueue(iop_base, free_q_head);
8374                 if (next_qp != ASC_QLINK_END) {
8375                         scsiq->q1.q_no = free_q_head;
8376                         sta = AscPutReadyQueue(asc_dvc, scsiq, free_q_head);
8377                 }
8378         }
8379         if (sta == 1) {
8380                 AscPutVarFreeQHead(iop_base, next_qp);
8381                 asc_dvc->cur_total_qng += n_q_required;
8382                 asc_dvc->cur_dvc_qng[tid_no]++;
8383         }
8384         return sta;
8385 }
8386
8387 #define ASC_SYN_OFFSET_ONE_DISABLE_LIST  16
8388 static uchar _syn_offset_one_disable_cmd[ASC_SYN_OFFSET_ONE_DISABLE_LIST] = {
8389         INQUIRY,
8390         REQUEST_SENSE,
8391         READ_CAPACITY,
8392         READ_TOC,
8393         MODE_SELECT,
8394         MODE_SENSE,
8395         MODE_SELECT_10,
8396         MODE_SENSE_10,
8397         0xFF,
8398         0xFF,
8399         0xFF,
8400         0xFF,
8401         0xFF,
8402         0xFF,
8403         0xFF,
8404         0xFF
8405 };
8406
8407 static int AscExeScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq)
8408 {
8409         PortAddr iop_base;
8410         int sta;
8411         int n_q_required;
8412         bool disable_syn_offset_one_fix;
8413         int i;
8414         u32 addr;
8415         ushort sg_entry_cnt = 0;
8416         ushort sg_entry_cnt_minus_one = 0;
8417         uchar target_ix;
8418         uchar tid_no;
8419         uchar sdtr_data;
8420         uchar extra_bytes;
8421         uchar scsi_cmd;
8422         uchar disable_cmd;
8423         ASC_SG_HEAD *sg_head;
8424         unsigned long data_cnt;
8425
8426         iop_base = asc_dvc->iop_base;
8427         sg_head = scsiq->sg_head;
8428         if (asc_dvc->err_code != 0)
8429                 return ASC_ERROR;
8430         scsiq->q1.q_no = 0;
8431         if ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0) {
8432                 scsiq->q1.extra_bytes = 0;
8433         }
8434         sta = 0;
8435         target_ix = scsiq->q2.target_ix;
8436         tid_no = ASC_TIX_TO_TID(target_ix);
8437         n_q_required = 1;
8438         if (scsiq->cdbptr[0] == REQUEST_SENSE) {
8439                 if ((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) {
8440                         asc_dvc->sdtr_done &= ~scsiq->q1.target_id;
8441                         sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8442                         AscMsgOutSDTR(asc_dvc,
8443                                       asc_dvc->
8444                                       sdtr_period_tbl[(sdtr_data >> 4) &
8445                                                       (uchar)(asc_dvc->
8446                                                               max_sdtr_index -
8447                                                               1)],
8448                                       (uchar)(sdtr_data & (uchar)
8449                                               ASC_SYN_MAX_OFFSET));
8450                         scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);
8451                 }
8452         }
8453         if (asc_dvc->in_critical_cnt != 0) {
8454                 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CRITICAL_RE_ENTRY);
8455                 return ASC_ERROR;
8456         }
8457         asc_dvc->in_critical_cnt++;
8458         if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8459                 if ((sg_entry_cnt = sg_head->entry_cnt) == 0) {
8460                         asc_dvc->in_critical_cnt--;
8461                         return ASC_ERROR;
8462                 }
8463 #if !CC_VERY_LONG_SG_LIST
8464                 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8465                         asc_dvc->in_critical_cnt--;
8466                         return ASC_ERROR;
8467                 }
8468 #endif /* !CC_VERY_LONG_SG_LIST */
8469                 if (sg_entry_cnt == 1) {
8470                         scsiq->q1.data_addr = sg_head->sg_list[0].addr;
8471                         scsiq->q1.data_cnt = sg_head->sg_list[0].bytes;
8472                         scsiq->q1.cntl &= ~(QC_SG_HEAD | QC_SG_SWAP_QUEUE);
8473                 }
8474                 sg_entry_cnt_minus_one = sg_entry_cnt - 1;
8475         }
8476         scsi_cmd = scsiq->cdbptr[0];
8477         disable_syn_offset_one_fix = false;
8478         if ((asc_dvc->pci_fix_asyn_xfer & scsiq->q1.target_id) &&
8479             !(asc_dvc->pci_fix_asyn_xfer_always & scsiq->q1.target_id)) {
8480                 if (scsiq->q1.cntl & QC_SG_HEAD) {
8481                         data_cnt = 0;
8482                         for (i = 0; i < sg_entry_cnt; i++) {
8483                                 data_cnt += le32_to_cpu(sg_head->sg_list[i].
8484                                                         bytes);
8485                         }
8486                 } else {
8487                         data_cnt = le32_to_cpu(scsiq->q1.data_cnt);
8488                 }
8489                 if (data_cnt != 0UL) {
8490                         if (data_cnt < 512UL) {
8491                                 disable_syn_offset_one_fix = true;
8492                         } else {
8493                                 for (i = 0; i < ASC_SYN_OFFSET_ONE_DISABLE_LIST;
8494                                      i++) {
8495                                         disable_cmd =
8496                                             _syn_offset_one_disable_cmd[i];
8497                                         if (disable_cmd == 0xFF) {
8498                                                 break;
8499                                         }
8500                                         if (scsi_cmd == disable_cmd) {
8501                                                 disable_syn_offset_one_fix =
8502                                                     true;
8503                                                 break;
8504                                         }
8505                                 }
8506                         }
8507                 }
8508         }
8509         if (disable_syn_offset_one_fix) {
8510                 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
8511                 scsiq->q2.tag_code |= (ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX |
8512                                        ASC_TAG_FLAG_DISABLE_DISCONNECT);
8513         } else {
8514                 scsiq->q2.tag_code &= 0x27;
8515         }
8516         if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8517                 if (asc_dvc->bug_fix_cntl) {
8518                         if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8519                                 if ((scsi_cmd == READ_6) ||
8520                                     (scsi_cmd == READ_10)) {
8521                                         addr = le32_to_cpu(sg_head->
8522                                                                    sg_list
8523                                                                    [sg_entry_cnt_minus_one].
8524                                                                    addr) +
8525                                                 le32_to_cpu(sg_head->
8526                                                                   sg_list
8527                                                                   [sg_entry_cnt_minus_one].
8528                                                                   bytes);
8529                                         extra_bytes =
8530                                             (uchar)((ushort)addr & 0x0003);
8531                                         if ((extra_bytes != 0)
8532                                             &&
8533                                             ((scsiq->q2.
8534                                               tag_code &
8535                                               ASC_TAG_FLAG_EXTRA_BYTES)
8536                                              == 0)) {
8537                                                 scsiq->q2.tag_code |=
8538                                                     ASC_TAG_FLAG_EXTRA_BYTES;
8539                                                 scsiq->q1.extra_bytes =
8540                                                     extra_bytes;
8541                                                 data_cnt =
8542                                                     le32_to_cpu(sg_head->
8543                                                                 sg_list
8544                                                                 [sg_entry_cnt_minus_one].
8545                                                                 bytes);
8546                                                 data_cnt -= extra_bytes;
8547                                                 sg_head->
8548                                                     sg_list
8549                                                     [sg_entry_cnt_minus_one].
8550                                                     bytes =
8551                                                     cpu_to_le32(data_cnt);
8552                                         }
8553                                 }
8554                         }
8555                 }
8556                 sg_head->entry_to_copy = sg_head->entry_cnt;
8557 #if CC_VERY_LONG_SG_LIST
8558                 /*
8559                  * Set the sg_entry_cnt to the maximum possible. The rest of
8560                  * the SG elements will be copied when the RISC completes the
8561                  * SG elements that fit and halts.
8562                  */
8563                 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8564                         sg_entry_cnt = ASC_MAX_SG_LIST;
8565                 }
8566 #endif /* CC_VERY_LONG_SG_LIST */
8567                 n_q_required = AscSgListToQueue(sg_entry_cnt);
8568                 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, n_q_required) >=
8569                      (uint) n_q_required)
8570                     || ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8571                         if ((sta =
8572                              AscSendScsiQueue(asc_dvc, scsiq,
8573                                               n_q_required)) == 1) {
8574                                 asc_dvc->in_critical_cnt--;
8575                                 return (sta);
8576                         }
8577                 }
8578         } else {
8579                 if (asc_dvc->bug_fix_cntl) {
8580                         if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8581                                 if ((scsi_cmd == READ_6) ||
8582                                     (scsi_cmd == READ_10)) {
8583                                         addr =
8584                                             le32_to_cpu(scsiq->q1.data_addr) +
8585                                             le32_to_cpu(scsiq->q1.data_cnt);
8586                                         extra_bytes =
8587                                             (uchar)((ushort)addr & 0x0003);
8588                                         if ((extra_bytes != 0)
8589                                             &&
8590                                             ((scsiq->q2.
8591                                               tag_code &
8592                                               ASC_TAG_FLAG_EXTRA_BYTES)
8593                                              == 0)) {
8594                                                 data_cnt =
8595                                                     le32_to_cpu(scsiq->q1.
8596                                                                 data_cnt);
8597                                                 if (((ushort)data_cnt & 0x01FF)
8598                                                     == 0) {
8599                                                         scsiq->q2.tag_code |=
8600                                                             ASC_TAG_FLAG_EXTRA_BYTES;
8601                                                         data_cnt -= extra_bytes;
8602                                                         scsiq->q1.data_cnt =
8603                                                             cpu_to_le32
8604                                                             (data_cnt);
8605                                                         scsiq->q1.extra_bytes =
8606                                                             extra_bytes;
8607                                                 }
8608                                         }
8609                                 }
8610                         }
8611                 }
8612                 n_q_required = 1;
8613                 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, 1) >= 1) ||
8614                     ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8615                         if ((sta = AscSendScsiQueue(asc_dvc, scsiq,
8616                                                     n_q_required)) == 1) {
8617                                 asc_dvc->in_critical_cnt--;
8618                                 return (sta);
8619                         }
8620                 }
8621         }
8622         asc_dvc->in_critical_cnt--;
8623         return (sta);
8624 }
8625
8626 /*
8627  * AdvExeScsiQueue() - Send a request to the RISC microcode program.
8628  *
8629  *   Allocate a carrier structure, point the carrier to the ADV_SCSI_REQ_Q,
8630  *   add the carrier to the ICQ (Initiator Command Queue), and tickle the
8631  *   RISC to notify it a new command is ready to be executed.
8632  *
8633  * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
8634  * set to SCSI_MAX_RETRY.
8635  *
8636  * Multi-byte fields in the ASC_SCSI_REQ_Q that are used by the microcode
8637  * for DMA addresses or math operations are byte swapped to little-endian
8638  * order.
8639  *
8640  * Return:
8641  *      ADV_SUCCESS(1) - The request was successfully queued.
8642  *      ADV_BUSY(0) -    Resource unavailable; Retry again after pending
8643  *                       request completes.
8644  *      ADV_ERROR(-1) -  Invalid ADV_SCSI_REQ_Q request structure
8645  *                       host IC error.
8646  */
8647 static int AdvExeScsiQueue(ADV_DVC_VAR *asc_dvc, adv_req_t *reqp)
8648 {
8649         AdvPortAddr iop_base;
8650         ADV_CARR_T *new_carrp;
8651         ADV_SCSI_REQ_Q *scsiq = &reqp->scsi_req_q;
8652
8653         /*
8654          * The ADV_SCSI_REQ_Q 'target_id' field should never exceed ADV_MAX_TID.
8655          */
8656         if (scsiq->target_id > ADV_MAX_TID) {
8657                 scsiq->host_status = QHSTA_M_INVALID_DEVICE;
8658                 scsiq->done_status = QD_WITH_ERROR;
8659                 return ADV_ERROR;
8660         }
8661
8662         iop_base = asc_dvc->iop_base;
8663
8664         /*
8665          * Allocate a carrier ensuring at least one carrier always
8666          * remains on the freelist and initialize fields.
8667          */
8668         new_carrp = adv_get_next_carrier(asc_dvc);
8669         if (!new_carrp) {
8670                 ASC_DBG(1, "No free carriers\n");
8671                 return ADV_BUSY;
8672         }
8673
8674         asc_dvc->carr_pending_cnt++;
8675
8676         /*
8677          * Clear the ADV_SCSI_REQ_Q done flag.
8678          */
8679         scsiq->a_flag &= ~ADV_SCSIQ_DONE;
8680
8681         /* Save virtual and physical address of ADV_SCSI_REQ_Q and carrier. */
8682         scsiq->scsiq_ptr = cpu_to_le32(scsiq->srb_tag);
8683         scsiq->scsiq_rptr = cpu_to_le32(reqp->req_addr);
8684
8685         scsiq->carr_va = asc_dvc->icq_sp->carr_va;
8686         scsiq->carr_pa = asc_dvc->icq_sp->carr_pa;
8687
8688         /*
8689          * Use the current stopper to send the ADV_SCSI_REQ_Q command to
8690          * the microcode. The newly allocated stopper will become the new
8691          * stopper.
8692          */
8693         asc_dvc->icq_sp->areq_vpa = scsiq->scsiq_rptr;
8694
8695         /*
8696          * Set the 'next_vpa' pointer for the old stopper to be the
8697          * physical address of the new stopper. The RISC can only
8698          * follow physical addresses.
8699          */
8700         asc_dvc->icq_sp->next_vpa = new_carrp->carr_pa;
8701
8702         /*
8703          * Set the host adapter stopper pointer to point to the new carrier.
8704          */
8705         asc_dvc->icq_sp = new_carrp;
8706
8707         if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
8708             asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
8709                 /*
8710                  * Tickle the RISC to tell it to read its Command Queue Head pointer.
8711                  */
8712                 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_A);
8713                 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
8714                         /*
8715                          * Clear the tickle value. In the ASC-3550 the RISC flag
8716                          * command 'clr_tickle_a' does not work unless the host
8717                          * value is cleared.
8718                          */
8719                         AdvWriteByteRegister(iop_base, IOPB_TICKLE,
8720                                              ADV_TICKLE_NOP);
8721                 }
8722         } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
8723                 /*
8724                  * Notify the RISC a carrier is ready by writing the physical
8725                  * address of the new carrier stopper to the COMMA register.
8726                  */
8727                 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
8728                                       le32_to_cpu(new_carrp->carr_pa));
8729         }
8730
8731         return ADV_SUCCESS;
8732 }
8733
8734 /*
8735  * Execute a single 'Scsi_Cmnd'.
8736  */
8737 static int asc_execute_scsi_cmnd(struct scsi_cmnd *scp)
8738 {
8739         int ret, err_code;
8740         struct asc_board *boardp = shost_priv(scp->device->host);
8741
8742         ASC_DBG(1, "scp 0x%p\n", scp);
8743
8744         if (ASC_NARROW_BOARD(boardp)) {
8745                 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
8746                 struct asc_scsi_q asc_scsi_q;
8747
8748                 /* asc_build_req() can not return ASC_BUSY. */
8749                 ret = asc_build_req(boardp, scp, &asc_scsi_q);
8750                 if (ret == ASC_ERROR) {
8751                         ASC_STATS(scp->device->host, build_error);
8752                         return ASC_ERROR;
8753                 }
8754
8755                 ret = AscExeScsiQueue(asc_dvc, &asc_scsi_q);
8756                 kfree(asc_scsi_q.sg_head);
8757                 err_code = asc_dvc->err_code;
8758         } else {
8759                 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
8760                 adv_req_t *adv_reqp;
8761
8762                 switch (adv_build_req(boardp, scp, &adv_reqp)) {
8763                 case ASC_NOERROR:
8764                         ASC_DBG(3, "adv_build_req ASC_NOERROR\n");
8765                         break;
8766                 case ASC_BUSY:
8767                         ASC_DBG(1, "adv_build_req ASC_BUSY\n");
8768                         /*
8769                          * The asc_stats fields 'adv_build_noreq' and
8770                          * 'adv_build_nosg' count wide board busy conditions.
8771                          * They are updated in adv_build_req and
8772                          * adv_get_sglist, respectively.
8773                          */
8774                         return ASC_BUSY;
8775                 case ASC_ERROR:
8776                 default:
8777                         ASC_DBG(1, "adv_build_req ASC_ERROR\n");
8778                         ASC_STATS(scp->device->host, build_error);
8779                         return ASC_ERROR;
8780                 }
8781
8782                 ret = AdvExeScsiQueue(adv_dvc, adv_reqp);
8783                 err_code = adv_dvc->err_code;
8784         }
8785
8786         switch (ret) {
8787         case ASC_NOERROR:
8788                 ASC_STATS(scp->device->host, exe_noerror);
8789                 /*
8790                  * Increment monotonically increasing per device
8791                  * successful request counter. Wrapping doesn't matter.
8792                  */
8793                 boardp->reqcnt[scp->device->id]++;
8794                 ASC_DBG(1, "ExeScsiQueue() ASC_NOERROR\n");
8795                 break;
8796         case ASC_BUSY:
8797                 ASC_DBG(1, "ExeScsiQueue() ASC_BUSY\n");
8798                 ASC_STATS(scp->device->host, exe_busy);
8799                 break;
8800         case ASC_ERROR:
8801                 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() ASC_ERROR, "
8802                         "err_code 0x%x\n", err_code);
8803                 ASC_STATS(scp->device->host, exe_error);
8804                 scp->result = HOST_BYTE(DID_ERROR);
8805                 break;
8806         default:
8807                 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() unknown, "
8808                         "err_code 0x%x\n", err_code);
8809                 ASC_STATS(scp->device->host, exe_unknown);
8810                 scp->result = HOST_BYTE(DID_ERROR);
8811                 break;
8812         }
8813
8814         ASC_DBG(1, "end\n");
8815         return ret;
8816 }
8817
8818 /*
8819  * advansys_queuecommand() - interrupt-driven I/O entrypoint.
8820  *
8821  * This function always returns 0. Command return status is saved
8822  * in the 'scp' result field.
8823  */
8824 static int
8825 advansys_queuecommand_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
8826 {
8827         struct Scsi_Host *shost = scp->device->host;
8828         int asc_res, result = 0;
8829
8830         ASC_STATS(shost, queuecommand);
8831         scp->scsi_done = done;
8832
8833         asc_res = asc_execute_scsi_cmnd(scp);
8834
8835         switch (asc_res) {
8836         case ASC_NOERROR:
8837                 break;
8838         case ASC_BUSY:
8839                 result = SCSI_MLQUEUE_HOST_BUSY;
8840                 break;
8841         case ASC_ERROR:
8842         default:
8843                 asc_scsi_done(scp);
8844                 break;
8845         }
8846
8847         return result;
8848 }
8849
8850 static DEF_SCSI_QCMD(advansys_queuecommand)
8851
8852 static ushort AscGetEisaChipCfg(PortAddr iop_base)
8853 {
8854         PortAddr eisa_cfg_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8855             (PortAddr) (ASC_EISA_CFG_IOP_MASK);
8856         return inpw(eisa_cfg_iop);
8857 }
8858
8859 /*
8860  * Return the BIOS address of the adapter at the specified
8861  * I/O port and with the specified bus type.
8862  */
8863 static unsigned short AscGetChipBiosAddress(PortAddr iop_base,
8864                                             unsigned short bus_type)
8865 {
8866         unsigned short cfg_lsw;
8867         unsigned short bios_addr;
8868
8869         /*
8870          * The PCI BIOS is re-located by the motherboard BIOS. Because
8871          * of this the driver can not determine where a PCI BIOS is
8872          * loaded and executes.
8873          */
8874         if (bus_type & ASC_IS_PCI)
8875                 return 0;
8876
8877         if ((bus_type & ASC_IS_EISA) != 0) {
8878                 cfg_lsw = AscGetEisaChipCfg(iop_base);
8879                 cfg_lsw &= 0x000F;
8880                 bios_addr = ASC_BIOS_MIN_ADDR + cfg_lsw * ASC_BIOS_BANK_SIZE;
8881                 return bios_addr;
8882         }
8883
8884         cfg_lsw = AscGetChipCfgLsw(iop_base);
8885
8886         /*
8887          *  ISA PnP uses the top bit as the 32K BIOS flag
8888          */
8889         if (bus_type == ASC_IS_ISAPNP)
8890                 cfg_lsw &= 0x7FFF;
8891         bios_addr = ASC_BIOS_MIN_ADDR + (cfg_lsw >> 12) * ASC_BIOS_BANK_SIZE;
8892         return bios_addr;
8893 }
8894
8895 static uchar AscSetChipScsiID(PortAddr iop_base, uchar new_host_id)
8896 {
8897         ushort cfg_lsw;
8898
8899         if (AscGetChipScsiID(iop_base) == new_host_id) {
8900                 return (new_host_id);
8901         }
8902         cfg_lsw = AscGetChipCfgLsw(iop_base);
8903         cfg_lsw &= 0xF8FF;
8904         cfg_lsw |= (ushort)((new_host_id & ASC_MAX_TID) << 8);
8905         AscSetChipCfgLsw(iop_base, cfg_lsw);
8906         return (AscGetChipScsiID(iop_base));
8907 }
8908
8909 static unsigned char AscGetChipScsiCtrl(PortAddr iop_base)
8910 {
8911         unsigned char sc;
8912
8913         AscSetBank(iop_base, 1);
8914         sc = inp(iop_base + IOP_REG_SC);
8915         AscSetBank(iop_base, 0);
8916         return sc;
8917 }
8918
8919 static unsigned char AscGetChipVersion(PortAddr iop_base,
8920                                        unsigned short bus_type)
8921 {
8922         if (bus_type & ASC_IS_EISA) {
8923                 PortAddr eisa_iop;
8924                 unsigned char revision;
8925                 eisa_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8926                     (PortAddr) ASC_EISA_REV_IOP_MASK;
8927                 revision = inp(eisa_iop);
8928                 return ASC_CHIP_MIN_VER_EISA - 1 + revision;
8929         }
8930         return AscGetChipVerNo(iop_base);
8931 }
8932
8933 #ifdef CONFIG_ISA
8934 static void AscEnableIsaDma(uchar dma_channel)
8935 {
8936         if (dma_channel < 4) {
8937                 outp(0x000B, (ushort)(0xC0 | dma_channel));
8938                 outp(0x000A, dma_channel);
8939         } else if (dma_channel < 8) {
8940                 outp(0x00D6, (ushort)(0xC0 | (dma_channel - 4)));
8941                 outp(0x00D4, (ushort)(dma_channel - 4));
8942         }
8943 }
8944 #endif /* CONFIG_ISA */
8945
8946 static int AscStopQueueExe(PortAddr iop_base)
8947 {
8948         int count = 0;
8949
8950         if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) == 0) {
8951                 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
8952                                  ASC_STOP_REQ_RISC_STOP);
8953                 do {
8954                         if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) &
8955                             ASC_STOP_ACK_RISC_STOP) {
8956                                 return (1);
8957                         }
8958                         mdelay(100);
8959                 } while (count++ < 20);
8960         }
8961         return (0);
8962 }
8963
8964 static unsigned int AscGetMaxDmaCount(ushort bus_type)
8965 {
8966         if (bus_type & ASC_IS_ISA)
8967                 return ASC_MAX_ISA_DMA_COUNT;
8968         else if (bus_type & (ASC_IS_EISA | ASC_IS_VL))
8969                 return ASC_MAX_VL_DMA_COUNT;
8970         return ASC_MAX_PCI_DMA_COUNT;
8971 }
8972
8973 #ifdef CONFIG_ISA
8974 static ushort AscGetIsaDmaChannel(PortAddr iop_base)
8975 {
8976         ushort channel;
8977
8978         channel = AscGetChipCfgLsw(iop_base) & 0x0003;
8979         if (channel == 0x03)
8980                 return (0);
8981         else if (channel == 0x00)
8982                 return (7);
8983         return (channel + 4);
8984 }
8985
8986 static ushort AscSetIsaDmaChannel(PortAddr iop_base, ushort dma_channel)
8987 {
8988         ushort cfg_lsw;
8989         uchar value;
8990
8991         if ((dma_channel >= 5) && (dma_channel <= 7)) {
8992                 if (dma_channel == 7)
8993                         value = 0x00;
8994                 else
8995                         value = dma_channel - 4;
8996                 cfg_lsw = AscGetChipCfgLsw(iop_base) & 0xFFFC;
8997                 cfg_lsw |= value;
8998                 AscSetChipCfgLsw(iop_base, cfg_lsw);
8999                 return (AscGetIsaDmaChannel(iop_base));
9000         }
9001         return 0;
9002 }
9003
9004 static uchar AscGetIsaDmaSpeed(PortAddr iop_base)
9005 {
9006         uchar speed_value;
9007
9008         AscSetBank(iop_base, 1);
9009         speed_value = AscReadChipDmaSpeed(iop_base);
9010         speed_value &= 0x07;
9011         AscSetBank(iop_base, 0);
9012         return speed_value;
9013 }
9014
9015 static uchar AscSetIsaDmaSpeed(PortAddr iop_base, uchar speed_value)
9016 {
9017         speed_value &= 0x07;
9018         AscSetBank(iop_base, 1);
9019         AscWriteChipDmaSpeed(iop_base, speed_value);
9020         AscSetBank(iop_base, 0);
9021         return AscGetIsaDmaSpeed(iop_base);
9022 }
9023 #endif /* CONFIG_ISA */
9024
9025 static void AscInitAscDvcVar(ASC_DVC_VAR *asc_dvc)
9026 {
9027         int i;
9028         PortAddr iop_base;
9029         uchar chip_version;
9030
9031         iop_base = asc_dvc->iop_base;
9032         asc_dvc->err_code = 0;
9033         if ((asc_dvc->bus_type &
9034              (ASC_IS_ISA | ASC_IS_PCI | ASC_IS_EISA | ASC_IS_VL)) == 0) {
9035                 asc_dvc->err_code |= ASC_IERR_NO_BUS_TYPE;
9036         }
9037         AscSetChipControl(iop_base, CC_HALT);
9038         AscSetChipStatus(iop_base, 0);
9039         asc_dvc->bug_fix_cntl = 0;
9040         asc_dvc->pci_fix_asyn_xfer = 0;
9041         asc_dvc->pci_fix_asyn_xfer_always = 0;
9042         /* asc_dvc->init_state initialized in AscInitGetConfig(). */
9043         asc_dvc->sdtr_done = 0;
9044         asc_dvc->cur_total_qng = 0;
9045         asc_dvc->is_in_int = false;
9046         asc_dvc->in_critical_cnt = 0;
9047         asc_dvc->last_q_shortage = 0;
9048         asc_dvc->use_tagged_qng = 0;
9049         asc_dvc->no_scam = 0;
9050         asc_dvc->unit_not_ready = 0;
9051         asc_dvc->queue_full_or_busy = 0;
9052         asc_dvc->redo_scam = 0;
9053         asc_dvc->res2 = 0;
9054         asc_dvc->min_sdtr_index = 0;
9055         asc_dvc->cfg->can_tagged_qng = 0;
9056         asc_dvc->cfg->cmd_qng_enabled = 0;
9057         asc_dvc->dvc_cntl = ASC_DEF_DVC_CNTL;
9058         asc_dvc->init_sdtr = 0;
9059         asc_dvc->max_total_qng = ASC_DEF_MAX_TOTAL_QNG;
9060         asc_dvc->scsi_reset_wait = 3;
9061         asc_dvc->start_motor = ASC_SCSI_WIDTH_BIT_SET;
9062         asc_dvc->max_dma_count = AscGetMaxDmaCount(asc_dvc->bus_type);
9063         asc_dvc->cfg->sdtr_enable = ASC_SCSI_WIDTH_BIT_SET;
9064         asc_dvc->cfg->disc_enable = ASC_SCSI_WIDTH_BIT_SET;
9065         asc_dvc->cfg->chip_scsi_id = ASC_DEF_CHIP_SCSI_ID;
9066         chip_version = AscGetChipVersion(iop_base, asc_dvc->bus_type);
9067         asc_dvc->cfg->chip_version = chip_version;
9068         asc_dvc->sdtr_period_tbl = asc_syn_xfer_period;
9069         asc_dvc->max_sdtr_index = 7;
9070         if ((asc_dvc->bus_type & ASC_IS_PCI) &&
9071             (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3150)) {
9072                 asc_dvc->bus_type = ASC_IS_PCI_ULTRA;
9073                 asc_dvc->sdtr_period_tbl = asc_syn_ultra_xfer_period;
9074                 asc_dvc->max_sdtr_index = 15;
9075                 if (chip_version == ASC_CHIP_VER_PCI_ULTRA_3150) {
9076                         AscSetExtraControl(iop_base,
9077                                            (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
9078                 } else if (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3050) {
9079                         AscSetExtraControl(iop_base,
9080                                            (SEC_ACTIVE_NEGATE |
9081                                             SEC_ENABLE_FILTER));
9082                 }
9083         }
9084         if (asc_dvc->bus_type == ASC_IS_PCI) {
9085                 AscSetExtraControl(iop_base,
9086                                    (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
9087         }
9088
9089         asc_dvc->cfg->isa_dma_speed = ASC_DEF_ISA_DMA_SPEED;
9090 #ifdef CONFIG_ISA
9091         if ((asc_dvc->bus_type & ASC_IS_ISA) != 0) {
9092                 if (chip_version >= ASC_CHIP_MIN_VER_ISA_PNP) {
9093                         AscSetChipIFC(iop_base, IFC_INIT_DEFAULT);
9094                         asc_dvc->bus_type = ASC_IS_ISAPNP;
9095                 }
9096                 asc_dvc->cfg->isa_dma_channel =
9097                     (uchar)AscGetIsaDmaChannel(iop_base);
9098         }
9099 #endif /* CONFIG_ISA */
9100         for (i = 0; i <= ASC_MAX_TID; i++) {
9101                 asc_dvc->cur_dvc_qng[i] = 0;
9102                 asc_dvc->max_dvc_qng[i] = ASC_MAX_SCSI1_QNG;
9103                 asc_dvc->scsiq_busy_head[i] = (ASC_SCSI_Q *)0L;
9104                 asc_dvc->scsiq_busy_tail[i] = (ASC_SCSI_Q *)0L;
9105                 asc_dvc->cfg->max_tag_qng[i] = ASC_MAX_INRAM_TAG_QNG;
9106         }
9107 }
9108
9109 static int AscWriteEEPCmdReg(PortAddr iop_base, uchar cmd_reg)
9110 {
9111         int retry;
9112
9113         for (retry = 0; retry < ASC_EEP_MAX_RETRY; retry++) {
9114                 unsigned char read_back;
9115                 AscSetChipEEPCmd(iop_base, cmd_reg);
9116                 mdelay(1);
9117                 read_back = AscGetChipEEPCmd(iop_base);
9118                 if (read_back == cmd_reg)
9119                         return 1;
9120         }
9121         return 0;
9122 }
9123
9124 static void AscWaitEEPRead(void)
9125 {
9126         mdelay(1);
9127 }
9128
9129 static ushort AscReadEEPWord(PortAddr iop_base, uchar addr)
9130 {
9131         ushort read_wval;
9132         uchar cmd_reg;
9133
9134         AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
9135         AscWaitEEPRead();
9136         cmd_reg = addr | ASC_EEP_CMD_READ;
9137         AscWriteEEPCmdReg(iop_base, cmd_reg);
9138         AscWaitEEPRead();
9139         read_wval = AscGetChipEEPData(iop_base);
9140         AscWaitEEPRead();
9141         return read_wval;
9142 }
9143
9144 static ushort AscGetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9145                               ushort bus_type)
9146 {
9147         ushort wval;
9148         ushort sum;
9149         ushort *wbuf;
9150         int cfg_beg;
9151         int cfg_end;
9152         int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9153         int s_addr;
9154
9155         wbuf = (ushort *)cfg_buf;
9156         sum = 0;
9157         /* Read two config words; Byte-swapping done by AscReadEEPWord(). */
9158         for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9159                 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
9160                 sum += *wbuf;
9161         }
9162         if (bus_type & ASC_IS_VL) {
9163                 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9164                 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9165         } else {
9166                 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9167                 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9168         }
9169         for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9170                 wval = AscReadEEPWord(iop_base, (uchar)s_addr);
9171                 if (s_addr <= uchar_end_in_config) {
9172                         /*
9173                          * Swap all char fields - must unswap bytes already swapped
9174                          * by AscReadEEPWord().
9175                          */
9176                         *wbuf = le16_to_cpu(wval);
9177                 } else {
9178                         /* Don't swap word field at the end - cntl field. */
9179                         *wbuf = wval;
9180                 }
9181                 sum += wval;    /* Checksum treats all EEPROM data as words. */
9182         }
9183         /*
9184          * Read the checksum word which will be compared against 'sum'
9185          * by the caller. Word field already swapped.
9186          */
9187         *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
9188         return sum;
9189 }
9190
9191 static int AscTestExternalLram(ASC_DVC_VAR *asc_dvc)
9192 {
9193         PortAddr iop_base;
9194         ushort q_addr;
9195         ushort saved_word;
9196         int sta;
9197
9198         iop_base = asc_dvc->iop_base;
9199         sta = 0;
9200         q_addr = ASC_QNO_TO_QADDR(241);
9201         saved_word = AscReadLramWord(iop_base, q_addr);
9202         AscSetChipLramAddr(iop_base, q_addr);
9203         AscSetChipLramData(iop_base, 0x55AA);
9204         mdelay(10);
9205         AscSetChipLramAddr(iop_base, q_addr);
9206         if (AscGetChipLramData(iop_base) == 0x55AA) {
9207                 sta = 1;
9208                 AscWriteLramWord(iop_base, q_addr, saved_word);
9209         }
9210         return (sta);
9211 }
9212
9213 static void AscWaitEEPWrite(void)
9214 {
9215         mdelay(20);
9216 }
9217
9218 static int AscWriteEEPDataReg(PortAddr iop_base, ushort data_reg)
9219 {
9220         ushort read_back;
9221         int retry;
9222
9223         retry = 0;
9224         while (true) {
9225                 AscSetChipEEPData(iop_base, data_reg);
9226                 mdelay(1);
9227                 read_back = AscGetChipEEPData(iop_base);
9228                 if (read_back == data_reg) {
9229                         return (1);
9230                 }
9231                 if (retry++ > ASC_EEP_MAX_RETRY) {
9232                         return (0);
9233                 }
9234         }
9235 }
9236
9237 static ushort AscWriteEEPWord(PortAddr iop_base, uchar addr, ushort word_val)
9238 {
9239         ushort read_wval;
9240
9241         read_wval = AscReadEEPWord(iop_base, addr);
9242         if (read_wval != word_val) {
9243                 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_ABLE);
9244                 AscWaitEEPRead();
9245                 AscWriteEEPDataReg(iop_base, word_val);
9246                 AscWaitEEPRead();
9247                 AscWriteEEPCmdReg(iop_base,
9248                                   (uchar)((uchar)ASC_EEP_CMD_WRITE | addr));
9249                 AscWaitEEPWrite();
9250                 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
9251                 AscWaitEEPRead();
9252                 return (AscReadEEPWord(iop_base, addr));
9253         }
9254         return (read_wval);
9255 }
9256
9257 static int AscSetEEPConfigOnce(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9258                                ushort bus_type)
9259 {
9260         int n_error;
9261         ushort *wbuf;
9262         ushort word;
9263         ushort sum;
9264         int s_addr;
9265         int cfg_beg;
9266         int cfg_end;
9267         int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9268
9269         wbuf = (ushort *)cfg_buf;
9270         n_error = 0;
9271         sum = 0;
9272         /* Write two config words; AscWriteEEPWord() will swap bytes. */
9273         for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9274                 sum += *wbuf;
9275                 if (*wbuf != AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9276                         n_error++;
9277                 }
9278         }
9279         if (bus_type & ASC_IS_VL) {
9280                 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9281                 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9282         } else {
9283                 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9284                 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9285         }
9286         for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9287                 if (s_addr <= uchar_end_in_config) {
9288                         /*
9289                          * This is a char field. Swap char fields before they are
9290                          * swapped again by AscWriteEEPWord().
9291                          */
9292                         word = cpu_to_le16(*wbuf);
9293                         if (word !=
9294                             AscWriteEEPWord(iop_base, (uchar)s_addr, word)) {
9295                                 n_error++;
9296                         }
9297                 } else {
9298                         /* Don't swap word field at the end - cntl field. */
9299                         if (*wbuf !=
9300                             AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9301                                 n_error++;
9302                         }
9303                 }
9304                 sum += *wbuf;   /* Checksum calculated from word values. */
9305         }
9306         /* Write checksum word. It will be swapped by AscWriteEEPWord(). */
9307         *wbuf = sum;
9308         if (sum != AscWriteEEPWord(iop_base, (uchar)s_addr, sum)) {
9309                 n_error++;
9310         }
9311
9312         /* Read EEPROM back again. */
9313         wbuf = (ushort *)cfg_buf;
9314         /*
9315          * Read two config words; Byte-swapping done by AscReadEEPWord().
9316          */
9317         for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9318                 if (*wbuf != AscReadEEPWord(iop_base, (uchar)s_addr)) {
9319                         n_error++;
9320                 }
9321         }
9322         if (bus_type & ASC_IS_VL) {
9323                 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9324                 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9325         } else {
9326                 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9327                 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9328         }
9329         for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9330                 if (s_addr <= uchar_end_in_config) {
9331                         /*
9332                          * Swap all char fields. Must unswap bytes already swapped
9333                          * by AscReadEEPWord().
9334                          */
9335                         word =
9336                             le16_to_cpu(AscReadEEPWord
9337                                         (iop_base, (uchar)s_addr));
9338                 } else {
9339                         /* Don't swap word field at the end - cntl field. */
9340                         word = AscReadEEPWord(iop_base, (uchar)s_addr);
9341                 }
9342                 if (*wbuf != word) {
9343                         n_error++;
9344                 }
9345         }
9346         /* Read checksum; Byte swapping not needed. */
9347         if (AscReadEEPWord(iop_base, (uchar)s_addr) != sum) {
9348                 n_error++;
9349         }
9350         return n_error;
9351 }
9352
9353 static int AscSetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9354                            ushort bus_type)
9355 {
9356         int retry;
9357         int n_error;
9358
9359         retry = 0;
9360         while (true) {
9361                 if ((n_error = AscSetEEPConfigOnce(iop_base, cfg_buf,
9362                                                    bus_type)) == 0) {
9363                         break;
9364                 }
9365                 if (++retry > ASC_EEP_MAX_RETRY) {
9366                         break;
9367                 }
9368         }
9369         return n_error;
9370 }
9371
9372 static int AscInitFromEEP(ASC_DVC_VAR *asc_dvc)
9373 {
9374         ASCEEP_CONFIG eep_config_buf;
9375         ASCEEP_CONFIG *eep_config;
9376         PortAddr iop_base;
9377         ushort chksum;
9378         ushort warn_code;
9379         ushort cfg_msw, cfg_lsw;
9380         int i;
9381         int write_eep = 0;
9382
9383         iop_base = asc_dvc->iop_base;
9384         warn_code = 0;
9385         AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0x00FE);
9386         AscStopQueueExe(iop_base);
9387         if ((AscStopChip(iop_base)) ||
9388             (AscGetChipScsiCtrl(iop_base) != 0)) {
9389                 asc_dvc->init_state |= ASC_INIT_RESET_SCSI_DONE;
9390                 AscResetChipAndScsiBus(asc_dvc);
9391                 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
9392         }
9393         if (!AscIsChipHalted(iop_base)) {
9394                 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
9395                 return (warn_code);
9396         }
9397         AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
9398         if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
9399                 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
9400                 return (warn_code);
9401         }
9402         eep_config = (ASCEEP_CONFIG *)&eep_config_buf;
9403         cfg_msw = AscGetChipCfgMsw(iop_base);
9404         cfg_lsw = AscGetChipCfgLsw(iop_base);
9405         if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9406                 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9407                 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9408                 AscSetChipCfgMsw(iop_base, cfg_msw);
9409         }
9410         chksum = AscGetEEPConfig(iop_base, eep_config, asc_dvc->bus_type);
9411         ASC_DBG(1, "chksum 0x%x\n", chksum);
9412         if (chksum == 0) {
9413                 chksum = 0xaa55;
9414         }
9415         if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9416                 warn_code |= ASC_WARN_AUTO_CONFIG;
9417                 if (asc_dvc->cfg->chip_version == 3) {
9418                         if (eep_config->cfg_lsw != cfg_lsw) {
9419                                 warn_code |= ASC_WARN_EEPROM_RECOVER;
9420                                 eep_config->cfg_lsw =
9421                                     AscGetChipCfgLsw(iop_base);
9422                         }
9423                         if (eep_config->cfg_msw != cfg_msw) {
9424                                 warn_code |= ASC_WARN_EEPROM_RECOVER;
9425                                 eep_config->cfg_msw =
9426                                     AscGetChipCfgMsw(iop_base);
9427                         }
9428                 }
9429         }
9430         eep_config->cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9431         eep_config->cfg_lsw |= ASC_CFG0_HOST_INT_ON;
9432         ASC_DBG(1, "eep_config->chksum 0x%x\n", eep_config->chksum);
9433         if (chksum != eep_config->chksum) {
9434                 if (AscGetChipVersion(iop_base, asc_dvc->bus_type) ==
9435                     ASC_CHIP_VER_PCI_ULTRA_3050) {
9436                         ASC_DBG(1, "chksum error ignored; EEPROM-less board\n");
9437                         eep_config->init_sdtr = 0xFF;
9438                         eep_config->disc_enable = 0xFF;
9439                         eep_config->start_motor = 0xFF;
9440                         eep_config->use_cmd_qng = 0;
9441                         eep_config->max_total_qng = 0xF0;
9442                         eep_config->max_tag_qng = 0x20;
9443                         eep_config->cntl = 0xBFFF;
9444                         ASC_EEP_SET_CHIP_ID(eep_config, 7);
9445                         eep_config->no_scam = 0;
9446                         eep_config->adapter_info[0] = 0;
9447                         eep_config->adapter_info[1] = 0;
9448                         eep_config->adapter_info[2] = 0;
9449                         eep_config->adapter_info[3] = 0;
9450                         eep_config->adapter_info[4] = 0;
9451                         /* Indicate EEPROM-less board. */
9452                         eep_config->adapter_info[5] = 0xBB;
9453                 } else {
9454                         ASC_PRINT
9455                             ("AscInitFromEEP: EEPROM checksum error; Will try to re-write EEPROM.\n");
9456                         write_eep = 1;
9457                         warn_code |= ASC_WARN_EEPROM_CHKSUM;
9458                 }
9459         }
9460         asc_dvc->cfg->sdtr_enable = eep_config->init_sdtr;
9461         asc_dvc->cfg->disc_enable = eep_config->disc_enable;
9462         asc_dvc->cfg->cmd_qng_enabled = eep_config->use_cmd_qng;
9463         asc_dvc->cfg->isa_dma_speed = ASC_EEP_GET_DMA_SPD(eep_config);
9464         asc_dvc->start_motor = eep_config->start_motor;
9465         asc_dvc->dvc_cntl = eep_config->cntl;
9466         asc_dvc->no_scam = eep_config->no_scam;
9467         asc_dvc->cfg->adapter_info[0] = eep_config->adapter_info[0];
9468         asc_dvc->cfg->adapter_info[1] = eep_config->adapter_info[1];
9469         asc_dvc->cfg->adapter_info[2] = eep_config->adapter_info[2];
9470         asc_dvc->cfg->adapter_info[3] = eep_config->adapter_info[3];
9471         asc_dvc->cfg->adapter_info[4] = eep_config->adapter_info[4];
9472         asc_dvc->cfg->adapter_info[5] = eep_config->adapter_info[5];
9473         if (!AscTestExternalLram(asc_dvc)) {
9474                 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) ==
9475                      ASC_IS_PCI_ULTRA)) {
9476                         eep_config->max_total_qng =
9477                             ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
9478                         eep_config->max_tag_qng =
9479                             ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG;
9480                 } else {
9481                         eep_config->cfg_msw |= 0x0800;
9482                         cfg_msw |= 0x0800;
9483                         AscSetChipCfgMsw(iop_base, cfg_msw);
9484                         eep_config->max_total_qng = ASC_MAX_PCI_INRAM_TOTAL_QNG;
9485                         eep_config->max_tag_qng = ASC_MAX_INRAM_TAG_QNG;
9486                 }
9487         } else {
9488         }
9489         if (eep_config->max_total_qng < ASC_MIN_TOTAL_QNG) {
9490                 eep_config->max_total_qng = ASC_MIN_TOTAL_QNG;
9491         }
9492         if (eep_config->max_total_qng > ASC_MAX_TOTAL_QNG) {
9493                 eep_config->max_total_qng = ASC_MAX_TOTAL_QNG;
9494         }
9495         if (eep_config->max_tag_qng > eep_config->max_total_qng) {
9496                 eep_config->max_tag_qng = eep_config->max_total_qng;
9497         }
9498         if (eep_config->max_tag_qng < ASC_MIN_TAG_Q_PER_DVC) {
9499                 eep_config->max_tag_qng = ASC_MIN_TAG_Q_PER_DVC;
9500         }
9501         asc_dvc->max_total_qng = eep_config->max_total_qng;
9502         if ((eep_config->use_cmd_qng & eep_config->disc_enable) !=
9503             eep_config->use_cmd_qng) {
9504                 eep_config->disc_enable = eep_config->use_cmd_qng;
9505                 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9506         }
9507         ASC_EEP_SET_CHIP_ID(eep_config,
9508                             ASC_EEP_GET_CHIP_ID(eep_config) & ASC_MAX_TID);
9509         asc_dvc->cfg->chip_scsi_id = ASC_EEP_GET_CHIP_ID(eep_config);
9510         if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) &&
9511             !(asc_dvc->dvc_cntl & ASC_CNTL_SDTR_ENABLE_ULTRA)) {
9512                 asc_dvc->min_sdtr_index = ASC_SDTR_ULTRA_PCI_10MB_INDEX;
9513         }
9514
9515         for (i = 0; i <= ASC_MAX_TID; i++) {
9516                 asc_dvc->dos_int13_table[i] = eep_config->dos_int13_table[i];
9517                 asc_dvc->cfg->max_tag_qng[i] = eep_config->max_tag_qng;
9518                 asc_dvc->cfg->sdtr_period_offset[i] =
9519                     (uchar)(ASC_DEF_SDTR_OFFSET |
9520                             (asc_dvc->min_sdtr_index << 4));
9521         }
9522         eep_config->cfg_msw = AscGetChipCfgMsw(iop_base);
9523         if (write_eep) {
9524                 if ((i = AscSetEEPConfig(iop_base, eep_config,
9525                                      asc_dvc->bus_type)) != 0) {
9526                         ASC_PRINT1
9527                             ("AscInitFromEEP: Failed to re-write EEPROM with %d errors.\n",
9528                              i);
9529                 } else {
9530                         ASC_PRINT
9531                             ("AscInitFromEEP: Successfully re-wrote EEPROM.\n");
9532                 }
9533         }
9534         return (warn_code);
9535 }
9536
9537 static int AscInitGetConfig(struct Scsi_Host *shost)
9538 {
9539         struct asc_board *board = shost_priv(shost);
9540         ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
9541         unsigned short warn_code = 0;
9542
9543         asc_dvc->init_state = ASC_INIT_STATE_BEG_GET_CFG;
9544         if (asc_dvc->err_code != 0)
9545                 return asc_dvc->err_code;
9546
9547         if (AscFindSignature(asc_dvc->iop_base)) {
9548                 AscInitAscDvcVar(asc_dvc);
9549                 warn_code = AscInitFromEEP(asc_dvc);
9550                 asc_dvc->init_state |= ASC_INIT_STATE_END_GET_CFG;
9551                 if (asc_dvc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
9552                         asc_dvc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
9553         } else {
9554                 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9555         }
9556
9557         switch (warn_code) {
9558         case 0: /* No error */
9559                 break;
9560         case ASC_WARN_IO_PORT_ROTATE:
9561                 shost_printk(KERN_WARNING, shost, "I/O port address "
9562                                 "modified\n");
9563                 break;
9564         case ASC_WARN_AUTO_CONFIG:
9565                 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9566                                 "enabled\n");
9567                 break;
9568         case ASC_WARN_EEPROM_CHKSUM:
9569                 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
9570                 break;
9571         case ASC_WARN_IRQ_MODIFIED:
9572                 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
9573                 break;
9574         case ASC_WARN_CMD_QNG_CONFLICT:
9575                 shost_printk(KERN_WARNING, shost, "tag queuing enabled w/o "
9576                                 "disconnects\n");
9577                 break;
9578         default:
9579                 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9580                                 warn_code);
9581                 break;
9582         }
9583
9584         if (asc_dvc->err_code != 0)
9585                 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9586                         "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
9587
9588         return asc_dvc->err_code;
9589 }
9590
9591 static int AscInitSetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
9592 {
9593         struct asc_board *board = shost_priv(shost);
9594         ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
9595         PortAddr iop_base = asc_dvc->iop_base;
9596         unsigned short cfg_msw;
9597         unsigned short warn_code = 0;
9598
9599         asc_dvc->init_state |= ASC_INIT_STATE_BEG_SET_CFG;
9600         if (asc_dvc->err_code != 0)
9601                 return asc_dvc->err_code;
9602         if (!AscFindSignature(asc_dvc->iop_base)) {
9603                 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9604                 return asc_dvc->err_code;
9605         }
9606
9607         cfg_msw = AscGetChipCfgMsw(iop_base);
9608         if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9609                 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9610                 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9611                 AscSetChipCfgMsw(iop_base, cfg_msw);
9612         }
9613         if ((asc_dvc->cfg->cmd_qng_enabled & asc_dvc->cfg->disc_enable) !=
9614             asc_dvc->cfg->cmd_qng_enabled) {
9615                 asc_dvc->cfg->disc_enable = asc_dvc->cfg->cmd_qng_enabled;
9616                 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9617         }
9618         if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9619                 warn_code |= ASC_WARN_AUTO_CONFIG;
9620         }
9621 #ifdef CONFIG_PCI
9622         if (asc_dvc->bus_type & ASC_IS_PCI) {
9623                 cfg_msw &= 0xFFC0;
9624                 AscSetChipCfgMsw(iop_base, cfg_msw);
9625                 if ((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) {
9626                 } else {
9627                         if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
9628                             (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
9629                                 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_IF_NOT_DWB;
9630                                 asc_dvc->bug_fix_cntl |=
9631                                     ASC_BUG_FIX_ASYN_USE_SYN;
9632                         }
9633                 }
9634         } else
9635 #endif /* CONFIG_PCI */
9636         if (asc_dvc->bus_type == ASC_IS_ISAPNP) {
9637                 if (AscGetChipVersion(iop_base, asc_dvc->bus_type)
9638                     == ASC_CHIP_VER_ASYN_BUG) {
9639                         asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
9640                 }
9641         }
9642         if (AscSetChipScsiID(iop_base, asc_dvc->cfg->chip_scsi_id) !=
9643             asc_dvc->cfg->chip_scsi_id) {
9644                 asc_dvc->err_code |= ASC_IERR_SET_SCSI_ID;
9645         }
9646 #ifdef CONFIG_ISA
9647         if (asc_dvc->bus_type & ASC_IS_ISA) {
9648                 AscSetIsaDmaChannel(iop_base, asc_dvc->cfg->isa_dma_channel);
9649                 AscSetIsaDmaSpeed(iop_base, asc_dvc->cfg->isa_dma_speed);
9650         }
9651 #endif /* CONFIG_ISA */
9652
9653         asc_dvc->init_state |= ASC_INIT_STATE_END_SET_CFG;
9654
9655         switch (warn_code) {
9656         case 0: /* No error. */
9657                 break;
9658         case ASC_WARN_IO_PORT_ROTATE:
9659                 shost_printk(KERN_WARNING, shost, "I/O port address "
9660                                 "modified\n");
9661                 break;
9662         case ASC_WARN_AUTO_CONFIG:
9663                 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9664                                 "enabled\n");
9665                 break;
9666         case ASC_WARN_EEPROM_CHKSUM:
9667                 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
9668                 break;
9669         case ASC_WARN_IRQ_MODIFIED:
9670                 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
9671                 break;
9672         case ASC_WARN_CMD_QNG_CONFLICT:
9673                 shost_printk(KERN_WARNING, shost, "tag queuing w/o "
9674                                 "disconnects\n");
9675                 break;
9676         default:
9677                 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9678                                 warn_code);
9679                 break;
9680         }
9681
9682         if (asc_dvc->err_code != 0)
9683                 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9684                         "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
9685
9686         return asc_dvc->err_code;
9687 }
9688
9689 /*
9690  * EEPROM Configuration.
9691  *
9692  * All drivers should use this structure to set the default EEPROM
9693  * configuration. The BIOS now uses this structure when it is built.
9694  * Additional structure information can be found in a_condor.h where
9695  * the structure is defined.
9696  *
9697  * The *_Field_IsChar structs are needed to correct for endianness.
9698  * These values are read from the board 16 bits at a time directly
9699  * into the structs. Because some fields are char, the values will be
9700  * in the wrong order. The *_Field_IsChar tells when to flip the
9701  * bytes. Data read and written to PCI memory is automatically swapped
9702  * on big-endian platforms so char fields read as words are actually being
9703  * unswapped on big-endian platforms.
9704  */
9705 static ADVEEP_3550_CONFIG Default_3550_EEPROM_Config = {
9706         ADV_EEPROM_BIOS_ENABLE, /* cfg_lsw */
9707         0x0000,                 /* cfg_msw */
9708         0xFFFF,                 /* disc_enable */
9709         0xFFFF,                 /* wdtr_able */
9710         0xFFFF,                 /* sdtr_able */
9711         0xFFFF,                 /* start_motor */
9712         0xFFFF,                 /* tagqng_able */
9713         0xFFFF,                 /* bios_scan */
9714         0,                      /* scam_tolerant */
9715         7,                      /* adapter_scsi_id */
9716         0,                      /* bios_boot_delay */
9717         3,                      /* scsi_reset_delay */
9718         0,                      /* bios_id_lun */
9719         0,                      /* termination */
9720         0,                      /* reserved1 */
9721         0xFFE7,                 /* bios_ctrl */
9722         0xFFFF,                 /* ultra_able */
9723         0,                      /* reserved2 */
9724         ASC_DEF_MAX_HOST_QNG,   /* max_host_qng */
9725         ASC_DEF_MAX_DVC_QNG,    /* max_dvc_qng */
9726         0,                      /* dvc_cntl */
9727         0,                      /* bug_fix */
9728         0,                      /* serial_number_word1 */
9729         0,                      /* serial_number_word2 */
9730         0,                      /* serial_number_word3 */
9731         0,                      /* check_sum */
9732         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9733         ,                       /* oem_name[16] */
9734         0,                      /* dvc_err_code */
9735         0,                      /* adv_err_code */
9736         0,                      /* adv_err_addr */
9737         0,                      /* saved_dvc_err_code */
9738         0,                      /* saved_adv_err_code */
9739         0,                      /* saved_adv_err_addr */
9740         0                       /* num_of_err */
9741 };
9742
9743 static ADVEEP_3550_CONFIG ADVEEP_3550_Config_Field_IsChar = {
9744         0,                      /* cfg_lsw */
9745         0,                      /* cfg_msw */
9746         0,                      /* -disc_enable */
9747         0,                      /* wdtr_able */
9748         0,                      /* sdtr_able */
9749         0,                      /* start_motor */
9750         0,                      /* tagqng_able */
9751         0,                      /* bios_scan */
9752         0,                      /* scam_tolerant */
9753         1,                      /* adapter_scsi_id */
9754         1,                      /* bios_boot_delay */
9755         1,                      /* scsi_reset_delay */
9756         1,                      /* bios_id_lun */
9757         1,                      /* termination */
9758         1,                      /* reserved1 */
9759         0,                      /* bios_ctrl */
9760         0,                      /* ultra_able */
9761         0,                      /* reserved2 */
9762         1,                      /* max_host_qng */
9763         1,                      /* max_dvc_qng */
9764         0,                      /* dvc_cntl */
9765         0,                      /* bug_fix */
9766         0,                      /* serial_number_word1 */
9767         0,                      /* serial_number_word2 */
9768         0,                      /* serial_number_word3 */
9769         0,                      /* check_sum */
9770         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9771         ,                       /* oem_name[16] */
9772         0,                      /* dvc_err_code */
9773         0,                      /* adv_err_code */
9774         0,                      /* adv_err_addr */
9775         0,                      /* saved_dvc_err_code */
9776         0,                      /* saved_adv_err_code */
9777         0,                      /* saved_adv_err_addr */
9778         0                       /* num_of_err */
9779 };
9780
9781 static ADVEEP_38C0800_CONFIG Default_38C0800_EEPROM_Config = {
9782         ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9783         0x0000,                 /* 01 cfg_msw */
9784         0xFFFF,                 /* 02 disc_enable */
9785         0xFFFF,                 /* 03 wdtr_able */
9786         0x4444,                 /* 04 sdtr_speed1 */
9787         0xFFFF,                 /* 05 start_motor */
9788         0xFFFF,                 /* 06 tagqng_able */
9789         0xFFFF,                 /* 07 bios_scan */
9790         0,                      /* 08 scam_tolerant */
9791         7,                      /* 09 adapter_scsi_id */
9792         0,                      /*    bios_boot_delay */
9793         3,                      /* 10 scsi_reset_delay */
9794         0,                      /*    bios_id_lun */
9795         0,                      /* 11 termination_se */
9796         0,                      /*    termination_lvd */
9797         0xFFE7,                 /* 12 bios_ctrl */
9798         0x4444,                 /* 13 sdtr_speed2 */
9799         0x4444,                 /* 14 sdtr_speed3 */
9800         ASC_DEF_MAX_HOST_QNG,   /* 15 max_host_qng */
9801         ASC_DEF_MAX_DVC_QNG,    /*    max_dvc_qng */
9802         0,                      /* 16 dvc_cntl */
9803         0x4444,                 /* 17 sdtr_speed4 */
9804         0,                      /* 18 serial_number_word1 */
9805         0,                      /* 19 serial_number_word2 */
9806         0,                      /* 20 serial_number_word3 */
9807         0,                      /* 21 check_sum */
9808         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9809         ,                       /* 22-29 oem_name[16] */
9810         0,                      /* 30 dvc_err_code */
9811         0,                      /* 31 adv_err_code */
9812         0,                      /* 32 adv_err_addr */
9813         0,                      /* 33 saved_dvc_err_code */
9814         0,                      /* 34 saved_adv_err_code */
9815         0,                      /* 35 saved_adv_err_addr */
9816         0,                      /* 36 reserved */
9817         0,                      /* 37 reserved */
9818         0,                      /* 38 reserved */
9819         0,                      /* 39 reserved */
9820         0,                      /* 40 reserved */
9821         0,                      /* 41 reserved */
9822         0,                      /* 42 reserved */
9823         0,                      /* 43 reserved */
9824         0,                      /* 44 reserved */
9825         0,                      /* 45 reserved */
9826         0,                      /* 46 reserved */
9827         0,                      /* 47 reserved */
9828         0,                      /* 48 reserved */
9829         0,                      /* 49 reserved */
9830         0,                      /* 50 reserved */
9831         0,                      /* 51 reserved */
9832         0,                      /* 52 reserved */
9833         0,                      /* 53 reserved */
9834         0,                      /* 54 reserved */
9835         0,                      /* 55 reserved */
9836         0,                      /* 56 cisptr_lsw */
9837         0,                      /* 57 cisprt_msw */
9838         PCI_VENDOR_ID_ASP,      /* 58 subsysvid */
9839         PCI_DEVICE_ID_38C0800_REV1,     /* 59 subsysid */
9840         0,                      /* 60 reserved */
9841         0,                      /* 61 reserved */
9842         0,                      /* 62 reserved */
9843         0                       /* 63 reserved */
9844 };
9845
9846 static ADVEEP_38C0800_CONFIG ADVEEP_38C0800_Config_Field_IsChar = {
9847         0,                      /* 00 cfg_lsw */
9848         0,                      /* 01 cfg_msw */
9849         0,                      /* 02 disc_enable */
9850         0,                      /* 03 wdtr_able */
9851         0,                      /* 04 sdtr_speed1 */
9852         0,                      /* 05 start_motor */
9853         0,                      /* 06 tagqng_able */
9854         0,                      /* 07 bios_scan */
9855         0,                      /* 08 scam_tolerant */
9856         1,                      /* 09 adapter_scsi_id */
9857         1,                      /*    bios_boot_delay */
9858         1,                      /* 10 scsi_reset_delay */
9859         1,                      /*    bios_id_lun */
9860         1,                      /* 11 termination_se */
9861         1,                      /*    termination_lvd */
9862         0,                      /* 12 bios_ctrl */
9863         0,                      /* 13 sdtr_speed2 */
9864         0,                      /* 14 sdtr_speed3 */
9865         1,                      /* 15 max_host_qng */
9866         1,                      /*    max_dvc_qng */
9867         0,                      /* 16 dvc_cntl */
9868         0,                      /* 17 sdtr_speed4 */
9869         0,                      /* 18 serial_number_word1 */
9870         0,                      /* 19 serial_number_word2 */
9871         0,                      /* 20 serial_number_word3 */
9872         0,                      /* 21 check_sum */
9873         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9874         ,                       /* 22-29 oem_name[16] */
9875         0,                      /* 30 dvc_err_code */
9876         0,                      /* 31 adv_err_code */
9877         0,                      /* 32 adv_err_addr */
9878         0,                      /* 33 saved_dvc_err_code */
9879         0,                      /* 34 saved_adv_err_code */
9880         0,                      /* 35 saved_adv_err_addr */
9881         0,                      /* 36 reserved */
9882         0,                      /* 37 reserved */
9883         0,                      /* 38 reserved */
9884         0,                      /* 39 reserved */
9885         0,                      /* 40 reserved */
9886         0,                      /* 41 reserved */
9887         0,                      /* 42 reserved */
9888         0,                      /* 43 reserved */
9889         0,                      /* 44 reserved */
9890         0,                      /* 45 reserved */
9891         0,                      /* 46 reserved */
9892         0,                      /* 47 reserved */
9893         0,                      /* 48 reserved */
9894         0,                      /* 49 reserved */
9895         0,                      /* 50 reserved */
9896         0,                      /* 51 reserved */
9897         0,                      /* 52 reserved */
9898         0,                      /* 53 reserved */
9899         0,                      /* 54 reserved */
9900         0,                      /* 55 reserved */
9901         0,                      /* 56 cisptr_lsw */
9902         0,                      /* 57 cisprt_msw */
9903         0,                      /* 58 subsysvid */
9904         0,                      /* 59 subsysid */
9905         0,                      /* 60 reserved */
9906         0,                      /* 61 reserved */
9907         0,                      /* 62 reserved */
9908         0                       /* 63 reserved */
9909 };
9910
9911 static ADVEEP_38C1600_CONFIG Default_38C1600_EEPROM_Config = {
9912         ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9913         0x0000,                 /* 01 cfg_msw */
9914         0xFFFF,                 /* 02 disc_enable */
9915         0xFFFF,                 /* 03 wdtr_able */
9916         0x5555,                 /* 04 sdtr_speed1 */
9917         0xFFFF,                 /* 05 start_motor */
9918         0xFFFF,                 /* 06 tagqng_able */
9919         0xFFFF,                 /* 07 bios_scan */
9920         0,                      /* 08 scam_tolerant */
9921         7,                      /* 09 adapter_scsi_id */
9922         0,                      /*    bios_boot_delay */
9923         3,                      /* 10 scsi_reset_delay */
9924         0,                      /*    bios_id_lun */
9925         0,                      /* 11 termination_se */
9926         0,                      /*    termination_lvd */
9927         0xFFE7,                 /* 12 bios_ctrl */
9928         0x5555,                 /* 13 sdtr_speed2 */
9929         0x5555,                 /* 14 sdtr_speed3 */
9930         ASC_DEF_MAX_HOST_QNG,   /* 15 max_host_qng */
9931         ASC_DEF_MAX_DVC_QNG,    /*    max_dvc_qng */
9932         0,                      /* 16 dvc_cntl */
9933         0x5555,                 /* 17 sdtr_speed4 */
9934         0,                      /* 18 serial_number_word1 */
9935         0,                      /* 19 serial_number_word2 */
9936         0,                      /* 20 serial_number_word3 */
9937         0,                      /* 21 check_sum */
9938         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9939         ,                       /* 22-29 oem_name[16] */
9940         0,                      /* 30 dvc_err_code */
9941         0,                      /* 31 adv_err_code */
9942         0,                      /* 32 adv_err_addr */
9943         0,                      /* 33 saved_dvc_err_code */
9944         0,                      /* 34 saved_adv_err_code */
9945         0,                      /* 35 saved_adv_err_addr */
9946         0,                      /* 36 reserved */
9947         0,                      /* 37 reserved */
9948         0,                      /* 38 reserved */
9949         0,                      /* 39 reserved */
9950         0,                      /* 40 reserved */
9951         0,                      /* 41 reserved */
9952         0,                      /* 42 reserved */
9953         0,                      /* 43 reserved */
9954         0,                      /* 44 reserved */
9955         0,                      /* 45 reserved */
9956         0,                      /* 46 reserved */
9957         0,                      /* 47 reserved */
9958         0,                      /* 48 reserved */
9959         0,                      /* 49 reserved */
9960         0,                      /* 50 reserved */
9961         0,                      /* 51 reserved */
9962         0,                      /* 52 reserved */
9963         0,                      /* 53 reserved */
9964         0,                      /* 54 reserved */
9965         0,                      /* 55 reserved */
9966         0,                      /* 56 cisptr_lsw */
9967         0,                      /* 57 cisprt_msw */
9968         PCI_VENDOR_ID_ASP,      /* 58 subsysvid */
9969         PCI_DEVICE_ID_38C1600_REV1,     /* 59 subsysid */
9970         0,                      /* 60 reserved */
9971         0,                      /* 61 reserved */
9972         0,                      /* 62 reserved */
9973         0                       /* 63 reserved */
9974 };
9975
9976 static ADVEEP_38C1600_CONFIG ADVEEP_38C1600_Config_Field_IsChar = {
9977         0,                      /* 00 cfg_lsw */
9978         0,                      /* 01 cfg_msw */
9979         0,                      /* 02 disc_enable */
9980         0,                      /* 03 wdtr_able */
9981         0,                      /* 04 sdtr_speed1 */
9982         0,                      /* 05 start_motor */
9983         0,                      /* 06 tagqng_able */
9984         0,                      /* 07 bios_scan */
9985         0,                      /* 08 scam_tolerant */
9986         1,                      /* 09 adapter_scsi_id */
9987         1,                      /*    bios_boot_delay */
9988         1,                      /* 10 scsi_reset_delay */
9989         1,                      /*    bios_id_lun */
9990         1,                      /* 11 termination_se */
9991         1,                      /*    termination_lvd */
9992         0,                      /* 12 bios_ctrl */
9993         0,                      /* 13 sdtr_speed2 */
9994         0,                      /* 14 sdtr_speed3 */
9995         1,                      /* 15 max_host_qng */
9996         1,                      /*    max_dvc_qng */
9997         0,                      /* 16 dvc_cntl */
9998         0,                      /* 17 sdtr_speed4 */
9999         0,                      /* 18 serial_number_word1 */
10000         0,                      /* 19 serial_number_word2 */
10001         0,                      /* 20 serial_number_word3 */
10002         0,                      /* 21 check_sum */
10003         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
10004         ,                       /* 22-29 oem_name[16] */
10005         0,                      /* 30 dvc_err_code */
10006         0,                      /* 31 adv_err_code */
10007         0,                      /* 32 adv_err_addr */
10008         0,                      /* 33 saved_dvc_err_code */
10009         0,                      /* 34 saved_adv_err_code */
10010         0,                      /* 35 saved_adv_err_addr */
10011         0,                      /* 36 reserved */
10012         0,                      /* 37 reserved */
10013         0,                      /* 38 reserved */
10014         0,                      /* 39 reserved */
10015         0,                      /* 40 reserved */
10016         0,                      /* 41 reserved */
10017         0,                      /* 42 reserved */
10018         0,                      /* 43 reserved */
10019         0,                      /* 44 reserved */
10020         0,                      /* 45 reserved */
10021         0,                      /* 46 reserved */
10022         0,                      /* 47 reserved */
10023         0,                      /* 48 reserved */
10024         0,                      /* 49 reserved */
10025         0,                      /* 50 reserved */
10026         0,                      /* 51 reserved */
10027         0,                      /* 52 reserved */
10028         0,                      /* 53 reserved */
10029         0,                      /* 54 reserved */
10030         0,                      /* 55 reserved */
10031         0,                      /* 56 cisptr_lsw */
10032         0,                      /* 57 cisprt_msw */
10033         0,                      /* 58 subsysvid */
10034         0,                      /* 59 subsysid */
10035         0,                      /* 60 reserved */
10036         0,                      /* 61 reserved */
10037         0,                      /* 62 reserved */
10038         0                       /* 63 reserved */
10039 };
10040
10041 #ifdef CONFIG_PCI
10042 /*
10043  * Wait for EEPROM command to complete
10044  */
10045 static void AdvWaitEEPCmd(AdvPortAddr iop_base)
10046 {
10047         int eep_delay_ms;
10048
10049         for (eep_delay_ms = 0; eep_delay_ms < ADV_EEP_DELAY_MS; eep_delay_ms++) {
10050                 if (AdvReadWordRegister(iop_base, IOPW_EE_CMD) &
10051                     ASC_EEP_CMD_DONE) {
10052                         break;
10053                 }
10054                 mdelay(1);
10055         }
10056         if ((AdvReadWordRegister(iop_base, IOPW_EE_CMD) & ASC_EEP_CMD_DONE) ==
10057             0)
10058                 BUG();
10059 }
10060
10061 /*
10062  * Read the EEPROM from specified location
10063  */
10064 static ushort AdvReadEEPWord(AdvPortAddr iop_base, int eep_word_addr)
10065 {
10066         AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10067                              ASC_EEP_CMD_READ | eep_word_addr);
10068         AdvWaitEEPCmd(iop_base);
10069         return AdvReadWordRegister(iop_base, IOPW_EE_DATA);
10070 }
10071
10072 /*
10073  * Write the EEPROM from 'cfg_buf'.
10074  */
10075 static void AdvSet3550EEPConfig(AdvPortAddr iop_base,
10076                                 ADVEEP_3550_CONFIG *cfg_buf)
10077 {
10078         ushort *wbuf;
10079         ushort addr, chksum;
10080         ushort *charfields;
10081
10082         wbuf = (ushort *)cfg_buf;
10083         charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
10084         chksum = 0;
10085
10086         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10087         AdvWaitEEPCmd(iop_base);
10088
10089         /*
10090          * Write EEPROM from word 0 to word 20.
10091          */
10092         for (addr = ADV_EEP_DVC_CFG_BEGIN;
10093              addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10094                 ushort word;
10095
10096                 if (*charfields++) {
10097                         word = cpu_to_le16(*wbuf);
10098                 } else {
10099                         word = *wbuf;
10100                 }
10101                 chksum += *wbuf;        /* Checksum is calculated from word values. */
10102                 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10103                 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10104                                      ASC_EEP_CMD_WRITE | addr);
10105                 AdvWaitEEPCmd(iop_base);
10106                 mdelay(ADV_EEP_DELAY_MS);
10107         }
10108
10109         /*
10110          * Write EEPROM checksum at word 21.
10111          */
10112         AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10113         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10114         AdvWaitEEPCmd(iop_base);
10115         wbuf++;
10116         charfields++;
10117
10118         /*
10119          * Write EEPROM OEM name at words 22 to 29.
10120          */
10121         for (addr = ADV_EEP_DVC_CTL_BEGIN;
10122              addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10123                 ushort word;
10124
10125                 if (*charfields++) {
10126                         word = cpu_to_le16(*wbuf);
10127                 } else {
10128                         word = *wbuf;
10129                 }
10130                 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10131                 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10132                                      ASC_EEP_CMD_WRITE | addr);
10133                 AdvWaitEEPCmd(iop_base);
10134         }
10135         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10136         AdvWaitEEPCmd(iop_base);
10137 }
10138
10139 /*
10140  * Write the EEPROM from 'cfg_buf'.
10141  */
10142 static void AdvSet38C0800EEPConfig(AdvPortAddr iop_base,
10143                                    ADVEEP_38C0800_CONFIG *cfg_buf)
10144 {
10145         ushort *wbuf;
10146         ushort *charfields;
10147         ushort addr, chksum;
10148
10149         wbuf = (ushort *)cfg_buf;
10150         charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10151         chksum = 0;
10152
10153         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10154         AdvWaitEEPCmd(iop_base);
10155
10156         /*
10157          * Write EEPROM from word 0 to word 20.
10158          */
10159         for (addr = ADV_EEP_DVC_CFG_BEGIN;
10160              addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10161                 ushort word;
10162
10163                 if (*charfields++) {
10164                         word = cpu_to_le16(*wbuf);
10165                 } else {
10166                         word = *wbuf;
10167                 }
10168                 chksum += *wbuf;        /* Checksum is calculated from word values. */
10169                 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10170                 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10171                                      ASC_EEP_CMD_WRITE | addr);
10172                 AdvWaitEEPCmd(iop_base);
10173                 mdelay(ADV_EEP_DELAY_MS);
10174         }
10175
10176         /*
10177          * Write EEPROM checksum at word 21.
10178          */
10179         AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10180         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10181         AdvWaitEEPCmd(iop_base);
10182         wbuf++;
10183         charfields++;
10184
10185         /*
10186          * Write EEPROM OEM name at words 22 to 29.
10187          */
10188         for (addr = ADV_EEP_DVC_CTL_BEGIN;
10189              addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10190                 ushort word;
10191
10192                 if (*charfields++) {
10193                         word = cpu_to_le16(*wbuf);
10194                 } else {
10195                         word = *wbuf;
10196                 }
10197                 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10198                 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10199                                      ASC_EEP_CMD_WRITE | addr);
10200                 AdvWaitEEPCmd(iop_base);
10201         }
10202         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10203         AdvWaitEEPCmd(iop_base);
10204 }
10205
10206 /*
10207  * Write the EEPROM from 'cfg_buf'.
10208  */
10209 static void AdvSet38C1600EEPConfig(AdvPortAddr iop_base,
10210                                    ADVEEP_38C1600_CONFIG *cfg_buf)
10211 {
10212         ushort *wbuf;
10213         ushort *charfields;
10214         ushort addr, chksum;
10215
10216         wbuf = (ushort *)cfg_buf;
10217         charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
10218         chksum = 0;
10219
10220         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10221         AdvWaitEEPCmd(iop_base);
10222
10223         /*
10224          * Write EEPROM from word 0 to word 20.
10225          */
10226         for (addr = ADV_EEP_DVC_CFG_BEGIN;
10227              addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10228                 ushort word;
10229
10230                 if (*charfields++) {
10231                         word = cpu_to_le16(*wbuf);
10232                 } else {
10233                         word = *wbuf;
10234                 }
10235                 chksum += *wbuf;        /* Checksum is calculated from word values. */
10236                 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10237                 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10238                                      ASC_EEP_CMD_WRITE | addr);
10239                 AdvWaitEEPCmd(iop_base);
10240                 mdelay(ADV_EEP_DELAY_MS);
10241         }
10242
10243         /*
10244          * Write EEPROM checksum at word 21.
10245          */
10246         AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10247         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10248         AdvWaitEEPCmd(iop_base);
10249         wbuf++;
10250         charfields++;
10251
10252         /*
10253          * Write EEPROM OEM name at words 22 to 29.
10254          */
10255         for (addr = ADV_EEP_DVC_CTL_BEGIN;
10256              addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10257                 ushort word;
10258
10259                 if (*charfields++) {
10260                         word = cpu_to_le16(*wbuf);
10261                 } else {
10262                         word = *wbuf;
10263                 }
10264                 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10265                 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10266                                      ASC_EEP_CMD_WRITE | addr);
10267                 AdvWaitEEPCmd(iop_base);
10268         }
10269         AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10270         AdvWaitEEPCmd(iop_base);
10271 }
10272
10273 /*
10274  * Read EEPROM configuration into the specified buffer.
10275  *
10276  * Return a checksum based on the EEPROM configuration read.
10277  */
10278 static ushort AdvGet3550EEPConfig(AdvPortAddr iop_base,
10279                                   ADVEEP_3550_CONFIG *cfg_buf)
10280 {
10281         ushort wval, chksum;
10282         ushort *wbuf;
10283         int eep_addr;
10284         ushort *charfields;
10285
10286         charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
10287         wbuf = (ushort *)cfg_buf;
10288         chksum = 0;
10289
10290         for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10291              eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10292                 wval = AdvReadEEPWord(iop_base, eep_addr);
10293                 chksum += wval; /* Checksum is calculated from word values. */
10294                 if (*charfields++) {
10295                         *wbuf = le16_to_cpu(wval);
10296                 } else {
10297                         *wbuf = wval;
10298                 }
10299         }
10300         /* Read checksum word. */
10301         *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10302         wbuf++;
10303         charfields++;
10304
10305         /* Read rest of EEPROM not covered by the checksum. */
10306         for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10307              eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10308                 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10309                 if (*charfields++) {
10310                         *wbuf = le16_to_cpu(*wbuf);
10311                 }
10312         }
10313         return chksum;
10314 }
10315
10316 /*
10317  * Read EEPROM configuration into the specified buffer.
10318  *
10319  * Return a checksum based on the EEPROM configuration read.
10320  */
10321 static ushort AdvGet38C0800EEPConfig(AdvPortAddr iop_base,
10322                                      ADVEEP_38C0800_CONFIG *cfg_buf)
10323 {
10324         ushort wval, chksum;
10325         ushort *wbuf;
10326         int eep_addr;
10327         ushort *charfields;
10328
10329         charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10330         wbuf = (ushort *)cfg_buf;
10331         chksum = 0;
10332
10333         for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10334              eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10335                 wval = AdvReadEEPWord(iop_base, eep_addr);
10336                 chksum += wval; /* Checksum is calculated from word values. */
10337                 if (*charfields++) {
10338                         *wbuf = le16_to_cpu(wval);
10339                 } else {
10340                         *wbuf = wval;
10341                 }
10342         }
10343         /* Read checksum word. */
10344         *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10345         wbuf++;
10346         charfields++;
10347
10348         /* Read rest of EEPROM not covered by the checksum. */
10349         for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10350              eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10351                 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10352                 if (*charfields++) {
10353                         *wbuf = le16_to_cpu(*wbuf);
10354                 }
10355         }
10356         return chksum;
10357 }
10358
10359 /*
10360  * Read EEPROM configuration into the specified buffer.
10361  *
10362  * Return a checksum based on the EEPROM configuration read.
10363  */
10364 static ushort AdvGet38C1600EEPConfig(AdvPortAddr iop_base,
10365                                      ADVEEP_38C1600_CONFIG *cfg_buf)
10366 {
10367         ushort wval, chksum;
10368         ushort *wbuf;
10369         int eep_addr;
10370         ushort *charfields;
10371
10372         charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
10373         wbuf = (ushort *)cfg_buf;
10374         chksum = 0;
10375
10376         for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10377              eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10378                 wval = AdvReadEEPWord(iop_base, eep_addr);
10379                 chksum += wval; /* Checksum is calculated from word values. */
10380                 if (*charfields++) {
10381                         *wbuf = le16_to_cpu(wval);
10382                 } else {
10383                         *wbuf = wval;
10384                 }
10385         }
10386         /* Read checksum word. */
10387         *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10388         wbuf++;
10389         charfields++;
10390
10391         /* Read rest of EEPROM not covered by the checksum. */
10392         for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10393              eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10394                 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10395                 if (*charfields++) {
10396                         *wbuf = le16_to_cpu(*wbuf);
10397                 }
10398         }
10399         return chksum;
10400 }
10401
10402 /*
10403  * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10404  * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10405  * all of this is done.
10406  *
10407  * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10408  *
10409  * For a non-fatal error return a warning code. If there are no warnings
10410  * then 0 is returned.
10411  *
10412  * Note: Chip is stopped on entry.
10413  */
10414 static int AdvInitFrom3550EEP(ADV_DVC_VAR *asc_dvc)
10415 {
10416         AdvPortAddr iop_base;
10417         ushort warn_code;
10418         ADVEEP_3550_CONFIG eep_config;
10419
10420         iop_base = asc_dvc->iop_base;
10421
10422         warn_code = 0;
10423
10424         /*
10425          * Read the board's EEPROM configuration.
10426          *
10427          * Set default values if a bad checksum is found.
10428          */
10429         if (AdvGet3550EEPConfig(iop_base, &eep_config) != eep_config.check_sum) {
10430                 warn_code |= ASC_WARN_EEPROM_CHKSUM;
10431
10432                 /*
10433                  * Set EEPROM default values.
10434                  */
10435                 memcpy(&eep_config, &Default_3550_EEPROM_Config,
10436                         sizeof(ADVEEP_3550_CONFIG));
10437
10438                 /*
10439                  * Assume the 6 byte board serial number that was read from
10440                  * EEPROM is correct even if the EEPROM checksum failed.
10441                  */
10442                 eep_config.serial_number_word3 =
10443                     AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
10444
10445                 eep_config.serial_number_word2 =
10446                     AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
10447
10448                 eep_config.serial_number_word1 =
10449                     AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
10450
10451                 AdvSet3550EEPConfig(iop_base, &eep_config);
10452         }
10453         /*
10454          * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10455          * EEPROM configuration that was read.
10456          *
10457          * This is the mapping of EEPROM fields to Adv Library fields.
10458          */
10459         asc_dvc->wdtr_able = eep_config.wdtr_able;
10460         asc_dvc->sdtr_able = eep_config.sdtr_able;
10461         asc_dvc->ultra_able = eep_config.ultra_able;
10462         asc_dvc->tagqng_able = eep_config.tagqng_able;
10463         asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10464         asc_dvc->max_host_qng = eep_config.max_host_qng;
10465         asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10466         asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10467         asc_dvc->start_motor = eep_config.start_motor;
10468         asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10469         asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10470         asc_dvc->no_scam = eep_config.scam_tolerant;
10471         asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10472         asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10473         asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
10474
10475         /*
10476          * Set the host maximum queuing (max. 253, min. 16) and the per device
10477          * maximum queuing (max. 63, min. 4).
10478          */
10479         if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10480                 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10481         } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10482                 /* If the value is zero, assume it is uninitialized. */
10483                 if (eep_config.max_host_qng == 0) {
10484                         eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10485                 } else {
10486                         eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10487                 }
10488         }
10489
10490         if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10491                 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10492         } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10493                 /* If the value is zero, assume it is uninitialized. */
10494                 if (eep_config.max_dvc_qng == 0) {
10495                         eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10496                 } else {
10497                         eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10498                 }
10499         }
10500
10501         /*
10502          * If 'max_dvc_qng' is greater than 'max_host_qng', then
10503          * set 'max_dvc_qng' to 'max_host_qng'.
10504          */
10505         if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10506                 eep_config.max_dvc_qng = eep_config.max_host_qng;
10507         }
10508
10509         /*
10510          * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10511          * values based on possibly adjusted EEPROM values.
10512          */
10513         asc_dvc->max_host_qng = eep_config.max_host_qng;
10514         asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10515
10516         /*
10517          * If the EEPROM 'termination' field is set to automatic (0), then set
10518          * the ADV_DVC_CFG 'termination' field to automatic also.
10519          *
10520          * If the termination is specified with a non-zero 'termination'
10521          * value check that a legal value is set and set the ADV_DVC_CFG
10522          * 'termination' field appropriately.
10523          */
10524         if (eep_config.termination == 0) {
10525                 asc_dvc->cfg->termination = 0;  /* auto termination */
10526         } else {
10527                 /* Enable manual control with low off / high off. */
10528                 if (eep_config.termination == 1) {
10529                         asc_dvc->cfg->termination = TERM_CTL_SEL;
10530
10531                         /* Enable manual control with low off / high on. */
10532                 } else if (eep_config.termination == 2) {
10533                         asc_dvc->cfg->termination = TERM_CTL_SEL | TERM_CTL_H;
10534
10535                         /* Enable manual control with low on / high on. */
10536                 } else if (eep_config.termination == 3) {
10537                         asc_dvc->cfg->termination =
10538                             TERM_CTL_SEL | TERM_CTL_H | TERM_CTL_L;
10539                 } else {
10540                         /*
10541                          * The EEPROM 'termination' field contains a bad value. Use
10542                          * automatic termination instead.
10543                          */
10544                         asc_dvc->cfg->termination = 0;
10545                         warn_code |= ASC_WARN_EEPROM_TERMINATION;
10546                 }
10547         }
10548
10549         return warn_code;
10550 }
10551
10552 /*
10553  * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10554  * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10555  * all of this is done.
10556  *
10557  * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10558  *
10559  * For a non-fatal error return a warning code. If there are no warnings
10560  * then 0 is returned.
10561  *
10562  * Note: Chip is stopped on entry.
10563  */
10564 static int AdvInitFrom38C0800EEP(ADV_DVC_VAR *asc_dvc)
10565 {
10566         AdvPortAddr iop_base;
10567         ushort warn_code;
10568         ADVEEP_38C0800_CONFIG eep_config;
10569         uchar tid, termination;
10570         ushort sdtr_speed = 0;
10571
10572         iop_base = asc_dvc->iop_base;
10573
10574         warn_code = 0;
10575
10576         /*
10577          * Read the board's EEPROM configuration.
10578          *
10579          * Set default values if a bad checksum is found.
10580          */
10581         if (AdvGet38C0800EEPConfig(iop_base, &eep_config) !=
10582             eep_config.check_sum) {
10583                 warn_code |= ASC_WARN_EEPROM_CHKSUM;
10584
10585                 /*
10586                  * Set EEPROM default values.
10587                  */
10588                 memcpy(&eep_config, &Default_38C0800_EEPROM_Config,
10589                         sizeof(ADVEEP_38C0800_CONFIG));
10590
10591                 /*
10592                  * Assume the 6 byte board serial number that was read from
10593                  * EEPROM is correct even if the EEPROM checksum failed.
10594                  */
10595                 eep_config.serial_number_word3 =
10596                     AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
10597
10598                 eep_config.serial_number_word2 =
10599                     AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
10600
10601                 eep_config.serial_number_word1 =
10602                     AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
10603
10604                 AdvSet38C0800EEPConfig(iop_base, &eep_config);
10605         }
10606         /*
10607          * Set ADV_DVC_VAR and ADV_DVC_CFG variables from the
10608          * EEPROM configuration that was read.
10609          *
10610          * This is the mapping of EEPROM fields to Adv Library fields.
10611          */
10612         asc_dvc->wdtr_able = eep_config.wdtr_able;
10613         asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10614         asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10615         asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10616         asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10617         asc_dvc->tagqng_able = eep_config.tagqng_able;
10618         asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10619         asc_dvc->max_host_qng = eep_config.max_host_qng;
10620         asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10621         asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10622         asc_dvc->start_motor = eep_config.start_motor;
10623         asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10624         asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10625         asc_dvc->no_scam = eep_config.scam_tolerant;
10626         asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10627         asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10628         asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
10629
10630         /*
10631          * For every Target ID if any of its 'sdtr_speed[1234]' bits
10632          * are set, then set an 'sdtr_able' bit for it.
10633          */
10634         asc_dvc->sdtr_able = 0;
10635         for (tid = 0; tid <= ADV_MAX_TID; tid++) {
10636                 if (tid == 0) {
10637                         sdtr_speed = asc_dvc->sdtr_speed1;
10638                 } else if (tid == 4) {
10639                         sdtr_speed = asc_dvc->sdtr_speed2;
10640                 } else if (tid == 8) {
10641                         sdtr_speed = asc_dvc->sdtr_speed3;
10642                 } else if (tid == 12) {
10643                         sdtr_speed = asc_dvc->sdtr_speed4;
10644                 }
10645                 if (sdtr_speed & ADV_MAX_TID) {
10646                         asc_dvc->sdtr_able |= (1 << tid);
10647                 }
10648                 sdtr_speed >>= 4;
10649         }
10650
10651         /*
10652          * Set the host maximum queuing (max. 253, min. 16) and the per device
10653          * maximum queuing (max. 63, min. 4).
10654          */
10655         if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10656                 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10657         } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10658                 /* If the value is zero, assume it is uninitialized. */
10659                 if (eep_config.max_host_qng == 0) {
10660                         eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10661                 } else {
10662                         eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10663                 }
10664         }
10665
10666         if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10667                 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10668         } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10669                 /* If the value is zero, assume it is uninitialized. */
10670                 if (eep_config.max_dvc_qng == 0) {
10671                         eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10672                 } else {
10673                         eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10674                 }
10675         }
10676
10677         /*
10678          * If 'max_dvc_qng' is greater than 'max_host_qng', then
10679          * set 'max_dvc_qng' to 'max_host_qng'.
10680          */
10681         if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10682                 eep_config.max_dvc_qng = eep_config.max_host_qng;
10683         }
10684
10685         /*
10686          * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10687          * values based on possibly adjusted EEPROM values.
10688          */
10689         asc_dvc->max_host_qng = eep_config.max_host_qng;
10690         asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10691
10692         /*
10693          * If the EEPROM 'termination' field is set to automatic (0), then set
10694          * the ADV_DVC_CFG 'termination' field to automatic also.
10695          *
10696          * If the termination is specified with a non-zero 'termination'
10697          * value check that a legal value is set and set the ADV_DVC_CFG
10698          * 'termination' field appropriately.
10699          */
10700         if (eep_config.termination_se == 0) {
10701                 termination = 0;        /* auto termination for SE */
10702         } else {
10703                 /* Enable manual control with low off / high off. */
10704                 if (eep_config.termination_se == 1) {
10705                         termination = 0;
10706
10707                         /* Enable manual control with low off / high on. */
10708                 } else if (eep_config.termination_se == 2) {
10709                         termination = TERM_SE_HI;
10710
10711                         /* Enable manual control with low on / high on. */
10712                 } else if (eep_config.termination_se == 3) {
10713                         termination = TERM_SE;
10714                 } else {
10715                         /*
10716                          * The EEPROM 'termination_se' field contains a bad value.
10717                          * Use automatic termination instead.
10718                          */
10719                         termination = 0;
10720                         warn_code |= ASC_WARN_EEPROM_TERMINATION;
10721                 }
10722         }
10723
10724         if (eep_config.termination_lvd == 0) {
10725                 asc_dvc->cfg->termination = termination;        /* auto termination for LVD */
10726         } else {
10727                 /* Enable manual control with low off / high off. */
10728                 if (eep_config.termination_lvd == 1) {
10729                         asc_dvc->cfg->termination = termination;
10730
10731                         /* Enable manual control with low off / high on. */
10732                 } else if (eep_config.termination_lvd == 2) {
10733                         asc_dvc->cfg->termination = termination | TERM_LVD_HI;
10734
10735                         /* Enable manual control with low on / high on. */
10736                 } else if (eep_config.termination_lvd == 3) {
10737                         asc_dvc->cfg->termination = termination | TERM_LVD;
10738                 } else {
10739                         /*
10740                          * The EEPROM 'termination_lvd' field contains a bad value.
10741                          * Use automatic termination instead.
10742                          */
10743                         asc_dvc->cfg->termination = termination;
10744                         warn_code |= ASC_WARN_EEPROM_TERMINATION;
10745                 }
10746         }
10747
10748         return warn_code;
10749 }
10750
10751 /*
10752  * Read the board's EEPROM configuration. Set fields in ASC_DVC_VAR and
10753  * ASC_DVC_CFG based on the EEPROM settings. The chip is stopped while
10754  * all of this is done.
10755  *
10756  * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
10757  *
10758  * For a non-fatal error return a warning code. If there are no warnings
10759  * then 0 is returned.
10760  *
10761  * Note: Chip is stopped on entry.
10762  */
10763 static int AdvInitFrom38C1600EEP(ADV_DVC_VAR *asc_dvc)
10764 {
10765         AdvPortAddr iop_base;
10766         ushort warn_code;
10767         ADVEEP_38C1600_CONFIG eep_config;
10768         uchar tid, termination;
10769         ushort sdtr_speed = 0;
10770
10771         iop_base = asc_dvc->iop_base;
10772
10773         warn_code = 0;
10774
10775         /*
10776          * Read the board's EEPROM configuration.
10777          *
10778          * Set default values if a bad checksum is found.
10779          */
10780         if (AdvGet38C1600EEPConfig(iop_base, &eep_config) !=
10781             eep_config.check_sum) {
10782                 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
10783                 warn_code |= ASC_WARN_EEPROM_CHKSUM;
10784
10785                 /*
10786                  * Set EEPROM default values.
10787                  */
10788                 memcpy(&eep_config, &Default_38C1600_EEPROM_Config,
10789                         sizeof(ADVEEP_38C1600_CONFIG));
10790
10791                 if (PCI_FUNC(pdev->devfn) != 0) {
10792                         u8 ints;
10793                         /*
10794                          * Disable Bit 14 (BIOS_ENABLE) to fix SPARC Ultra 60
10795                          * and old Mac system booting problem. The Expansion
10796                          * ROM must be disabled in Function 1 for these systems
10797                          */
10798                         eep_config.cfg_lsw &= ~ADV_EEPROM_BIOS_ENABLE;
10799                         /*
10800                          * Clear the INTAB (bit 11) if the GPIO 0 input
10801                          * indicates the Function 1 interrupt line is wired
10802                          * to INTB.
10803                          *
10804                          * Set/Clear Bit 11 (INTAB) from the GPIO bit 0 input:
10805                          *   1 - Function 1 interrupt line wired to INT A.
10806                          *   0 - Function 1 interrupt line wired to INT B.
10807                          *
10808                          * Note: Function 0 is always wired to INTA.
10809                          * Put all 5 GPIO bits in input mode and then read
10810                          * their input values.
10811                          */
10812                         AdvWriteByteRegister(iop_base, IOPB_GPIO_CNTL, 0);
10813                         ints = AdvReadByteRegister(iop_base, IOPB_GPIO_DATA);
10814                         if ((ints & 0x01) == 0)
10815                                 eep_config.cfg_lsw &= ~ADV_EEPROM_INTAB;
10816                 }
10817
10818                 /*
10819                  * Assume the 6 byte board serial number that was read from
10820                  * EEPROM is correct even if the EEPROM checksum failed.
10821                  */
10822                 eep_config.serial_number_word3 =
10823                         AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
10824                 eep_config.serial_number_word2 =
10825                         AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
10826                 eep_config.serial_number_word1 =
10827                         AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
10828
10829                 AdvSet38C1600EEPConfig(iop_base, &eep_config);
10830         }
10831
10832         /*
10833          * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10834          * EEPROM configuration that was read.
10835          *
10836          * This is the mapping of EEPROM fields to Adv Library fields.
10837          */
10838         asc_dvc->wdtr_able = eep_config.wdtr_able;
10839         asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10840         asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10841         asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10842         asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10843         asc_dvc->ppr_able = 0;
10844         asc_dvc->tagqng_able = eep_config.tagqng_able;
10845         asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10846         asc_dvc->max_host_qng = eep_config.max_host_qng;
10847         asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10848         asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ASC_MAX_TID);
10849         asc_dvc->start_motor = eep_config.start_motor;
10850         asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10851         asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10852         asc_dvc->no_scam = eep_config.scam_tolerant;
10853
10854         /*
10855          * For every Target ID if any of its 'sdtr_speed[1234]' bits
10856          * are set, then set an 'sdtr_able' bit for it.
10857          */
10858         asc_dvc->sdtr_able = 0;
10859         for (tid = 0; tid <= ASC_MAX_TID; tid++) {
10860                 if (tid == 0) {
10861                         sdtr_speed = asc_dvc->sdtr_speed1;
10862                 } else if (tid == 4) {
10863                         sdtr_speed = asc_dvc->sdtr_speed2;
10864                 } else if (tid == 8) {
10865                         sdtr_speed = asc_dvc->sdtr_speed3;
10866                 } else if (tid == 12) {
10867                         sdtr_speed = asc_dvc->sdtr_speed4;
10868                 }
10869                 if (sdtr_speed & ASC_MAX_TID) {
10870                         asc_dvc->sdtr_able |= (1 << tid);
10871                 }
10872                 sdtr_speed >>= 4;
10873         }
10874
10875         /*
10876          * Set the host maximum queuing (max. 253, min. 16) and the per device
10877          * maximum queuing (max. 63, min. 4).
10878          */
10879         if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10880                 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10881         } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10882                 /* If the value is zero, assume it is uninitialized. */
10883                 if (eep_config.max_host_qng == 0) {
10884                         eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10885                 } else {
10886                         eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10887                 }
10888         }
10889
10890         if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10891                 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10892         } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10893                 /* If the value is zero, assume it is uninitialized. */
10894                 if (eep_config.max_dvc_qng == 0) {
10895                         eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10896                 } else {
10897                         eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10898                 }
10899         }
10900
10901         /*
10902          * If 'max_dvc_qng' is greater than 'max_host_qng', then
10903          * set 'max_dvc_qng' to 'max_host_qng'.
10904          */
10905         if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10906                 eep_config.max_dvc_qng = eep_config.max_host_qng;
10907         }
10908
10909         /*
10910          * Set ASC_DVC_VAR 'max_host_qng' and ASC_DVC_VAR 'max_dvc_qng'
10911          * values based on possibly adjusted EEPROM values.
10912          */
10913         asc_dvc->max_host_qng = eep_config.max_host_qng;
10914         asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10915
10916         /*
10917          * If the EEPROM 'termination' field is set to automatic (0), then set
10918          * the ASC_DVC_CFG 'termination' field to automatic also.
10919          *
10920          * If the termination is specified with a non-zero 'termination'
10921          * value check that a legal value is set and set the ASC_DVC_CFG
10922          * 'termination' field appropriately.
10923          */
10924         if (eep_config.termination_se == 0) {
10925                 termination = 0;        /* auto termination for SE */
10926         } else {
10927                 /* Enable manual control with low off / high off. */
10928                 if (eep_config.termination_se == 1) {
10929                         termination = 0;
10930
10931                         /* Enable manual control with low off / high on. */
10932                 } else if (eep_config.termination_se == 2) {
10933                         termination = TERM_SE_HI;
10934
10935                         /* Enable manual control with low on / high on. */
10936                 } else if (eep_config.termination_se == 3) {
10937                         termination = TERM_SE;
10938                 } else {
10939                         /*
10940                          * The EEPROM 'termination_se' field contains a bad value.
10941                          * Use automatic termination instead.
10942                          */
10943                         termination = 0;
10944                         warn_code |= ASC_WARN_EEPROM_TERMINATION;
10945                 }
10946         }
10947
10948         if (eep_config.termination_lvd == 0) {
10949                 asc_dvc->cfg->termination = termination;        /* auto termination for LVD */
10950         } else {
10951                 /* Enable manual control with low off / high off. */
10952                 if (eep_config.termination_lvd == 1) {
10953                         asc_dvc->cfg->termination = termination;
10954
10955                         /* Enable manual control with low off / high on. */
10956                 } else if (eep_config.termination_lvd == 2) {
10957                         asc_dvc->cfg->termination = termination | TERM_LVD_HI;
10958
10959                         /* Enable manual control with low on / high on. */
10960                 } else if (eep_config.termination_lvd == 3) {
10961                         asc_dvc->cfg->termination = termination | TERM_LVD;
10962                 } else {
10963                         /*
10964                          * The EEPROM 'termination_lvd' field contains a bad value.
10965                          * Use automatic termination instead.
10966                          */
10967                         asc_dvc->cfg->termination = termination;
10968                         warn_code |= ASC_WARN_EEPROM_TERMINATION;
10969                 }
10970         }
10971
10972         return warn_code;
10973 }
10974
10975 /*
10976  * Initialize the ADV_DVC_VAR structure.
10977  *
10978  * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10979  *
10980  * For a non-fatal error return a warning code. If there are no warnings
10981  * then 0 is returned.
10982  */
10983 static int AdvInitGetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
10984 {
10985         struct asc_board *board = shost_priv(shost);
10986         ADV_DVC_VAR *asc_dvc = &board->dvc_var.adv_dvc_var;
10987         unsigned short warn_code = 0;
10988         AdvPortAddr iop_base = asc_dvc->iop_base;
10989         u16 cmd;
10990         int status;
10991
10992         asc_dvc->err_code = 0;
10993
10994         /*
10995          * Save the state of the PCI Configuration Command Register
10996          * "Parity Error Response Control" Bit. If the bit is clear (0),
10997          * in AdvInitAsc3550/38C0800Driver() tell the microcode to ignore
10998          * DMA parity errors.
10999          */
11000         asc_dvc->cfg->control_flag = 0;
11001         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
11002         if ((cmd & PCI_COMMAND_PARITY) == 0)
11003                 asc_dvc->cfg->control_flag |= CONTROL_FLAG_IGNORE_PERR;
11004
11005         asc_dvc->cfg->chip_version =
11006             AdvGetChipVersion(iop_base, asc_dvc->bus_type);
11007
11008         ASC_DBG(1, "iopb_chip_id_1: 0x%x 0x%x\n",
11009                  (ushort)AdvReadByteRegister(iop_base, IOPB_CHIP_ID_1),
11010                  (ushort)ADV_CHIP_ID_BYTE);
11011
11012         ASC_DBG(1, "iopw_chip_id_0: 0x%x 0x%x\n",
11013                  (ushort)AdvReadWordRegister(iop_base, IOPW_CHIP_ID_0),
11014                  (ushort)ADV_CHIP_ID_WORD);
11015
11016         /*
11017          * Reset the chip to start and allow register writes.
11018          */
11019         if (AdvFindSignature(iop_base) == 0) {
11020                 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
11021                 return ADV_ERROR;
11022         } else {
11023                 /*
11024                  * The caller must set 'chip_type' to a valid setting.
11025                  */
11026                 if (asc_dvc->chip_type != ADV_CHIP_ASC3550 &&
11027                     asc_dvc->chip_type != ADV_CHIP_ASC38C0800 &&
11028                     asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
11029                         asc_dvc->err_code |= ASC_IERR_BAD_CHIPTYPE;
11030                         return ADV_ERROR;
11031                 }
11032
11033                 /*
11034                  * Reset Chip.
11035                  */
11036                 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
11037                                      ADV_CTRL_REG_CMD_RESET);
11038                 mdelay(100);
11039                 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
11040                                      ADV_CTRL_REG_CMD_WR_IO_REG);
11041
11042                 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
11043                         status = AdvInitFrom38C1600EEP(asc_dvc);
11044                 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
11045                         status = AdvInitFrom38C0800EEP(asc_dvc);
11046                 } else {
11047                         status = AdvInitFrom3550EEP(asc_dvc);
11048                 }
11049                 warn_code |= status;
11050         }
11051
11052         if (warn_code != 0)
11053                 shost_printk(KERN_WARNING, shost, "warning: 0x%x\n", warn_code);
11054
11055         if (asc_dvc->err_code)
11056                 shost_printk(KERN_ERR, shost, "error code 0x%x\n",
11057                                 asc_dvc->err_code);
11058
11059         return asc_dvc->err_code;
11060 }
11061 #endif
11062
11063 static struct scsi_host_template advansys_template = {
11064         .proc_name = DRV_NAME,
11065 #ifdef CONFIG_PROC_FS
11066         .show_info = advansys_show_info,
11067 #endif
11068         .name = DRV_NAME,
11069         .info = advansys_info,
11070         .queuecommand = advansys_queuecommand,
11071         .eh_host_reset_handler = advansys_reset,
11072         .bios_param = advansys_biosparam,
11073         .slave_configure = advansys_slave_configure,
11074         /*
11075          * Because the driver may control an ISA adapter 'unchecked_isa_dma'
11076          * must be set. The flag will be cleared in advansys_board_found
11077          * for non-ISA adapters.
11078          */
11079         .unchecked_isa_dma = true,
11080         /*
11081          * All adapters controlled by this driver are capable of large
11082          * scatter-gather lists. According to the mid-level SCSI documentation
11083          * this obviates any performance gain provided by setting
11084          * 'use_clustering'. But empirically while CPU utilization is increased
11085          * by enabling clustering, I/O throughput increases as well.
11086          */
11087         .use_clustering = ENABLE_CLUSTERING,
11088         .use_blk_tags = 1,
11089 };
11090
11091 static int advansys_wide_init_chip(struct Scsi_Host *shost)
11092 {
11093         struct asc_board *board = shost_priv(shost);
11094         struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
11095         size_t sgblk_pool_size;
11096         int warn_code, err_code;
11097
11098         /*
11099          * Allocate buffer carrier structures. The total size
11100          * is about 8 KB, so allocate all at once.
11101          */
11102         adv_dvc->carrier = dma_alloc_coherent(board->dev,
11103                 ADV_CARRIER_BUFSIZE, &adv_dvc->carrier_addr, GFP_KERNEL);
11104         ASC_DBG(1, "carrier 0x%p\n", adv_dvc->carrier);
11105
11106         if (!adv_dvc->carrier)
11107                 goto kmalloc_failed;
11108
11109         /*
11110          * Allocate up to 'max_host_qng' request structures for the Wide
11111          * board. The total size is about 16 KB, so allocate all at once.
11112          * If the allocation fails decrement and try again.
11113          */
11114         board->adv_reqp_size = adv_dvc->max_host_qng * sizeof(adv_req_t);
11115         if (board->adv_reqp_size & 0x1f) {
11116                 ASC_DBG(1, "unaligned reqp %lu bytes\n", sizeof(adv_req_t));
11117                 board->adv_reqp_size = ADV_32BALIGN(board->adv_reqp_size);
11118         }
11119         board->adv_reqp = dma_alloc_coherent(board->dev, board->adv_reqp_size,
11120                 &board->adv_reqp_addr, GFP_KERNEL);
11121
11122         if (!board->adv_reqp)
11123                 goto kmalloc_failed;
11124
11125         ASC_DBG(1, "reqp 0x%p, req_cnt %d, bytes %lu\n", board->adv_reqp,
11126                 adv_dvc->max_host_qng, board->adv_reqp_size);
11127
11128         /*
11129          * Allocate up to ADV_TOT_SG_BLOCK request structures for
11130          * the Wide board. Each structure is about 136 bytes.
11131          */
11132         sgblk_pool_size = sizeof(adv_sgblk_t) * ADV_TOT_SG_BLOCK;
11133         board->adv_sgblk_pool = dma_pool_create("adv_sgblk", board->dev,
11134                                                 sgblk_pool_size, 32, 0);
11135
11136         ASC_DBG(1, "sg_cnt %d * %lu = %lu bytes\n", ADV_TOT_SG_BLOCK,
11137                 sizeof(adv_sgblk_t), sgblk_pool_size);
11138
11139         if (!board->adv_sgblk_pool)
11140                 goto kmalloc_failed;
11141
11142         if (adv_dvc->chip_type == ADV_CHIP_ASC3550) {
11143                 ASC_DBG(2, "AdvInitAsc3550Driver()\n");
11144                 warn_code = AdvInitAsc3550Driver(adv_dvc);
11145         } else if (adv_dvc->chip_type == ADV_CHIP_ASC38C0800) {
11146                 ASC_DBG(2, "AdvInitAsc38C0800Driver()\n");
11147                 warn_code = AdvInitAsc38C0800Driver(adv_dvc);
11148         } else {
11149                 ASC_DBG(2, "AdvInitAsc38C1600Driver()\n");
11150                 warn_code = AdvInitAsc38C1600Driver(adv_dvc);
11151         }
11152         err_code = adv_dvc->err_code;
11153
11154         if (warn_code || err_code) {
11155                 shost_printk(KERN_WARNING, shost, "error: warn 0x%x, error "
11156                         "0x%x\n", warn_code, err_code);
11157         }
11158
11159         goto exit;
11160
11161  kmalloc_failed:
11162         shost_printk(KERN_ERR, shost, "error: kmalloc() failed\n");
11163         err_code = ADV_ERROR;
11164  exit:
11165         return err_code;
11166 }
11167
11168 static void advansys_wide_free_mem(struct asc_board *board)
11169 {
11170         struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
11171
11172         if (adv_dvc->carrier) {
11173                 dma_free_coherent(board->dev, ADV_CARRIER_BUFSIZE,
11174                                   adv_dvc->carrier, adv_dvc->carrier_addr);
11175                 adv_dvc->carrier = NULL;
11176         }
11177         if (board->adv_reqp) {
11178                 dma_free_coherent(board->dev, board->adv_reqp_size,
11179                                   board->adv_reqp, board->adv_reqp_addr);
11180                 board->adv_reqp = NULL;
11181         }
11182         if (board->adv_sgblk_pool) {
11183                 dma_pool_destroy(board->adv_sgblk_pool);
11184                 board->adv_sgblk_pool = NULL;
11185         }
11186 }
11187
11188 static int advansys_board_found(struct Scsi_Host *shost, unsigned int iop,
11189                                 int bus_type)
11190 {
11191         struct pci_dev *pdev;
11192         struct asc_board *boardp = shost_priv(shost);
11193         ASC_DVC_VAR *asc_dvc_varp = NULL;
11194         ADV_DVC_VAR *adv_dvc_varp = NULL;
11195         int share_irq, warn_code, ret;
11196
11197         pdev = (bus_type == ASC_IS_PCI) ? to_pci_dev(boardp->dev) : NULL;
11198
11199         if (ASC_NARROW_BOARD(boardp)) {
11200                 ASC_DBG(1, "narrow board\n");
11201                 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
11202                 asc_dvc_varp->bus_type = bus_type;
11203                 asc_dvc_varp->drv_ptr = boardp;
11204                 asc_dvc_varp->cfg = &boardp->dvc_cfg.asc_dvc_cfg;
11205                 asc_dvc_varp->iop_base = iop;
11206         } else {
11207 #ifdef CONFIG_PCI
11208                 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
11209                 adv_dvc_varp->drv_ptr = boardp;
11210                 adv_dvc_varp->cfg = &boardp->dvc_cfg.adv_dvc_cfg;
11211                 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW) {
11212                         ASC_DBG(1, "wide board ASC-3550\n");
11213                         adv_dvc_varp->chip_type = ADV_CHIP_ASC3550;
11214                 } else if (pdev->device == PCI_DEVICE_ID_38C0800_REV1) {
11215                         ASC_DBG(1, "wide board ASC-38C0800\n");
11216                         adv_dvc_varp->chip_type = ADV_CHIP_ASC38C0800;
11217                 } else {
11218                         ASC_DBG(1, "wide board ASC-38C1600\n");
11219                         adv_dvc_varp->chip_type = ADV_CHIP_ASC38C1600;
11220                 }
11221
11222                 boardp->asc_n_io_port = pci_resource_len(pdev, 1);
11223                 boardp->ioremap_addr = pci_ioremap_bar(pdev, 1);
11224                 if (!boardp->ioremap_addr) {
11225                         shost_printk(KERN_ERR, shost, "ioremap(%lx, %d) "
11226                                         "returned NULL\n",
11227                                         (long)pci_resource_start(pdev, 1),
11228                                         boardp->asc_n_io_port);
11229                         ret = -ENODEV;
11230                         goto err_shost;
11231                 }
11232                 adv_dvc_varp->iop_base = (AdvPortAddr)boardp->ioremap_addr;
11233                 ASC_DBG(1, "iop_base: 0x%p\n", adv_dvc_varp->iop_base);
11234
11235                 /*
11236                  * Even though it isn't used to access wide boards, other
11237                  * than for the debug line below, save I/O Port address so
11238                  * that it can be reported.
11239                  */
11240                 boardp->ioport = iop;
11241
11242                 ASC_DBG(1, "iopb_chip_id_1 0x%x, iopw_chip_id_0 0x%x\n",
11243                                 (ushort)inp(iop + 1), (ushort)inpw(iop));
11244 #endif /* CONFIG_PCI */
11245         }
11246
11247         if (ASC_NARROW_BOARD(boardp)) {
11248                 /*
11249                  * Set the board bus type and PCI IRQ before
11250                  * calling AscInitGetConfig().
11251                  */
11252                 switch (asc_dvc_varp->bus_type) {
11253 #ifdef CONFIG_ISA
11254                 case ASC_IS_ISA:
11255                         shost->unchecked_isa_dma = true;
11256                         share_irq = 0;
11257                         break;
11258                 case ASC_IS_VL:
11259                         shost->unchecked_isa_dma = false;
11260                         share_irq = 0;
11261                         break;
11262                 case ASC_IS_EISA:
11263                         shost->unchecked_isa_dma = false;
11264                         share_irq = IRQF_SHARED;
11265                         break;
11266 #endif /* CONFIG_ISA */
11267 #ifdef CONFIG_PCI
11268                 case ASC_IS_PCI:
11269                         shost->unchecked_isa_dma = false;
11270                         share_irq = IRQF_SHARED;
11271                         break;
11272 #endif /* CONFIG_PCI */
11273                 default:
11274                         shost_printk(KERN_ERR, shost, "unknown adapter type: "
11275                                         "%d\n", asc_dvc_varp->bus_type);
11276                         shost->unchecked_isa_dma = false;
11277                         share_irq = 0;
11278                         break;
11279                 }
11280
11281                 /*
11282                  * NOTE: AscInitGetConfig() may change the board's
11283                  * bus_type value. The bus_type value should no
11284                  * longer be used. If the bus_type field must be
11285                  * referenced only use the bit-wise AND operator "&".
11286                  */
11287                 ASC_DBG(2, "AscInitGetConfig()\n");
11288                 ret = AscInitGetConfig(shost) ? -ENODEV : 0;
11289         } else {
11290 #ifdef CONFIG_PCI
11291                 /*
11292                  * For Wide boards set PCI information before calling
11293                  * AdvInitGetConfig().
11294                  */
11295                 shost->unchecked_isa_dma = false;
11296                 share_irq = IRQF_SHARED;
11297                 ASC_DBG(2, "AdvInitGetConfig()\n");
11298
11299                 ret = AdvInitGetConfig(pdev, shost) ? -ENODEV : 0;
11300 #endif /* CONFIG_PCI */
11301         }
11302
11303         if (ret)
11304                 goto err_unmap;
11305
11306         /*
11307          * Save the EEPROM configuration so that it can be displayed
11308          * from /proc/scsi/advansys/[0...].
11309          */
11310         if (ASC_NARROW_BOARD(boardp)) {
11311
11312                 ASCEEP_CONFIG *ep;
11313
11314                 /*
11315                  * Set the adapter's target id bit in the 'init_tidmask' field.
11316                  */
11317                 boardp->init_tidmask |=
11318                     ADV_TID_TO_TIDMASK(asc_dvc_varp->cfg->chip_scsi_id);
11319
11320                 /*
11321                  * Save EEPROM settings for the board.
11322                  */
11323                 ep = &boardp->eep_config.asc_eep;
11324
11325                 ep->init_sdtr = asc_dvc_varp->cfg->sdtr_enable;
11326                 ep->disc_enable = asc_dvc_varp->cfg->disc_enable;
11327                 ep->use_cmd_qng = asc_dvc_varp->cfg->cmd_qng_enabled;
11328                 ASC_EEP_SET_DMA_SPD(ep, asc_dvc_varp->cfg->isa_dma_speed);
11329                 ep->start_motor = asc_dvc_varp->start_motor;
11330                 ep->cntl = asc_dvc_varp->dvc_cntl;
11331                 ep->no_scam = asc_dvc_varp->no_scam;
11332                 ep->max_total_qng = asc_dvc_varp->max_total_qng;
11333                 ASC_EEP_SET_CHIP_ID(ep, asc_dvc_varp->cfg->chip_scsi_id);
11334                 /* 'max_tag_qng' is set to the same value for every device. */
11335                 ep->max_tag_qng = asc_dvc_varp->cfg->max_tag_qng[0];
11336                 ep->adapter_info[0] = asc_dvc_varp->cfg->adapter_info[0];
11337                 ep->adapter_info[1] = asc_dvc_varp->cfg->adapter_info[1];
11338                 ep->adapter_info[2] = asc_dvc_varp->cfg->adapter_info[2];
11339                 ep->adapter_info[3] = asc_dvc_varp->cfg->adapter_info[3];
11340                 ep->adapter_info[4] = asc_dvc_varp->cfg->adapter_info[4];
11341                 ep->adapter_info[5] = asc_dvc_varp->cfg->adapter_info[5];
11342
11343                 /*
11344                  * Modify board configuration.
11345                  */
11346                 ASC_DBG(2, "AscInitSetConfig()\n");
11347                 ret = AscInitSetConfig(pdev, shost) ? -ENODEV : 0;
11348                 if (ret)
11349                         goto err_unmap;
11350         } else {
11351                 ADVEEP_3550_CONFIG *ep_3550;
11352                 ADVEEP_38C0800_CONFIG *ep_38C0800;
11353                 ADVEEP_38C1600_CONFIG *ep_38C1600;
11354
11355                 /*
11356                  * Save Wide EEP Configuration Information.
11357                  */
11358                 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
11359                         ep_3550 = &boardp->eep_config.adv_3550_eep;
11360
11361                         ep_3550->adapter_scsi_id = adv_dvc_varp->chip_scsi_id;
11362                         ep_3550->max_host_qng = adv_dvc_varp->max_host_qng;
11363                         ep_3550->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11364                         ep_3550->termination = adv_dvc_varp->cfg->termination;
11365                         ep_3550->disc_enable = adv_dvc_varp->cfg->disc_enable;
11366                         ep_3550->bios_ctrl = adv_dvc_varp->bios_ctrl;
11367                         ep_3550->wdtr_able = adv_dvc_varp->wdtr_able;
11368                         ep_3550->sdtr_able = adv_dvc_varp->sdtr_able;
11369                         ep_3550->ultra_able = adv_dvc_varp->ultra_able;
11370                         ep_3550->tagqng_able = adv_dvc_varp->tagqng_able;
11371                         ep_3550->start_motor = adv_dvc_varp->start_motor;
11372                         ep_3550->scsi_reset_delay =
11373                             adv_dvc_varp->scsi_reset_wait;
11374                         ep_3550->serial_number_word1 =
11375                             adv_dvc_varp->cfg->serial1;
11376                         ep_3550->serial_number_word2 =
11377                             adv_dvc_varp->cfg->serial2;
11378                         ep_3550->serial_number_word3 =
11379                             adv_dvc_varp->cfg->serial3;
11380                 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
11381                         ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
11382
11383                         ep_38C0800->adapter_scsi_id =
11384                             adv_dvc_varp->chip_scsi_id;
11385                         ep_38C0800->max_host_qng = adv_dvc_varp->max_host_qng;
11386                         ep_38C0800->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11387                         ep_38C0800->termination_lvd =
11388                             adv_dvc_varp->cfg->termination;
11389                         ep_38C0800->disc_enable =
11390                             adv_dvc_varp->cfg->disc_enable;
11391                         ep_38C0800->bios_ctrl = adv_dvc_varp->bios_ctrl;
11392                         ep_38C0800->wdtr_able = adv_dvc_varp->wdtr_able;
11393                         ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11394                         ep_38C0800->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11395                         ep_38C0800->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11396                         ep_38C0800->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11397                         ep_38C0800->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11398                         ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11399                         ep_38C0800->start_motor = adv_dvc_varp->start_motor;
11400                         ep_38C0800->scsi_reset_delay =
11401                             adv_dvc_varp->scsi_reset_wait;
11402                         ep_38C0800->serial_number_word1 =
11403                             adv_dvc_varp->cfg->serial1;
11404                         ep_38C0800->serial_number_word2 =
11405                             adv_dvc_varp->cfg->serial2;
11406                         ep_38C0800->serial_number_word3 =
11407                             adv_dvc_varp->cfg->serial3;
11408                 } else {
11409                         ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
11410
11411                         ep_38C1600->adapter_scsi_id =
11412                             adv_dvc_varp->chip_scsi_id;
11413                         ep_38C1600->max_host_qng = adv_dvc_varp->max_host_qng;
11414                         ep_38C1600->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11415                         ep_38C1600->termination_lvd =
11416                             adv_dvc_varp->cfg->termination;
11417                         ep_38C1600->disc_enable =
11418                             adv_dvc_varp->cfg->disc_enable;
11419                         ep_38C1600->bios_ctrl = adv_dvc_varp->bios_ctrl;
11420                         ep_38C1600->wdtr_able = adv_dvc_varp->wdtr_able;
11421                         ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11422                         ep_38C1600->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11423                         ep_38C1600->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11424                         ep_38C1600->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11425                         ep_38C1600->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11426                         ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11427                         ep_38C1600->start_motor = adv_dvc_varp->start_motor;
11428                         ep_38C1600->scsi_reset_delay =
11429                             adv_dvc_varp->scsi_reset_wait;
11430                         ep_38C1600->serial_number_word1 =
11431                             adv_dvc_varp->cfg->serial1;
11432                         ep_38C1600->serial_number_word2 =
11433                             adv_dvc_varp->cfg->serial2;
11434                         ep_38C1600->serial_number_word3 =
11435                             adv_dvc_varp->cfg->serial3;
11436                 }
11437
11438                 /*
11439                  * Set the adapter's target id bit in the 'init_tidmask' field.
11440                  */
11441                 boardp->init_tidmask |=
11442                     ADV_TID_TO_TIDMASK(adv_dvc_varp->chip_scsi_id);
11443         }
11444
11445         /*
11446          * Channels are numbered beginning with 0. For AdvanSys one host
11447          * structure supports one channel. Multi-channel boards have a
11448          * separate host structure for each channel.
11449          */
11450         shost->max_channel = 0;
11451         if (ASC_NARROW_BOARD(boardp)) {
11452                 shost->max_id = ASC_MAX_TID + 1;
11453                 shost->max_lun = ASC_MAX_LUN + 1;
11454                 shost->max_cmd_len = ASC_MAX_CDB_LEN;
11455
11456                 shost->io_port = asc_dvc_varp->iop_base;
11457                 boardp->asc_n_io_port = ASC_IOADR_GAP;
11458                 shost->this_id = asc_dvc_varp->cfg->chip_scsi_id;
11459
11460                 /* Set maximum number of queues the adapter can handle. */
11461                 shost->can_queue = asc_dvc_varp->max_total_qng;
11462         } else {
11463                 shost->max_id = ADV_MAX_TID + 1;
11464                 shost->max_lun = ADV_MAX_LUN + 1;
11465                 shost->max_cmd_len = ADV_MAX_CDB_LEN;
11466
11467                 /*
11468                  * Save the I/O Port address and length even though
11469                  * I/O ports are not used to access Wide boards.
11470                  * Instead the Wide boards are accessed with
11471                  * PCI Memory Mapped I/O.
11472                  */
11473                 shost->io_port = iop;
11474
11475                 shost->this_id = adv_dvc_varp->chip_scsi_id;
11476
11477                 /* Set maximum number of queues the adapter can handle. */
11478                 shost->can_queue = adv_dvc_varp->max_host_qng;
11479         }
11480         ret = scsi_init_shared_tag_map(shost, shost->can_queue);
11481         if (ret) {
11482                 shost_printk(KERN_ERR, shost, "init tag map failed\n");
11483                 goto err_free_dma;
11484         }
11485
11486         /*
11487          * Following v1.3.89, 'cmd_per_lun' is no longer needed
11488          * and should be set to zero.
11489          *
11490          * But because of a bug introduced in v1.3.89 if the driver is
11491          * compiled as a module and 'cmd_per_lun' is zero, the Mid-Level
11492          * SCSI function 'allocate_device' will panic. To allow the driver
11493          * to work as a module in these kernels set 'cmd_per_lun' to 1.
11494          *
11495          * Note: This is wrong.  cmd_per_lun should be set to the depth
11496          * you want on untagged devices always.
11497          #ifdef MODULE
11498          */
11499         shost->cmd_per_lun = 1;
11500 /* #else
11501             shost->cmd_per_lun = 0;
11502 #endif */
11503
11504         /*
11505          * Set the maximum number of scatter-gather elements the
11506          * adapter can handle.
11507          */
11508         if (ASC_NARROW_BOARD(boardp)) {
11509                 /*
11510                  * Allow two commands with 'sg_tablesize' scatter-gather
11511                  * elements to be executed simultaneously. This value is
11512                  * the theoretical hardware limit. It may be decreased
11513                  * below.
11514                  */
11515                 shost->sg_tablesize =
11516                     (((asc_dvc_varp->max_total_qng - 2) / 2) *
11517                      ASC_SG_LIST_PER_Q) + 1;
11518         } else {
11519                 shost->sg_tablesize = ADV_MAX_SG_LIST;
11520         }
11521
11522         /*
11523          * The value of 'sg_tablesize' can not exceed the SCSI
11524          * mid-level driver definition of SG_ALL. SG_ALL also
11525          * must not be exceeded, because it is used to define the
11526          * size of the scatter-gather table in 'struct asc_sg_head'.
11527          */
11528         if (shost->sg_tablesize > SG_ALL) {
11529                 shost->sg_tablesize = SG_ALL;
11530         }
11531
11532         ASC_DBG(1, "sg_tablesize: %d\n", shost->sg_tablesize);
11533
11534         /* BIOS start address. */
11535         if (ASC_NARROW_BOARD(boardp)) {
11536                 shost->base = AscGetChipBiosAddress(asc_dvc_varp->iop_base,
11537                                                     asc_dvc_varp->bus_type);
11538         } else {
11539                 /*
11540                  * Fill-in BIOS board variables. The Wide BIOS saves
11541                  * information in LRAM that is used by the driver.
11542                  */
11543                 AdvReadWordLram(adv_dvc_varp->iop_base,
11544                                 BIOS_SIGNATURE, boardp->bios_signature);
11545                 AdvReadWordLram(adv_dvc_varp->iop_base,
11546                                 BIOS_VERSION, boardp->bios_version);
11547                 AdvReadWordLram(adv_dvc_varp->iop_base,
11548                                 BIOS_CODESEG, boardp->bios_codeseg);
11549                 AdvReadWordLram(adv_dvc_varp->iop_base,
11550                                 BIOS_CODELEN, boardp->bios_codelen);
11551
11552                 ASC_DBG(1, "bios_signature 0x%x, bios_version 0x%x\n",
11553                          boardp->bios_signature, boardp->bios_version);
11554
11555                 ASC_DBG(1, "bios_codeseg 0x%x, bios_codelen 0x%x\n",
11556                          boardp->bios_codeseg, boardp->bios_codelen);
11557
11558                 /*
11559                  * If the BIOS saved a valid signature, then fill in
11560                  * the BIOS code segment base address.
11561                  */
11562                 if (boardp->bios_signature == 0x55AA) {
11563                         /*
11564                          * Convert x86 realmode code segment to a linear
11565                          * address by shifting left 4.
11566                          */
11567                         shost->base = ((ulong)boardp->bios_codeseg << 4);
11568                 } else {
11569                         shost->base = 0;
11570                 }
11571         }
11572
11573         /*
11574          * Register Board Resources - I/O Port, DMA, IRQ
11575          */
11576
11577         /* Register DMA Channel for Narrow boards. */
11578         shost->dma_channel = NO_ISA_DMA;        /* Default to no ISA DMA. */
11579 #ifdef CONFIG_ISA
11580         if (ASC_NARROW_BOARD(boardp)) {
11581                 /* Register DMA channel for ISA bus. */
11582                 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
11583                         shost->dma_channel = asc_dvc_varp->cfg->isa_dma_channel;
11584                         ret = request_dma(shost->dma_channel, DRV_NAME);
11585                         if (ret) {
11586                                 shost_printk(KERN_ERR, shost, "request_dma() "
11587                                                 "%d failed %d\n",
11588                                                 shost->dma_channel, ret);
11589                                 goto err_unmap;
11590                         }
11591                         AscEnableIsaDma(shost->dma_channel);
11592                 }
11593         }
11594 #endif /* CONFIG_ISA */
11595
11596         /* Register IRQ Number. */
11597         ASC_DBG(2, "request_irq(%d, %p)\n", boardp->irq, shost);
11598
11599         ret = request_irq(boardp->irq, advansys_interrupt, share_irq,
11600                           DRV_NAME, shost);
11601
11602         if (ret) {
11603                 if (ret == -EBUSY) {
11604                         shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11605                                         "already in use\n", boardp->irq);
11606                 } else if (ret == -EINVAL) {
11607                         shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11608                                         "not valid\n", boardp->irq);
11609                 } else {
11610                         shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11611                                         "failed with %d\n", boardp->irq, ret);
11612                 }
11613                 goto err_free_dma;
11614         }
11615
11616         /*
11617          * Initialize board RISC chip and enable interrupts.
11618          */
11619         if (ASC_NARROW_BOARD(boardp)) {
11620                 ASC_DBG(2, "AscInitAsc1000Driver()\n");
11621
11622                 asc_dvc_varp->overrun_buf = kzalloc(ASC_OVERRUN_BSIZE, GFP_KERNEL);
11623                 if (!asc_dvc_varp->overrun_buf) {
11624                         ret = -ENOMEM;
11625                         goto err_free_irq;
11626                 }
11627                 warn_code = AscInitAsc1000Driver(asc_dvc_varp);
11628
11629                 if (warn_code || asc_dvc_varp->err_code) {
11630                         shost_printk(KERN_ERR, shost, "error: init_state 0x%x, "
11631                                         "warn 0x%x, error 0x%x\n",
11632                                         asc_dvc_varp->init_state, warn_code,
11633                                         asc_dvc_varp->err_code);
11634                         if (!asc_dvc_varp->overrun_dma) {
11635                                 ret = -ENODEV;
11636                                 goto err_free_mem;
11637                         }
11638                 }
11639         } else {
11640                 if (advansys_wide_init_chip(shost)) {
11641                         ret = -ENODEV;
11642                         goto err_free_mem;
11643                 }
11644         }
11645
11646         ASC_DBG_PRT_SCSI_HOST(2, shost);
11647
11648         ret = scsi_add_host(shost, boardp->dev);
11649         if (ret)
11650                 goto err_free_mem;
11651
11652         scsi_scan_host(shost);
11653         return 0;
11654
11655  err_free_mem:
11656         if (ASC_NARROW_BOARD(boardp)) {
11657                 if (asc_dvc_varp->overrun_dma)
11658                         dma_unmap_single(boardp->dev, asc_dvc_varp->overrun_dma,
11659                                          ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
11660                 kfree(asc_dvc_varp->overrun_buf);
11661         } else
11662                 advansys_wide_free_mem(boardp);
11663  err_free_irq:
11664         free_irq(boardp->irq, shost);
11665  err_free_dma:
11666 #ifdef CONFIG_ISA
11667         if (shost->dma_channel != NO_ISA_DMA)
11668                 free_dma(shost->dma_channel);
11669 #endif
11670  err_unmap:
11671         if (boardp->ioremap_addr)
11672                 iounmap(boardp->ioremap_addr);
11673  err_shost:
11674         return ret;
11675 }
11676
11677 /*
11678  * advansys_release()
11679  *
11680  * Release resources allocated for a single AdvanSys adapter.
11681  */
11682 static int advansys_release(struct Scsi_Host *shost)
11683 {
11684         struct asc_board *board = shost_priv(shost);
11685         ASC_DBG(1, "begin\n");
11686         scsi_remove_host(shost);
11687         free_irq(board->irq, shost);
11688 #ifdef CONFIG_ISA
11689         if (shost->dma_channel != NO_ISA_DMA) {
11690                 ASC_DBG(1, "free_dma()\n");
11691                 free_dma(shost->dma_channel);
11692         }
11693 #endif
11694         if (ASC_NARROW_BOARD(board)) {
11695                 dma_unmap_single(board->dev,
11696                                         board->dvc_var.asc_dvc_var.overrun_dma,
11697                                         ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
11698                 kfree(board->dvc_var.asc_dvc_var.overrun_buf);
11699         } else {
11700                 iounmap(board->ioremap_addr);
11701                 advansys_wide_free_mem(board);
11702         }
11703         scsi_host_put(shost);
11704         ASC_DBG(1, "end\n");
11705         return 0;
11706 }
11707
11708 #define ASC_IOADR_TABLE_MAX_IX  11
11709
11710 static PortAddr _asc_def_iop_base[ASC_IOADR_TABLE_MAX_IX] = {
11711         0x100, 0x0110, 0x120, 0x0130, 0x140, 0x0150, 0x0190,
11712         0x0210, 0x0230, 0x0250, 0x0330
11713 };
11714
11715 /*
11716  * The ISA IRQ number is found in bits 2 and 3 of the CfgLsw.  It decodes as:
11717  * 00: 10
11718  * 01: 11
11719  * 10: 12
11720  * 11: 15
11721  */
11722 static unsigned int advansys_isa_irq_no(PortAddr iop_base)
11723 {
11724         unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11725         unsigned int chip_irq = ((cfg_lsw >> 2) & 0x03) + 10;
11726         if (chip_irq == 13)
11727                 chip_irq = 15;
11728         return chip_irq;
11729 }
11730
11731 static int advansys_isa_probe(struct device *dev, unsigned int id)
11732 {
11733         int err = -ENODEV;
11734         PortAddr iop_base = _asc_def_iop_base[id];
11735         struct Scsi_Host *shost;
11736         struct asc_board *board;
11737
11738         if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
11739                 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
11740                 return -ENODEV;
11741         }
11742         ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
11743         if (!AscFindSignature(iop_base))
11744                 goto release_region;
11745         if (!(AscGetChipVersion(iop_base, ASC_IS_ISA) & ASC_CHIP_VER_ISA_BIT))
11746                 goto release_region;
11747
11748         err = -ENOMEM;
11749         shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11750         if (!shost)
11751                 goto release_region;
11752
11753         board = shost_priv(shost);
11754         board->irq = advansys_isa_irq_no(iop_base);
11755         board->dev = dev;
11756         board->shost = shost;
11757
11758         err = advansys_board_found(shost, iop_base, ASC_IS_ISA);
11759         if (err)
11760                 goto free_host;
11761
11762         dev_set_drvdata(dev, shost);
11763         return 0;
11764
11765  free_host:
11766         scsi_host_put(shost);
11767  release_region:
11768         release_region(iop_base, ASC_IOADR_GAP);
11769         return err;
11770 }
11771
11772 static int advansys_isa_remove(struct device *dev, unsigned int id)
11773 {
11774         int ioport = _asc_def_iop_base[id];
11775         advansys_release(dev_get_drvdata(dev));
11776         release_region(ioport, ASC_IOADR_GAP);
11777         return 0;
11778 }
11779
11780 static struct isa_driver advansys_isa_driver = {
11781         .probe          = advansys_isa_probe,
11782         .remove         = advansys_isa_remove,
11783         .driver = {
11784                 .owner  = THIS_MODULE,
11785                 .name   = DRV_NAME,
11786         },
11787 };
11788
11789 /*
11790  * The VLB IRQ number is found in bits 2 to 4 of the CfgLsw.  It decodes as:
11791  * 000: invalid
11792  * 001: 10
11793  * 010: 11
11794  * 011: 12
11795  * 100: invalid
11796  * 101: 14
11797  * 110: 15
11798  * 111: invalid
11799  */
11800 static unsigned int advansys_vlb_irq_no(PortAddr iop_base)
11801 {
11802         unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11803         unsigned int chip_irq = ((cfg_lsw >> 2) & 0x07) + 9;
11804         if ((chip_irq < 10) || (chip_irq == 13) || (chip_irq > 15))
11805                 return 0;
11806         return chip_irq;
11807 }
11808
11809 static int advansys_vlb_probe(struct device *dev, unsigned int id)
11810 {
11811         int err = -ENODEV;
11812         PortAddr iop_base = _asc_def_iop_base[id];
11813         struct Scsi_Host *shost;
11814         struct asc_board *board;
11815
11816         if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
11817                 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
11818                 return -ENODEV;
11819         }
11820         ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
11821         if (!AscFindSignature(iop_base))
11822                 goto release_region;
11823         /*
11824          * I don't think this condition can actually happen, but the old
11825          * driver did it, and the chances of finding a VLB setup in 2007
11826          * to do testing with is slight to none.
11827          */
11828         if (AscGetChipVersion(iop_base, ASC_IS_VL) > ASC_CHIP_MAX_VER_VL)
11829                 goto release_region;
11830
11831         err = -ENOMEM;
11832         shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11833         if (!shost)
11834                 goto release_region;
11835
11836         board = shost_priv(shost);
11837         board->irq = advansys_vlb_irq_no(iop_base);
11838         board->dev = dev;
11839         board->shost = shost;
11840
11841         err = advansys_board_found(shost, iop_base, ASC_IS_VL);
11842         if (err)
11843                 goto free_host;
11844
11845         dev_set_drvdata(dev, shost);
11846         return 0;
11847
11848  free_host:
11849         scsi_host_put(shost);
11850  release_region:
11851         release_region(iop_base, ASC_IOADR_GAP);
11852         return -ENODEV;
11853 }
11854
11855 static struct isa_driver advansys_vlb_driver = {
11856         .probe          = advansys_vlb_probe,
11857         .remove         = advansys_isa_remove,
11858         .driver = {
11859                 .owner  = THIS_MODULE,
11860                 .name   = "advansys_vlb",
11861         },
11862 };
11863
11864 static struct eisa_device_id advansys_eisa_table[] = {
11865         { "ABP7401" },
11866         { "ABP7501" },
11867         { "" }
11868 };
11869
11870 MODULE_DEVICE_TABLE(eisa, advansys_eisa_table);
11871
11872 /*
11873  * EISA is a little more tricky than PCI; each EISA device may have two
11874  * channels, and this driver is written to make each channel its own Scsi_Host
11875  */
11876 struct eisa_scsi_data {
11877         struct Scsi_Host *host[2];
11878 };
11879
11880 /*
11881  * The EISA IRQ number is found in bits 8 to 10 of the CfgLsw.  It decodes as:
11882  * 000: 10
11883  * 001: 11
11884  * 010: 12
11885  * 011: invalid
11886  * 100: 14
11887  * 101: 15
11888  * 110: invalid
11889  * 111: invalid
11890  */
11891 static unsigned int advansys_eisa_irq_no(struct eisa_device *edev)
11892 {
11893         unsigned short cfg_lsw = inw(edev->base_addr + 0xc86);
11894         unsigned int chip_irq = ((cfg_lsw >> 8) & 0x07) + 10;
11895         if ((chip_irq == 13) || (chip_irq > 15))
11896                 return 0;
11897         return chip_irq;
11898 }
11899
11900 static int advansys_eisa_probe(struct device *dev)
11901 {
11902         int i, ioport, irq = 0;
11903         int err;
11904         struct eisa_device *edev = to_eisa_device(dev);
11905         struct eisa_scsi_data *data;
11906
11907         err = -ENOMEM;
11908         data = kzalloc(sizeof(*data), GFP_KERNEL);
11909         if (!data)
11910                 goto fail;
11911         ioport = edev->base_addr + 0xc30;
11912
11913         err = -ENODEV;
11914         for (i = 0; i < 2; i++, ioport += 0x20) {
11915                 struct asc_board *board;
11916                 struct Scsi_Host *shost;
11917                 if (!request_region(ioport, ASC_IOADR_GAP, DRV_NAME)) {
11918                         printk(KERN_WARNING "Region %x-%x busy\n", ioport,
11919                                ioport + ASC_IOADR_GAP - 1);
11920                         continue;
11921                 }
11922                 if (!AscFindSignature(ioport)) {
11923                         release_region(ioport, ASC_IOADR_GAP);
11924                         continue;
11925                 }
11926
11927                 /*
11928                  * I don't know why we need to do this for EISA chips, but
11929                  * not for any others.  It looks to be equivalent to
11930                  * AscGetChipCfgMsw, but I may have overlooked something,
11931                  * so I'm not converting it until I get an EISA board to
11932                  * test with.
11933                  */
11934                 inw(ioport + 4);
11935
11936                 if (!irq)
11937                         irq = advansys_eisa_irq_no(edev);
11938
11939                 err = -ENOMEM;
11940                 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11941                 if (!shost)
11942                         goto release_region;
11943
11944                 board = shost_priv(shost);
11945                 board->irq = irq;
11946                 board->dev = dev;
11947                 board->shost = shost;
11948
11949                 err = advansys_board_found(shost, ioport, ASC_IS_EISA);
11950                 if (!err) {
11951                         data->host[i] = shost;
11952                         continue;
11953                 }
11954
11955                 scsi_host_put(shost);
11956  release_region:
11957                 release_region(ioport, ASC_IOADR_GAP);
11958                 break;
11959         }
11960
11961         if (err)
11962                 goto free_data;
11963         dev_set_drvdata(dev, data);
11964         return 0;
11965
11966  free_data:
11967         kfree(data->host[0]);
11968         kfree(data->host[1]);
11969         kfree(data);
11970  fail:
11971         return err;
11972 }
11973
11974 static int advansys_eisa_remove(struct device *dev)
11975 {
11976         int i;
11977         struct eisa_scsi_data *data = dev_get_drvdata(dev);
11978
11979         for (i = 0; i < 2; i++) {
11980                 int ioport;
11981                 struct Scsi_Host *shost = data->host[i];
11982                 if (!shost)
11983                         continue;
11984                 ioport = shost->io_port;
11985                 advansys_release(shost);
11986                 release_region(ioport, ASC_IOADR_GAP);
11987         }
11988
11989         kfree(data);
11990         return 0;
11991 }
11992
11993 static struct eisa_driver advansys_eisa_driver = {
11994         .id_table =             advansys_eisa_table,
11995         .driver = {
11996                 .name =         DRV_NAME,
11997                 .probe =        advansys_eisa_probe,
11998                 .remove =       advansys_eisa_remove,
11999         }
12000 };
12001
12002 /* PCI Devices supported by this driver */
12003 static struct pci_device_id advansys_pci_tbl[] = {
12004         {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_1200A,
12005          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12006         {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940,
12007          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12008         {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940U,
12009          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12010         {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940UW,
12011          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12012         {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C0800_REV1,
12013          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12014         {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C1600_REV1,
12015          PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12016         {}
12017 };
12018
12019 MODULE_DEVICE_TABLE(pci, advansys_pci_tbl);
12020
12021 static void advansys_set_latency(struct pci_dev *pdev)
12022 {
12023         if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
12024             (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
12025                 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0);
12026         } else {
12027                 u8 latency;
12028                 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
12029                 if (latency < 0x20)
12030                         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
12031         }
12032 }
12033
12034 static int advansys_pci_probe(struct pci_dev *pdev,
12035                               const struct pci_device_id *ent)
12036 {
12037         int err, ioport;
12038         struct Scsi_Host *shost;
12039         struct asc_board *board;
12040
12041         err = pci_enable_device(pdev);
12042         if (err)
12043                 goto fail;
12044         err = pci_request_regions(pdev, DRV_NAME);
12045         if (err)
12046                 goto disable_device;
12047         pci_set_master(pdev);
12048         advansys_set_latency(pdev);
12049
12050         err = -ENODEV;
12051         if (pci_resource_len(pdev, 0) == 0)
12052                 goto release_region;
12053
12054         ioport = pci_resource_start(pdev, 0);
12055
12056         err = -ENOMEM;
12057         shost = scsi_host_alloc(&advansys_template, sizeof(*board));
12058         if (!shost)
12059                 goto release_region;
12060
12061         board = shost_priv(shost);
12062         board->irq = pdev->irq;
12063         board->dev = &pdev->dev;
12064         board->shost = shost;
12065
12066         if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW ||
12067             pdev->device == PCI_DEVICE_ID_38C0800_REV1 ||
12068             pdev->device == PCI_DEVICE_ID_38C1600_REV1) {
12069                 board->flags |= ASC_IS_WIDE_BOARD;
12070         }
12071
12072         err = advansys_board_found(shost, ioport, ASC_IS_PCI);
12073         if (err)
12074                 goto free_host;
12075
12076         pci_set_drvdata(pdev, shost);
12077         return 0;
12078
12079  free_host:
12080         scsi_host_put(shost);
12081  release_region:
12082         pci_release_regions(pdev);
12083  disable_device:
12084         pci_disable_device(pdev);
12085  fail:
12086         return err;
12087 }
12088
12089 static void advansys_pci_remove(struct pci_dev *pdev)
12090 {
12091         advansys_release(pci_get_drvdata(pdev));
12092         pci_release_regions(pdev);
12093         pci_disable_device(pdev);
12094 }
12095
12096 static struct pci_driver advansys_pci_driver = {
12097         .name =         DRV_NAME,
12098         .id_table =     advansys_pci_tbl,
12099         .probe =        advansys_pci_probe,
12100         .remove =       advansys_pci_remove,
12101 };
12102
12103 static int __init advansys_init(void)
12104 {
12105         int error;
12106
12107         error = isa_register_driver(&advansys_isa_driver,
12108                                     ASC_IOADR_TABLE_MAX_IX);
12109         if (error)
12110                 goto fail;
12111
12112         error = isa_register_driver(&advansys_vlb_driver,
12113                                     ASC_IOADR_TABLE_MAX_IX);
12114         if (error)
12115                 goto unregister_isa;
12116
12117         error = eisa_driver_register(&advansys_eisa_driver);
12118         if (error)
12119                 goto unregister_vlb;
12120
12121         error = pci_register_driver(&advansys_pci_driver);
12122         if (error)
12123                 goto unregister_eisa;
12124
12125         return 0;
12126
12127  unregister_eisa:
12128         eisa_driver_unregister(&advansys_eisa_driver);
12129  unregister_vlb:
12130         isa_unregister_driver(&advansys_vlb_driver);
12131  unregister_isa:
12132         isa_unregister_driver(&advansys_isa_driver);
12133  fail:
12134         return error;
12135 }
12136
12137 static void __exit advansys_exit(void)
12138 {
12139         pci_unregister_driver(&advansys_pci_driver);
12140         eisa_driver_unregister(&advansys_eisa_driver);
12141         isa_unregister_driver(&advansys_vlb_driver);
12142         isa_unregister_driver(&advansys_isa_driver);
12143 }
12144
12145 module_init(advansys_init);
12146 module_exit(advansys_exit);
12147
12148 MODULE_LICENSE("GPL");
12149 MODULE_FIRMWARE("advansys/mcode.bin");
12150 MODULE_FIRMWARE("advansys/3550.bin");
12151 MODULE_FIRMWARE("advansys/38C0800.bin");
12152 MODULE_FIRMWARE("advansys/38C1600.bin");