blob: e1ce1b70c97389e9371d3ea574d540bb9471f249 [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;
Hannes Reinecke00742c92015-04-24 13:18:40 +02007245 unsigned long flags;
Matthew Wilcox51219352007-10-02 21:55:22 -04007246
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007247 ASC_DBG(2, "boardp 0x%p\n", boardp);
Hannes Reinecke00742c92015-04-24 13:18:40 +02007248 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox51219352007-10-02 21:55:22 -04007249 if (ASC_NARROW_BOARD(boardp)) {
7250 if (AscIsIntPending(shost->io_port)) {
7251 result = IRQ_HANDLED;
7252 ASC_STATS(shost, interrupt);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007253 ASC_DBG(1, "before AscISR()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007254 AscISR(&boardp->dvc_var.asc_dvc_var);
7255 }
7256 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007257 ASC_DBG(1, "before AdvISR()\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007258 if (AdvISR(&boardp->dvc_var.adv_dvc_var)) {
7259 result = IRQ_HANDLED;
7260 ASC_STATS(shost, interrupt);
7261 }
7262 }
Hannes Reinecke00742c92015-04-24 13:18:40 +02007263 spin_unlock_irqrestore(shost->host_lock, flags);
Matthew Wilcox51219352007-10-02 21:55:22 -04007264
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007265 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007266 return result;
7267}
7268
Hannes Reinecked647c782015-04-24 13:18:26 +02007269static bool AscHostReqRiscHalt(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04007270{
7271 int count = 0;
Hannes Reinecked647c782015-04-24 13:18:26 +02007272 bool sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007273 uchar saved_stop_code;
7274
7275 if (AscIsChipHalted(iop_base))
Hannes Reinecked647c782015-04-24 13:18:26 +02007276 return true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007277 saved_stop_code = AscReadLramByte(iop_base, ASCV_STOP_CODE_B);
7278 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
7279 ASC_STOP_HOST_REQ_RISC_HALT | ASC_STOP_REQ_RISC_STOP);
7280 do {
7281 if (AscIsChipHalted(iop_base)) {
Hannes Reinecked647c782015-04-24 13:18:26 +02007282 sta = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04007283 break;
7284 }
7285 mdelay(100);
7286 } while (count++ < 20);
7287 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B, saved_stop_code);
Hannes Reinecked647c782015-04-24 13:18:26 +02007288 return sta;
Matthew Wilcox51219352007-10-02 21:55:22 -04007289}
7290
Hannes Reinecked647c782015-04-24 13:18:26 +02007291static bool
Matthew Wilcox51219352007-10-02 21:55:22 -04007292AscSetRunChipSynRegAtID(PortAddr iop_base, uchar tid_no, uchar sdtr_data)
7293{
Hannes Reinecked647c782015-04-24 13:18:26 +02007294 bool sta = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04007295
7296 if (AscHostReqRiscHalt(iop_base)) {
7297 sta = AscSetChipSynRegAtID(iop_base, tid_no, sdtr_data);
7298 AscStartChip(iop_base);
7299 }
7300 return sta;
7301}
7302
7303static void AscAsyncFix(ASC_DVC_VAR *asc_dvc, struct scsi_device *sdev)
7304{
7305 char type = sdev->type;
7306 ASC_SCSI_BIT_ID_TYPE tid_bits = 1 << sdev->id;
7307
7308 if (!(asc_dvc->bug_fix_cntl & ASC_BUG_FIX_ASYN_USE_SYN))
7309 return;
7310 if (asc_dvc->init_sdtr & tid_bits)
7311 return;
7312
7313 if ((type == TYPE_ROM) && (strncmp(sdev->vendor, "HP ", 3) == 0))
7314 asc_dvc->pci_fix_asyn_xfer_always |= tid_bits;
7315
7316 asc_dvc->pci_fix_asyn_xfer |= tid_bits;
7317 if ((type == TYPE_PROCESSOR) || (type == TYPE_SCANNER) ||
7318 (type == TYPE_ROM) || (type == TYPE_TAPE))
7319 asc_dvc->pci_fix_asyn_xfer &= ~tid_bits;
7320
7321 if (asc_dvc->pci_fix_asyn_xfer & tid_bits)
7322 AscSetRunChipSynRegAtID(asc_dvc->iop_base, sdev->id,
7323 ASYN_SDTR_DATA_FIX_PCI_REV_AB);
7324}
7325
7326static void
7327advansys_narrow_slave_configure(struct scsi_device *sdev, ASC_DVC_VAR *asc_dvc)
7328{
7329 ASC_SCSI_BIT_ID_TYPE tid_bit = 1 << sdev->id;
7330 ASC_SCSI_BIT_ID_TYPE orig_use_tagged_qng = asc_dvc->use_tagged_qng;
7331
7332 if (sdev->lun == 0) {
7333 ASC_SCSI_BIT_ID_TYPE orig_init_sdtr = asc_dvc->init_sdtr;
7334 if ((asc_dvc->cfg->sdtr_enable & tid_bit) && sdev->sdtr) {
7335 asc_dvc->init_sdtr |= tid_bit;
7336 } else {
7337 asc_dvc->init_sdtr &= ~tid_bit;
7338 }
7339
7340 if (orig_init_sdtr != asc_dvc->init_sdtr)
7341 AscAsyncFix(asc_dvc, sdev);
7342 }
7343
7344 if (sdev->tagged_supported) {
7345 if (asc_dvc->cfg->cmd_qng_enabled & tid_bit) {
7346 if (sdev->lun == 0) {
7347 asc_dvc->cfg->can_tagged_qng |= tid_bit;
7348 asc_dvc->use_tagged_qng |= tid_bit;
7349 }
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007350 scsi_change_queue_depth(sdev,
Matthew Wilcox51219352007-10-02 21:55:22 -04007351 asc_dvc->max_dvc_qng[sdev->id]);
7352 }
7353 } else {
7354 if (sdev->lun == 0) {
7355 asc_dvc->cfg->can_tagged_qng &= ~tid_bit;
7356 asc_dvc->use_tagged_qng &= ~tid_bit;
7357 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007358 }
7359
7360 if ((sdev->lun == 0) &&
7361 (orig_use_tagged_qng != asc_dvc->use_tagged_qng)) {
7362 AscWriteLramByte(asc_dvc->iop_base, ASCV_DISC_ENABLE_B,
7363 asc_dvc->cfg->disc_enable);
7364 AscWriteLramByte(asc_dvc->iop_base, ASCV_USE_TAGGED_QNG_B,
7365 asc_dvc->use_tagged_qng);
7366 AscWriteLramByte(asc_dvc->iop_base, ASCV_CAN_TAGGED_QNG_B,
7367 asc_dvc->cfg->can_tagged_qng);
7368
7369 asc_dvc->max_dvc_qng[sdev->id] =
7370 asc_dvc->cfg->max_tag_qng[sdev->id];
7371 AscWriteLramByte(asc_dvc->iop_base,
7372 (ushort)(ASCV_MAX_DVC_QNG_BEG + sdev->id),
7373 asc_dvc->max_dvc_qng[sdev->id]);
7374 }
7375}
7376
7377/*
7378 * Wide Transfers
7379 *
7380 * If the EEPROM enabled WDTR for the device and the device supports wide
7381 * bus (16 bit) transfers, then turn on the device's 'wdtr_able' bit and
7382 * write the new value to the microcode.
7383 */
7384static void
7385advansys_wide_enable_wdtr(AdvPortAddr iop_base, unsigned short tidmask)
7386{
7387 unsigned short cfg_word;
7388 AdvReadWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7389 if ((cfg_word & tidmask) != 0)
7390 return;
7391
7392 cfg_word |= tidmask;
7393 AdvWriteWordLram(iop_base, ASC_MC_WDTR_ABLE, cfg_word);
7394
7395 /*
7396 * Clear the microcode SDTR and WDTR negotiation done indicators for
7397 * the target to cause it to negotiate with the new setting set above.
7398 * WDTR when accepted causes the target to enter asynchronous mode, so
7399 * SDTR must be negotiated.
7400 */
7401 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7402 cfg_word &= ~tidmask;
7403 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7404 AdvReadWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7405 cfg_word &= ~tidmask;
7406 AdvWriteWordLram(iop_base, ASC_MC_WDTR_DONE, cfg_word);
7407}
7408
7409/*
7410 * Synchronous Transfers
7411 *
7412 * If the EEPROM enabled SDTR for the device and the device
7413 * supports synchronous transfers, then turn on the device's
7414 * 'sdtr_able' bit. Write the new value to the microcode.
7415 */
7416static void
7417advansys_wide_enable_sdtr(AdvPortAddr iop_base, unsigned short tidmask)
7418{
7419 unsigned short cfg_word;
7420 AdvReadWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7421 if ((cfg_word & tidmask) != 0)
7422 return;
7423
7424 cfg_word |= tidmask;
7425 AdvWriteWordLram(iop_base, ASC_MC_SDTR_ABLE, cfg_word);
7426
7427 /*
7428 * Clear the microcode "SDTR negotiation" done indicator for the
7429 * target to cause it to negotiate with the new setting set above.
7430 */
7431 AdvReadWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7432 cfg_word &= ~tidmask;
7433 AdvWriteWordLram(iop_base, ASC_MC_SDTR_DONE, cfg_word);
7434}
7435
7436/*
7437 * PPR (Parallel Protocol Request) Capable
7438 *
7439 * If the device supports DT mode, then it must be PPR capable.
7440 * The PPR message will be used in place of the SDTR and WDTR
7441 * messages to negotiate synchronous speed and offset, transfer
7442 * width, and protocol options.
7443 */
7444static void advansys_wide_enable_ppr(ADV_DVC_VAR *adv_dvc,
7445 AdvPortAddr iop_base, unsigned short tidmask)
7446{
7447 AdvReadWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7448 adv_dvc->ppr_able |= tidmask;
7449 AdvWriteWordLram(iop_base, ASC_MC_PPR_ABLE, adv_dvc->ppr_able);
7450}
7451
7452static void
7453advansys_wide_slave_configure(struct scsi_device *sdev, ADV_DVC_VAR *adv_dvc)
7454{
7455 AdvPortAddr iop_base = adv_dvc->iop_base;
7456 unsigned short tidmask = 1 << sdev->id;
7457
7458 if (sdev->lun == 0) {
7459 /*
7460 * Handle WDTR, SDTR, and Tag Queuing. If the feature
7461 * is enabled in the EEPROM and the device supports the
7462 * feature, then enable it in the microcode.
7463 */
7464
7465 if ((adv_dvc->wdtr_able & tidmask) && sdev->wdtr)
7466 advansys_wide_enable_wdtr(iop_base, tidmask);
7467 if ((adv_dvc->sdtr_able & tidmask) && sdev->sdtr)
7468 advansys_wide_enable_sdtr(iop_base, tidmask);
7469 if (adv_dvc->chip_type == ADV_CHIP_ASC38C1600 && sdev->ppr)
7470 advansys_wide_enable_ppr(adv_dvc, iop_base, tidmask);
7471
7472 /*
7473 * Tag Queuing is disabled for the BIOS which runs in polled
7474 * mode and would see no benefit from Tag Queuing. Also by
7475 * disabling Tag Queuing in the BIOS devices with Tag Queuing
7476 * bugs will at least work with the BIOS.
7477 */
7478 if ((adv_dvc->tagqng_able & tidmask) &&
7479 sdev->tagged_supported) {
7480 unsigned short cfg_word;
7481 AdvReadWordLram(iop_base, ASC_MC_TAGQNG_ABLE, cfg_word);
7482 cfg_word |= tidmask;
7483 AdvWriteWordLram(iop_base, ASC_MC_TAGQNG_ABLE,
7484 cfg_word);
7485 AdvWriteByteLram(iop_base,
7486 ASC_MC_NUMBER_OF_MAX_CMD + sdev->id,
7487 adv_dvc->max_dvc_qng);
7488 }
7489 }
7490
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007491 if ((adv_dvc->tagqng_able & tidmask) && sdev->tagged_supported)
7492 scsi_change_queue_depth(sdev, adv_dvc->max_dvc_qng);
Matthew Wilcox51219352007-10-02 21:55:22 -04007493}
7494
7495/*
7496 * Set the number of commands to queue per device for the
7497 * specified host adapter.
7498 */
7499static int advansys_slave_configure(struct scsi_device *sdev)
7500{
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007501 struct asc_board *boardp = shost_priv(sdev->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04007502
Matthew Wilcox51219352007-10-02 21:55:22 -04007503 if (ASC_NARROW_BOARD(boardp))
7504 advansys_narrow_slave_configure(sdev,
7505 &boardp->dvc_var.asc_dvc_var);
7506 else
7507 advansys_wide_slave_configure(sdev,
7508 &boardp->dvc_var.adv_dvc_var);
7509
7510 return 0;
7511}
7512
Hannes Reineckebc31ec42015-04-24 13:18:41 +02007513static __le32 asc_get_sense_buffer_dma(struct scsi_cmnd *scp)
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007514{
7515 struct asc_board *board = shost_priv(scp->device->host);
Hannes Reineckebc31ec42015-04-24 13:18:41 +02007516
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007517 scp->SCp.dma_handle = dma_map_single(board->dev, scp->sense_buffer,
Hannes Reineckebc31ec42015-04-24 13:18:41 +02007518 SCSI_SENSE_BUFFERSIZE,
7519 DMA_FROM_DEVICE);
Hannes Reinecke22070632015-04-24 13:18:39 +02007520 if (dma_mapping_error(board->dev, scp->SCp.dma_handle)) {
7521 ASC_DBG(1, "failed to map sense buffer\n");
7522 return 0;
7523 }
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007524 return cpu_to_le32(scp->SCp.dma_handle);
7525}
7526
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007527static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
Matthew Wilcox05848b62007-10-02 21:55:25 -04007528 struct asc_scsi_q *asc_scsi_q)
Matthew Wilcox51219352007-10-02 21:55:22 -04007529{
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007530 struct asc_dvc_var *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007531 int use_sg;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007532 u32 srb_tag;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007533
Matthew Wilcox05848b62007-10-02 21:55:25 -04007534 memset(asc_scsi_q, 0, sizeof(*asc_scsi_q));
Matthew Wilcox51219352007-10-02 21:55:22 -04007535
7536 /*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007537 * Set the srb_tag to the command tag + 1, as
7538 * srb_tag '0' is used internally by the chip.
Matthew Wilcox51219352007-10-02 21:55:22 -04007539 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007540 srb_tag = scp->request->tag + 1;
7541 asc_scsi_q->q2.srb_tag = srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007542
7543 /*
7544 * Build the ASC_SCSI_Q request.
7545 */
Matthew Wilcox05848b62007-10-02 21:55:25 -04007546 asc_scsi_q->cdbptr = &scp->cmnd[0];
7547 asc_scsi_q->q2.cdb_len = scp->cmd_len;
7548 asc_scsi_q->q1.target_id = ASC_TID_TO_TARGET_ID(scp->device->id);
7549 asc_scsi_q->q1.target_lun = scp->device->lun;
7550 asc_scsi_q->q2.target_ix =
Matthew Wilcox51219352007-10-02 21:55:22 -04007551 ASC_TIDLUN_TO_IX(scp->device->id, scp->device->lun);
Hannes Reineckebc31ec42015-04-24 13:18:41 +02007552 asc_scsi_q->q1.sense_addr = asc_get_sense_buffer_dma(scp);
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09007553 asc_scsi_q->q1.sense_len = SCSI_SENSE_BUFFERSIZE;
Hannes Reinecke22070632015-04-24 13:18:39 +02007554 if (!asc_scsi_q->q1.sense_addr)
7555 return ASC_BUSY;
Matthew Wilcox51219352007-10-02 21:55:22 -04007556
7557 /*
7558 * If there are any outstanding requests for the current target,
7559 * then every 255th request send an ORDERED request. This heuristic
7560 * tries to retain the benefit of request sorting while preventing
7561 * request starvation. 255 is the max number of tags or pending commands
7562 * a device may have outstanding.
7563 *
7564 * The request count is incremented below for every successfully
7565 * started request.
7566 *
7567 */
Matthew Wilcoxb249c7f2007-10-02 21:55:40 -04007568 if ((asc_dvc->cur_dvc_qng[scp->device->id] > 0) &&
Matthew Wilcox51219352007-10-02 21:55:22 -04007569 (boardp->reqcnt[scp->device->id] % 255) == 0) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007570 asc_scsi_q->q2.tag_code = ORDERED_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04007571 } else {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08007572 asc_scsi_q->q2.tag_code = SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04007573 }
7574
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007575 /* Build ASC_SCSI_Q */
7576 use_sg = scsi_dma_map(scp);
Hannes Reinecke22070632015-04-24 13:18:39 +02007577 if (use_sg < 0) {
7578 ASC_DBG(1, "failed to map sglist\n");
7579 return ASC_BUSY;
7580 } else if (use_sg > 0) {
Matthew Wilcox51219352007-10-02 21:55:22 -04007581 int sgcnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04007582 struct scatterlist *slp;
Matthew Wilcox05848b62007-10-02 21:55:25 -04007583 struct asc_sg_head *asc_sg_head;
Matthew Wilcox51219352007-10-02 21:55:22 -04007584
Matthew Wilcox51219352007-10-02 21:55:22 -04007585 if (use_sg > scp->device->host->sg_tablesize) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04007586 scmd_printk(KERN_ERR, scp, "use_sg %d > "
7587 "sg_tablesize %d\n", use_sg,
7588 scp->device->host->sg_tablesize);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007589 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007590 scp->result = HOST_BYTE(DID_ERROR);
7591 return ASC_ERROR;
7592 }
7593
Matthew Wilcox05848b62007-10-02 21:55:25 -04007594 asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) +
7595 use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC);
7596 if (!asc_sg_head) {
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007597 scsi_dma_unmap(scp);
Matthew Wilcox05848b62007-10-02 21:55:25 -04007598 scp->result = HOST_BYTE(DID_SOFT_ERROR);
7599 return ASC_ERROR;
7600 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007601
Matthew Wilcox05848b62007-10-02 21:55:25 -04007602 asc_scsi_q->q1.cntl |= QC_SG_HEAD;
7603 asc_scsi_q->sg_head = asc_sg_head;
7604 asc_scsi_q->q1.data_cnt = 0;
7605 asc_scsi_q->q1.data_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007606 /* This is a byte value, otherwise it would need to be swapped. */
Matthew Wilcox05848b62007-10-02 21:55:25 -04007607 asc_sg_head->entry_cnt = asc_scsi_q->q1.sg_queue_cnt = use_sg;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007608 ASC_STATS_ADD(scp->device->host, xfer_elem,
Matthew Wilcox05848b62007-10-02 21:55:25 -04007609 asc_sg_head->entry_cnt);
Matthew Wilcox51219352007-10-02 21:55:22 -04007610
7611 /*
7612 * Convert scatter-gather list into ASC_SG_HEAD list.
7613 */
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007614 scsi_for_each_sg(scp, slp, use_sg, sgcnt) {
Matthew Wilcox05848b62007-10-02 21:55:25 -04007615 asc_sg_head->sg_list[sgcnt].addr =
Matthew Wilcox51219352007-10-02 21:55:22 -04007616 cpu_to_le32(sg_dma_address(slp));
Matthew Wilcox05848b62007-10-02 21:55:25 -04007617 asc_sg_head->sg_list[sgcnt].bytes =
Matthew Wilcox51219352007-10-02 21:55:22 -04007618 cpu_to_le32(sg_dma_len(slp));
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007619 ASC_STATS_ADD(scp->device->host, xfer_sect,
7620 DIV_ROUND_UP(sg_dma_len(slp), 512));
Matthew Wilcox51219352007-10-02 21:55:22 -04007621 }
7622 }
7623
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007624 ASC_STATS(scp->device->host, xfer_cnt);
7625
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007626 ASC_DBG_PRT_ASC_SCSI_Q(2, asc_scsi_q);
Matthew Wilcox51219352007-10-02 21:55:22 -04007627 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7628
7629 return ASC_NOERROR;
7630}
7631
7632/*
7633 * Build scatter-gather list for Adv Library (Wide Board).
7634 *
7635 * Additional ADV_SG_BLOCK structures will need to be allocated
7636 * if the total number of scatter-gather elements exceeds
7637 * NO_OF_SG_PER_BLOCK (15). The ADV_SG_BLOCK structures are
7638 * assumed to be physically contiguous.
7639 *
7640 * Return:
7641 * ADV_SUCCESS(1) - SG List successfully created
7642 * ADV_ERROR(-1) - SG List creation failed
7643 */
7644static int
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007645adv_get_sglist(struct asc_board *boardp, adv_req_t *reqp,
7646 ADV_SCSI_REQ_Q *scsiqp, struct scsi_cmnd *scp, int use_sg)
Matthew Wilcox51219352007-10-02 21:55:22 -04007647{
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007648 adv_sgblk_t *sgblkp, *prev_sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007649 struct scatterlist *slp;
7650 int sg_elem_cnt;
7651 ADV_SG_BLOCK *sg_block, *prev_sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007652 dma_addr_t sgblk_paddr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007653 int i;
7654
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007655 slp = scsi_sglist(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007656 sg_elem_cnt = use_sg;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007657 prev_sgblkp = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007658 prev_sg_block = NULL;
7659 reqp->sgblkp = NULL;
7660
7661 for (;;) {
7662 /*
7663 * Allocate a 'adv_sgblk_t' structure from the board free
7664 * list. One 'adv_sgblk_t' structure holds NO_OF_SG_PER_BLOCK
7665 * (15) scatter-gather elements.
7666 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007667 sgblkp = dma_pool_alloc(boardp->adv_sgblk_pool, GFP_ATOMIC,
7668 &sgblk_paddr);
7669 if (!sgblkp) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007670 ASC_DBG(1, "no free adv_sgblk_t\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007671 ASC_STATS(scp->device->host, adv_build_nosg);
7672
7673 /*
7674 * Allocation failed. Free 'adv_sgblk_t' structures
7675 * already allocated for the request.
7676 */
7677 while ((sgblkp = reqp->sgblkp) != NULL) {
7678 /* Remove 'sgblkp' from the request list. */
7679 reqp->sgblkp = sgblkp->next_sgblkp;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007680 sgblkp->next_sgblkp = NULL;
7681 dma_pool_free(boardp->adv_sgblk_pool, sgblkp,
7682 sgblkp->sg_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007683 }
7684 return ASC_BUSY;
7685 }
Matthew Wilcox51219352007-10-02 21:55:22 -04007686 /* Complete 'adv_sgblk_t' board allocation. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007687 sgblkp->sg_addr = sgblk_paddr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007688 sgblkp->next_sgblkp = NULL;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007689 sg_block = &sgblkp->sg_block;
Matthew Wilcox51219352007-10-02 21:55:22 -04007690
7691 /*
7692 * Check if this is the first 'adv_sgblk_t' for the
7693 * request.
7694 */
7695 if (reqp->sgblkp == NULL) {
7696 /* Request's first scatter-gather block. */
7697 reqp->sgblkp = sgblkp;
7698
7699 /*
7700 * Set ADV_SCSI_REQ_T ADV_SG_BLOCK virtual and physical
7701 * address pointers.
7702 */
7703 scsiqp->sg_list_ptr = sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007704 scsiqp->sg_real_addr = cpu_to_le32(sgblk_paddr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007705 } else {
7706 /* Request's second or later scatter-gather block. */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007707 prev_sgblkp->next_sgblkp = sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007708
7709 /*
7710 * Point the previous ADV_SG_BLOCK structure to
7711 * the newly allocated ADV_SG_BLOCK structure.
7712 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007713 prev_sg_block->sg_ptr = cpu_to_le32(sgblk_paddr);
Matthew Wilcox51219352007-10-02 21:55:22 -04007714 }
7715
7716 for (i = 0; i < NO_OF_SG_PER_BLOCK; i++) {
7717 sg_block->sg_list[i].sg_addr =
7718 cpu_to_le32(sg_dma_address(slp));
7719 sg_block->sg_list[i].sg_count =
7720 cpu_to_le32(sg_dma_len(slp));
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007721 ASC_STATS_ADD(scp->device->host, xfer_sect,
7722 DIV_ROUND_UP(sg_dma_len(slp), 512));
Matthew Wilcox51219352007-10-02 21:55:22 -04007723
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007724 if (--sg_elem_cnt == 0) {
7725 /*
7726 * Last ADV_SG_BLOCK and scatter-gather entry.
7727 */
Matthew Wilcox51219352007-10-02 21:55:22 -04007728 sg_block->sg_cnt = i + 1;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007729 sg_block->sg_ptr = 0L; /* Last ADV_SG_BLOCK in list. */
Matthew Wilcox51219352007-10-02 21:55:22 -04007730 return ADV_SUCCESS;
7731 }
7732 slp++;
7733 }
7734 sg_block->sg_cnt = NO_OF_SG_PER_BLOCK;
7735 prev_sg_block = sg_block;
Hannes Reinecke0ce53822015-04-24 13:18:25 +02007736 prev_sgblkp = sgblkp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007737 }
7738}
7739
7740/*
7741 * Build a request structure for the Adv Library (Wide Board).
7742 *
7743 * If an adv_req_t can not be allocated to issue the request,
7744 * then return ASC_BUSY. If an error occurs, then return ASC_ERROR.
7745 *
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02007746 * Multi-byte fields in the ADV_SCSI_REQ_Q that are used by the
Matthew Wilcox51219352007-10-02 21:55:22 -04007747 * microcode for DMA addresses or math operations are byte swapped
7748 * to little-endian order.
7749 */
7750static int
Matthew Wilcoxd2411492007-10-02 21:55:31 -04007751adv_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007752 adv_req_t **adv_reqpp)
Matthew Wilcox51219352007-10-02 21:55:22 -04007753{
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007754 u32 srb_tag = scp->request->tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007755 adv_req_t *reqp;
7756 ADV_SCSI_REQ_Q *scsiqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007757 int ret;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007758 int use_sg;
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007759 dma_addr_t sense_addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04007760
7761 /*
7762 * Allocate an adv_req_t structure from the board to execute
7763 * the command.
7764 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007765 reqp = &boardp->adv_reqp[srb_tag];
7766 if (reqp->cmndp && reqp->cmndp != scp ) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04007767 ASC_DBG(1, "no free adv_req_t\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04007768 ASC_STATS(scp->device->host, adv_build_noreq);
7769 return ASC_BUSY;
Matthew Wilcox51219352007-10-02 21:55:22 -04007770 }
7771
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007772 reqp->req_addr = boardp->adv_reqp_addr + (srb_tag * sizeof(adv_req_t));
7773
7774 scsiqp = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04007775
7776 /*
7777 * Initialize the structure.
7778 */
7779 scsiqp->cntl = scsiqp->scsi_cntl = scsiqp->done_status = 0;
7780
7781 /*
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007782 * Set the srb_tag to the command tag.
Matthew Wilcox51219352007-10-02 21:55:22 -04007783 */
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007784 scsiqp->srb_tag = srb_tag;
Matthew Wilcox51219352007-10-02 21:55:22 -04007785
7786 /*
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007787 * Set 'host_scribble' to point to the adv_req_t structure.
Matthew Wilcox51219352007-10-02 21:55:22 -04007788 */
7789 reqp->cmndp = scp;
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007790 scp->host_scribble = (void *)reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007791
7792 /*
7793 * Build the ADV_SCSI_REQ_Q request.
7794 */
7795
7796 /* Set CDB length and copy it to the request structure. */
7797 scsiqp->cdb_len = scp->cmd_len;
7798 /* Copy first 12 CDB bytes to cdb[]. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007799 memcpy(scsiqp->cdb, scp->cmnd, scp->cmd_len < 12 ? scp->cmd_len : 12);
Matthew Wilcox51219352007-10-02 21:55:22 -04007800 /* Copy last 4 CDB bytes, if present, to cdb16[]. */
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007801 if (scp->cmd_len > 12) {
7802 int cdb16_len = scp->cmd_len - 12;
7803
7804 memcpy(scsiqp->cdb16, &scp->cmnd[12], cdb16_len);
Matthew Wilcox51219352007-10-02 21:55:22 -04007805 }
7806
7807 scsiqp->target_id = scp->device->id;
7808 scsiqp->target_lun = scp->device->lun;
7809
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007810 sense_addr = dma_map_single(boardp->dev, scp->sense_buffer,
7811 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
Hannes Reinecke22070632015-04-24 13:18:39 +02007812 if (dma_mapping_error(boardp->dev, sense_addr)) {
7813 ASC_DBG(1, "failed to map sense buffer\n");
7814 ASC_STATS(scp->device->host, adv_build_noreq);
7815 return ASC_BUSY;
7816 }
Hannes Reinecke811ddc02015-04-24 13:18:22 +02007817 scsiqp->sense_addr = cpu_to_le32(sense_addr);
7818 scsiqp->sense_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
Matthew Wilcox51219352007-10-02 21:55:22 -04007819
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007820 /* Build ADV_SCSI_REQ_Q */
Matthew Wilcox51219352007-10-02 21:55:22 -04007821
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007822 use_sg = scsi_dma_map(scp);
Hannes Reinecke22070632015-04-24 13:18:39 +02007823 if (use_sg < 0) {
7824 ASC_DBG(1, "failed to map SG list\n");
7825 ASC_STATS(scp->device->host, adv_build_noreq);
7826 return ASC_BUSY;
7827 } else if (use_sg == 0) {
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007828 /* Zero-length transfer */
Matthew Wilcox51219352007-10-02 21:55:22 -04007829 reqp->sgblkp = NULL;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007830 scsiqp->data_cnt = 0;
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007831
7832 scsiqp->data_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007833 scsiqp->sg_list_ptr = NULL;
7834 scsiqp->sg_real_addr = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04007835 } else {
Matthew Wilcox51219352007-10-02 21:55:22 -04007836 if (use_sg > ADV_MAX_SG_LIST) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04007837 scmd_printk(KERN_ERR, scp, "use_sg %d > "
7838 "ADV_MAX_SG_LIST %d\n", use_sg,
Matthew Wilcox51219352007-10-02 21:55:22 -04007839 scp->device->host->sg_tablesize);
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007840 scsi_dma_unmap(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04007841 scp->result = HOST_BYTE(DID_ERROR);
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007842 reqp->cmndp = NULL;
7843 scp->host_scribble = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007844
7845 return ASC_ERROR;
7846 }
7847
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007848 scsiqp->data_cnt = cpu_to_le32(scsi_bufflen(scp));
7849
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007850 ret = adv_get_sglist(boardp, reqp, scsiqp, scp, use_sg);
Matthew Wilcox51219352007-10-02 21:55:22 -04007851 if (ret != ADV_SUCCESS) {
Hannes Reinecke9c17c622015-04-24 13:18:21 +02007852 scsi_dma_unmap(scp);
7853 scp->result = HOST_BYTE(DID_ERROR);
7854 reqp->cmndp = NULL;
7855 scp->host_scribble = NULL;
Matthew Wilcox51219352007-10-02 21:55:22 -04007856
7857 return ret;
7858 }
7859
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007860 ASC_STATS_ADD(scp->device->host, xfer_elem, use_sg);
Matthew Wilcox51219352007-10-02 21:55:22 -04007861 }
7862
Matthew Wilcox52c334e2007-10-02 21:55:39 -04007863 ASC_STATS(scp->device->host, xfer_cnt);
7864
Matthew Wilcox51219352007-10-02 21:55:22 -04007865 ASC_DBG_PRT_ADV_SCSI_REQ_Q(2, scsiqp);
7866 ASC_DBG_PRT_CDB(1, scp->cmnd, scp->cmd_len);
7867
Hannes Reinecke4b47e462015-04-24 13:18:24 +02007868 *adv_reqpp = reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04007869
7870 return ASC_NOERROR;
7871}
7872
7873static int AscSgListToQueue(int sg_list)
7874{
7875 int n_sg_list_qs;
7876
7877 n_sg_list_qs = ((sg_list - 1) / ASC_SG_LIST_PER_Q);
7878 if (((sg_list - 1) % ASC_SG_LIST_PER_Q) != 0)
7879 n_sg_list_qs++;
7880 return n_sg_list_qs + 1;
7881}
7882
7883static uint
7884AscGetNumOfFreeQueue(ASC_DVC_VAR *asc_dvc, uchar target_ix, uchar n_qs)
7885{
7886 uint cur_used_qs;
7887 uint cur_free_qs;
7888 ASC_SCSI_BIT_ID_TYPE target_id;
7889 uchar tid_no;
7890
7891 target_id = ASC_TIX_TO_TARGET_ID(target_ix);
7892 tid_no = ASC_TIX_TO_TID(target_ix);
7893 if ((asc_dvc->unit_not_ready & target_id) ||
7894 (asc_dvc->queue_full_or_busy & target_id)) {
7895 return 0;
7896 }
7897 if (n_qs == 1) {
7898 cur_used_qs = (uint) asc_dvc->cur_total_qng +
7899 (uint) asc_dvc->last_q_shortage + (uint) ASC_MIN_FREE_Q;
7900 } else {
7901 cur_used_qs = (uint) asc_dvc->cur_total_qng +
7902 (uint) ASC_MIN_FREE_Q;
7903 }
7904 if ((uint) (cur_used_qs + n_qs) <= (uint) asc_dvc->max_total_qng) {
7905 cur_free_qs = (uint) asc_dvc->max_total_qng - cur_used_qs;
7906 if (asc_dvc->cur_dvc_qng[tid_no] >=
7907 asc_dvc->max_dvc_qng[tid_no]) {
7908 return 0;
7909 }
7910 return cur_free_qs;
7911 }
7912 if (n_qs > 1) {
7913 if ((n_qs > asc_dvc->last_q_shortage)
7914 && (n_qs <= (asc_dvc->max_total_qng - ASC_MIN_FREE_Q))) {
7915 asc_dvc->last_q_shortage = n_qs;
7916 }
7917 }
7918 return 0;
7919}
7920
7921static uchar AscAllocFreeQueue(PortAddr iop_base, uchar free_q_head)
7922{
7923 ushort q_addr;
7924 uchar next_qp;
7925 uchar q_status;
7926
7927 q_addr = ASC_QNO_TO_QADDR(free_q_head);
7928 q_status = (uchar)AscReadLramByte(iop_base,
7929 (ushort)(q_addr +
7930 ASC_SCSIQ_B_STATUS));
7931 next_qp = AscReadLramByte(iop_base, (ushort)(q_addr + ASC_SCSIQ_B_FWD));
7932 if (((q_status & QS_READY) == 0) && (next_qp != ASC_QLINK_END))
7933 return next_qp;
7934 return ASC_QLINK_END;
7935}
7936
7937static uchar
7938AscAllocMultipleFreeQueue(PortAddr iop_base, uchar free_q_head, uchar n_free_q)
7939{
7940 uchar i;
7941
7942 for (i = 0; i < n_free_q; i++) {
7943 free_q_head = AscAllocFreeQueue(iop_base, free_q_head);
7944 if (free_q_head == ASC_QLINK_END)
7945 break;
7946 }
7947 return free_q_head;
7948}
7949
7950/*
7951 * void
7952 * DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
7953 *
7954 * Calling/Exit State:
7955 * none
7956 *
7957 * Description:
7958 * Output an ASC_SCSI_Q structure to the chip
7959 */
7960static void
7961DvcPutScsiQ(PortAddr iop_base, ushort s_addr, uchar *outbuf, int words)
7962{
7963 int i;
7964
7965 ASC_DBG_PRT_HEX(2, "DvcPutScsiQ", outbuf, 2 * words);
7966 AscSetChipLramAddr(iop_base, s_addr);
7967 for (i = 0; i < 2 * words; i += 2) {
7968 if (i == 4 || i == 20) {
7969 continue;
7970 }
7971 outpw(iop_base + IOP_RAM_DATA,
7972 ((ushort)outbuf[i + 1] << 8) | outbuf[i]);
7973 }
7974}
7975
7976static int AscPutReadyQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
7977{
7978 ushort q_addr;
7979 uchar tid_no;
7980 uchar sdtr_data;
7981 uchar syn_period_ix;
7982 uchar syn_offset;
7983 PortAddr iop_base;
7984
7985 iop_base = asc_dvc->iop_base;
7986 if (((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) &&
7987 ((asc_dvc->sdtr_done & scsiq->q1.target_id) == 0)) {
7988 tid_no = ASC_TIX_TO_TID(scsiq->q2.target_ix);
7989 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
7990 syn_period_ix =
7991 (sdtr_data >> 4) & (asc_dvc->max_sdtr_index - 1);
7992 syn_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
7993 AscMsgOutSDTR(asc_dvc,
7994 asc_dvc->sdtr_period_tbl[syn_period_ix],
7995 syn_offset);
7996 scsiq->q1.cntl |= QC_MSG_OUT;
7997 }
7998 q_addr = ASC_QNO_TO_QADDR(q_no);
7999 if ((scsiq->q1.target_id & asc_dvc->use_tagged_qng) == 0) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08008000 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04008001 }
8002 scsiq->q1.status = QS_FREE;
8003 AscMemWordCopyPtrToLram(iop_base,
8004 q_addr + ASC_SCSIQ_CDB_BEG,
8005 (uchar *)scsiq->cdbptr, scsiq->q2.cdb_len >> 1);
8006
8007 DvcPutScsiQ(iop_base,
8008 q_addr + ASC_SCSIQ_CPY_BEG,
8009 (uchar *)&scsiq->q1.cntl,
8010 ((sizeof(ASC_SCSIQ_1) + sizeof(ASC_SCSIQ_2)) / 2) - 1);
8011 AscWriteLramWord(iop_base,
8012 (ushort)(q_addr + (ushort)ASC_SCSIQ_B_STATUS),
8013 (ushort)(((ushort)scsiq->q1.
8014 q_no << 8) | (ushort)QS_READY));
8015 return 1;
8016}
8017
8018static int
8019AscPutReadySgListQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar q_no)
8020{
8021 int sta;
8022 int i;
8023 ASC_SG_HEAD *sg_head;
8024 ASC_SG_LIST_Q scsi_sg_q;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008025 __le32 saved_data_addr;
8026 __le32 saved_data_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008027 PortAddr iop_base;
8028 ushort sg_list_dwords;
8029 ushort sg_index;
8030 ushort sg_entry_cnt;
8031 ushort q_addr;
8032 uchar next_qp;
8033
8034 iop_base = asc_dvc->iop_base;
8035 sg_head = scsiq->sg_head;
8036 saved_data_addr = scsiq->q1.data_addr;
8037 saved_data_cnt = scsiq->q1.data_cnt;
Hannes Reinecke37f64602015-04-24 13:18:32 +02008038 scsiq->q1.data_addr = cpu_to_le32(sg_head->sg_list[0].addr);
8039 scsiq->q1.data_cnt = cpu_to_le32(sg_head->sg_list[0].bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008040 /*
Hannes Reinecke96aabb22015-04-24 13:18:34 +02008041 * Set sg_entry_cnt to be the number of SG elements that
8042 * will fit in the allocated SG queues. It is minus 1, because
8043 * the first SG element is handled above.
Matthew Wilcox51219352007-10-02 21:55:22 -04008044 */
Hannes Reinecke96aabb22015-04-24 13:18:34 +02008045 sg_entry_cnt = sg_head->entry_cnt - 1;
Matthew Wilcox51219352007-10-02 21:55:22 -04008046
Matthew Wilcox51219352007-10-02 21:55:22 -04008047 if (sg_entry_cnt != 0) {
8048 scsiq->q1.cntl |= QC_SG_HEAD;
8049 q_addr = ASC_QNO_TO_QADDR(q_no);
8050 sg_index = 1;
8051 scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
8052 scsi_sg_q.sg_head_qp = q_no;
8053 scsi_sg_q.cntl = QCSG_SG_XFER_LIST;
8054 for (i = 0; i < sg_head->queue_cnt; i++) {
8055 scsi_sg_q.seq_no = i + 1;
8056 if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
8057 sg_list_dwords = (uchar)(ASC_SG_LIST_PER_Q * 2);
8058 sg_entry_cnt -= ASC_SG_LIST_PER_Q;
8059 if (i == 0) {
8060 scsi_sg_q.sg_list_cnt =
8061 ASC_SG_LIST_PER_Q;
8062 scsi_sg_q.sg_cur_list_cnt =
8063 ASC_SG_LIST_PER_Q;
8064 } else {
8065 scsi_sg_q.sg_list_cnt =
8066 ASC_SG_LIST_PER_Q - 1;
8067 scsi_sg_q.sg_cur_list_cnt =
8068 ASC_SG_LIST_PER_Q - 1;
8069 }
8070 } else {
Hannes Reinecke96aabb22015-04-24 13:18:34 +02008071 scsi_sg_q.cntl |= QCSG_SG_XFER_END;
Matthew Wilcox51219352007-10-02 21:55:22 -04008072 sg_list_dwords = sg_entry_cnt << 1;
8073 if (i == 0) {
8074 scsi_sg_q.sg_list_cnt = sg_entry_cnt;
8075 scsi_sg_q.sg_cur_list_cnt =
8076 sg_entry_cnt;
8077 } else {
8078 scsi_sg_q.sg_list_cnt =
8079 sg_entry_cnt - 1;
8080 scsi_sg_q.sg_cur_list_cnt =
8081 sg_entry_cnt - 1;
8082 }
8083 sg_entry_cnt = 0;
8084 }
8085 next_qp = AscReadLramByte(iop_base,
8086 (ushort)(q_addr +
8087 ASC_SCSIQ_B_FWD));
8088 scsi_sg_q.q_no = next_qp;
8089 q_addr = ASC_QNO_TO_QADDR(next_qp);
8090 AscMemWordCopyPtrToLram(iop_base,
8091 q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
8092 (uchar *)&scsi_sg_q,
8093 sizeof(ASC_SG_LIST_Q) >> 1);
8094 AscMemDWordCopyPtrToLram(iop_base,
8095 q_addr + ASC_SGQ_LIST_BEG,
8096 (uchar *)&sg_head->
8097 sg_list[sg_index],
8098 sg_list_dwords);
8099 sg_index += ASC_SG_LIST_PER_Q;
8100 scsiq->next_sg_index = sg_index;
8101 }
8102 } else {
8103 scsiq->q1.cntl &= ~QC_SG_HEAD;
8104 }
8105 sta = AscPutReadyQueue(asc_dvc, scsiq, q_no);
8106 scsiq->q1.data_addr = saved_data_addr;
8107 scsiq->q1.data_cnt = saved_data_cnt;
8108 return (sta);
8109}
8110
8111static int
8112AscSendScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq, uchar n_q_required)
8113{
8114 PortAddr iop_base;
8115 uchar free_q_head;
8116 uchar next_qp;
8117 uchar tid_no;
8118 uchar target_ix;
8119 int sta;
8120
8121 iop_base = asc_dvc->iop_base;
8122 target_ix = scsiq->q2.target_ix;
8123 tid_no = ASC_TIX_TO_TID(target_ix);
8124 sta = 0;
8125 free_q_head = (uchar)AscGetVarFreeQHead(iop_base);
8126 if (n_q_required > 1) {
8127 next_qp = AscAllocMultipleFreeQueue(iop_base, free_q_head,
8128 (uchar)n_q_required);
8129 if (next_qp != ASC_QLINK_END) {
8130 asc_dvc->last_q_shortage = 0;
8131 scsiq->sg_head->queue_cnt = n_q_required - 1;
8132 scsiq->q1.q_no = free_q_head;
8133 sta = AscPutReadySgListQueue(asc_dvc, scsiq,
8134 free_q_head);
8135 }
8136 } else if (n_q_required == 1) {
8137 next_qp = AscAllocFreeQueue(iop_base, free_q_head);
8138 if (next_qp != ASC_QLINK_END) {
8139 scsiq->q1.q_no = free_q_head;
8140 sta = AscPutReadyQueue(asc_dvc, scsiq, free_q_head);
8141 }
8142 }
8143 if (sta == 1) {
8144 AscPutVarFreeQHead(iop_base, next_qp);
8145 asc_dvc->cur_total_qng += n_q_required;
8146 asc_dvc->cur_dvc_qng[tid_no]++;
8147 }
8148 return sta;
8149}
8150
8151#define ASC_SYN_OFFSET_ONE_DISABLE_LIST 16
8152static uchar _syn_offset_one_disable_cmd[ASC_SYN_OFFSET_ONE_DISABLE_LIST] = {
8153 INQUIRY,
8154 REQUEST_SENSE,
8155 READ_CAPACITY,
8156 READ_TOC,
8157 MODE_SELECT,
8158 MODE_SENSE,
8159 MODE_SELECT_10,
8160 MODE_SENSE_10,
8161 0xFF,
8162 0xFF,
8163 0xFF,
8164 0xFF,
8165 0xFF,
8166 0xFF,
8167 0xFF,
8168 0xFF
8169};
8170
8171static int AscExeScsiQueue(ASC_DVC_VAR *asc_dvc, ASC_SCSI_Q *scsiq)
8172{
8173 PortAddr iop_base;
8174 int sta;
8175 int n_q_required;
Hannes Reineckeae267592015-04-24 13:18:28 +02008176 bool disable_syn_offset_one_fix;
Matthew Wilcox51219352007-10-02 21:55:22 -04008177 int i;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008178 u32 addr;
Matthew Wilcox51219352007-10-02 21:55:22 -04008179 ushort sg_entry_cnt = 0;
8180 ushort sg_entry_cnt_minus_one = 0;
8181 uchar target_ix;
8182 uchar tid_no;
8183 uchar sdtr_data;
8184 uchar extra_bytes;
8185 uchar scsi_cmd;
8186 uchar disable_cmd;
8187 ASC_SG_HEAD *sg_head;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008188 unsigned long data_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008189
8190 iop_base = asc_dvc->iop_base;
8191 sg_head = scsiq->sg_head;
8192 if (asc_dvc->err_code != 0)
Hannes Reineckef1138a82015-04-24 13:18:29 +02008193 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008194 scsiq->q1.q_no = 0;
8195 if ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0) {
8196 scsiq->q1.extra_bytes = 0;
8197 }
8198 sta = 0;
8199 target_ix = scsiq->q2.target_ix;
8200 tid_no = ASC_TIX_TO_TID(target_ix);
8201 n_q_required = 1;
8202 if (scsiq->cdbptr[0] == REQUEST_SENSE) {
8203 if ((asc_dvc->init_sdtr & scsiq->q1.target_id) != 0) {
8204 asc_dvc->sdtr_done &= ~scsiq->q1.target_id;
8205 sdtr_data = AscGetMCodeInitSDTRAtID(iop_base, tid_no);
8206 AscMsgOutSDTR(asc_dvc,
8207 asc_dvc->
8208 sdtr_period_tbl[(sdtr_data >> 4) &
8209 (uchar)(asc_dvc->
8210 max_sdtr_index -
8211 1)],
8212 (uchar)(sdtr_data & (uchar)
8213 ASC_SYN_MAX_OFFSET));
8214 scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);
8215 }
8216 }
8217 if (asc_dvc->in_critical_cnt != 0) {
8218 AscSetLibErrorCode(asc_dvc, ASCQ_ERR_CRITICAL_RE_ENTRY);
Hannes Reineckef1138a82015-04-24 13:18:29 +02008219 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008220 }
8221 asc_dvc->in_critical_cnt++;
8222 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8223 if ((sg_entry_cnt = sg_head->entry_cnt) == 0) {
8224 asc_dvc->in_critical_cnt--;
Hannes Reineckef1138a82015-04-24 13:18:29 +02008225 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008226 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008227 if (sg_entry_cnt > ASC_MAX_SG_LIST) {
8228 asc_dvc->in_critical_cnt--;
Hannes Reineckef1138a82015-04-24 13:18:29 +02008229 return ASC_ERROR;
Matthew Wilcox51219352007-10-02 21:55:22 -04008230 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008231 if (sg_entry_cnt == 1) {
Hannes Reinecke37f64602015-04-24 13:18:32 +02008232 scsiq->q1.data_addr = cpu_to_le32(sg_head->sg_list[0].addr);
8233 scsiq->q1.data_cnt = cpu_to_le32(sg_head->sg_list[0].bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008234 scsiq->q1.cntl &= ~(QC_SG_HEAD | QC_SG_SWAP_QUEUE);
8235 }
8236 sg_entry_cnt_minus_one = sg_entry_cnt - 1;
8237 }
8238 scsi_cmd = scsiq->cdbptr[0];
Hannes Reineckeae267592015-04-24 13:18:28 +02008239 disable_syn_offset_one_fix = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04008240 if ((asc_dvc->pci_fix_asyn_xfer & scsiq->q1.target_id) &&
8241 !(asc_dvc->pci_fix_asyn_xfer_always & scsiq->q1.target_id)) {
8242 if (scsiq->q1.cntl & QC_SG_HEAD) {
8243 data_cnt = 0;
8244 for (i = 0; i < sg_entry_cnt; i++) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008245 data_cnt += le32_to_cpu(sg_head->sg_list[i].
8246 bytes);
Matthew Wilcox51219352007-10-02 21:55:22 -04008247 }
8248 } else {
8249 data_cnt = le32_to_cpu(scsiq->q1.data_cnt);
8250 }
8251 if (data_cnt != 0UL) {
8252 if (data_cnt < 512UL) {
Hannes Reineckeae267592015-04-24 13:18:28 +02008253 disable_syn_offset_one_fix = true;
Matthew Wilcox51219352007-10-02 21:55:22 -04008254 } else {
8255 for (i = 0; i < ASC_SYN_OFFSET_ONE_DISABLE_LIST;
8256 i++) {
8257 disable_cmd =
8258 _syn_offset_one_disable_cmd[i];
8259 if (disable_cmd == 0xFF) {
8260 break;
8261 }
8262 if (scsi_cmd == disable_cmd) {
8263 disable_syn_offset_one_fix =
Hannes Reineckeae267592015-04-24 13:18:28 +02008264 true;
Matthew Wilcox51219352007-10-02 21:55:22 -04008265 break;
8266 }
8267 }
8268 }
8269 }
8270 }
8271 if (disable_syn_offset_one_fix) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08008272 scsiq->q2.tag_code &= ~SIMPLE_QUEUE_TAG;
Matthew Wilcox51219352007-10-02 21:55:22 -04008273 scsiq->q2.tag_code |= (ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX |
8274 ASC_TAG_FLAG_DISABLE_DISCONNECT);
8275 } else {
8276 scsiq->q2.tag_code &= 0x27;
8277 }
8278 if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {
8279 if (asc_dvc->bug_fix_cntl) {
8280 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8281 if ((scsi_cmd == READ_6) ||
8282 (scsi_cmd == READ_10)) {
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008283 addr = le32_to_cpu(sg_head->
Matthew Wilcox51219352007-10-02 21:55:22 -04008284 sg_list
8285 [sg_entry_cnt_minus_one].
8286 addr) +
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008287 le32_to_cpu(sg_head->
Matthew Wilcox51219352007-10-02 21:55:22 -04008288 sg_list
8289 [sg_entry_cnt_minus_one].
8290 bytes);
8291 extra_bytes =
8292 (uchar)((ushort)addr & 0x0003);
8293 if ((extra_bytes != 0)
8294 &&
8295 ((scsiq->q2.
8296 tag_code &
8297 ASC_TAG_FLAG_EXTRA_BYTES)
8298 == 0)) {
8299 scsiq->q2.tag_code |=
8300 ASC_TAG_FLAG_EXTRA_BYTES;
8301 scsiq->q1.extra_bytes =
8302 extra_bytes;
8303 data_cnt =
8304 le32_to_cpu(sg_head->
8305 sg_list
8306 [sg_entry_cnt_minus_one].
8307 bytes);
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008308 data_cnt -= extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008309 sg_head->
8310 sg_list
8311 [sg_entry_cnt_minus_one].
8312 bytes =
8313 cpu_to_le32(data_cnt);
8314 }
8315 }
8316 }
8317 }
8318 sg_head->entry_to_copy = sg_head->entry_cnt;
Matthew Wilcox51219352007-10-02 21:55:22 -04008319 n_q_required = AscSgListToQueue(sg_entry_cnt);
8320 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, n_q_required) >=
8321 (uint) n_q_required)
8322 || ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8323 if ((sta =
8324 AscSendScsiQueue(asc_dvc, scsiq,
8325 n_q_required)) == 1) {
8326 asc_dvc->in_critical_cnt--;
8327 return (sta);
8328 }
8329 }
8330 } else {
8331 if (asc_dvc->bug_fix_cntl) {
8332 if (asc_dvc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
8333 if ((scsi_cmd == READ_6) ||
8334 (scsi_cmd == READ_10)) {
8335 addr =
8336 le32_to_cpu(scsiq->q1.data_addr) +
8337 le32_to_cpu(scsiq->q1.data_cnt);
8338 extra_bytes =
8339 (uchar)((ushort)addr & 0x0003);
8340 if ((extra_bytes != 0)
8341 &&
8342 ((scsiq->q2.
8343 tag_code &
8344 ASC_TAG_FLAG_EXTRA_BYTES)
8345 == 0)) {
8346 data_cnt =
8347 le32_to_cpu(scsiq->q1.
8348 data_cnt);
8349 if (((ushort)data_cnt & 0x01FF)
8350 == 0) {
8351 scsiq->q2.tag_code |=
8352 ASC_TAG_FLAG_EXTRA_BYTES;
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008353 data_cnt -= extra_bytes;
Matthew Wilcox51219352007-10-02 21:55:22 -04008354 scsiq->q1.data_cnt =
8355 cpu_to_le32
8356 (data_cnt);
8357 scsiq->q1.extra_bytes =
8358 extra_bytes;
8359 }
8360 }
8361 }
8362 }
8363 }
8364 n_q_required = 1;
8365 if ((AscGetNumOfFreeQueue(asc_dvc, target_ix, 1) >= 1) ||
8366 ((scsiq->q1.cntl & QC_URGENT) != 0)) {
8367 if ((sta = AscSendScsiQueue(asc_dvc, scsiq,
8368 n_q_required)) == 1) {
8369 asc_dvc->in_critical_cnt--;
8370 return (sta);
8371 }
8372 }
8373 }
8374 asc_dvc->in_critical_cnt--;
8375 return (sta);
8376}
8377
8378/*
8379 * AdvExeScsiQueue() - Send a request to the RISC microcode program.
8380 *
8381 * Allocate a carrier structure, point the carrier to the ADV_SCSI_REQ_Q,
8382 * add the carrier to the ICQ (Initiator Command Queue), and tickle the
8383 * RISC to notify it a new command is ready to be executed.
8384 *
8385 * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
8386 * set to SCSI_MAX_RETRY.
8387 *
Hannes Reinecke9fef6ba2015-04-24 13:18:33 +02008388 * Multi-byte fields in the ADV_SCSI_REQ_Q that are used by the microcode
Matthew Wilcox51219352007-10-02 21:55:22 -04008389 * for DMA addresses or math operations are byte swapped to little-endian
8390 * order.
8391 *
8392 * Return:
8393 * ADV_SUCCESS(1) - The request was successfully queued.
8394 * ADV_BUSY(0) - Resource unavailable; Retry again after pending
8395 * request completes.
8396 * ADV_ERROR(-1) - Invalid ADV_SCSI_REQ_Q request structure
8397 * host IC error.
8398 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008399static int AdvExeScsiQueue(ADV_DVC_VAR *asc_dvc, adv_req_t *reqp)
Matthew Wilcox51219352007-10-02 21:55:22 -04008400{
8401 AdvPortAddr iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008402 ADV_CARR_T *new_carrp;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008403 ADV_SCSI_REQ_Q *scsiq = &reqp->scsi_req_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04008404
8405 /*
8406 * The ADV_SCSI_REQ_Q 'target_id' field should never exceed ADV_MAX_TID.
8407 */
8408 if (scsiq->target_id > ADV_MAX_TID) {
8409 scsiq->host_status = QHSTA_M_INVALID_DEVICE;
8410 scsiq->done_status = QD_WITH_ERROR;
8411 return ADV_ERROR;
8412 }
8413
8414 iop_base = asc_dvc->iop_base;
8415
8416 /*
8417 * Allocate a carrier ensuring at least one carrier always
8418 * remains on the freelist and initialize fields.
8419 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008420 new_carrp = adv_get_next_carrier(asc_dvc);
8421 if (!new_carrp) {
8422 ASC_DBG(1, "No free carriers\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008423 return ADV_BUSY;
8424 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008425
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008426 asc_dvc->carr_pending_cnt++;
Matthew Wilcox51219352007-10-02 21:55:22 -04008427
Matthew Wilcox51219352007-10-02 21:55:22 -04008428 /* Save virtual and physical address of ADV_SCSI_REQ_Q and carrier. */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008429 scsiq->scsiq_ptr = cpu_to_le32(scsiq->srb_tag);
8430 scsiq->scsiq_rptr = cpu_to_le32(reqp->req_addr);
Matthew Wilcox51219352007-10-02 21:55:22 -04008431
Hannes Reinecke98b96a72015-04-24 13:18:23 +02008432 scsiq->carr_va = asc_dvc->icq_sp->carr_va;
Matthew Wilcox51219352007-10-02 21:55:22 -04008433 scsiq->carr_pa = asc_dvc->icq_sp->carr_pa;
8434
8435 /*
8436 * Use the current stopper to send the ADV_SCSI_REQ_Q command to
8437 * the microcode. The newly allocated stopper will become the new
8438 * stopper.
8439 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008440 asc_dvc->icq_sp->areq_vpa = scsiq->scsiq_rptr;
Matthew Wilcox51219352007-10-02 21:55:22 -04008441
8442 /*
8443 * Set the 'next_vpa' pointer for the old stopper to be the
8444 * physical address of the new stopper. The RISC can only
8445 * follow physical addresses.
8446 */
8447 asc_dvc->icq_sp->next_vpa = new_carrp->carr_pa;
8448
8449 /*
8450 * Set the host adapter stopper pointer to point to the new carrier.
8451 */
8452 asc_dvc->icq_sp = new_carrp;
8453
8454 if (asc_dvc->chip_type == ADV_CHIP_ASC3550 ||
8455 asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
8456 /*
8457 * Tickle the RISC to tell it to read its Command Queue Head pointer.
8458 */
8459 AdvWriteByteRegister(iop_base, IOPB_TICKLE, ADV_TICKLE_A);
8460 if (asc_dvc->chip_type == ADV_CHIP_ASC3550) {
8461 /*
8462 * Clear the tickle value. In the ASC-3550 the RISC flag
8463 * command 'clr_tickle_a' does not work unless the host
8464 * value is cleared.
8465 */
8466 AdvWriteByteRegister(iop_base, IOPB_TICKLE,
8467 ADV_TICKLE_NOP);
8468 }
8469 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
8470 /*
8471 * Notify the RISC a carrier is ready by writing the physical
8472 * address of the new carrier stopper to the COMMA register.
8473 */
8474 AdvWriteDWordRegister(iop_base, IOPDW_COMMA,
8475 le32_to_cpu(new_carrp->carr_pa));
8476 }
8477
8478 return ADV_SUCCESS;
8479}
8480
8481/*
8482 * Execute a single 'Scsi_Cmnd'.
Matthew Wilcox51219352007-10-02 21:55:22 -04008483 */
8484static int asc_execute_scsi_cmnd(struct scsi_cmnd *scp)
8485{
Matthew Wilcox41d24932007-10-02 21:55:24 -04008486 int ret, err_code;
Matthew Wilcoxd2411492007-10-02 21:55:31 -04008487 struct asc_board *boardp = shost_priv(scp->device->host);
Matthew Wilcox51219352007-10-02 21:55:22 -04008488
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008489 ASC_DBG(1, "scp 0x%p\n", scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008490
8491 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox41d24932007-10-02 21:55:24 -04008492 ASC_DVC_VAR *asc_dvc = &boardp->dvc_var.asc_dvc_var;
Matthew Wilcox05848b62007-10-02 21:55:25 -04008493 struct asc_scsi_q asc_scsi_q;
Matthew Wilcox51219352007-10-02 21:55:22 -04008494
Matthew Wilcox05848b62007-10-02 21:55:25 -04008495 ret = asc_build_req(boardp, scp, &asc_scsi_q);
Hannes Reinecke22070632015-04-24 13:18:39 +02008496 if (ret != ASC_NOERROR) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008497 ASC_STATS(scp->device->host, build_error);
Hannes Reinecke22070632015-04-24 13:18:39 +02008498 return ret;
Matthew Wilcox51219352007-10-02 21:55:22 -04008499 }
8500
Matthew Wilcox41d24932007-10-02 21:55:24 -04008501 ret = AscExeScsiQueue(asc_dvc, &asc_scsi_q);
Matthew Wilcox05848b62007-10-02 21:55:25 -04008502 kfree(asc_scsi_q.sg_head);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008503 err_code = asc_dvc->err_code;
Matthew Wilcox51219352007-10-02 21:55:22 -04008504 } else {
Matthew Wilcox41d24932007-10-02 21:55:24 -04008505 ADV_DVC_VAR *adv_dvc = &boardp->dvc_var.adv_dvc_var;
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008506 adv_req_t *adv_reqp;
Matthew Wilcox51219352007-10-02 21:55:22 -04008507
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008508 switch (adv_build_req(boardp, scp, &adv_reqp)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008509 case ASC_NOERROR:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008510 ASC_DBG(3, "adv_build_req ASC_NOERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008511 break;
8512 case ASC_BUSY:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008513 ASC_DBG(1, "adv_build_req ASC_BUSY\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008514 /*
8515 * The asc_stats fields 'adv_build_noreq' and
8516 * 'adv_build_nosg' count wide board busy conditions.
8517 * They are updated in adv_build_req and
8518 * adv_get_sglist, respectively.
8519 */
8520 return ASC_BUSY;
8521 case ASC_ERROR:
8522 default:
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008523 ASC_DBG(1, "adv_build_req ASC_ERROR\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008524 ASC_STATS(scp->device->host, build_error);
8525 return ASC_ERROR;
8526 }
8527
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008528 ret = AdvExeScsiQueue(adv_dvc, adv_reqp);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008529 err_code = adv_dvc->err_code;
8530 }
8531
8532 switch (ret) {
8533 case ASC_NOERROR:
8534 ASC_STATS(scp->device->host, exe_noerror);
8535 /*
8536 * Increment monotonically increasing per device
8537 * successful request counter. Wrapping doesn't matter.
8538 */
8539 boardp->reqcnt[scp->device->id]++;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008540 ASC_DBG(1, "ExeScsiQueue() ASC_NOERROR\n");
Matthew Wilcox41d24932007-10-02 21:55:24 -04008541 break;
8542 case ASC_BUSY:
Hannes Reinecke4b47e462015-04-24 13:18:24 +02008543 ASC_DBG(1, "ExeScsiQueue() ASC_BUSY\n");
Matthew Wilcox41d24932007-10-02 21:55:24 -04008544 ASC_STATS(scp->device->host, exe_busy);
8545 break;
8546 case ASC_ERROR:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008547 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() ASC_ERROR, "
8548 "err_code 0x%x\n", err_code);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008549 ASC_STATS(scp->device->host, exe_error);
8550 scp->result = HOST_BYTE(DID_ERROR);
8551 break;
8552 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04008553 scmd_printk(KERN_ERR, scp, "ExeScsiQueue() unknown, "
8554 "err_code 0x%x\n", err_code);
Matthew Wilcox41d24932007-10-02 21:55:24 -04008555 ASC_STATS(scp->device->host, exe_unknown);
8556 scp->result = HOST_BYTE(DID_ERROR);
8557 break;
Matthew Wilcox51219352007-10-02 21:55:22 -04008558 }
8559
Matthew Wilcoxb352f922007-10-02 21:55:33 -04008560 ASC_DBG(1, "end\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04008561 return ret;
8562}
8563
8564/*
8565 * advansys_queuecommand() - interrupt-driven I/O entrypoint.
8566 *
8567 * This function always returns 0. Command return status is saved
8568 * in the 'scp' result field.
8569 */
8570static int
Jeff Garzikf2812332010-11-16 02:10:29 -05008571advansys_queuecommand_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
Matthew Wilcox51219352007-10-02 21:55:22 -04008572{
8573 struct Scsi_Host *shost = scp->device->host;
Matthew Wilcox51219352007-10-02 21:55:22 -04008574 int asc_res, result = 0;
8575
8576 ASC_STATS(shost, queuecommand);
8577 scp->scsi_done = done;
8578
Matthew Wilcox51219352007-10-02 21:55:22 -04008579 asc_res = asc_execute_scsi_cmnd(scp);
Matthew Wilcox51219352007-10-02 21:55:22 -04008580
8581 switch (asc_res) {
8582 case ASC_NOERROR:
8583 break;
8584 case ASC_BUSY:
8585 result = SCSI_MLQUEUE_HOST_BUSY;
8586 break;
8587 case ASC_ERROR:
8588 default:
8589 asc_scsi_done(scp);
8590 break;
8591 }
8592
8593 return result;
8594}
8595
Jeff Garzikf2812332010-11-16 02:10:29 -05008596static DEF_SCSI_QCMD(advansys_queuecommand)
8597
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008598static ushort AscGetEisaChipCfg(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008599{
8600 PortAddr eisa_cfg_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8601 (PortAddr) (ASC_EISA_CFG_IOP_MASK);
8602 return inpw(eisa_cfg_iop);
8603}
8604
8605/*
8606 * Return the BIOS address of the adapter at the specified
8607 * I/O port and with the specified bus type.
8608 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008609static unsigned short AscGetChipBiosAddress(PortAddr iop_base,
8610 unsigned short bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008611{
8612 unsigned short cfg_lsw;
8613 unsigned short bios_addr;
8614
8615 /*
8616 * The PCI BIOS is re-located by the motherboard BIOS. Because
8617 * of this the driver can not determine where a PCI BIOS is
8618 * loaded and executes.
8619 */
8620 if (bus_type & ASC_IS_PCI)
8621 return 0;
8622
8623 if ((bus_type & ASC_IS_EISA) != 0) {
8624 cfg_lsw = AscGetEisaChipCfg(iop_base);
8625 cfg_lsw &= 0x000F;
8626 bios_addr = ASC_BIOS_MIN_ADDR + cfg_lsw * ASC_BIOS_BANK_SIZE;
8627 return bios_addr;
8628 }
8629
8630 cfg_lsw = AscGetChipCfgLsw(iop_base);
8631
8632 /*
8633 * ISA PnP uses the top bit as the 32K BIOS flag
8634 */
8635 if (bus_type == ASC_IS_ISAPNP)
8636 cfg_lsw &= 0x7FFF;
8637 bios_addr = ASC_BIOS_MIN_ADDR + (cfg_lsw >> 12) * ASC_BIOS_BANK_SIZE;
8638 return bios_addr;
8639}
8640
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008641static uchar AscSetChipScsiID(PortAddr iop_base, uchar new_host_id)
Matthew Wilcox51219352007-10-02 21:55:22 -04008642{
8643 ushort cfg_lsw;
8644
8645 if (AscGetChipScsiID(iop_base) == new_host_id) {
8646 return (new_host_id);
8647 }
8648 cfg_lsw = AscGetChipCfgLsw(iop_base);
8649 cfg_lsw &= 0xF8FF;
8650 cfg_lsw |= (ushort)((new_host_id & ASC_MAX_TID) << 8);
8651 AscSetChipCfgLsw(iop_base, cfg_lsw);
8652 return (AscGetChipScsiID(iop_base));
8653}
8654
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008655static unsigned char AscGetChipScsiCtrl(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008656{
8657 unsigned char sc;
8658
8659 AscSetBank(iop_base, 1);
8660 sc = inp(iop_base + IOP_REG_SC);
8661 AscSetBank(iop_base, 0);
8662 return sc;
8663}
8664
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008665static unsigned char AscGetChipVersion(PortAddr iop_base,
8666 unsigned short bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008667{
8668 if (bus_type & ASC_IS_EISA) {
8669 PortAddr eisa_iop;
8670 unsigned char revision;
8671 eisa_iop = (PortAddr) ASC_GET_EISA_SLOT(iop_base) |
8672 (PortAddr) ASC_EISA_REV_IOP_MASK;
8673 revision = inp(eisa_iop);
8674 return ASC_CHIP_MIN_VER_EISA - 1 + revision;
8675 }
8676 return AscGetChipVerNo(iop_base);
8677}
8678
Matthew Wilcox51219352007-10-02 21:55:22 -04008679#ifdef CONFIG_ISA
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008680static void AscEnableIsaDma(uchar dma_channel)
Matthew Wilcox51219352007-10-02 21:55:22 -04008681{
8682 if (dma_channel < 4) {
8683 outp(0x000B, (ushort)(0xC0 | dma_channel));
8684 outp(0x000A, dma_channel);
8685 } else if (dma_channel < 8) {
8686 outp(0x00D6, (ushort)(0xC0 | (dma_channel - 4)));
8687 outp(0x00D4, (ushort)(dma_channel - 4));
8688 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008689}
8690#endif /* CONFIG_ISA */
8691
8692static int AscStopQueueExe(PortAddr iop_base)
8693{
8694 int count = 0;
8695
8696 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) == 0) {
8697 AscWriteLramByte(iop_base, ASCV_STOP_CODE_B,
8698 ASC_STOP_REQ_RISC_STOP);
8699 do {
8700 if (AscReadLramByte(iop_base, ASCV_STOP_CODE_B) &
8701 ASC_STOP_ACK_RISC_STOP) {
8702 return (1);
8703 }
8704 mdelay(100);
8705 } while (count++ < 20);
8706 }
8707 return (0);
8708}
8709
Hannes Reinecke95cfab62015-04-24 13:18:27 +02008710static unsigned int AscGetMaxDmaCount(ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008711{
8712 if (bus_type & ASC_IS_ISA)
8713 return ASC_MAX_ISA_DMA_COUNT;
8714 else if (bus_type & (ASC_IS_EISA | ASC_IS_VL))
8715 return ASC_MAX_VL_DMA_COUNT;
8716 return ASC_MAX_PCI_DMA_COUNT;
8717}
8718
8719#ifdef CONFIG_ISA
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008720static ushort AscGetIsaDmaChannel(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008721{
8722 ushort channel;
8723
8724 channel = AscGetChipCfgLsw(iop_base) & 0x0003;
8725 if (channel == 0x03)
8726 return (0);
8727 else if (channel == 0x00)
8728 return (7);
8729 return (channel + 4);
8730}
8731
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008732static ushort AscSetIsaDmaChannel(PortAddr iop_base, ushort dma_channel)
Matthew Wilcox51219352007-10-02 21:55:22 -04008733{
8734 ushort cfg_lsw;
8735 uchar value;
8736
8737 if ((dma_channel >= 5) && (dma_channel <= 7)) {
8738 if (dma_channel == 7)
8739 value = 0x00;
8740 else
8741 value = dma_channel - 4;
8742 cfg_lsw = AscGetChipCfgLsw(iop_base) & 0xFFFC;
8743 cfg_lsw |= value;
8744 AscSetChipCfgLsw(iop_base, cfg_lsw);
8745 return (AscGetIsaDmaChannel(iop_base));
8746 }
8747 return 0;
8748}
8749
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008750static uchar AscGetIsaDmaSpeed(PortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04008751{
8752 uchar speed_value;
8753
8754 AscSetBank(iop_base, 1);
8755 speed_value = AscReadChipDmaSpeed(iop_base);
8756 speed_value &= 0x07;
8757 AscSetBank(iop_base, 0);
8758 return speed_value;
8759}
8760
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008761static uchar AscSetIsaDmaSpeed(PortAddr iop_base, uchar speed_value)
Matthew Wilcox51219352007-10-02 21:55:22 -04008762{
8763 speed_value &= 0x07;
8764 AscSetBank(iop_base, 1);
8765 AscWriteChipDmaSpeed(iop_base, speed_value);
8766 AscSetBank(iop_base, 0);
8767 return AscGetIsaDmaSpeed(iop_base);
8768}
8769#endif /* CONFIG_ISA */
8770
Hannes Reineckef33134e2015-04-24 13:18:31 +02008771static void AscInitAscDvcVar(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04008772{
8773 int i;
8774 PortAddr iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008775 uchar chip_version;
8776
8777 iop_base = asc_dvc->iop_base;
Matthew Wilcox51219352007-10-02 21:55:22 -04008778 asc_dvc->err_code = 0;
8779 if ((asc_dvc->bus_type &
8780 (ASC_IS_ISA | ASC_IS_PCI | ASC_IS_EISA | ASC_IS_VL)) == 0) {
8781 asc_dvc->err_code |= ASC_IERR_NO_BUS_TYPE;
8782 }
8783 AscSetChipControl(iop_base, CC_HALT);
8784 AscSetChipStatus(iop_base, 0);
8785 asc_dvc->bug_fix_cntl = 0;
8786 asc_dvc->pci_fix_asyn_xfer = 0;
8787 asc_dvc->pci_fix_asyn_xfer_always = 0;
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02008788 /* asc_dvc->init_state initialized in AscInitGetConfig(). */
Matthew Wilcox51219352007-10-02 21:55:22 -04008789 asc_dvc->sdtr_done = 0;
8790 asc_dvc->cur_total_qng = 0;
Hannes Reineckeae267592015-04-24 13:18:28 +02008791 asc_dvc->is_in_int = false;
Matthew Wilcox51219352007-10-02 21:55:22 -04008792 asc_dvc->in_critical_cnt = 0;
8793 asc_dvc->last_q_shortage = 0;
8794 asc_dvc->use_tagged_qng = 0;
8795 asc_dvc->no_scam = 0;
8796 asc_dvc->unit_not_ready = 0;
8797 asc_dvc->queue_full_or_busy = 0;
8798 asc_dvc->redo_scam = 0;
8799 asc_dvc->res2 = 0;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04008800 asc_dvc->min_sdtr_index = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -04008801 asc_dvc->cfg->can_tagged_qng = 0;
8802 asc_dvc->cfg->cmd_qng_enabled = 0;
8803 asc_dvc->dvc_cntl = ASC_DEF_DVC_CNTL;
8804 asc_dvc->init_sdtr = 0;
8805 asc_dvc->max_total_qng = ASC_DEF_MAX_TOTAL_QNG;
8806 asc_dvc->scsi_reset_wait = 3;
8807 asc_dvc->start_motor = ASC_SCSI_WIDTH_BIT_SET;
8808 asc_dvc->max_dma_count = AscGetMaxDmaCount(asc_dvc->bus_type);
8809 asc_dvc->cfg->sdtr_enable = ASC_SCSI_WIDTH_BIT_SET;
8810 asc_dvc->cfg->disc_enable = ASC_SCSI_WIDTH_BIT_SET;
8811 asc_dvc->cfg->chip_scsi_id = ASC_DEF_CHIP_SCSI_ID;
Matthew Wilcox51219352007-10-02 21:55:22 -04008812 chip_version = AscGetChipVersion(iop_base, asc_dvc->bus_type);
8813 asc_dvc->cfg->chip_version = chip_version;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04008814 asc_dvc->sdtr_period_tbl = asc_syn_xfer_period;
Matthew Wilcox51219352007-10-02 21:55:22 -04008815 asc_dvc->max_sdtr_index = 7;
8816 if ((asc_dvc->bus_type & ASC_IS_PCI) &&
8817 (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3150)) {
8818 asc_dvc->bus_type = ASC_IS_PCI_ULTRA;
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04008819 asc_dvc->sdtr_period_tbl = asc_syn_ultra_xfer_period;
Matthew Wilcox51219352007-10-02 21:55:22 -04008820 asc_dvc->max_sdtr_index = 15;
8821 if (chip_version == ASC_CHIP_VER_PCI_ULTRA_3150) {
8822 AscSetExtraControl(iop_base,
8823 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
8824 } else if (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3050) {
8825 AscSetExtraControl(iop_base,
8826 (SEC_ACTIVE_NEGATE |
8827 SEC_ENABLE_FILTER));
8828 }
8829 }
8830 if (asc_dvc->bus_type == ASC_IS_PCI) {
8831 AscSetExtraControl(iop_base,
8832 (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
8833 }
8834
8835 asc_dvc->cfg->isa_dma_speed = ASC_DEF_ISA_DMA_SPEED;
8836#ifdef CONFIG_ISA
8837 if ((asc_dvc->bus_type & ASC_IS_ISA) != 0) {
8838 if (chip_version >= ASC_CHIP_MIN_VER_ISA_PNP) {
8839 AscSetChipIFC(iop_base, IFC_INIT_DEFAULT);
8840 asc_dvc->bus_type = ASC_IS_ISAPNP;
8841 }
8842 asc_dvc->cfg->isa_dma_channel =
8843 (uchar)AscGetIsaDmaChannel(iop_base);
8844 }
8845#endif /* CONFIG_ISA */
8846 for (i = 0; i <= ASC_MAX_TID; i++) {
8847 asc_dvc->cur_dvc_qng[i] = 0;
8848 asc_dvc->max_dvc_qng[i] = ASC_MAX_SCSI1_QNG;
8849 asc_dvc->scsiq_busy_head[i] = (ASC_SCSI_Q *)0L;
8850 asc_dvc->scsiq_busy_tail[i] = (ASC_SCSI_Q *)0L;
8851 asc_dvc->cfg->max_tag_qng[i] = ASC_MAX_INRAM_TAG_QNG;
8852 }
Matthew Wilcox51219352007-10-02 21:55:22 -04008853}
8854
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008855static int AscWriteEEPCmdReg(PortAddr iop_base, uchar cmd_reg)
Matthew Wilcox51219352007-10-02 21:55:22 -04008856{
8857 int retry;
8858
8859 for (retry = 0; retry < ASC_EEP_MAX_RETRY; retry++) {
8860 unsigned char read_back;
8861 AscSetChipEEPCmd(iop_base, cmd_reg);
8862 mdelay(1);
8863 read_back = AscGetChipEEPCmd(iop_base);
8864 if (read_back == cmd_reg)
8865 return 1;
8866 }
8867 return 0;
8868}
8869
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008870static void AscWaitEEPRead(void)
Matthew Wilcox51219352007-10-02 21:55:22 -04008871{
8872 mdelay(1);
8873}
8874
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008875static ushort AscReadEEPWord(PortAddr iop_base, uchar addr)
Matthew Wilcox51219352007-10-02 21:55:22 -04008876{
8877 ushort read_wval;
8878 uchar cmd_reg;
8879
8880 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
8881 AscWaitEEPRead();
8882 cmd_reg = addr | ASC_EEP_CMD_READ;
8883 AscWriteEEPCmdReg(iop_base, cmd_reg);
8884 AscWaitEEPRead();
8885 read_wval = AscGetChipEEPData(iop_base);
8886 AscWaitEEPRead();
8887 return read_wval;
8888}
8889
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008890static ushort AscGetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
8891 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04008892{
8893 ushort wval;
8894 ushort sum;
8895 ushort *wbuf;
8896 int cfg_beg;
8897 int cfg_end;
8898 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
8899 int s_addr;
8900
8901 wbuf = (ushort *)cfg_buf;
8902 sum = 0;
8903 /* Read two config words; Byte-swapping done by AscReadEEPWord(). */
8904 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
8905 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
8906 sum += *wbuf;
8907 }
8908 if (bus_type & ASC_IS_VL) {
8909 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
8910 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
8911 } else {
8912 cfg_beg = ASC_EEP_DVC_CFG_BEG;
8913 cfg_end = ASC_EEP_MAX_DVC_ADDR;
8914 }
8915 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
8916 wval = AscReadEEPWord(iop_base, (uchar)s_addr);
8917 if (s_addr <= uchar_end_in_config) {
8918 /*
8919 * Swap all char fields - must unswap bytes already swapped
8920 * by AscReadEEPWord().
8921 */
8922 *wbuf = le16_to_cpu(wval);
8923 } else {
8924 /* Don't swap word field at the end - cntl field. */
8925 *wbuf = wval;
8926 }
8927 sum += wval; /* Checksum treats all EEPROM data as words. */
8928 }
8929 /*
8930 * Read the checksum word which will be compared against 'sum'
8931 * by the caller. Word field already swapped.
8932 */
8933 *wbuf = AscReadEEPWord(iop_base, (uchar)s_addr);
8934 return sum;
8935}
8936
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008937static int AscTestExternalLram(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04008938{
8939 PortAddr iop_base;
8940 ushort q_addr;
8941 ushort saved_word;
8942 int sta;
8943
8944 iop_base = asc_dvc->iop_base;
8945 sta = 0;
8946 q_addr = ASC_QNO_TO_QADDR(241);
8947 saved_word = AscReadLramWord(iop_base, q_addr);
8948 AscSetChipLramAddr(iop_base, q_addr);
8949 AscSetChipLramData(iop_base, 0x55AA);
8950 mdelay(10);
8951 AscSetChipLramAddr(iop_base, q_addr);
8952 if (AscGetChipLramData(iop_base) == 0x55AA) {
8953 sta = 1;
8954 AscWriteLramWord(iop_base, q_addr, saved_word);
8955 }
8956 return (sta);
8957}
8958
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008959static void AscWaitEEPWrite(void)
Matthew Wilcox51219352007-10-02 21:55:22 -04008960{
8961 mdelay(20);
Matthew Wilcox51219352007-10-02 21:55:22 -04008962}
8963
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008964static int AscWriteEEPDataReg(PortAddr iop_base, ushort data_reg)
Matthew Wilcox51219352007-10-02 21:55:22 -04008965{
8966 ushort read_back;
8967 int retry;
8968
8969 retry = 0;
Hannes Reineckeae267592015-04-24 13:18:28 +02008970 while (true) {
Matthew Wilcox51219352007-10-02 21:55:22 -04008971 AscSetChipEEPData(iop_base, data_reg);
8972 mdelay(1);
8973 read_back = AscGetChipEEPData(iop_base);
8974 if (read_back == data_reg) {
8975 return (1);
8976 }
8977 if (retry++ > ASC_EEP_MAX_RETRY) {
8978 return (0);
8979 }
8980 }
8981}
8982
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008983static ushort AscWriteEEPWord(PortAddr iop_base, uchar addr, ushort word_val)
Matthew Wilcox51219352007-10-02 21:55:22 -04008984{
8985 ushort read_wval;
8986
8987 read_wval = AscReadEEPWord(iop_base, addr);
8988 if (read_wval != word_val) {
8989 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_ABLE);
8990 AscWaitEEPRead();
8991 AscWriteEEPDataReg(iop_base, word_val);
8992 AscWaitEEPRead();
8993 AscWriteEEPCmdReg(iop_base,
8994 (uchar)((uchar)ASC_EEP_CMD_WRITE | addr));
8995 AscWaitEEPWrite();
8996 AscWriteEEPCmdReg(iop_base, ASC_EEP_CMD_WRITE_DISABLE);
8997 AscWaitEEPRead();
8998 return (AscReadEEPWord(iop_base, addr));
8999 }
9000 return (read_wval);
9001}
9002
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009003static int AscSetEEPConfigOnce(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9004 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009005{
9006 int n_error;
9007 ushort *wbuf;
9008 ushort word;
9009 ushort sum;
9010 int s_addr;
9011 int cfg_beg;
9012 int cfg_end;
9013 int uchar_end_in_config = ASC_EEP_MAX_DVC_ADDR - 2;
9014
9015 wbuf = (ushort *)cfg_buf;
9016 n_error = 0;
9017 sum = 0;
9018 /* Write two config words; AscWriteEEPWord() will swap bytes. */
9019 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9020 sum += *wbuf;
9021 if (*wbuf != AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9022 n_error++;
9023 }
9024 }
9025 if (bus_type & ASC_IS_VL) {
9026 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9027 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9028 } else {
9029 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9030 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9031 }
9032 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9033 if (s_addr <= uchar_end_in_config) {
9034 /*
9035 * This is a char field. Swap char fields before they are
9036 * swapped again by AscWriteEEPWord().
9037 */
9038 word = cpu_to_le16(*wbuf);
9039 if (word !=
9040 AscWriteEEPWord(iop_base, (uchar)s_addr, word)) {
9041 n_error++;
9042 }
9043 } else {
9044 /* Don't swap word field at the end - cntl field. */
9045 if (*wbuf !=
9046 AscWriteEEPWord(iop_base, (uchar)s_addr, *wbuf)) {
9047 n_error++;
9048 }
9049 }
9050 sum += *wbuf; /* Checksum calculated from word values. */
9051 }
9052 /* Write checksum word. It will be swapped by AscWriteEEPWord(). */
9053 *wbuf = sum;
9054 if (sum != AscWriteEEPWord(iop_base, (uchar)s_addr, sum)) {
9055 n_error++;
9056 }
9057
9058 /* Read EEPROM back again. */
9059 wbuf = (ushort *)cfg_buf;
9060 /*
9061 * Read two config words; Byte-swapping done by AscReadEEPWord().
9062 */
9063 for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
9064 if (*wbuf != AscReadEEPWord(iop_base, (uchar)s_addr)) {
9065 n_error++;
9066 }
9067 }
9068 if (bus_type & ASC_IS_VL) {
9069 cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
9070 cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
9071 } else {
9072 cfg_beg = ASC_EEP_DVC_CFG_BEG;
9073 cfg_end = ASC_EEP_MAX_DVC_ADDR;
9074 }
9075 for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
9076 if (s_addr <= uchar_end_in_config) {
9077 /*
9078 * Swap all char fields. Must unswap bytes already swapped
9079 * by AscReadEEPWord().
9080 */
9081 word =
9082 le16_to_cpu(AscReadEEPWord
9083 (iop_base, (uchar)s_addr));
9084 } else {
9085 /* Don't swap word field at the end - cntl field. */
9086 word = AscReadEEPWord(iop_base, (uchar)s_addr);
9087 }
9088 if (*wbuf != word) {
9089 n_error++;
9090 }
9091 }
9092 /* Read checksum; Byte swapping not needed. */
9093 if (AscReadEEPWord(iop_base, (uchar)s_addr) != sum) {
9094 n_error++;
9095 }
9096 return n_error;
9097}
9098
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009099static int AscSetEEPConfig(PortAddr iop_base, ASCEEP_CONFIG *cfg_buf,
9100 ushort bus_type)
Matthew Wilcox51219352007-10-02 21:55:22 -04009101{
9102 int retry;
9103 int n_error;
9104
9105 retry = 0;
Hannes Reineckeae267592015-04-24 13:18:28 +02009106 while (true) {
Matthew Wilcox51219352007-10-02 21:55:22 -04009107 if ((n_error = AscSetEEPConfigOnce(iop_base, cfg_buf,
9108 bus_type)) == 0) {
9109 break;
9110 }
9111 if (++retry > ASC_EEP_MAX_RETRY) {
9112 break;
9113 }
9114 }
9115 return n_error;
9116}
9117
Hannes Reineckef33134e2015-04-24 13:18:31 +02009118static int AscInitFromEEP(ASC_DVC_VAR *asc_dvc)
Matthew Wilcox51219352007-10-02 21:55:22 -04009119{
9120 ASCEEP_CONFIG eep_config_buf;
9121 ASCEEP_CONFIG *eep_config;
9122 PortAddr iop_base;
9123 ushort chksum;
9124 ushort warn_code;
9125 ushort cfg_msw, cfg_lsw;
9126 int i;
9127 int write_eep = 0;
9128
9129 iop_base = asc_dvc->iop_base;
9130 warn_code = 0;
9131 AscWriteLramWord(iop_base, ASCV_HALTCODE_W, 0x00FE);
9132 AscStopQueueExe(iop_base);
Hannes Reineckeae267592015-04-24 13:18:28 +02009133 if ((AscStopChip(iop_base)) ||
Matthew Wilcox51219352007-10-02 21:55:22 -04009134 (AscGetChipScsiCtrl(iop_base) != 0)) {
9135 asc_dvc->init_state |= ASC_INIT_RESET_SCSI_DONE;
9136 AscResetChipAndScsiBus(asc_dvc);
9137 mdelay(asc_dvc->scsi_reset_wait * 1000); /* XXX: msleep? */
9138 }
Hannes Reinecked647c782015-04-24 13:18:26 +02009139 if (!AscIsChipHalted(iop_base)) {
Matthew Wilcox51219352007-10-02 21:55:22 -04009140 asc_dvc->err_code |= ASC_IERR_START_STOP_CHIP;
9141 return (warn_code);
9142 }
9143 AscSetPCAddr(iop_base, ASC_MCODE_START_ADDR);
9144 if (AscGetPCAddr(iop_base) != ASC_MCODE_START_ADDR) {
9145 asc_dvc->err_code |= ASC_IERR_SET_PC_ADDR;
9146 return (warn_code);
9147 }
9148 eep_config = (ASCEEP_CONFIG *)&eep_config_buf;
9149 cfg_msw = AscGetChipCfgMsw(iop_base);
9150 cfg_lsw = AscGetChipCfgLsw(iop_base);
9151 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9152 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9153 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9154 AscSetChipCfgMsw(iop_base, cfg_msw);
9155 }
9156 chksum = AscGetEEPConfig(iop_base, eep_config, asc_dvc->bus_type);
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009157 ASC_DBG(1, "chksum 0x%x\n", chksum);
Matthew Wilcox51219352007-10-02 21:55:22 -04009158 if (chksum == 0) {
9159 chksum = 0xaa55;
9160 }
9161 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9162 warn_code |= ASC_WARN_AUTO_CONFIG;
9163 if (asc_dvc->cfg->chip_version == 3) {
9164 if (eep_config->cfg_lsw != cfg_lsw) {
9165 warn_code |= ASC_WARN_EEPROM_RECOVER;
9166 eep_config->cfg_lsw =
9167 AscGetChipCfgLsw(iop_base);
9168 }
9169 if (eep_config->cfg_msw != cfg_msw) {
9170 warn_code |= ASC_WARN_EEPROM_RECOVER;
9171 eep_config->cfg_msw =
9172 AscGetChipCfgMsw(iop_base);
9173 }
9174 }
9175 }
9176 eep_config->cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9177 eep_config->cfg_lsw |= ASC_CFG0_HOST_INT_ON;
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009178 ASC_DBG(1, "eep_config->chksum 0x%x\n", eep_config->chksum);
Matthew Wilcox51219352007-10-02 21:55:22 -04009179 if (chksum != eep_config->chksum) {
9180 if (AscGetChipVersion(iop_base, asc_dvc->bus_type) ==
9181 ASC_CHIP_VER_PCI_ULTRA_3050) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -04009182 ASC_DBG(1, "chksum error ignored; EEPROM-less board\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009183 eep_config->init_sdtr = 0xFF;
9184 eep_config->disc_enable = 0xFF;
9185 eep_config->start_motor = 0xFF;
9186 eep_config->use_cmd_qng = 0;
9187 eep_config->max_total_qng = 0xF0;
9188 eep_config->max_tag_qng = 0x20;
9189 eep_config->cntl = 0xBFFF;
9190 ASC_EEP_SET_CHIP_ID(eep_config, 7);
9191 eep_config->no_scam = 0;
9192 eep_config->adapter_info[0] = 0;
9193 eep_config->adapter_info[1] = 0;
9194 eep_config->adapter_info[2] = 0;
9195 eep_config->adapter_info[3] = 0;
9196 eep_config->adapter_info[4] = 0;
9197 /* Indicate EEPROM-less board. */
9198 eep_config->adapter_info[5] = 0xBB;
9199 } else {
9200 ASC_PRINT
9201 ("AscInitFromEEP: EEPROM checksum error; Will try to re-write EEPROM.\n");
9202 write_eep = 1;
9203 warn_code |= ASC_WARN_EEPROM_CHKSUM;
9204 }
9205 }
9206 asc_dvc->cfg->sdtr_enable = eep_config->init_sdtr;
9207 asc_dvc->cfg->disc_enable = eep_config->disc_enable;
9208 asc_dvc->cfg->cmd_qng_enabled = eep_config->use_cmd_qng;
9209 asc_dvc->cfg->isa_dma_speed = ASC_EEP_GET_DMA_SPD(eep_config);
9210 asc_dvc->start_motor = eep_config->start_motor;
9211 asc_dvc->dvc_cntl = eep_config->cntl;
9212 asc_dvc->no_scam = eep_config->no_scam;
9213 asc_dvc->cfg->adapter_info[0] = eep_config->adapter_info[0];
9214 asc_dvc->cfg->adapter_info[1] = eep_config->adapter_info[1];
9215 asc_dvc->cfg->adapter_info[2] = eep_config->adapter_info[2];
9216 asc_dvc->cfg->adapter_info[3] = eep_config->adapter_info[3];
9217 asc_dvc->cfg->adapter_info[4] = eep_config->adapter_info[4];
9218 asc_dvc->cfg->adapter_info[5] = eep_config->adapter_info[5];
9219 if (!AscTestExternalLram(asc_dvc)) {
9220 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) ==
9221 ASC_IS_PCI_ULTRA)) {
9222 eep_config->max_total_qng =
9223 ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
9224 eep_config->max_tag_qng =
9225 ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG;
9226 } else {
9227 eep_config->cfg_msw |= 0x0800;
9228 cfg_msw |= 0x0800;
9229 AscSetChipCfgMsw(iop_base, cfg_msw);
9230 eep_config->max_total_qng = ASC_MAX_PCI_INRAM_TOTAL_QNG;
9231 eep_config->max_tag_qng = ASC_MAX_INRAM_TAG_QNG;
9232 }
9233 } else {
9234 }
9235 if (eep_config->max_total_qng < ASC_MIN_TOTAL_QNG) {
9236 eep_config->max_total_qng = ASC_MIN_TOTAL_QNG;
9237 }
9238 if (eep_config->max_total_qng > ASC_MAX_TOTAL_QNG) {
9239 eep_config->max_total_qng = ASC_MAX_TOTAL_QNG;
9240 }
9241 if (eep_config->max_tag_qng > eep_config->max_total_qng) {
9242 eep_config->max_tag_qng = eep_config->max_total_qng;
9243 }
9244 if (eep_config->max_tag_qng < ASC_MIN_TAG_Q_PER_DVC) {
9245 eep_config->max_tag_qng = ASC_MIN_TAG_Q_PER_DVC;
9246 }
9247 asc_dvc->max_total_qng = eep_config->max_total_qng;
9248 if ((eep_config->use_cmd_qng & eep_config->disc_enable) !=
9249 eep_config->use_cmd_qng) {
9250 eep_config->disc_enable = eep_config->use_cmd_qng;
9251 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9252 }
Matthew Wilcox51219352007-10-02 21:55:22 -04009253 ASC_EEP_SET_CHIP_ID(eep_config,
9254 ASC_EEP_GET_CHIP_ID(eep_config) & ASC_MAX_TID);
9255 asc_dvc->cfg->chip_scsi_id = ASC_EEP_GET_CHIP_ID(eep_config);
9256 if (((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) &&
9257 !(asc_dvc->dvc_cntl & ASC_CNTL_SDTR_ENABLE_ULTRA)) {
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009258 asc_dvc->min_sdtr_index = ASC_SDTR_ULTRA_PCI_10MB_INDEX;
Matthew Wilcox51219352007-10-02 21:55:22 -04009259 }
9260
9261 for (i = 0; i <= ASC_MAX_TID; i++) {
9262 asc_dvc->dos_int13_table[i] = eep_config->dos_int13_table[i];
9263 asc_dvc->cfg->max_tag_qng[i] = eep_config->max_tag_qng;
9264 asc_dvc->cfg->sdtr_period_offset[i] =
9265 (uchar)(ASC_DEF_SDTR_OFFSET |
Matthew Wilcoxafbb68c2007-10-02 21:55:36 -04009266 (asc_dvc->min_sdtr_index << 4));
Matthew Wilcox51219352007-10-02 21:55:22 -04009267 }
9268 eep_config->cfg_msw = AscGetChipCfgMsw(iop_base);
9269 if (write_eep) {
9270 if ((i = AscSetEEPConfig(iop_base, eep_config,
9271 asc_dvc->bus_type)) != 0) {
9272 ASC_PRINT1
9273 ("AscInitFromEEP: Failed to re-write EEPROM with %d errors.\n",
9274 i);
9275 } else {
9276 ASC_PRINT
9277 ("AscInitFromEEP: Successfully re-wrote EEPROM.\n");
9278 }
9279 }
9280 return (warn_code);
9281}
9282
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009283static int AscInitGetConfig(struct Scsi_Host *shost)
Matthew Wilcox51219352007-10-02 21:55:22 -04009284{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009285 struct asc_board *board = shost_priv(shost);
9286 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04009287 unsigned short warn_code = 0;
9288
9289 asc_dvc->init_state = ASC_INIT_STATE_BEG_GET_CFG;
9290 if (asc_dvc->err_code != 0)
9291 return asc_dvc->err_code;
9292
9293 if (AscFindSignature(asc_dvc->iop_base)) {
Hannes Reineckef33134e2015-04-24 13:18:31 +02009294 AscInitAscDvcVar(asc_dvc);
9295 warn_code = AscInitFromEEP(asc_dvc);
Matthew Wilcox51219352007-10-02 21:55:22 -04009296 asc_dvc->init_state |= ASC_INIT_STATE_END_GET_CFG;
9297 if (asc_dvc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
9298 asc_dvc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
9299 } else {
9300 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9301 }
9302
9303 switch (warn_code) {
9304 case 0: /* No error */
9305 break;
9306 case ASC_WARN_IO_PORT_ROTATE:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009307 shost_printk(KERN_WARNING, shost, "I/O port address "
9308 "modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009309 break;
9310 case ASC_WARN_AUTO_CONFIG:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009311 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9312 "enabled\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009313 break;
9314 case ASC_WARN_EEPROM_CHKSUM:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009315 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009316 break;
9317 case ASC_WARN_IRQ_MODIFIED:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009318 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009319 break;
9320 case ASC_WARN_CMD_QNG_CONFLICT:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009321 shost_printk(KERN_WARNING, shost, "tag queuing enabled w/o "
9322 "disconnects\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009323 break;
9324 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009325 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9326 warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -04009327 break;
9328 }
9329
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009330 if (asc_dvc->err_code != 0)
9331 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9332 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
Matthew Wilcox51219352007-10-02 21:55:22 -04009333
9334 return asc_dvc->err_code;
9335}
9336
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009337static int AscInitSetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
Matthew Wilcox51219352007-10-02 21:55:22 -04009338{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009339 struct asc_board *board = shost_priv(shost);
9340 ASC_DVC_VAR *asc_dvc = &board->dvc_var.asc_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -04009341 PortAddr iop_base = asc_dvc->iop_base;
9342 unsigned short cfg_msw;
9343 unsigned short warn_code = 0;
9344
9345 asc_dvc->init_state |= ASC_INIT_STATE_BEG_SET_CFG;
9346 if (asc_dvc->err_code != 0)
9347 return asc_dvc->err_code;
9348 if (!AscFindSignature(asc_dvc->iop_base)) {
9349 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
9350 return asc_dvc->err_code;
9351 }
9352
9353 cfg_msw = AscGetChipCfgMsw(iop_base);
9354 if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
9355 cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
9356 warn_code |= ASC_WARN_CFG_MSW_RECOVER;
9357 AscSetChipCfgMsw(iop_base, cfg_msw);
9358 }
9359 if ((asc_dvc->cfg->cmd_qng_enabled & asc_dvc->cfg->disc_enable) !=
9360 asc_dvc->cfg->cmd_qng_enabled) {
9361 asc_dvc->cfg->disc_enable = asc_dvc->cfg->cmd_qng_enabled;
9362 warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
9363 }
9364 if (AscGetChipStatus(iop_base) & CSW_AUTO_CONFIG) {
9365 warn_code |= ASC_WARN_AUTO_CONFIG;
9366 }
Matthew Wilcox51219352007-10-02 21:55:22 -04009367#ifdef CONFIG_PCI
9368 if (asc_dvc->bus_type & ASC_IS_PCI) {
9369 cfg_msw &= 0xFFC0;
9370 AscSetChipCfgMsw(iop_base, cfg_msw);
9371 if ((asc_dvc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) {
9372 } else {
9373 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
9374 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
9375 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_IF_NOT_DWB;
9376 asc_dvc->bug_fix_cntl |=
9377 ASC_BUG_FIX_ASYN_USE_SYN;
9378 }
9379 }
9380 } else
9381#endif /* CONFIG_PCI */
9382 if (asc_dvc->bus_type == ASC_IS_ISAPNP) {
9383 if (AscGetChipVersion(iop_base, asc_dvc->bus_type)
9384 == ASC_CHIP_VER_ASYN_BUG) {
9385 asc_dvc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
9386 }
9387 }
9388 if (AscSetChipScsiID(iop_base, asc_dvc->cfg->chip_scsi_id) !=
9389 asc_dvc->cfg->chip_scsi_id) {
9390 asc_dvc->err_code |= ASC_IERR_SET_SCSI_ID;
9391 }
9392#ifdef CONFIG_ISA
9393 if (asc_dvc->bus_type & ASC_IS_ISA) {
9394 AscSetIsaDmaChannel(iop_base, asc_dvc->cfg->isa_dma_channel);
9395 AscSetIsaDmaSpeed(iop_base, asc_dvc->cfg->isa_dma_speed);
9396 }
9397#endif /* CONFIG_ISA */
9398
9399 asc_dvc->init_state |= ASC_INIT_STATE_END_SET_CFG;
9400
9401 switch (warn_code) {
9402 case 0: /* No error. */
9403 break;
9404 case ASC_WARN_IO_PORT_ROTATE:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009405 shost_printk(KERN_WARNING, shost, "I/O port address "
9406 "modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009407 break;
9408 case ASC_WARN_AUTO_CONFIG:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009409 shost_printk(KERN_WARNING, shost, "I/O port increment switch "
9410 "enabled\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009411 break;
9412 case ASC_WARN_EEPROM_CHKSUM:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009413 shost_printk(KERN_WARNING, shost, "EEPROM checksum error\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009414 break;
9415 case ASC_WARN_IRQ_MODIFIED:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009416 shost_printk(KERN_WARNING, shost, "IRQ modified\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009417 break;
9418 case ASC_WARN_CMD_QNG_CONFLICT:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009419 shost_printk(KERN_WARNING, shost, "tag queuing w/o "
9420 "disconnects\n");
Matthew Wilcox51219352007-10-02 21:55:22 -04009421 break;
9422 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009423 shost_printk(KERN_WARNING, shost, "unknown warning: 0x%x\n",
9424 warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -04009425 break;
9426 }
9427
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -04009428 if (asc_dvc->err_code != 0)
9429 shost_printk(KERN_ERR, shost, "error 0x%x at init_state "
9430 "0x%x\n", asc_dvc->err_code, asc_dvc->init_state);
Matthew Wilcox51219352007-10-02 21:55:22 -04009431
9432 return asc_dvc->err_code;
9433}
9434
9435/*
9436 * EEPROM Configuration.
9437 *
9438 * All drivers should use this structure to set the default EEPROM
9439 * configuration. The BIOS now uses this structure when it is built.
9440 * Additional structure information can be found in a_condor.h where
9441 * the structure is defined.
9442 *
9443 * The *_Field_IsChar structs are needed to correct for endianness.
9444 * These values are read from the board 16 bits at a time directly
9445 * into the structs. Because some fields are char, the values will be
9446 * in the wrong order. The *_Field_IsChar tells when to flip the
9447 * bytes. Data read and written to PCI memory is automatically swapped
9448 * on big-endian platforms so char fields read as words are actually being
9449 * unswapped on big-endian platforms.
9450 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009451static ADVEEP_3550_CONFIG Default_3550_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009452 ADV_EEPROM_BIOS_ENABLE, /* cfg_lsw */
9453 0x0000, /* cfg_msw */
9454 0xFFFF, /* disc_enable */
9455 0xFFFF, /* wdtr_able */
9456 0xFFFF, /* sdtr_able */
9457 0xFFFF, /* start_motor */
9458 0xFFFF, /* tagqng_able */
9459 0xFFFF, /* bios_scan */
9460 0, /* scam_tolerant */
9461 7, /* adapter_scsi_id */
9462 0, /* bios_boot_delay */
9463 3, /* scsi_reset_delay */
9464 0, /* bios_id_lun */
9465 0, /* termination */
9466 0, /* reserved1 */
9467 0xFFE7, /* bios_ctrl */
9468 0xFFFF, /* ultra_able */
9469 0, /* reserved2 */
9470 ASC_DEF_MAX_HOST_QNG, /* max_host_qng */
9471 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9472 0, /* dvc_cntl */
9473 0, /* bug_fix */
9474 0, /* serial_number_word1 */
9475 0, /* serial_number_word2 */
9476 0, /* serial_number_word3 */
9477 0, /* check_sum */
9478 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9479 , /* oem_name[16] */
9480 0, /* dvc_err_code */
9481 0, /* adv_err_code */
9482 0, /* adv_err_addr */
9483 0, /* saved_dvc_err_code */
9484 0, /* saved_adv_err_code */
9485 0, /* saved_adv_err_addr */
9486 0 /* num_of_err */
9487};
9488
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009489static ADVEEP_3550_CONFIG ADVEEP_3550_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009490 0, /* cfg_lsw */
9491 0, /* cfg_msw */
9492 0, /* -disc_enable */
9493 0, /* wdtr_able */
9494 0, /* sdtr_able */
9495 0, /* start_motor */
9496 0, /* tagqng_able */
9497 0, /* bios_scan */
9498 0, /* scam_tolerant */
9499 1, /* adapter_scsi_id */
9500 1, /* bios_boot_delay */
9501 1, /* scsi_reset_delay */
9502 1, /* bios_id_lun */
9503 1, /* termination */
9504 1, /* reserved1 */
9505 0, /* bios_ctrl */
9506 0, /* ultra_able */
9507 0, /* reserved2 */
9508 1, /* max_host_qng */
9509 1, /* max_dvc_qng */
9510 0, /* dvc_cntl */
9511 0, /* bug_fix */
9512 0, /* serial_number_word1 */
9513 0, /* serial_number_word2 */
9514 0, /* serial_number_word3 */
9515 0, /* check_sum */
9516 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9517 , /* oem_name[16] */
9518 0, /* dvc_err_code */
9519 0, /* adv_err_code */
9520 0, /* adv_err_addr */
9521 0, /* saved_dvc_err_code */
9522 0, /* saved_adv_err_code */
9523 0, /* saved_adv_err_addr */
9524 0 /* num_of_err */
9525};
9526
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009527static ADVEEP_38C0800_CONFIG Default_38C0800_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009528 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9529 0x0000, /* 01 cfg_msw */
9530 0xFFFF, /* 02 disc_enable */
9531 0xFFFF, /* 03 wdtr_able */
9532 0x4444, /* 04 sdtr_speed1 */
9533 0xFFFF, /* 05 start_motor */
9534 0xFFFF, /* 06 tagqng_able */
9535 0xFFFF, /* 07 bios_scan */
9536 0, /* 08 scam_tolerant */
9537 7, /* 09 adapter_scsi_id */
9538 0, /* bios_boot_delay */
9539 3, /* 10 scsi_reset_delay */
9540 0, /* bios_id_lun */
9541 0, /* 11 termination_se */
9542 0, /* termination_lvd */
9543 0xFFE7, /* 12 bios_ctrl */
9544 0x4444, /* 13 sdtr_speed2 */
9545 0x4444, /* 14 sdtr_speed3 */
9546 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9547 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9548 0, /* 16 dvc_cntl */
9549 0x4444, /* 17 sdtr_speed4 */
9550 0, /* 18 serial_number_word1 */
9551 0, /* 19 serial_number_word2 */
9552 0, /* 20 serial_number_word3 */
9553 0, /* 21 check_sum */
9554 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9555 , /* 22-29 oem_name[16] */
9556 0, /* 30 dvc_err_code */
9557 0, /* 31 adv_err_code */
9558 0, /* 32 adv_err_addr */
9559 0, /* 33 saved_dvc_err_code */
9560 0, /* 34 saved_adv_err_code */
9561 0, /* 35 saved_adv_err_addr */
9562 0, /* 36 reserved */
9563 0, /* 37 reserved */
9564 0, /* 38 reserved */
9565 0, /* 39 reserved */
9566 0, /* 40 reserved */
9567 0, /* 41 reserved */
9568 0, /* 42 reserved */
9569 0, /* 43 reserved */
9570 0, /* 44 reserved */
9571 0, /* 45 reserved */
9572 0, /* 46 reserved */
9573 0, /* 47 reserved */
9574 0, /* 48 reserved */
9575 0, /* 49 reserved */
9576 0, /* 50 reserved */
9577 0, /* 51 reserved */
9578 0, /* 52 reserved */
9579 0, /* 53 reserved */
9580 0, /* 54 reserved */
9581 0, /* 55 reserved */
9582 0, /* 56 cisptr_lsw */
9583 0, /* 57 cisprt_msw */
9584 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9585 PCI_DEVICE_ID_38C0800_REV1, /* 59 subsysid */
9586 0, /* 60 reserved */
9587 0, /* 61 reserved */
9588 0, /* 62 reserved */
9589 0 /* 63 reserved */
9590};
9591
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009592static ADVEEP_38C0800_CONFIG ADVEEP_38C0800_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009593 0, /* 00 cfg_lsw */
9594 0, /* 01 cfg_msw */
9595 0, /* 02 disc_enable */
9596 0, /* 03 wdtr_able */
9597 0, /* 04 sdtr_speed1 */
9598 0, /* 05 start_motor */
9599 0, /* 06 tagqng_able */
9600 0, /* 07 bios_scan */
9601 0, /* 08 scam_tolerant */
9602 1, /* 09 adapter_scsi_id */
9603 1, /* bios_boot_delay */
9604 1, /* 10 scsi_reset_delay */
9605 1, /* bios_id_lun */
9606 1, /* 11 termination_se */
9607 1, /* termination_lvd */
9608 0, /* 12 bios_ctrl */
9609 0, /* 13 sdtr_speed2 */
9610 0, /* 14 sdtr_speed3 */
9611 1, /* 15 max_host_qng */
9612 1, /* max_dvc_qng */
9613 0, /* 16 dvc_cntl */
9614 0, /* 17 sdtr_speed4 */
9615 0, /* 18 serial_number_word1 */
9616 0, /* 19 serial_number_word2 */
9617 0, /* 20 serial_number_word3 */
9618 0, /* 21 check_sum */
9619 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9620 , /* 22-29 oem_name[16] */
9621 0, /* 30 dvc_err_code */
9622 0, /* 31 adv_err_code */
9623 0, /* 32 adv_err_addr */
9624 0, /* 33 saved_dvc_err_code */
9625 0, /* 34 saved_adv_err_code */
9626 0, /* 35 saved_adv_err_addr */
9627 0, /* 36 reserved */
9628 0, /* 37 reserved */
9629 0, /* 38 reserved */
9630 0, /* 39 reserved */
9631 0, /* 40 reserved */
9632 0, /* 41 reserved */
9633 0, /* 42 reserved */
9634 0, /* 43 reserved */
9635 0, /* 44 reserved */
9636 0, /* 45 reserved */
9637 0, /* 46 reserved */
9638 0, /* 47 reserved */
9639 0, /* 48 reserved */
9640 0, /* 49 reserved */
9641 0, /* 50 reserved */
9642 0, /* 51 reserved */
9643 0, /* 52 reserved */
9644 0, /* 53 reserved */
9645 0, /* 54 reserved */
9646 0, /* 55 reserved */
9647 0, /* 56 cisptr_lsw */
9648 0, /* 57 cisprt_msw */
9649 0, /* 58 subsysvid */
9650 0, /* 59 subsysid */
9651 0, /* 60 reserved */
9652 0, /* 61 reserved */
9653 0, /* 62 reserved */
9654 0 /* 63 reserved */
9655};
9656
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009657static ADVEEP_38C1600_CONFIG Default_38C1600_EEPROM_Config = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009658 ADV_EEPROM_BIOS_ENABLE, /* 00 cfg_lsw */
9659 0x0000, /* 01 cfg_msw */
9660 0xFFFF, /* 02 disc_enable */
9661 0xFFFF, /* 03 wdtr_able */
9662 0x5555, /* 04 sdtr_speed1 */
9663 0xFFFF, /* 05 start_motor */
9664 0xFFFF, /* 06 tagqng_able */
9665 0xFFFF, /* 07 bios_scan */
9666 0, /* 08 scam_tolerant */
9667 7, /* 09 adapter_scsi_id */
9668 0, /* bios_boot_delay */
9669 3, /* 10 scsi_reset_delay */
9670 0, /* bios_id_lun */
9671 0, /* 11 termination_se */
9672 0, /* termination_lvd */
9673 0xFFE7, /* 12 bios_ctrl */
9674 0x5555, /* 13 sdtr_speed2 */
9675 0x5555, /* 14 sdtr_speed3 */
9676 ASC_DEF_MAX_HOST_QNG, /* 15 max_host_qng */
9677 ASC_DEF_MAX_DVC_QNG, /* max_dvc_qng */
9678 0, /* 16 dvc_cntl */
9679 0x5555, /* 17 sdtr_speed4 */
9680 0, /* 18 serial_number_word1 */
9681 0, /* 19 serial_number_word2 */
9682 0, /* 20 serial_number_word3 */
9683 0, /* 21 check_sum */
9684 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
9685 , /* 22-29 oem_name[16] */
9686 0, /* 30 dvc_err_code */
9687 0, /* 31 adv_err_code */
9688 0, /* 32 adv_err_addr */
9689 0, /* 33 saved_dvc_err_code */
9690 0, /* 34 saved_adv_err_code */
9691 0, /* 35 saved_adv_err_addr */
9692 0, /* 36 reserved */
9693 0, /* 37 reserved */
9694 0, /* 38 reserved */
9695 0, /* 39 reserved */
9696 0, /* 40 reserved */
9697 0, /* 41 reserved */
9698 0, /* 42 reserved */
9699 0, /* 43 reserved */
9700 0, /* 44 reserved */
9701 0, /* 45 reserved */
9702 0, /* 46 reserved */
9703 0, /* 47 reserved */
9704 0, /* 48 reserved */
9705 0, /* 49 reserved */
9706 0, /* 50 reserved */
9707 0, /* 51 reserved */
9708 0, /* 52 reserved */
9709 0, /* 53 reserved */
9710 0, /* 54 reserved */
9711 0, /* 55 reserved */
9712 0, /* 56 cisptr_lsw */
9713 0, /* 57 cisprt_msw */
9714 PCI_VENDOR_ID_ASP, /* 58 subsysvid */
9715 PCI_DEVICE_ID_38C1600_REV1, /* 59 subsysid */
9716 0, /* 60 reserved */
9717 0, /* 61 reserved */
9718 0, /* 62 reserved */
9719 0 /* 63 reserved */
9720};
9721
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009722static ADVEEP_38C1600_CONFIG ADVEEP_38C1600_Config_Field_IsChar = {
Matthew Wilcox51219352007-10-02 21:55:22 -04009723 0, /* 00 cfg_lsw */
9724 0, /* 01 cfg_msw */
9725 0, /* 02 disc_enable */
9726 0, /* 03 wdtr_able */
9727 0, /* 04 sdtr_speed1 */
9728 0, /* 05 start_motor */
9729 0, /* 06 tagqng_able */
9730 0, /* 07 bios_scan */
9731 0, /* 08 scam_tolerant */
9732 1, /* 09 adapter_scsi_id */
9733 1, /* bios_boot_delay */
9734 1, /* 10 scsi_reset_delay */
9735 1, /* bios_id_lun */
9736 1, /* 11 termination_se */
9737 1, /* termination_lvd */
9738 0, /* 12 bios_ctrl */
9739 0, /* 13 sdtr_speed2 */
9740 0, /* 14 sdtr_speed3 */
9741 1, /* 15 max_host_qng */
9742 1, /* max_dvc_qng */
9743 0, /* 16 dvc_cntl */
9744 0, /* 17 sdtr_speed4 */
9745 0, /* 18 serial_number_word1 */
9746 0, /* 19 serial_number_word2 */
9747 0, /* 20 serial_number_word3 */
9748 0, /* 21 check_sum */
9749 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
9750 , /* 22-29 oem_name[16] */
9751 0, /* 30 dvc_err_code */
9752 0, /* 31 adv_err_code */
9753 0, /* 32 adv_err_addr */
9754 0, /* 33 saved_dvc_err_code */
9755 0, /* 34 saved_adv_err_code */
9756 0, /* 35 saved_adv_err_addr */
9757 0, /* 36 reserved */
9758 0, /* 37 reserved */
9759 0, /* 38 reserved */
9760 0, /* 39 reserved */
9761 0, /* 40 reserved */
9762 0, /* 41 reserved */
9763 0, /* 42 reserved */
9764 0, /* 43 reserved */
9765 0, /* 44 reserved */
9766 0, /* 45 reserved */
9767 0, /* 46 reserved */
9768 0, /* 47 reserved */
9769 0, /* 48 reserved */
9770 0, /* 49 reserved */
9771 0, /* 50 reserved */
9772 0, /* 51 reserved */
9773 0, /* 52 reserved */
9774 0, /* 53 reserved */
9775 0, /* 54 reserved */
9776 0, /* 55 reserved */
9777 0, /* 56 cisptr_lsw */
9778 0, /* 57 cisprt_msw */
9779 0, /* 58 subsysvid */
9780 0, /* 59 subsysid */
9781 0, /* 60 reserved */
9782 0, /* 61 reserved */
9783 0, /* 62 reserved */
9784 0 /* 63 reserved */
9785};
9786
9787#ifdef CONFIG_PCI
9788/*
9789 * Wait for EEPROM command to complete
9790 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009791static void AdvWaitEEPCmd(AdvPortAddr iop_base)
Matthew Wilcox51219352007-10-02 21:55:22 -04009792{
9793 int eep_delay_ms;
9794
9795 for (eep_delay_ms = 0; eep_delay_ms < ADV_EEP_DELAY_MS; eep_delay_ms++) {
9796 if (AdvReadWordRegister(iop_base, IOPW_EE_CMD) &
9797 ASC_EEP_CMD_DONE) {
9798 break;
9799 }
9800 mdelay(1);
9801 }
9802 if ((AdvReadWordRegister(iop_base, IOPW_EE_CMD) & ASC_EEP_CMD_DONE) ==
9803 0)
9804 BUG();
9805}
9806
9807/*
9808 * Read the EEPROM from specified location
9809 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009810static ushort AdvReadEEPWord(AdvPortAddr iop_base, int eep_word_addr)
Matthew Wilcox51219352007-10-02 21:55:22 -04009811{
9812 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9813 ASC_EEP_CMD_READ | eep_word_addr);
9814 AdvWaitEEPCmd(iop_base);
9815 return AdvReadWordRegister(iop_base, IOPW_EE_DATA);
9816}
9817
9818/*
9819 * Write the EEPROM from 'cfg_buf'.
9820 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009821static void AdvSet3550EEPConfig(AdvPortAddr iop_base,
9822 ADVEEP_3550_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -04009823{
9824 ushort *wbuf;
9825 ushort addr, chksum;
9826 ushort *charfields;
9827
9828 wbuf = (ushort *)cfg_buf;
9829 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
9830 chksum = 0;
9831
9832 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
9833 AdvWaitEEPCmd(iop_base);
9834
9835 /*
9836 * Write EEPROM from word 0 to word 20.
9837 */
9838 for (addr = ADV_EEP_DVC_CFG_BEGIN;
9839 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
9840 ushort word;
9841
9842 if (*charfields++) {
9843 word = cpu_to_le16(*wbuf);
9844 } else {
9845 word = *wbuf;
9846 }
9847 chksum += *wbuf; /* Checksum is calculated from word values. */
9848 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9849 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9850 ASC_EEP_CMD_WRITE | addr);
9851 AdvWaitEEPCmd(iop_base);
9852 mdelay(ADV_EEP_DELAY_MS);
9853 }
9854
9855 /*
9856 * Write EEPROM checksum at word 21.
9857 */
9858 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
9859 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
9860 AdvWaitEEPCmd(iop_base);
9861 wbuf++;
9862 charfields++;
9863
9864 /*
9865 * Write EEPROM OEM name at words 22 to 29.
9866 */
9867 for (addr = ADV_EEP_DVC_CTL_BEGIN;
9868 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
9869 ushort word;
9870
9871 if (*charfields++) {
9872 word = cpu_to_le16(*wbuf);
9873 } else {
9874 word = *wbuf;
9875 }
9876 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9877 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9878 ASC_EEP_CMD_WRITE | addr);
9879 AdvWaitEEPCmd(iop_base);
9880 }
9881 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
9882 AdvWaitEEPCmd(iop_base);
9883}
9884
9885/*
9886 * Write the EEPROM from 'cfg_buf'.
9887 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009888static void AdvSet38C0800EEPConfig(AdvPortAddr iop_base,
9889 ADVEEP_38C0800_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -04009890{
9891 ushort *wbuf;
9892 ushort *charfields;
9893 ushort addr, chksum;
9894
9895 wbuf = (ushort *)cfg_buf;
9896 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
9897 chksum = 0;
9898
9899 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
9900 AdvWaitEEPCmd(iop_base);
9901
9902 /*
9903 * Write EEPROM from word 0 to word 20.
9904 */
9905 for (addr = ADV_EEP_DVC_CFG_BEGIN;
9906 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
9907 ushort word;
9908
9909 if (*charfields++) {
9910 word = cpu_to_le16(*wbuf);
9911 } else {
9912 word = *wbuf;
9913 }
9914 chksum += *wbuf; /* Checksum is calculated from word values. */
9915 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9916 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9917 ASC_EEP_CMD_WRITE | addr);
9918 AdvWaitEEPCmd(iop_base);
9919 mdelay(ADV_EEP_DELAY_MS);
9920 }
9921
9922 /*
9923 * Write EEPROM checksum at word 21.
9924 */
9925 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
9926 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
9927 AdvWaitEEPCmd(iop_base);
9928 wbuf++;
9929 charfields++;
9930
9931 /*
9932 * Write EEPROM OEM name at words 22 to 29.
9933 */
9934 for (addr = ADV_EEP_DVC_CTL_BEGIN;
9935 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
9936 ushort word;
9937
9938 if (*charfields++) {
9939 word = cpu_to_le16(*wbuf);
9940 } else {
9941 word = *wbuf;
9942 }
9943 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9944 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9945 ASC_EEP_CMD_WRITE | addr);
9946 AdvWaitEEPCmd(iop_base);
9947 }
9948 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
9949 AdvWaitEEPCmd(iop_base);
9950}
9951
9952/*
9953 * Write the EEPROM from 'cfg_buf'.
9954 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009955static void AdvSet38C1600EEPConfig(AdvPortAddr iop_base,
9956 ADVEEP_38C1600_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -04009957{
9958 ushort *wbuf;
9959 ushort *charfields;
9960 ushort addr, chksum;
9961
9962 wbuf = (ushort *)cfg_buf;
9963 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
9964 chksum = 0;
9965
9966 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
9967 AdvWaitEEPCmd(iop_base);
9968
9969 /*
9970 * Write EEPROM from word 0 to word 20.
9971 */
9972 for (addr = ADV_EEP_DVC_CFG_BEGIN;
9973 addr < ADV_EEP_DVC_CFG_END; addr++, wbuf++) {
9974 ushort word;
9975
9976 if (*charfields++) {
9977 word = cpu_to_le16(*wbuf);
9978 } else {
9979 word = *wbuf;
9980 }
9981 chksum += *wbuf; /* Checksum is calculated from word values. */
9982 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
9983 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
9984 ASC_EEP_CMD_WRITE | addr);
9985 AdvWaitEEPCmd(iop_base);
9986 mdelay(ADV_EEP_DELAY_MS);
9987 }
9988
9989 /*
9990 * Write EEPROM checksum at word 21.
9991 */
9992 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, chksum);
9993 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE | addr);
9994 AdvWaitEEPCmd(iop_base);
9995 wbuf++;
9996 charfields++;
9997
9998 /*
9999 * Write EEPROM OEM name at words 22 to 29.
10000 */
10001 for (addr = ADV_EEP_DVC_CTL_BEGIN;
10002 addr < ADV_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
10003 ushort word;
10004
10005 if (*charfields++) {
10006 word = cpu_to_le16(*wbuf);
10007 } else {
10008 word = *wbuf;
10009 }
10010 AdvWriteWordRegister(iop_base, IOPW_EE_DATA, word);
10011 AdvWriteWordRegister(iop_base, IOPW_EE_CMD,
10012 ASC_EEP_CMD_WRITE | addr);
10013 AdvWaitEEPCmd(iop_base);
10014 }
10015 AdvWriteWordRegister(iop_base, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_DISABLE);
10016 AdvWaitEEPCmd(iop_base);
10017}
10018
10019/*
10020 * Read EEPROM configuration into the specified buffer.
10021 *
10022 * Return a checksum based on the EEPROM configuration read.
10023 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010024static ushort AdvGet3550EEPConfig(AdvPortAddr iop_base,
10025 ADVEEP_3550_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010026{
10027 ushort wval, chksum;
10028 ushort *wbuf;
10029 int eep_addr;
10030 ushort *charfields;
10031
10032 charfields = (ushort *)&ADVEEP_3550_Config_Field_IsChar;
10033 wbuf = (ushort *)cfg_buf;
10034 chksum = 0;
10035
10036 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10037 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10038 wval = AdvReadEEPWord(iop_base, eep_addr);
10039 chksum += wval; /* Checksum is calculated from word values. */
10040 if (*charfields++) {
10041 *wbuf = le16_to_cpu(wval);
10042 } else {
10043 *wbuf = wval;
10044 }
10045 }
10046 /* Read checksum word. */
10047 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10048 wbuf++;
10049 charfields++;
10050
10051 /* Read rest of EEPROM not covered by the checksum. */
10052 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10053 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10054 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10055 if (*charfields++) {
10056 *wbuf = le16_to_cpu(*wbuf);
10057 }
10058 }
10059 return chksum;
10060}
10061
10062/*
10063 * Read EEPROM configuration into the specified buffer.
10064 *
10065 * Return a checksum based on the EEPROM configuration read.
10066 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010067static ushort AdvGet38C0800EEPConfig(AdvPortAddr iop_base,
10068 ADVEEP_38C0800_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010069{
10070 ushort wval, chksum;
10071 ushort *wbuf;
10072 int eep_addr;
10073 ushort *charfields;
10074
10075 charfields = (ushort *)&ADVEEP_38C0800_Config_Field_IsChar;
10076 wbuf = (ushort *)cfg_buf;
10077 chksum = 0;
10078
10079 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10080 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10081 wval = AdvReadEEPWord(iop_base, eep_addr);
10082 chksum += wval; /* Checksum is calculated from word values. */
10083 if (*charfields++) {
10084 *wbuf = le16_to_cpu(wval);
10085 } else {
10086 *wbuf = wval;
10087 }
10088 }
10089 /* Read checksum word. */
10090 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10091 wbuf++;
10092 charfields++;
10093
10094 /* Read rest of EEPROM not covered by the checksum. */
10095 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10096 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10097 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10098 if (*charfields++) {
10099 *wbuf = le16_to_cpu(*wbuf);
10100 }
10101 }
10102 return chksum;
10103}
10104
10105/*
10106 * Read EEPROM configuration into the specified buffer.
10107 *
10108 * Return a checksum based on the EEPROM configuration read.
10109 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010110static ushort AdvGet38C1600EEPConfig(AdvPortAddr iop_base,
10111 ADVEEP_38C1600_CONFIG *cfg_buf)
Matthew Wilcox51219352007-10-02 21:55:22 -040010112{
10113 ushort wval, chksum;
10114 ushort *wbuf;
10115 int eep_addr;
10116 ushort *charfields;
10117
10118 charfields = (ushort *)&ADVEEP_38C1600_Config_Field_IsChar;
10119 wbuf = (ushort *)cfg_buf;
10120 chksum = 0;
10121
10122 for (eep_addr = ADV_EEP_DVC_CFG_BEGIN;
10123 eep_addr < ADV_EEP_DVC_CFG_END; eep_addr++, wbuf++) {
10124 wval = AdvReadEEPWord(iop_base, eep_addr);
10125 chksum += wval; /* Checksum is calculated from word values. */
10126 if (*charfields++) {
10127 *wbuf = le16_to_cpu(wval);
10128 } else {
10129 *wbuf = wval;
10130 }
10131 }
10132 /* Read checksum word. */
10133 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10134 wbuf++;
10135 charfields++;
10136
10137 /* Read rest of EEPROM not covered by the checksum. */
10138 for (eep_addr = ADV_EEP_DVC_CTL_BEGIN;
10139 eep_addr < ADV_EEP_MAX_WORD_ADDR; eep_addr++, wbuf++) {
10140 *wbuf = AdvReadEEPWord(iop_base, eep_addr);
10141 if (*charfields++) {
10142 *wbuf = le16_to_cpu(*wbuf);
10143 }
10144 }
10145 return chksum;
10146}
10147
10148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10150 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10151 * all of this is done.
10152 *
10153 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10154 *
10155 * For a non-fatal error return a warning code. If there are no warnings
10156 * then 0 is returned.
10157 *
10158 * Note: Chip is stopped on entry.
10159 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010160static int AdvInitFrom3550EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010162 AdvPortAddr iop_base;
10163 ushort warn_code;
10164 ADVEEP_3550_CONFIG eep_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010166 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010168 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010170 /*
10171 * Read the board's EEPROM configuration.
10172 *
10173 * Set default values if a bad checksum is found.
10174 */
10175 if (AdvGet3550EEPConfig(iop_base, &eep_config) != eep_config.check_sum) {
10176 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010178 /*
10179 * Set EEPROM default values.
10180 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010181 memcpy(&eep_config, &Default_3550_EEPROM_Config,
10182 sizeof(ADVEEP_3550_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010184 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010185 * Assume the 6 byte board serial number that was read from
10186 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010187 */
10188 eep_config.serial_number_word3 =
10189 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010191 eep_config.serial_number_word2 =
10192 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010194 eep_config.serial_number_word1 =
10195 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010196
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010197 AdvSet3550EEPConfig(iop_base, &eep_config);
10198 }
10199 /*
10200 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10201 * EEPROM configuration that was read.
10202 *
10203 * This is the mapping of EEPROM fields to Adv Library fields.
10204 */
10205 asc_dvc->wdtr_able = eep_config.wdtr_able;
10206 asc_dvc->sdtr_able = eep_config.sdtr_able;
10207 asc_dvc->ultra_able = eep_config.ultra_able;
10208 asc_dvc->tagqng_able = eep_config.tagqng_able;
10209 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10210 asc_dvc->max_host_qng = eep_config.max_host_qng;
10211 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10212 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10213 asc_dvc->start_motor = eep_config.start_motor;
10214 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10215 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10216 asc_dvc->no_scam = eep_config.scam_tolerant;
10217 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10218 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10219 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010221 /*
10222 * Set the host maximum queuing (max. 253, min. 16) and the per device
10223 * maximum queuing (max. 63, min. 4).
10224 */
10225 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10226 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10227 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10228 /* If the value is zero, assume it is uninitialized. */
10229 if (eep_config.max_host_qng == 0) {
10230 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10231 } else {
10232 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10233 }
10234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010236 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10237 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10238 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10239 /* If the value is zero, assume it is uninitialized. */
10240 if (eep_config.max_dvc_qng == 0) {
10241 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10242 } else {
10243 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10244 }
10245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010246
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010247 /*
10248 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10249 * set 'max_dvc_qng' to 'max_host_qng'.
10250 */
10251 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10252 eep_config.max_dvc_qng = eep_config.max_host_qng;
10253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010255 /*
10256 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10257 * values based on possibly adjusted EEPROM values.
10258 */
10259 asc_dvc->max_host_qng = eep_config.max_host_qng;
10260 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010261
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010262 /*
10263 * If the EEPROM 'termination' field is set to automatic (0), then set
10264 * the ADV_DVC_CFG 'termination' field to automatic also.
10265 *
10266 * If the termination is specified with a non-zero 'termination'
10267 * value check that a legal value is set and set the ADV_DVC_CFG
10268 * 'termination' field appropriately.
10269 */
10270 if (eep_config.termination == 0) {
10271 asc_dvc->cfg->termination = 0; /* auto termination */
10272 } else {
10273 /* Enable manual control with low off / high off. */
10274 if (eep_config.termination == 1) {
10275 asc_dvc->cfg->termination = TERM_CTL_SEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010277 /* Enable manual control with low off / high on. */
10278 } else if (eep_config.termination == 2) {
10279 asc_dvc->cfg->termination = TERM_CTL_SEL | TERM_CTL_H;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010280
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010281 /* Enable manual control with low on / high on. */
10282 } else if (eep_config.termination == 3) {
10283 asc_dvc->cfg->termination =
10284 TERM_CTL_SEL | TERM_CTL_H | TERM_CTL_L;
10285 } else {
10286 /*
10287 * The EEPROM 'termination' field contains a bad value. Use
10288 * automatic termination instead.
10289 */
10290 asc_dvc->cfg->termination = 0;
10291 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10292 }
10293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010295 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296}
10297
10298/*
10299 * Read the board's EEPROM configuration. Set fields in ADV_DVC_VAR and
10300 * ADV_DVC_CFG based on the EEPROM settings. The chip is stopped while
10301 * all of this is done.
10302 *
10303 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
10304 *
10305 * For a non-fatal error return a warning code. If there are no warnings
10306 * then 0 is returned.
10307 *
10308 * Note: Chip is stopped on entry.
10309 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010310static int AdvInitFrom38C0800EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010311{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010312 AdvPortAddr iop_base;
10313 ushort warn_code;
10314 ADVEEP_38C0800_CONFIG eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010315 uchar tid, termination;
10316 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010318 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010320 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010322 /*
10323 * Read the board's EEPROM configuration.
10324 *
10325 * Set default values if a bad checksum is found.
10326 */
10327 if (AdvGet38C0800EEPConfig(iop_base, &eep_config) !=
10328 eep_config.check_sum) {
10329 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010331 /*
10332 * Set EEPROM default values.
10333 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010334 memcpy(&eep_config, &Default_38C0800_EEPROM_Config,
10335 sizeof(ADVEEP_38C0800_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010336
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010337 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010338 * Assume the 6 byte board serial number that was read from
10339 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010340 */
10341 eep_config.serial_number_word3 =
10342 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010343
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010344 eep_config.serial_number_word2 =
10345 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010346
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010347 eep_config.serial_number_word1 =
10348 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010350 AdvSet38C0800EEPConfig(iop_base, &eep_config);
10351 }
10352 /*
10353 * Set ADV_DVC_VAR and ADV_DVC_CFG variables from the
10354 * EEPROM configuration that was read.
10355 *
10356 * This is the mapping of EEPROM fields to Adv Library fields.
10357 */
10358 asc_dvc->wdtr_able = eep_config.wdtr_able;
10359 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10360 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10361 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10362 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10363 asc_dvc->tagqng_able = eep_config.tagqng_able;
10364 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10365 asc_dvc->max_host_qng = eep_config.max_host_qng;
10366 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10367 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ADV_MAX_TID);
10368 asc_dvc->start_motor = eep_config.start_motor;
10369 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10370 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10371 asc_dvc->no_scam = eep_config.scam_tolerant;
10372 asc_dvc->cfg->serial1 = eep_config.serial_number_word1;
10373 asc_dvc->cfg->serial2 = eep_config.serial_number_word2;
10374 asc_dvc->cfg->serial3 = eep_config.serial_number_word3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010376 /*
10377 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10378 * are set, then set an 'sdtr_able' bit for it.
10379 */
10380 asc_dvc->sdtr_able = 0;
10381 for (tid = 0; tid <= ADV_MAX_TID; tid++) {
10382 if (tid == 0) {
10383 sdtr_speed = asc_dvc->sdtr_speed1;
10384 } else if (tid == 4) {
10385 sdtr_speed = asc_dvc->sdtr_speed2;
10386 } else if (tid == 8) {
10387 sdtr_speed = asc_dvc->sdtr_speed3;
10388 } else if (tid == 12) {
10389 sdtr_speed = asc_dvc->sdtr_speed4;
10390 }
10391 if (sdtr_speed & ADV_MAX_TID) {
10392 asc_dvc->sdtr_able |= (1 << tid);
10393 }
10394 sdtr_speed >>= 4;
10395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010397 /*
10398 * Set the host maximum queuing (max. 253, min. 16) and the per device
10399 * maximum queuing (max. 63, min. 4).
10400 */
10401 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10402 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10403 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10404 /* If the value is zero, assume it is uninitialized. */
10405 if (eep_config.max_host_qng == 0) {
10406 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10407 } else {
10408 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10409 }
10410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010412 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10413 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10414 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10415 /* If the value is zero, assume it is uninitialized. */
10416 if (eep_config.max_dvc_qng == 0) {
10417 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10418 } else {
10419 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10420 }
10421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010422
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010423 /*
10424 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10425 * set 'max_dvc_qng' to 'max_host_qng'.
10426 */
10427 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10428 eep_config.max_dvc_qng = eep_config.max_host_qng;
10429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010431 /*
10432 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
10433 * values based on possibly adjusted EEPROM values.
10434 */
10435 asc_dvc->max_host_qng = eep_config.max_host_qng;
10436 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010438 /*
10439 * If the EEPROM 'termination' field is set to automatic (0), then set
10440 * the ADV_DVC_CFG 'termination' field to automatic also.
10441 *
10442 * If the termination is specified with a non-zero 'termination'
10443 * value check that a legal value is set and set the ADV_DVC_CFG
10444 * 'termination' field appropriately.
10445 */
10446 if (eep_config.termination_se == 0) {
10447 termination = 0; /* auto termination for SE */
10448 } else {
10449 /* Enable manual control with low off / high off. */
10450 if (eep_config.termination_se == 1) {
10451 termination = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010453 /* Enable manual control with low off / high on. */
10454 } else if (eep_config.termination_se == 2) {
10455 termination = TERM_SE_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010456
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010457 /* Enable manual control with low on / high on. */
10458 } else if (eep_config.termination_se == 3) {
10459 termination = TERM_SE;
10460 } else {
10461 /*
10462 * The EEPROM 'termination_se' field contains a bad value.
10463 * Use automatic termination instead.
10464 */
10465 termination = 0;
10466 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10467 }
10468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010469
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010470 if (eep_config.termination_lvd == 0) {
10471 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10472 } else {
10473 /* Enable manual control with low off / high off. */
10474 if (eep_config.termination_lvd == 1) {
10475 asc_dvc->cfg->termination = termination;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010477 /* Enable manual control with low off / high on. */
10478 } else if (eep_config.termination_lvd == 2) {
10479 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010481 /* Enable manual control with low on / high on. */
10482 } else if (eep_config.termination_lvd == 3) {
10483 asc_dvc->cfg->termination = termination | TERM_LVD;
10484 } else {
10485 /*
10486 * The EEPROM 'termination_lvd' field contains a bad value.
10487 * Use automatic termination instead.
10488 */
10489 asc_dvc->cfg->termination = termination;
10490 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10491 }
10492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010494 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010495}
10496
10497/*
10498 * Read the board's EEPROM configuration. Set fields in ASC_DVC_VAR and
10499 * ASC_DVC_CFG based on the EEPROM settings. The chip is stopped while
10500 * all of this is done.
10501 *
10502 * On failure set the ASC_DVC_VAR field 'err_code' and return ADV_ERROR.
10503 *
10504 * For a non-fatal error return a warning code. If there are no warnings
10505 * then 0 is returned.
10506 *
10507 * Note: Chip is stopped on entry.
10508 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010509static int AdvInitFrom38C1600EEP(ADV_DVC_VAR *asc_dvc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510{
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010511 AdvPortAddr iop_base;
10512 ushort warn_code;
10513 ADVEEP_38C1600_CONFIG eep_config;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010514 uchar tid, termination;
10515 ushort sdtr_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010516
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010517 iop_base = asc_dvc->iop_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010518
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010519 warn_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010520
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010521 /*
10522 * Read the board's EEPROM configuration.
10523 *
10524 * Set default values if a bad checksum is found.
10525 */
10526 if (AdvGet38C1600EEPConfig(iop_base, &eep_config) !=
10527 eep_config.check_sum) {
Matthew Wilcox13ac2d92007-07-30 08:10:23 -060010528 struct pci_dev *pdev = adv_dvc_to_pdev(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010529 warn_code |= ASC_WARN_EEPROM_CHKSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010531 /*
10532 * Set EEPROM default values.
10533 */
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010534 memcpy(&eep_config, &Default_38C1600_EEPROM_Config,
10535 sizeof(ADVEEP_38C1600_CONFIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010537 if (PCI_FUNC(pdev->devfn) != 0) {
10538 u8 ints;
10539 /*
10540 * Disable Bit 14 (BIOS_ENABLE) to fix SPARC Ultra 60
10541 * and old Mac system booting problem. The Expansion
10542 * ROM must be disabled in Function 1 for these systems
10543 */
10544 eep_config.cfg_lsw &= ~ADV_EEPROM_BIOS_ENABLE;
10545 /*
10546 * Clear the INTAB (bit 11) if the GPIO 0 input
10547 * indicates the Function 1 interrupt line is wired
10548 * to INTB.
10549 *
10550 * Set/Clear Bit 11 (INTAB) from the GPIO bit 0 input:
10551 * 1 - Function 1 interrupt line wired to INT A.
10552 * 0 - Function 1 interrupt line wired to INT B.
10553 *
10554 * Note: Function 0 is always wired to INTA.
10555 * Put all 5 GPIO bits in input mode and then read
10556 * their input values.
10557 */
10558 AdvWriteByteRegister(iop_base, IOPB_GPIO_CNTL, 0);
10559 ints = AdvReadByteRegister(iop_base, IOPB_GPIO_DATA);
10560 if ((ints & 0x01) == 0)
10561 eep_config.cfg_lsw &= ~ADV_EEPROM_INTAB;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010563
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010564 /*
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010565 * Assume the 6 byte board serial number that was read from
10566 * EEPROM is correct even if the EEPROM checksum failed.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010567 */
10568 eep_config.serial_number_word3 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010569 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 1);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010570 eep_config.serial_number_word2 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010571 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 2);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010572 eep_config.serial_number_word1 =
Matthew Wilcoxd68f4322007-07-26 11:58:12 -040010573 AdvReadEEPWord(iop_base, ADV_EEP_DVC_CFG_END - 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010575 AdvSet38C1600EEPConfig(iop_base, &eep_config);
10576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010577
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010578 /*
10579 * Set ASC_DVC_VAR and ASC_DVC_CFG variables from the
10580 * EEPROM configuration that was read.
10581 *
10582 * This is the mapping of EEPROM fields to Adv Library fields.
10583 */
10584 asc_dvc->wdtr_able = eep_config.wdtr_able;
10585 asc_dvc->sdtr_speed1 = eep_config.sdtr_speed1;
10586 asc_dvc->sdtr_speed2 = eep_config.sdtr_speed2;
10587 asc_dvc->sdtr_speed3 = eep_config.sdtr_speed3;
10588 asc_dvc->sdtr_speed4 = eep_config.sdtr_speed4;
10589 asc_dvc->ppr_able = 0;
10590 asc_dvc->tagqng_able = eep_config.tagqng_able;
10591 asc_dvc->cfg->disc_enable = eep_config.disc_enable;
10592 asc_dvc->max_host_qng = eep_config.max_host_qng;
10593 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
10594 asc_dvc->chip_scsi_id = (eep_config.adapter_scsi_id & ASC_MAX_TID);
10595 asc_dvc->start_motor = eep_config.start_motor;
10596 asc_dvc->scsi_reset_wait = eep_config.scsi_reset_delay;
10597 asc_dvc->bios_ctrl = eep_config.bios_ctrl;
10598 asc_dvc->no_scam = eep_config.scam_tolerant;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010599
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010600 /*
10601 * For every Target ID if any of its 'sdtr_speed[1234]' bits
10602 * are set, then set an 'sdtr_able' bit for it.
10603 */
10604 asc_dvc->sdtr_able = 0;
10605 for (tid = 0; tid <= ASC_MAX_TID; tid++) {
10606 if (tid == 0) {
10607 sdtr_speed = asc_dvc->sdtr_speed1;
10608 } else if (tid == 4) {
10609 sdtr_speed = asc_dvc->sdtr_speed2;
10610 } else if (tid == 8) {
10611 sdtr_speed = asc_dvc->sdtr_speed3;
10612 } else if (tid == 12) {
10613 sdtr_speed = asc_dvc->sdtr_speed4;
10614 }
10615 if (sdtr_speed & ASC_MAX_TID) {
10616 asc_dvc->sdtr_able |= (1 << tid);
10617 }
10618 sdtr_speed >>= 4;
10619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010620
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010621 /*
10622 * Set the host maximum queuing (max. 253, min. 16) and the per device
10623 * maximum queuing (max. 63, min. 4).
10624 */
10625 if (eep_config.max_host_qng > ASC_DEF_MAX_HOST_QNG) {
10626 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10627 } else if (eep_config.max_host_qng < ASC_DEF_MIN_HOST_QNG) {
10628 /* If the value is zero, assume it is uninitialized. */
10629 if (eep_config.max_host_qng == 0) {
10630 eep_config.max_host_qng = ASC_DEF_MAX_HOST_QNG;
10631 } else {
10632 eep_config.max_host_qng = ASC_DEF_MIN_HOST_QNG;
10633 }
10634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010635
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010636 if (eep_config.max_dvc_qng > ASC_DEF_MAX_DVC_QNG) {
10637 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10638 } else if (eep_config.max_dvc_qng < ASC_DEF_MIN_DVC_QNG) {
10639 /* If the value is zero, assume it is uninitialized. */
10640 if (eep_config.max_dvc_qng == 0) {
10641 eep_config.max_dvc_qng = ASC_DEF_MAX_DVC_QNG;
10642 } else {
10643 eep_config.max_dvc_qng = ASC_DEF_MIN_DVC_QNG;
10644 }
10645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010646
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010647 /*
10648 * If 'max_dvc_qng' is greater than 'max_host_qng', then
10649 * set 'max_dvc_qng' to 'max_host_qng'.
10650 */
10651 if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
10652 eep_config.max_dvc_qng = eep_config.max_host_qng;
10653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010654
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010655 /*
10656 * Set ASC_DVC_VAR 'max_host_qng' and ASC_DVC_VAR 'max_dvc_qng'
10657 * values based on possibly adjusted EEPROM values.
10658 */
10659 asc_dvc->max_host_qng = eep_config.max_host_qng;
10660 asc_dvc->max_dvc_qng = eep_config.max_dvc_qng;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010661
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010662 /*
10663 * If the EEPROM 'termination' field is set to automatic (0), then set
10664 * the ASC_DVC_CFG 'termination' field to automatic also.
10665 *
10666 * If the termination is specified with a non-zero 'termination'
10667 * value check that a legal value is set and set the ASC_DVC_CFG
10668 * 'termination' field appropriately.
10669 */
10670 if (eep_config.termination_se == 0) {
10671 termination = 0; /* auto termination for SE */
10672 } else {
10673 /* Enable manual control with low off / high off. */
10674 if (eep_config.termination_se == 1) {
10675 termination = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010677 /* Enable manual control with low off / high on. */
10678 } else if (eep_config.termination_se == 2) {
10679 termination = TERM_SE_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010681 /* Enable manual control with low on / high on. */
10682 } else if (eep_config.termination_se == 3) {
10683 termination = TERM_SE;
10684 } else {
10685 /*
10686 * The EEPROM 'termination_se' field contains a bad value.
10687 * Use automatic termination instead.
10688 */
10689 termination = 0;
10690 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10691 }
10692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010694 if (eep_config.termination_lvd == 0) {
10695 asc_dvc->cfg->termination = termination; /* auto termination for LVD */
10696 } else {
10697 /* Enable manual control with low off / high off. */
10698 if (eep_config.termination_lvd == 1) {
10699 asc_dvc->cfg->termination = termination;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010701 /* Enable manual control with low off / high on. */
10702 } else if (eep_config.termination_lvd == 2) {
10703 asc_dvc->cfg->termination = termination | TERM_LVD_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010705 /* Enable manual control with low on / high on. */
10706 } else if (eep_config.termination_lvd == 3) {
10707 asc_dvc->cfg->termination = termination | TERM_LVD;
10708 } else {
10709 /*
10710 * The EEPROM 'termination_lvd' field contains a bad value.
10711 * Use automatic termination instead.
10712 */
10713 asc_dvc->cfg->termination = termination;
10714 warn_code |= ASC_WARN_EEPROM_TERMINATION;
10715 }
10716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010718 return warn_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010719}
10720
10721/*
Matthew Wilcox51219352007-10-02 21:55:22 -040010722 * Initialize the ADV_DVC_VAR structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723 *
Matthew Wilcox51219352007-10-02 21:55:22 -040010724 * On failure set the ADV_DVC_VAR field 'err_code' and return ADV_ERROR.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725 *
Matthew Wilcox51219352007-10-02 21:55:22 -040010726 * For a non-fatal error return a warning code. If there are no warnings
10727 * then 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010729static int AdvInitGetConfig(struct pci_dev *pdev, struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010730{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010731 struct asc_board *board = shost_priv(shost);
10732 ADV_DVC_VAR *asc_dvc = &board->dvc_var.adv_dvc_var;
Matthew Wilcox51219352007-10-02 21:55:22 -040010733 unsigned short warn_code = 0;
10734 AdvPortAddr iop_base = asc_dvc->iop_base;
10735 u16 cmd;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010736 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010737
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010738 asc_dvc->err_code = 0;
Matthew Wilcox51219352007-10-02 21:55:22 -040010739
10740 /*
10741 * Save the state of the PCI Configuration Command Register
10742 * "Parity Error Response Control" Bit. If the bit is clear (0),
10743 * in AdvInitAsc3550/38C0800Driver() tell the microcode to ignore
10744 * DMA parity errors.
10745 */
10746 asc_dvc->cfg->control_flag = 0;
10747 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
10748 if ((cmd & PCI_COMMAND_PARITY) == 0)
10749 asc_dvc->cfg->control_flag |= CONTROL_FLAG_IGNORE_PERR;
10750
Matthew Wilcox51219352007-10-02 21:55:22 -040010751 asc_dvc->cfg->chip_version =
10752 AdvGetChipVersion(iop_base, asc_dvc->bus_type);
10753
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010754 ASC_DBG(1, "iopb_chip_id_1: 0x%x 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -040010755 (ushort)AdvReadByteRegister(iop_base, IOPB_CHIP_ID_1),
10756 (ushort)ADV_CHIP_ID_BYTE);
10757
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010758 ASC_DBG(1, "iopw_chip_id_0: 0x%x 0x%x\n",
Matthew Wilcox51219352007-10-02 21:55:22 -040010759 (ushort)AdvReadWordRegister(iop_base, IOPW_CHIP_ID_0),
10760 (ushort)ADV_CHIP_ID_WORD);
10761
10762 /*
10763 * Reset the chip to start and allow register writes.
10764 */
10765 if (AdvFindSignature(iop_base) == 0) {
10766 asc_dvc->err_code = ASC_IERR_BAD_SIGNATURE;
10767 return ADV_ERROR;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010768 } else {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010769 /*
Matthew Wilcox51219352007-10-02 21:55:22 -040010770 * The caller must set 'chip_type' to a valid setting.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010771 */
Matthew Wilcox51219352007-10-02 21:55:22 -040010772 if (asc_dvc->chip_type != ADV_CHIP_ASC3550 &&
10773 asc_dvc->chip_type != ADV_CHIP_ASC38C0800 &&
10774 asc_dvc->chip_type != ADV_CHIP_ASC38C1600) {
10775 asc_dvc->err_code |= ASC_IERR_BAD_CHIPTYPE;
10776 return ADV_ERROR;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010778
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010779 /*
Matthew Wilcox51219352007-10-02 21:55:22 -040010780 * Reset Chip.
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010781 */
Matthew Wilcox51219352007-10-02 21:55:22 -040010782 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
10783 ADV_CTRL_REG_CMD_RESET);
10784 mdelay(100);
10785 AdvWriteWordRegister(iop_base, IOPW_CTRL_REG,
10786 ADV_CTRL_REG_CMD_WR_IO_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010787
Matthew Wilcox51219352007-10-02 21:55:22 -040010788 if (asc_dvc->chip_type == ADV_CHIP_ASC38C1600) {
10789 status = AdvInitFrom38C1600EEP(asc_dvc);
10790 } else if (asc_dvc->chip_type == ADV_CHIP_ASC38C0800) {
10791 status = AdvInitFrom38C0800EEP(asc_dvc);
10792 } else {
10793 status = AdvInitFrom3550EEP(asc_dvc);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010794 }
Matthew Wilcox51219352007-10-02 21:55:22 -040010795 warn_code |= status;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010797
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010798 if (warn_code != 0)
10799 shost_printk(KERN_WARNING, shost, "warning: 0x%x\n", warn_code);
Matthew Wilcox51219352007-10-02 21:55:22 -040010800
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010801 if (asc_dvc->err_code)
10802 shost_printk(KERN_ERR, shost, "error code 0x%x\n",
10803 asc_dvc->err_code);
Matthew Wilcox51219352007-10-02 21:55:22 -040010804
10805 return asc_dvc->err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010806}
Matthew Wilcox51219352007-10-02 21:55:22 -040010807#endif
10808
10809static struct scsi_host_template advansys_template = {
10810 .proc_name = DRV_NAME,
10811#ifdef CONFIG_PROC_FS
Al Virob59fb6f2013-03-31 02:59:55 -040010812 .show_info = advansys_show_info,
Matthew Wilcox51219352007-10-02 21:55:22 -040010813#endif
10814 .name = DRV_NAME,
10815 .info = advansys_info,
10816 .queuecommand = advansys_queuecommand,
Hannes Reineckeeac0b0c2015-04-24 13:18:20 +020010817 .eh_host_reset_handler = advansys_reset,
Matthew Wilcox51219352007-10-02 21:55:22 -040010818 .bios_param = advansys_biosparam,
10819 .slave_configure = advansys_slave_configure,
10820 /*
10821 * Because the driver may control an ISA adapter 'unchecked_isa_dma'
10822 * must be set. The flag will be cleared in advansys_board_found
10823 * for non-ISA adapters.
10824 */
Hannes Reineckeae267592015-04-24 13:18:28 +020010825 .unchecked_isa_dma = true,
Matthew Wilcox51219352007-10-02 21:55:22 -040010826 /*
10827 * All adapters controlled by this driver are capable of large
10828 * scatter-gather lists. According to the mid-level SCSI documentation
10829 * this obviates any performance gain provided by setting
10830 * 'use_clustering'. But empirically while CPU utilization is increased
10831 * by enabling clustering, I/O throughput increases as well.
10832 */
10833 .use_clustering = ENABLE_CLUSTERING,
Hannes Reinecke9c17c622015-04-24 13:18:21 +020010834 .use_blk_tags = 1,
Matthew Wilcox51219352007-10-02 21:55:22 -040010835};
Linus Torvalds1da177e2005-04-16 15:20:36 -070010836
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010837static int advansys_wide_init_chip(struct Scsi_Host *shost)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010838{
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010839 struct asc_board *board = shost_priv(shost);
10840 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010841 size_t sgblk_pool_size;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010842 int warn_code, err_code;
10843
10844 /*
10845 * Allocate buffer carrier structures. The total size
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010846 * is about 8 KB, so allocate all at once.
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010847 */
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010848 adv_dvc->carrier = dma_alloc_coherent(board->dev,
10849 ADV_CARRIER_BUFSIZE, &adv_dvc->carrier_addr, GFP_KERNEL);
10850 ASC_DBG(1, "carrier 0x%p\n", adv_dvc->carrier);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010851
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010852 if (!adv_dvc->carrier)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010853 goto kmalloc_failed;
10854
10855 /*
10856 * Allocate up to 'max_host_qng' request structures for the Wide
10857 * board. The total size is about 16 KB, so allocate all at once.
10858 * If the allocation fails decrement and try again.
10859 */
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010860 board->adv_reqp_size = adv_dvc->max_host_qng * sizeof(adv_req_t);
10861 if (board->adv_reqp_size & 0x1f) {
10862 ASC_DBG(1, "unaligned reqp %lu bytes\n", sizeof(adv_req_t));
10863 board->adv_reqp_size = ADV_32BALIGN(board->adv_reqp_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010864 }
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010865 board->adv_reqp = dma_alloc_coherent(board->dev, board->adv_reqp_size,
10866 &board->adv_reqp_addr, GFP_KERNEL);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010867
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010868 if (!board->adv_reqp)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010869 goto kmalloc_failed;
10870
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010871 ASC_DBG(1, "reqp 0x%p, req_cnt %d, bytes %lu\n", board->adv_reqp,
10872 adv_dvc->max_host_qng, board->adv_reqp_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010873
10874 /*
10875 * Allocate up to ADV_TOT_SG_BLOCK request structures for
10876 * the Wide board. Each structure is about 136 bytes.
10877 */
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010878 sgblk_pool_size = sizeof(adv_sgblk_t) * ADV_TOT_SG_BLOCK;
10879 board->adv_sgblk_pool = dma_pool_create("adv_sgblk", board->dev,
10880 sgblk_pool_size, 32, 0);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010881
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010882 ASC_DBG(1, "sg_cnt %d * %lu = %lu bytes\n", ADV_TOT_SG_BLOCK,
10883 sizeof(adv_sgblk_t), sgblk_pool_size);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010884
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010885 if (!board->adv_sgblk_pool)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010886 goto kmalloc_failed;
10887
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010888 if (adv_dvc->chip_type == ADV_CHIP_ASC3550) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010889 ASC_DBG(2, "AdvInitAsc3550Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010890 warn_code = AdvInitAsc3550Driver(adv_dvc);
10891 } else if (adv_dvc->chip_type == ADV_CHIP_ASC38C0800) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010892 ASC_DBG(2, "AdvInitAsc38C0800Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010893 warn_code = AdvInitAsc38C0800Driver(adv_dvc);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010894 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010895 ASC_DBG(2, "AdvInitAsc38C1600Driver()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010896 warn_code = AdvInitAsc38C1600Driver(adv_dvc);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010897 }
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010898 err_code = adv_dvc->err_code;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010899
10900 if (warn_code || err_code) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010901 shost_printk(KERN_WARNING, shost, "error: warn 0x%x, error "
10902 "0x%x\n", warn_code, err_code);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010903 }
10904
10905 goto exit;
10906
10907 kmalloc_failed:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010908 shost_printk(KERN_ERR, shost, "error: kmalloc() failed\n");
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010909 err_code = ADV_ERROR;
10910 exit:
10911 return err_code;
10912}
10913
Matthew Wilcox98d41c22007-10-02 21:55:37 -040010914static void advansys_wide_free_mem(struct asc_board *board)
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010915{
Matthew Wilcox98d41c22007-10-02 21:55:37 -040010916 struct adv_dvc_var *adv_dvc = &board->dvc_var.adv_dvc_var;
Hannes Reinecke98b96a72015-04-24 13:18:23 +020010917
10918 if (adv_dvc->carrier) {
10919 dma_free_coherent(board->dev, ADV_CARRIER_BUFSIZE,
10920 adv_dvc->carrier, adv_dvc->carrier_addr);
10921 adv_dvc->carrier = NULL;
10922 }
Hannes Reinecke4b47e462015-04-24 13:18:24 +020010923 if (board->adv_reqp) {
10924 dma_free_coherent(board->dev, board->adv_reqp_size,
10925 board->adv_reqp, board->adv_reqp_addr);
10926 board->adv_reqp = NULL;
10927 }
Hannes Reinecke0ce53822015-04-24 13:18:25 +020010928 if (board->adv_sgblk_pool) {
10929 dma_pool_destroy(board->adv_sgblk_pool);
10930 board->adv_sgblk_pool = NULL;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010931 }
10932}
10933
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010934static int advansys_board_found(struct Scsi_Host *shost, unsigned int iop,
10935 int bus_type)
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010936{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010937 struct pci_dev *pdev;
Matthew Wilcoxd2411492007-10-02 21:55:31 -040010938 struct asc_board *boardp = shost_priv(shost);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010939 ASC_DVC_VAR *asc_dvc_varp = NULL;
10940 ADV_DVC_VAR *adv_dvc_varp = NULL;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010941 int share_irq, warn_code, ret;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010942
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010943 pdev = (bus_type == ASC_IS_PCI) ? to_pci_dev(boardp->dev) : NULL;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010944
10945 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010946 ASC_DBG(1, "narrow board\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010947 asc_dvc_varp = &boardp->dvc_var.asc_dvc_var;
10948 asc_dvc_varp->bus_type = bus_type;
10949 asc_dvc_varp->drv_ptr = boardp;
10950 asc_dvc_varp->cfg = &boardp->dvc_cfg.asc_dvc_cfg;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010951 asc_dvc_varp->iop_base = iop;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010952 } else {
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010953#ifdef CONFIG_PCI
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010954 adv_dvc_varp = &boardp->dvc_var.adv_dvc_var;
10955 adv_dvc_varp->drv_ptr = boardp;
10956 adv_dvc_varp->cfg = &boardp->dvc_cfg.adv_dvc_cfg;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010957 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010958 ASC_DBG(1, "wide board ASC-3550\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010959 adv_dvc_varp->chip_type = ADV_CHIP_ASC3550;
10960 } else if (pdev->device == PCI_DEVICE_ID_38C0800_REV1) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010961 ASC_DBG(1, "wide board ASC-38C0800\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010962 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C0800;
10963 } else {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010964 ASC_DBG(1, "wide board ASC-38C1600\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010965 adv_dvc_varp->chip_type = ADV_CHIP_ASC38C1600;
10966 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010967
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010968 boardp->asc_n_io_port = pci_resource_len(pdev, 1);
Arjan van de Ven25729a72008-09-28 16:18:02 -070010969 boardp->ioremap_addr = pci_ioremap_bar(pdev, 1);
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010970 if (!boardp->ioremap_addr) {
Matthew Wilcox9d511a42007-10-02 21:55:42 -040010971 shost_printk(KERN_ERR, shost, "ioremap(%lx, %d) "
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010972 "returned NULL\n",
Matthew Wilcox9d511a42007-10-02 21:55:42 -040010973 (long)pci_resource_start(pdev, 1),
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040010974 boardp->asc_n_io_port);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040010975 ret = -ENODEV;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060010976 goto err_shost;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010977 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010978 adv_dvc_varp->iop_base = (AdvPortAddr)boardp->ioremap_addr;
10979 ASC_DBG(1, "iop_base: 0x%p\n", adv_dvc_varp->iop_base);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010980
10981 /*
10982 * Even though it isn't used to access wide boards, other
10983 * than for the debug line below, save I/O Port address so
10984 * that it can be reported.
10985 */
10986 boardp->ioport = iop;
10987
Matthew Wilcoxb352f922007-10-02 21:55:33 -040010988 ASC_DBG(1, "iopb_chip_id_1 0x%x, iopw_chip_id_0 0x%x\n",
10989 (ushort)inp(iop + 1), (ushort)inpw(iop));
Matthew Wilcox57ba5fe2007-07-26 11:55:07 -040010990#endif /* CONFIG_PCI */
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010991 }
10992
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010993 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040010994 /*
10995 * Set the board bus type and PCI IRQ before
10996 * calling AscInitGetConfig().
10997 */
10998 switch (asc_dvc_varp->bus_type) {
10999#ifdef CONFIG_ISA
11000 case ASC_IS_ISA:
Hannes Reineckeae267592015-04-24 13:18:28 +020011001 shost->unchecked_isa_dma = true;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011002 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011003 break;
11004 case ASC_IS_VL:
Hannes Reineckeae267592015-04-24 13:18:28 +020011005 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011006 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011007 break;
11008 case ASC_IS_EISA:
Hannes Reineckeae267592015-04-24 13:18:28 +020011009 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011010 share_irq = IRQF_SHARED;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011011 break;
11012#endif /* CONFIG_ISA */
11013#ifdef CONFIG_PCI
11014 case ASC_IS_PCI:
Hannes Reineckeae267592015-04-24 13:18:28 +020011015 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011016 share_irq = IRQF_SHARED;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011017 break;
11018#endif /* CONFIG_PCI */
11019 default:
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011020 shost_printk(KERN_ERR, shost, "unknown adapter type: "
11021 "%d\n", asc_dvc_varp->bus_type);
Hannes Reineckeae267592015-04-24 13:18:28 +020011022 shost->unchecked_isa_dma = false;
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011023 share_irq = 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011024 break;
11025 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011026
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011027 /*
11028 * NOTE: AscInitGetConfig() may change the board's
11029 * bus_type value. The bus_type value should no
11030 * longer be used. If the bus_type field must be
11031 * referenced only use the bit-wise AND operator "&".
11032 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011033 ASC_DBG(2, "AscInitGetConfig()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011034 ret = AscInitGetConfig(shost) ? -ENODEV : 0;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011035 } else {
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011036#ifdef CONFIG_PCI
11037 /*
11038 * For Wide boards set PCI information before calling
11039 * AdvInitGetConfig().
11040 */
Hannes Reineckeae267592015-04-24 13:18:28 +020011041 shost->unchecked_isa_dma = false;
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011042 share_irq = IRQF_SHARED;
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011043 ASC_DBG(2, "AdvInitGetConfig()\n");
Matthew Wilcox394dbf32007-07-26 11:56:40 -040011044
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011045 ret = AdvInitGetConfig(pdev, shost) ? -ENODEV : 0;
Matthew Wilcoxc2dce2f2007-09-09 08:56:30 -060011046#endif /* CONFIG_PCI */
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011047 }
11048
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011049 if (ret)
Al Virob59fb6f2013-03-31 02:59:55 -040011050 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011051
11052 /*
11053 * Save the EEPROM configuration so that it can be displayed
11054 * from /proc/scsi/advansys/[0...].
11055 */
11056 if (ASC_NARROW_BOARD(boardp)) {
11057
11058 ASCEEP_CONFIG *ep;
11059
11060 /*
11061 * Set the adapter's target id bit in the 'init_tidmask' field.
11062 */
11063 boardp->init_tidmask |=
11064 ADV_TID_TO_TIDMASK(asc_dvc_varp->cfg->chip_scsi_id);
11065
11066 /*
11067 * Save EEPROM settings for the board.
11068 */
11069 ep = &boardp->eep_config.asc_eep;
11070
11071 ep->init_sdtr = asc_dvc_varp->cfg->sdtr_enable;
11072 ep->disc_enable = asc_dvc_varp->cfg->disc_enable;
11073 ep->use_cmd_qng = asc_dvc_varp->cfg->cmd_qng_enabled;
11074 ASC_EEP_SET_DMA_SPD(ep, asc_dvc_varp->cfg->isa_dma_speed);
11075 ep->start_motor = asc_dvc_varp->start_motor;
11076 ep->cntl = asc_dvc_varp->dvc_cntl;
11077 ep->no_scam = asc_dvc_varp->no_scam;
11078 ep->max_total_qng = asc_dvc_varp->max_total_qng;
11079 ASC_EEP_SET_CHIP_ID(ep, asc_dvc_varp->cfg->chip_scsi_id);
11080 /* 'max_tag_qng' is set to the same value for every device. */
11081 ep->max_tag_qng = asc_dvc_varp->cfg->max_tag_qng[0];
11082 ep->adapter_info[0] = asc_dvc_varp->cfg->adapter_info[0];
11083 ep->adapter_info[1] = asc_dvc_varp->cfg->adapter_info[1];
11084 ep->adapter_info[2] = asc_dvc_varp->cfg->adapter_info[2];
11085 ep->adapter_info[3] = asc_dvc_varp->cfg->adapter_info[3];
11086 ep->adapter_info[4] = asc_dvc_varp->cfg->adapter_info[4];
11087 ep->adapter_info[5] = asc_dvc_varp->cfg->adapter_info[5];
11088
11089 /*
11090 * Modify board configuration.
11091 */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011092 ASC_DBG(2, "AscInitSetConfig()\n");
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011093 ret = AscInitSetConfig(pdev, shost) ? -ENODEV : 0;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011094 if (ret)
Al Virob59fb6f2013-03-31 02:59:55 -040011095 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011096 } else {
11097 ADVEEP_3550_CONFIG *ep_3550;
11098 ADVEEP_38C0800_CONFIG *ep_38C0800;
11099 ADVEEP_38C1600_CONFIG *ep_38C1600;
11100
11101 /*
11102 * Save Wide EEP Configuration Information.
11103 */
11104 if (adv_dvc_varp->chip_type == ADV_CHIP_ASC3550) {
11105 ep_3550 = &boardp->eep_config.adv_3550_eep;
11106
11107 ep_3550->adapter_scsi_id = adv_dvc_varp->chip_scsi_id;
11108 ep_3550->max_host_qng = adv_dvc_varp->max_host_qng;
11109 ep_3550->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11110 ep_3550->termination = adv_dvc_varp->cfg->termination;
11111 ep_3550->disc_enable = adv_dvc_varp->cfg->disc_enable;
11112 ep_3550->bios_ctrl = adv_dvc_varp->bios_ctrl;
11113 ep_3550->wdtr_able = adv_dvc_varp->wdtr_able;
11114 ep_3550->sdtr_able = adv_dvc_varp->sdtr_able;
11115 ep_3550->ultra_able = adv_dvc_varp->ultra_able;
11116 ep_3550->tagqng_able = adv_dvc_varp->tagqng_able;
11117 ep_3550->start_motor = adv_dvc_varp->start_motor;
11118 ep_3550->scsi_reset_delay =
11119 adv_dvc_varp->scsi_reset_wait;
11120 ep_3550->serial_number_word1 =
11121 adv_dvc_varp->cfg->serial1;
11122 ep_3550->serial_number_word2 =
11123 adv_dvc_varp->cfg->serial2;
11124 ep_3550->serial_number_word3 =
11125 adv_dvc_varp->cfg->serial3;
11126 } else if (adv_dvc_varp->chip_type == ADV_CHIP_ASC38C0800) {
11127 ep_38C0800 = &boardp->eep_config.adv_38C0800_eep;
11128
11129 ep_38C0800->adapter_scsi_id =
11130 adv_dvc_varp->chip_scsi_id;
11131 ep_38C0800->max_host_qng = adv_dvc_varp->max_host_qng;
11132 ep_38C0800->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11133 ep_38C0800->termination_lvd =
11134 adv_dvc_varp->cfg->termination;
11135 ep_38C0800->disc_enable =
11136 adv_dvc_varp->cfg->disc_enable;
11137 ep_38C0800->bios_ctrl = adv_dvc_varp->bios_ctrl;
11138 ep_38C0800->wdtr_able = adv_dvc_varp->wdtr_able;
11139 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11140 ep_38C0800->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11141 ep_38C0800->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11142 ep_38C0800->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11143 ep_38C0800->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11144 ep_38C0800->tagqng_able = adv_dvc_varp->tagqng_able;
11145 ep_38C0800->start_motor = adv_dvc_varp->start_motor;
11146 ep_38C0800->scsi_reset_delay =
11147 adv_dvc_varp->scsi_reset_wait;
11148 ep_38C0800->serial_number_word1 =
11149 adv_dvc_varp->cfg->serial1;
11150 ep_38C0800->serial_number_word2 =
11151 adv_dvc_varp->cfg->serial2;
11152 ep_38C0800->serial_number_word3 =
11153 adv_dvc_varp->cfg->serial3;
11154 } else {
11155 ep_38C1600 = &boardp->eep_config.adv_38C1600_eep;
11156
11157 ep_38C1600->adapter_scsi_id =
11158 adv_dvc_varp->chip_scsi_id;
11159 ep_38C1600->max_host_qng = adv_dvc_varp->max_host_qng;
11160 ep_38C1600->max_dvc_qng = adv_dvc_varp->max_dvc_qng;
11161 ep_38C1600->termination_lvd =
11162 adv_dvc_varp->cfg->termination;
11163 ep_38C1600->disc_enable =
11164 adv_dvc_varp->cfg->disc_enable;
11165 ep_38C1600->bios_ctrl = adv_dvc_varp->bios_ctrl;
11166 ep_38C1600->wdtr_able = adv_dvc_varp->wdtr_able;
11167 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11168 ep_38C1600->sdtr_speed1 = adv_dvc_varp->sdtr_speed1;
11169 ep_38C1600->sdtr_speed2 = adv_dvc_varp->sdtr_speed2;
11170 ep_38C1600->sdtr_speed3 = adv_dvc_varp->sdtr_speed3;
11171 ep_38C1600->sdtr_speed4 = adv_dvc_varp->sdtr_speed4;
11172 ep_38C1600->tagqng_able = adv_dvc_varp->tagqng_able;
11173 ep_38C1600->start_motor = adv_dvc_varp->start_motor;
11174 ep_38C1600->scsi_reset_delay =
11175 adv_dvc_varp->scsi_reset_wait;
11176 ep_38C1600->serial_number_word1 =
11177 adv_dvc_varp->cfg->serial1;
11178 ep_38C1600->serial_number_word2 =
11179 adv_dvc_varp->cfg->serial2;
11180 ep_38C1600->serial_number_word3 =
11181 adv_dvc_varp->cfg->serial3;
11182 }
11183
11184 /*
11185 * Set the adapter's target id bit in the 'init_tidmask' field.
11186 */
11187 boardp->init_tidmask |=
11188 ADV_TID_TO_TIDMASK(adv_dvc_varp->chip_scsi_id);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011189 }
11190
11191 /*
11192 * Channels are numbered beginning with 0. For AdvanSys one host
11193 * structure supports one channel. Multi-channel boards have a
11194 * separate host structure for each channel.
11195 */
11196 shost->max_channel = 0;
11197 if (ASC_NARROW_BOARD(boardp)) {
11198 shost->max_id = ASC_MAX_TID + 1;
11199 shost->max_lun = ASC_MAX_LUN + 1;
Matthew Wilcoxf05ec592007-09-09 08:56:36 -060011200 shost->max_cmd_len = ASC_MAX_CDB_LEN;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011201
11202 shost->io_port = asc_dvc_varp->iop_base;
11203 boardp->asc_n_io_port = ASC_IOADR_GAP;
11204 shost->this_id = asc_dvc_varp->cfg->chip_scsi_id;
11205
11206 /* Set maximum number of queues the adapter can handle. */
11207 shost->can_queue = asc_dvc_varp->max_total_qng;
11208 } else {
11209 shost->max_id = ADV_MAX_TID + 1;
11210 shost->max_lun = ADV_MAX_LUN + 1;
Matthew Wilcoxf05ec592007-09-09 08:56:36 -060011211 shost->max_cmd_len = ADV_MAX_CDB_LEN;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011212
11213 /*
11214 * Save the I/O Port address and length even though
11215 * I/O ports are not used to access Wide boards.
11216 * Instead the Wide boards are accessed with
11217 * PCI Memory Mapped I/O.
11218 */
11219 shost->io_port = iop;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011220
11221 shost->this_id = adv_dvc_varp->chip_scsi_id;
11222
11223 /* Set maximum number of queues the adapter can handle. */
11224 shost->can_queue = adv_dvc_varp->max_host_qng;
11225 }
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011226 ret = scsi_init_shared_tag_map(shost, shost->can_queue);
11227 if (ret) {
11228 shost_printk(KERN_ERR, shost, "init tag map failed\n");
11229 goto err_free_dma;
11230 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011231
11232 /*
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011233 * Set the maximum number of scatter-gather elements the
11234 * adapter can handle.
11235 */
11236 if (ASC_NARROW_BOARD(boardp)) {
11237 /*
11238 * Allow two commands with 'sg_tablesize' scatter-gather
11239 * elements to be executed simultaneously. This value is
11240 * the theoretical hardware limit. It may be decreased
11241 * below.
11242 */
11243 shost->sg_tablesize =
11244 (((asc_dvc_varp->max_total_qng - 2) / 2) *
11245 ASC_SG_LIST_PER_Q) + 1;
11246 } else {
11247 shost->sg_tablesize = ADV_MAX_SG_LIST;
11248 }
11249
11250 /*
11251 * The value of 'sg_tablesize' can not exceed the SCSI
11252 * mid-level driver definition of SG_ALL. SG_ALL also
11253 * must not be exceeded, because it is used to define the
11254 * size of the scatter-gather table in 'struct asc_sg_head'.
11255 */
11256 if (shost->sg_tablesize > SG_ALL) {
11257 shost->sg_tablesize = SG_ALL;
11258 }
11259
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011260 ASC_DBG(1, "sg_tablesize: %d\n", shost->sg_tablesize);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011261
11262 /* BIOS start address. */
11263 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011264 shost->base = AscGetChipBiosAddress(asc_dvc_varp->iop_base,
11265 asc_dvc_varp->bus_type);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011266 } else {
11267 /*
11268 * Fill-in BIOS board variables. The Wide BIOS saves
11269 * information in LRAM that is used by the driver.
11270 */
11271 AdvReadWordLram(adv_dvc_varp->iop_base,
11272 BIOS_SIGNATURE, boardp->bios_signature);
11273 AdvReadWordLram(adv_dvc_varp->iop_base,
11274 BIOS_VERSION, boardp->bios_version);
11275 AdvReadWordLram(adv_dvc_varp->iop_base,
11276 BIOS_CODESEG, boardp->bios_codeseg);
11277 AdvReadWordLram(adv_dvc_varp->iop_base,
11278 BIOS_CODELEN, boardp->bios_codelen);
11279
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011280 ASC_DBG(1, "bios_signature 0x%x, bios_version 0x%x\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011281 boardp->bios_signature, boardp->bios_version);
11282
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011283 ASC_DBG(1, "bios_codeseg 0x%x, bios_codelen 0x%x\n",
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011284 boardp->bios_codeseg, boardp->bios_codelen);
11285
11286 /*
11287 * If the BIOS saved a valid signature, then fill in
11288 * the BIOS code segment base address.
11289 */
11290 if (boardp->bios_signature == 0x55AA) {
11291 /*
11292 * Convert x86 realmode code segment to a linear
11293 * address by shifting left 4.
11294 */
11295 shost->base = ((ulong)boardp->bios_codeseg << 4);
11296 } else {
11297 shost->base = 0;
11298 }
11299 }
11300
11301 /*
11302 * Register Board Resources - I/O Port, DMA, IRQ
11303 */
11304
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011305 /* Register DMA Channel for Narrow boards. */
11306 shost->dma_channel = NO_ISA_DMA; /* Default to no ISA DMA. */
11307#ifdef CONFIG_ISA
11308 if (ASC_NARROW_BOARD(boardp)) {
11309 /* Register DMA channel for ISA bus. */
11310 if (asc_dvc_varp->bus_type & ASC_IS_ISA) {
11311 shost->dma_channel = asc_dvc_varp->cfg->isa_dma_channel;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011312 ret = request_dma(shost->dma_channel, DRV_NAME);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011313 if (ret) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011314 shost_printk(KERN_ERR, shost, "request_dma() "
11315 "%d failed %d\n",
11316 shost->dma_channel, ret);
Al Virob59fb6f2013-03-31 02:59:55 -040011317 goto err_unmap;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011318 }
11319 AscEnableIsaDma(shost->dma_channel);
11320 }
11321 }
11322#endif /* CONFIG_ISA */
11323
11324 /* Register IRQ Number. */
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011325 ASC_DBG(2, "request_irq(%d, %p)\n", boardp->irq, shost);
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011326
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011327 ret = request_irq(boardp->irq, advansys_interrupt, share_irq,
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011328 DRV_NAME, shost);
Matthew Wilcox074c8fe2007-07-28 23:11:05 -060011329
11330 if (ret) {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011331 if (ret == -EBUSY) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011332 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11333 "already in use\n", boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011334 } else if (ret == -EINVAL) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011335 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11336 "not valid\n", boardp->irq);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011337 } else {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011338 shost_printk(KERN_ERR, shost, "request_irq(): IRQ 0x%x "
11339 "failed with %d\n", boardp->irq, ret);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011340 }
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011341 goto err_free_dma;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011342 }
11343
11344 /*
11345 * Initialize board RISC chip and enable interrupts.
11346 */
11347 if (ASC_NARROW_BOARD(boardp)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011348 ASC_DBG(2, "AscInitAsc1000Driver()\n");
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011349
11350 asc_dvc_varp->overrun_buf = kzalloc(ASC_OVERRUN_BSIZE, GFP_KERNEL);
11351 if (!asc_dvc_varp->overrun_buf) {
11352 ret = -ENOMEM;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011353 goto err_free_irq;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011354 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011355 warn_code = AscInitAsc1000Driver(asc_dvc_varp);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011356
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011357 if (warn_code || asc_dvc_varp->err_code) {
Matthew Wilcox9d0e96e2007-10-02 21:55:35 -040011358 shost_printk(KERN_ERR, shost, "error: init_state 0x%x, "
11359 "warn 0x%x, error 0x%x\n",
11360 asc_dvc_varp->init_state, warn_code,
11361 asc_dvc_varp->err_code);
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011362 if (!asc_dvc_varp->overrun_dma) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011363 ret = -ENODEV;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011364 goto err_free_mem;
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011365 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011366 }
11367 } else {
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011368 if (advansys_wide_init_chip(shost)) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011369 ret = -ENODEV;
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011370 goto err_free_mem;
11371 }
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011372 }
11373
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011374 ASC_DBG_PRT_SCSI_HOST(2, shost);
11375
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011376 ret = scsi_add_host(shost, boardp->dev);
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011377 if (ret)
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011378 goto err_free_mem;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011379
11380 scsi_scan_host(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011381 return 0;
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011382
Herton Ronaldo Krzesinski9a908c12010-03-30 13:35:38 -030011383 err_free_mem:
11384 if (ASC_NARROW_BOARD(boardp)) {
11385 if (asc_dvc_varp->overrun_dma)
11386 dma_unmap_single(boardp->dev, asc_dvc_varp->overrun_dma,
11387 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
11388 kfree(asc_dvc_varp->overrun_buf);
11389 } else
11390 advansys_wide_free_mem(boardp);
11391 err_free_irq:
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011392 free_irq(boardp->irq, shost);
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011393 err_free_dma:
Al Viro30037812008-11-22 17:34:54 +000011394#ifdef CONFIG_ISA
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011395 if (shost->dma_channel != NO_ISA_DMA)
11396 free_dma(shost->dma_channel);
Al Viro30037812008-11-22 17:34:54 +000011397#endif
Matthew Wilcoxb2c16f52007-07-29 17:30:28 -060011398 err_unmap:
11399 if (boardp->ioremap_addr)
11400 iounmap(boardp->ioremap_addr);
11401 err_shost:
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011402 return ret;
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011403}
11404
11405/*
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011406 * advansys_release()
11407 *
11408 * Release resources allocated for a single AdvanSys adapter.
11409 */
11410static int advansys_release(struct Scsi_Host *shost)
11411{
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011412 struct asc_board *board = shost_priv(shost);
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011413 ASC_DBG(1, "begin\n");
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011414 scsi_remove_host(shost);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011415 free_irq(board->irq, shost);
Al Viro30037812008-11-22 17:34:54 +000011416#ifdef CONFIG_ISA
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011417 if (shost->dma_channel != NO_ISA_DMA) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011418 ASC_DBG(1, "free_dma()\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011419 free_dma(shost->dma_channel);
11420 }
Al Viro30037812008-11-22 17:34:54 +000011421#endif
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011422 if (ASC_NARROW_BOARD(board)) {
11423 dma_unmap_single(board->dev,
11424 board->dvc_var.asc_dvc_var.overrun_dma,
11425 ASC_OVERRUN_BSIZE, DMA_FROM_DEVICE);
FUJITA Tomonori7d5d4082008-02-08 09:50:08 +090011426 kfree(board->dvc_var.asc_dvc_var.overrun_buf);
Matthew Wilcoxd10fb2c2007-10-02 21:55:41 -040011427 } else {
11428 iounmap(board->ioremap_addr);
11429 advansys_wide_free_mem(board);
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011430 }
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011431 scsi_host_put(shost);
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011432 ASC_DBG(1, "end\n");
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011433 return 0;
11434}
11435
Matthew Wilcox95c9f162007-09-09 08:56:39 -060011436#define ASC_IOADR_TABLE_MAX_IX 11
11437
Randy Dunlap747d0162008-01-14 00:55:18 -080011438static PortAddr _asc_def_iop_base[ASC_IOADR_TABLE_MAX_IX] = {
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011439 0x100, 0x0110, 0x120, 0x0130, 0x140, 0x0150, 0x0190,
11440 0x0210, 0x0230, 0x0250, 0x0330
11441};
11442
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011443/*
11444 * The ISA IRQ number is found in bits 2 and 3 of the CfgLsw. It decodes as:
11445 * 00: 10
11446 * 01: 11
11447 * 10: 12
11448 * 11: 15
11449 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011450static unsigned int advansys_isa_irq_no(PortAddr iop_base)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011451{
11452 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11453 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x03) + 10;
11454 if (chip_irq == 13)
11455 chip_irq = 15;
11456 return chip_irq;
11457}
11458
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011459static int advansys_isa_probe(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011460{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011461 int err = -ENODEV;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011462 PortAddr iop_base = _asc_def_iop_base[id];
11463 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011464 struct asc_board *board;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011465
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011466 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011467 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011468 return -ENODEV;
11469 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011470 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011471 if (!AscFindSignature(iop_base))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011472 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011473 if (!(AscGetChipVersion(iop_base, ASC_IS_ISA) & ASC_CHIP_VER_ISA_BIT))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011474 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011475
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011476 err = -ENOMEM;
11477 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011478 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011479 goto release_region;
11480
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011481 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011482 board->irq = advansys_isa_irq_no(iop_base);
11483 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011484 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011485
11486 err = advansys_board_found(shost, iop_base, ASC_IS_ISA);
11487 if (err)
11488 goto free_host;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011489
11490 dev_set_drvdata(dev, shost);
11491 return 0;
11492
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011493 free_host:
11494 scsi_host_put(shost);
11495 release_region:
Matthew Wilcox71f361152007-07-30 08:04:53 -060011496 release_region(iop_base, ASC_IOADR_GAP);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011497 return err;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011498}
11499
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011500static int advansys_isa_remove(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011501{
Matthew Wilcox71f361152007-07-30 08:04:53 -060011502 int ioport = _asc_def_iop_base[id];
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011503 advansys_release(dev_get_drvdata(dev));
Matthew Wilcox71f361152007-07-30 08:04:53 -060011504 release_region(ioport, ASC_IOADR_GAP);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011505 return 0;
11506}
11507
11508static struct isa_driver advansys_isa_driver = {
11509 .probe = advansys_isa_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011510 .remove = advansys_isa_remove,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011511 .driver = {
11512 .owner = THIS_MODULE,
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011513 .name = DRV_NAME,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011514 },
11515};
11516
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011517/*
11518 * The VLB IRQ number is found in bits 2 to 4 of the CfgLsw. It decodes as:
11519 * 000: invalid
11520 * 001: 10
11521 * 010: 11
11522 * 011: 12
11523 * 100: invalid
11524 * 101: 14
11525 * 110: 15
11526 * 111: invalid
11527 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011528static unsigned int advansys_vlb_irq_no(PortAddr iop_base)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011529{
11530 unsigned short cfg_lsw = AscGetChipCfgLsw(iop_base);
11531 unsigned int chip_irq = ((cfg_lsw >> 2) & 0x07) + 9;
11532 if ((chip_irq < 10) || (chip_irq == 13) || (chip_irq > 15))
11533 return 0;
11534 return chip_irq;
11535}
11536
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011537static int advansys_vlb_probe(struct device *dev, unsigned int id)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011538{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011539 int err = -ENODEV;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011540 PortAddr iop_base = _asc_def_iop_base[id];
11541 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011542 struct asc_board *board;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011543
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011544 if (!request_region(iop_base, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011545 ASC_DBG(1, "I/O port 0x%x busy\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011546 return -ENODEV;
11547 }
Matthew Wilcoxb352f922007-10-02 21:55:33 -040011548 ASC_DBG(1, "probing I/O port 0x%x\n", iop_base);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011549 if (!AscFindSignature(iop_base))
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011550 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011551 /*
11552 * I don't think this condition can actually happen, but the old
11553 * driver did it, and the chances of finding a VLB setup in 2007
11554 * to do testing with is slight to none.
11555 */
11556 if (AscGetChipVersion(iop_base, ASC_IS_VL) > ASC_CHIP_MAX_VER_VL)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011557 goto release_region;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011558
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011559 err = -ENOMEM;
11560 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011561 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011562 goto release_region;
11563
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011564 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011565 board->irq = advansys_vlb_irq_no(iop_base);
11566 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011567 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011568
11569 err = advansys_board_found(shost, iop_base, ASC_IS_VL);
11570 if (err)
11571 goto free_host;
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011572
11573 dev_set_drvdata(dev, shost);
11574 return 0;
11575
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011576 free_host:
11577 scsi_host_put(shost);
11578 release_region:
Matthew Wilcox71f361152007-07-30 08:04:53 -060011579 release_region(iop_base, ASC_IOADR_GAP);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011580 return -ENODEV;
11581}
11582
11583static struct isa_driver advansys_vlb_driver = {
11584 .probe = advansys_vlb_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011585 .remove = advansys_isa_remove,
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011586 .driver = {
11587 .owner = THIS_MODULE,
Matthew Wilcoxb8e5152b2007-09-09 08:56:26 -060011588 .name = "advansys_vlb",
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011589 },
11590};
11591
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011592static struct eisa_device_id advansys_eisa_table[] = {
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011593 { "ABP7401" },
11594 { "ABP7501" },
11595 { "" }
11596};
11597
11598MODULE_DEVICE_TABLE(eisa, advansys_eisa_table);
11599
11600/*
11601 * EISA is a little more tricky than PCI; each EISA device may have two
11602 * channels, and this driver is written to make each channel its own Scsi_Host
11603 */
11604struct eisa_scsi_data {
11605 struct Scsi_Host *host[2];
11606};
11607
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011608/*
11609 * The EISA IRQ number is found in bits 8 to 10 of the CfgLsw. It decodes as:
11610 * 000: 10
11611 * 001: 11
11612 * 010: 12
11613 * 011: invalid
11614 * 100: 14
11615 * 101: 15
11616 * 110: invalid
11617 * 111: invalid
11618 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011619static unsigned int advansys_eisa_irq_no(struct eisa_device *edev)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011620{
11621 unsigned short cfg_lsw = inw(edev->base_addr + 0xc86);
11622 unsigned int chip_irq = ((cfg_lsw >> 8) & 0x07) + 10;
11623 if ((chip_irq == 13) || (chip_irq > 15))
11624 return 0;
11625 return chip_irq;
11626}
11627
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011628static int advansys_eisa_probe(struct device *dev)
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011629{
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011630 int i, ioport, irq = 0;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011631 int err;
11632 struct eisa_device *edev = to_eisa_device(dev);
11633 struct eisa_scsi_data *data;
11634
11635 err = -ENOMEM;
11636 data = kzalloc(sizeof(*data), GFP_KERNEL);
11637 if (!data)
11638 goto fail;
11639 ioport = edev->base_addr + 0xc30;
11640
11641 err = -ENODEV;
11642 for (i = 0; i < 2; i++, ioport += 0x20) {
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011643 struct asc_board *board;
11644 struct Scsi_Host *shost;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011645 if (!request_region(ioport, ASC_IOADR_GAP, DRV_NAME)) {
Matthew Wilcox71f361152007-07-30 08:04:53 -060011646 printk(KERN_WARNING "Region %x-%x busy\n", ioport,
11647 ioport + ASC_IOADR_GAP - 1);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011648 continue;
Matthew Wilcox71f361152007-07-30 08:04:53 -060011649 }
11650 if (!AscFindSignature(ioport)) {
11651 release_region(ioport, ASC_IOADR_GAP);
11652 continue;
11653 }
11654
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011655 /*
11656 * I don't know why we need to do this for EISA chips, but
11657 * not for any others. It looks to be equivalent to
11658 * AscGetChipCfgMsw, but I may have overlooked something,
11659 * so I'm not converting it until I get an EISA board to
11660 * test with.
11661 */
11662 inw(ioport + 4);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011663
11664 if (!irq)
11665 irq = advansys_eisa_irq_no(edev);
11666
11667 err = -ENOMEM;
11668 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
11669 if (!shost)
11670 goto release_region;
11671
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011672 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011673 board->irq = irq;
11674 board->dev = dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011675 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011676
11677 err = advansys_board_found(shost, ioport, ASC_IS_EISA);
11678 if (!err) {
11679 data->host[i] = shost;
11680 continue;
Matthew Wilcox71f361152007-07-30 08:04:53 -060011681 }
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011682
11683 scsi_host_put(shost);
11684 release_region:
11685 release_region(ioport, ASC_IOADR_GAP);
11686 break;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011687 }
11688
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011689 if (err)
11690 goto free_data;
11691 dev_set_drvdata(dev, data);
11692 return 0;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011693
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011694 free_data:
11695 kfree(data->host[0]);
11696 kfree(data->host[1]);
11697 kfree(data);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011698 fail:
11699 return err;
11700}
11701
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011702static int advansys_eisa_remove(struct device *dev)
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011703{
11704 int i;
11705 struct eisa_scsi_data *data = dev_get_drvdata(dev);
11706
11707 for (i = 0; i < 2; i++) {
Matthew Wilcox71f361152007-07-30 08:04:53 -060011708 int ioport;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011709 struct Scsi_Host *shost = data->host[i];
11710 if (!shost)
11711 continue;
Matthew Wilcox71f361152007-07-30 08:04:53 -060011712 ioport = shost->io_port;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011713 advansys_release(shost);
Matthew Wilcox71f361152007-07-30 08:04:53 -060011714 release_region(ioport, ASC_IOADR_GAP);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011715 }
11716
11717 kfree(data);
11718 return 0;
11719}
11720
11721static struct eisa_driver advansys_eisa_driver = {
11722 .id_table = advansys_eisa_table,
11723 .driver = {
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011724 .name = DRV_NAME,
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011725 .probe = advansys_eisa_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011726 .remove = advansys_eisa_remove,
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011727 }
11728};
11729
Dave Jones2672ea82006-08-02 17:11:49 -040011730/* PCI Devices supported by this driver */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011731static struct pci_device_id advansys_pci_tbl[] = {
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011732 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_1200A,
11733 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11734 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940,
11735 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11736 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940U,
11737 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11738 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_ASP_ABP940UW,
11739 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11740 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C0800_REV1,
11741 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11742 {PCI_VENDOR_ID_ASP, PCI_DEVICE_ID_38C1600_REV1,
11743 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
11744 {}
Dave Jones2672ea82006-08-02 17:11:49 -040011745};
Matthew Wilcox27c868c2007-07-26 10:56:23 -040011746
Dave Jones2672ea82006-08-02 17:11:49 -040011747MODULE_DEVICE_TABLE(pci, advansys_pci_tbl);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011748
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011749static void advansys_set_latency(struct pci_dev *pdev)
Matthew Wilcox9649af32007-07-26 21:51:47 -060011750{
11751 if ((pdev->device == PCI_DEVICE_ID_ASP_1200A) ||
11752 (pdev->device == PCI_DEVICE_ID_ASP_ABP940)) {
11753 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0);
11754 } else {
11755 u8 latency;
11756 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
11757 if (latency < 0x20)
11758 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
11759 }
11760}
11761
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011762static int advansys_pci_probe(struct pci_dev *pdev,
11763 const struct pci_device_id *ent)
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011764{
11765 int err, ioport;
11766 struct Scsi_Host *shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011767 struct asc_board *board;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011768
11769 err = pci_enable_device(pdev);
11770 if (err)
11771 goto fail;
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011772 err = pci_request_regions(pdev, DRV_NAME);
Matthew Wilcox71f361152007-07-30 08:04:53 -060011773 if (err)
11774 goto disable_device;
Matthew Wilcox9649af32007-07-26 21:51:47 -060011775 pci_set_master(pdev);
11776 advansys_set_latency(pdev);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011777
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011778 err = -ENODEV;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011779 if (pci_resource_len(pdev, 0) == 0)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011780 goto release_region;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011781
11782 ioport = pci_resource_start(pdev, 0);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011783
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011784 err = -ENOMEM;
11785 shost = scsi_host_alloc(&advansys_template, sizeof(*board));
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011786 if (!shost)
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011787 goto release_region;
11788
Matthew Wilcoxd2411492007-10-02 21:55:31 -040011789 board = shost_priv(shost);
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011790 board->irq = pdev->irq;
11791 board->dev = &pdev->dev;
Hannes Reinecke9c17c622015-04-24 13:18:21 +020011792 board->shost = shost;
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011793
11794 if (pdev->device == PCI_DEVICE_ID_ASP_ABP940UW ||
11795 pdev->device == PCI_DEVICE_ID_38C0800_REV1 ||
11796 pdev->device == PCI_DEVICE_ID_38C1600_REV1) {
11797 board->flags |= ASC_IS_WIDE_BOARD;
11798 }
11799
11800 err = advansys_board_found(shost, ioport, ASC_IS_PCI);
11801 if (err)
11802 goto free_host;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011803
11804 pci_set_drvdata(pdev, shost);
11805 return 0;
11806
Matthew Wilcoxd361db42007-10-02 21:55:29 -040011807 free_host:
11808 scsi_host_put(shost);
11809 release_region:
Matthew Wilcox71f361152007-07-30 08:04:53 -060011810 pci_release_regions(pdev);
11811 disable_device:
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011812 pci_disable_device(pdev);
11813 fail:
11814 return err;
11815}
11816
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011817static void advansys_pci_remove(struct pci_dev *pdev)
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011818{
11819 advansys_release(pci_get_drvdata(pdev));
Matthew Wilcox71f361152007-07-30 08:04:53 -060011820 pci_release_regions(pdev);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011821 pci_disable_device(pdev);
11822}
11823
11824static struct pci_driver advansys_pci_driver = {
Matthew Wilcox01fbfe02007-09-09 08:56:40 -060011825 .name = DRV_NAME,
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011826 .id_table = advansys_pci_tbl,
11827 .probe = advansys_pci_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011828 .remove = advansys_pci_remove,
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011829};
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040011830
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011831static int __init advansys_init(void)
11832{
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011833 int error;
11834
11835 error = isa_register_driver(&advansys_isa_driver,
11836 ASC_IOADR_TABLE_MAX_IX);
11837 if (error)
11838 goto fail;
11839
11840 error = isa_register_driver(&advansys_vlb_driver,
11841 ASC_IOADR_TABLE_MAX_IX);
11842 if (error)
11843 goto unregister_isa;
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011844
11845 error = eisa_driver_register(&advansys_eisa_driver);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011846 if (error)
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011847 goto unregister_vlb;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011848
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011849 error = pci_register_driver(&advansys_pci_driver);
11850 if (error)
11851 goto unregister_eisa;
11852
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011853 return 0;
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011854
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011855 unregister_eisa:
11856 eisa_driver_unregister(&advansys_eisa_driver);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011857 unregister_vlb:
11858 isa_unregister_driver(&advansys_vlb_driver);
11859 unregister_isa:
11860 isa_unregister_driver(&advansys_isa_driver);
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011861 fail:
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011862 return error;
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011863}
11864
11865static void __exit advansys_exit(void)
11866{
Matthew Wilcox78e77d82007-07-29 21:46:15 -060011867 pci_unregister_driver(&advansys_pci_driver);
Matthew Wilcoxb09e05a2007-07-30 09:14:52 -060011868 eisa_driver_unregister(&advansys_eisa_driver);
Matthew Wilcoxc304ec92007-07-30 09:18:45 -060011869 isa_unregister_driver(&advansys_vlb_driver);
11870 isa_unregister_driver(&advansys_isa_driver);
Matthew Wilcox8dfb5372007-07-30 09:08:34 -060011871}
11872
11873module_init(advansys_init);
11874module_exit(advansys_exit);
11875
Matthew Wilcox8c6af9e2007-07-26 11:03:19 -040011876MODULE_LICENSE("GPL");
Jaswinder Singh Rajput989bb5f2009-04-02 11:28:06 +053011877MODULE_FIRMWARE("advansys/mcode.bin");
11878MODULE_FIRMWARE("advansys/3550.bin");
11879MODULE_FIRMWARE("advansys/38C0800.bin");
11880MODULE_FIRMWARE("advansys/38C1600.bin");