Blame view

drivers/scsi/qlogicpti.c 40.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /* qlogicpti.c: Performance Technologies QlogicISP sbus card driver.
   *
5dc2536bb   David S. Miller   qlogicpti: Conver...
3
   * Copyright (C) 1996, 2006, 2008 David S. Miller (davem@davemloft.net)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   *
   * A lot of this driver was directly stolen from Erik H. Moe's PCI
   * Qlogic ISP driver.  Mucho kudos to him for this code.
   *
   * An even bigger kudos to John Grana at Performance Technologies
   * for providing me with the hardware to write this driver, you rule
   * John you really do.
   *
   * May, 2, 1997: Added support for QLGC,isp --jj
   */
  
  #include <linux/kernel.h>
  #include <linux/delay.h>
  #include <linux/types.h>
  #include <linux/string.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
  #include <linux/blkdev.h>
  #include <linux/proc_fs.h>
  #include <linux/stat.h>
  #include <linux/init.h>
  #include <linux/spinlock.h>
  #include <linux/interrupt.h>
  #include <linux/module.h>
60c904ae5   Marcelo Feitoza Parisi   [PATCH] drivers/s...
27
  #include <linux/jiffies.h>
738f2b7b8   David S. Miller   sparc: Convert al...
28
  #include <linux/dma-mapping.h>
5dc2536bb   David S. Miller   qlogicpti: Conver...
29
30
  #include <linux/of.h>
  #include <linux/of_device.h>
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
31
  #include <linux/firmware.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
  
  #include <asm/byteorder.h>
  
  #include "qlogicpti.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
41
42
43
44
45
46
47
  #include <asm/dma.h>
  #include <asm/system.h>
  #include <asm/ptrace.h>
  #include <asm/pgtable.h>
  #include <asm/oplib.h>
  #include <asm/io.h>
  #include <asm/irq.h>
  
  #include <scsi/scsi.h>
  #include <scsi/scsi_cmnd.h>
  #include <scsi/scsi_device.h>
  #include <scsi/scsi_eh.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
  #include <scsi/scsi_tcq.h>
  #include <scsi/scsi_host.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
  #define MAX_TARGETS	16
  #define MAX_LUNS	8	/* 32 for 1.31 F/W */
  
  #define DEFAULT_LOOP_COUNT	10000
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  static struct qlogicpti *qptichain = NULL;
  static DEFINE_SPINLOCK(qptichain_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  
  #define PACKB(a, b)			(((a)<<4)|(b))
  
  static const u_char mbox_param[] = {
  	PACKB(1, 1),	/* MBOX_NO_OP */
  	PACKB(5, 5),	/* MBOX_LOAD_RAM */
  	PACKB(2, 0),	/* MBOX_EXEC_FIRMWARE */
  	PACKB(5, 5),	/* MBOX_DUMP_RAM */
  	PACKB(3, 3),	/* MBOX_WRITE_RAM_WORD */
  	PACKB(2, 3),	/* MBOX_READ_RAM_WORD */
  	PACKB(6, 6),	/* MBOX_MAILBOX_REG_TEST */
  	PACKB(2, 3),	/* MBOX_VERIFY_CHECKSUM	*/
  	PACKB(1, 3),	/* MBOX_ABOUT_FIRMWARE */
  	PACKB(0, 0),	/* 0x0009 */
  	PACKB(0, 0),	/* 0x000a */
  	PACKB(0, 0),	/* 0x000b */
  	PACKB(0, 0),	/* 0x000c */
  	PACKB(0, 0),	/* 0x000d */
  	PACKB(1, 2),	/* MBOX_CHECK_FIRMWARE */
  	PACKB(0, 0),	/* 0x000f */
  	PACKB(5, 5),	/* MBOX_INIT_REQ_QUEUE */
  	PACKB(6, 6),	/* MBOX_INIT_RES_QUEUE */
  	PACKB(4, 4),	/* MBOX_EXECUTE_IOCB */
  	PACKB(2, 2),	/* MBOX_WAKE_UP	*/
  	PACKB(1, 6),	/* MBOX_STOP_FIRMWARE */
  	PACKB(4, 4),	/* MBOX_ABORT */
  	PACKB(2, 2),	/* MBOX_ABORT_DEVICE */
  	PACKB(3, 3),	/* MBOX_ABORT_TARGET */
  	PACKB(2, 2),	/* MBOX_BUS_RESET */
  	PACKB(2, 3),	/* MBOX_STOP_QUEUE */
  	PACKB(2, 3),	/* MBOX_START_QUEUE */
  	PACKB(2, 3),	/* MBOX_SINGLE_STEP_QUEUE */
  	PACKB(2, 3),	/* MBOX_ABORT_QUEUE */
  	PACKB(2, 4),	/* MBOX_GET_DEV_QUEUE_STATUS */
  	PACKB(0, 0),	/* 0x001e */
  	PACKB(1, 3),	/* MBOX_GET_FIRMWARE_STATUS */
  	PACKB(1, 2),	/* MBOX_GET_INIT_SCSI_ID */
  	PACKB(1, 2),	/* MBOX_GET_SELECT_TIMEOUT */
  	PACKB(1, 3),	/* MBOX_GET_RETRY_COUNT	*/
  	PACKB(1, 2),	/* MBOX_GET_TAG_AGE_LIMIT */
  	PACKB(1, 2),	/* MBOX_GET_CLOCK_RATE */
  	PACKB(1, 2),	/* MBOX_GET_ACT_NEG_STATE */
  	PACKB(1, 2),	/* MBOX_GET_ASYNC_DATA_SETUP_TIME */
  	PACKB(1, 3),	/* MBOX_GET_SBUS_PARAMS */
  	PACKB(2, 4),	/* MBOX_GET_TARGET_PARAMS */
  	PACKB(2, 4),	/* MBOX_GET_DEV_QUEUE_PARAMS */
  	PACKB(0, 0),	/* 0x002a */
  	PACKB(0, 0),	/* 0x002b */
  	PACKB(0, 0),	/* 0x002c */
  	PACKB(0, 0),	/* 0x002d */
  	PACKB(0, 0),	/* 0x002e */
  	PACKB(0, 0),	/* 0x002f */
  	PACKB(2, 2),	/* MBOX_SET_INIT_SCSI_ID */
  	PACKB(2, 2),	/* MBOX_SET_SELECT_TIMEOUT */
  	PACKB(3, 3),	/* MBOX_SET_RETRY_COUNT	*/
  	PACKB(2, 2),	/* MBOX_SET_TAG_AGE_LIMIT */
  	PACKB(2, 2),	/* MBOX_SET_CLOCK_RATE */
  	PACKB(2, 2),	/* MBOX_SET_ACTIVE_NEG_STATE */
  	PACKB(2, 2),	/* MBOX_SET_ASYNC_DATA_SETUP_TIME */
  	PACKB(3, 3),	/* MBOX_SET_SBUS_CONTROL_PARAMS */
  	PACKB(4, 4),	/* MBOX_SET_TARGET_PARAMS */
  	PACKB(4, 4),	/* MBOX_SET_DEV_QUEUE_PARAMS */
  	PACKB(0, 0),	/* 0x003a */
  	PACKB(0, 0),	/* 0x003b */
  	PACKB(0, 0),	/* 0x003c */
  	PACKB(0, 0),	/* 0x003d */
  	PACKB(0, 0),	/* 0x003e */
  	PACKB(0, 0),	/* 0x003f */
  	PACKB(0, 0),	/* 0x0040 */
  	PACKB(0, 0),	/* 0x0041 */
  	PACKB(0, 0)	/* 0x0042 */
  };
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
128
  #define MAX_MBOX_COMMAND	ARRAY_SIZE(mbox_param)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  
  /* queue length's _must_ be power of two: */
  #define QUEUE_DEPTH(in, out, ql)	((in - out) & (ql))
  #define REQ_QUEUE_DEPTH(in, out)	QUEUE_DEPTH(in, out, 		     \
  						    QLOGICPTI_REQ_QUEUE_LEN)
  #define RES_QUEUE_DEPTH(in, out)	QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
  
  static inline void qlogicpti_enable_irqs(struct qlogicpti *qpti)
  {
  	sbus_writew(SBUS_CTRL_ERIRQ | SBUS_CTRL_GENAB,
  		    qpti->qregs + SBUS_CTRL);
  }
  
  static inline void qlogicpti_disable_irqs(struct qlogicpti *qpti)
  {
  	sbus_writew(0, qpti->qregs + SBUS_CTRL);
  }
  
  static inline void set_sbus_cfg1(struct qlogicpti *qpti)
  {
  	u16 val;
  	u8 bursts = qpti->bursts;
  
  #if 0	/* It appears that at least PTI cards do not support
  	 * 64-byte bursts and that setting the B64 bit actually
  	 * is a nop and the chip ends up using the smallest burst
  	 * size. -DaveM
  	 */
63237eeb5   David S. Miller   sparc: Move SBUS ...
157
  	if (sbus_can_burst64() && (bursts & DMA_BURST64)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B64);
  	} else
  #endif
  	if (bursts & DMA_BURST32) {
  		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B32);
  	} else if (bursts & DMA_BURST16) {
  		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B16);
  	} else if (bursts & DMA_BURST8) {
  		val = (SBUS_CFG1_BENAB | SBUS_CFG1_B8);
  	} else {
  		val = 0; /* No sbus bursts for you... */
  	}
  	sbus_writew(val, qpti->qregs + SBUS_CFG1);
  }
  
  static int qlogicpti_mbox_command(struct qlogicpti *qpti, u_short param[], int force)
  {
  	int loop_count;
  	u16 tmp;
  
  	if (mbox_param[param[0]] == 0)
  		return 1;
  
  	/* Set SBUS semaphore. */
  	tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
  	tmp |= SBUS_SEMAPHORE_LCK;
  	sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
  
  	/* Wait for host IRQ bit to clear. */
  	loop_count = DEFAULT_LOOP_COUNT;
  	while (--loop_count && (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_HIRQ)) {
  		barrier();
  		cpu_relax();
  	}
  	if (!loop_count)
585a8a59e   Mark   [SCSI] qlogicpti:...
193
194
195
  		printk(KERN_EMERG "qlogicpti%d: mbox_command loop timeout #1
  ",
  		       qpti->qpti_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  
  	/* Write mailbox command registers. */
  	switch (mbox_param[param[0]] >> 4) {
  	case 6: sbus_writew(param[5], qpti->qregs + MBOX5);
  	case 5: sbus_writew(param[4], qpti->qregs + MBOX4);
  	case 4: sbus_writew(param[3], qpti->qregs + MBOX3);
  	case 3: sbus_writew(param[2], qpti->qregs + MBOX2);
  	case 2: sbus_writew(param[1], qpti->qregs + MBOX1);
  	case 1: sbus_writew(param[0], qpti->qregs + MBOX0);
  	}
  
  	/* Clear RISC interrupt. */
  	tmp = sbus_readw(qpti->qregs + HCCTRL);
  	tmp |= HCCTRL_CRIRQ;
  	sbus_writew(tmp, qpti->qregs + HCCTRL);
  
  	/* Clear SBUS semaphore. */
  	sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  
  	/* Set HOST interrupt. */
  	tmp = sbus_readw(qpti->qregs + HCCTRL);
  	tmp |= HCCTRL_SHIRQ;
  	sbus_writew(tmp, qpti->qregs + HCCTRL);
  
  	/* Wait for HOST interrupt clears. */
  	loop_count = DEFAULT_LOOP_COUNT;
  	while (--loop_count &&
  	       (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_CRIRQ))
  		udelay(20);
  	if (!loop_count)
585a8a59e   Mark   [SCSI] qlogicpti:...
226
227
228
  		printk(KERN_EMERG "qlogicpti%d: mbox_command[%04x] loop timeout #2
  ",
  		       qpti->qpti_id, param[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
234
235
236
237
238
239
240
  
  	/* Wait for SBUS semaphore to get set. */
  	loop_count = DEFAULT_LOOP_COUNT;
  	while (--loop_count &&
  	       !(sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK)) {
  		udelay(20);
  
  		/* Workaround for some buggy chips. */
  		if (sbus_readw(qpti->qregs + MBOX0) & 0x4000)
  			break;
  	}
  	if (!loop_count)
585a8a59e   Mark   [SCSI] qlogicpti:...
241
242
243
  		printk(KERN_EMERG "qlogicpti%d: mbox_command[%04x] loop timeout #3
  ",
  		       qpti->qpti_id, param[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
249
  
  	/* Wait for MBOX busy condition to go away. */
  	loop_count = DEFAULT_LOOP_COUNT;
  	while (--loop_count && (sbus_readw(qpti->qregs + MBOX0) == 0x04))
  		udelay(20);
  	if (!loop_count)
585a8a59e   Mark   [SCSI] qlogicpti:...
250
251
252
  		printk(KERN_EMERG "qlogicpti%d: mbox_command[%04x] loop timeout #4
  ",
  		       qpti->qpti_id, param[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  
  	/* Read back output parameters. */
  	switch (mbox_param[param[0]] & 0xf) {
  	case 6: param[5] = sbus_readw(qpti->qregs + MBOX5);
  	case 5: param[4] = sbus_readw(qpti->qregs + MBOX4);
  	case 4: param[3] = sbus_readw(qpti->qregs + MBOX3);
  	case 3: param[2] = sbus_readw(qpti->qregs + MBOX2);
  	case 2: param[1] = sbus_readw(qpti->qregs + MBOX1);
  	case 1: param[0] = sbus_readw(qpti->qregs + MBOX0);
  	}
  
  	/* Clear RISC interrupt. */
  	tmp = sbus_readw(qpti->qregs + HCCTRL);
  	tmp |= HCCTRL_CRIRQ;
  	sbus_writew(tmp, qpti->qregs + HCCTRL);
  
  	/* Release SBUS semaphore. */
  	tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
  	tmp &= ~(SBUS_SEMAPHORE_LCK);
  	sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
  
  	/* We're done. */
  	return 0;
  }
  
  static inline void qlogicpti_set_hostdev_defaults(struct qlogicpti *qpti)
  {
  	int i;
  
  	qpti->host_param.initiator_scsi_id = qpti->scsi_id;
  	qpti->host_param.bus_reset_delay = 3;
  	qpti->host_param.retry_count = 0;
  	qpti->host_param.retry_delay = 5;
  	qpti->host_param.async_data_setup_time = 3;
  	qpti->host_param.req_ack_active_negation = 1;
  	qpti->host_param.data_line_active_negation = 1;
  	qpti->host_param.data_dma_burst_enable = 1;
  	qpti->host_param.command_dma_burst_enable = 1;
  	qpti->host_param.tag_aging = 8;
  	qpti->host_param.selection_timeout = 250;
  	qpti->host_param.max_queue_depth = 256;
  
  	for(i = 0; i < MAX_TARGETS; i++) {
  		/*
  		 * disconnect, parity, arq, reneg on reset, and, oddly enough
  		 * tags...the midlayer's notion of tagged support has to match
  		 * our device settings, and since we base whether we enable a
  		 * tag on a  per-cmnd basis upon what the midlayer sez, we
  		 * actually enable the capability here.
  		 */
  		qpti->dev_param[i].device_flags = 0xcd;
  		qpti->dev_param[i].execution_throttle = 16;
  		if (qpti->ultra) {
  			qpti->dev_param[i].synchronous_period = 12;
  			qpti->dev_param[i].synchronous_offset = 8;
  		} else {
  			qpti->dev_param[i].synchronous_period = 25;
  			qpti->dev_param[i].synchronous_offset = 12;
  		}
  		qpti->dev_param[i].device_enable = 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  }
  
  static int qlogicpti_reset_hardware(struct Scsi_Host *host)
  {
  	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  	u_short param[6];
  	unsigned short risc_code_addr;
  	int loop_count, i;
  	unsigned long flags;
  
  	risc_code_addr = 0x1000;	/* all load addresses are at 0x1000 */
  
  	spin_lock_irqsave(host->host_lock, flags);
  
  	sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  
  	/* Only reset the scsi bus if it is not free. */
  	if (sbus_readw(qpti->qregs + CPU_PCTRL) & CPU_PCTRL_BSY) {
  		sbus_writew(CPU_ORIDE_RMOD, qpti->qregs + CPU_ORIDE);
  		sbus_writew(CPU_CMD_BRESET, qpti->qregs + CPU_CMD);
  		udelay(400);
  	}
  
  	sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  
  	loop_count = DEFAULT_LOOP_COUNT;
  	while (--loop_count && ((sbus_readw(qpti->qregs + MBOX0) & 0xff) == 0x04))
  		udelay(20);
  	if (!loop_count)
585a8a59e   Mark   [SCSI] qlogicpti:...
345
346
347
  		printk(KERN_EMERG "qlogicpti%d: reset_hardware loop timeout
  ",
  		       qpti->qpti_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  
  	sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  	set_sbus_cfg1(qpti);
  	qlogicpti_enable_irqs(qpti);
  
  	if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  		qpti->ultra = 1;
  		sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  			    qpti->qregs + RISC_MTREG);
  	} else {
  		qpti->ultra = 0;
  		sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  			    qpti->qregs + RISC_MTREG);
  	}
  
  	/* reset adapter and per-device default values. */
  	/* do it after finding out whether we're ultra mode capable */
  	qlogicpti_set_hostdev_defaults(qpti);
  
  	/* Release the RISC processor. */
  	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  
  	/* Get RISC to start executing the firmware code. */
  	param[0] = MBOX_EXEC_FIRMWARE;
  	param[1] = risc_code_addr;
  	if (qlogicpti_mbox_command(qpti, param, 1)) {
  		printk(KERN_EMERG "qlogicpti%d: Cannot execute ISP firmware.
  ",
  		       qpti->qpti_id);
  		spin_unlock_irqrestore(host->host_lock, flags);
  		return 1;
  	}
  
  	/* Set initiator scsi ID. */
  	param[0] = MBOX_SET_INIT_SCSI_ID;
  	param[1] = qpti->host_param.initiator_scsi_id;
  	if (qlogicpti_mbox_command(qpti, param, 1) ||
  	   (param[0] != MBOX_COMMAND_COMPLETE)) {
  		printk(KERN_EMERG "qlogicpti%d: Cannot set initiator SCSI ID.
  ",
  		       qpti->qpti_id);
  		spin_unlock_irqrestore(host->host_lock, flags);
  		return 1;
  	}
  
  	/* Initialize state of the queues, both hw and sw. */
  	qpti->req_in_ptr = qpti->res_out_ptr = 0;
  
  	param[0] = MBOX_INIT_RES_QUEUE;
  	param[1] = RES_QUEUE_LEN + 1;
  	param[2] = (u_short) (qpti->res_dvma >> 16);
  	param[3] = (u_short) (qpti->res_dvma & 0xffff);
  	param[4] = param[5] = 0;
  	if (qlogicpti_mbox_command(qpti, param, 1)) {
  		printk(KERN_EMERG "qlogicpti%d: Cannot init response queue.
  ",
  		       qpti->qpti_id);
  		spin_unlock_irqrestore(host->host_lock, flags);
  		return 1;
  	}
  
  	param[0] = MBOX_INIT_REQ_QUEUE;
  	param[1] = QLOGICPTI_REQ_QUEUE_LEN + 1;
  	param[2] = (u_short) (qpti->req_dvma >> 16);
  	param[3] = (u_short) (qpti->req_dvma & 0xffff);
  	param[4] = param[5] = 0;
  	if (qlogicpti_mbox_command(qpti, param, 1)) {
  		printk(KERN_EMERG "qlogicpti%d: Cannot init request queue.
  ",
  		       qpti->qpti_id);
  		spin_unlock_irqrestore(host->host_lock, flags);
  		return 1;
  	}
  
  	param[0] = MBOX_SET_RETRY_COUNT;
  	param[1] = qpti->host_param.retry_count;
  	param[2] = qpti->host_param.retry_delay;
  	qlogicpti_mbox_command(qpti, param, 0);
  
  	param[0] = MBOX_SET_TAG_AGE_LIMIT;
  	param[1] = qpti->host_param.tag_aging;
  	qlogicpti_mbox_command(qpti, param, 0);
  
  	for (i = 0; i < MAX_TARGETS; i++) {
  		param[0] = MBOX_GET_DEV_QUEUE_PARAMS;
  		param[1] = (i << 8);
  		qlogicpti_mbox_command(qpti, param, 0);
  	}
  
  	param[0] = MBOX_GET_FIRMWARE_STATUS;
  	qlogicpti_mbox_command(qpti, param, 0);
  
  	param[0] = MBOX_SET_SELECT_TIMEOUT;
  	param[1] = qpti->host_param.selection_timeout;
  	qlogicpti_mbox_command(qpti, param, 0);
  
  	for (i = 0; i < MAX_TARGETS; i++) {
  		param[0] = MBOX_SET_TARGET_PARAMS;
  		param[1] = (i << 8);
  		param[2] = (qpti->dev_param[i].device_flags << 8);
  		/*
  		 * Since we're now loading 1.31 f/w, force narrow/async.
  		 */
  		param[2] |= 0xc0;
  		param[3] = 0;	/* no offset, we do not have sync mode yet */
  		qlogicpti_mbox_command(qpti, param, 0);
  	}
  
  	/*
  	 * Always (sigh) do an initial bus reset (kicks f/w).
  	 */
  	param[0] = MBOX_BUS_RESET;
  	param[1] = qpti->host_param.bus_reset_delay;
  	qlogicpti_mbox_command(qpti, param, 0);
  	qpti->send_marker = 1;
  
  	spin_unlock_irqrestore(host->host_lock, flags);
  	return 0;
  }
  
  #define PTI_RESET_LIMIT 400
b48194bf0   Ben Collins   [SPARC]: Fix some...
469
  static int __devinit qlogicpti_load_firmware(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
  {
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
471
472
473
  	const struct firmware *fw;
  	const char fwname[] = "qlogic/isp1000.bin";
  	const __le16 *fw_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
  	struct Scsi_Host *host = qpti->qhost;
  	unsigned short csum = 0;
  	unsigned short param[6];
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
477
478
  	unsigned short risc_code_addr, risc_code_length;
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
  	unsigned long flags;
  	int i, timeout;
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  	err = request_firmware(&fw, fwname, &qpti->op->dev);
  	if (err) {
  		printk(KERN_ERR "Failed to load image \"%s\" err %d
  ",
  		       fwname, err);
  		return err;
  	}
  	if (fw->size % 2) {
  		printk(KERN_ERR "Bogus length %zu in image \"%s\"
  ",
  		       fw->size, fwname);
  		err = -EINVAL;
  		goto outfirm;
  	}
  	fw_data = (const __le16 *)&fw->data[0];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
  	risc_code_addr = 0x1000;	/* all f/w modules load at 0x1000 */
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
497
  	risc_code_length = fw->size / 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498
499
500
501
502
503
504
  
  	spin_lock_irqsave(host->host_lock, flags);
  
  	/* Verify the checksum twice, one before loading it, and once
  	 * afterwards via the mailbox commands.
  	 */
  	for (i = 0; i < risc_code_length; i++)
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
505
  		csum += __le16_to_cpu(fw_data[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
  	if (csum) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
  		printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
  		       qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
509
510
  		err = 1;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
512
513
514
515
516
517
518
  	}		
  	sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  	sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  	timeout = PTI_RESET_LIMIT;
  	while (--timeout && (sbus_readw(qpti->qregs + SBUS_CTRL) & SBUS_CTRL_RESET))
  		udelay(20);
  	if (!timeout) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
  		printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
520
521
  		err = 1;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  	}
  
  	sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  	mdelay(1);
  
  	sbus_writew((SBUS_CTRL_GENAB | SBUS_CTRL_ERIRQ), qpti->qregs + SBUS_CTRL);
  	set_sbus_cfg1(qpti);
  	sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  
  	if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  		qpti->ultra = 1;
  		sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  			    qpti->qregs + RISC_MTREG);
  	} else {
  		qpti->ultra = 0;
  		sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  			    qpti->qregs + RISC_MTREG);
  	}
  
  	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  
  	/* Pin lines are only stable while RISC is paused. */
  	sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  	if (sbus_readw(qpti->qregs + CPU_PDIFF) & CPU_PDIFF_MODE)
  		qpti->differential = 1;
  	else
  		qpti->differential = 0;
  	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  
  	/* This shouldn't be necessary- we've reset things so we should be
  	   running from the ROM now.. */
  
  	param[0] = MBOX_STOP_FIRMWARE;
  	param[1] = param[2] = param[3] = param[4] = param[5] = 0;
  	if (qlogicpti_mbox_command(qpti, param, 1)) {
  		printk(KERN_EMERG "qlogicpti%d: Cannot stop firmware for reload.
  ",
  		       qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
560
561
  		err = 1;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
563
564
565
566
567
  	}		
  
  	/* Load it up.. */
  	for (i = 0; i < risc_code_length; i++) {
  		param[0] = MBOX_WRITE_RAM_WORD;
  		param[1] = risc_code_addr + i;
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
568
  		param[2] = __le16_to_cpu(fw_data[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
572
573
  		if (qlogicpti_mbox_command(qpti, param, 1) ||
  		    param[0] != MBOX_COMMAND_COMPLETE) {
  			printk("qlogicpti%d: Firmware dload failed, I'm bolixed!
  ",
  			       qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
574
575
  			err = 1;
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  		}
  	}
  
  	/* Reset the ISP again. */
  	sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  	mdelay(1);
  
  	qlogicpti_enable_irqs(qpti);
  	sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  	sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  
  	/* Ask ISP to verify the checksum of the new code. */
  	param[0] = MBOX_VERIFY_CHECKSUM;
  	param[1] = risc_code_addr;
  	if (qlogicpti_mbox_command(qpti, param, 1) ||
  	    (param[0] != MBOX_COMMAND_COMPLETE)) {
  		printk(KERN_EMERG "qlogicpti%d: New firmware csum failure!
  ",
  		       qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
595
596
  		err = 1;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
599
600
601
602
603
604
605
606
607
608
609
  	}
  
  	/* Start using newly downloaded firmware. */
  	param[0] = MBOX_EXEC_FIRMWARE;
  	param[1] = risc_code_addr;
  	qlogicpti_mbox_command(qpti, param, 1);
  
  	param[0] = MBOX_ABOUT_FIRMWARE;
  	if (qlogicpti_mbox_command(qpti, param, 1) ||
  	    (param[0] != MBOX_COMMAND_COMPLETE)) {
  		printk(KERN_EMERG "qlogicpti%d: AboutFirmware cmd fails.
  ",
  		       qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
610
611
  		err = 1;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  	}
  
  	/* Snag the major and minor revisions from the result. */
  	qpti->fware_majrev = param[1];
  	qpti->fware_minrev = param[2];
  	qpti->fware_micrev = param[3];
  
  	/* Set the clock rate */
  	param[0] = MBOX_SET_CLOCK_RATE;
  	param[1] = qpti->clock;
  	if (qlogicpti_mbox_command(qpti, param, 1) ||
  	    (param[0] != MBOX_COMMAND_COMPLETE)) {
  		printk(KERN_EMERG "qlogicpti%d: could not set clock rate.
  ",
  		       qpti->qpti_id);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
627
628
  		err = 1;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  	}
  
  	if (qpti->is_pti != 0) {
  		/* Load scsi initiator ID and interrupt level into sbus static ram. */
  		param[0] = MBOX_WRITE_RAM_WORD;
  		param[1] = 0xff80;
  		param[2] = (unsigned short) qpti->scsi_id;
  		qlogicpti_mbox_command(qpti, param, 1);
  
  		param[0] = MBOX_WRITE_RAM_WORD;
  		param[1] = 0xff00;
  		param[2] = (unsigned short) 3;
  		qlogicpti_mbox_command(qpti, param, 1);
  	}
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
643
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
  	spin_unlock_irqrestore(host->host_lock, flags);
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
645
646
647
  outfirm:
  	release_firmware(fw);
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  }
  
  static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
  {
  	int curstat = sbus_readb(qpti->sreg);
  
  	curstat &= 0xf0;
  	if (!(curstat & SREG_FUSE) && (qpti->swsreg & SREG_FUSE))
  		printk("qlogicpti%d: Fuse returned to normal state.
  ", qpti->qpti_id);
  	if (!(curstat & SREG_TPOWER) && (qpti->swsreg & SREG_TPOWER))
  		printk("qlogicpti%d: termpwr back to normal state.
  ", qpti->qpti_id);
  	if (curstat != qpti->swsreg) {
  		int error = 0;
  		if (curstat & SREG_FUSE) {
  			error++;
  			printk("qlogicpti%d: Fuse is open!
  ", qpti->qpti_id);
  		}
  		if (curstat & SREG_TPOWER) {
  			error++;
  			printk("qlogicpti%d: termpwr failure
  ", qpti->qpti_id);
  		}
  		if (qpti->differential &&
  		    (curstat & SREG_DSENSE) != SREG_DSENSE) {
  			error++;
  			printk("qlogicpti%d: You have a single ended device on a "
  			       "differential bus!  Please fix!
  ", qpti->qpti_id);
  		}
  		qpti->swsreg = curstat;
  		return error;
  	}
  	return 0;
  }
7d12e780e   David Howells   IRQ: Maintain reg...
685
  static irqreturn_t qpti_intr(int irq, void *dev_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686

cfb37ae1e   Adrian Bunk   [SCSI] qlogicpt: ...
687
  static void __devinit qpti_chain_add(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
694
695
696
697
698
699
700
701
  {
  	spin_lock_irq(&qptichain_lock);
  	if (qptichain != NULL) {
  		struct qlogicpti *qlink = qptichain;
  
  		while(qlink->next)
  			qlink = qlink->next;
  		qlink->next = qpti;
  	} else {
  		qptichain = qpti;
  	}
  	qpti->next = NULL;
  	spin_unlock_irq(&qptichain_lock);
  }
cfb37ae1e   Adrian Bunk   [SCSI] qlogicpt: ...
702
  static void __devexit qpti_chain_del(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
705
706
707
708
709
710
711
712
713
714
715
  {
  	spin_lock_irq(&qptichain_lock);
  	if (qptichain == qpti) {
  		qptichain = qpti->next;
  	} else {
  		struct qlogicpti *qlink = qptichain;
  		while(qlink->next != qpti)
  			qlink = qlink->next;
  		qlink->next = qpti->next;
  	}
  	qpti->next = NULL;
  	spin_unlock_irq(&qptichain_lock);
  }
cfb37ae1e   Adrian Bunk   [SCSI] qlogicpt: ...
716
  static int __devinit qpti_map_regs(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
  {
2dc115813   Grant Likely   of/device: Replac...
718
  	struct platform_device *op = qpti->op;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719

5dc2536bb   David S. Miller   qlogicpti: Conver...
720
721
722
  	qpti->qregs = of_ioremap(&op->resource[0], 0,
  				 resource_size(&op->resource[0]),
  				 "PTI Qlogic/ISP");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
724
725
726
727
728
  	if (!qpti->qregs) {
  		printk("PTI: Qlogic/ISP registers are unmappable
  ");
  		return -1;
  	}
  	if (qpti->is_pti) {
5dc2536bb   David S. Miller   qlogicpti: Conver...
729
730
731
  		qpti->sreg = of_ioremap(&op->resource[0], (16 * 4096),
  					sizeof(unsigned char),
  					"PTI Qlogic/ISP statreg");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
734
735
736
737
738
739
  		if (!qpti->sreg) {
  			printk("PTI: Qlogic/ISP status register is unmappable
  ");
  			return -1;
  		}
  	}
  	return 0;
  }
cfb37ae1e   Adrian Bunk   [SCSI] qlogicpt: ...
740
  static int __devinit qpti_register_irq(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
  {
2dc115813   Grant Likely   of/device: Replac...
742
  	struct platform_device *op = qpti->op;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743

1636f8ac2   Grant Likely   sparc/of: Move of...
744
  	qpti->qhost->irq = qpti->irq = op->archdata.irqs[0];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
748
749
750
751
752
  
  	/* We used to try various overly-clever things to
  	 * reduce the interrupt processing overhead on
  	 * sun4c/sun4m when multiple PTI's shared the
  	 * same IRQ.  It was too complex and messy to
  	 * sanely maintain.
  	 */
  	if (request_irq(qpti->irq, qpti_intr,
77d392630   Meelis Roos   qlogicpti: Remove...
753
  			IRQF_SHARED, "QlogicPTI", qpti))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
  		goto fail;
585a8a59e   Mark   [SCSI] qlogicpti:...
755
  	printk("qlogicpti%d: IRQ %d ", qpti->qpti_id, qpti->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756
757
758
759
  
  	return 0;
  
  fail:
585a8a59e   Mark   [SCSI] qlogicpti:...
760
761
  	printk("qlogicpti%d: Cannot acquire irq line
  ", qpti->qpti_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
  	return -1;
  }
cfb37ae1e   Adrian Bunk   [SCSI] qlogicpt: ...
764
  static void __devinit qpti_get_scsi_id(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
  {
2dc115813   Grant Likely   of/device: Replac...
766
  	struct platform_device *op = qpti->op;
5dc2536bb   David S. Miller   qlogicpti: Conver...
767
  	struct device_node *dp;
61c7a080a   Grant Likely   of: Always use 's...
768
  	dp = op->dev.of_node;
5dc2536bb   David S. Miller   qlogicpti: Conver...
769
770
  
  	qpti->scsi_id = of_getintprop_default(dp, "initiator-id", -1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
  	if (qpti->scsi_id == -1)
5dc2536bb   David S. Miller   qlogicpti: Conver...
772
773
  		qpti->scsi_id = of_getintprop_default(dp, "scsi-initiator-id",
  						      -1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
  	if (qpti->scsi_id == -1)
  		qpti->scsi_id =
5dc2536bb   David S. Miller   qlogicpti: Conver...
776
777
  			of_getintprop_default(dp->parent,
  					      "scsi-initiator-id", 7);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
778
779
780
781
782
783
784
785
  	qpti->qhost->this_id = qpti->scsi_id;
  	qpti->qhost->max_sectors = 64;
  
  	printk("SCSI ID %d ", qpti->scsi_id);
  }
  
  static void qpti_get_bursts(struct qlogicpti *qpti)
  {
2dc115813   Grant Likely   of/device: Replac...
786
  	struct platform_device *op = qpti->op;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
  	u8 bursts, bmask;
61c7a080a   Grant Likely   of: Always use 's...
788
789
  	bursts = of_getintprop_default(op->dev.of_node, "burst-sizes", 0xff);
  	bmask = of_getintprop_default(op->dev.of_node->parent, "burst-sizes", 0xff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
  	if (bmask != 0xff)
  		bursts &= bmask;
  	if (bursts == 0xff ||
  	    (bursts & DMA_BURST16) == 0 ||
  	    (bursts & DMA_BURST32) == 0)
  		bursts = (DMA_BURST32 - 1);
  
  	qpti->bursts = bursts;
  }
  
  static void qpti_get_clock(struct qlogicpti *qpti)
  {
  	unsigned int cfreq;
  
  	/* Check for what the clock input to this card is.
  	 * Default to 40Mhz.
  	 */
  	cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
  	qpti->clock = (cfreq + 500000)/1000000;
  	if (qpti->clock == 0) /* bullshit */
  		qpti->clock = 40;
  }
  
  /* The request and response queues must each be aligned
   * on a page boundary.
   */
cfb37ae1e   Adrian Bunk   [SCSI] qlogicpt: ...
816
  static int __devinit qpti_map_queues(struct qlogicpti *qpti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
817
  {
2dc115813   Grant Likely   of/device: Replac...
818
  	struct platform_device *op = qpti->op;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
820
  
  #define QSIZE(entries)	(((entries) + 1) * QUEUE_ENTRY_LEN)
5dc2536bb   David S. Miller   qlogicpti: Conver...
821
  	qpti->res_cpu = dma_alloc_coherent(&op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
822
823
  					   QSIZE(RES_QUEUE_LEN),
  					   &qpti->res_dvma, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
825
826
827
828
829
  	if (qpti->res_cpu == NULL ||
  	    qpti->res_dvma == 0) {
  		printk("QPTI: Cannot map response queue.
  ");
  		return -1;
  	}
5dc2536bb   David S. Miller   qlogicpti: Conver...
830
  	qpti->req_cpu = dma_alloc_coherent(&op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
831
832
  					   QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  					   &qpti->req_dvma, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
834
  	if (qpti->req_cpu == NULL ||
  	    qpti->req_dvma == 0) {
5dc2536bb   David S. Miller   qlogicpti: Conver...
835
  		dma_free_coherent(&op->dev, QSIZE(RES_QUEUE_LEN),
738f2b7b8   David S. Miller   sparc: Convert al...
836
  				  qpti->res_cpu, qpti->res_dvma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
838
839
840
841
842
843
844
  		printk("QPTI: Cannot map request queue.
  ");
  		return -1;
  	}
  	memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
  	memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
847
848
  const char *qlogicpti_info(struct Scsi_Host *host)
  {
  	static char buf[80];
  	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
c6387a48c   David S. Miller   [SPARC]: Kill __i...
849
850
  	sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %d regs at %p",
  		qpti->qhost->irq, qpti->qregs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
  	return buf;
  }
  
  /* I am a certified frobtronicist. */
  static inline void marker_frob(struct Command_Entry *cmd)
  {
  	struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
  
  	memset(marker, 0, sizeof(struct Marker_Entry));
  	marker->hdr.entry_cnt = 1;
  	marker->hdr.entry_type = ENTRY_MARKER;
  	marker->modifier = SYNC_ALL;
  	marker->rsvd = 0;
  }
  
  static inline void cmd_frob(struct Command_Entry *cmd, struct scsi_cmnd *Cmnd,
  			    struct qlogicpti *qpti)
  {
  	memset(cmd, 0, sizeof(struct Command_Entry));
  	cmd->hdr.entry_cnt = 1;
  	cmd->hdr.entry_type = ENTRY_COMMAND;
  	cmd->target_id = Cmnd->device->id;
  	cmd->target_lun = Cmnd->device->lun;
  	cmd->cdb_length = Cmnd->cmd_len;
  	cmd->control_flags = 0;
  	if (Cmnd->device->tagged_supported) {
  		if (qpti->cmd_count[Cmnd->device->id] == 0)
  			qpti->tag_ages[Cmnd->device->id] = jiffies;
60c904ae5   Marcelo Feitoza Parisi   [PATCH] drivers/s...
879
  		if (time_after(jiffies, qpti->tag_ages[Cmnd->device->id] + (5*HZ))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
880
881
882
883
884
885
886
887
888
889
890
  			cmd->control_flags = CFLAG_ORDERED_TAG;
  			qpti->tag_ages[Cmnd->device->id] = jiffies;
  		} else
  			cmd->control_flags = CFLAG_SIMPLE_TAG;
  	}
  	if ((Cmnd->cmnd[0] == WRITE_6) ||
  	    (Cmnd->cmnd[0] == WRITE_10) ||
  	    (Cmnd->cmnd[0] == WRITE_12))
  		cmd->control_flags |= CFLAG_WRITE;
  	else
  		cmd->control_flags |= CFLAG_READ;
528c04ee6   Meelis Roos   [SCSI] qlogicpti:...
891
  	cmd->time_out = Cmnd->request->timeout/HZ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
893
894
895
896
897
898
899
  	memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
  }
  
  /* Do it to it baby. */
  static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
  			   struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
  {
  	struct dataseg *ds;
2f08fe522   Jens Axboe   qlogicpti: sg cha...
900
  	struct scatterlist *sg, *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
901
  	int i, n;
dfb104ff5   Boaz Harrosh   [SCSI] qlogicpti:...
902
  	if (scsi_bufflen(Cmnd)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
  		int sg_count;
dfb104ff5   Boaz Harrosh   [SCSI] qlogicpti:...
904
  		sg = scsi_sglist(Cmnd);
5dc2536bb   David S. Miller   qlogicpti: Conver...
905
  		sg_count = dma_map_sg(&qpti->op->dev, sg,
738f2b7b8   David S. Miller   sparc: Convert al...
906
907
  				      scsi_sg_count(Cmnd),
  				      Cmnd->sc_data_direction);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
909
910
911
912
913
914
915
  
  		ds = cmd->dataseg;
  		cmd->segment_cnt = sg_count;
  
  		/* Fill in first four sg entries: */
  		n = sg_count;
  		if (n > 4)
  			n = 4;
2f08fe522   Jens Axboe   qlogicpti: sg cha...
916
917
918
  		for_each_sg(sg, s, n, i) {
  			ds[i].d_base = sg_dma_address(s);
  			ds[i].d_count = sg_dma_len(s);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
920
  		}
  		sg_count -= 4;
2f08fe522   Jens Axboe   qlogicpti: sg cha...
921
  		sg = s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
  		while (sg_count > 0) {
  			struct Continuation_Entry *cont;
  
  			++cmd->hdr.entry_cnt;
  			cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
  			in_ptr = NEXT_REQ_PTR(in_ptr);
  			if (in_ptr == out_ptr)
  				return -1;
  
  			cont->hdr.entry_type = ENTRY_CONTINUATION;
  			cont->hdr.entry_cnt = 0;
  			cont->hdr.sys_def_1 = 0;
  			cont->hdr.flags = 0;
  			cont->reserved = 0;
  			ds = cont->dataseg;
  			n = sg_count;
  			if (n > 7)
  				n = 7;
2f08fe522   Jens Axboe   qlogicpti: sg cha...
940
941
942
  			for_each_sg(sg, s, n, i) {
  				ds[i].d_base = sg_dma_address(s);
  				ds[i].d_count = sg_dma_len(s);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
944
  			}
  			sg_count -= n;
7e6cfb54b   Boaz Harrosh   scsi: fix fall ou...
945
  			sg = s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  	} else {
  		cmd->dataseg[0].d_base = 0;
  		cmd->dataseg[0].d_count = 0;
  		cmd->segment_cnt = 1; /* Shouldn't this be 0? */
  	}
  
  	/* Committed, record Scsi_Cmd so we can find it later. */
  	cmd->handle = in_ptr;
  	qpti->cmd_slots[in_ptr] = Cmnd;
  
  	qpti->cmd_count[Cmnd->device->id]++;
  	sbus_writew(in_ptr, qpti->qregs + MBOX4);
  	qpti->req_in_ptr = in_ptr;
  
  	return in_ptr;
  }
  
  static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
  {
  	/* Temporary workaround until bug is found and fixed (one bug has been found
  	   already, but fixing it makes things even worse) -jj */
  	int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
  	host->can_queue = host->host_busy + num_free;
  	host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
  }
9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
972
  static int qlogicpti_slave_configure(struct scsi_device *sdev)
f75884d28   David S. Miller   [QLOGICPTI]: Hand...
973
  {
9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
974
975
976
  	struct qlogicpti *qpti = shost_priv(sdev->host);
  	int tgt = sdev->id;
  	u_short param[6];
f75884d28   David S. Miller   [QLOGICPTI]: Hand...
977

9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
978
979
980
981
  	/* tags handled in midlayer */
  	/* enable sync mode? */
  	if (sdev->sdtr) {
  		qpti->dev_param[tgt].device_flags |= 0x10;
f75884d28   David S. Miller   [QLOGICPTI]: Hand...
982
  	} else {
9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
983
984
  		qpti->dev_param[tgt].synchronous_offset = 0;
  		qpti->dev_param[tgt].synchronous_period = 0;
f75884d28   David S. Miller   [QLOGICPTI]: Hand...
985
  	}
9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
986
987
988
989
990
991
992
993
994
995
996
997
  	/* are we wide capable? */
  	if (sdev->wdtr)
  		qpti->dev_param[tgt].device_flags |= 0x20;
  
  	param[0] = MBOX_SET_TARGET_PARAMS;
  	param[1] = (tgt << 8);
  	param[2] = (qpti->dev_param[tgt].device_flags << 8);
  	if (qpti->dev_param[tgt].device_flags & 0x10) {
  		param[3] = (qpti->dev_param[tgt].synchronous_offset << 8) |
  			qpti->dev_param[tgt].synchronous_period;
  	} else {
  		param[3] = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
  	}
9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
999
1000
  	qlogicpti_mbox_command(qpti, param, 0);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
  }
  
  /*
   * The middle SCSI layer ensures that queuecommand never gets invoked
   * concurrently with itself or the interrupt handler (though the
   * interrupt handler may call this routine as part of
   * request-completion handling).
   *
   * "This code must fly." -davem
   */
f281233d3   Jeff Garzik   SCSI host lock pu...
1011
  static int qlogicpti_queuecommand_lck(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
  {
  	struct Scsi_Host *host = Cmnd->device->host;
  	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  	struct Command_Entry *cmd;
  	u_int out_ptr;
  	int in_ptr;
  
  	Cmnd->scsi_done = done;
  
  	in_ptr = qpti->req_in_ptr;
  	cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  	out_ptr = sbus_readw(qpti->qregs + MBOX4);
  	in_ptr = NEXT_REQ_PTR(in_ptr);
  	if (in_ptr == out_ptr)
  		goto toss_command;
  
  	if (qpti->send_marker) {
  		marker_frob(cmd);
  		qpti->send_marker = 0;
  		if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
  			sbus_writew(in_ptr, qpti->qregs + MBOX4);
  			qpti->req_in_ptr = in_ptr;
  			goto toss_command;
  		}
  		cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  		in_ptr = NEXT_REQ_PTR(in_ptr);
  	}
  	cmd_frob(cmd, Cmnd, qpti);
  	if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
  		goto toss_command;
  
  	update_can_queue(host, in_ptr, out_ptr);
  
  	return 0;
  
  toss_command:
  	printk(KERN_EMERG "qlogicpti%d: request queue overflow
  ",
  	       qpti->qpti_id);
  
  	/* Unfortunately, unless you use the new EH code, which
  	 * we don't, the midlayer will ignore the return value,
  	 * which is insane.  We pick up the pieces like this.
  	 */
  	Cmnd->result = DID_BUS_BUSY;
  	done(Cmnd);
  	return 1;
  }
f281233d3   Jeff Garzik   SCSI host lock pu...
1060
  static DEF_SCSI_QCMD(qlogicpti_queuecommand)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  static int qlogicpti_return_status(struct Status_Entry *sts, int id)
  {
  	int host_status = DID_ERROR;
  
  	switch (sts->completion_status) {
  	      case CS_COMPLETE:
  		host_status = DID_OK;
  		break;
  	      case CS_INCOMPLETE:
  		if (!(sts->state_flags & SF_GOT_BUS))
  			host_status = DID_NO_CONNECT;
  		else if (!(sts->state_flags & SF_GOT_TARGET))
  			host_status = DID_BAD_TARGET;
  		else if (!(sts->state_flags & SF_SENT_CDB))
  			host_status = DID_ERROR;
  		else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
  			host_status = DID_ERROR;
  		else if (!(sts->state_flags & SF_GOT_STATUS))
  			host_status = DID_ERROR;
  		else if (!(sts->state_flags & SF_GOT_SENSE))
  			host_status = DID_ERROR;
  		break;
  	      case CS_DMA_ERROR:
  	      case CS_TRANSPORT_ERROR:
  		host_status = DID_ERROR;
  		break;
  	      case CS_RESET_OCCURRED:
  	      case CS_BUS_RESET:
  		host_status = DID_RESET;
  		break;
  	      case CS_ABORTED:
  		host_status = DID_ABORT;
  		break;
  	      case CS_TIMEOUT:
  		host_status = DID_TIME_OUT;
  		break;
  	      case CS_DATA_OVERRUN:
  	      case CS_COMMAND_OVERRUN:
  	      case CS_STATUS_OVERRUN:
  	      case CS_BAD_MESSAGE:
  	      case CS_NO_MESSAGE_OUT:
  	      case CS_EXT_ID_FAILED:
  	      case CS_IDE_MSG_FAILED:
  	      case CS_ABORT_MSG_FAILED:
  	      case CS_NOP_MSG_FAILED:
  	      case CS_PARITY_ERROR_MSG_FAILED:
  	      case CS_DEVICE_RESET_MSG_FAILED:
  	      case CS_ID_MSG_FAILED:
  	      case CS_UNEXP_BUS_FREE:
  		host_status = DID_ERROR;
  		break;
  	      case CS_DATA_UNDERRUN:
  		host_status = DID_OK;
  		break;
  	      default:
585a8a59e   Mark   [SCSI] qlogicpti:...
1116
1117
  		printk(KERN_EMERG "qlogicpti%d: unknown completion status 0x%04x
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
  		       id, sts->completion_status);
  		host_status = DID_ERROR;
  		break;
  	}
  
  	return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
  }
  
  static struct scsi_cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
  {
  	struct scsi_cmnd *Cmnd, *done_queue = NULL;
  	struct Status_Entry *sts;
  	u_int in_ptr, out_ptr;
  
  	if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
  		return NULL;
  		
  	in_ptr = sbus_readw(qpti->qregs + MBOX5);
  	sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
  	if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
  		switch (sbus_readw(qpti->qregs + MBOX0)) {
  		case ASYNC_SCSI_BUS_RESET:
  		case EXECUTION_TIMEOUT_RESET:
  			qpti->send_marker = 1;
  			break;
  		case INVALID_COMMAND:
  		case HOST_INTERFACE_ERROR:
  		case COMMAND_ERROR:
  		case COMMAND_PARAM_ERROR:
  			break;
  		};
  		sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  	}
  
  	/* This looks like a network driver! */
  	out_ptr = qpti->res_out_ptr;
  	while (out_ptr != in_ptr) {
  		u_int cmd_slot;
  
  		sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
  		out_ptr = NEXT_RES_PTR(out_ptr);
  
  		/* We store an index in the handle, not the pointer in
  		 * some form.  This avoids problems due to the fact
  		 * that the handle provided is only 32-bits. -DaveM
  		 */
  		cmd_slot = sts->handle;
  		Cmnd = qpti->cmd_slots[cmd_slot];
  		qpti->cmd_slots[cmd_slot] = NULL;
  
  		if (sts->completion_status == CS_RESET_OCCURRED ||
  		    sts->completion_status == CS_ABORTED ||
  		    (sts->status_flags & STF_BUS_RESET))
  			qpti->send_marker = 1;
  
  		if (sts->state_flags & SF_GOT_SENSE)
  			memcpy(Cmnd->sense_buffer, sts->req_sense_data,
b80ca4f7e   FUJITA Tomonori   [SCSI] replace si...
1175
  			       SCSI_SENSE_BUFFERSIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1176
1177
1178
1179
1180
1181
  
  		if (sts->hdr.entry_type == ENTRY_STATUS)
  			Cmnd->result =
  			    qlogicpti_return_status(sts, qpti->qpti_id);
  		else
  			Cmnd->result = DID_ERROR << 16;
dfb104ff5   Boaz Harrosh   [SCSI] qlogicpti:...
1182
  		if (scsi_bufflen(Cmnd))
5dc2536bb   David S. Miller   qlogicpti: Conver...
1183
  			dma_unmap_sg(&qpti->op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
1184
1185
  				     scsi_sglist(Cmnd), scsi_sg_count(Cmnd),
  				     Cmnd->sc_data_direction);
dfb104ff5   Boaz Harrosh   [SCSI] qlogicpti:...
1186

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1187
1188
1189
1190
1191
1192
1193
1194
1195
  		qpti->cmd_count[Cmnd->device->id]--;
  		sbus_writew(out_ptr, qpti->qregs + MBOX5);
  		Cmnd->host_scribble = (unsigned char *) done_queue;
  		done_queue = Cmnd;
  	}
  	qpti->res_out_ptr = out_ptr;
  
  	return done_queue;
  }
7d12e780e   David Howells   IRQ: Maintain reg...
1196
  static irqreturn_t qpti_intr(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
  {
  	struct qlogicpti *qpti = dev_id;
  	unsigned long flags;
  	struct scsi_cmnd *dq;
  
  	spin_lock_irqsave(qpti->qhost->host_lock, flags);
  	dq = qlogicpti_intr_handler(qpti);
  
  	if (dq != NULL) {
  		do {
  			struct scsi_cmnd *next;
  
  			next = (struct scsi_cmnd *) dq->host_scribble;
  			dq->scsi_done(dq);
  			dq = next;
  		} while (dq != NULL);
  	}
  	spin_unlock_irqrestore(qpti->qhost->host_lock, flags);
  
  	return IRQ_HANDLED;
  }
  
  static int qlogicpti_abort(struct scsi_cmnd *Cmnd)
  {
  	u_short param[6];
  	struct Scsi_Host *host = Cmnd->device->host;
  	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  	int return_status = SUCCESS;
  	u32 cmd_cookie;
  	int i;
585a8a59e   Mark   [SCSI] qlogicpti:...
1227
1228
1229
  	printk(KERN_WARNING "qlogicpti%d: Aborting cmd for tgt[%d] lun[%d]
  ",
  	       qpti->qpti_id, (int)Cmnd->device->id, (int)Cmnd->device->lun);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
  
  	qlogicpti_disable_irqs(qpti);
  
  	/* Find the 32-bit cookie we gave to the firmware for
  	 * this command.
  	 */
  	for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
  		if (qpti->cmd_slots[i] == Cmnd)
  			break;
  	cmd_cookie = i;
  
  	param[0] = MBOX_ABORT;
  	param[1] = (((u_short) Cmnd->device->id) << 8) | Cmnd->device->lun;
  	param[2] = cmd_cookie >> 16;
  	param[3] = cmd_cookie & 0xffff;
  	if (qlogicpti_mbox_command(qpti, param, 0) ||
  	    (param[0] != MBOX_COMMAND_COMPLETE)) {
585a8a59e   Mark   [SCSI] qlogicpti:...
1247
1248
1249
  		printk(KERN_EMERG "qlogicpti%d: scsi abort failure: %x
  ",
  		       qpti->qpti_id, param[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
  		return_status = FAILED;
  	}
  
  	qlogicpti_enable_irqs(qpti);
  
  	return return_status;
  }
  
  static int qlogicpti_reset(struct scsi_cmnd *Cmnd)
  {
  	u_short param[6];
  	struct Scsi_Host *host = Cmnd->device->host;
  	struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  	int return_status = SUCCESS;
585a8a59e   Mark   [SCSI] qlogicpti:...
1264
1265
1266
  	printk(KERN_WARNING "qlogicpti%d: Resetting SCSI bus!
  ",
  	       qpti->qpti_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1267
1268
1269
1270
1271
1272
1273
  
  	qlogicpti_disable_irqs(qpti);
  
  	param[0] = MBOX_BUS_RESET;
  	param[1] = qpti->host_param.bus_reset_delay;
  	if (qlogicpti_mbox_command(qpti, param, 0) ||
  	   (param[0] != MBOX_COMMAND_COMPLETE)) {
585a8a59e   Mark   [SCSI] qlogicpti:...
1274
1275
1276
  		printk(KERN_EMERG "qlogicisp%d: scsi bus reset failure: %x
  ",
  		       qpti->qpti_id, param[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1277
1278
1279
1280
1281
1282
1283
  		return_status = FAILED;
  	}
  
  	qlogicpti_enable_irqs(qpti);
  
  	return return_status;
  }
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1284
1285
1286
  static struct scsi_host_template qpti_template = {
  	.module			= THIS_MODULE,
  	.name			= "qlogicpti",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1287
  	.info			= qlogicpti_info,
9ec76fbf7   Matthew Wilcox   [SCSI] qlogicpti:...
1288
1289
  	.queuecommand		= qlogicpti_queuecommand,
  	.slave_configure	= qlogicpti_slave_configure,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
1291
1292
1293
1294
1295
1296
1297
  	.eh_abort_handler	= qlogicpti_abort,
  	.eh_bus_reset_handler	= qlogicpti_reset,
  	.can_queue		= QLOGICPTI_REQ_QUEUE_LEN,
  	.this_id		= 7,
  	.sg_tablesize		= QLOGICPTI_MAX_SG(QLOGICPTI_REQ_QUEUE_LEN),
  	.cmd_per_lun		= 1,
  	.use_clustering		= ENABLE_CLUSTERING,
  };
b1608d69c   Grant Likely   drivercore: rever...
1298
  static const struct of_device_id qpti_match[];
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1299
  static int __devinit qpti_sbus_probe(struct platform_device *op)
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1300
  {
b1608d69c   Grant Likely   drivercore: rever...
1301
  	const struct of_device_id *match;
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1302
  	struct scsi_host_template *tpnt;
61c7a080a   Grant Likely   of: Always use 's...
1303
  	struct device_node *dp = op->dev.of_node;
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1304
1305
  	struct Scsi_Host *host;
  	struct qlogicpti *qpti;
5dc2536bb   David S. Miller   qlogicpti: Conver...
1306
  	static int nqptis;
ccf0dec6f   Stephen Rothwell   [SPARC/64] consti...
1307
  	const char *fcode;
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1308

b1608d69c   Grant Likely   drivercore: rever...
1309
1310
  	match = of_match_device(qpti_match, &op->dev);
  	if (!match)
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1311
  		return -EINVAL;
b1608d69c   Grant Likely   drivercore: rever...
1312
  	tpnt = match->data;
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1313

3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1314
1315
1316
  	/* Sometimes Antares cards come up not completely
  	 * setup, and we get a report of a zero IRQ.
  	 */
1636f8ac2   Grant Likely   sparc/of: Move of...
1317
  	if (op->archdata.irqs[0] == 0)
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1318
1319
1320
1321
1322
  		return -ENODEV;
  
  	host = scsi_host_alloc(tpnt, sizeof(struct qlogicpti));
  	if (!host)
  		return -ENOMEM;
5dc2536bb   David S. Miller   qlogicpti: Conver...
1323
  	qpti = shost_priv(host);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1324
1325
1326
  
  	host->max_id = MAX_TARGETS;
  	qpti->qhost = host;
5dc2536bb   David S. Miller   qlogicpti: Conver...
1327
  	qpti->op = op;
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1328
  	qpti->qpti_id = nqptis;
61c7a080a   Grant Likely   of: Always use 's...
1329
  	strcpy(qpti->prom_name, op->dev.of_node->name);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
  	qpti->is_pti = strcmp(qpti->prom_name, "QLGC,isp");
  
  	if (qpti_map_regs(qpti) < 0)
  		goto fail_unlink;
  
  	if (qpti_register_irq(qpti) < 0)
  		goto fail_unmap_regs;
  
  	qpti_get_scsi_id(qpti);
  	qpti_get_bursts(qpti);
  	qpti_get_clock(qpti);
  
  	/* Clear out scsi_cmnd array. */
  	memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
  
  	if (qpti_map_queues(qpti) < 0)
  		goto fail_free_irq;
  
  	/* Load the firmware. */
  	if (qlogicpti_load_firmware(qpti))
  		goto fail_unmap_queues;
  	if (qpti->is_pti) {
  		/* Check the PTI status reg. */
  		if (qlogicpti_verify_tmon(qpti))
  			goto fail_unmap_queues;
  	}
  
  	/* Reset the ISP and init res/req queues. */
  	if (qlogicpti_reset_hardware(host))
  		goto fail_unmap_queues;
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1360
1361
1362
1363
1364
  	printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
  	       qpti->fware_minrev, qpti->fware_micrev);
  
  	fcode = of_get_property(dp, "isp-fcode", NULL);
  	if (fcode && fcode[0])
585a8a59e   Mark   [SCSI] qlogicpti:...
1365
  		printk("(FCode %s)", fcode);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1366
1367
1368
  	if (of_find_property(dp, "differential", NULL) != NULL)
  		qpti->differential = 1;
  			
585a8a59e   Mark   [SCSI] qlogicpti:...
1369
1370
1371
1372
  	printk("
  qlogicpti%d: [%s Wide, using %s interface]
  ",
  		qpti->qpti_id,
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1373
1374
  		(qpti->ultra ? "Ultra" : "Fast"),
  		(qpti->differential ? "differential" : "single ended"));
5dc2536bb   David S. Miller   qlogicpti: Conver...
1375
  	if (scsi_add_host(host, &op->dev)) {
585a8a59e   Mark   [SCSI] qlogicpti:...
1376
1377
1378
1379
  		printk("qlogicpti%d: Failed scsi_add_host
  ", qpti->qpti_id);
  		goto fail_unmap_queues;
  	}
5dc2536bb   David S. Miller   qlogicpti: Conver...
1380
  	dev_set_drvdata(&op->dev, qpti);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  
  	qpti_chain_add(qpti);
  
  	scsi_scan_host(host);
  	nqptis++;
  
  	return 0;
  
  fail_unmap_queues:
  #define QSIZE(entries)	(((entries) + 1) * QUEUE_ENTRY_LEN)
5dc2536bb   David S. Miller   qlogicpti: Conver...
1391
  	dma_free_coherent(&op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
1392
1393
  			  QSIZE(RES_QUEUE_LEN),
  			  qpti->res_cpu, qpti->res_dvma);
5dc2536bb   David S. Miller   qlogicpti: Conver...
1394
  	dma_free_coherent(&op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
1395
1396
  			  QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  			  qpti->req_cpu, qpti->req_dvma);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1397
1398
1399
  #undef QSIZE
  
  fail_unmap_regs:
5dc2536bb   David S. Miller   qlogicpti: Conver...
1400
1401
  	of_iounmap(&op->resource[0], qpti->qregs,
  		   resource_size(&op->resource[0]));
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1402
  	if (qpti->is_pti)
5dc2536bb   David S. Miller   qlogicpti: Conver...
1403
1404
  		of_iounmap(&op->resource[0], qpti->sreg,
  			   sizeof(unsigned char));
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1405
1406
1407
1408
1409
1410
1411
1412
1413
  
  fail_free_irq:
  	free_irq(qpti->irq, qpti);
  
  fail_unlink:
  	scsi_host_put(host);
  
  	return -ENODEV;
  }
2dc115813   Grant Likely   of/device: Replac...
1414
  static int __devexit qpti_sbus_remove(struct platform_device *op)
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1415
  {
5dc2536bb   David S. Miller   qlogicpti: Conver...
1416
  	struct qlogicpti *qpti = dev_get_drvdata(&op->dev);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
  
  	qpti_chain_del(qpti);
  
  	scsi_remove_host(qpti->qhost);
  
  	/* Shut up the card. */
  	sbus_writew(0, qpti->qregs + SBUS_CTRL);
  
  	/* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
  	free_irq(qpti->irq, qpti);
  
  #define QSIZE(entries)	(((entries) + 1) * QUEUE_ENTRY_LEN)
5dc2536bb   David S. Miller   qlogicpti: Conver...
1429
  	dma_free_coherent(&op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
1430
1431
  			  QSIZE(RES_QUEUE_LEN),
  			  qpti->res_cpu, qpti->res_dvma);
5dc2536bb   David S. Miller   qlogicpti: Conver...
1432
  	dma_free_coherent(&op->dev,
738f2b7b8   David S. Miller   sparc: Convert al...
1433
1434
  			  QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  			  qpti->req_cpu, qpti->req_dvma);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1435
  #undef QSIZE
5dc2536bb   David S. Miller   qlogicpti: Conver...
1436
1437
  	of_iounmap(&op->resource[0], qpti->qregs,
  		   resource_size(&op->resource[0]));
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1438
  	if (qpti->is_pti)
5dc2536bb   David S. Miller   qlogicpti: Conver...
1439
  		of_iounmap(&op->resource[0], qpti->sreg, sizeof(unsigned char));
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1440
1441
1442
1443
1444
  
  	scsi_host_put(qpti->qhost);
  
  	return 0;
  }
fd098316e   David S. Miller   sparc: Annotate o...
1445
  static const struct of_device_id qpti_match[] = {
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
  	{
  		.name = "ptisp",
  		.data = &qpti_template,
  	},
  	{
  		.name = "PTI,ptisp",
  		.data = &qpti_template,
  	},
  	{
  		.name = "QLGC,isp",
  		.data = &qpti_template,
  	},
  	{
  		.name = "SUNW,isp",
  		.data = &qpti_template,
  	},
  	{},
  };
  MODULE_DEVICE_TABLE(of, qpti_match);
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1465
  static struct platform_driver qpti_sbus_driver = {
4018294b5   Grant Likely   of: Remove duplic...
1466
1467
1468
1469
1470
  	.driver = {
  		.name = "qpti",
  		.owner = THIS_MODULE,
  		.of_match_table = qpti_match,
  	},
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1471
1472
1473
  	.probe		= qpti_sbus_probe,
  	.remove		= __devexit_p(qpti_sbus_remove),
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1474

3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1475
1476
  static int __init qpti_init(void)
  {
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1477
  	return platform_driver_register(&qpti_sbus_driver);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1478
1479
1480
1481
  }
  
  static void __exit qpti_exit(void)
  {
4ebb24f70   Grant Likely   dt/sparc: Elimina...
1482
  	platform_driver_unregister(&qpti_sbus_driver);
3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1483
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484

3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1485
1486
  MODULE_DESCRIPTION("QlogicISP SBUS driver");
  MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1487
  MODULE_LICENSE("GPL");
5dc2536bb   David S. Miller   qlogicpti: Conver...
1488
  MODULE_VERSION("2.1");
f894e74dc   Jaswinder Singh Rajput   [SCSI] qlogicpti:...
1489
  MODULE_FIRMWARE("qlogic/isp1000.bin");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1490

3d4253d9a   David S. Miller   [SCSI] qlogicpti:...
1491
1492
  module_init(qpti_init);
  module_exit(qpti_exit);