aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/pluto.c
blob: 7abf64d1bfc9960c051b827b6d3c0377e51041be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
128
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
157
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
193
194
195
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* pluto.c: SparcSTORAGE Array SCSI host adapter driver.
 *
 * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
 *
 */

#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/blkdev.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/init.h>
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif

#include <asm/irq.h>

#include "scsi.h"
#include <scsi/scsi_host.h>
#include "../fc4/fcp_impl.h"
#include "pluto.h"

#include <linux/module.h>

#define RQ_SCSI_BUSY		0xffff
#define RQ_SCSI_DONE		0xfffe

/* #define PLUTO_DEBUG */

#define pluto_printk printk ("PLUTO %s: ", fc->name); printk

#ifdef PLUTO_DEBUG
#define PLD(x)  pluto_printk x;
#define PLND(x) printk ("PLUTO: "); printk x;
#else
#define PLD(x)
#define PLND(x)
#endif

static struct ctrl_inquiry {
	struct Scsi_Host host;
	struct pluto pluto;
	Scsi_Cmnd cmd;
	char inquiry[256];
	fc_channel *fc;
} *fcs __initdata;
static int fcscount __initdata = 0;
static atomic_t fcss __initdata = ATOMIC_INIT(0);
DECLARE_MUTEX_LOCKED(fc_sem);

static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd);

static void __init pluto_detect_timeout(unsigned long data)
{
	PLND(("Timeout\n"))
	up(&fc_sem);
}

static void __init pluto_detect_done(Scsi_Cmnd *SCpnt)
{
	/* Do nothing */
}

static void __init pluto_detect_scsi_done(Scsi_Cmnd *SCpnt)
{
	SCpnt->request->rq_status = RQ_SCSI_DONE;
	PLND(("Detect done %08lx\n", (long)SCpnt))
	if (atomic_dec_and_test (&fcss))
		up(&fc_sem);
}

int pluto_slave_configure(struct scsi_device *device)
{
	int depth_to_use;

	if (device->tagged_supported)
		depth_to_use = /* 254 */ 8;
	else
		depth_to_use = 2;

	scsi_adjust_queue_depth(device,
				(device->tagged_supported ?
				 MSG_SIMPLE_TAG : 0),
				depth_to_use);

	return 0;
}

/* Detect all SSAs attached to the machine.
   To be fast, do it on all online FC channels at the same time. */
int __init pluto_detect(struct scsi_host_template *tpnt)
{
	int i, retry, nplutos;
	fc_channel *fc;
	struct scsi_device dev;
	DEFINE_TIMER(fc_timer, pluto_detect_timeout, 0, 0);

	tpnt->proc_name = "pluto";
	fcscount = 0;
	for_each_online_fc_channel(fc) {
		if (!fc->posmap)
			fcscount++;
	}
	PLND(("%d channels online\n", fcscount))
	if (!fcscount) {
#if defined(MODULE) && defined(CONFIG_FC4_SOC_MODULE) && defined(CONFIG_KMOD)
		request_module("soc");
		
		for_each_online_fc_channel(fc) {
			if (!fc->posmap)
				fcscount++;
		}
		if (!fcscount)
#endif
			return 0;
	}
	fcs = (struct ctrl_inquiry *) kmalloc (sizeof (struct ctrl_inquiry) * fcscount, GFP_DMA);
	if (!fcs) {
		printk ("PLUTO: Not enough memory to probe\n");
		return 0;
	}
	
	memset (fcs, 0, sizeof (struct ctrl_inquiry) * fcscount);
	memset (&dev, 0, sizeof(dev));
	atomic_set (&fcss, fcscount);
	
	i = 0;
	for_each_online_fc_channel(fc) {
		Scsi_Cmnd *SCpnt;
		struct Scsi_Host *host;
		struct pluto *pluto;

		if (i == fcscount) break;
		if (fc->posmap) continue;
		
		PLD(("trying to find SSA\n"))

		/* If this is already registered to some other SCSI host, then it cannot be pluto */
		if (fc->scsi_name[0]) continue;
		memcpy (fc->scsi_name, "SSA", 4);
		
		fcs[i].fc = fc;
		
		fc->can_queue = PLUTO_CAN_QUEUE;
		fc->rsp_size = 64;
		fc->encode_addr = pluto_encode_addr;
		
		fc->fcp_register(fc, TYPE_SCSI_FCP, 0);
	
		SCpnt = &(fcs[i].cmd);
		host = &(fcs[i].host);
		pluto = (struct pluto *)host->hostdata;
		
		pluto->fc = fc;
	
		SCpnt->cmnd[0] = INQUIRY;
		SCpnt->cmnd[4] = 255;
		
		/* FC layer requires this, so that SCpnt->device->tagged_supported is initially 0 */
		SCpnt->device = &dev;
		dev.host = host;
		
		SCpnt->cmd_len = COMMAND_SIZE(INQUIRY);
	
		SCpnt->request->rq_status = RQ_SCSI_BUSY;
		
		SCpnt->done = pluto_detect_done;
		SCpnt->bufflen = 256;
		SCpnt->buffer = fcs[i].inquiry;
		SCpnt->request_bufflen = 256;
		SCpnt->request_buffer = fcs[i].inquiry;
		PLD(("set up %d %08lx\n", i, (long)SCpnt))
		i++;
	}
	
	for (retry = 0; retry < 5; retry++) {
		for (i = 0; i < fcscount; i++) {
			if (!fcs[i].fc) break;
			if (fcs[i].cmd.request->rq_status != RQ_SCSI_DONE) {
				disable_irq(fcs[i].fc->irq);
				PLND(("queuecommand %d %d\n", retry, i))
				fcp_scsi_queuecommand (&(fcs[i].cmd), 
					pluto_detect_scsi_done);
				enable_irq(fcs[i].fc->irq);
			}
		}
	    
		fc_timer.expires = jiffies + 10 * HZ;
		add_timer(&fc_timer);
		
		down(&fc_sem);
		PLND(("Woken up\n"))
		if (!atomic_read(&fcss))
			break; /* All fc channels have answered us */
	}
	del_timer_sync(&fc_timer);

	PLND(("Finished search\n"))
	for (i = 0, nplutos = 0; i < fcscount; i++) {
		Scsi_Cmnd *SCpnt;
		
		if (!(fc = fcs[i].fc)) break;
	
		SCpnt = &(fcs[i].cmd);
		
		/* Let FC mid-level free allocated resources */
		SCpnt->done (SCpnt);
		
		if (!SCpnt->result) {
			struct pluto_inquiry *inq;
			struct pluto *pluto;
			struct Scsi_Host *host;
			
			inq = (struct pluto_inquiry *)fcs[i].inquiry;

			if ((inq->dtype & 0x1f) == TYPE_PROCESSOR &&
			    !strncmp (inq->vendor_id, "SUN", 3) &&
			    !strncmp (inq->product_id, "SSA", 3)) {
				char *p;
				long *ages;
				
				ages = kmalloc (((inq->channels + 1) * inq->targets) * sizeof(long), GFP_KERNEL);
				if (!ages) continue;
				
				host = scsi_register (tpnt, sizeof (struct pluto));
				if(!host)
				{
					kfree(ages);
					continue;
				}
				
				if (!try_module_get(fc->module)) {
					kfree(ages);
					scsi_unregister(host);
					continue;
				}

				nplutos++;
				
				pluto = (struct pluto *)host->hostdata;
				
				host->max_id = inq->targets;
				host->max_channel = inq->channels;
				host->irq = fc->irq;

				fc->channels = inq->channels + 1;
				fc->targets = inq->targets;
				fc->ages = ages;
				memset (ages, 0, ((inq->channels + 1) * inq->targets) * sizeof(long));
				
				pluto->fc = fc;
				memcpy (pluto->rev_str, inq->revision, 4);
				pluto->rev_str[4] = 0;
				p = strchr (pluto->rev_str, ' ');
				if (p) *p = 0;
				memcpy (pluto->fw_rev_str, inq->fw_revision, 4);
				pluto->fw_rev_str[4] = 0;
				p = strchr (pluto->fw_rev_str, ' ');
				if (p) *p = 0;
				memcpy (pluto->serial_str, inq->serial, 12);
				pluto->serial_str[12] = 0;
				p = strchr (pluto->serial_str, ' ');
				if (p) *p = 0;
				
				PLD(("Found SSA rev %s fw rev %s serial %s %dx%d\n", pluto->rev_str, pluto->fw_rev_str, pluto->serial_str, host->max_channel, host->max_id))
			} else
				fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
		} else
			fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
	}
	kfree((char *)fcs);
	if (nplutos)
		printk ("PLUTO: Total of %d SparcSTORAGE Arrays found\n", nplutos);
	return nplutos;
}

int pluto_release(struct Scsi_Host *host)
{
	struct pluto *pluto = (struct pluto *)host->hostdata;
	fc_channel *fc = pluto->fc;

	module_put(fc->module);
	
	fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
	PLND((" releasing pluto.\n"));
	kfree (fc->ages);
	PLND(("released pluto!\n"));
	return 0;
}

const char *pluto_info(struct Scsi_Host *host)
{
	static char buf[128], *p;
	struct pluto *pluto = (struct pluto *) host->hostdata;

	sprintf(buf, "SUN SparcSTORAGE Array %s fw %s serial %s %dx%d on %s",
		pluto->rev_str, pluto->fw_rev_str, pluto->serial_str,
		host->max_channel, host->max_id, pluto->fc->name);
#ifdef __sparc__
	p = strchr(buf, 0);
	sprintf(p, " PROM node %x", pluto->fc->dev->prom_node);
#endif	
	return buf;
}

/* SSA uses this FC4S addressing:
   switch (addr[0])
   {
   case 0: CONTROLLER - All of addr[1]..addr[3] has to be 0
   case 1: SINGLE DISK - addr[1] channel, addr[2] id, addr[3] 0
   case 2: DISK GROUP - ???
   }
   
   So that SCSI mid-layer can access to these, we reserve
   channel 0 id 0 lun 0 for CONTROLLER
   and channels 1 .. max_channel are normal single disks.
 */
static int pluto_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd)
{
	PLND(("encode addr %d %d %d\n", SCpnt->device->channel, SCpnt->device->id, SCpnt->cmnd[1] & 0xe0))
	/* We don't support LUNs - neither does SSA :) */
	if (SCpnt->cmnd[1] & 0xe0)
		return -EINVAL;
	if (!SCpnt->device->channel) {
		if (SCpnt->device->id)
			return -EINVAL;
		memset (addr, 0, 4 * sizeof(u16));
	} else {
		addr[0] = 1;
		addr[1] = SCpnt->device->channel - 1;
		addr[2] = SCpnt->device->id;
		addr[3] = 0;
	}
	/* We're Point-to-Point, so target it to the default DID */
	fcmd->did = fc->did;
	PLND(("trying %04x%04x%04x%04x\n", addr[0], addr[1], addr[2], addr[3]))
	return 0;
}

static struct scsi_host_template driver_template = {
	.name			= "Sparc Storage Array 100/200",
	.detect			= pluto_detect,
	.release		= pluto_release,
	.info			= pluto_info,
	.queuecommand		= fcp_scsi_queuecommand,
	.slave_configure	= pluto_slave_configure,
	.can_queue		= PLUTO_CAN_QUEUE,
	.this_id		= -1,
	.sg_tablesize		= 1,
	.cmd_per_lun		= 1,
	.use_clustering		= ENABLE_CLUSTERING,
	.eh_abort_handler	= fcp_scsi_abort,
	.eh_device_reset_handler = fcp_scsi_dev_reset,
	.eh_host_reset_handler	= fcp_scsi_host_reset,
};

#include "scsi_module.c"

MODULE_LICENSE("GPL");