1 /*
2  *
3  *			Linux MegaRAID device driver
4  *
5  * Copyright (c) 2002  LSI Logic Corporation.
6  *
7  *	   This program is free software; you can redistribute it and/or
8  *	   modify it under the terms of the GNU General Public License
9  *	   as published by the Free Software Foundation; either version
10  *	   2 of the License, or (at your option) any later version.
11  *
12  * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
13  *	  - fixes
14  *	  - speed-ups (list handling fixes, issued_list, optimizations.)
15  *	  - lots of cleanups.
16  *
17  * Version : v2.10.10.1 (January 27, 2005)
18  *
19  * Authors:	Atul Mukker <Atul.Mukker@lsil.com>
20  *		Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
21  *
22  * Description: Linux device driver for LSI Logic MegaRAID controller
23  *
24  * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
25  *					518, 520, 531, 532
26  *
27  * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
28  * and others. Please send updates to the public mailing list
29  *
30  * For history of changes, see ChangeLog.megaraid.
31  *
32  */
33 
34 #include <linux/mm.h>
35 #include <linux/fs.h>
36 #include <linux/blk.h>
37 #include <asm/uaccess.h>
38 #include <linux/delay.h>
39 #include <linux/reboot.h>
40 #include <linux/module.h>
41 #include <linux/list.h>
42 
43 #include "sd.h"
44 #include "scsi.h"
45 #include "hosts.h"
46 
47 #include "megaraid2.h"
48 
49 #ifdef LSI_CONFIG_COMPAT
50 #include <asm/ioctl32.h>
51 #endif
52 
53 MODULE_AUTHOR ("LSI Logic Corporation");
54 MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
55 MODULE_LICENSE ("GPL");
56 
57 static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
58 MODULE_PARM(max_cmd_per_lun, "i");
59 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)");
60 
61 static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
62 MODULE_PARM(max_sectors_per_io, "h");
63 MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
64 
65 
66 static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
67 MODULE_PARM(max_mbox_busy_wait, "h");
68 MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
69 
70 #define RDINDOOR(adapter)		readl((adapter)->base + 0x20)
71 #define RDOUTDOOR(adapter)		readl((adapter)->base + 0x2C)
72 #define WRINDOOR(adapter,value)		writel(value, (adapter)->base + 0x20)
73 #define WROUTDOOR(adapter,value)	writel(value, (adapter)->base + 0x2C)
74 
75 /*
76  * Global variables
77  */
78 
79 static int hba_count;
80 static adapter_t *hba_soft_state[MAX_CONTROLLERS];
81 #ifdef CONFIG_PROC_FS
82 static struct proc_dir_entry *mega_proc_dir_entry;
83 #endif
84 
85 static struct notifier_block mega_notifier = {
86 	.notifier_call = megaraid_reboot_notify
87 };
88 
89 /* For controller re-ordering */
90 static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
91 
92 /*
93  * Lock to protect access to IOCTL
94  */
95 static struct semaphore megaraid_ioc_mtx;
96 
97 /*
98  * The File Operations structure for the serial/ioctl interface of the driver
99  */
100 static struct file_operations megadev_fops = {
101 	.ioctl		= megadev_ioctl_entry,
102 	.open		= megadev_open,
103 	.release	= megadev_close,
104 	.owner		= THIS_MODULE,
105 };
106 
107 /*
108  * Array to structures for storing the information about the controllers. This
109  * information is sent to the user level applications, when they do an ioctl
110  * for this information.
111  */
112 static struct mcontroller mcontroller[MAX_CONTROLLERS];
113 
114 /* The current driver version */
115 static u32 driver_ver = 0x02104000;
116 
117 /* major number used by the device for character interface */
118 static int major;
119 
120 #define IS_RAID_CH(hba, ch)	(((hba)->mega_ch_class >> (ch)) & 0x01)
121 
122 
123 /*
124  * Debug variable to print some diagnostic messages
125  */
126 static int trace_level;
127 
128 /*
129  * megaraid_validate_parms()
130  *
131  * Validate that any module parms passed in
132  * have proper values.
133  */
134 static void
megaraid_validate_parms(void)135 megaraid_validate_parms(void)
136 {
137 	if( (max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN) )
138 		max_cmd_per_lun = MAX_CMD_PER_LUN;
139 	if( max_mbox_busy_wait > MBOX_BUSY_WAIT )
140 		max_mbox_busy_wait = MBOX_BUSY_WAIT;
141 }
142 
143 
144 /**
145  * megaraid_detect()
146  * @host_template - Our soft state maintained by mid-layer
147  *
148  * the detect entry point for the mid-layer.
149  * We scan the PCI bus for our controllers and start them.
150  *
151  * Note: PCI_DEVICE_ID_PERC4_DI below represents the PERC4/Di class of
152  * products. All of them share the same vendor id, device id, and subsystem
153  * vendor id but different subsystem ids. As of now, driver does not use the
154  * subsystem id.
155  * PERC4E device ids are for the PCI-Express controllers
156  */
157 static int
megaraid_detect(Scsi_Host_Template * host_template)158 megaraid_detect(Scsi_Host_Template *host_template)
159 {
160 	int	i;
161 	u16	dev_sw_table[] = {	/* Table of all supported
162 					   vendor/device ids */
163 
164 		PCI_VENDOR_ID_LSI_LOGIC,	PCI_DEVICE_ID_LSI_SATA_PCIX,
165 		PCI_VENDOR_ID_LSI_LOGIC,	PCI_DEVICE_ID_PERC4E_DC_SC,
166 		PCI_VENDOR_ID_DELL,		PCI_DEVICE_ID_PERC4E_SI_DI,
167 		PCI_VENDOR_ID_DELL,		PCI_DEVICE_ID_DISCOVERY,
168 		PCI_VENDOR_ID_DELL,		PCI_DEVICE_ID_PERC4_DI,
169 		PCI_VENDOR_ID_LSI_LOGIC,	PCI_DEVICE_ID_PERC4_QC_VERDE,
170 		PCI_VENDOR_ID_AMI,		PCI_DEVICE_ID_AMI_MEGARAID,
171 		PCI_VENDOR_ID_AMI,		PCI_DEVICE_ID_AMI_MEGARAID2,
172 		PCI_VENDOR_ID_AMI,		PCI_DEVICE_ID_AMI_MEGARAID3,
173 		PCI_VENDOR_ID_INTEL,		PCI_DEVICE_ID_AMI_MEGARAID3,
174 		PCI_VENDOR_ID_LSI_LOGIC,	PCI_DEVICE_ID_AMI_MEGARAID3 };
175 
176 
177 	printk(KERN_NOTICE "megaraid: " MEGARAID_VERSION);
178 
179 	megaraid_validate_parms();
180 
181 	/*
182 	 * Scan PCI bus for our all devices.
183 	 */
184 	for( i = 0; i < ((int) (sizeof(dev_sw_table)/sizeof(u16))); i += 2 ) {
185 
186 		mega_find_card(host_template, dev_sw_table[i],
187 				dev_sw_table[i+1]);
188 	}
189 
190 	if(hba_count) {
191 		/*
192 		 * re-order hosts so that one with bootable logical drive
193 		 * comes first
194 		 */
195 		mega_reorder_hosts();
196 
197 		/*
198 		 * Initialize the IOCTL lock
199 		 */
200 		init_MUTEX( &megaraid_ioc_mtx );
201 
202 #ifdef CONFIG_PROC_FS
203 		mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
204 
205 		if(!mega_proc_dir_entry) {
206 			printk(KERN_WARNING
207 				"megaraid: failed to create megaraid root\n");
208 		}
209 		else {
210 			for(i = 0; i < hba_count; i++) {
211 				mega_create_proc_entry(i, mega_proc_dir_entry);
212 			}
213 		}
214 #endif
215 
216 		/*
217 		 * Register the driver as a character device, for applications
218 		 * to access it for ioctls.
219 		 * First argument (major) to register_chrdev implies a dynamic
220 		 * major number allocation.
221 		 */
222 		major = register_chrdev(0, "megadev", &megadev_fops);
223 
224 		if (major < 0) {
225 			printk(KERN_WARNING
226 				"megaraid: failed to register char device.\n");
227 		}
228 		/*
229 		 * Register the Shutdown Notification hook in kernel
230 		 */
231 		if(register_reboot_notifier(&mega_notifier)) {
232 			printk(KERN_WARNING
233 				"MegaRAID Shutdown routine not registered!!\n");
234 		}
235 
236 #ifdef LSI_CONFIG_COMPAT
237 		/*
238 		 * Register the 32-bit ioctl conversion
239 		 */
240 		register_ioctl32_conversion(MEGAIOCCMD, megadev_compat_ioctl);
241 #endif
242 
243 	}
244 
245 	return hba_count;
246 }
247 
248 
249 
250 /**
251  * mega_find_card() - find and start this controller
252  * @host_template - Our soft state maintained by mid-layer
253  * @pci_vendor - pci vendor id for this controller
254  * @pci_device - pci device id for this controller
255  *
256  * Scans the PCI bus for this vendor and device id combination, setup the
257  * resources, and register ourselves as a SCSI HBA driver, and setup all
258  * parameters for our soft state.
259  *
260  * This routine also checks for some buggy firmware and ajust the flags
261  * accordingly.
262  */
263 static void
mega_find_card(Scsi_Host_Template * host_template,u16 pci_vendor,u16 pci_device)264 mega_find_card(Scsi_Host_Template *host_template, u16 pci_vendor,
265 	u16 pci_device)
266 {
267 	struct Scsi_Host	*host = NULL;
268 	adapter_t	*adapter = NULL;
269 	u32	magic64;
270 	unsigned long	mega_baseport;
271 	u16	subsysid, subsysvid;
272 	u8	pci_bus;
273 	u8	pci_dev_func;
274 	u8	irq;
275 	struct pci_dev	*pdev = NULL;
276 	u8	did_ioremap_f = 0;
277 	u8	did_req_region_f = 0;
278 	u8	did_scsi_reg_f = 0;
279 	u8	alloc_int_buf_f = 0;
280 	u8	alloc_scb_f = 0;
281 	u8	got_irq_f = 0;
282 	u8	did_setup_mbox_f = 0;
283 	unsigned long	tbase;
284 	unsigned long	flag = 0;
285 	int	i, j;
286 	u8	did_int_pthru_f	= 0;
287 	u8	did_int_data_f	= 0;
288 
289 	while((pdev = pci_find_device(pci_vendor, pci_device, pdev))) {
290 
291 		// reset flags for all controllers in this class
292 		did_ioremap_f = 0;
293 		did_req_region_f = 0;
294 		did_scsi_reg_f = 0;
295 		alloc_int_buf_f = 0;
296 		alloc_scb_f = 0;
297 		got_irq_f = 0;
298 		did_setup_mbox_f = 0;
299 
300 		if(pci_enable_device (pdev)) continue;
301 
302 		pci_bus = pdev->bus->number;
303 		pci_dev_func = pdev->devfn;
304 
305 		/*
306 		 * For these vendor and device ids, signature offsets are not
307 		 * valid and 64 bit is implicit
308 		 */
309 		if( (pci_vendor == PCI_VENDOR_ID_DELL &&
310 				pci_device == PCI_DEVICE_ID_PERC4_DI) ||
311 			(pci_vendor == PCI_VENDOR_ID_LSI_LOGIC &&
312 				pci_device == PCI_DEVICE_ID_PERC4_QC_VERDE) ||
313 			(pci_vendor == PCI_VENDOR_ID_LSI_LOGIC &&
314 				pci_device == PCI_DEVICE_ID_PERC4E_DC_SC) ||
315 			(pci_vendor == PCI_VENDOR_ID_DELL &&
316 				pci_device == PCI_DEVICE_ID_PERC4E_SI_DI) ||
317 			(pci_vendor == PCI_VENDOR_ID_LSI_LOGIC &&
318 				pci_device == PCI_DEVICE_ID_LSI_SATA_PCIX)) {
319 
320 			flag |= BOARD_64BIT;
321 		}
322 		else {
323 			pci_read_config_dword(pdev, PCI_CONF_AMISIG64,
324 					&magic64);
325 
326 			if (magic64 == HBA_SIGNATURE_64BIT)
327 				flag |= BOARD_64BIT;
328 		}
329 
330 		subsysvid = pdev->subsystem_vendor;
331 		subsysid = pdev->subsystem_device;
332 
333 		/*
334 		 * If we do not find the valid subsys vendor id, refuse to
335 		 * load the driver. This is part of PCI200X compliance
336 		 * We load the driver if subsysvid is 0.
337 		 */
338 		if( subsysvid && (subsysvid != AMI_SUBSYS_VID) &&
339 				(subsysvid != DELL_SUBSYS_VID) &&
340 				(subsysvid != HP_SUBSYS_VID) &&
341 				(subsysvid != INTEL_SUBSYS_VID) &&
342 				(subsysvid != FSC_SUBSYS_VID) &&
343 				(subsysvid != ACER_SUBSYS_VID) &&
344 				(subsysvid != NEC_SUBSYS_VID) &&
345 				(subsysvid != LSI_SUBSYS_VID) ) continue;
346 
347 
348 		printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
349 			pci_vendor, pci_device, pci_bus);
350 
351 		printk("slot %d:func %d\n",
352 			PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
353 
354 		/* Read the base port and IRQ from PCI */
355 		mega_baseport = pci_resource_start(pdev, 0);
356 		irq = pdev->irq;
357 
358 		tbase = mega_baseport;
359 
360 		if( pci_resource_flags(pdev, 0) & IORESOURCE_MEM ) {
361 
362 			if( check_mem_region(mega_baseport, 128) ) {
363 				printk(KERN_WARNING
364 					"megaraid: mem region busy!\n");
365 				continue;
366 			}
367 			request_mem_region(mega_baseport, 128,
368 					"MegaRAID: LSI Logic Corporation.");
369 
370 			mega_baseport =
371 				(unsigned long)ioremap(mega_baseport, 128);
372 
373 			if( !mega_baseport ) {
374 				printk(KERN_WARNING
375 					"megaraid: could not map hba memory\n");
376 
377 				release_mem_region(tbase, 128);
378 
379 				continue;
380 			}
381 
382 			flag |= BOARD_MEMMAP;
383 
384 			did_ioremap_f = 1;
385 		}
386 		else {
387 			mega_baseport += 0x10;
388 
389 			if( !request_region(mega_baseport, 16, "megaraid") )
390 				goto fail_attach;
391 
392 			flag |= BOARD_IOMAP;
393 
394 			did_req_region_f = 1;
395 		}
396 
397 		/* Initialize SCSI Host structure */
398 		host = scsi_register(host_template, sizeof(adapter_t));
399 
400 		if(!host) goto fail_attach;
401 
402 		did_scsi_reg_f = 1;
403 
404 		scsi_set_pci_device(host, pdev);
405 
406 		adapter = (adapter_t *)host->hostdata;
407 		memset(adapter, 0, sizeof(adapter_t));
408 
409 		printk(KERN_NOTICE
410 			"scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
411 			host->host_no, mega_baseport, irq);
412 
413 		adapter->base = mega_baseport;
414 
415 		/* Copy resource info into structure */
416 		INIT_LIST_HEAD(&adapter->free_list);
417 		INIT_LIST_HEAD(&adapter->pending_list);
418 
419 		adapter->flag = flag;
420 		spin_lock_init(&adapter->lock);
421 
422 #ifdef SCSI_HAS_HOST_LOCK
423 #  if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,9)
424 		/* This is the Red Hat AS2.1 kernel */
425 		adapter->host_lock = &adapter->lock;
426 		host->lock = adapter->host_lock;
427 #  elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
428 		/* This is the later Red Hat 2.4 kernels */
429 		adapter->host_lock = &adapter->lock;
430 		host->host_lock = adapter->host_lock;
431 #  else
432 		/* This is the 2.6 and later kernel series */
433 		adapter->host_lock = &adapter->lock;
434 		scsi_set_host_lock(&adapter->lock);
435 #  endif
436 #else
437 		/* And this is the remainder of the 2.4 kernel
438 		series */
439 		adapter->host_lock = &io_request_lock;
440 #endif
441 
442 		host->cmd_per_lun = max_cmd_per_lun;
443 		host->max_sectors = max_sectors_per_io;
444 
445 		adapter->dev = pdev;
446 		adapter->host = host;
447 
448 		adapter->host->irq = irq;
449 
450 		if( flag & BOARD_MEMMAP ) {
451 			adapter->host->base = tbase;
452 		}
453 		else {
454 			adapter->host->io_port = tbase;
455 			adapter->host->n_io_port = 16;
456 		}
457 
458 		adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
459 
460 		/*
461 		 * Allocate buffer to issue internal commands.
462 		 */
463 		adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
464 			MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
465 
466 		if( !adapter->mega_buffer ) {
467 			printk(KERN_WARNING "megaraid: out of RAM.\n");
468 			goto fail_attach;
469 		}
470 		alloc_int_buf_f = 1;
471 
472 		adapter->scb_list = kmalloc(sizeof(scb_t)*MAX_COMMANDS,
473 				GFP_KERNEL);
474 
475 		if(!adapter->scb_list) {
476 			printk(KERN_WARNING "megaraid: out of RAM.\n");
477 			goto fail_attach;
478 		}
479 
480 		alloc_scb_f = 1;
481 
482 		/*
483 		 * Allocate memory for ioctls
484 		 */
485 		adapter->int_pthru = pci_alloc_consistent (
486 					adapter->dev,
487 					sizeof(mega_passthru),
488 					&adapter->int_pthru_dma_hndl );
489 
490 		if( adapter->int_pthru == NULL ) {
491 			printk(KERN_WARNING "megaraid: out of RAM.\n");
492 			goto fail_attach;
493 		}
494 		else
495 			did_int_pthru_f = 1;
496 
497 		adapter->int_data = pci_alloc_consistent (
498 					adapter->dev,
499 					INT_MEMBLK_SZ,
500 					&adapter->int_data_dma_hndl );
501 
502 		if( adapter->int_data == NULL ) {
503 			printk(KERN_WARNING "megaraid: out of RAM.\n");
504 			goto fail_attach;
505 		}
506 		else
507 			did_int_data_f = 1;
508 
509 		/* Request our IRQ */
510 		if( adapter->flag & BOARD_MEMMAP ) {
511 			if(request_irq(irq, megaraid_isr_memmapped, SA_SHIRQ,
512 						"megaraid", adapter)) {
513 				printk(KERN_WARNING
514 					"megaraid: Couldn't register IRQ %d!\n",
515 					irq);
516 				goto fail_attach;
517 			}
518 		}
519 		else {
520 			if(request_irq(irq, megaraid_isr_iomapped, SA_SHIRQ,
521 						"megaraid", adapter)) {
522 				printk(KERN_WARNING
523 					"megaraid: Couldn't register IRQ %d!\n",
524 					irq);
525 				goto fail_attach;
526 			}
527 		}
528 		got_irq_f = 1;
529 
530 		if( mega_setup_mailbox(adapter) != 0 )
531 			goto fail_attach;
532 
533 		did_setup_mbox_f = 1;
534 
535 		if( mega_query_adapter(adapter) != 0 )
536 			goto fail_attach;
537 
538 		/*
539 		 * Have checks for some buggy f/w
540 		 */
541 		if((subsysid == 0x1111) && (subsysvid == 0x1111)) {
542 			/*
543 			 * Which firmware
544 			 */
545 			if (!strcmp(adapter->fw_version, "3.00") ||
546 					!strcmp(adapter->fw_version, "3.01")) {
547 
548 				printk( KERN_WARNING
549 					"megaraid: Your  card is a Dell PERC "
550 					"2/SC RAID controller with  "
551 					"firmware\nmegaraid: 3.00 or 3.01.  "
552 					"This driver is known to have "
553 					"corruption issues\nmegaraid: with "
554 					"those firmware versions on this "
555 					"specific card.  In order\nmegaraid: "
556 					"to protect your data, please upgrade "
557 					"your firmware to version\nmegaraid: "
558 					"3.10 or later, available from the "
559 					"Dell Technical Support web\n"
560 					"megaraid: site at\nhttp://support."
561 					"dell.com/us/en/filelib/download/"
562 					"index.asp?fileid=2940\n"
563 				);
564 			}
565 		}
566 
567 		/*
568 		 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
569 		 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
570 		 * support, since this firmware cannot handle 64 bit
571 		 * addressing
572 		 */
573 
574 		if((subsysvid == HP_SUBSYS_VID) &&
575 				((subsysid == 0x60E7)||(subsysid == 0x60E8))) {
576 
577 			/*
578 			 * which firmware
579 			 */
580 			if( !strcmp(adapter->fw_version, "H01.07") ||
581 				!strcmp(adapter->fw_version, "H01.08") ||
582 				!strcmp(adapter->fw_version, "H01.09") ) {
583 
584 				printk(KERN_WARNING
585 					"megaraid: Firmware H.01.07, "
586 					"H.01.08, and H.01.09 on 1M/2M "
587 					"controllers\n"
588 					"megaraid: do not support 64 bit "
589 					"addressing.\nmegaraid: DISABLING "
590 					"64 bit support.\n");
591 				adapter->flag &= ~BOARD_64BIT;
592 			}
593 		}
594 
595 
596 		if(mega_is_bios_enabled(adapter)) {
597 			mega_hbas[hba_count].is_bios_enabled = 1;
598 		}
599 		mega_hbas[hba_count].hostdata_addr = adapter;
600 
601 		/*
602 		 * Find out which channel is raid and which is scsi. This is
603 		 * for ROMB support.
604 		 */
605 		mega_enum_raid_scsi(adapter);
606 
607 		/*
608 		 * Find out if a logical drive is set as the boot drive. If
609 		 * there is one, will make that as the first logical drive.
610 		 * ROMB: Do we have to boot from a physical drive. Then all
611 		 * the physical drives would appear before the logical disks.
612 		 * Else, all the physical drives would be exported to the mid
613 		 * layer after logical drives.
614 		 */
615 		mega_get_boot_drv(adapter);
616 
617 		if( ! adapter->boot_pdrv_enabled ) {
618 			for( i = 0; i < NVIRT_CHAN; i++ )
619 				adapter->logdrv_chan[i] = 1;
620 
621 			for( i = NVIRT_CHAN; i<MAX_CHANNELS+NVIRT_CHAN; i++ )
622 				adapter->logdrv_chan[i] = 0;
623 
624 			adapter->mega_ch_class <<= NVIRT_CHAN;
625 		}
626 		else {
627 			j = adapter->product_info.nchannels;
628 			for( i = 0; i < j; i++ )
629 				adapter->logdrv_chan[i] = 0;
630 
631 			for( i = j; i < NVIRT_CHAN + j; i++ )
632 				adapter->logdrv_chan[i] = 1;
633 		}
634 
635 
636 		/*
637 		 * Do we support random deletion and addition of logical
638 		 * drives
639 		 */
640 		adapter->read_ldidmap = 0;	/* set it after first logdrv
641 						   delete cmd */
642 		adapter->support_random_del = mega_support_random_del(adapter);
643 
644 		/* Initialize SCBs */
645 		if(mega_init_scb(adapter)) {
646 			goto fail_attach;
647 		}
648 
649 		/*
650 		 * Reset the pending commands counter
651 		 */
652 		atomic_set(&adapter->pend_cmds, 0);
653 
654 		/*
655 		 * Reset the adapter quiescent flag
656 		 */
657 		atomic_set(&adapter->quiescent, 0);
658 
659 		hba_soft_state[hba_count] = adapter;
660 
661 		/*
662 		 * Fill in the structure which needs to be passed back to the
663 		 * application when it does an ioctl() for controller related
664 		 * information.
665 		 */
666 		i = hba_count;
667 
668 		mcontroller[i].base = mega_baseport;
669 		mcontroller[i].irq = irq;
670 		mcontroller[i].numldrv = adapter->numldrv;
671 		mcontroller[i].pcibus = pci_bus;
672 		mcontroller[i].pcidev = pci_device;
673 		mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
674 		mcontroller[i].pciid = -1;
675 		mcontroller[i].pcivendor = pci_vendor;
676 		mcontroller[i].pcislot = PCI_SLOT (pci_dev_func);
677 		mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
678 
679 
680 		/* Set the Mode of addressing to 64 bit if we can */
681 		if((adapter->flag & BOARD_64BIT)&&(sizeof(dma_addr_t) == 8)) {
682 			if (pci_set_dma_mask(pdev, 0xffffffffffffffffULL) == 0)
683 				adapter->has_64bit_addr = 1;
684 		}
685 		if (!adapter->has_64bit_addr)  {
686 			if (pci_set_dma_mask(pdev, 0xffffffffULL) != 0) {
687 				printk("megaraid%d: DMA not available.\n",
688 					host->host_no);
689 				goto fail_attach;
690 			}
691 		}
692 
693 		init_MUTEX(&adapter->int_mtx);
694 		init_waitqueue_head(&adapter->int_waitq);
695 
696 		adapter->this_id = DEFAULT_INITIATOR_ID;
697 		adapter->host->this_id = DEFAULT_INITIATOR_ID;
698 
699 #if MEGA_HAVE_CLUSTERING
700 		/*
701 		 * Is cluster support enabled on this controller
702 		 * Note: In a cluster the HBAs ( the initiators ) will have
703 		 * different target IDs and we cannot assume it to be 7. Call
704 		 * to mega_support_cluster() will get the target ids also if
705 		 * the cluster support is available
706 		 */
707 		adapter->has_cluster = mega_support_cluster(adapter);
708 
709 		if( adapter->has_cluster ) {
710 			printk(KERN_NOTICE
711 				"megaraid: Cluster driver, initiator id:%d\n",
712 				adapter->this_id);
713 		}
714 #endif
715 
716 		hba_count++;
717 		continue;
718 
719 fail_attach:
720 		if( did_int_data_f ) {
721 			pci_free_consistent(
722 				adapter->dev, INT_MEMBLK_SZ, adapter->int_data,
723 				adapter->int_data_dma_hndl );
724 		}
725 
726 		if( did_int_pthru_f ) {
727 			pci_free_consistent(
728 				adapter->dev, sizeof(mega_passthru),
729 				(void*) adapter->int_pthru,
730 				adapter->int_pthru_dma_hndl );
731 		}
732 
733 		if( did_setup_mbox_f ) {
734 			pci_free_consistent(adapter->dev, sizeof(mbox64_t),
735 					(void *)adapter->una_mbox64,
736 					adapter->una_mbox64_dma);
737 		}
738 
739 		if( got_irq_f ) {
740 			irq_disable(adapter);
741 			free_irq(adapter->host->irq, adapter);
742 		}
743 
744 		if( alloc_scb_f ) {
745 			kfree(adapter->scb_list);
746 		}
747 
748 		if( alloc_int_buf_f ) {
749 			pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
750 					(void *)adapter->mega_buffer,
751 					adapter->buf_dma_handle);
752 		}
753 
754 		if( did_scsi_reg_f ) scsi_unregister(host);
755 
756 		if( did_ioremap_f ) {
757 			iounmap((void *)mega_baseport);
758 			release_mem_region(tbase, 128);
759 		}
760 
761 		if( did_req_region_f )
762 			release_region(mega_baseport, 16);
763 	}
764 
765 	return;
766 }
767 
768 
769 /**
770  * mega_setup_mailbox()
771  * @adapter - pointer to our soft state
772  *
773  * Allocates a 8 byte aligned memory for the handshake mailbox.
774  */
775 static int
mega_setup_mailbox(adapter_t * adapter)776 mega_setup_mailbox(adapter_t *adapter)
777 {
778 	unsigned long	align;
779 
780 	adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
781 			sizeof(mbox64_t), &adapter->una_mbox64_dma);
782 
783 	if( !adapter->una_mbox64 ) return -1;
784 
785 	adapter->mbox = &adapter->una_mbox64->mbox;
786 
787 	adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
788 			(~0UL ^ 0xFUL));
789 
790 	adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
791 
792 	align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
793 
794 	adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
795 
796 	/*
797 	 * Register the mailbox if the controller is an io-mapped controller
798 	 */
799 	if( adapter->flag & BOARD_IOMAP ) {
800 
801 		outb_p(adapter->mbox_dma & 0xFF,
802 				adapter->host->io_port + MBOX_PORT0);
803 
804 		outb_p((adapter->mbox_dma >> 8) & 0xFF,
805 				adapter->host->io_port + MBOX_PORT1);
806 
807 		outb_p((adapter->mbox_dma >> 16) & 0xFF,
808 				adapter->host->io_port + MBOX_PORT2);
809 
810 		outb_p((adapter->mbox_dma >> 24) & 0xFF,
811 				adapter->host->io_port + MBOX_PORT3);
812 
813 		outb_p(ENABLE_MBOX_BYTE,
814 				adapter->host->io_port + ENABLE_MBOX_REGION);
815 
816 		irq_ack(adapter);
817 
818 		irq_enable(adapter);
819 	}
820 
821 	return 0;
822 }
823 
824 
825 /**
826  * issue_scb()
827  * @adapter - pointer to our soft state
828  * @scb - scsi control block
829  *
830  * Post a command to the card if the mailbox is available, otherwise return
831  * busy. We also take the scb from the pending list if the mailbox is
832  * available.
833  */
834 static inline int
issue_scb(adapter_t * adapter,scb_t * scb)835 issue_scb(adapter_t *adapter, scb_t *scb)
836 {
837 	volatile mbox64_t	*mbox64 = adapter->mbox64;
838 	volatile mbox_t		*mbox = adapter->mbox;
839 	unsigned int	i = 0;
840 
841 	if(unlikely(mbox->busy)) {
842 		do {
843 			udelay(1);
844 			i++;
845 		} while( mbox->busy && (i < max_mbox_busy_wait) );
846 
847 		if(mbox->busy) return -1;
848 	}
849 
850 	/* Copy mailbox data into host structure */
851 	memcpy((char *)mbox, (char *)scb->raw_mbox, 16);
852 
853 	mbox->cmdid = scb->idx;	/* Set cmdid */
854 	mbox->busy = 1;		/* Set busy */
855 
856 
857 	/*
858 	 * Increment the pending queue counter
859 	 */
860 	atomic_inc(&adapter->pend_cmds);
861 
862 	switch (mbox->cmd) {
863 	case MEGA_MBOXCMD_EXTPTHRU:
864 		if( !adapter->has_64bit_addr ) break;
865 		// else fall through
866 	case MEGA_MBOXCMD_LREAD64:
867 	case MEGA_MBOXCMD_LWRITE64:
868 	case MEGA_MBOXCMD_PASSTHRU64:
869 		mbox64->xfer_segment_lo = mbox->xferaddr;
870 		mbox64->xfer_segment_hi = 0;
871 		mbox->xferaddr = 0xFFFFFFFF;
872 		break;
873 	default:
874 		mbox64->xfer_segment_lo = 0;
875 		mbox64->xfer_segment_hi = 0;
876 	}
877 
878 	/*
879 	 * post the command
880 	 */
881 	scb->state |= SCB_ISSUED;
882 
883 	if( likely(adapter->flag & BOARD_MEMMAP) ) {
884 		mbox->poll = 0;
885 		mbox->ack = 0;
886 		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
887 	}
888 	else {
889 		irq_enable(adapter);
890 		issue_command(adapter);
891 	}
892 
893 	return 0;
894 }
895 
896 
897 /*
898  * mega_query_adapter()
899  * @adapter - pointer to our soft state
900  *
901  * Issue the adapter inquiry commands to the controller and find out
902  * information and parameter about the devices attached
903  */
904 static int
mega_query_adapter(adapter_t * adapter)905 mega_query_adapter(adapter_t *adapter)
906 {
907 	dma_addr_t	prod_info_dma_handle;
908 	mega_inquiry3	*inquiry3;
909 	u8	raw_mbox[sizeof(mbox_t)];
910 	mbox_t	*mbox;
911 	int	retval;
912 
913 	/* Initialize adapter inquiry mailbox */
914 
915 	mbox = (mbox_t *)raw_mbox;
916 
917 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
918 	memset(raw_mbox, 0, sizeof(raw_mbox));
919 
920 	/*
921 	 * Try to issue Inquiry3 command
922 	 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
923 	 * update enquiry3 structure
924 	 */
925 	mbox->xferaddr = (u32)adapter->buf_dma_handle;
926 
927 	inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
928 
929 	raw_mbox[0] = FC_NEW_CONFIG;		/* i.e. mbox->cmd=0xA1 */
930 	raw_mbox[2] = NC_SUBOP_ENQUIRY3;	/* i.e. 0x0F */
931 	raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;	/* i.e. 0x02 */
932 
933 	/* Issue a blocking command to the card */
934 	if ((retval = issue_scb_block(adapter, raw_mbox))) {
935 		/* the adapter does not support 40ld */
936 
937 		mraid_ext_inquiry	*ext_inq;
938 		mraid_inquiry		*inq;
939 		dma_addr_t		dma_handle;
940 
941 		ext_inq = pci_alloc_consistent(adapter->dev,
942 				sizeof(mraid_ext_inquiry), &dma_handle);
943 
944 		if( ext_inq == NULL ) return -1;
945 
946 		inq = &ext_inq->raid_inq;
947 
948 		mbox->xferaddr = (u32)dma_handle;
949 
950 		/*issue old 0x04 command to adapter */
951 		mbox->cmd = MEGA_MBOXCMD_ADPEXTINQ;
952 
953 		issue_scb_block(adapter, raw_mbox);
954 
955 		/*
956 		 * update Enquiry3 and ProductInfo structures with
957 		 * mraid_inquiry structure
958 		 */
959 		mega_8_to_40ld(inq, inquiry3,
960 				(mega_product_info *)&adapter->product_info);
961 
962 		pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
963 				ext_inq, dma_handle);
964 
965 	} else {		/*adapter supports 40ld */
966 		adapter->flag |= BOARD_40LD;
967 
968 		/*
969 		 * get product_info, which is static information and will be
970 		 * unchanged
971 		 */
972 		prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
973 				&adapter->product_info,
974 				sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
975 
976 		mbox->xferaddr = prod_info_dma_handle;
977 
978 		raw_mbox[0] = FC_NEW_CONFIG;	/* i.e. mbox->cmd=0xA1 */
979 		raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;	/* i.e. 0x0E */
980 
981 		if ((retval = issue_scb_block(adapter, raw_mbox)))
982 			printk(KERN_WARNING
983 			"megaraid: Product_info cmd failed with error: %d\n",
984 				retval);
985 
986 		pci_dma_sync_single(adapter->dev, prod_info_dma_handle,
987 				sizeof(mega_product_info),
988 				PCI_DMA_FROMDEVICE);
989 
990 		pci_unmap_single(adapter->dev, prod_info_dma_handle,
991 				sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
992 	}
993 
994 
995 	/*
996 	 * kernel scans the channels from 0 to <= max_channel
997 	 */
998 	adapter->host->max_channel =
999 		adapter->product_info.nchannels + NVIRT_CHAN -1;
1000 
1001 	adapter->host->max_id = 16;	/* max targets per channel */
1002 
1003 	adapter->host->max_lun = 7;	/* Upto 7 luns for non disk devices */
1004 
1005 	adapter->host->cmd_per_lun = max_cmd_per_lun;
1006 
1007 	adapter->numldrv = inquiry3->num_ldrv;
1008 
1009 	adapter->max_cmds = adapter->product_info.max_commands;
1010 
1011 	if(adapter->max_cmds > MAX_COMMANDS)
1012 		adapter->max_cmds = MAX_COMMANDS;
1013 
1014 	adapter->host->can_queue = adapter->max_cmds - 1;
1015 
1016 	/*
1017 	 * Get the maximum number of scatter-gather elements supported by this
1018 	 * firmware
1019 	 */
1020 	mega_get_max_sgl(adapter);
1021 
1022 	adapter->host->sg_tablesize = adapter->sglen;
1023 
1024 
1025 	/* use HP firmware and bios version encoding */
1026 	if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
1027 		sprintf (adapter->fw_version, "%c%d%d.%d%d",
1028 			 adapter->product_info.fw_version[2],
1029 			 adapter->product_info.fw_version[1] >> 8,
1030 			 adapter->product_info.fw_version[1] & 0x0f,
1031 			 adapter->product_info.fw_version[0] >> 8,
1032 			 adapter->product_info.fw_version[0] & 0x0f);
1033 		sprintf (adapter->bios_version, "%c%d%d.%d%d",
1034 			 adapter->product_info.bios_version[2],
1035 			 adapter->product_info.bios_version[1] >> 8,
1036 			 adapter->product_info.bios_version[1] & 0x0f,
1037 			 adapter->product_info.bios_version[0] >> 8,
1038 			 adapter->product_info.bios_version[0] & 0x0f);
1039 	} else {
1040 		memcpy(adapter->fw_version,
1041 				(char *)adapter->product_info.fw_version, 4);
1042 		adapter->fw_version[4] = 0;
1043 
1044 		memcpy(adapter->bios_version,
1045 				(char *)adapter->product_info.bios_version, 4);
1046 
1047 		adapter->bios_version[4] = 0;
1048 	}
1049 
1050 	printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
1051 		adapter->fw_version, adapter->bios_version, adapter->numldrv);
1052 
1053 	/*
1054 	 * Do we support extended (>10 bytes) cdbs
1055 	 */
1056 	adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
1057 	if (adapter->support_ext_cdb)
1058 		printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
1059 
1060 
1061 	return 0;
1062 }
1063 
1064 
1065 /**
1066  * mega_runpendq()
1067  * @adapter - pointer to our soft state
1068  *
1069  * Runs through the list of pending requests.
1070  */
1071 static inline void
mega_runpendq(adapter_t * adapter)1072 mega_runpendq(adapter_t *adapter)
1073 {
1074 	if(!list_empty(&adapter->pending_list))
1075 		__mega_runpendq(adapter);
1076 }
1077 
1078 static void
__mega_runpendq(adapter_t * adapter)1079 __mega_runpendq(adapter_t *adapter)
1080 {
1081 	scb_t *scb;
1082 	struct list_head *pos, *next;
1083 
1084 	/* Issue any pending commands to the card */
1085 	list_for_each_safe(pos, next, &adapter->pending_list) {
1086 
1087 		scb = list_entry(pos, scb_t, list);
1088 
1089 		if( !(scb->state & SCB_ISSUED) ) {
1090 
1091 			if( issue_scb(adapter, scb) != 0 )
1092 				return;
1093 		}
1094 	}
1095 
1096 	return;
1097 }
1098 
1099 
1100 /**
1101  * mega_allocate_scb()
1102  * @adapter - pointer to our soft state
1103  * @cmd - scsi command from the mid-layer
1104  *
1105  * Allocate a SCB structure. This is the central structure for controller
1106  * commands.
1107  */
1108 static inline scb_t *
mega_allocate_scb(adapter_t * adapter,Scsi_Cmnd * cmd)1109 mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
1110 {
1111 	struct list_head *head = &adapter->free_list;
1112 	scb_t	*scb;
1113 
1114 	/* Unlink command from Free List */
1115 	if( !list_empty(head) ) {
1116 
1117 		scb = list_entry(head->next, scb_t, list);
1118 
1119 		list_del_init(head->next);
1120 
1121 		scb->state = SCB_ACTIVE;
1122 		scb->cmd = cmd;
1123 		scb->dma_type = MEGA_DMA_TYPE_NONE;
1124 
1125 		return scb;
1126 	}
1127 
1128 	return NULL;
1129 }
1130 
1131 
1132 /**
1133  * mega_get_ldrv_num()
1134  * @adapter - pointer to our soft state
1135  * @cmd - scsi mid layer command
1136  * @channel - channel on the controller
1137  *
1138  * Calculate the logical drive number based on the information in scsi command
1139  * and the channel number.
1140  */
1141 static inline int
mega_get_ldrv_num(adapter_t * adapter,Scsi_Cmnd * cmd,int channel)1142 mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
1143 {
1144 	int		tgt;
1145 	int		ldrv_num;
1146 
1147 	tgt = cmd->target;
1148 
1149 	if ( tgt > adapter->this_id )
1150 		tgt--;	/* we do not get inquires for initiator id */
1151 
1152 	ldrv_num = (channel * 15) + tgt;
1153 
1154 
1155 	/*
1156 	 * If we have a logical drive with boot enabled, project it first
1157 	 */
1158 	if( adapter->boot_ldrv_enabled ) {
1159 		if( ldrv_num == 0 ) {
1160 			ldrv_num = adapter->boot_ldrv;
1161 		}
1162 		else {
1163 			if( ldrv_num <= adapter->boot_ldrv ) {
1164 				ldrv_num--;
1165 			}
1166 		}
1167 	}
1168 
1169 	/*
1170 	 * If "delete logical drive" feature is enabled on this controller,
1171 	 * the value returned should be 0x80+logical drive id.
1172 	 */
1173 	if (adapter->support_random_del)
1174 		ldrv_num += 0x80;
1175 
1176 	return ldrv_num;
1177 }
1178 
1179 
1180 /*
1181  * megaraid_queue()
1182  * @scmd - Issue this scsi command
1183  * @done - the callback hook into the scsi mid-layer
1184  *
1185  * The command queuing entry point for the mid-layer.
1186  */
1187 static int
megaraid_queue(Scsi_Cmnd * scmd,void (* done)(Scsi_Cmnd *))1188 megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
1189 {
1190 	adapter_t	*adapter;
1191 	scb_t	*scb;
1192 	int	busy=0;
1193 
1194 	adapter = (adapter_t *)scmd->host->hostdata;
1195 
1196 	scmd->scsi_done = done;
1197 
1198 
1199 	/*
1200 	 * Allocate and build a SCB request
1201 	 * busy flag will be set if mega_build_cmd() command could not
1202 	 * allocate scb. We will return non-zero status in that case.
1203 	 * NOTE: scb can be null even though certain commands completed
1204 	 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
1205 	 * return 0 in that case.
1206 	 */
1207 
1208 	scb = mega_build_cmd(adapter, scmd, &busy);
1209 
1210 	if(scb) {
1211 		scb->state |= SCB_PENDQ;
1212 		list_add_tail(&scb->list, &adapter->pending_list);
1213 
1214 		/*
1215 		 * Check if the HBA is in quiescent state, e.g., during a
1216 		 * delete logical drive opertion. If it is, don't run
1217 		 * the pending_list.
1218 		 */
1219 		if(atomic_read(&adapter->quiescent) == 0) {
1220 			mega_runpendq(adapter);
1221 		}
1222 		return 0;
1223 	}
1224 
1225 	return busy;
1226 }
1227 
1228 
1229 /**
1230  * mega_build_cmd()
1231  * @adapter - pointer to our soft state
1232  * @cmd - Prepare using this scsi command
1233  * @busy - busy flag if no resources
1234  *
1235  * Prepares a command and scatter gather list for the controller. This routine
1236  * also finds out if the commands is intended for a logical drive or a
1237  * physical device and prepares the controller command accordingly.
1238  *
1239  * We also re-order the logical drives and physical devices based on their
1240  * boot settings.
1241  */
1242 static scb_t *
mega_build_cmd(adapter_t * adapter,Scsi_Cmnd * cmd,int * busy)1243 mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
1244 {
1245 	mega_ext_passthru	*epthru;
1246 	mega_passthru	*pthru;
1247 	scb_t	*scb;
1248 	mbox_t	*mbox;
1249 	long	seg;
1250 	char	islogical;
1251 	int	channel = 0;
1252 	int	target = 0;
1253 	int	ldrv_num = 0;   /* logical drive number */
1254 
1255 
1256 	/*
1257 	 * filter the internal and ioctl commands
1258 	 */
1259 	if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
1260 		return cmd->buffer;
1261 	}
1262 
1263 
1264 	/*
1265 	 * We know what channels our logical drives are on - mega_find_card()
1266 	 */
1267 	islogical = adapter->logdrv_chan[cmd->channel];
1268 
1269 	/*
1270 	 * The theory: If physical drive is chosen for boot, all the physical
1271 	 * devices are exported before the logical drives, otherwise physical
1272 	 * devices are pushed after logical drives, in which case - Kernel sees
1273 	 * the physical devices on virtual channel which is obviously converted
1274 	 * to actual channel on the HBA.
1275 	 */
1276 	if( adapter->boot_pdrv_enabled ) {
1277 		if( islogical ) {
1278 			/* logical channel */
1279 			channel = cmd->channel -
1280 				adapter->product_info.nchannels;
1281 		}
1282 		else {
1283 			channel = cmd->channel; /* this is physical channel */
1284 			target = cmd->target;
1285 
1286 			/*
1287 			 * boot from a physical disk, that disk needs to be
1288 			 * exposed first IF both the channels are SCSI, then
1289 			 * booting from the second channel is not allowed.
1290 			 */
1291 			if( target == 0 ) {
1292 				target = adapter->boot_pdrv_tgt;
1293 			}
1294 			else if( target == adapter->boot_pdrv_tgt ) {
1295 				target = 0;
1296 			}
1297 		}
1298 	}
1299 	else {
1300 		if( islogical ) {
1301 			channel = cmd->channel;	/* this is the logical channel
1302 						 */
1303 		}
1304 		else {
1305 			channel = cmd->channel - NVIRT_CHAN;	/* physical
1306 								   channel */
1307 			target = cmd->target;
1308 		}
1309 	}
1310 
1311 
1312 	if(islogical) {
1313 
1314 		/* have just LUN 0 for each target on virtual channels */
1315 		if (cmd->lun) {
1316 			cmd->result = (DID_BAD_TARGET << 16);
1317 			cmd->scsi_done(cmd);
1318 			return NULL;
1319 		}
1320 
1321 		ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
1322 	}
1323 	else {
1324 		if( cmd->lun > 7) {
1325 			/*
1326 			 * Do not support lun >7 for physically accessed
1327 			 * devices
1328 			 */
1329 			cmd->result = (DID_BAD_TARGET << 16);
1330 			cmd->scsi_done(cmd);
1331 			return NULL;
1332 		}
1333 	}
1334 
1335 	/*
1336 	 *
1337 	 * Logical drive commands
1338 	 *
1339 	 */
1340 	if(islogical) {
1341 		switch (cmd->cmnd[0]) {
1342 		case TEST_UNIT_READY:
1343 			memset(cmd->request_buffer, 0, cmd->request_bufflen);
1344 
1345 #if MEGA_HAVE_CLUSTERING
1346 			/*
1347 			 * Do we support clustering and is the support enabled
1348 			 * If no, return success always
1349 			 */
1350 			if( !adapter->has_cluster ) {
1351 				cmd->result = (DID_OK << 16);
1352 				cmd->scsi_done(cmd);
1353 				return NULL;
1354 			}
1355 
1356 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
1357 
1358 				cmd->result = (DID_ERROR << 16);
1359 				cmd->scsi_done(cmd);
1360 				*busy = 1;
1361 
1362 				return NULL;
1363 			}
1364 
1365 			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
1366 			scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
1367 			scb->raw_mbox[3] = ldrv_num;
1368 
1369 			scb->dma_direction = PCI_DMA_NONE;
1370 
1371 			return scb;
1372 #else
1373 			cmd->result = (DID_OK << 16);
1374 			cmd->scsi_done(cmd);
1375 			return NULL;
1376 #endif
1377 
1378 		case MODE_SENSE:
1379 			memset(cmd->request_buffer, 0, cmd->cmnd[4]);
1380 			cmd->result = (DID_OK << 16);
1381 			cmd->scsi_done(cmd);
1382 			return NULL;
1383 
1384 		case READ_CAPACITY:
1385 		case INQUIRY:
1386 
1387 			if(!(adapter->flag & (1L << cmd->channel))) {
1388 
1389 				printk(KERN_NOTICE
1390 					"scsi%d: scanning scsi channel %d ",
1391 						adapter->host->host_no,
1392 						cmd->channel);
1393 				printk("for logical drives.\n");
1394 
1395 				adapter->flag |= (1L << cmd->channel);
1396 			}
1397 
1398 			/* Allocate a SCB and initialize passthru */
1399 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
1400 
1401 				cmd->result = (DID_ERROR << 16);
1402 				cmd->scsi_done(cmd);
1403 				*busy = 1;
1404 
1405 				return NULL;
1406 			}
1407 			pthru = scb->pthru;
1408 
1409 			mbox = (mbox_t *)scb->raw_mbox;
1410 			memset(mbox, 0, sizeof(scb->raw_mbox));
1411 			memset(pthru, 0, sizeof(mega_passthru));
1412 
1413 			pthru->timeout = 0;
1414 			pthru->ars = 1;
1415 			pthru->reqsenselen = 14;
1416 			pthru->islogical = 1;
1417 			pthru->logdrv = ldrv_num;
1418 			pthru->cdblen = cmd->cmd_len;
1419 			memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
1420 
1421 			if( adapter->has_64bit_addr ) {
1422 				mbox->cmd = MEGA_MBOXCMD_PASSTHRU64;
1423 			}
1424 			else {
1425 				mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
1426 			}
1427 
1428 			scb->dma_direction = PCI_DMA_FROMDEVICE;
1429 
1430 			pthru->numsgelements = mega_build_sglist(adapter, scb,
1431 				&pthru->dataxferaddr, &pthru->dataxferlen);
1432 
1433 			mbox->xferaddr = scb->pthru_dma_addr;
1434 
1435 			return scb;
1436 
1437 		case READ_6:
1438 		case WRITE_6:
1439 		case READ_10:
1440 		case WRITE_10:
1441 		case READ_12:
1442 		case WRITE_12:
1443 
1444 			/* Allocate a SCB and initialize mailbox */
1445 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
1446 
1447 				cmd->result = (DID_ERROR << 16);
1448 				cmd->scsi_done(cmd);
1449 				*busy = 1;
1450 
1451 				return NULL;
1452 			}
1453 			mbox = (mbox_t *)scb->raw_mbox;
1454 
1455 			memset(mbox, 0, sizeof(scb->raw_mbox));
1456 			mbox->logdrv = ldrv_num;
1457 
1458 			/*
1459 			 * A little hack: 2nd bit is zero for all scsi read
1460 			 * commands and is set for all scsi write commands
1461 			 */
1462 			if( adapter->has_64bit_addr ) {
1463 				mbox->cmd = (*cmd->cmnd & 0x02) ?
1464 					MEGA_MBOXCMD_LWRITE64:
1465 					MEGA_MBOXCMD_LREAD64 ;
1466 			}
1467 			else {
1468 				mbox->cmd = (*cmd->cmnd & 0x02) ?
1469 					MEGA_MBOXCMD_LWRITE:
1470 					MEGA_MBOXCMD_LREAD ;
1471 			}
1472 
1473 			/*
1474 			 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1475 			 */
1476 			if( cmd->cmd_len == 6 ) {
1477 				mbox->numsectors = (u32) cmd->cmnd[4];
1478 				mbox->lba =
1479 					((u32)cmd->cmnd[1] << 16) |
1480 					((u32)cmd->cmnd[2] << 8) |
1481 					(u32)cmd->cmnd[3];
1482 
1483 				mbox->lba &= 0x1FFFFF;
1484 
1485 #if MEGA_HAVE_STATS
1486 				/*
1487 				 * Take modulo 0x80, since the logical drive
1488 				 * number increases by 0x80 when a logical
1489 				 * drive was deleted
1490 				 */
1491 				if (*cmd->cmnd == READ_6) {
1492 					adapter->nreads[ldrv_num%0x80]++;
1493 					adapter->nreadblocks[ldrv_num%0x80] +=
1494 						mbox->numsectors;
1495 				} else {
1496 					adapter->nwrites[ldrv_num%0x80]++;
1497 					adapter->nwriteblocks[ldrv_num%0x80] +=
1498 						mbox->numsectors;
1499 				}
1500 #endif
1501 			}
1502 
1503 			/*
1504 			 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1505 			 */
1506 			if( cmd->cmd_len == 10 ) {
1507 				mbox->numsectors =
1508 					(u32)cmd->cmnd[8] |
1509 					((u32)cmd->cmnd[7] << 8);
1510 				mbox->lba =
1511 					((u32)cmd->cmnd[2] << 24) |
1512 					((u32)cmd->cmnd[3] << 16) |
1513 					((u32)cmd->cmnd[4] << 8) |
1514 					(u32)cmd->cmnd[5];
1515 
1516 #if MEGA_HAVE_STATS
1517 				if (*cmd->cmnd == READ_10) {
1518 					adapter->nreads[ldrv_num%0x80]++;
1519 					adapter->nreadblocks[ldrv_num%0x80] +=
1520 						mbox->numsectors;
1521 				} else {
1522 					adapter->nwrites[ldrv_num%0x80]++;
1523 					adapter->nwriteblocks[ldrv_num%0x80] +=
1524 						mbox->numsectors;
1525 				}
1526 #endif
1527 			}
1528 
1529 			/*
1530 			 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1531 			 */
1532 			if( cmd->cmd_len == 12 ) {
1533 				mbox->lba =
1534 					((u32)cmd->cmnd[2] << 24) |
1535 					((u32)cmd->cmnd[3] << 16) |
1536 					((u32)cmd->cmnd[4] << 8) |
1537 					(u32)cmd->cmnd[5];
1538 
1539 				mbox->numsectors =
1540 					((u32)cmd->cmnd[6] << 24) |
1541 					((u32)cmd->cmnd[7] << 16) |
1542 					((u32)cmd->cmnd[8] << 8) |
1543 					(u32)cmd->cmnd[9];
1544 
1545 #if MEGA_HAVE_STATS
1546 				if (*cmd->cmnd == READ_12) {
1547 					adapter->nreads[ldrv_num%0x80]++;
1548 					adapter->nreadblocks[ldrv_num%0x80] +=
1549 						mbox->numsectors;
1550 				} else {
1551 					adapter->nwrites[ldrv_num%0x80]++;
1552 					adapter->nwriteblocks[ldrv_num%0x80] +=
1553 						mbox->numsectors;
1554 				}
1555 #endif
1556 			}
1557 
1558 			/*
1559 			 * If it is a read command
1560 			 */
1561 			if( (*cmd->cmnd & 0x0F) == 0x08 ) {
1562 				scb->dma_direction = PCI_DMA_FROMDEVICE;
1563 			}
1564 			else {
1565 				scb->dma_direction = PCI_DMA_TODEVICE;
1566 			}
1567 
1568 			/* Calculate Scatter-Gather info */
1569 			mbox->numsgelements = mega_build_sglist(adapter, scb,
1570 					(u32 *)&mbox->xferaddr, (u32 *)&seg);
1571 
1572 			return scb;
1573 
1574 #if MEGA_HAVE_CLUSTERING
1575 		case RESERVE:	/* Fall through */
1576 		case RELEASE:
1577 
1578 			/*
1579 			 * Do we support clustering and is the support enabled
1580 			 */
1581 			if( ! adapter->has_cluster ) {
1582 
1583 				cmd->result = (DID_BAD_TARGET << 16);
1584 				cmd->scsi_done(cmd);
1585 				return NULL;
1586 			}
1587 
1588 			/* Allocate a SCB and initialize mailbox */
1589 			if(!(scb = mega_allocate_scb(adapter, cmd))) {
1590 
1591 				cmd->result = (DID_ERROR << 16);
1592 				cmd->scsi_done(cmd);
1593 				*busy = 1;
1594 
1595 				return NULL;
1596 			}
1597 
1598 			scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
1599 			scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
1600 				MEGA_RESERVE_LD : MEGA_RELEASE_LD;
1601 
1602 			scb->raw_mbox[3] = ldrv_num;
1603 
1604 			scb->dma_direction = PCI_DMA_NONE;
1605 
1606 			return scb;
1607 #endif
1608 
1609 		default:
1610 			cmd->result = (DID_BAD_TARGET << 16);
1611 			cmd->scsi_done(cmd);
1612 			return NULL;
1613 		}
1614 	}
1615 
1616 	/*
1617 	 * Passthru drive commands
1618 	 */
1619 	else {
1620 		/* Allocate a SCB and initialize passthru */
1621 		if(!(scb = mega_allocate_scb(adapter, cmd))) {
1622 
1623 			cmd->result = (DID_ERROR << 16);
1624 			cmd->scsi_done(cmd);
1625 			*busy = 1;
1626 
1627 			return NULL;
1628 		}
1629 
1630 		mbox = (mbox_t *)scb->raw_mbox;
1631 		memset(mbox, 0, sizeof(scb->raw_mbox));
1632 
1633 		if( adapter->support_ext_cdb ) {
1634 
1635 			epthru = mega_prepare_extpassthru(adapter, scb, cmd,
1636 					channel, target);
1637 
1638 			mbox->cmd = MEGA_MBOXCMD_EXTPTHRU;
1639 
1640 			mbox->xferaddr = scb->epthru_dma_addr;
1641 
1642 		}
1643 		else {
1644 
1645 			pthru = mega_prepare_passthru(adapter, scb, cmd,
1646 					channel, target);
1647 
1648 			/* Initialize mailbox */
1649 			if( adapter->has_64bit_addr ) {
1650 				mbox->cmd = MEGA_MBOXCMD_PASSTHRU64;
1651 			}
1652 			else {
1653 				mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
1654 			}
1655 
1656 			mbox->xferaddr = scb->pthru_dma_addr;
1657 
1658 		}
1659 		return scb;
1660 	}
1661 	return NULL;
1662 }
1663 
1664 
1665 /**
1666  * mega_prepare_passthru()
1667  * @adapter - pointer to our soft state
1668  * @scb - our scsi control block
1669  * @cmd - scsi command from the mid-layer
1670  * @channel - actual channel on the controller
1671  * @target - actual id on the controller.
1672  *
1673  * prepare a command for the scsi physical devices.
1674  */
1675 static mega_passthru *
mega_prepare_passthru(adapter_t * adapter,scb_t * scb,Scsi_Cmnd * cmd,int channel,int target)1676 mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1677 		int channel, int target)
1678 {
1679 	mega_passthru *pthru;
1680 
1681 	pthru = scb->pthru;
1682 	memset(pthru, 0, sizeof (mega_passthru));
1683 
1684 	/* 0=6sec/1=60sec/2=10min/3=3hrs/4=NO timeout */
1685 	pthru->timeout = 4;
1686 
1687 	pthru->ars = 1;
1688 	pthru->reqsenselen = 14;
1689 	pthru->islogical = 0;
1690 
1691 	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1692 
1693 	pthru->target = (adapter->flag & BOARD_40LD) ?
1694 		(channel << 4) | target : target;
1695 
1696 	pthru->cdblen = cmd->cmd_len;
1697 	pthru->logdrv = cmd->lun;
1698 
1699 	memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
1700 
1701 	/* Not sure about the direction */
1702 	scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1703 
1704 	/* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
1705 	switch (cmd->cmnd[0]) {
1706 	case INQUIRY:
1707 	case READ_CAPACITY:
1708 		if(!(adapter->flag & (1L << cmd->channel))) {
1709 
1710 			printk(KERN_NOTICE
1711 				"scsi%d: scanning scsi channel %d [P%d] ",
1712 					adapter->host->host_no,
1713 					cmd->channel, channel);
1714 			printk("for physical devices.\n");
1715 
1716 			adapter->flag |= (1L << cmd->channel);
1717 		}
1718 		/* Fall through */
1719 	default:
1720 		pthru->numsgelements = mega_build_sglist(adapter, scb,
1721 				&pthru->dataxferaddr, &pthru->dataxferlen);
1722 		break;
1723 	}
1724 	return pthru;
1725 }
1726 
1727 
1728 /**
1729  * mega_prepare_extpassthru()
1730  * @adapter - pointer to our soft state
1731  * @scb - our scsi control block
1732  * @cmd - scsi command from the mid-layer
1733  * @channel - actual channel on the controller
1734  * @target - actual id on the controller.
1735  *
1736  * prepare a command for the scsi physical devices. This rountine prepares
1737  * commands for devices which can take extended CDBs (>10 bytes)
1738  */
1739 static mega_ext_passthru *
mega_prepare_extpassthru(adapter_t * adapter,scb_t * scb,Scsi_Cmnd * cmd,int channel,int target)1740 mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1741 		int channel, int target)
1742 {
1743 	mega_ext_passthru	*epthru;
1744 
1745 	epthru = scb->epthru;
1746 	memset(epthru, 0, sizeof(mega_ext_passthru));
1747 
1748 	/* 0=6sec/1=60sec/2=10min/3=3hrs/4=NO timeout */
1749 	epthru->timeout = 4;
1750 
1751 	epthru->ars = 1;
1752 	epthru->reqsenselen = 14;
1753 	epthru->islogical = 0;
1754 
1755 	epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1756 	epthru->target = (adapter->flag & BOARD_40LD) ?
1757 		(channel << 4) | target : target;
1758 
1759 	epthru->cdblen = cmd->cmd_len;
1760 	epthru->logdrv = cmd->lun;
1761 
1762 	memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1763 
1764 	/* Not sure about the direction */
1765 	scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1766 
1767 	switch(cmd->cmnd[0]) {
1768 	case INQUIRY:
1769 	case READ_CAPACITY:
1770 		if(!(adapter->flag & (1L << cmd->channel))) {
1771 
1772 			printk(KERN_NOTICE
1773 				"scsi%d: scanning scsi channel %d [P%d] ",
1774 					adapter->host->host_no,
1775 					cmd->channel, channel);
1776 			printk("for physical devices.\n");
1777 
1778 			adapter->flag |= (1L << cmd->channel);
1779 		}
1780 		/* Fall through */
1781 	default:
1782 		epthru->numsgelements = mega_build_sglist(adapter, scb,
1783 				&epthru->dataxferaddr, &epthru->dataxferlen);
1784 		break;
1785 	}
1786 
1787 	return epthru;
1788 }
1789 
1790 
1791 /*
1792  * Wait until the controller's mailbox is available
1793  */
1794 static inline int
mega_busywait_mbox(adapter_t * adapter)1795 mega_busywait_mbox (adapter_t *adapter)
1796 {
1797 	if (adapter->mbox->busy)
1798 		return __mega_busywait_mbox(adapter);
1799 	return 0;
1800 }
1801 
1802 static int
__mega_busywait_mbox(adapter_t * adapter)1803 __mega_busywait_mbox (adapter_t *adapter)
1804 {
1805 	volatile mbox_t *mbox = adapter->mbox;
1806 	long counter;
1807 
1808 	for (counter = 0; counter < 10000; counter++) {
1809 		if (!mbox->busy)
1810 			return 0;
1811 		udelay(100); yield();
1812 	}
1813 	return -1;		/* give up after 1 second */
1814 }
1815 
1816 
1817 /**
1818  * issue_scb_block()
1819  * @adapter - pointer to our soft state
1820  * @raw_mbox - the mailbox
1821  *
1822  * Issue a scb in synchronous and non-interrupt mode
1823  */
1824 static int
issue_scb_block(adapter_t * adapter,u_char * raw_mbox)1825 issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1826 {
1827 	volatile mbox64_t *mbox64 = adapter->mbox64;
1828 	volatile mbox_t *mbox = adapter->mbox;
1829 	u8	byte;
1830 	u8	status;
1831 	int	i;
1832 
1833 	/* Wait until mailbox is free */
1834 	if(mega_busywait_mbox (adapter))
1835 		goto bug_blocked_mailbox;
1836 
1837 	/* Copy mailbox data into host structure */
1838 	memcpy((char *)mbox, raw_mbox, 16);
1839 	mbox->cmdid = 0xFE;
1840 	mbox->busy = 1;
1841 
1842 	switch (raw_mbox[0]) {
1843 	case MEGA_MBOXCMD_EXTPTHRU:
1844 		if( !adapter->has_64bit_addr ) break;
1845 		// else fall through
1846 	case MEGA_MBOXCMD_LREAD64:
1847 	case MEGA_MBOXCMD_LWRITE64:
1848 	case MEGA_MBOXCMD_PASSTHRU64:
1849 		mbox64->xfer_segment_lo = mbox->xferaddr;
1850 		mbox64->xfer_segment_hi = 0;
1851 		mbox->xferaddr = 0xFFFFFFFF;
1852 		break;
1853 	default:
1854 		mbox64->xfer_segment_lo = 0;
1855 		mbox64->xfer_segment_hi = 0;
1856 	}
1857 
1858 	if( likely(adapter->flag & BOARD_MEMMAP) ) {
1859 		mbox->poll = 0;
1860 		mbox->ack = 0;
1861 		mbox->numstatus = 0xFF;
1862 		mbox->status = 0xFF;
1863 		WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1864 
1865 		while((volatile u8)mbox->numstatus == 0xFF)
1866 			cpu_relax();
1867 
1868 		mbox->numstatus = 0xFF;
1869 
1870 		while((volatile u8)mbox->status == 0xFF)
1871 			cpu_relax();
1872 
1873 		status = mbox->status;
1874 		mbox->status = 0xFF;
1875 
1876 		while( (volatile u8)mbox->poll != 0x77 )
1877 			cpu_relax();
1878 
1879 		mbox->poll = 0;
1880 		mbox->ack = 0x77;
1881 
1882 		WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1883 
1884 		while(RDINDOOR(adapter) & 0x2)
1885 			cpu_relax();
1886 	}
1887 	else {
1888 		irq_disable(adapter);
1889 		issue_command(adapter);
1890 
1891 		while (!((byte = irq_state(adapter)) & INTR_VALID))
1892 			cpu_relax();
1893 
1894 		status = mbox->status;
1895 		mbox->numstatus = 0xFF;
1896 		mbox->status = 0xFF;
1897 
1898 		set_irq_state(adapter, byte);
1899 		irq_enable(adapter);
1900 		irq_ack(adapter);
1901 	}
1902 
1903 	// invalidate the completed command id array. After command
1904 	// completion, firmware would write the valid id.
1905 	for (i = 0; i < MAX_FIRMWARE_STATUS; i++) {
1906 		mbox->completed[i] = 0xFF;
1907 	}
1908 
1909 	return status;
1910 
1911 bug_blocked_mailbox:
1912 	printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1913 	udelay (1000);
1914 	return -1;
1915 }
1916 
1917 
1918 /**
1919  * mega_cmd_done()
1920  * @adapter - pointer to our soft state
1921  * @completed - array of ids of completed commands
1922  * @nstatus - number of completed commands
1923  * @status - status of the last command completed
1924  *
1925  * Complete the comamnds and call the scsi mid-layer callback hooks.
1926  */
1927 static inline void
mega_cmd_done(adapter_t * adapter,u8 completed[],int nstatus,int status)1928 mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1929 {
1930 	mega_ext_passthru	*epthru = NULL;
1931 	struct scatterlist	*sgl;
1932 	Scsi_Cmnd	*cmd = NULL;
1933 	mega_passthru	*pthru = NULL;
1934 	mbox_t	*mbox = NULL;
1935 	int	islogical;
1936 	u8	c;
1937 	scb_t	*scb;
1938 	int	cmdid;
1939 	int	i;
1940 
1941 	/*
1942 	 * for all the commands completed, call the mid-layer callback routine
1943 	 * and free the scb.
1944 	 */
1945 	for( i = 0; i < nstatus; i++ ) {
1946 
1947 		cmdid = completed[i];
1948 
1949 		if( cmdid == CMDID_INT_CMDS ) { /* internal command */
1950 			scb = &adapter->int_scb;
1951 			cmd = scb->cmd;
1952 			mbox = (mbox_t *)scb->raw_mbox;
1953 
1954 			/*
1955 			 * Internal command interface do not fire the extended
1956 			 * passthru or 64-bit passthru
1957 			 */
1958 			pthru = scb->pthru;
1959 
1960 		}
1961 		else {
1962 			scb = &adapter->scb_list[cmdid];
1963 			cmd = scb->cmd;
1964 			pthru = scb->pthru;
1965 			epthru = scb->epthru;
1966 			mbox = (mbox_t *)scb->raw_mbox;
1967 
1968 			/*
1969 			 * Make sure f/w has completed a valid command
1970 			 */
1971 			if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
1972 				printk(KERN_CRIT
1973 					"megaraid: invalid command ");
1974 				printk("Id %d, scb->state:%x, scsi cmd:%p\n",
1975 					cmdid, scb->state, scb->cmd);
1976 
1977 				continue;
1978 			}
1979 
1980 			/*
1981 			 * Was an abort issued for this command
1982 			 */
1983 			if( scb->state & SCB_ABORT ) {
1984 
1985 				printk(KERN_NOTICE
1986 				"megaraid: aborted cmd %lx[%x] complete.\n",
1987 					scb->cmd->serial_number, scb->idx);
1988 
1989 				cmd->result = (DID_ABORT << 16);
1990 
1991 				mega_free_scb(adapter, scb);
1992 
1993 				cmd->scsi_done(cmd);
1994 
1995 				continue;
1996 			}
1997 
1998 			/*
1999 			 * Was a reset issued for this command
2000 			 */
2001 			if( scb->state & SCB_RESET ) {
2002 
2003 				printk(KERN_WARNING
2004 				"megaraid: reset cmd %lx[%x] complete.\n",
2005 					scb->cmd->serial_number, scb->idx);
2006 
2007 				scb->cmd->result = (DID_RESET << 16);
2008 
2009 				mega_free_scb (adapter, scb);
2010 
2011 				cmd->scsi_done(cmd);
2012 
2013 				continue;
2014 			}
2015 
2016 #if MEGA_HAVE_STATS
2017 			{
2018 
2019 			int	logdrv = mbox->logdrv;
2020 
2021 			islogical = adapter->logdrv_chan[cmd->channel];
2022 
2023 			/*
2024 			 * Maintain an error counter for the logical drive.
2025 			 * Some application like SNMP agent need such
2026 			 * statistics
2027 			 */
2028 			if( status && islogical && (cmd->cmnd[0] == READ_6 ||
2029 						cmd->cmnd[0] == READ_10 ||
2030 						cmd->cmnd[0] == READ_12)) {
2031 				/*
2032 				 * Logical drive number increases by 0x80 when
2033 				 * a logical drive is deleted
2034 				 */
2035 				adapter->rd_errors[logdrv%0x80]++;
2036 			}
2037 
2038 			if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
2039 						cmd->cmnd[0] == WRITE_10 ||
2040 						cmd->cmnd[0] == WRITE_12)) {
2041 				/*
2042 				 * Logical drive number increases by 0x80 when
2043 				 * a logical drive is deleted
2044 				 */
2045 				adapter->wr_errors[logdrv%0x80]++;
2046 			}
2047 
2048 			}
2049 #endif
2050 		}
2051 
2052 		/*
2053 		 * Do not return the presence of hard disk on the channel so,
2054 		 * inquiry sent, and returned data==hard disk or removable
2055 		 * hard disk and not logical, request should return failure! -
2056 		 * PJ
2057 		 */
2058 		islogical = adapter->logdrv_chan[cmd->channel];
2059 		if (cmd->cmnd[0] == INQUIRY && !islogical) {
2060 
2061 			if( cmd->use_sg ) {
2062 				sgl = (struct scatterlist *)
2063 					cmd->request_buffer;
2064 				c = *(u8 *)sgl[0].address;
2065 			}
2066 			else {
2067 				c = *(u8 *)cmd->request_buffer;
2068 			}
2069 
2070 			if(IS_RAID_CH(adapter, cmd->channel) &&
2071 					((c & 0x1F ) == TYPE_DISK)) {
2072 				status = 0xF0;
2073 			}
2074 		}
2075 
2076 		/* clear result; otherwise, success returns corrupt value */
2077 		cmd->result = 0;
2078 
2079 		/* Convert MegaRAID status to Linux error code */
2080 		switch (status) {
2081 		case 0x00:	/* SUCCESS , i.e. SCSI_STATUS_GOOD */
2082 			cmd->result |= (DID_OK << 16);
2083 			break;
2084 
2085 		case 0x02:	/* ERROR_ABORTED, i.e.
2086 				   SCSI_STATUS_CHECK_CONDITION */
2087 
2088 			/* set sense_buffer and result fields */
2089 			if( mbox->cmd == MEGA_MBOXCMD_PASSTHRU ||
2090 				mbox->cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
2091 
2092 				memcpy(cmd->sense_buffer, pthru->reqsensearea,
2093 						14);
2094 
2095 				cmd->result = (DRIVER_SENSE << 24) |
2096 					(DID_OK << 16) |
2097 					(CHECK_CONDITION << 1);
2098 			}
2099 			else {
2100 				if (mbox->cmd == MEGA_MBOXCMD_EXTPTHRU) {
2101 
2102 					memcpy(cmd->sense_buffer,
2103 						epthru->reqsensearea, 14);
2104 
2105 					cmd->result = (DRIVER_SENSE << 24) |
2106 						(DID_OK << 16) |
2107 						(CHECK_CONDITION << 1);
2108 				} else {
2109 					cmd->sense_buffer[0] = 0x70;
2110 					cmd->sense_buffer[2] = ABORTED_COMMAND;
2111 					cmd->result |= (CHECK_CONDITION << 1);
2112 				}
2113 			}
2114 			break;
2115 
2116 		case 0x08:	/* ERR_DEST_DRIVE_FAILED, i.e.
2117 				   SCSI_STATUS_BUSY */
2118 			cmd->result |= (DID_BUS_BUSY << 16) | status;
2119 			break;
2120 
2121 		default:
2122 #if MEGA_HAVE_CLUSTERING
2123 			/*
2124 			 * If TEST_UNIT_READY fails, we know
2125 			 * MEGA_RESERVATION_STATUS failed
2126 			 */
2127 			if( cmd->cmnd[0] == TEST_UNIT_READY ) {
2128 				cmd->result |= (DID_ERROR << 16) |
2129 					(RESERVATION_CONFLICT << 1);
2130 			}
2131 			else
2132 			/*
2133 			 * Error code returned is 1 if Reserve or Release
2134 			 * failed or the input parameter is invalid
2135 			 */
2136 			if( status == 1 &&
2137 				(cmd->cmnd[0] == RESERVE ||
2138 					 cmd->cmnd[0] == RELEASE) ) {
2139 
2140 				cmd->result |= (DID_ERROR << 16) |
2141 					(RESERVATION_CONFLICT << 1);
2142 			}
2143 			else
2144 #endif
2145 				cmd->result |= (DID_BAD_TARGET << 16)|status;
2146 		}
2147 
2148 		/*
2149 		 * Only free SCBs for the commands coming down from the
2150 		 * mid-layer, not for which were issued internally
2151 		 *
2152 		 * For internal command, restore the status returned by the
2153 		 * firmware so that user can interpret it.
2154 		 */
2155 		if( cmdid == CMDID_INT_CMDS ) { /* internal command */
2156 			cmd->result = status;
2157 
2158 			/*
2159 			 * Remove the internal command from the pending list
2160 			 */
2161 			list_del_init(&scb->list);
2162 			scb->state = SCB_FREE;
2163 		}
2164 		else {
2165 			mega_free_scb(adapter, scb);
2166 		}
2167 
2168 		/*
2169 		 * Call the mid-layer callback for this command
2170 		 */
2171 		cmd->scsi_done(cmd);
2172 	}
2173 }
2174 
2175 
2176 /**
2177  * megaraid_iombox_ack_sequence - interrupt ack sequence for IO mapped HBAs
2178  * @adapter	- controller's soft state
2179  *
2180  * Interrupt ackrowledgement sequence for IO mapped HBAs
2181  */
2182 static inline void
megaraid_iombox_ack_sequence(adapter_t * adapter)2183 megaraid_iombox_ack_sequence(adapter_t *adapter)
2184 {
2185 	u8	status;
2186 	int	nstatus;
2187 	u8	completed[MAX_FIRMWARE_STATUS];
2188 	u8	byte;
2189 	int	i;
2190 
2191 
2192 	/*
2193 	 * loop till F/W has more commands for us to complete.
2194 	 */
2195 	do {
2196 		/* Check if a valid interrupt is pending */
2197 		byte = irq_state(adapter);
2198 		if( (byte & VALID_INTR_BYTE) == 0 ) {
2199 			return;
2200 		}
2201 		set_irq_state(adapter, byte);
2202 
2203 		while ((nstatus = adapter->mbox->numstatus) == 0xFF) {
2204 			cpu_relax();
2205 		}
2206 		adapter->mbox->numstatus = 0xFF;
2207 
2208 		for (i = 0; i < nstatus; i++) {
2209 			while ((completed[i] = adapter->mbox->completed[i])
2210 					== 0xFF) {
2211 				cpu_relax();
2212 			}
2213 
2214 			adapter->mbox->completed[i] = 0xFF;
2215 		}
2216 
2217 		// we must read the valid status now
2218 		if ((status = adapter->mbox->status) == 0xFF) {
2219 			printk(KERN_WARNING
2220 			"megaraid critical: status 0xFF from firmware.\n");
2221 		}
2222 		adapter->mbox->status = 0xFF;
2223 
2224 		/*
2225 		 * decrement the pending queue counter
2226 		 */
2227 		atomic_sub(nstatus, &adapter->pend_cmds);
2228 
2229 		/* Acknowledge interrupt */
2230 		irq_ack(adapter);
2231 
2232 		mega_cmd_done(adapter, completed, nstatus, status);
2233 
2234 	} while(1);
2235 }
2236 
2237 
2238 /**
2239  * megaraid_isr_iomapped()
2240  * @irq - irq
2241  * @devp - pointer to our soft state
2242  * @regs - unused
2243  *
2244  * Interrupt service routine for io-mapped controllers.
2245  * Find out if our device is interrupting. If yes, acknowledge the interrupt
2246  * and service the completed commands.
2247  */
2248 static void
megaraid_isr_iomapped(int irq,void * devp,struct pt_regs * regs)2249 megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
2250 {
2251 	adapter_t	*adapter = devp;
2252 	unsigned long	flags;
2253 
2254 	spin_lock_irqsave(adapter->host_lock, flags);
2255 
2256 	megaraid_iombox_ack_sequence(adapter);
2257 
2258 	/* Loop through any pending requests */
2259 	if( atomic_read(&adapter->quiescent ) == 0) {
2260 		mega_runpendq(adapter);
2261 	}
2262 
2263 	spin_unlock_irqrestore(adapter->host_lock, flags);
2264 
2265 	return;
2266 }
2267 
2268 
2269 /**
2270  * megaraid_memmbox_ack_sequence - interrupt ack sequence for memory mapped HBAs
2271  * @adapter	- controller's soft state
2272  *
2273  * Interrupt ackrowledgement sequence for memory mapped HBAs
2274  */
2275 static inline void
megaraid_memmbox_ack_sequence(adapter_t * adapter)2276 megaraid_memmbox_ack_sequence(adapter_t *adapter)
2277 {
2278 	u8	status;
2279 	u32	dword = 0;
2280 	int	nstatus;
2281 	u8	completed[MAX_FIRMWARE_STATUS];
2282 	int	i;
2283 
2284 
2285 	/*
2286 	 * loop till F/W has more commands for us to complete.
2287 	 */
2288 	do {
2289 		/* Check if a valid interrupt is pending */
2290 		dword = RDOUTDOOR(adapter);
2291 		if( dword != 0x10001234 ) {
2292 			/*
2293 			 * No more pending commands
2294 			 */
2295 			return;
2296 		}
2297 		WROUTDOOR(adapter, 0x10001234);
2298 
2299 		while ((nstatus = adapter->mbox->numstatus) == 0xFF) {
2300 			cpu_relax();
2301 		}
2302 		adapter->mbox->numstatus = 0xFF;
2303 
2304 		for (i = 0; i < nstatus; i++ ) {
2305 			while ((completed[i] = adapter->mbox->completed[i])
2306 					== 0xFF) {
2307 				cpu_relax();
2308 			}
2309 
2310 			adapter->mbox->completed[i] = 0xFF;
2311 		}
2312 
2313 		// we must read the valid status now
2314 		if ((status = adapter->mbox->status) == 0xFF) {
2315 			printk(KERN_WARNING
2316 			"megaraid critical: status 0xFF from firmware.\n");
2317 		}
2318 		adapter->mbox->status = 0xFF;
2319 
2320 		/*
2321 		 * decrement the pending queue counter
2322 		 */
2323 		atomic_sub(nstatus, &adapter->pend_cmds);
2324 
2325 		/* Acknowledge interrupt */
2326 		WRINDOOR(adapter, 0x2);
2327 
2328 		while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
2329 
2330 		mega_cmd_done(adapter, completed, nstatus, status);
2331 
2332 	} while(1);
2333 }
2334 
2335 
2336 /**
2337  * megaraid_isr_memmapped()
2338  * @irq - irq
2339  * @devp - pointer to our soft state
2340  * @regs - unused
2341  *
2342  * Interrupt service routine for memory-mapped controllers.
2343  * Find out if our device is interrupting. If yes, acknowledge the interrupt
2344  * and service the completed commands.
2345  */
2346 static void
megaraid_isr_memmapped(int irq,void * devp,struct pt_regs * regs)2347 megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
2348 {
2349 	adapter_t	*adapter = devp;
2350 	unsigned long	flags;
2351 
2352 	spin_lock_irqsave(adapter->host_lock, flags);
2353 
2354 	megaraid_memmbox_ack_sequence(adapter);
2355 
2356 	/* Loop through any pending requests */
2357 	if(atomic_read(&adapter->quiescent) == 0) {
2358 		mega_runpendq(adapter);
2359 	}
2360 
2361 	spin_unlock_irqrestore(adapter->host_lock, flags);
2362 
2363 	return;
2364 }
2365 
2366 
2367 /*
2368  * Free a SCB structure
2369  * Note: We assume the scsi commands associated with this scb is not free yet.
2370  */
2371 static void
mega_free_scb(adapter_t * adapter,scb_t * scb)2372 mega_free_scb(adapter_t *adapter, scb_t *scb)
2373 {
2374 	switch( scb->dma_type ) {
2375 
2376 	case MEGA_DMA_TYPE_NONE:
2377 		break;
2378 
2379 	case MEGA_BULK_DATA:
2380 		if( scb->dma_direction == PCI_DMA_FROMDEVICE ) {
2381 			pci_dma_sync_single(adapter->dev, scb->dma_h_bulkdata,
2382 					scb->cmd->request_bufflen,
2383 					PCI_DMA_FROMDEVICE);
2384 		}
2385 
2386 		pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
2387 			scb->cmd->request_bufflen, scb->dma_direction);
2388 
2389 		break;
2390 
2391 	case MEGA_SGLIST:
2392 		if( scb->dma_direction == PCI_DMA_FROMDEVICE ) {
2393 			pci_dma_sync_sg(adapter->dev,
2394 				(struct scatterlist *)scb->cmd->request_buffer,
2395 				scb->cmd->use_sg, PCI_DMA_FROMDEVICE);
2396 		}
2397 
2398 		pci_unmap_sg(adapter->dev,
2399 			(struct scatterlist *)scb->cmd->request_buffer,
2400 			scb->cmd->use_sg, scb->dma_direction);
2401 
2402 		break;
2403 
2404 	default:
2405 		break;
2406 	}
2407 
2408 	/*
2409 	 * Remove from the pending list
2410 	 */
2411 	list_del_init(&scb->list);
2412 
2413 	/* Link the scb back into free list */
2414 	scb->state = SCB_FREE;
2415 	scb->cmd = NULL;
2416 
2417 	list_add(&scb->list, &adapter->free_list);
2418 }
2419 
2420 
2421 /*
2422  * Copies data to SGLIST
2423  * Note: For 64 bit cards, we need a minimum of one SG element for read/write
2424  */
2425 static int
mega_build_sglist(adapter_t * adapter,scb_t * scb,u32 * buf,u32 * len)2426 mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
2427 {
2428 	struct scatterlist	*sgl;
2429 	struct page	*page;
2430 	unsigned long	offset;
2431 	Scsi_Cmnd	*cmd;
2432 	int	sgcnt;
2433 	int	idx;
2434 
2435 	cmd = scb->cmd;
2436 
2437 	// return 0 elements if no data transfer
2438 	if (!cmd->request_buffer || !cmd->request_bufflen)
2439 		return 0;
2440 
2441 	/* Scatter-gather not used */
2442 	if( !cmd->use_sg ) {
2443 
2444 		page = virt_to_page(cmd->request_buffer);
2445 
2446 		offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
2447 
2448 		scb->dma_h_bulkdata = pci_map_page(adapter->dev, page, offset,
2449 						  cmd->request_bufflen,
2450 						  scb->dma_direction);
2451 		scb->dma_type = MEGA_BULK_DATA;
2452 
2453 		/*
2454 		 * We need to handle special 64-bit commands that need a
2455 		 * minimum of 1 SG
2456 		 */
2457 		if( adapter->has_64bit_addr ) {
2458 			scb->sgl64[0].address = scb->dma_h_bulkdata;
2459 			scb->sgl64[0].length = cmd->request_bufflen;
2460 			*buf = (u32)scb->sgl_dma_addr;
2461 			*len = (u32)cmd->request_bufflen;
2462 			return 1;
2463 		}
2464 		else {
2465 			*buf = (u32)scb->dma_h_bulkdata;
2466 			*len = (u32)cmd->request_bufflen;
2467 		}
2468 
2469 		if( scb->dma_direction == PCI_DMA_TODEVICE ) {
2470 			pci_dma_sync_single(adapter->dev,
2471 					scb->dma_h_bulkdata,
2472 					cmd->request_bufflen,
2473 					PCI_DMA_TODEVICE);
2474 		}
2475 
2476 		return 0;
2477 	}
2478 
2479 	sgl = (struct scatterlist *)cmd->request_buffer;
2480 
2481 	/*
2482 	 * Copy Scatter-Gather list info into controller structure.
2483 	 *
2484 	 * The number of sg elements returned must not exceed our limit
2485 	 */
2486 	sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg, scb->dma_direction);
2487 
2488 	scb->dma_type = MEGA_SGLIST;
2489 
2490 	if( sgcnt > adapter->sglen ) BUG();
2491 
2492 	for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
2493 
2494 		if( adapter->has_64bit_addr ) {
2495 			scb->sgl64[idx].address = sg_dma_address(sgl);
2496 			scb->sgl64[idx].length = sg_dma_len(sgl);
2497 		}
2498 		else {
2499 			scb->sgl[idx].address = sg_dma_address(sgl);
2500 			scb->sgl[idx].length = sg_dma_len(sgl);
2501 		}
2502 	}
2503 
2504 	/* Reset pointer and length fields */
2505 	*buf = scb->sgl_dma_addr;
2506 
2507 	/*
2508 	 * For passthru command, dataxferlen must be set, even for commands
2509 	 * with a sg list
2510 	 */
2511 	*len = (u32)cmd->request_bufflen;
2512 
2513 	if( scb->dma_direction == PCI_DMA_TODEVICE ) {
2514 		pci_dma_sync_sg(adapter->dev,
2515 			(struct scatterlist *)cmd->request_buffer,
2516 			cmd->use_sg, PCI_DMA_TODEVICE);
2517 	}
2518 
2519 	/* Return count of SG requests */
2520 	return sgcnt;
2521 }
2522 
2523 
2524 /*
2525  * mega_8_to_40ld()
2526  *
2527  * takes all info in AdapterInquiry structure and puts it into ProductInfo and
2528  * Enquiry3 structures for later use
2529  */
2530 static void
mega_8_to_40ld(mraid_inquiry * inquiry,mega_inquiry3 * enquiry3,mega_product_info * product_info)2531 mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
2532 		mega_product_info *product_info)
2533 {
2534 	int i;
2535 
2536 	product_info->max_commands = inquiry->adapter_info.max_commands;
2537 	enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
2538 	product_info->nchannels = inquiry->adapter_info.nchannels;
2539 
2540 	for (i = 0; i < 4; i++) {
2541 		product_info->fw_version[i] =
2542 			inquiry->adapter_info.fw_version[i];
2543 
2544 		product_info->bios_version[i] =
2545 			inquiry->adapter_info.bios_version[i];
2546 	}
2547 	enquiry3->cache_flush_interval =
2548 		inquiry->adapter_info.cache_flush_interval;
2549 
2550 	product_info->dram_size = inquiry->adapter_info.dram_size;
2551 
2552 	enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
2553 
2554 	for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
2555 		enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
2556 		enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
2557 		enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
2558 	}
2559 
2560 	for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
2561 		enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
2562 }
2563 
2564 
2565 static inline void
mega_free_sgl(adapter_t * adapter)2566 mega_free_sgl(adapter_t *adapter)
2567 {
2568 	scb_t	*scb;
2569 	int	i;
2570 
2571 	for(i = 0; i < adapter->max_cmds; i++) {
2572 
2573 		scb = &adapter->scb_list[i];
2574 
2575 		if( scb->sgl64 ) {
2576 			pci_free_consistent(adapter->dev,
2577 				sizeof(mega_sgl64) * adapter->sglen,
2578 				scb->sgl64,
2579 				scb->sgl_dma_addr);
2580 
2581 			scb->sgl64 = NULL;
2582 		}
2583 
2584 		if( scb->pthru ) {
2585 			pci_free_consistent(adapter->dev, sizeof(mega_passthru),
2586 				scb->pthru, scb->pthru_dma_addr);
2587 
2588 			scb->pthru = NULL;
2589 		}
2590 
2591 		if( scb->epthru ) {
2592 			pci_free_consistent(adapter->dev,
2593 				sizeof(mega_ext_passthru),
2594 				scb->epthru, scb->epthru_dma_addr);
2595 
2596 			scb->epthru = NULL;
2597 		}
2598 
2599 	}
2600 }
2601 
2602 
2603 /*
2604  * Release the controller's resources
2605  */
2606 static int
megaraid_release(struct Scsi_Host * host)2607 megaraid_release(struct Scsi_Host *host)
2608 {
2609 	adapter_t	*adapter;
2610 	mbox_t	*mbox;
2611 	u_char	raw_mbox[sizeof(mbox_t)];
2612 #ifdef CONFIG_PROC_FS
2613 	char	buf[12] = { 0 };
2614 #endif
2615 
2616 	adapter = (adapter_t *)host->hostdata;
2617 	mbox = (mbox_t *)raw_mbox;
2618 
2619 	printk(KERN_NOTICE "megaraid: being unloaded...");
2620 
2621 	/* Flush adapter cache */
2622 	memset(raw_mbox, 0, sizeof(raw_mbox));
2623 	raw_mbox[0] = FLUSH_ADAPTER;
2624 
2625 	if (adapter->flag & BOARD_IOMAP)
2626 		irq_disable(adapter);
2627 
2628 	free_irq(adapter->host->irq, adapter);
2629 
2630 	/* Issue a blocking (interrupts disabled) command to the card */
2631 	issue_scb_block(adapter, raw_mbox);
2632 
2633 	/* Flush disks cache */
2634 	memset(raw_mbox, 0, sizeof(raw_mbox));
2635 	raw_mbox[0] = FLUSH_SYSTEM;
2636 
2637 	/* Issue a blocking (interrupts disabled) command to the card */
2638 	issue_scb_block(adapter, raw_mbox);
2639 
2640 
2641 	/* Free our resources */
2642 	if( adapter->flag & BOARD_MEMMAP ) {
2643 		iounmap((void *)adapter->base);
2644 		release_mem_region(adapter->host->base, 128);
2645 	}
2646 	else {
2647 		release_region(adapter->base, 16);
2648 	}
2649 
2650 	mega_free_sgl(adapter);
2651 
2652 #ifdef CONFIG_PROC_FS
2653 	if( adapter->controller_proc_dir_entry ) {
2654 		remove_proc_entry("stat", adapter->controller_proc_dir_entry);
2655 		remove_proc_entry("config",
2656 				adapter->controller_proc_dir_entry);
2657 		remove_proc_entry("mailbox",
2658 				adapter->controller_proc_dir_entry);
2659 #if MEGA_HAVE_ENH_PROC
2660 		remove_proc_entry("rebuild-rate",
2661 				adapter->controller_proc_dir_entry);
2662 		remove_proc_entry("battery-status",
2663 				adapter->controller_proc_dir_entry);
2664 
2665 		remove_proc_entry("diskdrives-ch0",
2666 				adapter->controller_proc_dir_entry);
2667 		remove_proc_entry("diskdrives-ch1",
2668 				adapter->controller_proc_dir_entry);
2669 		remove_proc_entry("diskdrives-ch2",
2670 				adapter->controller_proc_dir_entry);
2671 		remove_proc_entry("diskdrives-ch3",
2672 				adapter->controller_proc_dir_entry);
2673 
2674 		remove_proc_entry("raiddrives-0-9",
2675 				adapter->controller_proc_dir_entry);
2676 		remove_proc_entry("raiddrives-10-19",
2677 				adapter->controller_proc_dir_entry);
2678 		remove_proc_entry("raiddrives-20-29",
2679 				adapter->controller_proc_dir_entry);
2680 		remove_proc_entry("raiddrives-30-39",
2681 				adapter->controller_proc_dir_entry);
2682 #endif
2683 
2684 		sprintf(buf, "hba%d", adapter->host->host_no);
2685 		remove_proc_entry(buf, mega_proc_dir_entry);
2686 	}
2687 #endif
2688 
2689 	pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
2690 			adapter->mega_buffer, adapter->buf_dma_handle);
2691 	kfree(adapter->scb_list);
2692 	pci_free_consistent(adapter->dev, sizeof(mbox64_t),
2693 			(void *)adapter->una_mbox64, adapter->una_mbox64_dma);
2694 
2695 	pci_free_consistent( adapter->dev, sizeof(mega_passthru),
2696 				(void*) adapter->int_pthru,
2697 				adapter->int_pthru_dma_hndl );
2698 
2699 	pci_free_consistent( adapter->dev, INT_MEMBLK_SZ, adapter->int_data,
2700 				adapter->int_data_dma_hndl );
2701 
2702 	hba_count--;
2703 
2704 	if( hba_count == 0 ) {
2705 
2706 		/*
2707 		 * Unregister the character device interface to the driver.
2708 		 */
2709 		if (major >= 0) {
2710 			unregister_chrdev(major, "megadev");
2711 		}
2712 
2713 		unregister_reboot_notifier(&mega_notifier);
2714 
2715 #ifdef CONFIG_PROC_FS
2716 		if( adapter->controller_proc_dir_entry ) {
2717 			remove_proc_entry ("megaraid", &proc_root);
2718 		}
2719 #endif
2720 
2721 	}
2722 
2723 	/*
2724 	 * Release the controller memory. A word of warning this frees
2725 	 * hostdata and that includes adapter-> so be careful what you
2726 	 * dereference beyond this point
2727 	 */
2728 	scsi_unregister(host);
2729 
2730 #ifdef LSI_CONFIG_COMPAT
2731 	unregister_ioctl32_conversion(MEGAIOCCMD);
2732 #endif
2733 
2734 	printk("ok.\n");
2735 
2736 	return 0;
2737 }
2738 
2739 /*
2740  * Get information about the card/driver
2741  */
2742 const char *
megaraid_info(struct Scsi_Host * host)2743 megaraid_info(struct Scsi_Host *host)
2744 {
2745 	static char buffer[512];
2746 	adapter_t *adapter;
2747 
2748 	adapter = (adapter_t *)host->hostdata;
2749 
2750 	sprintf (buffer,
2751 		 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
2752 		 adapter->fw_version, adapter->product_info.max_commands,
2753 		 adapter->host->max_id, adapter->host->max_channel,
2754 		 adapter->host->max_lun);
2755 	return buffer;
2756 }
2757 
2758 /* shouldn't be used, but included for completeness */
2759 static int
megaraid_command(Scsi_Cmnd * cmd)2760 megaraid_command (Scsi_Cmnd *cmd)
2761 {
2762 	printk(KERN_WARNING
2763 	"megaraid critcal error: synchronous interface is not implemented.\n");
2764 
2765 	cmd->result = (DID_ERROR << 16);
2766 	cmd->scsi_done(cmd);
2767 
2768 	return 1;
2769 }
2770 
2771 
2772 static int
megaraid_abort(Scsi_Cmnd * scp)2773 megaraid_abort(Scsi_Cmnd *scp)
2774 {
2775 	adapter_t		*adapter;
2776 	struct list_head	*pos, *next;
2777 	scb_t			*scb;
2778 
2779 	printk("megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2780 		scp->serial_number, scp->cmnd[0], scp->channel,
2781 		scp->target, scp->lun);
2782 
2783 	adapter = (adapter_t *)scp->host->hostdata;
2784 
2785 	/*
2786 	 * Check if hw_error flag was set in previous RESET call. If it was,
2787 	 * then FW is hanging and unlikely to function. We can return FAILURE
2788 	 * from here and expect the RESET handler to be called.
2789 	 */
2790 
2791 	if (adapter->hw_error) {
2792 		printk("megaraid: hw error, cannot abort\n");
2793 		return FAILED;
2794 	}
2795 
2796 	ASSERT( spin_is_locked(adapter->host_lock) );
2797 
2798 	/*
2799 	 * If cmd is waiting to be issued to FW, ABORT it with SUCEESS. If it
2800 	 * has already been issued, return FAILURE and expect RESET later.
2801 	 */
2802 
2803 	list_for_each_safe( pos, next, &adapter->pending_list ) {
2804 
2805 		scb = list_entry(pos, scb_t, list);
2806 
2807 		if( scb->cmd == scp ) { /* Found command */
2808 
2809 			scb->state |= SCB_ABORT;
2810 
2811 			if( !(scb->state & SCB_ISSUED) ) {
2812 
2813 				/* Not issued to the FW yet; ABORT it */
2814 
2815 				printk( "megaraid: %ld:%d, driver owner.\n",
2816 					scp->serial_number, scb->idx);
2817 
2818 				scp->result = (DID_ABORT << 16);
2819 
2820 				mega_free_scb(adapter, scb);
2821 
2822 				scp->scsi_done(scp);
2823 
2824 				return SUCCESS;
2825 			}
2826 			else {
2827 				/* Issued to the FW; can do nothing */
2828 				return FAILED;
2829 			}
2830 		}
2831 	}
2832 
2833 	/*
2834 	 * cmd is _not_ in our pending_list. Most likely we completed the cmd
2835 	 */
2836 	return SUCCESS;
2837 }
2838 
2839 static int
megaraid_reset(Scsi_Cmnd * cmd)2840 megaraid_reset(Scsi_Cmnd *cmd)
2841 {
2842 	DECLARE_WAIT_QUEUE_HEAD(wq);
2843 	int			i;
2844 	scb_t			*scb;
2845 	adapter_t		*adapter;
2846 	struct list_head	*pos, *next;
2847 	int			rval;
2848 
2849 	adapter = (adapter_t *)cmd->host->hostdata;
2850 
2851 	ASSERT( spin_is_locked(adapter->host_lock) );
2852 
2853 	printk("megaraid: reset-%ld cmd=%x <c=%d t=%d l=%d>\n",
2854 		cmd->serial_number, cmd->cmnd[0], cmd->channel, cmd->target,
2855 		cmd->lun);
2856 
2857 	/*
2858 	 * Check if hw_error flag was set in previous RESET call. If it was,
2859 	 * then we needn't do any handling here. The controller will be marked
2860 	 * offline soon
2861 	 */
2862 
2863 	if (adapter->hw_error) {
2864 		printk("megaraid: hw error, cannot reset\n");
2865 		return FAILED;
2866 	}
2867 
2868 	/*
2869 	 * Return all the pending cmds to the mid-layer with the cmd result
2870 	 * DID_RESET. Make sure you don't return the cmds ISSUED to FW.
2871 	 */
2872 	list_for_each_safe( pos, next, &adapter->pending_list ) {
2873 
2874 		scb		= list_entry(pos, scb_t, list);
2875 		scb->state	|= SCB_RESET;
2876 
2877 		if( !(scb->state & SCB_ISSUED) ) {
2878 
2879 			/* Not issued to the FW; return with RESET */
2880 			cmd->result = (DID_RESET << 16);
2881 
2882 			mega_free_scb(adapter, scb);
2883 			cmd->scsi_done(cmd);
2884 		}
2885 	}
2886 
2887 	/*
2888 	 * Under exceptional conditions, FW may take up to 3 mins to complete
2889 	 * processing all pending commands. We'll wait for maximum 3 mins to
2890 	 * see if all outstanding commands are completed.
2891 	 */
2892 
2893 	if (atomic_read(&adapter->pend_cmds) == 0)
2894 		return SUCCESS;
2895 
2896 	printk("megaraid: %d pending cmds; max wait %d seconds\n",
2897 		atomic_read(&adapter->pend_cmds), MBOX_RESET_WAIT );
2898 
2899 	for(i=0; (i<MBOX_RESET_WAIT)&&(atomic_read(&adapter->pend_cmds)); i++){
2900 
2901 		ASSERT( spin_is_locked(adapter->host_lock) );
2902 
2903 		/*
2904 		 * Perform the ack sequence, since interrupts are unavailable
2905 		 */
2906 		if (adapter->flag & BOARD_MEMMAP)
2907 			megaraid_memmbox_ack_sequence(adapter);
2908 		else
2909 			megaraid_iombox_ack_sequence(adapter);
2910 
2911 		spin_unlock(adapter->host_lock);
2912 
2913 		/* Print a message once every 5 seconds */
2914 		if (!(i % 5)) {
2915 			printk("megaraid: pending %d; remaining %d seconds\n",
2916 				atomic_read(&adapter->pend_cmds),
2917 				MBOX_RESET_WAIT - i);
2918 		}
2919 
2920 		sleep_on_timeout(&wq, HZ);
2921 
2922 		spin_lock(adapter->host_lock);
2923 	}
2924 
2925 	/*
2926 	 * If after 3 mins there are still outstanding cmds, set the hw_error
2927 	 * flag so that we can return from subsequent ABORT/RESET handlers
2928 	 * without any processing
2929 	 */
2930 
2931 	rval = SUCCESS;
2932 	if (atomic_read(&adapter->pend_cmds)) {
2933 
2934 		adapter->hw_error = 1;
2935 		printk("megaraid: critical hardware error!\n" );
2936 		rval = FAILED;
2937 	}
2938 
2939 	return rval;
2940 }
2941 
2942 #ifdef CONFIG_PROC_FS
2943 /* Following code handles /proc fs  */
2944 
2945 #define CREATE_READ_PROC(string, func)	create_proc_read_entry(string,	\
2946 					S_IRUSR | S_IFREG,		\
2947 					controller_proc_dir_entry,	\
2948 					func, adapter)
2949 
2950 /**
2951  * mega_create_proc_entry()
2952  * @index - index in soft state array
2953  * @parent - parent node for this /proc entry
2954  *
2955  * Creates /proc entries for our controllers.
2956  */
2957 static void
mega_create_proc_entry(int index,struct proc_dir_entry * parent)2958 mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2959 {
2960 	struct proc_dir_entry	*controller_proc_dir_entry = NULL;
2961 	u8		string[64] = { 0 };
2962 	adapter_t	*adapter = hba_soft_state[index];
2963 
2964 	sprintf(string, "hba%d", adapter->host->host_no);
2965 
2966 	controller_proc_dir_entry =
2967 		adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2968 
2969 	if(!controller_proc_dir_entry) {
2970 		printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2971 		return;
2972 	}
2973 	adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2974 	adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2975 	adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2976 #if MEGA_HAVE_ENH_PROC
2977 	adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2978 	adapter->proc_battery = CREATE_READ_PROC("battery-status",
2979 			proc_battery);
2980 
2981 	/*
2982 	 * Display each physical drive on its channel
2983 	 */
2984 	adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2985 					proc_pdrv_ch0);
2986 	adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2987 					proc_pdrv_ch1);
2988 	adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2989 					proc_pdrv_ch2);
2990 	adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2991 					proc_pdrv_ch3);
2992 
2993 	/*
2994 	 * Display a set of up to 10 logical drive through each of following
2995 	 * /proc entries
2996 	 */
2997 	adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2998 					proc_rdrv_10);
2999 	adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
3000 					proc_rdrv_20);
3001 	adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
3002 					proc_rdrv_30);
3003 	adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
3004 					proc_rdrv_40);
3005 #endif
3006 }
3007 
3008 
3009 /**
3010  * proc_read_config()
3011  * @page - buffer to write the data in
3012  * @start - where the actual data has been written in page
3013  * @offset - same meaning as the read system call
3014  * @count - same meaning as the read system call
3015  * @eof - set if no more data needs to be returned
3016  * @data - pointer to our soft state
3017  *
3018  * Display configuration information about the controller.
3019  */
3020 static int
proc_read_config(char * page,char ** start,off_t offset,int count,int * eof,void * data)3021 proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
3022 		void *data)
3023 {
3024 
3025 	adapter_t *adapter = (adapter_t *)data;
3026 	int len = 0;
3027 
3028 	len += sprintf(page+len, "%s", MEGARAID_VERSION);
3029 
3030 	if(adapter->product_info.product_name[0])
3031 		len += sprintf(page+len, "%s\n",
3032 				adapter->product_info.product_name);
3033 
3034 	len += sprintf(page+len, "Controller Type: ");
3035 
3036 	if( adapter->flag & BOARD_MEMMAP ) {
3037 		len += sprintf(page+len,
3038 			"438/466/467/471/493/518/520/531/532\n");
3039 	}
3040 	else {
3041 		len += sprintf(page+len,
3042 			"418/428/434\n");
3043 	}
3044 
3045 	if(adapter->flag & BOARD_40LD) {
3046 		len += sprintf(page+len,
3047 				"Controller Supports 40 Logical Drives\n");
3048 	}
3049 
3050 	if(adapter->flag & BOARD_64BIT) {
3051 		len += sprintf(page+len,
3052 		"Controller capable of 64-bit memory addressing\n");
3053 	}
3054 	if( adapter->has_64bit_addr ) {
3055 		len += sprintf(page+len,
3056 			"Controller using 64-bit memory addressing\n");
3057 	}
3058 	else {
3059 		len += sprintf(page+len,
3060 			"Controller is not using 64-bit memory addressing\n");
3061 	}
3062 
3063 	len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
3064 			adapter->host->irq);
3065 
3066 	len += sprintf(page+len, "Initial Logical Drives = %d, Channels = %d\n",
3067 			adapter->numldrv, adapter->product_info.nchannels);
3068 
3069 	len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
3070 			adapter->fw_version, adapter->bios_version,
3071 			adapter->product_info.dram_size);
3072 
3073 	len += sprintf(page+len,
3074 		"Controller Queue Depth = %d, Driver Queue Depth = %d\n",
3075 		adapter->product_info.max_commands, adapter->max_cmds);
3076 
3077 	len += sprintf(page+len, "support_ext_cdb    = %d\n",
3078 			adapter->support_ext_cdb);
3079 	len += sprintf(page+len, "support_random_del = %d\n",
3080 			adapter->support_random_del);
3081 	len += sprintf(page+len, "boot_ldrv_enabled  = %d\n",
3082 			adapter->boot_ldrv_enabled);
3083 	len += sprintf(page+len, "boot_ldrv          = %d\n",
3084 			adapter->boot_ldrv);
3085 	len += sprintf(page+len, "boot_pdrv_enabled  = %d\n",
3086 			adapter->boot_pdrv_enabled);
3087 	len += sprintf(page+len, "boot_pdrv_ch       = %d\n",
3088 			adapter->boot_pdrv_ch);
3089 	len += sprintf(page+len, "boot_pdrv_tgt      = %d\n",
3090 			adapter->boot_pdrv_tgt);
3091 	len += sprintf(page+len, "quiescent          = %d\n",
3092 			atomic_read(&adapter->quiescent));
3093 	len += sprintf(page+len, "has_cluster        = %d\n",
3094 			adapter->has_cluster);
3095 
3096 	len += sprintf(page+len, "\nModule Parameters:\n");
3097 	len += sprintf(page+len, "max_cmd_per_lun    = %d\n",
3098 			max_cmd_per_lun);
3099 	len += sprintf(page+len, "max_sectors_per_io = %d\n",
3100 			max_sectors_per_io);
3101 
3102 	*eof = 1;
3103 
3104 	return len;
3105 }
3106 
3107 
3108 
3109 /**
3110  * proc_read_stat()
3111  * @page - buffer to write the data in
3112  * @start - where the actual data has been written in page
3113  * @offset - same meaning as the read system call
3114  * @count - same meaning as the read system call
3115  * @eof - set if no more data needs to be returned
3116  * @data - pointer to our soft state
3117  *
3118  * Diaplay statistical information about the I/O activity.
3119  */
3120 static int
proc_read_stat(char * page,char ** start,off_t offset,int count,int * eof,void * data)3121 proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
3122 		void *data)
3123 {
3124 	adapter_t	*adapter;
3125 	int	len;
3126 	int	i;
3127 
3128 	i = 0;	/* avoid compilation warnings */
3129 	len = 0;
3130 	adapter = (adapter_t *)data;
3131 
3132 	len = sprintf(page, "Statistical Information for this controller\n");
3133 	len += sprintf(page+len, "pend_cmds = %d\n",
3134 			atomic_read(&adapter->pend_cmds));
3135 #if MEGA_HAVE_STATS
3136 	for(i = 0; i < adapter->numldrv; i++) {
3137 		len += sprintf(page+len, "Logical Drive %d:\n", i);
3138 
3139 		len += sprintf(page+len,
3140 			"\tReads Issued = %lu, Writes Issued = %lu\n",
3141 			adapter->nreads[i], adapter->nwrites[i]);
3142 
3143 		len += sprintf(page+len,
3144 			"\tSectors Read = %lu, Sectors Written = %lu\n",
3145 			adapter->nreadblocks[i], adapter->nwriteblocks[i]);
3146 
3147 		len += sprintf(page+len,
3148 			"\tRead errors = %lu, Write errors = %lu\n\n",
3149 			adapter->rd_errors[i], adapter->wr_errors[i]);
3150 	}
3151 #else
3152 	len += sprintf(page+len,
3153 			"IO and error counters not compiled in driver.\n");
3154 #endif
3155 
3156 	*eof = 1;
3157 
3158 	return len;
3159 }
3160 
3161 
3162 /**
3163  * proc_read_mbox()
3164  * @page - buffer to write the data in
3165  * @start - where the actual data has been written in page
3166  * @offset - same meaning as the read system call
3167  * @count - same meaning as the read system call
3168  * @eof - set if no more data needs to be returned
3169  * @data - pointer to our soft state
3170  *
3171  * Display mailbox information for the last command issued. This information
3172  * is good for debugging.
3173  */
3174 static int
proc_read_mbox(char * page,char ** start,off_t offset,int count,int * eof,void * data)3175 proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
3176 		void *data)
3177 {
3178 
3179 	adapter_t	*adapter = (adapter_t *)data;
3180 	volatile mbox_t	*mbox = adapter->mbox;
3181 	int	len = 0;
3182 
3183 	len = sprintf(page, "Contents of Mail Box Structure\n");
3184 	len += sprintf(page+len, "  Fw Command   = 0x%02x\n", mbox->cmd);
3185 	len += sprintf(page+len, "  Cmd Sequence = 0x%02x\n", mbox->cmdid);
3186 	len += sprintf(page+len, "  No of Sectors= %04d\n", mbox->numsectors);
3187 	len += sprintf(page+len, "  LBA          = 0x%02x\n", mbox->lba);
3188 	len += sprintf(page+len, "  DTA          = 0x%08x\n", mbox->xferaddr);
3189 	len += sprintf(page+len, "  Logical Drive= 0x%02x\n", mbox->logdrv);
3190 	len += sprintf(page+len, "  No of SG Elmt= 0x%02x\n",
3191 			mbox->numsgelements);
3192 	len += sprintf(page+len, "  Busy         = %01x\n", mbox->busy);
3193 	len += sprintf(page+len, "  Status       = 0x%02x\n", mbox->status);
3194 
3195 	*eof = 1;
3196 
3197 	return len;
3198 }
3199 
3200 
3201 /**
3202  * mega_allocate_inquiry()
3203  * @dma_handle - handle returned for dma address
3204  * @pdev - handle to pci device
3205  *
3206  * allocates memory for inquiry structure
3207  */
3208 static inline caddr_t
mega_allocate_inquiry(dma_addr_t * dma_handle,struct pci_dev * pdev)3209 mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
3210 {
3211 	return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
3212 }
3213 
3214 
3215 static inline void
mega_free_inquiry(caddr_t inquiry,dma_addr_t dma_handle,struct pci_dev * pdev)3216 mega_free_inquiry(caddr_t inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
3217 {
3218 	pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
3219 }
3220 
3221 
3222 /**
3223  * proc_rebuild_rate()
3224  * @page - buffer to write the data in
3225  * @start - where the actual data has been written in page
3226  * @offset - same meaning as the read system call
3227  * @count - same meaning as the read system call
3228  * @eof - set if no more data needs to be returned
3229  * @data - pointer to our soft state
3230  *
3231  * Display current rebuild rate
3232  */
3233 static int
proc_rebuild_rate(char * page,char ** start,off_t offset,int count,int * eof,void * data)3234 proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
3235 		void *data)
3236 {
3237 	adapter_t	*adapter = (adapter_t *)data;
3238 	dma_addr_t	dma_handle;
3239 	caddr_t		inquiry;
3240 	struct pci_dev	*pdev;
3241 	int	len = 0;
3242 
3243 	pdev = adapter->dev;
3244 
3245 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3246 		*eof = 1;
3247 		return len;
3248 	}
3249 
3250 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
3251 
3252 		len = sprintf(page, "Adapter inquiry failed.\n");
3253 
3254 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
3255 
3256 		mega_free_inquiry(inquiry, dma_handle, pdev);
3257 
3258 		*eof = 1;
3259 
3260 		return len;
3261 	}
3262 
3263 	if( adapter->flag & BOARD_40LD ) {
3264 		len = sprintf(page, "Rebuild Rate: [%d%%]\n",
3265 			((mega_inquiry3 *)inquiry)->rebuild_rate);
3266 	}
3267 	else {
3268 		len = sprintf(page, "Rebuild Rate: [%d%%]\n",
3269 			((mraid_ext_inquiry *)
3270 			inquiry)->raid_inq.adapter_info.rebuild_rate);
3271 	}
3272 
3273 
3274 	mega_free_inquiry(inquiry, dma_handle, pdev);
3275 
3276 	*eof = 1;
3277 
3278 	return len;
3279 }
3280 
3281 
3282 /**
3283  * proc_battery()
3284  * @page - buffer to write the data in
3285  * @start - where the actual data has been written in page
3286  * @offset - same meaning as the read system call
3287  * @count - same meaning as the read system call
3288  * @eof - set if no more data needs to be returned
3289  * @data - pointer to our soft state
3290  *
3291  * Display information about the battery module on the controller.
3292  */
3293 static int
proc_battery(char * page,char ** start,off_t offset,int count,int * eof,void * data)3294 proc_battery(char *page, char **start, off_t offset, int count, int *eof,
3295 		void *data)
3296 {
3297 	adapter_t	*adapter = (adapter_t *)data;
3298 	dma_addr_t	dma_handle;
3299 	caddr_t		inquiry;
3300 	struct pci_dev	*pdev;
3301 	u8	battery_status = 0;
3302 	char	str[256];
3303 	int	len = 0;
3304 
3305 	pdev = adapter->dev;
3306 
3307 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3308 		*eof = 1;
3309 		return len;
3310 	}
3311 
3312 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
3313 
3314 		len = sprintf(page, "Adapter inquiry failed.\n");
3315 
3316 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
3317 
3318 		mega_free_inquiry(inquiry, dma_handle, pdev);
3319 
3320 		*eof = 1;
3321 
3322 		return len;
3323 	}
3324 
3325 	if( adapter->flag & BOARD_40LD ) {
3326 		battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
3327 	}
3328 	else {
3329 		battery_status = ((mraid_ext_inquiry *)inquiry)->
3330 			raid_inq.adapter_info.battery_status;
3331 	}
3332 
3333 	/*
3334 	 * Decode the battery status
3335 	 */
3336 	sprintf(str, "Battery Status:[%d]", battery_status);
3337 
3338 	if(battery_status == MEGA_BATT_CHARGE_DONE)
3339 		strcat(str, " Charge Done");
3340 
3341 	if(battery_status & MEGA_BATT_MODULE_MISSING)
3342 		strcat(str, " Module Missing");
3343 
3344 	if(battery_status & MEGA_BATT_LOW_VOLTAGE)
3345 		strcat(str, " Low Voltage");
3346 
3347 	if(battery_status & MEGA_BATT_TEMP_HIGH)
3348 		strcat(str, " Temperature High");
3349 
3350 	if(battery_status & MEGA_BATT_PACK_MISSING)
3351 		strcat(str, " Pack Missing");
3352 
3353 	if(battery_status & MEGA_BATT_CHARGE_INPROG)
3354 		strcat(str, " Charge In-progress");
3355 
3356 	if(battery_status & MEGA_BATT_CHARGE_FAIL)
3357 		strcat(str, " Charge Fail");
3358 
3359 	if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
3360 		strcat(str, " Cycles Exceeded");
3361 
3362 	len = sprintf(page, "%s\n", str);
3363 
3364 
3365 	mega_free_inquiry(inquiry, dma_handle, pdev);
3366 
3367 	*eof = 1;
3368 
3369 	return len;
3370 }
3371 
3372 
3373 /**
3374  * proc_pdrv_ch0()
3375  * @page - buffer to write the data in
3376  * @start - where the actual data has been written in page
3377  * @offset - same meaning as the read system call
3378  * @count - same meaning as the read system call
3379  * @eof - set if no more data needs to be returned
3380  * @data - pointer to our soft state
3381  *
3382  * Display information about the physical drives on physical channel 0.
3383  */
3384 static int
proc_pdrv_ch0(char * page,char ** start,off_t offset,int count,int * eof,void * data)3385 proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
3386 		void *data)
3387 {
3388 	adapter_t *adapter = (adapter_t *)data;
3389 
3390 	*eof = 1;
3391 
3392 	return (proc_pdrv(adapter, page, 0));
3393 }
3394 
3395 
3396 /**
3397  * proc_pdrv_ch1()
3398  * @page - buffer to write the data in
3399  * @start - where the actual data has been written in page
3400  * @offset - same meaning as the read system call
3401  * @count - same meaning as the read system call
3402  * @eof - set if no more data needs to be returned
3403  * @data - pointer to our soft state
3404  *
3405  * Display information about the physical drives on physical channel 1.
3406  */
3407 static int
proc_pdrv_ch1(char * page,char ** start,off_t offset,int count,int * eof,void * data)3408 proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
3409 		void *data)
3410 {
3411 	adapter_t *adapter = (adapter_t *)data;
3412 
3413 	*eof = 1;
3414 
3415 	return (proc_pdrv(adapter, page, 1));
3416 }
3417 
3418 
3419 /**
3420  * proc_pdrv_ch2()
3421  * @page - buffer to write the data in
3422  * @start - where the actual data has been written in page
3423  * @offset - same meaning as the read system call
3424  * @count - same meaning as the read system call
3425  * @eof - set if no more data needs to be returned
3426  * @data - pointer to our soft state
3427  *
3428  * Display information about the physical drives on physical channel 2.
3429  */
3430 static int
proc_pdrv_ch2(char * page,char ** start,off_t offset,int count,int * eof,void * data)3431 proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
3432 		void *data)
3433 {
3434 	adapter_t *adapter = (adapter_t *)data;
3435 
3436 	*eof = 1;
3437 
3438 	return (proc_pdrv(adapter, page, 2));
3439 }
3440 
3441 
3442 /**
3443  * proc_pdrv_ch3()
3444  * @page - buffer to write the data in
3445  * @start - where the actual data has been written in page
3446  * @offset - same meaning as the read system call
3447  * @count - same meaning as the read system call
3448  * @eof - set if no more data needs to be returned
3449  * @data - pointer to our soft state
3450  *
3451  * Display information about the physical drives on physical channel 3.
3452  */
3453 static int
proc_pdrv_ch3(char * page,char ** start,off_t offset,int count,int * eof,void * data)3454 proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
3455 		void *data)
3456 {
3457 	adapter_t *adapter = (adapter_t *)data;
3458 
3459 	*eof = 1;
3460 
3461 	return (proc_pdrv(adapter, page, 3));
3462 }
3463 
3464 
3465 /**
3466  * proc_pdrv()
3467  * @page - buffer to write the data in
3468  * @adapter - pointer to our soft state
3469  *
3470  * Display information about the physical drives.
3471  */
3472 static int
proc_pdrv(adapter_t * adapter,char * page,int channel)3473 proc_pdrv(adapter_t *adapter, char *page, int channel)
3474 {
3475 	dma_addr_t	dma_handle;
3476 	char		*scsi_inq;
3477 	dma_addr_t	scsi_inq_dma_handle;
3478 	caddr_t		inquiry;
3479 	struct pci_dev	*pdev;
3480 	u8	*pdrv_state;
3481 	u8	state;
3482 	int	tgt;
3483 	int	max_channels;
3484 	int	len = 0;
3485 	char	str[80];
3486 	int	i;
3487 
3488 	pdev = adapter->dev;
3489 
3490 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3491 		return len;
3492 	}
3493 
3494 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
3495 
3496 		len = sprintf(page, "Adapter inquiry failed.\n");
3497 
3498 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
3499 
3500 		mega_free_inquiry(inquiry, dma_handle, pdev);
3501 
3502 		return len;
3503 	}
3504 
3505 
3506 	scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
3507 
3508 	if( scsi_inq == NULL ) {
3509 		len = sprintf(page, "memory not available for scsi inq.\n");
3510 
3511 		mega_free_inquiry(inquiry, dma_handle, pdev);
3512 
3513 		return len;
3514 	}
3515 
3516 	if( adapter->flag & BOARD_40LD ) {
3517 		pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
3518 	}
3519 	else {
3520 		pdrv_state = ((mraid_ext_inquiry *)inquiry)->
3521 			raid_inq.pdrv_info.pdrv_state;
3522 	}
3523 
3524 	max_channels = adapter->product_info.nchannels;
3525 
3526 	if (channel >= max_channels) {
3527 		pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
3528 		mega_free_inquiry(inquiry, dma_handle, pdev);
3529 		return 0;
3530 	}
3531 
3532 	for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
3533 
3534 		i = channel*16 + tgt;
3535 
3536 		state = *(pdrv_state + i);
3537 
3538 		switch( state & 0x0F ) {
3539 
3540 		case PDRV_ONLINE:
3541 			sprintf(str, "Channel:%2d Id:%2d State: Online",
3542 				channel, tgt);
3543 			break;
3544 
3545 		case PDRV_FAILED:
3546 			sprintf(str, "Channel:%2d Id:%2d State: Failed",
3547 				channel, tgt);
3548 			break;
3549 
3550 		case PDRV_RBLD:
3551 			sprintf(str, "Channel:%2d Id:%2d State: Rebuild",
3552 				channel, tgt);
3553 			break;
3554 
3555 		case PDRV_HOTSPARE:
3556 			sprintf(str, "Channel:%2d Id:%2d State: Hot spare",
3557 				channel, tgt);
3558 			break;
3559 
3560 		default:
3561 			sprintf(str, "Channel:%2d Id:%2d State: Un-configured",
3562 				channel, tgt);
3563 			break;
3564 
3565 		}
3566 
3567 		/*
3568 		 * This interface displays inquiries for disk drives
3569 		 * only. Inquries for logical drives and non-disk
3570 		 * devices are available through /proc/scsi/scsi
3571 		 */
3572 		memset(scsi_inq, 0, 256);
3573 		if( mega_internal_dev_inquiry(adapter, channel, tgt,
3574 				scsi_inq_dma_handle) ||
3575 				(scsi_inq[0] & 0x1F) != TYPE_DISK ) {
3576 			continue;
3577 		}
3578 
3579 		/*
3580 		 * Check for overflow. We print less than 240
3581 		 * characters for inquiry
3582 		 */
3583 		if( (len + 240) >= ((int) PAGE_SIZE) ) break;
3584 
3585 		len += sprintf(page+len, "%s.\n", str);
3586 
3587 		len += mega_print_inquiry(page+len, scsi_inq);
3588 	}
3589 
3590 	pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
3591 
3592 	mega_free_inquiry(inquiry, dma_handle, pdev);
3593 
3594 	return len;
3595 }
3596 
3597 
3598 /*
3599  * Display scsi inquiry
3600  */
3601 static int
mega_print_inquiry(char * page,char * scsi_inq)3602 mega_print_inquiry(char *page, char *scsi_inq)
3603 {
3604 	int	len = 0;
3605 	int	i;
3606 
3607 	len = sprintf(page, "  Vendor: ");
3608 	for( i = 8; i < 16; i++ ) {
3609 		len += sprintf(page+len, "%c", scsi_inq[i]);
3610 	}
3611 
3612 	len += sprintf(page+len, "  Model: ");
3613 
3614 	for( i = 16; i < 32; i++ ) {
3615 		len += sprintf(page+len, "%c", scsi_inq[i]);
3616 	}
3617 
3618 	len += sprintf(page+len, "  Rev: ");
3619 
3620 	for( i = 32; i < 36; i++ ) {
3621 		len += sprintf(page+len, "%c", scsi_inq[i]);
3622 	}
3623 
3624 	len += sprintf(page+len, "\n");
3625 
3626 	i = scsi_inq[0] & 0x1f;
3627 
3628 	len += sprintf(page+len, "  Type:   %s ",
3629 		i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
3630 		   "Unknown          ");
3631 
3632 	len += sprintf(page+len,
3633 	"                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
3634 
3635 	if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
3636 		len += sprintf(page+len, " CCS\n");
3637 	else
3638 		len += sprintf(page+len, "\n");
3639 
3640 	return len;
3641 }
3642 
3643 
3644 /**
3645  * proc_rdrv_10()
3646  * @page - buffer to write the data in
3647  * @start - where the actual data has been written in page
3648  * @offset - same meaning as the read system call
3649  * @count - same meaning as the read system call
3650  * @eof - set if no more data needs to be returned
3651  * @data - pointer to our soft state
3652  *
3653  * Display real time information about the logical drives 0 through 9.
3654  */
3655 static int
proc_rdrv_10(char * page,char ** start,off_t offset,int count,int * eof,void * data)3656 proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
3657 		void *data)
3658 {
3659 	adapter_t *adapter = (adapter_t *)data;
3660 
3661 	*eof = 1;
3662 
3663 	return (proc_rdrv(adapter, page, 0, 9));
3664 }
3665 
3666 
3667 /**
3668  * proc_rdrv_20()
3669  * @page - buffer to write the data in
3670  * @start - where the actual data has been written in page
3671  * @offset - same meaning as the read system call
3672  * @count - same meaning as the read system call
3673  * @eof - set if no more data needs to be returned
3674  * @data - pointer to our soft state
3675  *
3676  * Display real time information about the logical drives 10 through 19.
3677  */
3678 static int
proc_rdrv_20(char * page,char ** start,off_t offset,int count,int * eof,void * data)3679 proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
3680 		void *data)
3681 {
3682 	adapter_t *adapter = (adapter_t *)data;
3683 
3684 	*eof = 1;
3685 
3686 	return (proc_rdrv(adapter, page, 10, 19));
3687 }
3688 
3689 
3690 /**
3691  * proc_rdrv_30()
3692  * @page - buffer to write the data in
3693  * @start - where the actual data has been written in page
3694  * @offset - same meaning as the read system call
3695  * @count - same meaning as the read system call
3696  * @eof - set if no more data needs to be returned
3697  * @data - pointer to our soft state
3698  *
3699  * Display real time information about the logical drives 20 through 29.
3700  */
3701 static int
proc_rdrv_30(char * page,char ** start,off_t offset,int count,int * eof,void * data)3702 proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
3703 		void *data)
3704 {
3705 	adapter_t *adapter = (adapter_t *)data;
3706 
3707 	*eof = 1;
3708 
3709 	return (proc_rdrv(adapter, page, 20, 29));
3710 }
3711 
3712 
3713 /**
3714  * proc_rdrv_40()
3715  * @page - buffer to write the data in
3716  * @start - where the actual data has been written in page
3717  * @offset - same meaning as the read system call
3718  * @count - same meaning as the read system call
3719  * @eof - set if no more data needs to be returned
3720  * @data - pointer to our soft state
3721  *
3722  * Display real time information about the logical drives 30 through 39.
3723  */
3724 static int
proc_rdrv_40(char * page,char ** start,off_t offset,int count,int * eof,void * data)3725 proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
3726 		void *data)
3727 {
3728 	adapter_t *adapter = (adapter_t *)data;
3729 
3730 	*eof = 1;
3731 
3732 	return (proc_rdrv(adapter, page, 30, 39));
3733 }
3734 
3735 
3736 /**
3737  * proc_rdrv()
3738  * @page - buffer to write the data in
3739  * @adapter - pointer to our soft state
3740  * @start - starting logical drive to display
3741  * @end - ending logical drive to display
3742  *
3743  * We do not print the inquiry information since its already available through
3744  * /proc/scsi/scsi interface
3745  */
3746 static int
proc_rdrv(adapter_t * adapter,char * page,int start,int end)3747 proc_rdrv(adapter_t *adapter, char *page, int start, int end)
3748 {
3749 	dma_addr_t	dma_handle;
3750 	logdrv_param	*lparam;
3751 	megacmd_t	mc;
3752 	char		*disk_array;
3753 	dma_addr_t	disk_array_dma_handle;
3754 	caddr_t		inquiry;
3755 	struct pci_dev	*pdev;
3756 	u8	*rdrv_state;
3757 	int	num_ldrv;
3758 	u32	array_sz;
3759 	int	len = 0;
3760 	int	i;
3761 	u8	span8_flag = 1;
3762 
3763 	pdev = adapter->dev;
3764 
3765 	if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3766 		return len;
3767 	}
3768 
3769 	if( mega_adapinq(adapter, dma_handle) != 0 ) {
3770 
3771 		len = sprintf(page, "Adapter inquiry failed.\n");
3772 
3773 		printk(KERN_WARNING "megaraid: inquiry failed.\n");
3774 
3775 		mega_free_inquiry(inquiry, dma_handle, pdev);
3776 
3777 		return len;
3778 	}
3779 
3780 	memset(&mc, 0, sizeof(megacmd_t));
3781 
3782 	if( adapter->flag & BOARD_40LD ) {
3783 
3784 		array_sz = sizeof(disk_array_40ld);
3785 
3786 		rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
3787 
3788 		num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
3789 	}
3790 	else {
3791 		/*
3792 		 * 'array_sz' is either the size of diskarray_span4_t or the
3793 		 * size of disk_array_span8_t. We use span8_t's size because
3794 		 * it is bigger of the two.
3795 		 */
3796 		array_sz = sizeof( diskarray_span8_t );
3797 
3798 		rdrv_state = ((mraid_ext_inquiry *)inquiry)->
3799 			raid_inq.logdrv_info.ldrv_state;
3800 
3801 		num_ldrv = ((mraid_ext_inquiry *)inquiry)->
3802 			raid_inq.logdrv_info.num_ldrv;
3803 	}
3804 
3805 	disk_array = pci_alloc_consistent(pdev, array_sz,
3806 			&disk_array_dma_handle);
3807 
3808 	if( disk_array == NULL ) {
3809 		len = sprintf(page, "memory not available.\n");
3810 
3811 		mega_free_inquiry(inquiry, dma_handle, pdev);
3812 
3813 		return len;
3814 	}
3815 
3816 	mc.xferaddr = (u32)disk_array_dma_handle;
3817 
3818 	if( adapter->flag & BOARD_40LD ) {
3819 		mc.cmd = FC_NEW_CONFIG;
3820 		mc.opcode = OP_DCMD_READ_CONFIG;
3821 
3822 		if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
3823 
3824 			len = sprintf(page, "40LD read config failed.\n");
3825 
3826 			mega_free_inquiry(inquiry, dma_handle, pdev);
3827 
3828 			pci_free_consistent(pdev, array_sz, disk_array,
3829 					disk_array_dma_handle);
3830 
3831 			return len;
3832 		}
3833 
3834 	}
3835 	else {
3836 		/*
3837 		 * Try 8-Span "read config" command
3838 		 */
3839 		mc.cmd = NEW_READ_CONFIG_8LD;
3840 
3841 		if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
3842 
3843 			/*
3844 			 * 8-Span command failed; try 4-Span command
3845 			 */
3846 			span8_flag = 0;
3847 			mc.cmd = READ_CONFIG_8LD;
3848 
3849 			if( mega_internal_command(adapter, LOCK_INT, &mc,
3850 						NULL) ){
3851 
3852 				len = sprintf(page,
3853 					"8LD read config failed.\n");
3854 
3855 				mega_free_inquiry(inquiry, dma_handle, pdev);
3856 
3857 				pci_free_consistent(pdev, array_sz,
3858 						disk_array,
3859 						disk_array_dma_handle);
3860 
3861 				return len;
3862 			}
3863 		}
3864 	}
3865 
3866 	for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
3867 
3868 		if( adapter->flag & BOARD_40LD ) {
3869 			lparam =
3870 			&((disk_array_40ld *)disk_array)->ldrv[i].lparam;
3871 		}
3872 		else {
3873 			if( span8_flag ) {
3874 				lparam = (logdrv_param*) &((diskarray_span8_t*)
3875 						(disk_array))->log_drv[i];
3876 			}
3877 			else {
3878 				lparam = (logdrv_param*) &((diskarray_span4_t*)
3879 						(disk_array))->log_drv[i];
3880 			}
3881 		}
3882 
3883 		/*
3884 		 * Check for overflow. We print less than 240 characters for
3885 		 * information about each logical drive.
3886 		 */
3887 		if( (len + 240) >= ((int) PAGE_SIZE) ) break;
3888 
3889 		len += sprintf(page+len, "Logical drive:%2d:, ", i);
3890 
3891 		switch( rdrv_state[i] & 0x0F ) {
3892 		case RDRV_OFFLINE:
3893 			len += sprintf(page+len, "state: offline");
3894 			break;
3895 
3896 		case RDRV_DEGRADED:
3897 			len += sprintf(page+len, "state: degraded");
3898 			break;
3899 
3900 		case RDRV_OPTIMAL:
3901 			len += sprintf(page+len, "state: optimal");
3902 			break;
3903 
3904 		case RDRV_DELETED:
3905 			len += sprintf(page+len, "state: deleted");
3906 			break;
3907 
3908 		default:
3909 			len += sprintf(page+len, "state: unknown");
3910 			break;
3911 		}
3912 
3913 		/*
3914 		 * Check if check consistency or initialization is going on
3915 		 * for this logical drive.
3916 		 */
3917 		if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3918 			len += sprintf(page+len,
3919 					", check-consistency in progress");
3920 		}
3921 		else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3922 			len += sprintf(page+len,
3923 					", initialization in progress");
3924 		}
3925 
3926 		len += sprintf(page+len, "\n");
3927 
3928 		len += sprintf(page+len, "Span depth:%3d, ",
3929 				lparam->span_depth);
3930 
3931 		len += sprintf(page+len, "RAID level:%3d, ",
3932 				lparam->level);
3933 
3934 		len += sprintf(page+len, "Stripe size:%3d, ",
3935 				lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3936 
3937 		len += sprintf(page+len, "Row size:%3d\n",
3938 				lparam->row_size);
3939 
3940 
3941 		len += sprintf(page+len, "Read Policy: ");
3942 
3943 		switch(lparam->read_ahead) {
3944 
3945 		case NO_READ_AHEAD:
3946 			len += sprintf(page+len, "No read ahead, ");
3947 			break;
3948 
3949 		case READ_AHEAD:
3950 			len += sprintf(page+len, "Read ahead, ");
3951 			break;
3952 
3953 		case ADAP_READ_AHEAD:
3954 			len += sprintf(page+len, "Adaptive, ");
3955 			break;
3956 
3957 		}
3958 
3959 		len += sprintf(page+len, "Write Policy: ");
3960 
3961 		switch(lparam->write_mode) {
3962 
3963 		case WRMODE_WRITE_THRU:
3964 			len += sprintf(page+len, "Write thru, ");
3965 			break;
3966 
3967 		case WRMODE_WRITE_BACK:
3968 			len += sprintf(page+len, "Write back, ");
3969 			break;
3970 		}
3971 
3972 		len += sprintf(page+len, "Cache Policy: ");
3973 
3974 		switch(lparam->direct_io) {
3975 
3976 		case CACHED_IO:
3977 			len += sprintf(page+len, "Cached IO\n\n");
3978 			break;
3979 
3980 		case DIRECT_IO:
3981 			len += sprintf(page+len, "Direct IO\n\n");
3982 			break;
3983 		}
3984 	}
3985 
3986 	mega_free_inquiry(inquiry, dma_handle, pdev);
3987 
3988 	pci_free_consistent(pdev, array_sz, disk_array,
3989 			disk_array_dma_handle);
3990 
3991 	return len;
3992 }
3993 
3994 #endif
3995 
3996 
3997 /**
3998  * megaraid_reboot_notify()
3999  * @this - unused
4000  * @code - shutdown code
4001  * @unused - unused
4002  *
4003  * This routine will be called when the use has done a forced shutdown on the
4004  * system. Flush the Adapter and disks cache.
4005  */
4006 static int
megaraid_reboot_notify(struct notifier_block * this,unsigned long code,void * unused)4007 megaraid_reboot_notify (struct notifier_block *this, unsigned long code,
4008 		void *unused)
4009 {
4010 	DECLARE_WAIT_QUEUE_HEAD(wq);
4011 	adapter_t *adapter;
4012 	struct Scsi_Host *host;
4013 	u8 raw_mbox[sizeof(mbox_t)];
4014 	mbox_t *mbox;
4015 	int i;
4016 
4017 	/*
4018 	 * Flush the controller's cache irrespective of the codes coming down.
4019 	 * SYS_DOWN, SYS_HALT, SYS_RESTART, SYS_POWER_OFF
4020 	 */
4021 	for( i = 0; i < hba_count; i++ ) {
4022 		printk(KERN_INFO "megaraid: flushing adapter %d..", i);
4023 		host = hba_soft_state[i]->host;
4024 
4025 		adapter = (adapter_t *)host->hostdata;
4026 		mbox = (mbox_t *)raw_mbox;
4027 
4028 		/* Flush adapter cache */
4029 		memset(raw_mbox, 0, sizeof(raw_mbox));
4030 		raw_mbox[0] = FLUSH_ADAPTER;
4031 
4032 		if (adapter->flag & BOARD_IOMAP)
4033 			irq_disable(adapter);
4034 
4035 		free_irq(adapter->host->irq, adapter);
4036 
4037 		/*
4038 		 * Issue a blocking (interrupts disabled) command to
4039 		 * the card
4040 		 */
4041 		issue_scb_block(adapter, raw_mbox);
4042 
4043 		/* Flush disks cache */
4044 		memset(raw_mbox, 0, sizeof(raw_mbox));
4045 		raw_mbox[0] = FLUSH_SYSTEM;
4046 
4047 		issue_scb_block(adapter, raw_mbox);
4048 
4049 		printk("Done.\n");
4050 
4051 		if( atomic_read(&adapter->pend_cmds) > 0 ) {
4052 			printk(KERN_WARNING "megaraid: pending commands!!\n");
4053 		}
4054 	}
4055 
4056 	/*
4057 	 * Have a delibrate delay to make sure all the caches are
4058 	 * actually flushed.
4059 	 */
4060 	printk(KERN_INFO "megaraid: cache flush delay:   ");
4061 	for( i = 9; i >= 0; i-- ) {
4062 		printk("\b\b\b[%d]", i);
4063 		sleep_on_timeout(&wq, HZ);
4064 	}
4065 	printk("\b\b\b[done]\n");
4066 	sleep_on_timeout(&wq, HZ);
4067 
4068 	return NOTIFY_DONE;
4069 }
4070 
4071 /**
4072  * mega_init_scb()
4073  * @adapter - pointer to our soft state
4074  *
4075  * Allocate memory for the various pointers in the scb structures:
4076  * scatter-gather list pointer, passthru and extended passthru structure
4077  * pointers.
4078  */
4079 static int
mega_init_scb(adapter_t * adapter)4080 mega_init_scb(adapter_t *adapter)
4081 {
4082 	scb_t	*scb;
4083 	int	i;
4084 
4085 	for( i = 0; i < adapter->max_cmds; i++ ) {
4086 
4087 		scb = &adapter->scb_list[i];
4088 
4089 		scb->sgl64 = NULL;
4090 		scb->sgl = NULL;
4091 		scb->pthru = NULL;
4092 		scb->epthru = NULL;
4093 	}
4094 
4095 	for( i = 0; i < adapter->max_cmds; i++ ) {
4096 
4097 		scb = &adapter->scb_list[i];
4098 
4099 		scb->idx = i;
4100 
4101 		scb->sgl64 = pci_alloc_consistent(adapter->dev,
4102 				sizeof(mega_sgl64) * adapter->sglen,
4103 				&scb->sgl_dma_addr);
4104 
4105 		scb->sgl = (mega_sglist *)scb->sgl64;
4106 
4107 		if( !scb->sgl ) {
4108 			printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
4109 			mega_free_sgl(adapter);
4110 			return -1;
4111 		}
4112 
4113 		scb->pthru = pci_alloc_consistent(adapter->dev,
4114 				sizeof(mega_passthru),
4115 				&scb->pthru_dma_addr);
4116 
4117 		if( !scb->pthru ) {
4118 			printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
4119 			mega_free_sgl(adapter);
4120 			return -1;
4121 		}
4122 
4123 		scb->epthru = pci_alloc_consistent(adapter->dev,
4124 				sizeof(mega_ext_passthru),
4125 				&scb->epthru_dma_addr);
4126 
4127 		if( !scb->epthru ) {
4128 			printk(KERN_WARNING
4129 				"Can't allocate extended passthru.\n");
4130 			mega_free_sgl(adapter);
4131 			return -1;
4132 		}
4133 
4134 
4135 		scb->dma_type = MEGA_DMA_TYPE_NONE;
4136 
4137 		/*
4138 		 * Link to free list
4139 		 * lock not required since we are loading the driver, so no
4140 		 * commands possible right now.
4141 		 */
4142 		scb->state = SCB_FREE;
4143 		scb->cmd = NULL;
4144 		list_add(&scb->list, &adapter->free_list);
4145 	}
4146 
4147 	return 0;
4148 }
4149 
4150 
4151 /**
4152  * megadev_open()
4153  * @inode - unused
4154  * @filep - unused
4155  *
4156  * Routines for the character/ioctl interface to the driver. Find out if this
4157  * is a valid open. If yes, increment the module use count so that it cannot
4158  * be unloaded.
4159  */
4160 static int
megadev_open(struct inode * inode,struct file * filep)4161 megadev_open (struct inode *inode, struct file *filep)
4162 {
4163 	/*
4164 	 * Only allow superuser to access private ioctl interface
4165 	 */
4166 	if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
4167 
4168 	MOD_INC_USE_COUNT;
4169 	return 0;
4170 }
4171 
4172 
4173 #ifdef LSI_CONFIG_COMPAT
4174 static int
megadev_compat_ioctl(unsigned int fd,unsigned int cmd,unsigned long arg,struct file * filep)4175 megadev_compat_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg,
4176 		struct file *filep)
4177 {
4178 	struct inode *inode = filep->f_dentry->d_inode;
4179 
4180 	return megadev_ioctl_entry(inode, filep, cmd, arg);
4181 }
4182 #endif
4183 
4184 static int
megadev_ioctl_entry(struct inode * inode,struct file * filep,unsigned int cmd,unsigned long arg)4185 megadev_ioctl_entry(struct inode *inode, struct file *filep, unsigned int cmd,
4186 		unsigned long arg)
4187 {
4188 	int rval;
4189 	down( &megaraid_ioc_mtx );
4190 	rval = megadev_ioctl( inode, filep, cmd, arg );
4191 	up( &megaraid_ioc_mtx );
4192 	return rval;
4193 }
4194 
4195 /**
4196  * megadev_ioctl()
4197  * @inode - Our device inode
4198  * @filep - unused
4199  * @cmd - ioctl command
4200  * @arg - user buffer
4201  *
4202  * ioctl entry point for our private ioctl interface. We move the data in from
4203  * the user space, prepare the command (if necessary, convert the old MIMD
4204  * ioctl to new ioctl command), and issue a synchronous command to the
4205  * controller.
4206  */
4207 static int
megadev_ioctl(struct inode * inode,struct file * filep,unsigned int cmd,unsigned long arg)4208 megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
4209 		unsigned long arg)
4210 {
4211 	adapter_t	*adapter;
4212 	nitioctl_t	uioc;
4213 	int		adapno;
4214 	int		rval;
4215 	mega_passthru	*upthru;	/* user address for passthru */
4216 	mega_passthru	*pthru;		/* copy user passthru here */
4217 	void		*data = NULL;	/* data to be transferred */
4218 	dma_addr_t	data_dma_hndl = 0;
4219 	megacmd_t	mc;
4220 	megastat_t	*ustats;
4221 	int		num_ldrv;
4222 	caddr_t		uxferaddr=NULL;
4223 	struct pci_dev	*pdev;
4224 
4225 	ustats = NULL; /* avoid compilation warnings */
4226 	num_ldrv = 0;
4227 
4228 	/*
4229 	 * Make sure only USCSICMD are issued through this interface.
4230 	 * MIMD application would still fire different command.
4231 	 */
4232 	if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
4233 		return -EINVAL;
4234 	}
4235 
4236 	/*
4237 	 * Check and convert a possible MIMD command to NIT command.
4238 	 * mega_m_to_n() copies the data from the user space, so we do not
4239 	 * have to do it here.
4240 	 * NOTE: We will need some user address to copyout the data, therefore
4241 	 * the inteface layer will also provide us with the required user
4242 	 * addresses.
4243 	 */
4244 	memset(&uioc, 0, sizeof(nitioctl_t));
4245 	if( (rval = mega_m_to_n( (void *)arg, &uioc)) != 0 )
4246 		return rval;
4247 
4248 
4249 	switch( uioc.opcode ) {
4250 
4251 	case GET_DRIVER_VER:
4252 		if( put_user(driver_ver, (u32 *)uioc.u_dataaddr) )
4253 			return (-EFAULT);
4254 
4255 		break;
4256 
4257 	case GET_N_ADAP:
4258 		if( put_user(hba_count, (u32 *)uioc.u_dataaddr) )
4259 			return (-EFAULT);
4260 
4261 		/*
4262 		 * Shucks. MIMD interface returns a positive value for number
4263 		 * of adapters. TODO: Change it to return 0 when there is no
4264 		 * applicatio using mimd interface.
4265 		 */
4266 		return hba_count;
4267 
4268 	case GET_ADAP_INFO:
4269 
4270 		/*
4271 		 * Which adapter
4272 		 */
4273 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
4274 			return (-ENODEV);
4275 
4276 		if( copy_to_user(uioc.u_dataaddr, mcontroller+adapno,
4277 				sizeof(struct mcontroller)) )
4278 			return (-EFAULT);
4279 		break;
4280 
4281 #if MEGA_HAVE_STATS
4282 
4283 	case GET_STATS:
4284 		/*
4285 		 * Which adapter
4286 		 */
4287 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
4288 			return (-ENODEV);
4289 
4290 		adapter = hba_soft_state[adapno];
4291 
4292 		ustats = (megastat_t *)uioc.u_dataaddr;
4293 
4294 		if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
4295 			return (-EFAULT);
4296 
4297 		/*
4298 		 * Check for the validity of the logical drive number
4299 		 */
4300 		if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
4301 
4302 		if( copy_to_user(ustats->nreads, adapter->nreads,
4303 					num_ldrv*sizeof(u32)) )
4304 			return -EFAULT;
4305 
4306 		if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
4307 					num_ldrv*sizeof(u32)) )
4308 			return -EFAULT;
4309 
4310 		if( copy_to_user(ustats->nwrites, adapter->nwrites,
4311 					num_ldrv*sizeof(u32)) )
4312 			return -EFAULT;
4313 
4314 		if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
4315 					num_ldrv*sizeof(u32)) )
4316 			return -EFAULT;
4317 
4318 		if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
4319 					num_ldrv*sizeof(u32)) )
4320 			return -EFAULT;
4321 
4322 		if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
4323 					num_ldrv*sizeof(u32)) )
4324 			return -EFAULT;
4325 
4326 		return 0;
4327 
4328 #endif
4329 	case MBOX_CMD:
4330 
4331 		/*
4332 		 * Which adapter
4333 		 */
4334 		if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
4335 			return (-ENODEV);
4336 
4337 		adapter = hba_soft_state[adapno];
4338 
4339 		/*
4340 		 * Deletion of logical drive is a special case. The adapter
4341 		 * should be quiescent before this command is issued.
4342 		 */
4343 		if( RMBOX(uioc)[0] == FC_DEL_LOGDRV ) {
4344 			if ( RMBOX(uioc)[2] == OP_DEL_LOGDRV ) {
4345 				/*
4346 				 * Do we support this feature
4347 				 */
4348 				if( !adapter->support_random_del ) {
4349 					printk(KERN_WARNING "megaraid: logdrv ");
4350 					printk("delete on non-supporting F/W.\n");
4351 
4352 					return (-EINVAL);
4353 				}
4354 
4355 				rval = mega_del_logdrv( adapter, RMBOX(uioc)[3] );
4356 
4357 				if( rval == 0 ) {
4358 					memset(&mc, 0, sizeof(megacmd_t));
4359 
4360 					mc.status = rval;
4361 
4362 					rval = mega_n_to_m((void *)arg, &mc);
4363 				}
4364 
4365 				return rval;
4366 			}
4367 		}
4368 		/*
4369 		 * This interface only support the regular passthru commands.
4370 		 * Reject extended passthru and 64-bit passthru
4371 		 */
4372 		if( RMBOX(uioc)[0] == MEGA_MBOXCMD_PASSTHRU64 ||
4373 			RMBOX(uioc)[0] == MEGA_MBOXCMD_EXTPTHRU ) {
4374 
4375 			printk(KERN_WARNING "megaraid: rejected passthru.\n");
4376 
4377 			return (-EINVAL);
4378 		}
4379 
4380 		/*
4381 		 * For all internal commands, the buffer must be allocated in
4382 		 * <4GB address range
4383 		 */
4384 		pdev = adapter->dev;
4385 
4386 		/* Is it a passthru command or a DCMD */
4387 		if( RMBOX(uioc)[0] == MEGA_MBOXCMD_PASSTHRU ) {
4388 			/* Passthru commands */
4389 
4390 			pthru = adapter->int_pthru;
4391 
4392 			/*
4393 			 * The user passthru structure
4394 			 */
4395 			 upthru = &uioc.pthru;
4396 			 memcpy(pthru, (char *)upthru,sizeof(mega_passthru));
4397 			/*
4398 			 * Is there a data transfer; If the data transfer
4399 			 * length is <= INT_MEMBLK_SZ, usr the buffer
4400 			 * allocated at the load time. Otherwise, allocate it
4401 			 * here.
4402 			 */
4403 			if (pthru->dataxferlen) {
4404 				if (pthru->dataxferlen > INT_MEMBLK_SZ) {
4405 					data = pci_alloc_consistent (
4406 							pdev,
4407 							pthru->dataxferlen,
4408 							&data_dma_hndl );
4409 
4410 					if (data == NULL) {
4411 						return (-ENOMEM);
4412 					}
4413 				}
4414 				else {
4415 					data = adapter->int_data;
4416 				}
4417 
4418 				/*
4419 				 * Save the user address and point the kernel
4420 				 * address at just allocated memory
4421 				 */
4422 				uxferaddr = (caddr_t) uioc.u_dataaddr;
4423 				if (data_dma_hndl)
4424 					pthru->dataxferaddr = data_dma_hndl;
4425 				else
4426 					pthru->dataxferaddr =
4427 						adapter->int_data_dma_hndl;
4428 			}
4429 
4430 
4431 			/*
4432 			 * Is data coming down-stream
4433 			 */
4434 			if(pthru->dataxferlen  && (uioc.flags & UIOC_WR) ) {
4435 				/*
4436 				 * Get the user data
4437 				 */
4438 				if( copy_from_user(data,
4439 						(char *)uxferaddr,
4440 						pthru->dataxferlen) ) {
4441 					rval = (-EFAULT);
4442 					goto freedata_and_return;
4443 				}
4444 			}
4445 
4446 			memset(&mc, 0, sizeof(megacmd_t));
4447 
4448 			mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4449 			mc.xferaddr = (u32)adapter->int_pthru_dma_hndl;
4450 
4451 			/*
4452 			 * Issue the command
4453 			 */
4454 			mega_internal_command(adapter, LOCK_INT, &mc, pthru);
4455 
4456 			rval = mega_n_to_m((void *)arg, &mc);
4457 
4458 			if( rval ) goto freedata_and_return;
4459 
4460 
4461 			/*
4462 			 * Is data going up-stream
4463 			 */
4464 			if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
4465 				if( copy_to_user((char *)uxferaddr,
4466 						data, pthru->dataxferlen) ) {
4467 					rval = (-EFAULT);
4468 				}
4469 			}
4470 
4471 			/*
4472 			 * Send the request sense data also, irrespective of
4473 			 * whether the user has asked for it or not.
4474 			 */
4475 			copy_to_user(upthru->reqsensearea,
4476 					pthru->reqsensearea, 14);
4477 freedata_and_return:
4478 			if (data_dma_hndl) {
4479 				pci_free_consistent( pdev, pthru->dataxferlen,
4480 							data, data_dma_hndl );
4481 			}
4482 
4483 			return rval;
4484 
4485 		}
4486 		else {
4487 			/* DCMD commands */
4488 
4489 			/*
4490 			 * Is there a data transfer
4491 			 */
4492 			if( uioc.xferlen ) {
4493 				if (uioc.xferlen > INT_MEMBLK_SZ) {
4494 					data = pci_alloc_consistent(
4495 							pdev,
4496 							uioc.xferlen,
4497 							&data_dma_hndl );
4498 
4499 					if (data == NULL)
4500 						return (-ENOMEM);
4501 				}
4502 				else {
4503 					data = adapter->int_data;
4504 				}
4505 				uxferaddr = uioc.u_dataaddr;
4506 			}
4507 
4508 			/*
4509 			 * Is data coming down-stream
4510 			 */
4511 			if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
4512 				/*
4513 				 * Get the user data
4514 				 */
4515 				if( copy_from_user(data,
4516 						(char *)((ulong)uxferaddr),
4517 						uioc.xferlen) ) {
4518 
4519 					pci_free_consistent(
4520 						pdev, uioc.xferlen,
4521 						data, data_dma_hndl );
4522 
4523 					return (-EFAULT);
4524 				}
4525 			}
4526 
4527 			memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
4528 
4529 			if (data_dma_hndl )
4530 				mc.xferaddr = (u32)data_dma_hndl;
4531 			else
4532 				mc.xferaddr = (u32)(adapter->int_data_dma_hndl);
4533 
4534 			/*
4535 			 * Issue the command
4536 			 */
4537 			mega_internal_command(adapter, LOCK_INT, &mc, NULL);
4538 
4539 			rval = mega_n_to_m((void *)arg, &mc);
4540 
4541 			if( rval ) {
4542 				if (data_dma_hndl) {
4543 					pci_free_consistent( pdev, uioc.xferlen,
4544 							data, data_dma_hndl );
4545 				}
4546 				return rval;
4547 			}
4548 
4549 			/*
4550 			 * Is data going up-stream
4551 			 */
4552 			if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
4553 				if( copy_to_user((char *)uxferaddr,
4554 						data, uioc.xferlen) ) {
4555 
4556 					rval = (-EFAULT);
4557 				}
4558 			}
4559 
4560 			if (data_dma_hndl) {
4561 				pci_free_consistent( pdev, uioc.xferlen,
4562 							data, data_dma_hndl );
4563 			}
4564 
4565 			return rval;
4566 		}
4567 
4568 	default:
4569 		return (-EINVAL);
4570 	}
4571 
4572 	return 0;
4573 }
4574 
4575 /**
4576  * mega_m_to_n()
4577  * @arg - user address
4578  * @uioc - new ioctl structure
4579  *
4580  * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
4581  * structure
4582  *
4583  * Converts the older mimd ioctl structure to newer NIT structure
4584  */
4585 static int
mega_m_to_n(void * arg,nitioctl_t * uioc)4586 mega_m_to_n(void *arg, nitioctl_t *uioc)
4587 {
4588 	struct uioctl_t	uioc_mimd;
4589 	char	signature[8] = {0};
4590 	u8	opcode;
4591 	u8	subopcode;
4592 
4593 
4594 	/*
4595 	 * check is the application conforms to NIT. We do not have to do much
4596 	 * in that case.
4597 	 * We exploit the fact that the signature is stored in the very
4598 	 * begining of the structure.
4599 	 */
4600 
4601 	if( copy_from_user(signature, (char *)arg, 7) )
4602 		return (-EFAULT);
4603 
4604 	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
4605 
4606 		/*
4607 		 * NOTE NOTE: The nit ioctl is still under flux because of
4608 		 * change of mailbox definition, in HPE. No applications yet
4609 		 * use this interface and let's not have applications use this
4610 		 * interface till the new specifitions are in place.
4611 		 */
4612 		return -EINVAL;
4613 #if 0
4614 		if( copy_from_user(uioc, (char *)arg, sizeof(nitioctl_t)) )
4615 			return (-EFAULT);
4616 		return 0;
4617 #endif
4618 	}
4619 
4620 	/*
4621 	 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
4622 	 *
4623 	 * Get the user ioctl structure
4624 	 */
4625 	if( copy_from_user(&uioc_mimd, (char *)arg, sizeof(struct uioctl_t)) )
4626 		return (-EFAULT);
4627 
4628 
4629 	/*
4630 	 * Get the opcode and subopcode for the commands
4631 	 */
4632 	opcode = uioc_mimd.ui.fcs.opcode;
4633 	subopcode = uioc_mimd.ui.fcs.subopcode;
4634 
4635 	switch (opcode) {
4636 	case 0x82:
4637 
4638 		switch (subopcode) {
4639 
4640 		case MEGAIOC_QDRVRVER:	/* Query driver version */
4641 			uioc->opcode = GET_DRIVER_VER;
4642 			uioc->u_dataaddr = uioc_mimd.data;
4643 			break;
4644 
4645 		case MEGAIOC_QNADAP:	/* Get # of adapters */
4646 			uioc->opcode = GET_N_ADAP;
4647 			uioc->u_dataaddr = uioc_mimd.data;
4648 			break;
4649 
4650 		case MEGAIOC_QADAPINFO:	/* Get adapter information */
4651 			uioc->opcode = GET_ADAP_INFO;
4652 			uioc->adapno = uioc_mimd.ui.fcs.adapno;
4653 			uioc->u_dataaddr = uioc_mimd.data;
4654 			break;
4655 
4656 		default:
4657 			return(-EINVAL);
4658 		}
4659 
4660 		break;
4661 
4662 
4663 	case 0x81:
4664 
4665 		uioc->opcode = MBOX_CMD;
4666 		uioc->adapno = uioc_mimd.ui.fcs.adapno;
4667 
4668 		memcpy(&uioc->u_mbox, uioc_mimd.mbox, 18);
4669 
4670 		uioc->xferlen = uioc_mimd.ui.fcs.length;
4671 		uioc->u_dataaddr = uioc_mimd.ui.fcs.buffer;
4672 
4673 		if (uioc_mimd.mbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
4674 			memcpy(&uioc->pthru,&uioc_mimd.pthru,
4675 				sizeof(mega_passthru));
4676 
4677 		}
4678 
4679 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
4680 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
4681 
4682 		break;
4683 
4684 	case 0x80:
4685 
4686 		uioc->opcode = MBOX_CMD;
4687 		uioc->adapno = uioc_mimd.ui.fcs.adapno;
4688 
4689 		memcpy(&uioc->u_mbox, uioc_mimd.mbox, 18);
4690 
4691 		/*
4692 		 * Choose the xferlen bigger of input and output data
4693 		 */
4694 		uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
4695 			uioc_mimd.outlen : uioc_mimd.inlen;
4696 		uioc->u_dataaddr = uioc_mimd.data;
4697 
4698 		if (uioc_mimd.mbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
4699 			memcpy(&uioc->pthru,&uioc_mimd.pthru,
4700 				sizeof(mega_passthru));
4701 
4702 		}
4703 
4704 		if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
4705 		if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
4706 
4707 		break;
4708 
4709 	default:
4710 		return (-EINVAL);
4711 
4712 	}
4713 
4714 	return 0;
4715 }
4716 
4717 /*
4718  * mega_n_to_m()
4719  * @arg - user address
4720  * @mc - mailbox command
4721  *
4722  * Updates the status information to the application, depending on application
4723  * conforms to older mimd ioctl interface or newer NIT ioctl interface
4724  */
4725 static int
mega_n_to_m(void * arg,megacmd_t * mc)4726 mega_n_to_m(void *arg, megacmd_t *mc)
4727 {
4728 	megacmd_t	*umc;
4729 	megacmd_t	kmc;
4730 	mega_passthru	*upthru;
4731 	struct uioctl_t	*uioc_mimd;
4732 	char	signature[8] = {0};
4733 
4734 	/*
4735 	 * check is the application conforms to NIT.
4736 	 */
4737 	if( copy_from_user(signature, (char *)arg, 7) )
4738 		return -EFAULT;
4739 
4740 	if( memcmp(signature, "MEGANIT", 7) == 0 ) {
4741 
4742 
4743 		/*
4744 		 * NOTE: The nit ioctl is still under flux because of
4745 		 * change of mailbox definition, in HPE. No applications yet
4746 		 * use this interface and let's not have applications use this
4747 		 * interface till the new specifitions are in place.
4748 		 */
4749 		return -EINVAL;
4750 	}
4751 	else {
4752 		uioc_mimd = (struct uioctl_t *)arg;
4753 
4754 		if( put_user(mc->status, (u8 *)&uioc_mimd->mbox[17]) ) {
4755 			return (-EFAULT);
4756 		}
4757 
4758 		if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4759 
4760 			umc = (megacmd_t *)uioc_mimd->mbox;
4761 			if (copy_from_user(&kmc, umc, sizeof(megacmd_t))) {
4762 				return -EFAULT;
4763 			}
4764 			upthru = (mega_passthru *)((ulong)&uioc_mimd->pthru);
4765 
4766 			if( put_user(mc->status, (u8 *)&upthru->scsistatus) ){
4767 				return (-EFAULT);
4768 			}
4769 		}
4770 	}
4771 
4772 	return 0;
4773 }
4774 
4775 
4776 static int
megadev_close(struct inode * inode,struct file * filep)4777 megadev_close (struct inode *inode, struct file *filep)
4778 {
4779 	MOD_DEC_USE_COUNT;
4780 	return 0;
4781 }
4782 
4783 
4784 /*
4785  * MEGARAID 'FW' commands.
4786  */
4787 
4788 /**
4789  * mega_is_bios_enabled()
4790  * @adapter - pointer to our soft state
4791  *
4792  * issue command to find out if the BIOS is enabled for this controller
4793  */
4794 static int
mega_is_bios_enabled(adapter_t * adapter)4795 mega_is_bios_enabled(adapter_t *adapter)
4796 {
4797 	unsigned char	raw_mbox[sizeof(mbox_t)];
4798 	mbox_t	*mbox;
4799 	int	ret;
4800 
4801 	mbox = (mbox_t *)raw_mbox;
4802 
4803 	memset(raw_mbox, 0, sizeof(raw_mbox));
4804 
4805 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4806 
4807 	mbox->xferaddr = (u32)adapter->buf_dma_handle;
4808 
4809 	raw_mbox[0] = IS_BIOS_ENABLED;
4810 	raw_mbox[2] = GET_BIOS;
4811 
4812 
4813 	ret = issue_scb_block(adapter, raw_mbox);
4814 
4815 	return *(char *)adapter->mega_buffer;
4816 }
4817 
4818 
4819 /**
4820  * mega_enum_raid_scsi()
4821  * @adapter - pointer to our soft state
4822  *
4823  * Find out what channels are RAID/SCSI. This information is used to
4824  * differentiate the virtual channels and physical channels and to support
4825  * ROMB feature and non-disk devices.
4826  */
4827 static void
mega_enum_raid_scsi(adapter_t * adapter)4828 mega_enum_raid_scsi(adapter_t *adapter)
4829 {
4830 	unsigned char raw_mbox[sizeof(mbox_t)];
4831 	mbox_t *mbox;
4832 	int i;
4833 
4834 	mbox = (mbox_t *)raw_mbox;
4835 
4836 	memset(raw_mbox, 0, sizeof(raw_mbox));
4837 
4838 	/*
4839 	 * issue command to find out what channels are raid/scsi
4840 	 */
4841 	raw_mbox[0] = CHNL_CLASS;
4842 	raw_mbox[2] = GET_CHNL_CLASS;
4843 
4844 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4845 
4846 	mbox->xferaddr = (u32)adapter->buf_dma_handle;
4847 
4848 	/*
4849 	 * Non-ROMB firware fail this command, so all channels
4850 	 * must be shown RAID
4851 	 */
4852 	adapter->mega_ch_class = 0xFF;
4853 
4854 	if(!issue_scb_block(adapter, raw_mbox)) {
4855 		adapter->mega_ch_class = *((char *)adapter->mega_buffer);
4856 
4857 	}
4858 
4859 	for( i = 0; i < adapter->product_info.nchannels; i++ ) {
4860 		if( (adapter->mega_ch_class >> i) & 0x01 ) {
4861 			printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
4862 					i);
4863 		}
4864 		else {
4865 			printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
4866 					i);
4867 		}
4868 	}
4869 
4870 	return;
4871 }
4872 
4873 
4874 /**
4875  * mega_get_boot_drv()
4876  * @adapter - pointer to our soft state
4877  *
4878  * Find out which device is the boot device. Note, any logical drive or any
4879  * phyical device (e.g., a CDROM) can be designated as a boot device.
4880  */
4881 static void
mega_get_boot_drv(adapter_t * adapter)4882 mega_get_boot_drv(adapter_t *adapter)
4883 {
4884 	struct private_bios_data	*prv_bios_data;
4885 	unsigned char	raw_mbox[sizeof(mbox_t)];
4886 	mbox_t	*mbox;
4887 	u16	cksum = 0;
4888 	u8	*cksum_p;
4889 	u8	boot_pdrv;
4890 	int	i;
4891 
4892 	mbox = (mbox_t *)raw_mbox;
4893 
4894 	memset(raw_mbox, 0, sizeof(raw_mbox));
4895 
4896 	raw_mbox[0] = BIOS_PVT_DATA;
4897 	raw_mbox[2] = GET_BIOS_PVT_DATA;
4898 
4899 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4900 
4901 	mbox->xferaddr = (u32)adapter->buf_dma_handle;
4902 
4903 	adapter->boot_ldrv_enabled = 0;
4904 	adapter->boot_ldrv = 0;
4905 
4906 	adapter->boot_pdrv_enabled = 0;
4907 	adapter->boot_pdrv_ch = 0;
4908 	adapter->boot_pdrv_tgt = 0;
4909 
4910 	if(issue_scb_block(adapter, raw_mbox) == 0) {
4911 		prv_bios_data =
4912 			(struct private_bios_data *)adapter->mega_buffer;
4913 
4914 		cksum = 0;
4915 		cksum_p = (char *)prv_bios_data;
4916 		for (i = 0; i < 14; i++ ) {
4917 			cksum += (u16)(*cksum_p++);
4918 		}
4919 
4920 		if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4921 
4922 			/*
4923 			 * If MSB is set, a physical drive is set as boot
4924 			 * device
4925 			 */
4926 			if( prv_bios_data->boot_drv & 0x80 ) {
4927 				adapter->boot_pdrv_enabled = 1;
4928 				boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4929 				adapter->boot_pdrv_ch = boot_pdrv / 16;
4930 				adapter->boot_pdrv_tgt = boot_pdrv % 16;
4931 			}
4932 			else {
4933 				adapter->boot_ldrv_enabled = 1;
4934 				adapter->boot_ldrv = prv_bios_data->boot_drv;
4935 			}
4936 		}
4937 	}
4938 
4939 }
4940 
4941 /**
4942  * mega_support_random_del()
4943  * @adapter - pointer to our soft state
4944  *
4945  * Find out if this controller supports random deletion and addition of
4946  * logical drives
4947  */
4948 static int
mega_support_random_del(adapter_t * adapter)4949 mega_support_random_del(adapter_t *adapter)
4950 {
4951 	unsigned char raw_mbox[sizeof(mbox_t)];
4952 	mbox_t *mbox;
4953 	int rval;
4954 
4955 	mbox = (mbox_t *)raw_mbox;
4956 
4957 	memset(raw_mbox, 0, sizeof(raw_mbox));
4958 
4959 	/*
4960 	 * issue command
4961 	 */
4962 	raw_mbox[0] = FC_DEL_LOGDRV;
4963 	raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4964 
4965 	rval = issue_scb_block(adapter, raw_mbox);
4966 
4967 	return !rval;
4968 }
4969 
4970 
4971 /**
4972  * mega_support_ext_cdb()
4973  * @adapter - pointer to our soft state
4974  *
4975  * Find out if this firmware support cdblen > 10
4976  */
4977 static int
mega_support_ext_cdb(adapter_t * adapter)4978 mega_support_ext_cdb(adapter_t *adapter)
4979 {
4980 	unsigned char raw_mbox[sizeof(mbox_t)];
4981 	mbox_t *mbox;
4982 	int rval;
4983 
4984 	mbox = (mbox_t *)raw_mbox;
4985 
4986 	memset(raw_mbox, 0, sizeof(raw_mbox));
4987 	/*
4988 	 * issue command to find out if controller supports extended CDBs.
4989 	 */
4990 	raw_mbox[0] = 0xA4;
4991 	raw_mbox[2] = 0x16;
4992 
4993 	rval = issue_scb_block(adapter, raw_mbox);
4994 
4995 	return !rval;
4996 }
4997 
4998 
4999 /**
5000  * mega_del_logdrv()
5001  * @adapter - pointer to our soft state
5002  * @logdrv - logical drive to be deleted
5003  *
5004  * Delete the specified logical drive. It is the responsibility of the user
5005  * app to let the OS know about this operation.
5006  */
5007 static int
mega_del_logdrv(adapter_t * adapter,int logdrv)5008 mega_del_logdrv(adapter_t *adapter, int logdrv)
5009 {
5010 	DECLARE_WAIT_QUEUE_HEAD(wq);
5011 	unsigned long flags;
5012 	scb_t *scb;
5013 	int rval;
5014 
5015 	ASSERT( !spin_is_locked(adapter->host_lock) );
5016 
5017 	/*
5018 	 * Stop sending commands to the controller, queue them internally.
5019 	 * When deletion is complete, ISR will flush the queue.
5020 	 */
5021 	atomic_set(&adapter->quiescent, 1);
5022 
5023 	/*
5024 	 * Wait till all the issued commands are complete and there are no
5025 	 * commands in the pending queue
5026 	 */
5027 	while( atomic_read(&adapter->pend_cmds) > 0 ) {
5028 
5029 		sleep_on_timeout( &wq, 1*HZ );	/* sleep for 1s */
5030 	}
5031 
5032 	rval = mega_do_del_logdrv(adapter, logdrv);
5033 
5034 
5035 	spin_lock_irqsave(adapter->host_lock, flags);
5036 
5037 	/*
5038 	 * If delete operation was successful, add 0x80 to the logical drive
5039 	 * ids for commands in the pending queue.
5040 	 */
5041 	if (adapter->read_ldidmap) {
5042 		struct list_head *pos;
5043 		list_for_each(pos, &adapter->pending_list) {
5044 			scb = list_entry(pos, scb_t, list);
5045 			if (((mbox_t *)scb->raw_mbox)->logdrv < 0x80 )
5046 				((mbox_t *)scb->raw_mbox)->logdrv += 0x80 ;
5047 		}
5048 	}
5049 
5050 	atomic_set(&adapter->quiescent, 0);
5051 
5052 	mega_runpendq(adapter);
5053 
5054 	spin_unlock_irqrestore(adapter->host_lock, flags);
5055 
5056 	return rval;
5057 }
5058 
5059 
5060 static int
mega_do_del_logdrv(adapter_t * adapter,int logdrv)5061 mega_do_del_logdrv(adapter_t *adapter, int logdrv)
5062 {
5063 	int	rval;
5064 	u8	raw_mbox[sizeof(mbox_t)];
5065 
5066 	memset(raw_mbox, 0, sizeof(raw_mbox));
5067 
5068 	raw_mbox[0] = FC_DEL_LOGDRV;
5069 	raw_mbox[2] = OP_DEL_LOGDRV;
5070 	raw_mbox[3] = logdrv;
5071 
5072 	/* Issue a blocking command to the card */
5073 	rval = issue_scb_block(adapter, raw_mbox);
5074 
5075 	/* log this event */
5076 	if(rval) {
5077 		printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
5078 		return rval;
5079 	}
5080 
5081 	/*
5082 	 * After deleting first logical drive, the logical drives must be
5083 	 * addressed by adding 0x80 to the logical drive id.
5084 	 */
5085 	adapter->read_ldidmap = 1;
5086 
5087 	return rval;
5088 }
5089 
5090 
5091 /**
5092  * mega_get_max_sgl()
5093  * @adapter - pointer to our soft state
5094  *
5095  * Find out the maximum number of scatter-gather elements supported by this
5096  * version of the firmware
5097  */
5098 static void
mega_get_max_sgl(adapter_t * adapter)5099 mega_get_max_sgl(adapter_t *adapter)
5100 {
5101 	unsigned char	raw_mbox[sizeof(mbox_t)];
5102 	mbox_t	*mbox;
5103 
5104 	mbox = (mbox_t *)raw_mbox;
5105 
5106 	memset(raw_mbox, 0, sizeof(raw_mbox));
5107 
5108 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
5109 
5110 	mbox->xferaddr = (u32)adapter->buf_dma_handle;
5111 
5112 	raw_mbox[0] = MAIN_MISC_OPCODE;
5113 	raw_mbox[2] = GET_MAX_SG_SUPPORT;
5114 
5115 
5116 	if( issue_scb_block(adapter, raw_mbox) ) {
5117 		/*
5118 		 * f/w does not support this command. Choose the default value
5119 		 */
5120 		adapter->sglen = MIN_SGLIST;
5121 	}
5122 	else {
5123 		adapter->sglen = *((char *)adapter->mega_buffer);
5124 
5125 		/*
5126 		 * Make sure this is not more than the resources we are
5127 		 * planning to allocate
5128 		 */
5129 		if ( adapter->sglen > MAX_SGLIST )
5130 			adapter->sglen = MAX_SGLIST;
5131 	}
5132 
5133 	return;
5134 }
5135 
5136 
5137 /**
5138  * mega_support_cluster()
5139  * @adapter - pointer to our soft state
5140  *
5141  * Find out if this firmware support cluster calls.
5142  */
5143 static int
mega_support_cluster(adapter_t * adapter)5144 mega_support_cluster(adapter_t *adapter)
5145 {
5146 	unsigned char	raw_mbox[sizeof(mbox_t)];
5147 	mbox_t	*mbox;
5148 
5149 	mbox = (mbox_t *)raw_mbox;
5150 
5151 	memset(raw_mbox, 0, sizeof(raw_mbox));
5152 
5153 	memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
5154 
5155 	mbox->xferaddr = (u32)adapter->buf_dma_handle;
5156 
5157 	/*
5158 	 * Try to get the initiator id. This command will succeed iff the
5159 	 * clustering is available on this HBA.
5160 	 */
5161 	raw_mbox[0] = MEGA_GET_TARGET_ID;
5162 
5163 	if( issue_scb_block(adapter, raw_mbox) == 0 ) {
5164 
5165 		/*
5166 		 * Cluster support available. Get the initiator target id.
5167 		 * Tell our id to mid-layer too.
5168 		 */
5169 		adapter->this_id = *(u32 *)adapter->mega_buffer;
5170 		adapter->host->this_id = adapter->this_id;
5171 
5172 		return 1;
5173 	}
5174 
5175 	return 0;
5176 }
5177 
5178 
5179 
5180 /**
5181  * mega_reorder_hosts()
5182  *
5183  * Hack: reorder the scsi hosts in mid-layer so that the controller with the
5184  * boot device on it appears first in the list.
5185  */
5186 static void
mega_reorder_hosts(void)5187 mega_reorder_hosts(void)
5188 {
5189 	struct Scsi_Host *shpnt;
5190 	struct Scsi_Host *shone;
5191 	struct Scsi_Host *shtwo;
5192 	adapter_t *boot_host;
5193 	int i;
5194 
5195 	/*
5196 	 * Find the (first) host which has it's BIOS enabled
5197 	 */
5198 	boot_host = NULL;
5199 	for (i = 0; i < MAX_CONTROLLERS; i++) {
5200 		if (mega_hbas[i].is_bios_enabled) {
5201 			boot_host = mega_hbas[i].hostdata_addr;
5202 			break;
5203 		}
5204 	}
5205 
5206 	if (!boot_host) {
5207 		printk(KERN_NOTICE "megaraid: no BIOS enabled.\n");
5208 		return;
5209 	}
5210 
5211 	/*
5212 	 * Traverse through the list of SCSI hosts for our HBA locations
5213 	 */
5214 	shone = shtwo = NULL;
5215 	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
5216 		/* Is it one of ours? */
5217 		for (i = 0; i < MAX_CONTROLLERS; i++) {
5218 			if ((adapter_t *) shpnt->hostdata ==
5219 				mega_hbas[i].hostdata_addr) {
5220 				/* Does this one has BIOS enabled */
5221 				if (mega_hbas[i].hostdata_addr == boot_host) {
5222 
5223 					/* Are we first */
5224 					if (!shtwo)	/* Yes! */
5225 						return;
5226 					else	/* :-( */
5227 						shone = shpnt;
5228 				} else {
5229 					if (!shtwo) {
5230 						/* were we here before? xchng
5231 						 * first */
5232 						shtwo = shpnt;
5233 					}
5234 				}
5235 				break;
5236 			}
5237 		}
5238 		/*
5239 		 * Have we got the boot host and one which does not have the
5240 		 * bios enabled.
5241 		 */
5242 		if (shone && shtwo)
5243 			break;
5244 	}
5245 	if (shone && shtwo) {
5246 		mega_swap_hosts (shone, shtwo);
5247 	}
5248 
5249 	return;
5250 }
5251 
5252 
5253 static void
mega_swap_hosts(struct Scsi_Host * shone,struct Scsi_Host * shtwo)5254 mega_swap_hosts (struct Scsi_Host *shone, struct Scsi_Host *shtwo)
5255 {
5256 	struct Scsi_Host *prevtoshtwo;
5257 	struct Scsi_Host *prevtoshone;
5258 	struct Scsi_Host *save = NULL;
5259 
5260 	/* Are these two nodes adjacent */
5261 	if (shtwo->next == shone) {
5262 
5263 		if (shtwo == scsi_hostlist && !shone->next) {
5264 
5265 			/* just two nodes */
5266 			scsi_hostlist = shone;
5267 			shone->next = shtwo;
5268 			shtwo->next = NULL;
5269 		} else if (shtwo == scsi_hostlist) {
5270 			/* first two nodes of the list */
5271 
5272 			scsi_hostlist = shone;
5273 			shtwo->next = shone->next;
5274 			scsi_hostlist->next = shtwo;
5275 		} else if (!shone->next) {
5276 			/* last two nodes of the list */
5277 
5278 			prevtoshtwo = scsi_hostlist;
5279 
5280 			while (prevtoshtwo->next != shtwo)
5281 				prevtoshtwo = prevtoshtwo->next;
5282 
5283 			prevtoshtwo->next = shone;
5284 			shone->next = shtwo;
5285 			shtwo->next = NULL;
5286 		} else {
5287 			prevtoshtwo = scsi_hostlist;
5288 
5289 			while (prevtoshtwo->next != shtwo)
5290 				prevtoshtwo = prevtoshtwo->next;
5291 
5292 			prevtoshtwo->next = shone;
5293 			shtwo->next = shone->next;
5294 			shone->next = shtwo;
5295 		}
5296 
5297 	} else if (shtwo == scsi_hostlist && !shone->next) {
5298 		/* shtwo at head, shone at tail, not adjacent */
5299 
5300 		prevtoshone = scsi_hostlist;
5301 
5302 		while (prevtoshone->next != shone)
5303 			prevtoshone = prevtoshone->next;
5304 
5305 		scsi_hostlist = shone;
5306 		shone->next = shtwo->next;
5307 		prevtoshone->next = shtwo;
5308 		shtwo->next = NULL;
5309 	} else if (shtwo == scsi_hostlist && shone->next) {
5310 		/* shtwo at head, shone is not at tail */
5311 
5312 		prevtoshone = scsi_hostlist;
5313 		while (prevtoshone->next != shone)
5314 			prevtoshone = prevtoshone->next;
5315 
5316 		scsi_hostlist = shone;
5317 		prevtoshone->next = shtwo;
5318 		save = shtwo->next;
5319 		shtwo->next = shone->next;
5320 		shone->next = save;
5321 	} else if (!shone->next) {
5322 		/* shtwo not at head, shone at tail */
5323 
5324 		prevtoshtwo = scsi_hostlist;
5325 		prevtoshone = scsi_hostlist;
5326 
5327 		while (prevtoshtwo->next != shtwo)
5328 			prevtoshtwo = prevtoshtwo->next;
5329 		while (prevtoshone->next != shone)
5330 			prevtoshone = prevtoshone->next;
5331 
5332 		prevtoshtwo->next = shone;
5333 		shone->next = shtwo->next;
5334 		prevtoshone->next = shtwo;
5335 		shtwo->next = NULL;
5336 
5337 	} else {
5338 		prevtoshtwo = scsi_hostlist;
5339 		prevtoshone = scsi_hostlist;
5340 		save = NULL;
5341 
5342 		while (prevtoshtwo->next != shtwo)
5343 			prevtoshtwo = prevtoshtwo->next;
5344 		while (prevtoshone->next != shone)
5345 			prevtoshone = prevtoshone->next;
5346 
5347 		prevtoshtwo->next = shone;
5348 		save = shone->next;
5349 		shone->next = shtwo->next;
5350 		prevtoshone->next = shtwo;
5351 		shtwo->next = save;
5352 	}
5353 	return;
5354 }
5355 
5356 
5357 
5358 #ifdef CONFIG_PROC_FS
5359 /**
5360  * mega_adapinq()
5361  * @adapter - pointer to our soft state
5362  * @dma_handle - DMA address of the buffer
5363  *
5364  * Issue internal comamnds while interrupts are available.
5365  * We only issue direct mailbox commands from within the driver. ioctl()
5366  * interface using these routines can issue passthru commands.
5367  */
5368 static int
mega_adapinq(adapter_t * adapter,dma_addr_t dma_handle)5369 mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
5370 {
5371 	megacmd_t	mc;
5372 
5373 	memset(&mc, 0, sizeof(megacmd_t));
5374 
5375 	if( adapter->flag & BOARD_40LD ) {
5376 		mc.cmd = FC_NEW_CONFIG;
5377 		mc.opcode = NC_SUBOP_ENQUIRY3;
5378 		mc.subopcode = ENQ3_GET_SOLICITED_FULL;
5379 	}
5380 	else {
5381 		mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
5382 	}
5383 
5384 	mc.xferaddr = (u32)dma_handle;
5385 
5386 	if ( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
5387 		return -1;
5388 	}
5389 
5390 	return 0;
5391 }
5392 
5393 
5394 /** mega_internal_dev_inquiry()
5395  * @adapter - pointer to our soft state
5396  * @ch - channel for this device
5397  * @tgt - ID of this device
5398  * @buf_dma_handle - DMA address of the buffer
5399  *
5400  * Issue the scsi inquiry for the specified device.
5401  */
5402 static int
mega_internal_dev_inquiry(adapter_t * adapter,u8 ch,u8 tgt,dma_addr_t buf_dma_handle)5403 mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
5404 		dma_addr_t buf_dma_handle)
5405 {
5406 	mega_passthru	*pthru;
5407 	dma_addr_t	pthru_dma_handle;
5408 	megacmd_t	mc;
5409 	int		rval;
5410 	struct pci_dev	*pdev;
5411 
5412 
5413 	/*
5414 	 * For all internal commands, the buffer must be allocated in <4GB
5415 	 * address range
5416 	 */
5417 	pdev = adapter->dev;
5418 
5419 	pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
5420 			&pthru_dma_handle);
5421 
5422 	if( pthru == NULL ) {
5423 		return -1;
5424 	}
5425 
5426 	pthru->timeout = 2;
5427 	pthru->ars = 1;
5428 	pthru->reqsenselen = 14;
5429 	pthru->islogical = 0;
5430 
5431 	pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
5432 
5433 	pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
5434 
5435 	pthru->cdblen = 6;
5436 
5437 	pthru->cdb[0] = INQUIRY;
5438 	pthru->cdb[1] = 0;
5439 	pthru->cdb[2] = 0;
5440 	pthru->cdb[3] = 0;
5441 	pthru->cdb[4] = 255;
5442 	pthru->cdb[5] = 0;
5443 
5444 
5445 	pthru->dataxferaddr = (u32)buf_dma_handle;
5446 	pthru->dataxferlen = 256;
5447 
5448 	memset(&mc, 0, sizeof(megacmd_t));
5449 
5450 	mc.cmd = MEGA_MBOXCMD_PASSTHRU;
5451 	mc.xferaddr = (u32)pthru_dma_handle;
5452 
5453 	rval = mega_internal_command(adapter, LOCK_INT, &mc, pthru);
5454 
5455 	pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
5456 			pthru_dma_handle);
5457 
5458 	return rval;
5459 }
5460 #endif	// #ifdef CONFIG_PROC_FS
5461 
5462 
5463 /**
5464  * mega_internal_command()
5465  * @adapter - pointer to our soft state
5466  * @ls - the scope of the exclusion lock.
5467  * @mc - the mailbox command
5468  * @pthru - Passthru structure for DCDB commands
5469  *
5470  * Issue the internal commands in interrupt mode.
5471  * The last argument is the address of the passthru structure if the command
5472  * to be fired is a passthru command
5473  *
5474  * lockscope specifies whether the caller has already acquired the lock. Of
5475  * course, the caller must know which lock we are talking about.
5476  *
5477  * Note: parameter 'pthru' is null for non-passthru commands.
5478  */
5479 static int
mega_internal_command(adapter_t * adapter,lockscope_t ls,megacmd_t * mc,mega_passthru * pthru)5480 mega_internal_command(adapter_t *adapter, lockscope_t ls, megacmd_t *mc,
5481 		mega_passthru *pthru )
5482 {
5483 	Scsi_Cmnd	*scmd;
5484 	unsigned long	flags = 0;
5485 	scb_t	*scb;
5486 	int	rval;
5487 
5488 	/*
5489 	 * The internal commands share one command id and hence are
5490 	 * serialized. This is so because we want to reserve maximum number of
5491 	 * available command ids for the I/O commands.
5492 	 */
5493 	down(&adapter->int_mtx);
5494 
5495 	scb = &adapter->int_scb;
5496 	memset(scb, 0, sizeof(scb_t));
5497 
5498 	scmd = &adapter->int_scmd;
5499 	memset(scmd, 0, sizeof(Scsi_Cmnd));
5500 
5501 	scmd->host = adapter->host;
5502 	scmd->buffer = (void *)scb;
5503 	scmd->cmnd[0] = MEGA_INTERNAL_CMD;
5504 
5505 	scb->state |= SCB_ACTIVE;
5506 	scb->cmd = scmd;
5507 
5508 	memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
5509 
5510 	/*
5511 	 * Is it a passthru command
5512 	 */
5513 	if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
5514 
5515 		scb->pthru = pthru;
5516 	}
5517 
5518 	scb->idx = CMDID_INT_CMDS;
5519 
5520 	scmd->state = 0;
5521 
5522 	/*
5523 	 * Get the lock only if the caller has not acquired it already
5524 	 */
5525 	if( ls == LOCK_INT ) spin_lock_irqsave(adapter->host_lock, flags);
5526 
5527 	megaraid_queue(scmd, mega_internal_done);
5528 
5529 	if( ls == LOCK_INT ) spin_unlock_irqrestore(adapter->host_lock, flags);
5530 
5531 	/*
5532 	 * Wait till this command finishes. Do not use
5533 	 * wait_event_interruptible(). It causes panic if CTRL-C is hit when
5534 	 * dumping e.g., physical disk information through /proc interface.
5535 	 * Catching the return value should solve the issue but for now keep
5536 	 * the call non-interruptible.
5537 	 */
5538 #if 0
5539 	wait_event_interruptible(adapter->int_waitq, scmd->state);
5540 #endif
5541 	wait_event(adapter->int_waitq, scmd->state);
5542 
5543 	rval = scmd->result;
5544 	mc->status = scmd->result;
5545 
5546 	/*
5547 	 * Print a debug message for all failed commands. Applications can use
5548 	 * this information.
5549 	 */
5550 	if( scmd->result && trace_level ) {
5551 		printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
5552 			mc->cmd, mc->opcode, mc->subopcode, scmd->result);
5553 	}
5554 
5555 	up(&adapter->int_mtx);
5556 
5557 	return rval;
5558 }
5559 
5560 
5561 /**
5562  * mega_internal_done()
5563  * @scmd - internal scsi command
5564  *
5565  * Callback routine for internal commands.
5566  */
5567 static void
mega_internal_done(Scsi_Cmnd * scmd)5568 mega_internal_done(Scsi_Cmnd *scmd)
5569 {
5570 	adapter_t	*adapter;
5571 
5572 	adapter = (adapter_t *)scmd->host->hostdata;
5573 
5574 	scmd->state = 1; /* thread waiting for its command to complete */
5575 
5576 	/*
5577 	 * See comment in mega_internal_command() routine for
5578 	 * wait_event_interruptible()
5579 	 */
5580 #if 0
5581 	wake_up_interruptible(&adapter->int_waitq);
5582 #endif
5583 	wake_up(&adapter->int_waitq);
5584 
5585 }
5586 
5587 static Scsi_Host_Template driver_template = MEGARAID;
5588 
5589 #include "scsi_module.c"
5590 
5591 /* vi: set ts=8 sw=8 tw=78: */
5592