Blame view

drivers/scsi/megaraid.c 107 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
  /*
   *
   *			Linux MegaRAID device driver
   *
3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
5
   * Copyright (c) 2002  LSI Logic Corporation.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   *
   *	   This program is free software; you can redistribute it and/or
   *	   modify it under the terms of the GNU General Public License
   *	   as published by the Free Software Foundation; either version
   *	   2 of the License, or (at your option) any later version.
   *
   * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
   *	  - fixes
   *	  - speed-ups (list handling fixes, issued_list, optimizations.)
   *	  - lots of cleanups.
   *
   * Copyright (c) 2003  Christoph Hellwig  <hch@lst.de>
   *	  - new-style, hotplug-aware pci probing and scsi registration
   *
3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
20
21
   * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
   * 						<Seokmann.Ju@lsil.com>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   *
   * Description: Linux device driver for LSI Logic MegaRAID controller
   *
   * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
   *					518, 520, 531, 532
   *
   * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
   * and others. Please send updates to the mailing list
   * linux-scsi@vger.kernel.org .
   *
   */
  
  #include <linux/mm.h>
  #include <linux/fs.h>
  #include <linux/blkdev.h>
  #include <asm/uaccess.h>
  #include <asm/io.h>
8d115f845   Christoph Hellwig   [SCSI] remove scs...
39
  #include <linux/completion.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
  #include <linux/delay.h>
  #include <linux/proc_fs.h>
c7f079ca3   David Howells   megaraid: Don't u...
42
  #include <linux/seq_file.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
48
  #include <linux/reboot.h>
  #include <linux/module.h>
  #include <linux/list.h>
  #include <linux/interrupt.h>
  #include <linux/pci.h>
  #include <linux/init.h>
910638ae7   Matthias Gehre   [PATCH] Replace 0...
49
  #include <linux/dma-mapping.h>
c45d15d24   Arnd Bergmann   scsi: autoconvert...
50
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
51
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
57
  #include <scsi/scsicam.h>
  
  #include "scsi.h"
  #include <scsi/scsi_host.h>
  
  #include "megaraid.h"
3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
58
  #define MEGARAID_MODULE_VERSION "2.00.4"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59

3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
60
61
  MODULE_AUTHOR ("sju@lsil.com");
  MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
  MODULE_LICENSE ("GPL");
  MODULE_VERSION(MEGARAID_MODULE_VERSION);
c45d15d24   Arnd Bergmann   scsi: autoconvert...
64
  static DEFINE_MUTEX(megadev_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
71
72
73
74
75
76
  static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  module_param(max_cmd_per_lun, uint, 0);
  MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  
  static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  module_param(max_sectors_per_io, ushort, 0);
  MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  
  
  static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  module_param(max_mbox_busy_wait, ushort, 0);
  MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
00769ec40   Jeff Garzik   [SCSI] megaraid: ...
77
78
79
80
  #define RDINDOOR(adapter)	readl((adapter)->mmio_base + 0x20)
  #define RDOUTDOOR(adapter)	readl((adapter)->mmio_base + 0x2C)
  #define WRINDOOR(adapter,value)	 writel(value, (adapter)->mmio_base + 0x20)
  #define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
86
87
88
89
90
91
  
  /*
   * Global variables
   */
  
  static int hba_count;
  static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  static struct proc_dir_entry *mega_proc_dir_entry;
  
  /* For controller re-ordering */
  static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
f4927c45b   Arnd Bergmann   scsi: Push down B...
92
93
  static long
  megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
  /*
   * The File Operations structure for the serial/ioctl interface of the driver
   */
00977a59b   Arjan van de Ven   [PATCH] mark stru...
97
  static const struct file_operations megadev_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  	.owner		= THIS_MODULE,
f4927c45b   Arnd Bergmann   scsi: Push down B...
99
  	.unlocked_ioctl	= megadev_unlocked_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  	.open		= megadev_open,
6038f373a   Arnd Bergmann   llseek: automatic...
101
  	.llseek		= noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  };
  
  /*
   * Array to structures for storing the information about the controllers. This
   * information is sent to the user level applications, when they do an ioctl
   * for this information.
   */
  static struct mcontroller mcontroller[MAX_CONTROLLERS];
  
  /* The current driver version */
  static u32 driver_ver = 0x02000000;
  
  /* major number used by the device for character interface */
  static int major;
  
  #define IS_RAID_CH(hba, ch)	(((hba)->mega_ch_class >> (ch)) & 0x01)
  
  
  /*
   * Debug variable to print some diagnostic messages
   */
  static int trace_level;
  
  /**
   * mega_setup_mailbox()
   * @adapter - pointer to our soft state
   *
   * Allocates a 8 byte aligned memory for the handshake mailbox.
   */
  static int
  mega_setup_mailbox(adapter_t *adapter)
  {
  	unsigned long	align;
  
  	adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
  			sizeof(mbox64_t), &adapter->una_mbox64_dma);
  
  	if( !adapter->una_mbox64 ) return -1;
  		
  	adapter->mbox = &adapter->una_mbox64->mbox;
  
  	adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
  			(~0UL ^ 0xFUL));
  
  	adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
  
  	align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
  
  	adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
  
  	/*
  	 * Register the mailbox if the controller is an io-mapped controller
  	 */
  	if( adapter->flag & BOARD_IOMAP ) {
7d1abbe82   Alan Cox   [SCSI] megaraid: ...
156
  		outb(adapter->mbox_dma & 0xFF,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  				adapter->host->io_port + MBOX_PORT0);
7d1abbe82   Alan Cox   [SCSI] megaraid: ...
158
  		outb((adapter->mbox_dma >> 8) & 0xFF,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
  				adapter->host->io_port + MBOX_PORT1);
7d1abbe82   Alan Cox   [SCSI] megaraid: ...
160
  		outb((adapter->mbox_dma >> 16) & 0xFF,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  				adapter->host->io_port + MBOX_PORT2);
7d1abbe82   Alan Cox   [SCSI] megaraid: ...
162
  		outb((adapter->mbox_dma >> 24) & 0xFF,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
  				adapter->host->io_port + MBOX_PORT3);
7d1abbe82   Alan Cox   [SCSI] megaraid: ...
164
  		outb(ENABLE_MBOX_BYTE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  				adapter->host->io_port + ENABLE_MBOX_REGION);
  
  		irq_ack(adapter);
  
  		irq_enable(adapter);
  	}
  
  	return 0;
  }
  
  
  /*
   * mega_query_adapter()
   * @adapter - pointer to our soft state
   *
   * Issue the adapter inquiry commands to the controller and find out
   * information and parameter about the devices attached
   */
  static int
  mega_query_adapter(adapter_t *adapter)
  {
  	dma_addr_t	prod_info_dma_handle;
  	mega_inquiry3	*inquiry3;
  	u8	raw_mbox[sizeof(struct mbox_out)];
  	mbox_t	*mbox;
  	int	retval;
  
  	/* Initialize adapter inquiry mailbox */
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  
  	/*
  	 * Try to issue Inquiry3 command
  	 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
  	 * update enquiry3 structure
  	 */
  	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  
  	inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
  
  	raw_mbox[0] = FC_NEW_CONFIG;		/* i.e. mbox->cmd=0xA1 */
  	raw_mbox[2] = NC_SUBOP_ENQUIRY3;	/* i.e. 0x0F */
  	raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;	/* i.e. 0x02 */
  
  	/* Issue a blocking command to the card */
  	if ((retval = issue_scb_block(adapter, raw_mbox))) {
  		/* the adapter does not support 40ld */
  
  		mraid_ext_inquiry	*ext_inq;
  		mraid_inquiry		*inq;
  		dma_addr_t		dma_handle;
  
  		ext_inq = pci_alloc_consistent(adapter->dev,
  				sizeof(mraid_ext_inquiry), &dma_handle);
  
  		if( ext_inq == NULL ) return -1;
  
  		inq = &ext_inq->raid_inq;
  
  		mbox->m_out.xferaddr = (u32)dma_handle;
  
  		/*issue old 0x04 command to adapter */
  		mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  
  		issue_scb_block(adapter, raw_mbox);
  
  		/*
  		 * update Enquiry3 and ProductInfo structures with
  		 * mraid_inquiry structure
  		 */
  		mega_8_to_40ld(inq, inquiry3,
  				(mega_product_info *)&adapter->product_info);
  
  		pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
  				ext_inq, dma_handle);
  
  	} else {		/*adapter supports 40ld */
  		adapter->flag |= BOARD_40LD;
  
  		/*
  		 * get product_info, which is static information and will be
  		 * unchanged
  		 */
  		prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
  				&adapter->product_info,
  				sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  
  		mbox->m_out.xferaddr = prod_info_dma_handle;
  
  		raw_mbox[0] = FC_NEW_CONFIG;	/* i.e. mbox->cmd=0xA1 */
  		raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;	/* i.e. 0x0E */
  
  		if ((retval = issue_scb_block(adapter, raw_mbox)))
  			printk(KERN_WARNING
  			"megaraid: Product_info cmd failed with error: %d
  ",
  				retval);
  
  		pci_unmap_single(adapter->dev, prod_info_dma_handle,
  				sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  	}
  
  
  	/*
  	 * kernel scans the channels from 0 to <= max_channel
  	 */
  	adapter->host->max_channel =
  		adapter->product_info.nchannels + NVIRT_CHAN -1;
  
  	adapter->host->max_id = 16;	/* max targets per channel */
25985edce   Lucas De Marchi   Fix common misspe...
278
  	adapter->host->max_lun = 7;	/* Up to 7 luns for non disk devices */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  
  	adapter->host->cmd_per_lun = max_cmd_per_lun;
  
  	adapter->numldrv = inquiry3->num_ldrv;
  
  	adapter->max_cmds = adapter->product_info.max_commands;
  
  	if(adapter->max_cmds > MAX_COMMANDS)
  		adapter->max_cmds = MAX_COMMANDS;
  
  	adapter->host->can_queue = adapter->max_cmds - 1;
  
  	/*
  	 * Get the maximum number of scatter-gather elements supported by this
  	 * firmware
  	 */
  	mega_get_max_sgl(adapter);
  
  	adapter->host->sg_tablesize = adapter->sglen;
124dd90f6   Adam Radford   [SCSI] megaraid: ...
298
299
300
301
  	/* use HP firmware and bios version encoding
  	   Note: fw_version[0|1] and bios_version[0|1] were originally shifted
  	   right 8 bits making them zero. This 0 value was hardcoded to fix
  	   sparse warnings. */
54ebfd578   Jon Mason   megaraid: remove ...
302
  	if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
  		sprintf (adapter->fw_version, "%c%d%d.%d%d",
  			 adapter->product_info.fw_version[2],
124dd90f6   Adam Radford   [SCSI] megaraid: ...
305
  			 0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
  			 adapter->product_info.fw_version[1] & 0x0f,
124dd90f6   Adam Radford   [SCSI] megaraid: ...
307
  			 0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
  			 adapter->product_info.fw_version[0] & 0x0f);
  		sprintf (adapter->bios_version, "%c%d%d.%d%d",
  			 adapter->product_info.bios_version[2],
124dd90f6   Adam Radford   [SCSI] megaraid: ...
311
  			 0,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  			 adapter->product_info.bios_version[1] & 0x0f,
124dd90f6   Adam Radford   [SCSI] megaraid: ...
313
  			 0,
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  			 adapter->product_info.bios_version[0] & 0x0f);
  	} else {
  		memcpy(adapter->fw_version,
  				(char *)adapter->product_info.fw_version, 4);
  		adapter->fw_version[4] = 0;
  
  		memcpy(adapter->bios_version,
  				(char *)adapter->product_info.bios_version, 4);
  
  		adapter->bios_version[4] = 0;
  	}
  
  	printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.
  ",
  		adapter->fw_version, adapter->bios_version, adapter->numldrv);
  
  	/*
  	 * Do we support extended (>10 bytes) cdbs
  	 */
  	adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
  	if (adapter->support_ext_cdb)
  		printk(KERN_NOTICE "megaraid: supports extended CDBs.
  ");
  
  
  	return 0;
  }
  
  /**
   * mega_runpendq()
   * @adapter - pointer to our soft state
   *
   * Runs through the list of pending requests.
   */
  static inline void
  mega_runpendq(adapter_t *adapter)
  {
  	if(!list_empty(&adapter->pending_list))
  		__mega_runpendq(adapter);
  }
  
  /*
   * megaraid_queue()
   * @scmd - Issue this scsi command
   * @done - the callback hook into the scsi mid-layer
   *
   * The command queuing entry point for the mid-layer.
   */
  static int
f281233d3   Jeff Garzik   SCSI host lock pu...
363
  megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
  {
  	adapter_t	*adapter;
  	scb_t	*scb;
  	int	busy=0;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
368
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  
  	adapter = (adapter_t *)scmd->device->host->hostdata;
  
  	scmd->scsi_done = done;
  
  
  	/*
  	 * Allocate and build a SCB request
  	 * busy flag will be set if mega_build_cmd() command could not
  	 * allocate scb. We will return non-zero status in that case.
  	 * NOTE: scb can be null even though certain commands completed
  	 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
  	 * return 0 in that case.
  	 */
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
383
  	spin_lock_irqsave(&adapter->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
  	scb = mega_build_cmd(adapter, scmd, &busy);
238f9b063   Christoph Hellwig   [PATCH] fix megar...
385
386
  	if (!scb)
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387

238f9b063   Christoph Hellwig   [PATCH] fix megar...
388
389
  	scb->state |= SCB_PENDQ;
  	list_add_tail(&scb->list, &adapter->pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390

238f9b063   Christoph Hellwig   [PATCH] fix megar...
391
392
393
394
395
396
397
  	/*
  	 * Check if the HBA is in quiescent state, e.g., during a
  	 * delete logical drive opertion. If it is, don't run
  	 * the pending_list.
  	 */
  	if (atomic_read(&adapter->quiescent) == 0)
  		mega_runpendq(adapter);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398

238f9b063   Christoph Hellwig   [PATCH] fix megar...
399
400
401
  	busy = 0;
   out:
  	spin_unlock_irqrestore(&adapter->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
  	return busy;
  }
f281233d3   Jeff Garzik   SCSI host lock pu...
404
  static DEF_SCSI_QCMD(megaraid_queue)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  /**
   * mega_allocate_scb()
   * @adapter - pointer to our soft state
   * @cmd - scsi command from the mid-layer
   *
   * Allocate a SCB structure. This is the central structure for controller
   * commands.
   */
  static inline scb_t *
  mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
  {
  	struct list_head *head = &adapter->free_list;
  	scb_t	*scb;
  
  	/* Unlink command from Free List */
  	if( !list_empty(head) ) {
  
  		scb = list_entry(head->next, scb_t, list);
  
  		list_del_init(head->next);
  
  		scb->state = SCB_ACTIVE;
  		scb->cmd = cmd;
  		scb->dma_type = MEGA_DMA_TYPE_NONE;
  
  		return scb;
  	}
  
  	return NULL;
  }
  
  /**
   * mega_get_ldrv_num()
   * @adapter - pointer to our soft state
   * @cmd - scsi mid layer command
   * @channel - channel on the controller
   *
   * Calculate the logical drive number based on the information in scsi command
   * and the channel number.
   */
  static inline int
  mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
  {
  	int		tgt;
  	int		ldrv_num;
  
  	tgt = cmd->device->id;
  	
  	if ( tgt > adapter->this_id )
  		tgt--;	/* we do not get inquires for initiator id */
  
  	ldrv_num = (channel * 15) + tgt;
  
  
  	/*
  	 * If we have a logical drive with boot enabled, project it first
  	 */
  	if( adapter->boot_ldrv_enabled ) {
  		if( ldrv_num == 0 ) {
  			ldrv_num = adapter->boot_ldrv;
  		}
  		else {
  			if( ldrv_num <= adapter->boot_ldrv ) {
  				ldrv_num--;
  			}
  		}
  	}
  
  	/*
  	 * If "delete logical drive" feature is enabled on this controller.
  	 * Do only if at least one delete logical drive operation was done.
  	 *
  	 * Also, after logical drive deletion, instead of logical drive number,
  	 * the value returned should be 0x80+logical drive id.
  	 *
  	 * These is valid only for IO commands.
  	 */
  
  	if (adapter->support_random_del && adapter->read_ldidmap )
  		switch (cmd->cmnd[0]) {
  		case READ_6:	/* fall through */
  		case WRITE_6:	/* fall through */
  		case READ_10:	/* fall through */
  		case WRITE_10:
  			ldrv_num += 0x80;
  		}
  
  	return ldrv_num;
  }
  
  /**
   * mega_build_cmd()
   * @adapter - pointer to our soft state
   * @cmd - Prepare using this scsi command
   * @busy - busy flag if no resources
   *
   * Prepares a command and scatter gather list for the controller. This routine
   * also finds out if the commands is intended for a logical drive or a
   * physical device and prepares the controller command accordingly.
   *
   * We also re-order the logical drives and physical devices based on their
   * boot settings.
   */
  static scb_t *
  mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
  {
  	mega_ext_passthru	*epthru;
  	mega_passthru	*pthru;
  	scb_t	*scb;
  	mbox_t	*mbox;
9d5d93e32   Dan Carpenter   [SCSI] megaraid: ...
515
  	u32	seg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
519
520
  	char	islogical;
  	int	max_ldrv_num;
  	int	channel = 0;
  	int	target = 0;
  	int	ldrv_num = 0;   /* logical drive number */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
  	/*
  	 * We know what channels our logical drives are on - mega_find_card()
  	 */
  	islogical = adapter->logdrv_chan[cmd->device->channel];
  
  	/*
  	 * The theory: If physical drive is chosen for boot, all the physical
  	 * devices are exported before the logical drives, otherwise physical
  	 * devices are pushed after logical drives, in which case - Kernel sees
  	 * the physical devices on virtual channel which is obviously converted
  	 * to actual channel on the HBA.
  	 */
  	if( adapter->boot_pdrv_enabled ) {
  		if( islogical ) {
  			/* logical channel */
  			channel = cmd->device->channel -
  				adapter->product_info.nchannels;
  		}
  		else {
  			/* this is physical channel */
  			channel = cmd->device->channel; 
  			target = cmd->device->id;
  
  			/*
  			 * boot from a physical disk, that disk needs to be
  			 * exposed first IF both the channels are SCSI, then
  			 * booting from the second channel is not allowed.
  			 */
  			if( target == 0 ) {
  				target = adapter->boot_pdrv_tgt;
  			}
  			else if( target == adapter->boot_pdrv_tgt ) {
  				target = 0;
  			}
  		}
  	}
  	else {
  		if( islogical ) {
  			/* this is the logical channel */
  			channel = cmd->device->channel;	
  		}
  		else {
  			/* physical channel */
  			channel = cmd->device->channel - NVIRT_CHAN;	
  			target = cmd->device->id;
  		}
  	}
  
  
  	if(islogical) {
  
  		/* have just LUN 0 for each target on virtual channels */
  		if (cmd->device->lun) {
  			cmd->result = (DID_BAD_TARGET << 16);
  			cmd->scsi_done(cmd);
  			return NULL;
  		}
  
  		ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
  
  
  		max_ldrv_num = (adapter->flag & BOARD_40LD) ?
  			MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
  
  		/*
  		 * max_ldrv_num increases by 0x80 if some logical drive was
  		 * deleted.
  		 */
  		if(adapter->read_ldidmap)
  			max_ldrv_num += 0x80;
  
  		if(ldrv_num > max_ldrv_num ) {
  			cmd->result = (DID_BAD_TARGET << 16);
  			cmd->scsi_done(cmd);
  			return NULL;
  		}
  
  	}
  	else {
  		if( cmd->device->lun > 7) {
  			/*
  			 * Do not support lun >7 for physically accessed
  			 * devices
  			 */
  			cmd->result = (DID_BAD_TARGET << 16);
  			cmd->scsi_done(cmd);
  			return NULL;
  		}
  	}
  
  	/*
  	 *
  	 * Logical drive commands
  	 *
  	 */
  	if(islogical) {
  		switch (cmd->cmnd[0]) {
  		case TEST_UNIT_READY:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
  #if MEGA_HAVE_CLUSTERING
  			/*
  			 * Do we support clustering and is the support enabled
  			 * If no, return success always
  			 */
  			if( !adapter->has_cluster ) {
  				cmd->result = (DID_OK << 16);
  				cmd->scsi_done(cmd);
  				return NULL;
  			}
  
  			if(!(scb = mega_allocate_scb(adapter, cmd))) {
  				*busy = 1;
  				return NULL;
  			}
  
  			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  			scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
  			scb->raw_mbox[3] = ldrv_num;
  
  			scb->dma_direction = PCI_DMA_NONE;
  
  			return scb;
  #else
  			cmd->result = (DID_OK << 16);
  			cmd->scsi_done(cmd);
  			return NULL;
  #endif
51c928c34   James Bottomley   [SCSI] Legacy Meg...
647
648
  		case MODE_SENSE: {
  			char *buf;
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
649
  			struct scatterlist *sg;
51c928c34   James Bottomley   [SCSI] Legacy Meg...
650

3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
651
  			sg = scsi_sglist(cmd);
77dfce076   Cong Wang   scsi: remove the ...
652
  			buf = kmap_atomic(sg_page(sg)) + sg->offset;
51c928c34   James Bottomley   [SCSI] Legacy Meg...
653

f0353301e   Mark Lord   [SCSI] Fix incorr...
654
  			memset(buf, 0, cmd->cmnd[4]);
77dfce076   Cong Wang   scsi: remove the ...
655
  			kunmap_atomic(buf - sg->offset);
51c928c34   James Bottomley   [SCSI] Legacy Meg...
656

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
  			cmd->result = (DID_OK << 16);
  			cmd->scsi_done(cmd);
  			return NULL;
51c928c34   James Bottomley   [SCSI] Legacy Meg...
660
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
  
  		case READ_CAPACITY:
  		case INQUIRY:
  
  			if(!(adapter->flag & (1L << cmd->device->channel))) {
  
  				printk(KERN_NOTICE
  					"scsi%d: scanning scsi channel %d ",
  						adapter->host->host_no,
  						cmd->device->channel);
  				printk("for logical drives.
  ");
  
  				adapter->flag |= (1L << cmd->device->channel);
  			}
  
  			/* Allocate a SCB and initialize passthru */
  			if(!(scb = mega_allocate_scb(adapter, cmd))) {
  				*busy = 1;
  				return NULL;
  			}
  			pthru = scb->pthru;
  
  			mbox = (mbox_t *)scb->raw_mbox;
  			memset(mbox, 0, sizeof(scb->raw_mbox));
  			memset(pthru, 0, sizeof(mega_passthru));
  
  			pthru->timeout = 0;
  			pthru->ars = 1;
  			pthru->reqsenselen = 14;
  			pthru->islogical = 1;
  			pthru->logdrv = ldrv_num;
  			pthru->cdblen = cmd->cmd_len;
  			memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  
  			if( adapter->has_64bit_addr ) {
  				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  			}
  			else {
  				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  			}
  
  			scb->dma_direction = PCI_DMA_FROMDEVICE;
  
  			pthru->numsgelements = mega_build_sglist(adapter, scb,
  				&pthru->dataxferaddr, &pthru->dataxferlen);
  
  			mbox->m_out.xferaddr = scb->pthru_dma_addr;
  
  			return scb;
  
  		case READ_6:
  		case WRITE_6:
  		case READ_10:
  		case WRITE_10:
  		case READ_12:
  		case WRITE_12:
  
  			/* Allocate a SCB and initialize mailbox */
  			if(!(scb = mega_allocate_scb(adapter, cmd))) {
  				*busy = 1;
  				return NULL;
  			}
  			mbox = (mbox_t *)scb->raw_mbox;
  
  			memset(mbox, 0, sizeof(scb->raw_mbox));
  			mbox->m_out.logdrv = ldrv_num;
  
  			/*
  			 * A little hack: 2nd bit is zero for all scsi read
  			 * commands and is set for all scsi write commands
  			 */
  			if( adapter->has_64bit_addr ) {
  				mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  					MEGA_MBOXCMD_LWRITE64:
  					MEGA_MBOXCMD_LREAD64 ;
  			}
  			else {
  				mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  					MEGA_MBOXCMD_LWRITE:
  					MEGA_MBOXCMD_LREAD ;
  			}
  
  			/*
  			 * 6-byte READ(0x08) or WRITE(0x0A) cdb
  			 */
  			if( cmd->cmd_len == 6 ) {
  				mbox->m_out.numsectors = (u32) cmd->cmnd[4];
  				mbox->m_out.lba =
  					((u32)cmd->cmnd[1] << 16) |
  					((u32)cmd->cmnd[2] << 8) |
  					(u32)cmd->cmnd[3];
  
  				mbox->m_out.lba &= 0x1FFFFF;
  
  #if MEGA_HAVE_STATS
  				/*
  				 * Take modulo 0x80, since the logical drive
  				 * number increases by 0x80 when a logical
  				 * drive was deleted
  				 */
  				if (*cmd->cmnd == READ_6) {
  					adapter->nreads[ldrv_num%0x80]++;
  					adapter->nreadblocks[ldrv_num%0x80] +=
  						mbox->m_out.numsectors;
  				} else {
  					adapter->nwrites[ldrv_num%0x80]++;
  					adapter->nwriteblocks[ldrv_num%0x80] +=
  						mbox->m_out.numsectors;
  				}
  #endif
  			}
  
  			/*
  			 * 10-byte READ(0x28) or WRITE(0x2A) cdb
  			 */
  			if( cmd->cmd_len == 10 ) {
  				mbox->m_out.numsectors =
  					(u32)cmd->cmnd[8] |
  					((u32)cmd->cmnd[7] << 8);
  				mbox->m_out.lba =
  					((u32)cmd->cmnd[2] << 24) |
  					((u32)cmd->cmnd[3] << 16) |
  					((u32)cmd->cmnd[4] << 8) |
  					(u32)cmd->cmnd[5];
  
  #if MEGA_HAVE_STATS
  				if (*cmd->cmnd == READ_10) {
  					adapter->nreads[ldrv_num%0x80]++;
  					adapter->nreadblocks[ldrv_num%0x80] +=
  						mbox->m_out.numsectors;
  				} else {
  					adapter->nwrites[ldrv_num%0x80]++;
  					adapter->nwriteblocks[ldrv_num%0x80] +=
  						mbox->m_out.numsectors;
  				}
  #endif
  			}
  
  			/*
  			 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  			 */
  			if( cmd->cmd_len == 12 ) {
  				mbox->m_out.lba =
  					((u32)cmd->cmnd[2] << 24) |
  					((u32)cmd->cmnd[3] << 16) |
  					((u32)cmd->cmnd[4] << 8) |
  					(u32)cmd->cmnd[5];
  
  				mbox->m_out.numsectors =
  					((u32)cmd->cmnd[6] << 24) |
  					((u32)cmd->cmnd[7] << 16) |
  					((u32)cmd->cmnd[8] << 8) |
  					(u32)cmd->cmnd[9];
  
  #if MEGA_HAVE_STATS
  				if (*cmd->cmnd == READ_12) {
  					adapter->nreads[ldrv_num%0x80]++;
  					adapter->nreadblocks[ldrv_num%0x80] +=
  						mbox->m_out.numsectors;
  				} else {
  					adapter->nwrites[ldrv_num%0x80]++;
  					adapter->nwriteblocks[ldrv_num%0x80] +=
  						mbox->m_out.numsectors;
  				}
  #endif
  			}
  
  			/*
  			 * If it is a read command
  			 */
  			if( (*cmd->cmnd & 0x0F) == 0x08 ) {
  				scb->dma_direction = PCI_DMA_FROMDEVICE;
  			}
  			else {
  				scb->dma_direction = PCI_DMA_TODEVICE;
  			}
  
  			/* Calculate Scatter-Gather info */
  			mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
9d5d93e32   Dan Carpenter   [SCSI] megaraid: ...
841
  					(u32 *)&mbox->m_out.xferaddr, &seg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
844
845
846
847
848
849
850
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
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
1060
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
1116
1117
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
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
  
  			return scb;
  
  #if MEGA_HAVE_CLUSTERING
  		case RESERVE:	/* Fall through */
  		case RELEASE:
  
  			/*
  			 * Do we support clustering and is the support enabled
  			 */
  			if( ! adapter->has_cluster ) {
  
  				cmd->result = (DID_BAD_TARGET << 16);
  				cmd->scsi_done(cmd);
  				return NULL;
  			}
  
  			/* Allocate a SCB and initialize mailbox */
  			if(!(scb = mega_allocate_scb(adapter, cmd))) {
  				*busy = 1;
  				return NULL;
  			}
  
  			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  			scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
  				MEGA_RESERVE_LD : MEGA_RELEASE_LD;
  
  			scb->raw_mbox[3] = ldrv_num;
  
  			scb->dma_direction = PCI_DMA_NONE;
  
  			return scb;
  #endif
  
  		default:
  			cmd->result = (DID_BAD_TARGET << 16);
  			cmd->scsi_done(cmd);
  			return NULL;
  		}
  	}
  
  	/*
  	 * Passthru drive commands
  	 */
  	else {
  		/* Allocate a SCB and initialize passthru */
  		if(!(scb = mega_allocate_scb(adapter, cmd))) {
  			*busy = 1;
  			return NULL;
  		}
  
  		mbox = (mbox_t *)scb->raw_mbox;
  		memset(mbox, 0, sizeof(scb->raw_mbox));
  
  		if( adapter->support_ext_cdb ) {
  
  			epthru = mega_prepare_extpassthru(adapter, scb, cmd,
  					channel, target);
  
  			mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
  
  			mbox->m_out.xferaddr = scb->epthru_dma_addr;
  
  		}
  		else {
  
  			pthru = mega_prepare_passthru(adapter, scb, cmd,
  					channel, target);
  
  			/* Initialize mailbox */
  			if( adapter->has_64bit_addr ) {
  				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  			}
  			else {
  				mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  			}
  
  			mbox->m_out.xferaddr = scb->pthru_dma_addr;
  
  		}
  		return scb;
  	}
  	return NULL;
  }
  
  
  /**
   * mega_prepare_passthru()
   * @adapter - pointer to our soft state
   * @scb - our scsi control block
   * @cmd - scsi command from the mid-layer
   * @channel - actual channel on the controller
   * @target - actual id on the controller.
   *
   * prepare a command for the scsi physical devices.
   */
  static mega_passthru *
  mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  		int channel, int target)
  {
  	mega_passthru *pthru;
  
  	pthru = scb->pthru;
  	memset(pthru, 0, sizeof (mega_passthru));
  
  	/* 0=6sec/1=60sec/2=10min/3=3hrs */
  	pthru->timeout = 2;
  
  	pthru->ars = 1;
  	pthru->reqsenselen = 14;
  	pthru->islogical = 0;
  
  	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  
  	pthru->target = (adapter->flag & BOARD_40LD) ?
  		(channel << 4) | target : target;
  
  	pthru->cdblen = cmd->cmd_len;
  	pthru->logdrv = cmd->device->lun;
  
  	memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  
  	/* Not sure about the direction */
  	scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  
  	/* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  	switch (cmd->cmnd[0]) {
  	case INQUIRY:
  	case READ_CAPACITY:
  		if(!(adapter->flag & (1L << cmd->device->channel))) {
  
  			printk(KERN_NOTICE
  				"scsi%d: scanning scsi channel %d [P%d] ",
  					adapter->host->host_no,
  					cmd->device->channel, channel);
  			printk("for physical devices.
  ");
  
  			adapter->flag |= (1L << cmd->device->channel);
  		}
  		/* Fall through */
  	default:
  		pthru->numsgelements = mega_build_sglist(adapter, scb,
  				&pthru->dataxferaddr, &pthru->dataxferlen);
  		break;
  	}
  	return pthru;
  }
  
  
  /**
   * mega_prepare_extpassthru()
   * @adapter - pointer to our soft state
   * @scb - our scsi control block
   * @cmd - scsi command from the mid-layer
   * @channel - actual channel on the controller
   * @target - actual id on the controller.
   *
   * prepare a command for the scsi physical devices. This rountine prepares
   * commands for devices which can take extended CDBs (>10 bytes)
   */
  static mega_ext_passthru *
  mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  		int channel, int target)
  {
  	mega_ext_passthru	*epthru;
  
  	epthru = scb->epthru;
  	memset(epthru, 0, sizeof(mega_ext_passthru));
  
  	/* 0=6sec/1=60sec/2=10min/3=3hrs */
  	epthru->timeout = 2;
  
  	epthru->ars = 1;
  	epthru->reqsenselen = 14;
  	epthru->islogical = 0;
  
  	epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  	epthru->target = (adapter->flag & BOARD_40LD) ?
  		(channel << 4) | target : target;
  
  	epthru->cdblen = cmd->cmd_len;
  	epthru->logdrv = cmd->device->lun;
  
  	memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
  
  	/* Not sure about the direction */
  	scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  
  	switch(cmd->cmnd[0]) {
  	case INQUIRY:
  	case READ_CAPACITY:
  		if(!(adapter->flag & (1L << cmd->device->channel))) {
  
  			printk(KERN_NOTICE
  				"scsi%d: scanning scsi channel %d [P%d] ",
  					adapter->host->host_no,
  					cmd->device->channel, channel);
  			printk("for physical devices.
  ");
  
  			adapter->flag |= (1L << cmd->device->channel);
  		}
  		/* Fall through */
  	default:
  		epthru->numsgelements = mega_build_sglist(adapter, scb,
  				&epthru->dataxferaddr, &epthru->dataxferlen);
  		break;
  	}
  
  	return epthru;
  }
  
  static void
  __mega_runpendq(adapter_t *adapter)
  {
  	scb_t *scb;
  	struct list_head *pos, *next;
  
  	/* Issue any pending commands to the card */
  	list_for_each_safe(pos, next, &adapter->pending_list) {
  
  		scb = list_entry(pos, scb_t, list);
  
  		if( !(scb->state & SCB_ISSUED) ) {
  
  			if( issue_scb(adapter, scb) != 0 )
  				return;
  		}
  	}
  
  	return;
  }
  
  
  /**
   * issue_scb()
   * @adapter - pointer to our soft state
   * @scb - scsi control block
   *
   * Post a command to the card if the mailbox is available, otherwise return
   * busy. We also take the scb from the pending list if the mailbox is
   * available.
   */
  static int
  issue_scb(adapter_t *adapter, scb_t *scb)
  {
  	volatile mbox64_t	*mbox64 = adapter->mbox64;
  	volatile mbox_t		*mbox = adapter->mbox;
  	unsigned int	i = 0;
  
  	if(unlikely(mbox->m_in.busy)) {
  		do {
  			udelay(1);
  			i++;
  		} while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
  
  		if(mbox->m_in.busy) return -1;
  	}
  
  	/* Copy mailbox data into host structure */
  	memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox, 
  			sizeof(struct mbox_out));
  
  	mbox->m_out.cmdid = scb->idx;	/* Set cmdid */
  	mbox->m_in.busy = 1;		/* Set busy */
  
  
  	/*
  	 * Increment the pending queue counter
  	 */
  	atomic_inc(&adapter->pend_cmds);
  
  	switch (mbox->m_out.cmd) {
  	case MEGA_MBOXCMD_LREAD64:
  	case MEGA_MBOXCMD_LWRITE64:
  	case MEGA_MBOXCMD_PASSTHRU64:
  	case MEGA_MBOXCMD_EXTPTHRU:
  		mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  		mbox64->xfer_segment_hi = 0;
  		mbox->m_out.xferaddr = 0xFFFFFFFF;
  		break;
  	default:
  		mbox64->xfer_segment_lo = 0;
  		mbox64->xfer_segment_hi = 0;
  	}
  
  	/*
  	 * post the command
  	 */
  	scb->state |= SCB_ISSUED;
  
  	if( likely(adapter->flag & BOARD_MEMMAP) ) {
  		mbox->m_in.poll = 0;
  		mbox->m_in.ack = 0;
  		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  	}
  	else {
  		irq_enable(adapter);
  		issue_command(adapter);
  	}
  
  	return 0;
  }
  
  /*
   * Wait until the controller's mailbox is available
   */
  static inline int
  mega_busywait_mbox (adapter_t *adapter)
  {
  	if (adapter->mbox->m_in.busy)
  		return __mega_busywait_mbox(adapter);
  	return 0;
  }
  
  /**
   * issue_scb_block()
   * @adapter - pointer to our soft state
   * @raw_mbox - the mailbox
   *
   * Issue a scb in synchronous and non-interrupt mode
   */
  static int
  issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
  {
  	volatile mbox64_t *mbox64 = adapter->mbox64;
  	volatile mbox_t *mbox = adapter->mbox;
  	u8	byte;
  
  	/* Wait until mailbox is free */
  	if(mega_busywait_mbox (adapter))
  		goto bug_blocked_mailbox;
  
  	/* Copy mailbox data into host structure */
  	memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
  	mbox->m_out.cmdid = 0xFE;
  	mbox->m_in.busy = 1;
  
  	switch (raw_mbox[0]) {
  	case MEGA_MBOXCMD_LREAD64:
  	case MEGA_MBOXCMD_LWRITE64:
  	case MEGA_MBOXCMD_PASSTHRU64:
  	case MEGA_MBOXCMD_EXTPTHRU:
  		mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  		mbox64->xfer_segment_hi = 0;
  		mbox->m_out.xferaddr = 0xFFFFFFFF;
  		break;
  	default:
  		mbox64->xfer_segment_lo = 0;
  		mbox64->xfer_segment_hi = 0;
  	}
  
  	if( likely(adapter->flag & BOARD_MEMMAP) ) {
  		mbox->m_in.poll = 0;
  		mbox->m_in.ack = 0;
  		mbox->m_in.numstatus = 0xFF;
  		mbox->m_in.status = 0xFF;
  		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  
  		while((volatile u8)mbox->m_in.numstatus == 0xFF)
  			cpu_relax();
  
  		mbox->m_in.numstatus = 0xFF;
  
  		while( (volatile u8)mbox->m_in.poll != 0x77 )
  			cpu_relax();
  
  		mbox->m_in.poll = 0;
  		mbox->m_in.ack = 0x77;
  
  		WRINDOOR(adapter, adapter->mbox_dma | 0x2);
  
  		while(RDINDOOR(adapter) & 0x2)
  			cpu_relax();
  	}
  	else {
  		irq_disable(adapter);
  		issue_command(adapter);
  
  		while (!((byte = irq_state(adapter)) & INTR_VALID))
  			cpu_relax();
  
  		set_irq_state(adapter, byte);
  		irq_enable(adapter);
  		irq_ack(adapter);
  	}
  
  	return mbox->m_in.status;
  
  bug_blocked_mailbox:
  	printk(KERN_WARNING "megaraid: Blocked mailbox......!!
  ");
  	udelay (1000);
  	return -1;
  }
  
  
  /**
   * megaraid_isr_iomapped()
   * @irq - irq
   * @devp - pointer to our soft state
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1244
1245
1246
1247
1248
1249
   *
   * Interrupt service routine for io-mapped controllers.
   * Find out if our device is interrupting. If yes, acknowledge the interrupt
   * and service the completed commands.
   */
  static irqreturn_t
7d12e780e   David Howells   IRQ: Maintain reg...
1250
  megaraid_isr_iomapped(int irq, void *devp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
  {
  	adapter_t	*adapter = devp;
  	unsigned long	flags;
  	u8	status;
  	u8	nstatus;
  	u8	completed[MAX_FIRMWARE_STATUS];
  	u8	byte;
  	int	handled = 0;
  
  
  	/*
  	 * loop till F/W has more commands for us to complete.
  	 */
  	spin_lock_irqsave(&adapter->lock, flags);
  
  	do {
  		/* Check if a valid interrupt is pending */
  		byte = irq_state(adapter);
  		if( (byte & VALID_INTR_BYTE) == 0 ) {
  			/*
  			 * No more pending commands
  			 */
  			goto out_unlock;
  		}
  		set_irq_state(adapter, byte);
  
  		while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  				== 0xFF)
  			cpu_relax();
  		adapter->mbox->m_in.numstatus = 0xFF;
  
  		status = adapter->mbox->m_in.status;
  
  		/*
  		 * decrement the pending queue counter
  		 */
  		atomic_sub(nstatus, &adapter->pend_cmds);
  
  		memcpy(completed, (void *)adapter->mbox->m_in.completed, 
  				nstatus);
  
  		/* Acknowledge interrupt */
  		irq_ack(adapter);
  
  		mega_cmd_done(adapter, completed, nstatus, status);
  
  		mega_rundoneq(adapter);
  
  		handled = 1;
  
  		/* Loop through any pending requests */
  		if(atomic_read(&adapter->quiescent) == 0) {
  			mega_runpendq(adapter);
  		}
  
  	} while(1);
  
   out_unlock:
  
  	spin_unlock_irqrestore(&adapter->lock, flags);
  
  	return IRQ_RETVAL(handled);
  }
  
  
  /**
   * megaraid_isr_memmapped()
   * @irq - irq
   * @devp - pointer to our soft state
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1320
1321
1322
1323
1324
1325
   *
   * Interrupt service routine for memory-mapped controllers.
   * Find out if our device is interrupting. If yes, acknowledge the interrupt
   * and service the completed commands.
   */
  static irqreturn_t
7d12e780e   David Howells   IRQ: Maintain reg...
1326
  megaraid_isr_memmapped(int irq, void *devp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1327
1328
1329
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
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
  {
  	adapter_t	*adapter = devp;
  	unsigned long	flags;
  	u8	status;
  	u32	dword = 0;
  	u8	nstatus;
  	u8	completed[MAX_FIRMWARE_STATUS];
  	int	handled = 0;
  
  
  	/*
  	 * loop till F/W has more commands for us to complete.
  	 */
  	spin_lock_irqsave(&adapter->lock, flags);
  
  	do {
  		/* Check if a valid interrupt is pending */
  		dword = RDOUTDOOR(adapter);
  		if(dword != 0x10001234) {
  			/*
  			 * No more pending commands
  			 */
  			goto out_unlock;
  		}
  		WROUTDOOR(adapter, 0x10001234);
  
  		while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  				== 0xFF) {
  			cpu_relax();
  		}
  		adapter->mbox->m_in.numstatus = 0xFF;
  
  		status = adapter->mbox->m_in.status;
  
  		/*
  		 * decrement the pending queue counter
  		 */
  		atomic_sub(nstatus, &adapter->pend_cmds);
  
  		memcpy(completed, (void *)adapter->mbox->m_in.completed, 
  				nstatus);
  
  		/* Acknowledge interrupt */
  		WRINDOOR(adapter, 0x2);
  
  		handled = 1;
00769ec40   Jeff Garzik   [SCSI] megaraid: ...
1373
1374
  		while( RDINDOOR(adapter) & 0x02 )
  			cpu_relax();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
  
  		mega_cmd_done(adapter, completed, nstatus, status);
  
  		mega_rundoneq(adapter);
  
  		/* Loop through any pending requests */
  		if(atomic_read(&adapter->quiescent) == 0) {
  			mega_runpendq(adapter);
  		}
  
  	} while(1);
  
   out_unlock:
  
  	spin_unlock_irqrestore(&adapter->lock, flags);
  
  	return IRQ_RETVAL(handled);
  }
  /**
   * mega_cmd_done()
   * @adapter - pointer to our soft state
   * @completed - array of ids of completed commands
   * @nstatus - number of completed commands
   * @status - status of the last command completed
   *
8e572bab3   Justin P. Mattock   fix typos 'comamn...
1400
   * Complete the commands and call the scsi mid-layer callback hooks.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
   */
  static void
  mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
  {
  	mega_ext_passthru	*epthru = NULL;
  	struct scatterlist	*sgl;
  	Scsi_Cmnd	*cmd = NULL;
  	mega_passthru	*pthru = NULL;
  	mbox_t	*mbox = NULL;
  	u8	c;
  	scb_t	*scb;
  	int	islogical;
  	int	cmdid;
  	int	i;
  
  	/*
  	 * for all the commands completed, call the mid-layer callback routine
  	 * and free the scb.
  	 */
  	for( i = 0; i < nstatus; i++ ) {
  
  		cmdid = completed[i];
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
1423
1424
1425
1426
1427
1428
1429
1430
  		/*
  		 * Only free SCBs for the commands coming down from the
  		 * mid-layer, not for which were issued internally
  		 *
  		 * For internal command, restore the status returned by the
  		 * firmware so that user can interpret it.
  		 */
  		if (cmdid == CMDID_INT_CMDS) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1431
  			scb = &adapter->int_scb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1432

0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
1433
1434
  			list_del_init(&scb->list);
  			scb->state = SCB_FREE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1435

0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
1436
1437
1438
  			adapter->int_status = status;
  			complete(&adapter->int_waitq);
  		} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
  			scb = &adapter->scb_list[cmdid];
  
  			/*
  			 * Make sure f/w has completed a valid command
  			 */
  			if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
  				printk(KERN_CRIT
  					"megaraid: invalid command ");
  				printk("Id %d, scb->state:%x, scsi cmd:%p
  ",
  					cmdid, scb->state, scb->cmd);
  
  				continue;
  			}
  
  			/*
  			 * Was a abort issued for this command
  			 */
  			if( scb->state & SCB_ABORT ) {
  
  				printk(KERN_WARNING
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1460
1461
1462
  				"megaraid: aborted cmd [%x] complete.
  ",
  					scb->idx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
  
  				scb->cmd->result = (DID_ABORT << 16);
  
  				list_add_tail(SCSI_LIST(scb->cmd),
  						&adapter->completed_list);
  
  				mega_free_scb(adapter, scb);
  
  				continue;
  			}
  
  			/*
  			 * Was a reset issued for this command
  			 */
  			if( scb->state & SCB_RESET ) {
  
  				printk(KERN_WARNING
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1480
1481
1482
  				"megaraid: reset cmd [%x] complete.
  ",
  					scb->idx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
  
  				scb->cmd->result = (DID_RESET << 16);
  
  				list_add_tail(SCSI_LIST(scb->cmd),
  						&adapter->completed_list);
  
  				mega_free_scb (adapter, scb);
  
  				continue;
  			}
  
  			cmd = scb->cmd;
  			pthru = scb->pthru;
  			epthru = scb->epthru;
  			mbox = (mbox_t *)scb->raw_mbox;
  
  #if MEGA_HAVE_STATS
  			{
  
  			int	logdrv = mbox->m_out.logdrv;
  
  			islogical = adapter->logdrv_chan[cmd->channel];
  			/*
  			 * Maintain an error counter for the logical drive.
  			 * Some application like SNMP agent need such
  			 * statistics
  			 */
  			if( status && islogical && (cmd->cmnd[0] == READ_6 ||
  						cmd->cmnd[0] == READ_10 ||
  						cmd->cmnd[0] == READ_12)) {
  				/*
  				 * Logical drive number increases by 0x80 when
  				 * a logical drive is deleted
  				 */
  				adapter->rd_errors[logdrv%0x80]++;
  			}
  
  			if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
  						cmd->cmnd[0] == WRITE_10 ||
  						cmd->cmnd[0] == WRITE_12)) {
  				/*
  				 * Logical drive number increases by 0x80 when
  				 * a logical drive is deleted
  				 */
  				adapter->wr_errors[logdrv%0x80]++;
  			}
  
  			}
  #endif
  		}
  
  		/*
  		 * Do not return the presence of hard disk on the channel so,
  		 * inquiry sent, and returned data==hard disk or removable
  		 * hard disk and not logical, request should return failure! -
  		 * PJ
  		 */
  		islogical = adapter->logdrv_chan[cmd->device->channel];
  		if( cmd->cmnd[0] == INQUIRY && !islogical ) {
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1542
  			sgl = scsi_sglist(cmd);
45711f1af   Jens Axboe   [SG] Update drive...
1543
1544
  			if( sg_page(sgl) ) {
  				c = *(unsigned char *) sg_virt(&sgl[0]);
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1545
1546
1547
1548
1549
  			} else {
  				printk(KERN_WARNING
  				       "megaraid: invalid sg.
  ");
  				c = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
  			}
  
  			if(IS_RAID_CH(adapter, cmd->device->channel) &&
  					((c & 0x1F ) == TYPE_DISK)) {
  				status = 0xF0;
  			}
  		}
  
  		/* clear result; otherwise, success returns corrupt value */
  		cmd->result = 0;
  
  		/* Convert MegaRAID status to Linux error code */
  		switch (status) {
  		case 0x00:	/* SUCCESS , i.e. SCSI_STATUS_GOOD */
  			cmd->result |= (DID_OK << 16);
  			break;
  
  		case 0x02:	/* ERROR_ABORTED, i.e.
  				   SCSI_STATUS_CHECK_CONDITION */
  
  			/* set sense_buffer and result fields */
  			if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  				mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  
  				memcpy(cmd->sense_buffer, pthru->reqsensearea,
  						14);
  
  				cmd->result = (DRIVER_SENSE << 24) |
  					(DID_OK << 16) |
  					(CHECK_CONDITION << 1);
  			}
  			else {
  				if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  
  					memcpy(cmd->sense_buffer,
  						epthru->reqsensearea, 14);
  
  					cmd->result = (DRIVER_SENSE << 24) |
  						(DID_OK << 16) |
  						(CHECK_CONDITION << 1);
  				} else {
  					cmd->sense_buffer[0] = 0x70;
  					cmd->sense_buffer[2] = ABORTED_COMMAND;
  					cmd->result |= (CHECK_CONDITION << 1);
  				}
  			}
  			break;
  
  		case 0x08:	/* ERR_DEST_DRIVE_FAILED, i.e.
  				   SCSI_STATUS_BUSY */
  			cmd->result |= (DID_BUS_BUSY << 16) | status;
  			break;
  
  		default:
  #if MEGA_HAVE_CLUSTERING
  			/*
  			 * If TEST_UNIT_READY fails, we know
  			 * MEGA_RESERVATION_STATUS failed
  			 */
  			if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  				cmd->result |= (DID_ERROR << 16) |
  					(RESERVATION_CONFLICT << 1);
  			}
  			else
  			/*
  			 * Error code returned is 1 if Reserve or Release
  			 * failed or the input parameter is invalid
  			 */
  			if( status == 1 &&
  				(cmd->cmnd[0] == RESERVE ||
  					 cmd->cmnd[0] == RELEASE) ) {
  
  				cmd->result |= (DID_ERROR << 16) |
  					(RESERVATION_CONFLICT << 1);
  			}
  			else
  #endif
  				cmd->result |= (DID_BAD_TARGET << 16)|status;
  		}
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
1629
  		mega_free_scb(adapter, scb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
  
  		/* Add Scsi_Command to end of completed queue */
  		list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  	}
  }
  
  
  /*
   * mega_runpendq()
   *
   * Run through the list of completed requests and finish it
   */
  static void
  mega_rundoneq (adapter_t *adapter)
  {
  	Scsi_Cmnd *cmd;
  	struct list_head *pos;
  
  	list_for_each(pos, &adapter->completed_list) {
0a04137e7   Christoph Hellwig   [SCSI] remove Scs...
1649
  		struct scsi_pointer* spos = (struct scsi_pointer *)pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
  
  		cmd = list_entry(spos, Scsi_Cmnd, SCp);
  		cmd->scsi_done(cmd);
  	}
  
  	INIT_LIST_HEAD(&adapter->completed_list);
  }
  
  
  /*
   * Free a SCB structure
   * Note: We assume the scsi commands associated with this scb is not free yet.
   */
  static void
  mega_free_scb(adapter_t *adapter, scb_t *scb)
  {
  	switch( scb->dma_type ) {
  
  	case MEGA_DMA_TYPE_NONE:
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1670
  	case MEGA_SGLIST:
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1671
  		scsi_dma_unmap(scb->cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1672
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
  	default:
  		break;
  	}
  
  	/*
  	 * Remove from the pending list
  	 */
  	list_del_init(&scb->list);
  
  	/* Link the scb back into free list */
  	scb->state = SCB_FREE;
  	scb->cmd = NULL;
  
  	list_add(&scb->list, &adapter->free_list);
  }
  
  
  static int
  __mega_busywait_mbox (adapter_t *adapter)
  {
  	volatile mbox_t *mbox = adapter->mbox;
  	long counter;
  
  	for (counter = 0; counter < 10000; counter++) {
  		if (!mbox->m_in.busy)
  			return 0;
e1fa0ceaf   Amol Lad   [SCSI] megaraid: ...
1699
1700
  		udelay(100);
  		cond_resched();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
  	}
  	return -1;		/* give up after 1 second */
  }
  
  /*
   * Copies data to SGLIST
   * Note: For 64 bit cards, we need a minimum of one SG element for read/write
   */
  static int
  mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  {
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1712
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1713
1714
1715
1716
1717
  	Scsi_Cmnd	*cmd;
  	int	sgcnt;
  	int	idx;
  
  	cmd = scb->cmd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1718
1719
1720
1721
1722
  	/*
  	 * Copy Scatter-Gather list info into controller structure.
  	 *
  	 * The number of sg elements returned must not exceed our limit
  	 */
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1723
  	sgcnt = scsi_dma_map(cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1724
1725
  
  	scb->dma_type = MEGA_SGLIST;
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1726
  	BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1727

51c928c34   James Bottomley   [SCSI] Legacy Meg...
1728
  	*len = 0;
d5e89385e   FUJITA Tomonori   [SCSI] megaraid_o...
1729
1730
1731
1732
1733
1734
1735
  	if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
  		sg = scsi_sglist(cmd);
  		scb->dma_h_bulkdata = sg_dma_address(sg);
  		*buf = (u32)scb->dma_h_bulkdata;
  		*len = sg_dma_len(sg);
  		return 0;
  	}
3f6270ef7   FUJITA Tomonori   [SCSI] megaraid_o...
1736
1737
1738
1739
1740
1741
1742
  	scsi_for_each_sg(cmd, sg, sgcnt, idx) {
  		if (adapter->has_64bit_addr) {
  			scb->sgl64[idx].address = sg_dma_address(sg);
  			*len += scb->sgl64[idx].length = sg_dma_len(sg);
  		} else {
  			scb->sgl[idx].address = sg_dma_address(sg);
  			*len += scb->sgl[idx].length = sg_dma_len(sg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1743
1744
1745
1746
1747
  		}
  	}
  
  	/* Reset pointer and length fields */
  	*buf = scb->sgl_dma_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
  	/* Return count of SG requests */
  	return sgcnt;
  }
  
  
  /*
   * mega_8_to_40ld()
   *
   * takes all info in AdapterInquiry structure and puts it into ProductInfo and
   * Enquiry3 structures for later use
   */
  static void
  mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  		mega_product_info *product_info)
  {
  	int i;
  
  	product_info->max_commands = inquiry->adapter_info.max_commands;
  	enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  	product_info->nchannels = inquiry->adapter_info.nchannels;
  
  	for (i = 0; i < 4; i++) {
  		product_info->fw_version[i] =
  			inquiry->adapter_info.fw_version[i];
  
  		product_info->bios_version[i] =
  			inquiry->adapter_info.bios_version[i];
  	}
  	enquiry3->cache_flush_interval =
  		inquiry->adapter_info.cache_flush_interval;
  
  	product_info->dram_size = inquiry->adapter_info.dram_size;
  
  	enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  
  	for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  		enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  		enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  		enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  	}
  
  	for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  		enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  }
  
  static inline void
  mega_free_sgl(adapter_t *adapter)
  {
  	scb_t	*scb;
  	int	i;
  
  	for(i = 0; i < adapter->max_cmds; i++) {
  
  		scb = &adapter->scb_list[i];
  
  		if( scb->sgl64 ) {
  			pci_free_consistent(adapter->dev,
  				sizeof(mega_sgl64) * adapter->sglen,
  				scb->sgl64,
  				scb->sgl_dma_addr);
  
  			scb->sgl64 = NULL;
  		}
  
  		if( scb->pthru ) {
  			pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  				scb->pthru, scb->pthru_dma_addr);
  
  			scb->pthru = NULL;
  		}
  
  		if( scb->epthru ) {
  			pci_free_consistent(adapter->dev,
  				sizeof(mega_ext_passthru),
  				scb->epthru, scb->epthru_dma_addr);
  
  			scb->epthru = NULL;
  		}
  
  	}
  }
  
  
  /*
   * Get information about the card/driver
   */
  const char *
  megaraid_info(struct Scsi_Host *host)
  {
  	static char buffer[512];
  	adapter_t *adapter;
  
  	adapter = (adapter_t *)host->hostdata;
  
  	sprintf (buffer,
  		 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  		 adapter->fw_version, adapter->product_info.max_commands,
  		 adapter->host->max_id, adapter->host->max_channel,
1abf635d2   Hannes Reinecke   scsi: use 64-bit ...
1846
  		 (u32)adapter->host->max_lun);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
  	return buffer;
  }
  
  /*
   * Abort a previous SCSI request. Only commands on the pending list can be
   * aborted. All the commands issued to the F/W must complete.
   */
  static int
  megaraid_abort(Scsi_Cmnd *cmd)
  {
  	adapter_t	*adapter;
  	int		rval;
  
  	adapter = (adapter_t *)cmd->device->host->hostdata;
  
  	rval =  megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  
  	/*
  	 * This is required here to complete any completed requests
  	 * to be communicated over to the mid layer.
  	 */
  	mega_rundoneq(adapter);
  
  	return rval;
  }
  
  
  static int
fa4c49669   James Bottomley   [SCSI] megaraid:...
1875
  megaraid_reset(struct scsi_cmnd *cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
  {
  	adapter_t	*adapter;
  	megacmd_t	mc;
  	int		rval;
  
  	adapter = (adapter_t *)cmd->device->host->hostdata;
  
  #if MEGA_HAVE_CLUSTERING
  	mc.cmd = MEGA_CLUSTER_CMD;
  	mc.opcode = MEGA_RESET_RESERVATIONS;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
1886
  	if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1887
1888
1889
1890
1891
1892
1893
1894
  		printk(KERN_WARNING
  				"megaraid: reservation reset failed.
  ");
  	}
  	else {
  		printk(KERN_INFO "megaraid: reservation reset.
  ");
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1895
  #endif
fa4c49669   James Bottomley   [SCSI] megaraid:...
1896
  	spin_lock_irq(&adapter->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1897
1898
1899
1900
1901
1902
1903
  	rval =  megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  
  	/*
  	 * This is required here to complete any completed requests
  	 * to be communicated over to the mid layer.
  	 */
  	mega_rundoneq(adapter);
94d0e7b80   Jeff Garzik   [SCSI] allow slee...
1904
  	spin_unlock_irq(&adapter->lock);
fa4c49669   James Bottomley   [SCSI] megaraid:...
1905
  	return rval;
94d0e7b80   Jeff Garzik   [SCSI] allow slee...
1906
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1907

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
  /**
   * megaraid_abort_and_reset()
   * @adapter - megaraid soft state
   * @cmd - scsi command to be aborted or reset
   * @aor - abort or reset flag
   *
   * Try to locate the scsi command in the pending queue. If found and is not
   * issued to the controller, abort/reset it. Otherwise return failure
   */
  static int
  megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  {
  	struct list_head	*pos, *next;
  	scb_t			*scb;
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1922
1923
1924
  	printk(KERN_WARNING "megaraid: %s cmd=%x <c=%d t=%d l=%d>
  ",
  	     (aor == SCB_ABORT)? "ABORTING":"RESET",
9cb78c16f   Hannes Reinecke   scsi: use 64-bit ...
1925
1926
  	     cmd->cmnd[0], cmd->device->channel,
  	     cmd->device->id, (u32)cmd->device->lun);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1927
1928
  
  	if(list_empty(&adapter->pending_list))
b6c92b7e0   Hannes Reinecke   scsi: correct ret...
1929
  		return FAILED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
  
  	list_for_each_safe(pos, next, &adapter->pending_list) {
  
  		scb = list_entry(pos, scb_t, list);
  
  		if (scb->cmd == cmd) { /* Found command */
  
  			scb->state |= aor;
  
  			/*
d41ad9387   Nick Andrew   trivial: Fix miss...
1940
1941
  			 * Check if this command has firmware ownership. If
  			 * yes, we cannot reset this command. Whenever f/w
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1942
1943
1944
1945
1946
1947
  			 * completes this command, we will return appropriate
  			 * status from ISR.
  			 */
  			if( scb->state & SCB_ISSUED ) {
  
  				printk(KERN_WARNING
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1948
1949
  					"megaraid: %s[%x], fw owner.
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1950
  					(aor==SCB_ABORT) ? "ABORTING":"RESET",
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1951
  					scb->idx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1952

b6c92b7e0   Hannes Reinecke   scsi: correct ret...
1953
  				return FAILED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1954
1955
1956
1957
1958
1959
1960
1961
  			}
  			else {
  
  				/*
  				 * Not yet issued! Remove from the pending
  				 * list
  				 */
  				printk(KERN_WARNING
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1962
1963
  					"megaraid: %s-[%x], driver owner.
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1964
  					(aor==SCB_ABORT) ? "ABORTING":"RESET",
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
1965
  					scb->idx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
  
  				mega_free_scb(adapter, scb);
  
  				if( aor == SCB_ABORT ) {
  					cmd->result = (DID_ABORT << 16);
  				}
  				else {
  					cmd->result = (DID_RESET << 16);
  				}
  
  				list_add_tail(SCSI_LIST(cmd),
  						&adapter->completed_list);
b6c92b7e0   Hannes Reinecke   scsi: correct ret...
1978
  				return SUCCESS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1979
1980
1981
  			}
  		}
  	}
b6c92b7e0   Hannes Reinecke   scsi: correct ret...
1982
  	return FAILED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1983
1984
1985
1986
1987
  }
  
  static inline int
  make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  {
8b1fce04d   Gu Zheng   PCI: Convert allo...
1988
  	*pdev = pci_alloc_dev(NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1989
1990
1991
1992
  
  	if( *pdev == NULL ) return -1;
  
  	memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
284901a90   Yang Hongyang   dma-mapping: repl...
1993
  	if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
  		kfree(*pdev);
  		return -1;
  	}
  
  	return 0;
  }
  
  static inline void
  free_local_pdev(struct pci_dev *pdev)
  {
  	kfree(pdev);
  }
  
  /**
   * mega_allocate_inquiry()
   * @dma_handle - handle returned for dma address
   * @pdev - handle to pci device
   *
   * allocates memory for inquiry structure
   */
  static inline void *
  mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  {
  	return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  }
  
  
  static inline void
  mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  {
  	pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  }
  
  
  #ifdef CONFIG_PROC_FS
  /* Following code handles /proc fs  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2030
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2031
2032
2033
   * proc_show_config()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2034
2035
2036
2037
   *
   * Display configuration information about the controller.
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2038
  proc_show_config(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2039
  {
c7f079ca3   David Howells   megaraid: Don't u...
2040
  	adapter_t *adapter = m->private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2041

c7f079ca3   David Howells   megaraid: Don't u...
2042
  	seq_puts(m, MEGARAID_VERSION);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2043
  	if(adapter->product_info.product_name[0])
c7f079ca3   David Howells   megaraid: Don't u...
2044
2045
  		seq_printf(m, "%s
  ", adapter->product_info.product_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2046

c7f079ca3   David Howells   megaraid: Don't u...
2047
  	seq_puts(m, "Controller Type: ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2048

c7f079ca3   David Howells   megaraid: Don't u...
2049
2050
2051
2052
2053
2054
  	if( adapter->flag & BOARD_MEMMAP )
  		seq_puts(m, "438/466/467/471/493/518/520/531/532
  ");
  	else
  		seq_puts(m, "418/428/434
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2055

c7f079ca3   David Howells   megaraid: Don't u...
2056
2057
2058
  	if(adapter->flag & BOARD_40LD)
  		seq_puts(m, "Controller Supports 40 Logical Drives
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2059

c7f079ca3   David Howells   megaraid: Don't u...
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
  	if(adapter->flag & BOARD_64BIT)
  		seq_puts(m, "Controller capable of 64-bit memory addressing
  ");
  	if( adapter->has_64bit_addr )
  		seq_puts(m, "Controller using 64-bit memory addressing
  ");
  	else
  		seq_puts(m, "Controller is not using 64-bit memory addressing
  ");
  
  	seq_printf(m, "Base = %08lx, Irq = %d, ",
  		   adapter->base, adapter->host->irq);
  
  	seq_printf(m, "Logical Drives = %d, Channels = %d
  ",
  		   adapter->numldrv, adapter->product_info.nchannels);
  
  	seq_printf(m, "Version =%s:%s, DRAM = %dMb
  ",
  		   adapter->fw_version, adapter->bios_version,
  		   adapter->product_info.dram_size);
  
  	seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d
  ",
  		   adapter->product_info.max_commands, adapter->max_cmds);
  
  	seq_printf(m, "support_ext_cdb    = %d
  ", adapter->support_ext_cdb);
  	seq_printf(m, "support_random_del = %d
  ", adapter->support_random_del);
  	seq_printf(m, "boot_ldrv_enabled  = %d
  ", adapter->boot_ldrv_enabled);
  	seq_printf(m, "boot_ldrv          = %d
  ", adapter->boot_ldrv);
  	seq_printf(m, "boot_pdrv_enabled  = %d
  ", adapter->boot_pdrv_enabled);
  	seq_printf(m, "boot_pdrv_ch       = %d
  ", adapter->boot_pdrv_ch);
  	seq_printf(m, "boot_pdrv_tgt      = %d
  ", adapter->boot_pdrv_tgt);
  	seq_printf(m, "quiescent          = %d
  ",
  		   atomic_read(&adapter->quiescent));
  	seq_printf(m, "has_cluster        = %d
  ", adapter->has_cluster);
  
  	seq_puts(m, "
  Module Parameters:
  ");
  	seq_printf(m, "max_cmd_per_lun    = %d
  ", max_cmd_per_lun);
  	seq_printf(m, "max_sectors_per_io = %d
  ", max_sectors_per_io);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2114
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2115
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2116
2117
2118
   * proc_show_stat()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2119
   *
c7f079ca3   David Howells   megaraid: Don't u...
2120
   * Display statistical information about the I/O activity.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2121
2122
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2123
  proc_show_stat(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2124
  {
c7f079ca3   David Howells   megaraid: Don't u...
2125
2126
  	adapter_t *adapter = m->private;
  #if MEGA_HAVE_STATS
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2127
  	int	i;
c7f079ca3   David Howells   megaraid: Don't u...
2128
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2129

c7f079ca3   David Howells   megaraid: Don't u...
2130
2131
2132
2133
  	seq_puts(m, "Statistical Information for this controller
  ");
  	seq_printf(m, "pend_cmds = %d
  ", atomic_read(&adapter->pend_cmds));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2134
2135
  #if MEGA_HAVE_STATS
  	for(i = 0; i < adapter->numldrv; i++) {
c7f079ca3   David Howells   megaraid: Don't u...
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
  		seq_printf(m, "Logical Drive %d:
  ", i);
  		seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu
  ",
  			   adapter->nreads[i], adapter->nwrites[i]);
  		seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu
  ",
  			   adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  		seq_printf(m, "\tRead errors = %lu, Write errors = %lu
  
  ",
  			   adapter->rd_errors[i], adapter->wr_errors[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2148
2149
  	}
  #else
c7f079ca3   David Howells   megaraid: Don't u...
2150
2151
  	seq_puts(m, "IO and error counters not compiled in driver.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2152
  #endif
c7f079ca3   David Howells   megaraid: Don't u...
2153
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2154
2155
2156
2157
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2158
2159
2160
   * proc_show_mbox()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2161
2162
2163
2164
2165
   *
   * Display mailbox information for the last command issued. This information
   * is good for debugging.
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2166
  proc_show_mbox(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2167
  {
c7f079ca3   David Howells   megaraid: Don't u...
2168
  	adapter_t	*adapter = m->private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2169
  	volatile mbox_t	*mbox = adapter->mbox;
c7f079ca3   David Howells   megaraid: Don't u...
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
  
  	seq_puts(m, "Contents of Mail Box Structure
  ");
  	seq_printf(m, "  Fw Command   = 0x%02x
  ", mbox->m_out.cmd);
  	seq_printf(m, "  Cmd Sequence = 0x%02x
  ", mbox->m_out.cmdid);
  	seq_printf(m, "  No of Sectors= %04d
  ", mbox->m_out.numsectors);
  	seq_printf(m, "  LBA          = 0x%02x
  ", mbox->m_out.lba);
  	seq_printf(m, "  DTA          = 0x%08x
  ", mbox->m_out.xferaddr);
  	seq_printf(m, "  Logical Drive= 0x%02x
  ", mbox->m_out.logdrv);
  	seq_printf(m, "  No of SG Elmt= 0x%02x
  ", mbox->m_out.numsgelements);
  	seq_printf(m, "  Busy         = %01x
  ", mbox->m_in.busy);
  	seq_printf(m, "  Status       = 0x%02x
  ", mbox->m_in.status);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2192
2193
2194
2195
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2196
2197
2198
   * proc_show_rebuild_rate()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2199
2200
2201
2202
   *
   * Display current rebuild rate
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2203
  proc_show_rebuild_rate(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2204
  {
c7f079ca3   David Howells   megaraid: Don't u...
2205
  	adapter_t	*adapter = m->private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2206
2207
2208
  	dma_addr_t	dma_handle;
  	caddr_t		inquiry;
  	struct pci_dev	*pdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2209

c7f079ca3   David Howells   megaraid: Don't u...
2210
2211
  	if( make_local_pdev(adapter, &pdev) != 0 )
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2212

c7f079ca3   David Howells   megaraid: Don't u...
2213
2214
  	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
  		goto free_pdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2215
2216
  
  	if( mega_adapinq(adapter, dma_handle) != 0 ) {
c7f079ca3   David Howells   megaraid: Don't u...
2217
2218
  		seq_puts(m, "Adapter inquiry failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2219
2220
  		printk(KERN_WARNING "megaraid: inquiry failed.
  ");
c7f079ca3   David Howells   megaraid: Don't u...
2221
  		goto free_inquiry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2222
  	}
c7f079ca3   David Howells   megaraid: Don't u...
2223
2224
2225
2226
2227
2228
2229
  	if( adapter->flag & BOARD_40LD )
  		seq_printf(m, "Rebuild Rate: [%d%%]
  ",
  			   ((mega_inquiry3 *)inquiry)->rebuild_rate);
  	else
  		seq_printf(m, "Rebuild Rate: [%d%%]
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2230
  			((mraid_ext_inquiry *)
c7f079ca3   David Howells   megaraid: Don't u...
2231
  			 inquiry)->raid_inq.adapter_info.rebuild_rate);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2232

c7f079ca3   David Howells   megaraid: Don't u...
2233
  free_inquiry:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2234
  	mega_free_inquiry(inquiry, dma_handle, pdev);
c7f079ca3   David Howells   megaraid: Don't u...
2235
  free_pdev:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2236
  	free_local_pdev(pdev);
c7f079ca3   David Howells   megaraid: Don't u...
2237
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2238
2239
2240
2241
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2242
2243
2244
   * proc_show_battery()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2245
2246
2247
2248
   *
   * Display information about the battery module on the controller.
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2249
  proc_show_battery(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2250
  {
c7f079ca3   David Howells   megaraid: Don't u...
2251
  	adapter_t	*adapter = m->private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2252
2253
2254
  	dma_addr_t	dma_handle;
  	caddr_t		inquiry;
  	struct pci_dev	*pdev;
c7f079ca3   David Howells   megaraid: Don't u...
2255
  	u8	battery_status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2256

c7f079ca3   David Howells   megaraid: Don't u...
2257
2258
  	if( make_local_pdev(adapter, &pdev) != 0 )
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2259

c7f079ca3   David Howells   megaraid: Don't u...
2260
2261
  	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
  		goto free_pdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2262
2263
  
  	if( mega_adapinq(adapter, dma_handle) != 0 ) {
c7f079ca3   David Howells   megaraid: Don't u...
2264
2265
  		seq_printf(m, "Adapter inquiry failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2266
2267
  		printk(KERN_WARNING "megaraid: inquiry failed.
  ");
c7f079ca3   David Howells   megaraid: Don't u...
2268
  		goto free_inquiry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
  	}
  
  	if( adapter->flag & BOARD_40LD ) {
  		battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  	}
  	else {
  		battery_status = ((mraid_ext_inquiry *)inquiry)->
  			raid_inq.adapter_info.battery_status;
  	}
  
  	/*
  	 * Decode the battery status
  	 */
c7f079ca3   David Howells   megaraid: Don't u...
2282
  	seq_printf(m, "Battery Status:[%d]", battery_status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2283
2284
  
  	if(battery_status == MEGA_BATT_CHARGE_DONE)
c7f079ca3   David Howells   megaraid: Don't u...
2285
  		seq_puts(m, " Charge Done");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2286
2287
  
  	if(battery_status & MEGA_BATT_MODULE_MISSING)
c7f079ca3   David Howells   megaraid: Don't u...
2288
  		seq_puts(m, " Module Missing");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2289
2290
  	
  	if(battery_status & MEGA_BATT_LOW_VOLTAGE)
c7f079ca3   David Howells   megaraid: Don't u...
2291
  		seq_puts(m, " Low Voltage");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2292
2293
  	
  	if(battery_status & MEGA_BATT_TEMP_HIGH)
c7f079ca3   David Howells   megaraid: Don't u...
2294
  		seq_puts(m, " Temperature High");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2295
2296
  	
  	if(battery_status & MEGA_BATT_PACK_MISSING)
c7f079ca3   David Howells   megaraid: Don't u...
2297
  		seq_puts(m, " Pack Missing");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2298
2299
  	
  	if(battery_status & MEGA_BATT_CHARGE_INPROG)
c7f079ca3   David Howells   megaraid: Don't u...
2300
  		seq_puts(m, " Charge In-progress");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2301
2302
  	
  	if(battery_status & MEGA_BATT_CHARGE_FAIL)
c7f079ca3   David Howells   megaraid: Don't u...
2303
  		seq_puts(m, " Charge Fail");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2304
2305
  	
  	if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
c7f079ca3   David Howells   megaraid: Don't u...
2306
  		seq_puts(m, " Cycles Exceeded");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2307

c7f079ca3   David Howells   megaraid: Don't u...
2308
2309
  	seq_putc(m, '
  ');
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2310

c7f079ca3   David Howells   megaraid: Don't u...
2311
  free_inquiry:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2312
  	mega_free_inquiry(inquiry, dma_handle, pdev);
c7f079ca3   David Howells   megaraid: Don't u...
2313
  free_pdev:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2314
  	free_local_pdev(pdev);
c7f079ca3   David Howells   megaraid: Don't u...
2315
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2316
  }
c7f079ca3   David Howells   megaraid: Don't u...
2317
2318
  /*
   * Display scsi inquiry
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2319
   */
c7f079ca3   David Howells   megaraid: Don't u...
2320
2321
  static void
  mega_print_inquiry(struct seq_file *m, char *scsi_inq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2322
  {
c7f079ca3   David Howells   megaraid: Don't u...
2323
  	int	i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2324

c7f079ca3   David Howells   megaraid: Don't u...
2325
2326
2327
2328
2329
2330
2331
2332
  	seq_puts(m, "  Vendor: ");
  	seq_write(m, scsi_inq + 8, 8);
  	seq_puts(m, "  Model: ");
  	seq_write(m, scsi_inq + 16, 16);
  	seq_puts(m, "  Rev: ");
  	seq_write(m, scsi_inq + 32, 4);
  	seq_putc(m, '
  ');
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2333

c7f079ca3   David Howells   megaraid: Don't u...
2334
2335
  	i = scsi_inq[0] & 0x1f;
  	seq_printf(m, "  Type:   %s ", scsi_device_type(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2336

c7f079ca3   David Howells   megaraid: Don't u...
2337
2338
  	seq_printf(m, "                 ANSI SCSI revision: %02x",
  		   scsi_inq[2] & 0x07);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2339

c7f079ca3   David Howells   megaraid: Don't u...
2340
2341
2342
2343
2344
2345
  	if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  		seq_puts(m, " CCS
  ");
  	else
  		seq_putc(m, '
  ');
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2346
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2347
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2348
2349
   * proc_show_pdrv()
   * @m - Synthetic file construction data
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2350
2351
2352
2353
2354
2355
   * @page - buffer to write the data in
   * @adapter - pointer to our soft state
   *
   * Display information about the physical drives.
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2356
  proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
  {
  	dma_addr_t	dma_handle;
  	char		*scsi_inq;
  	dma_addr_t	scsi_inq_dma_handle;
  	caddr_t		inquiry;
  	struct pci_dev	*pdev;
  	u8	*pdrv_state;
  	u8	state;
  	int	tgt;
  	int	max_channels;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2367
  	int	i;
c7f079ca3   David Howells   megaraid: Don't u...
2368
2369
  	if( make_local_pdev(adapter, &pdev) != 0 )
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2370

c7f079ca3   David Howells   megaraid: Don't u...
2371
  	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2372
  		goto free_pdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2373
2374
  
  	if( mega_adapinq(adapter, dma_handle) != 0 ) {
c7f079ca3   David Howells   megaraid: Don't u...
2375
2376
  		seq_puts(m, "Adapter inquiry failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2377
2378
  		printk(KERN_WARNING "megaraid: inquiry failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2379
2380
2381
2382
2383
  		goto free_inquiry;
  	}
  
  
  	scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2384
  	if( scsi_inq == NULL ) {
c7f079ca3   David Howells   megaraid: Don't u...
2385
2386
  		seq_puts(m, "memory not available for scsi inq.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
  		goto free_inquiry;
  	}
  
  	if( adapter->flag & BOARD_40LD ) {
  		pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  	}
  	else {
  		pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  			raid_inq.pdrv_info.pdrv_state;
  	}
  
  	max_channels = adapter->product_info.nchannels;
  
  	if( channel >= max_channels ) {
  		goto free_pci;
  	}
  
  	for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  
  		i = channel*16 + tgt;
  
  		state = *(pdrv_state + i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2409
  		switch( state & 0x0F ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2410
  		case PDRV_ONLINE:
c7f079ca3   David Howells   megaraid: Don't u...
2411
2412
  			seq_printf(m, "Channel:%2d Id:%2d State: Online",
  				   channel, tgt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2413
2414
2415
  			break;
  
  		case PDRV_FAILED:
c7f079ca3   David Howells   megaraid: Don't u...
2416
2417
  			seq_printf(m, "Channel:%2d Id:%2d State: Failed",
  				   channel, tgt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2418
2419
2420
  			break;
  
  		case PDRV_RBLD:
c7f079ca3   David Howells   megaraid: Don't u...
2421
2422
  			seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
  				   channel, tgt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2423
2424
2425
  			break;
  
  		case PDRV_HOTSPARE:
c7f079ca3   David Howells   megaraid: Don't u...
2426
2427
  			seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
  				   channel, tgt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2428
2429
2430
  			break;
  
  		default:
c7f079ca3   David Howells   megaraid: Don't u...
2431
2432
  			seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
  				   channel, tgt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2433
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
  		}
  
  		/*
  		 * This interface displays inquiries for disk drives
  		 * only. Inquries for logical drives and non-disk
  		 * devices are available through /proc/scsi/scsi
  		 */
  		memset(scsi_inq, 0, 256);
  		if( mega_internal_dev_inquiry(adapter, channel, tgt,
  				scsi_inq_dma_handle) ||
  				(scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  			continue;
  		}
  
  		/*
  		 * Check for overflow. We print less than 240
  		 * characters for inquiry
  		 */
c7f079ca3   David Howells   megaraid: Don't u...
2452
2453
2454
  		seq_puts(m, ".
  ");
  		mega_print_inquiry(m, scsi_inq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2455
2456
2457
2458
2459
2460
2461
2462
  	}
  
  free_pci:
  	pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  free_inquiry:
  	mega_free_inquiry(inquiry, dma_handle, pdev);
  free_pdev:
  	free_local_pdev(pdev);
c7f079ca3   David Howells   megaraid: Don't u...
2463
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2464
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2465
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2466
2467
2468
   * proc_show_pdrv_ch0()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2469
   *
c7f079ca3   David Howells   megaraid: Don't u...
2470
   * Display information about the physical drives on physical channel 0.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2471
2472
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2473
  proc_show_pdrv_ch0(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2474
  {
c7f079ca3   David Howells   megaraid: Don't u...
2475
  	return proc_show_pdrv(m, m->private, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2476
2477
2478
2479
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2480
2481
2482
   * proc_show_pdrv_ch1()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2483
   *
c7f079ca3   David Howells   megaraid: Don't u...
2484
   * Display information about the physical drives on physical channel 1.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2485
2486
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2487
  proc_show_pdrv_ch1(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2488
  {
c7f079ca3   David Howells   megaraid: Don't u...
2489
  	return proc_show_pdrv(m, m->private, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2490
2491
2492
2493
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2494
2495
2496
   * proc_show_pdrv_ch2()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2497
   *
c7f079ca3   David Howells   megaraid: Don't u...
2498
   * Display information about the physical drives on physical channel 2.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2499
2500
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2501
  proc_show_pdrv_ch2(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2502
  {
c7f079ca3   David Howells   megaraid: Don't u...
2503
  	return proc_show_pdrv(m, m->private, 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2504
2505
2506
2507
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2508
2509
2510
   * proc_show_pdrv_ch3()
   * @m - Synthetic file construction data
   * @v - File iterator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2511
   *
c7f079ca3   David Howells   megaraid: Don't u...
2512
   * Display information about the physical drives on physical channel 3.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2513
2514
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2515
  proc_show_pdrv_ch3(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2516
  {
c7f079ca3   David Howells   megaraid: Don't u...
2517
  	return proc_show_pdrv(m, m->private, 3);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2518
2519
2520
2521
  }
  
  
  /**
c7f079ca3   David Howells   megaraid: Don't u...
2522
2523
   * proc_show_rdrv()
   * @m - Synthetic file construction data
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2524
2525
2526
2527
2528
2529
2530
2531
   * @adapter - pointer to our soft state
   * @start - starting logical drive to display
   * @end - ending logical drive to display
   *
   * We do not print the inquiry information since its already available through
   * /proc/scsi/scsi interface
   */
  static int
c7f079ca3   David Howells   megaraid: Don't u...
2532
  proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
  {
  	dma_addr_t	dma_handle;
  	logdrv_param	*lparam;
  	megacmd_t	mc;
  	char		*disk_array;
  	dma_addr_t	disk_array_dma_handle;
  	caddr_t		inquiry;
  	struct pci_dev	*pdev;
  	u8	*rdrv_state;
  	int	num_ldrv;
  	u32	array_sz;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2544
  	int	i;
c7f079ca3   David Howells   megaraid: Don't u...
2545
2546
  	if( make_local_pdev(adapter, &pdev) != 0 )
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2547

c7f079ca3   David Howells   megaraid: Don't u...
2548
2549
  	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
  		goto free_pdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2550
2551
  
  	if( mega_adapinq(adapter, dma_handle) != 0 ) {
c7f079ca3   David Howells   megaraid: Don't u...
2552
2553
  		seq_puts(m, "Adapter inquiry failed.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2554
2555
  		printk(KERN_WARNING "megaraid: inquiry failed.
  ");
c7f079ca3   David Howells   megaraid: Don't u...
2556
  		goto free_inquiry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
  	}
  
  	memset(&mc, 0, sizeof(megacmd_t));
  
  	if( adapter->flag & BOARD_40LD ) {
  		array_sz = sizeof(disk_array_40ld);
  
  		rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  
  		num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  	}
  	else {
  		array_sz = sizeof(disk_array_8ld);
  
  		rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  			raid_inq.logdrv_info.ldrv_state;
  
  		num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  			raid_inq.logdrv_info.num_ldrv;
  	}
  
  	disk_array = pci_alloc_consistent(pdev, array_sz,
  			&disk_array_dma_handle);
  
  	if( disk_array == NULL ) {
c7f079ca3   David Howells   megaraid: Don't u...
2582
2583
2584
  		seq_puts(m, "memory not available.
  ");
  		goto free_inquiry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2585
2586
2587
2588
2589
2590
2591
  	}
  
  	mc.xferaddr = (u32)disk_array_dma_handle;
  
  	if( adapter->flag & BOARD_40LD ) {
  		mc.cmd = FC_NEW_CONFIG;
  		mc.opcode = OP_DCMD_READ_CONFIG;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
2592
  		if( mega_internal_command(adapter, &mc, NULL) ) {
c7f079ca3   David Howells   megaraid: Don't u...
2593
2594
2595
  			seq_puts(m, "40LD read config failed.
  ");
  			goto free_pci;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2596
2597
2598
2599
2600
  		}
  
  	}
  	else {
  		mc.cmd = NEW_READ_CONFIG_8LD;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
2601
  		if( mega_internal_command(adapter, &mc, NULL) ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2602
  			mc.cmd = READ_CONFIG_8LD;
c7f079ca3   David Howells   megaraid: Don't u...
2603
2604
2605
2606
  			if( mega_internal_command(adapter, &mc, NULL) ) {
  				seq_puts(m, "8LD read config failed.
  ");
  				goto free_pci;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
  			}
  		}
  	}
  
  	for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  
  		if( adapter->flag & BOARD_40LD ) {
  			lparam =
  			&((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  		}
  		else {
  			lparam =
  			&((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  		}
  
  		/*
  		 * Check for overflow. We print less than 240 characters for
  		 * information about each logical drive.
  		 */
c7f079ca3   David Howells   megaraid: Don't u...
2626
  		seq_printf(m, "Logical drive:%2d:, ", i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2627
2628
2629
  
  		switch( rdrv_state[i] & 0x0F ) {
  		case RDRV_OFFLINE:
c7f079ca3   David Howells   megaraid: Don't u...
2630
  			seq_puts(m, "state: offline");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2631
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2632
  		case RDRV_DEGRADED:
c7f079ca3   David Howells   megaraid: Don't u...
2633
  			seq_puts(m, "state: degraded");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2634
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2635
  		case RDRV_OPTIMAL:
c7f079ca3   David Howells   megaraid: Don't u...
2636
  			seq_puts(m, "state: optimal");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2637
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2638
  		case RDRV_DELETED:
c7f079ca3   David Howells   megaraid: Don't u...
2639
  			seq_puts(m, "state: deleted");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2640
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2641
  		default:
c7f079ca3   David Howells   megaraid: Don't u...
2642
  			seq_puts(m, "state: unknown");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2643
2644
2645
2646
2647
2648
2649
  			break;
  		}
  
  		/*
  		 * Check if check consistency or initialization is going on
  		 * for this logical drive.
  		 */
c7f079ca3   David Howells   megaraid: Don't u...
2650
2651
2652
2653
  		if( (rdrv_state[i] & 0xF0) == 0x20 )
  			seq_puts(m, ", check-consistency in progress");
  		else if( (rdrv_state[i] & 0xF0) == 0x10 )
  			seq_puts(m, ", initialization in progress");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2654
  		
c7f079ca3   David Howells   megaraid: Don't u...
2655
2656
  		seq_putc(m, '
  ');
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2657

c7f079ca3   David Howells   megaraid: Don't u...
2658
2659
2660
2661
2662
2663
  		seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
  		seq_printf(m, "RAID level:%3d, ", lparam->level);
  		seq_printf(m, "Stripe size:%3d, ",
  			   lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  		seq_printf(m, "Row size:%3d
  ", lparam->row_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2664

c7f079ca3   David Howells   megaraid: Don't u...
2665
  		seq_puts(m, "Read Policy: ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2666
  		switch(lparam->read_ahead) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2667
  		case NO_READ_AHEAD:
c7f079ca3   David Howells   megaraid: Don't u...
2668
  			seq_puts(m, "No read ahead, ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2669
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2670
  		case READ_AHEAD:
c7f079ca3   David Howells   megaraid: Don't u...
2671
  			seq_puts(m, "Read ahead, ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2672
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2673
  		case ADAP_READ_AHEAD:
c7f079ca3   David Howells   megaraid: Don't u...
2674
  			seq_puts(m, "Adaptive, ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2675
2676
2677
  			break;
  
  		}
c7f079ca3   David Howells   megaraid: Don't u...
2678
  		seq_puts(m, "Write Policy: ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2679
  		switch(lparam->write_mode) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2680
  		case WRMODE_WRITE_THRU:
c7f079ca3   David Howells   megaraid: Don't u...
2681
  			seq_puts(m, "Write thru, ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2682
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2683
  		case WRMODE_WRITE_BACK:
c7f079ca3   David Howells   megaraid: Don't u...
2684
  			seq_puts(m, "Write back, ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2685
2686
  			break;
  		}
c7f079ca3   David Howells   megaraid: Don't u...
2687
  		seq_puts(m, "Cache Policy: ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2688
  		switch(lparam->direct_io) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2689
  		case CACHED_IO:
c7f079ca3   David Howells   megaraid: Don't u...
2690
2691
2692
  			seq_puts(m, "Cached IO
  
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2693
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2694
  		case DIRECT_IO:
c7f079ca3   David Howells   megaraid: Don't u...
2695
2696
2697
  			seq_puts(m, "Direct IO
  
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2698
2699
2700
  			break;
  		}
  	}
c7f079ca3   David Howells   megaraid: Don't u...
2701
  free_pci:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2702
2703
  	pci_free_consistent(pdev, array_sz, disk_array,
  			disk_array_dma_handle);
c7f079ca3   David Howells   megaraid: Don't u...
2704
2705
2706
  free_inquiry:
  	mega_free_inquiry(inquiry, dma_handle, pdev);
  free_pdev:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2707
  	free_local_pdev(pdev);
c7f079ca3   David Howells   megaraid: Don't u...
2708
2709
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2710

c7f079ca3   David Howells   megaraid: Don't u...
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
  /**
   * proc_show_rdrv_10()
   * @m - Synthetic file construction data
   * @v - File iterator
   *
   * Display real time information about the logical drives 0 through 9.
   */
  static int
  proc_show_rdrv_10(struct seq_file *m, void *v)
  {
  	return proc_show_rdrv(m, m->private, 0, 9);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2722
  }
c7f079ca3   David Howells   megaraid: Don't u...
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
  
  
  /**
   * proc_show_rdrv_20()
   * @m - Synthetic file construction data
   * @v - File iterator
   *
   * Display real time information about the logical drives 0 through 9.
   */
  static int
  proc_show_rdrv_20(struct seq_file *m, void *v)
  {
  	return proc_show_rdrv(m, m->private, 10, 19);
  }
  
  
  /**
   * proc_show_rdrv_30()
   * @m - Synthetic file construction data
   * @v - File iterator
   *
   * Display real time information about the logical drives 0 through 9.
   */
  static int
  proc_show_rdrv_30(struct seq_file *m, void *v)
  {
  	return proc_show_rdrv(m, m->private, 20, 29);
  }
  
  
  /**
   * proc_show_rdrv_40()
   * @m - Synthetic file construction data
   * @v - File iterator
   *
   * Display real time information about the logical drives 0 through 9.
   */
  static int
  proc_show_rdrv_40(struct seq_file *m, void *v)
  {
  	return proc_show_rdrv(m, m->private, 30, 39);
  }
  
  
  /*
   * seq_file wrappers for procfile show routines.
   */
  static int mega_proc_open(struct inode *inode, struct file *file)
  {
4a520d276   David Howells   proc: Supply an a...
2772
  	adapter_t *adapter = proc_get_parent_data(inode);
c7f079ca3   David Howells   megaraid: Don't u...
2773
2774
2775
2776
2777
2778
2779
2780
2781
  	int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
  
  	return single_open(file, show, adapter);
  }
  
  static const struct file_operations mega_proc_fops = {
  	.open		= mega_proc_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
9bec8a740   Al Viro   megaraid: single_...
2782
  	.release	= single_release,
c7f079ca3   David Howells   megaraid: Don't u...
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
  };
  
  /*
   * Table of proc files we need to create.
   */
  struct mega_proc_file {
  	const char *name;
  	unsigned short ptr_offset;
  	int (*show) (struct seq_file *m, void *v);
  };
  
  static const struct mega_proc_file mega_proc_files[] = {
  	{ "config",	      offsetof(adapter_t, proc_read), proc_show_config },
  	{ "stat",	      offsetof(adapter_t, proc_stat), proc_show_stat },
  	{ "mailbox",	      offsetof(adapter_t, proc_mbox), proc_show_mbox },
  #if MEGA_HAVE_ENH_PROC
  	{ "rebuild-rate",     offsetof(adapter_t, proc_rr), proc_show_rebuild_rate },
  	{ "battery-status",   offsetof(adapter_t, proc_battery), proc_show_battery },
  	{ "diskdrives-ch0",   offsetof(adapter_t, proc_pdrvstat[0]), proc_show_pdrv_ch0 },
  	{ "diskdrives-ch1",   offsetof(adapter_t, proc_pdrvstat[1]), proc_show_pdrv_ch1 },
  	{ "diskdrives-ch2",   offsetof(adapter_t, proc_pdrvstat[2]), proc_show_pdrv_ch2 },
  	{ "diskdrives-ch3",   offsetof(adapter_t, proc_pdrvstat[3]), proc_show_pdrv_ch3 },
  	{ "raiddrives-0-9",   offsetof(adapter_t, proc_rdrvstat[0]), proc_show_rdrv_10 },
  	{ "raiddrives-10-19", offsetof(adapter_t, proc_rdrvstat[1]), proc_show_rdrv_20 },
  	{ "raiddrives-20-29", offsetof(adapter_t, proc_rdrvstat[2]), proc_show_rdrv_30 },
  	{ "raiddrives-30-39", offsetof(adapter_t, proc_rdrvstat[3]), proc_show_rdrv_40 },
  #endif
  	{ NULL }
  };
  
  /**
   * mega_create_proc_entry()
   * @index - index in soft state array
   * @parent - parent node for this /proc entry
   *
   * Creates /proc entries for our controllers.
   */
  static void
  mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  {
  	const struct mega_proc_file *f;
  	adapter_t	*adapter = hba_soft_state[index];
  	struct proc_dir_entry	*dir, *de, **ppde;
  	u8		string[16];
  
  	sprintf(string, "hba%d", adapter->host->host_no);
270b5ac21   David Howells   proc: Add proc_mk...
2829
2830
  	dir = adapter->controller_proc_dir_entry =
  		proc_mkdir_data(string, 0, parent, adapter);
c7f079ca3   David Howells   megaraid: Don't u...
2831
2832
2833
2834
2835
2836
  	if(!dir) {
  		printk(KERN_WARNING "
  megaraid: proc_mkdir failed
  ");
  		return;
  	}
c7f079ca3   David Howells   megaraid: Don't u...
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
  
  	for (f = mega_proc_files; f->name; f++) {
  		de = proc_create_data(f->name, S_IRUSR, dir, &mega_proc_fops,
  				      f->show);
  		if (!de) {
  			printk(KERN_WARNING "
  megaraid: proc_create failed
  ");
  			return;
  		}
  
  		ppde = (void *)adapter + f->ptr_offset;
  		*ppde = de;
  	}
  }
84a3c97b9   walter harms   [SCSI] megaraid: ...
2852
2853
2854
2855
  #else
  static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  {
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
  #endif
  
  
  /**
   * megaraid_biosparam()
   *
   * Return the disk geometry for a particular disk
   */
  static int
  megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  		    sector_t capacity, int geom[])
  {
  	adapter_t	*adapter;
  	unsigned char	*bh;
  	int	heads;
  	int	sectors;
  	int	cylinders;
  	int	rval;
  
  	/* Get pointer to host config structure */
  	adapter = (adapter_t *)sdev->host->hostdata;
  
  	if (IS_RAID_CH(adapter, sdev->channel)) {
  			/* Default heads (64) & sectors (32) */
  			heads = 64;
  			sectors = 32;
  			cylinders = (ulong)capacity / (heads * sectors);
  
  			/*
  			 * Handle extended translation size for logical drives
  			 * > 1Gb
  			 */
  			if ((ulong)capacity >= 0x200000) {
  				heads = 255;
  				sectors = 63;
  				cylinders = (ulong)capacity / (heads * sectors);
  			}
  
  			/* return result */
  			geom[0] = heads;
  			geom[1] = sectors;
  			geom[2] = cylinders;
  	}
  	else {
  		bh = scsi_bios_ptable(bdev);
  
  		if( bh ) {
  			rval = scsi_partsize(bh, capacity,
  					    &geom[2], &geom[0], &geom[1]);
  			kfree(bh);
  			if( rval != -1 )
  				return rval;
  		}
  
  		printk(KERN_INFO
  		"megaraid: invalid partition on this disk on channel %d
  ",
  				sdev->channel);
  
  		/* Default heads (64) & sectors (32) */
  		heads = 64;
  		sectors = 32;
  		cylinders = (ulong)capacity / (heads * sectors);
  
  		/* Handle extended translation size for logical drives > 1Gb */
  		if ((ulong)capacity >= 0x200000) {
  			heads = 255;
  			sectors = 63;
  			cylinders = (ulong)capacity / (heads * sectors);
  		}
  
  		/* return result */
  		geom[0] = heads;
  		geom[1] = sectors;
  		geom[2] = cylinders;
  	}
  
  	return 0;
  }
  
  /**
   * mega_init_scb()
   * @adapter - pointer to our soft state
   *
   * Allocate memory for the various pointers in the scb structures:
   * scatter-gather list pointer, passthru and extended passthru structure
   * pointers.
   */
  static int
  mega_init_scb(adapter_t *adapter)
  {
  	scb_t	*scb;
  	int	i;
  
  	for( i = 0; i < adapter->max_cmds; i++ ) {
  
  		scb = &adapter->scb_list[i];
  
  		scb->sgl64 = NULL;
  		scb->sgl = NULL;
  		scb->pthru = NULL;
  		scb->epthru = NULL;
  	}
  
  	for( i = 0; i < adapter->max_cmds; i++ ) {
  
  		scb = &adapter->scb_list[i];
  
  		scb->idx = i;
  
  		scb->sgl64 = pci_alloc_consistent(adapter->dev,
  				sizeof(mega_sgl64) * adapter->sglen,
  				&scb->sgl_dma_addr);
  
  		scb->sgl = (mega_sglist *)scb->sgl64;
  
  		if( !scb->sgl ) {
  			printk(KERN_WARNING "RAID: Can't allocate sglist.
  ");
  			mega_free_sgl(adapter);
  			return -1;
  		}
  
  		scb->pthru = pci_alloc_consistent(adapter->dev,
  				sizeof(mega_passthru),
  				&scb->pthru_dma_addr);
  
  		if( !scb->pthru ) {
  			printk(KERN_WARNING "RAID: Can't allocate passthru.
  ");
  			mega_free_sgl(adapter);
  			return -1;
  		}
  
  		scb->epthru = pci_alloc_consistent(adapter->dev,
  				sizeof(mega_ext_passthru),
  				&scb->epthru_dma_addr);
  
  		if( !scb->epthru ) {
  			printk(KERN_WARNING
  				"Can't allocate extended passthru.
  ");
  			mega_free_sgl(adapter);
  			return -1;
  		}
  
  
  		scb->dma_type = MEGA_DMA_TYPE_NONE;
  
  		/*
  		 * Link to free list
  		 * lock not required since we are loading the driver, so no
  		 * commands possible right now.
  		 */
  		scb->state = SCB_FREE;
  		scb->cmd = NULL;
  		list_add(&scb->list, &adapter->free_list);
  	}
  
  	return 0;
  }
  
  
  /**
   * megadev_open()
   * @inode - unused
   * @filep - unused
   *
   * Routines for the character/ioctl interface to the driver. Find out if this
d21c95c56   Jonathan Corbet   Add "no BKL neede...
3025
   * is a valid open. 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
   */
  static int
  megadev_open (struct inode *inode, struct file *filep)
  {
  	/*
  	 * Only allow superuser to access private ioctl interface
  	 */
  	if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  
  	return 0;
  }
  
  
  /**
   * megadev_ioctl()
   * @inode - Our device inode
   * @filep - unused
   * @cmd - ioctl command
   * @arg - user buffer
   *
   * ioctl entry point for our private ioctl interface. We move the data in from
   * the user space, prepare the command (if necessary, convert the old MIMD
   * ioctl to new ioctl command), and issue a synchronous command to the
   * controller.
   */
  static int
f4927c45b   Arnd Bergmann   scsi: Push down B...
3052
  megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
  {
  	adapter_t	*adapter;
  	nitioctl_t	uioc;
  	int		adapno;
  	int		rval;
  	mega_passthru	__user *upthru;	/* user address for passthru */
  	mega_passthru	*pthru;		/* copy user passthru here */
  	dma_addr_t	pthru_dma_hndl;
  	void		*data = NULL;	/* data to be transferred */
  	dma_addr_t	data_dma_hndl;	/* dma handle for data xfer area */
  	megacmd_t	mc;
  	megastat_t	__user *ustats;
  	int		num_ldrv;
  	u32		uxferaddr = 0;
  	struct pci_dev	*pdev;
  
  	ustats = NULL; /* avoid compilation warnings */
  	num_ldrv = 0;
  
  	/*
  	 * Make sure only USCSICMD are issued through this interface.
  	 * MIMD application would still fire different command.
  	 */
  	if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  		return -EINVAL;
  	}
  
  	/*
  	 * Check and convert a possible MIMD command to NIT command.
  	 * mega_m_to_n() copies the data from the user space, so we do not
  	 * have to do it here.
  	 * NOTE: We will need some user address to copyout the data, therefore
  	 * the inteface layer will also provide us with the required user
  	 * addresses.
  	 */
  	memset(&uioc, 0, sizeof(nitioctl_t));
  	if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  		return rval;
  
  
  	switch( uioc.opcode ) {
  
  	case GET_DRIVER_VER:
  		if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  			return (-EFAULT);
  
  		break;
  
  	case GET_N_ADAP:
  		if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  			return (-EFAULT);
  
  		/*
  		 * Shucks. MIMD interface returns a positive value for number
  		 * of adapters. TODO: Change it to return 0 when there is no
  		 * applicatio using mimd interface.
  		 */
  		return hba_count;
  
  	case GET_ADAP_INFO:
  
  		/*
  		 * Which adapter
  		 */
  		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  			return (-ENODEV);
  
  		if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  				sizeof(struct mcontroller)) )
  			return (-EFAULT);
  		break;
  
  #if MEGA_HAVE_STATS
  
  	case GET_STATS:
  		/*
  		 * Which adapter
  		 */
  		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  			return (-ENODEV);
  
  		adapter = hba_soft_state[adapno];
  
  		ustats = uioc.uioc_uaddr;
  
  		if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  			return (-EFAULT);
  
  		/*
  		 * Check for the validity of the logical drive number
  		 */
  		if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  
  		if( copy_to_user(ustats->nreads, adapter->nreads,
  					num_ldrv*sizeof(u32)) )
  			return -EFAULT;
  
  		if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  					num_ldrv*sizeof(u32)) )
  			return -EFAULT;
  
  		if( copy_to_user(ustats->nwrites, adapter->nwrites,
  					num_ldrv*sizeof(u32)) )
  			return -EFAULT;
  
  		if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  					num_ldrv*sizeof(u32)) )
  			return -EFAULT;
  
  		if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  					num_ldrv*sizeof(u32)) )
  			return -EFAULT;
  
  		if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  					num_ldrv*sizeof(u32)) )
  			return -EFAULT;
  
  		return 0;
  
  #endif
  	case MBOX_CMD:
  
  		/*
  		 * Which adapter
  		 */
  		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  			return (-ENODEV);
  
  		adapter = hba_soft_state[adapno];
  
  		/*
  		 * Deletion of logical drive is a special case. The adapter
  		 * should be quiescent before this command is issued.
  		 */
  		if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  				uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  
  			/*
  			 * Do we support this feature
  			 */
  			if( !adapter->support_random_del ) {
  				printk(KERN_WARNING "megaraid: logdrv ");
  				printk("delete on non-supporting F/W.
  ");
  
  				return (-EINVAL);
  			}
  
  			rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  
  			if( rval == 0 ) {
  				memset(&mc, 0, sizeof(megacmd_t));
  
  				mc.status = rval;
  
  				rval = mega_n_to_m((void __user *)arg, &mc);
  			}
  
  			return rval;
  		}
  		/*
  		 * This interface only support the regular passthru commands.
  		 * Reject extended passthru and 64-bit passthru
  		 */
  		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  			uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  
  			printk(KERN_WARNING "megaraid: rejected passthru.
  ");
  
  			return (-EINVAL);
  		}
  
  		/*
  		 * For all internal commands, the buffer must be allocated in
  		 * <4GB address range
  		 */
  		if( make_local_pdev(adapter, &pdev) != 0 )
  			return -EIO;
  
  		/* Is it a passthru command or a DCMD */
  		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  			/* Passthru commands */
  
  			pthru = pci_alloc_consistent(pdev,
  					sizeof(mega_passthru),
  					&pthru_dma_hndl);
  
  			if( pthru == NULL ) {
  				free_local_pdev(pdev);
  				return (-ENOMEM);
  			}
  
  			/*
  			 * The user passthru structure
  			 */
de5952e91   Martin Bligh   [SCSI] megaraid: ...
3249
  			upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
  
  			/*
  			 * Copy in the user passthru here.
  			 */
  			if( copy_from_user(pthru, upthru,
  						sizeof(mega_passthru)) ) {
  
  				pci_free_consistent(pdev,
  						sizeof(mega_passthru), pthru,
  						pthru_dma_hndl);
  
  				free_local_pdev(pdev);
  
  				return (-EFAULT);
  			}
  
  			/*
  			 * Is there a data transfer
  			 */
  			if( pthru->dataxferlen ) {
  				data = pci_alloc_consistent(pdev,
  						pthru->dataxferlen,
  						&data_dma_hndl);
  
  				if( data == NULL ) {
  					pci_free_consistent(pdev,
  							sizeof(mega_passthru),
  							pthru,
  							pthru_dma_hndl);
  
  					free_local_pdev(pdev);
  
  					return (-ENOMEM);
  				}
  
  				/*
  				 * Save the user address and point the kernel
  				 * address at just allocated memory
  				 */
  				uxferaddr = pthru->dataxferaddr;
  				pthru->dataxferaddr = data_dma_hndl;
  			}
  
  
  			/*
  			 * Is data coming down-stream
  			 */
  			if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  				/*
  				 * Get the user data
  				 */
de5952e91   Martin Bligh   [SCSI] megaraid: ...
3301
  				if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
  							pthru->dataxferlen) ) {
  					rval = (-EFAULT);
  					goto freemem_and_return;
  				}
  			}
  
  			memset(&mc, 0, sizeof(megacmd_t));
  
  			mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  			mc.xferaddr = (u32)pthru_dma_hndl;
  
  			/*
  			 * Issue the command
  			 */
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
3316
  			mega_internal_command(adapter, &mc, pthru);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
  
  			rval = mega_n_to_m((void __user *)arg, &mc);
  
  			if( rval ) goto freemem_and_return;
  
  
  			/*
  			 * Is data going up-stream
  			 */
  			if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
de5952e91   Martin Bligh   [SCSI] megaraid: ...
3327
  				if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3328
3329
3330
3331
3332
3333
3334
3335
3336
  							pthru->dataxferlen) ) {
  					rval = (-EFAULT);
  				}
  			}
  
  			/*
  			 * Send the request sense data also, irrespective of
  			 * whether the user has asked for it or not.
  			 */
2d2f8d59b   Jesper Juhl   [SCSI] megaraid: ...
3337
3338
3339
  			if (copy_to_user(upthru->reqsensearea,
  					pthru->reqsensearea, 14))
  				rval = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
  
  freemem_and_return:
  			if( pthru->dataxferlen ) {
  				pci_free_consistent(pdev,
  						pthru->dataxferlen, data,
  						data_dma_hndl);
  			}
  
  			pci_free_consistent(pdev, sizeof(mega_passthru),
  					pthru, pthru_dma_hndl);
  
  			free_local_pdev(pdev);
  
  			return rval;
  		}
  		else {
  			/* DCMD commands */
  
  			/*
  			 * Is there a data transfer
  			 */
  			if( uioc.xferlen ) {
  				data = pci_alloc_consistent(pdev,
  						uioc.xferlen, &data_dma_hndl);
  
  				if( data == NULL ) {
  					free_local_pdev(pdev);
  					return (-ENOMEM);
  				}
  
  				uxferaddr = MBOX(uioc)->xferaddr;
  			}
  
  			/*
  			 * Is data coming down-stream
  			 */
  			if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  				/*
  				 * Get the user data
  				 */
de5952e91   Martin Bligh   [SCSI] megaraid: ...
3380
  				if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
  							uioc.xferlen) ) {
  
  					pci_free_consistent(pdev,
  							uioc.xferlen,
  							data, data_dma_hndl);
  
  					free_local_pdev(pdev);
  
  					return (-EFAULT);
  				}
  			}
  
  			memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  
  			mc.xferaddr = (u32)data_dma_hndl;
  
  			/*
  			 * Issue the command
  			 */
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
3400
  			mega_internal_command(adapter, &mc, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
  
  			rval = mega_n_to_m((void __user *)arg, &mc);
  
  			if( rval ) {
  				if( uioc.xferlen ) {
  					pci_free_consistent(pdev,
  							uioc.xferlen, data,
  							data_dma_hndl);
  				}
  
  				free_local_pdev(pdev);
  
  				return rval;
  			}
  
  			/*
  			 * Is data going up-stream
  			 */
  			if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
de5952e91   Martin Bligh   [SCSI] megaraid: ...
3420
  				if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
  							uioc.xferlen) ) {
  
  					rval = (-EFAULT);
  				}
  			}
  
  			if( uioc.xferlen ) {
  				pci_free_consistent(pdev,
  						uioc.xferlen, data,
  						data_dma_hndl);
  			}
  
  			free_local_pdev(pdev);
  
  			return rval;
  		}
  
  	default:
  		return (-EINVAL);
  	}
  
  	return 0;
  }
f4927c45b   Arnd Bergmann   scsi: Push down B...
3444
3445
3446
3447
  static long
  megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  {
  	int ret;
c45d15d24   Arnd Bergmann   scsi: autoconvert...
3448
  	mutex_lock(&megadev_mutex);
f4927c45b   Arnd Bergmann   scsi: Push down B...
3449
  	ret = megadev_ioctl(filep, cmd, arg);
c45d15d24   Arnd Bergmann   scsi: autoconvert...
3450
  	mutex_unlock(&megadev_mutex);
f4927c45b   Arnd Bergmann   scsi: Push down B...
3451
3452
3453
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
  /**
   * mega_m_to_n()
   * @arg - user address
   * @uioc - new ioctl structure
   *
   * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
   * structure
   *
   * Converts the older mimd ioctl structure to newer NIT structure
   */
  static int
  mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  {
  	struct uioctl_t	uioc_mimd;
  	char	signature[8] = {0};
  	u8	opcode;
  	u8	subopcode;
  
  
  	/*
  	 * check is the application conforms to NIT. We do not have to do much
  	 * in that case.
  	 * We exploit the fact that the signature is stored in the very
25985edce   Lucas De Marchi   Fix common misspe...
3477
  	 * beginning of the structure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
  	 */
  
  	if( copy_from_user(signature, arg, 7) )
  		return (-EFAULT);
  
  	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  
  		/*
  		 * NOTE NOTE: The nit ioctl is still under flux because of
  		 * change of mailbox definition, in HPE. No applications yet
  		 * use this interface and let's not have applications use this
  		 * interface till the new specifitions are in place.
  		 */
  		return -EINVAL;
  #if 0
  		if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  			return (-EFAULT);
  		return 0;
  #endif
  	}
  
  	/*
  	 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  	 *
  	 * Get the user ioctl structure
  	 */
  	if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  		return (-EFAULT);
  
  
  	/*
  	 * Get the opcode and subopcode for the commands
  	 */
  	opcode = uioc_mimd.ui.fcs.opcode;
  	subopcode = uioc_mimd.ui.fcs.subopcode;
  
  	switch (opcode) {
  	case 0x82:
  
  		switch (subopcode) {
  
  		case MEGAIOC_QDRVRVER:	/* Query driver version */
  			uioc->opcode = GET_DRIVER_VER;
  			uioc->uioc_uaddr = uioc_mimd.data;
  			break;
  
  		case MEGAIOC_QNADAP:	/* Get # of adapters */
  			uioc->opcode = GET_N_ADAP;
  			uioc->uioc_uaddr = uioc_mimd.data;
  			break;
  
  		case MEGAIOC_QADAPINFO:	/* Get adapter information */
  			uioc->opcode = GET_ADAP_INFO;
  			uioc->adapno = uioc_mimd.ui.fcs.adapno;
  			uioc->uioc_uaddr = uioc_mimd.data;
  			break;
  
  		default:
  			return(-EINVAL);
  		}
  
  		break;
  
  
  	case 0x81:
  
  		uioc->opcode = MBOX_CMD;
  		uioc->adapno = uioc_mimd.ui.fcs.adapno;
  
  		memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  
  		uioc->xferlen = uioc_mimd.ui.fcs.length;
  
  		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  
  		break;
  
  	case 0x80:
  
  		uioc->opcode = MBOX_CMD;
  		uioc->adapno = uioc_mimd.ui.fcs.adapno;
  
  		memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  
  		/*
  		 * Choose the xferlen bigger of input and output data
  		 */
  		uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  			uioc_mimd.outlen : uioc_mimd.inlen;
  
  		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  
  		break;
  
  	default:
  		return (-EINVAL);
  
  	}
  
  	return 0;
  }
  
  /*
   * mega_n_to_m()
   * @arg - user address
   * @mc - mailbox command
   *
   * Updates the status information to the application, depending on application
   * conforms to older mimd ioctl interface or newer NIT ioctl interface
   */
  static int
  mega_n_to_m(void __user *arg, megacmd_t *mc)
  {
  	nitioctl_t	__user *uiocp;
  	megacmd_t	__user *umc;
  	mega_passthru	__user *upthru;
  	struct uioctl_t	__user *uioc_mimd;
  	char	signature[8] = {0};
  
  	/*
  	 * check is the application conforms to NIT.
  	 */
  	if( copy_from_user(signature, arg, 7) )
  		return -EFAULT;
  
  	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  
  		uiocp = arg;
  
  		if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  			return (-EFAULT);
  
  		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  
  			umc = MBOX_P(uiocp);
  
  			if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  				return -EFAULT;
  
  			if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  				return (-EFAULT);
  		}
  	}
  	else {
  		uioc_mimd = arg;
  
  		if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  			return (-EFAULT);
  
  		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  
  			umc = (megacmd_t __user *)uioc_mimd->mbox;
  
  			if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  				return (-EFAULT);
  
  			if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  				return (-EFAULT);
  		}
  	}
  
  	return 0;
  }
  
  
  /*
   * MEGARAID 'FW' commands.
   */
  
  /**
   * mega_is_bios_enabled()
   * @adapter - pointer to our soft state
   *
   * issue command to find out if the BIOS is enabled for this controller
   */
  static int
  mega_is_bios_enabled(adapter_t *adapter)
  {
  	unsigned char	raw_mbox[sizeof(struct mbox_out)];
  	mbox_t	*mbox;
  	int	ret;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  
  	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  
  	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  
  	raw_mbox[0] = IS_BIOS_ENABLED;
  	raw_mbox[2] = GET_BIOS;
  
  
  	ret = issue_scb_block(adapter, raw_mbox);
  
  	return *(char *)adapter->mega_buffer;
  }
  
  
  /**
   * mega_enum_raid_scsi()
   * @adapter - pointer to our soft state
   *
   * Find out what channels are RAID/SCSI. This information is used to
   * differentiate the virtual channels and physical channels and to support
   * ROMB feature and non-disk devices.
   */
  static void
  mega_enum_raid_scsi(adapter_t *adapter)
  {
  	unsigned char raw_mbox[sizeof(struct mbox_out)];
  	mbox_t *mbox;
  	int i;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  
  	/*
  	 * issue command to find out what channels are raid/scsi
  	 */
  	raw_mbox[0] = CHNL_CLASS;
  	raw_mbox[2] = GET_CHNL_CLASS;
  
  	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  
  	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  
  	/*
  	 * Non-ROMB firmware fail this command, so all channels
  	 * must be shown RAID
  	 */
  	adapter->mega_ch_class = 0xFF;
  
  	if(!issue_scb_block(adapter, raw_mbox)) {
  		adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  
  	}
  
  	for( i = 0; i < adapter->product_info.nchannels; i++ ) { 
  		if( (adapter->mega_ch_class >> i) & 0x01 ) {
  			printk(KERN_INFO "megaraid: channel[%d] is raid.
  ",
  					i);
  		}
  		else {
  			printk(KERN_INFO "megaraid: channel[%d] is scsi.
  ",
  					i);
  		}
  	}
  
  	return;
  }
  
  
  /**
   * mega_get_boot_drv()
   * @adapter - pointer to our soft state
   *
   * Find out which device is the boot device. Note, any logical drive or any
   * phyical device (e.g., a CDROM) can be designated as a boot device.
   */
  static void
  mega_get_boot_drv(adapter_t *adapter)
  {
  	struct private_bios_data	*prv_bios_data;
  	unsigned char	raw_mbox[sizeof(struct mbox_out)];
  	mbox_t	*mbox;
  	u16	cksum = 0;
  	u8	*cksum_p;
  	u8	boot_pdrv;
  	int	i;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  
  	raw_mbox[0] = BIOS_PVT_DATA;
  	raw_mbox[2] = GET_BIOS_PVT_DATA;
  
  	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  
  	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  
  	adapter->boot_ldrv_enabled = 0;
  	adapter->boot_ldrv = 0;
  
  	adapter->boot_pdrv_enabled = 0;
  	adapter->boot_pdrv_ch = 0;
  	adapter->boot_pdrv_tgt = 0;
  
  	if(issue_scb_block(adapter, raw_mbox) == 0) {
  		prv_bios_data =
  			(struct private_bios_data *)adapter->mega_buffer;
  
  		cksum = 0;
  		cksum_p = (char *)prv_bios_data;
  		for (i = 0; i < 14; i++ ) {
  			cksum += (u16)(*cksum_p++);
  		}
  
  		if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  
  			/*
  			 * If MSB is set, a physical drive is set as boot
  			 * device
  			 */
  			if( prv_bios_data->boot_drv & 0x80 ) {
  				adapter->boot_pdrv_enabled = 1;
  				boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  				adapter->boot_pdrv_ch = boot_pdrv / 16;
  				adapter->boot_pdrv_tgt = boot_pdrv % 16;
  			}
  			else {
  				adapter->boot_ldrv_enabled = 1;
  				adapter->boot_ldrv = prv_bios_data->boot_drv;
  			}
  		}
  	}
  
  }
  
  /**
   * mega_support_random_del()
   * @adapter - pointer to our soft state
   *
   * Find out if this controller supports random deletion and addition of
   * logical drives
   */
  static int
  mega_support_random_del(adapter_t *adapter)
  {
  	unsigned char raw_mbox[sizeof(struct mbox_out)];
  	mbox_t *mbox;
  	int rval;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  
  	/*
  	 * issue command
  	 */
  	raw_mbox[0] = FC_DEL_LOGDRV;
  	raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  
  	rval = issue_scb_block(adapter, raw_mbox);
  
  	return !rval;
  }
  
  
  /**
   * mega_support_ext_cdb()
   * @adapter - pointer to our soft state
   *
   * Find out if this firmware support cdblen > 10
   */
  static int
  mega_support_ext_cdb(adapter_t *adapter)
  {
  	unsigned char raw_mbox[sizeof(struct mbox_out)];
  	mbox_t *mbox;
  	int rval;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  	/*
  	 * issue command to find out if controller supports extended CDBs.
  	 */
  	raw_mbox[0] = 0xA4;
  	raw_mbox[2] = 0x16;
  
  	rval = issue_scb_block(adapter, raw_mbox);
  
  	return !rval;
  }
  
  
  /**
   * mega_del_logdrv()
   * @adapter - pointer to our soft state
   * @logdrv - logical drive to be deleted
   *
   * Delete the specified logical drive. It is the responsibility of the user
   * app to let the OS know about this operation.
   */
  static int
  mega_del_logdrv(adapter_t *adapter, int logdrv)
  {
  	unsigned long flags;
  	scb_t *scb;
  	int rval;
  
  	/*
  	 * Stop sending commands to the controller, queue them internally.
  	 * When deletion is complete, ISR will flush the queue.
  	 */
  	atomic_set(&adapter->quiescent, 1);
  
  	/*
  	 * Wait till all the issued commands are complete and there are no
  	 * commands in the pending queue
  	 */
  	while (atomic_read(&adapter->pend_cmds) > 0 ||
  	       !list_empty(&adapter->pending_list))
  		msleep(1000);	/* sleep for 1s */
  
  	rval = mega_do_del_logdrv(adapter, logdrv);
  
  	spin_lock_irqsave(&adapter->lock, flags);
  
  	/*
  	 * If delete operation was successful, add 0x80 to the logical drive
  	 * ids for commands in the pending queue.
  	 */
  	if (adapter->read_ldidmap) {
  		struct list_head *pos;
  		list_for_each(pos, &adapter->pending_list) {
  			scb = list_entry(pos, scb_t, list);
  			if (scb->pthru->logdrv < 0x80 )
  				scb->pthru->logdrv += 0x80;
  		}
  	}
  
  	atomic_set(&adapter->quiescent, 0);
  
  	mega_runpendq(adapter);
  
  	spin_unlock_irqrestore(&adapter->lock, flags);
  
  	return rval;
  }
  
  
  static int
  mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  {
  	megacmd_t	mc;
  	int	rval;
  
  	memset( &mc, 0, sizeof(megacmd_t));
  
  	mc.cmd = FC_DEL_LOGDRV;
  	mc.opcode = OP_DEL_LOGDRV;
  	mc.subopcode = logdrv;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
3929
  	rval = mega_internal_command(adapter, &mc, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
  
  	/* log this event */
  	if(rval) {
  		printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  		return rval;
  	}
  
  	/*
  	 * After deleting first logical drive, the logical drives must be
  	 * addressed by adding 0x80 to the logical drive id.
  	 */
  	adapter->read_ldidmap = 1;
  
  	return rval;
  }
  
  
  /**
   * mega_get_max_sgl()
   * @adapter - pointer to our soft state
   *
   * Find out the maximum number of scatter-gather elements supported by this
   * version of the firmware
   */
  static void
  mega_get_max_sgl(adapter_t *adapter)
  {
  	unsigned char	raw_mbox[sizeof(struct mbox_out)];
  	mbox_t	*mbox;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(mbox, 0, sizeof(raw_mbox));
  
  	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  
  	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  
  	raw_mbox[0] = MAIN_MISC_OPCODE;
  	raw_mbox[2] = GET_MAX_SG_SUPPORT;
  
  
  	if( issue_scb_block(adapter, raw_mbox) ) {
  		/*
  		 * f/w does not support this command. Choose the default value
  		 */
  		adapter->sglen = MIN_SGLIST;
  	}
  	else {
  		adapter->sglen = *((char *)adapter->mega_buffer);
  		
  		/*
  		 * Make sure this is not more than the resources we are
  		 * planning to allocate
  		 */
  		if ( adapter->sglen > MAX_SGLIST )
  			adapter->sglen = MAX_SGLIST;
  	}
  
  	return;
  }
  
  
  /**
   * mega_support_cluster()
   * @adapter - pointer to our soft state
   *
   * Find out if this firmware support cluster calls.
   */
  static int
  mega_support_cluster(adapter_t *adapter)
  {
  	unsigned char	raw_mbox[sizeof(struct mbox_out)];
  	mbox_t	*mbox;
  
  	mbox = (mbox_t *)raw_mbox;
  
  	memset(mbox, 0, sizeof(raw_mbox));
  
  	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  
  	mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  
  	/*
  	 * Try to get the initiator id. This command will succeed iff the
  	 * clustering is available on this HBA.
  	 */
  	raw_mbox[0] = MEGA_GET_TARGET_ID;
  
  	if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  
  		/*
  		 * Cluster support available. Get the initiator target id.
  		 * Tell our id to mid-layer too.
  		 */
  		adapter->this_id = *(u32 *)adapter->mega_buffer;
  		adapter->host->this_id = adapter->this_id;
  
  		return 1;
  	}
  
  	return 0;
  }
84a3c97b9   walter harms   [SCSI] megaraid: ...
4033
  #ifdef CONFIG_PROC_FS
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4034
4035
4036
4037
4038
  /**
   * mega_adapinq()
   * @adapter - pointer to our soft state
   * @dma_handle - DMA address of the buffer
   *
8e572bab3   Justin P. Mattock   fix typos 'comamn...
4039
   * Issue internal commands while interrupts are available.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
   * We only issue direct mailbox commands from within the driver. ioctl()
   * interface using these routines can issue passthru commands.
   */
  static int
  mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  {
  	megacmd_t	mc;
  
  	memset(&mc, 0, sizeof(megacmd_t));
  
  	if( adapter->flag & BOARD_40LD ) {
  		mc.cmd = FC_NEW_CONFIG;
  		mc.opcode = NC_SUBOP_ENQUIRY3;
  		mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  	}
  	else {
  		mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  	}
  
  	mc.xferaddr = (u32)dma_handle;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
4060
  	if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
  		return -1;
  	}
  
  	return 0;
  }
  
  
  /** mega_internal_dev_inquiry()
   * @adapter - pointer to our soft state
   * @ch - channel for this device
   * @tgt - ID of this device
   * @buf_dma_handle - DMA address of the buffer
   *
   * Issue the scsi inquiry for the specified device.
   */
  static int
  mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  		dma_addr_t buf_dma_handle)
  {
  	mega_passthru	*pthru;
  	dma_addr_t	pthru_dma_handle;
  	megacmd_t	mc;
  	int		rval;
  	struct pci_dev	*pdev;
  
  
  	/*
  	 * For all internal commands, the buffer must be allocated in <4GB
  	 * address range
  	 */
  	if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  
  	pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  			&pthru_dma_handle);
  
  	if( pthru == NULL ) {
  		free_local_pdev(pdev);
  		return -1;
  	}
  
  	pthru->timeout = 2;
  	pthru->ars = 1;
  	pthru->reqsenselen = 14;
  	pthru->islogical = 0;
  
  	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  
  	pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  
  	pthru->cdblen = 6;
  
  	pthru->cdb[0] = INQUIRY;
  	pthru->cdb[1] = 0;
  	pthru->cdb[2] = 0;
  	pthru->cdb[3] = 0;
  	pthru->cdb[4] = 255;
  	pthru->cdb[5] = 0;
  
  
  	pthru->dataxferaddr = (u32)buf_dma_handle;
  	pthru->dataxferlen = 256;
  
  	memset(&mc, 0, sizeof(megacmd_t));
  
  	mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  	mc.xferaddr = (u32)pthru_dma_handle;
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
4127
  	rval = mega_internal_command(adapter, &mc, pthru);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4128
4129
4130
4131
4132
4133
4134
4135
  
  	pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  			pthru_dma_handle);
  
  	free_local_pdev(pdev);
  
  	return rval;
  }
84a3c97b9   walter harms   [SCSI] megaraid: ...
4136
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4137
4138
4139
4140
  
  /**
   * mega_internal_command()
   * @adapter - pointer to our soft state
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4141
4142
4143
4144
4145
4146
4147
   * @mc - the mailbox command
   * @pthru - Passthru structure for DCDB commands
   *
   * Issue the internal commands in interrupt mode.
   * The last argument is the address of the passthru structure if the command
   * to be fired is a passthru command
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4148
4149
4150
   * Note: parameter 'pthru' is null for non-passthru commands.
   */
  static int
cb0258a2f   Christoph Hellwig   [SCSI] megaraid (...
4151
  mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4152
  {
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4153
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4154
4155
4156
4157
4158
4159
4160
4161
  	scb_t	*scb;
  	int	rval;
  
  	/*
  	 * The internal commands share one command id and hence are
  	 * serialized. This is so because we want to reserve maximum number of
  	 * available command ids for the I/O commands.
  	 */
0b9506723   Arjan van de Ven   [SCSI] turn most ...
4162
  	mutex_lock(&adapter->int_mtx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4163
4164
4165
  
  	scb = &adapter->int_scb;
  	memset(scb, 0, sizeof(scb_t));
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4166
4167
  	scb->idx = CMDID_INT_CMDS;
  	scb->state |= SCB_ACTIVE | SCB_PENDQ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4168
4169
4170
4171
4172
4173
  
  	memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  
  	/*
  	 * Is it a passthru command
  	 */
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4174
  	if (mc->cmd == MEGA_MBOXCMD_PASSTHRU)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4175
  		scb->pthru = pthru;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4176

0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
  	spin_lock_irqsave(&adapter->lock, flags);
  	list_add_tail(&scb->list, &adapter->pending_list);
  	/*
  	 * Check if the HBA is in quiescent state, e.g., during a
  	 * delete logical drive opertion. If it is, don't run
  	 * the pending_list.
  	 */
  	if (atomic_read(&adapter->quiescent) == 0)
  		mega_runpendq(adapter);
  	spin_unlock_irqrestore(&adapter->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4187

8d115f845   Christoph Hellwig   [SCSI] remove scs...
4188
  	wait_for_completion(&adapter->int_waitq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4189

0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4190
  	mc->status = rval = adapter->int_status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4191
4192
4193
4194
4195
  
  	/*
  	 * Print a debug message for all failed commands. Applications can use
  	 * this information.
  	 */
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4196
  	if (rval && trace_level) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4197
4198
  		printk("megaraid: cmd [%x, %x, %x] status:[%x]
  ",
0f2bb84d2   Christoph Hellwig   [SCSI] megaraid: ...
4199
  			mc->cmd, mc->opcode, mc->subopcode, rval);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4200
  	}
0b9506723   Arjan van de Ven   [SCSI] turn most ...
4201
  	mutex_unlock(&adapter->int_mtx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4202
4203
  	return rval;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4204
4205
4206
  static struct scsi_host_template megaraid_template = {
  	.module				= THIS_MODULE,
  	.name				= "MegaRAID",
3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
4207
  	.proc_name			= "megaraid_legacy",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
  	.info				= megaraid_info,
  	.queuecommand			= megaraid_queue,	
  	.bios_param			= megaraid_biosparam,
  	.max_sectors			= MAX_SECTORS_PER_IO,
  	.can_queue			= MAX_COMMANDS,
  	.this_id			= DEFAULT_INITIATOR_ID,
  	.sg_tablesize			= MAX_SGLIST,
  	.cmd_per_lun			= DEF_CMD_PER_LUN,
  	.use_clustering			= ENABLE_CLUSTERING,
  	.eh_abort_handler		= megaraid_abort,
  	.eh_device_reset_handler	= megaraid_reset,
  	.eh_bus_reset_handler		= megaraid_reset,
  	.eh_host_reset_handler		= megaraid_reset,
54b2b50c2   Martin K. Petersen   [SCSI] Disable WR...
4221
  	.no_write_same			= 1,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4222
  };
6f0397905   Greg Kroah-Hartman   Drivers: scsi: re...
4223
  static int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
  megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  {
  	struct Scsi_Host *host;
  	adapter_t *adapter;
  	unsigned long mega_baseport, tbase, flag = 0;
  	u16 subsysid, subsysvid;
  	u8 pci_bus, pci_dev_func;
  	int irq, i, j;
  	int error = -ENODEV;
  
  	if (pci_enable_device(pdev))
  		goto out;
  	pci_set_master(pdev);
  
  	pci_bus = pdev->bus->number;
  	pci_dev_func = pdev->devfn;
  
  	/*
  	 * The megaraid3 stuff reports the ID of the Intel part which is not
  	 * remotely specific to the megaraid
  	 */
  	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  		u16 magic;
  		/*
  		 * Don't fall over the Compaq management cards using the same
  		 * PCI identifier
  		 */
  		if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  		    pdev->subsystem_device == 0xC000)
  		   	return -ENODEV;
  		/* Now check the magic signature byte */
  		pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  		if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  			return -ENODEV;
  		/* Ok it is probably a megaraid */
  	}
  
  	/*
  	 * For these vendor and device ids, signature offsets are not
  	 * valid and 64 bit is implicit
  	 */
  	if (id->driver_data & BOARD_64BIT)
  		flag |= BOARD_64BIT;
  	else {
  		u32 magic64;
  
  		pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  		if (magic64 == HBA_SIGNATURE_64BIT)
  			flag |= BOARD_64BIT;
  	}
  
  	subsysvid = pdev->subsystem_vendor;
  	subsysid = pdev->subsystem_device;
  
  	printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  		id->vendor, id->device, pci_bus);
  
  	printk("slot %d:func %d
  ",
  		PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  
  	/* Read the base port and IRQ from PCI */
  	mega_baseport = pci_resource_start(pdev, 0);
  	irq = pdev->irq;
  
  	tbase = mega_baseport;
  	if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  		flag |= BOARD_MEMMAP;
  
  		if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  			printk(KERN_WARNING "megaraid: mem region busy!
  ");
  			goto out_disable_device;
  		}
  
  		mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  		if (!mega_baseport) {
  			printk(KERN_WARNING
  			       "megaraid: could not map hba memory
  ");
  			goto out_release_region;
  		}
  	} else {
  		flag |= BOARD_IOMAP;
  		mega_baseport += 0x10;
  
  		if (!request_region(mega_baseport, 16, "megaraid"))
  			goto out_disable_device;
  	}
  
  	/* Initialize SCSI Host structure */
  	host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  	if (!host)
  		goto out_iounmap;
  
  	adapter = (adapter_t *)host->hostdata;
  	memset(adapter, 0, sizeof(adapter_t));
  
  	printk(KERN_NOTICE
  		"scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d
  ",
  		host->host_no, mega_baseport, irq);
  
  	adapter->base = mega_baseport;
00769ec40   Jeff Garzik   [SCSI] megaraid: ...
4328
4329
  	if (flag & BOARD_MEMMAP)
  		adapter->mmio_base = (void __iomem *) mega_baseport;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4330
4331
4332
4333
4334
4335
4336
  
  	INIT_LIST_HEAD(&adapter->free_list);
  	INIT_LIST_HEAD(&adapter->pending_list);
  	INIT_LIST_HEAD(&adapter->completed_list);
  
  	adapter->flag = flag;
  	spin_lock_init(&adapter->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
  
  	host->cmd_per_lun = max_cmd_per_lun;
  	host->max_sectors = max_sectors_per_io;
  
  	adapter->dev = pdev;
  	adapter->host = host;
  
  	adapter->host->irq = irq;
  
  	if (flag & BOARD_MEMMAP)
  		adapter->host->base = tbase;
  	else {
  		adapter->host->io_port = tbase;
  		adapter->host->n_io_port = 16;
  	}
  
  	adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  
  	/*
  	 * Allocate buffer to issue internal commands.
  	 */
  	adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  		MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  	if (!adapter->mega_buffer) {
  		printk(KERN_WARNING "megaraid: out of RAM.
  ");
  		goto out_host_put;
  	}
  
  	adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  	if (!adapter->scb_list) {
  		printk(KERN_WARNING "megaraid: out of RAM.
  ");
  		goto out_free_cmd_buffer;
  	}
  
  	if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  				megaraid_isr_memmapped : megaraid_isr_iomapped,
1d6f359a2   Thomas Gleixner   [PATCH] irq-flags...
4375
  					IRQF_SHARED, "megaraid", adapter)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
  		printk(KERN_WARNING
  			"megaraid: Couldn't register IRQ %d!
  ", irq);
  		goto out_free_scb_list;
  	}
  
  	if (mega_setup_mailbox(adapter))
  		goto out_free_irq;
  
  	if (mega_query_adapter(adapter))
  		goto out_free_mbox;
  
  	/*
  	 * Have checks for some buggy f/w
  	 */
  	if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  		/*
  		 * Which firmware
  		 */
  		if (!strcmp(adapter->fw_version, "3.00") ||
  				!strcmp(adapter->fw_version, "3.01")) {
  
  			printk( KERN_WARNING
  				"megaraid: Your  card is a Dell PERC "
  				"2/SC RAID controller with  "
  				"firmware
  megaraid: 3.00 or 3.01.  "
  				"This driver is known to have "
  				"corruption issues
  megaraid: with "
  				"those firmware versions on this "
  				"specific card.  In order
  megaraid: "
  				"to protect your data, please upgrade "
  				"your firmware to version
  megaraid: "
  				"3.10 or later, available from the "
  				"Dell Technical Support web
  "
  				"megaraid: site at
  http://support."
  				"dell.com/us/en/filelib/download/"
  				"index.asp?fileid=2940
  "
  			);
  		}
  	}
  
  	/*
  	 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  	 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  	 * support, since this firmware cannot handle 64 bit
  	 * addressing
  	 */
54ebfd578   Jon Mason   megaraid: remove ...
4430
  	if ((subsysvid == PCI_VENDOR_ID_HP) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
  	    ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  		/*
  		 * which firmware
  		 */
  		if (!strcmp(adapter->fw_version, "H01.07") ||
  		    !strcmp(adapter->fw_version, "H01.08") ||
  		    !strcmp(adapter->fw_version, "H01.09") ) {
  			printk(KERN_WARNING
  				"megaraid: Firmware H.01.07, "
  				"H.01.08, and H.01.09 on 1M/2M "
  				"controllers
  "
  				"megaraid: do not support 64 bit "
  				"addressing.
  megaraid: DISABLING "
  				"64 bit support.
  ");
  			adapter->flag &= ~BOARD_64BIT;
  		}
  	}
  
  	if (mega_is_bios_enabled(adapter))
  		mega_hbas[hba_count].is_bios_enabled = 1;
  	mega_hbas[hba_count].hostdata_addr = adapter;
  
  	/*
  	 * Find out which channel is raid and which is scsi. This is
  	 * for ROMB support.
  	 */
  	mega_enum_raid_scsi(adapter);
  
  	/*
  	 * Find out if a logical drive is set as the boot drive. If
  	 * there is one, will make that as the first logical drive.
  	 * ROMB: Do we have to boot from a physical drive. Then all
  	 * the physical drives would appear before the logical disks.
  	 * Else, all the physical drives would be exported to the mid
  	 * layer after logical drives.
  	 */
  	mega_get_boot_drv(adapter);
  
  	if (adapter->boot_pdrv_enabled) {
  		j = adapter->product_info.nchannels;
  		for( i = 0; i < j; i++ )
  			adapter->logdrv_chan[i] = 0;
  		for( i = j; i < NVIRT_CHAN + j; i++ )
  			adapter->logdrv_chan[i] = 1;
  	} else {
  		for (i = 0; i < NVIRT_CHAN; i++)
  			adapter->logdrv_chan[i] = 1;
  		for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  			adapter->logdrv_chan[i] = 0;
  		adapter->mega_ch_class <<= NVIRT_CHAN;
  	}
  
  	/*
  	 * Do we support random deletion and addition of logical
  	 * drives
  	 */
  	adapter->read_ldidmap = 0;	/* set it after first logdrv
  						   delete cmd */
  	adapter->support_random_del = mega_support_random_del(adapter);
  
  	/* Initialize SCBs */
  	if (mega_init_scb(adapter))
  		goto out_free_mbox;
  
  	/*
  	 * Reset the pending commands counter
  	 */
  	atomic_set(&adapter->pend_cmds, 0);
  
  	/*
  	 * Reset the adapter quiescent flag
  	 */
  	atomic_set(&adapter->quiescent, 0);
  
  	hba_soft_state[hba_count] = adapter;
  
  	/*
  	 * Fill in the structure which needs to be passed back to the
  	 * application when it does an ioctl() for controller related
  	 * information.
  	 */
  	i = hba_count;
  
  	mcontroller[i].base = mega_baseport;
  	mcontroller[i].irq = irq;
  	mcontroller[i].numldrv = adapter->numldrv;
  	mcontroller[i].pcibus = pci_bus;
  	mcontroller[i].pcidev = id->device;
  	mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  	mcontroller[i].pciid = -1;
  	mcontroller[i].pcivendor = id->vendor;
  	mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  	mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  
  
  	/* Set the Mode of addressing to 64 bit if we can */
  	if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
6a35528a8   Yang Hongyang   dma-mapping: repl...
4531
  		pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4532
4533
  		adapter->has_64bit_addr = 1;
  	} else  {
284901a90   Yang Hongyang   dma-mapping: repl...
4534
  		pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4535
4536
4537
  		adapter->has_64bit_addr = 0;
  	}
  		
0b9506723   Arjan van de Ven   [SCSI] turn most ...
4538
  	mutex_init(&adapter->int_mtx);
8d115f845   Christoph Hellwig   [SCSI] remove scs...
4539
  	init_completion(&adapter->int_waitq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
  
  	adapter->this_id = DEFAULT_INITIATOR_ID;
  	adapter->host->this_id = DEFAULT_INITIATOR_ID;
  
  #if MEGA_HAVE_CLUSTERING
  	/*
  	 * Is cluster support enabled on this controller
  	 * Note: In a cluster the HBAs ( the initiators ) will have
  	 * different target IDs and we cannot assume it to be 7. Call
  	 * to mega_support_cluster() will get the target ids also if
  	 * the cluster support is available
  	 */
  	adapter->has_cluster = mega_support_cluster(adapter);
  	if (adapter->has_cluster) {
  		printk(KERN_NOTICE
  			"megaraid: Cluster driver, initiator id:%d
  ",
  			adapter->this_id);
  	}
  #endif
  
  	pci_set_drvdata(pdev, host);
  
  	mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  
  	error = scsi_add_host(host, &pdev->dev);
  	if (error)
  		goto out_free_mbox;
  
  	scsi_scan_host(host);
  	hba_count++;
  	return 0;
  
   out_free_mbox:
  	pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  			adapter->una_mbox64, adapter->una_mbox64_dma);
   out_free_irq:
  	free_irq(adapter->host->irq, adapter);
   out_free_scb_list:
  	kfree(adapter->scb_list);
   out_free_cmd_buffer:
  	pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  			adapter->mega_buffer, adapter->buf_dma_handle);
   out_host_put:
  	scsi_host_put(host);
   out_iounmap:
  	if (flag & BOARD_MEMMAP)
  		iounmap((void *)mega_baseport);
   out_release_region:
  	if (flag & BOARD_MEMMAP)
  		release_mem_region(tbase, 128);
  	else
  		release_region(mega_baseport, 16);
   out_disable_device:
  	pci_disable_device(pdev);
   out:
  	return error;
  }
  
  static void
  __megaraid_shutdown(adapter_t *adapter)
  {
  	u_char	raw_mbox[sizeof(struct mbox_out)];
  	mbox_t	*mbox = (mbox_t *)raw_mbox;
  	int	i;
  
  	/* Flush adapter cache */
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  	raw_mbox[0] = FLUSH_ADAPTER;
  
  	free_irq(adapter->host->irq, adapter);
  
  	/* Issue a blocking (interrupts disabled) command to the card */
  	issue_scb_block(adapter, raw_mbox);
  
  	/* Flush disks cache */
  	memset(&mbox->m_out, 0, sizeof(raw_mbox));
  	raw_mbox[0] = FLUSH_SYSTEM;
  
  	/* Issue a blocking (interrupts disabled) command to the card */
  	issue_scb_block(adapter, raw_mbox);
  	
  	if (atomic_read(&adapter->pend_cmds) > 0)
  		printk(KERN_WARNING "megaraid: pending commands!!
  ");
  
  	/*
  	 * Have a delibrate delay to make sure all the caches are
  	 * actually flushed.
  	 */
  	for (i = 0; i <= 10; i++)
  		mdelay(1000);
  }
6f0397905   Greg Kroah-Hartman   Drivers: scsi: re...
4633
  static void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4634
4635
4636
4637
  megaraid_remove_one(struct pci_dev *pdev)
  {
  	struct Scsi_Host *host = pci_get_drvdata(pdev);
  	adapter_t *adapter = (adapter_t *)host->hostdata;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
  
  	scsi_remove_host(host);
  
  	__megaraid_shutdown(adapter);
  
  	/* Free our resources */
  	if (adapter->flag & BOARD_MEMMAP) {
  		iounmap((void *)adapter->base);
  		release_mem_region(adapter->host->base, 128);
  	} else
  		release_region(adapter->base, 16);
  
  	mega_free_sgl(adapter);
  
  #ifdef CONFIG_PROC_FS
  	if (adapter->controller_proc_dir_entry) {
  		remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  		remove_proc_entry("config",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("mailbox",
  				adapter->controller_proc_dir_entry);
  #if MEGA_HAVE_ENH_PROC
  		remove_proc_entry("rebuild-rate",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("battery-status",
  				adapter->controller_proc_dir_entry);
  
  		remove_proc_entry("diskdrives-ch0",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("diskdrives-ch1",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("diskdrives-ch2",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("diskdrives-ch3",
  				adapter->controller_proc_dir_entry);
  
  		remove_proc_entry("raiddrives-0-9",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("raiddrives-10-19",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("raiddrives-20-29",
  				adapter->controller_proc_dir_entry);
  		remove_proc_entry("raiddrives-30-39",
  				adapter->controller_proc_dir_entry);
  #endif
84a3c97b9   walter harms   [SCSI] megaraid: ...
4683
4684
4685
4686
4687
  		{
  			char	buf[12] = { 0 };
  			sprintf(buf, "hba%d", adapter->host->host_no);
  			remove_proc_entry(buf, mega_proc_dir_entry);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
  	}
  #endif
  
  	pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  			adapter->mega_buffer, adapter->buf_dma_handle);
  	kfree(adapter->scb_list);
  	pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  			adapter->una_mbox64, adapter->una_mbox64_dma);
  
  	scsi_host_put(host);
  	pci_disable_device(pdev);
  
  	hba_count--;
  }
  
  static void
d18c3db58   Greg Kroah-Hartman   [PATCH] PCI: make...
4704
  megaraid_shutdown(struct pci_dev *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4705
  {
d18c3db58   Greg Kroah-Hartman   [PATCH] PCI: make...
4706
  	struct Scsi_Host *host = pci_get_drvdata(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4707
4708
4709
4710
4711
4712
  	adapter_t *adapter = (adapter_t *)host->hostdata;
  
  	__megaraid_shutdown(adapter);
  }
  
  static struct pci_device_id megaraid_pci_tbl[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4713
4714
4715
4716
  	{PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  	{PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4717
4718
  	{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4719
4720
4721
4722
4723
  	{0,}
  };
  MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  
  static struct pci_driver megaraid_pci_driver = {
3542adcb3   Ju, Seokmann   [SCSI] megaraid_l...
4724
  	.name		= "megaraid_legacy",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4725
4726
  	.id_table	= megaraid_pci_tbl,
  	.probe		= megaraid_probe_one,
6f0397905   Greg Kroah-Hartman   Drivers: scsi: re...
4727
  	.remove		= megaraid_remove_one,
d18c3db58   Greg Kroah-Hartman   [PATCH] PCI: make...
4728
  	.shutdown	= megaraid_shutdown,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
  };
  
  static int __init megaraid_init(void)
  {
  	int error;
  
  	if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  		max_cmd_per_lun = MAX_CMD_PER_LUN;
  	if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  		max_mbox_busy_wait = MBOX_BUSY_WAIT;
  
  #ifdef CONFIG_PROC_FS
c74c120a2   Alexey Dobriyan   proc: remove proc...
4741
  	mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4742
4743
4744
4745
4746
4747
  	if (!mega_proc_dir_entry) {
  		printk(KERN_WARNING
  				"megaraid: failed to create megaraid root
  ");
  	}
  #endif
4520b0089   Richard Knutsson   [SCSI] megaraid: ...
4748
  	error = pci_register_driver(&megaraid_pci_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4749
4750
  	if (error) {
  #ifdef CONFIG_PROC_FS
c74c120a2   Alexey Dobriyan   proc: remove proc...
4751
  		remove_proc_entry("megaraid", NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
  #endif
  		return error;
  	}
  
  	/*
  	 * Register the driver as a character device, for applications
  	 * to access it for ioctls.
  	 * First argument (major) to register_chrdev implies a dynamic
  	 * major number allocation.
  	 */
3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
4762
  	major = register_chrdev(0, "megadev_legacy", &megadev_fops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
  	if (!major) {
  		printk(KERN_WARNING
  				"megaraid: failed to register char device
  ");
  	}
  
  	return 0;
  }
  
  static void __exit megaraid_exit(void)
  {
  	/*
  	 * Unregister the character device interface to the driver.
  	 */
3492b3288   Ju, Seokmann   [SCSI] megaraid_l...
4777
  	unregister_chrdev(major, "megadev_legacy");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4778
4779
4780
4781
  
  	pci_unregister_driver(&megaraid_pci_driver);
  
  #ifdef CONFIG_PROC_FS
c74c120a2   Alexey Dobriyan   proc: remove proc...
4782
  	remove_proc_entry("megaraid", NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4783
4784
4785
4786
4787
4788
4789
  #endif
  }
  
  module_init(megaraid_init);
  module_exit(megaraid_exit);
  
  /* vi: set ts=8 sw=8 tw=78: */