blob: 4e52625f79293d41fe7d559f04ec77aa15493b62 [file] [log] [blame]
Matthew Wilcox01fbfe02007-09-09 08:56:40 -06001#define DRV_NAME "advansys"
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -04002#define ASC_VERSION "3.4" /* AdvanSys Driver Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
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.
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -04009 * Copyright (c) 2007 Matthew Wilcox <matthew@wil.cx>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All Rights Reserved.
11 *
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040012 * 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * As of March 8, 2000 Advanced System Products, Inc. (AdvanSys)
20 * changed its name to ConnectCom Solutions, Inc.
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040021 * On June 18, 2001 Initio Corp. acquired ConnectCom's SCSI assets
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060036#include <linux/isa.h>
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060037#include <linux/eisa.h>
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040038#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/spinlock.h>
40#include <linux/dma-mapping.h>
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +053041#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/dma.h>
45
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040046#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
Matthew Wilcox4bd6d7f2007-07-30 08:41:03 -060052/* FIXME:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 *
Matthew Wilcox4bd6d7f2007-07-30 08:41:03 -060054 * 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
Matthew Wilcox349d2c42007-09-09 08:56:34 -060067 * 6. Use scsi_transport_spi
68 * 7. advansys_info is not safe against multiple simultaneous callers
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040069 * 8. Add module_param to override ISA/VLB ioport array
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
71#warning this driver is still not properly converted to the DMA API
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Enable driver /proc statistics. */
74#define ADVANSYS_STATS
75
76/* Enable driver tracing. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040077#undef ADVANSYS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079typedef unsigned char uchar;
80
81#ifndef TRUE
82#define TRUE (1)
83#endif
84#ifndef FALSE
85#define FALSE (0)
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define ERR (-1)
89#define UW_ERR (uint)(0xFFFF)
90#define isodd_word(val) ((((uint)val) & (uint)0x0001) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dave Jones2672ea82006-08-02 17:11:49 -040092#define PCI_VENDOR_ID_ASP 0x10cd
93#define PCI_DEVICE_ID_ASP_1200A 0x1100
94#define PCI_DEVICE_ID_ASP_ABP940 0x1200
95#define PCI_DEVICE_ID_ASP_ABP940U 0x1300
96#define PCI_DEVICE_ID_ASP_ABP940UW 0x2300
97#define PCI_DEVICE_ID_38C0800_REV1 0x2500
98#define PCI_DEVICE_ID_38C1600_REV1 0x2700
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * Enable CC_VERY_LONG_SG_LIST to support up to 64K element SG lists.
102 * The SRB structure will have to be changed and the ASC_SRB2SCSIQ()
103 * macro re-defined to be able to obtain a ASC_SCSI_Q pointer from the
104 * SRB structure.
105 */
106#define CC_VERY_LONG_SG_LIST 0
107#define ASC_SRB2SCSIQ(srb_ptr) (srb_ptr)
108
Matthew Wilcox9d511a42007-10-02 21:55:42 -0400109#define PortAddr unsigned int /* port address size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define inp(port) inb(port)
111#define outp(port, byte) outb((byte), (port))
112
113#define inpw(port) inw(port)
114#define outpw(port, word) outw((word), (port))
115
116#define ASC_MAX_SG_QUEUE 7
117#define ASC_MAX_SG_LIST 255
118
119#define ASC_CS_TYPE unsigned short
120
121#define ASC_IS_ISA (0x0001)
122#define ASC_IS_ISAPNP (0x0081)
123#define ASC_IS_EISA (0x0002)
124#define ASC_IS_PCI (0x0004)
125#define ASC_IS_PCI_ULTRA (0x0104)
126#define ASC_IS_PCMCIA (0x0008)
127#define ASC_IS_MCA (0x0020)
128#define ASC_IS_VL (0x0040)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define ASC_IS_WIDESCSI_16 (0x0100)
130#define ASC_IS_WIDESCSI_32 (0x0200)
131#define ASC_IS_BIG_ENDIAN (0x8000)
Matthew Wilcox95c9f162007-09-09 08:56:39 -0600132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define ASC_CHIP_MIN_VER_VL (0x01)
134#define ASC_CHIP_MAX_VER_VL (0x07)
135#define ASC_CHIP_MIN_VER_PCI (0x09)
136#define ASC_CHIP_MAX_VER_PCI (0x0F)
137#define ASC_CHIP_VER_PCI_BIT (0x08)
138#define ASC_CHIP_MIN_VER_ISA (0x11)
139#define ASC_CHIP_MIN_VER_ISA_PNP (0x21)
140#define ASC_CHIP_MAX_VER_ISA (0x27)
141#define ASC_CHIP_VER_ISA_BIT (0x30)
142#define ASC_CHIP_VER_ISAPNP_BIT (0x20)
143#define ASC_CHIP_VER_ASYN_BUG (0x21)
144#define ASC_CHIP_VER_PCI 0x08
145#define ASC_CHIP_VER_PCI_ULTRA_3150 (ASC_CHIP_VER_PCI | 0x02)
146#define ASC_CHIP_VER_PCI_ULTRA_3050 (ASC_CHIP_VER_PCI | 0x03)
147#define ASC_CHIP_MIN_VER_EISA (0x41)
148#define ASC_CHIP_MAX_VER_EISA (0x47)
149#define ASC_CHIP_VER_EISA_BIT (0x40)
150#define ASC_CHIP_LATEST_VER_EISA ((ASC_CHIP_MIN_VER_EISA - 1) + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define ASC_MAX_VL_DMA_COUNT (0x07FFFFFFL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define ASC_MAX_PCI_DMA_COUNT (0xFFFFFFFFL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define ASC_MAX_ISA_DMA_COUNT (0x00FFFFFFL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155#define ASC_SCSI_ID_BITS 3
156#define ASC_SCSI_TIX_TYPE uchar
157#define ASC_ALL_DEVICE_BIT_SET 0xFF
158#define ASC_SCSI_BIT_ID_TYPE uchar
159#define ASC_MAX_TID 7
160#define ASC_MAX_LUN 7
161#define ASC_SCSI_WIDTH_BIT_SET 0xFF
162#define ASC_MAX_SENSE_LEN 32
163#define ASC_MIN_SENSE_LEN 14
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define ASC_SCSI_RESET_HOLD_TIME_US 60
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Matthew Wilcoxf05ec592007-09-09 08:56:36 -0600167 * Narrow boards only support 12-byte commands, while wide boards
168 * extend to 16-byte commands.
169 */
170#define ASC_MAX_CDB_LEN 12
171#define ADV_MAX_CDB_LEN 16
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define MS_SDTR_LEN 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#define MS_WDTR_LEN 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176#define ASC_SG_LIST_PER_Q 7
177#define QS_FREE 0x00
178#define QS_READY 0x01
179#define QS_DISC1 0x02
180#define QS_DISC2 0x04
181#define QS_BUSY 0x08
182#define QS_ABORTED 0x40
183#define QS_DONE 0x80
184#define QC_NO_CALLBACK 0x01
185#define QC_SG_SWAP_QUEUE 0x02
186#define QC_SG_HEAD 0x04
187#define QC_DATA_IN 0x08
188#define QC_DATA_OUT 0x10
189#define QC_URGENT 0x20
190#define QC_MSG_OUT 0x40
191#define QC_REQ_SENSE 0x80
192#define QCSG_SG_XFER_LIST 0x02
193#define QCSG_SG_XFER_MORE 0x04
194#define QCSG_SG_XFER_END 0x08
195#define QD_IN_PROGRESS 0x00
196#define QD_NO_ERROR 0x01
197#define QD_ABORTED_BY_HOST 0x02
198#define QD_WITH_ERROR 0x04
199#define QD_INVALID_REQUEST 0x80
200#define QD_INVALID_HOST_NUM 0x81
201#define QD_INVALID_DEVICE 0x82
202#define QD_ERR_INTERNAL 0xFF
203#define QHSTA_NO_ERROR 0x00
204#define QHSTA_M_SEL_TIMEOUT 0x11
205#define QHSTA_M_DATA_OVER_RUN 0x12
206#define QHSTA_M_DATA_UNDER_RUN 0x12
207#define QHSTA_M_UNEXPECTED_BUS_FREE 0x13
208#define QHSTA_M_BAD_BUS_PHASE_SEQ 0x14
209#define QHSTA_D_QDONE_SG_LIST_CORRUPTED 0x21
210#define QHSTA_D_ASC_DVC_ERROR_CODE_SET 0x22
211#define QHSTA_D_HOST_ABORT_FAILED 0x23
212#define QHSTA_D_EXE_SCSI_Q_FAILED 0x24
213#define QHSTA_D_EXE_SCSI_Q_BUSY_TIMEOUT 0x25
214#define QHSTA_D_ASPI_NO_BUF_POOL 0x26
215#define QHSTA_M_WTM_TIMEOUT 0x41
216#define QHSTA_M_BAD_CMPL_STATUS_IN 0x42
217#define QHSTA_M_NO_AUTO_REQ_SENSE 0x43
218#define QHSTA_M_AUTO_REQ_SENSE_FAIL 0x44
219#define QHSTA_M_TARGET_STATUS_BUSY 0x45
220#define QHSTA_M_BAD_TAG_CODE 0x46
221#define QHSTA_M_BAD_QUEUE_FULL_OR_BUSY 0x47
222#define QHSTA_M_HUNG_REQ_SCSI_BUS_RESET 0x48
223#define QHSTA_D_LRAM_CMP_ERROR 0x81
224#define QHSTA_M_MICRO_CODE_ERROR_HALT 0xA1
225#define ASC_FLAG_SCSIQ_REQ 0x01
226#define ASC_FLAG_BIOS_SCSIQ_REQ 0x02
227#define ASC_FLAG_BIOS_ASYNC_IO 0x04
228#define ASC_FLAG_SRB_LINEAR_ADDR 0x08
229#define ASC_FLAG_WIN16 0x10
230#define ASC_FLAG_WIN32 0x20
231#define ASC_FLAG_ISA_OVER_16MB 0x40
232#define ASC_FLAG_DOS_VM_CALLBACK 0x80
233#define ASC_TAG_FLAG_EXTRA_BYTES 0x10
234#define ASC_TAG_FLAG_DISABLE_DISCONNECT 0x04
235#define ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX 0x08
236#define ASC_TAG_FLAG_DISABLE_CHK_COND_INT_HOST 0x40
237#define ASC_SCSIQ_CPY_BEG 4
238#define ASC_SCSIQ_SGHD_CPY_BEG 2
239#define ASC_SCSIQ_B_FWD 0
240#define ASC_SCSIQ_B_BWD 1
241#define ASC_SCSIQ_B_STATUS 2
242#define ASC_SCSIQ_B_QNO 3
243#define ASC_SCSIQ_B_CNTL 4
244#define ASC_SCSIQ_B_SG_QUEUE_CNT 5
245#define ASC_SCSIQ_D_DATA_ADDR 8
246#define ASC_SCSIQ_D_DATA_CNT 12
247#define ASC_SCSIQ_B_SENSE_LEN 20
248#define ASC_SCSIQ_DONE_INFO_BEG 22
249#define ASC_SCSIQ_D_SRBPTR 22
250#define ASC_SCSIQ_B_TARGET_IX 26
251#define ASC_SCSIQ_B_CDB_LEN 28
252#define ASC_SCSIQ_B_TAG_CODE 29
253#define ASC_SCSIQ_W_VM_ID 30
254#define ASC_SCSIQ_DONE_STATUS 32
255#define ASC_SCSIQ_HOST_STATUS 33
256#define ASC_SCSIQ_SCSI_STATUS 34
257#define ASC_SCSIQ_CDB_BEG 36
258#define ASC_SCSIQ_DW_REMAIN_XFER_ADDR 56
259#define ASC_SCSIQ_DW_REMAIN_XFER_CNT 60
260#define ASC_SCSIQ_B_FIRST_SG_WK_QP 48
261#define ASC_SCSIQ_B_SG_WK_QP 49
262#define ASC_SCSIQ_B_SG_WK_IX 50
263#define ASC_SCSIQ_W_ALT_DC1 52
264#define ASC_SCSIQ_B_LIST_CNT 6
265#define ASC_SCSIQ_B_CUR_LIST_CNT 7
266#define ASC_SGQ_B_SG_CNTL 4
267#define ASC_SGQ_B_SG_HEAD_QP 5
268#define ASC_SGQ_B_SG_LIST_CNT 6
269#define ASC_SGQ_B_SG_CUR_LIST_CNT 7
270#define ASC_SGQ_LIST_BEG 8
271#define ASC_DEF_SCSI1_QNG 4
272#define ASC_MAX_SCSI1_QNG 4
273#define ASC_DEF_SCSI2_QNG 16
274#define ASC_MAX_SCSI2_QNG 32
275#define ASC_TAG_CODE_MASK 0x23
276#define ASC_STOP_REQ_RISC_STOP 0x01
277#define ASC_STOP_ACK_RISC_STOP 0x03
278#define ASC_STOP_CLEAN_UP_BUSY_Q 0x10
279#define ASC_STOP_CLEAN_UP_DISC_Q 0x20
280#define ASC_STOP_HOST_REQ_RISC_HALT 0x40
281#define ASC_TIDLUN_TO_IX(tid, lun) (ASC_SCSI_TIX_TYPE)((tid) + ((lun)<<ASC_SCSI_ID_BITS))
282#define ASC_TID_TO_TARGET_ID(tid) (ASC_SCSI_BIT_ID_TYPE)(0x01 << (tid))
283#define ASC_TIX_TO_TARGET_ID(tix) (0x01 << ((tix) & ASC_MAX_TID))
284#define ASC_TIX_TO_TID(tix) ((tix) & ASC_MAX_TID)
285#define ASC_TID_TO_TIX(tid) ((tid) & ASC_MAX_TID)
286#define ASC_TIX_TO_LUN(tix) (((tix) >> ASC_SCSI_ID_BITS) & ASC_MAX_LUN)
287#define ASC_QNO_TO_QADDR(q_no) ((ASC_QADR_BEG)+((int)(q_no) << 6))
288
289typedef struct asc_scsiq_1 {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400290 uchar status;
291 uchar q_no;
292 uchar cntl;
293 uchar sg_queue_cnt;
294 uchar target_id;
295 uchar target_lun;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200296 __le32 data_addr;
297 __le32 data_cnt;
298 __le32 sense_addr;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400299 uchar sense_len;
300 uchar extra_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301} ASC_SCSIQ_1;
302
303typedef struct asc_scsiq_2 {
Hannes Reinecke9c17c622015-04-24 13:18:21 +0200304 u32 srb_tag;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400305 uchar target_ix;
306 uchar flag;
307 uchar cdb_len;
308 uchar tag_code;
309 ushort vm_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310} ASC_SCSIQ_2;
311
312typedef struct asc_scsiq_3 {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400313 uchar done_stat;
314 uchar host_stat;
315 uchar scsi_stat;
316 uchar scsi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317} ASC_SCSIQ_3;
318
319typedef struct asc_scsiq_4 {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400320 uchar cdb[ASC_MAX_CDB_LEN];
321 uchar y_first_sg_list_qp;
322 uchar y_working_sg_qp;
323 uchar y_working_sg_ix;
324 uchar y_res;
325 ushort x_req_count;
326 ushort x_reconnect_rtn;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200327 __le32 x_saved_data_addr;
328 __le32 x_saved_data_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329} ASC_SCSIQ_4;
330
331typedef struct asc_q_done_info {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400332 ASC_SCSIQ_2 d2;
333 ASC_SCSIQ_3 d3;
334 uchar q_status;
335 uchar q_no;
336 uchar cntl;
337 uchar sense_len;
338 uchar extra_bytes;
339 uchar res;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200340 u32 remain_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341} ASC_QDONE_INFO;
342
343typedef struct asc_sg_list {
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200344 __le32 addr;
345 __le32 bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346} ASC_SG_LIST;
347
348typedef struct asc_sg_head {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400349 ushort entry_cnt;
350 ushort queue_cnt;
351 ushort entry_to_copy;
352 ushort res;
Matthew Wilcox05848b62007-10-02 21:55:25 -0400353 ASC_SG_LIST sg_list[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354} ASC_SG_HEAD;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356typedef struct asc_scsi_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400357 ASC_SCSIQ_1 q1;
358 ASC_SCSIQ_2 q2;
359 uchar *cdbptr;
360 ASC_SG_HEAD *sg_head;
361 ushort remain_sg_entry_cnt;
362 ushort next_sg_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363} ASC_SCSI_Q;
364
365typedef struct asc_scsi_req_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400366 ASC_SCSIQ_1 r1;
367 ASC_SCSIQ_2 r2;
368 uchar *cdbptr;
369 ASC_SG_HEAD *sg_head;
370 uchar *sense_ptr;
371 ASC_SCSIQ_3 r3;
372 uchar cdb[ASC_MAX_CDB_LEN];
373 uchar sense[ASC_MIN_SENSE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374} ASC_SCSI_REQ_Q;
375
376typedef struct asc_scsi_bios_req_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400377 ASC_SCSIQ_1 r1;
378 ASC_SCSIQ_2 r2;
379 uchar *cdbptr;
380 ASC_SG_HEAD *sg_head;
381 uchar *sense_ptr;
382 ASC_SCSIQ_3 r3;
383 uchar cdb[ASC_MAX_CDB_LEN];
384 uchar sense[ASC_MIN_SENSE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385} ASC_SCSI_BIOS_REQ_Q;
386
387typedef struct asc_risc_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400388 uchar fwd;
389 uchar bwd;
390 ASC_SCSIQ_1 i1;
391 ASC_SCSIQ_2 i2;
392 ASC_SCSIQ_3 i3;
393 ASC_SCSIQ_4 i4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394} ASC_RISC_Q;
395
396typedef struct asc_sg_list_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400397 uchar seq_no;
398 uchar q_no;
399 uchar cntl;
400 uchar sg_head_qp;
401 uchar sg_list_cnt;
402 uchar sg_cur_list_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403} ASC_SG_LIST_Q;
404
405typedef struct asc_risc_sg_list_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400406 uchar fwd;
407 uchar bwd;
408 ASC_SG_LIST_Q sg;
409 ASC_SG_LIST sg_list[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410} ASC_RISC_SG_LIST_Q;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#define ASCQ_ERR_Q_STATUS 0x0D
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#define ASCQ_ERR_CUR_QNG 0x17
414#define ASCQ_ERR_SG_Q_LINKS 0x18
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#define ASCQ_ERR_ISR_RE_ENTRY 0x1A
416#define ASCQ_ERR_CRITICAL_RE_ENTRY 0x1B
417#define ASCQ_ERR_ISR_ON_CRITICAL 0x1C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/*
420 * Warning code values are set in ASC_DVC_VAR 'warn_code'.
421 */
422#define ASC_WARN_NO_ERROR 0x0000
423#define ASC_WARN_IO_PORT_ROTATE 0x0001
424#define ASC_WARN_EEPROM_CHKSUM 0x0002
425#define ASC_WARN_IRQ_MODIFIED 0x0004
426#define ASC_WARN_AUTO_CONFIG 0x0008
427#define ASC_WARN_CMD_QNG_CONFLICT 0x0010
428#define ASC_WARN_EEPROM_RECOVER 0x0020
429#define ASC_WARN_CFG_MSW_RECOVER 0x0040
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Matthew Wilcox720349a2007-10-02 21:55:30 -0400432 * Error code values are set in {ASC/ADV}_DVC_VAR 'err_code'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
Matthew Wilcox720349a2007-10-02 21:55:30 -0400434#define ASC_IERR_NO_CARRIER 0x0001 /* No more carrier memory */
435#define ASC_IERR_MCODE_CHKSUM 0x0002 /* micro code check sum error */
436#define ASC_IERR_SET_PC_ADDR 0x0004
437#define ASC_IERR_START_STOP_CHIP 0x0008 /* start/stop chip failed */
438#define ASC_IERR_ILLEGAL_CONNECTION 0x0010 /* Illegal cable connection */
439#define ASC_IERR_SINGLE_END_DEVICE 0x0020 /* SE device on DIFF bus */
440#define ASC_IERR_REVERSED_CABLE 0x0040 /* Narrow flat cable reversed */
441#define ASC_IERR_SET_SCSI_ID 0x0080 /* set SCSI ID failed */
442#define ASC_IERR_HVD_DEVICE 0x0100 /* HVD device on LVD port */
443#define ASC_IERR_BAD_SIGNATURE 0x0200 /* signature not found */
444#define ASC_IERR_NO_BUS_TYPE 0x0400
445#define ASC_IERR_BIST_PRE_TEST 0x0800 /* BIST pre-test error */
446#define ASC_IERR_BIST_RAM_TEST 0x1000 /* BIST RAM test error */
447#define ASC_IERR_BAD_CHIPTYPE 0x2000 /* Invalid chip_type setting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#define ASC_DEF_MAX_TOTAL_QNG (0xF0)
450#define ASC_MIN_TAG_Q_PER_DVC (0x04)
Matthew Wilcox95c9f162007-09-09 08:56:39 -0600451#define ASC_MIN_FREE_Q (0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#define ASC_MIN_TOTAL_QNG ((ASC_MAX_SG_QUEUE)+(ASC_MIN_FREE_Q))
453#define ASC_MAX_TOTAL_QNG 240
454#define ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG 16
455#define ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG 8
456#define ASC_MAX_PCI_INRAM_TOTAL_QNG 20
457#define ASC_MAX_INRAM_TAG_QNG 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#define ASC_IOADR_GAP 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#define ASC_SYN_MAX_OFFSET 0x0F
460#define ASC_DEF_SDTR_OFFSET 0x0F
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#define ASC_SDTR_ULTRA_PCI_10MB_INDEX 0x02
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -0400462#define ASYN_SDTR_DATA_FIX_PCI_REV_AB 0x41
463
464/* The narrow chip only supports a limited selection of transfer rates.
465 * These are encoded in the range 0..7 or 0..15 depending whether the chip
466 * is Ultra-capable or not. These tables let us convert from one to the other.
467 */
468static const unsigned char asc_syn_xfer_period[8] = {
469 25, 30, 35, 40, 50, 60, 70, 85
470};
471
472static const unsigned char asc_syn_ultra_xfer_period[16] = {
473 12, 19, 25, 32, 38, 44, 50, 57, 63, 69, 75, 82, 88, 94, 100, 107
474};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476typedef struct ext_msg {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400477 uchar msg_type;
478 uchar msg_len;
479 uchar msg_req;
480 union {
481 struct {
482 uchar sdtr_xfer_period;
483 uchar sdtr_req_ack_offset;
484 } sdtr;
485 struct {
486 uchar wdtr_width;
487 } wdtr;
488 struct {
489 uchar mdp_b3;
490 uchar mdp_b2;
491 uchar mdp_b1;
492 uchar mdp_b0;
493 } mdp;
494 } u_ext_msg;
495 uchar res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496} EXT_MSG;
497
498#define xfer_period u_ext_msg.sdtr.sdtr_xfer_period
499#define req_ack_offset u_ext_msg.sdtr.sdtr_req_ack_offset
500#define wdtr_width u_ext_msg.wdtr.wdtr_width
501#define mdp_b3 u_ext_msg.mdp_b3
502#define mdp_b2 u_ext_msg.mdp_b2
503#define mdp_b1 u_ext_msg.mdp_b1
504#define mdp_b0 u_ext_msg.mdp_b0
505
506typedef struct asc_dvc_cfg {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400507 ASC_SCSI_BIT_ID_TYPE can_tagged_qng;
508 ASC_SCSI_BIT_ID_TYPE cmd_qng_enabled;
509 ASC_SCSI_BIT_ID_TYPE disc_enable;
510 ASC_SCSI_BIT_ID_TYPE sdtr_enable;
511 uchar chip_scsi_id;
512 uchar isa_dma_speed;
513 uchar isa_dma_channel;
514 uchar chip_version;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400515 ushort mcode_date;
516 ushort mcode_version;
517 uchar max_tag_qng[ASC_MAX_TID + 1];
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400518 uchar sdtr_period_offset[ASC_MAX_TID + 1];
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400519 uchar adapter_info[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520} ASC_DVC_CFG;
521
522#define ASC_DEF_DVC_CNTL 0xFFFF
523#define ASC_DEF_CHIP_SCSI_ID 7
524#define ASC_DEF_ISA_DMA_SPEED 4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#define ASC_INIT_STATE_BEG_GET_CFG 0x0001
526#define ASC_INIT_STATE_END_GET_CFG 0x0002
527#define ASC_INIT_STATE_BEG_SET_CFG 0x0004
528#define ASC_INIT_STATE_END_SET_CFG 0x0008
529#define ASC_INIT_STATE_BEG_LOAD_MC 0x0010
530#define ASC_INIT_STATE_END_LOAD_MC 0x0020
531#define ASC_INIT_STATE_BEG_INQUIRY 0x0040
532#define ASC_INIT_STATE_END_INQUIRY 0x0080
533#define ASC_INIT_RESET_SCSI_DONE 0x0100
534#define ASC_INIT_STATE_WITHOUT_EEP 0x8000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#define ASC_BUG_FIX_IF_NOT_DWB 0x0001
536#define ASC_BUG_FIX_ASYN_USE_SYN 0x0002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#define ASC_MIN_TAGGED_CMD 7
538#define ASC_MAX_SCSI_RESET_WAIT 30
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -0400539#define ASC_OVERRUN_BSIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400541struct asc_dvc_var; /* Forward Declaration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543typedef struct asc_dvc_var {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400544 PortAddr iop_base;
545 ushort err_code;
546 ushort dvc_cntl;
547 ushort bug_fix_cntl;
548 ushort bus_type;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400549 ASC_SCSI_BIT_ID_TYPE init_sdtr;
550 ASC_SCSI_BIT_ID_TYPE sdtr_done;
551 ASC_SCSI_BIT_ID_TYPE use_tagged_qng;
552 ASC_SCSI_BIT_ID_TYPE unit_not_ready;
553 ASC_SCSI_BIT_ID_TYPE queue_full_or_busy;
554 ASC_SCSI_BIT_ID_TYPE start_motor;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +0900555 uchar *overrun_buf;
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -0400556 dma_addr_t overrun_dma;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400557 uchar scsi_reset_wait;
558 uchar chip_no;
559 char is_in_int;
560 uchar max_total_qng;
561 uchar cur_total_qng;
562 uchar in_critical_cnt;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400563 uchar last_q_shortage;
564 ushort init_state;
565 uchar cur_dvc_qng[ASC_MAX_TID + 1];
566 uchar max_dvc_qng[ASC_MAX_TID + 1];
567 ASC_SCSI_Q *scsiq_busy_head[ASC_MAX_TID + 1];
568 ASC_SCSI_Q *scsiq_busy_tail[ASC_MAX_TID + 1];
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -0400569 const uchar *sdtr_period_tbl;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400570 ASC_DVC_CFG *cfg;
571 ASC_SCSI_BIT_ID_TYPE pci_fix_asyn_xfer_always;
572 char redo_scam;
573 ushort res2;
574 uchar dos_int13_table[ASC_MAX_TID + 1];
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200575 unsigned int max_dma_count;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400576 ASC_SCSI_BIT_ID_TYPE no_scam;
577 ASC_SCSI_BIT_ID_TYPE pci_fix_asyn_xfer;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -0400578 uchar min_sdtr_index;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400579 uchar max_sdtr_index;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400580 struct asc_board *drv_ptr;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200581 unsigned int uc_break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582} ASC_DVC_VAR;
583
584typedef struct asc_dvc_inq_info {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400585 uchar type[ASC_MAX_TID + 1][ASC_MAX_LUN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586} ASC_DVC_INQ_INFO;
587
588typedef struct asc_cap_info {
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200589 u32 lba;
590 u32 blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591} ASC_CAP_INFO;
592
593typedef struct asc_cap_info_array {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400594 ASC_CAP_INFO cap_info[ASC_MAX_TID + 1][ASC_MAX_LUN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595} ASC_CAP_INFO_ARRAY;
596
597#define ASC_MCNTL_NO_SEL_TIMEOUT (ushort)0x0001
598#define ASC_MCNTL_NULL_TARGET (ushort)0x0002
599#define ASC_CNTL_INITIATOR (ushort)0x0001
600#define ASC_CNTL_BIOS_GT_1GB (ushort)0x0002
601#define ASC_CNTL_BIOS_GT_2_DISK (ushort)0x0004
602#define ASC_CNTL_BIOS_REMOVABLE (ushort)0x0008
603#define ASC_CNTL_NO_SCAM (ushort)0x0010
604#define ASC_CNTL_INT_MULTI_Q (ushort)0x0080
605#define ASC_CNTL_NO_LUN_SUPPORT (ushort)0x0040
606#define ASC_CNTL_NO_VERIFY_COPY (ushort)0x0100
607#define ASC_CNTL_RESET_SCSI (ushort)0x0200
608#define ASC_CNTL_INIT_INQUIRY (ushort)0x0400
609#define ASC_CNTL_INIT_VERBOSE (ushort)0x0800
610#define ASC_CNTL_SCSI_PARITY (ushort)0x1000
611#define ASC_CNTL_BURST_MODE (ushort)0x2000
612#define ASC_CNTL_SDTR_ENABLE_ULTRA (ushort)0x4000
613#define ASC_EEP_DVC_CFG_BEG_VL 2
614#define ASC_EEP_MAX_DVC_ADDR_VL 15
615#define ASC_EEP_DVC_CFG_BEG 32
616#define ASC_EEP_MAX_DVC_ADDR 45
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#define ASC_EEP_MAX_RETRY 20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619/*
620 * These macros keep the chip SCSI id and ISA DMA speed
621 * bitfields in board order. C bitfields aren't portable
622 * between big and little-endian platforms so they are
623 * not used.
624 */
625
626#define ASC_EEP_GET_CHIP_ID(cfg) ((cfg)->id_speed & 0x0f)
627#define ASC_EEP_GET_DMA_SPD(cfg) (((cfg)->id_speed & 0xf0) >> 4)
628#define ASC_EEP_SET_CHIP_ID(cfg, sid) \
629 ((cfg)->id_speed = ((cfg)->id_speed & 0xf0) | ((sid) & ASC_MAX_TID))
630#define ASC_EEP_SET_DMA_SPD(cfg, spd) \
631 ((cfg)->id_speed = ((cfg)->id_speed & 0x0f) | ((spd) & 0x0f) << 4)
632
633typedef struct asceep_config {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400634 ushort cfg_lsw;
635 ushort cfg_msw;
636 uchar init_sdtr;
637 uchar disc_enable;
638 uchar use_cmd_qng;
639 uchar start_motor;
640 uchar max_total_qng;
641 uchar max_tag_qng;
642 uchar bios_scan;
643 uchar power_up_wait;
644 uchar no_scam;
645 uchar id_speed; /* low order 4 bits is chip scsi id */
646 /* high order 4 bits is isa dma speed */
647 uchar dos_int13_table[ASC_MAX_TID + 1];
648 uchar adapter_info[6];
649 ushort cntl;
650 ushort chksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651} ASCEEP_CONFIG;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#define ASC_EEP_CMD_READ 0x80
654#define ASC_EEP_CMD_WRITE 0x40
655#define ASC_EEP_CMD_WRITE_ABLE 0x30
656#define ASC_EEP_CMD_WRITE_DISABLE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#define ASCV_MSGOUT_BEG 0x0000
658#define ASCV_MSGOUT_SDTR_PERIOD (ASCV_MSGOUT_BEG+3)
659#define ASCV_MSGOUT_SDTR_OFFSET (ASCV_MSGOUT_BEG+4)
660#define ASCV_BREAK_SAVED_CODE (ushort)0x0006
661#define ASCV_MSGIN_BEG (ASCV_MSGOUT_BEG+8)
662#define ASCV_MSGIN_SDTR_PERIOD (ASCV_MSGIN_BEG+3)
663#define ASCV_MSGIN_SDTR_OFFSET (ASCV_MSGIN_BEG+4)
664#define ASCV_SDTR_DATA_BEG (ASCV_MSGIN_BEG+8)
665#define ASCV_SDTR_DONE_BEG (ASCV_SDTR_DATA_BEG+8)
666#define ASCV_MAX_DVC_QNG_BEG (ushort)0x0020
667#define ASCV_BREAK_ADDR (ushort)0x0028
668#define ASCV_BREAK_NOTIFY_COUNT (ushort)0x002A
669#define ASCV_BREAK_CONTROL (ushort)0x002C
670#define ASCV_BREAK_HIT_COUNT (ushort)0x002E
671
672#define ASCV_ASCDVC_ERR_CODE_W (ushort)0x0030
673#define ASCV_MCODE_CHKSUM_W (ushort)0x0032
674#define ASCV_MCODE_SIZE_W (ushort)0x0034
675#define ASCV_STOP_CODE_B (ushort)0x0036
676#define ASCV_DVC_ERR_CODE_B (ushort)0x0037
677#define ASCV_OVERRUN_PADDR_D (ushort)0x0038
678#define ASCV_OVERRUN_BSIZE_D (ushort)0x003C
679#define ASCV_HALTCODE_W (ushort)0x0040
680#define ASCV_CHKSUM_W (ushort)0x0042
681#define ASCV_MC_DATE_W (ushort)0x0044
682#define ASCV_MC_VER_W (ushort)0x0046
683#define ASCV_NEXTRDY_B (ushort)0x0048
684#define ASCV_DONENEXT_B (ushort)0x0049
685#define ASCV_USE_TAGGED_QNG_B (ushort)0x004A
686#define ASCV_SCSIBUSY_B (ushort)0x004B
687#define ASCV_Q_DONE_IN_PROGRESS_B (ushort)0x004C
688#define ASCV_CURCDB_B (ushort)0x004D
689#define ASCV_RCLUN_B (ushort)0x004E
690#define ASCV_BUSY_QHEAD_B (ushort)0x004F
691#define ASCV_DISC1_QHEAD_B (ushort)0x0050
692#define ASCV_DISC_ENABLE_B (ushort)0x0052
693#define ASCV_CAN_TAGGED_QNG_B (ushort)0x0053
694#define ASCV_HOSTSCSI_ID_B (ushort)0x0055
695#define ASCV_MCODE_CNTL_B (ushort)0x0056
696#define ASCV_NULL_TARGET_B (ushort)0x0057
697#define ASCV_FREE_Q_HEAD_W (ushort)0x0058
698#define ASCV_DONE_Q_TAIL_W (ushort)0x005A
699#define ASCV_FREE_Q_HEAD_B (ushort)(ASCV_FREE_Q_HEAD_W+1)
700#define ASCV_DONE_Q_TAIL_B (ushort)(ASCV_DONE_Q_TAIL_W+1)
701#define ASCV_HOST_FLAG_B (ushort)0x005D
702#define ASCV_TOTAL_READY_Q_B (ushort)0x0064
703#define ASCV_VER_SERIAL_B (ushort)0x0065
704#define ASCV_HALTCODE_SAVED_W (ushort)0x0066
705#define ASCV_WTM_FLAG_B (ushort)0x0068
706#define ASCV_RISC_FLAG_B (ushort)0x006A
707#define ASCV_REQ_SG_LIST_QP (ushort)0x006B
708#define ASC_HOST_FLAG_IN_ISR 0x01
709#define ASC_HOST_FLAG_ACK_INT 0x02
710#define ASC_RISC_FLAG_GEN_INT 0x01
711#define ASC_RISC_FLAG_REQ_SG_LIST 0x02
712#define IOP_CTRL (0x0F)
713#define IOP_STATUS (0x0E)
714#define IOP_INT_ACK IOP_STATUS
715#define IOP_REG_IFC (0x0D)
716#define IOP_SYN_OFFSET (0x0B)
717#define IOP_EXTRA_CONTROL (0x0D)
718#define IOP_REG_PC (0x0C)
719#define IOP_RAM_ADDR (0x0A)
720#define IOP_RAM_DATA (0x08)
721#define IOP_EEP_DATA (0x06)
722#define IOP_EEP_CMD (0x07)
723#define IOP_VERSION (0x03)
724#define IOP_CONFIG_HIGH (0x04)
725#define IOP_CONFIG_LOW (0x02)
726#define IOP_SIG_BYTE (0x01)
727#define IOP_SIG_WORD (0x00)
728#define IOP_REG_DC1 (0x0E)
729#define IOP_REG_DC0 (0x0C)
730#define IOP_REG_SB (0x0B)
731#define IOP_REG_DA1 (0x0A)
732#define IOP_REG_DA0 (0x08)
733#define IOP_REG_SC (0x09)
734#define IOP_DMA_SPEED (0x07)
735#define IOP_REG_FLAG (0x07)
736#define IOP_FIFO_H (0x06)
737#define IOP_FIFO_L (0x04)
738#define IOP_REG_ID (0x05)
739#define IOP_REG_QP (0x03)
740#define IOP_REG_IH (0x02)
741#define IOP_REG_IX (0x01)
742#define IOP_REG_AX (0x00)
743#define IFC_REG_LOCK (0x00)
744#define IFC_REG_UNLOCK (0x09)
745#define IFC_WR_EN_FILTER (0x10)
746#define IFC_RD_NO_EEPROM (0x10)
747#define IFC_SLEW_RATE (0x20)
748#define IFC_ACT_NEG (0x40)
749#define IFC_INP_FILTER (0x80)
750#define IFC_INIT_DEFAULT (IFC_ACT_NEG | IFC_REG_UNLOCK)
751#define SC_SEL (uchar)(0x80)
752#define SC_BSY (uchar)(0x40)
753#define SC_ACK (uchar)(0x20)
754#define SC_REQ (uchar)(0x10)
755#define SC_ATN (uchar)(0x08)
756#define SC_IO (uchar)(0x04)
757#define SC_CD (uchar)(0x02)
758#define SC_MSG (uchar)(0x01)
759#define SEC_SCSI_CTL (uchar)(0x80)
760#define SEC_ACTIVE_NEGATE (uchar)(0x40)
761#define SEC_SLEW_RATE (uchar)(0x20)
762#define SEC_ENABLE_FILTER (uchar)(0x10)
763#define ASC_HALT_EXTMSG_IN (ushort)0x8000
764#define ASC_HALT_CHK_CONDITION (ushort)0x8100
765#define ASC_HALT_SS_QUEUE_FULL (ushort)0x8200
766#define ASC_HALT_DISABLE_ASYN_USE_SYN_FIX (ushort)0x8300
767#define ASC_HALT_ENABLE_ASYN_USE_SYN_FIX (ushort)0x8400
768#define ASC_HALT_SDTR_REJECTED (ushort)0x4000
769#define ASC_HALT_HOST_COPY_SG_LIST_TO_RISC ( ushort )0x2000
770#define ASC_MAX_QNO 0xF8
771#define ASC_DATA_SEC_BEG (ushort)0x0080
772#define ASC_DATA_SEC_END (ushort)0x0080
773#define ASC_CODE_SEC_BEG (ushort)0x0080
774#define ASC_CODE_SEC_END (ushort)0x0080
775#define ASC_QADR_BEG (0x4000)
776#define ASC_QADR_USED (ushort)(ASC_MAX_QNO * 64)
777#define ASC_QADR_END (ushort)0x7FFF
778#define ASC_QLAST_ADR (ushort)0x7FC0
779#define ASC_QBLK_SIZE 0x40
780#define ASC_BIOS_DATA_QBEG 0xF8
781#define ASC_MIN_ACTIVE_QNO 0x01
782#define ASC_QLINK_END 0xFF
783#define ASC_EEPROM_WORDS 0x10
784#define ASC_MAX_MGS_LEN 0x10
785#define ASC_BIOS_ADDR_DEF 0xDC00
786#define ASC_BIOS_SIZE 0x3800
787#define ASC_BIOS_RAM_OFF 0x3800
788#define ASC_BIOS_RAM_SIZE 0x800
789#define ASC_BIOS_MIN_ADDR 0xC000
790#define ASC_BIOS_MAX_ADDR 0xEC00
791#define ASC_BIOS_BANK_SIZE 0x0400
792#define ASC_MCODE_START_ADDR 0x0080
793#define ASC_CFG0_HOST_INT_ON 0x0020
794#define ASC_CFG0_BIOS_ON 0x0040
795#define ASC_CFG0_VERA_BURST_ON 0x0080
796#define ASC_CFG0_SCSI_PARITY_ON 0x0800
797#define ASC_CFG1_SCSI_TARGET_ON 0x0080
798#define ASC_CFG1_LRAM_8BITS_ON 0x0800
799#define ASC_CFG_MSW_CLR_MASK 0x3080
800#define CSW_TEST1 (ASC_CS_TYPE)0x8000
801#define CSW_AUTO_CONFIG (ASC_CS_TYPE)0x4000
802#define CSW_RESERVED1 (ASC_CS_TYPE)0x2000
803#define CSW_IRQ_WRITTEN (ASC_CS_TYPE)0x1000
804#define CSW_33MHZ_SELECTED (ASC_CS_TYPE)0x0800
805#define CSW_TEST2 (ASC_CS_TYPE)0x0400
806#define CSW_TEST3 (ASC_CS_TYPE)0x0200
807#define CSW_RESERVED2 (ASC_CS_TYPE)0x0100
808#define CSW_DMA_DONE (ASC_CS_TYPE)0x0080
809#define CSW_FIFO_RDY (ASC_CS_TYPE)0x0040
810#define CSW_EEP_READ_DONE (ASC_CS_TYPE)0x0020
811#define CSW_HALTED (ASC_CS_TYPE)0x0010
812#define CSW_SCSI_RESET_ACTIVE (ASC_CS_TYPE)0x0008
813#define CSW_PARITY_ERR (ASC_CS_TYPE)0x0004
814#define CSW_SCSI_RESET_LATCH (ASC_CS_TYPE)0x0002
815#define CSW_INT_PENDING (ASC_CS_TYPE)0x0001
816#define CIW_CLR_SCSI_RESET_INT (ASC_CS_TYPE)0x1000
817#define CIW_INT_ACK (ASC_CS_TYPE)0x0100
818#define CIW_TEST1 (ASC_CS_TYPE)0x0200
819#define CIW_TEST2 (ASC_CS_TYPE)0x0400
820#define CIW_SEL_33MHZ (ASC_CS_TYPE)0x0800
821#define CIW_IRQ_ACT (ASC_CS_TYPE)0x1000
822#define CC_CHIP_RESET (uchar)0x80
823#define CC_SCSI_RESET (uchar)0x40
824#define CC_HALT (uchar)0x20
825#define CC_SINGLE_STEP (uchar)0x10
826#define CC_DMA_ABLE (uchar)0x08
827#define CC_TEST (uchar)0x04
828#define CC_BANK_ONE (uchar)0x02
829#define CC_DIAG (uchar)0x01
830#define ASC_1000_ID0W 0x04C1
831#define ASC_1000_ID0W_FIX 0x00C1
832#define ASC_1000_ID1B 0x25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#define ASC_EISA_REV_IOP_MASK (0x0C83)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#define ASC_EISA_CFG_IOP_MASK (0x0C86)
835#define ASC_GET_EISA_SLOT(iop) (PortAddr)((iop) & 0xF000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#define INS_HALTINT (ushort)0x6281
837#define INS_HALT (ushort)0x6280
838#define INS_SINT (ushort)0x6200
839#define INS_RFLAG_WTM (ushort)0x7380
840#define ASC_MC_SAVE_CODE_WSIZE 0x500
841#define ASC_MC_SAVE_DATA_WSIZE 0x40
842
843typedef struct asc_mc_saved {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400844 ushort data[ASC_MC_SAVE_DATA_WSIZE];
845 ushort code[ASC_MC_SAVE_CODE_WSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846} ASC_MC_SAVED;
847
848#define AscGetQDoneInProgress(port) AscReadLramByte((port), ASCV_Q_DONE_IN_PROGRESS_B)
849#define AscPutQDoneInProgress(port, val) AscWriteLramByte((port), ASCV_Q_DONE_IN_PROGRESS_B, val)
850#define AscGetVarFreeQHead(port) AscReadLramWord((port), ASCV_FREE_Q_HEAD_W)
851#define AscGetVarDoneQTail(port) AscReadLramWord((port), ASCV_DONE_Q_TAIL_W)
852#define AscPutVarFreeQHead(port, val) AscWriteLramWord((port), ASCV_FREE_Q_HEAD_W, val)
853#define AscPutVarDoneQTail(port, val) AscWriteLramWord((port), ASCV_DONE_Q_TAIL_W, val)
854#define AscGetRiscVarFreeQHead(port) AscReadLramByte((port), ASCV_NEXTRDY_B)
855#define AscGetRiscVarDoneQTail(port) AscReadLramByte((port), ASCV_DONENEXT_B)
856#define AscPutRiscVarFreeQHead(port, val) AscWriteLramByte((port), ASCV_NEXTRDY_B, val)
857#define AscPutRiscVarDoneQTail(port, val) AscWriteLramByte((port), ASCV_DONENEXT_B, val)
Matthew Wilcox51219352007-10-02 21:55:22 -0400858#define AscPutMCodeSDTRDoneAtID(port, id, data) AscWriteLramByte((port), (ushort)((ushort)ASCV_SDTR_DONE_BEG+(ushort)id), (data))
859#define AscGetMCodeSDTRDoneAtID(port, id) AscReadLramByte((port), (ushort)((ushort)ASCV_SDTR_DONE_BEG+(ushort)id))
860#define AscPutMCodeInitSDTRAtID(port, id, data) AscWriteLramByte((port), (ushort)((ushort)ASCV_SDTR_DATA_BEG+(ushort)id), data)
861#define AscGetMCodeInitSDTRAtID(port, id) AscReadLramByte((port), (ushort)((ushort)ASCV_SDTR_DATA_BEG+(ushort)id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#define AscGetChipSignatureByte(port) (uchar)inp((port)+IOP_SIG_BYTE)
863#define AscGetChipSignatureWord(port) (ushort)inpw((port)+IOP_SIG_WORD)
864#define AscGetChipVerNo(port) (uchar)inp((port)+IOP_VERSION)
865#define AscGetChipCfgLsw(port) (ushort)inpw((port)+IOP_CONFIG_LOW)
866#define AscGetChipCfgMsw(port) (ushort)inpw((port)+IOP_CONFIG_HIGH)
867#define AscSetChipCfgLsw(port, data) outpw((port)+IOP_CONFIG_LOW, data)
868#define AscSetChipCfgMsw(port, data) outpw((port)+IOP_CONFIG_HIGH, data)
869#define AscGetChipEEPCmd(port) (uchar)inp((port)+IOP_EEP_CMD)
870#define AscSetChipEEPCmd(port, data) outp((port)+IOP_EEP_CMD, data)
871#define AscGetChipEEPData(port) (ushort)inpw((port)+IOP_EEP_DATA)
872#define AscSetChipEEPData(port, data) outpw((port)+IOP_EEP_DATA, data)
873#define AscGetChipLramAddr(port) (ushort)inpw((PortAddr)((port)+IOP_RAM_ADDR))
874#define AscSetChipLramAddr(port, addr) outpw((PortAddr)((port)+IOP_RAM_ADDR), addr)
875#define AscGetChipLramData(port) (ushort)inpw((port)+IOP_RAM_DATA)
876#define AscSetChipLramData(port, data) outpw((port)+IOP_RAM_DATA, data)
877#define AscGetChipIFC(port) (uchar)inp((port)+IOP_REG_IFC)
878#define AscSetChipIFC(port, data) outp((port)+IOP_REG_IFC, data)
879#define AscGetChipStatus(port) (ASC_CS_TYPE)inpw((port)+IOP_STATUS)
880#define AscSetChipStatus(port, cs_val) outpw((port)+IOP_STATUS, cs_val)
881#define AscGetChipControl(port) (uchar)inp((port)+IOP_CTRL)
882#define AscSetChipControl(port, cc_val) outp((port)+IOP_CTRL, cc_val)
883#define AscGetChipSyn(port) (uchar)inp((port)+IOP_SYN_OFFSET)
884#define AscSetChipSyn(port, data) outp((port)+IOP_SYN_OFFSET, data)
885#define AscSetPCAddr(port, data) outpw((port)+IOP_REG_PC, data)
886#define AscGetPCAddr(port) (ushort)inpw((port)+IOP_REG_PC)
887#define AscIsIntPending(port) (AscGetChipStatus(port) & (CSW_INT_PENDING | CSW_SCSI_RESET_LATCH))
888#define AscGetChipScsiID(port) ((AscGetChipCfgLsw(port) >> 8) & ASC_MAX_TID)
889#define AscGetExtraControl(port) (uchar)inp((port)+IOP_EXTRA_CONTROL)
890#define AscSetExtraControl(port, data) outp((port)+IOP_EXTRA_CONTROL, data)
891#define AscReadChipAX(port) (ushort)inpw((port)+IOP_REG_AX)
892#define AscWriteChipAX(port, data) outpw((port)+IOP_REG_AX, data)
893#define AscReadChipIX(port) (uchar)inp((port)+IOP_REG_IX)
894#define AscWriteChipIX(port, data) outp((port)+IOP_REG_IX, data)
895#define AscReadChipIH(port) (ushort)inpw((port)+IOP_REG_IH)
896#define AscWriteChipIH(port, data) outpw((port)+IOP_REG_IH, data)
897#define AscReadChipQP(port) (uchar)inp((port)+IOP_REG_QP)
898#define AscWriteChipQP(port, data) outp((port)+IOP_REG_QP, data)
899#define AscReadChipFIFO_L(port) (ushort)inpw((port)+IOP_REG_FIFO_L)
900#define AscWriteChipFIFO_L(port, data) outpw((port)+IOP_REG_FIFO_L, data)
901#define AscReadChipFIFO_H(port) (ushort)inpw((port)+IOP_REG_FIFO_H)
902#define AscWriteChipFIFO_H(port, data) outpw((port)+IOP_REG_FIFO_H, data)
903#define AscReadChipDmaSpeed(port) (uchar)inp((port)+IOP_DMA_SPEED)
904#define AscWriteChipDmaSpeed(port, data) outp((port)+IOP_DMA_SPEED, data)
905#define AscReadChipDA0(port) (ushort)inpw((port)+IOP_REG_DA0)
906#define AscWriteChipDA0(port) outpw((port)+IOP_REG_DA0, data)
907#define AscReadChipDA1(port) (ushort)inpw((port)+IOP_REG_DA1)
908#define AscWriteChipDA1(port) outpw((port)+IOP_REG_DA1, data)
909#define AscReadChipDC0(port) (ushort)inpw((port)+IOP_REG_DC0)
910#define AscWriteChipDC0(port) outpw((port)+IOP_REG_DC0, data)
911#define AscReadChipDC1(port) (ushort)inpw((port)+IOP_REG_DC1)
912#define AscWriteChipDC1(port) outpw((port)+IOP_REG_DC1, data)
913#define AscReadChipDvcID(port) (uchar)inp((port)+IOP_REG_ID)
914#define AscWriteChipDvcID(port, data) outp((port)+IOP_REG_ID, data)
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * These macros are used to convert a virtual address to a
918 * 32-bit value. This currently can be used on Linux Alpha
919 * which uses 64-bit virtual address but a 32-bit bus address.
920 * This is likely to break in the future, but doing this now
921 * will give us time to change the HW and FW to handle 64-bit
922 * addresses.
923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#define ADV_U32_TO_VADDR bus_to_virt
925
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400926#define AdvPortAddr void __iomem * /* Virtual memory address size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928/*
929 * Define Adv Library required memory access macros.
930 */
931#define ADV_MEM_READB(addr) readb(addr)
932#define ADV_MEM_READW(addr) readw(addr)
933#define ADV_MEM_WRITEB(addr, byte) writeb(byte, addr)
934#define ADV_MEM_WRITEW(addr, word) writew(word, addr)
935#define ADV_MEM_WRITEDW(addr, dword) writel(dword, addr)
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * Define total number of simultaneous maximum element scatter-gather
939 * request blocks per wide adapter. ASC_DEF_MAX_HOST_QNG (253) is the
940 * maximum number of outstanding commands per wide host adapter. Each
941 * command uses one or more ADV_SG_BLOCK each with 15 scatter-gather
942 * elements. Allow each command to have at least one ADV_SG_BLOCK structure.
943 * This allows about 15 commands to have the maximum 17 ADV_SG_BLOCK
944 * structures or 255 scatter-gather elements.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946#define ADV_TOT_SG_BLOCK ASC_DEF_MAX_HOST_QNG
947
948/*
Matthew Wilcox98d41c22007-10-02 21:55:37 -0400949 * Define maximum number of scatter-gather elements per request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951#define ADV_MAX_SG_LIST 255
Matthew Wilcox98d41c22007-10-02 21:55:37 -0400952#define NO_OF_SG_PER_BLOCK 15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#define ADV_EEP_DVC_CFG_BEGIN (0x00)
955#define ADV_EEP_DVC_CFG_END (0x15)
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400956#define ADV_EEP_DVC_CTL_BEGIN (0x16) /* location of OEM name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957#define ADV_EEP_MAX_WORD_ADDR (0x1E)
958
959#define ADV_EEP_DELAY_MS 100
960
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400961#define ADV_EEPROM_BIG_ENDIAN 0x8000 /* EEPROM Bit 15 */
962#define ADV_EEPROM_BIOS_ENABLE 0x4000 /* EEPROM Bit 14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/*
964 * For the ASC3550 Bit 13 is Termination Polarity control bit.
965 * For later ICs Bit 13 controls whether the CIS (Card Information
966 * Service Section) is loaded from EEPROM.
967 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400968#define ADV_EEPROM_TERM_POL 0x2000 /* EEPROM Bit 13 */
969#define ADV_EEPROM_CIS_LD 0x2000 /* EEPROM Bit 13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/*
971 * ASC38C1600 Bit 11
972 *
973 * If EEPROM Bit 11 is 0 for Function 0, then Function 0 will specify
974 * INT A in the PCI Configuration Space Int Pin field. If it is 1, then
975 * Function 0 will specify INT B.
976 *
977 * If EEPROM Bit 11 is 0 for Function 1, then Function 1 will specify
978 * INT B in the PCI Configuration Space Int Pin field. If it is 1, then
979 * Function 1 will specify INT A.
980 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400981#define ADV_EEPROM_INTAB 0x0800 /* EEPROM Bit 11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400983typedef struct adveep_3550_config {
984 /* Word Offset, Description */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400986 ushort cfg_lsw; /* 00 power up initialization */
987 /* bit 13 set - Term Polarity Control */
988 /* bit 14 set - BIOS Enable */
989 /* bit 15 set - Big Endian Mode */
990 ushort cfg_msw; /* 01 unused */
991 ushort disc_enable; /* 02 disconnect enable */
992 ushort wdtr_able; /* 03 Wide DTR able */
993 ushort sdtr_able; /* 04 Synchronous DTR able */
994 ushort start_motor; /* 05 send start up motor */
995 ushort tagqng_able; /* 06 tag queuing able */
996 ushort bios_scan; /* 07 BIOS device control */
997 ushort scam_tolerant; /* 08 no scam */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400999 uchar adapter_scsi_id; /* 09 Host Adapter ID */
1000 uchar bios_boot_delay; /* power up wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001002 uchar scsi_reset_delay; /* 10 reset delay */
1003 uchar bios_id_lun; /* first boot device scsi id & lun */
1004 /* high nibble is lun */
1005 /* low nibble is scsi id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001007 uchar termination; /* 11 0 - automatic */
1008 /* 1 - low off / high off */
1009 /* 2 - low off / high on */
1010 /* 3 - low on / high on */
1011 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001013 uchar reserved1; /* reserved byte (not used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001015 ushort bios_ctrl; /* 12 BIOS control bits */
1016 /* bit 0 BIOS don't act as initiator. */
1017 /* bit 1 BIOS > 1 GB support */
1018 /* bit 2 BIOS > 2 Disk Support */
1019 /* bit 3 BIOS don't support removables */
1020 /* bit 4 BIOS support bootable CD */
1021 /* bit 5 BIOS scan enabled */
1022 /* bit 6 BIOS support multiple LUNs */
1023 /* bit 7 BIOS display of message */
1024 /* bit 8 SCAM disabled */
1025 /* bit 9 Reset SCSI bus during init. */
1026 /* bit 10 */
1027 /* bit 11 No verbose initialization. */
1028 /* bit 12 SCSI parity enabled */
1029 /* bit 13 */
1030 /* bit 14 */
1031 /* bit 15 */
1032 ushort ultra_able; /* 13 ULTRA speed able */
1033 ushort reserved2; /* 14 reserved */
1034 uchar max_host_qng; /* 15 maximum host queuing */
1035 uchar max_dvc_qng; /* maximum per device queuing */
1036 ushort dvc_cntl; /* 16 control bit for driver */
1037 ushort bug_fix; /* 17 control bit for bug fix */
1038 ushort serial_number_word1; /* 18 Board serial number word 1 */
1039 ushort serial_number_word2; /* 19 Board serial number word 2 */
1040 ushort serial_number_word3; /* 20 Board serial number word 3 */
1041 ushort check_sum; /* 21 EEP check sum */
1042 uchar oem_name[16]; /* 22 OEM name */
1043 ushort dvc_err_code; /* 30 last device driver error code */
1044 ushort adv_err_code; /* 31 last uc and Adv Lib error code */
1045 ushort adv_err_addr; /* 32 last uc error address */
1046 ushort saved_dvc_err_code; /* 33 saved last dev. driver error code */
1047 ushort saved_adv_err_code; /* 34 saved last uc and Adv Lib error code */
1048 ushort saved_adv_err_addr; /* 35 saved last uc error address */
1049 ushort num_of_err; /* 36 number of error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050} ADVEEP_3550_CONFIG;
1051
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001052typedef struct adveep_38C0800_config {
1053 /* Word Offset, Description */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001055 ushort cfg_lsw; /* 00 power up initialization */
1056 /* bit 13 set - Load CIS */
1057 /* bit 14 set - BIOS Enable */
1058 /* bit 15 set - Big Endian Mode */
1059 ushort cfg_msw; /* 01 unused */
1060 ushort disc_enable; /* 02 disconnect enable */
1061 ushort wdtr_able; /* 03 Wide DTR able */
1062 ushort sdtr_speed1; /* 04 SDTR Speed TID 0-3 */
1063 ushort start_motor; /* 05 send start up motor */
1064 ushort tagqng_able; /* 06 tag queuing able */
1065 ushort bios_scan; /* 07 BIOS device control */
1066 ushort scam_tolerant; /* 08 no scam */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001068 uchar adapter_scsi_id; /* 09 Host Adapter ID */
1069 uchar bios_boot_delay; /* power up wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001071 uchar scsi_reset_delay; /* 10 reset delay */
1072 uchar bios_id_lun; /* first boot device scsi id & lun */
1073 /* high nibble is lun */
1074 /* low nibble is scsi id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001076 uchar termination_se; /* 11 0 - automatic */
1077 /* 1 - low off / high off */
1078 /* 2 - low off / high on */
1079 /* 3 - low on / high on */
1080 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001082 uchar termination_lvd; /* 11 0 - automatic */
1083 /* 1 - low off / high off */
1084 /* 2 - low off / high on */
1085 /* 3 - low on / high on */
1086 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001088 ushort bios_ctrl; /* 12 BIOS control bits */
1089 /* bit 0 BIOS don't act as initiator. */
1090 /* bit 1 BIOS > 1 GB support */
1091 /* bit 2 BIOS > 2 Disk Support */
1092 /* bit 3 BIOS don't support removables */
1093 /* bit 4 BIOS support bootable CD */
1094 /* bit 5 BIOS scan enabled */
1095 /* bit 6 BIOS support multiple LUNs */
1096 /* bit 7 BIOS display of message */
1097 /* bit 8 SCAM disabled */
1098 /* bit 9 Reset SCSI bus during init. */
1099 /* bit 10 */
1100 /* bit 11 No verbose initialization. */
1101 /* bit 12 SCSI parity enabled */
1102 /* bit 13 */
1103 /* bit 14 */
1104 /* bit 15 */
1105 ushort sdtr_speed2; /* 13 SDTR speed TID 4-7 */
1106 ushort sdtr_speed3; /* 14 SDTR speed TID 8-11 */
1107 uchar max_host_qng; /* 15 maximum host queueing */
1108 uchar max_dvc_qng; /* maximum per device queuing */
1109 ushort dvc_cntl; /* 16 control bit for driver */
1110 ushort sdtr_speed4; /* 17 SDTR speed 4 TID 12-15 */
1111 ushort serial_number_word1; /* 18 Board serial number word 1 */
1112 ushort serial_number_word2; /* 19 Board serial number word 2 */
1113 ushort serial_number_word3; /* 20 Board serial number word 3 */
1114 ushort check_sum; /* 21 EEP check sum */
1115 uchar oem_name[16]; /* 22 OEM name */
1116 ushort dvc_err_code; /* 30 last device driver error code */
1117 ushort adv_err_code; /* 31 last uc and Adv Lib error code */
1118 ushort adv_err_addr; /* 32 last uc error address */
1119 ushort saved_dvc_err_code; /* 33 saved last dev. driver error code */
1120 ushort saved_adv_err_code; /* 34 saved last uc and Adv Lib error code */
1121 ushort saved_adv_err_addr; /* 35 saved last uc error address */
1122 ushort reserved36; /* 36 reserved */
1123 ushort reserved37; /* 37 reserved */
1124 ushort reserved38; /* 38 reserved */
1125 ushort reserved39; /* 39 reserved */
1126 ushort reserved40; /* 40 reserved */
1127 ushort reserved41; /* 41 reserved */
1128 ushort reserved42; /* 42 reserved */
1129 ushort reserved43; /* 43 reserved */
1130 ushort reserved44; /* 44 reserved */
1131 ushort reserved45; /* 45 reserved */
1132 ushort reserved46; /* 46 reserved */
1133 ushort reserved47; /* 47 reserved */
1134 ushort reserved48; /* 48 reserved */
1135 ushort reserved49; /* 49 reserved */
1136 ushort reserved50; /* 50 reserved */
1137 ushort reserved51; /* 51 reserved */
1138 ushort reserved52; /* 52 reserved */
1139 ushort reserved53; /* 53 reserved */
1140 ushort reserved54; /* 54 reserved */
1141 ushort reserved55; /* 55 reserved */
1142 ushort cisptr_lsw; /* 56 CIS PTR LSW */
1143 ushort cisprt_msw; /* 57 CIS PTR MSW */
1144 ushort subsysvid; /* 58 SubSystem Vendor ID */
1145 ushort subsysid; /* 59 SubSystem ID */
1146 ushort reserved60; /* 60 reserved */
1147 ushort reserved61; /* 61 reserved */
1148 ushort reserved62; /* 62 reserved */
1149 ushort reserved63; /* 63 reserved */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150} ADVEEP_38C0800_CONFIG;
1151
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001152typedef struct adveep_38C1600_config {
1153 /* Word Offset, Description */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001155 ushort cfg_lsw; /* 00 power up initialization */
1156 /* bit 11 set - Func. 0 INTB, Func. 1 INTA */
1157 /* clear - Func. 0 INTA, Func. 1 INTB */
1158 /* bit 13 set - Load CIS */
1159 /* bit 14 set - BIOS Enable */
1160 /* bit 15 set - Big Endian Mode */
1161 ushort cfg_msw; /* 01 unused */
1162 ushort disc_enable; /* 02 disconnect enable */
1163 ushort wdtr_able; /* 03 Wide DTR able */
1164 ushort sdtr_speed1; /* 04 SDTR Speed TID 0-3 */
1165 ushort start_motor; /* 05 send start up motor */
1166 ushort tagqng_able; /* 06 tag queuing able */
1167 ushort bios_scan; /* 07 BIOS device control */
1168 ushort scam_tolerant; /* 08 no scam */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001170 uchar adapter_scsi_id; /* 09 Host Adapter ID */
1171 uchar bios_boot_delay; /* power up wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001173 uchar scsi_reset_delay; /* 10 reset delay */
1174 uchar bios_id_lun; /* first boot device scsi id & lun */
1175 /* high nibble is lun */
1176 /* low nibble is scsi id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001178 uchar termination_se; /* 11 0 - automatic */
1179 /* 1 - low off / high off */
1180 /* 2 - low off / high on */
1181 /* 3 - low on / high on */
1182 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001184 uchar termination_lvd; /* 11 0 - automatic */
1185 /* 1 - low off / high off */
1186 /* 2 - low off / high on */
1187 /* 3 - low on / high on */
1188 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001190 ushort bios_ctrl; /* 12 BIOS control bits */
1191 /* bit 0 BIOS don't act as initiator. */
1192 /* bit 1 BIOS > 1 GB support */
1193 /* bit 2 BIOS > 2 Disk Support */
1194 /* bit 3 BIOS don't support removables */
1195 /* bit 4 BIOS support bootable CD */
1196 /* bit 5 BIOS scan enabled */
1197 /* bit 6 BIOS support multiple LUNs */
1198 /* bit 7 BIOS display of message */
1199 /* bit 8 SCAM disabled */
1200 /* bit 9 Reset SCSI bus during init. */
1201 /* bit 10 Basic Integrity Checking disabled */
1202 /* bit 11 No verbose initialization. */
1203 /* bit 12 SCSI parity enabled */
1204 /* bit 13 AIPP (Asyn. Info. Ph. Prot.) dis. */
1205 /* bit 14 */
1206 /* bit 15 */
1207 ushort sdtr_speed2; /* 13 SDTR speed TID 4-7 */
1208 ushort sdtr_speed3; /* 14 SDTR speed TID 8-11 */
1209 uchar max_host_qng; /* 15 maximum host queueing */
1210 uchar max_dvc_qng; /* maximum per device queuing */
1211 ushort dvc_cntl; /* 16 control bit for driver */
1212 ushort sdtr_speed4; /* 17 SDTR speed 4 TID 12-15 */
1213 ushort serial_number_word1; /* 18 Board serial number word 1 */
1214 ushort serial_number_word2; /* 19 Board serial number word 2 */
1215 ushort serial_number_word3; /* 20 Board serial number word 3 */
1216 ushort check_sum; /* 21 EEP check sum */
1217 uchar oem_name[16]; /* 22 OEM name */
1218 ushort dvc_err_code; /* 30 last device driver error code */
1219 ushort adv_err_code; /* 31 last uc and Adv Lib error code */
1220 ushort adv_err_addr; /* 32 last uc error address */
1221 ushort saved_dvc_err_code; /* 33 saved last dev. driver error code */
1222 ushort saved_adv_err_code; /* 34 saved last uc and Adv Lib error code */
1223 ushort saved_adv_err_addr; /* 35 saved last uc error address */
1224 ushort reserved36; /* 36 reserved */
1225 ushort reserved37; /* 37 reserved */
1226 ushort reserved38; /* 38 reserved */
1227 ushort reserved39; /* 39 reserved */
1228 ushort reserved40; /* 40 reserved */
1229 ushort reserved41; /* 41 reserved */
1230 ushort reserved42; /* 42 reserved */
1231 ushort reserved43; /* 43 reserved */
1232 ushort reserved44; /* 44 reserved */
1233 ushort reserved45; /* 45 reserved */
1234 ushort reserved46; /* 46 reserved */
1235 ushort reserved47; /* 47 reserved */
1236 ushort reserved48; /* 48 reserved */
1237 ushort reserved49; /* 49 reserved */
1238 ushort reserved50; /* 50 reserved */
1239 ushort reserved51; /* 51 reserved */
1240 ushort reserved52; /* 52 reserved */
1241 ushort reserved53; /* 53 reserved */
1242 ushort reserved54; /* 54 reserved */
1243 ushort reserved55; /* 55 reserved */
1244 ushort cisptr_lsw; /* 56 CIS PTR LSW */
1245 ushort cisprt_msw; /* 57 CIS PTR MSW */
1246 ushort subsysvid; /* 58 SubSystem Vendor ID */
1247 ushort subsysid; /* 59 SubSystem ID */
1248 ushort reserved60; /* 60 reserved */
1249 ushort reserved61; /* 61 reserved */
1250 ushort reserved62; /* 62 reserved */
1251 ushort reserved63; /* 63 reserved */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252} ADVEEP_38C1600_CONFIG;
1253
1254/*
1255 * EEPROM Commands
1256 */
1257#define ASC_EEP_CMD_DONE 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259/* bios_ctrl */
1260#define BIOS_CTRL_BIOS 0x0001
1261#define BIOS_CTRL_EXTENDED_XLAT 0x0002
1262#define BIOS_CTRL_GT_2_DISK 0x0004
1263#define BIOS_CTRL_BIOS_REMOVABLE 0x0008
1264#define BIOS_CTRL_BOOTABLE_CD 0x0010
1265#define BIOS_CTRL_MULTIPLE_LUN 0x0040
1266#define BIOS_CTRL_DISPLAY_MSG 0x0080
1267#define BIOS_CTRL_NO_SCAM 0x0100
1268#define BIOS_CTRL_RESET_SCSI_BUS 0x0200
1269#define BIOS_CTRL_INIT_VERBOSE 0x0800
1270#define BIOS_CTRL_SCSI_PARITY 0x1000
1271#define BIOS_CTRL_AIPP_DIS 0x2000
1272
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001273#define ADV_3550_MEMSIZE 0x2000 /* 8 KB Internal Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001275#define ADV_38C0800_MEMSIZE 0x4000 /* 16 KB Internal Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277/*
1278 * XXX - Since ASC38C1600 Rev.3 has a local RAM failure issue, there is
1279 * a special 16K Adv Library and Microcode version. After the issue is
1280 * resolved, should restore 32K support.
1281 *
1282 * #define ADV_38C1600_MEMSIZE 0x8000L * 32 KB Internal Memory *
1283 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001284#define ADV_38C1600_MEMSIZE 0x4000 /* 16 KB Internal Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286/*
1287 * Byte I/O register address from base of 'iop_base'.
1288 */
1289#define IOPB_INTR_STATUS_REG 0x00
1290#define IOPB_CHIP_ID_1 0x01
1291#define IOPB_INTR_ENABLES 0x02
1292#define IOPB_CHIP_TYPE_REV 0x03
1293#define IOPB_RES_ADDR_4 0x04
1294#define IOPB_RES_ADDR_5 0x05
1295#define IOPB_RAM_DATA 0x06
1296#define IOPB_RES_ADDR_7 0x07
1297#define IOPB_FLAG_REG 0x08
1298#define IOPB_RES_ADDR_9 0x09
1299#define IOPB_RISC_CSR 0x0A
1300#define IOPB_RES_ADDR_B 0x0B
1301#define IOPB_RES_ADDR_C 0x0C
1302#define IOPB_RES_ADDR_D 0x0D
1303#define IOPB_SOFT_OVER_WR 0x0E
1304#define IOPB_RES_ADDR_F 0x0F
1305#define IOPB_MEM_CFG 0x10
1306#define IOPB_RES_ADDR_11 0x11
1307#define IOPB_GPIO_DATA 0x12
1308#define IOPB_RES_ADDR_13 0x13
1309#define IOPB_FLASH_PAGE 0x14
1310#define IOPB_RES_ADDR_15 0x15
1311#define IOPB_GPIO_CNTL 0x16
1312#define IOPB_RES_ADDR_17 0x17
1313#define IOPB_FLASH_DATA 0x18
1314#define IOPB_RES_ADDR_19 0x19
1315#define IOPB_RES_ADDR_1A 0x1A
1316#define IOPB_RES_ADDR_1B 0x1B
1317#define IOPB_RES_ADDR_1C 0x1C
1318#define IOPB_RES_ADDR_1D 0x1D
1319#define IOPB_RES_ADDR_1E 0x1E
1320#define IOPB_RES_ADDR_1F 0x1F
1321#define IOPB_DMA_CFG0 0x20
1322#define IOPB_DMA_CFG1 0x21
1323#define IOPB_TICKLE 0x22
1324#define IOPB_DMA_REG_WR 0x23
1325#define IOPB_SDMA_STATUS 0x24
1326#define IOPB_SCSI_BYTE_CNT 0x25
1327#define IOPB_HOST_BYTE_CNT 0x26
1328#define IOPB_BYTE_LEFT_TO_XFER 0x27
1329#define IOPB_BYTE_TO_XFER_0 0x28
1330#define IOPB_BYTE_TO_XFER_1 0x29
1331#define IOPB_BYTE_TO_XFER_2 0x2A
1332#define IOPB_BYTE_TO_XFER_3 0x2B
1333#define IOPB_ACC_GRP 0x2C
1334#define IOPB_RES_ADDR_2D 0x2D
1335#define IOPB_DEV_ID 0x2E
1336#define IOPB_RES_ADDR_2F 0x2F
1337#define IOPB_SCSI_DATA 0x30
1338#define IOPB_RES_ADDR_31 0x31
1339#define IOPB_RES_ADDR_32 0x32
1340#define IOPB_SCSI_DATA_HSHK 0x33
1341#define IOPB_SCSI_CTRL 0x34
1342#define IOPB_RES_ADDR_35 0x35
1343#define IOPB_RES_ADDR_36 0x36
1344#define IOPB_RES_ADDR_37 0x37
1345#define IOPB_RAM_BIST 0x38
1346#define IOPB_PLL_TEST 0x39
1347#define IOPB_PCI_INT_CFG 0x3A
1348#define IOPB_RES_ADDR_3B 0x3B
1349#define IOPB_RFIFO_CNT 0x3C
1350#define IOPB_RES_ADDR_3D 0x3D
1351#define IOPB_RES_ADDR_3E 0x3E
1352#define IOPB_RES_ADDR_3F 0x3F
1353
1354/*
1355 * Word I/O register address from base of 'iop_base'.
1356 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001357#define IOPW_CHIP_ID_0 0x00 /* CID0 */
1358#define IOPW_CTRL_REG 0x02 /* CC */
1359#define IOPW_RAM_ADDR 0x04 /* LA */
1360#define IOPW_RAM_DATA 0x06 /* LD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361#define IOPW_RES_ADDR_08 0x08
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001362#define IOPW_RISC_CSR 0x0A /* CSR */
1363#define IOPW_SCSI_CFG0 0x0C /* CFG0 */
1364#define IOPW_SCSI_CFG1 0x0E /* CFG1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365#define IOPW_RES_ADDR_10 0x10
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001366#define IOPW_SEL_MASK 0x12 /* SM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#define IOPW_RES_ADDR_14 0x14
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001368#define IOPW_FLASH_ADDR 0x16 /* FA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#define IOPW_RES_ADDR_18 0x18
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001370#define IOPW_EE_CMD 0x1A /* EC */
1371#define IOPW_EE_DATA 0x1C /* ED */
1372#define IOPW_SFIFO_CNT 0x1E /* SFC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373#define IOPW_RES_ADDR_20 0x20
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001374#define IOPW_Q_BASE 0x22 /* QB */
1375#define IOPW_QP 0x24 /* QP */
1376#define IOPW_IX 0x26 /* IX */
1377#define IOPW_SP 0x28 /* SP */
1378#define IOPW_PC 0x2A /* PC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#define IOPW_RES_ADDR_2C 0x2C
1380#define IOPW_RES_ADDR_2E 0x2E
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001381#define IOPW_SCSI_DATA 0x30 /* SD */
1382#define IOPW_SCSI_DATA_HSHK 0x32 /* SDH */
1383#define IOPW_SCSI_CTRL 0x34 /* SC */
1384#define IOPW_HSHK_CFG 0x36 /* HCFG */
1385#define IOPW_SXFR_STATUS 0x36 /* SXS */
1386#define IOPW_SXFR_CNTL 0x38 /* SXL */
1387#define IOPW_SXFR_CNTH 0x3A /* SXH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#define IOPW_RES_ADDR_3C 0x3C
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001389#define IOPW_RFIFO_DATA 0x3E /* RFD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/*
1392 * Doubleword I/O register address from base of 'iop_base'.
1393 */
1394#define IOPDW_RES_ADDR_0 0x00
1395#define IOPDW_RAM_DATA 0x04
1396#define IOPDW_RES_ADDR_8 0x08
1397#define IOPDW_RES_ADDR_C 0x0C
1398#define IOPDW_RES_ADDR_10 0x10
1399#define IOPDW_COMMA 0x14
1400#define IOPDW_COMMB 0x18
1401#define IOPDW_RES_ADDR_1C 0x1C
1402#define IOPDW_SDMA_ADDR0 0x20
1403#define IOPDW_SDMA_ADDR1 0x24
1404#define IOPDW_SDMA_COUNT 0x28
1405#define IOPDW_SDMA_ERROR 0x2C
1406#define IOPDW_RDMA_ADDR0 0x30
1407#define IOPDW_RDMA_ADDR1 0x34
1408#define IOPDW_RDMA_COUNT 0x38
1409#define IOPDW_RDMA_ERROR 0x3C
1410
1411#define ADV_CHIP_ID_BYTE 0x25
1412#define ADV_CHIP_ID_WORD 0x04C1
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#define ADV_INTR_ENABLE_HOST_INTR 0x01
1415#define ADV_INTR_ENABLE_SEL_INTR 0x02
1416#define ADV_INTR_ENABLE_DPR_INTR 0x04
1417#define ADV_INTR_ENABLE_RTA_INTR 0x08
1418#define ADV_INTR_ENABLE_RMA_INTR 0x10
1419#define ADV_INTR_ENABLE_RST_INTR 0x20
1420#define ADV_INTR_ENABLE_DPE_INTR 0x40
1421#define ADV_INTR_ENABLE_GLOBAL_INTR 0x80
1422
1423#define ADV_INTR_STATUS_INTRA 0x01
1424#define ADV_INTR_STATUS_INTRB 0x02
1425#define ADV_INTR_STATUS_INTRC 0x04
1426
1427#define ADV_RISC_CSR_STOP (0x0000)
1428#define ADV_RISC_TEST_COND (0x2000)
1429#define ADV_RISC_CSR_RUN (0x4000)
1430#define ADV_RISC_CSR_SINGLE_STEP (0x8000)
1431
1432#define ADV_CTRL_REG_HOST_INTR 0x0100
1433#define ADV_CTRL_REG_SEL_INTR 0x0200
1434#define ADV_CTRL_REG_DPR_INTR 0x0400
1435#define ADV_CTRL_REG_RTA_INTR 0x0800
1436#define ADV_CTRL_REG_RMA_INTR 0x1000
1437#define ADV_CTRL_REG_RES_BIT14 0x2000
1438#define ADV_CTRL_REG_DPE_INTR 0x4000
1439#define ADV_CTRL_REG_POWER_DONE 0x8000
1440#define ADV_CTRL_REG_ANY_INTR 0xFF00
1441
1442#define ADV_CTRL_REG_CMD_RESET 0x00C6
1443#define ADV_CTRL_REG_CMD_WR_IO_REG 0x00C5
1444#define ADV_CTRL_REG_CMD_RD_IO_REG 0x00C4
1445#define ADV_CTRL_REG_CMD_WR_PCI_CFG_SPACE 0x00C3
1446#define ADV_CTRL_REG_CMD_RD_PCI_CFG_SPACE 0x00C2
1447
1448#define ADV_TICKLE_NOP 0x00
1449#define ADV_TICKLE_A 0x01
1450#define ADV_TICKLE_B 0x02
1451#define ADV_TICKLE_C 0x03
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#define AdvIsIntPending(port) \
1454 (AdvReadWordRegister(port, IOPW_CTRL_REG) & ADV_CTRL_REG_HOST_INTR)
1455
1456/*
1457 * SCSI_CFG0 Register bit definitions
1458 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001459#define TIMER_MODEAB 0xC000 /* Watchdog, Second, and Select. Timer Ctrl. */
1460#define PARITY_EN 0x2000 /* Enable SCSI Parity Error detection */
1461#define EVEN_PARITY 0x1000 /* Select Even Parity */
1462#define WD_LONG 0x0800 /* Watchdog Interval, 1: 57 min, 0: 13 sec */
1463#define QUEUE_128 0x0400 /* Queue Size, 1: 128 byte, 0: 64 byte */
1464#define PRIM_MODE 0x0100 /* Primitive SCSI mode */
1465#define SCAM_EN 0x0080 /* Enable SCAM selection */
1466#define SEL_TMO_LONG 0x0040 /* Sel/Resel Timeout, 1: 400 ms, 0: 1.6 ms */
1467#define CFRM_ID 0x0020 /* SCAM id sel. confirm., 1: fast, 0: 6.4 ms */
1468#define OUR_ID_EN 0x0010 /* Enable OUR_ID bits */
1469#define OUR_ID 0x000F /* SCSI ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471/*
1472 * SCSI_CFG1 Register bit definitions
1473 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001474#define BIG_ENDIAN 0x8000 /* Enable Big Endian Mode MIO:15, EEP:15 */
1475#define TERM_POL 0x2000 /* Terminator Polarity Ctrl. MIO:13, EEP:13 */
1476#define SLEW_RATE 0x1000 /* SCSI output buffer slew rate */
1477#define FILTER_SEL 0x0C00 /* Filter Period Selection */
1478#define FLTR_DISABLE 0x0000 /* Input Filtering Disabled */
1479#define FLTR_11_TO_20NS 0x0800 /* Input Filtering 11ns to 20ns */
1480#define FLTR_21_TO_39NS 0x0C00 /* Input Filtering 21ns to 39ns */
1481#define ACTIVE_DBL 0x0200 /* Disable Active Negation */
1482#define DIFF_MODE 0x0100 /* SCSI differential Mode (Read-Only) */
1483#define DIFF_SENSE 0x0080 /* 1: No SE cables, 0: SE cable (Read-Only) */
1484#define TERM_CTL_SEL 0x0040 /* Enable TERM_CTL_H and TERM_CTL_L */
1485#define TERM_CTL 0x0030 /* External SCSI Termination Bits */
1486#define TERM_CTL_H 0x0020 /* Enable External SCSI Upper Termination */
1487#define TERM_CTL_L 0x0010 /* Enable External SCSI Lower Termination */
1488#define CABLE_DETECT 0x000F /* External SCSI Cable Connection Status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490/*
1491 * Addendum for ASC-38C0800 Chip
1492 *
1493 * The ASC-38C1600 Chip uses the same definitions except that the
1494 * bus mode override bits [12:10] have been moved to byte register
1495 * offset 0xE (IOPB_SOFT_OVER_WR) bits [12:10]. The [12:10] bits in
1496 * SCSI_CFG1 are read-only and always available. Bit 14 (DIS_TERM_DRV)
1497 * is not needed. The [12:10] bits in IOPB_SOFT_OVER_WR are write-only.
1498 * Also each ASC-38C1600 function or channel uses only cable bits [5:4]
1499 * and [1:0]. Bits [14], [7:6], [3:2] are unused.
1500 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001501#define DIS_TERM_DRV 0x4000 /* 1: Read c_det[3:0], 0: cannot read */
1502#define HVD_LVD_SE 0x1C00 /* Device Detect Bits */
1503#define HVD 0x1000 /* HVD Device Detect */
1504#define LVD 0x0800 /* LVD Device Detect */
1505#define SE 0x0400 /* SE Device Detect */
1506#define TERM_LVD 0x00C0 /* LVD Termination Bits */
1507#define TERM_LVD_HI 0x0080 /* Enable LVD Upper Termination */
1508#define TERM_LVD_LO 0x0040 /* Enable LVD Lower Termination */
1509#define TERM_SE 0x0030 /* SE Termination Bits */
1510#define TERM_SE_HI 0x0020 /* Enable SE Upper Termination */
1511#define TERM_SE_LO 0x0010 /* Enable SE Lower Termination */
1512#define C_DET_LVD 0x000C /* LVD Cable Detect Bits */
1513#define C_DET3 0x0008 /* Cable Detect for LVD External Wide */
1514#define C_DET2 0x0004 /* Cable Detect for LVD Internal Wide */
1515#define C_DET_SE 0x0003 /* SE Cable Detect Bits */
1516#define C_DET1 0x0002 /* Cable Detect for SE Internal Wide */
1517#define C_DET0 0x0001 /* Cable Detect for SE Internal Narrow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519#define CABLE_ILLEGAL_A 0x7
1520 /* x 0 0 0 | on on | Illegal (all 3 connectors are used) */
1521
1522#define CABLE_ILLEGAL_B 0xB
1523 /* 0 x 0 0 | on on | Illegal (all 3 connectors are used) */
1524
1525/*
1526 * MEM_CFG Register bit definitions
1527 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001528#define BIOS_EN 0x40 /* BIOS Enable MIO:14,EEP:14 */
1529#define FAST_EE_CLK 0x20 /* Diagnostic Bit */
1530#define RAM_SZ 0x1C /* Specify size of RAM to RISC */
1531#define RAM_SZ_2KB 0x00 /* 2 KB */
1532#define RAM_SZ_4KB 0x04 /* 4 KB */
1533#define RAM_SZ_8KB 0x08 /* 8 KB */
1534#define RAM_SZ_16KB 0x0C /* 16 KB */
1535#define RAM_SZ_32KB 0x10 /* 32 KB */
1536#define RAM_SZ_64KB 0x14 /* 64 KB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538/*
1539 * DMA_CFG0 Register bit definitions
1540 *
1541 * This register is only accessible to the host.
1542 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001543#define BC_THRESH_ENB 0x80 /* PCI DMA Start Conditions */
1544#define FIFO_THRESH 0x70 /* PCI DMA FIFO Threshold */
1545#define FIFO_THRESH_16B 0x00 /* 16 bytes */
1546#define FIFO_THRESH_32B 0x20 /* 32 bytes */
1547#define FIFO_THRESH_48B 0x30 /* 48 bytes */
1548#define FIFO_THRESH_64B 0x40 /* 64 bytes */
1549#define FIFO_THRESH_80B 0x50 /* 80 bytes (default) */
1550#define FIFO_THRESH_96B 0x60 /* 96 bytes */
1551#define FIFO_THRESH_112B 0x70 /* 112 bytes */
1552#define START_CTL 0x0C /* DMA start conditions */
1553#define START_CTL_TH 0x00 /* Wait threshold level (default) */
1554#define START_CTL_ID 0x04 /* Wait SDMA/SBUS idle */
1555#define START_CTL_THID 0x08 /* Wait threshold and SDMA/SBUS idle */
1556#define START_CTL_EMFU 0x0C /* Wait SDMA FIFO empty/full */
1557#define READ_CMD 0x03 /* Memory Read Method */
1558#define READ_CMD_MR 0x00 /* Memory Read */
1559#define READ_CMD_MRL 0x02 /* Memory Read Long */
1560#define READ_CMD_MRM 0x03 /* Memory Read Multiple (default) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562/*
1563 * ASC-38C0800 RAM BIST Register bit definitions
1564 */
1565#define RAM_TEST_MODE 0x80
1566#define PRE_TEST_MODE 0x40
1567#define NORMAL_MODE 0x00
1568#define RAM_TEST_DONE 0x10
1569#define RAM_TEST_STATUS 0x0F
1570#define RAM_TEST_HOST_ERROR 0x08
1571#define RAM_TEST_INTRAM_ERROR 0x04
1572#define RAM_TEST_RISC_ERROR 0x02
1573#define RAM_TEST_SCSI_ERROR 0x01
1574#define RAM_TEST_SUCCESS 0x00
1575#define PRE_TEST_VALUE 0x05
1576#define NORMAL_VALUE 0x00
1577
1578/*
1579 * ASC38C1600 Definitions
1580 *
1581 * IOPB_PCI_INT_CFG Bit Field Definitions
1582 */
1583
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001584#define INTAB_LD 0x80 /* Value loaded from EEPROM Bit 11. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586/*
1587 * Bit 1 can be set to change the interrupt for the Function to operate in
1588 * Totem Pole mode. By default Bit 1 is 0 and the interrupt operates in
1589 * Open Drain mode. Both functions of the ASC38C1600 must be set to the same
1590 * mode, otherwise the operating mode is undefined.
1591 */
1592#define TOTEMPOLE 0x02
1593
1594/*
1595 * Bit 0 can be used to change the Int Pin for the Function. The value is
1596 * 0 by default for both Functions with Function 0 using INT A and Function
1597 * B using INT B. For Function 0 if set, INT B is used. For Function 1 if set,
1598 * INT A is used.
1599 *
1600 * EEPROM Word 0 Bit 11 for each Function may change the initial Int Pin
1601 * value specified in the PCI Configuration Space.
1602 */
1603#define INTAB 0x01
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605/*
1606 * Adv Library Status Definitions
1607 */
1608#define ADV_TRUE 1
1609#define ADV_FALSE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610#define ADV_SUCCESS 1
1611#define ADV_BUSY 0
1612#define ADV_ERROR (-1)
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614/*
1615 * ADV_DVC_VAR 'warn_code' values
1616 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001617#define ASC_WARN_BUSRESET_ERROR 0x0001 /* SCSI Bus Reset error */
1618#define ASC_WARN_EEPROM_CHKSUM 0x0002 /* EEP check sum error */
1619#define ASC_WARN_EEPROM_TERMINATION 0x0004 /* EEP termination bad field */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001620#define ASC_WARN_ERROR 0xFFFF /* ADV_ERROR return */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001622#define ADV_MAX_TID 15 /* max. target identifier */
1623#define ADV_MAX_LUN 7 /* max. logical unit number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 * Fixed locations of microcode operating variables.
1627 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001628#define ASC_MC_CODE_BEGIN_ADDR 0x0028 /* microcode start address */
1629#define ASC_MC_CODE_END_ADDR 0x002A /* microcode end address */
1630#define ASC_MC_CODE_CHK_SUM 0x002C /* microcode code checksum */
1631#define ASC_MC_VERSION_DATE 0x0038 /* microcode version */
1632#define ASC_MC_VERSION_NUM 0x003A /* microcode number */
1633#define ASC_MC_BIOSMEM 0x0040 /* BIOS RISC Memory Start */
1634#define ASC_MC_BIOSLEN 0x0050 /* BIOS RISC Memory Length */
1635#define ASC_MC_BIOS_SIGNATURE 0x0058 /* BIOS Signature 0x55AA */
1636#define ASC_MC_BIOS_VERSION 0x005A /* BIOS Version (2 bytes) */
1637#define ASC_MC_SDTR_SPEED1 0x0090 /* SDTR Speed for TID 0-3 */
1638#define ASC_MC_SDTR_SPEED2 0x0092 /* SDTR Speed for TID 4-7 */
1639#define ASC_MC_SDTR_SPEED3 0x0094 /* SDTR Speed for TID 8-11 */
1640#define ASC_MC_SDTR_SPEED4 0x0096 /* SDTR Speed for TID 12-15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641#define ASC_MC_CHIP_TYPE 0x009A
1642#define ASC_MC_INTRB_CODE 0x009B
1643#define ASC_MC_WDTR_ABLE 0x009C
1644#define ASC_MC_SDTR_ABLE 0x009E
1645#define ASC_MC_TAGQNG_ABLE 0x00A0
1646#define ASC_MC_DISC_ENABLE 0x00A2
1647#define ASC_MC_IDLE_CMD_STATUS 0x00A4
1648#define ASC_MC_IDLE_CMD 0x00A6
1649#define ASC_MC_IDLE_CMD_PARAMETER 0x00A8
1650#define ASC_MC_DEFAULT_SCSI_CFG0 0x00AC
1651#define ASC_MC_DEFAULT_SCSI_CFG1 0x00AE
1652#define ASC_MC_DEFAULT_MEM_CFG 0x00B0
1653#define ASC_MC_DEFAULT_SEL_MASK 0x00B2
1654#define ASC_MC_SDTR_DONE 0x00B6
1655#define ASC_MC_NUMBER_OF_QUEUED_CMD 0x00C0
1656#define ASC_MC_NUMBER_OF_MAX_CMD 0x00D0
1657#define ASC_MC_DEVICE_HSHK_CFG_TABLE 0x0100
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001658#define ASC_MC_CONTROL_FLAG 0x0122 /* Microcode control flag. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659#define ASC_MC_WDTR_DONE 0x0124
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001660#define ASC_MC_CAM_MODE_MASK 0x015E /* CAM mode TID bitmask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661#define ASC_MC_ICQ 0x0160
1662#define ASC_MC_IRQ 0x0164
1663#define ASC_MC_PPR_ABLE 0x017A
1664
1665/*
1666 * BIOS LRAM variable absolute offsets.
1667 */
1668#define BIOS_CODESEG 0x54
1669#define BIOS_CODELEN 0x56
1670#define BIOS_SIGNATURE 0x58
1671#define BIOS_VERSION 0x5A
1672
1673/*
1674 * Microcode Control Flags
1675 *
1676 * Flags set by the Adv Library in RISC variable 'control_flag' (0x122)
1677 * and handled by the microcode.
1678 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001679#define CONTROL_FLAG_IGNORE_PERR 0x0001 /* Ignore DMA Parity Errors */
1680#define CONTROL_FLAG_ENABLE_AIPP 0x0002 /* Enabled AIPP checking. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/*
1683 * ASC_MC_DEVICE_HSHK_CFG_TABLE microcode table or HSHK_CFG register format
1684 */
1685#define HSHK_CFG_WIDE_XFR 0x8000
1686#define HSHK_CFG_RATE 0x0F00
1687#define HSHK_CFG_OFFSET 0x001F
1688
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001689#define ASC_DEF_MAX_HOST_QNG 0xFD /* Max. number of host commands (253) */
1690#define ASC_DEF_MIN_HOST_QNG 0x10 /* Min. number of host commands (16) */
1691#define ASC_DEF_MAX_DVC_QNG 0x3F /* Max. number commands per device (63) */
1692#define ASC_DEF_MIN_DVC_QNG 0x04 /* Min. number commands per device (4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001694#define ASC_QC_DATA_CHECK 0x01 /* Require ASC_QC_DATA_OUT set or clear. */
1695#define ASC_QC_DATA_OUT 0x02 /* Data out DMA transfer. */
1696#define ASC_QC_START_MOTOR 0x04 /* Send auto-start motor before request. */
1697#define ASC_QC_NO_OVERRUN 0x08 /* Don't report overrun. */
1698#define ASC_QC_FREEZE_TIDQ 0x10 /* Freeze TID queue after request. XXX TBD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001700#define ASC_QSC_NO_DISC 0x01 /* Don't allow disconnect for request. */
1701#define ASC_QSC_NO_TAGMSG 0x02 /* Don't allow tag queuing for request. */
1702#define ASC_QSC_NO_SYNC 0x04 /* Don't use Synch. transfer on request. */
1703#define ASC_QSC_NO_WIDE 0x08 /* Don't use Wide transfer on request. */
1704#define ASC_QSC_REDO_DTR 0x10 /* Renegotiate WDTR/SDTR before request. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705/*
1706 * Note: If a Tag Message is to be sent and neither ASC_QSC_HEAD_TAG or
1707 * ASC_QSC_ORDERED_TAG is set, then a Simple Tag Message (0x20) is used.
1708 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001709#define ASC_QSC_HEAD_TAG 0x40 /* Use Head Tag Message (0x21). */
1710#define ASC_QSC_ORDERED_TAG 0x80 /* Use Ordered Tag Message (0x22). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712/*
1713 * All fields here are accessed by the board microcode and need to be
1714 * little-endian.
1715 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001716typedef struct adv_carr_t {
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001717 __le32 carr_va; /* Carrier Virtual Address */
1718 __le32 carr_pa; /* Carrier Physical Address */
1719 __le32 areq_vpa; /* ASC_SCSI_REQ_Q Virtual or Physical Address */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001720 /*
1721 * next_vpa [31:4] Carrier Virtual or Physical Next Pointer
1722 *
1723 * next_vpa [3:1] Reserved Bits
1724 * next_vpa [0] Done Flag set in Response Queue.
1725 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001726 __le32 next_vpa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727} ADV_CARR_T;
1728
1729/*
1730 * Mask used to eliminate low 4 bits of carrier 'next_vpa' field.
1731 */
1732#define ASC_NEXT_VPA_MASK 0xFFFFFFF0
1733
1734#define ASC_RQ_DONE 0x00000001
1735#define ASC_RQ_GOOD 0x00000002
1736#define ASC_CQ_STOPPER 0x00000000
1737
1738#define ASC_GET_CARRP(carrp) ((carrp) & ASC_NEXT_VPA_MASK)
1739
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001740/*
1741 * Each carrier is 64 bytes, and we need three additional
1742 * carrier for icq, irq, and the termination carrier.
1743 */
1744#define ADV_CARRIER_COUNT (ASC_DEF_MAX_HOST_QNG + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746#define ADV_CARRIER_BUFSIZE \
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001747 (ADV_CARRIER_COUNT * sizeof(ADV_CARR_T))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749/*
1750 * ASC_SCSI_REQ_Q 'a_flag' definitions
1751 *
1752 * The Adv Library should limit use to the lower nibble (4 bits) of
1753 * a_flag. Drivers are free to use the upper nibble (4 bits) of a_flag.
1754 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001755#define ADV_POLL_REQUEST 0x01 /* poll for request completion */
1756#define ADV_SCSIQ_DONE 0x02 /* request done */
1757#define ADV_DONT_RETRY 0x08 /* don't do retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001759#define ADV_CHIP_ASC3550 0x01 /* Ultra-Wide IC */
1760#define ADV_CHIP_ASC38C0800 0x02 /* Ultra2-Wide/LVD IC */
1761#define ADV_CHIP_ASC38C1600 0x03 /* Ultra3-Wide/LVD2 IC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763/*
1764 * Adapter temporary configuration structure
1765 *
1766 * This structure can be discarded after initialization. Don't add
1767 * fields here needed after initialization.
1768 *
1769 * Field naming convention:
1770 *
1771 * *_enable indicates the field enables or disables a feature. The
1772 * value of the field is never reset.
1773 */
1774typedef struct adv_dvc_cfg {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001775 ushort disc_enable; /* enable disconnection */
1776 uchar chip_version; /* chip version */
1777 uchar termination; /* Term. Ctrl. bits 6-5 of SCSI_CFG1 register */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001778 ushort control_flag; /* Microcode Control Flag */
1779 ushort mcode_date; /* Microcode date */
1780 ushort mcode_version; /* Microcode version */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001781 ushort serial1; /* EEPROM serial number word 1 */
1782 ushort serial2; /* EEPROM serial number word 2 */
1783 ushort serial3; /* EEPROM serial number word 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784} ADV_DVC_CFG;
1785
1786struct adv_dvc_var;
1787struct adv_scsi_req_q;
1788
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001789typedef struct adv_sg_block {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001790 uchar reserved1;
1791 uchar reserved2;
1792 uchar reserved3;
1793 uchar sg_cnt; /* Valid entries in block. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001794 __le32 sg_ptr; /* Pointer to next sg block. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001795 struct {
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001796 __le32 sg_addr; /* SG element address. */
1797 __le32 sg_count; /* SG element count. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001798 } sg_list[NO_OF_SG_PER_BLOCK];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799} ADV_SG_BLOCK;
1800
1801/*
1802 * ADV_SCSI_REQ_Q - microcode request structure
1803 *
1804 * All fields in this structure up to byte 60 are used by the microcode.
1805 * The microcode makes assumptions about the size and ordering of fields
1806 * in this structure. Do not change the structure definition here without
1807 * coordinating the change with the microcode.
1808 *
1809 * All fields accessed by microcode must be maintained in little_endian
1810 * order.
1811 */
1812typedef struct adv_scsi_req_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001813 uchar cntl; /* Ucode flags and state (ASC_MC_QC_*). */
1814 uchar target_cmd;
1815 uchar target_id; /* Device target identifier. */
1816 uchar target_lun; /* Device target logical unit number. */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02001817 __le32 data_addr; /* Data buffer physical address. */
1818 __le32 data_cnt; /* Data count. Ucode sets to residual. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02001819 __le32 sense_addr;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001820 __le32 carr_pa;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001821 uchar mflag;
1822 uchar sense_len;
1823 uchar cdb_len; /* SCSI CDB length. Must <= 16 bytes. */
1824 uchar scsi_cntl;
1825 uchar done_status; /* Completion status. */
1826 uchar scsi_status; /* SCSI status byte. */
1827 uchar host_status; /* Ucode host status. */
1828 uchar sg_working_ix;
1829 uchar cdb[12]; /* SCSI CDB bytes 0-11. */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02001830 __le32 sg_real_addr; /* SG list physical address. */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001831 __le32 scsiq_rptr;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001832 uchar cdb16[4]; /* SCSI CDB bytes 12-15. */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001833 __le32 scsiq_ptr;
1834 __le32 carr_va;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001835 /*
1836 * End of microcode structure - 60 bytes. The rest of the structure
1837 * is used by the Adv Library and ignored by the microcode.
1838 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02001839 u32 srb_tag;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001840 uchar a_flag;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001841 uchar pad[3]; /* Pad out to a word boundary. */
1842 ADV_SG_BLOCK *sg_list_ptr; /* SG list virtual address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843} ADV_SCSI_REQ_Q;
1844
1845/*
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001846 * The following two structures are used to process Wide Board requests.
1847 *
1848 * The ADV_SCSI_REQ_Q structure in adv_req_t is passed to the Adv Library
Hannes Reinecke9c17c622015-04-24 13:18:21 +02001849 * and microcode with the ADV_SCSI_REQ_Q field 'srb_tag' set to the
1850 * SCSI request tag. The adv_req_t structure 'cmndp' field in turn points
1851 * to the Mid-Level SCSI request structure.
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001852 *
1853 * Zero or more ADV_SG_BLOCK are used with each ADV_SCSI_REQ_Q. Each
1854 * ADV_SG_BLOCK structure holds 15 scatter-gather elements. Under Linux
1855 * up to 255 scatter-gather elements may be used per request or
1856 * ADV_SCSI_REQ_Q.
1857 *
1858 * Both structures must be 32 byte aligned.
1859 */
1860typedef struct adv_sgblk {
1861 ADV_SG_BLOCK sg_block; /* Sgblock structure. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001862 dma_addr_t sg_addr; /* Physical address */
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001863 struct adv_sgblk *next_sgblkp; /* Next scatter-gather structure. */
1864} adv_sgblk_t;
1865
1866typedef struct adv_req {
1867 ADV_SCSI_REQ_Q scsi_req_q; /* Adv Library request structure. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02001868 uchar align[24]; /* Request structure padding. */
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001869 struct scsi_cmnd *cmndp; /* Mid-Level SCSI command pointer. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02001870 dma_addr_t req_addr;
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001871 adv_sgblk_t *sgblkp; /* Adv Library scatter-gather pointer. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02001872} adv_req_t __aligned(32);
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001873
1874/*
1875 * Adapter operation variable structure.
1876 *
1877 * One structure is required per host adapter.
1878 *
1879 * Field naming convention:
1880 *
1881 * *_able indicates both whether a feature should be enabled or disabled
1882 * and whether a device isi capable of the feature. At initialization
1883 * this field may be set, but later if a device is found to be incapable
1884 * of the feature, the field is cleared.
1885 */
1886typedef struct adv_dvc_var {
1887 AdvPortAddr iop_base; /* I/O port address */
1888 ushort err_code; /* fatal error code */
1889 ushort bios_ctrl; /* BIOS control word, EEPROM word 12 */
1890 ushort wdtr_able; /* try WDTR for a device */
1891 ushort sdtr_able; /* try SDTR for a device */
1892 ushort ultra_able; /* try SDTR Ultra speed for a device */
1893 ushort sdtr_speed1; /* EEPROM SDTR Speed for TID 0-3 */
1894 ushort sdtr_speed2; /* EEPROM SDTR Speed for TID 4-7 */
1895 ushort sdtr_speed3; /* EEPROM SDTR Speed for TID 8-11 */
1896 ushort sdtr_speed4; /* EEPROM SDTR Speed for TID 12-15 */
1897 ushort tagqng_able; /* try tagged queuing with a device */
1898 ushort ppr_able; /* PPR message capable per TID bitmask. */
1899 uchar max_dvc_qng; /* maximum number of tagged commands per device */
1900 ushort start_motor; /* start motor command allowed */
1901 uchar scsi_reset_wait; /* delay in seconds after scsi bus reset */
1902 uchar chip_no; /* should be assigned by caller */
1903 uchar max_host_qng; /* maximum number of Q'ed command allowed */
1904 ushort no_scam; /* scam_tolerant of EEPROM */
1905 struct asc_board *drv_ptr; /* driver pointer to private structure */
1906 uchar chip_scsi_id; /* chip SCSI target ID */
1907 uchar chip_type;
1908 uchar bist_err_code;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001909 ADV_CARR_T *carrier;
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001910 ADV_CARR_T *carr_freelist; /* Carrier free list. */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001911 dma_addr_t carrier_addr;
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001912 ADV_CARR_T *icq_sp; /* Initiator command queue stopper pointer. */
1913 ADV_CARR_T *irq_sp; /* Initiator response queue stopper pointer. */
1914 ushort carr_pending_cnt; /* Count of pending carriers. */
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001915 /*
1916 * Note: The following fields will not be used after initialization. The
1917 * driver may discard the buffer after initialization is done.
1918 */
1919 ADV_DVC_CFG *cfg; /* temporary configuration structure */
1920} ADV_DVC_VAR;
1921
1922/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * Microcode idle loop commands
1924 */
1925#define IDLE_CMD_COMPLETED 0
1926#define IDLE_CMD_STOP_CHIP 0x0001
1927#define IDLE_CMD_STOP_CHIP_SEND_INT 0x0002
1928#define IDLE_CMD_SEND_INT 0x0004
1929#define IDLE_CMD_ABORT 0x0008
1930#define IDLE_CMD_DEVICE_RESET 0x0010
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001931#define IDLE_CMD_SCSI_RESET_START 0x0020 /* Assert SCSI Bus Reset */
1932#define IDLE_CMD_SCSI_RESET_END 0x0040 /* Deassert SCSI Bus Reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933#define IDLE_CMD_SCSIREQ 0x0080
1934
1935#define IDLE_CMD_STATUS_SUCCESS 0x0001
1936#define IDLE_CMD_STATUS_FAILURE 0x0002
1937
1938/*
1939 * AdvSendIdleCmd() flag definitions.
1940 */
1941#define ADV_NOWAIT 0x01
1942
1943/*
1944 * Wait loop time out values.
1945 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001946#define SCSI_WAIT_100_MSEC 100UL /* 100 milliseconds */
1947#define SCSI_US_PER_MSEC 1000 /* microseconds per millisecond */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001948#define SCSI_MAX_RETRY 10 /* retry count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001950#define ADV_ASYNC_RDMA_FAILURE 0x01 /* Fatal RDMA failure. */
1951#define ADV_ASYNC_SCSI_BUS_RESET_DET 0x02 /* Detected SCSI Bus Reset. */
1952#define ADV_ASYNC_CARRIER_READY_FAILURE 0x03 /* Carrier Ready failure. */
1953#define ADV_RDMA_IN_CARR_AND_Q_INVALID 0x04 /* RDMAed-in data invalid. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001955#define ADV_HOST_SCSI_BUS_RESET 0x80 /* Host Initiated SCSI Bus Reset. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/* Read byte from a register. */
1958#define AdvReadByteRegister(iop_base, reg_off) \
1959 (ADV_MEM_READB((iop_base) + (reg_off)))
1960
1961/* Write byte to a register. */
1962#define AdvWriteByteRegister(iop_base, reg_off, byte) \
1963 (ADV_MEM_WRITEB((iop_base) + (reg_off), (byte)))
1964
1965/* Read word (2 bytes) from a register. */
1966#define AdvReadWordRegister(iop_base, reg_off) \
1967 (ADV_MEM_READW((iop_base) + (reg_off)))
1968
1969/* Write word (2 bytes) to a register. */
1970#define AdvWriteWordRegister(iop_base, reg_off, word) \
1971 (ADV_MEM_WRITEW((iop_base) + (reg_off), (word)))
1972
1973/* Write dword (4 bytes) to a register. */
1974#define AdvWriteDWordRegister(iop_base, reg_off, dword) \
1975 (ADV_MEM_WRITEDW((iop_base) + (reg_off), (dword)))
1976
1977/* Read byte from LRAM. */
1978#define AdvReadByteLram(iop_base, addr, byte) \
1979do { \
1980 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)); \
1981 (byte) = ADV_MEM_READB((iop_base) + IOPB_RAM_DATA); \
1982} while (0)
1983
1984/* Write byte to LRAM. */
1985#define AdvWriteByteLram(iop_base, addr, byte) \
1986 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1987 ADV_MEM_WRITEB((iop_base) + IOPB_RAM_DATA, (byte)))
1988
1989/* Read word (2 bytes) from LRAM. */
1990#define AdvReadWordLram(iop_base, addr, word) \
1991do { \
1992 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)); \
1993 (word) = (ADV_MEM_READW((iop_base) + IOPW_RAM_DATA)); \
1994} while (0)
1995
1996/* Write word (2 bytes) to LRAM. */
1997#define AdvWriteWordLram(iop_base, addr, word) \
1998 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1999 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, (word)))
2000
2001/* Write little-endian double word (4 bytes) to LRAM */
2002/* Because of unspecified C language ordering don't use auto-increment. */
2003#define AdvWriteDWordLramNoSwap(iop_base, addr, dword) \
2004 ((ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
2005 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, \
2006 cpu_to_le16((ushort) ((dword) & 0xFFFF)))), \
2007 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr) + 2), \
2008 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, \
2009 cpu_to_le16((ushort) ((dword >> 16) & 0xFFFF)))))
2010
2011/* Read word (2 bytes) from LRAM assuming that the address is already set. */
2012#define AdvReadWordAutoIncLram(iop_base) \
2013 (ADV_MEM_READW((iop_base) + IOPW_RAM_DATA))
2014
2015/* Write word (2 bytes) to LRAM assuming that the address is already set. */
2016#define AdvWriteWordAutoIncLram(iop_base, word) \
2017 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, (word)))
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019/*
2020 * Define macro to check for Condor signature.
2021 *
2022 * Evaluate to ADV_TRUE if a Condor chip is found the specified port
2023 * address 'iop_base'. Otherwise evalue to ADV_FALSE.
2024 */
2025#define AdvFindSignature(iop_base) \
2026 (((AdvReadByteRegister((iop_base), IOPB_CHIP_ID_1) == \
2027 ADV_CHIP_ID_BYTE) && \
2028 (AdvReadWordRegister((iop_base), IOPW_CHIP_ID_0) == \
2029 ADV_CHIP_ID_WORD)) ? ADV_TRUE : ADV_FALSE)
2030
2031/*
2032 * Define macro to Return the version number of the chip at 'iop_base'.
2033 *
2034 * The second parameter 'bus_type' is currently unused.
2035 */
2036#define AdvGetChipVersion(iop_base, bus_type) \
2037 AdvReadByteRegister((iop_base), IOPB_CHIP_TYPE_REV)
2038
2039/*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002040 * Abort an SRB in the chip's RISC Memory. The 'srb_tag' argument must
2041 * match the ASC_SCSI_REQ_Q 'srb_tag' field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 *
2043 * If the request has not yet been sent to the device it will simply be
2044 * aborted from RISC memory. If the request is disconnected it will be
2045 * aborted on reselection by sending an Abort Message to the target ID.
2046 *
2047 * Return value:
2048 * ADV_TRUE(1) - Queue was successfully aborted.
2049 * ADV_FALSE(0) - Queue was not found on the active queue list.
2050 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002051#define AdvAbortQueue(asc_dvc, srb_tag) \
2052 AdvSendIdleCmd((asc_dvc), (ushort) IDLE_CMD_ABORT, \
2053 (ADV_DCNT) (srb_tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055/*
2056 * Send a Bus Device Reset Message to the specified target ID.
2057 *
2058 * All outstanding commands will be purged if sending the
2059 * Bus Device Reset Message is successful.
2060 *
2061 * Return Value:
2062 * ADV_TRUE(1) - All requests on the target are purged.
2063 * ADV_FALSE(0) - Couldn't issue Bus Device Reset Message; Requests
2064 * are not purged.
2065 */
2066#define AdvResetDevice(asc_dvc, target_id) \
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002067 AdvSendIdleCmd((asc_dvc), (ushort) IDLE_CMD_DEVICE_RESET, \
2068 (ADV_DCNT) (target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070/*
2071 * SCSI Wide Type definition.
2072 */
2073#define ADV_SCSI_BIT_ID_TYPE ushort
2074
2075/*
2076 * AdvInitScsiTarget() 'cntl_flag' options.
2077 */
2078#define ADV_SCAN_LUN 0x01
2079#define ADV_CAPINFO_NOLUN 0x02
2080
2081/*
2082 * Convert target id to target id bit mask.
2083 */
2084#define ADV_TID_TO_TIDMASK(tid) (0x01 << ((tid) & ADV_MAX_TID))
2085
2086/*
2087 * ASC_SCSI_REQ_Q 'done_status' and 'host_status' return values.
2088 */
2089
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002090#define QD_NO_STATUS 0x00 /* Request not completed yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091#define QD_NO_ERROR 0x01
2092#define QD_ABORTED_BY_HOST 0x02
2093#define QD_WITH_ERROR 0x04
2094
2095#define QHSTA_NO_ERROR 0x00
2096#define QHSTA_M_SEL_TIMEOUT 0x11
2097#define QHSTA_M_DATA_OVER_RUN 0x12
2098#define QHSTA_M_UNEXPECTED_BUS_FREE 0x13
2099#define QHSTA_M_QUEUE_ABORTED 0x15
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002100#define QHSTA_M_SXFR_SDMA_ERR 0x16 /* SXFR_STATUS SCSI DMA Error */
2101#define QHSTA_M_SXFR_SXFR_PERR 0x17 /* SXFR_STATUS SCSI Bus Parity Error */
2102#define QHSTA_M_RDMA_PERR 0x18 /* RISC PCI DMA parity error */
2103#define QHSTA_M_SXFR_OFF_UFLW 0x19 /* SXFR_STATUS Offset Underflow */
2104#define QHSTA_M_SXFR_OFF_OFLW 0x20 /* SXFR_STATUS Offset Overflow */
2105#define QHSTA_M_SXFR_WD_TMO 0x21 /* SXFR_STATUS Watchdog Timeout */
2106#define QHSTA_M_SXFR_DESELECTED 0x22 /* SXFR_STATUS Deselected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107/* Note: QHSTA_M_SXFR_XFR_OFLW is identical to QHSTA_M_DATA_OVER_RUN. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002108#define QHSTA_M_SXFR_XFR_OFLW 0x12 /* SXFR_STATUS Transfer Overflow */
2109#define QHSTA_M_SXFR_XFR_PH_ERR 0x24 /* SXFR_STATUS Transfer Phase Error */
2110#define QHSTA_M_SXFR_UNKNOWN_ERROR 0x25 /* SXFR_STATUS Unknown Error */
2111#define QHSTA_M_SCSI_BUS_RESET 0x30 /* Request aborted from SBR */
2112#define QHSTA_M_SCSI_BUS_RESET_UNSOL 0x31 /* Request aborted from unsol. SBR */
2113#define QHSTA_M_BUS_DEVICE_RESET 0x32 /* Request aborted from BDR */
2114#define QHSTA_M_DIRECTION_ERR 0x35 /* Data Phase mismatch */
2115#define QHSTA_M_DIRECTION_ERR_HUNG 0x36 /* Data Phase mismatch and bus hang */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#define QHSTA_M_WTM_TIMEOUT 0x41
2117#define QHSTA_M_BAD_CMPL_STATUS_IN 0x42
2118#define QHSTA_M_NO_AUTO_REQ_SENSE 0x43
2119#define QHSTA_M_AUTO_REQ_SENSE_FAIL 0x44
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002120#define QHSTA_M_INVALID_DEVICE 0x45 /* Bad target ID */
2121#define QHSTA_M_FROZEN_TIDQ 0x46 /* TID Queue frozen. */
2122#define QHSTA_M_SGBACKUP_ERROR 0x47 /* Scatter-Gather backup error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124/* Return the address that is aligned at the next doubleword >= to 'addr'. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125#define ADV_32BALIGN(addr) (((ulong) (addr) + 0x1F) & ~0x1F)
2126
2127/*
2128 * Total contiguous memory needed for driver SG blocks.
2129 *
2130 * ADV_MAX_SG_LIST must be defined by a driver. It is the maximum
2131 * number of scatter-gather elements the driver supports in a
2132 * single request.
2133 */
2134
2135#define ADV_SG_LIST_MAX_BYTE_SIZE \
2136 (sizeof(ADV_SG_BLOCK) * \
2137 ((ADV_MAX_SG_LIST + (NO_OF_SG_PER_BLOCK - 1))/NO_OF_SG_PER_BLOCK))
2138
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002139/* struct asc_board flags */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002140#define ASC_IS_WIDE_BOARD 0x04 /* AdvanSys Wide Board */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142#define ASC_NARROW_BOARD(boardp) (((boardp)->flags & ASC_IS_WIDE_BOARD) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002144#define NO_ISA_DMA 0xff /* No ISA DMA Channel Used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002146#define ASC_INFO_SIZE 128 /* advansys_info() line size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/* Asc Library return codes */
2149#define ASC_TRUE 1
2150#define ASC_FALSE 0
2151#define ASC_NOERROR 1
2152#define ASC_BUSY 0
2153#define ASC_ERROR (-1)
2154
2155/* struct scsi_cmnd function return codes */
2156#define STATUS_BYTE(byte) (byte)
2157#define MSG_BYTE(byte) ((byte) << 8)
2158#define HOST_BYTE(byte) ((byte) << 16)
2159#define DRIVER_BYTE(byte) ((byte) << 24)
2160
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002161#define ASC_STATS(shost, counter) ASC_STATS_ADD(shost, counter, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#ifndef ADVANSYS_STATS
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002163#define ASC_STATS_ADD(shost, counter, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164#else /* ADVANSYS_STATS */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002165#define ASC_STATS_ADD(shost, counter, count) \
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002166 (((struct asc_board *) shost_priv(shost))->asc_stats.counter += (count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#endif /* ADVANSYS_STATS */
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169/* If the result wraps when calculating tenths, return 0. */
2170#define ASC_TENTHS(num, den) \
2171 (((10 * ((num)/(den))) > (((num) * 10)/(den))) ? \
2172 0 : ((((num) * 10)/(den)) - (10 * ((num)/(den)))))
2173
2174/*
2175 * Display a message to the console.
2176 */
2177#define ASC_PRINT(s) \
2178 { \
2179 printk("advansys: "); \
2180 printk(s); \
2181 }
2182
2183#define ASC_PRINT1(s, a1) \
2184 { \
2185 printk("advansys: "); \
2186 printk((s), (a1)); \
2187 }
2188
2189#define ASC_PRINT2(s, a1, a2) \
2190 { \
2191 printk("advansys: "); \
2192 printk((s), (a1), (a2)); \
2193 }
2194
2195#define ASC_PRINT3(s, a1, a2, a3) \
2196 { \
2197 printk("advansys: "); \
2198 printk((s), (a1), (a2), (a3)); \
2199 }
2200
2201#define ASC_PRINT4(s, a1, a2, a3, a4) \
2202 { \
2203 printk("advansys: "); \
2204 printk((s), (a1), (a2), (a3), (a4)); \
2205 }
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207#ifndef ADVANSYS_DEBUG
2208
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002209#define ASC_DBG(lvl, s...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210#define ASC_DBG_PRT_SCSI_HOST(lvl, s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211#define ASC_DBG_PRT_ASC_SCSI_Q(lvl, scsiqp)
2212#define ASC_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp)
2213#define ASC_DBG_PRT_ASC_QDONE_INFO(lvl, qdone)
2214#define ADV_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp)
2215#define ASC_DBG_PRT_HEX(lvl, name, start, length)
2216#define ASC_DBG_PRT_CDB(lvl, cdb, len)
2217#define ASC_DBG_PRT_SENSE(lvl, sense, len)
2218#define ASC_DBG_PRT_INQUIRY(lvl, inq, len)
2219
2220#else /* ADVANSYS_DEBUG */
2221
2222/*
2223 * Debugging Message Levels:
2224 * 0: Errors Only
2225 * 1: High-Level Tracing
2226 * 2-N: Verbose Tracing
2227 */
2228
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002229#define ASC_DBG(lvl, format, arg...) { \
2230 if (asc_dbglvl >= (lvl)) \
2231 printk(KERN_DEBUG "%s: %s: " format, DRV_NAME, \
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002232 __func__ , ## arg); \
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235#define ASC_DBG_PRT_SCSI_HOST(lvl, s) \
2236 { \
2237 if (asc_dbglvl >= (lvl)) { \
2238 asc_prt_scsi_host(s); \
2239 } \
2240 }
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#define ASC_DBG_PRT_ASC_SCSI_Q(lvl, scsiqp) \
2243 { \
2244 if (asc_dbglvl >= (lvl)) { \
2245 asc_prt_asc_scsi_q(scsiqp); \
2246 } \
2247 }
2248
2249#define ASC_DBG_PRT_ASC_QDONE_INFO(lvl, qdone) \
2250 { \
2251 if (asc_dbglvl >= (lvl)) { \
2252 asc_prt_asc_qdone_info(qdone); \
2253 } \
2254 }
2255
2256#define ASC_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp) \
2257 { \
2258 if (asc_dbglvl >= (lvl)) { \
2259 asc_prt_adv_scsi_req_q(scsiqp); \
2260 } \
2261 }
2262
2263#define ASC_DBG_PRT_HEX(lvl, name, start, length) \
2264 { \
2265 if (asc_dbglvl >= (lvl)) { \
2266 asc_prt_hex((name), (start), (length)); \
2267 } \
2268 }
2269
2270#define ASC_DBG_PRT_CDB(lvl, cdb, len) \
2271 ASC_DBG_PRT_HEX((lvl), "CDB", (uchar *) (cdb), (len));
2272
2273#define ASC_DBG_PRT_SENSE(lvl, sense, len) \
2274 ASC_DBG_PRT_HEX((lvl), "SENSE", (uchar *) (sense), (len));
2275
2276#define ASC_DBG_PRT_INQUIRY(lvl, inq, len) \
2277 ASC_DBG_PRT_HEX((lvl), "INQUIRY", (uchar *) (inq), (len));
2278#endif /* ADVANSYS_DEBUG */
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280#ifdef ADVANSYS_STATS
2281
2282/* Per board statistics structure */
2283struct asc_stats {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002284 /* Driver Entrypoint Statistics */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02002285 unsigned int queuecommand; /* # calls to advansys_queuecommand() */
2286 unsigned int reset; /* # calls to advansys_eh_bus_reset() */
2287 unsigned int biosparam; /* # calls to advansys_biosparam() */
2288 unsigned int interrupt; /* # advansys_interrupt() calls */
2289 unsigned int callback; /* # calls to asc/adv_isr_callback() */
2290 unsigned int done; /* # calls to request's scsi_done function */
2291 unsigned int build_error; /* # asc/adv_build_req() ASC_ERROR returns. */
2292 unsigned int adv_build_noreq; /* # adv_build_req() adv_req_t alloc. fail. */
2293 unsigned int adv_build_nosg; /* # adv_build_req() adv_sgblk_t alloc. fail. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002294 /* AscExeScsiQueue()/AdvExeScsiQueue() Statistics */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02002295 unsigned int exe_noerror; /* # ASC_NOERROR returns. */
2296 unsigned int exe_busy; /* # ASC_BUSY returns. */
2297 unsigned int exe_error; /* # ASC_ERROR returns. */
2298 unsigned int exe_unknown; /* # unknown returns. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002299 /* Data Transfer Statistics */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02002300 unsigned int xfer_cnt; /* # I/O requests received */
2301 unsigned int xfer_elem; /* # scatter-gather elements */
2302 unsigned int xfer_sect; /* # 512-byte blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303};
2304#endif /* ADVANSYS_STATS */
2305
2306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * Structure allocated for each board.
2308 *
Matthew Wilcox8dfb5372007-07-30 09:08:34 -06002309 * This structure is allocated by scsi_host_alloc() at the end
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 * of the 'Scsi_Host' structure starting at the 'hostdata'
2311 * field. It is guaranteed to be allocated from DMA-able memory.
2312 */
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002313struct asc_board {
Matthew Wilcox394dbf32007-07-26 11:56:40 -04002314 struct device *dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002315 struct Scsi_Host *shost;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002316 uint flags; /* Board flags */
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002317 unsigned int irq;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002318 union {
2319 ASC_DVC_VAR asc_dvc_var; /* Narrow board */
2320 ADV_DVC_VAR adv_dvc_var; /* Wide board */
2321 } dvc_var;
2322 union {
2323 ASC_DVC_CFG asc_dvc_cfg; /* Narrow board */
2324 ADV_DVC_CFG adv_dvc_cfg; /* Wide board */
2325 } dvc_cfg;
2326 ushort asc_n_io_port; /* Number I/O ports. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002327 ADV_SCSI_BIT_ID_TYPE init_tidmask; /* Target init./valid mask */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002328 ushort reqcnt[ADV_MAX_TID + 1]; /* Starvation request count */
2329 ADV_SCSI_BIT_ID_TYPE queue_full; /* Queue full mask */
2330 ushort queue_full_cnt[ADV_MAX_TID + 1]; /* Queue full count */
2331 union {
2332 ASCEEP_CONFIG asc_eep; /* Narrow EEPROM config. */
2333 ADVEEP_3550_CONFIG adv_3550_eep; /* 3550 EEPROM config. */
2334 ADVEEP_38C0800_CONFIG adv_38C0800_eep; /* 38C0800 EEPROM config. */
2335 ADVEEP_38C1600_CONFIG adv_38C1600_eep; /* 38C1600 EEPROM config. */
2336 } eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002337 /* /proc/scsi/advansys/[0...] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338#ifdef ADVANSYS_STATS
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002339 struct asc_stats asc_stats; /* Board statistics */
2340#endif /* ADVANSYS_STATS */
2341 /*
2342 * The following fields are used only for Narrow Boards.
2343 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002344 uchar sdtr_data[ASC_MAX_TID + 1]; /* SDTR information */
2345 /*
2346 * The following fields are used only for Wide Boards.
2347 */
2348 void __iomem *ioremap_addr; /* I/O Memory remap address. */
2349 ushort ioport; /* I/O Port address. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002350 adv_req_t *adv_reqp; /* Request structures. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02002351 dma_addr_t adv_reqp_addr;
2352 size_t adv_reqp_size;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002353 struct dma_pool *adv_sgblk_pool; /* Scatter-gather structures. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002354 ushort bios_signature; /* BIOS Signature. */
2355 ushort bios_version; /* BIOS Version. */
2356 ushort bios_codeseg; /* BIOS Code Segment. */
2357 ushort bios_codelen; /* BIOS Code Segment Length. */
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002358};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04002360#define asc_dvc_to_board(asc_dvc) container_of(asc_dvc, struct asc_board, \
2361 dvc_var.asc_dvc_var)
Matthew Wilcox13ac2d92007-07-30 08:10:23 -06002362#define adv_dvc_to_board(adv_dvc) container_of(adv_dvc, struct asc_board, \
2363 dvc_var.adv_dvc_var)
2364#define adv_dvc_to_pdev(adv_dvc) to_pci_dev(adv_dvc_to_board(adv_dvc)->dev)
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366#ifdef ADVANSYS_DEBUG
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002367static int asc_dbglvl = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/*
Matthew Wilcox51219352007-10-02 21:55:22 -04002370 * asc_prt_asc_dvc_var()
2371 */
2372static void asc_prt_asc_dvc_var(ASC_DVC_VAR *h)
2373{
2374 printk("ASC_DVC_VAR at addr 0x%lx\n", (ulong)h);
2375
2376 printk(" iop_base 0x%x, err_code 0x%x, dvc_cntl 0x%x, bug_fix_cntl "
2377 "%d,\n", h->iop_base, h->err_code, h->dvc_cntl, h->bug_fix_cntl);
2378
2379 printk(" bus_type %d, init_sdtr 0x%x,\n", h->bus_type,
2380 (unsigned)h->init_sdtr);
2381
2382 printk(" sdtr_done 0x%x, use_tagged_qng 0x%x, unit_not_ready 0x%x, "
2383 "chip_no 0x%x,\n", (unsigned)h->sdtr_done,
2384 (unsigned)h->use_tagged_qng, (unsigned)h->unit_not_ready,
2385 (unsigned)h->chip_no);
2386
2387 printk(" queue_full_or_busy 0x%x, start_motor 0x%x, scsi_reset_wait "
2388 "%u,\n", (unsigned)h->queue_full_or_busy,
2389 (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
2390
2391 printk(" is_in_int %u, max_total_qng %u, cur_total_qng %u, "
2392 "in_critical_cnt %u,\n", (unsigned)h->is_in_int,
2393 (unsigned)h->max_total_qng, (unsigned)h->cur_total_qng,
2394 (unsigned)h->in_critical_cnt);
2395
2396 printk(" last_q_shortage %u, init_state 0x%x, no_scam 0x%x, "
2397 "pci_fix_asyn_xfer 0x%x,\n", (unsigned)h->last_q_shortage,
2398 (unsigned)h->init_state, (unsigned)h->no_scam,
2399 (unsigned)h->pci_fix_asyn_xfer);
2400
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002401 printk(" cfg 0x%lx\n", (ulong)h->cfg);
Matthew Wilcox51219352007-10-02 21:55:22 -04002402}
2403
2404/*
2405 * asc_prt_asc_dvc_cfg()
2406 */
2407static void asc_prt_asc_dvc_cfg(ASC_DVC_CFG *h)
2408{
2409 printk("ASC_DVC_CFG at addr 0x%lx\n", (ulong)h);
2410
2411 printk(" can_tagged_qng 0x%x, cmd_qng_enabled 0x%x,\n",
2412 h->can_tagged_qng, h->cmd_qng_enabled);
2413 printk(" disc_enable 0x%x, sdtr_enable 0x%x,\n",
2414 h->disc_enable, h->sdtr_enable);
2415
Matthew Wilcoxb08fc562007-10-02 21:55:32 -04002416 printk(" chip_scsi_id %d, isa_dma_speed %d, isa_dma_channel %d, "
2417 "chip_version %d,\n", h->chip_scsi_id, h->isa_dma_speed,
2418 h->isa_dma_channel, h->chip_version);
Matthew Wilcox51219352007-10-02 21:55:22 -04002419
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04002420 printk(" mcode_date 0x%x, mcode_version %d\n",
2421 h->mcode_date, h->mcode_version);
Matthew Wilcox51219352007-10-02 21:55:22 -04002422}
2423
2424/*
Matthew Wilcox51219352007-10-02 21:55:22 -04002425 * asc_prt_adv_dvc_var()
2426 *
2427 * Display an ADV_DVC_VAR structure.
2428 */
2429static void asc_prt_adv_dvc_var(ADV_DVC_VAR *h)
2430{
2431 printk(" ADV_DVC_VAR at addr 0x%lx\n", (ulong)h);
2432
2433 printk(" iop_base 0x%lx, err_code 0x%x, ultra_able 0x%x\n",
2434 (ulong)h->iop_base, h->err_code, (unsigned)h->ultra_able);
2435
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002436 printk(" sdtr_able 0x%x, wdtr_able 0x%x\n",
2437 (unsigned)h->sdtr_able, (unsigned)h->wdtr_able);
Matthew Wilcox51219352007-10-02 21:55:22 -04002438
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002439 printk(" start_motor 0x%x, scsi_reset_wait 0x%x\n",
2440 (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
Matthew Wilcox51219352007-10-02 21:55:22 -04002441
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002442 printk(" max_host_qng %u, max_dvc_qng %u, carr_freelist 0x%p\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04002443 (unsigned)h->max_host_qng, (unsigned)h->max_dvc_qng,
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002444 h->carr_freelist);
Matthew Wilcox51219352007-10-02 21:55:22 -04002445
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002446 printk(" icq_sp 0x%p, irq_sp 0x%p\n", h->icq_sp, h->irq_sp);
Matthew Wilcox51219352007-10-02 21:55:22 -04002447
2448 printk(" no_scam 0x%x, tagqng_able 0x%x\n",
2449 (unsigned)h->no_scam, (unsigned)h->tagqng_able);
2450
2451 printk(" chip_scsi_id 0x%x, cfg 0x%lx\n",
2452 (unsigned)h->chip_scsi_id, (ulong)h->cfg);
2453}
2454
2455/*
2456 * asc_prt_adv_dvc_cfg()
2457 *
2458 * Display an ADV_DVC_CFG structure.
2459 */
2460static void asc_prt_adv_dvc_cfg(ADV_DVC_CFG *h)
2461{
2462 printk(" ADV_DVC_CFG at addr 0x%lx\n", (ulong)h);
2463
2464 printk(" disc_enable 0x%x, termination 0x%x\n",
2465 h->disc_enable, h->termination);
2466
2467 printk(" chip_version 0x%x, mcode_date 0x%x\n",
2468 h->chip_version, h->mcode_date);
2469
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002470 printk(" mcode_version 0x%x, control_flag 0x%x\n",
2471 h->mcode_version, h->control_flag);
Matthew Wilcox51219352007-10-02 21:55:22 -04002472}
2473
2474/*
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002475 * asc_prt_scsi_host()
Matthew Wilcox51219352007-10-02 21:55:22 -04002476 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002477static void asc_prt_scsi_host(struct Scsi_Host *s)
Matthew Wilcox51219352007-10-02 21:55:22 -04002478{
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002479 struct asc_board *boardp = shost_priv(s);
Matthew Wilcox51219352007-10-02 21:55:22 -04002480
Kay Sievers71610f52008-12-03 22:41:36 +01002481 printk("Scsi_Host at addr 0x%p, device %s\n", s, dev_name(boardp->dev));
Hannes Reinecke50d14a72013-10-23 10:51:17 +02002482 printk(" host_busy %u, host_no %d,\n",
Christoph Hellwig74665012014-01-22 15:29:29 +01002483 atomic_read(&s->host_busy), s->host_no);
Matthew Wilcox51219352007-10-02 21:55:22 -04002484
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002485 printk(" base 0x%lx, io_port 0x%lx, irq %d,\n",
2486 (ulong)s->base, (ulong)s->io_port, boardp->irq);
Matthew Wilcox51219352007-10-02 21:55:22 -04002487
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002488 printk(" dma_channel %d, this_id %d, can_queue %d,\n",
2489 s->dma_channel, s->this_id, s->can_queue);
Matthew Wilcox51219352007-10-02 21:55:22 -04002490
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002491 printk(" cmd_per_lun %d, sg_tablesize %d, unchecked_isa_dma %d\n",
2492 s->cmd_per_lun, s->sg_tablesize, s->unchecked_isa_dma);
Matthew Wilcox51219352007-10-02 21:55:22 -04002493
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002494 if (ASC_NARROW_BOARD(boardp)) {
2495 asc_prt_asc_dvc_var(&boardp->dvc_var.asc_dvc_var);
2496 asc_prt_asc_dvc_cfg(&boardp->dvc_cfg.asc_dvc_cfg);
2497 } else {
2498 asc_prt_adv_dvc_var(&boardp->dvc_var.adv_dvc_var);
2499 asc_prt_adv_dvc_cfg(&boardp->dvc_cfg.adv_dvc_cfg);
Matthew Wilcox51219352007-10-02 21:55:22 -04002500 }
2501}
2502
2503/*
2504 * asc_prt_hex()
2505 *
2506 * Print hexadecimal output in 4 byte groupings 32 bytes
2507 * or 8 double-words per line.
2508 */
2509static void asc_prt_hex(char *f, uchar *s, int l)
2510{
2511 int i;
2512 int j;
2513 int k;
2514 int m;
2515
2516 printk("%s: (%d bytes)\n", f, l);
2517
2518 for (i = 0; i < l; i += 32) {
2519
2520 /* Display a maximum of 8 double-words per line. */
2521 if ((k = (l - i) / 4) >= 8) {
2522 k = 8;
2523 m = 0;
2524 } else {
2525 m = (l - i) % 4;
2526 }
2527
2528 for (j = 0; j < k; j++) {
2529 printk(" %2.2X%2.2X%2.2X%2.2X",
2530 (unsigned)s[i + (j * 4)],
2531 (unsigned)s[i + (j * 4) + 1],
2532 (unsigned)s[i + (j * 4) + 2],
2533 (unsigned)s[i + (j * 4) + 3]);
2534 }
2535
2536 switch (m) {
2537 case 0:
2538 default:
2539 break;
2540 case 1:
2541 printk(" %2.2X", (unsigned)s[i + (j * 4)]);
2542 break;
2543 case 2:
2544 printk(" %2.2X%2.2X",
2545 (unsigned)s[i + (j * 4)],
2546 (unsigned)s[i + (j * 4) + 1]);
2547 break;
2548 case 3:
2549 printk(" %2.2X%2.2X%2.2X",
2550 (unsigned)s[i + (j * 4) + 1],
2551 (unsigned)s[i + (j * 4) + 2],
2552 (unsigned)s[i + (j * 4) + 3]);
2553 break;
2554 }
2555
2556 printk("\n");
2557 }
2558}
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002559
2560/*
2561 * asc_prt_asc_scsi_q()
2562 */
2563static void asc_prt_asc_scsi_q(ASC_SCSI_Q *q)
2564{
2565 ASC_SG_HEAD *sgp;
2566 int i;
2567
2568 printk("ASC_SCSI_Q at addr 0x%lx\n", (ulong)q);
2569
2570 printk
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002571 (" target_ix 0x%x, target_lun %u, srb_tag 0x%x, tag_code 0x%x,\n",
2572 q->q2.target_ix, q->q1.target_lun, q->q2.srb_tag,
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002573 q->q2.tag_code);
2574
2575 printk
2576 (" data_addr 0x%lx, data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2577 (ulong)le32_to_cpu(q->q1.data_addr),
2578 (ulong)le32_to_cpu(q->q1.data_cnt),
2579 (ulong)le32_to_cpu(q->q1.sense_addr), q->q1.sense_len);
2580
2581 printk(" cdbptr 0x%lx, cdb_len %u, sg_head 0x%lx, sg_queue_cnt %u\n",
2582 (ulong)q->cdbptr, q->q2.cdb_len,
2583 (ulong)q->sg_head, q->q1.sg_queue_cnt);
2584
2585 if (q->sg_head) {
2586 sgp = q->sg_head;
2587 printk("ASC_SG_HEAD at addr 0x%lx\n", (ulong)sgp);
2588 printk(" entry_cnt %u, queue_cnt %u\n", sgp->entry_cnt,
2589 sgp->queue_cnt);
2590 for (i = 0; i < sgp->entry_cnt; i++) {
2591 printk(" [%u]: addr 0x%lx, bytes %lu\n",
2592 i, (ulong)le32_to_cpu(sgp->sg_list[i].addr),
2593 (ulong)le32_to_cpu(sgp->sg_list[i].bytes));
2594 }
2595
2596 }
2597}
2598
2599/*
2600 * asc_prt_asc_qdone_info()
2601 */
2602static void asc_prt_asc_qdone_info(ASC_QDONE_INFO *q)
2603{
2604 printk("ASC_QDONE_INFO at addr 0x%lx\n", (ulong)q);
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002605 printk(" srb_tag 0x%x, target_ix %u, cdb_len %u, tag_code %u,\n",
2606 q->d2.srb_tag, q->d2.target_ix, q->d2.cdb_len,
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002607 q->d2.tag_code);
2608 printk
2609 (" done_stat 0x%x, host_stat 0x%x, scsi_stat 0x%x, scsi_msg 0x%x\n",
2610 q->d3.done_stat, q->d3.host_stat, q->d3.scsi_stat, q->d3.scsi_msg);
2611}
2612
2613/*
2614 * asc_prt_adv_sgblock()
2615 *
2616 * Display an ADV_SG_BLOCK structure.
2617 */
2618static void asc_prt_adv_sgblock(int sgblockno, ADV_SG_BLOCK *b)
2619{
2620 int i;
2621
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002622 printk(" ADV_SG_BLOCK at addr 0x%lx (sgblockno %d)\n",
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002623 (ulong)b, sgblockno);
2624 printk(" sg_cnt %u, sg_ptr 0x%lx\n",
2625 b->sg_cnt, (ulong)le32_to_cpu(b->sg_ptr));
2626 BUG_ON(b->sg_cnt > NO_OF_SG_PER_BLOCK);
2627 if (b->sg_ptr != 0)
2628 BUG_ON(b->sg_cnt != NO_OF_SG_PER_BLOCK);
2629 for (i = 0; i < b->sg_cnt; i++) {
2630 printk(" [%u]: sg_addr 0x%lx, sg_count 0x%lx\n",
2631 i, (ulong)b->sg_list[i].sg_addr,
2632 (ulong)b->sg_list[i].sg_count);
2633 }
2634}
2635
2636/*
2637 * asc_prt_adv_scsi_req_q()
2638 *
2639 * Display an ADV_SCSI_REQ_Q structure.
2640 */
2641static void asc_prt_adv_scsi_req_q(ADV_SCSI_REQ_Q *q)
2642{
2643 int sg_blk_cnt;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002644 struct adv_sg_block *sg_ptr;
2645 adv_sgblk_t *sgblkp;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002646
2647 printk("ADV_SCSI_REQ_Q at addr 0x%lx\n", (ulong)q);
2648
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002649 printk(" target_id %u, target_lun %u, srb_tag 0x%x, a_flag 0x%x\n",
2650 q->target_id, q->target_lun, q->srb_tag, q->a_flag);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002651
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002652 printk(" cntl 0x%x, data_addr 0x%lx\n",
2653 q->cntl, (ulong)le32_to_cpu(q->data_addr));
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002654
2655 printk(" data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2656 (ulong)le32_to_cpu(q->data_cnt),
2657 (ulong)le32_to_cpu(q->sense_addr), q->sense_len);
2658
2659 printk
2660 (" cdb_len %u, done_status 0x%x, host_status 0x%x, scsi_status 0x%x\n",
2661 q->cdb_len, q->done_status, q->host_status, q->scsi_status);
2662
2663 printk(" sg_working_ix 0x%x, target_cmd %u\n",
2664 q->sg_working_ix, q->target_cmd);
2665
2666 printk(" scsiq_rptr 0x%lx, sg_real_addr 0x%lx, sg_list_ptr 0x%lx\n",
2667 (ulong)le32_to_cpu(q->scsiq_rptr),
2668 (ulong)le32_to_cpu(q->sg_real_addr), (ulong)q->sg_list_ptr);
2669
2670 /* Display the request's ADV_SG_BLOCK structures. */
2671 if (q->sg_list_ptr != NULL) {
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002672 sgblkp = container_of(q->sg_list_ptr, adv_sgblk_t, sg_block);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002673 sg_blk_cnt = 0;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002674 while (sgblkp) {
2675 sg_ptr = &sgblkp->sg_block;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002676 asc_prt_adv_sgblock(sg_blk_cnt, sg_ptr);
2677 if (sg_ptr->sg_ptr == 0) {
2678 break;
2679 }
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002680 sgblkp = sgblkp->next_sgblkp;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002681 sg_blk_cnt++;
2682 }
2683 }
2684}
Matthew Wilcox51219352007-10-02 21:55:22 -04002685#endif /* ADVANSYS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 * advansys_info()
2689 *
2690 * Return suitable for printing on the console with the argument
2691 * adapter's configuration information.
2692 *
2693 * Note: The information line should not exceed ASC_INFO_SIZE bytes,
2694 * otherwise the static 'info' array will be overrun.
2695 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002696static const char *advansys_info(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002698 static char info[ASC_INFO_SIZE];
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002699 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002700 ASC_DVC_VAR *asc_dvc_varp;
2701 ADV_DVC_VAR *adv_dvc_varp;
2702 char *busname;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002703 char *widename = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002705 if (ASC_NARROW_BOARD(boardp)) {
2706 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002707 ASC_DBG(1, "begin\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002708 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
2709 if ((asc_dvc_varp->bus_type & ASC_IS_ISAPNP) ==
2710 ASC_IS_ISAPNP) {
2711 busname = "ISA PnP";
2712 } else {
2713 busname = "ISA";
2714 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002715 sprintf(info,
2716 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X, DMA 0x%X",
2717 ASC_VERSION, busname,
2718 (ulong)shost->io_port,
Matthew Wilcox4a2d31c2007-07-26 11:55:34 -04002719 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002720 boardp->irq, shost->dma_channel);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002721 } else {
2722 if (asc_dvc_varp->bus_type & ASC_IS_VL) {
2723 busname = "VL";
2724 } else if (asc_dvc_varp->bus_type & ASC_IS_EISA) {
2725 busname = "EISA";
2726 } else if (asc_dvc_varp->bus_type & ASC_IS_PCI) {
2727 if ((asc_dvc_varp->bus_type & ASC_IS_PCI_ULTRA)
2728 == ASC_IS_PCI_ULTRA) {
2729 busname = "PCI Ultra";
2730 } else {
2731 busname = "PCI";
2732 }
2733 } else {
2734 busname = "?";
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04002735 shost_printk(KERN_ERR, shost, "unknown bus "
2736 "type %d\n", asc_dvc_varp->bus_type);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002737 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002738 sprintf(info,
2739 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X",
Matthew Wilcoxecec1942007-07-30 08:08:22 -06002740 ASC_VERSION, busname, (ulong)shost->io_port,
Matthew Wilcox4a2d31c2007-07-26 11:55:34 -04002741 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002742 boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002743 }
2744 } else {
2745 /*
2746 * Wide Adapter Information
2747 *
2748 * Memory-mapped I/O is used instead of I/O space to access
2749 * the adapter, but display the I/O Port range. The Memory
2750 * I/O address is displayed through the driver /proc file.
2751 */
2752 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
2753 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002754 widename = "Ultra-Wide";
2755 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002756 widename = "Ultra2-Wide";
2757 } else {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002758 widename = "Ultra3-Wide";
2759 }
2760 sprintf(info,
2761 "AdvanSys SCSI %s: PCI %s: PCIMEM 0x%lX-0x%lX, IRQ 0x%X",
2762 ASC_VERSION, widename, (ulong)adv_dvc_varp->iop_base,
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002763 (ulong)adv_dvc_varp->iop_base + boardp->asc_n_io_port - 1, boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002764 }
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06002765 BUG_ON(strlen(info) >= ASC_INFO_SIZE);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002766 ASC_DBG(1, "end\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002767 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
Matthew Wilcox51219352007-10-02 21:55:22 -04002770#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * asc_prt_board_devices()
2774 *
2775 * Print driver information for devices attached to the board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 */
Al Virob59fb6f2013-03-31 02:59:55 -04002777static void asc_prt_board_devices(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002779 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002780 int chip_scsi_id;
2781 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Al Virob59fb6f2013-03-31 02:59:55 -04002783 seq_printf(m,
2784 "\nDevice Information for AdvanSys SCSI Host %d:\n",
2785 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002787 if (ASC_NARROW_BOARD(boardp)) {
2788 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
2789 } else {
2790 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002793 seq_puts(m, "Target IDs Detected:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002794 for (i = 0; i <= ADV_MAX_TID; i++) {
Al Virob59fb6f2013-03-31 02:59:55 -04002795 if (boardp->init_tidmask & ADV_TID_TO_TIDMASK(i))
2796 seq_printf(m, " %X,", i);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002797 }
Al Virob59fb6f2013-03-31 02:59:55 -04002798 seq_printf(m, " (%X=Host Adapter)\n", chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
2801/*
2802 * Display Wide Board BIOS Information.
2803 */
Al Virob59fb6f2013-03-31 02:59:55 -04002804static void asc_prt_adv_bios(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002806 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002807 ushort major, minor, letter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002809 seq_puts(m, "\nROM BIOS Version: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002811 /*
2812 * If the BIOS saved a valid signature, then fill in
2813 * the BIOS code segment base address.
2814 */
2815 if (boardp->bios_signature != 0x55AA) {
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002816 seq_puts(m, "Disabled or Pre-3.1\n"
2817 "BIOS either disabled or Pre-3.1. If it is pre-3.1, then a newer version\n"
2818 "can be found at the ConnectCom FTP site: ftp://ftp.connectcom.net/pub\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002819 } else {
2820 major = (boardp->bios_version >> 12) & 0xF;
2821 minor = (boardp->bios_version >> 8) & 0xF;
2822 letter = (boardp->bios_version & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Al Virob59fb6f2013-03-31 02:59:55 -04002824 seq_printf(m, "%d.%d%c\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002825 major, minor,
2826 letter >= 26 ? '?' : letter + 'A');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002827 /*
2828 * Current available ROM BIOS release is 3.1I for UW
2829 * and 3.2I for U2W. This code doesn't differentiate
2830 * UW and U2W boards.
2831 */
2832 if (major < 3 || (major <= 3 && minor < 1) ||
2833 (major <= 3 && minor <= 1 && letter < ('I' - 'A'))) {
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002834 seq_puts(m, "Newer version of ROM BIOS is available at the ConnectCom FTP site:\n"
2835 "ftp://ftp.connectcom.net/pub\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002836 }
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840/*
2841 * Add serial number to information bar if signature AAh
2842 * is found in at bit 15-9 (7 bits) of word 1.
2843 *
2844 * Serial Number consists fo 12 alpha-numeric digits.
2845 *
2846 * 1 - Product type (A,B,C,D..) Word0: 15-13 (3 bits)
2847 * 2 - MFG Location (A,B,C,D..) Word0: 12-10 (3 bits)
2848 * 3-4 - Product ID (0-99) Word0: 9-0 (10 bits)
2849 * 5 - Product revision (A-J) Word0: " "
2850 *
2851 * Signature Word1: 15-9 (7 bits)
2852 * 6 - Year (0-9) Word1: 8-6 (3 bits) & Word2: 15 (1 bit)
2853 * 7-8 - Week of the year (1-52) Word1: 5-0 (6 bits)
2854 *
2855 * 9-12 - Serial Number (A001-Z999) Word2: 14-0 (15 bits)
2856 *
2857 * Note 1: Only production cards will have a serial number.
2858 *
2859 * Note 2: Signature is most significant 7 bits (0xFE).
2860 *
2861 * Returns ASC_TRUE if serial number found, otherwise returns ASC_FALSE.
2862 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002863static int asc_get_eeprom_string(ushort *serialnum, uchar *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002865 ushort w, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002867 if ((serialnum[1] & 0xFE00) != ((ushort)0xAA << 8)) {
2868 return ASC_FALSE;
2869 } else {
2870 /*
2871 * First word - 6 digits.
2872 */
2873 w = serialnum[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002875 /* Product type - 1st digit. */
2876 if ((*cp = 'A' + ((w & 0xE000) >> 13)) == 'H') {
2877 /* Product type is P=Prototype */
2878 *cp += 0x8;
2879 }
2880 cp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002882 /* Manufacturing location - 2nd digit. */
2883 *cp++ = 'A' + ((w & 0x1C00) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002885 /* Product ID - 3rd, 4th digits. */
2886 num = w & 0x3FF;
2887 *cp++ = '0' + (num / 100);
2888 num %= 100;
2889 *cp++ = '0' + (num / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002891 /* Product revision - 5th digit. */
2892 *cp++ = 'A' + (num % 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002894 /*
2895 * Second word
2896 */
2897 w = serialnum[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002899 /*
2900 * Year - 6th digit.
2901 *
2902 * If bit 15 of third word is set, then the
2903 * last digit of the year is greater than 7.
2904 */
2905 if (serialnum[2] & 0x8000) {
2906 *cp++ = '8' + ((w & 0x1C0) >> 6);
2907 } else {
2908 *cp++ = '0' + ((w & 0x1C0) >> 6);
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002911 /* Week of year - 7th, 8th digits. */
2912 num = w & 0x003F;
2913 *cp++ = '0' + num / 10;
2914 num %= 10;
2915 *cp++ = '0' + num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002917 /*
2918 * Third word
2919 */
2920 w = serialnum[2] & 0x7FFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002922 /* Serial number - 9th digit. */
2923 *cp++ = 'A' + (w / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002925 /* 10th, 11th, 12th digits. */
2926 num = w % 1000;
2927 *cp++ = '0' + num / 100;
2928 num %= 100;
2929 *cp++ = '0' + num / 10;
2930 num %= 10;
2931 *cp++ = '0' + num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002933 *cp = '\0'; /* Null Terminate the string. */
2934 return ASC_TRUE;
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
2938/*
2939 * asc_prt_asc_board_eeprom()
2940 *
2941 * Print board EEPROM configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 */
Al Virob59fb6f2013-03-31 02:59:55 -04002943static void asc_prt_asc_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002945 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002946 ASC_DVC_VAR *asc_dvc_varp;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002947 ASCEEP_CONFIG *ep;
2948 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002950 int isa_dma_speed[] = { 10, 8, 7, 6, 5, 4, 3, 2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951#endif /* CONFIG_ISA */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002952 uchar serialstr[13];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002954 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
2955 ep = &boardp->eep_config.asc_eep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Al Virob59fb6f2013-03-31 02:59:55 -04002957 seq_printf(m,
2958 "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
2959 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002961 if (asc_get_eeprom_string((ushort *)&ep->adapter_info[0], serialstr)
Al Virob59fb6f2013-03-31 02:59:55 -04002962 == ASC_TRUE)
2963 seq_printf(m, " Serial Number: %s\n", serialstr);
2964 else if (ep->adapter_info[5] == 0xBB)
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002965 seq_puts(m,
2966 " Default Settings Used for EEPROM-less Adapter.\n");
Al Virob59fb6f2013-03-31 02:59:55 -04002967 else
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002968 seq_puts(m, " Serial Number Signature Not Present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Al Virob59fb6f2013-03-31 02:59:55 -04002970 seq_printf(m,
2971 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
2972 ASC_EEP_GET_CHIP_ID(ep), ep->max_total_qng,
2973 ep->max_tag_qng);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Al Virob59fb6f2013-03-31 02:59:55 -04002975 seq_printf(m,
2976 " cntl 0x%x, no_scam 0x%x\n", ep->cntl, ep->no_scam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002978 seq_puts(m, " Target ID: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002979 for (i = 0; i <= ASC_MAX_TID; i++)
2980 seq_printf(m, " %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002982 seq_puts(m, "\n Disconnects: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002983 for (i = 0; i <= ASC_MAX_TID; i++)
2984 seq_printf(m, " %c",
2985 (ep->disc_enable & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002987 seq_puts(m, "\n Command Queuing: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002988 for (i = 0; i <= ASC_MAX_TID; i++)
2989 seq_printf(m, " %c",
2990 (ep->use_cmd_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002992 seq_puts(m, "\n Start Motor: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002993 for (i = 0; i <= ASC_MAX_TID; i++)
2994 seq_printf(m, " %c",
2995 (ep->start_motor & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002997 seq_puts(m, "\n Synchronous Transfer:");
Al Virob59fb6f2013-03-31 02:59:55 -04002998 for (i = 0; i <= ASC_MAX_TID; i++)
2999 seq_printf(m, " %c",
3000 (ep->init_sdtr & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003001 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003004 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
Al Virob59fb6f2013-03-31 02:59:55 -04003005 seq_printf(m,
3006 " Host ISA DMA speed: %d MB/S\n",
3007 isa_dma_speed[ASC_EEP_GET_DMA_SPD(ep)]);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
3012/*
3013 * asc_prt_adv_board_eeprom()
3014 *
3015 * Print board EEPROM configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 */
Al Virob59fb6f2013-03-31 02:59:55 -04003017static void asc_prt_adv_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003019 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003020 ADV_DVC_VAR *adv_dvc_varp;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003021 int i;
3022 char *termstr;
3023 uchar serialstr[13];
3024 ADVEEP_3550_CONFIG *ep_3550 = NULL;
3025 ADVEEP_38C0800_CONFIG *ep_38C0800 = NULL;
3026 ADVEEP_38C1600_CONFIG *ep_38C1600 = NULL;
3027 ushort word;
3028 ushort *wordp;
3029 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003031 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
3032 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3033 ep_3550 = &boardp->eep_config.adv_3550_eep;
3034 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3035 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
3036 } else {
3037 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
3038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Al Virob59fb6f2013-03-31 02:59:55 -04003040 seq_printf(m,
3041 "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
3042 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003044 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3045 wordp = &ep_3550->serial_number_word1;
3046 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3047 wordp = &ep_38C0800->serial_number_word1;
3048 } else {
3049 wordp = &ep_38C1600->serial_number_word1;
3050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Al Virob59fb6f2013-03-31 02:59:55 -04003052 if (asc_get_eeprom_string(wordp, serialstr) == ASC_TRUE)
3053 seq_printf(m, " Serial Number: %s\n", serialstr);
3054 else
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003055 seq_puts(m, " Serial Number Signature Not Present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Al Virob59fb6f2013-03-31 02:59:55 -04003057 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3058 seq_printf(m,
3059 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3060 ep_3550->adapter_scsi_id,
3061 ep_3550->max_host_qng, ep_3550->max_dvc_qng);
3062 else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3063 seq_printf(m,
3064 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3065 ep_38C0800->adapter_scsi_id,
3066 ep_38C0800->max_host_qng,
3067 ep_38C0800->max_dvc_qng);
3068 else
3069 seq_printf(m,
3070 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3071 ep_38C1600->adapter_scsi_id,
3072 ep_38C1600->max_host_qng,
3073 ep_38C1600->max_dvc_qng);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003074 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3075 word = ep_3550->termination;
3076 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3077 word = ep_38C0800->termination_lvd;
3078 } else {
3079 word = ep_38C1600->termination_lvd;
3080 }
3081 switch (word) {
3082 case 1:
3083 termstr = "Low Off/High Off";
3084 break;
3085 case 2:
3086 termstr = "Low Off/High On";
3087 break;
3088 case 3:
3089 termstr = "Low On/High On";
3090 break;
3091 default:
3092 case 0:
3093 termstr = "Automatic";
3094 break;
3095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Al Virob59fb6f2013-03-31 02:59:55 -04003097 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3098 seq_printf(m,
3099 " termination: %u (%s), bios_ctrl: 0x%x\n",
3100 ep_3550->termination, termstr,
3101 ep_3550->bios_ctrl);
3102 else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3103 seq_printf(m,
3104 " termination: %u (%s), bios_ctrl: 0x%x\n",
3105 ep_38C0800->termination_lvd, termstr,
3106 ep_38C0800->bios_ctrl);
3107 else
3108 seq_printf(m,
3109 " termination: %u (%s), bios_ctrl: 0x%x\n",
3110 ep_38C1600->termination_lvd, termstr,
3111 ep_38C1600->bios_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003113 seq_puts(m, " Target ID: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003114 for (i = 0; i <= ADV_MAX_TID; i++)
3115 seq_printf(m, " %X", i);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003116 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003118 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3119 word = ep_3550->disc_enable;
3120 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3121 word = ep_38C0800->disc_enable;
3122 } else {
3123 word = ep_38C1600->disc_enable;
3124 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003125 seq_puts(m, " Disconnects: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003126 for (i = 0; i <= ADV_MAX_TID; i++)
3127 seq_printf(m, " %c",
3128 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003129 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003131 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3132 word = ep_3550->tagqng_able;
3133 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3134 word = ep_38C0800->tagqng_able;
3135 } else {
3136 word = ep_38C1600->tagqng_able;
3137 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003138 seq_puts(m, " Command Queuing: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003139 for (i = 0; i <= ADV_MAX_TID; i++)
3140 seq_printf(m, " %c",
3141 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003142 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003144 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3145 word = ep_3550->start_motor;
3146 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3147 word = ep_38C0800->start_motor;
3148 } else {
3149 word = ep_38C1600->start_motor;
3150 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003151 seq_puts(m, " Start Motor: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003152 for (i = 0; i <= ADV_MAX_TID; i++)
3153 seq_printf(m, " %c",
3154 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003155 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003157 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003158 seq_puts(m, " Synchronous Transfer:");
Al Virob59fb6f2013-03-31 02:59:55 -04003159 for (i = 0; i <= ADV_MAX_TID; i++)
3160 seq_printf(m, " %c",
3161 (ep_3550->sdtr_able & ADV_TID_TO_TIDMASK(i)) ?
3162 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003163 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003166 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003167 seq_puts(m, " Ultra Transfer: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003168 for (i = 0; i <= ADV_MAX_TID; i++)
3169 seq_printf(m, " %c",
3170 (ep_3550->ultra_able & ADV_TID_TO_TIDMASK(i))
3171 ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003172 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003175 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3176 word = ep_3550->wdtr_able;
3177 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3178 word = ep_38C0800->wdtr_able;
3179 } else {
3180 word = ep_38C1600->wdtr_able;
3181 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003182 seq_puts(m, " Wide Transfer: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003183 for (i = 0; i <= ADV_MAX_TID; i++)
3184 seq_printf(m, " %c",
3185 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003186 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003188 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800 ||
3189 adv_dvc_varp->chip_type == ADV_CHIP_ASC38C1600) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003190 seq_puts(m, " Synchronous Transfer Speed (Mhz):\n ");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003191 for (i = 0; i <= ADV_MAX_TID; i++) {
3192 char *speed_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003194 if (i == 0) {
3195 sdtr_speed = adv_dvc_varp->sdtr_speed1;
3196 } else if (i == 4) {
3197 sdtr_speed = adv_dvc_varp->sdtr_speed2;
3198 } else if (i == 8) {
3199 sdtr_speed = adv_dvc_varp->sdtr_speed3;
3200 } else if (i == 12) {
3201 sdtr_speed = adv_dvc_varp->sdtr_speed4;
3202 }
3203 switch (sdtr_speed & ADV_MAX_TID) {
3204 case 0:
3205 speed_str = "Off";
3206 break;
3207 case 1:
3208 speed_str = " 5";
3209 break;
3210 case 2:
3211 speed_str = " 10";
3212 break;
3213 case 3:
3214 speed_str = " 20";
3215 break;
3216 case 4:
3217 speed_str = " 40";
3218 break;
3219 case 5:
3220 speed_str = " 80";
3221 break;
3222 default:
3223 speed_str = "Unk";
3224 break;
3225 }
Al Virob59fb6f2013-03-31 02:59:55 -04003226 seq_printf(m, "%X:%s ", i, speed_str);
3227 if (i == 7)
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003228 seq_puts(m, "\n ");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003229 sdtr_speed >>= 4;
3230 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003231 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
3235/*
3236 * asc_prt_driver_conf()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 */
Al Virob59fb6f2013-03-31 02:59:55 -04003238static void asc_prt_driver_conf(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003240 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003241 int chip_scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Al Virob59fb6f2013-03-31 02:59:55 -04003243 seq_printf(m,
3244 "\nLinux Driver Configuration and Information for AdvanSys SCSI Host %d:\n",
3245 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Al Virob59fb6f2013-03-31 02:59:55 -04003247 seq_printf(m,
Hannes Reinecke1abf6352014-06-25 15:27:38 +02003248 " host_busy %u, max_id %u, max_lun %llu, max_channel %u\n",
Christoph Hellwig74665012014-01-22 15:29:29 +01003249 atomic_read(&shost->host_busy), shost->max_id,
Al Virob59fb6f2013-03-31 02:59:55 -04003250 shost->max_lun, shost->max_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Al Virob59fb6f2013-03-31 02:59:55 -04003252 seq_printf(m,
3253 " unique_id %d, can_queue %d, this_id %d, sg_tablesize %u, cmd_per_lun %u\n",
3254 shost->unique_id, shost->can_queue, shost->this_id,
3255 shost->sg_tablesize, shost->cmd_per_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Al Virob59fb6f2013-03-31 02:59:55 -04003257 seq_printf(m,
3258 " unchecked_isa_dma %d, use_clustering %d\n",
3259 shost->unchecked_isa_dma, shost->use_clustering);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Al Virob59fb6f2013-03-31 02:59:55 -04003261 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003262 " flags 0x%x, last_reset 0x%lx, jiffies 0x%lx, asc_n_io_port 0x%x\n",
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02003263 boardp->flags, shost->last_reset, jiffies,
Al Virob59fb6f2013-03-31 02:59:55 -04003264 boardp->asc_n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Al Viro31491e12013-03-31 03:04:13 -04003266 seq_printf(m, " io_port 0x%lx\n", shost->io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003268 if (ASC_NARROW_BOARD(boardp)) {
3269 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
3270 } else {
3271 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275/*
3276 * asc_prt_asc_board_info()
3277 *
3278 * Print dynamic board configuration information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 */
Al Virob59fb6f2013-03-31 02:59:55 -04003280static void asc_prt_asc_board_info(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003282 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003283 int chip_scsi_id;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003284 ASC_DVC_VAR *v;
3285 ASC_DVC_CFG *c;
3286 int i;
3287 int renegotiate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003289 v = &boardp->dvc_var.asc_dvc_var;
3290 c = &boardp->dvc_cfg.asc_dvc_cfg;
3291 chip_scsi_id = c->chip_scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Al Virob59fb6f2013-03-31 02:59:55 -04003293 seq_printf(m,
3294 "\nAsc Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3295 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Al Virob59fb6f2013-03-31 02:59:55 -04003297 seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3298 "mcode_version 0x%x, err_code %u\n",
3299 c->chip_version, c->mcode_date, c->mcode_version,
3300 v->err_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003302 /* Current number of commands waiting for the host. */
Al Virob59fb6f2013-03-31 02:59:55 -04003303 seq_printf(m,
3304 " Total Command Pending: %d\n", v->cur_total_qng);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003306 seq_puts(m, " Command Queuing:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003307 for (i = 0; i <= ASC_MAX_TID; i++) {
3308 if ((chip_scsi_id == i) ||
3309 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3310 continue;
3311 }
Al Virob59fb6f2013-03-31 02:59:55 -04003312 seq_printf(m, " %X:%c",
3313 i,
3314 (v->use_tagged_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003317 /* Current number of commands waiting for a device. */
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003318 seq_puts(m, "\n Command Queue Pending:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003319 for (i = 0; i <= ASC_MAX_TID; i++) {
3320 if ((chip_scsi_id == i) ||
3321 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3322 continue;
3323 }
Al Virob59fb6f2013-03-31 02:59:55 -04003324 seq_printf(m, " %X:%u", i, v->cur_dvc_qng[i]);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003327 /* Current limit on number of commands that can be sent to a device. */
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003328 seq_puts(m, "\n Command Queue Limit:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003329 for (i = 0; i <= ASC_MAX_TID; i++) {
3330 if ((chip_scsi_id == i) ||
3331 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3332 continue;
3333 }
Al Virob59fb6f2013-03-31 02:59:55 -04003334 seq_printf(m, " %X:%u", i, v->max_dvc_qng[i]);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003337 /* Indicate whether the device has returned queue full status. */
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003338 seq_puts(m, "\n Command Queue Full:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003339 for (i = 0; i <= ASC_MAX_TID; i++) {
3340 if ((chip_scsi_id == i) ||
3341 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3342 continue;
3343 }
Al Virob59fb6f2013-03-31 02:59:55 -04003344 if (boardp->queue_full & ADV_TID_TO_TIDMASK(i))
3345 seq_printf(m, " %X:Y-%d",
3346 i, boardp->queue_full_cnt[i]);
3347 else
3348 seq_printf(m, " %X:N", i);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003351 seq_puts(m, "\n Synchronous Transfer:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003352 for (i = 0; i <= ASC_MAX_TID; i++) {
3353 if ((chip_scsi_id == i) ||
3354 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3355 continue;
3356 }
Al Virob59fb6f2013-03-31 02:59:55 -04003357 seq_printf(m, " %X:%c",
3358 i,
3359 (v->sdtr_done & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003360 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003361 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003363 for (i = 0; i <= ASC_MAX_TID; i++) {
3364 uchar syn_period_ix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003366 if ((chip_scsi_id == i) ||
3367 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3368 ((v->init_sdtr & ADV_TID_TO_TIDMASK(i)) == 0)) {
3369 continue;
3370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Al Virob59fb6f2013-03-31 02:59:55 -04003372 seq_printf(m, " %X:", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003374 if ((boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET) == 0) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003375 seq_puts(m, " Asynchronous");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003376 } else {
3377 syn_period_ix =
3378 (boardp->sdtr_data[i] >> 4) & (v->max_sdtr_index -
3379 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Al Virob59fb6f2013-03-31 02:59:55 -04003381 seq_printf(m,
3382 " Transfer Period Factor: %d (%d.%d Mhz),",
3383 v->sdtr_period_tbl[syn_period_ix],
3384 250 / v->sdtr_period_tbl[syn_period_ix],
3385 ASC_TENTHS(250,
3386 v->sdtr_period_tbl[syn_period_ix]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Al Virob59fb6f2013-03-31 02:59:55 -04003388 seq_printf(m, " REQ/ACK Offset: %d",
3389 boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003392 if ((v->sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003393 seq_puts(m, "*\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003394 renegotiate = 1;
3395 } else {
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003396 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003397 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003400 if (renegotiate) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003401 seq_puts(m, " * = Re-negotiation pending before next command.\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405/*
3406 * asc_prt_adv_board_info()
3407 *
3408 * Print dynamic board configuration information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 */
Al Virob59fb6f2013-03-31 02:59:55 -04003410static void asc_prt_adv_board_info(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003412 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003413 int i;
3414 ADV_DVC_VAR *v;
3415 ADV_DVC_CFG *c;
3416 AdvPortAddr iop_base;
3417 ushort chip_scsi_id;
3418 ushort lramword;
3419 uchar lrambyte;
3420 ushort tagqng_able;
3421 ushort sdtr_able, wdtr_able;
3422 ushort wdtr_done, sdtr_done;
3423 ushort period = 0;
3424 int renegotiate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003426 v = &boardp->dvc_var.adv_dvc_var;
3427 c = &boardp->dvc_cfg.adv_dvc_cfg;
3428 iop_base = v->iop_base;
3429 chip_scsi_id = v->chip_scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Al Virob59fb6f2013-03-31 02:59:55 -04003431 seq_printf(m,
3432 "\nAdv Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3433 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Al Virob59fb6f2013-03-31 02:59:55 -04003435 seq_printf(m,
3436 " iop_base 0x%lx, cable_detect: %X, err_code %u\n",
Al Viro31491e12013-03-31 03:04:13 -04003437 (unsigned long)v->iop_base,
Al Virob59fb6f2013-03-31 02:59:55 -04003438 AdvReadWordRegister(iop_base,IOPW_SCSI_CFG1) & CABLE_DETECT,
3439 v->err_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Al Virob59fb6f2013-03-31 02:59:55 -04003441 seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3442 "mcode_version 0x%x\n", c->chip_version,
3443 c->mcode_date, c->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003445 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003446 seq_puts(m, " Queuing Enabled:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003447 for (i = 0; i <= ADV_MAX_TID; i++) {
3448 if ((chip_scsi_id == i) ||
3449 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3450 continue;
3451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Al Virob59fb6f2013-03-31 02:59:55 -04003453 seq_printf(m, " %X:%c",
3454 i,
3455 (tagqng_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003458 seq_puts(m, "\n Queue Limit:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003459 for (i = 0; i <= ADV_MAX_TID; i++) {
3460 if ((chip_scsi_id == i) ||
3461 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3462 continue;
3463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003465 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + i,
3466 lrambyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Al Virob59fb6f2013-03-31 02:59:55 -04003468 seq_printf(m, " %X:%d", i, lrambyte);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003471 seq_puts(m, "\n Command Pending:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003472 for (i = 0; i <= ADV_MAX_TID; i++) {
3473 if ((chip_scsi_id == i) ||
3474 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3475 continue;
3476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003478 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_QUEUED_CMD + i,
3479 lrambyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Al Virob59fb6f2013-03-31 02:59:55 -04003481 seq_printf(m, " %X:%d", i, lrambyte);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003482 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003483 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003485 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003486 seq_puts(m, " Wide Enabled:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003487 for (i = 0; i <= ADV_MAX_TID; i++) {
3488 if ((chip_scsi_id == i) ||
3489 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3490 continue;
3491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Al Virob59fb6f2013-03-31 02:59:55 -04003493 seq_printf(m, " %X:%c",
3494 i,
3495 (wdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003496 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003497 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003499 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, wdtr_done);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003500 seq_puts(m, " Transfer Bit Width:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003501 for (i = 0; i <= ADV_MAX_TID; i++) {
3502 if ((chip_scsi_id == i) ||
3503 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3504 continue;
3505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003507 AdvReadWordLram(iop_base,
3508 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3509 lramword);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Al Virob59fb6f2013-03-31 02:59:55 -04003511 seq_printf(m, " %X:%d",
3512 i, (lramword & 0x8000) ? 16 : 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003514 if ((wdtr_able & ADV_TID_TO_TIDMASK(i)) &&
3515 (wdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003516 seq_putc(m, '*');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003517 renegotiate = 1;
3518 }
3519 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003520 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003522 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003523 seq_puts(m, " Synchronous Enabled:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003524 for (i = 0; i <= ADV_MAX_TID; i++) {
3525 if ((chip_scsi_id == i) ||
3526 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3527 continue;
3528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Al Virob59fb6f2013-03-31 02:59:55 -04003530 seq_printf(m, " %X:%c",
3531 i,
3532 (sdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003533 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003534 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003536 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, sdtr_done);
3537 for (i = 0; i <= ADV_MAX_TID; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003539 AdvReadWordLram(iop_base,
3540 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3541 lramword);
3542 lramword &= ~0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003544 if ((chip_scsi_id == i) ||
3545 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3546 ((sdtr_able & ADV_TID_TO_TIDMASK(i)) == 0)) {
3547 continue;
3548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Al Virob59fb6f2013-03-31 02:59:55 -04003550 seq_printf(m, " %X:", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003552 if ((lramword & 0x1F) == 0) { /* Check for REQ/ACK Offset 0. */
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003553 seq_puts(m, " Asynchronous");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003554 } else {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003555 seq_puts(m, " Transfer Period Factor: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003557 if ((lramword & 0x1F00) == 0x1100) { /* 80 Mhz */
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003558 seq_puts(m, "9 (80.0 Mhz),");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003559 } else if ((lramword & 0x1F00) == 0x1000) { /* 40 Mhz */
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003560 seq_puts(m, "10 (40.0 Mhz),");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003561 } else { /* 20 Mhz or below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003563 period = (((lramword >> 8) * 25) + 50) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003565 if (period == 0) { /* Should never happen. */
Al Viro31491e12013-03-31 03:04:13 -04003566 seq_printf(m, "%d (? Mhz), ", period);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003567 } else {
Al Virob59fb6f2013-03-31 02:59:55 -04003568 seq_printf(m,
3569 "%d (%d.%d Mhz),",
3570 period, 250 / period,
3571 ASC_TENTHS(250, period));
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003572 }
3573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Al Virob59fb6f2013-03-31 02:59:55 -04003575 seq_printf(m, " REQ/ACK Offset: %d",
3576 lramword & 0x1F);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003579 if ((sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003580 seq_puts(m, "*\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003581 renegotiate = 1;
3582 } else {
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003583 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003584 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003587 if (renegotiate) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003588 seq_puts(m, " * = Re-negotiation pending before next command.\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592#ifdef ADVANSYS_STATS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593/*
3594 * asc_prt_board_stats()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 */
Al Virob59fb6f2013-03-31 02:59:55 -04003596static void asc_prt_board_stats(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003598 struct asc_board *boardp = shost_priv(shost);
3599 struct asc_stats *s = &boardp->asc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Al Virob59fb6f2013-03-31 02:59:55 -04003601 seq_printf(m,
3602 "\nLinux Driver Statistics for AdvanSys SCSI Host %d:\n",
3603 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Al Virob59fb6f2013-03-31 02:59:55 -04003605 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003606 " queuecommand %u, reset %u, biosparam %u, interrupt %u\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003607 s->queuecommand, s->reset, s->biosparam,
3608 s->interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Al Virob59fb6f2013-03-31 02:59:55 -04003610 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003611 " callback %u, done %u, build_error %u, build_noreq %u, build_nosg %u\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003612 s->callback, s->done, s->build_error,
3613 s->adv_build_noreq, s->adv_build_nosg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Al Virob59fb6f2013-03-31 02:59:55 -04003615 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003616 " exe_noerror %u, exe_busy %u, exe_error %u, exe_unknown %u\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003617 s->exe_noerror, s->exe_busy, s->exe_error,
3618 s->exe_unknown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003620 /*
3621 * Display data transfer statistics.
3622 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04003623 if (s->xfer_cnt > 0) {
Al Viro31491e12013-03-31 03:04:13 -04003624 seq_printf(m, " xfer_cnt %u, xfer_elem %u, ",
Al Virob59fb6f2013-03-31 02:59:55 -04003625 s->xfer_cnt, s->xfer_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Al Viro31491e12013-03-31 03:04:13 -04003627 seq_printf(m, "xfer_bytes %u.%01u kb\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003628 s->xfer_sect / 2, ASC_TENTHS(s->xfer_sect, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003630 /* Scatter gather transfer statistics */
Al Viro31491e12013-03-31 03:04:13 -04003631 seq_printf(m, " avg_num_elem %u.%01u, ",
Al Virob59fb6f2013-03-31 02:59:55 -04003632 s->xfer_elem / s->xfer_cnt,
3633 ASC_TENTHS(s->xfer_elem, s->xfer_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Al Viro31491e12013-03-31 03:04:13 -04003635 seq_printf(m, "avg_elem_size %u.%01u kb, ",
Al Virob59fb6f2013-03-31 02:59:55 -04003636 (s->xfer_sect / 2) / s->xfer_elem,
3637 ASC_TENTHS((s->xfer_sect / 2), s->xfer_elem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Al Viro31491e12013-03-31 03:04:13 -04003639 seq_printf(m, "avg_xfer_size %u.%01u kb\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003640 (s->xfer_sect / 2) / s->xfer_cnt,
3641 ASC_TENTHS((s->xfer_sect / 2), s->xfer_cnt));
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644#endif /* ADVANSYS_STATS */
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646/*
Al Virob59fb6f2013-03-31 02:59:55 -04003647 * advansys_show_info() - /proc/scsi/advansys/{0,1,2,3,...}
Matthew Wilcox51219352007-10-02 21:55:22 -04003648 *
Al Virob59fb6f2013-03-31 02:59:55 -04003649 * m: seq_file to print into
3650 * shost: Scsi_Host
Matthew Wilcox51219352007-10-02 21:55:22 -04003651 *
3652 * Return the number of bytes read from or written to a
3653 * /proc/scsi/advansys/[0...] file.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
Matthew Wilcox51219352007-10-02 21:55:22 -04003655static int
Al Virob59fb6f2013-03-31 02:59:55 -04003656advansys_show_info(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003658 struct asc_board *boardp = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003660 ASC_DBG(1, "begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Matthew Wilcox51219352007-10-02 21:55:22 -04003662 /*
Matthew Wilcox51219352007-10-02 21:55:22 -04003663 * User read of /proc/scsi/advansys/[0...] file.
3664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Matthew Wilcox51219352007-10-02 21:55:22 -04003666 /*
3667 * Get board configuration information.
3668 *
3669 * advansys_info() returns the board string from its own static buffer.
3670 */
Matthew Wilcox51219352007-10-02 21:55:22 -04003671 /* Copy board information. */
Al Virob59fb6f2013-03-31 02:59:55 -04003672 seq_printf(m, "%s\n", (char *)advansys_info(shost));
Matthew Wilcox51219352007-10-02 21:55:22 -04003673 /*
3674 * Display Wide Board BIOS Information.
3675 */
Al Virob59fb6f2013-03-31 02:59:55 -04003676 if (!ASC_NARROW_BOARD(boardp))
3677 asc_prt_adv_bios(m, shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04003678
3679 /*
3680 * Display driver information for each device attached to the board.
3681 */
Al Virob59fb6f2013-03-31 02:59:55 -04003682 asc_prt_board_devices(m, shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04003683
3684 /*
3685 * Display EEPROM configuration for the board.
3686 */
Al Virob59fb6f2013-03-31 02:59:55 -04003687 if (ASC_NARROW_BOARD(boardp))
3688 asc_prt_asc_board_eeprom(m, shost);
3689 else
3690 asc_prt_adv_board_eeprom(m, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Matthew Wilcox51219352007-10-02 21:55:22 -04003692 /*
3693 * Display driver configuration and information for the board.
3694 */
Al Virob59fb6f2013-03-31 02:59:55 -04003695 asc_prt_driver_conf(m, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Matthew Wilcox51219352007-10-02 21:55:22 -04003697#ifdef ADVANSYS_STATS
3698 /*
3699 * Display driver statistics for the board.
3700 */
Al Virob59fb6f2013-03-31 02:59:55 -04003701 asc_prt_board_stats(m, shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04003702#endif /* ADVANSYS_STATS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Matthew Wilcox51219352007-10-02 21:55:22 -04003704 /*
3705 * Display Asc Library dynamic configuration information
3706 * for the board.
3707 */
Al Virob59fb6f2013-03-31 02:59:55 -04003708 if (ASC_NARROW_BOARD(boardp))
3709 asc_prt_asc_board_info(m, shost);
3710 else
3711 asc_prt_adv_board_info(m, shost);
3712 return 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04003713}
3714#endif /* CONFIG_PROC_FS */
3715
3716static void asc_scsi_done(struct scsi_cmnd *scp)
3717{
Matthew Wilcox52c334e2007-10-02 21:55:39 -04003718 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04003719 ASC_STATS(scp->device->host, done);
Matthew Wilcox51219352007-10-02 21:55:22 -04003720 scp->scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
Matthew Wilcox51219352007-10-02 21:55:22 -04003723static void AscSetBank(PortAddr iop_base, uchar bank)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
Matthew Wilcox51219352007-10-02 21:55:22 -04003725 uchar val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Matthew Wilcox51219352007-10-02 21:55:22 -04003727 val = AscGetChipControl(iop_base) &
3728 (~
3729 (CC_SINGLE_STEP | CC_TEST | CC_DIAG | CC_SCSI_RESET |
3730 CC_CHIP_RESET));
3731 if (bank == 1) {
3732 val |= CC_BANK_ONE;
3733 } else if (bank == 2) {
3734 val |= CC_DIAG | CC_BANK_ONE;
3735 } else {
3736 val &= ~CC_BANK_ONE;
3737 }
3738 AscSetChipControl(iop_base, val);
Matthew Wilcox51219352007-10-02 21:55:22 -04003739}
3740
3741static void AscSetChipIH(PortAddr iop_base, ushort ins_code)
3742{
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003743 AscSetBank(iop_base, 1);
Matthew Wilcox51219352007-10-02 21:55:22 -04003744 AscWriteChipIH(iop_base, ins_code);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003745 AscSetBank(iop_base, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
Matthew Wilcox51219352007-10-02 21:55:22 -04003748static int AscStartChip(PortAddr iop_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Matthew Wilcox51219352007-10-02 21:55:22 -04003750 AscSetChipControl(iop_base, 0);
3751 if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3752 return (0);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003753 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003754 return (1);
3755}
3756
3757static int AscStopChip(PortAddr iop_base)
3758{
3759 uchar cc_val;
3760
3761 cc_val =
3762 AscGetChipControl(iop_base) &
3763 (~(CC_SINGLE_STEP | CC_TEST | CC_DIAG));
3764 AscSetChipControl(iop_base, (uchar)(cc_val | CC_HALT));
3765 AscSetChipIH(iop_base, INS_HALT);
3766 AscSetChipIH(iop_base, INS_RFLAG_WTM);
3767 if ((AscGetChipStatus(iop_base) & CSW_HALTED) == 0) {
3768 return (0);
3769 }
3770 return (1);
3771}
3772
Hannes Reinecked647c782015-04-24 13:18:26 +02003773static bool AscIsChipHalted(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04003774{
3775 if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3776 if ((AscGetChipControl(iop_base) & CC_HALT) != 0) {
Hannes Reinecked647c782015-04-24 13:18:26 +02003777 return true;
Matthew Wilcox51219352007-10-02 21:55:22 -04003778 }
3779 }
Hannes Reinecked647c782015-04-24 13:18:26 +02003780 return false;
Matthew Wilcox51219352007-10-02 21:55:22 -04003781}
3782
Hannes Reinecked647c782015-04-24 13:18:26 +02003783static bool AscResetChipAndScsiBus(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04003784{
3785 PortAddr iop_base;
3786 int i = 10;
3787
3788 iop_base = asc_dvc->iop_base;
3789 while ((AscGetChipStatus(iop_base) & CSW_SCSI_RESET_ACTIVE)
3790 && (i-- > 0)) {
3791 mdelay(100);
3792 }
3793 AscStopChip(iop_base);
3794 AscSetChipControl(iop_base, CC_CHIP_RESET | CC_SCSI_RESET | CC_HALT);
3795 udelay(60);
3796 AscSetChipIH(iop_base, INS_RFLAG_WTM);
3797 AscSetChipIH(iop_base, INS_HALT);
3798 AscSetChipControl(iop_base, CC_CHIP_RESET | CC_HALT);
3799 AscSetChipControl(iop_base, CC_HALT);
3800 mdelay(200);
3801 AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
3802 AscSetChipStatus(iop_base, 0);
3803 return (AscIsChipHalted(iop_base));
3804}
3805
3806static int AscFindSignature(PortAddr iop_base)
3807{
3808 ushort sig_word;
3809
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003810 ASC_DBG(1, "AscGetChipSignatureByte(0x%x) 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04003811 iop_base, AscGetChipSignatureByte(iop_base));
3812 if (AscGetChipSignatureByte(iop_base) == (uchar)ASC_1000_ID1B) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003813 ASC_DBG(1, "AscGetChipSignatureWord(0x%x) 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04003814 iop_base, AscGetChipSignatureWord(iop_base));
3815 sig_word = AscGetChipSignatureWord(iop_base);
3816 if ((sig_word == (ushort)ASC_1000_ID0W) ||
3817 (sig_word == (ushort)ASC_1000_ID0W_FIX)) {
3818 return (1);
3819 }
3820 }
3821 return (0);
3822}
3823
3824static void AscEnableInterrupt(PortAddr iop_base)
3825{
3826 ushort cfg;
3827
3828 cfg = AscGetChipCfgLsw(iop_base);
3829 AscSetChipCfgLsw(iop_base, cfg | ASC_CFG0_HOST_INT_ON);
Matthew Wilcox51219352007-10-02 21:55:22 -04003830}
3831
3832static void AscDisableInterrupt(PortAddr iop_base)
3833{
3834 ushort cfg;
3835
3836 cfg = AscGetChipCfgLsw(iop_base);
3837 AscSetChipCfgLsw(iop_base, cfg & (~ASC_CFG0_HOST_INT_ON));
Matthew Wilcox51219352007-10-02 21:55:22 -04003838}
3839
3840static uchar AscReadLramByte(PortAddr iop_base, ushort addr)
3841{
3842 unsigned char byte_data;
3843 unsigned short word_data;
3844
3845 if (isodd_word(addr)) {
3846 AscSetChipLramAddr(iop_base, addr - 1);
3847 word_data = AscGetChipLramData(iop_base);
3848 byte_data = (word_data >> 8) & 0xFF;
3849 } else {
3850 AscSetChipLramAddr(iop_base, addr);
3851 word_data = AscGetChipLramData(iop_base);
3852 byte_data = word_data & 0xFF;
3853 }
3854 return byte_data;
3855}
3856
3857static ushort AscReadLramWord(PortAddr iop_base, ushort addr)
3858{
3859 ushort word_data;
3860
3861 AscSetChipLramAddr(iop_base, addr);
3862 word_data = AscGetChipLramData(iop_base);
3863 return (word_data);
3864}
3865
3866#if CC_VERY_LONG_SG_LIST
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003867static u32 AscReadLramDWord(PortAddr iop_base, ushort addr)
Matthew Wilcox51219352007-10-02 21:55:22 -04003868{
3869 ushort val_low, val_high;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003870 u32 dword_data;
Matthew Wilcox51219352007-10-02 21:55:22 -04003871
3872 AscSetChipLramAddr(iop_base, addr);
3873 val_low = AscGetChipLramData(iop_base);
3874 val_high = AscGetChipLramData(iop_base);
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003875 dword_data = ((u32) val_high << 16) | (u32) val_low;
Matthew Wilcox51219352007-10-02 21:55:22 -04003876 return (dword_data);
3877}
3878#endif /* CC_VERY_LONG_SG_LIST */
3879
3880static void
3881AscMemWordSetLram(PortAddr iop_base, ushort s_addr, ushort set_wval, int words)
3882{
3883 int i;
3884
3885 AscSetChipLramAddr(iop_base, s_addr);
3886 for (i = 0; i < words; i++) {
3887 AscSetChipLramData(iop_base, set_wval);
3888 }
3889}
3890
3891static void AscWriteLramWord(PortAddr iop_base, ushort addr, ushort word_val)
3892{
3893 AscSetChipLramAddr(iop_base, addr);
3894 AscSetChipLramData(iop_base, word_val);
Matthew Wilcox51219352007-10-02 21:55:22 -04003895}
3896
3897static void AscWriteLramByte(PortAddr iop_base, ushort addr, uchar byte_val)
3898{
3899 ushort word_data;
3900
3901 if (isodd_word(addr)) {
3902 addr--;
3903 word_data = AscReadLramWord(iop_base, addr);
3904 word_data &= 0x00FF;
3905 word_data |= (((ushort)byte_val << 8) & 0xFF00);
3906 } else {
3907 word_data = AscReadLramWord(iop_base, addr);
3908 word_data &= 0xFF00;
3909 word_data |= ((ushort)byte_val & 0x00FF);
3910 }
3911 AscWriteLramWord(iop_base, addr, word_data);
Matthew Wilcox51219352007-10-02 21:55:22 -04003912}
3913
3914/*
3915 * Copy 2 bytes to LRAM.
3916 *
3917 * The source data is assumed to be in little-endian order in memory
3918 * and is maintained in little-endian order when written to LRAM.
3919 */
3920static void
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05303921AscMemWordCopyPtrToLram(PortAddr iop_base, ushort s_addr,
3922 const uchar *s_buffer, int words)
Matthew Wilcox51219352007-10-02 21:55:22 -04003923{
3924 int i;
3925
3926 AscSetChipLramAddr(iop_base, s_addr);
3927 for (i = 0; i < 2 * words; i += 2) {
3928 /*
3929 * On a little-endian system the second argument below
3930 * produces a little-endian ushort which is written to
3931 * LRAM in little-endian order. On a big-endian system
3932 * the second argument produces a big-endian ushort which
3933 * is "transparently" byte-swapped by outpw() and written
3934 * in little-endian order to LRAM.
3935 */
3936 outpw(iop_base + IOP_RAM_DATA,
3937 ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]);
3938 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003939}
3940
3941/*
3942 * Copy 4 bytes to LRAM.
3943 *
3944 * The source data is assumed to be in little-endian order in memory
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003945 * and is maintained in little-endian order when written to LRAM.
Matthew Wilcox51219352007-10-02 21:55:22 -04003946 */
3947static void
3948AscMemDWordCopyPtrToLram(PortAddr iop_base,
3949 ushort s_addr, uchar *s_buffer, int dwords)
3950{
3951 int i;
3952
3953 AscSetChipLramAddr(iop_base, s_addr);
3954 for (i = 0; i < 4 * dwords; i += 4) {
3955 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]); /* LSW */
3956 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 3] << 8) | s_buffer[i + 2]); /* MSW */
3957 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003958}
3959
3960/*
3961 * Copy 2 bytes from LRAM.
3962 *
3963 * The source data is assumed to be in little-endian order in LRAM
3964 * and is maintained in little-endian order when written to memory.
3965 */
3966static void
3967AscMemWordCopyPtrFromLram(PortAddr iop_base,
3968 ushort s_addr, uchar *d_buffer, int words)
3969{
3970 int i;
3971 ushort word;
3972
3973 AscSetChipLramAddr(iop_base, s_addr);
3974 for (i = 0; i < 2 * words; i += 2) {
3975 word = inpw(iop_base + IOP_RAM_DATA);
3976 d_buffer[i] = word & 0xff;
3977 d_buffer[i + 1] = (word >> 8) & 0xff;
3978 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003979}
3980
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003981static u32 AscMemSumLramWord(PortAddr iop_base, ushort s_addr, int words)
Matthew Wilcox51219352007-10-02 21:55:22 -04003982{
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003983 u32 sum = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04003984 int i;
3985
Matthew Wilcox51219352007-10-02 21:55:22 -04003986 for (i = 0; i < words; i++, s_addr += 2) {
3987 sum += AscReadLramWord(iop_base, s_addr);
3988 }
3989 return (sum);
3990}
3991
3992static ushort AscInitLram(ASC_DVC_VAR *asc_dvc)
3993{
3994 uchar i;
3995 ushort s_addr;
3996 PortAddr iop_base;
3997 ushort warn_code;
3998
3999 iop_base = asc_dvc->iop_base;
4000 warn_code = 0;
4001 AscMemWordSetLram(iop_base, ASC_QADR_BEG, 0,
4002 (ushort)(((int)(asc_dvc->max_total_qng + 2 + 1) *
4003 64) >> 1));
4004 i = ASC_MIN_ACTIVE_QNO;
4005 s_addr = ASC_QADR_BEG + ASC_QBLK_SIZE;
4006 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4007 (uchar)(i + 1));
4008 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4009 (uchar)(asc_dvc->max_total_qng));
4010 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4011 (uchar)i);
4012 i++;
4013 s_addr += ASC_QBLK_SIZE;
4014 for (; i < asc_dvc->max_total_qng; i++, s_addr += ASC_QBLK_SIZE) {
4015 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4016 (uchar)(i + 1));
4017 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4018 (uchar)(i - 1));
4019 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4020 (uchar)i);
4021 }
4022 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
4023 (uchar)ASC_QLINK_END);
4024 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
4025 (uchar)(asc_dvc->max_total_qng - 1));
4026 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
4027 (uchar)asc_dvc->max_total_qng);
4028 i++;
4029 s_addr += ASC_QBLK_SIZE;
4030 for (; i <= (uchar)(asc_dvc->max_total_qng + 3);
4031 i++, s_addr += ASC_QBLK_SIZE) {
4032 AscWriteLramByte(iop_base,
4033 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_FWD), i);
4034 AscWriteLramByte(iop_base,
4035 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_BWD), i);
4036 AscWriteLramByte(iop_base,
4037 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_QNO), i);
4038 }
4039 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040}
4041
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004042static u32
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304043AscLoadMicroCode(PortAddr iop_base, ushort s_addr,
4044 const uchar *mcode_buf, ushort mcode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004046 u32 chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004047 ushort mcode_word_size;
4048 ushort mcode_chksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004050 /* Write the microcode buffer starting at LRAM address 0. */
4051 mcode_word_size = (ushort)(mcode_size >> 1);
4052 AscMemWordSetLram(iop_base, s_addr, 0, mcode_word_size);
4053 AscMemWordCopyPtrToLram(iop_base, s_addr, mcode_buf, mcode_word_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004055 chksum = AscMemSumLramWord(iop_base, s_addr, mcode_word_size);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04004056 ASC_DBG(1, "chksum 0x%lx\n", (ulong)chksum);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004057 mcode_chksum = (ushort)AscMemSumLramWord(iop_base,
4058 (ushort)ASC_CODE_SEC_BEG,
4059 (ushort)((mcode_size -
4060 s_addr - (ushort)
4061 ASC_CODE_SEC_BEG) /
4062 2));
Matthew Wilcoxb352f922007-10-02 21:55:33 -04004063 ASC_DBG(1, "mcode_chksum 0x%lx\n", (ulong)mcode_chksum);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004064 AscWriteLramWord(iop_base, ASCV_MCODE_CHKSUM_W, mcode_chksum);
4065 AscWriteLramWord(iop_base, ASCV_MCODE_SIZE_W, mcode_size);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04004066 return chksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
Matthew Wilcox51219352007-10-02 21:55:22 -04004069static void AscInitQLinkVar(ASC_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
Matthew Wilcox51219352007-10-02 21:55:22 -04004071 PortAddr iop_base;
4072 int i;
4073 ushort lram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Matthew Wilcox51219352007-10-02 21:55:22 -04004075 iop_base = asc_dvc->iop_base;
4076 AscPutRiscVarFreeQHead(iop_base, 1);
4077 AscPutRiscVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4078 AscPutVarFreeQHead(iop_base, 1);
4079 AscPutVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4080 AscWriteLramByte(iop_base, ASCV_BUSY_QHEAD_B,
4081 (uchar)((int)asc_dvc->max_total_qng + 1));
4082 AscWriteLramByte(iop_base, ASCV_DISC1_QHEAD_B,
4083 (uchar)((int)asc_dvc->max_total_qng + 2));
4084 AscWriteLramByte(iop_base, (ushort)ASCV_TOTAL_READY_Q_B,
4085 asc_dvc->max_total_qng);
4086 AscWriteLramWord(iop_base, ASCV_ASCDVC_ERR_CODE_W, 0);
4087 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
4088 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, 0);
4089 AscWriteLramByte(iop_base, ASCV_SCSIBUSY_B, 0);
4090 AscWriteLramByte(iop_base, ASCV_WTM_FLAG_B, 0);
4091 AscPutQDoneInProgress(iop_base, 0);
4092 lram_addr = ASC_QADR_BEG;
4093 for (i = 0; i < 32; i++, lram_addr += 2) {
4094 AscWriteLramWord(iop_base, lram_addr, 0);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
Matthew Wilcox51219352007-10-02 21:55:22 -04004098static ushort AscInitMicroCodeVar(ASC_DVC_VAR *asc_dvc)
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004099{
Matthew Wilcox51219352007-10-02 21:55:22 -04004100 int i;
4101 ushort warn_code;
4102 PortAddr iop_base;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004103 __le32 phy_addr;
4104 __le32 phy_size;
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004105 struct asc_board *board = asc_dvc_to_board(asc_dvc);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004106
Matthew Wilcox51219352007-10-02 21:55:22 -04004107 iop_base = asc_dvc->iop_base;
4108 warn_code = 0;
4109 for (i = 0; i <= ASC_MAX_TID; i++) {
4110 AscPutMCodeInitSDTRAtID(iop_base, i,
4111 asc_dvc->cfg->sdtr_period_offset[i]);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004112 }
4113
Matthew Wilcox51219352007-10-02 21:55:22 -04004114 AscInitQLinkVar(asc_dvc);
4115 AscWriteLramByte(iop_base, ASCV_DISC_ENABLE_B,
4116 asc_dvc->cfg->disc_enable);
4117 AscWriteLramByte(iop_base, ASCV_HOSTSCSI_ID_B,
4118 ASC_TID_TO_TARGET_ID(asc_dvc->cfg->chip_scsi_id));
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004119
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004120 /* Ensure overrun buffer is aligned on an 8 byte boundary. */
4121 BUG_ON((unsigned long)asc_dvc->overrun_buf & 7);
4122 asc_dvc->overrun_dma = dma_map_single(board->dev, asc_dvc->overrun_buf,
4123 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004124 if (dma_mapping_error(board->dev, asc_dvc->overrun_dma)) {
4125 warn_code = -ENOMEM;
4126 goto err_dma_map;
4127 }
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004128 phy_addr = cpu_to_le32(asc_dvc->overrun_dma);
Matthew Wilcox51219352007-10-02 21:55:22 -04004129 AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_PADDR_D,
4130 (uchar *)&phy_addr, 1);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004131 phy_size = cpu_to_le32(ASC_OVERRUN_BSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04004132 AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_BSIZE_D,
4133 (uchar *)&phy_size, 1);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004134
Matthew Wilcox51219352007-10-02 21:55:22 -04004135 asc_dvc->cfg->mcode_date =
4136 AscReadLramWord(iop_base, (ushort)ASCV_MC_DATE_W);
4137 asc_dvc->cfg->mcode_version =
4138 AscReadLramWord(iop_base, (ushort)ASCV_MC_VER_W);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004139
Matthew Wilcox51219352007-10-02 21:55:22 -04004140 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
4141 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
4142 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004143 warn_code = UW_ERR;
4144 goto err_mcode_start;
Matthew Wilcox51219352007-10-02 21:55:22 -04004145 }
4146 if (AscStartChip(iop_base) != 1) {
4147 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004148 warn_code = UW_ERR;
4149 goto err_mcode_start;
Matthew Wilcox51219352007-10-02 21:55:22 -04004150 }
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004151
Matthew Wilcox51219352007-10-02 21:55:22 -04004152 return warn_code;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004153
4154err_mcode_start:
4155 dma_unmap_single(board->dev, asc_dvc->overrun_dma,
4156 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
4157err_dma_map:
4158 asc_dvc->overrun_dma = 0;
4159 return warn_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04004160}
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004161
Matthew Wilcox51219352007-10-02 21:55:22 -04004162static ushort AscInitAsc1000Driver(ASC_DVC_VAR *asc_dvc)
4163{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304164 const struct firmware *fw;
4165 const char fwname[] = "advansys/mcode.bin";
4166 int err;
4167 unsigned long chksum;
Matthew Wilcox51219352007-10-02 21:55:22 -04004168 ushort warn_code;
4169 PortAddr iop_base;
4170
4171 iop_base = asc_dvc->iop_base;
4172 warn_code = 0;
4173 if ((asc_dvc->dvc_cntl & ASC_CNTL_RESET_SCSI) &&
4174 !(asc_dvc->init_state & ASC_INIT_RESET_SCSI_DONE)) {
4175 AscResetChipAndScsiBus(asc_dvc);
4176 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4177 }
4178 asc_dvc->init_state |= ASC_INIT_STATE_BEG_LOAD_MC;
4179 if (asc_dvc->err_code != 0)
4180 return UW_ERR;
4181 if (!AscFindSignature(asc_dvc->iop_base)) {
4182 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
4183 return warn_code;
4184 }
4185 AscDisableInterrupt(iop_base);
4186 warn_code |= AscInitLram(asc_dvc);
4187 if (asc_dvc->err_code != 0)
4188 return UW_ERR;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304189
4190 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4191 if (err) {
4192 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4193 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004194 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304195 return err;
4196 }
4197 if (fw->size < 4) {
4198 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4199 fw->size, fwname);
4200 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004201 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304202 return -EINVAL;
4203 }
4204 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4205 (fw->data[1] << 8) | fw->data[0];
4206 ASC_DBG(1, "_asc_mcode_chksum 0x%lx\n", (ulong)chksum);
4207 if (AscLoadMicroCode(iop_base, 0, &fw->data[4],
4208 fw->size - 4) != chksum) {
Matthew Wilcox51219352007-10-02 21:55:22 -04004209 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304210 release_firmware(fw);
Matthew Wilcox51219352007-10-02 21:55:22 -04004211 return warn_code;
4212 }
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304213 release_firmware(fw);
Matthew Wilcox51219352007-10-02 21:55:22 -04004214 warn_code |= AscInitMicroCodeVar(asc_dvc);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004215 if (!asc_dvc->overrun_dma)
4216 return warn_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04004217 asc_dvc->init_state |= ASC_INIT_STATE_END_LOAD_MC;
4218 AscEnableInterrupt(iop_base);
4219 return warn_code;
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004220}
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222/*
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004223 * Load the Microcode
4224 *
4225 * Write the microcode image to RISC memory starting at address 0.
4226 *
4227 * The microcode is stored compressed in the following format:
4228 *
4229 * 254 word (508 byte) table indexed by byte code followed
4230 * by the following byte codes:
4231 *
4232 * 1-Byte Code:
4233 * 00: Emit word 0 in table.
4234 * 01: Emit word 1 in table.
4235 * .
4236 * FD: Emit word 253 in table.
4237 *
4238 * Multi-Byte Code:
4239 * FE WW WW: (3 byte code) Word to emit is the next word WW WW.
4240 * FF BB WW WW: (4 byte code) Emit BB count times next word WW WW.
4241 *
4242 * Returns 0 or an error if the checksum doesn't match
4243 */
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304244static int AdvLoadMicrocode(AdvPortAddr iop_base, const unsigned char *buf,
4245 int size, int memsize, int chksum)
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004246{
4247 int i, j, end, len = 0;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004248 u32 sum;
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004249
4250 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4251
4252 for (i = 253 * 2; i < size; i++) {
4253 if (buf[i] == 0xff) {
4254 unsigned short word = (buf[i + 3] << 8) | buf[i + 2];
4255 for (j = 0; j < buf[i + 1]; j++) {
4256 AdvWriteWordAutoIncLram(iop_base, word);
4257 len += 2;
4258 }
4259 i += 3;
4260 } else if (buf[i] == 0xfe) {
4261 unsigned short word = (buf[i + 2] << 8) | buf[i + 1];
4262 AdvWriteWordAutoIncLram(iop_base, word);
4263 i += 2;
4264 len += 2;
4265 } else {
Matthew Wilcox951b62c2007-10-05 15:57:06 -04004266 unsigned int off = buf[i] * 2;
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004267 unsigned short word = (buf[off + 1] << 8) | buf[off];
4268 AdvWriteWordAutoIncLram(iop_base, word);
4269 len += 2;
4270 }
4271 }
4272
4273 end = len;
4274
4275 while (len < memsize) {
4276 AdvWriteWordAutoIncLram(iop_base, 0);
4277 len += 2;
4278 }
4279
4280 /* Verify the microcode checksum. */
4281 sum = 0;
4282 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4283
4284 for (len = 0; len < end; len += 2) {
4285 sum += AdvReadWordAutoIncLram(iop_base);
4286 }
4287
4288 if (sum != chksum)
4289 return ASC_IERR_MCODE_CHKSUM;
4290
4291 return 0;
4292}
4293
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004294static void AdvBuildCarrierFreelist(struct adv_dvc_var *adv_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04004295{
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004296 off_t carr_offset = 0, next_offset;
4297 dma_addr_t carr_paddr;
4298 int carr_num = ADV_CARRIER_BUFSIZE / sizeof(ADV_CARR_T), i;
Matthew Wilcox51219352007-10-02 21:55:22 -04004299
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004300 for (i = 0; i < carr_num; i++) {
4301 carr_offset = i * sizeof(ADV_CARR_T);
4302 /* Get physical address of the carrier 'carrp'. */
4303 carr_paddr = adv_dvc->carrier_addr + carr_offset;
4304
4305 adv_dvc->carrier[i].carr_pa = cpu_to_le32(carr_paddr);
4306 adv_dvc->carrier[i].carr_va = cpu_to_le32(carr_offset);
4307 adv_dvc->carrier[i].areq_vpa = 0;
4308 next_offset = carr_offset + sizeof(ADV_CARR_T);
4309 if (i == carr_num)
4310 next_offset = ~0;
4311 adv_dvc->carrier[i].next_vpa = cpu_to_le32(next_offset);
4312 }
4313 /*
4314 * We cannot have a carrier with 'carr_va' of '0', as
4315 * a reference to this carrier would be interpreted as
4316 * list termination.
4317 * So start at carrier 1 with the freelist.
4318 */
4319 adv_dvc->carr_freelist = &adv_dvc->carrier[1];
4320}
4321
4322static ADV_CARR_T *adv_get_carrier(struct adv_dvc_var *adv_dvc, u32 offset)
4323{
4324 int index;
4325
4326 BUG_ON(offset > ADV_CARRIER_BUFSIZE);
4327
4328 index = offset / sizeof(ADV_CARR_T);
4329 return &adv_dvc->carrier[index];
4330}
4331
4332static ADV_CARR_T *adv_get_next_carrier(struct adv_dvc_var *adv_dvc)
4333{
4334 ADV_CARR_T *carrp = adv_dvc->carr_freelist;
4335 u32 next_vpa = le32_to_cpu(carrp->next_vpa);
4336
4337 if (next_vpa == 0 || next_vpa == ~0) {
4338 ASC_DBG(1, "invalid vpa offset 0x%x\n", next_vpa);
4339 return NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04004340 }
4341
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004342 adv_dvc->carr_freelist = adv_get_carrier(adv_dvc, next_vpa);
4343 /*
4344 * insert stopper carrier to terminate list
4345 */
4346 carrp->next_vpa = cpu_to_le32(ASC_CQ_STOPPER);
Matthew Wilcox51219352007-10-02 21:55:22 -04004347
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004348 return carrp;
Matthew Wilcox51219352007-10-02 21:55:22 -04004349}
4350
4351/*
Hannes Reinecke4b47e462015-04-24 13:18:24 +02004352 * 'offset' is the index in the request pointer array
4353 */
4354static adv_req_t * adv_get_reqp(struct adv_dvc_var *adv_dvc, u32 offset)
4355{
4356 struct asc_board *boardp = adv_dvc->drv_ptr;
4357
4358 BUG_ON(offset > adv_dvc->max_host_qng);
4359 return &boardp->adv_reqp[offset];
4360}
4361
4362/*
Matthew Wilcox51219352007-10-02 21:55:22 -04004363 * Send an idle command to the chip and wait for completion.
4364 *
4365 * Command completion is polled for once per microsecond.
4366 *
4367 * The function can be called from anywhere including an interrupt handler.
4368 * But the function is not re-entrant, so it uses the DvcEnter/LeaveCritical()
4369 * functions to prevent reentrancy.
4370 *
4371 * Return Values:
4372 * ADV_TRUE - command completed successfully
4373 * ADV_FALSE - command failed
4374 * ADV_ERROR - command timed out
4375 */
4376static int
4377AdvSendIdleCmd(ADV_DVC_VAR *asc_dvc,
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004378 ushort idle_cmd, u32 idle_cmd_parameter)
Matthew Wilcox51219352007-10-02 21:55:22 -04004379{
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004380 int result, i, j;
Matthew Wilcox51219352007-10-02 21:55:22 -04004381 AdvPortAddr iop_base;
4382
4383 iop_base = asc_dvc->iop_base;
4384
4385 /*
4386 * Clear the idle command status which is set by the microcode
4387 * to a non-zero value to indicate when the command is completed.
4388 * The non-zero result is one of the IDLE_CMD_STATUS_* values
4389 */
4390 AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS, (ushort)0);
4391
4392 /*
4393 * Write the idle command value after the idle command parameter
4394 * has been written to avoid a race condition. If the order is not
4395 * followed, the microcode may process the idle command before the
4396 * parameters have been written to LRAM.
4397 */
4398 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IDLE_CMD_PARAMETER,
4399 cpu_to_le32(idle_cmd_parameter));
4400 AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD, idle_cmd);
4401
4402 /*
4403 * Tickle the RISC to tell it to process the idle command.
4404 */
4405 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_B);
4406 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
4407 /*
4408 * Clear the tickle value. In the ASC-3550 the RISC flag
4409 * command 'clr_tickle_b' does not work unless the host
4410 * value is cleared.
4411 */
4412 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_NOP);
4413 }
4414
4415 /* Wait for up to 100 millisecond for the idle command to timeout. */
4416 for (i = 0; i < SCSI_WAIT_100_MSEC; i++) {
4417 /* Poll once each microsecond for command completion. */
4418 for (j = 0; j < SCSI_US_PER_MSEC; j++) {
4419 AdvReadWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS,
4420 result);
4421 if (result != 0)
4422 return result;
4423 udelay(1);
4424 }
4425 }
4426
4427 BUG(); /* The idle command should never timeout. */
4428 return ADV_ERROR;
4429}
4430
4431/*
4432 * Reset SCSI Bus and purge all outstanding requests.
4433 *
4434 * Return Value:
4435 * ADV_TRUE(1) - All requests are purged and SCSI Bus is reset.
4436 * ADV_FALSE(0) - Microcode command failed.
4437 * ADV_ERROR(-1) - Microcode command timed-out. Microcode or IC
4438 * may be hung which requires driver recovery.
4439 */
4440static int AdvResetSB(ADV_DVC_VAR *asc_dvc)
4441{
4442 int status;
4443
4444 /*
4445 * Send the SCSI Bus Reset idle start idle command which asserts
4446 * the SCSI Bus Reset signal.
4447 */
4448 status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_START, 0L);
4449 if (status != ADV_TRUE) {
4450 return status;
4451 }
4452
4453 /*
4454 * Delay for the specified SCSI Bus Reset hold time.
4455 *
4456 * The hold time delay is done on the host because the RISC has no
4457 * microsecond accurate timer.
4458 */
4459 udelay(ASC_SCSI_RESET_HOLD_TIME_US);
4460
4461 /*
4462 * Send the SCSI Bus Reset end idle command which de-asserts
4463 * the SCSI Bus Reset signal and purges any pending requests.
4464 */
4465 status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_END, 0L);
4466 if (status != ADV_TRUE) {
4467 return status;
4468 }
4469
4470 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4471
4472 return status;
4473}
4474
4475/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 * Initialize the ASC-3550.
4477 *
4478 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4479 *
4480 * For a non-fatal error return a warning code. If there are no warnings
4481 * then 0 is returned.
4482 *
4483 * Needed after initialization for error recovery.
4484 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004485static int AdvInitAsc3550Driver(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304487 const struct firmware *fw;
4488 const char fwname[] = "advansys/3550.bin";
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004489 AdvPortAddr iop_base;
4490 ushort warn_code;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004491 int begin_addr;
4492 int end_addr;
4493 ushort code_sum;
4494 int word;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004495 int i;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304496 int err;
4497 unsigned long chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004498 ushort scsi_cfg1;
4499 uchar tid;
4500 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
4501 ushort wdtr_able = 0, sdtr_able, tagqng_able;
4502 uchar max_cmd[ADV_MAX_TID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004504 /* If there is already an error, don't continue. */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004505 if (asc_dvc->err_code != 0)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004506 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004508 /*
4509 * The caller must set 'chip_type' to ADV_CHIP_ASC3550.
4510 */
4511 if (asc_dvc->chip_type != ADV_CHIP_ASC3550) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004512 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004513 return ADV_ERROR;
4514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004516 warn_code = 0;
4517 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004519 /*
4520 * Save the RISC memory BIOS region before writing the microcode.
4521 * The BIOS may already be loaded and using its RISC LRAM region
4522 * so its region must be saved and restored.
4523 *
4524 * Note: This code makes the assumption, which is currently true,
4525 * that a chip reset does not clear RISC LRAM.
4526 */
4527 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4528 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4529 bios_mem[i]);
4530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004532 /*
4533 * Save current per TID negotiated values.
4534 */
4535 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] == 0x55AA) {
4536 ushort bios_version, major, minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004538 bios_version =
4539 bios_mem[(ASC_MC_BIOS_VERSION - ASC_MC_BIOSMEM) / 2];
4540 major = (bios_version >> 12) & 0xF;
4541 minor = (bios_version >> 8) & 0xF;
4542 if (major < 3 || (major == 3 && minor == 1)) {
4543 /* BIOS 3.1 and earlier location of 'wdtr_able' variable. */
4544 AdvReadWordLram(iop_base, 0x120, wdtr_able);
4545 } else {
4546 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4547 }
4548 }
4549 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4550 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4551 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4552 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4553 max_cmd[tid]);
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304556 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4557 if (err) {
4558 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4559 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004560 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304561 return err;
4562 }
4563 if (fw->size < 4) {
4564 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4565 fw->size, fwname);
4566 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004567 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304568 return -EINVAL;
4569 }
4570 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4571 (fw->data[1] << 8) | fw->data[0];
4572 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
4573 fw->size - 4, ADV_3550_MEMSIZE,
4574 chksum);
4575 release_firmware(fw);
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004576 if (asc_dvc->err_code)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004577 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004579 /*
4580 * Restore the RISC memory BIOS region.
4581 */
4582 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4583 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4584 bios_mem[i]);
4585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004587 /*
4588 * Calculate and write the microcode code checksum to the microcode
4589 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
4590 */
4591 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
4592 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
4593 code_sum = 0;
4594 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
4595 for (word = begin_addr; word < end_addr; word += 2) {
4596 code_sum += AdvReadWordAutoIncLram(iop_base);
4597 }
4598 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004600 /*
4601 * Read and save microcode version and date.
4602 */
4603 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
4604 asc_dvc->cfg->mcode_date);
4605 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
4606 asc_dvc->cfg->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004608 /*
4609 * Set the chip type to indicate the ASC3550.
4610 */
4611 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC3550);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004613 /*
4614 * If the PCI Configuration Command Register "Parity Error Response
4615 * Control" Bit was clear (0), then set the microcode variable
4616 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
4617 * to ignore DMA parity errors.
4618 */
4619 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
4620 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4621 word |= CONTROL_FLAG_IGNORE_PERR;
4622 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004625 /*
4626 * For ASC-3550, setting the START_CTL_EMFU [3:2] bits sets a FIFO
4627 * threshold of 128 bytes. This register is only accessible to the host.
4628 */
4629 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
4630 START_CTL_EMFU | READ_CMD_MRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004632 /*
4633 * Microcode operating variables for WDTR, SDTR, and command tag
Matthew Wilcox47d853c2007-07-26 11:41:33 -04004634 * queuing will be set in slave_configure() based on what a
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004635 * device reports it is capable of in Inquiry byte 7.
4636 *
4637 * If SCSI Bus Resets have been disabled, then directly set
4638 * SDTR and WDTR from the EEPROM configuration. This will allow
4639 * the BIOS and warm boot to work without a SCSI bus hang on
4640 * the Inquiry caused by host and target mismatched DTR values.
4641 * Without the SCSI Bus Reset, before an Inquiry a device can't
4642 * be assumed to be in Asynchronous, Narrow mode.
4643 */
4644 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
4645 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
4646 asc_dvc->wdtr_able);
4647 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
4648 asc_dvc->sdtr_able);
4649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004651 /*
4652 * Set microcode operating variables for SDTR_SPEED1, SDTR_SPEED2,
4653 * SDTR_SPEED3, and SDTR_SPEED4 based on the ULTRA EEPROM per TID
4654 * bitmask. These values determine the maximum SDTR speed negotiated
4655 * with a device.
4656 *
4657 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
4658 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
4659 * without determining here whether the device supports SDTR.
4660 *
4661 * 4-bit speed SDTR speed name
4662 * =========== ===============
4663 * 0000b (0x0) SDTR disabled
4664 * 0001b (0x1) 5 Mhz
4665 * 0010b (0x2) 10 Mhz
4666 * 0011b (0x3) 20 Mhz (Ultra)
4667 * 0100b (0x4) 40 Mhz (LVD/Ultra2)
4668 * 0101b (0x5) 80 Mhz (LVD2/Ultra3)
4669 * 0110b (0x6) Undefined
4670 * .
4671 * 1111b (0xF) Undefined
4672 */
4673 word = 0;
4674 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4675 if (ADV_TID_TO_TIDMASK(tid) & asc_dvc->ultra_able) {
4676 /* Set Ultra speed for TID 'tid'. */
4677 word |= (0x3 << (4 * (tid % 4)));
4678 } else {
4679 /* Set Fast speed for TID 'tid'. */
4680 word |= (0x2 << (4 * (tid % 4)));
4681 }
4682 if (tid == 3) { /* Check if done with sdtr_speed1. */
4683 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, word);
4684 word = 0;
4685 } else if (tid == 7) { /* Check if done with sdtr_speed2. */
4686 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, word);
4687 word = 0;
4688 } else if (tid == 11) { /* Check if done with sdtr_speed3. */
4689 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, word);
4690 word = 0;
4691 } else if (tid == 15) { /* Check if done with sdtr_speed4. */
4692 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, word);
4693 /* End of loop. */
4694 }
4695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004697 /*
4698 * Set microcode operating variable for the disconnect per TID bitmask.
4699 */
4700 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
4701 asc_dvc->cfg->disc_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004703 /*
4704 * Set SCSI_CFG0 Microcode Default Value.
4705 *
4706 * The microcode will set the SCSI_CFG0 register using this value
4707 * after it is started below.
4708 */
4709 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
4710 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
4711 asc_dvc->chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004713 /*
4714 * Determine SCSI_CFG1 Microcode Default Value.
4715 *
4716 * The microcode will set the SCSI_CFG1 register using this value
4717 * after it is started below.
4718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004720 /* Read current SCSI_CFG1 Register value. */
4721 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004723 /*
4724 * If all three connectors are in use, return an error.
4725 */
4726 if ((scsi_cfg1 & CABLE_ILLEGAL_A) == 0 ||
4727 (scsi_cfg1 & CABLE_ILLEGAL_B) == 0) {
4728 asc_dvc->err_code |= ASC_IERR_ILLEGAL_CONNECTION;
4729 return ADV_ERROR;
4730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004732 /*
4733 * If the internal narrow cable is reversed all of the SCSI_CTRL
4734 * register signals will be set. Check for and return an error if
4735 * this condition is found.
4736 */
4737 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
4738 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
4739 return ADV_ERROR;
4740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004742 /*
4743 * If this is a differential board and a single-ended device
4744 * is attached to one of the connectors, return an error.
4745 */
4746 if ((scsi_cfg1 & DIFF_MODE) && (scsi_cfg1 & DIFF_SENSE) == 0) {
4747 asc_dvc->err_code |= ASC_IERR_SINGLE_END_DEVICE;
4748 return ADV_ERROR;
4749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004751 /*
4752 * If automatic termination control is enabled, then set the
4753 * termination value based on a table listed in a_condor.h.
4754 *
4755 * If manual termination was specified with an EEPROM setting
4756 * then 'termination' was set-up in AdvInitFrom3550EEPROM() and
4757 * is ready to be 'ored' into SCSI_CFG1.
4758 */
4759 if (asc_dvc->cfg->termination == 0) {
4760 /*
4761 * The software always controls termination by setting TERM_CTL_SEL.
4762 * If TERM_CTL_SEL were set to 0, the hardware would set termination.
4763 */
4764 asc_dvc->cfg->termination |= TERM_CTL_SEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004766 switch (scsi_cfg1 & CABLE_DETECT) {
4767 /* TERM_CTL_H: on, TERM_CTL_L: on */
4768 case 0x3:
4769 case 0x7:
4770 case 0xB:
4771 case 0xD:
4772 case 0xE:
4773 case 0xF:
4774 asc_dvc->cfg->termination |= (TERM_CTL_H | TERM_CTL_L);
4775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004777 /* TERM_CTL_H: on, TERM_CTL_L: off */
4778 case 0x1:
4779 case 0x5:
4780 case 0x9:
4781 case 0xA:
4782 case 0xC:
4783 asc_dvc->cfg->termination |= TERM_CTL_H;
4784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004786 /* TERM_CTL_H: off, TERM_CTL_L: off */
4787 case 0x2:
4788 case 0x6:
4789 break;
4790 }
4791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004793 /*
4794 * Clear any set TERM_CTL_H and TERM_CTL_L bits.
4795 */
4796 scsi_cfg1 &= ~TERM_CTL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004798 /*
4799 * Invert the TERM_CTL_H and TERM_CTL_L bits and then
4800 * set 'scsi_cfg1'. The TERM_POL bit does not need to be
4801 * referenced, because the hardware internally inverts
4802 * the Termination High and Low bits if TERM_POL is set.
4803 */
4804 scsi_cfg1 |= (TERM_CTL_SEL | (~asc_dvc->cfg->termination & TERM_CTL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004806 /*
4807 * Set SCSI_CFG1 Microcode Default Value
4808 *
4809 * Set filter value and possibly modified termination control
4810 * bits in the Microcode SCSI_CFG1 Register Value.
4811 *
4812 * The microcode will set the SCSI_CFG1 register using this value
4813 * after it is started below.
4814 */
4815 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1,
4816 FLTR_DISABLE | scsi_cfg1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004818 /*
4819 * Set MEM_CFG Microcode Default Value
4820 *
4821 * The microcode will set the MEM_CFG register using this value
4822 * after it is started below.
4823 *
4824 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
4825 * are defined.
4826 *
4827 * ASC-3550 has 8KB internal memory.
4828 */
4829 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
4830 BIOS_EN | RAM_SZ_8KB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004832 /*
4833 * Set SEL_MASK Microcode Default Value
4834 *
4835 * The microcode will set the SEL_MASK register using this value
4836 * after it is started below.
4837 */
4838 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
4839 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004841 AdvBuildCarrierFreelist(asc_dvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004843 /*
4844 * Set-up the Host->RISC Initiator Command Queue (ICQ).
4845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004847 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
4848 if (!asc_dvc->icq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004849 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4850 return ADV_ERROR;
4851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004853 /*
4854 * Set RISC ICQ physical address start value.
4855 */
4856 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004858 /*
4859 * Set-up the RISC->Host Initiator Response Queue (IRQ).
4860 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004861 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
4862 if (!asc_dvc->irq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004863 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4864 return ADV_ERROR;
4865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004867 /*
4868 * Set RISC IRQ physical address start value.
4869 */
4870 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
4871 asc_dvc->carr_pending_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004873 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
4874 (ADV_INTR_ENABLE_HOST_INTR |
4875 ADV_INTR_ENABLE_GLOBAL_INTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004877 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
4878 AdvWriteWordRegister(iop_base, IOPW_PC, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004880 /* finally, finally, gentlemen, start your engine */
4881 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004883 /*
4884 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
4885 * Resets should be performed. The RISC has to be running
4886 * to issue a SCSI Bus Reset.
4887 */
4888 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
4889 /*
4890 * If the BIOS Signature is present in memory, restore the
4891 * BIOS Handshake Configuration Table and do not perform
4892 * a SCSI Bus Reset.
4893 */
4894 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
4895 0x55AA) {
4896 /*
4897 * Restore per TID negotiated values.
4898 */
4899 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4900 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4901 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
4902 tagqng_able);
4903 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4904 AdvWriteByteLram(iop_base,
4905 ASC_MC_NUMBER_OF_MAX_CMD + tid,
4906 max_cmd[tid]);
4907 }
4908 } else {
4909 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
4910 warn_code = ASC_WARN_BUSRESET_ERROR;
4911 }
4912 }
4913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004915 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
4918/*
4919 * Initialize the ASC-38C0800.
4920 *
4921 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4922 *
4923 * For a non-fatal error return a warning code. If there are no warnings
4924 * then 0 is returned.
4925 *
4926 * Needed after initialization for error recovery.
4927 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004928static int AdvInitAsc38C0800Driver(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304930 const struct firmware *fw;
4931 const char fwname[] = "advansys/38C0800.bin";
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004932 AdvPortAddr iop_base;
4933 ushort warn_code;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004934 int begin_addr;
4935 int end_addr;
4936 ushort code_sum;
4937 int word;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004938 int i;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304939 int err;
4940 unsigned long chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004941 ushort scsi_cfg1;
4942 uchar byte;
4943 uchar tid;
4944 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
4945 ushort wdtr_able, sdtr_able, tagqng_able;
4946 uchar max_cmd[ADV_MAX_TID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004948 /* If there is already an error, don't continue. */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004949 if (asc_dvc->err_code != 0)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004950 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004952 /*
4953 * The caller must set 'chip_type' to ADV_CHIP_ASC38C0800.
4954 */
4955 if (asc_dvc->chip_type != ADV_CHIP_ASC38C0800) {
4956 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
4957 return ADV_ERROR;
4958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004960 warn_code = 0;
4961 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004963 /*
4964 * Save the RISC memory BIOS region before writing the microcode.
4965 * The BIOS may already be loaded and using its RISC LRAM region
4966 * so its region must be saved and restored.
4967 *
4968 * Note: This code makes the assumption, which is currently true,
4969 * that a chip reset does not clear RISC LRAM.
4970 */
4971 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4972 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4973 bios_mem[i]);
4974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004976 /*
4977 * Save current per TID negotiated values.
4978 */
4979 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4980 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4981 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4982 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4983 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4984 max_cmd[tid]);
4985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004987 /*
4988 * RAM BIST (RAM Built-In Self Test)
4989 *
4990 * Address : I/O base + offset 0x38h register (byte).
4991 * Function: Bit 7-6(RW) : RAM mode
4992 * Normal Mode : 0x00
4993 * Pre-test Mode : 0x40
4994 * RAM Test Mode : 0x80
4995 * Bit 5 : unused
4996 * Bit 4(RO) : Done bit
4997 * Bit 3-0(RO) : Status
4998 * Host Error : 0x08
4999 * Int_RAM Error : 0x04
5000 * RISC Error : 0x02
5001 * SCSI Error : 0x01
5002 * No Error : 0x00
5003 *
5004 * Note: RAM BIST code should be put right here, before loading the
5005 * microcode and after saving the RISC memory BIOS region.
5006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005008 /*
5009 * LRAM Pre-test
5010 *
5011 * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
5012 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
5013 * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5014 * to NORMAL_MODE, return an error too.
5015 */
5016 for (i = 0; i < 2; i++) {
5017 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005018 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005019 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5020 if ((byte & RAM_TEST_DONE) == 0
5021 || (byte & 0x0F) != PRE_TEST_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005022 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005023 return ADV_ERROR;
5024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005026 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005027 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005028 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5029 != NORMAL_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005030 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005031 return ADV_ERROR;
5032 }
5033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005035 /*
5036 * LRAM Test - It takes about 1.5 ms to run through the test.
5037 *
5038 * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5039 * If Done bit not set or Status not 0, save register byte, set the
5040 * err_code, and return an error.
5041 */
5042 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005043 mdelay(10); /* Wait for 10ms before checking status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005045 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5046 if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5047 /* Get here if Done bit not set or Status not 0. */
5048 asc_dvc->bist_err_code = byte; /* for BIOS display message */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005049 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005050 return ADV_ERROR;
5051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005053 /* We need to reset back to normal mode after LRAM test passes. */
5054 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305056 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5057 if (err) {
5058 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5059 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03005060 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305061 return err;
5062 }
5063 if (fw->size < 4) {
5064 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5065 fw->size, fwname);
5066 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03005067 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305068 return -EINVAL;
5069 }
5070 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5071 (fw->data[1] << 8) | fw->data[0];
5072 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5073 fw->size - 4, ADV_38C0800_MEMSIZE,
5074 chksum);
5075 release_firmware(fw);
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005076 if (asc_dvc->err_code)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005077 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005079 /*
5080 * Restore the RISC memory BIOS region.
5081 */
5082 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5083 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5084 bios_mem[i]);
5085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005087 /*
5088 * Calculate and write the microcode code checksum to the microcode
5089 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5090 */
5091 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5092 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5093 code_sum = 0;
5094 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5095 for (word = begin_addr; word < end_addr; word += 2) {
5096 code_sum += AdvReadWordAutoIncLram(iop_base);
5097 }
5098 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005100 /*
5101 * Read microcode version and date.
5102 */
5103 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5104 asc_dvc->cfg->mcode_date);
5105 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5106 asc_dvc->cfg->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005108 /*
5109 * Set the chip type to indicate the ASC38C0800.
5110 */
5111 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005113 /*
5114 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5115 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5116 * cable detection and then we are able to read C_DET[3:0].
5117 *
5118 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5119 * Microcode Default Value' section below.
5120 */
5121 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5122 AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5123 scsi_cfg1 | DIS_TERM_DRV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005125 /*
5126 * If the PCI Configuration Command Register "Parity Error Response
5127 * Control" Bit was clear (0), then set the microcode variable
5128 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5129 * to ignore DMA parity errors.
5130 */
5131 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5132 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5133 word |= CONTROL_FLAG_IGNORE_PERR;
5134 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005137 /*
5138 * For ASC-38C0800, set FIFO_THRESH_80B [6:4] bits and START_CTL_TH [3:2]
5139 * bits for the default FIFO threshold.
5140 *
5141 * Note: ASC-38C0800 FIFO threshold has been changed to 256 bytes.
5142 *
5143 * For DMA Errata #4 set the BC_THRESH_ENB bit.
5144 */
5145 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5146 BC_THRESH_ENB | FIFO_THRESH_80B | START_CTL_TH |
5147 READ_CMD_MRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005149 /*
5150 * Microcode operating variables for WDTR, SDTR, and command tag
Matthew Wilcox47d853c2007-07-26 11:41:33 -04005151 * queuing will be set in slave_configure() based on what a
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005152 * device reports it is capable of in Inquiry byte 7.
5153 *
5154 * If SCSI Bus Resets have been disabled, then directly set
5155 * SDTR and WDTR from the EEPROM configuration. This will allow
5156 * the BIOS and warm boot to work without a SCSI bus hang on
5157 * the Inquiry caused by host and target mismatched DTR values.
5158 * Without the SCSI Bus Reset, before an Inquiry a device can't
5159 * be assumed to be in Asynchronous, Narrow mode.
5160 */
5161 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5162 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5163 asc_dvc->wdtr_able);
5164 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5165 asc_dvc->sdtr_able);
5166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005168 /*
5169 * Set microcode operating variables for DISC and SDTR_SPEED1,
5170 * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5171 * configuration values.
5172 *
5173 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5174 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5175 * without determining here whether the device supports SDTR.
5176 */
5177 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5178 asc_dvc->cfg->disc_enable);
5179 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5180 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5181 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5182 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005184 /*
5185 * Set SCSI_CFG0 Microcode Default Value.
5186 *
5187 * The microcode will set the SCSI_CFG0 register using this value
5188 * after it is started below.
5189 */
5190 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5191 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5192 asc_dvc->chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005194 /*
5195 * Determine SCSI_CFG1 Microcode Default Value.
5196 *
5197 * The microcode will set the SCSI_CFG1 register using this value
5198 * after it is started below.
5199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005201 /* Read current SCSI_CFG1 Register value. */
5202 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005204 /*
5205 * If the internal narrow cable is reversed all of the SCSI_CTRL
5206 * register signals will be set. Check for and return an error if
5207 * this condition is found.
5208 */
5209 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5210 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5211 return ADV_ERROR;
5212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005214 /*
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005215 * All kind of combinations of devices attached to one of four
5216 * connectors are acceptable except HVD device attached. For example,
5217 * LVD device can be attached to SE connector while SE device attached
5218 * to LVD connector. If LVD device attached to SE connector, it only
5219 * runs up to Ultra speed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005220 *
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005221 * If an HVD device is attached to one of LVD connectors, return an
5222 * error. However, there is no way to detect HVD device attached to
5223 * SE connectors.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005224 */
5225 if (scsi_cfg1 & HVD) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005226 asc_dvc->err_code = ASC_IERR_HVD_DEVICE;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005227 return ADV_ERROR;
5228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005230 /*
5231 * If either SE or LVD automatic termination control is enabled, then
5232 * set the termination value based on a table listed in a_condor.h.
5233 *
5234 * If manual termination was specified with an EEPROM setting then
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005235 * 'termination' was set-up in AdvInitFrom38C0800EEPROM() and is ready
5236 * to be 'ored' into SCSI_CFG1.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005237 */
5238 if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
5239 /* SE automatic termination control is enabled. */
5240 switch (scsi_cfg1 & C_DET_SE) {
5241 /* TERM_SE_HI: on, TERM_SE_LO: on */
5242 case 0x1:
5243 case 0x2:
5244 case 0x3:
5245 asc_dvc->cfg->termination |= TERM_SE;
5246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005248 /* TERM_SE_HI: on, TERM_SE_LO: off */
5249 case 0x0:
5250 asc_dvc->cfg->termination |= TERM_SE_HI;
5251 break;
5252 }
5253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005255 if ((asc_dvc->cfg->termination & TERM_LVD) == 0) {
5256 /* LVD automatic termination control is enabled. */
5257 switch (scsi_cfg1 & C_DET_LVD) {
5258 /* TERM_LVD_HI: on, TERM_LVD_LO: on */
5259 case 0x4:
5260 case 0x8:
5261 case 0xC:
5262 asc_dvc->cfg->termination |= TERM_LVD;
5263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005265 /* TERM_LVD_HI: off, TERM_LVD_LO: off */
5266 case 0x0:
5267 break;
5268 }
5269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005271 /*
5272 * Clear any set TERM_SE and TERM_LVD bits.
5273 */
5274 scsi_cfg1 &= (~TERM_SE & ~TERM_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005276 /*
5277 * Invert the TERM_SE and TERM_LVD bits and then set 'scsi_cfg1'.
5278 */
5279 scsi_cfg1 |= (~asc_dvc->cfg->termination & 0xF0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005281 /*
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005282 * Clear BIG_ENDIAN, DIS_TERM_DRV, Terminator Polarity and HVD/LVD/SE
5283 * bits and set possibly modified termination control bits in the
5284 * Microcode SCSI_CFG1 Register Value.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005285 */
5286 scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL & ~HVD_LVD_SE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005288 /*
5289 * Set SCSI_CFG1 Microcode Default Value
5290 *
5291 * Set possibly modified termination control and reset DIS_TERM_DRV
5292 * bits in the Microcode SCSI_CFG1 Register Value.
5293 *
5294 * The microcode will set the SCSI_CFG1 register using this value
5295 * after it is started below.
5296 */
5297 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005299 /*
5300 * Set MEM_CFG Microcode Default Value
5301 *
5302 * The microcode will set the MEM_CFG register using this value
5303 * after it is started below.
5304 *
5305 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5306 * are defined.
5307 *
5308 * ASC-38C0800 has 16KB internal memory.
5309 */
5310 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5311 BIOS_EN | RAM_SZ_16KB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005313 /*
5314 * Set SEL_MASK Microcode Default Value
5315 *
5316 * The microcode will set the SEL_MASK register using this value
5317 * after it is started below.
5318 */
5319 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5320 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06005322 AdvBuildCarrierFreelist(asc_dvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005324 /*
5325 * Set-up the Host->RISC Initiator Command Queue (ICQ).
5326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005328 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5329 if (!asc_dvc->icq_sp) {
5330 ASC_DBG(0, "Failed to get ICQ carrier\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005331 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5332 return ADV_ERROR;
5333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005335 /*
5336 * Set RISC ICQ physical address start value.
5337 * carr_pa is LE, must be native before write
5338 */
5339 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005341 /*
5342 * Set-up the RISC->Host Initiator Response Queue (IRQ).
5343 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005344 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5345 if (!asc_dvc->irq_sp) {
5346 ASC_DBG(0, "Failed to get IRQ carrier\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005347 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5348 return ADV_ERROR;
5349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005351 /*
5352 * Set RISC IRQ physical address start value.
5353 *
5354 * carr_pa is LE, must be native before write *
5355 */
5356 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5357 asc_dvc->carr_pending_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005359 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5360 (ADV_INTR_ENABLE_HOST_INTR |
5361 ADV_INTR_ENABLE_GLOBAL_INTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005363 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5364 AdvWriteWordRegister(iop_base, IOPW_PC, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005366 /* finally, finally, gentlemen, start your engine */
5367 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005369 /*
5370 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5371 * Resets should be performed. The RISC has to be running
5372 * to issue a SCSI Bus Reset.
5373 */
5374 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5375 /*
5376 * If the BIOS Signature is present in memory, restore the
5377 * BIOS Handshake Configuration Table and do not perform
5378 * a SCSI Bus Reset.
5379 */
5380 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5381 0x55AA) {
5382 /*
5383 * Restore per TID negotiated values.
5384 */
5385 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5386 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5387 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5388 tagqng_able);
5389 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5390 AdvWriteByteLram(iop_base,
5391 ASC_MC_NUMBER_OF_MAX_CMD + tid,
5392 max_cmd[tid]);
5393 }
5394 } else {
5395 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5396 warn_code = ASC_WARN_BUSRESET_ERROR;
5397 }
5398 }
5399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005401 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
5404/*
5405 * Initialize the ASC-38C1600.
5406 *
5407 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
5408 *
5409 * For a non-fatal error return a warning code. If there are no warnings
5410 * then 0 is returned.
5411 *
5412 * Needed after initialization for error recovery.
5413 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005414static int AdvInitAsc38C1600Driver(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305416 const struct firmware *fw;
5417 const char fwname[] = "advansys/38C1600.bin";
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005418 AdvPortAddr iop_base;
5419 ushort warn_code;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005420 int begin_addr;
5421 int end_addr;
5422 ushort code_sum;
5423 long word;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005424 int i;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305425 int err;
5426 unsigned long chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005427 ushort scsi_cfg1;
5428 uchar byte;
5429 uchar tid;
5430 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
5431 ushort wdtr_able, sdtr_able, ppr_able, tagqng_able;
5432 uchar max_cmd[ASC_MAX_TID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005434 /* If there is already an error, don't continue. */
5435 if (asc_dvc->err_code != 0) {
5436 return ADV_ERROR;
5437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005439 /*
5440 * The caller must set 'chip_type' to ADV_CHIP_ASC38C1600.
5441 */
5442 if (asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
5443 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
5444 return ADV_ERROR;
5445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005447 warn_code = 0;
5448 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005450 /*
5451 * Save the RISC memory BIOS region before writing the microcode.
5452 * The BIOS may already be loaded and using its RISC LRAM region
5453 * so its region must be saved and restored.
5454 *
5455 * Note: This code makes the assumption, which is currently true,
5456 * that a chip reset does not clear RISC LRAM.
5457 */
5458 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5459 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5460 bios_mem[i]);
5461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005463 /*
5464 * Save current per TID negotiated values.
5465 */
5466 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5467 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5468 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5469 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5470 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5471 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5472 max_cmd[tid]);
5473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005475 /*
5476 * RAM BIST (Built-In Self Test)
5477 *
5478 * Address : I/O base + offset 0x38h register (byte).
5479 * Function: Bit 7-6(RW) : RAM mode
5480 * Normal Mode : 0x00
5481 * Pre-test Mode : 0x40
5482 * RAM Test Mode : 0x80
5483 * Bit 5 : unused
5484 * Bit 4(RO) : Done bit
5485 * Bit 3-0(RO) : Status
5486 * Host Error : 0x08
5487 * Int_RAM Error : 0x04
5488 * RISC Error : 0x02
5489 * SCSI Error : 0x01
5490 * No Error : 0x00
5491 *
5492 * Note: RAM BIST code should be put right here, before loading the
5493 * microcode and after saving the RISC memory BIOS region.
5494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005496 /*
5497 * LRAM Pre-test
5498 *
5499 * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
5500 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
5501 * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5502 * to NORMAL_MODE, return an error too.
5503 */
5504 for (i = 0; i < 2; i++) {
5505 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005506 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005507 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5508 if ((byte & RAM_TEST_DONE) == 0
5509 || (byte & 0x0F) != PRE_TEST_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005510 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005511 return ADV_ERROR;
5512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005514 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005515 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005516 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5517 != NORMAL_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005518 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005519 return ADV_ERROR;
5520 }
5521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005523 /*
5524 * LRAM Test - It takes about 1.5 ms to run through the test.
5525 *
5526 * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5527 * If Done bit not set or Status not 0, save register byte, set the
5528 * err_code, and return an error.
5529 */
5530 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005531 mdelay(10); /* Wait for 10ms before checking status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005533 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5534 if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5535 /* Get here if Done bit not set or Status not 0. */
5536 asc_dvc->bist_err_code = byte; /* for BIOS display message */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005537 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005538 return ADV_ERROR;
5539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005541 /* We need to reset back to normal mode after LRAM test passes. */
5542 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305544 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5545 if (err) {
5546 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5547 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03005548 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305549 return err;
5550 }
5551 if (fw->size < 4) {
5552 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5553 fw->size, fwname);
5554 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03005555 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305556 return -EINVAL;
5557 }
5558 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5559 (fw->data[1] << 8) | fw->data[0];
5560 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5561 fw->size - 4, ADV_38C1600_MEMSIZE,
5562 chksum);
5563 release_firmware(fw);
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005564 if (asc_dvc->err_code)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005565 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005567 /*
5568 * Restore the RISC memory BIOS region.
5569 */
5570 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5571 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5572 bios_mem[i]);
5573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005575 /*
5576 * Calculate and write the microcode code checksum to the microcode
5577 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5578 */
5579 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5580 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5581 code_sum = 0;
5582 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5583 for (word = begin_addr; word < end_addr; word += 2) {
5584 code_sum += AdvReadWordAutoIncLram(iop_base);
5585 }
5586 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005588 /*
5589 * Read microcode version and date.
5590 */
5591 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5592 asc_dvc->cfg->mcode_date);
5593 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5594 asc_dvc->cfg->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005596 /*
5597 * Set the chip type to indicate the ASC38C1600.
5598 */
5599 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C1600);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005601 /*
5602 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5603 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5604 * cable detection and then we are able to read C_DET[3:0].
5605 *
5606 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5607 * Microcode Default Value' section below.
5608 */
5609 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5610 AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5611 scsi_cfg1 | DIS_TERM_DRV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005613 /*
5614 * If the PCI Configuration Command Register "Parity Error Response
5615 * Control" Bit was clear (0), then set the microcode variable
5616 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5617 * to ignore DMA parity errors.
5618 */
5619 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5620 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5621 word |= CONTROL_FLAG_IGNORE_PERR;
5622 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005625 /*
5626 * If the BIOS control flag AIPP (Asynchronous Information
5627 * Phase Protection) disable bit is not set, then set the firmware
5628 * 'control_flag' CONTROL_FLAG_ENABLE_AIPP bit to enable
5629 * AIPP checking and encoding.
5630 */
5631 if ((asc_dvc->bios_ctrl & BIOS_CTRL_AIPP_DIS) == 0) {
5632 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5633 word |= CONTROL_FLAG_ENABLE_AIPP;
5634 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005637 /*
5638 * For ASC-38C1600 use DMA_CFG0 default values: FIFO_THRESH_80B [6:4],
5639 * and START_CTL_TH [3:2].
5640 */
5641 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5642 FIFO_THRESH_80B | START_CTL_TH | READ_CMD_MRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005644 /*
5645 * Microcode operating variables for WDTR, SDTR, and command tag
Matthew Wilcox47d853c2007-07-26 11:41:33 -04005646 * queuing will be set in slave_configure() based on what a
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005647 * device reports it is capable of in Inquiry byte 7.
5648 *
5649 * If SCSI Bus Resets have been disabled, then directly set
5650 * SDTR and WDTR from the EEPROM configuration. This will allow
5651 * the BIOS and warm boot to work without a SCSI bus hang on
5652 * the Inquiry caused by host and target mismatched DTR values.
5653 * Without the SCSI Bus Reset, before an Inquiry a device can't
5654 * be assumed to be in Asynchronous, Narrow mode.
5655 */
5656 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5657 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5658 asc_dvc->wdtr_able);
5659 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5660 asc_dvc->sdtr_able);
5661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005663 /*
5664 * Set microcode operating variables for DISC and SDTR_SPEED1,
5665 * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5666 * configuration values.
5667 *
5668 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5669 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5670 * without determining here whether the device supports SDTR.
5671 */
5672 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5673 asc_dvc->cfg->disc_enable);
5674 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5675 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5676 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5677 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005679 /*
5680 * Set SCSI_CFG0 Microcode Default Value.
5681 *
5682 * The microcode will set the SCSI_CFG0 register using this value
5683 * after it is started below.
5684 */
5685 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5686 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5687 asc_dvc->chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005689 /*
5690 * Calculate SCSI_CFG1 Microcode Default Value.
5691 *
5692 * The microcode will set the SCSI_CFG1 register using this value
5693 * after it is started below.
5694 *
5695 * Each ASC-38C1600 function has only two cable detect bits.
5696 * The bus mode override bits are in IOPB_SOFT_OVER_WR.
5697 */
5698 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005700 /*
5701 * If the cable is reversed all of the SCSI_CTRL register signals
5702 * will be set. Check for and return an error if this condition is
5703 * found.
5704 */
5705 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5706 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5707 return ADV_ERROR;
5708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005710 /*
5711 * Each ASC-38C1600 function has two connectors. Only an HVD device
5712 * can not be connected to either connector. An LVD device or SE device
5713 * may be connected to either connecor. If an SE device is connected,
5714 * then at most Ultra speed (20 Mhz) can be used on both connectors.
5715 *
5716 * If an HVD device is attached, return an error.
5717 */
5718 if (scsi_cfg1 & HVD) {
5719 asc_dvc->err_code |= ASC_IERR_HVD_DEVICE;
5720 return ADV_ERROR;
5721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005723 /*
5724 * Each function in the ASC-38C1600 uses only the SE cable detect and
5725 * termination because there are two connectors for each function. Each
5726 * function may use either LVD or SE mode. Corresponding the SE automatic
5727 * termination control EEPROM bits are used for each function. Each
5728 * function has its own EEPROM. If SE automatic control is enabled for
5729 * the function, then set the termination value based on a table listed
5730 * in a_condor.h.
5731 *
5732 * If manual termination is specified in the EEPROM for the function,
5733 * then 'termination' was set-up in AscInitFrom38C1600EEPROM() and is
5734 * ready to be 'ored' into SCSI_CFG1.
5735 */
5736 if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
Matthew Wilcox13ac2d92007-07-30 08:10:23 -06005737 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005738 /* SE automatic termination control is enabled. */
5739 switch (scsi_cfg1 & C_DET_SE) {
5740 /* TERM_SE_HI: on, TERM_SE_LO: on */
5741 case 0x1:
5742 case 0x2:
5743 case 0x3:
5744 asc_dvc->cfg->termination |= TERM_SE;
5745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005747 case 0x0:
Matthew Wilcox13ac2d92007-07-30 08:10:23 -06005748 if (PCI_FUNC(pdev->devfn) == 0) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005749 /* Function 0 - TERM_SE_HI: off, TERM_SE_LO: off */
5750 } else {
5751 /* Function 1 - TERM_SE_HI: on, TERM_SE_LO: off */
5752 asc_dvc->cfg->termination |= TERM_SE_HI;
5753 }
5754 break;
5755 }
5756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005758 /*
5759 * Clear any set TERM_SE bits.
5760 */
5761 scsi_cfg1 &= ~TERM_SE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005763 /*
5764 * Invert the TERM_SE bits and then set 'scsi_cfg1'.
5765 */
5766 scsi_cfg1 |= (~asc_dvc->cfg->termination & TERM_SE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005768 /*
5769 * Clear Big Endian and Terminator Polarity bits and set possibly
5770 * modified termination control bits in the Microcode SCSI_CFG1
5771 * Register Value.
5772 *
5773 * Big Endian bit is not used even on big endian machines.
5774 */
5775 scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005777 /*
5778 * Set SCSI_CFG1 Microcode Default Value
5779 *
5780 * Set possibly modified termination control bits in the Microcode
5781 * SCSI_CFG1 Register Value.
5782 *
5783 * The microcode will set the SCSI_CFG1 register using this value
5784 * after it is started below.
5785 */
5786 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005788 /*
5789 * Set MEM_CFG Microcode Default Value
5790 *
5791 * The microcode will set the MEM_CFG register using this value
5792 * after it is started below.
5793 *
5794 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5795 * are defined.
5796 *
5797 * ASC-38C1600 has 32KB internal memory.
5798 *
5799 * XXX - Since ASC38C1600 Rev.3 has a Local RAM failure issue, we come
5800 * out a special 16K Adv Library and Microcode version. After the issue
5801 * resolved, we should turn back to the 32K support. Both a_condor.h and
5802 * mcode.sas files also need to be updated.
5803 *
5804 * AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5805 * BIOS_EN | RAM_SZ_32KB);
5806 */
5807 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5808 BIOS_EN | RAM_SZ_16KB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005810 /*
5811 * Set SEL_MASK Microcode Default Value
5812 *
5813 * The microcode will set the SEL_MASK register using this value
5814 * after it is started below.
5815 */
5816 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5817 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06005819 AdvBuildCarrierFreelist(asc_dvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005821 /*
5822 * Set-up the Host->RISC Initiator Command Queue (ICQ).
5823 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005824 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5825 if (!asc_dvc->icq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005826 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5827 return ADV_ERROR;
5828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005830 /*
5831 * Set RISC ICQ physical address start value. Initialize the
5832 * COMMA register to the same value otherwise the RISC will
5833 * prematurely detect a command is available.
5834 */
5835 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
5836 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
5837 le32_to_cpu(asc_dvc->icq_sp->carr_pa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005839 /*
5840 * Set-up the RISC->Host Initiator Response Queue (IRQ).
5841 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005842 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5843 if (!asc_dvc->irq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005844 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5845 return ADV_ERROR;
5846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005848 /*
5849 * Set RISC IRQ physical address start value.
5850 */
5851 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5852 asc_dvc->carr_pending_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005854 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5855 (ADV_INTR_ENABLE_HOST_INTR |
5856 ADV_INTR_ENABLE_GLOBAL_INTR));
5857 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5858 AdvWriteWordRegister(iop_base, IOPW_PC, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005860 /* finally, finally, gentlemen, start your engine */
5861 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005863 /*
5864 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5865 * Resets should be performed. The RISC has to be running
5866 * to issue a SCSI Bus Reset.
5867 */
5868 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5869 /*
5870 * If the BIOS Signature is present in memory, restore the
5871 * per TID microcode operating variables.
5872 */
5873 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5874 0x55AA) {
5875 /*
5876 * Restore per TID negotiated values.
5877 */
5878 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5879 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5880 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5881 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5882 tagqng_able);
5883 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5884 AdvWriteByteLram(iop_base,
5885 ASC_MC_NUMBER_OF_MAX_CMD + tid,
5886 max_cmd[tid]);
5887 }
5888 } else {
5889 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5890 warn_code = ASC_WARN_BUSRESET_ERROR;
5891 }
5892 }
5893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005895 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896}
5897
5898/*
Matthew Wilcox51219352007-10-02 21:55:22 -04005899 * Reset chip and SCSI Bus.
5900 *
5901 * Return Value:
5902 * ADV_TRUE(1) - Chip re-initialization and SCSI Bus Reset successful.
5903 * ADV_FALSE(0) - Chip re-initialization and SCSI Bus Reset failure.
5904 */
5905static int AdvResetChipAndSB(ADV_DVC_VAR *asc_dvc)
5906{
5907 int status;
5908 ushort wdtr_able, sdtr_able, tagqng_able;
5909 ushort ppr_able = 0;
5910 uchar tid, max_cmd[ADV_MAX_TID + 1];
5911 AdvPortAddr iop_base;
5912 ushort bios_sig;
5913
5914 iop_base = asc_dvc->iop_base;
5915
5916 /*
5917 * Save current per TID negotiated values.
5918 */
5919 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5920 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5921 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5922 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5923 }
5924 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5925 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5926 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5927 max_cmd[tid]);
5928 }
5929
5930 /*
5931 * Force the AdvInitAsc3550/38C0800Driver() function to
5932 * perform a SCSI Bus Reset by clearing the BIOS signature word.
5933 * The initialization functions assumes a SCSI Bus Reset is not
5934 * needed if the BIOS signature word is present.
5935 */
5936 AdvReadWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5937 AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, 0);
5938
5939 /*
5940 * Stop chip and reset it.
5941 */
5942 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_STOP);
5943 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG, ADV_CTRL_REG_CMD_RESET);
5944 mdelay(100);
5945 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
5946 ADV_CTRL_REG_CMD_WR_IO_REG);
5947
5948 /*
5949 * Reset Adv Library error code, if any, and try
5950 * re-initializing the chip.
5951 */
5952 asc_dvc->err_code = 0;
5953 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5954 status = AdvInitAsc38C1600Driver(asc_dvc);
5955 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
5956 status = AdvInitAsc38C0800Driver(asc_dvc);
5957 } else {
5958 status = AdvInitAsc3550Driver(asc_dvc);
5959 }
5960
5961 /* Translate initialization return value to status value. */
5962 if (status == 0) {
5963 status = ADV_TRUE;
5964 } else {
5965 status = ADV_FALSE;
5966 }
5967
5968 /*
5969 * Restore the BIOS signature word.
5970 */
5971 AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5972
5973 /*
5974 * Restore per TID negotiated values.
5975 */
5976 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5977 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5978 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5979 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5980 }
5981 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5982 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5983 AdvWriteByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5984 max_cmd[tid]);
5985 }
5986
5987 return status;
5988}
5989
5990/*
5991 * adv_async_callback() - Adv Library asynchronous event callback function.
5992 */
5993static void adv_async_callback(ADV_DVC_VAR *adv_dvc_varp, uchar code)
5994{
5995 switch (code) {
5996 case ADV_ASYNC_SCSI_BUS_RESET_DET:
5997 /*
5998 * The firmware detected a SCSI Bus reset.
5999 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006000 ASC_DBG(0, "ADV_ASYNC_SCSI_BUS_RESET_DET\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006001 break;
6002
6003 case ADV_ASYNC_RDMA_FAILURE:
6004 /*
6005 * Handle RDMA failure by resetting the SCSI Bus and
6006 * possibly the chip if it is unresponsive. Log the error
6007 * with a unique code.
6008 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006009 ASC_DBG(0, "ADV_ASYNC_RDMA_FAILURE\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006010 AdvResetChipAndSB(adv_dvc_varp);
6011 break;
6012
6013 case ADV_HOST_SCSI_BUS_RESET:
6014 /*
6015 * Host generated SCSI bus reset occurred.
6016 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006017 ASC_DBG(0, "ADV_HOST_SCSI_BUS_RESET\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006018 break;
6019
6020 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006021 ASC_DBG(0, "unknown code 0x%x\n", code);
Matthew Wilcox51219352007-10-02 21:55:22 -04006022 break;
6023 }
6024}
6025
6026/*
6027 * adv_isr_callback() - Second Level Interrupt Handler called by AdvISR().
6028 *
6029 * Callback function for the Wide SCSI Adv Library.
6030 */
6031static void adv_isr_callback(ADV_DVC_VAR *adv_dvc_varp, ADV_SCSI_REQ_Q *scsiqp)
6032{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006033 struct asc_board *boardp = adv_dvc_varp->drv_ptr;
6034 u32 srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04006035 adv_req_t *reqp;
6036 adv_sgblk_t *sgblkp;
6037 struct scsi_cmnd *scp;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006038 u32 resid_cnt;
Hannes Reinecke811ddc02015-04-24 13:18:22 +02006039 dma_addr_t sense_addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04006040
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006041 ASC_DBG(1, "adv_dvc_varp 0x%p, scsiqp 0x%p\n",
6042 adv_dvc_varp, scsiqp);
Matthew Wilcox51219352007-10-02 21:55:22 -04006043 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
6044
6045 /*
6046 * Get the adv_req_t structure for the command that has been
6047 * completed. The adv_req_t structure actually contains the
6048 * completed ADV_SCSI_REQ_Q structure.
6049 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006050 srb_tag = le32_to_cpu(scsiqp->srb_tag);
6051 scp = scsi_host_find_tag(boardp->shost, scsiqp->srb_tag);
Matthew Wilcox51219352007-10-02 21:55:22 -04006052
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006053 ASC_DBG(1, "scp 0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04006054 if (scp == NULL) {
6055 ASC_PRINT
6056 ("adv_isr_callback: scp is NULL; adv_req_t dropped.\n");
6057 return;
6058 }
6059 ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
6060
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006061 reqp = (adv_req_t *)scp->host_scribble;
6062 ASC_DBG(1, "reqp 0x%lx\n", (ulong)reqp);
6063 if (reqp == NULL) {
6064 ASC_PRINT("adv_isr_callback: reqp is NULL\n");
6065 return;
6066 }
6067 /*
6068 * Remove backreferences to avoid duplicate
6069 * command completions.
6070 */
6071 scp->host_scribble = NULL;
6072 reqp->cmndp = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04006073
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006074 ASC_STATS(boardp->shost, callback);
6075 ASC_DBG(1, "shost 0x%p\n", boardp->shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04006076
Hannes Reinecke811ddc02015-04-24 13:18:22 +02006077 sense_addr = le32_to_cpu(scsiqp->sense_addr);
6078 dma_unmap_single(boardp->dev, sense_addr,
6079 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
6080
Matthew Wilcox51219352007-10-02 21:55:22 -04006081 /*
6082 * 'done_status' contains the command's ending status.
6083 */
6084 switch (scsiqp->done_status) {
6085 case QD_NO_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006086 ASC_DBG(2, "QD_NO_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006087 scp->result = 0;
6088
6089 /*
6090 * Check for an underrun condition.
6091 *
6092 * If there was no error and an underrun condition, then
6093 * then return the number of underrun bytes.
6094 */
6095 resid_cnt = le32_to_cpu(scsiqp->data_cnt);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04006096 if (scsi_bufflen(scp) != 0 && resid_cnt != 0 &&
6097 resid_cnt <= scsi_bufflen(scp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006098 ASC_DBG(1, "underrun condition %lu bytes\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04006099 (ulong)resid_cnt);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04006100 scsi_set_resid(scp, resid_cnt);
Matthew Wilcox51219352007-10-02 21:55:22 -04006101 }
6102 break;
6103
6104 case QD_WITH_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006105 ASC_DBG(2, "QD_WITH_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006106 switch (scsiqp->host_status) {
6107 case QHSTA_NO_ERROR:
6108 if (scsiqp->scsi_status == SAM_STAT_CHECK_CONDITION) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006109 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006110 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09006111 SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04006112 /*
6113 * Note: The 'status_byte()' macro used by
6114 * target drivers defined in scsi.h shifts the
6115 * status byte returned by host drivers right
6116 * by 1 bit. This is why target drivers also
6117 * use right shifted status byte definitions.
6118 * For instance target drivers use
6119 * CHECK_CONDITION, defined to 0x1, instead of
6120 * the SCSI defined check condition value of
6121 * 0x2. Host drivers are supposed to return
6122 * the status byte as it is defined by SCSI.
6123 */
6124 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
6125 STATUS_BYTE(scsiqp->scsi_status);
6126 } else {
6127 scp->result = STATUS_BYTE(scsiqp->scsi_status);
6128 }
6129 break;
6130
6131 default:
6132 /* Some other QHSTA error occurred. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006133 ASC_DBG(1, "host_status 0x%x\n", scsiqp->host_status);
Matthew Wilcox51219352007-10-02 21:55:22 -04006134 scp->result = HOST_BYTE(DID_BAD_TARGET);
6135 break;
6136 }
6137 break;
6138
6139 case QD_ABORTED_BY_HOST:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006140 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006141 scp->result =
6142 HOST_BYTE(DID_ABORT) | STATUS_BYTE(scsiqp->scsi_status);
6143 break;
6144
6145 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006146 ASC_DBG(1, "done_status 0x%x\n", scsiqp->done_status);
Matthew Wilcox51219352007-10-02 21:55:22 -04006147 scp->result =
6148 HOST_BYTE(DID_ERROR) | STATUS_BYTE(scsiqp->scsi_status);
6149 break;
6150 }
6151
6152 /*
6153 * If the 'init_tidmask' bit isn't already set for the target and the
6154 * current request finished normally, then set the bit for the target
6155 * to indicate that a device is present.
6156 */
6157 if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
6158 scsiqp->done_status == QD_NO_ERROR &&
6159 scsiqp->host_status == QHSTA_NO_ERROR) {
6160 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
6161 }
6162
6163 asc_scsi_done(scp);
6164
6165 /*
6166 * Free all 'adv_sgblk_t' structures allocated for the request.
6167 */
6168 while ((sgblkp = reqp->sgblkp) != NULL) {
6169 /* Remove 'sgblkp' from the request list. */
6170 reqp->sgblkp = sgblkp->next_sgblkp;
6171
Hannes Reinecke0ce53822015-04-24 13:18:25 +02006172 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
6173 sgblkp->sg_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04006174 }
6175
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006176 ASC_DBG(1, "done\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006177}
6178
6179/*
6180 * Adv Library Interrupt Service Routine
6181 *
6182 * This function is called by a driver's interrupt service routine.
6183 * The function disables and re-enables interrupts.
6184 *
6185 * When a microcode idle command is completed, the ADV_DVC_VAR
6186 * 'idle_cmd_done' field is set to ADV_TRUE.
6187 *
6188 * Note: AdvISR() can be called when interrupts are disabled or even
6189 * when there is no hardware interrupt condition present. It will
6190 * always check for completed idle commands and microcode requests.
6191 * This is an important feature that shouldn't be changed because it
6192 * allows commands to be completed from polling mode loops.
6193 *
6194 * Return:
6195 * ADV_TRUE(1) - interrupt was pending
6196 * ADV_FALSE(0) - no interrupt was pending
6197 */
6198static int AdvISR(ADV_DVC_VAR *asc_dvc)
6199{
6200 AdvPortAddr iop_base;
6201 uchar int_stat;
6202 ushort target_bit;
6203 ADV_CARR_T *free_carrp;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006204 __le32 irq_next_vpa;
Matthew Wilcox51219352007-10-02 21:55:22 -04006205 ADV_SCSI_REQ_Q *scsiq;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02006206 adv_req_t *reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04006207
6208 iop_base = asc_dvc->iop_base;
6209
6210 /* Reading the register clears the interrupt. */
6211 int_stat = AdvReadByteRegister(iop_base, IOPB_INTR_STATUS_REG);
6212
6213 if ((int_stat & (ADV_INTR_STATUS_INTRA | ADV_INTR_STATUS_INTRB |
6214 ADV_INTR_STATUS_INTRC)) == 0) {
6215 return ADV_FALSE;
6216 }
6217
6218 /*
6219 * Notify the driver of an asynchronous microcode condition by
6220 * calling the adv_async_callback function. The function
6221 * is passed the microcode ASC_MC_INTRB_CODE byte value.
6222 */
6223 if (int_stat & ADV_INTR_STATUS_INTRB) {
6224 uchar intrb_code;
6225
6226 AdvReadByteLram(iop_base, ASC_MC_INTRB_CODE, intrb_code);
6227
6228 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
6229 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
6230 if (intrb_code == ADV_ASYNC_CARRIER_READY_FAILURE &&
6231 asc_dvc->carr_pending_cnt != 0) {
6232 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
6233 ADV_TICKLE_A);
6234 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
6235 AdvWriteByteRegister(iop_base,
6236 IOPB_TICKLE,
6237 ADV_TICKLE_NOP);
6238 }
6239 }
6240 }
6241
6242 adv_async_callback(asc_dvc, intrb_code);
6243 }
6244
6245 /*
6246 * Check if the IRQ stopper carrier contains a completed request.
6247 */
6248 while (((irq_next_vpa =
6249 le32_to_cpu(asc_dvc->irq_sp->next_vpa)) & ASC_RQ_DONE) != 0) {
6250 /*
6251 * Get a pointer to the newly completed ADV_SCSI_REQ_Q structure.
6252 * The RISC will have set 'areq_vpa' to a virtual address.
6253 *
6254 * The firmware will have copied the ASC_SCSI_REQ_Q.scsiq_ptr
6255 * field to the carrier ADV_CARR_T.areq_vpa field. The conversion
6256 * below complements the conversion of ASC_SCSI_REQ_Q.scsiq_ptr'
6257 * in AdvExeScsiQueue().
6258 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02006259 u32 pa_offset = le32_to_cpu(asc_dvc->irq_sp->areq_vpa);
6260 ASC_DBG(1, "irq_sp %p areq_vpa %u\n",
6261 asc_dvc->irq_sp, pa_offset);
6262 reqp = adv_get_reqp(asc_dvc, pa_offset);
6263 scsiq = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04006264
6265 /*
6266 * Request finished with good status and the queue was not
6267 * DMAed to host memory by the firmware. Set all status fields
6268 * to indicate good status.
6269 */
6270 if ((irq_next_vpa & ASC_RQ_GOOD) != 0) {
6271 scsiq->done_status = QD_NO_ERROR;
6272 scsiq->host_status = scsiq->scsi_status = 0;
6273 scsiq->data_cnt = 0L;
6274 }
6275
6276 /*
6277 * Advance the stopper pointer to the next carrier
6278 * ignoring the lower four bits. Free the previous
6279 * stopper carrier.
6280 */
6281 free_carrp = asc_dvc->irq_sp;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02006282 asc_dvc->irq_sp = adv_get_carrier(asc_dvc,
6283 ASC_GET_CARRP(irq_next_vpa));
Matthew Wilcox51219352007-10-02 21:55:22 -04006284
Hannes Reinecke98b96a72015-04-24 13:18:23 +02006285 free_carrp->next_vpa = asc_dvc->carr_freelist->carr_va;
Matthew Wilcox51219352007-10-02 21:55:22 -04006286 asc_dvc->carr_freelist = free_carrp;
6287 asc_dvc->carr_pending_cnt--;
6288
6289 target_bit = ADV_TID_TO_TIDMASK(scsiq->target_id);
6290
6291 /*
6292 * Clear request microcode control flag.
6293 */
6294 scsiq->cntl = 0;
6295
6296 /*
6297 * Notify the driver of the completed request by passing
6298 * the ADV_SCSI_REQ_Q pointer to its callback function.
6299 */
6300 scsiq->a_flag |= ADV_SCSIQ_DONE;
6301 adv_isr_callback(asc_dvc, scsiq);
6302 /*
6303 * Note: After the driver callback function is called, 'scsiq'
6304 * can no longer be referenced.
6305 *
6306 * Fall through and continue processing other completed
6307 * requests...
6308 */
6309 }
6310 return ADV_TRUE;
6311}
6312
6313static int AscSetLibErrorCode(ASC_DVC_VAR *asc_dvc, ushort err_code)
6314{
6315 if (asc_dvc->err_code == 0) {
6316 asc_dvc->err_code = err_code;
6317 AscWriteLramWord(asc_dvc->iop_base, ASCV_ASCDVC_ERR_CODE_W,
6318 err_code);
6319 }
6320 return err_code;
6321}
6322
6323static void AscAckInterrupt(PortAddr iop_base)
6324{
6325 uchar host_flag;
6326 uchar risc_flag;
6327 ushort loop;
6328
6329 loop = 0;
6330 do {
6331 risc_flag = AscReadLramByte(iop_base, ASCV_RISC_FLAG_B);
6332 if (loop++ > 0x7FFF) {
6333 break;
6334 }
6335 } while ((risc_flag & ASC_RISC_FLAG_GEN_INT) != 0);
6336 host_flag =
6337 AscReadLramByte(iop_base,
6338 ASCV_HOST_FLAG_B) & (~ASC_HOST_FLAG_ACK_INT);
6339 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
6340 (uchar)(host_flag | ASC_HOST_FLAG_ACK_INT));
6341 AscSetChipStatus(iop_base, CIW_INT_ACK);
6342 loop = 0;
6343 while (AscGetChipStatus(iop_base) & CSW_INT_PENDING) {
6344 AscSetChipStatus(iop_base, CIW_INT_ACK);
6345 if (loop++ > 3) {
6346 break;
6347 }
6348 }
6349 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
Matthew Wilcox51219352007-10-02 21:55:22 -04006350}
6351
6352static uchar AscGetSynPeriodIndex(ASC_DVC_VAR *asc_dvc, uchar syn_time)
6353{
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006354 const uchar *period_table;
Matthew Wilcox51219352007-10-02 21:55:22 -04006355 int max_index;
6356 int min_index;
6357 int i;
6358
6359 period_table = asc_dvc->sdtr_period_tbl;
6360 max_index = (int)asc_dvc->max_sdtr_index;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006361 min_index = (int)asc_dvc->min_sdtr_index;
Matthew Wilcox51219352007-10-02 21:55:22 -04006362 if ((syn_time <= period_table[max_index])) {
6363 for (i = min_index; i < (max_index - 1); i++) {
6364 if (syn_time <= period_table[i]) {
6365 return (uchar)i;
6366 }
6367 }
6368 return (uchar)max_index;
6369 } else {
6370 return (uchar)(max_index + 1);
6371 }
6372}
6373
6374static uchar
6375AscMsgOutSDTR(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar sdtr_offset)
6376{
6377 EXT_MSG sdtr_buf;
6378 uchar sdtr_period_index;
6379 PortAddr iop_base;
6380
6381 iop_base = asc_dvc->iop_base;
6382 sdtr_buf.msg_type = EXTENDED_MESSAGE;
6383 sdtr_buf.msg_len = MS_SDTR_LEN;
6384 sdtr_buf.msg_req = EXTENDED_SDTR;
6385 sdtr_buf.xfer_period = sdtr_period;
6386 sdtr_offset &= ASC_SYN_MAX_OFFSET;
6387 sdtr_buf.req_ack_offset = sdtr_offset;
6388 sdtr_period_index = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
6389 if (sdtr_period_index <= asc_dvc->max_sdtr_index) {
6390 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6391 (uchar *)&sdtr_buf,
6392 sizeof(EXT_MSG) >> 1);
6393 return ((sdtr_period_index << 4) | sdtr_offset);
6394 } else {
6395 sdtr_buf.req_ack_offset = 0;
6396 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6397 (uchar *)&sdtr_buf,
6398 sizeof(EXT_MSG) >> 1);
6399 return 0;
6400 }
6401}
6402
6403static uchar
6404AscCalSDTRData(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar syn_offset)
6405{
6406 uchar byte;
6407 uchar sdtr_period_ix;
6408
6409 sdtr_period_ix = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006410 if (sdtr_period_ix > asc_dvc->max_sdtr_index)
Matthew Wilcox51219352007-10-02 21:55:22 -04006411 return 0xFF;
Matthew Wilcox51219352007-10-02 21:55:22 -04006412 byte = (sdtr_period_ix << 4) | (syn_offset & ASC_SYN_MAX_OFFSET);
6413 return byte;
6414}
6415
Hannes Reinecked647c782015-04-24 13:18:26 +02006416static bool AscSetChipSynRegAtID(PortAddr iop_base, uchar id, uchar sdtr_data)
Matthew Wilcox51219352007-10-02 21:55:22 -04006417{
6418 ASC_SCSI_BIT_ID_TYPE org_id;
6419 int i;
Hannes Reinecked647c782015-04-24 13:18:26 +02006420 bool sta = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04006421
6422 AscSetBank(iop_base, 1);
6423 org_id = AscReadChipDvcID(iop_base);
6424 for (i = 0; i <= ASC_MAX_TID; i++) {
6425 if (org_id == (0x01 << i))
6426 break;
6427 }
6428 org_id = (ASC_SCSI_BIT_ID_TYPE) i;
6429 AscWriteChipDvcID(iop_base, id);
6430 if (AscReadChipDvcID(iop_base) == (0x01 << id)) {
6431 AscSetBank(iop_base, 0);
6432 AscSetChipSyn(iop_base, sdtr_data);
6433 if (AscGetChipSyn(iop_base) != sdtr_data) {
Hannes Reinecked647c782015-04-24 13:18:26 +02006434 sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006435 }
6436 } else {
Hannes Reinecked647c782015-04-24 13:18:26 +02006437 sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006438 }
6439 AscSetBank(iop_base, 1);
6440 AscWriteChipDvcID(iop_base, org_id);
6441 AscSetBank(iop_base, 0);
6442 return (sta);
6443}
6444
6445static void AscSetChipSDTR(PortAddr iop_base, uchar sdtr_data, uchar tid_no)
6446{
6447 AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
6448 AscPutMCodeSDTRDoneAtID(iop_base, tid_no, sdtr_data);
6449}
6450
6451static int AscIsrChipHalted(ASC_DVC_VAR *asc_dvc)
6452{
6453 EXT_MSG ext_msg;
6454 EXT_MSG out_msg;
6455 ushort halt_q_addr;
6456 int sdtr_accept;
6457 ushort int_halt_code;
6458 ASC_SCSI_BIT_ID_TYPE scsi_busy;
6459 ASC_SCSI_BIT_ID_TYPE target_id;
6460 PortAddr iop_base;
6461 uchar tag_code;
6462 uchar q_status;
6463 uchar halt_qp;
6464 uchar sdtr_data;
6465 uchar target_ix;
6466 uchar q_cntl, tid_no;
6467 uchar cur_dvc_qng;
6468 uchar asyn_sdtr;
6469 uchar scsi_status;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04006470 struct asc_board *boardp;
Matthew Wilcox51219352007-10-02 21:55:22 -04006471
6472 BUG_ON(!asc_dvc->drv_ptr);
6473 boardp = asc_dvc->drv_ptr;
6474
6475 iop_base = asc_dvc->iop_base;
6476 int_halt_code = AscReadLramWord(iop_base, ASCV_HALTCODE_W);
6477
6478 halt_qp = AscReadLramByte(iop_base, ASCV_CURCDB_B);
6479 halt_q_addr = ASC_QNO_TO_QADDR(halt_qp);
6480 target_ix = AscReadLramByte(iop_base,
6481 (ushort)(halt_q_addr +
6482 (ushort)ASC_SCSIQ_B_TARGET_IX));
6483 q_cntl = AscReadLramByte(iop_base,
6484 (ushort)(halt_q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6485 tid_no = ASC_TIX_TO_TID(target_ix);
6486 target_id = (uchar)ASC_TID_TO_TARGET_ID(tid_no);
6487 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6488 asyn_sdtr = ASYN_SDTR_DATA_FIX_PCI_REV_AB;
6489 } else {
6490 asyn_sdtr = 0;
6491 }
6492 if (int_halt_code == ASC_HALT_DISABLE_ASYN_USE_SYN_FIX) {
6493 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6494 AscSetChipSDTR(iop_base, 0, tid_no);
6495 boardp->sdtr_data[tid_no] = 0;
6496 }
6497 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6498 return (0);
6499 } else if (int_halt_code == ASC_HALT_ENABLE_ASYN_USE_SYN_FIX) {
6500 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6501 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6502 boardp->sdtr_data[tid_no] = asyn_sdtr;
6503 }
6504 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6505 return (0);
6506 } else if (int_halt_code == ASC_HALT_EXTMSG_IN) {
6507 AscMemWordCopyPtrFromLram(iop_base,
6508 ASCV_MSGIN_BEG,
6509 (uchar *)&ext_msg,
6510 sizeof(EXT_MSG) >> 1);
6511
6512 if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6513 ext_msg.msg_req == EXTENDED_SDTR &&
6514 ext_msg.msg_len == MS_SDTR_LEN) {
6515 sdtr_accept = TRUE;
6516 if ((ext_msg.req_ack_offset > ASC_SYN_MAX_OFFSET)) {
6517
6518 sdtr_accept = FALSE;
6519 ext_msg.req_ack_offset = ASC_SYN_MAX_OFFSET;
6520 }
6521 if ((ext_msg.xfer_period <
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006522 asc_dvc->sdtr_period_tbl[asc_dvc->min_sdtr_index])
Matthew Wilcox51219352007-10-02 21:55:22 -04006523 || (ext_msg.xfer_period >
6524 asc_dvc->sdtr_period_tbl[asc_dvc->
6525 max_sdtr_index])) {
6526 sdtr_accept = FALSE;
6527 ext_msg.xfer_period =
6528 asc_dvc->sdtr_period_tbl[asc_dvc->
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006529 min_sdtr_index];
Matthew Wilcox51219352007-10-02 21:55:22 -04006530 }
6531 if (sdtr_accept) {
6532 sdtr_data =
6533 AscCalSDTRData(asc_dvc, ext_msg.xfer_period,
6534 ext_msg.req_ack_offset);
6535 if ((sdtr_data == 0xFF)) {
6536
6537 q_cntl |= QC_MSG_OUT;
6538 asc_dvc->init_sdtr &= ~target_id;
6539 asc_dvc->sdtr_done &= ~target_id;
6540 AscSetChipSDTR(iop_base, asyn_sdtr,
6541 tid_no);
6542 boardp->sdtr_data[tid_no] = asyn_sdtr;
6543 }
6544 }
6545 if (ext_msg.req_ack_offset == 0) {
6546
6547 q_cntl &= ~QC_MSG_OUT;
6548 asc_dvc->init_sdtr &= ~target_id;
6549 asc_dvc->sdtr_done &= ~target_id;
6550 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6551 } else {
6552 if (sdtr_accept && (q_cntl & QC_MSG_OUT)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04006553 q_cntl &= ~QC_MSG_OUT;
6554 asc_dvc->sdtr_done |= target_id;
6555 asc_dvc->init_sdtr |= target_id;
6556 asc_dvc->pci_fix_asyn_xfer &=
6557 ~target_id;
6558 sdtr_data =
6559 AscCalSDTRData(asc_dvc,
6560 ext_msg.xfer_period,
6561 ext_msg.
6562 req_ack_offset);
6563 AscSetChipSDTR(iop_base, sdtr_data,
6564 tid_no);
6565 boardp->sdtr_data[tid_no] = sdtr_data;
6566 } else {
Matthew Wilcox51219352007-10-02 21:55:22 -04006567 q_cntl |= QC_MSG_OUT;
6568 AscMsgOutSDTR(asc_dvc,
6569 ext_msg.xfer_period,
6570 ext_msg.req_ack_offset);
6571 asc_dvc->pci_fix_asyn_xfer &=
6572 ~target_id;
6573 sdtr_data =
6574 AscCalSDTRData(asc_dvc,
6575 ext_msg.xfer_period,
6576 ext_msg.
6577 req_ack_offset);
6578 AscSetChipSDTR(iop_base, sdtr_data,
6579 tid_no);
6580 boardp->sdtr_data[tid_no] = sdtr_data;
6581 asc_dvc->sdtr_done |= target_id;
6582 asc_dvc->init_sdtr |= target_id;
6583 }
6584 }
6585
6586 AscWriteLramByte(iop_base,
6587 (ushort)(halt_q_addr +
6588 (ushort)ASC_SCSIQ_B_CNTL),
6589 q_cntl);
6590 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6591 return (0);
6592 } else if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6593 ext_msg.msg_req == EXTENDED_WDTR &&
6594 ext_msg.msg_len == MS_WDTR_LEN) {
6595
6596 ext_msg.wdtr_width = 0;
6597 AscMemWordCopyPtrToLram(iop_base,
6598 ASCV_MSGOUT_BEG,
6599 (uchar *)&ext_msg,
6600 sizeof(EXT_MSG) >> 1);
6601 q_cntl |= QC_MSG_OUT;
6602 AscWriteLramByte(iop_base,
6603 (ushort)(halt_q_addr +
6604 (ushort)ASC_SCSIQ_B_CNTL),
6605 q_cntl);
6606 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6607 return (0);
6608 } else {
6609
6610 ext_msg.msg_type = MESSAGE_REJECT;
6611 AscMemWordCopyPtrToLram(iop_base,
6612 ASCV_MSGOUT_BEG,
6613 (uchar *)&ext_msg,
6614 sizeof(EXT_MSG) >> 1);
6615 q_cntl |= QC_MSG_OUT;
6616 AscWriteLramByte(iop_base,
6617 (ushort)(halt_q_addr +
6618 (ushort)ASC_SCSIQ_B_CNTL),
6619 q_cntl);
6620 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6621 return (0);
6622 }
6623 } else if (int_halt_code == ASC_HALT_CHK_CONDITION) {
6624
6625 q_cntl |= QC_REQ_SENSE;
6626
6627 if ((asc_dvc->init_sdtr & target_id) != 0) {
6628
6629 asc_dvc->sdtr_done &= ~target_id;
6630
6631 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
6632 q_cntl |= QC_MSG_OUT;
6633 AscMsgOutSDTR(asc_dvc,
6634 asc_dvc->
6635 sdtr_period_tbl[(sdtr_data >> 4) &
6636 (uchar)(asc_dvc->
6637 max_sdtr_index -
6638 1)],
6639 (uchar)(sdtr_data & (uchar)
6640 ASC_SYN_MAX_OFFSET));
6641 }
6642
6643 AscWriteLramByte(iop_base,
6644 (ushort)(halt_q_addr +
6645 (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6646
6647 tag_code = AscReadLramByte(iop_base,
6648 (ushort)(halt_q_addr + (ushort)
6649 ASC_SCSIQ_B_TAG_CODE));
6650 tag_code &= 0xDC;
6651 if ((asc_dvc->pci_fix_asyn_xfer & target_id)
6652 && !(asc_dvc->pci_fix_asyn_xfer_always & target_id)
6653 ) {
6654
6655 tag_code |= (ASC_TAG_FLAG_DISABLE_DISCONNECT
6656 | ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);
6657
6658 }
6659 AscWriteLramByte(iop_base,
6660 (ushort)(halt_q_addr +
6661 (ushort)ASC_SCSIQ_B_TAG_CODE),
6662 tag_code);
6663
6664 q_status = AscReadLramByte(iop_base,
6665 (ushort)(halt_q_addr + (ushort)
6666 ASC_SCSIQ_B_STATUS));
6667 q_status |= (QS_READY | QS_BUSY);
6668 AscWriteLramByte(iop_base,
6669 (ushort)(halt_q_addr +
6670 (ushort)ASC_SCSIQ_B_STATUS),
6671 q_status);
6672
6673 scsi_busy = AscReadLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B);
6674 scsi_busy &= ~target_id;
6675 AscWriteLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6676
6677 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6678 return (0);
6679 } else if (int_halt_code == ASC_HALT_SDTR_REJECTED) {
6680
6681 AscMemWordCopyPtrFromLram(iop_base,
6682 ASCV_MSGOUT_BEG,
6683 (uchar *)&out_msg,
6684 sizeof(EXT_MSG) >> 1);
6685
6686 if ((out_msg.msg_type == EXTENDED_MESSAGE) &&
6687 (out_msg.msg_len == MS_SDTR_LEN) &&
6688 (out_msg.msg_req == EXTENDED_SDTR)) {
6689
6690 asc_dvc->init_sdtr &= ~target_id;
6691 asc_dvc->sdtr_done &= ~target_id;
6692 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6693 boardp->sdtr_data[tid_no] = asyn_sdtr;
6694 }
6695 q_cntl &= ~QC_MSG_OUT;
6696 AscWriteLramByte(iop_base,
6697 (ushort)(halt_q_addr +
6698 (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6699 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6700 return (0);
6701 } else if (int_halt_code == ASC_HALT_SS_QUEUE_FULL) {
6702
6703 scsi_status = AscReadLramByte(iop_base,
6704 (ushort)((ushort)halt_q_addr +
6705 (ushort)
6706 ASC_SCSIQ_SCSI_STATUS));
6707 cur_dvc_qng =
6708 AscReadLramByte(iop_base,
6709 (ushort)((ushort)ASC_QADR_BEG +
6710 (ushort)target_ix));
6711 if ((cur_dvc_qng > 0) && (asc_dvc->cur_dvc_qng[tid_no] > 0)) {
6712
6713 scsi_busy = AscReadLramByte(iop_base,
6714 (ushort)ASCV_SCSIBUSY_B);
6715 scsi_busy |= target_id;
6716 AscWriteLramByte(iop_base,
6717 (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6718 asc_dvc->queue_full_or_busy |= target_id;
6719
6720 if (scsi_status == SAM_STAT_TASK_SET_FULL) {
6721 if (cur_dvc_qng > ASC_MIN_TAGGED_CMD) {
6722 cur_dvc_qng -= 1;
6723 asc_dvc->max_dvc_qng[tid_no] =
6724 cur_dvc_qng;
6725
6726 AscWriteLramByte(iop_base,
6727 (ushort)((ushort)
6728 ASCV_MAX_DVC_QNG_BEG
6729 + (ushort)
6730 tid_no),
6731 cur_dvc_qng);
6732
6733 /*
6734 * Set the device queue depth to the
6735 * number of active requests when the
6736 * QUEUE FULL condition was encountered.
6737 */
6738 boardp->queue_full |= target_id;
6739 boardp->queue_full_cnt[tid_no] =
6740 cur_dvc_qng;
6741 }
6742 }
6743 }
6744 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6745 return (0);
6746 }
6747#if CC_VERY_LONG_SG_LIST
6748 else if (int_halt_code == ASC_HALT_HOST_COPY_SG_LIST_TO_RISC) {
6749 uchar q_no;
6750 ushort q_addr;
6751 uchar sg_wk_q_no;
6752 uchar first_sg_wk_q_no;
6753 ASC_SCSI_Q *scsiq; /* Ptr to driver request. */
6754 ASC_SG_HEAD *sg_head; /* Ptr to driver SG request. */
6755 ASC_SG_LIST_Q scsi_sg_q; /* Structure written to queue. */
6756 ushort sg_list_dwords;
6757 ushort sg_entry_cnt;
6758 uchar next_qp;
6759 int i;
6760
6761 q_no = AscReadLramByte(iop_base, (ushort)ASCV_REQ_SG_LIST_QP);
6762 if (q_no == ASC_QLINK_END)
6763 return 0;
6764
6765 q_addr = ASC_QNO_TO_QADDR(q_no);
6766
6767 /*
6768 * Convert the request's SRB pointer to a host ASC_SCSI_REQ
6769 * structure pointer using a macro provided by the driver.
6770 * The ASC_SCSI_REQ pointer provides a pointer to the
6771 * host ASC_SG_HEAD structure.
6772 */
6773 /* Read request's SRB pointer. */
6774 scsiq = (ASC_SCSI_Q *)
6775 ASC_SRB2SCSIQ(ASC_U32_TO_VADDR(AscReadLramDWord(iop_base,
6776 (ushort)
6777 (q_addr +
6778 ASC_SCSIQ_D_SRBPTR))));
6779
6780 /*
6781 * Get request's first and working SG queue.
6782 */
6783 sg_wk_q_no = AscReadLramByte(iop_base,
6784 (ushort)(q_addr +
6785 ASC_SCSIQ_B_SG_WK_QP));
6786
6787 first_sg_wk_q_no = AscReadLramByte(iop_base,
6788 (ushort)(q_addr +
6789 ASC_SCSIQ_B_FIRST_SG_WK_QP));
6790
6791 /*
6792 * Reset request's working SG queue back to the
6793 * first SG queue.
6794 */
6795 AscWriteLramByte(iop_base,
6796 (ushort)(q_addr +
6797 (ushort)ASC_SCSIQ_B_SG_WK_QP),
6798 first_sg_wk_q_no);
6799
6800 sg_head = scsiq->sg_head;
6801
6802 /*
6803 * Set sg_entry_cnt to the number of SG elements
6804 * that will be completed on this interrupt.
6805 *
6806 * Note: The allocated SG queues contain ASC_MAX_SG_LIST - 1
6807 * SG elements. The data_cnt and data_addr fields which
6808 * add 1 to the SG element capacity are not used when
6809 * restarting SG handling after a halt.
6810 */
6811 if (scsiq->remain_sg_entry_cnt > (ASC_MAX_SG_LIST - 1)) {
6812 sg_entry_cnt = ASC_MAX_SG_LIST - 1;
6813
6814 /*
6815 * Keep track of remaining number of SG elements that
6816 * will need to be handled on the next interrupt.
6817 */
6818 scsiq->remain_sg_entry_cnt -= (ASC_MAX_SG_LIST - 1);
6819 } else {
6820 sg_entry_cnt = scsiq->remain_sg_entry_cnt;
6821 scsiq->remain_sg_entry_cnt = 0;
6822 }
6823
6824 /*
6825 * Copy SG elements into the list of allocated SG queues.
6826 *
6827 * Last index completed is saved in scsiq->next_sg_index.
6828 */
6829 next_qp = first_sg_wk_q_no;
6830 q_addr = ASC_QNO_TO_QADDR(next_qp);
6831 scsi_sg_q.sg_head_qp = q_no;
6832 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
6833 for (i = 0; i < sg_head->queue_cnt; i++) {
6834 scsi_sg_q.seq_no = i + 1;
6835 if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
6836 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
6837 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
6838 /*
6839 * After very first SG queue RISC FW uses next
6840 * SG queue first element then checks sg_list_cnt
6841 * against zero and then decrements, so set
6842 * sg_list_cnt 1 less than number of SG elements
6843 * in each SG queue.
6844 */
6845 scsi_sg_q.sg_list_cnt = ASC_SG_LIST_PER_Q - 1;
6846 scsi_sg_q.sg_cur_list_cnt =
6847 ASC_SG_LIST_PER_Q - 1;
6848 } else {
6849 /*
6850 * This is the last SG queue in the list of
6851 * allocated SG queues. If there are more
6852 * SG elements than will fit in the allocated
6853 * queues, then set the QCSG_SG_XFER_MORE flag.
6854 */
6855 if (scsiq->remain_sg_entry_cnt != 0) {
6856 scsi_sg_q.cntl |= QCSG_SG_XFER_MORE;
6857 } else {
6858 scsi_sg_q.cntl |= QCSG_SG_XFER_END;
6859 }
6860 /* equals sg_entry_cnt * 2 */
6861 sg_list_dwords = sg_entry_cnt << 1;
6862 scsi_sg_q.sg_list_cnt = sg_entry_cnt - 1;
6863 scsi_sg_q.sg_cur_list_cnt = sg_entry_cnt - 1;
6864 sg_entry_cnt = 0;
6865 }
6866
6867 scsi_sg_q.q_no = next_qp;
6868 AscMemWordCopyPtrToLram(iop_base,
6869 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
6870 (uchar *)&scsi_sg_q,
6871 sizeof(ASC_SG_LIST_Q) >> 1);
6872
6873 AscMemDWordCopyPtrToLram(iop_base,
6874 q_addr + ASC_SGQ_LIST_BEG,
6875 (uchar *)&sg_head->
6876 sg_list[scsiq->next_sg_index],
6877 sg_list_dwords);
6878
6879 scsiq->next_sg_index += ASC_SG_LIST_PER_Q;
6880
6881 /*
6882 * If the just completed SG queue contained the
6883 * last SG element, then no more SG queues need
6884 * to be written.
6885 */
6886 if (scsi_sg_q.cntl & QCSG_SG_XFER_END) {
6887 break;
6888 }
6889
6890 next_qp = AscReadLramByte(iop_base,
6891 (ushort)(q_addr +
6892 ASC_SCSIQ_B_FWD));
6893 q_addr = ASC_QNO_TO_QADDR(next_qp);
6894 }
6895
6896 /*
6897 * Clear the halt condition so the RISC will be restarted
6898 * after the return.
6899 */
6900 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
6901 return (0);
6902 }
6903#endif /* CC_VERY_LONG_SG_LIST */
6904 return (0);
6905}
6906
6907/*
6908 * void
6909 * DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6910 *
6911 * Calling/Exit State:
6912 * none
6913 *
6914 * Description:
6915 * Input an ASC_QDONE_INFO structure from the chip
6916 */
6917static void
6918DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6919{
6920 int i;
6921 ushort word;
6922
6923 AscSetChipLramAddr(iop_base, s_addr);
6924 for (i = 0; i < 2 * words; i += 2) {
6925 if (i == 10) {
6926 continue;
6927 }
6928 word = inpw(iop_base + IOP_RAM_DATA);
6929 inbuf[i] = word & 0xff;
6930 inbuf[i + 1] = (word >> 8) & 0xff;
6931 }
6932 ASC_DBG_PRT_HEX(2, "DvcGetQinfo", inbuf, 2 * words);
6933}
6934
6935static uchar
6936_AscCopyLramScsiDoneQ(PortAddr iop_base,
6937 ushort q_addr,
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006938 ASC_QDONE_INFO *scsiq, unsigned int max_dma_count)
Matthew Wilcox51219352007-10-02 21:55:22 -04006939{
6940 ushort _val;
6941 uchar sg_queue_cnt;
6942
6943 DvcGetQinfo(iop_base,
6944 q_addr + ASC_SCSIQ_DONE_INFO_BEG,
6945 (uchar *)scsiq,
6946 (sizeof(ASC_SCSIQ_2) + sizeof(ASC_SCSIQ_3)) / 2);
6947
6948 _val = AscReadLramWord(iop_base,
6949 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS));
6950 scsiq->q_status = (uchar)_val;
6951 scsiq->q_no = (uchar)(_val >> 8);
6952 _val = AscReadLramWord(iop_base,
6953 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6954 scsiq->cntl = (uchar)_val;
6955 sg_queue_cnt = (uchar)(_val >> 8);
6956 _val = AscReadLramWord(iop_base,
6957 (ushort)(q_addr +
6958 (ushort)ASC_SCSIQ_B_SENSE_LEN));
6959 scsiq->sense_len = (uchar)_val;
6960 scsiq->extra_bytes = (uchar)(_val >> 8);
6961
6962 /*
6963 * Read high word of remain bytes from alternate location.
6964 */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006965 scsiq->remain_bytes = (((u32)AscReadLramWord(iop_base,
6966 (ushort)(q_addr +
6967 (ushort)
6968 ASC_SCSIQ_W_ALT_DC1)))
Matthew Wilcox51219352007-10-02 21:55:22 -04006969 << 16);
6970 /*
6971 * Read low word of remain bytes from original location.
6972 */
6973 scsiq->remain_bytes += AscReadLramWord(iop_base,
6974 (ushort)(q_addr + (ushort)
6975 ASC_SCSIQ_DW_REMAIN_XFER_CNT));
6976
6977 scsiq->remain_bytes &= max_dma_count;
6978 return sg_queue_cnt;
6979}
6980
6981/*
6982 * asc_isr_callback() - Second Level Interrupt Handler called by AscISR().
6983 *
6984 * Interrupt callback function for the Narrow SCSI Asc Library.
6985 */
6986static void asc_isr_callback(ASC_DVC_VAR *asc_dvc_varp, ASC_QDONE_INFO *qdonep)
6987{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006988 struct asc_board *boardp = asc_dvc_varp->drv_ptr;
6989 u32 srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04006990 struct scsi_cmnd *scp;
Matthew Wilcox51219352007-10-02 21:55:22 -04006991
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006992 ASC_DBG(1, "asc_dvc_varp 0x%p, qdonep 0x%p\n", asc_dvc_varp, qdonep);
Matthew Wilcox51219352007-10-02 21:55:22 -04006993 ASC_DBG_PRT_ASC_QDONE_INFO(2, qdonep);
6994
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006995 /*
6996 * Decrease the srb_tag by 1 to find the SCSI command
6997 */
6998 srb_tag = qdonep->d2.srb_tag - 1;
6999 scp = scsi_host_find_tag(boardp->shost, srb_tag);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007000 if (!scp)
Matthew Wilcox51219352007-10-02 21:55:22 -04007001 return;
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007002
Matthew Wilcox51219352007-10-02 21:55:22 -04007003 ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
7004
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007005 ASC_STATS(boardp->shost, callback);
Matthew Wilcox51219352007-10-02 21:55:22 -04007006
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007007 dma_unmap_single(boardp->dev, scp->SCp.dma_handle,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007008 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Matthew Wilcox51219352007-10-02 21:55:22 -04007009 /*
7010 * 'qdonep' contains the command's ending status.
7011 */
7012 switch (qdonep->d3.done_stat) {
7013 case QD_NO_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007014 ASC_DBG(2, "QD_NO_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007015 scp->result = 0;
7016
7017 /*
7018 * Check for an underrun condition.
7019 *
7020 * If there was no error and an underrun condition, then
7021 * return the number of underrun bytes.
7022 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007023 if (scsi_bufflen(scp) != 0 && qdonep->remain_bytes != 0 &&
7024 qdonep->remain_bytes <= scsi_bufflen(scp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007025 ASC_DBG(1, "underrun condition %u bytes\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04007026 (unsigned)qdonep->remain_bytes);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007027 scsi_set_resid(scp, qdonep->remain_bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04007028 }
7029 break;
7030
7031 case QD_WITH_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007032 ASC_DBG(2, "QD_WITH_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007033 switch (qdonep->d3.host_stat) {
7034 case QHSTA_NO_ERROR:
7035 if (qdonep->d3.scsi_stat == SAM_STAT_CHECK_CONDITION) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007036 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007037 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007038 SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04007039 /*
7040 * Note: The 'status_byte()' macro used by
7041 * target drivers defined in scsi.h shifts the
7042 * status byte returned by host drivers right
7043 * by 1 bit. This is why target drivers also
7044 * use right shifted status byte definitions.
7045 * For instance target drivers use
7046 * CHECK_CONDITION, defined to 0x1, instead of
7047 * the SCSI defined check condition value of
7048 * 0x2. Host drivers are supposed to return
7049 * the status byte as it is defined by SCSI.
7050 */
7051 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
7052 STATUS_BYTE(qdonep->d3.scsi_stat);
7053 } else {
7054 scp->result = STATUS_BYTE(qdonep->d3.scsi_stat);
7055 }
7056 break;
7057
7058 default:
7059 /* QHSTA error occurred */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007060 ASC_DBG(1, "host_stat 0x%x\n", qdonep->d3.host_stat);
Matthew Wilcox51219352007-10-02 21:55:22 -04007061 scp->result = HOST_BYTE(DID_BAD_TARGET);
7062 break;
7063 }
7064 break;
7065
7066 case QD_ABORTED_BY_HOST:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007067 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007068 scp->result =
7069 HOST_BYTE(DID_ABORT) | MSG_BYTE(qdonep->d3.
7070 scsi_msg) |
7071 STATUS_BYTE(qdonep->d3.scsi_stat);
7072 break;
7073
7074 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007075 ASC_DBG(1, "done_stat 0x%x\n", qdonep->d3.done_stat);
Matthew Wilcox51219352007-10-02 21:55:22 -04007076 scp->result =
7077 HOST_BYTE(DID_ERROR) | MSG_BYTE(qdonep->d3.
7078 scsi_msg) |
7079 STATUS_BYTE(qdonep->d3.scsi_stat);
7080 break;
7081 }
7082
7083 /*
7084 * If the 'init_tidmask' bit isn't already set for the target and the
7085 * current request finished normally, then set the bit for the target
7086 * to indicate that a device is present.
7087 */
7088 if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
7089 qdonep->d3.done_stat == QD_NO_ERROR &&
7090 qdonep->d3.host_stat == QHSTA_NO_ERROR) {
7091 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
7092 }
7093
7094 asc_scsi_done(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007095}
7096
7097static int AscIsrQDone(ASC_DVC_VAR *asc_dvc)
7098{
7099 uchar next_qp;
7100 uchar n_q_used;
7101 uchar sg_list_qp;
7102 uchar sg_queue_cnt;
7103 uchar q_cnt;
7104 uchar done_q_tail;
7105 uchar tid_no;
7106 ASC_SCSI_BIT_ID_TYPE scsi_busy;
7107 ASC_SCSI_BIT_ID_TYPE target_id;
7108 PortAddr iop_base;
7109 ushort q_addr;
7110 ushort sg_q_addr;
7111 uchar cur_target_qng;
7112 ASC_QDONE_INFO scsiq_buf;
7113 ASC_QDONE_INFO *scsiq;
7114 int false_overrun;
7115
7116 iop_base = asc_dvc->iop_base;
7117 n_q_used = 1;
7118 scsiq = (ASC_QDONE_INFO *)&scsiq_buf;
7119 done_q_tail = (uchar)AscGetVarDoneQTail(iop_base);
7120 q_addr = ASC_QNO_TO_QADDR(done_q_tail);
7121 next_qp = AscReadLramByte(iop_base,
7122 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_FWD));
7123 if (next_qp != ASC_QLINK_END) {
7124 AscPutVarDoneQTail(iop_base, next_qp);
7125 q_addr = ASC_QNO_TO_QADDR(next_qp);
7126 sg_queue_cnt = _AscCopyLramScsiDoneQ(iop_base, q_addr, scsiq,
7127 asc_dvc->max_dma_count);
7128 AscWriteLramByte(iop_base,
7129 (ushort)(q_addr +
7130 (ushort)ASC_SCSIQ_B_STATUS),
7131 (uchar)(scsiq->
7132 q_status & (uchar)~(QS_READY |
7133 QS_ABORTED)));
7134 tid_no = ASC_TIX_TO_TID(scsiq->d2.target_ix);
7135 target_id = ASC_TIX_TO_TARGET_ID(scsiq->d2.target_ix);
7136 if ((scsiq->cntl & QC_SG_HEAD) != 0) {
7137 sg_q_addr = q_addr;
7138 sg_list_qp = next_qp;
7139 for (q_cnt = 0; q_cnt < sg_queue_cnt; q_cnt++) {
7140 sg_list_qp = AscReadLramByte(iop_base,
7141 (ushort)(sg_q_addr
7142 + (ushort)
7143 ASC_SCSIQ_B_FWD));
7144 sg_q_addr = ASC_QNO_TO_QADDR(sg_list_qp);
7145 if (sg_list_qp == ASC_QLINK_END) {
7146 AscSetLibErrorCode(asc_dvc,
7147 ASCQ_ERR_SG_Q_LINKS);
7148 scsiq->d3.done_stat = QD_WITH_ERROR;
7149 scsiq->d3.host_stat =
7150 QHSTA_D_QDONE_SG_LIST_CORRUPTED;
7151 goto FATAL_ERR_QDONE;
7152 }
7153 AscWriteLramByte(iop_base,
7154 (ushort)(sg_q_addr + (ushort)
7155 ASC_SCSIQ_B_STATUS),
7156 QS_FREE);
7157 }
7158 n_q_used = sg_queue_cnt + 1;
7159 AscPutVarDoneQTail(iop_base, sg_list_qp);
7160 }
7161 if (asc_dvc->queue_full_or_busy & target_id) {
7162 cur_target_qng = AscReadLramByte(iop_base,
7163 (ushort)((ushort)
7164 ASC_QADR_BEG
7165 + (ushort)
7166 scsiq->d2.
7167 target_ix));
7168 if (cur_target_qng < asc_dvc->max_dvc_qng[tid_no]) {
7169 scsi_busy = AscReadLramByte(iop_base, (ushort)
7170 ASCV_SCSIBUSY_B);
7171 scsi_busy &= ~target_id;
7172 AscWriteLramByte(iop_base,
7173 (ushort)ASCV_SCSIBUSY_B,
7174 scsi_busy);
7175 asc_dvc->queue_full_or_busy &= ~target_id;
7176 }
7177 }
7178 if (asc_dvc->cur_total_qng >= n_q_used) {
7179 asc_dvc->cur_total_qng -= n_q_used;
7180 if (asc_dvc->cur_dvc_qng[tid_no] != 0) {
7181 asc_dvc->cur_dvc_qng[tid_no]--;
7182 }
7183 } else {
7184 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CUR_QNG);
7185 scsiq->d3.done_stat = QD_WITH_ERROR;
7186 goto FATAL_ERR_QDONE;
7187 }
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007188 if ((scsiq->d2.srb_tag == 0UL) ||
Matthew Wilcox51219352007-10-02 21:55:22 -04007189 ((scsiq->q_status & QS_ABORTED) != 0)) {
7190 return (0x11);
7191 } else if (scsiq->q_status == QS_DONE) {
7192 false_overrun = FALSE;
7193 if (scsiq->extra_bytes != 0) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02007194 scsiq->remain_bytes += scsiq->extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04007195 }
7196 if (scsiq->d3.done_stat == QD_WITH_ERROR) {
7197 if (scsiq->d3.host_stat ==
7198 QHSTA_M_DATA_OVER_RUN) {
7199 if ((scsiq->
7200 cntl & (QC_DATA_IN | QC_DATA_OUT))
7201 == 0) {
7202 scsiq->d3.done_stat =
7203 QD_NO_ERROR;
7204 scsiq->d3.host_stat =
7205 QHSTA_NO_ERROR;
7206 } else if (false_overrun) {
7207 scsiq->d3.done_stat =
7208 QD_NO_ERROR;
7209 scsiq->d3.host_stat =
7210 QHSTA_NO_ERROR;
7211 }
7212 } else if (scsiq->d3.host_stat ==
7213 QHSTA_M_HUNG_REQ_SCSI_BUS_RESET) {
7214 AscStopChip(iop_base);
7215 AscSetChipControl(iop_base,
7216 (uchar)(CC_SCSI_RESET
7217 | CC_HALT));
7218 udelay(60);
7219 AscSetChipControl(iop_base, CC_HALT);
7220 AscSetChipStatus(iop_base,
7221 CIW_CLR_SCSI_RESET_INT);
7222 AscSetChipStatus(iop_base, 0);
7223 AscSetChipControl(iop_base, 0);
7224 }
7225 }
7226 if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7227 asc_isr_callback(asc_dvc, scsiq);
7228 } else {
7229 if ((AscReadLramByte(iop_base,
7230 (ushort)(q_addr + (ushort)
7231 ASC_SCSIQ_CDB_BEG))
7232 == START_STOP)) {
7233 asc_dvc->unit_not_ready &= ~target_id;
7234 if (scsiq->d3.done_stat != QD_NO_ERROR) {
7235 asc_dvc->start_motor &=
7236 ~target_id;
7237 }
7238 }
7239 }
7240 return (1);
7241 } else {
7242 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_Q_STATUS);
7243 FATAL_ERR_QDONE:
7244 if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7245 asc_isr_callback(asc_dvc, scsiq);
7246 }
7247 return (0x80);
7248 }
7249 }
7250 return (0);
7251}
7252
7253static int AscISR(ASC_DVC_VAR *asc_dvc)
7254{
7255 ASC_CS_TYPE chipstat;
7256 PortAddr iop_base;
7257 ushort saved_ram_addr;
7258 uchar ctrl_reg;
7259 uchar saved_ctrl_reg;
7260 int int_pending;
7261 int status;
7262 uchar host_flag;
7263
7264 iop_base = asc_dvc->iop_base;
7265 int_pending = FALSE;
7266
7267 if (AscIsIntPending(iop_base) == 0)
7268 return int_pending;
7269
7270 if ((asc_dvc->init_state & ASC_INIT_STATE_END_LOAD_MC) == 0) {
7271 return ERR;
7272 }
7273 if (asc_dvc->in_critical_cnt != 0) {
7274 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_ON_CRITICAL);
7275 return ERR;
7276 }
7277 if (asc_dvc->is_in_int) {
7278 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_RE_ENTRY);
7279 return ERR;
7280 }
7281 asc_dvc->is_in_int = TRUE;
7282 ctrl_reg = AscGetChipControl(iop_base);
7283 saved_ctrl_reg = ctrl_reg & (~(CC_SCSI_RESET | CC_CHIP_RESET |
7284 CC_SINGLE_STEP | CC_DIAG | CC_TEST));
7285 chipstat = AscGetChipStatus(iop_base);
7286 if (chipstat & CSW_SCSI_RESET_LATCH) {
7287 if (!(asc_dvc->bus_type & (ASC_IS_VL | ASC_IS_EISA))) {
7288 int i = 10;
7289 int_pending = TRUE;
7290 asc_dvc->sdtr_done = 0;
7291 saved_ctrl_reg &= (uchar)(~CC_HALT);
7292 while ((AscGetChipStatus(iop_base) &
7293 CSW_SCSI_RESET_ACTIVE) && (i-- > 0)) {
7294 mdelay(100);
7295 }
7296 AscSetChipControl(iop_base, (CC_CHIP_RESET | CC_HALT));
7297 AscSetChipControl(iop_base, CC_HALT);
7298 AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
7299 AscSetChipStatus(iop_base, 0);
7300 chipstat = AscGetChipStatus(iop_base);
7301 }
7302 }
7303 saved_ram_addr = AscGetChipLramAddr(iop_base);
7304 host_flag = AscReadLramByte(iop_base,
7305 ASCV_HOST_FLAG_B) &
7306 (uchar)(~ASC_HOST_FLAG_IN_ISR);
7307 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
7308 (uchar)(host_flag | (uchar)ASC_HOST_FLAG_IN_ISR));
7309 if ((chipstat & CSW_INT_PENDING) || (int_pending)) {
7310 AscAckInterrupt(iop_base);
7311 int_pending = TRUE;
7312 if ((chipstat & CSW_HALTED) && (ctrl_reg & CC_SINGLE_STEP)) {
7313 if (AscIsrChipHalted(asc_dvc) == ERR) {
7314 goto ISR_REPORT_QDONE_FATAL_ERROR;
7315 } else {
7316 saved_ctrl_reg &= (uchar)(~CC_HALT);
7317 }
7318 } else {
7319 ISR_REPORT_QDONE_FATAL_ERROR:
7320 if ((asc_dvc->dvc_cntl & ASC_CNTL_INT_MULTI_Q) != 0) {
7321 while (((status =
7322 AscIsrQDone(asc_dvc)) & 0x01) != 0) {
7323 }
7324 } else {
7325 do {
7326 if ((status =
7327 AscIsrQDone(asc_dvc)) == 1) {
7328 break;
7329 }
7330 } while (status == 0x11);
7331 }
7332 if ((status & 0x80) != 0)
7333 int_pending = ERR;
7334 }
7335 }
7336 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
7337 AscSetChipLramAddr(iop_base, saved_ram_addr);
7338 AscSetChipControl(iop_base, saved_ctrl_reg);
7339 asc_dvc->is_in_int = FALSE;
7340 return int_pending;
7341}
7342
7343/*
7344 * advansys_reset()
7345 *
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007346 * Reset the host associated with the command 'scp'.
Matthew Wilcox51219352007-10-02 21:55:22 -04007347 *
7348 * This function runs its own thread. Interrupts must be blocked but
7349 * sleeping is allowed and no locking other than for host structures is
7350 * required. Returns SUCCESS or FAILED.
7351 */
7352static int advansys_reset(struct scsi_cmnd *scp)
7353{
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007354 struct Scsi_Host *shost = scp->device->host;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007355 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007356 unsigned long flags;
Matthew Wilcox51219352007-10-02 21:55:22 -04007357 int status;
7358 int ret = SUCCESS;
7359
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007360 ASC_DBG(1, "0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007361
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007362 ASC_STATS(shost, reset);
Matthew Wilcox51219352007-10-02 21:55:22 -04007363
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007364 scmd_printk(KERN_INFO, scp, "SCSI host reset started...\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007365
7366 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007367 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04007368
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007369 /* Reset the chip and SCSI bus. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007370 ASC_DBG(1, "before AscInitAsc1000Driver()\n");
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007371 status = AscInitAsc1000Driver(asc_dvc);
Matthew Wilcox51219352007-10-02 21:55:22 -04007372
Adam Buchbinder6070d812009-12-04 15:47:01 -05007373 /* Refer to ASC_IERR_* definitions for meaning of 'err_code'. */
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03007374 if (asc_dvc->err_code || !asc_dvc->overrun_dma) {
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007375 scmd_printk(KERN_INFO, scp, "SCSI host reset error: "
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03007376 "0x%x, status: 0x%x\n", asc_dvc->err_code,
7377 status);
Matthew Wilcox51219352007-10-02 21:55:22 -04007378 ret = FAILED;
7379 } else if (status) {
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007380 scmd_printk(KERN_INFO, scp, "SCSI host reset warning: "
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007381 "0x%x\n", status);
Matthew Wilcox51219352007-10-02 21:55:22 -04007382 } else {
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007383 scmd_printk(KERN_INFO, scp, "SCSI host reset "
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007384 "successful\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007385 }
7386
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007387 ASC_DBG(1, "after AscInitAsc1000Driver()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007388 } else {
7389 /*
Matthew Wilcox51219352007-10-02 21:55:22 -04007390 * If the suggest reset bus flags are set, then reset the bus.
7391 * Otherwise only reset the device.
7392 */
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007393 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04007394
7395 /*
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007396 * Reset the chip and SCSI bus.
Matthew Wilcox51219352007-10-02 21:55:22 -04007397 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007398 ASC_DBG(1, "before AdvResetChipAndSB()\n");
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007399 switch (AdvResetChipAndSB(adv_dvc)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04007400 case ASC_TRUE:
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007401 scmd_printk(KERN_INFO, scp, "SCSI host reset "
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007402 "successful\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007403 break;
7404 case ASC_FALSE:
7405 default:
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007406 scmd_printk(KERN_INFO, scp, "SCSI host reset error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007407 ret = FAILED;
7408 break;
7409 }
Matthew Wilcoxf092d222007-10-02 21:55:34 -04007410 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007411 AdvISR(adv_dvc);
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007412 spin_unlock_irqrestore(shost->host_lock, flags);
Matthew Wilcox51219352007-10-02 21:55:22 -04007413 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007414
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007415 ASC_DBG(1, "ret %d\n", ret);
Matthew Wilcox51219352007-10-02 21:55:22 -04007416
7417 return ret;
7418}
7419
7420/*
7421 * advansys_biosparam()
7422 *
7423 * Translate disk drive geometry if the "BIOS greater than 1 GB"
7424 * support is enabled for a drive.
7425 *
7426 * ip (information pointer) is an int array with the following definition:
7427 * ip[0]: heads
7428 * ip[1]: sectors
7429 * ip[2]: cylinders
7430 */
7431static int
7432advansys_biosparam(struct scsi_device *sdev, struct block_device *bdev,
7433 sector_t capacity, int ip[])
7434{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007435 struct asc_board *boardp = shost_priv(sdev->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04007436
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007437 ASC_DBG(1, "begin\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007438 ASC_STATS(sdev->host, biosparam);
Matthew Wilcox51219352007-10-02 21:55:22 -04007439 if (ASC_NARROW_BOARD(boardp)) {
7440 if ((boardp->dvc_var.asc_dvc_var.dvc_cntl &
7441 ASC_CNTL_BIOS_GT_1GB) && capacity > 0x200000) {
7442 ip[0] = 255;
7443 ip[1] = 63;
7444 } else {
7445 ip[0] = 64;
7446 ip[1] = 32;
7447 }
7448 } else {
7449 if ((boardp->dvc_var.adv_dvc_var.bios_ctrl &
7450 BIOS_CTRL_EXTENDED_XLAT) && capacity > 0x200000) {
7451 ip[0] = 255;
7452 ip[1] = 63;
7453 } else {
7454 ip[0] = 64;
7455 ip[1] = 32;
7456 }
7457 }
7458 ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007459 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007460 return 0;
7461}
7462
7463/*
7464 * First-level interrupt handler.
7465 *
7466 * 'dev_id' is a pointer to the interrupting adapter's Scsi_Host.
7467 */
7468static irqreturn_t advansys_interrupt(int irq, void *dev_id)
7469{
Matthew Wilcox51219352007-10-02 21:55:22 -04007470 struct Scsi_Host *shost = dev_id;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007471 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04007472 irqreturn_t result = IRQ_NONE;
7473
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007474 ASC_DBG(2, "boardp 0x%p\n", boardp);
Matthew Wilcoxf092d222007-10-02 21:55:34 -04007475 spin_lock(shost->host_lock);
Matthew Wilcox51219352007-10-02 21:55:22 -04007476 if (ASC_NARROW_BOARD(boardp)) {
7477 if (AscIsIntPending(shost->io_port)) {
7478 result = IRQ_HANDLED;
7479 ASC_STATS(shost, interrupt);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007480 ASC_DBG(1, "before AscISR()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007481 AscISR(&boardp->dvc_var.asc_dvc_var);
7482 }
7483 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007484 ASC_DBG(1, "before AdvISR()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007485 if (AdvISR(&boardp->dvc_var.adv_dvc_var)) {
7486 result = IRQ_HANDLED;
7487 ASC_STATS(shost, interrupt);
7488 }
7489 }
Matthew Wilcoxf092d222007-10-02 21:55:34 -04007490 spin_unlock(shost->host_lock);
Matthew Wilcox51219352007-10-02 21:55:22 -04007491
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007492 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007493 return result;
7494}
7495
Hannes Reinecked647c782015-04-24 13:18:26 +02007496static bool AscHostReqRiscHalt(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04007497{
7498 int count = 0;
Hannes Reinecked647c782015-04-24 13:18:26 +02007499 bool sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007500 uchar saved_stop_code;
7501
7502 if (AscIsChipHalted(iop_base))
Hannes Reinecked647c782015-04-24 13:18:26 +02007503 return true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007504 saved_stop_code = AscReadLramByte(iop_base, ASCV_STOP_CODE_B);
7505 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
7506 ASC_STOP_HOST_REQ_RISC_HALT | ASC_STOP_REQ_RISC_STOP);
7507 do {
7508 if (AscIsChipHalted(iop_base)) {
Hannes Reinecked647c782015-04-24 13:18:26 +02007509 sta = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007510 break;
7511 }
7512 mdelay(100);
7513 } while (count++ < 20);
7514 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, saved_stop_code);
Hannes Reinecked647c782015-04-24 13:18:26 +02007515 return sta;
Matthew Wilcox51219352007-10-02 21:55:22 -04007516}
7517
Hannes Reinecked647c782015-04-24 13:18:26 +02007518static bool
Matthew Wilcox51219352007-10-02 21:55:22 -04007519AscSetRunChipSynRegAtID(PortAddr iop_base, uchar tid_no, uchar sdtr_data)
7520{
Hannes Reinecked647c782015-04-24 13:18:26 +02007521 bool sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007522
7523 if (AscHostReqRiscHalt(iop_base)) {
7524 sta = AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
7525 AscStartChip(iop_base);
7526 }
7527 return sta;
7528}
7529
7530static void AscAsyncFix(ASC_DVC_VAR *asc_dvc, struct scsi_device *sdev)
7531{
7532 char type = sdev->type;
7533 ASC_SCSI_BIT_ID_TYPE tid_bits = 1 << sdev->id;
7534
7535 if (!(asc_dvc->bug_fix_cntl & ASC_BUG_FIX_ASYN_USE_SYN))
7536 return;
7537 if (asc_dvc->init_sdtr & tid_bits)
7538 return;
7539
7540 if ((type == TYPE_ROM) && (strncmp(sdev->vendor, "HP ", 3) == 0))
7541 asc_dvc->pci_fix_asyn_xfer_always |= tid_bits;
7542
7543 asc_dvc->pci_fix_asyn_xfer |= tid_bits;
7544 if ((type == TYPE_PROCESSOR) || (type == TYPE_SCANNER) ||
7545 (type == TYPE_ROM) || (type == TYPE_TAPE))
7546 asc_dvc->pci_fix_asyn_xfer &= ~tid_bits;
7547
7548 if (asc_dvc->pci_fix_asyn_xfer & tid_bits)
7549 AscSetRunChipSynRegAtID(asc_dvc->iop_base, sdev->id,
7550 ASYN_SDTR_DATA_FIX_PCI_REV_AB);
7551}
7552
7553static void
7554advansys_narrow_slave_configure(struct scsi_device *sdev, ASC_DVC_VAR *asc_dvc)
7555{
7556 ASC_SCSI_BIT_ID_TYPE tid_bit = 1 << sdev->id;
7557 ASC_SCSI_BIT_ID_TYPE orig_use_tagged_qng = asc_dvc->use_tagged_qng;
7558
7559 if (sdev->lun == 0) {
7560 ASC_SCSI_BIT_ID_TYPE orig_init_sdtr = asc_dvc->init_sdtr;
7561 if ((asc_dvc->cfg->sdtr_enable & tid_bit) && sdev->sdtr) {
7562 asc_dvc->init_sdtr |= tid_bit;
7563 } else {
7564 asc_dvc->init_sdtr &= ~tid_bit;
7565 }
7566
7567 if (orig_init_sdtr != asc_dvc->init_sdtr)
7568 AscAsyncFix(asc_dvc, sdev);
7569 }
7570
7571 if (sdev->tagged_supported) {
7572 if (asc_dvc->cfg->cmd_qng_enabled & tid_bit) {
7573 if (sdev->lun == 0) {
7574 asc_dvc->cfg->can_tagged_qng |= tid_bit;
7575 asc_dvc->use_tagged_qng |= tid_bit;
7576 }
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007577 scsi_change_queue_depth(sdev,
Matthew Wilcox51219352007-10-02 21:55:22 -04007578 asc_dvc->max_dvc_qng[sdev->id]);
7579 }
7580 } else {
7581 if (sdev->lun == 0) {
7582 asc_dvc->cfg->can_tagged_qng &= ~tid_bit;
7583 asc_dvc->use_tagged_qng &= ~tid_bit;
7584 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007585 }
7586
7587 if ((sdev->lun == 0) &&
7588 (orig_use_tagged_qng != asc_dvc->use_tagged_qng)) {
7589 AscWriteLramByte(asc_dvc->iop_base, ASCV_DISC_ENABLE_B,
7590 asc_dvc->cfg->disc_enable);
7591 AscWriteLramByte(asc_dvc->iop_base, ASCV_USE_TAGGED_QNG_B,
7592 asc_dvc->use_tagged_qng);
7593 AscWriteLramByte(asc_dvc->iop_base, ASCV_CAN_TAGGED_QNG_B,
7594 asc_dvc->cfg->can_tagged_qng);
7595
7596 asc_dvc->max_dvc_qng[sdev->id] =
7597 asc_dvc->cfg->max_tag_qng[sdev->id];
7598 AscWriteLramByte(asc_dvc->iop_base,
7599 (ushort)(ASCV_MAX_DVC_QNG_BEG + sdev->id),
7600 asc_dvc->max_dvc_qng[sdev->id]);
7601 }
7602}
7603
7604/*
7605 * Wide Transfers
7606 *
7607 * If the EEPROM enabled WDTR for the device and the device supports wide
7608 * bus (16 bit) transfers, then turn on the device's 'wdtr_able' bit and
7609 * write the new value to the microcode.
7610 */
7611static void
7612advansys_wide_enable_wdtr(AdvPortAddr iop_base, unsigned short tidmask)
7613{
7614 unsigned short cfg_word;
7615 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7616 if ((cfg_word & tidmask) != 0)
7617 return;
7618
7619 cfg_word |= tidmask;
7620 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7621
7622 /*
7623 * Clear the microcode SDTR and WDTR negotiation done indicators for
7624 * the target to cause it to negotiate with the new setting set above.
7625 * WDTR when accepted causes the target to enter asynchronous mode, so
7626 * SDTR must be negotiated.
7627 */
7628 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7629 cfg_word &= ~tidmask;
7630 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7631 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7632 cfg_word &= ~tidmask;
7633 AdvWriteWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7634}
7635
7636/*
7637 * Synchronous Transfers
7638 *
7639 * If the EEPROM enabled SDTR for the device and the device
7640 * supports synchronous transfers, then turn on the device's
7641 * 'sdtr_able' bit. Write the new value to the microcode.
7642 */
7643static void
7644advansys_wide_enable_sdtr(AdvPortAddr iop_base, unsigned short tidmask)
7645{
7646 unsigned short cfg_word;
7647 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7648 if ((cfg_word & tidmask) != 0)
7649 return;
7650
7651 cfg_word |= tidmask;
7652 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7653
7654 /*
7655 * Clear the microcode "SDTR negotiation" done indicator for the
7656 * target to cause it to negotiate with the new setting set above.
7657 */
7658 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7659 cfg_word &= ~tidmask;
7660 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7661}
7662
7663/*
7664 * PPR (Parallel Protocol Request) Capable
7665 *
7666 * If the device supports DT mode, then it must be PPR capable.
7667 * The PPR message will be used in place of the SDTR and WDTR
7668 * messages to negotiate synchronous speed and offset, transfer
7669 * width, and protocol options.
7670 */
7671static void advansys_wide_enable_ppr(ADV_DVC_VAR *adv_dvc,
7672 AdvPortAddr iop_base, unsigned short tidmask)
7673{
7674 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7675 adv_dvc->ppr_able |= tidmask;
7676 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7677}
7678
7679static void
7680advansys_wide_slave_configure(struct scsi_device *sdev, ADV_DVC_VAR *adv_dvc)
7681{
7682 AdvPortAddr iop_base = adv_dvc->iop_base;
7683 unsigned short tidmask = 1 << sdev->id;
7684
7685 if (sdev->lun == 0) {
7686 /*
7687 * Handle WDTR, SDTR, and Tag Queuing. If the feature
7688 * is enabled in the EEPROM and the device supports the
7689 * feature, then enable it in the microcode.
7690 */
7691
7692 if ((adv_dvc->wdtr_able & tidmask) && sdev->wdtr)
7693 advansys_wide_enable_wdtr(iop_base, tidmask);
7694 if ((adv_dvc->sdtr_able & tidmask) && sdev->sdtr)
7695 advansys_wide_enable_sdtr(iop_base, tidmask);
7696 if (adv_dvc->chip_type == ADV_CHIP_ASC38C1600 && sdev->ppr)
7697 advansys_wide_enable_ppr(adv_dvc, iop_base, tidmask);
7698
7699 /*
7700 * Tag Queuing is disabled for the BIOS which runs in polled
7701 * mode and would see no benefit from Tag Queuing. Also by
7702 * disabling Tag Queuing in the BIOS devices with Tag Queuing
7703 * bugs will at least work with the BIOS.
7704 */
7705 if ((adv_dvc->tagqng_able & tidmask) &&
7706 sdev->tagged_supported) {
7707 unsigned short cfg_word;
7708 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, cfg_word);
7709 cfg_word |= tidmask;
7710 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
7711 cfg_word);
7712 AdvWriteByteLram(iop_base,
7713 ASC_MC_NUMBER_OF_MAX_CMD + sdev->id,
7714 adv_dvc->max_dvc_qng);
7715 }
7716 }
7717
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007718 if ((adv_dvc->tagqng_able & tidmask) && sdev->tagged_supported)
7719 scsi_change_queue_depth(sdev, adv_dvc->max_dvc_qng);
Matthew Wilcox51219352007-10-02 21:55:22 -04007720}
7721
7722/*
7723 * Set the number of commands to queue per device for the
7724 * specified host adapter.
7725 */
7726static int advansys_slave_configure(struct scsi_device *sdev)
7727{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007728 struct asc_board *boardp = shost_priv(sdev->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04007729
Matthew Wilcox51219352007-10-02 21:55:22 -04007730 if (ASC_NARROW_BOARD(boardp))
7731 advansys_narrow_slave_configure(sdev,
7732 &boardp->dvc_var.asc_dvc_var);
7733 else
7734 advansys_wide_slave_configure(sdev,
7735 &boardp->dvc_var.adv_dvc_var);
7736
7737 return 0;
7738}
7739
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007740static __le32 advansys_get_sense_buffer_dma(struct scsi_cmnd *scp)
7741{
7742 struct asc_board *board = shost_priv(scp->device->host);
7743 scp->SCp.dma_handle = dma_map_single(board->dev, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007744 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007745 dma_cache_sync(board->dev, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007746 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007747 return cpu_to_le32(scp->SCp.dma_handle);
7748}
7749
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007750static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
Matthew Wilcox05848b62007-10-02 21:55:25 -04007751 struct asc_scsi_q *asc_scsi_q)
Matthew Wilcox51219352007-10-02 21:55:22 -04007752{
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007753 struct asc_dvc_var *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007754 int use_sg;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007755 u32 srb_tag;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007756
Matthew Wilcox05848b62007-10-02 21:55:25 -04007757 memset(asc_scsi_q, 0, sizeof(*asc_scsi_q));
Matthew Wilcox51219352007-10-02 21:55:22 -04007758
7759 /*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007760 * Set the srb_tag to the command tag + 1, as
7761 * srb_tag '0' is used internally by the chip.
Matthew Wilcox51219352007-10-02 21:55:22 -04007762 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007763 srb_tag = scp->request->tag + 1;
7764 asc_scsi_q->q2.srb_tag = srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007765
7766 /*
7767 * Build the ASC_SCSI_Q request.
7768 */
Matthew Wilcox05848b62007-10-02 21:55:25 -04007769 asc_scsi_q->cdbptr = &scp->cmnd[0];
7770 asc_scsi_q->q2.cdb_len = scp->cmd_len;
7771 asc_scsi_q->q1.target_id = ASC_TID_TO_TARGET_ID(scp->device->id);
7772 asc_scsi_q->q1.target_lun = scp->device->lun;
7773 asc_scsi_q->q2.target_ix =
Matthew Wilcox51219352007-10-02 21:55:22 -04007774 ASC_TIDLUN_TO_IX(scp->device->id, scp->device->lun);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007775 asc_scsi_q->q1.sense_addr = advansys_get_sense_buffer_dma(scp);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007776 asc_scsi_q->q1.sense_len = SCSI_SENSE_BUFFERSIZE;
Matthew Wilcox51219352007-10-02 21:55:22 -04007777
7778 /*
7779 * If there are any outstanding requests for the current target,
7780 * then every 255th request send an ORDERED request. This heuristic
7781 * tries to retain the benefit of request sorting while preventing
7782 * request starvation. 255 is the max number of tags or pending commands
7783 * a device may have outstanding.
7784 *
7785 * The request count is incremented below for every successfully
7786 * started request.
7787 *
7788 */
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007789 if ((asc_dvc->cur_dvc_qng[scp->device->id] > 0) &&
Matthew Wilcox51219352007-10-02 21:55:22 -04007790 (boardp->reqcnt[scp->device->id] % 255) == 0) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007791 asc_scsi_q->q2.tag_code = ORDERED_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04007792 } else {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007793 asc_scsi_q->q2.tag_code = SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04007794 }
7795
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007796 /* Build ASC_SCSI_Q */
7797 use_sg = scsi_dma_map(scp);
7798 if (use_sg != 0) {
Matthew Wilcox51219352007-10-02 21:55:22 -04007799 int sgcnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04007800 struct scatterlist *slp;
Matthew Wilcox05848b62007-10-02 21:55:25 -04007801 struct asc_sg_head *asc_sg_head;
Matthew Wilcox51219352007-10-02 21:55:22 -04007802
Matthew Wilcox51219352007-10-02 21:55:22 -04007803 if (use_sg > scp->device->host->sg_tablesize) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04007804 scmd_printk(KERN_ERR, scp, "use_sg %d > "
7805 "sg_tablesize %d\n", use_sg,
7806 scp->device->host->sg_tablesize);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007807 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007808 scp->result = HOST_BYTE(DID_ERROR);
7809 return ASC_ERROR;
7810 }
7811
Matthew Wilcox05848b62007-10-02 21:55:25 -04007812 asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) +
7813 use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC);
7814 if (!asc_sg_head) {
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007815 scsi_dma_unmap(scp);
Matthew Wilcox05848b62007-10-02 21:55:25 -04007816 scp->result = HOST_BYTE(DID_SOFT_ERROR);
7817 return ASC_ERROR;
7818 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007819
Matthew Wilcox05848b62007-10-02 21:55:25 -04007820 asc_scsi_q->q1.cntl |= QC_SG_HEAD;
7821 asc_scsi_q->sg_head = asc_sg_head;
7822 asc_scsi_q->q1.data_cnt = 0;
7823 asc_scsi_q->q1.data_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007824 /* This is a byte value, otherwise it would need to be swapped. */
Matthew Wilcox05848b62007-10-02 21:55:25 -04007825 asc_sg_head->entry_cnt = asc_scsi_q->q1.sg_queue_cnt = use_sg;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007826 ASC_STATS_ADD(scp->device->host, xfer_elem,
Matthew Wilcox05848b62007-10-02 21:55:25 -04007827 asc_sg_head->entry_cnt);
Matthew Wilcox51219352007-10-02 21:55:22 -04007828
7829 /*
7830 * Convert scatter-gather list into ASC_SG_HEAD list.
7831 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007832 scsi_for_each_sg(scp, slp, use_sg, sgcnt) {
Matthew Wilcox05848b62007-10-02 21:55:25 -04007833 asc_sg_head->sg_list[sgcnt].addr =
Matthew Wilcox51219352007-10-02 21:55:22 -04007834 cpu_to_le32(sg_dma_address(slp));
Matthew Wilcox05848b62007-10-02 21:55:25 -04007835 asc_sg_head->sg_list[sgcnt].bytes =
Matthew Wilcox51219352007-10-02 21:55:22 -04007836 cpu_to_le32(sg_dma_len(slp));
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007837 ASC_STATS_ADD(scp->device->host, xfer_sect,
7838 DIV_ROUND_UP(sg_dma_len(slp), 512));
Matthew Wilcox51219352007-10-02 21:55:22 -04007839 }
7840 }
7841
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007842 ASC_STATS(scp->device->host, xfer_cnt);
7843
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007844 ASC_DBG_PRT_ASC_SCSI_Q(2, asc_scsi_q);
Matthew Wilcox51219352007-10-02 21:55:22 -04007845 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7846
7847 return ASC_NOERROR;
7848}
7849
7850/*
7851 * Build scatter-gather list for Adv Library (Wide Board).
7852 *
7853 * Additional ADV_SG_BLOCK structures will need to be allocated
7854 * if the total number of scatter-gather elements exceeds
7855 * NO_OF_SG_PER_BLOCK (15). The ADV_SG_BLOCK structures are
7856 * assumed to be physically contiguous.
7857 *
7858 * Return:
7859 * ADV_SUCCESS(1) - SG List successfully created
7860 * ADV_ERROR(-1) - SG List creation failed
7861 */
7862static int
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007863adv_get_sglist(struct asc_board *boardp, adv_req_t *reqp,
7864 ADV_SCSI_REQ_Q *scsiqp, struct scsi_cmnd *scp, int use_sg)
Matthew Wilcox51219352007-10-02 21:55:22 -04007865{
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007866 adv_sgblk_t *sgblkp, *prev_sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007867 struct scatterlist *slp;
7868 int sg_elem_cnt;
7869 ADV_SG_BLOCK *sg_block, *prev_sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007870 dma_addr_t sgblk_paddr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007871 int i;
7872
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007873 slp = scsi_sglist(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007874 sg_elem_cnt = use_sg;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007875 prev_sgblkp = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007876 prev_sg_block = NULL;
7877 reqp->sgblkp = NULL;
7878
7879 for (;;) {
7880 /*
7881 * Allocate a 'adv_sgblk_t' structure from the board free
7882 * list. One 'adv_sgblk_t' structure holds NO_OF_SG_PER_BLOCK
7883 * (15) scatter-gather elements.
7884 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007885 sgblkp = dma_pool_alloc(boardp->adv_sgblk_pool, GFP_ATOMIC,
7886 &sgblk_paddr);
7887 if (!sgblkp) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007888 ASC_DBG(1, "no free adv_sgblk_t\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007889 ASC_STATS(scp->device->host, adv_build_nosg);
7890
7891 /*
7892 * Allocation failed. Free 'adv_sgblk_t' structures
7893 * already allocated for the request.
7894 */
7895 while ((sgblkp = reqp->sgblkp) != NULL) {
7896 /* Remove 'sgblkp' from the request list. */
7897 reqp->sgblkp = sgblkp->next_sgblkp;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007898 sgblkp->next_sgblkp = NULL;
7899 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
7900 sgblkp->sg_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007901 }
7902 return ASC_BUSY;
7903 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007904 /* Complete 'adv_sgblk_t' board allocation. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007905 sgblkp->sg_addr = sgblk_paddr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007906 sgblkp->next_sgblkp = NULL;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007907 sg_block = &sgblkp->sg_block;
Matthew Wilcox51219352007-10-02 21:55:22 -04007908
7909 /*
7910 * Check if this is the first 'adv_sgblk_t' for the
7911 * request.
7912 */
7913 if (reqp->sgblkp == NULL) {
7914 /* Request's first scatter-gather block. */
7915 reqp->sgblkp = sgblkp;
7916
7917 /*
7918 * Set ADV_SCSI_REQ_T ADV_SG_BLOCK virtual and physical
7919 * address pointers.
7920 */
7921 scsiqp->sg_list_ptr = sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007922 scsiqp->sg_real_addr = cpu_to_le32(sgblk_paddr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007923 } else {
7924 /* Request's second or later scatter-gather block. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007925 prev_sgblkp->next_sgblkp = sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007926
7927 /*
7928 * Point the previous ADV_SG_BLOCK structure to
7929 * the newly allocated ADV_SG_BLOCK structure.
7930 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007931 prev_sg_block->sg_ptr = cpu_to_le32(sgblk_paddr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007932 }
7933
7934 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
7935 sg_block->sg_list[i].sg_addr =
7936 cpu_to_le32(sg_dma_address(slp));
7937 sg_block->sg_list[i].sg_count =
7938 cpu_to_le32(sg_dma_len(slp));
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007939 ASC_STATS_ADD(scp->device->host, xfer_sect,
7940 DIV_ROUND_UP(sg_dma_len(slp), 512));
Matthew Wilcox51219352007-10-02 21:55:22 -04007941
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007942 if (--sg_elem_cnt == 0) {
7943 /*
7944 * Last ADV_SG_BLOCK and scatter-gather entry.
7945 */
Matthew Wilcox51219352007-10-02 21:55:22 -04007946 sg_block->sg_cnt = i + 1;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007947 sg_block->sg_ptr = 0L; /* Last ADV_SG_BLOCK in list. */
Matthew Wilcox51219352007-10-02 21:55:22 -04007948 return ADV_SUCCESS;
7949 }
7950 slp++;
7951 }
7952 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
7953 prev_sg_block = sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007954 prev_sgblkp = sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007955 }
7956}
7957
7958/*
7959 * Build a request structure for the Adv Library (Wide Board).
7960 *
7961 * If an adv_req_t can not be allocated to issue the request,
7962 * then return ASC_BUSY. If an error occurs, then return ASC_ERROR.
7963 *
7964 * Multi-byte fields in the ASC_SCSI_REQ_Q that are used by the
7965 * microcode for DMA addresses or math operations are byte swapped
7966 * to little-endian order.
7967 */
7968static int
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007969adv_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007970 adv_req_t **adv_reqpp)
Matthew Wilcox51219352007-10-02 21:55:22 -04007971{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007972 u32 srb_tag = scp->request->tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007973 adv_req_t *reqp;
7974 ADV_SCSI_REQ_Q *scsiqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007975 int ret;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007976 int use_sg;
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007977 dma_addr_t sense_addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007978
7979 /*
7980 * Allocate an adv_req_t structure from the board to execute
7981 * the command.
7982 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007983 reqp = &boardp->adv_reqp[srb_tag];
7984 if (reqp->cmndp && reqp->cmndp != scp ) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007985 ASC_DBG(1, "no free adv_req_t\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007986 ASC_STATS(scp->device->host, adv_build_noreq);
7987 return ASC_BUSY;
Matthew Wilcox51219352007-10-02 21:55:22 -04007988 }
7989
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007990 reqp->req_addr = boardp->adv_reqp_addr + (srb_tag * sizeof(adv_req_t));
7991
7992 scsiqp = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04007993
7994 /*
7995 * Initialize the structure.
7996 */
7997 scsiqp->cntl = scsiqp->scsi_cntl = scsiqp->done_status = 0;
7998
7999 /*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02008000 * Set the srb_tag to the command tag.
Matthew Wilcox51219352007-10-02 21:55:22 -04008001 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02008002 scsiqp->srb_tag = srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04008003
8004 /*
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008005 * Set 'host_scribble' to point to the adv_req_t structure.
Matthew Wilcox51219352007-10-02 21:55:22 -04008006 */
8007 reqp->cmndp = scp;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02008008 scp->host_scribble = (void *)reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04008009
8010 /*
8011 * Build the ADV_SCSI_REQ_Q request.
8012 */
8013
8014 /* Set CDB length and copy it to the request structure. */
8015 scsiqp->cdb_len = scp->cmd_len;
8016 /* Copy first 12 CDB bytes to cdb[]. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02008017 memcpy(scsiqp->cdb, scp->cmnd, scp->cmd_len < 12 ? scp->cmd_len : 12);
Matthew Wilcox51219352007-10-02 21:55:22 -04008018 /* Copy last 4 CDB bytes, if present, to cdb16[]. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02008019 if (scp->cmd_len > 12) {
8020 int cdb16_len = scp->cmd_len - 12;
8021
8022 memcpy(scsiqp->cdb16, &scp->cmnd[12], cdb16_len);
Matthew Wilcox51219352007-10-02 21:55:22 -04008023 }
8024
8025 scsiqp->target_id = scp->device->id;
8026 scsiqp->target_lun = scp->device->lun;
8027
Hannes Reinecke811ddc02015-04-24 13:18:22 +02008028 sense_addr = dma_map_single(boardp->dev, scp->sense_buffer,
8029 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
8030 scsiqp->sense_addr = cpu_to_le32(sense_addr);
8031 scsiqp->sense_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04008032
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008033 /* Build ADV_SCSI_REQ_Q */
Matthew Wilcox51219352007-10-02 21:55:22 -04008034
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008035 use_sg = scsi_dma_map(scp);
8036 if (use_sg == 0) {
8037 /* Zero-length transfer */
Matthew Wilcox51219352007-10-02 21:55:22 -04008038 reqp->sgblkp = NULL;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008039 scsiqp->data_cnt = 0;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008040
8041 scsiqp->data_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04008042 scsiqp->sg_list_ptr = NULL;
8043 scsiqp->sg_real_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04008044 } else {
Matthew Wilcox51219352007-10-02 21:55:22 -04008045 if (use_sg > ADV_MAX_SG_LIST) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008046 scmd_printk(KERN_ERR, scp, "use_sg %d > "
8047 "ADV_MAX_SG_LIST %d\n", use_sg,
Matthew Wilcox51219352007-10-02 21:55:22 -04008048 scp->device->host->sg_tablesize);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008049 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008050 scp->result = HOST_BYTE(DID_ERROR);
Hannes Reinecke9c17c622015-04-24 13:18:21 +02008051 reqp->cmndp = NULL;
8052 scp->host_scribble = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04008053
8054 return ASC_ERROR;
8055 }
8056
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008057 scsiqp->data_cnt = cpu_to_le32(scsi_bufflen(scp));
8058
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008059 ret = adv_get_sglist(boardp, reqp, scsiqp, scp, use_sg);
Matthew Wilcox51219352007-10-02 21:55:22 -04008060 if (ret != ADV_SUCCESS) {
Hannes Reinecke9c17c622015-04-24 13:18:21 +02008061 scsi_dma_unmap(scp);
8062 scp->result = HOST_BYTE(DID_ERROR);
8063 reqp->cmndp = NULL;
8064 scp->host_scribble = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04008065
8066 return ret;
8067 }
8068
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008069 ASC_STATS_ADD(scp->device->host, xfer_elem, use_sg);
Matthew Wilcox51219352007-10-02 21:55:22 -04008070 }
8071
Matthew Wilcox52c334e2007-10-02 21:55:39 -04008072 ASC_STATS(scp->device->host, xfer_cnt);
8073
Matthew Wilcox51219352007-10-02 21:55:22 -04008074 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
8075 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
8076
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008077 *adv_reqpp = reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04008078
8079 return ASC_NOERROR;
8080}
8081
8082static int AscSgListToQueue(int sg_list)
8083{
8084 int n_sg_list_qs;
8085
8086 n_sg_list_qs = ((sg_list - 1) / ASC_SG_LIST_PER_Q);
8087 if (((sg_list - 1) % ASC_SG_LIST_PER_Q) != 0)
8088 n_sg_list_qs++;
8089 return n_sg_list_qs + 1;
8090}
8091
8092static uint
8093AscGetNumOfFreeQueue(ASC_DVC_VAR *asc_dvc, uchar target_ix, uchar n_qs)
8094{
8095 uint cur_used_qs;
8096 uint cur_free_qs;
8097 ASC_SCSI_BIT_ID_TYPE target_id;
8098 uchar tid_no;
8099
8100 target_id = ASC_TIX_TO_TARGET_ID(target_ix);
8101 tid_no = ASC_TIX_TO_TID(target_ix);
8102 if ((asc_dvc->unit_not_ready & target_id) ||
8103 (asc_dvc->queue_full_or_busy & target_id)) {
8104 return 0;
8105 }
8106 if (n_qs == 1) {
8107 cur_used_qs = (uint) asc_dvc->cur_total_qng +
8108 (uint) asc_dvc->last_q_shortage + (uint) ASC_MIN_FREE_Q;
8109 } else {
8110 cur_used_qs = (uint) asc_dvc->cur_total_qng +
8111 (uint) ASC_MIN_FREE_Q;
8112 }
8113 if ((uint) (cur_used_qs + n_qs) <= (uint) asc_dvc->max_total_qng) {
8114 cur_free_qs = (uint) asc_dvc->max_total_qng - cur_used_qs;
8115 if (asc_dvc->cur_dvc_qng[tid_no] >=
8116 asc_dvc->max_dvc_qng[tid_no]) {
8117 return 0;
8118 }
8119 return cur_free_qs;
8120 }
8121 if (n_qs > 1) {
8122 if ((n_qs > asc_dvc->last_q_shortage)
8123 && (n_qs <= (asc_dvc->max_total_qng - ASC_MIN_FREE_Q))) {
8124 asc_dvc->last_q_shortage = n_qs;
8125 }
8126 }
8127 return 0;
8128}
8129
8130static uchar AscAllocFreeQueue(PortAddr iop_base, uchar free_q_head)
8131{
8132 ushort q_addr;
8133 uchar next_qp;
8134 uchar q_status;
8135
8136 q_addr = ASC_QNO_TO_QADDR(free_q_head);
8137 q_status = (uchar)AscReadLramByte(iop_base,
8138 (ushort)(q_addr +
8139 ASC_SCSIQ_B_STATUS));
8140 next_qp = AscReadLramByte(iop_base, (ushort)(q_addr + ASC_SCSIQ_B_FWD));
8141 if (((q_status & QS_READY) == 0) && (next_qp != ASC_QLINK_END))
8142 return next_qp;
8143 return ASC_QLINK_END;
8144}
8145
8146static uchar
8147AscAllocMultipleFreeQueue(PortAddr iop_base, uchar free_q_head, uchar n_free_q)
8148{
8149 uchar i;
8150
8151 for (i = 0; i < n_free_q; i++) {
8152 free_q_head = AscAllocFreeQueue(iop_base, free_q_head);
8153 if (free_q_head == ASC_QLINK_END)
8154 break;
8155 }
8156 return free_q_head;
8157}
8158
8159/*
8160 * void
8161 * DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
8162 *
8163 * Calling/Exit State:
8164 * none
8165 *
8166 * Description:
8167 * Output an ASC_SCSI_Q structure to the chip
8168 */
8169static void
8170DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
8171{
8172 int i;
8173
8174 ASC_DBG_PRT_HEX(2, "DvcPutScsiQ", outbuf, 2 * words);
8175 AscSetChipLramAddr(iop_base, s_addr);
8176 for (i = 0; i < 2 * words; i += 2) {
8177 if (i == 4 || i == 20) {
8178 continue;
8179 }
8180 outpw(iop_base + IOP_RAM_DATA,
8181 ((ushort)outbuf[i + 1] << 8) | outbuf[i]);
8182 }
8183}
8184
8185static int AscPutReadyQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8186{
8187 ushort q_addr;
8188 uchar tid_no;
8189 uchar sdtr_data;
8190 uchar syn_period_ix;
8191 uchar syn_offset;
8192 PortAddr iop_base;
8193
8194 iop_base = asc_dvc->iop_base;
8195 if (((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) &&
8196 ((asc_dvc->sdtr_done & scsiq->q1.target_id) == 0)) {
8197 tid_no = ASC_TIX_TO_TID(scsiq->q2.target_ix);
8198 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8199 syn_period_ix =
8200 (sdtr_data >> 4) & (asc_dvc->max_sdtr_index - 1);
8201 syn_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
8202 AscMsgOutSDTR(asc_dvc,
8203 asc_dvc->sdtr_period_tbl[syn_period_ix],
8204 syn_offset);
8205 scsiq->q1.cntl |= QC_MSG_OUT;
8206 }
8207 q_addr = ASC_QNO_TO_QADDR(q_no);
8208 if ((scsiq->q1.target_id & asc_dvc->use_tagged_qng) == 0) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08008209 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04008210 }
8211 scsiq->q1.status = QS_FREE;
8212 AscMemWordCopyPtrToLram(iop_base,
8213 q_addr + ASC_SCSIQ_CDB_BEG,
8214 (uchar *)scsiq->cdbptr, scsiq->q2.cdb_len >> 1);
8215
8216 DvcPutScsiQ(iop_base,
8217 q_addr + ASC_SCSIQ_CPY_BEG,
8218 (uchar *)&scsiq->q1.cntl,
8219 ((sizeof(ASC_SCSIQ_1) + sizeof(ASC_SCSIQ_2)) / 2) - 1);
8220 AscWriteLramWord(iop_base,
8221 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS),
8222 (ushort)(((ushort)scsiq->q1.
8223 q_no << 8) | (ushort)QS_READY));
8224 return 1;
8225}
8226
8227static int
8228AscPutReadySgListQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8229{
8230 int sta;
8231 int i;
8232 ASC_SG_HEAD *sg_head;
8233 ASC_SG_LIST_Q scsi_sg_q;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008234 __le32 saved_data_addr;
8235 __le32 saved_data_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008236 PortAddr iop_base;
8237 ushort sg_list_dwords;
8238 ushort sg_index;
8239 ushort sg_entry_cnt;
8240 ushort q_addr;
8241 uchar next_qp;
8242
8243 iop_base = asc_dvc->iop_base;
8244 sg_head = scsiq->sg_head;
8245 saved_data_addr = scsiq->q1.data_addr;
8246 saved_data_cnt = scsiq->q1.data_cnt;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008247 scsiq->q1.data_addr = sg_head->sg_list[0].addr;
8248 scsiq->q1.data_cnt = sg_head->sg_list[0].bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008249#if CC_VERY_LONG_SG_LIST
8250 /*
8251 * If sg_head->entry_cnt is greater than ASC_MAX_SG_LIST
8252 * then not all SG elements will fit in the allocated queues.
8253 * The rest of the SG elements will be copied when the RISC
8254 * completes the SG elements that fit and halts.
8255 */
8256 if (sg_head->entry_cnt > ASC_MAX_SG_LIST) {
8257 /*
8258 * Set sg_entry_cnt to be the number of SG elements that
8259 * will fit in the allocated SG queues. It is minus 1, because
8260 * the first SG element is handled above. ASC_MAX_SG_LIST is
8261 * already inflated by 1 to account for this. For example it
8262 * may be 50 which is 1 + 7 queues * 7 SG elements.
8263 */
8264 sg_entry_cnt = ASC_MAX_SG_LIST - 1;
8265
8266 /*
8267 * Keep track of remaining number of SG elements that will
8268 * need to be handled from a_isr.c.
8269 */
8270 scsiq->remain_sg_entry_cnt =
8271 sg_head->entry_cnt - ASC_MAX_SG_LIST;
8272 } else {
8273#endif /* CC_VERY_LONG_SG_LIST */
8274 /*
8275 * Set sg_entry_cnt to be the number of SG elements that
8276 * will fit in the allocated SG queues. It is minus 1, because
8277 * the first SG element is handled above.
8278 */
8279 sg_entry_cnt = sg_head->entry_cnt - 1;
8280#if CC_VERY_LONG_SG_LIST
8281 }
8282#endif /* CC_VERY_LONG_SG_LIST */
8283 if (sg_entry_cnt != 0) {
8284 scsiq->q1.cntl |= QC_SG_HEAD;
8285 q_addr = ASC_QNO_TO_QADDR(q_no);
8286 sg_index = 1;
8287 scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
8288 scsi_sg_q.sg_head_qp = q_no;
8289 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
8290 for (i = 0; i < sg_head->queue_cnt; i++) {
8291 scsi_sg_q.seq_no = i + 1;
8292 if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
8293 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
8294 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
8295 if (i == 0) {
8296 scsi_sg_q.sg_list_cnt =
8297 ASC_SG_LIST_PER_Q;
8298 scsi_sg_q.sg_cur_list_cnt =
8299 ASC_SG_LIST_PER_Q;
8300 } else {
8301 scsi_sg_q.sg_list_cnt =
8302 ASC_SG_LIST_PER_Q - 1;
8303 scsi_sg_q.sg_cur_list_cnt =
8304 ASC_SG_LIST_PER_Q - 1;
8305 }
8306 } else {
8307#if CC_VERY_LONG_SG_LIST
8308 /*
8309 * This is the last SG queue in the list of
8310 * allocated SG queues. If there are more
8311 * SG elements than will fit in the allocated
8312 * queues, then set the QCSG_SG_XFER_MORE flag.
8313 */
8314 if (sg_head->entry_cnt > ASC_MAX_SG_LIST) {
8315 scsi_sg_q.cntl |= QCSG_SG_XFER_MORE;
8316 } else {
8317#endif /* CC_VERY_LONG_SG_LIST */
8318 scsi_sg_q.cntl |= QCSG_SG_XFER_END;
8319#if CC_VERY_LONG_SG_LIST
8320 }
8321#endif /* CC_VERY_LONG_SG_LIST */
8322 sg_list_dwords = sg_entry_cnt << 1;
8323 if (i == 0) {
8324 scsi_sg_q.sg_list_cnt = sg_entry_cnt;
8325 scsi_sg_q.sg_cur_list_cnt =
8326 sg_entry_cnt;
8327 } else {
8328 scsi_sg_q.sg_list_cnt =
8329 sg_entry_cnt - 1;
8330 scsi_sg_q.sg_cur_list_cnt =
8331 sg_entry_cnt - 1;
8332 }
8333 sg_entry_cnt = 0;
8334 }
8335 next_qp = AscReadLramByte(iop_base,
8336 (ushort)(q_addr +
8337 ASC_SCSIQ_B_FWD));
8338 scsi_sg_q.q_no = next_qp;
8339 q_addr = ASC_QNO_TO_QADDR(next_qp);
8340 AscMemWordCopyPtrToLram(iop_base,
8341 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
8342 (uchar *)&scsi_sg_q,
8343 sizeof(ASC_SG_LIST_Q) >> 1);
8344 AscMemDWordCopyPtrToLram(iop_base,
8345 q_addr + ASC_SGQ_LIST_BEG,
8346 (uchar *)&sg_head->
8347 sg_list[sg_index],
8348 sg_list_dwords);
8349 sg_index += ASC_SG_LIST_PER_Q;
8350 scsiq->next_sg_index = sg_index;
8351 }
8352 } else {
8353 scsiq->q1.cntl &= ~QC_SG_HEAD;
8354 }
8355 sta = AscPutReadyQueue(asc_dvc, scsiq, q_no);
8356 scsiq->q1.data_addr = saved_data_addr;
8357 scsiq->q1.data_cnt = saved_data_cnt;
8358 return (sta);
8359}
8360
8361static int
8362AscSendScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar n_q_required)
8363{
8364 PortAddr iop_base;
8365 uchar free_q_head;
8366 uchar next_qp;
8367 uchar tid_no;
8368 uchar target_ix;
8369 int sta;
8370
8371 iop_base = asc_dvc->iop_base;
8372 target_ix = scsiq->q2.target_ix;
8373 tid_no = ASC_TIX_TO_TID(target_ix);
8374 sta = 0;
8375 free_q_head = (uchar)AscGetVarFreeQHead(iop_base);
8376 if (n_q_required > 1) {
8377 next_qp = AscAllocMultipleFreeQueue(iop_base, free_q_head,
8378 (uchar)n_q_required);
8379 if (next_qp != ASC_QLINK_END) {
8380 asc_dvc->last_q_shortage = 0;
8381 scsiq->sg_head->queue_cnt = n_q_required - 1;
8382 scsiq->q1.q_no = free_q_head;
8383 sta = AscPutReadySgListQueue(asc_dvc, scsiq,
8384 free_q_head);
8385 }
8386 } else if (n_q_required == 1) {
8387 next_qp = AscAllocFreeQueue(iop_base, free_q_head);
8388 if (next_qp != ASC_QLINK_END) {
8389 scsiq->q1.q_no = free_q_head;
8390 sta = AscPutReadyQueue(asc_dvc, scsiq, free_q_head);
8391 }
8392 }
8393 if (sta == 1) {
8394 AscPutVarFreeQHead(iop_base, next_qp);
8395 asc_dvc->cur_total_qng += n_q_required;
8396 asc_dvc->cur_dvc_qng[tid_no]++;
8397 }
8398 return sta;
8399}
8400
8401#define ASC_SYN_OFFSET_ONE_DISABLE_LIST 16
8402static uchar _syn_offset_one_disable_cmd[ASC_SYN_OFFSET_ONE_DISABLE_LIST] = {
8403 INQUIRY,
8404 REQUEST_SENSE,
8405 READ_CAPACITY,
8406 READ_TOC,
8407 MODE_SELECT,
8408 MODE_SENSE,
8409 MODE_SELECT_10,
8410 MODE_SENSE_10,
8411 0xFF,
8412 0xFF,
8413 0xFF,
8414 0xFF,
8415 0xFF,
8416 0xFF,
8417 0xFF,
8418 0xFF
8419};
8420
8421static int AscExeScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq)
8422{
8423 PortAddr iop_base;
8424 int sta;
8425 int n_q_required;
8426 int disable_syn_offset_one_fix;
8427 int i;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008428 u32 addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04008429 ushort sg_entry_cnt = 0;
8430 ushort sg_entry_cnt_minus_one = 0;
8431 uchar target_ix;
8432 uchar tid_no;
8433 uchar sdtr_data;
8434 uchar extra_bytes;
8435 uchar scsi_cmd;
8436 uchar disable_cmd;
8437 ASC_SG_HEAD *sg_head;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008438 unsigned long data_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008439
8440 iop_base = asc_dvc->iop_base;
8441 sg_head = scsiq->sg_head;
8442 if (asc_dvc->err_code != 0)
8443 return (ERR);
8444 scsiq->q1.q_no = 0;
8445 if ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0) {
8446 scsiq->q1.extra_bytes = 0;
8447 }
8448 sta = 0;
8449 target_ix = scsiq->q2.target_ix;
8450 tid_no = ASC_TIX_TO_TID(target_ix);
8451 n_q_required = 1;
8452 if (scsiq->cdbptr[0] == REQUEST_SENSE) {
8453 if ((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) {
8454 asc_dvc->sdtr_done &= ~scsiq->q1.target_id;
8455 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8456 AscMsgOutSDTR(asc_dvc,
8457 asc_dvc->
8458 sdtr_period_tbl[(sdtr_data >> 4) &
8459 (uchar)(asc_dvc->
8460 max_sdtr_index -
8461 1)],
8462 (uchar)(sdtr_data & (uchar)
8463 ASC_SYN_MAX_OFFSET));
8464 scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);
8465 }
8466 }
8467 if (asc_dvc->in_critical_cnt != 0) {
8468 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CRITICAL_RE_ENTRY);
8469 return (ERR);
8470 }
8471 asc_dvc->in_critical_cnt++;
8472 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8473 if ((sg_entry_cnt = sg_head->entry_cnt) == 0) {
8474 asc_dvc->in_critical_cnt--;
8475 return (ERR);
8476 }
8477#if !CC_VERY_LONG_SG_LIST
8478 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8479 asc_dvc->in_critical_cnt--;
8480 return (ERR);
8481 }
8482#endif /* !CC_VERY_LONG_SG_LIST */
8483 if (sg_entry_cnt == 1) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008484 scsiq->q1.data_addr = sg_head->sg_list[0].addr;
8485 scsiq->q1.data_cnt = sg_head->sg_list[0].bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008486 scsiq->q1.cntl &= ~(QC_SG_HEAD | QC_SG_SWAP_QUEUE);
8487 }
8488 sg_entry_cnt_minus_one = sg_entry_cnt - 1;
8489 }
8490 scsi_cmd = scsiq->cdbptr[0];
8491 disable_syn_offset_one_fix = FALSE;
8492 if ((asc_dvc->pci_fix_asyn_xfer & scsiq->q1.target_id) &&
8493 !(asc_dvc->pci_fix_asyn_xfer_always & scsiq->q1.target_id)) {
8494 if (scsiq->q1.cntl & QC_SG_HEAD) {
8495 data_cnt = 0;
8496 for (i = 0; i < sg_entry_cnt; i++) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008497 data_cnt += le32_to_cpu(sg_head->sg_list[i].
8498 bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008499 }
8500 } else {
8501 data_cnt = le32_to_cpu(scsiq->q1.data_cnt);
8502 }
8503 if (data_cnt != 0UL) {
8504 if (data_cnt < 512UL) {
8505 disable_syn_offset_one_fix = TRUE;
8506 } else {
8507 for (i = 0; i < ASC_SYN_OFFSET_ONE_DISABLE_LIST;
8508 i++) {
8509 disable_cmd =
8510 _syn_offset_one_disable_cmd[i];
8511 if (disable_cmd == 0xFF) {
8512 break;
8513 }
8514 if (scsi_cmd == disable_cmd) {
8515 disable_syn_offset_one_fix =
8516 TRUE;
8517 break;
8518 }
8519 }
8520 }
8521 }
8522 }
8523 if (disable_syn_offset_one_fix) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08008524 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04008525 scsiq->q2.tag_code |= (ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX |
8526 ASC_TAG_FLAG_DISABLE_DISCONNECT);
8527 } else {
8528 scsiq->q2.tag_code &= 0x27;
8529 }
8530 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8531 if (asc_dvc->bug_fix_cntl) {
8532 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8533 if ((scsi_cmd == READ_6) ||
8534 (scsi_cmd == READ_10)) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008535 addr = le32_to_cpu(sg_head->
Matthew Wilcox51219352007-10-02 21:55:22 -04008536 sg_list
8537 [sg_entry_cnt_minus_one].
8538 addr) +
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008539 le32_to_cpu(sg_head->
Matthew Wilcox51219352007-10-02 21:55:22 -04008540 sg_list
8541 [sg_entry_cnt_minus_one].
8542 bytes);
8543 extra_bytes =
8544 (uchar)((ushort)addr & 0x0003);
8545 if ((extra_bytes != 0)
8546 &&
8547 ((scsiq->q2.
8548 tag_code &
8549 ASC_TAG_FLAG_EXTRA_BYTES)
8550 == 0)) {
8551 scsiq->q2.tag_code |=
8552 ASC_TAG_FLAG_EXTRA_BYTES;
8553 scsiq->q1.extra_bytes =
8554 extra_bytes;
8555 data_cnt =
8556 le32_to_cpu(sg_head->
8557 sg_list
8558 [sg_entry_cnt_minus_one].
8559 bytes);
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008560 data_cnt -= extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008561 sg_head->
8562 sg_list
8563 [sg_entry_cnt_minus_one].
8564 bytes =
8565 cpu_to_le32(data_cnt);
8566 }
8567 }
8568 }
8569 }
8570 sg_head->entry_to_copy = sg_head->entry_cnt;
8571#if CC_VERY_LONG_SG_LIST
8572 /*
8573 * Set the sg_entry_cnt to the maximum possible. The rest of
8574 * the SG elements will be copied when the RISC completes the
8575 * SG elements that fit and halts.
8576 */
8577 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8578 sg_entry_cnt = ASC_MAX_SG_LIST;
8579 }
8580#endif /* CC_VERY_LONG_SG_LIST */
8581 n_q_required = AscSgListToQueue(sg_entry_cnt);
8582 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, n_q_required) >=
8583 (uint) n_q_required)
8584 || ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8585 if ((sta =
8586 AscSendScsiQueue(asc_dvc, scsiq,
8587 n_q_required)) == 1) {
8588 asc_dvc->in_critical_cnt--;
8589 return (sta);
8590 }
8591 }
8592 } else {
8593 if (asc_dvc->bug_fix_cntl) {
8594 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8595 if ((scsi_cmd == READ_6) ||
8596 (scsi_cmd == READ_10)) {
8597 addr =
8598 le32_to_cpu(scsiq->q1.data_addr) +
8599 le32_to_cpu(scsiq->q1.data_cnt);
8600 extra_bytes =
8601 (uchar)((ushort)addr & 0x0003);
8602 if ((extra_bytes != 0)
8603 &&
8604 ((scsiq->q2.
8605 tag_code &
8606 ASC_TAG_FLAG_EXTRA_BYTES)
8607 == 0)) {
8608 data_cnt =
8609 le32_to_cpu(scsiq->q1.
8610 data_cnt);
8611 if (((ushort)data_cnt & 0x01FF)
8612 == 0) {
8613 scsiq->q2.tag_code |=
8614 ASC_TAG_FLAG_EXTRA_BYTES;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008615 data_cnt -= extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008616 scsiq->q1.data_cnt =
8617 cpu_to_le32
8618 (data_cnt);
8619 scsiq->q1.extra_bytes =
8620 extra_bytes;
8621 }
8622 }
8623 }
8624 }
8625 }
8626 n_q_required = 1;
8627 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, 1) >= 1) ||
8628 ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8629 if ((sta = AscSendScsiQueue(asc_dvc, scsiq,
8630 n_q_required)) == 1) {
8631 asc_dvc->in_critical_cnt--;
8632 return (sta);
8633 }
8634 }
8635 }
8636 asc_dvc->in_critical_cnt--;
8637 return (sta);
8638}
8639
8640/*
8641 * AdvExeScsiQueue() - Send a request to the RISC microcode program.
8642 *
8643 * Allocate a carrier structure, point the carrier to the ADV_SCSI_REQ_Q,
8644 * add the carrier to the ICQ (Initiator Command Queue), and tickle the
8645 * RISC to notify it a new command is ready to be executed.
8646 *
8647 * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
8648 * set to SCSI_MAX_RETRY.
8649 *
8650 * Multi-byte fields in the ASC_SCSI_REQ_Q that are used by the microcode
8651 * for DMA addresses or math operations are byte swapped to little-endian
8652 * order.
8653 *
8654 * Return:
8655 * ADV_SUCCESS(1) - The request was successfully queued.
8656 * ADV_BUSY(0) - Resource unavailable; Retry again after pending
8657 * request completes.
8658 * ADV_ERROR(-1) - Invalid ADV_SCSI_REQ_Q request structure
8659 * host IC error.
8660 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008661static int AdvExeScsiQueue(ADV_DVC_VAR *asc_dvc, adv_req_t *reqp)
Matthew Wilcox51219352007-10-02 21:55:22 -04008662{
8663 AdvPortAddr iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008664 ADV_CARR_T *new_carrp;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008665 ADV_SCSI_REQ_Q *scsiq = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04008666
8667 /*
8668 * The ADV_SCSI_REQ_Q 'target_id' field should never exceed ADV_MAX_TID.
8669 */
8670 if (scsiq->target_id > ADV_MAX_TID) {
8671 scsiq->host_status = QHSTA_M_INVALID_DEVICE;
8672 scsiq->done_status = QD_WITH_ERROR;
8673 return ADV_ERROR;
8674 }
8675
8676 iop_base = asc_dvc->iop_base;
8677
8678 /*
8679 * Allocate a carrier ensuring at least one carrier always
8680 * remains on the freelist and initialize fields.
8681 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008682 new_carrp = adv_get_next_carrier(asc_dvc);
8683 if (!new_carrp) {
8684 ASC_DBG(1, "No free carriers\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008685 return ADV_BUSY;
8686 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008687
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008688 asc_dvc->carr_pending_cnt++;
Matthew Wilcox51219352007-10-02 21:55:22 -04008689
8690 /*
8691 * Clear the ADV_SCSI_REQ_Q done flag.
8692 */
8693 scsiq->a_flag &= ~ADV_SCSIQ_DONE;
8694
Matthew Wilcox51219352007-10-02 21:55:22 -04008695 /* Save virtual and physical address of ADV_SCSI_REQ_Q and carrier. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008696 scsiq->scsiq_ptr = cpu_to_le32(scsiq->srb_tag);
8697 scsiq->scsiq_rptr = cpu_to_le32(reqp->req_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04008698
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008699 scsiq->carr_va = asc_dvc->icq_sp->carr_va;
Matthew Wilcox51219352007-10-02 21:55:22 -04008700 scsiq->carr_pa = asc_dvc->icq_sp->carr_pa;
8701
8702 /*
8703 * Use the current stopper to send the ADV_SCSI_REQ_Q command to
8704 * the microcode. The newly allocated stopper will become the new
8705 * stopper.
8706 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008707 asc_dvc->icq_sp->areq_vpa = scsiq->scsiq_rptr;
Matthew Wilcox51219352007-10-02 21:55:22 -04008708
8709 /*
8710 * Set the 'next_vpa' pointer for the old stopper to be the
8711 * physical address of the new stopper. The RISC can only
8712 * follow physical addresses.
8713 */
8714 asc_dvc->icq_sp->next_vpa = new_carrp->carr_pa;
8715
8716 /*
8717 * Set the host adapter stopper pointer to point to the new carrier.
8718 */
8719 asc_dvc->icq_sp = new_carrp;
8720
8721 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
8722 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
8723 /*
8724 * Tickle the RISC to tell it to read its Command Queue Head pointer.
8725 */
8726 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_A);
8727 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
8728 /*
8729 * Clear the tickle value. In the ASC-3550 the RISC flag
8730 * command 'clr_tickle_a' does not work unless the host
8731 * value is cleared.
8732 */
8733 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
8734 ADV_TICKLE_NOP);
8735 }
8736 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
8737 /*
8738 * Notify the RISC a carrier is ready by writing the physical
8739 * address of the new carrier stopper to the COMMA register.
8740 */
8741 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
8742 le32_to_cpu(new_carrp->carr_pa));
8743 }
8744
8745 return ADV_SUCCESS;
8746}
8747
8748/*
8749 * Execute a single 'Scsi_Cmnd'.
Matthew Wilcox51219352007-10-02 21:55:22 -04008750 */
8751static int asc_execute_scsi_cmnd(struct scsi_cmnd *scp)
8752{
Matthew Wilcox41d24932007-10-02 21:55:24 -04008753 int ret, err_code;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04008754 struct asc_board *boardp = shost_priv(scp->device->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04008755
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008756 ASC_DBG(1, "scp 0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008757
8758 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox41d24932007-10-02 21:55:24 -04008759 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox05848b62007-10-02 21:55:25 -04008760 struct asc_scsi_q asc_scsi_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04008761
Matthew Wilcox41d24932007-10-02 21:55:24 -04008762 /* asc_build_req() can not return ASC_BUSY. */
Matthew Wilcox05848b62007-10-02 21:55:25 -04008763 ret = asc_build_req(boardp, scp, &asc_scsi_q);
8764 if (ret == ASC_ERROR) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008765 ASC_STATS(scp->device->host, build_error);
8766 return ASC_ERROR;
8767 }
8768
Matthew Wilcox41d24932007-10-02 21:55:24 -04008769 ret = AscExeScsiQueue(asc_dvc, &asc_scsi_q);
Matthew Wilcox05848b62007-10-02 21:55:25 -04008770 kfree(asc_scsi_q.sg_head);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008771 err_code = asc_dvc->err_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04008772 } else {
Matthew Wilcox41d24932007-10-02 21:55:24 -04008773 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008774 adv_req_t *adv_reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04008775
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008776 switch (adv_build_req(boardp, scp, &adv_reqp)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008777 case ASC_NOERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008778 ASC_DBG(3, "adv_build_req ASC_NOERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008779 break;
8780 case ASC_BUSY:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008781 ASC_DBG(1, "adv_build_req ASC_BUSY\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008782 /*
8783 * The asc_stats fields 'adv_build_noreq' and
8784 * 'adv_build_nosg' count wide board busy conditions.
8785 * They are updated in adv_build_req and
8786 * adv_get_sglist, respectively.
8787 */
8788 return ASC_BUSY;
8789 case ASC_ERROR:
8790 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008791 ASC_DBG(1, "adv_build_req ASC_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008792 ASC_STATS(scp->device->host, build_error);
8793 return ASC_ERROR;
8794 }
8795
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008796 ret = AdvExeScsiQueue(adv_dvc, adv_reqp);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008797 err_code = adv_dvc->err_code;
8798 }
8799
8800 switch (ret) {
8801 case ASC_NOERROR:
8802 ASC_STATS(scp->device->host, exe_noerror);
8803 /*
8804 * Increment monotonically increasing per device
8805 * successful request counter. Wrapping doesn't matter.
8806 */
8807 boardp->reqcnt[scp->device->id]++;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008808 ASC_DBG(1, "ExeScsiQueue() ASC_NOERROR\n");
Matthew Wilcox41d24932007-10-02 21:55:24 -04008809 break;
8810 case ASC_BUSY:
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008811 ASC_DBG(1, "ExeScsiQueue() ASC_BUSY\n");
Matthew Wilcox41d24932007-10-02 21:55:24 -04008812 ASC_STATS(scp->device->host, exe_busy);
8813 break;
8814 case ASC_ERROR:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008815 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() ASC_ERROR, "
8816 "err_code 0x%x\n", err_code);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008817 ASC_STATS(scp->device->host, exe_error);
8818 scp->result = HOST_BYTE(DID_ERROR);
8819 break;
8820 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008821 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() unknown, "
8822 "err_code 0x%x\n", err_code);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008823 ASC_STATS(scp->device->host, exe_unknown);
8824 scp->result = HOST_BYTE(DID_ERROR);
8825 break;
Matthew Wilcox51219352007-10-02 21:55:22 -04008826 }
8827
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008828 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008829 return ret;
8830}
8831
8832/*
8833 * advansys_queuecommand() - interrupt-driven I/O entrypoint.
8834 *
8835 * This function always returns 0. Command return status is saved
8836 * in the 'scp' result field.
8837 */
8838static int
Jeff Garzikf2812332010-11-16 02:10:29 -05008839advansys_queuecommand_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
Matthew Wilcox51219352007-10-02 21:55:22 -04008840{
8841 struct Scsi_Host *shost = scp->device->host;
Matthew Wilcox51219352007-10-02 21:55:22 -04008842 int asc_res, result = 0;
8843
8844 ASC_STATS(shost, queuecommand);
8845 scp->scsi_done = done;
8846
Matthew Wilcox51219352007-10-02 21:55:22 -04008847 asc_res = asc_execute_scsi_cmnd(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008848
8849 switch (asc_res) {
8850 case ASC_NOERROR:
8851 break;
8852 case ASC_BUSY:
8853 result = SCSI_MLQUEUE_HOST_BUSY;
8854 break;
8855 case ASC_ERROR:
8856 default:
8857 asc_scsi_done(scp);
8858 break;
8859 }
8860
8861 return result;
8862}
8863
Jeff Garzikf2812332010-11-16 02:10:29 -05008864static DEF_SCSI_QCMD(advansys_queuecommand)
8865
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008866static ushort AscGetEisaChipCfg(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008867{
8868 PortAddr eisa_cfg_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8869 (PortAddr) (ASC_EISA_CFG_IOP_MASK);
8870 return inpw(eisa_cfg_iop);
8871}
8872
8873/*
8874 * Return the BIOS address of the adapter at the specified
8875 * I/O port and with the specified bus type.
8876 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008877static unsigned short AscGetChipBiosAddress(PortAddr iop_base,
8878 unsigned short bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008879{
8880 unsigned short cfg_lsw;
8881 unsigned short bios_addr;
8882
8883 /*
8884 * The PCI BIOS is re-located by the motherboard BIOS. Because
8885 * of this the driver can not determine where a PCI BIOS is
8886 * loaded and executes.
8887 */
8888 if (bus_type & ASC_IS_PCI)
8889 return 0;
8890
8891 if ((bus_type & ASC_IS_EISA) != 0) {
8892 cfg_lsw = AscGetEisaChipCfg(iop_base);
8893 cfg_lsw &= 0x000F;
8894 bios_addr = ASC_BIOS_MIN_ADDR + cfg_lsw * ASC_BIOS_BANK_SIZE;
8895 return bios_addr;
8896 }
8897
8898 cfg_lsw = AscGetChipCfgLsw(iop_base);
8899
8900 /*
8901 * ISA PnP uses the top bit as the 32K BIOS flag
8902 */
8903 if (bus_type == ASC_IS_ISAPNP)
8904 cfg_lsw &= 0x7FFF;
8905 bios_addr = ASC_BIOS_MIN_ADDR + (cfg_lsw >> 12) * ASC_BIOS_BANK_SIZE;
8906 return bios_addr;
8907}
8908
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008909static uchar AscSetChipScsiID(PortAddr iop_base, uchar new_host_id)
Matthew Wilcox51219352007-10-02 21:55:22 -04008910{
8911 ushort cfg_lsw;
8912
8913 if (AscGetChipScsiID(iop_base) == new_host_id) {
8914 return (new_host_id);
8915 }
8916 cfg_lsw = AscGetChipCfgLsw(iop_base);
8917 cfg_lsw &= 0xF8FF;
8918 cfg_lsw |= (ushort)((new_host_id & ASC_MAX_TID) << 8);
8919 AscSetChipCfgLsw(iop_base, cfg_lsw);
8920 return (AscGetChipScsiID(iop_base));
8921}
8922
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008923static unsigned char AscGetChipScsiCtrl(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008924{
8925 unsigned char sc;
8926
8927 AscSetBank(iop_base, 1);
8928 sc = inp(iop_base + IOP_REG_SC);
8929 AscSetBank(iop_base, 0);
8930 return sc;
8931}
8932
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008933static unsigned char AscGetChipVersion(PortAddr iop_base,
8934 unsigned short bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008935{
8936 if (bus_type & ASC_IS_EISA) {
8937 PortAddr eisa_iop;
8938 unsigned char revision;
8939 eisa_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8940 (PortAddr) ASC_EISA_REV_IOP_MASK;
8941 revision = inp(eisa_iop);
8942 return ASC_CHIP_MIN_VER_EISA - 1 + revision;
8943 }
8944 return AscGetChipVerNo(iop_base);
8945}
8946
Matthew Wilcox51219352007-10-02 21:55:22 -04008947#ifdef CONFIG_ISA
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008948static void AscEnableIsaDma(uchar dma_channel)
Matthew Wilcox51219352007-10-02 21:55:22 -04008949{
8950 if (dma_channel < 4) {
8951 outp(0x000B, (ushort)(0xC0 | dma_channel));
8952 outp(0x000A, dma_channel);
8953 } else if (dma_channel < 8) {
8954 outp(0x00D6, (ushort)(0xC0 | (dma_channel - 4)));
8955 outp(0x00D4, (ushort)(dma_channel - 4));
8956 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008957}
8958#endif /* CONFIG_ISA */
8959
8960static int AscStopQueueExe(PortAddr iop_base)
8961{
8962 int count = 0;
8963
8964 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) == 0) {
8965 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
8966 ASC_STOP_REQ_RISC_STOP);
8967 do {
8968 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) &
8969 ASC_STOP_ACK_RISC_STOP) {
8970 return (1);
8971 }
8972 mdelay(100);
8973 } while (count++ < 20);
8974 }
8975 return (0);
8976}
8977
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008978static unsigned int AscGetMaxDmaCount(ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008979{
8980 if (bus_type & ASC_IS_ISA)
8981 return ASC_MAX_ISA_DMA_COUNT;
8982 else if (bus_type & (ASC_IS_EISA | ASC_IS_VL))
8983 return ASC_MAX_VL_DMA_COUNT;
8984 return ASC_MAX_PCI_DMA_COUNT;
8985}
8986
8987#ifdef CONFIG_ISA
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008988static ushort AscGetIsaDmaChannel(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008989{
8990 ushort channel;
8991
8992 channel = AscGetChipCfgLsw(iop_base) & 0x0003;
8993 if (channel == 0x03)
8994 return (0);
8995 else if (channel == 0x00)
8996 return (7);
8997 return (channel + 4);
8998}
8999
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009000static ushort AscSetIsaDmaChannel(PortAddr iop_base, ushort dma_channel)
Matthew Wilcox51219352007-10-02 21:55:22 -04009001{
9002 ushort cfg_lsw;
9003 uchar value;
9004
9005 if ((dma_channel >= 5) && (dma_channel <= 7)) {
9006 if (dma_channel == 7)
9007 value = 0x00;
9008 else
9009 value = dma_channel - 4;
9010 cfg_lsw = AscGetChipCfgLsw(iop_base) & 0xFFFC;
9011 cfg_lsw |= value;
9012 AscSetChipCfgLsw(iop_base, cfg_lsw);
9013 return (AscGetIsaDmaChannel(iop_base));
9014 }
9015 return 0;
9016}
9017
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009018static uchar AscGetIsaDmaSpeed(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04009019{
9020 uchar speed_value;
9021
9022 AscSetBank(iop_base, 1);
9023 speed_value = AscReadChipDmaSpeed(iop_base);
9024 speed_value &= 0x07;
9025 AscSetBank(iop_base, 0);
9026 return speed_value;
9027}
9028
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009029static uchar AscSetIsaDmaSpeed(PortAddr iop_base, uchar speed_value)
Matthew Wilcox51219352007-10-02 21:55:22 -04009030{
9031 speed_value &= 0x07;
9032 AscSetBank(iop_base, 1);
9033 AscWriteChipDmaSpeed(iop_base, speed_value);
9034 AscSetBank(iop_base, 0);
9035 return AscGetIsaDmaSpeed(iop_base);
9036}
9037#endif /* CONFIG_ISA */
9038
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009039static ushort AscInitAscDvcVar(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04009040{
9041 int i;
9042 PortAddr iop_base;
9043 ushort warn_code;
9044 uchar chip_version;
9045
9046 iop_base = asc_dvc->iop_base;
9047 warn_code = 0;
9048 asc_dvc->err_code = 0;
9049 if ((asc_dvc->bus_type &
9050 (ASC_IS_ISA | ASC_IS_PCI | ASC_IS_EISA | ASC_IS_VL)) == 0) {
9051 asc_dvc->err_code |= ASC_IERR_NO_BUS_TYPE;
9052 }
9053 AscSetChipControl(iop_base, CC_HALT);
9054 AscSetChipStatus(iop_base, 0);
9055 asc_dvc->bug_fix_cntl = 0;
9056 asc_dvc->pci_fix_asyn_xfer = 0;
9057 asc_dvc->pci_fix_asyn_xfer_always = 0;
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02009058 /* asc_dvc->init_state initialized in AscInitGetConfig(). */
Matthew Wilcox51219352007-10-02 21:55:22 -04009059 asc_dvc->sdtr_done = 0;
9060 asc_dvc->cur_total_qng = 0;
9061 asc_dvc->is_in_int = 0;
9062 asc_dvc->in_critical_cnt = 0;
9063 asc_dvc->last_q_shortage = 0;
9064 asc_dvc->use_tagged_qng = 0;
9065 asc_dvc->no_scam = 0;
9066 asc_dvc->unit_not_ready = 0;
9067 asc_dvc->queue_full_or_busy = 0;
9068 asc_dvc->redo_scam = 0;
9069 asc_dvc->res2 = 0;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009070 asc_dvc->min_sdtr_index = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04009071 asc_dvc->cfg->can_tagged_qng = 0;
9072 asc_dvc->cfg->cmd_qng_enabled = 0;
9073 asc_dvc->dvc_cntl = ASC_DEF_DVC_CNTL;
9074 asc_dvc->init_sdtr = 0;
9075 asc_dvc->max_total_qng = ASC_DEF_MAX_TOTAL_QNG;
9076 asc_dvc->scsi_reset_wait = 3;
9077 asc_dvc->start_motor = ASC_SCSI_WIDTH_BIT_SET;
9078 asc_dvc->max_dma_count = AscGetMaxDmaCount(asc_dvc->bus_type);
9079 asc_dvc->cfg->sdtr_enable = ASC_SCSI_WIDTH_BIT_SET;
9080 asc_dvc->cfg->disc_enable = ASC_SCSI_WIDTH_BIT_SET;
9081 asc_dvc->cfg->chip_scsi_id = ASC_DEF_CHIP_SCSI_ID;
Matthew Wilcox51219352007-10-02 21:55:22 -04009082 chip_version = AscGetChipVersion(iop_base, asc_dvc->bus_type);
9083 asc_dvc->cfg->chip_version = chip_version;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009084 asc_dvc->sdtr_period_tbl = asc_syn_xfer_period;
Matthew Wilcox51219352007-10-02 21:55:22 -04009085 asc_dvc->max_sdtr_index = 7;
9086 if ((asc_dvc->bus_type & ASC_IS_PCI) &&
9087 (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3150)) {
9088 asc_dvc->bus_type = ASC_IS_PCI_ULTRA;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009089 asc_dvc->sdtr_period_tbl = asc_syn_ultra_xfer_period;
Matthew Wilcox51219352007-10-02 21:55:22 -04009090 asc_dvc->max_sdtr_index = 15;
9091 if (chip_version == ASC_CHIP_VER_PCI_ULTRA_3150) {
9092 AscSetExtraControl(iop_base,
9093 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
9094 } else if (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3050) {
9095 AscSetExtraControl(iop_base,
9096 (SEC_ACTIVE_NEGATE |
9097 SEC_ENABLE_FILTER));
9098 }
9099 }
9100 if (asc_dvc->bus_type == ASC_IS_PCI) {
9101 AscSetExtraControl(iop_base,
9102 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
9103 }
9104
9105 asc_dvc->cfg->isa_dma_speed = ASC_DEF_ISA_DMA_SPEED;
9106#ifdef CONFIG_ISA
9107 if ((asc_dvc->bus_type & ASC_IS_ISA) != 0) {
9108 if (chip_version >= ASC_CHIP_MIN_VER_ISA_PNP) {
9109 AscSetChipIFC(iop_base, IFC_INIT_DEFAULT);
9110 asc_dvc->bus_type = ASC_IS_ISAPNP;
9111 }
9112 asc_dvc->cfg->isa_dma_channel =
9113 (uchar)AscGetIsaDmaChannel(iop_base);
9114 }
9115#endif /* CONFIG_ISA */
9116 for (i = 0; i <= ASC_MAX_TID; i++) {
9117 asc_dvc->cur_dvc_qng[i] = 0;
9118 asc_dvc->max_dvc_qng[i] = ASC_MAX_SCSI1_QNG;
9119 asc_dvc->scsiq_busy_head[i] = (ASC_SCSI_Q *)0L;
9120 asc_dvc->scsiq_busy_tail[i] = (ASC_SCSI_Q *)0L;
9121 asc_dvc->cfg->max_tag_qng[i] = ASC_MAX_INRAM_TAG_QNG;
9122 }
9123 return warn_code;
9124}
9125
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009126static int AscWriteEEPCmdReg(PortAddr iop_base, uchar cmd_reg)
Matthew Wilcox51219352007-10-02 21:55:22 -04009127{
9128 int retry;
9129
9130 for (retry = 0; retry < ASC_EEP_MAX_RETRY; retry++) {
9131 unsigned char read_back;
9132 AscSetChipEEPCmd(iop_base, cmd_reg);
9133 mdelay(1);
9134 read_back = AscGetChipEEPCmd(iop_base);
9135 if (read_back == cmd_reg)
9136 return 1;
9137 }
9138 return 0;
9139}
9140
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009141static void AscWaitEEPRead(void)
Matthew Wilcox51219352007-10-02 21:55:22 -04009142{
9143 mdelay(1);
9144}
9145
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009146static ushort AscReadEEPWord(PortAddr iop_base, uchar addr)
Matthew Wilcox51219352007-10-02 21:55:22 -04009147{
9148 ushort read_wval;
9149 uchar cmd_reg;
9150
9151 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
9152 AscWaitEEPRead();
9153 cmd_reg = addr | ASC_EEP_CMD_READ;
9154 AscWriteEEPCmdReg(iop_base, cmd_reg);
9155 AscWaitEEPRead();
9156 read_wval = AscGetChipEEPData(iop_base);
9157 AscWaitEEPRead();
9158 return read_wval;
9159}
9160
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009161static ushort AscGetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9162 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009163{
9164 ushort wval;
9165 ushort sum;
9166 ushort *wbuf;
9167 int cfg_beg;
9168 int cfg_end;
9169 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9170 int s_addr;
9171
9172 wbuf = (ushort *)cfg_buf;
9173 sum = 0;
9174 /* Read two config words; Byte-swapping done by AscReadEEPWord(). */
9175 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9176 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
9177 sum += *wbuf;
9178 }
9179 if (bus_type & ASC_IS_VL) {
9180 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9181 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9182 } else {
9183 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9184 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9185 }
9186 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9187 wval = AscReadEEPWord(iop_base, (uchar)s_addr);
9188 if (s_addr <= uchar_end_in_config) {
9189 /*
9190 * Swap all char fields - must unswap bytes already swapped
9191 * by AscReadEEPWord().
9192 */
9193 *wbuf = le16_to_cpu(wval);
9194 } else {
9195 /* Don't swap word field at the end - cntl field. */
9196 *wbuf = wval;
9197 }
9198 sum += wval; /* Checksum treats all EEPROM data as words. */
9199 }
9200 /*
9201 * Read the checksum word which will be compared against 'sum'
9202 * by the caller. Word field already swapped.
9203 */
9204 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
9205 return sum;
9206}
9207
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009208static int AscTestExternalLram(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04009209{
9210 PortAddr iop_base;
9211 ushort q_addr;
9212 ushort saved_word;
9213 int sta;
9214
9215 iop_base = asc_dvc->iop_base;
9216 sta = 0;
9217 q_addr = ASC_QNO_TO_QADDR(241);
9218 saved_word = AscReadLramWord(iop_base, q_addr);
9219 AscSetChipLramAddr(iop_base, q_addr);
9220 AscSetChipLramData(iop_base, 0x55AA);
9221 mdelay(10);
9222 AscSetChipLramAddr(iop_base, q_addr);
9223 if (AscGetChipLramData(iop_base) == 0x55AA) {
9224 sta = 1;
9225 AscWriteLramWord(iop_base, q_addr, saved_word);
9226 }
9227 return (sta);
9228}
9229
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009230static void AscWaitEEPWrite(void)
Matthew Wilcox51219352007-10-02 21:55:22 -04009231{
9232 mdelay(20);
Matthew Wilcox51219352007-10-02 21:55:22 -04009233}
9234
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009235static int AscWriteEEPDataReg(PortAddr iop_base, ushort data_reg)
Matthew Wilcox51219352007-10-02 21:55:22 -04009236{
9237 ushort read_back;
9238 int retry;
9239
9240 retry = 0;
9241 while (TRUE) {
9242 AscSetChipEEPData(iop_base, data_reg);
9243 mdelay(1);
9244 read_back = AscGetChipEEPData(iop_base);
9245 if (read_back == data_reg) {
9246 return (1);
9247 }
9248 if (retry++ > ASC_EEP_MAX_RETRY) {
9249 return (0);
9250 }
9251 }
9252}
9253
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009254static ushort AscWriteEEPWord(PortAddr iop_base, uchar addr, ushort word_val)
Matthew Wilcox51219352007-10-02 21:55:22 -04009255{
9256 ushort read_wval;
9257
9258 read_wval = AscReadEEPWord(iop_base, addr);
9259 if (read_wval != word_val) {
9260 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_ABLE);
9261 AscWaitEEPRead();
9262 AscWriteEEPDataReg(iop_base, word_val);
9263 AscWaitEEPRead();
9264 AscWriteEEPCmdReg(iop_base,
9265 (uchar)((uchar)ASC_EEP_CMD_WRITE | addr));
9266 AscWaitEEPWrite();
9267 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
9268 AscWaitEEPRead();
9269 return (AscReadEEPWord(iop_base, addr));
9270 }
9271 return (read_wval);
9272}
9273
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009274static int AscSetEEPConfigOnce(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9275 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009276{
9277 int n_error;
9278 ushort *wbuf;
9279 ushort word;
9280 ushort sum;
9281 int s_addr;
9282 int cfg_beg;
9283 int cfg_end;
9284 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9285
9286 wbuf = (ushort *)cfg_buf;
9287 n_error = 0;
9288 sum = 0;
9289 /* Write two config words; AscWriteEEPWord() will swap bytes. */
9290 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9291 sum += *wbuf;
9292 if (*wbuf != AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9293 n_error++;
9294 }
9295 }
9296 if (bus_type & ASC_IS_VL) {
9297 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9298 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9299 } else {
9300 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9301 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9302 }
9303 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9304 if (s_addr <= uchar_end_in_config) {
9305 /*
9306 * This is a char field. Swap char fields before they are
9307 * swapped again by AscWriteEEPWord().
9308 */
9309 word = cpu_to_le16(*wbuf);
9310 if (word !=
9311 AscWriteEEPWord(iop_base, (uchar)s_addr, word)) {
9312 n_error++;
9313 }
9314 } else {
9315 /* Don't swap word field at the end - cntl field. */
9316 if (*wbuf !=
9317 AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9318 n_error++;
9319 }
9320 }
9321 sum += *wbuf; /* Checksum calculated from word values. */
9322 }
9323 /* Write checksum word. It will be swapped by AscWriteEEPWord(). */
9324 *wbuf = sum;
9325 if (sum != AscWriteEEPWord(iop_base, (uchar)s_addr, sum)) {
9326 n_error++;
9327 }
9328
9329 /* Read EEPROM back again. */
9330 wbuf = (ushort *)cfg_buf;
9331 /*
9332 * Read two config words; Byte-swapping done by AscReadEEPWord().
9333 */
9334 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9335 if (*wbuf != AscReadEEPWord(iop_base, (uchar)s_addr)) {
9336 n_error++;
9337 }
9338 }
9339 if (bus_type & ASC_IS_VL) {
9340 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9341 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9342 } else {
9343 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9344 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9345 }
9346 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9347 if (s_addr <= uchar_end_in_config) {
9348 /*
9349 * Swap all char fields. Must unswap bytes already swapped
9350 * by AscReadEEPWord().
9351 */
9352 word =
9353 le16_to_cpu(AscReadEEPWord
9354 (iop_base, (uchar)s_addr));
9355 } else {
9356 /* Don't swap word field at the end - cntl field. */
9357 word = AscReadEEPWord(iop_base, (uchar)s_addr);
9358 }
9359 if (*wbuf != word) {
9360 n_error++;
9361 }
9362 }
9363 /* Read checksum; Byte swapping not needed. */
9364 if (AscReadEEPWord(iop_base, (uchar)s_addr) != sum) {
9365 n_error++;
9366 }
9367 return n_error;
9368}
9369
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009370static int AscSetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9371 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009372{
9373 int retry;
9374 int n_error;
9375
9376 retry = 0;
9377 while (TRUE) {
9378 if ((n_error = AscSetEEPConfigOnce(iop_base, cfg_buf,
9379 bus_type)) == 0) {
9380 break;
9381 }
9382 if (++retry > ASC_EEP_MAX_RETRY) {
9383 break;
9384 }
9385 }
9386 return n_error;
9387}
9388
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009389static ushort AscInitFromEEP(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04009390{
9391 ASCEEP_CONFIG eep_config_buf;
9392 ASCEEP_CONFIG *eep_config;
9393 PortAddr iop_base;
9394 ushort chksum;
9395 ushort warn_code;
9396 ushort cfg_msw, cfg_lsw;
9397 int i;
9398 int write_eep = 0;
9399
9400 iop_base = asc_dvc->iop_base;
9401 warn_code = 0;
9402 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0x00FE);
9403 AscStopQueueExe(iop_base);
9404 if ((AscStopChip(iop_base) == FALSE) ||
9405 (AscGetChipScsiCtrl(iop_base) != 0)) {
9406 asc_dvc->init_state |= ASC_INIT_RESET_SCSI_DONE;
9407 AscResetChipAndScsiBus(asc_dvc);
9408 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
9409 }
Hannes Reinecked647c782015-04-24 13:18:26 +02009410 if (!AscIsChipHalted(iop_base)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04009411 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
9412 return (warn_code);
9413 }
9414 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
9415 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
9416 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
9417 return (warn_code);
9418 }
9419 eep_config = (ASCEEP_CONFIG *)&eep_config_buf;
9420 cfg_msw = AscGetChipCfgMsw(iop_base);
9421 cfg_lsw = AscGetChipCfgLsw(iop_base);
9422 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9423 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9424 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9425 AscSetChipCfgMsw(iop_base, cfg_msw);
9426 }
9427 chksum = AscGetEEPConfig(iop_base, eep_config, asc_dvc->bus_type);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009428 ASC_DBG(1, "chksum 0x%x\n", chksum);
Matthew Wilcox51219352007-10-02 21:55:22 -04009429 if (chksum == 0) {
9430 chksum = 0xaa55;
9431 }
9432 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9433 warn_code |= ASC_WARN_AUTO_CONFIG;
9434 if (asc_dvc->cfg->chip_version == 3) {
9435 if (eep_config->cfg_lsw != cfg_lsw) {
9436 warn_code |= ASC_WARN_EEPROM_RECOVER;
9437 eep_config->cfg_lsw =
9438 AscGetChipCfgLsw(iop_base);
9439 }
9440 if (eep_config->cfg_msw != cfg_msw) {
9441 warn_code |= ASC_WARN_EEPROM_RECOVER;
9442 eep_config->cfg_msw =
9443 AscGetChipCfgMsw(iop_base);
9444 }
9445 }
9446 }
9447 eep_config->cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9448 eep_config->cfg_lsw |= ASC_CFG0_HOST_INT_ON;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009449 ASC_DBG(1, "eep_config->chksum 0x%x\n", eep_config->chksum);
Matthew Wilcox51219352007-10-02 21:55:22 -04009450 if (chksum != eep_config->chksum) {
9451 if (AscGetChipVersion(iop_base, asc_dvc->bus_type) ==
9452 ASC_CHIP_VER_PCI_ULTRA_3050) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009453 ASC_DBG(1, "chksum error ignored; EEPROM-less board\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009454 eep_config->init_sdtr = 0xFF;
9455 eep_config->disc_enable = 0xFF;
9456 eep_config->start_motor = 0xFF;
9457 eep_config->use_cmd_qng = 0;
9458 eep_config->max_total_qng = 0xF0;
9459 eep_config->max_tag_qng = 0x20;
9460 eep_config->cntl = 0xBFFF;
9461 ASC_EEP_SET_CHIP_ID(eep_config, 7);
9462 eep_config->no_scam = 0;
9463 eep_config->adapter_info[0] = 0;
9464 eep_config->adapter_info[1] = 0;
9465 eep_config->adapter_info[2] = 0;
9466 eep_config->adapter_info[3] = 0;
9467 eep_config->adapter_info[4] = 0;
9468 /* Indicate EEPROM-less board. */
9469 eep_config->adapter_info[5] = 0xBB;
9470 } else {
9471 ASC_PRINT
9472 ("AscInitFromEEP: EEPROM checksum error; Will try to re-write EEPROM.\n");
9473 write_eep = 1;
9474 warn_code |= ASC_WARN_EEPROM_CHKSUM;
9475 }
9476 }
9477 asc_dvc->cfg->sdtr_enable = eep_config->init_sdtr;
9478 asc_dvc->cfg->disc_enable = eep_config->disc_enable;
9479 asc_dvc->cfg->cmd_qng_enabled = eep_config->use_cmd_qng;
9480 asc_dvc->cfg->isa_dma_speed = ASC_EEP_GET_DMA_SPD(eep_config);
9481 asc_dvc->start_motor = eep_config->start_motor;
9482 asc_dvc->dvc_cntl = eep_config->cntl;
9483 asc_dvc->no_scam = eep_config->no_scam;
9484 asc_dvc->cfg->adapter_info[0] = eep_config->adapter_info[0];
9485 asc_dvc->cfg->adapter_info[1] = eep_config->adapter_info[1];
9486 asc_dvc->cfg->adapter_info[2] = eep_config->adapter_info[2];
9487 asc_dvc->cfg->adapter_info[3] = eep_config->adapter_info[3];
9488 asc_dvc->cfg->adapter_info[4] = eep_config->adapter_info[4];
9489 asc_dvc->cfg->adapter_info[5] = eep_config->adapter_info[5];
9490 if (!AscTestExternalLram(asc_dvc)) {
9491 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) ==
9492 ASC_IS_PCI_ULTRA)) {
9493 eep_config->max_total_qng =
9494 ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
9495 eep_config->max_tag_qng =
9496 ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG;
9497 } else {
9498 eep_config->cfg_msw |= 0x0800;
9499 cfg_msw |= 0x0800;
9500 AscSetChipCfgMsw(iop_base, cfg_msw);
9501 eep_config->max_total_qng = ASC_MAX_PCI_INRAM_TOTAL_QNG;
9502 eep_config->max_tag_qng = ASC_MAX_INRAM_TAG_QNG;
9503 }
9504 } else {
9505 }
9506 if (eep_config->max_total_qng < ASC_MIN_TOTAL_QNG) {
9507 eep_config->max_total_qng = ASC_MIN_TOTAL_QNG;
9508 }
9509 if (eep_config->max_total_qng > ASC_MAX_TOTAL_QNG) {
9510 eep_config->max_total_qng = ASC_MAX_TOTAL_QNG;
9511 }
9512 if (eep_config->max_tag_qng > eep_config->max_total_qng) {
9513 eep_config->max_tag_qng = eep_config->max_total_qng;
9514 }
9515 if (eep_config->max_tag_qng < ASC_MIN_TAG_Q_PER_DVC) {
9516 eep_config->max_tag_qng = ASC_MIN_TAG_Q_PER_DVC;
9517 }
9518 asc_dvc->max_total_qng = eep_config->max_total_qng;
9519 if ((eep_config->use_cmd_qng & eep_config->disc_enable) !=
9520 eep_config->use_cmd_qng) {
9521 eep_config->disc_enable = eep_config->use_cmd_qng;
9522 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9523 }
Matthew Wilcox51219352007-10-02 21:55:22 -04009524 ASC_EEP_SET_CHIP_ID(eep_config,
9525 ASC_EEP_GET_CHIP_ID(eep_config) & ASC_MAX_TID);
9526 asc_dvc->cfg->chip_scsi_id = ASC_EEP_GET_CHIP_ID(eep_config);
9527 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) &&
9528 !(asc_dvc->dvc_cntl & ASC_CNTL_SDTR_ENABLE_ULTRA)) {
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009529 asc_dvc->min_sdtr_index = ASC_SDTR_ULTRA_PCI_10MB_INDEX;
Matthew Wilcox51219352007-10-02 21:55:22 -04009530 }
9531
9532 for (i = 0; i <= ASC_MAX_TID; i++) {
9533 asc_dvc->dos_int13_table[i] = eep_config->dos_int13_table[i];
9534 asc_dvc->cfg->max_tag_qng[i] = eep_config->max_tag_qng;
9535 asc_dvc->cfg->sdtr_period_offset[i] =
9536 (uchar)(ASC_DEF_SDTR_OFFSET |
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009537 (asc_dvc->min_sdtr_index << 4));
Matthew Wilcox51219352007-10-02 21:55:22 -04009538 }
9539 eep_config->cfg_msw = AscGetChipCfgMsw(iop_base);
9540 if (write_eep) {
9541 if ((i = AscSetEEPConfig(iop_base, eep_config,
9542 asc_dvc->bus_type)) != 0) {
9543 ASC_PRINT1
9544 ("AscInitFromEEP: Failed to re-write EEPROM with %d errors.\n",
9545 i);
9546 } else {
9547 ASC_PRINT
9548 ("AscInitFromEEP: Successfully re-wrote EEPROM.\n");
9549 }
9550 }
9551 return (warn_code);
9552}
9553
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009554static int AscInitGetConfig(struct Scsi_Host *shost)
Matthew Wilcox51219352007-10-02 21:55:22 -04009555{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009556 struct asc_board *board = shost_priv(shost);
9557 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04009558 unsigned short warn_code = 0;
9559
9560 asc_dvc->init_state = ASC_INIT_STATE_BEG_GET_CFG;
9561 if (asc_dvc->err_code != 0)
9562 return asc_dvc->err_code;
9563
9564 if (AscFindSignature(asc_dvc->iop_base)) {
9565 warn_code |= AscInitAscDvcVar(asc_dvc);
9566 warn_code |= AscInitFromEEP(asc_dvc);
9567 asc_dvc->init_state |= ASC_INIT_STATE_END_GET_CFG;
9568 if (asc_dvc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
9569 asc_dvc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
9570 } else {
9571 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9572 }
9573
9574 switch (warn_code) {
9575 case 0: /* No error */
9576 break;
9577 case ASC_WARN_IO_PORT_ROTATE:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009578 shost_printk(KERN_WARNING, shost, "I/O port address "
9579 "modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009580 break;
9581 case ASC_WARN_AUTO_CONFIG:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009582 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9583 "enabled\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009584 break;
9585 case ASC_WARN_EEPROM_CHKSUM:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009586 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009587 break;
9588 case ASC_WARN_IRQ_MODIFIED:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009589 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009590 break;
9591 case ASC_WARN_CMD_QNG_CONFLICT:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009592 shost_printk(KERN_WARNING, shost, "tag queuing enabled w/o "
9593 "disconnects\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009594 break;
9595 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009596 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9597 warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -04009598 break;
9599 }
9600
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009601 if (asc_dvc->err_code != 0)
9602 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9603 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
Matthew Wilcox51219352007-10-02 21:55:22 -04009604
9605 return asc_dvc->err_code;
9606}
9607
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009608static int AscInitSetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
Matthew Wilcox51219352007-10-02 21:55:22 -04009609{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009610 struct asc_board *board = shost_priv(shost);
9611 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04009612 PortAddr iop_base = asc_dvc->iop_base;
9613 unsigned short cfg_msw;
9614 unsigned short warn_code = 0;
9615
9616 asc_dvc->init_state |= ASC_INIT_STATE_BEG_SET_CFG;
9617 if (asc_dvc->err_code != 0)
9618 return asc_dvc->err_code;
9619 if (!AscFindSignature(asc_dvc->iop_base)) {
9620 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9621 return asc_dvc->err_code;
9622 }
9623
9624 cfg_msw = AscGetChipCfgMsw(iop_base);
9625 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9626 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9627 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9628 AscSetChipCfgMsw(iop_base, cfg_msw);
9629 }
9630 if ((asc_dvc->cfg->cmd_qng_enabled & asc_dvc->cfg->disc_enable) !=
9631 asc_dvc->cfg->cmd_qng_enabled) {
9632 asc_dvc->cfg->disc_enable = asc_dvc->cfg->cmd_qng_enabled;
9633 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9634 }
9635 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9636 warn_code |= ASC_WARN_AUTO_CONFIG;
9637 }
Matthew Wilcox51219352007-10-02 21:55:22 -04009638#ifdef CONFIG_PCI
9639 if (asc_dvc->bus_type & ASC_IS_PCI) {
9640 cfg_msw &= 0xFFC0;
9641 AscSetChipCfgMsw(iop_base, cfg_msw);
9642 if ((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) {
9643 } else {
9644 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
9645 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
9646 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_IF_NOT_DWB;
9647 asc_dvc->bug_fix_cntl |=
9648 ASC_BUG_FIX_ASYN_USE_SYN;
9649 }
9650 }
9651 } else
9652#endif /* CONFIG_PCI */
9653 if (asc_dvc->bus_type == ASC_IS_ISAPNP) {
9654 if (AscGetChipVersion(iop_base, asc_dvc->bus_type)
9655 == ASC_CHIP_VER_ASYN_BUG) {
9656 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
9657 }
9658 }
9659 if (AscSetChipScsiID(iop_base, asc_dvc->cfg->chip_scsi_id) !=
9660 asc_dvc->cfg->chip_scsi_id) {
9661 asc_dvc->err_code |= ASC_IERR_SET_SCSI_ID;
9662 }
9663#ifdef CONFIG_ISA
9664 if (asc_dvc->bus_type & ASC_IS_ISA) {
9665 AscSetIsaDmaChannel(iop_base, asc_dvc->cfg->isa_dma_channel);
9666 AscSetIsaDmaSpeed(iop_base, asc_dvc->cfg->isa_dma_speed);
9667 }
9668#endif /* CONFIG_ISA */
9669
9670 asc_dvc->init_state |= ASC_INIT_STATE_END_SET_CFG;
9671
9672 switch (warn_code) {
9673 case 0: /* No error. */
9674 break;
9675 case ASC_WARN_IO_PORT_ROTATE:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009676 shost_printk(KERN_WARNING, shost, "I/O port address "
9677 "modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009678 break;
9679 case ASC_WARN_AUTO_CONFIG:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009680 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9681 "enabled\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009682 break;
9683 case ASC_WARN_EEPROM_CHKSUM:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009684 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009685 break;
9686 case ASC_WARN_IRQ_MODIFIED:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009687 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009688 break;
9689 case ASC_WARN_CMD_QNG_CONFLICT:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009690 shost_printk(KERN_WARNING, shost, "tag queuing w/o "
9691 "disconnects\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009692 break;
9693 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009694 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9695 warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -04009696 break;
9697 }
9698
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009699 if (asc_dvc->err_code != 0)
9700 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9701 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
Matthew Wilcox51219352007-10-02 21:55:22 -04009702
9703 return asc_dvc->err_code;
9704}
9705
9706/*
9707 * EEPROM Configuration.
9708 *
9709 * All drivers should use this structure to set the default EEPROM
9710 * configuration. The BIOS now uses this structure when it is built.
9711 * Additional structure information can be found in a_condor.h where
9712 * the structure is defined.
9713 *
9714 * The *_Field_IsChar structs are needed to correct for endianness.
9715 * These values are read from the board 16 bits at a time directly
9716 * into the structs. Because some fields are char, the values will be
9717 * in the wrong order. The *_Field_IsChar tells when to flip the
9718 * bytes. Data read and written to PCI memory is automatically swapped
9719 * on big-endian platforms so char fields read as words are actually being
9720 * unswapped on big-endian platforms.
9721 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009722static ADVEEP_3550_CONFIG Default_3550_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009723 ADV_EEPROM_BIOS_ENABLE, /* cfg_lsw */
9724 0x0000, /* cfg_msw */
9725 0xFFFF, /* disc_enable */
9726 0xFFFF, /* wdtr_able */
9727 0xFFFF, /* sdtr_able */
9728 0xFFFF, /* start_motor */
9729 0xFFFF, /* tagqng_able */
9730 0xFFFF, /* bios_scan */
9731 0, /* scam_tolerant */
9732 7, /* adapter_scsi_id */
9733 0, /* bios_boot_delay */
9734 3, /* scsi_reset_delay */
9735 0, /* bios_id_lun */
9736 0, /* termination */
9737 0, /* reserved1 */
9738 0xFFE7, /* bios_ctrl */
9739 0xFFFF, /* ultra_able */
9740 0, /* reserved2 */
9741 ASC_DEF_MAX_HOST_QNG, /* max_host_qng */
9742 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9743 0, /* dvc_cntl */
9744 0, /* bug_fix */
9745 0, /* serial_number_word1 */
9746 0, /* serial_number_word2 */
9747 0, /* serial_number_word3 */
9748 0, /* check_sum */
9749 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9750 , /* oem_name[16] */
9751 0, /* dvc_err_code */
9752 0, /* adv_err_code */
9753 0, /* adv_err_addr */
9754 0, /* saved_dvc_err_code */
9755 0, /* saved_adv_err_code */
9756 0, /* saved_adv_err_addr */
9757 0 /* num_of_err */
9758};
9759
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009760static ADVEEP_3550_CONFIG ADVEEP_3550_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009761 0, /* cfg_lsw */
9762 0, /* cfg_msw */
9763 0, /* -disc_enable */
9764 0, /* wdtr_able */
9765 0, /* sdtr_able */
9766 0, /* start_motor */
9767 0, /* tagqng_able */
9768 0, /* bios_scan */
9769 0, /* scam_tolerant */
9770 1, /* adapter_scsi_id */
9771 1, /* bios_boot_delay */
9772 1, /* scsi_reset_delay */
9773 1, /* bios_id_lun */
9774 1, /* termination */
9775 1, /* reserved1 */
9776 0, /* bios_ctrl */
9777 0, /* ultra_able */
9778 0, /* reserved2 */
9779 1, /* max_host_qng */
9780 1, /* max_dvc_qng */
9781 0, /* dvc_cntl */
9782 0, /* bug_fix */
9783 0, /* serial_number_word1 */
9784 0, /* serial_number_word2 */
9785 0, /* serial_number_word3 */
9786 0, /* check_sum */
9787 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9788 , /* oem_name[16] */
9789 0, /* dvc_err_code */
9790 0, /* adv_err_code */
9791 0, /* adv_err_addr */
9792 0, /* saved_dvc_err_code */
9793 0, /* saved_adv_err_code */
9794 0, /* saved_adv_err_addr */
9795 0 /* num_of_err */
9796};
9797
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009798static ADVEEP_38C0800_CONFIG Default_38C0800_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009799 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9800 0x0000, /* 01 cfg_msw */
9801 0xFFFF, /* 02 disc_enable */
9802 0xFFFF, /* 03 wdtr_able */
9803 0x4444, /* 04 sdtr_speed1 */
9804 0xFFFF, /* 05 start_motor */
9805 0xFFFF, /* 06 tagqng_able */
9806 0xFFFF, /* 07 bios_scan */
9807 0, /* 08 scam_tolerant */
9808 7, /* 09 adapter_scsi_id */
9809 0, /* bios_boot_delay */
9810 3, /* 10 scsi_reset_delay */
9811 0, /* bios_id_lun */
9812 0, /* 11 termination_se */
9813 0, /* termination_lvd */
9814 0xFFE7, /* 12 bios_ctrl */
9815 0x4444, /* 13 sdtr_speed2 */
9816 0x4444, /* 14 sdtr_speed3 */
9817 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9818 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9819 0, /* 16 dvc_cntl */
9820 0x4444, /* 17 sdtr_speed4 */
9821 0, /* 18 serial_number_word1 */
9822 0, /* 19 serial_number_word2 */
9823 0, /* 20 serial_number_word3 */
9824 0, /* 21 check_sum */
9825 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9826 , /* 22-29 oem_name[16] */
9827 0, /* 30 dvc_err_code */
9828 0, /* 31 adv_err_code */
9829 0, /* 32 adv_err_addr */
9830 0, /* 33 saved_dvc_err_code */
9831 0, /* 34 saved_adv_err_code */
9832 0, /* 35 saved_adv_err_addr */
9833 0, /* 36 reserved */
9834 0, /* 37 reserved */
9835 0, /* 38 reserved */
9836 0, /* 39 reserved */
9837 0, /* 40 reserved */
9838 0, /* 41 reserved */
9839 0, /* 42 reserved */
9840 0, /* 43 reserved */
9841 0, /* 44 reserved */
9842 0, /* 45 reserved */
9843 0, /* 46 reserved */
9844 0, /* 47 reserved */
9845 0, /* 48 reserved */
9846 0, /* 49 reserved */
9847 0, /* 50 reserved */
9848 0, /* 51 reserved */
9849 0, /* 52 reserved */
9850 0, /* 53 reserved */
9851 0, /* 54 reserved */
9852 0, /* 55 reserved */
9853 0, /* 56 cisptr_lsw */
9854 0, /* 57 cisprt_msw */
9855 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9856 PCI_DEVICE_ID_38C0800_REV1, /* 59 subsysid */
9857 0, /* 60 reserved */
9858 0, /* 61 reserved */
9859 0, /* 62 reserved */
9860 0 /* 63 reserved */
9861};
9862
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009863static ADVEEP_38C0800_CONFIG ADVEEP_38C0800_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009864 0, /* 00 cfg_lsw */
9865 0, /* 01 cfg_msw */
9866 0, /* 02 disc_enable */
9867 0, /* 03 wdtr_able */
9868 0, /* 04 sdtr_speed1 */
9869 0, /* 05 start_motor */
9870 0, /* 06 tagqng_able */
9871 0, /* 07 bios_scan */
9872 0, /* 08 scam_tolerant */
9873 1, /* 09 adapter_scsi_id */
9874 1, /* bios_boot_delay */
9875 1, /* 10 scsi_reset_delay */
9876 1, /* bios_id_lun */
9877 1, /* 11 termination_se */
9878 1, /* termination_lvd */
9879 0, /* 12 bios_ctrl */
9880 0, /* 13 sdtr_speed2 */
9881 0, /* 14 sdtr_speed3 */
9882 1, /* 15 max_host_qng */
9883 1, /* max_dvc_qng */
9884 0, /* 16 dvc_cntl */
9885 0, /* 17 sdtr_speed4 */
9886 0, /* 18 serial_number_word1 */
9887 0, /* 19 serial_number_word2 */
9888 0, /* 20 serial_number_word3 */
9889 0, /* 21 check_sum */
9890 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9891 , /* 22-29 oem_name[16] */
9892 0, /* 30 dvc_err_code */
9893 0, /* 31 adv_err_code */
9894 0, /* 32 adv_err_addr */
9895 0, /* 33 saved_dvc_err_code */
9896 0, /* 34 saved_adv_err_code */
9897 0, /* 35 saved_adv_err_addr */
9898 0, /* 36 reserved */
9899 0, /* 37 reserved */
9900 0, /* 38 reserved */
9901 0, /* 39 reserved */
9902 0, /* 40 reserved */
9903 0, /* 41 reserved */
9904 0, /* 42 reserved */
9905 0, /* 43 reserved */
9906 0, /* 44 reserved */
9907 0, /* 45 reserved */
9908 0, /* 46 reserved */
9909 0, /* 47 reserved */
9910 0, /* 48 reserved */
9911 0, /* 49 reserved */
9912 0, /* 50 reserved */
9913 0, /* 51 reserved */
9914 0, /* 52 reserved */
9915 0, /* 53 reserved */
9916 0, /* 54 reserved */
9917 0, /* 55 reserved */
9918 0, /* 56 cisptr_lsw */
9919 0, /* 57 cisprt_msw */
9920 0, /* 58 subsysvid */
9921 0, /* 59 subsysid */
9922 0, /* 60 reserved */
9923 0, /* 61 reserved */
9924 0, /* 62 reserved */
9925 0 /* 63 reserved */
9926};
9927
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009928static ADVEEP_38C1600_CONFIG Default_38C1600_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009929 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9930 0x0000, /* 01 cfg_msw */
9931 0xFFFF, /* 02 disc_enable */
9932 0xFFFF, /* 03 wdtr_able */
9933 0x5555, /* 04 sdtr_speed1 */
9934 0xFFFF, /* 05 start_motor */
9935 0xFFFF, /* 06 tagqng_able */
9936 0xFFFF, /* 07 bios_scan */
9937 0, /* 08 scam_tolerant */
9938 7, /* 09 adapter_scsi_id */
9939 0, /* bios_boot_delay */
9940 3, /* 10 scsi_reset_delay */
9941 0, /* bios_id_lun */
9942 0, /* 11 termination_se */
9943 0, /* termination_lvd */
9944 0xFFE7, /* 12 bios_ctrl */
9945 0x5555, /* 13 sdtr_speed2 */
9946 0x5555, /* 14 sdtr_speed3 */
9947 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9948 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9949 0, /* 16 dvc_cntl */
9950 0x5555, /* 17 sdtr_speed4 */
9951 0, /* 18 serial_number_word1 */
9952 0, /* 19 serial_number_word2 */
9953 0, /* 20 serial_number_word3 */
9954 0, /* 21 check_sum */
9955 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9956 , /* 22-29 oem_name[16] */
9957 0, /* 30 dvc_err_code */
9958 0, /* 31 adv_err_code */
9959 0, /* 32 adv_err_addr */
9960 0, /* 33 saved_dvc_err_code */
9961 0, /* 34 saved_adv_err_code */
9962 0, /* 35 saved_adv_err_addr */
9963 0, /* 36 reserved */
9964 0, /* 37 reserved */
9965 0, /* 38 reserved */
9966 0, /* 39 reserved */
9967 0, /* 40 reserved */
9968 0, /* 41 reserved */
9969 0, /* 42 reserved */
9970 0, /* 43 reserved */
9971 0, /* 44 reserved */
9972 0, /* 45 reserved */
9973 0, /* 46 reserved */
9974 0, /* 47 reserved */
9975 0, /* 48 reserved */
9976 0, /* 49 reserved */
9977 0, /* 50 reserved */
9978 0, /* 51 reserved */
9979 0, /* 52 reserved */
9980 0, /* 53 reserved */
9981 0, /* 54 reserved */
9982 0, /* 55 reserved */
9983 0, /* 56 cisptr_lsw */
9984 0, /* 57 cisprt_msw */
9985 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9986 PCI_DEVICE_ID_38C1600_REV1, /* 59 subsysid */
9987 0, /* 60 reserved */
9988 0, /* 61 reserved */
9989 0, /* 62 reserved */
9990 0 /* 63 reserved */
9991};
9992
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009993static ADVEEP_38C1600_CONFIG ADVEEP_38C1600_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009994 0, /* 00 cfg_lsw */
9995 0, /* 01 cfg_msw */
9996 0, /* 02 disc_enable */
9997 0, /* 03 wdtr_able */
9998 0, /* 04 sdtr_speed1 */
9999 0, /* 05 start_motor */
10000 0, /* 06 tagqng_able */
10001 0, /* 07 bios_scan */
10002 0, /* 08 scam_tolerant */
10003 1, /* 09 adapter_scsi_id */
10004 1, /* bios_boot_delay */
10005 1, /* 10 scsi_reset_delay */
10006 1, /* bios_id_lun */
10007 1, /* 11 termination_se */
10008 1, /* termination_lvd */
10009 0, /* 12 bios_ctrl */
10010 0, /* 13 sdtr_speed2 */
10011 0, /* 14 sdtr_speed3 */
10012 1, /* 15 max_host_qng */
10013 1, /* max_dvc_qng */
10014 0, /* 16 dvc_cntl */
10015 0, /* 17 sdtr_speed4 */
10016 0, /* 18 serial_number_word1 */
10017 0, /* 19 serial_number_word2 */
10018 0, /* 20 serial_number_word3 */
10019 0, /* 21 check_sum */
10020 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
10021 , /* 22-29 oem_name[16] */
10022 0, /* 30 dvc_err_code */
10023 0, /* 31 adv_err_code */
10024 0, /* 32 adv_err_addr */
10025 0, /* 33 saved_dvc_err_code */
10026 0, /* 34 saved_adv_err_code */
10027 0, /* 35 saved_adv_err_addr */
10028 0, /* 36 reserved */
10029 0, /* 37 reserved */
10030 0, /* 38 reserved */
10031 0, /* 39 reserved */
10032 0, /* 40 reserved */
10033 0, /* 41 reserved */
10034 0, /* 42 reserved */
10035 0, /* 43 reserved */
10036 0, /* 44 reserved */
10037 0, /* 45 reserved */
10038 0, /* 46 reserved */
10039 0, /* 47 reserved */
10040 0, /* 48 reserved */
10041 0, /* 49 reserved */
10042 0, /* 50 reserved */
10043 0, /* 51 reserved */
10044 0, /* 52 reserved */
10045 0, /* 53 reserved */
10046 0, /* 54 reserved */
10047 0, /* 55 reserved */
10048 0, /* 56 cisptr_lsw */
10049 0, /* 57 cisprt_msw */
10050 0, /* 58 subsysvid */
10051 0, /* 59 subsysid */
10052 0, /* 60 reserved */
10053 0, /* 61 reserved */
10054 0, /* 62 reserved */
10055 0 /* 63 reserved */
10056};
10057
10058#ifdef CONFIG_PCI
10059/*
10060 * Wait for EEPROM command to complete
10061 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010062static void AdvWaitEEPCmd(AdvPortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -040010063{
10064 int eep_delay_ms;
10065
10066 for (eep_delay_ms = 0; eep_delay_ms < ADV_EEP_DELAY_MS; eep_delay_ms++) {
10067 if (AdvReadWordRegister(iop_base, IOPW_EE_CMD) &
10068 ASC_EEP_CMD_DONE) {
10069 break;
10070 }
10071 mdelay(1);
10072 }
10073 if ((AdvReadWordRegister(iop_base, IOPW_EE_CMD) & ASC_EEP_CMD_DONE) ==
10074 0)
10075 BUG();
10076}
10077
10078/*
10079 * Read the EEPROM from specified location
10080 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010081static ushort AdvReadEEPWord(AdvPortAddr iop_base, int eep_word_addr)
Matthew Wilcox51219352007-10-02 21:55:22 -040010082{
10083 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10084 ASC_EEP_CMD_READ | eep_word_addr);
10085 AdvWaitEEPCmd(iop_base);
10086 return AdvReadWordRegister(iop_base, IOPW_EE_DATA);
10087}
10088
10089/*
10090 * Write the EEPROM from 'cfg_buf'.
10091 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010092static void AdvSet3550EEPConfig(AdvPortAddr iop_base,
10093 ADVEEP_3550_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010094{
10095 ushort *wbuf;
10096 ushort addr, chksum;
10097 ushort *charfields;
10098
10099 wbuf = (ushort *)cfg_buf;
10100 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
10101 chksum = 0;
10102
10103 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10104 AdvWaitEEPCmd(iop_base);
10105
10106 /*
10107 * Write EEPROM from word 0 to word 20.
10108 */
10109 for (addr = ADV_EEP_DVC_CFG_BEGIN;
10110 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10111 ushort word;
10112
10113 if (*charfields++) {
10114 word = cpu_to_le16(*wbuf);
10115 } else {
10116 word = *wbuf;
10117 }
10118 chksum += *wbuf; /* Checksum is calculated from word values. */
10119 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10120 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10121 ASC_EEP_CMD_WRITE | addr);
10122 AdvWaitEEPCmd(iop_base);
10123 mdelay(ADV_EEP_DELAY_MS);
10124 }
10125
10126 /*
10127 * Write EEPROM checksum at word 21.
10128 */
10129 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10130 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10131 AdvWaitEEPCmd(iop_base);
10132 wbuf++;
10133 charfields++;
10134
10135 /*
10136 * Write EEPROM OEM name at words 22 to 29.
10137 */
10138 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10139 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10140 ushort word;
10141
10142 if (*charfields++) {
10143 word = cpu_to_le16(*wbuf);
10144 } else {
10145 word = *wbuf;
10146 }
10147 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10148 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10149 ASC_EEP_CMD_WRITE | addr);
10150 AdvWaitEEPCmd(iop_base);
10151 }
10152 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10153 AdvWaitEEPCmd(iop_base);
10154}
10155
10156/*
10157 * Write the EEPROM from 'cfg_buf'.
10158 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010159static void AdvSet38C0800EEPConfig(AdvPortAddr iop_base,
10160 ADVEEP_38C0800_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010161{
10162 ushort *wbuf;
10163 ushort *charfields;
10164 ushort addr, chksum;
10165
10166 wbuf = (ushort *)cfg_buf;
10167 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10168 chksum = 0;
10169
10170 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10171 AdvWaitEEPCmd(iop_base);
10172
10173 /*
10174 * Write EEPROM from word 0 to word 20.
10175 */
10176 for (addr = ADV_EEP_DVC_CFG_BEGIN;
10177 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10178 ushort word;
10179
10180 if (*charfields++) {
10181 word = cpu_to_le16(*wbuf);
10182 } else {
10183 word = *wbuf;
10184 }
10185 chksum += *wbuf; /* Checksum is calculated from word values. */
10186 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10187 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10188 ASC_EEP_CMD_WRITE | addr);
10189 AdvWaitEEPCmd(iop_base);
10190 mdelay(ADV_EEP_DELAY_MS);
10191 }
10192
10193 /*
10194 * Write EEPROM checksum at word 21.
10195 */
10196 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10197 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10198 AdvWaitEEPCmd(iop_base);
10199 wbuf++;
10200 charfields++;
10201
10202 /*
10203 * Write EEPROM OEM name at words 22 to 29.
10204 */
10205 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10206 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10207 ushort word;
10208
10209 if (*charfields++) {
10210 word = cpu_to_le16(*wbuf);
10211 } else {
10212 word = *wbuf;
10213 }
10214 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10215 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10216 ASC_EEP_CMD_WRITE | addr);
10217 AdvWaitEEPCmd(iop_base);
10218 }
10219 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10220 AdvWaitEEPCmd(iop_base);
10221}
10222
10223/*
10224 * Write the EEPROM from 'cfg_buf'.
10225 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010226static void AdvSet38C1600EEPConfig(AdvPortAddr iop_base,
10227 ADVEEP_38C1600_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010228{
10229 ushort *wbuf;
10230 ushort *charfields;
10231 ushort addr, chksum;
10232
10233 wbuf = (ushort *)cfg_buf;
10234 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
10235 chksum = 0;
10236
10237 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
10238 AdvWaitEEPCmd(iop_base);
10239
10240 /*
10241 * Write EEPROM from word 0 to word 20.
10242 */
10243 for (addr = ADV_EEP_DVC_CFG_BEGIN;
10244 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
10245 ushort word;
10246
10247 if (*charfields++) {
10248 word = cpu_to_le16(*wbuf);
10249 } else {
10250 word = *wbuf;
10251 }
10252 chksum += *wbuf; /* Checksum is calculated from word values. */
10253 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10254 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10255 ASC_EEP_CMD_WRITE | addr);
10256 AdvWaitEEPCmd(iop_base);
10257 mdelay(ADV_EEP_DELAY_MS);
10258 }
10259
10260 /*
10261 * Write EEPROM checksum at word 21.
10262 */
10263 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
10264 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
10265 AdvWaitEEPCmd(iop_base);
10266 wbuf++;
10267 charfields++;
10268
10269 /*
10270 * Write EEPROM OEM name at words 22 to 29.
10271 */
10272 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10273 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10274 ushort word;
10275
10276 if (*charfields++) {
10277 word = cpu_to_le16(*wbuf);
10278 } else {
10279 word = *wbuf;
10280 }
10281 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10282 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10283 ASC_EEP_CMD_WRITE | addr);
10284 AdvWaitEEPCmd(iop_base);
10285 }
10286 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10287 AdvWaitEEPCmd(iop_base);
10288}
10289
10290/*
10291 * Read EEPROM configuration into the specified buffer.
10292 *
10293 * Return a checksum based on the EEPROM configuration read.
10294 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010295static ushort AdvGet3550EEPConfig(AdvPortAddr iop_base,
10296 ADVEEP_3550_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010297{
10298 ushort wval, chksum;
10299 ushort *wbuf;
10300 int eep_addr;
10301 ushort *charfields;
10302
10303 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
10304 wbuf = (ushort *)cfg_buf;
10305 chksum = 0;
10306
10307 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10308 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10309 wval = AdvReadEEPWord(iop_base, eep_addr);
10310 chksum += wval; /* Checksum is calculated from word values. */
10311 if (*charfields++) {
10312 *wbuf = le16_to_cpu(wval);
10313 } else {
10314 *wbuf = wval;
10315 }
10316 }
10317 /* Read checksum word. */
10318 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10319 wbuf++;
10320 charfields++;
10321
10322 /* Read rest of EEPROM not covered by the checksum. */
10323 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10324 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10325 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10326 if (*charfields++) {
10327 *wbuf = le16_to_cpu(*wbuf);
10328 }
10329 }
10330 return chksum;
10331}
10332
10333/*
10334 * Read EEPROM configuration into the specified buffer.
10335 *
10336 * Return a checksum based on the EEPROM configuration read.
10337 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010338static ushort AdvGet38C0800EEPConfig(AdvPortAddr iop_base,
10339 ADVEEP_38C0800_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010340{
10341 ushort wval, chksum;
10342 ushort *wbuf;
10343 int eep_addr;
10344 ushort *charfields;
10345
10346 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10347 wbuf = (ushort *)cfg_buf;
10348 chksum = 0;
10349
10350 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10351 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10352 wval = AdvReadEEPWord(iop_base, eep_addr);
10353 chksum += wval; /* Checksum is calculated from word values. */
10354 if (*charfields++) {
10355 *wbuf = le16_to_cpu(wval);
10356 } else {
10357 *wbuf = wval;
10358 }
10359 }
10360 /* Read checksum word. */
10361 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10362 wbuf++;
10363 charfields++;
10364
10365 /* Read rest of EEPROM not covered by the checksum. */
10366 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10367 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10368 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10369 if (*charfields++) {
10370 *wbuf = le16_to_cpu(*wbuf);
10371 }
10372 }
10373 return chksum;
10374}
10375
10376/*
10377 * Read EEPROM configuration into the specified buffer.
10378 *
10379 * Return a checksum based on the EEPROM configuration read.
10380 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010381static ushort AdvGet38C1600EEPConfig(AdvPortAddr iop_base,
10382 ADVEEP_38C1600_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010383{
10384 ushort wval, chksum;
10385 ushort *wbuf;
10386 int eep_addr;
10387 ushort *charfields;
10388
10389 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
10390 wbuf = (ushort *)cfg_buf;
10391 chksum = 0;
10392
10393 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10394 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10395 wval = AdvReadEEPWord(iop_base, eep_addr);
10396 chksum += wval; /* Checksum is calculated from word values. */
10397 if (*charfields++) {
10398 *wbuf = le16_to_cpu(wval);
10399 } else {
10400 *wbuf = wval;
10401 }
10402 }
10403 /* Read checksum word. */
10404 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10405 wbuf++;
10406 charfields++;
10407
10408 /* Read rest of EEPROM not covered by the checksum. */
10409 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10410 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10411 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10412 if (*charfields++) {
10413 *wbuf = le16_to_cpu(*wbuf);
10414 }
10415 }
10416 return chksum;
10417}
10418
10419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10421 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10422 * all of this is done.
10423 *
10424 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10425 *
10426 * For a non-fatal error return a warning code. If there are no warnings
10427 * then 0 is returned.
10428 *
10429 * Note: Chip is stopped on entry.
10430 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010431static int AdvInitFrom3550EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010433 AdvPortAddr iop_base;
10434 ushort warn_code;
10435 ADVEEP_3550_CONFIG eep_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010437 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010439 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010441 /*
10442 * Read the board's EEPROM configuration.
10443 *
10444 * Set default values if a bad checksum is found.
10445 */
10446 if (AdvGet3550EEPConfig(iop_base, &eep_config) != eep_config.check_sum) {
10447 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010449 /*
10450 * Set EEPROM default values.
10451 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010452 memcpy(&eep_config, &Default_3550_EEPROM_Config,
10453 sizeof(ADVEEP_3550_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010455 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010456 * Assume the 6 byte board serial number that was read from
10457 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010458 */
10459 eep_config.serial_number_word3 =
10460 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010462 eep_config.serial_number_word2 =
10463 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010465 eep_config.serial_number_word1 =
10466 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010468 AdvSet3550EEPConfig(iop_base, &eep_config);
10469 }
10470 /*
10471 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10472 * EEPROM configuration that was read.
10473 *
10474 * This is the mapping of EEPROM fields to Adv Library fields.
10475 */
10476 asc_dvc->wdtr_able = eep_config.wdtr_able;
10477 asc_dvc->sdtr_able = eep_config.sdtr_able;
10478 asc_dvc->ultra_able = eep_config.ultra_able;
10479 asc_dvc->tagqng_able = eep_config.tagqng_able;
10480 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10481 asc_dvc->max_host_qng = eep_config.max_host_qng;
10482 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10483 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10484 asc_dvc->start_motor = eep_config.start_motor;
10485 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10486 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10487 asc_dvc->no_scam = eep_config.scam_tolerant;
10488 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10489 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10490 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010492 /*
10493 * Set the host maximum queuing (max. 253, min. 16) and the per device
10494 * maximum queuing (max. 63, min. 4).
10495 */
10496 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10497 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10498 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10499 /* If the value is zero, assume it is uninitialized. */
10500 if (eep_config.max_host_qng == 0) {
10501 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10502 } else {
10503 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10504 }
10505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010506
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010507 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10508 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10509 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10510 /* If the value is zero, assume it is uninitialized. */
10511 if (eep_config.max_dvc_qng == 0) {
10512 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10513 } else {
10514 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10515 }
10516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010518 /*
10519 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10520 * set 'max_dvc_qng' to 'max_host_qng'.
10521 */
10522 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10523 eep_config.max_dvc_qng = eep_config.max_host_qng;
10524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010526 /*
10527 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10528 * values based on possibly adjusted EEPROM values.
10529 */
10530 asc_dvc->max_host_qng = eep_config.max_host_qng;
10531 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010533 /*
10534 * If the EEPROM 'termination' field is set to automatic (0), then set
10535 * the ADV_DVC_CFG 'termination' field to automatic also.
10536 *
10537 * If the termination is specified with a non-zero 'termination'
10538 * value check that a legal value is set and set the ADV_DVC_CFG
10539 * 'termination' field appropriately.
10540 */
10541 if (eep_config.termination == 0) {
10542 asc_dvc->cfg->termination = 0; /* auto termination */
10543 } else {
10544 /* Enable manual control with low off / high off. */
10545 if (eep_config.termination == 1) {
10546 asc_dvc->cfg->termination = TERM_CTL_SEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010547
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010548 /* Enable manual control with low off / high on. */
10549 } else if (eep_config.termination == 2) {
10550 asc_dvc->cfg->termination = TERM_CTL_SEL | TERM_CTL_H;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010552 /* Enable manual control with low on / high on. */
10553 } else if (eep_config.termination == 3) {
10554 asc_dvc->cfg->termination =
10555 TERM_CTL_SEL | TERM_CTL_H | TERM_CTL_L;
10556 } else {
10557 /*
10558 * The EEPROM 'termination' field contains a bad value. Use
10559 * automatic termination instead.
10560 */
10561 asc_dvc->cfg->termination = 0;
10562 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10563 }
10564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010566 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010567}
10568
10569/*
10570 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10571 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10572 * all of this is done.
10573 *
10574 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10575 *
10576 * For a non-fatal error return a warning code. If there are no warnings
10577 * then 0 is returned.
10578 *
10579 * Note: Chip is stopped on entry.
10580 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010581static int AdvInitFrom38C0800EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010583 AdvPortAddr iop_base;
10584 ushort warn_code;
10585 ADVEEP_38C0800_CONFIG eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010586 uchar tid, termination;
10587 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010589 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010590
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010591 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010592
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010593 /*
10594 * Read the board's EEPROM configuration.
10595 *
10596 * Set default values if a bad checksum is found.
10597 */
10598 if (AdvGet38C0800EEPConfig(iop_base, &eep_config) !=
10599 eep_config.check_sum) {
10600 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010602 /*
10603 * Set EEPROM default values.
10604 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010605 memcpy(&eep_config, &Default_38C0800_EEPROM_Config,
10606 sizeof(ADVEEP_38C0800_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010607
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010608 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010609 * Assume the 6 byte board serial number that was read from
10610 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010611 */
10612 eep_config.serial_number_word3 =
10613 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010615 eep_config.serial_number_word2 =
10616 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010617
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010618 eep_config.serial_number_word1 =
10619 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010620
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010621 AdvSet38C0800EEPConfig(iop_base, &eep_config);
10622 }
10623 /*
10624 * Set ADV_DVC_VAR and ADV_DVC_CFG variables from the
10625 * EEPROM configuration that was read.
10626 *
10627 * This is the mapping of EEPROM fields to Adv Library fields.
10628 */
10629 asc_dvc->wdtr_able = eep_config.wdtr_able;
10630 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10631 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10632 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10633 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10634 asc_dvc->tagqng_able = eep_config.tagqng_able;
10635 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10636 asc_dvc->max_host_qng = eep_config.max_host_qng;
10637 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10638 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10639 asc_dvc->start_motor = eep_config.start_motor;
10640 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10641 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10642 asc_dvc->no_scam = eep_config.scam_tolerant;
10643 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10644 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10645 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010646
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010647 /*
10648 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10649 * are set, then set an 'sdtr_able' bit for it.
10650 */
10651 asc_dvc->sdtr_able = 0;
10652 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
10653 if (tid == 0) {
10654 sdtr_speed = asc_dvc->sdtr_speed1;
10655 } else if (tid == 4) {
10656 sdtr_speed = asc_dvc->sdtr_speed2;
10657 } else if (tid == 8) {
10658 sdtr_speed = asc_dvc->sdtr_speed3;
10659 } else if (tid == 12) {
10660 sdtr_speed = asc_dvc->sdtr_speed4;
10661 }
10662 if (sdtr_speed & ADV_MAX_TID) {
10663 asc_dvc->sdtr_able |= (1 << tid);
10664 }
10665 sdtr_speed >>= 4;
10666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010667
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010668 /*
10669 * Set the host maximum queuing (max. 253, min. 16) and the per device
10670 * maximum queuing (max. 63, min. 4).
10671 */
10672 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10673 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10674 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10675 /* If the value is zero, assume it is uninitialized. */
10676 if (eep_config.max_host_qng == 0) {
10677 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10678 } else {
10679 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10680 }
10681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010683 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10684 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10685 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10686 /* If the value is zero, assume it is uninitialized. */
10687 if (eep_config.max_dvc_qng == 0) {
10688 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10689 } else {
10690 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10691 }
10692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010694 /*
10695 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10696 * set 'max_dvc_qng' to 'max_host_qng'.
10697 */
10698 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10699 eep_config.max_dvc_qng = eep_config.max_host_qng;
10700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010701
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010702 /*
10703 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10704 * values based on possibly adjusted EEPROM values.
10705 */
10706 asc_dvc->max_host_qng = eep_config.max_host_qng;
10707 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010708
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010709 /*
10710 * If the EEPROM 'termination' field is set to automatic (0), then set
10711 * the ADV_DVC_CFG 'termination' field to automatic also.
10712 *
10713 * If the termination is specified with a non-zero 'termination'
10714 * value check that a legal value is set and set the ADV_DVC_CFG
10715 * 'termination' field appropriately.
10716 */
10717 if (eep_config.termination_se == 0) {
10718 termination = 0; /* auto termination for SE */
10719 } else {
10720 /* Enable manual control with low off / high off. */
10721 if (eep_config.termination_se == 1) {
10722 termination = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010724 /* Enable manual control with low off / high on. */
10725 } else if (eep_config.termination_se == 2) {
10726 termination = TERM_SE_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010727
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010728 /* Enable manual control with low on / high on. */
10729 } else if (eep_config.termination_se == 3) {
10730 termination = TERM_SE;
10731 } else {
10732 /*
10733 * The EEPROM 'termination_se' field contains a bad value.
10734 * Use automatic termination instead.
10735 */
10736 termination = 0;
10737 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10738 }
10739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010740
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010741 if (eep_config.termination_lvd == 0) {
10742 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10743 } else {
10744 /* Enable manual control with low off / high off. */
10745 if (eep_config.termination_lvd == 1) {
10746 asc_dvc->cfg->termination = termination;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010747
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010748 /* Enable manual control with low off / high on. */
10749 } else if (eep_config.termination_lvd == 2) {
10750 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010751
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010752 /* Enable manual control with low on / high on. */
10753 } else if (eep_config.termination_lvd == 3) {
10754 asc_dvc->cfg->termination = termination | TERM_LVD;
10755 } else {
10756 /*
10757 * The EEPROM 'termination_lvd' field contains a bad value.
10758 * Use automatic termination instead.
10759 */
10760 asc_dvc->cfg->termination = termination;
10761 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10762 }
10763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010765 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010766}
10767
10768/*
10769 * Read the board's EEPROM configuration. Set fields in ASC_DVC_VAR and
10770 * ASC_DVC_CFG based on the EEPROM settings. The chip is stopped while
10771 * all of this is done.
10772 *
10773 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
10774 *
10775 * For a non-fatal error return a warning code. If there are no warnings
10776 * then 0 is returned.
10777 *
10778 * Note: Chip is stopped on entry.
10779 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010780static int AdvInitFrom38C1600EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010781{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010782 AdvPortAddr iop_base;
10783 ushort warn_code;
10784 ADVEEP_38C1600_CONFIG eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010785 uchar tid, termination;
10786 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010787
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010788 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010789
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010790 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010791
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010792 /*
10793 * Read the board's EEPROM configuration.
10794 *
10795 * Set default values if a bad checksum is found.
10796 */
10797 if (AdvGet38C1600EEPConfig(iop_base, &eep_config) !=
10798 eep_config.check_sum) {
Matthew Wilcox13ac2d92007-07-30 08:10:23 -060010799 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010800 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010801
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010802 /*
10803 * Set EEPROM default values.
10804 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010805 memcpy(&eep_config, &Default_38C1600_EEPROM_Config,
10806 sizeof(ADVEEP_38C1600_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010807
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010808 if (PCI_FUNC(pdev->devfn) != 0) {
10809 u8 ints;
10810 /*
10811 * Disable Bit 14 (BIOS_ENABLE) to fix SPARC Ultra 60
10812 * and old Mac system booting problem. The Expansion
10813 * ROM must be disabled in Function 1 for these systems
10814 */
10815 eep_config.cfg_lsw &= ~ADV_EEPROM_BIOS_ENABLE;
10816 /*
10817 * Clear the INTAB (bit 11) if the GPIO 0 input
10818 * indicates the Function 1 interrupt line is wired
10819 * to INTB.
10820 *
10821 * Set/Clear Bit 11 (INTAB) from the GPIO bit 0 input:
10822 * 1 - Function 1 interrupt line wired to INT A.
10823 * 0 - Function 1 interrupt line wired to INT B.
10824 *
10825 * Note: Function 0 is always wired to INTA.
10826 * Put all 5 GPIO bits in input mode and then read
10827 * their input values.
10828 */
10829 AdvWriteByteRegister(iop_base, IOPB_GPIO_CNTL, 0);
10830 ints = AdvReadByteRegister(iop_base, IOPB_GPIO_DATA);
10831 if ((ints & 0x01) == 0)
10832 eep_config.cfg_lsw &= ~ADV_EEPROM_INTAB;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010834
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010835 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010836 * Assume the 6 byte board serial number that was read from
10837 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010838 */
10839 eep_config.serial_number_word3 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010840 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010841 eep_config.serial_number_word2 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010842 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010843 eep_config.serial_number_word1 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010844 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010845
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010846 AdvSet38C1600EEPConfig(iop_base, &eep_config);
10847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010848
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010849 /*
10850 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10851 * EEPROM configuration that was read.
10852 *
10853 * This is the mapping of EEPROM fields to Adv Library fields.
10854 */
10855 asc_dvc->wdtr_able = eep_config.wdtr_able;
10856 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10857 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10858 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10859 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10860 asc_dvc->ppr_able = 0;
10861 asc_dvc->tagqng_able = eep_config.tagqng_able;
10862 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10863 asc_dvc->max_host_qng = eep_config.max_host_qng;
10864 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10865 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ASC_MAX_TID);
10866 asc_dvc->start_motor = eep_config.start_motor;
10867 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10868 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10869 asc_dvc->no_scam = eep_config.scam_tolerant;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010871 /*
10872 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10873 * are set, then set an 'sdtr_able' bit for it.
10874 */
10875 asc_dvc->sdtr_able = 0;
10876 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
10877 if (tid == 0) {
10878 sdtr_speed = asc_dvc->sdtr_speed1;
10879 } else if (tid == 4) {
10880 sdtr_speed = asc_dvc->sdtr_speed2;
10881 } else if (tid == 8) {
10882 sdtr_speed = asc_dvc->sdtr_speed3;
10883 } else if (tid == 12) {
10884 sdtr_speed = asc_dvc->sdtr_speed4;
10885 }
10886 if (sdtr_speed & ASC_MAX_TID) {
10887 asc_dvc->sdtr_able |= (1 << tid);
10888 }
10889 sdtr_speed >>= 4;
10890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010891
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010892 /*
10893 * Set the host maximum queuing (max. 253, min. 16) and the per device
10894 * maximum queuing (max. 63, min. 4).
10895 */
10896 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10897 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10898 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10899 /* If the value is zero, assume it is uninitialized. */
10900 if (eep_config.max_host_qng == 0) {
10901 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10902 } else {
10903 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10904 }
10905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010906
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010907 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10908 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10909 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10910 /* If the value is zero, assume it is uninitialized. */
10911 if (eep_config.max_dvc_qng == 0) {
10912 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10913 } else {
10914 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10915 }
10916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010917
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010918 /*
10919 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10920 * set 'max_dvc_qng' to 'max_host_qng'.
10921 */
10922 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10923 eep_config.max_dvc_qng = eep_config.max_host_qng;
10924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010926 /*
10927 * Set ASC_DVC_VAR 'max_host_qng' and ASC_DVC_VAR 'max_dvc_qng'
10928 * values based on possibly adjusted EEPROM values.
10929 */
10930 asc_dvc->max_host_qng = eep_config.max_host_qng;
10931 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010932
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010933 /*
10934 * If the EEPROM 'termination' field is set to automatic (0), then set
10935 * the ASC_DVC_CFG 'termination' field to automatic also.
10936 *
10937 * If the termination is specified with a non-zero 'termination'
10938 * value check that a legal value is set and set the ASC_DVC_CFG
10939 * 'termination' field appropriately.
10940 */
10941 if (eep_config.termination_se == 0) {
10942 termination = 0; /* auto termination for SE */
10943 } else {
10944 /* Enable manual control with low off / high off. */
10945 if (eep_config.termination_se == 1) {
10946 termination = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010947
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010948 /* Enable manual control with low off / high on. */
10949 } else if (eep_config.termination_se == 2) {
10950 termination = TERM_SE_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010951
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010952 /* Enable manual control with low on / high on. */
10953 } else if (eep_config.termination_se == 3) {
10954 termination = TERM_SE;
10955 } else {
10956 /*
10957 * The EEPROM 'termination_se' field contains a bad value.
10958 * Use automatic termination instead.
10959 */
10960 termination = 0;
10961 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10962 }
10963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010964
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010965 if (eep_config.termination_lvd == 0) {
10966 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10967 } else {
10968 /* Enable manual control with low off / high off. */
10969 if (eep_config.termination_lvd == 1) {
10970 asc_dvc->cfg->termination = termination;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010971
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010972 /* Enable manual control with low off / high on. */
10973 } else if (eep_config.termination_lvd == 2) {
10974 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010975
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010976 /* Enable manual control with low on / high on. */
10977 } else if (eep_config.termination_lvd == 3) {
10978 asc_dvc->cfg->termination = termination | TERM_LVD;
10979 } else {
10980 /*
10981 * The EEPROM 'termination_lvd' field contains a bad value.
10982 * Use automatic termination instead.
10983 */
10984 asc_dvc->cfg->termination = termination;
10985 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10986 }
10987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010988
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010989 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010990}
10991
10992/*
Matthew Wilcox51219352007-10-02 21:55:22 -040010993 * Initialize the ADV_DVC_VAR structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010994 *
Matthew Wilcox51219352007-10-02 21:55:22 -040010995 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010996 *
Matthew Wilcox51219352007-10-02 21:55:22 -040010997 * For a non-fatal error return a warning code. If there are no warnings
10998 * then 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010999 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011000static int AdvInitGetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011001{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011002 struct asc_board *board = shost_priv(shost);
11003 ADV_DVC_VAR *asc_dvc = &board->dvc_var.adv_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -040011004 unsigned short warn_code = 0;
11005 AdvPortAddr iop_base = asc_dvc->iop_base;
11006 u16 cmd;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011007 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011008
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011009 asc_dvc->err_code = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -040011010
11011 /*
11012 * Save the state of the PCI Configuration Command Register
11013 * "Parity Error Response Control" Bit. If the bit is clear (0),
11014 * in AdvInitAsc3550/38C0800Driver() tell the microcode to ignore
11015 * DMA parity errors.
11016 */
11017 asc_dvc->cfg->control_flag = 0;
11018 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
11019 if ((cmd & PCI_COMMAND_PARITY) == 0)
11020 asc_dvc->cfg->control_flag |= CONTROL_FLAG_IGNORE_PERR;
11021
Matthew Wilcox51219352007-10-02 21:55:22 -040011022 asc_dvc->cfg->chip_version =
11023 AdvGetChipVersion(iop_base, asc_dvc->bus_type);
11024
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011025 ASC_DBG(1, "iopb_chip_id_1: 0x%x 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -040011026 (ushort)AdvReadByteRegister(iop_base, IOPB_CHIP_ID_1),
11027 (ushort)ADV_CHIP_ID_BYTE);
11028
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011029 ASC_DBG(1, "iopw_chip_id_0: 0x%x 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -040011030 (ushort)AdvReadWordRegister(iop_base, IOPW_CHIP_ID_0),
11031 (ushort)ADV_CHIP_ID_WORD);
11032
11033 /*
11034 * Reset the chip to start and allow register writes.
11035 */
11036 if (AdvFindSignature(iop_base) == 0) {
11037 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
11038 return ADV_ERROR;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011039 } else {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011040 /*
Matthew Wilcox51219352007-10-02 21:55:22 -040011041 * The caller must set 'chip_type' to a valid setting.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011042 */
Matthew Wilcox51219352007-10-02 21:55:22 -040011043 if (asc_dvc->chip_type != ADV_CHIP_ASC3550 &&
11044 asc_dvc->chip_type != ADV_CHIP_ASC38C0800 &&
11045 asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
11046 asc_dvc->err_code |= ASC_IERR_BAD_CHIPTYPE;
11047 return ADV_ERROR;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011049
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011050 /*
Matthew Wilcox51219352007-10-02 21:55:22 -040011051 * Reset Chip.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011052 */
Matthew Wilcox51219352007-10-02 21:55:22 -040011053 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
11054 ADV_CTRL_REG_CMD_RESET);
11055 mdelay(100);
11056 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
11057 ADV_CTRL_REG_CMD_WR_IO_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058
Matthew Wilcox51219352007-10-02 21:55:22 -040011059 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
11060 status = AdvInitFrom38C1600EEP(asc_dvc);
11061 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
11062 status = AdvInitFrom38C0800EEP(asc_dvc);
11063 } else {
11064 status = AdvInitFrom3550EEP(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011065 }
Matthew Wilcox51219352007-10-02 21:55:22 -040011066 warn_code |= status;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011068
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011069 if (warn_code != 0)
11070 shost_printk(KERN_WARNING, shost, "warning: 0x%x\n", warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -040011071
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011072 if (asc_dvc->err_code)
11073 shost_printk(KERN_ERR, shost, "error code 0x%x\n",
11074 asc_dvc->err_code);
Matthew Wilcox51219352007-10-02 21:55:22 -040011075
11076 return asc_dvc->err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011077}
Matthew Wilcox51219352007-10-02 21:55:22 -040011078#endif
11079
11080static struct scsi_host_template advansys_template = {
11081 .proc_name = DRV_NAME,
11082#ifdef CONFIG_PROC_FS
Al Virob59fb6f2013-03-31 02:59:55 -040011083 .show_info = advansys_show_info,
Matthew Wilcox51219352007-10-02 21:55:22 -040011084#endif
11085 .name = DRV_NAME,
11086 .info = advansys_info,
11087 .queuecommand = advansys_queuecommand,
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +020011088 .eh_host_reset_handler = advansys_reset,
Matthew Wilcox51219352007-10-02 21:55:22 -040011089 .bios_param = advansys_biosparam,
11090 .slave_configure = advansys_slave_configure,
11091 /*
11092 * Because the driver may control an ISA adapter 'unchecked_isa_dma'
11093 * must be set. The flag will be cleared in advansys_board_found
11094 * for non-ISA adapters.
11095 */
11096 .unchecked_isa_dma = 1,
11097 /*
11098 * All adapters controlled by this driver are capable of large
11099 * scatter-gather lists. According to the mid-level SCSI documentation
11100 * this obviates any performance gain provided by setting
11101 * 'use_clustering'. But empirically while CPU utilization is increased
11102 * by enabling clustering, I/O throughput increases as well.
11103 */
11104 .use_clustering = ENABLE_CLUSTERING,
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011105 .use_blk_tags = 1,
Matthew Wilcox51219352007-10-02 21:55:22 -040011106};
Linus Torvalds1da177e2005-04-16 15:20:36 -070011107
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011108static int advansys_wide_init_chip(struct Scsi_Host *shost)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011109{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011110 struct asc_board *board = shost_priv(shost);
11111 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
Hannes Reinecke0ce53822015-04-24 13:18:25 +020011112 size_t sgblk_pool_size;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011113 int warn_code, err_code;
11114
11115 /*
11116 * Allocate buffer carrier structures. The total size
Hannes Reinecke98b96a72015-04-24 13:18:23 +020011117 * is about 8 KB, so allocate all at once.
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011118 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +020011119 adv_dvc->carrier = dma_alloc_coherent(board->dev,
11120 ADV_CARRIER_BUFSIZE, &adv_dvc->carrier_addr, GFP_KERNEL);
11121 ASC_DBG(1, "carrier 0x%p\n", adv_dvc->carrier);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011122
Hannes Reinecke98b96a72015-04-24 13:18:23 +020011123 if (!adv_dvc->carrier)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011124 goto kmalloc_failed;
11125
11126 /*
11127 * Allocate up to 'max_host_qng' request structures for the Wide
11128 * board. The total size is about 16 KB, so allocate all at once.
11129 * If the allocation fails decrement and try again.
11130 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +020011131 board->adv_reqp_size = adv_dvc->max_host_qng * sizeof(adv_req_t);
11132 if (board->adv_reqp_size & 0x1f) {
11133 ASC_DBG(1, "unaligned reqp %lu bytes\n", sizeof(adv_req_t));
11134 board->adv_reqp_size = ADV_32BALIGN(board->adv_reqp_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011135 }
Hannes Reinecke4b47e462015-04-24 13:18:24 +020011136 board->adv_reqp = dma_alloc_coherent(board->dev, board->adv_reqp_size,
11137 &board->adv_reqp_addr, GFP_KERNEL);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011138
Hannes Reinecke4b47e462015-04-24 13:18:24 +020011139 if (!board->adv_reqp)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011140 goto kmalloc_failed;
11141
Hannes Reinecke4b47e462015-04-24 13:18:24 +020011142 ASC_DBG(1, "reqp 0x%p, req_cnt %d, bytes %lu\n", board->adv_reqp,
11143 adv_dvc->max_host_qng, board->adv_reqp_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011144
11145 /*
11146 * Allocate up to ADV_TOT_SG_BLOCK request structures for
11147 * the Wide board. Each structure is about 136 bytes.
11148 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +020011149 sgblk_pool_size = sizeof(adv_sgblk_t) * ADV_TOT_SG_BLOCK;
11150 board->adv_sgblk_pool = dma_pool_create("adv_sgblk", board->dev,
11151 sgblk_pool_size, 32, 0);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011152
Hannes Reinecke0ce53822015-04-24 13:18:25 +020011153 ASC_DBG(1, "sg_cnt %d * %lu = %lu bytes\n", ADV_TOT_SG_BLOCK,
11154 sizeof(adv_sgblk_t), sgblk_pool_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011155
Hannes Reinecke0ce53822015-04-24 13:18:25 +020011156 if (!board->adv_sgblk_pool)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011157 goto kmalloc_failed;
11158
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011159 if (adv_dvc->chip_type == ADV_CHIP_ASC3550) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011160 ASC_DBG(2, "AdvInitAsc3550Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011161 warn_code = AdvInitAsc3550Driver(adv_dvc);
11162 } else if (adv_dvc->chip_type == ADV_CHIP_ASC38C0800) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011163 ASC_DBG(2, "AdvInitAsc38C0800Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011164 warn_code = AdvInitAsc38C0800Driver(adv_dvc);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011165 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011166 ASC_DBG(2, "AdvInitAsc38C1600Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011167 warn_code = AdvInitAsc38C1600Driver(adv_dvc);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011168 }
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011169 err_code = adv_dvc->err_code;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011170
11171 if (warn_code || err_code) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011172 shost_printk(KERN_WARNING, shost, "error: warn 0x%x, error "
11173 "0x%x\n", warn_code, err_code);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011174 }
11175
11176 goto exit;
11177
11178 kmalloc_failed:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011179 shost_printk(KERN_ERR, shost, "error: kmalloc() failed\n");
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011180 err_code = ADV_ERROR;
11181 exit:
11182 return err_code;
11183}
11184
Matthew Wilcox98d41c22007-10-02 21:55:37 -040011185static void advansys_wide_free_mem(struct asc_board *board)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011186{
Matthew Wilcox98d41c22007-10-02 21:55:37 -040011187 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
Hannes Reinecke98b96a72015-04-24 13:18:23 +020011188
11189 if (adv_dvc->carrier) {
11190 dma_free_coherent(board->dev, ADV_CARRIER_BUFSIZE,
11191 adv_dvc->carrier, adv_dvc->carrier_addr);
11192 adv_dvc->carrier = NULL;
11193 }
Hannes Reinecke4b47e462015-04-24 13:18:24 +020011194 if (board->adv_reqp) {
11195 dma_free_coherent(board->dev, board->adv_reqp_size,
11196 board->adv_reqp, board->adv_reqp_addr);
11197 board->adv_reqp = NULL;
11198 }
Hannes Reinecke0ce53822015-04-24 13:18:25 +020011199 if (board->adv_sgblk_pool) {
11200 dma_pool_destroy(board->adv_sgblk_pool);
11201 board->adv_sgblk_pool = NULL;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011202 }
11203}
11204
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011205static int advansys_board_found(struct Scsi_Host *shost, unsigned int iop,
11206 int bus_type)
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011207{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011208 struct pci_dev *pdev;
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011209 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011210 ASC_DVC_VAR *asc_dvc_varp = NULL;
11211 ADV_DVC_VAR *adv_dvc_varp = NULL;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011212 int share_irq, warn_code, ret;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011213
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011214 pdev = (bus_type == ASC_IS_PCI) ? to_pci_dev(boardp->dev) : NULL;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011215
11216 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011217 ASC_DBG(1, "narrow board\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011218 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
11219 asc_dvc_varp->bus_type = bus_type;
11220 asc_dvc_varp->drv_ptr = boardp;
11221 asc_dvc_varp->cfg = &boardp->dvc_cfg.asc_dvc_cfg;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011222 asc_dvc_varp->iop_base = iop;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011223 } else {
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040011224#ifdef CONFIG_PCI
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011225 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
11226 adv_dvc_varp->drv_ptr = boardp;
11227 adv_dvc_varp->cfg = &boardp->dvc_cfg.adv_dvc_cfg;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011228 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011229 ASC_DBG(1, "wide board ASC-3550\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011230 adv_dvc_varp->chip_type = ADV_CHIP_ASC3550;
11231 } else if (pdev->device == PCI_DEVICE_ID_38C0800_REV1) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011232 ASC_DBG(1, "wide board ASC-38C0800\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011233 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C0800;
11234 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011235 ASC_DBG(1, "wide board ASC-38C1600\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011236 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C1600;
11237 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011238
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040011239 boardp->asc_n_io_port = pci_resource_len(pdev, 1);
Arjan van de Ven25729a72008-09-28 16:18:02 -070011240 boardp->ioremap_addr = pci_ioremap_bar(pdev, 1);
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040011241 if (!boardp->ioremap_addr) {
Matthew Wilcox9d511a42007-10-02 21:55:42 -040011242 shost_printk(KERN_ERR, shost, "ioremap(%lx, %d) "
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011243 "returned NULL\n",
Matthew Wilcox9d511a42007-10-02 21:55:42 -040011244 (long)pci_resource_start(pdev, 1),
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011245 boardp->asc_n_io_port);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011246 ret = -ENODEV;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011247 goto err_shost;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011248 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011249 adv_dvc_varp->iop_base = (AdvPortAddr)boardp->ioremap_addr;
11250 ASC_DBG(1, "iop_base: 0x%p\n", adv_dvc_varp->iop_base);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011251
11252 /*
11253 * Even though it isn't used to access wide boards, other
11254 * than for the debug line below, save I/O Port address so
11255 * that it can be reported.
11256 */
11257 boardp->ioport = iop;
11258
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011259 ASC_DBG(1, "iopb_chip_id_1 0x%x, iopw_chip_id_0 0x%x\n",
11260 (ushort)inp(iop + 1), (ushort)inpw(iop));
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040011261#endif /* CONFIG_PCI */
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011262 }
11263
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011264 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011265 /*
11266 * Set the board bus type and PCI IRQ before
11267 * calling AscInitGetConfig().
11268 */
11269 switch (asc_dvc_varp->bus_type) {
11270#ifdef CONFIG_ISA
11271 case ASC_IS_ISA:
11272 shost->unchecked_isa_dma = TRUE;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011273 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011274 break;
11275 case ASC_IS_VL:
11276 shost->unchecked_isa_dma = FALSE;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011277 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011278 break;
11279 case ASC_IS_EISA:
11280 shost->unchecked_isa_dma = FALSE;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011281 share_irq = IRQF_SHARED;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011282 break;
11283#endif /* CONFIG_ISA */
11284#ifdef CONFIG_PCI
11285 case ASC_IS_PCI:
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011286 shost->unchecked_isa_dma = FALSE;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011287 share_irq = IRQF_SHARED;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011288 break;
11289#endif /* CONFIG_PCI */
11290 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011291 shost_printk(KERN_ERR, shost, "unknown adapter type: "
11292 "%d\n", asc_dvc_varp->bus_type);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011293 shost->unchecked_isa_dma = TRUE;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011294 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011295 break;
11296 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011297
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011298 /*
11299 * NOTE: AscInitGetConfig() may change the board's
11300 * bus_type value. The bus_type value should no
11301 * longer be used. If the bus_type field must be
11302 * referenced only use the bit-wise AND operator "&".
11303 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011304 ASC_DBG(2, "AscInitGetConfig()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011305 ret = AscInitGetConfig(shost) ? -ENODEV : 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011306 } else {
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011307#ifdef CONFIG_PCI
11308 /*
11309 * For Wide boards set PCI information before calling
11310 * AdvInitGetConfig().
11311 */
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011312 shost->unchecked_isa_dma = FALSE;
11313 share_irq = IRQF_SHARED;
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011314 ASC_DBG(2, "AdvInitGetConfig()\n");
Matthew Wilcox394dbf32007-07-26 11:56:40 -040011315
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011316 ret = AdvInitGetConfig(pdev, shost) ? -ENODEV : 0;
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011317#endif /* CONFIG_PCI */
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011318 }
11319
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011320 if (ret)
Al Virob59fb6f2013-03-31 02:59:55 -040011321 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011322
11323 /*
11324 * Save the EEPROM configuration so that it can be displayed
11325 * from /proc/scsi/advansys/[0...].
11326 */
11327 if (ASC_NARROW_BOARD(boardp)) {
11328
11329 ASCEEP_CONFIG *ep;
11330
11331 /*
11332 * Set the adapter's target id bit in the 'init_tidmask' field.
11333 */
11334 boardp->init_tidmask |=
11335 ADV_TID_TO_TIDMASK(asc_dvc_varp->cfg->chip_scsi_id);
11336
11337 /*
11338 * Save EEPROM settings for the board.
11339 */
11340 ep = &boardp->eep_config.asc_eep;
11341
11342 ep->init_sdtr = asc_dvc_varp->cfg->sdtr_enable;
11343 ep->disc_enable = asc_dvc_varp->cfg->disc_enable;
11344 ep->use_cmd_qng = asc_dvc_varp->cfg->cmd_qng_enabled;
11345 ASC_EEP_SET_DMA_SPD(ep, asc_dvc_varp->cfg->isa_dma_speed);
11346 ep->start_motor = asc_dvc_varp->start_motor;
11347 ep->cntl = asc_dvc_varp->dvc_cntl;
11348 ep->no_scam = asc_dvc_varp->no_scam;
11349 ep->max_total_qng = asc_dvc_varp->max_total_qng;
11350 ASC_EEP_SET_CHIP_ID(ep, asc_dvc_varp->cfg->chip_scsi_id);
11351 /* 'max_tag_qng' is set to the same value for every device. */
11352 ep->max_tag_qng = asc_dvc_varp->cfg->max_tag_qng[0];
11353 ep->adapter_info[0] = asc_dvc_varp->cfg->adapter_info[0];
11354 ep->adapter_info[1] = asc_dvc_varp->cfg->adapter_info[1];
11355 ep->adapter_info[2] = asc_dvc_varp->cfg->adapter_info[2];
11356 ep->adapter_info[3] = asc_dvc_varp->cfg->adapter_info[3];
11357 ep->adapter_info[4] = asc_dvc_varp->cfg->adapter_info[4];
11358 ep->adapter_info[5] = asc_dvc_varp->cfg->adapter_info[5];
11359
11360 /*
11361 * Modify board configuration.
11362 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011363 ASC_DBG(2, "AscInitSetConfig()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011364 ret = AscInitSetConfig(pdev, shost) ? -ENODEV : 0;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011365 if (ret)
Al Virob59fb6f2013-03-31 02:59:55 -040011366 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011367 } else {
11368 ADVEEP_3550_CONFIG *ep_3550;
11369 ADVEEP_38C0800_CONFIG *ep_38C0800;
11370 ADVEEP_38C1600_CONFIG *ep_38C1600;
11371
11372 /*
11373 * Save Wide EEP Configuration Information.
11374 */
11375 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
11376 ep_3550 = &boardp->eep_config.adv_3550_eep;
11377
11378 ep_3550->adapter_scsi_id = adv_dvc_varp->chip_scsi_id;
11379 ep_3550->max_host_qng = adv_dvc_varp->max_host_qng;
11380 ep_3550->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11381 ep_3550->termination = adv_dvc_varp->cfg->termination;
11382 ep_3550->disc_enable = adv_dvc_varp->cfg->disc_enable;
11383 ep_3550->bios_ctrl = adv_dvc_varp->bios_ctrl;
11384 ep_3550->wdtr_able = adv_dvc_varp->wdtr_able;
11385 ep_3550->sdtr_able = adv_dvc_varp->sdtr_able;
11386 ep_3550->ultra_able = adv_dvc_varp->ultra_able;
11387 ep_3550->tagqng_able = adv_dvc_varp->tagqng_able;
11388 ep_3550->start_motor = adv_dvc_varp->start_motor;
11389 ep_3550->scsi_reset_delay =
11390 adv_dvc_varp->scsi_reset_wait;
11391 ep_3550->serial_number_word1 =
11392 adv_dvc_varp->cfg->serial1;
11393 ep_3550->serial_number_word2 =
11394 adv_dvc_varp->cfg->serial2;
11395 ep_3550->serial_number_word3 =
11396 adv_dvc_varp->cfg->serial3;
11397 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
11398 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
11399
11400 ep_38C0800->adapter_scsi_id =
11401 adv_dvc_varp->chip_scsi_id;
11402 ep_38C0800->max_host_qng = adv_dvc_varp->max_host_qng;
11403 ep_38C0800->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11404 ep_38C0800->termination_lvd =
11405 adv_dvc_varp->cfg->termination;
11406 ep_38C0800->disc_enable =
11407 adv_dvc_varp->cfg->disc_enable;
11408 ep_38C0800->bios_ctrl = adv_dvc_varp->bios_ctrl;
11409 ep_38C0800->wdtr_able = adv_dvc_varp->wdtr_able;
11410 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11411 ep_38C0800->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11412 ep_38C0800->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11413 ep_38C0800->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11414 ep_38C0800->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11415 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11416 ep_38C0800->start_motor = adv_dvc_varp->start_motor;
11417 ep_38C0800->scsi_reset_delay =
11418 adv_dvc_varp->scsi_reset_wait;
11419 ep_38C0800->serial_number_word1 =
11420 adv_dvc_varp->cfg->serial1;
11421 ep_38C0800->serial_number_word2 =
11422 adv_dvc_varp->cfg->serial2;
11423 ep_38C0800->serial_number_word3 =
11424 adv_dvc_varp->cfg->serial3;
11425 } else {
11426 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
11427
11428 ep_38C1600->adapter_scsi_id =
11429 adv_dvc_varp->chip_scsi_id;
11430 ep_38C1600->max_host_qng = adv_dvc_varp->max_host_qng;
11431 ep_38C1600->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11432 ep_38C1600->termination_lvd =
11433 adv_dvc_varp->cfg->termination;
11434 ep_38C1600->disc_enable =
11435 adv_dvc_varp->cfg->disc_enable;
11436 ep_38C1600->bios_ctrl = adv_dvc_varp->bios_ctrl;
11437 ep_38C1600->wdtr_able = adv_dvc_varp->wdtr_able;
11438 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11439 ep_38C1600->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11440 ep_38C1600->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11441 ep_38C1600->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11442 ep_38C1600->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11443 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11444 ep_38C1600->start_motor = adv_dvc_varp->start_motor;
11445 ep_38C1600->scsi_reset_delay =
11446 adv_dvc_varp->scsi_reset_wait;
11447 ep_38C1600->serial_number_word1 =
11448 adv_dvc_varp->cfg->serial1;
11449 ep_38C1600->serial_number_word2 =
11450 adv_dvc_varp->cfg->serial2;
11451 ep_38C1600->serial_number_word3 =
11452 adv_dvc_varp->cfg->serial3;
11453 }
11454
11455 /*
11456 * Set the adapter's target id bit in the 'init_tidmask' field.
11457 */
11458 boardp->init_tidmask |=
11459 ADV_TID_TO_TIDMASK(adv_dvc_varp->chip_scsi_id);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011460 }
11461
11462 /*
11463 * Channels are numbered beginning with 0. For AdvanSys one host
11464 * structure supports one channel. Multi-channel boards have a
11465 * separate host structure for each channel.
11466 */
11467 shost->max_channel = 0;
11468 if (ASC_NARROW_BOARD(boardp)) {
11469 shost->max_id = ASC_MAX_TID + 1;
11470 shost->max_lun = ASC_MAX_LUN + 1;
Matthew Wilcoxf05ec592007-09-09 08:56:36 -060011471 shost->max_cmd_len = ASC_MAX_CDB_LEN;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011472
11473 shost->io_port = asc_dvc_varp->iop_base;
11474 boardp->asc_n_io_port = ASC_IOADR_GAP;
11475 shost->this_id = asc_dvc_varp->cfg->chip_scsi_id;
11476
11477 /* Set maximum number of queues the adapter can handle. */
11478 shost->can_queue = asc_dvc_varp->max_total_qng;
11479 } else {
11480 shost->max_id = ADV_MAX_TID + 1;
11481 shost->max_lun = ADV_MAX_LUN + 1;
Matthew Wilcoxf05ec592007-09-09 08:56:36 -060011482 shost->max_cmd_len = ADV_MAX_CDB_LEN;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011483
11484 /*
11485 * Save the I/O Port address and length even though
11486 * I/O ports are not used to access Wide boards.
11487 * Instead the Wide boards are accessed with
11488 * PCI Memory Mapped I/O.
11489 */
11490 shost->io_port = iop;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011491
11492 shost->this_id = adv_dvc_varp->chip_scsi_id;
11493
11494 /* Set maximum number of queues the adapter can handle. */
11495 shost->can_queue = adv_dvc_varp->max_host_qng;
11496 }
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011497 ret = scsi_init_shared_tag_map(shost, shost->can_queue);
11498 if (ret) {
11499 shost_printk(KERN_ERR, shost, "init tag map failed\n");
11500 goto err_free_dma;
11501 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011502
11503 /*
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011504 * Following v1.3.89, 'cmd_per_lun' is no longer needed
11505 * and should be set to zero.
11506 *
11507 * But because of a bug introduced in v1.3.89 if the driver is
11508 * compiled as a module and 'cmd_per_lun' is zero, the Mid-Level
11509 * SCSI function 'allocate_device' will panic. To allow the driver
11510 * to work as a module in these kernels set 'cmd_per_lun' to 1.
11511 *
11512 * Note: This is wrong. cmd_per_lun should be set to the depth
11513 * you want on untagged devices always.
11514 #ifdef MODULE
11515 */
11516 shost->cmd_per_lun = 1;
11517/* #else
11518 shost->cmd_per_lun = 0;
11519#endif */
11520
11521 /*
11522 * Set the maximum number of scatter-gather elements the
11523 * adapter can handle.
11524 */
11525 if (ASC_NARROW_BOARD(boardp)) {
11526 /*
11527 * Allow two commands with 'sg_tablesize' scatter-gather
11528 * elements to be executed simultaneously. This value is
11529 * the theoretical hardware limit. It may be decreased
11530 * below.
11531 */
11532 shost->sg_tablesize =
11533 (((asc_dvc_varp->max_total_qng - 2) / 2) *
11534 ASC_SG_LIST_PER_Q) + 1;
11535 } else {
11536 shost->sg_tablesize = ADV_MAX_SG_LIST;
11537 }
11538
11539 /*
11540 * The value of 'sg_tablesize' can not exceed the SCSI
11541 * mid-level driver definition of SG_ALL. SG_ALL also
11542 * must not be exceeded, because it is used to define the
11543 * size of the scatter-gather table in 'struct asc_sg_head'.
11544 */
11545 if (shost->sg_tablesize > SG_ALL) {
11546 shost->sg_tablesize = SG_ALL;
11547 }
11548
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011549 ASC_DBG(1, "sg_tablesize: %d\n", shost->sg_tablesize);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011550
11551 /* BIOS start address. */
11552 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011553 shost->base = AscGetChipBiosAddress(asc_dvc_varp->iop_base,
11554 asc_dvc_varp->bus_type);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011555 } else {
11556 /*
11557 * Fill-in BIOS board variables. The Wide BIOS saves
11558 * information in LRAM that is used by the driver.
11559 */
11560 AdvReadWordLram(adv_dvc_varp->iop_base,
11561 BIOS_SIGNATURE, boardp->bios_signature);
11562 AdvReadWordLram(adv_dvc_varp->iop_base,
11563 BIOS_VERSION, boardp->bios_version);
11564 AdvReadWordLram(adv_dvc_varp->iop_base,
11565 BIOS_CODESEG, boardp->bios_codeseg);
11566 AdvReadWordLram(adv_dvc_varp->iop_base,
11567 BIOS_CODELEN, boardp->bios_codelen);
11568
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011569 ASC_DBG(1, "bios_signature 0x%x, bios_version 0x%x\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011570 boardp->bios_signature, boardp->bios_version);
11571
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011572 ASC_DBG(1, "bios_codeseg 0x%x, bios_codelen 0x%x\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011573 boardp->bios_codeseg, boardp->bios_codelen);
11574
11575 /*
11576 * If the BIOS saved a valid signature, then fill in
11577 * the BIOS code segment base address.
11578 */
11579 if (boardp->bios_signature == 0x55AA) {
11580 /*
11581 * Convert x86 realmode code segment to a linear
11582 * address by shifting left 4.
11583 */
11584 shost->base = ((ulong)boardp->bios_codeseg << 4);
11585 } else {
11586 shost->base = 0;
11587 }
11588 }
11589
11590 /*
11591 * Register Board Resources - I/O Port, DMA, IRQ
11592 */
11593
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011594 /* Register DMA Channel for Narrow boards. */
11595 shost->dma_channel = NO_ISA_DMA; /* Default to no ISA DMA. */
11596#ifdef CONFIG_ISA
11597 if (ASC_NARROW_BOARD(boardp)) {
11598 /* Register DMA channel for ISA bus. */
11599 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
11600 shost->dma_channel = asc_dvc_varp->cfg->isa_dma_channel;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011601 ret = request_dma(shost->dma_channel, DRV_NAME);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011602 if (ret) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011603 shost_printk(KERN_ERR, shost, "request_dma() "
11604 "%d failed %d\n",
11605 shost->dma_channel, ret);
Al Virob59fb6f2013-03-31 02:59:55 -040011606 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011607 }
11608 AscEnableIsaDma(shost->dma_channel);
11609 }
11610 }
11611#endif /* CONFIG_ISA */
11612
11613 /* Register IRQ Number. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011614 ASC_DBG(2, "request_irq(%d, %p)\n", boardp->irq, shost);
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011615
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011616 ret = request_irq(boardp->irq, advansys_interrupt, share_irq,
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011617 DRV_NAME, shost);
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011618
11619 if (ret) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011620 if (ret == -EBUSY) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011621 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11622 "already in use\n", boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011623 } else if (ret == -EINVAL) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011624 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11625 "not valid\n", boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011626 } else {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011627 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11628 "failed with %d\n", boardp->irq, ret);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011629 }
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011630 goto err_free_dma;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011631 }
11632
11633 /*
11634 * Initialize board RISC chip and enable interrupts.
11635 */
11636 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011637 ASC_DBG(2, "AscInitAsc1000Driver()\n");
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011638
11639 asc_dvc_varp->overrun_buf = kzalloc(ASC_OVERRUN_BSIZE, GFP_KERNEL);
11640 if (!asc_dvc_varp->overrun_buf) {
11641 ret = -ENOMEM;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011642 goto err_free_irq;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011643 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011644 warn_code = AscInitAsc1000Driver(asc_dvc_varp);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011645
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011646 if (warn_code || asc_dvc_varp->err_code) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011647 shost_printk(KERN_ERR, shost, "error: init_state 0x%x, "
11648 "warn 0x%x, error 0x%x\n",
11649 asc_dvc_varp->init_state, warn_code,
11650 asc_dvc_varp->err_code);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011651 if (!asc_dvc_varp->overrun_dma) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011652 ret = -ENODEV;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011653 goto err_free_mem;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011654 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011655 }
11656 } else {
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011657 if (advansys_wide_init_chip(shost)) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011658 ret = -ENODEV;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011659 goto err_free_mem;
11660 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011661 }
11662
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011663 ASC_DBG_PRT_SCSI_HOST(2, shost);
11664
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011665 ret = scsi_add_host(shost, boardp->dev);
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011666 if (ret)
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011667 goto err_free_mem;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011668
11669 scsi_scan_host(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011670 return 0;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011671
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011672 err_free_mem:
11673 if (ASC_NARROW_BOARD(boardp)) {
11674 if (asc_dvc_varp->overrun_dma)
11675 dma_unmap_single(boardp->dev, asc_dvc_varp->overrun_dma,
11676 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
11677 kfree(asc_dvc_varp->overrun_buf);
11678 } else
11679 advansys_wide_free_mem(boardp);
11680 err_free_irq:
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011681 free_irq(boardp->irq, shost);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011682 err_free_dma:
Al Viro30037812008-11-22 17:34:54 +000011683#ifdef CONFIG_ISA
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011684 if (shost->dma_channel != NO_ISA_DMA)
11685 free_dma(shost->dma_channel);
Al Viro30037812008-11-22 17:34:54 +000011686#endif
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011687 err_unmap:
11688 if (boardp->ioremap_addr)
11689 iounmap(boardp->ioremap_addr);
11690 err_shost:
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011691 return ret;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011692}
11693
11694/*
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011695 * advansys_release()
11696 *
11697 * Release resources allocated for a single AdvanSys adapter.
11698 */
11699static int advansys_release(struct Scsi_Host *shost)
11700{
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011701 struct asc_board *board = shost_priv(shost);
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011702 ASC_DBG(1, "begin\n");
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011703 scsi_remove_host(shost);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011704 free_irq(board->irq, shost);
Al Viro30037812008-11-22 17:34:54 +000011705#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011706 if (shost->dma_channel != NO_ISA_DMA) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011707 ASC_DBG(1, "free_dma()\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011708 free_dma(shost->dma_channel);
11709 }
Al Viro30037812008-11-22 17:34:54 +000011710#endif
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011711 if (ASC_NARROW_BOARD(board)) {
11712 dma_unmap_single(board->dev,
11713 board->dvc_var.asc_dvc_var.overrun_dma,
11714 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011715 kfree(board->dvc_var.asc_dvc_var.overrun_buf);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011716 } else {
11717 iounmap(board->ioremap_addr);
11718 advansys_wide_free_mem(board);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011719 }
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011720 scsi_host_put(shost);
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011721 ASC_DBG(1, "end\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011722 return 0;
11723}
11724
Matthew Wilcox95c9f162007-09-09 08:56:39 -060011725#define ASC_IOADR_TABLE_MAX_IX 11
11726
Randy Dunlap747d0162008-01-14 00:55:18 -080011727static PortAddr _asc_def_iop_base[ASC_IOADR_TABLE_MAX_IX] = {
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011728 0x100, 0x0110, 0x120, 0x0130, 0x140, 0x0150, 0x0190,
11729 0x0210, 0x0230, 0x0250, 0x0330
11730};
11731
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011732/*
11733 * The ISA IRQ number is found in bits 2 and 3 of the CfgLsw. It decodes as:
11734 * 00: 10
11735 * 01: 11
11736 * 10: 12
11737 * 11: 15
11738 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011739static unsigned int advansys_isa_irq_no(PortAddr iop_base)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011740{
11741 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11742 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x03) + 10;
11743 if (chip_irq == 13)
11744 chip_irq = 15;
11745 return chip_irq;
11746}
11747
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011748static int advansys_isa_probe(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011749{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011750 int err = -ENODEV;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011751 PortAddr iop_base = _asc_def_iop_base[id];
11752 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011753 struct asc_board *board;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011754
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011755 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011756 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011757 return -ENODEV;
11758 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011759 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011760 if (!AscFindSignature(iop_base))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011761 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011762 if (!(AscGetChipVersion(iop_base, ASC_IS_ISA) & ASC_CHIP_VER_ISA_BIT))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011763 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011764
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011765 err = -ENOMEM;
11766 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011767 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011768 goto release_region;
11769
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011770 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011771 board->irq = advansys_isa_irq_no(iop_base);
11772 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011773 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011774
11775 err = advansys_board_found(shost, iop_base, ASC_IS_ISA);
11776 if (err)
11777 goto free_host;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011778
11779 dev_set_drvdata(dev, shost);
11780 return 0;
11781
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011782 free_host:
11783 scsi_host_put(shost);
11784 release_region:
Matthew Wilcox71f36112007-07-30 08:04:53 -060011785 release_region(iop_base, ASC_IOADR_GAP);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011786 return err;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011787}
11788
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011789static int advansys_isa_remove(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011790{
Matthew Wilcox71f36112007-07-30 08:04:53 -060011791 int ioport = _asc_def_iop_base[id];
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011792 advansys_release(dev_get_drvdata(dev));
Matthew Wilcox71f36112007-07-30 08:04:53 -060011793 release_region(ioport, ASC_IOADR_GAP);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011794 return 0;
11795}
11796
11797static struct isa_driver advansys_isa_driver = {
11798 .probe = advansys_isa_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011799 .remove = advansys_isa_remove,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011800 .driver = {
11801 .owner = THIS_MODULE,
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011802 .name = DRV_NAME,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011803 },
11804};
11805
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011806/*
11807 * The VLB IRQ number is found in bits 2 to 4 of the CfgLsw. It decodes as:
11808 * 000: invalid
11809 * 001: 10
11810 * 010: 11
11811 * 011: 12
11812 * 100: invalid
11813 * 101: 14
11814 * 110: 15
11815 * 111: invalid
11816 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011817static unsigned int advansys_vlb_irq_no(PortAddr iop_base)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011818{
11819 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11820 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x07) + 9;
11821 if ((chip_irq < 10) || (chip_irq == 13) || (chip_irq > 15))
11822 return 0;
11823 return chip_irq;
11824}
11825
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011826static int advansys_vlb_probe(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011827{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011828 int err = -ENODEV;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011829 PortAddr iop_base = _asc_def_iop_base[id];
11830 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011831 struct asc_board *board;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011832
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011833 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011834 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011835 return -ENODEV;
11836 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011837 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011838 if (!AscFindSignature(iop_base))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011839 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011840 /*
11841 * I don't think this condition can actually happen, but the old
11842 * driver did it, and the chances of finding a VLB setup in 2007
11843 * to do testing with is slight to none.
11844 */
11845 if (AscGetChipVersion(iop_base, ASC_IS_VL) > ASC_CHIP_MAX_VER_VL)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011846 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011847
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011848 err = -ENOMEM;
11849 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011850 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011851 goto release_region;
11852
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011853 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011854 board->irq = advansys_vlb_irq_no(iop_base);
11855 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011856 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011857
11858 err = advansys_board_found(shost, iop_base, ASC_IS_VL);
11859 if (err)
11860 goto free_host;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011861
11862 dev_set_drvdata(dev, shost);
11863 return 0;
11864
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011865 free_host:
11866 scsi_host_put(shost);
11867 release_region:
Matthew Wilcox71f36112007-07-30 08:04:53 -060011868 release_region(iop_base, ASC_IOADR_GAP);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011869 return -ENODEV;
11870}
11871
11872static struct isa_driver advansys_vlb_driver = {
11873 .probe = advansys_vlb_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011874 .remove = advansys_isa_remove,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011875 .driver = {
11876 .owner = THIS_MODULE,
Matthew Wilcoxb8e5152b2007-09-09 08:56:26 -060011877 .name = "advansys_vlb",
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011878 },
11879};
11880
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011881static struct eisa_device_id advansys_eisa_table[] = {
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011882 { "ABP7401" },
11883 { "ABP7501" },
11884 { "" }
11885};
11886
11887MODULE_DEVICE_TABLE(eisa, advansys_eisa_table);
11888
11889/*
11890 * EISA is a little more tricky than PCI; each EISA device may have two
11891 * channels, and this driver is written to make each channel its own Scsi_Host
11892 */
11893struct eisa_scsi_data {
11894 struct Scsi_Host *host[2];
11895};
11896
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011897/*
11898 * The EISA IRQ number is found in bits 8 to 10 of the CfgLsw. It decodes as:
11899 * 000: 10
11900 * 001: 11
11901 * 010: 12
11902 * 011: invalid
11903 * 100: 14
11904 * 101: 15
11905 * 110: invalid
11906 * 111: invalid
11907 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011908static unsigned int advansys_eisa_irq_no(struct eisa_device *edev)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011909{
11910 unsigned short cfg_lsw = inw(edev->base_addr + 0xc86);
11911 unsigned int chip_irq = ((cfg_lsw >> 8) & 0x07) + 10;
11912 if ((chip_irq == 13) || (chip_irq > 15))
11913 return 0;
11914 return chip_irq;
11915}
11916
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011917static int advansys_eisa_probe(struct device *dev)
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011918{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011919 int i, ioport, irq = 0;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011920 int err;
11921 struct eisa_device *edev = to_eisa_device(dev);
11922 struct eisa_scsi_data *data;
11923
11924 err = -ENOMEM;
11925 data = kzalloc(sizeof(*data), GFP_KERNEL);
11926 if (!data)
11927 goto fail;
11928 ioport = edev->base_addr + 0xc30;
11929
11930 err = -ENODEV;
11931 for (i = 0; i < 2; i++, ioport += 0x20) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011932 struct asc_board *board;
11933 struct Scsi_Host *shost;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011934 if (!request_region(ioport, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcox71f36112007-07-30 08:04:53 -060011935 printk(KERN_WARNING "Region %x-%x busy\n", ioport,
11936 ioport + ASC_IOADR_GAP - 1);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011937 continue;
Matthew Wilcox71f36112007-07-30 08:04:53 -060011938 }
11939 if (!AscFindSignature(ioport)) {
11940 release_region(ioport, ASC_IOADR_GAP);
11941 continue;
11942 }
11943
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011944 /*
11945 * I don't know why we need to do this for EISA chips, but
11946 * not for any others. It looks to be equivalent to
11947 * AscGetChipCfgMsw, but I may have overlooked something,
11948 * so I'm not converting it until I get an EISA board to
11949 * test with.
11950 */
11951 inw(ioport + 4);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011952
11953 if (!irq)
11954 irq = advansys_eisa_irq_no(edev);
11955
11956 err = -ENOMEM;
11957 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11958 if (!shost)
11959 goto release_region;
11960
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011961 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011962 board->irq = irq;
11963 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011964 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011965
11966 err = advansys_board_found(shost, ioport, ASC_IS_EISA);
11967 if (!err) {
11968 data->host[i] = shost;
11969 continue;
Matthew Wilcox71f36112007-07-30 08:04:53 -060011970 }
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011971
11972 scsi_host_put(shost);
11973 release_region:
11974 release_region(ioport, ASC_IOADR_GAP);
11975 break;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011976 }
11977
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011978 if (err)
11979 goto free_data;
11980 dev_set_drvdata(dev, data);
11981 return 0;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011982
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011983 free_data:
11984 kfree(data->host[0]);
11985 kfree(data->host[1]);
11986 kfree(data);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011987 fail:
11988 return err;
11989}
11990
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011991static int advansys_eisa_remove(struct device *dev)
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011992{
11993 int i;
11994 struct eisa_scsi_data *data = dev_get_drvdata(dev);
11995
11996 for (i = 0; i < 2; i++) {
Matthew Wilcox71f36112007-07-30 08:04:53 -060011997 int ioport;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011998 struct Scsi_Host *shost = data->host[i];
11999 if (!shost)
12000 continue;
Matthew Wilcox71f36112007-07-30 08:04:53 -060012001 ioport = shost->io_port;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012002 advansys_release(shost);
Matthew Wilcox71f36112007-07-30 08:04:53 -060012003 release_region(ioport, ASC_IOADR_GAP);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012004 }
12005
12006 kfree(data);
12007 return 0;
12008}
12009
12010static struct eisa_driver advansys_eisa_driver = {
12011 .id_table = advansys_eisa_table,
12012 .driver = {
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060012013 .name = DRV_NAME,
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012014 .probe = advansys_eisa_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012015 .remove = advansys_eisa_remove,
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012016 }
12017};
12018
Dave Jones2672ea82006-08-02 17:11:49 -040012019/* PCI Devices supported by this driver */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012020static struct pci_device_id advansys_pci_tbl[] = {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040012021 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_1200A,
12022 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12023 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940,
12024 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12025 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940U,
12026 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12027 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940UW,
12028 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12029 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C0800_REV1,
12030 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12031 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C1600_REV1,
12032 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
12033 {}
Dave Jones2672ea82006-08-02 17:11:49 -040012034};
Matthew Wilcox27c868c2007-07-26 10:56:23 -040012035
Dave Jones2672ea82006-08-02 17:11:49 -040012036MODULE_DEVICE_TABLE(pci, advansys_pci_tbl);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012037
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012038static void advansys_set_latency(struct pci_dev *pdev)
Matthew Wilcox9649af32007-07-26 21:51:47 -060012039{
12040 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
12041 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
12042 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0);
12043 } else {
12044 u8 latency;
12045 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
12046 if (latency < 0x20)
12047 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
12048 }
12049}
12050
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012051static int advansys_pci_probe(struct pci_dev *pdev,
12052 const struct pci_device_id *ent)
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012053{
12054 int err, ioport;
12055 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012056 struct asc_board *board;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012057
12058 err = pci_enable_device(pdev);
12059 if (err)
12060 goto fail;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060012061 err = pci_request_regions(pdev, DRV_NAME);
Matthew Wilcox71f36112007-07-30 08:04:53 -060012062 if (err)
12063 goto disable_device;
Matthew Wilcox9649af32007-07-26 21:51:47 -060012064 pci_set_master(pdev);
12065 advansys_set_latency(pdev);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012066
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012067 err = -ENODEV;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012068 if (pci_resource_len(pdev, 0) == 0)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012069 goto release_region;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012070
12071 ioport = pci_resource_start(pdev, 0);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012072
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012073 err = -ENOMEM;
12074 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012075 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012076 goto release_region;
12077
Matthew Wilcoxd2411492007-10-02 21:55:31 -040012078 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012079 board->irq = pdev->irq;
12080 board->dev = &pdev->dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020012081 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012082
12083 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW ||
12084 pdev->device == PCI_DEVICE_ID_38C0800_REV1 ||
12085 pdev->device == PCI_DEVICE_ID_38C1600_REV1) {
12086 board->flags |= ASC_IS_WIDE_BOARD;
12087 }
12088
12089 err = advansys_board_found(shost, ioport, ASC_IS_PCI);
12090 if (err)
12091 goto free_host;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012092
12093 pci_set_drvdata(pdev, shost);
12094 return 0;
12095
Matthew Wilcoxd361db42007-10-02 21:55:29 -040012096 free_host:
12097 scsi_host_put(shost);
12098 release_region:
Matthew Wilcox71f36112007-07-30 08:04:53 -060012099 pci_release_regions(pdev);
12100 disable_device:
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012101 pci_disable_device(pdev);
12102 fail:
12103 return err;
12104}
12105
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012106static void advansys_pci_remove(struct pci_dev *pdev)
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012107{
12108 advansys_release(pci_get_drvdata(pdev));
Matthew Wilcox71f36112007-07-30 08:04:53 -060012109 pci_release_regions(pdev);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012110 pci_disable_device(pdev);
12111}
12112
12113static struct pci_driver advansys_pci_driver = {
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060012114 .name = DRV_NAME,
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012115 .id_table = advansys_pci_tbl,
12116 .probe = advansys_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012117 .remove = advansys_pci_remove,
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012118};
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040012119
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060012120static int __init advansys_init(void)
12121{
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060012122 int error;
12123
12124 error = isa_register_driver(&advansys_isa_driver,
12125 ASC_IOADR_TABLE_MAX_IX);
12126 if (error)
12127 goto fail;
12128
12129 error = isa_register_driver(&advansys_vlb_driver,
12130 ASC_IOADR_TABLE_MAX_IX);
12131 if (error)
12132 goto unregister_isa;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012133
12134 error = eisa_driver_register(&advansys_eisa_driver);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012135 if (error)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060012136 goto unregister_vlb;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060012137
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012138 error = pci_register_driver(&advansys_pci_driver);
12139 if (error)
12140 goto unregister_eisa;
12141
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060012142 return 0;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012143
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012144 unregister_eisa:
12145 eisa_driver_unregister(&advansys_eisa_driver);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060012146 unregister_vlb:
12147 isa_unregister_driver(&advansys_vlb_driver);
12148 unregister_isa:
12149 isa_unregister_driver(&advansys_isa_driver);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012150 fail:
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012151 return error;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060012152}
12153
12154static void __exit advansys_exit(void)
12155{
Matthew Wilcox78e77d82007-07-29 21:46:15 -060012156 pci_unregister_driver(&advansys_pci_driver);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060012157 eisa_driver_unregister(&advansys_eisa_driver);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060012158 isa_unregister_driver(&advansys_vlb_driver);
12159 isa_unregister_driver(&advansys_isa_driver);
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060012160}
12161
12162module_init(advansys_init);
12163module_exit(advansys_exit);
12164
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040012165MODULE_LICENSE("GPL");
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +053012166MODULE_FIRMWARE("advansys/mcode.bin");
12167MODULE_FIRMWARE("advansys/3550.bin");
12168MODULE_FIRMWARE("advansys/38C0800.bin");
12169MODULE_FIRMWARE("advansys/38C1600.bin");