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