trm.c revision 1.4.2.6 1 1.4.2.6 nathanw /* $NetBSD: trm.c,v 1.4.2.6 2002/10/18 02:43:21 nathanw Exp $ */
2 1.4.2.2 nathanw /*
3 1.4.2.2 nathanw * Device Driver for Tekram DC395U/UW/F, DC315/U
4 1.4.2.2 nathanw * PCI SCSI Bus Master Host Adapter
5 1.4.2.2 nathanw * (SCSI chip set used Tekram ASIC TRM-S1040)
6 1.4.2.2 nathanw *
7 1.4.2.4 nathanw * Copyright (c) 2002 Izumi Tsutsui
8 1.4.2.2 nathanw * Copyright (c) 2001 Rui-Xiang Guo
9 1.4.2.2 nathanw * All rights reserved.
10 1.4.2.2 nathanw *
11 1.4.2.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.4.2.2 nathanw * modification, are permitted provided that the following conditions
13 1.4.2.2 nathanw * are met:
14 1.4.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.4.2.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.4.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.4.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.4.2.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.4.2.2 nathanw * 3. The name of the author may not be used to endorse or promote products
20 1.4.2.2 nathanw * derived from this software without specific prior written permission.
21 1.4.2.2 nathanw *
22 1.4.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.4.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.4.2.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.4.2.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.4.2.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.4.2.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.4.2.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.4.2.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.4.2.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.4.2.2 nathanw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.4.2.2 nathanw */
33 1.4.2.2 nathanw /*
34 1.4.2.2 nathanw * Ported from
35 1.4.2.2 nathanw * dc395x_trm.c
36 1.4.2.2 nathanw *
37 1.4.2.2 nathanw * Written for NetBSD 1.4.x by
38 1.4.2.2 nathanw * Erich Chen (erich (at) tekram.com.tw)
39 1.4.2.2 nathanw *
40 1.4.2.2 nathanw * Provided by
41 1.4.2.2 nathanw * (C)Copyright 1995-1999 Tekram Technology Co., Ltd. All rights reserved.
42 1.4.2.2 nathanw */
43 1.4.2.2 nathanw
44 1.4.2.2 nathanw #include <sys/cdefs.h>
45 1.4.2.6 nathanw __KERNEL_RCSID(0, "$NetBSD: trm.c,v 1.4.2.6 2002/10/18 02:43:21 nathanw Exp $");
46 1.4.2.2 nathanw
47 1.4.2.2 nathanw /* #define TRM_DEBUG */
48 1.4.2.4 nathanw #ifdef TRM_DEBUG
49 1.4.2.4 nathanw int trm_debug = 1;
50 1.4.2.4 nathanw #define DPRINTF(arg) if (trm_debug > 0) printf arg;
51 1.4.2.4 nathanw #else
52 1.4.2.4 nathanw #define DPRINTF(arg)
53 1.4.2.4 nathanw #endif
54 1.4.2.2 nathanw
55 1.4.2.2 nathanw #include <sys/param.h>
56 1.4.2.2 nathanw #include <sys/systm.h>
57 1.4.2.2 nathanw #include <sys/malloc.h>
58 1.4.2.2 nathanw #include <sys/buf.h>
59 1.4.2.2 nathanw #include <sys/kernel.h>
60 1.4.2.2 nathanw #include <sys/device.h>
61 1.4.2.4 nathanw #include <sys/queue.h>
62 1.4.2.2 nathanw
63 1.4.2.2 nathanw #include <machine/bus.h>
64 1.4.2.2 nathanw #include <machine/intr.h>
65 1.4.2.2 nathanw
66 1.4.2.2 nathanw #include <uvm/uvm_extern.h>
67 1.4.2.2 nathanw
68 1.4.2.2 nathanw #include <dev/scsipi/scsi_all.h>
69 1.4.2.2 nathanw #include <dev/scsipi/scsi_message.h>
70 1.4.2.2 nathanw #include <dev/scsipi/scsipi_all.h>
71 1.4.2.2 nathanw #include <dev/scsipi/scsiconf.h>
72 1.4.2.2 nathanw
73 1.4.2.2 nathanw #include <dev/pci/pcidevs.h>
74 1.4.2.2 nathanw #include <dev/pci/pcireg.h>
75 1.4.2.2 nathanw #include <dev/pci/pcivar.h>
76 1.4.2.2 nathanw #include <dev/pci/trmreg.h>
77 1.4.2.2 nathanw
78 1.4.2.2 nathanw /*
79 1.4.2.2 nathanw * feature of chip set MAX value
80 1.4.2.2 nathanw */
81 1.4.2.2 nathanw #define TRM_MAX_TARGETS 16
82 1.4.2.4 nathanw #define TRM_MAX_LUNS 8
83 1.4.2.2 nathanw #define TRM_MAX_SG_ENTRIES (MAXPHYS / PAGE_SIZE + 1)
84 1.4.2.4 nathanw #define TRM_MAX_SRB 32 /* XXX */
85 1.4.2.4 nathanw #define TRM_MAX_TAG TRM_MAX_SRB /* XXX */
86 1.4.2.4 nathanw #define TRM_MAX_OFFSET 15
87 1.4.2.4 nathanw #define TRM_MAX_PERIOD 125
88 1.4.2.2 nathanw
89 1.4.2.2 nathanw /*
90 1.4.2.2 nathanw * Segment Entry
91 1.4.2.2 nathanw */
92 1.4.2.2 nathanw struct trm_sg_entry {
93 1.4.2.2 nathanw u_int32_t address;
94 1.4.2.2 nathanw u_int32_t length;
95 1.4.2.2 nathanw };
96 1.4.2.2 nathanw
97 1.4.2.2 nathanw #define TRM_SG_SIZE (sizeof(struct trm_sg_entry) * TRM_MAX_SG_ENTRIES)
98 1.4.2.2 nathanw
99 1.4.2.2 nathanw /*
100 1.4.2.2 nathanw **********************************************************************
101 1.4.2.2 nathanw * The SEEPROM structure for TRM_S1040
102 1.4.2.2 nathanw **********************************************************************
103 1.4.2.2 nathanw */
104 1.4.2.2 nathanw struct nvram_target {
105 1.4.2.2 nathanw u_int8_t config0; /* Target configuration byte 0 */
106 1.4.2.2 nathanw #define NTC_DO_WIDE_NEGO 0x20 /* Wide negotiate */
107 1.4.2.4 nathanw #define NTC_DO_TAG_QUEUING 0x10 /* Enable SCSI tagged queuing */
108 1.4.2.2 nathanw #define NTC_DO_SEND_START 0x08 /* Send start command SPINUP */
109 1.4.2.2 nathanw #define NTC_DO_DISCONNECT 0x04 /* Enable SCSI disconnect */
110 1.4.2.2 nathanw #define NTC_DO_SYNC_NEGO 0x02 /* Sync negotiation */
111 1.4.2.3 nathanw #define NTC_DO_PARITY_CHK 0x01 /* Parity check enable */
112 1.4.2.2 nathanw u_int8_t period; /* Target period */
113 1.4.2.2 nathanw u_int8_t config2; /* Target configuration byte 2 */
114 1.4.2.2 nathanw u_int8_t config3; /* Target configuration byte 3 */
115 1.4.2.2 nathanw };
116 1.4.2.2 nathanw
117 1.4.2.2 nathanw struct trm_nvram {
118 1.4.2.2 nathanw u_int8_t subvendor_id[2]; /* 0,1 Sub Vendor ID */
119 1.4.2.2 nathanw u_int8_t subsys_id[2]; /* 2,3 Sub System ID */
120 1.4.2.2 nathanw u_int8_t subclass; /* 4 Sub Class */
121 1.4.2.2 nathanw u_int8_t vendor_id[2]; /* 5,6 Vendor ID */
122 1.4.2.2 nathanw u_int8_t device_id[2]; /* 7,8 Device ID */
123 1.4.2.2 nathanw u_int8_t reserved0; /* 9 Reserved */
124 1.4.2.2 nathanw struct nvram_target target[TRM_MAX_TARGETS];
125 1.4.2.2 nathanw /* 10,11,12,13
126 1.4.2.2 nathanw * 14,15,16,17
127 1.4.2.2 nathanw * ....
128 1.4.2.2 nathanw * 70,71,72,73 */
129 1.4.2.2 nathanw u_int8_t scsi_id; /* 74 Host Adapter SCSI ID */
130 1.4.2.2 nathanw u_int8_t channel_cfg; /* 75 Channel configuration */
131 1.4.2.2 nathanw #define NAC_SCANLUN 0x20 /* Include LUN as BIOS device */
132 1.4.2.2 nathanw #define NAC_DO_PARITY_CHK 0x08 /* Parity check enable */
133 1.4.2.2 nathanw #define NAC_POWERON_SCSI_RESET 0x04 /* Power on reset enable */
134 1.4.2.2 nathanw #define NAC_GREATER_1G 0x02 /* > 1G support enable */
135 1.4.2.2 nathanw #define NAC_GT2DRIVES 0x01 /* Support more than 2 drives */
136 1.4.2.2 nathanw u_int8_t delay_time; /* 76 Power on delay time */
137 1.4.2.2 nathanw u_int8_t max_tag; /* 77 Maximum tags */
138 1.4.2.2 nathanw u_int8_t reserved1; /* 78 */
139 1.4.2.2 nathanw u_int8_t boot_target; /* 79 */
140 1.4.2.2 nathanw u_int8_t boot_lun; /* 80 */
141 1.4.2.2 nathanw u_int8_t reserved2; /* 81 */
142 1.4.2.2 nathanw u_int8_t reserved3[44]; /* 82,..125 */
143 1.4.2.2 nathanw u_int8_t checksum0; /* 126 */
144 1.4.2.2 nathanw u_int8_t checksum1; /* 127 */
145 1.4.2.2 nathanw #define TRM_NVRAM_CKSUM 0x1234
146 1.4.2.2 nathanw };
147 1.4.2.2 nathanw
148 1.4.2.2 nathanw /* Nvram Initiater bits definition */
149 1.4.2.2 nathanw #define MORE2_DRV 0x00000001
150 1.4.2.2 nathanw #define GREATER_1G 0x00000002
151 1.4.2.2 nathanw #define RST_SCSI_BUS 0x00000004
152 1.4.2.2 nathanw #define ACTIVE_NEGATION 0x00000008
153 1.4.2.2 nathanw #define NO_SEEK 0x00000010
154 1.4.2.2 nathanw #define LUN_CHECK 0x00000020
155 1.4.2.2 nathanw
156 1.4.2.4 nathanw #define trm_eeprom_wait() DELAY(30)
157 1.4.2.2 nathanw
158 1.4.2.2 nathanw /*
159 1.4.2.2 nathanw *-----------------------------------------------------------------------
160 1.4.2.2 nathanw * SCSI Request Block
161 1.4.2.2 nathanw *-----------------------------------------------------------------------
162 1.4.2.2 nathanw */
163 1.4.2.2 nathanw struct trm_srb {
164 1.4.2.4 nathanw TAILQ_ENTRY(trm_srb) next;
165 1.4.2.2 nathanw
166 1.4.2.2 nathanw struct trm_sg_entry *sgentry;
167 1.4.2.3 nathanw struct scsipi_xfer *xs; /* scsipi_xfer for this cmd */
168 1.4.2.2 nathanw bus_dmamap_t dmap;
169 1.4.2.2 nathanw bus_size_t sgoffset; /* Xfer buf offset */
170 1.4.2.2 nathanw
171 1.4.2.2 nathanw u_int32_t buflen; /* Total xfer length */
172 1.4.2.2 nathanw u_int32_t sgaddr; /* SGList physical starting address */
173 1.4.2.2 nathanw
174 1.4.2.2 nathanw int sgcnt;
175 1.4.2.2 nathanw int sgindex;
176 1.4.2.2 nathanw
177 1.4.2.2 nathanw int hastat; /* Host Adapter Status */
178 1.4.2.2 nathanw #define H_STATUS_GOOD 0x00
179 1.4.2.2 nathanw #define H_SEL_TIMEOUT 0x11
180 1.4.2.2 nathanw #define H_OVER_UNDER_RUN 0x12
181 1.4.2.2 nathanw #define H_UNEXP_BUS_FREE 0x13
182 1.4.2.2 nathanw #define H_TARGET_PHASE_F 0x14
183 1.4.2.2 nathanw #define H_INVALID_CCB_OP 0x16
184 1.4.2.2 nathanw #define H_LINK_CCB_BAD 0x17
185 1.4.2.2 nathanw #define H_BAD_TARGET_DIR 0x18
186 1.4.2.2 nathanw #define H_DUPLICATE_CCB 0x19
187 1.4.2.2 nathanw #define H_BAD_CCB_OR_SG 0x1A
188 1.4.2.2 nathanw #define H_ABORT 0xFF
189 1.4.2.2 nathanw int tastat; /* Target SCSI Status Byte */
190 1.4.2.2 nathanw int flag; /* SRBFlag */
191 1.4.2.2 nathanw #define AUTO_REQSENSE 0x0001
192 1.4.2.4 nathanw #define PARITY_ERROR 0x0002
193 1.4.2.4 nathanw #define SRB_TIMEOUT 0x0004
194 1.4.2.2 nathanw
195 1.4.2.2 nathanw int cmdlen; /* SCSI command length */
196 1.4.2.2 nathanw u_int8_t cmd[12]; /* SCSI command */
197 1.4.2.2 nathanw
198 1.4.2.4 nathanw u_int8_t tag[2];
199 1.4.2.2 nathanw };
200 1.4.2.2 nathanw
201 1.4.2.2 nathanw /*
202 1.4.2.4 nathanw * some info about each target and lun on the SCSI bus
203 1.4.2.2 nathanw */
204 1.4.2.4 nathanw struct trm_linfo {
205 1.4.2.4 nathanw int used; /* number of slots in use */
206 1.4.2.4 nathanw int avail; /* where to start scanning */
207 1.4.2.4 nathanw int busy; /* lun in use */
208 1.4.2.4 nathanw struct trm_srb *untagged;
209 1.4.2.4 nathanw struct trm_srb *queued[TRM_MAX_TAG];
210 1.4.2.4 nathanw };
211 1.4.2.2 nathanw
212 1.4.2.4 nathanw struct trm_tinfo {
213 1.4.2.4 nathanw u_int flag; /* Sync mode ? (1 sync):(0 async) */
214 1.4.2.4 nathanw #define SYNC_NEGO_ENABLE 0x0001
215 1.4.2.4 nathanw #define SYNC_NEGO_DOING 0x0002
216 1.4.2.4 nathanw #define SYNC_NEGO_DONE 0x0004
217 1.4.2.4 nathanw #define WIDE_NEGO_ENABLE 0x0008
218 1.4.2.4 nathanw #define WIDE_NEGO_DOING 0x0010
219 1.4.2.4 nathanw #define WIDE_NEGO_DONE 0x0020
220 1.4.2.4 nathanw #define USE_TAG_QUEUING 0x0040
221 1.4.2.4 nathanw #define NO_RESELECT 0x0080
222 1.4.2.4 nathanw struct trm_linfo *linfo[TRM_MAX_LUNS];
223 1.4.2.2 nathanw
224 1.4.2.4 nathanw u_int8_t config0; /* Target Config */
225 1.4.2.2 nathanw u_int8_t period; /* Max Period for nego. */
226 1.4.2.2 nathanw u_int8_t synctl; /* Sync control for reg. */
227 1.4.2.2 nathanw u_int8_t offset; /* Sync offset for reg. and nego.(low nibble) */
228 1.4.2.2 nathanw };
229 1.4.2.2 nathanw
230 1.4.2.2 nathanw /*
231 1.4.2.2 nathanw *-----------------------------------------------------------------------
232 1.4.2.2 nathanw * Adapter Control Block
233 1.4.2.2 nathanw *-----------------------------------------------------------------------
234 1.4.2.2 nathanw */
235 1.4.2.2 nathanw struct trm_softc {
236 1.4.2.2 nathanw struct device sc_dev;
237 1.4.2.2 nathanw
238 1.4.2.2 nathanw bus_space_tag_t sc_iot;
239 1.4.2.2 nathanw bus_space_handle_t sc_ioh;
240 1.4.2.2 nathanw bus_dma_tag_t sc_dmat;
241 1.4.2.2 nathanw bus_dmamap_t sc_dmamap; /* Map the control structures */
242 1.4.2.2 nathanw
243 1.4.2.4 nathanw struct trm_srb *sc_actsrb;
244 1.4.2.4 nathanw struct trm_tinfo sc_tinfo[TRM_MAX_TARGETS];
245 1.4.2.2 nathanw
246 1.4.2.4 nathanw TAILQ_HEAD(, trm_srb) sc_freesrb,
247 1.4.2.4 nathanw sc_readysrb;
248 1.4.2.2 nathanw struct trm_srb *sc_srb; /* SRB array */
249 1.4.2.2 nathanw
250 1.4.2.2 nathanw struct trm_sg_entry *sc_sglist;
251 1.4.2.2 nathanw
252 1.4.2.4 nathanw int sc_maxid;
253 1.4.2.2 nathanw /*
254 1.4.2.2 nathanw * Link to the generic SCSI driver
255 1.4.2.2 nathanw */
256 1.4.2.2 nathanw struct scsipi_channel sc_channel;
257 1.4.2.2 nathanw struct scsipi_adapter sc_adapter;
258 1.4.2.2 nathanw
259 1.4.2.2 nathanw int sc_id; /* Adapter SCSI Target ID */
260 1.4.2.2 nathanw
261 1.4.2.4 nathanw int sc_state; /* SRB State */
262 1.4.2.4 nathanw #define TRM_IDLE 0
263 1.4.2.4 nathanw #define TRM_WAIT 1
264 1.4.2.4 nathanw #define TRM_READY 2
265 1.4.2.4 nathanw #define TRM_MSGOUT 3 /* arbitration+msg_out 1st byte */
266 1.4.2.4 nathanw #define TRM_MSGIN 4
267 1.4.2.4 nathanw #define TRM_EXTEND_MSGIN 5
268 1.4.2.4 nathanw #define TRM_COMMAND 6
269 1.4.2.4 nathanw #define TRM_START 7 /* arbitration+msg_out+command_out */
270 1.4.2.4 nathanw #define TRM_DISCONNECTED 8
271 1.4.2.4 nathanw #define TRM_DATA_XFER 9
272 1.4.2.4 nathanw #define TRM_XFERPAD 10
273 1.4.2.4 nathanw #define TRM_STATUS 11
274 1.4.2.4 nathanw #define TRM_COMPLETED 12
275 1.4.2.4 nathanw #define TRM_ABORT_SENT 13
276 1.4.2.4 nathanw #define TRM_UNEXPECT_RESEL 14
277 1.4.2.2 nathanw
278 1.4.2.4 nathanw int sc_phase; /* SCSI phase */
279 1.4.2.2 nathanw int sc_config;
280 1.4.2.4 nathanw #define HCC_WIDE_CARD 0x01
281 1.4.2.4 nathanw #define HCC_SCSI_RESET 0x02
282 1.4.2.4 nathanw #define HCC_PARITY 0x04
283 1.4.2.4 nathanw #define HCC_AUTOTERM 0x08
284 1.4.2.4 nathanw #define HCC_LOW8TERM 0x10
285 1.4.2.4 nathanw #define HCC_UP8TERM 0x20
286 1.4.2.4 nathanw
287 1.4.2.2 nathanw int sc_flag;
288 1.4.2.2 nathanw #define RESET_DEV 0x01
289 1.4.2.2 nathanw #define RESET_DETECT 0x02
290 1.4.2.2 nathanw #define RESET_DONE 0x04
291 1.4.2.4 nathanw #define WAIT_TAGMSG 0x08 /* XXX */
292 1.4.2.4 nathanw
293 1.4.2.4 nathanw int sc_msgcnt;
294 1.4.2.4 nathanw
295 1.4.2.4 nathanw int resel_target; /* XXX */
296 1.4.2.4 nathanw int resel_lun; /* XXX */
297 1.4.2.4 nathanw
298 1.4.2.4 nathanw u_int8_t *sc_msg;
299 1.4.2.4 nathanw u_int8_t sc_msgbuf[6];
300 1.4.2.2 nathanw };
301 1.4.2.2 nathanw
302 1.4.2.2 nathanw /*
303 1.4.2.2 nathanw * SCSI Status codes not defined in scsi_all.h
304 1.4.2.2 nathanw */
305 1.4.2.2 nathanw #define SCSI_COND_MET 0x04 /* Condition Met */
306 1.4.2.2 nathanw #define SCSI_INTERM_COND_MET 0x14 /* Intermediate condition met */
307 1.4.2.4 nathanw #define SCSI_UNEXP_BUS_FREE 0xFD /* Unexpected Bus Free */
308 1.4.2.4 nathanw #define SCSI_BUS_RST_DETECT 0xFE /* SCSI Bus Reset detected */
309 1.4.2.4 nathanw #define SCSI_SEL_TIMEOUT 0xFF /* Selection Timeout */
310 1.4.2.4 nathanw
311 1.4.2.4 nathanw static int trm_probe(struct device *, struct cfdata *, void *);
312 1.4.2.4 nathanw static void trm_attach(struct device *, struct device *, void *);
313 1.4.2.4 nathanw
314 1.4.2.4 nathanw static int trm_init(struct trm_softc *);
315 1.4.2.4 nathanw
316 1.4.2.4 nathanw static void trm_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
317 1.4.2.4 nathanw void *);
318 1.4.2.4 nathanw static void trm_update_xfer_mode(struct trm_softc *, int);
319 1.4.2.4 nathanw static void trm_sched(struct trm_softc *);
320 1.4.2.4 nathanw static int trm_select(struct trm_softc *, struct trm_srb *);
321 1.4.2.4 nathanw static void trm_reset(struct trm_softc *);
322 1.4.2.4 nathanw static void trm_timeout(void *);
323 1.4.2.2 nathanw static int trm_intr(void *);
324 1.4.2.2 nathanw
325 1.4.2.4 nathanw static void trm_dataout_phase0(struct trm_softc *, int);
326 1.4.2.4 nathanw static void trm_datain_phase0(struct trm_softc *, int);
327 1.4.2.4 nathanw static void trm_status_phase0(struct trm_softc *);
328 1.4.2.4 nathanw static void trm_msgin_phase0(struct trm_softc *);
329 1.4.2.4 nathanw static void trm_command_phase1(struct trm_softc *);
330 1.4.2.4 nathanw static void trm_status_phase1(struct trm_softc *);
331 1.4.2.4 nathanw static void trm_msgout_phase1(struct trm_softc *);
332 1.4.2.4 nathanw static void trm_msgin_phase1(struct trm_softc *);
333 1.4.2.4 nathanw
334 1.4.2.4 nathanw static void trm_dataio_xfer(struct trm_softc *, int);
335 1.4.2.2 nathanw static void trm_disconnect(struct trm_softc *);
336 1.4.2.2 nathanw static void trm_reselect(struct trm_softc *);
337 1.4.2.4 nathanw static void trm_done(struct trm_softc *, struct trm_srb *);
338 1.4.2.4 nathanw static int trm_request_sense(struct trm_softc *, struct trm_srb *);
339 1.4.2.4 nathanw static void trm_dequeue(struct trm_softc *, struct trm_srb *);
340 1.4.2.4 nathanw
341 1.4.2.2 nathanw static void trm_scsi_reset_detect(struct trm_softc *);
342 1.4.2.2 nathanw static void trm_reset_scsi_bus(struct trm_softc *);
343 1.4.2.2 nathanw
344 1.4.2.4 nathanw static void trm_check_eeprom(struct trm_softc *, struct trm_nvram *);
345 1.4.2.2 nathanw static void trm_eeprom_read_all(struct trm_softc *, struct trm_nvram *);
346 1.4.2.2 nathanw static void trm_eeprom_write_all(struct trm_softc *, struct trm_nvram *);
347 1.4.2.2 nathanw static void trm_eeprom_set_data(struct trm_softc *, u_int8_t, u_int8_t);
348 1.4.2.2 nathanw static void trm_eeprom_write_cmd(struct trm_softc *, u_int8_t, u_int8_t);
349 1.4.2.2 nathanw static u_int8_t trm_eeprom_get_data(struct trm_softc *, u_int8_t);
350 1.4.2.2 nathanw
351 1.4.2.6 nathanw CFATTACH_DECL(trm, sizeof(struct trm_softc),
352 1.4.2.6 nathanw trm_probe, trm_attach, NULL, NULL);
353 1.4.2.2 nathanw
354 1.4.2.2 nathanw /* real period: */
355 1.4.2.2 nathanw static const u_int8_t trm_clock_period[] = {
356 1.4.2.4 nathanw 12, /* 48 ns 20.0 MB/sec */
357 1.4.2.4 nathanw 18, /* 72 ns 13.3 MB/sec */
358 1.4.2.4 nathanw 25, /* 100 ns 10.0 MB/sec */
359 1.4.2.4 nathanw 31, /* 124 ns 8.0 MB/sec */
360 1.4.2.4 nathanw 37, /* 148 ns 6.6 MB/sec */
361 1.4.2.4 nathanw 43, /* 172 ns 5.7 MB/sec */
362 1.4.2.4 nathanw 50, /* 200 ns 5.0 MB/sec */
363 1.4.2.4 nathanw 62 /* 248 ns 4.0 MB/sec */
364 1.4.2.2 nathanw };
365 1.4.2.4 nathanw #define NPERIOD (sizeof(trm_clock_period)/sizeof(trm_clock_period[0]))
366 1.4.2.4 nathanw
367 1.4.2.4 nathanw static int
368 1.4.2.4 nathanw trm_probe(parent, match, aux)
369 1.4.2.4 nathanw struct device *parent;
370 1.4.2.4 nathanw struct cfdata *match;
371 1.4.2.4 nathanw void *aux;
372 1.4.2.4 nathanw {
373 1.4.2.4 nathanw struct pci_attach_args *pa = aux;
374 1.4.2.4 nathanw
375 1.4.2.4 nathanw if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TEKRAM2)
376 1.4.2.4 nathanw switch (PCI_PRODUCT(pa->pa_id)) {
377 1.4.2.4 nathanw case PCI_PRODUCT_TEKRAM2_DC315:
378 1.4.2.4 nathanw return (1);
379 1.4.2.4 nathanw }
380 1.4.2.4 nathanw return (0);
381 1.4.2.4 nathanw }
382 1.4.2.2 nathanw
383 1.4.2.2 nathanw /*
384 1.4.2.4 nathanw * attach and init a host adapter
385 1.4.2.2 nathanw */
386 1.4.2.2 nathanw static void
387 1.4.2.4 nathanw trm_attach(parent, self, aux)
388 1.4.2.4 nathanw struct device *parent;
389 1.4.2.4 nathanw struct device *self;
390 1.4.2.4 nathanw void *aux;
391 1.4.2.2 nathanw {
392 1.4.2.4 nathanw struct pci_attach_args *const pa = aux;
393 1.4.2.4 nathanw struct trm_softc *sc = (struct trm_softc *)self;
394 1.4.2.4 nathanw bus_space_tag_t iot;
395 1.4.2.4 nathanw bus_space_handle_t ioh;
396 1.4.2.4 nathanw pci_intr_handle_t ih;
397 1.4.2.4 nathanw pcireg_t command;
398 1.4.2.4 nathanw const char *intrstr;
399 1.4.2.2 nathanw
400 1.4.2.4 nathanw /*
401 1.4.2.4 nathanw * These cards do not allow memory mapped accesses
402 1.4.2.4 nathanw * pa_pc: chipset tag
403 1.4.2.4 nathanw * pa_tag: pci tag
404 1.4.2.4 nathanw */
405 1.4.2.4 nathanw command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
406 1.4.2.4 nathanw if ((command & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE)) !=
407 1.4.2.4 nathanw (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
408 1.4.2.4 nathanw command |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
409 1.4.2.4 nathanw pci_conf_write(pa->pa_pc, pa->pa_tag,
410 1.4.2.4 nathanw PCI_COMMAND_STATUS_REG, command);
411 1.4.2.4 nathanw }
412 1.4.2.4 nathanw /*
413 1.4.2.4 nathanw * mask for get correct base address of pci IO port
414 1.4.2.4 nathanw */
415 1.4.2.4 nathanw if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
416 1.4.2.4 nathanw &iot, &ioh, NULL, NULL)) {
417 1.4.2.4 nathanw printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
418 1.4.2.4 nathanw return;
419 1.4.2.4 nathanw }
420 1.4.2.4 nathanw /*
421 1.4.2.4 nathanw * test checksum of eeprom.. & initialize softc...
422 1.4.2.4 nathanw */
423 1.4.2.4 nathanw sc->sc_iot = iot;
424 1.4.2.4 nathanw sc->sc_ioh = ioh;
425 1.4.2.4 nathanw sc->sc_dmat = pa->pa_dmat;
426 1.4.2.2 nathanw
427 1.4.2.4 nathanw if (trm_init(sc) != 0) {
428 1.4.2.4 nathanw /*
429 1.4.2.4 nathanw * Error during initialization!
430 1.4.2.4 nathanw */
431 1.4.2.4 nathanw printf(": Error during initialization\n");
432 1.4.2.4 nathanw return;
433 1.4.2.4 nathanw }
434 1.4.2.4 nathanw /*
435 1.4.2.4 nathanw * Now try to attach all the sub-devices
436 1.4.2.4 nathanw */
437 1.4.2.4 nathanw if ((sc->sc_config & HCC_WIDE_CARD) != 0)
438 1.4.2.4 nathanw printf(": Tekram DC395UW/F (TRM-S1040) Fast40 "
439 1.4.2.4 nathanw "Ultra Wide SCSI Adapter\n");
440 1.4.2.4 nathanw else
441 1.4.2.4 nathanw printf(": Tekram DC395U, DC315/U (TRM-S1040) Fast20 "
442 1.4.2.4 nathanw "Ultra SCSI Adapter\n");
443 1.4.2.2 nathanw
444 1.4.2.4 nathanw /*
445 1.4.2.4 nathanw * Now tell the generic SCSI layer about our bus.
446 1.4.2.4 nathanw * map and establish interrupt
447 1.4.2.4 nathanw */
448 1.4.2.4 nathanw if (pci_intr_map(pa, &ih)) {
449 1.4.2.4 nathanw printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
450 1.4.2.4 nathanw return;
451 1.4.2.2 nathanw }
452 1.4.2.4 nathanw intrstr = pci_intr_string(pa->pa_pc, ih);
453 1.4.2.2 nathanw
454 1.4.2.4 nathanw if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, trm_intr, sc) == NULL) {
455 1.4.2.4 nathanw printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
456 1.4.2.4 nathanw if (intrstr != NULL)
457 1.4.2.4 nathanw printf(" at %s", intrstr);
458 1.4.2.4 nathanw printf("\n");
459 1.4.2.4 nathanw return;
460 1.4.2.4 nathanw }
461 1.4.2.4 nathanw if (intrstr != NULL)
462 1.4.2.4 nathanw printf("%s: interrupting at %s\n",
463 1.4.2.4 nathanw sc->sc_dev.dv_xname, intrstr);
464 1.4.2.4 nathanw
465 1.4.2.4 nathanw sc->sc_adapter.adapt_dev = &sc->sc_dev;
466 1.4.2.4 nathanw sc->sc_adapter.adapt_nchannels = 1;
467 1.4.2.4 nathanw sc->sc_adapter.adapt_openings = TRM_MAX_SRB;
468 1.4.2.4 nathanw sc->sc_adapter.adapt_max_periph = TRM_MAX_SRB;
469 1.4.2.4 nathanw sc->sc_adapter.adapt_request = trm_scsipi_request;
470 1.4.2.4 nathanw sc->sc_adapter.adapt_minphys = minphys;
471 1.4.2.4 nathanw
472 1.4.2.4 nathanw sc->sc_channel.chan_adapter = &sc->sc_adapter;
473 1.4.2.4 nathanw sc->sc_channel.chan_bustype = &scsi_bustype;
474 1.4.2.4 nathanw sc->sc_channel.chan_channel = 0;
475 1.4.2.4 nathanw sc->sc_channel.chan_ntargets = sc->sc_maxid + 1;
476 1.4.2.4 nathanw sc->sc_channel.chan_nluns = 8;
477 1.4.2.4 nathanw sc->sc_channel.chan_id = sc->sc_id;
478 1.4.2.4 nathanw
479 1.4.2.4 nathanw config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
480 1.4.2.2 nathanw }
481 1.4.2.2 nathanw
482 1.4.2.4 nathanw /*
483 1.4.2.4 nathanw * initialize the internal structures for a given SCSI host
484 1.4.2.4 nathanw */
485 1.4.2.4 nathanw static int
486 1.4.2.4 nathanw trm_init(sc)
487 1.4.2.2 nathanw struct trm_softc *sc;
488 1.4.2.2 nathanw {
489 1.4.2.4 nathanw bus_space_tag_t iot = sc->sc_iot;
490 1.4.2.4 nathanw bus_space_handle_t ioh = sc->sc_ioh;
491 1.4.2.4 nathanw bus_dma_segment_t seg;
492 1.4.2.4 nathanw struct trm_nvram eeprom;
493 1.4.2.2 nathanw struct trm_srb *srb;
494 1.4.2.4 nathanw struct trm_tinfo *ti;
495 1.4.2.4 nathanw struct nvram_target *tconf;
496 1.4.2.4 nathanw int error, rseg, all_sgsize;
497 1.4.2.4 nathanw int i, target;
498 1.4.2.4 nathanw u_int8_t bval;
499 1.4.2.2 nathanw
500 1.4.2.4 nathanw DPRINTF(("\n"));
501 1.4.2.2 nathanw
502 1.4.2.4 nathanw /*
503 1.4.2.4 nathanw * allocate the space for all SCSI control blocks (SRB) for DMA memory
504 1.4.2.4 nathanw */
505 1.4.2.4 nathanw all_sgsize = TRM_MAX_SRB * TRM_SG_SIZE;
506 1.4.2.4 nathanw if ((error = bus_dmamem_alloc(sc->sc_dmat, all_sgsize, PAGE_SIZE,
507 1.4.2.4 nathanw 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
508 1.4.2.4 nathanw printf(": unable to allocate SCSI REQUEST BLOCKS, "
509 1.4.2.4 nathanw "error = %d\n", error);
510 1.4.2.4 nathanw return (1);
511 1.4.2.2 nathanw }
512 1.4.2.4 nathanw if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
513 1.4.2.4 nathanw all_sgsize, (caddr_t *) &sc->sc_sglist,
514 1.4.2.4 nathanw BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
515 1.4.2.4 nathanw printf(": unable to map SCSI REQUEST BLOCKS, "
516 1.4.2.4 nathanw "error = %d\n", error);
517 1.4.2.4 nathanw return (1);
518 1.4.2.4 nathanw }
519 1.4.2.4 nathanw if ((error = bus_dmamap_create(sc->sc_dmat, all_sgsize, 1,
520 1.4.2.4 nathanw all_sgsize, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
521 1.4.2.4 nathanw printf(": unable to create SRB DMA maps, "
522 1.4.2.4 nathanw "error = %d\n", error);
523 1.4.2.4 nathanw return (1);
524 1.4.2.4 nathanw }
525 1.4.2.4 nathanw if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
526 1.4.2.4 nathanw sc->sc_sglist, all_sgsize, NULL, BUS_DMA_NOWAIT)) != 0) {
527 1.4.2.4 nathanw printf(": unable to load SRB DMA maps, "
528 1.4.2.4 nathanw "error = %d\n", error);
529 1.4.2.4 nathanw return (1);
530 1.4.2.4 nathanw }
531 1.4.2.4 nathanw DPRINTF(("all_sgsize=%x\n", all_sgsize));
532 1.4.2.4 nathanw memset(sc->sc_sglist, 0, all_sgsize);
533 1.4.2.2 nathanw
534 1.4.2.4 nathanw /*
535 1.4.2.4 nathanw * EEPROM CHECKSUM
536 1.4.2.4 nathanw */
537 1.4.2.4 nathanw trm_check_eeprom(sc, &eeprom);
538 1.4.2.2 nathanw
539 1.4.2.4 nathanw sc->sc_maxid = 7;
540 1.4.2.4 nathanw sc->sc_config = HCC_AUTOTERM | HCC_PARITY;
541 1.4.2.4 nathanw if (bus_space_read_1(iot, ioh, TRM_GEN_STATUS) & WIDESCSI) {
542 1.4.2.4 nathanw sc->sc_config |= HCC_WIDE_CARD;
543 1.4.2.4 nathanw sc->sc_maxid = 15;
544 1.4.2.4 nathanw }
545 1.4.2.4 nathanw if (eeprom.channel_cfg & NAC_POWERON_SCSI_RESET)
546 1.4.2.4 nathanw sc->sc_config |= HCC_SCSI_RESET;
547 1.4.2.4 nathanw
548 1.4.2.4 nathanw sc->sc_actsrb = NULL;
549 1.4.2.4 nathanw sc->sc_id = eeprom.scsi_id;
550 1.4.2.4 nathanw sc->sc_flag = 0;
551 1.4.2.2 nathanw
552 1.4.2.2 nathanw /*
553 1.4.2.4 nathanw * initialize and link all device's SRB queues of this adapter
554 1.4.2.2 nathanw */
555 1.4.2.4 nathanw TAILQ_INIT(&sc->sc_freesrb);
556 1.4.2.4 nathanw TAILQ_INIT(&sc->sc_readysrb);
557 1.4.2.2 nathanw
558 1.4.2.4 nathanw sc->sc_srb = malloc(sizeof(struct trm_srb) * TRM_MAX_SRB,
559 1.4.2.4 nathanw M_DEVBUF, M_NOWAIT|M_ZERO);
560 1.4.2.4 nathanw DPRINTF(("all SRB size=%x\n", sizeof(struct trm_srb) * TRM_MAX_SRB));
561 1.4.2.4 nathanw if (sc->sc_srb == NULL) {
562 1.4.2.4 nathanw printf(": can not allocate SRB\n");
563 1.4.2.4 nathanw return (1);
564 1.4.2.2 nathanw }
565 1.4.2.4 nathanw
566 1.4.2.4 nathanw for (i = 0, srb = sc->sc_srb; i < TRM_MAX_SRB; i++) {
567 1.4.2.4 nathanw srb->sgentry = sc->sc_sglist + TRM_MAX_SG_ENTRIES * i;
568 1.4.2.4 nathanw srb->sgoffset = TRM_SG_SIZE * i;
569 1.4.2.4 nathanw srb->sgaddr = sc->sc_dmamap->dm_segs[0].ds_addr + srb->sgoffset;
570 1.4.2.2 nathanw /*
571 1.4.2.4 nathanw * map all SRB space to SRB_array
572 1.4.2.2 nathanw */
573 1.4.2.4 nathanw if (bus_dmamap_create(sc->sc_dmat,
574 1.4.2.4 nathanw MAXPHYS, TRM_MAX_SG_ENTRIES, MAXPHYS, 0,
575 1.4.2.4 nathanw BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &srb->dmap)) {
576 1.4.2.5 nathanw printf(": unable to create DMA transfer map...\n");
577 1.4.2.4 nathanw free(sc->sc_srb, M_DEVBUF);
578 1.4.2.4 nathanw return (1);
579 1.4.2.2 nathanw }
580 1.4.2.4 nathanw TAILQ_INSERT_TAIL(&sc->sc_freesrb, srb, next);
581 1.4.2.4 nathanw srb++;
582 1.4.2.2 nathanw }
583 1.4.2.2 nathanw
584 1.4.2.4 nathanw /*
585 1.4.2.4 nathanw * initialize all target info structures
586 1.4.2.4 nathanw */
587 1.4.2.4 nathanw for (target = 0; target < TRM_MAX_TARGETS; target++) {
588 1.4.2.4 nathanw ti = &sc->sc_tinfo[target];
589 1.4.2.4 nathanw ti->synctl = 0;
590 1.4.2.4 nathanw ti->offset = 0;
591 1.4.2.4 nathanw tconf = &eeprom.target[target];
592 1.4.2.4 nathanw ti->config0 = tconf->config0;
593 1.4.2.4 nathanw ti->period = trm_clock_period[tconf->period & 0x07];
594 1.4.2.4 nathanw ti->flag = 0;
595 1.4.2.4 nathanw if ((ti->config0 & NTC_DO_WIDE_NEGO) != 0 &&
596 1.4.2.4 nathanw (sc->sc_config & HCC_WIDE_CARD) != 0)
597 1.4.2.4 nathanw ti->flag |= WIDE_NEGO_ENABLE;
598 1.4.2.4 nathanw if ((ti->config0 & NTC_DO_SYNC_NEGO) != 0)
599 1.4.2.4 nathanw ti->flag |= SYNC_NEGO_ENABLE;
600 1.4.2.4 nathanw if ((ti->config0 & NTC_DO_DISCONNECT) != 0) {
601 1.4.2.4 nathanw #ifdef notyet
602 1.4.2.4 nathanw if ((ti->config0 & NTC_DO_TAG_QUEUING) != 0)
603 1.4.2.4 nathanw ti->flag |= USE_TAG_QUEUING;
604 1.4.2.4 nathanw #endif
605 1.4.2.4 nathanw } else
606 1.4.2.4 nathanw ti->flag |= NO_RESELECT;
607 1.4.2.4 nathanw
608 1.4.2.4 nathanw DPRINTF(("target %d: config0 = 0x%02x, period = 0x%02x",
609 1.4.2.4 nathanw target, ti->config0, ti->period));
610 1.4.2.4 nathanw DPRINTF((", flag = 0x%02x\n", ti->flag));
611 1.4.2.4 nathanw }
612 1.4.2.2 nathanw
613 1.4.2.4 nathanw /* program configuration 0 */
614 1.4.2.4 nathanw bval = PHASELATCH | INITIATOR | BLOCKRST;
615 1.4.2.4 nathanw if ((sc->sc_config & HCC_PARITY) != 0)
616 1.4.2.4 nathanw bval |= PARITYCHECK;
617 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_CONFIG0, bval);
618 1.4.2.4 nathanw
619 1.4.2.4 nathanw /* program configuration 1 */
620 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_CONFIG1,
621 1.4.2.4 nathanw ACTIVE_NEG | ACTIVE_NEGPLUS);
622 1.4.2.4 nathanw
623 1.4.2.4 nathanw /* 250ms selection timeout */
624 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_TIMEOUT, SEL_TIMEOUT);
625 1.4.2.4 nathanw
626 1.4.2.4 nathanw /* Mask all interrupts */
627 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_DMA_INTEN, 0);
628 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_INTEN, 0);
629 1.4.2.4 nathanw
630 1.4.2.4 nathanw /* Reset SCSI module */
631 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_RSTMODULE);
632 1.4.2.4 nathanw
633 1.4.2.4 nathanw /* program Host ID */
634 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_HOSTID, sc->sc_id);
635 1.4.2.4 nathanw
636 1.4.2.4 nathanw /* set asynchronous transfer */
637 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_OFFSET, 0);
638 1.4.2.4 nathanw
639 1.4.2.4 nathanw /* Turn LED control off */
640 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_GEN_CONTROL,
641 1.4.2.4 nathanw bus_space_read_2(iot, ioh, TRM_GEN_CONTROL) & ~EN_LED);
642 1.4.2.4 nathanw
643 1.4.2.4 nathanw /* DMA config */
644 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_DMA_CONFIG,
645 1.4.2.4 nathanw bus_space_read_2(iot, ioh, TRM_DMA_CONFIG) | DMA_ENHANCE);
646 1.4.2.4 nathanw
647 1.4.2.4 nathanw /* Clear pending interrupt status */
648 1.4.2.4 nathanw bus_space_read_1(iot, ioh, TRM_SCSI_INTSTATUS);
649 1.4.2.4 nathanw
650 1.4.2.4 nathanw /* Enable SCSI interrupt */
651 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_INTEN,
652 1.4.2.4 nathanw EN_SELECT | EN_SELTIMEOUT | EN_DISCONNECT | EN_RESELECTED |
653 1.4.2.4 nathanw EN_SCSIRESET | EN_BUSSERVICE | EN_CMDDONE);
654 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_DMA_INTEN, EN_SCSIINTR);
655 1.4.2.4 nathanw
656 1.4.2.4 nathanw trm_reset(sc);
657 1.4.2.4 nathanw
658 1.4.2.4 nathanw return (0);
659 1.4.2.4 nathanw }
660 1.4.2.4 nathanw
661 1.4.2.4 nathanw /*
662 1.4.2.4 nathanw * enqueues a SCSI command
663 1.4.2.4 nathanw * called by the higher level SCSI driver
664 1.4.2.4 nathanw */
665 1.4.2.4 nathanw static void
666 1.4.2.4 nathanw trm_scsipi_request(chan, req, arg)
667 1.4.2.4 nathanw struct scsipi_channel *chan;
668 1.4.2.4 nathanw scsipi_adapter_req_t req;
669 1.4.2.4 nathanw void *arg;
670 1.4.2.4 nathanw {
671 1.4.2.4 nathanw bus_space_tag_t iot;
672 1.4.2.4 nathanw bus_space_handle_t ioh;
673 1.4.2.2 nathanw struct trm_softc *sc;
674 1.4.2.2 nathanw struct trm_srb *srb;
675 1.4.2.2 nathanw struct scsipi_xfer *xs;
676 1.4.2.4 nathanw int error, i, target, lun, s;
677 1.4.2.2 nathanw
678 1.4.2.2 nathanw sc = (struct trm_softc *)chan->chan_adapter->adapt_dev;
679 1.4.2.2 nathanw iot = sc->sc_iot;
680 1.4.2.2 nathanw ioh = sc->sc_ioh;
681 1.4.2.2 nathanw
682 1.4.2.2 nathanw switch (req) {
683 1.4.2.2 nathanw case ADAPTER_REQ_RUN_XFER:
684 1.4.2.2 nathanw xs = arg;
685 1.4.2.4 nathanw target = xs->xs_periph->periph_target;
686 1.4.2.2 nathanw lun = xs->xs_periph->periph_lun;
687 1.4.2.4 nathanw DPRINTF(("trm_scsipi_request.....\n"));
688 1.4.2.4 nathanw DPRINTF(("target= %d lun= %d\n", target, lun));
689 1.4.2.2 nathanw if (xs->xs_control & XS_CTL_RESET) {
690 1.4.2.2 nathanw trm_reset(sc);
691 1.4.2.2 nathanw xs->error = XS_NOERROR | XS_RESET;
692 1.4.2.2 nathanw return;
693 1.4.2.2 nathanw }
694 1.4.2.2 nathanw if (xs->xs_status & XS_STS_DONE) {
695 1.4.2.2 nathanw printf("%s: Is it done?\n", sc->sc_dev.dv_xname);
696 1.4.2.2 nathanw xs->xs_status &= ~XS_STS_DONE;
697 1.4.2.2 nathanw }
698 1.4.2.2 nathanw
699 1.4.2.2 nathanw s = splbio();
700 1.4.2.2 nathanw
701 1.4.2.2 nathanw /* Get SRB */
702 1.4.2.4 nathanw srb = TAILQ_FIRST(&sc->sc_freesrb);
703 1.4.2.2 nathanw if (srb != NULL) {
704 1.4.2.4 nathanw TAILQ_REMOVE(&sc->sc_freesrb, srb, next);
705 1.4.2.2 nathanw } else {
706 1.4.2.2 nathanw xs->error = XS_RESOURCE_SHORTAGE;
707 1.4.2.2 nathanw scsipi_done(xs);
708 1.4.2.2 nathanw splx(s);
709 1.4.2.2 nathanw return;
710 1.4.2.2 nathanw }
711 1.4.2.4 nathanw
712 1.4.2.2 nathanw srb->xs = xs;
713 1.4.2.2 nathanw srb->cmdlen = xs->cmdlen;
714 1.4.2.2 nathanw memcpy(srb->cmd, xs->cmd, xs->cmdlen);
715 1.4.2.4 nathanw
716 1.4.2.3 nathanw if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
717 1.4.2.2 nathanw if ((error = bus_dmamap_load(sc->sc_dmat, srb->dmap,
718 1.4.2.2 nathanw xs->data, xs->datalen, NULL,
719 1.4.2.3 nathanw ((xs->xs_control & XS_CTL_NOSLEEP) ?
720 1.4.2.3 nathanw BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
721 1.4.2.3 nathanw BUS_DMA_STREAMING |
722 1.4.2.3 nathanw ((xs->xs_control & XS_CTL_DATA_IN) ?
723 1.4.2.3 nathanw BUS_DMA_READ : BUS_DMA_WRITE))) != 0) {
724 1.4.2.2 nathanw printf("%s: DMA transfer map unable to load, "
725 1.4.2.2 nathanw "error = %d\n", sc->sc_dev.dv_xname, error);
726 1.4.2.2 nathanw xs->error = XS_DRIVER_STUFFUP;
727 1.4.2.2 nathanw /*
728 1.4.2.2 nathanw * free SRB
729 1.4.2.2 nathanw */
730 1.4.2.4 nathanw TAILQ_INSERT_TAIL(&sc->sc_freesrb, srb, next);
731 1.4.2.2 nathanw splx(s);
732 1.4.2.2 nathanw return;
733 1.4.2.2 nathanw }
734 1.4.2.2 nathanw bus_dmamap_sync(sc->sc_dmat, srb->dmap, 0,
735 1.4.2.2 nathanw srb->dmap->dm_mapsize,
736 1.4.2.2 nathanw (xs->xs_control & XS_CTL_DATA_IN) ?
737 1.4.2.2 nathanw BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
738 1.4.2.2 nathanw
739 1.4.2.2 nathanw /* Set up the scatter gather list */
740 1.4.2.2 nathanw for (i = 0; i < srb->dmap->dm_nsegs; i++) {
741 1.4.2.2 nathanw srb->sgentry[i].address =
742 1.4.2.2 nathanw htole32(srb->dmap->dm_segs[i].ds_addr);
743 1.4.2.2 nathanw srb->sgentry[i].length =
744 1.4.2.2 nathanw htole32(srb->dmap->dm_segs[i].ds_len);
745 1.4.2.2 nathanw }
746 1.4.2.2 nathanw srb->buflen = xs->datalen;
747 1.4.2.2 nathanw srb->sgcnt = srb->dmap->dm_nsegs;
748 1.4.2.2 nathanw } else {
749 1.4.2.2 nathanw srb->sgentry[0].address = 0;
750 1.4.2.2 nathanw srb->sgentry[0].length = 0;
751 1.4.2.2 nathanw srb->buflen = 0;
752 1.4.2.2 nathanw srb->sgcnt = 0;
753 1.4.2.2 nathanw }
754 1.4.2.2 nathanw bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
755 1.4.2.2 nathanw srb->sgoffset, TRM_SG_SIZE, BUS_DMASYNC_PREWRITE);
756 1.4.2.2 nathanw
757 1.4.2.4 nathanw sc->sc_phase = PH_BUS_FREE; /* SCSI bus free Phase */
758 1.4.2.4 nathanw
759 1.4.2.2 nathanw srb->sgindex = 0;
760 1.4.2.2 nathanw srb->hastat = 0;
761 1.4.2.2 nathanw srb->tastat = 0;
762 1.4.2.2 nathanw srb->flag = 0;
763 1.4.2.2 nathanw
764 1.4.2.4 nathanw TAILQ_INSERT_TAIL(&sc->sc_readysrb, srb, next);
765 1.4.2.4 nathanw if (sc->sc_actsrb == NULL)
766 1.4.2.4 nathanw trm_sched(sc);
767 1.4.2.2 nathanw splx(s);
768 1.4.2.2 nathanw
769 1.4.2.4 nathanw if ((xs->xs_control & XS_CTL_POLL) != 0) {
770 1.4.2.2 nathanw int timeout = xs->timeout;
771 1.4.2.4 nathanw
772 1.4.2.2 nathanw s = splbio();
773 1.4.2.2 nathanw do {
774 1.4.2.4 nathanw while (--timeout) {
775 1.4.2.2 nathanw DELAY(1000);
776 1.4.2.2 nathanw if (bus_space_read_2(iot, ioh,
777 1.4.2.2 nathanw TRM_SCSI_STATUS) & SCSIINTERRUPT)
778 1.4.2.2 nathanw break;
779 1.4.2.2 nathanw }
780 1.4.2.4 nathanw if (timeout == 0) {
781 1.4.2.2 nathanw trm_timeout(srb);
782 1.4.2.2 nathanw break;
783 1.4.2.2 nathanw } else
784 1.4.2.2 nathanw trm_intr(sc);
785 1.4.2.2 nathanw } while ((xs->xs_status & XS_STS_DONE) == 0);
786 1.4.2.2 nathanw splx(s);
787 1.4.2.2 nathanw }
788 1.4.2.2 nathanw return;
789 1.4.2.2 nathanw
790 1.4.2.2 nathanw case ADAPTER_REQ_GROW_RESOURCES:
791 1.4.2.2 nathanw /* XXX Not supported. */
792 1.4.2.2 nathanw return;
793 1.4.2.2 nathanw
794 1.4.2.2 nathanw case ADAPTER_REQ_SET_XFER_MODE:
795 1.4.2.4 nathanw {
796 1.4.2.4 nathanw struct trm_tinfo *ti;
797 1.4.2.4 nathanw struct scsipi_xfer_mode *xm;
798 1.4.2.4 nathanw
799 1.4.2.4 nathanw xm = arg;
800 1.4.2.4 nathanw ti = &sc->sc_tinfo[xm->xm_target];
801 1.4.2.4 nathanw ti->flag &= ~(SYNC_NEGO_ENABLE|WIDE_NEGO_ENABLE);
802 1.4.2.4 nathanw
803 1.4.2.4 nathanw #ifdef notyet
804 1.4.2.4 nathanw if ((xm->xm_mode & PERIPH_CAP_TQING) != 0)
805 1.4.2.4 nathanw ti->flag |= USE_TAG_QUEUING;
806 1.4.2.4 nathanw else
807 1.4.2.4 nathanw #endif
808 1.4.2.4 nathanw ti->flag &= ~USE_TAG_QUEUING;
809 1.4.2.4 nathanw
810 1.4.2.4 nathanw if ((xm->xm_mode & PERIPH_CAP_WIDE16) != 0) {
811 1.4.2.4 nathanw ti->flag |= WIDE_NEGO_ENABLE;
812 1.4.2.4 nathanw ti->flag &= ~WIDE_NEGO_DONE;
813 1.4.2.4 nathanw }
814 1.4.2.4 nathanw
815 1.4.2.4 nathanw if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0) {
816 1.4.2.4 nathanw ti->flag |= SYNC_NEGO_ENABLE;
817 1.4.2.4 nathanw ti->flag &= ~SYNC_NEGO_DONE;
818 1.4.2.4 nathanw ti->period = trm_clock_period[0];
819 1.4.2.4 nathanw }
820 1.4.2.4 nathanw
821 1.4.2.4 nathanw /*
822 1.4.2.4 nathanw * If we're not going to negotiate, send the
823 1.4.2.4 nathanw * notification now, since it won't happen later.
824 1.4.2.4 nathanw */
825 1.4.2.4 nathanw if ((ti->flag & (WIDE_NEGO_DONE|SYNC_NEGO_DONE)) ==
826 1.4.2.4 nathanw (WIDE_NEGO_DONE|SYNC_NEGO_DONE))
827 1.4.2.4 nathanw trm_update_xfer_mode(sc, xm->xm_target);
828 1.4.2.4 nathanw
829 1.4.2.4 nathanw return;
830 1.4.2.4 nathanw }
831 1.4.2.2 nathanw }
832 1.4.2.2 nathanw }
833 1.4.2.2 nathanw
834 1.4.2.2 nathanw static void
835 1.4.2.4 nathanw trm_update_xfer_mode(sc, target)
836 1.4.2.2 nathanw struct trm_softc *sc;
837 1.4.2.4 nathanw int target;
838 1.4.2.2 nathanw {
839 1.4.2.4 nathanw struct scsipi_xfer_mode xm;
840 1.4.2.4 nathanw struct trm_tinfo *ti;
841 1.4.2.2 nathanw
842 1.4.2.4 nathanw ti = &sc->sc_tinfo[target];
843 1.4.2.4 nathanw xm.xm_target = target;
844 1.4.2.4 nathanw xm.xm_mode = 0;
845 1.4.2.4 nathanw xm.xm_period = 0;
846 1.4.2.4 nathanw xm.xm_offset = 0;
847 1.4.2.2 nathanw
848 1.4.2.4 nathanw if ((ti->synctl & WIDE_SYNC) != 0)
849 1.4.2.4 nathanw xm.xm_mode |= PERIPH_CAP_WIDE16;
850 1.4.2.2 nathanw
851 1.4.2.4 nathanw if (ti->period > 0) {
852 1.4.2.4 nathanw xm.xm_mode |= PERIPH_CAP_SYNC;
853 1.4.2.4 nathanw xm.xm_period = ti->period;
854 1.4.2.4 nathanw xm.xm_offset = ti->offset;
855 1.4.2.2 nathanw }
856 1.4.2.4 nathanw
857 1.4.2.4 nathanw #ifdef notyet
858 1.4.2.4 nathanw if ((ti->flag & USE_TAG_QUEUING) != 0)
859 1.4.2.4 nathanw xm.xm_mode |= PERIPH_CAP_TQING;
860 1.4.2.4 nathanw #endif
861 1.4.2.4 nathanw
862 1.4.2.4 nathanw scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
863 1.4.2.2 nathanw }
864 1.4.2.2 nathanw
865 1.4.2.2 nathanw static void
866 1.4.2.4 nathanw trm_sched(sc)
867 1.4.2.2 nathanw struct trm_softc *sc;
868 1.4.2.2 nathanw {
869 1.4.2.4 nathanw struct trm_srb *srb;
870 1.4.2.4 nathanw struct scsipi_periph *periph;
871 1.4.2.4 nathanw struct trm_tinfo *ti;
872 1.4.2.4 nathanw struct trm_linfo *li;
873 1.4.2.4 nathanw int s, lun, tag;
874 1.4.2.2 nathanw
875 1.4.2.4 nathanw DPRINTF(("trm_sched...\n"));
876 1.4.2.4 nathanw
877 1.4.2.4 nathanw TAILQ_FOREACH(srb, &sc->sc_readysrb, next) {
878 1.4.2.4 nathanw periph = srb->xs->xs_periph;
879 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
880 1.4.2.4 nathanw lun = periph->periph_lun;
881 1.4.2.4 nathanw
882 1.4.2.4 nathanw /* select type of tag for this command */
883 1.4.2.4 nathanw if ((ti->flag & NO_RESELECT) != 0 ||
884 1.4.2.4 nathanw (ti->flag & USE_TAG_QUEUING) == 0 ||
885 1.4.2.4 nathanw (srb->flag & AUTO_REQSENSE) != 0 ||
886 1.4.2.4 nathanw (srb->xs->xs_control & XS_CTL_REQSENSE) != 0)
887 1.4.2.4 nathanw tag = 0;
888 1.4.2.4 nathanw else
889 1.4.2.4 nathanw tag = srb->xs->xs_tag_type;
890 1.4.2.4 nathanw #if 0
891 1.4.2.4 nathanw /* XXX use tags for polled commands? */
892 1.4.2.4 nathanw if (srb->xs->xs_control & XS_CTL_POLL)
893 1.4.2.4 nathanw tag = 0;
894 1.4.2.4 nathanw #endif
895 1.4.2.4 nathanw
896 1.4.2.4 nathanw s = splbio();
897 1.4.2.4 nathanw li = ti->linfo[lun];
898 1.4.2.4 nathanw if (li == NULL) {
899 1.4.2.4 nathanw /* initialize lun info */
900 1.4.2.4 nathanw if ((li = malloc(sizeof(*li), M_DEVBUF,
901 1.4.2.4 nathanw M_NOWAIT|M_ZERO)) == NULL) {
902 1.4.2.4 nathanw splx(s);
903 1.4.2.4 nathanw continue;
904 1.4.2.4 nathanw }
905 1.4.2.4 nathanw ti->linfo[lun] = li;
906 1.4.2.4 nathanw }
907 1.4.2.2 nathanw
908 1.4.2.4 nathanw if (tag == 0) {
909 1.4.2.4 nathanw /* try to issue this srb as an un-tagged command */
910 1.4.2.4 nathanw if (li->untagged == NULL)
911 1.4.2.4 nathanw li->untagged = srb;
912 1.4.2.4 nathanw }
913 1.4.2.4 nathanw if (li->untagged != NULL) {
914 1.4.2.4 nathanw tag = 0;
915 1.4.2.4 nathanw if (li->busy != 1 && li->used == 0) {
916 1.4.2.4 nathanw /* we need to issue the untagged command now */
917 1.4.2.4 nathanw srb = li->untagged;
918 1.4.2.4 nathanw periph = srb->xs->xs_periph;
919 1.4.2.2 nathanw } else {
920 1.4.2.4 nathanw /* not ready yet */
921 1.4.2.4 nathanw splx(s);
922 1.4.2.4 nathanw continue;
923 1.4.2.2 nathanw }
924 1.4.2.2 nathanw }
925 1.4.2.4 nathanw srb->tag[0] = tag;
926 1.4.2.4 nathanw if (tag != 0) {
927 1.4.2.4 nathanw li->queued[srb->xs->xs_tag_id] = srb;
928 1.4.2.4 nathanw srb->tag[1] = srb->xs->xs_tag_id;
929 1.4.2.4 nathanw li->used++;
930 1.4.2.4 nathanw }
931 1.4.2.4 nathanw
932 1.4.2.4 nathanw if (li->untagged != NULL && li->busy != 1) {
933 1.4.2.4 nathanw li->busy = 1;
934 1.4.2.4 nathanw TAILQ_REMOVE(&sc->sc_readysrb, srb, next);
935 1.4.2.4 nathanw sc->sc_actsrb = srb;
936 1.4.2.4 nathanw trm_select(sc, srb);
937 1.4.2.4 nathanw splx(s);
938 1.4.2.4 nathanw break;
939 1.4.2.4 nathanw }
940 1.4.2.4 nathanw if (li->untagged == NULL && tag != 0) {
941 1.4.2.4 nathanw TAILQ_REMOVE(&sc->sc_readysrb, srb, next);
942 1.4.2.4 nathanw sc->sc_actsrb = srb;
943 1.4.2.4 nathanw trm_select(sc, srb);
944 1.4.2.4 nathanw splx(s);
945 1.4.2.4 nathanw break;
946 1.4.2.4 nathanw } else
947 1.4.2.4 nathanw splx(s);
948 1.4.2.4 nathanw }
949 1.4.2.4 nathanw }
950 1.4.2.4 nathanw
951 1.4.2.4 nathanw static int
952 1.4.2.4 nathanw trm_select(sc, srb)
953 1.4.2.4 nathanw struct trm_softc *sc;
954 1.4.2.4 nathanw struct trm_srb *srb;
955 1.4.2.4 nathanw {
956 1.4.2.4 nathanw bus_space_tag_t iot = sc->sc_iot;
957 1.4.2.4 nathanw bus_space_handle_t ioh = sc->sc_ioh;
958 1.4.2.4 nathanw struct scsipi_periph *periph = srb->xs->xs_periph;
959 1.4.2.4 nathanw int target = periph->periph_target;
960 1.4.2.4 nathanw int lun = periph->periph_lun;
961 1.4.2.4 nathanw struct trm_tinfo *ti = &sc->sc_tinfo[target];
962 1.4.2.4 nathanw u_int8_t scsicmd;
963 1.4.2.4 nathanw
964 1.4.2.4 nathanw DPRINTF(("trm_select.....\n"));
965 1.4.2.4 nathanw
966 1.4.2.4 nathanw if ((srb->xs->xs_control & XS_CTL_POLL) == 0) {
967 1.4.2.5 nathanw callout_reset(&srb->xs->xs_callout, mstohz(srb->xs->timeout),
968 1.4.2.5 nathanw trm_timeout, srb);
969 1.4.2.4 nathanw }
970 1.4.2.4 nathanw
971 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_HOSTID, sc->sc_id);
972 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_TARGETID, target);
973 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_SYNC, ti->synctl);
974 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_OFFSET, ti->offset);
975 1.4.2.4 nathanw /* Flush FIFO */
976 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRFIFO);
977 1.4.2.4 nathanw DELAY(10);
978 1.4.2.4 nathanw
979 1.4.2.4 nathanw sc->sc_phase = PH_BUS_FREE; /* initial phase */
980 1.4.2.4 nathanw
981 1.4.2.4 nathanw DPRINTF(("cmd = 0x%02x\n", srb->cmd[0]));
982 1.4.2.4 nathanw
983 1.4.2.4 nathanw if (((ti->flag & WIDE_NEGO_ENABLE) &&
984 1.4.2.4 nathanw (ti->flag & WIDE_NEGO_DONE) == 0) ||
985 1.4.2.4 nathanw ((ti->flag & SYNC_NEGO_ENABLE) &&
986 1.4.2.4 nathanw (ti->flag & SYNC_NEGO_DONE) == 0)) {
987 1.4.2.4 nathanw sc->sc_state = TRM_MSGOUT;
988 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_FIFO,
989 1.4.2.4 nathanw MSG_IDENTIFY(lun, 0));
990 1.4.2.4 nathanw bus_space_write_multi_1(iot, ioh,
991 1.4.2.4 nathanw TRM_SCSI_FIFO, srb->cmd, srb->cmdlen);
992 1.4.2.4 nathanw /* it's important for atn stop */
993 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL,
994 1.4.2.4 nathanw DO_DATALATCH | DO_HWRESELECT);
995 1.4.2.4 nathanw /* SCSI command */
996 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_SEL_ATNSTOP);
997 1.4.2.4 nathanw DPRINTF(("select with SEL_ATNSTOP\n"));
998 1.4.2.4 nathanw return (0);
999 1.4.2.4 nathanw }
1000 1.4.2.4 nathanw
1001 1.4.2.4 nathanw if (srb->tag[0] != 0) {
1002 1.4.2.4 nathanw /* Send identify message */
1003 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_FIFO,
1004 1.4.2.4 nathanw MSG_IDENTIFY(lun, 1));
1005 1.4.2.4 nathanw /* Send Tag id */
1006 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_FIFO, srb->tag[0]);
1007 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_FIFO, srb->tag[1]);
1008 1.4.2.4 nathanw scsicmd = SCMD_SEL_ATN3;
1009 1.4.2.4 nathanw DPRINTF(("select with SEL_ATN3\n"));
1010 1.4.2.4 nathanw } else {
1011 1.4.2.4 nathanw /* Send identify message */
1012 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_FIFO,
1013 1.4.2.4 nathanw MSG_IDENTIFY(lun,
1014 1.4.2.4 nathanw (ti->flag & NO_RESELECT) == 0 &&
1015 1.4.2.4 nathanw (srb->flag & AUTO_REQSENSE) == 0 &&
1016 1.4.2.4 nathanw (srb->xs->xs_control & XS_CTL_REQSENSE) == 0));
1017 1.4.2.4 nathanw scsicmd = SCMD_SEL_ATN;
1018 1.4.2.4 nathanw DPRINTF(("select with SEL_ATN\n"));
1019 1.4.2.2 nathanw }
1020 1.4.2.4 nathanw sc->sc_state = TRM_START;
1021 1.4.2.4 nathanw
1022 1.4.2.4 nathanw /*
1023 1.4.2.4 nathanw * Send CDB ..command block...
1024 1.4.2.4 nathanw */
1025 1.4.2.4 nathanw bus_space_write_multi_1(iot, ioh, TRM_SCSI_FIFO, srb->cmd, srb->cmdlen);
1026 1.4.2.4 nathanw
1027 1.4.2.4 nathanw /*
1028 1.4.2.4 nathanw * If trm_select returns 0: current interrupt status
1029 1.4.2.4 nathanw * is interrupt enable. It's said that SCSI processor is
1030 1.4.2.4 nathanw * unoccupied.
1031 1.4.2.4 nathanw */
1032 1.4.2.4 nathanw sc->sc_phase = PH_BUS_FREE; /* SCSI bus free Phase */
1033 1.4.2.4 nathanw /* SCSI command */
1034 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, scsicmd);
1035 1.4.2.4 nathanw return (0);
1036 1.4.2.2 nathanw }
1037 1.4.2.2 nathanw
1038 1.4.2.2 nathanw /*
1039 1.4.2.2 nathanw * perform a hard reset on the SCSI bus (and TRM_S1040 chip).
1040 1.4.2.2 nathanw */
1041 1.4.2.2 nathanw static void
1042 1.4.2.2 nathanw trm_reset(sc)
1043 1.4.2.2 nathanw struct trm_softc *sc;
1044 1.4.2.2 nathanw {
1045 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1046 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1047 1.4.2.2 nathanw int s;
1048 1.4.2.2 nathanw
1049 1.4.2.4 nathanw DPRINTF(("trm_reset.........\n"));
1050 1.4.2.4 nathanw
1051 1.4.2.2 nathanw s = splbio();
1052 1.4.2.2 nathanw
1053 1.4.2.2 nathanw /* disable SCSI and DMA interrupt */
1054 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_INTEN, 0);
1055 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_INTEN, 0);
1056 1.4.2.2 nathanw
1057 1.4.2.2 nathanw trm_reset_scsi_bus(sc);
1058 1.4.2.4 nathanw DELAY(100000);
1059 1.4.2.2 nathanw
1060 1.4.2.2 nathanw /* Enable SCSI interrupt */
1061 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_INTEN,
1062 1.4.2.2 nathanw EN_SELECT | EN_SELTIMEOUT | EN_DISCONNECT | EN_RESELECTED |
1063 1.4.2.2 nathanw EN_SCSIRESET | EN_BUSSERVICE | EN_CMDDONE);
1064 1.4.2.2 nathanw
1065 1.4.2.2 nathanw /* Enable DMA interrupt */
1066 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_INTEN, EN_SCSIINTR);
1067 1.4.2.2 nathanw
1068 1.4.2.2 nathanw /* Clear DMA FIFO */
1069 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_CONTROL, CLRXFIFO);
1070 1.4.2.2 nathanw
1071 1.4.2.2 nathanw /* Clear SCSI FIFO */
1072 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRFIFO);
1073 1.4.2.2 nathanw
1074 1.4.2.4 nathanw sc->sc_actsrb = NULL;
1075 1.4.2.2 nathanw sc->sc_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
1076 1.4.2.2 nathanw
1077 1.4.2.2 nathanw splx(s);
1078 1.4.2.2 nathanw }
1079 1.4.2.2 nathanw
1080 1.4.2.2 nathanw static void
1081 1.4.2.2 nathanw trm_timeout(arg)
1082 1.4.2.2 nathanw void *arg;
1083 1.4.2.2 nathanw {
1084 1.4.2.2 nathanw struct trm_srb *srb = (struct trm_srb *)arg;
1085 1.4.2.2 nathanw struct scsipi_xfer *xs = srb->xs;
1086 1.4.2.2 nathanw struct scsipi_periph *periph = xs->xs_periph;
1087 1.4.2.2 nathanw struct trm_softc *sc;
1088 1.4.2.2 nathanw int s;
1089 1.4.2.2 nathanw
1090 1.4.2.2 nathanw if (xs == NULL)
1091 1.4.2.2 nathanw printf("trm_timeout called with xs == NULL\n");
1092 1.4.2.2 nathanw
1093 1.4.2.2 nathanw else {
1094 1.4.2.2 nathanw scsipi_printaddr(xs->xs_periph);
1095 1.4.2.2 nathanw printf("SCSI OpCode 0x%02x timed out\n", xs->cmd->opcode);
1096 1.4.2.2 nathanw }
1097 1.4.2.2 nathanw
1098 1.4.2.2 nathanw sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
1099 1.4.2.2 nathanw
1100 1.4.2.2 nathanw trm_reset_scsi_bus(sc);
1101 1.4.2.4 nathanw s = splbio();
1102 1.4.2.4 nathanw srb->flag |= SRB_TIMEOUT;
1103 1.4.2.4 nathanw trm_done(sc, srb);
1104 1.4.2.4 nathanw /* XXX needs more.. */
1105 1.4.2.2 nathanw splx(s);
1106 1.4.2.2 nathanw }
1107 1.4.2.2 nathanw
1108 1.4.2.2 nathanw /*
1109 1.4.2.2 nathanw * Catch an interrupt from the adapter
1110 1.4.2.2 nathanw * Process pending device interrupts.
1111 1.4.2.2 nathanw */
1112 1.4.2.2 nathanw static int
1113 1.4.2.4 nathanw trm_intr(arg)
1114 1.4.2.4 nathanw void *arg;
1115 1.4.2.2 nathanw {
1116 1.4.2.2 nathanw bus_space_tag_t iot;
1117 1.4.2.2 nathanw bus_space_handle_t ioh;
1118 1.4.2.2 nathanw struct trm_softc *sc;
1119 1.4.2.2 nathanw struct trm_srb *srb;
1120 1.4.2.4 nathanw int intstat, stat;
1121 1.4.2.2 nathanw
1122 1.4.2.4 nathanw DPRINTF(("trm_intr......\n"));
1123 1.4.2.4 nathanw sc = (struct trm_softc *)arg;
1124 1.4.2.2 nathanw if (sc == NULL)
1125 1.4.2.2 nathanw return (0);
1126 1.4.2.2 nathanw
1127 1.4.2.4 nathanw iot = sc->sc_iot;
1128 1.4.2.4 nathanw ioh = sc->sc_ioh;
1129 1.4.2.4 nathanw
1130 1.4.2.2 nathanw stat = bus_space_read_2(iot, ioh, TRM_SCSI_STATUS);
1131 1.4.2.2 nathanw if ((stat & SCSIINTERRUPT) == 0)
1132 1.4.2.2 nathanw return (0);
1133 1.4.2.2 nathanw
1134 1.4.2.4 nathanw DPRINTF(("stat = %04x, ", stat));
1135 1.4.2.2 nathanw intstat = bus_space_read_1(iot, ioh, TRM_SCSI_INTSTATUS);
1136 1.4.2.2 nathanw
1137 1.4.2.4 nathanw DPRINTF(("intstat=%02x, ", intstat));
1138 1.4.2.2 nathanw if (intstat & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1139 1.4.2.4 nathanw DPRINTF(("\n"));
1140 1.4.2.2 nathanw trm_disconnect(sc);
1141 1.4.2.2 nathanw return (1);
1142 1.4.2.2 nathanw }
1143 1.4.2.2 nathanw if (intstat & INT_RESELECTED) {
1144 1.4.2.4 nathanw DPRINTF(("\n"));
1145 1.4.2.2 nathanw trm_reselect(sc);
1146 1.4.2.2 nathanw return (1);
1147 1.4.2.2 nathanw }
1148 1.4.2.2 nathanw if (intstat & INT_SCSIRESET) {
1149 1.4.2.4 nathanw DPRINTF(("\n"));
1150 1.4.2.2 nathanw trm_scsi_reset_detect(sc);
1151 1.4.2.2 nathanw return (1);
1152 1.4.2.2 nathanw }
1153 1.4.2.2 nathanw if (intstat & (INT_BUSSERVICE | INT_CMDDONE)) {
1154 1.4.2.4 nathanw srb = sc->sc_actsrb;
1155 1.4.2.4 nathanw DPRINTF(("sc->sc_phase = %2d, sc->sc_state = %2d\n",
1156 1.4.2.4 nathanw sc->sc_phase, sc->sc_state));
1157 1.4.2.2 nathanw /*
1158 1.4.2.2 nathanw * software sequential machine
1159 1.4.2.2 nathanw */
1160 1.4.2.2 nathanw
1161 1.4.2.2 nathanw /*
1162 1.4.2.4 nathanw * call phase0 functions... "phase entry" handle
1163 1.4.2.4 nathanw * every phase before start transfer
1164 1.4.2.2 nathanw */
1165 1.4.2.4 nathanw switch (sc->sc_phase) {
1166 1.4.2.4 nathanw case PH_DATA_OUT:
1167 1.4.2.4 nathanw trm_dataout_phase0(sc, stat);
1168 1.4.2.4 nathanw break;
1169 1.4.2.4 nathanw case PH_DATA_IN:
1170 1.4.2.4 nathanw trm_datain_phase0(sc, stat);
1171 1.4.2.4 nathanw break;
1172 1.4.2.4 nathanw case PH_COMMAND:
1173 1.4.2.4 nathanw break;
1174 1.4.2.4 nathanw case PH_STATUS:
1175 1.4.2.4 nathanw trm_status_phase0(sc);
1176 1.4.2.4 nathanw stat = PH_BUS_FREE;
1177 1.4.2.4 nathanw break;
1178 1.4.2.4 nathanw case PH_MSG_OUT:
1179 1.4.2.4 nathanw if (sc->sc_state == TRM_UNEXPECT_RESEL ||
1180 1.4.2.4 nathanw sc->sc_state == TRM_ABORT_SENT)
1181 1.4.2.4 nathanw stat = PH_BUS_FREE;
1182 1.4.2.4 nathanw break;
1183 1.4.2.4 nathanw case PH_MSG_IN:
1184 1.4.2.4 nathanw trm_msgin_phase0(sc);
1185 1.4.2.4 nathanw stat = PH_BUS_FREE;
1186 1.4.2.4 nathanw break;
1187 1.4.2.4 nathanw case PH_BUS_FREE:
1188 1.4.2.4 nathanw break;
1189 1.4.2.4 nathanw default:
1190 1.4.2.4 nathanw printf("%s: unexpected phase in trm_intr() phase0\n",
1191 1.4.2.4 nathanw sc->sc_dev.dv_xname);
1192 1.4.2.4 nathanw break;
1193 1.4.2.4 nathanw }
1194 1.4.2.2 nathanw
1195 1.4.2.4 nathanw sc->sc_phase = stat & PHASEMASK;
1196 1.4.2.4 nathanw
1197 1.4.2.4 nathanw switch (sc->sc_phase) {
1198 1.4.2.4 nathanw case PH_DATA_OUT:
1199 1.4.2.4 nathanw trm_dataio_xfer(sc, XFERDATAOUT);
1200 1.4.2.4 nathanw break;
1201 1.4.2.4 nathanw case PH_DATA_IN:
1202 1.4.2.4 nathanw trm_dataio_xfer(sc, XFERDATAIN);
1203 1.4.2.4 nathanw break;
1204 1.4.2.4 nathanw case PH_COMMAND:
1205 1.4.2.4 nathanw trm_command_phase1(sc);
1206 1.4.2.4 nathanw break;
1207 1.4.2.4 nathanw case PH_STATUS:
1208 1.4.2.4 nathanw trm_status_phase1(sc);
1209 1.4.2.4 nathanw break;
1210 1.4.2.4 nathanw case PH_MSG_OUT:
1211 1.4.2.4 nathanw trm_msgout_phase1(sc);
1212 1.4.2.4 nathanw break;
1213 1.4.2.4 nathanw case PH_MSG_IN:
1214 1.4.2.4 nathanw trm_msgin_phase1(sc);
1215 1.4.2.4 nathanw break;
1216 1.4.2.4 nathanw case PH_BUS_FREE:
1217 1.4.2.4 nathanw break;
1218 1.4.2.4 nathanw default:
1219 1.4.2.4 nathanw printf("%s: unexpected phase in trm_intr() phase1\n",
1220 1.4.2.4 nathanw sc->sc_dev.dv_xname);
1221 1.4.2.4 nathanw break;
1222 1.4.2.4 nathanw }
1223 1.4.2.2 nathanw
1224 1.4.2.2 nathanw return (1);
1225 1.4.2.2 nathanw }
1226 1.4.2.2 nathanw return (0);
1227 1.4.2.2 nathanw }
1228 1.4.2.2 nathanw
1229 1.4.2.2 nathanw static void
1230 1.4.2.4 nathanw trm_msgout_phase1(sc)
1231 1.4.2.2 nathanw struct trm_softc *sc;
1232 1.4.2.2 nathanw {
1233 1.4.2.4 nathanw bus_space_tag_t iot = sc->sc_iot;
1234 1.4.2.4 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1235 1.4.2.4 nathanw struct trm_srb *srb;
1236 1.4.2.4 nathanw struct scsipi_periph *periph;
1237 1.4.2.4 nathanw struct trm_tinfo *ti;
1238 1.4.2.2 nathanw
1239 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRFIFO);
1240 1.4.2.2 nathanw
1241 1.4.2.4 nathanw srb = sc->sc_actsrb;
1242 1.4.2.2 nathanw
1243 1.4.2.4 nathanw /* message out phase */
1244 1.4.2.4 nathanw if (srb != NULL) {
1245 1.4.2.4 nathanw periph = srb->xs->xs_periph;
1246 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
1247 1.4.2.4 nathanw
1248 1.4.2.4 nathanw if ((ti->flag & WIDE_NEGO_DOING) == 0 &&
1249 1.4.2.4 nathanw (ti->flag & WIDE_NEGO_ENABLE)) {
1250 1.4.2.4 nathanw /* send WDTR */
1251 1.4.2.4 nathanw ti->flag &= ~SYNC_NEGO_DONE;
1252 1.4.2.4 nathanw
1253 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_IDENTIFY(periph->periph_lun, 0);
1254 1.4.2.4 nathanw sc->sc_msgbuf[1] = MSG_EXTENDED;
1255 1.4.2.4 nathanw sc->sc_msgbuf[2] = MSG_EXT_WDTR_LEN;
1256 1.4.2.4 nathanw sc->sc_msgbuf[3] = MSG_EXT_WDTR;
1257 1.4.2.4 nathanw sc->sc_msgbuf[4] = MSG_EXT_WDTR_BUS_16_BIT;
1258 1.4.2.4 nathanw sc->sc_msgcnt = 5;
1259 1.4.2.4 nathanw
1260 1.4.2.4 nathanw ti->flag |= WIDE_NEGO_DOING;
1261 1.4.2.4 nathanw } else if ((ti->flag & SYNC_NEGO_DOING) == 0 &&
1262 1.4.2.4 nathanw (ti->flag & SYNC_NEGO_ENABLE)) {
1263 1.4.2.4 nathanw /* send SDTR */
1264 1.4.2.4 nathanw int cnt = 0;
1265 1.4.2.4 nathanw
1266 1.4.2.4 nathanw if ((ti->flag & WIDE_NEGO_DONE) == 0)
1267 1.4.2.4 nathanw sc->sc_msgbuf[cnt++] =
1268 1.4.2.4 nathanw MSG_IDENTIFY(periph->periph_lun, 0);
1269 1.4.2.4 nathanw
1270 1.4.2.4 nathanw sc->sc_msgbuf[cnt++] = MSG_EXTENDED;
1271 1.4.2.4 nathanw sc->sc_msgbuf[cnt++] = MSG_EXT_SDTR_LEN;
1272 1.4.2.4 nathanw sc->sc_msgbuf[cnt++] = MSG_EXT_SDTR;
1273 1.4.2.4 nathanw sc->sc_msgbuf[cnt++] = ti->period;
1274 1.4.2.4 nathanw sc->sc_msgbuf[cnt++] = TRM_MAX_OFFSET;
1275 1.4.2.4 nathanw sc->sc_msgcnt = cnt;
1276 1.4.2.4 nathanw ti->flag |= SYNC_NEGO_DOING;
1277 1.4.2.4 nathanw }
1278 1.4.2.4 nathanw }
1279 1.4.2.4 nathanw if (sc->sc_msgcnt == 0) {
1280 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_ABORT;
1281 1.4.2.4 nathanw sc->sc_msgcnt = 1;
1282 1.4.2.4 nathanw sc->sc_state = TRM_ABORT_SENT;
1283 1.4.2.4 nathanw }
1284 1.4.2.4 nathanw
1285 1.4.2.4 nathanw DPRINTF(("msgout: cnt = %d, ", sc->sc_msgcnt));
1286 1.4.2.4 nathanw DPRINTF(("msgbuf = %02x %02x %02x %02x %02x %02x\n",
1287 1.4.2.4 nathanw sc->sc_msgbuf[0], sc->sc_msgbuf[1], sc->sc_msgbuf[2],
1288 1.4.2.4 nathanw sc->sc_msgbuf[3], sc->sc_msgbuf[4], sc->sc_msgbuf[5]));
1289 1.4.2.4 nathanw
1290 1.4.2.4 nathanw bus_space_write_multi_1(iot, ioh, TRM_SCSI_FIFO,
1291 1.4.2.4 nathanw sc->sc_msgbuf, sc->sc_msgcnt);
1292 1.4.2.4 nathanw sc->sc_msgcnt = 0;
1293 1.4.2.4 nathanw memset(sc->sc_msgbuf, 0, sizeof(sc->sc_msgbuf));
1294 1.4.2.2 nathanw
1295 1.4.2.2 nathanw /* it's important for atn stop */
1296 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
1297 1.4.2.2 nathanw
1298 1.4.2.2 nathanw /*
1299 1.4.2.4 nathanw * SCSI command
1300 1.4.2.2 nathanw */
1301 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_FIFO_OUT);
1302 1.4.2.2 nathanw }
1303 1.4.2.2 nathanw
1304 1.4.2.2 nathanw static void
1305 1.4.2.4 nathanw trm_command_phase1(sc)
1306 1.4.2.2 nathanw struct trm_softc *sc;
1307 1.4.2.2 nathanw {
1308 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1309 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1310 1.4.2.4 nathanw struct trm_srb *srb;
1311 1.4.2.4 nathanw
1312 1.4.2.4 nathanw srb = sc->sc_actsrb;
1313 1.4.2.4 nathanw if (srb == NULL) {
1314 1.4.2.4 nathanw DPRINTF(("trm_command_phase1: no active srb\n"));
1315 1.4.2.4 nathanw return;
1316 1.4.2.4 nathanw }
1317 1.4.2.2 nathanw
1318 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRATN | DO_CLRFIFO);
1319 1.4.2.3 nathanw bus_space_write_multi_1(iot, ioh, TRM_SCSI_FIFO, srb->cmd, srb->cmdlen);
1320 1.4.2.2 nathanw
1321 1.4.2.4 nathanw sc->sc_state = TRM_COMMAND;
1322 1.4.2.2 nathanw /* it's important for atn stop */
1323 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
1324 1.4.2.2 nathanw
1325 1.4.2.2 nathanw /*
1326 1.4.2.4 nathanw * SCSI command
1327 1.4.2.2 nathanw */
1328 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_FIFO_OUT);
1329 1.4.2.2 nathanw }
1330 1.4.2.2 nathanw
1331 1.4.2.2 nathanw static void
1332 1.4.2.4 nathanw trm_dataout_phase0(sc, stat)
1333 1.4.2.2 nathanw struct trm_softc *sc;
1334 1.4.2.4 nathanw int stat;
1335 1.4.2.2 nathanw {
1336 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1337 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1338 1.4.2.4 nathanw struct trm_srb *srb;
1339 1.4.2.4 nathanw struct scsipi_periph *periph;
1340 1.4.2.4 nathanw struct trm_tinfo *ti;
1341 1.4.2.2 nathanw struct trm_sg_entry *sg;
1342 1.4.2.2 nathanw int sgindex;
1343 1.4.2.2 nathanw u_int32_t xferlen, leftcnt = 0;
1344 1.4.2.2 nathanw
1345 1.4.2.4 nathanw if (sc->sc_state == TRM_XFERPAD)
1346 1.4.2.4 nathanw return;
1347 1.4.2.4 nathanw
1348 1.4.2.4 nathanw srb = sc->sc_actsrb;
1349 1.4.2.4 nathanw if (srb == NULL) {
1350 1.4.2.4 nathanw DPRINTF(("trm_dataout_phase0: no active srb\n"));
1351 1.4.2.4 nathanw return;
1352 1.4.2.4 nathanw }
1353 1.4.2.4 nathanw periph = srb->xs->xs_periph;
1354 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
1355 1.4.2.2 nathanw
1356 1.4.2.4 nathanw if ((stat & PARITYERROR) != 0)
1357 1.4.2.4 nathanw srb->flag |= PARITY_ERROR;
1358 1.4.2.2 nathanw
1359 1.4.2.4 nathanw if ((stat & SCSIXFERDONE) == 0) {
1360 1.4.2.4 nathanw /*
1361 1.4.2.4 nathanw * when data transfer from DMA FIFO to SCSI FIFO
1362 1.4.2.4 nathanw * if there was some data left in SCSI FIFO
1363 1.4.2.4 nathanw */
1364 1.4.2.4 nathanw leftcnt = bus_space_read_1(iot, ioh, TRM_SCSI_FIFOCNT) &
1365 1.4.2.4 nathanw SCSI_FIFOCNT_MASK;
1366 1.4.2.4 nathanw if (ti->synctl & WIDE_SYNC)
1367 1.4.2.2 nathanw /*
1368 1.4.2.4 nathanw * if WIDE scsi SCSI FIFOCNT unit is word
1369 1.4.2.4 nathanw * so need to * 2
1370 1.4.2.2 nathanw */
1371 1.4.2.4 nathanw leftcnt <<= 1;
1372 1.4.2.4 nathanw }
1373 1.4.2.4 nathanw /*
1374 1.4.2.4 nathanw * calculate all the residue data that was not yet transferred
1375 1.4.2.4 nathanw * SCSI transfer counter + left in SCSI FIFO data
1376 1.4.2.4 nathanw *
1377 1.4.2.4 nathanw * .....TRM_SCSI_XCNT (24bits)
1378 1.4.2.4 nathanw * The counter always decrements by one for every SCSI
1379 1.4.2.4 nathanw * byte transfer.
1380 1.4.2.4 nathanw * .....TRM_SCSI_FIFOCNT ( 5bits)
1381 1.4.2.4 nathanw * The counter is SCSI FIFO offset counter
1382 1.4.2.4 nathanw */
1383 1.4.2.4 nathanw leftcnt += bus_space_read_4(iot, ioh, TRM_SCSI_XCNT);
1384 1.4.2.4 nathanw if (leftcnt == 1) {
1385 1.4.2.4 nathanw leftcnt = 0;
1386 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRFIFO);
1387 1.4.2.4 nathanw }
1388 1.4.2.4 nathanw if ((leftcnt == 0) || (stat & SCSIXFERCNT_2_ZERO)) {
1389 1.4.2.4 nathanw while ((bus_space_read_1(iot, ioh, TRM_DMA_STATUS) &
1390 1.4.2.4 nathanw DMAXFERCOMP) == 0)
1391 1.4.2.4 nathanw ; /* XXX needs timeout */
1392 1.4.2.4 nathanw
1393 1.4.2.4 nathanw srb->buflen = 0;
1394 1.4.2.4 nathanw } else {
1395 1.4.2.4 nathanw /* Update SG list */
1396 1.4.2.4 nathanw
1397 1.4.2.2 nathanw /*
1398 1.4.2.4 nathanw * if transfer not yet complete
1399 1.4.2.4 nathanw * there were some data residue in SCSI FIFO or
1400 1.4.2.4 nathanw * SCSI transfer counter not empty
1401 1.4.2.2 nathanw */
1402 1.4.2.4 nathanw if (srb->buflen != leftcnt) {
1403 1.4.2.4 nathanw /* data that had transferred length */
1404 1.4.2.4 nathanw xferlen = srb->buflen - leftcnt;
1405 1.4.2.4 nathanw
1406 1.4.2.4 nathanw /* next time to be transferred length */
1407 1.4.2.4 nathanw srb->buflen = leftcnt;
1408 1.4.2.2 nathanw
1409 1.4.2.2 nathanw /*
1410 1.4.2.4 nathanw * parsing from last time disconnect sgindex
1411 1.4.2.2 nathanw */
1412 1.4.2.4 nathanw sg = srb->sgentry + srb->sgindex;
1413 1.4.2.4 nathanw for (sgindex = srb->sgindex;
1414 1.4.2.4 nathanw sgindex < srb->sgcnt;
1415 1.4.2.4 nathanw sgindex++, sg++) {
1416 1.4.2.2 nathanw /*
1417 1.4.2.4 nathanw * find last time which SG transfer
1418 1.4.2.4 nathanw * be disconnect
1419 1.4.2.2 nathanw */
1420 1.4.2.4 nathanw if (xferlen >= le32toh(sg->length))
1421 1.4.2.4 nathanw xferlen -= le32toh(sg->length);
1422 1.4.2.4 nathanw else {
1423 1.4.2.2 nathanw /*
1424 1.4.2.4 nathanw * update last time
1425 1.4.2.4 nathanw * disconnected SG list
1426 1.4.2.2 nathanw */
1427 1.4.2.4 nathanw /* residue data length */
1428 1.4.2.4 nathanw sg->length =
1429 1.4.2.4 nathanw htole32(le32toh(sg->length)
1430 1.4.2.4 nathanw - xferlen);
1431 1.4.2.4 nathanw /* residue data pointer */
1432 1.4.2.4 nathanw sg->address =
1433 1.4.2.4 nathanw htole32(le32toh(sg->address)
1434 1.4.2.4 nathanw + xferlen);
1435 1.4.2.4 nathanw srb->sgindex = sgindex;
1436 1.4.2.4 nathanw break;
1437 1.4.2.2 nathanw }
1438 1.4.2.2 nathanw }
1439 1.4.2.4 nathanw bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1440 1.4.2.4 nathanw srb->sgoffset, TRM_SG_SIZE, BUS_DMASYNC_PREWRITE);
1441 1.4.2.2 nathanw }
1442 1.4.2.2 nathanw }
1443 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_CONTROL, STOPDMAXFER);
1444 1.4.2.2 nathanw }
1445 1.4.2.2 nathanw
1446 1.4.2.2 nathanw static void
1447 1.4.2.4 nathanw trm_datain_phase0(sc, stat)
1448 1.4.2.2 nathanw struct trm_softc *sc;
1449 1.4.2.4 nathanw int stat;
1450 1.4.2.2 nathanw {
1451 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1452 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1453 1.4.2.4 nathanw struct trm_srb *srb;
1454 1.4.2.2 nathanw struct trm_sg_entry *sg;
1455 1.4.2.2 nathanw int sgindex;
1456 1.4.2.2 nathanw u_int32_t xferlen, leftcnt = 0;
1457 1.4.2.2 nathanw
1458 1.4.2.4 nathanw if (sc->sc_state == TRM_XFERPAD)
1459 1.4.2.4 nathanw return;
1460 1.4.2.2 nathanw
1461 1.4.2.4 nathanw srb = sc->sc_actsrb;
1462 1.4.2.4 nathanw if (srb == NULL) {
1463 1.4.2.4 nathanw DPRINTF(("trm_datain_phase0: no active srb\n"));
1464 1.4.2.4 nathanw return;
1465 1.4.2.4 nathanw }
1466 1.4.2.4 nathanw
1467 1.4.2.4 nathanw if (stat & PARITYERROR)
1468 1.4.2.4 nathanw srb->flag |= PARITY_ERROR;
1469 1.4.2.4 nathanw
1470 1.4.2.4 nathanw leftcnt += bus_space_read_4(iot, ioh, TRM_SCSI_XCNT);
1471 1.4.2.4 nathanw if ((leftcnt == 0) || (stat & SCSIXFERCNT_2_ZERO)) {
1472 1.4.2.4 nathanw while ((bus_space_read_1(iot, ioh, TRM_DMA_STATUS) &
1473 1.4.2.4 nathanw DMAXFERCOMP) == 0)
1474 1.4.2.4 nathanw ; /* XXX needs timeout */
1475 1.4.2.4 nathanw
1476 1.4.2.4 nathanw srb->buflen = 0;
1477 1.4.2.4 nathanw } else { /* phase changed */
1478 1.4.2.4 nathanw /*
1479 1.4.2.4 nathanw * parsing the case:
1480 1.4.2.4 nathanw * when a transfer not yet complete
1481 1.4.2.4 nathanw * but be disconnected by upper layer
1482 1.4.2.4 nathanw * if transfer not yet complete
1483 1.4.2.4 nathanw * there were some data residue in SCSI FIFO or
1484 1.4.2.4 nathanw * SCSI transfer counter not empty
1485 1.4.2.4 nathanw */
1486 1.4.2.4 nathanw if (srb->buflen != leftcnt) {
1487 1.4.2.2 nathanw /*
1488 1.4.2.4 nathanw * data that had transferred length
1489 1.4.2.2 nathanw */
1490 1.4.2.4 nathanw xferlen = srb->buflen - leftcnt;
1491 1.4.2.2 nathanw
1492 1.4.2.4 nathanw /*
1493 1.4.2.4 nathanw * next time to be transferred length
1494 1.4.2.4 nathanw */
1495 1.4.2.4 nathanw srb->buflen = leftcnt;
1496 1.4.2.2 nathanw
1497 1.4.2.4 nathanw /*
1498 1.4.2.4 nathanw * parsing from last time disconnect sgindex
1499 1.4.2.4 nathanw */
1500 1.4.2.4 nathanw sg = srb->sgentry + srb->sgindex;
1501 1.4.2.4 nathanw for (sgindex = srb->sgindex;
1502 1.4.2.4 nathanw sgindex < srb->sgcnt;
1503 1.4.2.4 nathanw sgindex++, sg++) {
1504 1.4.2.2 nathanw /*
1505 1.4.2.4 nathanw * find last time which SG transfer
1506 1.4.2.4 nathanw * be disconnect
1507 1.4.2.2 nathanw */
1508 1.4.2.4 nathanw if (xferlen >= le32toh(sg->length))
1509 1.4.2.4 nathanw xferlen -= le32toh(sg->length);
1510 1.4.2.4 nathanw else {
1511 1.4.2.2 nathanw /*
1512 1.4.2.4 nathanw * update last time
1513 1.4.2.4 nathanw * disconnected SG list
1514 1.4.2.2 nathanw */
1515 1.4.2.4 nathanw /* residue data length */
1516 1.4.2.4 nathanw sg->length =
1517 1.4.2.4 nathanw htole32(le32toh(sg->length)
1518 1.4.2.4 nathanw - xferlen);
1519 1.4.2.4 nathanw /* residue data pointer */
1520 1.4.2.4 nathanw sg->address =
1521 1.4.2.4 nathanw htole32(le32toh(sg->address)
1522 1.4.2.4 nathanw + xferlen);
1523 1.4.2.4 nathanw srb->sgindex = sgindex;
1524 1.4.2.4 nathanw break;
1525 1.4.2.2 nathanw }
1526 1.4.2.2 nathanw }
1527 1.4.2.4 nathanw bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
1528 1.4.2.4 nathanw srb->sgoffset, TRM_SG_SIZE, BUS_DMASYNC_PREWRITE);
1529 1.4.2.2 nathanw }
1530 1.4.2.2 nathanw }
1531 1.4.2.2 nathanw }
1532 1.4.2.2 nathanw
1533 1.4.2.2 nathanw static void
1534 1.4.2.4 nathanw trm_dataio_xfer(sc, iodir)
1535 1.4.2.2 nathanw struct trm_softc *sc;
1536 1.4.2.2 nathanw int iodir;
1537 1.4.2.2 nathanw {
1538 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1539 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1540 1.4.2.4 nathanw struct trm_srb *srb;
1541 1.4.2.4 nathanw struct scsipi_periph *periph;
1542 1.4.2.4 nathanw struct trm_tinfo *ti;
1543 1.4.2.4 nathanw
1544 1.4.2.4 nathanw srb = sc->sc_actsrb;
1545 1.4.2.4 nathanw if (srb == NULL) {
1546 1.4.2.4 nathanw DPRINTF(("trm_dataio_xfer: no active srb\n"));
1547 1.4.2.4 nathanw return;
1548 1.4.2.4 nathanw }
1549 1.4.2.4 nathanw periph = srb->xs->xs_periph;
1550 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
1551 1.4.2.2 nathanw
1552 1.4.2.2 nathanw if (srb->sgindex < srb->sgcnt) {
1553 1.4.2.2 nathanw if (srb->buflen > 0) {
1554 1.4.2.2 nathanw /*
1555 1.4.2.2 nathanw * load what physical address of Scatter/Gather
1556 1.4.2.2 nathanw * list table want to be transfer
1557 1.4.2.2 nathanw */
1558 1.4.2.4 nathanw sc->sc_state = TRM_DATA_XFER;
1559 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_DMA_XHIGHADDR, 0);
1560 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_DMA_XLOWADDR,
1561 1.4.2.2 nathanw srb->sgaddr +
1562 1.4.2.2 nathanw srb->sgindex * sizeof(struct trm_sg_entry));
1563 1.4.2.2 nathanw /*
1564 1.4.2.2 nathanw * load how many bytes in the Scatter/Gather list table
1565 1.4.2.2 nathanw */
1566 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_DMA_XCNT,
1567 1.4.2.2 nathanw (srb->sgcnt - srb->sgindex)
1568 1.4.2.2 nathanw * sizeof(struct trm_sg_entry));
1569 1.4.2.2 nathanw /*
1570 1.4.2.2 nathanw * load total xfer length (24bits) max value 16Mbyte
1571 1.4.2.2 nathanw */
1572 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_SCSI_XCNT, srb->buflen);
1573 1.4.2.2 nathanw /* Start DMA transfer */
1574 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_COMMAND,
1575 1.4.2.2 nathanw iodir | SGXFER);
1576 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_CONTROL,
1577 1.4.2.2 nathanw STARTDMAXFER);
1578 1.4.2.2 nathanw
1579 1.4.2.2 nathanw /* Start SCSI transfer */
1580 1.4.2.2 nathanw /* it's important for atn stop */
1581 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL,
1582 1.4.2.2 nathanw DO_DATALATCH);
1583 1.4.2.2 nathanw
1584 1.4.2.2 nathanw /*
1585 1.4.2.4 nathanw * SCSI command
1586 1.4.2.2 nathanw */
1587 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND,
1588 1.4.2.2 nathanw (iodir == XFERDATAOUT) ?
1589 1.4.2.2 nathanw SCMD_DMA_OUT : SCMD_DMA_IN);
1590 1.4.2.2 nathanw } else { /* xfer pad */
1591 1.4.2.2 nathanw if (srb->sgcnt) {
1592 1.4.2.2 nathanw srb->hastat = H_OVER_UNDER_RUN;
1593 1.4.2.2 nathanw }
1594 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_SCSI_XCNT,
1595 1.4.2.4 nathanw (ti->synctl & WIDE_SYNC) ? 2 : 1);
1596 1.4.2.2 nathanw
1597 1.4.2.2 nathanw if (iodir == XFERDATAOUT)
1598 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_FIFO, 0);
1599 1.4.2.2 nathanw else
1600 1.4.2.2 nathanw bus_space_read_2(iot, ioh, TRM_SCSI_FIFO);
1601 1.4.2.2 nathanw
1602 1.4.2.4 nathanw sc->sc_state = TRM_XFERPAD;
1603 1.4.2.2 nathanw /* it's important for atn stop */
1604 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL,
1605 1.4.2.2 nathanw DO_DATALATCH);
1606 1.4.2.2 nathanw
1607 1.4.2.2 nathanw /*
1608 1.4.2.4 nathanw * SCSI command
1609 1.4.2.2 nathanw */
1610 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND,
1611 1.4.2.2 nathanw (iodir == XFERDATAOUT) ?
1612 1.4.2.2 nathanw SCMD_FIFO_OUT : SCMD_FIFO_IN);
1613 1.4.2.2 nathanw }
1614 1.4.2.2 nathanw }
1615 1.4.2.2 nathanw }
1616 1.4.2.2 nathanw
1617 1.4.2.2 nathanw static void
1618 1.4.2.4 nathanw trm_status_phase0(sc)
1619 1.4.2.2 nathanw struct trm_softc *sc;
1620 1.4.2.2 nathanw {
1621 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1622 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1623 1.4.2.4 nathanw struct trm_srb *srb;
1624 1.4.2.2 nathanw
1625 1.4.2.4 nathanw srb = sc->sc_actsrb;
1626 1.4.2.4 nathanw if (srb == NULL) {
1627 1.4.2.4 nathanw DPRINTF(("trm_status_phase0: no active srb\n"));
1628 1.4.2.4 nathanw return;
1629 1.4.2.4 nathanw }
1630 1.4.2.2 nathanw srb->tastat = bus_space_read_1(iot, ioh, TRM_SCSI_FIFO);
1631 1.4.2.4 nathanw sc->sc_state = TRM_COMPLETED;
1632 1.4.2.2 nathanw /* it's important for atn stop */
1633 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
1634 1.4.2.2 nathanw
1635 1.4.2.2 nathanw /*
1636 1.4.2.4 nathanw * SCSI command
1637 1.4.2.2 nathanw */
1638 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_MSGACCEPT);
1639 1.4.2.2 nathanw }
1640 1.4.2.2 nathanw
1641 1.4.2.2 nathanw static void
1642 1.4.2.4 nathanw trm_status_phase1(sc)
1643 1.4.2.2 nathanw struct trm_softc *sc;
1644 1.4.2.2 nathanw {
1645 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1646 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1647 1.4.2.2 nathanw
1648 1.4.2.2 nathanw if (bus_space_read_1(iot, ioh, TRM_DMA_COMMAND) & XFERDATAIN) {
1649 1.4.2.2 nathanw if ((bus_space_read_1(iot, ioh, TRM_SCSI_FIFOCNT)
1650 1.4.2.2 nathanw & SCSI_FIFO_EMPTY) == 0)
1651 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1652 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_CLRFIFO);
1653 1.4.2.2 nathanw if ((bus_space_read_1(iot, ioh, TRM_DMA_FIFOSTATUS)
1654 1.4.2.2 nathanw & DMA_FIFO_EMPTY) == 0)
1655 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_CONTROL, CLRXFIFO);
1656 1.4.2.2 nathanw } else {
1657 1.4.2.2 nathanw if ((bus_space_read_1(iot, ioh, TRM_DMA_FIFOSTATUS)
1658 1.4.2.2 nathanw & DMA_FIFO_EMPTY) == 0)
1659 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_DMA_CONTROL, CLRXFIFO);
1660 1.4.2.2 nathanw if ((bus_space_read_1(iot, ioh, TRM_SCSI_FIFOCNT)
1661 1.4.2.2 nathanw & SCSI_FIFO_EMPTY) == 0)
1662 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1663 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_CLRFIFO);
1664 1.4.2.2 nathanw }
1665 1.4.2.4 nathanw sc->sc_state = TRM_STATUS;
1666 1.4.2.2 nathanw /* it's important for atn stop */
1667 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
1668 1.4.2.2 nathanw
1669 1.4.2.2 nathanw /*
1670 1.4.2.4 nathanw * SCSI command
1671 1.4.2.2 nathanw */
1672 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_COMP);
1673 1.4.2.2 nathanw }
1674 1.4.2.2 nathanw
1675 1.4.2.2 nathanw static void
1676 1.4.2.4 nathanw trm_msgin_phase0(sc)
1677 1.4.2.2 nathanw struct trm_softc *sc;
1678 1.4.2.2 nathanw {
1679 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1680 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1681 1.4.2.4 nathanw struct trm_srb *srb;
1682 1.4.2.4 nathanw struct scsipi_periph *periph;
1683 1.4.2.4 nathanw struct trm_tinfo *ti;
1684 1.4.2.4 nathanw int index;
1685 1.4.2.2 nathanw u_int8_t msgin_code;
1686 1.4.2.2 nathanw
1687 1.4.2.2 nathanw msgin_code = bus_space_read_1(iot, ioh, TRM_SCSI_FIFO);
1688 1.4.2.4 nathanw if (sc->sc_state != TRM_EXTEND_MSGIN) {
1689 1.4.2.4 nathanw DPRINTF(("msgin: code = %02x\n", msgin_code));
1690 1.4.2.4 nathanw switch (msgin_code) {
1691 1.4.2.4 nathanw case MSG_DISCONNECT:
1692 1.4.2.4 nathanw sc->sc_state = TRM_DISCONNECTED;
1693 1.4.2.4 nathanw break;
1694 1.4.2.4 nathanw
1695 1.4.2.4 nathanw case MSG_SAVEDATAPOINTER:
1696 1.4.2.4 nathanw break;
1697 1.4.2.4 nathanw
1698 1.4.2.4 nathanw case MSG_EXTENDED:
1699 1.4.2.4 nathanw case MSG_SIMPLE_Q_TAG:
1700 1.4.2.4 nathanw case MSG_HEAD_OF_Q_TAG:
1701 1.4.2.4 nathanw case MSG_ORDERED_Q_TAG:
1702 1.4.2.4 nathanw sc->sc_state = TRM_EXTEND_MSGIN;
1703 1.4.2.2 nathanw /* extended message (01h) */
1704 1.4.2.4 nathanw sc->sc_msgbuf[0] = msgin_code;
1705 1.4.2.2 nathanw
1706 1.4.2.4 nathanw sc->sc_msgcnt = 1;
1707 1.4.2.2 nathanw /* extended message length (n) */
1708 1.4.2.4 nathanw sc->sc_msg = &sc->sc_msgbuf[1];
1709 1.4.2.2 nathanw
1710 1.4.2.4 nathanw break;
1711 1.4.2.4 nathanw case MSG_MESSAGE_REJECT:
1712 1.4.2.2 nathanw /* Reject message */
1713 1.4.2.4 nathanw srb = sc->sc_actsrb;
1714 1.4.2.4 nathanw if (srb == NULL) {
1715 1.4.2.4 nathanw DPRINTF(("trm_msgin_phase0: "
1716 1.4.2.4 nathanw " message reject without actsrb\n"));
1717 1.4.2.4 nathanw break;
1718 1.4.2.4 nathanw }
1719 1.4.2.4 nathanw periph = srb->xs->xs_periph;
1720 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
1721 1.4.2.4 nathanw
1722 1.4.2.4 nathanw if (ti->flag & WIDE_NEGO_ENABLE) {
1723 1.4.2.4 nathanw /* do wide nego reject */
1724 1.4.2.4 nathanw ti->flag |= WIDE_NEGO_DONE;
1725 1.4.2.4 nathanw ti->flag &=
1726 1.4.2.4 nathanw ~(SYNC_NEGO_DONE | WIDE_NEGO_ENABLE);
1727 1.4.2.4 nathanw if ((ti->flag & SYNC_NEGO_ENABLE) &&
1728 1.4.2.4 nathanw (ti->flag & SYNC_NEGO_DONE) == 0) {
1729 1.4.2.2 nathanw /* Set ATN, in case ATN was clear */
1730 1.4.2.4 nathanw sc->sc_state = TRM_MSGOUT;
1731 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1732 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_SETATN);
1733 1.4.2.2 nathanw } else
1734 1.4.2.2 nathanw /* Clear ATN */
1735 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1736 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_CLRATN);
1737 1.4.2.4 nathanw } else if (ti->flag & SYNC_NEGO_ENABLE) {
1738 1.4.2.2 nathanw /* do sync nego reject */
1739 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1740 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_CLRATN);
1741 1.4.2.4 nathanw if (ti->flag & SYNC_NEGO_DOING) {
1742 1.4.2.4 nathanw ti->flag &=~(SYNC_NEGO_ENABLE |
1743 1.4.2.2 nathanw SYNC_NEGO_DONE);
1744 1.4.2.4 nathanw ti->synctl = 0;
1745 1.4.2.4 nathanw ti->offset = 0;
1746 1.4.2.4 nathanw bus_space_write_1(iot, ioh,
1747 1.4.2.4 nathanw TRM_SCSI_SYNC, ti->synctl);
1748 1.4.2.4 nathanw bus_space_write_1(iot, ioh,
1749 1.4.2.4 nathanw TRM_SCSI_OFFSET, ti->offset);
1750 1.4.2.2 nathanw }
1751 1.4.2.2 nathanw }
1752 1.4.2.4 nathanw break;
1753 1.4.2.4 nathanw
1754 1.4.2.4 nathanw case MSG_IGN_WIDE_RESIDUE:
1755 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_SCSI_XCNT, 1);
1756 1.4.2.2 nathanw bus_space_read_1(iot, ioh, TRM_SCSI_FIFO);
1757 1.4.2.4 nathanw break;
1758 1.4.2.4 nathanw
1759 1.4.2.4 nathanw default:
1760 1.4.2.2 nathanw /*
1761 1.4.2.2 nathanw * Restore data pointer message
1762 1.4.2.2 nathanw * Save data pointer message
1763 1.4.2.2 nathanw * Completion message
1764 1.4.2.2 nathanw * NOP message
1765 1.4.2.2 nathanw */
1766 1.4.2.4 nathanw break;
1767 1.4.2.2 nathanw }
1768 1.4.2.2 nathanw } else {
1769 1.4.2.2 nathanw /*
1770 1.4.2.4 nathanw * when extend message in: sc->sc_state = TRM_EXTEND_MSGIN
1771 1.4.2.4 nathanw * Parsing incoming extented messages
1772 1.4.2.2 nathanw */
1773 1.4.2.4 nathanw *sc->sc_msg++ = msgin_code;
1774 1.4.2.4 nathanw sc->sc_msgcnt++;
1775 1.4.2.4 nathanw
1776 1.4.2.4 nathanw DPRINTF(("extended_msgin: cnt = %d, ", sc->sc_msgcnt));
1777 1.4.2.4 nathanw DPRINTF(("msgbuf = %02x %02x %02x %02x %02x %02x\n",
1778 1.4.2.4 nathanw sc->sc_msgbuf[0], sc->sc_msgbuf[1], sc->sc_msgbuf[2],
1779 1.4.2.4 nathanw sc->sc_msgbuf[3], sc->sc_msgbuf[4], sc->sc_msgbuf[5]));
1780 1.4.2.4 nathanw
1781 1.4.2.4 nathanw switch (sc->sc_msgbuf[0]) {
1782 1.4.2.4 nathanw case MSG_SIMPLE_Q_TAG:
1783 1.4.2.4 nathanw case MSG_HEAD_OF_Q_TAG:
1784 1.4.2.4 nathanw case MSG_ORDERED_Q_TAG:
1785 1.4.2.2 nathanw /*
1786 1.4.2.2 nathanw * is QUEUE tag message :
1787 1.4.2.2 nathanw *
1788 1.4.2.2 nathanw * byte 0:
1789 1.4.2.2 nathanw * HEAD QUEUE TAG (20h)
1790 1.4.2.2 nathanw * ORDERED QUEUE TAG (21h)
1791 1.4.2.2 nathanw * SIMPLE QUEUE TAG (22h)
1792 1.4.2.2 nathanw * byte 1:
1793 1.4.2.2 nathanw * Queue tag (00h - FFh)
1794 1.4.2.2 nathanw */
1795 1.4.2.4 nathanw if (sc->sc_msgcnt == 2 && sc->sc_actsrb == NULL) {
1796 1.4.2.4 nathanw /* XXX XXX XXX */
1797 1.4.2.4 nathanw struct trm_linfo *li;
1798 1.4.2.4 nathanw int tagid;
1799 1.4.2.4 nathanw
1800 1.4.2.4 nathanw sc->sc_flag &= ~WAIT_TAGMSG;
1801 1.4.2.4 nathanw tagid = sc->sc_msgbuf[1];
1802 1.4.2.4 nathanw ti = &sc->sc_tinfo[sc->resel_target];
1803 1.4.2.4 nathanw li = ti->linfo[sc->resel_lun];
1804 1.4.2.4 nathanw srb = li->queued[tagid];
1805 1.4.2.4 nathanw if (srb != NULL) {
1806 1.4.2.4 nathanw sc->sc_actsrb = srb;
1807 1.4.2.4 nathanw sc->sc_state = TRM_DATA_XFER;
1808 1.4.2.4 nathanw break;
1809 1.4.2.2 nathanw } else {
1810 1.4.2.4 nathanw printf("%s: invalid tag id\n",
1811 1.4.2.4 nathanw sc->sc_dev.dv_xname);
1812 1.4.2.2 nathanw }
1813 1.4.2.4 nathanw
1814 1.4.2.4 nathanw sc->sc_state = TRM_UNEXPECT_RESEL;
1815 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_ABORT_TAG;
1816 1.4.2.4 nathanw sc->sc_msgcnt = 1;
1817 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1818 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_SETATN);
1819 1.4.2.4 nathanw } else
1820 1.4.2.4 nathanw sc->sc_state = TRM_IDLE;
1821 1.4.2.4 nathanw break;
1822 1.4.2.4 nathanw
1823 1.4.2.4 nathanw case MSG_EXTENDED:
1824 1.4.2.4 nathanw srb = sc->sc_actsrb;
1825 1.4.2.4 nathanw if (srb == NULL) {
1826 1.4.2.4 nathanw DPRINTF(("trm_msgin_phase0: "
1827 1.4.2.4 nathanw "extended message without actsrb\n"));
1828 1.4.2.4 nathanw break;
1829 1.4.2.2 nathanw }
1830 1.4.2.4 nathanw periph = srb->xs->xs_periph;
1831 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
1832 1.4.2.4 nathanw
1833 1.4.2.4 nathanw if (sc->sc_msgbuf[2] == MSG_EXT_WDTR &&
1834 1.4.2.4 nathanw sc->sc_msgcnt == 4) {
1835 1.4.2.4 nathanw /*
1836 1.4.2.4 nathanw * is Wide data xfer Extended message :
1837 1.4.2.4 nathanw * ======================================
1838 1.4.2.4 nathanw * WIDE DATA TRANSFER REQUEST
1839 1.4.2.4 nathanw * ======================================
1840 1.4.2.4 nathanw * byte 0 : Extended message (01h)
1841 1.4.2.4 nathanw * byte 1 : Extended message length (02h)
1842 1.4.2.4 nathanw * byte 2 : WIDE DATA TRANSFER code (03h)
1843 1.4.2.4 nathanw * byte 3 : Transfer width exponent
1844 1.4.2.4 nathanw */
1845 1.4.2.4 nathanw if (sc->sc_msgbuf[1] != MSG_EXT_WDTR_LEN) {
1846 1.4.2.4 nathanw /* Length is wrong, reject it */
1847 1.4.2.4 nathanw ti->flag &= ~(WIDE_NEGO_ENABLE |
1848 1.4.2.4 nathanw WIDE_NEGO_DONE);
1849 1.4.2.4 nathanw sc->sc_state = TRM_MSGOUT;
1850 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_MESSAGE_REJECT;
1851 1.4.2.4 nathanw sc->sc_msgcnt = 1;
1852 1.4.2.4 nathanw bus_space_write_2(iot, ioh,
1853 1.4.2.4 nathanw TRM_SCSI_CONTROL, DO_SETATN);
1854 1.4.2.4 nathanw break;
1855 1.4.2.4 nathanw }
1856 1.4.2.4 nathanw
1857 1.4.2.4 nathanw if ((ti->flag & WIDE_NEGO_ENABLE) == 0)
1858 1.4.2.4 nathanw sc->sc_msgbuf[3] =
1859 1.4.2.4 nathanw MSG_EXT_WDTR_BUS_8_BIT;
1860 1.4.2.4 nathanw
1861 1.4.2.4 nathanw if (sc->sc_msgbuf[3] >
1862 1.4.2.4 nathanw MSG_EXT_WDTR_BUS_32_BIT) {
1863 1.4.2.2 nathanw /* reject_msg: */
1864 1.4.2.4 nathanw ti->flag &= ~(WIDE_NEGO_ENABLE |
1865 1.4.2.2 nathanw WIDE_NEGO_DONE);
1866 1.4.2.4 nathanw sc->sc_state = TRM_MSGOUT;
1867 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_MESSAGE_REJECT;
1868 1.4.2.4 nathanw sc->sc_msgcnt = 1;
1869 1.4.2.2 nathanw bus_space_write_2(iot, ioh,
1870 1.4.2.2 nathanw TRM_SCSI_CONTROL, DO_SETATN);
1871 1.4.2.4 nathanw break;
1872 1.4.2.2 nathanw }
1873 1.4.2.4 nathanw if (sc->sc_msgbuf[3] == MSG_EXT_WDTR_BUS_32_BIT)
1874 1.4.2.2 nathanw /* do 16 bits */
1875 1.4.2.4 nathanw sc->sc_msgbuf[3] =
1876 1.4.2.4 nathanw MSG_EXT_WDTR_BUS_16_BIT;
1877 1.4.2.4 nathanw if ((ti->flag & WIDE_NEGO_DONE) == 0) {
1878 1.4.2.4 nathanw ti->flag |= WIDE_NEGO_DONE;
1879 1.4.2.4 nathanw ti->flag &= ~(SYNC_NEGO_DONE |
1880 1.4.2.4 nathanw WIDE_NEGO_ENABLE);
1881 1.4.2.4 nathanw if (sc->sc_msgbuf[3] !=
1882 1.4.2.4 nathanw MSG_EXT_WDTR_BUS_8_BIT)
1883 1.4.2.4 nathanw /* is Wide data xfer */
1884 1.4.2.4 nathanw ti->synctl |= WIDE_SYNC;
1885 1.4.2.4 nathanw trm_update_xfer_mode(sc,
1886 1.4.2.4 nathanw periph->periph_target);
1887 1.4.2.2 nathanw }
1888 1.4.2.2 nathanw
1889 1.4.2.4 nathanw sc->sc_state = TRM_MSGOUT;
1890 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL,
1891 1.4.2.2 nathanw DO_SETATN);
1892 1.4.2.4 nathanw break;
1893 1.4.2.2 nathanw
1894 1.4.2.4 nathanw } else if (sc->sc_msgbuf[2] == MSG_EXT_SDTR &&
1895 1.4.2.4 nathanw sc->sc_msgcnt == 5) {
1896 1.4.2.2 nathanw /*
1897 1.4.2.4 nathanw * is 8bit transfer Extended message :
1898 1.4.2.4 nathanw * =================================
1899 1.4.2.4 nathanw * SYNCHRONOUS DATA TRANSFER REQUEST
1900 1.4.2.4 nathanw * =================================
1901 1.4.2.4 nathanw * byte 0 : Extended message (01h)
1902 1.4.2.4 nathanw * byte 1 : Extended message length (03)
1903 1.4.2.4 nathanw * byte 2 : SYNC DATA TRANSFER code (01h)
1904 1.4.2.4 nathanw * byte 3 : Transfer period factor
1905 1.4.2.4 nathanw * byte 4 : REQ/ACK offset
1906 1.4.2.2 nathanw */
1907 1.4.2.4 nathanw if (sc->sc_msgbuf[1] != MSG_EXT_SDTR_LEN) {
1908 1.4.2.4 nathanw /* reject_msg */
1909 1.4.2.4 nathanw sc->sc_state = TRM_MSGOUT;
1910 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_MESSAGE_REJECT;
1911 1.4.2.4 nathanw sc->sc_msgcnt = 1;
1912 1.4.2.4 nathanw bus_space_write_2(iot, ioh,
1913 1.4.2.4 nathanw TRM_SCSI_CONTROL, DO_SETATN);
1914 1.4.2.4 nathanw break;
1915 1.4.2.2 nathanw }
1916 1.4.2.4 nathanw
1917 1.4.2.4 nathanw if ((ti->flag & SYNC_NEGO_DONE) == 0) {
1918 1.4.2.4 nathanw ti->flag &=
1919 1.4.2.4 nathanw ~(SYNC_NEGO_ENABLE|SYNC_NEGO_DOING);
1920 1.4.2.4 nathanw ti->flag |= SYNC_NEGO_DONE;
1921 1.4.2.4 nathanw if (sc->sc_msgbuf[3] >= TRM_MAX_PERIOD)
1922 1.4.2.4 nathanw sc->sc_msgbuf[3] = 0;
1923 1.4.2.4 nathanw if (sc->sc_msgbuf[4] > TRM_MAX_OFFSET)
1924 1.4.2.4 nathanw sc->sc_msgbuf[4] =
1925 1.4.2.4 nathanw TRM_MAX_OFFSET;
1926 1.4.2.4 nathanw
1927 1.4.2.4 nathanw if (sc->sc_msgbuf[3] == 0 ||
1928 1.4.2.4 nathanw sc->sc_msgbuf[4] == 0) {
1929 1.4.2.4 nathanw /* set async */
1930 1.4.2.4 nathanw ti->synctl = 0;
1931 1.4.2.4 nathanw ti->offset = 0;
1932 1.4.2.4 nathanw } else {
1933 1.4.2.4 nathanw /* set sync */
1934 1.4.2.4 nathanw /* Transfer period factor */
1935 1.4.2.4 nathanw ti->period = sc->sc_msgbuf[3];
1936 1.4.2.4 nathanw /* REQ/ACK offset */
1937 1.4.2.4 nathanw ti->offset = sc->sc_msgbuf[4];
1938 1.4.2.4 nathanw for (index = 0;
1939 1.4.2.4 nathanw index < NPERIOD;
1940 1.4.2.4 nathanw index++)
1941 1.4.2.4 nathanw if (ti->period <=
1942 1.4.2.4 nathanw trm_clock_period[
1943 1.4.2.4 nathanw index])
1944 1.4.2.4 nathanw break;
1945 1.4.2.4 nathanw
1946 1.4.2.4 nathanw ti->synctl |= ALT_SYNC | index;
1947 1.4.2.4 nathanw }
1948 1.4.2.4 nathanw /*
1949 1.4.2.4 nathanw * program SCSI control register
1950 1.4.2.4 nathanw */
1951 1.4.2.4 nathanw bus_space_write_1(iot, ioh,
1952 1.4.2.4 nathanw TRM_SCSI_SYNC, ti->synctl);
1953 1.4.2.4 nathanw bus_space_write_1(iot, ioh,
1954 1.4.2.4 nathanw TRM_SCSI_OFFSET, ti->offset);
1955 1.4.2.4 nathanw trm_update_xfer_mode(sc,
1956 1.4.2.4 nathanw periph->periph_target);
1957 1.4.2.4 nathanw }
1958 1.4.2.4 nathanw sc->sc_state = TRM_IDLE;
1959 1.4.2.2 nathanw }
1960 1.4.2.4 nathanw break;
1961 1.4.2.4 nathanw default:
1962 1.4.2.4 nathanw break;
1963 1.4.2.2 nathanw }
1964 1.4.2.2 nathanw }
1965 1.4.2.4 nathanw
1966 1.4.2.2 nathanw /* it's important for atn stop */
1967 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
1968 1.4.2.2 nathanw
1969 1.4.2.2 nathanw /*
1970 1.4.2.4 nathanw * SCSI command
1971 1.4.2.2 nathanw */
1972 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_MSGACCEPT);
1973 1.4.2.2 nathanw }
1974 1.4.2.2 nathanw
1975 1.4.2.2 nathanw static void
1976 1.4.2.4 nathanw trm_msgin_phase1(sc)
1977 1.4.2.2 nathanw struct trm_softc *sc;
1978 1.4.2.2 nathanw {
1979 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
1980 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
1981 1.4.2.2 nathanw
1982 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRFIFO);
1983 1.4.2.2 nathanw bus_space_write_4(iot, ioh, TRM_SCSI_XCNT, 1);
1984 1.4.2.4 nathanw if (sc->sc_state != TRM_MSGIN && sc->sc_state != TRM_EXTEND_MSGIN) {
1985 1.4.2.4 nathanw sc->sc_state = TRM_MSGIN;
1986 1.4.2.2 nathanw }
1987 1.4.2.4 nathanw
1988 1.4.2.2 nathanw /* it's important for atn stop */
1989 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
1990 1.4.2.2 nathanw
1991 1.4.2.2 nathanw /*
1992 1.4.2.4 nathanw * SCSI command
1993 1.4.2.2 nathanw */
1994 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_FIFO_IN);
1995 1.4.2.2 nathanw }
1996 1.4.2.2 nathanw
1997 1.4.2.2 nathanw static void
1998 1.4.2.2 nathanw trm_disconnect(sc)
1999 1.4.2.2 nathanw struct trm_softc *sc;
2000 1.4.2.2 nathanw {
2001 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2002 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2003 1.4.2.4 nathanw struct trm_srb *srb;
2004 1.4.2.4 nathanw int s;
2005 1.4.2.2 nathanw
2006 1.4.2.2 nathanw s = splbio();
2007 1.4.2.2 nathanw
2008 1.4.2.4 nathanw srb = sc->sc_actsrb;
2009 1.4.2.4 nathanw DPRINTF(("trm_disconnect...............\n"));
2010 1.4.2.4 nathanw
2011 1.4.2.4 nathanw if (srb == NULL) {
2012 1.4.2.4 nathanw DPRINTF(("trm_disconnect: no active srb\n"));
2013 1.4.2.2 nathanw DELAY(1000); /* 1 msec */
2014 1.4.2.2 nathanw
2015 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL,
2016 1.4.2.2 nathanw DO_CLRFIFO | DO_HWRESELECT);
2017 1.4.2.2 nathanw return;
2018 1.4.2.2 nathanw }
2019 1.4.2.4 nathanw sc->sc_phase = PH_BUS_FREE; /* SCSI bus free Phase */
2020 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL,
2021 1.4.2.2 nathanw DO_CLRFIFO | DO_HWRESELECT);
2022 1.4.2.2 nathanw DELAY(100);
2023 1.4.2.4 nathanw
2024 1.4.2.4 nathanw switch (sc->sc_state) {
2025 1.4.2.4 nathanw case TRM_UNEXPECT_RESEL:
2026 1.4.2.4 nathanw sc->sc_state = TRM_IDLE;
2027 1.4.2.4 nathanw break;
2028 1.4.2.4 nathanw
2029 1.4.2.4 nathanw case TRM_ABORT_SENT:
2030 1.4.2.4 nathanw goto finish;
2031 1.4.2.4 nathanw
2032 1.4.2.4 nathanw case TRM_START:
2033 1.4.2.4 nathanw case TRM_MSGOUT:
2034 1.4.2.4 nathanw {
2035 1.4.2.4 nathanw /* Selection time out - discard all LUNs if empty */
2036 1.4.2.4 nathanw struct scsipi_periph *periph;
2037 1.4.2.4 nathanw struct trm_tinfo *ti;
2038 1.4.2.4 nathanw struct trm_linfo *li;
2039 1.4.2.4 nathanw int lun;
2040 1.4.2.4 nathanw
2041 1.4.2.4 nathanw DPRINTF(("selection timeout\n"));
2042 1.4.2.4 nathanw
2043 1.4.2.4 nathanw srb->tastat = SCSI_SEL_TIMEOUT; /* XXX Ok? */
2044 1.4.2.4 nathanw
2045 1.4.2.4 nathanw periph = srb->xs->xs_periph;
2046 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
2047 1.4.2.4 nathanw for (lun = 0; lun < TRM_MAX_LUNS; lun++) {
2048 1.4.2.4 nathanw li = ti->linfo[lun];
2049 1.4.2.4 nathanw if (li != NULL &&
2050 1.4.2.4 nathanw li->untagged == NULL && li->used == 0) {
2051 1.4.2.4 nathanw ti->linfo[lun] = NULL;
2052 1.4.2.4 nathanw free(li, M_DEVBUF);
2053 1.4.2.4 nathanw }
2054 1.4.2.2 nathanw }
2055 1.4.2.2 nathanw }
2056 1.4.2.4 nathanw goto finish;
2057 1.4.2.4 nathanw
2058 1.4.2.4 nathanw case TRM_DISCONNECTED:
2059 1.4.2.4 nathanw sc->sc_actsrb = NULL;
2060 1.4.2.4 nathanw sc->sc_state = TRM_IDLE;
2061 1.4.2.4 nathanw goto sched;
2062 1.4.2.4 nathanw
2063 1.4.2.4 nathanw case TRM_COMPLETED:
2064 1.4.2.4 nathanw goto finish;
2065 1.4.2.2 nathanw }
2066 1.4.2.4 nathanw
2067 1.4.2.4 nathanw out:
2068 1.4.2.2 nathanw splx(s);
2069 1.4.2.4 nathanw return;
2070 1.4.2.4 nathanw
2071 1.4.2.4 nathanw finish:
2072 1.4.2.4 nathanw sc->sc_state = TRM_IDLE;
2073 1.4.2.4 nathanw trm_done(sc, srb);
2074 1.4.2.4 nathanw goto out;
2075 1.4.2.4 nathanw
2076 1.4.2.4 nathanw sched:
2077 1.4.2.4 nathanw trm_sched(sc);
2078 1.4.2.4 nathanw goto out;
2079 1.4.2.2 nathanw }
2080 1.4.2.2 nathanw
2081 1.4.2.2 nathanw static void
2082 1.4.2.2 nathanw trm_reselect(sc)
2083 1.4.2.2 nathanw struct trm_softc *sc;
2084 1.4.2.2 nathanw {
2085 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2086 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2087 1.4.2.4 nathanw struct trm_tinfo *ti;
2088 1.4.2.4 nathanw struct trm_linfo *li;
2089 1.4.2.4 nathanw int target, lun;
2090 1.4.2.4 nathanw
2091 1.4.2.4 nathanw DPRINTF(("trm_reselect.................\n"));
2092 1.4.2.4 nathanw
2093 1.4.2.4 nathanw if (sc->sc_actsrb != NULL) {
2094 1.4.2.4 nathanw /* arbitration lost but reselection win */
2095 1.4.2.4 nathanw sc->sc_state = TRM_READY;
2096 1.4.2.4 nathanw target = sc->sc_actsrb->xs->xs_periph->periph_target;
2097 1.4.2.4 nathanw ti = &sc->sc_tinfo[target];
2098 1.4.2.2 nathanw } else {
2099 1.4.2.4 nathanw /* Read Reselected Target Id and LUN */
2100 1.4.2.4 nathanw target = bus_space_read_1(iot, ioh, TRM_SCSI_TARGETID);
2101 1.4.2.4 nathanw lun = bus_space_read_1(iot, ioh, TRM_SCSI_IDMSG) & 0x07;
2102 1.4.2.4 nathanw ti = &sc->sc_tinfo[target];
2103 1.4.2.4 nathanw li = ti->linfo[lun];
2104 1.4.2.4 nathanw DPRINTF(("target = %d, lun = %d\n", target, lun));
2105 1.4.2.4 nathanw
2106 1.4.2.4 nathanw /*
2107 1.4.2.4 nathanw * Check to see if we are running an un-tagged command.
2108 1.4.2.4 nathanw * Otherwise ack the IDENTIFY and wait for a tag message.
2109 1.4.2.4 nathanw */
2110 1.4.2.4 nathanw if (li != NULL) {
2111 1.4.2.4 nathanw if (li->untagged != NULL && li->busy) {
2112 1.4.2.4 nathanw sc->sc_actsrb = li->untagged;
2113 1.4.2.4 nathanw sc->sc_state = TRM_DATA_XFER;
2114 1.4.2.4 nathanw } else {
2115 1.4.2.4 nathanw sc->resel_target = target;
2116 1.4.2.4 nathanw sc->resel_lun = lun;
2117 1.4.2.4 nathanw /* XXX XXX XXX */
2118 1.4.2.4 nathanw sc->sc_flag |= WAIT_TAGMSG;
2119 1.4.2.4 nathanw }
2120 1.4.2.4 nathanw }
2121 1.4.2.4 nathanw
2122 1.4.2.4 nathanw if ((ti->flag & USE_TAG_QUEUING) == 0 &&
2123 1.4.2.4 nathanw sc->sc_actsrb == NULL) {
2124 1.4.2.4 nathanw printf("%s: reselect from target %d lun %d "
2125 1.4.2.4 nathanw "without nexus; sending abort\n",
2126 1.4.2.4 nathanw sc->sc_dev.dv_xname, target, lun);
2127 1.4.2.4 nathanw sc->sc_state = TRM_UNEXPECT_RESEL;
2128 1.4.2.4 nathanw sc->sc_msgbuf[0] = MSG_ABORT_TAG;
2129 1.4.2.4 nathanw sc->sc_msgcnt = 1;
2130 1.4.2.4 nathanw bus_space_write_2(iot, ioh,
2131 1.4.2.4 nathanw TRM_SCSI_CONTROL, DO_SETATN);
2132 1.4.2.2 nathanw }
2133 1.4.2.2 nathanw }
2134 1.4.2.4 nathanw sc->sc_phase = PH_BUS_FREE; /* SCSI bus free Phase */
2135 1.4.2.2 nathanw /*
2136 1.4.2.2 nathanw * Program HA ID, target ID, period and offset
2137 1.4.2.2 nathanw */
2138 1.4.2.2 nathanw /* target ID */
2139 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_TARGETID, target);
2140 1.4.2.2 nathanw
2141 1.4.2.2 nathanw /* host ID */
2142 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_HOSTID, sc->sc_id);
2143 1.4.2.2 nathanw
2144 1.4.2.2 nathanw /* period */
2145 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_SYNC, ti->synctl);
2146 1.4.2.2 nathanw
2147 1.4.2.2 nathanw /* offset */
2148 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_OFFSET, ti->offset);
2149 1.4.2.2 nathanw
2150 1.4.2.2 nathanw /* it's important for atn stop */
2151 1.4.2.2 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_DATALATCH);
2152 1.4.2.2 nathanw /*
2153 1.4.2.4 nathanw * SCSI command
2154 1.4.2.2 nathanw */
2155 1.4.2.2 nathanw /* to rls the /ACK signal */
2156 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_SCSI_COMMAND, SCMD_MSGACCEPT);
2157 1.4.2.2 nathanw }
2158 1.4.2.2 nathanw
2159 1.4.2.2 nathanw /*
2160 1.4.2.2 nathanw * Complete execution of a SCSI command
2161 1.4.2.2 nathanw * Signal completion to the generic SCSI driver
2162 1.4.2.2 nathanw */
2163 1.4.2.2 nathanw static void
2164 1.4.2.4 nathanw trm_done(sc, srb)
2165 1.4.2.2 nathanw struct trm_softc *sc;
2166 1.4.2.2 nathanw struct trm_srb *srb;
2167 1.4.2.2 nathanw {
2168 1.4.2.2 nathanw struct scsipi_xfer *xs = srb->xs;
2169 1.4.2.2 nathanw
2170 1.4.2.4 nathanw DPRINTF(("trm_done..................\n"));
2171 1.4.2.2 nathanw
2172 1.4.2.2 nathanw if (xs == NULL)
2173 1.4.2.2 nathanw return;
2174 1.4.2.2 nathanw
2175 1.4.2.3 nathanw if ((xs->xs_control & XS_CTL_POLL) == 0)
2176 1.4.2.3 nathanw callout_stop(&xs->xs_callout);
2177 1.4.2.2 nathanw
2178 1.4.2.3 nathanw if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT) ||
2179 1.4.2.3 nathanw srb->flag & AUTO_REQSENSE) {
2180 1.4.2.3 nathanw bus_dmamap_sync(sc->sc_dmat, srb->dmap, 0,
2181 1.4.2.3 nathanw srb->dmap->dm_mapsize,
2182 1.4.2.3 nathanw ((xs->xs_control & XS_CTL_DATA_IN) ||
2183 1.4.2.3 nathanw (srb->flag & AUTO_REQSENSE)) ?
2184 1.4.2.3 nathanw BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2185 1.4.2.3 nathanw bus_dmamap_unload(sc->sc_dmat, srb->dmap);
2186 1.4.2.2 nathanw }
2187 1.4.2.3 nathanw
2188 1.4.2.2 nathanw /*
2189 1.4.2.2 nathanw * target status
2190 1.4.2.2 nathanw */
2191 1.4.2.3 nathanw xs->status = srb->tastat;
2192 1.4.2.3 nathanw
2193 1.4.2.4 nathanw DPRINTF(("xs->status = 0x%02x\n", xs->status));
2194 1.4.2.4 nathanw
2195 1.4.2.3 nathanw switch (xs->status) {
2196 1.4.2.3 nathanw case SCSI_OK:
2197 1.4.2.2 nathanw /*
2198 1.4.2.2 nathanw * process initiator status......
2199 1.4.2.2 nathanw * Adapter (initiator) status
2200 1.4.2.2 nathanw */
2201 1.4.2.4 nathanw if ((srb->hastat & H_OVER_UNDER_RUN) != 0) {
2202 1.4.2.4 nathanw printf("%s: over/under run error\n",
2203 1.4.2.4 nathanw sc->sc_dev.dv_xname);
2204 1.4.2.2 nathanw srb->tastat = 0;
2205 1.4.2.2 nathanw /* Illegal length (over/under run) */
2206 1.4.2.2 nathanw xs->error = XS_DRIVER_STUFFUP;
2207 1.4.2.4 nathanw } else if ((srb->flag & PARITY_ERROR) != 0) {
2208 1.4.2.4 nathanw printf("%s: parity error\n",
2209 1.4.2.4 nathanw sc->sc_dev.dv_xname);
2210 1.4.2.2 nathanw /* Driver failed to perform operation */
2211 1.4.2.4 nathanw xs->error = XS_DRIVER_STUFFUP; /* XXX */
2212 1.4.2.4 nathanw } else if ((srb->flag & SRB_TIMEOUT) != 0) {
2213 1.4.2.4 nathanw xs->resid = srb->buflen;
2214 1.4.2.4 nathanw xs->error = XS_TIMEOUT;
2215 1.4.2.3 nathanw } else {
2216 1.4.2.3 nathanw /* No error */
2217 1.4.2.4 nathanw xs->resid = srb->buflen;
2218 1.4.2.2 nathanw srb->hastat = 0;
2219 1.4.2.3 nathanw if (srb->flag & AUTO_REQSENSE) {
2220 1.4.2.3 nathanw /* there is no error, (sense is invalid) */
2221 1.4.2.3 nathanw xs->error = XS_SENSE;
2222 1.4.2.3 nathanw } else {
2223 1.4.2.3 nathanw srb->tastat = 0;
2224 1.4.2.3 nathanw xs->error = XS_NOERROR;
2225 1.4.2.3 nathanw }
2226 1.4.2.3 nathanw }
2227 1.4.2.3 nathanw break;
2228 1.4.2.4 nathanw
2229 1.4.2.3 nathanw case SCSI_CHECK:
2230 1.4.2.3 nathanw if ((srb->flag & AUTO_REQSENSE) != 0 ||
2231 1.4.2.4 nathanw trm_request_sense(sc, srb) != 0) {
2232 1.4.2.3 nathanw printf("%s: request sense failed\n",
2233 1.4.2.3 nathanw sc->sc_dev.dv_xname);
2234 1.4.2.3 nathanw xs->error = XS_DRIVER_STUFFUP;
2235 1.4.2.3 nathanw break;
2236 1.4.2.2 nathanw }
2237 1.4.2.3 nathanw xs->error = XS_SENSE;
2238 1.4.2.3 nathanw return;
2239 1.4.2.4 nathanw
2240 1.4.2.3 nathanw case SCSI_SEL_TIMEOUT:
2241 1.4.2.3 nathanw srb->hastat = H_SEL_TIMEOUT;
2242 1.4.2.3 nathanw srb->tastat = 0;
2243 1.4.2.4 nathanw xs->error = XS_SELTIMEOUT;
2244 1.4.2.3 nathanw break;
2245 1.4.2.4 nathanw
2246 1.4.2.4 nathanw case SCSI_QUEUE_FULL:
2247 1.4.2.3 nathanw case SCSI_BUSY:
2248 1.4.2.3 nathanw xs->error = XS_BUSY;
2249 1.4.2.3 nathanw break;
2250 1.4.2.4 nathanw
2251 1.4.2.3 nathanw case SCSI_RESV_CONFLICT:
2252 1.4.2.4 nathanw DPRINTF(("%s: target reserved at ", sc->sc_dev.dv_xname));
2253 1.4.2.4 nathanw DPRINTF(("%s %d\n", __FILE__, __LINE__));
2254 1.4.2.3 nathanw xs->error = XS_BUSY;
2255 1.4.2.3 nathanw break;
2256 1.4.2.4 nathanw
2257 1.4.2.3 nathanw default:
2258 1.4.2.3 nathanw srb->hastat = 0;
2259 1.4.2.4 nathanw printf("%s: trm_done(): unknown status = %02x\n",
2260 1.4.2.4 nathanw sc->sc_dev.dv_xname, xs->status);
2261 1.4.2.3 nathanw xs->error = XS_DRIVER_STUFFUP;
2262 1.4.2.3 nathanw break;
2263 1.4.2.2 nathanw }
2264 1.4.2.3 nathanw
2265 1.4.2.4 nathanw trm_dequeue(sc, srb);
2266 1.4.2.4 nathanw if (srb == sc->sc_actsrb) {
2267 1.4.2.4 nathanw sc->sc_actsrb = NULL;
2268 1.4.2.4 nathanw trm_sched(sc);
2269 1.4.2.4 nathanw }
2270 1.4.2.2 nathanw
2271 1.4.2.4 nathanw TAILQ_INSERT_TAIL(&sc->sc_freesrb, srb, next);
2272 1.4.2.2 nathanw
2273 1.4.2.2 nathanw /* Notify cmd done */
2274 1.4.2.2 nathanw scsipi_done(xs);
2275 1.4.2.2 nathanw }
2276 1.4.2.2 nathanw
2277 1.4.2.4 nathanw static int
2278 1.4.2.4 nathanw trm_request_sense(sc, srb)
2279 1.4.2.2 nathanw struct trm_softc *sc;
2280 1.4.2.2 nathanw struct trm_srb *srb;
2281 1.4.2.2 nathanw {
2282 1.4.2.2 nathanw struct scsipi_xfer *xs;
2283 1.4.2.4 nathanw struct scsipi_periph *periph;
2284 1.4.2.4 nathanw struct trm_tinfo *ti;
2285 1.4.2.4 nathanw struct trm_linfo *li;
2286 1.4.2.4 nathanw struct scsipi_sense *ss = (struct scsipi_sense *)srb->cmd;
2287 1.4.2.4 nathanw int error;
2288 1.4.2.2 nathanw
2289 1.4.2.4 nathanw DPRINTF(("trm_request_sense...\n"));
2290 1.4.2.2 nathanw
2291 1.4.2.4 nathanw xs = srb->xs;
2292 1.4.2.4 nathanw periph = xs->xs_periph;
2293 1.4.2.2 nathanw
2294 1.4.2.2 nathanw srb->flag |= AUTO_REQSENSE;
2295 1.4.2.2 nathanw
2296 1.4.2.2 nathanw /* Status of initiator/target */
2297 1.4.2.2 nathanw srb->hastat = 0;
2298 1.4.2.2 nathanw srb->tastat = 0;
2299 1.4.2.2 nathanw
2300 1.4.2.2 nathanw ss->opcode = REQUEST_SENSE;
2301 1.4.2.4 nathanw ss->byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
2302 1.4.2.2 nathanw ss->unused[0] = ss->unused[1] = 0;
2303 1.4.2.2 nathanw ss->length = sizeof(struct scsipi_sense_data);
2304 1.4.2.2 nathanw ss->control = 0;
2305 1.4.2.2 nathanw
2306 1.4.2.2 nathanw srb->buflen = sizeof(struct scsipi_sense_data);
2307 1.4.2.2 nathanw srb->sgcnt = 1;
2308 1.4.2.2 nathanw srb->sgindex = 0;
2309 1.4.2.2 nathanw srb->cmdlen = sizeof(struct scsipi_sense);
2310 1.4.2.2 nathanw
2311 1.4.2.2 nathanw if ((error = bus_dmamap_load(sc->sc_dmat, srb->dmap,
2312 1.4.2.2 nathanw &xs->sense.scsi_sense, srb->buflen, NULL,
2313 1.4.2.2 nathanw BUS_DMA_READ|BUS_DMA_NOWAIT)) != 0) {
2314 1.4.2.3 nathanw return error;
2315 1.4.2.2 nathanw }
2316 1.4.2.2 nathanw bus_dmamap_sync(sc->sc_dmat, srb->dmap, 0,
2317 1.4.2.2 nathanw srb->buflen, BUS_DMASYNC_PREREAD);
2318 1.4.2.2 nathanw
2319 1.4.2.2 nathanw srb->sgentry[0].address = htole32(srb->dmap->dm_segs[0].ds_addr);
2320 1.4.2.2 nathanw srb->sgentry[0].length = htole32(sizeof(struct scsipi_sense_data));
2321 1.4.2.2 nathanw bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, srb->sgoffset,
2322 1.4.2.2 nathanw TRM_SG_SIZE, BUS_DMASYNC_PREWRITE);
2323 1.4.2.2 nathanw
2324 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
2325 1.4.2.4 nathanw li = ti->linfo[periph->periph_lun];
2326 1.4.2.4 nathanw if (li->busy > 0)
2327 1.4.2.4 nathanw li->busy = 0;
2328 1.4.2.4 nathanw trm_dequeue(sc, srb);
2329 1.4.2.4 nathanw li->untagged = srb; /* must be executed first to fix C/A */
2330 1.4.2.4 nathanw li->busy = 2;
2331 1.4.2.4 nathanw
2332 1.4.2.4 nathanw if (srb == sc->sc_actsrb)
2333 1.4.2.4 nathanw trm_select(sc, srb);
2334 1.4.2.4 nathanw else {
2335 1.4.2.4 nathanw TAILQ_INSERT_HEAD(&sc->sc_readysrb, srb, next);
2336 1.4.2.4 nathanw if (sc->sc_actsrb == NULL)
2337 1.4.2.4 nathanw trm_sched(sc);
2338 1.4.2.4 nathanw }
2339 1.4.2.3 nathanw return 0;
2340 1.4.2.2 nathanw }
2341 1.4.2.2 nathanw
2342 1.4.2.2 nathanw static void
2343 1.4.2.4 nathanw trm_dequeue(sc, srb)
2344 1.4.2.2 nathanw struct trm_softc *sc;
2345 1.4.2.2 nathanw struct trm_srb *srb;
2346 1.4.2.2 nathanw {
2347 1.4.2.4 nathanw struct scsipi_periph *periph;
2348 1.4.2.4 nathanw struct trm_tinfo *ti;
2349 1.4.2.4 nathanw struct trm_linfo *li;
2350 1.4.2.4 nathanw
2351 1.4.2.4 nathanw periph = srb->xs->xs_periph;
2352 1.4.2.4 nathanw ti = &sc->sc_tinfo[periph->periph_target];
2353 1.4.2.4 nathanw li = ti->linfo[periph->periph_lun];
2354 1.4.2.2 nathanw
2355 1.4.2.4 nathanw if (li->untagged == srb) {
2356 1.4.2.4 nathanw li->busy = 0;
2357 1.4.2.4 nathanw li->untagged = NULL;
2358 1.4.2.4 nathanw }
2359 1.4.2.4 nathanw if (srb->tag[0] != 0 && li->queued[srb->tag[1]] != NULL) {
2360 1.4.2.4 nathanw li->queued[srb->tag[1]] = NULL;
2361 1.4.2.4 nathanw li->used--;
2362 1.4.2.4 nathanw }
2363 1.4.2.2 nathanw }
2364 1.4.2.2 nathanw
2365 1.4.2.2 nathanw static void
2366 1.4.2.4 nathanw trm_reset_scsi_bus(sc)
2367 1.4.2.2 nathanw struct trm_softc *sc;
2368 1.4.2.2 nathanw {
2369 1.4.2.4 nathanw bus_space_tag_t iot = sc->sc_iot;
2370 1.4.2.4 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2371 1.4.2.4 nathanw int timeout, s;
2372 1.4.2.2 nathanw
2373 1.4.2.4 nathanw DPRINTF(("trm_reset_scsi_bus.........\n"));
2374 1.4.2.2 nathanw
2375 1.4.2.2 nathanw s = splbio();
2376 1.4.2.4 nathanw
2377 1.4.2.4 nathanw sc->sc_flag |= RESET_DEV;
2378 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_RSTSCSI);
2379 1.4.2.4 nathanw for (timeout = 20000; timeout >= 0; timeout--) {
2380 1.4.2.4 nathanw DELAY(1);
2381 1.4.2.4 nathanw if ((bus_space_read_2(iot, ioh, TRM_SCSI_INTSTATUS) &
2382 1.4.2.4 nathanw INT_SCSIRESET) == 0)
2383 1.4.2.4 nathanw break;
2384 1.4.2.2 nathanw }
2385 1.4.2.4 nathanw if (timeout == 0)
2386 1.4.2.4 nathanw printf(": scsibus reset timeout\n");
2387 1.4.2.4 nathanw
2388 1.4.2.2 nathanw splx(s);
2389 1.4.2.2 nathanw }
2390 1.4.2.2 nathanw
2391 1.4.2.2 nathanw static void
2392 1.4.2.4 nathanw trm_scsi_reset_detect(sc)
2393 1.4.2.2 nathanw struct trm_softc *sc;
2394 1.4.2.2 nathanw {
2395 1.4.2.4 nathanw bus_space_tag_t iot = sc->sc_iot;
2396 1.4.2.4 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2397 1.4.2.4 nathanw int s;
2398 1.4.2.2 nathanw
2399 1.4.2.4 nathanw DPRINTF(("trm_scsi_reset_detect...............\n"));
2400 1.4.2.4 nathanw DELAY(1000000); /* delay 1 sec */
2401 1.4.2.2 nathanw
2402 1.4.2.4 nathanw s = splbio();
2403 1.4.2.4 nathanw
2404 1.4.2.4 nathanw bus_space_write_1(iot, ioh, TRM_DMA_CONTROL, STOPDMAXFER);
2405 1.4.2.4 nathanw bus_space_write_2(iot, ioh, TRM_SCSI_CONTROL, DO_CLRFIFO);
2406 1.4.2.4 nathanw
2407 1.4.2.4 nathanw if (sc->sc_flag & RESET_DEV) {
2408 1.4.2.4 nathanw sc->sc_flag |= RESET_DONE;
2409 1.4.2.4 nathanw } else {
2410 1.4.2.4 nathanw sc->sc_flag |= RESET_DETECT;
2411 1.4.2.4 nathanw sc->sc_actsrb = NULL;
2412 1.4.2.4 nathanw sc->sc_flag = 0;
2413 1.4.2.4 nathanw trm_sched(sc);
2414 1.4.2.2 nathanw }
2415 1.4.2.4 nathanw splx(s);
2416 1.4.2.2 nathanw }
2417 1.4.2.2 nathanw
2418 1.4.2.2 nathanw /*
2419 1.4.2.4 nathanw * read seeprom 128 bytes to struct eeprom and check checksum.
2420 1.4.2.4 nathanw * If it is wrong, update with default value.
2421 1.4.2.2 nathanw */
2422 1.4.2.2 nathanw static void
2423 1.4.2.4 nathanw trm_check_eeprom(sc, eeprom)
2424 1.4.2.2 nathanw struct trm_softc *sc;
2425 1.4.2.2 nathanw struct trm_nvram *eeprom;
2426 1.4.2.4 nathanw {
2427 1.4.2.4 nathanw struct nvram_target *target;
2428 1.4.2.4 nathanw u_int16_t *ep;
2429 1.4.2.4 nathanw u_int16_t chksum;
2430 1.4.2.4 nathanw int i;
2431 1.4.2.2 nathanw
2432 1.4.2.4 nathanw DPRINTF(("trm_check_eeprom......\n"));
2433 1.4.2.4 nathanw trm_eeprom_read_all(sc, eeprom);
2434 1.4.2.4 nathanw ep = (u_int16_t *)eeprom;
2435 1.4.2.4 nathanw chksum = 0;
2436 1.4.2.4 nathanw for (i = 0; i < 64; i++)
2437 1.4.2.4 nathanw chksum += le16toh(*ep++);
2438 1.4.2.2 nathanw
2439 1.4.2.4 nathanw if (chksum != TRM_NVRAM_CKSUM) {
2440 1.4.2.4 nathanw DPRINTF(("TRM_S1040 EEPROM Check Sum ERROR (load default).\n"));
2441 1.4.2.4 nathanw /*
2442 1.4.2.4 nathanw * Checksum error, load default
2443 1.4.2.4 nathanw */
2444 1.4.2.4 nathanw eeprom->subvendor_id[0] = PCI_VENDOR_TEKRAM2 & 0xFF;
2445 1.4.2.4 nathanw eeprom->subvendor_id[1] = PCI_VENDOR_TEKRAM2 >> 8;
2446 1.4.2.4 nathanw eeprom->subsys_id[0] = PCI_PRODUCT_TEKRAM2_DC315 & 0xFF;
2447 1.4.2.4 nathanw eeprom->subsys_id[1] = PCI_PRODUCT_TEKRAM2_DC315 >> 8;
2448 1.4.2.4 nathanw eeprom->subclass = 0x00;
2449 1.4.2.4 nathanw eeprom->vendor_id[0] = PCI_VENDOR_TEKRAM2 & 0xFF;
2450 1.4.2.4 nathanw eeprom->vendor_id[1] = PCI_VENDOR_TEKRAM2 >> 8;
2451 1.4.2.4 nathanw eeprom->device_id[0] = PCI_PRODUCT_TEKRAM2_DC315 & 0xFF;
2452 1.4.2.4 nathanw eeprom->device_id[1] = PCI_PRODUCT_TEKRAM2_DC315 >> 8;
2453 1.4.2.4 nathanw eeprom->reserved0 = 0x00;
2454 1.4.2.2 nathanw
2455 1.4.2.4 nathanw for (i = 0, target = eeprom->target;
2456 1.4.2.4 nathanw i < TRM_MAX_TARGETS;
2457 1.4.2.4 nathanw i++, target++) {
2458 1.4.2.4 nathanw target->config0 = 0x77;
2459 1.4.2.4 nathanw target->period = 0x00;
2460 1.4.2.4 nathanw target->config2 = 0x00;
2461 1.4.2.4 nathanw target->config3 = 0x00;
2462 1.4.2.4 nathanw }
2463 1.4.2.2 nathanw
2464 1.4.2.4 nathanw eeprom->scsi_id = 7;
2465 1.4.2.4 nathanw eeprom->channel_cfg = 0x0F;
2466 1.4.2.4 nathanw eeprom->delay_time = 0;
2467 1.4.2.4 nathanw eeprom->max_tag = 4;
2468 1.4.2.4 nathanw eeprom->reserved1 = 0x15;
2469 1.4.2.4 nathanw eeprom->boot_target = 0;
2470 1.4.2.4 nathanw eeprom->boot_lun = 0;
2471 1.4.2.4 nathanw eeprom->reserved2 = 0;
2472 1.4.2.4 nathanw memset(eeprom->reserved3, 0, sizeof(eeprom->reserved3));
2473 1.4.2.4 nathanw
2474 1.4.2.4 nathanw chksum = 0;
2475 1.4.2.4 nathanw ep = (u_int16_t *)eeprom;
2476 1.4.2.4 nathanw for (i = 0; i < 63; i++)
2477 1.4.2.4 nathanw chksum += le16toh(*ep++);
2478 1.4.2.4 nathanw
2479 1.4.2.4 nathanw chksum = TRM_NVRAM_CKSUM - chksum;
2480 1.4.2.4 nathanw eeprom->checksum0 = chksum & 0xFF;
2481 1.4.2.4 nathanw eeprom->checksum1 = chksum >> 8;
2482 1.4.2.4 nathanw
2483 1.4.2.4 nathanw trm_eeprom_write_all(sc, eeprom);
2484 1.4.2.4 nathanw }
2485 1.4.2.2 nathanw }
2486 1.4.2.2 nathanw
2487 1.4.2.2 nathanw /*
2488 1.4.2.4 nathanw * write struct eeprom 128 bytes to seeprom
2489 1.4.2.2 nathanw */
2490 1.4.2.2 nathanw static void
2491 1.4.2.2 nathanw trm_eeprom_write_all(sc, eeprom)
2492 1.4.2.2 nathanw struct trm_softc *sc;
2493 1.4.2.2 nathanw struct trm_nvram *eeprom;
2494 1.4.2.2 nathanw {
2495 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2496 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2497 1.4.2.2 nathanw u_int8_t *buf = (u_int8_t *)eeprom;
2498 1.4.2.2 nathanw u_int8_t addr;
2499 1.4.2.2 nathanw
2500 1.4.2.2 nathanw /* Enable SEEPROM */
2501 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_CONTROL,
2502 1.4.2.2 nathanw bus_space_read_1(iot, ioh, TRM_GEN_CONTROL) | EN_EEPROM);
2503 1.4.2.2 nathanw
2504 1.4.2.2 nathanw /*
2505 1.4.2.2 nathanw * Write enable
2506 1.4.2.2 nathanw */
2507 1.4.2.2 nathanw trm_eeprom_write_cmd(sc, 0x04, 0xFF);
2508 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, 0);
2509 1.4.2.4 nathanw trm_eeprom_wait();
2510 1.4.2.2 nathanw
2511 1.4.2.2 nathanw for (addr = 0; addr < 128; addr++, buf++)
2512 1.4.2.2 nathanw trm_eeprom_set_data(sc, addr, *buf);
2513 1.4.2.2 nathanw
2514 1.4.2.2 nathanw /*
2515 1.4.2.2 nathanw * Write disable
2516 1.4.2.2 nathanw */
2517 1.4.2.2 nathanw trm_eeprom_write_cmd(sc, 0x04, 0x00);
2518 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, 0);
2519 1.4.2.4 nathanw trm_eeprom_wait();
2520 1.4.2.2 nathanw
2521 1.4.2.2 nathanw /* Disable SEEPROM */
2522 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_CONTROL,
2523 1.4.2.2 nathanw bus_space_read_1(iot, ioh, TRM_GEN_CONTROL) & ~EN_EEPROM);
2524 1.4.2.2 nathanw }
2525 1.4.2.2 nathanw
2526 1.4.2.2 nathanw /*
2527 1.4.2.2 nathanw * write one byte to seeprom
2528 1.4.2.2 nathanw */
2529 1.4.2.2 nathanw static void
2530 1.4.2.2 nathanw trm_eeprom_set_data(sc, addr, data)
2531 1.4.2.2 nathanw struct trm_softc *sc;
2532 1.4.2.2 nathanw u_int8_t addr;
2533 1.4.2.2 nathanw u_int8_t data;
2534 1.4.2.2 nathanw {
2535 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2536 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2537 1.4.2.2 nathanw int i;
2538 1.4.2.2 nathanw u_int8_t send;
2539 1.4.2.2 nathanw
2540 1.4.2.2 nathanw /*
2541 1.4.2.2 nathanw * Send write command & address
2542 1.4.2.2 nathanw */
2543 1.4.2.2 nathanw trm_eeprom_write_cmd(sc, 0x05, addr);
2544 1.4.2.2 nathanw /*
2545 1.4.2.2 nathanw * Write data
2546 1.4.2.2 nathanw */
2547 1.4.2.2 nathanw for (i = 0; i < 8; i++, data <<= 1) {
2548 1.4.2.2 nathanw send = NVR_SELECT;
2549 1.4.2.2 nathanw if (data & 0x80) /* Start from bit 7 */
2550 1.4.2.2 nathanw send |= NVR_BITOUT;
2551 1.4.2.2 nathanw
2552 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, send);
2553 1.4.2.4 nathanw trm_eeprom_wait();
2554 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, send | NVR_CLOCK);
2555 1.4.2.4 nathanw trm_eeprom_wait();
2556 1.4.2.2 nathanw }
2557 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, NVR_SELECT);
2558 1.4.2.4 nathanw trm_eeprom_wait();
2559 1.4.2.2 nathanw /*
2560 1.4.2.2 nathanw * Disable chip select
2561 1.4.2.2 nathanw */
2562 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, 0);
2563 1.4.2.4 nathanw trm_eeprom_wait();
2564 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, NVR_SELECT);
2565 1.4.2.4 nathanw trm_eeprom_wait();
2566 1.4.2.2 nathanw /*
2567 1.4.2.2 nathanw * Wait for write ready
2568 1.4.2.2 nathanw */
2569 1.4.2.2 nathanw for (;;) {
2570 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM,
2571 1.4.2.2 nathanw NVR_SELECT | NVR_CLOCK);
2572 1.4.2.4 nathanw trm_eeprom_wait();
2573 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, NVR_SELECT);
2574 1.4.2.4 nathanw trm_eeprom_wait();
2575 1.4.2.2 nathanw if (bus_space_read_1(iot, ioh, TRM_GEN_NVRAM) & NVR_BITIN)
2576 1.4.2.2 nathanw break;
2577 1.4.2.2 nathanw }
2578 1.4.2.2 nathanw /*
2579 1.4.2.2 nathanw * Disable chip select
2580 1.4.2.2 nathanw */
2581 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, 0);
2582 1.4.2.2 nathanw }
2583 1.4.2.2 nathanw
2584 1.4.2.2 nathanw /*
2585 1.4.2.4 nathanw * read seeprom 128 bytes to struct eeprom
2586 1.4.2.2 nathanw */
2587 1.4.2.2 nathanw static void
2588 1.4.2.2 nathanw trm_eeprom_read_all(sc, eeprom)
2589 1.4.2.2 nathanw struct trm_softc *sc;
2590 1.4.2.2 nathanw struct trm_nvram *eeprom;
2591 1.4.2.2 nathanw {
2592 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2593 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2594 1.4.2.2 nathanw u_int8_t *buf = (u_int8_t *)eeprom;
2595 1.4.2.2 nathanw u_int8_t addr;
2596 1.4.2.2 nathanw
2597 1.4.2.2 nathanw /*
2598 1.4.2.2 nathanw * Enable SEEPROM
2599 1.4.2.2 nathanw */
2600 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_CONTROL,
2601 1.4.2.2 nathanw bus_space_read_1(iot, ioh, TRM_GEN_CONTROL) | EN_EEPROM);
2602 1.4.2.2 nathanw
2603 1.4.2.4 nathanw for (addr = 0; addr < 128; addr++)
2604 1.4.2.4 nathanw *buf++ = trm_eeprom_get_data(sc, addr);
2605 1.4.2.2 nathanw
2606 1.4.2.2 nathanw /*
2607 1.4.2.2 nathanw * Disable SEEPROM
2608 1.4.2.2 nathanw */
2609 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_CONTROL,
2610 1.4.2.2 nathanw bus_space_read_1(iot, ioh, TRM_GEN_CONTROL) & ~EN_EEPROM);
2611 1.4.2.2 nathanw }
2612 1.4.2.2 nathanw
2613 1.4.2.2 nathanw /*
2614 1.4.2.2 nathanw * read one byte from seeprom
2615 1.4.2.2 nathanw */
2616 1.4.2.2 nathanw static u_int8_t
2617 1.4.2.2 nathanw trm_eeprom_get_data(sc, addr)
2618 1.4.2.2 nathanw struct trm_softc *sc;
2619 1.4.2.2 nathanw u_int8_t addr;
2620 1.4.2.2 nathanw {
2621 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2622 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2623 1.4.2.2 nathanw int i;
2624 1.4.2.2 nathanw u_int8_t read, data = 0;
2625 1.4.2.2 nathanw
2626 1.4.2.2 nathanw /*
2627 1.4.2.2 nathanw * Send read command & address
2628 1.4.2.2 nathanw */
2629 1.4.2.2 nathanw trm_eeprom_write_cmd(sc, 0x06, addr);
2630 1.4.2.2 nathanw
2631 1.4.2.2 nathanw for (i = 0; i < 8; i++) { /* Read data */
2632 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM,
2633 1.4.2.2 nathanw NVR_SELECT | NVR_CLOCK);
2634 1.4.2.4 nathanw trm_eeprom_wait();
2635 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, NVR_SELECT);
2636 1.4.2.2 nathanw /*
2637 1.4.2.2 nathanw * Get data bit while falling edge
2638 1.4.2.2 nathanw */
2639 1.4.2.2 nathanw read = bus_space_read_1(iot, ioh, TRM_GEN_NVRAM);
2640 1.4.2.2 nathanw data <<= 1;
2641 1.4.2.2 nathanw if (read & NVR_BITIN)
2642 1.4.2.2 nathanw data |= 1;
2643 1.4.2.2 nathanw
2644 1.4.2.4 nathanw trm_eeprom_wait();
2645 1.4.2.2 nathanw }
2646 1.4.2.2 nathanw /*
2647 1.4.2.2 nathanw * Disable chip select
2648 1.4.2.2 nathanw */
2649 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, 0);
2650 1.4.2.2 nathanw return (data);
2651 1.4.2.2 nathanw }
2652 1.4.2.2 nathanw
2653 1.4.2.2 nathanw /*
2654 1.4.2.2 nathanw * write SB and Op Code into seeprom
2655 1.4.2.2 nathanw */
2656 1.4.2.2 nathanw static void
2657 1.4.2.2 nathanw trm_eeprom_write_cmd(sc, cmd, addr)
2658 1.4.2.2 nathanw struct trm_softc *sc;
2659 1.4.2.2 nathanw u_int8_t cmd;
2660 1.4.2.2 nathanw u_int8_t addr;
2661 1.4.2.2 nathanw {
2662 1.4.2.2 nathanw bus_space_tag_t iot = sc->sc_iot;
2663 1.4.2.2 nathanw bus_space_handle_t ioh = sc->sc_ioh;
2664 1.4.2.2 nathanw int i;
2665 1.4.2.2 nathanw u_int8_t send;
2666 1.4.2.2 nathanw
2667 1.4.2.2 nathanw /* Program SB+OP code */
2668 1.4.2.2 nathanw for (i = 0; i < 3; i++, cmd <<= 1) {
2669 1.4.2.2 nathanw send = NVR_SELECT;
2670 1.4.2.2 nathanw if (cmd & 0x04) /* Start from bit 2 */
2671 1.4.2.2 nathanw send |= NVR_BITOUT;
2672 1.4.2.2 nathanw
2673 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, send);
2674 1.4.2.4 nathanw trm_eeprom_wait();
2675 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, send | NVR_CLOCK);
2676 1.4.2.4 nathanw trm_eeprom_wait();
2677 1.4.2.2 nathanw }
2678 1.4.2.2 nathanw
2679 1.4.2.2 nathanw /* Program address */
2680 1.4.2.2 nathanw for (i = 0; i < 7; i++, addr <<= 1) {
2681 1.4.2.2 nathanw send = NVR_SELECT;
2682 1.4.2.2 nathanw if (addr & 0x40) /* Start from bit 6 */
2683 1.4.2.2 nathanw send |= NVR_BITOUT;
2684 1.4.2.2 nathanw
2685 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, send);
2686 1.4.2.4 nathanw trm_eeprom_wait();
2687 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, send | NVR_CLOCK);
2688 1.4.2.4 nathanw trm_eeprom_wait();
2689 1.4.2.2 nathanw }
2690 1.4.2.2 nathanw bus_space_write_1(iot, ioh, TRM_GEN_NVRAM, NVR_SELECT);
2691 1.4.2.4 nathanw trm_eeprom_wait();
2692 1.4.2.2 nathanw }
2693