blob: 15bb8d050457024ae7d962b52c56a2f5e6281bb2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define isodd_word(val) ((((uint)val) & (uint)0x0001) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Dave Jones2672ea82006-08-02 17:11:49 -040083#define PCI_VENDOR_ID_ASP 0x10cd
84#define PCI_DEVICE_ID_ASP_1200A 0x1100
85#define PCI_DEVICE_ID_ASP_ABP940 0x1200
86#define PCI_DEVICE_ID_ASP_ABP940U 0x1300
87#define PCI_DEVICE_ID_ASP_ABP940UW 0x2300
88#define PCI_DEVICE_ID_38C0800_REV1 0x2500
89#define PCI_DEVICE_ID_38C1600_REV1 0x2700
90
Matthew Wilcox9d511a42007-10-02 21:55:42 -040091#define PortAddr unsigned int /* port address size */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define inp(port) inb(port)
93#define outp(port, byte) outb((byte), (port))
94
95#define inpw(port) inw(port)
96#define outpw(port, word) outw((word), (port))
97
98#define ASC_MAX_SG_QUEUE 7
99#define ASC_MAX_SG_LIST 255
100
101#define ASC_CS_TYPE unsigned short
102
103#define ASC_IS_ISA (0x0001)
104#define ASC_IS_ISAPNP (0x0081)
105#define ASC_IS_EISA (0x0002)
106#define ASC_IS_PCI (0x0004)
107#define ASC_IS_PCI_ULTRA (0x0104)
108#define ASC_IS_PCMCIA (0x0008)
109#define ASC_IS_MCA (0x0020)
110#define ASC_IS_VL (0x0040)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define ASC_IS_WIDESCSI_16 (0x0100)
112#define ASC_IS_WIDESCSI_32 (0x0200)
113#define ASC_IS_BIG_ENDIAN (0x8000)
Matthew Wilcox95c9f162007-09-09 08:56:39 -0600114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define ASC_CHIP_MIN_VER_VL (0x01)
116#define ASC_CHIP_MAX_VER_VL (0x07)
117#define ASC_CHIP_MIN_VER_PCI (0x09)
118#define ASC_CHIP_MAX_VER_PCI (0x0F)
119#define ASC_CHIP_VER_PCI_BIT (0x08)
120#define ASC_CHIP_MIN_VER_ISA (0x11)
121#define ASC_CHIP_MIN_VER_ISA_PNP (0x21)
122#define ASC_CHIP_MAX_VER_ISA (0x27)
123#define ASC_CHIP_VER_ISA_BIT (0x30)
124#define ASC_CHIP_VER_ISAPNP_BIT (0x20)
125#define ASC_CHIP_VER_ASYN_BUG (0x21)
126#define ASC_CHIP_VER_PCI 0x08
127#define ASC_CHIP_VER_PCI_ULTRA_3150 (ASC_CHIP_VER_PCI | 0x02)
128#define ASC_CHIP_VER_PCI_ULTRA_3050 (ASC_CHIP_VER_PCI | 0x03)
129#define ASC_CHIP_MIN_VER_EISA (0x41)
130#define ASC_CHIP_MAX_VER_EISA (0x47)
131#define ASC_CHIP_VER_EISA_BIT (0x40)
132#define ASC_CHIP_LATEST_VER_EISA ((ASC_CHIP_MIN_VER_EISA - 1) + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define ASC_MAX_VL_DMA_COUNT (0x07FFFFFFL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define ASC_MAX_PCI_DMA_COUNT (0xFFFFFFFFL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define ASC_MAX_ISA_DMA_COUNT (0x00FFFFFFL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#define ASC_SCSI_ID_BITS 3
138#define ASC_SCSI_TIX_TYPE uchar
139#define ASC_ALL_DEVICE_BIT_SET 0xFF
140#define ASC_SCSI_BIT_ID_TYPE uchar
141#define ASC_MAX_TID 7
142#define ASC_MAX_LUN 7
143#define ASC_SCSI_WIDTH_BIT_SET 0xFF
144#define ASC_MAX_SENSE_LEN 32
145#define ASC_MIN_SENSE_LEN 14
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define ASC_SCSI_RESET_HOLD_TIME_US 60
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
Matthew Wilcoxf05ec592007-09-09 08:56:36 -0600149 * Narrow boards only support 12-byte commands, while wide boards
150 * extend to 16-byte commands.
151 */
152#define ASC_MAX_CDB_LEN 12
153#define ADV_MAX_CDB_LEN 16
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define MS_SDTR_LEN 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define MS_WDTR_LEN 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#define ASC_SG_LIST_PER_Q 7
159#define QS_FREE 0x00
160#define QS_READY 0x01
161#define QS_DISC1 0x02
162#define QS_DISC2 0x04
163#define QS_BUSY 0x08
164#define QS_ABORTED 0x40
165#define QS_DONE 0x80
166#define QC_NO_CALLBACK 0x01
167#define QC_SG_SWAP_QUEUE 0x02
168#define QC_SG_HEAD 0x04
169#define QC_DATA_IN 0x08
170#define QC_DATA_OUT 0x10
171#define QC_URGENT 0x20
172#define QC_MSG_OUT 0x40
173#define QC_REQ_SENSE 0x80
174#define QCSG_SG_XFER_LIST 0x02
175#define QCSG_SG_XFER_MORE 0x04
176#define QCSG_SG_XFER_END 0x08
177#define QD_IN_PROGRESS 0x00
178#define QD_NO_ERROR 0x01
179#define QD_ABORTED_BY_HOST 0x02
180#define QD_WITH_ERROR 0x04
181#define QD_INVALID_REQUEST 0x80
182#define QD_INVALID_HOST_NUM 0x81
183#define QD_INVALID_DEVICE 0x82
184#define QD_ERR_INTERNAL 0xFF
185#define QHSTA_NO_ERROR 0x00
186#define QHSTA_M_SEL_TIMEOUT 0x11
187#define QHSTA_M_DATA_OVER_RUN 0x12
188#define QHSTA_M_DATA_UNDER_RUN 0x12
189#define QHSTA_M_UNEXPECTED_BUS_FREE 0x13
190#define QHSTA_M_BAD_BUS_PHASE_SEQ 0x14
191#define QHSTA_D_QDONE_SG_LIST_CORRUPTED 0x21
192#define QHSTA_D_ASC_DVC_ERROR_CODE_SET 0x22
193#define QHSTA_D_HOST_ABORT_FAILED 0x23
194#define QHSTA_D_EXE_SCSI_Q_FAILED 0x24
195#define QHSTA_D_EXE_SCSI_Q_BUSY_TIMEOUT 0x25
196#define QHSTA_D_ASPI_NO_BUF_POOL 0x26
197#define QHSTA_M_WTM_TIMEOUT 0x41
198#define QHSTA_M_BAD_CMPL_STATUS_IN 0x42
199#define QHSTA_M_NO_AUTO_REQ_SENSE 0x43
200#define QHSTA_M_AUTO_REQ_SENSE_FAIL 0x44
201#define QHSTA_M_TARGET_STATUS_BUSY 0x45
202#define QHSTA_M_BAD_TAG_CODE 0x46
203#define QHSTA_M_BAD_QUEUE_FULL_OR_BUSY 0x47
204#define QHSTA_M_HUNG_REQ_SCSI_BUS_RESET 0x48
205#define QHSTA_D_LRAM_CMP_ERROR 0x81
206#define QHSTA_M_MICRO_CODE_ERROR_HALT 0xA1
207#define ASC_FLAG_SCSIQ_REQ 0x01
208#define ASC_FLAG_BIOS_SCSIQ_REQ 0x02
209#define ASC_FLAG_BIOS_ASYNC_IO 0x04
210#define ASC_FLAG_SRB_LINEAR_ADDR 0x08
211#define ASC_FLAG_WIN16 0x10
212#define ASC_FLAG_WIN32 0x20
213#define ASC_FLAG_ISA_OVER_16MB 0x40
214#define ASC_FLAG_DOS_VM_CALLBACK 0x80
215#define ASC_TAG_FLAG_EXTRA_BYTES 0x10
216#define ASC_TAG_FLAG_DISABLE_DISCONNECT 0x04
217#define ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX 0x08
218#define ASC_TAG_FLAG_DISABLE_CHK_COND_INT_HOST 0x40
219#define ASC_SCSIQ_CPY_BEG 4
220#define ASC_SCSIQ_SGHD_CPY_BEG 2
221#define ASC_SCSIQ_B_FWD 0
222#define ASC_SCSIQ_B_BWD 1
223#define ASC_SCSIQ_B_STATUS 2
224#define ASC_SCSIQ_B_QNO 3
225#define ASC_SCSIQ_B_CNTL 4
226#define ASC_SCSIQ_B_SG_QUEUE_CNT 5
227#define ASC_SCSIQ_D_DATA_ADDR 8
228#define ASC_SCSIQ_D_DATA_CNT 12
229#define ASC_SCSIQ_B_SENSE_LEN 20
230#define ASC_SCSIQ_DONE_INFO_BEG 22
231#define ASC_SCSIQ_D_SRBPTR 22
232#define ASC_SCSIQ_B_TARGET_IX 26
233#define ASC_SCSIQ_B_CDB_LEN 28
234#define ASC_SCSIQ_B_TAG_CODE 29
235#define ASC_SCSIQ_W_VM_ID 30
236#define ASC_SCSIQ_DONE_STATUS 32
237#define ASC_SCSIQ_HOST_STATUS 33
238#define ASC_SCSIQ_SCSI_STATUS 34
239#define ASC_SCSIQ_CDB_BEG 36
240#define ASC_SCSIQ_DW_REMAIN_XFER_ADDR 56
241#define ASC_SCSIQ_DW_REMAIN_XFER_CNT 60
242#define ASC_SCSIQ_B_FIRST_SG_WK_QP 48
243#define ASC_SCSIQ_B_SG_WK_QP 49
244#define ASC_SCSIQ_B_SG_WK_IX 50
245#define ASC_SCSIQ_W_ALT_DC1 52
246#define ASC_SCSIQ_B_LIST_CNT 6
247#define ASC_SCSIQ_B_CUR_LIST_CNT 7
248#define ASC_SGQ_B_SG_CNTL 4
249#define ASC_SGQ_B_SG_HEAD_QP 5
250#define ASC_SGQ_B_SG_LIST_CNT 6
251#define ASC_SGQ_B_SG_CUR_LIST_CNT 7
252#define ASC_SGQ_LIST_BEG 8
253#define ASC_DEF_SCSI1_QNG 4
254#define ASC_MAX_SCSI1_QNG 4
255#define ASC_DEF_SCSI2_QNG 16
256#define ASC_MAX_SCSI2_QNG 32
257#define ASC_TAG_CODE_MASK 0x23
258#define ASC_STOP_REQ_RISC_STOP 0x01
259#define ASC_STOP_ACK_RISC_STOP 0x03
260#define ASC_STOP_CLEAN_UP_BUSY_Q 0x10
261#define ASC_STOP_CLEAN_UP_DISC_Q 0x20
262#define ASC_STOP_HOST_REQ_RISC_HALT 0x40
263#define ASC_TIDLUN_TO_IX(tid, lun) (ASC_SCSI_TIX_TYPE)((tid) + ((lun)<<ASC_SCSI_ID_BITS))
264#define ASC_TID_TO_TARGET_ID(tid) (ASC_SCSI_BIT_ID_TYPE)(0x01 << (tid))
265#define ASC_TIX_TO_TARGET_ID(tix) (0x01 << ((tix) & ASC_MAX_TID))
266#define ASC_TIX_TO_TID(tix) ((tix) & ASC_MAX_TID)
267#define ASC_TID_TO_TIX(tid) ((tid) & ASC_MAX_TID)
268#define ASC_TIX_TO_LUN(tix) (((tix) >> ASC_SCSI_ID_BITS) & ASC_MAX_LUN)
269#define ASC_QNO_TO_QADDR(q_no) ((ASC_QADR_BEG)+((int)(q_no) << 6))
270
271typedef struct asc_scsiq_1 {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400272 uchar status;
273 uchar q_no;
274 uchar cntl;
275 uchar sg_queue_cnt;
276 uchar target_id;
277 uchar target_lun;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200278 __le32 data_addr;
279 __le32 data_cnt;
280 __le32 sense_addr;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400281 uchar sense_len;
282 uchar extra_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283} ASC_SCSIQ_1;
284
285typedef struct asc_scsiq_2 {
Hannes Reinecke9c17c622015-04-24 13:18:21 +0200286 u32 srb_tag;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400287 uchar target_ix;
288 uchar flag;
289 uchar cdb_len;
290 uchar tag_code;
291 ushort vm_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292} ASC_SCSIQ_2;
293
294typedef struct asc_scsiq_3 {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400295 uchar done_stat;
296 uchar host_stat;
297 uchar scsi_stat;
298 uchar scsi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299} ASC_SCSIQ_3;
300
301typedef struct asc_scsiq_4 {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400302 uchar cdb[ASC_MAX_CDB_LEN];
303 uchar y_first_sg_list_qp;
304 uchar y_working_sg_qp;
305 uchar y_working_sg_ix;
306 uchar y_res;
307 ushort x_req_count;
308 ushort x_reconnect_rtn;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200309 __le32 x_saved_data_addr;
310 __le32 x_saved_data_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311} ASC_SCSIQ_4;
312
313typedef struct asc_q_done_info {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400314 ASC_SCSIQ_2 d2;
315 ASC_SCSIQ_3 d3;
316 uchar q_status;
317 uchar q_no;
318 uchar cntl;
319 uchar sense_len;
320 uchar extra_bytes;
321 uchar res;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200322 u32 remain_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323} ASC_QDONE_INFO;
324
325typedef struct asc_sg_list {
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200326 __le32 addr;
327 __le32 bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328} ASC_SG_LIST;
329
330typedef struct asc_sg_head {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400331 ushort entry_cnt;
332 ushort queue_cnt;
333 ushort entry_to_copy;
334 ushort res;
Matthew Wilcox05848b62007-10-02 21:55:25 -0400335 ASC_SG_LIST sg_list[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336} ASC_SG_HEAD;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338typedef struct asc_scsi_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400339 ASC_SCSIQ_1 q1;
340 ASC_SCSIQ_2 q2;
341 uchar *cdbptr;
342 ASC_SG_HEAD *sg_head;
343 ushort remain_sg_entry_cnt;
344 ushort next_sg_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345} ASC_SCSI_Q;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347typedef struct asc_scsi_bios_req_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400348 ASC_SCSIQ_1 r1;
349 ASC_SCSIQ_2 r2;
350 uchar *cdbptr;
351 ASC_SG_HEAD *sg_head;
352 uchar *sense_ptr;
353 ASC_SCSIQ_3 r3;
354 uchar cdb[ASC_MAX_CDB_LEN];
355 uchar sense[ASC_MIN_SENSE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356} ASC_SCSI_BIOS_REQ_Q;
357
358typedef struct asc_risc_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400359 uchar fwd;
360 uchar bwd;
361 ASC_SCSIQ_1 i1;
362 ASC_SCSIQ_2 i2;
363 ASC_SCSIQ_3 i3;
364 ASC_SCSIQ_4 i4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365} ASC_RISC_Q;
366
367typedef struct asc_sg_list_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400368 uchar seq_no;
369 uchar q_no;
370 uchar cntl;
371 uchar sg_head_qp;
372 uchar sg_list_cnt;
373 uchar sg_cur_list_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374} ASC_SG_LIST_Q;
375
376typedef struct asc_risc_sg_list_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400377 uchar fwd;
378 uchar bwd;
379 ASC_SG_LIST_Q sg;
380 ASC_SG_LIST sg_list[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381} ASC_RISC_SG_LIST_Q;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#define ASCQ_ERR_Q_STATUS 0x0D
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#define ASCQ_ERR_CUR_QNG 0x17
385#define ASCQ_ERR_SG_Q_LINKS 0x18
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#define ASCQ_ERR_ISR_RE_ENTRY 0x1A
387#define ASCQ_ERR_CRITICAL_RE_ENTRY 0x1B
388#define ASCQ_ERR_ISR_ON_CRITICAL 0x1C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/*
391 * Warning code values are set in ASC_DVC_VAR 'warn_code'.
392 */
393#define ASC_WARN_NO_ERROR 0x0000
394#define ASC_WARN_IO_PORT_ROTATE 0x0001
395#define ASC_WARN_EEPROM_CHKSUM 0x0002
396#define ASC_WARN_IRQ_MODIFIED 0x0004
397#define ASC_WARN_AUTO_CONFIG 0x0008
398#define ASC_WARN_CMD_QNG_CONFLICT 0x0010
399#define ASC_WARN_EEPROM_RECOVER 0x0020
400#define ASC_WARN_CFG_MSW_RECOVER 0x0040
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/*
Matthew Wilcox720349a2007-10-02 21:55:30 -0400403 * Error code values are set in {ASC/ADV}_DVC_VAR 'err_code'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Matthew Wilcox720349a2007-10-02 21:55:30 -0400405#define ASC_IERR_NO_CARRIER 0x0001 /* No more carrier memory */
406#define ASC_IERR_MCODE_CHKSUM 0x0002 /* micro code check sum error */
407#define ASC_IERR_SET_PC_ADDR 0x0004
408#define ASC_IERR_START_STOP_CHIP 0x0008 /* start/stop chip failed */
409#define ASC_IERR_ILLEGAL_CONNECTION 0x0010 /* Illegal cable connection */
410#define ASC_IERR_SINGLE_END_DEVICE 0x0020 /* SE device on DIFF bus */
411#define ASC_IERR_REVERSED_CABLE 0x0040 /* Narrow flat cable reversed */
412#define ASC_IERR_SET_SCSI_ID 0x0080 /* set SCSI ID failed */
413#define ASC_IERR_HVD_DEVICE 0x0100 /* HVD device on LVD port */
414#define ASC_IERR_BAD_SIGNATURE 0x0200 /* signature not found */
415#define ASC_IERR_NO_BUS_TYPE 0x0400
416#define ASC_IERR_BIST_PRE_TEST 0x0800 /* BIST pre-test error */
417#define ASC_IERR_BIST_RAM_TEST 0x1000 /* BIST RAM test error */
418#define ASC_IERR_BAD_CHIPTYPE 0x2000 /* Invalid chip_type setting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#define ASC_DEF_MAX_TOTAL_QNG (0xF0)
421#define ASC_MIN_TAG_Q_PER_DVC (0x04)
Matthew Wilcox95c9f162007-09-09 08:56:39 -0600422#define ASC_MIN_FREE_Q (0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#define ASC_MIN_TOTAL_QNG ((ASC_MAX_SG_QUEUE)+(ASC_MIN_FREE_Q))
424#define ASC_MAX_TOTAL_QNG 240
425#define ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG 16
426#define ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG 8
427#define ASC_MAX_PCI_INRAM_TOTAL_QNG 20
428#define ASC_MAX_INRAM_TAG_QNG 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#define ASC_IOADR_GAP 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#define ASC_SYN_MAX_OFFSET 0x0F
431#define ASC_DEF_SDTR_OFFSET 0x0F
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#define ASC_SDTR_ULTRA_PCI_10MB_INDEX 0x02
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -0400433#define ASYN_SDTR_DATA_FIX_PCI_REV_AB 0x41
434
435/* The narrow chip only supports a limited selection of transfer rates.
436 * These are encoded in the range 0..7 or 0..15 depending whether the chip
437 * is Ultra-capable or not. These tables let us convert from one to the other.
438 */
439static const unsigned char asc_syn_xfer_period[8] = {
440 25, 30, 35, 40, 50, 60, 70, 85
441};
442
443static const unsigned char asc_syn_ultra_xfer_period[16] = {
444 12, 19, 25, 32, 38, 44, 50, 57, 63, 69, 75, 82, 88, 94, 100, 107
445};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447typedef struct ext_msg {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400448 uchar msg_type;
449 uchar msg_len;
450 uchar msg_req;
451 union {
452 struct {
453 uchar sdtr_xfer_period;
454 uchar sdtr_req_ack_offset;
455 } sdtr;
456 struct {
457 uchar wdtr_width;
458 } wdtr;
459 struct {
460 uchar mdp_b3;
461 uchar mdp_b2;
462 uchar mdp_b1;
463 uchar mdp_b0;
464 } mdp;
465 } u_ext_msg;
466 uchar res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467} EXT_MSG;
468
469#define xfer_period u_ext_msg.sdtr.sdtr_xfer_period
470#define req_ack_offset u_ext_msg.sdtr.sdtr_req_ack_offset
471#define wdtr_width u_ext_msg.wdtr.wdtr_width
472#define mdp_b3 u_ext_msg.mdp_b3
473#define mdp_b2 u_ext_msg.mdp_b2
474#define mdp_b1 u_ext_msg.mdp_b1
475#define mdp_b0 u_ext_msg.mdp_b0
476
477typedef struct asc_dvc_cfg {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400478 ASC_SCSI_BIT_ID_TYPE can_tagged_qng;
479 ASC_SCSI_BIT_ID_TYPE cmd_qng_enabled;
480 ASC_SCSI_BIT_ID_TYPE disc_enable;
481 ASC_SCSI_BIT_ID_TYPE sdtr_enable;
482 uchar chip_scsi_id;
483 uchar isa_dma_speed;
484 uchar isa_dma_channel;
485 uchar chip_version;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400486 ushort mcode_date;
487 ushort mcode_version;
488 uchar max_tag_qng[ASC_MAX_TID + 1];
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400489 uchar sdtr_period_offset[ASC_MAX_TID + 1];
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400490 uchar adapter_info[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491} ASC_DVC_CFG;
492
493#define ASC_DEF_DVC_CNTL 0xFFFF
494#define ASC_DEF_CHIP_SCSI_ID 7
495#define ASC_DEF_ISA_DMA_SPEED 4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#define ASC_INIT_STATE_BEG_GET_CFG 0x0001
497#define ASC_INIT_STATE_END_GET_CFG 0x0002
498#define ASC_INIT_STATE_BEG_SET_CFG 0x0004
499#define ASC_INIT_STATE_END_SET_CFG 0x0008
500#define ASC_INIT_STATE_BEG_LOAD_MC 0x0010
501#define ASC_INIT_STATE_END_LOAD_MC 0x0020
502#define ASC_INIT_STATE_BEG_INQUIRY 0x0040
503#define ASC_INIT_STATE_END_INQUIRY 0x0080
504#define ASC_INIT_RESET_SCSI_DONE 0x0100
505#define ASC_INIT_STATE_WITHOUT_EEP 0x8000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#define ASC_BUG_FIX_IF_NOT_DWB 0x0001
507#define ASC_BUG_FIX_ASYN_USE_SYN 0x0002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508#define ASC_MIN_TAGGED_CMD 7
509#define ASC_MAX_SCSI_RESET_WAIT 30
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -0400510#define ASC_OVERRUN_BSIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400512struct asc_dvc_var; /* Forward Declaration. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514typedef struct asc_dvc_var {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400515 PortAddr iop_base;
516 ushort err_code;
517 ushort dvc_cntl;
518 ushort bug_fix_cntl;
519 ushort bus_type;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400520 ASC_SCSI_BIT_ID_TYPE init_sdtr;
521 ASC_SCSI_BIT_ID_TYPE sdtr_done;
522 ASC_SCSI_BIT_ID_TYPE use_tagged_qng;
523 ASC_SCSI_BIT_ID_TYPE unit_not_ready;
524 ASC_SCSI_BIT_ID_TYPE queue_full_or_busy;
525 ASC_SCSI_BIT_ID_TYPE start_motor;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +0900526 uchar *overrun_buf;
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -0400527 dma_addr_t overrun_dma;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400528 uchar scsi_reset_wait;
529 uchar chip_no;
Hannes Reineckeae267592015-04-24 13:18:28 +0200530 bool is_in_int;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400531 uchar max_total_qng;
532 uchar cur_total_qng;
533 uchar in_critical_cnt;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400534 uchar last_q_shortage;
535 ushort init_state;
536 uchar cur_dvc_qng[ASC_MAX_TID + 1];
537 uchar max_dvc_qng[ASC_MAX_TID + 1];
538 ASC_SCSI_Q *scsiq_busy_head[ASC_MAX_TID + 1];
539 ASC_SCSI_Q *scsiq_busy_tail[ASC_MAX_TID + 1];
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -0400540 const uchar *sdtr_period_tbl;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400541 ASC_DVC_CFG *cfg;
542 ASC_SCSI_BIT_ID_TYPE pci_fix_asyn_xfer_always;
543 char redo_scam;
544 ushort res2;
545 uchar dos_int13_table[ASC_MAX_TID + 1];
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200546 unsigned int max_dma_count;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400547 ASC_SCSI_BIT_ID_TYPE no_scam;
548 ASC_SCSI_BIT_ID_TYPE pci_fix_asyn_xfer;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -0400549 uchar min_sdtr_index;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400550 uchar max_sdtr_index;
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400551 struct asc_board *drv_ptr;
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200552 unsigned int uc_break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553} ASC_DVC_VAR;
554
555typedef struct asc_dvc_inq_info {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400556 uchar type[ASC_MAX_TID + 1][ASC_MAX_LUN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557} ASC_DVC_INQ_INFO;
558
559typedef struct asc_cap_info {
Hannes Reinecke95cfab62015-04-24 13:18:27 +0200560 u32 lba;
561 u32 blk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562} ASC_CAP_INFO;
563
564typedef struct asc_cap_info_array {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400565 ASC_CAP_INFO cap_info[ASC_MAX_TID + 1][ASC_MAX_LUN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566} ASC_CAP_INFO_ARRAY;
567
568#define ASC_MCNTL_NO_SEL_TIMEOUT (ushort)0x0001
569#define ASC_MCNTL_NULL_TARGET (ushort)0x0002
570#define ASC_CNTL_INITIATOR (ushort)0x0001
571#define ASC_CNTL_BIOS_GT_1GB (ushort)0x0002
572#define ASC_CNTL_BIOS_GT_2_DISK (ushort)0x0004
573#define ASC_CNTL_BIOS_REMOVABLE (ushort)0x0008
574#define ASC_CNTL_NO_SCAM (ushort)0x0010
575#define ASC_CNTL_INT_MULTI_Q (ushort)0x0080
576#define ASC_CNTL_NO_LUN_SUPPORT (ushort)0x0040
577#define ASC_CNTL_NO_VERIFY_COPY (ushort)0x0100
578#define ASC_CNTL_RESET_SCSI (ushort)0x0200
579#define ASC_CNTL_INIT_INQUIRY (ushort)0x0400
580#define ASC_CNTL_INIT_VERBOSE (ushort)0x0800
581#define ASC_CNTL_SCSI_PARITY (ushort)0x1000
582#define ASC_CNTL_BURST_MODE (ushort)0x2000
583#define ASC_CNTL_SDTR_ENABLE_ULTRA (ushort)0x4000
584#define ASC_EEP_DVC_CFG_BEG_VL 2
585#define ASC_EEP_MAX_DVC_ADDR_VL 15
586#define ASC_EEP_DVC_CFG_BEG 32
587#define ASC_EEP_MAX_DVC_ADDR 45
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#define ASC_EEP_MAX_RETRY 20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590/*
591 * These macros keep the chip SCSI id and ISA DMA speed
592 * bitfields in board order. C bitfields aren't portable
593 * between big and little-endian platforms so they are
594 * not used.
595 */
596
597#define ASC_EEP_GET_CHIP_ID(cfg) ((cfg)->id_speed & 0x0f)
598#define ASC_EEP_GET_DMA_SPD(cfg) (((cfg)->id_speed & 0xf0) >> 4)
599#define ASC_EEP_SET_CHIP_ID(cfg, sid) \
600 ((cfg)->id_speed = ((cfg)->id_speed & 0xf0) | ((sid) & ASC_MAX_TID))
601#define ASC_EEP_SET_DMA_SPD(cfg, spd) \
602 ((cfg)->id_speed = ((cfg)->id_speed & 0x0f) | ((spd) & 0x0f) << 4)
603
604typedef struct asceep_config {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400605 ushort cfg_lsw;
606 ushort cfg_msw;
607 uchar init_sdtr;
608 uchar disc_enable;
609 uchar use_cmd_qng;
610 uchar start_motor;
611 uchar max_total_qng;
612 uchar max_tag_qng;
613 uchar bios_scan;
614 uchar power_up_wait;
615 uchar no_scam;
616 uchar id_speed; /* low order 4 bits is chip scsi id */
617 /* high order 4 bits is isa dma speed */
618 uchar dos_int13_table[ASC_MAX_TID + 1];
619 uchar adapter_info[6];
620 ushort cntl;
621 ushort chksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622} ASCEEP_CONFIG;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624#define ASC_EEP_CMD_READ 0x80
625#define ASC_EEP_CMD_WRITE 0x40
626#define ASC_EEP_CMD_WRITE_ABLE 0x30
627#define ASC_EEP_CMD_WRITE_DISABLE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#define ASCV_MSGOUT_BEG 0x0000
629#define ASCV_MSGOUT_SDTR_PERIOD (ASCV_MSGOUT_BEG+3)
630#define ASCV_MSGOUT_SDTR_OFFSET (ASCV_MSGOUT_BEG+4)
631#define ASCV_BREAK_SAVED_CODE (ushort)0x0006
632#define ASCV_MSGIN_BEG (ASCV_MSGOUT_BEG+8)
633#define ASCV_MSGIN_SDTR_PERIOD (ASCV_MSGIN_BEG+3)
634#define ASCV_MSGIN_SDTR_OFFSET (ASCV_MSGIN_BEG+4)
635#define ASCV_SDTR_DATA_BEG (ASCV_MSGIN_BEG+8)
636#define ASCV_SDTR_DONE_BEG (ASCV_SDTR_DATA_BEG+8)
637#define ASCV_MAX_DVC_QNG_BEG (ushort)0x0020
638#define ASCV_BREAK_ADDR (ushort)0x0028
639#define ASCV_BREAK_NOTIFY_COUNT (ushort)0x002A
640#define ASCV_BREAK_CONTROL (ushort)0x002C
641#define ASCV_BREAK_HIT_COUNT (ushort)0x002E
642
643#define ASCV_ASCDVC_ERR_CODE_W (ushort)0x0030
644#define ASCV_MCODE_CHKSUM_W (ushort)0x0032
645#define ASCV_MCODE_SIZE_W (ushort)0x0034
646#define ASCV_STOP_CODE_B (ushort)0x0036
647#define ASCV_DVC_ERR_CODE_B (ushort)0x0037
648#define ASCV_OVERRUN_PADDR_D (ushort)0x0038
649#define ASCV_OVERRUN_BSIZE_D (ushort)0x003C
650#define ASCV_HALTCODE_W (ushort)0x0040
651#define ASCV_CHKSUM_W (ushort)0x0042
652#define ASCV_MC_DATE_W (ushort)0x0044
653#define ASCV_MC_VER_W (ushort)0x0046
654#define ASCV_NEXTRDY_B (ushort)0x0048
655#define ASCV_DONENEXT_B (ushort)0x0049
656#define ASCV_USE_TAGGED_QNG_B (ushort)0x004A
657#define ASCV_SCSIBUSY_B (ushort)0x004B
658#define ASCV_Q_DONE_IN_PROGRESS_B (ushort)0x004C
659#define ASCV_CURCDB_B (ushort)0x004D
660#define ASCV_RCLUN_B (ushort)0x004E
661#define ASCV_BUSY_QHEAD_B (ushort)0x004F
662#define ASCV_DISC1_QHEAD_B (ushort)0x0050
663#define ASCV_DISC_ENABLE_B (ushort)0x0052
664#define ASCV_CAN_TAGGED_QNG_B (ushort)0x0053
665#define ASCV_HOSTSCSI_ID_B (ushort)0x0055
666#define ASCV_MCODE_CNTL_B (ushort)0x0056
667#define ASCV_NULL_TARGET_B (ushort)0x0057
668#define ASCV_FREE_Q_HEAD_W (ushort)0x0058
669#define ASCV_DONE_Q_TAIL_W (ushort)0x005A
670#define ASCV_FREE_Q_HEAD_B (ushort)(ASCV_FREE_Q_HEAD_W+1)
671#define ASCV_DONE_Q_TAIL_B (ushort)(ASCV_DONE_Q_TAIL_W+1)
672#define ASCV_HOST_FLAG_B (ushort)0x005D
673#define ASCV_TOTAL_READY_Q_B (ushort)0x0064
674#define ASCV_VER_SERIAL_B (ushort)0x0065
675#define ASCV_HALTCODE_SAVED_W (ushort)0x0066
676#define ASCV_WTM_FLAG_B (ushort)0x0068
677#define ASCV_RISC_FLAG_B (ushort)0x006A
678#define ASCV_REQ_SG_LIST_QP (ushort)0x006B
679#define ASC_HOST_FLAG_IN_ISR 0x01
680#define ASC_HOST_FLAG_ACK_INT 0x02
681#define ASC_RISC_FLAG_GEN_INT 0x01
682#define ASC_RISC_FLAG_REQ_SG_LIST 0x02
683#define IOP_CTRL (0x0F)
684#define IOP_STATUS (0x0E)
685#define IOP_INT_ACK IOP_STATUS
686#define IOP_REG_IFC (0x0D)
687#define IOP_SYN_OFFSET (0x0B)
688#define IOP_EXTRA_CONTROL (0x0D)
689#define IOP_REG_PC (0x0C)
690#define IOP_RAM_ADDR (0x0A)
691#define IOP_RAM_DATA (0x08)
692#define IOP_EEP_DATA (0x06)
693#define IOP_EEP_CMD (0x07)
694#define IOP_VERSION (0x03)
695#define IOP_CONFIG_HIGH (0x04)
696#define IOP_CONFIG_LOW (0x02)
697#define IOP_SIG_BYTE (0x01)
698#define IOP_SIG_WORD (0x00)
699#define IOP_REG_DC1 (0x0E)
700#define IOP_REG_DC0 (0x0C)
701#define IOP_REG_SB (0x0B)
702#define IOP_REG_DA1 (0x0A)
703#define IOP_REG_DA0 (0x08)
704#define IOP_REG_SC (0x09)
705#define IOP_DMA_SPEED (0x07)
706#define IOP_REG_FLAG (0x07)
707#define IOP_FIFO_H (0x06)
708#define IOP_FIFO_L (0x04)
709#define IOP_REG_ID (0x05)
710#define IOP_REG_QP (0x03)
711#define IOP_REG_IH (0x02)
712#define IOP_REG_IX (0x01)
713#define IOP_REG_AX (0x00)
714#define IFC_REG_LOCK (0x00)
715#define IFC_REG_UNLOCK (0x09)
716#define IFC_WR_EN_FILTER (0x10)
717#define IFC_RD_NO_EEPROM (0x10)
718#define IFC_SLEW_RATE (0x20)
719#define IFC_ACT_NEG (0x40)
720#define IFC_INP_FILTER (0x80)
721#define IFC_INIT_DEFAULT (IFC_ACT_NEG | IFC_REG_UNLOCK)
722#define SC_SEL (uchar)(0x80)
723#define SC_BSY (uchar)(0x40)
724#define SC_ACK (uchar)(0x20)
725#define SC_REQ (uchar)(0x10)
726#define SC_ATN (uchar)(0x08)
727#define SC_IO (uchar)(0x04)
728#define SC_CD (uchar)(0x02)
729#define SC_MSG (uchar)(0x01)
730#define SEC_SCSI_CTL (uchar)(0x80)
731#define SEC_ACTIVE_NEGATE (uchar)(0x40)
732#define SEC_SLEW_RATE (uchar)(0x20)
733#define SEC_ENABLE_FILTER (uchar)(0x10)
734#define ASC_HALT_EXTMSG_IN (ushort)0x8000
735#define ASC_HALT_CHK_CONDITION (ushort)0x8100
736#define ASC_HALT_SS_QUEUE_FULL (ushort)0x8200
737#define ASC_HALT_DISABLE_ASYN_USE_SYN_FIX (ushort)0x8300
738#define ASC_HALT_ENABLE_ASYN_USE_SYN_FIX (ushort)0x8400
739#define ASC_HALT_SDTR_REJECTED (ushort)0x4000
740#define ASC_HALT_HOST_COPY_SG_LIST_TO_RISC ( ushort )0x2000
741#define ASC_MAX_QNO 0xF8
742#define ASC_DATA_SEC_BEG (ushort)0x0080
743#define ASC_DATA_SEC_END (ushort)0x0080
744#define ASC_CODE_SEC_BEG (ushort)0x0080
745#define ASC_CODE_SEC_END (ushort)0x0080
746#define ASC_QADR_BEG (0x4000)
747#define ASC_QADR_USED (ushort)(ASC_MAX_QNO * 64)
748#define ASC_QADR_END (ushort)0x7FFF
749#define ASC_QLAST_ADR (ushort)0x7FC0
750#define ASC_QBLK_SIZE 0x40
751#define ASC_BIOS_DATA_QBEG 0xF8
752#define ASC_MIN_ACTIVE_QNO 0x01
753#define ASC_QLINK_END 0xFF
754#define ASC_EEPROM_WORDS 0x10
755#define ASC_MAX_MGS_LEN 0x10
756#define ASC_BIOS_ADDR_DEF 0xDC00
757#define ASC_BIOS_SIZE 0x3800
758#define ASC_BIOS_RAM_OFF 0x3800
759#define ASC_BIOS_RAM_SIZE 0x800
760#define ASC_BIOS_MIN_ADDR 0xC000
761#define ASC_BIOS_MAX_ADDR 0xEC00
762#define ASC_BIOS_BANK_SIZE 0x0400
763#define ASC_MCODE_START_ADDR 0x0080
764#define ASC_CFG0_HOST_INT_ON 0x0020
765#define ASC_CFG0_BIOS_ON 0x0040
766#define ASC_CFG0_VERA_BURST_ON 0x0080
767#define ASC_CFG0_SCSI_PARITY_ON 0x0800
768#define ASC_CFG1_SCSI_TARGET_ON 0x0080
769#define ASC_CFG1_LRAM_8BITS_ON 0x0800
770#define ASC_CFG_MSW_CLR_MASK 0x3080
771#define CSW_TEST1 (ASC_CS_TYPE)0x8000
772#define CSW_AUTO_CONFIG (ASC_CS_TYPE)0x4000
773#define CSW_RESERVED1 (ASC_CS_TYPE)0x2000
774#define CSW_IRQ_WRITTEN (ASC_CS_TYPE)0x1000
775#define CSW_33MHZ_SELECTED (ASC_CS_TYPE)0x0800
776#define CSW_TEST2 (ASC_CS_TYPE)0x0400
777#define CSW_TEST3 (ASC_CS_TYPE)0x0200
778#define CSW_RESERVED2 (ASC_CS_TYPE)0x0100
779#define CSW_DMA_DONE (ASC_CS_TYPE)0x0080
780#define CSW_FIFO_RDY (ASC_CS_TYPE)0x0040
781#define CSW_EEP_READ_DONE (ASC_CS_TYPE)0x0020
782#define CSW_HALTED (ASC_CS_TYPE)0x0010
783#define CSW_SCSI_RESET_ACTIVE (ASC_CS_TYPE)0x0008
784#define CSW_PARITY_ERR (ASC_CS_TYPE)0x0004
785#define CSW_SCSI_RESET_LATCH (ASC_CS_TYPE)0x0002
786#define CSW_INT_PENDING (ASC_CS_TYPE)0x0001
787#define CIW_CLR_SCSI_RESET_INT (ASC_CS_TYPE)0x1000
788#define CIW_INT_ACK (ASC_CS_TYPE)0x0100
789#define CIW_TEST1 (ASC_CS_TYPE)0x0200
790#define CIW_TEST2 (ASC_CS_TYPE)0x0400
791#define CIW_SEL_33MHZ (ASC_CS_TYPE)0x0800
792#define CIW_IRQ_ACT (ASC_CS_TYPE)0x1000
793#define CC_CHIP_RESET (uchar)0x80
794#define CC_SCSI_RESET (uchar)0x40
795#define CC_HALT (uchar)0x20
796#define CC_SINGLE_STEP (uchar)0x10
797#define CC_DMA_ABLE (uchar)0x08
798#define CC_TEST (uchar)0x04
799#define CC_BANK_ONE (uchar)0x02
800#define CC_DIAG (uchar)0x01
801#define ASC_1000_ID0W 0x04C1
802#define ASC_1000_ID0W_FIX 0x00C1
803#define ASC_1000_ID1B 0x25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#define ASC_EISA_REV_IOP_MASK (0x0C83)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#define ASC_EISA_CFG_IOP_MASK (0x0C86)
806#define ASC_GET_EISA_SLOT(iop) (PortAddr)((iop) & 0xF000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807#define INS_HALTINT (ushort)0x6281
808#define INS_HALT (ushort)0x6280
809#define INS_SINT (ushort)0x6200
810#define INS_RFLAG_WTM (ushort)0x7380
811#define ASC_MC_SAVE_CODE_WSIZE 0x500
812#define ASC_MC_SAVE_DATA_WSIZE 0x40
813
814typedef struct asc_mc_saved {
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400815 ushort data[ASC_MC_SAVE_DATA_WSIZE];
816 ushort code[ASC_MC_SAVE_CODE_WSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817} ASC_MC_SAVED;
818
819#define AscGetQDoneInProgress(port) AscReadLramByte((port), ASCV_Q_DONE_IN_PROGRESS_B)
820#define AscPutQDoneInProgress(port, val) AscWriteLramByte((port), ASCV_Q_DONE_IN_PROGRESS_B, val)
821#define AscGetVarFreeQHead(port) AscReadLramWord((port), ASCV_FREE_Q_HEAD_W)
822#define AscGetVarDoneQTail(port) AscReadLramWord((port), ASCV_DONE_Q_TAIL_W)
823#define AscPutVarFreeQHead(port, val) AscWriteLramWord((port), ASCV_FREE_Q_HEAD_W, val)
824#define AscPutVarDoneQTail(port, val) AscWriteLramWord((port), ASCV_DONE_Q_TAIL_W, val)
825#define AscGetRiscVarFreeQHead(port) AscReadLramByte((port), ASCV_NEXTRDY_B)
826#define AscGetRiscVarDoneQTail(port) AscReadLramByte((port), ASCV_DONENEXT_B)
827#define AscPutRiscVarFreeQHead(port, val) AscWriteLramByte((port), ASCV_NEXTRDY_B, val)
828#define AscPutRiscVarDoneQTail(port, val) AscWriteLramByte((port), ASCV_DONENEXT_B, val)
Matthew Wilcox51219352007-10-02 21:55:22 -0400829#define AscPutMCodeSDTRDoneAtID(port, id, data) AscWriteLramByte((port), (ushort)((ushort)ASCV_SDTR_DONE_BEG+(ushort)id), (data))
830#define AscGetMCodeSDTRDoneAtID(port, id) AscReadLramByte((port), (ushort)((ushort)ASCV_SDTR_DONE_BEG+(ushort)id))
831#define AscPutMCodeInitSDTRAtID(port, id, data) AscWriteLramByte((port), (ushort)((ushort)ASCV_SDTR_DATA_BEG+(ushort)id), data)
832#define AscGetMCodeInitSDTRAtID(port, id) AscReadLramByte((port), (ushort)((ushort)ASCV_SDTR_DATA_BEG+(ushort)id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#define AscGetChipSignatureByte(port) (uchar)inp((port)+IOP_SIG_BYTE)
834#define AscGetChipSignatureWord(port) (ushort)inpw((port)+IOP_SIG_WORD)
835#define AscGetChipVerNo(port) (uchar)inp((port)+IOP_VERSION)
836#define AscGetChipCfgLsw(port) (ushort)inpw((port)+IOP_CONFIG_LOW)
837#define AscGetChipCfgMsw(port) (ushort)inpw((port)+IOP_CONFIG_HIGH)
838#define AscSetChipCfgLsw(port, data) outpw((port)+IOP_CONFIG_LOW, data)
839#define AscSetChipCfgMsw(port, data) outpw((port)+IOP_CONFIG_HIGH, data)
840#define AscGetChipEEPCmd(port) (uchar)inp((port)+IOP_EEP_CMD)
841#define AscSetChipEEPCmd(port, data) outp((port)+IOP_EEP_CMD, data)
842#define AscGetChipEEPData(port) (ushort)inpw((port)+IOP_EEP_DATA)
843#define AscSetChipEEPData(port, data) outpw((port)+IOP_EEP_DATA, data)
844#define AscGetChipLramAddr(port) (ushort)inpw((PortAddr)((port)+IOP_RAM_ADDR))
845#define AscSetChipLramAddr(port, addr) outpw((PortAddr)((port)+IOP_RAM_ADDR), addr)
846#define AscGetChipLramData(port) (ushort)inpw((port)+IOP_RAM_DATA)
847#define AscSetChipLramData(port, data) outpw((port)+IOP_RAM_DATA, data)
848#define AscGetChipIFC(port) (uchar)inp((port)+IOP_REG_IFC)
849#define AscSetChipIFC(port, data) outp((port)+IOP_REG_IFC, data)
850#define AscGetChipStatus(port) (ASC_CS_TYPE)inpw((port)+IOP_STATUS)
851#define AscSetChipStatus(port, cs_val) outpw((port)+IOP_STATUS, cs_val)
852#define AscGetChipControl(port) (uchar)inp((port)+IOP_CTRL)
853#define AscSetChipControl(port, cc_val) outp((port)+IOP_CTRL, cc_val)
854#define AscGetChipSyn(port) (uchar)inp((port)+IOP_SYN_OFFSET)
855#define AscSetChipSyn(port, data) outp((port)+IOP_SYN_OFFSET, data)
856#define AscSetPCAddr(port, data) outpw((port)+IOP_REG_PC, data)
857#define AscGetPCAddr(port) (ushort)inpw((port)+IOP_REG_PC)
858#define AscIsIntPending(port) (AscGetChipStatus(port) & (CSW_INT_PENDING | CSW_SCSI_RESET_LATCH))
859#define AscGetChipScsiID(port) ((AscGetChipCfgLsw(port) >> 8) & ASC_MAX_TID)
860#define AscGetExtraControl(port) (uchar)inp((port)+IOP_EXTRA_CONTROL)
861#define AscSetExtraControl(port, data) outp((port)+IOP_EXTRA_CONTROL, data)
862#define AscReadChipAX(port) (ushort)inpw((port)+IOP_REG_AX)
863#define AscWriteChipAX(port, data) outpw((port)+IOP_REG_AX, data)
864#define AscReadChipIX(port) (uchar)inp((port)+IOP_REG_IX)
865#define AscWriteChipIX(port, data) outp((port)+IOP_REG_IX, data)
866#define AscReadChipIH(port) (ushort)inpw((port)+IOP_REG_IH)
867#define AscWriteChipIH(port, data) outpw((port)+IOP_REG_IH, data)
868#define AscReadChipQP(port) (uchar)inp((port)+IOP_REG_QP)
869#define AscWriteChipQP(port, data) outp((port)+IOP_REG_QP, data)
870#define AscReadChipFIFO_L(port) (ushort)inpw((port)+IOP_REG_FIFO_L)
871#define AscWriteChipFIFO_L(port, data) outpw((port)+IOP_REG_FIFO_L, data)
872#define AscReadChipFIFO_H(port) (ushort)inpw((port)+IOP_REG_FIFO_H)
873#define AscWriteChipFIFO_H(port, data) outpw((port)+IOP_REG_FIFO_H, data)
874#define AscReadChipDmaSpeed(port) (uchar)inp((port)+IOP_DMA_SPEED)
875#define AscWriteChipDmaSpeed(port, data) outp((port)+IOP_DMA_SPEED, data)
876#define AscReadChipDA0(port) (ushort)inpw((port)+IOP_REG_DA0)
877#define AscWriteChipDA0(port) outpw((port)+IOP_REG_DA0, data)
878#define AscReadChipDA1(port) (ushort)inpw((port)+IOP_REG_DA1)
879#define AscWriteChipDA1(port) outpw((port)+IOP_REG_DA1, data)
880#define AscReadChipDC0(port) (ushort)inpw((port)+IOP_REG_DC0)
881#define AscWriteChipDC0(port) outpw((port)+IOP_REG_DC0, data)
882#define AscReadChipDC1(port) (ushort)inpw((port)+IOP_REG_DC1)
883#define AscWriteChipDC1(port) outpw((port)+IOP_REG_DC1, data)
884#define AscReadChipDvcID(port) (uchar)inp((port)+IOP_REG_ID)
885#define AscWriteChipDvcID(port, data) outp((port)+IOP_REG_ID, data)
886
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400887#define AdvPortAddr void __iomem * /* Virtual memory address size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889/*
890 * Define Adv Library required memory access macros.
891 */
892#define ADV_MEM_READB(addr) readb(addr)
893#define ADV_MEM_READW(addr) readw(addr)
894#define ADV_MEM_WRITEB(addr, byte) writeb(byte, addr)
895#define ADV_MEM_WRITEW(addr, word) writew(word, addr)
896#define ADV_MEM_WRITEDW(addr, dword) writel(dword, addr)
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * Define total number of simultaneous maximum element scatter-gather
900 * request blocks per wide adapter. ASC_DEF_MAX_HOST_QNG (253) is the
901 * maximum number of outstanding commands per wide host adapter. Each
902 * command uses one or more ADV_SG_BLOCK each with 15 scatter-gather
903 * elements. Allow each command to have at least one ADV_SG_BLOCK structure.
904 * This allows about 15 commands to have the maximum 17 ADV_SG_BLOCK
905 * structures or 255 scatter-gather elements.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
907#define ADV_TOT_SG_BLOCK ASC_DEF_MAX_HOST_QNG
908
909/*
Matthew Wilcox98d41c22007-10-02 21:55:37 -0400910 * Define maximum number of scatter-gather elements per request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912#define ADV_MAX_SG_LIST 255
Matthew Wilcox98d41c22007-10-02 21:55:37 -0400913#define NO_OF_SG_PER_BLOCK 15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#define ADV_EEP_DVC_CFG_BEGIN (0x00)
916#define ADV_EEP_DVC_CFG_END (0x15)
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400917#define ADV_EEP_DVC_CTL_BEGIN (0x16) /* location of OEM name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#define ADV_EEP_MAX_WORD_ADDR (0x1E)
919
920#define ADV_EEP_DELAY_MS 100
921
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400922#define ADV_EEPROM_BIG_ENDIAN 0x8000 /* EEPROM Bit 15 */
923#define ADV_EEPROM_BIOS_ENABLE 0x4000 /* EEPROM Bit 14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/*
925 * For the ASC3550 Bit 13 is Termination Polarity control bit.
926 * For later ICs Bit 13 controls whether the CIS (Card Information
927 * Service Section) is loaded from EEPROM.
928 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400929#define ADV_EEPROM_TERM_POL 0x2000 /* EEPROM Bit 13 */
930#define ADV_EEPROM_CIS_LD 0x2000 /* EEPROM Bit 13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * ASC38C1600 Bit 11
933 *
934 * If EEPROM Bit 11 is 0 for Function 0, then Function 0 will specify
935 * INT A in the PCI Configuration Space Int Pin field. If it is 1, then
936 * Function 0 will specify INT B.
937 *
938 * If EEPROM Bit 11 is 0 for Function 1, then Function 1 will specify
939 * INT B in the PCI Configuration Space Int Pin field. If it is 1, then
940 * Function 1 will specify INT A.
941 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400942#define ADV_EEPROM_INTAB 0x0800 /* EEPROM Bit 11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400944typedef struct adveep_3550_config {
945 /* Word Offset, Description */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400947 ushort cfg_lsw; /* 00 power up initialization */
948 /* bit 13 set - Term Polarity Control */
949 /* bit 14 set - BIOS Enable */
950 /* bit 15 set - Big Endian Mode */
951 ushort cfg_msw; /* 01 unused */
952 ushort disc_enable; /* 02 disconnect enable */
953 ushort wdtr_able; /* 03 Wide DTR able */
954 ushort sdtr_able; /* 04 Synchronous DTR able */
955 ushort start_motor; /* 05 send start up motor */
956 ushort tagqng_able; /* 06 tag queuing able */
957 ushort bios_scan; /* 07 BIOS device control */
958 ushort scam_tolerant; /* 08 no scam */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400960 uchar adapter_scsi_id; /* 09 Host Adapter ID */
961 uchar bios_boot_delay; /* power up wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400963 uchar scsi_reset_delay; /* 10 reset delay */
964 uchar bios_id_lun; /* first boot device scsi id & lun */
965 /* high nibble is lun */
966 /* low nibble is scsi id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400968 uchar termination; /* 11 0 - automatic */
969 /* 1 - low off / high off */
970 /* 2 - low off / high on */
971 /* 3 - low on / high on */
972 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400974 uchar reserved1; /* reserved byte (not used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Matthew Wilcox27c868c2007-07-26 10:56:23 -0400976 ushort bios_ctrl; /* 12 BIOS control bits */
977 /* bit 0 BIOS don't act as initiator. */
978 /* bit 1 BIOS > 1 GB support */
979 /* bit 2 BIOS > 2 Disk Support */
980 /* bit 3 BIOS don't support removables */
981 /* bit 4 BIOS support bootable CD */
982 /* bit 5 BIOS scan enabled */
983 /* bit 6 BIOS support multiple LUNs */
984 /* bit 7 BIOS display of message */
985 /* bit 8 SCAM disabled */
986 /* bit 9 Reset SCSI bus during init. */
987 /* bit 10 */
988 /* bit 11 No verbose initialization. */
989 /* bit 12 SCSI parity enabled */
990 /* bit 13 */
991 /* bit 14 */
992 /* bit 15 */
993 ushort ultra_able; /* 13 ULTRA speed able */
994 ushort reserved2; /* 14 reserved */
995 uchar max_host_qng; /* 15 maximum host queuing */
996 uchar max_dvc_qng; /* maximum per device queuing */
997 ushort dvc_cntl; /* 16 control bit for driver */
998 ushort bug_fix; /* 17 control bit for bug fix */
999 ushort serial_number_word1; /* 18 Board serial number word 1 */
1000 ushort serial_number_word2; /* 19 Board serial number word 2 */
1001 ushort serial_number_word3; /* 20 Board serial number word 3 */
1002 ushort check_sum; /* 21 EEP check sum */
1003 uchar oem_name[16]; /* 22 OEM name */
1004 ushort dvc_err_code; /* 30 last device driver error code */
1005 ushort adv_err_code; /* 31 last uc and Adv Lib error code */
1006 ushort adv_err_addr; /* 32 last uc error address */
1007 ushort saved_dvc_err_code; /* 33 saved last dev. driver error code */
1008 ushort saved_adv_err_code; /* 34 saved last uc and Adv Lib error code */
1009 ushort saved_adv_err_addr; /* 35 saved last uc error address */
1010 ushort num_of_err; /* 36 number of error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011} ADVEEP_3550_CONFIG;
1012
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001013typedef struct adveep_38C0800_config {
1014 /* Word Offset, Description */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001016 ushort cfg_lsw; /* 00 power up initialization */
1017 /* bit 13 set - Load CIS */
1018 /* bit 14 set - BIOS Enable */
1019 /* bit 15 set - Big Endian Mode */
1020 ushort cfg_msw; /* 01 unused */
1021 ushort disc_enable; /* 02 disconnect enable */
1022 ushort wdtr_able; /* 03 Wide DTR able */
1023 ushort sdtr_speed1; /* 04 SDTR Speed TID 0-3 */
1024 ushort start_motor; /* 05 send start up motor */
1025 ushort tagqng_able; /* 06 tag queuing able */
1026 ushort bios_scan; /* 07 BIOS device control */
1027 ushort scam_tolerant; /* 08 no scam */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001029 uchar adapter_scsi_id; /* 09 Host Adapter ID */
1030 uchar bios_boot_delay; /* power up wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001032 uchar scsi_reset_delay; /* 10 reset delay */
1033 uchar bios_id_lun; /* first boot device scsi id & lun */
1034 /* high nibble is lun */
1035 /* low nibble is scsi id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001037 uchar termination_se; /* 11 0 - automatic */
1038 /* 1 - low off / high off */
1039 /* 2 - low off / high on */
1040 /* 3 - low on / high on */
1041 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001043 uchar termination_lvd; /* 11 0 - automatic */
1044 /* 1 - low off / high off */
1045 /* 2 - low off / high on */
1046 /* 3 - low on / high on */
1047 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001049 ushort bios_ctrl; /* 12 BIOS control bits */
1050 /* bit 0 BIOS don't act as initiator. */
1051 /* bit 1 BIOS > 1 GB support */
1052 /* bit 2 BIOS > 2 Disk Support */
1053 /* bit 3 BIOS don't support removables */
1054 /* bit 4 BIOS support bootable CD */
1055 /* bit 5 BIOS scan enabled */
1056 /* bit 6 BIOS support multiple LUNs */
1057 /* bit 7 BIOS display of message */
1058 /* bit 8 SCAM disabled */
1059 /* bit 9 Reset SCSI bus during init. */
1060 /* bit 10 */
1061 /* bit 11 No verbose initialization. */
1062 /* bit 12 SCSI parity enabled */
1063 /* bit 13 */
1064 /* bit 14 */
1065 /* bit 15 */
1066 ushort sdtr_speed2; /* 13 SDTR speed TID 4-7 */
1067 ushort sdtr_speed3; /* 14 SDTR speed TID 8-11 */
1068 uchar max_host_qng; /* 15 maximum host queueing */
1069 uchar max_dvc_qng; /* maximum per device queuing */
1070 ushort dvc_cntl; /* 16 control bit for driver */
1071 ushort sdtr_speed4; /* 17 SDTR speed 4 TID 12-15 */
1072 ushort serial_number_word1; /* 18 Board serial number word 1 */
1073 ushort serial_number_word2; /* 19 Board serial number word 2 */
1074 ushort serial_number_word3; /* 20 Board serial number word 3 */
1075 ushort check_sum; /* 21 EEP check sum */
1076 uchar oem_name[16]; /* 22 OEM name */
1077 ushort dvc_err_code; /* 30 last device driver error code */
1078 ushort adv_err_code; /* 31 last uc and Adv Lib error code */
1079 ushort adv_err_addr; /* 32 last uc error address */
1080 ushort saved_dvc_err_code; /* 33 saved last dev. driver error code */
1081 ushort saved_adv_err_code; /* 34 saved last uc and Adv Lib error code */
1082 ushort saved_adv_err_addr; /* 35 saved last uc error address */
1083 ushort reserved36; /* 36 reserved */
1084 ushort reserved37; /* 37 reserved */
1085 ushort reserved38; /* 38 reserved */
1086 ushort reserved39; /* 39 reserved */
1087 ushort reserved40; /* 40 reserved */
1088 ushort reserved41; /* 41 reserved */
1089 ushort reserved42; /* 42 reserved */
1090 ushort reserved43; /* 43 reserved */
1091 ushort reserved44; /* 44 reserved */
1092 ushort reserved45; /* 45 reserved */
1093 ushort reserved46; /* 46 reserved */
1094 ushort reserved47; /* 47 reserved */
1095 ushort reserved48; /* 48 reserved */
1096 ushort reserved49; /* 49 reserved */
1097 ushort reserved50; /* 50 reserved */
1098 ushort reserved51; /* 51 reserved */
1099 ushort reserved52; /* 52 reserved */
1100 ushort reserved53; /* 53 reserved */
1101 ushort reserved54; /* 54 reserved */
1102 ushort reserved55; /* 55 reserved */
1103 ushort cisptr_lsw; /* 56 CIS PTR LSW */
1104 ushort cisprt_msw; /* 57 CIS PTR MSW */
1105 ushort subsysvid; /* 58 SubSystem Vendor ID */
1106 ushort subsysid; /* 59 SubSystem ID */
1107 ushort reserved60; /* 60 reserved */
1108 ushort reserved61; /* 61 reserved */
1109 ushort reserved62; /* 62 reserved */
1110 ushort reserved63; /* 63 reserved */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111} ADVEEP_38C0800_CONFIG;
1112
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001113typedef struct adveep_38C1600_config {
1114 /* Word Offset, Description */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001116 ushort cfg_lsw; /* 00 power up initialization */
1117 /* bit 11 set - Func. 0 INTB, Func. 1 INTA */
1118 /* clear - Func. 0 INTA, Func. 1 INTB */
1119 /* bit 13 set - Load CIS */
1120 /* bit 14 set - BIOS Enable */
1121 /* bit 15 set - Big Endian Mode */
1122 ushort cfg_msw; /* 01 unused */
1123 ushort disc_enable; /* 02 disconnect enable */
1124 ushort wdtr_able; /* 03 Wide DTR able */
1125 ushort sdtr_speed1; /* 04 SDTR Speed TID 0-3 */
1126 ushort start_motor; /* 05 send start up motor */
1127 ushort tagqng_able; /* 06 tag queuing able */
1128 ushort bios_scan; /* 07 BIOS device control */
1129 ushort scam_tolerant; /* 08 no scam */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001131 uchar adapter_scsi_id; /* 09 Host Adapter ID */
1132 uchar bios_boot_delay; /* power up wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001134 uchar scsi_reset_delay; /* 10 reset delay */
1135 uchar bios_id_lun; /* first boot device scsi id & lun */
1136 /* high nibble is lun */
1137 /* low nibble is scsi id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001139 uchar termination_se; /* 11 0 - automatic */
1140 /* 1 - low off / high off */
1141 /* 2 - low off / high on */
1142 /* 3 - low on / high on */
1143 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001145 uchar termination_lvd; /* 11 0 - automatic */
1146 /* 1 - low off / high off */
1147 /* 2 - low off / high on */
1148 /* 3 - low on / high on */
1149 /* There is no low on / high off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001151 ushort bios_ctrl; /* 12 BIOS control bits */
1152 /* bit 0 BIOS don't act as initiator. */
1153 /* bit 1 BIOS > 1 GB support */
1154 /* bit 2 BIOS > 2 Disk Support */
1155 /* bit 3 BIOS don't support removables */
1156 /* bit 4 BIOS support bootable CD */
1157 /* bit 5 BIOS scan enabled */
1158 /* bit 6 BIOS support multiple LUNs */
1159 /* bit 7 BIOS display of message */
1160 /* bit 8 SCAM disabled */
1161 /* bit 9 Reset SCSI bus during init. */
1162 /* bit 10 Basic Integrity Checking disabled */
1163 /* bit 11 No verbose initialization. */
1164 /* bit 12 SCSI parity enabled */
1165 /* bit 13 AIPP (Asyn. Info. Ph. Prot.) dis. */
1166 /* bit 14 */
1167 /* bit 15 */
1168 ushort sdtr_speed2; /* 13 SDTR speed TID 4-7 */
1169 ushort sdtr_speed3; /* 14 SDTR speed TID 8-11 */
1170 uchar max_host_qng; /* 15 maximum host queueing */
1171 uchar max_dvc_qng; /* maximum per device queuing */
1172 ushort dvc_cntl; /* 16 control bit for driver */
1173 ushort sdtr_speed4; /* 17 SDTR speed 4 TID 12-15 */
1174 ushort serial_number_word1; /* 18 Board serial number word 1 */
1175 ushort serial_number_word2; /* 19 Board serial number word 2 */
1176 ushort serial_number_word3; /* 20 Board serial number word 3 */
1177 ushort check_sum; /* 21 EEP check sum */
1178 uchar oem_name[16]; /* 22 OEM name */
1179 ushort dvc_err_code; /* 30 last device driver error code */
1180 ushort adv_err_code; /* 31 last uc and Adv Lib error code */
1181 ushort adv_err_addr; /* 32 last uc error address */
1182 ushort saved_dvc_err_code; /* 33 saved last dev. driver error code */
1183 ushort saved_adv_err_code; /* 34 saved last uc and Adv Lib error code */
1184 ushort saved_adv_err_addr; /* 35 saved last uc error address */
1185 ushort reserved36; /* 36 reserved */
1186 ushort reserved37; /* 37 reserved */
1187 ushort reserved38; /* 38 reserved */
1188 ushort reserved39; /* 39 reserved */
1189 ushort reserved40; /* 40 reserved */
1190 ushort reserved41; /* 41 reserved */
1191 ushort reserved42; /* 42 reserved */
1192 ushort reserved43; /* 43 reserved */
1193 ushort reserved44; /* 44 reserved */
1194 ushort reserved45; /* 45 reserved */
1195 ushort reserved46; /* 46 reserved */
1196 ushort reserved47; /* 47 reserved */
1197 ushort reserved48; /* 48 reserved */
1198 ushort reserved49; /* 49 reserved */
1199 ushort reserved50; /* 50 reserved */
1200 ushort reserved51; /* 51 reserved */
1201 ushort reserved52; /* 52 reserved */
1202 ushort reserved53; /* 53 reserved */
1203 ushort reserved54; /* 54 reserved */
1204 ushort reserved55; /* 55 reserved */
1205 ushort cisptr_lsw; /* 56 CIS PTR LSW */
1206 ushort cisprt_msw; /* 57 CIS PTR MSW */
1207 ushort subsysvid; /* 58 SubSystem Vendor ID */
1208 ushort subsysid; /* 59 SubSystem ID */
1209 ushort reserved60; /* 60 reserved */
1210 ushort reserved61; /* 61 reserved */
1211 ushort reserved62; /* 62 reserved */
1212 ushort reserved63; /* 63 reserved */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213} ADVEEP_38C1600_CONFIG;
1214
1215/*
1216 * EEPROM Commands
1217 */
1218#define ASC_EEP_CMD_DONE 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220/* bios_ctrl */
1221#define BIOS_CTRL_BIOS 0x0001
1222#define BIOS_CTRL_EXTENDED_XLAT 0x0002
1223#define BIOS_CTRL_GT_2_DISK 0x0004
1224#define BIOS_CTRL_BIOS_REMOVABLE 0x0008
1225#define BIOS_CTRL_BOOTABLE_CD 0x0010
1226#define BIOS_CTRL_MULTIPLE_LUN 0x0040
1227#define BIOS_CTRL_DISPLAY_MSG 0x0080
1228#define BIOS_CTRL_NO_SCAM 0x0100
1229#define BIOS_CTRL_RESET_SCSI_BUS 0x0200
1230#define BIOS_CTRL_INIT_VERBOSE 0x0800
1231#define BIOS_CTRL_SCSI_PARITY 0x1000
1232#define BIOS_CTRL_AIPP_DIS 0x2000
1233
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001234#define ADV_3550_MEMSIZE 0x2000 /* 8 KB Internal Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001236#define ADV_38C0800_MEMSIZE 0x4000 /* 16 KB Internal Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238/*
1239 * XXX - Since ASC38C1600 Rev.3 has a local RAM failure issue, there is
1240 * a special 16K Adv Library and Microcode version. After the issue is
1241 * resolved, should restore 32K support.
1242 *
1243 * #define ADV_38C1600_MEMSIZE 0x8000L * 32 KB Internal Memory *
1244 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001245#define ADV_38C1600_MEMSIZE 0x4000 /* 16 KB Internal Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247/*
1248 * Byte I/O register address from base of 'iop_base'.
1249 */
1250#define IOPB_INTR_STATUS_REG 0x00
1251#define IOPB_CHIP_ID_1 0x01
1252#define IOPB_INTR_ENABLES 0x02
1253#define IOPB_CHIP_TYPE_REV 0x03
1254#define IOPB_RES_ADDR_4 0x04
1255#define IOPB_RES_ADDR_5 0x05
1256#define IOPB_RAM_DATA 0x06
1257#define IOPB_RES_ADDR_7 0x07
1258#define IOPB_FLAG_REG 0x08
1259#define IOPB_RES_ADDR_9 0x09
1260#define IOPB_RISC_CSR 0x0A
1261#define IOPB_RES_ADDR_B 0x0B
1262#define IOPB_RES_ADDR_C 0x0C
1263#define IOPB_RES_ADDR_D 0x0D
1264#define IOPB_SOFT_OVER_WR 0x0E
1265#define IOPB_RES_ADDR_F 0x0F
1266#define IOPB_MEM_CFG 0x10
1267#define IOPB_RES_ADDR_11 0x11
1268#define IOPB_GPIO_DATA 0x12
1269#define IOPB_RES_ADDR_13 0x13
1270#define IOPB_FLASH_PAGE 0x14
1271#define IOPB_RES_ADDR_15 0x15
1272#define IOPB_GPIO_CNTL 0x16
1273#define IOPB_RES_ADDR_17 0x17
1274#define IOPB_FLASH_DATA 0x18
1275#define IOPB_RES_ADDR_19 0x19
1276#define IOPB_RES_ADDR_1A 0x1A
1277#define IOPB_RES_ADDR_1B 0x1B
1278#define IOPB_RES_ADDR_1C 0x1C
1279#define IOPB_RES_ADDR_1D 0x1D
1280#define IOPB_RES_ADDR_1E 0x1E
1281#define IOPB_RES_ADDR_1F 0x1F
1282#define IOPB_DMA_CFG0 0x20
1283#define IOPB_DMA_CFG1 0x21
1284#define IOPB_TICKLE 0x22
1285#define IOPB_DMA_REG_WR 0x23
1286#define IOPB_SDMA_STATUS 0x24
1287#define IOPB_SCSI_BYTE_CNT 0x25
1288#define IOPB_HOST_BYTE_CNT 0x26
1289#define IOPB_BYTE_LEFT_TO_XFER 0x27
1290#define IOPB_BYTE_TO_XFER_0 0x28
1291#define IOPB_BYTE_TO_XFER_1 0x29
1292#define IOPB_BYTE_TO_XFER_2 0x2A
1293#define IOPB_BYTE_TO_XFER_3 0x2B
1294#define IOPB_ACC_GRP 0x2C
1295#define IOPB_RES_ADDR_2D 0x2D
1296#define IOPB_DEV_ID 0x2E
1297#define IOPB_RES_ADDR_2F 0x2F
1298#define IOPB_SCSI_DATA 0x30
1299#define IOPB_RES_ADDR_31 0x31
1300#define IOPB_RES_ADDR_32 0x32
1301#define IOPB_SCSI_DATA_HSHK 0x33
1302#define IOPB_SCSI_CTRL 0x34
1303#define IOPB_RES_ADDR_35 0x35
1304#define IOPB_RES_ADDR_36 0x36
1305#define IOPB_RES_ADDR_37 0x37
1306#define IOPB_RAM_BIST 0x38
1307#define IOPB_PLL_TEST 0x39
1308#define IOPB_PCI_INT_CFG 0x3A
1309#define IOPB_RES_ADDR_3B 0x3B
1310#define IOPB_RFIFO_CNT 0x3C
1311#define IOPB_RES_ADDR_3D 0x3D
1312#define IOPB_RES_ADDR_3E 0x3E
1313#define IOPB_RES_ADDR_3F 0x3F
1314
1315/*
1316 * Word I/O register address from base of 'iop_base'.
1317 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001318#define IOPW_CHIP_ID_0 0x00 /* CID0 */
1319#define IOPW_CTRL_REG 0x02 /* CC */
1320#define IOPW_RAM_ADDR 0x04 /* LA */
1321#define IOPW_RAM_DATA 0x06 /* LD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#define IOPW_RES_ADDR_08 0x08
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001323#define IOPW_RISC_CSR 0x0A /* CSR */
1324#define IOPW_SCSI_CFG0 0x0C /* CFG0 */
1325#define IOPW_SCSI_CFG1 0x0E /* CFG1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326#define IOPW_RES_ADDR_10 0x10
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001327#define IOPW_SEL_MASK 0x12 /* SM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#define IOPW_RES_ADDR_14 0x14
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001329#define IOPW_FLASH_ADDR 0x16 /* FA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#define IOPW_RES_ADDR_18 0x18
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001331#define IOPW_EE_CMD 0x1A /* EC */
1332#define IOPW_EE_DATA 0x1C /* ED */
1333#define IOPW_SFIFO_CNT 0x1E /* SFC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#define IOPW_RES_ADDR_20 0x20
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001335#define IOPW_Q_BASE 0x22 /* QB */
1336#define IOPW_QP 0x24 /* QP */
1337#define IOPW_IX 0x26 /* IX */
1338#define IOPW_SP 0x28 /* SP */
1339#define IOPW_PC 0x2A /* PC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340#define IOPW_RES_ADDR_2C 0x2C
1341#define IOPW_RES_ADDR_2E 0x2E
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001342#define IOPW_SCSI_DATA 0x30 /* SD */
1343#define IOPW_SCSI_DATA_HSHK 0x32 /* SDH */
1344#define IOPW_SCSI_CTRL 0x34 /* SC */
1345#define IOPW_HSHK_CFG 0x36 /* HCFG */
1346#define IOPW_SXFR_STATUS 0x36 /* SXS */
1347#define IOPW_SXFR_CNTL 0x38 /* SXL */
1348#define IOPW_SXFR_CNTH 0x3A /* SXH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#define IOPW_RES_ADDR_3C 0x3C
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001350#define IOPW_RFIFO_DATA 0x3E /* RFD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352/*
1353 * Doubleword I/O register address from base of 'iop_base'.
1354 */
1355#define IOPDW_RES_ADDR_0 0x00
1356#define IOPDW_RAM_DATA 0x04
1357#define IOPDW_RES_ADDR_8 0x08
1358#define IOPDW_RES_ADDR_C 0x0C
1359#define IOPDW_RES_ADDR_10 0x10
1360#define IOPDW_COMMA 0x14
1361#define IOPDW_COMMB 0x18
1362#define IOPDW_RES_ADDR_1C 0x1C
1363#define IOPDW_SDMA_ADDR0 0x20
1364#define IOPDW_SDMA_ADDR1 0x24
1365#define IOPDW_SDMA_COUNT 0x28
1366#define IOPDW_SDMA_ERROR 0x2C
1367#define IOPDW_RDMA_ADDR0 0x30
1368#define IOPDW_RDMA_ADDR1 0x34
1369#define IOPDW_RDMA_COUNT 0x38
1370#define IOPDW_RDMA_ERROR 0x3C
1371
1372#define ADV_CHIP_ID_BYTE 0x25
1373#define ADV_CHIP_ID_WORD 0x04C1
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375#define ADV_INTR_ENABLE_HOST_INTR 0x01
1376#define ADV_INTR_ENABLE_SEL_INTR 0x02
1377#define ADV_INTR_ENABLE_DPR_INTR 0x04
1378#define ADV_INTR_ENABLE_RTA_INTR 0x08
1379#define ADV_INTR_ENABLE_RMA_INTR 0x10
1380#define ADV_INTR_ENABLE_RST_INTR 0x20
1381#define ADV_INTR_ENABLE_DPE_INTR 0x40
1382#define ADV_INTR_ENABLE_GLOBAL_INTR 0x80
1383
1384#define ADV_INTR_STATUS_INTRA 0x01
1385#define ADV_INTR_STATUS_INTRB 0x02
1386#define ADV_INTR_STATUS_INTRC 0x04
1387
1388#define ADV_RISC_CSR_STOP (0x0000)
1389#define ADV_RISC_TEST_COND (0x2000)
1390#define ADV_RISC_CSR_RUN (0x4000)
1391#define ADV_RISC_CSR_SINGLE_STEP (0x8000)
1392
1393#define ADV_CTRL_REG_HOST_INTR 0x0100
1394#define ADV_CTRL_REG_SEL_INTR 0x0200
1395#define ADV_CTRL_REG_DPR_INTR 0x0400
1396#define ADV_CTRL_REG_RTA_INTR 0x0800
1397#define ADV_CTRL_REG_RMA_INTR 0x1000
1398#define ADV_CTRL_REG_RES_BIT14 0x2000
1399#define ADV_CTRL_REG_DPE_INTR 0x4000
1400#define ADV_CTRL_REG_POWER_DONE 0x8000
1401#define ADV_CTRL_REG_ANY_INTR 0xFF00
1402
1403#define ADV_CTRL_REG_CMD_RESET 0x00C6
1404#define ADV_CTRL_REG_CMD_WR_IO_REG 0x00C5
1405#define ADV_CTRL_REG_CMD_RD_IO_REG 0x00C4
1406#define ADV_CTRL_REG_CMD_WR_PCI_CFG_SPACE 0x00C3
1407#define ADV_CTRL_REG_CMD_RD_PCI_CFG_SPACE 0x00C2
1408
1409#define ADV_TICKLE_NOP 0x00
1410#define ADV_TICKLE_A 0x01
1411#define ADV_TICKLE_B 0x02
1412#define ADV_TICKLE_C 0x03
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414#define AdvIsIntPending(port) \
1415 (AdvReadWordRegister(port, IOPW_CTRL_REG) & ADV_CTRL_REG_HOST_INTR)
1416
1417/*
1418 * SCSI_CFG0 Register bit definitions
1419 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001420#define TIMER_MODEAB 0xC000 /* Watchdog, Second, and Select. Timer Ctrl. */
1421#define PARITY_EN 0x2000 /* Enable SCSI Parity Error detection */
1422#define EVEN_PARITY 0x1000 /* Select Even Parity */
1423#define WD_LONG 0x0800 /* Watchdog Interval, 1: 57 min, 0: 13 sec */
1424#define QUEUE_128 0x0400 /* Queue Size, 1: 128 byte, 0: 64 byte */
1425#define PRIM_MODE 0x0100 /* Primitive SCSI mode */
1426#define SCAM_EN 0x0080 /* Enable SCAM selection */
1427#define SEL_TMO_LONG 0x0040 /* Sel/Resel Timeout, 1: 400 ms, 0: 1.6 ms */
1428#define CFRM_ID 0x0020 /* SCAM id sel. confirm., 1: fast, 0: 6.4 ms */
1429#define OUR_ID_EN 0x0010 /* Enable OUR_ID bits */
1430#define OUR_ID 0x000F /* SCSI ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432/*
1433 * SCSI_CFG1 Register bit definitions
1434 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001435#define BIG_ENDIAN 0x8000 /* Enable Big Endian Mode MIO:15, EEP:15 */
1436#define TERM_POL 0x2000 /* Terminator Polarity Ctrl. MIO:13, EEP:13 */
1437#define SLEW_RATE 0x1000 /* SCSI output buffer slew rate */
1438#define FILTER_SEL 0x0C00 /* Filter Period Selection */
1439#define FLTR_DISABLE 0x0000 /* Input Filtering Disabled */
1440#define FLTR_11_TO_20NS 0x0800 /* Input Filtering 11ns to 20ns */
1441#define FLTR_21_TO_39NS 0x0C00 /* Input Filtering 21ns to 39ns */
1442#define ACTIVE_DBL 0x0200 /* Disable Active Negation */
1443#define DIFF_MODE 0x0100 /* SCSI differential Mode (Read-Only) */
1444#define DIFF_SENSE 0x0080 /* 1: No SE cables, 0: SE cable (Read-Only) */
1445#define TERM_CTL_SEL 0x0040 /* Enable TERM_CTL_H and TERM_CTL_L */
1446#define TERM_CTL 0x0030 /* External SCSI Termination Bits */
1447#define TERM_CTL_H 0x0020 /* Enable External SCSI Upper Termination */
1448#define TERM_CTL_L 0x0010 /* Enable External SCSI Lower Termination */
1449#define CABLE_DETECT 0x000F /* External SCSI Cable Connection Status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451/*
1452 * Addendum for ASC-38C0800 Chip
1453 *
1454 * The ASC-38C1600 Chip uses the same definitions except that the
1455 * bus mode override bits [12:10] have been moved to byte register
1456 * offset 0xE (IOPB_SOFT_OVER_WR) bits [12:10]. The [12:10] bits in
1457 * SCSI_CFG1 are read-only and always available. Bit 14 (DIS_TERM_DRV)
1458 * is not needed. The [12:10] bits in IOPB_SOFT_OVER_WR are write-only.
1459 * Also each ASC-38C1600 function or channel uses only cable bits [5:4]
1460 * and [1:0]. Bits [14], [7:6], [3:2] are unused.
1461 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001462#define DIS_TERM_DRV 0x4000 /* 1: Read c_det[3:0], 0: cannot read */
1463#define HVD_LVD_SE 0x1C00 /* Device Detect Bits */
1464#define HVD 0x1000 /* HVD Device Detect */
1465#define LVD 0x0800 /* LVD Device Detect */
1466#define SE 0x0400 /* SE Device Detect */
1467#define TERM_LVD 0x00C0 /* LVD Termination Bits */
1468#define TERM_LVD_HI 0x0080 /* Enable LVD Upper Termination */
1469#define TERM_LVD_LO 0x0040 /* Enable LVD Lower Termination */
1470#define TERM_SE 0x0030 /* SE Termination Bits */
1471#define TERM_SE_HI 0x0020 /* Enable SE Upper Termination */
1472#define TERM_SE_LO 0x0010 /* Enable SE Lower Termination */
1473#define C_DET_LVD 0x000C /* LVD Cable Detect Bits */
1474#define C_DET3 0x0008 /* Cable Detect for LVD External Wide */
1475#define C_DET2 0x0004 /* Cable Detect for LVD Internal Wide */
1476#define C_DET_SE 0x0003 /* SE Cable Detect Bits */
1477#define C_DET1 0x0002 /* Cable Detect for SE Internal Wide */
1478#define C_DET0 0x0001 /* Cable Detect for SE Internal Narrow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480#define CABLE_ILLEGAL_A 0x7
1481 /* x 0 0 0 | on on | Illegal (all 3 connectors are used) */
1482
1483#define CABLE_ILLEGAL_B 0xB
1484 /* 0 x 0 0 | on on | Illegal (all 3 connectors are used) */
1485
1486/*
1487 * MEM_CFG Register bit definitions
1488 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001489#define BIOS_EN 0x40 /* BIOS Enable MIO:14,EEP:14 */
1490#define FAST_EE_CLK 0x20 /* Diagnostic Bit */
1491#define RAM_SZ 0x1C /* Specify size of RAM to RISC */
1492#define RAM_SZ_2KB 0x00 /* 2 KB */
1493#define RAM_SZ_4KB 0x04 /* 4 KB */
1494#define RAM_SZ_8KB 0x08 /* 8 KB */
1495#define RAM_SZ_16KB 0x0C /* 16 KB */
1496#define RAM_SZ_32KB 0x10 /* 32 KB */
1497#define RAM_SZ_64KB 0x14 /* 64 KB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499/*
1500 * DMA_CFG0 Register bit definitions
1501 *
1502 * This register is only accessible to the host.
1503 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001504#define BC_THRESH_ENB 0x80 /* PCI DMA Start Conditions */
1505#define FIFO_THRESH 0x70 /* PCI DMA FIFO Threshold */
1506#define FIFO_THRESH_16B 0x00 /* 16 bytes */
1507#define FIFO_THRESH_32B 0x20 /* 32 bytes */
1508#define FIFO_THRESH_48B 0x30 /* 48 bytes */
1509#define FIFO_THRESH_64B 0x40 /* 64 bytes */
1510#define FIFO_THRESH_80B 0x50 /* 80 bytes (default) */
1511#define FIFO_THRESH_96B 0x60 /* 96 bytes */
1512#define FIFO_THRESH_112B 0x70 /* 112 bytes */
1513#define START_CTL 0x0C /* DMA start conditions */
1514#define START_CTL_TH 0x00 /* Wait threshold level (default) */
1515#define START_CTL_ID 0x04 /* Wait SDMA/SBUS idle */
1516#define START_CTL_THID 0x08 /* Wait threshold and SDMA/SBUS idle */
1517#define START_CTL_EMFU 0x0C /* Wait SDMA FIFO empty/full */
1518#define READ_CMD 0x03 /* Memory Read Method */
1519#define READ_CMD_MR 0x00 /* Memory Read */
1520#define READ_CMD_MRL 0x02 /* Memory Read Long */
1521#define READ_CMD_MRM 0x03 /* Memory Read Multiple (default) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523/*
1524 * ASC-38C0800 RAM BIST Register bit definitions
1525 */
1526#define RAM_TEST_MODE 0x80
1527#define PRE_TEST_MODE 0x40
1528#define NORMAL_MODE 0x00
1529#define RAM_TEST_DONE 0x10
1530#define RAM_TEST_STATUS 0x0F
1531#define RAM_TEST_HOST_ERROR 0x08
1532#define RAM_TEST_INTRAM_ERROR 0x04
1533#define RAM_TEST_RISC_ERROR 0x02
1534#define RAM_TEST_SCSI_ERROR 0x01
1535#define RAM_TEST_SUCCESS 0x00
1536#define PRE_TEST_VALUE 0x05
1537#define NORMAL_VALUE 0x00
1538
1539/*
1540 * ASC38C1600 Definitions
1541 *
1542 * IOPB_PCI_INT_CFG Bit Field Definitions
1543 */
1544
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001545#define INTAB_LD 0x80 /* Value loaded from EEPROM Bit 11. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547/*
1548 * Bit 1 can be set to change the interrupt for the Function to operate in
1549 * Totem Pole mode. By default Bit 1 is 0 and the interrupt operates in
1550 * Open Drain mode. Both functions of the ASC38C1600 must be set to the same
1551 * mode, otherwise the operating mode is undefined.
1552 */
1553#define TOTEMPOLE 0x02
1554
1555/*
1556 * Bit 0 can be used to change the Int Pin for the Function. The value is
1557 * 0 by default for both Functions with Function 0 using INT A and Function
1558 * B using INT B. For Function 0 if set, INT B is used. For Function 1 if set,
1559 * INT A is used.
1560 *
1561 * EEPROM Word 0 Bit 11 for each Function may change the initial Int Pin
1562 * value specified in the PCI Configuration Space.
1563 */
1564#define INTAB 0x01
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566/*
1567 * Adv Library Status Definitions
1568 */
1569#define ADV_TRUE 1
1570#define ADV_FALSE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#define ADV_SUCCESS 1
1572#define ADV_BUSY 0
1573#define ADV_ERROR (-1)
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575/*
1576 * ADV_DVC_VAR 'warn_code' values
1577 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001578#define ASC_WARN_BUSRESET_ERROR 0x0001 /* SCSI Bus Reset error */
1579#define ASC_WARN_EEPROM_CHKSUM 0x0002 /* EEP check sum error */
1580#define ASC_WARN_EEPROM_TERMINATION 0x0004 /* EEP termination bad field */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001581#define ASC_WARN_ERROR 0xFFFF /* ADV_ERROR return */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001583#define ADV_MAX_TID 15 /* max. target identifier */
1584#define ADV_MAX_LUN 7 /* max. logical unit number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * Fixed locations of microcode operating variables.
1588 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001589#define ASC_MC_CODE_BEGIN_ADDR 0x0028 /* microcode start address */
1590#define ASC_MC_CODE_END_ADDR 0x002A /* microcode end address */
1591#define ASC_MC_CODE_CHK_SUM 0x002C /* microcode code checksum */
1592#define ASC_MC_VERSION_DATE 0x0038 /* microcode version */
1593#define ASC_MC_VERSION_NUM 0x003A /* microcode number */
1594#define ASC_MC_BIOSMEM 0x0040 /* BIOS RISC Memory Start */
1595#define ASC_MC_BIOSLEN 0x0050 /* BIOS RISC Memory Length */
1596#define ASC_MC_BIOS_SIGNATURE 0x0058 /* BIOS Signature 0x55AA */
1597#define ASC_MC_BIOS_VERSION 0x005A /* BIOS Version (2 bytes) */
1598#define ASC_MC_SDTR_SPEED1 0x0090 /* SDTR Speed for TID 0-3 */
1599#define ASC_MC_SDTR_SPEED2 0x0092 /* SDTR Speed for TID 4-7 */
1600#define ASC_MC_SDTR_SPEED3 0x0094 /* SDTR Speed for TID 8-11 */
1601#define ASC_MC_SDTR_SPEED4 0x0096 /* SDTR Speed for TID 12-15 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602#define ASC_MC_CHIP_TYPE 0x009A
1603#define ASC_MC_INTRB_CODE 0x009B
1604#define ASC_MC_WDTR_ABLE 0x009C
1605#define ASC_MC_SDTR_ABLE 0x009E
1606#define ASC_MC_TAGQNG_ABLE 0x00A0
1607#define ASC_MC_DISC_ENABLE 0x00A2
1608#define ASC_MC_IDLE_CMD_STATUS 0x00A4
1609#define ASC_MC_IDLE_CMD 0x00A6
1610#define ASC_MC_IDLE_CMD_PARAMETER 0x00A8
1611#define ASC_MC_DEFAULT_SCSI_CFG0 0x00AC
1612#define ASC_MC_DEFAULT_SCSI_CFG1 0x00AE
1613#define ASC_MC_DEFAULT_MEM_CFG 0x00B0
1614#define ASC_MC_DEFAULT_SEL_MASK 0x00B2
1615#define ASC_MC_SDTR_DONE 0x00B6
1616#define ASC_MC_NUMBER_OF_QUEUED_CMD 0x00C0
1617#define ASC_MC_NUMBER_OF_MAX_CMD 0x00D0
1618#define ASC_MC_DEVICE_HSHK_CFG_TABLE 0x0100
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001619#define ASC_MC_CONTROL_FLAG 0x0122 /* Microcode control flag. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#define ASC_MC_WDTR_DONE 0x0124
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001621#define ASC_MC_CAM_MODE_MASK 0x015E /* CAM mode TID bitmask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622#define ASC_MC_ICQ 0x0160
1623#define ASC_MC_IRQ 0x0164
1624#define ASC_MC_PPR_ABLE 0x017A
1625
1626/*
1627 * BIOS LRAM variable absolute offsets.
1628 */
1629#define BIOS_CODESEG 0x54
1630#define BIOS_CODELEN 0x56
1631#define BIOS_SIGNATURE 0x58
1632#define BIOS_VERSION 0x5A
1633
1634/*
1635 * Microcode Control Flags
1636 *
1637 * Flags set by the Adv Library in RISC variable 'control_flag' (0x122)
1638 * and handled by the microcode.
1639 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001640#define CONTROL_FLAG_IGNORE_PERR 0x0001 /* Ignore DMA Parity Errors */
1641#define CONTROL_FLAG_ENABLE_AIPP 0x0002 /* Enabled AIPP checking. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/*
1644 * ASC_MC_DEVICE_HSHK_CFG_TABLE microcode table or HSHK_CFG register format
1645 */
1646#define HSHK_CFG_WIDE_XFR 0x8000
1647#define HSHK_CFG_RATE 0x0F00
1648#define HSHK_CFG_OFFSET 0x001F
1649
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001650#define ASC_DEF_MAX_HOST_QNG 0xFD /* Max. number of host commands (253) */
1651#define ASC_DEF_MIN_HOST_QNG 0x10 /* Min. number of host commands (16) */
1652#define ASC_DEF_MAX_DVC_QNG 0x3F /* Max. number commands per device (63) */
1653#define ASC_DEF_MIN_DVC_QNG 0x04 /* Min. number commands per device (4) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001655#define ASC_QC_DATA_CHECK 0x01 /* Require ASC_QC_DATA_OUT set or clear. */
1656#define ASC_QC_DATA_OUT 0x02 /* Data out DMA transfer. */
1657#define ASC_QC_START_MOTOR 0x04 /* Send auto-start motor before request. */
1658#define ASC_QC_NO_OVERRUN 0x08 /* Don't report overrun. */
1659#define ASC_QC_FREEZE_TIDQ 0x10 /* Freeze TID queue after request. XXX TBD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001661#define ASC_QSC_NO_DISC 0x01 /* Don't allow disconnect for request. */
1662#define ASC_QSC_NO_TAGMSG 0x02 /* Don't allow tag queuing for request. */
1663#define ASC_QSC_NO_SYNC 0x04 /* Don't use Synch. transfer on request. */
1664#define ASC_QSC_NO_WIDE 0x08 /* Don't use Wide transfer on request. */
1665#define ASC_QSC_REDO_DTR 0x10 /* Renegotiate WDTR/SDTR before request. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/*
1667 * Note: If a Tag Message is to be sent and neither ASC_QSC_HEAD_TAG or
1668 * ASC_QSC_ORDERED_TAG is set, then a Simple Tag Message (0x20) is used.
1669 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001670#define ASC_QSC_HEAD_TAG 0x40 /* Use Head Tag Message (0x21). */
1671#define ASC_QSC_ORDERED_TAG 0x80 /* Use Ordered Tag Message (0x22). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673/*
1674 * All fields here are accessed by the board microcode and need to be
1675 * little-endian.
1676 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001677typedef struct adv_carr_t {
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001678 __le32 carr_va; /* Carrier Virtual Address */
1679 __le32 carr_pa; /* Carrier Physical Address */
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02001680 __le32 areq_vpa; /* ADV_SCSI_REQ_Q Virtual or Physical Address */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001681 /*
1682 * next_vpa [31:4] Carrier Virtual or Physical Next Pointer
1683 *
1684 * next_vpa [3:1] Reserved Bits
1685 * next_vpa [0] Done Flag set in Response Queue.
1686 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001687 __le32 next_vpa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688} ADV_CARR_T;
1689
1690/*
1691 * Mask used to eliminate low 4 bits of carrier 'next_vpa' field.
1692 */
Hannes Reinecke084e6c32015-04-24 13:18:35 +02001693#define ADV_NEXT_VPA_MASK 0xFFFFFFF0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Hannes Reinecke084e6c32015-04-24 13:18:35 +02001695#define ADV_RQ_DONE 0x00000001
1696#define ADV_RQ_GOOD 0x00000002
1697#define ADV_CQ_STOPPER 0x00000000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Hannes Reinecke084e6c32015-04-24 13:18:35 +02001699#define ADV_GET_CARRP(carrp) ((carrp) & ADV_NEXT_VPA_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001701/*
1702 * Each carrier is 64 bytes, and we need three additional
1703 * carrier for icq, irq, and the termination carrier.
1704 */
1705#define ADV_CARRIER_COUNT (ASC_DEF_MAX_HOST_QNG + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707#define ADV_CARRIER_BUFSIZE \
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001708 (ADV_CARRIER_COUNT * sizeof(ADV_CARR_T))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001710#define ADV_CHIP_ASC3550 0x01 /* Ultra-Wide IC */
1711#define ADV_CHIP_ASC38C0800 0x02 /* Ultra2-Wide/LVD IC */
1712#define ADV_CHIP_ASC38C1600 0x03 /* Ultra3-Wide/LVD2 IC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714/*
1715 * Adapter temporary configuration structure
1716 *
1717 * This structure can be discarded after initialization. Don't add
1718 * fields here needed after initialization.
1719 *
1720 * Field naming convention:
1721 *
1722 * *_enable indicates the field enables or disables a feature. The
1723 * value of the field is never reset.
1724 */
1725typedef struct adv_dvc_cfg {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001726 ushort disc_enable; /* enable disconnection */
1727 uchar chip_version; /* chip version */
1728 uchar termination; /* Term. Ctrl. bits 6-5 of SCSI_CFG1 register */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001729 ushort control_flag; /* Microcode Control Flag */
1730 ushort mcode_date; /* Microcode date */
1731 ushort mcode_version; /* Microcode version */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001732 ushort serial1; /* EEPROM serial number word 1 */
1733 ushort serial2; /* EEPROM serial number word 2 */
1734 ushort serial3; /* EEPROM serial number word 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735} ADV_DVC_CFG;
1736
1737struct adv_dvc_var;
1738struct adv_scsi_req_q;
1739
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001740typedef struct adv_sg_block {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001741 uchar reserved1;
1742 uchar reserved2;
1743 uchar reserved3;
1744 uchar sg_cnt; /* Valid entries in block. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001745 __le32 sg_ptr; /* Pointer to next sg block. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001746 struct {
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001747 __le32 sg_addr; /* SG element address. */
1748 __le32 sg_count; /* SG element count. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001749 } sg_list[NO_OF_SG_PER_BLOCK];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750} ADV_SG_BLOCK;
1751
1752/*
1753 * ADV_SCSI_REQ_Q - microcode request structure
1754 *
1755 * All fields in this structure up to byte 60 are used by the microcode.
1756 * The microcode makes assumptions about the size and ordering of fields
1757 * in this structure. Do not change the structure definition here without
1758 * coordinating the change with the microcode.
1759 *
1760 * All fields accessed by microcode must be maintained in little_endian
1761 * order.
1762 */
1763typedef struct adv_scsi_req_q {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001764 uchar cntl; /* Ucode flags and state (ASC_MC_QC_*). */
1765 uchar target_cmd;
1766 uchar target_id; /* Device target identifier. */
1767 uchar target_lun; /* Device target logical unit number. */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02001768 __le32 data_addr; /* Data buffer physical address. */
1769 __le32 data_cnt; /* Data count. Ucode sets to residual. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02001770 __le32 sense_addr;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001771 __le32 carr_pa;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001772 uchar mflag;
1773 uchar sense_len;
1774 uchar cdb_len; /* SCSI CDB length. Must <= 16 bytes. */
1775 uchar scsi_cntl;
1776 uchar done_status; /* Completion status. */
1777 uchar scsi_status; /* SCSI status byte. */
1778 uchar host_status; /* Ucode host status. */
1779 uchar sg_working_ix;
1780 uchar cdb[12]; /* SCSI CDB bytes 0-11. */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02001781 __le32 sg_real_addr; /* SG list physical address. */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001782 __le32 scsiq_rptr;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001783 uchar cdb16[4]; /* SCSI CDB bytes 12-15. */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001784 __le32 scsiq_ptr;
1785 __le32 carr_va;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001786 /*
1787 * End of microcode structure - 60 bytes. The rest of the structure
1788 * is used by the Adv Library and ignored by the microcode.
1789 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02001790 u32 srb_tag;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001791 ADV_SG_BLOCK *sg_list_ptr; /* SG list virtual address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792} ADV_SCSI_REQ_Q;
1793
1794/*
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001795 * The following two structures are used to process Wide Board requests.
1796 *
1797 * The ADV_SCSI_REQ_Q structure in adv_req_t is passed to the Adv Library
Hannes Reinecke9c17c622015-04-24 13:18:21 +02001798 * and microcode with the ADV_SCSI_REQ_Q field 'srb_tag' set to the
1799 * SCSI request tag. The adv_req_t structure 'cmndp' field in turn points
1800 * to the Mid-Level SCSI request structure.
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001801 *
1802 * Zero or more ADV_SG_BLOCK are used with each ADV_SCSI_REQ_Q. Each
1803 * ADV_SG_BLOCK structure holds 15 scatter-gather elements. Under Linux
1804 * up to 255 scatter-gather elements may be used per request or
1805 * ADV_SCSI_REQ_Q.
1806 *
1807 * Both structures must be 32 byte aligned.
1808 */
1809typedef struct adv_sgblk {
1810 ADV_SG_BLOCK sg_block; /* Sgblock structure. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02001811 dma_addr_t sg_addr; /* Physical address */
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001812 struct adv_sgblk *next_sgblkp; /* Next scatter-gather structure. */
1813} adv_sgblk_t;
1814
1815typedef struct adv_req {
1816 ADV_SCSI_REQ_Q scsi_req_q; /* Adv Library request structure. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02001817 uchar align[24]; /* Request structure padding. */
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001818 struct scsi_cmnd *cmndp; /* Mid-Level SCSI command pointer. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02001819 dma_addr_t req_addr;
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001820 adv_sgblk_t *sgblkp; /* Adv Library scatter-gather pointer. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02001821} adv_req_t __aligned(32);
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001822
1823/*
1824 * Adapter operation variable structure.
1825 *
1826 * One structure is required per host adapter.
1827 *
1828 * Field naming convention:
1829 *
1830 * *_able indicates both whether a feature should be enabled or disabled
1831 * and whether a device isi capable of the feature. At initialization
1832 * this field may be set, but later if a device is found to be incapable
1833 * of the feature, the field is cleared.
1834 */
1835typedef struct adv_dvc_var {
1836 AdvPortAddr iop_base; /* I/O port address */
1837 ushort err_code; /* fatal error code */
1838 ushort bios_ctrl; /* BIOS control word, EEPROM word 12 */
1839 ushort wdtr_able; /* try WDTR for a device */
1840 ushort sdtr_able; /* try SDTR for a device */
1841 ushort ultra_able; /* try SDTR Ultra speed for a device */
1842 ushort sdtr_speed1; /* EEPROM SDTR Speed for TID 0-3 */
1843 ushort sdtr_speed2; /* EEPROM SDTR Speed for TID 4-7 */
1844 ushort sdtr_speed3; /* EEPROM SDTR Speed for TID 8-11 */
1845 ushort sdtr_speed4; /* EEPROM SDTR Speed for TID 12-15 */
1846 ushort tagqng_able; /* try tagged queuing with a device */
1847 ushort ppr_able; /* PPR message capable per TID bitmask. */
1848 uchar max_dvc_qng; /* maximum number of tagged commands per device */
1849 ushort start_motor; /* start motor command allowed */
1850 uchar scsi_reset_wait; /* delay in seconds after scsi bus reset */
1851 uchar chip_no; /* should be assigned by caller */
1852 uchar max_host_qng; /* maximum number of Q'ed command allowed */
1853 ushort no_scam; /* scam_tolerant of EEPROM */
1854 struct asc_board *drv_ptr; /* driver pointer to private structure */
1855 uchar chip_scsi_id; /* chip SCSI target ID */
1856 uchar chip_type;
1857 uchar bist_err_code;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001858 ADV_CARR_T *carrier;
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001859 ADV_CARR_T *carr_freelist; /* Carrier free list. */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02001860 dma_addr_t carrier_addr;
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001861 ADV_CARR_T *icq_sp; /* Initiator command queue stopper pointer. */
1862 ADV_CARR_T *irq_sp; /* Initiator response queue stopper pointer. */
1863 ushort carr_pending_cnt; /* Count of pending carriers. */
Matthew Wilcox98d41c22007-10-02 21:55:37 -04001864 /*
1865 * Note: The following fields will not be used after initialization. The
1866 * driver may discard the buffer after initialization is done.
1867 */
1868 ADV_DVC_CFG *cfg; /* temporary configuration structure */
1869} ADV_DVC_VAR;
1870
1871/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * Microcode idle loop commands
1873 */
1874#define IDLE_CMD_COMPLETED 0
1875#define IDLE_CMD_STOP_CHIP 0x0001
1876#define IDLE_CMD_STOP_CHIP_SEND_INT 0x0002
1877#define IDLE_CMD_SEND_INT 0x0004
1878#define IDLE_CMD_ABORT 0x0008
1879#define IDLE_CMD_DEVICE_RESET 0x0010
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001880#define IDLE_CMD_SCSI_RESET_START 0x0020 /* Assert SCSI Bus Reset */
1881#define IDLE_CMD_SCSI_RESET_END 0x0040 /* Deassert SCSI Bus Reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882#define IDLE_CMD_SCSIREQ 0x0080
1883
1884#define IDLE_CMD_STATUS_SUCCESS 0x0001
1885#define IDLE_CMD_STATUS_FAILURE 0x0002
1886
1887/*
1888 * AdvSendIdleCmd() flag definitions.
1889 */
1890#define ADV_NOWAIT 0x01
1891
1892/*
1893 * Wait loop time out values.
1894 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001895#define SCSI_WAIT_100_MSEC 100UL /* 100 milliseconds */
1896#define SCSI_US_PER_MSEC 1000 /* microseconds per millisecond */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001897#define SCSI_MAX_RETRY 10 /* retry count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001899#define ADV_ASYNC_RDMA_FAILURE 0x01 /* Fatal RDMA failure. */
1900#define ADV_ASYNC_SCSI_BUS_RESET_DET 0x02 /* Detected SCSI Bus Reset. */
1901#define ADV_ASYNC_CARRIER_READY_FAILURE 0x03 /* Carrier Ready failure. */
1902#define ADV_RDMA_IN_CARR_AND_Q_INVALID 0x04 /* RDMAed-in data invalid. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Matthew Wilcox27c868c2007-07-26 10:56:23 -04001904#define ADV_HOST_SCSI_BUS_RESET 0x80 /* Host Initiated SCSI Bus Reset. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906/* Read byte from a register. */
1907#define AdvReadByteRegister(iop_base, reg_off) \
1908 (ADV_MEM_READB((iop_base) + (reg_off)))
1909
1910/* Write byte to a register. */
1911#define AdvWriteByteRegister(iop_base, reg_off, byte) \
1912 (ADV_MEM_WRITEB((iop_base) + (reg_off), (byte)))
1913
1914/* Read word (2 bytes) from a register. */
1915#define AdvReadWordRegister(iop_base, reg_off) \
1916 (ADV_MEM_READW((iop_base) + (reg_off)))
1917
1918/* Write word (2 bytes) to a register. */
1919#define AdvWriteWordRegister(iop_base, reg_off, word) \
1920 (ADV_MEM_WRITEW((iop_base) + (reg_off), (word)))
1921
1922/* Write dword (4 bytes) to a register. */
1923#define AdvWriteDWordRegister(iop_base, reg_off, dword) \
1924 (ADV_MEM_WRITEDW((iop_base) + (reg_off), (dword)))
1925
1926/* Read byte from LRAM. */
1927#define AdvReadByteLram(iop_base, addr, byte) \
1928do { \
1929 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)); \
1930 (byte) = ADV_MEM_READB((iop_base) + IOPB_RAM_DATA); \
1931} while (0)
1932
1933/* Write byte to LRAM. */
1934#define AdvWriteByteLram(iop_base, addr, byte) \
1935 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1936 ADV_MEM_WRITEB((iop_base) + IOPB_RAM_DATA, (byte)))
1937
1938/* Read word (2 bytes) from LRAM. */
1939#define AdvReadWordLram(iop_base, addr, word) \
1940do { \
1941 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)); \
1942 (word) = (ADV_MEM_READW((iop_base) + IOPW_RAM_DATA)); \
1943} while (0)
1944
1945/* Write word (2 bytes) to LRAM. */
1946#define AdvWriteWordLram(iop_base, addr, word) \
1947 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1948 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, (word)))
1949
1950/* Write little-endian double word (4 bytes) to LRAM */
1951/* Because of unspecified C language ordering don't use auto-increment. */
1952#define AdvWriteDWordLramNoSwap(iop_base, addr, dword) \
1953 ((ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr)), \
1954 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, \
1955 cpu_to_le16((ushort) ((dword) & 0xFFFF)))), \
1956 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_ADDR, (addr) + 2), \
1957 ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, \
1958 cpu_to_le16((ushort) ((dword >> 16) & 0xFFFF)))))
1959
1960/* Read word (2 bytes) from LRAM assuming that the address is already set. */
1961#define AdvReadWordAutoIncLram(iop_base) \
1962 (ADV_MEM_READW((iop_base) + IOPW_RAM_DATA))
1963
1964/* Write word (2 bytes) to LRAM assuming that the address is already set. */
1965#define AdvWriteWordAutoIncLram(iop_base, word) \
1966 (ADV_MEM_WRITEW((iop_base) + IOPW_RAM_DATA, (word)))
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * Define macro to check for Condor signature.
1970 *
1971 * Evaluate to ADV_TRUE if a Condor chip is found the specified port
1972 * address 'iop_base'. Otherwise evalue to ADV_FALSE.
1973 */
1974#define AdvFindSignature(iop_base) \
1975 (((AdvReadByteRegister((iop_base), IOPB_CHIP_ID_1) == \
1976 ADV_CHIP_ID_BYTE) && \
1977 (AdvReadWordRegister((iop_base), IOPW_CHIP_ID_0) == \
1978 ADV_CHIP_ID_WORD)) ? ADV_TRUE : ADV_FALSE)
1979
1980/*
1981 * Define macro to Return the version number of the chip at 'iop_base'.
1982 *
1983 * The second parameter 'bus_type' is currently unused.
1984 */
1985#define AdvGetChipVersion(iop_base, bus_type) \
1986 AdvReadByteRegister((iop_base), IOPB_CHIP_TYPE_REV)
1987
1988/*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02001989 * Abort an SRB in the chip's RISC Memory. The 'srb_tag' argument must
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02001990 * match the ADV_SCSI_REQ_Q 'srb_tag' field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 *
1992 * If the request has not yet been sent to the device it will simply be
1993 * aborted from RISC memory. If the request is disconnected it will be
1994 * aborted on reselection by sending an Abort Message to the target ID.
1995 *
1996 * Return value:
1997 * ADV_TRUE(1) - Queue was successfully aborted.
1998 * ADV_FALSE(0) - Queue was not found on the active queue list.
1999 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002000#define AdvAbortQueue(asc_dvc, srb_tag) \
2001 AdvSendIdleCmd((asc_dvc), (ushort) IDLE_CMD_ABORT, \
2002 (ADV_DCNT) (srb_tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004/*
2005 * Send a Bus Device Reset Message to the specified target ID.
2006 *
2007 * All outstanding commands will be purged if sending the
2008 * Bus Device Reset Message is successful.
2009 *
2010 * Return Value:
2011 * ADV_TRUE(1) - All requests on the target are purged.
2012 * ADV_FALSE(0) - Couldn't issue Bus Device Reset Message; Requests
2013 * are not purged.
2014 */
2015#define AdvResetDevice(asc_dvc, target_id) \
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002016 AdvSendIdleCmd((asc_dvc), (ushort) IDLE_CMD_DEVICE_RESET, \
2017 (ADV_DCNT) (target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019/*
2020 * SCSI Wide Type definition.
2021 */
2022#define ADV_SCSI_BIT_ID_TYPE ushort
2023
2024/*
2025 * AdvInitScsiTarget() 'cntl_flag' options.
2026 */
2027#define ADV_SCAN_LUN 0x01
2028#define ADV_CAPINFO_NOLUN 0x02
2029
2030/*
2031 * Convert target id to target id bit mask.
2032 */
2033#define ADV_TID_TO_TIDMASK(tid) (0x01 << ((tid) & ADV_MAX_TID))
2034
2035/*
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02002036 * ADV_SCSI_REQ_Q 'done_status' and 'host_status' return values.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002039#define QD_NO_STATUS 0x00 /* Request not completed yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040#define QD_NO_ERROR 0x01
2041#define QD_ABORTED_BY_HOST 0x02
2042#define QD_WITH_ERROR 0x04
2043
2044#define QHSTA_NO_ERROR 0x00
2045#define QHSTA_M_SEL_TIMEOUT 0x11
2046#define QHSTA_M_DATA_OVER_RUN 0x12
2047#define QHSTA_M_UNEXPECTED_BUS_FREE 0x13
2048#define QHSTA_M_QUEUE_ABORTED 0x15
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002049#define QHSTA_M_SXFR_SDMA_ERR 0x16 /* SXFR_STATUS SCSI DMA Error */
2050#define QHSTA_M_SXFR_SXFR_PERR 0x17 /* SXFR_STATUS SCSI Bus Parity Error */
2051#define QHSTA_M_RDMA_PERR 0x18 /* RISC PCI DMA parity error */
2052#define QHSTA_M_SXFR_OFF_UFLW 0x19 /* SXFR_STATUS Offset Underflow */
2053#define QHSTA_M_SXFR_OFF_OFLW 0x20 /* SXFR_STATUS Offset Overflow */
2054#define QHSTA_M_SXFR_WD_TMO 0x21 /* SXFR_STATUS Watchdog Timeout */
2055#define QHSTA_M_SXFR_DESELECTED 0x22 /* SXFR_STATUS Deselected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056/* Note: QHSTA_M_SXFR_XFR_OFLW is identical to QHSTA_M_DATA_OVER_RUN. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002057#define QHSTA_M_SXFR_XFR_OFLW 0x12 /* SXFR_STATUS Transfer Overflow */
2058#define QHSTA_M_SXFR_XFR_PH_ERR 0x24 /* SXFR_STATUS Transfer Phase Error */
2059#define QHSTA_M_SXFR_UNKNOWN_ERROR 0x25 /* SXFR_STATUS Unknown Error */
2060#define QHSTA_M_SCSI_BUS_RESET 0x30 /* Request aborted from SBR */
2061#define QHSTA_M_SCSI_BUS_RESET_UNSOL 0x31 /* Request aborted from unsol. SBR */
2062#define QHSTA_M_BUS_DEVICE_RESET 0x32 /* Request aborted from BDR */
2063#define QHSTA_M_DIRECTION_ERR 0x35 /* Data Phase mismatch */
2064#define QHSTA_M_DIRECTION_ERR_HUNG 0x36 /* Data Phase mismatch and bus hang */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065#define QHSTA_M_WTM_TIMEOUT 0x41
2066#define QHSTA_M_BAD_CMPL_STATUS_IN 0x42
2067#define QHSTA_M_NO_AUTO_REQ_SENSE 0x43
2068#define QHSTA_M_AUTO_REQ_SENSE_FAIL 0x44
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002069#define QHSTA_M_INVALID_DEVICE 0x45 /* Bad target ID */
2070#define QHSTA_M_FROZEN_TIDQ 0x46 /* TID Queue frozen. */
2071#define QHSTA_M_SGBACKUP_ERROR 0x47 /* Scatter-Gather backup error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073/* Return the address that is aligned at the next doubleword >= to 'addr'. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074#define ADV_32BALIGN(addr) (((ulong) (addr) + 0x1F) & ~0x1F)
2075
2076/*
2077 * Total contiguous memory needed for driver SG blocks.
2078 *
2079 * ADV_MAX_SG_LIST must be defined by a driver. It is the maximum
2080 * number of scatter-gather elements the driver supports in a
2081 * single request.
2082 */
2083
2084#define ADV_SG_LIST_MAX_BYTE_SIZE \
2085 (sizeof(ADV_SG_BLOCK) * \
2086 ((ADV_MAX_SG_LIST + (NO_OF_SG_PER_BLOCK - 1))/NO_OF_SG_PER_BLOCK))
2087
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002088/* struct asc_board flags */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002089#define ASC_IS_WIDE_BOARD 0x04 /* AdvanSys Wide Board */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091#define ASC_NARROW_BOARD(boardp) (((boardp)->flags & ASC_IS_WIDE_BOARD) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002093#define NO_ISA_DMA 0xff /* No ISA DMA Channel Used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002095#define ASC_INFO_SIZE 128 /* advansys_info() line size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/* Asc Library return codes */
2098#define ASC_TRUE 1
2099#define ASC_FALSE 0
2100#define ASC_NOERROR 1
2101#define ASC_BUSY 0
2102#define ASC_ERROR (-1)
2103
2104/* struct scsi_cmnd function return codes */
2105#define STATUS_BYTE(byte) (byte)
2106#define MSG_BYTE(byte) ((byte) << 8)
2107#define HOST_BYTE(byte) ((byte) << 16)
2108#define DRIVER_BYTE(byte) ((byte) << 24)
2109
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002110#define ASC_STATS(shost, counter) ASC_STATS_ADD(shost, counter, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111#ifndef ADVANSYS_STATS
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002112#define ASC_STATS_ADD(shost, counter, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#else /* ADVANSYS_STATS */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002114#define ASC_STATS_ADD(shost, counter, count) \
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002115 (((struct asc_board *) shost_priv(shost))->asc_stats.counter += (count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#endif /* ADVANSYS_STATS */
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/* If the result wraps when calculating tenths, return 0. */
2119#define ASC_TENTHS(num, den) \
2120 (((10 * ((num)/(den))) > (((num) * 10)/(den))) ? \
2121 0 : ((((num) * 10)/(den)) - (10 * ((num)/(den)))))
2122
2123/*
2124 * Display a message to the console.
2125 */
2126#define ASC_PRINT(s) \
2127 { \
2128 printk("advansys: "); \
2129 printk(s); \
2130 }
2131
2132#define ASC_PRINT1(s, a1) \
2133 { \
2134 printk("advansys: "); \
2135 printk((s), (a1)); \
2136 }
2137
2138#define ASC_PRINT2(s, a1, a2) \
2139 { \
2140 printk("advansys: "); \
2141 printk((s), (a1), (a2)); \
2142 }
2143
2144#define ASC_PRINT3(s, a1, a2, a3) \
2145 { \
2146 printk("advansys: "); \
2147 printk((s), (a1), (a2), (a3)); \
2148 }
2149
2150#define ASC_PRINT4(s, a1, a2, a3, a4) \
2151 { \
2152 printk("advansys: "); \
2153 printk((s), (a1), (a2), (a3), (a4)); \
2154 }
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#ifndef ADVANSYS_DEBUG
2157
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002158#define ASC_DBG(lvl, s...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#define ASC_DBG_PRT_SCSI_HOST(lvl, s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#define ASC_DBG_PRT_ASC_SCSI_Q(lvl, scsiqp)
2161#define ASC_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp)
2162#define ASC_DBG_PRT_ASC_QDONE_INFO(lvl, qdone)
2163#define ADV_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp)
2164#define ASC_DBG_PRT_HEX(lvl, name, start, length)
2165#define ASC_DBG_PRT_CDB(lvl, cdb, len)
2166#define ASC_DBG_PRT_SENSE(lvl, sense, len)
2167#define ASC_DBG_PRT_INQUIRY(lvl, inq, len)
2168
2169#else /* ADVANSYS_DEBUG */
2170
2171/*
2172 * Debugging Message Levels:
2173 * 0: Errors Only
2174 * 1: High-Level Tracing
2175 * 2-N: Verbose Tracing
2176 */
2177
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002178#define ASC_DBG(lvl, format, arg...) { \
2179 if (asc_dbglvl >= (lvl)) \
2180 printk(KERN_DEBUG "%s: %s: " format, DRV_NAME, \
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002181 __func__ , ## arg); \
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184#define ASC_DBG_PRT_SCSI_HOST(lvl, s) \
2185 { \
2186 if (asc_dbglvl >= (lvl)) { \
2187 asc_prt_scsi_host(s); \
2188 } \
2189 }
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#define ASC_DBG_PRT_ASC_SCSI_Q(lvl, scsiqp) \
2192 { \
2193 if (asc_dbglvl >= (lvl)) { \
2194 asc_prt_asc_scsi_q(scsiqp); \
2195 } \
2196 }
2197
2198#define ASC_DBG_PRT_ASC_QDONE_INFO(lvl, qdone) \
2199 { \
2200 if (asc_dbglvl >= (lvl)) { \
2201 asc_prt_asc_qdone_info(qdone); \
2202 } \
2203 }
2204
2205#define ASC_DBG_PRT_ADV_SCSI_REQ_Q(lvl, scsiqp) \
2206 { \
2207 if (asc_dbglvl >= (lvl)) { \
2208 asc_prt_adv_scsi_req_q(scsiqp); \
2209 } \
2210 }
2211
2212#define ASC_DBG_PRT_HEX(lvl, name, start, length) \
2213 { \
2214 if (asc_dbglvl >= (lvl)) { \
2215 asc_prt_hex((name), (start), (length)); \
2216 } \
2217 }
2218
2219#define ASC_DBG_PRT_CDB(lvl, cdb, len) \
2220 ASC_DBG_PRT_HEX((lvl), "CDB", (uchar *) (cdb), (len));
2221
2222#define ASC_DBG_PRT_SENSE(lvl, sense, len) \
2223 ASC_DBG_PRT_HEX((lvl), "SENSE", (uchar *) (sense), (len));
2224
2225#define ASC_DBG_PRT_INQUIRY(lvl, inq, len) \
2226 ASC_DBG_PRT_HEX((lvl), "INQUIRY", (uchar *) (inq), (len));
2227#endif /* ADVANSYS_DEBUG */
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229#ifdef ADVANSYS_STATS
2230
2231/* Per board statistics structure */
2232struct asc_stats {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002233 /* Driver Entrypoint Statistics */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02002234 unsigned int queuecommand; /* # calls to advansys_queuecommand() */
2235 unsigned int reset; /* # calls to advansys_eh_bus_reset() */
2236 unsigned int biosparam; /* # calls to advansys_biosparam() */
2237 unsigned int interrupt; /* # advansys_interrupt() calls */
2238 unsigned int callback; /* # calls to asc/adv_isr_callback() */
2239 unsigned int done; /* # calls to request's scsi_done function */
2240 unsigned int build_error; /* # asc/adv_build_req() ASC_ERROR returns. */
2241 unsigned int adv_build_noreq; /* # adv_build_req() adv_req_t alloc. fail. */
2242 unsigned int adv_build_nosg; /* # adv_build_req() adv_sgblk_t alloc. fail. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002243 /* AscExeScsiQueue()/AdvExeScsiQueue() Statistics */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02002244 unsigned int exe_noerror; /* # ASC_NOERROR returns. */
2245 unsigned int exe_busy; /* # ASC_BUSY returns. */
2246 unsigned int exe_error; /* # ASC_ERROR returns. */
2247 unsigned int exe_unknown; /* # unknown returns. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002248 /* Data Transfer Statistics */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02002249 unsigned int xfer_cnt; /* # I/O requests received */
2250 unsigned int xfer_elem; /* # scatter-gather elements */
2251 unsigned int xfer_sect; /* # 512-byte blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252};
2253#endif /* ADVANSYS_STATS */
2254
2255/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 * Structure allocated for each board.
2257 *
Matthew Wilcox8dfb5372007-07-30 09:08:34 -06002258 * This structure is allocated by scsi_host_alloc() at the end
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * of the 'Scsi_Host' structure starting at the 'hostdata'
2260 * field. It is guaranteed to be allocated from DMA-able memory.
2261 */
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002262struct asc_board {
Matthew Wilcox394dbf32007-07-26 11:56:40 -04002263 struct device *dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002264 struct Scsi_Host *shost;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002265 uint flags; /* Board flags */
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002266 unsigned int irq;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002267 union {
2268 ASC_DVC_VAR asc_dvc_var; /* Narrow board */
2269 ADV_DVC_VAR adv_dvc_var; /* Wide board */
2270 } dvc_var;
2271 union {
2272 ASC_DVC_CFG asc_dvc_cfg; /* Narrow board */
2273 ADV_DVC_CFG adv_dvc_cfg; /* Wide board */
2274 } dvc_cfg;
2275 ushort asc_n_io_port; /* Number I/O ports. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002276 ADV_SCSI_BIT_ID_TYPE init_tidmask; /* Target init./valid mask */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002277 ushort reqcnt[ADV_MAX_TID + 1]; /* Starvation request count */
2278 ADV_SCSI_BIT_ID_TYPE queue_full; /* Queue full mask */
2279 ushort queue_full_cnt[ADV_MAX_TID + 1]; /* Queue full count */
2280 union {
2281 ASCEEP_CONFIG asc_eep; /* Narrow EEPROM config. */
2282 ADVEEP_3550_CONFIG adv_3550_eep; /* 3550 EEPROM config. */
2283 ADVEEP_38C0800_CONFIG adv_38C0800_eep; /* 38C0800 EEPROM config. */
2284 ADVEEP_38C1600_CONFIG adv_38C1600_eep; /* 38C1600 EEPROM config. */
2285 } eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002286 /* /proc/scsi/advansys/[0...] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287#ifdef ADVANSYS_STATS
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002288 struct asc_stats asc_stats; /* Board statistics */
2289#endif /* ADVANSYS_STATS */
2290 /*
2291 * The following fields are used only for Narrow Boards.
2292 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002293 uchar sdtr_data[ASC_MAX_TID + 1]; /* SDTR information */
2294 /*
2295 * The following fields are used only for Wide Boards.
2296 */
2297 void __iomem *ioremap_addr; /* I/O Memory remap address. */
2298 ushort ioport; /* I/O Port address. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002299 adv_req_t *adv_reqp; /* Request structures. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02002300 dma_addr_t adv_reqp_addr;
2301 size_t adv_reqp_size;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002302 struct dma_pool *adv_sgblk_pool; /* Scatter-gather structures. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002303 ushort bios_signature; /* BIOS Signature. */
2304 ushort bios_version; /* BIOS Version. */
2305 ushort bios_codeseg; /* BIOS Code Segment. */
2306 ushort bios_codelen; /* BIOS Code Segment Length. */
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002307};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04002309#define asc_dvc_to_board(asc_dvc) container_of(asc_dvc, struct asc_board, \
2310 dvc_var.asc_dvc_var)
Matthew Wilcox13ac2d92007-07-30 08:10:23 -06002311#define adv_dvc_to_board(adv_dvc) container_of(adv_dvc, struct asc_board, \
2312 dvc_var.adv_dvc_var)
2313#define adv_dvc_to_pdev(adv_dvc) to_pci_dev(adv_dvc_to_board(adv_dvc)->dev)
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315#ifdef ADVANSYS_DEBUG
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002316static int asc_dbglvl = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318/*
Matthew Wilcox51219352007-10-02 21:55:22 -04002319 * asc_prt_asc_dvc_var()
2320 */
2321static void asc_prt_asc_dvc_var(ASC_DVC_VAR *h)
2322{
2323 printk("ASC_DVC_VAR at addr 0x%lx\n", (ulong)h);
2324
2325 printk(" iop_base 0x%x, err_code 0x%x, dvc_cntl 0x%x, bug_fix_cntl "
2326 "%d,\n", h->iop_base, h->err_code, h->dvc_cntl, h->bug_fix_cntl);
2327
2328 printk(" bus_type %d, init_sdtr 0x%x,\n", h->bus_type,
2329 (unsigned)h->init_sdtr);
2330
2331 printk(" sdtr_done 0x%x, use_tagged_qng 0x%x, unit_not_ready 0x%x, "
2332 "chip_no 0x%x,\n", (unsigned)h->sdtr_done,
2333 (unsigned)h->use_tagged_qng, (unsigned)h->unit_not_ready,
2334 (unsigned)h->chip_no);
2335
2336 printk(" queue_full_or_busy 0x%x, start_motor 0x%x, scsi_reset_wait "
2337 "%u,\n", (unsigned)h->queue_full_or_busy,
2338 (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
2339
2340 printk(" is_in_int %u, max_total_qng %u, cur_total_qng %u, "
2341 "in_critical_cnt %u,\n", (unsigned)h->is_in_int,
2342 (unsigned)h->max_total_qng, (unsigned)h->cur_total_qng,
2343 (unsigned)h->in_critical_cnt);
2344
2345 printk(" last_q_shortage %u, init_state 0x%x, no_scam 0x%x, "
2346 "pci_fix_asyn_xfer 0x%x,\n", (unsigned)h->last_q_shortage,
2347 (unsigned)h->init_state, (unsigned)h->no_scam,
2348 (unsigned)h->pci_fix_asyn_xfer);
2349
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002350 printk(" cfg 0x%lx\n", (ulong)h->cfg);
Matthew Wilcox51219352007-10-02 21:55:22 -04002351}
2352
2353/*
2354 * asc_prt_asc_dvc_cfg()
2355 */
2356static void asc_prt_asc_dvc_cfg(ASC_DVC_CFG *h)
2357{
2358 printk("ASC_DVC_CFG at addr 0x%lx\n", (ulong)h);
2359
2360 printk(" can_tagged_qng 0x%x, cmd_qng_enabled 0x%x,\n",
2361 h->can_tagged_qng, h->cmd_qng_enabled);
2362 printk(" disc_enable 0x%x, sdtr_enable 0x%x,\n",
2363 h->disc_enable, h->sdtr_enable);
2364
Matthew Wilcoxb08fc562007-10-02 21:55:32 -04002365 printk(" chip_scsi_id %d, isa_dma_speed %d, isa_dma_channel %d, "
2366 "chip_version %d,\n", h->chip_scsi_id, h->isa_dma_speed,
2367 h->isa_dma_channel, h->chip_version);
Matthew Wilcox51219352007-10-02 21:55:22 -04002368
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04002369 printk(" mcode_date 0x%x, mcode_version %d\n",
2370 h->mcode_date, h->mcode_version);
Matthew Wilcox51219352007-10-02 21:55:22 -04002371}
2372
2373/*
Matthew Wilcox51219352007-10-02 21:55:22 -04002374 * asc_prt_adv_dvc_var()
2375 *
2376 * Display an ADV_DVC_VAR structure.
2377 */
2378static void asc_prt_adv_dvc_var(ADV_DVC_VAR *h)
2379{
2380 printk(" ADV_DVC_VAR at addr 0x%lx\n", (ulong)h);
2381
2382 printk(" iop_base 0x%lx, err_code 0x%x, ultra_able 0x%x\n",
2383 (ulong)h->iop_base, h->err_code, (unsigned)h->ultra_able);
2384
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002385 printk(" sdtr_able 0x%x, wdtr_able 0x%x\n",
2386 (unsigned)h->sdtr_able, (unsigned)h->wdtr_able);
Matthew Wilcox51219352007-10-02 21:55:22 -04002387
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002388 printk(" start_motor 0x%x, scsi_reset_wait 0x%x\n",
2389 (unsigned)h->start_motor, (unsigned)h->scsi_reset_wait);
Matthew Wilcox51219352007-10-02 21:55:22 -04002390
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002391 printk(" max_host_qng %u, max_dvc_qng %u, carr_freelist 0x%p\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04002392 (unsigned)h->max_host_qng, (unsigned)h->max_dvc_qng,
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002393 h->carr_freelist);
Matthew Wilcox51219352007-10-02 21:55:22 -04002394
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002395 printk(" icq_sp 0x%p, irq_sp 0x%p\n", h->icq_sp, h->irq_sp);
Matthew Wilcox51219352007-10-02 21:55:22 -04002396
2397 printk(" no_scam 0x%x, tagqng_able 0x%x\n",
2398 (unsigned)h->no_scam, (unsigned)h->tagqng_able);
2399
2400 printk(" chip_scsi_id 0x%x, cfg 0x%lx\n",
2401 (unsigned)h->chip_scsi_id, (ulong)h->cfg);
2402}
2403
2404/*
2405 * asc_prt_adv_dvc_cfg()
2406 *
2407 * Display an ADV_DVC_CFG structure.
2408 */
2409static void asc_prt_adv_dvc_cfg(ADV_DVC_CFG *h)
2410{
2411 printk(" ADV_DVC_CFG at addr 0x%lx\n", (ulong)h);
2412
2413 printk(" disc_enable 0x%x, termination 0x%x\n",
2414 h->disc_enable, h->termination);
2415
2416 printk(" chip_version 0x%x, mcode_date 0x%x\n",
2417 h->chip_version, h->mcode_date);
2418
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002419 printk(" mcode_version 0x%x, control_flag 0x%x\n",
2420 h->mcode_version, h->control_flag);
Matthew Wilcox51219352007-10-02 21:55:22 -04002421}
2422
2423/*
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002424 * asc_prt_scsi_host()
Matthew Wilcox51219352007-10-02 21:55:22 -04002425 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002426static void asc_prt_scsi_host(struct Scsi_Host *s)
Matthew Wilcox51219352007-10-02 21:55:22 -04002427{
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002428 struct asc_board *boardp = shost_priv(s);
Matthew Wilcox51219352007-10-02 21:55:22 -04002429
Kay Sievers71610f52008-12-03 22:41:36 +01002430 printk("Scsi_Host at addr 0x%p, device %s\n", s, dev_name(boardp->dev));
Hannes Reinecke50d14a72013-10-23 10:51:17 +02002431 printk(" host_busy %u, host_no %d,\n",
Christoph Hellwig74665012014-01-22 15:29:29 +01002432 atomic_read(&s->host_busy), s->host_no);
Matthew Wilcox51219352007-10-02 21:55:22 -04002433
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002434 printk(" base 0x%lx, io_port 0x%lx, irq %d,\n",
2435 (ulong)s->base, (ulong)s->io_port, boardp->irq);
Matthew Wilcox51219352007-10-02 21:55:22 -04002436
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002437 printk(" dma_channel %d, this_id %d, can_queue %d,\n",
2438 s->dma_channel, s->this_id, s->can_queue);
Matthew Wilcox51219352007-10-02 21:55:22 -04002439
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002440 printk(" cmd_per_lun %d, sg_tablesize %d, unchecked_isa_dma %d\n",
2441 s->cmd_per_lun, s->sg_tablesize, s->unchecked_isa_dma);
Matthew Wilcox51219352007-10-02 21:55:22 -04002442
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002443 if (ASC_NARROW_BOARD(boardp)) {
2444 asc_prt_asc_dvc_var(&boardp->dvc_var.asc_dvc_var);
2445 asc_prt_asc_dvc_cfg(&boardp->dvc_cfg.asc_dvc_cfg);
2446 } else {
2447 asc_prt_adv_dvc_var(&boardp->dvc_var.adv_dvc_var);
2448 asc_prt_adv_dvc_cfg(&boardp->dvc_cfg.adv_dvc_cfg);
Matthew Wilcox51219352007-10-02 21:55:22 -04002449 }
2450}
2451
2452/*
2453 * asc_prt_hex()
2454 *
2455 * Print hexadecimal output in 4 byte groupings 32 bytes
2456 * or 8 double-words per line.
2457 */
2458static void asc_prt_hex(char *f, uchar *s, int l)
2459{
2460 int i;
2461 int j;
2462 int k;
2463 int m;
2464
2465 printk("%s: (%d bytes)\n", f, l);
2466
2467 for (i = 0; i < l; i += 32) {
2468
2469 /* Display a maximum of 8 double-words per line. */
2470 if ((k = (l - i) / 4) >= 8) {
2471 k = 8;
2472 m = 0;
2473 } else {
2474 m = (l - i) % 4;
2475 }
2476
2477 for (j = 0; j < k; j++) {
2478 printk(" %2.2X%2.2X%2.2X%2.2X",
2479 (unsigned)s[i + (j * 4)],
2480 (unsigned)s[i + (j * 4) + 1],
2481 (unsigned)s[i + (j * 4) + 2],
2482 (unsigned)s[i + (j * 4) + 3]);
2483 }
2484
2485 switch (m) {
2486 case 0:
2487 default:
2488 break;
2489 case 1:
2490 printk(" %2.2X", (unsigned)s[i + (j * 4)]);
2491 break;
2492 case 2:
2493 printk(" %2.2X%2.2X",
2494 (unsigned)s[i + (j * 4)],
2495 (unsigned)s[i + (j * 4) + 1]);
2496 break;
2497 case 3:
2498 printk(" %2.2X%2.2X%2.2X",
2499 (unsigned)s[i + (j * 4) + 1],
2500 (unsigned)s[i + (j * 4) + 2],
2501 (unsigned)s[i + (j * 4) + 3]);
2502 break;
2503 }
2504
2505 printk("\n");
2506 }
2507}
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002508
2509/*
2510 * asc_prt_asc_scsi_q()
2511 */
2512static void asc_prt_asc_scsi_q(ASC_SCSI_Q *q)
2513{
2514 ASC_SG_HEAD *sgp;
2515 int i;
2516
2517 printk("ASC_SCSI_Q at addr 0x%lx\n", (ulong)q);
2518
2519 printk
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002520 (" target_ix 0x%x, target_lun %u, srb_tag 0x%x, tag_code 0x%x,\n",
2521 q->q2.target_ix, q->q1.target_lun, q->q2.srb_tag,
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002522 q->q2.tag_code);
2523
2524 printk
2525 (" data_addr 0x%lx, data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2526 (ulong)le32_to_cpu(q->q1.data_addr),
2527 (ulong)le32_to_cpu(q->q1.data_cnt),
2528 (ulong)le32_to_cpu(q->q1.sense_addr), q->q1.sense_len);
2529
2530 printk(" cdbptr 0x%lx, cdb_len %u, sg_head 0x%lx, sg_queue_cnt %u\n",
2531 (ulong)q->cdbptr, q->q2.cdb_len,
2532 (ulong)q->sg_head, q->q1.sg_queue_cnt);
2533
2534 if (q->sg_head) {
2535 sgp = q->sg_head;
2536 printk("ASC_SG_HEAD at addr 0x%lx\n", (ulong)sgp);
2537 printk(" entry_cnt %u, queue_cnt %u\n", sgp->entry_cnt,
2538 sgp->queue_cnt);
2539 for (i = 0; i < sgp->entry_cnt; i++) {
2540 printk(" [%u]: addr 0x%lx, bytes %lu\n",
2541 i, (ulong)le32_to_cpu(sgp->sg_list[i].addr),
2542 (ulong)le32_to_cpu(sgp->sg_list[i].bytes));
2543 }
2544
2545 }
2546}
2547
2548/*
2549 * asc_prt_asc_qdone_info()
2550 */
2551static void asc_prt_asc_qdone_info(ASC_QDONE_INFO *q)
2552{
2553 printk("ASC_QDONE_INFO at addr 0x%lx\n", (ulong)q);
Hannes Reinecke9c17c622015-04-24 13:18:21 +02002554 printk(" srb_tag 0x%x, target_ix %u, cdb_len %u, tag_code %u,\n",
2555 q->d2.srb_tag, q->d2.target_ix, q->d2.cdb_len,
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002556 q->d2.tag_code);
2557 printk
2558 (" done_stat 0x%x, host_stat 0x%x, scsi_stat 0x%x, scsi_msg 0x%x\n",
2559 q->d3.done_stat, q->d3.host_stat, q->d3.scsi_stat, q->d3.scsi_msg);
2560}
2561
2562/*
2563 * asc_prt_adv_sgblock()
2564 *
2565 * Display an ADV_SG_BLOCK structure.
2566 */
2567static void asc_prt_adv_sgblock(int sgblockno, ADV_SG_BLOCK *b)
2568{
2569 int i;
2570
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002571 printk(" ADV_SG_BLOCK at addr 0x%lx (sgblockno %d)\n",
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002572 (ulong)b, sgblockno);
Hannes Reinecked9748db2015-04-24 13:18:36 +02002573 printk(" sg_cnt %u, sg_ptr 0x%x\n",
2574 b->sg_cnt, (u32)le32_to_cpu(b->sg_ptr));
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002575 BUG_ON(b->sg_cnt > NO_OF_SG_PER_BLOCK);
2576 if (b->sg_ptr != 0)
2577 BUG_ON(b->sg_cnt != NO_OF_SG_PER_BLOCK);
2578 for (i = 0; i < b->sg_cnt; i++) {
Hannes Reinecked9748db2015-04-24 13:18:36 +02002579 printk(" [%u]: sg_addr 0x%x, sg_count 0x%x\n",
2580 i, (u32)le32_to_cpu(b->sg_list[i].sg_addr),
2581 (u32)le32_to_cpu(b->sg_list[i].sg_count));
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002582 }
2583}
2584
2585/*
2586 * asc_prt_adv_scsi_req_q()
2587 *
2588 * Display an ADV_SCSI_REQ_Q structure.
2589 */
2590static void asc_prt_adv_scsi_req_q(ADV_SCSI_REQ_Q *q)
2591{
2592 int sg_blk_cnt;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002593 struct adv_sg_block *sg_ptr;
2594 adv_sgblk_t *sgblkp;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002595
2596 printk("ADV_SCSI_REQ_Q at addr 0x%lx\n", (ulong)q);
2597
Hannes Reinecked9748db2015-04-24 13:18:36 +02002598 printk(" target_id %u, target_lun %u, srb_tag 0x%x\n",
2599 q->target_id, q->target_lun, q->srb_tag);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002600
Hannes Reinecke98b96a72015-04-24 13:18:23 +02002601 printk(" cntl 0x%x, data_addr 0x%lx\n",
2602 q->cntl, (ulong)le32_to_cpu(q->data_addr));
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002603
2604 printk(" data_cnt %lu, sense_addr 0x%lx, sense_len %u,\n",
2605 (ulong)le32_to_cpu(q->data_cnt),
2606 (ulong)le32_to_cpu(q->sense_addr), q->sense_len);
2607
2608 printk
2609 (" cdb_len %u, done_status 0x%x, host_status 0x%x, scsi_status 0x%x\n",
2610 q->cdb_len, q->done_status, q->host_status, q->scsi_status);
2611
2612 printk(" sg_working_ix 0x%x, target_cmd %u\n",
2613 q->sg_working_ix, q->target_cmd);
2614
2615 printk(" scsiq_rptr 0x%lx, sg_real_addr 0x%lx, sg_list_ptr 0x%lx\n",
2616 (ulong)le32_to_cpu(q->scsiq_rptr),
2617 (ulong)le32_to_cpu(q->sg_real_addr), (ulong)q->sg_list_ptr);
2618
2619 /* Display the request's ADV_SG_BLOCK structures. */
2620 if (q->sg_list_ptr != NULL) {
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002621 sgblkp = container_of(q->sg_list_ptr, adv_sgblk_t, sg_block);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002622 sg_blk_cnt = 0;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002623 while (sgblkp) {
2624 sg_ptr = &sgblkp->sg_block;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002625 asc_prt_adv_sgblock(sg_blk_cnt, sg_ptr);
2626 if (sg_ptr->sg_ptr == 0) {
2627 break;
2628 }
Hannes Reinecke0ce53822015-04-24 13:18:25 +02002629 sgblkp = sgblkp->next_sgblkp;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002630 sg_blk_cnt++;
2631 }
2632 }
2633}
Matthew Wilcox51219352007-10-02 21:55:22 -04002634#endif /* ADVANSYS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * advansys_info()
2638 *
2639 * Return suitable for printing on the console with the argument
2640 * adapter's configuration information.
2641 *
2642 * Note: The information line should not exceed ASC_INFO_SIZE bytes,
2643 * otherwise the static 'info' array will be overrun.
2644 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002645static const char *advansys_info(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002647 static char info[ASC_INFO_SIZE];
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002648 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002649 ASC_DVC_VAR *asc_dvc_varp;
2650 ADV_DVC_VAR *adv_dvc_varp;
2651 char *busname;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002652 char *widename = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002654 if (ASC_NARROW_BOARD(boardp)) {
2655 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002656 ASC_DBG(1, "begin\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002657 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
2658 if ((asc_dvc_varp->bus_type & ASC_IS_ISAPNP) ==
2659 ASC_IS_ISAPNP) {
2660 busname = "ISA PnP";
2661 } else {
2662 busname = "ISA";
2663 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002664 sprintf(info,
2665 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X, DMA 0x%X",
2666 ASC_VERSION, busname,
2667 (ulong)shost->io_port,
Matthew Wilcox4a2d31c2007-07-26 11:55:34 -04002668 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002669 boardp->irq, shost->dma_channel);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002670 } else {
2671 if (asc_dvc_varp->bus_type & ASC_IS_VL) {
2672 busname = "VL";
2673 } else if (asc_dvc_varp->bus_type & ASC_IS_EISA) {
2674 busname = "EISA";
2675 } else if (asc_dvc_varp->bus_type & ASC_IS_PCI) {
2676 if ((asc_dvc_varp->bus_type & ASC_IS_PCI_ULTRA)
2677 == ASC_IS_PCI_ULTRA) {
2678 busname = "PCI Ultra";
2679 } else {
2680 busname = "PCI";
2681 }
2682 } else {
2683 busname = "?";
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04002684 shost_printk(KERN_ERR, shost, "unknown bus "
2685 "type %d\n", asc_dvc_varp->bus_type);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002686 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002687 sprintf(info,
2688 "AdvanSys SCSI %s: %s: IO 0x%lX-0x%lX, IRQ 0x%X",
Matthew Wilcoxecec1942007-07-30 08:08:22 -06002689 ASC_VERSION, busname, (ulong)shost->io_port,
Matthew Wilcox4a2d31c2007-07-26 11:55:34 -04002690 (ulong)shost->io_port + ASC_IOADR_GAP - 1,
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002691 boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002692 }
2693 } else {
2694 /*
2695 * Wide Adapter Information
2696 *
2697 * Memory-mapped I/O is used instead of I/O space to access
2698 * the adapter, but display the I/O Port range. The Memory
2699 * I/O address is displayed through the driver /proc file.
2700 */
2701 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
2702 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002703 widename = "Ultra-Wide";
2704 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002705 widename = "Ultra2-Wide";
2706 } else {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002707 widename = "Ultra3-Wide";
2708 }
2709 sprintf(info,
2710 "AdvanSys SCSI %s: PCI %s: PCIMEM 0x%lX-0x%lX, IRQ 0x%X",
2711 ASC_VERSION, widename, (ulong)adv_dvc_varp->iop_base,
Matthew Wilcoxd361db42007-10-02 21:55:29 -04002712 (ulong)adv_dvc_varp->iop_base + boardp->asc_n_io_port - 1, boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002713 }
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06002714 BUG_ON(strlen(info) >= ASC_INFO_SIZE);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04002715 ASC_DBG(1, "end\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002716 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Matthew Wilcox51219352007-10-02 21:55:22 -04002719#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 * asc_prt_board_devices()
2723 *
2724 * Print driver information for devices attached to the board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
Al Virob59fb6f2013-03-31 02:59:55 -04002726static void asc_prt_board_devices(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002728 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002729 int chip_scsi_id;
2730 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Al Virob59fb6f2013-03-31 02:59:55 -04002732 seq_printf(m,
2733 "\nDevice Information for AdvanSys SCSI Host %d:\n",
2734 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002736 if (ASC_NARROW_BOARD(boardp)) {
2737 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
2738 } else {
2739 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
2740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002742 seq_puts(m, "Target IDs Detected:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002743 for (i = 0; i <= ADV_MAX_TID; i++) {
Al Virob59fb6f2013-03-31 02:59:55 -04002744 if (boardp->init_tidmask & ADV_TID_TO_TIDMASK(i))
2745 seq_printf(m, " %X,", i);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002746 }
Al Virob59fb6f2013-03-31 02:59:55 -04002747 seq_printf(m, " (%X=Host Adapter)\n", chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
2750/*
2751 * Display Wide Board BIOS Information.
2752 */
Al Virob59fb6f2013-03-31 02:59:55 -04002753static void asc_prt_adv_bios(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002755 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002756 ushort major, minor, letter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002758 seq_puts(m, "\nROM BIOS Version: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002760 /*
2761 * If the BIOS saved a valid signature, then fill in
2762 * the BIOS code segment base address.
2763 */
2764 if (boardp->bios_signature != 0x55AA) {
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002765 seq_puts(m, "Disabled or Pre-3.1\n"
2766 "BIOS either disabled or Pre-3.1. If it is pre-3.1, then a newer version\n"
2767 "can be found at the ConnectCom FTP site: ftp://ftp.connectcom.net/pub\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002768 } else {
2769 major = (boardp->bios_version >> 12) & 0xF;
2770 minor = (boardp->bios_version >> 8) & 0xF;
2771 letter = (boardp->bios_version & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Al Virob59fb6f2013-03-31 02:59:55 -04002773 seq_printf(m, "%d.%d%c\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002774 major, minor,
2775 letter >= 26 ? '?' : letter + 'A');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002776 /*
2777 * Current available ROM BIOS release is 3.1I for UW
2778 * and 3.2I for U2W. This code doesn't differentiate
2779 * UW and U2W boards.
2780 */
2781 if (major < 3 || (major <= 3 && minor < 1) ||
2782 (major <= 3 && minor <= 1 && letter < ('I' - 'A'))) {
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002783 seq_puts(m, "Newer version of ROM BIOS is available at the ConnectCom FTP site:\n"
2784 "ftp://ftp.connectcom.net/pub\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002785 }
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789/*
2790 * Add serial number to information bar if signature AAh
2791 * is found in at bit 15-9 (7 bits) of word 1.
2792 *
2793 * Serial Number consists fo 12 alpha-numeric digits.
2794 *
2795 * 1 - Product type (A,B,C,D..) Word0: 15-13 (3 bits)
2796 * 2 - MFG Location (A,B,C,D..) Word0: 12-10 (3 bits)
2797 * 3-4 - Product ID (0-99) Word0: 9-0 (10 bits)
2798 * 5 - Product revision (A-J) Word0: " "
2799 *
2800 * Signature Word1: 15-9 (7 bits)
2801 * 6 - Year (0-9) Word1: 8-6 (3 bits) & Word2: 15 (1 bit)
2802 * 7-8 - Week of the year (1-52) Word1: 5-0 (6 bits)
2803 *
2804 * 9-12 - Serial Number (A001-Z999) Word2: 14-0 (15 bits)
2805 *
2806 * Note 1: Only production cards will have a serial number.
2807 *
2808 * Note 2: Signature is most significant 7 bits (0xFE).
2809 *
2810 * Returns ASC_TRUE if serial number found, otherwise returns ASC_FALSE.
2811 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002812static int asc_get_eeprom_string(ushort *serialnum, uchar *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002814 ushort w, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002816 if ((serialnum[1] & 0xFE00) != ((ushort)0xAA << 8)) {
2817 return ASC_FALSE;
2818 } else {
2819 /*
2820 * First word - 6 digits.
2821 */
2822 w = serialnum[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002824 /* Product type - 1st digit. */
2825 if ((*cp = 'A' + ((w & 0xE000) >> 13)) == 'H') {
2826 /* Product type is P=Prototype */
2827 *cp += 0x8;
2828 }
2829 cp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002831 /* Manufacturing location - 2nd digit. */
2832 *cp++ = 'A' + ((w & 0x1C00) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002834 /* Product ID - 3rd, 4th digits. */
2835 num = w & 0x3FF;
2836 *cp++ = '0' + (num / 100);
2837 num %= 100;
2838 *cp++ = '0' + (num / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002840 /* Product revision - 5th digit. */
2841 *cp++ = 'A' + (num % 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002843 /*
2844 * Second word
2845 */
2846 w = serialnum[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002848 /*
2849 * Year - 6th digit.
2850 *
2851 * If bit 15 of third word is set, then the
2852 * last digit of the year is greater than 7.
2853 */
2854 if (serialnum[2] & 0x8000) {
2855 *cp++ = '8' + ((w & 0x1C0) >> 6);
2856 } else {
2857 *cp++ = '0' + ((w & 0x1C0) >> 6);
2858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002860 /* Week of year - 7th, 8th digits. */
2861 num = w & 0x003F;
2862 *cp++ = '0' + num / 10;
2863 num %= 10;
2864 *cp++ = '0' + num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002866 /*
2867 * Third word
2868 */
2869 w = serialnum[2] & 0x7FFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002871 /* Serial number - 9th digit. */
2872 *cp++ = 'A' + (w / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002874 /* 10th, 11th, 12th digits. */
2875 num = w % 1000;
2876 *cp++ = '0' + num / 100;
2877 num %= 100;
2878 *cp++ = '0' + num / 10;
2879 num %= 10;
2880 *cp++ = '0' + num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002882 *cp = '\0'; /* Null Terminate the string. */
2883 return ASC_TRUE;
2884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
2887/*
2888 * asc_prt_asc_board_eeprom()
2889 *
2890 * Print board EEPROM configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Al Virob59fb6f2013-03-31 02:59:55 -04002892static void asc_prt_asc_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002894 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002895 ASC_DVC_VAR *asc_dvc_varp;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002896 ASCEEP_CONFIG *ep;
2897 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002899 int isa_dma_speed[] = { 10, 8, 7, 6, 5, 4, 3, 2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900#endif /* CONFIG_ISA */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002901 uchar serialstr[13];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002903 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
2904 ep = &boardp->eep_config.asc_eep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Al Virob59fb6f2013-03-31 02:59:55 -04002906 seq_printf(m,
2907 "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
2908 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002910 if (asc_get_eeprom_string((ushort *)&ep->adapter_info[0], serialstr)
Al Virob59fb6f2013-03-31 02:59:55 -04002911 == ASC_TRUE)
2912 seq_printf(m, " Serial Number: %s\n", serialstr);
2913 else if (ep->adapter_info[5] == 0xBB)
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002914 seq_puts(m,
2915 " Default Settings Used for EEPROM-less Adapter.\n");
Al Virob59fb6f2013-03-31 02:59:55 -04002916 else
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002917 seq_puts(m, " Serial Number Signature Not Present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Al Virob59fb6f2013-03-31 02:59:55 -04002919 seq_printf(m,
2920 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
2921 ASC_EEP_GET_CHIP_ID(ep), ep->max_total_qng,
2922 ep->max_tag_qng);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Al Virob59fb6f2013-03-31 02:59:55 -04002924 seq_printf(m,
2925 " cntl 0x%x, no_scam 0x%x\n", ep->cntl, ep->no_scam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Rasmus Villemoes2f979422014-12-03 00:10:50 +01002927 seq_puts(m, " Target ID: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002928 for (i = 0; i <= ASC_MAX_TID; i++)
2929 seq_printf(m, " %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002931 seq_puts(m, "\n Disconnects: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002932 for (i = 0; i <= ASC_MAX_TID; i++)
2933 seq_printf(m, " %c",
2934 (ep->disc_enable & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002936 seq_puts(m, "\n Command Queuing: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002937 for (i = 0; i <= ASC_MAX_TID; i++)
2938 seq_printf(m, " %c",
2939 (ep->use_cmd_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002941 seq_puts(m, "\n Start Motor: ");
Al Virob59fb6f2013-03-31 02:59:55 -04002942 for (i = 0; i <= ASC_MAX_TID; i++)
2943 seq_printf(m, " %c",
2944 (ep->start_motor & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Rasmus Villemoes3d300792014-12-03 00:10:53 +01002946 seq_puts(m, "\n Synchronous Transfer:");
Al Virob59fb6f2013-03-31 02:59:55 -04002947 for (i = 0; i <= ASC_MAX_TID; i++)
2948 seq_printf(m, " %c",
2949 (ep->init_sdtr & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01002950 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002953 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
Al Virob59fb6f2013-03-31 02:59:55 -04002954 seq_printf(m,
2955 " Host ISA DMA speed: %d MB/S\n",
2956 isa_dma_speed[ASC_EEP_GET_DMA_SPD(ep)]);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
2961/*
2962 * asc_prt_adv_board_eeprom()
2963 *
2964 * Print board EEPROM configuration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
Al Virob59fb6f2013-03-31 02:59:55 -04002966static void asc_prt_adv_board_eeprom(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04002968 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002969 ADV_DVC_VAR *adv_dvc_varp;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002970 int i;
2971 char *termstr;
2972 uchar serialstr[13];
2973 ADVEEP_3550_CONFIG *ep_3550 = NULL;
2974 ADVEEP_38C0800_CONFIG *ep_38C0800 = NULL;
2975 ADVEEP_38C1600_CONFIG *ep_38C1600 = NULL;
2976 ushort word;
2977 ushort *wordp;
2978 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002980 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
2981 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
2982 ep_3550 = &boardp->eep_config.adv_3550_eep;
2983 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
2984 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
2985 } else {
2986 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
2987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Al Virob59fb6f2013-03-31 02:59:55 -04002989 seq_printf(m,
2990 "\nEEPROM Settings for AdvanSys SCSI Host %d:\n",
2991 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Matthew Wilcox27c868c2007-07-26 10:56:23 -04002993 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
2994 wordp = &ep_3550->serial_number_word1;
2995 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
2996 wordp = &ep_38C0800->serial_number_word1;
2997 } else {
2998 wordp = &ep_38C1600->serial_number_word1;
2999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Al Virob59fb6f2013-03-31 02:59:55 -04003001 if (asc_get_eeprom_string(wordp, serialstr) == ASC_TRUE)
3002 seq_printf(m, " Serial Number: %s\n", serialstr);
3003 else
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003004 seq_puts(m, " Serial Number Signature Not Present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Al Virob59fb6f2013-03-31 02:59:55 -04003006 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3007 seq_printf(m,
3008 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3009 ep_3550->adapter_scsi_id,
3010 ep_3550->max_host_qng, ep_3550->max_dvc_qng);
3011 else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3012 seq_printf(m,
3013 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3014 ep_38C0800->adapter_scsi_id,
3015 ep_38C0800->max_host_qng,
3016 ep_38C0800->max_dvc_qng);
3017 else
3018 seq_printf(m,
3019 " Host SCSI ID: %u, Host Queue Size: %u, Device Queue Size: %u\n",
3020 ep_38C1600->adapter_scsi_id,
3021 ep_38C1600->max_host_qng,
3022 ep_38C1600->max_dvc_qng);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003023 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3024 word = ep_3550->termination;
3025 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3026 word = ep_38C0800->termination_lvd;
3027 } else {
3028 word = ep_38C1600->termination_lvd;
3029 }
3030 switch (word) {
3031 case 1:
3032 termstr = "Low Off/High Off";
3033 break;
3034 case 2:
3035 termstr = "Low Off/High On";
3036 break;
3037 case 3:
3038 termstr = "Low On/High On";
3039 break;
3040 default:
3041 case 0:
3042 termstr = "Automatic";
3043 break;
3044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Al Virob59fb6f2013-03-31 02:59:55 -04003046 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550)
3047 seq_printf(m,
3048 " termination: %u (%s), bios_ctrl: 0x%x\n",
3049 ep_3550->termination, termstr,
3050 ep_3550->bios_ctrl);
3051 else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800)
3052 seq_printf(m,
3053 " termination: %u (%s), bios_ctrl: 0x%x\n",
3054 ep_38C0800->termination_lvd, termstr,
3055 ep_38C0800->bios_ctrl);
3056 else
3057 seq_printf(m,
3058 " termination: %u (%s), bios_ctrl: 0x%x\n",
3059 ep_38C1600->termination_lvd, termstr,
3060 ep_38C1600->bios_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003062 seq_puts(m, " Target ID: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003063 for (i = 0; i <= ADV_MAX_TID; i++)
3064 seq_printf(m, " %X", i);
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003065 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003067 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3068 word = ep_3550->disc_enable;
3069 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3070 word = ep_38C0800->disc_enable;
3071 } else {
3072 word = ep_38C1600->disc_enable;
3073 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003074 seq_puts(m, " Disconnects: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003075 for (i = 0; i <= ADV_MAX_TID; i++)
3076 seq_printf(m, " %c",
3077 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003078 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003080 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3081 word = ep_3550->tagqng_able;
3082 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3083 word = ep_38C0800->tagqng_able;
3084 } else {
3085 word = ep_38C1600->tagqng_able;
3086 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003087 seq_puts(m, " Command Queuing: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003088 for (i = 0; i <= ADV_MAX_TID; i++)
3089 seq_printf(m, " %c",
3090 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003091 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003093 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3094 word = ep_3550->start_motor;
3095 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3096 word = ep_38C0800->start_motor;
3097 } else {
3098 word = ep_38C1600->start_motor;
3099 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003100 seq_puts(m, " Start Motor: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003101 for (i = 0; i <= ADV_MAX_TID; i++)
3102 seq_printf(m, " %c",
3103 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003104 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003106 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003107 seq_puts(m, " Synchronous Transfer:");
Al Virob59fb6f2013-03-31 02:59:55 -04003108 for (i = 0; i <= ADV_MAX_TID; i++)
3109 seq_printf(m, " %c",
3110 (ep_3550->sdtr_able & ADV_TID_TO_TIDMASK(i)) ?
3111 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003112 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003115 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003116 seq_puts(m, " Ultra Transfer: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003117 for (i = 0; i <= ADV_MAX_TID; i++)
3118 seq_printf(m, " %c",
3119 (ep_3550->ultra_able & ADV_TID_TO_TIDMASK(i))
3120 ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003121 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003124 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
3125 word = ep_3550->wdtr_able;
3126 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
3127 word = ep_38C0800->wdtr_able;
3128 } else {
3129 word = ep_38C1600->wdtr_able;
3130 }
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003131 seq_puts(m, " Wide Transfer: ");
Al Virob59fb6f2013-03-31 02:59:55 -04003132 for (i = 0; i <= ADV_MAX_TID; i++)
3133 seq_printf(m, " %c",
3134 (word & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003135 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003137 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800 ||
3138 adv_dvc_varp->chip_type == ADV_CHIP_ASC38C1600) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003139 seq_puts(m, " Synchronous Transfer Speed (Mhz):\n ");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003140 for (i = 0; i <= ADV_MAX_TID; i++) {
3141 char *speed_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003143 if (i == 0) {
3144 sdtr_speed = adv_dvc_varp->sdtr_speed1;
3145 } else if (i == 4) {
3146 sdtr_speed = adv_dvc_varp->sdtr_speed2;
3147 } else if (i == 8) {
3148 sdtr_speed = adv_dvc_varp->sdtr_speed3;
3149 } else if (i == 12) {
3150 sdtr_speed = adv_dvc_varp->sdtr_speed4;
3151 }
3152 switch (sdtr_speed & ADV_MAX_TID) {
3153 case 0:
3154 speed_str = "Off";
3155 break;
3156 case 1:
3157 speed_str = " 5";
3158 break;
3159 case 2:
3160 speed_str = " 10";
3161 break;
3162 case 3:
3163 speed_str = " 20";
3164 break;
3165 case 4:
3166 speed_str = " 40";
3167 break;
3168 case 5:
3169 speed_str = " 80";
3170 break;
3171 default:
3172 speed_str = "Unk";
3173 break;
3174 }
Al Virob59fb6f2013-03-31 02:59:55 -04003175 seq_printf(m, "%X:%s ", i, speed_str);
3176 if (i == 7)
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003177 seq_puts(m, "\n ");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003178 sdtr_speed >>= 4;
3179 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003180 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
3184/*
3185 * asc_prt_driver_conf()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 */
Al Virob59fb6f2013-03-31 02:59:55 -04003187static void asc_prt_driver_conf(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003189 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003190 int chip_scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Al Virob59fb6f2013-03-31 02:59:55 -04003192 seq_printf(m,
3193 "\nLinux Driver Configuration and Information for AdvanSys SCSI Host %d:\n",
3194 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Al Virob59fb6f2013-03-31 02:59:55 -04003196 seq_printf(m,
Hannes Reinecke1abf6352014-06-25 15:27:38 +02003197 " host_busy %u, max_id %u, max_lun %llu, max_channel %u\n",
Christoph Hellwig74665012014-01-22 15:29:29 +01003198 atomic_read(&shost->host_busy), shost->max_id,
Al Virob59fb6f2013-03-31 02:59:55 -04003199 shost->max_lun, shost->max_channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Al Virob59fb6f2013-03-31 02:59:55 -04003201 seq_printf(m,
3202 " unique_id %d, can_queue %d, this_id %d, sg_tablesize %u, cmd_per_lun %u\n",
3203 shost->unique_id, shost->can_queue, shost->this_id,
3204 shost->sg_tablesize, shost->cmd_per_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Al Virob59fb6f2013-03-31 02:59:55 -04003206 seq_printf(m,
3207 " unchecked_isa_dma %d, use_clustering %d\n",
3208 shost->unchecked_isa_dma, shost->use_clustering);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Al Virob59fb6f2013-03-31 02:59:55 -04003210 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003211 " flags 0x%x, last_reset 0x%lx, jiffies 0x%lx, asc_n_io_port 0x%x\n",
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02003212 boardp->flags, shost->last_reset, jiffies,
Al Virob59fb6f2013-03-31 02:59:55 -04003213 boardp->asc_n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Al Viro31491e12013-03-31 03:04:13 -04003215 seq_printf(m, " io_port 0x%lx\n", shost->io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003217 if (ASC_NARROW_BOARD(boardp)) {
3218 chip_scsi_id = boardp->dvc_cfg.asc_dvc_cfg.chip_scsi_id;
3219 } else {
3220 chip_scsi_id = boardp->dvc_var.adv_dvc_var.chip_scsi_id;
3221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
3224/*
3225 * asc_prt_asc_board_info()
3226 *
3227 * Print dynamic board configuration information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 */
Al Virob59fb6f2013-03-31 02:59:55 -04003229static void asc_prt_asc_board_info(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003231 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003232 int chip_scsi_id;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003233 ASC_DVC_VAR *v;
3234 ASC_DVC_CFG *c;
3235 int i;
3236 int renegotiate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003238 v = &boardp->dvc_var.asc_dvc_var;
3239 c = &boardp->dvc_cfg.asc_dvc_cfg;
3240 chip_scsi_id = c->chip_scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Al Virob59fb6f2013-03-31 02:59:55 -04003242 seq_printf(m,
3243 "\nAsc Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3244 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Al Virob59fb6f2013-03-31 02:59:55 -04003246 seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3247 "mcode_version 0x%x, err_code %u\n",
3248 c->chip_version, c->mcode_date, c->mcode_version,
3249 v->err_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003251 /* Current number of commands waiting for the host. */
Al Virob59fb6f2013-03-31 02:59:55 -04003252 seq_printf(m,
3253 " Total Command Pending: %d\n", v->cur_total_qng);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003255 seq_puts(m, " Command Queuing:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003256 for (i = 0; i <= ASC_MAX_TID; i++) {
3257 if ((chip_scsi_id == i) ||
3258 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3259 continue;
3260 }
Al Virob59fb6f2013-03-31 02:59:55 -04003261 seq_printf(m, " %X:%c",
3262 i,
3263 (v->use_tagged_qng & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003266 /* Current number of commands waiting for a device. */
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003267 seq_puts(m, "\n Command Queue Pending:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003268 for (i = 0; i <= ASC_MAX_TID; i++) {
3269 if ((chip_scsi_id == i) ||
3270 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3271 continue;
3272 }
Al Virob59fb6f2013-03-31 02:59:55 -04003273 seq_printf(m, " %X:%u", i, v->cur_dvc_qng[i]);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003276 /* Current limit on number of commands that can be sent to a device. */
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003277 seq_puts(m, "\n Command Queue Limit:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003278 for (i = 0; i <= ASC_MAX_TID; i++) {
3279 if ((chip_scsi_id == i) ||
3280 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3281 continue;
3282 }
Al Virob59fb6f2013-03-31 02:59:55 -04003283 seq_printf(m, " %X:%u", i, v->max_dvc_qng[i]);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003286 /* Indicate whether the device has returned queue full status. */
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003287 seq_puts(m, "\n Command Queue Full:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003288 for (i = 0; i <= ASC_MAX_TID; i++) {
3289 if ((chip_scsi_id == i) ||
3290 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3291 continue;
3292 }
Al Virob59fb6f2013-03-31 02:59:55 -04003293 if (boardp->queue_full & ADV_TID_TO_TIDMASK(i))
3294 seq_printf(m, " %X:Y-%d",
3295 i, boardp->queue_full_cnt[i]);
3296 else
3297 seq_printf(m, " %X:N", i);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003300 seq_puts(m, "\n Synchronous Transfer:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003301 for (i = 0; i <= ASC_MAX_TID; i++) {
3302 if ((chip_scsi_id == i) ||
3303 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3304 continue;
3305 }
Al Virob59fb6f2013-03-31 02:59:55 -04003306 seq_printf(m, " %X:%c",
3307 i,
3308 (v->sdtr_done & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003309 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003310 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003312 for (i = 0; i <= ASC_MAX_TID; i++) {
3313 uchar syn_period_ix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003315 if ((chip_scsi_id == i) ||
3316 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3317 ((v->init_sdtr & ADV_TID_TO_TIDMASK(i)) == 0)) {
3318 continue;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Al Virob59fb6f2013-03-31 02:59:55 -04003321 seq_printf(m, " %X:", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003323 if ((boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET) == 0) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003324 seq_puts(m, " Asynchronous");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003325 } else {
3326 syn_period_ix =
3327 (boardp->sdtr_data[i] >> 4) & (v->max_sdtr_index -
3328 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Al Virob59fb6f2013-03-31 02:59:55 -04003330 seq_printf(m,
3331 " Transfer Period Factor: %d (%d.%d Mhz),",
3332 v->sdtr_period_tbl[syn_period_ix],
3333 250 / v->sdtr_period_tbl[syn_period_ix],
3334 ASC_TENTHS(250,
3335 v->sdtr_period_tbl[syn_period_ix]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Al Virob59fb6f2013-03-31 02:59:55 -04003337 seq_printf(m, " REQ/ACK Offset: %d",
3338 boardp->sdtr_data[i] & ASC_SYN_MAX_OFFSET);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003341 if ((v->sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003342 seq_puts(m, "*\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003343 renegotiate = 1;
3344 } else {
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003345 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003346 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003349 if (renegotiate) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003350 seq_puts(m, " * = Re-negotiation pending before next command.\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
3354/*
3355 * asc_prt_adv_board_info()
3356 *
3357 * Print dynamic board configuration information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 */
Al Virob59fb6f2013-03-31 02:59:55 -04003359static void asc_prt_adv_board_info(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003361 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003362 int i;
3363 ADV_DVC_VAR *v;
3364 ADV_DVC_CFG *c;
3365 AdvPortAddr iop_base;
3366 ushort chip_scsi_id;
3367 ushort lramword;
3368 uchar lrambyte;
3369 ushort tagqng_able;
3370 ushort sdtr_able, wdtr_able;
3371 ushort wdtr_done, sdtr_done;
3372 ushort period = 0;
3373 int renegotiate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003375 v = &boardp->dvc_var.adv_dvc_var;
3376 c = &boardp->dvc_cfg.adv_dvc_cfg;
3377 iop_base = v->iop_base;
3378 chip_scsi_id = v->chip_scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Al Virob59fb6f2013-03-31 02:59:55 -04003380 seq_printf(m,
3381 "\nAdv Library Configuration and Statistics for AdvanSys SCSI Host %d:\n",
3382 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Al Virob59fb6f2013-03-31 02:59:55 -04003384 seq_printf(m,
3385 " iop_base 0x%lx, cable_detect: %X, err_code %u\n",
Al Viro31491e12013-03-31 03:04:13 -04003386 (unsigned long)v->iop_base,
Al Virob59fb6f2013-03-31 02:59:55 -04003387 AdvReadWordRegister(iop_base,IOPW_SCSI_CFG1) & CABLE_DETECT,
3388 v->err_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Al Virob59fb6f2013-03-31 02:59:55 -04003390 seq_printf(m, " chip_version %u, mcode_date 0x%x, "
3391 "mcode_version 0x%x\n", c->chip_version,
3392 c->mcode_date, c->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003394 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003395 seq_puts(m, " Queuing Enabled:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003396 for (i = 0; i <= ADV_MAX_TID; i++) {
3397 if ((chip_scsi_id == i) ||
3398 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3399 continue;
3400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Al Virob59fb6f2013-03-31 02:59:55 -04003402 seq_printf(m, " %X:%c",
3403 i,
3404 (tagqng_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003407 seq_puts(m, "\n Queue Limit:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003408 for (i = 0; i <= ADV_MAX_TID; i++) {
3409 if ((chip_scsi_id == i) ||
3410 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3411 continue;
3412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003414 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + i,
3415 lrambyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Al Virob59fb6f2013-03-31 02:59:55 -04003417 seq_printf(m, " %X:%d", i, lrambyte);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Rasmus Villemoes3d300792014-12-03 00:10:53 +01003420 seq_puts(m, "\n Command Pending:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003421 for (i = 0; i <= ADV_MAX_TID; i++) {
3422 if ((chip_scsi_id == i) ||
3423 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3424 continue;
3425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003427 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_QUEUED_CMD + i,
3428 lrambyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Al Virob59fb6f2013-03-31 02:59:55 -04003430 seq_printf(m, " %X:%d", i, lrambyte);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003431 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003432 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003434 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003435 seq_puts(m, " Wide Enabled:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003436 for (i = 0; i <= ADV_MAX_TID; i++) {
3437 if ((chip_scsi_id == i) ||
3438 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3439 continue;
3440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Al Virob59fb6f2013-03-31 02:59:55 -04003442 seq_printf(m, " %X:%c",
3443 i,
3444 (wdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003445 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003446 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003448 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, wdtr_done);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003449 seq_puts(m, " Transfer Bit Width:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003450 for (i = 0; i <= ADV_MAX_TID; i++) {
3451 if ((chip_scsi_id == i) ||
3452 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3453 continue;
3454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003456 AdvReadWordLram(iop_base,
3457 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3458 lramword);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Al Virob59fb6f2013-03-31 02:59:55 -04003460 seq_printf(m, " %X:%d",
3461 i, (lramword & 0x8000) ? 16 : 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003463 if ((wdtr_able & ADV_TID_TO_TIDMASK(i)) &&
3464 (wdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003465 seq_putc(m, '*');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003466 renegotiate = 1;
3467 }
3468 }
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003469 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003471 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003472 seq_puts(m, " Synchronous Enabled:");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003473 for (i = 0; i <= ADV_MAX_TID; i++) {
3474 if ((chip_scsi_id == i) ||
3475 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0)) {
3476 continue;
3477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Al Virob59fb6f2013-03-31 02:59:55 -04003479 seq_printf(m, " %X:%c",
3480 i,
3481 (sdtr_able & ADV_TID_TO_TIDMASK(i)) ? 'Y' : 'N');
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_SDTR_DONE, sdtr_done);
3486 for (i = 0; i <= ADV_MAX_TID; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003488 AdvReadWordLram(iop_base,
3489 ASC_MC_DEVICE_HSHK_CFG_TABLE + (2 * i),
3490 lramword);
3491 lramword &= ~0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003493 if ((chip_scsi_id == i) ||
3494 ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(i)) == 0) ||
3495 ((sdtr_able & ADV_TID_TO_TIDMASK(i)) == 0)) {
3496 continue;
3497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Al Virob59fb6f2013-03-31 02:59:55 -04003499 seq_printf(m, " %X:", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003501 if ((lramword & 0x1F) == 0) { /* Check for REQ/ACK Offset 0. */
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003502 seq_puts(m, " Asynchronous");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003503 } else {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003504 seq_puts(m, " Transfer Period Factor: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003506 if ((lramword & 0x1F00) == 0x1100) { /* 80 Mhz */
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003507 seq_puts(m, "9 (80.0 Mhz),");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003508 } else if ((lramword & 0x1F00) == 0x1000) { /* 40 Mhz */
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003509 seq_puts(m, "10 (40.0 Mhz),");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003510 } else { /* 20 Mhz or below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003512 period = (((lramword >> 8) * 25) + 50) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003514 if (period == 0) { /* Should never happen. */
Al Viro31491e12013-03-31 03:04:13 -04003515 seq_printf(m, "%d (? Mhz), ", period);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003516 } else {
Al Virob59fb6f2013-03-31 02:59:55 -04003517 seq_printf(m,
3518 "%d (%d.%d Mhz),",
3519 period, 250 / period,
3520 ASC_TENTHS(250, period));
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003521 }
3522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Al Virob59fb6f2013-03-31 02:59:55 -04003524 seq_printf(m, " REQ/ACK Offset: %d",
3525 lramword & 0x1F);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003528 if ((sdtr_done & ADV_TID_TO_TIDMASK(i)) == 0) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003529 seq_puts(m, "*\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003530 renegotiate = 1;
3531 } else {
Rasmus Villemoesf50332f2014-12-03 00:10:54 +01003532 seq_putc(m, '\n');
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003533 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003536 if (renegotiate) {
Rasmus Villemoes2f979422014-12-03 00:10:50 +01003537 seq_puts(m, " * = Re-negotiation pending before next command.\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539}
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541#ifdef ADVANSYS_STATS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542/*
3543 * asc_prt_board_stats()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 */
Al Virob59fb6f2013-03-31 02:59:55 -04003545static void asc_prt_board_stats(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003547 struct asc_board *boardp = shost_priv(shost);
3548 struct asc_stats *s = &boardp->asc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Al Virob59fb6f2013-03-31 02:59:55 -04003550 seq_printf(m,
3551 "\nLinux Driver Statistics for AdvanSys SCSI Host %d:\n",
3552 shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Al Virob59fb6f2013-03-31 02:59:55 -04003554 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003555 " queuecommand %u, reset %u, biosparam %u, interrupt %u\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003556 s->queuecommand, s->reset, s->biosparam,
3557 s->interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Al Virob59fb6f2013-03-31 02:59:55 -04003559 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003560 " callback %u, done %u, build_error %u, build_noreq %u, build_nosg %u\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003561 s->callback, s->done, s->build_error,
3562 s->adv_build_noreq, s->adv_build_nosg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Al Virob59fb6f2013-03-31 02:59:55 -04003564 seq_printf(m,
Al Viro31491e12013-03-31 03:04:13 -04003565 " exe_noerror %u, exe_busy %u, exe_error %u, exe_unknown %u\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003566 s->exe_noerror, s->exe_busy, s->exe_error,
3567 s->exe_unknown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003569 /*
3570 * Display data transfer statistics.
3571 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04003572 if (s->xfer_cnt > 0) {
Al Viro31491e12013-03-31 03:04:13 -04003573 seq_printf(m, " xfer_cnt %u, xfer_elem %u, ",
Al Virob59fb6f2013-03-31 02:59:55 -04003574 s->xfer_cnt, s->xfer_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Al Viro31491e12013-03-31 03:04:13 -04003576 seq_printf(m, "xfer_bytes %u.%01u kb\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003577 s->xfer_sect / 2, ASC_TENTHS(s->xfer_sect, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003579 /* Scatter gather transfer statistics */
Al Viro31491e12013-03-31 03:04:13 -04003580 seq_printf(m, " avg_num_elem %u.%01u, ",
Al Virob59fb6f2013-03-31 02:59:55 -04003581 s->xfer_elem / s->xfer_cnt,
3582 ASC_TENTHS(s->xfer_elem, s->xfer_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Al Viro31491e12013-03-31 03:04:13 -04003584 seq_printf(m, "avg_elem_size %u.%01u kb, ",
Al Virob59fb6f2013-03-31 02:59:55 -04003585 (s->xfer_sect / 2) / s->xfer_elem,
3586 ASC_TENTHS((s->xfer_sect / 2), s->xfer_elem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Al Viro31491e12013-03-31 03:04:13 -04003588 seq_printf(m, "avg_xfer_size %u.%01u kb\n",
Al Virob59fb6f2013-03-31 02:59:55 -04003589 (s->xfer_sect / 2) / s->xfer_cnt,
3590 ASC_TENTHS((s->xfer_sect / 2), s->xfer_cnt));
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593#endif /* ADVANSYS_STATS */
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595/*
Al Virob59fb6f2013-03-31 02:59:55 -04003596 * advansys_show_info() - /proc/scsi/advansys/{0,1,2,3,...}
Matthew Wilcox51219352007-10-02 21:55:22 -04003597 *
Al Virob59fb6f2013-03-31 02:59:55 -04003598 * m: seq_file to print into
3599 * shost: Scsi_Host
Matthew Wilcox51219352007-10-02 21:55:22 -04003600 *
3601 * Return the number of bytes read from or written to a
3602 * /proc/scsi/advansys/[0...] file.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 */
Matthew Wilcox51219352007-10-02 21:55:22 -04003604static int
Al Virob59fb6f2013-03-31 02:59:55 -04003605advansys_show_info(struct seq_file *m, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04003607 struct asc_board *boardp = shost_priv(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003609 ASC_DBG(1, "begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Matthew Wilcox51219352007-10-02 21:55:22 -04003611 /*
Matthew Wilcox51219352007-10-02 21:55:22 -04003612 * User read of /proc/scsi/advansys/[0...] file.
3613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Matthew Wilcox51219352007-10-02 21:55:22 -04003615 /*
3616 * Get board configuration information.
3617 *
3618 * advansys_info() returns the board string from its own static buffer.
3619 */
Matthew Wilcox51219352007-10-02 21:55:22 -04003620 /* Copy board information. */
Al Virob59fb6f2013-03-31 02:59:55 -04003621 seq_printf(m, "%s\n", (char *)advansys_info(shost));
Matthew Wilcox51219352007-10-02 21:55:22 -04003622 /*
3623 * Display Wide Board BIOS Information.
3624 */
Al Virob59fb6f2013-03-31 02:59:55 -04003625 if (!ASC_NARROW_BOARD(boardp))
3626 asc_prt_adv_bios(m, shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04003627
3628 /*
3629 * Display driver information for each device attached to the board.
3630 */
Al Virob59fb6f2013-03-31 02:59:55 -04003631 asc_prt_board_devices(m, shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04003632
3633 /*
3634 * Display EEPROM configuration for the board.
3635 */
Al Virob59fb6f2013-03-31 02:59:55 -04003636 if (ASC_NARROW_BOARD(boardp))
3637 asc_prt_asc_board_eeprom(m, shost);
3638 else
3639 asc_prt_adv_board_eeprom(m, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Matthew Wilcox51219352007-10-02 21:55:22 -04003641 /*
3642 * Display driver configuration and information for the board.
3643 */
Al Virob59fb6f2013-03-31 02:59:55 -04003644 asc_prt_driver_conf(m, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Matthew Wilcox51219352007-10-02 21:55:22 -04003646#ifdef ADVANSYS_STATS
3647 /*
3648 * Display driver statistics for the board.
3649 */
Al Virob59fb6f2013-03-31 02:59:55 -04003650 asc_prt_board_stats(m, shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04003651#endif /* ADVANSYS_STATS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Matthew Wilcox51219352007-10-02 21:55:22 -04003653 /*
3654 * Display Asc Library dynamic configuration information
3655 * for the board.
3656 */
Al Virob59fb6f2013-03-31 02:59:55 -04003657 if (ASC_NARROW_BOARD(boardp))
3658 asc_prt_asc_board_info(m, shost);
3659 else
3660 asc_prt_adv_board_info(m, shost);
3661 return 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04003662}
3663#endif /* CONFIG_PROC_FS */
3664
3665static void asc_scsi_done(struct scsi_cmnd *scp)
3666{
Matthew Wilcox52c334e2007-10-02 21:55:39 -04003667 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04003668 ASC_STATS(scp->device->host, done);
Matthew Wilcox51219352007-10-02 21:55:22 -04003669 scp->scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Matthew Wilcox51219352007-10-02 21:55:22 -04003672static void AscSetBank(PortAddr iop_base, uchar bank)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Matthew Wilcox51219352007-10-02 21:55:22 -04003674 uchar val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Matthew Wilcox51219352007-10-02 21:55:22 -04003676 val = AscGetChipControl(iop_base) &
3677 (~
3678 (CC_SINGLE_STEP | CC_TEST | CC_DIAG | CC_SCSI_RESET |
3679 CC_CHIP_RESET));
3680 if (bank == 1) {
3681 val |= CC_BANK_ONE;
3682 } else if (bank == 2) {
3683 val |= CC_DIAG | CC_BANK_ONE;
3684 } else {
3685 val &= ~CC_BANK_ONE;
3686 }
3687 AscSetChipControl(iop_base, val);
Matthew Wilcox51219352007-10-02 21:55:22 -04003688}
3689
3690static void AscSetChipIH(PortAddr iop_base, ushort ins_code)
3691{
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003692 AscSetBank(iop_base, 1);
Matthew Wilcox51219352007-10-02 21:55:22 -04003693 AscWriteChipIH(iop_base, ins_code);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003694 AscSetBank(iop_base, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695}
3696
Matthew Wilcox51219352007-10-02 21:55:22 -04003697static int AscStartChip(PortAddr iop_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Matthew Wilcox51219352007-10-02 21:55:22 -04003699 AscSetChipControl(iop_base, 0);
3700 if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3701 return (0);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003702 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003703 return (1);
3704}
3705
Hannes Reineckeae267592015-04-24 13:18:28 +02003706static bool AscStopChip(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04003707{
3708 uchar cc_val;
3709
3710 cc_val =
3711 AscGetChipControl(iop_base) &
3712 (~(CC_SINGLE_STEP | CC_TEST | CC_DIAG));
3713 AscSetChipControl(iop_base, (uchar)(cc_val | CC_HALT));
3714 AscSetChipIH(iop_base, INS_HALT);
3715 AscSetChipIH(iop_base, INS_RFLAG_WTM);
3716 if ((AscGetChipStatus(iop_base) & CSW_HALTED) == 0) {
Hannes Reineckeae267592015-04-24 13:18:28 +02003717 return false;
Matthew Wilcox51219352007-10-02 21:55:22 -04003718 }
Hannes Reineckeae267592015-04-24 13:18:28 +02003719 return true;
Matthew Wilcox51219352007-10-02 21:55:22 -04003720}
3721
Hannes Reinecked647c782015-04-24 13:18:26 +02003722static bool AscIsChipHalted(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04003723{
3724 if ((AscGetChipStatus(iop_base) & CSW_HALTED) != 0) {
3725 if ((AscGetChipControl(iop_base) & CC_HALT) != 0) {
Hannes Reinecked647c782015-04-24 13:18:26 +02003726 return true;
Matthew Wilcox51219352007-10-02 21:55:22 -04003727 }
3728 }
Hannes Reinecked647c782015-04-24 13:18:26 +02003729 return false;
Matthew Wilcox51219352007-10-02 21:55:22 -04003730}
3731
Hannes Reinecked647c782015-04-24 13:18:26 +02003732static bool AscResetChipAndScsiBus(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04003733{
3734 PortAddr iop_base;
3735 int i = 10;
3736
3737 iop_base = asc_dvc->iop_base;
3738 while ((AscGetChipStatus(iop_base) & CSW_SCSI_RESET_ACTIVE)
3739 && (i-- > 0)) {
3740 mdelay(100);
3741 }
3742 AscStopChip(iop_base);
3743 AscSetChipControl(iop_base, CC_CHIP_RESET | CC_SCSI_RESET | CC_HALT);
3744 udelay(60);
3745 AscSetChipIH(iop_base, INS_RFLAG_WTM);
3746 AscSetChipIH(iop_base, INS_HALT);
3747 AscSetChipControl(iop_base, CC_CHIP_RESET | CC_HALT);
3748 AscSetChipControl(iop_base, CC_HALT);
3749 mdelay(200);
3750 AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
3751 AscSetChipStatus(iop_base, 0);
3752 return (AscIsChipHalted(iop_base));
3753}
3754
3755static int AscFindSignature(PortAddr iop_base)
3756{
3757 ushort sig_word;
3758
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003759 ASC_DBG(1, "AscGetChipSignatureByte(0x%x) 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04003760 iop_base, AscGetChipSignatureByte(iop_base));
3761 if (AscGetChipSignatureByte(iop_base) == (uchar)ASC_1000_ID1B) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003762 ASC_DBG(1, "AscGetChipSignatureWord(0x%x) 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04003763 iop_base, AscGetChipSignatureWord(iop_base));
3764 sig_word = AscGetChipSignatureWord(iop_base);
3765 if ((sig_word == (ushort)ASC_1000_ID0W) ||
3766 (sig_word == (ushort)ASC_1000_ID0W_FIX)) {
3767 return (1);
3768 }
3769 }
3770 return (0);
3771}
3772
3773static void AscEnableInterrupt(PortAddr iop_base)
3774{
3775 ushort cfg;
3776
3777 cfg = AscGetChipCfgLsw(iop_base);
3778 AscSetChipCfgLsw(iop_base, cfg | ASC_CFG0_HOST_INT_ON);
Matthew Wilcox51219352007-10-02 21:55:22 -04003779}
3780
3781static void AscDisableInterrupt(PortAddr iop_base)
3782{
3783 ushort cfg;
3784
3785 cfg = AscGetChipCfgLsw(iop_base);
3786 AscSetChipCfgLsw(iop_base, cfg & (~ASC_CFG0_HOST_INT_ON));
Matthew Wilcox51219352007-10-02 21:55:22 -04003787}
3788
3789static uchar AscReadLramByte(PortAddr iop_base, ushort addr)
3790{
3791 unsigned char byte_data;
3792 unsigned short word_data;
3793
3794 if (isodd_word(addr)) {
3795 AscSetChipLramAddr(iop_base, addr - 1);
3796 word_data = AscGetChipLramData(iop_base);
3797 byte_data = (word_data >> 8) & 0xFF;
3798 } else {
3799 AscSetChipLramAddr(iop_base, addr);
3800 word_data = AscGetChipLramData(iop_base);
3801 byte_data = word_data & 0xFF;
3802 }
3803 return byte_data;
3804}
3805
3806static ushort AscReadLramWord(PortAddr iop_base, ushort addr)
3807{
3808 ushort word_data;
3809
3810 AscSetChipLramAddr(iop_base, addr);
3811 word_data = AscGetChipLramData(iop_base);
3812 return (word_data);
3813}
3814
Matthew Wilcox51219352007-10-02 21:55:22 -04003815static void
3816AscMemWordSetLram(PortAddr iop_base, ushort s_addr, ushort set_wval, int words)
3817{
3818 int i;
3819
3820 AscSetChipLramAddr(iop_base, s_addr);
3821 for (i = 0; i < words; i++) {
3822 AscSetChipLramData(iop_base, set_wval);
3823 }
3824}
3825
3826static void AscWriteLramWord(PortAddr iop_base, ushort addr, ushort word_val)
3827{
3828 AscSetChipLramAddr(iop_base, addr);
3829 AscSetChipLramData(iop_base, word_val);
Matthew Wilcox51219352007-10-02 21:55:22 -04003830}
3831
3832static void AscWriteLramByte(PortAddr iop_base, ushort addr, uchar byte_val)
3833{
3834 ushort word_data;
3835
3836 if (isodd_word(addr)) {
3837 addr--;
3838 word_data = AscReadLramWord(iop_base, addr);
3839 word_data &= 0x00FF;
3840 word_data |= (((ushort)byte_val << 8) & 0xFF00);
3841 } else {
3842 word_data = AscReadLramWord(iop_base, addr);
3843 word_data &= 0xFF00;
3844 word_data |= ((ushort)byte_val & 0x00FF);
3845 }
3846 AscWriteLramWord(iop_base, addr, word_data);
Matthew Wilcox51219352007-10-02 21:55:22 -04003847}
3848
3849/*
3850 * Copy 2 bytes to LRAM.
3851 *
3852 * The source data is assumed to be in little-endian order in memory
3853 * and is maintained in little-endian order when written to LRAM.
3854 */
3855static void
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05303856AscMemWordCopyPtrToLram(PortAddr iop_base, ushort s_addr,
3857 const uchar *s_buffer, int words)
Matthew Wilcox51219352007-10-02 21:55:22 -04003858{
3859 int i;
3860
3861 AscSetChipLramAddr(iop_base, s_addr);
3862 for (i = 0; i < 2 * words; i += 2) {
3863 /*
3864 * On a little-endian system the second argument below
3865 * produces a little-endian ushort which is written to
3866 * LRAM in little-endian order. On a big-endian system
3867 * the second argument produces a big-endian ushort which
3868 * is "transparently" byte-swapped by outpw() and written
3869 * in little-endian order to LRAM.
3870 */
3871 outpw(iop_base + IOP_RAM_DATA,
3872 ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]);
3873 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003874}
3875
3876/*
3877 * Copy 4 bytes to LRAM.
3878 *
3879 * The source data is assumed to be in little-endian order in memory
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003880 * and is maintained in little-endian order when written to LRAM.
Matthew Wilcox51219352007-10-02 21:55:22 -04003881 */
3882static void
3883AscMemDWordCopyPtrToLram(PortAddr iop_base,
3884 ushort s_addr, uchar *s_buffer, int dwords)
3885{
3886 int i;
3887
3888 AscSetChipLramAddr(iop_base, s_addr);
3889 for (i = 0; i < 4 * dwords; i += 4) {
3890 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 1] << 8) | s_buffer[i]); /* LSW */
3891 outpw(iop_base + IOP_RAM_DATA, ((ushort)s_buffer[i + 3] << 8) | s_buffer[i + 2]); /* MSW */
3892 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003893}
3894
3895/*
3896 * Copy 2 bytes from LRAM.
3897 *
3898 * The source data is assumed to be in little-endian order in LRAM
3899 * and is maintained in little-endian order when written to memory.
3900 */
3901static void
3902AscMemWordCopyPtrFromLram(PortAddr iop_base,
3903 ushort s_addr, uchar *d_buffer, int words)
3904{
3905 int i;
3906 ushort word;
3907
3908 AscSetChipLramAddr(iop_base, s_addr);
3909 for (i = 0; i < 2 * words; i += 2) {
3910 word = inpw(iop_base + IOP_RAM_DATA);
3911 d_buffer[i] = word & 0xff;
3912 d_buffer[i + 1] = (word >> 8) & 0xff;
3913 }
Matthew Wilcox51219352007-10-02 21:55:22 -04003914}
3915
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003916static u32 AscMemSumLramWord(PortAddr iop_base, ushort s_addr, int words)
Matthew Wilcox51219352007-10-02 21:55:22 -04003917{
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003918 u32 sum = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04003919 int i;
3920
Matthew Wilcox51219352007-10-02 21:55:22 -04003921 for (i = 0; i < words; i++, s_addr += 2) {
3922 sum += AscReadLramWord(iop_base, s_addr);
3923 }
3924 return (sum);
3925}
3926
Hannes Reineckef33134e2015-04-24 13:18:31 +02003927static void AscInitLram(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04003928{
3929 uchar i;
3930 ushort s_addr;
3931 PortAddr iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04003932
3933 iop_base = asc_dvc->iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04003934 AscMemWordSetLram(iop_base, ASC_QADR_BEG, 0,
3935 (ushort)(((int)(asc_dvc->max_total_qng + 2 + 1) *
3936 64) >> 1));
3937 i = ASC_MIN_ACTIVE_QNO;
3938 s_addr = ASC_QADR_BEG + ASC_QBLK_SIZE;
3939 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
3940 (uchar)(i + 1));
3941 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
3942 (uchar)(asc_dvc->max_total_qng));
3943 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
3944 (uchar)i);
3945 i++;
3946 s_addr += ASC_QBLK_SIZE;
3947 for (; i < asc_dvc->max_total_qng; i++, s_addr += ASC_QBLK_SIZE) {
3948 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
3949 (uchar)(i + 1));
3950 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
3951 (uchar)(i - 1));
3952 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
3953 (uchar)i);
3954 }
3955 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_FWD),
3956 (uchar)ASC_QLINK_END);
3957 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_BWD),
3958 (uchar)(asc_dvc->max_total_qng - 1));
3959 AscWriteLramByte(iop_base, (ushort)(s_addr + ASC_SCSIQ_B_QNO),
3960 (uchar)asc_dvc->max_total_qng);
3961 i++;
3962 s_addr += ASC_QBLK_SIZE;
3963 for (; i <= (uchar)(asc_dvc->max_total_qng + 3);
3964 i++, s_addr += ASC_QBLK_SIZE) {
3965 AscWriteLramByte(iop_base,
3966 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_FWD), i);
3967 AscWriteLramByte(iop_base,
3968 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_BWD), i);
3969 AscWriteLramByte(iop_base,
3970 (ushort)(s_addr + (ushort)ASC_SCSIQ_B_QNO), i);
3971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003974static u32
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05303975AscLoadMicroCode(PortAddr iop_base, ushort s_addr,
3976 const uchar *mcode_buf, ushort mcode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
Hannes Reinecke95cfab62015-04-24 13:18:27 +02003978 u32 chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003979 ushort mcode_word_size;
3980 ushort mcode_chksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003982 /* Write the microcode buffer starting at LRAM address 0. */
3983 mcode_word_size = (ushort)(mcode_size >> 1);
3984 AscMemWordSetLram(iop_base, s_addr, 0, mcode_word_size);
3985 AscMemWordCopyPtrToLram(iop_base, s_addr, mcode_buf, mcode_word_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003987 chksum = AscMemSumLramWord(iop_base, s_addr, mcode_word_size);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003988 ASC_DBG(1, "chksum 0x%lx\n", (ulong)chksum);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003989 mcode_chksum = (ushort)AscMemSumLramWord(iop_base,
3990 (ushort)ASC_CODE_SEC_BEG,
3991 (ushort)((mcode_size -
3992 s_addr - (ushort)
3993 ASC_CODE_SEC_BEG) /
3994 2));
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003995 ASC_DBG(1, "mcode_chksum 0x%lx\n", (ulong)mcode_chksum);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04003996 AscWriteLramWord(iop_base, ASCV_MCODE_CHKSUM_W, mcode_chksum);
3997 AscWriteLramWord(iop_base, ASCV_MCODE_SIZE_W, mcode_size);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04003998 return chksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
Matthew Wilcox51219352007-10-02 21:55:22 -04004001static void AscInitQLinkVar(ASC_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Matthew Wilcox51219352007-10-02 21:55:22 -04004003 PortAddr iop_base;
4004 int i;
4005 ushort lram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Matthew Wilcox51219352007-10-02 21:55:22 -04004007 iop_base = asc_dvc->iop_base;
4008 AscPutRiscVarFreeQHead(iop_base, 1);
4009 AscPutRiscVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4010 AscPutVarFreeQHead(iop_base, 1);
4011 AscPutVarDoneQTail(iop_base, asc_dvc->max_total_qng);
4012 AscWriteLramByte(iop_base, ASCV_BUSY_QHEAD_B,
4013 (uchar)((int)asc_dvc->max_total_qng + 1));
4014 AscWriteLramByte(iop_base, ASCV_DISC1_QHEAD_B,
4015 (uchar)((int)asc_dvc->max_total_qng + 2));
4016 AscWriteLramByte(iop_base, (ushort)ASCV_TOTAL_READY_Q_B,
4017 asc_dvc->max_total_qng);
4018 AscWriteLramWord(iop_base, ASCV_ASCDVC_ERR_CODE_W, 0);
4019 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
4020 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, 0);
4021 AscWriteLramByte(iop_base, ASCV_SCSIBUSY_B, 0);
4022 AscWriteLramByte(iop_base, ASCV_WTM_FLAG_B, 0);
4023 AscPutQDoneInProgress(iop_base, 0);
4024 lram_addr = ASC_QADR_BEG;
4025 for (i = 0; i < 32; i++, lram_addr += 2) {
4026 AscWriteLramWord(iop_base, lram_addr, 0);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Hannes Reineckef33134e2015-04-24 13:18:31 +02004030static int AscInitMicroCodeVar(ASC_DVC_VAR *asc_dvc)
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004031{
Matthew Wilcox51219352007-10-02 21:55:22 -04004032 int i;
Hannes Reineckef33134e2015-04-24 13:18:31 +02004033 int warn_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04004034 PortAddr iop_base;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004035 __le32 phy_addr;
4036 __le32 phy_size;
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004037 struct asc_board *board = asc_dvc_to_board(asc_dvc);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004038
Matthew Wilcox51219352007-10-02 21:55:22 -04004039 iop_base = asc_dvc->iop_base;
4040 warn_code = 0;
4041 for (i = 0; i <= ASC_MAX_TID; i++) {
4042 AscPutMCodeInitSDTRAtID(iop_base, i,
4043 asc_dvc->cfg->sdtr_period_offset[i]);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004044 }
4045
Matthew Wilcox51219352007-10-02 21:55:22 -04004046 AscInitQLinkVar(asc_dvc);
4047 AscWriteLramByte(iop_base, ASCV_DISC_ENABLE_B,
4048 asc_dvc->cfg->disc_enable);
4049 AscWriteLramByte(iop_base, ASCV_HOSTSCSI_ID_B,
4050 ASC_TID_TO_TARGET_ID(asc_dvc->cfg->chip_scsi_id));
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004051
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004052 /* Ensure overrun buffer is aligned on an 8 byte boundary. */
4053 BUG_ON((unsigned long)asc_dvc->overrun_buf & 7);
4054 asc_dvc->overrun_dma = dma_map_single(board->dev, asc_dvc->overrun_buf,
4055 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004056 if (dma_mapping_error(board->dev, asc_dvc->overrun_dma)) {
4057 warn_code = -ENOMEM;
4058 goto err_dma_map;
4059 }
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004060 phy_addr = cpu_to_le32(asc_dvc->overrun_dma);
Matthew Wilcox51219352007-10-02 21:55:22 -04004061 AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_PADDR_D,
4062 (uchar *)&phy_addr, 1);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -04004063 phy_size = cpu_to_le32(ASC_OVERRUN_BSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04004064 AscMemDWordCopyPtrToLram(iop_base, ASCV_OVERRUN_BSIZE_D,
4065 (uchar *)&phy_size, 1);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004066
Matthew Wilcox51219352007-10-02 21:55:22 -04004067 asc_dvc->cfg->mcode_date =
4068 AscReadLramWord(iop_base, (ushort)ASCV_MC_DATE_W);
4069 asc_dvc->cfg->mcode_version =
4070 AscReadLramWord(iop_base, (ushort)ASCV_MC_VER_W);
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004071
Matthew Wilcox51219352007-10-02 21:55:22 -04004072 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
4073 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
4074 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
Hannes Reineckef33134e2015-04-24 13:18:31 +02004075 warn_code = -EINVAL;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004076 goto err_mcode_start;
Matthew Wilcox51219352007-10-02 21:55:22 -04004077 }
4078 if (AscStartChip(iop_base) != 1) {
4079 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
Hannes Reineckef33134e2015-04-24 13:18:31 +02004080 warn_code = -EIO;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004081 goto err_mcode_start;
Matthew Wilcox51219352007-10-02 21:55:22 -04004082 }
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004083
Matthew Wilcox51219352007-10-02 21:55:22 -04004084 return warn_code;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004085
4086err_mcode_start:
4087 dma_unmap_single(board->dev, asc_dvc->overrun_dma,
4088 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
4089err_dma_map:
4090 asc_dvc->overrun_dma = 0;
4091 return warn_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04004092}
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004093
Hannes Reineckef33134e2015-04-24 13:18:31 +02004094static int AscInitAsc1000Driver(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04004095{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304096 const struct firmware *fw;
4097 const char fwname[] = "advansys/mcode.bin";
4098 int err;
4099 unsigned long chksum;
Hannes Reineckef33134e2015-04-24 13:18:31 +02004100 int warn_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04004101 PortAddr iop_base;
4102
4103 iop_base = asc_dvc->iop_base;
4104 warn_code = 0;
4105 if ((asc_dvc->dvc_cntl & ASC_CNTL_RESET_SCSI) &&
4106 !(asc_dvc->init_state & ASC_INIT_RESET_SCSI_DONE)) {
4107 AscResetChipAndScsiBus(asc_dvc);
4108 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4109 }
4110 asc_dvc->init_state |= ASC_INIT_STATE_BEG_LOAD_MC;
4111 if (asc_dvc->err_code != 0)
Hannes Reineckef33134e2015-04-24 13:18:31 +02004112 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04004113 if (!AscFindSignature(asc_dvc->iop_base)) {
4114 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
4115 return warn_code;
4116 }
4117 AscDisableInterrupt(iop_base);
Hannes Reineckef33134e2015-04-24 13:18:31 +02004118 AscInitLram(asc_dvc);
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304119
4120 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4121 if (err) {
4122 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4123 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004124 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304125 return err;
4126 }
4127 if (fw->size < 4) {
4128 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4129 fw->size, fwname);
4130 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004131 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304132 return -EINVAL;
4133 }
4134 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4135 (fw->data[1] << 8) | fw->data[0];
4136 ASC_DBG(1, "_asc_mcode_chksum 0x%lx\n", (ulong)chksum);
4137 if (AscLoadMicroCode(iop_base, 0, &fw->data[4],
4138 fw->size - 4) != chksum) {
Matthew Wilcox51219352007-10-02 21:55:22 -04004139 asc_dvc->err_code |= ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304140 release_firmware(fw);
Matthew Wilcox51219352007-10-02 21:55:22 -04004141 return warn_code;
4142 }
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304143 release_firmware(fw);
Matthew Wilcox51219352007-10-02 21:55:22 -04004144 warn_code |= AscInitMicroCodeVar(asc_dvc);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03004145 if (!asc_dvc->overrun_dma)
4146 return warn_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04004147 asc_dvc->init_state |= ASC_INIT_STATE_END_LOAD_MC;
4148 AscEnableInterrupt(iop_base);
4149 return warn_code;
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004150}
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152/*
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004153 * Load the Microcode
4154 *
4155 * Write the microcode image to RISC memory starting at address 0.
4156 *
4157 * The microcode is stored compressed in the following format:
4158 *
4159 * 254 word (508 byte) table indexed by byte code followed
4160 * by the following byte codes:
4161 *
4162 * 1-Byte Code:
4163 * 00: Emit word 0 in table.
4164 * 01: Emit word 1 in table.
4165 * .
4166 * FD: Emit word 253 in table.
4167 *
4168 * Multi-Byte Code:
4169 * FE WW WW: (3 byte code) Word to emit is the next word WW WW.
4170 * FF BB WW WW: (4 byte code) Emit BB count times next word WW WW.
4171 *
4172 * Returns 0 or an error if the checksum doesn't match
4173 */
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304174static int AdvLoadMicrocode(AdvPortAddr iop_base, const unsigned char *buf,
4175 int size, int memsize, int chksum)
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004176{
4177 int i, j, end, len = 0;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004178 u32 sum;
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004179
4180 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4181
4182 for (i = 253 * 2; i < size; i++) {
4183 if (buf[i] == 0xff) {
4184 unsigned short word = (buf[i + 3] << 8) | buf[i + 2];
4185 for (j = 0; j < buf[i + 1]; j++) {
4186 AdvWriteWordAutoIncLram(iop_base, word);
4187 len += 2;
4188 }
4189 i += 3;
4190 } else if (buf[i] == 0xfe) {
4191 unsigned short word = (buf[i + 2] << 8) | buf[i + 1];
4192 AdvWriteWordAutoIncLram(iop_base, word);
4193 i += 2;
4194 len += 2;
4195 } else {
Matthew Wilcox951b62c2007-10-05 15:57:06 -04004196 unsigned int off = buf[i] * 2;
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004197 unsigned short word = (buf[off + 1] << 8) | buf[off];
4198 AdvWriteWordAutoIncLram(iop_base, word);
4199 len += 2;
4200 }
4201 }
4202
4203 end = len;
4204
4205 while (len < memsize) {
4206 AdvWriteWordAutoIncLram(iop_base, 0);
4207 len += 2;
4208 }
4209
4210 /* Verify the microcode checksum. */
4211 sum = 0;
4212 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, 0);
4213
4214 for (len = 0; len < end; len += 2) {
4215 sum += AdvReadWordAutoIncLram(iop_base);
4216 }
4217
4218 if (sum != chksum)
4219 return ASC_IERR_MCODE_CHKSUM;
4220
4221 return 0;
4222}
4223
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004224static void AdvBuildCarrierFreelist(struct adv_dvc_var *adv_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04004225{
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004226 off_t carr_offset = 0, next_offset;
4227 dma_addr_t carr_paddr;
4228 int carr_num = ADV_CARRIER_BUFSIZE / sizeof(ADV_CARR_T), i;
Matthew Wilcox51219352007-10-02 21:55:22 -04004229
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004230 for (i = 0; i < carr_num; i++) {
4231 carr_offset = i * sizeof(ADV_CARR_T);
4232 /* Get physical address of the carrier 'carrp'. */
4233 carr_paddr = adv_dvc->carrier_addr + carr_offset;
4234
4235 adv_dvc->carrier[i].carr_pa = cpu_to_le32(carr_paddr);
4236 adv_dvc->carrier[i].carr_va = cpu_to_le32(carr_offset);
4237 adv_dvc->carrier[i].areq_vpa = 0;
4238 next_offset = carr_offset + sizeof(ADV_CARR_T);
4239 if (i == carr_num)
4240 next_offset = ~0;
4241 adv_dvc->carrier[i].next_vpa = cpu_to_le32(next_offset);
4242 }
4243 /*
4244 * We cannot have a carrier with 'carr_va' of '0', as
4245 * a reference to this carrier would be interpreted as
4246 * list termination.
4247 * So start at carrier 1 with the freelist.
4248 */
4249 adv_dvc->carr_freelist = &adv_dvc->carrier[1];
4250}
4251
4252static ADV_CARR_T *adv_get_carrier(struct adv_dvc_var *adv_dvc, u32 offset)
4253{
4254 int index;
4255
4256 BUG_ON(offset > ADV_CARRIER_BUFSIZE);
4257
4258 index = offset / sizeof(ADV_CARR_T);
4259 return &adv_dvc->carrier[index];
4260}
4261
4262static ADV_CARR_T *adv_get_next_carrier(struct adv_dvc_var *adv_dvc)
4263{
4264 ADV_CARR_T *carrp = adv_dvc->carr_freelist;
4265 u32 next_vpa = le32_to_cpu(carrp->next_vpa);
4266
4267 if (next_vpa == 0 || next_vpa == ~0) {
4268 ASC_DBG(1, "invalid vpa offset 0x%x\n", next_vpa);
4269 return NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04004270 }
4271
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004272 adv_dvc->carr_freelist = adv_get_carrier(adv_dvc, next_vpa);
4273 /*
4274 * insert stopper carrier to terminate list
4275 */
Hannes Reinecke084e6c32015-04-24 13:18:35 +02004276 carrp->next_vpa = cpu_to_le32(ADV_CQ_STOPPER);
Matthew Wilcox51219352007-10-02 21:55:22 -04004277
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004278 return carrp;
Matthew Wilcox51219352007-10-02 21:55:22 -04004279}
4280
4281/*
Hannes Reinecke4b47e462015-04-24 13:18:24 +02004282 * 'offset' is the index in the request pointer array
4283 */
4284static adv_req_t * adv_get_reqp(struct adv_dvc_var *adv_dvc, u32 offset)
4285{
4286 struct asc_board *boardp = adv_dvc->drv_ptr;
4287
4288 BUG_ON(offset > adv_dvc->max_host_qng);
4289 return &boardp->adv_reqp[offset];
4290}
4291
4292/*
Matthew Wilcox51219352007-10-02 21:55:22 -04004293 * Send an idle command to the chip and wait for completion.
4294 *
4295 * Command completion is polled for once per microsecond.
4296 *
4297 * The function can be called from anywhere including an interrupt handler.
4298 * But the function is not re-entrant, so it uses the DvcEnter/LeaveCritical()
4299 * functions to prevent reentrancy.
4300 *
4301 * Return Values:
4302 * ADV_TRUE - command completed successfully
4303 * ADV_FALSE - command failed
4304 * ADV_ERROR - command timed out
4305 */
4306static int
4307AdvSendIdleCmd(ADV_DVC_VAR *asc_dvc,
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004308 ushort idle_cmd, u32 idle_cmd_parameter)
Matthew Wilcox51219352007-10-02 21:55:22 -04004309{
Hannes Reinecke95cfab62015-04-24 13:18:27 +02004310 int result, i, j;
Matthew Wilcox51219352007-10-02 21:55:22 -04004311 AdvPortAddr iop_base;
4312
4313 iop_base = asc_dvc->iop_base;
4314
4315 /*
4316 * Clear the idle command status which is set by the microcode
4317 * to a non-zero value to indicate when the command is completed.
4318 * The non-zero result is one of the IDLE_CMD_STATUS_* values
4319 */
4320 AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS, (ushort)0);
4321
4322 /*
4323 * Write the idle command value after the idle command parameter
4324 * has been written to avoid a race condition. If the order is not
4325 * followed, the microcode may process the idle command before the
4326 * parameters have been written to LRAM.
4327 */
4328 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IDLE_CMD_PARAMETER,
4329 cpu_to_le32(idle_cmd_parameter));
4330 AdvWriteWordLram(iop_base, ASC_MC_IDLE_CMD, idle_cmd);
4331
4332 /*
4333 * Tickle the RISC to tell it to process the idle command.
4334 */
4335 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_B);
4336 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
4337 /*
4338 * Clear the tickle value. In the ASC-3550 the RISC flag
4339 * command 'clr_tickle_b' does not work unless the host
4340 * value is cleared.
4341 */
4342 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_NOP);
4343 }
4344
4345 /* Wait for up to 100 millisecond for the idle command to timeout. */
4346 for (i = 0; i < SCSI_WAIT_100_MSEC; i++) {
4347 /* Poll once each microsecond for command completion. */
4348 for (j = 0; j < SCSI_US_PER_MSEC; j++) {
4349 AdvReadWordLram(iop_base, ASC_MC_IDLE_CMD_STATUS,
4350 result);
4351 if (result != 0)
4352 return result;
4353 udelay(1);
4354 }
4355 }
4356
4357 BUG(); /* The idle command should never timeout. */
4358 return ADV_ERROR;
4359}
4360
4361/*
4362 * Reset SCSI Bus and purge all outstanding requests.
4363 *
4364 * Return Value:
4365 * ADV_TRUE(1) - All requests are purged and SCSI Bus is reset.
4366 * ADV_FALSE(0) - Microcode command failed.
4367 * ADV_ERROR(-1) - Microcode command timed-out. Microcode or IC
4368 * may be hung which requires driver recovery.
4369 */
4370static int AdvResetSB(ADV_DVC_VAR *asc_dvc)
4371{
4372 int status;
4373
4374 /*
4375 * Send the SCSI Bus Reset idle start idle command which asserts
4376 * the SCSI Bus Reset signal.
4377 */
4378 status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_START, 0L);
4379 if (status != ADV_TRUE) {
4380 return status;
4381 }
4382
4383 /*
4384 * Delay for the specified SCSI Bus Reset hold time.
4385 *
4386 * The hold time delay is done on the host because the RISC has no
4387 * microsecond accurate timer.
4388 */
4389 udelay(ASC_SCSI_RESET_HOLD_TIME_US);
4390
4391 /*
4392 * Send the SCSI Bus Reset end idle command which de-asserts
4393 * the SCSI Bus Reset signal and purges any pending requests.
4394 */
4395 status = AdvSendIdleCmd(asc_dvc, (ushort)IDLE_CMD_SCSI_RESET_END, 0L);
4396 if (status != ADV_TRUE) {
4397 return status;
4398 }
4399
4400 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
4401
4402 return status;
4403}
4404
4405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 * Initialize the ASC-3550.
4407 *
4408 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4409 *
4410 * For a non-fatal error return a warning code. If there are no warnings
4411 * then 0 is returned.
4412 *
4413 * Needed after initialization for error recovery.
4414 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004415static int AdvInitAsc3550Driver(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304417 const struct firmware *fw;
4418 const char fwname[] = "advansys/3550.bin";
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004419 AdvPortAddr iop_base;
4420 ushort warn_code;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004421 int begin_addr;
4422 int end_addr;
4423 ushort code_sum;
4424 int word;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004425 int i;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304426 int err;
4427 unsigned long chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004428 ushort scsi_cfg1;
4429 uchar tid;
4430 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
4431 ushort wdtr_able = 0, sdtr_able, tagqng_able;
4432 uchar max_cmd[ADV_MAX_TID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004434 /* If there is already an error, don't continue. */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004435 if (asc_dvc->err_code != 0)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004436 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004438 /*
4439 * The caller must set 'chip_type' to ADV_CHIP_ASC3550.
4440 */
4441 if (asc_dvc->chip_type != ADV_CHIP_ASC3550) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004442 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004443 return ADV_ERROR;
4444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004446 warn_code = 0;
4447 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004449 /*
4450 * Save the RISC memory BIOS region before writing the microcode.
4451 * The BIOS may already be loaded and using its RISC LRAM region
4452 * so its region must be saved and restored.
4453 *
4454 * Note: This code makes the assumption, which is currently true,
4455 * that a chip reset does not clear RISC LRAM.
4456 */
4457 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4458 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4459 bios_mem[i]);
4460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004462 /*
4463 * Save current per TID negotiated values.
4464 */
4465 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] == 0x55AA) {
4466 ushort bios_version, major, minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004468 bios_version =
4469 bios_mem[(ASC_MC_BIOS_VERSION - ASC_MC_BIOSMEM) / 2];
4470 major = (bios_version >> 12) & 0xF;
4471 minor = (bios_version >> 8) & 0xF;
4472 if (major < 3 || (major == 3 && minor == 1)) {
4473 /* BIOS 3.1 and earlier location of 'wdtr_able' variable. */
4474 AdvReadWordLram(iop_base, 0x120, wdtr_able);
4475 } else {
4476 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4477 }
4478 }
4479 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4480 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4481 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4482 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4483 max_cmd[tid]);
4484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304486 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4487 if (err) {
4488 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4489 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004490 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304491 return err;
4492 }
4493 if (fw->size < 4) {
4494 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4495 fw->size, fwname);
4496 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004497 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304498 return -EINVAL;
4499 }
4500 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
4501 (fw->data[1] << 8) | fw->data[0];
4502 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
4503 fw->size - 4, ADV_3550_MEMSIZE,
4504 chksum);
4505 release_firmware(fw);
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004506 if (asc_dvc->err_code)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004507 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004509 /*
4510 * Restore the RISC memory BIOS region.
4511 */
4512 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4513 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4514 bios_mem[i]);
4515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004517 /*
4518 * Calculate and write the microcode code checksum to the microcode
4519 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
4520 */
4521 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
4522 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
4523 code_sum = 0;
4524 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
4525 for (word = begin_addr; word < end_addr; word += 2) {
4526 code_sum += AdvReadWordAutoIncLram(iop_base);
4527 }
4528 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004530 /*
4531 * Read and save microcode version and date.
4532 */
4533 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
4534 asc_dvc->cfg->mcode_date);
4535 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
4536 asc_dvc->cfg->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004538 /*
4539 * Set the chip type to indicate the ASC3550.
4540 */
4541 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC3550);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004543 /*
4544 * If the PCI Configuration Command Register "Parity Error Response
4545 * Control" Bit was clear (0), then set the microcode variable
4546 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
4547 * to ignore DMA parity errors.
4548 */
4549 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
4550 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4551 word |= CONTROL_FLAG_IGNORE_PERR;
4552 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
4553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004555 /*
4556 * For ASC-3550, setting the START_CTL_EMFU [3:2] bits sets a FIFO
4557 * threshold of 128 bytes. This register is only accessible to the host.
4558 */
4559 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
4560 START_CTL_EMFU | READ_CMD_MRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004562 /*
4563 * Microcode operating variables for WDTR, SDTR, and command tag
Matthew Wilcox47d853c2007-07-26 11:41:33 -04004564 * queuing will be set in slave_configure() based on what a
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004565 * device reports it is capable of in Inquiry byte 7.
4566 *
4567 * If SCSI Bus Resets have been disabled, then directly set
4568 * SDTR and WDTR from the EEPROM configuration. This will allow
4569 * the BIOS and warm boot to work without a SCSI bus hang on
4570 * the Inquiry caused by host and target mismatched DTR values.
4571 * Without the SCSI Bus Reset, before an Inquiry a device can't
4572 * be assumed to be in Asynchronous, Narrow mode.
4573 */
4574 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
4575 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
4576 asc_dvc->wdtr_able);
4577 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
4578 asc_dvc->sdtr_able);
4579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004581 /*
4582 * Set microcode operating variables for SDTR_SPEED1, SDTR_SPEED2,
4583 * SDTR_SPEED3, and SDTR_SPEED4 based on the ULTRA EEPROM per TID
4584 * bitmask. These values determine the maximum SDTR speed negotiated
4585 * with a device.
4586 *
4587 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
4588 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
4589 * without determining here whether the device supports SDTR.
4590 *
4591 * 4-bit speed SDTR speed name
4592 * =========== ===============
4593 * 0000b (0x0) SDTR disabled
4594 * 0001b (0x1) 5 Mhz
4595 * 0010b (0x2) 10 Mhz
4596 * 0011b (0x3) 20 Mhz (Ultra)
4597 * 0100b (0x4) 40 Mhz (LVD/Ultra2)
4598 * 0101b (0x5) 80 Mhz (LVD2/Ultra3)
4599 * 0110b (0x6) Undefined
4600 * .
4601 * 1111b (0xF) Undefined
4602 */
4603 word = 0;
4604 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4605 if (ADV_TID_TO_TIDMASK(tid) & asc_dvc->ultra_able) {
4606 /* Set Ultra speed for TID 'tid'. */
4607 word |= (0x3 << (4 * (tid % 4)));
4608 } else {
4609 /* Set Fast speed for TID 'tid'. */
4610 word |= (0x2 << (4 * (tid % 4)));
4611 }
4612 if (tid == 3) { /* Check if done with sdtr_speed1. */
4613 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, word);
4614 word = 0;
4615 } else if (tid == 7) { /* Check if done with sdtr_speed2. */
4616 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, word);
4617 word = 0;
4618 } else if (tid == 11) { /* Check if done with sdtr_speed3. */
4619 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, word);
4620 word = 0;
4621 } else if (tid == 15) { /* Check if done with sdtr_speed4. */
4622 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, word);
4623 /* End of loop. */
4624 }
4625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004627 /*
4628 * Set microcode operating variable for the disconnect per TID bitmask.
4629 */
4630 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
4631 asc_dvc->cfg->disc_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004633 /*
4634 * Set SCSI_CFG0 Microcode Default Value.
4635 *
4636 * The microcode will set the SCSI_CFG0 register using this value
4637 * after it is started below.
4638 */
4639 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
4640 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
4641 asc_dvc->chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004643 /*
4644 * Determine SCSI_CFG1 Microcode Default Value.
4645 *
4646 * The microcode will set the SCSI_CFG1 register using this value
4647 * after it is started below.
4648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004650 /* Read current SCSI_CFG1 Register value. */
4651 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004653 /*
4654 * If all three connectors are in use, return an error.
4655 */
4656 if ((scsi_cfg1 & CABLE_ILLEGAL_A) == 0 ||
4657 (scsi_cfg1 & CABLE_ILLEGAL_B) == 0) {
4658 asc_dvc->err_code |= ASC_IERR_ILLEGAL_CONNECTION;
4659 return ADV_ERROR;
4660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004662 /*
4663 * If the internal narrow cable is reversed all of the SCSI_CTRL
4664 * register signals will be set. Check for and return an error if
4665 * this condition is found.
4666 */
4667 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
4668 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
4669 return ADV_ERROR;
4670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004672 /*
4673 * If this is a differential board and a single-ended device
4674 * is attached to one of the connectors, return an error.
4675 */
4676 if ((scsi_cfg1 & DIFF_MODE) && (scsi_cfg1 & DIFF_SENSE) == 0) {
4677 asc_dvc->err_code |= ASC_IERR_SINGLE_END_DEVICE;
4678 return ADV_ERROR;
4679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004681 /*
4682 * If automatic termination control is enabled, then set the
4683 * termination value based on a table listed in a_condor.h.
4684 *
4685 * If manual termination was specified with an EEPROM setting
4686 * then 'termination' was set-up in AdvInitFrom3550EEPROM() and
4687 * is ready to be 'ored' into SCSI_CFG1.
4688 */
4689 if (asc_dvc->cfg->termination == 0) {
4690 /*
4691 * The software always controls termination by setting TERM_CTL_SEL.
4692 * If TERM_CTL_SEL were set to 0, the hardware would set termination.
4693 */
4694 asc_dvc->cfg->termination |= TERM_CTL_SEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004696 switch (scsi_cfg1 & CABLE_DETECT) {
4697 /* TERM_CTL_H: on, TERM_CTL_L: on */
4698 case 0x3:
4699 case 0x7:
4700 case 0xB:
4701 case 0xD:
4702 case 0xE:
4703 case 0xF:
4704 asc_dvc->cfg->termination |= (TERM_CTL_H | TERM_CTL_L);
4705 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004707 /* TERM_CTL_H: on, TERM_CTL_L: off */
4708 case 0x1:
4709 case 0x5:
4710 case 0x9:
4711 case 0xA:
4712 case 0xC:
4713 asc_dvc->cfg->termination |= TERM_CTL_H;
4714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004716 /* TERM_CTL_H: off, TERM_CTL_L: off */
4717 case 0x2:
4718 case 0x6:
4719 break;
4720 }
4721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004723 /*
4724 * Clear any set TERM_CTL_H and TERM_CTL_L bits.
4725 */
4726 scsi_cfg1 &= ~TERM_CTL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004728 /*
4729 * Invert the TERM_CTL_H and TERM_CTL_L bits and then
4730 * set 'scsi_cfg1'. The TERM_POL bit does not need to be
4731 * referenced, because the hardware internally inverts
4732 * the Termination High and Low bits if TERM_POL is set.
4733 */
4734 scsi_cfg1 |= (TERM_CTL_SEL | (~asc_dvc->cfg->termination & TERM_CTL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004736 /*
4737 * Set SCSI_CFG1 Microcode Default Value
4738 *
4739 * Set filter value and possibly modified termination control
4740 * bits in the Microcode SCSI_CFG1 Register Value.
4741 *
4742 * The microcode will set the SCSI_CFG1 register using this value
4743 * after it is started below.
4744 */
4745 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1,
4746 FLTR_DISABLE | scsi_cfg1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004748 /*
4749 * Set MEM_CFG Microcode Default Value
4750 *
4751 * The microcode will set the MEM_CFG register using this value
4752 * after it is started below.
4753 *
4754 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
4755 * are defined.
4756 *
4757 * ASC-3550 has 8KB internal memory.
4758 */
4759 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
4760 BIOS_EN | RAM_SZ_8KB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004762 /*
4763 * Set SEL_MASK Microcode Default Value
4764 *
4765 * The microcode will set the SEL_MASK register using this value
4766 * after it is started below.
4767 */
4768 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
4769 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06004771 AdvBuildCarrierFreelist(asc_dvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004773 /*
4774 * Set-up the Host->RISC Initiator Command Queue (ICQ).
4775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004777 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
4778 if (!asc_dvc->icq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004779 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4780 return ADV_ERROR;
4781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004783 /*
4784 * Set RISC ICQ physical address start value.
4785 */
4786 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004788 /*
4789 * Set-up the RISC->Host Initiator Response Queue (IRQ).
4790 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02004791 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
4792 if (!asc_dvc->irq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004793 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
4794 return ADV_ERROR;
4795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004797 /*
4798 * Set RISC IRQ physical address start value.
4799 */
4800 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
4801 asc_dvc->carr_pending_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004803 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
4804 (ADV_INTR_ENABLE_HOST_INTR |
4805 ADV_INTR_ENABLE_GLOBAL_INTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004807 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
4808 AdvWriteWordRegister(iop_base, IOPW_PC, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004810 /* finally, finally, gentlemen, start your engine */
4811 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004813 /*
4814 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
4815 * Resets should be performed. The RISC has to be running
4816 * to issue a SCSI Bus Reset.
4817 */
4818 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
4819 /*
4820 * If the BIOS Signature is present in memory, restore the
4821 * BIOS Handshake Configuration Table and do not perform
4822 * a SCSI Bus Reset.
4823 */
4824 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
4825 0x55AA) {
4826 /*
4827 * Restore per TID negotiated values.
4828 */
4829 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4830 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4831 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
4832 tagqng_able);
4833 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4834 AdvWriteByteLram(iop_base,
4835 ASC_MC_NUMBER_OF_MAX_CMD + tid,
4836 max_cmd[tid]);
4837 }
4838 } else {
4839 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
4840 warn_code = ASC_WARN_BUSRESET_ERROR;
4841 }
4842 }
4843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004845 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
4848/*
4849 * Initialize the ASC-38C0800.
4850 *
4851 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
4852 *
4853 * For a non-fatal error return a warning code. If there are no warnings
4854 * then 0 is returned.
4855 *
4856 * Needed after initialization for error recovery.
4857 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004858static int AdvInitAsc38C0800Driver(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304860 const struct firmware *fw;
4861 const char fwname[] = "advansys/38C0800.bin";
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004862 AdvPortAddr iop_base;
4863 ushort warn_code;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004864 int begin_addr;
4865 int end_addr;
4866 ushort code_sum;
4867 int word;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004868 int i;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304869 int err;
4870 unsigned long chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004871 ushort scsi_cfg1;
4872 uchar byte;
4873 uchar tid;
4874 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
4875 ushort wdtr_able, sdtr_able, tagqng_able;
4876 uchar max_cmd[ADV_MAX_TID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004878 /* If there is already an error, don't continue. */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004879 if (asc_dvc->err_code != 0)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004880 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004882 /*
4883 * The caller must set 'chip_type' to ADV_CHIP_ASC38C0800.
4884 */
4885 if (asc_dvc->chip_type != ADV_CHIP_ASC38C0800) {
4886 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
4887 return ADV_ERROR;
4888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004890 warn_code = 0;
4891 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004893 /*
4894 * Save the RISC memory BIOS region before writing the microcode.
4895 * The BIOS may already be loaded and using its RISC LRAM region
4896 * so its region must be saved and restored.
4897 *
4898 * Note: This code makes the assumption, which is currently true,
4899 * that a chip reset does not clear RISC LRAM.
4900 */
4901 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
4902 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
4903 bios_mem[i]);
4904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004906 /*
4907 * Save current per TID negotiated values.
4908 */
4909 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
4910 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
4911 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
4912 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
4913 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
4914 max_cmd[tid]);
4915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004917 /*
4918 * RAM BIST (RAM Built-In Self Test)
4919 *
4920 * Address : I/O base + offset 0x38h register (byte).
4921 * Function: Bit 7-6(RW) : RAM mode
4922 * Normal Mode : 0x00
4923 * Pre-test Mode : 0x40
4924 * RAM Test Mode : 0x80
4925 * Bit 5 : unused
4926 * Bit 4(RO) : Done bit
4927 * Bit 3-0(RO) : Status
4928 * Host Error : 0x08
4929 * Int_RAM Error : 0x04
4930 * RISC Error : 0x02
4931 * SCSI Error : 0x01
4932 * No Error : 0x00
4933 *
4934 * Note: RAM BIST code should be put right here, before loading the
4935 * microcode and after saving the RISC memory BIOS region.
4936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004938 /*
4939 * LRAM Pre-test
4940 *
4941 * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
4942 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
4943 * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
4944 * to NORMAL_MODE, return an error too.
4945 */
4946 for (i = 0; i < 2; i++) {
4947 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06004948 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004949 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
4950 if ((byte & RAM_TEST_DONE) == 0
4951 || (byte & 0x0F) != PRE_TEST_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004952 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004953 return ADV_ERROR;
4954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004956 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06004957 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004958 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
4959 != NORMAL_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004960 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004961 return ADV_ERROR;
4962 }
4963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004965 /*
4966 * LRAM Test - It takes about 1.5 ms to run through the test.
4967 *
4968 * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
4969 * If Done bit not set or Status not 0, save register byte, set the
4970 * err_code, and return an error.
4971 */
4972 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06004973 mdelay(10); /* Wait for 10ms before checking status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004975 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
4976 if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
4977 /* Get here if Done bit not set or Status not 0. */
4978 asc_dvc->bist_err_code = byte; /* for BIOS display message */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06004979 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004980 return ADV_ERROR;
4981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Matthew Wilcox27c868c2007-07-26 10:56:23 -04004983 /* We need to reset back to normal mode after LRAM test passes. */
4984 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304986 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
4987 if (err) {
4988 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
4989 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004990 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304991 return err;
4992 }
4993 if (fw->size < 4) {
4994 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
4995 fw->size, fwname);
4996 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03004997 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05304998 return -EINVAL;
4999 }
5000 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5001 (fw->data[1] << 8) | fw->data[0];
5002 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5003 fw->size - 4, ADV_38C0800_MEMSIZE,
5004 chksum);
5005 release_firmware(fw);
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005006 if (asc_dvc->err_code)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005007 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005009 /*
5010 * Restore the RISC memory BIOS region.
5011 */
5012 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5013 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5014 bios_mem[i]);
5015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005017 /*
5018 * Calculate and write the microcode code checksum to the microcode
5019 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5020 */
5021 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5022 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5023 code_sum = 0;
5024 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5025 for (word = begin_addr; word < end_addr; word += 2) {
5026 code_sum += AdvReadWordAutoIncLram(iop_base);
5027 }
5028 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005030 /*
5031 * Read microcode version and date.
5032 */
5033 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5034 asc_dvc->cfg->mcode_date);
5035 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5036 asc_dvc->cfg->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005038 /*
5039 * Set the chip type to indicate the ASC38C0800.
5040 */
5041 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005043 /*
5044 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5045 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5046 * cable detection and then we are able to read C_DET[3:0].
5047 *
5048 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5049 * Microcode Default Value' section below.
5050 */
5051 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5052 AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5053 scsi_cfg1 | DIS_TERM_DRV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005055 /*
5056 * If the PCI Configuration Command Register "Parity Error Response
5057 * Control" Bit was clear (0), then set the microcode variable
5058 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5059 * to ignore DMA parity errors.
5060 */
5061 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5062 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5063 word |= CONTROL_FLAG_IGNORE_PERR;
5064 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005067 /*
5068 * For ASC-38C0800, set FIFO_THRESH_80B [6:4] bits and START_CTL_TH [3:2]
5069 * bits for the default FIFO threshold.
5070 *
5071 * Note: ASC-38C0800 FIFO threshold has been changed to 256 bytes.
5072 *
5073 * For DMA Errata #4 set the BC_THRESH_ENB bit.
5074 */
5075 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5076 BC_THRESH_ENB | FIFO_THRESH_80B | START_CTL_TH |
5077 READ_CMD_MRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005079 /*
5080 * Microcode operating variables for WDTR, SDTR, and command tag
Matthew Wilcox47d853c2007-07-26 11:41:33 -04005081 * queuing will be set in slave_configure() based on what a
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005082 * device reports it is capable of in Inquiry byte 7.
5083 *
5084 * If SCSI Bus Resets have been disabled, then directly set
5085 * SDTR and WDTR from the EEPROM configuration. This will allow
5086 * the BIOS and warm boot to work without a SCSI bus hang on
5087 * the Inquiry caused by host and target mismatched DTR values.
5088 * Without the SCSI Bus Reset, before an Inquiry a device can't
5089 * be assumed to be in Asynchronous, Narrow mode.
5090 */
5091 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5092 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5093 asc_dvc->wdtr_able);
5094 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5095 asc_dvc->sdtr_able);
5096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005098 /*
5099 * Set microcode operating variables for DISC and SDTR_SPEED1,
5100 * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5101 * configuration values.
5102 *
5103 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5104 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5105 * without determining here whether the device supports SDTR.
5106 */
5107 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5108 asc_dvc->cfg->disc_enable);
5109 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5110 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5111 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5112 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005114 /*
5115 * Set SCSI_CFG0 Microcode Default Value.
5116 *
5117 * The microcode will set the SCSI_CFG0 register using this value
5118 * after it is started below.
5119 */
5120 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5121 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5122 asc_dvc->chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005124 /*
5125 * Determine SCSI_CFG1 Microcode Default Value.
5126 *
5127 * The microcode will set the SCSI_CFG1 register using this value
5128 * after it is started below.
5129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005131 /* Read current SCSI_CFG1 Register value. */
5132 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005134 /*
5135 * If the internal narrow cable is reversed all of the SCSI_CTRL
5136 * register signals will be set. Check for and return an error if
5137 * this condition is found.
5138 */
5139 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5140 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5141 return ADV_ERROR;
5142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005144 /*
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005145 * All kind of combinations of devices attached to one of four
5146 * connectors are acceptable except HVD device attached. For example,
5147 * LVD device can be attached to SE connector while SE device attached
5148 * to LVD connector. If LVD device attached to SE connector, it only
5149 * runs up to Ultra speed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005150 *
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005151 * If an HVD device is attached to one of LVD connectors, return an
5152 * error. However, there is no way to detect HVD device attached to
5153 * SE connectors.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005154 */
5155 if (scsi_cfg1 & HVD) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005156 asc_dvc->err_code = ASC_IERR_HVD_DEVICE;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005157 return ADV_ERROR;
5158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005160 /*
5161 * If either SE or LVD automatic termination control is enabled, then
5162 * set the termination value based on a table listed in a_condor.h.
5163 *
5164 * If manual termination was specified with an EEPROM setting then
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005165 * 'termination' was set-up in AdvInitFrom38C0800EEPROM() and is ready
5166 * to be 'ored' into SCSI_CFG1.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005167 */
5168 if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
5169 /* SE automatic termination control is enabled. */
5170 switch (scsi_cfg1 & C_DET_SE) {
5171 /* TERM_SE_HI: on, TERM_SE_LO: on */
5172 case 0x1:
5173 case 0x2:
5174 case 0x3:
5175 asc_dvc->cfg->termination |= TERM_SE;
5176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005178 /* TERM_SE_HI: on, TERM_SE_LO: off */
5179 case 0x0:
5180 asc_dvc->cfg->termination |= TERM_SE_HI;
5181 break;
5182 }
5183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005185 if ((asc_dvc->cfg->termination & TERM_LVD) == 0) {
5186 /* LVD automatic termination control is enabled. */
5187 switch (scsi_cfg1 & C_DET_LVD) {
5188 /* TERM_LVD_HI: on, TERM_LVD_LO: on */
5189 case 0x4:
5190 case 0x8:
5191 case 0xC:
5192 asc_dvc->cfg->termination |= TERM_LVD;
5193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005195 /* TERM_LVD_HI: off, TERM_LVD_LO: off */
5196 case 0x0:
5197 break;
5198 }
5199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005201 /*
5202 * Clear any set TERM_SE and TERM_LVD bits.
5203 */
5204 scsi_cfg1 &= (~TERM_SE & ~TERM_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005206 /*
5207 * Invert the TERM_SE and TERM_LVD bits and then set 'scsi_cfg1'.
5208 */
5209 scsi_cfg1 |= (~asc_dvc->cfg->termination & 0xF0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005211 /*
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005212 * Clear BIG_ENDIAN, DIS_TERM_DRV, Terminator Polarity and HVD/LVD/SE
5213 * bits and set possibly modified termination control bits in the
5214 * Microcode SCSI_CFG1 Register Value.
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005215 */
5216 scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL & ~HVD_LVD_SE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005218 /*
5219 * Set SCSI_CFG1 Microcode Default Value
5220 *
5221 * Set possibly modified termination control and reset DIS_TERM_DRV
5222 * bits in the Microcode SCSI_CFG1 Register Value.
5223 *
5224 * The microcode will set the SCSI_CFG1 register using this value
5225 * after it is started below.
5226 */
5227 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005229 /*
5230 * Set MEM_CFG Microcode Default Value
5231 *
5232 * The microcode will set the MEM_CFG register using this value
5233 * after it is started below.
5234 *
5235 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5236 * are defined.
5237 *
5238 * ASC-38C0800 has 16KB internal memory.
5239 */
5240 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5241 BIOS_EN | RAM_SZ_16KB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005243 /*
5244 * Set SEL_MASK Microcode Default Value
5245 *
5246 * The microcode will set the SEL_MASK register using this value
5247 * after it is started below.
5248 */
5249 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5250 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06005252 AdvBuildCarrierFreelist(asc_dvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005254 /*
5255 * Set-up the Host->RISC Initiator Command Queue (ICQ).
5256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005258 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5259 if (!asc_dvc->icq_sp) {
5260 ASC_DBG(0, "Failed to get ICQ carrier\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005261 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5262 return ADV_ERROR;
5263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005265 /*
5266 * Set RISC ICQ physical address start value.
5267 * carr_pa is LE, must be native before write
5268 */
5269 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005271 /*
5272 * Set-up the RISC->Host Initiator Response Queue (IRQ).
5273 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005274 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5275 if (!asc_dvc->irq_sp) {
5276 ASC_DBG(0, "Failed to get IRQ carrier\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005277 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5278 return ADV_ERROR;
5279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005281 /*
5282 * Set RISC IRQ physical address start value.
5283 *
5284 * carr_pa is LE, must be native before write *
5285 */
5286 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5287 asc_dvc->carr_pending_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005289 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5290 (ADV_INTR_ENABLE_HOST_INTR |
5291 ADV_INTR_ENABLE_GLOBAL_INTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005293 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5294 AdvWriteWordRegister(iop_base, IOPW_PC, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005296 /* finally, finally, gentlemen, start your engine */
5297 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005299 /*
5300 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5301 * Resets should be performed. The RISC has to be running
5302 * to issue a SCSI Bus Reset.
5303 */
5304 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5305 /*
5306 * If the BIOS Signature is present in memory, restore the
5307 * BIOS Handshake Configuration Table and do not perform
5308 * a SCSI Bus Reset.
5309 */
5310 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5311 0x55AA) {
5312 /*
5313 * Restore per TID negotiated values.
5314 */
5315 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5316 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5317 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5318 tagqng_able);
5319 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5320 AdvWriteByteLram(iop_base,
5321 ASC_MC_NUMBER_OF_MAX_CMD + tid,
5322 max_cmd[tid]);
5323 }
5324 } else {
5325 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5326 warn_code = ASC_WARN_BUSRESET_ERROR;
5327 }
5328 }
5329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005331 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332}
5333
5334/*
5335 * Initialize the ASC-38C1600.
5336 *
5337 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
5338 *
5339 * For a non-fatal error return a warning code. If there are no warnings
5340 * then 0 is returned.
5341 *
5342 * Needed after initialization for error recovery.
5343 */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005344static int AdvInitAsc38C1600Driver(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305346 const struct firmware *fw;
5347 const char fwname[] = "advansys/38C1600.bin";
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005348 AdvPortAddr iop_base;
5349 ushort warn_code;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005350 int begin_addr;
5351 int end_addr;
5352 ushort code_sum;
5353 long word;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005354 int i;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305355 int err;
5356 unsigned long chksum;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005357 ushort scsi_cfg1;
5358 uchar byte;
5359 uchar tid;
5360 ushort bios_mem[ASC_MC_BIOSLEN / 2]; /* BIOS RISC Memory 0x40-0x8F. */
5361 ushort wdtr_able, sdtr_able, ppr_able, tagqng_able;
5362 uchar max_cmd[ASC_MAX_TID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005364 /* If there is already an error, don't continue. */
5365 if (asc_dvc->err_code != 0) {
5366 return ADV_ERROR;
5367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005369 /*
5370 * The caller must set 'chip_type' to ADV_CHIP_ASC38C1600.
5371 */
5372 if (asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
5373 asc_dvc->err_code = ASC_IERR_BAD_CHIPTYPE;
5374 return ADV_ERROR;
5375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005377 warn_code = 0;
5378 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005380 /*
5381 * Save the RISC memory BIOS region before writing the microcode.
5382 * The BIOS may already be loaded and using its RISC LRAM region
5383 * so its region must be saved and restored.
5384 *
5385 * Note: This code makes the assumption, which is currently true,
5386 * that a chip reset does not clear RISC LRAM.
5387 */
5388 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5389 AdvReadWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5390 bios_mem[i]);
5391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005393 /*
5394 * Save current per TID negotiated values.
5395 */
5396 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5397 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5398 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5399 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5400 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5401 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5402 max_cmd[tid]);
5403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005405 /*
5406 * RAM BIST (Built-In Self Test)
5407 *
5408 * Address : I/O base + offset 0x38h register (byte).
5409 * Function: Bit 7-6(RW) : RAM mode
5410 * Normal Mode : 0x00
5411 * Pre-test Mode : 0x40
5412 * RAM Test Mode : 0x80
5413 * Bit 5 : unused
5414 * Bit 4(RO) : Done bit
5415 * Bit 3-0(RO) : Status
5416 * Host Error : 0x08
5417 * Int_RAM Error : 0x04
5418 * RISC Error : 0x02
5419 * SCSI Error : 0x01
5420 * No Error : 0x00
5421 *
5422 * Note: RAM BIST code should be put right here, before loading the
5423 * microcode and after saving the RISC memory BIOS region.
5424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005426 /*
5427 * LRAM Pre-test
5428 *
5429 * Write PRE_TEST_MODE (0x40) to register and wait for 10 milliseconds.
5430 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05), return
5431 * an error. Reset to NORMAL_MODE (0x00) and do again. If cannot reset
5432 * to NORMAL_MODE, return an error too.
5433 */
5434 for (i = 0; i < 2; i++) {
5435 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, PRE_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005436 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005437 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5438 if ((byte & RAM_TEST_DONE) == 0
5439 || (byte & 0x0F) != PRE_TEST_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005440 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005441 return ADV_ERROR;
5442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005444 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005445 mdelay(10); /* Wait for 10ms before reading back. */
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005446 if (AdvReadByteRegister(iop_base, IOPB_RAM_BIST)
5447 != NORMAL_VALUE) {
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005448 asc_dvc->err_code = ASC_IERR_BIST_PRE_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005449 return ADV_ERROR;
5450 }
5451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005453 /*
5454 * LRAM Test - It takes about 1.5 ms to run through the test.
5455 *
5456 * Write RAM_TEST_MODE (0x80) to register and wait for 10 milliseconds.
5457 * If Done bit not set or Status not 0, save register byte, set the
5458 * err_code, and return an error.
5459 */
5460 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, RAM_TEST_MODE);
Matthew Wilcoxb009bef2007-09-09 08:56:38 -06005461 mdelay(10); /* Wait for 10ms before checking status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005463 byte = AdvReadByteRegister(iop_base, IOPB_RAM_BIST);
5464 if ((byte & RAM_TEST_DONE) == 0 || (byte & RAM_TEST_STATUS) != 0) {
5465 /* Get here if Done bit not set or Status not 0. */
5466 asc_dvc->bist_err_code = byte; /* for BIOS display message */
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005467 asc_dvc->err_code = ASC_IERR_BIST_RAM_TEST;
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005468 return ADV_ERROR;
5469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005471 /* We need to reset back to normal mode after LRAM test passes. */
5472 AdvWriteByteRegister(iop_base, IOPB_RAM_BIST, NORMAL_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305474 err = request_firmware(&fw, fwname, asc_dvc->drv_ptr->dev);
5475 if (err) {
5476 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
5477 fwname, err);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03005478 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305479 return err;
5480 }
5481 if (fw->size < 4) {
5482 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
5483 fw->size, fwname);
5484 release_firmware(fw);
Herton Ronaldo Krzesinskicf747442010-03-19 19:37:26 -03005485 asc_dvc->err_code = ASC_IERR_MCODE_CHKSUM;
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +05305486 return -EINVAL;
5487 }
5488 chksum = (fw->data[3] << 24) | (fw->data[2] << 16) |
5489 (fw->data[1] << 8) | fw->data[0];
5490 asc_dvc->err_code = AdvLoadMicrocode(iop_base, &fw->data[4],
5491 fw->size - 4, ADV_38C1600_MEMSIZE,
5492 chksum);
5493 release_firmware(fw);
Matthew Wilcoxb9d96612007-09-09 08:56:28 -06005494 if (asc_dvc->err_code)
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005495 return ADV_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005497 /*
5498 * Restore the RISC memory BIOS region.
5499 */
5500 for (i = 0; i < ASC_MC_BIOSLEN / 2; i++) {
5501 AdvWriteWordLram(iop_base, ASC_MC_BIOSMEM + (2 * i),
5502 bios_mem[i]);
5503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005505 /*
5506 * Calculate and write the microcode code checksum to the microcode
5507 * code checksum location ASC_MC_CODE_CHK_SUM (0x2C).
5508 */
5509 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, begin_addr);
5510 AdvReadWordLram(iop_base, ASC_MC_CODE_END_ADDR, end_addr);
5511 code_sum = 0;
5512 AdvWriteWordRegister(iop_base, IOPW_RAM_ADDR, begin_addr);
5513 for (word = begin_addr; word < end_addr; word += 2) {
5514 code_sum += AdvReadWordAutoIncLram(iop_base);
5515 }
5516 AdvWriteWordLram(iop_base, ASC_MC_CODE_CHK_SUM, code_sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005518 /*
5519 * Read microcode version and date.
5520 */
5521 AdvReadWordLram(iop_base, ASC_MC_VERSION_DATE,
5522 asc_dvc->cfg->mcode_date);
5523 AdvReadWordLram(iop_base, ASC_MC_VERSION_NUM,
5524 asc_dvc->cfg->mcode_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005526 /*
5527 * Set the chip type to indicate the ASC38C1600.
5528 */
5529 AdvWriteWordLram(iop_base, ASC_MC_CHIP_TYPE, ADV_CHIP_ASC38C1600);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005531 /*
5532 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
5533 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
5534 * cable detection and then we are able to read C_DET[3:0].
5535 *
5536 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
5537 * Microcode Default Value' section below.
5538 */
5539 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
5540 AdvWriteWordRegister(iop_base, IOPW_SCSI_CFG1,
5541 scsi_cfg1 | DIS_TERM_DRV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005543 /*
5544 * If the PCI Configuration Command Register "Parity Error Response
5545 * Control" Bit was clear (0), then set the microcode variable
5546 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
5547 * to ignore DMA parity errors.
5548 */
5549 if (asc_dvc->cfg->control_flag & CONTROL_FLAG_IGNORE_PERR) {
5550 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5551 word |= CONTROL_FLAG_IGNORE_PERR;
5552 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005555 /*
5556 * If the BIOS control flag AIPP (Asynchronous Information
5557 * Phase Protection) disable bit is not set, then set the firmware
5558 * 'control_flag' CONTROL_FLAG_ENABLE_AIPP bit to enable
5559 * AIPP checking and encoding.
5560 */
5561 if ((asc_dvc->bios_ctrl & BIOS_CTRL_AIPP_DIS) == 0) {
5562 AdvReadWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5563 word |= CONTROL_FLAG_ENABLE_AIPP;
5564 AdvWriteWordLram(iop_base, ASC_MC_CONTROL_FLAG, word);
5565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005567 /*
5568 * For ASC-38C1600 use DMA_CFG0 default values: FIFO_THRESH_80B [6:4],
5569 * and START_CTL_TH [3:2].
5570 */
5571 AdvWriteByteRegister(iop_base, IOPB_DMA_CFG0,
5572 FIFO_THRESH_80B | START_CTL_TH | READ_CMD_MRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005574 /*
5575 * Microcode operating variables for WDTR, SDTR, and command tag
Matthew Wilcox47d853c2007-07-26 11:41:33 -04005576 * queuing will be set in slave_configure() based on what a
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005577 * device reports it is capable of in Inquiry byte 7.
5578 *
5579 * If SCSI Bus Resets have been disabled, then directly set
5580 * SDTR and WDTR from the EEPROM configuration. This will allow
5581 * the BIOS and warm boot to work without a SCSI bus hang on
5582 * the Inquiry caused by host and target mismatched DTR values.
5583 * Without the SCSI Bus Reset, before an Inquiry a device can't
5584 * be assumed to be in Asynchronous, Narrow mode.
5585 */
5586 if ((asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
5587 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE,
5588 asc_dvc->wdtr_able);
5589 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE,
5590 asc_dvc->sdtr_able);
5591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005593 /*
5594 * Set microcode operating variables for DISC and SDTR_SPEED1,
5595 * SDTR_SPEED2, SDTR_SPEED3, and SDTR_SPEED4 based on the EEPROM
5596 * configuration values.
5597 *
5598 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
5599 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
5600 * without determining here whether the device supports SDTR.
5601 */
5602 AdvWriteWordLram(iop_base, ASC_MC_DISC_ENABLE,
5603 asc_dvc->cfg->disc_enable);
5604 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED1, asc_dvc->sdtr_speed1);
5605 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED2, asc_dvc->sdtr_speed2);
5606 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED3, asc_dvc->sdtr_speed3);
5607 AdvWriteWordLram(iop_base, ASC_MC_SDTR_SPEED4, asc_dvc->sdtr_speed4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005609 /*
5610 * Set SCSI_CFG0 Microcode Default Value.
5611 *
5612 * The microcode will set the SCSI_CFG0 register using this value
5613 * after it is started below.
5614 */
5615 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG0,
5616 PARITY_EN | QUEUE_128 | SEL_TMO_LONG | OUR_ID_EN |
5617 asc_dvc->chip_scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005619 /*
5620 * Calculate SCSI_CFG1 Microcode Default Value.
5621 *
5622 * The microcode will set the SCSI_CFG1 register using this value
5623 * after it is started below.
5624 *
5625 * Each ASC-38C1600 function has only two cable detect bits.
5626 * The bus mode override bits are in IOPB_SOFT_OVER_WR.
5627 */
5628 scsi_cfg1 = AdvReadWordRegister(iop_base, IOPW_SCSI_CFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005630 /*
5631 * If the cable is reversed all of the SCSI_CTRL register signals
5632 * will be set. Check for and return an error if this condition is
5633 * found.
5634 */
5635 if ((AdvReadWordRegister(iop_base, IOPW_SCSI_CTRL) & 0x3F07) == 0x3F07) {
5636 asc_dvc->err_code |= ASC_IERR_REVERSED_CABLE;
5637 return ADV_ERROR;
5638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005640 /*
5641 * Each ASC-38C1600 function has two connectors. Only an HVD device
5642 * can not be connected to either connector. An LVD device or SE device
5643 * may be connected to either connecor. If an SE device is connected,
5644 * then at most Ultra speed (20 Mhz) can be used on both connectors.
5645 *
5646 * If an HVD device is attached, return an error.
5647 */
5648 if (scsi_cfg1 & HVD) {
5649 asc_dvc->err_code |= ASC_IERR_HVD_DEVICE;
5650 return ADV_ERROR;
5651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005653 /*
5654 * Each function in the ASC-38C1600 uses only the SE cable detect and
5655 * termination because there are two connectors for each function. Each
5656 * function may use either LVD or SE mode. Corresponding the SE automatic
5657 * termination control EEPROM bits are used for each function. Each
5658 * function has its own EEPROM. If SE automatic control is enabled for
5659 * the function, then set the termination value based on a table listed
5660 * in a_condor.h.
5661 *
5662 * If manual termination is specified in the EEPROM for the function,
5663 * then 'termination' was set-up in AscInitFrom38C1600EEPROM() and is
5664 * ready to be 'ored' into SCSI_CFG1.
5665 */
5666 if ((asc_dvc->cfg->termination & TERM_SE) == 0) {
Matthew Wilcox13ac2d92007-07-30 08:10:23 -06005667 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005668 /* SE automatic termination control is enabled. */
5669 switch (scsi_cfg1 & C_DET_SE) {
5670 /* TERM_SE_HI: on, TERM_SE_LO: on */
5671 case 0x1:
5672 case 0x2:
5673 case 0x3:
5674 asc_dvc->cfg->termination |= TERM_SE;
5675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005677 case 0x0:
Matthew Wilcox13ac2d92007-07-30 08:10:23 -06005678 if (PCI_FUNC(pdev->devfn) == 0) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005679 /* Function 0 - TERM_SE_HI: off, TERM_SE_LO: off */
5680 } else {
5681 /* Function 1 - TERM_SE_HI: on, TERM_SE_LO: off */
5682 asc_dvc->cfg->termination |= TERM_SE_HI;
5683 }
5684 break;
5685 }
5686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005688 /*
5689 * Clear any set TERM_SE bits.
5690 */
5691 scsi_cfg1 &= ~TERM_SE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005693 /*
5694 * Invert the TERM_SE bits and then set 'scsi_cfg1'.
5695 */
5696 scsi_cfg1 |= (~asc_dvc->cfg->termination & TERM_SE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005698 /*
5699 * Clear Big Endian and Terminator Polarity bits and set possibly
5700 * modified termination control bits in the Microcode SCSI_CFG1
5701 * Register Value.
5702 *
5703 * Big Endian bit is not used even on big endian machines.
5704 */
5705 scsi_cfg1 &= (~BIG_ENDIAN & ~DIS_TERM_DRV & ~TERM_POL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005707 /*
5708 * Set SCSI_CFG1 Microcode Default Value
5709 *
5710 * Set possibly modified termination control bits in the Microcode
5711 * SCSI_CFG1 Register Value.
5712 *
5713 * The microcode will set the SCSI_CFG1 register using this value
5714 * after it is started below.
5715 */
5716 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005718 /*
5719 * Set MEM_CFG Microcode Default Value
5720 *
5721 * The microcode will set the MEM_CFG register using this value
5722 * after it is started below.
5723 *
5724 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
5725 * are defined.
5726 *
5727 * ASC-38C1600 has 32KB internal memory.
5728 *
5729 * XXX - Since ASC38C1600 Rev.3 has a Local RAM failure issue, we come
5730 * out a special 16K Adv Library and Microcode version. After the issue
5731 * resolved, we should turn back to the 32K support. Both a_condor.h and
5732 * mcode.sas files also need to be updated.
5733 *
5734 * AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5735 * BIOS_EN | RAM_SZ_32KB);
5736 */
5737 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_MEM_CFG,
5738 BIOS_EN | RAM_SZ_16KB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005740 /*
5741 * Set SEL_MASK Microcode Default Value
5742 *
5743 * The microcode will set the SEL_MASK register using this value
5744 * after it is started below.
5745 */
5746 AdvWriteWordLram(iop_base, ASC_MC_DEFAULT_SEL_MASK,
5747 ADV_TID_TO_TIDMASK(asc_dvc->chip_scsi_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Matthew Wilcoxa9f4a592007-09-09 08:56:27 -06005749 AdvBuildCarrierFreelist(asc_dvc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005751 /*
5752 * Set-up the Host->RISC Initiator Command Queue (ICQ).
5753 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005754 asc_dvc->icq_sp = adv_get_next_carrier(asc_dvc);
5755 if (!asc_dvc->icq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005756 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5757 return ADV_ERROR;
5758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005760 /*
5761 * Set RISC ICQ physical address start value. Initialize the
5762 * COMMA register to the same value otherwise the RISC will
5763 * prematurely detect a command is available.
5764 */
5765 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_ICQ, asc_dvc->icq_sp->carr_pa);
5766 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
5767 le32_to_cpu(asc_dvc->icq_sp->carr_pa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005769 /*
5770 * Set-up the RISC->Host Initiator Response Queue (IRQ).
5771 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02005772 asc_dvc->irq_sp = adv_get_next_carrier(asc_dvc);
5773 if (!asc_dvc->irq_sp) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005774 asc_dvc->err_code |= ASC_IERR_NO_CARRIER;
5775 return ADV_ERROR;
5776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005778 /*
5779 * Set RISC IRQ physical address start value.
5780 */
5781 AdvWriteDWordLramNoSwap(iop_base, ASC_MC_IRQ, asc_dvc->irq_sp->carr_pa);
5782 asc_dvc->carr_pending_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005784 AdvWriteByteRegister(iop_base, IOPB_INTR_ENABLES,
5785 (ADV_INTR_ENABLE_HOST_INTR |
5786 ADV_INTR_ENABLE_GLOBAL_INTR));
5787 AdvReadWordLram(iop_base, ASC_MC_CODE_BEGIN_ADDR, word);
5788 AdvWriteWordRegister(iop_base, IOPW_PC, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005790 /* finally, finally, gentlemen, start your engine */
5791 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_RUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005793 /*
5794 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
5795 * Resets should be performed. The RISC has to be running
5796 * to issue a SCSI Bus Reset.
5797 */
5798 if (asc_dvc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) {
5799 /*
5800 * If the BIOS Signature is present in memory, restore the
5801 * per TID microcode operating variables.
5802 */
5803 if (bios_mem[(ASC_MC_BIOS_SIGNATURE - ASC_MC_BIOSMEM) / 2] ==
5804 0x55AA) {
5805 /*
5806 * Restore per TID negotiated values.
5807 */
5808 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5809 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5810 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5811 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
5812 tagqng_able);
5813 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
5814 AdvWriteByteLram(iop_base,
5815 ASC_MC_NUMBER_OF_MAX_CMD + tid,
5816 max_cmd[tid]);
5817 }
5818 } else {
5819 if (AdvResetSB(asc_dvc) != ADV_TRUE) {
5820 warn_code = ASC_WARN_BUSRESET_ERROR;
5821 }
5822 }
5823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Matthew Wilcox27c868c2007-07-26 10:56:23 -04005825 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
5828/*
Matthew Wilcox51219352007-10-02 21:55:22 -04005829 * Reset chip and SCSI Bus.
5830 *
5831 * Return Value:
5832 * ADV_TRUE(1) - Chip re-initialization and SCSI Bus Reset successful.
5833 * ADV_FALSE(0) - Chip re-initialization and SCSI Bus Reset failure.
5834 */
5835static int AdvResetChipAndSB(ADV_DVC_VAR *asc_dvc)
5836{
5837 int status;
5838 ushort wdtr_able, sdtr_able, tagqng_able;
5839 ushort ppr_able = 0;
5840 uchar tid, max_cmd[ADV_MAX_TID + 1];
5841 AdvPortAddr iop_base;
5842 ushort bios_sig;
5843
5844 iop_base = asc_dvc->iop_base;
5845
5846 /*
5847 * Save current per TID negotiated values.
5848 */
5849 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5850 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5851 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5852 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5853 }
5854 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5855 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5856 AdvReadByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5857 max_cmd[tid]);
5858 }
5859
5860 /*
5861 * Force the AdvInitAsc3550/38C0800Driver() function to
5862 * perform a SCSI Bus Reset by clearing the BIOS signature word.
5863 * The initialization functions assumes a SCSI Bus Reset is not
5864 * needed if the BIOS signature word is present.
5865 */
5866 AdvReadWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5867 AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, 0);
5868
5869 /*
5870 * Stop chip and reset it.
5871 */
5872 AdvWriteWordRegister(iop_base, IOPW_RISC_CSR, ADV_RISC_CSR_STOP);
5873 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG, ADV_CTRL_REG_CMD_RESET);
5874 mdelay(100);
5875 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
5876 ADV_CTRL_REG_CMD_WR_IO_REG);
5877
5878 /*
5879 * Reset Adv Library error code, if any, and try
5880 * re-initializing the chip.
5881 */
5882 asc_dvc->err_code = 0;
5883 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5884 status = AdvInitAsc38C1600Driver(asc_dvc);
5885 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
5886 status = AdvInitAsc38C0800Driver(asc_dvc);
5887 } else {
5888 status = AdvInitAsc3550Driver(asc_dvc);
5889 }
5890
5891 /* Translate initialization return value to status value. */
5892 if (status == 0) {
5893 status = ADV_TRUE;
5894 } else {
5895 status = ADV_FALSE;
5896 }
5897
5898 /*
5899 * Restore the BIOS signature word.
5900 */
5901 AdvWriteWordLram(iop_base, ASC_MC_BIOS_SIGNATURE, bios_sig);
5902
5903 /*
5904 * Restore per TID negotiated values.
5905 */
5906 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, wdtr_able);
5907 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, sdtr_able);
5908 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
5909 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, ppr_able);
5910 }
5911 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE, tagqng_able);
5912 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
5913 AdvWriteByteLram(iop_base, ASC_MC_NUMBER_OF_MAX_CMD + tid,
5914 max_cmd[tid]);
5915 }
5916
5917 return status;
5918}
5919
5920/*
5921 * adv_async_callback() - Adv Library asynchronous event callback function.
5922 */
5923static void adv_async_callback(ADV_DVC_VAR *adv_dvc_varp, uchar code)
5924{
5925 switch (code) {
5926 case ADV_ASYNC_SCSI_BUS_RESET_DET:
5927 /*
5928 * The firmware detected a SCSI Bus reset.
5929 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04005930 ASC_DBG(0, "ADV_ASYNC_SCSI_BUS_RESET_DET\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04005931 break;
5932
5933 case ADV_ASYNC_RDMA_FAILURE:
5934 /*
5935 * Handle RDMA failure by resetting the SCSI Bus and
5936 * possibly the chip if it is unresponsive. Log the error
5937 * with a unique code.
5938 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04005939 ASC_DBG(0, "ADV_ASYNC_RDMA_FAILURE\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04005940 AdvResetChipAndSB(adv_dvc_varp);
5941 break;
5942
5943 case ADV_HOST_SCSI_BUS_RESET:
5944 /*
5945 * Host generated SCSI bus reset occurred.
5946 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04005947 ASC_DBG(0, "ADV_HOST_SCSI_BUS_RESET\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04005948 break;
5949
5950 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04005951 ASC_DBG(0, "unknown code 0x%x\n", code);
Matthew Wilcox51219352007-10-02 21:55:22 -04005952 break;
5953 }
5954}
5955
5956/*
5957 * adv_isr_callback() - Second Level Interrupt Handler called by AdvISR().
5958 *
5959 * Callback function for the Wide SCSI Adv Library.
5960 */
5961static void adv_isr_callback(ADV_DVC_VAR *adv_dvc_varp, ADV_SCSI_REQ_Q *scsiqp)
5962{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02005963 struct asc_board *boardp = adv_dvc_varp->drv_ptr;
5964 u32 srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04005965 adv_req_t *reqp;
5966 adv_sgblk_t *sgblkp;
5967 struct scsi_cmnd *scp;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02005968 u32 resid_cnt;
Hannes Reinecke811ddc02015-04-24 13:18:22 +02005969 dma_addr_t sense_addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04005970
Hannes Reinecke9c17c622015-04-24 13:18:21 +02005971 ASC_DBG(1, "adv_dvc_varp 0x%p, scsiqp 0x%p\n",
5972 adv_dvc_varp, scsiqp);
Matthew Wilcox51219352007-10-02 21:55:22 -04005973 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
5974
5975 /*
5976 * Get the adv_req_t structure for the command that has been
5977 * completed. The adv_req_t structure actually contains the
5978 * completed ADV_SCSI_REQ_Q structure.
5979 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02005980 srb_tag = le32_to_cpu(scsiqp->srb_tag);
5981 scp = scsi_host_find_tag(boardp->shost, scsiqp->srb_tag);
Matthew Wilcox51219352007-10-02 21:55:22 -04005982
Matthew Wilcoxb352f922007-10-02 21:55:33 -04005983 ASC_DBG(1, "scp 0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04005984 if (scp == NULL) {
5985 ASC_PRINT
5986 ("adv_isr_callback: scp is NULL; adv_req_t dropped.\n");
5987 return;
5988 }
5989 ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
5990
Hannes Reinecke9c17c622015-04-24 13:18:21 +02005991 reqp = (adv_req_t *)scp->host_scribble;
5992 ASC_DBG(1, "reqp 0x%lx\n", (ulong)reqp);
5993 if (reqp == NULL) {
5994 ASC_PRINT("adv_isr_callback: reqp is NULL\n");
5995 return;
5996 }
5997 /*
5998 * Remove backreferences to avoid duplicate
5999 * command completions.
6000 */
6001 scp->host_scribble = NULL;
6002 reqp->cmndp = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04006003
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006004 ASC_STATS(boardp->shost, callback);
6005 ASC_DBG(1, "shost 0x%p\n", boardp->shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04006006
Hannes Reinecke811ddc02015-04-24 13:18:22 +02006007 sense_addr = le32_to_cpu(scsiqp->sense_addr);
6008 dma_unmap_single(boardp->dev, sense_addr,
6009 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
6010
Matthew Wilcox51219352007-10-02 21:55:22 -04006011 /*
6012 * 'done_status' contains the command's ending status.
6013 */
6014 switch (scsiqp->done_status) {
6015 case QD_NO_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006016 ASC_DBG(2, "QD_NO_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006017 scp->result = 0;
6018
6019 /*
6020 * Check for an underrun condition.
6021 *
6022 * If there was no error and an underrun condition, then
6023 * then return the number of underrun bytes.
6024 */
6025 resid_cnt = le32_to_cpu(scsiqp->data_cnt);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04006026 if (scsi_bufflen(scp) != 0 && resid_cnt != 0 &&
6027 resid_cnt <= scsi_bufflen(scp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006028 ASC_DBG(1, "underrun condition %lu bytes\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04006029 (ulong)resid_cnt);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04006030 scsi_set_resid(scp, resid_cnt);
Matthew Wilcox51219352007-10-02 21:55:22 -04006031 }
6032 break;
6033
6034 case QD_WITH_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006035 ASC_DBG(2, "QD_WITH_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006036 switch (scsiqp->host_status) {
6037 case QHSTA_NO_ERROR:
6038 if (scsiqp->scsi_status == SAM_STAT_CHECK_CONDITION) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006039 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006040 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09006041 SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04006042 /*
6043 * Note: The 'status_byte()' macro used by
6044 * target drivers defined in scsi.h shifts the
6045 * status byte returned by host drivers right
6046 * by 1 bit. This is why target drivers also
6047 * use right shifted status byte definitions.
6048 * For instance target drivers use
6049 * CHECK_CONDITION, defined to 0x1, instead of
6050 * the SCSI defined check condition value of
6051 * 0x2. Host drivers are supposed to return
6052 * the status byte as it is defined by SCSI.
6053 */
6054 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
6055 STATUS_BYTE(scsiqp->scsi_status);
6056 } else {
6057 scp->result = STATUS_BYTE(scsiqp->scsi_status);
6058 }
6059 break;
6060
6061 default:
6062 /* Some other QHSTA error occurred. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006063 ASC_DBG(1, "host_status 0x%x\n", scsiqp->host_status);
Matthew Wilcox51219352007-10-02 21:55:22 -04006064 scp->result = HOST_BYTE(DID_BAD_TARGET);
6065 break;
6066 }
6067 break;
6068
6069 case QD_ABORTED_BY_HOST:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006070 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006071 scp->result =
6072 HOST_BYTE(DID_ABORT) | STATUS_BYTE(scsiqp->scsi_status);
6073 break;
6074
6075 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006076 ASC_DBG(1, "done_status 0x%x\n", scsiqp->done_status);
Matthew Wilcox51219352007-10-02 21:55:22 -04006077 scp->result =
6078 HOST_BYTE(DID_ERROR) | STATUS_BYTE(scsiqp->scsi_status);
6079 break;
6080 }
6081
6082 /*
6083 * If the 'init_tidmask' bit isn't already set for the target and the
6084 * current request finished normally, then set the bit for the target
6085 * to indicate that a device is present.
6086 */
6087 if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
6088 scsiqp->done_status == QD_NO_ERROR &&
6089 scsiqp->host_status == QHSTA_NO_ERROR) {
6090 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
6091 }
6092
6093 asc_scsi_done(scp);
6094
6095 /*
6096 * Free all 'adv_sgblk_t' structures allocated for the request.
6097 */
6098 while ((sgblkp = reqp->sgblkp) != NULL) {
6099 /* Remove 'sgblkp' from the request list. */
6100 reqp->sgblkp = sgblkp->next_sgblkp;
6101
Hannes Reinecke0ce53822015-04-24 13:18:25 +02006102 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
6103 sgblkp->sg_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04006104 }
6105
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006106 ASC_DBG(1, "done\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006107}
6108
6109/*
6110 * Adv Library Interrupt Service Routine
6111 *
6112 * This function is called by a driver's interrupt service routine.
6113 * The function disables and re-enables interrupts.
6114 *
6115 * When a microcode idle command is completed, the ADV_DVC_VAR
6116 * 'idle_cmd_done' field is set to ADV_TRUE.
6117 *
6118 * Note: AdvISR() can be called when interrupts are disabled or even
6119 * when there is no hardware interrupt condition present. It will
6120 * always check for completed idle commands and microcode requests.
6121 * This is an important feature that shouldn't be changed because it
6122 * allows commands to be completed from polling mode loops.
6123 *
6124 * Return:
6125 * ADV_TRUE(1) - interrupt was pending
6126 * ADV_FALSE(0) - no interrupt was pending
6127 */
6128static int AdvISR(ADV_DVC_VAR *asc_dvc)
6129{
6130 AdvPortAddr iop_base;
6131 uchar int_stat;
6132 ushort target_bit;
6133 ADV_CARR_T *free_carrp;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006134 __le32 irq_next_vpa;
Matthew Wilcox51219352007-10-02 21:55:22 -04006135 ADV_SCSI_REQ_Q *scsiq;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02006136 adv_req_t *reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04006137
6138 iop_base = asc_dvc->iop_base;
6139
6140 /* Reading the register clears the interrupt. */
6141 int_stat = AdvReadByteRegister(iop_base, IOPB_INTR_STATUS_REG);
6142
6143 if ((int_stat & (ADV_INTR_STATUS_INTRA | ADV_INTR_STATUS_INTRB |
6144 ADV_INTR_STATUS_INTRC)) == 0) {
6145 return ADV_FALSE;
6146 }
6147
6148 /*
6149 * Notify the driver of an asynchronous microcode condition by
6150 * calling the adv_async_callback function. The function
6151 * is passed the microcode ASC_MC_INTRB_CODE byte value.
6152 */
6153 if (int_stat & ADV_INTR_STATUS_INTRB) {
6154 uchar intrb_code;
6155
6156 AdvReadByteLram(iop_base, ASC_MC_INTRB_CODE, intrb_code);
6157
6158 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
6159 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
6160 if (intrb_code == ADV_ASYNC_CARRIER_READY_FAILURE &&
6161 asc_dvc->carr_pending_cnt != 0) {
6162 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
6163 ADV_TICKLE_A);
6164 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
6165 AdvWriteByteRegister(iop_base,
6166 IOPB_TICKLE,
6167 ADV_TICKLE_NOP);
6168 }
6169 }
6170 }
6171
6172 adv_async_callback(asc_dvc, intrb_code);
6173 }
6174
6175 /*
6176 * Check if the IRQ stopper carrier contains a completed request.
6177 */
6178 while (((irq_next_vpa =
Hannes Reinecke084e6c32015-04-24 13:18:35 +02006179 le32_to_cpu(asc_dvc->irq_sp->next_vpa)) & ADV_RQ_DONE) != 0) {
Matthew Wilcox51219352007-10-02 21:55:22 -04006180 /*
6181 * Get a pointer to the newly completed ADV_SCSI_REQ_Q structure.
6182 * The RISC will have set 'areq_vpa' to a virtual address.
6183 *
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02006184 * The firmware will have copied the ADV_SCSI_REQ_Q.scsiq_ptr
Matthew Wilcox51219352007-10-02 21:55:22 -04006185 * field to the carrier ADV_CARR_T.areq_vpa field. The conversion
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02006186 * below complements the conversion of ADV_SCSI_REQ_Q.scsiq_ptr'
Matthew Wilcox51219352007-10-02 21:55:22 -04006187 * in AdvExeScsiQueue().
6188 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02006189 u32 pa_offset = le32_to_cpu(asc_dvc->irq_sp->areq_vpa);
6190 ASC_DBG(1, "irq_sp %p areq_vpa %u\n",
6191 asc_dvc->irq_sp, pa_offset);
6192 reqp = adv_get_reqp(asc_dvc, pa_offset);
6193 scsiq = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04006194
6195 /*
6196 * Request finished with good status and the queue was not
6197 * DMAed to host memory by the firmware. Set all status fields
6198 * to indicate good status.
6199 */
Hannes Reinecke084e6c32015-04-24 13:18:35 +02006200 if ((irq_next_vpa & ADV_RQ_GOOD) != 0) {
Matthew Wilcox51219352007-10-02 21:55:22 -04006201 scsiq->done_status = QD_NO_ERROR;
6202 scsiq->host_status = scsiq->scsi_status = 0;
6203 scsiq->data_cnt = 0L;
6204 }
6205
6206 /*
6207 * Advance the stopper pointer to the next carrier
6208 * ignoring the lower four bits. Free the previous
6209 * stopper carrier.
6210 */
6211 free_carrp = asc_dvc->irq_sp;
Hannes Reinecke98b96a72015-04-24 13:18:23 +02006212 asc_dvc->irq_sp = adv_get_carrier(asc_dvc,
Hannes Reinecke084e6c32015-04-24 13:18:35 +02006213 ADV_GET_CARRP(irq_next_vpa));
Matthew Wilcox51219352007-10-02 21:55:22 -04006214
Hannes Reinecke98b96a72015-04-24 13:18:23 +02006215 free_carrp->next_vpa = asc_dvc->carr_freelist->carr_va;
Matthew Wilcox51219352007-10-02 21:55:22 -04006216 asc_dvc->carr_freelist = free_carrp;
6217 asc_dvc->carr_pending_cnt--;
6218
6219 target_bit = ADV_TID_TO_TIDMASK(scsiq->target_id);
6220
6221 /*
6222 * Clear request microcode control flag.
6223 */
6224 scsiq->cntl = 0;
6225
6226 /*
6227 * Notify the driver of the completed request by passing
6228 * the ADV_SCSI_REQ_Q pointer to its callback function.
6229 */
Matthew Wilcox51219352007-10-02 21:55:22 -04006230 adv_isr_callback(asc_dvc, scsiq);
6231 /*
6232 * Note: After the driver callback function is called, 'scsiq'
6233 * can no longer be referenced.
6234 *
6235 * Fall through and continue processing other completed
6236 * requests...
6237 */
6238 }
6239 return ADV_TRUE;
6240}
6241
6242static int AscSetLibErrorCode(ASC_DVC_VAR *asc_dvc, ushort err_code)
6243{
6244 if (asc_dvc->err_code == 0) {
6245 asc_dvc->err_code = err_code;
6246 AscWriteLramWord(asc_dvc->iop_base, ASCV_ASCDVC_ERR_CODE_W,
6247 err_code);
6248 }
6249 return err_code;
6250}
6251
6252static void AscAckInterrupt(PortAddr iop_base)
6253{
6254 uchar host_flag;
6255 uchar risc_flag;
6256 ushort loop;
6257
6258 loop = 0;
6259 do {
6260 risc_flag = AscReadLramByte(iop_base, ASCV_RISC_FLAG_B);
6261 if (loop++ > 0x7FFF) {
6262 break;
6263 }
6264 } while ((risc_flag & ASC_RISC_FLAG_GEN_INT) != 0);
6265 host_flag =
6266 AscReadLramByte(iop_base,
6267 ASCV_HOST_FLAG_B) & (~ASC_HOST_FLAG_ACK_INT);
6268 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
6269 (uchar)(host_flag | ASC_HOST_FLAG_ACK_INT));
6270 AscSetChipStatus(iop_base, CIW_INT_ACK);
6271 loop = 0;
6272 while (AscGetChipStatus(iop_base) & CSW_INT_PENDING) {
6273 AscSetChipStatus(iop_base, CIW_INT_ACK);
6274 if (loop++ > 3) {
6275 break;
6276 }
6277 }
6278 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
Matthew Wilcox51219352007-10-02 21:55:22 -04006279}
6280
6281static uchar AscGetSynPeriodIndex(ASC_DVC_VAR *asc_dvc, uchar syn_time)
6282{
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006283 const uchar *period_table;
Matthew Wilcox51219352007-10-02 21:55:22 -04006284 int max_index;
6285 int min_index;
6286 int i;
6287
6288 period_table = asc_dvc->sdtr_period_tbl;
6289 max_index = (int)asc_dvc->max_sdtr_index;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006290 min_index = (int)asc_dvc->min_sdtr_index;
Matthew Wilcox51219352007-10-02 21:55:22 -04006291 if ((syn_time <= period_table[max_index])) {
6292 for (i = min_index; i < (max_index - 1); i++) {
6293 if (syn_time <= period_table[i]) {
6294 return (uchar)i;
6295 }
6296 }
6297 return (uchar)max_index;
6298 } else {
6299 return (uchar)(max_index + 1);
6300 }
6301}
6302
6303static uchar
6304AscMsgOutSDTR(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar sdtr_offset)
6305{
6306 EXT_MSG sdtr_buf;
6307 uchar sdtr_period_index;
6308 PortAddr iop_base;
6309
6310 iop_base = asc_dvc->iop_base;
6311 sdtr_buf.msg_type = EXTENDED_MESSAGE;
6312 sdtr_buf.msg_len = MS_SDTR_LEN;
6313 sdtr_buf.msg_req = EXTENDED_SDTR;
6314 sdtr_buf.xfer_period = sdtr_period;
6315 sdtr_offset &= ASC_SYN_MAX_OFFSET;
6316 sdtr_buf.req_ack_offset = sdtr_offset;
6317 sdtr_period_index = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
6318 if (sdtr_period_index <= asc_dvc->max_sdtr_index) {
6319 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6320 (uchar *)&sdtr_buf,
6321 sizeof(EXT_MSG) >> 1);
6322 return ((sdtr_period_index << 4) | sdtr_offset);
6323 } else {
6324 sdtr_buf.req_ack_offset = 0;
6325 AscMemWordCopyPtrToLram(iop_base, ASCV_MSGOUT_BEG,
6326 (uchar *)&sdtr_buf,
6327 sizeof(EXT_MSG) >> 1);
6328 return 0;
6329 }
6330}
6331
6332static uchar
6333AscCalSDTRData(ASC_DVC_VAR *asc_dvc, uchar sdtr_period, uchar syn_offset)
6334{
6335 uchar byte;
6336 uchar sdtr_period_ix;
6337
6338 sdtr_period_ix = AscGetSynPeriodIndex(asc_dvc, sdtr_period);
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006339 if (sdtr_period_ix > asc_dvc->max_sdtr_index)
Matthew Wilcox51219352007-10-02 21:55:22 -04006340 return 0xFF;
Matthew Wilcox51219352007-10-02 21:55:22 -04006341 byte = (sdtr_period_ix << 4) | (syn_offset & ASC_SYN_MAX_OFFSET);
6342 return byte;
6343}
6344
Hannes Reinecked647c782015-04-24 13:18:26 +02006345static bool AscSetChipSynRegAtID(PortAddr iop_base, uchar id, uchar sdtr_data)
Matthew Wilcox51219352007-10-02 21:55:22 -04006346{
6347 ASC_SCSI_BIT_ID_TYPE org_id;
6348 int i;
Hannes Reinecked647c782015-04-24 13:18:26 +02006349 bool sta = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04006350
6351 AscSetBank(iop_base, 1);
6352 org_id = AscReadChipDvcID(iop_base);
6353 for (i = 0; i <= ASC_MAX_TID; i++) {
6354 if (org_id == (0x01 << i))
6355 break;
6356 }
6357 org_id = (ASC_SCSI_BIT_ID_TYPE) i;
6358 AscWriteChipDvcID(iop_base, id);
6359 if (AscReadChipDvcID(iop_base) == (0x01 << id)) {
6360 AscSetBank(iop_base, 0);
6361 AscSetChipSyn(iop_base, sdtr_data);
6362 if (AscGetChipSyn(iop_base) != sdtr_data) {
Hannes Reinecked647c782015-04-24 13:18:26 +02006363 sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006364 }
6365 } else {
Hannes Reinecked647c782015-04-24 13:18:26 +02006366 sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006367 }
6368 AscSetBank(iop_base, 1);
6369 AscWriteChipDvcID(iop_base, org_id);
6370 AscSetBank(iop_base, 0);
6371 return (sta);
6372}
6373
6374static void AscSetChipSDTR(PortAddr iop_base, uchar sdtr_data, uchar tid_no)
6375{
6376 AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
6377 AscPutMCodeSDTRDoneAtID(iop_base, tid_no, sdtr_data);
6378}
6379
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006380static void AscIsrChipHalted(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04006381{
6382 EXT_MSG ext_msg;
6383 EXT_MSG out_msg;
6384 ushort halt_q_addr;
Hannes Reineckeae267592015-04-24 13:18:28 +02006385 bool sdtr_accept;
Matthew Wilcox51219352007-10-02 21:55:22 -04006386 ushort int_halt_code;
6387 ASC_SCSI_BIT_ID_TYPE scsi_busy;
6388 ASC_SCSI_BIT_ID_TYPE target_id;
6389 PortAddr iop_base;
6390 uchar tag_code;
6391 uchar q_status;
6392 uchar halt_qp;
6393 uchar sdtr_data;
6394 uchar target_ix;
6395 uchar q_cntl, tid_no;
6396 uchar cur_dvc_qng;
6397 uchar asyn_sdtr;
6398 uchar scsi_status;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04006399 struct asc_board *boardp;
Matthew Wilcox51219352007-10-02 21:55:22 -04006400
6401 BUG_ON(!asc_dvc->drv_ptr);
6402 boardp = asc_dvc->drv_ptr;
6403
6404 iop_base = asc_dvc->iop_base;
6405 int_halt_code = AscReadLramWord(iop_base, ASCV_HALTCODE_W);
6406
6407 halt_qp = AscReadLramByte(iop_base, ASCV_CURCDB_B);
6408 halt_q_addr = ASC_QNO_TO_QADDR(halt_qp);
6409 target_ix = AscReadLramByte(iop_base,
6410 (ushort)(halt_q_addr +
6411 (ushort)ASC_SCSIQ_B_TARGET_IX));
6412 q_cntl = AscReadLramByte(iop_base,
6413 (ushort)(halt_q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6414 tid_no = ASC_TIX_TO_TID(target_ix);
6415 target_id = (uchar)ASC_TID_TO_TARGET_ID(tid_no);
6416 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6417 asyn_sdtr = ASYN_SDTR_DATA_FIX_PCI_REV_AB;
6418 } else {
6419 asyn_sdtr = 0;
6420 }
6421 if (int_halt_code == ASC_HALT_DISABLE_ASYN_USE_SYN_FIX) {
6422 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6423 AscSetChipSDTR(iop_base, 0, tid_no);
6424 boardp->sdtr_data[tid_no] = 0;
6425 }
6426 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006427 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006428 } else if (int_halt_code == ASC_HALT_ENABLE_ASYN_USE_SYN_FIX) {
6429 if (asc_dvc->pci_fix_asyn_xfer & target_id) {
6430 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6431 boardp->sdtr_data[tid_no] = asyn_sdtr;
6432 }
6433 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006434 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006435 } else if (int_halt_code == ASC_HALT_EXTMSG_IN) {
6436 AscMemWordCopyPtrFromLram(iop_base,
6437 ASCV_MSGIN_BEG,
6438 (uchar *)&ext_msg,
6439 sizeof(EXT_MSG) >> 1);
6440
6441 if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6442 ext_msg.msg_req == EXTENDED_SDTR &&
6443 ext_msg.msg_len == MS_SDTR_LEN) {
Hannes Reineckeae267592015-04-24 13:18:28 +02006444 sdtr_accept = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04006445 if ((ext_msg.req_ack_offset > ASC_SYN_MAX_OFFSET)) {
6446
Hannes Reineckeae267592015-04-24 13:18:28 +02006447 sdtr_accept = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006448 ext_msg.req_ack_offset = ASC_SYN_MAX_OFFSET;
6449 }
6450 if ((ext_msg.xfer_period <
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006451 asc_dvc->sdtr_period_tbl[asc_dvc->min_sdtr_index])
Matthew Wilcox51219352007-10-02 21:55:22 -04006452 || (ext_msg.xfer_period >
6453 asc_dvc->sdtr_period_tbl[asc_dvc->
6454 max_sdtr_index])) {
Hannes Reineckeae267592015-04-24 13:18:28 +02006455 sdtr_accept = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006456 ext_msg.xfer_period =
6457 asc_dvc->sdtr_period_tbl[asc_dvc->
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04006458 min_sdtr_index];
Matthew Wilcox51219352007-10-02 21:55:22 -04006459 }
6460 if (sdtr_accept) {
6461 sdtr_data =
6462 AscCalSDTRData(asc_dvc, ext_msg.xfer_period,
6463 ext_msg.req_ack_offset);
6464 if ((sdtr_data == 0xFF)) {
6465
6466 q_cntl |= QC_MSG_OUT;
6467 asc_dvc->init_sdtr &= ~target_id;
6468 asc_dvc->sdtr_done &= ~target_id;
6469 AscSetChipSDTR(iop_base, asyn_sdtr,
6470 tid_no);
6471 boardp->sdtr_data[tid_no] = asyn_sdtr;
6472 }
6473 }
6474 if (ext_msg.req_ack_offset == 0) {
6475
6476 q_cntl &= ~QC_MSG_OUT;
6477 asc_dvc->init_sdtr &= ~target_id;
6478 asc_dvc->sdtr_done &= ~target_id;
6479 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6480 } else {
6481 if (sdtr_accept && (q_cntl & QC_MSG_OUT)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04006482 q_cntl &= ~QC_MSG_OUT;
6483 asc_dvc->sdtr_done |= target_id;
6484 asc_dvc->init_sdtr |= target_id;
6485 asc_dvc->pci_fix_asyn_xfer &=
6486 ~target_id;
6487 sdtr_data =
6488 AscCalSDTRData(asc_dvc,
6489 ext_msg.xfer_period,
6490 ext_msg.
6491 req_ack_offset);
6492 AscSetChipSDTR(iop_base, sdtr_data,
6493 tid_no);
6494 boardp->sdtr_data[tid_no] = sdtr_data;
6495 } else {
Matthew Wilcox51219352007-10-02 21:55:22 -04006496 q_cntl |= QC_MSG_OUT;
6497 AscMsgOutSDTR(asc_dvc,
6498 ext_msg.xfer_period,
6499 ext_msg.req_ack_offset);
6500 asc_dvc->pci_fix_asyn_xfer &=
6501 ~target_id;
6502 sdtr_data =
6503 AscCalSDTRData(asc_dvc,
6504 ext_msg.xfer_period,
6505 ext_msg.
6506 req_ack_offset);
6507 AscSetChipSDTR(iop_base, sdtr_data,
6508 tid_no);
6509 boardp->sdtr_data[tid_no] = sdtr_data;
6510 asc_dvc->sdtr_done |= target_id;
6511 asc_dvc->init_sdtr |= target_id;
6512 }
6513 }
6514
6515 AscWriteLramByte(iop_base,
6516 (ushort)(halt_q_addr +
6517 (ushort)ASC_SCSIQ_B_CNTL),
6518 q_cntl);
6519 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006520 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006521 } else if (ext_msg.msg_type == EXTENDED_MESSAGE &&
6522 ext_msg.msg_req == EXTENDED_WDTR &&
6523 ext_msg.msg_len == MS_WDTR_LEN) {
6524
6525 ext_msg.wdtr_width = 0;
6526 AscMemWordCopyPtrToLram(iop_base,
6527 ASCV_MSGOUT_BEG,
6528 (uchar *)&ext_msg,
6529 sizeof(EXT_MSG) >> 1);
6530 q_cntl |= QC_MSG_OUT;
6531 AscWriteLramByte(iop_base,
6532 (ushort)(halt_q_addr +
6533 (ushort)ASC_SCSIQ_B_CNTL),
6534 q_cntl);
6535 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006536 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006537 } else {
6538
6539 ext_msg.msg_type = MESSAGE_REJECT;
6540 AscMemWordCopyPtrToLram(iop_base,
6541 ASCV_MSGOUT_BEG,
6542 (uchar *)&ext_msg,
6543 sizeof(EXT_MSG) >> 1);
6544 q_cntl |= QC_MSG_OUT;
6545 AscWriteLramByte(iop_base,
6546 (ushort)(halt_q_addr +
6547 (ushort)ASC_SCSIQ_B_CNTL),
6548 q_cntl);
6549 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006550 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006551 }
6552 } else if (int_halt_code == ASC_HALT_CHK_CONDITION) {
6553
6554 q_cntl |= QC_REQ_SENSE;
6555
6556 if ((asc_dvc->init_sdtr & target_id) != 0) {
6557
6558 asc_dvc->sdtr_done &= ~target_id;
6559
6560 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
6561 q_cntl |= QC_MSG_OUT;
6562 AscMsgOutSDTR(asc_dvc,
6563 asc_dvc->
6564 sdtr_period_tbl[(sdtr_data >> 4) &
6565 (uchar)(asc_dvc->
6566 max_sdtr_index -
6567 1)],
6568 (uchar)(sdtr_data & (uchar)
6569 ASC_SYN_MAX_OFFSET));
6570 }
6571
6572 AscWriteLramByte(iop_base,
6573 (ushort)(halt_q_addr +
6574 (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6575
6576 tag_code = AscReadLramByte(iop_base,
6577 (ushort)(halt_q_addr + (ushort)
6578 ASC_SCSIQ_B_TAG_CODE));
6579 tag_code &= 0xDC;
6580 if ((asc_dvc->pci_fix_asyn_xfer & target_id)
6581 && !(asc_dvc->pci_fix_asyn_xfer_always & target_id)
6582 ) {
6583
6584 tag_code |= (ASC_TAG_FLAG_DISABLE_DISCONNECT
6585 | ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);
6586
6587 }
6588 AscWriteLramByte(iop_base,
6589 (ushort)(halt_q_addr +
6590 (ushort)ASC_SCSIQ_B_TAG_CODE),
6591 tag_code);
6592
6593 q_status = AscReadLramByte(iop_base,
6594 (ushort)(halt_q_addr + (ushort)
6595 ASC_SCSIQ_B_STATUS));
6596 q_status |= (QS_READY | QS_BUSY);
6597 AscWriteLramByte(iop_base,
6598 (ushort)(halt_q_addr +
6599 (ushort)ASC_SCSIQ_B_STATUS),
6600 q_status);
6601
6602 scsi_busy = AscReadLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B);
6603 scsi_busy &= ~target_id;
6604 AscWriteLramByte(iop_base, (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6605
6606 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006607 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006608 } else if (int_halt_code == ASC_HALT_SDTR_REJECTED) {
6609
6610 AscMemWordCopyPtrFromLram(iop_base,
6611 ASCV_MSGOUT_BEG,
6612 (uchar *)&out_msg,
6613 sizeof(EXT_MSG) >> 1);
6614
6615 if ((out_msg.msg_type == EXTENDED_MESSAGE) &&
6616 (out_msg.msg_len == MS_SDTR_LEN) &&
6617 (out_msg.msg_req == EXTENDED_SDTR)) {
6618
6619 asc_dvc->init_sdtr &= ~target_id;
6620 asc_dvc->sdtr_done &= ~target_id;
6621 AscSetChipSDTR(iop_base, asyn_sdtr, tid_no);
6622 boardp->sdtr_data[tid_no] = asyn_sdtr;
6623 }
6624 q_cntl &= ~QC_MSG_OUT;
6625 AscWriteLramByte(iop_base,
6626 (ushort)(halt_q_addr +
6627 (ushort)ASC_SCSIQ_B_CNTL), q_cntl);
6628 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006629 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006630 } else if (int_halt_code == ASC_HALT_SS_QUEUE_FULL) {
6631
6632 scsi_status = AscReadLramByte(iop_base,
6633 (ushort)((ushort)halt_q_addr +
6634 (ushort)
6635 ASC_SCSIQ_SCSI_STATUS));
6636 cur_dvc_qng =
6637 AscReadLramByte(iop_base,
6638 (ushort)((ushort)ASC_QADR_BEG +
6639 (ushort)target_ix));
6640 if ((cur_dvc_qng > 0) && (asc_dvc->cur_dvc_qng[tid_no] > 0)) {
6641
6642 scsi_busy = AscReadLramByte(iop_base,
6643 (ushort)ASCV_SCSIBUSY_B);
6644 scsi_busy |= target_id;
6645 AscWriteLramByte(iop_base,
6646 (ushort)ASCV_SCSIBUSY_B, scsi_busy);
6647 asc_dvc->queue_full_or_busy |= target_id;
6648
6649 if (scsi_status == SAM_STAT_TASK_SET_FULL) {
6650 if (cur_dvc_qng > ASC_MIN_TAGGED_CMD) {
6651 cur_dvc_qng -= 1;
6652 asc_dvc->max_dvc_qng[tid_no] =
6653 cur_dvc_qng;
6654
6655 AscWriteLramByte(iop_base,
6656 (ushort)((ushort)
6657 ASCV_MAX_DVC_QNG_BEG
6658 + (ushort)
6659 tid_no),
6660 cur_dvc_qng);
6661
6662 /*
6663 * Set the device queue depth to the
6664 * number of active requests when the
6665 * QUEUE FULL condition was encountered.
6666 */
6667 boardp->queue_full |= target_id;
6668 boardp->queue_full_cnt[tid_no] =
6669 cur_dvc_qng;
6670 }
6671 }
6672 }
6673 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0);
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006674 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006675 }
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02006676 return;
Matthew Wilcox51219352007-10-02 21:55:22 -04006677}
6678
6679/*
6680 * void
6681 * DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6682 *
6683 * Calling/Exit State:
6684 * none
6685 *
6686 * Description:
6687 * Input an ASC_QDONE_INFO structure from the chip
6688 */
6689static void
6690DvcGetQinfo(PortAddr iop_base, ushort s_addr, uchar *inbuf, int words)
6691{
6692 int i;
6693 ushort word;
6694
6695 AscSetChipLramAddr(iop_base, s_addr);
6696 for (i = 0; i < 2 * words; i += 2) {
6697 if (i == 10) {
6698 continue;
6699 }
6700 word = inpw(iop_base + IOP_RAM_DATA);
6701 inbuf[i] = word & 0xff;
6702 inbuf[i + 1] = (word >> 8) & 0xff;
6703 }
6704 ASC_DBG_PRT_HEX(2, "DvcGetQinfo", inbuf, 2 * words);
6705}
6706
6707static uchar
6708_AscCopyLramScsiDoneQ(PortAddr iop_base,
6709 ushort q_addr,
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006710 ASC_QDONE_INFO *scsiq, unsigned int max_dma_count)
Matthew Wilcox51219352007-10-02 21:55:22 -04006711{
6712 ushort _val;
6713 uchar sg_queue_cnt;
6714
6715 DvcGetQinfo(iop_base,
6716 q_addr + ASC_SCSIQ_DONE_INFO_BEG,
6717 (uchar *)scsiq,
6718 (sizeof(ASC_SCSIQ_2) + sizeof(ASC_SCSIQ_3)) / 2);
6719
6720 _val = AscReadLramWord(iop_base,
6721 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS));
6722 scsiq->q_status = (uchar)_val;
6723 scsiq->q_no = (uchar)(_val >> 8);
6724 _val = AscReadLramWord(iop_base,
6725 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_CNTL));
6726 scsiq->cntl = (uchar)_val;
6727 sg_queue_cnt = (uchar)(_val >> 8);
6728 _val = AscReadLramWord(iop_base,
6729 (ushort)(q_addr +
6730 (ushort)ASC_SCSIQ_B_SENSE_LEN));
6731 scsiq->sense_len = (uchar)_val;
6732 scsiq->extra_bytes = (uchar)(_val >> 8);
6733
6734 /*
6735 * Read high word of remain bytes from alternate location.
6736 */
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006737 scsiq->remain_bytes = (((u32)AscReadLramWord(iop_base,
6738 (ushort)(q_addr +
6739 (ushort)
6740 ASC_SCSIQ_W_ALT_DC1)))
Matthew Wilcox51219352007-10-02 21:55:22 -04006741 << 16);
6742 /*
6743 * Read low word of remain bytes from original location.
6744 */
6745 scsiq->remain_bytes += AscReadLramWord(iop_base,
6746 (ushort)(q_addr + (ushort)
6747 ASC_SCSIQ_DW_REMAIN_XFER_CNT));
6748
6749 scsiq->remain_bytes &= max_dma_count;
6750 return sg_queue_cnt;
6751}
6752
6753/*
6754 * asc_isr_callback() - Second Level Interrupt Handler called by AscISR().
6755 *
6756 * Interrupt callback function for the Narrow SCSI Asc Library.
6757 */
6758static void asc_isr_callback(ASC_DVC_VAR *asc_dvc_varp, ASC_QDONE_INFO *qdonep)
6759{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006760 struct asc_board *boardp = asc_dvc_varp->drv_ptr;
6761 u32 srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04006762 struct scsi_cmnd *scp;
Matthew Wilcox51219352007-10-02 21:55:22 -04006763
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006764 ASC_DBG(1, "asc_dvc_varp 0x%p, qdonep 0x%p\n", asc_dvc_varp, qdonep);
Matthew Wilcox51219352007-10-02 21:55:22 -04006765 ASC_DBG_PRT_ASC_QDONE_INFO(2, qdonep);
6766
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006767 /*
6768 * Decrease the srb_tag by 1 to find the SCSI command
6769 */
6770 srb_tag = qdonep->d2.srb_tag - 1;
6771 scp = scsi_host_find_tag(boardp->shost, srb_tag);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04006772 if (!scp)
Matthew Wilcox51219352007-10-02 21:55:22 -04006773 return;
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04006774
Matthew Wilcox51219352007-10-02 21:55:22 -04006775 ASC_DBG_PRT_CDB(2, scp->cmnd, scp->cmd_len);
6776
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006777 ASC_STATS(boardp->shost, callback);
Matthew Wilcox51219352007-10-02 21:55:22 -04006778
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04006779 dma_unmap_single(boardp->dev, scp->SCp.dma_handle,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09006780 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Matthew Wilcox51219352007-10-02 21:55:22 -04006781 /*
6782 * 'qdonep' contains the command's ending status.
6783 */
6784 switch (qdonep->d3.done_stat) {
6785 case QD_NO_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006786 ASC_DBG(2, "QD_NO_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006787 scp->result = 0;
6788
6789 /*
6790 * Check for an underrun condition.
6791 *
6792 * If there was no error and an underrun condition, then
6793 * return the number of underrun bytes.
6794 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04006795 if (scsi_bufflen(scp) != 0 && qdonep->remain_bytes != 0 &&
6796 qdonep->remain_bytes <= scsi_bufflen(scp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006797 ASC_DBG(1, "underrun condition %u bytes\n",
Matthew Wilcox51219352007-10-02 21:55:22 -04006798 (unsigned)qdonep->remain_bytes);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04006799 scsi_set_resid(scp, qdonep->remain_bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04006800 }
6801 break;
6802
6803 case QD_WITH_ERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006804 ASC_DBG(2, "QD_WITH_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006805 switch (qdonep->d3.host_stat) {
6806 case QHSTA_NO_ERROR:
6807 if (qdonep->d3.scsi_stat == SAM_STAT_CHECK_CONDITION) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006808 ASC_DBG(2, "SAM_STAT_CHECK_CONDITION\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006809 ASC_DBG_PRT_SENSE(2, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09006810 SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04006811 /*
6812 * Note: The 'status_byte()' macro used by
6813 * target drivers defined in scsi.h shifts the
6814 * status byte returned by host drivers right
6815 * by 1 bit. This is why target drivers also
6816 * use right shifted status byte definitions.
6817 * For instance target drivers use
6818 * CHECK_CONDITION, defined to 0x1, instead of
6819 * the SCSI defined check condition value of
6820 * 0x2. Host drivers are supposed to return
6821 * the status byte as it is defined by SCSI.
6822 */
6823 scp->result = DRIVER_BYTE(DRIVER_SENSE) |
6824 STATUS_BYTE(qdonep->d3.scsi_stat);
6825 } else {
6826 scp->result = STATUS_BYTE(qdonep->d3.scsi_stat);
6827 }
6828 break;
6829
6830 default:
6831 /* QHSTA error occurred */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006832 ASC_DBG(1, "host_stat 0x%x\n", qdonep->d3.host_stat);
Matthew Wilcox51219352007-10-02 21:55:22 -04006833 scp->result = HOST_BYTE(DID_BAD_TARGET);
6834 break;
6835 }
6836 break;
6837
6838 case QD_ABORTED_BY_HOST:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006839 ASC_DBG(1, "QD_ABORTED_BY_HOST\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04006840 scp->result =
6841 HOST_BYTE(DID_ABORT) | MSG_BYTE(qdonep->d3.
6842 scsi_msg) |
6843 STATUS_BYTE(qdonep->d3.scsi_stat);
6844 break;
6845
6846 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04006847 ASC_DBG(1, "done_stat 0x%x\n", qdonep->d3.done_stat);
Matthew Wilcox51219352007-10-02 21:55:22 -04006848 scp->result =
6849 HOST_BYTE(DID_ERROR) | MSG_BYTE(qdonep->d3.
6850 scsi_msg) |
6851 STATUS_BYTE(qdonep->d3.scsi_stat);
6852 break;
6853 }
6854
6855 /*
6856 * If the 'init_tidmask' bit isn't already set for the target and the
6857 * current request finished normally, then set the bit for the target
6858 * to indicate that a device is present.
6859 */
6860 if ((boardp->init_tidmask & ADV_TID_TO_TIDMASK(scp->device->id)) == 0 &&
6861 qdonep->d3.done_stat == QD_NO_ERROR &&
6862 qdonep->d3.host_stat == QHSTA_NO_ERROR) {
6863 boardp->init_tidmask |= ADV_TID_TO_TIDMASK(scp->device->id);
6864 }
6865
6866 asc_scsi_done(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04006867}
6868
6869static int AscIsrQDone(ASC_DVC_VAR *asc_dvc)
6870{
6871 uchar next_qp;
6872 uchar n_q_used;
6873 uchar sg_list_qp;
6874 uchar sg_queue_cnt;
6875 uchar q_cnt;
6876 uchar done_q_tail;
6877 uchar tid_no;
6878 ASC_SCSI_BIT_ID_TYPE scsi_busy;
6879 ASC_SCSI_BIT_ID_TYPE target_id;
6880 PortAddr iop_base;
6881 ushort q_addr;
6882 ushort sg_q_addr;
6883 uchar cur_target_qng;
6884 ASC_QDONE_INFO scsiq_buf;
6885 ASC_QDONE_INFO *scsiq;
Hannes Reineckeae267592015-04-24 13:18:28 +02006886 bool false_overrun;
Matthew Wilcox51219352007-10-02 21:55:22 -04006887
6888 iop_base = asc_dvc->iop_base;
6889 n_q_used = 1;
6890 scsiq = (ASC_QDONE_INFO *)&scsiq_buf;
6891 done_q_tail = (uchar)AscGetVarDoneQTail(iop_base);
6892 q_addr = ASC_QNO_TO_QADDR(done_q_tail);
6893 next_qp = AscReadLramByte(iop_base,
6894 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_FWD));
6895 if (next_qp != ASC_QLINK_END) {
6896 AscPutVarDoneQTail(iop_base, next_qp);
6897 q_addr = ASC_QNO_TO_QADDR(next_qp);
6898 sg_queue_cnt = _AscCopyLramScsiDoneQ(iop_base, q_addr, scsiq,
6899 asc_dvc->max_dma_count);
6900 AscWriteLramByte(iop_base,
6901 (ushort)(q_addr +
6902 (ushort)ASC_SCSIQ_B_STATUS),
6903 (uchar)(scsiq->
6904 q_status & (uchar)~(QS_READY |
6905 QS_ABORTED)));
6906 tid_no = ASC_TIX_TO_TID(scsiq->d2.target_ix);
6907 target_id = ASC_TIX_TO_TARGET_ID(scsiq->d2.target_ix);
6908 if ((scsiq->cntl & QC_SG_HEAD) != 0) {
6909 sg_q_addr = q_addr;
6910 sg_list_qp = next_qp;
6911 for (q_cnt = 0; q_cnt < sg_queue_cnt; q_cnt++) {
6912 sg_list_qp = AscReadLramByte(iop_base,
6913 (ushort)(sg_q_addr
6914 + (ushort)
6915 ASC_SCSIQ_B_FWD));
6916 sg_q_addr = ASC_QNO_TO_QADDR(sg_list_qp);
6917 if (sg_list_qp == ASC_QLINK_END) {
6918 AscSetLibErrorCode(asc_dvc,
6919 ASCQ_ERR_SG_Q_LINKS);
6920 scsiq->d3.done_stat = QD_WITH_ERROR;
6921 scsiq->d3.host_stat =
6922 QHSTA_D_QDONE_SG_LIST_CORRUPTED;
6923 goto FATAL_ERR_QDONE;
6924 }
6925 AscWriteLramByte(iop_base,
6926 (ushort)(sg_q_addr + (ushort)
6927 ASC_SCSIQ_B_STATUS),
6928 QS_FREE);
6929 }
6930 n_q_used = sg_queue_cnt + 1;
6931 AscPutVarDoneQTail(iop_base, sg_list_qp);
6932 }
6933 if (asc_dvc->queue_full_or_busy & target_id) {
6934 cur_target_qng = AscReadLramByte(iop_base,
6935 (ushort)((ushort)
6936 ASC_QADR_BEG
6937 + (ushort)
6938 scsiq->d2.
6939 target_ix));
6940 if (cur_target_qng < asc_dvc->max_dvc_qng[tid_no]) {
6941 scsi_busy = AscReadLramByte(iop_base, (ushort)
6942 ASCV_SCSIBUSY_B);
6943 scsi_busy &= ~target_id;
6944 AscWriteLramByte(iop_base,
6945 (ushort)ASCV_SCSIBUSY_B,
6946 scsi_busy);
6947 asc_dvc->queue_full_or_busy &= ~target_id;
6948 }
6949 }
6950 if (asc_dvc->cur_total_qng >= n_q_used) {
6951 asc_dvc->cur_total_qng -= n_q_used;
6952 if (asc_dvc->cur_dvc_qng[tid_no] != 0) {
6953 asc_dvc->cur_dvc_qng[tid_no]--;
6954 }
6955 } else {
6956 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CUR_QNG);
6957 scsiq->d3.done_stat = QD_WITH_ERROR;
6958 goto FATAL_ERR_QDONE;
6959 }
Hannes Reinecke9c17c622015-04-24 13:18:21 +02006960 if ((scsiq->d2.srb_tag == 0UL) ||
Matthew Wilcox51219352007-10-02 21:55:22 -04006961 ((scsiq->q_status & QS_ABORTED) != 0)) {
6962 return (0x11);
6963 } else if (scsiq->q_status == QS_DONE) {
Hannes Reineckeae267592015-04-24 13:18:28 +02006964 /*
6965 * This is also curious.
6966 * false_overrun will _always_ be set to 'false'
6967 */
6968 false_overrun = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04006969 if (scsiq->extra_bytes != 0) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02006970 scsiq->remain_bytes += scsiq->extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04006971 }
6972 if (scsiq->d3.done_stat == QD_WITH_ERROR) {
6973 if (scsiq->d3.host_stat ==
6974 QHSTA_M_DATA_OVER_RUN) {
6975 if ((scsiq->
6976 cntl & (QC_DATA_IN | QC_DATA_OUT))
6977 == 0) {
6978 scsiq->d3.done_stat =
6979 QD_NO_ERROR;
6980 scsiq->d3.host_stat =
6981 QHSTA_NO_ERROR;
6982 } else if (false_overrun) {
6983 scsiq->d3.done_stat =
6984 QD_NO_ERROR;
6985 scsiq->d3.host_stat =
6986 QHSTA_NO_ERROR;
6987 }
6988 } else if (scsiq->d3.host_stat ==
6989 QHSTA_M_HUNG_REQ_SCSI_BUS_RESET) {
6990 AscStopChip(iop_base);
6991 AscSetChipControl(iop_base,
6992 (uchar)(CC_SCSI_RESET
6993 | CC_HALT));
6994 udelay(60);
6995 AscSetChipControl(iop_base, CC_HALT);
6996 AscSetChipStatus(iop_base,
6997 CIW_CLR_SCSI_RESET_INT);
6998 AscSetChipStatus(iop_base, 0);
6999 AscSetChipControl(iop_base, 0);
7000 }
7001 }
7002 if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7003 asc_isr_callback(asc_dvc, scsiq);
7004 } else {
7005 if ((AscReadLramByte(iop_base,
7006 (ushort)(q_addr + (ushort)
7007 ASC_SCSIQ_CDB_BEG))
7008 == START_STOP)) {
7009 asc_dvc->unit_not_ready &= ~target_id;
7010 if (scsiq->d3.done_stat != QD_NO_ERROR) {
7011 asc_dvc->start_motor &=
7012 ~target_id;
7013 }
7014 }
7015 }
7016 return (1);
7017 } else {
7018 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_Q_STATUS);
7019 FATAL_ERR_QDONE:
7020 if ((scsiq->cntl & QC_NO_CALLBACK) == 0) {
7021 asc_isr_callback(asc_dvc, scsiq);
7022 }
7023 return (0x80);
7024 }
7025 }
7026 return (0);
7027}
7028
7029static int AscISR(ASC_DVC_VAR *asc_dvc)
7030{
7031 ASC_CS_TYPE chipstat;
7032 PortAddr iop_base;
7033 ushort saved_ram_addr;
7034 uchar ctrl_reg;
7035 uchar saved_ctrl_reg;
7036 int int_pending;
7037 int status;
7038 uchar host_flag;
7039
7040 iop_base = asc_dvc->iop_base;
Hannes Reineckeae267592015-04-24 13:18:28 +02007041 int_pending = ASC_FALSE;
Matthew Wilcox51219352007-10-02 21:55:22 -04007042
7043 if (AscIsIntPending(iop_base) == 0)
7044 return int_pending;
7045
7046 if ((asc_dvc->init_state & ASC_INIT_STATE_END_LOAD_MC) == 0) {
Hannes Reineckeae267592015-04-24 13:18:28 +02007047 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04007048 }
7049 if (asc_dvc->in_critical_cnt != 0) {
7050 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_ON_CRITICAL);
Hannes Reineckeae267592015-04-24 13:18:28 +02007051 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04007052 }
7053 if (asc_dvc->is_in_int) {
7054 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_ISR_RE_ENTRY);
Hannes Reineckeae267592015-04-24 13:18:28 +02007055 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04007056 }
Hannes Reineckeae267592015-04-24 13:18:28 +02007057 asc_dvc->is_in_int = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007058 ctrl_reg = AscGetChipControl(iop_base);
7059 saved_ctrl_reg = ctrl_reg & (~(CC_SCSI_RESET | CC_CHIP_RESET |
7060 CC_SINGLE_STEP | CC_DIAG | CC_TEST));
7061 chipstat = AscGetChipStatus(iop_base);
7062 if (chipstat & CSW_SCSI_RESET_LATCH) {
7063 if (!(asc_dvc->bus_type & (ASC_IS_VL | ASC_IS_EISA))) {
7064 int i = 10;
Hannes Reineckeae267592015-04-24 13:18:28 +02007065 int_pending = ASC_TRUE;
Matthew Wilcox51219352007-10-02 21:55:22 -04007066 asc_dvc->sdtr_done = 0;
7067 saved_ctrl_reg &= (uchar)(~CC_HALT);
7068 while ((AscGetChipStatus(iop_base) &
7069 CSW_SCSI_RESET_ACTIVE) && (i-- > 0)) {
7070 mdelay(100);
7071 }
7072 AscSetChipControl(iop_base, (CC_CHIP_RESET | CC_HALT));
7073 AscSetChipControl(iop_base, CC_HALT);
7074 AscSetChipStatus(iop_base, CIW_CLR_SCSI_RESET_INT);
7075 AscSetChipStatus(iop_base, 0);
7076 chipstat = AscGetChipStatus(iop_base);
7077 }
7078 }
7079 saved_ram_addr = AscGetChipLramAddr(iop_base);
7080 host_flag = AscReadLramByte(iop_base,
7081 ASCV_HOST_FLAG_B) &
7082 (uchar)(~ASC_HOST_FLAG_IN_ISR);
7083 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B,
7084 (uchar)(host_flag | (uchar)ASC_HOST_FLAG_IN_ISR));
7085 if ((chipstat & CSW_INT_PENDING) || (int_pending)) {
7086 AscAckInterrupt(iop_base);
Hannes Reineckeae267592015-04-24 13:18:28 +02007087 int_pending = ASC_TRUE;
Matthew Wilcox51219352007-10-02 21:55:22 -04007088 if ((chipstat & CSW_HALTED) && (ctrl_reg & CC_SINGLE_STEP)) {
Hannes Reinecke6f0d2e12015-04-24 13:18:30 +02007089 AscIsrChipHalted(asc_dvc);
7090 saved_ctrl_reg &= (uchar)(~CC_HALT);
Matthew Wilcox51219352007-10-02 21:55:22 -04007091 } else {
Matthew Wilcox51219352007-10-02 21:55:22 -04007092 if ((asc_dvc->dvc_cntl & ASC_CNTL_INT_MULTI_Q) != 0) {
7093 while (((status =
7094 AscIsrQDone(asc_dvc)) & 0x01) != 0) {
7095 }
7096 } else {
7097 do {
7098 if ((status =
7099 AscIsrQDone(asc_dvc)) == 1) {
7100 break;
7101 }
7102 } while (status == 0x11);
7103 }
7104 if ((status & 0x80) != 0)
Hannes Reineckeae267592015-04-24 13:18:28 +02007105 int_pending = ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04007106 }
7107 }
7108 AscWriteLramByte(iop_base, ASCV_HOST_FLAG_B, host_flag);
7109 AscSetChipLramAddr(iop_base, saved_ram_addr);
7110 AscSetChipControl(iop_base, saved_ctrl_reg);
Hannes Reineckeae267592015-04-24 13:18:28 +02007111 asc_dvc->is_in_int = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007112 return int_pending;
7113}
7114
7115/*
7116 * advansys_reset()
7117 *
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007118 * Reset the host associated with the command 'scp'.
Matthew Wilcox51219352007-10-02 21:55:22 -04007119 *
7120 * This function runs its own thread. Interrupts must be blocked but
7121 * sleeping is allowed and no locking other than for host structures is
7122 * required. Returns SUCCESS or FAILED.
7123 */
7124static int advansys_reset(struct scsi_cmnd *scp)
7125{
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007126 struct Scsi_Host *shost = scp->device->host;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007127 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007128 unsigned long flags;
Matthew Wilcox51219352007-10-02 21:55:22 -04007129 int status;
7130 int ret = SUCCESS;
7131
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007132 ASC_DBG(1, "0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007133
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007134 ASC_STATS(shost, reset);
Matthew Wilcox51219352007-10-02 21:55:22 -04007135
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007136 scmd_printk(KERN_INFO, scp, "SCSI host reset started...\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007137
7138 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007139 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04007140
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007141 /* Reset the chip and SCSI bus. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007142 ASC_DBG(1, "before AscInitAsc1000Driver()\n");
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007143 status = AscInitAsc1000Driver(asc_dvc);
Matthew Wilcox51219352007-10-02 21:55:22 -04007144
Adam Buchbinder6070d812009-12-04 15:47:01 -05007145 /* Refer to ASC_IERR_* definitions for meaning of 'err_code'. */
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03007146 if (asc_dvc->err_code || !asc_dvc->overrun_dma) {
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007147 scmd_printk(KERN_INFO, scp, "SCSI host reset error: "
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -03007148 "0x%x, status: 0x%x\n", asc_dvc->err_code,
7149 status);
Matthew Wilcox51219352007-10-02 21:55:22 -04007150 ret = FAILED;
7151 } else if (status) {
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007152 scmd_printk(KERN_INFO, scp, "SCSI host reset warning: "
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007153 "0x%x\n", status);
Matthew Wilcox51219352007-10-02 21:55:22 -04007154 } else {
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007155 scmd_printk(KERN_INFO, scp, "SCSI host reset "
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007156 "successful\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007157 }
7158
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007159 ASC_DBG(1, "after AscInitAsc1000Driver()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007160 } else {
7161 /*
Matthew Wilcox51219352007-10-02 21:55:22 -04007162 * If the suggest reset bus flags are set, then reset the bus.
7163 * Otherwise only reset the device.
7164 */
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007165 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04007166
7167 /*
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007168 * Reset the chip and SCSI bus.
Matthew Wilcox51219352007-10-02 21:55:22 -04007169 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007170 ASC_DBG(1, "before AdvResetChipAndSB()\n");
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007171 switch (AdvResetChipAndSB(adv_dvc)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04007172 case ASC_TRUE:
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007173 scmd_printk(KERN_INFO, scp, "SCSI host reset "
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007174 "successful\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007175 break;
7176 case ASC_FALSE:
7177 default:
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007178 scmd_printk(KERN_INFO, scp, "SCSI host reset error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007179 ret = FAILED;
7180 break;
7181 }
Matthew Wilcoxf092d222007-10-02 21:55:34 -04007182 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox52fa0772007-10-02 21:55:26 -04007183 AdvISR(adv_dvc);
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +02007184 spin_unlock_irqrestore(shost->host_lock, flags);
Matthew Wilcox51219352007-10-02 21:55:22 -04007185 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007186
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007187 ASC_DBG(1, "ret %d\n", ret);
Matthew Wilcox51219352007-10-02 21:55:22 -04007188
7189 return ret;
7190}
7191
7192/*
7193 * advansys_biosparam()
7194 *
7195 * Translate disk drive geometry if the "BIOS greater than 1 GB"
7196 * support is enabled for a drive.
7197 *
7198 * ip (information pointer) is an int array with the following definition:
7199 * ip[0]: heads
7200 * ip[1]: sectors
7201 * ip[2]: cylinders
7202 */
7203static int
7204advansys_biosparam(struct scsi_device *sdev, struct block_device *bdev,
7205 sector_t capacity, int ip[])
7206{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007207 struct asc_board *boardp = shost_priv(sdev->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04007208
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007209 ASC_DBG(1, "begin\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007210 ASC_STATS(sdev->host, biosparam);
Matthew Wilcox51219352007-10-02 21:55:22 -04007211 if (ASC_NARROW_BOARD(boardp)) {
7212 if ((boardp->dvc_var.asc_dvc_var.dvc_cntl &
7213 ASC_CNTL_BIOS_GT_1GB) && capacity > 0x200000) {
7214 ip[0] = 255;
7215 ip[1] = 63;
7216 } else {
7217 ip[0] = 64;
7218 ip[1] = 32;
7219 }
7220 } else {
7221 if ((boardp->dvc_var.adv_dvc_var.bios_ctrl &
7222 BIOS_CTRL_EXTENDED_XLAT) && capacity > 0x200000) {
7223 ip[0] = 255;
7224 ip[1] = 63;
7225 } else {
7226 ip[0] = 64;
7227 ip[1] = 32;
7228 }
7229 }
7230 ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007231 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007232 return 0;
7233}
7234
7235/*
7236 * First-level interrupt handler.
7237 *
7238 * 'dev_id' is a pointer to the interrupting adapter's Scsi_Host.
7239 */
7240static irqreturn_t advansys_interrupt(int irq, void *dev_id)
7241{
Matthew Wilcox51219352007-10-02 21:55:22 -04007242 struct Scsi_Host *shost = dev_id;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007243 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox51219352007-10-02 21:55:22 -04007244 irqreturn_t result = IRQ_NONE;
7245
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007246 ASC_DBG(2, "boardp 0x%p\n", boardp);
Matthew Wilcoxf092d222007-10-02 21:55:34 -04007247 spin_lock(shost->host_lock);
Matthew Wilcox51219352007-10-02 21:55:22 -04007248 if (ASC_NARROW_BOARD(boardp)) {
7249 if (AscIsIntPending(shost->io_port)) {
7250 result = IRQ_HANDLED;
7251 ASC_STATS(shost, interrupt);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007252 ASC_DBG(1, "before AscISR()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007253 AscISR(&boardp->dvc_var.asc_dvc_var);
7254 }
7255 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007256 ASC_DBG(1, "before AdvISR()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007257 if (AdvISR(&boardp->dvc_var.adv_dvc_var)) {
7258 result = IRQ_HANDLED;
7259 ASC_STATS(shost, interrupt);
7260 }
7261 }
Matthew Wilcoxf092d222007-10-02 21:55:34 -04007262 spin_unlock(shost->host_lock);
Matthew Wilcox51219352007-10-02 21:55:22 -04007263
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007264 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007265 return result;
7266}
7267
Hannes Reinecked647c782015-04-24 13:18:26 +02007268static bool AscHostReqRiscHalt(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04007269{
7270 int count = 0;
Hannes Reinecked647c782015-04-24 13:18:26 +02007271 bool sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007272 uchar saved_stop_code;
7273
7274 if (AscIsChipHalted(iop_base))
Hannes Reinecked647c782015-04-24 13:18:26 +02007275 return true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007276 saved_stop_code = AscReadLramByte(iop_base, ASCV_STOP_CODE_B);
7277 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
7278 ASC_STOP_HOST_REQ_RISC_HALT | ASC_STOP_REQ_RISC_STOP);
7279 do {
7280 if (AscIsChipHalted(iop_base)) {
Hannes Reinecked647c782015-04-24 13:18:26 +02007281 sta = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007282 break;
7283 }
7284 mdelay(100);
7285 } while (count++ < 20);
7286 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, saved_stop_code);
Hannes Reinecked647c782015-04-24 13:18:26 +02007287 return sta;
Matthew Wilcox51219352007-10-02 21:55:22 -04007288}
7289
Hannes Reinecked647c782015-04-24 13:18:26 +02007290static bool
Matthew Wilcox51219352007-10-02 21:55:22 -04007291AscSetRunChipSynRegAtID(PortAddr iop_base, uchar tid_no, uchar sdtr_data)
7292{
Hannes Reinecked647c782015-04-24 13:18:26 +02007293 bool sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007294
7295 if (AscHostReqRiscHalt(iop_base)) {
7296 sta = AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
7297 AscStartChip(iop_base);
7298 }
7299 return sta;
7300}
7301
7302static void AscAsyncFix(ASC_DVC_VAR *asc_dvc, struct scsi_device *sdev)
7303{
7304 char type = sdev->type;
7305 ASC_SCSI_BIT_ID_TYPE tid_bits = 1 << sdev->id;
7306
7307 if (!(asc_dvc->bug_fix_cntl & ASC_BUG_FIX_ASYN_USE_SYN))
7308 return;
7309 if (asc_dvc->init_sdtr & tid_bits)
7310 return;
7311
7312 if ((type == TYPE_ROM) && (strncmp(sdev->vendor, "HP ", 3) == 0))
7313 asc_dvc->pci_fix_asyn_xfer_always |= tid_bits;
7314
7315 asc_dvc->pci_fix_asyn_xfer |= tid_bits;
7316 if ((type == TYPE_PROCESSOR) || (type == TYPE_SCANNER) ||
7317 (type == TYPE_ROM) || (type == TYPE_TAPE))
7318 asc_dvc->pci_fix_asyn_xfer &= ~tid_bits;
7319
7320 if (asc_dvc->pci_fix_asyn_xfer & tid_bits)
7321 AscSetRunChipSynRegAtID(asc_dvc->iop_base, sdev->id,
7322 ASYN_SDTR_DATA_FIX_PCI_REV_AB);
7323}
7324
7325static void
7326advansys_narrow_slave_configure(struct scsi_device *sdev, ASC_DVC_VAR *asc_dvc)
7327{
7328 ASC_SCSI_BIT_ID_TYPE tid_bit = 1 << sdev->id;
7329 ASC_SCSI_BIT_ID_TYPE orig_use_tagged_qng = asc_dvc->use_tagged_qng;
7330
7331 if (sdev->lun == 0) {
7332 ASC_SCSI_BIT_ID_TYPE orig_init_sdtr = asc_dvc->init_sdtr;
7333 if ((asc_dvc->cfg->sdtr_enable & tid_bit) && sdev->sdtr) {
7334 asc_dvc->init_sdtr |= tid_bit;
7335 } else {
7336 asc_dvc->init_sdtr &= ~tid_bit;
7337 }
7338
7339 if (orig_init_sdtr != asc_dvc->init_sdtr)
7340 AscAsyncFix(asc_dvc, sdev);
7341 }
7342
7343 if (sdev->tagged_supported) {
7344 if (asc_dvc->cfg->cmd_qng_enabled & tid_bit) {
7345 if (sdev->lun == 0) {
7346 asc_dvc->cfg->can_tagged_qng |= tid_bit;
7347 asc_dvc->use_tagged_qng |= tid_bit;
7348 }
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007349 scsi_change_queue_depth(sdev,
Matthew Wilcox51219352007-10-02 21:55:22 -04007350 asc_dvc->max_dvc_qng[sdev->id]);
7351 }
7352 } else {
7353 if (sdev->lun == 0) {
7354 asc_dvc->cfg->can_tagged_qng &= ~tid_bit;
7355 asc_dvc->use_tagged_qng &= ~tid_bit;
7356 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007357 }
7358
7359 if ((sdev->lun == 0) &&
7360 (orig_use_tagged_qng != asc_dvc->use_tagged_qng)) {
7361 AscWriteLramByte(asc_dvc->iop_base, ASCV_DISC_ENABLE_B,
7362 asc_dvc->cfg->disc_enable);
7363 AscWriteLramByte(asc_dvc->iop_base, ASCV_USE_TAGGED_QNG_B,
7364 asc_dvc->use_tagged_qng);
7365 AscWriteLramByte(asc_dvc->iop_base, ASCV_CAN_TAGGED_QNG_B,
7366 asc_dvc->cfg->can_tagged_qng);
7367
7368 asc_dvc->max_dvc_qng[sdev->id] =
7369 asc_dvc->cfg->max_tag_qng[sdev->id];
7370 AscWriteLramByte(asc_dvc->iop_base,
7371 (ushort)(ASCV_MAX_DVC_QNG_BEG + sdev->id),
7372 asc_dvc->max_dvc_qng[sdev->id]);
7373 }
7374}
7375
7376/*
7377 * Wide Transfers
7378 *
7379 * If the EEPROM enabled WDTR for the device and the device supports wide
7380 * bus (16 bit) transfers, then turn on the device's 'wdtr_able' bit and
7381 * write the new value to the microcode.
7382 */
7383static void
7384advansys_wide_enable_wdtr(AdvPortAddr iop_base, unsigned short tidmask)
7385{
7386 unsigned short cfg_word;
7387 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7388 if ((cfg_word & tidmask) != 0)
7389 return;
7390
7391 cfg_word |= tidmask;
7392 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7393
7394 /*
7395 * Clear the microcode SDTR and WDTR negotiation done indicators for
7396 * the target to cause it to negotiate with the new setting set above.
7397 * WDTR when accepted causes the target to enter asynchronous mode, so
7398 * SDTR must be negotiated.
7399 */
7400 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7401 cfg_word &= ~tidmask;
7402 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7403 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7404 cfg_word &= ~tidmask;
7405 AdvWriteWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7406}
7407
7408/*
7409 * Synchronous Transfers
7410 *
7411 * If the EEPROM enabled SDTR for the device and the device
7412 * supports synchronous transfers, then turn on the device's
7413 * 'sdtr_able' bit. Write the new value to the microcode.
7414 */
7415static void
7416advansys_wide_enable_sdtr(AdvPortAddr iop_base, unsigned short tidmask)
7417{
7418 unsigned short cfg_word;
7419 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7420 if ((cfg_word & tidmask) != 0)
7421 return;
7422
7423 cfg_word |= tidmask;
7424 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7425
7426 /*
7427 * Clear the microcode "SDTR negotiation" done indicator for the
7428 * target to cause it to negotiate with the new setting set above.
7429 */
7430 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7431 cfg_word &= ~tidmask;
7432 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7433}
7434
7435/*
7436 * PPR (Parallel Protocol Request) Capable
7437 *
7438 * If the device supports DT mode, then it must be PPR capable.
7439 * The PPR message will be used in place of the SDTR and WDTR
7440 * messages to negotiate synchronous speed and offset, transfer
7441 * width, and protocol options.
7442 */
7443static void advansys_wide_enable_ppr(ADV_DVC_VAR *adv_dvc,
7444 AdvPortAddr iop_base, unsigned short tidmask)
7445{
7446 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7447 adv_dvc->ppr_able |= tidmask;
7448 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7449}
7450
7451static void
7452advansys_wide_slave_configure(struct scsi_device *sdev, ADV_DVC_VAR *adv_dvc)
7453{
7454 AdvPortAddr iop_base = adv_dvc->iop_base;
7455 unsigned short tidmask = 1 << sdev->id;
7456
7457 if (sdev->lun == 0) {
7458 /*
7459 * Handle WDTR, SDTR, and Tag Queuing. If the feature
7460 * is enabled in the EEPROM and the device supports the
7461 * feature, then enable it in the microcode.
7462 */
7463
7464 if ((adv_dvc->wdtr_able & tidmask) && sdev->wdtr)
7465 advansys_wide_enable_wdtr(iop_base, tidmask);
7466 if ((adv_dvc->sdtr_able & tidmask) && sdev->sdtr)
7467 advansys_wide_enable_sdtr(iop_base, tidmask);
7468 if (adv_dvc->chip_type == ADV_CHIP_ASC38C1600 && sdev->ppr)
7469 advansys_wide_enable_ppr(adv_dvc, iop_base, tidmask);
7470
7471 /*
7472 * Tag Queuing is disabled for the BIOS which runs in polled
7473 * mode and would see no benefit from Tag Queuing. Also by
7474 * disabling Tag Queuing in the BIOS devices with Tag Queuing
7475 * bugs will at least work with the BIOS.
7476 */
7477 if ((adv_dvc->tagqng_able & tidmask) &&
7478 sdev->tagged_supported) {
7479 unsigned short cfg_word;
7480 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, cfg_word);
7481 cfg_word |= tidmask;
7482 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
7483 cfg_word);
7484 AdvWriteByteLram(iop_base,
7485 ASC_MC_NUMBER_OF_MAX_CMD + sdev->id,
7486 adv_dvc->max_dvc_qng);
7487 }
7488 }
7489
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007490 if ((adv_dvc->tagqng_able & tidmask) && sdev->tagged_supported)
7491 scsi_change_queue_depth(sdev, adv_dvc->max_dvc_qng);
Matthew Wilcox51219352007-10-02 21:55:22 -04007492}
7493
7494/*
7495 * Set the number of commands to queue per device for the
7496 * specified host adapter.
7497 */
7498static int advansys_slave_configure(struct scsi_device *sdev)
7499{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007500 struct asc_board *boardp = shost_priv(sdev->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04007501
Matthew Wilcox51219352007-10-02 21:55:22 -04007502 if (ASC_NARROW_BOARD(boardp))
7503 advansys_narrow_slave_configure(sdev,
7504 &boardp->dvc_var.asc_dvc_var);
7505 else
7506 advansys_wide_slave_configure(sdev,
7507 &boardp->dvc_var.adv_dvc_var);
7508
7509 return 0;
7510}
7511
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007512static __le32 advansys_get_sense_buffer_dma(struct scsi_cmnd *scp)
7513{
7514 struct asc_board *board = shost_priv(scp->device->host);
7515 scp->SCp.dma_handle = dma_map_single(board->dev, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007516 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Hannes Reinecke22070632015-04-24 13:18:39 +02007517 if (dma_mapping_error(board->dev, scp->SCp.dma_handle)) {
7518 ASC_DBG(1, "failed to map sense buffer\n");
7519 return 0;
7520 }
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007521 dma_cache_sync(board->dev, scp->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007522 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007523 return cpu_to_le32(scp->SCp.dma_handle);
7524}
7525
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007526static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
Matthew Wilcox05848b62007-10-02 21:55:25 -04007527 struct asc_scsi_q *asc_scsi_q)
Matthew Wilcox51219352007-10-02 21:55:22 -04007528{
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007529 struct asc_dvc_var *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007530 int use_sg;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007531 u32 srb_tag;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007532
Matthew Wilcox05848b62007-10-02 21:55:25 -04007533 memset(asc_scsi_q, 0, sizeof(*asc_scsi_q));
Matthew Wilcox51219352007-10-02 21:55:22 -04007534
7535 /*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007536 * Set the srb_tag to the command tag + 1, as
7537 * srb_tag '0' is used internally by the chip.
Matthew Wilcox51219352007-10-02 21:55:22 -04007538 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007539 srb_tag = scp->request->tag + 1;
7540 asc_scsi_q->q2.srb_tag = srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007541
7542 /*
7543 * Build the ASC_SCSI_Q request.
7544 */
Matthew Wilcox05848b62007-10-02 21:55:25 -04007545 asc_scsi_q->cdbptr = &scp->cmnd[0];
7546 asc_scsi_q->q2.cdb_len = scp->cmd_len;
7547 asc_scsi_q->q1.target_id = ASC_TID_TO_TARGET_ID(scp->device->id);
7548 asc_scsi_q->q1.target_lun = scp->device->lun;
7549 asc_scsi_q->q2.target_ix =
Matthew Wilcox51219352007-10-02 21:55:22 -04007550 ASC_TIDLUN_TO_IX(scp->device->id, scp->device->lun);
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007551 asc_scsi_q->q1.sense_addr = advansys_get_sense_buffer_dma(scp);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007552 asc_scsi_q->q1.sense_len = SCSI_SENSE_BUFFERSIZE;
Hannes Reinecke22070632015-04-24 13:18:39 +02007553 if (!asc_scsi_q->q1.sense_addr)
7554 return ASC_BUSY;
Matthew Wilcox51219352007-10-02 21:55:22 -04007555
7556 /*
7557 * If there are any outstanding requests for the current target,
7558 * then every 255th request send an ORDERED request. This heuristic
7559 * tries to retain the benefit of request sorting while preventing
7560 * request starvation. 255 is the max number of tags or pending commands
7561 * a device may have outstanding.
7562 *
7563 * The request count is incremented below for every successfully
7564 * started request.
7565 *
7566 */
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007567 if ((asc_dvc->cur_dvc_qng[scp->device->id] > 0) &&
Matthew Wilcox51219352007-10-02 21:55:22 -04007568 (boardp->reqcnt[scp->device->id] % 255) == 0) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007569 asc_scsi_q->q2.tag_code = ORDERED_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04007570 } else {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007571 asc_scsi_q->q2.tag_code = SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04007572 }
7573
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007574 /* Build ASC_SCSI_Q */
7575 use_sg = scsi_dma_map(scp);
Hannes Reinecke22070632015-04-24 13:18:39 +02007576 if (use_sg < 0) {
7577 ASC_DBG(1, "failed to map sglist\n");
7578 return ASC_BUSY;
7579 } else if (use_sg > 0) {
Matthew Wilcox51219352007-10-02 21:55:22 -04007580 int sgcnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04007581 struct scatterlist *slp;
Matthew Wilcox05848b62007-10-02 21:55:25 -04007582 struct asc_sg_head *asc_sg_head;
Matthew Wilcox51219352007-10-02 21:55:22 -04007583
Matthew Wilcox51219352007-10-02 21:55:22 -04007584 if (use_sg > scp->device->host->sg_tablesize) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04007585 scmd_printk(KERN_ERR, scp, "use_sg %d > "
7586 "sg_tablesize %d\n", use_sg,
7587 scp->device->host->sg_tablesize);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007588 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007589 scp->result = HOST_BYTE(DID_ERROR);
7590 return ASC_ERROR;
7591 }
7592
Matthew Wilcox05848b62007-10-02 21:55:25 -04007593 asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) +
7594 use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC);
7595 if (!asc_sg_head) {
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007596 scsi_dma_unmap(scp);
Matthew Wilcox05848b62007-10-02 21:55:25 -04007597 scp->result = HOST_BYTE(DID_SOFT_ERROR);
7598 return ASC_ERROR;
7599 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007600
Matthew Wilcox05848b62007-10-02 21:55:25 -04007601 asc_scsi_q->q1.cntl |= QC_SG_HEAD;
7602 asc_scsi_q->sg_head = asc_sg_head;
7603 asc_scsi_q->q1.data_cnt = 0;
7604 asc_scsi_q->q1.data_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007605 /* This is a byte value, otherwise it would need to be swapped. */
Matthew Wilcox05848b62007-10-02 21:55:25 -04007606 asc_sg_head->entry_cnt = asc_scsi_q->q1.sg_queue_cnt = use_sg;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007607 ASC_STATS_ADD(scp->device->host, xfer_elem,
Matthew Wilcox05848b62007-10-02 21:55:25 -04007608 asc_sg_head->entry_cnt);
Matthew Wilcox51219352007-10-02 21:55:22 -04007609
7610 /*
7611 * Convert scatter-gather list into ASC_SG_HEAD list.
7612 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007613 scsi_for_each_sg(scp, slp, use_sg, sgcnt) {
Matthew Wilcox05848b62007-10-02 21:55:25 -04007614 asc_sg_head->sg_list[sgcnt].addr =
Matthew Wilcox51219352007-10-02 21:55:22 -04007615 cpu_to_le32(sg_dma_address(slp));
Matthew Wilcox05848b62007-10-02 21:55:25 -04007616 asc_sg_head->sg_list[sgcnt].bytes =
Matthew Wilcox51219352007-10-02 21:55:22 -04007617 cpu_to_le32(sg_dma_len(slp));
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007618 ASC_STATS_ADD(scp->device->host, xfer_sect,
7619 DIV_ROUND_UP(sg_dma_len(slp), 512));
Matthew Wilcox51219352007-10-02 21:55:22 -04007620 }
7621 }
7622
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007623 ASC_STATS(scp->device->host, xfer_cnt);
7624
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007625 ASC_DBG_PRT_ASC_SCSI_Q(2, asc_scsi_q);
Matthew Wilcox51219352007-10-02 21:55:22 -04007626 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7627
7628 return ASC_NOERROR;
7629}
7630
7631/*
7632 * Build scatter-gather list for Adv Library (Wide Board).
7633 *
7634 * Additional ADV_SG_BLOCK structures will need to be allocated
7635 * if the total number of scatter-gather elements exceeds
7636 * NO_OF_SG_PER_BLOCK (15). The ADV_SG_BLOCK structures are
7637 * assumed to be physically contiguous.
7638 *
7639 * Return:
7640 * ADV_SUCCESS(1) - SG List successfully created
7641 * ADV_ERROR(-1) - SG List creation failed
7642 */
7643static int
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007644adv_get_sglist(struct asc_board *boardp, adv_req_t *reqp,
7645 ADV_SCSI_REQ_Q *scsiqp, struct scsi_cmnd *scp, int use_sg)
Matthew Wilcox51219352007-10-02 21:55:22 -04007646{
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007647 adv_sgblk_t *sgblkp, *prev_sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007648 struct scatterlist *slp;
7649 int sg_elem_cnt;
7650 ADV_SG_BLOCK *sg_block, *prev_sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007651 dma_addr_t sgblk_paddr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007652 int i;
7653
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007654 slp = scsi_sglist(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007655 sg_elem_cnt = use_sg;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007656 prev_sgblkp = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007657 prev_sg_block = NULL;
7658 reqp->sgblkp = NULL;
7659
7660 for (;;) {
7661 /*
7662 * Allocate a 'adv_sgblk_t' structure from the board free
7663 * list. One 'adv_sgblk_t' structure holds NO_OF_SG_PER_BLOCK
7664 * (15) scatter-gather elements.
7665 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007666 sgblkp = dma_pool_alloc(boardp->adv_sgblk_pool, GFP_ATOMIC,
7667 &sgblk_paddr);
7668 if (!sgblkp) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007669 ASC_DBG(1, "no free adv_sgblk_t\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007670 ASC_STATS(scp->device->host, adv_build_nosg);
7671
7672 /*
7673 * Allocation failed. Free 'adv_sgblk_t' structures
7674 * already allocated for the request.
7675 */
7676 while ((sgblkp = reqp->sgblkp) != NULL) {
7677 /* Remove 'sgblkp' from the request list. */
7678 reqp->sgblkp = sgblkp->next_sgblkp;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007679 sgblkp->next_sgblkp = NULL;
7680 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
7681 sgblkp->sg_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007682 }
7683 return ASC_BUSY;
7684 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007685 /* Complete 'adv_sgblk_t' board allocation. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007686 sgblkp->sg_addr = sgblk_paddr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007687 sgblkp->next_sgblkp = NULL;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007688 sg_block = &sgblkp->sg_block;
Matthew Wilcox51219352007-10-02 21:55:22 -04007689
7690 /*
7691 * Check if this is the first 'adv_sgblk_t' for the
7692 * request.
7693 */
7694 if (reqp->sgblkp == NULL) {
7695 /* Request's first scatter-gather block. */
7696 reqp->sgblkp = sgblkp;
7697
7698 /*
7699 * Set ADV_SCSI_REQ_T ADV_SG_BLOCK virtual and physical
7700 * address pointers.
7701 */
7702 scsiqp->sg_list_ptr = sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007703 scsiqp->sg_real_addr = cpu_to_le32(sgblk_paddr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007704 } else {
7705 /* Request's second or later scatter-gather block. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007706 prev_sgblkp->next_sgblkp = sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007707
7708 /*
7709 * Point the previous ADV_SG_BLOCK structure to
7710 * the newly allocated ADV_SG_BLOCK structure.
7711 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007712 prev_sg_block->sg_ptr = cpu_to_le32(sgblk_paddr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007713 }
7714
7715 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
7716 sg_block->sg_list[i].sg_addr =
7717 cpu_to_le32(sg_dma_address(slp));
7718 sg_block->sg_list[i].sg_count =
7719 cpu_to_le32(sg_dma_len(slp));
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007720 ASC_STATS_ADD(scp->device->host, xfer_sect,
7721 DIV_ROUND_UP(sg_dma_len(slp), 512));
Matthew Wilcox51219352007-10-02 21:55:22 -04007722
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007723 if (--sg_elem_cnt == 0) {
7724 /*
7725 * Last ADV_SG_BLOCK and scatter-gather entry.
7726 */
Matthew Wilcox51219352007-10-02 21:55:22 -04007727 sg_block->sg_cnt = i + 1;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007728 sg_block->sg_ptr = 0L; /* Last ADV_SG_BLOCK in list. */
Matthew Wilcox51219352007-10-02 21:55:22 -04007729 return ADV_SUCCESS;
7730 }
7731 slp++;
7732 }
7733 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
7734 prev_sg_block = sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007735 prev_sgblkp = sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007736 }
7737}
7738
7739/*
7740 * Build a request structure for the Adv Library (Wide Board).
7741 *
7742 * If an adv_req_t can not be allocated to issue the request,
7743 * then return ASC_BUSY. If an error occurs, then return ASC_ERROR.
7744 *
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02007745 * Multi-byte fields in the ADV_SCSI_REQ_Q that are used by the
Matthew Wilcox51219352007-10-02 21:55:22 -04007746 * microcode for DMA addresses or math operations are byte swapped
7747 * to little-endian order.
7748 */
7749static int
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007750adv_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007751 adv_req_t **adv_reqpp)
Matthew Wilcox51219352007-10-02 21:55:22 -04007752{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007753 u32 srb_tag = scp->request->tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007754 adv_req_t *reqp;
7755 ADV_SCSI_REQ_Q *scsiqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007756 int ret;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007757 int use_sg;
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007758 dma_addr_t sense_addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007759
7760 /*
7761 * Allocate an adv_req_t structure from the board to execute
7762 * the command.
7763 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007764 reqp = &boardp->adv_reqp[srb_tag];
7765 if (reqp->cmndp && reqp->cmndp != scp ) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007766 ASC_DBG(1, "no free adv_req_t\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007767 ASC_STATS(scp->device->host, adv_build_noreq);
7768 return ASC_BUSY;
Matthew Wilcox51219352007-10-02 21:55:22 -04007769 }
7770
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007771 reqp->req_addr = boardp->adv_reqp_addr + (srb_tag * sizeof(adv_req_t));
7772
7773 scsiqp = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04007774
7775 /*
7776 * Initialize the structure.
7777 */
7778 scsiqp->cntl = scsiqp->scsi_cntl = scsiqp->done_status = 0;
7779
7780 /*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007781 * Set the srb_tag to the command tag.
Matthew Wilcox51219352007-10-02 21:55:22 -04007782 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007783 scsiqp->srb_tag = srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007784
7785 /*
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007786 * Set 'host_scribble' to point to the adv_req_t structure.
Matthew Wilcox51219352007-10-02 21:55:22 -04007787 */
7788 reqp->cmndp = scp;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007789 scp->host_scribble = (void *)reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007790
7791 /*
7792 * Build the ADV_SCSI_REQ_Q request.
7793 */
7794
7795 /* Set CDB length and copy it to the request structure. */
7796 scsiqp->cdb_len = scp->cmd_len;
7797 /* Copy first 12 CDB bytes to cdb[]. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007798 memcpy(scsiqp->cdb, scp->cmnd, scp->cmd_len < 12 ? scp->cmd_len : 12);
Matthew Wilcox51219352007-10-02 21:55:22 -04007799 /* Copy last 4 CDB bytes, if present, to cdb16[]. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007800 if (scp->cmd_len > 12) {
7801 int cdb16_len = scp->cmd_len - 12;
7802
7803 memcpy(scsiqp->cdb16, &scp->cmnd[12], cdb16_len);
Matthew Wilcox51219352007-10-02 21:55:22 -04007804 }
7805
7806 scsiqp->target_id = scp->device->id;
7807 scsiqp->target_lun = scp->device->lun;
7808
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007809 sense_addr = dma_map_single(boardp->dev, scp->sense_buffer,
7810 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Hannes Reinecke22070632015-04-24 13:18:39 +02007811 if (dma_mapping_error(boardp->dev, sense_addr)) {
7812 ASC_DBG(1, "failed to map sense buffer\n");
7813 ASC_STATS(scp->device->host, adv_build_noreq);
7814 return ASC_BUSY;
7815 }
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007816 scsiqp->sense_addr = cpu_to_le32(sense_addr);
7817 scsiqp->sense_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04007818
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007819 /* Build ADV_SCSI_REQ_Q */
Matthew Wilcox51219352007-10-02 21:55:22 -04007820
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007821 use_sg = scsi_dma_map(scp);
Hannes Reinecke22070632015-04-24 13:18:39 +02007822 if (use_sg < 0) {
7823 ASC_DBG(1, "failed to map SG list\n");
7824 ASC_STATS(scp->device->host, adv_build_noreq);
7825 return ASC_BUSY;
7826 } else if (use_sg == 0) {
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007827 /* Zero-length transfer */
Matthew Wilcox51219352007-10-02 21:55:22 -04007828 reqp->sgblkp = NULL;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007829 scsiqp->data_cnt = 0;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007830
7831 scsiqp->data_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007832 scsiqp->sg_list_ptr = NULL;
7833 scsiqp->sg_real_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007834 } else {
Matthew Wilcox51219352007-10-02 21:55:22 -04007835 if (use_sg > ADV_MAX_SG_LIST) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04007836 scmd_printk(KERN_ERR, scp, "use_sg %d > "
7837 "ADV_MAX_SG_LIST %d\n", use_sg,
Matthew Wilcox51219352007-10-02 21:55:22 -04007838 scp->device->host->sg_tablesize);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007839 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007840 scp->result = HOST_BYTE(DID_ERROR);
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007841 reqp->cmndp = NULL;
7842 scp->host_scribble = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007843
7844 return ASC_ERROR;
7845 }
7846
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007847 scsiqp->data_cnt = cpu_to_le32(scsi_bufflen(scp));
7848
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007849 ret = adv_get_sglist(boardp, reqp, scsiqp, scp, use_sg);
Matthew Wilcox51219352007-10-02 21:55:22 -04007850 if (ret != ADV_SUCCESS) {
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007851 scsi_dma_unmap(scp);
7852 scp->result = HOST_BYTE(DID_ERROR);
7853 reqp->cmndp = NULL;
7854 scp->host_scribble = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007855
7856 return ret;
7857 }
7858
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007859 ASC_STATS_ADD(scp->device->host, xfer_elem, use_sg);
Matthew Wilcox51219352007-10-02 21:55:22 -04007860 }
7861
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007862 ASC_STATS(scp->device->host, xfer_cnt);
7863
Matthew Wilcox51219352007-10-02 21:55:22 -04007864 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
7865 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7866
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007867 *adv_reqpp = reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007868
7869 return ASC_NOERROR;
7870}
7871
7872static int AscSgListToQueue(int sg_list)
7873{
7874 int n_sg_list_qs;
7875
7876 n_sg_list_qs = ((sg_list - 1) / ASC_SG_LIST_PER_Q);
7877 if (((sg_list - 1) % ASC_SG_LIST_PER_Q) != 0)
7878 n_sg_list_qs++;
7879 return n_sg_list_qs + 1;
7880}
7881
7882static uint
7883AscGetNumOfFreeQueue(ASC_DVC_VAR *asc_dvc, uchar target_ix, uchar n_qs)
7884{
7885 uint cur_used_qs;
7886 uint cur_free_qs;
7887 ASC_SCSI_BIT_ID_TYPE target_id;
7888 uchar tid_no;
7889
7890 target_id = ASC_TIX_TO_TARGET_ID(target_ix);
7891 tid_no = ASC_TIX_TO_TID(target_ix);
7892 if ((asc_dvc->unit_not_ready & target_id) ||
7893 (asc_dvc->queue_full_or_busy & target_id)) {
7894 return 0;
7895 }
7896 if (n_qs == 1) {
7897 cur_used_qs = (uint) asc_dvc->cur_total_qng +
7898 (uint) asc_dvc->last_q_shortage + (uint) ASC_MIN_FREE_Q;
7899 } else {
7900 cur_used_qs = (uint) asc_dvc->cur_total_qng +
7901 (uint) ASC_MIN_FREE_Q;
7902 }
7903 if ((uint) (cur_used_qs + n_qs) <= (uint) asc_dvc->max_total_qng) {
7904 cur_free_qs = (uint) asc_dvc->max_total_qng - cur_used_qs;
7905 if (asc_dvc->cur_dvc_qng[tid_no] >=
7906 asc_dvc->max_dvc_qng[tid_no]) {
7907 return 0;
7908 }
7909 return cur_free_qs;
7910 }
7911 if (n_qs > 1) {
7912 if ((n_qs > asc_dvc->last_q_shortage)
7913 && (n_qs <= (asc_dvc->max_total_qng - ASC_MIN_FREE_Q))) {
7914 asc_dvc->last_q_shortage = n_qs;
7915 }
7916 }
7917 return 0;
7918}
7919
7920static uchar AscAllocFreeQueue(PortAddr iop_base, uchar free_q_head)
7921{
7922 ushort q_addr;
7923 uchar next_qp;
7924 uchar q_status;
7925
7926 q_addr = ASC_QNO_TO_QADDR(free_q_head);
7927 q_status = (uchar)AscReadLramByte(iop_base,
7928 (ushort)(q_addr +
7929 ASC_SCSIQ_B_STATUS));
7930 next_qp = AscReadLramByte(iop_base, (ushort)(q_addr + ASC_SCSIQ_B_FWD));
7931 if (((q_status & QS_READY) == 0) && (next_qp != ASC_QLINK_END))
7932 return next_qp;
7933 return ASC_QLINK_END;
7934}
7935
7936static uchar
7937AscAllocMultipleFreeQueue(PortAddr iop_base, uchar free_q_head, uchar n_free_q)
7938{
7939 uchar i;
7940
7941 for (i = 0; i < n_free_q; i++) {
7942 free_q_head = AscAllocFreeQueue(iop_base, free_q_head);
7943 if (free_q_head == ASC_QLINK_END)
7944 break;
7945 }
7946 return free_q_head;
7947}
7948
7949/*
7950 * void
7951 * DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
7952 *
7953 * Calling/Exit State:
7954 * none
7955 *
7956 * Description:
7957 * Output an ASC_SCSI_Q structure to the chip
7958 */
7959static void
7960DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
7961{
7962 int i;
7963
7964 ASC_DBG_PRT_HEX(2, "DvcPutScsiQ", outbuf, 2 * words);
7965 AscSetChipLramAddr(iop_base, s_addr);
7966 for (i = 0; i < 2 * words; i += 2) {
7967 if (i == 4 || i == 20) {
7968 continue;
7969 }
7970 outpw(iop_base + IOP_RAM_DATA,
7971 ((ushort)outbuf[i + 1] << 8) | outbuf[i]);
7972 }
7973}
7974
7975static int AscPutReadyQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
7976{
7977 ushort q_addr;
7978 uchar tid_no;
7979 uchar sdtr_data;
7980 uchar syn_period_ix;
7981 uchar syn_offset;
7982 PortAddr iop_base;
7983
7984 iop_base = asc_dvc->iop_base;
7985 if (((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) &&
7986 ((asc_dvc->sdtr_done & scsiq->q1.target_id) == 0)) {
7987 tid_no = ASC_TIX_TO_TID(scsiq->q2.target_ix);
7988 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
7989 syn_period_ix =
7990 (sdtr_data >> 4) & (asc_dvc->max_sdtr_index - 1);
7991 syn_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
7992 AscMsgOutSDTR(asc_dvc,
7993 asc_dvc->sdtr_period_tbl[syn_period_ix],
7994 syn_offset);
7995 scsiq->q1.cntl |= QC_MSG_OUT;
7996 }
7997 q_addr = ASC_QNO_TO_QADDR(q_no);
7998 if ((scsiq->q1.target_id & asc_dvc->use_tagged_qng) == 0) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007999 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04008000 }
8001 scsiq->q1.status = QS_FREE;
8002 AscMemWordCopyPtrToLram(iop_base,
8003 q_addr + ASC_SCSIQ_CDB_BEG,
8004 (uchar *)scsiq->cdbptr, scsiq->q2.cdb_len >> 1);
8005
8006 DvcPutScsiQ(iop_base,
8007 q_addr + ASC_SCSIQ_CPY_BEG,
8008 (uchar *)&scsiq->q1.cntl,
8009 ((sizeof(ASC_SCSIQ_1) + sizeof(ASC_SCSIQ_2)) / 2) - 1);
8010 AscWriteLramWord(iop_base,
8011 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS),
8012 (ushort)(((ushort)scsiq->q1.
8013 q_no << 8) | (ushort)QS_READY));
8014 return 1;
8015}
8016
8017static int
8018AscPutReadySgListQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8019{
8020 int sta;
8021 int i;
8022 ASC_SG_HEAD *sg_head;
8023 ASC_SG_LIST_Q scsi_sg_q;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008024 __le32 saved_data_addr;
8025 __le32 saved_data_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008026 PortAddr iop_base;
8027 ushort sg_list_dwords;
8028 ushort sg_index;
8029 ushort sg_entry_cnt;
8030 ushort q_addr;
8031 uchar next_qp;
8032
8033 iop_base = asc_dvc->iop_base;
8034 sg_head = scsiq->sg_head;
8035 saved_data_addr = scsiq->q1.data_addr;
8036 saved_data_cnt = scsiq->q1.data_cnt;
Hannes Reinecke37f64602015-04-24 13:18:32 +02008037 scsiq->q1.data_addr = cpu_to_le32(sg_head->sg_list[0].addr);
8038 scsiq->q1.data_cnt = cpu_to_le32(sg_head->sg_list[0].bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008039 /*
Hannes Reinecke96aabb22015-04-24 13:18:34 +02008040 * Set sg_entry_cnt to be the number of SG elements that
8041 * will fit in the allocated SG queues. It is minus 1, because
8042 * the first SG element is handled above.
Matthew Wilcox51219352007-10-02 21:55:22 -04008043 */
Hannes Reinecke96aabb22015-04-24 13:18:34 +02008044 sg_entry_cnt = sg_head->entry_cnt - 1;
Matthew Wilcox51219352007-10-02 21:55:22 -04008045
Matthew Wilcox51219352007-10-02 21:55:22 -04008046 if (sg_entry_cnt != 0) {
8047 scsiq->q1.cntl |= QC_SG_HEAD;
8048 q_addr = ASC_QNO_TO_QADDR(q_no);
8049 sg_index = 1;
8050 scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
8051 scsi_sg_q.sg_head_qp = q_no;
8052 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
8053 for (i = 0; i < sg_head->queue_cnt; i++) {
8054 scsi_sg_q.seq_no = i + 1;
8055 if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
8056 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
8057 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
8058 if (i == 0) {
8059 scsi_sg_q.sg_list_cnt =
8060 ASC_SG_LIST_PER_Q;
8061 scsi_sg_q.sg_cur_list_cnt =
8062 ASC_SG_LIST_PER_Q;
8063 } else {
8064 scsi_sg_q.sg_list_cnt =
8065 ASC_SG_LIST_PER_Q - 1;
8066 scsi_sg_q.sg_cur_list_cnt =
8067 ASC_SG_LIST_PER_Q - 1;
8068 }
8069 } else {
Hannes Reinecke96aabb22015-04-24 13:18:34 +02008070 scsi_sg_q.cntl |= QCSG_SG_XFER_END;
Matthew Wilcox51219352007-10-02 21:55:22 -04008071 sg_list_dwords = sg_entry_cnt << 1;
8072 if (i == 0) {
8073 scsi_sg_q.sg_list_cnt = sg_entry_cnt;
8074 scsi_sg_q.sg_cur_list_cnt =
8075 sg_entry_cnt;
8076 } else {
8077 scsi_sg_q.sg_list_cnt =
8078 sg_entry_cnt - 1;
8079 scsi_sg_q.sg_cur_list_cnt =
8080 sg_entry_cnt - 1;
8081 }
8082 sg_entry_cnt = 0;
8083 }
8084 next_qp = AscReadLramByte(iop_base,
8085 (ushort)(q_addr +
8086 ASC_SCSIQ_B_FWD));
8087 scsi_sg_q.q_no = next_qp;
8088 q_addr = ASC_QNO_TO_QADDR(next_qp);
8089 AscMemWordCopyPtrToLram(iop_base,
8090 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
8091 (uchar *)&scsi_sg_q,
8092 sizeof(ASC_SG_LIST_Q) >> 1);
8093 AscMemDWordCopyPtrToLram(iop_base,
8094 q_addr + ASC_SGQ_LIST_BEG,
8095 (uchar *)&sg_head->
8096 sg_list[sg_index],
8097 sg_list_dwords);
8098 sg_index += ASC_SG_LIST_PER_Q;
8099 scsiq->next_sg_index = sg_index;
8100 }
8101 } else {
8102 scsiq->q1.cntl &= ~QC_SG_HEAD;
8103 }
8104 sta = AscPutReadyQueue(asc_dvc, scsiq, q_no);
8105 scsiq->q1.data_addr = saved_data_addr;
8106 scsiq->q1.data_cnt = saved_data_cnt;
8107 return (sta);
8108}
8109
8110static int
8111AscSendScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar n_q_required)
8112{
8113 PortAddr iop_base;
8114 uchar free_q_head;
8115 uchar next_qp;
8116 uchar tid_no;
8117 uchar target_ix;
8118 int sta;
8119
8120 iop_base = asc_dvc->iop_base;
8121 target_ix = scsiq->q2.target_ix;
8122 tid_no = ASC_TIX_TO_TID(target_ix);
8123 sta = 0;
8124 free_q_head = (uchar)AscGetVarFreeQHead(iop_base);
8125 if (n_q_required > 1) {
8126 next_qp = AscAllocMultipleFreeQueue(iop_base, free_q_head,
8127 (uchar)n_q_required);
8128 if (next_qp != ASC_QLINK_END) {
8129 asc_dvc->last_q_shortage = 0;
8130 scsiq->sg_head->queue_cnt = n_q_required - 1;
8131 scsiq->q1.q_no = free_q_head;
8132 sta = AscPutReadySgListQueue(asc_dvc, scsiq,
8133 free_q_head);
8134 }
8135 } else if (n_q_required == 1) {
8136 next_qp = AscAllocFreeQueue(iop_base, free_q_head);
8137 if (next_qp != ASC_QLINK_END) {
8138 scsiq->q1.q_no = free_q_head;
8139 sta = AscPutReadyQueue(asc_dvc, scsiq, free_q_head);
8140 }
8141 }
8142 if (sta == 1) {
8143 AscPutVarFreeQHead(iop_base, next_qp);
8144 asc_dvc->cur_total_qng += n_q_required;
8145 asc_dvc->cur_dvc_qng[tid_no]++;
8146 }
8147 return sta;
8148}
8149
8150#define ASC_SYN_OFFSET_ONE_DISABLE_LIST 16
8151static uchar _syn_offset_one_disable_cmd[ASC_SYN_OFFSET_ONE_DISABLE_LIST] = {
8152 INQUIRY,
8153 REQUEST_SENSE,
8154 READ_CAPACITY,
8155 READ_TOC,
8156 MODE_SELECT,
8157 MODE_SENSE,
8158 MODE_SELECT_10,
8159 MODE_SENSE_10,
8160 0xFF,
8161 0xFF,
8162 0xFF,
8163 0xFF,
8164 0xFF,
8165 0xFF,
8166 0xFF,
8167 0xFF
8168};
8169
8170static int AscExeScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq)
8171{
8172 PortAddr iop_base;
8173 int sta;
8174 int n_q_required;
Hannes Reineckeae267592015-04-24 13:18:28 +02008175 bool disable_syn_offset_one_fix;
Matthew Wilcox51219352007-10-02 21:55:22 -04008176 int i;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008177 u32 addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04008178 ushort sg_entry_cnt = 0;
8179 ushort sg_entry_cnt_minus_one = 0;
8180 uchar target_ix;
8181 uchar tid_no;
8182 uchar sdtr_data;
8183 uchar extra_bytes;
8184 uchar scsi_cmd;
8185 uchar disable_cmd;
8186 ASC_SG_HEAD *sg_head;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008187 unsigned long data_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008188
8189 iop_base = asc_dvc->iop_base;
8190 sg_head = scsiq->sg_head;
8191 if (asc_dvc->err_code != 0)
Hannes Reineckef1138a82015-04-24 13:18:29 +02008192 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008193 scsiq->q1.q_no = 0;
8194 if ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0) {
8195 scsiq->q1.extra_bytes = 0;
8196 }
8197 sta = 0;
8198 target_ix = scsiq->q2.target_ix;
8199 tid_no = ASC_TIX_TO_TID(target_ix);
8200 n_q_required = 1;
8201 if (scsiq->cdbptr[0] == REQUEST_SENSE) {
8202 if ((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) {
8203 asc_dvc->sdtr_done &= ~scsiq->q1.target_id;
8204 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8205 AscMsgOutSDTR(asc_dvc,
8206 asc_dvc->
8207 sdtr_period_tbl[(sdtr_data >> 4) &
8208 (uchar)(asc_dvc->
8209 max_sdtr_index -
8210 1)],
8211 (uchar)(sdtr_data & (uchar)
8212 ASC_SYN_MAX_OFFSET));
8213 scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);
8214 }
8215 }
8216 if (asc_dvc->in_critical_cnt != 0) {
8217 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CRITICAL_RE_ENTRY);
Hannes Reineckef1138a82015-04-24 13:18:29 +02008218 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008219 }
8220 asc_dvc->in_critical_cnt++;
8221 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8222 if ((sg_entry_cnt = sg_head->entry_cnt) == 0) {
8223 asc_dvc->in_critical_cnt--;
Hannes Reineckef1138a82015-04-24 13:18:29 +02008224 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008225 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008226 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8227 asc_dvc->in_critical_cnt--;
Hannes Reineckef1138a82015-04-24 13:18:29 +02008228 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008229 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008230 if (sg_entry_cnt == 1) {
Hannes Reinecke37f64602015-04-24 13:18:32 +02008231 scsiq->q1.data_addr = cpu_to_le32(sg_head->sg_list[0].addr);
8232 scsiq->q1.data_cnt = cpu_to_le32(sg_head->sg_list[0].bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008233 scsiq->q1.cntl &= ~(QC_SG_HEAD | QC_SG_SWAP_QUEUE);
8234 }
8235 sg_entry_cnt_minus_one = sg_entry_cnt - 1;
8236 }
8237 scsi_cmd = scsiq->cdbptr[0];
Hannes Reineckeae267592015-04-24 13:18:28 +02008238 disable_syn_offset_one_fix = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04008239 if ((asc_dvc->pci_fix_asyn_xfer & scsiq->q1.target_id) &&
8240 !(asc_dvc->pci_fix_asyn_xfer_always & scsiq->q1.target_id)) {
8241 if (scsiq->q1.cntl & QC_SG_HEAD) {
8242 data_cnt = 0;
8243 for (i = 0; i < sg_entry_cnt; i++) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008244 data_cnt += le32_to_cpu(sg_head->sg_list[i].
8245 bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008246 }
8247 } else {
8248 data_cnt = le32_to_cpu(scsiq->q1.data_cnt);
8249 }
8250 if (data_cnt != 0UL) {
8251 if (data_cnt < 512UL) {
Hannes Reineckeae267592015-04-24 13:18:28 +02008252 disable_syn_offset_one_fix = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04008253 } else {
8254 for (i = 0; i < ASC_SYN_OFFSET_ONE_DISABLE_LIST;
8255 i++) {
8256 disable_cmd =
8257 _syn_offset_one_disable_cmd[i];
8258 if (disable_cmd == 0xFF) {
8259 break;
8260 }
8261 if (scsi_cmd == disable_cmd) {
8262 disable_syn_offset_one_fix =
Hannes Reineckeae267592015-04-24 13:18:28 +02008263 true;
Matthew Wilcox51219352007-10-02 21:55:22 -04008264 break;
8265 }
8266 }
8267 }
8268 }
8269 }
8270 if (disable_syn_offset_one_fix) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08008271 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04008272 scsiq->q2.tag_code |= (ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX |
8273 ASC_TAG_FLAG_DISABLE_DISCONNECT);
8274 } else {
8275 scsiq->q2.tag_code &= 0x27;
8276 }
8277 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8278 if (asc_dvc->bug_fix_cntl) {
8279 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8280 if ((scsi_cmd == READ_6) ||
8281 (scsi_cmd == READ_10)) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008282 addr = le32_to_cpu(sg_head->
Matthew Wilcox51219352007-10-02 21:55:22 -04008283 sg_list
8284 [sg_entry_cnt_minus_one].
8285 addr) +
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008286 le32_to_cpu(sg_head->
Matthew Wilcox51219352007-10-02 21:55:22 -04008287 sg_list
8288 [sg_entry_cnt_minus_one].
8289 bytes);
8290 extra_bytes =
8291 (uchar)((ushort)addr & 0x0003);
8292 if ((extra_bytes != 0)
8293 &&
8294 ((scsiq->q2.
8295 tag_code &
8296 ASC_TAG_FLAG_EXTRA_BYTES)
8297 == 0)) {
8298 scsiq->q2.tag_code |=
8299 ASC_TAG_FLAG_EXTRA_BYTES;
8300 scsiq->q1.extra_bytes =
8301 extra_bytes;
8302 data_cnt =
8303 le32_to_cpu(sg_head->
8304 sg_list
8305 [sg_entry_cnt_minus_one].
8306 bytes);
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008307 data_cnt -= extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008308 sg_head->
8309 sg_list
8310 [sg_entry_cnt_minus_one].
8311 bytes =
8312 cpu_to_le32(data_cnt);
8313 }
8314 }
8315 }
8316 }
8317 sg_head->entry_to_copy = sg_head->entry_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008318 n_q_required = AscSgListToQueue(sg_entry_cnt);
8319 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, n_q_required) >=
8320 (uint) n_q_required)
8321 || ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8322 if ((sta =
8323 AscSendScsiQueue(asc_dvc, scsiq,
8324 n_q_required)) == 1) {
8325 asc_dvc->in_critical_cnt--;
8326 return (sta);
8327 }
8328 }
8329 } else {
8330 if (asc_dvc->bug_fix_cntl) {
8331 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8332 if ((scsi_cmd == READ_6) ||
8333 (scsi_cmd == READ_10)) {
8334 addr =
8335 le32_to_cpu(scsiq->q1.data_addr) +
8336 le32_to_cpu(scsiq->q1.data_cnt);
8337 extra_bytes =
8338 (uchar)((ushort)addr & 0x0003);
8339 if ((extra_bytes != 0)
8340 &&
8341 ((scsiq->q2.
8342 tag_code &
8343 ASC_TAG_FLAG_EXTRA_BYTES)
8344 == 0)) {
8345 data_cnt =
8346 le32_to_cpu(scsiq->q1.
8347 data_cnt);
8348 if (((ushort)data_cnt & 0x01FF)
8349 == 0) {
8350 scsiq->q2.tag_code |=
8351 ASC_TAG_FLAG_EXTRA_BYTES;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008352 data_cnt -= extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008353 scsiq->q1.data_cnt =
8354 cpu_to_le32
8355 (data_cnt);
8356 scsiq->q1.extra_bytes =
8357 extra_bytes;
8358 }
8359 }
8360 }
8361 }
8362 }
8363 n_q_required = 1;
8364 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, 1) >= 1) ||
8365 ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8366 if ((sta = AscSendScsiQueue(asc_dvc, scsiq,
8367 n_q_required)) == 1) {
8368 asc_dvc->in_critical_cnt--;
8369 return (sta);
8370 }
8371 }
8372 }
8373 asc_dvc->in_critical_cnt--;
8374 return (sta);
8375}
8376
8377/*
8378 * AdvExeScsiQueue() - Send a request to the RISC microcode program.
8379 *
8380 * Allocate a carrier structure, point the carrier to the ADV_SCSI_REQ_Q,
8381 * add the carrier to the ICQ (Initiator Command Queue), and tickle the
8382 * RISC to notify it a new command is ready to be executed.
8383 *
8384 * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
8385 * set to SCSI_MAX_RETRY.
8386 *
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02008387 * Multi-byte fields in the ADV_SCSI_REQ_Q that are used by the microcode
Matthew Wilcox51219352007-10-02 21:55:22 -04008388 * for DMA addresses or math operations are byte swapped to little-endian
8389 * order.
8390 *
8391 * Return:
8392 * ADV_SUCCESS(1) - The request was successfully queued.
8393 * ADV_BUSY(0) - Resource unavailable; Retry again after pending
8394 * request completes.
8395 * ADV_ERROR(-1) - Invalid ADV_SCSI_REQ_Q request structure
8396 * host IC error.
8397 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008398static int AdvExeScsiQueue(ADV_DVC_VAR *asc_dvc, adv_req_t *reqp)
Matthew Wilcox51219352007-10-02 21:55:22 -04008399{
8400 AdvPortAddr iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008401 ADV_CARR_T *new_carrp;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008402 ADV_SCSI_REQ_Q *scsiq = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04008403
8404 /*
8405 * The ADV_SCSI_REQ_Q 'target_id' field should never exceed ADV_MAX_TID.
8406 */
8407 if (scsiq->target_id > ADV_MAX_TID) {
8408 scsiq->host_status = QHSTA_M_INVALID_DEVICE;
8409 scsiq->done_status = QD_WITH_ERROR;
8410 return ADV_ERROR;
8411 }
8412
8413 iop_base = asc_dvc->iop_base;
8414
8415 /*
8416 * Allocate a carrier ensuring at least one carrier always
8417 * remains on the freelist and initialize fields.
8418 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008419 new_carrp = adv_get_next_carrier(asc_dvc);
8420 if (!new_carrp) {
8421 ASC_DBG(1, "No free carriers\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008422 return ADV_BUSY;
8423 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008424
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008425 asc_dvc->carr_pending_cnt++;
Matthew Wilcox51219352007-10-02 21:55:22 -04008426
Matthew Wilcox51219352007-10-02 21:55:22 -04008427 /* Save virtual and physical address of ADV_SCSI_REQ_Q and carrier. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008428 scsiq->scsiq_ptr = cpu_to_le32(scsiq->srb_tag);
8429 scsiq->scsiq_rptr = cpu_to_le32(reqp->req_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04008430
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008431 scsiq->carr_va = asc_dvc->icq_sp->carr_va;
Matthew Wilcox51219352007-10-02 21:55:22 -04008432 scsiq->carr_pa = asc_dvc->icq_sp->carr_pa;
8433
8434 /*
8435 * Use the current stopper to send the ADV_SCSI_REQ_Q command to
8436 * the microcode. The newly allocated stopper will become the new
8437 * stopper.
8438 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008439 asc_dvc->icq_sp->areq_vpa = scsiq->scsiq_rptr;
Matthew Wilcox51219352007-10-02 21:55:22 -04008440
8441 /*
8442 * Set the 'next_vpa' pointer for the old stopper to be the
8443 * physical address of the new stopper. The RISC can only
8444 * follow physical addresses.
8445 */
8446 asc_dvc->icq_sp->next_vpa = new_carrp->carr_pa;
8447
8448 /*
8449 * Set the host adapter stopper pointer to point to the new carrier.
8450 */
8451 asc_dvc->icq_sp = new_carrp;
8452
8453 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
8454 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
8455 /*
8456 * Tickle the RISC to tell it to read its Command Queue Head pointer.
8457 */
8458 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_A);
8459 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
8460 /*
8461 * Clear the tickle value. In the ASC-3550 the RISC flag
8462 * command 'clr_tickle_a' does not work unless the host
8463 * value is cleared.
8464 */
8465 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
8466 ADV_TICKLE_NOP);
8467 }
8468 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
8469 /*
8470 * Notify the RISC a carrier is ready by writing the physical
8471 * address of the new carrier stopper to the COMMA register.
8472 */
8473 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
8474 le32_to_cpu(new_carrp->carr_pa));
8475 }
8476
8477 return ADV_SUCCESS;
8478}
8479
8480/*
8481 * Execute a single 'Scsi_Cmnd'.
Matthew Wilcox51219352007-10-02 21:55:22 -04008482 */
8483static int asc_execute_scsi_cmnd(struct scsi_cmnd *scp)
8484{
Matthew Wilcox41d24932007-10-02 21:55:24 -04008485 int ret, err_code;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04008486 struct asc_board *boardp = shost_priv(scp->device->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04008487
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008488 ASC_DBG(1, "scp 0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008489
8490 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox41d24932007-10-02 21:55:24 -04008491 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox05848b62007-10-02 21:55:25 -04008492 struct asc_scsi_q asc_scsi_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04008493
Matthew Wilcox05848b62007-10-02 21:55:25 -04008494 ret = asc_build_req(boardp, scp, &asc_scsi_q);
Hannes Reinecke22070632015-04-24 13:18:39 +02008495 if (ret != ASC_NOERROR) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008496 ASC_STATS(scp->device->host, build_error);
Hannes Reinecke22070632015-04-24 13:18:39 +02008497 return ret;
Matthew Wilcox51219352007-10-02 21:55:22 -04008498 }
8499
Matthew Wilcox41d24932007-10-02 21:55:24 -04008500 ret = AscExeScsiQueue(asc_dvc, &asc_scsi_q);
Matthew Wilcox05848b62007-10-02 21:55:25 -04008501 kfree(asc_scsi_q.sg_head);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008502 err_code = asc_dvc->err_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04008503 } else {
Matthew Wilcox41d24932007-10-02 21:55:24 -04008504 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008505 adv_req_t *adv_reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04008506
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008507 switch (adv_build_req(boardp, scp, &adv_reqp)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008508 case ASC_NOERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008509 ASC_DBG(3, "adv_build_req ASC_NOERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008510 break;
8511 case ASC_BUSY:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008512 ASC_DBG(1, "adv_build_req ASC_BUSY\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008513 /*
8514 * The asc_stats fields 'adv_build_noreq' and
8515 * 'adv_build_nosg' count wide board busy conditions.
8516 * They are updated in adv_build_req and
8517 * adv_get_sglist, respectively.
8518 */
8519 return ASC_BUSY;
8520 case ASC_ERROR:
8521 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008522 ASC_DBG(1, "adv_build_req ASC_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008523 ASC_STATS(scp->device->host, build_error);
8524 return ASC_ERROR;
8525 }
8526
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008527 ret = AdvExeScsiQueue(adv_dvc, adv_reqp);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008528 err_code = adv_dvc->err_code;
8529 }
8530
8531 switch (ret) {
8532 case ASC_NOERROR:
8533 ASC_STATS(scp->device->host, exe_noerror);
8534 /*
8535 * Increment monotonically increasing per device
8536 * successful request counter. Wrapping doesn't matter.
8537 */
8538 boardp->reqcnt[scp->device->id]++;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008539 ASC_DBG(1, "ExeScsiQueue() ASC_NOERROR\n");
Matthew Wilcox41d24932007-10-02 21:55:24 -04008540 break;
8541 case ASC_BUSY:
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008542 ASC_DBG(1, "ExeScsiQueue() ASC_BUSY\n");
Matthew Wilcox41d24932007-10-02 21:55:24 -04008543 ASC_STATS(scp->device->host, exe_busy);
8544 break;
8545 case ASC_ERROR:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008546 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() ASC_ERROR, "
8547 "err_code 0x%x\n", err_code);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008548 ASC_STATS(scp->device->host, exe_error);
8549 scp->result = HOST_BYTE(DID_ERROR);
8550 break;
8551 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008552 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() unknown, "
8553 "err_code 0x%x\n", err_code);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008554 ASC_STATS(scp->device->host, exe_unknown);
8555 scp->result = HOST_BYTE(DID_ERROR);
8556 break;
Matthew Wilcox51219352007-10-02 21:55:22 -04008557 }
8558
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008559 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008560 return ret;
8561}
8562
8563/*
8564 * advansys_queuecommand() - interrupt-driven I/O entrypoint.
8565 *
8566 * This function always returns 0. Command return status is saved
8567 * in the 'scp' result field.
8568 */
8569static int
Jeff Garzikf2812332010-11-16 02:10:29 -05008570advansys_queuecommand_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
Matthew Wilcox51219352007-10-02 21:55:22 -04008571{
8572 struct Scsi_Host *shost = scp->device->host;
Matthew Wilcox51219352007-10-02 21:55:22 -04008573 int asc_res, result = 0;
8574
8575 ASC_STATS(shost, queuecommand);
8576 scp->scsi_done = done;
8577
Matthew Wilcox51219352007-10-02 21:55:22 -04008578 asc_res = asc_execute_scsi_cmnd(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008579
8580 switch (asc_res) {
8581 case ASC_NOERROR:
8582 break;
8583 case ASC_BUSY:
8584 result = SCSI_MLQUEUE_HOST_BUSY;
8585 break;
8586 case ASC_ERROR:
8587 default:
8588 asc_scsi_done(scp);
8589 break;
8590 }
8591
8592 return result;
8593}
8594
Jeff Garzikf2812332010-11-16 02:10:29 -05008595static DEF_SCSI_QCMD(advansys_queuecommand)
8596
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008597static ushort AscGetEisaChipCfg(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008598{
8599 PortAddr eisa_cfg_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8600 (PortAddr) (ASC_EISA_CFG_IOP_MASK);
8601 return inpw(eisa_cfg_iop);
8602}
8603
8604/*
8605 * Return the BIOS address of the adapter at the specified
8606 * I/O port and with the specified bus type.
8607 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008608static unsigned short AscGetChipBiosAddress(PortAddr iop_base,
8609 unsigned short bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008610{
8611 unsigned short cfg_lsw;
8612 unsigned short bios_addr;
8613
8614 /*
8615 * The PCI BIOS is re-located by the motherboard BIOS. Because
8616 * of this the driver can not determine where a PCI BIOS is
8617 * loaded and executes.
8618 */
8619 if (bus_type & ASC_IS_PCI)
8620 return 0;
8621
8622 if ((bus_type & ASC_IS_EISA) != 0) {
8623 cfg_lsw = AscGetEisaChipCfg(iop_base);
8624 cfg_lsw &= 0x000F;
8625 bios_addr = ASC_BIOS_MIN_ADDR + cfg_lsw * ASC_BIOS_BANK_SIZE;
8626 return bios_addr;
8627 }
8628
8629 cfg_lsw = AscGetChipCfgLsw(iop_base);
8630
8631 /*
8632 * ISA PnP uses the top bit as the 32K BIOS flag
8633 */
8634 if (bus_type == ASC_IS_ISAPNP)
8635 cfg_lsw &= 0x7FFF;
8636 bios_addr = ASC_BIOS_MIN_ADDR + (cfg_lsw >> 12) * ASC_BIOS_BANK_SIZE;
8637 return bios_addr;
8638}
8639
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008640static uchar AscSetChipScsiID(PortAddr iop_base, uchar new_host_id)
Matthew Wilcox51219352007-10-02 21:55:22 -04008641{
8642 ushort cfg_lsw;
8643
8644 if (AscGetChipScsiID(iop_base) == new_host_id) {
8645 return (new_host_id);
8646 }
8647 cfg_lsw = AscGetChipCfgLsw(iop_base);
8648 cfg_lsw &= 0xF8FF;
8649 cfg_lsw |= (ushort)((new_host_id & ASC_MAX_TID) << 8);
8650 AscSetChipCfgLsw(iop_base, cfg_lsw);
8651 return (AscGetChipScsiID(iop_base));
8652}
8653
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008654static unsigned char AscGetChipScsiCtrl(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008655{
8656 unsigned char sc;
8657
8658 AscSetBank(iop_base, 1);
8659 sc = inp(iop_base + IOP_REG_SC);
8660 AscSetBank(iop_base, 0);
8661 return sc;
8662}
8663
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008664static unsigned char AscGetChipVersion(PortAddr iop_base,
8665 unsigned short bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008666{
8667 if (bus_type & ASC_IS_EISA) {
8668 PortAddr eisa_iop;
8669 unsigned char revision;
8670 eisa_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8671 (PortAddr) ASC_EISA_REV_IOP_MASK;
8672 revision = inp(eisa_iop);
8673 return ASC_CHIP_MIN_VER_EISA - 1 + revision;
8674 }
8675 return AscGetChipVerNo(iop_base);
8676}
8677
Matthew Wilcox51219352007-10-02 21:55:22 -04008678#ifdef CONFIG_ISA
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008679static void AscEnableIsaDma(uchar dma_channel)
Matthew Wilcox51219352007-10-02 21:55:22 -04008680{
8681 if (dma_channel < 4) {
8682 outp(0x000B, (ushort)(0xC0 | dma_channel));
8683 outp(0x000A, dma_channel);
8684 } else if (dma_channel < 8) {
8685 outp(0x00D6, (ushort)(0xC0 | (dma_channel - 4)));
8686 outp(0x00D4, (ushort)(dma_channel - 4));
8687 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008688}
8689#endif /* CONFIG_ISA */
8690
8691static int AscStopQueueExe(PortAddr iop_base)
8692{
8693 int count = 0;
8694
8695 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) == 0) {
8696 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
8697 ASC_STOP_REQ_RISC_STOP);
8698 do {
8699 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) &
8700 ASC_STOP_ACK_RISC_STOP) {
8701 return (1);
8702 }
8703 mdelay(100);
8704 } while (count++ < 20);
8705 }
8706 return (0);
8707}
8708
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008709static unsigned int AscGetMaxDmaCount(ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008710{
8711 if (bus_type & ASC_IS_ISA)
8712 return ASC_MAX_ISA_DMA_COUNT;
8713 else if (bus_type & (ASC_IS_EISA | ASC_IS_VL))
8714 return ASC_MAX_VL_DMA_COUNT;
8715 return ASC_MAX_PCI_DMA_COUNT;
8716}
8717
8718#ifdef CONFIG_ISA
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008719static ushort AscGetIsaDmaChannel(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008720{
8721 ushort channel;
8722
8723 channel = AscGetChipCfgLsw(iop_base) & 0x0003;
8724 if (channel == 0x03)
8725 return (0);
8726 else if (channel == 0x00)
8727 return (7);
8728 return (channel + 4);
8729}
8730
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008731static ushort AscSetIsaDmaChannel(PortAddr iop_base, ushort dma_channel)
Matthew Wilcox51219352007-10-02 21:55:22 -04008732{
8733 ushort cfg_lsw;
8734 uchar value;
8735
8736 if ((dma_channel >= 5) && (dma_channel <= 7)) {
8737 if (dma_channel == 7)
8738 value = 0x00;
8739 else
8740 value = dma_channel - 4;
8741 cfg_lsw = AscGetChipCfgLsw(iop_base) & 0xFFFC;
8742 cfg_lsw |= value;
8743 AscSetChipCfgLsw(iop_base, cfg_lsw);
8744 return (AscGetIsaDmaChannel(iop_base));
8745 }
8746 return 0;
8747}
8748
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008749static uchar AscGetIsaDmaSpeed(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008750{
8751 uchar speed_value;
8752
8753 AscSetBank(iop_base, 1);
8754 speed_value = AscReadChipDmaSpeed(iop_base);
8755 speed_value &= 0x07;
8756 AscSetBank(iop_base, 0);
8757 return speed_value;
8758}
8759
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008760static uchar AscSetIsaDmaSpeed(PortAddr iop_base, uchar speed_value)
Matthew Wilcox51219352007-10-02 21:55:22 -04008761{
8762 speed_value &= 0x07;
8763 AscSetBank(iop_base, 1);
8764 AscWriteChipDmaSpeed(iop_base, speed_value);
8765 AscSetBank(iop_base, 0);
8766 return AscGetIsaDmaSpeed(iop_base);
8767}
8768#endif /* CONFIG_ISA */
8769
Hannes Reineckef33134e2015-04-24 13:18:31 +02008770static void AscInitAscDvcVar(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04008771{
8772 int i;
8773 PortAddr iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008774 uchar chip_version;
8775
8776 iop_base = asc_dvc->iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008777 asc_dvc->err_code = 0;
8778 if ((asc_dvc->bus_type &
8779 (ASC_IS_ISA | ASC_IS_PCI | ASC_IS_EISA | ASC_IS_VL)) == 0) {
8780 asc_dvc->err_code |= ASC_IERR_NO_BUS_TYPE;
8781 }
8782 AscSetChipControl(iop_base, CC_HALT);
8783 AscSetChipStatus(iop_base, 0);
8784 asc_dvc->bug_fix_cntl = 0;
8785 asc_dvc->pci_fix_asyn_xfer = 0;
8786 asc_dvc->pci_fix_asyn_xfer_always = 0;
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02008787 /* asc_dvc->init_state initialized in AscInitGetConfig(). */
Matthew Wilcox51219352007-10-02 21:55:22 -04008788 asc_dvc->sdtr_done = 0;
8789 asc_dvc->cur_total_qng = 0;
Hannes Reineckeae267592015-04-24 13:18:28 +02008790 asc_dvc->is_in_int = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04008791 asc_dvc->in_critical_cnt = 0;
8792 asc_dvc->last_q_shortage = 0;
8793 asc_dvc->use_tagged_qng = 0;
8794 asc_dvc->no_scam = 0;
8795 asc_dvc->unit_not_ready = 0;
8796 asc_dvc->queue_full_or_busy = 0;
8797 asc_dvc->redo_scam = 0;
8798 asc_dvc->res2 = 0;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04008799 asc_dvc->min_sdtr_index = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04008800 asc_dvc->cfg->can_tagged_qng = 0;
8801 asc_dvc->cfg->cmd_qng_enabled = 0;
8802 asc_dvc->dvc_cntl = ASC_DEF_DVC_CNTL;
8803 asc_dvc->init_sdtr = 0;
8804 asc_dvc->max_total_qng = ASC_DEF_MAX_TOTAL_QNG;
8805 asc_dvc->scsi_reset_wait = 3;
8806 asc_dvc->start_motor = ASC_SCSI_WIDTH_BIT_SET;
8807 asc_dvc->max_dma_count = AscGetMaxDmaCount(asc_dvc->bus_type);
8808 asc_dvc->cfg->sdtr_enable = ASC_SCSI_WIDTH_BIT_SET;
8809 asc_dvc->cfg->disc_enable = ASC_SCSI_WIDTH_BIT_SET;
8810 asc_dvc->cfg->chip_scsi_id = ASC_DEF_CHIP_SCSI_ID;
Matthew Wilcox51219352007-10-02 21:55:22 -04008811 chip_version = AscGetChipVersion(iop_base, asc_dvc->bus_type);
8812 asc_dvc->cfg->chip_version = chip_version;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04008813 asc_dvc->sdtr_period_tbl = asc_syn_xfer_period;
Matthew Wilcox51219352007-10-02 21:55:22 -04008814 asc_dvc->max_sdtr_index = 7;
8815 if ((asc_dvc->bus_type & ASC_IS_PCI) &&
8816 (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3150)) {
8817 asc_dvc->bus_type = ASC_IS_PCI_ULTRA;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04008818 asc_dvc->sdtr_period_tbl = asc_syn_ultra_xfer_period;
Matthew Wilcox51219352007-10-02 21:55:22 -04008819 asc_dvc->max_sdtr_index = 15;
8820 if (chip_version == ASC_CHIP_VER_PCI_ULTRA_3150) {
8821 AscSetExtraControl(iop_base,
8822 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
8823 } else if (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3050) {
8824 AscSetExtraControl(iop_base,
8825 (SEC_ACTIVE_NEGATE |
8826 SEC_ENABLE_FILTER));
8827 }
8828 }
8829 if (asc_dvc->bus_type == ASC_IS_PCI) {
8830 AscSetExtraControl(iop_base,
8831 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
8832 }
8833
8834 asc_dvc->cfg->isa_dma_speed = ASC_DEF_ISA_DMA_SPEED;
8835#ifdef CONFIG_ISA
8836 if ((asc_dvc->bus_type & ASC_IS_ISA) != 0) {
8837 if (chip_version >= ASC_CHIP_MIN_VER_ISA_PNP) {
8838 AscSetChipIFC(iop_base, IFC_INIT_DEFAULT);
8839 asc_dvc->bus_type = ASC_IS_ISAPNP;
8840 }
8841 asc_dvc->cfg->isa_dma_channel =
8842 (uchar)AscGetIsaDmaChannel(iop_base);
8843 }
8844#endif /* CONFIG_ISA */
8845 for (i = 0; i <= ASC_MAX_TID; i++) {
8846 asc_dvc->cur_dvc_qng[i] = 0;
8847 asc_dvc->max_dvc_qng[i] = ASC_MAX_SCSI1_QNG;
8848 asc_dvc->scsiq_busy_head[i] = (ASC_SCSI_Q *)0L;
8849 asc_dvc->scsiq_busy_tail[i] = (ASC_SCSI_Q *)0L;
8850 asc_dvc->cfg->max_tag_qng[i] = ASC_MAX_INRAM_TAG_QNG;
8851 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008852}
8853
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008854static int AscWriteEEPCmdReg(PortAddr iop_base, uchar cmd_reg)
Matthew Wilcox51219352007-10-02 21:55:22 -04008855{
8856 int retry;
8857
8858 for (retry = 0; retry < ASC_EEP_MAX_RETRY; retry++) {
8859 unsigned char read_back;
8860 AscSetChipEEPCmd(iop_base, cmd_reg);
8861 mdelay(1);
8862 read_back = AscGetChipEEPCmd(iop_base);
8863 if (read_back == cmd_reg)
8864 return 1;
8865 }
8866 return 0;
8867}
8868
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008869static void AscWaitEEPRead(void)
Matthew Wilcox51219352007-10-02 21:55:22 -04008870{
8871 mdelay(1);
8872}
8873
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008874static ushort AscReadEEPWord(PortAddr iop_base, uchar addr)
Matthew Wilcox51219352007-10-02 21:55:22 -04008875{
8876 ushort read_wval;
8877 uchar cmd_reg;
8878
8879 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
8880 AscWaitEEPRead();
8881 cmd_reg = addr | ASC_EEP_CMD_READ;
8882 AscWriteEEPCmdReg(iop_base, cmd_reg);
8883 AscWaitEEPRead();
8884 read_wval = AscGetChipEEPData(iop_base);
8885 AscWaitEEPRead();
8886 return read_wval;
8887}
8888
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008889static ushort AscGetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
8890 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008891{
8892 ushort wval;
8893 ushort sum;
8894 ushort *wbuf;
8895 int cfg_beg;
8896 int cfg_end;
8897 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
8898 int s_addr;
8899
8900 wbuf = (ushort *)cfg_buf;
8901 sum = 0;
8902 /* Read two config words; Byte-swapping done by AscReadEEPWord(). */
8903 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
8904 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
8905 sum += *wbuf;
8906 }
8907 if (bus_type & ASC_IS_VL) {
8908 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
8909 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
8910 } else {
8911 cfg_beg = ASC_EEP_DVC_CFG_BEG;
8912 cfg_end = ASC_EEP_MAX_DVC_ADDR;
8913 }
8914 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
8915 wval = AscReadEEPWord(iop_base, (uchar)s_addr);
8916 if (s_addr <= uchar_end_in_config) {
8917 /*
8918 * Swap all char fields - must unswap bytes already swapped
8919 * by AscReadEEPWord().
8920 */
8921 *wbuf = le16_to_cpu(wval);
8922 } else {
8923 /* Don't swap word field at the end - cntl field. */
8924 *wbuf = wval;
8925 }
8926 sum += wval; /* Checksum treats all EEPROM data as words. */
8927 }
8928 /*
8929 * Read the checksum word which will be compared against 'sum'
8930 * by the caller. Word field already swapped.
8931 */
8932 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
8933 return sum;
8934}
8935
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008936static int AscTestExternalLram(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04008937{
8938 PortAddr iop_base;
8939 ushort q_addr;
8940 ushort saved_word;
8941 int sta;
8942
8943 iop_base = asc_dvc->iop_base;
8944 sta = 0;
8945 q_addr = ASC_QNO_TO_QADDR(241);
8946 saved_word = AscReadLramWord(iop_base, q_addr);
8947 AscSetChipLramAddr(iop_base, q_addr);
8948 AscSetChipLramData(iop_base, 0x55AA);
8949 mdelay(10);
8950 AscSetChipLramAddr(iop_base, q_addr);
8951 if (AscGetChipLramData(iop_base) == 0x55AA) {
8952 sta = 1;
8953 AscWriteLramWord(iop_base, q_addr, saved_word);
8954 }
8955 return (sta);
8956}
8957
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008958static void AscWaitEEPWrite(void)
Matthew Wilcox51219352007-10-02 21:55:22 -04008959{
8960 mdelay(20);
Matthew Wilcox51219352007-10-02 21:55:22 -04008961}
8962
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008963static int AscWriteEEPDataReg(PortAddr iop_base, ushort data_reg)
Matthew Wilcox51219352007-10-02 21:55:22 -04008964{
8965 ushort read_back;
8966 int retry;
8967
8968 retry = 0;
Hannes Reineckeae267592015-04-24 13:18:28 +02008969 while (true) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008970 AscSetChipEEPData(iop_base, data_reg);
8971 mdelay(1);
8972 read_back = AscGetChipEEPData(iop_base);
8973 if (read_back == data_reg) {
8974 return (1);
8975 }
8976 if (retry++ > ASC_EEP_MAX_RETRY) {
8977 return (0);
8978 }
8979 }
8980}
8981
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008982static ushort AscWriteEEPWord(PortAddr iop_base, uchar addr, ushort word_val)
Matthew Wilcox51219352007-10-02 21:55:22 -04008983{
8984 ushort read_wval;
8985
8986 read_wval = AscReadEEPWord(iop_base, addr);
8987 if (read_wval != word_val) {
8988 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_ABLE);
8989 AscWaitEEPRead();
8990 AscWriteEEPDataReg(iop_base, word_val);
8991 AscWaitEEPRead();
8992 AscWriteEEPCmdReg(iop_base,
8993 (uchar)((uchar)ASC_EEP_CMD_WRITE | addr));
8994 AscWaitEEPWrite();
8995 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
8996 AscWaitEEPRead();
8997 return (AscReadEEPWord(iop_base, addr));
8998 }
8999 return (read_wval);
9000}
9001
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009002static int AscSetEEPConfigOnce(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9003 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009004{
9005 int n_error;
9006 ushort *wbuf;
9007 ushort word;
9008 ushort sum;
9009 int s_addr;
9010 int cfg_beg;
9011 int cfg_end;
9012 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9013
9014 wbuf = (ushort *)cfg_buf;
9015 n_error = 0;
9016 sum = 0;
9017 /* Write two config words; AscWriteEEPWord() will swap bytes. */
9018 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9019 sum += *wbuf;
9020 if (*wbuf != AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9021 n_error++;
9022 }
9023 }
9024 if (bus_type & ASC_IS_VL) {
9025 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9026 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9027 } else {
9028 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9029 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9030 }
9031 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9032 if (s_addr <= uchar_end_in_config) {
9033 /*
9034 * This is a char field. Swap char fields before they are
9035 * swapped again by AscWriteEEPWord().
9036 */
9037 word = cpu_to_le16(*wbuf);
9038 if (word !=
9039 AscWriteEEPWord(iop_base, (uchar)s_addr, word)) {
9040 n_error++;
9041 }
9042 } else {
9043 /* Don't swap word field at the end - cntl field. */
9044 if (*wbuf !=
9045 AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9046 n_error++;
9047 }
9048 }
9049 sum += *wbuf; /* Checksum calculated from word values. */
9050 }
9051 /* Write checksum word. It will be swapped by AscWriteEEPWord(). */
9052 *wbuf = sum;
9053 if (sum != AscWriteEEPWord(iop_base, (uchar)s_addr, sum)) {
9054 n_error++;
9055 }
9056
9057 /* Read EEPROM back again. */
9058 wbuf = (ushort *)cfg_buf;
9059 /*
9060 * Read two config words; Byte-swapping done by AscReadEEPWord().
9061 */
9062 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9063 if (*wbuf != AscReadEEPWord(iop_base, (uchar)s_addr)) {
9064 n_error++;
9065 }
9066 }
9067 if (bus_type & ASC_IS_VL) {
9068 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9069 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9070 } else {
9071 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9072 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9073 }
9074 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9075 if (s_addr <= uchar_end_in_config) {
9076 /*
9077 * Swap all char fields. Must unswap bytes already swapped
9078 * by AscReadEEPWord().
9079 */
9080 word =
9081 le16_to_cpu(AscReadEEPWord
9082 (iop_base, (uchar)s_addr));
9083 } else {
9084 /* Don't swap word field at the end - cntl field. */
9085 word = AscReadEEPWord(iop_base, (uchar)s_addr);
9086 }
9087 if (*wbuf != word) {
9088 n_error++;
9089 }
9090 }
9091 /* Read checksum; Byte swapping not needed. */
9092 if (AscReadEEPWord(iop_base, (uchar)s_addr) != sum) {
9093 n_error++;
9094 }
9095 return n_error;
9096}
9097
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009098static int AscSetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9099 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009100{
9101 int retry;
9102 int n_error;
9103
9104 retry = 0;
Hannes Reineckeae267592015-04-24 13:18:28 +02009105 while (true) {
Matthew Wilcox51219352007-10-02 21:55:22 -04009106 if ((n_error = AscSetEEPConfigOnce(iop_base, cfg_buf,
9107 bus_type)) == 0) {
9108 break;
9109 }
9110 if (++retry > ASC_EEP_MAX_RETRY) {
9111 break;
9112 }
9113 }
9114 return n_error;
9115}
9116
Hannes Reineckef33134e2015-04-24 13:18:31 +02009117static int AscInitFromEEP(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04009118{
9119 ASCEEP_CONFIG eep_config_buf;
9120 ASCEEP_CONFIG *eep_config;
9121 PortAddr iop_base;
9122 ushort chksum;
9123 ushort warn_code;
9124 ushort cfg_msw, cfg_lsw;
9125 int i;
9126 int write_eep = 0;
9127
9128 iop_base = asc_dvc->iop_base;
9129 warn_code = 0;
9130 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0x00FE);
9131 AscStopQueueExe(iop_base);
Hannes Reineckeae267592015-04-24 13:18:28 +02009132 if ((AscStopChip(iop_base)) ||
Matthew Wilcox51219352007-10-02 21:55:22 -04009133 (AscGetChipScsiCtrl(iop_base) != 0)) {
9134 asc_dvc->init_state |= ASC_INIT_RESET_SCSI_DONE;
9135 AscResetChipAndScsiBus(asc_dvc);
9136 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
9137 }
Hannes Reinecked647c782015-04-24 13:18:26 +02009138 if (!AscIsChipHalted(iop_base)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04009139 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
9140 return (warn_code);
9141 }
9142 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
9143 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
9144 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
9145 return (warn_code);
9146 }
9147 eep_config = (ASCEEP_CONFIG *)&eep_config_buf;
9148 cfg_msw = AscGetChipCfgMsw(iop_base);
9149 cfg_lsw = AscGetChipCfgLsw(iop_base);
9150 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9151 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9152 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9153 AscSetChipCfgMsw(iop_base, cfg_msw);
9154 }
9155 chksum = AscGetEEPConfig(iop_base, eep_config, asc_dvc->bus_type);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009156 ASC_DBG(1, "chksum 0x%x\n", chksum);
Matthew Wilcox51219352007-10-02 21:55:22 -04009157 if (chksum == 0) {
9158 chksum = 0xaa55;
9159 }
9160 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9161 warn_code |= ASC_WARN_AUTO_CONFIG;
9162 if (asc_dvc->cfg->chip_version == 3) {
9163 if (eep_config->cfg_lsw != cfg_lsw) {
9164 warn_code |= ASC_WARN_EEPROM_RECOVER;
9165 eep_config->cfg_lsw =
9166 AscGetChipCfgLsw(iop_base);
9167 }
9168 if (eep_config->cfg_msw != cfg_msw) {
9169 warn_code |= ASC_WARN_EEPROM_RECOVER;
9170 eep_config->cfg_msw =
9171 AscGetChipCfgMsw(iop_base);
9172 }
9173 }
9174 }
9175 eep_config->cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9176 eep_config->cfg_lsw |= ASC_CFG0_HOST_INT_ON;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009177 ASC_DBG(1, "eep_config->chksum 0x%x\n", eep_config->chksum);
Matthew Wilcox51219352007-10-02 21:55:22 -04009178 if (chksum != eep_config->chksum) {
9179 if (AscGetChipVersion(iop_base, asc_dvc->bus_type) ==
9180 ASC_CHIP_VER_PCI_ULTRA_3050) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009181 ASC_DBG(1, "chksum error ignored; EEPROM-less board\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009182 eep_config->init_sdtr = 0xFF;
9183 eep_config->disc_enable = 0xFF;
9184 eep_config->start_motor = 0xFF;
9185 eep_config->use_cmd_qng = 0;
9186 eep_config->max_total_qng = 0xF0;
9187 eep_config->max_tag_qng = 0x20;
9188 eep_config->cntl = 0xBFFF;
9189 ASC_EEP_SET_CHIP_ID(eep_config, 7);
9190 eep_config->no_scam = 0;
9191 eep_config->adapter_info[0] = 0;
9192 eep_config->adapter_info[1] = 0;
9193 eep_config->adapter_info[2] = 0;
9194 eep_config->adapter_info[3] = 0;
9195 eep_config->adapter_info[4] = 0;
9196 /* Indicate EEPROM-less board. */
9197 eep_config->adapter_info[5] = 0xBB;
9198 } else {
9199 ASC_PRINT
9200 ("AscInitFromEEP: EEPROM checksum error; Will try to re-write EEPROM.\n");
9201 write_eep = 1;
9202 warn_code |= ASC_WARN_EEPROM_CHKSUM;
9203 }
9204 }
9205 asc_dvc->cfg->sdtr_enable = eep_config->init_sdtr;
9206 asc_dvc->cfg->disc_enable = eep_config->disc_enable;
9207 asc_dvc->cfg->cmd_qng_enabled = eep_config->use_cmd_qng;
9208 asc_dvc->cfg->isa_dma_speed = ASC_EEP_GET_DMA_SPD(eep_config);
9209 asc_dvc->start_motor = eep_config->start_motor;
9210 asc_dvc->dvc_cntl = eep_config->cntl;
9211 asc_dvc->no_scam = eep_config->no_scam;
9212 asc_dvc->cfg->adapter_info[0] = eep_config->adapter_info[0];
9213 asc_dvc->cfg->adapter_info[1] = eep_config->adapter_info[1];
9214 asc_dvc->cfg->adapter_info[2] = eep_config->adapter_info[2];
9215 asc_dvc->cfg->adapter_info[3] = eep_config->adapter_info[3];
9216 asc_dvc->cfg->adapter_info[4] = eep_config->adapter_info[4];
9217 asc_dvc->cfg->adapter_info[5] = eep_config->adapter_info[5];
9218 if (!AscTestExternalLram(asc_dvc)) {
9219 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) ==
9220 ASC_IS_PCI_ULTRA)) {
9221 eep_config->max_total_qng =
9222 ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
9223 eep_config->max_tag_qng =
9224 ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG;
9225 } else {
9226 eep_config->cfg_msw |= 0x0800;
9227 cfg_msw |= 0x0800;
9228 AscSetChipCfgMsw(iop_base, cfg_msw);
9229 eep_config->max_total_qng = ASC_MAX_PCI_INRAM_TOTAL_QNG;
9230 eep_config->max_tag_qng = ASC_MAX_INRAM_TAG_QNG;
9231 }
9232 } else {
9233 }
9234 if (eep_config->max_total_qng < ASC_MIN_TOTAL_QNG) {
9235 eep_config->max_total_qng = ASC_MIN_TOTAL_QNG;
9236 }
9237 if (eep_config->max_total_qng > ASC_MAX_TOTAL_QNG) {
9238 eep_config->max_total_qng = ASC_MAX_TOTAL_QNG;
9239 }
9240 if (eep_config->max_tag_qng > eep_config->max_total_qng) {
9241 eep_config->max_tag_qng = eep_config->max_total_qng;
9242 }
9243 if (eep_config->max_tag_qng < ASC_MIN_TAG_Q_PER_DVC) {
9244 eep_config->max_tag_qng = ASC_MIN_TAG_Q_PER_DVC;
9245 }
9246 asc_dvc->max_total_qng = eep_config->max_total_qng;
9247 if ((eep_config->use_cmd_qng & eep_config->disc_enable) !=
9248 eep_config->use_cmd_qng) {
9249 eep_config->disc_enable = eep_config->use_cmd_qng;
9250 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9251 }
Matthew Wilcox51219352007-10-02 21:55:22 -04009252 ASC_EEP_SET_CHIP_ID(eep_config,
9253 ASC_EEP_GET_CHIP_ID(eep_config) & ASC_MAX_TID);
9254 asc_dvc->cfg->chip_scsi_id = ASC_EEP_GET_CHIP_ID(eep_config);
9255 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) &&
9256 !(asc_dvc->dvc_cntl & ASC_CNTL_SDTR_ENABLE_ULTRA)) {
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009257 asc_dvc->min_sdtr_index = ASC_SDTR_ULTRA_PCI_10MB_INDEX;
Matthew Wilcox51219352007-10-02 21:55:22 -04009258 }
9259
9260 for (i = 0; i <= ASC_MAX_TID; i++) {
9261 asc_dvc->dos_int13_table[i] = eep_config->dos_int13_table[i];
9262 asc_dvc->cfg->max_tag_qng[i] = eep_config->max_tag_qng;
9263 asc_dvc->cfg->sdtr_period_offset[i] =
9264 (uchar)(ASC_DEF_SDTR_OFFSET |
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009265 (asc_dvc->min_sdtr_index << 4));
Matthew Wilcox51219352007-10-02 21:55:22 -04009266 }
9267 eep_config->cfg_msw = AscGetChipCfgMsw(iop_base);
9268 if (write_eep) {
9269 if ((i = AscSetEEPConfig(iop_base, eep_config,
9270 asc_dvc->bus_type)) != 0) {
9271 ASC_PRINT1
9272 ("AscInitFromEEP: Failed to re-write EEPROM with %d errors.\n",
9273 i);
9274 } else {
9275 ASC_PRINT
9276 ("AscInitFromEEP: Successfully re-wrote EEPROM.\n");
9277 }
9278 }
9279 return (warn_code);
9280}
9281
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009282static int AscInitGetConfig(struct Scsi_Host *shost)
Matthew Wilcox51219352007-10-02 21:55:22 -04009283{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009284 struct asc_board *board = shost_priv(shost);
9285 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04009286 unsigned short warn_code = 0;
9287
9288 asc_dvc->init_state = ASC_INIT_STATE_BEG_GET_CFG;
9289 if (asc_dvc->err_code != 0)
9290 return asc_dvc->err_code;
9291
9292 if (AscFindSignature(asc_dvc->iop_base)) {
Hannes Reineckef33134e2015-04-24 13:18:31 +02009293 AscInitAscDvcVar(asc_dvc);
9294 warn_code = AscInitFromEEP(asc_dvc);
Matthew Wilcox51219352007-10-02 21:55:22 -04009295 asc_dvc->init_state |= ASC_INIT_STATE_END_GET_CFG;
9296 if (asc_dvc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
9297 asc_dvc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
9298 } else {
9299 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9300 }
9301
9302 switch (warn_code) {
9303 case 0: /* No error */
9304 break;
9305 case ASC_WARN_IO_PORT_ROTATE:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009306 shost_printk(KERN_WARNING, shost, "I/O port address "
9307 "modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009308 break;
9309 case ASC_WARN_AUTO_CONFIG:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009310 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9311 "enabled\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009312 break;
9313 case ASC_WARN_EEPROM_CHKSUM:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009314 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009315 break;
9316 case ASC_WARN_IRQ_MODIFIED:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009317 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009318 break;
9319 case ASC_WARN_CMD_QNG_CONFLICT:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009320 shost_printk(KERN_WARNING, shost, "tag queuing enabled w/o "
9321 "disconnects\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009322 break;
9323 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009324 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9325 warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -04009326 break;
9327 }
9328
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009329 if (asc_dvc->err_code != 0)
9330 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9331 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
Matthew Wilcox51219352007-10-02 21:55:22 -04009332
9333 return asc_dvc->err_code;
9334}
9335
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009336static int AscInitSetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
Matthew Wilcox51219352007-10-02 21:55:22 -04009337{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009338 struct asc_board *board = shost_priv(shost);
9339 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04009340 PortAddr iop_base = asc_dvc->iop_base;
9341 unsigned short cfg_msw;
9342 unsigned short warn_code = 0;
9343
9344 asc_dvc->init_state |= ASC_INIT_STATE_BEG_SET_CFG;
9345 if (asc_dvc->err_code != 0)
9346 return asc_dvc->err_code;
9347 if (!AscFindSignature(asc_dvc->iop_base)) {
9348 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9349 return asc_dvc->err_code;
9350 }
9351
9352 cfg_msw = AscGetChipCfgMsw(iop_base);
9353 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9354 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9355 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9356 AscSetChipCfgMsw(iop_base, cfg_msw);
9357 }
9358 if ((asc_dvc->cfg->cmd_qng_enabled & asc_dvc->cfg->disc_enable) !=
9359 asc_dvc->cfg->cmd_qng_enabled) {
9360 asc_dvc->cfg->disc_enable = asc_dvc->cfg->cmd_qng_enabled;
9361 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9362 }
9363 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9364 warn_code |= ASC_WARN_AUTO_CONFIG;
9365 }
Matthew Wilcox51219352007-10-02 21:55:22 -04009366#ifdef CONFIG_PCI
9367 if (asc_dvc->bus_type & ASC_IS_PCI) {
9368 cfg_msw &= 0xFFC0;
9369 AscSetChipCfgMsw(iop_base, cfg_msw);
9370 if ((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) {
9371 } else {
9372 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
9373 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
9374 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_IF_NOT_DWB;
9375 asc_dvc->bug_fix_cntl |=
9376 ASC_BUG_FIX_ASYN_USE_SYN;
9377 }
9378 }
9379 } else
9380#endif /* CONFIG_PCI */
9381 if (asc_dvc->bus_type == ASC_IS_ISAPNP) {
9382 if (AscGetChipVersion(iop_base, asc_dvc->bus_type)
9383 == ASC_CHIP_VER_ASYN_BUG) {
9384 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
9385 }
9386 }
9387 if (AscSetChipScsiID(iop_base, asc_dvc->cfg->chip_scsi_id) !=
9388 asc_dvc->cfg->chip_scsi_id) {
9389 asc_dvc->err_code |= ASC_IERR_SET_SCSI_ID;
9390 }
9391#ifdef CONFIG_ISA
9392 if (asc_dvc->bus_type & ASC_IS_ISA) {
9393 AscSetIsaDmaChannel(iop_base, asc_dvc->cfg->isa_dma_channel);
9394 AscSetIsaDmaSpeed(iop_base, asc_dvc->cfg->isa_dma_speed);
9395 }
9396#endif /* CONFIG_ISA */
9397
9398 asc_dvc->init_state |= ASC_INIT_STATE_END_SET_CFG;
9399
9400 switch (warn_code) {
9401 case 0: /* No error. */
9402 break;
9403 case ASC_WARN_IO_PORT_ROTATE:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009404 shost_printk(KERN_WARNING, shost, "I/O port address "
9405 "modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009406 break;
9407 case ASC_WARN_AUTO_CONFIG:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009408 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9409 "enabled\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009410 break;
9411 case ASC_WARN_EEPROM_CHKSUM:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009412 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009413 break;
9414 case ASC_WARN_IRQ_MODIFIED:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009415 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009416 break;
9417 case ASC_WARN_CMD_QNG_CONFLICT:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009418 shost_printk(KERN_WARNING, shost, "tag queuing w/o "
9419 "disconnects\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009420 break;
9421 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009422 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9423 warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -04009424 break;
9425 }
9426
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009427 if (asc_dvc->err_code != 0)
9428 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9429 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
Matthew Wilcox51219352007-10-02 21:55:22 -04009430
9431 return asc_dvc->err_code;
9432}
9433
9434/*
9435 * EEPROM Configuration.
9436 *
9437 * All drivers should use this structure to set the default EEPROM
9438 * configuration. The BIOS now uses this structure when it is built.
9439 * Additional structure information can be found in a_condor.h where
9440 * the structure is defined.
9441 *
9442 * The *_Field_IsChar structs are needed to correct for endianness.
9443 * These values are read from the board 16 bits at a time directly
9444 * into the structs. Because some fields are char, the values will be
9445 * in the wrong order. The *_Field_IsChar tells when to flip the
9446 * bytes. Data read and written to PCI memory is automatically swapped
9447 * on big-endian platforms so char fields read as words are actually being
9448 * unswapped on big-endian platforms.
9449 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009450static ADVEEP_3550_CONFIG Default_3550_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009451 ADV_EEPROM_BIOS_ENABLE, /* cfg_lsw */
9452 0x0000, /* cfg_msw */
9453 0xFFFF, /* disc_enable */
9454 0xFFFF, /* wdtr_able */
9455 0xFFFF, /* sdtr_able */
9456 0xFFFF, /* start_motor */
9457 0xFFFF, /* tagqng_able */
9458 0xFFFF, /* bios_scan */
9459 0, /* scam_tolerant */
9460 7, /* adapter_scsi_id */
9461 0, /* bios_boot_delay */
9462 3, /* scsi_reset_delay */
9463 0, /* bios_id_lun */
9464 0, /* termination */
9465 0, /* reserved1 */
9466 0xFFE7, /* bios_ctrl */
9467 0xFFFF, /* ultra_able */
9468 0, /* reserved2 */
9469 ASC_DEF_MAX_HOST_QNG, /* max_host_qng */
9470 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9471 0, /* dvc_cntl */
9472 0, /* bug_fix */
9473 0, /* serial_number_word1 */
9474 0, /* serial_number_word2 */
9475 0, /* serial_number_word3 */
9476 0, /* check_sum */
9477 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9478 , /* oem_name[16] */
9479 0, /* dvc_err_code */
9480 0, /* adv_err_code */
9481 0, /* adv_err_addr */
9482 0, /* saved_dvc_err_code */
9483 0, /* saved_adv_err_code */
9484 0, /* saved_adv_err_addr */
9485 0 /* num_of_err */
9486};
9487
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009488static ADVEEP_3550_CONFIG ADVEEP_3550_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009489 0, /* cfg_lsw */
9490 0, /* cfg_msw */
9491 0, /* -disc_enable */
9492 0, /* wdtr_able */
9493 0, /* sdtr_able */
9494 0, /* start_motor */
9495 0, /* tagqng_able */
9496 0, /* bios_scan */
9497 0, /* scam_tolerant */
9498 1, /* adapter_scsi_id */
9499 1, /* bios_boot_delay */
9500 1, /* scsi_reset_delay */
9501 1, /* bios_id_lun */
9502 1, /* termination */
9503 1, /* reserved1 */
9504 0, /* bios_ctrl */
9505 0, /* ultra_able */
9506 0, /* reserved2 */
9507 1, /* max_host_qng */
9508 1, /* max_dvc_qng */
9509 0, /* dvc_cntl */
9510 0, /* bug_fix */
9511 0, /* serial_number_word1 */
9512 0, /* serial_number_word2 */
9513 0, /* serial_number_word3 */
9514 0, /* check_sum */
9515 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9516 , /* oem_name[16] */
9517 0, /* dvc_err_code */
9518 0, /* adv_err_code */
9519 0, /* adv_err_addr */
9520 0, /* saved_dvc_err_code */
9521 0, /* saved_adv_err_code */
9522 0, /* saved_adv_err_addr */
9523 0 /* num_of_err */
9524};
9525
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009526static ADVEEP_38C0800_CONFIG Default_38C0800_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009527 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9528 0x0000, /* 01 cfg_msw */
9529 0xFFFF, /* 02 disc_enable */
9530 0xFFFF, /* 03 wdtr_able */
9531 0x4444, /* 04 sdtr_speed1 */
9532 0xFFFF, /* 05 start_motor */
9533 0xFFFF, /* 06 tagqng_able */
9534 0xFFFF, /* 07 bios_scan */
9535 0, /* 08 scam_tolerant */
9536 7, /* 09 adapter_scsi_id */
9537 0, /* bios_boot_delay */
9538 3, /* 10 scsi_reset_delay */
9539 0, /* bios_id_lun */
9540 0, /* 11 termination_se */
9541 0, /* termination_lvd */
9542 0xFFE7, /* 12 bios_ctrl */
9543 0x4444, /* 13 sdtr_speed2 */
9544 0x4444, /* 14 sdtr_speed3 */
9545 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9546 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9547 0, /* 16 dvc_cntl */
9548 0x4444, /* 17 sdtr_speed4 */
9549 0, /* 18 serial_number_word1 */
9550 0, /* 19 serial_number_word2 */
9551 0, /* 20 serial_number_word3 */
9552 0, /* 21 check_sum */
9553 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9554 , /* 22-29 oem_name[16] */
9555 0, /* 30 dvc_err_code */
9556 0, /* 31 adv_err_code */
9557 0, /* 32 adv_err_addr */
9558 0, /* 33 saved_dvc_err_code */
9559 0, /* 34 saved_adv_err_code */
9560 0, /* 35 saved_adv_err_addr */
9561 0, /* 36 reserved */
9562 0, /* 37 reserved */
9563 0, /* 38 reserved */
9564 0, /* 39 reserved */
9565 0, /* 40 reserved */
9566 0, /* 41 reserved */
9567 0, /* 42 reserved */
9568 0, /* 43 reserved */
9569 0, /* 44 reserved */
9570 0, /* 45 reserved */
9571 0, /* 46 reserved */
9572 0, /* 47 reserved */
9573 0, /* 48 reserved */
9574 0, /* 49 reserved */
9575 0, /* 50 reserved */
9576 0, /* 51 reserved */
9577 0, /* 52 reserved */
9578 0, /* 53 reserved */
9579 0, /* 54 reserved */
9580 0, /* 55 reserved */
9581 0, /* 56 cisptr_lsw */
9582 0, /* 57 cisprt_msw */
9583 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9584 PCI_DEVICE_ID_38C0800_REV1, /* 59 subsysid */
9585 0, /* 60 reserved */
9586 0, /* 61 reserved */
9587 0, /* 62 reserved */
9588 0 /* 63 reserved */
9589};
9590
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009591static ADVEEP_38C0800_CONFIG ADVEEP_38C0800_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009592 0, /* 00 cfg_lsw */
9593 0, /* 01 cfg_msw */
9594 0, /* 02 disc_enable */
9595 0, /* 03 wdtr_able */
9596 0, /* 04 sdtr_speed1 */
9597 0, /* 05 start_motor */
9598 0, /* 06 tagqng_able */
9599 0, /* 07 bios_scan */
9600 0, /* 08 scam_tolerant */
9601 1, /* 09 adapter_scsi_id */
9602 1, /* bios_boot_delay */
9603 1, /* 10 scsi_reset_delay */
9604 1, /* bios_id_lun */
9605 1, /* 11 termination_se */
9606 1, /* termination_lvd */
9607 0, /* 12 bios_ctrl */
9608 0, /* 13 sdtr_speed2 */
9609 0, /* 14 sdtr_speed3 */
9610 1, /* 15 max_host_qng */
9611 1, /* max_dvc_qng */
9612 0, /* 16 dvc_cntl */
9613 0, /* 17 sdtr_speed4 */
9614 0, /* 18 serial_number_word1 */
9615 0, /* 19 serial_number_word2 */
9616 0, /* 20 serial_number_word3 */
9617 0, /* 21 check_sum */
9618 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9619 , /* 22-29 oem_name[16] */
9620 0, /* 30 dvc_err_code */
9621 0, /* 31 adv_err_code */
9622 0, /* 32 adv_err_addr */
9623 0, /* 33 saved_dvc_err_code */
9624 0, /* 34 saved_adv_err_code */
9625 0, /* 35 saved_adv_err_addr */
9626 0, /* 36 reserved */
9627 0, /* 37 reserved */
9628 0, /* 38 reserved */
9629 0, /* 39 reserved */
9630 0, /* 40 reserved */
9631 0, /* 41 reserved */
9632 0, /* 42 reserved */
9633 0, /* 43 reserved */
9634 0, /* 44 reserved */
9635 0, /* 45 reserved */
9636 0, /* 46 reserved */
9637 0, /* 47 reserved */
9638 0, /* 48 reserved */
9639 0, /* 49 reserved */
9640 0, /* 50 reserved */
9641 0, /* 51 reserved */
9642 0, /* 52 reserved */
9643 0, /* 53 reserved */
9644 0, /* 54 reserved */
9645 0, /* 55 reserved */
9646 0, /* 56 cisptr_lsw */
9647 0, /* 57 cisprt_msw */
9648 0, /* 58 subsysvid */
9649 0, /* 59 subsysid */
9650 0, /* 60 reserved */
9651 0, /* 61 reserved */
9652 0, /* 62 reserved */
9653 0 /* 63 reserved */
9654};
9655
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009656static ADVEEP_38C1600_CONFIG Default_38C1600_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009657 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9658 0x0000, /* 01 cfg_msw */
9659 0xFFFF, /* 02 disc_enable */
9660 0xFFFF, /* 03 wdtr_able */
9661 0x5555, /* 04 sdtr_speed1 */
9662 0xFFFF, /* 05 start_motor */
9663 0xFFFF, /* 06 tagqng_able */
9664 0xFFFF, /* 07 bios_scan */
9665 0, /* 08 scam_tolerant */
9666 7, /* 09 adapter_scsi_id */
9667 0, /* bios_boot_delay */
9668 3, /* 10 scsi_reset_delay */
9669 0, /* bios_id_lun */
9670 0, /* 11 termination_se */
9671 0, /* termination_lvd */
9672 0xFFE7, /* 12 bios_ctrl */
9673 0x5555, /* 13 sdtr_speed2 */
9674 0x5555, /* 14 sdtr_speed3 */
9675 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9676 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9677 0, /* 16 dvc_cntl */
9678 0x5555, /* 17 sdtr_speed4 */
9679 0, /* 18 serial_number_word1 */
9680 0, /* 19 serial_number_word2 */
9681 0, /* 20 serial_number_word3 */
9682 0, /* 21 check_sum */
9683 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9684 , /* 22-29 oem_name[16] */
9685 0, /* 30 dvc_err_code */
9686 0, /* 31 adv_err_code */
9687 0, /* 32 adv_err_addr */
9688 0, /* 33 saved_dvc_err_code */
9689 0, /* 34 saved_adv_err_code */
9690 0, /* 35 saved_adv_err_addr */
9691 0, /* 36 reserved */
9692 0, /* 37 reserved */
9693 0, /* 38 reserved */
9694 0, /* 39 reserved */
9695 0, /* 40 reserved */
9696 0, /* 41 reserved */
9697 0, /* 42 reserved */
9698 0, /* 43 reserved */
9699 0, /* 44 reserved */
9700 0, /* 45 reserved */
9701 0, /* 46 reserved */
9702 0, /* 47 reserved */
9703 0, /* 48 reserved */
9704 0, /* 49 reserved */
9705 0, /* 50 reserved */
9706 0, /* 51 reserved */
9707 0, /* 52 reserved */
9708 0, /* 53 reserved */
9709 0, /* 54 reserved */
9710 0, /* 55 reserved */
9711 0, /* 56 cisptr_lsw */
9712 0, /* 57 cisprt_msw */
9713 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9714 PCI_DEVICE_ID_38C1600_REV1, /* 59 subsysid */
9715 0, /* 60 reserved */
9716 0, /* 61 reserved */
9717 0, /* 62 reserved */
9718 0 /* 63 reserved */
9719};
9720
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009721static ADVEEP_38C1600_CONFIG ADVEEP_38C1600_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009722 0, /* 00 cfg_lsw */
9723 0, /* 01 cfg_msw */
9724 0, /* 02 disc_enable */
9725 0, /* 03 wdtr_able */
9726 0, /* 04 sdtr_speed1 */
9727 0, /* 05 start_motor */
9728 0, /* 06 tagqng_able */
9729 0, /* 07 bios_scan */
9730 0, /* 08 scam_tolerant */
9731 1, /* 09 adapter_scsi_id */
9732 1, /* bios_boot_delay */
9733 1, /* 10 scsi_reset_delay */
9734 1, /* bios_id_lun */
9735 1, /* 11 termination_se */
9736 1, /* termination_lvd */
9737 0, /* 12 bios_ctrl */
9738 0, /* 13 sdtr_speed2 */
9739 0, /* 14 sdtr_speed3 */
9740 1, /* 15 max_host_qng */
9741 1, /* max_dvc_qng */
9742 0, /* 16 dvc_cntl */
9743 0, /* 17 sdtr_speed4 */
9744 0, /* 18 serial_number_word1 */
9745 0, /* 19 serial_number_word2 */
9746 0, /* 20 serial_number_word3 */
9747 0, /* 21 check_sum */
9748 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9749 , /* 22-29 oem_name[16] */
9750 0, /* 30 dvc_err_code */
9751 0, /* 31 adv_err_code */
9752 0, /* 32 adv_err_addr */
9753 0, /* 33 saved_dvc_err_code */
9754 0, /* 34 saved_adv_err_code */
9755 0, /* 35 saved_adv_err_addr */
9756 0, /* 36 reserved */
9757 0, /* 37 reserved */
9758 0, /* 38 reserved */
9759 0, /* 39 reserved */
9760 0, /* 40 reserved */
9761 0, /* 41 reserved */
9762 0, /* 42 reserved */
9763 0, /* 43 reserved */
9764 0, /* 44 reserved */
9765 0, /* 45 reserved */
9766 0, /* 46 reserved */
9767 0, /* 47 reserved */
9768 0, /* 48 reserved */
9769 0, /* 49 reserved */
9770 0, /* 50 reserved */
9771 0, /* 51 reserved */
9772 0, /* 52 reserved */
9773 0, /* 53 reserved */
9774 0, /* 54 reserved */
9775 0, /* 55 reserved */
9776 0, /* 56 cisptr_lsw */
9777 0, /* 57 cisprt_msw */
9778 0, /* 58 subsysvid */
9779 0, /* 59 subsysid */
9780 0, /* 60 reserved */
9781 0, /* 61 reserved */
9782 0, /* 62 reserved */
9783 0 /* 63 reserved */
9784};
9785
9786#ifdef CONFIG_PCI
9787/*
9788 * Wait for EEPROM command to complete
9789 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009790static void AdvWaitEEPCmd(AdvPortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04009791{
9792 int eep_delay_ms;
9793
9794 for (eep_delay_ms = 0; eep_delay_ms < ADV_EEP_DELAY_MS; eep_delay_ms++) {
9795 if (AdvReadWordRegister(iop_base, IOPW_EE_CMD) &
9796 ASC_EEP_CMD_DONE) {
9797 break;
9798 }
9799 mdelay(1);
9800 }
9801 if ((AdvReadWordRegister(iop_base, IOPW_EE_CMD) & ASC_EEP_CMD_DONE) ==
9802 0)
9803 BUG();
9804}
9805
9806/*
9807 * Read the EEPROM from specified location
9808 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009809static ushort AdvReadEEPWord(AdvPortAddr iop_base, int eep_word_addr)
Matthew Wilcox51219352007-10-02 21:55:22 -04009810{
9811 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9812 ASC_EEP_CMD_READ | eep_word_addr);
9813 AdvWaitEEPCmd(iop_base);
9814 return AdvReadWordRegister(iop_base, IOPW_EE_DATA);
9815}
9816
9817/*
9818 * Write the EEPROM from 'cfg_buf'.
9819 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009820static void AdvSet3550EEPConfig(AdvPortAddr iop_base,
9821 ADVEEP_3550_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -04009822{
9823 ushort *wbuf;
9824 ushort addr, chksum;
9825 ushort *charfields;
9826
9827 wbuf = (ushort *)cfg_buf;
9828 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
9829 chksum = 0;
9830
9831 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
9832 AdvWaitEEPCmd(iop_base);
9833
9834 /*
9835 * Write EEPROM from word 0 to word 20.
9836 */
9837 for (addr = ADV_EEP_DVC_CFG_BEGIN;
9838 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
9839 ushort word;
9840
9841 if (*charfields++) {
9842 word = cpu_to_le16(*wbuf);
9843 } else {
9844 word = *wbuf;
9845 }
9846 chksum += *wbuf; /* Checksum is calculated from word values. */
9847 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9848 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9849 ASC_EEP_CMD_WRITE | addr);
9850 AdvWaitEEPCmd(iop_base);
9851 mdelay(ADV_EEP_DELAY_MS);
9852 }
9853
9854 /*
9855 * Write EEPROM checksum at word 21.
9856 */
9857 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
9858 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
9859 AdvWaitEEPCmd(iop_base);
9860 wbuf++;
9861 charfields++;
9862
9863 /*
9864 * Write EEPROM OEM name at words 22 to 29.
9865 */
9866 for (addr = ADV_EEP_DVC_CTL_BEGIN;
9867 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
9868 ushort word;
9869
9870 if (*charfields++) {
9871 word = cpu_to_le16(*wbuf);
9872 } else {
9873 word = *wbuf;
9874 }
9875 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9876 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9877 ASC_EEP_CMD_WRITE | addr);
9878 AdvWaitEEPCmd(iop_base);
9879 }
9880 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
9881 AdvWaitEEPCmd(iop_base);
9882}
9883
9884/*
9885 * Write the EEPROM from 'cfg_buf'.
9886 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009887static void AdvSet38C0800EEPConfig(AdvPortAddr iop_base,
9888 ADVEEP_38C0800_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -04009889{
9890 ushort *wbuf;
9891 ushort *charfields;
9892 ushort addr, chksum;
9893
9894 wbuf = (ushort *)cfg_buf;
9895 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
9896 chksum = 0;
9897
9898 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
9899 AdvWaitEEPCmd(iop_base);
9900
9901 /*
9902 * Write EEPROM from word 0 to word 20.
9903 */
9904 for (addr = ADV_EEP_DVC_CFG_BEGIN;
9905 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
9906 ushort word;
9907
9908 if (*charfields++) {
9909 word = cpu_to_le16(*wbuf);
9910 } else {
9911 word = *wbuf;
9912 }
9913 chksum += *wbuf; /* Checksum is calculated from word values. */
9914 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9915 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9916 ASC_EEP_CMD_WRITE | addr);
9917 AdvWaitEEPCmd(iop_base);
9918 mdelay(ADV_EEP_DELAY_MS);
9919 }
9920
9921 /*
9922 * Write EEPROM checksum at word 21.
9923 */
9924 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
9925 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
9926 AdvWaitEEPCmd(iop_base);
9927 wbuf++;
9928 charfields++;
9929
9930 /*
9931 * Write EEPROM OEM name at words 22 to 29.
9932 */
9933 for (addr = ADV_EEP_DVC_CTL_BEGIN;
9934 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
9935 ushort word;
9936
9937 if (*charfields++) {
9938 word = cpu_to_le16(*wbuf);
9939 } else {
9940 word = *wbuf;
9941 }
9942 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9943 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9944 ASC_EEP_CMD_WRITE | addr);
9945 AdvWaitEEPCmd(iop_base);
9946 }
9947 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
9948 AdvWaitEEPCmd(iop_base);
9949}
9950
9951/*
9952 * Write the EEPROM from 'cfg_buf'.
9953 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009954static void AdvSet38C1600EEPConfig(AdvPortAddr iop_base,
9955 ADVEEP_38C1600_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -04009956{
9957 ushort *wbuf;
9958 ushort *charfields;
9959 ushort addr, chksum;
9960
9961 wbuf = (ushort *)cfg_buf;
9962 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
9963 chksum = 0;
9964
9965 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
9966 AdvWaitEEPCmd(iop_base);
9967
9968 /*
9969 * Write EEPROM from word 0 to word 20.
9970 */
9971 for (addr = ADV_EEP_DVC_CFG_BEGIN;
9972 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
9973 ushort word;
9974
9975 if (*charfields++) {
9976 word = cpu_to_le16(*wbuf);
9977 } else {
9978 word = *wbuf;
9979 }
9980 chksum += *wbuf; /* Checksum is calculated from word values. */
9981 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9982 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9983 ASC_EEP_CMD_WRITE | addr);
9984 AdvWaitEEPCmd(iop_base);
9985 mdelay(ADV_EEP_DELAY_MS);
9986 }
9987
9988 /*
9989 * Write EEPROM checksum at word 21.
9990 */
9991 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
9992 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
9993 AdvWaitEEPCmd(iop_base);
9994 wbuf++;
9995 charfields++;
9996
9997 /*
9998 * Write EEPROM OEM name at words 22 to 29.
9999 */
10000 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10001 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10002 ushort word;
10003
10004 if (*charfields++) {
10005 word = cpu_to_le16(*wbuf);
10006 } else {
10007 word = *wbuf;
10008 }
10009 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10010 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10011 ASC_EEP_CMD_WRITE | addr);
10012 AdvWaitEEPCmd(iop_base);
10013 }
10014 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10015 AdvWaitEEPCmd(iop_base);
10016}
10017
10018/*
10019 * Read EEPROM configuration into the specified buffer.
10020 *
10021 * Return a checksum based on the EEPROM configuration read.
10022 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010023static ushort AdvGet3550EEPConfig(AdvPortAddr iop_base,
10024 ADVEEP_3550_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010025{
10026 ushort wval, chksum;
10027 ushort *wbuf;
10028 int eep_addr;
10029 ushort *charfields;
10030
10031 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
10032 wbuf = (ushort *)cfg_buf;
10033 chksum = 0;
10034
10035 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10036 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10037 wval = AdvReadEEPWord(iop_base, eep_addr);
10038 chksum += wval; /* Checksum is calculated from word values. */
10039 if (*charfields++) {
10040 *wbuf = le16_to_cpu(wval);
10041 } else {
10042 *wbuf = wval;
10043 }
10044 }
10045 /* Read checksum word. */
10046 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10047 wbuf++;
10048 charfields++;
10049
10050 /* Read rest of EEPROM not covered by the checksum. */
10051 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10052 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10053 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10054 if (*charfields++) {
10055 *wbuf = le16_to_cpu(*wbuf);
10056 }
10057 }
10058 return chksum;
10059}
10060
10061/*
10062 * Read EEPROM configuration into the specified buffer.
10063 *
10064 * Return a checksum based on the EEPROM configuration read.
10065 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010066static ushort AdvGet38C0800EEPConfig(AdvPortAddr iop_base,
10067 ADVEEP_38C0800_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010068{
10069 ushort wval, chksum;
10070 ushort *wbuf;
10071 int eep_addr;
10072 ushort *charfields;
10073
10074 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10075 wbuf = (ushort *)cfg_buf;
10076 chksum = 0;
10077
10078 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10079 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10080 wval = AdvReadEEPWord(iop_base, eep_addr);
10081 chksum += wval; /* Checksum is calculated from word values. */
10082 if (*charfields++) {
10083 *wbuf = le16_to_cpu(wval);
10084 } else {
10085 *wbuf = wval;
10086 }
10087 }
10088 /* Read checksum word. */
10089 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10090 wbuf++;
10091 charfields++;
10092
10093 /* Read rest of EEPROM not covered by the checksum. */
10094 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10095 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10096 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10097 if (*charfields++) {
10098 *wbuf = le16_to_cpu(*wbuf);
10099 }
10100 }
10101 return chksum;
10102}
10103
10104/*
10105 * Read EEPROM configuration into the specified buffer.
10106 *
10107 * Return a checksum based on the EEPROM configuration read.
10108 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010109static ushort AdvGet38C1600EEPConfig(AdvPortAddr iop_base,
10110 ADVEEP_38C1600_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010111{
10112 ushort wval, chksum;
10113 ushort *wbuf;
10114 int eep_addr;
10115 ushort *charfields;
10116
10117 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
10118 wbuf = (ushort *)cfg_buf;
10119 chksum = 0;
10120
10121 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10122 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10123 wval = AdvReadEEPWord(iop_base, eep_addr);
10124 chksum += wval; /* Checksum is calculated from word values. */
10125 if (*charfields++) {
10126 *wbuf = le16_to_cpu(wval);
10127 } else {
10128 *wbuf = wval;
10129 }
10130 }
10131 /* Read checksum word. */
10132 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10133 wbuf++;
10134 charfields++;
10135
10136 /* Read rest of EEPROM not covered by the checksum. */
10137 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10138 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10139 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10140 if (*charfields++) {
10141 *wbuf = le16_to_cpu(*wbuf);
10142 }
10143 }
10144 return chksum;
10145}
10146
10147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10149 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10150 * all of this is done.
10151 *
10152 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10153 *
10154 * For a non-fatal error return a warning code. If there are no warnings
10155 * then 0 is returned.
10156 *
10157 * Note: Chip is stopped on entry.
10158 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010159static int AdvInitFrom3550EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010161 AdvPortAddr iop_base;
10162 ushort warn_code;
10163 ADVEEP_3550_CONFIG eep_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010165 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010167 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010169 /*
10170 * Read the board's EEPROM configuration.
10171 *
10172 * Set default values if a bad checksum is found.
10173 */
10174 if (AdvGet3550EEPConfig(iop_base, &eep_config) != eep_config.check_sum) {
10175 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010177 /*
10178 * Set EEPROM default values.
10179 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010180 memcpy(&eep_config, &Default_3550_EEPROM_Config,
10181 sizeof(ADVEEP_3550_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010183 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010184 * Assume the 6 byte board serial number that was read from
10185 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010186 */
10187 eep_config.serial_number_word3 =
10188 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010190 eep_config.serial_number_word2 =
10191 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010193 eep_config.serial_number_word1 =
10194 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010196 AdvSet3550EEPConfig(iop_base, &eep_config);
10197 }
10198 /*
10199 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10200 * EEPROM configuration that was read.
10201 *
10202 * This is the mapping of EEPROM fields to Adv Library fields.
10203 */
10204 asc_dvc->wdtr_able = eep_config.wdtr_able;
10205 asc_dvc->sdtr_able = eep_config.sdtr_able;
10206 asc_dvc->ultra_able = eep_config.ultra_able;
10207 asc_dvc->tagqng_able = eep_config.tagqng_able;
10208 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10209 asc_dvc->max_host_qng = eep_config.max_host_qng;
10210 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10211 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10212 asc_dvc->start_motor = eep_config.start_motor;
10213 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10214 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10215 asc_dvc->no_scam = eep_config.scam_tolerant;
10216 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10217 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10218 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010220 /*
10221 * Set the host maximum queuing (max. 253, min. 16) and the per device
10222 * maximum queuing (max. 63, min. 4).
10223 */
10224 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10225 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10226 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10227 /* If the value is zero, assume it is uninitialized. */
10228 if (eep_config.max_host_qng == 0) {
10229 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10230 } else {
10231 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10232 }
10233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010235 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10236 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10237 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10238 /* If the value is zero, assume it is uninitialized. */
10239 if (eep_config.max_dvc_qng == 0) {
10240 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10241 } else {
10242 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10243 }
10244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010246 /*
10247 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10248 * set 'max_dvc_qng' to 'max_host_qng'.
10249 */
10250 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10251 eep_config.max_dvc_qng = eep_config.max_host_qng;
10252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010254 /*
10255 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10256 * values based on possibly adjusted EEPROM values.
10257 */
10258 asc_dvc->max_host_qng = eep_config.max_host_qng;
10259 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010261 /*
10262 * If the EEPROM 'termination' field is set to automatic (0), then set
10263 * the ADV_DVC_CFG 'termination' field to automatic also.
10264 *
10265 * If the termination is specified with a non-zero 'termination'
10266 * value check that a legal value is set and set the ADV_DVC_CFG
10267 * 'termination' field appropriately.
10268 */
10269 if (eep_config.termination == 0) {
10270 asc_dvc->cfg->termination = 0; /* auto termination */
10271 } else {
10272 /* Enable manual control with low off / high off. */
10273 if (eep_config.termination == 1) {
10274 asc_dvc->cfg->termination = TERM_CTL_SEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010276 /* Enable manual control with low off / high on. */
10277 } else if (eep_config.termination == 2) {
10278 asc_dvc->cfg->termination = TERM_CTL_SEL | TERM_CTL_H;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010280 /* Enable manual control with low on / high on. */
10281 } else if (eep_config.termination == 3) {
10282 asc_dvc->cfg->termination =
10283 TERM_CTL_SEL | TERM_CTL_H | TERM_CTL_L;
10284 } else {
10285 /*
10286 * The EEPROM 'termination' field contains a bad value. Use
10287 * automatic termination instead.
10288 */
10289 asc_dvc->cfg->termination = 0;
10290 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10291 }
10292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010294 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010295}
10296
10297/*
10298 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10299 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10300 * all of this is done.
10301 *
10302 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10303 *
10304 * For a non-fatal error return a warning code. If there are no warnings
10305 * then 0 is returned.
10306 *
10307 * Note: Chip is stopped on entry.
10308 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010309static int AdvInitFrom38C0800EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010311 AdvPortAddr iop_base;
10312 ushort warn_code;
10313 ADVEEP_38C0800_CONFIG eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010314 uchar tid, termination;
10315 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010317 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010318
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010319 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010321 /*
10322 * Read the board's EEPROM configuration.
10323 *
10324 * Set default values if a bad checksum is found.
10325 */
10326 if (AdvGet38C0800EEPConfig(iop_base, &eep_config) !=
10327 eep_config.check_sum) {
10328 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010330 /*
10331 * Set EEPROM default values.
10332 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010333 memcpy(&eep_config, &Default_38C0800_EEPROM_Config,
10334 sizeof(ADVEEP_38C0800_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010336 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010337 * Assume the 6 byte board serial number that was read from
10338 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010339 */
10340 eep_config.serial_number_word3 =
10341 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010342
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010343 eep_config.serial_number_word2 =
10344 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010346 eep_config.serial_number_word1 =
10347 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010348
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010349 AdvSet38C0800EEPConfig(iop_base, &eep_config);
10350 }
10351 /*
10352 * Set ADV_DVC_VAR and ADV_DVC_CFG variables from the
10353 * EEPROM configuration that was read.
10354 *
10355 * This is the mapping of EEPROM fields to Adv Library fields.
10356 */
10357 asc_dvc->wdtr_able = eep_config.wdtr_able;
10358 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10359 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10360 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10361 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10362 asc_dvc->tagqng_able = eep_config.tagqng_able;
10363 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10364 asc_dvc->max_host_qng = eep_config.max_host_qng;
10365 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10366 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10367 asc_dvc->start_motor = eep_config.start_motor;
10368 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10369 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10370 asc_dvc->no_scam = eep_config.scam_tolerant;
10371 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10372 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10373 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010375 /*
10376 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10377 * are set, then set an 'sdtr_able' bit for it.
10378 */
10379 asc_dvc->sdtr_able = 0;
10380 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
10381 if (tid == 0) {
10382 sdtr_speed = asc_dvc->sdtr_speed1;
10383 } else if (tid == 4) {
10384 sdtr_speed = asc_dvc->sdtr_speed2;
10385 } else if (tid == 8) {
10386 sdtr_speed = asc_dvc->sdtr_speed3;
10387 } else if (tid == 12) {
10388 sdtr_speed = asc_dvc->sdtr_speed4;
10389 }
10390 if (sdtr_speed & ADV_MAX_TID) {
10391 asc_dvc->sdtr_able |= (1 << tid);
10392 }
10393 sdtr_speed >>= 4;
10394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010396 /*
10397 * Set the host maximum queuing (max. 253, min. 16) and the per device
10398 * maximum queuing (max. 63, min. 4).
10399 */
10400 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10401 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10402 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10403 /* If the value is zero, assume it is uninitialized. */
10404 if (eep_config.max_host_qng == 0) {
10405 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10406 } else {
10407 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10408 }
10409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010411 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10412 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10413 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10414 /* If the value is zero, assume it is uninitialized. */
10415 if (eep_config.max_dvc_qng == 0) {
10416 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10417 } else {
10418 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10419 }
10420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010422 /*
10423 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10424 * set 'max_dvc_qng' to 'max_host_qng'.
10425 */
10426 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10427 eep_config.max_dvc_qng = eep_config.max_host_qng;
10428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010430 /*
10431 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10432 * values based on possibly adjusted EEPROM values.
10433 */
10434 asc_dvc->max_host_qng = eep_config.max_host_qng;
10435 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010437 /*
10438 * If the EEPROM 'termination' field is set to automatic (0), then set
10439 * the ADV_DVC_CFG 'termination' field to automatic also.
10440 *
10441 * If the termination is specified with a non-zero 'termination'
10442 * value check that a legal value is set and set the ADV_DVC_CFG
10443 * 'termination' field appropriately.
10444 */
10445 if (eep_config.termination_se == 0) {
10446 termination = 0; /* auto termination for SE */
10447 } else {
10448 /* Enable manual control with low off / high off. */
10449 if (eep_config.termination_se == 1) {
10450 termination = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010451
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010452 /* Enable manual control with low off / high on. */
10453 } else if (eep_config.termination_se == 2) {
10454 termination = TERM_SE_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010455
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010456 /* Enable manual control with low on / high on. */
10457 } else if (eep_config.termination_se == 3) {
10458 termination = TERM_SE;
10459 } else {
10460 /*
10461 * The EEPROM 'termination_se' field contains a bad value.
10462 * Use automatic termination instead.
10463 */
10464 termination = 0;
10465 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10466 }
10467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010469 if (eep_config.termination_lvd == 0) {
10470 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10471 } else {
10472 /* Enable manual control with low off / high off. */
10473 if (eep_config.termination_lvd == 1) {
10474 asc_dvc->cfg->termination = termination;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010475
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010476 /* Enable manual control with low off / high on. */
10477 } else if (eep_config.termination_lvd == 2) {
10478 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010480 /* Enable manual control with low on / high on. */
10481 } else if (eep_config.termination_lvd == 3) {
10482 asc_dvc->cfg->termination = termination | TERM_LVD;
10483 } else {
10484 /*
10485 * The EEPROM 'termination_lvd' field contains a bad value.
10486 * Use automatic termination instead.
10487 */
10488 asc_dvc->cfg->termination = termination;
10489 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10490 }
10491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010492
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010493 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010494}
10495
10496/*
10497 * Read the board's EEPROM configuration. Set fields in ASC_DVC_VAR and
10498 * ASC_DVC_CFG based on the EEPROM settings. The chip is stopped while
10499 * all of this is done.
10500 *
10501 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
10502 *
10503 * For a non-fatal error return a warning code. If there are no warnings
10504 * then 0 is returned.
10505 *
10506 * Note: Chip is stopped on entry.
10507 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010508static int AdvInitFrom38C1600EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010510 AdvPortAddr iop_base;
10511 ushort warn_code;
10512 ADVEEP_38C1600_CONFIG eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010513 uchar tid, termination;
10514 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010515
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010516 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010518 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010520 /*
10521 * Read the board's EEPROM configuration.
10522 *
10523 * Set default values if a bad checksum is found.
10524 */
10525 if (AdvGet38C1600EEPConfig(iop_base, &eep_config) !=
10526 eep_config.check_sum) {
Matthew Wilcox13ac2d92007-07-30 08:10:23 -060010527 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010528 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010530 /*
10531 * Set EEPROM default values.
10532 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010533 memcpy(&eep_config, &Default_38C1600_EEPROM_Config,
10534 sizeof(ADVEEP_38C1600_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010536 if (PCI_FUNC(pdev->devfn) != 0) {
10537 u8 ints;
10538 /*
10539 * Disable Bit 14 (BIOS_ENABLE) to fix SPARC Ultra 60
10540 * and old Mac system booting problem. The Expansion
10541 * ROM must be disabled in Function 1 for these systems
10542 */
10543 eep_config.cfg_lsw &= ~ADV_EEPROM_BIOS_ENABLE;
10544 /*
10545 * Clear the INTAB (bit 11) if the GPIO 0 input
10546 * indicates the Function 1 interrupt line is wired
10547 * to INTB.
10548 *
10549 * Set/Clear Bit 11 (INTAB) from the GPIO bit 0 input:
10550 * 1 - Function 1 interrupt line wired to INT A.
10551 * 0 - Function 1 interrupt line wired to INT B.
10552 *
10553 * Note: Function 0 is always wired to INTA.
10554 * Put all 5 GPIO bits in input mode and then read
10555 * their input values.
10556 */
10557 AdvWriteByteRegister(iop_base, IOPB_GPIO_CNTL, 0);
10558 ints = AdvReadByteRegister(iop_base, IOPB_GPIO_DATA);
10559 if ((ints & 0x01) == 0)
10560 eep_config.cfg_lsw &= ~ADV_EEPROM_INTAB;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010562
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010563 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010564 * Assume the 6 byte board serial number that was read from
10565 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010566 */
10567 eep_config.serial_number_word3 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010568 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010569 eep_config.serial_number_word2 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010570 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010571 eep_config.serial_number_word1 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010572 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010574 AdvSet38C1600EEPConfig(iop_base, &eep_config);
10575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010577 /*
10578 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10579 * EEPROM configuration that was read.
10580 *
10581 * This is the mapping of EEPROM fields to Adv Library fields.
10582 */
10583 asc_dvc->wdtr_able = eep_config.wdtr_able;
10584 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10585 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10586 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10587 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10588 asc_dvc->ppr_able = 0;
10589 asc_dvc->tagqng_able = eep_config.tagqng_able;
10590 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10591 asc_dvc->max_host_qng = eep_config.max_host_qng;
10592 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10593 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ASC_MAX_TID);
10594 asc_dvc->start_motor = eep_config.start_motor;
10595 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10596 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10597 asc_dvc->no_scam = eep_config.scam_tolerant;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010599 /*
10600 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10601 * are set, then set an 'sdtr_able' bit for it.
10602 */
10603 asc_dvc->sdtr_able = 0;
10604 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
10605 if (tid == 0) {
10606 sdtr_speed = asc_dvc->sdtr_speed1;
10607 } else if (tid == 4) {
10608 sdtr_speed = asc_dvc->sdtr_speed2;
10609 } else if (tid == 8) {
10610 sdtr_speed = asc_dvc->sdtr_speed3;
10611 } else if (tid == 12) {
10612 sdtr_speed = asc_dvc->sdtr_speed4;
10613 }
10614 if (sdtr_speed & ASC_MAX_TID) {
10615 asc_dvc->sdtr_able |= (1 << tid);
10616 }
10617 sdtr_speed >>= 4;
10618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010620 /*
10621 * Set the host maximum queuing (max. 253, min. 16) and the per device
10622 * maximum queuing (max. 63, min. 4).
10623 */
10624 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10625 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10626 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10627 /* If the value is zero, assume it is uninitialized. */
10628 if (eep_config.max_host_qng == 0) {
10629 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10630 } else {
10631 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10632 }
10633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010634
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010635 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10636 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10637 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10638 /* If the value is zero, assume it is uninitialized. */
10639 if (eep_config.max_dvc_qng == 0) {
10640 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10641 } else {
10642 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10643 }
10644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010646 /*
10647 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10648 * set 'max_dvc_qng' to 'max_host_qng'.
10649 */
10650 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10651 eep_config.max_dvc_qng = eep_config.max_host_qng;
10652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010653
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010654 /*
10655 * Set ASC_DVC_VAR 'max_host_qng' and ASC_DVC_VAR 'max_dvc_qng'
10656 * values based on possibly adjusted EEPROM values.
10657 */
10658 asc_dvc->max_host_qng = eep_config.max_host_qng;
10659 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010661 /*
10662 * If the EEPROM 'termination' field is set to automatic (0), then set
10663 * the ASC_DVC_CFG 'termination' field to automatic also.
10664 *
10665 * If the termination is specified with a non-zero 'termination'
10666 * value check that a legal value is set and set the ASC_DVC_CFG
10667 * 'termination' field appropriately.
10668 */
10669 if (eep_config.termination_se == 0) {
10670 termination = 0; /* auto termination for SE */
10671 } else {
10672 /* Enable manual control with low off / high off. */
10673 if (eep_config.termination_se == 1) {
10674 termination = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010675
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010676 /* Enable manual control with low off / high on. */
10677 } else if (eep_config.termination_se == 2) {
10678 termination = TERM_SE_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010679
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010680 /* Enable manual control with low on / high on. */
10681 } else if (eep_config.termination_se == 3) {
10682 termination = TERM_SE;
10683 } else {
10684 /*
10685 * The EEPROM 'termination_se' field contains a bad value.
10686 * Use automatic termination instead.
10687 */
10688 termination = 0;
10689 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10690 }
10691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010692
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010693 if (eep_config.termination_lvd == 0) {
10694 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10695 } else {
10696 /* Enable manual control with low off / high off. */
10697 if (eep_config.termination_lvd == 1) {
10698 asc_dvc->cfg->termination = termination;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010700 /* Enable manual control with low off / high on. */
10701 } else if (eep_config.termination_lvd == 2) {
10702 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010703
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010704 /* Enable manual control with low on / high on. */
10705 } else if (eep_config.termination_lvd == 3) {
10706 asc_dvc->cfg->termination = termination | TERM_LVD;
10707 } else {
10708 /*
10709 * The EEPROM 'termination_lvd' field contains a bad value.
10710 * Use automatic termination instead.
10711 */
10712 asc_dvc->cfg->termination = termination;
10713 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10714 }
10715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010716
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010717 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718}
10719
10720/*
Matthew Wilcox51219352007-10-02 21:55:22 -040010721 * Initialize the ADV_DVC_VAR structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010722 *
Matthew Wilcox51219352007-10-02 21:55:22 -040010723 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724 *
Matthew Wilcox51219352007-10-02 21:55:22 -040010725 * For a non-fatal error return a warning code. If there are no warnings
10726 * then 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010727 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010728static int AdvInitGetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010729{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010730 struct asc_board *board = shost_priv(shost);
10731 ADV_DVC_VAR *asc_dvc = &board->dvc_var.adv_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -040010732 unsigned short warn_code = 0;
10733 AdvPortAddr iop_base = asc_dvc->iop_base;
10734 u16 cmd;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010735 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010736
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010737 asc_dvc->err_code = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -040010738
10739 /*
10740 * Save the state of the PCI Configuration Command Register
10741 * "Parity Error Response Control" Bit. If the bit is clear (0),
10742 * in AdvInitAsc3550/38C0800Driver() tell the microcode to ignore
10743 * DMA parity errors.
10744 */
10745 asc_dvc->cfg->control_flag = 0;
10746 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
10747 if ((cmd & PCI_COMMAND_PARITY) == 0)
10748 asc_dvc->cfg->control_flag |= CONTROL_FLAG_IGNORE_PERR;
10749
Matthew Wilcox51219352007-10-02 21:55:22 -040010750 asc_dvc->cfg->chip_version =
10751 AdvGetChipVersion(iop_base, asc_dvc->bus_type);
10752
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010753 ASC_DBG(1, "iopb_chip_id_1: 0x%x 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -040010754 (ushort)AdvReadByteRegister(iop_base, IOPB_CHIP_ID_1),
10755 (ushort)ADV_CHIP_ID_BYTE);
10756
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010757 ASC_DBG(1, "iopw_chip_id_0: 0x%x 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -040010758 (ushort)AdvReadWordRegister(iop_base, IOPW_CHIP_ID_0),
10759 (ushort)ADV_CHIP_ID_WORD);
10760
10761 /*
10762 * Reset the chip to start and allow register writes.
10763 */
10764 if (AdvFindSignature(iop_base) == 0) {
10765 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
10766 return ADV_ERROR;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010767 } else {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010768 /*
Matthew Wilcox51219352007-10-02 21:55:22 -040010769 * The caller must set 'chip_type' to a valid setting.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010770 */
Matthew Wilcox51219352007-10-02 21:55:22 -040010771 if (asc_dvc->chip_type != ADV_CHIP_ASC3550 &&
10772 asc_dvc->chip_type != ADV_CHIP_ASC38C0800 &&
10773 asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
10774 asc_dvc->err_code |= ASC_IERR_BAD_CHIPTYPE;
10775 return ADV_ERROR;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010777
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010778 /*
Matthew Wilcox51219352007-10-02 21:55:22 -040010779 * Reset Chip.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010780 */
Matthew Wilcox51219352007-10-02 21:55:22 -040010781 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
10782 ADV_CTRL_REG_CMD_RESET);
10783 mdelay(100);
10784 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
10785 ADV_CTRL_REG_CMD_WR_IO_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010786
Matthew Wilcox51219352007-10-02 21:55:22 -040010787 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
10788 status = AdvInitFrom38C1600EEP(asc_dvc);
10789 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
10790 status = AdvInitFrom38C0800EEP(asc_dvc);
10791 } else {
10792 status = AdvInitFrom3550EEP(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010793 }
Matthew Wilcox51219352007-10-02 21:55:22 -040010794 warn_code |= status;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010796
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010797 if (warn_code != 0)
10798 shost_printk(KERN_WARNING, shost, "warning: 0x%x\n", warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -040010799
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010800 if (asc_dvc->err_code)
10801 shost_printk(KERN_ERR, shost, "error code 0x%x\n",
10802 asc_dvc->err_code);
Matthew Wilcox51219352007-10-02 21:55:22 -040010803
10804 return asc_dvc->err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805}
Matthew Wilcox51219352007-10-02 21:55:22 -040010806#endif
10807
10808static struct scsi_host_template advansys_template = {
10809 .proc_name = DRV_NAME,
10810#ifdef CONFIG_PROC_FS
Al Virob59fb6f2013-03-31 02:59:55 -040010811 .show_info = advansys_show_info,
Matthew Wilcox51219352007-10-02 21:55:22 -040010812#endif
10813 .name = DRV_NAME,
10814 .info = advansys_info,
10815 .queuecommand = advansys_queuecommand,
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +020010816 .eh_host_reset_handler = advansys_reset,
Matthew Wilcox51219352007-10-02 21:55:22 -040010817 .bios_param = advansys_biosparam,
10818 .slave_configure = advansys_slave_configure,
10819 /*
10820 * Because the driver may control an ISA adapter 'unchecked_isa_dma'
10821 * must be set. The flag will be cleared in advansys_board_found
10822 * for non-ISA adapters.
10823 */
Hannes Reineckeae267592015-04-24 13:18:28 +020010824 .unchecked_isa_dma = true,
Matthew Wilcox51219352007-10-02 21:55:22 -040010825 /*
10826 * All adapters controlled by this driver are capable of large
10827 * scatter-gather lists. According to the mid-level SCSI documentation
10828 * this obviates any performance gain provided by setting
10829 * 'use_clustering'. But empirically while CPU utilization is increased
10830 * by enabling clustering, I/O throughput increases as well.
10831 */
10832 .use_clustering = ENABLE_CLUSTERING,
Hannes Reinecke9c17c622015-04-24 13:18:21 +020010833 .use_blk_tags = 1,
Matthew Wilcox51219352007-10-02 21:55:22 -040010834};
Linus Torvalds1da177e2005-04-16 15:20:36 -070010835
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010836static int advansys_wide_init_chip(struct Scsi_Host *shost)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010837{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010838 struct asc_board *board = shost_priv(shost);
10839 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010840 size_t sgblk_pool_size;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010841 int warn_code, err_code;
10842
10843 /*
10844 * Allocate buffer carrier structures. The total size
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010845 * is about 8 KB, so allocate all at once.
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010846 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010847 adv_dvc->carrier = dma_alloc_coherent(board->dev,
10848 ADV_CARRIER_BUFSIZE, &adv_dvc->carrier_addr, GFP_KERNEL);
10849 ASC_DBG(1, "carrier 0x%p\n", adv_dvc->carrier);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010850
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010851 if (!adv_dvc->carrier)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010852 goto kmalloc_failed;
10853
10854 /*
10855 * Allocate up to 'max_host_qng' request structures for the Wide
10856 * board. The total size is about 16 KB, so allocate all at once.
10857 * If the allocation fails decrement and try again.
10858 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010859 board->adv_reqp_size = adv_dvc->max_host_qng * sizeof(adv_req_t);
10860 if (board->adv_reqp_size & 0x1f) {
10861 ASC_DBG(1, "unaligned reqp %lu bytes\n", sizeof(adv_req_t));
10862 board->adv_reqp_size = ADV_32BALIGN(board->adv_reqp_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010863 }
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010864 board->adv_reqp = dma_alloc_coherent(board->dev, board->adv_reqp_size,
10865 &board->adv_reqp_addr, GFP_KERNEL);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010866
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010867 if (!board->adv_reqp)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010868 goto kmalloc_failed;
10869
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010870 ASC_DBG(1, "reqp 0x%p, req_cnt %d, bytes %lu\n", board->adv_reqp,
10871 adv_dvc->max_host_qng, board->adv_reqp_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010872
10873 /*
10874 * Allocate up to ADV_TOT_SG_BLOCK request structures for
10875 * the Wide board. Each structure is about 136 bytes.
10876 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010877 sgblk_pool_size = sizeof(adv_sgblk_t) * ADV_TOT_SG_BLOCK;
10878 board->adv_sgblk_pool = dma_pool_create("adv_sgblk", board->dev,
10879 sgblk_pool_size, 32, 0);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010880
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010881 ASC_DBG(1, "sg_cnt %d * %lu = %lu bytes\n", ADV_TOT_SG_BLOCK,
10882 sizeof(adv_sgblk_t), sgblk_pool_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010883
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010884 if (!board->adv_sgblk_pool)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010885 goto kmalloc_failed;
10886
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010887 if (adv_dvc->chip_type == ADV_CHIP_ASC3550) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010888 ASC_DBG(2, "AdvInitAsc3550Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010889 warn_code = AdvInitAsc3550Driver(adv_dvc);
10890 } else if (adv_dvc->chip_type == ADV_CHIP_ASC38C0800) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010891 ASC_DBG(2, "AdvInitAsc38C0800Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010892 warn_code = AdvInitAsc38C0800Driver(adv_dvc);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010893 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010894 ASC_DBG(2, "AdvInitAsc38C1600Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010895 warn_code = AdvInitAsc38C1600Driver(adv_dvc);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010896 }
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010897 err_code = adv_dvc->err_code;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010898
10899 if (warn_code || err_code) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010900 shost_printk(KERN_WARNING, shost, "error: warn 0x%x, error "
10901 "0x%x\n", warn_code, err_code);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010902 }
10903
10904 goto exit;
10905
10906 kmalloc_failed:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010907 shost_printk(KERN_ERR, shost, "error: kmalloc() failed\n");
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010908 err_code = ADV_ERROR;
10909 exit:
10910 return err_code;
10911}
10912
Matthew Wilcox98d41c22007-10-02 21:55:37 -040010913static void advansys_wide_free_mem(struct asc_board *board)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010914{
Matthew Wilcox98d41c22007-10-02 21:55:37 -040010915 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010916
10917 if (adv_dvc->carrier) {
10918 dma_free_coherent(board->dev, ADV_CARRIER_BUFSIZE,
10919 adv_dvc->carrier, adv_dvc->carrier_addr);
10920 adv_dvc->carrier = NULL;
10921 }
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010922 if (board->adv_reqp) {
10923 dma_free_coherent(board->dev, board->adv_reqp_size,
10924 board->adv_reqp, board->adv_reqp_addr);
10925 board->adv_reqp = NULL;
10926 }
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010927 if (board->adv_sgblk_pool) {
10928 dma_pool_destroy(board->adv_sgblk_pool);
10929 board->adv_sgblk_pool = NULL;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010930 }
10931}
10932
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010933static int advansys_board_found(struct Scsi_Host *shost, unsigned int iop,
10934 int bus_type)
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010935{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010936 struct pci_dev *pdev;
Matthew Wilcoxd2411492007-10-02 21:55:31 -040010937 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010938 ASC_DVC_VAR *asc_dvc_varp = NULL;
10939 ADV_DVC_VAR *adv_dvc_varp = NULL;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010940 int share_irq, warn_code, ret;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010941
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010942 pdev = (bus_type == ASC_IS_PCI) ? to_pci_dev(boardp->dev) : NULL;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010943
10944 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010945 ASC_DBG(1, "narrow board\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010946 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
10947 asc_dvc_varp->bus_type = bus_type;
10948 asc_dvc_varp->drv_ptr = boardp;
10949 asc_dvc_varp->cfg = &boardp->dvc_cfg.asc_dvc_cfg;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010950 asc_dvc_varp->iop_base = iop;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010951 } else {
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010952#ifdef CONFIG_PCI
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010953 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
10954 adv_dvc_varp->drv_ptr = boardp;
10955 adv_dvc_varp->cfg = &boardp->dvc_cfg.adv_dvc_cfg;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010956 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010957 ASC_DBG(1, "wide board ASC-3550\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010958 adv_dvc_varp->chip_type = ADV_CHIP_ASC3550;
10959 } else if (pdev->device == PCI_DEVICE_ID_38C0800_REV1) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010960 ASC_DBG(1, "wide board ASC-38C0800\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010961 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C0800;
10962 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010963 ASC_DBG(1, "wide board ASC-38C1600\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010964 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C1600;
10965 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010966
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010967 boardp->asc_n_io_port = pci_resource_len(pdev, 1);
Arjan van de Ven25729a72008-09-28 16:18:02 -070010968 boardp->ioremap_addr = pci_ioremap_bar(pdev, 1);
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010969 if (!boardp->ioremap_addr) {
Matthew Wilcox9d511a42007-10-02 21:55:42 -040010970 shost_printk(KERN_ERR, shost, "ioremap(%lx, %d) "
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010971 "returned NULL\n",
Matthew Wilcox9d511a42007-10-02 21:55:42 -040010972 (long)pci_resource_start(pdev, 1),
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010973 boardp->asc_n_io_port);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010974 ret = -ENODEV;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010975 goto err_shost;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010976 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010977 adv_dvc_varp->iop_base = (AdvPortAddr)boardp->ioremap_addr;
10978 ASC_DBG(1, "iop_base: 0x%p\n", adv_dvc_varp->iop_base);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010979
10980 /*
10981 * Even though it isn't used to access wide boards, other
10982 * than for the debug line below, save I/O Port address so
10983 * that it can be reported.
10984 */
10985 boardp->ioport = iop;
10986
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010987 ASC_DBG(1, "iopb_chip_id_1 0x%x, iopw_chip_id_0 0x%x\n",
10988 (ushort)inp(iop + 1), (ushort)inpw(iop));
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010989#endif /* CONFIG_PCI */
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010990 }
10991
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010992 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010993 /*
10994 * Set the board bus type and PCI IRQ before
10995 * calling AscInitGetConfig().
10996 */
10997 switch (asc_dvc_varp->bus_type) {
10998#ifdef CONFIG_ISA
10999 case ASC_IS_ISA:
Hannes Reineckeae267592015-04-24 13:18:28 +020011000 shost->unchecked_isa_dma = true;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011001 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011002 break;
11003 case ASC_IS_VL:
Hannes Reineckeae267592015-04-24 13:18:28 +020011004 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011005 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011006 break;
11007 case ASC_IS_EISA:
Hannes Reineckeae267592015-04-24 13:18:28 +020011008 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011009 share_irq = IRQF_SHARED;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011010 break;
11011#endif /* CONFIG_ISA */
11012#ifdef CONFIG_PCI
11013 case ASC_IS_PCI:
Hannes Reineckeae267592015-04-24 13:18:28 +020011014 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011015 share_irq = IRQF_SHARED;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011016 break;
11017#endif /* CONFIG_PCI */
11018 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011019 shost_printk(KERN_ERR, shost, "unknown adapter type: "
11020 "%d\n", asc_dvc_varp->bus_type);
Hannes Reineckeae267592015-04-24 13:18:28 +020011021 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011022 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011023 break;
11024 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011025
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011026 /*
11027 * NOTE: AscInitGetConfig() may change the board's
11028 * bus_type value. The bus_type value should no
11029 * longer be used. If the bus_type field must be
11030 * referenced only use the bit-wise AND operator "&".
11031 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011032 ASC_DBG(2, "AscInitGetConfig()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011033 ret = AscInitGetConfig(shost) ? -ENODEV : 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011034 } else {
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011035#ifdef CONFIG_PCI
11036 /*
11037 * For Wide boards set PCI information before calling
11038 * AdvInitGetConfig().
11039 */
Hannes Reineckeae267592015-04-24 13:18:28 +020011040 shost->unchecked_isa_dma = false;
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011041 share_irq = IRQF_SHARED;
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011042 ASC_DBG(2, "AdvInitGetConfig()\n");
Matthew Wilcox394dbf32007-07-26 11:56:40 -040011043
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011044 ret = AdvInitGetConfig(pdev, shost) ? -ENODEV : 0;
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011045#endif /* CONFIG_PCI */
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011046 }
11047
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011048 if (ret)
Al Virob59fb6f2013-03-31 02:59:55 -040011049 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011050
11051 /*
11052 * Save the EEPROM configuration so that it can be displayed
11053 * from /proc/scsi/advansys/[0...].
11054 */
11055 if (ASC_NARROW_BOARD(boardp)) {
11056
11057 ASCEEP_CONFIG *ep;
11058
11059 /*
11060 * Set the adapter's target id bit in the 'init_tidmask' field.
11061 */
11062 boardp->init_tidmask |=
11063 ADV_TID_TO_TIDMASK(asc_dvc_varp->cfg->chip_scsi_id);
11064
11065 /*
11066 * Save EEPROM settings for the board.
11067 */
11068 ep = &boardp->eep_config.asc_eep;
11069
11070 ep->init_sdtr = asc_dvc_varp->cfg->sdtr_enable;
11071 ep->disc_enable = asc_dvc_varp->cfg->disc_enable;
11072 ep->use_cmd_qng = asc_dvc_varp->cfg->cmd_qng_enabled;
11073 ASC_EEP_SET_DMA_SPD(ep, asc_dvc_varp->cfg->isa_dma_speed);
11074 ep->start_motor = asc_dvc_varp->start_motor;
11075 ep->cntl = asc_dvc_varp->dvc_cntl;
11076 ep->no_scam = asc_dvc_varp->no_scam;
11077 ep->max_total_qng = asc_dvc_varp->max_total_qng;
11078 ASC_EEP_SET_CHIP_ID(ep, asc_dvc_varp->cfg->chip_scsi_id);
11079 /* 'max_tag_qng' is set to the same value for every device. */
11080 ep->max_tag_qng = asc_dvc_varp->cfg->max_tag_qng[0];
11081 ep->adapter_info[0] = asc_dvc_varp->cfg->adapter_info[0];
11082 ep->adapter_info[1] = asc_dvc_varp->cfg->adapter_info[1];
11083 ep->adapter_info[2] = asc_dvc_varp->cfg->adapter_info[2];
11084 ep->adapter_info[3] = asc_dvc_varp->cfg->adapter_info[3];
11085 ep->adapter_info[4] = asc_dvc_varp->cfg->adapter_info[4];
11086 ep->adapter_info[5] = asc_dvc_varp->cfg->adapter_info[5];
11087
11088 /*
11089 * Modify board configuration.
11090 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011091 ASC_DBG(2, "AscInitSetConfig()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011092 ret = AscInitSetConfig(pdev, shost) ? -ENODEV : 0;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011093 if (ret)
Al Virob59fb6f2013-03-31 02:59:55 -040011094 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011095 } else {
11096 ADVEEP_3550_CONFIG *ep_3550;
11097 ADVEEP_38C0800_CONFIG *ep_38C0800;
11098 ADVEEP_38C1600_CONFIG *ep_38C1600;
11099
11100 /*
11101 * Save Wide EEP Configuration Information.
11102 */
11103 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
11104 ep_3550 = &boardp->eep_config.adv_3550_eep;
11105
11106 ep_3550->adapter_scsi_id = adv_dvc_varp->chip_scsi_id;
11107 ep_3550->max_host_qng = adv_dvc_varp->max_host_qng;
11108 ep_3550->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11109 ep_3550->termination = adv_dvc_varp->cfg->termination;
11110 ep_3550->disc_enable = adv_dvc_varp->cfg->disc_enable;
11111 ep_3550->bios_ctrl = adv_dvc_varp->bios_ctrl;
11112 ep_3550->wdtr_able = adv_dvc_varp->wdtr_able;
11113 ep_3550->sdtr_able = adv_dvc_varp->sdtr_able;
11114 ep_3550->ultra_able = adv_dvc_varp->ultra_able;
11115 ep_3550->tagqng_able = adv_dvc_varp->tagqng_able;
11116 ep_3550->start_motor = adv_dvc_varp->start_motor;
11117 ep_3550->scsi_reset_delay =
11118 adv_dvc_varp->scsi_reset_wait;
11119 ep_3550->serial_number_word1 =
11120 adv_dvc_varp->cfg->serial1;
11121 ep_3550->serial_number_word2 =
11122 adv_dvc_varp->cfg->serial2;
11123 ep_3550->serial_number_word3 =
11124 adv_dvc_varp->cfg->serial3;
11125 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
11126 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
11127
11128 ep_38C0800->adapter_scsi_id =
11129 adv_dvc_varp->chip_scsi_id;
11130 ep_38C0800->max_host_qng = adv_dvc_varp->max_host_qng;
11131 ep_38C0800->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11132 ep_38C0800->termination_lvd =
11133 adv_dvc_varp->cfg->termination;
11134 ep_38C0800->disc_enable =
11135 adv_dvc_varp->cfg->disc_enable;
11136 ep_38C0800->bios_ctrl = adv_dvc_varp->bios_ctrl;
11137 ep_38C0800->wdtr_able = adv_dvc_varp->wdtr_able;
11138 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11139 ep_38C0800->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11140 ep_38C0800->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11141 ep_38C0800->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11142 ep_38C0800->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11143 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11144 ep_38C0800->start_motor = adv_dvc_varp->start_motor;
11145 ep_38C0800->scsi_reset_delay =
11146 adv_dvc_varp->scsi_reset_wait;
11147 ep_38C0800->serial_number_word1 =
11148 adv_dvc_varp->cfg->serial1;
11149 ep_38C0800->serial_number_word2 =
11150 adv_dvc_varp->cfg->serial2;
11151 ep_38C0800->serial_number_word3 =
11152 adv_dvc_varp->cfg->serial3;
11153 } else {
11154 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
11155
11156 ep_38C1600->adapter_scsi_id =
11157 adv_dvc_varp->chip_scsi_id;
11158 ep_38C1600->max_host_qng = adv_dvc_varp->max_host_qng;
11159 ep_38C1600->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11160 ep_38C1600->termination_lvd =
11161 adv_dvc_varp->cfg->termination;
11162 ep_38C1600->disc_enable =
11163 adv_dvc_varp->cfg->disc_enable;
11164 ep_38C1600->bios_ctrl = adv_dvc_varp->bios_ctrl;
11165 ep_38C1600->wdtr_able = adv_dvc_varp->wdtr_able;
11166 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11167 ep_38C1600->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11168 ep_38C1600->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11169 ep_38C1600->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11170 ep_38C1600->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11171 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11172 ep_38C1600->start_motor = adv_dvc_varp->start_motor;
11173 ep_38C1600->scsi_reset_delay =
11174 adv_dvc_varp->scsi_reset_wait;
11175 ep_38C1600->serial_number_word1 =
11176 adv_dvc_varp->cfg->serial1;
11177 ep_38C1600->serial_number_word2 =
11178 adv_dvc_varp->cfg->serial2;
11179 ep_38C1600->serial_number_word3 =
11180 adv_dvc_varp->cfg->serial3;
11181 }
11182
11183 /*
11184 * Set the adapter's target id bit in the 'init_tidmask' field.
11185 */
11186 boardp->init_tidmask |=
11187 ADV_TID_TO_TIDMASK(adv_dvc_varp->chip_scsi_id);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011188 }
11189
11190 /*
11191 * Channels are numbered beginning with 0. For AdvanSys one host
11192 * structure supports one channel. Multi-channel boards have a
11193 * separate host structure for each channel.
11194 */
11195 shost->max_channel = 0;
11196 if (ASC_NARROW_BOARD(boardp)) {
11197 shost->max_id = ASC_MAX_TID + 1;
11198 shost->max_lun = ASC_MAX_LUN + 1;
Matthew Wilcoxf05ec592007-09-09 08:56:36 -060011199 shost->max_cmd_len = ASC_MAX_CDB_LEN;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011200
11201 shost->io_port = asc_dvc_varp->iop_base;
11202 boardp->asc_n_io_port = ASC_IOADR_GAP;
11203 shost->this_id = asc_dvc_varp->cfg->chip_scsi_id;
11204
11205 /* Set maximum number of queues the adapter can handle. */
11206 shost->can_queue = asc_dvc_varp->max_total_qng;
11207 } else {
11208 shost->max_id = ADV_MAX_TID + 1;
11209 shost->max_lun = ADV_MAX_LUN + 1;
Matthew Wilcoxf05ec592007-09-09 08:56:36 -060011210 shost->max_cmd_len = ADV_MAX_CDB_LEN;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011211
11212 /*
11213 * Save the I/O Port address and length even though
11214 * I/O ports are not used to access Wide boards.
11215 * Instead the Wide boards are accessed with
11216 * PCI Memory Mapped I/O.
11217 */
11218 shost->io_port = iop;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011219
11220 shost->this_id = adv_dvc_varp->chip_scsi_id;
11221
11222 /* Set maximum number of queues the adapter can handle. */
11223 shost->can_queue = adv_dvc_varp->max_host_qng;
11224 }
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011225 ret = scsi_init_shared_tag_map(shost, shost->can_queue);
11226 if (ret) {
11227 shost_printk(KERN_ERR, shost, "init tag map failed\n");
11228 goto err_free_dma;
11229 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011230
11231 /*
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011232 * Set the maximum number of scatter-gather elements the
11233 * adapter can handle.
11234 */
11235 if (ASC_NARROW_BOARD(boardp)) {
11236 /*
11237 * Allow two commands with 'sg_tablesize' scatter-gather
11238 * elements to be executed simultaneously. This value is
11239 * the theoretical hardware limit. It may be decreased
11240 * below.
11241 */
11242 shost->sg_tablesize =
11243 (((asc_dvc_varp->max_total_qng - 2) / 2) *
11244 ASC_SG_LIST_PER_Q) + 1;
11245 } else {
11246 shost->sg_tablesize = ADV_MAX_SG_LIST;
11247 }
11248
11249 /*
11250 * The value of 'sg_tablesize' can not exceed the SCSI
11251 * mid-level driver definition of SG_ALL. SG_ALL also
11252 * must not be exceeded, because it is used to define the
11253 * size of the scatter-gather table in 'struct asc_sg_head'.
11254 */
11255 if (shost->sg_tablesize > SG_ALL) {
11256 shost->sg_tablesize = SG_ALL;
11257 }
11258
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011259 ASC_DBG(1, "sg_tablesize: %d\n", shost->sg_tablesize);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011260
11261 /* BIOS start address. */
11262 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011263 shost->base = AscGetChipBiosAddress(asc_dvc_varp->iop_base,
11264 asc_dvc_varp->bus_type);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011265 } else {
11266 /*
11267 * Fill-in BIOS board variables. The Wide BIOS saves
11268 * information in LRAM that is used by the driver.
11269 */
11270 AdvReadWordLram(adv_dvc_varp->iop_base,
11271 BIOS_SIGNATURE, boardp->bios_signature);
11272 AdvReadWordLram(adv_dvc_varp->iop_base,
11273 BIOS_VERSION, boardp->bios_version);
11274 AdvReadWordLram(adv_dvc_varp->iop_base,
11275 BIOS_CODESEG, boardp->bios_codeseg);
11276 AdvReadWordLram(adv_dvc_varp->iop_base,
11277 BIOS_CODELEN, boardp->bios_codelen);
11278
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011279 ASC_DBG(1, "bios_signature 0x%x, bios_version 0x%x\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011280 boardp->bios_signature, boardp->bios_version);
11281
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011282 ASC_DBG(1, "bios_codeseg 0x%x, bios_codelen 0x%x\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011283 boardp->bios_codeseg, boardp->bios_codelen);
11284
11285 /*
11286 * If the BIOS saved a valid signature, then fill in
11287 * the BIOS code segment base address.
11288 */
11289 if (boardp->bios_signature == 0x55AA) {
11290 /*
11291 * Convert x86 realmode code segment to a linear
11292 * address by shifting left 4.
11293 */
11294 shost->base = ((ulong)boardp->bios_codeseg << 4);
11295 } else {
11296 shost->base = 0;
11297 }
11298 }
11299
11300 /*
11301 * Register Board Resources - I/O Port, DMA, IRQ
11302 */
11303
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011304 /* Register DMA Channel for Narrow boards. */
11305 shost->dma_channel = NO_ISA_DMA; /* Default to no ISA DMA. */
11306#ifdef CONFIG_ISA
11307 if (ASC_NARROW_BOARD(boardp)) {
11308 /* Register DMA channel for ISA bus. */
11309 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
11310 shost->dma_channel = asc_dvc_varp->cfg->isa_dma_channel;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011311 ret = request_dma(shost->dma_channel, DRV_NAME);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011312 if (ret) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011313 shost_printk(KERN_ERR, shost, "request_dma() "
11314 "%d failed %d\n",
11315 shost->dma_channel, ret);
Al Virob59fb6f2013-03-31 02:59:55 -040011316 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011317 }
11318 AscEnableIsaDma(shost->dma_channel);
11319 }
11320 }
11321#endif /* CONFIG_ISA */
11322
11323 /* Register IRQ Number. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011324 ASC_DBG(2, "request_irq(%d, %p)\n", boardp->irq, shost);
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011325
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011326 ret = request_irq(boardp->irq, advansys_interrupt, share_irq,
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011327 DRV_NAME, shost);
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011328
11329 if (ret) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011330 if (ret == -EBUSY) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011331 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11332 "already in use\n", boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011333 } else if (ret == -EINVAL) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011334 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11335 "not valid\n", boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011336 } else {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011337 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11338 "failed with %d\n", boardp->irq, ret);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011339 }
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011340 goto err_free_dma;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011341 }
11342
11343 /*
11344 * Initialize board RISC chip and enable interrupts.
11345 */
11346 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011347 ASC_DBG(2, "AscInitAsc1000Driver()\n");
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011348
11349 asc_dvc_varp->overrun_buf = kzalloc(ASC_OVERRUN_BSIZE, GFP_KERNEL);
11350 if (!asc_dvc_varp->overrun_buf) {
11351 ret = -ENOMEM;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011352 goto err_free_irq;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011353 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011354 warn_code = AscInitAsc1000Driver(asc_dvc_varp);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011355
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011356 if (warn_code || asc_dvc_varp->err_code) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011357 shost_printk(KERN_ERR, shost, "error: init_state 0x%x, "
11358 "warn 0x%x, error 0x%x\n",
11359 asc_dvc_varp->init_state, warn_code,
11360 asc_dvc_varp->err_code);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011361 if (!asc_dvc_varp->overrun_dma) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011362 ret = -ENODEV;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011363 goto err_free_mem;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011364 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011365 }
11366 } else {
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011367 if (advansys_wide_init_chip(shost)) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011368 ret = -ENODEV;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011369 goto err_free_mem;
11370 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011371 }
11372
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011373 ASC_DBG_PRT_SCSI_HOST(2, shost);
11374
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011375 ret = scsi_add_host(shost, boardp->dev);
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011376 if (ret)
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011377 goto err_free_mem;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011378
11379 scsi_scan_host(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011380 return 0;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011381
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011382 err_free_mem:
11383 if (ASC_NARROW_BOARD(boardp)) {
11384 if (asc_dvc_varp->overrun_dma)
11385 dma_unmap_single(boardp->dev, asc_dvc_varp->overrun_dma,
11386 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
11387 kfree(asc_dvc_varp->overrun_buf);
11388 } else
11389 advansys_wide_free_mem(boardp);
11390 err_free_irq:
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011391 free_irq(boardp->irq, shost);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011392 err_free_dma:
Al Viro30037812008-11-22 17:34:54 +000011393#ifdef CONFIG_ISA
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011394 if (shost->dma_channel != NO_ISA_DMA)
11395 free_dma(shost->dma_channel);
Al Viro30037812008-11-22 17:34:54 +000011396#endif
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011397 err_unmap:
11398 if (boardp->ioremap_addr)
11399 iounmap(boardp->ioremap_addr);
11400 err_shost:
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011401 return ret;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011402}
11403
11404/*
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011405 * advansys_release()
11406 *
11407 * Release resources allocated for a single AdvanSys adapter.
11408 */
11409static int advansys_release(struct Scsi_Host *shost)
11410{
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011411 struct asc_board *board = shost_priv(shost);
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011412 ASC_DBG(1, "begin\n");
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011413 scsi_remove_host(shost);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011414 free_irq(board->irq, shost);
Al Viro30037812008-11-22 17:34:54 +000011415#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011416 if (shost->dma_channel != NO_ISA_DMA) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011417 ASC_DBG(1, "free_dma()\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011418 free_dma(shost->dma_channel);
11419 }
Al Viro30037812008-11-22 17:34:54 +000011420#endif
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011421 if (ASC_NARROW_BOARD(board)) {
11422 dma_unmap_single(board->dev,
11423 board->dvc_var.asc_dvc_var.overrun_dma,
11424 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011425 kfree(board->dvc_var.asc_dvc_var.overrun_buf);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011426 } else {
11427 iounmap(board->ioremap_addr);
11428 advansys_wide_free_mem(board);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011429 }
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011430 scsi_host_put(shost);
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011431 ASC_DBG(1, "end\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011432 return 0;
11433}
11434
Matthew Wilcox95c9f162007-09-09 08:56:39 -060011435#define ASC_IOADR_TABLE_MAX_IX 11
11436
Randy Dunlap747d0162008-01-14 00:55:18 -080011437static PortAddr _asc_def_iop_base[ASC_IOADR_TABLE_MAX_IX] = {
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011438 0x100, 0x0110, 0x120, 0x0130, 0x140, 0x0150, 0x0190,
11439 0x0210, 0x0230, 0x0250, 0x0330
11440};
11441
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011442/*
11443 * The ISA IRQ number is found in bits 2 and 3 of the CfgLsw. It decodes as:
11444 * 00: 10
11445 * 01: 11
11446 * 10: 12
11447 * 11: 15
11448 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011449static unsigned int advansys_isa_irq_no(PortAddr iop_base)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011450{
11451 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11452 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x03) + 10;
11453 if (chip_irq == 13)
11454 chip_irq = 15;
11455 return chip_irq;
11456}
11457
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011458static int advansys_isa_probe(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011459{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011460 int err = -ENODEV;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011461 PortAddr iop_base = _asc_def_iop_base[id];
11462 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011463 struct asc_board *board;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011464
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011465 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011466 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011467 return -ENODEV;
11468 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011469 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011470 if (!AscFindSignature(iop_base))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011471 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011472 if (!(AscGetChipVersion(iop_base, ASC_IS_ISA) & ASC_CHIP_VER_ISA_BIT))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011473 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011474
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011475 err = -ENOMEM;
11476 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011477 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011478 goto release_region;
11479
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011480 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011481 board->irq = advansys_isa_irq_no(iop_base);
11482 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011483 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011484
11485 err = advansys_board_found(shost, iop_base, ASC_IS_ISA);
11486 if (err)
11487 goto free_host;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011488
11489 dev_set_drvdata(dev, shost);
11490 return 0;
11491
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011492 free_host:
11493 scsi_host_put(shost);
11494 release_region:
Matthew Wilcox71f36112007-07-30 08:04:53 -060011495 release_region(iop_base, ASC_IOADR_GAP);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011496 return err;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011497}
11498
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011499static int advansys_isa_remove(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011500{
Matthew Wilcox71f36112007-07-30 08:04:53 -060011501 int ioport = _asc_def_iop_base[id];
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011502 advansys_release(dev_get_drvdata(dev));
Matthew Wilcox71f36112007-07-30 08:04:53 -060011503 release_region(ioport, ASC_IOADR_GAP);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011504 return 0;
11505}
11506
11507static struct isa_driver advansys_isa_driver = {
11508 .probe = advansys_isa_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011509 .remove = advansys_isa_remove,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011510 .driver = {
11511 .owner = THIS_MODULE,
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011512 .name = DRV_NAME,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011513 },
11514};
11515
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011516/*
11517 * The VLB IRQ number is found in bits 2 to 4 of the CfgLsw. It decodes as:
11518 * 000: invalid
11519 * 001: 10
11520 * 010: 11
11521 * 011: 12
11522 * 100: invalid
11523 * 101: 14
11524 * 110: 15
11525 * 111: invalid
11526 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011527static unsigned int advansys_vlb_irq_no(PortAddr iop_base)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011528{
11529 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11530 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x07) + 9;
11531 if ((chip_irq < 10) || (chip_irq == 13) || (chip_irq > 15))
11532 return 0;
11533 return chip_irq;
11534}
11535
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011536static int advansys_vlb_probe(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011537{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011538 int err = -ENODEV;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011539 PortAddr iop_base = _asc_def_iop_base[id];
11540 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011541 struct asc_board *board;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011542
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011543 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011544 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011545 return -ENODEV;
11546 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011547 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011548 if (!AscFindSignature(iop_base))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011549 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011550 /*
11551 * I don't think this condition can actually happen, but the old
11552 * driver did it, and the chances of finding a VLB setup in 2007
11553 * to do testing with is slight to none.
11554 */
11555 if (AscGetChipVersion(iop_base, ASC_IS_VL) > ASC_CHIP_MAX_VER_VL)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011556 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011557
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011558 err = -ENOMEM;
11559 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011560 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011561 goto release_region;
11562
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011563 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011564 board->irq = advansys_vlb_irq_no(iop_base);
11565 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011566 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011567
11568 err = advansys_board_found(shost, iop_base, ASC_IS_VL);
11569 if (err)
11570 goto free_host;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011571
11572 dev_set_drvdata(dev, shost);
11573 return 0;
11574
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011575 free_host:
11576 scsi_host_put(shost);
11577 release_region:
Matthew Wilcox71f36112007-07-30 08:04:53 -060011578 release_region(iop_base, ASC_IOADR_GAP);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011579 return -ENODEV;
11580}
11581
11582static struct isa_driver advansys_vlb_driver = {
11583 .probe = advansys_vlb_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011584 .remove = advansys_isa_remove,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011585 .driver = {
11586 .owner = THIS_MODULE,
Matthew Wilcoxb8e5152b2007-09-09 08:56:26 -060011587 .name = "advansys_vlb",
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011588 },
11589};
11590
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011591static struct eisa_device_id advansys_eisa_table[] = {
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011592 { "ABP7401" },
11593 { "ABP7501" },
11594 { "" }
11595};
11596
11597MODULE_DEVICE_TABLE(eisa, advansys_eisa_table);
11598
11599/*
11600 * EISA is a little more tricky than PCI; each EISA device may have two
11601 * channels, and this driver is written to make each channel its own Scsi_Host
11602 */
11603struct eisa_scsi_data {
11604 struct Scsi_Host *host[2];
11605};
11606
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011607/*
11608 * The EISA IRQ number is found in bits 8 to 10 of the CfgLsw. It decodes as:
11609 * 000: 10
11610 * 001: 11
11611 * 010: 12
11612 * 011: invalid
11613 * 100: 14
11614 * 101: 15
11615 * 110: invalid
11616 * 111: invalid
11617 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011618static unsigned int advansys_eisa_irq_no(struct eisa_device *edev)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011619{
11620 unsigned short cfg_lsw = inw(edev->base_addr + 0xc86);
11621 unsigned int chip_irq = ((cfg_lsw >> 8) & 0x07) + 10;
11622 if ((chip_irq == 13) || (chip_irq > 15))
11623 return 0;
11624 return chip_irq;
11625}
11626
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011627static int advansys_eisa_probe(struct device *dev)
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011628{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011629 int i, ioport, irq = 0;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011630 int err;
11631 struct eisa_device *edev = to_eisa_device(dev);
11632 struct eisa_scsi_data *data;
11633
11634 err = -ENOMEM;
11635 data = kzalloc(sizeof(*data), GFP_KERNEL);
11636 if (!data)
11637 goto fail;
11638 ioport = edev->base_addr + 0xc30;
11639
11640 err = -ENODEV;
11641 for (i = 0; i < 2; i++, ioport += 0x20) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011642 struct asc_board *board;
11643 struct Scsi_Host *shost;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011644 if (!request_region(ioport, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcox71f36112007-07-30 08:04:53 -060011645 printk(KERN_WARNING "Region %x-%x busy\n", ioport,
11646 ioport + ASC_IOADR_GAP - 1);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011647 continue;
Matthew Wilcox71f36112007-07-30 08:04:53 -060011648 }
11649 if (!AscFindSignature(ioport)) {
11650 release_region(ioport, ASC_IOADR_GAP);
11651 continue;
11652 }
11653
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011654 /*
11655 * I don't know why we need to do this for EISA chips, but
11656 * not for any others. It looks to be equivalent to
11657 * AscGetChipCfgMsw, but I may have overlooked something,
11658 * so I'm not converting it until I get an EISA board to
11659 * test with.
11660 */
11661 inw(ioport + 4);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011662
11663 if (!irq)
11664 irq = advansys_eisa_irq_no(edev);
11665
11666 err = -ENOMEM;
11667 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11668 if (!shost)
11669 goto release_region;
11670
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011671 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011672 board->irq = irq;
11673 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011674 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011675
11676 err = advansys_board_found(shost, ioport, ASC_IS_EISA);
11677 if (!err) {
11678 data->host[i] = shost;
11679 continue;
Matthew Wilcox71f36112007-07-30 08:04:53 -060011680 }
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011681
11682 scsi_host_put(shost);
11683 release_region:
11684 release_region(ioport, ASC_IOADR_GAP);
11685 break;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011686 }
11687
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011688 if (err)
11689 goto free_data;
11690 dev_set_drvdata(dev, data);
11691 return 0;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011692
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011693 free_data:
11694 kfree(data->host[0]);
11695 kfree(data->host[1]);
11696 kfree(data);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011697 fail:
11698 return err;
11699}
11700
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011701static int advansys_eisa_remove(struct device *dev)
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011702{
11703 int i;
11704 struct eisa_scsi_data *data = dev_get_drvdata(dev);
11705
11706 for (i = 0; i < 2; i++) {
Matthew Wilcox71f36112007-07-30 08:04:53 -060011707 int ioport;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011708 struct Scsi_Host *shost = data->host[i];
11709 if (!shost)
11710 continue;
Matthew Wilcox71f36112007-07-30 08:04:53 -060011711 ioport = shost->io_port;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011712 advansys_release(shost);
Matthew Wilcox71f36112007-07-30 08:04:53 -060011713 release_region(ioport, ASC_IOADR_GAP);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011714 }
11715
11716 kfree(data);
11717 return 0;
11718}
11719
11720static struct eisa_driver advansys_eisa_driver = {
11721 .id_table = advansys_eisa_table,
11722 .driver = {
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011723 .name = DRV_NAME,
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011724 .probe = advansys_eisa_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011725 .remove = advansys_eisa_remove,
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011726 }
11727};
11728
Dave Jones2672ea82006-08-02 17:11:49 -040011729/* PCI Devices supported by this driver */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011730static struct pci_device_id advansys_pci_tbl[] = {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011731 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_1200A,
11732 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11733 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940,
11734 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11735 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940U,
11736 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11737 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940UW,
11738 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11739 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C0800_REV1,
11740 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11741 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C1600_REV1,
11742 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11743 {}
Dave Jones2672ea82006-08-02 17:11:49 -040011744};
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011745
Dave Jones2672ea82006-08-02 17:11:49 -040011746MODULE_DEVICE_TABLE(pci, advansys_pci_tbl);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011747
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011748static void advansys_set_latency(struct pci_dev *pdev)
Matthew Wilcox9649af32007-07-26 21:51:47 -060011749{
11750 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
11751 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
11752 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0);
11753 } else {
11754 u8 latency;
11755 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
11756 if (latency < 0x20)
11757 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
11758 }
11759}
11760
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011761static int advansys_pci_probe(struct pci_dev *pdev,
11762 const struct pci_device_id *ent)
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011763{
11764 int err, ioport;
11765 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011766 struct asc_board *board;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011767
11768 err = pci_enable_device(pdev);
11769 if (err)
11770 goto fail;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011771 err = pci_request_regions(pdev, DRV_NAME);
Matthew Wilcox71f36112007-07-30 08:04:53 -060011772 if (err)
11773 goto disable_device;
Matthew Wilcox9649af32007-07-26 21:51:47 -060011774 pci_set_master(pdev);
11775 advansys_set_latency(pdev);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011776
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011777 err = -ENODEV;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011778 if (pci_resource_len(pdev, 0) == 0)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011779 goto release_region;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011780
11781 ioport = pci_resource_start(pdev, 0);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011782
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011783 err = -ENOMEM;
11784 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011785 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011786 goto release_region;
11787
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011788 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011789 board->irq = pdev->irq;
11790 board->dev = &pdev->dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011791 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011792
11793 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW ||
11794 pdev->device == PCI_DEVICE_ID_38C0800_REV1 ||
11795 pdev->device == PCI_DEVICE_ID_38C1600_REV1) {
11796 board->flags |= ASC_IS_WIDE_BOARD;
11797 }
11798
11799 err = advansys_board_found(shost, ioport, ASC_IS_PCI);
11800 if (err)
11801 goto free_host;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011802
11803 pci_set_drvdata(pdev, shost);
11804 return 0;
11805
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011806 free_host:
11807 scsi_host_put(shost);
11808 release_region:
Matthew Wilcox71f36112007-07-30 08:04:53 -060011809 pci_release_regions(pdev);
11810 disable_device:
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011811 pci_disable_device(pdev);
11812 fail:
11813 return err;
11814}
11815
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011816static void advansys_pci_remove(struct pci_dev *pdev)
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011817{
11818 advansys_release(pci_get_drvdata(pdev));
Matthew Wilcox71f36112007-07-30 08:04:53 -060011819 pci_release_regions(pdev);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011820 pci_disable_device(pdev);
11821}
11822
11823static struct pci_driver advansys_pci_driver = {
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011824 .name = DRV_NAME,
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011825 .id_table = advansys_pci_tbl,
11826 .probe = advansys_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011827 .remove = advansys_pci_remove,
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011828};
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040011829
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011830static int __init advansys_init(void)
11831{
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011832 int error;
11833
11834 error = isa_register_driver(&advansys_isa_driver,
11835 ASC_IOADR_TABLE_MAX_IX);
11836 if (error)
11837 goto fail;
11838
11839 error = isa_register_driver(&advansys_vlb_driver,
11840 ASC_IOADR_TABLE_MAX_IX);
11841 if (error)
11842 goto unregister_isa;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011843
11844 error = eisa_driver_register(&advansys_eisa_driver);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011845 if (error)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011846 goto unregister_vlb;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011847
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011848 error = pci_register_driver(&advansys_pci_driver);
11849 if (error)
11850 goto unregister_eisa;
11851
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011852 return 0;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011853
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011854 unregister_eisa:
11855 eisa_driver_unregister(&advansys_eisa_driver);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011856 unregister_vlb:
11857 isa_unregister_driver(&advansys_vlb_driver);
11858 unregister_isa:
11859 isa_unregister_driver(&advansys_isa_driver);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011860 fail:
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011861 return error;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011862}
11863
11864static void __exit advansys_exit(void)
11865{
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011866 pci_unregister_driver(&advansys_pci_driver);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011867 eisa_driver_unregister(&advansys_eisa_driver);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011868 isa_unregister_driver(&advansys_vlb_driver);
11869 isa_unregister_driver(&advansys_isa_driver);
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011870}
11871
11872module_init(advansys_init);
11873module_exit(advansys_exit);
11874
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040011875MODULE_LICENSE("GPL");
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +053011876MODULE_FIRMWARE("advansys/mcode.bin");
11877MODULE_FIRMWARE("advansys/3550.bin");
11878MODULE_FIRMWARE("advansys/38C0800.bin");
11879MODULE_FIRMWARE("advansys/38C1600.bin");