advansys: Use dma_pool for sg elements
[deliverable/linux.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
0ce53822 1818typedef struct adv_sg_block {
27c868c2
MW
1819 uchar reserved1;
1820 uchar reserved2;
1821 uchar reserved3;
1822 uchar sg_cnt; /* Valid entries in block. */
0ce53822 1823 __le32 sg_ptr; /* Pointer to next sg block. */
27c868c2 1824 struct {
0ce53822
HR
1825 __le32 sg_addr; /* SG element address. */
1826 __le32 sg_count; /* SG element count. */
27c868c2 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. */
0ce53822 1891 dma_addr_t sg_addr; /* Physical address */
98d41c29
MW
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. */
4b47e464 1897 uchar align[24]; /* Request structure padding. */
98d41c29 1898 struct scsi_cmnd *cmndp; /* Mid-Level SCSI command pointer. */
4b47e464 1899 dma_addr_t req_addr;
98d41c29 1900 adv_sgblk_t *sgblkp; /* Adv Library scatter-gather pointer. */
4b47e464 1901} adv_req_t __aligned(32);
98d41c29
MW
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 2381 adv_req_t *adv_reqp; /* Request structures. */
4b47e464
HR
2382 dma_addr_t adv_reqp_addr;
2383 size_t adv_reqp_size;
0ce53822 2384 struct dma_pool *adv_sgblk_pool; /* Scatter-gather structures. */
27c868c2
MW
2385 ushort bios_signature; /* BIOS Signature. */
2386 ushort bios_version; /* BIOS Version. */
2387 ushort bios_codeseg; /* BIOS Code Segment. */
2388 ushort bios_codelen; /* BIOS Code Segment Length. */
d2411495 2389};
1da177e4 2390
d10fb2c7
MW
2391#define asc_dvc_to_board(asc_dvc) container_of(asc_dvc, struct asc_board, \
2392 dvc_var.asc_dvc_var)
13ac2d9c
MW
2393#define adv_dvc_to_board(adv_dvc) container_of(adv_dvc, struct asc_board, \
2394 dvc_var.adv_dvc_var)
2395#define adv_dvc_to_pdev(adv_dvc) to_pci_dev(adv_dvc_to_board(adv_dvc)->dev)
2396
1da177e4 2397#ifdef ADVANSYS_DEBUG
27c868c2 2398static int asc_dbglvl = 3;
1da177e4 2399
1da177e4 2400/*
51219358 2401 * asc_prt_asc_dvc_var()
1da177e4 2402 */
51219358 2403static void asc_prt_asc_dvc_var(ASC_DVC_VAR *h)
1da177e4 2404{
51219358 2405 printk("ASC_DVC_VAR at addr 0x%lx\n", (ulong)h);
27c868c2 2406
51219358
MW
2407 printk(" iop_base 0x%x, err_code 0x%x, dvc_cntl 0x%x, bug_fix_cntl "
2408 "%d,\n", h->iop_base, h->err_code, h->dvc_cntl, h->bug_fix_cntl);
2409
2410 printk(" bus_type %d, init_sdtr 0x%x,\n", h->bus_type,
2411 (unsigned)h->init_sdtr);
2412
2413 printk(" sdtr_done 0x%x, use_tagged_qng 0x%x, unit_not_ready 0x%x, "
2414 "chip_no 0x%x,\n", (unsigned)h->sdtr_done,
2415 (unsigned)h->use_tagged_qng, (unsigned)h->unit_not_ready,
2416 (unsigned)h->chip_no);
2417
2418 printk(" queue_full_or_busy 0x%x, start_motor 0x%x, scsi_reset_wait "
2419 "%u,\n", (unsigned)h->queue_full_or_busy,
2420 (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
2421
2422 printk(" is_in_int %u, max_total_qng %u, cur_total_qng %u, "
2423 "in_critical_cnt %u,\n", (unsigned)h->is_in_int,
2424 (unsigned)h->max_total_qng, (unsigned)h->cur_total_qng,
2425 (unsigned)h->in_critical_cnt);
2426
2427 printk(" last_q_shortage %u, init_state 0x%x, no_scam 0x%x, "
2428 "pci_fix_asyn_xfer 0x%x,\n", (unsigned)h->last_q_shortage,
2429 (unsigned)h->init_state, (unsigned)h->no_scam,
2430 (unsigned)h->pci_fix_asyn_xfer);
2431
d361db48 2432 printk(" cfg 0x%lx\n", (ulong)h->cfg);
1da177e4
LT
2433}
2434
51219358
MW
2435/*
2436 * asc_prt_asc_dvc_cfg()
2437 */
2438static void asc_prt_asc_dvc_cfg(ASC_DVC_CFG *h)
6ed1ef07 2439{
51219358 2440 printk("ASC_DVC_CFG at addr 0x%lx\n", (ulong)h);
6ed1ef07 2441
51219358
MW
2442 printk(" can_tagged_qng 0x%x, cmd_qng_enabled 0x%x,\n",
2443 h->can_tagged_qng, h->cmd_qng_enabled);
2444 printk(" disc_enable 0x%x, sdtr_enable 0x%x,\n",
2445 h->disc_enable, h->sdtr_enable);
6ed1ef07 2446
b08fc565
MW
2447 printk(" chip_scsi_id %d, isa_dma_speed %d, isa_dma_channel %d, "
2448 "chip_version %d,\n", h->chip_scsi_id, h->isa_dma_speed,
2449 h->isa_dma_channel, h->chip_version);
51219358 2450
d10fb2c7
MW
2451 printk(" mcode_date 0x%x, mcode_version %d\n",
2452 h->mcode_date, h->mcode_version);
6ed1ef07
MW
2453}
2454
1da177e4 2455/*
51219358 2456 * asc_prt_adv_dvc_var()
1da177e4 2457 *
51219358 2458 * Display an ADV_DVC_VAR structure.
1da177e4 2459 */
51219358 2460static void asc_prt_adv_dvc_var(ADV_DVC_VAR *h)
1da177e4 2461{
51219358 2462 printk(" ADV_DVC_VAR at addr 0x%lx\n", (ulong)h);
27c868c2 2463
51219358
MW
2464 printk(" iop_base 0x%lx, err_code 0x%x, ultra_able 0x%x\n",
2465 (ulong)h->iop_base, h->err_code, (unsigned)h->ultra_able);
1da177e4 2466
b352f923
MW
2467 printk(" sdtr_able 0x%x, wdtr_able 0x%x\n",
2468 (unsigned)h->sdtr_able, (unsigned)h->wdtr_able);
1da177e4 2469
d361db48
MW
2470 printk(" start_motor 0x%x, scsi_reset_wait 0x%x\n",
2471 (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
1da177e4 2472
98b96a7d 2473 printk(" max_host_qng %u, max_dvc_qng %u, carr_freelist 0x%p\n",
51219358 2474 (unsigned)h->max_host_qng, (unsigned)h->max_dvc_qng,
98b96a7d 2475 h->carr_freelist);
1da177e4 2476
98b96a7d 2477 printk(" icq_sp 0x%p, irq_sp 0x%p\n", h->icq_sp, h->irq_sp);
1da177e4 2478
51219358
MW
2479 printk(" no_scam 0x%x, tagqng_able 0x%x\n",
2480 (unsigned)h->no_scam, (unsigned)h->tagqng_able);
1da177e4 2481
51219358
MW
2482 printk(" chip_scsi_id 0x%x, cfg 0x%lx\n",
2483 (unsigned)h->chip_scsi_id, (ulong)h->cfg);
2484}
1da177e4 2485
51219358
MW
2486/*
2487 * asc_prt_adv_dvc_cfg()
2488 *
2489 * Display an ADV_DVC_CFG structure.
2490 */
2491static void asc_prt_adv_dvc_cfg(ADV_DVC_CFG *h)
2492{
2493 printk(" ADV_DVC_CFG at addr 0x%lx\n", (ulong)h);
27c868c2 2494
51219358
MW
2495 printk(" disc_enable 0x%x, termination 0x%x\n",
2496 h->disc_enable, h->termination);
1da177e4 2497
51219358
MW
2498 printk(" chip_version 0x%x, mcode_date 0x%x\n",
2499 h->chip_version, h->mcode_date);
27c868c2 2500
b352f923
MW
2501 printk(" mcode_version 0x%x, control_flag 0x%x\n",
2502 h->mcode_version, h->control_flag);
1da177e4
LT
2503}
2504
2505/*
b352f923 2506 * asc_prt_scsi_host()
1da177e4 2507 */
b352f923 2508static void asc_prt_scsi_host(struct Scsi_Host *s)
1da177e4 2509{
b352f923 2510 struct asc_board *boardp = shost_priv(s);
51219358 2511
71610f55 2512 printk("Scsi_Host at addr 0x%p, device %s\n", s, dev_name(boardp->dev));
50d14a70 2513 printk(" host_busy %u, host_no %d,\n",
74665016 2514 atomic_read(&s->host_busy), s->host_no);
51219358 2515
b352f923
MW
2516 printk(" base 0x%lx, io_port 0x%lx, irq %d,\n",
2517 (ulong)s->base, (ulong)s->io_port, boardp->irq);
51219358 2518
b352f923
MW
2519 printk(" dma_channel %d, this_id %d, can_queue %d,\n",
2520 s->dma_channel, s->this_id, s->can_queue);
1da177e4 2521
b352f923
MW
2522 printk(" cmd_per_lun %d, sg_tablesize %d, unchecked_isa_dma %d\n",
2523 s->cmd_per_lun, s->sg_tablesize, s->unchecked_isa_dma);
27c868c2 2524
b352f923
MW
2525 if (ASC_NARROW_BOARD(boardp)) {
2526 asc_prt_asc_dvc_var(&boardp->dvc_var.asc_dvc_var);
2527 asc_prt_asc_dvc_cfg(&boardp->dvc_cfg.asc_dvc_cfg);
2528 } else {
2529 asc_prt_adv_dvc_var(&boardp->dvc_var.adv_dvc_var);
2530 asc_prt_adv_dvc_cfg(&boardp->dvc_cfg.adv_dvc_cfg);
074c8fe4 2531 }
1da177e4
LT
2532}
2533
51219358
MW
2534/*
2535 * asc_prt_hex()
2536 *
2537 * Print hexadecimal output in 4 byte groupings 32 bytes
2538 * or 8 double-words per line.
2539 */
2540static void asc_prt_hex(char *f, uchar *s, int l)
47d853cc 2541{
51219358
MW
2542 int i;
2543 int j;
2544 int k;
2545 int m;
47d853cc 2546
51219358
MW
2547 printk("%s: (%d bytes)\n", f, l);
2548
2549 for (i = 0; i < l; i += 32) {
2550
2551 /* Display a maximum of 8 double-words per line. */
2552 if ((k = (l - i) / 4) >= 8) {
2553 k = 8;
2554 m = 0;
47d853cc 2555 } else {
51219358 2556 m = (l - i) % 4;
47d853cc
MW
2557 }
2558
51219358
MW
2559 for (j = 0; j < k; j++) {
2560 printk(" %2.2X%2.2X%2.2X%2.2X",
2561 (unsigned)s[i + (j * 4)],
2562 (unsigned)s[i + (j * 4) + 1],
2563 (unsigned)s[i + (j * 4) + 2],
2564 (unsigned)s[i + (j * 4) + 3]);
47d853cc 2565 }
47d853cc 2566
51219358
MW
2567 switch (m) {
2568 case 0:
2569 default:
2570 break;
2571 case 1:
2572 printk(" %2.2X", (unsigned)s[i + (j * 4)]);
2573 break;
2574 case 2:
2575 printk(" %2.2X%2.2X",
2576 (unsigned)s[i + (j * 4)],
2577 (unsigned)s[i + (j * 4) + 1]);
2578 break;
2579 case 3:
2580 printk(" %2.2X%2.2X%2.2X",
2581 (unsigned)s[i + (j * 4) + 1],
2582 (unsigned)s[i + (j * 4) + 2],
2583 (unsigned)s[i + (j * 4) + 3]);
2584 break;
2585 }
47d853cc 2586
51219358 2587 printk("\n");
47d853cc
MW
2588 }
2589}
b352f923
MW
2590
2591/*
2592 * asc_prt_asc_scsi_q()
2593 */
2594static void asc_prt_asc_scsi_q(ASC_SCSI_Q *q)
2595{
2596 ASC_SG_HEAD *sgp;
2597 int i;
2598
2599 printk("ASC_SCSI_Q at addr 0x%lx\n", (ulong)q);
2600
2601 printk
9c17c62a
HR
2602 (" target_ix 0x%x, target_lun %u, srb_tag 0x%x, tag_code 0x%x,\n",
2603 q->q2.target_ix, q->q1.target_lun, q->q2.srb_tag,
b352f923
MW
2604 q->q2.tag_code);
2605
2606 printk
2607 (" data_addr 0x%lx, data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2608 (ulong)le32_to_cpu(q->q1.data_addr),
2609 (ulong)le32_to_cpu(q->q1.data_cnt),
2610 (ulong)le32_to_cpu(q->q1.sense_addr), q->q1.sense_len);
2611
2612 printk(" cdbptr 0x%lx, cdb_len %u, sg_head 0x%lx, sg_queue_cnt %u\n",
2613 (ulong)q->cdbptr, q->q2.cdb_len,
2614 (ulong)q->sg_head, q->q1.sg_queue_cnt);
2615
2616 if (q->sg_head) {
2617 sgp = q->sg_head;
2618 printk("ASC_SG_HEAD at addr 0x%lx\n", (ulong)sgp);
2619 printk(" entry_cnt %u, queue_cnt %u\n", sgp->entry_cnt,
2620 sgp->queue_cnt);
2621 for (i = 0; i < sgp->entry_cnt; i++) {
2622 printk(" [%u]: addr 0x%lx, bytes %lu\n",
2623 i, (ulong)le32_to_cpu(sgp->sg_list[i].addr),
2624 (ulong)le32_to_cpu(sgp->sg_list[i].bytes));
2625 }
2626
2627 }
2628}
2629
2630/*
2631 * asc_prt_asc_qdone_info()
2632 */
2633static void asc_prt_asc_qdone_info(ASC_QDONE_INFO *q)
2634{
2635 printk("ASC_QDONE_INFO at addr 0x%lx\n", (ulong)q);
9c17c62a
HR
2636 printk(" srb_tag 0x%x, target_ix %u, cdb_len %u, tag_code %u,\n",
2637 q->d2.srb_tag, q->d2.target_ix, q->d2.cdb_len,
b352f923
MW
2638 q->d2.tag_code);
2639 printk
2640 (" done_stat 0x%x, host_stat 0x%x, scsi_stat 0x%x, scsi_msg 0x%x\n",
2641 q->d3.done_stat, q->d3.host_stat, q->d3.scsi_stat, q->d3.scsi_msg);
2642}
2643
2644/*
2645 * asc_prt_adv_sgblock()
2646 *
2647 * Display an ADV_SG_BLOCK structure.
2648 */
2649static void asc_prt_adv_sgblock(int sgblockno, ADV_SG_BLOCK *b)
2650{
2651 int i;
2652
0ce53822 2653 printk(" ADV_SG_BLOCK at addr 0x%lx (sgblockno %d)\n",
b352f923
MW
2654 (ulong)b, sgblockno);
2655 printk(" sg_cnt %u, sg_ptr 0x%lx\n",
2656 b->sg_cnt, (ulong)le32_to_cpu(b->sg_ptr));
2657 BUG_ON(b->sg_cnt > NO_OF_SG_PER_BLOCK);
2658 if (b->sg_ptr != 0)
2659 BUG_ON(b->sg_cnt != NO_OF_SG_PER_BLOCK);
2660 for (i = 0; i < b->sg_cnt; i++) {
2661 printk(" [%u]: sg_addr 0x%lx, sg_count 0x%lx\n",
2662 i, (ulong)b->sg_list[i].sg_addr,
2663 (ulong)b->sg_list[i].sg_count);
2664 }
2665}
2666
2667/*
2668 * asc_prt_adv_scsi_req_q()
2669 *
2670 * Display an ADV_SCSI_REQ_Q structure.
2671 */
2672static void asc_prt_adv_scsi_req_q(ADV_SCSI_REQ_Q *q)
2673{
2674 int sg_blk_cnt;
0ce53822
HR
2675 struct adv_sg_block *sg_ptr;
2676 adv_sgblk_t *sgblkp;
b352f923
MW
2677
2678 printk("ADV_SCSI_REQ_Q at addr 0x%lx\n", (ulong)q);
2679
9c17c62a
HR
2680 printk(" target_id %u, target_lun %u, srb_tag 0x%x, a_flag 0x%x\n",
2681 q->target_id, q->target_lun, q->srb_tag, q->a_flag);
b352f923 2682
98b96a7d
HR
2683 printk(" cntl 0x%x, data_addr 0x%lx\n",
2684 q->cntl, (ulong)le32_to_cpu(q->data_addr));
b352f923
MW
2685
2686 printk(" data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2687 (ulong)le32_to_cpu(q->data_cnt),
2688 (ulong)le32_to_cpu(q->sense_addr), q->sense_len);
2689
2690 printk
2691 (" cdb_len %u, done_status 0x%x, host_status 0x%x, scsi_status 0x%x\n",
2692 q->cdb_len, q->done_status, q->host_status, q->scsi_status);
2693
2694 printk(" sg_working_ix 0x%x, target_cmd %u\n",
2695 q->sg_working_ix, q->target_cmd);
2696
2697 printk(" scsiq_rptr 0x%lx, sg_real_addr 0x%lx, sg_list_ptr 0x%lx\n",
2698 (ulong)le32_to_cpu(q->scsiq_rptr),
2699 (ulong)le32_to_cpu(q->sg_real_addr), (ulong)q->sg_list_ptr);
2700
2701 /* Display the request's ADV_SG_BLOCK structures. */
2702 if (q->sg_list_ptr != NULL) {
0ce53822 2703 sgblkp = container_of(q->sg_list_ptr, adv_sgblk_t, sg_block);
b352f923 2704 sg_blk_cnt = 0;
0ce53822
HR
2705 while (sgblkp) {
2706 sg_ptr = &sgblkp->sg_block;
b352f923
MW
2707 asc_prt_adv_sgblock(sg_blk_cnt, sg_ptr);
2708 if (sg_ptr->sg_ptr == 0) {
2709 break;
2710 }
0ce53822 2711 sgblkp = sgblkp->next_sgblkp;
b352f923
MW
2712 sg_blk_cnt++;
2713 }
2714 }
2715}
51219358 2716#endif /* ADVANSYS_DEBUG */
47d853cc 2717
1da177e4 2718/*
51219358 2719 * advansys_info()
47d853cc 2720 *
51219358
MW
2721 * Return suitable for printing on the console with the argument
2722 * adapter's configuration information.
2723 *
2724 * Note: The information line should not exceed ASC_INFO_SIZE bytes,
2725 * otherwise the static 'info' array will be overrun.
1da177e4 2726 */
51219358 2727static const char *advansys_info(struct Scsi_Host *shost)
1da177e4 2728{
51219358 2729 static char info[ASC_INFO_SIZE];
d2411495 2730 struct asc_board *boardp = shost_priv(shost);
51219358
MW
2731 ASC_DVC_VAR *asc_dvc_varp;
2732 ADV_DVC_VAR *adv_dvc_varp;
2733 char *busname;
2734 char *widename = NULL;
1da177e4 2735
51219358
MW
2736 if (ASC_NARROW_BOARD(boardp)) {
2737 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
b352f923 2738 ASC_DBG(1, "begin\n");
51219358
MW
2739 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
2740 if ((asc_dvc_varp->bus_type & ASC_IS_ISAPNP) ==
2741 ASC_IS_ISAPNP) {
2742 busname = "ISA PnP";
2743 } else {
2744 busname = "ISA";
2745 }
2746 sprintf(info,
2747 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X, DMA 0x%X",
2748 ASC_VERSION, busname,
2749 (ulong)shost->io_port,
2750 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
d361db48 2751 boardp->irq, shost->dma_channel);
51219358
MW
2752 } else {
2753 if (asc_dvc_varp->bus_type & ASC_IS_VL) {
2754 busname = "VL";
2755 } else if (asc_dvc_varp->bus_type & ASC_IS_EISA) {
2756 busname = "EISA";
2757 } else if (asc_dvc_varp->bus_type & ASC_IS_PCI) {
2758 if ((asc_dvc_varp->bus_type & ASC_IS_PCI_ULTRA)
2759 == ASC_IS_PCI_ULTRA) {
2760 busname = "PCI Ultra";
2761 } else {
2762 busname = "PCI";
2763 }
2764 } else {
2765 busname = "?";
9d0e96eb
MW
2766 shost_printk(KERN_ERR, shost, "unknown bus "
2767 "type %d\n", asc_dvc_varp->bus_type);
51219358
MW
2768 }
2769 sprintf(info,
2770 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X",
2771 ASC_VERSION, busname, (ulong)shost->io_port,
2772 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
d361db48 2773 boardp->irq);
51219358
MW
2774 }
2775 } else {
2776 /*
2777 * Wide Adapter Information
2778 *
2779 * Memory-mapped I/O is used instead of I/O space to access
2780 * the adapter, but display the I/O Port range. The Memory
2781 * I/O address is displayed through the driver /proc file.
2782 */
2783 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
2784 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
2785 widename = "Ultra-Wide";
2786 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
2787 widename = "Ultra2-Wide";
2788 } else {
2789 widename = "Ultra3-Wide";
2790 }
2791 sprintf(info,
2792 "AdvanSys SCSI %s: PCI %s: PCIMEM 0x%lX-0x%lX, IRQ 0x%X",
2793 ASC_VERSION, widename, (ulong)adv_dvc_varp->iop_base,
d361db48 2794 (ulong)adv_dvc_varp->iop_base + boardp->asc_n_io_port - 1, boardp->irq);
51219358
MW
2795 }
2796 BUG_ON(strlen(info) >= ASC_INFO_SIZE);
b352f923 2797 ASC_DBG(1, "end\n");
51219358 2798 return info;
47d853cc
MW
2799}
2800
51219358 2801#ifdef CONFIG_PROC_FS
47d853cc
MW
2802
2803/*
51219358 2804 * asc_prt_board_devices()
47d853cc 2805 *
51219358 2806 * Print driver information for devices attached to the board.
47d853cc 2807 */
b59fb6fd 2808static void asc_prt_board_devices(struct seq_file *m, struct Scsi_Host *shost)
47d853cc 2809{
d2411495 2810 struct asc_board *boardp = shost_priv(shost);
51219358
MW
2811 int chip_scsi_id;
2812 int i;
47d853cc 2813
b59fb6fd
AV
2814 seq_printf(m,
2815 "\nDevice Information for AdvanSys SCSI Host %d:\n",
2816 shost->host_no);
47d853cc 2817
51219358
MW
2818 if (ASC_NARROW_BOARD(boardp)) {
2819 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
2820 } else {
2821 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
47d853cc
MW
2822 }
2823
2f979427 2824 seq_puts(m, "Target IDs Detected:");
51219358 2825 for (i = 0; i <= ADV_MAX_TID; i++) {
b59fb6fd
AV
2826 if (boardp->init_tidmask & ADV_TID_TO_TIDMASK(i))
2827 seq_printf(m, " %X,", i);
27c868c2 2828 }
b59fb6fd 2829 seq_printf(m, " (%X=Host Adapter)\n", chip_scsi_id);
47d853cc
MW
2830}
2831
2832/*
51219358 2833 * Display Wide Board BIOS Information.
47d853cc 2834 */
b59fb6fd 2835static void asc_prt_adv_bios(struct seq_file *m, struct Scsi_Host *shost)
47d853cc 2836{
d2411495 2837 struct asc_board *boardp = shost_priv(shost);
51219358
MW
2838 ushort major, minor, letter;
2839
2f979427 2840 seq_puts(m, "\nROM BIOS Version: ");
47d853cc
MW
2841
2842 /*
51219358
MW
2843 * If the BIOS saved a valid signature, then fill in
2844 * the BIOS code segment base address.
47d853cc 2845 */
51219358 2846 if (boardp->bios_signature != 0x55AA) {
3d30079c
RV
2847 seq_puts(m, "Disabled or Pre-3.1\n"
2848 "BIOS either disabled or Pre-3.1. If it is pre-3.1, then a newer version\n"
2849 "can be found at the ConnectCom FTP site: ftp://ftp.connectcom.net/pub\n");
51219358
MW
2850 } else {
2851 major = (boardp->bios_version >> 12) & 0xF;
2852 minor = (boardp->bios_version >> 8) & 0xF;
2853 letter = (boardp->bios_version & 0xFF);
47d853cc 2854
b59fb6fd 2855 seq_printf(m, "%d.%d%c\n",
51219358
MW
2856 major, minor,
2857 letter >= 26 ? '?' : letter + 'A');
51219358
MW
2858 /*
2859 * Current available ROM BIOS release is 3.1I for UW
2860 * and 3.2I for U2W. This code doesn't differentiate
2861 * UW and U2W boards.
2862 */
2863 if (major < 3 || (major <= 3 && minor < 1) ||
2864 (major <= 3 && minor <= 1 && letter < ('I' - 'A'))) {
3d30079c
RV
2865 seq_puts(m, "Newer version of ROM BIOS is available at the ConnectCom FTP site:\n"
2866 "ftp://ftp.connectcom.net/pub\n");
51219358
MW
2867 }
2868 }
1da177e4
LT
2869}
2870
1da177e4 2871/*
51219358
MW
2872 * Add serial number to information bar if signature AAh
2873 * is found in at bit 15-9 (7 bits) of word 1.
1da177e4 2874 *
51219358 2875 * Serial Number consists fo 12 alpha-numeric digits.
1da177e4 2876 *
51219358
MW
2877 * 1 - Product type (A,B,C,D..) Word0: 15-13 (3 bits)
2878 * 2 - MFG Location (A,B,C,D..) Word0: 12-10 (3 bits)
2879 * 3-4 - Product ID (0-99) Word0: 9-0 (10 bits)
2880 * 5 - Product revision (A-J) Word0: " "
1da177e4 2881 *
51219358
MW
2882 * Signature Word1: 15-9 (7 bits)
2883 * 6 - Year (0-9) Word1: 8-6 (3 bits) & Word2: 15 (1 bit)
2884 * 7-8 - Week of the year (1-52) Word1: 5-0 (6 bits)
1da177e4 2885 *
51219358 2886 * 9-12 - Serial Number (A001-Z999) Word2: 14-0 (15 bits)
1da177e4 2887 *
51219358 2888 * Note 1: Only production cards will have a serial number.
1da177e4 2889 *
51219358 2890 * Note 2: Signature is most significant 7 bits (0xFE).
1da177e4 2891 *
51219358 2892 * Returns ASC_TRUE if serial number found, otherwise returns ASC_FALSE.
1da177e4 2893 */
51219358 2894static int asc_get_eeprom_string(ushort *serialnum, uchar *cp)
1da177e4 2895{
51219358 2896 ushort w, num;
27c868c2 2897
51219358
MW
2898 if ((serialnum[1] & 0xFE00) != ((ushort)0xAA << 8)) {
2899 return ASC_FALSE;
2900 } else {
2901 /*
2902 * First word - 6 digits.
2903 */
2904 w = serialnum[0];
27c868c2 2905
51219358
MW
2906 /* Product type - 1st digit. */
2907 if ((*cp = 'A' + ((w & 0xE000) >> 13)) == 'H') {
2908 /* Product type is P=Prototype */
2909 *cp += 0x8;
2910 }
2911 cp++;
2912
2913 /* Manufacturing location - 2nd digit. */
2914 *cp++ = 'A' + ((w & 0x1C00) >> 10);
2915
2916 /* Product ID - 3rd, 4th digits. */
2917 num = w & 0x3FF;
2918 *cp++ = '0' + (num / 100);
2919 num %= 100;
2920 *cp++ = '0' + (num / 10);
2921
2922 /* Product revision - 5th digit. */
2923 *cp++ = 'A' + (num % 10);
27c868c2 2924
27c868c2 2925 /*
51219358 2926 * Second word
27c868c2 2927 */
51219358 2928 w = serialnum[1];
27c868c2
MW
2929
2930 /*
51219358 2931 * Year - 6th digit.
27c868c2 2932 *
51219358
MW
2933 * If bit 15 of third word is set, then the
2934 * last digit of the year is greater than 7.
27c868c2 2935 */
51219358
MW
2936 if (serialnum[2] & 0x8000) {
2937 *cp++ = '8' + ((w & 0x1C0) >> 6);
2938 } else {
2939 *cp++ = '0' + ((w & 0x1C0) >> 6);
27c868c2
MW
2940 }
2941
51219358
MW
2942 /* Week of year - 7th, 8th digits. */
2943 num = w & 0x003F;
2944 *cp++ = '0' + num / 10;
2945 num %= 10;
2946 *cp++ = '0' + num;
27c868c2
MW
2947
2948 /*
51219358 2949 * Third word
27c868c2 2950 */
51219358 2951 w = serialnum[2] & 0x7FFF;
1da177e4 2952
51219358
MW
2953 /* Serial number - 9th digit. */
2954 *cp++ = 'A' + (w / 1000);
27c868c2 2955
51219358
MW
2956 /* 10th, 11th, 12th digits. */
2957 num = w % 1000;
2958 *cp++ = '0' + num / 100;
2959 num %= 100;
2960 *cp++ = '0' + num / 10;
2961 num %= 10;
2962 *cp++ = '0' + num;
2963
2964 *cp = '\0'; /* Null Terminate the string. */
2965 return ASC_TRUE;
2966 }
1da177e4
LT
2967}
2968
2969/*
51219358 2970 * asc_prt_asc_board_eeprom()
1da177e4 2971 *
51219358 2972 * Print board EEPROM configuration.
1da177e4 2973 */
b59fb6fd 2974static void asc_prt_asc_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 2975{
d2411495 2976 struct asc_board *boardp = shost_priv(shost);
51219358 2977 ASC_DVC_VAR *asc_dvc_varp;
51219358
MW
2978 ASCEEP_CONFIG *ep;
2979 int i;
2980#ifdef CONFIG_ISA
2981 int isa_dma_speed[] = { 10, 8, 7, 6, 5, 4, 3, 2 };
2982#endif /* CONFIG_ISA */
2983 uchar serialstr[13];
27c868c2 2984
51219358
MW
2985 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
2986 ep = &boardp->eep_config.asc_eep;
27c868c2 2987
b59fb6fd
AV
2988 seq_printf(m,
2989 "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
2990 shost->host_no);
1da177e4 2991
51219358 2992 if (asc_get_eeprom_string((ushort *)&ep->adapter_info[0], serialstr)
b59fb6fd
AV
2993 == ASC_TRUE)
2994 seq_printf(m, " Serial Number: %s\n", serialstr);
2995 else if (ep->adapter_info[5] == 0xBB)
2f979427
RV
2996 seq_puts(m,
2997 " Default Settings Used for EEPROM-less Adapter.\n");
b59fb6fd 2998 else
2f979427 2999 seq_puts(m, " Serial Number Signature Not Present.\n");
b59fb6fd
AV
3000
3001 seq_printf(m,
3002 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3003 ASC_EEP_GET_CHIP_ID(ep), ep->max_total_qng,
3004 ep->max_tag_qng);
3005
3006 seq_printf(m,
3007 " cntl 0x%x, no_scam 0x%x\n", ep->cntl, ep->no_scam);
3008
2f979427 3009 seq_puts(m, " Target ID: ");
b59fb6fd
AV
3010 for (i = 0; i <= ASC_MAX_TID; i++)
3011 seq_printf(m, " %d", i);
b59fb6fd 3012
3d30079c 3013 seq_puts(m, "\n Disconnects: ");
b59fb6fd
AV
3014 for (i = 0; i <= ASC_MAX_TID; i++)
3015 seq_printf(m, " %c",
3016 (ep->disc_enable & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
b59fb6fd 3017
3d30079c 3018 seq_puts(m, "\n Command Queuing: ");
b59fb6fd
AV
3019 for (i = 0; i <= ASC_MAX_TID; i++)
3020 seq_printf(m, " %c",
3021 (ep->use_cmd_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
b59fb6fd 3022
3d30079c 3023 seq_puts(m, "\n Start Motor: ");
b59fb6fd
AV
3024 for (i = 0; i <= ASC_MAX_TID; i++)
3025 seq_printf(m, " %c",
3026 (ep->start_motor & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
b59fb6fd 3027
3d30079c 3028 seq_puts(m, "\n Synchronous Transfer:");
b59fb6fd
AV
3029 for (i = 0; i <= ASC_MAX_TID; i++)
3030 seq_printf(m, " %c",
3031 (ep->init_sdtr & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
f50332ff 3032 seq_putc(m, '\n');
51219358
MW
3033
3034#ifdef CONFIG_ISA
3035 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
b59fb6fd
AV
3036 seq_printf(m,
3037 " Host ISA DMA speed: %d MB/S\n",
3038 isa_dma_speed[ASC_EEP_GET_DMA_SPD(ep)]);
51219358
MW
3039 }
3040#endif /* CONFIG_ISA */
1da177e4
LT
3041}
3042
3043/*
51219358 3044 * asc_prt_adv_board_eeprom()
1da177e4 3045 *
51219358 3046 * Print board EEPROM configuration.
1da177e4 3047 */
b59fb6fd 3048static void asc_prt_adv_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 3049{
d2411495 3050 struct asc_board *boardp = shost_priv(shost);
51219358 3051 ADV_DVC_VAR *adv_dvc_varp;
27c868c2 3052 int i;
51219358
MW
3053 char *termstr;
3054 uchar serialstr[13];
3055 ADVEEP_3550_CONFIG *ep_3550 = NULL;
3056 ADVEEP_38C0800_CONFIG *ep_38C0800 = NULL;
3057 ADVEEP_38C1600_CONFIG *ep_38C1600 = NULL;
3058 ushort word;
3059 ushort *wordp;
3060 ushort sdtr_speed = 0;
27c868c2 3061
51219358
MW
3062 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
3063 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3064 ep_3550 = &boardp->eep_config.adv_3550_eep;
3065 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3066 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
27c868c2 3067 } else {
51219358 3068 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
27c868c2 3069 }
1da177e4 3070
b59fb6fd
AV
3071 seq_printf(m,
3072 "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
3073 shost->host_no);
27c868c2 3074
51219358
MW
3075 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3076 wordp = &ep_3550->serial_number_word1;
3077 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3078 wordp = &ep_38C0800->serial_number_word1;
3079 } else {
3080 wordp = &ep_38C1600->serial_number_word1;
3081 }
27c868c2 3082
b59fb6fd
AV
3083 if (asc_get_eeprom_string(wordp, serialstr) == ASC_TRUE)
3084 seq_printf(m, " Serial Number: %s\n", serialstr);
3085 else
2f979427 3086 seq_puts(m, " Serial Number Signature Not Present.\n");
27c868c2 3087
b59fb6fd
AV
3088 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3089 seq_printf(m,
3090 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3091 ep_3550->adapter_scsi_id,
3092 ep_3550->max_host_qng, ep_3550->max_dvc_qng);
3093 else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3094 seq_printf(m,
3095 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3096 ep_38C0800->adapter_scsi_id,
3097 ep_38C0800->max_host_qng,
3098 ep_38C0800->max_dvc_qng);
3099 else
3100 seq_printf(m,
3101 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3102 ep_38C1600->adapter_scsi_id,
3103 ep_38C1600->max_host_qng,
3104 ep_38C1600->max_dvc_qng);
51219358
MW
3105 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3106 word = ep_3550->termination;
3107 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3108 word = ep_38C0800->termination_lvd;
3109 } else {
3110 word = ep_38C1600->termination_lvd;
3111 }
3112 switch (word) {
3113 case 1:
3114 termstr = "Low Off/High Off";
3115 break;
3116 case 2:
3117 termstr = "Low Off/High On";
3118 break;
3119 case 3:
3120 termstr = "Low On/High On";
3121 break;
3122 default:
3123 case 0:
3124 termstr = "Automatic";
3125 break;
27c868c2 3126 }
1da177e4 3127
b59fb6fd
AV
3128 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3129 seq_printf(m,
3130 " termination: %u (%s), bios_ctrl: 0x%x\n",
3131 ep_3550->termination, termstr,
3132 ep_3550->bios_ctrl);
3133 else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3134 seq_printf(m,
3135 " termination: %u (%s), bios_ctrl: 0x%x\n",
3136 ep_38C0800->termination_lvd, termstr,
3137 ep_38C0800->bios_ctrl);
3138 else
3139 seq_printf(m,
3140 " termination: %u (%s), bios_ctrl: 0x%x\n",
3141 ep_38C1600->termination_lvd, termstr,
3142 ep_38C1600->bios_ctrl);
1da177e4 3143
2f979427 3144 seq_puts(m, " Target ID: ");
b59fb6fd
AV
3145 for (i = 0; i <= ADV_MAX_TID; i++)
3146 seq_printf(m, " %X", i);
f50332ff 3147 seq_putc(m, '\n');
1da177e4 3148
51219358
MW
3149 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3150 word = ep_3550->disc_enable;
3151 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3152 word = ep_38C0800->disc_enable;
3153 } else {
3154 word = ep_38C1600->disc_enable;
3155 }
2f979427 3156 seq_puts(m, " Disconnects: ");
b59fb6fd
AV
3157 for (i = 0; i <= ADV_MAX_TID; i++)
3158 seq_printf(m, " %c",
3159 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
f50332ff 3160 seq_putc(m, '\n');
1da177e4 3161
51219358
MW
3162 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3163 word = ep_3550->tagqng_able;
3164 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3165 word = ep_38C0800->tagqng_able;
3166 } else {
3167 word = ep_38C1600->tagqng_able;
3168 }
2f979427 3169 seq_puts(m, " Command Queuing: ");
b59fb6fd
AV
3170 for (i = 0; i <= ADV_MAX_TID; i++)
3171 seq_printf(m, " %c",
3172 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
f50332ff 3173 seq_putc(m, '\n');
1da177e4 3174
51219358
MW
3175 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3176 word = ep_3550->start_motor;
3177 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3178 word = ep_38C0800->start_motor;
27c868c2 3179 } else {
51219358
MW
3180 word = ep_38C1600->start_motor;
3181 }
2f979427 3182 seq_puts(m, " Start Motor: ");
b59fb6fd
AV
3183 for (i = 0; i <= ADV_MAX_TID; i++)
3184 seq_printf(m, " %c",
3185 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
f50332ff 3186 seq_putc(m, '\n');
27c868c2 3187
51219358 3188 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
2f979427 3189 seq_puts(m, " Synchronous Transfer:");
b59fb6fd
AV
3190 for (i = 0; i <= ADV_MAX_TID; i++)
3191 seq_printf(m, " %c",
3192 (ep_3550->sdtr_able & ADV_TID_TO_TIDMASK(i)) ?
3193 'Y' : 'N');
f50332ff 3194 seq_putc(m, '\n');
51219358 3195 }
27c868c2 3196
51219358 3197 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
2f979427 3198 seq_puts(m, " Ultra Transfer: ");
b59fb6fd
AV
3199 for (i = 0; i <= ADV_MAX_TID; i++)
3200 seq_printf(m, " %c",
3201 (ep_3550->ultra_able & ADV_TID_TO_TIDMASK(i))
3202 ? 'Y' : 'N');
f50332ff 3203 seq_putc(m, '\n');
51219358 3204 }
27c868c2 3205
51219358
MW
3206 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3207 word = ep_3550->wdtr_able;
3208 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3209 word = ep_38C0800->wdtr_able;
3210 } else {
3211 word = ep_38C1600->wdtr_able;
3212 }
2f979427 3213 seq_puts(m, " Wide Transfer: ");
b59fb6fd
AV
3214 for (i = 0; i <= ADV_MAX_TID; i++)
3215 seq_printf(m, " %c",
3216 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
f50332ff 3217 seq_putc(m, '\n');
1da177e4 3218
51219358
MW
3219 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800 ||
3220 adv_dvc_varp->chip_type == ADV_CHIP_ASC38C1600) {
2f979427 3221 seq_puts(m, " Synchronous Transfer Speed (Mhz):\n ");
51219358
MW
3222 for (i = 0; i <= ADV_MAX_TID; i++) {
3223 char *speed_str;
1da177e4 3224
51219358
MW
3225 if (i == 0) {
3226 sdtr_speed = adv_dvc_varp->sdtr_speed1;
3227 } else if (i == 4) {
3228 sdtr_speed = adv_dvc_varp->sdtr_speed2;
3229 } else if (i == 8) {
3230 sdtr_speed = adv_dvc_varp->sdtr_speed3;
3231 } else if (i == 12) {
3232 sdtr_speed = adv_dvc_varp->sdtr_speed4;
3233 }
3234 switch (sdtr_speed & ADV_MAX_TID) {
3235 case 0:
3236 speed_str = "Off";
3237 break;
3238 case 1:
3239 speed_str = " 5";
3240 break;
3241 case 2:
3242 speed_str = " 10";
3243 break;
3244 case 3:
3245 speed_str = " 20";
3246 break;
3247 case 4:
3248 speed_str = " 40";
3249 break;
3250 case 5:
3251 speed_str = " 80";
3252 break;
3253 default:
3254 speed_str = "Unk";
3255 break;
3256 }
b59fb6fd
AV
3257 seq_printf(m, "%X:%s ", i, speed_str);
3258 if (i == 7)
2f979427 3259 seq_puts(m, "\n ");
51219358
MW
3260 sdtr_speed >>= 4;
3261 }
f50332ff 3262 seq_putc(m, '\n');
51219358 3263 }
1da177e4
LT
3264}
3265
3266/*
51219358 3267 * asc_prt_driver_conf()
1da177e4 3268 */
b59fb6fd 3269static void asc_prt_driver_conf(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 3270{
d2411495 3271 struct asc_board *boardp = shost_priv(shost);
51219358 3272 int chip_scsi_id;
27c868c2 3273
b59fb6fd
AV
3274 seq_printf(m,
3275 "\nLinux Driver Configuration and Information for AdvanSys SCSI Host %d:\n",
3276 shost->host_no);
27c868c2 3277
b59fb6fd 3278 seq_printf(m,
1abf635d 3279 " host_busy %u, max_id %u, max_lun %llu, max_channel %u\n",
74665016 3280 atomic_read(&shost->host_busy), shost->max_id,
b59fb6fd 3281 shost->max_lun, shost->max_channel);
95c9f162 3282
b59fb6fd
AV
3283 seq_printf(m,
3284 " unique_id %d, can_queue %d, this_id %d, sg_tablesize %u, cmd_per_lun %u\n",
3285 shost->unique_id, shost->can_queue, shost->this_id,
3286 shost->sg_tablesize, shost->cmd_per_lun);
95c9f162 3287
b59fb6fd
AV
3288 seq_printf(m,
3289 " unchecked_isa_dma %d, use_clustering %d\n",
3290 shost->unchecked_isa_dma, shost->use_clustering);
95c9f162 3291
b59fb6fd 3292 seq_printf(m,
31491e1a 3293 " flags 0x%x, last_reset 0x%lx, jiffies 0x%lx, asc_n_io_port 0x%x\n",
eac0b0c7 3294 boardp->flags, shost->last_reset, jiffies,
b59fb6fd 3295 boardp->asc_n_io_port);
27c868c2 3296
31491e1a 3297 seq_printf(m, " io_port 0x%lx\n", shost->io_port);
27c868c2 3298
51219358
MW
3299 if (ASC_NARROW_BOARD(boardp)) {
3300 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
3301 } else {
3302 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
27c868c2 3303 }
1da177e4
LT
3304}
3305
3306/*
51219358 3307 * asc_prt_asc_board_info()
1da177e4 3308 *
51219358 3309 * Print dynamic board configuration information.
1da177e4 3310 */
b59fb6fd 3311static void asc_prt_asc_board_info(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 3312{
d2411495 3313 struct asc_board *boardp = shost_priv(shost);
51219358 3314 int chip_scsi_id;
51219358
MW
3315 ASC_DVC_VAR *v;
3316 ASC_DVC_CFG *c;
3317 int i;
3318 int renegotiate = 0;
27c868c2 3319
51219358
MW
3320 v = &boardp->dvc_var.asc_dvc_var;
3321 c = &boardp->dvc_cfg.asc_dvc_cfg;
3322 chip_scsi_id = c->chip_scsi_id;
27c868c2 3323
b59fb6fd
AV
3324 seq_printf(m,
3325 "\nAsc Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3326 shost->host_no);
27c868c2 3327
b59fb6fd
AV
3328 seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3329 "mcode_version 0x%x, err_code %u\n",
3330 c->chip_version, c->mcode_date, c->mcode_version,
3331 v->err_code);
1da177e4 3332
51219358 3333 /* Current number of commands waiting for the host. */
b59fb6fd
AV
3334 seq_printf(m,
3335 " Total Command Pending: %d\n", v->cur_total_qng);
1da177e4 3336
2f979427 3337 seq_puts(m, " Command Queuing:");
51219358
MW
3338 for (i = 0; i <= ASC_MAX_TID; i++) {
3339 if ((chip_scsi_id == i) ||
3340 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3341 continue;
27c868c2 3342 }
b59fb6fd
AV
3343 seq_printf(m, " %X:%c",
3344 i,
3345 (v->use_tagged_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
51219358 3346 }
27c868c2 3347
51219358 3348 /* Current number of commands waiting for a device. */
3d30079c 3349 seq_puts(m, "\n Command Queue Pending:");
51219358
MW
3350 for (i = 0; i <= ASC_MAX_TID; i++) {
3351 if ((chip_scsi_id == i) ||
3352 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3353 continue;
27c868c2 3354 }
b59fb6fd 3355 seq_printf(m, " %X:%u", i, v->cur_dvc_qng[i]);
27c868c2 3356 }
1da177e4 3357
51219358 3358 /* Current limit on number of commands that can be sent to a device. */
3d30079c 3359 seq_puts(m, "\n Command Queue Limit:");
51219358
MW
3360 for (i = 0; i <= ASC_MAX_TID; i++) {
3361 if ((chip_scsi_id == i) ||
3362 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3363 continue;
3364 }
b59fb6fd 3365 seq_printf(m, " %X:%u", i, v->max_dvc_qng[i]);
27c868c2 3366 }
1da177e4 3367
51219358 3368 /* Indicate whether the device has returned queue full status. */
3d30079c 3369 seq_puts(m, "\n Command Queue Full:");
51219358
MW
3370 for (i = 0; i <= ASC_MAX_TID; i++) {
3371 if ((chip_scsi_id == i) ||
3372 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3373 continue;
3374 }
b59fb6fd
AV
3375 if (boardp->queue_full & ADV_TID_TO_TIDMASK(i))
3376 seq_printf(m, " %X:Y-%d",
3377 i, boardp->queue_full_cnt[i]);
3378 else
3379 seq_printf(m, " %X:N", i);
51219358 3380 }
1da177e4 3381
3d30079c 3382 seq_puts(m, "\n Synchronous Transfer:");
51219358
MW
3383 for (i = 0; i <= ASC_MAX_TID; i++) {
3384 if ((chip_scsi_id == i) ||
3385 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3386 continue;
3387 }
b59fb6fd
AV
3388 seq_printf(m, " %X:%c",
3389 i,
3390 (v->sdtr_done & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
27c868c2 3391 }
f50332ff 3392 seq_putc(m, '\n');
1da177e4 3393
51219358
MW
3394 for (i = 0; i <= ASC_MAX_TID; i++) {
3395 uchar syn_period_ix;
1da177e4 3396
51219358
MW
3397 if ((chip_scsi_id == i) ||
3398 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3399 ((v->init_sdtr & ADV_TID_TO_TIDMASK(i)) == 0)) {
3400 continue;
27c868c2 3401 }
27c868c2 3402
b59fb6fd 3403 seq_printf(m, " %X:", i);
27c868c2 3404
51219358 3405 if ((boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET) == 0) {
2f979427 3406 seq_puts(m, " Asynchronous");
51219358
MW
3407 } else {
3408 syn_period_ix =
3409 (boardp->sdtr_data[i] >> 4) & (v->max_sdtr_index -
3410 1);
27c868c2 3411
b59fb6fd
AV
3412 seq_printf(m,
3413 " Transfer Period Factor: %d (%d.%d Mhz),",
3414 v->sdtr_period_tbl[syn_period_ix],
3415 250 / v->sdtr_period_tbl[syn_period_ix],
3416 ASC_TENTHS(250,
3417 v->sdtr_period_tbl[syn_period_ix]));
27c868c2 3418
b59fb6fd
AV
3419 seq_printf(m, " REQ/ACK Offset: %d",
3420 boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET);
51219358 3421 }
1da177e4 3422
51219358 3423 if ((v->sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
2f979427 3424 seq_puts(m, "*\n");
51219358
MW
3425 renegotiate = 1;
3426 } else {
f50332ff 3427 seq_putc(m, '\n');
51219358 3428 }
27c868c2 3429 }
1da177e4 3430
51219358 3431 if (renegotiate) {
2f979427 3432 seq_puts(m, " * = Re-negotiation pending before next command.\n");
27c868c2 3433 }
1da177e4
LT
3434}
3435
1da177e4 3436/*
51219358 3437 * asc_prt_adv_board_info()
1da177e4 3438 *
51219358 3439 * Print dynamic board configuration information.
1da177e4 3440 */
b59fb6fd 3441static void asc_prt_adv_board_info(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 3442{
d2411495 3443 struct asc_board *boardp = shost_priv(shost);
27c868c2 3444 int i;
51219358
MW
3445 ADV_DVC_VAR *v;
3446 ADV_DVC_CFG *c;
3447 AdvPortAddr iop_base;
3448 ushort chip_scsi_id;
3449 ushort lramword;
3450 uchar lrambyte;
3451 ushort tagqng_able;
3452 ushort sdtr_able, wdtr_able;
3453 ushort wdtr_done, sdtr_done;
3454 ushort period = 0;
3455 int renegotiate = 0;
27c868c2 3456
51219358
MW
3457 v = &boardp->dvc_var.adv_dvc_var;
3458 c = &boardp->dvc_cfg.adv_dvc_cfg;
3459 iop_base = v->iop_base;
3460 chip_scsi_id = v->chip_scsi_id;
3461
b59fb6fd
AV
3462 seq_printf(m,
3463 "\nAdv Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3464 shost->host_no);
27c868c2 3465
b59fb6fd
AV
3466 seq_printf(m,
3467 " iop_base 0x%lx, cable_detect: %X, err_code %u\n",
31491e1a 3468 (unsigned long)v->iop_base,
b59fb6fd
AV
3469 AdvReadWordRegister(iop_base,IOPW_SCSI_CFG1) & CABLE_DETECT,
3470 v->err_code);
1da177e4 3471
b59fb6fd
AV
3472 seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3473 "mcode_version 0x%x\n", c->chip_version,
3474 c->mcode_date, c->mcode_version);
51219358
MW
3475
3476 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
2f979427 3477 seq_puts(m, " Queuing Enabled:");
27c868c2 3478 for (i = 0; i <= ADV_MAX_TID; i++) {
51219358
MW
3479 if ((chip_scsi_id == i) ||
3480 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3481 continue;
27c868c2 3482 }
51219358 3483
b59fb6fd
AV
3484 seq_printf(m, " %X:%c",
3485 i,
3486 (tagqng_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
27c868c2 3487 }
1da177e4 3488
3d30079c 3489 seq_puts(m, "\n Queue Limit:");
51219358
MW
3490 for (i = 0; i <= ADV_MAX_TID; i++) {
3491 if ((chip_scsi_id == i) ||
3492 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3493 continue;
3494 }
1da177e4 3495
51219358
MW
3496 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + i,
3497 lrambyte);
27c868c2 3498
b59fb6fd 3499 seq_printf(m, " %X:%d", i, lrambyte);
51219358 3500 }
27c868c2 3501
3d30079c 3502 seq_puts(m, "\n Command Pending:");
51219358
MW
3503 for (i = 0; i <= ADV_MAX_TID; i++) {
3504 if ((chip_scsi_id == i) ||
3505 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3506 continue;
3507 }
27c868c2 3508
51219358
MW
3509 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_QUEUED_CMD + i,
3510 lrambyte);
1da177e4 3511
b59fb6fd 3512 seq_printf(m, " %X:%d", i, lrambyte);
51219358 3513 }
f50332ff 3514 seq_putc(m, '\n');
1da177e4 3515
51219358 3516 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
2f979427 3517 seq_puts(m, " Wide Enabled:");
51219358
MW
3518 for (i = 0; i <= ADV_MAX_TID; i++) {
3519 if ((chip_scsi_id == i) ||
3520 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3521 continue;
27c868c2 3522 }
51219358 3523
b59fb6fd
AV
3524 seq_printf(m, " %X:%c",
3525 i,
3526 (wdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
27c868c2 3527 }
f50332ff 3528 seq_putc(m, '\n');
1da177e4 3529
51219358 3530 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, wdtr_done);
2f979427 3531 seq_puts(m, " Transfer Bit Width:");
51219358
MW
3532 for (i = 0; i <= ADV_MAX_TID; i++) {
3533 if ((chip_scsi_id == i) ||
3534 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3535 continue;
3536 }
1da177e4 3537
51219358
MW
3538 AdvReadWordLram(iop_base,
3539 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3540 lramword);
27c868c2 3541
b59fb6fd
AV
3542 seq_printf(m, " %X:%d",
3543 i, (lramword & 0x8000) ? 16 : 8);
27c868c2 3544
51219358
MW
3545 if ((wdtr_able & ADV_TID_TO_TIDMASK(i)) &&
3546 (wdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
f50332ff 3547 seq_putc(m, '*');
51219358 3548 renegotiate = 1;
27c868c2 3549 }
51219358 3550 }
f50332ff 3551 seq_putc(m, '\n');
27c868c2 3552
51219358 3553 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
2f979427 3554 seq_puts(m, " Synchronous Enabled:");
51219358
MW
3555 for (i = 0; i <= ADV_MAX_TID; i++) {
3556 if ((chip_scsi_id == i) ||
3557 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3558 continue;
3559 }
27c868c2 3560
b59fb6fd
AV
3561 seq_printf(m, " %X:%c",
3562 i,
3563 (sdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
51219358 3564 }
f50332ff 3565 seq_putc(m, '\n');
27c868c2 3566
51219358
MW
3567 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, sdtr_done);
3568 for (i = 0; i <= ADV_MAX_TID; i++) {
27c868c2 3569
51219358
MW
3570 AdvReadWordLram(iop_base,
3571 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3572 lramword);
3573 lramword &= ~0x8000;
27c868c2 3574
51219358
MW
3575 if ((chip_scsi_id == i) ||
3576 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3577 ((sdtr_able & ADV_TID_TO_TIDMASK(i)) == 0)) {
3578 continue;
27c868c2
MW
3579 }
3580
b59fb6fd 3581 seq_printf(m, " %X:", i);
27c868c2 3582
51219358 3583 if ((lramword & 0x1F) == 0) { /* Check for REQ/ACK Offset 0. */
2f979427 3584 seq_puts(m, " Asynchronous");
51219358 3585 } else {
2f979427 3586 seq_puts(m, " Transfer Period Factor: ");
27c868c2 3587
51219358 3588 if ((lramword & 0x1F00) == 0x1100) { /* 80 Mhz */
2f979427 3589 seq_puts(m, "9 (80.0 Mhz),");
51219358 3590 } else if ((lramword & 0x1F00) == 0x1000) { /* 40 Mhz */
2f979427 3591 seq_puts(m, "10 (40.0 Mhz),");
51219358 3592 } else { /* 20 Mhz or below. */
27c868c2 3593
51219358
MW
3594 period = (((lramword >> 8) * 25) + 50) / 4;
3595
3596 if (period == 0) { /* Should never happen. */
31491e1a 3597 seq_printf(m, "%d (? Mhz), ", period);
51219358 3598 } else {
b59fb6fd
AV
3599 seq_printf(m,
3600 "%d (%d.%d Mhz),",
3601 period, 250 / period,
3602 ASC_TENTHS(250, period));
51219358
MW
3603 }
3604 }
3605
b59fb6fd
AV
3606 seq_printf(m, " REQ/ACK Offset: %d",
3607 lramword & 0x1F);
51219358
MW
3608 }
3609
3610 if ((sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
2f979427 3611 seq_puts(m, "*\n");
51219358
MW
3612 renegotiate = 1;
3613 } else {
f50332ff 3614 seq_putc(m, '\n');
51219358 3615 }
27c868c2 3616 }
51219358
MW
3617
3618 if (renegotiate) {
2f979427 3619 seq_puts(m, " * = Re-negotiation pending before next command.\n");
51219358 3620 }
51219358
MW
3621}
3622
3623#ifdef ADVANSYS_STATS
3624/*
3625 * asc_prt_board_stats()
1da177e4 3626 */
b59fb6fd 3627static void asc_prt_board_stats(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 3628{
d2411495
MW
3629 struct asc_board *boardp = shost_priv(shost);
3630 struct asc_stats *s = &boardp->asc_stats;
27c868c2 3631
b59fb6fd
AV
3632 seq_printf(m,
3633 "\nLinux Driver Statistics for AdvanSys SCSI Host %d:\n",
3634 shost->host_no);
51219358 3635
b59fb6fd 3636 seq_printf(m,
31491e1a 3637 " queuecommand %u, reset %u, biosparam %u, interrupt %u\n",
b59fb6fd
AV
3638 s->queuecommand, s->reset, s->biosparam,
3639 s->interrupt);
27c868c2 3640
b59fb6fd 3641 seq_printf(m,
31491e1a 3642 " callback %u, done %u, build_error %u, build_noreq %u, build_nosg %u\n",
b59fb6fd
AV
3643 s->callback, s->done, s->build_error,
3644 s->adv_build_noreq, s->adv_build_nosg);
27c868c2 3645
b59fb6fd 3646 seq_printf(m,
31491e1a 3647 " exe_noerror %u, exe_busy %u, exe_error %u, exe_unknown %u\n",
b59fb6fd
AV
3648 s->exe_noerror, s->exe_busy, s->exe_error,
3649 s->exe_unknown);
51219358
MW
3650
3651 /*
3652 * Display data transfer statistics.
3653 */
52c334e9 3654 if (s->xfer_cnt > 0) {
31491e1a 3655 seq_printf(m, " xfer_cnt %u, xfer_elem %u, ",
b59fb6fd 3656 s->xfer_cnt, s->xfer_elem);
27c868c2 3657
31491e1a 3658 seq_printf(m, "xfer_bytes %u.%01u kb\n",
b59fb6fd 3659 s->xfer_sect / 2, ASC_TENTHS(s->xfer_sect, 2));
1da177e4 3660
51219358 3661 /* Scatter gather transfer statistics */
31491e1a 3662 seq_printf(m, " avg_num_elem %u.%01u, ",
b59fb6fd
AV
3663 s->xfer_elem / s->xfer_cnt,
3664 ASC_TENTHS(s->xfer_elem, s->xfer_cnt));
51219358 3665
31491e1a 3666 seq_printf(m, "avg_elem_size %u.%01u kb, ",
b59fb6fd
AV
3667 (s->xfer_sect / 2) / s->xfer_elem,
3668 ASC_TENTHS((s->xfer_sect / 2), s->xfer_elem));
51219358 3669
31491e1a 3670 seq_printf(m, "avg_xfer_size %u.%01u kb\n",
b59fb6fd
AV
3671 (s->xfer_sect / 2) / s->xfer_cnt,
3672 ASC_TENTHS((s->xfer_sect / 2), s->xfer_cnt));
27c868c2 3673 }
1da177e4 3674}
51219358 3675#endif /* ADVANSYS_STATS */
1da177e4
LT
3676
3677/*
b59fb6fd 3678 * advansys_show_info() - /proc/scsi/advansys/{0,1,2,3,...}
1da177e4 3679 *
b59fb6fd
AV
3680 * m: seq_file to print into
3681 * shost: Scsi_Host
1da177e4 3682 *
51219358
MW
3683 * Return the number of bytes read from or written to a
3684 * /proc/scsi/advansys/[0...] file.
1da177e4 3685 */
51219358 3686static int
b59fb6fd 3687advansys_show_info(struct seq_file *m, struct Scsi_Host *shost)
1da177e4 3688{
d2411495 3689 struct asc_board *boardp = shost_priv(shost);
27c868c2 3690
b352f923 3691 ASC_DBG(1, "begin\n");
51219358 3692
51219358
MW
3693 /*
3694 * User read of /proc/scsi/advansys/[0...] file.
3695 */
1da177e4 3696
51219358
MW
3697 /*
3698 * Get board configuration information.
3699 *
3700 * advansys_info() returns the board string from its own static buffer.
3701 */
51219358 3702 /* Copy board information. */
b59fb6fd 3703 seq_printf(m, "%s\n", (char *)advansys_info(shost));
51219358
MW
3704 /*
3705 * Display Wide Board BIOS Information.
3706 */
b59fb6fd
AV
3707 if (!ASC_NARROW_BOARD(boardp))
3708 asc_prt_adv_bios(m, shost);
1da177e4 3709
51219358
MW
3710 /*
3711 * Display driver information for each device attached to the board.
3712 */
b59fb6fd 3713 asc_prt_board_devices(m, shost);
51219358
MW
3714
3715 /*
3716 * Display EEPROM configuration for the board.
3717 */
b59fb6fd
AV
3718 if (ASC_NARROW_BOARD(boardp))
3719 asc_prt_asc_board_eeprom(m, shost);
3720 else
3721 asc_prt_adv_board_eeprom(m, shost);
1da177e4 3722
51219358
MW
3723 /*
3724 * Display driver configuration and information for the board.
3725 */
b59fb6fd 3726 asc_prt_driver_conf(m, shost);
1da177e4 3727
51219358
MW
3728#ifdef ADVANSYS_STATS
3729 /*
3730 * Display driver statistics for the board.
3731 */
b59fb6fd 3732 asc_prt_board_stats(m, shost);
51219358 3733#endif /* ADVANSYS_STATS */
1da177e4 3734
51219358
MW
3735 /*
3736 * Display Asc Library dynamic configuration information
3737 * for the board.
3738 */
b59fb6fd
AV
3739 if (ASC_NARROW_BOARD(boardp))
3740 asc_prt_asc_board_info(m, shost);
3741 else
3742 asc_prt_adv_board_info(m, shost);
3743 return 0;
51219358
MW
3744}
3745#endif /* CONFIG_PROC_FS */
3746
3747static void asc_scsi_done(struct scsi_cmnd *scp)
3748{
52c334e9 3749 scsi_dma_unmap(scp);
51219358 3750 ASC_STATS(scp->device->host, done);
51219358
MW
3751 scp->scsi_done(scp);
3752}
3753
3754static void AscSetBank(PortAddr iop_base, uchar bank)
3755{
3756 uchar val;
3757
3758 val = AscGetChipControl(iop_base) &
3759 (~
3760 (CC_SINGLE_STEP | CC_TEST | CC_DIAG | CC_SCSI_RESET |
3761 CC_CHIP_RESET));
3762 if (bank == 1) {
3763 val |= CC_BANK_ONE;
3764 } else if (bank == 2) {
3765 val |= CC_DIAG | CC_BANK_ONE;
27c868c2 3766 } else {
51219358 3767 val &= ~CC_BANK_ONE;
27c868c2 3768 }
51219358 3769 AscSetChipControl(iop_base, val);
51219358
MW
3770}
3771
3772static void AscSetChipIH(PortAddr iop_base, ushort ins_code)
3773{
3774 AscSetBank(iop_base, 1);
3775 AscWriteChipIH(iop_base, ins_code);
3776 AscSetBank(iop_base, 0);
51219358
MW
3777}
3778
3779static int AscStartChip(PortAddr iop_base)
3780{
3781 AscSetChipControl(iop_base, 0);
3782 if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3783 return (0);
27c868c2 3784 }
51219358
MW
3785 return (1);
3786}
27c868c2 3787
51219358
MW
3788static int AscStopChip(PortAddr iop_base)
3789{
3790 uchar cc_val;
3791
3792 cc_val =
3793 AscGetChipControl(iop_base) &
3794 (~(CC_SINGLE_STEP | CC_TEST | CC_DIAG));
3795 AscSetChipControl(iop_base, (uchar)(cc_val | CC_HALT));
3796 AscSetChipIH(iop_base, INS_HALT);
3797 AscSetChipIH(iop_base, INS_RFLAG_WTM);
3798 if ((AscGetChipStatus(iop_base) & CSW_HALTED) == 0) {
3799 return (0);
27c868c2 3800 }
51219358
MW
3801 return (1);
3802}
27c868c2 3803
51219358
MW
3804static int AscIsChipHalted(PortAddr iop_base)
3805{
3806 if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3807 if ((AscGetChipControl(iop_base) & CC_HALT) != 0) {
3808 return (1);
27c868c2 3809 }
27c868c2 3810 }
51219358
MW
3811 return (0);
3812}
27c868c2 3813
51219358
MW
3814static int AscResetChipAndScsiBus(ASC_DVC_VAR *asc_dvc)
3815{
3816 PortAddr iop_base;
3817 int i = 10;
3818
3819 iop_base = asc_dvc->iop_base;
3820 while ((AscGetChipStatus(iop_base) & CSW_SCSI_RESET_ACTIVE)
3821 && (i-- > 0)) {
3822 mdelay(100);
27c868c2 3823 }
51219358
MW
3824 AscStopChip(iop_base);
3825 AscSetChipControl(iop_base, CC_CHIP_RESET | CC_SCSI_RESET | CC_HALT);
3826 udelay(60);
3827 AscSetChipIH(iop_base, INS_RFLAG_WTM);
3828 AscSetChipIH(iop_base, INS_HALT);
3829 AscSetChipControl(iop_base, CC_CHIP_RESET | CC_HALT);
3830 AscSetChipControl(iop_base, CC_HALT);
3831 mdelay(200);
3832 AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
3833 AscSetChipStatus(iop_base, 0);
3834 return (AscIsChipHalted(iop_base));
3835}
27c868c2 3836
51219358
MW
3837static int AscFindSignature(PortAddr iop_base)
3838{
3839 ushort sig_word;
27c868c2 3840
b352f923 3841 ASC_DBG(1, "AscGetChipSignatureByte(0x%x) 0x%x\n",
51219358
MW
3842 iop_base, AscGetChipSignatureByte(iop_base));
3843 if (AscGetChipSignatureByte(iop_base) == (uchar)ASC_1000_ID1B) {
b352f923 3844 ASC_DBG(1, "AscGetChipSignatureWord(0x%x) 0x%x\n",
51219358
MW
3845 iop_base, AscGetChipSignatureWord(iop_base));
3846 sig_word = AscGetChipSignatureWord(iop_base);
3847 if ((sig_word == (ushort)ASC_1000_ID0W) ||
3848 (sig_word == (ushort)ASC_1000_ID0W_FIX)) {
3849 return (1);
27c868c2 3850 }
27c868c2 3851 }
51219358 3852 return (0);
27c868c2
MW
3853}
3854
51219358 3855static void AscEnableInterrupt(PortAddr iop_base)
1da177e4 3856{
51219358 3857 ushort cfg;
27c868c2 3858
51219358
MW
3859 cfg = AscGetChipCfgLsw(iop_base);
3860 AscSetChipCfgLsw(iop_base, cfg | ASC_CFG0_HOST_INT_ON);
51219358 3861}
27c868c2 3862
51219358
MW
3863static void AscDisableInterrupt(PortAddr iop_base)
3864{
3865 ushort cfg;
27c868c2 3866
51219358
MW
3867 cfg = AscGetChipCfgLsw(iop_base);
3868 AscSetChipCfgLsw(iop_base, cfg & (~ASC_CFG0_HOST_INT_ON));
51219358 3869}
27c868c2 3870
51219358
MW
3871static uchar AscReadLramByte(PortAddr iop_base, ushort addr)
3872{
3873 unsigned char byte_data;
3874 unsigned short word_data;
27c868c2 3875
51219358
MW
3876 if (isodd_word(addr)) {
3877 AscSetChipLramAddr(iop_base, addr - 1);
3878 word_data = AscGetChipLramData(iop_base);
3879 byte_data = (word_data >> 8) & 0xFF;
3880 } else {
3881 AscSetChipLramAddr(iop_base, addr);
3882 word_data = AscGetChipLramData(iop_base);
3883 byte_data = word_data & 0xFF;
3884 }
3885 return byte_data;
3886}
27c868c2 3887
51219358
MW
3888static ushort AscReadLramWord(PortAddr iop_base, ushort addr)
3889{
3890 ushort word_data;
27c868c2 3891
51219358
MW
3892 AscSetChipLramAddr(iop_base, addr);
3893 word_data = AscGetChipLramData(iop_base);
3894 return (word_data);
3895}
27c868c2 3896
51219358
MW
3897#if CC_VERY_LONG_SG_LIST
3898static ASC_DCNT AscReadLramDWord(PortAddr iop_base, ushort addr)
3899{
3900 ushort val_low, val_high;
3901 ASC_DCNT dword_data;
27c868c2 3902
51219358
MW
3903 AscSetChipLramAddr(iop_base, addr);
3904 val_low = AscGetChipLramData(iop_base);
3905 val_high = AscGetChipLramData(iop_base);
3906 dword_data = ((ASC_DCNT) val_high << 16) | (ASC_DCNT) val_low;
3907 return (dword_data);
3908}
3909#endif /* CC_VERY_LONG_SG_LIST */
3910
3911static void
3912AscMemWordSetLram(PortAddr iop_base, ushort s_addr, ushort set_wval, int words)
3913{
3914 int i;
3915
3916 AscSetChipLramAddr(iop_base, s_addr);
3917 for (i = 0; i < words; i++) {
3918 AscSetChipLramData(iop_base, set_wval);
27c868c2 3919 }
51219358 3920}
1da177e4 3921
51219358
MW
3922static void AscWriteLramWord(PortAddr iop_base, ushort addr, ushort word_val)
3923{
3924 AscSetChipLramAddr(iop_base, addr);
3925 AscSetChipLramData(iop_base, word_val);
51219358
MW
3926}
3927
3928static void AscWriteLramByte(PortAddr iop_base, ushort addr, uchar byte_val)
3929{
3930 ushort word_data;
3931
3932 if (isodd_word(addr)) {
3933 addr--;
3934 word_data = AscReadLramWord(iop_base, addr);
3935 word_data &= 0x00FF;
3936 word_data |= (((ushort)byte_val << 8) & 0xFF00);
3937 } else {
3938 word_data = AscReadLramWord(iop_base, addr);
3939 word_data &= 0xFF00;
3940 word_data |= ((ushort)byte_val & 0x00FF);
3941 }
3942 AscWriteLramWord(iop_base, addr, word_data);
1da177e4
LT
3943}
3944
3945/*
51219358 3946 * Copy 2 bytes to LRAM.
1da177e4 3947 *
51219358
MW
3948 * The source data is assumed to be in little-endian order in memory
3949 * and is maintained in little-endian order when written to LRAM.
1da177e4 3950 */
51219358 3951static void
989bb5f5
JSR
3952AscMemWordCopyPtrToLram(PortAddr iop_base, ushort s_addr,
3953 const uchar *s_buffer, int words)
1da177e4 3954{
27c868c2 3955 int i;
27c868c2 3956
51219358
MW
3957 AscSetChipLramAddr(iop_base, s_addr);
3958 for (i = 0; i < 2 * words; i += 2) {
3959 /*
3960 * On a little-endian system the second argument below
3961 * produces a little-endian ushort which is written to
3962 * LRAM in little-endian order. On a big-endian system
3963 * the second argument produces a big-endian ushort which
3964 * is "transparently" byte-swapped by outpw() and written
3965 * in little-endian order to LRAM.
3966 */
3967 outpw(iop_base + IOP_RAM_DATA,
3968 ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]);
3969 }
51219358 3970}
27c868c2 3971
51219358
MW
3972/*
3973 * Copy 4 bytes to LRAM.
3974 *
3975 * The source data is assumed to be in little-endian order in memory
25985edc 3976 * and is maintained in little-endian order when written to LRAM.
51219358
MW
3977 */
3978static void
3979AscMemDWordCopyPtrToLram(PortAddr iop_base,
3980 ushort s_addr, uchar *s_buffer, int dwords)
3981{
3982 int i;
27c868c2 3983
51219358
MW
3984 AscSetChipLramAddr(iop_base, s_addr);
3985 for (i = 0; i < 4 * dwords; i += 4) {
3986 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]); /* LSW */
3987 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 3] << 8) | s_buffer[i + 2]); /* MSW */
3988 }
51219358 3989}
27c868c2 3990
51219358
MW
3991/*
3992 * Copy 2 bytes from LRAM.
3993 *
3994 * The source data is assumed to be in little-endian order in LRAM
3995 * and is maintained in little-endian order when written to memory.
3996 */
3997static void
3998AscMemWordCopyPtrFromLram(PortAddr iop_base,
3999 ushort s_addr, uchar *d_buffer, int words)
4000{
4001 int i;
4002 ushort word;
27c868c2 4003
51219358
MW
4004 AscSetChipLramAddr(iop_base, s_addr);
4005 for (i = 0; i < 2 * words; i += 2) {
4006 word = inpw(iop_base + IOP_RAM_DATA);
4007 d_buffer[i] = word & 0xff;
4008 d_buffer[i + 1] = (word >> 8) & 0xff;
27c868c2 4009 }
51219358 4010}
27c868c2 4011
51219358
MW
4012static ASC_DCNT AscMemSumLramWord(PortAddr iop_base, ushort s_addr, int words)
4013{
4014 ASC_DCNT sum;
4015 int i;
27c868c2 4016
51219358
MW
4017 sum = 0L;
4018 for (i = 0; i < words; i++, s_addr += 2) {
4019 sum += AscReadLramWord(iop_base, s_addr);
27c868c2 4020 }
51219358
MW
4021 return (sum);
4022}
27c868c2 4023
51219358
MW
4024static ushort AscInitLram(ASC_DVC_VAR *asc_dvc)
4025{
4026 uchar i;
4027 ushort s_addr;
4028 PortAddr iop_base;
4029 ushort warn_code;
27c868c2 4030
51219358
MW
4031 iop_base = asc_dvc->iop_base;
4032 warn_code = 0;
4033 AscMemWordSetLram(iop_base, ASC_QADR_BEG, 0,
4034 (ushort)(((int)(asc_dvc->max_total_qng + 2 + 1) *
4035 64) >> 1));
4036 i = ASC_MIN_ACTIVE_QNO;
4037 s_addr = ASC_QADR_BEG + ASC_QBLK_SIZE;
4038 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4039 (uchar)(i + 1));
4040 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4041 (uchar)(asc_dvc->max_total_qng));
4042 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4043 (uchar)i);
4044 i++;
4045 s_addr += ASC_QBLK_SIZE;
4046 for (; i < asc_dvc->max_total_qng; i++, s_addr += ASC_QBLK_SIZE) {
4047 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4048 (uchar)(i + 1));
4049 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4050 (uchar)(i - 1));
4051 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4052 (uchar)i);
27c868c2 4053 }
51219358
MW
4054 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4055 (uchar)ASC_QLINK_END);
4056 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4057 (uchar)(asc_dvc->max_total_qng - 1));
4058 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4059 (uchar)asc_dvc->max_total_qng);
4060 i++;
4061 s_addr += ASC_QBLK_SIZE;
4062 for (; i <= (uchar)(asc_dvc->max_total_qng + 3);
4063 i++, s_addr += ASC_QBLK_SIZE) {
4064 AscWriteLramByte(iop_base,
4065 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_FWD), i);
4066 AscWriteLramByte(iop_base,
4067 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_BWD), i);
4068 AscWriteLramByte(iop_base,
4069 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_QNO), i);
27c868c2 4070 }
51219358 4071 return warn_code;
1da177e4
LT
4072}
4073
51219358 4074static ASC_DCNT
989bb5f5
JSR
4075AscLoadMicroCode(PortAddr iop_base, ushort s_addr,
4076 const uchar *mcode_buf, ushort mcode_size)
1da177e4 4077{
51219358
MW
4078 ASC_DCNT chksum;
4079 ushort mcode_word_size;
4080 ushort mcode_chksum;
27c868c2 4081
51219358
MW
4082 /* Write the microcode buffer starting at LRAM address 0. */
4083 mcode_word_size = (ushort)(mcode_size >> 1);
4084 AscMemWordSetLram(iop_base, s_addr, 0, mcode_word_size);
4085 AscMemWordCopyPtrToLram(iop_base, s_addr, mcode_buf, mcode_word_size);
27c868c2 4086
51219358 4087 chksum = AscMemSumLramWord(iop_base, s_addr, mcode_word_size);
b352f923 4088 ASC_DBG(1, "chksum 0x%lx\n", (ulong)chksum);
51219358
MW
4089 mcode_chksum = (ushort)AscMemSumLramWord(iop_base,
4090 (ushort)ASC_CODE_SEC_BEG,
4091 (ushort)((mcode_size -
4092 s_addr - (ushort)
4093 ASC_CODE_SEC_BEG) /
4094 2));
b352f923 4095 ASC_DBG(1, "mcode_chksum 0x%lx\n", (ulong)mcode_chksum);
51219358
MW
4096 AscWriteLramWord(iop_base, ASCV_MCODE_CHKSUM_W, mcode_chksum);
4097 AscWriteLramWord(iop_base, ASCV_MCODE_SIZE_W, mcode_size);
b352f923 4098 return chksum;
51219358 4099}
27c868c2 4100
51219358
MW
4101static void AscInitQLinkVar(ASC_DVC_VAR *asc_dvc)
4102{
4103 PortAddr iop_base;
4104 int i;
4105 ushort lram_addr;
4106
4107 iop_base = asc_dvc->iop_base;
4108 AscPutRiscVarFreeQHead(iop_base, 1);
4109 AscPutRiscVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4110 AscPutVarFreeQHead(iop_base, 1);
4111 AscPutVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4112 AscWriteLramByte(iop_base, ASCV_BUSY_QHEAD_B,
4113 (uchar)((int)asc_dvc->max_total_qng + 1));
4114 AscWriteLramByte(iop_base, ASCV_DISC1_QHEAD_B,
4115 (uchar)((int)asc_dvc->max_total_qng + 2));
4116 AscWriteLramByte(iop_base, (ushort)ASCV_TOTAL_READY_Q_B,
4117 asc_dvc->max_total_qng);
4118 AscWriteLramWord(iop_base, ASCV_ASCDVC_ERR_CODE_W, 0);
4119 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
4120 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, 0);
4121 AscWriteLramByte(iop_base, ASCV_SCSIBUSY_B, 0);
4122 AscWriteLramByte(iop_base, ASCV_WTM_FLAG_B, 0);
4123 AscPutQDoneInProgress(iop_base, 0);
4124 lram_addr = ASC_QADR_BEG;
4125 for (i = 0; i < 32; i++, lram_addr += 2) {
4126 AscWriteLramWord(iop_base, lram_addr, 0);
4127 }
4128}
4129
4130static ushort AscInitMicroCodeVar(ASC_DVC_VAR *asc_dvc)
4131{
4132 int i;
4133 ushort warn_code;
4134 PortAddr iop_base;
4135 ASC_PADDR phy_addr;
4136 ASC_DCNT phy_size;
d10fb2c7 4137 struct asc_board *board = asc_dvc_to_board(asc_dvc);
51219358
MW
4138
4139 iop_base = asc_dvc->iop_base;
4140 warn_code = 0;
4141 for (i = 0; i <= ASC_MAX_TID; i++) {
4142 AscPutMCodeInitSDTRAtID(iop_base, i,
4143 asc_dvc->cfg->sdtr_period_offset[i]);
4144 }
4145
4146 AscInitQLinkVar(asc_dvc);
4147 AscWriteLramByte(iop_base, ASCV_DISC_ENABLE_B,
4148 asc_dvc->cfg->disc_enable);
4149 AscWriteLramByte(iop_base, ASCV_HOSTSCSI_ID_B,
4150 ASC_TID_TO_TARGET_ID(asc_dvc->cfg->chip_scsi_id));
4151
d10fb2c7
MW
4152 /* Ensure overrun buffer is aligned on an 8 byte boundary. */
4153 BUG_ON((unsigned long)asc_dvc->overrun_buf & 7);
4154 asc_dvc->overrun_dma = dma_map_single(board->dev, asc_dvc->overrun_buf,
4155 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
9a908c1a
HRK
4156 if (dma_mapping_error(board->dev, asc_dvc->overrun_dma)) {
4157 warn_code = -ENOMEM;
4158 goto err_dma_map;
4159 }
d10fb2c7 4160 phy_addr = cpu_to_le32(asc_dvc->overrun_dma);
51219358
MW
4161 AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_PADDR_D,
4162 (uchar *)&phy_addr, 1);
d10fb2c7 4163 phy_size = cpu_to_le32(ASC_OVERRUN_BSIZE);
51219358
MW
4164 AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_BSIZE_D,
4165 (uchar *)&phy_size, 1);
4166
4167 asc_dvc->cfg->mcode_date =
4168 AscReadLramWord(iop_base, (ushort)ASCV_MC_DATE_W);
4169 asc_dvc->cfg->mcode_version =
4170 AscReadLramWord(iop_base, (ushort)ASCV_MC_VER_W);
4171
4172 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
4173 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
4174 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
9a908c1a
HRK
4175 warn_code = UW_ERR;
4176 goto err_mcode_start;
51219358
MW
4177 }
4178 if (AscStartChip(iop_base) != 1) {
4179 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
9a908c1a
HRK
4180 warn_code = UW_ERR;
4181 goto err_mcode_start;
51219358
MW
4182 }
4183
4184 return warn_code;
9a908c1a
HRK
4185
4186err_mcode_start:
4187 dma_unmap_single(board->dev, asc_dvc->overrun_dma,
4188 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
4189err_dma_map:
4190 asc_dvc->overrun_dma = 0;
4191 return warn_code;
51219358
MW
4192}
4193
4194static ushort AscInitAsc1000Driver(ASC_DVC_VAR *asc_dvc)
4195{
989bb5f5
JSR
4196 const struct firmware *fw;
4197 const char fwname[] = "advansys/mcode.bin";
4198 int err;
4199 unsigned long chksum;
51219358
MW
4200 ushort warn_code;
4201 PortAddr iop_base;
4202
4203 iop_base = asc_dvc->iop_base;
4204 warn_code = 0;
4205 if ((asc_dvc->dvc_cntl & ASC_CNTL_RESET_SCSI) &&
4206 !(asc_dvc->init_state & ASC_INIT_RESET_SCSI_DONE)) {
4207 AscResetChipAndScsiBus(asc_dvc);
4208 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4209 }
4210 asc_dvc->init_state |= ASC_INIT_STATE_BEG_LOAD_MC;
4211 if (asc_dvc->err_code != 0)
4212 return UW_ERR;
4213 if (!AscFindSignature(asc_dvc->iop_base)) {
4214 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
4215 return warn_code;
4216 }
4217 AscDisableInterrupt(iop_base);
4218 warn_code |= AscInitLram(asc_dvc);
4219 if (asc_dvc->err_code != 0)
4220 return UW_ERR;
989bb5f5
JSR
4221
4222 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4223 if (err) {
4224 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4225 fwname, err);
cf747445 4226 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
4227 return err;
4228 }
4229 if (fw->size < 4) {
4230 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4231 fw->size, fwname);
4232 release_firmware(fw);
cf747445 4233 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
4234 return -EINVAL;
4235 }
4236 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4237 (fw->data[1] << 8) | fw->data[0];
4238 ASC_DBG(1, "_asc_mcode_chksum 0x%lx\n", (ulong)chksum);
4239 if (AscLoadMicroCode(iop_base, 0, &fw->data[4],
4240 fw->size - 4) != chksum) {
51219358 4241 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
989bb5f5 4242 release_firmware(fw);
51219358
MW
4243 return warn_code;
4244 }
989bb5f5 4245 release_firmware(fw);
51219358 4246 warn_code |= AscInitMicroCodeVar(asc_dvc);
9a908c1a
HRK
4247 if (!asc_dvc->overrun_dma)
4248 return warn_code;
51219358
MW
4249 asc_dvc->init_state |= ASC_INIT_STATE_END_LOAD_MC;
4250 AscEnableInterrupt(iop_base);
4251 return warn_code;
4252}
4253
4254/*
4255 * Load the Microcode
4256 *
4257 * Write the microcode image to RISC memory starting at address 0.
4258 *
4259 * The microcode is stored compressed in the following format:
4260 *
4261 * 254 word (508 byte) table indexed by byte code followed
4262 * by the following byte codes:
4263 *
4264 * 1-Byte Code:
4265 * 00: Emit word 0 in table.
4266 * 01: Emit word 1 in table.
4267 * .
4268 * FD: Emit word 253 in table.
4269 *
4270 * Multi-Byte Code:
4271 * FE WW WW: (3 byte code) Word to emit is the next word WW WW.
4272 * FF BB WW WW: (4 byte code) Emit BB count times next word WW WW.
4273 *
4274 * Returns 0 or an error if the checksum doesn't match
4275 */
989bb5f5
JSR
4276static int AdvLoadMicrocode(AdvPortAddr iop_base, const unsigned char *buf,
4277 int size, int memsize, int chksum)
51219358
MW
4278{
4279 int i, j, end, len = 0;
4280 ADV_DCNT sum;
4281
4282 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4283
4284 for (i = 253 * 2; i < size; i++) {
4285 if (buf[i] == 0xff) {
4286 unsigned short word = (buf[i + 3] << 8) | buf[i + 2];
4287 for (j = 0; j < buf[i + 1]; j++) {
4288 AdvWriteWordAutoIncLram(iop_base, word);
4289 len += 2;
4290 }
4291 i += 3;
4292 } else if (buf[i] == 0xfe) {
4293 unsigned short word = (buf[i + 2] << 8) | buf[i + 1];
4294 AdvWriteWordAutoIncLram(iop_base, word);
4295 i += 2;
4296 len += 2;
4297 } else {
951b62c1 4298 unsigned int off = buf[i] * 2;
51219358
MW
4299 unsigned short word = (buf[off + 1] << 8) | buf[off];
4300 AdvWriteWordAutoIncLram(iop_base, word);
4301 len += 2;
4302 }
4303 }
4304
4305 end = len;
4306
4307 while (len < memsize) {
4308 AdvWriteWordAutoIncLram(iop_base, 0);
4309 len += 2;
4310 }
4311
4312 /* Verify the microcode checksum. */
4313 sum = 0;
4314 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4315
4316 for (len = 0; len < end; len += 2) {
4317 sum += AdvReadWordAutoIncLram(iop_base);
4318 }
4319
4320 if (sum != chksum)
4321 return ASC_IERR_MCODE_CHKSUM;
4322
4323 return 0;
4324}
4325
98b96a7d 4326static void AdvBuildCarrierFreelist(struct adv_dvc_var *adv_dvc)
51219358 4327{
98b96a7d
HR
4328 off_t carr_offset = 0, next_offset;
4329 dma_addr_t carr_paddr;
4330 int carr_num = ADV_CARRIER_BUFSIZE / sizeof(ADV_CARR_T), i;
51219358 4331
98b96a7d
HR
4332 for (i = 0; i < carr_num; i++) {
4333 carr_offset = i * sizeof(ADV_CARR_T);
4334 /* Get physical address of the carrier 'carrp'. */
4335 carr_paddr = adv_dvc->carrier_addr + carr_offset;
4336
4337 adv_dvc->carrier[i].carr_pa = cpu_to_le32(carr_paddr);
4338 adv_dvc->carrier[i].carr_va = cpu_to_le32(carr_offset);
4339 adv_dvc->carrier[i].areq_vpa = 0;
4340 next_offset = carr_offset + sizeof(ADV_CARR_T);
4341 if (i == carr_num)
4342 next_offset = ~0;
4343 adv_dvc->carrier[i].next_vpa = cpu_to_le32(next_offset);
51219358 4344 }
98b96a7d
HR
4345 /*
4346 * We cannot have a carrier with 'carr_va' of '0', as
4347 * a reference to this carrier would be interpreted as
4348 * list termination.
4349 * So start at carrier 1 with the freelist.
4350 */
4351 adv_dvc->carr_freelist = &adv_dvc->carrier[1];
4352}
51219358 4353
98b96a7d
HR
4354static ADV_CARR_T *adv_get_carrier(struct adv_dvc_var *adv_dvc, u32 offset)
4355{
4356 int index;
51219358 4357
98b96a7d 4358 BUG_ON(offset > ADV_CARRIER_BUFSIZE);
51219358 4359
98b96a7d
HR
4360 index = offset / sizeof(ADV_CARR_T);
4361 return &adv_dvc->carrier[index];
4362}
51219358 4363
98b96a7d
HR
4364static ADV_CARR_T *adv_get_next_carrier(struct adv_dvc_var *adv_dvc)
4365{
4366 ADV_CARR_T *carrp = adv_dvc->carr_freelist;
4367 u32 next_vpa = le32_to_cpu(carrp->next_vpa);
4368
4369 if (next_vpa == 0 || next_vpa == ~0) {
4370 ASC_DBG(1, "invalid vpa offset 0x%x\n", next_vpa);
4371 return NULL;
4372 }
51219358 4373
98b96a7d
HR
4374 adv_dvc->carr_freelist = adv_get_carrier(adv_dvc, next_vpa);
4375 /*
4376 * insert stopper carrier to terminate list
4377 */
4378 carrp->next_vpa = cpu_to_le32(ASC_CQ_STOPPER);
4379
4380 return carrp;
51219358
MW
4381}
4382
4b47e464
HR
4383/*
4384 * 'offset' is the index in the request pointer array
4385 */
4386static adv_req_t * adv_get_reqp(struct adv_dvc_var *adv_dvc, u32 offset)
4387{
4388 struct asc_board *boardp = adv_dvc->drv_ptr;
4389
4390 BUG_ON(offset > adv_dvc->max_host_qng);
4391 return &boardp->adv_reqp[offset];
4392}
4393
51219358
MW
4394/*
4395 * Send an idle command to the chip and wait for completion.
4396 *
4397 * Command completion is polled for once per microsecond.
4398 *
4399 * The function can be called from anywhere including an interrupt handler.
4400 * But the function is not re-entrant, so it uses the DvcEnter/LeaveCritical()
4401 * functions to prevent reentrancy.
4402 *
4403 * Return Values:
4404 * ADV_TRUE - command completed successfully
4405 * ADV_FALSE - command failed
4406 * ADV_ERROR - command timed out
4407 */
4408static int
4409AdvSendIdleCmd(ADV_DVC_VAR *asc_dvc,
4410 ushort idle_cmd, ADV_DCNT idle_cmd_parameter)
4411{
4412 int result;
4413 ADV_DCNT i, j;
4414 AdvPortAddr iop_base;
4415
4416 iop_base = asc_dvc->iop_base;
4417
4418 /*
4419 * Clear the idle command status which is set by the microcode
4420 * to a non-zero value to indicate when the command is completed.
4421 * The non-zero result is one of the IDLE_CMD_STATUS_* values
4422 */
4423 AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS, (ushort)0);
4424
4425 /*
4426 * Write the idle command value after the idle command parameter
4427 * has been written to avoid a race condition. If the order is not
4428 * followed, the microcode may process the idle command before the
4429 * parameters have been written to LRAM.
4430 */
4431 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IDLE_CMD_PARAMETER,
4432 cpu_to_le32(idle_cmd_parameter));
4433 AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD, idle_cmd);
4434
4435 /*
4436 * Tickle the RISC to tell it to process the idle command.
4437 */
4438 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_B);
4439 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
4440 /*
4441 * Clear the tickle value. In the ASC-3550 the RISC flag
4442 * command 'clr_tickle_b' does not work unless the host
4443 * value is cleared.
4444 */
4445 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_NOP);
4446 }
4447
4448 /* Wait for up to 100 millisecond for the idle command to timeout. */
4449 for (i = 0; i < SCSI_WAIT_100_MSEC; i++) {
4450 /* Poll once each microsecond for command completion. */
4451 for (j = 0; j < SCSI_US_PER_MSEC; j++) {
4452 AdvReadWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS,
4453 result);
4454 if (result != 0)
4455 return result;
4456 udelay(1);
4457 }
4458 }
4459
4460 BUG(); /* The idle command should never timeout. */
4461 return ADV_ERROR;
4462}
4463
4464/*
4465 * Reset SCSI Bus and purge all outstanding requests.
4466 *
4467 * Return Value:
4468 * ADV_TRUE(1) - All requests are purged and SCSI Bus is reset.
4469 * ADV_FALSE(0) - Microcode command failed.
4470 * ADV_ERROR(-1) - Microcode command timed-out. Microcode or IC
4471 * may be hung which requires driver recovery.
4472 */
4473static int AdvResetSB(ADV_DVC_VAR *asc_dvc)
4474{
4475 int status;
4476
4477 /*
4478 * Send the SCSI Bus Reset idle start idle command which asserts
4479 * the SCSI Bus Reset signal.
4480 */
4481 status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_START, 0L);
4482 if (status != ADV_TRUE) {
4483 return status;
4484 }
4485
4486 /*
4487 * Delay for the specified SCSI Bus Reset hold time.
4488 *
4489 * The hold time delay is done on the host because the RISC has no
4490 * microsecond accurate timer.
4491 */
4492 udelay(ASC_SCSI_RESET_HOLD_TIME_US);
4493
4494 /*
4495 * Send the SCSI Bus Reset end idle command which de-asserts
4496 * the SCSI Bus Reset signal and purges any pending requests.
4497 */
4498 status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_END, 0L);
4499 if (status != ADV_TRUE) {
4500 return status;
4501 }
4502
4503 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4504
4505 return status;
4506}
4507
4508/*
4509 * Initialize the ASC-3550.
4510 *
4511 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4512 *
4513 * For a non-fatal error return a warning code. If there are no warnings
4514 * then 0 is returned.
4515 *
4516 * Needed after initialization for error recovery.
4517 */
4518static int AdvInitAsc3550Driver(ADV_DVC_VAR *asc_dvc)
4519{
989bb5f5
JSR
4520 const struct firmware *fw;
4521 const char fwname[] = "advansys/3550.bin";
51219358
MW
4522 AdvPortAddr iop_base;
4523 ushort warn_code;
4524 int begin_addr;
4525 int end_addr;
4526 ushort code_sum;
4527 int word;
4528 int i;
989bb5f5
JSR
4529 int err;
4530 unsigned long chksum;
51219358
MW
4531 ushort scsi_cfg1;
4532 uchar tid;
4533 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
4534 ushort wdtr_able = 0, sdtr_able, tagqng_able;
4535 uchar max_cmd[ADV_MAX_TID + 1];
4536
4537 /* If there is already an error, don't continue. */
4538 if (asc_dvc->err_code != 0)
4539 return ADV_ERROR;
4540
4541 /*
4542 * The caller must set 'chip_type' to ADV_CHIP_ASC3550.
4543 */
4544 if (asc_dvc->chip_type != ADV_CHIP_ASC3550) {
4545 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
4546 return ADV_ERROR;
4547 }
4548
4549 warn_code = 0;
4550 iop_base = asc_dvc->iop_base;
4551
4552 /*
4553 * Save the RISC memory BIOS region before writing the microcode.
4554 * The BIOS may already be loaded and using its RISC LRAM region
4555 * so its region must be saved and restored.
4556 *
4557 * Note: This code makes the assumption, which is currently true,
4558 * that a chip reset does not clear RISC LRAM.
4559 */
4560 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4561 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4562 bios_mem[i]);
4563 }
4564
4565 /*
4566 * Save current per TID negotiated values.
4567 */
4568 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] == 0x55AA) {
4569 ushort bios_version, major, minor;
4570
4571 bios_version =
4572 bios_mem[(ASC_MC_BIOS_VERSION - ASC_MC_BIOSMEM) / 2];
4573 major = (bios_version >> 12) & 0xF;
4574 minor = (bios_version >> 8) & 0xF;
4575 if (major < 3 || (major == 3 && minor == 1)) {
4576 /* BIOS 3.1 and earlier location of 'wdtr_able' variable. */
4577 AdvReadWordLram(iop_base, 0x120, wdtr_able);
4578 } else {
4579 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4580 }
4581 }
4582 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4583 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4584 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4585 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4586 max_cmd[tid]);
4587 }
4588
989bb5f5
JSR
4589 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4590 if (err) {
4591 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4592 fwname, err);
cf747445 4593 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
4594 return err;
4595 }
4596 if (fw->size < 4) {
4597 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4598 fw->size, fwname);
4599 release_firmware(fw);
cf747445 4600 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
4601 return -EINVAL;
4602 }
4603 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4604 (fw->data[1] << 8) | fw->data[0];
4605 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
4606 fw->size - 4, ADV_3550_MEMSIZE,
4607 chksum);
4608 release_firmware(fw);
51219358
MW
4609 if (asc_dvc->err_code)
4610 return ADV_ERROR;
4611
4612 /*
4613 * Restore the RISC memory BIOS region.
4614 */
4615 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4616 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4617 bios_mem[i]);
4618 }
4619
4620 /*
4621 * Calculate and write the microcode code checksum to the microcode
4622 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
4623 */
4624 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
4625 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
4626 code_sum = 0;
4627 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
4628 for (word = begin_addr; word < end_addr; word += 2) {
4629 code_sum += AdvReadWordAutoIncLram(iop_base);
4630 }
4631 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
4632
4633 /*
4634 * Read and save microcode version and date.
4635 */
4636 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
4637 asc_dvc->cfg->mcode_date);
4638 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
4639 asc_dvc->cfg->mcode_version);
4640
4641 /*
4642 * Set the chip type to indicate the ASC3550.
4643 */
4644 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC3550);
4645
4646 /*
4647 * If the PCI Configuration Command Register "Parity Error Response
4648 * Control" Bit was clear (0), then set the microcode variable
4649 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
4650 * to ignore DMA parity errors.
4651 */
4652 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
4653 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4654 word |= CONTROL_FLAG_IGNORE_PERR;
4655 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4656 }
4657
4658 /*
4659 * For ASC-3550, setting the START_CTL_EMFU [3:2] bits sets a FIFO
4660 * threshold of 128 bytes. This register is only accessible to the host.
4661 */
4662 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
4663 START_CTL_EMFU | READ_CMD_MRM);
4664
4665 /*
4666 * Microcode operating variables for WDTR, SDTR, and command tag
4667 * queuing will be set in slave_configure() based on what a
4668 * device reports it is capable of in Inquiry byte 7.
4669 *
4670 * If SCSI Bus Resets have been disabled, then directly set
4671 * SDTR and WDTR from the EEPROM configuration. This will allow
4672 * the BIOS and warm boot to work without a SCSI bus hang on
4673 * the Inquiry caused by host and target mismatched DTR values.
4674 * Without the SCSI Bus Reset, before an Inquiry a device can't
4675 * be assumed to be in Asynchronous, Narrow mode.
4676 */
4677 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
4678 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
4679 asc_dvc->wdtr_able);
4680 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
4681 asc_dvc->sdtr_able);
4682 }
4683
4684 /*
4685 * Set microcode operating variables for SDTR_SPEED1, SDTR_SPEED2,
4686 * SDTR_SPEED3, and SDTR_SPEED4 based on the ULTRA EEPROM per TID
4687 * bitmask. These values determine the maximum SDTR speed negotiated
4688 * with a device.
4689 *
4690 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
4691 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
4692 * without determining here whether the device supports SDTR.
4693 *
4694 * 4-bit speed SDTR speed name
4695 * =========== ===============
4696 * 0000b (0x0) SDTR disabled
4697 * 0001b (0x1) 5 Mhz
4698 * 0010b (0x2) 10 Mhz
4699 * 0011b (0x3) 20 Mhz (Ultra)
4700 * 0100b (0x4) 40 Mhz (LVD/Ultra2)
4701 * 0101b (0x5) 80 Mhz (LVD2/Ultra3)
4702 * 0110b (0x6) Undefined
4703 * .
4704 * 1111b (0xF) Undefined
4705 */
4706 word = 0;
4707 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4708 if (ADV_TID_TO_TIDMASK(tid) & asc_dvc->ultra_able) {
4709 /* Set Ultra speed for TID 'tid'. */
4710 word |= (0x3 << (4 * (tid % 4)));
4711 } else {
4712 /* Set Fast speed for TID 'tid'. */
4713 word |= (0x2 << (4 * (tid % 4)));
4714 }
4715 if (tid == 3) { /* Check if done with sdtr_speed1. */
4716 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, word);
4717 word = 0;
4718 } else if (tid == 7) { /* Check if done with sdtr_speed2. */
4719 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, word);
4720 word = 0;
4721 } else if (tid == 11) { /* Check if done with sdtr_speed3. */
4722 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, word);
4723 word = 0;
4724 } else if (tid == 15) { /* Check if done with sdtr_speed4. */
4725 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, word);
4726 /* End of loop. */
4727 }
4728 }
4729
4730 /*
4731 * Set microcode operating variable for the disconnect per TID bitmask.
4732 */
4733 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
4734 asc_dvc->cfg->disc_enable);
4735
4736 /*
4737 * Set SCSI_CFG0 Microcode Default Value.
4738 *
4739 * The microcode will set the SCSI_CFG0 register using this value
4740 * after it is started below.
4741 */
4742 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
4743 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
4744 asc_dvc->chip_scsi_id);
4745
4746 /*
4747 * Determine SCSI_CFG1 Microcode Default Value.
4748 *
4749 * The microcode will set the SCSI_CFG1 register using this value
4750 * after it is started below.
4751 */
4752
4753 /* Read current SCSI_CFG1 Register value. */
4754 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
4755
4756 /*
4757 * If all three connectors are in use, return an error.
4758 */
4759 if ((scsi_cfg1 & CABLE_ILLEGAL_A) == 0 ||
4760 (scsi_cfg1 & CABLE_ILLEGAL_B) == 0) {
4761 asc_dvc->err_code |= ASC_IERR_ILLEGAL_CONNECTION;
4762 return ADV_ERROR;
4763 }
4764
4765 /*
4766 * If the internal narrow cable is reversed all of the SCSI_CTRL
4767 * register signals will be set. Check for and return an error if
4768 * this condition is found.
4769 */
4770 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
4771 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
4772 return ADV_ERROR;
4773 }
4774
4775 /*
4776 * If this is a differential board and a single-ended device
4777 * is attached to one of the connectors, return an error.
4778 */
4779 if ((scsi_cfg1 & DIFF_MODE) && (scsi_cfg1 & DIFF_SENSE) == 0) {
4780 asc_dvc->err_code |= ASC_IERR_SINGLE_END_DEVICE;
4781 return ADV_ERROR;
4782 }
4783
4784 /*
4785 * If automatic termination control is enabled, then set the
4786 * termination value based on a table listed in a_condor.h.
4787 *
4788 * If manual termination was specified with an EEPROM setting
4789 * then 'termination' was set-up in AdvInitFrom3550EEPROM() and
4790 * is ready to be 'ored' into SCSI_CFG1.
4791 */
4792 if (asc_dvc->cfg->termination == 0) {
4793 /*
4794 * The software always controls termination by setting TERM_CTL_SEL.
4795 * If TERM_CTL_SEL were set to 0, the hardware would set termination.
4796 */
4797 asc_dvc->cfg->termination |= TERM_CTL_SEL;
4798
4799 switch (scsi_cfg1 & CABLE_DETECT) {
4800 /* TERM_CTL_H: on, TERM_CTL_L: on */
4801 case 0x3:
4802 case 0x7:
4803 case 0xB:
4804 case 0xD:
4805 case 0xE:
4806 case 0xF:
4807 asc_dvc->cfg->termination |= (TERM_CTL_H | TERM_CTL_L);
4808 break;
4809
4810 /* TERM_CTL_H: on, TERM_CTL_L: off */
4811 case 0x1:
4812 case 0x5:
4813 case 0x9:
4814 case 0xA:
4815 case 0xC:
4816 asc_dvc->cfg->termination |= TERM_CTL_H;
4817 break;
4818
4819 /* TERM_CTL_H: off, TERM_CTL_L: off */
4820 case 0x2:
4821 case 0x6:
4822 break;
4823 }
4824 }
4825
4826 /*
4827 * Clear any set TERM_CTL_H and TERM_CTL_L bits.
4828 */
4829 scsi_cfg1 &= ~TERM_CTL;
4830
4831 /*
4832 * Invert the TERM_CTL_H and TERM_CTL_L bits and then
4833 * set 'scsi_cfg1'. The TERM_POL bit does not need to be
4834 * referenced, because the hardware internally inverts
4835 * the Termination High and Low bits if TERM_POL is set.
4836 */
4837 scsi_cfg1 |= (TERM_CTL_SEL | (~asc_dvc->cfg->termination & TERM_CTL));
4838
4839 /*
4840 * Set SCSI_CFG1 Microcode Default Value
4841 *
4842 * Set filter value and possibly modified termination control
4843 * bits in the Microcode SCSI_CFG1 Register Value.
4844 *
4845 * The microcode will set the SCSI_CFG1 register using this value
4846 * after it is started below.
4847 */
4848 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1,
4849 FLTR_DISABLE | scsi_cfg1);
4850
4851 /*
4852 * Set MEM_CFG Microcode Default Value
4853 *
4854 * The microcode will set the MEM_CFG register using this value
4855 * after it is started below.
4856 *
4857 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
4858 * are defined.
4859 *
4860 * ASC-3550 has 8KB internal memory.
4861 */
4862 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
4863 BIOS_EN | RAM_SZ_8KB);
4864
4865 /*
4866 * Set SEL_MASK Microcode Default Value
4867 *
4868 * The microcode will set the SEL_MASK register using this value
4869 * after it is started below.
4870 */
4871 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
4872 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
4873
4874 AdvBuildCarrierFreelist(asc_dvc);
4875
4876 /*
4877 * Set-up the Host->RISC Initiator Command Queue (ICQ).
4878 */
4879
98b96a7d
HR
4880 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
4881 if (!asc_dvc->icq_sp) {
51219358
MW
4882 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4883 return ADV_ERROR;
4884 }
51219358
MW
4885
4886 /*
4887 * Set RISC ICQ physical address start value.
4888 */
4889 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
4890
4891 /*
4892 * Set-up the RISC->Host Initiator Response Queue (IRQ).
4893 */
98b96a7d
HR
4894 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
4895 if (!asc_dvc->irq_sp) {
51219358
MW
4896 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4897 return ADV_ERROR;
4898 }
51219358
MW
4899
4900 /*
4901 * Set RISC IRQ physical address start value.
4902 */
4903 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
4904 asc_dvc->carr_pending_cnt = 0;
4905
4906 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
4907 (ADV_INTR_ENABLE_HOST_INTR |
4908 ADV_INTR_ENABLE_GLOBAL_INTR));
4909
4910 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
4911 AdvWriteWordRegister(iop_base, IOPW_PC, word);
4912
4913 /* finally, finally, gentlemen, start your engine */
4914 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
4915
4916 /*
4917 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
4918 * Resets should be performed. The RISC has to be running
4919 * to issue a SCSI Bus Reset.
4920 */
4921 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
4922 /*
4923 * If the BIOS Signature is present in memory, restore the
4924 * BIOS Handshake Configuration Table and do not perform
4925 * a SCSI Bus Reset.
4926 */
4927 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
4928 0x55AA) {
4929 /*
4930 * Restore per TID negotiated values.
4931 */
4932 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4933 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4934 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
4935 tagqng_able);
4936 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4937 AdvWriteByteLram(iop_base,
4938 ASC_MC_NUMBER_OF_MAX_CMD + tid,
4939 max_cmd[tid]);
4940 }
4941 } else {
4942 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
4943 warn_code = ASC_WARN_BUSRESET_ERROR;
4944 }
4945 }
4946 }
4947
4948 return warn_code;
4949}
4950
4951/*
4952 * Initialize the ASC-38C0800.
4953 *
4954 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4955 *
4956 * For a non-fatal error return a warning code. If there are no warnings
4957 * then 0 is returned.
4958 *
4959 * Needed after initialization for error recovery.
4960 */
4961static int AdvInitAsc38C0800Driver(ADV_DVC_VAR *asc_dvc)
4962{
989bb5f5
JSR
4963 const struct firmware *fw;
4964 const char fwname[] = "advansys/38C0800.bin";
51219358
MW
4965 AdvPortAddr iop_base;
4966 ushort warn_code;
4967 int begin_addr;
4968 int end_addr;
4969 ushort code_sum;
4970 int word;
4971 int i;
989bb5f5
JSR
4972 int err;
4973 unsigned long chksum;
51219358
MW
4974 ushort scsi_cfg1;
4975 uchar byte;
4976 uchar tid;
4977 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
4978 ushort wdtr_able, sdtr_able, tagqng_able;
4979 uchar max_cmd[ADV_MAX_TID + 1];
4980
4981 /* If there is already an error, don't continue. */
4982 if (asc_dvc->err_code != 0)
4983 return ADV_ERROR;
4984
4985 /*
4986 * The caller must set 'chip_type' to ADV_CHIP_ASC38C0800.
4987 */
4988 if (asc_dvc->chip_type != ADV_CHIP_ASC38C0800) {
4989 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
4990 return ADV_ERROR;
4991 }
4992
4993 warn_code = 0;
4994 iop_base = asc_dvc->iop_base;
4995
4996 /*
4997 * Save the RISC memory BIOS region before writing the microcode.
4998 * The BIOS may already be loaded and using its RISC LRAM region
4999 * so its region must be saved and restored.
5000 *
5001 * Note: This code makes the assumption, which is currently true,
5002 * that a chip reset does not clear RISC LRAM.
5003 */
5004 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5005 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5006 bios_mem[i]);
5007 }
5008
5009 /*
5010 * Save current per TID negotiated values.
5011 */
5012 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5013 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5014 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5015 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5016 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5017 max_cmd[tid]);
5018 }
5019
5020 /*
5021 * RAM BIST (RAM Built-In Self Test)
5022 *
5023 * Address : I/O base + offset 0x38h register (byte).
5024 * Function: Bit 7-6(RW) : RAM mode
5025 * Normal Mode : 0x00
5026 * Pre-test Mode : 0x40
5027 * RAM Test Mode : 0x80
5028 * Bit 5 : unused
5029 * Bit 4(RO) : Done bit
5030 * Bit 3-0(RO) : Status
5031 * Host Error : 0x08
5032 * Int_RAM Error : 0x04
5033 * RISC Error : 0x02
5034 * SCSI Error : 0x01
5035 * No Error : 0x00
5036 *
5037 * Note: RAM BIST code should be put right here, before loading the
5038 * microcode and after saving the RISC memory BIOS region.
5039 */
5040
5041 /*
5042 * LRAM Pre-test
5043 *
5044 * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
5045 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
5046 * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5047 * to NORMAL_MODE, return an error too.
5048 */
5049 for (i = 0; i < 2; i++) {
5050 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
5051 mdelay(10); /* Wait for 10ms before reading back. */
5052 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5053 if ((byte & RAM_TEST_DONE) == 0
5054 || (byte & 0x0F) != PRE_TEST_VALUE) {
5055 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5056 return ADV_ERROR;
5057 }
5058
5059 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5060 mdelay(10); /* Wait for 10ms before reading back. */
5061 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5062 != NORMAL_VALUE) {
5063 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5064 return ADV_ERROR;
5065 }
5066 }
5067
5068 /*
5069 * LRAM Test - It takes about 1.5 ms to run through the test.
5070 *
5071 * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5072 * If Done bit not set or Status not 0, save register byte, set the
5073 * err_code, and return an error.
5074 */
5075 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
5076 mdelay(10); /* Wait for 10ms before checking status. */
5077
5078 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5079 if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5080 /* Get here if Done bit not set or Status not 0. */
5081 asc_dvc->bist_err_code = byte; /* for BIOS display message */
5082 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
5083 return ADV_ERROR;
5084 }
5085
5086 /* We need to reset back to normal mode after LRAM test passes. */
5087 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5088
989bb5f5
JSR
5089 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5090 if (err) {
5091 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5092 fwname, err);
cf747445 5093 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
5094 return err;
5095 }
5096 if (fw->size < 4) {
5097 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5098 fw->size, fwname);
5099 release_firmware(fw);
cf747445 5100 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
5101 return -EINVAL;
5102 }
5103 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5104 (fw->data[1] << 8) | fw->data[0];
5105 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5106 fw->size - 4, ADV_38C0800_MEMSIZE,
5107 chksum);
5108 release_firmware(fw);
51219358
MW
5109 if (asc_dvc->err_code)
5110 return ADV_ERROR;
5111
5112 /*
5113 * Restore the RISC memory BIOS region.
5114 */
5115 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5116 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5117 bios_mem[i]);
5118 }
5119
5120 /*
5121 * Calculate and write the microcode code checksum to the microcode
5122 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5123 */
5124 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5125 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5126 code_sum = 0;
5127 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5128 for (word = begin_addr; word < end_addr; word += 2) {
5129 code_sum += AdvReadWordAutoIncLram(iop_base);
5130 }
5131 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
5132
5133 /*
5134 * Read microcode version and date.
5135 */
5136 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5137 asc_dvc->cfg->mcode_date);
5138 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5139 asc_dvc->cfg->mcode_version);
5140
5141 /*
5142 * Set the chip type to indicate the ASC38C0800.
5143 */
5144 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C0800);
5145
5146 /*
5147 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5148 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5149 * cable detection and then we are able to read C_DET[3:0].
5150 *
5151 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5152 * Microcode Default Value' section below.
5153 */
5154 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5155 AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5156 scsi_cfg1 | DIS_TERM_DRV);
5157
5158 /*
5159 * If the PCI Configuration Command Register "Parity Error Response
5160 * Control" Bit was clear (0), then set the microcode variable
5161 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5162 * to ignore DMA parity errors.
5163 */
5164 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5165 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5166 word |= CONTROL_FLAG_IGNORE_PERR;
5167 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5168 }
5169
5170 /*
5171 * For ASC-38C0800, set FIFO_THRESH_80B [6:4] bits and START_CTL_TH [3:2]
5172 * bits for the default FIFO threshold.
5173 *
5174 * Note: ASC-38C0800 FIFO threshold has been changed to 256 bytes.
5175 *
5176 * For DMA Errata #4 set the BC_THRESH_ENB bit.
5177 */
5178 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5179 BC_THRESH_ENB | FIFO_THRESH_80B | START_CTL_TH |
5180 READ_CMD_MRM);
5181
5182 /*
5183 * Microcode operating variables for WDTR, SDTR, and command tag
5184 * queuing will be set in slave_configure() based on what a
5185 * device reports it is capable of in Inquiry byte 7.
5186 *
5187 * If SCSI Bus Resets have been disabled, then directly set
5188 * SDTR and WDTR from the EEPROM configuration. This will allow
5189 * the BIOS and warm boot to work without a SCSI bus hang on
5190 * the Inquiry caused by host and target mismatched DTR values.
5191 * Without the SCSI Bus Reset, before an Inquiry a device can't
5192 * be assumed to be in Asynchronous, Narrow mode.
5193 */
5194 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5195 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5196 asc_dvc->wdtr_able);
5197 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5198 asc_dvc->sdtr_able);
5199 }
5200
5201 /*
5202 * Set microcode operating variables for DISC and SDTR_SPEED1,
5203 * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5204 * configuration values.
5205 *
5206 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5207 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5208 * without determining here whether the device supports SDTR.
5209 */
5210 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5211 asc_dvc->cfg->disc_enable);
5212 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5213 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5214 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5215 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
5216
5217 /*
5218 * Set SCSI_CFG0 Microcode Default Value.
5219 *
5220 * The microcode will set the SCSI_CFG0 register using this value
5221 * after it is started below.
5222 */
5223 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5224 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5225 asc_dvc->chip_scsi_id);
5226
5227 /*
5228 * Determine SCSI_CFG1 Microcode Default Value.
5229 *
5230 * The microcode will set the SCSI_CFG1 register using this value
5231 * after it is started below.
5232 */
5233
5234 /* Read current SCSI_CFG1 Register value. */
5235 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5236
5237 /*
5238 * If the internal narrow cable is reversed all of the SCSI_CTRL
5239 * register signals will be set. Check for and return an error if
5240 * this condition is found.
5241 */
5242 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5243 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5244 return ADV_ERROR;
5245 }
5246
5247 /*
5248 * All kind of combinations of devices attached to one of four
5249 * connectors are acceptable except HVD device attached. For example,
5250 * LVD device can be attached to SE connector while SE device attached
5251 * to LVD connector. If LVD device attached to SE connector, it only
5252 * runs up to Ultra speed.
5253 *
5254 * If an HVD device is attached to one of LVD connectors, return an
5255 * error. However, there is no way to detect HVD device attached to
5256 * SE connectors.
5257 */
5258 if (scsi_cfg1 & HVD) {
5259 asc_dvc->err_code = ASC_IERR_HVD_DEVICE;
5260 return ADV_ERROR;
5261 }
5262
5263 /*
5264 * If either SE or LVD automatic termination control is enabled, then
5265 * set the termination value based on a table listed in a_condor.h.
5266 *
5267 * If manual termination was specified with an EEPROM setting then
5268 * 'termination' was set-up in AdvInitFrom38C0800EEPROM() and is ready
5269 * to be 'ored' into SCSI_CFG1.
5270 */
5271 if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
5272 /* SE automatic termination control is enabled. */
5273 switch (scsi_cfg1 & C_DET_SE) {
5274 /* TERM_SE_HI: on, TERM_SE_LO: on */
5275 case 0x1:
5276 case 0x2:
5277 case 0x3:
5278 asc_dvc->cfg->termination |= TERM_SE;
5279 break;
5280
5281 /* TERM_SE_HI: on, TERM_SE_LO: off */
5282 case 0x0:
5283 asc_dvc->cfg->termination |= TERM_SE_HI;
5284 break;
5285 }
5286 }
5287
5288 if ((asc_dvc->cfg->termination & TERM_LVD) == 0) {
5289 /* LVD automatic termination control is enabled. */
5290 switch (scsi_cfg1 & C_DET_LVD) {
5291 /* TERM_LVD_HI: on, TERM_LVD_LO: on */
5292 case 0x4:
5293 case 0x8:
5294 case 0xC:
5295 asc_dvc->cfg->termination |= TERM_LVD;
5296 break;
5297
5298 /* TERM_LVD_HI: off, TERM_LVD_LO: off */
5299 case 0x0:
5300 break;
5301 }
5302 }
5303
5304 /*
5305 * Clear any set TERM_SE and TERM_LVD bits.
5306 */
5307 scsi_cfg1 &= (~TERM_SE & ~TERM_LVD);
5308
5309 /*
5310 * Invert the TERM_SE and TERM_LVD bits and then set 'scsi_cfg1'.
5311 */
5312 scsi_cfg1 |= (~asc_dvc->cfg->termination & 0xF0);
5313
5314 /*
5315 * Clear BIG_ENDIAN, DIS_TERM_DRV, Terminator Polarity and HVD/LVD/SE
5316 * bits and set possibly modified termination control bits in the
5317 * Microcode SCSI_CFG1 Register Value.
5318 */
5319 scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL & ~HVD_LVD_SE);
5320
5321 /*
5322 * Set SCSI_CFG1 Microcode Default Value
5323 *
5324 * Set possibly modified termination control and reset DIS_TERM_DRV
5325 * bits in the Microcode SCSI_CFG1 Register Value.
5326 *
5327 * The microcode will set the SCSI_CFG1 register using this value
5328 * after it is started below.
5329 */
5330 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
5331
5332 /*
5333 * Set MEM_CFG Microcode Default Value
5334 *
5335 * The microcode will set the MEM_CFG register using this value
5336 * after it is started below.
5337 *
5338 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5339 * are defined.
5340 *
5341 * ASC-38C0800 has 16KB internal memory.
5342 */
5343 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5344 BIOS_EN | RAM_SZ_16KB);
5345
5346 /*
5347 * Set SEL_MASK Microcode Default Value
5348 *
5349 * The microcode will set the SEL_MASK register using this value
5350 * after it is started below.
5351 */
5352 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5353 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
5354
5355 AdvBuildCarrierFreelist(asc_dvc);
5356
5357 /*
5358 * Set-up the Host->RISC Initiator Command Queue (ICQ).
5359 */
5360
98b96a7d
HR
5361 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5362 if (!asc_dvc->icq_sp) {
5363 ASC_DBG(0, "Failed to get ICQ carrier\n");
51219358
MW
5364 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5365 return ADV_ERROR;
5366 }
51219358
MW
5367
5368 /*
5369 * Set RISC ICQ physical address start value.
5370 * carr_pa is LE, must be native before write
5371 */
5372 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
5373
5374 /*
5375 * Set-up the RISC->Host Initiator Response Queue (IRQ).
5376 */
98b96a7d
HR
5377 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5378 if (!asc_dvc->irq_sp) {
5379 ASC_DBG(0, "Failed to get IRQ carrier\n");
51219358
MW
5380 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5381 return ADV_ERROR;
5382 }
51219358
MW
5383
5384 /*
5385 * Set RISC IRQ physical address start value.
5386 *
5387 * carr_pa is LE, must be native before write *
5388 */
5389 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5390 asc_dvc->carr_pending_cnt = 0;
5391
5392 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5393 (ADV_INTR_ENABLE_HOST_INTR |
5394 ADV_INTR_ENABLE_GLOBAL_INTR));
5395
5396 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5397 AdvWriteWordRegister(iop_base, IOPW_PC, word);
5398
5399 /* finally, finally, gentlemen, start your engine */
5400 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
5401
5402 /*
5403 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5404 * Resets should be performed. The RISC has to be running
5405 * to issue a SCSI Bus Reset.
5406 */
5407 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5408 /*
5409 * If the BIOS Signature is present in memory, restore the
5410 * BIOS Handshake Configuration Table and do not perform
5411 * a SCSI Bus Reset.
5412 */
5413 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5414 0x55AA) {
5415 /*
5416 * Restore per TID negotiated values.
5417 */
5418 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5419 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5420 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5421 tagqng_able);
5422 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5423 AdvWriteByteLram(iop_base,
5424 ASC_MC_NUMBER_OF_MAX_CMD + tid,
5425 max_cmd[tid]);
5426 }
5427 } else {
5428 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5429 warn_code = ASC_WARN_BUSRESET_ERROR;
5430 }
5431 }
5432 }
5433
5434 return warn_code;
5435}
5436
5437/*
5438 * Initialize the ASC-38C1600.
5439 *
5440 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
5441 *
5442 * For a non-fatal error return a warning code. If there are no warnings
5443 * then 0 is returned.
5444 *
5445 * Needed after initialization for error recovery.
5446 */
5447static int AdvInitAsc38C1600Driver(ADV_DVC_VAR *asc_dvc)
5448{
989bb5f5
JSR
5449 const struct firmware *fw;
5450 const char fwname[] = "advansys/38C1600.bin";
51219358
MW
5451 AdvPortAddr iop_base;
5452 ushort warn_code;
5453 int begin_addr;
5454 int end_addr;
5455 ushort code_sum;
5456 long word;
5457 int i;
989bb5f5
JSR
5458 int err;
5459 unsigned long chksum;
51219358
MW
5460 ushort scsi_cfg1;
5461 uchar byte;
5462 uchar tid;
5463 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
5464 ushort wdtr_able, sdtr_able, ppr_able, tagqng_able;
5465 uchar max_cmd[ASC_MAX_TID + 1];
5466
5467 /* If there is already an error, don't continue. */
5468 if (asc_dvc->err_code != 0) {
5469 return ADV_ERROR;
5470 }
5471
5472 /*
5473 * The caller must set 'chip_type' to ADV_CHIP_ASC38C1600.
5474 */
5475 if (asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
5476 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
5477 return ADV_ERROR;
5478 }
5479
5480 warn_code = 0;
5481 iop_base = asc_dvc->iop_base;
5482
5483 /*
5484 * Save the RISC memory BIOS region before writing the microcode.
5485 * The BIOS may already be loaded and using its RISC LRAM region
5486 * so its region must be saved and restored.
5487 *
5488 * Note: This code makes the assumption, which is currently true,
5489 * that a chip reset does not clear RISC LRAM.
5490 */
5491 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5492 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5493 bios_mem[i]);
5494 }
5495
5496 /*
5497 * Save current per TID negotiated values.
5498 */
5499 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5500 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5501 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5502 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5503 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5504 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5505 max_cmd[tid]);
5506 }
5507
5508 /*
5509 * RAM BIST (Built-In Self Test)
5510 *
5511 * Address : I/O base + offset 0x38h register (byte).
5512 * Function: Bit 7-6(RW) : RAM mode
5513 * Normal Mode : 0x00
5514 * Pre-test Mode : 0x40
5515 * RAM Test Mode : 0x80
5516 * Bit 5 : unused
5517 * Bit 4(RO) : Done bit
5518 * Bit 3-0(RO) : Status
5519 * Host Error : 0x08
5520 * Int_RAM Error : 0x04
5521 * RISC Error : 0x02
5522 * SCSI Error : 0x01
5523 * No Error : 0x00
5524 *
5525 * Note: RAM BIST code should be put right here, before loading the
5526 * microcode and after saving the RISC memory BIOS region.
5527 */
5528
5529 /*
5530 * LRAM Pre-test
5531 *
5532 * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
5533 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
5534 * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5535 * to NORMAL_MODE, return an error too.
5536 */
5537 for (i = 0; i < 2; i++) {
5538 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
5539 mdelay(10); /* Wait for 10ms before reading back. */
5540 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5541 if ((byte & RAM_TEST_DONE) == 0
5542 || (byte & 0x0F) != PRE_TEST_VALUE) {
5543 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5544 return ADV_ERROR;
5545 }
5546
5547 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5548 mdelay(10); /* Wait for 10ms before reading back. */
5549 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5550 != NORMAL_VALUE) {
5551 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
5552 return ADV_ERROR;
5553 }
5554 }
5555
5556 /*
5557 * LRAM Test - It takes about 1.5 ms to run through the test.
5558 *
5559 * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5560 * If Done bit not set or Status not 0, save register byte, set the
5561 * err_code, and return an error.
5562 */
5563 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
5564 mdelay(10); /* Wait for 10ms before checking status. */
5565
5566 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5567 if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5568 /* Get here if Done bit not set or Status not 0. */
5569 asc_dvc->bist_err_code = byte; /* for BIOS display message */
5570 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
5571 return ADV_ERROR;
5572 }
5573
5574 /* We need to reset back to normal mode after LRAM test passes. */
5575 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
5576
989bb5f5
JSR
5577 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5578 if (err) {
5579 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5580 fwname, err);
cf747445 5581 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
5582 return err;
5583 }
5584 if (fw->size < 4) {
5585 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5586 fw->size, fwname);
5587 release_firmware(fw);
cf747445 5588 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
989bb5f5
JSR
5589 return -EINVAL;
5590 }
5591 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5592 (fw->data[1] << 8) | fw->data[0];
5593 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5594 fw->size - 4, ADV_38C1600_MEMSIZE,
5595 chksum);
5596 release_firmware(fw);
51219358
MW
5597 if (asc_dvc->err_code)
5598 return ADV_ERROR;
5599
5600 /*
5601 * Restore the RISC memory BIOS region.
5602 */
5603 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5604 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5605 bios_mem[i]);
5606 }
5607
5608 /*
5609 * Calculate and write the microcode code checksum to the microcode
5610 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5611 */
5612 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5613 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5614 code_sum = 0;
5615 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5616 for (word = begin_addr; word < end_addr; word += 2) {
5617 code_sum += AdvReadWordAutoIncLram(iop_base);
5618 }
5619 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
5620
5621 /*
5622 * Read microcode version and date.
5623 */
5624 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5625 asc_dvc->cfg->mcode_date);
5626 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5627 asc_dvc->cfg->mcode_version);
5628
5629 /*
5630 * Set the chip type to indicate the ASC38C1600.
5631 */
5632 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C1600);
5633
5634 /*
5635 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5636 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5637 * cable detection and then we are able to read C_DET[3:0].
5638 *
5639 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5640 * Microcode Default Value' section below.
5641 */
5642 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5643 AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5644 scsi_cfg1 | DIS_TERM_DRV);
5645
5646 /*
5647 * If the PCI Configuration Command Register "Parity Error Response
5648 * Control" Bit was clear (0), then set the microcode variable
5649 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5650 * to ignore DMA parity errors.
5651 */
5652 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5653 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5654 word |= CONTROL_FLAG_IGNORE_PERR;
5655 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5656 }
5657
5658 /*
5659 * If the BIOS control flag AIPP (Asynchronous Information
5660 * Phase Protection) disable bit is not set, then set the firmware
5661 * 'control_flag' CONTROL_FLAG_ENABLE_AIPP bit to enable
5662 * AIPP checking and encoding.
5663 */
5664 if ((asc_dvc->bios_ctrl & BIOS_CTRL_AIPP_DIS) == 0) {
5665 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5666 word |= CONTROL_FLAG_ENABLE_AIPP;
5667 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5668 }
5669
5670 /*
5671 * For ASC-38C1600 use DMA_CFG0 default values: FIFO_THRESH_80B [6:4],
5672 * and START_CTL_TH [3:2].
5673 */
5674 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5675 FIFO_THRESH_80B | START_CTL_TH | READ_CMD_MRM);
5676
5677 /*
5678 * Microcode operating variables for WDTR, SDTR, and command tag
5679 * queuing will be set in slave_configure() based on what a
5680 * device reports it is capable of in Inquiry byte 7.
5681 *
5682 * If SCSI Bus Resets have been disabled, then directly set
5683 * SDTR and WDTR from the EEPROM configuration. This will allow
5684 * the BIOS and warm boot to work without a SCSI bus hang on
5685 * the Inquiry caused by host and target mismatched DTR values.
5686 * Without the SCSI Bus Reset, before an Inquiry a device can't
5687 * be assumed to be in Asynchronous, Narrow mode.
5688 */
5689 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5690 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5691 asc_dvc->wdtr_able);
5692 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5693 asc_dvc->sdtr_able);
5694 }
5695
5696 /*
5697 * Set microcode operating variables for DISC and SDTR_SPEED1,
5698 * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5699 * configuration values.
5700 *
5701 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5702 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5703 * without determining here whether the device supports SDTR.
5704 */
5705 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5706 asc_dvc->cfg->disc_enable);
5707 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5708 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5709 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5710 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
5711
5712 /*
5713 * Set SCSI_CFG0 Microcode Default Value.
5714 *
5715 * The microcode will set the SCSI_CFG0 register using this value
5716 * after it is started below.
5717 */
5718 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5719 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5720 asc_dvc->chip_scsi_id);
5721
5722 /*
5723 * Calculate SCSI_CFG1 Microcode Default Value.
5724 *
5725 * The microcode will set the SCSI_CFG1 register using this value
5726 * after it is started below.
5727 *
5728 * Each ASC-38C1600 function has only two cable detect bits.
5729 * The bus mode override bits are in IOPB_SOFT_OVER_WR.
5730 */
5731 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5732
5733 /*
5734 * If the cable is reversed all of the SCSI_CTRL register signals
5735 * will be set. Check for and return an error if this condition is
5736 * found.
5737 */
5738 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5739 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5740 return ADV_ERROR;
5741 }
5742
5743 /*
5744 * Each ASC-38C1600 function has two connectors. Only an HVD device
5745 * can not be connected to either connector. An LVD device or SE device
5746 * may be connected to either connecor. If an SE device is connected,
5747 * then at most Ultra speed (20 Mhz) can be used on both connectors.
5748 *
5749 * If an HVD device is attached, return an error.
5750 */
5751 if (scsi_cfg1 & HVD) {
5752 asc_dvc->err_code |= ASC_IERR_HVD_DEVICE;
5753 return ADV_ERROR;
5754 }
5755
5756 /*
5757 * Each function in the ASC-38C1600 uses only the SE cable detect and
5758 * termination because there are two connectors for each function. Each
5759 * function may use either LVD or SE mode. Corresponding the SE automatic
5760 * termination control EEPROM bits are used for each function. Each
5761 * function has its own EEPROM. If SE automatic control is enabled for
5762 * the function, then set the termination value based on a table listed
5763 * in a_condor.h.
5764 *
5765 * If manual termination is specified in the EEPROM for the function,
5766 * then 'termination' was set-up in AscInitFrom38C1600EEPROM() and is
5767 * ready to be 'ored' into SCSI_CFG1.
5768 */
5769 if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
5770 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
5771 /* SE automatic termination control is enabled. */
5772 switch (scsi_cfg1 & C_DET_SE) {
5773 /* TERM_SE_HI: on, TERM_SE_LO: on */
5774 case 0x1:
5775 case 0x2:
5776 case 0x3:
5777 asc_dvc->cfg->termination |= TERM_SE;
5778 break;
5779
5780 case 0x0:
5781 if (PCI_FUNC(pdev->devfn) == 0) {
5782 /* Function 0 - TERM_SE_HI: off, TERM_SE_LO: off */
5783 } else {
5784 /* Function 1 - TERM_SE_HI: on, TERM_SE_LO: off */
5785 asc_dvc->cfg->termination |= TERM_SE_HI;
5786 }
5787 break;
5788 }
5789 }
5790
5791 /*
5792 * Clear any set TERM_SE bits.
5793 */
5794 scsi_cfg1 &= ~TERM_SE;
5795
5796 /*
5797 * Invert the TERM_SE bits and then set 'scsi_cfg1'.
5798 */
5799 scsi_cfg1 |= (~asc_dvc->cfg->termination & TERM_SE);
5800
5801 /*
5802 * Clear Big Endian and Terminator Polarity bits and set possibly
5803 * modified termination control bits in the Microcode SCSI_CFG1
5804 * Register Value.
5805 *
5806 * Big Endian bit is not used even on big endian machines.
5807 */
5808 scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL);
5809
5810 /*
5811 * Set SCSI_CFG1 Microcode Default Value
5812 *
5813 * Set possibly modified termination control bits in the Microcode
5814 * SCSI_CFG1 Register Value.
5815 *
5816 * The microcode will set the SCSI_CFG1 register using this value
5817 * after it is started below.
5818 */
5819 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
5820
5821 /*
5822 * Set MEM_CFG Microcode Default Value
5823 *
5824 * The microcode will set the MEM_CFG register using this value
5825 * after it is started below.
5826 *
5827 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5828 * are defined.
5829 *
5830 * ASC-38C1600 has 32KB internal memory.
5831 *
5832 * XXX - Since ASC38C1600 Rev.3 has a Local RAM failure issue, we come
5833 * out a special 16K Adv Library and Microcode version. After the issue
5834 * resolved, we should turn back to the 32K support. Both a_condor.h and
5835 * mcode.sas files also need to be updated.
5836 *
5837 * AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5838 * BIOS_EN | RAM_SZ_32KB);
5839 */
5840 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5841 BIOS_EN | RAM_SZ_16KB);
5842
5843 /*
5844 * Set SEL_MASK Microcode Default Value
5845 *
5846 * The microcode will set the SEL_MASK register using this value
5847 * after it is started below.
5848 */
5849 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5850 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
5851
5852 AdvBuildCarrierFreelist(asc_dvc);
5853
5854 /*
5855 * Set-up the Host->RISC Initiator Command Queue (ICQ).
5856 */
98b96a7d
HR
5857 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5858 if (!asc_dvc->icq_sp) {
51219358
MW
5859 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5860 return ADV_ERROR;
5861 }
51219358
MW
5862
5863 /*
5864 * Set RISC ICQ physical address start value. Initialize the
5865 * COMMA register to the same value otherwise the RISC will
5866 * prematurely detect a command is available.
5867 */
5868 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
5869 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
5870 le32_to_cpu(asc_dvc->icq_sp->carr_pa));
5871
5872 /*
5873 * Set-up the RISC->Host Initiator Response Queue (IRQ).
5874 */
98b96a7d
HR
5875 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5876 if (!asc_dvc->irq_sp) {
51219358
MW
5877 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5878 return ADV_ERROR;
5879 }
51219358
MW
5880
5881 /*
5882 * Set RISC IRQ physical address start value.
5883 */
5884 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5885 asc_dvc->carr_pending_cnt = 0;
5886
5887 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5888 (ADV_INTR_ENABLE_HOST_INTR |
5889 ADV_INTR_ENABLE_GLOBAL_INTR));
5890 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5891 AdvWriteWordRegister(iop_base, IOPW_PC, word);
5892
5893 /* finally, finally, gentlemen, start your engine */
5894 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
5895
5896 /*
5897 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5898 * Resets should be performed. The RISC has to be running
5899 * to issue a SCSI Bus Reset.
5900 */
5901 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5902 /*
5903 * If the BIOS Signature is present in memory, restore the
5904 * per TID microcode operating variables.
5905 */
5906 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5907 0x55AA) {
5908 /*
5909 * Restore per TID negotiated values.
5910 */
5911 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5912 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5913 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5914 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5915 tagqng_able);
5916 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5917 AdvWriteByteLram(iop_base,
5918 ASC_MC_NUMBER_OF_MAX_CMD + tid,
5919 max_cmd[tid]);
5920 }
5921 } else {
5922 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5923 warn_code = ASC_WARN_BUSRESET_ERROR;
5924 }
5925 }
5926 }
5927
5928 return warn_code;
5929}
5930
5931/*
5932 * Reset chip and SCSI Bus.
5933 *
5934 * Return Value:
5935 * ADV_TRUE(1) - Chip re-initialization and SCSI Bus Reset successful.
5936 * ADV_FALSE(0) - Chip re-initialization and SCSI Bus Reset failure.
5937 */
5938static int AdvResetChipAndSB(ADV_DVC_VAR *asc_dvc)
5939{
5940 int status;
5941 ushort wdtr_able, sdtr_able, tagqng_able;
5942 ushort ppr_able = 0;
5943 uchar tid, max_cmd[ADV_MAX_TID + 1];
5944 AdvPortAddr iop_base;
5945 ushort bios_sig;
5946
5947 iop_base = asc_dvc->iop_base;
5948
5949 /*
5950 * Save current per TID negotiated values.
5951 */
5952 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5953 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5954 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5955 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5956 }
5957 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5958 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5959 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5960 max_cmd[tid]);
5961 }
5962
5963 /*
5964 * Force the AdvInitAsc3550/38C0800Driver() function to
5965 * perform a SCSI Bus Reset by clearing the BIOS signature word.
5966 * The initialization functions assumes a SCSI Bus Reset is not
5967 * needed if the BIOS signature word is present.
5968 */
5969 AdvReadWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5970 AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, 0);
5971
5972 /*
5973 * Stop chip and reset it.
5974 */
5975 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_STOP);
5976 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG, ADV_CTRL_REG_CMD_RESET);
5977 mdelay(100);
5978 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
5979 ADV_CTRL_REG_CMD_WR_IO_REG);
5980
5981 /*
5982 * Reset Adv Library error code, if any, and try
5983 * re-initializing the chip.
5984 */
5985 asc_dvc->err_code = 0;
5986 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5987 status = AdvInitAsc38C1600Driver(asc_dvc);
5988 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
5989 status = AdvInitAsc38C0800Driver(asc_dvc);
5990 } else {
5991 status = AdvInitAsc3550Driver(asc_dvc);
5992 }
5993
5994 /* Translate initialization return value to status value. */
5995 if (status == 0) {
5996 status = ADV_TRUE;
5997 } else {
5998 status = ADV_FALSE;
5999 }
6000
6001 /*
6002 * Restore the BIOS signature word.
6003 */
6004 AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
6005
6006 /*
6007 * Restore per TID negotiated values.
6008 */
6009 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
6010 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
6011 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
6012 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
6013 }
6014 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
6015 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
6016 AdvWriteByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
6017 max_cmd[tid]);
6018 }
6019
6020 return status;
6021}
6022
6023/*
6024 * adv_async_callback() - Adv Library asynchronous event callback function.
6025 */
6026static void adv_async_callback(ADV_DVC_VAR *adv_dvc_varp, uchar code)
6027{
6028 switch (code) {
6029 case ADV_ASYNC_SCSI_BUS_RESET_DET:
6030 /*
6031 * The firmware detected a SCSI Bus reset.
6032 */
b352f923 6033 ASC_DBG(0, "ADV_ASYNC_SCSI_BUS_RESET_DET\n");
51219358
MW
6034 break;
6035
6036 case ADV_ASYNC_RDMA_FAILURE:
6037 /*
6038 * Handle RDMA failure by resetting the SCSI Bus and
6039 * possibly the chip if it is unresponsive. Log the error
6040 * with a unique code.
6041 */
b352f923 6042 ASC_DBG(0, "ADV_ASYNC_RDMA_FAILURE\n");
51219358
MW
6043 AdvResetChipAndSB(adv_dvc_varp);
6044 break;
6045
6046 case ADV_HOST_SCSI_BUS_RESET:
6047 /*
6048 * Host generated SCSI bus reset occurred.
6049 */
b352f923 6050 ASC_DBG(0, "ADV_HOST_SCSI_BUS_RESET\n");
51219358
MW
6051 break;
6052
6053 default:
b352f923 6054 ASC_DBG(0, "unknown code 0x%x\n", code);
51219358
MW
6055 break;
6056 }
6057}
6058
6059/*
6060 * adv_isr_callback() - Second Level Interrupt Handler called by AdvISR().
6061 *
6062 * Callback function for the Wide SCSI Adv Library.
6063 */
6064static void adv_isr_callback(ADV_DVC_VAR *adv_dvc_varp, ADV_SCSI_REQ_Q *scsiqp)
6065{
9c17c62a
HR
6066 struct asc_board *boardp = adv_dvc_varp->drv_ptr;
6067 u32 srb_tag;
51219358
MW
6068 adv_req_t *reqp;
6069 adv_sgblk_t *sgblkp;
6070 struct scsi_cmnd *scp;
51219358 6071 ADV_DCNT resid_cnt;
811ddc05 6072 dma_addr_t sense_addr;
51219358 6073
9c17c62a
HR
6074 ASC_DBG(1, "adv_dvc_varp 0x%p, scsiqp 0x%p\n",
6075 adv_dvc_varp, scsiqp);
51219358
MW
6076 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
6077
6078 /*
6079 * Get the adv_req_t structure for the command that has been
6080 * completed. The adv_req_t structure actually contains the
6081 * completed ADV_SCSI_REQ_Q structure.
6082 */
9c17c62a
HR
6083 srb_tag = le32_to_cpu(scsiqp->srb_tag);
6084 scp = scsi_host_find_tag(boardp->shost, scsiqp->srb_tag);
51219358 6085
b352f923 6086 ASC_DBG(1, "scp 0x%p\n", scp);
51219358
MW
6087 if (scp == NULL) {
6088 ASC_PRINT
6089 ("adv_isr_callback: scp is NULL; adv_req_t dropped.\n");
6090 return;
6091 }
6092 ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
6093
9c17c62a
HR
6094 reqp = (adv_req_t *)scp->host_scribble;
6095 ASC_DBG(1, "reqp 0x%lx\n", (ulong)reqp);
6096 if (reqp == NULL) {
6097 ASC_PRINT("adv_isr_callback: reqp is NULL\n");
6098 return;
6099 }
6100 /*
6101 * Remove backreferences to avoid duplicate
6102 * command completions.
6103 */
6104 scp->host_scribble = NULL;
6105 reqp->cmndp = NULL;
51219358 6106
9c17c62a
HR
6107 ASC_STATS(boardp->shost, callback);
6108 ASC_DBG(1, "shost 0x%p\n", boardp->shost);
51219358 6109
811ddc05
HR
6110 sense_addr = le32_to_cpu(scsiqp->sense_addr);
6111 dma_unmap_single(boardp->dev, sense_addr,
6112 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
6113
51219358
MW
6114 /*
6115 * 'done_status' contains the command's ending status.
6116 */
6117 switch (scsiqp->done_status) {
6118 case QD_NO_ERROR:
b352f923 6119 ASC_DBG(2, "QD_NO_ERROR\n");
51219358
MW
6120 scp->result = 0;
6121
6122 /*
6123 * Check for an underrun condition.
6124 *
6125 * If there was no error and an underrun condition, then
6126 * then return the number of underrun bytes.
6127 */
6128 resid_cnt = le32_to_cpu(scsiqp->data_cnt);
52c334e9
MW
6129 if (scsi_bufflen(scp) != 0 && resid_cnt != 0 &&
6130 resid_cnt <= scsi_bufflen(scp)) {
b352f923 6131 ASC_DBG(1, "underrun condition %lu bytes\n",
51219358 6132 (ulong)resid_cnt);
52c334e9 6133 scsi_set_resid(scp, resid_cnt);
51219358
MW
6134 }
6135 break;
6136
6137 case QD_WITH_ERROR:
b352f923 6138 ASC_DBG(2, "QD_WITH_ERROR\n");
51219358
MW
6139 switch (scsiqp->host_status) {
6140 case QHSTA_NO_ERROR:
6141 if (scsiqp->scsi_status == SAM_STAT_CHECK_CONDITION) {
b352f923 6142 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
51219358 6143 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
b80ca4f7 6144 SCSI_SENSE_BUFFERSIZE);
51219358
MW
6145 /*
6146 * Note: The 'status_byte()' macro used by
6147 * target drivers defined in scsi.h shifts the
6148 * status byte returned by host drivers right
6149 * by 1 bit. This is why target drivers also
6150 * use right shifted status byte definitions.
6151 * For instance target drivers use
6152 * CHECK_CONDITION, defined to 0x1, instead of
6153 * the SCSI defined check condition value of
6154 * 0x2. Host drivers are supposed to return
6155 * the status byte as it is defined by SCSI.
6156 */
6157 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
6158 STATUS_BYTE(scsiqp->scsi_status);
6159 } else {
6160 scp->result = STATUS_BYTE(scsiqp->scsi_status);
6161 }
6162 break;
6163
6164 default:
6165 /* Some other QHSTA error occurred. */
b352f923 6166 ASC_DBG(1, "host_status 0x%x\n", scsiqp->host_status);
51219358
MW
6167 scp->result = HOST_BYTE(DID_BAD_TARGET);
6168 break;
6169 }
6170 break;
6171
6172 case QD_ABORTED_BY_HOST:
b352f923 6173 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
51219358
MW
6174 scp->result =
6175 HOST_BYTE(DID_ABORT) | STATUS_BYTE(scsiqp->scsi_status);
6176 break;
6177
6178 default:
b352f923 6179 ASC_DBG(1, "done_status 0x%x\n", scsiqp->done_status);
51219358
MW
6180 scp->result =
6181 HOST_BYTE(DID_ERROR) | STATUS_BYTE(scsiqp->scsi_status);
6182 break;
6183 }
6184
6185 /*
6186 * If the 'init_tidmask' bit isn't already set for the target and the
6187 * current request finished normally, then set the bit for the target
6188 * to indicate that a device is present.
6189 */
6190 if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
6191 scsiqp->done_status == QD_NO_ERROR &&
6192 scsiqp->host_status == QHSTA_NO_ERROR) {
6193 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
6194 }
6195
6196 asc_scsi_done(scp);
6197
6198 /*
6199 * Free all 'adv_sgblk_t' structures allocated for the request.
6200 */
6201 while ((sgblkp = reqp->sgblkp) != NULL) {
6202 /* Remove 'sgblkp' from the request list. */
6203 reqp->sgblkp = sgblkp->next_sgblkp;
6204
0ce53822
HR
6205 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
6206 sgblkp->sg_addr);
51219358
MW
6207 }
6208
b352f923 6209 ASC_DBG(1, "done\n");
51219358
MW
6210}
6211
6212/*
6213 * Adv Library Interrupt Service Routine
6214 *
6215 * This function is called by a driver's interrupt service routine.
6216 * The function disables and re-enables interrupts.
6217 *
6218 * When a microcode idle command is completed, the ADV_DVC_VAR
6219 * 'idle_cmd_done' field is set to ADV_TRUE.
6220 *
6221 * Note: AdvISR() can be called when interrupts are disabled or even
6222 * when there is no hardware interrupt condition present. It will
6223 * always check for completed idle commands and microcode requests.
6224 * This is an important feature that shouldn't be changed because it
6225 * allows commands to be completed from polling mode loops.
6226 *
6227 * Return:
6228 * ADV_TRUE(1) - interrupt was pending
6229 * ADV_FALSE(0) - no interrupt was pending
6230 */
6231static int AdvISR(ADV_DVC_VAR *asc_dvc)
6232{
6233 AdvPortAddr iop_base;
6234 uchar int_stat;
6235 ushort target_bit;
6236 ADV_CARR_T *free_carrp;
6237 ADV_VADDR irq_next_vpa;
6238 ADV_SCSI_REQ_Q *scsiq;
4b47e464 6239 adv_req_t *reqp;
51219358
MW
6240
6241 iop_base = asc_dvc->iop_base;
6242
6243 /* Reading the register clears the interrupt. */
6244 int_stat = AdvReadByteRegister(iop_base, IOPB_INTR_STATUS_REG);
6245
6246 if ((int_stat & (ADV_INTR_STATUS_INTRA | ADV_INTR_STATUS_INTRB |
6247 ADV_INTR_STATUS_INTRC)) == 0) {
6248 return ADV_FALSE;
6249 }
6250
6251 /*
6252 * Notify the driver of an asynchronous microcode condition by
6253 * calling the adv_async_callback function. The function
6254 * is passed the microcode ASC_MC_INTRB_CODE byte value.
6255 */
6256 if (int_stat & ADV_INTR_STATUS_INTRB) {
6257 uchar intrb_code;
6258
6259 AdvReadByteLram(iop_base, ASC_MC_INTRB_CODE, intrb_code);
6260
6261 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
6262 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
6263 if (intrb_code == ADV_ASYNC_CARRIER_READY_FAILURE &&
6264 asc_dvc->carr_pending_cnt != 0) {
6265 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
6266 ADV_TICKLE_A);
6267 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
6268 AdvWriteByteRegister(iop_base,
6269 IOPB_TICKLE,
6270 ADV_TICKLE_NOP);
6271 }
6272 }
6273 }
6274
6275 adv_async_callback(asc_dvc, intrb_code);
6276 }
6277
6278 /*
6279 * Check if the IRQ stopper carrier contains a completed request.
6280 */
6281 while (((irq_next_vpa =
6282 le32_to_cpu(asc_dvc->irq_sp->next_vpa)) & ASC_RQ_DONE) != 0) {
6283 /*
6284 * Get a pointer to the newly completed ADV_SCSI_REQ_Q structure.
6285 * The RISC will have set 'areq_vpa' to a virtual address.
6286 *
6287 * The firmware will have copied the ASC_SCSI_REQ_Q.scsiq_ptr
6288 * field to the carrier ADV_CARR_T.areq_vpa field. The conversion
6289 * below complements the conversion of ASC_SCSI_REQ_Q.scsiq_ptr'
6290 * in AdvExeScsiQueue().
6291 */
4b47e464
HR
6292 u32 pa_offset = le32_to_cpu(asc_dvc->irq_sp->areq_vpa);
6293 ASC_DBG(1, "irq_sp %p areq_vpa %u\n",
6294 asc_dvc->irq_sp, pa_offset);
6295 reqp = adv_get_reqp(asc_dvc, pa_offset);
6296 scsiq = &reqp->scsi_req_q;
51219358
MW
6297
6298 /*
6299 * Request finished with good status and the queue was not
6300 * DMAed to host memory by the firmware. Set all status fields
6301 * to indicate good status.
6302 */
6303 if ((irq_next_vpa & ASC_RQ_GOOD) != 0) {
6304 scsiq->done_status = QD_NO_ERROR;
6305 scsiq->host_status = scsiq->scsi_status = 0;
6306 scsiq->data_cnt = 0L;
6307 }
6308
6309 /*
6310 * Advance the stopper pointer to the next carrier
6311 * ignoring the lower four bits. Free the previous
6312 * stopper carrier.
6313 */
6314 free_carrp = asc_dvc->irq_sp;
98b96a7d
HR
6315 asc_dvc->irq_sp = adv_get_carrier(asc_dvc,
6316 ASC_GET_CARRP(irq_next_vpa));
51219358 6317
98b96a7d 6318 free_carrp->next_vpa = asc_dvc->carr_freelist->carr_va;
51219358
MW
6319 asc_dvc->carr_freelist = free_carrp;
6320 asc_dvc->carr_pending_cnt--;
6321
6322 target_bit = ADV_TID_TO_TIDMASK(scsiq->target_id);
6323
6324 /*
6325 * Clear request microcode control flag.
6326 */
6327 scsiq->cntl = 0;
6328
6329 /*
6330 * Notify the driver of the completed request by passing
6331 * the ADV_SCSI_REQ_Q pointer to its callback function.
6332 */
6333 scsiq->a_flag |= ADV_SCSIQ_DONE;
6334 adv_isr_callback(asc_dvc, scsiq);
6335 /*
6336 * Note: After the driver callback function is called, 'scsiq'
6337 * can no longer be referenced.
6338 *
6339 * Fall through and continue processing other completed
6340 * requests...
6341 */
6342 }
6343 return ADV_TRUE;
6344}
6345
6346static int AscSetLibErrorCode(ASC_DVC_VAR *asc_dvc, ushort err_code)
6347{
6348 if (asc_dvc->err_code == 0) {
6349 asc_dvc->err_code = err_code;
6350 AscWriteLramWord(asc_dvc->iop_base, ASCV_ASCDVC_ERR_CODE_W,
6351 err_code);
6352 }
6353 return err_code;
6354}
6355
6356static void AscAckInterrupt(PortAddr iop_base)
6357{
6358 uchar host_flag;
6359 uchar risc_flag;
6360 ushort loop;
6361
6362 loop = 0;
6363 do {
6364 risc_flag = AscReadLramByte(iop_base, ASCV_RISC_FLAG_B);
6365 if (loop++ > 0x7FFF) {
6366 break;
6367 }
6368 } while ((risc_flag & ASC_RISC_FLAG_GEN_INT) != 0);
6369 host_flag =
6370 AscReadLramByte(iop_base,
6371 ASCV_HOST_FLAG_B) & (~ASC_HOST_FLAG_ACK_INT);
6372 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
6373 (uchar)(host_flag | ASC_HOST_FLAG_ACK_INT));
6374 AscSetChipStatus(iop_base, CIW_INT_ACK);
6375 loop = 0;
6376 while (AscGetChipStatus(iop_base) & CSW_INT_PENDING) {
6377 AscSetChipStatus(iop_base, CIW_INT_ACK);
6378 if (loop++ > 3) {
6379 break;
6380 }
6381 }
6382 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
51219358
MW
6383}
6384
6385static uchar AscGetSynPeriodIndex(ASC_DVC_VAR *asc_dvc, uchar syn_time)
6386{
afbb68c3 6387 const uchar *period_table;
51219358
MW
6388 int max_index;
6389 int min_index;
6390 int i;
6391
6392 period_table = asc_dvc->sdtr_period_tbl;
6393 max_index = (int)asc_dvc->max_sdtr_index;
afbb68c3 6394 min_index = (int)asc_dvc->min_sdtr_index;
51219358
MW
6395 if ((syn_time <= period_table[max_index])) {
6396 for (i = min_index; i < (max_index - 1); i++) {
6397 if (syn_time <= period_table[i]) {
6398 return (uchar)i;
6399 }
6400 }
6401 return (uchar)max_index;
6402 } else {
6403 return (uchar)(max_index + 1);
6404 }
6405}
6406
6407static uchar
6408AscMsgOutSDTR(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar sdtr_offset)
6409{
6410 EXT_MSG sdtr_buf;
6411 uchar sdtr_period_index;
6412 PortAddr iop_base;
6413
6414 iop_base = asc_dvc->iop_base;
6415 sdtr_buf.msg_type = EXTENDED_MESSAGE;
6416 sdtr_buf.msg_len = MS_SDTR_LEN;
6417 sdtr_buf.msg_req = EXTENDED_SDTR;
6418 sdtr_buf.xfer_period = sdtr_period;
6419 sdtr_offset &= ASC_SYN_MAX_OFFSET;
6420 sdtr_buf.req_ack_offset = sdtr_offset;
6421 sdtr_period_index = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
6422 if (sdtr_period_index <= asc_dvc->max_sdtr_index) {
6423 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6424 (uchar *)&sdtr_buf,
6425 sizeof(EXT_MSG) >> 1);
6426 return ((sdtr_period_index << 4) | sdtr_offset);
6427 } else {
6428 sdtr_buf.req_ack_offset = 0;
6429 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6430 (uchar *)&sdtr_buf,
6431 sizeof(EXT_MSG) >> 1);
6432 return 0;
6433 }
6434}
6435
6436static uchar
6437AscCalSDTRData(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar syn_offset)
6438{
6439 uchar byte;
6440 uchar sdtr_period_ix;
6441
6442 sdtr_period_ix = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
afbb68c3 6443 if (sdtr_period_ix > asc_dvc->max_sdtr_index)
51219358 6444 return 0xFF;
51219358
MW
6445 byte = (sdtr_period_ix << 4) | (syn_offset & ASC_SYN_MAX_OFFSET);
6446 return byte;
6447}
6448
6449static int AscSetChipSynRegAtID(PortAddr iop_base, uchar id, uchar sdtr_data)
6450{
6451 ASC_SCSI_BIT_ID_TYPE org_id;
6452 int i;
6453 int sta = TRUE;
6454
6455 AscSetBank(iop_base, 1);
6456 org_id = AscReadChipDvcID(iop_base);
6457 for (i = 0; i <= ASC_MAX_TID; i++) {
6458 if (org_id == (0x01 << i))
6459 break;
6460 }
6461 org_id = (ASC_SCSI_BIT_ID_TYPE) i;
6462 AscWriteChipDvcID(iop_base, id);
6463 if (AscReadChipDvcID(iop_base) == (0x01 << id)) {
6464 AscSetBank(iop_base, 0);
6465 AscSetChipSyn(iop_base, sdtr_data);
6466 if (AscGetChipSyn(iop_base) != sdtr_data) {
6467 sta = FALSE;
6468 }
6469 } else {
6470 sta = FALSE;
6471 }
6472 AscSetBank(iop_base, 1);
6473 AscWriteChipDvcID(iop_base, org_id);
6474 AscSetBank(iop_base, 0);
6475 return (sta);
6476}
6477
6478static void AscSetChipSDTR(PortAddr iop_base, uchar sdtr_data, uchar tid_no)
6479{
6480 AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
6481 AscPutMCodeSDTRDoneAtID(iop_base, tid_no, sdtr_data);
6482}
6483
6484static int AscIsrChipHalted(ASC_DVC_VAR *asc_dvc)
6485{
6486 EXT_MSG ext_msg;
6487 EXT_MSG out_msg;
6488 ushort halt_q_addr;
6489 int sdtr_accept;
6490 ushort int_halt_code;
6491 ASC_SCSI_BIT_ID_TYPE scsi_busy;
6492 ASC_SCSI_BIT_ID_TYPE target_id;
6493 PortAddr iop_base;
6494 uchar tag_code;
6495 uchar q_status;
6496 uchar halt_qp;
6497 uchar sdtr_data;
6498 uchar target_ix;
6499 uchar q_cntl, tid_no;
6500 uchar cur_dvc_qng;
6501 uchar asyn_sdtr;
6502 uchar scsi_status;
d2411495 6503 struct asc_board *boardp;
51219358
MW
6504
6505 BUG_ON(!asc_dvc->drv_ptr);
6506 boardp = asc_dvc->drv_ptr;
6507
6508 iop_base = asc_dvc->iop_base;
6509 int_halt_code = AscReadLramWord(iop_base, ASCV_HALTCODE_W);
6510
6511 halt_qp = AscReadLramByte(iop_base, ASCV_CURCDB_B);
6512 halt_q_addr = ASC_QNO_TO_QADDR(halt_qp);
6513 target_ix = AscReadLramByte(iop_base,
6514 (ushort)(halt_q_addr +
6515 (ushort)ASC_SCSIQ_B_TARGET_IX));
6516 q_cntl = AscReadLramByte(iop_base,
6517 (ushort)(halt_q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6518 tid_no = ASC_TIX_TO_TID(target_ix);
6519 target_id = (uchar)ASC_TID_TO_TARGET_ID(tid_no);
6520 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6521 asyn_sdtr = ASYN_SDTR_DATA_FIX_PCI_REV_AB;
6522 } else {
6523 asyn_sdtr = 0;
6524 }
6525 if (int_halt_code == ASC_HALT_DISABLE_ASYN_USE_SYN_FIX) {
6526 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6527 AscSetChipSDTR(iop_base, 0, tid_no);
6528 boardp->sdtr_data[tid_no] = 0;
6529 }
6530 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6531 return (0);
6532 } else if (int_halt_code == ASC_HALT_ENABLE_ASYN_USE_SYN_FIX) {
6533 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6534 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6535 boardp->sdtr_data[tid_no] = asyn_sdtr;
6536 }
6537 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6538 return (0);
6539 } else if (int_halt_code == ASC_HALT_EXTMSG_IN) {
6540 AscMemWordCopyPtrFromLram(iop_base,
6541 ASCV_MSGIN_BEG,
6542 (uchar *)&ext_msg,
6543 sizeof(EXT_MSG) >> 1);
6544
6545 if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6546 ext_msg.msg_req == EXTENDED_SDTR &&
6547 ext_msg.msg_len == MS_SDTR_LEN) {
6548 sdtr_accept = TRUE;
6549 if ((ext_msg.req_ack_offset > ASC_SYN_MAX_OFFSET)) {
6550
6551 sdtr_accept = FALSE;
6552 ext_msg.req_ack_offset = ASC_SYN_MAX_OFFSET;
6553 }
6554 if ((ext_msg.xfer_period <
afbb68c3 6555 asc_dvc->sdtr_period_tbl[asc_dvc->min_sdtr_index])
51219358
MW
6556 || (ext_msg.xfer_period >
6557 asc_dvc->sdtr_period_tbl[asc_dvc->
6558 max_sdtr_index])) {
6559 sdtr_accept = FALSE;
6560 ext_msg.xfer_period =
6561 asc_dvc->sdtr_period_tbl[asc_dvc->
afbb68c3 6562 min_sdtr_index];
51219358
MW
6563 }
6564 if (sdtr_accept) {
6565 sdtr_data =
6566 AscCalSDTRData(asc_dvc, ext_msg.xfer_period,
6567 ext_msg.req_ack_offset);
6568 if ((sdtr_data == 0xFF)) {
6569
6570 q_cntl |= QC_MSG_OUT;
6571 asc_dvc->init_sdtr &= ~target_id;
6572 asc_dvc->sdtr_done &= ~target_id;
6573 AscSetChipSDTR(iop_base, asyn_sdtr,
6574 tid_no);
6575 boardp->sdtr_data[tid_no] = asyn_sdtr;
6576 }
6577 }
6578 if (ext_msg.req_ack_offset == 0) {
6579
6580 q_cntl &= ~QC_MSG_OUT;
6581 asc_dvc->init_sdtr &= ~target_id;
6582 asc_dvc->sdtr_done &= ~target_id;
6583 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6584 } else {
6585 if (sdtr_accept && (q_cntl & QC_MSG_OUT)) {
51219358
MW
6586 q_cntl &= ~QC_MSG_OUT;
6587 asc_dvc->sdtr_done |= target_id;
6588 asc_dvc->init_sdtr |= target_id;
6589 asc_dvc->pci_fix_asyn_xfer &=
6590 ~target_id;
6591 sdtr_data =
6592 AscCalSDTRData(asc_dvc,
6593 ext_msg.xfer_period,
6594 ext_msg.
6595 req_ack_offset);
6596 AscSetChipSDTR(iop_base, sdtr_data,
6597 tid_no);
6598 boardp->sdtr_data[tid_no] = sdtr_data;
6599 } else {
51219358
MW
6600 q_cntl |= QC_MSG_OUT;
6601 AscMsgOutSDTR(asc_dvc,
6602 ext_msg.xfer_period,
6603 ext_msg.req_ack_offset);
6604 asc_dvc->pci_fix_asyn_xfer &=
6605 ~target_id;
6606 sdtr_data =
6607 AscCalSDTRData(asc_dvc,
6608 ext_msg.xfer_period,
6609 ext_msg.
6610 req_ack_offset);
6611 AscSetChipSDTR(iop_base, sdtr_data,
6612 tid_no);
6613 boardp->sdtr_data[tid_no] = sdtr_data;
6614 asc_dvc->sdtr_done |= target_id;
6615 asc_dvc->init_sdtr |= target_id;
6616 }
6617 }
6618
6619 AscWriteLramByte(iop_base,
6620 (ushort)(halt_q_addr +
6621 (ushort)ASC_SCSIQ_B_CNTL),
6622 q_cntl);
6623 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6624 return (0);
6625 } else if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6626 ext_msg.msg_req == EXTENDED_WDTR &&
6627 ext_msg.msg_len == MS_WDTR_LEN) {
6628
6629 ext_msg.wdtr_width = 0;
6630 AscMemWordCopyPtrToLram(iop_base,
6631 ASCV_MSGOUT_BEG,
6632 (uchar *)&ext_msg,
6633 sizeof(EXT_MSG) >> 1);
6634 q_cntl |= QC_MSG_OUT;
6635 AscWriteLramByte(iop_base,
6636 (ushort)(halt_q_addr +
6637 (ushort)ASC_SCSIQ_B_CNTL),
6638 q_cntl);
6639 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6640 return (0);
6641 } else {
6642
6643 ext_msg.msg_type = MESSAGE_REJECT;
6644 AscMemWordCopyPtrToLram(iop_base,
6645 ASCV_MSGOUT_BEG,
6646 (uchar *)&ext_msg,
6647 sizeof(EXT_MSG) >> 1);
6648 q_cntl |= QC_MSG_OUT;
6649 AscWriteLramByte(iop_base,
6650 (ushort)(halt_q_addr +
6651 (ushort)ASC_SCSIQ_B_CNTL),
6652 q_cntl);
6653 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6654 return (0);
6655 }
6656 } else if (int_halt_code == ASC_HALT_CHK_CONDITION) {
6657
6658 q_cntl |= QC_REQ_SENSE;
6659
6660 if ((asc_dvc->init_sdtr & target_id) != 0) {
6661
6662 asc_dvc->sdtr_done &= ~target_id;
6663
6664 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
6665 q_cntl |= QC_MSG_OUT;
6666 AscMsgOutSDTR(asc_dvc,
6667 asc_dvc->
6668 sdtr_period_tbl[(sdtr_data >> 4) &
6669 (uchar)(asc_dvc->
6670 max_sdtr_index -
6671 1)],
6672 (uchar)(sdtr_data & (uchar)
6673 ASC_SYN_MAX_OFFSET));
6674 }
6675
6676 AscWriteLramByte(iop_base,
6677 (ushort)(halt_q_addr +
6678 (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6679
6680 tag_code = AscReadLramByte(iop_base,
6681 (ushort)(halt_q_addr + (ushort)
6682 ASC_SCSIQ_B_TAG_CODE));
6683 tag_code &= 0xDC;
6684 if ((asc_dvc->pci_fix_asyn_xfer & target_id)
6685 && !(asc_dvc->pci_fix_asyn_xfer_always & target_id)
6686 ) {
6687
6688 tag_code |= (ASC_TAG_FLAG_DISABLE_DISCONNECT
6689 | ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);
6690
6691 }
6692 AscWriteLramByte(iop_base,
6693 (ushort)(halt_q_addr +
6694 (ushort)ASC_SCSIQ_B_TAG_CODE),
6695 tag_code);
6696
6697 q_status = AscReadLramByte(iop_base,
6698 (ushort)(halt_q_addr + (ushort)
6699 ASC_SCSIQ_B_STATUS));
6700 q_status |= (QS_READY | QS_BUSY);
6701 AscWriteLramByte(iop_base,
6702 (ushort)(halt_q_addr +
6703 (ushort)ASC_SCSIQ_B_STATUS),
6704 q_status);
6705
6706 scsi_busy = AscReadLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B);
6707 scsi_busy &= ~target_id;
6708 AscWriteLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6709
6710 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6711 return (0);
6712 } else if (int_halt_code == ASC_HALT_SDTR_REJECTED) {
6713
6714 AscMemWordCopyPtrFromLram(iop_base,
6715 ASCV_MSGOUT_BEG,
6716 (uchar *)&out_msg,
6717 sizeof(EXT_MSG) >> 1);
6718
6719 if ((out_msg.msg_type == EXTENDED_MESSAGE) &&
6720 (out_msg.msg_len == MS_SDTR_LEN) &&
6721 (out_msg.msg_req == EXTENDED_SDTR)) {
6722
6723 asc_dvc->init_sdtr &= ~target_id;
6724 asc_dvc->sdtr_done &= ~target_id;
6725 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6726 boardp->sdtr_data[tid_no] = asyn_sdtr;
6727 }
6728 q_cntl &= ~QC_MSG_OUT;
6729 AscWriteLramByte(iop_base,
6730 (ushort)(halt_q_addr +
6731 (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6732 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6733 return (0);
6734 } else if (int_halt_code == ASC_HALT_SS_QUEUE_FULL) {
6735
6736 scsi_status = AscReadLramByte(iop_base,
6737 (ushort)((ushort)halt_q_addr +
6738 (ushort)
6739 ASC_SCSIQ_SCSI_STATUS));
6740 cur_dvc_qng =
6741 AscReadLramByte(iop_base,
6742 (ushort)((ushort)ASC_QADR_BEG +
6743 (ushort)target_ix));
6744 if ((cur_dvc_qng > 0) && (asc_dvc->cur_dvc_qng[tid_no] > 0)) {
6745
6746 scsi_busy = AscReadLramByte(iop_base,
6747 (ushort)ASCV_SCSIBUSY_B);
6748 scsi_busy |= target_id;
6749 AscWriteLramByte(iop_base,
6750 (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6751 asc_dvc->queue_full_or_busy |= target_id;
6752
6753 if (scsi_status == SAM_STAT_TASK_SET_FULL) {
6754 if (cur_dvc_qng > ASC_MIN_TAGGED_CMD) {
6755 cur_dvc_qng -= 1;
6756 asc_dvc->max_dvc_qng[tid_no] =
6757 cur_dvc_qng;
6758
6759 AscWriteLramByte(iop_base,
6760 (ushort)((ushort)
6761 ASCV_MAX_DVC_QNG_BEG
6762 + (ushort)
6763 tid_no),
6764 cur_dvc_qng);
6765
6766 /*
6767 * Set the device queue depth to the
6768 * number of active requests when the
6769 * QUEUE FULL condition was encountered.
6770 */
6771 boardp->queue_full |= target_id;
6772 boardp->queue_full_cnt[tid_no] =
6773 cur_dvc_qng;
6774 }
6775 }
6776 }
6777 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6778 return (0);
6779 }
6780#if CC_VERY_LONG_SG_LIST
6781 else if (int_halt_code == ASC_HALT_HOST_COPY_SG_LIST_TO_RISC) {
6782 uchar q_no;
6783 ushort q_addr;
6784 uchar sg_wk_q_no;
6785 uchar first_sg_wk_q_no;
6786 ASC_SCSI_Q *scsiq; /* Ptr to driver request. */
6787 ASC_SG_HEAD *sg_head; /* Ptr to driver SG request. */
6788 ASC_SG_LIST_Q scsi_sg_q; /* Structure written to queue. */
6789 ushort sg_list_dwords;
6790 ushort sg_entry_cnt;
6791 uchar next_qp;
6792 int i;
6793
6794 q_no = AscReadLramByte(iop_base, (ushort)ASCV_REQ_SG_LIST_QP);
6795 if (q_no == ASC_QLINK_END)
6796 return 0;
6797
6798 q_addr = ASC_QNO_TO_QADDR(q_no);
6799
6800 /*
6801 * Convert the request's SRB pointer to a host ASC_SCSI_REQ
6802 * structure pointer using a macro provided by the driver.
6803 * The ASC_SCSI_REQ pointer provides a pointer to the
6804 * host ASC_SG_HEAD structure.
6805 */
6806 /* Read request's SRB pointer. */
6807 scsiq = (ASC_SCSI_Q *)
6808 ASC_SRB2SCSIQ(ASC_U32_TO_VADDR(AscReadLramDWord(iop_base,
6809 (ushort)
6810 (q_addr +
6811 ASC_SCSIQ_D_SRBPTR))));
6812
6813 /*
6814 * Get request's first and working SG queue.
6815 */
6816 sg_wk_q_no = AscReadLramByte(iop_base,
6817 (ushort)(q_addr +
6818 ASC_SCSIQ_B_SG_WK_QP));
6819
6820 first_sg_wk_q_no = AscReadLramByte(iop_base,
6821 (ushort)(q_addr +
6822 ASC_SCSIQ_B_FIRST_SG_WK_QP));
6823
6824 /*
6825 * Reset request's working SG queue back to the
6826 * first SG queue.
6827 */
6828 AscWriteLramByte(iop_base,
6829 (ushort)(q_addr +
6830 (ushort)ASC_SCSIQ_B_SG_WK_QP),
6831 first_sg_wk_q_no);
6832
6833 sg_head = scsiq->sg_head;
6834
6835 /*
6836 * Set sg_entry_cnt to the number of SG elements
6837 * that will be completed on this interrupt.
6838 *
6839 * Note: The allocated SG queues contain ASC_MAX_SG_LIST - 1
6840 * SG elements. The data_cnt and data_addr fields which
6841 * add 1 to the SG element capacity are not used when
6842 * restarting SG handling after a halt.
6843 */
6844 if (scsiq->remain_sg_entry_cnt > (ASC_MAX_SG_LIST - 1)) {
6845 sg_entry_cnt = ASC_MAX_SG_LIST - 1;
6846
6847 /*
6848 * Keep track of remaining number of SG elements that
6849 * will need to be handled on the next interrupt.
6850 */
6851 scsiq->remain_sg_entry_cnt -= (ASC_MAX_SG_LIST - 1);
6852 } else {
6853 sg_entry_cnt = scsiq->remain_sg_entry_cnt;
6854 scsiq->remain_sg_entry_cnt = 0;
6855 }
6856
6857 /*
6858 * Copy SG elements into the list of allocated SG queues.
6859 *
6860 * Last index completed is saved in scsiq->next_sg_index.
6861 */
6862 next_qp = first_sg_wk_q_no;
6863 q_addr = ASC_QNO_TO_QADDR(next_qp);
6864 scsi_sg_q.sg_head_qp = q_no;
6865 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
6866 for (i = 0; i < sg_head->queue_cnt; i++) {
6867 scsi_sg_q.seq_no = i + 1;
6868 if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
6869 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
6870 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
6871 /*
6872 * After very first SG queue RISC FW uses next
6873 * SG queue first element then checks sg_list_cnt
6874 * against zero and then decrements, so set
6875 * sg_list_cnt 1 less than number of SG elements
6876 * in each SG queue.
6877 */
6878 scsi_sg_q.sg_list_cnt = ASC_SG_LIST_PER_Q - 1;
6879 scsi_sg_q.sg_cur_list_cnt =
6880 ASC_SG_LIST_PER_Q - 1;
6881 } else {
6882 /*
6883 * This is the last SG queue in the list of
6884 * allocated SG queues. If there are more
6885 * SG elements than will fit in the allocated
6886 * queues, then set the QCSG_SG_XFER_MORE flag.
6887 */
6888 if (scsiq->remain_sg_entry_cnt != 0) {
6889 scsi_sg_q.cntl |= QCSG_SG_XFER_MORE;
6890 } else {
6891 scsi_sg_q.cntl |= QCSG_SG_XFER_END;
6892 }
6893 /* equals sg_entry_cnt * 2 */
6894 sg_list_dwords = sg_entry_cnt << 1;
6895 scsi_sg_q.sg_list_cnt = sg_entry_cnt - 1;
6896 scsi_sg_q.sg_cur_list_cnt = sg_entry_cnt - 1;
6897 sg_entry_cnt = 0;
6898 }
6899
6900 scsi_sg_q.q_no = next_qp;
6901 AscMemWordCopyPtrToLram(iop_base,
6902 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
6903 (uchar *)&scsi_sg_q,
6904 sizeof(ASC_SG_LIST_Q) >> 1);
6905
6906 AscMemDWordCopyPtrToLram(iop_base,
6907 q_addr + ASC_SGQ_LIST_BEG,
6908 (uchar *)&sg_head->
6909 sg_list[scsiq->next_sg_index],
6910 sg_list_dwords);
6911
6912 scsiq->next_sg_index += ASC_SG_LIST_PER_Q;
6913
6914 /*
6915 * If the just completed SG queue contained the
6916 * last SG element, then no more SG queues need
6917 * to be written.
6918 */
6919 if (scsi_sg_q.cntl & QCSG_SG_XFER_END) {
6920 break;
6921 }
6922
6923 next_qp = AscReadLramByte(iop_base,
6924 (ushort)(q_addr +
6925 ASC_SCSIQ_B_FWD));
6926 q_addr = ASC_QNO_TO_QADDR(next_qp);
6927 }
6928
6929 /*
6930 * Clear the halt condition so the RISC will be restarted
6931 * after the return.
6932 */
6933 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6934 return (0);
6935 }
6936#endif /* CC_VERY_LONG_SG_LIST */
6937 return (0);
6938}
1da177e4 6939
1da177e4 6940/*
51219358
MW
6941 * void
6942 * DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
1da177e4 6943 *
51219358
MW
6944 * Calling/Exit State:
6945 * none
1da177e4 6946 *
51219358
MW
6947 * Description:
6948 * Input an ASC_QDONE_INFO structure from the chip
1da177e4 6949 */
51219358
MW
6950static void
6951DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6952{
6953 int i;
6954 ushort word;
6955
6956 AscSetChipLramAddr(iop_base, s_addr);
6957 for (i = 0; i < 2 * words; i += 2) {
6958 if (i == 10) {
6959 continue;
6960 }
6961 word = inpw(iop_base + IOP_RAM_DATA);
6962 inbuf[i] = word & 0xff;
6963 inbuf[i + 1] = (word >> 8) & 0xff;
6964 }
6965 ASC_DBG_PRT_HEX(2, "DvcGetQinfo", inbuf, 2 * words);
6966}
6967
6968static uchar
6969_AscCopyLramScsiDoneQ(PortAddr iop_base,
6970 ushort q_addr,
6971 ASC_QDONE_INFO *scsiq, ASC_DCNT max_dma_count)
6972{
6973 ushort _val;
6974 uchar sg_queue_cnt;
6975
6976 DvcGetQinfo(iop_base,
6977 q_addr + ASC_SCSIQ_DONE_INFO_BEG,
6978 (uchar *)scsiq,
6979 (sizeof(ASC_SCSIQ_2) + sizeof(ASC_SCSIQ_3)) / 2);
6980
6981 _val = AscReadLramWord(iop_base,
6982 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS));
6983 scsiq->q_status = (uchar)_val;
6984 scsiq->q_no = (uchar)(_val >> 8);
6985 _val = AscReadLramWord(iop_base,
6986 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6987 scsiq->cntl = (uchar)_val;
6988 sg_queue_cnt = (uchar)(_val >> 8);
6989 _val = AscReadLramWord(iop_base,
6990 (ushort)(q_addr +
6991 (ushort)ASC_SCSIQ_B_SENSE_LEN));
6992 scsiq->sense_len = (uchar)_val;
6993 scsiq->extra_bytes = (uchar)(_val >> 8);
6994
6995 /*
6996 * Read high word of remain bytes from alternate location.
6997 */
6998 scsiq->remain_bytes = (((ADV_DCNT)AscReadLramWord(iop_base,
6999 (ushort)(q_addr +
7000 (ushort)
7001 ASC_SCSIQ_W_ALT_DC1)))
7002 << 16);
7003 /*
7004 * Read low word of remain bytes from original location.
7005 */
7006 scsiq->remain_bytes += AscReadLramWord(iop_base,
7007 (ushort)(q_addr + (ushort)
7008 ASC_SCSIQ_DW_REMAIN_XFER_CNT));
7009
7010 scsiq->remain_bytes &= max_dma_count;
7011 return sg_queue_cnt;
7012}
7013
7014/*
7015 * asc_isr_callback() - Second Level Interrupt Handler called by AscISR().
7016 *
7017 * Interrupt callback function for the Narrow SCSI Asc Library.
7018 */
7019static void asc_isr_callback(ASC_DVC_VAR *asc_dvc_varp, ASC_QDONE_INFO *qdonep)
7020{
9c17c62a
HR
7021 struct asc_board *boardp = asc_dvc_varp->drv_ptr;
7022 u32 srb_tag;
51219358 7023 struct scsi_cmnd *scp;
51219358 7024
b352f923 7025 ASC_DBG(1, "asc_dvc_varp 0x%p, qdonep 0x%p\n", asc_dvc_varp, qdonep);
51219358
MW
7026 ASC_DBG_PRT_ASC_QDONE_INFO(2, qdonep);
7027
9c17c62a
HR
7028 /*
7029 * Decrease the srb_tag by 1 to find the SCSI command
7030 */
7031 srb_tag = qdonep->d2.srb_tag - 1;
7032 scp = scsi_host_find_tag(boardp->shost, srb_tag);
b249c7fd 7033 if (!scp)
51219358 7034 return;
b249c7fd 7035
51219358
MW
7036 ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
7037
9c17c62a 7038 ASC_STATS(boardp->shost, callback);
51219358 7039
b249c7fd 7040 dma_unmap_single(boardp->dev, scp->SCp.dma_handle,
b80ca4f7 7041 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
51219358
MW
7042 /*
7043 * 'qdonep' contains the command's ending status.
7044 */
7045 switch (qdonep->d3.done_stat) {
7046 case QD_NO_ERROR:
b352f923 7047 ASC_DBG(2, "QD_NO_ERROR\n");
51219358
MW
7048 scp->result = 0;
7049
7050 /*
7051 * Check for an underrun condition.
7052 *
7053 * If there was no error and an underrun condition, then
7054 * return the number of underrun bytes.
7055 */
52c334e9
MW
7056 if (scsi_bufflen(scp) != 0 && qdonep->remain_bytes != 0 &&
7057 qdonep->remain_bytes <= scsi_bufflen(scp)) {
b352f923 7058 ASC_DBG(1, "underrun condition %u bytes\n",
51219358 7059 (unsigned)qdonep->remain_bytes);
52c334e9 7060 scsi_set_resid(scp, qdonep->remain_bytes);
51219358
MW
7061 }
7062 break;
7063
7064 case QD_WITH_ERROR:
b352f923 7065 ASC_DBG(2, "QD_WITH_ERROR\n");
51219358
MW
7066 switch (qdonep->d3.host_stat) {
7067 case QHSTA_NO_ERROR:
7068 if (qdonep->d3.scsi_stat == SAM_STAT_CHECK_CONDITION) {
b352f923 7069 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
51219358 7070 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
b80ca4f7 7071 SCSI_SENSE_BUFFERSIZE);
51219358
MW
7072 /*
7073 * Note: The 'status_byte()' macro used by
7074 * target drivers defined in scsi.h shifts the
7075 * status byte returned by host drivers right
7076 * by 1 bit. This is why target drivers also
7077 * use right shifted status byte definitions.
7078 * For instance target drivers use
7079 * CHECK_CONDITION, defined to 0x1, instead of
7080 * the SCSI defined check condition value of
7081 * 0x2. Host drivers are supposed to return
7082 * the status byte as it is defined by SCSI.
7083 */
7084 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
7085 STATUS_BYTE(qdonep->d3.scsi_stat);
7086 } else {
7087 scp->result = STATUS_BYTE(qdonep->d3.scsi_stat);
7088 }
7089 break;
7090
7091 default:
7092 /* QHSTA error occurred */
b352f923 7093 ASC_DBG(1, "host_stat 0x%x\n", qdonep->d3.host_stat);
51219358
MW
7094 scp->result = HOST_BYTE(DID_BAD_TARGET);
7095 break;
7096 }
7097 break;
7098
7099 case QD_ABORTED_BY_HOST:
b352f923 7100 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
51219358
MW
7101 scp->result =
7102 HOST_BYTE(DID_ABORT) | MSG_BYTE(qdonep->d3.
7103 scsi_msg) |
7104 STATUS_BYTE(qdonep->d3.scsi_stat);
7105 break;
7106
7107 default:
b352f923 7108 ASC_DBG(1, "done_stat 0x%x\n", qdonep->d3.done_stat);
51219358
MW
7109 scp->result =
7110 HOST_BYTE(DID_ERROR) | MSG_BYTE(qdonep->d3.
7111 scsi_msg) |
7112 STATUS_BYTE(qdonep->d3.scsi_stat);
7113 break;
7114 }
7115
7116 /*
7117 * If the 'init_tidmask' bit isn't already set for the target and the
7118 * current request finished normally, then set the bit for the target
7119 * to indicate that a device is present.
7120 */
7121 if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
7122 qdonep->d3.done_stat == QD_NO_ERROR &&
7123 qdonep->d3.host_stat == QHSTA_NO_ERROR) {
7124 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
7125 }
1da177e4 7126
51219358 7127 asc_scsi_done(scp);
51219358
MW
7128}
7129
7130static int AscIsrQDone(ASC_DVC_VAR *asc_dvc)
7131{
7132 uchar next_qp;
7133 uchar n_q_used;
7134 uchar sg_list_qp;
7135 uchar sg_queue_cnt;
7136 uchar q_cnt;
7137 uchar done_q_tail;
7138 uchar tid_no;
7139 ASC_SCSI_BIT_ID_TYPE scsi_busy;
7140 ASC_SCSI_BIT_ID_TYPE target_id;
7141 PortAddr iop_base;
7142 ushort q_addr;
7143 ushort sg_q_addr;
7144 uchar cur_target_qng;
7145 ASC_QDONE_INFO scsiq_buf;
7146 ASC_QDONE_INFO *scsiq;
7147 int false_overrun;
7148
7149 iop_base = asc_dvc->iop_base;
7150 n_q_used = 1;
7151 scsiq = (ASC_QDONE_INFO *)&scsiq_buf;
7152 done_q_tail = (uchar)AscGetVarDoneQTail(iop_base);
7153 q_addr = ASC_QNO_TO_QADDR(done_q_tail);
7154 next_qp = AscReadLramByte(iop_base,
7155 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_FWD));
7156 if (next_qp != ASC_QLINK_END) {
7157 AscPutVarDoneQTail(iop_base, next_qp);
7158 q_addr = ASC_QNO_TO_QADDR(next_qp);
7159 sg_queue_cnt = _AscCopyLramScsiDoneQ(iop_base, q_addr, scsiq,
7160 asc_dvc->max_dma_count);
7161 AscWriteLramByte(iop_base,
7162 (ushort)(q_addr +
7163 (ushort)ASC_SCSIQ_B_STATUS),
7164 (uchar)(scsiq->
7165 q_status & (uchar)~(QS_READY |
7166 QS_ABORTED)));
7167 tid_no = ASC_TIX_TO_TID(scsiq->d2.target_ix);
7168 target_id = ASC_TIX_TO_TARGET_ID(scsiq->d2.target_ix);
7169 if ((scsiq->cntl & QC_SG_HEAD) != 0) {
7170 sg_q_addr = q_addr;
7171 sg_list_qp = next_qp;
7172 for (q_cnt = 0; q_cnt < sg_queue_cnt; q_cnt++) {
7173 sg_list_qp = AscReadLramByte(iop_base,
7174 (ushort)(sg_q_addr
7175 + (ushort)
7176 ASC_SCSIQ_B_FWD));
7177 sg_q_addr = ASC_QNO_TO_QADDR(sg_list_qp);
7178 if (sg_list_qp == ASC_QLINK_END) {
7179 AscSetLibErrorCode(asc_dvc,
7180 ASCQ_ERR_SG_Q_LINKS);
7181 scsiq->d3.done_stat = QD_WITH_ERROR;
7182 scsiq->d3.host_stat =
7183 QHSTA_D_QDONE_SG_LIST_CORRUPTED;
7184 goto FATAL_ERR_QDONE;
7185 }
7186 AscWriteLramByte(iop_base,
7187 (ushort)(sg_q_addr + (ushort)
7188 ASC_SCSIQ_B_STATUS),
7189 QS_FREE);
7190 }
7191 n_q_used = sg_queue_cnt + 1;
7192 AscPutVarDoneQTail(iop_base, sg_list_qp);
7193 }
7194 if (asc_dvc->queue_full_or_busy & target_id) {
7195 cur_target_qng = AscReadLramByte(iop_base,
7196 (ushort)((ushort)
7197 ASC_QADR_BEG
7198 + (ushort)
7199 scsiq->d2.
7200 target_ix));
7201 if (cur_target_qng < asc_dvc->max_dvc_qng[tid_no]) {
7202 scsi_busy = AscReadLramByte(iop_base, (ushort)
7203 ASCV_SCSIBUSY_B);
7204 scsi_busy &= ~target_id;
7205 AscWriteLramByte(iop_base,
7206 (ushort)ASCV_SCSIBUSY_B,
7207 scsi_busy);
7208 asc_dvc->queue_full_or_busy &= ~target_id;
7209 }
7210 }
7211 if (asc_dvc->cur_total_qng >= n_q_used) {
7212 asc_dvc->cur_total_qng -= n_q_used;
7213 if (asc_dvc->cur_dvc_qng[tid_no] != 0) {
7214 asc_dvc->cur_dvc_qng[tid_no]--;
7215 }
7216 } else {
7217 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CUR_QNG);
7218 scsiq->d3.done_stat = QD_WITH_ERROR;
7219 goto FATAL_ERR_QDONE;
7220 }
9c17c62a 7221 if ((scsiq->d2.srb_tag == 0UL) ||
51219358
MW
7222 ((scsiq->q_status & QS_ABORTED) != 0)) {
7223 return (0x11);
7224 } else if (scsiq->q_status == QS_DONE) {
7225 false_overrun = FALSE;
7226 if (scsiq->extra_bytes != 0) {
7227 scsiq->remain_bytes +=
7228 (ADV_DCNT)scsiq->extra_bytes;
7229 }
7230 if (scsiq->d3.done_stat == QD_WITH_ERROR) {
7231 if (scsiq->d3.host_stat ==
7232 QHSTA_M_DATA_OVER_RUN) {
7233 if ((scsiq->
7234 cntl & (QC_DATA_IN | QC_DATA_OUT))
7235 == 0) {
7236 scsiq->d3.done_stat =
7237 QD_NO_ERROR;
7238 scsiq->d3.host_stat =
7239 QHSTA_NO_ERROR;
7240 } else if (false_overrun) {
7241 scsiq->d3.done_stat =
7242 QD_NO_ERROR;
7243 scsiq->d3.host_stat =
7244 QHSTA_NO_ERROR;
7245 }
7246 } else if (scsiq->d3.host_stat ==
7247 QHSTA_M_HUNG_REQ_SCSI_BUS_RESET) {
7248 AscStopChip(iop_base);
7249 AscSetChipControl(iop_base,
7250 (uchar)(CC_SCSI_RESET
7251 | CC_HALT));
7252 udelay(60);
7253 AscSetChipControl(iop_base, CC_HALT);
7254 AscSetChipStatus(iop_base,
7255 CIW_CLR_SCSI_RESET_INT);
7256 AscSetChipStatus(iop_base, 0);
7257 AscSetChipControl(iop_base, 0);
7258 }
7259 }
7260 if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7261 asc_isr_callback(asc_dvc, scsiq);
7262 } else {
7263 if ((AscReadLramByte(iop_base,
7264 (ushort)(q_addr + (ushort)
7265 ASC_SCSIQ_CDB_BEG))
7266 == START_STOP)) {
7267 asc_dvc->unit_not_ready &= ~target_id;
7268 if (scsiq->d3.done_stat != QD_NO_ERROR) {
7269 asc_dvc->start_motor &=
7270 ~target_id;
7271 }
7272 }
7273 }
7274 return (1);
7275 } else {
7276 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_Q_STATUS);
7277 FATAL_ERR_QDONE:
7278 if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7279 asc_isr_callback(asc_dvc, scsiq);
7280 }
7281 return (0x80);
7282 }
7283 }
7284 return (0);
7285}
1da177e4 7286
51219358
MW
7287static int AscISR(ASC_DVC_VAR *asc_dvc)
7288{
7289 ASC_CS_TYPE chipstat;
7290 PortAddr iop_base;
7291 ushort saved_ram_addr;
7292 uchar ctrl_reg;
7293 uchar saved_ctrl_reg;
7294 int int_pending;
7295 int status;
7296 uchar host_flag;
1da177e4 7297
51219358
MW
7298 iop_base = asc_dvc->iop_base;
7299 int_pending = FALSE;
1da177e4 7300
51219358
MW
7301 if (AscIsIntPending(iop_base) == 0)
7302 return int_pending;
7303
7304 if ((asc_dvc->init_state & ASC_INIT_STATE_END_LOAD_MC) == 0) {
7305 return ERR;
7306 }
7307 if (asc_dvc->in_critical_cnt != 0) {
7308 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_ON_CRITICAL);
7309 return ERR;
7310 }
7311 if (asc_dvc->is_in_int) {
7312 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_RE_ENTRY);
7313 return ERR;
7314 }
7315 asc_dvc->is_in_int = TRUE;
7316 ctrl_reg = AscGetChipControl(iop_base);
7317 saved_ctrl_reg = ctrl_reg & (~(CC_SCSI_RESET | CC_CHIP_RESET |
7318 CC_SINGLE_STEP | CC_DIAG | CC_TEST));
7319 chipstat = AscGetChipStatus(iop_base);
7320 if (chipstat & CSW_SCSI_RESET_LATCH) {
7321 if (!(asc_dvc->bus_type & (ASC_IS_VL | ASC_IS_EISA))) {
7322 int i = 10;
7323 int_pending = TRUE;
7324 asc_dvc->sdtr_done = 0;
7325 saved_ctrl_reg &= (uchar)(~CC_HALT);
7326 while ((AscGetChipStatus(iop_base) &
7327 CSW_SCSI_RESET_ACTIVE) && (i-- > 0)) {
7328 mdelay(100);
7329 }
7330 AscSetChipControl(iop_base, (CC_CHIP_RESET | CC_HALT));
7331 AscSetChipControl(iop_base, CC_HALT);
7332 AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
7333 AscSetChipStatus(iop_base, 0);
7334 chipstat = AscGetChipStatus(iop_base);
7335 }
7336 }
7337 saved_ram_addr = AscGetChipLramAddr(iop_base);
7338 host_flag = AscReadLramByte(iop_base,
7339 ASCV_HOST_FLAG_B) &
7340 (uchar)(~ASC_HOST_FLAG_IN_ISR);
7341 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
7342 (uchar)(host_flag | (uchar)ASC_HOST_FLAG_IN_ISR));
7343 if ((chipstat & CSW_INT_PENDING) || (int_pending)) {
7344 AscAckInterrupt(iop_base);
7345 int_pending = TRUE;
7346 if ((chipstat & CSW_HALTED) && (ctrl_reg & CC_SINGLE_STEP)) {
7347 if (AscIsrChipHalted(asc_dvc) == ERR) {
7348 goto ISR_REPORT_QDONE_FATAL_ERROR;
7349 } else {
7350 saved_ctrl_reg &= (uchar)(~CC_HALT);
7351 }
7352 } else {
7353 ISR_REPORT_QDONE_FATAL_ERROR:
7354 if ((asc_dvc->dvc_cntl & ASC_CNTL_INT_MULTI_Q) != 0) {
7355 while (((status =
7356 AscIsrQDone(asc_dvc)) & 0x01) != 0) {
7357 }
7358 } else {
7359 do {
7360 if ((status =
7361 AscIsrQDone(asc_dvc)) == 1) {
7362 break;
7363 }
7364 } while (status == 0x11);
7365 }
7366 if ((status & 0x80) != 0)
7367 int_pending = ERR;
7368 }
7369 }
7370 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
7371 AscSetChipLramAddr(iop_base, saved_ram_addr);
7372 AscSetChipControl(iop_base, saved_ctrl_reg);
7373 asc_dvc->is_in_int = FALSE;
7374 return int_pending;
7375}
1da177e4
LT
7376
7377/*
51219358 7378 * advansys_reset()
1da177e4 7379 *
eac0b0c7 7380 * Reset the host associated with the command 'scp'.
1da177e4 7381 *
51219358
MW
7382 * This function runs its own thread. Interrupts must be blocked but
7383 * sleeping is allowed and no locking other than for host structures is
7384 * required. Returns SUCCESS or FAILED.
1da177e4 7385 */
51219358 7386static int advansys_reset(struct scsi_cmnd *scp)
1da177e4 7387{
52fa0777 7388 struct Scsi_Host *shost = scp->device->host;
d2411495 7389 struct asc_board *boardp = shost_priv(shost);
52fa0777 7390 unsigned long flags;
27c868c2 7391 int status;
51219358 7392 int ret = SUCCESS;
27c868c2 7393
b352f923 7394 ASC_DBG(1, "0x%p\n", scp);
27c868c2 7395
52fa0777 7396 ASC_STATS(shost, reset);
27c868c2 7397
eac0b0c7 7398 scmd_printk(KERN_INFO, scp, "SCSI host reset started...\n");
51219358
MW
7399
7400 if (ASC_NARROW_BOARD(boardp)) {
52fa0777 7401 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
1da177e4 7402
52fa0777 7403 /* Reset the chip and SCSI bus. */
b352f923 7404 ASC_DBG(1, "before AscInitAsc1000Driver()\n");
52fa0777 7405 status = AscInitAsc1000Driver(asc_dvc);
27c868c2 7406
6070d81e 7407 /* Refer to ASC_IERR_* definitions for meaning of 'err_code'. */
9a908c1a 7408 if (asc_dvc->err_code || !asc_dvc->overrun_dma) {
eac0b0c7 7409 scmd_printk(KERN_INFO, scp, "SCSI host reset error: "
9a908c1a
HRK
7410 "0x%x, status: 0x%x\n", asc_dvc->err_code,
7411 status);
51219358
MW
7412 ret = FAILED;
7413 } else if (status) {
eac0b0c7 7414 scmd_printk(KERN_INFO, scp, "SCSI host reset warning: "
52fa0777 7415 "0x%x\n", status);
27c868c2 7416 } else {
eac0b0c7 7417 scmd_printk(KERN_INFO, scp, "SCSI host reset "
52fa0777 7418 "successful\n");
27c868c2 7419 }
a9f4a59a 7420
b352f923 7421 ASC_DBG(1, "after AscInitAsc1000Driver()\n");
a9f4a59a 7422 } else {
a9f4a59a 7423 /*
51219358
MW
7424 * If the suggest reset bus flags are set, then reset the bus.
7425 * Otherwise only reset the device.
a9f4a59a 7426 */
52fa0777 7427 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
a9f4a59a
MW
7428
7429 /*
eac0b0c7 7430 * Reset the chip and SCSI bus.
a9f4a59a 7431 */
b352f923 7432 ASC_DBG(1, "before AdvResetChipAndSB()\n");
52fa0777 7433 switch (AdvResetChipAndSB(adv_dvc)) {
51219358 7434 case ASC_TRUE:
eac0b0c7 7435 scmd_printk(KERN_INFO, scp, "SCSI host reset "
52fa0777 7436 "successful\n");
51219358
MW
7437 break;
7438 case ASC_FALSE:
7439 default:
eac0b0c7 7440 scmd_printk(KERN_INFO, scp, "SCSI host reset error\n");
51219358
MW
7441 ret = FAILED;
7442 break;
b9d96614 7443 }
f092d229 7444 spin_lock_irqsave(shost->host_lock, flags);
52fa0777 7445 AdvISR(adv_dvc);
eac0b0c7 7446 spin_unlock_irqrestore(shost->host_lock, flags);
b9d96614
MW
7447 }
7448
b352f923 7449 ASC_DBG(1, "ret %d\n", ret);
b9d96614 7450
51219358 7451 return ret;
b9d96614
MW
7452}
7453
1da177e4 7454/*
51219358 7455 * advansys_biosparam()
1da177e4 7456 *
51219358
MW
7457 * Translate disk drive geometry if the "BIOS greater than 1 GB"
7458 * support is enabled for a drive.
1da177e4 7459 *
51219358
MW
7460 * ip (information pointer) is an int array with the following definition:
7461 * ip[0]: heads
7462 * ip[1]: sectors
7463 * ip[2]: cylinders
1da177e4 7464 */
51219358
MW
7465static int
7466advansys_biosparam(struct scsi_device *sdev, struct block_device *bdev,
7467 sector_t capacity, int ip[])
1da177e4 7468{
d2411495 7469 struct asc_board *boardp = shost_priv(sdev->host);
1da177e4 7470
b352f923 7471 ASC_DBG(1, "begin\n");
51219358 7472 ASC_STATS(sdev->host, biosparam);
51219358
MW
7473 if (ASC_NARROW_BOARD(boardp)) {
7474 if ((boardp->dvc_var.asc_dvc_var.dvc_cntl &
7475 ASC_CNTL_BIOS_GT_1GB) && capacity > 0x200000) {
7476 ip[0] = 255;
7477 ip[1] = 63;
7478 } else {
7479 ip[0] = 64;
7480 ip[1] = 32;
7481 }
7482 } else {
7483 if ((boardp->dvc_var.adv_dvc_var.bios_ctrl &
7484 BIOS_CTRL_EXTENDED_XLAT) && capacity > 0x200000) {
7485 ip[0] = 255;
7486 ip[1] = 63;
7487 } else {
7488 ip[0] = 64;
7489 ip[1] = 32;
7490 }
27c868c2 7491 }
51219358 7492 ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
b352f923 7493 ASC_DBG(1, "end\n");
51219358
MW
7494 return 0;
7495}
1da177e4 7496
51219358
MW
7497/*
7498 * First-level interrupt handler.
7499 *
7500 * 'dev_id' is a pointer to the interrupting adapter's Scsi_Host.
7501 */
7502static irqreturn_t advansys_interrupt(int irq, void *dev_id)
7503{
51219358 7504 struct Scsi_Host *shost = dev_id;
d2411495 7505 struct asc_board *boardp = shost_priv(shost);
51219358 7506 irqreturn_t result = IRQ_NONE;
27c868c2 7507
b352f923 7508 ASC_DBG(2, "boardp 0x%p\n", boardp);
f092d229 7509 spin_lock(shost->host_lock);
51219358
MW
7510 if (ASC_NARROW_BOARD(boardp)) {
7511 if (AscIsIntPending(shost->io_port)) {
7512 result = IRQ_HANDLED;
7513 ASC_STATS(shost, interrupt);
b352f923 7514 ASC_DBG(1, "before AscISR()\n");
51219358
MW
7515 AscISR(&boardp->dvc_var.asc_dvc_var);
7516 }
7517 } else {
b352f923 7518 ASC_DBG(1, "before AdvISR()\n");
51219358
MW
7519 if (AdvISR(&boardp->dvc_var.adv_dvc_var)) {
7520 result = IRQ_HANDLED;
7521 ASC_STATS(shost, interrupt);
7522 }
27c868c2 7523 }
f092d229 7524 spin_unlock(shost->host_lock);
1da177e4 7525
b352f923 7526 ASC_DBG(1, "end\n");
51219358
MW
7527 return result;
7528}
27c868c2 7529
51219358
MW
7530static int AscHostReqRiscHalt(PortAddr iop_base)
7531{
7532 int count = 0;
7533 int sta = 0;
7534 uchar saved_stop_code;
7535
7536 if (AscIsChipHalted(iop_base))
7537 return (1);
7538 saved_stop_code = AscReadLramByte(iop_base, ASCV_STOP_CODE_B);
7539 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
7540 ASC_STOP_HOST_REQ_RISC_HALT | ASC_STOP_REQ_RISC_STOP);
7541 do {
7542 if (AscIsChipHalted(iop_base)) {
7543 sta = 1;
7544 break;
27c868c2 7545 }
51219358
MW
7546 mdelay(100);
7547 } while (count++ < 20);
7548 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, saved_stop_code);
7549 return (sta);
7550}
1da177e4 7551
51219358
MW
7552static int
7553AscSetRunChipSynRegAtID(PortAddr iop_base, uchar tid_no, uchar sdtr_data)
7554{
7555 int sta = FALSE;
1da177e4 7556
51219358
MW
7557 if (AscHostReqRiscHalt(iop_base)) {
7558 sta = AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
7559 AscStartChip(iop_base);
27c868c2 7560 }
51219358
MW
7561 return sta;
7562}
1da177e4 7563
51219358
MW
7564static void AscAsyncFix(ASC_DVC_VAR *asc_dvc, struct scsi_device *sdev)
7565{
7566 char type = sdev->type;
7567 ASC_SCSI_BIT_ID_TYPE tid_bits = 1 << sdev->id;
27c868c2 7568
51219358
MW
7569 if (!(asc_dvc->bug_fix_cntl & ASC_BUG_FIX_ASYN_USE_SYN))
7570 return;
7571 if (asc_dvc->init_sdtr & tid_bits)
7572 return;
27c868c2 7573
51219358
MW
7574 if ((type == TYPE_ROM) && (strncmp(sdev->vendor, "HP ", 3) == 0))
7575 asc_dvc->pci_fix_asyn_xfer_always |= tid_bits;
27c868c2 7576
51219358
MW
7577 asc_dvc->pci_fix_asyn_xfer |= tid_bits;
7578 if ((type == TYPE_PROCESSOR) || (type == TYPE_SCANNER) ||
7579 (type == TYPE_ROM) || (type == TYPE_TAPE))
7580 asc_dvc->pci_fix_asyn_xfer &= ~tid_bits;
7581
7582 if (asc_dvc->pci_fix_asyn_xfer & tid_bits)
7583 AscSetRunChipSynRegAtID(asc_dvc->iop_base, sdev->id,
7584 ASYN_SDTR_DATA_FIX_PCI_REV_AB);
7585}
1da177e4 7586
51219358
MW
7587static void
7588advansys_narrow_slave_configure(struct scsi_device *sdev, ASC_DVC_VAR *asc_dvc)
7589{
7590 ASC_SCSI_BIT_ID_TYPE tid_bit = 1 << sdev->id;
7591 ASC_SCSI_BIT_ID_TYPE orig_use_tagged_qng = asc_dvc->use_tagged_qng;
27c868c2 7592
51219358
MW
7593 if (sdev->lun == 0) {
7594 ASC_SCSI_BIT_ID_TYPE orig_init_sdtr = asc_dvc->init_sdtr;
7595 if ((asc_dvc->cfg->sdtr_enable & tid_bit) && sdev->sdtr) {
7596 asc_dvc->init_sdtr |= tid_bit;
7597 } else {
7598 asc_dvc->init_sdtr &= ~tid_bit;
7599 }
7600
7601 if (orig_init_sdtr != asc_dvc->init_sdtr)
7602 AscAsyncFix(asc_dvc, sdev);
27c868c2 7603 }
1da177e4 7604
51219358
MW
7605 if (sdev->tagged_supported) {
7606 if (asc_dvc->cfg->cmd_qng_enabled & tid_bit) {
7607 if (sdev->lun == 0) {
7608 asc_dvc->cfg->can_tagged_qng |= tid_bit;
7609 asc_dvc->use_tagged_qng |= tid_bit;
7610 }
db5ed4df 7611 scsi_change_queue_depth(sdev,
51219358 7612 asc_dvc->max_dvc_qng[sdev->id]);
27c868c2 7613 }
51219358
MW
7614 } else {
7615 if (sdev->lun == 0) {
7616 asc_dvc->cfg->can_tagged_qng &= ~tid_bit;
7617 asc_dvc->use_tagged_qng &= ~tid_bit;
27c868c2
MW
7618 }
7619 }
1da177e4 7620
51219358
MW
7621 if ((sdev->lun == 0) &&
7622 (orig_use_tagged_qng != asc_dvc->use_tagged_qng)) {
7623 AscWriteLramByte(asc_dvc->iop_base, ASCV_DISC_ENABLE_B,
7624 asc_dvc->cfg->disc_enable);
7625 AscWriteLramByte(asc_dvc->iop_base, ASCV_USE_TAGGED_QNG_B,
7626 asc_dvc->use_tagged_qng);
7627 AscWriteLramByte(asc_dvc->iop_base, ASCV_CAN_TAGGED_QNG_B,
7628 asc_dvc->cfg->can_tagged_qng);
27c868c2 7629
51219358
MW
7630 asc_dvc->max_dvc_qng[sdev->id] =
7631 asc_dvc->cfg->max_tag_qng[sdev->id];
7632 AscWriteLramByte(asc_dvc->iop_base,
7633 (ushort)(ASCV_MAX_DVC_QNG_BEG + sdev->id),
7634 asc_dvc->max_dvc_qng[sdev->id]);
7635 }
7636}
27c868c2 7637
51219358
MW
7638/*
7639 * Wide Transfers
7640 *
7641 * If the EEPROM enabled WDTR for the device and the device supports wide
7642 * bus (16 bit) transfers, then turn on the device's 'wdtr_able' bit and
7643 * write the new value to the microcode.
7644 */
7645static void
7646advansys_wide_enable_wdtr(AdvPortAddr iop_base, unsigned short tidmask)
7647{
7648 unsigned short cfg_word;
7649 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7650 if ((cfg_word & tidmask) != 0)
7651 return;
27c868c2 7652
51219358
MW
7653 cfg_word |= tidmask;
7654 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
27c868c2
MW
7655
7656 /*
51219358
MW
7657 * Clear the microcode SDTR and WDTR negotiation done indicators for
7658 * the target to cause it to negotiate with the new setting set above.
7659 * WDTR when accepted causes the target to enter asynchronous mode, so
7660 * SDTR must be negotiated.
27c868c2 7661 */
51219358
MW
7662 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7663 cfg_word &= ~tidmask;
7664 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7665 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7666 cfg_word &= ~tidmask;
7667 AdvWriteWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7668}
1da177e4 7669
51219358
MW
7670/*
7671 * Synchronous Transfers
7672 *
7673 * If the EEPROM enabled SDTR for the device and the device
7674 * supports synchronous transfers, then turn on the device's
7675 * 'sdtr_able' bit. Write the new value to the microcode.
7676 */
7677static void
7678advansys_wide_enable_sdtr(AdvPortAddr iop_base, unsigned short tidmask)
7679{
7680 unsigned short cfg_word;
7681 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7682 if ((cfg_word & tidmask) != 0)
7683 return;
1da177e4 7684
51219358
MW
7685 cfg_word |= tidmask;
7686 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
1da177e4 7687
27c868c2 7688 /*
51219358
MW
7689 * Clear the microcode "SDTR negotiation" done indicator for the
7690 * target to cause it to negotiate with the new setting set above.
27c868c2 7691 */
51219358
MW
7692 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7693 cfg_word &= ~tidmask;
7694 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7695}
27c868c2 7696
51219358
MW
7697/*
7698 * PPR (Parallel Protocol Request) Capable
7699 *
7700 * If the device supports DT mode, then it must be PPR capable.
7701 * The PPR message will be used in place of the SDTR and WDTR
7702 * messages to negotiate synchronous speed and offset, transfer
7703 * width, and protocol options.
7704 */
7705static void advansys_wide_enable_ppr(ADV_DVC_VAR *adv_dvc,
7706 AdvPortAddr iop_base, unsigned short tidmask)
7707{
7708 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7709 adv_dvc->ppr_able |= tidmask;
7710 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7711}
27c868c2 7712
51219358
MW
7713static void
7714advansys_wide_slave_configure(struct scsi_device *sdev, ADV_DVC_VAR *adv_dvc)
7715{
7716 AdvPortAddr iop_base = adv_dvc->iop_base;
7717 unsigned short tidmask = 1 << sdev->id;
7718
7719 if (sdev->lun == 0) {
7720 /*
7721 * Handle WDTR, SDTR, and Tag Queuing. If the feature
7722 * is enabled in the EEPROM and the device supports the
7723 * feature, then enable it in the microcode.
7724 */
27c868c2 7725
51219358
MW
7726 if ((adv_dvc->wdtr_able & tidmask) && sdev->wdtr)
7727 advansys_wide_enable_wdtr(iop_base, tidmask);
7728 if ((adv_dvc->sdtr_able & tidmask) && sdev->sdtr)
7729 advansys_wide_enable_sdtr(iop_base, tidmask);
7730 if (adv_dvc->chip_type == ADV_CHIP_ASC38C1600 && sdev->ppr)
7731 advansys_wide_enable_ppr(adv_dvc, iop_base, tidmask);
7732
7733 /*
7734 * Tag Queuing is disabled for the BIOS which runs in polled
7735 * mode and would see no benefit from Tag Queuing. Also by
7736 * disabling Tag Queuing in the BIOS devices with Tag Queuing
7737 * bugs will at least work with the BIOS.
7738 */
7739 if ((adv_dvc->tagqng_able & tidmask) &&
7740 sdev->tagged_supported) {
7741 unsigned short cfg_word;
7742 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, cfg_word);
7743 cfg_word |= tidmask;
7744 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
7745 cfg_word);
7746 AdvWriteByteLram(iop_base,
7747 ASC_MC_NUMBER_OF_MAX_CMD + sdev->id,
7748 adv_dvc->max_dvc_qng);
27c868c2
MW
7749 }
7750 }
1da177e4 7751
db5ed4df
CH
7752 if ((adv_dvc->tagqng_able & tidmask) && sdev->tagged_supported)
7753 scsi_change_queue_depth(sdev, adv_dvc->max_dvc_qng);
51219358 7754}
27c868c2 7755
51219358
MW
7756/*
7757 * Set the number of commands to queue per device for the
7758 * specified host adapter.
7759 */
7760static int advansys_slave_configure(struct scsi_device *sdev)
7761{
d2411495 7762 struct asc_board *boardp = shost_priv(sdev->host);
27c868c2 7763
51219358
MW
7764 if (ASC_NARROW_BOARD(boardp))
7765 advansys_narrow_slave_configure(sdev,
7766 &boardp->dvc_var.asc_dvc_var);
7767 else
7768 advansys_wide_slave_configure(sdev,
7769 &boardp->dvc_var.adv_dvc_var);
1da177e4 7770
51219358
MW
7771 return 0;
7772}
27c868c2 7773
b249c7fd
MW
7774static __le32 advansys_get_sense_buffer_dma(struct scsi_cmnd *scp)
7775{
7776 struct asc_board *board = shost_priv(scp->device->host);
7777 scp->SCp.dma_handle = dma_map_single(board->dev, scp->sense_buffer,
b80ca4f7 7778 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
b249c7fd 7779 dma_cache_sync(board->dev, scp->sense_buffer,
b80ca4f7 7780 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
b249c7fd
MW
7781 return cpu_to_le32(scp->SCp.dma_handle);
7782}
7783
d2411495 7784static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
05848b6e 7785 struct asc_scsi_q *asc_scsi_q)
51219358 7786{
b249c7fd 7787 struct asc_dvc_var *asc_dvc = &boardp->dvc_var.asc_dvc_var;
52c334e9 7788 int use_sg;
9c17c62a 7789 u32 srb_tag;
52c334e9 7790
05848b6e 7791 memset(asc_scsi_q, 0, sizeof(*asc_scsi_q));
27c868c2
MW
7792
7793 /*
9c17c62a
HR
7794 * Set the srb_tag to the command tag + 1, as
7795 * srb_tag '0' is used internally by the chip.
27c868c2 7796 */
9c17c62a
HR
7797 srb_tag = scp->request->tag + 1;
7798 asc_scsi_q->q2.srb_tag = srb_tag;
27c868c2
MW
7799
7800 /*
51219358 7801 * Build the ASC_SCSI_Q request.
27c868c2 7802 */
05848b6e
MW
7803 asc_scsi_q->cdbptr = &scp->cmnd[0];
7804 asc_scsi_q->q2.cdb_len = scp->cmd_len;
7805 asc_scsi_q->q1.target_id = ASC_TID_TO_TARGET_ID(scp->device->id);
7806 asc_scsi_q->q1.target_lun = scp->device->lun;
7807 asc_scsi_q->q2.target_ix =
51219358 7808 ASC_TIDLUN_TO_IX(scp->device->id, scp->device->lun);
b249c7fd 7809 asc_scsi_q->q1.sense_addr = advansys_get_sense_buffer_dma(scp);
b80ca4f7 7810 asc_scsi_q->q1.sense_len = SCSI_SENSE_BUFFERSIZE;
27c868c2
MW
7811
7812 /*
51219358
MW
7813 * If there are any outstanding requests for the current target,
7814 * then every 255th request send an ORDERED request. This heuristic
7815 * tries to retain the benefit of request sorting while preventing
7816 * request starvation. 255 is the max number of tags or pending commands
7817 * a device may have outstanding.
7818 *
7819 * The request count is incremented below for every successfully
7820 * started request.
27c868c2 7821 *
27c868c2 7822 */
b249c7fd 7823 if ((asc_dvc->cur_dvc_qng[scp->device->id] > 0) &&
51219358 7824 (boardp->reqcnt[scp->device->id] % 255) == 0) {
68d81f40 7825 asc_scsi_q->q2.tag_code = ORDERED_QUEUE_TAG;
51219358 7826 } else {
68d81f40 7827 asc_scsi_q->q2.tag_code = SIMPLE_QUEUE_TAG;
51219358 7828 }
27c868c2 7829
52c334e9
MW
7830 /* Build ASC_SCSI_Q */
7831 use_sg = scsi_dma_map(scp);
7832 if (use_sg != 0) {
51219358 7833 int sgcnt;
51219358 7834 struct scatterlist *slp;
05848b6e 7835 struct asc_sg_head *asc_sg_head;
27c868c2 7836
51219358 7837 if (use_sg > scp->device->host->sg_tablesize) {
9d0e96eb
MW
7838 scmd_printk(KERN_ERR, scp, "use_sg %d > "
7839 "sg_tablesize %d\n", use_sg,
7840 scp->device->host->sg_tablesize);
52c334e9 7841 scsi_dma_unmap(scp);
51219358
MW
7842 scp->result = HOST_BYTE(DID_ERROR);
7843 return ASC_ERROR;
7844 }
27c868c2 7845
05848b6e
MW
7846 asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) +
7847 use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC);
7848 if (!asc_sg_head) {
52c334e9 7849 scsi_dma_unmap(scp);
05848b6e
MW
7850 scp->result = HOST_BYTE(DID_SOFT_ERROR);
7851 return ASC_ERROR;
7852 }
51219358 7853
05848b6e
MW
7854 asc_scsi_q->q1.cntl |= QC_SG_HEAD;
7855 asc_scsi_q->sg_head = asc_sg_head;
7856 asc_scsi_q->q1.data_cnt = 0;
7857 asc_scsi_q->q1.data_addr = 0;
51219358 7858 /* This is a byte value, otherwise it would need to be swapped. */
05848b6e 7859 asc_sg_head->entry_cnt = asc_scsi_q->q1.sg_queue_cnt = use_sg;
52c334e9 7860 ASC_STATS_ADD(scp->device->host, xfer_elem,
05848b6e 7861 asc_sg_head->entry_cnt);
51219358
MW
7862
7863 /*
7864 * Convert scatter-gather list into ASC_SG_HEAD list.
7865 */
52c334e9 7866 scsi_for_each_sg(scp, slp, use_sg, sgcnt) {
05848b6e 7867 asc_sg_head->sg_list[sgcnt].addr =
51219358 7868 cpu_to_le32(sg_dma_address(slp));
05848b6e 7869 asc_sg_head->sg_list[sgcnt].bytes =
51219358 7870 cpu_to_le32(sg_dma_len(slp));
52c334e9
MW
7871 ASC_STATS_ADD(scp->device->host, xfer_sect,
7872 DIV_ROUND_UP(sg_dma_len(slp), 512));
27c868c2
MW
7873 }
7874 }
1da177e4 7875
52c334e9
MW
7876 ASC_STATS(scp->device->host, xfer_cnt);
7877
b352f923 7878 ASC_DBG_PRT_ASC_SCSI_Q(2, asc_scsi_q);
51219358
MW
7879 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7880
7881 return ASC_NOERROR;
27c868c2 7882}
1da177e4 7883
27c868c2 7884/*
51219358 7885 * Build scatter-gather list for Adv Library (Wide Board).
27c868c2 7886 *
51219358
MW
7887 * Additional ADV_SG_BLOCK structures will need to be allocated
7888 * if the total number of scatter-gather elements exceeds
7889 * NO_OF_SG_PER_BLOCK (15). The ADV_SG_BLOCK structures are
7890 * assumed to be physically contiguous.
27c868c2 7891 *
51219358
MW
7892 * Return:
7893 * ADV_SUCCESS(1) - SG List successfully created
7894 * ADV_ERROR(-1) - SG List creation failed
27c868c2 7895 */
51219358 7896static int
4b47e464
HR
7897adv_get_sglist(struct asc_board *boardp, adv_req_t *reqp,
7898 ADV_SCSI_REQ_Q *scsiqp, struct scsi_cmnd *scp, int use_sg)
27c868c2 7899{
0ce53822 7900 adv_sgblk_t *sgblkp, *prev_sgblkp;
51219358
MW
7901 struct scatterlist *slp;
7902 int sg_elem_cnt;
7903 ADV_SG_BLOCK *sg_block, *prev_sg_block;
0ce53822 7904 dma_addr_t sgblk_paddr;
27c868c2 7905 int i;
27c868c2 7906
52c334e9 7907 slp = scsi_sglist(scp);
51219358 7908 sg_elem_cnt = use_sg;
0ce53822 7909 prev_sgblkp = NULL;
51219358
MW
7910 prev_sg_block = NULL;
7911 reqp->sgblkp = NULL;
1da177e4 7912
51219358
MW
7913 for (;;) {
7914 /*
7915 * Allocate a 'adv_sgblk_t' structure from the board free
7916 * list. One 'adv_sgblk_t' structure holds NO_OF_SG_PER_BLOCK
7917 * (15) scatter-gather elements.
7918 */
0ce53822
HR
7919 sgblkp = dma_pool_alloc(boardp->adv_sgblk_pool, GFP_ATOMIC,
7920 &sgblk_paddr);
7921 if (!sgblkp) {
b352f923 7922 ASC_DBG(1, "no free adv_sgblk_t\n");
51219358 7923 ASC_STATS(scp->device->host, adv_build_nosg);
1da177e4 7924
51219358
MW
7925 /*
7926 * Allocation failed. Free 'adv_sgblk_t' structures
7927 * already allocated for the request.
7928 */
7929 while ((sgblkp = reqp->sgblkp) != NULL) {
7930 /* Remove 'sgblkp' from the request list. */
7931 reqp->sgblkp = sgblkp->next_sgblkp;
0ce53822
HR
7932 sgblkp->next_sgblkp = NULL;
7933 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
7934 sgblkp->sg_addr);
51219358
MW
7935 }
7936 return ASC_BUSY;
7937 }
51219358 7938 /* Complete 'adv_sgblk_t' board allocation. */
0ce53822 7939 sgblkp->sg_addr = sgblk_paddr;
51219358 7940 sgblkp->next_sgblkp = NULL;
0ce53822 7941 sg_block = &sgblkp->sg_block;
27c868c2 7942
51219358
MW
7943 /*
7944 * Check if this is the first 'adv_sgblk_t' for the
7945 * request.
7946 */
7947 if (reqp->sgblkp == NULL) {
7948 /* Request's first scatter-gather block. */
7949 reqp->sgblkp = sgblkp;
27c868c2 7950
51219358
MW
7951 /*
7952 * Set ADV_SCSI_REQ_T ADV_SG_BLOCK virtual and physical
7953 * address pointers.
7954 */
7955 scsiqp->sg_list_ptr = sg_block;
0ce53822 7956 scsiqp->sg_real_addr = cpu_to_le32(sgblk_paddr);
51219358
MW
7957 } else {
7958 /* Request's second or later scatter-gather block. */
0ce53822 7959 prev_sgblkp->next_sgblkp = sgblkp;
51219358
MW
7960
7961 /*
7962 * Point the previous ADV_SG_BLOCK structure to
7963 * the newly allocated ADV_SG_BLOCK structure.
7964 */
0ce53822 7965 prev_sg_block->sg_ptr = cpu_to_le32(sgblk_paddr);
27c868c2 7966 }
1da177e4 7967
51219358
MW
7968 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
7969 sg_block->sg_list[i].sg_addr =
7970 cpu_to_le32(sg_dma_address(slp));
7971 sg_block->sg_list[i].sg_count =
7972 cpu_to_le32(sg_dma_len(slp));
52c334e9
MW
7973 ASC_STATS_ADD(scp->device->host, xfer_sect,
7974 DIV_ROUND_UP(sg_dma_len(slp), 512));
27c868c2 7975
0ce53822
HR
7976 if (--sg_elem_cnt == 0) {
7977 /*
7978 * Last ADV_SG_BLOCK and scatter-gather entry.
7979 */
51219358 7980 sg_block->sg_cnt = i + 1;
0ce53822 7981 sg_block->sg_ptr = 0L; /* Last ADV_SG_BLOCK in list. */
51219358
MW
7982 return ADV_SUCCESS;
7983 }
7984 slp++;
7985 }
7986 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
7987 prev_sg_block = sg_block;
0ce53822 7988 prev_sgblkp = sgblkp;
27c868c2 7989 }
51219358 7990}
1da177e4 7991
51219358
MW
7992/*
7993 * Build a request structure for the Adv Library (Wide Board).
7994 *
7995 * If an adv_req_t can not be allocated to issue the request,
7996 * then return ASC_BUSY. If an error occurs, then return ASC_ERROR.
7997 *
7998 * Multi-byte fields in the ASC_SCSI_REQ_Q that are used by the
7999 * microcode for DMA addresses or math operations are byte swapped
8000 * to little-endian order.
8001 */
8002static int
d2411495 8003adv_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
4b47e464 8004 adv_req_t **adv_reqpp)
51219358 8005{
9c17c62a 8006 u32 srb_tag = scp->request->tag;
51219358
MW
8007 adv_req_t *reqp;
8008 ADV_SCSI_REQ_Q *scsiqp;
51219358 8009 int ret;
52c334e9 8010 int use_sg;
811ddc05 8011 dma_addr_t sense_addr;
1da177e4 8012
27c868c2 8013 /*
51219358
MW
8014 * Allocate an adv_req_t structure from the board to execute
8015 * the command.
27c868c2 8016 */
9c17c62a
HR
8017 reqp = &boardp->adv_reqp[srb_tag];
8018 if (reqp->cmndp && reqp->cmndp != scp ) {
b352f923 8019 ASC_DBG(1, "no free adv_req_t\n");
51219358
MW
8020 ASC_STATS(scp->device->host, adv_build_noreq);
8021 return ASC_BUSY;
27c868c2 8022 }
1da177e4 8023
4b47e464
HR
8024 reqp->req_addr = boardp->adv_reqp_addr + (srb_tag * sizeof(adv_req_t));
8025
8026 scsiqp = &reqp->scsi_req_q;
27c868c2
MW
8027
8028 /*
51219358 8029 * Initialize the structure.
27c868c2 8030 */
51219358 8031 scsiqp->cntl = scsiqp->scsi_cntl = scsiqp->done_status = 0;
27c868c2
MW
8032
8033 /*
9c17c62a 8034 * Set the srb_tag to the command tag.
27c868c2 8035 */
9c17c62a 8036 scsiqp->srb_tag = srb_tag;
27c868c2
MW
8037
8038 /*
4b47e464 8039 * Set 'host_scribble' to point to the adv_req_t structure.
27c868c2 8040 */
51219358 8041 reqp->cmndp = scp;
9c17c62a 8042 scp->host_scribble = (void *)reqp;
27c868c2
MW
8043
8044 /*
51219358 8045 * Build the ADV_SCSI_REQ_Q request.
27c868c2 8046 */
51219358
MW
8047
8048 /* Set CDB length and copy it to the request structure. */
8049 scsiqp->cdb_len = scp->cmd_len;
8050 /* Copy first 12 CDB bytes to cdb[]. */
811ddc05 8051 memcpy(scsiqp->cdb, scp->cmnd, scp->cmd_len < 12 ? scp->cmd_len : 12);
51219358 8052 /* Copy last 4 CDB bytes, if present, to cdb16[]. */
811ddc05
HR
8053 if (scp->cmd_len > 12) {
8054 int cdb16_len = scp->cmd_len - 12;
8055
8056 memcpy(scsiqp->cdb16, &scp->cmnd[12], cdb16_len);
27c868c2 8057 }
1da177e4 8058
51219358
MW
8059 scsiqp->target_id = scp->device->id;
8060 scsiqp->target_lun = scp->device->lun;
8061
811ddc05
HR
8062 sense_addr = dma_map_single(boardp->dev, scp->sense_buffer,
8063 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
8064 scsiqp->sense_addr = cpu_to_le32(sense_addr);
8065 scsiqp->sense_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
27c868c2 8066
52c334e9 8067 /* Build ADV_SCSI_REQ_Q */
51219358 8068
52c334e9
MW
8069 use_sg = scsi_dma_map(scp);
8070 if (use_sg == 0) {
8071 /* Zero-length transfer */
51219358 8072 reqp->sgblkp = NULL;
52c334e9 8073 scsiqp->data_cnt = 0;
52c334e9
MW
8074
8075 scsiqp->data_addr = 0;
51219358
MW
8076 scsiqp->sg_list_ptr = NULL;
8077 scsiqp->sg_real_addr = 0;
51219358 8078 } else {
51219358 8079 if (use_sg > ADV_MAX_SG_LIST) {
9d0e96eb
MW
8080 scmd_printk(KERN_ERR, scp, "use_sg %d > "
8081 "ADV_MAX_SG_LIST %d\n", use_sg,
51219358 8082 scp->device->host->sg_tablesize);
52c334e9 8083 scsi_dma_unmap(scp);
51219358 8084 scp->result = HOST_BYTE(DID_ERROR);
9c17c62a
HR
8085 reqp->cmndp = NULL;
8086 scp->host_scribble = NULL;
51219358
MW
8087
8088 return ASC_ERROR;
8089 }
8090
52c334e9
MW
8091 scsiqp->data_cnt = cpu_to_le32(scsi_bufflen(scp));
8092
4b47e464 8093 ret = adv_get_sglist(boardp, reqp, scsiqp, scp, use_sg);
51219358 8094 if (ret != ADV_SUCCESS) {
9c17c62a
HR
8095 scsi_dma_unmap(scp);
8096 scp->result = HOST_BYTE(DID_ERROR);
8097 reqp->cmndp = NULL;
8098 scp->host_scribble = NULL;
51219358
MW
8099
8100 return ret;
8101 }
8102
52c334e9 8103 ASC_STATS_ADD(scp->device->host, xfer_elem, use_sg);
27c868c2 8104 }
1da177e4 8105
52c334e9
MW
8106 ASC_STATS(scp->device->host, xfer_cnt);
8107
51219358
MW
8108 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
8109 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
27c868c2 8110
4b47e464 8111 *adv_reqpp = reqp;
27c868c2 8112
51219358
MW
8113 return ASC_NOERROR;
8114}
8115
8116static int AscSgListToQueue(int sg_list)
8117{
8118 int n_sg_list_qs;
8119
8120 n_sg_list_qs = ((sg_list - 1) / ASC_SG_LIST_PER_Q);
8121 if (((sg_list - 1) % ASC_SG_LIST_PER_Q) != 0)
8122 n_sg_list_qs++;
8123 return n_sg_list_qs + 1;
8124}
8125
8126static uint
8127AscGetNumOfFreeQueue(ASC_DVC_VAR *asc_dvc, uchar target_ix, uchar n_qs)
8128{
8129 uint cur_used_qs;
8130 uint cur_free_qs;
8131 ASC_SCSI_BIT_ID_TYPE target_id;
8132 uchar tid_no;
8133
8134 target_id = ASC_TIX_TO_TARGET_ID(target_ix);
8135 tid_no = ASC_TIX_TO_TID(target_ix);
8136 if ((asc_dvc->unit_not_ready & target_id) ||
8137 (asc_dvc->queue_full_or_busy & target_id)) {
8138 return 0;
8139 }
8140 if (n_qs == 1) {
8141 cur_used_qs = (uint) asc_dvc->cur_total_qng +
8142 (uint) asc_dvc->last_q_shortage + (uint) ASC_MIN_FREE_Q;
8143 } else {
8144 cur_used_qs = (uint) asc_dvc->cur_total_qng +
8145 (uint) ASC_MIN_FREE_Q;
8146 }
8147 if ((uint) (cur_used_qs + n_qs) <= (uint) asc_dvc->max_total_qng) {
8148 cur_free_qs = (uint) asc_dvc->max_total_qng - cur_used_qs;
8149 if (asc_dvc->cur_dvc_qng[tid_no] >=
8150 asc_dvc->max_dvc_qng[tid_no]) {
8151 return 0;
8152 }
8153 return cur_free_qs;
8154 }
8155 if (n_qs > 1) {
8156 if ((n_qs > asc_dvc->last_q_shortage)
8157 && (n_qs <= (asc_dvc->max_total_qng - ASC_MIN_FREE_Q))) {
8158 asc_dvc->last_q_shortage = n_qs;
8159 }
8160 }
8161 return 0;
8162}
8163
8164static uchar AscAllocFreeQueue(PortAddr iop_base, uchar free_q_head)
8165{
8166 ushort q_addr;
8167 uchar next_qp;
8168 uchar q_status;
8169
8170 q_addr = ASC_QNO_TO_QADDR(free_q_head);
8171 q_status = (uchar)AscReadLramByte(iop_base,
8172 (ushort)(q_addr +
8173 ASC_SCSIQ_B_STATUS));
8174 next_qp = AscReadLramByte(iop_base, (ushort)(q_addr + ASC_SCSIQ_B_FWD));
8175 if (((q_status & QS_READY) == 0) && (next_qp != ASC_QLINK_END))
8176 return next_qp;
8177 return ASC_QLINK_END;
8178}
8179
8180static uchar
8181AscAllocMultipleFreeQueue(PortAddr iop_base, uchar free_q_head, uchar n_free_q)
8182{
8183 uchar i;
27c868c2 8184
51219358
MW
8185 for (i = 0; i < n_free_q; i++) {
8186 free_q_head = AscAllocFreeQueue(iop_base, free_q_head);
8187 if (free_q_head == ASC_QLINK_END)
8188 break;
8189 }
8190 return free_q_head;
8191}
27c868c2 8192
51219358
MW
8193/*
8194 * void
8195 * DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
8196 *
8197 * Calling/Exit State:
8198 * none
8199 *
8200 * Description:
8201 * Output an ASC_SCSI_Q structure to the chip
8202 */
8203static void
8204DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
8205{
8206 int i;
8207
8208 ASC_DBG_PRT_HEX(2, "DvcPutScsiQ", outbuf, 2 * words);
8209 AscSetChipLramAddr(iop_base, s_addr);
8210 for (i = 0; i < 2 * words; i += 2) {
8211 if (i == 4 || i == 20) {
8212 continue;
8213 }
8214 outpw(iop_base + IOP_RAM_DATA,
8215 ((ushort)outbuf[i + 1] << 8) | outbuf[i]);
27c868c2 8216 }
51219358 8217}
1da177e4 8218
51219358
MW
8219static int AscPutReadyQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8220{
8221 ushort q_addr;
8222 uchar tid_no;
8223 uchar sdtr_data;
8224 uchar syn_period_ix;
8225 uchar syn_offset;
8226 PortAddr iop_base;
8227
8228 iop_base = asc_dvc->iop_base;
8229 if (((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) &&
8230 ((asc_dvc->sdtr_done & scsiq->q1.target_id) == 0)) {
8231 tid_no = ASC_TIX_TO_TID(scsiq->q2.target_ix);
8232 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8233 syn_period_ix =
8234 (sdtr_data >> 4) & (asc_dvc->max_sdtr_index - 1);
8235 syn_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
8236 AscMsgOutSDTR(asc_dvc,
8237 asc_dvc->sdtr_period_tbl[syn_period_ix],
8238 syn_offset);
8239 scsiq->q1.cntl |= QC_MSG_OUT;
8240 }
8241 q_addr = ASC_QNO_TO_QADDR(q_no);
8242 if ((scsiq->q1.target_id & asc_dvc->use_tagged_qng) == 0) {
68d81f40 8243 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
27c868c2 8244 }
51219358
MW
8245 scsiq->q1.status = QS_FREE;
8246 AscMemWordCopyPtrToLram(iop_base,
8247 q_addr + ASC_SCSIQ_CDB_BEG,
8248 (uchar *)scsiq->cdbptr, scsiq->q2.cdb_len >> 1);
8249
8250 DvcPutScsiQ(iop_base,
8251 q_addr + ASC_SCSIQ_CPY_BEG,
8252 (uchar *)&scsiq->q1.cntl,
8253 ((sizeof(ASC_SCSIQ_1) + sizeof(ASC_SCSIQ_2)) / 2) - 1);
8254 AscWriteLramWord(iop_base,
8255 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS),
8256 (ushort)(((ushort)scsiq->q1.
8257 q_no << 8) | (ushort)QS_READY));
8258 return 1;
8259}
8260
8261static int
8262AscPutReadySgListQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8263{
8264 int sta;
8265 int i;
8266 ASC_SG_HEAD *sg_head;
8267 ASC_SG_LIST_Q scsi_sg_q;
8268 ASC_DCNT saved_data_addr;
8269 ASC_DCNT saved_data_cnt;
8270 PortAddr iop_base;
8271 ushort sg_list_dwords;
8272 ushort sg_index;
8273 ushort sg_entry_cnt;
8274 ushort q_addr;
8275 uchar next_qp;
1da177e4 8276
51219358
MW
8277 iop_base = asc_dvc->iop_base;
8278 sg_head = scsiq->sg_head;
8279 saved_data_addr = scsiq->q1.data_addr;
8280 saved_data_cnt = scsiq->q1.data_cnt;
8281 scsiq->q1.data_addr = (ASC_PADDR) sg_head->sg_list[0].addr;
8282 scsiq->q1.data_cnt = (ASC_DCNT) sg_head->sg_list[0].bytes;
8283#if CC_VERY_LONG_SG_LIST
27c868c2 8284 /*
51219358
MW
8285 * If sg_head->entry_cnt is greater than ASC_MAX_SG_LIST
8286 * then not all SG elements will fit in the allocated queues.
8287 * The rest of the SG elements will be copied when the RISC
8288 * completes the SG elements that fit and halts.
27c868c2 8289 */
51219358
MW
8290 if (sg_head->entry_cnt > ASC_MAX_SG_LIST) {
8291 /*
8292 * Set sg_entry_cnt to be the number of SG elements that
8293 * will fit in the allocated SG queues. It is minus 1, because
8294 * the first SG element is handled above. ASC_MAX_SG_LIST is
8295 * already inflated by 1 to account for this. For example it
8296 * may be 50 which is 1 + 7 queues * 7 SG elements.
8297 */
8298 sg_entry_cnt = ASC_MAX_SG_LIST - 1;
27c868c2 8299
51219358
MW
8300 /*
8301 * Keep track of remaining number of SG elements that will
8302 * need to be handled from a_isr.c.
8303 */
8304 scsiq->remain_sg_entry_cnt =
8305 sg_head->entry_cnt - ASC_MAX_SG_LIST;
8306 } else {
8307#endif /* CC_VERY_LONG_SG_LIST */
8308 /*
8309 * Set sg_entry_cnt to be the number of SG elements that
8310 * will fit in the allocated SG queues. It is minus 1, because
8311 * the first SG element is handled above.
8312 */
8313 sg_entry_cnt = sg_head->entry_cnt - 1;
8314#if CC_VERY_LONG_SG_LIST
8315 }
8316#endif /* CC_VERY_LONG_SG_LIST */
8317 if (sg_entry_cnt != 0) {
8318 scsiq->q1.cntl |= QC_SG_HEAD;
8319 q_addr = ASC_QNO_TO_QADDR(q_no);
8320 sg_index = 1;
8321 scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
8322 scsi_sg_q.sg_head_qp = q_no;
8323 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
8324 for (i = 0; i < sg_head->queue_cnt; i++) {
8325 scsi_sg_q.seq_no = i + 1;
8326 if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
8327 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
8328 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
8329 if (i == 0) {
8330 scsi_sg_q.sg_list_cnt =
8331 ASC_SG_LIST_PER_Q;
8332 scsi_sg_q.sg_cur_list_cnt =
8333 ASC_SG_LIST_PER_Q;
8334 } else {
8335 scsi_sg_q.sg_list_cnt =
8336 ASC_SG_LIST_PER_Q - 1;
8337 scsi_sg_q.sg_cur_list_cnt =
8338 ASC_SG_LIST_PER_Q - 1;
8339 }
8340 } else {
8341#if CC_VERY_LONG_SG_LIST
8342 /*
8343 * This is the last SG queue in the list of
8344 * allocated SG queues. If there are more
8345 * SG elements than will fit in the allocated
8346 * queues, then set the QCSG_SG_XFER_MORE flag.
8347 */
8348 if (sg_head->entry_cnt > ASC_MAX_SG_LIST) {
8349 scsi_sg_q.cntl |= QCSG_SG_XFER_MORE;
8350 } else {
8351#endif /* CC_VERY_LONG_SG_LIST */
8352 scsi_sg_q.cntl |= QCSG_SG_XFER_END;
8353#if CC_VERY_LONG_SG_LIST
8354 }
8355#endif /* CC_VERY_LONG_SG_LIST */
8356 sg_list_dwords = sg_entry_cnt << 1;
8357 if (i == 0) {
8358 scsi_sg_q.sg_list_cnt = sg_entry_cnt;
8359 scsi_sg_q.sg_cur_list_cnt =
8360 sg_entry_cnt;
8361 } else {
8362 scsi_sg_q.sg_list_cnt =
8363 sg_entry_cnt - 1;
8364 scsi_sg_q.sg_cur_list_cnt =
8365 sg_entry_cnt - 1;
8366 }
8367 sg_entry_cnt = 0;
8368 }
8369 next_qp = AscReadLramByte(iop_base,
8370 (ushort)(q_addr +
8371 ASC_SCSIQ_B_FWD));
8372 scsi_sg_q.q_no = next_qp;
8373 q_addr = ASC_QNO_TO_QADDR(next_qp);
8374 AscMemWordCopyPtrToLram(iop_base,
8375 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
8376 (uchar *)&scsi_sg_q,
8377 sizeof(ASC_SG_LIST_Q) >> 1);
8378 AscMemDWordCopyPtrToLram(iop_base,
8379 q_addr + ASC_SGQ_LIST_BEG,
8380 (uchar *)&sg_head->
8381 sg_list[sg_index],
8382 sg_list_dwords);
8383 sg_index += ASC_SG_LIST_PER_Q;
8384 scsiq->next_sg_index = sg_index;
27c868c2 8385 }
51219358
MW
8386 } else {
8387 scsiq->q1.cntl &= ~QC_SG_HEAD;
27c868c2 8388 }
51219358
MW
8389 sta = AscPutReadyQueue(asc_dvc, scsiq, q_no);
8390 scsiq->q1.data_addr = saved_data_addr;
8391 scsiq->q1.data_cnt = saved_data_cnt;
8392 return (sta);
8393}
27c868c2 8394
51219358
MW
8395static int
8396AscSendScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar n_q_required)
8397{
8398 PortAddr iop_base;
8399 uchar free_q_head;
8400 uchar next_qp;
8401 uchar tid_no;
8402 uchar target_ix;
8403 int sta;
27c868c2 8404
51219358
MW
8405 iop_base = asc_dvc->iop_base;
8406 target_ix = scsiq->q2.target_ix;
8407 tid_no = ASC_TIX_TO_TID(target_ix);
8408 sta = 0;
8409 free_q_head = (uchar)AscGetVarFreeQHead(iop_base);
8410 if (n_q_required > 1) {
8411 next_qp = AscAllocMultipleFreeQueue(iop_base, free_q_head,
8412 (uchar)n_q_required);
8413 if (next_qp != ASC_QLINK_END) {
8414 asc_dvc->last_q_shortage = 0;
8415 scsiq->sg_head->queue_cnt = n_q_required - 1;
8416 scsiq->q1.q_no = free_q_head;
8417 sta = AscPutReadySgListQueue(asc_dvc, scsiq,
8418 free_q_head);
8419 }
8420 } else if (n_q_required == 1) {
8421 next_qp = AscAllocFreeQueue(iop_base, free_q_head);
8422 if (next_qp != ASC_QLINK_END) {
8423 scsiq->q1.q_no = free_q_head;
8424 sta = AscPutReadyQueue(asc_dvc, scsiq, free_q_head);
27c868c2
MW
8425 }
8426 }
51219358
MW
8427 if (sta == 1) {
8428 AscPutVarFreeQHead(iop_base, next_qp);
8429 asc_dvc->cur_total_qng += n_q_required;
8430 asc_dvc->cur_dvc_qng[tid_no]++;
27c868c2 8431 }
51219358
MW
8432 return sta;
8433}
27c868c2 8434
51219358
MW
8435#define ASC_SYN_OFFSET_ONE_DISABLE_LIST 16
8436static uchar _syn_offset_one_disable_cmd[ASC_SYN_OFFSET_ONE_DISABLE_LIST] = {
8437 INQUIRY,
8438 REQUEST_SENSE,
8439 READ_CAPACITY,
8440 READ_TOC,
8441 MODE_SELECT,
8442 MODE_SENSE,
8443 MODE_SELECT_10,
8444 MODE_SENSE_10,
8445 0xFF,
8446 0xFF,
8447 0xFF,
8448 0xFF,
8449 0xFF,
8450 0xFF,
8451 0xFF,
8452 0xFF
8453};
27c868c2 8454
51219358
MW
8455static int AscExeScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq)
8456{
8457 PortAddr iop_base;
8458 int sta;
8459 int n_q_required;
8460 int disable_syn_offset_one_fix;
8461 int i;
8462 ASC_PADDR addr;
8463 ushort sg_entry_cnt = 0;
8464 ushort sg_entry_cnt_minus_one = 0;
8465 uchar target_ix;
8466 uchar tid_no;
8467 uchar sdtr_data;
8468 uchar extra_bytes;
8469 uchar scsi_cmd;
8470 uchar disable_cmd;
8471 ASC_SG_HEAD *sg_head;
8472 ASC_DCNT data_cnt;
27c868c2 8473
51219358
MW
8474 iop_base = asc_dvc->iop_base;
8475 sg_head = scsiq->sg_head;
8476 if (asc_dvc->err_code != 0)
8477 return (ERR);
8478 scsiq->q1.q_no = 0;
8479 if ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0) {
8480 scsiq->q1.extra_bytes = 0;
27c868c2 8481 }
51219358
MW
8482 sta = 0;
8483 target_ix = scsiq->q2.target_ix;
8484 tid_no = ASC_TIX_TO_TID(target_ix);
8485 n_q_required = 1;
8486 if (scsiq->cdbptr[0] == REQUEST_SENSE) {
8487 if ((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) {
8488 asc_dvc->sdtr_done &= ~scsiq->q1.target_id;
8489 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8490 AscMsgOutSDTR(asc_dvc,
8491 asc_dvc->
8492 sdtr_period_tbl[(sdtr_data >> 4) &
8493 (uchar)(asc_dvc->
8494 max_sdtr_index -
8495 1)],
8496 (uchar)(sdtr_data & (uchar)
8497 ASC_SYN_MAX_OFFSET));
8498 scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);
8499 }
8500 }
8501 if (asc_dvc->in_critical_cnt != 0) {
8502 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CRITICAL_RE_ENTRY);
8503 return (ERR);
8504 }
8505 asc_dvc->in_critical_cnt++;
8506 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8507 if ((sg_entry_cnt = sg_head->entry_cnt) == 0) {
8508 asc_dvc->in_critical_cnt--;
8509 return (ERR);
8510 }
8511#if !CC_VERY_LONG_SG_LIST
8512 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8513 asc_dvc->in_critical_cnt--;
8514 return (ERR);
8515 }
8516#endif /* !CC_VERY_LONG_SG_LIST */
8517 if (sg_entry_cnt == 1) {
8518 scsiq->q1.data_addr =
8519 (ADV_PADDR)sg_head->sg_list[0].addr;
8520 scsiq->q1.data_cnt =
8521 (ADV_DCNT)sg_head->sg_list[0].bytes;
8522 scsiq->q1.cntl &= ~(QC_SG_HEAD | QC_SG_SWAP_QUEUE);
8523 }
8524 sg_entry_cnt_minus_one = sg_entry_cnt - 1;
8525 }
8526 scsi_cmd = scsiq->cdbptr[0];
8527 disable_syn_offset_one_fix = FALSE;
8528 if ((asc_dvc->pci_fix_asyn_xfer & scsiq->q1.target_id) &&
8529 !(asc_dvc->pci_fix_asyn_xfer_always & scsiq->q1.target_id)) {
8530 if (scsiq->q1.cntl & QC_SG_HEAD) {
8531 data_cnt = 0;
8532 for (i = 0; i < sg_entry_cnt; i++) {
8533 data_cnt +=
8534 (ADV_DCNT)le32_to_cpu(sg_head->sg_list[i].
8535 bytes);
8536 }
8537 } else {
8538 data_cnt = le32_to_cpu(scsiq->q1.data_cnt);
8539 }
8540 if (data_cnt != 0UL) {
8541 if (data_cnt < 512UL) {
8542 disable_syn_offset_one_fix = TRUE;
8543 } else {
8544 for (i = 0; i < ASC_SYN_OFFSET_ONE_DISABLE_LIST;
8545 i++) {
8546 disable_cmd =
8547 _syn_offset_one_disable_cmd[i];
8548 if (disable_cmd == 0xFF) {
8549 break;
8550 }
8551 if (scsi_cmd == disable_cmd) {
8552 disable_syn_offset_one_fix =
8553 TRUE;
8554 break;
8555 }
8556 }
8557 }
8558 }
8559 }
8560 if (disable_syn_offset_one_fix) {
68d81f40 8561 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
51219358
MW
8562 scsiq->q2.tag_code |= (ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX |
8563 ASC_TAG_FLAG_DISABLE_DISCONNECT);
8564 } else {
8565 scsiq->q2.tag_code &= 0x27;
8566 }
8567 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8568 if (asc_dvc->bug_fix_cntl) {
8569 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8570 if ((scsi_cmd == READ_6) ||
8571 (scsi_cmd == READ_10)) {
8572 addr =
8573 (ADV_PADDR)le32_to_cpu(sg_head->
8574 sg_list
8575 [sg_entry_cnt_minus_one].
8576 addr) +
8577 (ADV_DCNT)le32_to_cpu(sg_head->
8578 sg_list
8579 [sg_entry_cnt_minus_one].
8580 bytes);
8581 extra_bytes =
8582 (uchar)((ushort)addr & 0x0003);
8583 if ((extra_bytes != 0)
8584 &&
8585 ((scsiq->q2.
8586 tag_code &
8587 ASC_TAG_FLAG_EXTRA_BYTES)
8588 == 0)) {
8589 scsiq->q2.tag_code |=
8590 ASC_TAG_FLAG_EXTRA_BYTES;
8591 scsiq->q1.extra_bytes =
8592 extra_bytes;
8593 data_cnt =
8594 le32_to_cpu(sg_head->
8595 sg_list
8596 [sg_entry_cnt_minus_one].
8597 bytes);
8598 data_cnt -=
8599 (ASC_DCNT) extra_bytes;
8600 sg_head->
8601 sg_list
8602 [sg_entry_cnt_minus_one].
8603 bytes =
8604 cpu_to_le32(data_cnt);
8605 }
8606 }
8607 }
8608 }
8609 sg_head->entry_to_copy = sg_head->entry_cnt;
8610#if CC_VERY_LONG_SG_LIST
27c868c2 8611 /*
51219358
MW
8612 * Set the sg_entry_cnt to the maximum possible. The rest of
8613 * the SG elements will be copied when the RISC completes the
8614 * SG elements that fit and halts.
27c868c2 8615 */
51219358
MW
8616 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8617 sg_entry_cnt = ASC_MAX_SG_LIST;
8618 }
8619#endif /* CC_VERY_LONG_SG_LIST */
8620 n_q_required = AscSgListToQueue(sg_entry_cnt);
8621 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, n_q_required) >=
8622 (uint) n_q_required)
8623 || ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8624 if ((sta =
8625 AscSendScsiQueue(asc_dvc, scsiq,
8626 n_q_required)) == 1) {
8627 asc_dvc->in_critical_cnt--;
8628 return (sta);
27c868c2 8629 }
51219358
MW
8630 }
8631 } else {
8632 if (asc_dvc->bug_fix_cntl) {
8633 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8634 if ((scsi_cmd == READ_6) ||
8635 (scsi_cmd == READ_10)) {
8636 addr =
8637 le32_to_cpu(scsiq->q1.data_addr) +
8638 le32_to_cpu(scsiq->q1.data_cnt);
8639 extra_bytes =
8640 (uchar)((ushort)addr & 0x0003);
8641 if ((extra_bytes != 0)
8642 &&
8643 ((scsiq->q2.
8644 tag_code &
8645 ASC_TAG_FLAG_EXTRA_BYTES)
8646 == 0)) {
8647 data_cnt =
8648 le32_to_cpu(scsiq->q1.
8649 data_cnt);
8650 if (((ushort)data_cnt & 0x01FF)
8651 == 0) {
8652 scsiq->q2.tag_code |=
8653 ASC_TAG_FLAG_EXTRA_BYTES;
8654 data_cnt -= (ASC_DCNT)
8655 extra_bytes;
8656 scsiq->q1.data_cnt =
8657 cpu_to_le32
8658 (data_cnt);
8659 scsiq->q1.extra_bytes =
8660 extra_bytes;
8661 }
8662 }
8663 }
8664 }
8665 }
8666 n_q_required = 1;
8667 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, 1) >= 1) ||
8668 ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8669 if ((sta = AscSendScsiQueue(asc_dvc, scsiq,
8670 n_q_required)) == 1) {
8671 asc_dvc->in_critical_cnt--;
8672 return (sta);
27c868c2
MW
8673 }
8674 }
8675 }
51219358
MW
8676 asc_dvc->in_critical_cnt--;
8677 return (sta);
1da177e4
LT
8678}
8679
8680/*
51219358 8681 * AdvExeScsiQueue() - Send a request to the RISC microcode program.
1da177e4 8682 *
51219358
MW
8683 * Allocate a carrier structure, point the carrier to the ADV_SCSI_REQ_Q,
8684 * add the carrier to the ICQ (Initiator Command Queue), and tickle the
8685 * RISC to notify it a new command is ready to be executed.
8686 *
8687 * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
8688 * set to SCSI_MAX_RETRY.
8689 *
8690 * Multi-byte fields in the ASC_SCSI_REQ_Q that are used by the microcode
8691 * for DMA addresses or math operations are byte swapped to little-endian
8692 * order.
8693 *
8694 * Return:
8695 * ADV_SUCCESS(1) - The request was successfully queued.
8696 * ADV_BUSY(0) - Resource unavailable; Retry again after pending
8697 * request completes.
8698 * ADV_ERROR(-1) - Invalid ADV_SCSI_REQ_Q request structure
8699 * host IC error.
8700 */
4b47e464 8701static int AdvExeScsiQueue(ADV_DVC_VAR *asc_dvc, adv_req_t *reqp)
51219358
MW
8702{
8703 AdvPortAddr iop_base;
51219358 8704 ADV_CARR_T *new_carrp;
4b47e464 8705 ADV_SCSI_REQ_Q *scsiq = &reqp->scsi_req_q;
1da177e4 8706
27c868c2 8707 /*
51219358 8708 * The ADV_SCSI_REQ_Q 'target_id' field should never exceed ADV_MAX_TID.
27c868c2 8709 */
51219358
MW
8710 if (scsiq->target_id > ADV_MAX_TID) {
8711 scsiq->host_status = QHSTA_M_INVALID_DEVICE;
8712 scsiq->done_status = QD_WITH_ERROR;
27c868c2
MW
8713 return ADV_ERROR;
8714 }
1da177e4 8715
51219358 8716 iop_base = asc_dvc->iop_base;
1da177e4 8717
27c868c2 8718 /*
51219358
MW
8719 * Allocate a carrier ensuring at least one carrier always
8720 * remains on the freelist and initialize fields.
27c868c2 8721 */
98b96a7d
HR
8722 new_carrp = adv_get_next_carrier(asc_dvc);
8723 if (!new_carrp) {
8724 ASC_DBG(1, "No free carriers\n");
51219358 8725 return ADV_BUSY;
27c868c2 8726 }
1da177e4 8727
98b96a7d 8728 asc_dvc->carr_pending_cnt++;
27c868c2
MW
8729
8730 /*
51219358 8731 * Clear the ADV_SCSI_REQ_Q done flag.
27c868c2 8732 */
51219358 8733 scsiq->a_flag &= ~ADV_SCSIQ_DONE;
27c868c2 8734
51219358 8735 /* Save virtual and physical address of ADV_SCSI_REQ_Q and carrier. */
4b47e464
HR
8736 scsiq->scsiq_ptr = cpu_to_le32(scsiq->srb_tag);
8737 scsiq->scsiq_rptr = cpu_to_le32(reqp->req_addr);
51219358 8738
98b96a7d 8739 scsiq->carr_va = asc_dvc->icq_sp->carr_va;
51219358 8740 scsiq->carr_pa = asc_dvc->icq_sp->carr_pa;
1da177e4 8741
27c868c2 8742 /*
51219358
MW
8743 * Use the current stopper to send the ADV_SCSI_REQ_Q command to
8744 * the microcode. The newly allocated stopper will become the new
8745 * stopper.
27c868c2 8746 */
4b47e464 8747 asc_dvc->icq_sp->areq_vpa = scsiq->scsiq_rptr;
1da177e4 8748
27c868c2 8749 /*
51219358
MW
8750 * Set the 'next_vpa' pointer for the old stopper to be the
8751 * physical address of the new stopper. The RISC can only
8752 * follow physical addresses.
27c868c2 8753 */
51219358 8754 asc_dvc->icq_sp->next_vpa = new_carrp->carr_pa;
27c868c2
MW
8755
8756 /*
51219358 8757 * Set the host adapter stopper pointer to point to the new carrier.
27c868c2 8758 */
51219358
MW
8759 asc_dvc->icq_sp = new_carrp;
8760
8761 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
8762 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
8763 /*
8764 * Tickle the RISC to tell it to read its Command Queue Head pointer.
8765 */
8766 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_A);
8767 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
8768 /*
8769 * Clear the tickle value. In the ASC-3550 the RISC flag
8770 * command 'clr_tickle_a' does not work unless the host
8771 * value is cleared.
8772 */
8773 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
8774 ADV_TICKLE_NOP);
8775 }
8776 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
8777 /*
8778 * Notify the RISC a carrier is ready by writing the physical
8779 * address of the new carrier stopper to the COMMA register.
8780 */
8781 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
8782 le32_to_cpu(new_carrp->carr_pa));
27c868c2 8783 }
1da177e4 8784
51219358
MW
8785 return ADV_SUCCESS;
8786}
8787
8788/*
8789 * Execute a single 'Scsi_Cmnd'.
51219358
MW
8790 */
8791static int asc_execute_scsi_cmnd(struct scsi_cmnd *scp)
8792{
41d2493d 8793 int ret, err_code;
d2411495 8794 struct asc_board *boardp = shost_priv(scp->device->host);
51219358 8795
b352f923 8796 ASC_DBG(1, "scp 0x%p\n", scp);
27c868c2 8797
51219358 8798 if (ASC_NARROW_BOARD(boardp)) {
41d2493d 8799 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
05848b6e 8800 struct asc_scsi_q asc_scsi_q;
27c868c2 8801
41d2493d 8802 /* asc_build_req() can not return ASC_BUSY. */
05848b6e
MW
8803 ret = asc_build_req(boardp, scp, &asc_scsi_q);
8804 if (ret == ASC_ERROR) {
51219358
MW
8805 ASC_STATS(scp->device->host, build_error);
8806 return ASC_ERROR;
8807 }
1da177e4 8808
41d2493d 8809 ret = AscExeScsiQueue(asc_dvc, &asc_scsi_q);
05848b6e 8810 kfree(asc_scsi_q.sg_head);
41d2493d 8811 err_code = asc_dvc->err_code;
51219358 8812 } else {
41d2493d 8813 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
4b47e464 8814 adv_req_t *adv_reqp;
27c868c2 8815
4b47e464 8816 switch (adv_build_req(boardp, scp, &adv_reqp)) {
51219358 8817 case ASC_NOERROR:
b352f923 8818 ASC_DBG(3, "adv_build_req ASC_NOERROR\n");
51219358
MW
8819 break;
8820 case ASC_BUSY:
b352f923 8821 ASC_DBG(1, "adv_build_req ASC_BUSY\n");
51219358
MW
8822 /*
8823 * The asc_stats fields 'adv_build_noreq' and
8824 * 'adv_build_nosg' count wide board busy conditions.
8825 * They are updated in adv_build_req and
8826 * adv_get_sglist, respectively.
8827 */
8828 return ASC_BUSY;
8829 case ASC_ERROR:
8830 default:
b352f923 8831 ASC_DBG(1, "adv_build_req ASC_ERROR\n");
51219358
MW
8832 ASC_STATS(scp->device->host, build_error);
8833 return ASC_ERROR;
8834 }
8835
4b47e464 8836 ret = AdvExeScsiQueue(adv_dvc, adv_reqp);
41d2493d
MW
8837 err_code = adv_dvc->err_code;
8838 }
8839
8840 switch (ret) {
8841 case ASC_NOERROR:
8842 ASC_STATS(scp->device->host, exe_noerror);
8843 /*
8844 * Increment monotonically increasing per device
8845 * successful request counter. Wrapping doesn't matter.
8846 */
8847 boardp->reqcnt[scp->device->id]++;
b352f923 8848 ASC_DBG(1, "ExeScsiQueue() ASC_NOERROR\n");
41d2493d
MW
8849 break;
8850 case ASC_BUSY:
4b47e464 8851 ASC_DBG(1, "ExeScsiQueue() ASC_BUSY\n");
41d2493d
MW
8852 ASC_STATS(scp->device->host, exe_busy);
8853 break;
8854 case ASC_ERROR:
9d0e96eb
MW
8855 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() ASC_ERROR, "
8856 "err_code 0x%x\n", err_code);
41d2493d
MW
8857 ASC_STATS(scp->device->host, exe_error);
8858 scp->result = HOST_BYTE(DID_ERROR);
8859 break;
8860 default:
9d0e96eb
MW
8861 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() unknown, "
8862 "err_code 0x%x\n", err_code);
41d2493d
MW
8863 ASC_STATS(scp->device->host, exe_unknown);
8864 scp->result = HOST_BYTE(DID_ERROR);
8865 break;
27c868c2 8866 }
1da177e4 8867
b352f923 8868 ASC_DBG(1, "end\n");
51219358
MW
8869 return ret;
8870}
27c868c2 8871
51219358
MW
8872/*
8873 * advansys_queuecommand() - interrupt-driven I/O entrypoint.
8874 *
8875 * This function always returns 0. Command return status is saved
8876 * in the 'scp' result field.
8877 */
8878static int
f281233d 8879advansys_queuecommand_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
51219358
MW
8880{
8881 struct Scsi_Host *shost = scp->device->host;
51219358 8882 int asc_res, result = 0;
27c868c2 8883
51219358
MW
8884 ASC_STATS(shost, queuecommand);
8885 scp->scsi_done = done;
27c868c2 8886
51219358 8887 asc_res = asc_execute_scsi_cmnd(scp);
51219358
MW
8888
8889 switch (asc_res) {
8890 case ASC_NOERROR:
8891 break;
8892 case ASC_BUSY:
8893 result = SCSI_MLQUEUE_HOST_BUSY;
8894 break;
8895 case ASC_ERROR:
8896 default:
8897 asc_scsi_done(scp);
8898 break;
8899 }
8900
8901 return result;
8902}
8903
f281233d
JG
8904static DEF_SCSI_QCMD(advansys_queuecommand)
8905
6f039790 8906static ushort AscGetEisaChipCfg(PortAddr iop_base)
51219358
MW
8907{
8908 PortAddr eisa_cfg_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8909 (PortAddr) (ASC_EISA_CFG_IOP_MASK);
8910 return inpw(eisa_cfg_iop);
8911}
8912
8913/*
8914 * Return the BIOS address of the adapter at the specified
8915 * I/O port and with the specified bus type.
8916 */
6f039790
GKH
8917static unsigned short AscGetChipBiosAddress(PortAddr iop_base,
8918 unsigned short bus_type)
51219358
MW
8919{
8920 unsigned short cfg_lsw;
8921 unsigned short bios_addr;
27c868c2
MW
8922
8923 /*
51219358
MW
8924 * The PCI BIOS is re-located by the motherboard BIOS. Because
8925 * of this the driver can not determine where a PCI BIOS is
8926 * loaded and executes.
27c868c2 8927 */
51219358
MW
8928 if (bus_type & ASC_IS_PCI)
8929 return 0;
27c868c2 8930
51219358
MW
8931 if ((bus_type & ASC_IS_EISA) != 0) {
8932 cfg_lsw = AscGetEisaChipCfg(iop_base);
8933 cfg_lsw &= 0x000F;
8934 bios_addr = ASC_BIOS_MIN_ADDR + cfg_lsw * ASC_BIOS_BANK_SIZE;
8935 return bios_addr;
8936 }
27c868c2 8937
51219358 8938 cfg_lsw = AscGetChipCfgLsw(iop_base);
27c868c2
MW
8939
8940 /*
51219358 8941 * ISA PnP uses the top bit as the 32K BIOS flag
27c868c2 8942 */
51219358
MW
8943 if (bus_type == ASC_IS_ISAPNP)
8944 cfg_lsw &= 0x7FFF;
8945 bios_addr = ASC_BIOS_MIN_ADDR + (cfg_lsw >> 12) * ASC_BIOS_BANK_SIZE;
8946 return bios_addr;
8947}
8948
6f039790 8949static uchar AscSetChipScsiID(PortAddr iop_base, uchar new_host_id)
51219358
MW
8950{
8951 ushort cfg_lsw;
8952
8953 if (AscGetChipScsiID(iop_base) == new_host_id) {
8954 return (new_host_id);
27c868c2 8955 }
51219358
MW
8956 cfg_lsw = AscGetChipCfgLsw(iop_base);
8957 cfg_lsw &= 0xF8FF;
8958 cfg_lsw |= (ushort)((new_host_id & ASC_MAX_TID) << 8);
8959 AscSetChipCfgLsw(iop_base, cfg_lsw);
8960 return (AscGetChipScsiID(iop_base));
8961}
27c868c2 8962
6f039790 8963static unsigned char AscGetChipScsiCtrl(PortAddr iop_base)
51219358
MW
8964{
8965 unsigned char sc;
27c868c2 8966
51219358
MW
8967 AscSetBank(iop_base, 1);
8968 sc = inp(iop_base + IOP_REG_SC);
8969 AscSetBank(iop_base, 0);
8970 return sc;
8971}
27c868c2 8972
6f039790
GKH
8973static unsigned char AscGetChipVersion(PortAddr iop_base,
8974 unsigned short bus_type)
51219358
MW
8975{
8976 if (bus_type & ASC_IS_EISA) {
8977 PortAddr eisa_iop;
8978 unsigned char revision;
8979 eisa_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8980 (PortAddr) ASC_EISA_REV_IOP_MASK;
8981 revision = inp(eisa_iop);
8982 return ASC_CHIP_MIN_VER_EISA - 1 + revision;
27c868c2 8983 }
51219358
MW
8984 return AscGetChipVerNo(iop_base);
8985}
27c868c2 8986
51219358 8987#ifdef CONFIG_ISA
6f039790 8988static void AscEnableIsaDma(uchar dma_channel)
51219358
MW
8989{
8990 if (dma_channel < 4) {
8991 outp(0x000B, (ushort)(0xC0 | dma_channel));
8992 outp(0x000A, dma_channel);
8993 } else if (dma_channel < 8) {
8994 outp(0x00D6, (ushort)(0xC0 | (dma_channel - 4)));
8995 outp(0x00D4, (ushort)(dma_channel - 4));
8996 }
51219358
MW
8997}
8998#endif /* CONFIG_ISA */
8999
9000static int AscStopQueueExe(PortAddr iop_base)
9001{
9002 int count = 0;
9003
9004 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) == 0) {
9005 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
9006 ASC_STOP_REQ_RISC_STOP);
9007 do {
9008 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) &
9009 ASC_STOP_ACK_RISC_STOP) {
9010 return (1);
9011 }
9012 mdelay(100);
9013 } while (count++ < 20);
9014 }
9015 return (0);
9016}
9017
6f039790 9018static ASC_DCNT AscGetMaxDmaCount(ushort bus_type)
51219358
MW
9019{
9020 if (bus_type & ASC_IS_ISA)
9021 return ASC_MAX_ISA_DMA_COUNT;
9022 else if (bus_type & (ASC_IS_EISA | ASC_IS_VL))
9023 return ASC_MAX_VL_DMA_COUNT;
9024 return ASC_MAX_PCI_DMA_COUNT;
27c868c2 9025}
1da177e4 9026
51219358 9027#ifdef CONFIG_ISA
6f039790 9028static ushort AscGetIsaDmaChannel(PortAddr iop_base)
27c868c2 9029{
51219358 9030 ushort channel;
1da177e4 9031
51219358
MW
9032 channel = AscGetChipCfgLsw(iop_base) & 0x0003;
9033 if (channel == 0x03)
9034 return (0);
9035 else if (channel == 0x00)
9036 return (7);
9037 return (channel + 4);
9038}
1da177e4 9039
6f039790 9040static ushort AscSetIsaDmaChannel(PortAddr iop_base, ushort dma_channel)
51219358
MW
9041{
9042 ushort cfg_lsw;
9043 uchar value;
1da177e4 9044
51219358
MW
9045 if ((dma_channel >= 5) && (dma_channel <= 7)) {
9046 if (dma_channel == 7)
9047 value = 0x00;
9048 else
9049 value = dma_channel - 4;
9050 cfg_lsw = AscGetChipCfgLsw(iop_base) & 0xFFFC;
9051 cfg_lsw |= value;
9052 AscSetChipCfgLsw(iop_base, cfg_lsw);
9053 return (AscGetIsaDmaChannel(iop_base));
9054 }
9055 return 0;
9056}
1da177e4 9057
6f039790 9058static uchar AscGetIsaDmaSpeed(PortAddr iop_base)
51219358
MW
9059{
9060 uchar speed_value;
1da177e4 9061
51219358
MW
9062 AscSetBank(iop_base, 1);
9063 speed_value = AscReadChipDmaSpeed(iop_base);
9064 speed_value &= 0x07;
9065 AscSetBank(iop_base, 0);
9066 return speed_value;
9067}
1da177e4 9068
6f039790 9069static uchar AscSetIsaDmaSpeed(PortAddr iop_base, uchar speed_value)
51219358
MW
9070{
9071 speed_value &= 0x07;
9072 AscSetBank(iop_base, 1);
9073 AscWriteChipDmaSpeed(iop_base, speed_value);
9074 AscSetBank(iop_base, 0);
9075 return AscGetIsaDmaSpeed(iop_base);
9076}
9077#endif /* CONFIG_ISA */
1da177e4 9078
6f039790 9079static ushort AscInitAscDvcVar(ASC_DVC_VAR *asc_dvc)
51219358
MW
9080{
9081 int i;
9082 PortAddr iop_base;
9083 ushort warn_code;
9084 uchar chip_version;
1da177e4 9085
51219358
MW
9086 iop_base = asc_dvc->iop_base;
9087 warn_code = 0;
9088 asc_dvc->err_code = 0;
9089 if ((asc_dvc->bus_type &
9090 (ASC_IS_ISA | ASC_IS_PCI | ASC_IS_EISA | ASC_IS_VL)) == 0) {
9091 asc_dvc->err_code |= ASC_IERR_NO_BUS_TYPE;
27c868c2 9092 }
51219358
MW
9093 AscSetChipControl(iop_base, CC_HALT);
9094 AscSetChipStatus(iop_base, 0);
9095 asc_dvc->bug_fix_cntl = 0;
9096 asc_dvc->pci_fix_asyn_xfer = 0;
9097 asc_dvc->pci_fix_asyn_xfer_always = 0;
421f91d2 9098 /* asc_dvc->init_state initialized in AscInitGetConfig(). */
51219358
MW
9099 asc_dvc->sdtr_done = 0;
9100 asc_dvc->cur_total_qng = 0;
9101 asc_dvc->is_in_int = 0;
9102 asc_dvc->in_critical_cnt = 0;
9103 asc_dvc->last_q_shortage = 0;
9104 asc_dvc->use_tagged_qng = 0;
9105 asc_dvc->no_scam = 0;
9106 asc_dvc->unit_not_ready = 0;
9107 asc_dvc->queue_full_or_busy = 0;
9108 asc_dvc->redo_scam = 0;
9109 asc_dvc->res2 = 0;
afbb68c3 9110 asc_dvc->min_sdtr_index = 0;
51219358
MW
9111 asc_dvc->cfg->can_tagged_qng = 0;
9112 asc_dvc->cfg->cmd_qng_enabled = 0;
9113 asc_dvc->dvc_cntl = ASC_DEF_DVC_CNTL;
9114 asc_dvc->init_sdtr = 0;
9115 asc_dvc->max_total_qng = ASC_DEF_MAX_TOTAL_QNG;
9116 asc_dvc->scsi_reset_wait = 3;
9117 asc_dvc->start_motor = ASC_SCSI_WIDTH_BIT_SET;
9118 asc_dvc->max_dma_count = AscGetMaxDmaCount(asc_dvc->bus_type);
9119 asc_dvc->cfg->sdtr_enable = ASC_SCSI_WIDTH_BIT_SET;
9120 asc_dvc->cfg->disc_enable = ASC_SCSI_WIDTH_BIT_SET;
9121 asc_dvc->cfg->chip_scsi_id = ASC_DEF_CHIP_SCSI_ID;
51219358
MW
9122 chip_version = AscGetChipVersion(iop_base, asc_dvc->bus_type);
9123 asc_dvc->cfg->chip_version = chip_version;
afbb68c3 9124 asc_dvc->sdtr_period_tbl = asc_syn_xfer_period;
51219358
MW
9125 asc_dvc->max_sdtr_index = 7;
9126 if ((asc_dvc->bus_type & ASC_IS_PCI) &&
9127 (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3150)) {
9128 asc_dvc->bus_type = ASC_IS_PCI_ULTRA;
afbb68c3 9129 asc_dvc->sdtr_period_tbl = asc_syn_ultra_xfer_period;
51219358
MW
9130 asc_dvc->max_sdtr_index = 15;
9131 if (chip_version == ASC_CHIP_VER_PCI_ULTRA_3150) {
9132 AscSetExtraControl(iop_base,
9133 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
9134 } else if (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3050) {
9135 AscSetExtraControl(iop_base,
9136 (SEC_ACTIVE_NEGATE |
9137 SEC_ENABLE_FILTER));
27c868c2
MW
9138 }
9139 }
51219358
MW
9140 if (asc_dvc->bus_type == ASC_IS_PCI) {
9141 AscSetExtraControl(iop_base,
9142 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
9143 }
1da177e4 9144
51219358
MW
9145 asc_dvc->cfg->isa_dma_speed = ASC_DEF_ISA_DMA_SPEED;
9146#ifdef CONFIG_ISA
9147 if ((asc_dvc->bus_type & ASC_IS_ISA) != 0) {
9148 if (chip_version >= ASC_CHIP_MIN_VER_ISA_PNP) {
9149 AscSetChipIFC(iop_base, IFC_INIT_DEFAULT);
9150 asc_dvc->bus_type = ASC_IS_ISAPNP;
27c868c2 9151 }
51219358
MW
9152 asc_dvc->cfg->isa_dma_channel =
9153 (uchar)AscGetIsaDmaChannel(iop_base);
27c868c2 9154 }
51219358
MW
9155#endif /* CONFIG_ISA */
9156 for (i = 0; i <= ASC_MAX_TID; i++) {
9157 asc_dvc->cur_dvc_qng[i] = 0;
9158 asc_dvc->max_dvc_qng[i] = ASC_MAX_SCSI1_QNG;
9159 asc_dvc->scsiq_busy_head[i] = (ASC_SCSI_Q *)0L;
9160 asc_dvc->scsiq_busy_tail[i] = (ASC_SCSI_Q *)0L;
9161 asc_dvc->cfg->max_tag_qng[i] = ASC_MAX_INRAM_TAG_QNG;
27c868c2 9162 }
51219358
MW
9163 return warn_code;
9164}
1da177e4 9165
6f039790 9166static int AscWriteEEPCmdReg(PortAddr iop_base, uchar cmd_reg)
51219358
MW
9167{
9168 int retry;
27c868c2 9169
51219358
MW
9170 for (retry = 0; retry < ASC_EEP_MAX_RETRY; retry++) {
9171 unsigned char read_back;
9172 AscSetChipEEPCmd(iop_base, cmd_reg);
9173 mdelay(1);
9174 read_back = AscGetChipEEPCmd(iop_base);
9175 if (read_back == cmd_reg)
9176 return 1;
27c868c2 9177 }
51219358
MW
9178 return 0;
9179}
1da177e4 9180
6f039790 9181static void AscWaitEEPRead(void)
51219358
MW
9182{
9183 mdelay(1);
27c868c2 9184}
1da177e4 9185
6f039790 9186static ushort AscReadEEPWord(PortAddr iop_base, uchar addr)
27c868c2 9187{
51219358
MW
9188 ushort read_wval;
9189 uchar cmd_reg;
27c868c2 9190
51219358
MW
9191 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
9192 AscWaitEEPRead();
9193 cmd_reg = addr | ASC_EEP_CMD_READ;
9194 AscWriteEEPCmdReg(iop_base, cmd_reg);
9195 AscWaitEEPRead();
9196 read_wval = AscGetChipEEPData(iop_base);
9197 AscWaitEEPRead();
9198 return read_wval;
9199}
27c868c2 9200
6f039790
GKH
9201static ushort AscGetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9202 ushort bus_type)
51219358
MW
9203{
9204 ushort wval;
9205 ushort sum;
9206 ushort *wbuf;
9207 int cfg_beg;
9208 int cfg_end;
9209 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9210 int s_addr;
27c868c2 9211
51219358
MW
9212 wbuf = (ushort *)cfg_buf;
9213 sum = 0;
9214 /* Read two config words; Byte-swapping done by AscReadEEPWord(). */
9215 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9216 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
9217 sum += *wbuf;
9218 }
9219 if (bus_type & ASC_IS_VL) {
9220 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9221 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9222 } else {
9223 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9224 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9225 }
9226 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9227 wval = AscReadEEPWord(iop_base, (uchar)s_addr);
9228 if (s_addr <= uchar_end_in_config) {
9229 /*
9230 * Swap all char fields - must unswap bytes already swapped
9231 * by AscReadEEPWord().
9232 */
9233 *wbuf = le16_to_cpu(wval);
9234 } else {
9235 /* Don't swap word field at the end - cntl field. */
9236 *wbuf = wval;
9237 }
9238 sum += wval; /* Checksum treats all EEPROM data as words. */
9239 }
27c868c2 9240 /*
51219358
MW
9241 * Read the checksum word which will be compared against 'sum'
9242 * by the caller. Word field already swapped.
27c868c2 9243 */
51219358
MW
9244 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
9245 return sum;
9246}
1da177e4 9247
6f039790 9248static int AscTestExternalLram(ASC_DVC_VAR *asc_dvc)
51219358
MW
9249{
9250 PortAddr iop_base;
9251 ushort q_addr;
9252 ushort saved_word;
9253 int sta;
1da177e4 9254
51219358
MW
9255 iop_base = asc_dvc->iop_base;
9256 sta = 0;
9257 q_addr = ASC_QNO_TO_QADDR(241);
9258 saved_word = AscReadLramWord(iop_base, q_addr);
9259 AscSetChipLramAddr(iop_base, q_addr);
9260 AscSetChipLramData(iop_base, 0x55AA);
9261 mdelay(10);
9262 AscSetChipLramAddr(iop_base, q_addr);
9263 if (AscGetChipLramData(iop_base) == 0x55AA) {
9264 sta = 1;
9265 AscWriteLramWord(iop_base, q_addr, saved_word);
9266 }
9267 return (sta);
9268}
1da177e4 9269
6f039790 9270static void AscWaitEEPWrite(void)
51219358
MW
9271{
9272 mdelay(20);
51219358 9273}
1da177e4 9274
6f039790 9275static int AscWriteEEPDataReg(PortAddr iop_base, ushort data_reg)
51219358
MW
9276{
9277 ushort read_back;
9278 int retry;
1da177e4 9279
51219358
MW
9280 retry = 0;
9281 while (TRUE) {
9282 AscSetChipEEPData(iop_base, data_reg);
9283 mdelay(1);
9284 read_back = AscGetChipEEPData(iop_base);
9285 if (read_back == data_reg) {
9286 return (1);
9287 }
9288 if (retry++ > ASC_EEP_MAX_RETRY) {
9289 return (0);
9290 }
27c868c2 9291 }
51219358 9292}
27c868c2 9293
6f039790 9294static ushort AscWriteEEPWord(PortAddr iop_base, uchar addr, ushort word_val)
51219358
MW
9295{
9296 ushort read_wval;
9297
9298 read_wval = AscReadEEPWord(iop_base, addr);
9299 if (read_wval != word_val) {
9300 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_ABLE);
9301 AscWaitEEPRead();
9302 AscWriteEEPDataReg(iop_base, word_val);
9303 AscWaitEEPRead();
9304 AscWriteEEPCmdReg(iop_base,
9305 (uchar)((uchar)ASC_EEP_CMD_WRITE | addr));
9306 AscWaitEEPWrite();
9307 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
9308 AscWaitEEPRead();
9309 return (AscReadEEPWord(iop_base, addr));
9310 }
9311 return (read_wval);
9312}
9313
6f039790
GKH
9314static int AscSetEEPConfigOnce(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9315 ushort bus_type)
51219358
MW
9316{
9317 int n_error;
9318 ushort *wbuf;
9319 ushort word;
9320 ushort sum;
9321 int s_addr;
9322 int cfg_beg;
9323 int cfg_end;
9324 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9325
9326 wbuf = (ushort *)cfg_buf;
9327 n_error = 0;
9328 sum = 0;
9329 /* Write two config words; AscWriteEEPWord() will swap bytes. */
9330 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9331 sum += *wbuf;
9332 if (*wbuf != AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9333 n_error++;
27c868c2 9334 }
51219358
MW
9335 }
9336 if (bus_type & ASC_IS_VL) {
9337 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9338 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9339 } else {
9340 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9341 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9342 }
9343 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9344 if (s_addr <= uchar_end_in_config) {
9345 /*
9346 * This is a char field. Swap char fields before they are
9347 * swapped again by AscWriteEEPWord().
9348 */
9349 word = cpu_to_le16(*wbuf);
9350 if (word !=
9351 AscWriteEEPWord(iop_base, (uchar)s_addr, word)) {
9352 n_error++;
9353 }
9354 } else {
9355 /* Don't swap word field at the end - cntl field. */
9356 if (*wbuf !=
9357 AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9358 n_error++;
9359 }
27c868c2 9360 }
51219358
MW
9361 sum += *wbuf; /* Checksum calculated from word values. */
9362 }
9363 /* Write checksum word. It will be swapped by AscWriteEEPWord(). */
9364 *wbuf = sum;
9365 if (sum != AscWriteEEPWord(iop_base, (uchar)s_addr, sum)) {
9366 n_error++;
27c868c2 9367 }
1da177e4 9368
51219358
MW
9369 /* Read EEPROM back again. */
9370 wbuf = (ushort *)cfg_buf;
27c868c2 9371 /*
51219358 9372 * Read two config words; Byte-swapping done by AscReadEEPWord().
27c868c2 9373 */
51219358
MW
9374 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9375 if (*wbuf != AscReadEEPWord(iop_base, (uchar)s_addr)) {
9376 n_error++;
27c868c2
MW
9377 }
9378 }
51219358
MW
9379 if (bus_type & ASC_IS_VL) {
9380 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9381 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9382 } else {
9383 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9384 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9385 }
9386 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9387 if (s_addr <= uchar_end_in_config) {
9388 /*
9389 * Swap all char fields. Must unswap bytes already swapped
9390 * by AscReadEEPWord().
9391 */
9392 word =
9393 le16_to_cpu(AscReadEEPWord
9394 (iop_base, (uchar)s_addr));
27c868c2 9395 } else {
51219358
MW
9396 /* Don't swap word field at the end - cntl field. */
9397 word = AscReadEEPWord(iop_base, (uchar)s_addr);
9398 }
9399 if (*wbuf != word) {
9400 n_error++;
27c868c2
MW
9401 }
9402 }
51219358
MW
9403 /* Read checksum; Byte swapping not needed. */
9404 if (AscReadEEPWord(iop_base, (uchar)s_addr) != sum) {
9405 n_error++;
27c868c2 9406 }
51219358
MW
9407 return n_error;
9408}
1da177e4 9409
6f039790
GKH
9410static int AscSetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9411 ushort bus_type)
51219358
MW
9412{
9413 int retry;
9414 int n_error;
27c868c2 9415
51219358
MW
9416 retry = 0;
9417 while (TRUE) {
9418 if ((n_error = AscSetEEPConfigOnce(iop_base, cfg_buf,
9419 bus_type)) == 0) {
9420 break;
9421 }
9422 if (++retry > ASC_EEP_MAX_RETRY) {
9423 break;
9424 }
9425 }
9426 return n_error;
9427}
27c868c2 9428
6f039790 9429static ushort AscInitFromEEP(ASC_DVC_VAR *asc_dvc)
51219358
MW
9430{
9431 ASCEEP_CONFIG eep_config_buf;
9432 ASCEEP_CONFIG *eep_config;
9433 PortAddr iop_base;
9434 ushort chksum;
9435 ushort warn_code;
9436 ushort cfg_msw, cfg_lsw;
9437 int i;
9438 int write_eep = 0;
27c868c2 9439
51219358
MW
9440 iop_base = asc_dvc->iop_base;
9441 warn_code = 0;
9442 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0x00FE);
9443 AscStopQueueExe(iop_base);
9444 if ((AscStopChip(iop_base) == FALSE) ||
9445 (AscGetChipScsiCtrl(iop_base) != 0)) {
9446 asc_dvc->init_state |= ASC_INIT_RESET_SCSI_DONE;
9447 AscResetChipAndScsiBus(asc_dvc);
9448 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
9449 }
9450 if (AscIsChipHalted(iop_base) == FALSE) {
9451 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
9452 return (warn_code);
9453 }
9454 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
9455 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
9456 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
9457 return (warn_code);
9458 }
9459 eep_config = (ASCEEP_CONFIG *)&eep_config_buf;
9460 cfg_msw = AscGetChipCfgMsw(iop_base);
9461 cfg_lsw = AscGetChipCfgLsw(iop_base);
9462 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9463 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9464 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9465 AscSetChipCfgMsw(iop_base, cfg_msw);
9466 }
9467 chksum = AscGetEEPConfig(iop_base, eep_config, asc_dvc->bus_type);
b352f923 9468 ASC_DBG(1, "chksum 0x%x\n", chksum);
51219358
MW
9469 if (chksum == 0) {
9470 chksum = 0xaa55;
9471 }
9472 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9473 warn_code |= ASC_WARN_AUTO_CONFIG;
9474 if (asc_dvc->cfg->chip_version == 3) {
9475 if (eep_config->cfg_lsw != cfg_lsw) {
9476 warn_code |= ASC_WARN_EEPROM_RECOVER;
9477 eep_config->cfg_lsw =
9478 AscGetChipCfgLsw(iop_base);
9479 }
9480 if (eep_config->cfg_msw != cfg_msw) {
9481 warn_code |= ASC_WARN_EEPROM_RECOVER;
9482 eep_config->cfg_msw =
9483 AscGetChipCfgMsw(iop_base);
9484 }
9485 }
9486 }
9487 eep_config->cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9488 eep_config->cfg_lsw |= ASC_CFG0_HOST_INT_ON;
b352f923 9489 ASC_DBG(1, "eep_config->chksum 0x%x\n", eep_config->chksum);
51219358
MW
9490 if (chksum != eep_config->chksum) {
9491 if (AscGetChipVersion(iop_base, asc_dvc->bus_type) ==
9492 ASC_CHIP_VER_PCI_ULTRA_3050) {
b352f923 9493 ASC_DBG(1, "chksum error ignored; EEPROM-less board\n");
51219358
MW
9494 eep_config->init_sdtr = 0xFF;
9495 eep_config->disc_enable = 0xFF;
9496 eep_config->start_motor = 0xFF;
9497 eep_config->use_cmd_qng = 0;
9498 eep_config->max_total_qng = 0xF0;
9499 eep_config->max_tag_qng = 0x20;
9500 eep_config->cntl = 0xBFFF;
9501 ASC_EEP_SET_CHIP_ID(eep_config, 7);
9502 eep_config->no_scam = 0;
9503 eep_config->adapter_info[0] = 0;
9504 eep_config->adapter_info[1] = 0;
9505 eep_config->adapter_info[2] = 0;
9506 eep_config->adapter_info[3] = 0;
9507 eep_config->adapter_info[4] = 0;
9508 /* Indicate EEPROM-less board. */
9509 eep_config->adapter_info[5] = 0xBB;
27c868c2 9510 } else {
51219358
MW
9511 ASC_PRINT
9512 ("AscInitFromEEP: EEPROM checksum error; Will try to re-write EEPROM.\n");
9513 write_eep = 1;
9514 warn_code |= ASC_WARN_EEPROM_CHKSUM;
9515 }
9516 }
9517 asc_dvc->cfg->sdtr_enable = eep_config->init_sdtr;
9518 asc_dvc->cfg->disc_enable = eep_config->disc_enable;
9519 asc_dvc->cfg->cmd_qng_enabled = eep_config->use_cmd_qng;
9520 asc_dvc->cfg->isa_dma_speed = ASC_EEP_GET_DMA_SPD(eep_config);
9521 asc_dvc->start_motor = eep_config->start_motor;
9522 asc_dvc->dvc_cntl = eep_config->cntl;
9523 asc_dvc->no_scam = eep_config->no_scam;
9524 asc_dvc->cfg->adapter_info[0] = eep_config->adapter_info[0];
9525 asc_dvc->cfg->adapter_info[1] = eep_config->adapter_info[1];
9526 asc_dvc->cfg->adapter_info[2] = eep_config->adapter_info[2];
9527 asc_dvc->cfg->adapter_info[3] = eep_config->adapter_info[3];
9528 asc_dvc->cfg->adapter_info[4] = eep_config->adapter_info[4];
9529 asc_dvc->cfg->adapter_info[5] = eep_config->adapter_info[5];
9530 if (!AscTestExternalLram(asc_dvc)) {
9531 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) ==
9532 ASC_IS_PCI_ULTRA)) {
9533 eep_config->max_total_qng =
9534 ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
9535 eep_config->max_tag_qng =
9536 ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG;
9537 } else {
9538 eep_config->cfg_msw |= 0x0800;
9539 cfg_msw |= 0x0800;
9540 AscSetChipCfgMsw(iop_base, cfg_msw);
9541 eep_config->max_total_qng = ASC_MAX_PCI_INRAM_TOTAL_QNG;
9542 eep_config->max_tag_qng = ASC_MAX_INRAM_TAG_QNG;
27c868c2 9543 }
51219358
MW
9544 } else {
9545 }
9546 if (eep_config->max_total_qng < ASC_MIN_TOTAL_QNG) {
9547 eep_config->max_total_qng = ASC_MIN_TOTAL_QNG;
9548 }
9549 if (eep_config->max_total_qng > ASC_MAX_TOTAL_QNG) {
9550 eep_config->max_total_qng = ASC_MAX_TOTAL_QNG;
9551 }
9552 if (eep_config->max_tag_qng > eep_config->max_total_qng) {
9553 eep_config->max_tag_qng = eep_config->max_total_qng;
9554 }
9555 if (eep_config->max_tag_qng < ASC_MIN_TAG_Q_PER_DVC) {
9556 eep_config->max_tag_qng = ASC_MIN_TAG_Q_PER_DVC;
9557 }
9558 asc_dvc->max_total_qng = eep_config->max_total_qng;
9559 if ((eep_config->use_cmd_qng & eep_config->disc_enable) !=
9560 eep_config->use_cmd_qng) {
9561 eep_config->disc_enable = eep_config->use_cmd_qng;
9562 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9563 }
51219358
MW
9564 ASC_EEP_SET_CHIP_ID(eep_config,
9565 ASC_EEP_GET_CHIP_ID(eep_config) & ASC_MAX_TID);
9566 asc_dvc->cfg->chip_scsi_id = ASC_EEP_GET_CHIP_ID(eep_config);
9567 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) &&
9568 !(asc_dvc->dvc_cntl & ASC_CNTL_SDTR_ENABLE_ULTRA)) {
afbb68c3 9569 asc_dvc->min_sdtr_index = ASC_SDTR_ULTRA_PCI_10MB_INDEX;
27c868c2 9570 }
1da177e4 9571
51219358
MW
9572 for (i = 0; i <= ASC_MAX_TID; i++) {
9573 asc_dvc->dos_int13_table[i] = eep_config->dos_int13_table[i];
9574 asc_dvc->cfg->max_tag_qng[i] = eep_config->max_tag_qng;
9575 asc_dvc->cfg->sdtr_period_offset[i] =
9576 (uchar)(ASC_DEF_SDTR_OFFSET |
afbb68c3 9577 (asc_dvc->min_sdtr_index << 4));
51219358
MW
9578 }
9579 eep_config->cfg_msw = AscGetChipCfgMsw(iop_base);
9580 if (write_eep) {
9581 if ((i = AscSetEEPConfig(iop_base, eep_config,
9582 asc_dvc->bus_type)) != 0) {
9583 ASC_PRINT1
9584 ("AscInitFromEEP: Failed to re-write EEPROM with %d errors.\n",
9585 i);
27c868c2 9586 } else {
51219358
MW
9587 ASC_PRINT
9588 ("AscInitFromEEP: Successfully re-wrote EEPROM.\n");
27c868c2
MW
9589 }
9590 }
51219358 9591 return (warn_code);
1da177e4
LT
9592}
9593
6f039790 9594static int AscInitGetConfig(struct Scsi_Host *shost)
1da177e4 9595{
9d0e96eb
MW
9596 struct asc_board *board = shost_priv(shost);
9597 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
51219358 9598 unsigned short warn_code = 0;
27c868c2 9599
51219358
MW
9600 asc_dvc->init_state = ASC_INIT_STATE_BEG_GET_CFG;
9601 if (asc_dvc->err_code != 0)
9602 return asc_dvc->err_code;
27c868c2 9603
51219358
MW
9604 if (AscFindSignature(asc_dvc->iop_base)) {
9605 warn_code |= AscInitAscDvcVar(asc_dvc);
9606 warn_code |= AscInitFromEEP(asc_dvc);
9607 asc_dvc->init_state |= ASC_INIT_STATE_END_GET_CFG;
9608 if (asc_dvc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
9609 asc_dvc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
9610 } else {
9611 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9612 }
27c868c2 9613
51219358
MW
9614 switch (warn_code) {
9615 case 0: /* No error */
9616 break;
9617 case ASC_WARN_IO_PORT_ROTATE:
9d0e96eb
MW
9618 shost_printk(KERN_WARNING, shost, "I/O port address "
9619 "modified\n");
51219358
MW
9620 break;
9621 case ASC_WARN_AUTO_CONFIG:
9d0e96eb
MW
9622 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9623 "enabled\n");
51219358
MW
9624 break;
9625 case ASC_WARN_EEPROM_CHKSUM:
9d0e96eb 9626 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
51219358
MW
9627 break;
9628 case ASC_WARN_IRQ_MODIFIED:
9d0e96eb 9629 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
51219358
MW
9630 break;
9631 case ASC_WARN_CMD_QNG_CONFLICT:
9d0e96eb
MW
9632 shost_printk(KERN_WARNING, shost, "tag queuing enabled w/o "
9633 "disconnects\n");
51219358
MW
9634 break;
9635 default:
9d0e96eb
MW
9636 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9637 warn_code);
51219358
MW
9638 break;
9639 }
1da177e4 9640
9d0e96eb
MW
9641 if (asc_dvc->err_code != 0)
9642 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9643 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
27c868c2 9644
51219358
MW
9645 return asc_dvc->err_code;
9646}
1da177e4 9647
6f039790 9648static int AscInitSetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
51219358 9649{
9d0e96eb
MW
9650 struct asc_board *board = shost_priv(shost);
9651 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
51219358
MW
9652 PortAddr iop_base = asc_dvc->iop_base;
9653 unsigned short cfg_msw;
9654 unsigned short warn_code = 0;
1da177e4 9655
51219358
MW
9656 asc_dvc->init_state |= ASC_INIT_STATE_BEG_SET_CFG;
9657 if (asc_dvc->err_code != 0)
9658 return asc_dvc->err_code;
9659 if (!AscFindSignature(asc_dvc->iop_base)) {
9660 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9661 return asc_dvc->err_code;
27c868c2 9662 }
1da177e4 9663
51219358
MW
9664 cfg_msw = AscGetChipCfgMsw(iop_base);
9665 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9666 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9667 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9668 AscSetChipCfgMsw(iop_base, cfg_msw);
9669 }
9670 if ((asc_dvc->cfg->cmd_qng_enabled & asc_dvc->cfg->disc_enable) !=
9671 asc_dvc->cfg->cmd_qng_enabled) {
9672 asc_dvc->cfg->disc_enable = asc_dvc->cfg->cmd_qng_enabled;
9673 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9674 }
9675 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9676 warn_code |= ASC_WARN_AUTO_CONFIG;
9677 }
51219358
MW
9678#ifdef CONFIG_PCI
9679 if (asc_dvc->bus_type & ASC_IS_PCI) {
9680 cfg_msw &= 0xFFC0;
9681 AscSetChipCfgMsw(iop_base, cfg_msw);
9682 if ((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) {
27c868c2 9683 } else {
51219358
MW
9684 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
9685 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
9686 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_IF_NOT_DWB;
9687 asc_dvc->bug_fix_cntl |=
9688 ASC_BUG_FIX_ASYN_USE_SYN;
9689 }
27c868c2 9690 }
51219358
MW
9691 } else
9692#endif /* CONFIG_PCI */
9693 if (asc_dvc->bus_type == ASC_IS_ISAPNP) {
9694 if (AscGetChipVersion(iop_base, asc_dvc->bus_type)
9695 == ASC_CHIP_VER_ASYN_BUG) {
9696 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
27c868c2
MW
9697 }
9698 }
51219358
MW
9699 if (AscSetChipScsiID(iop_base, asc_dvc->cfg->chip_scsi_id) !=
9700 asc_dvc->cfg->chip_scsi_id) {
9701 asc_dvc->err_code |= ASC_IERR_SET_SCSI_ID;
9702 }
9703#ifdef CONFIG_ISA
9704 if (asc_dvc->bus_type & ASC_IS_ISA) {
9705 AscSetIsaDmaChannel(iop_base, asc_dvc->cfg->isa_dma_channel);
9706 AscSetIsaDmaSpeed(iop_base, asc_dvc->cfg->isa_dma_speed);
9707 }
9708#endif /* CONFIG_ISA */
1da177e4 9709
51219358
MW
9710 asc_dvc->init_state |= ASC_INIT_STATE_END_SET_CFG;
9711
9712 switch (warn_code) {
9713 case 0: /* No error. */
9714 break;
9715 case ASC_WARN_IO_PORT_ROTATE:
9d0e96eb
MW
9716 shost_printk(KERN_WARNING, shost, "I/O port address "
9717 "modified\n");
51219358
MW
9718 break;
9719 case ASC_WARN_AUTO_CONFIG:
9d0e96eb
MW
9720 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9721 "enabled\n");
51219358
MW
9722 break;
9723 case ASC_WARN_EEPROM_CHKSUM:
9d0e96eb 9724 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
51219358
MW
9725 break;
9726 case ASC_WARN_IRQ_MODIFIED:
9d0e96eb 9727 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
51219358
MW
9728 break;
9729 case ASC_WARN_CMD_QNG_CONFLICT:
9d0e96eb
MW
9730 shost_printk(KERN_WARNING, shost, "tag queuing w/o "
9731 "disconnects\n");
51219358
MW
9732 break;
9733 default:
9d0e96eb
MW
9734 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9735 warn_code);
51219358 9736 break;
27c868c2 9737 }
1da177e4 9738
9d0e96eb
MW
9739 if (asc_dvc->err_code != 0)
9740 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9741 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
27c868c2 9742
51219358
MW
9743 return asc_dvc->err_code;
9744}
27c868c2 9745
51219358
MW
9746/*
9747 * EEPROM Configuration.
9748 *
9749 * All drivers should use this structure to set the default EEPROM
9750 * configuration. The BIOS now uses this structure when it is built.
9751 * Additional structure information can be found in a_condor.h where
9752 * the structure is defined.
9753 *
9754 * The *_Field_IsChar structs are needed to correct for endianness.
9755 * These values are read from the board 16 bits at a time directly
9756 * into the structs. Because some fields are char, the values will be
9757 * in the wrong order. The *_Field_IsChar tells when to flip the
9758 * bytes. Data read and written to PCI memory is automatically swapped
9759 * on big-endian platforms so char fields read as words are actually being
9760 * unswapped on big-endian platforms.
9761 */
6f039790 9762static ADVEEP_3550_CONFIG Default_3550_EEPROM_Config = {
51219358
MW
9763 ADV_EEPROM_BIOS_ENABLE, /* cfg_lsw */
9764 0x0000, /* cfg_msw */
9765 0xFFFF, /* disc_enable */
9766 0xFFFF, /* wdtr_able */
9767 0xFFFF, /* sdtr_able */
9768 0xFFFF, /* start_motor */
9769 0xFFFF, /* tagqng_able */
9770 0xFFFF, /* bios_scan */
9771 0, /* scam_tolerant */
9772 7, /* adapter_scsi_id */
9773 0, /* bios_boot_delay */
9774 3, /* scsi_reset_delay */
9775 0, /* bios_id_lun */
9776 0, /* termination */
9777 0, /* reserved1 */
9778 0xFFE7, /* bios_ctrl */
9779 0xFFFF, /* ultra_able */
9780 0, /* reserved2 */
9781 ASC_DEF_MAX_HOST_QNG, /* max_host_qng */
9782 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9783 0, /* dvc_cntl */
9784 0, /* bug_fix */
9785 0, /* serial_number_word1 */
9786 0, /* serial_number_word2 */
9787 0, /* serial_number_word3 */
9788 0, /* check_sum */
9789 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9790 , /* oem_name[16] */
9791 0, /* dvc_err_code */
9792 0, /* adv_err_code */
9793 0, /* adv_err_addr */
9794 0, /* saved_dvc_err_code */
9795 0, /* saved_adv_err_code */
9796 0, /* saved_adv_err_addr */
9797 0 /* num_of_err */
9798};
27c868c2 9799
6f039790 9800static ADVEEP_3550_CONFIG ADVEEP_3550_Config_Field_IsChar = {
51219358
MW
9801 0, /* cfg_lsw */
9802 0, /* cfg_msw */
9803 0, /* -disc_enable */
9804 0, /* wdtr_able */
9805 0, /* sdtr_able */
9806 0, /* start_motor */
9807 0, /* tagqng_able */
9808 0, /* bios_scan */
9809 0, /* scam_tolerant */
9810 1, /* adapter_scsi_id */
9811 1, /* bios_boot_delay */
9812 1, /* scsi_reset_delay */
9813 1, /* bios_id_lun */
9814 1, /* termination */
9815 1, /* reserved1 */
9816 0, /* bios_ctrl */
9817 0, /* ultra_able */
9818 0, /* reserved2 */
9819 1, /* max_host_qng */
9820 1, /* max_dvc_qng */
9821 0, /* dvc_cntl */
9822 0, /* bug_fix */
9823 0, /* serial_number_word1 */
9824 0, /* serial_number_word2 */
9825 0, /* serial_number_word3 */
9826 0, /* check_sum */
9827 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9828 , /* oem_name[16] */
9829 0, /* dvc_err_code */
9830 0, /* adv_err_code */
9831 0, /* adv_err_addr */
9832 0, /* saved_dvc_err_code */
9833 0, /* saved_adv_err_code */
9834 0, /* saved_adv_err_addr */
9835 0 /* num_of_err */
9836};
1da177e4 9837
6f039790 9838static ADVEEP_38C0800_CONFIG Default_38C0800_EEPROM_Config = {
51219358
MW
9839 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9840 0x0000, /* 01 cfg_msw */
9841 0xFFFF, /* 02 disc_enable */
9842 0xFFFF, /* 03 wdtr_able */
9843 0x4444, /* 04 sdtr_speed1 */
9844 0xFFFF, /* 05 start_motor */
9845 0xFFFF, /* 06 tagqng_able */
9846 0xFFFF, /* 07 bios_scan */
9847 0, /* 08 scam_tolerant */
9848 7, /* 09 adapter_scsi_id */
9849 0, /* bios_boot_delay */
9850 3, /* 10 scsi_reset_delay */
9851 0, /* bios_id_lun */
9852 0, /* 11 termination_se */
9853 0, /* termination_lvd */
9854 0xFFE7, /* 12 bios_ctrl */
9855 0x4444, /* 13 sdtr_speed2 */
9856 0x4444, /* 14 sdtr_speed3 */
9857 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9858 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9859 0, /* 16 dvc_cntl */
9860 0x4444, /* 17 sdtr_speed4 */
9861 0, /* 18 serial_number_word1 */
9862 0, /* 19 serial_number_word2 */
9863 0, /* 20 serial_number_word3 */
9864 0, /* 21 check_sum */
9865 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9866 , /* 22-29 oem_name[16] */
9867 0, /* 30 dvc_err_code */
9868 0, /* 31 adv_err_code */
9869 0, /* 32 adv_err_addr */
9870 0, /* 33 saved_dvc_err_code */
9871 0, /* 34 saved_adv_err_code */
9872 0, /* 35 saved_adv_err_addr */
9873 0, /* 36 reserved */
9874 0, /* 37 reserved */
9875 0, /* 38 reserved */
9876 0, /* 39 reserved */
9877 0, /* 40 reserved */
9878 0, /* 41 reserved */
9879 0, /* 42 reserved */
9880 0, /* 43 reserved */
9881 0, /* 44 reserved */
9882 0, /* 45 reserved */
9883 0, /* 46 reserved */
9884 0, /* 47 reserved */
9885 0, /* 48 reserved */
9886 0, /* 49 reserved */
9887 0, /* 50 reserved */
9888 0, /* 51 reserved */
9889 0, /* 52 reserved */
9890 0, /* 53 reserved */
9891 0, /* 54 reserved */
9892 0, /* 55 reserved */
9893 0, /* 56 cisptr_lsw */
9894 0, /* 57 cisprt_msw */
9895 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9896 PCI_DEVICE_ID_38C0800_REV1, /* 59 subsysid */
9897 0, /* 60 reserved */
9898 0, /* 61 reserved */
9899 0, /* 62 reserved */
9900 0 /* 63 reserved */
9901};
27c868c2 9902
6f039790 9903static ADVEEP_38C0800_CONFIG ADVEEP_38C0800_Config_Field_IsChar = {
51219358
MW
9904 0, /* 00 cfg_lsw */
9905 0, /* 01 cfg_msw */
9906 0, /* 02 disc_enable */
9907 0, /* 03 wdtr_able */
9908 0, /* 04 sdtr_speed1 */
9909 0, /* 05 start_motor */
9910 0, /* 06 tagqng_able */
9911 0, /* 07 bios_scan */
9912 0, /* 08 scam_tolerant */
9913 1, /* 09 adapter_scsi_id */
9914 1, /* bios_boot_delay */
9915 1, /* 10 scsi_reset_delay */
9916 1, /* bios_id_lun */
9917 1, /* 11 termination_se */
9918 1, /* termination_lvd */
9919 0, /* 12 bios_ctrl */
9920 0, /* 13 sdtr_speed2 */
9921 0, /* 14 sdtr_speed3 */
9922 1, /* 15 max_host_qng */
9923 1, /* max_dvc_qng */
9924 0, /* 16 dvc_cntl */
9925 0, /* 17 sdtr_speed4 */
9926 0, /* 18 serial_number_word1 */
9927 0, /* 19 serial_number_word2 */
9928 0, /* 20 serial_number_word3 */
9929 0, /* 21 check_sum */
9930 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9931 , /* 22-29 oem_name[16] */
9932 0, /* 30 dvc_err_code */
9933 0, /* 31 adv_err_code */
9934 0, /* 32 adv_err_addr */
9935 0, /* 33 saved_dvc_err_code */
9936 0, /* 34 saved_adv_err_code */
9937 0, /* 35 saved_adv_err_addr */
9938 0, /* 36 reserved */
9939 0, /* 37 reserved */
9940 0, /* 38 reserved */
9941 0, /* 39 reserved */
9942 0, /* 40 reserved */
9943 0, /* 41 reserved */
9944 0, /* 42 reserved */
9945 0, /* 43 reserved */
9946 0, /* 44 reserved */
9947 0, /* 45 reserved */
9948 0, /* 46 reserved */
9949 0, /* 47 reserved */
9950 0, /* 48 reserved */
9951 0, /* 49 reserved */
9952 0, /* 50 reserved */
9953 0, /* 51 reserved */
9954 0, /* 52 reserved */
9955 0, /* 53 reserved */
9956 0, /* 54 reserved */
9957 0, /* 55 reserved */
9958 0, /* 56 cisptr_lsw */
9959 0, /* 57 cisprt_msw */
9960 0, /* 58 subsysvid */
9961 0, /* 59 subsysid */
9962 0, /* 60 reserved */
9963 0, /* 61 reserved */
9964 0, /* 62 reserved */
9965 0 /* 63 reserved */
9966};
27c868c2 9967
6f039790 9968static ADVEEP_38C1600_CONFIG Default_38C1600_EEPROM_Config = {
51219358
MW
9969 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9970 0x0000, /* 01 cfg_msw */
9971 0xFFFF, /* 02 disc_enable */
9972 0xFFFF, /* 03 wdtr_able */
9973 0x5555, /* 04 sdtr_speed1 */
9974 0xFFFF, /* 05 start_motor */
9975 0xFFFF, /* 06 tagqng_able */
9976 0xFFFF, /* 07 bios_scan */
9977 0, /* 08 scam_tolerant */
9978 7, /* 09 adapter_scsi_id */
9979 0, /* bios_boot_delay */
9980 3, /* 10 scsi_reset_delay */
9981 0, /* bios_id_lun */
9982 0, /* 11 termination_se */
9983 0, /* termination_lvd */
9984 0xFFE7, /* 12 bios_ctrl */
9985 0x5555, /* 13 sdtr_speed2 */
9986 0x5555, /* 14 sdtr_speed3 */
9987 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9988 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9989 0, /* 16 dvc_cntl */
9990 0x5555, /* 17 sdtr_speed4 */
9991 0, /* 18 serial_number_word1 */
9992 0, /* 19 serial_number_word2 */
9993 0, /* 20 serial_number_word3 */
9994 0, /* 21 check_sum */
9995 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9996 , /* 22-29 oem_name[16] */
9997 0, /* 30 dvc_err_code */
9998 0, /* 31 adv_err_code */
9999 0, /* 32 adv_err_addr */
10000 0, /* 33 saved_dvc_err_code */
10001 0, /* 34 saved_adv_err_code */
10002 0, /* 35 saved_adv_err_addr */
10003 0, /* 36 reserved */
10004 0, /* 37 reserved */
10005 0, /* 38 reserved */
10006 0, /* 39 reserved */
10007 0, /* 40 reserved */
10008 0, /* 41 reserved */
10009 0, /* 42 reserved */
10010 0, /* 43 reserved */
10011 0, /* 44 reserved */
10012 0, /* 45 reserved */
10013 0, /* 46 reserved */
10014 0, /* 47 reserved */
10015 0, /* 48 reserved */
10016 0, /* 49 reserved */
10017 0, /* 50 reserved */
10018 0, /* 51 reserved */
10019 0, /* 52 reserved */
10020 0, /* 53 reserved */
10021 0, /* 54 reserved */
10022 0, /* 55 reserved */
10023 0, /* 56 cisptr_lsw */
10024 0, /* 57 cisprt_msw */
10025 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
10026 PCI_DEVICE_ID_38C1600_REV1, /* 59 subsysid */
10027 0, /* 60 reserved */
10028 0, /* 61 reserved */
10029 0, /* 62 reserved */
10030 0 /* 63 reserved */
10031};
1da177e4 10032
6f039790 10033static ADVEEP_38C1600_CONFIG ADVEEP_38C1600_Config_Field_IsChar = {
51219358
MW
10034 0, /* 00 cfg_lsw */
10035 0, /* 01 cfg_msw */
10036 0, /* 02 disc_enable */
10037 0, /* 03 wdtr_able */
10038 0, /* 04 sdtr_speed1 */
10039 0, /* 05 start_motor */
10040 0, /* 06 tagqng_able */
10041 0, /* 07 bios_scan */
10042 0, /* 08 scam_tolerant */
10043 1, /* 09 adapter_scsi_id */
10044 1, /* bios_boot_delay */
10045 1, /* 10 scsi_reset_delay */
10046 1, /* bios_id_lun */
10047 1, /* 11 termination_se */
10048 1, /* termination_lvd */
10049 0, /* 12 bios_ctrl */
10050 0, /* 13 sdtr_speed2 */
10051 0, /* 14 sdtr_speed3 */
10052 1, /* 15 max_host_qng */
10053 1, /* max_dvc_qng */
10054 0, /* 16 dvc_cntl */
10055 0, /* 17 sdtr_speed4 */
10056 0, /* 18 serial_number_word1 */
10057 0, /* 19 serial_number_word2 */
10058 0, /* 20 serial_number_word3 */
10059 0, /* 21 check_sum */
10060 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
10061 , /* 22-29 oem_name[16] */
10062 0, /* 30 dvc_err_code */
10063 0, /* 31 adv_err_code */
10064 0, /* 32 adv_err_addr */
10065 0, /* 33 saved_dvc_err_code */
10066 0, /* 34 saved_adv_err_code */
10067 0, /* 35 saved_adv_err_addr */
10068 0, /* 36 reserved */
10069 0, /* 37 reserved */
10070 0, /* 38 reserved */
10071 0, /* 39 reserved */
10072 0, /* 40 reserved */
10073 0, /* 41 reserved */
10074 0, /* 42 reserved */
10075 0, /* 43 reserved */
10076 0, /* 44 reserved */
10077 0, /* 45 reserved */
10078 0, /* 46 reserved */
10079 0, /* 47 reserved */
10080 0, /* 48 reserved */
10081 0, /* 49 reserved */
10082 0, /* 50 reserved */
10083 0, /* 51 reserved */
10084 0, /* 52 reserved */
10085 0, /* 53 reserved */
10086 0, /* 54 reserved */
10087 0, /* 55 reserved */
10088 0, /* 56 cisptr_lsw */
10089 0, /* 57 cisprt_msw */
10090 0, /* 58 subsysvid */
10091 0, /* 59 subsysid */
10092 0, /* 60 reserved */
10093 0, /* 61 reserved */
10094 0, /* 62 reserved */
10095 0 /* 63 reserved */
10096};
1da177e4 10097
51219358 10098#ifdef CONFIG_PCI
1da177e4 10099/*
51219358 10100 * Wait for EEPROM command to complete
1da177e4 10101 */
6f039790 10102static void AdvWaitEEPCmd(AdvPortAddr iop_base)
1da177e4 10103{
51219358 10104 int eep_delay_ms;
27c868c2 10105
51219358
MW
10106 for (eep_delay_ms = 0; eep_delay_ms < ADV_EEP_DELAY_MS; eep_delay_ms++) {
10107 if (AdvReadWordRegister(iop_base, IOPW_EE_CMD) &
10108 ASC_EEP_CMD_DONE) {
10109 break;
27c868c2 10110 }
51219358 10111 mdelay(1);
27c868c2 10112 }
51219358
MW
10113 if ((AdvReadWordRegister(iop_base, IOPW_EE_CMD) & ASC_EEP_CMD_DONE) ==
10114 0)
10115 BUG();
1da177e4
LT
10116}
10117
10118/*
51219358 10119 * Read the EEPROM from specified location
1da177e4 10120 */
6f039790 10121static ushort AdvReadEEPWord(AdvPortAddr iop_base, int eep_word_addr)
51219358
MW
10122{
10123 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10124 ASC_EEP_CMD_READ | eep_word_addr);
10125 AdvWaitEEPCmd(iop_base);
10126 return AdvReadWordRegister(iop_base, IOPW_EE_DATA);
10127}
10128
10129/*
10130 * Write the EEPROM from 'cfg_buf'.
10131 */
6f039790
GKH
10132static void AdvSet3550EEPConfig(AdvPortAddr iop_base,
10133 ADVEEP_3550_CONFIG *cfg_buf)
1da177e4 10134{
27c868c2 10135 ushort *wbuf;
51219358 10136 ushort addr, chksum;
27c868c2
MW
10137 ushort *charfields;
10138
27c868c2 10139 wbuf = (ushort *)cfg_buf;
51219358 10140 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
27c868c2
MW
10141 chksum = 0;
10142
51219358
MW
10143 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10144 AdvWaitEEPCmd(iop_base);
10145
10146 /*
10147 * Write EEPROM from word 0 to word 20.
10148 */
10149 for (addr = ADV_EEP_DVC_CFG_BEGIN;
10150 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10151 ushort word;
10152
27c868c2 10153 if (*charfields++) {
51219358 10154 word = cpu_to_le16(*wbuf);
27c868c2 10155 } else {
51219358 10156 word = *wbuf;
27c868c2 10157 }
51219358
MW
10158 chksum += *wbuf; /* Checksum is calculated from word values. */
10159 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10160 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10161 ASC_EEP_CMD_WRITE | addr);
10162 AdvWaitEEPCmd(iop_base);
10163 mdelay(ADV_EEP_DELAY_MS);
27c868c2 10164 }
51219358
MW
10165
10166 /*
10167 * Write EEPROM checksum at word 21.
10168 */
10169 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10170 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10171 AdvWaitEEPCmd(iop_base);
27c868c2
MW
10172 wbuf++;
10173 charfields++;
10174
51219358
MW
10175 /*
10176 * Write EEPROM OEM name at words 22 to 29.
10177 */
10178 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10179 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10180 ushort word;
10181
27c868c2 10182 if (*charfields++) {
51219358
MW
10183 word = cpu_to_le16(*wbuf);
10184 } else {
10185 word = *wbuf;
27c868c2 10186 }
51219358
MW
10187 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10188 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10189 ASC_EEP_CMD_WRITE | addr);
10190 AdvWaitEEPCmd(iop_base);
27c868c2 10191 }
51219358
MW
10192 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10193 AdvWaitEEPCmd(iop_base);
1da177e4
LT
10194}
10195
10196/*
51219358 10197 * Write the EEPROM from 'cfg_buf'.
1da177e4 10198 */
6f039790
GKH
10199static void AdvSet38C0800EEPConfig(AdvPortAddr iop_base,
10200 ADVEEP_38C0800_CONFIG *cfg_buf)
1da177e4 10201{
27c868c2 10202 ushort *wbuf;
27c868c2 10203 ushort *charfields;
51219358 10204 ushort addr, chksum;
27c868c2 10205
27c868c2 10206 wbuf = (ushort *)cfg_buf;
51219358 10207 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
27c868c2
MW
10208 chksum = 0;
10209
51219358
MW
10210 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10211 AdvWaitEEPCmd(iop_base);
10212
10213 /*
10214 * Write EEPROM from word 0 to word 20.
10215 */
10216 for (addr = ADV_EEP_DVC_CFG_BEGIN;
10217 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10218 ushort word;
10219
27c868c2 10220 if (*charfields++) {
51219358 10221 word = cpu_to_le16(*wbuf);
27c868c2 10222 } else {
51219358 10223 word = *wbuf;
27c868c2 10224 }
51219358
MW
10225 chksum += *wbuf; /* Checksum is calculated from word values. */
10226 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10227 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10228 ASC_EEP_CMD_WRITE | addr);
10229 AdvWaitEEPCmd(iop_base);
10230 mdelay(ADV_EEP_DELAY_MS);
27c868c2 10231 }
51219358
MW
10232
10233 /*
10234 * Write EEPROM checksum at word 21.
10235 */
10236 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10237 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10238 AdvWaitEEPCmd(iop_base);
27c868c2
MW
10239 wbuf++;
10240 charfields++;
10241
51219358
MW
10242 /*
10243 * Write EEPROM OEM name at words 22 to 29.
10244 */
10245 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10246 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10247 ushort word;
10248
27c868c2 10249 if (*charfields++) {
51219358
MW
10250 word = cpu_to_le16(*wbuf);
10251 } else {
10252 word = *wbuf;
27c868c2 10253 }
51219358
MW
10254 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10255 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10256 ASC_EEP_CMD_WRITE | addr);
10257 AdvWaitEEPCmd(iop_base);
27c868c2 10258 }
51219358 10259 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
27c868c2 10260 AdvWaitEEPCmd(iop_base);
1da177e4
LT
10261}
10262
10263/*
10264 * Write the EEPROM from 'cfg_buf'.
10265 */
6f039790
GKH
10266static void AdvSet38C1600EEPConfig(AdvPortAddr iop_base,
10267 ADVEEP_38C1600_CONFIG *cfg_buf)
1da177e4 10268{
27c868c2 10269 ushort *wbuf;
27c868c2 10270 ushort *charfields;
51219358 10271 ushort addr, chksum;
27c868c2
MW
10272
10273 wbuf = (ushort *)cfg_buf;
51219358 10274 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
27c868c2
MW
10275 chksum = 0;
10276
10277 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10278 AdvWaitEEPCmd(iop_base);
10279
10280 /*
10281 * Write EEPROM from word 0 to word 20.
10282 */
10283 for (addr = ADV_EEP_DVC_CFG_BEGIN;
10284 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10285 ushort word;
10286
10287 if (*charfields++) {
10288 word = cpu_to_le16(*wbuf);
10289 } else {
10290 word = *wbuf;
10291 }
10292 chksum += *wbuf; /* Checksum is calculated from word values. */
10293 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10294 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10295 ASC_EEP_CMD_WRITE | addr);
10296 AdvWaitEEPCmd(iop_base);
b009bef6 10297 mdelay(ADV_EEP_DELAY_MS);
27c868c2 10298 }
1da177e4 10299
27c868c2
MW
10300 /*
10301 * Write EEPROM checksum at word 21.
10302 */
10303 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10304 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10305 AdvWaitEEPCmd(iop_base);
10306 wbuf++;
10307 charfields++;
10308
10309 /*
10310 * Write EEPROM OEM name at words 22 to 29.
10311 */
10312 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10313 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10314 ushort word;
10315
10316 if (*charfields++) {
10317 word = cpu_to_le16(*wbuf);
10318 } else {
10319 word = *wbuf;
10320 }
10321 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10322 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10323 ASC_EEP_CMD_WRITE | addr);
10324 AdvWaitEEPCmd(iop_base);
10325 }
10326 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10327 AdvWaitEEPCmd(iop_base);
1da177e4
LT
10328}
10329
10330/*
51219358
MW
10331 * Read EEPROM configuration into the specified buffer.
10332 *
10333 * Return a checksum based on the EEPROM configuration read.
1da177e4 10334 */
6f039790
GKH
10335static ushort AdvGet3550EEPConfig(AdvPortAddr iop_base,
10336 ADVEEP_3550_CONFIG *cfg_buf)
1da177e4 10337{
51219358 10338 ushort wval, chksum;
27c868c2 10339 ushort *wbuf;
51219358 10340 int eep_addr;
27c868c2 10341 ushort *charfields;
27c868c2 10342
51219358 10343 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
27c868c2 10344 wbuf = (ushort *)cfg_buf;
27c868c2
MW
10345 chksum = 0;
10346
51219358
MW
10347 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10348 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10349 wval = AdvReadEEPWord(iop_base, eep_addr);
10350 chksum += wval; /* Checksum is calculated from word values. */
10351 if (*charfields++) {
10352 *wbuf = le16_to_cpu(wval);
10353 } else {
10354 *wbuf = wval;
10355 }
10356 }
10357 /* Read checksum word. */
10358 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10359 wbuf++;
10360 charfields++;
27c868c2 10361
51219358
MW
10362 /* Read rest of EEPROM not covered by the checksum. */
10363 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10364 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10365 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10366 if (*charfields++) {
10367 *wbuf = le16_to_cpu(*wbuf);
10368 }
10369 }
10370 return chksum;
10371}
10372
10373/*
10374 * Read EEPROM configuration into the specified buffer.
10375 *
10376 * Return a checksum based on the EEPROM configuration read.
10377 */
6f039790
GKH
10378static ushort AdvGet38C0800EEPConfig(AdvPortAddr iop_base,
10379 ADVEEP_38C0800_CONFIG *cfg_buf)
51219358
MW
10380{
10381 ushort wval, chksum;
10382 ushort *wbuf;
10383 int eep_addr;
10384 ushort *charfields;
27c868c2 10385
51219358
MW
10386 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10387 wbuf = (ushort *)cfg_buf;
10388 chksum = 0;
10389
10390 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10391 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10392 wval = AdvReadEEPWord(iop_base, eep_addr);
10393 chksum += wval; /* Checksum is calculated from word values. */
27c868c2 10394 if (*charfields++) {
51219358 10395 *wbuf = le16_to_cpu(wval);
27c868c2 10396 } else {
51219358 10397 *wbuf = wval;
27c868c2 10398 }
27c868c2 10399 }
51219358
MW
10400 /* Read checksum word. */
10401 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
27c868c2
MW
10402 wbuf++;
10403 charfields++;
10404
51219358
MW
10405 /* Read rest of EEPROM not covered by the checksum. */
10406 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10407 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10408 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
27c868c2 10409 if (*charfields++) {
51219358 10410 *wbuf = le16_to_cpu(*wbuf);
27c868c2 10411 }
27c868c2 10412 }
51219358 10413 return chksum;
1da177e4
LT
10414}
10415
10416/*
51219358
MW
10417 * Read EEPROM configuration into the specified buffer.
10418 *
10419 * Return a checksum based on the EEPROM configuration read.
1da177e4 10420 */
6f039790
GKH
10421static ushort AdvGet38C1600EEPConfig(AdvPortAddr iop_base,
10422 ADVEEP_38C1600_CONFIG *cfg_buf)
1da177e4 10423{
51219358 10424 ushort wval, chksum;
27c868c2 10425 ushort *wbuf;
51219358 10426 int eep_addr;
27c868c2 10427 ushort *charfields;
27c868c2 10428
27c868c2 10429 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
51219358 10430 wbuf = (ushort *)cfg_buf;
27c868c2
MW
10431 chksum = 0;
10432
51219358
MW
10433 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10434 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10435 wval = AdvReadEEPWord(iop_base, eep_addr);
10436 chksum += wval; /* Checksum is calculated from word values. */
27c868c2 10437 if (*charfields++) {
51219358 10438 *wbuf = le16_to_cpu(wval);
27c868c2 10439 } else {
51219358 10440 *wbuf = wval;
27c868c2 10441 }
27c868c2 10442 }
51219358
MW
10443 /* Read checksum word. */
10444 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
27c868c2
MW
10445 wbuf++;
10446 charfields++;
10447
51219358
MW
10448 /* Read rest of EEPROM not covered by the checksum. */
10449 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10450 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10451 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
27c868c2 10452 if (*charfields++) {
51219358 10453 *wbuf = le16_to_cpu(*wbuf);
27c868c2 10454 }
27c868c2 10455 }
51219358 10456 return chksum;
1da177e4
LT
10457}
10458
1da177e4 10459/*
51219358
MW
10460 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10461 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10462 * all of this is done.
1da177e4 10463 *
51219358 10464 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
1da177e4 10465 *
51219358
MW
10466 * For a non-fatal error return a warning code. If there are no warnings
10467 * then 0 is returned.
1da177e4 10468 *
51219358 10469 * Note: Chip is stopped on entry.
1da177e4 10470 */
6f039790 10471static int AdvInitFrom3550EEP(ADV_DVC_VAR *asc_dvc)
1da177e4 10472{
27c868c2 10473 AdvPortAddr iop_base;
51219358
MW
10474 ushort warn_code;
10475 ADVEEP_3550_CONFIG eep_config;
1da177e4 10476
27c868c2 10477 iop_base = asc_dvc->iop_base;
1da177e4 10478
51219358 10479 warn_code = 0;
27c868c2
MW
10480
10481 /*
51219358
MW
10482 * Read the board's EEPROM configuration.
10483 *
10484 * Set default values if a bad checksum is found.
27c868c2 10485 */
51219358
MW
10486 if (AdvGet3550EEPConfig(iop_base, &eep_config) != eep_config.check_sum) {
10487 warn_code |= ASC_WARN_EEPROM_CHKSUM;
27c868c2 10488
51219358
MW
10489 /*
10490 * Set EEPROM default values.
10491 */
10492 memcpy(&eep_config, &Default_3550_EEPROM_Config,
10493 sizeof(ADVEEP_3550_CONFIG));
27c868c2 10494
51219358
MW
10495 /*
10496 * Assume the 6 byte board serial number that was read from
10497 * EEPROM is correct even if the EEPROM checksum failed.
10498 */
10499 eep_config.serial_number_word3 =
10500 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
27c868c2 10501
51219358
MW
10502 eep_config.serial_number_word2 =
10503 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
27c868c2 10504
51219358
MW
10505 eep_config.serial_number_word1 =
10506 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
27c868c2 10507
51219358
MW
10508 AdvSet3550EEPConfig(iop_base, &eep_config);
10509 }
10510 /*
10511 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10512 * EEPROM configuration that was read.
10513 *
10514 * This is the mapping of EEPROM fields to Adv Library fields.
10515 */
10516 asc_dvc->wdtr_able = eep_config.wdtr_able;
10517 asc_dvc->sdtr_able = eep_config.sdtr_able;
10518 asc_dvc->ultra_able = eep_config.ultra_able;
10519 asc_dvc->tagqng_able = eep_config.tagqng_able;
10520 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10521 asc_dvc->max_host_qng = eep_config.max_host_qng;
10522 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10523 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10524 asc_dvc->start_motor = eep_config.start_motor;
10525 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10526 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10527 asc_dvc->no_scam = eep_config.scam_tolerant;
10528 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10529 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10530 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
27c868c2 10531
27c868c2 10532 /*
51219358
MW
10533 * Set the host maximum queuing (max. 253, min. 16) and the per device
10534 * maximum queuing (max. 63, min. 4).
27c868c2 10535 */
51219358
MW
10536 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10537 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10538 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10539 /* If the value is zero, assume it is uninitialized. */
10540 if (eep_config.max_host_qng == 0) {
10541 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10542 } else {
10543 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10544 }
10545 }
10546
10547 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10548 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10549 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10550 /* If the value is zero, assume it is uninitialized. */
10551 if (eep_config.max_dvc_qng == 0) {
10552 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10553 } else {
10554 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10555 }
10556 }
27c868c2
MW
10557
10558 /*
51219358
MW
10559 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10560 * set 'max_dvc_qng' to 'max_host_qng'.
27c868c2 10561 */
51219358
MW
10562 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10563 eep_config.max_dvc_qng = eep_config.max_host_qng;
10564 }
27c868c2
MW
10565
10566 /*
51219358
MW
10567 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10568 * values based on possibly adjusted EEPROM values.
27c868c2 10569 */
51219358
MW
10570 asc_dvc->max_host_qng = eep_config.max_host_qng;
10571 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
27c868c2
MW
10572
10573 /*
51219358
MW
10574 * If the EEPROM 'termination' field is set to automatic (0), then set
10575 * the ADV_DVC_CFG 'termination' field to automatic also.
10576 *
10577 * If the termination is specified with a non-zero 'termination'
10578 * value check that a legal value is set and set the ADV_DVC_CFG
10579 * 'termination' field appropriately.
27c868c2 10580 */
51219358
MW
10581 if (eep_config.termination == 0) {
10582 asc_dvc->cfg->termination = 0; /* auto termination */
10583 } else {
10584 /* Enable manual control with low off / high off. */
10585 if (eep_config.termination == 1) {
10586 asc_dvc->cfg->termination = TERM_CTL_SEL;
27c868c2 10587
51219358
MW
10588 /* Enable manual control with low off / high on. */
10589 } else if (eep_config.termination == 2) {
10590 asc_dvc->cfg->termination = TERM_CTL_SEL | TERM_CTL_H;
10591
10592 /* Enable manual control with low on / high on. */
10593 } else if (eep_config.termination == 3) {
10594 asc_dvc->cfg->termination =
10595 TERM_CTL_SEL | TERM_CTL_H | TERM_CTL_L;
10596 } else {
27c868c2 10597 /*
51219358
MW
10598 * The EEPROM 'termination' field contains a bad value. Use
10599 * automatic termination instead.
27c868c2 10600 */
51219358
MW
10601 asc_dvc->cfg->termination = 0;
10602 warn_code |= ASC_WARN_EEPROM_TERMINATION;
27c868c2 10603 }
27c868c2 10604 }
1da177e4 10605
51219358 10606 return warn_code;
1da177e4
LT
10607}
10608
10609/*
51219358
MW
10610 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10611 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10612 * all of this is done.
1da177e4 10613 *
51219358
MW
10614 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10615 *
10616 * For a non-fatal error return a warning code. If there are no warnings
10617 * then 0 is returned.
10618 *
10619 * Note: Chip is stopped on entry.
1da177e4 10620 */
6f039790 10621static int AdvInitFrom38C0800EEP(ADV_DVC_VAR *asc_dvc)
1da177e4 10622{
51219358
MW
10623 AdvPortAddr iop_base;
10624 ushort warn_code;
10625 ADVEEP_38C0800_CONFIG eep_config;
10626 uchar tid, termination;
10627 ushort sdtr_speed = 0;
27c868c2 10628
51219358 10629 iop_base = asc_dvc->iop_base;
1da177e4 10630
51219358 10631 warn_code = 0;
27c868c2
MW
10632
10633 /*
51219358
MW
10634 * Read the board's EEPROM configuration.
10635 *
10636 * Set default values if a bad checksum is found.
27c868c2 10637 */
51219358
MW
10638 if (AdvGet38C0800EEPConfig(iop_base, &eep_config) !=
10639 eep_config.check_sum) {
10640 warn_code |= ASC_WARN_EEPROM_CHKSUM;
27c868c2 10641
51219358
MW
10642 /*
10643 * Set EEPROM default values.
10644 */
10645 memcpy(&eep_config, &Default_38C0800_EEPROM_Config,
10646 sizeof(ADVEEP_38C0800_CONFIG));
1da177e4 10647
51219358
MW
10648 /*
10649 * Assume the 6 byte board serial number that was read from
10650 * EEPROM is correct even if the EEPROM checksum failed.
10651 */
10652 eep_config.serial_number_word3 =
10653 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
1da177e4 10654
51219358
MW
10655 eep_config.serial_number_word2 =
10656 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
27c868c2 10657
51219358
MW
10658 eep_config.serial_number_word1 =
10659 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
27c868c2 10660
51219358 10661 AdvSet38C0800EEPConfig(iop_base, &eep_config);
27c868c2 10662 }
27c868c2 10663 /*
51219358
MW
10664 * Set ADV_DVC_VAR and ADV_DVC_CFG variables from the
10665 * EEPROM configuration that was read.
10666 *
10667 * This is the mapping of EEPROM fields to Adv Library fields.
27c868c2 10668 */
51219358
MW
10669 asc_dvc->wdtr_able = eep_config.wdtr_able;
10670 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10671 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10672 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10673 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10674 asc_dvc->tagqng_able = eep_config.tagqng_able;
10675 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10676 asc_dvc->max_host_qng = eep_config.max_host_qng;
10677 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10678 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10679 asc_dvc->start_motor = eep_config.start_motor;
10680 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10681 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10682 asc_dvc->no_scam = eep_config.scam_tolerant;
10683 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10684 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10685 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
27c868c2
MW
10686
10687 /*
51219358
MW
10688 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10689 * are set, then set an 'sdtr_able' bit for it.
27c868c2 10690 */
51219358
MW
10691 asc_dvc->sdtr_able = 0;
10692 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
10693 if (tid == 0) {
10694 sdtr_speed = asc_dvc->sdtr_speed1;
10695 } else if (tid == 4) {
10696 sdtr_speed = asc_dvc->sdtr_speed2;
10697 } else if (tid == 8) {
10698 sdtr_speed = asc_dvc->sdtr_speed3;
10699 } else if (tid == 12) {
10700 sdtr_speed = asc_dvc->sdtr_speed4;
10701 }
10702 if (sdtr_speed & ADV_MAX_TID) {
10703 asc_dvc->sdtr_able |= (1 << tid);
10704 }
10705 sdtr_speed >>= 4;
10706 }
27c868c2
MW
10707
10708 /*
51219358
MW
10709 * Set the host maximum queuing (max. 253, min. 16) and the per device
10710 * maximum queuing (max. 63, min. 4).
27c868c2 10711 */
51219358
MW
10712 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10713 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10714 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10715 /* If the value is zero, assume it is uninitialized. */
10716 if (eep_config.max_host_qng == 0) {
10717 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10718 } else {
10719 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10720 }
27c868c2 10721 }
1da177e4 10722
51219358
MW
10723 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10724 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10725 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10726 /* If the value is zero, assume it is uninitialized. */
10727 if (eep_config.max_dvc_qng == 0) {
10728 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10729 } else {
10730 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10731 }
10732 }
10733
10734 /*
10735 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10736 * set 'max_dvc_qng' to 'max_host_qng'.
10737 */
10738 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10739 eep_config.max_dvc_qng = eep_config.max_host_qng;
27c868c2 10740 }
1da177e4 10741
27c868c2 10742 /*
51219358
MW
10743 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10744 * values based on possibly adjusted EEPROM values.
27c868c2 10745 */
51219358
MW
10746 asc_dvc->max_host_qng = eep_config.max_host_qng;
10747 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
27c868c2
MW
10748
10749 /*
51219358
MW
10750 * If the EEPROM 'termination' field is set to automatic (0), then set
10751 * the ADV_DVC_CFG 'termination' field to automatic also.
10752 *
10753 * If the termination is specified with a non-zero 'termination'
10754 * value check that a legal value is set and set the ADV_DVC_CFG
10755 * 'termination' field appropriately.
27c868c2 10756 */
51219358
MW
10757 if (eep_config.termination_se == 0) {
10758 termination = 0; /* auto termination for SE */
10759 } else {
10760 /* Enable manual control with low off / high off. */
10761 if (eep_config.termination_se == 1) {
10762 termination = 0;
10763
10764 /* Enable manual control with low off / high on. */
10765 } else if (eep_config.termination_se == 2) {
10766 termination = TERM_SE_HI;
10767
10768 /* Enable manual control with low on / high on. */
10769 } else if (eep_config.termination_se == 3) {
10770 termination = TERM_SE;
10771 } else {
10772 /*
10773 * The EEPROM 'termination_se' field contains a bad value.
10774 * Use automatic termination instead.
10775 */
10776 termination = 0;
10777 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10778 }
27c868c2 10779 }
51219358
MW
10780
10781 if (eep_config.termination_lvd == 0) {
10782 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10783 } else {
10784 /* Enable manual control with low off / high off. */
10785 if (eep_config.termination_lvd == 1) {
10786 asc_dvc->cfg->termination = termination;
10787
10788 /* Enable manual control with low off / high on. */
10789 } else if (eep_config.termination_lvd == 2) {
10790 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
10791
10792 /* Enable manual control with low on / high on. */
10793 } else if (eep_config.termination_lvd == 3) {
10794 asc_dvc->cfg->termination = termination | TERM_LVD;
10795 } else {
10796 /*
10797 * The EEPROM 'termination_lvd' field contains a bad value.
10798 * Use automatic termination instead.
10799 */
10800 asc_dvc->cfg->termination = termination;
10801 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10802 }
27c868c2 10803 }
1da177e4 10804
51219358 10805 return warn_code;
1da177e4
LT
10806}
10807
10808/*
51219358
MW
10809 * Read the board's EEPROM configuration. Set fields in ASC_DVC_VAR and
10810 * ASC_DVC_CFG based on the EEPROM settings. The chip is stopped while
10811 * all of this is done.
1da177e4 10812 *
51219358 10813 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
1da177e4 10814 *
51219358
MW
10815 * For a non-fatal error return a warning code. If there are no warnings
10816 * then 0 is returned.
1da177e4 10817 *
51219358 10818 * Note: Chip is stopped on entry.
1da177e4 10819 */
6f039790 10820static int AdvInitFrom38C1600EEP(ADV_DVC_VAR *asc_dvc)
1da177e4 10821{
27c868c2 10822 AdvPortAddr iop_base;
51219358
MW
10823 ushort warn_code;
10824 ADVEEP_38C1600_CONFIG eep_config;
10825 uchar tid, termination;
10826 ushort sdtr_speed = 0;
1da177e4 10827
27c868c2
MW
10828 iop_base = asc_dvc->iop_base;
10829
51219358 10830 warn_code = 0;
27c868c2 10831
51219358
MW
10832 /*
10833 * Read the board's EEPROM configuration.
10834 *
10835 * Set default values if a bad checksum is found.
10836 */
10837 if (AdvGet38C1600EEPConfig(iop_base, &eep_config) !=
10838 eep_config.check_sum) {
10839 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
10840 warn_code |= ASC_WARN_EEPROM_CHKSUM;
10841
10842 /*
10843 * Set EEPROM default values.
10844 */
10845 memcpy(&eep_config, &Default_38C1600_EEPROM_Config,
10846 sizeof(ADVEEP_38C1600_CONFIG));
10847
10848 if (PCI_FUNC(pdev->devfn) != 0) {
10849 u8 ints;
10850 /*
10851 * Disable Bit 14 (BIOS_ENABLE) to fix SPARC Ultra 60
10852 * and old Mac system booting problem. The Expansion
10853 * ROM must be disabled in Function 1 for these systems
10854 */
10855 eep_config.cfg_lsw &= ~ADV_EEPROM_BIOS_ENABLE;
10856 /*
10857 * Clear the INTAB (bit 11) if the GPIO 0 input
10858 * indicates the Function 1 interrupt line is wired
10859 * to INTB.
10860 *
10861 * Set/Clear Bit 11 (INTAB) from the GPIO bit 0 input:
10862 * 1 - Function 1 interrupt line wired to INT A.
10863 * 0 - Function 1 interrupt line wired to INT B.
10864 *
10865 * Note: Function 0 is always wired to INTA.
10866 * Put all 5 GPIO bits in input mode and then read
10867 * their input values.
10868 */
10869 AdvWriteByteRegister(iop_base, IOPB_GPIO_CNTL, 0);
10870 ints = AdvReadByteRegister(iop_base, IOPB_GPIO_DATA);
10871 if ((ints & 0x01) == 0)
10872 eep_config.cfg_lsw &= ~ADV_EEPROM_INTAB;
10873 }
10874
10875 /*
10876 * Assume the 6 byte board serial number that was read from
10877 * EEPROM is correct even if the EEPROM checksum failed.
10878 */
10879 eep_config.serial_number_word3 =
10880 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
10881 eep_config.serial_number_word2 =
10882 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
10883 eep_config.serial_number_word1 =
10884 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
10885
10886 AdvSet38C1600EEPConfig(iop_base, &eep_config);
27c868c2
MW
10887 }
10888
10889 /*
51219358
MW
10890 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10891 * EEPROM configuration that was read.
10892 *
10893 * This is the mapping of EEPROM fields to Adv Library fields.
27c868c2 10894 */
51219358
MW
10895 asc_dvc->wdtr_able = eep_config.wdtr_able;
10896 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10897 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10898 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10899 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10900 asc_dvc->ppr_able = 0;
10901 asc_dvc->tagqng_able = eep_config.tagqng_able;
10902 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10903 asc_dvc->max_host_qng = eep_config.max_host_qng;
10904 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10905 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ASC_MAX_TID);
10906 asc_dvc->start_motor = eep_config.start_motor;
10907 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10908 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10909 asc_dvc->no_scam = eep_config.scam_tolerant;
27c868c2 10910
51219358
MW
10911 /*
10912 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10913 * are set, then set an 'sdtr_able' bit for it.
10914 */
10915 asc_dvc->sdtr_able = 0;
10916 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
10917 if (tid == 0) {
10918 sdtr_speed = asc_dvc->sdtr_speed1;
10919 } else if (tid == 4) {
10920 sdtr_speed = asc_dvc->sdtr_speed2;
10921 } else if (tid == 8) {
10922 sdtr_speed = asc_dvc->sdtr_speed3;
10923 } else if (tid == 12) {
10924 sdtr_speed = asc_dvc->sdtr_speed4;
10925 }
10926 if (sdtr_speed & ASC_MAX_TID) {
10927 asc_dvc->sdtr_able |= (1 << tid);
10928 }
10929 sdtr_speed >>= 4;
10930 }
27c868c2 10931
51219358
MW
10932 /*
10933 * Set the host maximum queuing (max. 253, min. 16) and the per device
10934 * maximum queuing (max. 63, min. 4).
10935 */
10936 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10937 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10938 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10939 /* If the value is zero, assume it is uninitialized. */
10940 if (eep_config.max_host_qng == 0) {
10941 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10942 } else {
10943 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
27c868c2 10944 }
51219358 10945 }
27c868c2 10946
51219358
MW
10947 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10948 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10949 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10950 /* If the value is zero, assume it is uninitialized. */
10951 if (eep_config.max_dvc_qng == 0) {
10952 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10953 } else {
10954 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10955 }
10956 }
10957
10958 /*
10959 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10960 * set 'max_dvc_qng' to 'max_host_qng'.
10961 */
10962 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10963 eep_config.max_dvc_qng = eep_config.max_host_qng;
27c868c2
MW
10964 }
10965
10966 /*
51219358
MW
10967 * Set ASC_DVC_VAR 'max_host_qng' and ASC_DVC_VAR 'max_dvc_qng'
10968 * values based on possibly adjusted EEPROM values.
10969 */
10970 asc_dvc->max_host_qng = eep_config.max_host_qng;
10971 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10972
10973 /*
10974 * If the EEPROM 'termination' field is set to automatic (0), then set
10975 * the ASC_DVC_CFG 'termination' field to automatic also.
10976 *
10977 * If the termination is specified with a non-zero 'termination'
10978 * value check that a legal value is set and set the ASC_DVC_CFG
10979 * 'termination' field appropriately.
27c868c2 10980 */
51219358
MW
10981 if (eep_config.termination_se == 0) {
10982 termination = 0; /* auto termination for SE */
10983 } else {
10984 /* Enable manual control with low off / high off. */
10985 if (eep_config.termination_se == 1) {
10986 termination = 0;
27c868c2 10987
51219358
MW
10988 /* Enable manual control with low off / high on. */
10989 } else if (eep_config.termination_se == 2) {
10990 termination = TERM_SE_HI;
27c868c2 10991
51219358
MW
10992 /* Enable manual control with low on / high on. */
10993 } else if (eep_config.termination_se == 3) {
10994 termination = TERM_SE;
10995 } else {
10996 /*
10997 * The EEPROM 'termination_se' field contains a bad value.
10998 * Use automatic termination instead.
10999 */
11000 termination = 0;
11001 warn_code |= ASC_WARN_EEPROM_TERMINATION;
11002 }
11003 }
27c868c2 11004
51219358
MW
11005 if (eep_config.termination_lvd == 0) {
11006 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
11007 } else {
11008 /* Enable manual control with low off / high off. */
11009 if (eep_config.termination_lvd == 1) {
11010 asc_dvc->cfg->termination = termination;
27c868c2 11011
51219358
MW
11012 /* Enable manual control with low off / high on. */
11013 } else if (eep_config.termination_lvd == 2) {
11014 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
27c868c2 11015
51219358
MW
11016 /* Enable manual control with low on / high on. */
11017 } else if (eep_config.termination_lvd == 3) {
11018 asc_dvc->cfg->termination = termination | TERM_LVD;
11019 } else {
11020 /*
11021 * The EEPROM 'termination_lvd' field contains a bad value.
11022 * Use automatic termination instead.
11023 */
11024 asc_dvc->cfg->termination = termination;
11025 warn_code |= ASC_WARN_EEPROM_TERMINATION;
11026 }
27c868c2 11027 }
51219358
MW
11028
11029 return warn_code;
1da177e4
LT
11030}
11031
11032/*
51219358 11033 * Initialize the ADV_DVC_VAR structure.
1da177e4 11034 *
51219358 11035 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
1da177e4 11036 *
51219358
MW
11037 * For a non-fatal error return a warning code. If there are no warnings
11038 * then 0 is returned.
1da177e4 11039 */
6f039790 11040static int AdvInitGetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
1da177e4 11041{
9d0e96eb
MW
11042 struct asc_board *board = shost_priv(shost);
11043 ADV_DVC_VAR *asc_dvc = &board->dvc_var.adv_dvc_var;
51219358
MW
11044 unsigned short warn_code = 0;
11045 AdvPortAddr iop_base = asc_dvc->iop_base;
11046 u16 cmd;
11047 int status;
27c868c2 11048
51219358 11049 asc_dvc->err_code = 0;
27c868c2
MW
11050
11051 /*
51219358
MW
11052 * Save the state of the PCI Configuration Command Register
11053 * "Parity Error Response Control" Bit. If the bit is clear (0),
11054 * in AdvInitAsc3550/38C0800Driver() tell the microcode to ignore
11055 * DMA parity errors.
27c868c2 11056 */
51219358
MW
11057 asc_dvc->cfg->control_flag = 0;
11058 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
11059 if ((cmd & PCI_COMMAND_PARITY) == 0)
11060 asc_dvc->cfg->control_flag |= CONTROL_FLAG_IGNORE_PERR;
27c868c2 11061
51219358
MW
11062 asc_dvc->cfg->chip_version =
11063 AdvGetChipVersion(iop_base, asc_dvc->bus_type);
11064
b352f923 11065 ASC_DBG(1, "iopb_chip_id_1: 0x%x 0x%x\n",
51219358
MW
11066 (ushort)AdvReadByteRegister(iop_base, IOPB_CHIP_ID_1),
11067 (ushort)ADV_CHIP_ID_BYTE);
11068
b352f923 11069 ASC_DBG(1, "iopw_chip_id_0: 0x%x 0x%x\n",
51219358
MW
11070 (ushort)AdvReadWordRegister(iop_base, IOPW_CHIP_ID_0),
11071 (ushort)ADV_CHIP_ID_WORD);
27c868c2
MW
11072
11073 /*
51219358 11074 * Reset the chip to start and allow register writes.
27c868c2 11075 */
51219358
MW
11076 if (AdvFindSignature(iop_base) == 0) {
11077 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
11078 return ADV_ERROR;
11079 } else {
27c868c2 11080 /*
51219358 11081 * The caller must set 'chip_type' to a valid setting.
27c868c2 11082 */
51219358
MW
11083 if (asc_dvc->chip_type != ADV_CHIP_ASC3550 &&
11084 asc_dvc->chip_type != ADV_CHIP_ASC38C0800 &&
11085 asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
11086 asc_dvc->err_code |= ASC_IERR_BAD_CHIPTYPE;
11087 return ADV_ERROR;
11088 }
1da177e4 11089
51219358
MW
11090 /*
11091 * Reset Chip.
11092 */
11093 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
11094 ADV_CTRL_REG_CMD_RESET);
11095 mdelay(100);
11096 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
11097 ADV_CTRL_REG_CMD_WR_IO_REG);
11098
11099 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
11100 status = AdvInitFrom38C1600EEP(asc_dvc);
11101 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
11102 status = AdvInitFrom38C0800EEP(asc_dvc);
11103 } else {
11104 status = AdvInitFrom3550EEP(asc_dvc);
27c868c2 11105 }
51219358 11106 warn_code |= status;
27c868c2 11107 }
1da177e4 11108
9d0e96eb
MW
11109 if (warn_code != 0)
11110 shost_printk(KERN_WARNING, shost, "warning: 0x%x\n", warn_code);
51219358 11111
9d0e96eb
MW
11112 if (asc_dvc->err_code)
11113 shost_printk(KERN_ERR, shost, "error code 0x%x\n",
11114 asc_dvc->err_code);
51219358
MW
11115
11116 return asc_dvc->err_code;
1da177e4 11117}
51219358
MW
11118#endif
11119
11120static struct scsi_host_template advansys_template = {
11121 .proc_name = DRV_NAME,
11122#ifdef CONFIG_PROC_FS
b59fb6fd 11123 .show_info = advansys_show_info,
51219358
MW
11124#endif
11125 .name = DRV_NAME,
11126 .info = advansys_info,
11127 .queuecommand = advansys_queuecommand,
eac0b0c7 11128 .eh_host_reset_handler = advansys_reset,
51219358
MW
11129 .bios_param = advansys_biosparam,
11130 .slave_configure = advansys_slave_configure,
11131 /*
11132 * Because the driver may control an ISA adapter 'unchecked_isa_dma'
11133 * must be set. The flag will be cleared in advansys_board_found
11134 * for non-ISA adapters.
11135 */
11136 .unchecked_isa_dma = 1,
11137 /*
11138 * All adapters controlled by this driver are capable of large
11139 * scatter-gather lists. According to the mid-level SCSI documentation
11140 * this obviates any performance gain provided by setting
11141 * 'use_clustering'. But empirically while CPU utilization is increased
11142 * by enabling clustering, I/O throughput increases as well.
11143 */
11144 .use_clustering = ENABLE_CLUSTERING,
9c17c62a 11145 .use_blk_tags = 1,
51219358 11146};
1da177e4 11147
6f039790 11148static int advansys_wide_init_chip(struct Scsi_Host *shost)
b2c16f58 11149{
9d0e96eb
MW
11150 struct asc_board *board = shost_priv(shost);
11151 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
0ce53822 11152 size_t sgblk_pool_size;
b2c16f58
MW
11153 int warn_code, err_code;
11154
11155 /*
11156 * Allocate buffer carrier structures. The total size
98b96a7d 11157 * is about 8 KB, so allocate all at once.
b2c16f58 11158 */
98b96a7d
HR
11159 adv_dvc->carrier = dma_alloc_coherent(board->dev,
11160 ADV_CARRIER_BUFSIZE, &adv_dvc->carrier_addr, GFP_KERNEL);
11161 ASC_DBG(1, "carrier 0x%p\n", adv_dvc->carrier);
b2c16f58 11162
98b96a7d 11163 if (!adv_dvc->carrier)
b2c16f58
MW
11164 goto kmalloc_failed;
11165
11166 /*
11167 * Allocate up to 'max_host_qng' request structures for the Wide
11168 * board. The total size is about 16 KB, so allocate all at once.
11169 * If the allocation fails decrement and try again.
11170 */
4b47e464
HR
11171 board->adv_reqp_size = adv_dvc->max_host_qng * sizeof(adv_req_t);
11172 if (board->adv_reqp_size & 0x1f) {
11173 ASC_DBG(1, "unaligned reqp %lu bytes\n", sizeof(adv_req_t));
11174 board->adv_reqp_size = ADV_32BALIGN(board->adv_reqp_size);
b2c16f58 11175 }
4b47e464
HR
11176 board->adv_reqp = dma_alloc_coherent(board->dev, board->adv_reqp_size,
11177 &board->adv_reqp_addr, GFP_KERNEL);
b2c16f58 11178
4b47e464 11179 if (!board->adv_reqp)
b2c16f58
MW
11180 goto kmalloc_failed;
11181
4b47e464
HR
11182 ASC_DBG(1, "reqp 0x%p, req_cnt %d, bytes %lu\n", board->adv_reqp,
11183 adv_dvc->max_host_qng, board->adv_reqp_size);
b2c16f58
MW
11184
11185 /*
11186 * Allocate up to ADV_TOT_SG_BLOCK request structures for
11187 * the Wide board. Each structure is about 136 bytes.
11188 */
0ce53822
HR
11189 sgblk_pool_size = sizeof(adv_sgblk_t) * ADV_TOT_SG_BLOCK;
11190 board->adv_sgblk_pool = dma_pool_create("adv_sgblk", board->dev,
11191 sgblk_pool_size, 32, 0);
b2c16f58 11192
0ce53822
HR
11193 ASC_DBG(1, "sg_cnt %d * %lu = %lu bytes\n", ADV_TOT_SG_BLOCK,
11194 sizeof(adv_sgblk_t), sgblk_pool_size);
b2c16f58 11195
0ce53822 11196 if (!board->adv_sgblk_pool)
b2c16f58
MW
11197 goto kmalloc_failed;
11198
9d0e96eb 11199 if (adv_dvc->chip_type == ADV_CHIP_ASC3550) {
b352f923 11200 ASC_DBG(2, "AdvInitAsc3550Driver()\n");
9d0e96eb
MW
11201 warn_code = AdvInitAsc3550Driver(adv_dvc);
11202 } else if (adv_dvc->chip_type == ADV_CHIP_ASC38C0800) {
b352f923 11203 ASC_DBG(2, "AdvInitAsc38C0800Driver()\n");
9d0e96eb 11204 warn_code = AdvInitAsc38C0800Driver(adv_dvc);
b2c16f58 11205 } else {
b352f923 11206 ASC_DBG(2, "AdvInitAsc38C1600Driver()\n");
9d0e96eb 11207 warn_code = AdvInitAsc38C1600Driver(adv_dvc);
b2c16f58 11208 }
9d0e96eb 11209 err_code = adv_dvc->err_code;
b2c16f58
MW
11210
11211 if (warn_code || err_code) {
9d0e96eb
MW
11212 shost_printk(KERN_WARNING, shost, "error: warn 0x%x, error "
11213 "0x%x\n", warn_code, err_code);
b2c16f58
MW
11214 }
11215
11216 goto exit;
11217
11218 kmalloc_failed:
9d0e96eb 11219 shost_printk(KERN_ERR, shost, "error: kmalloc() failed\n");
b2c16f58
MW
11220 err_code = ADV_ERROR;
11221 exit:
11222 return err_code;
11223}
11224
98d41c29 11225static void advansys_wide_free_mem(struct asc_board *board)
b2c16f58 11226{
98d41c29 11227 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
98b96a7d
HR
11228
11229 if (adv_dvc->carrier) {
11230 dma_free_coherent(board->dev, ADV_CARRIER_BUFSIZE,
11231 adv_dvc->carrier, adv_dvc->carrier_addr);
11232 adv_dvc->carrier = NULL;
11233 }
4b47e464
HR
11234 if (board->adv_reqp) {
11235 dma_free_coherent(board->dev, board->adv_reqp_size,
11236 board->adv_reqp, board->adv_reqp_addr);
11237 board->adv_reqp = NULL;
11238 }
0ce53822
HR
11239 if (board->adv_sgblk_pool) {
11240 dma_pool_destroy(board->adv_sgblk_pool);
11241 board->adv_sgblk_pool = NULL;
b2c16f58
MW
11242 }
11243}
11244
6f039790
GKH
11245static int advansys_board_found(struct Scsi_Host *shost, unsigned int iop,
11246 int bus_type)
27c868c2 11247{
d361db48 11248 struct pci_dev *pdev;
d2411495 11249 struct asc_board *boardp = shost_priv(shost);
27c868c2
MW
11250 ASC_DVC_VAR *asc_dvc_varp = NULL;
11251 ADV_DVC_VAR *adv_dvc_varp = NULL;
d361db48 11252 int share_irq, warn_code, ret;
27c868c2 11253
d361db48 11254 pdev = (bus_type == ASC_IS_PCI) ? to_pci_dev(boardp->dev) : NULL;
27c868c2
MW
11255
11256 if (ASC_NARROW_BOARD(boardp)) {
b352f923 11257 ASC_DBG(1, "narrow board\n");
27c868c2
MW
11258 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
11259 asc_dvc_varp->bus_type = bus_type;
11260 asc_dvc_varp->drv_ptr = boardp;
11261 asc_dvc_varp->cfg = &boardp->dvc_cfg.asc_dvc_cfg;
27c868c2 11262 asc_dvc_varp->iop_base = iop;
27c868c2 11263 } else {
57ba5fe9 11264#ifdef CONFIG_PCI
27c868c2
MW
11265 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
11266 adv_dvc_varp->drv_ptr = boardp;
11267 adv_dvc_varp->cfg = &boardp->dvc_cfg.adv_dvc_cfg;
27c868c2 11268 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW) {
b352f923 11269 ASC_DBG(1, "wide board ASC-3550\n");
27c868c2
MW
11270 adv_dvc_varp->chip_type = ADV_CHIP_ASC3550;
11271 } else if (pdev->device == PCI_DEVICE_ID_38C0800_REV1) {
b352f923 11272 ASC_DBG(1, "wide board ASC-38C0800\n");
27c868c2
MW
11273 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C0800;
11274 } else {
b352f923 11275 ASC_DBG(1, "wide board ASC-38C1600\n");
27c868c2
MW
11276 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C1600;
11277 }
27c868c2 11278
57ba5fe9 11279 boardp->asc_n_io_port = pci_resource_len(pdev, 1);
25729a7f 11280 boardp->ioremap_addr = pci_ioremap_bar(pdev, 1);
57ba5fe9 11281 if (!boardp->ioremap_addr) {
9d511a4b 11282 shost_printk(KERN_ERR, shost, "ioremap(%lx, %d) "
9d0e96eb 11283 "returned NULL\n",
9d511a4b 11284 (long)pci_resource_start(pdev, 1),
9d0e96eb 11285 boardp->asc_n_io_port);
d361db48 11286 ret = -ENODEV;
b2c16f58 11287 goto err_shost;
27c868c2 11288 }
b352f923
MW
11289 adv_dvc_varp->iop_base = (AdvPortAddr)boardp->ioremap_addr;
11290 ASC_DBG(1, "iop_base: 0x%p\n", adv_dvc_varp->iop_base);
27c868c2
MW
11291
11292 /*
11293 * Even though it isn't used to access wide boards, other
11294 * than for the debug line below, save I/O Port address so
11295 * that it can be reported.
11296 */
11297 boardp->ioport = iop;
11298
b352f923
MW
11299 ASC_DBG(1, "iopb_chip_id_1 0x%x, iopw_chip_id_0 0x%x\n",
11300 (ushort)inp(iop + 1), (ushort)inpw(iop));
57ba5fe9 11301#endif /* CONFIG_PCI */
27c868c2
MW
11302 }
11303
27c868c2 11304 if (ASC_NARROW_BOARD(boardp)) {
27c868c2
MW
11305 /*
11306 * Set the board bus type and PCI IRQ before
11307 * calling AscInitGetConfig().
11308 */
11309 switch (asc_dvc_varp->bus_type) {
11310#ifdef CONFIG_ISA
11311 case ASC_IS_ISA:
11312 shost->unchecked_isa_dma = TRUE;
074c8fe4 11313 share_irq = 0;
27c868c2
MW
11314 break;
11315 case ASC_IS_VL:
11316 shost->unchecked_isa_dma = FALSE;
074c8fe4 11317 share_irq = 0;
27c868c2
MW
11318 break;
11319 case ASC_IS_EISA:
11320 shost->unchecked_isa_dma = FALSE;
074c8fe4 11321 share_irq = IRQF_SHARED;
27c868c2
MW
11322 break;
11323#endif /* CONFIG_ISA */
11324#ifdef CONFIG_PCI
11325 case ASC_IS_PCI:
27c868c2 11326 shost->unchecked_isa_dma = FALSE;
074c8fe4 11327 share_irq = IRQF_SHARED;
27c868c2
MW
11328 break;
11329#endif /* CONFIG_PCI */
11330 default:
9d0e96eb
MW
11331 shost_printk(KERN_ERR, shost, "unknown adapter type: "
11332 "%d\n", asc_dvc_varp->bus_type);
27c868c2 11333 shost->unchecked_isa_dma = TRUE;
074c8fe4 11334 share_irq = 0;
27c868c2
MW
11335 break;
11336 }
27c868c2 11337
27c868c2
MW
11338 /*
11339 * NOTE: AscInitGetConfig() may change the board's
11340 * bus_type value. The bus_type value should no
11341 * longer be used. If the bus_type field must be
11342 * referenced only use the bit-wise AND operator "&".
11343 */
b352f923 11344 ASC_DBG(2, "AscInitGetConfig()\n");
9d0e96eb 11345 ret = AscInitGetConfig(shost) ? -ENODEV : 0;
27c868c2 11346 } else {
c2dce2fa
MW
11347#ifdef CONFIG_PCI
11348 /*
11349 * For Wide boards set PCI information before calling
11350 * AdvInitGetConfig().
11351 */
c2dce2fa
MW
11352 shost->unchecked_isa_dma = FALSE;
11353 share_irq = IRQF_SHARED;
b352f923 11354 ASC_DBG(2, "AdvInitGetConfig()\n");
394dbf3f 11355
9d0e96eb 11356 ret = AdvInitGetConfig(pdev, shost) ? -ENODEV : 0;
c2dce2fa 11357#endif /* CONFIG_PCI */
27c868c2
MW
11358 }
11359
d361db48 11360 if (ret)
b59fb6fd 11361 goto err_unmap;
27c868c2
MW
11362
11363 /*
11364 * Save the EEPROM configuration so that it can be displayed
11365 * from /proc/scsi/advansys/[0...].
11366 */
11367 if (ASC_NARROW_BOARD(boardp)) {
11368
11369 ASCEEP_CONFIG *ep;
11370
11371 /*
11372 * Set the adapter's target id bit in the 'init_tidmask' field.
11373 */
11374 boardp->init_tidmask |=
11375 ADV_TID_TO_TIDMASK(asc_dvc_varp->cfg->chip_scsi_id);
11376
11377 /*
11378 * Save EEPROM settings for the board.
11379 */
11380 ep = &boardp->eep_config.asc_eep;
11381
11382 ep->init_sdtr = asc_dvc_varp->cfg->sdtr_enable;
11383 ep->disc_enable = asc_dvc_varp->cfg->disc_enable;
11384 ep->use_cmd_qng = asc_dvc_varp->cfg->cmd_qng_enabled;
11385 ASC_EEP_SET_DMA_SPD(ep, asc_dvc_varp->cfg->isa_dma_speed);
11386 ep->start_motor = asc_dvc_varp->start_motor;
11387 ep->cntl = asc_dvc_varp->dvc_cntl;
11388 ep->no_scam = asc_dvc_varp->no_scam;
11389 ep->max_total_qng = asc_dvc_varp->max_total_qng;
11390 ASC_EEP_SET_CHIP_ID(ep, asc_dvc_varp->cfg->chip_scsi_id);
11391 /* 'max_tag_qng' is set to the same value for every device. */
11392 ep->max_tag_qng = asc_dvc_varp->cfg->max_tag_qng[0];
11393 ep->adapter_info[0] = asc_dvc_varp->cfg->adapter_info[0];
11394 ep->adapter_info[1] = asc_dvc_varp->cfg->adapter_info[1];
11395 ep->adapter_info[2] = asc_dvc_varp->cfg->adapter_info[2];
11396 ep->adapter_info[3] = asc_dvc_varp->cfg->adapter_info[3];
11397 ep->adapter_info[4] = asc_dvc_varp->cfg->adapter_info[4];
11398 ep->adapter_info[5] = asc_dvc_varp->cfg->adapter_info[5];
11399
11400 /*
11401 * Modify board configuration.
11402 */
b352f923 11403 ASC_DBG(2, "AscInitSetConfig()\n");
9d0e96eb 11404 ret = AscInitSetConfig(pdev, shost) ? -ENODEV : 0;
d361db48 11405 if (ret)
b59fb6fd 11406 goto err_unmap;
27c868c2
MW
11407 } else {
11408 ADVEEP_3550_CONFIG *ep_3550;
11409 ADVEEP_38C0800_CONFIG *ep_38C0800;
11410 ADVEEP_38C1600_CONFIG *ep_38C1600;
11411
11412 /*
11413 * Save Wide EEP Configuration Information.
11414 */
11415 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
11416 ep_3550 = &boardp->eep_config.adv_3550_eep;
11417
11418 ep_3550->adapter_scsi_id = adv_dvc_varp->chip_scsi_id;
11419 ep_3550->max_host_qng = adv_dvc_varp->max_host_qng;
11420 ep_3550->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11421 ep_3550->termination = adv_dvc_varp->cfg->termination;
11422 ep_3550->disc_enable = adv_dvc_varp->cfg->disc_enable;
11423 ep_3550->bios_ctrl = adv_dvc_varp->bios_ctrl;
11424 ep_3550->wdtr_able = adv_dvc_varp->wdtr_able;
11425 ep_3550->sdtr_able = adv_dvc_varp->sdtr_able;
11426 ep_3550->ultra_able = adv_dvc_varp->ultra_able;
11427 ep_3550->tagqng_able = adv_dvc_varp->tagqng_able;
11428 ep_3550->start_motor = adv_dvc_varp->start_motor;
11429 ep_3550->scsi_reset_delay =
11430 adv_dvc_varp->scsi_reset_wait;
11431 ep_3550->serial_number_word1 =
11432 adv_dvc_varp->cfg->serial1;
11433 ep_3550->serial_number_word2 =
11434 adv_dvc_varp->cfg->serial2;
11435 ep_3550->serial_number_word3 =
11436 adv_dvc_varp->cfg->serial3;
11437 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
11438 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
11439
11440 ep_38C0800->adapter_scsi_id =
11441 adv_dvc_varp->chip_scsi_id;
11442 ep_38C0800->max_host_qng = adv_dvc_varp->max_host_qng;
11443 ep_38C0800->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11444 ep_38C0800->termination_lvd =
11445 adv_dvc_varp->cfg->termination;
11446 ep_38C0800->disc_enable =
11447 adv_dvc_varp->cfg->disc_enable;
11448 ep_38C0800->bios_ctrl = adv_dvc_varp->bios_ctrl;
11449 ep_38C0800->wdtr_able = adv_dvc_varp->wdtr_able;
11450 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11451 ep_38C0800->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11452 ep_38C0800->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11453 ep_38C0800->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11454 ep_38C0800->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11455 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11456 ep_38C0800->start_motor = adv_dvc_varp->start_motor;
11457 ep_38C0800->scsi_reset_delay =
11458 adv_dvc_varp->scsi_reset_wait;
11459 ep_38C0800->serial_number_word1 =
11460 adv_dvc_varp->cfg->serial1;
11461 ep_38C0800->serial_number_word2 =
11462 adv_dvc_varp->cfg->serial2;
11463 ep_38C0800->serial_number_word3 =
11464 adv_dvc_varp->cfg->serial3;
11465 } else {
11466 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
11467
11468 ep_38C1600->adapter_scsi_id =
11469 adv_dvc_varp->chip_scsi_id;
11470 ep_38C1600->max_host_qng = adv_dvc_varp->max_host_qng;
11471 ep_38C1600->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11472 ep_38C1600->termination_lvd =
11473 adv_dvc_varp->cfg->termination;
11474 ep_38C1600->disc_enable =
11475 adv_dvc_varp->cfg->disc_enable;
11476 ep_38C1600->bios_ctrl = adv_dvc_varp->bios_ctrl;
11477 ep_38C1600->wdtr_able = adv_dvc_varp->wdtr_able;
11478 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11479 ep_38C1600->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11480 ep_38C1600->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11481 ep_38C1600->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11482 ep_38C1600->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11483 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11484 ep_38C1600->start_motor = adv_dvc_varp->start_motor;
11485 ep_38C1600->scsi_reset_delay =
11486 adv_dvc_varp->scsi_reset_wait;
11487 ep_38C1600->serial_number_word1 =
11488 adv_dvc_varp->cfg->serial1;
11489 ep_38C1600->serial_number_word2 =
11490 adv_dvc_varp->cfg->serial2;
11491 ep_38C1600->serial_number_word3 =
11492 adv_dvc_varp->cfg->serial3;
11493 }
11494
11495 /*
11496 * Set the adapter's target id bit in the 'init_tidmask' field.
11497 */
11498 boardp->init_tidmask |=
11499 ADV_TID_TO_TIDMASK(adv_dvc_varp->chip_scsi_id);
27c868c2
MW
11500 }
11501
11502 /*
11503 * Channels are numbered beginning with 0. For AdvanSys one host
11504 * structure supports one channel. Multi-channel boards have a
11505 * separate host structure for each channel.
11506 */
11507 shost->max_channel = 0;
11508 if (ASC_NARROW_BOARD(boardp)) {
11509 shost->max_id = ASC_MAX_TID + 1;
11510 shost->max_lun = ASC_MAX_LUN + 1;
f05ec594 11511 shost->max_cmd_len = ASC_MAX_CDB_LEN;
27c868c2
MW
11512
11513 shost->io_port = asc_dvc_varp->iop_base;
11514 boardp->asc_n_io_port = ASC_IOADR_GAP;
11515 shost->this_id = asc_dvc_varp->cfg->chip_scsi_id;
11516
11517 /* Set maximum number of queues the adapter can handle. */
11518 shost->can_queue = asc_dvc_varp->max_total_qng;
11519 } else {
11520 shost->max_id = ADV_MAX_TID + 1;
11521 shost->max_lun = ADV_MAX_LUN + 1;
f05ec594 11522 shost->max_cmd_len = ADV_MAX_CDB_LEN;
27c868c2
MW
11523
11524 /*
11525 * Save the I/O Port address and length even though
11526 * I/O ports are not used to access Wide boards.
11527 * Instead the Wide boards are accessed with
11528 * PCI Memory Mapped I/O.
11529 */
11530 shost->io_port = iop;
27c868c2
MW
11531
11532 shost->this_id = adv_dvc_varp->chip_scsi_id;
11533
11534 /* Set maximum number of queues the adapter can handle. */
11535 shost->can_queue = adv_dvc_varp->max_host_qng;
11536 }
9c17c62a
HR
11537 ret = scsi_init_shared_tag_map(shost, shost->can_queue);
11538 if (ret) {
11539 shost_printk(KERN_ERR, shost, "init tag map failed\n");
11540 goto err_free_dma;
11541 }
27c868c2 11542
27c868c2
MW
11543 /*
11544 * Following v1.3.89, 'cmd_per_lun' is no longer needed
11545 * and should be set to zero.
11546 *
11547 * But because of a bug introduced in v1.3.89 if the driver is
11548 * compiled as a module and 'cmd_per_lun' is zero, the Mid-Level
11549 * SCSI function 'allocate_device' will panic. To allow the driver
11550 * to work as a module in these kernels set 'cmd_per_lun' to 1.
11551 *
11552 * Note: This is wrong. cmd_per_lun should be set to the depth
11553 * you want on untagged devices always.
11554 #ifdef MODULE
11555 */
11556 shost->cmd_per_lun = 1;
11557/* #else
11558 shost->cmd_per_lun = 0;
11559#endif */
11560
11561 /*
11562 * Set the maximum number of scatter-gather elements the
11563 * adapter can handle.
11564 */
11565 if (ASC_NARROW_BOARD(boardp)) {
11566 /*
11567 * Allow two commands with 'sg_tablesize' scatter-gather
11568 * elements to be executed simultaneously. This value is
11569 * the theoretical hardware limit. It may be decreased
11570 * below.
11571 */
11572 shost->sg_tablesize =
11573 (((asc_dvc_varp->max_total_qng - 2) / 2) *
11574 ASC_SG_LIST_PER_Q) + 1;
11575 } else {
11576 shost->sg_tablesize = ADV_MAX_SG_LIST;
11577 }
11578
11579 /*
11580 * The value of 'sg_tablesize' can not exceed the SCSI
11581 * mid-level driver definition of SG_ALL. SG_ALL also
11582 * must not be exceeded, because it is used to define the
11583 * size of the scatter-gather table in 'struct asc_sg_head'.
11584 */
11585 if (shost->sg_tablesize > SG_ALL) {
11586 shost->sg_tablesize = SG_ALL;
11587 }
11588
b352f923 11589 ASC_DBG(1, "sg_tablesize: %d\n", shost->sg_tablesize);
27c868c2
MW
11590
11591 /* BIOS start address. */
11592 if (ASC_NARROW_BOARD(boardp)) {
b2c16f58
MW
11593 shost->base = AscGetChipBiosAddress(asc_dvc_varp->iop_base,
11594 asc_dvc_varp->bus_type);
27c868c2
MW
11595 } else {
11596 /*
11597 * Fill-in BIOS board variables. The Wide BIOS saves
11598 * information in LRAM that is used by the driver.
11599 */
11600 AdvReadWordLram(adv_dvc_varp->iop_base,
11601 BIOS_SIGNATURE, boardp->bios_signature);
11602 AdvReadWordLram(adv_dvc_varp->iop_base,
11603 BIOS_VERSION, boardp->bios_version);
11604 AdvReadWordLram(adv_dvc_varp->iop_base,
11605 BIOS_CODESEG, boardp->bios_codeseg);
11606 AdvReadWordLram(adv_dvc_varp->iop_base,
11607 BIOS_CODELEN, boardp->bios_codelen);
11608
b352f923 11609 ASC_DBG(1, "bios_signature 0x%x, bios_version 0x%x\n",
27c868c2
MW
11610 boardp->bios_signature, boardp->bios_version);
11611
b352f923 11612 ASC_DBG(1, "bios_codeseg 0x%x, bios_codelen 0x%x\n",
27c868c2
MW
11613 boardp->bios_codeseg, boardp->bios_codelen);
11614
11615 /*
11616 * If the BIOS saved a valid signature, then fill in
11617 * the BIOS code segment base address.
11618 */
11619 if (boardp->bios_signature == 0x55AA) {
11620 /*
11621 * Convert x86 realmode code segment to a linear
11622 * address by shifting left 4.
11623 */
11624 shost->base = ((ulong)boardp->bios_codeseg << 4);
11625 } else {
11626 shost->base = 0;
11627 }
11628 }
11629
11630 /*
11631 * Register Board Resources - I/O Port, DMA, IRQ
11632 */
11633
27c868c2
MW
11634 /* Register DMA Channel for Narrow boards. */
11635 shost->dma_channel = NO_ISA_DMA; /* Default to no ISA DMA. */
11636#ifdef CONFIG_ISA
11637 if (ASC_NARROW_BOARD(boardp)) {
11638 /* Register DMA channel for ISA bus. */
11639 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
11640 shost->dma_channel = asc_dvc_varp->cfg->isa_dma_channel;
01fbfe0b 11641 ret = request_dma(shost->dma_channel, DRV_NAME);
b2c16f58 11642 if (ret) {
9d0e96eb
MW
11643 shost_printk(KERN_ERR, shost, "request_dma() "
11644 "%d failed %d\n",
11645 shost->dma_channel, ret);
b59fb6fd 11646 goto err_unmap;
27c868c2
MW
11647 }
11648 AscEnableIsaDma(shost->dma_channel);
11649 }
11650 }
11651#endif /* CONFIG_ISA */
11652
11653 /* Register IRQ Number. */
b352f923 11654 ASC_DBG(2, "request_irq(%d, %p)\n", boardp->irq, shost);
074c8fe4 11655
d361db48 11656 ret = request_irq(boardp->irq, advansys_interrupt, share_irq,
01fbfe0b 11657 DRV_NAME, shost);
074c8fe4
MW
11658
11659 if (ret) {
27c868c2 11660 if (ret == -EBUSY) {
9d0e96eb
MW
11661 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11662 "already in use\n", boardp->irq);
27c868c2 11663 } else if (ret == -EINVAL) {
9d0e96eb
MW
11664 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11665 "not valid\n", boardp->irq);
27c868c2 11666 } else {
9d0e96eb
MW
11667 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11668 "failed with %d\n", boardp->irq, ret);
27c868c2 11669 }
b2c16f58 11670 goto err_free_dma;
27c868c2
MW
11671 }
11672
11673 /*
11674 * Initialize board RISC chip and enable interrupts.
11675 */
11676 if (ASC_NARROW_BOARD(boardp)) {
b352f923 11677 ASC_DBG(2, "AscInitAsc1000Driver()\n");
7d5d408c
FT
11678
11679 asc_dvc_varp->overrun_buf = kzalloc(ASC_OVERRUN_BSIZE, GFP_KERNEL);
11680 if (!asc_dvc_varp->overrun_buf) {
11681 ret = -ENOMEM;
9a908c1a 11682 goto err_free_irq;
7d5d408c 11683 }
27c868c2 11684 warn_code = AscInitAsc1000Driver(asc_dvc_varp);
27c868c2 11685
d361db48 11686 if (warn_code || asc_dvc_varp->err_code) {
9d0e96eb
MW
11687 shost_printk(KERN_ERR, shost, "error: init_state 0x%x, "
11688 "warn 0x%x, error 0x%x\n",
11689 asc_dvc_varp->init_state, warn_code,
11690 asc_dvc_varp->err_code);
9a908c1a 11691 if (!asc_dvc_varp->overrun_dma) {
d361db48 11692 ret = -ENODEV;
9a908c1a 11693 goto err_free_mem;
7d5d408c 11694 }
27c868c2
MW
11695 }
11696 } else {
9a908c1a 11697 if (advansys_wide_init_chip(shost)) {
d361db48 11698 ret = -ENODEV;
9a908c1a
HRK
11699 goto err_free_mem;
11700 }
27c868c2
MW
11701 }
11702
27c868c2
MW
11703 ASC_DBG_PRT_SCSI_HOST(2, shost);
11704
d361db48 11705 ret = scsi_add_host(shost, boardp->dev);
8dfb5379 11706 if (ret)
9a908c1a 11707 goto err_free_mem;
8dfb5379
MW
11708
11709 scsi_scan_host(shost);
d361db48 11710 return 0;
b2c16f58 11711
9a908c1a
HRK
11712 err_free_mem:
11713 if (ASC_NARROW_BOARD(boardp)) {
11714 if (asc_dvc_varp->overrun_dma)
11715 dma_unmap_single(boardp->dev, asc_dvc_varp->overrun_dma,
11716 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
11717 kfree(asc_dvc_varp->overrun_buf);
11718 } else
11719 advansys_wide_free_mem(boardp);
11720 err_free_irq:
d361db48 11721 free_irq(boardp->irq, shost);
b2c16f58 11722 err_free_dma:
30037818 11723#ifdef CONFIG_ISA
b2c16f58
MW
11724 if (shost->dma_channel != NO_ISA_DMA)
11725 free_dma(shost->dma_channel);
30037818 11726#endif
b2c16f58
MW
11727 err_unmap:
11728 if (boardp->ioremap_addr)
11729 iounmap(boardp->ioremap_addr);
11730 err_shost:
d361db48 11731 return ret;
27c868c2
MW
11732}
11733
27c868c2
MW
11734/*
11735 * advansys_release()
11736 *
11737 * Release resources allocated for a single AdvanSys adapter.
11738 */
11739static int advansys_release(struct Scsi_Host *shost)
11740{
d10fb2c7 11741 struct asc_board *board = shost_priv(shost);
b352f923 11742 ASC_DBG(1, "begin\n");
8dfb5379 11743 scsi_remove_host(shost);
d10fb2c7 11744 free_irq(board->irq, shost);
30037818 11745#ifdef CONFIG_ISA
27c868c2 11746 if (shost->dma_channel != NO_ISA_DMA) {
b352f923 11747 ASC_DBG(1, "free_dma()\n");
27c868c2
MW
11748 free_dma(shost->dma_channel);
11749 }
30037818 11750#endif
d10fb2c7
MW
11751 if (ASC_NARROW_BOARD(board)) {
11752 dma_unmap_single(board->dev,
11753 board->dvc_var.asc_dvc_var.overrun_dma,
11754 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
7d5d408c 11755 kfree(board->dvc_var.asc_dvc_var.overrun_buf);
d10fb2c7
MW
11756 } else {
11757 iounmap(board->ioremap_addr);
11758 advansys_wide_free_mem(board);
27c868c2 11759 }
8dfb5379 11760 scsi_host_put(shost);
b352f923 11761 ASC_DBG(1, "end\n");
27c868c2
MW
11762 return 0;
11763}
11764
95c9f162
MW
11765#define ASC_IOADR_TABLE_MAX_IX 11
11766
747d016e 11767static PortAddr _asc_def_iop_base[ASC_IOADR_TABLE_MAX_IX] = {
c304ec94
MW
11768 0x100, 0x0110, 0x120, 0x0130, 0x140, 0x0150, 0x0190,
11769 0x0210, 0x0230, 0x0250, 0x0330
11770};
11771
d361db48
MW
11772/*
11773 * The ISA IRQ number is found in bits 2 and 3 of the CfgLsw. It decodes as:
11774 * 00: 10
11775 * 01: 11
11776 * 10: 12
11777 * 11: 15
11778 */
6f039790 11779static unsigned int advansys_isa_irq_no(PortAddr iop_base)
d361db48
MW
11780{
11781 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11782 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x03) + 10;
11783 if (chip_irq == 13)
11784 chip_irq = 15;
11785 return chip_irq;
11786}
11787
6f039790 11788static int advansys_isa_probe(struct device *dev, unsigned int id)
c304ec94 11789{
d361db48 11790 int err = -ENODEV;
c304ec94
MW
11791 PortAddr iop_base = _asc_def_iop_base[id];
11792 struct Scsi_Host *shost;
d361db48 11793 struct asc_board *board;
c304ec94 11794
01fbfe0b 11795 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
b352f923 11796 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
c304ec94
MW
11797 return -ENODEV;
11798 }
b352f923 11799 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
c304ec94 11800 if (!AscFindSignature(iop_base))
d361db48 11801 goto release_region;
c304ec94 11802 if (!(AscGetChipVersion(iop_base, ASC_IS_ISA) & ASC_CHIP_VER_ISA_BIT))
d361db48 11803 goto release_region;
c304ec94 11804
d361db48
MW
11805 err = -ENOMEM;
11806 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
c304ec94 11807 if (!shost)
d361db48
MW
11808 goto release_region;
11809
d2411495 11810 board = shost_priv(shost);
d361db48
MW
11811 board->irq = advansys_isa_irq_no(iop_base);
11812 board->dev = dev;
9c17c62a 11813 board->shost = shost;
d361db48
MW
11814
11815 err = advansys_board_found(shost, iop_base, ASC_IS_ISA);
11816 if (err)
11817 goto free_host;
c304ec94
MW
11818
11819 dev_set_drvdata(dev, shost);
11820 return 0;
11821
d361db48
MW
11822 free_host:
11823 scsi_host_put(shost);
11824 release_region:
71f36115 11825 release_region(iop_base, ASC_IOADR_GAP);
d361db48 11826 return err;
c304ec94
MW
11827}
11828
6f039790 11829static int advansys_isa_remove(struct device *dev, unsigned int id)
c304ec94 11830{
71f36115 11831 int ioport = _asc_def_iop_base[id];
c304ec94 11832 advansys_release(dev_get_drvdata(dev));
71f36115 11833 release_region(ioport, ASC_IOADR_GAP);
c304ec94
MW
11834 return 0;
11835}
11836
11837static struct isa_driver advansys_isa_driver = {
11838 .probe = advansys_isa_probe,
6f039790 11839 .remove = advansys_isa_remove,
c304ec94
MW
11840 .driver = {
11841 .owner = THIS_MODULE,
01fbfe0b 11842 .name = DRV_NAME,
c304ec94
MW
11843 },
11844};
11845
d361db48
MW
11846/*
11847 * The VLB IRQ number is found in bits 2 to 4 of the CfgLsw. It decodes as:
11848 * 000: invalid
11849 * 001: 10
11850 * 010: 11
11851 * 011: 12
11852 * 100: invalid
11853 * 101: 14
11854 * 110: 15
11855 * 111: invalid
11856 */
6f039790 11857static unsigned int advansys_vlb_irq_no(PortAddr iop_base)
d361db48
MW
11858{
11859 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11860 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x07) + 9;
11861 if ((chip_irq < 10) || (chip_irq == 13) || (chip_irq > 15))
11862 return 0;
11863 return chip_irq;
11864}
11865
6f039790 11866static int advansys_vlb_probe(struct device *dev, unsigned int id)
c304ec94 11867{
d361db48 11868 int err = -ENODEV;
c304ec94
MW
11869 PortAddr iop_base = _asc_def_iop_base[id];
11870 struct Scsi_Host *shost;
d361db48 11871 struct asc_board *board;
c304ec94 11872
01fbfe0b 11873 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
b352f923 11874 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
c304ec94
MW
11875 return -ENODEV;
11876 }
b352f923 11877 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
c304ec94 11878 if (!AscFindSignature(iop_base))
d361db48 11879 goto release_region;
c304ec94
MW
11880 /*
11881 * I don't think this condition can actually happen, but the old
11882 * driver did it, and the chances of finding a VLB setup in 2007
11883 * to do testing with is slight to none.
11884 */
11885 if (AscGetChipVersion(iop_base, ASC_IS_VL) > ASC_CHIP_MAX_VER_VL)
d361db48 11886 goto release_region;
c304ec94 11887
d361db48
MW
11888 err = -ENOMEM;
11889 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
c304ec94 11890 if (!shost)
d361db48
MW
11891 goto release_region;
11892
d2411495 11893 board = shost_priv(shost);
d361db48
MW
11894 board->irq = advansys_vlb_irq_no(iop_base);
11895 board->dev = dev;
9c17c62a 11896 board->shost = shost;
d361db48
MW
11897
11898 err = advansys_board_found(shost, iop_base, ASC_IS_VL);
11899 if (err)
11900 goto free_host;
c304ec94
MW
11901
11902 dev_set_drvdata(dev, shost);
11903 return 0;
11904
d361db48
MW
11905 free_host:
11906 scsi_host_put(shost);
11907 release_region:
71f36115 11908 release_region(iop_base, ASC_IOADR_GAP);
c304ec94
MW
11909 return -ENODEV;
11910}
11911
11912static struct isa_driver advansys_vlb_driver = {
11913 .probe = advansys_vlb_probe,
6f039790 11914 .remove = advansys_isa_remove,
c304ec94
MW
11915 .driver = {
11916 .owner = THIS_MODULE,
b8e5152b 11917 .name = "advansys_vlb",
c304ec94
MW
11918 },
11919};
11920
6f039790 11921static struct eisa_device_id advansys_eisa_table[] = {
b09e05a7
MW
11922 { "ABP7401" },
11923 { "ABP7501" },
11924 { "" }
11925};
11926
11927MODULE_DEVICE_TABLE(eisa, advansys_eisa_table);
11928
11929/*
11930 * EISA is a little more tricky than PCI; each EISA device may have two
11931 * channels, and this driver is written to make each channel its own Scsi_Host
11932 */
11933struct eisa_scsi_data {
11934 struct Scsi_Host *host[2];
11935};
11936
d361db48
MW
11937/*
11938 * The EISA IRQ number is found in bits 8 to 10 of the CfgLsw. It decodes as:
11939 * 000: 10
11940 * 001: 11
11941 * 010: 12
11942 * 011: invalid
11943 * 100: 14
11944 * 101: 15
11945 * 110: invalid
11946 * 111: invalid
11947 */
6f039790 11948static unsigned int advansys_eisa_irq_no(struct eisa_device *edev)
d361db48
MW
11949{
11950 unsigned short cfg_lsw = inw(edev->base_addr + 0xc86);
11951 unsigned int chip_irq = ((cfg_lsw >> 8) & 0x07) + 10;
11952 if ((chip_irq == 13) || (chip_irq > 15))
11953 return 0;
11954 return chip_irq;
11955}
11956
6f039790 11957static int advansys_eisa_probe(struct device *dev)
b09e05a7 11958{
d361db48 11959 int i, ioport, irq = 0;
b09e05a7
MW
11960 int err;
11961 struct eisa_device *edev = to_eisa_device(dev);
11962 struct eisa_scsi_data *data;
11963
11964 err = -ENOMEM;
11965 data = kzalloc(sizeof(*data), GFP_KERNEL);
11966 if (!data)
11967 goto fail;
11968 ioport = edev->base_addr + 0xc30;
11969
11970 err = -ENODEV;
11971 for (i = 0; i < 2; i++, ioport += 0x20) {
d361db48
MW
11972 struct asc_board *board;
11973 struct Scsi_Host *shost;
01fbfe0b 11974 if (!request_region(ioport, ASC_IOADR_GAP, DRV_NAME)) {
71f36115
MW
11975 printk(KERN_WARNING "Region %x-%x busy\n", ioport,
11976 ioport + ASC_IOADR_GAP - 1);
11977 continue;
11978 }
11979 if (!AscFindSignature(ioport)) {
11980 release_region(ioport, ASC_IOADR_GAP);
b09e05a7 11981 continue;
71f36115
MW
11982 }
11983
b09e05a7
MW
11984 /*
11985 * I don't know why we need to do this for EISA chips, but
11986 * not for any others. It looks to be equivalent to
11987 * AscGetChipCfgMsw, but I may have overlooked something,
11988 * so I'm not converting it until I get an EISA board to
11989 * test with.
11990 */
11991 inw(ioport + 4);
d361db48
MW
11992
11993 if (!irq)
11994 irq = advansys_eisa_irq_no(edev);
11995
11996 err = -ENOMEM;
11997 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11998 if (!shost)
11999 goto release_region;
12000
d2411495 12001 board = shost_priv(shost);
d361db48
MW
12002 board->irq = irq;
12003 board->dev = dev;
9c17c62a 12004 board->shost = shost;
d361db48
MW
12005
12006 err = advansys_board_found(shost, ioport, ASC_IS_EISA);
12007 if (!err) {
12008 data->host[i] = shost;
12009 continue;
71f36115 12010 }
b09e05a7 12011
d361db48
MW
12012 scsi_host_put(shost);
12013 release_region:
12014 release_region(ioport, ASC_IOADR_GAP);
12015 break;
b09e05a7
MW
12016 }
12017
d361db48
MW
12018 if (err)
12019 goto free_data;
12020 dev_set_drvdata(dev, data);
12021 return 0;
12022
12023 free_data:
12024 kfree(data->host[0]);
12025 kfree(data->host[1]);
12026 kfree(data);
b09e05a7
MW
12027 fail:
12028 return err;
12029}
12030
6f039790 12031static int advansys_eisa_remove(struct device *dev)
b09e05a7
MW
12032{
12033 int i;
12034 struct eisa_scsi_data *data = dev_get_drvdata(dev);
12035
12036 for (i = 0; i < 2; i++) {
71f36115 12037 int ioport;
b09e05a7
MW
12038 struct Scsi_Host *shost = data->host[i];
12039 if (!shost)
12040 continue;
71f36115 12041 ioport = shost->io_port;
b09e05a7 12042 advansys_release(shost);
71f36115 12043 release_region(ioport, ASC_IOADR_GAP);
b09e05a7
MW
12044 }
12045
12046 kfree(data);
12047 return 0;
12048}
12049
12050static struct eisa_driver advansys_eisa_driver = {
12051 .id_table = advansys_eisa_table,
12052 .driver = {
01fbfe0b 12053 .name = DRV_NAME,
b09e05a7 12054 .probe = advansys_eisa_probe,
6f039790 12055 .remove = advansys_eisa_remove,
b09e05a7
MW
12056 }
12057};
12058
2672ea86 12059/* PCI Devices supported by this driver */
6f039790 12060static struct pci_device_id advansys_pci_tbl[] = {
27c868c2
MW
12061 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_1200A,
12062 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12063 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940,
12064 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12065 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940U,
12066 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12067 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940UW,
12068 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12069 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C0800_REV1,
12070 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12071 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C1600_REV1,
12072 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12073 {}
2672ea86 12074};
27c868c2 12075
2672ea86 12076MODULE_DEVICE_TABLE(pci, advansys_pci_tbl);
78e77d8b 12077
6f039790 12078static void advansys_set_latency(struct pci_dev *pdev)
9649af39
MW
12079{
12080 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
12081 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
12082 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0);
12083 } else {
12084 u8 latency;
12085 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
12086 if (latency < 0x20)
12087 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
12088 }
12089}
12090
6f039790
GKH
12091static int advansys_pci_probe(struct pci_dev *pdev,
12092 const struct pci_device_id *ent)
78e77d8b
MW
12093{
12094 int err, ioport;
12095 struct Scsi_Host *shost;
d361db48 12096 struct asc_board *board;
78e77d8b
MW
12097
12098 err = pci_enable_device(pdev);
12099 if (err)
12100 goto fail;
01fbfe0b 12101 err = pci_request_regions(pdev, DRV_NAME);
71f36115
MW
12102 if (err)
12103 goto disable_device;
9649af39
MW
12104 pci_set_master(pdev);
12105 advansys_set_latency(pdev);
78e77d8b 12106
d361db48 12107 err = -ENODEV;
78e77d8b 12108 if (pci_resource_len(pdev, 0) == 0)
d361db48 12109 goto release_region;
78e77d8b
MW
12110
12111 ioport = pci_resource_start(pdev, 0);
78e77d8b 12112
d361db48
MW
12113 err = -ENOMEM;
12114 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
78e77d8b 12115 if (!shost)
d361db48
MW
12116 goto release_region;
12117
d2411495 12118 board = shost_priv(shost);
d361db48
MW
12119 board->irq = pdev->irq;
12120 board->dev = &pdev->dev;
9c17c62a 12121 board->shost = shost;
d361db48
MW
12122
12123 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW ||
12124 pdev->device == PCI_DEVICE_ID_38C0800_REV1 ||
12125 pdev->device == PCI_DEVICE_ID_38C1600_REV1) {
12126 board->flags |= ASC_IS_WIDE_BOARD;
12127 }
12128
12129 err = advansys_board_found(shost, ioport, ASC_IS_PCI);
12130 if (err)
12131 goto free_host;
78e77d8b
MW
12132
12133 pci_set_drvdata(pdev, shost);
12134 return 0;
12135
d361db48
MW
12136 free_host:
12137 scsi_host_put(shost);
12138 release_region:
71f36115
MW
12139 pci_release_regions(pdev);
12140 disable_device:
78e77d8b
MW
12141 pci_disable_device(pdev);
12142 fail:
12143 return err;
12144}
12145
6f039790 12146static void advansys_pci_remove(struct pci_dev *pdev)
78e77d8b
MW
12147{
12148 advansys_release(pci_get_drvdata(pdev));
71f36115 12149 pci_release_regions(pdev);
78e77d8b
MW
12150 pci_disable_device(pdev);
12151}
12152
12153static struct pci_driver advansys_pci_driver = {
01fbfe0b 12154 .name = DRV_NAME,
78e77d8b
MW
12155 .id_table = advansys_pci_tbl,
12156 .probe = advansys_pci_probe,
6f039790 12157 .remove = advansys_pci_remove,
78e77d8b 12158};
8c6af9e1 12159
8dfb5379
MW
12160static int __init advansys_init(void)
12161{
c304ec94 12162 int error;
b09e05a7 12163
c304ec94
MW
12164 error = isa_register_driver(&advansys_isa_driver,
12165 ASC_IOADR_TABLE_MAX_IX);
78e77d8b
MW
12166 if (error)
12167 goto fail;
8dfb5379 12168
c304ec94
MW
12169 error = isa_register_driver(&advansys_vlb_driver,
12170 ASC_IOADR_TABLE_MAX_IX);
12171 if (error)
12172 goto unregister_isa;
12173
12174 error = eisa_driver_register(&advansys_eisa_driver);
12175 if (error)
12176 goto unregister_vlb;
12177
b09e05a7
MW
12178 error = pci_register_driver(&advansys_pci_driver);
12179 if (error)
12180 goto unregister_eisa;
12181
8dfb5379 12182 return 0;
78e77d8b 12183
b09e05a7
MW
12184 unregister_eisa:
12185 eisa_driver_unregister(&advansys_eisa_driver);
c304ec94
MW
12186 unregister_vlb:
12187 isa_unregister_driver(&advansys_vlb_driver);
12188 unregister_isa:
12189 isa_unregister_driver(&advansys_isa_driver);
78e77d8b 12190 fail:
78e77d8b 12191 return error;
8dfb5379
MW
12192}
12193
12194static void __exit advansys_exit(void)
12195{
78e77d8b 12196 pci_unregister_driver(&advansys_pci_driver);
b09e05a7 12197 eisa_driver_unregister(&advansys_eisa_driver);
c304ec94
MW
12198 isa_unregister_driver(&advansys_vlb_driver);
12199 isa_unregister_driver(&advansys_isa_driver);
8dfb5379
MW
12200}
12201
12202module_init(advansys_init);
12203module_exit(advansys_exit);
12204
8c6af9e1 12205MODULE_LICENSE("GPL");
989bb5f5
JSR
12206MODULE_FIRMWARE("advansys/mcode.bin");
12207MODULE_FIRMWARE("advansys/3550.bin");
12208MODULE_FIRMWARE("advansys/38C0800.bin");
12209MODULE_FIRMWARE("advansys/38C1600.bin");
This page took 1.604483 seconds and 5 git commands to generate.